I'm working with vulkan 1.2 and i try to reflect the input, ouput of the shader here:
#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;
}
The goal is to automatically set correctly the layout(location)
line in the pipeline Currently, i reflect my shader like this:
void Shader::reflectInput(const std::vector<char>& code)
{
SpvReflectShaderModule spvModule = {};
SpvReflectResult result = spvReflectCreateShaderModule(sizeof(code), reinterpret_cast<const uint32_t*>(code.data()), &spvModule);
assert(result == SPV_REFLECT_RESULT_SUCCESS);
uint32_t count = 0;
result = spvReflectEnumerateInputVariables(&spvModule, &count, NULL);
assert(result == SPV_REFLECT_RESULT_SUCCESS);
std::vector<SpvReflectInterfaceVariable*> input_vars(count);
result = spvReflectEnumerateInputVariables(&spvModule, &count, input_vars.data());
assert(result == SPV_REFLECT_RESULT_SUCCESS);
count = 0;
result = spvReflectEnumerateOutputVariables(&spvModule, &count, NULL);
assert(result == SPV_REFLECT_RESULT_SUCCESS);
std::vector<SpvReflectInterfaceVariable*> output_vars(count);
result = spvReflectEnumerateOutputVariables(&spvModule, &count, output_vars.data());
assert(result == SPV_REFLECT_RESULT_SUCCESS);
if (spvModule.shader_stage == SPV_REFLECT_SHADER_STAGE_VERTEX_BIT)
{
binding_description.binding = 0;
binding_description.stride = 0;
binding_description.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
attribute_descriptions.reserve(input_vars.size());
for (size_t i_var = 0; i_var < input_vars.size(); ++i_var)
{
const SpvReflectInterfaceVariable& refl_var = *(input_vars[i_var]);
if (refl_var.decoration_flags & SPV_REFLECT_DECORATION_BUILT_IN)
continue;
VkVertexInputAttributeDescription attr_desc{};
attr_desc.location = refl_var.location;
attr_desc.binding = binding_description.binding;
attr_desc.format = static_cast<VkFormat>(refl_var.format);
attr_desc.offset = 0; // final offset computed below after sorting.
attribute_descriptions.push_back(attr_desc);
}
std::sort(std::begin(attribute_descriptions), std::end(attribute_descriptions),
[](const VkVertexInputAttributeDescription& a, const VkVertexInputAttributeDescription& b) {
return a.location < b.location; });
// Compute final offsets of each attribute, and total vertex stride.
for (auto& attribute : attribute_descriptions) {
uint32_t format_size = FormatSize(attribute.format);
attribute.offset = binding_description.stride;
binding_description.stride += format_size;
}
}
}
uint32_t Shader::FormatSize(VkFormat format)
{
uint32_t result = 0;
switch (format) {
case VK_FORMAT_UNDEFINED: result = 0; break;
case VK_FORMAT_R4G4_UNORM_PACK8: result = 1; break;
case VK_FORMAT_R4G4B4A4_UNORM_PACK16: result = 2; break;
case VK_FORMAT_B4G4R4A4_UNORM_PACK16: result = 2; break;
case VK_FORMAT_R5G6B5_UNORM_PACK16: result = 2; break;
case VK_FORMAT_B5G6R5_UNORM_PACK16: result = 2; break;
case VK_FORMAT_R5G5B5A1_UNORM_PACK16: result = 2; break;
case VK_FORMAT_B5G5R5A1_UNORM_PACK16: result = 2; break;
case VK_FORMAT_A1R5G5B5_UNORM_PACK16: result = 2; break;
case VK_FORMAT_R8_UNORM: result = 1; break;
case VK_FORMAT_R8_SNORM: result = 1; break;
case VK_FORMAT_R8_USCALED: result = 1; break;
case VK_FORMAT_R8_SSCALED: result = 1; break;
case VK_FORMAT_R8_UINT: result = 1; break;
case VK_FORMAT_R8_SINT: result = 1; break;
case VK_FORMAT_R8_SRGB: result = 1; break;
case VK_FORMAT_R8G8_UNORM: result = 2; break;
case VK_FORMAT_R8G8_SNORM: result = 2; break;
case VK_FORMAT_R8G8_USCALED: result = 2; break;
case VK_FORMAT_R8G8_SSCALED: result = 2; break;
case VK_FORMAT_R8G8_UINT: result = 2; break;
case VK_FORMAT_R8G8_SINT: result = 2; break;
case VK_FORMAT_R8G8_SRGB: result = 2; break;
case VK_FORMAT_R8G8B8_UNORM: result = 3; break;
case VK_FORMAT_R8G8B8_SNORM: result = 3; break;
case VK_FORMAT_R8G8B8_USCALED: result = 3; break;
case VK_FORMAT_R8G8B8_SSCALED: result = 3; break;
case VK_FORMAT_R8G8B8_UINT: result = 3; break;
case VK_FORMAT_R8G8B8_SINT: result = 3; break;
case VK_FORMAT_R8G8B8_SRGB: result = 3; break;
case VK_FORMAT_B8G8R8_UNORM: result = 3; break;
case VK_FORMAT_B8G8R8_SNORM: result = 3; break;
case VK_FORMAT_B8G8R8_USCALED: result = 3; break;
case VK_FORMAT_B8G8R8_SSCALED: result = 3; break;
case VK_FORMAT_B8G8R8_UINT: result = 3; break;
case VK_FORMAT_B8G8R8_SINT: result = 3; break;
case VK_FORMAT_B8G8R8_SRGB: result = 3; break;
case VK_FORMAT_R8G8B8A8_UNORM: result = 4; break;
case VK_FORMAT_R8G8B8A8_SNORM: result = 4; break;
case VK_FORMAT_R8G8B8A8_USCALED: result = 4; break;
case VK_FORMAT_R8G8B8A8_SSCALED: result = 4; break;
case VK_FORMAT_R8G8B8A8_UINT: result = 4; break;
case VK_FORMAT_R8G8B8A8_SINT: result = 4; break;
case VK_FORMAT_R8G8B8A8_SRGB: result = 4; break;
case VK_FORMAT_B8G8R8A8_UNORM: result = 4; break;
case VK_FORMAT_B8G8R8A8_SNORM: result = 4; break;
case VK_FORMAT_B8G8R8A8_USCALED: result = 4; break;
case VK_FORMAT_B8G8R8A8_SSCALED: result = 4; break;
case VK_FORMAT_B8G8R8A8_UINT: result = 4; break;
case VK_FORMAT_B8G8R8A8_SINT: result = 4; break;
case VK_FORMAT_B8G8R8A8_SRGB: result = 4; break;
case VK_FORMAT_A8B8G8R8_UNORM_PACK32: result = 4; break;
case VK_FORMAT_A8B8G8R8_SNORM_PACK32: result = 4; break;
case VK_FORMAT_A8B8G8R8_USCALED_PACK32: result = 4; break;
case VK_FORMAT_A8B8G8R8_SSCALED_PACK32: result = 4; break;
case VK_FORMAT_A8B8G8R8_UINT_PACK32: result = 4; break;
case VK_FORMAT_A8B8G8R8_SINT_PACK32: result = 4; break;
case VK_FORMAT_A8B8G8R8_SRGB_PACK32: result = 4; break;
case VK_FORMAT_A2R10G10B10_UNORM_PACK32: result = 4; break;
case VK_FORMAT_A2R10G10B10_SNORM_PACK32: result = 4; break;
case VK_FORMAT_A2R10G10B10_USCALED_PACK32: result = 4; break;
case VK_FORMAT_A2R10G10B10_SSCALED_PACK32: result = 4; break;
case VK_FORMAT_A2R10G10B10_UINT_PACK32: result = 4; break;
case VK_FORMAT_A2R10G10B10_SINT_PACK32: result = 4; break;
case VK_FORMAT_A2B10G10R10_UNORM_PACK32: result = 4; break;
case VK_FORMAT_A2B10G10R10_SNORM_PACK32: result = 4; break;
case VK_FORMAT_A2B10G10R10_USCALED_PACK32: result = 4; break;
case VK_FORMAT_A2B10G10R10_SSCALED_PACK32: result = 4; break;
case VK_FORMAT_A2B10G10R10_UINT_PACK32: result = 4; break;
case VK_FORMAT_A2B10G10R10_SINT_PACK32: result = 4; break;
case VK_FORMAT_R16_UNORM: result = 2; break;
case VK_FORMAT_R16_SNORM: result = 2; break;
case VK_FORMAT_R16_USCALED: result = 2; break;
case VK_FORMAT_R16_SSCALED: result = 2; break;
case VK_FORMAT_R16_UINT: result = 2; break;
case VK_FORMAT_R16_SINT: result = 2; break;
case VK_FORMAT_R16_SFLOAT: result = 2; break;
case VK_FORMAT_R16G16_UNORM: result = 4; break;
case VK_FORMAT_R16G16_SNORM: result = 4; break;
case VK_FORMAT_R16G16_USCALED: result = 4; break;
case VK_FORMAT_R16G16_SSCALED: result = 4; break;
case VK_FORMAT_R16G16_UINT: result = 4; break;
case VK_FORMAT_R16G16_SINT: result = 4; break;
case VK_FORMAT_R16G16_SFLOAT: result = 4; break;
case VK_FORMAT_R16G16B16_UNORM: result = 6; break;
case VK_FORMAT_R16G16B16_SNORM: result = 6; break;
case VK_FORMAT_R16G16B16_USCALED: result = 6; break;
case VK_FORMAT_R16G16B16_SSCALED: result = 6; break;
case VK_FORMAT_R16G16B16_UINT: result = 6; break;
case VK_FORMAT_R16G16B16_SINT: result = 6; break;
case VK_FORMAT_R16G16B16_SFLOAT: result = 6; break;
case VK_FORMAT_R16G16B16A16_UNORM: result = 8; break;
case VK_FORMAT_R16G16B16A16_SNORM: result = 8; break;
case VK_FORMAT_R16G16B16A16_USCALED: result = 8; break;
case VK_FORMAT_R16G16B16A16_SSCALED: result = 8; break;
case VK_FORMAT_R16G16B16A16_UINT: result = 8; break;
case VK_FORMAT_R16G16B16A16_SINT: result = 8; break;
case VK_FORMAT_R16G16B16A16_SFLOAT: result = 8; break;
case VK_FORMAT_R32_UINT: result = 4; break;
case VK_FORMAT_R32_SINT: result = 4; break;
case VK_FORMAT_R32_SFLOAT: result = 4; break;
case VK_FORMAT_R32G32_UINT: result = 8; break;
case VK_FORMAT_R32G32_SINT: result = 8; break;
case VK_FORMAT_R32G32_SFLOAT: result = 8; break;
case VK_FORMAT_R32G32B32_UINT: result = 12; break;
case VK_FORMAT_R32G32B32_SINT: result = 12; break;
case VK_FORMAT_R32G32B32_SFLOAT: result = 12; break;
case VK_FORMAT_R32G32B32A32_UINT: result = 16; break;
case VK_FORMAT_R32G32B32A32_SINT: result = 16; break;
case VK_FORMAT_R32G32B32A32_SFLOAT: result = 16; break;
case VK_FORMAT_R64_UINT: result = 8; break;
case VK_FORMAT_R64_SINT: result = 8; break;
case VK_FORMAT_R64_SFLOAT: result = 8; break;
case VK_FORMAT_R64G64_UINT: result = 16; break;
case VK_FORMAT_R64G64_SINT: result = 16; break;
case VK_FORMAT_R64G64_SFLOAT: result = 16; break;
case VK_FORMAT_R64G64B64_UINT: result = 24; break;
case VK_FORMAT_R64G64B64_SINT: result = 24; break;
case VK_FORMAT_R64G64B64_SFLOAT: result = 24; break;
case VK_FORMAT_R64G64B64A64_UINT: result = 32; break;
case VK_FORMAT_R64G64B64A64_SINT: result = 32; break;
case VK_FORMAT_R64G64B64A64_SFLOAT: result = 32; break;
case VK_FORMAT_B10G11R11_UFLOAT_PACK32: result = 4; break;
case VK_FORMAT_E5B9G9R9_UFLOAT_PACK32: result = 4; break;
default:
break;
}
return result;
}
I'm just following the example Spirv-reflect give, nevertheless, it doesn't find any information. So maybe i'm using wrong (it's probably that^^). Even the simple information spvModule.shader_stage
is equal to 0 when i try to do it.
I precise that i translate my shader to spirv code. And it's the spv file i give to my reflection code.
Do you have ideas of what i could do wrong? Bad shader wrting? Bad use of the library?
Aucun commentaire:
Enregistrer un commentaire