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.

203 lines
6.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include <string.h>
  10. #include "const.h"
  11. #include "cpp_shader_constant_register_map.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. #include "cs_grass_vs20.inc"
  15. #include "cs_grass_ps20.inc"
  16. #include "cs_grass_ps20b.inc"
  17. #include "cs_grass_ps30.inc"
  18. ConVar cl_detail_scale( "cl_detail_scale", "2", FCVAR_CHEAT, "" );
  19. BEGIN_VS_SHADER( Grass, "Help for Grass" )
  20. BEGIN_SHADER_PARAMS
  21. //SHADER_PARAM( DUMMYPARAM, SHADER_PARAM_TYPE_VEC3, "[0 0 0]", "" )
  22. END_SHADER_PARAMS
  23. SHADER_FALLBACK
  24. {
  25. return 0;
  26. }
  27. SHADER_INIT_PARAMS()
  28. {
  29. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  30. SET_FLAGS( MATERIAL_VAR_VERTEXCOLOR );
  31. SET_FLAGS( MATERIAL_VAR_VERTEXALPHA );
  32. }
  33. SHADER_INIT
  34. {
  35. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  36. }
  37. #define SHADER_USE_VERTEX_COLOR 1
  38. #define SHADER_USE_CONSTANT_COLOR 2
  39. void SetCSGrassCommonShadowState( unsigned int shaderFlags, IMaterialVar **params )
  40. {
  41. IShaderShadow *pShaderShadow = s_pShaderShadow;
  42. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  43. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  44. bool bCSMEnabled;
  45. int nCSMQualityComboValue = 0;
  46. bool bSFM = ( ToolsEnabled() && IsPlatformWindowsPC() && g_pHardwareConfig->SupportsPixelShaders_3_0() ) ? true : false;
  47. bCSMEnabled = g_pHardwareConfig->SupportsCascadedShadowMapping() && !bSFM;
  48. if ( bCSMEnabled )
  49. {
  50. nCSMQualityComboValue = g_pHardwareConfig->GetCSMShaderMode( materials->GetCurrentConfigForVideoCard().GetCSMQualityMode() );
  51. }
  52. unsigned int flags = VERTEX_POSITION;
  53. //if( shaderFlags & SHADER_USE_VERTEX_COLOR )
  54. {
  55. flags |= VERTEX_COLOR;
  56. }
  57. int numTexCoords = 1;
  58. static int s_TexCoordDims[] = { 4 };
  59. pShaderShadow->VertexShaderVertexFormat( flags, numTexCoords, s_TexCoordDims, 0 );
  60. pShaderShadow->EnableAlphaTest( true );
  61. //pShaderShadow->EnableAlphaWrites( false );
  62. pShaderShadow->EnableDepthWrites( true );
  63. pShaderShadow->EnableCulling( false ); // grass quad winding order can go either way
  64. pShaderShadow->EnableSRGBWrite( true );
  65. DefaultFog();
  66. DECLARE_STATIC_VERTEX_SHADER( cs_grass_vs20 );
  67. SET_STATIC_VERTEX_SHADER( cs_grass_vs20 );
  68. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  69. {
  70. DECLARE_STATIC_PIXEL_SHADER( cs_grass_ps30 );
  71. SET_STATIC_PIXEL_SHADER_COMBO( CASCADED_SHADOW_MAPPING, bCSMEnabled );
  72. SET_STATIC_PIXEL_SHADER_COMBO( CSM_MODE, nCSMQualityComboValue);
  73. SET_STATIC_PIXEL_SHADER( cs_grass_ps30 );
  74. }
  75. else if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  76. {
  77. DECLARE_STATIC_PIXEL_SHADER( cs_grass_ps20b );
  78. SET_STATIC_PIXEL_SHADER( cs_grass_ps20b );
  79. }
  80. else
  81. {
  82. DECLARE_STATIC_PIXEL_SHADER( cs_grass_ps20 );
  83. SET_STATIC_PIXEL_SHADER( cs_grass_ps20 );
  84. }
  85. }
  86. void SetCSGrassCommonDynamicState( unsigned int shaderFlags )
  87. {
  88. IShaderDynamicAPI *pShaderAPI = s_pShaderAPI;
  89. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, FRAME );
  90. //LoadModelViewMatrixIntoVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0 );
  91. //LoadProjectionMatrixIntoVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3 );
  92. float flTime = pShaderAPI->CurrentTime();//sin( pShaderAPI->CurrentTime() ) + sin( pShaderAPI->CurrentTime() * 2 );
  93. float flConst[4]={ flTime, cl_detail_scale.GetFloat(), 0, 0 };
  94. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, flConst, 4 );
  95. static ConVarRef sv_dangerzone_enabled( "sv_dangerzone_enabled" );
  96. static ConVarRef sv_dangerzone_radius( "sv_dangerzone_radius" );
  97. static ConVarRef sv_dangerzone_x( "sv_dangerzone_x" );
  98. static ConVarRef sv_dangerzone_y( "sv_dangerzone_y" );
  99. static ConVarRef sv_dangerzone_z( "sv_dangerzone_z" );
  100. bool bCullGrassOusideDangerRadius = false;
  101. if ( sv_dangerzone_enabled.GetBool() )
  102. {
  103. bCullGrassOusideDangerRadius = true;
  104. flConst[0] = sv_dangerzone_x.GetFloat();
  105. flConst[1] = sv_dangerzone_y.GetFloat();
  106. flConst[2] = sv_dangerzone_z.GetFloat();
  107. flConst[3] = sv_dangerzone_radius.GetFloat();
  108. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, flConst, 4 );
  109. }
  110. DECLARE_DYNAMIC_VERTEX_SHADER( cs_grass_vs20 );
  111. SET_DYNAMIC_VERTEX_SHADER_COMBO( DZONE, bCullGrassOusideDangerRadius );
  112. SET_DYNAMIC_VERTEX_SHADER( cs_grass_vs20 );
  113. bool bCSMEnabled;
  114. bool bSFM = ( ToolsEnabled() && IsPlatformWindowsPC() && g_pHardwareConfig->SupportsPixelShaders_3_0() ) ? true : false;
  115. bCSMEnabled = g_pHardwareConfig->SupportsCascadedShadowMapping() && pShaderAPI->IsCascadedShadowMapping() && !bSFM;
  116. if ( bCSMEnabled )
  117. {
  118. ITexture *pDepthTextureAtlas = NULL;
  119. const CascadedShadowMappingState_t &cascadeState = pShaderAPI->GetCascadedShadowMappingState( &pDepthTextureAtlas );
  120. if ( pDepthTextureAtlas )
  121. {
  122. BindTexture( SHADER_SAMPLER15, TEXTURE_BINDFLAGS_SHADOWDEPTH, pDepthTextureAtlas, 0 );
  123. pShaderAPI->SetPixelShaderConstant( 64, &cascadeState.m_vLightColor.x, CASCADED_SHADOW_MAPPING_CONSTANT_BUFFER_SIZE );
  124. }
  125. }
  126. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  127. {
  128. DECLARE_DYNAMIC_PIXEL_SHADER( cs_grass_ps30 );
  129. SET_DYNAMIC_PIXEL_SHADER_COMBO( DYN_CSM_ENABLED, bCSMEnabled ? 1 : 0 );
  130. SET_DYNAMIC_PIXEL_SHADER( cs_grass_ps30 );
  131. }
  132. else if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  133. {
  134. DECLARE_DYNAMIC_PIXEL_SHADER( cs_grass_ps20b );
  135. SET_DYNAMIC_PIXEL_SHADER( cs_grass_ps20b );
  136. }
  137. else
  138. {
  139. DECLARE_DYNAMIC_PIXEL_SHADER( cs_grass_ps20 );
  140. SET_DYNAMIC_PIXEL_SHADER( cs_grass_ps20 );
  141. }
  142. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  143. float vEyePos_SpecExponent[4];
  144. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  145. vEyePos_SpecExponent[3] = 0.0f;
  146. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  147. }
  148. SHADER_DRAW
  149. {
  150. SHADOW_STATE
  151. {
  152. SetCSGrassCommonShadowState( 0, params );
  153. }
  154. DYNAMIC_STATE
  155. {
  156. SetCSGrassCommonDynamicState( 0 );
  157. }
  158. Draw();
  159. }
  160. END_SHADER