Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.3 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // DYNAMIC: "D_NUM_BLUR_SAMPLES" "0..14"
  3. #ifdef HDRTYPE
  4. #undef HDRTYPE
  5. #endif
  6. #define HDRTYPE HDR_TYPE_NONE
  7. // Includes =======================================================================================
  8. #include "common_ps_fxc.h"
  9. // Texture Samplers ===============================================================================
  10. sampler g_tTexSampler : register( s0 );
  11. // Shaders Constants and Globals ==================================================================
  12. float g_flMaxMotionBlur : register( c0 );
  13. float4 g_vConst5 : register( c1 );
  14. #define g_vGlobalBlurVector g_vConst5.xy
  15. #define g_flFallingMotionIntensity g_vConst5.z
  16. #define g_flRollBlurIntensity g_vConst5.w
  17. float4 g_vViewport : register( c2 );
  18. // Interpolated values ============================================================================
  19. struct PS_INPUT
  20. {
  21. float2 vUv0 : TEXCOORD0;
  22. };
  23. // Main ===========================================================================================
  24. float4 main( PS_INPUT i ) : COLOR
  25. {
  26. // Calculate blur vector
  27. float2 vFallingMotionBlurVector = ( ( i.vUv0.xy * 2.0f ) - 1.0f );
  28. float2 vRollBlurVector = cross( float3( vFallingMotionBlurVector.xy, 0.0f ), float3( 0.0f, 0.0f, 1.0f ) ).xy;
  29. float2 vGlobalBlurVector = g_vGlobalBlurVector;
  30. vGlobalBlurVector.y = -vGlobalBlurVector.y;
  31. //vGlobalBlurVector.xy = float2( 1.0f, 0.0f ); // For debugging
  32. float flFallingMotionBlurIntensity = -abs( g_flFallingMotionIntensity ); // Keep samples on screen by keeping vector pointing in
  33. //flFallingMotionBlurIntensity = step( 10, abs(g_flFallingMotionIntensity) ); // For finding the sweet spot in debug mode
  34. vFallingMotionBlurVector.xy *= dot( vFallingMotionBlurVector.xy, vFallingMotionBlurVector.xy ); // Dampen the effect in the middle of the screen
  35. vFallingMotionBlurVector.xy *= flFallingMotionBlurIntensity;
  36. float flRollBlurIntensity = g_flRollBlurIntensity;
  37. vRollBlurVector.xy *= flRollBlurIntensity;
  38. float2 vFinalBlurVector = vGlobalBlurVector.xy + vFallingMotionBlurVector.xy + vRollBlurVector.xy;
  39. #if defined( _X360 ) // Disable falling blur on the 360 for perf
  40. vFinalBlurVector.xy = vGlobalBlurVector.xy + vRollBlurVector.xy;
  41. #endif
  42. // Clamp blur vector to max length
  43. if ( length( vFinalBlurVector.xy ) > g_flMaxMotionBlur )
  44. {
  45. vFinalBlurVector.xy = normalize( vFinalBlurVector.xy ) * g_flMaxMotionBlur;
  46. }
  47. // Clamp fetches to viewport
  48. float2 vBlurVectorClamped = i.vUv0.xy + vFinalBlurVector.xy;
  49. vBlurVectorClamped.xy = clamp( vBlurVectorClamped.xy, g_vViewport.xy, g_vViewport.wz ) - i.vUv0.xy;
  50. // Set number of samples
  51. int nNumSamples = D_NUM_BLUR_SAMPLES + 1; // Make sure we always fetch 1 texel!
  52. // Step size
  53. float2 vUvOffset = { 0.0f, 0.0f };
  54. #if ( D_NUM_BLUR_SAMPLES > 0 )
  55. vUvOffset.xy = vBlurVectorClamped.xy / ( nNumSamples - 1 );
  56. #endif
  57. float4 cColor = { 0.0f, 0.0f, 0.0f, 0.0f };
  58. #if defined( _X360 )
  59. [unroll]
  60. #endif
  61. for ( int x = 0; x <= D_NUM_BLUR_SAMPLES; x++ )
  62. {
  63. // Calculate uv
  64. float2 vUvTmp = i.vUv0.xy + ( vUvOffset.xy * x );
  65. // Sample pixel
  66. //cColor += kernel[x] * tex2D( g_tTexSampler, vUvTmp ); // Use kernal from above
  67. cColor += ( 1.0f / nNumSamples ) * tex2D( g_tTexSampler, vUvTmp.xy ); // Evenly weight all samples
  68. }
  69. return float4( cColor.rgb, 1.0f );
  70. }