Vulkan aceita SPIR-V, não GLSL — e SPIR-V não se importa com a
linguagem que o gerou. O dxc da Microsoft (o DirectX Shader Compiler)
emite SPIR-V diretamente, então uma codebase HLSL existente pode alimentar um
renderer Vulkan sem reescrita. As lacunas estão no mapeamento: anotações de
register vs descriptor bindings, CBV vs UBO, e o onipresente
padrão row_major vs column_major.
Compilando
build.sh
dxc -spirv -T ps_6_0 -E main shader.hlsl -Fo shader.spv
# ^SPIR-V out ^profile ^entry
# Vulkan-target knobs you usually want:
dxc -spirv -fvk-use-gl-layout # std430/GL layout rules
-fvk-use-dx-layout # or keep DX offsets
-fvk-b-shift 0 0 # shift register b* → binding
-fvk-t-shift 0 0 # t* shift
-fvk-s-shift 0 0 # s* shift
-fvk-u-shift 0 0 # u* (UAV) shift
-fspv-target-env=vulkan1.3 # SPIR-V env target
-Zi # debug info → RenderDoc readableAs flags -fvk-*-shift remapeiam os register spaces do HLSL (b0, t0, u0,
s0) para a numeração plana binding = N do Vulkan — essencial quando o
mesmo shader alimenta ambas as APIs.
O mapeamento que morde
mapping.txt
HLSL: cbuffer Params : register(b0) → VK: layout(set=0,binding=0) UBO
HLSL: Texture2D t : register(t0) → VK: layout(set=0,binding=1) texture2D
HLSL: SamplerState s : register(s0) → VK: layout(set=0,binding=2) sampler
HLSL: RWTexture2D u : register(u0) → VK: layout(set=0,binding=3) image2D
HLSL: register(b0, space1) → VK: set=1, binding=0space → set, register → binding — as flags -fvk-*-shift
os deslocam para múltiplos spaces coexistirem num único set Vulkan.
As verdadeiras pegadinhas
pitfalls.txt
row_major vs column_major:
HLSL cbuffer defaults column_major, GLSL std140 row_major —
pick ONE with -fvk-use-gl-layout or #pragma pack_matrix —
silent transposed matrices are the #1 porting bug
push constants:
HLSL has no push constant concept — dxc maps a special
cbuffer to VK's push_constant block if you mark it
[[vk::push_constant]] — or use -fvk-use-dx-layout and treat
it as a normal UBO
vertex inputs:
HLSL uses semantic names (POSITION, TEXCOORD0); Vulkan needs
numbered locations — use [[vk::location(N)]] or map the
attributes in the pipeline creation
combined sampler:
HLSL separates t0 (texture) and s0 (sampler) — Vulkan
texture2D+sample() uses one descriptor; dxc handles this but
the binding numbers differshader.hlsl
// minimal Vulkan-friendly HLSL:
[[vk::binding(0,0)]] cbuffer Params { row_major float4x4 mvp; };
[[vk::binding(1,0)]] Texture2D tex;
[[vk::binding(2,0)]] SamplerState smp;
[[vk::location(0)]] float4 main([[vk::location(0)]] float3 pos : POSITION,
[[vk::location(1)]] float2 uv : TEXCOORD0)
: SV_Position { return mul(mvp, float4(pos,1)); }Quando vale a pena
- Portar um renderer D3D existente — mantém a fonte HLSL, emite SPIR-V, sem manutenção de fonte dupla.
- Cross-compilar uma shader lib — um
.hlsl→ DXIL para D3D12 + SPIR-V para Vulkan;spirv-cross(outro post) para a reflection. - Features de DXIL — dxc tem wave ops,
WaveMultiPrefixOpe intrínsecos HLSL novos que o GLSL do glslang só expõe via extensões.
Regras práticas
-fspv-target-env=vulkan1.3importa — o padrão mira uma versão SPIR-V mais antiga com menos capabilities.-Zi+spirv-valno CI — o SPIR-V do DXC é válido mas o mapeamento de atributos precisa das flags-fvk-*explicitadas.[[vk::binding(N,S)]]+[[vk::location(N)]]são os atributos específicos do DXC — eles sobrescrevem as heurísticas de register/space limpamente.- Divergência de
row_majoré a assassina silenciosa — toda matriz entregue ao shader fica errada se os layouts discordam; faça assert disso uma vez na construção do pipeline.
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow