Recursive AI

Fix an HLSL t0 and s0 binding collision on Vulkan

OneShader is a free, open-source HLSL compiler library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

register(t0) and register(s0) both map to set 0, binding 0. Put [[vk::binding(1, 0)]] on the sampler to move it to binding 1.

Terminal
dotnet add package RecursiveAI.OneShader

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneShader;
 
// sample.hlsl is shown below (in the zip).
var result = ShaderCompiler.CompileFile(
    "sample.hlsl");
if (result.Reflection is not { } reflection
    || !result.Success)
{
    foreach (var diagnostic in result.Diagnostics)
        Console.Error.WriteLine(diagnostic);
    return 1;
}
foreach (var resource in reflection.Resources)
{
    var slot = resource.Slots.First(
        s => s.Target == CompileTarget.SpirV);
    Console.WriteLine($"{resource.Name}: set " +
        $"{slot.Set}, binding {slot.Binding}");
}
return 0;

Unzip the example, then dotnet run.

Text
Tex: set 0, binding 0
Samp: set 0, binding 1

sample.hlsl

HLSL
Texture2D<float4> Tex : register(t0);
[[vk::binding(1, 0)]]
SamplerState Samp : register(s0);
[shader("pixel")]
float4 PS(float2 uv : TEXCOORD0) : SV_Target
{
    return Tex.Sample(Samp, uv);
}

The full program

The zip compiles the shader without the attribute first, then compiles the fixed source and prints both Vulkan slots. It compiles to SPIR-V without running on a GPU.

C#
using OneShader;
 
string hlsl = File.ReadAllText("sample.hlsl");
string colliding = hlsl.Replace("[[vk::binding(1, 0)]]", "");
var broken = ShaderCompiler.Compile(colliding, "sample.hlsl");
Console.WriteLine($"Without the attribute: {broken.Success}");
foreach (var diagnostic in broken.Diagnostics)
    Console.WriteLine(diagnostic.Message);
Console.WriteLine("With the attribute:");
var result = ShaderCompiler.Compile(hlsl, "sample.hlsl");
if (!result.Success || result.Reflection is null)
{
    foreach (var diagnostic in result.Diagnostics)
        Console.Error.WriteLine(diagnostic);
    return 1;
}
foreach (var resource in result.Reflection.Resources)
{
    var slot = resource.Slots.First(
        s => s.Target == CompileTarget.SpirV);
    Console.WriteLine($"{resource.Name}: set " +
        $"{slot.Set}, binding {slot.Binding}");
}
return 0;
Text
Without the attribute: False
The resources 'Tex' and 'Samp' both use set 0, binding 0. Add [[vk::binding(binding, set)]] to one of them, or give them different register numbers (t, s, b and u registers share binding numbers on Vulkan).
With the attribute:
Tex: set 0, binding 0
Samp: set 0, binding 1

If another resource already occupies set 0, binding 1, pick a free slot instead; OneShader reports the conflicting resource names and slot.

Variations

Renumber the sampler instead. Replace its attribute and declaration with this line to get the same Vulkan slots:

HLSL
SamplerState Samp : register(s1);

The short answer

register(t0) and register(s0) both map to Vulkan set 0, binding 0 in OneShader. Leave the texture at t0 and put [[vk::binding(1, 0)]] on the sampler to move it to binding 1 in the same set. The attribute takes binding first, set second and overrides the Vulkan slot implied by the register. Check the SPIR-V slots in reflection rather than reading the numbers off the register declarations.

What are descriptor sets in Vulkan?

A descriptor set groups the descriptors that tell Vulkan which resources the shader can access. The set number identifies the group, and the binding number identifies a slot inside that group. The resource type tells Vulkan what belongs at the slot, not which slot to use. A separately declared texture and sampler in one set therefore need distinct slots, even though the HLSL register letters differ. For the shader above, match the host descriptor set layout and writes to a sampled image at binding 0 and a sampler at binding 1.

How do HLSL register spaces map to Vulkan descriptor sets?

Without an explicit attribute, OneShader uses the register space as the set and the register number as the binding. For example, register(s0, space1) means binding 0 in set 1, not binding 1 in set 0. That would avoid the collision too, but changes which set the host must bind. If the texture and sampler belong together in set 0, change only the binding as shown above. The layout rules also explain how OneShader assigns slots to resources with neither a register nor an attribute.

All oneshader recipes