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.

2249 lines
74 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. // This is what all vs/ps (dx8+) shaders inherit from.
  7. //===========================================================================//
  8. #if !defined(_STATIC_LINKED) || defined(STDSHADER_DX8_DLL_EXPORT) || defined(STDSHADER_DX9_DLL_EXPORT)
  9. #include "BaseVSShader.h"
  10. #include "mathlib/vmatrix.h"
  11. #include "mathlib/bumpvects.h"
  12. #include "cpp_shader_constant_register_map.h"
  13. #include "convar.h"
  14. #ifndef GAME_SHADER_DLL
  15. #ifdef HDR
  16. #include "vertexlit_and_unlit_generic_hdr_ps20.inc"
  17. #include "vertexlit_and_unlit_generic_hdr_ps20b.inc"
  18. #endif
  19. #if SUPPORT_DX8
  20. #include "lightmappedgeneric_flashlight_vs11.inc"
  21. #include "flashlight_ps11.inc"
  22. #endif
  23. #ifdef STDSHADER_DX9_DLL_EXPORT
  24. #include "lightmappedgeneric_flashlight_vs20.inc"
  25. #endif
  26. #ifdef STDSHADER_DX9_DLL_EXPORT
  27. #include "flashlight_ps20.inc"
  28. #include "flashlight_ps20b.inc"
  29. #endif
  30. #include "unlitgeneric_vs11.inc"
  31. #include "VertexLitGeneric_EnvmappedBumpmap_NoLighting_ps14.inc"
  32. #include "VertexLitGeneric_EnvmappedBumpmap_NoLighting.inc"
  33. #include "vertexlitgeneric_flashlight_vs11.inc"
  34. #include "LightmappedGeneric_BaseTexture.inc"
  35. #include "LightmappedGeneric_BumpmappedLightmap_Base_ps14.inc"
  36. #include "LightmappedGeneric_BumpmappedLightmap_Blend_ps14.inc"
  37. #include "lightmappedgeneric_bumpmappedenvmap_ps14.inc"
  38. #include "lightmappedgeneric_bumpmappedenvmap.inc"
  39. #include "lightmappedgeneric_basetextureblend.inc"
  40. #include "lightmappedgeneric_bumpmappedlightmap.inc"
  41. #endif // GAME_SHADER_DLL
  42. // memdbgon must be the last include file in a .cpp file!!!
  43. #include "tier0/memdbgon.h"
  44. static ConVar mat_fullbright( "mat_fullbright","0", FCVAR_CHEAT );
  45. // These functions are to be called from the shaders.
  46. //-----------------------------------------------------------------------------
  47. // Pixel and vertex shader constants....
  48. //-----------------------------------------------------------------------------
  49. void CBaseVSShader::SetPixelShaderConstant( int pixelReg, int constantVar, int constantVar2 )
  50. {
  51. Assert( !IsSnapshotting() );
  52. if ((!s_ppParams) || (constantVar == -1) || (constantVar2 == -1))
  53. return;
  54. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  55. Assert( pPixelVar );
  56. IMaterialVar* pPixelVar2 = s_ppParams[constantVar2];
  57. Assert( pPixelVar2 );
  58. float val[4];
  59. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  60. {
  61. pPixelVar->GetVecValue( val, 3 );
  62. }
  63. else
  64. {
  65. val[0] = val[1] = val[2] = pPixelVar->GetFloatValue();
  66. }
  67. val[3] = pPixelVar2->GetFloatValue();
  68. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  69. }
  70. void CBaseVSShader::SetPixelShaderConstantGammaToLinear( int pixelReg, int constantVar, int constantVar2 )
  71. {
  72. Assert( !IsSnapshotting() );
  73. if ((!s_ppParams) || (constantVar == -1) || (constantVar2 == -1))
  74. return;
  75. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  76. Assert( pPixelVar );
  77. IMaterialVar* pPixelVar2 = s_ppParams[constantVar2];
  78. Assert( pPixelVar2 );
  79. float val[4];
  80. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  81. {
  82. pPixelVar->GetVecValue( val, 3 );
  83. }
  84. else
  85. {
  86. val[0] = val[1] = val[2] = pPixelVar->GetFloatValue();
  87. }
  88. val[3] = pPixelVar2->GetFloatValue();
  89. val[0] = val[0] > 1.0f ? val[0] : GammaToLinear( val[0] );
  90. val[1] = val[1] > 1.0f ? val[1] : GammaToLinear( val[1] );
  91. val[2] = val[2] > 1.0f ? val[2] : GammaToLinear( val[2] );
  92. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  93. }
  94. void CBaseVSShader::SetPixelShaderConstant_W( int pixelReg, int constantVar, float fWValue )
  95. {
  96. Assert( !IsSnapshotting() );
  97. if ((!s_ppParams) || (constantVar == -1))
  98. return;
  99. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  100. Assert( pPixelVar );
  101. float val[4];
  102. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  103. pPixelVar->GetVecValue( val, 4 );
  104. else
  105. val[0] = val[1] = val[2] = val[3] = pPixelVar->GetFloatValue();
  106. val[3]=fWValue;
  107. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  108. }
  109. void CBaseVSShader::SetPixelShaderConstant( int pixelReg, int constantVar )
  110. {
  111. Assert( !IsSnapshotting() );
  112. if ((!s_ppParams) || (constantVar == -1))
  113. return;
  114. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  115. Assert( pPixelVar );
  116. float val[4];
  117. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  118. pPixelVar->GetVecValue( val, 4 );
  119. else
  120. val[0] = val[1] = val[2] = val[3] = pPixelVar->GetFloatValue();
  121. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  122. }
  123. void CBaseVSShader::SetPixelShaderConstantGammaToLinear( int pixelReg, int constantVar )
  124. {
  125. Assert( !IsSnapshotting() );
  126. if ((!s_ppParams) || (constantVar == -1))
  127. return;
  128. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  129. Assert( pPixelVar );
  130. float val[4];
  131. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  132. pPixelVar->GetVecValue( val, 4 );
  133. else
  134. val[0] = val[1] = val[2] = val[3] = pPixelVar->GetFloatValue();
  135. val[0] = val[0] > 1.0f ? val[0] : GammaToLinear( val[0] );
  136. val[1] = val[1] > 1.0f ? val[1] : GammaToLinear( val[1] );
  137. val[2] = val[2] > 1.0f ? val[2] : GammaToLinear( val[2] );
  138. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  139. }
  140. void CBaseVSShader::SetVertexShaderConstantGammaToLinear( int var, float const* pVec, int numConst, bool bForce )
  141. {
  142. int i;
  143. for( i = 0; i < numConst; i++ )
  144. {
  145. float vec[4];
  146. vec[0] = pVec[i*4+0] > 1.0f ? pVec[i*4+0] : GammaToLinear( pVec[i*4+0] );
  147. vec[1] = pVec[i*4+1] > 1.0f ? pVec[i*4+1] : GammaToLinear( pVec[i*4+1] );
  148. vec[2] = pVec[i*4+2] > 1.0f ? pVec[i*4+2] : GammaToLinear( pVec[i*4+2] );
  149. vec[3] = pVec[i*4+3];
  150. s_pShaderAPI->SetVertexShaderConstant( var + i, vec, 1, bForce );
  151. }
  152. }
  153. void CBaseVSShader::SetPixelShaderConstantGammaToLinear( int var, float const* pVec, int numConst, bool bForce )
  154. {
  155. int i;
  156. for( i = 0; i < numConst; i++ )
  157. {
  158. float vec[4];
  159. vec[0] = pVec[i*4+0] > 1.0f ? pVec[i*4+0] : GammaToLinear( pVec[i*4+0] );
  160. vec[1] = pVec[i*4+1] > 1.0f ? pVec[i*4+1] : GammaToLinear( pVec[i*4+1] );
  161. vec[2] = pVec[i*4+2] > 1.0f ? pVec[i*4+2] : GammaToLinear( pVec[i*4+2] );
  162. vec[3] = pVec[i*4+3];
  163. s_pShaderAPI->SetPixelShaderConstant( var + i, vec, 1, bForce );
  164. }
  165. }
  166. // GR - special version with fix for const/lerp issue
  167. void CBaseVSShader::SetPixelShaderConstantFudge( int pixelReg, int constantVar )
  168. {
  169. Assert( !IsSnapshotting() );
  170. if ((!s_ppParams) || (constantVar == -1))
  171. return;
  172. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  173. Assert( pPixelVar );
  174. float val[4];
  175. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  176. {
  177. pPixelVar->GetVecValue( val, 4 );
  178. val[0] = val[0] * 0.992f + 0.0078f;
  179. val[1] = val[1] * 0.992f + 0.0078f;
  180. val[2] = val[2] * 0.992f + 0.0078f;
  181. val[3] = val[3] * 0.992f + 0.0078f;
  182. }
  183. else
  184. val[0] = val[1] = val[2] = val[3] = pPixelVar->GetFloatValue() * 0.992f + 0.0078f;
  185. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  186. }
  187. void CBaseVSShader::SetVertexShaderConstant( int vertexReg, int constantVar )
  188. {
  189. Assert( !IsSnapshotting() );
  190. if ((!s_ppParams) || (constantVar == -1))
  191. return;
  192. IMaterialVar* pVertexVar = s_ppParams[constantVar];
  193. Assert( pVertexVar );
  194. float val[4];
  195. if (pVertexVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  196. pVertexVar->GetVecValue( val, 4 );
  197. else
  198. val[0] = val[1] = val[2] = val[3] = pVertexVar->GetFloatValue();
  199. s_pShaderAPI->SetVertexShaderConstant( vertexReg, val );
  200. }
  201. //-----------------------------------------------------------------------------
  202. // Sets normalized light color for pixel shaders.
  203. //-----------------------------------------------------------------------------
  204. void CBaseVSShader::SetPixelShaderLightColors( int pixelReg )
  205. {
  206. int i;
  207. int maxLights = s_pShaderAPI->GetMaxLights();
  208. for( i = 0; i < maxLights; i++ )
  209. {
  210. const LightDesc_t & lightDesc = s_pShaderAPI->GetLight( i );
  211. if( lightDesc.m_Type != MATERIAL_LIGHT_DISABLE )
  212. {
  213. Vector color( lightDesc.m_Color[0], lightDesc.m_Color[1], lightDesc.m_Color[2] );
  214. VectorNormalize( color );
  215. float val[4] = { color[0], color[1], color[2], 1.0f };
  216. s_pShaderAPI->SetPixelShaderConstant( pixelReg + i, val, 1 );
  217. }
  218. else
  219. {
  220. float zero[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  221. s_pShaderAPI->SetPixelShaderConstant( pixelReg + i, zero, 1 );
  222. }
  223. }
  224. }
  225. //-----------------------------------------------------------------------------
  226. // Sets vertex shader texture transforms
  227. //-----------------------------------------------------------------------------
  228. void CBaseVSShader::SetVertexShaderTextureTranslation( int vertexReg, int translationVar )
  229. {
  230. float offset[2] = {0, 0};
  231. IMaterialVar* pTranslationVar = s_ppParams[translationVar];
  232. if (pTranslationVar)
  233. {
  234. if (pTranslationVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  235. pTranslationVar->GetVecValue( offset, 2 );
  236. else
  237. offset[0] = offset[1] = pTranslationVar->GetFloatValue();
  238. }
  239. Vector4D translation[2];
  240. translation[0].Init( 1.0f, 0.0f, 0.0f, offset[0] );
  241. translation[1].Init( 0.0f, 1.0f, 0.0f, offset[1] );
  242. s_pShaderAPI->SetVertexShaderConstant( vertexReg, translation[0].Base(), 2 );
  243. }
  244. void CBaseVSShader::SetVertexShaderTextureScale( int vertexReg, int scaleVar )
  245. {
  246. float scale[2] = {1, 1};
  247. IMaterialVar* pScaleVar = s_ppParams[scaleVar];
  248. if (pScaleVar)
  249. {
  250. if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  251. pScaleVar->GetVecValue( scale, 2 );
  252. else if (pScaleVar->IsDefined())
  253. scale[0] = scale[1] = pScaleVar->GetFloatValue();
  254. }
  255. Vector4D scaleMatrix[2];
  256. scaleMatrix[0].Init( scale[0], 0.0f, 0.0f, 0.0f );
  257. scaleMatrix[1].Init( 0.0f, scale[1], 0.0f, 0.0f );
  258. s_pShaderAPI->SetVertexShaderConstant( vertexReg, scaleMatrix[0].Base(), 2 );
  259. }
  260. void CBaseVSShader::SetVertexShaderTextureTransform( int vertexReg, int transformVar )
  261. {
  262. Vector4D transformation[2];
  263. IMaterialVar* pTransformationVar = s_ppParams[transformVar];
  264. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  265. {
  266. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  267. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  268. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  269. }
  270. else
  271. {
  272. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  273. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  274. }
  275. s_pShaderAPI->SetVertexShaderConstant( vertexReg, transformation[0].Base(), 2 );
  276. }
  277. void CBaseVSShader::SetVertexShaderTextureScaledTransform( int vertexReg, int transformVar, int scaleVar )
  278. {
  279. Vector4D transformation[2];
  280. IMaterialVar* pTransformationVar = s_ppParams[transformVar];
  281. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  282. {
  283. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  284. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  285. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  286. }
  287. else
  288. {
  289. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  290. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  291. }
  292. Vector2D scale( 1, 1 );
  293. IMaterialVar* pScaleVar = s_ppParams[scaleVar];
  294. if (pScaleVar)
  295. {
  296. if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  297. pScaleVar->GetVecValue( scale.Base(), 2 );
  298. else if (pScaleVar->IsDefined())
  299. scale[0] = scale[1] = pScaleVar->GetFloatValue();
  300. }
  301. // Apply the scaling
  302. transformation[0][0] *= scale[0];
  303. transformation[0][1] *= scale[1];
  304. transformation[1][0] *= scale[0];
  305. transformation[1][1] *= scale[1];
  306. transformation[0][3] *= scale[0];
  307. transformation[1][3] *= scale[1];
  308. s_pShaderAPI->SetVertexShaderConstant( vertexReg, transformation[0].Base(), 2 );
  309. }
  310. //-----------------------------------------------------------------------------
  311. // Sets pixel shader texture transforms
  312. //-----------------------------------------------------------------------------
  313. void CBaseVSShader::SetPixelShaderTextureTranslation( int pixelReg, int translationVar )
  314. {
  315. float offset[2] = {0, 0};
  316. IMaterialVar* pTranslationVar = s_ppParams[translationVar];
  317. if (pTranslationVar)
  318. {
  319. if (pTranslationVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  320. pTranslationVar->GetVecValue( offset, 2 );
  321. else
  322. offset[0] = offset[1] = pTranslationVar->GetFloatValue();
  323. }
  324. Vector4D translation[2];
  325. translation[0].Init( 1.0f, 0.0f, 0.0f, offset[0] );
  326. translation[1].Init( 0.0f, 1.0f, 0.0f, offset[1] );
  327. s_pShaderAPI->SetPixelShaderConstant( pixelReg, translation[0].Base(), 2 );
  328. }
  329. void CBaseVSShader::SetPixelShaderTextureScale( int pixelReg, int scaleVar )
  330. {
  331. float scale[2] = {1, 1};
  332. IMaterialVar* pScaleVar = s_ppParams[scaleVar];
  333. if (pScaleVar)
  334. {
  335. if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  336. pScaleVar->GetVecValue( scale, 2 );
  337. else if (pScaleVar->IsDefined())
  338. scale[0] = scale[1] = pScaleVar->GetFloatValue();
  339. }
  340. Vector4D scaleMatrix[2];
  341. scaleMatrix[0].Init( scale[0], 0.0f, 0.0f, 0.0f );
  342. scaleMatrix[1].Init( 0.0f, scale[1], 0.0f, 0.0f );
  343. s_pShaderAPI->SetPixelShaderConstant( pixelReg, scaleMatrix[0].Base(), 2 );
  344. }
  345. void CBaseVSShader::SetPixelShaderTextureTransform( int pixelReg, int transformVar )
  346. {
  347. Vector4D transformation[2];
  348. IMaterialVar* pTransformationVar = s_ppParams[transformVar];
  349. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  350. {
  351. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  352. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  353. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  354. }
  355. else
  356. {
  357. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  358. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  359. }
  360. s_pShaderAPI->SetPixelShaderConstant( pixelReg, transformation[0].Base(), 2 );
  361. }
  362. void CBaseVSShader::SetPixelShaderTextureScaledTransform( int pixelReg, int transformVar, int scaleVar )
  363. {
  364. Vector4D transformation[2];
  365. IMaterialVar* pTransformationVar = s_ppParams[transformVar];
  366. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  367. {
  368. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  369. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  370. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  371. }
  372. else
  373. {
  374. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  375. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  376. }
  377. Vector2D scale( 1, 1 );
  378. IMaterialVar* pScaleVar = s_ppParams[scaleVar];
  379. if (pScaleVar)
  380. {
  381. if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  382. pScaleVar->GetVecValue( scale.Base(), 2 );
  383. else if (pScaleVar->IsDefined())
  384. scale[0] = scale[1] = pScaleVar->GetFloatValue();
  385. }
  386. // Apply the scaling
  387. transformation[0][0] *= scale[0];
  388. transformation[0][1] *= scale[1];
  389. transformation[1][0] *= scale[0];
  390. transformation[1][1] *= scale[1];
  391. transformation[0][3] *= scale[0];
  392. transformation[1][3] *= scale[1];
  393. s_pShaderAPI->SetPixelShaderConstant( pixelReg, transformation[0].Base(), 2 );
  394. }
  395. //-----------------------------------------------------------------------------
  396. // Moves a matrix into vertex shader constants
  397. //-----------------------------------------------------------------------------
  398. void CBaseVSShader::SetVertexShaderMatrix2x4( int vertexReg, int matrixVar )
  399. {
  400. IMaterialVar* pTranslationVar = s_ppParams[ matrixVar ];
  401. if ( pTranslationVar )
  402. {
  403. s_pShaderAPI->SetVertexShaderConstant( vertexReg, &pTranslationVar->GetMatrixValue()[ 0 ][ 0 ], 2 );
  404. }
  405. else
  406. {
  407. VMatrix matrix;
  408. MatrixSetIdentity( matrix );
  409. s_pShaderAPI->SetVertexShaderConstant( vertexReg, &matrix[ 0 ][ 0 ], 2 );
  410. }
  411. }
  412. void CBaseVSShader::SetVertexShaderMatrix3x4( int vertexReg, int matrixVar )
  413. {
  414. IMaterialVar* pTranslationVar = s_ppParams[matrixVar];
  415. if (pTranslationVar)
  416. {
  417. s_pShaderAPI->SetVertexShaderConstant( vertexReg, &pTranslationVar->GetMatrixValue( )[0][0], 3 );
  418. }
  419. else
  420. {
  421. VMatrix matrix;
  422. MatrixSetIdentity( matrix );
  423. s_pShaderAPI->SetVertexShaderConstant( vertexReg, &matrix[0][0], 3 );
  424. }
  425. }
  426. void CBaseVSShader::SetVertexShaderMatrix4x4( int vertexReg, int matrixVar )
  427. {
  428. IMaterialVar* pTranslationVar = s_ppParams[matrixVar];
  429. if (pTranslationVar)
  430. {
  431. s_pShaderAPI->SetVertexShaderConstant( vertexReg, &pTranslationVar->GetMatrixValue( )[0][0], 4 );
  432. }
  433. else
  434. {
  435. VMatrix matrix;
  436. MatrixSetIdentity( matrix );
  437. s_pShaderAPI->SetVertexShaderConstant( vertexReg, &matrix[0][0], 4 );
  438. }
  439. }
  440. //-----------------------------------------------------------------------------
  441. // Loads the view matrix into pixel shader constants
  442. //-----------------------------------------------------------------------------
  443. void CBaseVSShader::LoadViewMatrixIntoVertexShaderConstant( int vertexReg )
  444. {
  445. VMatrix mat, transpose;
  446. s_pShaderAPI->GetMatrix( MATERIAL_VIEW, mat.m[0] );
  447. MatrixTranspose( mat, transpose );
  448. s_pShaderAPI->SetVertexShaderConstant( vertexReg, transpose.m[0], 3 );
  449. }
  450. //-----------------------------------------------------------------------------
  451. // Loads the projection matrix into pixel shader constants
  452. //-----------------------------------------------------------------------------
  453. void CBaseVSShader::LoadProjectionMatrixIntoVertexShaderConstant( int vertexReg )
  454. {
  455. VMatrix mat, transpose;
  456. s_pShaderAPI->GetMatrix( MATERIAL_PROJECTION, mat.m[0] );
  457. MatrixTranspose( mat, transpose );
  458. s_pShaderAPI->SetVertexShaderConstant( vertexReg, transpose.m[0], 4 );
  459. }
  460. //-----------------------------------------------------------------------------
  461. // Loads the projection matrix into pixel shader constants
  462. //-----------------------------------------------------------------------------
  463. void CBaseVSShader::LoadModelViewMatrixIntoVertexShaderConstant( int vertexReg )
  464. {
  465. VMatrix view, model, modelView, transpose;
  466. s_pShaderAPI->GetMatrix( MATERIAL_MODEL, model.m[0] );
  467. MatrixTranspose( model, model );
  468. s_pShaderAPI->GetMatrix( MATERIAL_VIEW, view.m[0] );
  469. MatrixTranspose( view, view );
  470. MatrixMultiply( view, model, modelView );
  471. s_pShaderAPI->SetVertexShaderConstant( vertexReg, modelView.m[0], 3 );
  472. }
  473. //-----------------------------------------------------------------------------
  474. // Loads a scale/offset version of the viewport transform into the specified constant.
  475. //-----------------------------------------------------------------------------
  476. void CBaseVSShader::LoadViewportTransformScaledIntoVertexShaderConstant( int vertexReg )
  477. {
  478. ShaderViewport_t viewport;
  479. s_pShaderAPI->GetViewports( &viewport, 1 );
  480. int bbWidth = 0,
  481. bbHeight = 0;
  482. s_pShaderAPI->GetBackBufferDimensions( bbWidth, bbHeight );
  483. // (x, y, z, w) = (Width / bbWidth, Height / bbHeight, MinX / bbWidth, MinY / bbHeight)
  484. Vector4D viewportTransform(
  485. 1.0f * viewport.m_nWidth / bbWidth,
  486. 1.0f * viewport.m_nHeight / bbHeight,
  487. 1.0f * viewport.m_nTopLeftX / bbWidth,
  488. 1.0f * viewport.m_nTopLeftY / bbHeight
  489. );
  490. s_pShaderAPI->SetVertexShaderConstant( vertexReg, viewportTransform.Base() );
  491. }
  492. //-----------------------------------------------------------------------------
  493. // Loads bump lightmap coordinates into the pixel shader
  494. //-----------------------------------------------------------------------------
  495. void CBaseVSShader::LoadBumpLightmapCoordinateAxes_PixelShader( int pixelReg )
  496. {
  497. Vector4D basis[3];
  498. for (int i = 0; i < 3; ++i)
  499. {
  500. memcpy( &basis[i], &g_localBumpBasis[i], 3 * sizeof(float) );
  501. basis[i][3] = 0.0f;
  502. }
  503. s_pShaderAPI->SetPixelShaderConstant( pixelReg, (float*)basis, 3 );
  504. }
  505. //-----------------------------------------------------------------------------
  506. // Loads bump lightmap coordinates into the pixel shader
  507. //-----------------------------------------------------------------------------
  508. void CBaseVSShader::LoadBumpLightmapCoordinateAxes_VertexShader( int vertexReg )
  509. {
  510. Vector4D basis[3];
  511. // transpose
  512. int i;
  513. for (i = 0; i < 3; ++i)
  514. {
  515. basis[i][0] = g_localBumpBasis[0][i];
  516. basis[i][1] = g_localBumpBasis[1][i];
  517. basis[i][2] = g_localBumpBasis[2][i];
  518. basis[i][3] = 0.0f;
  519. }
  520. s_pShaderAPI->SetVertexShaderConstant( vertexReg, (float*)basis, 3 );
  521. for (i = 0; i < 3; ++i)
  522. {
  523. memcpy( &basis[i], &g_localBumpBasis[i], 3 * sizeof(float) );
  524. basis[i][3] = 0.0f;
  525. }
  526. s_pShaderAPI->SetVertexShaderConstant( vertexReg + 3, (float*)basis, 3 );
  527. }
  528. //-----------------------------------------------------------------------------
  529. // Helper methods for pixel shader overbrighting
  530. //-----------------------------------------------------------------------------
  531. void CBaseVSShader::EnablePixelShaderOverbright( int reg, bool bEnable, bool bDivideByTwo )
  532. {
  533. // can't have other overbright values with pixel shaders as it stands.
  534. float v[4];
  535. if( bEnable )
  536. {
  537. v[0] = v[1] = v[2] = v[3] = bDivideByTwo ? OVERBRIGHT / 2.0f : OVERBRIGHT;
  538. }
  539. else
  540. {
  541. v[0] = v[1] = v[2] = v[3] = bDivideByTwo ? 1.0f / 2.0f : 1.0f;
  542. }
  543. s_pShaderAPI->SetPixelShaderConstant( reg, v, 1 );
  544. }
  545. //-----------------------------------------------------------------------------
  546. // Helper for dealing with modulation
  547. //-----------------------------------------------------------------------------
  548. void CBaseVSShader::SetModulationVertexShaderDynamicState()
  549. {
  550. float color[4] = { 1.0, 1.0, 1.0, 1.0 };
  551. ComputeModulationColor( color );
  552. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_MODULATION_COLOR, color );
  553. }
  554. void CBaseVSShader::SetModulationPixelShaderDynamicState( int modulationVar )
  555. {
  556. float color[4] = { 1.0, 1.0, 1.0, 1.0 };
  557. ComputeModulationColor( color );
  558. s_pShaderAPI->SetPixelShaderConstant( modulationVar, color );
  559. }
  560. void CBaseVSShader::SetModulationPixelShaderDynamicState_LinearColorSpace( int modulationVar )
  561. {
  562. float color[4] = { 1.0, 1.0, 1.0, 1.0 };
  563. ComputeModulationColor( color );
  564. color[0] = color[0] > 1.0f ? color[0] : GammaToLinear( color[0] );
  565. color[1] = color[1] > 1.0f ? color[1] : GammaToLinear( color[1] );
  566. color[2] = color[2] > 1.0f ? color[2] : GammaToLinear( color[2] );
  567. s_pShaderAPI->SetPixelShaderConstant( modulationVar, color );
  568. }
  569. void CBaseVSShader::SetModulationPixelShaderDynamicState_LinearColorSpace_LinearScale( int modulationVar, float flScale )
  570. {
  571. float color[4] = { 1.0, 1.0, 1.0, 1.0 };
  572. ComputeModulationColor( color );
  573. color[0] = ( color[0] > 1.0f ? color[0] : GammaToLinear( color[0] ) ) * flScale;
  574. color[1] = ( color[1] > 1.0f ? color[1] : GammaToLinear( color[1] ) ) * flScale;
  575. color[2] = ( color[2] > 1.0f ? color[2] : GammaToLinear( color[2] ) ) * flScale;
  576. s_pShaderAPI->SetPixelShaderConstant( modulationVar, color );
  577. }
  578. //-----------------------------------------------------------------------------
  579. // Converts a color + alpha into a vector4
  580. //-----------------------------------------------------------------------------
  581. void CBaseVSShader::ColorVarsToVector( int colorVar, int alphaVar, Vector4D &color )
  582. {
  583. color.Init( 1.0, 1.0, 1.0, 1.0 );
  584. if ( colorVar != -1 )
  585. {
  586. IMaterialVar* pColorVar = s_ppParams[colorVar];
  587. if ( pColorVar->GetType() == MATERIAL_VAR_TYPE_VECTOR )
  588. {
  589. pColorVar->GetVecValue( color.Base(), 3 );
  590. }
  591. else
  592. {
  593. color[0] = color[1] = color[2] = pColorVar->GetFloatValue();
  594. }
  595. }
  596. if ( alphaVar != -1 )
  597. {
  598. float flAlpha = s_ppParams[alphaVar]->GetFloatValue();
  599. color[3] = clamp( flAlpha, 0.0f, 1.0f );
  600. }
  601. }
  602. //-----------------------------------------------------------------------------
  603. // Sets a color + alpha into shader constants
  604. //-----------------------------------------------------------------------------
  605. void CBaseVSShader::SetColorVertexShaderConstant( int nVertexReg, int colorVar, int alphaVar )
  606. {
  607. Vector4D color;
  608. ColorVarsToVector( colorVar, alphaVar, color );
  609. s_pShaderAPI->SetVertexShaderConstant( nVertexReg, color.Base() );
  610. }
  611. void CBaseVSShader::SetColorPixelShaderConstant( int nPixelReg, int colorVar, int alphaVar )
  612. {
  613. Vector4D color;
  614. ColorVarsToVector( colorVar, alphaVar, color );
  615. s_pShaderAPI->SetPixelShaderConstant( nPixelReg, color.Base() );
  616. }
  617. #ifdef _DEBUG
  618. ConVar mat_envmaptintoverride( "mat_envmaptintoverride", "-1" );
  619. ConVar mat_envmaptintscale( "mat_envmaptintscale", "-1" );
  620. #endif
  621. //-----------------------------------------------------------------------------
  622. // Helpers for dealing with envmap tint
  623. //-----------------------------------------------------------------------------
  624. // set alphaVar to -1 to ignore it.
  625. void CBaseVSShader::SetEnvMapTintPixelShaderDynamicState( int pixelReg, int tintVar, int alphaVar, bool bConvertFromGammaToLinear )
  626. {
  627. float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  628. if( g_pConfig->bShowSpecular && mat_fullbright.GetInt() != 2 )
  629. {
  630. IMaterialVar* pAlphaVar = NULL;
  631. if( alphaVar >= 0 )
  632. {
  633. pAlphaVar = s_ppParams[alphaVar];
  634. }
  635. if( pAlphaVar )
  636. {
  637. color[3] = pAlphaVar->GetFloatValue();
  638. }
  639. IMaterialVar* pTintVar = s_ppParams[tintVar];
  640. #ifdef _DEBUG
  641. pTintVar->GetVecValue( color, 3 );
  642. float envmapTintOverride = mat_envmaptintoverride.GetFloat();
  643. float envmapTintScaleOverride = mat_envmaptintscale.GetFloat();
  644. if( envmapTintOverride != -1.0f )
  645. {
  646. color[0] = color[1] = color[2] = envmapTintOverride;
  647. }
  648. if( envmapTintScaleOverride != -1.0f )
  649. {
  650. color[0] *= envmapTintScaleOverride;
  651. color[1] *= envmapTintScaleOverride;
  652. color[2] *= envmapTintScaleOverride;
  653. }
  654. if( bConvertFromGammaToLinear )
  655. {
  656. color[0] = color[0] > 1.0f ? color[0] : GammaToLinear( color[0] );
  657. color[1] = color[1] > 1.0f ? color[1] : GammaToLinear( color[1] );
  658. color[2] = color[2] > 1.0f ? color[2] : GammaToLinear( color[2] );
  659. }
  660. #else
  661. if( bConvertFromGammaToLinear )
  662. {
  663. pTintVar->GetLinearVecValue( color, 3 );
  664. }
  665. else
  666. {
  667. pTintVar->GetVecValue( color, 3 );
  668. }
  669. #endif
  670. }
  671. else
  672. {
  673. color[0] = color[1] = color[2] = color[3] = 0.0f;
  674. }
  675. s_pShaderAPI->SetPixelShaderConstant( pixelReg, color, 1 );
  676. }
  677. void CBaseVSShader::SetAmbientCubeDynamicStateVertexShader( )
  678. {
  679. s_pShaderAPI->SetVertexShaderStateAmbientLightCube();
  680. }
  681. float CBaseVSShader::GetAmbientLightCubeLuminance( )
  682. {
  683. return s_pShaderAPI->GetAmbientLightCubeLuminance();
  684. }
  685. #ifndef GAME_SHADER_DLL
  686. const char *CBaseVSShader::UnlitGeneric_ComputePixelShaderName( bool bMask,
  687. bool bEnvmap,
  688. bool bBaseTexture,
  689. bool bBaseAlphaEnvmapMask,
  690. bool bDetail,
  691. bool bDetailMultiplyMode,
  692. bool bMaskBaseByDetailAlpha )
  693. {
  694. static char const* s_pPixelShaders[] =
  695. {
  696. "UnlitGeneric_NoTexture",
  697. "UnlitGeneric",
  698. "UnlitGeneric_EnvMapNoTexture",
  699. "UnlitGeneric_EnvMap",
  700. "UnlitGeneric_NoTexture",
  701. "UnlitGeneric",
  702. "UnlitGeneric_EnvMapMaskNoTexture",
  703. "UnlitGeneric_EnvMapMask",
  704. // Detail texture
  705. // The other commented-out versions are used if we want to
  706. // apply the detail *after* the environment map is added
  707. "UnlitGeneric_DetailNoTexture",
  708. "UnlitGeneric_Detail",
  709. "UnlitGeneric_EnvMapNoTexture", //"UnlitGeneric_DetailEnvMapNoTexture",
  710. "UnlitGeneric_DetailEnvMap",
  711. "UnlitGeneric_DetailNoTexture",
  712. "UnlitGeneric_Detail",
  713. "UnlitGeneric_EnvMapMaskNoTexture", //"UnlitGeneric_DetailEnvMapMaskNoTexture",
  714. "UnlitGeneric_DetailEnvMapMask",
  715. };
  716. // handle hud elements
  717. if ( bDetail & bDetailMultiplyMode )
  718. return "alphadist_ps11";
  719. if ( bDetail & bMaskBaseByDetailAlpha )
  720. return "UnlitGeneric_MaskBaseByDetailAlpha_ps11";
  721. if (!bMask && bEnvmap && bBaseTexture && bBaseAlphaEnvmapMask)
  722. {
  723. if (!bDetail)
  724. return "UnlitGeneric_BaseAlphaMaskedEnvMap";
  725. else
  726. return "UnlitGeneric_DetailBaseAlphaMaskedEnvMap";
  727. }
  728. else
  729. {
  730. int pshIndex = 0;
  731. if (bBaseTexture)
  732. pshIndex |= 0x1;
  733. if (bEnvmap)
  734. pshIndex |= 0x2;
  735. if (bMask)
  736. pshIndex |= 0x4;
  737. if (bDetail)
  738. pshIndex |= 0x8;
  739. return s_pPixelShaders[pshIndex];
  740. }
  741. }
  742. //-----------------------------------------------------------------------------
  743. // Sets up hw morphing state for the vertex shader
  744. //-----------------------------------------------------------------------------
  745. void CBaseVSShader::SetHWMorphVertexShaderState( int nDimConst, int nSubrectConst, VertexTextureSampler_t morphSampler )
  746. {
  747. #ifndef _X360
  748. if ( !s_pShaderAPI->IsHWMorphingEnabled() )
  749. return;
  750. int nMorphWidth, nMorphHeight;
  751. s_pShaderAPI->GetStandardTextureDimensions( &nMorphWidth, &nMorphHeight, TEXTURE_MORPH_ACCUMULATOR );
  752. int nDim = s_pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_MORPH_ACCUMULATOR_4TUPLE_COUNT );
  753. float pMorphAccumSize[4] = { (float)nMorphWidth, (float)nMorphHeight, (float)nDim, 0.0f };
  754. s_pShaderAPI->SetVertexShaderConstant( nDimConst, pMorphAccumSize );
  755. int nXOffset = s_pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_MORPH_ACCUMULATOR_X_OFFSET );
  756. int nYOffset = s_pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_MORPH_ACCUMULATOR_Y_OFFSET );
  757. int nWidth = s_pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_MORPH_ACCUMULATOR_SUBRECT_WIDTH );
  758. int nHeight = s_pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_MORPH_ACCUMULATOR_SUBRECT_HEIGHT );
  759. float pMorphAccumSubrect[4] = { (float)nXOffset, (float)nYOffset, (float)nWidth, (float)nHeight };
  760. s_pShaderAPI->SetVertexShaderConstant( nSubrectConst, pMorphAccumSubrect );
  761. s_pShaderAPI->BindStandardVertexTexture( morphSampler, TEXTURE_MORPH_ACCUMULATOR );
  762. #endif
  763. }
  764. //-----------------------------------------------------------------------------
  765. // Vertex shader unlit generic pass
  766. //-----------------------------------------------------------------------------
  767. void CBaseVSShader::VertexShaderUnlitGenericPass( int baseTextureVar, int frameVar,
  768. int baseTextureTransformVar,
  769. int detailVar, int detailTransform,
  770. bool bDetailTransformIsScale,
  771. int envmapVar, int envMapFrameVar,
  772. int envmapMaskVar, int envmapMaskFrameVar,
  773. int envmapMaskScaleVar, int envmapTintVar,
  774. int alphaTestReferenceVar,
  775. int nDetailBlendModeVar,
  776. int nOutlineVar,
  777. int nOutlineColorVar,
  778. int nOutlineStartVar,
  779. int nOutlineEndVar,
  780. int nSeparateDetailUVsVar )
  781. {
  782. IMaterialVar** params = s_ppParams;
  783. bool bBaseAlphaEnvmapMask = IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK);
  784. bool bEnvmap = (envmapVar >= 0) && params[envmapVar]->IsTexture();
  785. bool bMask = false;
  786. if (bEnvmap && (envmapMaskVar >= 0))
  787. {
  788. bMask = params[envmapMaskVar]->IsTexture();
  789. }
  790. bool bDetail = (detailVar >= 0) && params[detailVar]->IsTexture();
  791. bool bBaseTexture = (baseTextureVar >= 0) && params[baseTextureVar]->IsTexture();
  792. bool bVertexColor = IS_FLAG_SET(MATERIAL_VAR_VERTEXCOLOR);
  793. bool bEnvmapCameraSpace = IS_FLAG_SET(MATERIAL_VAR_ENVMAPCAMERASPACE);
  794. bool bEnvmapSphere = IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE);
  795. bool bDetailMultiply = ( nDetailBlendModeVar >= 0 ) && ( params[nDetailBlendModeVar]->GetIntValue() == 8 );
  796. bool bMaskBaseByDetailAlpha = ( nDetailBlendModeVar >= 0 ) && ( params[nDetailBlendModeVar]->GetIntValue() == 9 );
  797. bool bSeparateDetailUVs = ( nSeparateDetailUVsVar >= 0 ) && ( params[nSeparateDetailUVsVar]->GetIntValue() != 0 );
  798. if (IsSnapshotting())
  799. {
  800. // Alpha test
  801. s_pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  802. if( alphaTestReferenceVar != -1 && params[alphaTestReferenceVar]->GetFloatValue() > 0.0f )
  803. {
  804. s_pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, params[alphaTestReferenceVar]->GetFloatValue() );
  805. }
  806. // Base texture on stage 0
  807. if (bBaseTexture)
  808. {
  809. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  810. }
  811. if (bDetail)
  812. {
  813. s_pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  814. }
  815. if (bEnvmap)
  816. {
  817. // envmap on stage 1
  818. s_pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  819. // envmapmask on stage 2
  820. if (bMask || bBaseAlphaEnvmapMask )
  821. s_pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  822. }
  823. if (bBaseTexture)
  824. SetDefaultBlendingShadowState( baseTextureVar, true );
  825. else if (bMask)
  826. SetDefaultBlendingShadowState( envmapMaskVar, false );
  827. else
  828. SetDefaultBlendingShadowState();
  829. int fmt = VERTEX_POSITION;
  830. if( bEnvmap )
  831. fmt |= VERTEX_NORMAL;
  832. if ( bVertexColor )
  833. fmt |= VERTEX_COLOR;
  834. int numTexCoords = 1;
  835. if( bSeparateDetailUVs )
  836. {
  837. numTexCoords = 2;
  838. }
  839. s_pShaderShadow->VertexShaderVertexFormat( fmt, numTexCoords, 0, 0 );
  840. const char *pshName = UnlitGeneric_ComputePixelShaderName(
  841. bMask,
  842. bEnvmap,
  843. bBaseTexture,
  844. bBaseAlphaEnvmapMask,
  845. bDetail,
  846. bDetailMultiply,
  847. bMaskBaseByDetailAlpha );
  848. s_pShaderShadow->SetPixelShader( pshName );
  849. // Compute the vertex shader index.
  850. unlitgeneric_vs11_Static_Index vshIndex;
  851. vshIndex.SetDETAIL( bDetail );
  852. vshIndex.SetENVMAP( bEnvmap );
  853. vshIndex.SetENVMAPCAMERASPACE( bEnvmap && bEnvmapCameraSpace );
  854. vshIndex.SetENVMAPSPHERE( bEnvmap && bEnvmapSphere );
  855. vshIndex.SetVERTEXCOLOR( bVertexColor );
  856. vshIndex.SetSEPARATEDETAILUVS( bSeparateDetailUVs );
  857. s_pShaderShadow->SetVertexShader( "unlitgeneric_vs11", vshIndex.GetIndex() );
  858. DefaultFog();
  859. }
  860. else
  861. {
  862. if ( s_pShaderAPI->InFlashlightMode() ) // Not snapshotting && flashlight pass
  863. {
  864. Draw( false );
  865. return;
  866. }
  867. if (bBaseTexture)
  868. {
  869. BindTexture( SHADER_SAMPLER0, baseTextureVar, frameVar );
  870. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, baseTextureTransformVar );
  871. }
  872. if (bDetail)
  873. {
  874. BindTexture( SHADER_SAMPLER3, detailVar, frameVar );
  875. if (bDetailTransformIsScale)
  876. {
  877. SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, baseTextureTransformVar, detailTransform );
  878. }
  879. else
  880. {
  881. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, detailTransform );
  882. }
  883. }
  884. if (bEnvmap)
  885. {
  886. BindTexture( SHADER_SAMPLER1, envmapVar, envMapFrameVar );
  887. if (bMask || bBaseAlphaEnvmapMask)
  888. {
  889. if (bMask)
  890. BindTexture( SHADER_SAMPLER2, envmapMaskVar, envmapMaskFrameVar );
  891. else
  892. BindTexture( SHADER_SAMPLER2, baseTextureVar, frameVar );
  893. SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, baseTextureTransformVar, envmapMaskScaleVar );
  894. }
  895. SetEnvMapTintPixelShaderDynamicState( 2, envmapTintVar, -1 );
  896. if (bEnvmapSphere || IS_FLAG_SET(MATERIAL_VAR_ENVMAPCAMERASPACE))
  897. {
  898. LoadViewMatrixIntoVertexShaderConstant( VERTEX_SHADER_VIEWMODEL );
  899. }
  900. }
  901. SetModulationVertexShaderDynamicState();
  902. float flConsts[12]={ 0, 0, 0, 1, // color
  903. 0, 0, 0, 0, // max
  904. 0, 0, 0, .5, // min
  905. };
  906. // set up outline pixel shader constants
  907. if ( bDetailMultiply && ( nOutlineVar != -1 ) && ( params[nOutlineVar]->GetIntValue() ) )
  908. {
  909. if ( nOutlineColorVar != -1 )
  910. params[nOutlineColorVar]->GetVecValue( flConsts, 3 );
  911. if ( nOutlineEndVar != -1 )
  912. flConsts[7] = params[nOutlineEndVar]->GetFloatValue();
  913. if ( nOutlineStartVar != -1 )
  914. flConsts[11] = params[nOutlineStartVar]->GetFloatValue();
  915. }
  916. s_pShaderAPI->SetPixelShaderConstant( 0, flConsts, 3 );
  917. // Compute the vertex shader index.
  918. unlitgeneric_vs11_Dynamic_Index vshIndex;
  919. vshIndex.SetDOWATERFOG( s_pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  920. vshIndex.SetSKINNING( s_pShaderAPI->GetCurrentNumBones() > 0 );
  921. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  922. }
  923. Draw();
  924. }
  925. void CBaseVSShader::DrawWorldBaseTexture( int baseTextureVar, int baseTextureTransformVar,
  926. int frameVar, int colorVar, int alphaVar )
  927. {
  928. if( IsSnapshotting() )
  929. {
  930. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  931. s_pShaderShadow->VertexShaderVertexFormat(
  932. VERTEX_POSITION, 1, 0, 0 );
  933. s_pShaderShadow->SetPixelShader( "LightmappedGeneric_BaseTexture" );
  934. SetNormalBlendingShadowState();
  935. lightmappedgeneric_basetexture_Static_Index vshIndex;
  936. s_pShaderShadow->SetVertexShader( "LightmappedGeneric_BaseTexture", vshIndex.GetIndex() );
  937. FogToOOOverbright();
  938. }
  939. else
  940. {
  941. IMaterialVar** params = s_ppParams;
  942. bool bLightingOnly = mat_fullbright.GetInt() == 2 && !IS_FLAG_SET( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  943. if( bLightingOnly )
  944. {
  945. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_GREY );
  946. }
  947. else
  948. {
  949. BindTexture( SHADER_SAMPLER0, baseTextureVar, frameVar );
  950. }
  951. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, baseTextureTransformVar );
  952. SetColorPixelShaderConstant( 0, colorVar, alphaVar );
  953. lightmappedgeneric_basetexture_Dynamic_Index vshIndex;
  954. vshIndex.SetDOWATERFOG( s_pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  955. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  956. }
  957. Draw();
  958. }
  959. void CBaseVSShader::DrawWorldBumpedDiffuseLighting( int bumpmapVar, int bumpFrameVar,
  960. int bumpTransformVar, bool bMultiply,
  961. bool bSSBump )
  962. {
  963. if( IsSnapshotting() )
  964. {
  965. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  966. s_pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  967. s_pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  968. s_pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  969. if( bMultiply )
  970. {
  971. s_pShaderShadow->EnableBlending( true );
  972. SingleTextureLightmapBlendMode();
  973. }
  974. s_pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION, 3, 0, 0 );
  975. lightmappedgeneric_bumpmappedlightmap_Static_Index vshIndex;
  976. s_pShaderShadow->SetVertexShader( "LightmappedGeneric_BumpmappedLightmap", vshIndex.GetIndex() );
  977. if ( bSSBump )
  978. s_pShaderShadow->SetPixelShader( "LightmappedGeneric_SSBumpmappedLightmap" );
  979. else
  980. s_pShaderShadow->SetPixelShader( "LightmappedGeneric_BumpmappedLightmap" );
  981. FogToFogColor();
  982. }
  983. else
  984. {
  985. if( !g_pConfig->m_bFastNoBump )
  986. {
  987. BindTexture( SHADER_SAMPLER0, bumpmapVar, bumpFrameVar );
  988. }
  989. else
  990. {
  991. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_NORMALMAP_FLAT );
  992. }
  993. LoadBumpLightmapCoordinateAxes_PixelShader( 0 );
  994. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP_BUMPED );
  995. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, bumpTransformVar );
  996. SetModulationPixelShaderDynamicState( 3 );
  997. lightmappedgeneric_bumpmappedlightmap_Dynamic_Index vshIndex;
  998. vshIndex.SetDOWATERFOG( s_pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  999. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  1000. }
  1001. Draw();
  1002. }
  1003. void CBaseVSShader::DrawWorldBumpedDiffuseLighting_Base_ps14( int bumpmapVar, int bumpFrameVar,
  1004. int bumpTransformVar,
  1005. int baseTextureVar, int baseTextureTransformVar, int frameVar )
  1006. {
  1007. if( IsSnapshotting() )
  1008. {
  1009. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  1010. s_pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  1011. s_pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  1012. s_pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  1013. s_pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  1014. s_pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION, 3, 0, 0 );
  1015. lightmappedgeneric_bumpmappedlightmap_base_ps14_Static_Index vshIndex;
  1016. s_pShaderShadow->SetVertexShader( "LightmappedGeneric_BumpmappedLightmap_Base_ps14", vshIndex.GetIndex() );
  1017. s_pShaderShadow->SetPixelShader( "LightmappedGeneric_BumpmappedLightmap_Base_ps14" );
  1018. FogToFogColor();
  1019. }
  1020. else
  1021. {
  1022. if( !g_pConfig->m_bFastNoBump )
  1023. {
  1024. BindTexture( SHADER_SAMPLER0, bumpmapVar, bumpFrameVar );
  1025. }
  1026. else
  1027. {
  1028. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_NORMALMAP_FLAT );
  1029. }
  1030. LoadBumpLightmapCoordinateAxes_PixelShader( 0 );
  1031. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP_BUMPED );
  1032. BindTexture( SHADER_SAMPLER4, baseTextureVar, frameVar );
  1033. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, bumpTransformVar );
  1034. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, baseTextureTransformVar );
  1035. SetModulationPixelShaderDynamicState( 3 );
  1036. lightmappedgeneric_bumpmappedlightmap_base_ps14_Dynamic_Index vshIndex;
  1037. vshIndex.SetDOWATERFOG( s_pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  1038. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  1039. }
  1040. Draw();
  1041. }
  1042. void CBaseVSShader::DrawWorldBumpedDiffuseLighting_Blend_ps14( int bumpmapVar, int bumpFrameVar,
  1043. int bumpTransformVar,
  1044. int baseTextureVar, int baseTextureTransformVar,
  1045. int baseTextureFrameVar,
  1046. int baseTexture2Var, int baseTextureTransform2Var,
  1047. int baseTextureFrame2Var)
  1048. {
  1049. if( IsSnapshotting() )
  1050. {
  1051. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  1052. s_pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  1053. s_pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  1054. s_pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  1055. s_pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  1056. s_pShaderShadow->EnableTexture( SHADER_SAMPLER5, true );
  1057. s_pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION, 3, 0, 0 );
  1058. lightmappedgeneric_bumpmappedlightmap_blend_ps14_Static_Index vshIndex;
  1059. s_pShaderShadow->SetVertexShader( "LightmappedGeneric_BumpmappedLightmap_Blend_ps14", vshIndex.GetIndex() );
  1060. s_pShaderShadow->SetPixelShader( "LightmappedGeneric_BumpmappedLightmap_Blend_ps14" );
  1061. FogToFogColor();
  1062. }
  1063. else
  1064. {
  1065. if( !g_pConfig->m_bFastNoBump )
  1066. {
  1067. BindTexture( SHADER_SAMPLER0, bumpmapVar, bumpFrameVar );
  1068. }
  1069. else
  1070. {
  1071. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_NORMALMAP_FLAT );
  1072. }
  1073. LoadBumpLightmapCoordinateAxes_PixelShader( 0 );
  1074. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP_BUMPED );
  1075. BindTexture( SHADER_SAMPLER4, baseTextureVar, baseTextureFrameVar );
  1076. BindTexture( SHADER_SAMPLER5, baseTexture2Var, baseTextureFrame2Var );
  1077. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, bumpTransformVar );
  1078. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, baseTextureTransformVar );
  1079. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, baseTextureTransform2Var );
  1080. SetModulationPixelShaderDynamicState( 3 );
  1081. lightmappedgeneric_bumpmappedlightmap_blend_ps14_Dynamic_Index vshIndex;
  1082. vshIndex.SetDOWATERFOG( s_pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  1083. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  1084. }
  1085. Draw();
  1086. }
  1087. //#define USE_DEST_ALPHA
  1088. #define USE_NORMALMAP_ALPHA
  1089. void CBaseVSShader::DrawWorldBumpedSpecularLighting( int bumpmapVar, int envmapVar,
  1090. int bumpFrameVar, int envmapFrameVar,
  1091. int envmapTintVar, int alphaVar,
  1092. int envmapContrastVar, int envmapSaturationVar,
  1093. int bumpTransformVar, int fresnelReflectionVar,
  1094. bool bBlend, bool bNoWriteZ )
  1095. {
  1096. // + BUMPED CUBEMAP
  1097. if( IsSnapshotting() )
  1098. {
  1099. SetInitialShadowState( );
  1100. if ( bNoWriteZ )
  1101. {
  1102. s_pShaderShadow->EnableDepthWrites( false );
  1103. }
  1104. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  1105. s_pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  1106. if( g_pHardwareConfig->SupportsPixelShaders_1_4() )
  1107. {
  1108. s_pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  1109. }
  1110. if( bBlend )
  1111. {
  1112. s_pShaderShadow->EnableBlending( true );
  1113. s_pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  1114. }
  1115. // FIXME: Remove the normal (needed for tangent space gen)
  1116. s_pShaderShadow->VertexShaderVertexFormat(
  1117. VERTEX_POSITION | VERTEX_NORMAL | VERTEX_TANGENT_S |
  1118. VERTEX_TANGENT_T, 1, 0, 0 );
  1119. IMaterialVar** params = s_ppParams;
  1120. bool bHasNormalMapAlphaEnvMapMask = IS_FLAG_SET( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );
  1121. if( g_pHardwareConfig->SupportsPixelShaders_1_4() )
  1122. {
  1123. lightmappedgeneric_bumpmappedenvmap_ps14_Static_Index vshIndex;
  1124. s_pShaderShadow->SetVertexShader( "LightmappedGeneric_BumpmappedEnvmap_ps14", vshIndex.GetIndex() );
  1125. int nPshIndex = bHasNormalMapAlphaEnvMapMask ? 1 : 0;
  1126. s_pShaderShadow->SetPixelShader( "LightmappedGeneric_BumpmappedEnvmap_ps14", nPshIndex );
  1127. }
  1128. else
  1129. {
  1130. lightmappedgeneric_bumpmappedenvmap_Static_Index vshIndex;
  1131. s_pShaderShadow->SetVertexShader( "LightmappedGeneric_BumpmappedEnvmap", vshIndex.GetIndex() );
  1132. int nPshIndex = bHasNormalMapAlphaEnvMapMask ? 1 : 0;
  1133. s_pShaderShadow->SetPixelShader( "LightmappedGeneric_BumpmappedEnvmap", nPshIndex );
  1134. }
  1135. FogToBlack();
  1136. }
  1137. else
  1138. {
  1139. IMaterialVar** params = s_ppParams;
  1140. s_pShaderAPI->SetDefaultState();
  1141. BindTexture( SHADER_SAMPLER0, bumpmapVar, bumpFrameVar );
  1142. BindTexture( SHADER_SAMPLER3, envmapVar, envmapFrameVar );
  1143. if( g_pHardwareConfig->SupportsPixelShaders_1_4() )
  1144. {
  1145. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER4, TEXTURE_NORMALIZATION_CUBEMAP );
  1146. lightmappedgeneric_bumpmappedenvmap_ps14_Dynamic_Index vshIndex;
  1147. vshIndex.SetDOWATERFOG( s_pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  1148. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  1149. }
  1150. else
  1151. {
  1152. lightmappedgeneric_bumpmappedenvmap_Dynamic_Index vshIndex;
  1153. vshIndex.SetDOWATERFOG( s_pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  1154. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  1155. }
  1156. SetEnvMapTintPixelShaderDynamicState( 0, envmapTintVar, alphaVar );
  1157. // GR - fudge consts a bit to fix const/lerp issues
  1158. SetPixelShaderConstantFudge( 1, envmapContrastVar );
  1159. SetPixelShaderConstantFudge( 2, envmapSaturationVar );
  1160. float greyWeights[4] = { 0.299f, 0.587f, 0.114f, 0.0f };
  1161. s_pShaderAPI->SetPixelShaderConstant( 3, greyWeights );
  1162. // [ 0, 0 ,0, R(0) ]
  1163. float fresnel[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  1164. fresnel[3] = params[fresnelReflectionVar]->GetFloatValue();
  1165. s_pShaderAPI->SetPixelShaderConstant( 4, fresnel );
  1166. // [ 0, 0 ,0, 1-R(0) ]
  1167. fresnel[3] = 1.0f - fresnel[3];
  1168. s_pShaderAPI->SetPixelShaderConstant( 6, fresnel );
  1169. float one[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  1170. s_pShaderAPI->SetPixelShaderConstant( 5, one );
  1171. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, bumpTransformVar );
  1172. }
  1173. Draw();
  1174. }
  1175. void CBaseVSShader::DrawModelBumpedSpecularLighting( int bumpMapVar, int bumpMapFrameVar,
  1176. int envMapVar, int envMapVarFrame,
  1177. int envMapTintVar, int alphaVar,
  1178. int envMapContrastVar, int envMapSaturationVar,
  1179. int bumpTransformVar,
  1180. bool bBlendSpecular, bool bNoWriteZ )
  1181. {
  1182. IMaterialVar** params = s_ppParams;
  1183. if( IsSnapshotting() )
  1184. {
  1185. SetInitialShadowState( );
  1186. if ( bNoWriteZ )
  1187. {
  1188. s_pShaderShadow->EnableDepthWrites( false );
  1189. }
  1190. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  1191. s_pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  1192. if( g_pHardwareConfig->SupportsPixelShaders_1_4() )
  1193. {
  1194. s_pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  1195. }
  1196. s_pShaderShadow->EnableAlphaTest( false );
  1197. if( bBlendSpecular )
  1198. {
  1199. s_pShaderShadow->EnableBlending( true );
  1200. SetAdditiveBlendingShadowState( -1, false );
  1201. }
  1202. else
  1203. {
  1204. s_pShaderShadow->EnableBlending( false );
  1205. SetNormalBlendingShadowState( -1, false );
  1206. }
  1207. s_pShaderShadow->VertexShaderVertexFormat(
  1208. VERTEX_POSITION | VERTEX_NORMAL, 1, 0, 4 /* userDataSize */ );
  1209. bool bHasNormalMapAlphaEnvMapMask = IS_FLAG_SET( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );
  1210. if( g_pHardwareConfig->SupportsPixelShaders_1_4() )
  1211. {
  1212. vertexlitgeneric_envmappedbumpmap_nolighting_ps14_Static_Index vshIndex;
  1213. s_pShaderShadow->SetVertexShader( "VertexLitGeneric_EnvmappedBumpmap_NoLighting_ps14", vshIndex.GetIndex() );
  1214. if( bHasNormalMapAlphaEnvMapMask )
  1215. {
  1216. s_pShaderShadow->SetPixelShader( "VertexLitGeneric_EnvmappedBumpmapV2_MultByAlpha_ps14" );
  1217. }
  1218. else
  1219. {
  1220. s_pShaderShadow->SetPixelShader( "VertexLitGeneric_EnvmappedBumpmapV2_ps14" );
  1221. }
  1222. }
  1223. else
  1224. {
  1225. vertexlitgeneric_envmappedbumpmap_nolighting_Static_Index vshIndex;
  1226. s_pShaderShadow->SetVertexShader( "VertexLitGeneric_EnvmappedBumpmap_NoLighting", vshIndex.GetIndex() );
  1227. // This version does not multiply by lighting
  1228. // NOTE: We don't support multiplying by lighting for bumped specular stuff.
  1229. if( bHasNormalMapAlphaEnvMapMask )
  1230. {
  1231. s_pShaderShadow->SetPixelShader( "VertexLitGeneric_EnvmappedBumpmapV2_MultByAlpha" );
  1232. }
  1233. else
  1234. {
  1235. s_pShaderShadow->SetPixelShader( "VertexLitGeneric_EnvmappedBumpmapV2" );
  1236. }
  1237. }
  1238. FogToBlack();
  1239. }
  1240. else
  1241. {
  1242. s_pShaderAPI->SetDefaultState();
  1243. BindTexture( SHADER_SAMPLER0, bumpMapVar, bumpMapFrameVar );
  1244. BindTexture( SHADER_SAMPLER3, envMapVar, envMapVarFrame );
  1245. if( g_pHardwareConfig->SupportsPixelShaders_1_4() )
  1246. {
  1247. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER4, TEXTURE_NORMALIZATION_CUBEMAP );
  1248. }
  1249. if( bBlendSpecular )
  1250. {
  1251. SetEnvMapTintPixelShaderDynamicState( 0, envMapTintVar, -1 );
  1252. }
  1253. else
  1254. {
  1255. SetEnvMapTintPixelShaderDynamicState( 0, envMapTintVar, alphaVar );
  1256. }
  1257. // GR - fudge consts a bit to fix const/lerp issues
  1258. SetPixelShaderConstantFudge( 1, envMapContrastVar );
  1259. SetPixelShaderConstantFudge( 2, envMapSaturationVar );
  1260. float greyWeights[4] = { 0.299f, 0.587f, 0.114f, 0.0f };
  1261. s_pShaderAPI->SetPixelShaderConstant( 3, greyWeights );
  1262. // handle scrolling of bump texture
  1263. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, bumpTransformVar );
  1264. if( g_pHardwareConfig->SupportsPixelShaders_1_4() )
  1265. {
  1266. vertexlitgeneric_envmappedbumpmap_nolighting_ps14_Dynamic_Index vshIndex;
  1267. vshIndex.SetDOWATERFOG( s_pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  1268. vshIndex.SetSKINNING( s_pShaderAPI->GetCurrentNumBones() > 0 );
  1269. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  1270. }
  1271. else
  1272. {
  1273. vertexlitgeneric_envmappedbumpmap_nolighting_Dynamic_Index vshIndex;
  1274. vshIndex.SetDOWATERFOG( s_pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  1275. vshIndex.SetSKINNING( s_pShaderAPI->GetCurrentNumBones() > 0 );
  1276. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  1277. }
  1278. }
  1279. Draw();
  1280. }
  1281. void CBaseVSShader::DrawBaseTextureBlend( int baseTextureVar, int baseTextureTransformVar,
  1282. int baseTextureFrameVar,
  1283. int baseTexture2Var, int baseTextureTransform2Var,
  1284. int baseTextureFrame2Var, int colorVar, int alphaVar )
  1285. {
  1286. if( IsSnapshotting() )
  1287. {
  1288. SetInitialShadowState();
  1289. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  1290. s_pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  1291. s_pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  1292. s_pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 |
  1293. SHADER_DRAW_LIGHTMAP_TEXCOORD1 );
  1294. // FIXME: Remove the normal (needed for tangent space gen)
  1295. s_pShaderShadow->VertexShaderVertexFormat(
  1296. VERTEX_POSITION, 2, 0, 0 );
  1297. lightmappedgeneric_basetextureblend_Static_Index vshIndex;
  1298. s_pShaderShadow->SetVertexShader( "lightmappedgeneric_basetextureblend", vshIndex.GetIndex() );
  1299. s_pShaderShadow->SetPixelShader( "lightmappedgeneric_basetextureblend", 0 );
  1300. FogToOOOverbright();
  1301. }
  1302. else
  1303. {
  1304. IMaterialVar** params = s_ppParams;
  1305. bool bLightingOnly = mat_fullbright.GetInt() == 2 && !IS_FLAG_SET( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  1306. s_pShaderAPI->SetDefaultState();
  1307. if( bLightingOnly )
  1308. {
  1309. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_GREY );
  1310. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_GREY );
  1311. }
  1312. else
  1313. {
  1314. BindTexture( SHADER_SAMPLER0, baseTextureVar, baseTextureFrameVar );
  1315. BindTexture( SHADER_SAMPLER1, baseTexture2Var, baseTextureFrame2Var );
  1316. }
  1317. s_pShaderAPI->BindStandardTexture( SHADER_SAMPLER2, TEXTURE_LIGHTMAP );
  1318. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, baseTextureTransformVar );
  1319. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, baseTextureTransform2Var );
  1320. SetColorPixelShaderConstant( 0, colorVar, alphaVar );
  1321. lightmappedgeneric_basetextureblend_Dynamic_Index vshIndex;
  1322. vshIndex.SetDOWATERFOG( s_pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  1323. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  1324. }
  1325. Draw();
  1326. }
  1327. void CBaseVSShader::DrawWorldBumpedUsingVertexShader( int baseTextureVar, int baseTextureTransformVar,
  1328. int bumpmapVar, int bumpFrameVar,
  1329. int bumpTransformVar,
  1330. int envmapMaskVar, int envmapMaskFrame,
  1331. int envmapVar,
  1332. int envmapFrameVar,
  1333. int envmapTintVar, int colorVar, int alphaVar,
  1334. int envmapContrastVar, int envmapSaturationVar,
  1335. int frameVar, int fresnelReflectionVar,
  1336. bool doBaseTexture2,
  1337. int baseTexture2Var, int baseTextureTransform2Var,
  1338. int baseTextureFrame2Var,
  1339. bool bSSBump
  1340. )
  1341. {
  1342. IMaterialVar** params = s_ppParams;
  1343. // Draw base texture
  1344. bool bMultiplyDiffuseLighting = false;
  1345. bool bBlendSpecular = false;
  1346. // Draw base texture(s)
  1347. if( doBaseTexture2 && params[baseTexture2Var]->IsTexture() && params[baseTextureVar]->IsTexture() )
  1348. {
  1349. DrawBaseTextureBlend( baseTextureVar, baseTextureTransformVar, frameVar,
  1350. baseTexture2Var, baseTextureTransform2Var, baseTextureFrame2Var, colorVar, alphaVar );
  1351. bMultiplyDiffuseLighting = true;
  1352. bBlendSpecular = true;
  1353. }
  1354. else if( params[baseTextureVar]->IsTexture() )
  1355. {
  1356. DrawWorldBaseTexture( baseTextureVar, baseTextureTransformVar, frameVar, colorVar, alphaVar );
  1357. bMultiplyDiffuseLighting = true;
  1358. bBlendSpecular = true;
  1359. }
  1360. else
  1361. {
  1362. // Just use color here
  1363. }
  1364. // Draw diffuse lighting
  1365. if( params[baseTextureVar]->IsTexture() || !params[envmapVar]->IsTexture() )
  1366. {
  1367. DrawWorldBumpedDiffuseLighting( bumpmapVar, bumpFrameVar, bumpTransformVar,
  1368. bMultiplyDiffuseLighting, bSSBump );
  1369. bBlendSpecular = true;
  1370. }
  1371. // Add specular lighting
  1372. if( params[envmapVar]->IsTexture() )
  1373. {
  1374. DrawWorldBumpedSpecularLighting(
  1375. bumpmapVar, envmapVar,
  1376. bumpFrameVar, envmapFrameVar,
  1377. envmapTintVar, alphaVar,
  1378. envmapContrastVar, envmapSaturationVar,
  1379. bumpTransformVar, fresnelReflectionVar,
  1380. bBlendSpecular );
  1381. }
  1382. }
  1383. #endif // GAME_SHADER_DLL
  1384. //-----------------------------------------------------------------------------
  1385. // GR - translucency query
  1386. //-----------------------------------------------------------------------------
  1387. BlendType_t CBaseVSShader::EvaluateBlendRequirements( int textureVar, bool isBaseTexture,
  1388. int detailTextureVar )
  1389. {
  1390. // Either we've got a constant modulation
  1391. bool isTranslucent = IsAlphaModulating();
  1392. // Or we've got a vertex alpha
  1393. isTranslucent = isTranslucent || (CurrentMaterialVarFlags() & MATERIAL_VAR_VERTEXALPHA);
  1394. // Or we've got a texture alpha (for blending or alpha test)
  1395. isTranslucent = isTranslucent || ( TextureIsTranslucent( textureVar, isBaseTexture ) &&
  1396. !(CurrentMaterialVarFlags() & MATERIAL_VAR_ALPHATEST ) );
  1397. if ( ( detailTextureVar != -1 ) && ( ! isTranslucent ) )
  1398. {
  1399. isTranslucent = TextureIsTranslucent( detailTextureVar, isBaseTexture );
  1400. }
  1401. if ( CurrentMaterialVarFlags() & MATERIAL_VAR_ADDITIVE )
  1402. {
  1403. return isTranslucent ? BT_BLENDADD : BT_ADD; // Additive
  1404. }
  1405. else
  1406. {
  1407. return isTranslucent ? BT_BLEND : BT_NONE; // Normal blending
  1408. }
  1409. }
  1410. #ifndef GAME_SHADER_DLL
  1411. void CBaseVSShader::SetFlashlightVertexShaderConstants( bool bBump, int bumpTransformVar, bool bDetail, int detailScaleVar, bool bSetTextureTransforms )
  1412. {
  1413. Assert( !IsSnapshotting() );
  1414. VMatrix worldToTexture;
  1415. const FlashlightState_t &flashlightState = s_pShaderAPI->GetFlashlightState( worldToTexture );
  1416. // Set the flashlight origin
  1417. float pos[4];
  1418. pos[0] = flashlightState.m_vecLightOrigin[0];
  1419. pos[1] = flashlightState.m_vecLightOrigin[1];
  1420. pos[2] = flashlightState.m_vecLightOrigin[2];
  1421. pos[3] = 1.0f / ( ( 0.6f * flashlightState.m_FarZ ) - flashlightState.m_FarZ ); // DX8 needs this
  1422. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, pos, 1 );
  1423. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, worldToTexture.Base(), 4 );
  1424. // Set the flashlight attenuation factors
  1425. float atten[4];
  1426. atten[0] = flashlightState.m_fConstantAtten;
  1427. atten[1] = flashlightState.m_fLinearAtten;
  1428. atten[2] = flashlightState.m_fQuadraticAtten;
  1429. atten[3] = flashlightState.m_FarZ;
  1430. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_5, atten, 1 );
  1431. if ( bDetail )
  1432. {
  1433. SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_8, BASETEXTURETRANSFORM, detailScaleVar );
  1434. }
  1435. if( bSetTextureTransforms )
  1436. {
  1437. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, BASETEXTURETRANSFORM );
  1438. if( !bDetail && bBump && bumpTransformVar != -1 )
  1439. {
  1440. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_8, bumpTransformVar ); // aliased on top of detail transform
  1441. }
  1442. }
  1443. }
  1444. #if SUPPORT_DX8
  1445. void CBaseVSShader::DrawFlashlight_dx80( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow, bool bBump,
  1446. int bumpmapVar, int bumpmapFrame, int bumpTransform, int flashlightTextureVar, int flashlightTextureFrameVar,
  1447. bool bLightmappedGeneric, bool bWorldVertexTransition, int nWorldVertexTransitionPassID, int baseTexture2Var,
  1448. int baseTexture2FrameVar, bool bTeeth, int nTeethForwardVar, int nTeethIllumFactorVar )
  1449. {
  1450. // FLASHLIGHTFIXME: hack . . need to fix the vertex shader so that it can deal with and without bumps for vertexlitgeneric
  1451. if( !bLightmappedGeneric )
  1452. {
  1453. bBump = false;
  1454. }
  1455. if( pShaderShadow )
  1456. {
  1457. SetInitialShadowState();
  1458. pShaderShadow->EnableDepthWrites( false );
  1459. // Be sure not to write to dest alpha
  1460. pShaderShadow->EnableAlphaWrites( false );
  1461. // Never alpha test the flashlight pass
  1462. pShaderShadow->EnableAlphaTest( false );
  1463. if ( IS_FLAG_SET( MATERIAL_VAR_ALPHATEST ) )
  1464. {
  1465. // use zfunc zequals since alpha isn't guaranteed to
  1466. // be the same on both the regular pass and the flashlight pass.
  1467. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_EQUAL );
  1468. }
  1469. // Alpha blend
  1470. if( bWorldVertexTransition )
  1471. {
  1472. // use separate alpha blend to make sure that we aren't adding alpha from source
  1473. if( nWorldVertexTransitionPassID == 0 )
  1474. {
  1475. EnableAlphaBlending( SHADER_BLEND_DST_ALPHA, SHADER_BLEND_ONE );
  1476. }
  1477. else
  1478. {
  1479. EnableAlphaBlending( SHADER_BLEND_ONE_MINUS_DST_ALPHA, SHADER_BLEND_ONE );
  1480. }
  1481. }
  1482. else
  1483. {
  1484. SetAdditiveBlendingShadowState( BASETEXTURE, true );
  1485. }
  1486. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  1487. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  1488. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  1489. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  1490. if( bLightmappedGeneric )
  1491. {
  1492. bool bUsingVertexColor = IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR );
  1493. lightmappedgeneric_flashlight_vs11_Static_Index vshIndex;
  1494. vshIndex.SetNORMALMAP( bBump );
  1495. vshIndex.SetWORLDVERTEXTRANSITION( bWorldVertexTransition );
  1496. vshIndex.SetVERTEXCOLOR( bUsingVertexColor );
  1497. pShaderShadow->SetVertexShader( "lightmappedgeneric_flashlight_vs11", vshIndex.GetIndex() );
  1498. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  1499. if( bBump )
  1500. {
  1501. flags |= VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  1502. }
  1503. if ( bWorldVertexTransition || bUsingVertexColor )
  1504. {
  1505. flags |= VERTEX_COLOR;
  1506. }
  1507. pShaderShadow->VertexShaderVertexFormat( flags, 1, 0, 0 );
  1508. }
  1509. else
  1510. {
  1511. vertexlitgeneric_flashlight_vs11_Static_Index vshIndex;
  1512. vshIndex.SetTEETH( bTeeth );
  1513. pShaderShadow->SetVertexShader( "vertexlitgeneric_flashlight_vs11", vshIndex.GetIndex() );
  1514. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  1515. pShaderShadow->VertexShaderVertexFormat( flags, 1, 0, bBump ? 4 : 0 );
  1516. }
  1517. bool bNoCull = IS_FLAG_SET( MATERIAL_VAR_NOCULL );
  1518. flashlight_ps11_Static_Index pshIndex;
  1519. pshIndex.SetNORMALMAP( bBump );
  1520. pshIndex.SetNOCULL( bNoCull );
  1521. pShaderShadow->SetPixelShader( "flashlight_ps11", pshIndex.GetIndex() );
  1522. FogToBlack();
  1523. }
  1524. else
  1525. {
  1526. // Specify that we have XYZ texcoords that need to be divided by W before the pixel shader.
  1527. // NOTE Tried to divide XY by Z, but doesn't work.
  1528. // The dx9.0c runtime says that we shouldn't have a non-zero dimension when using vertex and pixel shaders.
  1529. pShaderAPI->SetTextureTransformDimension( SHADER_TEXTURE_STAGE0, 0, true );
  1530. BindTexture( SHADER_SAMPLER0, flashlightTextureVar, flashlightTextureFrameVar );
  1531. if( bWorldVertexTransition && ( nWorldVertexTransitionPassID == 1 ) )
  1532. {
  1533. BindTexture( SHADER_SAMPLER1, baseTexture2Var, baseTexture2FrameVar );
  1534. }
  1535. else
  1536. {
  1537. if( params[BASETEXTURE]->IsTexture() )
  1538. {
  1539. BindTexture( SHADER_SAMPLER1, BASETEXTURE, FRAME );
  1540. }
  1541. else
  1542. {
  1543. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_GREY );
  1544. }
  1545. }
  1546. pShaderAPI->BindStandardTexture( SHADER_SAMPLER2, TEXTURE_NORMALIZATION_CUBEMAP );
  1547. if( bBump )
  1548. {
  1549. BindTexture( SHADER_SAMPLER3, bumpmapVar, bumpmapFrame );
  1550. }
  1551. else
  1552. {
  1553. pShaderAPI->BindStandardTexture( SHADER_SAMPLER3, TEXTURE_NORMALIZATION_CUBEMAP );
  1554. }
  1555. if( bLightmappedGeneric )
  1556. {
  1557. lightmappedgeneric_flashlight_vs11_Dynamic_Index vshIndex;
  1558. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  1559. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  1560. }
  1561. else
  1562. {
  1563. vertexlitgeneric_flashlight_vs11_Dynamic_Index vshIndex;
  1564. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  1565. if( bTeeth )
  1566. {
  1567. Assert( nTeethForwardVar >= 0 );
  1568. Assert( nTeethIllumFactorVar >= 0 );
  1569. Vector4D lighting;
  1570. params[nTeethForwardVar]->GetVecValue( lighting.Base(), 3 );
  1571. lighting[3] = params[nTeethIllumFactorVar]->GetFloatValue();
  1572. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_10, lighting.Base() );
  1573. }
  1574. vshIndex.SetSKINNING( pShaderAPI->GetCurrentNumBones() > 0 );
  1575. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  1576. }
  1577. flashlight_ps11_Dynamic_Index pshIndex;
  1578. pShaderAPI->SetPixelShaderIndex( pshIndex.GetIndex() );
  1579. SetFlashlightVertexShaderConstants( bBump, bumpTransform, false, -1, true );
  1580. }
  1581. Draw();
  1582. }
  1583. #endif // support_dx8
  1584. #ifdef STDSHADER_DX9_DLL_EXPORT
  1585. void CBaseVSShader::DrawFlashlight_dx90( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  1586. IShaderShadow* pShaderShadow, DrawFlashlight_dx90_Vars_t &vars )
  1587. {
  1588. // FLASHLIGHTFIXME: hack . . need to fix the vertex shader so that it can deal with and without bumps for vertexlitgeneric
  1589. if( !vars.m_bLightmappedGeneric )
  1590. {
  1591. vars.m_bBump = false;
  1592. }
  1593. bool bBump2 = vars.m_bWorldVertexTransition && vars.m_bBump && vars.m_nBumpmap2Var != -1 && params[vars.m_nBumpmap2Var]->IsTexture();
  1594. bool bSeamless = vars.m_fSeamlessScale != 0.0;
  1595. bool bDetail = vars.m_bLightmappedGeneric && (vars.m_nDetailVar != -1) && params[vars.m_nDetailVar]->IsDefined() && (vars.m_nDetailScale != -1);
  1596. int nDetailBlendMode = 0;
  1597. if ( bDetail )
  1598. {
  1599. nDetailBlendMode = GetIntParam( vars.m_nDetailTextureCombineMode, params );
  1600. nDetailBlendMode = nDetailBlendMode > 1 ? 1 : nDetailBlendMode;
  1601. }
  1602. if( pShaderShadow )
  1603. {
  1604. SetInitialShadowState();
  1605. pShaderShadow->EnableDepthWrites( false );
  1606. pShaderShadow->EnableAlphaWrites( false );
  1607. // Alpha blend
  1608. SetAdditiveBlendingShadowState( BASETEXTURE, true );
  1609. // Alpha test
  1610. pShaderShadow->EnableAlphaTest( IS_FLAG_SET( MATERIAL_VAR_ALPHATEST ) );
  1611. if ( vars.m_nAlphaTestReference != -1 && params[vars.m_nAlphaTestReference]->GetFloatValue() > 0.0f )
  1612. {
  1613. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, params[vars.m_nAlphaTestReference]->GetFloatValue() );
  1614. }
  1615. // Spot sampler
  1616. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  1617. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  1618. // Base sampler
  1619. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  1620. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  1621. // Normalizing cubemap sampler
  1622. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  1623. // Normalizing cubemap sampler2 or normal map sampler
  1624. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  1625. // RandomRotation sampler
  1626. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true );
  1627. // Flashlight depth sampler
  1628. pShaderShadow->EnableTexture( SHADER_SAMPLER7, true );
  1629. pShaderShadow->SetShadowDepthFiltering( SHADER_SAMPLER7 );
  1630. if( vars.m_bWorldVertexTransition )
  1631. {
  1632. // $basetexture2
  1633. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  1634. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER4, true );
  1635. }
  1636. if( bBump2 )
  1637. {
  1638. // Normalmap2 sampler
  1639. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true );
  1640. }
  1641. if( bDetail )
  1642. {
  1643. pShaderShadow->EnableTexture( SHADER_SAMPLER8, true ); // detail sampler
  1644. if ( nDetailBlendMode != 0 ) //Not Mod2X
  1645. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER8, true );
  1646. }
  1647. pShaderShadow->EnableSRGBWrite( true );
  1648. if( vars.m_bLightmappedGeneric )
  1649. {
  1650. lightmappedgeneric_flashlight_vs20_Static_Index vshIndex;
  1651. vshIndex.SetWORLDVERTEXTRANSITION( vars.m_bWorldVertexTransition );
  1652. vshIndex.SetNORMALMAP( vars.m_bBump );
  1653. vshIndex.SetSEAMLESS( bSeamless );
  1654. vshIndex.SetDETAIL( bDetail );
  1655. pShaderShadow->SetVertexShader( "lightmappedgeneric_flashlight_vs20", vshIndex.GetIndex() );
  1656. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  1657. if( vars.m_bBump )
  1658. {
  1659. flags |= VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  1660. }
  1661. int numTexCoords = 1;
  1662. if( vars.m_bWorldVertexTransition )
  1663. {
  1664. flags |= VERTEX_COLOR;
  1665. numTexCoords = 2; // need lightmap texcoords to get alpha.
  1666. }
  1667. pShaderShadow->VertexShaderVertexFormat( flags, numTexCoords, 0, 0 );
  1668. }
  1669. else
  1670. {
  1671. vertexlitgeneric_flashlight_vs11_Static_Index vshIndex;
  1672. vshIndex.SetTEETH( vars.m_bTeeth );
  1673. pShaderShadow->SetVertexShader( "vertexlitgeneric_flashlight_vs11", vshIndex.GetIndex() );
  1674. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  1675. int numTexCoords = 1;
  1676. pShaderShadow->VertexShaderVertexFormat( flags, numTexCoords, 0, vars.m_bBump ? 4 : 0 );
  1677. }
  1678. int nBumpMapVariant = 0;
  1679. if ( vars.m_bBump )
  1680. {
  1681. nBumpMapVariant = ( vars.m_bSSBump ) ? 2 : 1;
  1682. }
  1683. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  1684. {
  1685. int nShadowFilterMode = g_pHardwareConfig->GetShadowFilterMode();
  1686. flashlight_ps20b_Static_Index pshIndex;
  1687. pshIndex.SetNORMALMAP( nBumpMapVariant );
  1688. pshIndex.SetNORMALMAP2( bBump2 );
  1689. pshIndex.SetWORLDVERTEXTRANSITION( vars.m_bWorldVertexTransition );
  1690. pshIndex.SetSEAMLESS( bSeamless );
  1691. pshIndex.SetDETAILTEXTURE( bDetail );
  1692. pshIndex.SetDETAIL_BLEND_MODE( nDetailBlendMode );
  1693. pshIndex.SetFLASHLIGHTDEPTHFILTERMODE( nShadowFilterMode );
  1694. pShaderShadow->SetPixelShader( "flashlight_ps20b", pshIndex.GetIndex() );
  1695. }
  1696. else
  1697. {
  1698. flashlight_ps20_Static_Index pshIndex;
  1699. pshIndex.SetNORMALMAP( nBumpMapVariant );
  1700. pshIndex.SetNORMALMAP2( bBump2 );
  1701. pshIndex.SetWORLDVERTEXTRANSITION( vars.m_bWorldVertexTransition );
  1702. pshIndex.SetSEAMLESS( bSeamless );
  1703. pshIndex.SetDETAILTEXTURE( bDetail );
  1704. pshIndex.SetDETAIL_BLEND_MODE( nDetailBlendMode );
  1705. pShaderShadow->SetPixelShader( "flashlight_ps20", pshIndex.GetIndex() );
  1706. }
  1707. FogToBlack();
  1708. }
  1709. else
  1710. {
  1711. VMatrix worldToTexture;
  1712. ITexture *pFlashlightDepthTexture;
  1713. FlashlightState_t flashlightState = pShaderAPI->GetFlashlightStateEx( worldToTexture, &pFlashlightDepthTexture );
  1714. SetFlashLightColorFromState( flashlightState, pShaderAPI );
  1715. BindTexture( SHADER_SAMPLER0, flashlightState.m_pSpotlightTexture, flashlightState.m_nSpotlightTextureFrame );
  1716. if( pFlashlightDepthTexture && g_pConfig->ShadowDepthTexture() && flashlightState.m_bEnableShadows )
  1717. {
  1718. BindTexture( SHADER_SAMPLER7, pFlashlightDepthTexture, 0 );
  1719. pShaderAPI->BindStandardTexture( SHADER_SAMPLER5, TEXTURE_SHADOW_NOISE_2D );
  1720. // Tweaks associated with a given flashlight
  1721. float tweaks[4];
  1722. tweaks[0] = ShadowFilterFromState( flashlightState );
  1723. tweaks[1] = ShadowAttenFromState( flashlightState );
  1724. HashShadow2DJitter( flashlightState.m_flShadowJitterSeed, &tweaks[2], &tweaks[3] );
  1725. pShaderAPI->SetPixelShaderConstant( PSREG_ENVMAP_TINT__SHADOW_TWEAKS, tweaks, 1 );
  1726. // Dimensions of screen, used for screen-space noise map sampling
  1727. float vScreenScale[4] = {1280.0f / 32.0f, 720.0f / 32.0f, 0, 0};
  1728. int nWidth, nHeight;
  1729. pShaderAPI->GetBackBufferDimensions( nWidth, nHeight );
  1730. vScreenScale[0] = (float) nWidth / 32.0f;
  1731. vScreenScale[1] = (float) nHeight / 32.0f;
  1732. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_SCREEN_SCALE, vScreenScale, 1 );
  1733. }
  1734. if( params[BASETEXTURE]->IsTexture() && mat_fullbright.GetInt() != 2 )
  1735. {
  1736. BindTexture( SHADER_SAMPLER1, BASETEXTURE, FRAME );
  1737. }
  1738. else
  1739. {
  1740. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_GREY );
  1741. }
  1742. if( vars.m_bWorldVertexTransition )
  1743. {
  1744. Assert( vars.m_nBaseTexture2Var >= 0 && vars.m_nBaseTexture2FrameVar >= 0 );
  1745. BindTexture( SHADER_SAMPLER4, vars.m_nBaseTexture2Var, vars.m_nBaseTexture2FrameVar );
  1746. }
  1747. pShaderAPI->BindStandardTexture( SHADER_SAMPLER2, TEXTURE_NORMALIZATION_CUBEMAP );
  1748. if( vars.m_bBump )
  1749. {
  1750. BindTexture( SHADER_SAMPLER3, vars.m_nBumpmapVar, vars.m_nBumpmapFrame );
  1751. }
  1752. else
  1753. {
  1754. pShaderAPI->BindStandardTexture( SHADER_SAMPLER3, TEXTURE_NORMALIZATION_CUBEMAP );
  1755. }
  1756. if( bDetail )
  1757. {
  1758. BindTexture( SHADER_SAMPLER8, vars.m_nDetailVar );
  1759. }
  1760. if( vars.m_bWorldVertexTransition )
  1761. {
  1762. if( bBump2 )
  1763. {
  1764. BindTexture( SHADER_SAMPLER6, vars.m_nBumpmap2Var, vars.m_nBumpmap2Frame );
  1765. }
  1766. }
  1767. if( vars.m_bLightmappedGeneric )
  1768. {
  1769. DECLARE_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_flashlight_vs20 );
  1770. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  1771. SET_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_flashlight_vs20 );
  1772. if ( bSeamless )
  1773. {
  1774. float const0[4]={ vars.m_fSeamlessScale,0,0,0};
  1775. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, const0 );
  1776. }
  1777. if ( bDetail )
  1778. {
  1779. float vDetailConstants[4] = {1,1,1,1};
  1780. if ( vars.m_nDetailTint != -1 )
  1781. {
  1782. params[vars.m_nDetailTint]->GetVecValue( vDetailConstants, 3 );
  1783. }
  1784. if ( vars.m_nDetailTextureBlendFactor != -1 )
  1785. {
  1786. vDetailConstants[3] = params[vars.m_nDetailTextureBlendFactor]->GetFloatValue();
  1787. }
  1788. pShaderAPI->SetPixelShaderConstant( 0, vDetailConstants, 1 );
  1789. }
  1790. }
  1791. else
  1792. {
  1793. vertexlitgeneric_flashlight_vs11_Dynamic_Index vshIndex;
  1794. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  1795. vshIndex.SetSKINNING( pShaderAPI->GetCurrentNumBones() > 0 );
  1796. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  1797. if( vars.m_bTeeth )
  1798. {
  1799. Assert( vars.m_nTeethForwardVar >= 0 );
  1800. Assert( vars.m_nTeethIllumFactorVar >= 0 );
  1801. Vector4D lighting;
  1802. params[vars.m_nTeethForwardVar]->GetVecValue( lighting.Base(), 3 );
  1803. lighting[3] = params[vars.m_nTeethIllumFactorVar]->GetFloatValue();
  1804. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, lighting.Base() );
  1805. }
  1806. }
  1807. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  1808. float vEyePos_SpecExponent[4];
  1809. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  1810. vEyePos_SpecExponent[3] = 0.0f;
  1811. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  1812. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  1813. {
  1814. DECLARE_DYNAMIC_PIXEL_SHADER( flashlight_ps20b );
  1815. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  1816. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, flashlightState.m_bEnableShadows && ( pFlashlightDepthTexture != NULL ) );
  1817. SET_DYNAMIC_PIXEL_SHADER( flashlight_ps20b );
  1818. }
  1819. else
  1820. {
  1821. DECLARE_DYNAMIC_PIXEL_SHADER( flashlight_ps20 );
  1822. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  1823. SET_DYNAMIC_PIXEL_SHADER( flashlight_ps20 );
  1824. }
  1825. float atten[4]; // Set the flashlight attenuation factors
  1826. atten[0] = flashlightState.m_fConstantAtten;
  1827. atten[1] = flashlightState.m_fLinearAtten;
  1828. atten[2] = flashlightState.m_fQuadraticAtten;
  1829. atten[3] = flashlightState.m_FarZ;
  1830. s_pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_ATTENUATION, atten, 1 );
  1831. SetFlashlightVertexShaderConstants( vars.m_bBump, vars.m_nBumpTransform, bDetail, vars.m_nDetailScale, bSeamless ? false : true );
  1832. }
  1833. Draw();
  1834. }
  1835. #endif
  1836. void CBaseVSShader::InitParamsUnlitGeneric_DX8(
  1837. int baseTextureVar,
  1838. int detailScaleVar,
  1839. int envmapOptionalVar,
  1840. int envmapVar,
  1841. int envmapTintVar,
  1842. int envmapMaskScaleVar,
  1843. int nDetailBlendMode )
  1844. {
  1845. IMaterialVar** params = s_ppParams;
  1846. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  1847. if( envmapTintVar >= 0 && !params[envmapTintVar]->IsDefined() )
  1848. {
  1849. params[envmapTintVar]->SetVecValue( 1.0f, 1.0f, 1.0f );
  1850. }
  1851. if( envmapMaskScaleVar >= 0 && !params[envmapMaskScaleVar]->IsDefined() )
  1852. {
  1853. params[envmapMaskScaleVar]->SetFloatValue( 1.0f );
  1854. }
  1855. if( detailScaleVar >= 0 && !params[detailScaleVar]->IsDefined() )
  1856. {
  1857. params[detailScaleVar]->SetFloatValue( 4.0f );
  1858. }
  1859. // No texture means no self-illum or env mask in base alpha
  1860. if ( baseTextureVar >= 0 && !params[baseTextureVar]->IsDefined() )
  1861. {
  1862. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  1863. }
  1864. // If in decal mode, no debug override...
  1865. if (IS_FLAG_SET(MATERIAL_VAR_DECAL))
  1866. {
  1867. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  1868. }
  1869. // Get rid of the envmap if it's optional for this dx level.
  1870. if( envmapOptionalVar >= 0 && params[envmapOptionalVar]->IsDefined() && params[envmapOptionalVar]->GetIntValue() )
  1871. {
  1872. if (envmapVar >= 0)
  1873. {
  1874. params[envmapVar]->SetUndefined();
  1875. }
  1876. }
  1877. // If mat_specular 0, then get rid of envmap
  1878. if( envmapVar >= 0 && baseTextureVar >= 0 && !g_pConfig->UseSpecular() && params[envmapVar]->IsDefined() && params[baseTextureVar]->IsDefined() )
  1879. {
  1880. params[envmapVar]->SetUndefined();
  1881. }
  1882. }
  1883. void CBaseVSShader::InitUnlitGeneric_DX8(
  1884. int baseTextureVar,
  1885. int detailVar,
  1886. int envmapVar,
  1887. int envmapMaskVar )
  1888. {
  1889. IMaterialVar** params = s_ppParams;
  1890. if (baseTextureVar >= 0 && params[baseTextureVar]->IsDefined())
  1891. {
  1892. LoadTexture( baseTextureVar );
  1893. if (!params[baseTextureVar]->GetTextureValue()->IsTranslucent())
  1894. {
  1895. if (IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK))
  1896. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  1897. }
  1898. }
  1899. // Don't alpha test if the alpha channel is used for other purposes
  1900. if ( IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  1901. CLEAR_FLAGS( MATERIAL_VAR_ALPHATEST );
  1902. // the second texture (if there is one)
  1903. if (detailVar >= 0 && params[detailVar]->IsDefined())
  1904. {
  1905. LoadTexture( detailVar );
  1906. }
  1907. if (envmapVar >= 0 && params[envmapVar]->IsDefined())
  1908. {
  1909. if( !IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) )
  1910. LoadCubeMap( envmapVar );
  1911. else
  1912. LoadTexture( envmapVar );
  1913. if( !g_pHardwareConfig->SupportsCubeMaps() )
  1914. SET_FLAGS(MATERIAL_VAR_ENVMAPSPHERE);
  1915. if (envmapMaskVar >= 0 && params[envmapMaskVar]->IsDefined())
  1916. LoadTexture( envmapMaskVar );
  1917. }
  1918. }
  1919. #endif // GAME_SHADER_DLL
  1920. #endif // !_STATIC_LINKED || STDSHADER_DX8_DLL_EXPORT
  1921. // Take 0..1 seed and map to (u, v) coordinate to be used in shadow filter jittering...
  1922. void CBaseVSShader::HashShadow2DJitter( const float fJitterSeed, float *fU, float* fV )
  1923. {
  1924. const int nTexRes = 32;
  1925. int nSeed = fmod (fJitterSeed, 1.0f) * nTexRes * nTexRes;
  1926. int nRow = nSeed / nTexRes;
  1927. int nCol = nSeed % nTexRes;
  1928. // Div and mod to get an individual texel in the fTexRes x fTexRes grid
  1929. *fU = nRow / (float) nTexRes; // Row
  1930. *fV = nCol / (float) nTexRes; // Column
  1931. }
  1932. void CBaseVSShader::DrawEqualDepthToDestAlpha( void )
  1933. {
  1934. #ifdef STDSHADER_DX9_DLL_EXPORT
  1935. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  1936. {
  1937. bool bMakeActualDrawCall = false;
  1938. if( s_pShaderShadow )
  1939. {
  1940. s_pShaderShadow->EnableColorWrites( false );
  1941. s_pShaderShadow->EnableAlphaWrites( true );
  1942. s_pShaderShadow->EnableDepthWrites( false );
  1943. s_pShaderShadow->EnableAlphaTest( false );
  1944. s_pShaderShadow->EnableBlending( false );
  1945. s_pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_EQUAL );
  1946. s_pShaderShadow->SetVertexShader( "depthtodestalpha_vs20", 0 );
  1947. s_pShaderShadow->SetPixelShader( "depthtodestalpha_ps20b", 0 );
  1948. }
  1949. if( s_pShaderAPI )
  1950. {
  1951. s_pShaderAPI->SetVertexShaderIndex( 0 );
  1952. s_pShaderAPI->SetPixelShaderIndex( 0 );
  1953. bMakeActualDrawCall = s_pShaderAPI->ShouldWriteDepthToDestAlpha();
  1954. }
  1955. Draw( bMakeActualDrawCall );
  1956. }
  1957. #else
  1958. Assert( 0 ); //probably just needs a shader update to the latest
  1959. #endif
  1960. }