当前位置: 首页 > news >正文

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);

相关文章:

  • Windows系统安装Docker(Win10系统升级,然后安装)
  • 区块链:跨链协的技术突破与产业重构
  • ASP.NET MVC​ 入门与提高指南六
  • 强化学习_Paper_2017_Curiosity-driven Exploration by Self-supervised Prediction
  • 树状结构转换工具类
  • 如何解决服务器文件丢失或损坏的问题
  • 大型连锁酒店集团数据仓库应用示例
  • [STM32] 4-2 USART与串口通信(2)
  • 从零构建 MCP Server 与 Client:打造你的第一个 AI 工具集成应用
  • 【AI提示词】二八法则专家
  • (31)VTK C++开发示例 ---绘制立方体
  • springboot集成Lucene详细使用
  • 【文献分享】Modelling the species-area提供数据和代码
  • 短视频矩阵系统贴牌开发实战:批量剪辑文件夹功能设计与实现
  • JAVA SE 反射,枚举与lambda表达式
  • 基于go的简单管理系统(增删改查)
  • Sehll编程的函数于数组
  • nacos和redis本地启动
  • 黑群晖Moments视频无缩略图,安装第三方ffmpeg解决
  • 搭建PCDN大节点,服务器该怎么配
  • 国务院安委办、应急管理部进一步调度部署“五一”假期安全防范工作
  • 光明日报:回应辅警“转正”呼声,是一门政民互动公开课
  • 五一“拼假”催热超长假期,热门酒店民宿一房难求
  • 西湖大学本科招生新增三省两市,首次面向上海招生
  • 药明康德一季度净利增长89%,在手订单增超四成至523亿元
  • 报告显示2024年全球军费开支增幅达冷战后最大