//Vertex Shader input struct VS_INPUT { float4 pos : POSITION; float2 tex0 : TEXCOORD0; }; //Vertex Shader output struct VS_OUTPUT { float4 pos : POSITION; float2 tex0 : TEXCOORD0; }; //Uniform parameters. uniform float4x4 modelViewProjection; uniform float Gain; uniform float dsWidth, dsHeight; // Texture samplers texture SourceTexture; sampler2D SourceTextureSampler = sampler_state { texture = ; mipfilter = LINEAR; AddressU = Wrap; AddressV = Wrap; }; texture LuminanceTexture; sampler2D LuminanceTextureSampler = sampler_state { texture = ; mipfilter = LINEAR; AddressU = Wrap; AddressV = Wrap; }; texture AverageLuminanceTexture; sampler2D AverageLuminanceTextureSampler = sampler_state { texture = ; mipfilter = LINEAR; AddressU = Wrap; AddressV = Wrap; }; // Simple Vertex Shader with MVP VS_OUTPUT VS_myVertexShader(in VS_INPUT input){ VS_OUTPUT output0; output0.pos = mul(input.pos,modelViewProjection); output0.tex0 = input.tex0 + 0.5*float2(1.0f/dsWidth, 1.0f/dsHeight); return output0; } // Luminance calculation float4 PS_Luminance(in float2 tex0:TEXCOORD0) : COLOR { half4 texLookUp; texLookUp.rgba = tex2D(SourceTextureSampler,tex0).rgba; // D65 white conversion and weighting half avg = texLookUp.r * 0.21f + texLookUp.g * 0.39f + texLookUp.b * 0.4f; return half4(avg, 0, 0, 1); } // Horizontal blur half4 PS_Blur_h(in float2 tex0:TEXCOORD0) : COLOR { half4 texLookUp_h; texLookUp_h = tex2D(LuminanceTextureSampler, float2(tex0.x-(3.86979f/dsWidth), tex0.y)) + tex2D(LuminanceTextureSampler, float2(tex0.x-(1.72291f/dsWidth), tex0.y)) + tex2D(LuminanceTextureSampler, tex0) + tex2D(LuminanceTextureSampler, float2(tex0.x+(1.72291f/dsWidth), tex0.y)) + tex2D(LuminanceTextureSampler, float2(tex0.x+(3.86979f/dsWidth), tex0.y)); return texLookUp_h / 5 ; } // Vertical blur half4 PS_Blur_v(in float2 tex0:TEXCOORD0) : COLOR { half4 texLookUp_v; texLookUp_v = tex2D(LuminanceTextureSampler, float2(tex0.x, tex0.y-(3.86979f/dsHeight)))+ tex2D(LuminanceTextureSampler, float2(tex0.x, tex0.y-(1.72291f/dsHeight)))+ tex2D(LuminanceTextureSampler, tex0)+ tex2D(LuminanceTextureSampler, float2(tex0.x, tex0.y+(1.72291f/dsHeight)))+ tex2D(LuminanceTextureSampler, float2(tex0.x, tex0.y+(3.86979f/dsHeight))); return texLookUp_v / 5 ; } // Final calculation half4 PS_Final(in float2 tex0:TEXCOORD0) : COLOR { // Automatic key calculation half Key = 1.03f - 2.0f / (2.0f + log10(tex2D(AverageLuminanceTextureSampler, tex0).r + 1.0f)); // Relative luminance half RelLum = Key* tex2D(LuminanceTextureSampler, tex0).r / tex2D(AverageLuminanceTextureSampler, tex0).r; half Lum = RelLum / (1 + RelLum); // Color weghting half4 Color = tex2D(SourceTextureSampler, tex0) * Lum; // Gamma correction Color = pow(Color * half4(1.05f, 0.97f, 1.27f, 1.0f), Gain); return Color; } technique Blur_h { pass P0{ // compiler directives VertexShader = compile vs_2_0 VS_myVertexShader(); PixelShader = compile ps_2_0 PS_Blur_h(); } } technique Blur_v { pass P0{ // compiler directives VertexShader = compile vs_2_0 VS_myVertexShader(); PixelShader = compile ps_2_0 PS_Blur_v(); } } technique Luminance { pass P0{ // compiler directives VertexShader = compile vs_2_0 VS_myVertexShader(); PixelShader = compile ps_2_0 PS_Luminance(); } } technique FinalTechnique { pass P0{ // compiler directives VertexShader = compile vs_2_0 VS_myVertexShader(); PixelShader = compile ps_2_0 PS_Final(); } }