不使用基于推送常量的顶点属性
创始人
2024-12-28 22:00:35
0

使用基于推送常量的顶点属性

在OpenGL中,我们可以通过顶点属性将数据传递给着色器。而推送常量则可以在渲染过程中实时改变着色器的属性。如果不使用基于推送常量的顶点属性,将会使得渲染过程变得更加复杂和低效。

以下是一个使用基于推送常量的顶点属性的示例:

// 设置推送常量
struct PushConstants {
    mat4 model_view_projection;
} push_constants;

// 设置管道布局,并将推送常量绑定到管道
VkPushConstantRange push_constant_range = {};
push_constant_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
push_constant_range.offset = 0;
push_constant_range.size = sizeof(PushConstants);

VkPipelineLayoutCreateInfo pipeline_layout_ci = {};
pipeline_layout_ci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipeline_layout_ci.setLayoutCount = 1;
pipeline_layout_ci.pSetLayouts = &descriptor_set_layout;
pipeline_layout_ci.pushConstantRangeCount = 1;
pipeline_layout_ci.pPushConstantRanges = &push_constant_range;

vkCreatePipelineLayout(device, &pipeline_layout_ci, nullptr, &pipeline_layout);

// 定义顶点属性结构体
struct Vertex {
    glm::vec3 position;
    glm::vec3 normal;
    glm::vec2 tex_coord;
};

// 创建顶点缓冲区
VkBuffer vertex_buffer;
VkDeviceMemory vertex_memory;

VkBufferCreateInfo buffer_ci = {};
buffer_ci.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
buffer_ci.size = sizeof(vertices);
buffer_ci.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
buffer_ci.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
vkCreateBuffer(device, &buffer_ci, nullptr, &vertex_buffer);

VkMemoryRequirements memory_requirements;
vkGetBufferMemoryRequirements(device, vertex_buffer, &memory_requirements);

VkMemoryAllocateInfo memory_alloc_info = {};
memory_alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
memory_alloc_info.allocationSize = memory_requirements.size;
memory_alloc_info.memoryTypeIndex = 0;

vkAllocateMemory(device, &memory_alloc_info, nullptr, &vertex_memory);
vkBindBufferMemory(device, vertex_buffer, vertex_memory, 0);

// 绑定推送常量和顶点缓冲区到着色器
VkDescriptorSet descriptor_sets;
vkAllocateDescriptorSets(descriptor_pool, 1, &descriptor_set, &descriptor_sets);

VkWriteDescriptorSet write_descriptor_set = {};
write_descriptor_set.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
write_descriptor

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...