[846] | 1 | //Vertex Shader input
|
---|
| 2 | struct VS_INPUT
|
---|
| 3 | {
|
---|
| 4 | float4 pos : POSITION;
|
---|
| 5 | float2 tex0 : TEXCOORD0;
|
---|
| 6 | };
|
---|
| 7 |
|
---|
| 8 | //Vertex Shader output
|
---|
| 9 | struct VS_OUTPUT
|
---|
| 10 | {
|
---|
| 11 | float4 pos : POSITION;
|
---|
| 12 | float2 tex0 : TEXCOORD0;
|
---|
| 13 | };
|
---|
| 14 |
|
---|
| 15 | //Uniform parameters.
|
---|
| 16 | uniform float4x4 modelViewProjection;
|
---|
| 17 | uniform float Gain;
|
---|
| 18 | uniform float dsWidth, dsHeight;
|
---|
| 19 |
|
---|
| 20 | // Texture samplers
|
---|
| 21 | texture SourceTexture;
|
---|
| 22 | sampler2D SourceTextureSampler = sampler_state
|
---|
| 23 | {
|
---|
| 24 | texture = <SourceTexture>;
|
---|
| 25 | mipfilter = LINEAR;
|
---|
| 26 | AddressU = Wrap;
|
---|
| 27 | AddressV = Wrap;
|
---|
| 28 | };
|
---|
| 29 |
|
---|
| 30 | texture LuminanceTexture;
|
---|
| 31 | sampler2D LuminanceTextureSampler = sampler_state
|
---|
| 32 | {
|
---|
| 33 | texture = <LuminanceTexture>;
|
---|
| 34 | mipfilter = LINEAR;
|
---|
| 35 | AddressU = Wrap;
|
---|
| 36 | AddressV = Wrap;
|
---|
| 37 | };
|
---|
| 38 |
|
---|
| 39 | texture AverageLuminanceTexture;
|
---|
| 40 | sampler2D AverageLuminanceTextureSampler = sampler_state
|
---|
| 41 | {
|
---|
| 42 | texture = <AverageLuminanceTexture>;
|
---|
| 43 | mipfilter = LINEAR;
|
---|
| 44 | AddressU = Wrap;
|
---|
| 45 | AddressV = Wrap;
|
---|
| 46 | };
|
---|
| 47 |
|
---|
| 48 | // Simple Vertex Shader with MVP
|
---|
| 49 | VS_OUTPUT VS_myVertexShader(in VS_INPUT input){
|
---|
| 50 | VS_OUTPUT output0;
|
---|
| 51 | output0.pos = mul(input.pos,modelViewProjection);
|
---|
| 52 | output0.tex0 = input.tex0 + 0.5*float2(1.0f/dsWidth, 1.0f/dsHeight);
|
---|
| 53 | return output0;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | // Luminance calculation
|
---|
| 57 | float4 PS_Luminance(in float2 tex0:TEXCOORD0) : COLOR {
|
---|
| 58 | half4 texLookUp;
|
---|
| 59 | texLookUp.rgba = tex2D(SourceTextureSampler,tex0).rgba;
|
---|
| 60 |
|
---|
| 61 | // D65 white conversion and weighting
|
---|
| 62 | half avg = texLookUp.r * 0.21f + texLookUp.g * 0.39f + texLookUp.b * 0.4f;
|
---|
| 63 | return half4(avg, 0, 0, 1);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | // Horizontal blur
|
---|
| 67 | half4 PS_Blur_h(in float2 tex0:TEXCOORD0) : COLOR {
|
---|
| 68 | half4 texLookUp_h;
|
---|
| 69 |
|
---|
| 70 | texLookUp_h = tex2D(LuminanceTextureSampler, float2(tex0.x-(3.86979f/dsWidth), tex0.y)) +
|
---|
| 71 | tex2D(LuminanceTextureSampler, float2(tex0.x-(1.72291f/dsWidth), tex0.y)) +
|
---|
| 72 | tex2D(LuminanceTextureSampler, tex0) +
|
---|
| 73 | tex2D(LuminanceTextureSampler, float2(tex0.x+(1.72291f/dsWidth), tex0.y)) +
|
---|
| 74 | tex2D(LuminanceTextureSampler, float2(tex0.x+(3.86979f/dsWidth), tex0.y));
|
---|
| 75 |
|
---|
| 76 | return texLookUp_h / 5 ;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | // Vertical blur
|
---|
| 80 | half4 PS_Blur_v(in float2 tex0:TEXCOORD0) : COLOR {
|
---|
| 81 | half4 texLookUp_v;
|
---|
| 82 |
|
---|
| 83 | texLookUp_v = tex2D(LuminanceTextureSampler, float2(tex0.x, tex0.y-(3.86979f/dsHeight)))+
|
---|
| 84 | tex2D(LuminanceTextureSampler, float2(tex0.x, tex0.y-(1.72291f/dsHeight)))+
|
---|
| 85 | tex2D(LuminanceTextureSampler, tex0)+
|
---|
| 86 | tex2D(LuminanceTextureSampler, float2(tex0.x, tex0.y+(1.72291f/dsHeight)))+
|
---|
| 87 | tex2D(LuminanceTextureSampler, float2(tex0.x, tex0.y+(3.86979f/dsHeight)));
|
---|
| 88 |
|
---|
| 89 | return texLookUp_v / 5 ;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | // Final calculation
|
---|
| 93 | half4 PS_Final(in float2 tex0:TEXCOORD0) : COLOR {
|
---|
| 94 |
|
---|
| 95 | // Automatic key calculation
|
---|
| 96 | half Key = 1.03f - 2.0f / (2.0f + log10(tex2D(AverageLuminanceTextureSampler, tex0).r + 1.0f));
|
---|
| 97 | // Relative luminance
|
---|
| 98 | half RelLum = Key* tex2D(LuminanceTextureSampler, tex0).r /
|
---|
| 99 | tex2D(AverageLuminanceTextureSampler, tex0).r;
|
---|
| 100 | half Lum = RelLum / (1 + RelLum);
|
---|
| 101 | // Color weghting
|
---|
| 102 | half4 Color = tex2D(SourceTextureSampler, tex0) * Lum;
|
---|
| 103 | // Gamma correction
|
---|
| 104 | Color = pow(Color * half4(1.05f, 0.97f, 1.27f, 1.0f), Gain);
|
---|
| 105 | return Color;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | technique Blur_h
|
---|
| 109 | {
|
---|
| 110 | pass P0{
|
---|
| 111 | // compiler directives
|
---|
| 112 | VertexShader = compile vs_2_0 VS_myVertexShader();
|
---|
| 113 | PixelShader = compile ps_2_0 PS_Blur_h();
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | technique Blur_v
|
---|
| 118 | {
|
---|
| 119 | pass P0{
|
---|
| 120 | // compiler directives
|
---|
| 121 | VertexShader = compile vs_2_0 VS_myVertexShader();
|
---|
| 122 | PixelShader = compile ps_2_0 PS_Blur_v();
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | technique Luminance
|
---|
| 127 | {
|
---|
| 128 | pass P0{
|
---|
| 129 | // compiler directives
|
---|
| 130 | VertexShader = compile vs_2_0 VS_myVertexShader();
|
---|
| 131 | PixelShader = compile ps_2_0 PS_Luminance();
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | technique FinalTechnique
|
---|
| 136 | {
|
---|
| 137 | pass P0{
|
---|
| 138 | // compiler directives
|
---|
| 139 | VertexShader = compile vs_2_0 VS_myVertexShader();
|
---|
| 140 | PixelShader = compile ps_2_0 PS_Final();
|
---|
| 141 | }
|
---|
| 142 | } |
---|