source: GTP/trunk/App/Demos/Illum/Ogre/Media/PMDemo/PathMapWeightCompute.hlsl @ 2246

Revision 2246, 3.8 KB checked in by szirmay, 17 years ago (diff)
RevLine 
[2188]1#define SAMPLECUTDIST2 0.01
[2217]2#define WEIGHTCUTOFF   0.01
3#define DIST_BIAS      0.005
[2188]4
5uniform int nRadionColumns;
6uniform float3 lightPos;
7uniform float3 lightDir;
[2246]8uniform float lightPower;
[2202]9uniform float clusterCount;
[2217]10uniform float4x4 lightViewProj;
11uniform float lightFarPlane;
[2188]12
13struct vsInputComputeWeights
14{
15    float4      pos                     : POSITION;
16    float2      tex                     : TEXCOORD0;
17};
18
19struct vsOutputComputeWeights
20{
21    float4      pos                     : POSITION;
22    float2      tex                     : TEXCOORD0;
23};
24
25vsOutputComputeWeights
26        vsComputeWeights(vsInputComputeWeights input)
27{
28    vsOutputComputeWeights output = (vsOutputComputeWeights)0;
29    output.pos = input.pos;
[2213]30    output.tex = (input.pos + 1.0) / 2.0;
[2217]31    //output.tex = input.tex;
[2207]32    output.tex.y = 1.0 - output.tex.y;
[2217]33   
[2188]34    return output;
35}
36
[2217]37float4 psComputeWeights(vsOutputComputeWeights input,
38                                                uniform sampler2D radionSampler : register(s0),
39                                                uniform sampler2D shadowMap : register(s1)) : COLOR0
[2188]40{
41        float dataColumnWidth = 1.0 / (float)nRadionColumns;
42        int bushIndex = input.tex.x * 4096 + input.tex.y * nRadionColumns * 4096;
43        int werx = bushIndex % nRadionColumns;
44        int wery = bushIndex / nRadionColumns;
45        float3 pos = tex2D(radionSampler, float2((werx + 0.25) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;
46        float3 dir = tex2D(radionSampler, float2((werx + 0.75) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;
[2246]47        dir = normalize(dir);
[2188]48       
49        float3 diff = lightPos - pos;
50        float dist2 = dot(diff, diff);
51        if(dist2 < SAMPLECUTDIST2)
52                dist2 = SAMPLECUTDIST2;
53        diff = normalize(diff);
[2202]54        float cosa = max(dot(lightDir, -diff), 0);
55        float cosb = max(dot(dir, diff), 0);   
[2188]56       
[2217]57       
58        float4 lightVPos = mul(lightViewProj, float4(pos,1));
59        float dist = sqrt(dist2) / lightFarPlane;
60        lightVPos /= lightVPos.w;
61        lightVPos.xy = (lightVPos.xy + 1.0) / 2.0;
62        lightVPos.y = 1.0 - lightVPos.y;
63        float storedDist = tex2D(shadowMap, lightVPos.xy).r;
64        dist -= DIST_BIAS;
65        float visibility = (dist <= storedDist);       
[2188]66
[2217]67        //float visibility = 1;
68        //visibility = visibility * 0.00001 + 1.0;
[2188]69       
[2246]70        float4 ret = visibility * pow(cosa, 9) * cosb / dist2 * lightPower;
[2213]71        return ret;
[2202]72}
73
[2217]74float4 psSumWeights(vsOutputComputeWeights input,
75                                        uniform sampler2D radionSampler       : register(s0),
76                                        uniform sampler2D entryPointCountSampler : register(s1),
77                                        uniform sampler2D allWeightsSampler   : register(s2)) : COLOR0
[2202]78{
79        float halfPixel = 0.5 / clusterCount;
80        float iCluster = input.tex.x + halfPixel;
81        int entryPointCount = tex2D(entryPointCountSampler, float2(iCluster, 0.25));
82        int currentEntryPoint = tex2D(entryPointCountSampler, float2(iCluster, 0.75));
83        //sum entrypoint weights
84        float weight = 0;
85        float clusterRad = 0;
86        for(int i = 0; i < entryPointCount; i++)
87        {
[2217]88               
89                float dataColumnWidth = 1.0 / (float)nRadionColumns;
90                int werx = currentEntryPoint % nRadionColumns;
91                int wery = currentEntryPoint / nRadionColumns;
92                float2 coord1 = float2((werx + 0.5) * dataColumnWidth, (wery  + 0.5) / 4096.0);
93                float2 coord2 = coord1 + float2(0.25 * dataColumnWidth, 0);
94                float w = tex2Dlod(allWeightsSampler, float4(coord1, 0, 0)).r;
95                float radrad = tex2Dlod(radionSampler, float4(coord1, 0, 0)).a;         
96                weight += w * radrad;           
[2213]97                clusterRad += radrad;
[2202]98                currentEntryPoint++;
99        }       
[2213]100       
[2217]101        if(clusterRad >= 0)
[2202]102                weight /= clusterRad;
[2188]103        else
[2217]104                weight = 0;
105   
[2202]106        return weight;
[2207]107}
108
109void EntryPointDisplayVS(float4 position :POSITION0,
110                                                 float4 color :COLOR0,
111                                                 uniform float4x4 worldViewProj,
112                                                 out float4 hPos:POSITION,
113                                                 out float2 texCoord: TEXCOORD0)
114{
115        hPos = mul(worldViewProj, position);
116        texCoord = color.xy;
117}
118
119float4 EntryPointDisplayPS(float2 texCoord : TEXCOORD0,
120                                                        uniform sampler2D weightTexture : register(s0)):COLOR0
121{
122        //return texCoord.y;
[2213]123        return texCoord.r;
[2207]124}
125                                                         
Note: See TracBrowser for help on using the repository browser.