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.

232 lines
8.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "BaseVSShader.h"
  9. #include "cpp_shader_constant_register_map.h"
  10. #include "VertexLit_and_unlit_Generic_vs20.inc"
  11. #include "decalmodulate_ps20.inc"
  12. #include "decalmodulate_ps20b.inc"
  13. #ifndef _X360
  14. #include "vertexlit_and_unlit_generic_vs30.inc"
  15. #include "decalmodulate_ps30.inc"
  16. #endif
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. DEFINE_FALLBACK_SHADER( DecalModulate, DecalModulate_DX9 )
  20. extern ConVar r_flashlight_version2;
  21. BEGIN_VS_SHADER( DecalModulate_dx9,
  22. "Help for DecalModulate_dx9" )
  23. BEGIN_SHADER_PARAMS
  24. END_SHADER_PARAMS
  25. SHADER_FALLBACK
  26. {
  27. if (g_pHardwareConfig->GetDXSupportLevel() < 90)
  28. return "DecalModulate_DX6";
  29. return 0;
  30. }
  31. SHADER_INIT_PARAMS()
  32. {
  33. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  34. #ifndef _X360
  35. if ( g_pHardwareConfig->HasFastVertexTextures() )
  36. {
  37. // The vertex shader uses the vertex id stream
  38. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  39. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  40. }
  41. #endif
  42. }
  43. SHADER_INIT
  44. {
  45. LoadTexture( BASETEXTURE );
  46. }
  47. SHADER_DRAW
  48. {
  49. SHADOW_STATE
  50. {
  51. pShaderShadow->EnableAlphaTest( true );
  52. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GREATER, 0.0f );
  53. pShaderShadow->EnableDepthWrites( false );
  54. pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DECAL );
  55. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  56. // Be sure not to write to dest alpha
  57. pShaderShadow->EnableAlphaWrites( false );
  58. // SRGB conversions hose the blend on some hardware, so keep we everything in gamma space
  59. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, false );
  60. pShaderShadow->EnableSRGBWrite( false );
  61. pShaderShadow->EnableBlending( true );
  62. pShaderShadow->BlendFunc( SHADER_BLEND_DST_COLOR, SHADER_BLEND_SRC_COLOR );
  63. pShaderShadow->DisableFogGammaCorrection( true ); //fog should stay exactly middle grey
  64. FogToGrey();
  65. #ifndef _X360
  66. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  67. #endif
  68. {
  69. bool bUseStaticControlFlow = g_pHardwareConfig->SupportsStaticControlFlow();
  70. DECLARE_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs20 );
  71. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, false );
  72. SET_STATIC_VERTEX_SHADER_COMBO( CUBEMAP, false );
  73. SET_STATIC_VERTEX_SHADER_COMBO( HALFLAMBERT, false );
  74. SET_STATIC_VERTEX_SHADER_COMBO( FLASHLIGHT, false );
  75. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS_BASE, false );
  76. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS_DETAIL, false );
  77. SET_STATIC_VERTEX_SHADER_COMBO( SEPARATE_DETAIL_UVS, false );
  78. SET_STATIC_VERTEX_SHADER_COMBO( USE_STATIC_CONTROL_FLOW, bUseStaticControlFlow );
  79. SET_STATIC_VERTEX_SHADER_COMBO( DONT_GAMMA_CONVERT_VERTEX_COLOR, 0 );
  80. SET_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs20 );
  81. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  82. {
  83. DECLARE_STATIC_PIXEL_SHADER( decalmodulate_ps20b );
  84. SET_STATIC_PIXEL_SHADER( decalmodulate_ps20b );
  85. }
  86. else
  87. {
  88. DECLARE_STATIC_PIXEL_SHADER( decalmodulate_ps20 );
  89. SET_STATIC_PIXEL_SHADER( decalmodulate_ps20 );
  90. }
  91. }
  92. #ifndef _X360
  93. else
  94. {
  95. DECLARE_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs30 );
  96. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, false );
  97. SET_STATIC_VERTEX_SHADER_COMBO( CUBEMAP, false );
  98. SET_STATIC_VERTEX_SHADER_COMBO( HALFLAMBERT, false );
  99. SET_STATIC_VERTEX_SHADER_COMBO( FLASHLIGHT, false );
  100. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS_BASE, false );
  101. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS_DETAIL, false );
  102. SET_STATIC_VERTEX_SHADER_COMBO( SEPARATE_DETAIL_UVS, false );
  103. SET_STATIC_VERTEX_SHADER_COMBO( DECAL, true );
  104. SET_STATIC_VERTEX_SHADER_COMBO( DONT_GAMMA_CONVERT_VERTEX_COLOR, 0 );
  105. SET_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs30 );
  106. DECLARE_STATIC_PIXEL_SHADER( decalmodulate_ps30 );
  107. SET_STATIC_PIXEL_SHADER( decalmodulate_ps30 );
  108. }
  109. #endif
  110. // Set stream format (note that this shader supports compression)
  111. unsigned int flags = VERTEX_POSITION | VERTEX_FORMAT_COMPRESSED;
  112. #ifndef _X360
  113. // The VS30 shader offsets decals along the normal (for morphed geom)
  114. flags |= g_pHardwareConfig->HasFastVertexTextures() ? VERTEX_NORMAL : 0;
  115. #endif
  116. int pTexCoordDim[3] = { 2, 0, 3 };
  117. int nTexCoordCount = 1;
  118. int userDataSize = 0;
  119. #ifndef _X360
  120. if ( g_pHardwareConfig->HasFastVertexTextures() )
  121. {
  122. nTexCoordCount = 3;
  123. }
  124. #endif
  125. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, pTexCoordDim, userDataSize );
  126. }
  127. DYNAMIC_STATE
  128. {
  129. if ( pShaderAPI->InFlashlightMode() && ( !IsX360() && ( r_flashlight_version2.GetInt() == 0 ) ) )
  130. {
  131. // Don't draw anything for the flashlight pass
  132. Draw( false );
  133. return;
  134. }
  135. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  136. // Set an identity base texture transformation
  137. Vector4D transformation[2];
  138. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  139. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  140. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, transformation[0].Base(), 2 );
  141. MaterialFogMode_t fogType = s_pShaderAPI->GetSceneFogMode();
  142. int fogIndex = ( fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ) ? 1 : 0;
  143. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  144. float vEyePos_SpecExponent[4];
  145. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  146. vEyePos_SpecExponent[3] = 0.0f;
  147. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  148. #ifndef _X360
  149. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  150. #endif
  151. {
  152. DECLARE_DYNAMIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs20 );
  153. SET_DYNAMIC_VERTEX_SHADER_COMBO( DYNAMIC_LIGHT, 0 ); // Use simplest possible vertex lighting, since ps is so simple
  154. SET_DYNAMIC_VERTEX_SHADER_COMBO( STATIC_LIGHT_VERTEX, 0 ); //
  155. SET_DYNAMIC_VERTEX_SHADER_COMBO( STATIC_LIGHT_LIGHTMAP, 0); //
  156. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  157. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, 0 );
  158. SET_DYNAMIC_VERTEX_SHADER_COMBO( LIGHTING_PREVIEW, 0 );
  159. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  160. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, 0 );
  161. SET_DYNAMIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs20 );
  162. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  163. {
  164. DECLARE_DYNAMIC_PIXEL_SHADER( decalmodulate_ps20b );
  165. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  166. SET_DYNAMIC_PIXEL_SHADER( decalmodulate_ps20b );
  167. }
  168. else
  169. {
  170. DECLARE_DYNAMIC_PIXEL_SHADER( decalmodulate_ps20 );
  171. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  172. SET_DYNAMIC_PIXEL_SHADER( decalmodulate_ps20 );
  173. }
  174. }
  175. #ifndef _X360
  176. else
  177. {
  178. SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, SHADER_VERTEXTEXTURE_SAMPLER0 );
  179. DECLARE_DYNAMIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs30 );
  180. SET_DYNAMIC_VERTEX_SHADER_COMBO( DYNAMIC_LIGHT, 0 ); // Use simplest possible vertex lighting, since ps is so simple
  181. SET_DYNAMIC_VERTEX_SHADER_COMBO( STATIC_LIGHT_VERTEX, 0 ); //
  182. SET_DYNAMIC_VERTEX_SHADER_COMBO( STATIC_LIGHT_LIGHTMAP, 0); //
  183. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  184. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  185. SET_DYNAMIC_VERTEX_SHADER_COMBO( LIGHTING_PREVIEW, 0 );
  186. SET_DYNAMIC_VERTEX_SHADER_COMBO( MORPHING, pShaderAPI->IsHWMorphingEnabled() );
  187. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  188. SET_DYNAMIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs30 );
  189. DECLARE_DYNAMIC_PIXEL_SHADER( decalmodulate_ps30 );
  190. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  191. SET_DYNAMIC_PIXEL_SHADER( decalmodulate_ps30 );
  192. bool bUnusedTexCoords[3] = { false, false, !pShaderAPI->IsHWMorphingEnabled() };
  193. pShaderAPI->MarkUnusedVertexFields( 0, 3, bUnusedTexCoords );
  194. }
  195. #endif
  196. }
  197. Draw( );
  198. }
  199. END_SHADER