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.

207 lines
6.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "depthwrite_ps20.inc"
  10. #include "depthwrite_ps20b.inc"
  11. #include "depthwrite_vs20.inc"
  12. #if !defined( _X360 )
  13. #include "depthwrite_ps30.inc"
  14. #include "depthwrite_vs30.inc"
  15. #endif
  16. BEGIN_VS_SHADER_FLAGS( DepthWrite, "Help for Depth Write", SHADER_NOT_EDITABLE )
  17. BEGIN_SHADER_PARAMS
  18. SHADER_PARAM( ALPHATESTREFERENCE, SHADER_PARAM_TYPE_FLOAT, "", "Alpha reference value" )
  19. SHADER_PARAM( COLOR_DEPTH, SHADER_PARAM_TYPE_BOOL, "0", "Write depth as color")
  20. END_SHADER_PARAMS
  21. SHADER_INIT_PARAMS()
  22. {
  23. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  24. }
  25. SHADER_FALLBACK
  26. {
  27. if ( g_pHardwareConfig->GetDXSupportLevel() < 90 )
  28. {
  29. return "Wireframe";
  30. }
  31. return 0;
  32. }
  33. SHADER_INIT
  34. {
  35. }
  36. SHADER_DRAW
  37. {
  38. bool bAlphaClip = IS_FLAG_SET( MATERIAL_VAR_ALPHATEST );
  39. int nColorDepth = GetIntParam( COLOR_DEPTH, params, 0 );
  40. SHADOW_STATE
  41. {
  42. // Set stream format (note that this shader supports compression)
  43. unsigned int flags = VERTEX_POSITION | VERTEX_FORMAT_COMPRESSED;
  44. int nTexCoordCount = 1;
  45. int userDataSize = 0;
  46. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  47. if ( nColorDepth == 0 )
  48. {
  49. // Bias primitives when rendering into shadow map so we get slope-scaled depth bias
  50. // rather than having to apply a constant bias in the filtering shader later
  51. pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_SHADOW_BIAS );
  52. }
  53. // Turn off writes to color buffer since we always sample shadows from the DEPTH texture later
  54. // This gives us double-speed fill when rendering INTO the shadow map
  55. pShaderShadow->EnableColorWrites( ( nColorDepth == 1 ) );
  56. pShaderShadow->EnableAlphaWrites( false );
  57. // Don't backface cull unless alpha clipping, since this can cause artifacts when the
  58. // geometry is clipped by the flashlight near plane
  59. // If a material was already marked nocull, don't cull it
  60. pShaderShadow->EnableCulling( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) && !IS_FLAG_SET(MATERIAL_VAR_NOCULL) );
  61. #ifndef _X360
  62. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  63. #endif
  64. {
  65. DECLARE_STATIC_VERTEX_SHADER( depthwrite_vs20 );
  66. SET_STATIC_VERTEX_SHADER_COMBO( ONLY_PROJECT_POSITION, !bAlphaClip && IsX360() && !nColorDepth ); //360 needs to know if it *shouldn't* output texture coordinates to avoid shader patches
  67. SET_STATIC_VERTEX_SHADER_COMBO( COLOR_DEPTH, nColorDepth );
  68. SET_STATIC_VERTEX_SHADER( depthwrite_vs20 );
  69. if ( bAlphaClip || g_pHardwareConfig->PlatformRequiresNonNullPixelShaders() || nColorDepth )
  70. {
  71. if( bAlphaClip )
  72. {
  73. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  74. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  75. }
  76. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  77. {
  78. DECLARE_STATIC_PIXEL_SHADER( depthwrite_ps20b );
  79. SET_STATIC_PIXEL_SHADER_COMBO( COLOR_DEPTH, nColorDepth );
  80. SET_STATIC_PIXEL_SHADER( depthwrite_ps20b );
  81. }
  82. else
  83. {
  84. DECLARE_STATIC_PIXEL_SHADER( depthwrite_ps20 );
  85. SET_STATIC_PIXEL_SHADER_COMBO( COLOR_DEPTH, nColorDepth );
  86. SET_STATIC_PIXEL_SHADER( depthwrite_ps20 );
  87. }
  88. }
  89. }
  90. #ifndef _X360
  91. else
  92. {
  93. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  94. DECLARE_STATIC_VERTEX_SHADER( depthwrite_vs30 );
  95. SET_STATIC_VERTEX_SHADER_COMBO( ONLY_PROJECT_POSITION, 0 ); //360 only combo, and this is a PC path
  96. SET_STATIC_VERTEX_SHADER_COMBO( COLOR_DEPTH, nColorDepth );
  97. SET_STATIC_VERTEX_SHADER( depthwrite_vs30 );
  98. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  99. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  100. DECLARE_STATIC_PIXEL_SHADER( depthwrite_ps30 );
  101. SET_STATIC_PIXEL_SHADER_COMBO( COLOR_DEPTH, nColorDepth );
  102. SET_STATIC_PIXEL_SHADER( depthwrite_ps30 );
  103. }
  104. #endif
  105. }
  106. DYNAMIC_STATE
  107. {
  108. #ifndef _X360
  109. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  110. #endif
  111. {
  112. depthwrite_vs20_Dynamic_Index vshIndex;
  113. vshIndex.SetSKINNING( pShaderAPI->GetCurrentNumBones() > 0 );
  114. vshIndex.SetCOMPRESSED_VERTS( (int)vertexCompression );
  115. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  116. if ( bAlphaClip )
  117. {
  118. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  119. float vAlphaThreshold[4] = {0.7f, 0.7f, 0.7f, 0.7f};
  120. if ( ALPHATESTREFERENCE != -1 && ( params[ALPHATESTREFERENCE]->GetFloatValue() > 0.0f ) )
  121. {
  122. vAlphaThreshold[0] = vAlphaThreshold[1] = vAlphaThreshold[2] = vAlphaThreshold[3] = params[ALPHATESTREFERENCE]->GetFloatValue();
  123. }
  124. pShaderAPI->SetPixelShaderConstant( 0, vAlphaThreshold, 1 );
  125. }
  126. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  127. {
  128. DECLARE_DYNAMIC_PIXEL_SHADER( depthwrite_ps20b );
  129. SET_DYNAMIC_PIXEL_SHADER_COMBO( ALPHACLIP, bAlphaClip );
  130. SET_DYNAMIC_PIXEL_SHADER( depthwrite_ps20b );
  131. }
  132. else
  133. {
  134. DECLARE_DYNAMIC_PIXEL_SHADER( depthwrite_ps20 );
  135. SET_DYNAMIC_PIXEL_SHADER_COMBO( ALPHACLIP, bAlphaClip );
  136. SET_DYNAMIC_PIXEL_SHADER( depthwrite_ps20 );
  137. }
  138. }
  139. #ifndef _X360
  140. else // 3.0 shader case (PC only)
  141. {
  142. SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, SHADER_VERTEXTEXTURE_SAMPLER0 );
  143. depthwrite_vs30_Dynamic_Index vshIndex;
  144. vshIndex.SetSKINNING( pShaderAPI->GetCurrentNumBones() > 0 );
  145. vshIndex.SetMORPHING( pShaderAPI->IsHWMorphingEnabled() );
  146. vshIndex.SetCOMPRESSED_VERTS( (int)vertexCompression );
  147. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  148. if ( bAlphaClip )
  149. {
  150. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  151. float vAlphaThreshold[4] = {0.7f, 0.7f, 0.7f, 0.7f};
  152. if ( ALPHATESTREFERENCE != -1 && ( params[ALPHATESTREFERENCE]->GetFloatValue() > 0.0f ) )
  153. {
  154. vAlphaThreshold[0] = vAlphaThreshold[1] = vAlphaThreshold[2] = vAlphaThreshold[3] = params[ALPHATESTREFERENCE]->GetFloatValue();
  155. }
  156. pShaderAPI->SetPixelShaderConstant( 0, vAlphaThreshold, 1 );
  157. }
  158. DECLARE_DYNAMIC_PIXEL_SHADER( depthwrite_ps30 );
  159. SET_DYNAMIC_PIXEL_SHADER_COMBO( ALPHACLIP, bAlphaClip );
  160. SET_DYNAMIC_PIXEL_SHADER( depthwrite_ps30 );
  161. }
  162. #endif
  163. Vector4D vParms;
  164. // set up arbitrary far planes, as the real ones are too far ( 30,000 )
  165. // pShaderAPI->SetPSNearAndFarZ( 1 );
  166. vParms.x = 7.0f; // arbitrary near
  167. vParms.y = 4000.0f; // arbitrary far
  168. vParms.z = 0.0f;
  169. vParms.w = 0.0f;
  170. pShaderAPI->SetPixelShaderConstant( 1, vParms.Base(), 2 );
  171. } // DYNAMIC_STATE
  172. Draw( );
  173. }
  174. END_SHADER