Shader Programming

🟢 Beginner Level (The Fundamentals)

  • The Shader Pipeline:
    1. Vertex Shader: Runs once per vertex. Outputs the screen-space position (gl_Position / SV_Position).
    2. Fragment (Pixel) Shader: Runs once per pixel. Outputs the final color of that pixel.
  • Shader Languages:
    • HLSL: High-Level Shader Language (DirectX, widely used in Unreal/Unity).
    • GLSL: OpenGL Shading Language (OpenGL, Vulkan).
    • MSL: Metal Shading Language (Apple, based on C++).
    • WGSL: WebGPU Shading Language (WebGPU).
  • Shaders are built on linear algebra. You must master:


Vector & Matrix Math

ConceptUsage
Dot Product dot(A, B)Lighting intensity. If Normal and Light Direction point the same way, dot = 1.0 (bright). Opposite = -1.0 (dark).
Cross Product cross(A, B)Finding a vector perpendicular to two others (calculating normals).
Transformations M * VMultiplying a Vertex (V) by a Model-View-Projection Matrix (M) moves it from 3D space to the 2D screen.

🟡 Intermediate Level (Lighting & Texturing)

  • Modern shaders do not use simple “Phong” lighting. They use PBR to accurately simulate how light behaves.

Physically Based Rendering (PBR)

  • Albedo: The base color without lighting.
  • Normal Map: Baking high-poly details into a texture to fake light scattering.
  • Roughness / Metallic: Dictates how light scatters (specular lobe).

UV Coordinates and Samplers

  • UVs: A 2D coordinate system (0.0 to 1.0) defining how to wrap a 2D texture onto a 3D model.
  • Samplers: An object that defines how a texture is read (e.g., nearest-neighbor for pixel art, linear blending for smooth gradients, anisotropic filtering for sharp angles).

🟠 Advanced Level (Compute & Post-Processing)

Compute Shaders

  • A Compute Shader operates independently of the graphics pipeline. It has no vertices or pixels.
  • It operates on a grid of “Threads” and “Workgroups”.
  • Uses: Particle physics, fluid simulation, culling unseen objects before rendering, or processing geometry.
  • Effects applied after the 3D scene is rendered.

Post-Processing (Screen-Space Shaders)

  • Bloom: Extract bright pixels, blur them heavily, add back to original image.
  • SSAO (Screen Space Ambient Occlusion): Use the depth buffer to calculate where crevices are and darken them.
  • Tone Mapping: Converting HDR values (colors brighter than 1.0) down to LDR (0.0 - 1.0) so monitors can display them without ugly clipping.

🔴 Super Advanced Level (Ray Tracing & Wave Intrinsics)

  • Used in DXR and Vulkan RT:

Hardware Ray Tracing Shaders

  • Ray Generation: The origin point. Spawns rays into the scene.
  • Closest Hit: Triggered when the ray hits the nearest geometry. Calculates lighting here.
  • Miss: Triggered if the ray flies off into the skybox.
  • Advanced GPU programming technique. Instead of threads reading/writing to memory (which is slow), threads within the same “Warp” or “Wavefront” (usually 32 or 64 threads executing locally) can share data directly through registers using commands like WaveActiveSum() or subgroupAdd(). This is massively faster for reductions and prefix-sums.


Wave Intrinsics (Subgroup Operations)

More Learn