SHARE
TWEET

Untitled

a guest Mar 28th, 2017 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////////////////////////////////
  2. //POST EFFECT SHADERS........
  3.  
  4. #include "fx_configuration.h"
  5.  
  6. //#define ENABLE_EDITOR
  7. //#define USE_CHARACTER_SHADOW_MERGE
  8.  
  9. float4 output_gamma = float4(2.2f, 2.2f, 2.2f, 2.2f);   //str_todo: vectorize
  10. float4 output_gamma_inv = float4(1.0f / 2.2f, 1.0f / 2.2f, 1.0f / 2.2f, 1.0f / 2.2f);
  11.  
  12. static const float3 LUMINANCE_WEIGHTS = float3(0.299f, 0.587f, 0.114f);
  13. static const float min_exposure = 0.15f;
  14. static const float max_exposure = 3.0f;
  15.  
  16. #pragma warning(disable: 3571)      //pow(f,e) warning!
  17.  
  18. #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)
  19.  
  20. // use postFX_sampler4 for point sampling
  21. #if defined(USE_FX_STATE_MANAGER) && !defined(USE_DEVICE_TEXTURE_ASSIGN)    //else we can use direct device access with sampler indexes...
  22.     texture postFX_texture0, postFX_texture1, postFX_texture2, postFX_texture3, postFX_texture4;
  23.  
  24.     //non-srgb samplers
  25.     sampler postFX_sampler0 : register(s0) = sampler_state  {  Texture = postFX_texture0;  };   //linear clamp
  26.     sampler postFX_sampler1 : register(s1) = sampler_state  {  Texture = postFX_texture1;  };   //linear clamp
  27.     sampler postFX_sampler2 : register(s2) = sampler_state  {  Texture = postFX_texture2;  };   //linear clamp
  28.     sampler postFX_sampler3 : register(s3) = sampler_state  {  Texture = postFX_texture3;  };   //linear clamp
  29.     sampler postFX_sampler4 : register(s4) = sampler_state  {  Texture = postFX_texture4;  };   //linear clamp
  30.    
  31. #else
  32.  
  33.     #ifdef USE_REGISTERED_SAMPLERS
  34.     sampler postFX_sampler0 : register(s0); //linear clamp
  35.     sampler postFX_sampler1 : register(s1); //linear clamp
  36.     sampler postFX_sampler2 : register(s2); //linear clamp
  37.     sampler postFX_sampler3 : register(s3); //linear clamp
  38.     sampler postFX_sampler4 : register(s4); //linear clamp
  39.     #else
  40.     sampler postFX_sampler0 : register(s0) = sampler_state{ AddressU = CLAMP; AddressV = CLAMP; MinFilter = LINEAR; MagFilter = LINEAR;  }; //linear clamp
  41.     sampler postFX_sampler1 : register(s1) = sampler_state{ AddressU = CLAMP; AddressV = CLAMP; MinFilter = LINEAR; MagFilter = LINEAR;  }; //linear clamp
  42.     sampler postFX_sampler2 : register(s2) = sampler_state{ AddressU = CLAMP; AddressV = CLAMP; MinFilter = LINEAR; MagFilter = LINEAR;  }; //linear clamp
  43.     sampler postFX_sampler3 : register(s3) = sampler_state{ AddressU = CLAMP; AddressV = CLAMP; MinFilter = LINEAR; MagFilter = LINEAR;  }; //linear clamp
  44.     sampler postFX_sampler4 : register(s4) = sampler_state{ AddressU = CLAMP; AddressV = CLAMP; MinFilter = LINEAR; MagFilter = LINEAR;  }; //linear clamp
  45.     #endif
  46.  
  47. #endif
  48.                                                                            
  49. static const float BlurPixelWeight[8] = { 0.256, 0.240, 0.144, 0.135, 0.120, 0.065, 0.030, 0.010 };
  50. //static const float BlurPixelWeight[8] = { 0.35537, 0.34185, 0.23821, 0.125861, 0.0813562, 0.04862, 0.025, 0.012552 };
  51. //static const float BlurPixelWeight[8] = { 0.5537, 0.4185, 0.3821, 0.25861, 0.13562, 0.0862, 0.05, 0.02552 };
  52. //static const float BlurPixelWeight[8] = { 0.2537, 0.2185, 0.1821, 0.15861, 0.082, 0.062, 0.03, 0.01552 };
  53.  
  54. bool showing_ranged_data = false;
  55.  
  56. float4 g_HalfPixel_ViewportSizeInv;
  57. float  g_HDR_frameTime;
  58.  
  59. float g_DOF_Focus = -0.005;
  60. float g_DOF_Range = 5.19876;
  61.  
  62. #ifndef PS_2_X
  63.     #define PS_2_X ps_2_b
  64. #endif
  65.  
  66. #ifdef ENABLE_EDITOR
  67.     //
  68.     //postFX0- x:blurStr, y:exposure, z:range, w:temp
  69.     //postFX1- brightness
  70.     //postFX2- constrast
  71.     //postFX3- saturation
  72.  
  73.     float4 postfx_editor_vector[4];
  74.    
  75.     #undef postfxTonemapOp
  76.     #undef postfxParams1
  77.     #undef postfxParams2
  78.     #undef postfxParams3
  79.    
  80.     #define postfxTonemapOp     ( int(postfx_editor_vector[0].x) )
  81.     #define postfxParams1       float4(postfx_editor_vector[1].x, postfx_editor_vector[1].y, postfx_editor_vector[1].z, postfx_editor_vector[1].w)
  82.     #define postfxParams2       float4(postfx_editor_vector[2].x, postfx_editor_vector[2].y, postfx_editor_vector[2].z, postfx_editor_vector[2].w)
  83.     #define postfxParams3       float4(postfx_editor_vector[3].x, postfx_editor_vector[3].y, postfx_editor_vector[3].z, postfx_editor_vector[3].w)
  84.                                                                                                                                          
  85.     #define RELATIVE_PS_TARGET PS_2_X   //we need more instruction_count to edit things dynamically
  86. #else
  87.     //COMPILE_TIME_POSTFX_CONSTANTS
  88.     // constants are defined from application to reduce inst. #
  89.     //--#define     postfxTonemapOp ((int)0)
  90.     //--#define     postfxParams1 (float4(16.0f, 1.0f, 1.0f, 1.0f))
  91.     //--#define     postfxParams2 (float4( 1.0f, 1.0f, 1.0f, 1.0f))
  92.    
  93.     #define RELATIVE_PS_TARGET ps_2_0
  94. #endif
  95.  
  96. #define HDRRange                (postfxParams1.x)
  97. #define HDRExposureScaler       (postfxParams1.y)
  98. #define LuminanceAverageScaler  (postfxParams1.z)
  99. #define LuminanceMaxScaler      (postfxParams1.w)
  100.  
  101. #define BrightpassTreshold  (postfxParams2.x)
  102. #define BrightpassPostPower (postfxParams2.y)
  103. #define BlurStrenght        (postfxParams2.z)
  104. #define BlurAmount          (postfxParams2.w)
  105.  
  106.  
  107. #define HDRRangeInv         (1.0f / HDRRange)
  108.  
  109. float CalculateWignette(float2 tc) {
  110.     tc = tc - 0.5; // [-1/2, 1/2]
  111.     return pow(1-dot(tc,tc), 4);
  112. }
  113. float4 radial(sampler2D tex, float2 texcoord, int samples, float startScale = 1.0, float scaleMul = 0.9){
  114.     float4 c = 0;
  115.     float scale = startScale;
  116.     for(int i=0; i<samples; i++) {
  117.         float2 uv = ((texcoord-0.5)*scale)+0.5;
  118.         float4 s = tex2D(tex, uv);
  119.         c += s;
  120.         scale *= scaleMul;
  121.     }
  122.     c /= samples;
  123.     return c;
  124. }
  125. float vignette(float2 pos, float inner, float outer){
  126.   //float r = length(pos); //orj
  127.   float r = dot(pos,pos);
  128.   r = 1.0 - smoothstep(inner, outer, r);
  129.   return r;
  130. }
  131.  
  132. float3 tonemapping(const float3 scene_color, const float2 luminanceAvgMax, const int tonemapOp) {
  133.    
  134.     float lum_avg       = luminanceAvgMax.x * LuminanceAverageScaler;
  135.     float lum_max       = luminanceAvgMax.y * LuminanceMaxScaler;
  136.    
  137.     static const float MiddleValue = 0.85f;
  138.     //float exposure = 1.4427 / (0.5 + lum_avg);
  139.     float exposure = MiddleValue / (0.00001 + lum_avg);
  140.     exposure = clamp(exposure*HDRExposureScaler, min_exposure, max_exposure);
  141.    
  142.     float3 scene_color_exposed = scene_color * exposure;
  143.    
  144.     float3 final_color;
  145.     {
  146.         if( tonemapOp==0 )
  147.         {
  148.             final_color = scene_color_exposed;
  149.         }
  150.         else if( tonemapOp==1 )
  151.         {
  152.             final_color.rgb = 1.0 - exp2(-scene_color_exposed);
  153.         }
  154.         else if( tonemapOp==2 )
  155.         {
  156.             final_color = scene_color_exposed / (scene_color_exposed+1);
  157.         }
  158.         else //if( tonemapOp==3 )
  159.         {
  160.             float Lp = (exposure / lum_avg) * max(scene_color_exposed.r, max(scene_color_exposed.g, scene_color_exposed.b));
  161.             float LmSqr = lum_max; //(lum_max * lum_max) * (lum_max * lum_max);
  162.             float toneScalar = ( Lp * ( 1.0f + ( Lp / ( LmSqr ) ) ) ) / ( 1.0f + Lp );
  163.  
  164.             final_color = scene_color_exposed * toneScalar;
  165.         }
  166.     }
  167.    
  168.     return final_color;
  169. }
  170.  
  171. /////////////////////////////////////////////////////////////////////////////////////
  172. struct VS_OUT_POSTFX
  173. {
  174.     float4 Pos: POSITION;
  175.     float2 Tex: TEXCOORD0;
  176. };
  177. VS_OUT_POSTFX vs_main_postFX(float4 pos: POSITION){
  178.     VS_OUT_POSTFX Out;
  179.  
  180.     Out.Pos = pos;
  181.     Out.Tex = (float2(pos.x, -pos.y) * 0.5f + 0.5f) + g_HalfPixel_ViewportSizeInv.xy;
  182.    
  183.     return Out;
  184. }
  185. VertexShader vs_main_postFX_compiled = compile vs_2_0 vs_main_postFX();
  186.  
  187. /////////////////////////////////////////////////////////////////////////////////////
  188. float4 ps_main_postFX_Show(float2 texCoord: TEXCOORD0) : COLOR {
  189.  
  190.     float4 color = tex2D(postFX_sampler0, texCoord);
  191.    
  192.     if(showing_ranged_data)
  193.     {
  194.         color.rgb *= HDRRange;
  195.         color.rgb = pow(color.rgb, output_gamma_inv);
  196.     }
  197.     return color;
  198. }
  199. technique postFX_Show
  200. {
  201.     pass P0
  202.     {
  203.         VertexShader = vs_main_postFX_compiled;
  204.         PixelShader = compile ps_2_0 ps_main_postFX_Show();
  205.     }
  206. }
  207.  
  208. /////////////////////////////////////////////////////////////////////////////////////
  209. #ifdef USE_CHARACTER_SHADOW_MERGE
  210.  
  211.  
  212. float4 ps_main_postFX_Shadowmap(float2 texCoord: TEXCOORD0) : COLOR {
  213.  
  214.     float original_shadowmap = tex2D(postFX_sampler0, texCoord).r;
  215.    
  216.     float character_shadow = tex2D(postFX_sampler1, texCoord).r;
  217.    
  218.     return min(original_shadowmap, character_shadow);
  219. }
  220. technique shadowmap_updater
  221. {
  222.     pass P0
  223.     {
  224.         VertexShader = vs_main_postFX_compiled;
  225.         PixelShader = compile ps_2_0 ps_main_postFX_Shadowmap();
  226.     }
  227. }
  228. #endif
  229. /////////////////////////////////////////////////////////////////////////////////////
  230. float4 color_value;
  231.  
  232. float4 ps_main_postFX_TrueColor(float2 texCoord: TEXCOORD0) : COLOR
  233. {
  234.     const bool use_vignette = true;
  235.    
  236.     float4 ret = color_value;
  237.     if(use_vignette)
  238.     {
  239.         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
  240.     }
  241.     return ret;
  242. }
  243. technique postFX_TrueColor
  244. {
  245.     pass P0
  246.     {
  247.         VertexShader = vs_main_postFX_compiled;
  248.         PixelShader = compile ps_2_0 ps_main_postFX_TrueColor();
  249.     }
  250. }
  251.  
  252. /////////////////////////////////////////////////////////////////////////////////////
  253.  
  254. float4 ps_main_brightPass(uniform const bool with_luminance, float2 inTex: TEXCOORD0 ) : COLOR0 {
  255.  
  256.     float3 color = tex2D( postFX_sampler0, inTex );
  257.        
  258.     //get real-range
  259.     color *= HDRRange;
  260.        
  261.     //bright pass
  262.     if(with_luminance)  //use luminance information to calculate exposure factor to be applied on blur rt
  263.     {
  264.         float2 lum_avgmax = tex2D( postFX_sampler4, float2(0.5f, 0.5f) ).rg;
  265.         //color.rgb = tonemapping(color.rgb, lum_avgmax,0);//get exposed color
  266.        
  267.             static const float MiddleValue = 0.85f;
  268.             //float exposure = 1.4427 / (0.5 + lum_avg);
  269.             float exposure_factor = MiddleValue / (0.00001 + lum_avgmax.x);
  270.             float exposure = 0.85 + exposure_factor * 0.15;
  271.             exposure = clamp(exposure*HDRExposureScaler, min_exposure, max_exposure);
  272.            
  273.             color.rgb = color.rgb * exposure;
  274.        
  275.         color.rgb = max(0.0f, color.rgb - BrightpassTreshold);
  276.         /*color.rgb = pow(color.rgb, BrightpassPostPower);
  277.         */
  278.         float intensity = dot(color.rgb, float3(.5f, .5f, .5f));
  279.         float bloom_intensity = pow(intensity, BrightpassPostPower);
  280.         color.rgb = color.rgb * ( bloom_intensity/intensity );
  281.     }
  282.     else
  283.     {
  284.         color.rgb = max(0.0f, color.rgb - BrightpassTreshold);
  285.         color.rgb = pow(color.rgb, BrightpassPostPower);
  286.     }
  287.    
  288.     if(dot(color.rgb, color.rgb) > 1000)
  289.     {
  290.         //avoid invalid flashes due to fp calc for nvidia cards..
  291.         color.rgb = float3(0, 0, 0);
  292.     }
  293.    
  294.     //we use interger format, turn back to normalized range
  295.     color *= HDRRangeInv;
  296.    
  297.     return float4(color,1);
  298. }
  299. technique postFX_brightPass
  300. {
  301.     pass P0
  302.     {
  303.         VertexShader = vs_main_postFX_compiled;
  304.         PixelShader = compile ps_2_0 ps_main_brightPass(false);
  305.     }
  306. }
  307. technique postFX_brightPass_WithLuminance
  308. {
  309.     pass P0
  310.     {
  311.         VertexShader = vs_main_postFX_compiled;
  312.         PixelShader = compile ps_2_0 ps_main_brightPass(true);
  313.     }
  314. }
  315.  
  316. /////////////////////////////////////////////////////////////////////////////////////
  317.  
  318. float4 ps_main_blurX( float2 inTex: TEXCOORD0 ) : COLOR0 {
  319.  
  320.     float2 BlurOffsetX = float2(g_HalfPixel_ViewportSizeInv.z,0);
  321.     float4 color = 0;
  322.    
  323.     for( int i = 0; i < 8; i++ )
  324.     {
  325.         color += tex2D( postFX_sampler0, inTex + ( BlurOffsetX * i ) ) * BlurPixelWeight[i];
  326.         color += tex2D( postFX_sampler0, inTex - ( BlurOffsetX * i ) ) * BlurPixelWeight[i];
  327.     }
  328.    
  329.     return color;
  330. }
  331. float4 ps_main_blurY( float2 inTex: TEXCOORD0 ) : COLOR0 {
  332.     float4 color = 0;//tex2D( postFX_sampler0, inTex ) ;
  333.  
  334.     float2 BlurOffsetY = float2(0, g_HalfPixel_ViewportSizeInv.w);
  335.    
  336.     for( int i = 0; i < 8; i++ )
  337.     {
  338.         color += tex2D( postFX_sampler0, inTex + ( BlurOffsetY * i ) ) * BlurPixelWeight[i];
  339.         color += tex2D( postFX_sampler0, inTex - ( BlurOffsetY * i ) ) * BlurPixelWeight[i];
  340.     }
  341.    
  342.     return color;
  343. }
  344. technique postFX_blurX
  345. {
  346.     pass P0
  347.     {
  348.         VertexShader = vs_main_postFX_compiled;
  349.         PixelShader = compile ps_2_0 ps_main_blurX();
  350.     }
  351. }
  352. technique postFX_blurY
  353. {
  354.     pass P0
  355.     {
  356.         VertexShader = vs_main_postFX_compiled;
  357.         PixelShader = compile ps_2_0 ps_main_blurY();
  358.     }
  359. }
  360.  
  361. /////////////////////////////////////////////////////////////////////////////////////
  362. //initial luminance calculation step
  363. float4 ps_main_postFX_Average(float2 texCoord: TEXCOORD0) : COLOR {
  364.  
  365.     static const float Offsets[4] = {-1.5f, -0.5f, 0.5f, 1.5f};
  366.            
  367.     float _max = 0;
  368.     float _log_sum = 0;
  369.    
  370.     for (int x = 0; x < 4; x++)
  371.     {
  372.         for (int y = 0; y < 4; y++)
  373.         {
  374.             float2 vOffset = float2(Offsets[x], Offsets[y]) * float2(g_HalfPixel_ViewportSizeInv.y, g_HalfPixel_ViewportSizeInv.w);
  375.             float3 color_here = tex2D(postFX_sampler0, texCoord + vOffset).rgb;
  376.             float lum_here = dot(color_here * HDRRange, LUMINANCE_WEIGHTS);
  377.            
  378.             _log_sum += /*log*/(lum_here/*+ 0.0000001f*/);
  379.             _max = max(_max, lum_here);
  380.         }
  381.     }
  382.    
  383.     return float4(_log_sum / 16, _max, 0, 1);
  384. }
  385. technique postFX_Average
  386. {
  387.     pass P0
  388.     {
  389.         VertexShader = vs_main_postFX_compiled;
  390.         PixelShader = compile PS_2_X ps_main_postFX_Average();
  391.     }
  392. }
  393.  
  394. float4 ps_main_postFX_AverageAvgMax(float2 texCoord: TEXCOORD0, uniform const bool smooth) : COLOR {
  395.  
  396.     static const float Offsets[4] = {-1.5f, -0.5f, 0.5f, 1.5f};
  397.            
  398.     float _max = 0;
  399.     float _sum = 0;
  400.    
  401.     //downsample and find avg-max luminance
  402.     for (int x = 0; x < 4; x++)
  403.     {
  404.         for (int y = 0; y < 4; y++)
  405.         {
  406.             float2 vOffset = float2(Offsets[x], Offsets[y]) * float2(g_HalfPixel_ViewportSizeInv.y, g_HalfPixel_ViewportSizeInv.w);
  407.             float2 lumAvgMax_here = tex2D(postFX_sampler0, texCoord + vOffset).rg;
  408.            
  409.             _sum += lumAvgMax_here.r * lumAvgMax_here.r;
  410.             _max = max(_max, lumAvgMax_here.g);
  411.         }
  412.     }
  413.     float _avg = _sum / 16;
  414.    
  415.     float4 new_ret = float4(sqrt(_avg), _max, 0, 1);
  416.    
  417.     if(smooth)
  418.     {
  419.         //last step,  finish average luminance calculation
  420.         new_ret.r = /*exp*/(new_ret.r);
  421.        
  422.         float2 prev_avgmax = tex2D(postFX_sampler4, float2(0.5f, 0.5f)).rg;
  423.        
  424.         //new_ret.xy = lerp(prev_avgmax, new_ret, /*1.0f); //*/g_HDR_frameTime );/***/
  425.         new_ret.x = lerp(prev_avgmax.x, new_ret.x, g_HDR_frameTime );
  426.         new_ret.y = max(0.1f, lerp(prev_avgmax.y, new_ret.y, g_HDR_frameTime ) );
  427.     }
  428.    
  429.     return new_ret;
  430. }
  431. technique postFX_AverageAvgMax
  432. {
  433.     pass P0
  434.     {
  435.         VertexShader = vs_main_postFX_compiled;
  436.         PixelShader = compile PS_2_X ps_main_postFX_AverageAvgMax(false);
  437.     }
  438. }
  439. technique postFX_AverageAvgMax_Smooth
  440. {
  441.     pass P0
  442.     {
  443.         VertexShader = vs_main_postFX_compiled;
  444.         PixelShader = compile PS_2_X ps_main_postFX_AverageAvgMax(true);
  445.     }
  446. }
  447.  
  448. /////////////////////////////////////////////////////////////////////////////////////
  449. struct VsOut_Convert_FP2I
  450. {
  451.     float4 Pos:         POSITION;
  452.     float2 texCoord0:   TEXCOORD0;
  453.     float2 texCoord1:   TEXCOORD1;
  454.     float2 texCoord2:   TEXCOORD2;
  455.     float2 texCoord3:   TEXCOORD3;
  456. };
  457. VsOut_Convert_FP2I vs_main_postFX_Convert_FP2I(float4 pos: POSITION){
  458.     VsOut_Convert_FP2I Out;
  459.  
  460.     Out.Pos = pos;
  461.  
  462.     // Texture coordinates
  463.     float2 texCoord = (float2(pos.x, -pos.y) * 0.5f + 0.5f) + g_HalfPixel_ViewportSizeInv.xy;
  464.    
  465.     Out.texCoord0 = texCoord + float2(-1.0,  1.0) * g_HalfPixel_ViewportSizeInv.xy;
  466.     Out.texCoord1 = texCoord + float2( 1.0,  1.0) * g_HalfPixel_ViewportSizeInv.xy;
  467.     Out.texCoord2 = texCoord + float2( 1.0, -1.0) * g_HalfPixel_ViewportSizeInv.xy;
  468.     Out.texCoord3 = texCoord + float2(-1.0, -1.0) * g_HalfPixel_ViewportSizeInv.xy;
  469.    
  470.     return Out;
  471. }
  472. float4 ps_main_postFX_Convert_FP2I(float2 texCoord0: TEXCOORD0, float2 texCoord1: TEXCOORD1, float2 texCoord2: TEXCOORD2, float2 texCoord3: TEXCOORD3) : COLOR0 {
  473.  
  474.     float3 rt;
  475.    
  476. #define gamma_corrected_input
  477. #ifdef gamma_corrected_input
  478.     rt  = tex2D(postFX_sampler4, texCoord0).rgb;
  479.     rt += tex2D(postFX_sampler4, texCoord1).rgb;
  480.     rt += tex2D(postFX_sampler4, texCoord2).rgb;
  481.     rt += tex2D(postFX_sampler4, texCoord3).rgb;
  482. #else
  483.  
  484.     rt  = pow(tex2D(postFX_sampler4, texCoord0).rgb, output_gamma);
  485.     rt += pow(tex2D(postFX_sampler4, texCoord1).rgb, output_gamma);
  486.     rt += pow(tex2D(postFX_sampler4, texCoord2).rgb, output_gamma);
  487.     rt += pow(tex2D(postFX_sampler4, texCoord3).rgb, output_gamma);
  488. #endif
  489.     rt *= 0.25;
  490.     //rt = BrightPass(rt);
  491.     rt *= HDRRangeInv;
  492.    
  493.     return float4(rt.rgb,1);
  494. }
  495. technique postFX_Convert_FP2I
  496. {
  497.     pass P0
  498.     {
  499.         VertexShader = compile vs_2_0 vs_main_postFX_Convert_FP2I();
  500.         PixelShader = compile ps_2_0 ps_main_postFX_Convert_FP2I();
  501.     }
  502. }
  503.  
  504. /////////////////////////////////////////////////////////////////////////////////////
  505. float4 ps_main_postFX_DofBlur(uniform const bool using_hdr, uniform const bool using_depth, float2 texCoord: TEXCOORD0) : COLOR {
  506.  
  507.     float3 sample_start = tex2D(postFX_sampler0, texCoord).rgb;
  508.     float depth_start;
  509.     if(using_depth)
  510.     {
  511.         depth_start = tex2D(postFX_sampler1, texCoord).rgb;
  512.     }
  513.    
  514.     static const int SAMPLE_COUNT = 8;
  515.     static const float2 offsets[SAMPLE_COUNT] = {
  516.         -1, -1,
  517.           0, -1,
  518.           1, -1,
  519.          -1,  0,
  520.           1,  0,
  521.          -1,  1,
  522.           0,  1,
  523.           1,  1,
  524.     };
  525.    
  526.     float sampleDist = g_HalfPixel_ViewportSizeInv.x * 3.14f;
  527.     float3 sample = sample_start;
  528.    
  529.     for (int i = 0; i < SAMPLE_COUNT; i++) {
  530.    
  531.         float2 sample_pos = texCoord + sampleDist * offsets[i];
  532.        
  533.         // !using_hdr -> non-lineer gamma!
  534.         float3 sample_here;
  535.         if(using_depth) {  
  536.             float depth_here = tex2D(postFX_sampler1, sample_pos).r;
  537.             if(depth_here < depth_start)
  538.             {
  539.                 sample_here = sample_start;
  540.             }
  541.             else {
  542.                 sample_here = tex2D(postFX_sampler0, sample_pos).rgb;
  543.             }
  544.         }
  545.         else {
  546.             sample_here = tex2D(postFX_sampler0, sample_pos).rgb;
  547.         }
  548.        
  549.         sample += sample_here;
  550.     }
  551.  
  552.     sample /= SAMPLE_COUNT+1;
  553.    
  554.     //sample.rgb =  pow(sample, input_gamma);
  555.     //sample.rgb = pow(sample.rgb, output_gamma_inv);
  556.    
  557.    
  558.     //return pow(tex2D(postFX_sampler0, texCoord), input_gamma);
  559.     return float4(sample.rgb, 1);
  560. }
  561. technique postFX_DofBlurHDR
  562. {
  563.     pass P0
  564.     {
  565.         VertexShader = vs_main_postFX_compiled;
  566.         PixelShader = compile ps_2_0 ps_main_postFX_DofBlur(true, false);
  567.     }
  568. }
  569. technique postFX_DofBlurLDR
  570. {
  571.     pass P0
  572.     {
  573.         VertexShader = vs_main_postFX_compiled;
  574.         PixelShader = compile ps_2_0 ps_main_postFX_DofBlur(false, false);
  575.     }
  576. }
  577. technique postFX_DofBlurHDR_Depth
  578. {
  579.     pass P0
  580.     {
  581.         VertexShader = vs_main_postFX_compiled;
  582.         PixelShader = compile ps_2_0 ps_main_postFX_DofBlur(true, true);
  583.     }
  584. }
  585. technique postFX_DofBlurLDR_Depth
  586. {
  587.     pass P0
  588.     {
  589.         VertexShader = vs_main_postFX_compiled;
  590.         PixelShader = compile ps_2_0 ps_main_postFX_DofBlur(false, true);
  591.     }
  592. }
  593.  
  594. /////////////////////////////////////////////////////////////////////////////////////
  595.  
  596. float4 FinalScenePassPS(uniform const bool use_dof, uniform const int use_hdr, uniform const bool use_auto_exp, float2 texCoord: TEXCOORD0) : COLOR {
  597.        
  598.     // Sample the scene
  599.     float4 scene = tex2D(postFX_sampler0, texCoord);
  600.     scene.rgb  = pow(scene.rgb, output_gamma);
  601.        
  602.    
  603.     #ifndef ENABLE_EDITOR   //we disable dof in editor mode so that we can fit in ps 2.0
  604.     if(use_dof) {
  605.         float  pixelDepth = tex2D(postFX_sampler4, texCoord).r;
  606.        
  607.         float focus_factor01 = abs(g_DOF_Focus - pixelDepth);
  608.        
  609.         // static const bool use_depthRT_focus = false;
  610.         // if(use_depthRT_focus) {
  611.         //  focus_factor01 = tex2D(postFX_samplerX, texCoord).r;
  612.         // }
  613.        
  614.         float lerp_factor = min(saturate(g_DOF_Range * focus_factor01), 0.62);
  615.         //float lerp_factor = saturate(5 * focus_factor01);
  616.        
  617.        
  618.         static const bool use_wignette = true;
  619.         if(use_wignette) {
  620.             lerp_factor *= 1 - vignette(float2(texCoord.x*2-1, texCoord.y-0.6), 0.015, 0.5);    //remove blur from center
  621.         }
  622.  
  623.         float4 dofColor = tex2D(postFX_sampler3, texCoord);
  624.         if(use_hdr) {
  625.             dofColor *= HDRRange;
  626.         }
  627.         dofColor.rgb  = pow(dofColor.rgb, output_gamma);
  628.        
  629.         scene = lerp(scene, dofColor, lerp_factor);
  630.     }
  631.     #endif
  632.    
  633.     float4 color, blur;
  634.    
  635.     if(use_hdr > 0) {
  636.         blur  = tex2D(postFX_sampler1,  texCoord);
  637.         blur.rgb = pow(blur.rgb, BlurStrenght);
  638.        
  639.         blur.rgb *= HDRRange;
  640.            
  641.         float2 luminanceAvgMax;
  642.         if(use_auto_exp) {
  643.             luminanceAvgMax = tex2D(postFX_sampler2, float2(0.5f, 0.5f)).rg;
  644.         }
  645.         else {
  646.             luminanceAvgMax = float2(0.5, 10.2);
  647.         }
  648.        
  649.         // tonemap..
  650.         color = scene;
  651.        
  652.         color += blur * BlurAmount;
  653.         color.rgb = tonemapping(color.rgb, luminanceAvgMax, postfxTonemapOp);
  654.     }
  655.     else {
  656.         color = scene;
  657.     }
  658.    
  659.     //gamma correction
  660.     color.rgb = pow(color.rgb, output_gamma_inv);
  661.    
  662.    
  663.     ////////////////////
  664.     //--float2 luminanceAvgMax = tex2D(postFX_sampler2, float2(0.5f, 0.5f)).rg;
  665.     //--return tex2D(postFX_sampler2, texCoord).y * 100;
  666.    
  667.     return color;
  668. }
  669.  
  670. //postFX_final_[dof]_[hdr_quality]_[auto_exposure]
  671. technique postFX_final_0_0_0{   pass P0 {   VertexShader = vs_main_postFX_compiled;     PixelShader = compile ps_2_0 FinalScenePassPS( false, 0, false);    } }
  672. technique postFX_final_0_1_0{   pass P0 {   VertexShader = vs_main_postFX_compiled;     PixelShader = compile ps_2_0 FinalScenePassPS( false, 1, false);    } }
  673. technique postFX_final_0_2_0{   pass P0 {   VertexShader = vs_main_postFX_compiled;     PixelShader = compile PS_2_X FinalScenePassPS( false, 2, false);    } }
  674. technique postFX_final_0_1_1{   pass P0 {   VertexShader = vs_main_postFX_compiled;     PixelShader = compile ps_2_0 FinalScenePassPS( false, 1, true); } }
  675. technique postFX_final_0_2_1{   pass P0 {   VertexShader = vs_main_postFX_compiled;     PixelShader = compile PS_2_X FinalScenePassPS( false, 2, true); } }
  676. technique postFX_final_1_0_0{   pass P0 {   VertexShader = vs_main_postFX_compiled;     PixelShader = compile ps_2_0 FinalScenePassPS(  true, 0, false);    } }
  677. technique postFX_final_1_1_0{   pass P0 {   VertexShader = vs_main_postFX_compiled;     PixelShader = compile ps_2_0 FinalScenePassPS(  true, 1, false);    } }
  678. technique postFX_final_1_2_0{   pass P0 {   VertexShader = vs_main_postFX_compiled;     PixelShader = compile PS_2_X FinalScenePassPS(  true, 2, false);    } }
  679. technique postFX_final_1_1_1{   pass P0 {   VertexShader = vs_main_postFX_compiled;     PixelShader = compile PS_2_X FinalScenePassPS(  true, 1, true); } }
  680. technique postFX_final_1_2_1{   pass P0 {   VertexShader = vs_main_postFX_compiled;     PixelShader = compile PS_2_X FinalScenePassPS(  true, 2, true); } }
  681.                                                                                                                                                  
  682.  
  683. //Recycle Bin:
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top