#define SAMPLECUTDIST2 0.01 #define WEIGHTCUTOFF 0.01 #define DIST_BIAS 0.005 uniform int nRadionColumns; uniform float3 lightPos; uniform float3 lightDir; uniform float lightPower; uniform float clusterCount; uniform float4x4 lightViewProj; uniform float lightFarPlane; struct vsInputComputeWeights { float4 pos : POSITION; float2 tex : TEXCOORD0; }; struct vsOutputComputeWeights { float4 pos : POSITION; float2 tex : TEXCOORD0; }; vsOutputComputeWeights vsComputeWeights(vsInputComputeWeights input) { vsOutputComputeWeights output = (vsOutputComputeWeights)0; output.pos = input.pos; output.tex = (input.pos + 1.0) / 2.0; //output.tex = input.tex; output.tex.y = 1.0 - output.tex.y; return output; } float4 psComputeWeights(vsOutputComputeWeights input, uniform sampler2D radionSampler : register(s0), uniform sampler2D shadowMap : register(s1)) : COLOR0 { float dataColumnWidth = 1.0 / (float)nRadionColumns; int bushIndex = input.tex.x * 4096 + input.tex.y * nRadionColumns * 4096; int werx = bushIndex % nRadionColumns; int wery = bushIndex / nRadionColumns; float3 pos = tex2D(radionSampler, float2((werx + 0.25) * dataColumnWidth, (wery + 0.5) / 4096.0) ).xyz; float3 dir = tex2D(radionSampler, float2((werx + 0.75) * dataColumnWidth, (wery + 0.5) / 4096.0) ).xyz; dir = normalize(dir); float3 diff = lightPos - pos; float dist2 = dot(diff, diff); if(dist2 < SAMPLECUTDIST2) dist2 = SAMPLECUTDIST2; diff = normalize(diff); float cosa = max(dot(lightDir, -diff), 0); float cosb = max(dot(dir, diff), 0); float4 lightVPos = mul(lightViewProj, float4(pos,1)); float dist = sqrt(dist2) / lightFarPlane; lightVPos /= lightVPos.w; lightVPos.xy = (lightVPos.xy + 1.0) / 2.0; lightVPos.y = 1.0 - lightVPos.y; float storedDist = tex2D(shadowMap, lightVPos.xy).r; dist -= DIST_BIAS; float visibility = (dist <= storedDist); //float visibility = 1; //visibility = visibility * 0.00001 + 1.0; float4 ret = visibility * pow(cosa, 9) * cosb / dist2 * lightPower; return ret; } float4 psSumWeights(vsOutputComputeWeights input, uniform sampler2D radionSampler : register(s0), uniform sampler2D entryPointCountSampler : register(s1), uniform sampler2D allWeightsSampler : register(s2)) : COLOR0 { float halfPixel = 0.5 / clusterCount; float iCluster = input.tex.x + halfPixel; int entryPointCount = tex2D(entryPointCountSampler, float2(iCluster, 0.25)); int currentEntryPoint = tex2D(entryPointCountSampler, float2(iCluster, 0.75)); //sum entrypoint weights float weight = 0; float clusterRad = 0; for(int i = 0; i < entryPointCount; i++) { float dataColumnWidth = 1.0 / (float)nRadionColumns; int werx = currentEntryPoint % nRadionColumns; int wery = currentEntryPoint / nRadionColumns; float2 coord1 = float2((werx + 0.5) * dataColumnWidth, (wery + 0.5) / 4096.0); float2 coord2 = coord1 + float2(0.25 * dataColumnWidth, 0); float w = tex2Dlod(allWeightsSampler, float4(coord1, 0, 0)).r; float radrad = tex2Dlod(radionSampler, float4(coord1, 0, 0)).a; weight += w * radrad; clusterRad += radrad; currentEntryPoint++; } if(clusterRad >= 0) weight /= clusterRad; else weight = 0; return weight; } void EntryPointDisplayVS(float4 position :POSITION0, float4 color :COLOR0, uniform float4x4 worldViewProj, out float4 hPos:POSITION, out float2 texCoord: TEXCOORD0) { hPos = mul(worldViewProj, position); texCoord = color.xy; } float4 EntryPointDisplayPS(float2 texCoord : TEXCOORD0, uniform sampler2D weightTexture : register(s0)):COLOR0 { //return texCoord.y; return texCoord.r; }