vulkanhlslspir-v

Is there a good HLSL to SPIR-V compiler?


I'm writing a voxel engine in C++, and I'm implementing a Vulkan renderer. I've decided to write shaders in HLSL, and translate them via SPIRV-Cross. However, this brings me to a problem - glslang's HLSL compiler does not allow samplers. For example, this pixel shader:

uniform sampler2D tex;
float4 main(float2 uv : TEXCOORD0) : COLOR0 {
    return tex2D(tex, uv);
}

gives this compiler output:

Expected Sampled Image to be of type OpTypeSampledImage
  %19 = OpImageSampleImplicitLod %v4float %17 %18

I don't know if I should write my shaders in GLSL, or use a different library. Any help would be appreciated.


Solution

  • The official DirectX Shader Compiler (that's the one for HLSL 6 and above, not the old DxCompiler) actually supports transforming HLSL into SPIR-V. See their wiki page about it for an explanaition on how to build the compiler with that feature enabled, and how to use it.

    That said, uniform sampler2D tex; is actually not HLSL but GLSL code. In HLSL, you would write

    SamplerState sampler;
    Texture2D tex;
    float4 main(float2 uv : TEXCOORD0) : COLOR0 {
        return tex.Sample(sampler, uv);
    }