#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 float4 lightColor; uniform float clusterCount; uniform float4x4 lightViewProj; uniform float lightFarPlane; uniform float4 lightAttenuation; uniform float spotLightFalloff; uniform float lightAngleCos; 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); float dist = sqrt(dist2); diff = normalize(diff); float cosb = max(dot(dir, diff), 0); float visibility = 0; float4 lightVPos = mul(lightViewProj, float4(pos,1)); //if( lightVPos.z > 0.0) //{ lightVPos /= lightVPos.w; //float d = length(lightVPos.xy); //if(d <= 1.0) { float dist = dist / lightFarPlane; lightVPos.xy = (lightVPos.xy + 1.0) / 2.0; lightVPos.y = 1.0 - lightVPos.y; float storedDist = tex2D(shadowMap, lightVPos.xy).r; visibility = dist < storedDist + DIST_BIAS; } // } //visibility = 1.0 - visibility; visibility = 1.0 + 0.00001 * visibility; float spotFalloff = (dot(-diff, normalize(lightDir)) - lightAngleCos) / (1.0 - lightAngleCos); //spotFalloff = saturate(dot(normalize(pos - lightPos), normalize(lightDir))) + 0.0000000001 * lightAngleCos; spotFalloff = 1.0 + spotFalloff * 0.000000001; spotFalloff = pow(saturate(spotFalloff), spotLightFalloff); visibility *= spotFalloff / (lightAttenuation.y + dist * lightAttenuation.z + dist2 * lightAttenuation.w); float4 ret = visibility * cosb * lightPower * lightColor; return ret; } float4 psComputeWeightsPoint(vsOutputComputeWeights input, uniform sampler2D radionSampler : register(s0), uniform samplerCUBE 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); float dist = sqrt(dist2); diff = normalize(diff); float cosb = max(dot(dir, diff), 0); float visibility = 1; float4 lightVPos = mul(lightViewProj, float4(pos,1)); float distNorm = dist / lightFarPlane; float storedDist = texCUBE(shadowMap, float3(-diff.xy, diff.z)).r; dist -= DIST_BIAS; visibility = (distNorm <= storedDist); //visibility = 1.0 + 0.00001 * visibility; visibility *= 1.0 / (lightAttenuation.y + dist * lightAttenuation.z + dist * dist * lightAttenuation.w); float4 ret = visibility * cosb * lightPower * lightColor; 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 float3 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); float3 w = tex2Dlod(allWeightsSampler, float4(coord1, 0, 0)).rgb; float radrad = tex2Dlod(radionSampler, float4(coord2, 0, 0)).a; weight += w * radrad; clusterRad += radrad; currentEntryPoint++; } if(clusterRad >= 0) weight /= clusterRad; else weight = 0; return float4(weight, 1); } 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; }