Heres sikkpins SSAO shader from sikkmod.
!!ARBvp1.0
OPTION ARB_position_invariant;
# input:
#--------------------------------
# texcoord[0] = TEX0 texcoords
#
# local[0] = sample radius, darkening, amount
MOV result.texcoord[0], vertex.texcoord[0];
MUL result.texcoord[1], program.local[0].x, { 1.0, 1.0, 2.0, 4.0 };
MOV result.texcoord[2], program.local[0].y;
MOV result.texcoord[3], program.local[0].z;
END
#=====================================================================================
!!ARBfp1.0
OPTION ARB_precision_hint_nicest;
# texture 0 = _depth
#
# env[0] = 1.0 to _currentRender conversion
# env[1] = fragment.position to 0.0 - 1.0 conversion
OUTPUT oColor = result.color;
ATTRIB fPos = fragment.position;
ATTRIB TC = fragment.texcoord[0];
ATTRIB Radius = fragment.texcoord[1];
ATTRIB Bias = fragment.texcoord[2];
ATTRIB Amount = fragment.texcoord[3];
PARAM nonPoT = program.env[0];
PARAM invRes = program.env[1];
PARAM dcode24 = { 1.0, 0.003921568627450980392156862745098, 0.000015259021896696421759365224689097 };
PARAM farClip = 8192.0;
PARAM inv8 = 0.125;
PARAM HQScale = 0.5;
PARAM DefVal = 0.55;
PARAM randUV = 0.25; # 0.25 = 4x4; 0.0625 = 16x16; 0.015625 = 64x64
# pre-normalized w/ other necessary math done
PARAM vec1 = { 0.01262953713852306359863762957346, 0.01262953713852306359863762957346, 0.01262953713852306359863762957346 };
PARAM vec2 = { -0.02525907427704612719727525914692, -0.02525907427704612719727525914692, -0.02525907427704612719727525914692 };
PARAM vec3 = { -0.03788861141556919079591288872038, -0.03788861141556919079591288872038, 0.03788861141556919079591288872038 };
PARAM vec4 = { -0.05051814855409225439455051829384, 0.05051814855409225439455051829384, -0.05051814855409225439455051829384 };
PARAM vec5 = { -0.06314768569261531799318814786729, 0.06314768569261531799318814786729, 0.06314768569261531799318814786729 };
PARAM vec6 = { 0.07577722283113838159182577744076, -0.07577722283113838159182577744076, -0.07577722283113838159182577744076 };
PARAM vec7 = { 0.08840675996966144519046340701422, -0.08840675996966144519046340701422, 0.08840675996966144519046340701422 };
PARAM vec8 = { 0.10103629710818450878910103658768, 0.10103629710818450878910103658768, -0.10103629710818450878910103658768 };
TEMP depthEnc, depth, random, ssao, uv;
TEMP radius, dist, aspect, diff, res;
TEMP SampleScale, distScaled, DepthRangeScale, DepthTestSoftness, RangeIsInvalid;
TEMP R0, R1, R2, R3, R4, R5, R6, R7, R8;
TEMP S0, S1, S2, S3, S4, S5, S6, S7, S8;
TEMP D1, D2, D3, D4;
# calculate the screen texcoord in the 0.0 to 1.0 range
# MUL R0, fPos, invRes;
# scale by the screen non-power-of-two-adjust
# MUL R0, R0, nonPoT;
# scale by the screen non-power-of-two-adjust
# MUL R0, TC, nonPoT;
MOV uv, 0.0;
MUL uv.xy, fPos, invRes;
# calculate screen width/height
RCP res.x, invRes.x;
RCP res.y, invRes.y;
# calculate aspect ratio
MOV aspect.x, 1.0;
MUL aspect.y, res.x, invRes.y;
# load the depth render
TEX depthEnc, uv, texture[0], 2D;
DP3 depth, depthEnc, dcode24;
# range conversions
MUL depth.z, depth, farClip;
# MAX depth.w, depth.z, 64.0;
# RCP depth.w, depth.w;
MUL_SAT R1.x, depth.z, 0.0078125; # make area smaller if distance less than 128 units
MAD R1.y, depth.z, 0.001953125, 1.0; # make area bigger if distance more than 512 units
MUL R1.x, R1.x, R1.y;
MUL SampleScale.xyz, R1.x, Radius;
RCP R1.x, SampleScale.z;
MUL R1.x, farClip, R1.x;
MUL DepthRangeScale, R1.x, 0.85;
# convert from units into SS units
RCP R1.x, depth.z;
MUL SampleScale.xy, SampleScale, R1.x;
MUL SampleScale.z, SampleScale.z, 0.000244140625;
MUL SampleScale.xy, SampleScale, aspect;
RCP R1.x, SampleScale.z;
MUL DepthTestSoftness, 32.0, R1.x;
# load random vector map
MUL R0.xy, fPos, randUV;
TEX random, R0, texture[2], 2D;
MAD random, random, 2.0, -1.0;
DP3 random.w, random, random;
RSQ random.w, random.w;
MUL random.xyz, random, random.w;
###=================================================
# calculate offsets
DP3 R1.w, random, vec1;
MUL R1.xyz, random, R1.w;
MAD R1.xyz, R1, -2.0, vec1;
DP3 R2.w, random, vec2;
MUL R2.xyz, random, R2.w;
MAD R2.xyz, R2, -2.0, vec2;
DP3 R3.w, random, vec3;
MUL R3.xyz, random, R3.w;
MAD R3.xyz, R3, -2.0, vec3;
DP3 R4.w, random, vec4;
MUL R4.xyz, random, R4.w;
MAD R4.xyz, R4, -2.0, vec4;
DP3 R5.w, random, vec5;
MUL R5.xyz, random, R5.w;
MAD R5.xyz, R5, -2.0, vec5;
DP3 R6.w, random, vec6;
MUL R6.xyz, random, R6.w;
MAD R6.xyz, R6, -2.0, vec6;
DP3 R7.w, random, vec7;
MUL R7.xyz, random, R7.w;
MAD R7.xyz, R7, -2.0, vec7;
DP3 R8.w, random, vec8;
MUL R8.xyz, random, R8.w;
MAD R8.xyz, R8, -2.0, vec8;
MUL S1.xyz, R1, SampleScale;
MUL S2.xyz, R2, SampleScale;
MUL S3.xyz, R3, SampleScale;
MUL S4.xyz, R4, SampleScale;
MUL S5.xyz, R5, SampleScale;
MUL S6.xyz, R6, SampleScale;
MUL S7.xyz, R7, SampleScale;
MUL S8.xyz, R8, SampleScale;
# snap texcoord to pixel center
MUL S1.xy, S1, res;
FLR S1.xy, S1;
MUL S2.xy, S2, res;
FLR S2.xy, S2;
MUL S3.xy, S3, res;
FLR S3.xy, S3;
MUL S4.xy, S4, res;
FLR S4.xy, S4;
MUL S5.xy, S5, res;
FLR S5.xy, S5;
MUL S6.xy, S6, res;
FLR S6.xy, S6;
MUL S7.xy, S7, res;
FLR S7.xy, S7;
MUL S8.xy, S8, res;
FLR S8.xy, S8;
# do first sample set
MAD_SAT R1.xy, S1, invRes, uv;
MAD_SAT R2.xy, S2, invRes, uv;
MAD_SAT R3.xy, S3, invRes, uv;
MAD_SAT R4.xy, S4, invRes, uv;
MAD_SAT R5.xy, S5, invRes, uv;
MAD_SAT R6.xy, S6, invRes, uv;
MAD_SAT R7.xy, S7, invRes, uv;
MAD_SAT R8.xy, S8, invRes, uv;
# sample depth texture
TEX R1, R1, texture[0], 2D;
TEX R2, R2, texture[0], 2D;
TEX R3, R3, texture[0], 2D;
TEX R4, R4, texture[0], 2D;
TEX R5, R5, texture[0], 2D;
TEX R6, R6, texture[0], 2D;
TEX R7, R7, texture[0], 2D;
TEX R8, R8, texture[0], 2D;
# decode 24-bit depth
DP3 D1.x, R1, dcode24;
DP3 D1.y, R2, dcode24;
DP3 D1.z, R3, dcode24;
DP3 D1.w, R4, dcode24;
DP3 D2.x, R5, dcode24;
DP3 D2.y, R6, dcode24;
DP3 D2.z, R7, dcode24;
DP3 D2.w, R8, dcode24;
ADD D1.x, D1.x, S1.z;
ADD D1.y, D1.y, S2.z;
ADD D1.z, D1.z, S3.z;
ADD D1.w, D1.w, S4.z;
ADD D2.x, D2.x, S5.z;
ADD D2.y, D2.y, S6.z;
ADD D2.z, D2.z, S7.z;
ADD D2.w, D2.w, S8.z;
# do second sample set
MUL S1.xyz, S1, HQScale;
MUL S2.xyz, S2, HQScale;
MUL S3.xyz, S3, HQScale;
MUL S4.xyz, S4, HQScale;
MUL S5.xyz, S5, HQScale;
MUL S6.xyz, S6, HQScale;
MUL S7.xyz, S7, HQScale;
MUL S8.xyz, S8, HQScale;
MAD_SAT R1.xy, S1, invRes, uv;
MAD_SAT R2.xy, S2, invRes, uv;
MAD_SAT R3.xy, S3, invRes, uv;
MAD_SAT R4.xy, S4, invRes, uv;
MAD_SAT R5.xy, S5, invRes, uv;
MAD_SAT R6.xy, S6, invRes, uv;
MAD_SAT R7.xy, S7, invRes, uv;
MAD_SAT R8.xy, S8, invRes, uv;
# sample depth texture
TEX R1, R1, texture[0], 2D;
TEX R2, R2, texture[0], 2D;
TEX R3, R3, texture[0], 2D;
TEX R4, R4, texture[0], 2D;
TEX R5, R5, texture[0], 2D;
TEX R6, R6, texture[0], 2D;
TEX R7, R7, texture[0], 2D;
TEX R8, R8, texture[0], 2D;
# decode 24-bit depth
DP3 D3.x, R1, dcode24;
DP3 D3.y, R2, dcode24;
DP3 D3.z, R3, dcode24;
DP3 D3.w, R4, dcode24;
DP3 D4.x, R5, dcode24;
DP3 D4.y, R6, dcode24;
DP3 D4.z, R7, dcode24;
DP3 D4.w, R8, dcode24;
ADD D3.x, D3.x, S1.z;
ADD D3.y, D3.y, S2.z;
ADD D3.z, D3.z, S3.z;
ADD D3.w, D3.w, S4.z;
ADD D4.x, D4.x, S5.z;
ADD D4.y, D4.y, S6.z;
ADD D4.z, D4.z, S7.z;
ADD D4.w, D4.w, S8.z;
###=============================================
# calculate occlusion
ADD dist, depth.x, -D1;
MUL distScaled, dist, DepthRangeScale;
ABS_SAT R1, distScaled;
MOV_SAT R2, distScaled;
ADD R1, R1, R2;
MUL RangeIsInvalid, R1, 0.5;
MUL_SAT R1, -dist, DepthTestSoftness;
LRP ssao, RangeIsInvalid, DefVal, R1;
ADD dist, depth.x, -D2;
MUL distScaled, dist, DepthRangeScale;
ABS_SAT R1, distScaled;
MOV_SAT R2, distScaled;
ADD R1, R1, R2;
MUL RangeIsInvalid, R1, 0.5;
MUL_SAT R1, -dist, DepthTestSoftness;
LRP R1, RangeIsInvalid, DefVal, R1;
ADD ssao, ssao, R1;
ADD dist, depth.x, -D3;
MUL distScaled, dist, DepthRangeScale;
ABS_SAT R1, distScaled;
MOV_SAT R2, distScaled;
ADD R1, R1, R2;
MUL RangeIsInvalid, R1, 0.5;
MUL_SAT R1, -dist, DepthTestSoftness;
LRP R1, RangeIsInvalid, DefVal, R1;
ADD ssao, ssao, R1;
ADD dist, depth.x, -D4;
MUL distScaled, dist, DepthRangeScale;
ABS_SAT R1, distScaled;
MOV_SAT R2, distScaled;
ADD R1, R1, R2;
MUL RangeIsInvalid, R1, 0.5;
MUL_SAT R1, -dist, DepthTestSoftness;
LRP R1, RangeIsInvalid, DefVal, R1;
ADD ssao, ssao, R1;
###=================================================
DP4 ssao.x, ssao, inv8;
ADD ssao.x, ssao, -Bias;
MOV oColor, depthEnc.xxyy;
LRP_SAT oColor.x, Amount, ssao.x, 0.9;
END