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.

990 lines
38 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #include "BaseVSShader.h"
  9. #include "skin_dx9_helper.h"
  10. #include "convar.h"
  11. #include "cpp_shader_constant_register_map.h"
  12. #include "skin_vs20.inc"
  13. #include "skin_ps20b.inc"
  14. #include "commandbuilder.h"
  15. #ifndef _X360
  16. #include "skin_vs30.inc"
  17. #include "skin_ps30.inc"
  18. #endif
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. static ConVar mat_fullbright( "mat_fullbright", "0", FCVAR_CHEAT );
  22. static ConVar r_lightwarpidentity( "r_lightwarpidentity", "0", FCVAR_CHEAT );
  23. static ConVar r_rimlight( "r_rimlight", "1", FCVAR_NONE );
  24. // Textures may be bound to the following samplers:
  25. // SHADER_SAMPLER0 Base (Albedo) / Gloss in alpha
  26. // SHADER_SAMPLER1 Specular warp (including iridescence)
  27. // SHADER_SAMPLER2 Diffuse Lighting warp texture
  28. // SHADER_SAMPLER3 Normal Map
  29. // SHADER_SAMPLER4 Flashlight Shadow Depth Map
  30. // SHADER_SAMPLER5 Normalization cube map
  31. // SHADER_SAMPLER6 Flashlight Cookie
  32. // SHADER_SAMPLER7 Specular exponent
  33. // SHADER_SAMPLER8 Cubic environment map
  34. // SHADER_SAMPLER9 Compressed wrinklemap
  35. // SHADER_SAMPLER10 Stretched wrinklemap
  36. // SHADER_SAMPLER11 Compressed wrinkle normal map
  37. // SHADER_SAMPLER12 Stretched wrinkle normal map
  38. // SHADER_SAMPLER13 Detail texture
  39. //-----------------------------------------------------------------------------
  40. // Initialize shader parameters
  41. //-----------------------------------------------------------------------------
  42. void InitParamsSkin_DX9( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, VertexLitGeneric_DX9_Vars_t &info )
  43. {
  44. // FLASHLIGHTFIXME: Do ShaderAPI::BindFlashlightTexture
  45. Assert( info.m_nFlashlightTexture >= 0 );
  46. if ( g_pHardwareConfig->SupportsBorderColor() )
  47. {
  48. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight_border" );
  49. }
  50. else
  51. {
  52. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight001" );
  53. }
  54. // Write over $basetexture with $info.m_nBumpmap if we are going to be using diffuse normal mapping.
  55. if( info.m_nAlbedo != -1 && g_pConfig->UseBumpmapping() && info.m_nBumpmap != -1 && params[info.m_nBumpmap]->IsDefined() && params[info.m_nAlbedo]->IsDefined() &&
  56. params[info.m_nBaseTexture]->IsDefined() )
  57. {
  58. params[info.m_nBaseTexture]->SetStringValue( params[info.m_nAlbedo]->GetStringValue() );
  59. }
  60. // This shader can be used with hw skinning
  61. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  62. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  63. // No texture means no env mask in base alpha
  64. if ( !params[info.m_nBaseTexture]->IsDefined() )
  65. {
  66. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  67. }
  68. // If in decal mode, no debug override...
  69. if (IS_FLAG_SET(MATERIAL_VAR_DECAL))
  70. {
  71. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  72. }
  73. // Lots of reasons to want tangent space, since we bind a flat normal map in many cases where we don't have a bump map
  74. bool bBump = (info.m_nBumpmap != -1) && g_pConfig->UseBumpmapping() && params[info.m_nBumpmap]->IsDefined();
  75. bool bEnvMap = (info.m_nEnvmap != -1) && params[info.m_nEnvmap]->IsDefined();
  76. bool bDiffuseWarp = (info.m_nDiffuseWarpTexture != -1) && params[info.m_nDiffuseWarpTexture]->IsDefined();
  77. bool bPhong = (info.m_nPhong != -1) && params[info.m_nPhong]->IsDefined();
  78. if( bBump || bEnvMap || bDiffuseWarp || bPhong )
  79. {
  80. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  81. }
  82. else
  83. {
  84. CLEAR_FLAGS( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );
  85. }
  86. if ( ( info.m_nSelfIllumFresnel != -1 ) && ( !params[info.m_nSelfIllumFresnel]->IsDefined() ) )
  87. {
  88. params[info.m_nSelfIllumFresnel]->SetIntValue( 0 );
  89. }
  90. if ( ( info.m_nSelfIllumFresnelMinMaxExp != -1 ) && ( !params[info.m_nSelfIllumFresnelMinMaxExp]->IsDefined() ) )
  91. {
  92. params[info.m_nSelfIllumFresnelMinMaxExp]->SetVecValue( 0.0f, 1.0f, 1.0f );
  93. }
  94. if ( ( info.m_nBaseMapAlphaPhongMask != -1 ) && ( !params[info.m_nBaseMapAlphaPhongMask]->IsDefined() ) )
  95. {
  96. params[info.m_nBaseMapAlphaPhongMask]->SetIntValue( 0 );
  97. }
  98. if ( ( info.m_nEnvmapFresnel != -1 ) && ( !params[info.m_nEnvmapFresnel]->IsDefined() ) )
  99. {
  100. params[info.m_nEnvmapFresnel]->SetFloatValue( 0 );
  101. }
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Initialize shader
  105. //-----------------------------------------------------------------------------
  106. void InitSkin_DX9( CBaseVSShader *pShader, IMaterialVar** params, VertexLitGeneric_DX9_Vars_t &info )
  107. {
  108. Assert( info.m_nFlashlightTexture >= 0 );
  109. pShader->LoadTexture( info.m_nFlashlightTexture, TEXTUREFLAGS_SRGB );
  110. bool bIsBaseTextureTranslucent = false;
  111. if ( params[info.m_nBaseTexture]->IsDefined() )
  112. {
  113. pShader->LoadTexture( info.m_nBaseTexture, TEXTUREFLAGS_SRGB );
  114. if ( params[info.m_nBaseTexture]->GetTextureValue()->IsTranslucent() )
  115. {
  116. bIsBaseTextureTranslucent = true;
  117. }
  118. if ( ( info.m_nWrinkle != -1 ) && ( info.m_nStretch != -1 ) &&
  119. params[info.m_nWrinkle]->IsDefined() && params[info.m_nStretch]->IsDefined() )
  120. {
  121. pShader->LoadTexture( info.m_nWrinkle, TEXTUREFLAGS_SRGB );
  122. pShader->LoadTexture( info.m_nStretch, TEXTUREFLAGS_SRGB );
  123. }
  124. }
  125. bool bHasSelfIllumMask = IS_FLAG_SET( MATERIAL_VAR_SELFILLUM ) && (info.m_nSelfIllumMask != -1) && params[info.m_nSelfIllumMask]->IsDefined();
  126. // No alpha channel in any of the textures? No self illum or envmapmask
  127. if ( !bIsBaseTextureTranslucent )
  128. {
  129. bool bHasSelfIllumFresnel = IS_FLAG_SET( MATERIAL_VAR_SELFILLUM ) && ( info.m_nSelfIllumFresnel != -1 ) && ( params[info.m_nSelfIllumFresnel]->GetIntValue() != 0 );
  130. // Can still be self illum with no base alpha if using one of these alternate modes
  131. if ( !bHasSelfIllumFresnel && !bHasSelfIllumMask )
  132. {
  133. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  134. }
  135. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  136. }
  137. if ( (info.m_nPhongExponentTexture != -1) && params[info.m_nPhongExponentTexture]->IsDefined() &&
  138. (info.m_nPhong != -1) && params[info.m_nPhong]->IsDefined() )
  139. {
  140. pShader->LoadTexture( info.m_nPhongExponentTexture );
  141. }
  142. if ( (info.m_nDiffuseWarpTexture != -1) && params[info.m_nDiffuseWarpTexture]->IsDefined() &&
  143. (info.m_nPhong != -1) && params[info.m_nPhong]->IsDefined() )
  144. {
  145. pShader->LoadTexture( info.m_nDiffuseWarpTexture );
  146. }
  147. if ( (info.m_nPhongWarpTexture != -1) && params[info.m_nPhongWarpTexture]->IsDefined() &&
  148. (info.m_nPhong != -1) && params[info.m_nPhong]->IsDefined() )
  149. {
  150. pShader->LoadTexture( info.m_nPhongWarpTexture );
  151. }
  152. if ( info.m_nDetail != -1 && params[info.m_nDetail]->IsDefined() )
  153. {
  154. int nDetailBlendMode = ( info.m_nDetailTextureCombineMode == -1 ) ? 0 : params[info.m_nDetailTextureCombineMode]->GetIntValue();
  155. if ( nDetailBlendMode == 0 ) // Mod2X
  156. pShader->LoadTexture( info.m_nDetail );
  157. else
  158. pShader->LoadTexture( info.m_nDetail, TEXTUREFLAGS_SRGB );
  159. }
  160. if ( g_pConfig->UseBumpmapping() )
  161. {
  162. if ( (info.m_nBumpmap != -1) && params[info.m_nBumpmap]->IsDefined() )
  163. {
  164. pShader->LoadBumpMap( info.m_nBumpmap );
  165. SET_FLAGS2( MATERIAL_VAR2_DIFFUSE_BUMPMAPPED_MODEL );
  166. if ( ( info.m_nNormalWrinkle != -1 ) && ( info.m_nNormalStretch != -1 ) &&
  167. params[info.m_nNormalWrinkle]->IsDefined() && params[info.m_nNormalStretch]->IsDefined() )
  168. {
  169. pShader->LoadTexture( info.m_nNormalWrinkle );
  170. pShader->LoadTexture( info.m_nNormalStretch );
  171. }
  172. }
  173. }
  174. if ( params[info.m_nEnvmap]->IsDefined() )
  175. {
  176. pShader->LoadCubeMap( info.m_nEnvmap, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE ? TEXTUREFLAGS_SRGB : 0 );
  177. }
  178. if ( bHasSelfIllumMask )
  179. {
  180. pShader->LoadTexture( info.m_nSelfIllumMask );
  181. }
  182. }
  183. class CSkin_DX9_Context : public CBasePerMaterialContextData
  184. {
  185. public:
  186. CCommandBufferBuilder< CFixedCommandStorageBuffer< 800 > > m_SemiStaticCmdsOut;
  187. bool m_bFastPath;
  188. };
  189. //-----------------------------------------------------------------------------
  190. // Draws the shader
  191. //-----------------------------------------------------------------------------
  192. void DrawSkin_DX9_Internal( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow,
  193. bool bHasFlashlight, VertexLitGeneric_DX9_Vars_t &info, VertexCompressionType_t vertexCompression,
  194. CBasePerMaterialContextData **pContextDataPtr )
  195. {
  196. bool bHasBaseTexture = (info.m_nBaseTexture != -1) && params[info.m_nBaseTexture]->IsTexture();
  197. bool bHasBump = (info.m_nBumpmap != -1) && params[info.m_nBumpmap]->IsTexture();
  198. bool bHasBaseTextureWrinkle = bHasBaseTexture &&
  199. (info.m_nWrinkle != -1) && params[info.m_nWrinkle]->IsTexture() &&
  200. (info.m_nStretch != -1) && params[info.m_nStretch]->IsTexture();
  201. bool bHasBumpWrinkle = bHasBump &&
  202. (info.m_nNormalWrinkle != -1) && params[info.m_nNormalWrinkle]->IsTexture() &&
  203. (info.m_nNormalStretch != -1) && params[info.m_nNormalStretch]->IsTexture();
  204. bool bHasVertexColor = IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR );
  205. bool bHasVertexAlpha = IS_FLAG_SET( MATERIAL_VAR_VERTEXALPHA );
  206. bool bIsAlphaTested = IS_FLAG_SET( MATERIAL_VAR_ALPHATEST ) != 0;
  207. bool bHasSelfIllum = IS_FLAG_SET( MATERIAL_VAR_SELFILLUM ) != 0;
  208. bool bHasSelfIllumFresnel = ( bHasSelfIllum ) && ( info.m_nSelfIllumFresnel != -1 ) && ( params[info.m_nSelfIllumFresnel]->GetIntValue() != 0 );
  209. bool bHasSelfIllumMask = ( bHasSelfIllum ) && (info.m_nSelfIllumMask != -1) && params[info.m_nSelfIllumMask]->IsTexture();
  210. // Tie these to specular
  211. bool bHasPhong = (info.m_nPhong != -1) && ( params[info.m_nPhong]->GetIntValue() != 0 );
  212. bool bHasSpecularExponentTexture = (info.m_nPhongExponentTexture != -1) && params[info.m_nPhongExponentTexture]->IsTexture();
  213. bool bHasPhongTintMap = bHasSpecularExponentTexture && (info.m_nPhongAlbedoTint != -1) && ( params[info.m_nPhongAlbedoTint]->GetIntValue() != 0 );
  214. bool bHasDiffuseWarp = (info.m_nDiffuseWarpTexture != -1) && params[info.m_nDiffuseWarpTexture]->IsTexture();
  215. bool bHasPhongWarp = (info.m_nPhongWarpTexture != -1) && params[info.m_nPhongWarpTexture]->IsTexture();
  216. bool bHasNormalMapAlphaEnvmapMask = IS_FLAG_SET( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );
  217. #if !defined( _X360 )
  218. bool bIsDecal = IS_FLAG_SET( MATERIAL_VAR_DECAL );
  219. #endif
  220. // Rimlight must be set to non-zero to trigger rim light combo (also requires Phong)
  221. bool bHasRimLight = r_rimlight.GetBool() && bHasPhong && (info.m_nRimLight != -1) && ( params[info.m_nRimLight]->GetIntValue() != 0 );
  222. bool bHasRimMaskMap = bHasSpecularExponentTexture && bHasRimLight && (info.m_nRimMask != -1) && ( params[info.m_nRimMask]->GetIntValue() != 0 );
  223. float fBlendFactor=( info.m_nDetailTextureBlendFactor == -1 )? 1 : params[info.m_nDetailTextureBlendFactor]->GetFloatValue();
  224. bool hasDetailTexture = ( info.m_nDetail != -1 ) && params[info.m_nDetail]->IsTexture();
  225. int nDetailBlendMode = ( hasDetailTexture && info.m_nDetailTextureCombineMode != -1 ) ? params[info.m_nDetailTextureCombineMode]->GetIntValue() : 0;
  226. bool bBlendTintByBaseAlpha = IsBoolSet( info.m_nBlendTintByBaseAlpha, params ) && !bHasSelfIllum; // Pixel shader can't do both BLENDTINTBYBASEALPHA and SELFILLUM, so let selfillum win
  227. float flTintReplacementAmount = GetFloatParam( info.m_nTintReplacesBaseColor, params );
  228. float flPhongExponentFactor = ( info.m_nPhongExponentFactor != -1 ) ? GetFloatParam( info.m_nPhongExponentFactor, params ) : 0.0f;
  229. const bool bHasPhongExponentFactor = flPhongExponentFactor != 0.0f;
  230. BlendType_t nBlendType= pShader->EvaluateBlendRequirements( bBlendTintByBaseAlpha ? -1 : info.m_nBaseTexture, true );
  231. bool bFullyOpaque = (nBlendType != BT_BLENDADD) && (nBlendType != BT_BLEND) && !bIsAlphaTested && !bHasFlashlight; //dest alpha is free for special use
  232. CSkin_DX9_Context *pContextData = reinterpret_cast< CSkin_DX9_Context *> ( *pContextDataPtr );
  233. if ( ! pContextData )
  234. {
  235. pContextData = new CSkin_DX9_Context;
  236. *pContextDataPtr = pContextData;
  237. }
  238. if( pShader->IsSnapshotting() )
  239. {
  240. // look at color and alphamod stuff.
  241. // Unlit generic never uses the flashlight
  242. bool bHasEnvmap = !bHasFlashlight && params[info.m_nEnvmap]->IsTexture();
  243. bool bHasNormal = params[info.m_nBumpmap]->IsTexture();
  244. bool bCanUseBaseAlphaPhongMaskFastPath = (info.m_nBaseMapAlphaPhongMask != -1) && ( params[info.m_nBaseMapAlphaPhongMask]->GetIntValue() != 0 );
  245. if ( ! ( params[info.m_nBaseTexture]->GetTextureValue()->IsTranslucent() ) )
  246. bCanUseBaseAlphaPhongMaskFastPath = true;
  247. pContextData->m_bFastPath =
  248. (! bHasBump ) &&
  249. (! bHasSpecularExponentTexture ) &&
  250. (! bHasPhongTintMap ) &&
  251. (! bHasPhongWarp ) &&
  252. (! bHasRimLight ) &&
  253. (! hasDetailTexture ) &&
  254. bCanUseBaseAlphaPhongMaskFastPath &&
  255. (! bHasSelfIllum ) &&
  256. (! bBlendTintByBaseAlpha );
  257. // Alpha test: FIXME: shouldn't this be handled in CBaseVSShader::SetInitialShadowState
  258. pShaderShadow->EnableAlphaTest( bIsAlphaTested );
  259. if( info.m_nAlphaTestReference != -1 && params[info.m_nAlphaTestReference]->GetFloatValue() > 0.0f )
  260. {
  261. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, params[info.m_nAlphaTestReference]->GetFloatValue() );
  262. }
  263. int nShadowFilterMode = 0;
  264. if( bHasFlashlight )
  265. {
  266. if (params[info.m_nBaseTexture]->IsTexture())
  267. {
  268. pShader->SetAdditiveBlendingShadowState( info.m_nBaseTexture, true );
  269. }
  270. if( bIsAlphaTested )
  271. {
  272. // disable alpha test and use the zfunc zequals since alpha isn't guaranteed to
  273. // be the same on both the regular pass and the flashlight pass.
  274. pShaderShadow->EnableAlphaTest( false );
  275. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_EQUAL );
  276. }
  277. pShaderShadow->EnableBlending( true );
  278. pShaderShadow->EnableDepthWrites( false );
  279. // Be sure not to write to dest alpha
  280. pShaderShadow->EnableAlphaWrites( false );
  281. nShadowFilterMode = g_pHardwareConfig->GetShadowFilterMode(); // Based upon vendor and device dependent formats
  282. }
  283. else // not flashlight pass
  284. {
  285. if (params[info.m_nBaseTexture]->IsTexture())
  286. {
  287. pShader->SetDefaultBlendingShadowState( info.m_nBaseTexture, true );
  288. }
  289. if ( bHasEnvmap )
  290. {
  291. pShaderShadow->EnableTexture( SHADER_SAMPLER8, true ); // Cubic environment map
  292. if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
  293. {
  294. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER8, true );
  295. }
  296. }
  297. }
  298. unsigned int flags = VERTEX_POSITION;
  299. if( bHasNormal )
  300. {
  301. flags |= VERTEX_NORMAL;
  302. }
  303. int userDataSize = 0;
  304. // Always enable...will bind white if nothing specified...
  305. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Base (albedo) map
  306. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  307. if ( bHasBaseTextureWrinkle || bHasBumpWrinkle )
  308. {
  309. pShaderShadow->EnableTexture( SHADER_SAMPLER9, true ); // Base (albedo) compression map
  310. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER9, true );
  311. pShaderShadow->EnableTexture( SHADER_SAMPLER10, true ); // Base (albedo) expansion map
  312. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER10, true );
  313. }
  314. if( bHasDiffuseWarp )
  315. {
  316. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true ); // Diffuse warp texture
  317. }
  318. if( bHasPhongWarp )
  319. {
  320. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Specular warp texture
  321. }
  322. // Specular exponent map or dummy
  323. pShaderShadow->EnableTexture( SHADER_SAMPLER7, true ); // Specular exponent map
  324. if( bHasFlashlight )
  325. {
  326. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true ); // Shadow depth map
  327. pShaderShadow->SetShadowDepthFiltering( SHADER_SAMPLER4 );
  328. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER4, false );
  329. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true ); // Noise map
  330. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true ); // Flashlight cookie
  331. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER6, true );
  332. userDataSize = 4; // tangent S
  333. }
  334. // Always enable, since flat normal will be bound
  335. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true ); // Normal map
  336. userDataSize = 4; // tangent S
  337. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true ); // Normalizing cube map
  338. if ( bHasBaseTextureWrinkle || bHasBumpWrinkle )
  339. {
  340. pShaderShadow->EnableTexture( SHADER_SAMPLER11, true ); // Normal compression map
  341. pShaderShadow->EnableTexture( SHADER_SAMPLER12, true ); // Normal expansion map
  342. }
  343. if ( hasDetailTexture )
  344. {
  345. pShaderShadow->EnableTexture( SHADER_SAMPLER13, true );
  346. if ( nDetailBlendMode != 0 ) //Not Mod2X
  347. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER13, true );
  348. }
  349. if ( bHasSelfIllum )
  350. {
  351. pShaderShadow->EnableTexture( SHADER_SAMPLER14, true );
  352. }
  353. if( bHasVertexColor || bHasVertexAlpha )
  354. {
  355. flags |= VERTEX_COLOR;
  356. }
  357. pShaderShadow->EnableSRGBWrite( true );
  358. // texcoord0 : base texcoord, texcoord2 : decal hw morph delta
  359. int pTexCoordDim[3] = { 2, 0, 3 };
  360. int nTexCoordCount = 1;
  361. #ifndef _X360
  362. // Special morphed decal information
  363. if ( bIsDecal && g_pHardwareConfig->HasFastVertexTextures() )
  364. {
  365. nTexCoordCount = 3;
  366. }
  367. #endif
  368. // This shader supports compressed vertices, so OR in that flag:
  369. flags |= VERTEX_FORMAT_COMPRESSED;
  370. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, pTexCoordDim, userDataSize );
  371. #ifndef _X360
  372. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  373. #endif
  374. {
  375. bool bUseStaticControlFlow = g_pHardwareConfig->SupportsStaticControlFlow();
  376. DECLARE_STATIC_VERTEX_SHADER( skin_vs20 );
  377. SET_STATIC_VERTEX_SHADER_COMBO( USE_STATIC_CONTROL_FLOW, bUseStaticControlFlow );
  378. SET_STATIC_VERTEX_SHADER( skin_vs20 );
  379. // Assume we're only going to get in here if we support 2b
  380. DECLARE_STATIC_PIXEL_SHADER( skin_ps20b );
  381. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  382. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, bHasSelfIllum && !bHasFlashlight );
  383. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUMFRESNEL, bHasSelfIllumFresnel && !bHasFlashlight );
  384. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTWARPTEXTURE, bHasDiffuseWarp && bHasPhong );
  385. SET_STATIC_PIXEL_SHADER_COMBO( PHONGWARPTEXTURE, bHasPhongWarp && bHasPhong );
  386. SET_STATIC_PIXEL_SHADER_COMBO( WRINKLEMAP, bHasBaseTextureWrinkle || bHasBumpWrinkle );
  387. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, hasDetailTexture );
  388. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  389. SET_STATIC_PIXEL_SHADER_COMBO( RIMLIGHT, bHasRimLight );
  390. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  391. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  392. SET_STATIC_PIXEL_SHADER_COMBO( CONVERT_TO_SRGB, 0 );
  393. SET_STATIC_PIXEL_SHADER_COMBO( FASTPATH_NOBUMP, pContextData->m_bFastPath );
  394. SET_STATIC_PIXEL_SHADER_COMBO( BLENDTINTBYBASEALPHA, bBlendTintByBaseAlpha );
  395. SET_STATIC_PIXEL_SHADER( skin_ps20b );
  396. }
  397. #ifndef _X360
  398. else
  399. {
  400. // The vertex shader uses the vertex id stream
  401. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  402. DECLARE_STATIC_VERTEX_SHADER( skin_vs30 );
  403. SET_STATIC_VERTEX_SHADER_COMBO( DECAL, bIsDecal );
  404. SET_STATIC_VERTEX_SHADER( skin_vs30 );
  405. DECLARE_STATIC_PIXEL_SHADER( skin_ps30 );
  406. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  407. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, bHasSelfIllum && !bHasFlashlight );
  408. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUMFRESNEL, bHasSelfIllumFresnel && !bHasFlashlight );
  409. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTWARPTEXTURE, bHasDiffuseWarp && bHasPhong );
  410. SET_STATIC_PIXEL_SHADER_COMBO( PHONGWARPTEXTURE, bHasPhongWarp && bHasPhong );
  411. SET_STATIC_PIXEL_SHADER_COMBO( WRINKLEMAP, bHasBaseTextureWrinkle || bHasBumpWrinkle );
  412. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, hasDetailTexture );
  413. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  414. SET_STATIC_PIXEL_SHADER_COMBO( RIMLIGHT, bHasRimLight );
  415. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  416. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  417. SET_STATIC_PIXEL_SHADER_COMBO( CONVERT_TO_SRGB, 0 );
  418. SET_STATIC_PIXEL_SHADER_COMBO( FASTPATH_NOBUMP, pContextData->m_bFastPath );
  419. SET_STATIC_PIXEL_SHADER_COMBO( BLENDTINTBYBASEALPHA, bBlendTintByBaseAlpha );
  420. SET_STATIC_PIXEL_SHADER( skin_ps30 );
  421. }
  422. #endif
  423. if( bHasFlashlight )
  424. {
  425. pShader->FogToBlack();
  426. }
  427. else
  428. {
  429. pShader->DefaultFog();
  430. }
  431. // HACK HACK HACK - enable alpha writes all the time so that we have them for underwater stuff
  432. pShaderShadow->EnableAlphaWrites( bFullyOpaque );
  433. }
  434. else // not snapshotting -- begin dynamic state
  435. {
  436. bool bLightingOnly = mat_fullbright.GetInt() == 2 && !IS_FLAG_SET( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  437. bool bHasEnvmap = !bHasFlashlight && params[info.m_nEnvmap]->IsTexture();
  438. if( bHasBaseTexture )
  439. {
  440. pShader->BindTexture( SHADER_SAMPLER0, info.m_nBaseTexture, info.m_nBaseTextureFrame );
  441. }
  442. else
  443. {
  444. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_WHITE );
  445. }
  446. if ( bHasBaseTextureWrinkle )
  447. {
  448. pShader->BindTexture( SHADER_SAMPLER9, info.m_nWrinkle, info.m_nBaseTextureFrame );
  449. pShader->BindTexture( SHADER_SAMPLER10, info.m_nStretch, info.m_nBaseTextureFrame );
  450. }
  451. else if ( bHasBumpWrinkle )
  452. {
  453. pShader->BindTexture( SHADER_SAMPLER9, info.m_nBaseTexture, info.m_nBaseTextureFrame );
  454. pShader->BindTexture( SHADER_SAMPLER10, info.m_nBaseTexture, info.m_nBaseTextureFrame );
  455. }
  456. if( bHasDiffuseWarp && bHasPhong )
  457. {
  458. if ( r_lightwarpidentity.GetBool() )
  459. {
  460. pShaderAPI->BindStandardTexture( SHADER_SAMPLER2, TEXTURE_IDENTITY_LIGHTWARP );
  461. }
  462. else
  463. {
  464. pShader->BindTexture( SHADER_SAMPLER2, info.m_nDiffuseWarpTexture );
  465. }
  466. }
  467. if( bHasPhongWarp )
  468. {
  469. pShader->BindTexture( SHADER_SAMPLER1, info.m_nPhongWarpTexture );
  470. }
  471. if( bHasSpecularExponentTexture && bHasPhong )
  472. {
  473. pShader->BindTexture( SHADER_SAMPLER7, info.m_nPhongExponentTexture );
  474. }
  475. else
  476. {
  477. pShaderAPI->BindStandardTexture( SHADER_SAMPLER7, TEXTURE_WHITE );
  478. }
  479. if( !g_pConfig->m_bFastNoBump )
  480. {
  481. if( bHasBump )
  482. pShader->BindTexture( SHADER_SAMPLER3, info.m_nBumpmap, info.m_nBumpFrame );
  483. else
  484. pShaderAPI->BindStandardTexture( SHADER_SAMPLER3, TEXTURE_NORMALMAP_FLAT );
  485. if ( bHasBumpWrinkle )
  486. {
  487. pShader->BindTexture( SHADER_SAMPLER11, info.m_nNormalWrinkle, info.m_nBumpFrame );
  488. pShader->BindTexture( SHADER_SAMPLER12, info.m_nNormalStretch, info.m_nBumpFrame );
  489. }
  490. else if ( bHasBaseTextureWrinkle )
  491. {
  492. pShader->BindTexture( SHADER_SAMPLER11, info.m_nBumpmap, info.m_nBumpFrame );
  493. pShader->BindTexture( SHADER_SAMPLER12, info.m_nBumpmap, info.m_nBumpFrame );
  494. }
  495. }
  496. else
  497. {
  498. if( bHasBump )
  499. {
  500. pShaderAPI->BindStandardTexture( SHADER_SAMPLER3, TEXTURE_NORMALMAP_FLAT );
  501. }
  502. if ( bHasBaseTextureWrinkle || bHasBumpWrinkle )
  503. {
  504. pShaderAPI->BindStandardTexture( SHADER_SAMPLER11, TEXTURE_NORMALMAP_FLAT );
  505. pShaderAPI->BindStandardTexture( SHADER_SAMPLER12, TEXTURE_NORMALMAP_FLAT );
  506. }
  507. }
  508. if ( hasDetailTexture )
  509. {
  510. pShader->BindTexture( SHADER_SAMPLER13, info.m_nDetail, info.m_nDetailFrame );
  511. }
  512. if ( bHasSelfIllum )
  513. {
  514. if ( bHasSelfIllumMask ) // Separate texture for self illum?
  515. {
  516. pShader->BindTexture( SHADER_SAMPLER14, info.m_nSelfIllumMask ); // Bind it
  517. }
  518. else // else
  519. {
  520. pShaderAPI->BindStandardTexture( SHADER_SAMPLER14, TEXTURE_BLACK ); // Bind dummy
  521. }
  522. }
  523. LightState_t lightState = { 0, false, false };
  524. bool bFlashlightShadows = false;
  525. if( bHasFlashlight )
  526. {
  527. Assert( info.m_nFlashlightTexture >= 0 && info.m_nFlashlightTextureFrame >= 0 );
  528. pShader->BindTexture( SHADER_SAMPLER6, info.m_nFlashlightTexture, info.m_nFlashlightTextureFrame );
  529. VMatrix worldToTexture;
  530. ITexture *pFlashlightDepthTexture;
  531. FlashlightState_t state = pShaderAPI->GetFlashlightStateEx( worldToTexture, &pFlashlightDepthTexture );
  532. bFlashlightShadows = state.m_bEnableShadows && ( pFlashlightDepthTexture != NULL );
  533. SetFlashLightColorFromState( state, pShaderAPI, PSREG_FLASHLIGHT_COLOR );
  534. if( pFlashlightDepthTexture && g_pConfig->ShadowDepthTexture() && state.m_bEnableShadows )
  535. {
  536. pShader->BindTexture( SHADER_SAMPLER4, pFlashlightDepthTexture, 0 );
  537. pShaderAPI->BindStandardTexture( SHADER_SAMPLER5, TEXTURE_SHADOW_NOISE_2D );
  538. }
  539. }
  540. else // no flashlight
  541. {
  542. if ( bHasEnvmap )
  543. {
  544. pShader->BindTexture( SHADER_SAMPLER8, info.m_nEnvmap, info.m_nEnvmapFrame );
  545. }
  546. pShaderAPI->GetDX9LightState( &lightState );
  547. }
  548. MaterialFogMode_t fogType = pShaderAPI->GetSceneFogMode();
  549. int fogIndex = ( fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ) ? 1 : 0;
  550. int numBones = pShaderAPI->GetCurrentNumBones();
  551. // don't have an easy way to get this through to GLM, so just print it old school
  552. //printf("\n-D- DrawSkin_DX9_Internal numBones is %d", numBones );
  553. bool bWriteDepthToAlpha = false;
  554. bool bWriteWaterFogToAlpha = false;
  555. if( bFullyOpaque )
  556. {
  557. bWriteDepthToAlpha = pShaderAPI->ShouldWriteDepthToDestAlpha();
  558. bWriteWaterFogToAlpha = (fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z);
  559. AssertMsg( !(bWriteDepthToAlpha && bWriteWaterFogToAlpha), "Can't write two values to alpha at the same time." );
  560. }
  561. #ifndef _X360
  562. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  563. #endif
  564. {
  565. bool bUseStaticControlFlow = g_pHardwareConfig->SupportsStaticControlFlow();
  566. DECLARE_DYNAMIC_VERTEX_SHADER( skin_vs20 );
  567. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  568. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, numBones > 0 );
  569. SET_DYNAMIC_VERTEX_SHADER_COMBO( LIGHTING_PREVIEW, pShaderAPI->GetIntRenderingParameter(INT_RENDERPARM_ENABLE_FIXED_LIGHTING)!=0);
  570. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  571. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, bUseStaticControlFlow ? 0 : lightState.m_nNumLights );
  572. SET_DYNAMIC_VERTEX_SHADER( skin_vs20 );
  573. DECLARE_DYNAMIC_PIXEL_SHADER( skin_ps20b );
  574. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  575. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, bWriteWaterFogToAlpha );
  576. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bWriteDepthToAlpha );
  577. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  578. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  579. SET_DYNAMIC_PIXEL_SHADER_COMBO( PHONG_USE_EXPONENT_FACTOR, bHasPhongExponentFactor );
  580. SET_DYNAMIC_PIXEL_SHADER( skin_ps20b );
  581. }
  582. #ifndef _X360
  583. else
  584. {
  585. pShader->SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, SHADER_VERTEXTEXTURE_SAMPLER0 );
  586. DECLARE_DYNAMIC_VERTEX_SHADER( skin_vs30 );
  587. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  588. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, numBones > 0 );
  589. SET_DYNAMIC_VERTEX_SHADER_COMBO( LIGHTING_PREVIEW, pShaderAPI->GetIntRenderingParameter(INT_RENDERPARM_ENABLE_FIXED_LIGHTING)!=0);
  590. SET_DYNAMIC_VERTEX_SHADER_COMBO( MORPHING, pShaderAPI->IsHWMorphingEnabled() );
  591. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  592. SET_DYNAMIC_VERTEX_SHADER( skin_vs30 );
  593. DECLARE_DYNAMIC_PIXEL_SHADER( skin_ps30 );
  594. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  595. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, bWriteWaterFogToAlpha );
  596. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bWriteDepthToAlpha );
  597. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  598. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  599. SET_DYNAMIC_PIXEL_SHADER_COMBO( PHONG_USE_EXPONENT_FACTOR, bHasPhongExponentFactor );
  600. SET_DYNAMIC_PIXEL_SHADER( skin_ps30 );
  601. bool bUnusedTexCoords[3] = { false, false, !pShaderAPI->IsHWMorphingEnabled() || !bIsDecal };
  602. pShaderAPI->MarkUnusedVertexFields( 0, 3, bUnusedTexCoords );
  603. }
  604. #endif
  605. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nBaseTextureTransform );
  606. if( bHasBump )
  607. {
  608. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, info.m_nBumpTransform );
  609. }
  610. if ( hasDetailTexture )
  611. {
  612. if ( IS_PARAM_DEFINED( info.m_nDetailTextureTransform ) )
  613. pShader->SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4,
  614. info.m_nDetailTextureTransform,
  615. info.m_nDetailScale );
  616. else
  617. pShader->SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4,
  618. info.m_nBaseTextureTransform,
  619. info.m_nDetailScale );
  620. }
  621. pShader->SetModulationPixelShaderDynamicState_LinearColorSpace( 1 );
  622. pShader->SetPixelShaderConstant_W( PSREG_SELFILLUMTINT, info.m_nSelfIllumTint, fBlendFactor );
  623. bool bInvertPhongMask = ( info.m_nInvertPhongMask != -1 ) && ( params[info.m_nInvertPhongMask]->GetIntValue() != 0 );
  624. float fInvertPhongMask = bInvertPhongMask ? 1 : 0;
  625. bool bHasBaseAlphaPhongMask = (info.m_nBaseMapAlphaPhongMask != -1) && ( params[info.m_nBaseMapAlphaPhongMask]->GetIntValue() != 0 );
  626. float fHasBaseAlphaPhongMask = bHasBaseAlphaPhongMask ? 1 : 0;
  627. // Controls for lerp-style paths through shader code
  628. float vShaderControls[4] = { fHasBaseAlphaPhongMask, 0.0f/*unused*/, flTintReplacementAmount, fInvertPhongMask };
  629. pShaderAPI->SetPixelShaderConstant( PSREG_CONSTANT_27, vShaderControls, 1 );
  630. if ( hasDetailTexture )
  631. {
  632. #if 0 // needs constant change
  633. if ( info.m_nDetailTint != -1 )
  634. pShader->SetPixelShaderConstantGammaToLinear( 10, info.m_nDetailTint );
  635. else
  636. {
  637. float boring_tint[4]={1,1,1,1};
  638. pShaderAPI->SetPixelShaderConstant( 10, boring_tint, 1 );
  639. }
  640. #endif
  641. }
  642. if ( bHasSelfIllumFresnel && !bHasFlashlight )
  643. {
  644. float vConstScaleBiasExp[4] = { 1.0f, 0.0f, 1.0f, 0.0f };
  645. float flMin = IS_PARAM_DEFINED( info.m_nSelfIllumFresnelMinMaxExp ) ? params[info.m_nSelfIllumFresnelMinMaxExp]->GetVecValue()[0] : 0.0f;
  646. float flMax = IS_PARAM_DEFINED( info.m_nSelfIllumFresnelMinMaxExp ) ? params[info.m_nSelfIllumFresnelMinMaxExp]->GetVecValue()[1] : 1.0f;
  647. float flExp = IS_PARAM_DEFINED( info.m_nSelfIllumFresnelMinMaxExp ) ? params[info.m_nSelfIllumFresnelMinMaxExp]->GetVecValue()[2] : 1.0f;
  648. vConstScaleBiasExp[1] = ( flMax != 0.0f ) ? ( flMin / flMax ) : 0.0f; // Bias
  649. vConstScaleBiasExp[0] = 1.0f - vConstScaleBiasExp[1]; // Scale
  650. vConstScaleBiasExp[2] = flExp; // Exp
  651. vConstScaleBiasExp[3] = flMax; // Brightness
  652. pShaderAPI->SetPixelShaderConstant( PSREG_SELFILLUM_SCALE_BIAS_EXP, vConstScaleBiasExp, 1 );
  653. }
  654. pShader->SetAmbientCubeDynamicStateVertexShader();
  655. if( !bHasFlashlight )
  656. {
  657. pShaderAPI->BindStandardTexture( SHADER_SAMPLER5, TEXTURE_NORMALIZATION_CUBEMAP_SIGNED );
  658. // Setting .x to 1 means to apply Fresnel to env map. Setting w to 1 means use separate selfillummask
  659. float vEnvMapFresnel_SelfIllumMask[4] = {0.0f, 0.0f, 0.0f, 0.0f};
  660. vEnvMapFresnel_SelfIllumMask[3] = bHasSelfIllumMask ? 1.0f : 0.0f;
  661. if( bHasEnvmap )
  662. {
  663. float vEnvMapTint_MaskControl[4] = {1.0f, 1.0f, 1.0f, 0.0f};
  664. // If we have a tint, grab it
  665. if ( (info.m_nEnvmapTint != -1) && params[info.m_nEnvmapTint]->IsDefined() )
  666. params[info.m_nEnvmapTint]->GetVecValue(vEnvMapTint_MaskControl, 3);
  667. // Set control for source of env map mask (normal alpha or base alpha)
  668. vEnvMapTint_MaskControl[3] = bHasNormalMapAlphaEnvmapMask ? 1.0f : 0.0f;
  669. if ( (info.m_nEnvmapFresnel != -1) && params[info.m_nEnvmapFresnel]->IsDefined() )
  670. vEnvMapFresnel_SelfIllumMask[0] = params[info.m_nEnvmapFresnel]->GetFloatValue();
  671. // Handle mat_fullbright 2 (diffuse lighting only with 50% gamma space basetexture)
  672. if( bLightingOnly )
  673. {
  674. vEnvMapTint_MaskControl[0] = vEnvMapTint_MaskControl[1] = vEnvMapTint_MaskControl[2] = 0.0f;
  675. }
  676. pShaderAPI->SetPixelShaderConstant( PSREG_ENVMAP_TINT__SHADOW_TWEAKS, vEnvMapTint_MaskControl, 1 );
  677. }
  678. pShaderAPI->SetPixelShaderConstant( PSREG_ENVMAP_FRESNEL__SELFILLUMMASK, vEnvMapFresnel_SelfIllumMask, 1 );
  679. }
  680. pShaderAPI->SetPixelShaderStateAmbientLightCube( PSREG_AMBIENT_CUBE, !lightState.m_bAmbientLight ); // Force to black if not bAmbientLight
  681. pShaderAPI->CommitPixelShaderLighting( PSREG_LIGHT_INFO_ARRAY );
  682. // Pack Phong exponent in with the eye position
  683. float vEyePos_SpecExponent[4], vFresnelRanges_SpecBoost[4] = {1, 0.5, 1, 1}, vRimBoost[4] = {1, 1, 1, 1};
  684. float vSpecularTint[4] = {1, 1, 1, 4};
  685. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  686. // If we have a phong exponent factor, then use that as a multiplier against the texture.
  687. if ( bHasPhongExponentFactor )
  688. {
  689. vEyePos_SpecExponent[3] = flPhongExponentFactor;
  690. }
  691. else
  692. {
  693. // Use the alpha channel of the normal map for the exponent by default
  694. vEyePos_SpecExponent[3] = -1.f;
  695. if ( (info.m_nPhongExponent != -1) && params[info.m_nPhongExponent]->IsDefined() )
  696. {
  697. float fValue = params[info.m_nPhongExponent]->GetFloatValue();
  698. if ( fValue > 0.f )
  699. {
  700. // Nonzero value in material overrides map channel
  701. vEyePos_SpecExponent[3] = fValue;
  702. }
  703. }
  704. }
  705. // Get the tint parameter
  706. if ( (info.m_nPhongTint != -1) && params[info.m_nPhongTint]->IsDefined() )
  707. {
  708. params[info.m_nPhongTint]->GetVecValue(vSpecularTint, 3);
  709. }
  710. // Get the rim light power (goes in w of Phong tint)
  711. if ( bHasRimLight && (info.m_nRimLightPower != -1) && params[info.m_nRimLightPower]->IsDefined() )
  712. {
  713. vSpecularTint[3] = params[info.m_nRimLightPower]->GetFloatValue();
  714. vSpecularTint[3] = max(vSpecularTint[3], 1.0f); // Make sure this is at least 1
  715. }
  716. // Get the rim boost (goes in w of flashlight position)
  717. if ( bHasRimLight && (info.m_nRimLightBoost != -1) && params[info.m_nRimLightBoost]->IsDefined() )
  718. {
  719. vRimBoost[3] = params[info.m_nRimLightBoost]->GetFloatValue();
  720. }
  721. if ( !bHasFlashlight )
  722. {
  723. float vRimMaskControl[4] = {0, 0, 0, 0}; // Only x is relevant in shader code
  724. vRimMaskControl[0] = bHasRimMaskMap ? params[info.m_nRimMask]->GetFloatValue() : 0.0f;
  725. // Rim mask...if this is true, use alpha channel of spec exponent texture to mask the rim term
  726. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_ATTENUATION, vRimMaskControl, 1 );
  727. }
  728. // If it's all zeros, there was no constant tint in the vmt
  729. if ( (vSpecularTint[0] == 0.0f) && (vSpecularTint[1] == 0.0f) && (vSpecularTint[2] == 0.0f) )
  730. {
  731. if ( bHasPhongTintMap ) // If we have a map to use, tell the shader
  732. {
  733. vSpecularTint[0] = -1;
  734. }
  735. else // Otherwise, just tint with white
  736. {
  737. vSpecularTint[0] = 1.0f;
  738. vSpecularTint[1] = 1.0f;
  739. vSpecularTint[2] = 1.0f;
  740. }
  741. }
  742. // handle mat_fullbright 2 (diffuse lighting only)
  743. if( bLightingOnly )
  744. {
  745. // BASETEXTURE
  746. if( bHasSelfIllum && !bHasFlashlight )
  747. {
  748. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_GREY_ALPHA_ZERO );
  749. }
  750. else
  751. {
  752. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_GREY );
  753. }
  754. // DETAILTEXTURE
  755. if ( hasDetailTexture )
  756. {
  757. pShaderAPI->BindStandardTexture( SHADER_SAMPLER13, TEXTURE_GREY );
  758. }
  759. // turn off specularity
  760. vSpecularTint[0] = vSpecularTint[1] = vSpecularTint[2] = 0.0f;
  761. }
  762. if ( (info.m_nPhongFresnelRanges != -1) && params[info.m_nPhongFresnelRanges]->IsDefined() )
  763. {
  764. params[info.m_nPhongFresnelRanges]->GetVecValue( vFresnelRanges_SpecBoost, 3 ); // Grab optional Fresnel range parameters
  765. // Change fresnel range encoding from (min, mid, max) to ((mid-min)*2, mid, (max-mid)*2)
  766. vFresnelRanges_SpecBoost[0] = (vFresnelRanges_SpecBoost[1] - vFresnelRanges_SpecBoost[0]) * 2;
  767. vFresnelRanges_SpecBoost[2] = (vFresnelRanges_SpecBoost[2] - vFresnelRanges_SpecBoost[1]) * 2;
  768. }
  769. if ( (info.m_nPhongBoost != -1 ) && params[info.m_nPhongBoost]->IsDefined()) // Grab optional Phong boost param
  770. vFresnelRanges_SpecBoost[3] = params[info.m_nPhongBoost]->GetFloatValue();
  771. else
  772. vFresnelRanges_SpecBoost[3] = 1.0f;
  773. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  774. pShaderAPI->SetPixelShaderConstant( PSREG_FRESNEL_SPEC_PARAMS, vFresnelRanges_SpecBoost, 1 );
  775. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_POSITION_RIM_BOOST, vRimBoost, 1 ); // Rim boost in w on non-flashlight pass
  776. pShaderAPI->SetPixelShaderConstant( PSREG_SPEC_RIM_PARAMS, vSpecularTint, 1 );
  777. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  778. // flashlightfixme: put this in common code.
  779. if( bHasFlashlight )
  780. {
  781. VMatrix worldToTexture;
  782. float atten[4], pos[4], tweaks[4];
  783. const FlashlightState_t &flashlightState = pShaderAPI->GetFlashlightState( worldToTexture );
  784. SetFlashLightColorFromState( flashlightState, pShaderAPI, PSREG_FLASHLIGHT_COLOR );
  785. pShader->BindTexture( SHADER_SAMPLER6, flashlightState.m_pSpotlightTexture, flashlightState.m_nSpotlightTextureFrame );
  786. atten[0] = flashlightState.m_fConstantAtten; // Set the flashlight attenuation factors
  787. atten[1] = flashlightState.m_fLinearAtten;
  788. atten[2] = flashlightState.m_fQuadraticAtten;
  789. atten[3] = flashlightState.m_FarZ;
  790. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_ATTENUATION, atten, 1 );
  791. pos[0] = flashlightState.m_vecLightOrigin[0]; // Set the flashlight origin
  792. pos[1] = flashlightState.m_vecLightOrigin[1];
  793. pos[2] = flashlightState.m_vecLightOrigin[2];
  794. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_POSITION_RIM_BOOST, pos, 1 ); // steps on rim boost
  795. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_TO_WORLD_TEXTURE, worldToTexture.Base(), 4 );
  796. // Tweaks associated with a given flashlight
  797. tweaks[0] = ShadowFilterFromState( flashlightState );
  798. tweaks[1] = ShadowAttenFromState( flashlightState );
  799. pShader->HashShadow2DJitter( flashlightState.m_flShadowJitterSeed, &tweaks[2], &tweaks[3] );
  800. pShaderAPI->SetPixelShaderConstant( PSREG_ENVMAP_TINT__SHADOW_TWEAKS, tweaks, 1 );
  801. // Dimensions of screen, used for screen-space noise map sampling
  802. float vScreenScale[4] = {1280.0f / 32.0f, 720.0f / 32.0f, 0, 0};
  803. int nWidth, nHeight;
  804. pShaderAPI->GetBackBufferDimensions( nWidth, nHeight );
  805. vScreenScale[0] = (float) nWidth / 32.0f;
  806. vScreenScale[1] = (float) nHeight / 32.0f;
  807. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_SCREEN_SCALE, vScreenScale, 1 );
  808. if ( IsX360() )
  809. {
  810. pShaderAPI->SetBooleanPixelShaderConstant( 0, &flashlightState.m_nShadowQuality, 1 );
  811. }
  812. }
  813. }
  814. pShader->Draw();
  815. }
  816. //-----------------------------------------------------------------------------
  817. // Draws the shader
  818. //-----------------------------------------------------------------------------
  819. extern ConVar r_flashlight_version2;
  820. void DrawSkin_DX9( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow,
  821. VertexLitGeneric_DX9_Vars_t &info, VertexCompressionType_t vertexCompression,
  822. CBasePerMaterialContextData **pContextDataPtr )
  823. {
  824. bool bHasFlashlight = pShader->UsingFlashlight( params );
  825. if ( bHasFlashlight && ( IsX360() || r_flashlight_version2.GetInt() ) )
  826. {
  827. DrawSkin_DX9_Internal( pShader, params, pShaderAPI,
  828. pShaderShadow, false, info, vertexCompression, pContextDataPtr++ );
  829. if ( pShaderShadow )
  830. {
  831. pShader->SetInitialShadowState( );
  832. }
  833. }
  834. DrawSkin_DX9_Internal( pShader, params, pShaderAPI,
  835. pShaderShadow, bHasFlashlight, info, vertexCompression, pContextDataPtr );
  836. }