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.

399 lines
15 KiB

  1. //========= Copyright � 1996-2007, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "convar.h"
  10. #include "depthwrite_ps20.inc"
  11. #include "depthwrite_ps20b.inc"
  12. #include "depthwrite_vs20.inc"
  13. #if !defined( _X360 ) &&! defined( _PS3 )
  14. #include "depthwrite_ps30.inc"
  15. #include "depthwrite_vs30.inc"
  16. #endif
  17. // NOTE: This has to be the last file included!
  18. #include "tier0/memdbgon.h"
  19. static ConVar mat_displacementmap( "mat_displacementmap", "1", FCVAR_CHEAT );
  20. BEGIN_VS_SHADER_FLAGS( DepthWrite, "Help for Depth Write", SHADER_NOT_EDITABLE )
  21. BEGIN_SHADER_PARAMS
  22. SHADER_PARAM( ALPHATESTREFERENCE, SHADER_PARAM_TYPE_FLOAT, "", "Alpha reference value" )
  23. SHADER_PARAM( DISPLACEMENTMAP, SHADER_PARAM_TYPE_TEXTURE, "shadertest/BaseTexture", "Displacement map" )
  24. SHADER_PARAM( DISPLACEMENTWRINKLE, SHADER_PARAM_TYPE_BOOL, "0", "Displacement map contains wrinkle displacements")
  25. SHADER_PARAM( COLOR_DEPTH, SHADER_PARAM_TYPE_BOOL, "0", "Write depth as color" )
  26. // vertexlitgeneric tree sway animation control
  27. SHADER_PARAM( TREESWAY, SHADER_PARAM_TYPE_INTEGER, "0", "" );
  28. SHADER_PARAM( TREESWAYHEIGHT, SHADER_PARAM_TYPE_FLOAT, "1000", "" );
  29. SHADER_PARAM( TREESWAYSTARTHEIGHT, SHADER_PARAM_TYPE_FLOAT, "0.2", "" );
  30. SHADER_PARAM( TREESWAYRADIUS, SHADER_PARAM_TYPE_FLOAT, "300", "" );
  31. SHADER_PARAM( TREESWAYSTARTRADIUS, SHADER_PARAM_TYPE_FLOAT, "0.1", "" );
  32. SHADER_PARAM( TREESWAYSPEED, SHADER_PARAM_TYPE_FLOAT, "1", "" );
  33. SHADER_PARAM( TREESWAYSPEEDHIGHWINDMULTIPLIER, SHADER_PARAM_TYPE_FLOAT, "2", "" );
  34. SHADER_PARAM( TREESWAYSTRENGTH, SHADER_PARAM_TYPE_FLOAT, "10", "" );
  35. SHADER_PARAM( TREESWAYSCRUMBLESPEED, SHADER_PARAM_TYPE_FLOAT, "0.1", "" );
  36. SHADER_PARAM( TREESWAYSCRUMBLESTRENGTH, SHADER_PARAM_TYPE_FLOAT, "0.1", "" );
  37. SHADER_PARAM( TREESWAYSCRUMBLEFREQUENCY, SHADER_PARAM_TYPE_FLOAT, "0.1", "" );
  38. SHADER_PARAM( TREESWAYFALLOFFEXP, SHADER_PARAM_TYPE_FLOAT, "1.5", "" );
  39. SHADER_PARAM( TREESWAYSCRUMBLEFALLOFFEXP, SHADER_PARAM_TYPE_FLOAT, "1.0", "" );
  40. SHADER_PARAM( TREESWAYSPEEDLERPSTART, SHADER_PARAM_TYPE_FLOAT, "3", "" );
  41. SHADER_PARAM( TREESWAYSPEEDLERPEND, SHADER_PARAM_TYPE_FLOAT, "6", "" );
  42. SHADER_PARAM( TREESWAYSTATIC, SHADER_PARAM_TYPE_BOOL, "0", "" );
  43. END_SHADER_PARAMS
  44. SHADER_INIT_PARAMS()
  45. {
  46. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  47. #if !defined( CSTRIKE15 )
  48. if ( IsGameConsole() )
  49. {
  50. params[TREESWAY]->SetIntValue( 0 );
  51. }
  52. #else
  53. if ( IsPlatformOSX() || IsPS3() )
  54. {
  55. params[TREESWAY]->SetIntValue( 0 );
  56. }
  57. #endif
  58. }
  59. SHADER_FALLBACK
  60. {
  61. return 0;
  62. }
  63. SHADER_INIT
  64. {
  65. if ( params[DISPLACEMENTMAP]->IsDefined() )
  66. {
  67. LoadTexture( DISPLACEMENTMAP );
  68. }
  69. }
  70. SHADER_DRAW
  71. {
  72. bool bAlphaClip = IS_FLAG_SET( MATERIAL_VAR_ALPHATEST );
  73. int nTreeSwayMode = GetIntParam( TREESWAY, params, 0 );
  74. nTreeSwayMode = clamp( nTreeSwayMode, 0, 2 );
  75. bool bHasDisplacement = params[DISPLACEMENTMAP]->IsTexture();
  76. #if !defined( PLATFORM_X360 ) && !defined( _PS3 )
  77. bool bHasDisplacementWrinkles = params[DISPLACEMENTWRINKLE]->GetIntValue() != 0;
  78. #endif
  79. int nColorDepth = GetIntParam( COLOR_DEPTH, params, 0 );
  80. SHADOW_STATE
  81. {
  82. // Set stream format (note that this shader supports compression)
  83. unsigned int flags = VERTEX_POSITION | VERTEX_FORMAT_COMPRESSED;
  84. int nTexCoordCount = 1;
  85. int userDataSize = 0;
  86. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  87. // Bias primitives when rendering into shadow map so we get slope-scaled depth bias
  88. // rather than having to apply a constant bias in the filtering shader later
  89. if ( nColorDepth == 0 )
  90. {
  91. pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_SHADOW_BIAS );
  92. }
  93. // Turn off writes to color buffer since we always sample shadows from the DEPTH texture later
  94. // This gives us double-speed fill when rendering INTO the shadow map
  95. pShaderShadow->EnableColorWrites( nColorDepth == 1 );
  96. pShaderShadow->EnableAlphaWrites( false );
  97. // Turn off srgb writes to color depth buffer
  98. pShaderShadow->EnableSRGBWrite( false );
  99. // Don't backface cull unless alpha clipping, since this can cause artifacts when the
  100. // geometry is clipped by the flashlight near plane
  101. // If a material was already marked nocull, don't cull it
  102. pShaderShadow->EnableCulling( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) && !IS_FLAG_SET(MATERIAL_VAR_NOCULL) );
  103. if ( bHasDisplacement && IsPC() && g_pHardwareConfig->SupportsPixelShaders_3_0() )
  104. {
  105. pShaderShadow->EnableVertexTexture( SHADER_VERTEXTEXTURE_SAMPLER2, true );
  106. }
  107. #if !defined( _X360 ) && !defined( _PS3 )
  108. if ( !g_pHardwareConfig->SupportsPixelShaders_3_0() )
  109. #endif
  110. {
  111. DECLARE_STATIC_VERTEX_SHADER( depthwrite_vs20 );
  112. 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
  113. SET_STATIC_VERTEX_SHADER_COMBO( COLOR_DEPTH, nColorDepth );
  114. SET_STATIC_VERTEX_SHADER_COMBO( TREESWAY, nTreeSwayMode );
  115. SET_STATIC_VERTEX_SHADER( depthwrite_vs20 );
  116. if ( bAlphaClip || g_pHardwareConfig->PlatformRequiresNonNullPixelShaders() || nColorDepth )
  117. {
  118. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  119. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  120. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  121. {
  122. DECLARE_STATIC_PIXEL_SHADER( depthwrite_ps20b );
  123. SET_STATIC_PIXEL_SHADER_COMBO( COLOR_DEPTH, nColorDepth );
  124. SET_STATIC_PIXEL_SHADER( depthwrite_ps20b );
  125. }
  126. else
  127. {
  128. DECLARE_STATIC_PIXEL_SHADER( depthwrite_ps20 );
  129. SET_STATIC_PIXEL_SHADER_COMBO( COLOR_DEPTH, nColorDepth );
  130. SET_STATIC_PIXEL_SHADER( depthwrite_ps20 );
  131. }
  132. }
  133. }
  134. #if !defined( _X360 ) && !defined( _PS3 )
  135. else
  136. {
  137. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  138. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_TESSELLATION );
  139. DECLARE_STATIC_VERTEX_SHADER( depthwrite_vs30 );
  140. SET_STATIC_VERTEX_SHADER_COMBO( ONLY_PROJECT_POSITION, 0 ); //360 only combo, and this is a PC path
  141. SET_STATIC_VERTEX_SHADER_COMBO( TREESWAY, nTreeSwayMode );
  142. SET_STATIC_VERTEX_SHADER_COMBO( COLOR_DEPTH, nColorDepth );
  143. SET_STATIC_VERTEX_SHADER( depthwrite_vs30 );
  144. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  145. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  146. DECLARE_STATIC_PIXEL_SHADER( depthwrite_ps30 );
  147. SET_STATIC_PIXEL_SHADER_COMBO( COLOR_DEPTH, nColorDepth );
  148. SET_STATIC_PIXEL_SHADER( depthwrite_ps30 );
  149. }
  150. #endif
  151. }
  152. DYNAMIC_STATE
  153. {
  154. #if !defined( _X360 ) && !defined( _PS3 )
  155. if ( !g_pHardwareConfig->SupportsPixelShaders_3_0() )
  156. #endif
  157. {
  158. DECLARE_DYNAMIC_VERTEX_SHADER( depthwrite_vs20 );
  159. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  160. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, ( int )vertexCompression );
  161. SET_DYNAMIC_VERTEX_SHADER_COMBO( TESSELLATION, 0 );
  162. SET_DYNAMIC_VERTEX_SHADER( depthwrite_vs20 );
  163. if ( bAlphaClip )
  164. {
  165. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, FRAME );
  166. float vAlphaThreshold[4] = {0.7f, 0.7f, 0.7f, 0.7f};
  167. if ( ALPHATESTREFERENCE != -1 && ( params[ALPHATESTREFERENCE]->GetFloatValue() > 0.0f ) )
  168. {
  169. vAlphaThreshold[0] = vAlphaThreshold[1] = vAlphaThreshold[2] = vAlphaThreshold[3] = params[ALPHATESTREFERENCE]->GetFloatValue();
  170. }
  171. pShaderAPI->SetPixelShaderConstant( 0, vAlphaThreshold, 1 );
  172. }
  173. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  174. {
  175. DECLARE_DYNAMIC_PIXEL_SHADER( depthwrite_ps20b );
  176. SET_DYNAMIC_PIXEL_SHADER_COMBO( ALPHACLIP, bAlphaClip );
  177. SET_DYNAMIC_PIXEL_SHADER( depthwrite_ps20b );
  178. }
  179. else
  180. {
  181. DECLARE_DYNAMIC_PIXEL_SHADER( depthwrite_ps20 );
  182. SET_DYNAMIC_PIXEL_SHADER_COMBO( ALPHACLIP, bAlphaClip );
  183. SET_DYNAMIC_PIXEL_SHADER( depthwrite_ps20 );
  184. }
  185. }
  186. #if !defined( _X360 ) && !defined( _PS3 )
  187. else // 3.0 shader case (PC only)
  188. {
  189. TessellationMode_t nTessellationMode = pShaderAPI->GetTessellationMode();
  190. if ( nTessellationMode != TESSELLATION_MODE_DISABLED )
  191. {
  192. pShaderAPI->BindStandardVertexTexture( SHADER_VERTEXTEXTURE_SAMPLER1, TEXTURE_SUBDIVISION_PATCHES );
  193. float vSubDControls[4] = { 1.0f/pShaderAPI->GetSubDHeight(),
  194. bHasDisplacement && mat_displacementmap.GetBool() ? 1.0f : 0.0f,
  195. bHasDisplacementWrinkles && mat_displacementmap.GetBool() ? 1.0f : 0.0f, 0.0f };
  196. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_8, vSubDControls );
  197. if( bHasDisplacement )
  198. {
  199. BindVertexTexture( SHADER_VERTEXTEXTURE_SAMPLER2, DISPLACEMENTMAP );
  200. }
  201. else
  202. {
  203. pShaderAPI->BindStandardVertexTexture( SHADER_VERTEXTEXTURE_SAMPLER2, TEXTURE_BLACK );
  204. }
  205. // Currently, tessellation is mutually exclusive with any kind of GPU-side skinning, morphing or vertex compression
  206. Assert( !pShaderAPI->IsHWMorphingEnabled() );
  207. Assert( pShaderAPI->GetCurrentNumBones() == 0 );
  208. Assert( vertexCompression == 0);
  209. }
  210. if ( g_pHardwareConfig->HasFastVertexTextures() )
  211. {
  212. SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, SHADER_VERTEXTEXTURE_SAMPLER0 );
  213. }
  214. DECLARE_DYNAMIC_VERTEX_SHADER( depthwrite_vs30 );
  215. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, ( pShaderAPI->GetCurrentNumBones() > 0 ) && ( nTessellationMode == TESSELLATION_MODE_DISABLED ) );
  216. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression && ( nTessellationMode == TESSELLATION_MODE_DISABLED ) );
  217. SET_DYNAMIC_VERTEX_SHADER_COMBO( TESSELLATION, nTessellationMode );
  218. SET_DYNAMIC_VERTEX_SHADER( depthwrite_vs30 );
  219. if ( bAlphaClip )
  220. {
  221. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, FRAME );
  222. float vAlphaThreshold[4] = {0.7f, 0.7f, 0.7f, 0.7f};
  223. if ( ALPHATESTREFERENCE != -1 && ( params[ALPHATESTREFERENCE]->GetFloatValue() > 0.0f ) )
  224. {
  225. vAlphaThreshold[0] = vAlphaThreshold[1] = vAlphaThreshold[2] = vAlphaThreshold[3] = params[ALPHATESTREFERENCE]->GetFloatValue();
  226. }
  227. pShaderAPI->SetPixelShaderConstant( 0, vAlphaThreshold, 1 );
  228. }
  229. DECLARE_DYNAMIC_PIXEL_SHADER( depthwrite_ps30 );
  230. SET_DYNAMIC_PIXEL_SHADER_COMBO( ALPHACLIP, bAlphaClip );
  231. SET_DYNAMIC_PIXEL_SHADER( depthwrite_ps30 );
  232. }
  233. #endif
  234. if ( nTreeSwayMode != 0 )
  235. {
  236. float flParams[4];
  237. flParams[0] = pShaderAPI->CurrentTime();
  238. Vector windDir = IsBoolSet( TREESWAYSTATIC, params ) ? Vector(0.5f,0.5f,0) : pShaderAPI->GetVectorRenderingParameter( VECTOR_RENDERPARM_WIND_DIRECTION );
  239. flParams[1] = windDir.x;
  240. flParams[2] = windDir.y;
  241. flParams[3] = 0.0f;
  242. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, flParams );
  243. flParams[0] = GetFloatParam( TREESWAYSCRUMBLEFALLOFFEXP, params, 1.0f );
  244. flParams[1] = GetFloatParam( TREESWAYFALLOFFEXP, params, 1.0f );
  245. flParams[2] = GetFloatParam( TREESWAYSCRUMBLESPEED, params, 3.0f );
  246. flParams[3] = GetFloatParam( TREESWAYSPEEDHIGHWINDMULTIPLIER, params, 2.0f );
  247. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, flParams );
  248. flParams[0] = GetFloatParam( TREESWAYHEIGHT, params, 1000.0f );
  249. flParams[1] = GetFloatParam( TREESWAYSTARTHEIGHT, params, 0.1f );
  250. flParams[2] = GetFloatParam( TREESWAYRADIUS, params, 300.0f );
  251. flParams[3] = GetFloatParam( TREESWAYSTARTRADIUS, params, 0.2f );
  252. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, flParams );
  253. flParams[0] = GetFloatParam( TREESWAYSPEED, params, 1.0f );
  254. flParams[1] = GetFloatParam( TREESWAYSTRENGTH, params, 10.0f );
  255. flParams[2] = GetFloatParam( TREESWAYSCRUMBLEFREQUENCY, params, 12.0f );
  256. flParams[3] = GetFloatParam( TREESWAYSCRUMBLESTRENGTH, params, 10.0f );
  257. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_5, flParams );
  258. flParams[0] = GetFloatParam( TREESWAYSPEEDLERPSTART, params, 3.0f );
  259. flParams[1] = GetFloatParam( TREESWAYSPEEDLERPEND, params, 6.0f );
  260. flParams[2] = 0.0f;
  261. flParams[3] = 0.0f;
  262. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_9, flParams );
  263. }
  264. } // DYNAMIC_STATE
  265. Draw( );
  266. }
  267. void ExecuteFastPath( int *dynVSIdx, int *dynPSIdx, IMaterialVar** params, IShaderDynamicAPI * pShaderAPI,
  268. VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContextDataPtr, BOOL bCSMEnabled )
  269. {
  270. *dynVSIdx = -1;
  271. *dynPSIdx = -1;
  272. int numBones = ShaderApiFast( pShaderAPI )->GetCurrentNumBones();
  273. int m_nCOMPRESSED_VERTS = vertexCompression? 1: 0;
  274. int m_nSKINNING = (numBones > 0)? 1: 0;
  275. int m_nMORPHING = 0;
  276. int m_nTESSELLATION = 0;
  277. *dynVSIdx = ( 1 * m_nCOMPRESSED_VERTS ) + ( 2 * m_nSKINNING ) + ( 4 * m_nMORPHING ) + ( 8 * m_nTESSELLATION ) + 0;
  278. int mtlFlags = params[FLAGS]->GetIntValue();
  279. int m_nALPHACLIP = (mtlFlags & MATERIAL_VAR_ALPHATEST)? 1: 0;
  280. *dynPSIdx = ( 1 * m_nALPHACLIP ) + 0;
  281. if ( m_nALPHACLIP )
  282. {
  283. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, FRAME );
  284. float vAlphaThreshold[4] = {0.7f, 0.7f, 0.7f, 0.7f};
  285. if ( ALPHATESTREFERENCE != -1 && ( params[ALPHATESTREFERENCE]->GetFloatValue() > 0.0f ) )
  286. {
  287. vAlphaThreshold[0] = vAlphaThreshold[1] = vAlphaThreshold[2] = vAlphaThreshold[3] =
  288. params[ALPHATESTREFERENCE]->GetFloatValue();
  289. }
  290. pShaderAPI->SetPixelShaderConstant( 0, vAlphaThreshold, 1 );
  291. }
  292. int nTreeSwayMode = GetIntParam( TREESWAY, params, 0 );
  293. if ( nTreeSwayMode != 0 )
  294. {
  295. float flParams[4];
  296. flParams[0] = pShaderAPI->CurrentTime();
  297. Vector windDir = IsBoolSet( TREESWAYSTATIC, params ) ? Vector(0.5f,0.5f,0) : pShaderAPI->GetVectorRenderingParameter( VECTOR_RENDERPARM_WIND_DIRECTION );
  298. flParams[1] = windDir.x;
  299. flParams[2] = windDir.y;
  300. flParams[3] = 0.0f;
  301. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, flParams );
  302. flParams[0] = GetFloatParam( TREESWAYSCRUMBLEFALLOFFEXP, params, 1.0f );
  303. flParams[1] = GetFloatParam( TREESWAYFALLOFFEXP, params, 1.0f );
  304. flParams[2] = GetFloatParam( TREESWAYSCRUMBLESPEED, params, 3.0f );
  305. flParams[3] = GetFloatParam( TREESWAYSPEEDHIGHWINDMULTIPLIER, params, 2.0f );
  306. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, flParams );
  307. flParams[0] = GetFloatParam( TREESWAYHEIGHT, params, 1000.0f );
  308. flParams[1] = GetFloatParam( TREESWAYSTARTHEIGHT, params, 0.1f );
  309. flParams[2] = GetFloatParam( TREESWAYRADIUS, params, 300.0f );
  310. flParams[3] = GetFloatParam( TREESWAYSTARTRADIUS, params, 0.2f );
  311. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, flParams );
  312. flParams[0] = GetFloatParam( TREESWAYSPEED, params, 1.0f );
  313. flParams[1] = GetFloatParam( TREESWAYSTRENGTH, params, 10.0f );
  314. flParams[2] = GetFloatParam( TREESWAYSCRUMBLEFREQUENCY, params, 12.0f );
  315. flParams[3] = GetFloatParam( TREESWAYSCRUMBLESTRENGTH, params, 10.0f );
  316. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_5, flParams );
  317. flParams[0] = GetFloatParam( TREESWAYSPEEDLERPSTART, params, 3.0f );
  318. flParams[1] = GetFloatParam( TREESWAYSPEEDLERPEND, params, 6.0f );
  319. flParams[2] = 0.0f;
  320. flParams[3] = 0.0f;
  321. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_9, flParams );
  322. }
  323. // WVP
  324. pShaderAPI->SetVertexShaderViewProj();
  325. pShaderAPI->UpdateVertexShaderMatrix(0);
  326. }
  327. END_SHADER