Vulkan 学习(16)---- 使用 VertexBuffer

Vertex Buffer

创建一个 VertexBuffer 存储 Vertex data,代替之前在 Shader 中使用固定顶点值的做法

Vertex Shader

修改 GLSLVertexShader 如下:
注意这里指定了 input Vertex datalocation 和 格式

#version 450
layout(location = 0) in vec2 inPosition;
layout(location = 1) in vec3 inColor;

layout(location = 0) out vec3 fragColor;
void main() {
    gl_Position = vec4(inPosition, 0.0, 1.0);
    fragColor = inColor;
}

Note:
Shaderlocation 用于表明 VertexData 是如何分布的, 结合输入参数传入的偏移就可以唯一的确定需要的值

Vertex Data

这里指定 Vertex data 如下:

struct Vertex {
    glm::vec2 pos;
    glm::vec3 color;
}

const std::vector<Vertex> vertices = {
    {{-0.5f, 0.5f}, {1.0f, 0.0f, 0.0f}},
    {{-0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}},
    {{0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}},
    {{0.5f, -0.5f }, {1.0f, 0.0f, 0.0f} }
};

glm lib 的数据结构可以和 VertexShader 中的 vec2vec3 结构保持一致,
Vulkan 中的坐标系如下:
vulkan 顶点坐标

VertexInputBindingDescription

VkVertexInputBindingDescription 说明了如何加载 VertexData,包括了步长,绑定索引和加载方式等信息

  • binding:是一个索引,用于标志顶点缓冲区的绑定位置,如果有多个顶点缓冲区(比如一个存储位置数据,一个存储法线数据,就可以用不同的 bindings 进行区分
  • stride: 表示每次加载 Vertex data 的步长
  • inputRate
    VK_VERTEX_INPUT_RATE_VERTEX 表示每个顶点都加载一次数据
    VK_VERTEX_INPUT_RATE_INSTANCE 每个 instance 加载一次数据

参考实现:

static VkVertexInputBindingDescription getBindingDescription() {
    VkVertexInputBindingDescription bindingDescription{};

    // 这里的 binding 默认使用 0, 注意最后要和 vkCmdBindVertexBuffers 参数对应
    // vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
    bindingDescription.binding = 0;
    bindingDescription.stride = sizeof(Vertex);
    bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
    return bindingDescription;
}
VertexInputAttributeDescription

VertexInputAttributeDescription 说明了 VertexData 的处理方式,或者说是 VertexData 的顶点属性

typedef struct VkVertexInputAttributeDescription {
    uint32_t    location;
    uint32_t    binding;
    VkFormat    format;
    uint32_t    offset;
} VkVertexInputAttributeDescription;

location: 这里的 location 必须和 Vertex Shader 中的 location 匹配,每个 location 都必须创建一个 VkVertexInputAttributeDescription 结构

binding: 顶点缓冲区的索引,和前面的 InputBindingDescription 对应

format: 可以和 VertexShader 中的数据类型匹配:

//float: VK_FORMAT_R32_SFLOAT
//vec2:  VK_FORMAT_R32G32_SFLOAT
//vec3:  VK_FORMAT_R32G32B32_SFLOAT
//vec4:  VK_FORMAT_R32G32B32A32_SFLOAT
//ivec2: VK_FORMAT_R32G32_SINT
//uvec4: VK_FORMAT_R32G32B32A32_UINT
//double: VK_FORMAT_R64_SFLOAT

offset: 对应的内容在 VertexData 中的偏移

加载 BindingDescriptionAttributeDescription

BindGraphicPipeline 时,在 pVertexInputState 阶段输入 vertexInputInfo
vertexInputInfo 包含了 bindingDescriptionattributeDescriptions

VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;

auto bindingDescription = Vertex::getBindingDescription();
auto attributeDescriptions = Vertex::getAttributeDescriptions();

vertexInputInfo.vertexBindingDescriptionCount = 1;
vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size());
vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();

// create and bind pipeline 的时候会传入这个参数 vertexInputInfo
VkGraphicsPipelineCreateInfo pipelineInfo{};
.....
pipelineInfo.pVertexInputState = &vertexInputInfo;
VertexBuffer 创建 和 绑定
void createVertexBuffer() {
    VkBufferCreateInfo bufferInfo{};
    bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
    bufferInfo.size = sizeof(vertices[0]) * vertices.size();
    bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;

   //VK_SHARING_MODE_EXCLUSIVE 设备队列独享资源,该资源一次只能被一种设备队列族中的队列访问
    //VK_SHARING_MODE_CONCURRENT 设备队列共享资源,该资源只能一次被多种设备队列族中的队列访问
    bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

    if (vkCreateBuffer(device, &bufferInfo, nullptr, &vertexBuffer) != VK_SUCCESS)
    {
        throw std::runtime_error("failed to create vertex buffer!");
    }

    VkMemoryRequirements memRequirements;
    vkGetBufferMemoryRequirements(device, vertexBuffer, &memRequirements);

    VkMemoryAllocateInfo allocInfo{};
    allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
    allocInfo.allocationSize = memRequirements.size;
    allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);

    if (vkAllocateMemory(device, &allocInfo, nullptr, &vertexBufferMemory) != VK_SUCCESS) {
        throw std::runtime_error("failed to allocate vertex buffer memory!");
    }

    vkBindBufferMemory(device, vertexBuffer, vertexBufferMemory, 0);

    void* data;
    vkMapMemory(device, vertexBufferMemory, 0, bufferInfo.size, 0, &data);
    memcpy(data, vertices.data(), (size_t)bufferInfo.size);
    vkUnmapMemory(device, vertexBufferMemory);
}

步骤如下:

  • 创建 VertexBuffer 对象,注意 usage 要使用 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
  • 获取 memRequirements, 关键是 memory 的大小和类型
  • allocate 对应类型的 memory
  • Mapmemory 并拷贝对应的顶点数据

最后在 recordCommandBuffer 的时候绑定这个 VertexBuffer 就行

vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);

VkBuffer vertexBuffers[] = { vertexBuffer };
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
vkCmdDraw(commandBuffer, static_cast<uint32_t>(vertices.size()), 1, 0, 0);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值