1 | #include "../shaderenv.h"
|
---|
2 |
|
---|
3 |
|
---|
4 | struct frag
|
---|
5 | {
|
---|
6 | // normalized screen position
|
---|
7 | float2 texCoord: TEXCOORD0;
|
---|
8 |
|
---|
9 | float2 lt: TEXCOORD1; // left top
|
---|
10 | float2 rb: TEXCOORD2; // right bottom
|
---|
11 | float2 rt: TEXCOORD3; // right top
|
---|
12 | float2 lb: TEXCOORD4; // left bottom
|
---|
13 | };
|
---|
14 |
|
---|
15 |
|
---|
16 | struct pixel
|
---|
17 | {
|
---|
18 | float4 col: COLOR0;
|
---|
19 | };
|
---|
20 |
|
---|
21 |
|
---|
22 | /*float4 DownSample(frag IN,
|
---|
23 | uniform sampler2D colors,
|
---|
24 | uniform float2 downSampleOffs[9]): COLOR
|
---|
25 | {
|
---|
26 | float4 average = .0f;
|
---|
27 |
|
---|
28 | const float4 avgColor = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
29 |
|
---|
30 | float j = 0;
|
---|
31 | float4 color;
|
---|
32 |
|
---|
33 | for (int i = 0; i < 9; ++ i)
|
---|
34 | {
|
---|
35 | color = tex2Dlod(colors, float4(IN.texCoord + downSampleOffs[i], 0, 0));
|
---|
36 | if (abs(color.w - avgColor.w) < 1e-3f)
|
---|
37 | {
|
---|
38 | average += color;
|
---|
39 | ++ j;
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | //average *= 1.0f / 9.0f;
|
---|
44 | average *= 1.0f / j;
|
---|
45 |
|
---|
46 | return average;
|
---|
47 | }*/
|
---|
48 |
|
---|
49 |
|
---|
50 | // let bilinear filtering do its work
|
---|
51 | float4 DownSample(frag IN,
|
---|
52 | uniform sampler2D colors,
|
---|
53 | uniform float2 downSampleOffs[NUM_DOWNSAMPLES]): COLOR
|
---|
54 | {
|
---|
55 | return tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
56 | /*
|
---|
57 | float4 average = .0f;
|
---|
58 |
|
---|
59 | for (int i = 0; i < NUM_DOWNSAMPLES; ++ i)
|
---|
60 | {
|
---|
61 | average += tex2Dlod(colors, float4(IN.texCoord + downSampleOffs[i], 0, 0));
|
---|
62 | }
|
---|
63 |
|
---|
64 | average *= 1.0f / (float)NUM_DOWNSAMPLES;
|
---|
65 |
|
---|
66 | return average;*/
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | /** Does the first downsampling step and on the same time calculates the
|
---|
71 | intensity.
|
---|
72 | */
|
---|
73 |
|
---|
74 | float4 GreyScaleDownSample(frag IN,
|
---|
75 | uniform sampler2D colors,
|
---|
76 | uniform float2 downSampleOffs[4]
|
---|
77 | ): COLOR
|
---|
78 | {
|
---|
79 |
|
---|
80 | // Compute the average of the 4 necessary samples
|
---|
81 | float average = .0f;
|
---|
82 | float maximum = .0f;
|
---|
83 |
|
---|
84 | // the rgb weights
|
---|
85 | const float3 w = float3(0.299f, 0.587f, 0.114f);
|
---|
86 | //float3 w = float3(0.2125f, 0.7154f, 0.0721f);
|
---|
87 |
|
---|
88 | float4 color;
|
---|
89 |
|
---|
90 | for (int i = 0; i < 4; ++ i)
|
---|
91 | {
|
---|
92 | color = tex2D(colors, downSampleOffs[i]);
|
---|
93 |
|
---|
94 | const float intensity = dot(cols[i].rgb, w);
|
---|
95 |
|
---|
96 | maximum = max(maximum, intensity);
|
---|
97 | average += log(1e-5f + intensity);
|
---|
98 | }
|
---|
99 |
|
---|
100 | average *= 0.25f;
|
---|
101 |
|
---|
102 | // Output the luminance to the render target
|
---|
103 | return float4(average, maximum, 0.0f, 1.0f);
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /** Used for downsampling the tone map parameters (average loglum, maximum)
|
---|
108 | to the next lower level. This has to be applied until there is only
|
---|
109 | a 1x1 texture which holds the required numbers.
|
---|
110 | */
|
---|
111 | float4 DownSampleForToneMapping(frag IN,
|
---|
112 | uniform sampler2D colors,
|
---|
113 | uniform float2 downSampleOffs[4]): COLOR
|
---|
114 | {
|
---|
115 | float average = .0f;
|
---|
116 | float maximum = .0f;
|
---|
117 |
|
---|
118 | float4 color;
|
---|
119 |
|
---|
120 | for (int i = 0; i < 4; ++ i)
|
---|
121 | {
|
---|
122 | color = tex2D(colors, downSampleOffs[i]);
|
---|
123 |
|
---|
124 | maximum = max(maximum, color.y);
|
---|
125 | average += color.x;
|
---|
126 | }
|
---|
127 |
|
---|
128 | average *= 1.0f / (float)NUM_DOWNSAMPLES;
|
---|
129 |
|
---|
130 | return float4(average, maximum, 0.0f, 1.0f);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 |
|
---|
135 | pixel ToneMap(frag IN,
|
---|
136 | uniform sampler2D colors,
|
---|
137 | uniform float imageKey,
|
---|
138 | uniform float whiteLum,
|
---|
139 | uniform float middleGrey)
|
---|
140 | {
|
---|
141 | pixel OUT;
|
---|
142 |
|
---|
143 | float4 color = tex2D(colors, IN.texCoord);
|
---|
144 |
|
---|
145 | const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
|
---|
146 |
|
---|
147 | // obtain new image key from highest mipmap level
|
---|
148 | float logLumScaled = tex2Dlod(colors, float4(.5f, .5f, 0, 99)).w;
|
---|
149 | float logLum = logLumScaled * LOGLUM_RANGE + MINLOGLUM;
|
---|
150 |
|
---|
151 | float newImageKey = exp(logLum);
|
---|
152 |
|
---|
153 | // adjust to middle gray
|
---|
154 | const float lum = middleGrey * pixLum / newImageKey;
|
---|
155 |
|
---|
156 | // map to range and calc burnout
|
---|
157 | const float scaleLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum);
|
---|
158 |
|
---|
159 | OUT.col = color * scaleLum / pixLum;
|
---|
160 | OUT.col.w = color.w;
|
---|
161 |
|
---|
162 | return OUT;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | pixel CalcAvgLogLum(frag IN, uniform sampler2D colors)
|
---|
167 | {
|
---|
168 | ////////////
|
---|
169 | //-- write out logaritmic luminance for tone mapping
|
---|
170 |
|
---|
171 | pixel OUT;
|
---|
172 |
|
---|
173 | const float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
174 |
|
---|
175 | OUT.col = color;
|
---|
176 |
|
---|
177 | // the old loglum is stored in the hightest mipmap-level
|
---|
178 | float oldLogLum = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL)).w;
|
---|
179 |
|
---|
180 | // the intensity weights
|
---|
181 | const float3 w = float3(0.299f, 0.587f, 0.114f);
|
---|
182 |
|
---|
183 | float lum = dot(color.rgb, w);
|
---|
184 | float logLum = log(1e-5f + lum);
|
---|
185 |
|
---|
186 | float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
|
---|
187 | float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
|
---|
188 |
|
---|
189 | if (oldLogLum > 1e-5f) // too bright
|
---|
190 | OUT.col.w = lerp(oldLogLum, logLumScaled, 0.1f);
|
---|
191 | else
|
---|
192 | OUT.col.w = logLumScaled;
|
---|
193 |
|
---|
194 | return OUT;
|
---|
195 | } |
---|