Introduction
HLSL, the High-Level Shader Language, is Microsoft’s C-like language for shaders in Direct3D. It is compiled ahead of time to a compiled shader object, which the program hands to the graphics driver. Microsoft’s newer compiler can also produce SPIR-V, so HLSL is used for Vulkan as well, and many game engines write their shaders in it.
Shader source is usually named .hlsl, with shared code in
.hlsli include files. .fx files are HLSL effects, an older
form that groups shaders and render states into techniques and passes, and
.fxh files are includes for them. The .fx extension was also
used for JavaFX Script, an unrelated language that is no longer developed.
Structure
An HLSL file is plain text, with C-style functions, structs and a preprocessor. Inputs
and outputs are marked with semantics after a colon, such as POSITION,
TEXCOORD0 or the system values SV_Position and
SV_Target. Constants from the program come in constant buffers declared with
cbuffer, and textures and samplers are bound to numbered registers. One
file can hold several entry points; the compiler is told which one to compile and for
which shader model, such as vs_5_0 or ps_6_0. Comments and
preprocessor directives work as in C.
cbuffer Frame : register(b0) {
float4x4 worldViewProj;
};
Texture2D colourMap : register(t0);
SamplerState linear0 : register(s0);
struct PSInput {
float4 position : SV_Position;
float2 uv : TEXCOORD0;
};
float4 PSMain(PSInput input) : SV_Target {
return colourMap.Sample(linear0, input.uv);
}
An effect file adds technique and pass blocks, which name
the shaders to use and the states to set. Effects are compiled for a profile such as
fx_5_0.
technique11 Main {
pass P0 {
SetVertexShader(CompileShader(vs_5_0, VSMain()));
SetPixelShader(CompileShader(ps_5_0, PSMain()));
}
}
.fx file.Not described
Known, not yet covered
- Reference: this overview is the only page. The language has no reference pages: the scalar, vector, matrix and resource types, the semantics (
SV_Position,SV_Target,SV_DispatchThreadIDand the rest), attributes ([numthreads],[branch],[unroll]), the intrinsic functions, register and constant buffer packing rules, and the shader models from 1.0 to 6.x. Microsoft’s HLSL documentation describes them. - Reference: effect files are shown by one example, but the effect syntax (
technique,pass, state blocks, annotations) is not described. HLSL 2021 templates and classes, the DirectX Shader Compiler’s SPIR-V attributes ([[vk::binding]]) and the compiled object .cso are not described here. - Viewer: a
//comment that ends in a backslash is not continued onto the next line, so the line after// text \is coloured as code. - Viewer: semantics (
POSITION,SV_Target) and register names (b0,t0) are plain, and so aretemplateandtypename, theuint64_ttype, and an attribute such as[branch](while[unroll(4)]and[numthreads(8, 8, 1)]are coloured as function calls). A struct, class or template parameter you declare is plain. - Viewer: the file is not preprocessed or compiled, so there is no shader model check, no list of entry points and no list of errors. Only the first 1 MiB is read and coloured.
Links
Software
- DirectX Shader Compiler – Microsoft’s
dxc, for shader model 6 and SPIR-V (Windows, Linux) fxc– the older compiler in the Windows SDK, for shader models up to 5.1 (Windows)- Visual Studio – Microsoft’s IDE, which compiles HLSL as part of a build (Windows)
- PIX – debugs the shaders of a running Direct3D 12 program (Windows)
- RenderDoc – graphics debugger that shows the shaders of a running program (Windows, Linux)
Developers
- FX11 – Microsoft’s runtime for Direct3D 11 effects (C++)
- SPIRV-Cross – converts SPIR-V to HLSL, GLSL and Metal source (C++)
- tree-sitter-hlsl – HLSL grammar for the Tree-sitter parser used by editors
Reference
- HLSL – Microsoft’s language reference
- HLSL to SPIR-V – how the DirectX Shader Compiler maps HLSL to Vulkan