# PRT Equations (Direct3D 9)

To fully understand a shader that implements PRT, it is useful to derive the formula the shader uses to calculate exit radiance.

To start off, the following equation is the general equation to calculate exit radiance resulting from direct lighting on a diffuse object with arbitrary distant lighting.

where:

ParameterDescription
RpThe exit radiance at vertex p. Evaluated at every vertex on the mesh.
pdThe albedo of the surface.
piA constant, used as an energy conservation normalization factor.
Vp(s)A binary visibility function for point p. It is 1 if the point can see the light, 0 if not.
Hnp(s)The cosine term from Lambert's law. Equal to max((Np· s), 0) where Np is the surface normal at point p.
sThe variable that integrates over the sphere.

Using spherical basis functions, such as spherical harmonics, the following equation approximates the lighting environment.

where:

ParameterDescription
L(s) The lighting environment (source radiance).
iAn integer that sums over the number of basis functions.
OThe order of spherical harmonics.
liA coefficient.
Yi(s)Some basis function over the sphere.

The collection of these coefficients, L', provides the optimal approximation for function L(s) with the basis functions Y(s). Substituting and distributing yields the following equation.

The integral of Yi(s)Vp(s)Hnp(s) is a transfer coefficient tpi that the simulator precomputes for every vertex on the mesh. Substituting this yields the following equation.

Changing this to vector notation yields the following uncompressed equation to calculate exit radiance for each channel.

where:

ParameterDescription
RpThe exit radiance at vertex p.
pdThe albedo of the surface.
L'The vector of li, and is the projection of the source radiance into the spherical harmonic basis functions. This is an order2 vector of spherical harmonic coefficients.
TpAn order2 transfer vector for vertex p. The simulator divides the transfer coefficients by p.

Both of these vectors are an order2 vector of spherical harmonic coefficients, so notice that this is simply a dot product. Depending on the order, the dot can be expensive so compression can be used. An algorithm called Clustered Principal Component Analysis (CPCA) efficiently compresses the data. This enables the use of a higher-order spherical harmonic approximation which results in sharper shadows.

CPCA provides the following equation to approximate the transfer vector.

where:

ParameterDescription
TpThe transfer vector for vertex p.
MkThe mean for cluster k.
jAn integer that sums over the number of PCA vectors.
NThe number of PCA vectors.
wpjThe jth PCA weight for point p.
BkjThe jth PCA basis vector for cluster k.

A cluster is simply some number of vertices that share the same mean vector. How to get the cluster mean, the PCA weights, the PCA basis vectors, and the cluster ids for the vertices is discussed below.

Substituting these two equations yields:

Then distributing the dot product yields the following equation.

Because both (Mk· L') and (Bkj· L') are constant per vertex, the sample calculates these values with the CPU and passes them as constants into the vertex shader; because wpj changes for each vertex, the sample stores this per-vertex data in the vertex buffer.