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.

1468 lines
60 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #include "BaseVSShader.h"
  9. #include "vertexlitgeneric_dx9_helper.h"
  10. #include "skin_dx9_helper.h"
  11. #include "VertexLit_and_unlit_Generic_vs20.inc"
  12. #include "VertexLit_and_unlit_Generic_bump_vs20.inc"
  13. #include "vertexlit_and_unlit_generic_ps20.inc"
  14. #include "vertexlit_and_unlit_generic_ps20b.inc"
  15. #include "vertexlit_and_unlit_generic_bump_ps20.inc"
  16. #include "vertexlit_and_unlit_generic_bump_ps20b.inc"
  17. #ifndef _X360
  18. #include "vertexlit_and_unlit_generic_vs30.inc"
  19. #include "vertexlit_and_unlit_generic_ps30.inc"
  20. #include "vertexlit_and_unlit_generic_bump_vs30.inc"
  21. #include "vertexlit_and_unlit_generic_bump_ps30.inc"
  22. #endif
  23. #include "commandbuilder.h"
  24. #include "convar.h"
  25. // memdbgon must be the last include file in a .cpp file!!!
  26. #include "tier0/memdbgon.h"
  27. static ConVar mat_fullbright( "mat_fullbright","0", FCVAR_CHEAT );
  28. static ConVar r_lightwarpidentity( "r_lightwarpidentity","0", FCVAR_CHEAT );
  29. static ConVar mat_luxels( "mat_luxels", "0", FCVAR_CHEAT );
  30. static inline bool WantsSkinShader( IMaterialVar** params, const VertexLitGeneric_DX9_Vars_t &info )
  31. {
  32. if ( info.m_nPhong == -1) // Don't use skin without Phong
  33. return false;
  34. if ( params[info.m_nPhong]->GetIntValue() == 0 ) // Don't use skin without Phong turned on
  35. return false;
  36. if ( ( info.m_nDiffuseWarpTexture != -1 ) && params[info.m_nDiffuseWarpTexture]->IsTexture() ) // If there's Phong and diffuse warp do skin
  37. return true;
  38. if ( ( info.m_nBaseMapAlphaPhongMask != -1 ) && params[info.m_nBaseMapAlphaPhongMask]->GetIntValue() != 1 )
  39. {
  40. if ( info.m_nBumpmap == -1 ) // Don't use without a bump map
  41. return false;
  42. if ( !params[info.m_nBumpmap]->IsTexture() ) // Don't use if the texture isn't specified
  43. return false;
  44. }
  45. return true;
  46. }
  47. int g_nSnapShots;
  48. //-----------------------------------------------------------------------------
  49. // Initialize shader parameters
  50. //-----------------------------------------------------------------------------
  51. void InitParamsVertexLitGeneric_DX9( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, bool bVertexLitGeneric, VertexLitGeneric_DX9_Vars_t &info )
  52. {
  53. InitIntParam( info.m_nPhong, params, 0 );
  54. InitFloatParam( info.m_nAlphaTestReference, params, 0.0f );
  55. InitIntParam( info.m_nVertexAlphaTest, params, 0 );
  56. InitIntParam( info.m_nFlashlightNoLambert, params, 0 );
  57. if ( info.m_nDetailTint != -1 && !params[info.m_nDetailTint]->IsDefined() )
  58. {
  59. params[info.m_nDetailTint]->SetVecValue( 1.0f, 1.0f, 1.0f );
  60. }
  61. if ( info.m_nEnvmapTint != -1 && !params[info.m_nEnvmapTint]->IsDefined() )
  62. {
  63. params[info.m_nEnvmapTint]->SetVecValue( 1.0f, 1.0f, 1.0f );
  64. }
  65. InitIntParam( info.m_nEnvmapFrame, params, 0 );
  66. InitIntParam( info.m_nBumpFrame, params, 0 );
  67. InitFloatParam( info.m_nDetailTextureBlendFactor, params, 1.0 );
  68. InitIntParam( info.m_nReceiveFlashlight, params, 0 );
  69. InitFloatParam( info.m_nDetailScale, params, 4.0f );
  70. if ( (info.m_nBlendTintByBaseAlpha != -1) && (!params[info.m_nBlendTintByBaseAlpha]->IsDefined()) )
  71. {
  72. params[info.m_nBlendTintByBaseAlpha]->SetIntValue( 0 );
  73. }
  74. InitFloatParam( info.m_nTintReplacesBaseColor, params, 0 );
  75. if ( (info.m_nSelfIllumTint != -1) && (!params[info.m_nSelfIllumTint]->IsDefined()) )
  76. {
  77. params[info.m_nSelfIllumTint]->SetVecValue( 1.0f, 1.0f, 1.0f );
  78. }
  79. if ( WantsSkinShader( params, info ) )
  80. {
  81. if ( !g_pHardwareConfig->SupportsPixelShaders_2_b() || !g_pConfig->UsePhong() )
  82. {
  83. params[info.m_nPhong]->SetIntValue( 0 );
  84. }
  85. else
  86. {
  87. InitParamsSkin_DX9( pShader, params, pMaterialName, info );
  88. return;
  89. }
  90. }
  91. // FLASHLIGHTFIXME: Do ShaderAPI::BindFlashlightTexture
  92. if ( info.m_nFlashlightTexture != -1 )
  93. {
  94. if ( g_pHardwareConfig->SupportsBorderColor() )
  95. {
  96. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight_border" );
  97. }
  98. else
  99. {
  100. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight001" );
  101. }
  102. }
  103. // Write over $basetexture with $info.m_nBumpmap if we are going to be using diffuse normal mapping.
  104. if ( info.m_nAlbedo != -1 && g_pConfig->UseBumpmapping() && info.m_nBumpmap != -1 && params[info.m_nBumpmap]->IsDefined() && params[info.m_nAlbedo]->IsDefined() &&
  105. params[info.m_nBaseTexture]->IsDefined() )
  106. {
  107. params[info.m_nBaseTexture]->SetStringValue( params[info.m_nAlbedo]->GetStringValue() );
  108. }
  109. // This shader can be used with hw skinning
  110. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  111. if ( bVertexLitGeneric )
  112. {
  113. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  114. }
  115. else
  116. {
  117. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  118. }
  119. InitIntParam( info.m_nEnvmapMaskFrame, params, 0 );
  120. InitFloatParam( info.m_nEnvmapContrast, params, 0.0 );
  121. InitFloatParam( info.m_nEnvmapSaturation, params, 1.0f );
  122. InitFloatParam( info.m_nSeamlessScale, params, 0.0 );
  123. // handle line art parms
  124. InitFloatParam( info.m_nEdgeSoftnessStart, params, 0.5 );
  125. InitFloatParam( info.m_nEdgeSoftnessEnd, params, 0.5 );
  126. InitFloatParam( info.m_nGlowAlpha, params, 1.0 );
  127. InitFloatParam( info.m_nOutlineAlpha, params, 1.0 );
  128. // No texture means no self-illum or env mask in base alpha
  129. if ( info.m_nBaseTexture != -1 && !params[info.m_nBaseTexture]->IsDefined() )
  130. {
  131. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  132. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  133. }
  134. // If in decal mode, no debug override...
  135. if (IS_FLAG_SET(MATERIAL_VAR_DECAL))
  136. {
  137. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  138. }
  139. if( ( (info.m_nBumpmap != -1) && g_pConfig->UseBumpmapping() && params[info.m_nBumpmap]->IsDefined() )
  140. // we don't need a tangent space if we have envmap without bumpmap
  141. // || ( info.m_nEnvmap != -1 && params[info.m_nEnvmap]->IsDefined() )
  142. )
  143. {
  144. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  145. }
  146. else if ( (info.m_nDiffuseWarpTexture != -1) && params[info.m_nDiffuseWarpTexture]->IsDefined() ) // diffuse warp goes down bump path...
  147. {
  148. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  149. }
  150. else // no tangent space needed
  151. {
  152. CLEAR_FLAGS( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );
  153. }
  154. bool hasNormalMapAlphaEnvmapMask = IS_FLAG_SET( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );
  155. if ( hasNormalMapAlphaEnvmapMask )
  156. {
  157. params[info.m_nEnvmapMask]->SetUndefined();
  158. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  159. }
  160. if ( IS_FLAG_SET( MATERIAL_VAR_BASEALPHAENVMAPMASK ) && info.m_nBumpmap != -1 &&
  161. params[info.m_nBumpmap]->IsDefined() && !hasNormalMapAlphaEnvmapMask )
  162. {
  163. Warning( "material %s has a normal map and $basealphaenvmapmask. Must use $normalmapalphaenvmapmask to get specular.\n\n", pMaterialName );
  164. params[info.m_nEnvmap]->SetUndefined();
  165. }
  166. if ( info.m_nEnvmapMask != -1 && params[info.m_nEnvmapMask]->IsDefined() && info.m_nBumpmap != -1 && params[info.m_nBumpmap]->IsDefined() )
  167. {
  168. params[info.m_nEnvmapMask]->SetUndefined();
  169. if ( !hasNormalMapAlphaEnvmapMask )
  170. {
  171. Warning( "material %s has a normal map and an envmapmask. Must use $normalmapalphaenvmapmask.\n\n", pMaterialName );
  172. params[info.m_nEnvmap]->SetUndefined();
  173. }
  174. }
  175. // If mat_specular 0, then get rid of envmap
  176. if ( !g_pConfig->UseSpecular() && info.m_nEnvmap != -1 && params[info.m_nEnvmap]->IsDefined() && params[info.m_nBaseTexture]->IsDefined() )
  177. {
  178. params[info.m_nEnvmap]->SetUndefined();
  179. }
  180. InitFloatParam( info.m_nHDRColorScale, params, 1.0f );
  181. InitIntParam( info.m_nLinearWrite, params, 0 );
  182. InitIntParam( info.m_nGammaColorRead, params, 0 );
  183. InitIntParam( info.m_nDepthBlend, params, 0 );
  184. InitFloatParam( info.m_nDepthBlendScale, params, 50.0f );
  185. }
  186. //-----------------------------------------------------------------------------
  187. // Initialize shader
  188. //-----------------------------------------------------------------------------
  189. void InitVertexLitGeneric_DX9( CBaseVSShader *pShader, IMaterialVar** params, bool bVertexLitGeneric, VertexLitGeneric_DX9_Vars_t &info )
  190. {
  191. // both detailed and bumped = needs skin shader (for now)
  192. bool bNeedsSkinBecauseOfDetail = false;
  193. //bool bHasBump = ( info.m_nBumpmap != -1 ) && params[info.m_nBumpmap]->IsTexture();
  194. //if ( bHasBump )
  195. //{
  196. // if ( ( info.m_nDetail != -1 ) && params[info.m_nDetail]->IsDefined() )
  197. // bNeedsSkinBecauseOfDetail = true;
  198. //}
  199. if ( bNeedsSkinBecauseOfDetail ||
  200. ( info.m_nPhong != -1 &&
  201. params[info.m_nPhong]->GetIntValue() &&
  202. g_pHardwareConfig->SupportsPixelShaders_2_b() ) )
  203. {
  204. InitSkin_DX9( pShader, params, info );
  205. return;
  206. }
  207. if ( info.m_nFlashlightTexture != -1 )
  208. {
  209. pShader->LoadTexture( info.m_nFlashlightTexture, TEXTUREFLAGS_SRGB );
  210. }
  211. bool bIsBaseTextureTranslucent = false;
  212. if ( info.m_nBaseTexture != -1 && params[info.m_nBaseTexture]->IsDefined() )
  213. {
  214. pShader->LoadTexture( info.m_nBaseTexture, ( info.m_nGammaColorRead != -1 ) && ( params[info.m_nGammaColorRead]->GetIntValue() == 1 ) ? 0 : TEXTUREFLAGS_SRGB );
  215. if ( params[info.m_nBaseTexture]->GetTextureValue()->IsTranslucent() )
  216. {
  217. bIsBaseTextureTranslucent = true;
  218. }
  219. }
  220. bool bHasSelfIllumMask = IS_FLAG_SET( MATERIAL_VAR_SELFILLUM ) && (info.m_nSelfIllumMask != -1) && params[info.m_nSelfIllumMask]->IsDefined();
  221. // No alpha channel in any of the textures? No self illum or envmapmask
  222. if ( !bIsBaseTextureTranslucent )
  223. {
  224. bool bHasSelfIllumFresnel = IS_FLAG_SET( MATERIAL_VAR_SELFILLUM ) && ( info.m_nSelfIllumFresnel != -1 ) && ( params[info.m_nSelfIllumFresnel]->GetIntValue() != 0 );
  225. // Can still be self illum with no base alpha if using one of these alternate modes
  226. if ( !bHasSelfIllumFresnel && !bHasSelfIllumMask )
  227. {
  228. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  229. }
  230. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  231. }
  232. if ( info.m_nDetail != -1 && params[info.m_nDetail]->IsDefined() )
  233. {
  234. int nDetailBlendMode = ( info.m_nDetailTextureCombineMode == -1 ) ? 0 : params[info.m_nDetailTextureCombineMode]->GetIntValue();
  235. if ( nDetailBlendMode == 0 ) //Mod2X
  236. pShader->LoadTexture( info.m_nDetail );
  237. else
  238. pShader->LoadTexture( info.m_nDetail, TEXTUREFLAGS_SRGB );
  239. }
  240. if ( g_pConfig->UseBumpmapping() )
  241. {
  242. if ( (info.m_nBumpmap != -1) && params[info.m_nBumpmap]->IsDefined() )
  243. {
  244. pShader->LoadBumpMap( info.m_nBumpmap );
  245. SET_FLAGS2( MATERIAL_VAR2_DIFFUSE_BUMPMAPPED_MODEL );
  246. }
  247. else if ( (info.m_nDiffuseWarpTexture != -1) && params[info.m_nDiffuseWarpTexture]->IsDefined() )
  248. {
  249. SET_FLAGS2( MATERIAL_VAR2_DIFFUSE_BUMPMAPPED_MODEL );
  250. }
  251. }
  252. // Don't alpha test if the alpha channel is used for other purposes
  253. if ( IS_FLAG_SET(MATERIAL_VAR_SELFILLUM) || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  254. {
  255. CLEAR_FLAGS( MATERIAL_VAR_ALPHATEST );
  256. }
  257. if ( info.m_nEnvmap != -1 && params[info.m_nEnvmap]->IsDefined() )
  258. {
  259. if ( !IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) )
  260. {
  261. pShader->LoadCubeMap( info.m_nEnvmap, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE ? TEXTUREFLAGS_SRGB : 0 );
  262. }
  263. else
  264. {
  265. pShader->LoadTexture( info.m_nEnvmap, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE ? TEXTUREFLAGS_SRGB : 0 );
  266. }
  267. if ( !g_pHardwareConfig->SupportsCubeMaps() )
  268. {
  269. SET_FLAGS( MATERIAL_VAR_ENVMAPSPHERE );
  270. }
  271. }
  272. if ( info.m_nEnvmapMask != -1 && params[info.m_nEnvmapMask]->IsDefined() )
  273. {
  274. pShader->LoadTexture( info.m_nEnvmapMask );
  275. }
  276. if ( (info.m_nDiffuseWarpTexture != -1) && params[info.m_nDiffuseWarpTexture]->IsDefined() )
  277. {
  278. pShader->LoadTexture( info.m_nDiffuseWarpTexture );
  279. }
  280. if ( bHasSelfIllumMask )
  281. {
  282. pShader->LoadTexture( info.m_nSelfIllumMask );
  283. }
  284. }
  285. class CVertexLitGeneric_DX9_Context : public CBasePerMaterialContextData
  286. {
  287. public:
  288. CCommandBufferBuilder< CFixedCommandStorageBuffer< 800 > > m_SemiStaticCmdsOut;
  289. };
  290. //-----------------------------------------------------------------------------
  291. // Draws the shader
  292. //-----------------------------------------------------------------------------
  293. static void DrawVertexLitGeneric_DX9_Internal( CBaseVSShader *pShader, IMaterialVar** params,
  294. IShaderDynamicAPI *pShaderAPI,
  295. IShaderShadow* pShaderShadow,
  296. bool bVertexLitGeneric, bool bHasFlashlight,
  297. VertexLitGeneric_DX9_Vars_t &info,
  298. VertexCompressionType_t vertexCompression,
  299. CBasePerMaterialContextData **pContextDataPtr )
  300. {
  301. CVertexLitGeneric_DX9_Context *pContextData = reinterpret_cast< CVertexLitGeneric_DX9_Context *> ( *pContextDataPtr );
  302. /*^*/ // printf("\t\t>DrawVertexLitGeneric_DX9_Internal\n");
  303. bool bHasBump = IsTextureSet( info.m_nBumpmap, params );
  304. #if !defined( _X360 )
  305. bool bIsDecal = IS_FLAG_SET( MATERIAL_VAR_DECAL );
  306. #endif
  307. bool hasDiffuseLighting = bVertexLitGeneric;
  308. /*^*/ // printf("\t\t[%d] bVertexLitGeneric\n",(int)bVertexLitGeneric);
  309. if ( IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) )
  310. {
  311. bHasFlashlight = false;
  312. }
  313. bool bIsAlphaTested = IS_FLAG_SET( MATERIAL_VAR_ALPHATEST ) != 0;
  314. bool bHasDiffuseWarp = (!bHasFlashlight || IsX360() ) && hasDiffuseLighting && (info.m_nDiffuseWarpTexture != -1) && params[info.m_nDiffuseWarpTexture]->IsTexture();
  315. bool bHasLightmapTexture = IsTextureSet( info.m_nLightmap, params );
  316. bool bHasMatLuxel = bHasLightmapTexture && mat_luxels.GetBool();
  317. //bool bNoCull = IS_FLAG_SET( MATERIAL_VAR_NOCULL );
  318. bool bFlashlightNoLambert = false;
  319. if ( ( info.m_nFlashlightNoLambert != -1 ) && params[info.m_nFlashlightNoLambert]->GetIntValue() )
  320. {
  321. bFlashlightNoLambert = true;
  322. }
  323. bool bAmbientOnly = IsBoolSet( info.m_nAmbientOnly, params );
  324. float fBlendFactor = GetFloatParam( info.m_nDetailTextureBlendFactor, params, 1.0 );
  325. bool bHasDetailTexture = IsTextureSet( info.m_nDetail, params );
  326. int nDetailBlendMode = bHasDetailTexture ? GetIntParam( info.m_nDetailTextureCombineMode, params ) : 0;
  327. int nDetailTranslucencyTexture = -1;
  328. if ( bHasDetailTexture )
  329. {
  330. if ( ( nDetailBlendMode == 6 ) && ( ! (g_pHardwareConfig->SupportsPixelShaders_2_b() ) ) )
  331. {
  332. nDetailBlendMode = 5; // skip fancy threshold blending if ps2.0
  333. }
  334. if ( ( nDetailBlendMode == 3 ) || ( nDetailBlendMode == 8 ) || ( nDetailBlendMode == 9 ) )
  335. nDetailTranslucencyTexture = info.m_nDetail;
  336. }
  337. bool bBlendTintByBaseAlpha = IsBoolSet( info.m_nBlendTintByBaseAlpha, params );
  338. float fTintReplaceFactor = GetFloatParam( info.m_nTintReplacesBaseColor, params, 0.0 );
  339. BlendType_t nBlendType;
  340. bool bHasBaseTexture = IsTextureSet( info.m_nBaseTexture, params );
  341. if ( bHasBaseTexture )
  342. {
  343. // if base alpha is used for tinting, ignore the base texture for computing translucency
  344. nBlendType = pShader->EvaluateBlendRequirements( bBlendTintByBaseAlpha ? -1 : info.m_nBaseTexture, true, nDetailTranslucencyTexture );
  345. }
  346. else
  347. {
  348. nBlendType = pShader->EvaluateBlendRequirements( info.m_nEnvmapMask, false );
  349. }
  350. bool bFullyOpaque = (nBlendType != BT_BLENDADD) && (nBlendType != BT_BLEND) && !bIsAlphaTested && (!bHasFlashlight || IsX360() ); //dest alpha is free for special use
  351. bool bHasEnvmap = (!bHasFlashlight || IsX360() ) && info.m_nEnvmap != -1 && params[info.m_nEnvmap]->IsTexture();
  352. bool bHasVertexColor = bVertexLitGeneric ? false : IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR );
  353. bool bHasVertexAlpha = bVertexLitGeneric ? false : IS_FLAG_SET( MATERIAL_VAR_VERTEXALPHA );
  354. /*^*/ // printf("\t\t[%d] bHasVertexColor\n",(int)bHasVertexColor);
  355. /*^*/ // printf("\t\t[%d] bHasVertexAlpha\n",(int)bHasVertexAlpha);
  356. if ( pShader->IsSnapshotting() || (! pContextData ) || ( pContextData->m_bMaterialVarsChanged ) )
  357. {
  358. /*^*/ // printf("\t\t[1] snapshotting=%d pContextData=%08x pContextData->m_bMaterialVarsChanged=%d \n",(int)pShader->IsSnapshotting(), (int)pContextData, pContextData ? (int)pContextData->m_bMaterialVarsChanged : -1 );
  359. bool bSeamlessBase = IsBoolSet( info.m_nSeamlessBase, params );
  360. bool bSeamlessDetail = IsBoolSet( info.m_nSeamlessDetail, params );
  361. bool bDistanceAlpha = IsBoolSet( info.m_nDistanceAlpha, params );
  362. bool bHasSelfIllum = (!bHasFlashlight || IsX360() ) && IS_FLAG_SET( MATERIAL_VAR_SELFILLUM );
  363. bool bHasEnvmapMask = (!bHasFlashlight || IsX360() ) && info.m_nEnvmapMask != -1 && params[info.m_nEnvmapMask]->IsTexture();
  364. bool bHasSelfIllumFresnel = ( !IsTextureSet( info.m_nDetail, params ) ) && ( bHasSelfIllum ) && ( info.m_nSelfIllumFresnel != -1 ) && ( params[info.m_nSelfIllumFresnel]->GetIntValue() != 0 );
  365. bool bHasSelfIllumMask = bHasSelfIllum && IsTextureSet( info.m_nSelfIllumMask, params );
  366. bool hasSelfIllumInEnvMapMask =
  367. ( info.m_nSelfIllumEnvMapMask_Alpha != -1 ) &&
  368. ( params[info.m_nSelfIllumEnvMapMask_Alpha]->GetFloatValue() != 0.0 ) ;
  369. if ( pShader->IsSnapshotting() )
  370. {
  371. /*^*/ // printf("\t\t[2] snapshotting...\n");
  372. bool hasBaseAlphaEnvmapMask = IS_FLAG_SET( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  373. bool hasNormalMapAlphaEnvmapMask = IS_FLAG_SET( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );
  374. if ( info.m_nVertexAlphaTest != -1 && params[info.m_nVertexAlphaTest]->GetIntValue() > 0 )
  375. {
  376. bHasVertexAlpha = true;
  377. }
  378. // look at color and alphamod stuff.
  379. // Unlit generic never uses the flashlight
  380. if ( bHasSelfIllumFresnel )
  381. {
  382. CLEAR_FLAGS( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );
  383. hasNormalMapAlphaEnvmapMask = false;
  384. }
  385. bool bHasEnvmap = (!bHasFlashlight || IsX360() ) && ( info.m_nEnvmap != -1 ) && params[info.m_nEnvmap]->IsTexture();
  386. bool bHasLegacyEnvSphereMap = bHasEnvmap && IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE);
  387. bool bHasNormal = bVertexLitGeneric || bHasEnvmap || bHasFlashlight || bSeamlessBase || bSeamlessDetail;
  388. if ( IsPC() )
  389. {
  390. // On PC, LIGHTING_PREVIEW requires normals (they won't use much memory - unlitgeneric isn't used on many models)
  391. bHasNormal = true;
  392. }
  393. bool bHalfLambert = IS_FLAG_SET( MATERIAL_VAR_HALFLAMBERT );
  394. // Alpha test: FIXME: shouldn't this be handled in CBaseVSShader::SetInitialShadowState
  395. pShaderShadow->EnableAlphaTest( bIsAlphaTested );
  396. if ( info.m_nAlphaTestReference != -1 && params[info.m_nAlphaTestReference]->GetFloatValue() > 0.0f )
  397. {
  398. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, params[info.m_nAlphaTestReference]->GetFloatValue() );
  399. }
  400. int nShadowFilterMode = 0;
  401. if ( bHasFlashlight )
  402. {
  403. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  404. {
  405. nShadowFilterMode = g_pHardwareConfig->GetShadowFilterMode(); // Based upon vendor and device dependent formats
  406. }
  407. if ( !IsX360() )
  408. {
  409. if (params[info.m_nBaseTexture]->IsTexture())
  410. {
  411. pShader->SetAdditiveBlendingShadowState( info.m_nBaseTexture, true );
  412. }
  413. else
  414. {
  415. pShader->SetAdditiveBlendingShadowState( info.m_nEnvmapMask, false );
  416. }
  417. if ( bIsAlphaTested )
  418. {
  419. // disable alpha test and use the zfunc zequals since alpha isn't guaranteed to
  420. // be the same on both the regular pass and the flashlight pass.
  421. pShaderShadow->EnableAlphaTest( false );
  422. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_EQUAL );
  423. }
  424. // Be sure not to write to dest alpha
  425. pShaderShadow->EnableAlphaWrites( false );
  426. pShaderShadow->EnableBlending( true );
  427. pShaderShadow->EnableDepthWrites( false );
  428. }
  429. else
  430. {
  431. pShader->SetBlendingShadowState( nBlendType );
  432. }
  433. }
  434. else
  435. {
  436. pShader->SetBlendingShadowState( nBlendType );
  437. }
  438. unsigned int flags = VERTEX_POSITION;
  439. if ( bHasNormal )
  440. {
  441. flags |= VERTEX_NORMAL;
  442. }
  443. /*^*/ // printf("\t\t[%1d] VERTEX_NORMAL\n",(flags&VERTEX_NORMAL)!=0);
  444. int userDataSize = 0;
  445. bool bSRGBInputAdapter = false;
  446. // basetexture
  447. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  448. if ( bHasBaseTexture )
  449. {
  450. if ( ( info.m_nGammaColorRead != -1 ) && ( params[info.m_nGammaColorRead]->GetIntValue() == 1 ) )
  451. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, false );
  452. else
  453. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  454. // If we're on OSX GL on a crappy OS which can't do sRGB from render targets, check to see if we're reading from one...
  455. if ( IsOSX() && !g_pHardwareConfig->CanDoSRGBReadFromRTs() )
  456. {
  457. ITexture *pBaseTexture = params[info.m_nBaseTexture]->GetTextureValue();
  458. if ( pBaseTexture && pBaseTexture->IsRenderTarget() )
  459. {
  460. bSRGBInputAdapter = true;
  461. }
  462. }
  463. }
  464. if ( bHasEnvmap )
  465. {
  466. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  467. if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
  468. {
  469. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  470. }
  471. }
  472. if ( bHasFlashlight )
  473. {
  474. pShaderShadow->EnableTexture( SHADER_SAMPLER8, true ); // Depth texture
  475. pShaderShadow->SetShadowDepthFiltering( SHADER_SAMPLER8 );
  476. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true ); // Noise map
  477. pShaderShadow->EnableTexture( SHADER_SAMPLER7, true ); // Flashlight cookie
  478. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER7, true );
  479. userDataSize = 4; // tangent S
  480. }
  481. if ( bHasDetailTexture )
  482. {
  483. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  484. if ( nDetailBlendMode != 0 ) //Not Mod2X
  485. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true );
  486. }
  487. if ( bHasBump || bHasDiffuseWarp )
  488. {
  489. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  490. userDataSize = 4; // tangent S
  491. // Normalizing cube map
  492. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true );
  493. }
  494. if ( bHasEnvmapMask )
  495. {
  496. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  497. }
  498. if ( bHasVertexColor || bHasVertexAlpha )
  499. {
  500. flags |= VERTEX_COLOR;
  501. }
  502. /*^*/ // printf("\t\t[%1d] VERTEX_COLOR\n",(flags&VERTEX_COLOR)!=0);
  503. /*^*/ // printf("\t\t[%1d] VERTEX_COLOR_STREAM_1\n",(flags&VERTEX_COLOR_STREAM_1)!=0);
  504. if( bHasDiffuseWarp && (!bHasFlashlight || IsX360() ) && !bHasSelfIllumFresnel )
  505. {
  506. pShaderShadow->EnableTexture( SHADER_SAMPLER9, true ); // Diffuse warp texture
  507. }
  508. if ( (info.m_nDepthBlend != -1) && (params[info.m_nDepthBlend]->GetIntValue()) )
  509. {
  510. if( bHasBump )
  511. Warning( "DEPTHBLEND not supported by bump mapped variations of vertexlitgeneric to avoid shader bloat. Either remove the bump map or convince a graphics programmer that it's worth it.\n" );
  512. pShaderShadow->EnableTexture( SHADER_SAMPLER10, true );
  513. }
  514. if( bHasSelfIllum )
  515. {
  516. pShaderShadow->EnableTexture( SHADER_SAMPLER11, true ); // self illum mask
  517. }
  518. // Always enable this sampler, used for lightmaps depending on the dynamic combo.
  519. // Lightmaps are generated in gamma space, but not sRGB, so leave that disabled. Conversion is done in the shader.
  520. pShaderShadow->EnableTexture( SHADER_SAMPLER12, true );
  521. bool bSRGBWrite = true;
  522. if( (info.m_nLinearWrite != -1) && (params[info.m_nLinearWrite]->GetIntValue() == 1) )
  523. {
  524. bSRGBWrite = false;
  525. }
  526. pShaderShadow->EnableSRGBWrite( bSRGBWrite );
  527. // texcoord0 : base texcoord
  528. int pTexCoordDim[3] = { 2, 2, 3 };
  529. int nTexCoordCount = 1;
  530. if ( IsBoolSet( info.m_nSeparateDetailUVs, params ) )
  531. {
  532. ++nTexCoordCount;
  533. }
  534. else
  535. {
  536. pTexCoordDim[1] = 0;
  537. }
  538. #ifndef _X360
  539. // Special morphed decal information
  540. if ( bIsDecal && g_pHardwareConfig->HasFastVertexTextures() )
  541. {
  542. nTexCoordCount = 3;
  543. }
  544. #endif
  545. // This shader supports compressed vertices, so OR in that flag:
  546. flags |= VERTEX_FORMAT_COMPRESSED;
  547. /*^*/ // printf("\t\t[%1d] VERTEX_FORMAT_COMPRESSED\n",(flags&VERTEX_FORMAT_COMPRESSED)!=0);
  548. /*^*/ // printf("\t\t -> CShaderShadowDX8::VertexShaderVertexFormat( flags=%08x, texcount=%d )\n",flags,nTexCoordCount);
  549. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, pTexCoordDim, userDataSize );
  550. if ( bHasBump || bHasDiffuseWarp )
  551. {
  552. #ifndef _X360
  553. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  554. #endif
  555. {
  556. bool bUseStaticControlFlow = g_pHardwareConfig->SupportsStaticControlFlow();
  557. DECLARE_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_bump_vs20 );
  558. SET_STATIC_VERTEX_SHADER_COMBO( HALFLAMBERT, bHalfLambert);
  559. SET_STATIC_VERTEX_SHADER_COMBO( USE_WITH_2B, g_pHardwareConfig->SupportsPixelShaders_2_b() );
  560. #ifdef _X360
  561. SET_STATIC_VERTEX_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  562. #endif
  563. SET_STATIC_VERTEX_SHADER_COMBO( USE_STATIC_CONTROL_FLOW, bUseStaticControlFlow );
  564. SET_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_bump_vs20 );
  565. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send GL this way
  566. {
  567. DECLARE_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_bump_ps20b );
  568. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  569. SET_STATIC_PIXEL_SHADER_COMBO( DIFFUSELIGHTING, hasDiffuseLighting );
  570. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTWARPTEXTURE, bHasDiffuseWarp && !bHasSelfIllumFresnel );
  571. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, bHasSelfIllum );
  572. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUMFRESNEL, bHasSelfIllumFresnel );
  573. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMAPALPHAENVMAPMASK, hasNormalMapAlphaEnvmapMask && bHasEnvmap );
  574. SET_STATIC_PIXEL_SHADER_COMBO( HALFLAMBERT, bHalfLambert);
  575. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  576. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, bHasDetailTexture );
  577. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  578. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  579. SET_STATIC_PIXEL_SHADER_COMBO( BLENDTINTBYBASEALPHA, bBlendTintByBaseAlpha );
  580. SET_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_bump_ps20b );
  581. }
  582. else // ps_2_0
  583. {
  584. DECLARE_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_bump_ps20 );
  585. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  586. SET_STATIC_PIXEL_SHADER_COMBO( DIFFUSELIGHTING, hasDiffuseLighting );
  587. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTWARPTEXTURE, bHasDiffuseWarp && !bHasSelfIllumFresnel );
  588. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, bHasSelfIllum );
  589. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUMFRESNEL, bHasSelfIllumFresnel );
  590. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMAPALPHAENVMAPMASK, hasNormalMapAlphaEnvmapMask && bHasEnvmap );
  591. SET_STATIC_PIXEL_SHADER_COMBO( HALFLAMBERT, bHalfLambert);
  592. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  593. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, bHasDetailTexture );
  594. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  595. SET_STATIC_PIXEL_SHADER_COMBO( BLENDTINTBYBASEALPHA, bBlendTintByBaseAlpha );
  596. SET_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_bump_ps20 );
  597. }
  598. }
  599. #ifndef _X360
  600. else
  601. {
  602. // The vertex shader uses the vertex id stream
  603. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  604. DECLARE_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_bump_vs30 );
  605. SET_STATIC_VERTEX_SHADER_COMBO( HALFLAMBERT, bHalfLambert);
  606. SET_STATIC_VERTEX_SHADER_COMBO( USE_WITH_2B, true );
  607. SET_STATIC_VERTEX_SHADER_COMBO( DECAL, bIsDecal );
  608. SET_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_bump_vs30 );
  609. DECLARE_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_bump_ps30 );
  610. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  611. SET_STATIC_PIXEL_SHADER_COMBO( DIFFUSELIGHTING, hasDiffuseLighting );
  612. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTWARPTEXTURE, bHasDiffuseWarp && !bHasSelfIllumFresnel );
  613. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, bHasSelfIllum );
  614. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUMFRESNEL, bHasSelfIllumFresnel );
  615. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMAPALPHAENVMAPMASK, hasNormalMapAlphaEnvmapMask && bHasEnvmap );
  616. SET_STATIC_PIXEL_SHADER_COMBO( HALFLAMBERT, bHalfLambert);
  617. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  618. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, bHasDetailTexture );
  619. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  620. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  621. SET_STATIC_PIXEL_SHADER_COMBO( BLENDTINTBYBASEALPHA, bBlendTintByBaseAlpha );
  622. SET_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_bump_ps30 );
  623. }
  624. #endif
  625. }
  626. else // !(bHasBump || bHasDiffuseWarp)
  627. {
  628. bool bDistanceAlphaFromDetail = false;
  629. bool bSoftMask = false;
  630. bool bGlow = false;
  631. bool bOutline = false;
  632. static ConVarRef mat_reduceparticles( "mat_reduceparticles" );
  633. bool bDoDepthBlend = IsBoolSet( info.m_nDepthBlend, params ) && !mat_reduceparticles.GetBool();
  634. if ( bDistanceAlpha )
  635. {
  636. bDistanceAlphaFromDetail = IsBoolSet( info.m_nDistanceAlphaFromDetail, params );
  637. bSoftMask = IsBoolSet( info.m_nSoftEdges, params );
  638. bGlow = IsBoolSet( info.m_nGlow, params );
  639. bOutline = IsBoolSet( info.m_nOutline, params );
  640. }
  641. #ifndef _X360
  642. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  643. #endif
  644. {
  645. bool bUseStaticControlFlow = g_pHardwareConfig->SupportsStaticControlFlow();
  646. DECLARE_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs20 );
  647. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, bHasVertexColor || bHasVertexAlpha );
  648. SET_STATIC_VERTEX_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  649. SET_STATIC_VERTEX_SHADER_COMBO( HALFLAMBERT, bHalfLambert );
  650. SET_STATIC_VERTEX_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  651. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS_BASE, bSeamlessBase );
  652. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS_DETAIL, bSeamlessDetail );
  653. SET_STATIC_VERTEX_SHADER_COMBO( SEPARATE_DETAIL_UVS, IsBoolSet( info.m_nSeparateDetailUVs, params ) );
  654. SET_STATIC_VERTEX_SHADER_COMBO( USE_STATIC_CONTROL_FLOW, bUseStaticControlFlow );
  655. SET_STATIC_VERTEX_SHADER_COMBO( DONT_GAMMA_CONVERT_VERTEX_COLOR, (! bSRGBWrite ) && bHasVertexColor );
  656. SET_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs20 );
  657. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send Gl this way
  658. {
  659. DECLARE_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_ps20b );
  660. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM_ENVMAPMASK_ALPHA, ( hasSelfIllumInEnvMapMask && ( bHasEnvmapMask ) ) );
  661. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  662. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP_SPHERE_LEGACY, bHasLegacyEnvSphereMap );
  663. SET_STATIC_PIXEL_SHADER_COMBO( DIFFUSELIGHTING, hasDiffuseLighting );
  664. SET_STATIC_PIXEL_SHADER_COMBO( ENVMAPMASK, bHasEnvmapMask );
  665. SET_STATIC_PIXEL_SHADER_COMBO( BASEALPHAENVMAPMASK, hasBaseAlphaEnvmapMask );
  666. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, bHasSelfIllum );
  667. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXCOLOR, bHasVertexColor );
  668. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  669. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, bHasDetailTexture );
  670. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  671. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS_BASE, bSeamlessBase );
  672. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS_DETAIL, bSeamlessDetail );
  673. SET_STATIC_PIXEL_SHADER_COMBO( DISTANCEALPHA, bDistanceAlpha );
  674. SET_STATIC_PIXEL_SHADER_COMBO( DISTANCEALPHAFROMDETAIL, bDistanceAlphaFromDetail );
  675. SET_STATIC_PIXEL_SHADER_COMBO( SOFT_MASK, bSoftMask );
  676. SET_STATIC_PIXEL_SHADER_COMBO( OUTLINE, bOutline );
  677. SET_STATIC_PIXEL_SHADER_COMBO( OUTER_GLOW, bGlow );
  678. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  679. SET_STATIC_PIXEL_SHADER_COMBO( DEPTHBLEND, bDoDepthBlend );
  680. SET_STATIC_PIXEL_SHADER_COMBO( SRGB_INPUT_ADAPTER, bSRGBInputAdapter ? 1 : 0 );
  681. SET_STATIC_PIXEL_SHADER_COMBO( BLENDTINTBYBASEALPHA, bBlendTintByBaseAlpha );
  682. SET_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_ps20b );
  683. }
  684. else // ps_2_0
  685. {
  686. DECLARE_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_ps20 );
  687. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM_ENVMAPMASK_ALPHA, ( hasSelfIllumInEnvMapMask && ( bHasEnvmapMask ) ) );
  688. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  689. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP_SPHERE_LEGACY, bHasLegacyEnvSphereMap );
  690. SET_STATIC_PIXEL_SHADER_COMBO( DIFFUSELIGHTING, hasDiffuseLighting );
  691. SET_STATIC_PIXEL_SHADER_COMBO( ENVMAPMASK, bHasEnvmapMask );
  692. SET_STATIC_PIXEL_SHADER_COMBO( BASEALPHAENVMAPMASK, hasBaseAlphaEnvmapMask );
  693. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, bHasSelfIllum );
  694. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXCOLOR, bHasVertexColor );
  695. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  696. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, bHasDetailTexture );
  697. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  698. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS_BASE, bSeamlessBase );
  699. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS_DETAIL, bSeamlessDetail );
  700. SET_STATIC_PIXEL_SHADER_COMBO( DISTANCEALPHA, bDistanceAlpha );
  701. SET_STATIC_PIXEL_SHADER_COMBO( DISTANCEALPHAFROMDETAIL, bDistanceAlphaFromDetail );
  702. SET_STATIC_PIXEL_SHADER_COMBO( SOFT_MASK, bSoftMask );
  703. SET_STATIC_PIXEL_SHADER_COMBO( OUTLINE, bOutline );
  704. SET_STATIC_PIXEL_SHADER_COMBO( OUTER_GLOW, bGlow );
  705. SET_STATIC_PIXEL_SHADER_COMBO( BLENDTINTBYBASEALPHA, bBlendTintByBaseAlpha );
  706. SET_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_ps20 );
  707. }
  708. }
  709. #ifndef _X360
  710. else
  711. {
  712. // The vertex shader uses the vertex id stream
  713. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  714. DECLARE_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs30 );
  715. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, bHasVertexColor || bHasVertexAlpha );
  716. SET_STATIC_VERTEX_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  717. SET_STATIC_VERTEX_SHADER_COMBO( HALFLAMBERT, bHalfLambert );
  718. SET_STATIC_VERTEX_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  719. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS_BASE, bSeamlessBase );
  720. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS_DETAIL, bSeamlessDetail );
  721. SET_STATIC_VERTEX_SHADER_COMBO( SEPARATE_DETAIL_UVS, IsBoolSet( info.m_nSeparateDetailUVs, params ) );
  722. SET_STATIC_VERTEX_SHADER_COMBO( DECAL, bIsDecal );
  723. SET_STATIC_VERTEX_SHADER_COMBO( DONT_GAMMA_CONVERT_VERTEX_COLOR, bSRGBWrite ? 0 : 1 );
  724. SET_STATIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs30 );
  725. DECLARE_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_ps30 );
  726. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM_ENVMAPMASK_ALPHA, ( hasSelfIllumInEnvMapMask && ( bHasEnvmapMask ) ) );
  727. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  728. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP_SPHERE_LEGACY, bHasLegacyEnvSphereMap );
  729. SET_STATIC_PIXEL_SHADER_COMBO( DIFFUSELIGHTING, hasDiffuseLighting );
  730. SET_STATIC_PIXEL_SHADER_COMBO( ENVMAPMASK, bHasEnvmapMask );
  731. SET_STATIC_PIXEL_SHADER_COMBO( BASEALPHAENVMAPMASK, hasBaseAlphaEnvmapMask );
  732. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, bHasSelfIllum );
  733. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXCOLOR, bHasVertexColor );
  734. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  735. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, bHasDetailTexture );
  736. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  737. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS_BASE, bSeamlessBase );
  738. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS_DETAIL, bSeamlessDetail );
  739. SET_STATIC_PIXEL_SHADER_COMBO( DISTANCEALPHA, bDistanceAlpha );
  740. SET_STATIC_PIXEL_SHADER_COMBO( DISTANCEALPHAFROMDETAIL, bDistanceAlphaFromDetail );
  741. SET_STATIC_PIXEL_SHADER_COMBO( SOFT_MASK, bSoftMask );
  742. SET_STATIC_PIXEL_SHADER_COMBO( OUTLINE, bOutline );
  743. SET_STATIC_PIXEL_SHADER_COMBO( OUTER_GLOW, bGlow );
  744. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  745. SET_STATIC_PIXEL_SHADER_COMBO( DEPTHBLEND, bDoDepthBlend );
  746. SET_STATIC_PIXEL_SHADER_COMBO( BLENDTINTBYBASEALPHA, bBlendTintByBaseAlpha );
  747. SET_STATIC_PIXEL_SHADER( vertexlit_and_unlit_generic_ps30 );
  748. }
  749. #endif
  750. }
  751. if ( bHasFlashlight && !IsX360() )
  752. {
  753. pShader->FogToBlack();
  754. }
  755. else
  756. {
  757. pShader->DefaultFog();
  758. }
  759. // HACK HACK HACK - enable alpha writes all the time so that we have them for
  760. // underwater stuff and the loadout and character select screens.
  761. pShaderShadow->EnableAlphaWrites( bFullyOpaque );
  762. }
  763. if ( pShaderAPI && ( (! pContextData ) || ( pContextData->m_bMaterialVarsChanged ) ) )
  764. {
  765. /*^*/ // printf("\t\t[3] pShaderAPI && ( (! pContextData ) || ( pContextData->m_bMaterialVarsChanged ) ) TRUE \n");
  766. if ( ! pContextData ) // make sure allocated
  767. {
  768. ++g_nSnapShots;
  769. pContextData = new CVertexLitGeneric_DX9_Context;
  770. *pContextDataPtr = pContextData;
  771. }
  772. pContextData->m_SemiStaticCmdsOut.Reset();
  773. pContextData->m_SemiStaticCmdsOut.SetPixelShaderFogParams( 21 );
  774. if ( bHasBaseTexture )
  775. {
  776. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER0, info.m_nBaseTexture, info.m_nBaseTextureFrame );
  777. }
  778. else
  779. {
  780. if( bHasEnvmap )
  781. {
  782. // if we only have an envmap (no basetexture), then we want the albedo to be black.
  783. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER0, TEXTURE_BLACK );
  784. }
  785. else
  786. {
  787. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER0, TEXTURE_WHITE );
  788. }
  789. }
  790. if ( bHasDetailTexture )
  791. {
  792. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER2, info.m_nDetail, info.m_nDetailFrame );
  793. }
  794. if ( bHasSelfIllum )
  795. {
  796. if ( bHasSelfIllumMask ) // Separate texture for self illum?
  797. {
  798. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER11, info.m_nSelfIllumMask, -1 ); // Bind it
  799. }
  800. else // else
  801. {
  802. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER11, TEXTURE_BLACK ); // Bind dummy
  803. }
  804. }
  805. if ( (info.m_nDepthBlend != -1) && (params[info.m_nDepthBlend]->GetIntValue()) )
  806. {
  807. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER10, TEXTURE_FRAME_BUFFER_FULL_DEPTH );
  808. }
  809. if ( bSeamlessDetail || bSeamlessBase )
  810. {
  811. float flSeamlessData[4]={ params[info.m_nSeamlessScale]->GetFloatValue(),
  812. 0,0,0};
  813. pContextData->m_SemiStaticCmdsOut.SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, flSeamlessData );
  814. }
  815. if ( info.m_nBaseTextureTransform != -1 )
  816. {
  817. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nBaseTextureTransform );
  818. }
  819. if ( bHasDetailTexture )
  820. {
  821. if ( IS_PARAM_DEFINED( info.m_nDetailTextureTransform ) )
  822. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, info.m_nDetailTextureTransform, info.m_nDetailScale );
  823. else
  824. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, info.m_nBaseTextureTransform, info.m_nDetailScale );
  825. //Assert( !bHasBump );
  826. if ( info.m_nDetailTint != -1 )
  827. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstantGammaToLinear( 10, info.m_nDetailTint );
  828. else
  829. {
  830. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant4( 10, 1, 1, 1, 1 );
  831. }
  832. }
  833. if ( bDistanceAlpha )
  834. {
  835. float flSoftStart = GetFloatParam( info.m_nEdgeSoftnessStart, params );
  836. float flSoftEnd = GetFloatParam( info.m_nEdgeSoftnessEnd, params );
  837. // set all line art shader parms
  838. bool bScaleEdges = IsBoolSet( info.m_nScaleEdgeSoftnessBasedOnScreenRes, params );
  839. bool bScaleOutline = IsBoolSet( info.m_nScaleOutlineSoftnessBasedOnScreenRes, params );
  840. float flResScale = 1.0;
  841. float flOutlineStart0 = GetFloatParam( info.m_nOutlineStart0, params );
  842. float flOutlineStart1 = GetFloatParam( info.m_nOutlineStart1, params );
  843. float flOutlineEnd0 = GetFloatParam( info.m_nOutlineEnd0, params );
  844. float flOutlineEnd1 = GetFloatParam( info.m_nOutlineEnd1, params );
  845. if ( bScaleEdges || bScaleOutline )
  846. {
  847. int nWidth, nHeight;
  848. pShaderAPI->GetBackBufferDimensions( nWidth, nHeight );
  849. flResScale=max( 0.5f, max( 1024.f/nWidth, 768.f/nHeight ) );
  850. if ( bScaleEdges )
  851. {
  852. float flMid = 0.5 * ( flSoftStart + flSoftEnd );
  853. flSoftStart = clamp( flMid + flResScale * ( flSoftStart - flMid ), 0.05, 0.99 );
  854. flSoftEnd = clamp( flMid + flResScale * ( flSoftEnd - flMid ), 0.05, 0.99 );
  855. }
  856. if ( bScaleOutline )
  857. {
  858. // shrink the soft part of the outline, enlarging hard part
  859. float flMidS = 0.5 * ( flOutlineStart1 + flOutlineStart0 );
  860. flOutlineStart1 = clamp( flMidS + flResScale * ( flOutlineStart1 - flMidS ), 0.05, 0.99 );
  861. float flMidE = 0.5 * ( flOutlineEnd1 + flOutlineEnd0 );
  862. flOutlineEnd1 = clamp( flMidE + flResScale * ( flOutlineEnd1 - flMidE ), 0.05, 0.99 );
  863. }
  864. }
  865. float flConsts[]={
  866. // c5 - glow values
  867. GetFloatParam( info.m_nGlowX, params ),
  868. GetFloatParam( info.m_nGlowY, params ),
  869. GetFloatParam( info.m_nGlowStart, params ),
  870. GetFloatParam( info.m_nGlowEnd, params ),
  871. // c6 - glow color
  872. 0,0,0, // will be filled in
  873. GetFloatParam( info.m_nGlowAlpha, params ),
  874. // c7 - mask range parms
  875. flSoftStart,
  876. flSoftEnd,
  877. 0,0,
  878. // c8 - outline color
  879. 0,0,0,
  880. GetFloatParam( info.m_nOutlineAlpha, params ),
  881. // c9 - outline parms. ordered for optimal ps20 .wzyx swizzling
  882. flOutlineStart0,
  883. flOutlineEnd1,
  884. flOutlineEnd0,
  885. flOutlineStart1,
  886. };
  887. if ( info.m_nGlowColor != -1 )
  888. {
  889. params[info.m_nGlowColor]->GetVecValue( flConsts+4, 3 );
  890. }
  891. if ( info.m_nOutlineColor != -1 )
  892. {
  893. params[info.m_nOutlineColor]->GetVecValue( flConsts+12, 3 );
  894. }
  895. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 5, flConsts, 5 );
  896. }
  897. if ( !g_pConfig->m_bFastNoBump )
  898. {
  899. if ( bHasBump )
  900. {
  901. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER3, info.m_nBumpmap, info.m_nBumpFrame );
  902. }
  903. else if ( bHasDiffuseWarp )
  904. {
  905. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER3, TEXTURE_NORMALMAP_FLAT );
  906. }
  907. }
  908. else
  909. {
  910. if ( bHasBump )
  911. {
  912. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER3, TEXTURE_NORMALMAP_FLAT );
  913. }
  914. }
  915. // Setting w to 1 means use separate selfillummask
  916. float vEnvMapSaturation_SelfIllumMask[4] = {1.0f, 1.0f, 1.0f, 0.0f};
  917. if ( info.m_nEnvmapSaturation != -1 )
  918. params[info.m_nEnvmapSaturation]->GetVecValue( vEnvMapSaturation_SelfIllumMask, 3 );
  919. vEnvMapSaturation_SelfIllumMask[3] = bHasSelfIllumMask ? 1.0f : 0.0f;
  920. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 3, vEnvMapSaturation_SelfIllumMask, 1 );
  921. if ( bHasEnvmap )
  922. {
  923. pContextData->m_SemiStaticCmdsOut.SetEnvMapTintPixelShaderDynamicStateGammaToLinear( 0, info.m_nEnvmapTint, fTintReplaceFactor );
  924. }
  925. else
  926. {
  927. pContextData->m_SemiStaticCmdsOut.SetEnvMapTintPixelShaderDynamicStateGammaToLinear( 0, -1, fTintReplaceFactor);
  928. }
  929. if ( bHasEnvmapMask )
  930. {
  931. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER4, info.m_nEnvmapMask, info.m_nEnvmapMaskFrame );
  932. }
  933. if ( bHasSelfIllumFresnel && (!bHasFlashlight || IsX360() ) )
  934. {
  935. float vConstScaleBiasExp[4] = { 1.0f, 0.0f, 1.0f, 0.0f };
  936. float flMin = IS_PARAM_DEFINED( info.m_nSelfIllumFresnelMinMaxExp ) ? params[info.m_nSelfIllumFresnelMinMaxExp]->GetVecValue()[0] : 0.0f;
  937. float flMax = IS_PARAM_DEFINED( info.m_nSelfIllumFresnelMinMaxExp ) ? params[info.m_nSelfIllumFresnelMinMaxExp]->GetVecValue()[1] : 1.0f;
  938. float flExp = IS_PARAM_DEFINED( info.m_nSelfIllumFresnelMinMaxExp ) ? params[info.m_nSelfIllumFresnelMinMaxExp]->GetVecValue()[2] : 1.0f;
  939. vConstScaleBiasExp[1] = ( flMax != 0.0f ) ? ( flMin / flMax ) : 0.0f; // Bias
  940. vConstScaleBiasExp[0] = 1.0f - vConstScaleBiasExp[1]; // Scale
  941. vConstScaleBiasExp[2] = flExp; // Exp
  942. vConstScaleBiasExp[3] = flMax; // Brightness
  943. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 11, vConstScaleBiasExp );
  944. }
  945. if( bHasDiffuseWarp && (!bHasFlashlight || IsX360() ) && !bHasSelfIllumFresnel )
  946. {
  947. if ( r_lightwarpidentity.GetBool() )
  948. {
  949. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER9, TEXTURE_IDENTITY_LIGHTWARP );
  950. }
  951. else
  952. {
  953. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER9, info.m_nDiffuseWarpTexture, -1 );
  954. }
  955. }
  956. if ( bHasFlashlight )
  957. {
  958. // Tweaks associated with a given flashlight
  959. VMatrix worldToTexture;
  960. const FlashlightState_t &flashlightState = pShaderAPI->GetFlashlightState( worldToTexture );
  961. float tweaks[4];
  962. tweaks[0] = flashlightState.m_flShadowFilterSize / flashlightState.m_flShadowMapResolution;
  963. tweaks[1] = ShadowAttenFromState( flashlightState );
  964. pShader->HashShadow2DJitter( flashlightState.m_flShadowJitterSeed, &tweaks[2], &tweaks[3] );
  965. pShaderAPI->SetPixelShaderConstant( 2, tweaks, 1 );
  966. // Dimensions of screen, used for screen-space noise map sampling
  967. float vScreenScale[4] = {1280.0f / 32.0f, 720.0f / 32.0f, 0, 0};
  968. int nWidth, nHeight;
  969. pShaderAPI->GetBackBufferDimensions( nWidth, nHeight );
  970. vScreenScale[0] = (float) nWidth / 32.0f;
  971. vScreenScale[1] = (float) nHeight / 32.0f;
  972. pShaderAPI->SetPixelShaderConstant( 31, vScreenScale, 1 );
  973. }
  974. if ( ( !bHasFlashlight || IsX360() ) && ( info.m_nEnvmapContrast != -1 ) )
  975. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 2, info.m_nEnvmapContrast );
  976. // mat_fullbright 2 handling
  977. bool bLightingOnly = bVertexLitGeneric && mat_fullbright.GetInt() == 2 && !IS_FLAG_SET( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  978. if( bLightingOnly )
  979. {
  980. if ( bHasBaseTexture )
  981. {
  982. if( ( bHasSelfIllum && !hasSelfIllumInEnvMapMask ) )
  983. {
  984. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER0, TEXTURE_GREY_ALPHA_ZERO );
  985. }
  986. else
  987. {
  988. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER0, TEXTURE_GREY );
  989. }
  990. }
  991. if ( bHasDetailTexture )
  992. {
  993. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER2, TEXTURE_GREY );
  994. }
  995. }
  996. if ( bHasBump || bHasDiffuseWarp )
  997. {
  998. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER5, TEXTURE_NORMALIZATION_CUBEMAP_SIGNED );
  999. pContextData->m_SemiStaticCmdsOut.SetPixelShaderStateAmbientLightCube( 5 );
  1000. pContextData->m_SemiStaticCmdsOut.CommitPixelShaderLighting( 13 );
  1001. }
  1002. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant_W( 4, info.m_nSelfIllumTint, fBlendFactor );
  1003. pContextData->m_SemiStaticCmdsOut.SetAmbientCubeDynamicStateVertexShader();
  1004. pContextData->m_SemiStaticCmdsOut.End();
  1005. }
  1006. }
  1007. if ( pShaderAPI )
  1008. {
  1009. CCommandBufferBuilder< CFixedCommandStorageBuffer< 1000 > > DynamicCmdsOut;
  1010. DynamicCmdsOut.Call( pContextData->m_SemiStaticCmdsOut.Base() );
  1011. if ( bHasEnvmap )
  1012. {
  1013. DynamicCmdsOut.BindTexture( pShader, SHADER_SAMPLER1, info.m_nEnvmap, info.m_nEnvmapFrame );
  1014. }
  1015. bool bFlashlightShadows = false;
  1016. if ( bHasFlashlight )
  1017. {
  1018. VMatrix worldToTexture;
  1019. ITexture *pFlashlightDepthTexture;
  1020. FlashlightState_t state = pShaderAPI->GetFlashlightStateEx( worldToTexture, &pFlashlightDepthTexture );
  1021. bFlashlightShadows = state.m_bEnableShadows && ( pFlashlightDepthTexture != NULL );
  1022. if( pFlashlightDepthTexture && g_pConfig->ShadowDepthTexture() && state.m_bEnableShadows )
  1023. {
  1024. pShader->BindTexture( SHADER_SAMPLER8, pFlashlightDepthTexture, 0 );
  1025. DynamicCmdsOut.BindStandardTexture( SHADER_SAMPLER6, TEXTURE_SHADOW_NOISE_2D );
  1026. }
  1027. SetFlashLightColorFromState( state, pShaderAPI, 28, bFlashlightNoLambert );
  1028. Assert( info.m_nFlashlightTexture >= 0 && info.m_nFlashlightTextureFrame >= 0 );
  1029. pShader->BindTexture( SHADER_SAMPLER7, state.m_pSpotlightTexture, state.m_nSpotlightTextureFrame );
  1030. }
  1031. // Set up light combo state
  1032. LightState_t lightState = { 0, false, false, false };
  1033. if ( bVertexLitGeneric && (!bHasFlashlight || IsX360() ) )
  1034. {
  1035. pShaderAPI->GetDX9LightState( &lightState );
  1036. }
  1037. // Override the lighting desired if we have a lightmap set!
  1038. if ( bHasLightmapTexture )
  1039. {
  1040. lightState.m_bStaticLightVertex = false;
  1041. lightState.m_bStaticLightTexel = true;
  1042. // Usual case, not debugging.
  1043. if (!bHasMatLuxel)
  1044. {
  1045. pShader->BindTexture(SHADER_SAMPLER12, info.m_nLightmap);
  1046. }
  1047. else
  1048. {
  1049. float dimensions[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  1050. DynamicCmdsOut.BindStandardTexture( SHADER_SAMPLER12, TEXTURE_DEBUG_LUXELS );
  1051. pShader->GetTextureDimensions( &dimensions[0], &dimensions[1], info.m_nLightmap );
  1052. DynamicCmdsOut.SetPixelShaderConstant( 11, dimensions, 1 );
  1053. }
  1054. }
  1055. MaterialFogMode_t fogType = pShaderAPI->GetSceneFogMode();
  1056. int fogIndex = ( fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ) ? 1 : 0;
  1057. int numBones = pShaderAPI->GetCurrentNumBones();
  1058. bool bWriteDepthToAlpha;
  1059. bool bWriteWaterFogToAlpha;
  1060. if( bFullyOpaque )
  1061. {
  1062. bWriteDepthToAlpha = pShaderAPI->ShouldWriteDepthToDestAlpha();
  1063. bWriteWaterFogToAlpha = (fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z);
  1064. AssertMsg( !(bWriteDepthToAlpha && bWriteWaterFogToAlpha), "Can't write two values to alpha at the same time." );
  1065. }
  1066. else
  1067. {
  1068. //can't write a special value to dest alpha if we're actually using as-intended alpha
  1069. bWriteDepthToAlpha = false;
  1070. bWriteWaterFogToAlpha = false;
  1071. }
  1072. if ( bHasBump || bHasDiffuseWarp )
  1073. {
  1074. #ifndef _X360
  1075. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  1076. #endif
  1077. {
  1078. bool bUseStaticControlFlow = g_pHardwareConfig->SupportsStaticControlFlow();
  1079. DECLARE_DYNAMIC_VERTEX_SHADER( vertexlit_and_unlit_generic_bump_vs20 );
  1080. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  1081. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, numBones > 0 );
  1082. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  1083. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, bUseStaticControlFlow ? 0 : lightState.m_nNumLights );
  1084. SET_DYNAMIC_VERTEX_SHADER_CMD( DynamicCmdsOut, vertexlit_and_unlit_generic_bump_vs20 );
  1085. // Bind ps_2_b shader so we can get shadow mapping...
  1086. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send GL this way
  1087. {
  1088. DECLARE_DYNAMIC_PIXEL_SHADER( vertexlit_and_unlit_generic_bump_ps20b );
  1089. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  1090. SET_DYNAMIC_PIXEL_SHADER_COMBO( AMBIENT_LIGHT, lightState.m_bAmbientLight ? 1 : 0 );
  1091. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  1092. // SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  1093. SET_DYNAMIC_PIXEL_SHADER_CMD( DynamicCmdsOut, vertexlit_and_unlit_generic_bump_ps20b );
  1094. }
  1095. else
  1096. {
  1097. DECLARE_DYNAMIC_PIXEL_SHADER( vertexlit_and_unlit_generic_bump_ps20 );
  1098. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  1099. SET_DYNAMIC_PIXEL_SHADER_COMBO( AMBIENT_LIGHT, lightState.m_bAmbientLight ? 1 : 0 );
  1100. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, bWriteWaterFogToAlpha );
  1101. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  1102. SET_DYNAMIC_PIXEL_SHADER_CMD( DynamicCmdsOut, vertexlit_and_unlit_generic_bump_ps20 );
  1103. }
  1104. }
  1105. #ifndef _X360
  1106. else
  1107. {
  1108. pShader->SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_10, VERTEX_SHADER_SHADER_SPECIFIC_CONST_11, SHADER_VERTEXTEXTURE_SAMPLER0 );
  1109. DECLARE_DYNAMIC_VERTEX_SHADER( vertexlit_and_unlit_generic_bump_vs30 );
  1110. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  1111. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, numBones > 0 );
  1112. SET_DYNAMIC_VERTEX_SHADER_COMBO( MORPHING, pShaderAPI->IsHWMorphingEnabled() );
  1113. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  1114. SET_DYNAMIC_VERTEX_SHADER( vertexlit_and_unlit_generic_bump_vs30 );
  1115. DECLARE_DYNAMIC_PIXEL_SHADER( vertexlit_and_unlit_generic_bump_ps30 );
  1116. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  1117. SET_DYNAMIC_PIXEL_SHADER_COMBO( AMBIENT_LIGHT, lightState.m_bAmbientLight ? 1 : 0 );
  1118. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  1119. // SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  1120. SET_DYNAMIC_PIXEL_SHADER_CMD( DynamicCmdsOut, vertexlit_and_unlit_generic_bump_ps30 );
  1121. bool bUnusedTexCoords[3] = { false, false, !pShaderAPI->IsHWMorphingEnabled() || !bIsDecal };
  1122. pShaderAPI->MarkUnusedVertexFields( 0, 3, bUnusedTexCoords );
  1123. }
  1124. #endif
  1125. }
  1126. else // !( bHasBump || bHasDiffuseWarp )
  1127. {
  1128. if ( bAmbientOnly ) // Override selected light combo to be ambient only
  1129. {
  1130. lightState.m_bAmbientLight = true;
  1131. lightState.m_bStaticLightVertex = false;
  1132. lightState.m_nNumLights = 0;
  1133. }
  1134. #ifndef _X360
  1135. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  1136. #endif
  1137. {
  1138. bool bUseStaticControlFlow = g_pHardwareConfig->SupportsStaticControlFlow();
  1139. DECLARE_DYNAMIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs20 );
  1140. SET_DYNAMIC_VERTEX_SHADER_COMBO( DYNAMIC_LIGHT, lightState.HasDynamicLight() );
  1141. SET_DYNAMIC_VERTEX_SHADER_COMBO( STATIC_LIGHT_VERTEX, lightState.m_bStaticLightVertex ? 1 : 0 );
  1142. SET_DYNAMIC_VERTEX_SHADER_COMBO( STATIC_LIGHT_LIGHTMAP, lightState.m_bStaticLightTexel ? 1 : 0);
  1143. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  1144. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, numBones > 0 );
  1145. SET_DYNAMIC_VERTEX_SHADER_COMBO(
  1146. LIGHTING_PREVIEW,
  1147. pShaderAPI->GetIntRenderingParameter(INT_RENDERPARM_ENABLE_FIXED_LIGHTING)!=0);
  1148. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  1149. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, bUseStaticControlFlow ? 0 : lightState.m_nNumLights );
  1150. SET_DYNAMIC_VERTEX_SHADER_CMD( DynamicCmdsOut, vertexlit_and_unlit_generic_vs20 );
  1151. // Bind ps_2_b shader so we can get shadow mapping
  1152. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send GL this way
  1153. {
  1154. DECLARE_DYNAMIC_PIXEL_SHADER( vertexlit_and_unlit_generic_ps20b );
  1155. // SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  1156. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  1157. SET_DYNAMIC_PIXEL_SHADER_COMBO( STATIC_LIGHT_LIGHTMAP, lightState.m_bStaticLightTexel ? 1 : 0 );
  1158. SET_DYNAMIC_PIXEL_SHADER_COMBO( DEBUG_LUXELS, bHasMatLuxel ? 1 : 0 );
  1159. SET_DYNAMIC_PIXEL_SHADER_COMBO(
  1160. LIGHTING_PREVIEW,
  1161. pShaderAPI->GetIntRenderingParameter(INT_RENDERPARM_ENABLE_FIXED_LIGHTING) );
  1162. SET_DYNAMIC_PIXEL_SHADER_CMD( DynamicCmdsOut, vertexlit_and_unlit_generic_ps20b );
  1163. }
  1164. else
  1165. {
  1166. DECLARE_DYNAMIC_PIXEL_SHADER( vertexlit_and_unlit_generic_ps20 );
  1167. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  1168. SET_DYNAMIC_PIXEL_SHADER_COMBO( STATIC_LIGHT_LIGHTMAP, lightState.m_bStaticLightTexel ? 1 : 0 );
  1169. SET_DYNAMIC_PIXEL_SHADER_COMBO(
  1170. LIGHTING_PREVIEW,
  1171. pShaderAPI->GetIntRenderingParameter(INT_RENDERPARM_ENABLE_FIXED_LIGHTING) );
  1172. SET_DYNAMIC_PIXEL_SHADER_CMD( DynamicCmdsOut, vertexlit_and_unlit_generic_ps20 );
  1173. }
  1174. }
  1175. #ifndef _X360
  1176. else
  1177. {
  1178. pShader->SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_10, VERTEX_SHADER_SHADER_SPECIFIC_CONST_11, SHADER_VERTEXTEXTURE_SAMPLER0 );
  1179. DECLARE_DYNAMIC_VERTEX_SHADER( vertexlit_and_unlit_generic_vs30 );
  1180. SET_DYNAMIC_VERTEX_SHADER_COMBO( DYNAMIC_LIGHT, lightState.HasDynamicLight() );
  1181. SET_DYNAMIC_VERTEX_SHADER_COMBO( STATIC_LIGHT_VERTEX, lightState.m_bStaticLightVertex ? 1 : 0 );
  1182. SET_DYNAMIC_VERTEX_SHADER_COMBO( STATIC_LIGHT_LIGHTMAP, lightState.m_bStaticLightTexel ? 1 : 0 );
  1183. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  1184. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, numBones > 0 );
  1185. SET_DYNAMIC_VERTEX_SHADER_COMBO( LIGHTING_PREVIEW,
  1186. pShaderAPI->GetIntRenderingParameter(INT_RENDERPARM_ENABLE_FIXED_LIGHTING)!=0);
  1187. SET_DYNAMIC_VERTEX_SHADER_COMBO( MORPHING, pShaderAPI->IsHWMorphingEnabled() );
  1188. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  1189. SET_DYNAMIC_VERTEX_SHADER_CMD( DynamicCmdsOut, vertexlit_and_unlit_generic_vs30 );
  1190. DECLARE_DYNAMIC_PIXEL_SHADER( vertexlit_and_unlit_generic_ps30 );
  1191. // SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  1192. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  1193. SET_DYNAMIC_PIXEL_SHADER_COMBO( STATIC_LIGHT_LIGHTMAP, lightState.m_bStaticLightTexel ? 1 : 0 );
  1194. SET_DYNAMIC_PIXEL_SHADER_COMBO( DEBUG_LUXELS, bHasMatLuxel ? 1 : 0 );
  1195. SET_DYNAMIC_PIXEL_SHADER_COMBO( LIGHTING_PREVIEW,
  1196. pShaderAPI->GetIntRenderingParameter(INT_RENDERPARM_ENABLE_FIXED_LIGHTING) );
  1197. SET_DYNAMIC_PIXEL_SHADER_CMD( DynamicCmdsOut, vertexlit_and_unlit_generic_ps30 );
  1198. bool bUnusedTexCoords[3] = { false, false, !pShaderAPI->IsHWMorphingEnabled() || !bIsDecal };
  1199. pShaderAPI->MarkUnusedVertexFields( 0, 3, bUnusedTexCoords );
  1200. }
  1201. #endif
  1202. }
  1203. if ( ( info.m_nHDRColorScale != -1 ) && pShader->IsHDREnabled() )
  1204. {
  1205. pShader->SetModulationPixelShaderDynamicState_LinearColorSpace_LinearScale( 1, params[info.m_nHDRColorScale]->GetFloatValue() );
  1206. }
  1207. else
  1208. {
  1209. pShader->SetModulationPixelShaderDynamicState_LinearColorSpace( 1 );
  1210. }
  1211. float eyePos[4];
  1212. pShaderAPI->GetWorldSpaceCameraPosition( eyePos );
  1213. DynamicCmdsOut.SetPixelShaderConstant( 20, eyePos );
  1214. // Non-bump case does its own depth feathering work
  1215. if ( !bHasBump && !bHasDiffuseWarp )
  1216. {
  1217. DynamicCmdsOut.SetDepthFeatheringPixelShaderConstant( 13, GetFloatParam( info.m_nDepthBlendScale, params, 50.0f ) );
  1218. }
  1219. float fPixelFogType = pShaderAPI->GetPixelFogCombo() == 1 ? 1 : 0;
  1220. float fWriteDepthToAlpha = bWriteDepthToAlpha && IsPC() ? 1 : 0;
  1221. float fWriteWaterFogToDestAlpha = (pShaderAPI->GetPixelFogCombo() == 1 && bWriteWaterFogToAlpha) ? 1 : 0;
  1222. float fVertexAlpha = bHasVertexAlpha ? 1 : 0;
  1223. // Controls for lerp-style paths through shader code (bump and non-bump have use different register)
  1224. float vShaderControls[4] = { fPixelFogType, fWriteDepthToAlpha, fWriteWaterFogToDestAlpha, fVertexAlpha };
  1225. DynamicCmdsOut.SetPixelShaderConstant( 12, vShaderControls, 1 );
  1226. // flashlightfixme: put this in common code.
  1227. if ( bHasFlashlight )
  1228. {
  1229. VMatrix worldToTexture;
  1230. const FlashlightState_t &flashlightState = pShaderAPI->GetFlashlightState( worldToTexture );
  1231. SetFlashLightColorFromState( flashlightState, pShaderAPI, 28, bFlashlightNoLambert );
  1232. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, worldToTexture.Base(), 4 );
  1233. pShader->BindTexture( SHADER_SAMPLER7, flashlightState.m_pSpotlightTexture, flashlightState.m_nSpotlightTextureFrame );
  1234. float atten_pos[8];
  1235. atten_pos[0] = flashlightState.m_fConstantAtten; // Set the flashlight attenuation factors
  1236. atten_pos[1] = flashlightState.m_fLinearAtten;
  1237. atten_pos[2] = flashlightState.m_fQuadraticAtten;
  1238. atten_pos[3] = flashlightState.m_FarZ;
  1239. atten_pos[4] = flashlightState.m_vecLightOrigin[0]; // Set the flashlight origin
  1240. atten_pos[5] = flashlightState.m_vecLightOrigin[1];
  1241. atten_pos[6] = flashlightState.m_vecLightOrigin[2];
  1242. atten_pos[7] = 1.0f;
  1243. DynamicCmdsOut.SetPixelShaderConstant( 22, atten_pos, 2 );
  1244. DynamicCmdsOut.SetPixelShaderConstant( 24, worldToTexture.Base(), 4 );
  1245. }
  1246. DynamicCmdsOut.End();
  1247. pShaderAPI->ExecuteCommandBuffer( DynamicCmdsOut.Base() );
  1248. }
  1249. pShader->Draw();
  1250. /*^*/ // printf("\t\t<DrawVertexLitGeneric_DX9_Internal\n");
  1251. }
  1252. void DrawVertexLitGeneric_DX9( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  1253. IShaderShadow* pShaderShadow, bool bVertexLitGeneric, VertexLitGeneric_DX9_Vars_t &info, VertexCompressionType_t vertexCompression,
  1254. CBasePerMaterialContextData **pContextDataPtr )
  1255. {
  1256. if ( WantsSkinShader( params, info ) && g_pHardwareConfig->SupportsPixelShaders_2_b() && g_pConfig->UseBumpmapping() && g_pConfig->UsePhong() )
  1257. {
  1258. DrawSkin_DX9( pShader, params, pShaderAPI, pShaderShadow, info, vertexCompression, pContextDataPtr );
  1259. return;
  1260. }
  1261. bool bReceiveFlashlight = bVertexLitGeneric;
  1262. bool bNewFlashlight = IsX360();
  1263. if ( bNewFlashlight )
  1264. {
  1265. bReceiveFlashlight = bReceiveFlashlight || ( GetIntParam( info.m_nReceiveFlashlight, params ) != 0 );
  1266. }
  1267. bool bHasFlashlight = bReceiveFlashlight && pShader->UsingFlashlight( params );
  1268. DrawVertexLitGeneric_DX9_Internal( pShader, params, pShaderAPI,
  1269. pShaderShadow, bVertexLitGeneric, bHasFlashlight, info, vertexCompression, pContextDataPtr );
  1270. }