#include "../shaderenv.h" struct frag { // normalized screen position float2 texCoord: TEXCOORD0; float2 lt: TEXCOORD1; // left top float2 rb: TEXCOORD2; // right bottom float2 rt: TEXCOORD3; // right top float2 lb: TEXCOORD4; // left bottom }; struct pixel { float4 col: COLOR0; }; float4 DownSample(frag IN, uniform sampler2D colors, float2 downSampleOffs[16]) : COLOR { float4 average = .0f; for(int i = 0; i < 16; ++ i) { average += tex2D(colors, IN.texCoord + downSampleOffs[i]); } average *= 1.0f / 16.0f; return average; } float4 GreyScaleDownSample(frag IN, uniform sampler2D colors ): COLOR { // Compute the average of the 4 necessary samples float average = .0f; float maximum = .0f; // the rgb weights const float3 w = float3(0.299f, 0.587f, 0.114f); //float3 w = float3(0.2125f, 0.7154f, 0.0721f); float4 cols[4]; cols[0] = tex2D(colors, IN.lt); cols[1] = tex2D(colors, IN.lb); cols[2] = tex2D(colors, IN.rt); cols[3] = tex2D(colors, IN.rb); for (int i = 0; i < 4; ++ i) { const float intensity = dot(cols[i].rgb, w); maximum = max(maximum, intensity); average += log(1e-5f + intensity); } average = exp(average * 0.25f); float4 hcols = cols[0] + cols[1] + cols[2] + cols[3]; return hcols * 0.25f; //return float4(average, maximum, 0.0f, cols[0].w); // Output the luminance to the render target //return float4(average, maximum, 0.0f, 1.0f); } pixel ToneMap(frag IN, uniform sampler2D colors, uniform float imageKey, uniform float whiteLum, uniform float middleGrey) { pixel OUT; float4 color = tex2D(colors, IN.texCoord); const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z; // obtain new image key from highest mipmap level float logLumScaled = tex2Dlod(colors, float4(.5f, .5f, 0, 99)).w; float logLum = logLumScaled * LOGLUM_RANGE + MINLOGLUM; float newImageKey = exp(logLum); // adjust to middle gray const float lum = middleGrey * pixLum / newImageKey; // map to range and calc burnout const float scaleLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum); OUT.col = color * scaleLum / pixLum; OUT.col.w = color.w; return OUT; }