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.

610 lines
25 KiB

  1. //========= Copyright (c) Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "mathlib/vmatrix.h"
  9. #include "common_hlsl_cpp_consts.h" // hack hack hack!
  10. #include "convar.h"
  11. #include "customclothing_vs20.inc"
  12. #include "customclothing_vs30.inc"
  13. #include "customclothing_ps20b.inc"
  14. #include "customclothing_ps30.inc"
  15. #include "writez_vs20.inc"
  16. #include "white_ps20.inc"
  17. #include "white_ps20b.inc"
  18. #include "shaderlib/commandbuilder.h"
  19. // NOTE: This has to be the last file included!
  20. #include "tier0/memdbgon.h"
  21. static const float kDefaultColor[3] = { 0.5f, 0.5f, 0.5f };
  22. #define MODE_3D_PREVIEW 0
  23. #define MODE_2D_COMPOSITE 1
  24. #define MODE_3D_POSTCOMPOSITE 2
  25. BEGIN_VS_SHADER( CustomClothing, "Help for CustomClothing" )
  26. BEGIN_SHADER_PARAMS
  27. //always required
  28. SHADER_PARAM( BASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  29. SHADER_PARAM( BUMPMAP, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  30. //only for 3d post-composite
  31. SHADER_PARAM( AOSCREENBUFFER, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  32. //composite inputs, only for 3d preview and 2d composite
  33. SHADER_PARAM( AOMAP, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  34. SHADER_PARAM( MASKMAP, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  35. SHADER_PARAM( OFFSETMAP, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  36. SHADER_PARAM( PATTERN1, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  37. SHADER_PARAM( PATTERN2, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  38. SHADER_PARAM( GRIME, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  39. SHADER_PARAM( LOGOMAP, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  40. SHADER_PARAM( LOGOX, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  41. SHADER_PARAM( LOGOY, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  42. SHADER_PARAM( LOGOSCALE, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  43. SHADER_PARAM( LOGOROTATE, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  44. SHADER_PARAM( LOGO2X, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  45. SHADER_PARAM( LOGO2Y, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  46. SHADER_PARAM( LOGO2SCALE, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  47. SHADER_PARAM( LOGO2ROTATE, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  48. SHADER_PARAM( LOGO2ENABLED, SHADER_PARAM_TYPE_BOOL, 0, "" )
  49. SHADER_PARAM( LOGOMASKCRISP, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  50. SHADER_PARAM( LOGOWEAR, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  51. SHADER_PARAM( PATTERN1COLOR1, SHADER_PARAM_TYPE_VEC3, "", "" )
  52. SHADER_PARAM( PATTERN1COLOR2, SHADER_PARAM_TYPE_VEC3, "", "" )
  53. SHADER_PARAM( PATTERN1COLOR3, SHADER_PARAM_TYPE_VEC3, "", "" )
  54. SHADER_PARAM( PATTERN1COLOR4, SHADER_PARAM_TYPE_VEC3, "", "" )
  55. SHADER_PARAM( PATTERN1SCALE, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  56. SHADER_PARAM( PATTERN2COLOR1, SHADER_PARAM_TYPE_VEC3, "", "" )
  57. SHADER_PARAM( PATTERN2COLOR2, SHADER_PARAM_TYPE_VEC3, "", "" )
  58. SHADER_PARAM( PATTERN2COLOR3, SHADER_PARAM_TYPE_VEC3, "", "" )
  59. SHADER_PARAM( PATTERN2COLOR4, SHADER_PARAM_TYPE_VEC3, "", "" )
  60. SHADER_PARAM( PATTERN2SCALE, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  61. SHADER_PARAM( SWAPPATTERNMASKS, SHADER_PARAM_TYPE_BOOL, 0, "" )
  62. SHADER_PARAM( CAVITYCONTRAST, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  63. SHADER_PARAM( OFFSETAMOUNT, SHADER_PARAM_TYPE_FLOAT, 0, "" )
  64. SHADER_PARAM( ENTCENTER, SHADER_PARAM_TYPE_VEC3, "", "" )
  65. //sets the behavior of the shader for different purposes
  66. SHADER_PARAM( COMPOSITEMODE, SHADER_PARAM_TYPE_INTEGER, MODE_3D_PREVIEW, "" )
  67. //override parameter for dynamic combo
  68. SHADER_PARAM( AOPASS, SHADER_PARAM_TYPE_BOOL, 0, "" )
  69. END_SHADER_PARAMS
  70. SHADER_INIT_PARAMS()
  71. {
  72. SET_PARAM_INT_IF_NOT_DEFINED( COMPOSITEMODE, MODE_3D_PREVIEW )
  73. SET_PARAM_INT_IF_NOT_DEFINED( AOPASS, 0 )
  74. SET_PARAM_STRING_IF_NOT_DEFINED( BASETEXTURE, "Dev/flat_normal" );
  75. SET_PARAM_STRING_IF_NOT_DEFINED( BUMPMAP, "Dev/flat_normal" );
  76. SET_PARAM_STRING_IF_NOT_DEFINED( AOSCREENBUFFER, "_rt_character_ssao" );
  77. if ( params[ COMPOSITEMODE ]->GetIntValue() == MODE_2D_COMPOSITE )
  78. {
  79. //we never want lighting to affect a 2D image-space composite
  80. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_UNLIT );
  81. }
  82. else
  83. {
  84. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  85. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  86. }
  87. if ( params[ COMPOSITEMODE ]->GetIntValue() == MODE_3D_PREVIEW && !g_pHardwareConfig->SupportsPixelShaders_3_0() )
  88. {
  89. DevWarning( "CustomClothing shader cannot run in preview quality mode on non-sm3 hardware.\n" );
  90. params[ COMPOSITEMODE ]->SetIntValue( MODE_3D_POSTCOMPOSITE );
  91. }
  92. if ( params[ COMPOSITEMODE ]->GetIntValue() == MODE_3D_PREVIEW || params[ COMPOSITEMODE ]->GetIntValue() == MODE_2D_COMPOSITE )
  93. {
  94. SET_PARAM_STRING_IF_NOT_DEFINED( GRIME, "models/player/custom_player/shared/clothing_grime" );
  95. // throw some warnings if we can't find necessary composite input maps
  96. if ( !params[ AOMAP ]->IsDefined() )
  97. {
  98. IMaterial *pOwnerMaterial = params[AOMAP]->GetOwningMaterial();
  99. char szMaterialName[MAX_PATH] = "unknown CustomClothing material";
  100. if ( pOwnerMaterial )
  101. V_strcpy_safe( szMaterialName, pOwnerMaterial->GetName() );
  102. DevWarning( "Warning: $aomap is undefined in %s\n", szMaterialName );
  103. }
  104. if ( !params[ MASKMAP ]->IsDefined() )
  105. {
  106. IMaterial *pOwnerMaterial = params[MASKMAP]->GetOwningMaterial();
  107. char szMaterialName[MAX_PATH] = "unknown CustomClothing material";
  108. if ( pOwnerMaterial )
  109. V_strcpy_safe( szMaterialName, pOwnerMaterial->GetName() );
  110. DevWarning( "Warning: $maskmap is undefined in %s\n", szMaterialName );
  111. }
  112. SET_PARAM_INT_IF_NOT_DEFINED( SWAPPATTERNMASKS, 0 );
  113. //if pattern2 is defined but pattern1 isn't, secretly move pattern2 into pattern1 and flag the pattern masks to swap
  114. if ( !params[ PATTERN1 ]->IsDefined() && params[ PATTERN2 ]->IsDefined() )
  115. {
  116. params[ PATTERN1 ]->SetStringValue( params[ PATTERN2 ]->GetStringValue() );
  117. params[ PATTERN1COLOR1 ]->SetVecValue( params[ PATTERN2COLOR1 ]->GetVecValue(), 4 );
  118. params[ PATTERN1COLOR2 ]->SetVecValue( params[ PATTERN2COLOR2 ]->GetVecValue(), 4 );
  119. params[ PATTERN1COLOR3 ]->SetVecValue( params[ PATTERN2COLOR3 ]->GetVecValue(), 4 );
  120. params[ PATTERN1COLOR4 ]->SetVecValue( params[ PATTERN2COLOR4 ]->GetVecValue(), 4 );
  121. params[ PATTERN1SCALE ]->SetFloatValue( params[ PATTERN2SCALE ]->GetFloatValue() );
  122. params[ PATTERN2 ]->SetUndefined();
  123. params[ SWAPPATTERNMASKS ]->SetIntValue( params[ SWAPPATTERNMASKS ]->GetIntValue() == 0 ? 1 : 0 );
  124. }
  125. //TODO: detect if pattern1 and pattern2 are the same and bind them to the same sampler
  126. SET_PARAM_FLOAT_IF_NOT_DEFINED( LOGOX, 0.5f );
  127. SET_PARAM_FLOAT_IF_NOT_DEFINED( LOGOY, 0.5f );
  128. SET_PARAM_FLOAT_IF_NOT_DEFINED( LOGOSCALE, 1 );
  129. SET_PARAM_FLOAT_IF_NOT_DEFINED( LOGOROTATE, 0 );
  130. SET_PARAM_FLOAT_IF_NOT_DEFINED( LOGO2X, 0.5f );
  131. SET_PARAM_FLOAT_IF_NOT_DEFINED( LOGO2Y, 0.5f );
  132. SET_PARAM_FLOAT_IF_NOT_DEFINED( LOGO2SCALE, 1 );
  133. SET_PARAM_FLOAT_IF_NOT_DEFINED( LOGO2ROTATE, 0 );
  134. SET_PARAM_FLOAT_IF_NOT_DEFINED( LOGOMASKCRISP, 0.5f );
  135. SET_PARAM_FLOAT_IF_NOT_DEFINED( LOGOWEAR, 0.5f );
  136. SET_PARAM_VEC_IF_NOT_DEFINED( PATTERN1COLOR1, kDefaultColor, 3 );
  137. SET_PARAM_VEC_IF_NOT_DEFINED( PATTERN1COLOR2, kDefaultColor, 3 );
  138. SET_PARAM_VEC_IF_NOT_DEFINED( PATTERN1COLOR3, kDefaultColor, 3 );
  139. SET_PARAM_VEC_IF_NOT_DEFINED( PATTERN1COLOR4, kDefaultColor, 3 );
  140. SET_PARAM_FLOAT_IF_NOT_DEFINED( PATTERN1SCALE, 1 );
  141. SET_PARAM_VEC_IF_NOT_DEFINED( PATTERN2COLOR1, kDefaultColor, 3 );
  142. SET_PARAM_VEC_IF_NOT_DEFINED( PATTERN2COLOR2, kDefaultColor, 3 );
  143. SET_PARAM_VEC_IF_NOT_DEFINED( PATTERN2COLOR3, kDefaultColor, 3 );
  144. SET_PARAM_VEC_IF_NOT_DEFINED( PATTERN2COLOR4, kDefaultColor, 3 );
  145. SET_PARAM_FLOAT_IF_NOT_DEFINED( PATTERN2SCALE, 1 );
  146. SET_PARAM_FLOAT_IF_NOT_DEFINED( CAVITYCONTRAST, 1 );
  147. SET_PARAM_FLOAT_IF_NOT_DEFINED( OFFSETAMOUNT, 0.2f );
  148. }
  149. }
  150. SHADER_FALLBACK
  151. {
  152. return 0;
  153. }
  154. SHADER_INIT
  155. {
  156. if ( params[AOPASS]->GetIntValue() != 1 )
  157. {
  158. if ( params[BASETEXTURE]->IsDefined() ) { LoadTexture( BASETEXTURE ); }
  159. }
  160. if ( params[BUMPMAP]->IsDefined() ) { LoadBumpMap( BUMPMAP ); }
  161. if ( params[ COMPOSITEMODE ]->GetIntValue() == MODE_3D_POSTCOMPOSITE )
  162. {
  163. if ( params[AOSCREENBUFFER]->IsDefined() ) { LoadTexture( AOSCREENBUFFER ); }
  164. }
  165. if ( params[ COMPOSITEMODE ]->GetIntValue() == MODE_3D_PREVIEW || params[ COMPOSITEMODE ]->GetIntValue() == MODE_2D_COMPOSITE )
  166. {
  167. if ( params[AOMAP]->IsDefined() ) { LoadTexture( AOMAP ); }
  168. if ( params[MASKMAP]->IsDefined() ) { LoadTexture( MASKMAP ); }
  169. if ( params[PATTERN1]->IsDefined() ) { LoadTexture( PATTERN1 ); }
  170. if ( params[PATTERN2]->IsDefined() ) { LoadTexture( PATTERN2 ); }
  171. if ( params[GRIME]->IsDefined() ) { LoadTexture( GRIME ); }
  172. if ( params[OFFSETMAP]->IsDefined() ) { LoadTexture( OFFSETMAP ); }
  173. if ( params[LOGOMAP]->IsDefined() ) { LoadTexture( LOGOMAP ); }
  174. }
  175. }
  176. inline void DrawCustomClothing( IMaterialVar **params, IShaderShadow* pShaderShadow, IShaderDynamicAPI* pShaderAPI, int vertexCompression )
  177. {
  178. SHADOW_STATE
  179. {
  180. SetInitialShadowState( );
  181. int iCompositeMode = params[ COMPOSITEMODE ]->GetIntValue();
  182. if ( params[AOPASS]->GetIntValue() != 1 )
  183. {
  184. if ( params[BASETEXTURE]->IsDefined() ) { pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); }
  185. }
  186. //pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  187. if ( params[BUMPMAP]->IsDefined() ) { pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); }
  188. if ( iCompositeMode == MODE_3D_POSTCOMPOSITE )
  189. {
  190. if ( params[AOSCREENBUFFER]->IsDefined() ) { pShaderShadow->EnableTexture( SHADER_SAMPLER10, true ); }
  191. }
  192. bool bNeedCompositeInputs = iCompositeMode == MODE_2D_COMPOSITE || iCompositeMode == MODE_3D_PREVIEW;
  193. if ( bNeedCompositeInputs )
  194. {
  195. if ( params[AOMAP]->IsDefined() ) { pShaderShadow->EnableTexture( SHADER_SAMPLER2, true ); }
  196. if ( params[MASKMAP]->IsDefined() ) { pShaderShadow->EnableTexture( SHADER_SAMPLER3, true ); }
  197. if ( params[PATTERN1]->IsDefined() ) { pShaderShadow->EnableTexture( SHADER_SAMPLER4, true ); }
  198. if ( params[PATTERN2]->IsDefined() ) { pShaderShadow->EnableTexture( SHADER_SAMPLER6, true ); }
  199. if ( params[GRIME]->IsDefined() ) { pShaderShadow->EnableTexture( SHADER_SAMPLER7, true ); }
  200. if ( params[OFFSETMAP]->IsDefined() ) { pShaderShadow->EnableTexture( SHADER_SAMPLER8, true ); }
  201. if ( params[LOGOMAP]->IsDefined() ) { pShaderShadow->EnableTexture( SHADER_SAMPLER9, true ); }
  202. }
  203. if ( iCompositeMode == MODE_2D_COMPOSITE )
  204. {
  205. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ZERO ); //assume we're compositing onto a dirty surface
  206. }
  207. else
  208. {
  209. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  210. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_ALWAYS, 0.0f );
  211. /*
  212. BlendFunc( SHADER_BLEND_ZERO, SHADER_BLEND_ZERO );
  213. BlendOp( SHADER_BLEND_OP_ADD );
  214. EnableBlendingSeparateAlpha( false );
  215. BlendFuncSeparateAlpha( SHADER_BLEND_ZERO, SHADER_BLEND_ZERO );
  216. BlendOpSeparateAlpha( SHADER_BLEND_OP_ADD );
  217. AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, 0.7f );
  218. */
  219. // Normalizing cube map
  220. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true );
  221. }
  222. unsigned int flags = VERTEX_POSITION| VERTEX_FORMAT_COMPRESSED;
  223. if ( iCompositeMode != MODE_2D_COMPOSITE )
  224. flags |= VERTEX_NORMAL;
  225. int nTexCoordCount = 1;
  226. int userDataSize = 0;
  227. if (IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR ))
  228. {
  229. flags |= VERTEX_COLOR;
  230. }
  231. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  232. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  233. {
  234. DECLARE_STATIC_VERTEX_SHADER( customclothing_vs30 );
  235. SET_STATIC_VERTEX_SHADER_COMBO( COMPOSITEMODE, iCompositeMode );
  236. SET_STATIC_VERTEX_SHADER( customclothing_vs30 );
  237. }
  238. else
  239. {
  240. DECLARE_STATIC_VERTEX_SHADER( customclothing_vs20 );
  241. SET_STATIC_VERTEX_SHADER_COMBO( COMPOSITEMODE, iCompositeMode );
  242. SET_STATIC_VERTEX_SHADER( customclothing_vs20 );
  243. }
  244. bool bUsePattern1 = bNeedCompositeInputs && params[ PATTERN1 ]->IsDefined() && params[ PATTERN1 ]->IsTexture();
  245. bool bUsePattern2 = bNeedCompositeInputs && params[ PATTERN2 ]->IsDefined() && params[ PATTERN2 ]->IsTexture();
  246. bool bUsePatternOffset = bNeedCompositeInputs && (bUsePattern1 || bUsePattern2) && params[ OFFSETMAP ]->IsDefined() && params[ OFFSETMAP ]->IsTexture();
  247. bool bUseLogo1 = bNeedCompositeInputs && params[ LOGOMAP ]->IsDefined() && params[ LOGOMAP ]->IsTexture();
  248. bool bUseLogo2 = bNeedCompositeInputs && bUseLogo1 && params[ LOGO2ENABLED ]->IsDefined() && (params[ LOGO2ENABLED ]->GetIntValue() > 0);
  249. bool bSwapPatternMasks = bNeedCompositeInputs && bUsePattern2 && (params[ SWAPPATTERNMASKS ]->GetIntValue() > 0);
  250. bool bCSMEnabled = g_pHardwareConfig->SupportsCascadedShadowMapping();
  251. int nCSMQualityComboValue = g_pHardwareConfig->GetCSMShaderMode( materials->GetCurrentConfigForVideoCard().GetCSMQualityMode() );
  252. if ( iCompositeMode == MODE_2D_COMPOSITE || !bCSMEnabled || params[ AOPASS ]->GetIntValue() > 0 )
  253. {
  254. bCSMEnabled = false;
  255. nCSMQualityComboValue = 0;
  256. }
  257. if ( params[ AOPASS ]->GetIntValue() > 0 )
  258. {
  259. bUsePattern1 = false;
  260. bUsePattern2 = false;
  261. bUsePatternOffset = false;
  262. bUseLogo1 = false;
  263. bUseLogo2 = false;
  264. bSwapPatternMasks = false;
  265. }
  266. if( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  267. {
  268. DECLARE_STATIC_PIXEL_SHADER( customclothing_ps30 );
  269. SET_STATIC_PIXEL_SHADER_COMBO( CASCADED_SHADOW_MAPPING, bCSMEnabled );
  270. SET_STATIC_PIXEL_SHADER_COMBO( COMPOSITEMODE, iCompositeMode );
  271. SET_STATIC_PIXEL_SHADER_COMBO( CSM_MODE, nCSMQualityComboValue );
  272. SET_STATIC_PIXEL_SHADER_COMBO( USE_PATTERN1, bUsePattern1 );
  273. SET_STATIC_PIXEL_SHADER_COMBO( USE_PATTERN2, bUsePattern2 );
  274. SET_STATIC_PIXEL_SHADER_COMBO( USE_PATTERN_OFFSET, bUsePatternOffset );
  275. SET_STATIC_PIXEL_SHADER_COMBO( USE_LOGO1, bUseLogo1 );
  276. SET_STATIC_PIXEL_SHADER_COMBO( USE_LOGO2, bUseLogo2 );
  277. SET_STATIC_PIXEL_SHADER_COMBO( SWAP_PATTERN_MASKS, bSwapPatternMasks );
  278. SET_STATIC_PIXEL_SHADER( customclothing_ps30 );
  279. }
  280. else
  281. {
  282. DECLARE_STATIC_PIXEL_SHADER( customclothing_ps20b );
  283. SET_STATIC_PIXEL_SHADER_COMBO( COMPOSITEMODE, iCompositeMode );
  284. SET_STATIC_PIXEL_SHADER_COMBO( USE_PATTERN1, bUsePattern1 );
  285. SET_STATIC_PIXEL_SHADER_COMBO( USE_PATTERN2, bUsePattern2 );
  286. SET_STATIC_PIXEL_SHADER_COMBO( USE_PATTERN_OFFSET, bUsePatternOffset );
  287. SET_STATIC_PIXEL_SHADER_COMBO( USE_LOGO1, bUseLogo1 );
  288. SET_STATIC_PIXEL_SHADER_COMBO( USE_LOGO2, bUseLogo2 );
  289. SET_STATIC_PIXEL_SHADER_COMBO( SWAP_PATTERN_MASKS, bSwapPatternMasks );
  290. SET_STATIC_PIXEL_SHADER( customclothing_ps20b );
  291. }
  292. pShaderShadow->EnableAlphaWrites( true );
  293. pShaderShadow->EnableDepthWrites( true );
  294. pShaderShadow->EnableSRGBWrite( true );
  295. pShaderShadow->EnableBlending( false ); // important for csm shadows
  296. pShaderShadow->EnableAlphaTest( true ); // for ghillie suits and other leafy things
  297. if ( iCompositeMode != MODE_2D_COMPOSITE )
  298. {
  299. PI_BeginCommandBuffer();
  300. PI_SetPixelShaderAmbientLightCube( PSREG_AMBIENT_CUBE );
  301. PI_SetPixelShaderLocalLighting( PSREG_LIGHT_INFO_ARRAY );
  302. //pShader->PI_SetModulationPixelShaderDynamicState_LinearColorSpace( 1 );
  303. PI_EndCommandBuffer();
  304. }
  305. }
  306. DYNAMIC_STATE
  307. {
  308. pShaderAPI->SetDefaultState();
  309. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  310. int iCompositeMode = params[ COMPOSITEMODE ]->GetIntValue();
  311. if ( params[AOPASS]->GetIntValue() != 1 )
  312. {
  313. if ( params[BASETEXTURE]->IsDefined() ) { BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, -1 ); }
  314. }
  315. if ( params[BUMPMAP]->IsDefined() ) { BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, BUMPMAP, -1 ); }
  316. if ( iCompositeMode == MODE_3D_POSTCOMPOSITE )
  317. {
  318. //TEXTURE_BINDFLAGS_SHADOWDEPTH
  319. if ( params[AOSCREENBUFFER]->IsDefined() ) { BindTexture( SHADER_SAMPLER10, TEXTURE_BINDFLAGS_NONE, AOSCREENBUFFER, -1 ); }
  320. }
  321. if ( iCompositeMode == MODE_2D_COMPOSITE || iCompositeMode == MODE_3D_PREVIEW )
  322. {
  323. if ( params[AOMAP]->IsDefined() ) { BindTexture( SHADER_SAMPLER2, TEXTURE_BINDFLAGS_NONE, AOMAP, -1 ); }
  324. if ( params[MASKMAP]->IsDefined() ) { BindTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_NONE, MASKMAP, -1 ); }
  325. if ( params[PATTERN1]->IsDefined() ) { BindTexture( SHADER_SAMPLER4, TEXTURE_BINDFLAGS_NONE, PATTERN1, -1 ); }
  326. if ( params[PATTERN2]->IsDefined() ) { BindTexture( SHADER_SAMPLER6, TEXTURE_BINDFLAGS_NONE, PATTERN2, -1 ); }
  327. if ( params[GRIME]->IsDefined() ) { BindTexture( SHADER_SAMPLER7, TEXTURE_BINDFLAGS_NONE, GRIME, -1 ); }
  328. if ( params[OFFSETMAP]->IsDefined() ) { BindTexture( SHADER_SAMPLER8, TEXTURE_BINDFLAGS_NONE, OFFSETMAP, -1 ); }
  329. if ( params[LOGOMAP]->IsDefined() ) { BindTexture( SHADER_SAMPLER9, TEXTURE_BINDFLAGS_SRGBREAD, LOGOMAP, -1 ); }
  330. float c0[4] = { SrgbGammaToLinear( params[PATTERN1COLOR1]->GetVecValue()[0] / 255.0 ),
  331. SrgbGammaToLinear( params[PATTERN1COLOR1]->GetVecValue()[1] / 255.0 ),
  332. SrgbGammaToLinear( params[PATTERN1COLOR1]->GetVecValue()[2] / 255.0 ),
  333. SrgbGammaToLinear( params[PATTERN1COLOR4]->GetVecValue()[0] / 255.0 ) };
  334. float c1[4] = { SrgbGammaToLinear( params[PATTERN1COLOR2]->GetVecValue()[0] / 255.0 ),
  335. SrgbGammaToLinear( params[PATTERN1COLOR2]->GetVecValue()[1] / 255.0 ),
  336. SrgbGammaToLinear( params[PATTERN1COLOR2]->GetVecValue()[2] / 255.0 ),
  337. SrgbGammaToLinear( params[PATTERN1COLOR4]->GetVecValue()[1] / 255.0 ) };
  338. float c2[4] = { SrgbGammaToLinear( params[PATTERN1COLOR3]->GetVecValue()[0] / 255.0 ),
  339. SrgbGammaToLinear( params[PATTERN1COLOR3]->GetVecValue()[1] / 255.0 ),
  340. SrgbGammaToLinear( params[PATTERN1COLOR3]->GetVecValue()[2] / 255.0 ),
  341. SrgbGammaToLinear( params[PATTERN1COLOR4]->GetVecValue()[2] / 255.0 ) };
  342. pShaderAPI->SetPixelShaderConstant( 0, c0, 1 );
  343. pShaderAPI->SetPixelShaderConstant( 1, c1, 1 );
  344. pShaderAPI->SetPixelShaderConstant( 2, c2, 1 );
  345. float c10[4] = { SrgbGammaToLinear( params[PATTERN2COLOR1]->GetVecValue()[0] / 255.0 ),
  346. SrgbGammaToLinear( params[PATTERN2COLOR1]->GetVecValue()[1] / 255.0 ),
  347. SrgbGammaToLinear( params[PATTERN2COLOR1]->GetVecValue()[2] / 255.0 ),
  348. SrgbGammaToLinear( params[PATTERN2COLOR4]->GetVecValue()[0] / 255.0 ) };
  349. float c11[4] = { SrgbGammaToLinear( params[PATTERN2COLOR2]->GetVecValue()[0] / 255.0 ),
  350. SrgbGammaToLinear( params[PATTERN2COLOR2]->GetVecValue()[1] / 255.0 ),
  351. SrgbGammaToLinear( params[PATTERN2COLOR2]->GetVecValue()[2] / 255.0 ),
  352. SrgbGammaToLinear( params[PATTERN2COLOR4]->GetVecValue()[1] / 255.0 ) };
  353. float c12[4] = { SrgbGammaToLinear( params[PATTERN2COLOR3]->GetVecValue()[0] / 255.0 ),
  354. SrgbGammaToLinear( params[PATTERN2COLOR3]->GetVecValue()[1] / 255.0 ),
  355. SrgbGammaToLinear( params[PATTERN2COLOR3]->GetVecValue()[2] / 255.0 ),
  356. SrgbGammaToLinear( params[PATTERN2COLOR4]->GetVecValue()[2] / 255.0 ) };
  357. pShaderAPI->SetPixelShaderConstant( 10, c10, 1 );
  358. pShaderAPI->SetPixelShaderConstant( 11, c11, 1 );
  359. pShaderAPI->SetPixelShaderConstant( 12, c12, 1 );
  360. float c13[4] = { params[PATTERN1SCALE]->GetFloatValue(),
  361. params[PATTERN2SCALE]->GetFloatValue(),
  362. params[CAVITYCONTRAST]->GetFloatValue(),
  363. params[OFFSETAMOUNT]->GetFloatValue() };
  364. pShaderAPI->SetPixelShaderConstant( 13, c13, 1 );
  365. float c14[4] = { params[LOGOX]->GetFloatValue(),
  366. params[LOGOY]->GetFloatValue(),
  367. params[LOGOSCALE]->GetFloatValue(),
  368. params[LOGOROTATE]->GetFloatValue() };
  369. pShaderAPI->SetPixelShaderConstant( 14, c14, 1 );
  370. float c15[4] = { params[LOGO2X]->GetFloatValue(),
  371. params[LOGO2Y]->GetFloatValue(),
  372. params[LOGO2SCALE]->GetFloatValue(),
  373. params[LOGO2ROTATE]->GetFloatValue() };
  374. pShaderAPI->SetPixelShaderConstant( 15, c15, 1 );
  375. float c16[4] = { 0.5f - params[LOGOMASKCRISP]->GetFloatValue(),
  376. 0.5f + params[LOGOMASKCRISP]->GetFloatValue(),
  377. params[LOGOWEAR]->GetFloatValue(),
  378. 0 };
  379. pShaderAPI->SetPixelShaderConstant( 16, c16, 1 );
  380. }
  381. bool bAOPrePass = params[ AOPASS ]->GetIntValue() > 0;
  382. if ( iCompositeMode != MODE_2D_COMPOSITE )
  383. {
  384. pShaderAPI->BindStandardTexture( SHADER_SAMPLER5, TEXTURE_BINDFLAGS_NONE, TEXTURE_NORMALIZATION_CUBEMAP_SIGNED );
  385. float c18[4];
  386. pShaderAPI->GetWorldSpaceCameraPosition( c18 );
  387. if ( bAOPrePass )
  388. {
  389. Vector vecWorldSpaceCamPos = Vector( c18[0], c18[1], c18[2] );
  390. Vector vecEntityCenter = Vector( params[ ENTCENTER ]->GetVecValue()[0], params[ ENTCENTER ]->GetVecValue()[1], params[ ENTCENTER ]->GetVecValue()[2] );
  391. c18[3] = (vecWorldSpaceCamPos - vecEntityCenter).Length();
  392. }
  393. else
  394. {
  395. c18[3] = 0;
  396. }
  397. pShaderAPI->SetPixelShaderConstant( 18, c18, 1 );
  398. }
  399. if ( iCompositeMode == MODE_3D_POSTCOMPOSITE )
  400. {
  401. // Get viewport and render target dimensions and set shader constant to do a 2D mad
  402. int nViewportX, nViewportY, nViewportWidth, nViewportHeight;
  403. pShaderAPI->GetCurrentViewport( nViewportX, nViewportY, nViewportWidth, nViewportHeight );
  404. int nRtWidth, nRtHeight;
  405. pShaderAPI->GetCurrentRenderTargetDimensions( nRtWidth, nRtHeight );
  406. // Compute viewport mad that takes projection space coords (post divide by W) into normalized screenspace, taking into account the currently set viewport.
  407. float vViewportMad[4];
  408. vViewportMad[0] = .5f * ( ( float )nViewportWidth / ( float )nRtWidth );
  409. vViewportMad[1] = -.5f * ( ( float )nViewportHeight / ( float )nRtHeight );
  410. vViewportMad[2] = vViewportMad[0] + ( ( float )nViewportX / ( float )nRtWidth );
  411. vViewportMad[3] = -vViewportMad[1] + ( ( float )nViewportY / ( float )nRtHeight );
  412. pShaderAPI->SetPixelShaderConstant( 17, vViewportMad, 1 );
  413. }
  414. int nNumLights = 0;
  415. if ( !bAOPrePass && iCompositeMode != MODE_2D_COMPOSITE )
  416. {
  417. LightState_t lightState = {0, false, false};
  418. pShaderAPI->GetDX9LightState( &lightState );
  419. nNumLights = lightState.m_nNumLights;
  420. }
  421. if( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  422. {
  423. DECLARE_DYNAMIC_VERTEX_SHADER( customclothing_vs30 );
  424. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  425. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  426. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, nNumLights );
  427. SET_DYNAMIC_VERTEX_SHADER( customclothing_vs30 );
  428. }
  429. else
  430. {
  431. DECLARE_DYNAMIC_VERTEX_SHADER( customclothing_vs20 );
  432. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  433. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  434. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, nNumLights );
  435. SET_DYNAMIC_VERTEX_SHADER( customclothing_vs20 );
  436. }
  437. bool bCSMEnabled = pShaderAPI->IsCascadedShadowMapping() && (iCompositeMode != MODE_2D_COMPOSITE);
  438. if ( bCSMEnabled && !bAOPrePass )
  439. {
  440. ITexture *pDepthTextureAtlas = NULL;
  441. const CascadedShadowMappingState_t &cascadeState = pShaderAPI->GetCascadedShadowMappingState( &pDepthTextureAtlas );
  442. if ( pDepthTextureAtlas )
  443. {
  444. BindTexture( SHADER_SAMPLER15, TEXTURE_BINDFLAGS_SHADOWDEPTH, pDepthTextureAtlas, 0 );
  445. //float vCSMTexParams[4] = { cascadeState.m_TexParams3.m_flDistLerpFactorBase, cascadeState.m_TexParams3.m_flDistLerpFactorInvRange, cascadeState.m_TexParams.m_flInvShadowTextureWidth, 0.0f };
  446. //pShaderAPI->SetPixelShaderConstant( 17, &cascadeState.m_vCamPosition.x, 1 );
  447. //pShaderAPI->SetPixelShaderConstant( 18, vCSMTexParams, 1 );
  448. pShaderAPI->SetPixelShaderConstant( 64, &cascadeState.m_vLightColor.x, CASCADED_SHADOW_MAPPING_CONSTANT_BUFFER_SIZE );
  449. }
  450. }
  451. if( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  452. {
  453. DECLARE_DYNAMIC_PIXEL_SHADER( customclothing_ps30 );
  454. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, nNumLights );
  455. SET_DYNAMIC_PIXEL_SHADER_COMBO( DYN_CSM_ENABLED, bCSMEnabled && !bAOPrePass );
  456. SET_DYNAMIC_PIXEL_SHADER_COMBO( AO_MODE, bAOPrePass );
  457. SET_DYNAMIC_PIXEL_SHADER( customclothing_ps30 );
  458. }
  459. else
  460. {
  461. DECLARE_DYNAMIC_PIXEL_SHADER( customclothing_ps20b );
  462. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, nNumLights );
  463. SET_DYNAMIC_PIXEL_SHADER_COMBO( DYN_CSM_ENABLED, 0 );
  464. SET_DYNAMIC_PIXEL_SHADER_COMBO( AO_MODE, bAOPrePass );
  465. SET_DYNAMIC_PIXEL_SHADER( customclothing_ps20b );
  466. }
  467. }
  468. Draw();
  469. }
  470. SHADER_DRAW
  471. {
  472. DrawCustomClothing( params, pShaderShadow, pShaderAPI, vertexCompression );
  473. }
  474. void ExecuteFastPath( int *dynVSIdx, int *dynPSIdx, IMaterialVar** params, IShaderDynamicAPI * pShaderAPI,
  475. VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContextDataPtr, BOOL bCSMEnabled )
  476. {
  477. //only intended for csm shadow depth pass
  478. DECLARE_DYNAMIC_VERTEX_SHADER( writez_vs20 );
  479. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  480. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  481. SET_DYNAMIC_VERTEX_SHADER( writez_vs20 );
  482. *dynVSIdx = _vshIndex.GetIndex();
  483. // No pixel shader on Direct3D, doubles fill rate
  484. if ( IsOSXOpenGL() )
  485. {
  486. DECLARE_DYNAMIC_PIXEL_SHADER( white_ps20 );
  487. SET_DYNAMIC_PIXEL_SHADER( white_ps20 );
  488. *dynPSIdx = _pshIndex.GetIndex();
  489. }
  490. else
  491. {
  492. *dynPSIdx = 0;
  493. }
  494. }
  495. bool IsTranslucent( IMaterialVar **params ) const
  496. {
  497. return false;
  498. }
  499. END_SHADER