当前位置: 首页 > 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);
http://www.xdnf.cn/news/235711.html

相关文章:

  • Python魔法函数深度解析
  • 关于epoch、batch_size等参数含义,及optimizer.step()的含义及数学过程
  • pinia实现数据持久化插件pinia-plugin-persist-uni
  • 10、属性和数据处理---c++17
  • 突破SQL注入字符转义的实战指南:绕过技巧与防御策略
  • 《Ultralytics HUB:开启AI视觉新时代的密钥》
  • Stack--Queue 栈和队列
  • 前端基础之《Vue(13)—重要API》
  • Dify Agent节点的信息收集策略示例
  • 【效率提升】Vibe Coding时代如何正确使用输入法:自定义短语实现Prompt快捷输入
  • windows系统 压力测试技术
  • Github开通第三方平台OAuth登录及Java对接步骤
  • ES使用之查询方式
  • 空域伦理与AI自主边界的系统建构
  • 《冰雪传奇点卡版》:第二大陆介绍!
  • Java 手写jdbc访问数据库
  • 代理脚本——爬虫
  • 【MySQL】索引特性
  • JGQ511机械振打袋式除尘器实验台装置设备
  • 鸿蒙的StorageLink
  • BT137-ASEMI机器人功率器件专用BT137
  • 【Hive入门】Hive性能优化:执行计划分析EXPLAIN命令的使用
  • 41 python http之requests 库
  • spring中的@Configuration注解详解
  • pytorch的cuda版本依据nvcc --version与nvidia-smi
  • 企业架构之旅(4):TOGAF ADM 中业务架构——企业数字化转型的 “骨架”
  • 永磁同步电机控制算法--单矢量模型预测电流控制MPCC
  • # 实现中文情感分析:基于TextRNN的模型部署与应用
  • 软件测试52讲学习分享:深入理解单元测试
  • BI平台是什么意思?一文讲清BI平台的具体应用!