Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1204 lines
40 KiB

  1. //===== Copyright (c) 1996-2007, 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_DX9_DLL_EXPORT)
  9. #include "cpp_shader_constant_register_map.h"
  10. #include "BaseVSShader.h"
  11. #include "mathlib/vmatrix.h"
  12. #include "mathlib/bumpvects.h"
  13. #include "convar.h"
  14. #include "tier0/icommandline.h"
  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 !defined( _X360 ) && !defined( _PS3 )
  20. #include "lightmappedgeneric_flashlight_vs30.inc"
  21. #include "flashlight_ps30.inc"
  22. #endif
  23. #include "lightmappedgeneric_flashlight_vs20.inc"
  24. #include "flashlight_ps20.inc"
  25. #include "flashlight_ps20b.inc"
  26. #include "vertexlitgeneric_flashlight_vs20.inc"
  27. #include "shaderapifast.h"
  28. // memdbgon must be the last include file in a .cpp file!!!
  29. #include "tier0/memdbgon.h"
  30. // NOTE: This is externed in BaseVSShader.h so it needs to be here
  31. #if defined( _PS3 ) || defined( OSX )
  32. extern ConVar r_flashlightbrightness;
  33. #else
  34. ConVar r_flashlightbrightness( "r_flashlightbrightness", "0.25", FCVAR_CHEAT );
  35. #endif
  36. // These functions are to be called from the shaders.
  37. //-----------------------------------------------------------------------------
  38. // Pixel and vertex shader constants....
  39. //-----------------------------------------------------------------------------
  40. void CBaseVSShader::SetPixelShaderConstant( int pixelReg, int constantVar, int constantVar2 )
  41. {
  42. Assert( !IsSnapshotting() );
  43. if ((!s_ppParams) || (constantVar == -1) || (constantVar2 == -1))
  44. return;
  45. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  46. Assert( pPixelVar );
  47. IMaterialVar* pPixelVar2 = s_ppParams[constantVar2];
  48. Assert( pPixelVar2 );
  49. float val[4];
  50. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  51. {
  52. pPixelVar->GetVecValue( val, 3 );
  53. }
  54. else
  55. {
  56. val[0] = val[1] = val[2] = pPixelVar->GetFloatValue();
  57. }
  58. val[3] = pPixelVar2->GetFloatValue();
  59. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  60. }
  61. void CBaseVSShader::SetPixelShaderConstantGammaToLinear( int pixelReg, int constantVar, int constantVar2 )
  62. {
  63. Assert( !IsSnapshotting() );
  64. if ((!s_ppParams) || (constantVar == -1) || (constantVar2 == -1))
  65. return;
  66. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  67. Assert( pPixelVar );
  68. IMaterialVar* pPixelVar2 = s_ppParams[constantVar2];
  69. Assert( pPixelVar2 );
  70. float val[4];
  71. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  72. {
  73. pPixelVar->GetVecValue( val, 3 );
  74. }
  75. else
  76. {
  77. val[0] = val[1] = val[2] = pPixelVar->GetFloatValue();
  78. }
  79. val[3] = pPixelVar2->GetFloatValue();
  80. val[0] = val[0] > 1.0f ? val[0] : GammaToLinear( val[0] );
  81. val[1] = val[1] > 1.0f ? val[1] : GammaToLinear( val[1] );
  82. val[2] = val[2] > 1.0f ? val[2] : GammaToLinear( val[2] );
  83. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  84. }
  85. void CBaseVSShader::SetPixelShaderConstant_W( int pixelReg, int constantVar, float fWValue )
  86. {
  87. Assert( !IsSnapshotting() );
  88. if ((!s_ppParams) || (constantVar == -1))
  89. return;
  90. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  91. Assert( pPixelVar );
  92. float val[4];
  93. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  94. pPixelVar->GetVecValue( val, 4 );
  95. else
  96. val[0] = val[1] = val[2] = val[3] = pPixelVar->GetFloatValue();
  97. val[3]=fWValue;
  98. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  99. }
  100. void CBaseVSShader::SetPixelShaderConstant( int pixelReg, int constantVar )
  101. {
  102. Assert( !IsSnapshotting() );
  103. if ((!s_ppParams) || (constantVar == -1))
  104. return;
  105. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  106. Assert( pPixelVar );
  107. float val[4];
  108. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  109. pPixelVar->GetVecValue( val, 4 );
  110. else
  111. val[0] = val[1] = val[2] = val[3] = pPixelVar->GetFloatValue();
  112. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  113. }
  114. void CBaseVSShader::SetPixelShaderConstantGammaToLinear( int pixelReg, int constantVar )
  115. {
  116. Assert( !IsSnapshotting() );
  117. if ((!s_ppParams) || (constantVar == -1))
  118. return;
  119. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  120. Assert( pPixelVar );
  121. float val[4];
  122. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  123. pPixelVar->GetVecValue( val, 4 );
  124. else
  125. val[0] = val[1] = val[2] = val[3] = pPixelVar->GetFloatValue();
  126. val[0] = val[0] > 1.0f ? val[0] : GammaToLinear( val[0] );
  127. val[1] = val[1] > 1.0f ? val[1] : GammaToLinear( val[1] );
  128. val[2] = val[2] > 1.0f ? val[2] : GammaToLinear( val[2] );
  129. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  130. }
  131. void CBaseVSShader::SetVertexShaderConstantGammaToLinear( int var, float const* pVec, int numConst, bool bForce )
  132. {
  133. int i;
  134. for( i = 0; i < numConst; i++ )
  135. {
  136. float vec[4];
  137. vec[0] = pVec[i*4+0] > 1.0f ? pVec[i*4+0] : GammaToLinear( pVec[i*4+0] );
  138. vec[1] = pVec[i*4+1] > 1.0f ? pVec[i*4+1] : GammaToLinear( pVec[i*4+1] );
  139. vec[2] = pVec[i*4+2] > 1.0f ? pVec[i*4+2] : GammaToLinear( pVec[i*4+2] );
  140. vec[3] = pVec[i*4+3];
  141. s_pShaderAPI->SetVertexShaderConstant( var + i, vec, 1, bForce );
  142. }
  143. }
  144. void CBaseVSShader::SetPixelShaderConstantGammaToLinear( int var, float const* pVec, int numConst, bool bForce )
  145. {
  146. int i;
  147. for( i = 0; i < numConst; i++ )
  148. {
  149. float vec[4];
  150. vec[0] = pVec[i*4+0] > 1.0f ? pVec[i*4+0] : GammaToLinear( pVec[i*4+0] );
  151. vec[1] = pVec[i*4+1] > 1.0f ? pVec[i*4+1] : GammaToLinear( pVec[i*4+1] );
  152. vec[2] = pVec[i*4+2] > 1.0f ? pVec[i*4+2] : GammaToLinear( pVec[i*4+2] );
  153. vec[3] = pVec[i*4+3];
  154. s_pShaderAPI->SetPixelShaderConstant( var + i, vec, 1, bForce );
  155. }
  156. }
  157. // GR - special version with fix for const/lerp issue
  158. void CBaseVSShader::SetPixelShaderConstantFudge( int pixelReg, int constantVar )
  159. {
  160. Assert( !IsSnapshotting() );
  161. if ((!s_ppParams) || (constantVar == -1))
  162. return;
  163. IMaterialVar* pPixelVar = s_ppParams[constantVar];
  164. Assert( pPixelVar );
  165. float val[4];
  166. if (pPixelVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  167. {
  168. pPixelVar->GetVecValue( val, 4 );
  169. val[0] = val[0] * 0.992f + 0.0078f;
  170. val[1] = val[1] * 0.992f + 0.0078f;
  171. val[2] = val[2] * 0.992f + 0.0078f;
  172. val[3] = val[3] * 0.992f + 0.0078f;
  173. }
  174. else
  175. val[0] = val[1] = val[2] = val[3] = pPixelVar->GetFloatValue() * 0.992f + 0.0078f;
  176. s_pShaderAPI->SetPixelShaderConstant( pixelReg, val );
  177. }
  178. void CBaseVSShader::SetVertexShaderConstant( int vertexReg, int constantVar )
  179. {
  180. Assert( !IsSnapshotting() );
  181. if ((!s_ppParams) || (constantVar == -1))
  182. return;
  183. IMaterialVar* pVertexVar = s_ppParams[constantVar];
  184. Assert( pVertexVar );
  185. float val[4];
  186. if (pVertexVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  187. pVertexVar->GetVecValue( val, 4 );
  188. else
  189. val[0] = val[1] = val[2] = val[3] = pVertexVar->GetFloatValue();
  190. s_pShaderAPI->SetVertexShaderConstant( vertexReg, val );
  191. }
  192. //-----------------------------------------------------------------------------
  193. // Sets vertex shader texture transforms
  194. //-----------------------------------------------------------------------------
  195. void CBaseVSShader::SetVertexShaderTextureTranslation( int vertexReg, int translationVar )
  196. {
  197. float offset[2] = {0, 0};
  198. IMaterialVar* pTranslationVar = s_ppParams[translationVar];
  199. if (pTranslationVar)
  200. {
  201. if (pTranslationVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  202. pTranslationVar->GetVecValue( offset, 2 );
  203. else
  204. offset[0] = offset[1] = pTranslationVar->GetFloatValue();
  205. }
  206. Vector4D translation[2];
  207. translation[0].Init( 1.0f, 0.0f, 0.0f, offset[0] );
  208. translation[1].Init( 0.0f, 1.0f, 0.0f, offset[1] );
  209. s_pShaderAPI->SetVertexShaderConstant( vertexReg, translation[0].Base(), 2 );
  210. }
  211. void CBaseVSShader::SetVertexShaderTextureScale( int vertexReg, int scaleVar )
  212. {
  213. float scale[2] = {1, 1};
  214. IMaterialVar* pScaleVar = s_ppParams[scaleVar];
  215. if (pScaleVar)
  216. {
  217. if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  218. pScaleVar->GetVecValue( scale, 2 );
  219. else if (pScaleVar->IsDefined())
  220. scale[0] = scale[1] = pScaleVar->GetFloatValue();
  221. }
  222. Vector4D scaleMatrix[2];
  223. scaleMatrix[0].Init( scale[0], 0.0f, 0.0f, 0.0f );
  224. scaleMatrix[1].Init( 0.0f, scale[1], 0.0f, 0.0f );
  225. s_pShaderAPI->SetVertexShaderConstant( vertexReg, scaleMatrix[0].Base(), 2 );
  226. }
  227. void CBaseVSShader::SetVertexShaderTextureTransform( int vertexReg, int transformVar )
  228. {
  229. Vector4D transformation[2];
  230. IMaterialVar* pTransformationVar = s_ppParams[transformVar];
  231. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  232. {
  233. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  234. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  235. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  236. }
  237. else
  238. {
  239. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  240. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  241. }
  242. s_pShaderAPI->SetVertexShaderConstant( vertexReg, transformation[0].Base(), 2 );
  243. }
  244. void CBaseVSShader::SetVertexShaderTextureScaledTransform( int vertexReg, int transformVar, int scaleVar )
  245. {
  246. Vector4D transformation[2];
  247. IMaterialVar* pTransformationVar = s_ppParams[transformVar];
  248. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  249. {
  250. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  251. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  252. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  253. }
  254. else
  255. {
  256. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  257. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  258. }
  259. Vector2D scale( 1, 1 );
  260. IMaterialVar* pScaleVar = s_ppParams[scaleVar];
  261. if (pScaleVar)
  262. {
  263. if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  264. pScaleVar->GetVecValue( scale.Base(), 2 );
  265. else if (pScaleVar->IsDefined())
  266. scale[0] = scale[1] = pScaleVar->GetFloatValue();
  267. }
  268. // Apply the scaling
  269. transformation[0][0] *= scale[0];
  270. transformation[0][1] *= scale[1];
  271. transformation[1][0] *= scale[0];
  272. transformation[1][1] *= scale[1];
  273. transformation[0][3] *= scale[0];
  274. transformation[1][3] *= scale[1];
  275. s_pShaderAPI->SetVertexShaderConstant( vertexReg, transformation[0].Base(), 2 );
  276. }
  277. //-----------------------------------------------------------------------------
  278. // Sets pixel shader texture transforms
  279. //-----------------------------------------------------------------------------
  280. void CBaseVSShader::SetPixelShaderTextureTranslation( int pixelReg, int translationVar )
  281. {
  282. float offset[2] = {0, 0};
  283. IMaterialVar* pTranslationVar = s_ppParams[translationVar];
  284. if (pTranslationVar)
  285. {
  286. if (pTranslationVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  287. pTranslationVar->GetVecValue( offset, 2 );
  288. else
  289. offset[0] = offset[1] = pTranslationVar->GetFloatValue();
  290. }
  291. Vector4D translation[2];
  292. translation[0].Init( 1.0f, 0.0f, 0.0f, offset[0] );
  293. translation[1].Init( 0.0f, 1.0f, 0.0f, offset[1] );
  294. s_pShaderAPI->SetPixelShaderConstant( pixelReg, translation[0].Base(), 2 );
  295. }
  296. void CBaseVSShader::SetPixelShaderTextureScale( int pixelReg, int scaleVar )
  297. {
  298. float scale[2] = {1, 1};
  299. IMaterialVar* pScaleVar = s_ppParams[scaleVar];
  300. if (pScaleVar)
  301. {
  302. if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  303. pScaleVar->GetVecValue( scale, 2 );
  304. else if (pScaleVar->IsDefined())
  305. scale[0] = scale[1] = pScaleVar->GetFloatValue();
  306. }
  307. Vector4D scaleMatrix[2];
  308. scaleMatrix[0].Init( scale[0], 0.0f, 0.0f, 0.0f );
  309. scaleMatrix[1].Init( 0.0f, scale[1], 0.0f, 0.0f );
  310. s_pShaderAPI->SetPixelShaderConstant( pixelReg, scaleMatrix[0].Base(), 2 );
  311. }
  312. void CBaseVSShader::SetPixelShaderTextureTransform( int pixelReg, int transformVar )
  313. {
  314. Vector4D transformation[2];
  315. IMaterialVar* pTransformationVar = s_ppParams[transformVar];
  316. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  317. {
  318. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  319. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  320. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  321. }
  322. else
  323. {
  324. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  325. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  326. }
  327. s_pShaderAPI->SetPixelShaderConstant( pixelReg, transformation[0].Base(), 2 );
  328. }
  329. void CBaseVSShader::SetPixelShaderTextureScaledTransform( int pixelReg, int transformVar, int scaleVar )
  330. {
  331. Vector4D transformation[2];
  332. IMaterialVar* pTransformationVar = s_ppParams[transformVar];
  333. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  334. {
  335. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  336. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  337. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  338. }
  339. else
  340. {
  341. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  342. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  343. }
  344. Vector2D scale( 1, 1 );
  345. IMaterialVar* pScaleVar = s_ppParams[scaleVar];
  346. if (pScaleVar)
  347. {
  348. if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  349. pScaleVar->GetVecValue( scale.Base(), 2 );
  350. else if (pScaleVar->IsDefined())
  351. scale[0] = scale[1] = pScaleVar->GetFloatValue();
  352. }
  353. // Apply the scaling
  354. transformation[0][0] *= scale[0];
  355. transformation[0][1] *= scale[1];
  356. transformation[1][0] *= scale[0];
  357. transformation[1][1] *= scale[1];
  358. transformation[0][3] *= scale[0];
  359. transformation[1][3] *= scale[1];
  360. s_pShaderAPI->SetPixelShaderConstant( pixelReg, transformation[0].Base(), 2 );
  361. }
  362. //-----------------------------------------------------------------------------
  363. // Moves a matrix into vertex shader constants
  364. //-----------------------------------------------------------------------------
  365. void CBaseVSShader::SetVertexShaderMatrix3x4( int vertexReg, int matrixVar )
  366. {
  367. IMaterialVar* pTranslationVar = s_ppParams[matrixVar];
  368. if (pTranslationVar)
  369. {
  370. s_pShaderAPI->SetVertexShaderConstant( vertexReg, &pTranslationVar->GetMatrixValue( )[0][0], 3 );
  371. }
  372. else
  373. {
  374. VMatrix matrix;
  375. MatrixSetIdentity( matrix );
  376. s_pShaderAPI->SetVertexShaderConstant( vertexReg, &matrix[0][0], 3 );
  377. }
  378. }
  379. void CBaseVSShader::SetVertexShaderMatrix4x4( int vertexReg, int matrixVar )
  380. {
  381. IMaterialVar* pTranslationVar = s_ppParams[matrixVar];
  382. if (pTranslationVar)
  383. {
  384. #ifdef _PS3
  385. VMatrix transpose = pTranslationVar->GetMatrixValue().Transpose();
  386. s_pShaderAPI->SetVertexShaderConstant( vertexReg, &transpose[0][0], 4 );
  387. #else // _PS3
  388. s_pShaderAPI->SetVertexShaderConstant( vertexReg, &pTranslationVar->GetMatrixValue( )[0][0], 4 );
  389. #endif // !_PS3
  390. }
  391. else
  392. {
  393. VMatrix matrix;
  394. MatrixSetIdentity( matrix );
  395. s_pShaderAPI->SetVertexShaderConstant( vertexReg, &matrix[0][0], 4 );
  396. }
  397. }
  398. //-----------------------------------------------------------------------------
  399. // Loads the view matrix into vertex shader constants
  400. //-----------------------------------------------------------------------------
  401. void CBaseVSShader::LoadViewMatrixIntoVertexShaderConstant( int vertexReg )
  402. {
  403. VMatrix mat, transpose;
  404. s_pShaderAPI->GetMatrix( MATERIAL_VIEW, mat.m[0] );
  405. #ifdef _PS3
  406. transpose = mat;
  407. #else // _PS3
  408. MatrixTranspose( mat, transpose );
  409. #endif // !_PS3
  410. s_pShaderAPI->SetVertexShaderConstant( vertexReg, transpose.m[0], 3 );
  411. }
  412. //-----------------------------------------------------------------------------
  413. // Loads the projection matrix into pixel shader constants
  414. //-----------------------------------------------------------------------------
  415. void CBaseVSShader::LoadProjectionMatrixIntoVertexShaderConstant( int vertexReg )
  416. {
  417. VMatrix mat, transpose;
  418. s_pShaderAPI->GetActualProjectionMatrix( mat.m[0] );
  419. #ifdef _PS3
  420. transpose = mat;
  421. #else // _PS3
  422. MatrixTranspose( mat, transpose );
  423. #endif // !_PS3
  424. s_pShaderAPI->SetVertexShaderConstant( vertexReg, transpose.m[0], 4 );
  425. }
  426. //-----------------------------------------------------------------------------
  427. // Loads the model * view matrix into pixel shader constants
  428. //-----------------------------------------------------------------------------
  429. void CBaseVSShader::LoadModelViewMatrixIntoVertexShaderConstant( int vertexReg )
  430. {
  431. VMatrix view, model, modelView, transpose;
  432. s_pShaderAPI->GetMatrix( MATERIAL_MODEL, model.m[0] );
  433. MatrixTranspose( model, model );
  434. s_pShaderAPI->GetMatrix( MATERIAL_VIEW, view.m[0] );
  435. MatrixTranspose( view, view );
  436. MatrixMultiply( view, model, modelView );
  437. s_pShaderAPI->SetVertexShaderConstant( vertexReg, modelView.m[0], 3 );
  438. }
  439. //-----------------------------------------------------------------------------
  440. // Loads bump lightmap coordinates into the pixel shader
  441. //-----------------------------------------------------------------------------
  442. void CBaseVSShader::LoadBumpLightmapCoordinateAxes_PixelShader( int pixelReg )
  443. {
  444. Vector4D basis[3];
  445. for (int i = 0; i < 3; ++i)
  446. {
  447. memcpy( &basis[i], &g_localBumpBasis[i], 3 * sizeof(float) );
  448. basis[i][3] = 0.0f;
  449. }
  450. s_pShaderAPI->SetPixelShaderConstant( pixelReg, (float*)basis, 3 );
  451. }
  452. //-----------------------------------------------------------------------------
  453. // Loads bump lightmap coordinates into the pixel shader
  454. //-----------------------------------------------------------------------------
  455. void CBaseVSShader::LoadBumpLightmapCoordinateAxes_VertexShader( int vertexReg )
  456. {
  457. Vector4D basis[3];
  458. // transpose
  459. int i;
  460. for (i = 0; i < 3; ++i)
  461. {
  462. basis[i][0] = g_localBumpBasis[0][i];
  463. basis[i][1] = g_localBumpBasis[1][i];
  464. basis[i][2] = g_localBumpBasis[2][i];
  465. basis[i][3] = 0.0f;
  466. }
  467. s_pShaderAPI->SetVertexShaderConstant( vertexReg, (float*)basis, 3 );
  468. for (i = 0; i < 3; ++i)
  469. {
  470. memcpy( &basis[i], &g_localBumpBasis[i], 3 * sizeof(float) );
  471. basis[i][3] = 0.0f;
  472. }
  473. s_pShaderAPI->SetVertexShaderConstant( vertexReg + 3, (float*)basis, 3 );
  474. }
  475. //-----------------------------------------------------------------------------
  476. // Helper methods for pixel shader overbrighting
  477. //-----------------------------------------------------------------------------
  478. void CBaseVSShader::EnablePixelShaderOverbright( int reg, bool bEnable, bool bDivideByTwo )
  479. {
  480. // can't have other overbright values with pixel shaders as it stands.
  481. float v[4];
  482. if( bEnable )
  483. {
  484. v[0] = v[1] = v[2] = v[3] = bDivideByTwo ? OVERBRIGHT / 2.0f : OVERBRIGHT;
  485. }
  486. else
  487. {
  488. v[0] = v[1] = v[2] = v[3] = bDivideByTwo ? 1.0f / 2.0f : 1.0f;
  489. }
  490. s_pShaderAPI->SetPixelShaderConstant( reg, v, 1 );
  491. }
  492. //-----------------------------------------------------------------------------
  493. // Converts a color + alpha into a vector4
  494. //-----------------------------------------------------------------------------
  495. void CBaseVSShader::ColorVarsToVector( int colorVar, int alphaVar, Vector4D &color )
  496. {
  497. color.Init( 1.0, 1.0, 1.0, 1.0 );
  498. if ( colorVar != -1 )
  499. {
  500. IMaterialVar* pColorVar = s_ppParams[colorVar];
  501. if ( pColorVar->GetType() == MATERIAL_VAR_TYPE_VECTOR )
  502. {
  503. pColorVar->GetVecValue( color.Base(), 3 );
  504. }
  505. else
  506. {
  507. color[0] = color[1] = color[2] = pColorVar->GetFloatValue();
  508. }
  509. }
  510. if ( alphaVar != -1 )
  511. {
  512. float flAlpha = s_ppParams[alphaVar]->GetFloatValue();
  513. color[3] = clamp( flAlpha, 0.0f, 1.0f );
  514. }
  515. }
  516. #ifdef _DEBUG
  517. ConVar mat_envmaptintoverride( "mat_envmaptintoverride", "-1" );
  518. ConVar mat_envmaptintscale( "mat_envmaptintscale", "-1" );
  519. #endif
  520. //-----------------------------------------------------------------------------
  521. // Helpers for dealing with envmap tint
  522. //-----------------------------------------------------------------------------
  523. // set alphaVar to -1 to ignore it.
  524. void CBaseVSShader::SetEnvMapTintPixelShaderDynamicState( int pixelReg, int tintVar, int alphaVar, bool bConvertFromGammaToLinear )
  525. {
  526. float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  527. if( g_pConfig->bShowSpecular && g_pConfig->nFullbright != 2 )
  528. {
  529. IMaterialVar* pAlphaVar = NULL;
  530. if( alphaVar >= 0 )
  531. {
  532. pAlphaVar = s_ppParams[alphaVar];
  533. }
  534. if( pAlphaVar )
  535. {
  536. color[3] = pAlphaVar->GetFloatValue();
  537. }
  538. IMaterialVar* pTintVar = s_ppParams[tintVar];
  539. #ifdef _DEBUG
  540. pTintVar->GetVecValue( color, 3 );
  541. float envmapTintOverride = mat_envmaptintoverride.GetFloat();
  542. float envmapTintScaleOverride = mat_envmaptintscale.GetFloat();
  543. if( envmapTintOverride != -1.0f )
  544. {
  545. color[0] = color[1] = color[2] = envmapTintOverride;
  546. }
  547. if( envmapTintScaleOverride != -1.0f )
  548. {
  549. color[0] *= envmapTintScaleOverride;
  550. color[1] *= envmapTintScaleOverride;
  551. color[2] *= envmapTintScaleOverride;
  552. }
  553. if( bConvertFromGammaToLinear )
  554. {
  555. color[0] = color[0] > 1.0f ? color[0] : GammaToLinear( color[0] );
  556. color[1] = color[1] > 1.0f ? color[1] : GammaToLinear( color[1] );
  557. color[2] = color[2] > 1.0f ? color[2] : GammaToLinear( color[2] );
  558. }
  559. #else
  560. if( bConvertFromGammaToLinear )
  561. {
  562. pTintVar->GetLinearVecValue( color, 3 );
  563. }
  564. else
  565. {
  566. pTintVar->GetVecValue( color, 3 );
  567. }
  568. #endif
  569. }
  570. else
  571. {
  572. color[0] = color[1] = color[2] = color[3] = 0.0f;
  573. }
  574. s_pShaderAPI->SetPixelShaderConstant( pixelReg, color, 1 );
  575. }
  576. //-----------------------------------------------------------------------------
  577. // Sets up hw morphing state for the vertex shader
  578. //-----------------------------------------------------------------------------
  579. void CBaseVSShader::SetHWMorphVertexShaderState( int nDimConst, int nSubrectConst, VertexTextureSampler_t morphSampler )
  580. {
  581. #if !defined( _X360 ) && !defined( _PS3 )
  582. if ( !s_pShaderAPI->IsHWMorphingEnabled() )
  583. return;
  584. int nMorphWidth, nMorphHeight;
  585. s_pShaderAPI->GetStandardTextureDimensions( &nMorphWidth, &nMorphHeight, TEXTURE_MORPH_ACCUMULATOR );
  586. int nDim = s_pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_MORPH_ACCUMULATOR_4TUPLE_COUNT );
  587. float pMorphAccumSize[4] = { nMorphWidth, nMorphHeight, nDim, 0.0f };
  588. s_pShaderAPI->SetVertexShaderConstant( nDimConst, pMorphAccumSize );
  589. int nXOffset = s_pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_MORPH_ACCUMULATOR_X_OFFSET );
  590. int nYOffset = s_pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_MORPH_ACCUMULATOR_Y_OFFSET );
  591. int nWidth = s_pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_MORPH_ACCUMULATOR_SUBRECT_WIDTH );
  592. int nHeight = s_pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_MORPH_ACCUMULATOR_SUBRECT_HEIGHT );
  593. float pMorphAccumSubrect[4] = { nXOffset, nYOffset, nWidth, nHeight };
  594. s_pShaderAPI->SetVertexShaderConstant( nSubrectConst, pMorphAccumSubrect );
  595. s_pShaderAPI->BindStandardVertexTexture( morphSampler, TEXTURE_MORPH_ACCUMULATOR );
  596. #endif
  597. }
  598. //-----------------------------------------------------------------------------
  599. // GR - translucency query
  600. //-----------------------------------------------------------------------------
  601. BlendType_t CBaseVSShader::EvaluateBlendRequirements( int textureVar, bool isBaseTexture,
  602. int detailTextureVar )
  603. {
  604. // Either we've got a constant modulation
  605. bool isTranslucent = IsAlphaModulating();
  606. // Or we've got a vertex alpha
  607. isTranslucent = isTranslucent || (CurrentMaterialVarFlags() & MATERIAL_VAR_VERTEXALPHA);
  608. // Or we've got a texture alpha (for blending or alpha test)
  609. isTranslucent = isTranslucent || ( TextureIsTranslucent( textureVar, isBaseTexture ) &&
  610. !(CurrentMaterialVarFlags() & MATERIAL_VAR_ALPHATEST ) );
  611. if ( ( detailTextureVar != -1 ) && ( ! isTranslucent ) )
  612. {
  613. isTranslucent = TextureIsTranslucent( detailTextureVar, isBaseTexture );
  614. }
  615. if ( CurrentMaterialVarFlags() & MATERIAL_VAR_ADDITIVE )
  616. {
  617. return isTranslucent ? BT_BLENDADD : BT_ADD; // Additive
  618. }
  619. else
  620. {
  621. return isTranslucent ? BT_BLEND : BT_NONE; // Normal blending
  622. }
  623. }
  624. void CBaseVSShader::SetFlashlightVertexShaderConstants( bool bBump, int bumpTransformVar, bool bDetail, int detailScaleVar, bool bSetTextureTransforms )
  625. {
  626. Assert( !IsSnapshotting() );
  627. VMatrix worldToTexture;
  628. const FlashlightState_t &flashlightState = s_pShaderAPI->GetFlashlightState( worldToTexture );
  629. // Set the flashlight origin
  630. float pos[4];
  631. pos[0] = flashlightState.m_vecLightOrigin[0];
  632. pos[1] = flashlightState.m_vecLightOrigin[1];
  633. pos[2] = flashlightState.m_vecLightOrigin[2];
  634. pos[3] = 1.0f / ( ( 0.6f * flashlightState.m_FarZ ) - flashlightState.m_FarZ ); // DX8 needs this
  635. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, pos, 1 );
  636. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, worldToTexture.Base(), 4 );
  637. // Set the flashlight attenuation factors
  638. float atten[4];
  639. atten[0] = flashlightState.m_fConstantAtten;
  640. atten[1] = flashlightState.m_fLinearAtten;
  641. atten[2] = flashlightState.m_fQuadraticAtten;
  642. atten[3] = flashlightState.m_FarZAtten;
  643. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_5, atten, 1 );
  644. if ( bDetail )
  645. {
  646. SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_8, BASETEXTURETRANSFORM, detailScaleVar );
  647. }
  648. if( bSetTextureTransforms )
  649. {
  650. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, BASETEXTURETRANSFORM );
  651. if( !bDetail && bBump && bumpTransformVar != -1 )
  652. {
  653. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_8, bumpTransformVar ); // aliased on top of detail transform
  654. }
  655. }
  656. }
  657. void CBaseVSShader::DrawFlashlight_dx90( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  658. IShaderShadow* pShaderShadow, DrawFlashlight_dx90_Vars_t &vars )
  659. {
  660. bool bSFM = ( ToolsEnabled() && IsPlatformWindowsPC() && g_pHardwareConfig->SupportsPixelShaders_3_0() ) ? true : false;
  661. // FLASHLIGHTFIXME: hack . . need to fix the vertex shader so that it can deal with and without bumps for vertexlitgeneric
  662. if( !vars.m_bLightmappedGeneric )
  663. {
  664. vars.m_bBump = false;
  665. }
  666. bool bBump2 = vars.m_bWorldVertexTransition && vars.m_bBump && vars.m_nBumpmapVar2 != -1 && params[vars.m_nBumpmapVar2]->IsTexture();
  667. bool bSeamless = vars.m_fSeamlessScale != 0.0;
  668. bool bDetail = vars.m_bLightmappedGeneric && (vars.m_nDetailVar != -1) && params[vars.m_nDetailVar]->IsDefined() && (vars.m_nDetailScale != -1) && g_pConfig->UseDetailTexturing();
  669. int nDetailBlendMode = DETAIL_BLEND_MODE_RGB_EQUALS_BASE_x_DETAILx2;
  670. if ( bDetail )
  671. {
  672. nDetailBlendMode = GetIntParam( vars.m_nDetailTextureCombineMode, params );
  673. ITexture *pDetailTexture = params[vars.m_nDetailVar]->GetTextureValue();
  674. if ( pDetailTexture->GetFlags() & TEXTUREFLAGS_SSBUMP )
  675. {
  676. if ( vars.m_bBump )
  677. nDetailBlendMode = DETAIL_BLEND_MODE_SSBUMP_BUMP; // ssbump
  678. else
  679. nDetailBlendMode = DETAIL_BLEND_MODE_SSBUMP_NOBUMP; // ssbump_nobump
  680. }
  681. }
  682. if( pShaderShadow )
  683. {
  684. SetInitialShadowState();
  685. pShaderShadow->EnableDepthWrites( false );
  686. pShaderShadow->EnableAlphaWrites( false );
  687. // Alpha blend
  688. SetAdditiveBlendingShadowState( BASETEXTURE, true );
  689. // Alpha test
  690. pShaderShadow->EnableAlphaTest( IS_FLAG_SET( MATERIAL_VAR_ALPHATEST ) );
  691. if ( vars.m_nAlphaTestReference != -1 && params[vars.m_nAlphaTestReference]->GetFloatValue() > 0.0f )
  692. {
  693. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, params[vars.m_nAlphaTestReference]->GetFloatValue() );
  694. }
  695. // Spot sampler
  696. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  697. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  698. // Base sampler
  699. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  700. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  701. // Normalizing cubemap sampler
  702. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  703. // Normalizing cubemap sampler2 or normal map sampler
  704. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  705. // RandomRotation sampler
  706. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true );
  707. // Flashlight depth sampler
  708. pShaderShadow->EnableTexture( SHADER_SAMPLER7, true );
  709. //pShaderShadow->SetShadowDepthFiltering( SHADER_SAMPLER7 );
  710. if( vars.m_bWorldVertexTransition )
  711. {
  712. // $basetexture2
  713. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  714. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER4, true );
  715. }
  716. if( bBump2 )
  717. {
  718. // Normalmap2 sampler
  719. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true );
  720. }
  721. if( bDetail )
  722. {
  723. pShaderShadow->EnableTexture( SHADER_SAMPLER8, true ); // detail sampler
  724. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER8, IsSRGBDetailTexture( nDetailBlendMode ) );
  725. }
  726. pShaderShadow->EnableSRGBWrite( true );
  727. if( vars.m_bLightmappedGeneric )
  728. {
  729. #if !defined( _X360 ) && !defined( _PS3 )
  730. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  731. {
  732. DECLARE_STATIC_VERTEX_SHADER( lightmappedgeneric_flashlight_vs30 );
  733. SET_STATIC_VERTEX_SHADER_COMBO( WORLDVERTEXTRANSITION, vars.m_bWorldVertexTransition );
  734. SET_STATIC_VERTEX_SHADER_COMBO( NORMALMAP, vars.m_bBump );
  735. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS, bSeamless );
  736. SET_STATIC_VERTEX_SHADER_COMBO( DETAIL, bDetail );
  737. SET_STATIC_VERTEX_SHADER( lightmappedgeneric_flashlight_vs30 );
  738. }
  739. else
  740. #endif
  741. {
  742. DECLARE_STATIC_VERTEX_SHADER( lightmappedgeneric_flashlight_vs20 );
  743. SET_STATIC_VERTEX_SHADER_COMBO( WORLDVERTEXTRANSITION, vars.m_bWorldVertexTransition );
  744. SET_STATIC_VERTEX_SHADER_COMBO( NORMALMAP, vars.m_bBump );
  745. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS, bSeamless );
  746. SET_STATIC_VERTEX_SHADER_COMBO( DETAIL, bDetail );
  747. SET_STATIC_VERTEX_SHADER( lightmappedgeneric_flashlight_vs20 );
  748. }
  749. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  750. if( vars.m_bBump )
  751. {
  752. flags |= VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  753. }
  754. int numTexCoords = 1;
  755. if( vars.m_bWorldVertexTransition )
  756. {
  757. flags |= VERTEX_COLOR;
  758. numTexCoords = 2; // need lightmap texcoords to get alpha.
  759. }
  760. pShaderShadow->VertexShaderVertexFormat( flags, numTexCoords, 0, 0 );
  761. }
  762. else
  763. {
  764. // Need a 3.0 vs here?
  765. DECLARE_STATIC_VERTEX_SHADER( vertexlitgeneric_flashlight_vs20 );
  766. SET_STATIC_VERTEX_SHADER_COMBO( TEETH, vars.m_bTeeth );
  767. SET_STATIC_VERTEX_SHADER( vertexlitgeneric_flashlight_vs20 );
  768. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  769. int numTexCoords = 1;
  770. pShaderShadow->VertexShaderVertexFormat( flags, numTexCoords, 0, vars.m_bBump ? 4 : 0 );
  771. }
  772. int nBumpMapVariant = 0;
  773. if ( vars.m_bBump )
  774. {
  775. nBumpMapVariant = ( vars.m_bSSBump ) ? 2 : 1;
  776. }
  777. #if !defined( _X360 ) && !defined( _PS3 )
  778. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  779. {
  780. ShadowFilterMode_t nShadowFilterMode = g_pHardwareConfig->GetShadowFilterMode( false /* bForceLowQuality */, true /* bPS30 */ );
  781. DECLARE_STATIC_PIXEL_SHADER( flashlight_ps30 );
  782. SET_STATIC_PIXEL_SHADER_COMBO( SFM, bSFM );
  783. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMAP, nBumpMapVariant );
  784. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMAP2, bBump2 );
  785. SET_STATIC_PIXEL_SHADER_COMBO( WORLDVERTEXTRANSITION, vars.m_bWorldVertexTransition );
  786. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS, bSeamless );
  787. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, bDetail );
  788. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  789. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  790. SET_STATIC_PIXEL_SHADER( flashlight_ps30 );
  791. }
  792. else
  793. #endif
  794. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  795. {
  796. ShadowFilterMode_t nShadowFilterMode = g_pHardwareConfig->GetShadowFilterMode( false /* bForceLowQuality */, false /* bPS30 */ );
  797. DECLARE_STATIC_PIXEL_SHADER( flashlight_ps20b );
  798. SET_STATIC_PIXEL_SHADER_COMBO( SFM, bSFM );
  799. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMAP, nBumpMapVariant );
  800. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMAP2, bBump2 );
  801. SET_STATIC_PIXEL_SHADER_COMBO( WORLDVERTEXTRANSITION, vars.m_bWorldVertexTransition );
  802. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS, bSeamless );
  803. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, bDetail );
  804. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  805. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  806. SET_STATIC_PIXEL_SHADER( flashlight_ps20b );
  807. }
  808. else
  809. {
  810. DECLARE_STATIC_PIXEL_SHADER( flashlight_ps20 );
  811. SET_STATIC_PIXEL_SHADER_COMBO( SFM, bSFM );
  812. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMAP, nBumpMapVariant );
  813. SET_STATIC_PIXEL_SHADER_COMBO( NORMALMAP2, bBump2 );
  814. SET_STATIC_PIXEL_SHADER_COMBO( WORLDVERTEXTRANSITION, vars.m_bWorldVertexTransition );
  815. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS, bSeamless );
  816. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, bDetail );
  817. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_BLEND_MODE, nDetailBlendMode );
  818. SET_STATIC_PIXEL_SHADER( flashlight_ps20 );
  819. }
  820. FogToBlack();
  821. PI_BeginCommandBuffer();
  822. PI_SetModulationPixelShaderDynamicState( PSREG_DIFFUSE_MODULATION );
  823. PI_EndCommandBuffer();
  824. }
  825. else
  826. {
  827. VMatrix worldToTexture;
  828. ITexture *pFlashlightDepthTexture;
  829. FlashlightState_t flashlightState = ShaderApiFast( pShaderAPI )->GetFlashlightStateEx( worldToTexture, &pFlashlightDepthTexture );
  830. SetFlashLightColorFromState( flashlightState, pShaderAPI, false );
  831. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, flashlightState.m_pSpotlightTexture, flashlightState.m_nSpotlightTextureFrame );
  832. ShaderApiFast( pShaderAPI )->BindStandardTexture( SHADER_SAMPLER5, TEXTURE_BINDFLAGS_NONE, TEXTURE_SHADOW_NOISE_2D );
  833. if( pFlashlightDepthTexture && g_pConfig->ShadowDepthTexture() && flashlightState.m_bEnableShadows )
  834. {
  835. BindTexture( SHADER_SAMPLER7, TEXTURE_BINDFLAGS_SHADOWDEPTH, pFlashlightDepthTexture, 0 );
  836. // Tweaks associated with a given flashlight
  837. float tweaks[4];
  838. tweaks[0] = ShadowFilterFromState( flashlightState );
  839. tweaks[1] = ShadowAttenFromState( flashlightState );
  840. HashShadow2DJitter( flashlightState.m_flShadowJitterSeed, &tweaks[2], &tweaks[3] );
  841. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( PSREG_ENVMAP_TINT__SHADOW_TWEAKS, tweaks, 1 );
  842. // Dimensions of screen, used for screen-space noise map sampling
  843. float vScreenScale[4] = {1280.0f / 32.0f, 720.0f / 32.0f, 0, 0};
  844. int nWidth, nHeight;
  845. ShaderApiFast( pShaderAPI )->GetBackBufferDimensions( nWidth, nHeight );
  846. int nTexWidth, nTexHeight;
  847. ShaderApiFast( pShaderAPI )->GetStandardTextureDimensions( &nTexWidth, &nTexHeight, TEXTURE_SHADOW_NOISE_2D );
  848. vScreenScale[0] = (float) nWidth / nTexWidth;
  849. vScreenScale[1] = (float) nHeight / nTexHeight;
  850. vScreenScale[2] = 1.0f / flashlightState.m_flShadowMapResolution;
  851. vScreenScale[3] = 2.0f / flashlightState.m_flShadowMapResolution;
  852. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( PSREG_FLASHLIGHT_SCREEN_SCALE, vScreenScale, 1 );
  853. }
  854. else
  855. {
  856. ShaderApiFast( pShaderAPI )->BindStandardTexture( SHADER_SAMPLER7, TEXTURE_BINDFLAGS_NONE, TEXTURE_WHITE );
  857. }
  858. if( params[BASETEXTURE]->IsTexture() && g_pConfig->nFullbright != 2 )
  859. {
  860. BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, FRAME );
  861. }
  862. else
  863. {
  864. ShaderApiFast( pShaderAPI )->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_SRGBREAD, TEXTURE_GREY );
  865. }
  866. if( vars.m_bWorldVertexTransition )
  867. {
  868. Assert( vars.m_nBaseTexture2Var >= 0 && vars.m_nBaseTexture2FrameVar >= 0 );
  869. BindTexture( SHADER_SAMPLER4, TEXTURE_BINDFLAGS_SRGBREAD, vars.m_nBaseTexture2Var, vars.m_nBaseTexture2FrameVar );
  870. }
  871. ShaderApiFast( pShaderAPI )->BindStandardTexture( SHADER_SAMPLER2, TEXTURE_BINDFLAGS_NONE, TEXTURE_NORMALIZATION_CUBEMAP );
  872. if( vars.m_bBump )
  873. {
  874. BindTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_NONE, vars.m_nBumpmapVar, vars.m_nBumpmapFrame );
  875. }
  876. else
  877. {
  878. ShaderApiFast( pShaderAPI )->BindStandardTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_NONE, TEXTURE_NORMALIZATION_CUBEMAP );
  879. }
  880. if( bDetail )
  881. {
  882. BindTexture( SHADER_SAMPLER8, IsSRGBDetailTexture( nDetailBlendMode ) ? TEXTURE_BINDFLAGS_SRGBREAD : TEXTURE_BINDFLAGS_NONE, vars.m_nDetailVar );
  883. }
  884. if( vars.m_bWorldVertexTransition )
  885. {
  886. if( bBump2 )
  887. {
  888. BindTexture( SHADER_SAMPLER6, TEXTURE_BINDFLAGS_NONE, vars.m_nBumpmapVar2, vars.m_nBumpmapFrame2 );
  889. }
  890. }
  891. if( vars.m_bLightmappedGeneric )
  892. {
  893. #if !defined( _X360 ) && !defined( _PS3 )
  894. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  895. {
  896. DECLARE_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_flashlight_vs30 );
  897. SET_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_flashlight_vs30 );
  898. }
  899. else
  900. #endif
  901. {
  902. DECLARE_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_flashlight_vs20 );
  903. SET_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_flashlight_vs20 );
  904. }
  905. if ( bSeamless )
  906. {
  907. float const0[4]={ vars.m_fSeamlessScale,0,0,0};
  908. ShaderApiFast( pShaderAPI )->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, const0 );
  909. }
  910. if ( bDetail )
  911. {
  912. float vDetailConstants[4] = {1,1,1,1};
  913. if ( vars.m_nDetailTint != -1 )
  914. {
  915. params[vars.m_nDetailTint]->GetVecValue( vDetailConstants, 3 );
  916. }
  917. if ( vars.m_nDetailTextureBlendFactor != -1 )
  918. {
  919. vDetailConstants[3] = params[vars.m_nDetailTextureBlendFactor]->GetFloatValue();
  920. }
  921. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( 0, vDetailConstants, 1 );
  922. }
  923. }
  924. else
  925. {
  926. DECLARE_DYNAMIC_VERTEX_SHADER( vertexlitgeneric_flashlight_vs20 );
  927. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, ShaderApiFast( pShaderAPI )->GetCurrentNumBones() > 0 );
  928. SET_DYNAMIC_VERTEX_SHADER( vertexlitgeneric_flashlight_vs20 );
  929. if( vars.m_bTeeth )
  930. {
  931. Assert( vars.m_nTeethForwardVar >= 0 );
  932. Assert( vars.m_nTeethIllumFactorVar >= 0 );
  933. Vector4D lighting;
  934. params[vars.m_nTeethForwardVar]->GetVecValue( lighting.Base(), 3 );
  935. lighting[3] = params[vars.m_nTeethIllumFactorVar]->GetFloatValue();
  936. ShaderApiFast( pShaderAPI )->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, lighting.Base() );
  937. }
  938. }
  939. ShaderApiFast( pShaderAPI )->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  940. float vEyePos_SpecExponent[4];
  941. ShaderApiFast( pShaderAPI )->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  942. vEyePos_SpecExponent[3] = 0.0f;
  943. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  944. #if !defined( _X360 ) && !defined( _PS3 )
  945. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  946. {
  947. DECLARE_DYNAMIC_PIXEL_SHADER( flashlight_ps30 );
  948. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, flashlightState.m_bEnableShadows && ( pFlashlightDepthTexture != NULL ) );
  949. SET_DYNAMIC_PIXEL_SHADER_COMBO( UBERLIGHT, flashlightState.m_bUberlight && bSFM );
  950. SET_DYNAMIC_PIXEL_SHADER( flashlight_ps30 );
  951. SetupUberlightFromState( pShaderAPI, flashlightState );
  952. }
  953. else
  954. #endif
  955. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  956. {
  957. DECLARE_DYNAMIC_PIXEL_SHADER( flashlight_ps20b );
  958. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, flashlightState.m_bEnableShadows && ( pFlashlightDepthTexture != NULL ) );
  959. SET_DYNAMIC_PIXEL_SHADER( flashlight_ps20b );
  960. }
  961. else
  962. {
  963. DECLARE_DYNAMIC_PIXEL_SHADER( flashlight_ps20 );
  964. SET_DYNAMIC_PIXEL_SHADER( flashlight_ps20 );
  965. }
  966. float atten[4]; // Set the flashlight attenuation factors
  967. atten[0] = flashlightState.m_fConstantAtten;
  968. atten[1] = flashlightState.m_fLinearAtten;
  969. atten[2] = flashlightState.m_fQuadraticAtten;
  970. atten[3] = flashlightState.m_FarZAtten;
  971. s_pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_ATTENUATION, atten, 1 );
  972. float pos[4]; // Set the flashlight origin
  973. pos[0] = flashlightState.m_vecLightOrigin[0];
  974. pos[1] = flashlightState.m_vecLightOrigin[1];
  975. pos[2] = flashlightState.m_vecLightOrigin[2];
  976. pos[3] = flashlightState.m_FarZ;
  977. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( PSREG_FLASHLIGHT_POSITION_RIM_BOOST, pos, 1 ); // rim boost not really used here
  978. SetFlashlightVertexShaderConstants( vars.m_bBump, vars.m_nBumpTransform, bDetail, vars.m_nDetailScale, bSeamless ? false : true );
  979. }
  980. Draw();
  981. }
  982. // Take 0..1 seed and map to (u, v) coordinate to be used in shadow filter jittering...
  983. void CBaseVSShader::HashShadow2DJitter( const float fJitterSeed, float *fU, float* fV )
  984. {
  985. int nTexWidth, nTexHeight;
  986. s_pShaderAPI->GetStandardTextureDimensions( &nTexWidth, &nTexHeight, TEXTURE_SHADOW_NOISE_2D );
  987. int nSeed = fmod (fJitterSeed, 1.0f) * nTexWidth * nTexHeight;
  988. int nRow = nSeed / nTexHeight;
  989. int nCol = nSeed % nTexWidth;
  990. // Div and mod to get an individual texel in the fTexRes x fTexRes grid
  991. *fU = nRow / (float) nTexHeight; // Row
  992. *fV = nCol / (float) nTexWidth; // Column
  993. }
  994. #endif // !_STATIC_LINKED || STDSHADER_DX8_DLL_EXPORT
  995. void CBaseVSShader::DrawEqualDepthToDestAlpha( void )
  996. {
  997. #ifdef STDSHADER_DX9_DLL_EXPORT
  998. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  999. {
  1000. bool bMakeActualDrawCall = false;
  1001. if( s_pShaderShadow )
  1002. {
  1003. s_pShaderShadow->EnableColorWrites( false );
  1004. s_pShaderShadow->EnableAlphaWrites( true );
  1005. s_pShaderShadow->EnableDepthWrites( false );
  1006. s_pShaderShadow->EnableAlphaTest( false );
  1007. s_pShaderShadow->EnableBlending( false );
  1008. s_pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_EQUAL );
  1009. s_pShaderShadow->SetVertexShader( "depthtodestalpha_vs20", 0 );
  1010. s_pShaderShadow->SetPixelShader( "depthtodestalpha_ps20b", 0 );
  1011. }
  1012. if( s_pShaderAPI )
  1013. {
  1014. s_pShaderAPI->SetVertexShaderIndex( 0 );
  1015. s_pShaderAPI->SetPixelShaderIndex( 0 );
  1016. bMakeActualDrawCall = s_pShaderAPI->ShouldWriteDepthToDestAlpha();
  1017. }
  1018. Draw( bMakeActualDrawCall );
  1019. }
  1020. #else
  1021. Assert( 0 ); //probably just needs a shader update to the latest
  1022. #endif
  1023. }
  1024. #if !defined( _PS3 ) && !defined( OSX )
  1025. //-----------------------------------------------------------------------------
  1026. bool ToolsEnabled()
  1027. {
  1028. static bool bToolsMode = ( CommandLine()->CheckParm( "-tools" ) != NULL );
  1029. return bToolsMode;
  1030. }
  1031. #endif