[2972] | 1 | #include "../shaderenv.h"
|
---|
| 2 |
|
---|
| 3 |
|
---|
[2974] | 4 | struct frag
|
---|
[2972] | 5 | {
|
---|
| 6 | // normalized screen position
|
---|
[2974] | 7 | float2 texCoord: TEXCOORD0;
|
---|
[2973] | 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
|
---|
[2972] | 13 | };
|
---|
| 14 |
|
---|
| 15 |
|
---|
[2973] | 16 | struct pixel
|
---|
[2972] | 17 | {
|
---|
[2973] | 18 | float4 col: COLOR0;
|
---|
| 19 | };
|
---|
[2972] | 20 |
|
---|
| 21 |
|
---|
[3010] | 22 | /*float4 DownSample(frag IN,
|
---|
[2972] | 23 | uniform sampler2D colors,
|
---|
[3010] | 24 | uniform float2 downSampleOffs[9]): COLOR
|
---|
[2972] | 25 | {
|
---|
| 26 | float4 average = .0f;
|
---|
| 27 |
|
---|
[3010] | 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)
|
---|
[2972] | 34 | {
|
---|
[3010] | 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 | }
|
---|
[2972] | 41 | }
|
---|
| 42 |
|
---|
[3010] | 43 | //average *= 1.0f / 9.0f;
|
---|
| 44 | average *= 1.0f / j;
|
---|
[2972] | 45 |
|
---|
[3006] | 46 | return average;
|
---|
[3010] | 47 | }*/
|
---|
[3017] | 48 |
|
---|
| 49 |
|
---|
| 50 | // let bilinear filtering do its work
|
---|
[3010] | 51 | float4 DownSample(frag IN,
|
---|
| 52 | uniform sampler2D colors,
|
---|
| 53 | uniform float2 downSampleOffs[NUM_DOWNSAMPLES]): COLOR
|
---|
| 54 | {
|
---|
[3016] | 55 | return tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 56 | /*
|
---|
[3010] | 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 |
|
---|
[3016] | 66 | return average;*/
|
---|
[2972] | 67 | }
|
---|
| 68 |
|
---|
[3010] | 69 |
|
---|
| 70 | /** Does the first downsampling step and on the same time calculates the
|
---|
| 71 | intensity.
|
---|
| 72 | */
|
---|
| 73 |
|
---|
[2974] | 74 | float4 GreyScaleDownSample(frag IN,
|
---|
[3010] | 75 | uniform sampler2D colors,
|
---|
| 76 | uniform float2 downSampleOffs[4]
|
---|
[2974] | 77 | ): COLOR
|
---|
[2972] | 78 | {
|
---|
| 79 |
|
---|
| 80 | // Compute the average of the 4 necessary samples
|
---|
[2974] | 81 | float average = .0f;
|
---|
| 82 | float maximum = .0f;
|
---|
[2972] | 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 |
|
---|
[3010] | 88 | float4 color;
|
---|
[2973] | 89 |
|
---|
[2972] | 90 | for (int i = 0; i < 4; ++ i)
|
---|
| 91 | {
|
---|
[3010] | 92 | color = tex2D(colors, downSampleOffs[i]);
|
---|
| 93 |
|
---|
[2973] | 94 | const float intensity = dot(cols[i].rgb, w);
|
---|
[2972] | 95 |
|
---|
[2973] | 96 | maximum = max(maximum, intensity);
|
---|
[2974] | 97 | average += log(1e-5f + intensity);
|
---|
[2972] | 98 | }
|
---|
| 99 |
|
---|
[3010] | 100 | average *= 0.25f;
|
---|
[2972] | 101 |
|
---|
| 102 | // Output the luminance to the render target
|
---|
[3010] | 103 | return float4(average, maximum, 0.0f, 1.0f);
|
---|
[2972] | 104 | }
|
---|
[2973] | 105 |
|
---|
[2972] | 106 |
|
---|
[3010] | 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 |
|
---|
[2974] | 135 | pixel ToneMap(frag IN,
|
---|
[2973] | 136 | uniform sampler2D colors,
|
---|
| 137 | uniform float imageKey,
|
---|
| 138 | uniform float whiteLum,
|
---|
| 139 | uniform float middleGrey)
|
---|
| 140 | {
|
---|
| 141 | pixel OUT;
|
---|
[3008] | 142 |
|
---|
[2974] | 143 | float4 color = tex2D(colors, IN.texCoord);
|
---|
[2973] | 144 |
|
---|
| 145 | const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
|
---|
| 146 |
|
---|
[2974] | 147 | // obtain new image key from highest mipmap level
|
---|
| 148 | float logLumScaled = tex2Dlod(colors, float4(.5f, .5f, 0, 99)).w;
|
---|
[2975] | 149 | float logLum = logLumScaled * LOGLUM_RANGE + MINLOGLUM;
|
---|
[2974] | 150 |
|
---|
| 151 | float newImageKey = exp(logLum);
|
---|
| 152 |
|
---|
[2973] | 153 | // adjust to middle gray
|
---|
[2974] | 154 | const float lum = middleGrey * pixLum / newImageKey;
|
---|
[2973] | 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;
|
---|
[2991] | 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;
|
---|
[3008] | 172 |
|
---|
[3007] | 173 | const float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
[2991] | 174 |
|
---|
[2992] | 175 | OUT.col = color;
|
---|
| 176 |
|
---|
[2991] | 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 |
|
---|
[3005] | 189 | if (oldLogLum > 1e-5f) // too bright
|
---|
[2992] | 190 | OUT.col.w = lerp(oldLogLum, logLumScaled, 0.1f);
|
---|
[2991] | 191 | else
|
---|
[2992] | 192 | OUT.col.w = logLumScaled;
|
---|
| 193 |
|
---|
[3008] | 194 | return OUT;
|
---|
[2973] | 195 | } |
---|