// Pixel shader for shadows maps
// with 4 higher than 8-bit precision samples
// and percentage closer filtering


ps.1.4
// Intensity in shadow
def c0, 0, 0, 0, 0
// Intensity in the light
def c1, 1, 1, 1, 1
// Fudge factor for back face occlusion
def c2, 0.1, 0, 0, 0
// Sample weights (1/4)
def c3, 0.25, 0.25, 0.25, 0.25
// Ambient color
def c4, 0.25, 0.25, 0.25, 0.25
// Fetch 4 shadow map samples
texld r0, t0
texld r1, t1
texld r2, t2
texld r3, t3
// Light space depth from vertex shader
texcrd r4.rgb, t4

// Unpack sample 0
mov_d8 r0.r, r0.r
add r0.r, r0.r, r0.g
// Unpack sample 1
mov_d8 r1.r, r1.r
add r0.g, r1.r, r1.g
// Unpack sample 2
mov_d8 r2.r, r2.r
add r0.b, r2.r, r2.g
// Unpack sample 3
mov_d8 r3.r, r3.r
add r1.r, r3.r, r3.g
	
phase

// Restore sample 3
mov r0.a, r1.r
// Compare shadow map samples to surface depth
sub r0, r4.b, r0
cmp r0, -r0, c1, c0
// Occlude back facing prims with respect to light
sub r5.r, v0.a, c2.r
cmp r0, r5.r, r0, c0
// Compute average
dp4 r0, r0, c3

// Modulate with diffuse and add ambient
mad r0, r0, v0, c4
