Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by zzz ( 16 years ago )
Texture2D texDepth : register( t0 );
Texture2D texRandomVec : register( t1 );
RWTexture2D<float4> OutputMap : register( u0 );
cbuffer cb : register( b0 )
{
row_major matrix mvp;
float2 screenSize;
float farClipDist;
float nearClipDist;
int groupSizeX;
int groupSizeY;
int groupSizeZ;
int groupBorder;
}
[numthreads(32, 32, 1)]
void CS( uint3 GroupID : SV_GroupID, uint3 DispatchThreadID : SV_DispatchThreadID, uint3 GroupThreadID : SV_GroupThreadID, uint GroupIndex : SV_GroupIndex )
{
float2 sS = screenSize;
int2 texL; // texture location
texL.x = GroupID.x * groupSizeX + GroupThreadID.x;
texL.y = GroupID.y * groupSizeY + GroupThreadID.y;
float2 cTC; // current pixel tex coord
cTC.x = (float)(texL.x) / sS.x;
cTC.y = (float)(texL.y) / sS.y;
float2 randTC = cTC * sS / 4; // random vector tex coord
float cDNrm = texDepth.Load( int3( cTC.x * sS.x, cTC.y * sS.y, 0 ) ).x; // current pixel depth in (0,1)
float cD = cDNrm * ( farClipDist - nearClipDist ) + nearClipDist; // current pixel depth
int3 texRandomCoords = int3( randTC.x * 128,randTC.y * 128, 0 ) % 128; // texture random vector coords
float3 randVec = 2 * texRandomVec.Load( texRandomCoords ).rgb - 1; // random vector
float3 cPos = float3( cTC, cD ); // current pixel position
const int samplersNum = 16; // number of samplers
const float moveVecLenStep = 1 + 1.4 / samplersNum; // move vector length step
int ignoredS = 0; // number of ignored Samplers
float moveVecLenMod = lerp( 4, 1, saturate( cDNrm * 30 ) ); // length modification depending on the current pixel depth
float moveVecLen = sS.x * 0.000003 * moveVecLenMod; // move vector length
float result = 0; // result color
[unroll(samplersNum / 8)]
for(int i = 0; i < ( samplersNum / 8 ); i++)
{
[unroll(2)]
for(int x = -1; x <= 1; x += 2)
{
[unroll(2)]
for(int y = -1; y <= 1; y += 2)
{
[unroll(2)]
for(int z = -1; z <= 1; z += 2)
{
moveVecLen *= moveVecLenStep; // lengthen move vector
float3 rotatedVec = cross( float3( x, y, z ), randVec ); // rotate initial vector
float3 moveVec = normalize (rotatedVec ) * ( moveVecLen ); // compute move vector
float3 sPos = cPos + moveVec; // compute sample pixel position
float clampTexX = clamp( sPos.x * sS.x, 0, sS.x - 1 );
float clampTexY = clamp( sPos.y * sS.y, 0, sS.y - 1 );
float sDNrm = texDepth.Load( int3( clampTexX, clampTexY,0) ).x; // sample pixel depth in (0,1)
float sD = sDNrm * ( farClipDist - nearClipDist ) + nearClipDist; // sample pixel depth
float shadowFactor = ( cD - sD) / sD * 50; // compute shadow factor
float depthTestPass = abs( sD - cD) < (cD * 0.4); // make sure sample depth is close to the current depth
float scaledSF = ( 1 + clamp( shadowFactor, -1, 1 ) ) / 2; // scale shadow factor to (0,1)
float currentRes = lerp( 1, 0.0, scaledSF ); // compute current result
if( depthTestPass == 0 && shadowFactor > 0 ) // ignore sample if it is too far behind current pixel
{
currentRes = 0.0; // cancel previous computation
ignoredS++; // increment number of ignored samples
}
result += currentRes; // add calculations to result
}
}
}
}
result /= ( samplersNum - ignoredS ); // divide result by the number of accepted samples
float re = saturate( result );
re = clamp( re * 2.0, 0, 1 ); // remove white corners
OutputMap[texL.xy] =float4( re, re, re,1 );
}
Revise this Paste
Parent: 12124