//////////////////////////////////////////////////////////////////////////////////// //POST EFFECT SHADERS........ #include "fx_configuration.h" //#define ENABLE_EDITOR //#define USE_CHARACTER_SHADOW_MERGE float4 output_gamma; // vectorize? float4 output_gamma_inv; static const float3 LUMINANCE_WEIGHTS = float3(0.299f, 0.587f, 0.114f); static const float min_exposure = 0.15f; static const float max_exposure = 3.0f; #pragma warning(disable: 3571) //pow(f,e) warning! #define ERROR_OUT(c) c = float4(texCoord.x * 10 - floor(texCoord.x * 10) > 0.5, texCoord.y * 10 - floor(texCoord.y * 10) > 0.5, 0, 1) // use postFX_sampler4 for point sampling #if defined(USE_FX_STATE_MANAGER) && !defined(USE_DEVICE_TEXTURE_ASSIGN) //else we can use direct device access with sampler indexes... texture postFX_texture0, postFX_texture1, postFX_texture2, postFX_texture3, postFX_texture4; //non-srgb samplers sampler postFX_sampler0 : register(s0) = sampler_state { Texture = postFX_texture0; }; // scene sampler postFX_sampler1 : register(s1) = sampler_state { Texture = postFX_texture1; }; // hdr-low = 1/2res high = 1/4res sampler postFX_sampler2 : register(s2) = sampler_state { Texture = postFX_texture2; }; // brightness .. AVG and MAX sampler postFX_sampler3 : register(s3) = sampler_state { Texture = postFX_texture3; }; // black sampler postFX_sampler4 : register(s4) = sampler_state { Texture = postFX_texture4; }; // == sampler 2 + WithLuminance??? sampler postFX_sampler5 : register(s5);// depth // sampler postFX_sampler6 : register(s6);// cubic ...black screen // sampler postFX_sampler7 : register(s7);// shadowmap ...white screen // sampler postFX_sampler8 : register(s8);// screen ...half resolution screen // sampler postFX_sampler9 : register(s9);// mesh ...shows random texture // sampler postFX_sampler10 : register(s10);// clamped ...black screen // sampler postFX_sampler11 : register(s11);// font ...shows font texture // sampler postFX_sampler12 : register(s12);// char shadow ...black screen // sampler postFX_sampler13 : register(s13);// mesh no filter ...shows random texture without filter // sampler postFX_sampler14 : register(s14);// diffuse no wrap ...black screen // sampler postFX_sampler15 : register(s15);// grass ...black screen // sampler postFX_sampler16 : register(s0);//reflection ...shows blurry screen // sampler postFX_sampler17 : register(s1);//env ...shows env texture // sampler postFX_sampler18 : register(s2);//diffuse ...shows random diffuse texture sampler postFX_sampler19 : register(s3);//normal ...shows random normal texture #else #ifdef USE_REGISTERED_SAMPLERS sampler postFX_sampler0 : register(s0); //linear clamp sampler postFX_sampler1 : register(s1); //linear clamp sampler postFX_sampler2 : register(s2); //linear clamp sampler postFX_sampler3 : register(s3); //linear clamp sampler postFX_sampler4 : register(s4); //linear clamp #else sampler postFX_sampler0 : register(s0) = sampler_state{ AddressU = CLAMP; AddressV = CLAMP; MinFilter = LINEAR; MagFilter = LINEAR; }; //linear clamp sampler postFX_sampler1 : register(s1) = sampler_state{ AddressU = CLAMP; AddressV = CLAMP; MinFilter = LINEAR; MagFilter = LINEAR; }; //linear clamp sampler postFX_sampler2 : register(s2) = sampler_state{ AddressU = CLAMP; AddressV = CLAMP; MinFilter = LINEAR; MagFilter = LINEAR; }; //linear clamp sampler postFX_sampler3 : register(s3) = sampler_state{ AddressU = CLAMP; AddressV = CLAMP; MinFilter = LINEAR; MagFilter = LINEAR; }; //linear clamp sampler postFX_sampler4 : register(s4) = sampler_state{ AddressU = CLAMP; AddressV = CLAMP; MinFilter = LINEAR; MagFilter = LINEAR; }; //linear clamp #endif #endif static const int clip_distance = 10000;// is 1250 by default you can change it in module.ini ...=> far_plane_distance = 10000 #was 1250 #clip distance static const int num_iterations = 8; static const float DarkBlurPixelWeight[num_iterations] = {1.000, 0.875, 0.750, 0.625, 0.500, 0.375, 0.250, 0.125}; static const float3 luma = float3(0.2126, 0.7152, 0.0722); static const float LightBlurPixelWeight[8] = { 0.256, 0.240, 0.144, 0.135, 0.120, 0.065, 0.030, 0.010 }; //static const float BlurPixelWeight[8] = { 0.35537, 0.34185, 0.23821, 0.125861, 0.0813562, 0.04862, 0.025, 0.012552 }; //static const float BlurPixelWeight[8] = { 0.5537, 0.4185, 0.3821, 0.25861, 0.13562, 0.0862, 0.05, 0.02552 }; //static const float BlurPixelWeight[8] = { 0.2537, 0.2185, 0.1821, 0.15861, 0.082, 0.062, 0.03, 0.01552 }; bool showing_ranged_data = false; float4 g_HalfPixel_ViewportSizeInv; float g_HDR_frameTime; float g_DOF_Focus = -0.005; float g_DOF_Range = 5.19876; #ifndef PS_2_X #define PS_2_X ps_2_b #endif #ifdef ENABLE_EDITOR // //postFX0- x:blurStr, y:exposure, z:range, w:temp //postFX1- brightness //postFX2- constrast //postFX3- saturation float4 postfx_editor_vector[4]; #undef postfxTonemapOp #undef postfxParams1 #undef postfxParams2 #undef postfxParams3 #define postfxTonemapOp ( int(postfx_editor_vector[0].x) ) #define postfxParams1 float4(postfx_editor_vector[1].x, postfx_editor_vector[1].y, postfx_editor_vector[1].z, postfx_editor_vector[1].w) #define postfxParams2 float4(postfx_editor_vector[2].x, postfx_editor_vector[2].y, postfx_editor_vector[2].z, postfx_editor_vector[2].w) #define postfxParams3 float4(postfx_editor_vector[3].x, postfx_editor_vector[3].y, postfx_editor_vector[3].z, postfx_editor_vector[3].w) #define RELATIVE_PS_TARGET PS_2_X //we need more instruction_count to edit things dynamically #else //COMPILE_TIME_POSTFX_CONSTANTS // constants are defined from application to reduce inst. # //--#define postfxTonemapOp ((int)0) //--#define postfxParams1 (float4(16.0f, 1.0f, 1.0f, 1.0f)) //--#define postfxParams2 (float4( 1.0f, 1.0f, 1.0f, 1.0f)) #define RELATIVE_PS_TARGET ps_2_0 #endif #define HDRRange (postfxParams1.x) #define HDRExposureScaler (postfxParams1.y) #define LuminanceAverageScaler (postfxParams1.z) #define LuminanceMaxScaler (postfxParams1.w) #define BrightpassTreshold (postfxParams2.x) #define BrightpassPostPower (postfxParams2.y) #define BlurStrenght (postfxParams2.z) #define BlurAmount (postfxParams2.w) #define HDRRangeInv (1.0f / HDRRange) //Tonemap float3 Tonemap(float3 color, float Gamma, float Exposure, float Saturation, float Bleach, float Defog, float3 FogColor){ float3 c = color; c = saturate(c - Defog * FogColor * 2.55); // Defog c *= pow(2.0f, Exposure); // Exposure c = pow(c, Gamma); // Gamma const float3 coefLuma = float3(0.2126, 0.7152, 0.0722); float lum = dot(coefLuma, c); float L = saturate(10.0 * (lum - 0.45)); float3 A2 = Bleach * c; float3 result1 = 2.0f * c * lum; float3 result2 = 1.0f - 2.0f * (1.0f - lum) * (1.0f - c); float3 newColor = lerp(result1, result2, L); float3 mixRGB = A2 * newColor; c += ((1.0f - A2) * mixRGB); float3 middlegray = dot(c, (1.0 / 3.0)); float3 diffcolor = c - middlegray; c = (c + diffcolor * Saturation) / (1 + (diffcolor * Saturation)); // Saturation return c; } float vignette(float2 pos, float inner, float outer){ //float r = length(pos); //orj float r = dot(pos,pos); r = 1.0 - smoothstep(inner, outer, r); return r; } float3 tonemapping(const float3 scene_color, const float2 luminanceAvgMax, const int tonemapOp) { float lum_avg = luminanceAvgMax.x * LuminanceAverageScaler; float lum_max = luminanceAvgMax.y * LuminanceMaxScaler; static const float MiddleValue = 0.85f; //float exposure = 1.4427 / (0.5 + lum_avg); float exposure = MiddleValue / (0.00001 + lum_avg); exposure = clamp(exposure*HDRExposureScaler, min_exposure, max_exposure); float3 scene_color_exposed = scene_color * exposure; float3 final_color; { if( tonemapOp==0 ) { final_color = scene_color_exposed; } else if( tonemapOp==1 ) { final_color.rgb = 1.0 - exp2(-scene_color_exposed); } else if( tonemapOp==2 ) { final_color = scene_color_exposed / (scene_color_exposed+1); } else //if( tonemapOp==3 ) { float Lp = (exposure / lum_avg) * max(scene_color_exposed.r, max(scene_color_exposed.g, scene_color_exposed.b)); float LmSqr = lum_max; //(lum_max * lum_max) * (lum_max * lum_max); float toneScalar = ( Lp * ( 1.0f + ( Lp / ( LmSqr ) ) ) ) / ( 1.0f + Lp ); final_color = scene_color_exposed * toneScalar; } } return final_color; } ///////////////////////////////////////////////////////////////////////////////////// struct VS_OUT_POSTFX { float4 Pos: POSITION; float2 Tex: TEXCOORD0; }; VS_OUT_POSTFX vs_main_postFX(float4 pos: POSITION){ VS_OUT_POSTFX Out; Out.Pos = pos; Out.Tex = (float2(pos.x, -pos.y) * 0.5f + 0.5f) + g_HalfPixel_ViewportSizeInv.xy; return Out; } VertexShader vs_main_postFX_compiled = compile vs_2_0 vs_main_postFX(); ///////////////////////////////////////////////////////////////////////////////////// float4 ps_main_postFX_Show(float2 texCoord: TEXCOORD0) : COLOR { float4 color = tex2D(postFX_sampler0, texCoord); if(showing_ranged_data) { color.rgb *= HDRRange; color.rgb = pow(color.rgb, output_gamma_inv); } return color; } technique postFX_Show { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile ps_2_0 ps_main_postFX_Show(); } } ///////////////////////////////////////////////////////////////////////////////////// #ifdef USE_CHARACTER_SHADOW_MERGE float4 ps_main_postFX_Shadowmap(float2 texCoord: TEXCOORD0) : COLOR { float original_shadowmap = tex2D(postFX_sampler0, texCoord).r; float character_shadow = tex2D(postFX_sampler1, texCoord).r; return min(original_shadowmap, character_shadow); } technique shadowmap_updater { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile ps_2_0 ps_main_postFX_Shadowmap(); } } #endif ///////////////////////////////////////////////////////////////////////////////////// float4 color_value; float4 ps_main_postFX_TrueColor(float2 texCoord: TEXCOORD0) : COLOR { const bool use_vignette = true; float4 ret = color_value; if(use_vignette) { ret.a = saturate(ret.a + ret.a * (1.0f - vignette(float2(texCoord.x*2-1, texCoord.y*2-1)*0.5f, 0.015f, 1.25f)) ); //remove blur from center } return ret; } technique postFX_TrueColor { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile ps_2_0 ps_main_postFX_TrueColor(); } } ///////////////////////////////////////////////////////////////////////////////////// float4 ps_main_brightPass(uniform const bool with_luminance, float2 inTex: TEXCOORD0 ) : COLOR0 { float3 color = tex2D( postFX_sampler0, inTex ); //get real-range color *= HDRRange; //bright pass if(with_luminance) //use luminance information to calculate exposure factor to be applied on blur rt { float2 lum_avgmax = tex2D( postFX_sampler4, float2(0.5f, 0.5f) ).rg; //color.rgb = tonemapping(color.rgb, lum_avgmax,0);//get exposed color static const float MiddleValue = 0.85f; //float exposure = 1.4427 / (0.5 + lum_avg); float exposure_factor = MiddleValue / (0.00001 + lum_avgmax.x); float exposure = 0.85 + exposure_factor * 0.15; exposure = clamp(exposure*HDRExposureScaler, min_exposure, max_exposure); color.rgb = color.rgb * exposure; color.rgb = max(0.0f, color.rgb - BrightpassTreshold); /*color.rgb = pow(color.rgb, BrightpassPostPower); */ float intensity = dot(color.rgb, float3(3.33f, 3.33f, 3.33f));//adjust boom float bloom_intensity = pow(intensity, BrightpassPostPower); color.rgb = color.rgb * ( bloom_intensity/intensity ); } else { color.rgb = max(0.0f, color.rgb - BrightpassTreshold); color.rgb = pow(color.rgb, BrightpassPostPower); } //we use interger format, turn back to normalized range color *= HDRRangeInv; //return float4(color,1); //SSAO float occlusion = 0.0f; float base_depth = tex2D(postFX_sampler5, inTex).r; if (base_depth < 1.0f)//1 = clip_distance { base_depth *= clip_distance; float2 normal;//now generate pseudo random values normal.x = frac(sin((inTex.x + 0.5) * (inTex.y + 0.5) * 1000.0f) * 1000.0f); normal.y = frac(cos((inTex.x + 0.5) * (inTex.y + 0.5) * 1000.0f) * 1000.0f); normal *= 2.0f; normal -= 1.0f; float2 offset = g_HalfPixel_ViewportSizeInv.zw * normal * 250.0f / base_depth; float2 cur_offset; float difference_1, difference_2, check_dist; for(int i = 1; i <= 4; i++) { check_dist = i; cur_offset = offset * i; difference_1 = base_depth - tex2D(postFX_sampler5, inTex + cur_offset).r * clip_distance; difference_2 = base_depth - tex2D(postFX_sampler5, inTex - cur_offset).r * clip_distance; // if one sample distance is invalid then use its twins inverse ... almost completely removes haloing if (difference_1 > check_dist && difference_2 < check_dist) difference_1 = -difference_2; else if (difference_2 > check_dist && difference_1 < check_dist) difference_2 = -difference_1; difference_1 = saturate(difference_1 / check_dist); difference_2 = saturate(difference_2 / check_dist); occlusion += saturate((difference_1 * (1.0f - difference_1) + difference_2 * (1.0f - difference_2)) - 0.25); } occlusion *= 4.0f; } return float4(color, saturate(occlusion)); //SSAO } technique postFX_brightPass { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X ps_main_brightPass(false); } } technique postFX_brightPass_WithLuminance { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X ps_main_brightPass(true); } } ///////////////////////////////////////////////////////////////////////////////////// /* float4 ps_main_blurX( float2 inTex: TEXCOORD0 ) : COLOR0 { float2 BlurOffsetX = float2(g_HalfPixel_ViewportSizeInv.z,0); float4 color = 0; for( int i = 0; i < 8; i++ ) { color += tex2D( postFX_sampler0, inTex + ( BlurOffsetX * i ) ) * BlurPixelWeight[i]; color += tex2D( postFX_sampler0, inTex - ( BlurOffsetX * i ) ) * BlurPixelWeight[i]; } return color; } float4 ps_main_blurY( float2 inTex: TEXCOORD0 ) : COLOR0 { float4 color = 0;//tex2D( postFX_sampler0, inTex ) ; float2 BlurOffsetY = float2(0, g_HalfPixel_ViewportSizeInv.w); for( int i = 0; i < 8; i++ ) { color += tex2D( postFX_sampler0, inTex + ( BlurOffsetY * i ) ) * BlurPixelWeight[i]; color += tex2D( postFX_sampler0, inTex - ( BlurOffsetY * i ) ) * BlurPixelWeight[i]; } return color; }*/ //SSAO float4 ps_main_blurX(float2 inTex: TEXCOORD0) : COLOR0 { float4 color = 0;//tex2D( postFX_sampler0, inTex ) ; float4 total_color = tex2D(postFX_sampler0, inTex) * DarkBlurPixelWeight[0]; float4 cur_color; float2 offset = float2(g_HalfPixel_ViewportSizeInv.z, 0); float2 cur_offset; float base_depth = tex2D(postFX_sampler5, inTex).r * clip_distance * 10.0f; float cur_amount; float2 blur_amount = 1.0f; for(int i = 1; i < num_iterations; i++) { for(int j = 0; j < 2; j++) { if(j == 0) cur_offset = inTex + offset * i; else cur_offset = inTex - offset * i; cur_color = tex2D(postFX_sampler0, cur_offset) * DarkBlurPixelWeight[i]; cur_amount = max(1.0f / i, 1.0f - abs(base_depth - tex2D(postFX_sampler5, cur_offset).r * clip_distance * 10.0f));//based on difference total_color.a += cur_color.a * cur_amount; total_color.rgb += cur_color.rgb; blur_amount.x += DarkBlurPixelWeight[i]; blur_amount.y += DarkBlurPixelWeight[i] * cur_amount; color += tex2D( postFX_sampler0, inTex + ( offset * i ) ) * LightBlurPixelWeight[i]; color += tex2D( postFX_sampler0, inTex - ( offset * i ) ) * LightBlurPixelWeight[i]; } } total_color.rgb /= blur_amount.x; total_color.a /= blur_amount.y; total_color += color/3.0f; total_color /= 2.0f; return total_color; } float4 ps_main_blurY(float2 inTex: TEXCOORD0) : COLOR0 { float4 color = 0;//tex2D( postFX_sampler0, inTex ) ; float4 total_color = tex2D(postFX_sampler0, inTex) * DarkBlurPixelWeight[0]; float4 cur_color; float2 offset = float2(0, g_HalfPixel_ViewportSizeInv.w); float2 cur_offset; float base_depth = tex2D(postFX_sampler5, inTex).r * clip_distance * 10.0f; float cur_amount; float2 blur_amount = 1.0f; for(int i = 1; i < num_iterations; i++) { for(int j = 0; j < 2; j++) { if(j == 0) cur_offset = inTex + offset * i; else cur_offset = inTex - offset * i; cur_color = tex2D(postFX_sampler0, cur_offset) * DarkBlurPixelWeight[i]; cur_amount = max(1.0f / i, 1.0f - abs(base_depth - tex2D(postFX_sampler5, cur_offset).r * clip_distance * 10.0f));//based on difference total_color.a += cur_color.a * cur_amount; total_color.rgb += cur_color.rgb; blur_amount.x += DarkBlurPixelWeight[i]; blur_amount.y += DarkBlurPixelWeight[i] * cur_amount; color += tex2D( postFX_sampler0, inTex + ( offset * i ) ) * LightBlurPixelWeight[i]; color += tex2D( postFX_sampler0, inTex - ( offset * i ) ) * LightBlurPixelWeight[i]; } } total_color.rgb /= blur_amount.x; total_color.a /= blur_amount.y; total_color += color/3.0f; total_color /= 2.0f; return total_color; } //SSAO technique postFX_blurX { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X ps_main_blurX(); } } technique postFX_blurY { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X ps_main_blurY(); } } ///////////////////////////////////////////////////////////////////////////////////// //initial luminance calculation step float4 ps_main_postFX_Average(float2 texCoord: TEXCOORD0) : COLOR { static const float Offsets[4] = {-1.5f, -0.5f, 0.5f, 1.5f}; float _max = 0; float _log_sum = 0; for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { float2 vOffset = float2(Offsets[x], Offsets[y]) * float2(g_HalfPixel_ViewportSizeInv.y, g_HalfPixel_ViewportSizeInv.w); float3 color_here = tex2D(postFX_sampler0, texCoord + vOffset).rgb; float lum_here = dot(color_here * HDRRange, LUMINANCE_WEIGHTS); _log_sum += /*log*/(lum_here/*+ 0.0000001f*/); _max = max(_max, lum_here); } } return float4(_log_sum / 16, _max, 0, 1); } technique postFX_Average { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X ps_main_postFX_Average(); } } float4 ps_main_postFX_AverageAvgMax(float2 texCoord: TEXCOORD0, uniform const bool smooth) : COLOR { static const float Offsets[4] = {-1.5f, -0.5f, 0.5f, 1.5f}; float _max = 0; float _sum = 0; //downsample and find avg-max luminance for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { float2 vOffset = float2(Offsets[x], Offsets[y]) * float2(g_HalfPixel_ViewportSizeInv.y, g_HalfPixel_ViewportSizeInv.w); float2 lumAvgMax_here = tex2D(postFX_sampler0, texCoord + vOffset).rg; _sum += lumAvgMax_here.r * lumAvgMax_here.r; _max = max(_max, lumAvgMax_here.g); } } float _avg = _sum / 16; float4 new_ret = float4(sqrt(_avg), _max, 0, 1); if(smooth) { //last step, finish average luminance calculation new_ret.r = /*exp*/(new_ret.r); float2 prev_avgmax = tex2D(postFX_sampler4, float2(0.5f, 0.5f)).rg; //new_ret.xy = lerp(prev_avgmax, new_ret, /*1.0f); //*/g_HDR_frameTime );/***/ new_ret.x = lerp(prev_avgmax.x, new_ret.x, g_HDR_frameTime ); new_ret.y = max(0.1f, lerp(prev_avgmax.y, new_ret.y, g_HDR_frameTime ) ); } return new_ret; } technique postFX_AverageAvgMax { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X ps_main_postFX_AverageAvgMax(false); } } technique postFX_AverageAvgMax_Smooth { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X ps_main_postFX_AverageAvgMax(true); } } ///////////////////////////////////////////////////////////////////////////////////// struct VsOut_Convert_FP2I { float4 Pos: POSITION; float2 texCoord0: TEXCOORD0; float2 texCoord1: TEXCOORD1; float2 texCoord2: TEXCOORD2; float2 texCoord3: TEXCOORD3; }; VsOut_Convert_FP2I vs_main_postFX_Convert_FP2I(float4 pos: POSITION){ VsOut_Convert_FP2I Out; Out.Pos = pos; // Texture coordinates float2 texCoord = (float2(pos.x, -pos.y) * 0.5f + 0.5f) + g_HalfPixel_ViewportSizeInv.xy; Out.texCoord0 = texCoord + float2(-1.0, 1.0) * g_HalfPixel_ViewportSizeInv.xy; Out.texCoord1 = texCoord + float2( 1.0, 1.0) * g_HalfPixel_ViewportSizeInv.xy; Out.texCoord2 = texCoord + float2( 1.0, -1.0) * g_HalfPixel_ViewportSizeInv.xy; Out.texCoord3 = texCoord + float2(-1.0, -1.0) * g_HalfPixel_ViewportSizeInv.xy; return Out; } float4 ps_main_postFX_Convert_FP2I(float2 texCoord0: TEXCOORD0, float2 texCoord1: TEXCOORD1, float2 texCoord2: TEXCOORD2, float2 texCoord3: TEXCOORD3) : COLOR0 { float3 rt; #define gamma_corrected_input #ifdef gamma_corrected_input rt = tex2D(postFX_sampler4, texCoord0).rgb; rt += tex2D(postFX_sampler4, texCoord1).rgb; rt += tex2D(postFX_sampler4, texCoord2).rgb; rt += tex2D(postFX_sampler4, texCoord3).rgb; #else rt = pow(tex2D(postFX_sampler4, texCoord0).rgb, output_gamma); rt += pow(tex2D(postFX_sampler4, texCoord1).rgb, output_gamma); rt += pow(tex2D(postFX_sampler4, texCoord2).rgb, output_gamma); rt += pow(tex2D(postFX_sampler4, texCoord3).rgb, output_gamma); #endif rt *= 0.25; //rt = BrightPass(rt); rt *= HDRRangeInv; return float4(rt.rgb,1); } technique postFX_Convert_FP2I { pass P0 { VertexShader = compile vs_2_0 vs_main_postFX_Convert_FP2I(); PixelShader = compile ps_2_0 ps_main_postFX_Convert_FP2I(); } } ///////////////////////////////////////////////////////////////////////////////////// float4 ps_main_postFX_DofBlur(uniform const bool using_hdr, uniform const bool using_depth, float2 texCoord: TEXCOORD0) : COLOR { float3 sample_start = tex2D(postFX_sampler0, texCoord).rgb; float depth_start; if(using_depth) { depth_start = tex2D(postFX_sampler1, texCoord).rgb; } static const int SAMPLE_COUNT = 8; static const float2 offsets[SAMPLE_COUNT] = { -1, -1, 0, -1, 1, -1, -1, 0, 1, 0, -1, 1, 0, 1, 1, 1, }; float sampleDist = g_HalfPixel_ViewportSizeInv.x * 3.14f; float3 sample = sample_start; for (int i = 0; i < SAMPLE_COUNT; i++) { float2 sample_pos = texCoord + sampleDist * offsets[i]; // !using_hdr -> non-lineer gamma! float3 sample_here; if(using_depth) { float depth_here = tex2D(postFX_sampler1, sample_pos).r; if(depth_here < depth_start) { sample_here = sample_start; } else { sample_here = tex2D(postFX_sampler0, sample_pos).rgb; } } else { sample_here = tex2D(postFX_sampler0, sample_pos).rgb; } sample += sample_here; } sample /= SAMPLE_COUNT+1; //sample.rgb = pow(sample, input_gamma); //sample.rgb = pow(sample.rgb, output_gamma_inv); //return pow(tex2D(postFX_sampler0, texCoord), input_gamma); return float4(sample.rgb, 1); } technique postFX_DofBlurHDR { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile ps_2_0 ps_main_postFX_DofBlur(true, false); } } technique postFX_DofBlurLDR { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile ps_2_0 ps_main_postFX_DofBlur(false, false); } } technique postFX_DofBlurHDR_Depth { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile ps_2_0 ps_main_postFX_DofBlur(true, true); } } technique postFX_DofBlurLDR_Depth { pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile ps_2_0 ps_main_postFX_DofBlur(false, true); } } ///////////////////////////////////////////////////////////////////////////////////// float4 FinalScenePassPS(uniform const bool use_dof, uniform const int use_hdr, uniform const bool use_auto_exp, float2 texCoord: TEXCOORD0) : COLOR { // Sample the scene float4 scene = tex2D(postFX_sampler0, texCoord); scene.rgb = pow(scene.rgb, output_gamma); #ifndef ENABLE_EDITOR //we disable dof in editor mode so that we can fit in ps 2.0 if(use_dof) { float pixelDepth = tex2D(postFX_sampler4, texCoord).r; float focus_factor01 = abs(g_DOF_Focus - pixelDepth); // static const bool use_depthRT_focus = false; // if(use_depthRT_focus) { // focus_factor01 = tex2D(postFX_samplerX, texCoord).r; // } float lerp_factor = min(saturate(g_DOF_Range * focus_factor01), 0.62); //float lerp_factor = saturate(5 * focus_factor01); static const bool use_wignette = true; if(use_wignette) { lerp_factor *= 1 - vignette(float2(texCoord.x*2-1, texCoord.y-0.6), 0.015, 0.5); //remove blur from center } float4 dofColor = tex2D(postFX_sampler3, texCoord); if(use_hdr) { dofColor *= HDRRange; } dofColor.rgb = pow(dofColor.rgb, output_gamma); scene = lerp(scene, dofColor, lerp_factor); } #endif float4 color, blur; float3 AlteredColor; float LerpVal; if(use_hdr > 0) { blur = tex2D(postFX_sampler1, texCoord); blur.rgb = pow(blur.rgb, BlurStrenght); blur.rgb *= HDRRange; float2 luminanceAvgMax; if(use_auto_exp) { luminanceAvgMax = tex2D(postFX_sampler2, float2(0.5f, 0.5f)).rg; } else { luminanceAvgMax = float2(0.5, 10.2); } // tonemap begin color = scene; //LIGHTING CORRECTION if(use_auto_exp) { float greyscale = dot(color.rgb, float3(0.30, 0.59, 0.11)); float HighTones = pow(greyscale,2); float LowTones = pow(1 - greyscale,2); float MidTones = pow(sqrt(pow(0.5-greyscale,2)),2); AlteredColor.b = color.b * ((1+HighTones)*1.1); //AlteredColor.g = color.g * ((1+HighTones)*0.8); //AlteredColor.b = color.b * ((1+LowTones)*0.9); AlteredColor.g = color.g * ((1+LowTones)*1.05); AlteredColor.r = color.r * ((1+LowTones)*1.09); LerpVal = 0.45; color.rgb = lerp(color.rgb,AlteredColor.rgb,LerpVal); color.rgb = pow(color.rgb,1.4); float Custom_blur = tex2D(postFX_sampler1, texCoord); Custom_blur = pow(Custom_blur,2.00); Custom_blur *= 100; color.rgb += Custom_blur; } else { AlteredColor = color; LerpVal = 0; color.rgb = lerp(color.rgb,AlteredColor.rgb,LerpVal); } //color.rgb = lerp(color.rgb,AlteredColor.rgb,LerpVal); //color.rgb = pow(color.rgb,1.1); // tonemap continue color += blur * BlurAmount; color.rgb = tonemapping(color.rgb, luminanceAvgMax, postfxTonemapOp); } else { color = scene; } //greyscale float greyscale = dot(color.rgb, float3(0.30, 0.59, 0.11)); color.rgb = (lerp(greyscale, color.rgb, 0.70)); //float3 pixelDepth2 = tex2D(postFX_sampler4, texCoord).rgb; //color.rgb = (pixelDepth2); //gamma correction color.rgb = pow(color.rgb, output_gamma_inv); //////////////////// //--float2 luminanceAvgMax = tex2D(postFX_sampler2, float2(0.5f, 0.5f)).rg; //--return tex2D(postFX_sampler2, texCoord).y * 100; //GI const float gi_strength = 0.75f; const float gi_contrast = 0.25f; const float gi_saturation = 0.75f; const float gi_max_distance = 1.0f; const float3 luma = float3(0.2126, 0.7152, 0.0722); float3 c = color.rgb; float depth=0 ; float3 bounce=0; float4 gi=0; float4 bounce_area = 0; bounce_area = tex2D(postFX_sampler0, float4(texCoord.x,texCoord.y,0, 2.5)); //depth = tex2D(postFX_sampler0, float4(texCoord.x,texCoord.y, 0, 1.5)).w; depth = tex2D(postFX_sampler5, texCoord).r; gi = tex2D(postFX_sampler0, texCoord); float gi_bright = max(gi.r,max(gi.g,gi.b)) + min(gi.r,min(gi.g,gi.b)); //Estimate amount of white light hitting area float light = gi_bright+.005; // Estimate base unlit color of c float3 unlit_c2 = c/light; //We take our bounce light and multiply it by base unlit color of c to get amount reflected from c. bounce=lerp(bounce, unlit_c2*max(0,2*bounce_area.rgb-c), 1.0); float contrast = dot(luma,max(0,c-0.10)/max(bounce_area.rgb+gi.rgb,0.00001)); contrast = (contrast)/(1+contrast)+.66666666667; contrast = lerp(1, contrast, gi_contrast); // Fake Global Illumination - even without depth it works better than you might expect! //estimate ambient light hitting pixel as blend between local and area brighness. float3 avg_light = length((2*gi_strength)*c+gi.rgb)/(1+2*gi_strength); float3 ambient = min(avg_light, lerp(1, 1+length(gi.rgb/(c+gi.rgb)),gi_saturation*gi_strength)*gi.rgb ); float3 gi_bounce = (1+.5*gi_saturation*gi_strength)*c*gi.rgb/ambient; //This adjustment is to avoid neaby objects casting color onto ones much further away. float gi_ratio = min(1, (gi.w+0.00001)/(depth+0.00001)); // This adjustment is to fade out as we reach the sky gi_ratio *= max(0, 1-depth*depth*depth*rcp(gi_max_distance*gi_max_distance*gi_max_distance)); c = lerp(c, gi_bounce*1.5f , .4*gi_ratio); c *= contrast; float ao = tex2D(postFX_sampler1, texCoord).a; bounce = bounce*1.0*min(ao,.5); ao *= 1.6f; // multiply to compensate for the bounce value we're adding bounce = min(c*ao,bounce); // Make sure bounce doesn't make pixel brighter than original. //ao *= 0.8f; //apply AO and clamp the pixel to avoid going completely black or white. c = clamp( c*(1-ao) + bounce, 0.25f*c, c ); //Tonemap c= Tonemap(c, 0.85, 0.0, 0.4, 0.05, 0.0, float3(0.0,0.0,0.0)); //Tonemap(float3 color, float Gamma, float Exposure, float Saturation, float Bleach, float Defog, float3 FogColor) color.rgb = c; return color; } //postFX_final_[dof]_[hdr_quality]_[auto_exposure] technique postFX_final_0_0_0{ pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X FinalScenePassPS( false, 0, false); } } technique postFX_final_0_1_0{ pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X FinalScenePassPS( false, 1, false); } } technique postFX_final_0_2_0{ pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X FinalScenePassPS( false, 2, false); } } technique postFX_final_0_1_1{ pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X FinalScenePassPS( false, 1, true); } } technique postFX_final_0_2_1{ pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X FinalScenePassPS( false, 2, true); } } technique postFX_final_1_0_0{ pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X FinalScenePassPS( true, 0, false); } } technique postFX_final_1_1_0{ pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X FinalScenePassPS( true, 1, false); } } technique postFX_final_1_2_0{ pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X FinalScenePassPS( true, 2, false); } } technique postFX_final_1_1_1{ pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X FinalScenePassPS( true, 1, true); } } technique postFX_final_1_2_1{ pass P0 { VertexShader = vs_main_postFX_compiled; PixelShader = compile PS_2_X FinalScenePassPS( true, 2, true); } } #if WSE2 #include "postFX_WSE2.fx" #endif