Team Fortress 2 Source Code as on 22/4/2020
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.

132 lines
3.9 KiB

  1. //========= Copyright 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. END_SHADER_PARAMS
  20. SHADER_INIT_PARAMS()
  21. {
  22. }
  23. SHADER_FALLBACK
  24. {
  25. if ( g_pHardwareConfig->GetDXSupportLevel() < 90 )
  26. {
  27. return "Wireframe";
  28. }
  29. return 0;
  30. }
  31. SHADER_INIT
  32. {
  33. if ( params[BASETEXTURE]->IsDefined() )
  34. {
  35. LoadTexture( BASETEXTURE, IsOSX() && g_pHardwareConfig->CanDoSRGBReadFromRTs() ? TEXTUREFLAGS_SRGB : 0 );
  36. }
  37. }
  38. SHADER_DRAW
  39. {
  40. SHADOW_STATE
  41. {
  42. pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION, 1, 0, 0 );
  43. // On OpenGL OSX, we must do sRGB reads and writes since these render targets are tagged as such
  44. bool bForceSRGBReadsAndWrites = IsOSX() && g_pHardwareConfig->CanDoSRGBReadFromRTs();
  45. // NOTE: sRGB is disabled because of the NV8800 brokenness
  46. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  47. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, bForceSRGBReadsAndWrites );
  48. pShaderShadow->EnableSRGBWrite( bForceSRGBReadsAndWrites );
  49. DECLARE_STATIC_VERTEX_SHADER( motion_blur_vs20 );
  50. SET_STATIC_VERTEX_SHADER( motion_blur_vs20 );
  51. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  52. {
  53. DECLARE_STATIC_PIXEL_SHADER( motion_blur_ps20b );
  54. SET_STATIC_PIXEL_SHADER( motion_blur_ps20b );
  55. }
  56. else
  57. {
  58. DECLARE_STATIC_PIXEL_SHADER( motion_blur_ps20 );
  59. SET_STATIC_PIXEL_SHADER( motion_blur_ps20 );
  60. }
  61. pShaderShadow->EnableDepthWrites( false );
  62. pShaderShadow->EnableAlphaWrites( false );
  63. }
  64. DYNAMIC_STATE
  65. {
  66. DECLARE_DYNAMIC_VERTEX_SHADER( motion_blur_vs20 );
  67. SET_DYNAMIC_VERTEX_SHADER( motion_blur_vs20 );
  68. // Bind textures
  69. BindTexture( SHADER_SAMPLER0, BASETEXTURE );
  70. // Get texture dimensions
  71. ITexture *src_texture = params[BASETEXTURE]->GetTextureValue();
  72. //int flTextureWidth = src_texture->GetActualWidth();
  73. int flTextureHeight = src_texture->GetActualHeight();
  74. // Percent of screen clamp
  75. float vConst[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  76. vConst[0] = mat_motion_blur_percent_of_screen_max.GetFloat() / 100.0f;
  77. pShaderAPI->SetPixelShaderConstant( 0, vConst, 1 );
  78. // Set values from material proxy
  79. pShaderAPI->SetPixelShaderConstant( 1, params[MOTIONBLURINTERNAL]->GetVecValue(), 1 );
  80. // Quality based on screen resolution height
  81. int nQuality = 1;
  82. if ( flTextureHeight >= 1080 ) // 1080p and higher
  83. nQuality = 3;
  84. else if ( flTextureHeight >= 720 ) // 720p to 1080p
  85. nQuality = 2;
  86. else // Lower resolution than 720p
  87. nQuality = 1;
  88. if ( fabs( params[MOTIONBLURINTERNAL]->GetVecValue()[0] ) + fabs( params[MOTIONBLURINTERNAL]->GetVecValue()[1] ) +
  89. fabs( params[MOTIONBLURINTERNAL]->GetVecValue()[2] ) + fabs( params[MOTIONBLURINTERNAL]->GetVecValue()[3] ) == 0.0f )
  90. {
  91. // No motion blur this frame, so force quality to 0
  92. nQuality = 0;
  93. }
  94. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  95. {
  96. DECLARE_DYNAMIC_PIXEL_SHADER( motion_blur_ps20b );
  97. SET_DYNAMIC_PIXEL_SHADER_COMBO( QUALITY, nQuality );
  98. SET_DYNAMIC_PIXEL_SHADER( motion_blur_ps20b );
  99. }
  100. else
  101. {
  102. DECLARE_DYNAMIC_PIXEL_SHADER( motion_blur_ps20 );
  103. SET_DYNAMIC_PIXEL_SHADER_COMBO( QUALITY, nQuality );
  104. SET_DYNAMIC_PIXEL_SHADER( motion_blur_ps20 );
  105. }
  106. }
  107. Draw();
  108. }
  109. END_SHADER