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.

145 lines
5.2 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "BaseVSShader.h"
  8. #include "motion_blur_vs20.inc"
  9. #include "motion_blur_ps20.inc"
  10. #include "motion_blur_ps20b.inc"
  11. #include "convar.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. ConVar mat_motion_blur_percent_of_screen_max( "mat_motion_blur_percent_of_screen_max", "4.0" );
  15. DEFINE_FALLBACK_SHADER( MotionBlur, MotionBlur_dx9 )
  16. BEGIN_VS_SHADER_FLAGS( MotionBlur_dx9, "Motion Blur", SHADER_NOT_EDITABLE )
  17. BEGIN_SHADER_PARAMS
  18. SHADER_PARAM( MOTIONBLURINTERNAL, SHADER_PARAM_TYPE_VEC4, "[0 0 0 0]", "Internal motion blur value set by proxy" )
  19. SHADER_PARAM( MOTIONBLURVIEWPORTINTERNAL, SHADER_PARAM_TYPE_VEC4, "[0 0 0 0]", "Internal motion blur value set by proxy" )
  20. END_SHADER_PARAMS
  21. SHADER_INIT_PARAMS()
  22. {
  23. }
  24. SHADER_FALLBACK
  25. {
  26. return 0;
  27. }
  28. SHADER_INIT
  29. {
  30. if ( params[BASETEXTURE]->IsDefined() )
  31. {
  32. LoadTexture( BASETEXTURE, IsOSX() && g_pHardwareConfig->CanDoSRGBReadFromRTs() ? TEXTUREFLAGS_SRGB : 0 );
  33. }
  34. }
  35. SHADER_DRAW
  36. {
  37. bool bForceSRGBReadsAndWrites = IsOSXOpenGL() && g_pHardwareConfig->CanDoSRGBReadFromRTs();
  38. SHADOW_STATE
  39. {
  40. pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION, 1, 0, 0 );
  41. // On OSX OpenGL, we must do sRGB reads and writes since these render targets are tagged as such
  42. bool bForceSRGBReadsAndWrites = IsOSX() && g_pHardwareConfig->CanDoSRGBReadFromRTs();
  43. // NOTE: sRGB is disabled because of the NV8800 brokenness
  44. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  45. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, bForceSRGBReadsAndWrites );
  46. pShaderShadow->EnableSRGBWrite( bForceSRGBReadsAndWrites );
  47. DECLARE_STATIC_VERTEX_SHADER( motion_blur_vs20 );
  48. SET_STATIC_VERTEX_SHADER( motion_blur_vs20 );
  49. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  50. {
  51. DECLARE_STATIC_PIXEL_SHADER( motion_blur_ps20b );
  52. SET_STATIC_PIXEL_SHADER( motion_blur_ps20b );
  53. }
  54. else
  55. {
  56. DECLARE_STATIC_PIXEL_SHADER( motion_blur_ps20 );
  57. SET_STATIC_PIXEL_SHADER( motion_blur_ps20 );
  58. }
  59. pShaderShadow->EnableDepthWrites( false );
  60. pShaderShadow->EnableAlphaWrites( false );
  61. }
  62. DYNAMIC_STATE
  63. {
  64. DECLARE_DYNAMIC_VERTEX_SHADER( motion_blur_vs20 );
  65. SET_DYNAMIC_VERTEX_SHADER( motion_blur_vs20 );
  66. // Bind textures
  67. BindTexture( SHADER_SAMPLER0, bForceSRGBReadsAndWrites ? TEXTURE_BINDFLAGS_SRGBREAD : TEXTURE_BINDFLAGS_NONE, BASETEXTURE );
  68. // Get texture dimensions
  69. ITexture *src_texture = params[BASETEXTURE]->GetTextureValue();
  70. //int flTextureWidth = src_texture->GetActualWidth();
  71. int flTextureHeight = src_texture->GetActualHeight();
  72. // Percent of screen clamp
  73. float vConst[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  74. vConst[0] = mat_motion_blur_percent_of_screen_max.GetFloat() / 100.0f;
  75. const float *pBlurViewportInternal = params[MOTIONBLURVIEWPORTINTERNAL]->GetVecValue();
  76. if ( ( pBlurViewportInternal[0] > 0.0f ) || ( pBlurViewportInternal[1] > 0.0f ) || ( pBlurViewportInternal[2] < 1.0f ) || ( pBlurViewportInternal[3] < 1.0f ) )
  77. {
  78. vConst[0] *= 0.25f; // Reduce the max if we're not rendering full screen (hack to dumb down motion blur in split screen)
  79. }
  80. pShaderAPI->SetPixelShaderConstant( 0, vConst, 1 );
  81. // Set values from material proxy
  82. pShaderAPI->SetPixelShaderConstant( 1, params[MOTIONBLURINTERNAL]->GetVecValue(), 1 );
  83. pShaderAPI->SetPixelShaderConstant( 2, params[MOTIONBLURVIEWPORTINTERNAL]->GetVecValue(), 1 );
  84. float flApproximateBlurLength = fabs( params[MOTIONBLURINTERNAL]->GetVecValue()[0] ) + fabs( params[MOTIONBLURINTERNAL]->GetVecValue()[1] ) +
  85. fabs( params[MOTIONBLURINTERNAL]->GetVecValue()[2] ) + fabs( params[MOTIONBLURINTERNAL]->GetVecValue()[3] );
  86. // Quality based on screen resolution height
  87. int nNumBlurSamples = 6;
  88. if ( flTextureHeight >= 1080 ) // 1080p and higher
  89. nNumBlurSamples = 14;
  90. else if ( flTextureHeight >= 720 ) // 720p to 1080p
  91. nNumBlurSamples = 10;
  92. else // Lower resolution than 720p
  93. nNumBlurSamples = 6;
  94. // If we are blurring less than half the allowed blur max, use fewer samples
  95. float flPercentMaxBlur = clamp( 2.0f * flApproximateBlurLength / MIN( mat_motion_blur_percent_of_screen_max.GetFloat() / 100.0f, 4.0f ), 0.0f, 1.0f );
  96. nNumBlurSamples = ( int )( ( float )nNumBlurSamples * flPercentMaxBlur );
  97. if ( nNumBlurSamples < 1 )
  98. nNumBlurSamples = 1;
  99. if ( flApproximateBlurLength == 0.0f )
  100. {
  101. // No motion blur this frame, so force 0 blur samples. This will cause the shader to do a single unblurred fetch.
  102. nNumBlurSamples = 0;
  103. }
  104. nNumBlurSamples = clamp( nNumBlurSamples, 0, 14 );
  105. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  106. {
  107. DECLARE_DYNAMIC_PIXEL_SHADER( motion_blur_ps20b );
  108. SET_DYNAMIC_PIXEL_SHADER_COMBO( D_NUM_BLUR_SAMPLES, nNumBlurSamples );
  109. SET_DYNAMIC_PIXEL_SHADER( motion_blur_ps20b );
  110. }
  111. else
  112. {
  113. DECLARE_DYNAMIC_PIXEL_SHADER( motion_blur_ps20 );
  114. SET_DYNAMIC_PIXEL_SHADER_COMBO( D_NUM_BLUR_SAMPLES, nNumBlurSamples );
  115. SET_DYNAMIC_PIXEL_SHADER( motion_blur_ps20 );
  116. }
  117. }
  118. Draw();
  119. }
  120. END_SHADER