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.

226 lines
7.7 KiB

  1. //===== Copyright � 1996-2008, Valve Corporation, All rights reserved. ======//
  2. #include "BaseVSShader.h"
  3. #include "convar.h"
  4. #include "splinerope_ps20.inc"
  5. #include "splinerope_ps20b.inc"
  6. #include "splinerope_vs20.inc"
  7. // memdbgon must be the last include file in a .cpp file!!!
  8. #include "tier0/memdbgon.h"
  9. static ConVar rope_min_pixel_diameter( "rope_min_pixel_diameter", "2.0", FCVAR_CHEAT );
  10. #if defined( CSTRIKE15 ) && defined( _X360 )
  11. static ConVar r_shader_srgbread( "r_shader_srgbread", "1", 0, "1 = use shader srgb texture reads, 0 = use HW" );
  12. #else
  13. static ConVar r_shader_srgbread( "r_shader_srgbread", "0", 0, "1 = use shader srgb texture reads, 0 = use HW" );
  14. #endif
  15. BEGIN_VS_SHADER( SplineRope, "Help for SplineRope" )
  16. BEGIN_SHADER_PARAMS
  17. SHADER_PARAM( SHADERSRGBREAD360, SHADER_PARAM_TYPE_BOOL, "0", "Simulate srgb read in shader code")
  18. SHADER_PARAM( SHADOWDEPTH, SHADER_PARAM_TYPE_INTEGER, "0", "writing to a shadow depth buffer" )
  19. SHADER_PARAM( BUMPMAP, SHADER_PARAM_TYPE_TEXTURE, "cable/cablenormalmap", "normal map" )
  20. SHADER_PARAM( TILING, SHADER_PARAM_TYPE_FLOAT, "1", "cable texture tiling multiplier" )
  21. SHADER_PARAM( ALPHATESTREFERENCE, SHADER_PARAM_TYPE_FLOAT, "1", "")
  22. END_SHADER_PARAMS
  23. SHADER_INIT_PARAMS()
  24. {
  25. // srgb read 360
  26. InitIntParam( SHADERSRGBREAD360, params, 0 );
  27. InitIntParam( SHADOWDEPTH, params, 0 );
  28. if ( !params[BUMPMAP]->IsDefined() )
  29. {
  30. params[BUMPMAP]->SetStringValue( "cable/cablenormalmap" );
  31. }
  32. if ( !params[TILING]->IsDefined() )
  33. {
  34. params[TILING]->SetFloatValue( 1.0f );
  35. }
  36. if ( !params[ALPHATESTREFERENCE]->IsDefined() )
  37. {
  38. params[ALPHATESTREFERENCE]->SetFloatValue( 1.0f );
  39. }
  40. SET_FLAGS2( MATERIAL_VAR2_IS_SPRITECARD ); // What's this for?
  41. }
  42. SHADER_FALLBACK
  43. {
  44. return 0;
  45. }
  46. SHADER_INIT
  47. {
  48. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  49. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  50. LoadBumpMap( BUMPMAP );
  51. }
  52. SHADER_DRAW
  53. {
  54. #if defined( CSTRIKE15 )
  55. bool bShaderSrgbRead = IsX360() && r_shader_srgbread.GetBool();
  56. #else
  57. bool bShaderSrgbRead = ( IsX360() && params[SHADERSRGBREAD360]->GetIntValue() );
  58. #endif
  59. bool bShadowDepth = ( params[SHADOWDEPTH]->GetIntValue() != 0 );
  60. bool bUseAlphaTestRef = ( bShadowDepth == false && ( params[ALPHATESTREFERENCE]->GetFloatValue() < 1 ) );
  61. float flAlphaTestRef = params[ALPHATESTREFERENCE]->GetFloatValue();
  62. float flTiling = params[TILING]->GetFloatValue();
  63. SHADOW_STATE
  64. {
  65. // draw back-facing because of yaw spin
  66. pShaderShadow->EnableCulling( false );
  67. if ( bShadowDepth )
  68. {
  69. // don't write color and alpha since we only interested in depth for shadow maps.
  70. pShaderShadow->EnableColorWrites( false );
  71. pShaderShadow->EnableAlphaWrites( false );
  72. // polyoffset for shadow maps.
  73. pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_SHADOW_BIAS );
  74. }
  75. else
  76. {
  77. // We need to write to dest alpha for depth feathering.
  78. pShaderShadow->EnableAlphaWrites( true );
  79. // base texture
  80. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  81. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, !bShaderSrgbRead );
  82. // normal map
  83. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  84. pShaderShadow->EnableSRGBWrite( true );
  85. FogToFogColor();
  86. }
  87. static int s_TexCoordSize[]={ 4, // (worldspace xyz) (radius (diameter?) of spline at this point) for first control point
  88. 4, // (worldspace xyz) (radius of spline at this point) for second control point
  89. 4, // (worldspace xyz) (radius of spline at this point) for third control point
  90. 4, // (worldspace xyz) (radius of spline at this point) for fourth control point
  91. };
  92. unsigned int flags = VERTEX_POSITION | VERTEX_COLOR;
  93. int numTexCoords = 4;
  94. pShaderShadow->VertexShaderVertexFormat( flags, numTexCoords, s_TexCoordSize, 0 );
  95. DECLARE_STATIC_VERTEX_SHADER( splinerope_vs20 );
  96. SET_STATIC_VERTEX_SHADER( splinerope_vs20 );
  97. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  98. {
  99. DECLARE_STATIC_PIXEL_SHADER( splinerope_ps20b );
  100. SET_STATIC_PIXEL_SHADER_COMBO( SHADER_SRGB_READ, bShaderSrgbRead );
  101. SET_STATIC_PIXEL_SHADER_COMBO( SHADOWDEPTH, bShadowDepth );
  102. SET_STATIC_PIXEL_SHADER_COMBO( ALPHATESTREF, bUseAlphaTestRef );
  103. SET_STATIC_PIXEL_SHADER( splinerope_ps20b );
  104. }
  105. else
  106. {
  107. DECLARE_STATIC_PIXEL_SHADER( splinerope_ps20 );
  108. SET_STATIC_PIXEL_SHADER_COMBO( SHADER_SRGB_READ, bShaderSrgbRead );
  109. SET_STATIC_PIXEL_SHADER_COMBO( SHADOWDEPTH, bShadowDepth );
  110. SET_STATIC_PIXEL_SHADER_COMBO( ALPHATESTREF, bUseAlphaTestRef );
  111. SET_STATIC_PIXEL_SHADER( splinerope_ps20 );
  112. }
  113. }
  114. DYNAMIC_STATE
  115. {
  116. // We need these only when screen-orienting, which we are always in this shader.
  117. LoadModelViewMatrixIntoVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0 );
  118. LoadProjectionMatrixIntoVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3 );
  119. // Get viewport and render target dimensions and set shader constant to do a 2D mad
  120. int nViewportX, nViewportY, nViewportWidth, nViewportHeight;
  121. pShaderAPI->GetCurrentViewport( nViewportX, nViewportY, nViewportWidth, nViewportHeight );
  122. float c7[4]={ 0.0f, 0.0f, 0.0f, 0.0f };
  123. if ( !g_pHardwareConfig->IsAAEnabled() )
  124. {
  125. float flMinPixelDiameter = rope_min_pixel_diameter.GetFloat() / ( float )nViewportWidth;
  126. c7[0]= c7[1] = c7[2] = c7[3] = flMinPixelDiameter;
  127. }
  128. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, c7, 1 );
  129. float c8[4]={ flTiling, 0.0f, 0.0f, 0.0f };
  130. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_8, c8, 1 );
  131. // bind base texture
  132. BindTexture( SHADER_SAMPLER0, SRGBReadMask( !bShaderSrgbRead ), BASETEXTURE, FRAME );
  133. // normal map
  134. BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, BUMPMAP );
  135. if ( !bShadowDepth )
  136. {
  137. pShaderAPI->SetPixelShaderFogParams( 0 );
  138. float vEyePos[4];
  139. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos );
  140. vEyePos[3] = flAlphaTestRef;
  141. pShaderAPI->SetPixelShaderConstant( 1, vEyePos, 1 );
  142. }
  143. DECLARE_DYNAMIC_VERTEX_SHADER( splinerope_vs20 );
  144. SET_DYNAMIC_VERTEX_SHADER( splinerope_vs20 );
  145. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  146. {
  147. DECLARE_DYNAMIC_PIXEL_SHADER( splinerope_ps20b );
  148. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, pShaderAPI->ShouldWriteDepthToDestAlpha() );
  149. SET_DYNAMIC_PIXEL_SHADER( splinerope_ps20b );
  150. }
  151. else
  152. {
  153. DECLARE_DYNAMIC_PIXEL_SHADER( splinerope_ps20 );
  154. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, pShaderAPI->ShouldWriteDepthToDestAlpha() );
  155. SET_DYNAMIC_PIXEL_SHADER( splinerope_ps20 );
  156. }
  157. }
  158. Draw( );
  159. }
  160. void ExecuteFastPath( int *dynVSIdx, int *dynPSIdx, IMaterialVar** params, IShaderDynamicAPI * pShaderAPI,
  161. VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContextDataPtr,BOOL bCSMEnabled )
  162. {
  163. // This is only to be used for shadow map gen!!!
  164. s_pShaderAPI = pShaderAPI;
  165. LoadModelViewMatrixIntoVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0 );
  166. LoadProjectionMatrixIntoVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3 );
  167. // Get viewport and render target dimensions and set shader constant to do a 2D mad
  168. int nViewportX, nViewportY, nViewportWidth, nViewportHeight;
  169. pShaderAPI->GetCurrentViewport( nViewportX, nViewportY, nViewportWidth, nViewportHeight );
  170. float c7[4]={ 0.0f, 0.0f, 0.0f, 0.0f };
  171. if ( !g_pHardwareConfig->IsAAEnabled() )
  172. {
  173. float flMinPixelDiameter = rope_min_pixel_diameter.GetFloat() / ( float )nViewportWidth;
  174. c7[0]= c7[1] = c7[2] = c7[3] = flMinPixelDiameter;
  175. }
  176. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, c7, 1 );
  177. pShaderAPI->SetVertexShaderViewProj();
  178. pShaderAPI->SetVertexShaderCameraPos();
  179. *dynVSIdx = 0;
  180. *dynPSIdx = 0;
  181. }
  182. END_SHADER