Introduction to Shaders & HLSL
A shader is a small program that runs on the GPU.
1.1 What is a shader?
A shader is a small program that runs on the GPU. Shaders run massively in parallel: a vertex shader runs once per vertex, a pixel shader once per covered pixel (or sample), a compute shader once per thread in a dispatch — millions of invocations per frame. GPUs execute these invocations in lockstep groups called waves (AMD: wavefronts of 32/64, NVIDIA: warps of 32, Intel: SIMD8/16/32).
1.2 What is HLSL?
HLSL (High-Level Shader Language) is Microsoft's C-like shading language, created for Direct3D. Today it is used far beyond Direct3D:
Direct3D 9/10/11/12 on Windows and Xbox.
Vulkan — DXC compiles HLSL to SPIR-V; many Vulkan engines author in HLSL.
Unity (ShaderLab + HLSL), Unreal Engine (.usf/.ush + material graph), Godot (via tooling), O3DE (AZSL, an HLSL dialect), The Forge, bgfx, and most AAA engines.
Metal / OpenGL / WebGPU via cross-compilation (SPIRV-Cross, Metal Shader Converter, Naga/Tint) and Slang, an HLSL-compatible superset.
1.3 Key characteristics
C syntax with built-in vector and matrix types (float3, float4x4) and swizzles (color.rgb, pos.xzy).
Semantics (: SV_Position, : TEXCOORD0) bind variables to pipeline inputs/outputs.
Resource objects with methods: tex.Sample(samp, uv), buf.Load(i).
Rich intrinsic library (dot, cross, normalize, lerp, saturate, mul).
No pointers (in standard HLSL), no recursion (except DXR TraceRay/CallShader recursion managed by the runtime), no dynamic allocation, no strings in shaders.
Modern HLSL (DXC, HLSL 2021) adds templates, operator overloading, bitfields, and stricter C++-like semantics.
1.4 HLSL vs GLSL at a glance
float3, int2, half4 vec3, ivec2 mul(M, v) (the * operator is component-wise) lerp(a, b, t) mix(a, b, t) Clamp to [0,1] clamp(x, 0.0, 1.0)
tex.Sample(s, uv) (separate texture & sampler) texture(sampler2D, uv) semantics on struct members/parameters
in/out variables, layout(location) cbuffer / ConstantBuffer<T> uniform, UBO blocks any function name, chosen at compile time dFdx, dFdy Compute shared memory