Vulkan:VkVertexInputBindingDescription总是错误的几何着色器

时间:2017-02-20 20:07:39

标签: vertex-buffer vulkan geometry-shader

我正在尝试在几何着色器中实现广告牌四边形来渲染粒子效果。几何着色器输入是点(vec3),其输出是具有位置和UV坐标(vec3,vec2)的三角形条带。我尝试了两种顶点输入绑定,但都不起作用。

如果我像这样设置顶点绑定:

VkVertexInputBindingDescription binding_desc[2] = {};
binding_desc[0].binding = 0;
binding_desc[0].stride = sizeof(glm::vec3);
binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;

binding_desc[1].binding = 1;
binding_desc[1].stride = sizeof(glm::vec2);
binding_desc[1].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;

VkVertexInputAttributeDescription attribute_desc[2] = {};
attribute_desc[0].location = 0;
attribute_desc[0].binding = binding_desc[0].binding;
attribute_desc[0].format = VK_FORMAT_R32G32B32_SFLOAT;
attribute_desc[0].offset = offsetof(vert_shader_vertex, pos);

attribute_desc[1].location = 1;
attribute_desc[1].binding = binding_desc[1].binding;
attribute_desc[1].format = VK_FORMAT_R32G32_SFLOAT;
attribute_desc[1].offset = offsetof(vert_shader_vertex, uv);

调用vkCmdDraw时出现以下错误:

  

错误[默认] DS :(对象0)(代码24)管道状态对象   (0x3c)期望这个命令缓冲区的顶点绑定索引1   应该通过vkCmdBindVertexBuffers设置。这是因为   索引1处的VkVertexInputBindingDescription结构   pVertexBindingDescriptions的绑定值为1.

但是,如果我将其设置为:

VkVertexInputBindingDescription binding_desc[1] = {};
binding_desc[0].binding = 0;
binding_desc[0].stride = sizeof(glm::vec3);
binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;

VkVertexInputAttributeDescription attribute_desc[1] = {};
attribute_desc[0].location = 0;
attribute_desc[0].binding = binding_desc[0].binding;
attribute_desc[0].format = VK_FORMAT_R32G32B32_SFLOAT;
attribute_desc[0].offset = offsetof(vert_shader_vertex, pos);

调用vkCreateGraphicsPipelines时出现此错误:

  

错误[默认] SC :(对象0)(代码3)顶点着色器消耗输入   位置1但未提供

  • VkVertexInputBindingDescription是否描述几何着色器或顶点着色器的输入?
  • 我的顶点缓冲区中是否需要“虚拟”UV坐标作为占位符?
  • 我的几何着色器是否可能未激活,我该如何确认?
  • 这两种方法中哪一种是正确的,我该如何解决相应的错误?
  • 另外,我是Vulkan的新手,所以欢迎对着色器发表评论。

几何着色器

#version 450

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout (points) in;
layout (triangle_strip, max_vertices = 4) out;

layout (location = 0) in vec3 inPos[];

layout (location = 0) out vec3 outPos;
layout (location = 1) out vec2 outUV;

layout (push_constant) uniform constants_t {
    vec3 up;
    vec3 right;
    mat4x4 world;
    mat4x4 projection;
} constants;

void main(void)
{
    const vec3 pos = gl_in[0].gl_Position.xyz;
    const vec3 up = constants.up;
    const vec3 right = constants.right;

    outPos = pos + up - right;
    outUV = vec2(0, 0);
    EmitVertex();

    outPos = pos + up + right;
    outUV = vec2(1, 0);
    EmitVertex();

    outPos = pos - up - right;
    outUV = vec2(0, 1);
    EmitVertex();

    outPos = pos - up + right;
    outUV = vec2(1, 1);
    EmitVertex();

    EndPrimitive();
}

顶点着色器

#version 450

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout (location = 0) in vec3 inPos;
layout (location = 1) in vec2 inUV;

layout (location = 0) out vec4 outPos;
layout (location = 1) out vec2 outUV;

layout (push_constant) uniform constants_t {
    vec3 up;
    vec3 right;
    mat4x4 world;
    mat4x4 projection;
} constants;

void main(void) {
    outUV = inUV;
    outPos = vec4(inPos.xyz, 1.0) * constants.world * constants.projection;
}

vkCmdBindVertexBuffers

VkBuffer vertex_buffers[1] = {vertexBuffer};
VkDeviceSize vertex_offset[1] = {0};
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertex_buffers, vertex_offset);

1 个答案:

答案 0 :(得分:2)

vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertex_buffers, vertex_offset);

这表示你将一个缓冲区绑定到索引0.但是你在创建它时告诉管道你将绑定两个缓冲区。

不要欺骗Vulkan; 它始终知道(当您使用验证图层时;))。

您可能希望两个顶点属性都使用相同的缓冲区对象。我从使用offsetof计算它们的相对偏移量的事实中推断出这一点。如果这是你的意图,那么你应该有两个顶点属性使用相同的缓冲区绑定

  

VkVertexInputBindingDescription是否描述了几何着色器或顶点着色器的输入?

它无法描述GS的输入,因为第一个管道着色器阶段是顶点着色器。并且无法创建没有VS的图形管道。