SPIRV-cross get vertex layout info for Vulkan

53 Views Asked by At

I need to reflect glsl shader and get vertex layout to automatically create VkPipelineVertexInputStateCreateInfo. I have done it that way:

spirv_cross::Compiler compiler(src);
spirv_cross::ShaderResources resources = compiler.get_shader_resources();

// only for vertex stage
for (const auto& resource : resources.stage_inputs)
{
    uint32_t location = compiler.get_decoration(resource.id, spv::DecorationLocation);
    auto type = compiler.get_type(resource.type_id);
}
// After calculate offsets and stride

Everything worked fine, until I wrote shader that does not use some vertex attributes. Vertex inputs look like this:

layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec2 a_TexCoords;
layout(location = 2) in vec3 a_Normal;
layout(location = 3) in vec3 a_Tangent;
layout(location = 4) in vec3 a_Bitangent;
layout(location = 5) in ivec4 a_BoneIDs;
layout(location = 6) in vec4 a_Weights;

This shader uses only a_Position, a_BoneIDs, a_Weights. SPIRV-cross optimizes out other attributes and give only those 3. And now I cant calculate offsets for each attribute and cant get stride for whole vertex. I tried to get offset this way: uint32 offset = compiler.get_decoration(resource.id, spv::DecorationOffset), but it always give me zero. Even if I had offset for each attribute I would not be able to build a stride, because I cant get last attribute also.

So is there a way to get enough information from spirv-cross?

0

There are 0 best solutions below