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.

1021 lines
38 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Lightmap only shader
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================
  8. #include "lightmappedgeneric_dx9_helper.h"
  9. #include "BaseVSShader.h"
  10. #include "commandbuilder.h"
  11. #include "convar.h"
  12. #include "lightmappedgeneric_ps20.inc"
  13. #include "lightmappedgeneric_vs20.inc"
  14. #include "lightmappedgeneric_ps20b.inc"
  15. #include "tier0/memdbgon.h"
  16. ConVar mat_disable_lightwarp( "mat_disable_lightwarp", "0" );
  17. ConVar mat_disable_fancy_blending( "mat_disable_fancy_blending", "0" );
  18. ConVar mat_fullbright( "mat_fullbright","0", FCVAR_CHEAT );
  19. ConVar my_mat_fullbright( "mat_fullbright","0", FCVAR_CHEAT );
  20. extern ConVar r_flashlight_version2;
  21. class CLightmappedGeneric_DX9_Context : public CBasePerMaterialContextData
  22. {
  23. public:
  24. uint8 *m_pStaticCmds;
  25. CCommandBufferBuilder< CFixedCommandStorageBuffer< 1000 > > m_SemiStaticCmdsOut;
  26. bool m_bVertexShaderFastPath;
  27. bool m_bPixelShaderFastPath;
  28. bool m_bPixelShaderForceFastPathBecauseOutline;
  29. bool m_bFullyOpaque;
  30. bool m_bFullyOpaqueWithoutAlphaTest;
  31. void ResetStaticCmds( void )
  32. {
  33. if ( m_pStaticCmds )
  34. {
  35. delete[] m_pStaticCmds;
  36. m_pStaticCmds = NULL;
  37. }
  38. }
  39. CLightmappedGeneric_DX9_Context( void )
  40. {
  41. m_pStaticCmds = NULL;
  42. }
  43. ~CLightmappedGeneric_DX9_Context( void )
  44. {
  45. ResetStaticCmds();
  46. }
  47. };
  48. void InitParamsLightmappedGeneric_DX9( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, LightmappedGeneric_DX9_Vars_t &info )
  49. {
  50. if ( g_pHardwareConfig->SupportsBorderColor() )
  51. {
  52. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight_border" );
  53. }
  54. else
  55. {
  56. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight001" );
  57. }
  58. // Write over $basetexture with $albedo if we are going to be using diffuse normal mapping.
  59. if( g_pConfig->UseBumpmapping() && params[info.m_nBumpmap]->IsDefined() && params[info.m_nAlbedo]->IsDefined() &&
  60. params[info.m_nBaseTexture]->IsDefined() &&
  61. !( params[info.m_nNoDiffuseBumpLighting]->IsDefined() && params[info.m_nNoDiffuseBumpLighting]->GetIntValue() ) )
  62. {
  63. params[info.m_nBaseTexture]->SetStringValue( params[info.m_nAlbedo]->GetStringValue() );
  64. }
  65. if( pShader->IsUsingGraphics() && params[info.m_nEnvmap]->IsDefined() && !pShader->CanUseEditorMaterials() )
  66. {
  67. if( stricmp( params[info.m_nEnvmap]->GetStringValue(), "env_cubemap" ) == 0 )
  68. {
  69. Warning( "env_cubemap used on world geometry without rebuilding map. . ignoring: %s\n", pMaterialName );
  70. params[info.m_nEnvmap]->SetUndefined();
  71. }
  72. }
  73. if ( (mat_disable_lightwarp.GetBool() ) &&
  74. (info.m_nLightWarpTexture != -1) )
  75. {
  76. params[info.m_nLightWarpTexture]->SetUndefined();
  77. }
  78. if ( (mat_disable_fancy_blending.GetBool() ) &&
  79. (info.m_nBlendModulateTexture != -1) )
  80. {
  81. params[info.m_nBlendModulateTexture]->SetUndefined();
  82. }
  83. if( !params[info.m_nEnvmapTint]->IsDefined() )
  84. params[info.m_nEnvmapTint]->SetVecValue( 1.0f, 1.0f, 1.0f );
  85. if( !params[info.m_nNoDiffuseBumpLighting]->IsDefined() )
  86. params[info.m_nNoDiffuseBumpLighting]->SetIntValue( 0 );
  87. if( !params[info.m_nSelfIllumTint]->IsDefined() )
  88. params[info.m_nSelfIllumTint]->SetVecValue( 1.0f, 1.0f, 1.0f );
  89. if( !params[info.m_nDetailScale]->IsDefined() )
  90. params[info.m_nDetailScale]->SetFloatValue( 4.0f );
  91. if ( !params[info.m_nDetailTint]->IsDefined() )
  92. params[info.m_nDetailTint]->SetVecValue( 1.0f, 1.0f, 1.0f, 1.0f );
  93. InitFloatParam( info.m_nDetailTextureBlendFactor, params, 1.0 );
  94. InitIntParam( info.m_nDetailTextureCombineMode, params, 0 );
  95. if( !params[info.m_nFresnelReflection]->IsDefined() )
  96. params[info.m_nFresnelReflection]->SetFloatValue( 1.0f );
  97. if( !params[info.m_nEnvmapMaskFrame]->IsDefined() )
  98. params[info.m_nEnvmapMaskFrame]->SetIntValue( 0 );
  99. if( !params[info.m_nEnvmapFrame]->IsDefined() )
  100. params[info.m_nEnvmapFrame]->SetIntValue( 0 );
  101. if( !params[info.m_nBumpFrame]->IsDefined() )
  102. params[info.m_nBumpFrame]->SetIntValue( 0 );
  103. if( !params[info.m_nDetailFrame]->IsDefined() )
  104. params[info.m_nDetailFrame]->SetIntValue( 0 );
  105. if( !params[info.m_nEnvmapContrast]->IsDefined() )
  106. params[info.m_nEnvmapContrast]->SetFloatValue( 0.0f );
  107. if( !params[info.m_nEnvmapSaturation]->IsDefined() )
  108. params[info.m_nEnvmapSaturation]->SetFloatValue( 1.0f );
  109. InitFloatParam( info.m_nAlphaTestReference, params, 0.0f );
  110. // No texture means no self-illum or env mask in base alpha
  111. if ( !params[info.m_nBaseTexture]->IsDefined() )
  112. {
  113. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  114. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  115. }
  116. if( params[info.m_nBumpmap]->IsDefined() )
  117. {
  118. params[info.m_nEnvmapMask]->SetUndefined();
  119. }
  120. // If in decal mode, no debug override...
  121. if (IS_FLAG_SET(MATERIAL_VAR_DECAL))
  122. {
  123. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  124. }
  125. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  126. if( g_pConfig->UseBumpmapping() && params[info.m_nBumpmap]->IsDefined() && (params[info.m_nNoDiffuseBumpLighting]->GetIntValue() == 0) )
  127. {
  128. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_BUMPED_LIGHTMAP );
  129. }
  130. // If mat_specular 0, then get rid of envmap
  131. if( !g_pConfig->UseSpecular() && params[info.m_nEnvmap]->IsDefined() && params[info.m_nBaseTexture]->IsDefined() )
  132. {
  133. params[info.m_nEnvmap]->SetUndefined();
  134. }
  135. if( !params[info.m_nBaseTextureNoEnvmap]->IsDefined() )
  136. {
  137. params[info.m_nBaseTextureNoEnvmap]->SetIntValue( 0 );
  138. }
  139. if( !params[info.m_nBaseTexture2NoEnvmap]->IsDefined() )
  140. {
  141. params[info.m_nBaseTexture2NoEnvmap]->SetIntValue( 0 );
  142. }
  143. if( ( info.m_nSelfShadowedBumpFlag != -1 ) &&
  144. ( !params[info.m_nSelfShadowedBumpFlag]->IsDefined() )
  145. )
  146. {
  147. params[info.m_nSelfShadowedBumpFlag]->SetIntValue( 0 );
  148. }
  149. // handle line art parms
  150. InitFloatParam( info.m_nEdgeSoftnessStart, params, 0.5 );
  151. InitFloatParam( info.m_nEdgeSoftnessEnd, params, 0.5 );
  152. InitFloatParam( info.m_nOutlineAlpha, params, 1.0 );
  153. }
  154. void InitLightmappedGeneric_DX9( CBaseVSShader *pShader, IMaterialVar** params, LightmappedGeneric_DX9_Vars_t &info )
  155. {
  156. if ( g_pConfig->UseBumpmapping() && params[info.m_nBumpmap]->IsDefined() )
  157. {
  158. pShader->LoadBumpMap( info.m_nBumpmap );
  159. }
  160. if ( g_pConfig->UseBumpmapping() && params[info.m_nBumpmap2]->IsDefined() )
  161. {
  162. pShader->LoadBumpMap( info.m_nBumpmap2 );
  163. }
  164. if ( g_pConfig->UseBumpmapping() && params[info.m_nBumpMask]->IsDefined() )
  165. {
  166. pShader->LoadBumpMap( info.m_nBumpMask );
  167. }
  168. if (params[info.m_nBaseTexture]->IsDefined())
  169. {
  170. pShader->LoadTexture( info.m_nBaseTexture, TEXTUREFLAGS_SRGB );
  171. if (!params[info.m_nBaseTexture]->GetTextureValue()->IsTranslucent())
  172. {
  173. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  174. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  175. }
  176. }
  177. if (params[info.m_nBaseTexture2]->IsDefined() )
  178. {
  179. pShader->LoadTexture( info.m_nBaseTexture2, TEXTUREFLAGS_SRGB );
  180. }
  181. if (params[info.m_nLightWarpTexture]->IsDefined() )
  182. {
  183. pShader->LoadTexture( info.m_nLightWarpTexture );
  184. }
  185. if ((info.m_nBlendModulateTexture != -1) &&
  186. (params[info.m_nBlendModulateTexture]->IsDefined()) )
  187. {
  188. pShader->LoadTexture( info.m_nBlendModulateTexture );
  189. }
  190. if (params[info.m_nDetail]->IsDefined())
  191. {
  192. int nDetailBlendMode = ( info.m_nDetailTextureCombineMode == -1 ) ? 0 : params[info.m_nDetailTextureCombineMode]->GetIntValue();
  193. nDetailBlendMode = nDetailBlendMode > 1 ? 1 : nDetailBlendMode;
  194. pShader->LoadTexture( info.m_nDetail, nDetailBlendMode != 0 ? TEXTUREFLAGS_SRGB : 0 );
  195. }
  196. pShader->LoadTexture( info.m_nFlashlightTexture, TEXTUREFLAGS_SRGB );
  197. // Don't alpha test if the alpha channel is used for other purposes
  198. if (IS_FLAG_SET(MATERIAL_VAR_SELFILLUM) || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  199. {
  200. CLEAR_FLAGS( MATERIAL_VAR_ALPHATEST );
  201. }
  202. if (params[info.m_nEnvmap]->IsDefined())
  203. {
  204. if ( !IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) )
  205. {
  206. pShader->LoadCubeMap( info.m_nEnvmap, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE ? TEXTUREFLAGS_SRGB : 0 );
  207. }
  208. else
  209. {
  210. pShader->LoadTexture( info.m_nEnvmap );
  211. }
  212. if ( !g_pHardwareConfig->SupportsCubeMaps() )
  213. {
  214. SET_FLAGS( MATERIAL_VAR_ENVMAPSPHERE );
  215. }
  216. if ( params[info.m_nEnvmapMask]->IsDefined() )
  217. {
  218. pShader->LoadTexture( info.m_nEnvmapMask );
  219. }
  220. }
  221. else
  222. {
  223. params[info.m_nEnvmapMask]->SetUndefined();
  224. }
  225. // We always need this because of the flashlight.
  226. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  227. }
  228. void DrawLightmappedGeneric_DX9_Internal(CBaseVSShader *pShader, IMaterialVar** params, bool hasFlashlight,
  229. IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow,
  230. LightmappedGeneric_DX9_Vars_t &info,
  231. CBasePerMaterialContextData **pContextDataPtr
  232. )
  233. {
  234. CLightmappedGeneric_DX9_Context *pContextData = reinterpret_cast< CLightmappedGeneric_DX9_Context *> ( *pContextDataPtr );
  235. if ( pShaderShadow || ( ! pContextData ) || pContextData->m_bMaterialVarsChanged || hasFlashlight )
  236. {
  237. bool hasBaseTexture = params[info.m_nBaseTexture]->IsTexture();
  238. int nAlphaChannelTextureVar = hasBaseTexture ? (int)info.m_nBaseTexture : (int)info.m_nEnvmapMask;
  239. BlendType_t nBlendType = pShader->EvaluateBlendRequirements( nAlphaChannelTextureVar, hasBaseTexture );
  240. bool bIsAlphaTested = IS_FLAG_SET( MATERIAL_VAR_ALPHATEST ) != 0;
  241. bool bFullyOpaqueWithoutAlphaTest = (nBlendType != BT_BLENDADD) && (nBlendType != BT_BLEND) && (!hasFlashlight || IsX360()); //dest alpha is free for special use
  242. bool bFullyOpaque = bFullyOpaqueWithoutAlphaTest && !bIsAlphaTested;
  243. bool bNeedRegenStaticCmds = (! pContextData ) || pShaderShadow;
  244. if ( ! pContextData ) // make sure allocated
  245. {
  246. pContextData = new CLightmappedGeneric_DX9_Context;
  247. *pContextDataPtr = pContextData;
  248. }
  249. bool hasBump = ( params[info.m_nBumpmap]->IsTexture() ) && ( !g_pHardwareConfig->PreferReducedFillrate() );
  250. bool hasSSBump = hasBump && (info.m_nSelfShadowedBumpFlag != -1) && ( params[info.m_nSelfShadowedBumpFlag]->GetIntValue() );
  251. bool hasBaseTexture2 = hasBaseTexture && params[info.m_nBaseTexture2]->IsTexture();
  252. bool hasLightWarpTexture = params[info.m_nLightWarpTexture]->IsTexture();
  253. bool hasBump2 = hasBump && params[info.m_nBumpmap2]->IsTexture();
  254. bool hasDetailTexture = params[info.m_nDetail]->IsTexture();
  255. bool hasSelfIllum = IS_FLAG_SET( MATERIAL_VAR_SELFILLUM );
  256. bool hasBumpMask = hasBump && hasBump2 && params[info.m_nBumpMask]->IsTexture() && !hasSelfIllum &&
  257. !hasDetailTexture && !hasBaseTexture2 && (params[info.m_nBaseTextureNoEnvmap]->GetIntValue() == 0);
  258. bool bHasBlendModulateTexture =
  259. (info.m_nBlendModulateTexture != -1) &&
  260. (params[info.m_nBlendModulateTexture]->IsTexture() );
  261. bool hasNormalMapAlphaEnvmapMask = IS_FLAG_SET( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );
  262. if ( hasFlashlight && !IsX360() )
  263. {
  264. // !!speed!! do this in the caller so we don't build struct every time
  265. CBaseVSShader::DrawFlashlight_dx90_Vars_t vars;
  266. vars.m_bBump = hasBump;
  267. vars.m_nBumpmapVar = info.m_nBumpmap;
  268. vars.m_nBumpmapFrame = info.m_nBumpFrame;
  269. vars.m_nBumpTransform = info.m_nBumpTransform;
  270. vars.m_nFlashlightTextureVar = info.m_nFlashlightTexture;
  271. vars.m_nFlashlightTextureFrameVar = info.m_nFlashlightTextureFrame;
  272. vars.m_bLightmappedGeneric = true;
  273. vars.m_bWorldVertexTransition = hasBaseTexture2;
  274. vars.m_nBaseTexture2Var = info.m_nBaseTexture2;
  275. vars.m_nBaseTexture2FrameVar = info.m_nBaseTexture2Frame;
  276. vars.m_nBumpmap2Var = info.m_nBumpmap2;
  277. vars.m_nBumpmap2Frame = info.m_nBumpFrame2;
  278. vars.m_nBump2Transform = info.m_nBumpTransform2;
  279. vars.m_nAlphaTestReference = info.m_nAlphaTestReference;
  280. vars.m_bSSBump = hasSSBump;
  281. vars.m_nDetailVar = info.m_nDetail;
  282. vars.m_nDetailScale = info.m_nDetailScale;
  283. vars.m_nDetailTextureCombineMode = info.m_nDetailTextureCombineMode;
  284. vars.m_nDetailTextureBlendFactor = info.m_nDetailTextureBlendFactor;
  285. vars.m_nDetailTint = info.m_nDetailTint;
  286. if ( ( info.m_nSeamlessMappingScale != -1 ) )
  287. vars.m_fSeamlessScale = params[info.m_nSeamlessMappingScale]->GetFloatValue();
  288. else
  289. vars.m_fSeamlessScale = 0.0;
  290. pShader->DrawFlashlight_dx90( params, pShaderAPI, pShaderShadow, vars );
  291. return;
  292. }
  293. pContextData->m_bFullyOpaque = bFullyOpaque;
  294. pContextData->m_bFullyOpaqueWithoutAlphaTest = bFullyOpaqueWithoutAlphaTest;
  295. bool bHasOutline = IsBoolSet( info.m_nOutline, params );
  296. pContextData->m_bPixelShaderForceFastPathBecauseOutline = bHasOutline;
  297. bool bHasSoftEdges = IsBoolSet( info.m_nSoftEdges, params );
  298. bool hasEnvmapMask = params[info.m_nEnvmapMask]->IsTexture();
  299. float fDetailBlendFactor = GetFloatParam( info.m_nDetailTextureBlendFactor, params, 1.0 );
  300. if ( pShaderShadow || bNeedRegenStaticCmds )
  301. {
  302. bool hasVertexColor = IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR );
  303. bool hasDiffuseBumpmap = hasBump && (params[info.m_nNoDiffuseBumpLighting]->GetIntValue() == 0);
  304. bool hasEnvmap = params[info.m_nEnvmap]->IsTexture();
  305. bool bSeamlessMapping = ( ( info.m_nSeamlessMappingScale != -1 ) &&
  306. ( params[info.m_nSeamlessMappingScale]->GetFloatValue() != 0.0 ) );
  307. if ( bNeedRegenStaticCmds )
  308. {
  309. pContextData->ResetStaticCmds();
  310. CCommandBufferBuilder< CFixedCommandStorageBuffer< 5000 > > staticCmdsBuf;
  311. if( !hasBaseTexture )
  312. {
  313. if( hasEnvmap )
  314. {
  315. // if we only have an envmap (no basetexture), then we want the albedo to be black.
  316. staticCmdsBuf.BindStandardTexture( SHADER_SAMPLER0, TEXTURE_BLACK );
  317. }
  318. else
  319. {
  320. staticCmdsBuf.BindStandardTexture( SHADER_SAMPLER0, TEXTURE_WHITE );
  321. }
  322. }
  323. staticCmdsBuf.BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP );
  324. if ( bSeamlessMapping )
  325. {
  326. staticCmdsBuf.SetVertexShaderConstant4(
  327. VERTEX_SHADER_SHADER_SPECIFIC_CONST_0,
  328. params[info.m_nSeamlessMappingScale]->GetFloatValue(),0,0,0 );
  329. }
  330. staticCmdsBuf.StoreEyePosInPixelShaderConstant( 10 );
  331. staticCmdsBuf.SetPixelShaderFogParams( 11 );
  332. staticCmdsBuf.End();
  333. // now, copy buf
  334. pContextData->m_pStaticCmds = new uint8[staticCmdsBuf.Size()];
  335. memcpy( pContextData->m_pStaticCmds, staticCmdsBuf.Base(), staticCmdsBuf.Size() );
  336. }
  337. if ( pShaderShadow )
  338. {
  339. // Alpha test: FIXME: shouldn't this be handled in Shader_t::SetInitialShadowState
  340. pShaderShadow->EnableAlphaTest( bIsAlphaTested );
  341. if ( info.m_nAlphaTestReference != -1 && params[info.m_nAlphaTestReference]->GetFloatValue() > 0.0f )
  342. {
  343. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, params[info.m_nAlphaTestReference]->GetFloatValue() );
  344. }
  345. pShader->SetDefaultBlendingShadowState( nAlphaChannelTextureVar, hasBaseTexture );
  346. unsigned int flags = VERTEX_POSITION;
  347. // base texture
  348. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  349. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  350. if ( hasLightWarpTexture )
  351. {
  352. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true );
  353. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER6, false );
  354. }
  355. if ( bHasBlendModulateTexture )
  356. {
  357. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  358. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, false );
  359. }
  360. if ( hasBaseTexture2 )
  361. {
  362. pShaderShadow->EnableTexture( SHADER_SAMPLER7, true );
  363. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER7, true );
  364. }
  365. // if( hasLightmap )
  366. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  367. if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
  368. {
  369. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  370. }
  371. else
  372. {
  373. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, false );
  374. }
  375. if( hasEnvmap || ( IsX360() && hasFlashlight ) )
  376. {
  377. if( hasEnvmap )
  378. {
  379. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  380. if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
  381. {
  382. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true );
  383. }
  384. }
  385. flags |= VERTEX_TANGENT_S | VERTEX_TANGENT_T | VERTEX_NORMAL;
  386. }
  387. int nDetailBlendMode = 0;
  388. if ( hasDetailTexture )
  389. {
  390. nDetailBlendMode = GetIntParam( info.m_nDetailTextureCombineMode, params );
  391. ITexture *pDetailTexture = params[info.m_nDetail]->GetTextureValue();
  392. if ( pDetailTexture->GetFlags() & TEXTUREFLAGS_SSBUMP )
  393. {
  394. if ( hasBump )
  395. nDetailBlendMode = 10; // ssbump
  396. else
  397. nDetailBlendMode = 11; // ssbump_nobump
  398. }
  399. }
  400. if( hasDetailTexture )
  401. {
  402. pShaderShadow->EnableTexture( SHADER_SAMPLER12, true );
  403. bool bSRGBState = ( nDetailBlendMode == 1 );
  404. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER12, bSRGBState );
  405. }
  406. if( hasBump || hasNormalMapAlphaEnvmapMask )
  407. {
  408. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  409. }
  410. if( hasBump2 )
  411. {
  412. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true );
  413. }
  414. if( hasBumpMask )
  415. {
  416. pShaderShadow->EnableTexture( SHADER_SAMPLER8, true );
  417. }
  418. if( hasEnvmapMask )
  419. {
  420. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true );
  421. }
  422. if( hasFlashlight && IsX360() )
  423. {
  424. pShaderShadow->EnableTexture( SHADER_SAMPLER13, true );
  425. pShaderShadow->EnableTexture( SHADER_SAMPLER14, true );
  426. pShaderShadow->SetShadowDepthFiltering( SHADER_SAMPLER14 );
  427. pShaderShadow->EnableTexture( SHADER_SAMPLER15, true );
  428. }
  429. if( hasVertexColor || hasBaseTexture2 || hasBump2 )
  430. {
  431. flags |= VERTEX_COLOR;
  432. }
  433. // texcoord0 : base texcoord
  434. // texcoord1 : lightmap texcoord
  435. // texcoord2 : lightmap texcoord offset
  436. int numTexCoords = 2;
  437. if( hasBump )
  438. {
  439. numTexCoords = 3;
  440. }
  441. pShaderShadow->VertexShaderVertexFormat( flags, numTexCoords, 0, 0 );
  442. // Pre-cache pixel shaders
  443. bool hasBaseAlphaEnvmapMask = IS_FLAG_SET( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  444. int bumpmap_variant=(hasSSBump) ? 2 : hasBump;
  445. bool bMaskedBlending=( (info.m_nMaskedBlending != -1) &&
  446. (params[info.m_nMaskedBlending]->GetIntValue() != 0) );
  447. DECLARE_STATIC_VERTEX_SHADER( lightmappedgeneric_vs20 );
  448. SET_STATIC_VERTEX_SHADER_COMBO( ENVMAP_MASK, hasEnvmapMask );
  449. SET_STATIC_VERTEX_SHADER_COMBO( TANGENTSPACE, params[info.m_nEnvmap]->IsTexture() );
  450. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMAP, hasBump );
  451. SET_STATIC_VERTEX_SHADER_COMBO( DIFFUSEBUMPMAP, hasDiffuseBumpmap );
  452. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR ) );
  453. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXALPHATEXBLENDFACTOR, hasBaseTexture2 || hasBump2 );
  454. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMASK, hasBumpMask );
  455. bool bReliefMapping = false; //( bumpmap_variant == 2 ) && ( ! bSeamlessMapping );
  456. SET_STATIC_VERTEX_SHADER_COMBO( RELIEF_MAPPING, false );//bReliefMapping );
  457. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS, bSeamlessMapping );
  458. #ifdef _X360
  459. SET_STATIC_VERTEX_SHADER_COMBO( FLASHLIGHT, hasFlashlight);
  460. #endif
  461. SET_STATIC_VERTEX_SHADER( lightmappedgeneric_vs20 );
  462. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  463. {
  464. DECLARE_STATIC_PIXEL_SHADER( lightmappedgeneric_ps20b );
  465. SET_STATIC_PIXEL_SHADER_COMBO( BASETEXTURE2, hasBaseTexture2 );
  466. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, hasDetailTexture );
  467. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, bumpmap_variant );
  468. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP2, hasBump2 );
  469. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMASK, hasBumpMask );
  470. SET_STATIC_PIXEL_SHADER_COMBO( DIFFUSEBUMPMAP, hasDiffuseBumpmap );
  471. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, hasEnvmap );
  472. SET_STATIC_PIXEL_SHADER_COMBO( ENVMAPMASK, hasEnvmapMask );
  473. SET_STATIC_PIXEL_SHADER_COMBO( BASEALPHAENVMAPMASK, hasBaseAlphaEnvmapMask );
  474. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, hasSelfIllum );
  475. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMAPALPHAENVMAPMASK, hasNormalMapAlphaEnvmapMask );
  476. SET_STATIC_PIXEL_SHADER_COMBO( BASETEXTURENOENVMAP, params[info.m_nBaseTextureNoEnvmap]->GetIntValue() );
  477. SET_STATIC_PIXEL_SHADER_COMBO( BASETEXTURE2NOENVMAP, params[info.m_nBaseTexture2NoEnvmap]->GetIntValue() );
  478. SET_STATIC_PIXEL_SHADER_COMBO( WARPLIGHTING, hasLightWarpTexture );
  479. SET_STATIC_PIXEL_SHADER_COMBO( FANCY_BLENDING, bHasBlendModulateTexture );
  480. SET_STATIC_PIXEL_SHADER_COMBO( MASKEDBLENDING, bMaskedBlending);
  481. SET_STATIC_PIXEL_SHADER_COMBO( RELIEF_MAPPING, bReliefMapping );
  482. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS, bSeamlessMapping );
  483. SET_STATIC_PIXEL_SHADER_COMBO( OUTLINE, bHasOutline );
  484. SET_STATIC_PIXEL_SHADER_COMBO( SOFTEDGES, bHasSoftEdges );
  485. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  486. SET_STATIC_PIXEL_SHADER_COMBO( NORMAL_DECODE_MODE, (int) NORMAL_DECODE_NONE );
  487. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMASK_DECODE_MODE, (int) NORMAL_DECODE_NONE );
  488. #ifdef _X360
  489. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, hasFlashlight);
  490. #endif
  491. SET_STATIC_PIXEL_SHADER( lightmappedgeneric_ps20b );
  492. }
  493. else
  494. {
  495. DECLARE_STATIC_PIXEL_SHADER( lightmappedgeneric_ps20 );
  496. SET_STATIC_PIXEL_SHADER_COMBO( BASETEXTURE2, hasBaseTexture2 );
  497. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, hasDetailTexture );
  498. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, bumpmap_variant );
  499. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP2, hasBump2 );
  500. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMASK, hasBumpMask );
  501. SET_STATIC_PIXEL_SHADER_COMBO( DIFFUSEBUMPMAP, hasDiffuseBumpmap );
  502. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, hasEnvmap );
  503. SET_STATIC_PIXEL_SHADER_COMBO( ENVMAPMASK, hasEnvmapMask );
  504. SET_STATIC_PIXEL_SHADER_COMBO( BASEALPHAENVMAPMASK, hasBaseAlphaEnvmapMask );
  505. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, hasSelfIllum );
  506. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMAPALPHAENVMAPMASK, hasNormalMapAlphaEnvmapMask );
  507. SET_STATIC_PIXEL_SHADER_COMBO( BASETEXTURENOENVMAP, params[info.m_nBaseTextureNoEnvmap]->GetIntValue() );
  508. SET_STATIC_PIXEL_SHADER_COMBO( BASETEXTURE2NOENVMAP, params[info.m_nBaseTexture2NoEnvmap]->GetIntValue() );
  509. SET_STATIC_PIXEL_SHADER_COMBO( WARPLIGHTING, hasLightWarpTexture );
  510. SET_STATIC_PIXEL_SHADER_COMBO( FANCY_BLENDING, bHasBlendModulateTexture );
  511. SET_STATIC_PIXEL_SHADER_COMBO( MASKEDBLENDING, bMaskedBlending);
  512. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS, bSeamlessMapping );
  513. SET_STATIC_PIXEL_SHADER_COMBO( OUTLINE, bHasOutline );
  514. SET_STATIC_PIXEL_SHADER_COMBO( SOFTEDGES, bHasSoftEdges );
  515. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  516. SET_STATIC_PIXEL_SHADER_COMBO( NORMAL_DECODE_MODE, 0 ); // No normal compression with ps_2_0 (yikes!)
  517. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMASK_DECODE_MODE, 0 ); // No normal compression with ps_2_0
  518. SET_STATIC_PIXEL_SHADER( lightmappedgeneric_ps20 );
  519. }
  520. // HACK HACK HACK - enable alpha writes all the time so that we have them for
  521. // underwater stuff and writing depth to dest alpha
  522. // But only do it if we're not using the alpha already for translucency
  523. pShaderShadow->EnableAlphaWrites( bFullyOpaque );
  524. pShaderShadow->EnableSRGBWrite( true );
  525. pShader->DefaultFog();
  526. } // end shadow state
  527. } // end shadow || regen display list
  528. if ( pShaderAPI && pContextData->m_bMaterialVarsChanged )
  529. {
  530. // need to regenerate the semistatic cmds
  531. pContextData->m_SemiStaticCmdsOut.Reset();
  532. pContextData->m_bMaterialVarsChanged = false;
  533. bool bHasBlendMaskTransform= (
  534. (info.m_nBlendMaskTransform != -1) &&
  535. (info.m_nMaskedBlending != -1) &&
  536. (params[info.m_nMaskedBlending]->GetIntValue() ) &&
  537. ( ! (params[info.m_nBumpTransform]->MatrixIsIdentity() ) ) );
  538. // If we don't have a texture transform, we don't have
  539. // to set vertex shader constants or run vertex shader instructions
  540. // for the texture transform.
  541. bool bHasTextureTransform =
  542. !( params[info.m_nBaseTextureTransform]->MatrixIsIdentity() &&
  543. params[info.m_nBumpTransform]->MatrixIsIdentity() &&
  544. params[info.m_nBumpTransform2]->MatrixIsIdentity() &&
  545. params[info.m_nEnvmapMaskTransform]->MatrixIsIdentity() );
  546. bHasTextureTransform |= bHasBlendMaskTransform;
  547. pContextData->m_bVertexShaderFastPath = !bHasTextureTransform;
  548. if( params[info.m_nDetail]->IsTexture() )
  549. {
  550. pContextData->m_bVertexShaderFastPath = false;
  551. }
  552. if (bHasBlendMaskTransform)
  553. {
  554. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform(
  555. VERTEX_SHADER_SHADER_SPECIFIC_CONST_10, info.m_nBlendMaskTransform );
  556. }
  557. if ( ! pContextData->m_bVertexShaderFastPath )
  558. {
  559. bool bSeamlessMapping = ( ( info.m_nSeamlessMappingScale != -1 ) &&
  560. ( params[info.m_nSeamlessMappingScale]->GetFloatValue() != 0.0 ) );
  561. bool hasEnvmapMask = params[info.m_nEnvmapMask]->IsTexture();
  562. if (!bSeamlessMapping )
  563. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nBaseTextureTransform );
  564. // If we have a detail texture, then the bump texcoords are the same as the base texcoords.
  565. if( hasBump && !hasDetailTexture )
  566. {
  567. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, info.m_nBumpTransform );
  568. }
  569. if( hasEnvmapMask )
  570. {
  571. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, info.m_nEnvmapMaskTransform );
  572. }
  573. else if ( hasBump2 )
  574. {
  575. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, info.m_nBumpTransform2 );
  576. }
  577. }
  578. pContextData->m_SemiStaticCmdsOut.SetEnvMapTintPixelShaderDynamicState( 0, info.m_nEnvmapTint );
  579. // set up shader modulation color
  580. float color[4] = { 1.0, 1.0, 1.0, 1.0 };
  581. pShader->ComputeModulationColor( color );
  582. float flLScale = pShaderAPI->GetLightMapScaleFactor();
  583. color[0] *= flLScale;
  584. color[1] *= flLScale;
  585. color[2] *= flLScale;
  586. pContextData->m_SemiStaticCmdsOut.SetVertexShaderConstant( VERTEX_SHADER_MODULATION_COLOR, color );
  587. color[3] *= ( IS_PARAM_DEFINED( info.m_nAlpha2 ) && params[ info.m_nAlpha2 ]->GetFloatValue() > 0.0f ) ? params[ info.m_nAlpha2 ]->GetFloatValue() : 1.0f;
  588. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 12, color );
  589. if ( hasDetailTexture )
  590. {
  591. float detailTintAndBlend[4] = {1, 1, 1, 1};
  592. if ( info.m_nDetailTint != -1 )
  593. {
  594. params[info.m_nDetailTint]->GetVecValue( detailTintAndBlend, 3 );
  595. }
  596. detailTintAndBlend[3] = fDetailBlendFactor;
  597. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 8, detailTintAndBlend );
  598. }
  599. float envmapTintVal[4];
  600. float selfIllumTintVal[4];
  601. params[info.m_nEnvmapTint]->GetVecValue( envmapTintVal, 3 );
  602. params[info.m_nSelfIllumTint]->GetVecValue( selfIllumTintVal, 3 );
  603. float envmapContrast = params[info.m_nEnvmapContrast]->GetFloatValue();
  604. float envmapSaturation = params[info.m_nEnvmapSaturation]->GetFloatValue();
  605. float fresnelReflection = params[info.m_nFresnelReflection]->GetFloatValue();
  606. bool hasEnvmap = params[info.m_nEnvmap]->IsTexture();
  607. pContextData->m_bPixelShaderFastPath = true;
  608. bool bUsingContrast = hasEnvmap && ( (envmapContrast != 0.0f) && (envmapContrast != 1.0f) ) && (envmapSaturation != 1.0f);
  609. bool bUsingFresnel = hasEnvmap && (fresnelReflection != 1.0f);
  610. bool bUsingSelfIllumTint = IS_FLAG_SET(MATERIAL_VAR_SELFILLUM) && (selfIllumTintVal[0] != 1.0f || selfIllumTintVal[1] != 1.0f || selfIllumTintVal[2] != 1.0f);
  611. if ( bUsingContrast || bUsingFresnel || bUsingSelfIllumTint || !g_pConfig->bShowSpecular )
  612. {
  613. pContextData->m_bPixelShaderFastPath = false;
  614. }
  615. if( !pContextData->m_bPixelShaderFastPath )
  616. {
  617. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstants( 2, 3 );
  618. pContextData->m_SemiStaticCmdsOut.OutputConstantData( params[info.m_nEnvmapContrast]->GetVecValue() );
  619. pContextData->m_SemiStaticCmdsOut.OutputConstantData( params[info.m_nEnvmapSaturation]->GetVecValue() );
  620. float flFresnel = params[info.m_nFresnelReflection]->GetFloatValue();
  621. // [ 0, 0, 1-R(0), R(0) ]
  622. pContextData->m_SemiStaticCmdsOut.OutputConstantData4( 0., 0., 1.0 - flFresnel, flFresnel );
  623. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 7, params[info.m_nSelfIllumTint]->GetVecValue() );
  624. }
  625. else
  626. {
  627. if ( bHasOutline )
  628. {
  629. float flOutlineParms[8] = { GetFloatParam( info.m_nOutlineStart0, params ),
  630. GetFloatParam( info.m_nOutlineStart1, params ),
  631. GetFloatParam( info.m_nOutlineEnd0, params ),
  632. GetFloatParam( info.m_nOutlineEnd1, params ),
  633. 0,0,0,
  634. GetFloatParam( info.m_nOutlineAlpha, params ) };
  635. if ( info.m_nOutlineColor != -1 )
  636. {
  637. params[info.m_nOutlineColor]->GetVecValue( flOutlineParms + 4, 3 );
  638. }
  639. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 2, flOutlineParms, 2 );
  640. }
  641. if ( bHasSoftEdges )
  642. {
  643. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant4(
  644. 4, GetFloatParam( info.m_nEdgeSoftnessStart, params ),
  645. GetFloatParam( info.m_nEdgeSoftnessEnd, params ),
  646. 0,0 );
  647. }
  648. }
  649. // texture binds
  650. if( hasBaseTexture )
  651. {
  652. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER0, info.m_nBaseTexture, info.m_nBaseTextureFrame );
  653. }
  654. // handle mat_fullbright 2
  655. bool bLightingOnly = mat_fullbright.GetInt() == 2 && !IS_FLAG_SET( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  656. if( bLightingOnly )
  657. {
  658. // BASE TEXTURE
  659. if( hasSelfIllum )
  660. {
  661. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER0, TEXTURE_GREY_ALPHA_ZERO );
  662. }
  663. else
  664. {
  665. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER0, TEXTURE_GREY );
  666. }
  667. // BASE TEXTURE 2
  668. if( hasBaseTexture2 )
  669. {
  670. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER7, TEXTURE_GREY );
  671. }
  672. // DETAIL TEXTURE
  673. if( hasDetailTexture )
  674. {
  675. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER12, TEXTURE_GREY );
  676. }
  677. // disable color modulation
  678. float color[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  679. pContextData->m_SemiStaticCmdsOut.SetVertexShaderConstant( VERTEX_SHADER_MODULATION_COLOR, color );
  680. // turn off environment mapping
  681. envmapTintVal[0] = 0.0f;
  682. envmapTintVal[1] = 0.0f;
  683. envmapTintVal[2] = 0.0f;
  684. }
  685. // always set the transform for detail textures since I'm assuming that you'll
  686. // always have a detailscale.
  687. if( hasDetailTexture )
  688. {
  689. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, info.m_nBaseTextureTransform, info.m_nDetailScale );
  690. }
  691. if( hasBaseTexture2 )
  692. {
  693. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER7, info.m_nBaseTexture2, info.m_nBaseTexture2Frame );
  694. }
  695. if( hasDetailTexture )
  696. {
  697. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER12, info.m_nDetail, info.m_nDetailFrame );
  698. }
  699. if( hasBump || hasNormalMapAlphaEnvmapMask )
  700. {
  701. if( !g_pConfig->m_bFastNoBump )
  702. {
  703. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER4, info.m_nBumpmap, info.m_nBumpFrame );
  704. }
  705. else
  706. {
  707. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER4, TEXTURE_NORMALMAP_FLAT );
  708. }
  709. }
  710. if( hasBump2 )
  711. {
  712. if( !g_pConfig->m_bFastNoBump )
  713. {
  714. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER5, info.m_nBumpmap2, info.m_nBumpFrame2 );
  715. }
  716. else
  717. {
  718. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER5, TEXTURE_NORMALMAP_FLAT );
  719. }
  720. }
  721. if( hasBumpMask )
  722. {
  723. if( !g_pConfig->m_bFastNoBump )
  724. {
  725. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER8, info.m_nBumpMask, -1 );
  726. }
  727. else
  728. {
  729. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER8, TEXTURE_NORMALMAP_FLAT );
  730. }
  731. }
  732. if( hasEnvmapMask )
  733. {
  734. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER5, info.m_nEnvmapMask, info.m_nEnvmapMaskFrame );
  735. }
  736. if ( hasLightWarpTexture )
  737. {
  738. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER6, info.m_nLightWarpTexture, -1 );
  739. }
  740. if ( bHasBlendModulateTexture )
  741. {
  742. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER3, info.m_nBlendModulateTexture, -1 );
  743. }
  744. pContextData->m_SemiStaticCmdsOut.End();
  745. }
  746. }
  747. DYNAMIC_STATE
  748. {
  749. CCommandBufferBuilder< CFixedCommandStorageBuffer< 1000 > > DynamicCmdsOut;
  750. DynamicCmdsOut.Call( pContextData->m_pStaticCmds );
  751. DynamicCmdsOut.Call( pContextData->m_SemiStaticCmdsOut.Base() );
  752. bool hasEnvmap = params[info.m_nEnvmap]->IsTexture();
  753. if( hasEnvmap )
  754. {
  755. DynamicCmdsOut.BindTexture( pShader, SHADER_SAMPLER2, info.m_nEnvmap, info.m_nEnvmapFrame );
  756. }
  757. int nFixedLightingMode = pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_ENABLE_FIXED_LIGHTING );
  758. bool bVertexShaderFastPath = pContextData->m_bVertexShaderFastPath;
  759. if( nFixedLightingMode != 0 )
  760. {
  761. if ( pContextData->m_bPixelShaderForceFastPathBecauseOutline )
  762. nFixedLightingMode = 0;
  763. else
  764. bVertexShaderFastPath = false;
  765. }
  766. MaterialFogMode_t fogType = pShaderAPI->GetSceneFogMode();
  767. DECLARE_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_vs20 );
  768. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  769. SET_DYNAMIC_VERTEX_SHADER_COMBO( FASTPATH, bVertexShaderFastPath );
  770. SET_DYNAMIC_VERTEX_SHADER_COMBO(
  771. LIGHTING_PREVIEW,
  772. (nFixedLightingMode)?1:0
  773. );
  774. SET_DYNAMIC_VERTEX_SHADER_CMD( DynamicCmdsOut, lightmappedgeneric_vs20 );
  775. bool bPixelShaderFastPath = pContextData->m_bPixelShaderFastPath;
  776. if( nFixedLightingMode !=0 )
  777. {
  778. bPixelShaderFastPath = false;
  779. }
  780. bool bWriteDepthToAlpha;
  781. bool bWriteWaterFogToAlpha;
  782. if( pContextData->m_bFullyOpaque )
  783. {
  784. bWriteDepthToAlpha = pShaderAPI->ShouldWriteDepthToDestAlpha();
  785. bWriteWaterFogToAlpha = (fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z);
  786. AssertMsg( !(bWriteDepthToAlpha && bWriteWaterFogToAlpha), "Can't write two values to alpha at the same time." );
  787. }
  788. else
  789. {
  790. //can't write a special value to dest alpha if we're actually using as-intended alpha
  791. bWriteDepthToAlpha = false;
  792. bWriteWaterFogToAlpha = false;
  793. }
  794. float envmapContrast = params[info.m_nEnvmapContrast]->GetFloatValue();
  795. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  796. {
  797. DECLARE_DYNAMIC_PIXEL_SHADER( lightmappedgeneric_ps20b );
  798. SET_DYNAMIC_PIXEL_SHADER_COMBO( FASTPATH, bPixelShaderFastPath || pContextData->m_bPixelShaderForceFastPathBecauseOutline );
  799. SET_DYNAMIC_PIXEL_SHADER_COMBO( FASTPATHENVMAPCONTRAST, bPixelShaderFastPath && envmapContrast == 1.0f );
  800. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  801. // Don't write fog to alpha if we're using translucency
  802. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bWriteDepthToAlpha );
  803. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, bWriteWaterFogToAlpha );
  804. SET_DYNAMIC_PIXEL_SHADER_COMBO( LIGHTING_PREVIEW, nFixedLightingMode );
  805. SET_DYNAMIC_PIXEL_SHADER_CMD( DynamicCmdsOut, lightmappedgeneric_ps20b );
  806. }
  807. else
  808. {
  809. DECLARE_DYNAMIC_PIXEL_SHADER( lightmappedgeneric_ps20 );
  810. SET_DYNAMIC_PIXEL_SHADER_COMBO( FASTPATH, bPixelShaderFastPath );
  811. SET_DYNAMIC_PIXEL_SHADER_COMBO( FASTPATHENVMAPCONTRAST, bPixelShaderFastPath && envmapContrast == 1.0f );
  812. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  813. // Don't write fog to alpha if we're using translucency
  814. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, bWriteWaterFogToAlpha );
  815. SET_DYNAMIC_PIXEL_SHADER_COMBO( LIGHTING_PREVIEW, nFixedLightingMode );
  816. SET_DYNAMIC_PIXEL_SHADER_CMD( DynamicCmdsOut, lightmappedgeneric_ps20 );
  817. }
  818. if( hasFlashlight && IsX360() )
  819. {
  820. VMatrix worldToTexture;
  821. ITexture *pFlashlightDepthTexture;
  822. FlashlightState_t flashlightState = pShaderAPI->GetFlashlightStateEx( worldToTexture, &pFlashlightDepthTexture );
  823. DynamicCmdsOut.SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, worldToTexture.Base(), 4 );
  824. SetFlashLightColorFromState( flashlightState, pShaderAPI );
  825. float atten[4], pos[4];
  826. atten[0] = flashlightState.m_fConstantAtten; // Set the flashlight attenuation factors
  827. atten[1] = flashlightState.m_fLinearAtten;
  828. atten[2] = flashlightState.m_fQuadraticAtten;
  829. atten[3] = flashlightState.m_FarZ;
  830. DynamicCmdsOut.SetPixelShaderConstant( 13, atten, 1 );
  831. pos[0] = flashlightState.m_vecLightOrigin[0]; // Set the flashlight origin
  832. pos[1] = flashlightState.m_vecLightOrigin[1];
  833. pos[2] = flashlightState.m_vecLightOrigin[2];
  834. DynamicCmdsOut.SetPixelShaderConstant( 14, pos, 1 );
  835. pShader->BindTexture( SHADER_SAMPLER13, flashlightState.m_pSpotlightTexture, flashlightState.m_nSpotlightTextureFrame );
  836. if( pFlashlightDepthTexture && g_pConfig->ShadowDepthTexture() && flashlightState.m_bEnableShadows )
  837. {
  838. pShader->BindTexture( SHADER_SAMPLER14, pFlashlightDepthTexture, 0 );
  839. DynamicCmdsOut.BindStandardTexture( SHADER_SAMPLER15, TEXTURE_SHADOW_NOISE_2D );
  840. // Tweaks associated with a given flashlight
  841. float tweaks[4];
  842. tweaks[0] = ShadowFilterFromState( flashlightState );
  843. tweaks[1] = ShadowAttenFromState( flashlightState );
  844. pShader->HashShadow2DJitter( flashlightState.m_flShadowJitterSeed, &tweaks[2], &tweaks[3] );
  845. DynamicCmdsOut.SetPixelShaderConstant( 19, tweaks, 1 );
  846. // Dimensions of screen, used for screen-space noise map sampling
  847. float vScreenScale[4] = {1280.0f / 32.0f, 720.0f / 32.0f, 0, 0};
  848. int nWidth, nHeight;
  849. pShaderAPI->GetBackBufferDimensions( nWidth, nHeight );
  850. vScreenScale[0] = (float) nWidth / 32.0f;
  851. vScreenScale[1] = (float) nHeight / 32.0f;
  852. DynamicCmdsOut.SetPixelShaderConstant( 31, vScreenScale, 1 );
  853. }
  854. }
  855. DynamicCmdsOut.End();
  856. pShaderAPI->ExecuteCommandBuffer( DynamicCmdsOut.Base() );
  857. }
  858. pShader->Draw();
  859. if( IsPC() && (IS_FLAG_SET( MATERIAL_VAR_ALPHATEST ) != 0) && pContextData->m_bFullyOpaqueWithoutAlphaTest )
  860. {
  861. //Alpha testing makes it so we can't write to dest alpha
  862. //Writing to depth makes it so later polygons can't write to dest alpha either
  863. //This leads to situations with garbage in dest alpha.
  864. //Fix it now by converting depth to dest alpha for any pixels that just wrote.
  865. pShader->DrawEqualDepthToDestAlpha();
  866. }
  867. }
  868. void DrawLightmappedGeneric_DX9(CBaseVSShader *pShader, IMaterialVar** params,
  869. IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow,
  870. LightmappedGeneric_DX9_Vars_t &info,
  871. CBasePerMaterialContextData **pContextDataPtr )
  872. {
  873. bool hasFlashlight = pShader->UsingFlashlight( params );
  874. if ( !IsX360() && !r_flashlight_version2.GetInt() )
  875. {
  876. DrawLightmappedGeneric_DX9_Internal( pShader, params, hasFlashlight, pShaderAPI, pShaderShadow, info, pContextDataPtr );
  877. return;
  878. }
  879. DrawLightmappedGeneric_DX9_Internal( pShader, params, hasFlashlight, pShaderAPI, pShaderShadow, info, pContextDataPtr );
  880. }