1

I have a vertex and a fragment shader, written in HLSL, which I compile into a SPIR-V binary using the DirectX Shader Compiler.

They both use a common cbuffer (which I want to treat as a uniform buffer) ubo. In order to construct a suitable VkDescriptorSetLayout, I want to use SPIRV-Reflect to obtain the necessary information about ubo.

While, I can create separate spv_reflect::ShaderModule's vertex_shader and fragment_shader from the binary of my SPIR-V binaries of my shaders, I can only retrieve the information about existing descriptor bindings by calling EnumerateDescriptorBindings() on both spv_reflect::ShaderModule's individidually.

They both yield the existence of ubo. But what I would want to do is tell the API that both shaders use the same uniform buffer ubo, i.e. create a VkDescriptorSetLayoutBinding for ubo and specify VK_SHADER_STAGE_VERTEX_BIT and VK_SHADER_STAGE_FRAGMENT_BIT for the stageFlags field.

But in order to do this I need to detect that both descriptor bindings yielded by the individual calls to EnumerateDescriptorBindings are referring to the same buffer. How can I do that?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107

1 Answers1

1

I don't know how HLSL handles resource bindings, but presumably it has some mechanism to specify how a given resource is associated with whatever D3D12's resource model is. Presumably, the compiler you're using to generate SPIR-V either maps the D3D12 resource model to the Vulkan descriptor set/binding model or it makes a modification to HLSL language itself to allow the user to specify Vulkan descriptor sets/bindings for resources.

Either way, the SPIR-V generated by this process must have a descriptor set and a binding index for every descriptor resource it uses.

If the VS and the FS are both using the same UBO, then that means they must be written to use the same set+binding for that UBO. So when you're reflecting over multiple SPIR-V modules to aggregate the resources they use, you will need to aggregate these resources by set+binding. If two modules declare a resource with the same set and binding, then they are the same resource in the layout.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Thank you for your answer. So, if two `SpvReflectDescriptorBinding`'s (possibly obtained from different shader modules) have the same set and binding, then I aggregate them into one `VkDescriptorSetLayoutBinding`, right? Is it guaranteed that the numbering of the "set" indices goes from "zero" to "the total count of sets minus one"? Or can this value be freely set at some point? – 0xbadf00d Jan 18 '21 at 16:31