#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.xy + 1.0) / 2.0; //output.tex = input.tex; output.tex.y = 1.0 - output.tex.y; //input.pos.y *= -1; 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;*/ float3 pos = tex2D(radionSampler, float2(input.tex.x + 0.25 * dataColumnWidth, input.tex.y + 0.5 / 4096.0) ).xyz; float3 dir = tex2D(radionSampler, float2(input.tex.x + 0.75 * dataColumnWidth, input.tex.y + 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 dd = dist / lightFarPlane; lightVPos.xy = (lightVPos.xy + 1.0) / 2.0; lightVPos.y = 1.0 - lightVPos.y; float storedDist = tex2D(shadowMap, lightVPos.xy).r; visibility = dd < storedDist + DIST_BIAS; } } //visibility = 1; //visibility = 1.0 - visibility; //visibility = 1.0 + 0.00001 * visibility; float spotFalloff = (dot(-diff, lightDir) - lightAngleCos) / (1.0 - lightAngleCos); //float spotFalloff = max( dot(-diff, lightDir), 0) + 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;*/ float3 pos = tex2D(radionSampler, float2(input.tex.x + 0.25 * dataColumnWidth, input.tex.y + 0.5 / 4096.0) ).xyz; float3 dir = tex2D(radionSampler, float2(input.tex.x + 0.75 * dataColumnWidth, input.tex.y + 0.5 / 4096.0) ).xyz; // float3 pos = tex2D(radionSampler, float2(0.25, input.tex.y + 0.5 / 4096.0) ).xyz; // float3 dir = tex2D(radionSampler, float2(0.75, input.tex.y + 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 = visibility * 0.0000001 + 1.0; visibility *= 1.0 / (lightAttenuation.y + dist * lightAttenuation.z + dist * dist * lightAttenuation.w); float4 ret = visibility * cosb * lightPower * lightColor; //ret = 0.00000001 * ret * nRadionColumns + (dist<25.0);//float4(pos,1);//input.tex.y * 4096.0; 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); //coord1 = coord1 * 0.00000001 + float2(0.5, (currentEntryPoint + 0.5)/4096); //coord2 = coord1 + float2(0.25, 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; float4 ret = 0; //if(abs(input.tex.x - 2*halfPixel*2)