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.
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.
struct PSInput {
float4 position : SV_Position;
float4 colour : COLOR;
};
float4 PSMain(PSInput input) : SV_Target {
return input.colour;
}
Links
Software
- DirectX Shader Compiler (
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, which compiles HLSL as part of a build (Windows)
- PIX, which debugs the shaders of a running Direct3D 12 program (Windows)
- RenderDoc (Windows, Linux)
Reference
- HLSL, Microsoft’s reference