Specular BRDF Equations in DirectX Shaders

This article provides an overview of the core mathematical formulations used to evaluate specular Bidirectional Reflectance Distribution Functions (BRDF) in modern DirectX HLSL shaders. Modern real-time rendering pipelines primarily implement the microfacet-based Cook-Torrance specular BRDF. Below is an examination of this foundational model, along with the specific equations governing its three key components: the Normal Distribution Function, the Geometric Masking-Shadowing function, and the Fresnel reflectance term.

The Cook-Torrance Specular Model

Physically based rendering (PBR) pipelines in DirectX (Direct3D 11 and 12) evaluate specular highlights using the Cook-Torrance microfacet specular BRDF. The general equation is defined as:

\[f_{\text{spec}}(\mathbf{l}, \mathbf{v}) = \frac{D(\mathbf{h}) \cdot G(\mathbf{l}, \mathbf{v}) \cdot F(\mathbf{v}, \mathbf{h})}{4 (\mathbf{n} \cdot \mathbf{l}) (\mathbf{n} \cdot \mathbf{v})}\]

The vectors involved are normalized:

  • \(\mathbf{n}\): Macro-surface normal vector.
  • \(\mathbf{v}\): View direction vector pointing toward the camera.
  • \(\mathbf{l}\): Light direction vector pointing toward the light source.
  • \(\mathbf{h}\): Half-vector between the view and light directions, computed as \(\mathbf{h} = \frac{\mathbf{l} + \mathbf{v}}{\|\mathbf{l} + \mathbf{v}\|}\).

1. Normal Distribution Function (NDF): GGX / Trowbridge-Reitz

The Normal Distribution Function \(D(\mathbf{h})\) determines the statistical distribution and concentration of microfacets oriented toward the half-vector \(\mathbf{h}\). The standard across DirectX engines is the GGX (Trowbridge-Reitz) distribution due to its realistic specular tails:

\[D_{\text{GGX}}(\mathbf{h}) = \frac{\alpha^2}{\pi \left( (\mathbf{n} \cdot \mathbf{h})^2 (\alpha^2 - 1) + 1 \right)^2}\]

In HLSL implementations, \(\alpha\) is typically mapped as \(\alpha = \text{roughness}^2\) to provide artists with a more linear perceptual control over the surface appearance.

2. Geometric Masking-Shadowing Term (\(G\))

The geometric term accounts for the probability that microfacets self-shadow incoming light or occlude outgoing reflected light. Real-time shaders typically use the Smith formulation paired with the GGX distribution. The Smith model splits the attenuation into two independent terms:

\[G(\mathbf{l}, \mathbf{v}) = G_1(\mathbf{l}) \cdot G_1(\mathbf{v})\]

A common optimization implemented in DirectX is the Schlick-GGX approximation for each direction:

\[G_1(\mathbf{x}) = \frac{\mathbf{n} \cdot \mathbf{x}}{(\mathbf{n} \cdot \mathbf{x})(1 - k) + k}\]

where \(\mathbf{x}\) represents either \(\mathbf{l}\) or \(\mathbf{v}\), and \(k\) is a remapping of roughness adapted for direct analytical light:

\[k_{\text{direct}} = \frac{(\alpha + 1)^2}{8}\]

To reduce instruction count in HLSL, engine developers frequently evaluate a combined Visibility function \(V(\mathbf{l}, \mathbf{v})\), which merges the geometry term and the denominator of the Cook-Torrance model:

\[V(\mathbf{l}, \mathbf{v}) = \frac{G(\mathbf{l}, \mathbf{v})}{4(\mathbf{n} \cdot \mathbf{l})(\mathbf{n} \cdot \mathbf{v})} = \frac{0.5}{(\mathbf{n} \cdot \mathbf{l})\sqrt{(\mathbf{n} \cdot \mathbf{v})^2(1 - \alpha^2) + \alpha^2} + (\mathbf{n} \cdot \mathbf{v})\sqrt{(\mathbf{n} \cdot \mathbf{l})^2(1 - \alpha^2) + \alpha^2}}\]

3. Fresnel Reflectance (\(F\)): Schlick's Approximation

The Fresnel term computes the fraction of light reflected versus refracted at microfacet interfaces based on the viewing angle. Shaders approximate the full Fresnel equations using Schlick's approximation:

\[F(\mathbf{v}, \mathbf{h}) = F_0 + (1 - F_0) \left( 1 - (\mathbf{v} \cdot \mathbf{h}) \right)^5\]

  • \(F_0\) represents the specular reflectance at normal incidence (\(\theta = 0^\circ\)).
  • For dielectrics (non-metals), \(F_0\) is generally around a constant 0.04 (4% reflectance).
  • For conductors (metals), \(F_0\) is set directly from the surface's albedo color using the material's metallic parameter: \(F_0 = \text{lerp}(0.04, \text{albedo}, \text{metallic})\).

Legacy Formulations: Blinn-Phong

Older DirectX shaders and performance-constrained forward passes often replace microfacet equations with the empirical Blinn-Phong formulation:

\[f_{\text{spec}}(\mathbf{l}, \mathbf{v}) = k_s (\mathbf{n} \cdot \mathbf{h})^s\]

where \(k_s\) is the specular color and \(s\) is the specular power (shininess). While computationally inexpensive, it lacks the physical energy conservation and realistic grazing-angle behavior provided by the Cook-Torrance microfacet model.