0xbadf00d
0xbadf00d

Reputation: 18178

Using SPIRV-Reflect, how do I know that two descriptor bindings from different shaders are actually referring to the same buffer?

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?

Upvotes: 1

Views: 1234

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473447

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.

Upvotes: 1

Related Questions