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.

928 lines
26 KiB

  1. //===== Copyright (c) Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. // Utility class for building command buffers into memory
  7. //==================================================================//
  8. #ifndef COMMANDBUILDER_H
  9. #define COMMANDBUILDER_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "shaderapi/commandbuffer.h"
  14. #include "shaderapi/ishaderapi.h"
  15. #include "shaderlib/BaseShader.h"
  16. #include "tier1/convar.h"
  17. #ifdef _PS3
  18. #include "ps3gcm\gcmdrawstate.h"
  19. #include "ps3gcm\gcmtexture.h"
  20. #endif
  21. #ifdef DBGFLAG_ASSERT
  22. #define TRACK_STORAGE 1
  23. #else
  24. #define TRACK_STORAGE 0
  25. #endif
  26. //-----------------------------------------------------------------------------
  27. // Buffer for storing commands into
  28. //-----------------------------------------------------------------------------
  29. template<int N> class CFixedCommandStorageBuffer
  30. {
  31. public:
  32. uint8 m_Data[N];
  33. uint8 *m_pDataOut;
  34. #if TRACK_STORAGE
  35. size_t m_nNumBytesRemaining;
  36. #endif
  37. FORCEINLINE CFixedCommandStorageBuffer( void )
  38. {
  39. m_pDataOut = m_Data;
  40. #if TRACK_STORAGE
  41. m_nNumBytesRemaining = N;
  42. #endif
  43. }
  44. FORCEINLINE void EnsureCapacity( size_t sz )
  45. {
  46. #if TRACK_STORAGE
  47. if ( m_nNumBytesRemaining < sz + 32 )
  48. Error( "getting scary\n" );
  49. #endif
  50. Assert( m_nNumBytesRemaining >= sz );
  51. }
  52. template<class T> FORCEINLINE void Put( T const &nValue )
  53. {
  54. EnsureCapacity( sizeof( T ) );
  55. *( reinterpret_cast<T *>( m_pDataOut ) ) = nValue;
  56. m_pDataOut += sizeof( nValue );
  57. #if TRACK_STORAGE
  58. m_nNumBytesRemaining -= sizeof( nValue );
  59. #endif
  60. }
  61. FORCEINLINE void PutInt( int nValue )
  62. {
  63. Put( nValue );
  64. }
  65. FORCEINLINE void PutFloat( float nValue )
  66. {
  67. Put( nValue );
  68. }
  69. FORCEINLINE void PutPtr( void * pPtr )
  70. {
  71. Put( pPtr );
  72. }
  73. FORCEINLINE void PutMemory( const void *pMemory, size_t nBytes )
  74. {
  75. EnsureCapacity( nBytes );
  76. memcpy( m_pDataOut, pMemory, nBytes );
  77. m_pDataOut += nBytes;
  78. #if TRACK_STORAGE
  79. m_nNumBytesRemaining -= nBytes;
  80. #endif
  81. }
  82. FORCEINLINE uint8 *Base( void )
  83. {
  84. return m_Data;
  85. }
  86. FORCEINLINE void Reset( void )
  87. {
  88. m_pDataOut = m_Data;
  89. #if TRACK_STORAGE
  90. m_nNumBytesRemaining = N;
  91. #endif
  92. }
  93. FORCEINLINE size_t Size( void ) const
  94. {
  95. return m_pDataOut - m_Data;
  96. }
  97. };
  98. #ifdef _PS3
  99. class CDynamicCommandStorageBuffer
  100. {
  101. public:
  102. uint8 *m_Data;
  103. uint8 *m_pDataOut;
  104. #if TRACK_STORAGE_PS3
  105. size_t m_nNumBytesRemaining;
  106. #endif
  107. FORCEINLINE CDynamicCommandStorageBuffer()
  108. {
  109. m_Data = gpGcmDrawState->OpenDynECB();
  110. m_pDataOut = m_Data;
  111. #if TRACK_STORAGE_PS3
  112. m_nNumBytesRemaining = 0x1000;
  113. #endif
  114. }
  115. FORCEINLINE void EnsureCapacity( size_t sz )
  116. {
  117. #if TRACK_STORAGE_PS3
  118. if ( m_nNumBytesRemaining < sz + 32 )
  119. Error( "getting scary\n" );
  120. Assert( m_nNumBytesRemaining >= sz );
  121. #endif
  122. }
  123. template<class T> FORCEINLINE void Put( T const &nValue )
  124. {
  125. EnsureCapacity( sizeof( T ) );
  126. *( reinterpret_cast<T *>( m_pDataOut ) ) = nValue;
  127. m_pDataOut += sizeof( nValue );
  128. #if TRACK_STORAGE_PS3
  129. m_nNumBytesRemaining -= sizeof( nValue );
  130. #endif
  131. }
  132. FORCEINLINE void PutInt( int nValue )
  133. {
  134. Put( nValue );
  135. }
  136. FORCEINLINE void PutFloat( float nValue )
  137. {
  138. Put( nValue );
  139. }
  140. FORCEINLINE void PutPtr( void * pPtr )
  141. {
  142. Put( pPtr );
  143. }
  144. FORCEINLINE void PutMemory( const void *pMemory, size_t nBytes )
  145. {
  146. EnsureCapacity( nBytes );
  147. memcpy( m_pDataOut, pMemory, nBytes );
  148. m_pDataOut += nBytes;
  149. #if TRACK_STORAGE_PS3
  150. m_nNumBytesRemaining -= nBytes;
  151. #endif
  152. }
  153. FORCEINLINE uint8 *Base( void )
  154. {
  155. return m_Data;
  156. }
  157. FORCEINLINE void Reset( void )
  158. {
  159. m_pDataOut = m_Data;
  160. #if TRACK_STORAGE_PS3
  161. m_nNumBytesRemaining = N;
  162. #endif
  163. }
  164. FORCEINLINE size_t Size( void ) const
  165. {
  166. return m_pDataOut - m_Data;
  167. }
  168. };
  169. #endif
  170. //-----------------------------------------------------------------------------
  171. // Base class used to build up command buffers
  172. //-----------------------------------------------------------------------------
  173. template<class S> class CBaseCommandBufferBuilder
  174. {
  175. public:
  176. #ifdef _PS3
  177. ALIGN16 S m_Storage ALIGN16_POST;
  178. #else
  179. S m_Storage;
  180. #endif
  181. FORCEINLINE void End( void )
  182. {
  183. m_Storage.PutInt( CBCMD_END );
  184. }
  185. FORCEINLINE IMaterialVar *Param( int nVar ) const
  186. {
  187. return CBaseShader::s_ppParams[nVar];
  188. }
  189. FORCEINLINE void Reset( void )
  190. {
  191. m_Storage.Reset();
  192. }
  193. FORCEINLINE size_t Size( void ) const
  194. {
  195. return m_Storage.Size();
  196. }
  197. FORCEINLINE uint8 *Base( void )
  198. {
  199. return m_Storage.Base();
  200. }
  201. FORCEINLINE void OutputConstantData( float const *pSrcData )
  202. {
  203. m_Storage.PutFloat( pSrcData[0] );
  204. m_Storage.PutFloat( pSrcData[1] );
  205. m_Storage.PutFloat( pSrcData[2] );
  206. m_Storage.PutFloat( pSrcData[3] );
  207. }
  208. FORCEINLINE void OutputConstantData4( float flVal0, float flVal1, float flVal2, float flVal3 )
  209. {
  210. m_Storage.PutFloat( flVal0 );
  211. m_Storage.PutFloat( flVal1 );
  212. m_Storage.PutFloat( flVal2 );
  213. m_Storage.PutFloat( flVal3 );
  214. }
  215. };
  216. //-----------------------------------------------------------------------------
  217. // Used by SetPixelShaderFlashlightState
  218. //-----------------------------------------------------------------------------
  219. struct CBCmdSetPixelShaderFlashlightState_t
  220. {
  221. Sampler_t m_LightSampler;
  222. Sampler_t m_DepthSampler;
  223. Sampler_t m_ShadowNoiseSampler;
  224. int m_nColorConstant;
  225. int m_nAttenConstant;
  226. int m_nOriginConstant;
  227. int m_nDepthTweakConstant;
  228. int m_nScreenScaleConstant;
  229. int m_nWorldToTextureConstant;
  230. bool m_bFlashlightNoLambert;
  231. bool m_bSinglePassFlashlight;
  232. };
  233. //-----------------------------------------------------------------------------
  234. // Used to build a per-pass command buffer
  235. //-----------------------------------------------------------------------------
  236. template<class S> class CCommandBufferBuilder : public CBaseCommandBufferBuilder<S>
  237. {
  238. typedef CBaseCommandBufferBuilder<S> PARENT;
  239. #ifdef _PS3
  240. uint32 m_numPs3Tex;
  241. #endif
  242. public:
  243. #ifdef _PS3
  244. FORCEINLINE CCommandBufferBuilder()
  245. {
  246. // For PS3, command buffers begin with up to four Std textures
  247. m_numPs3Tex = 0;
  248. this->m_Storage.PutInt(CBCMD_LENGTH);
  249. this->m_Storage.PutInt(0);
  250. this->m_Storage.PutInt(CBCMD_PS3TEX);
  251. for(int i = 0; i < CBCMD_MAX_PS3TEX; i++) this->m_Storage.PutInt(0);
  252. }
  253. FORCEINLINE void Reset()
  254. {
  255. this->m_Storage.Reset();
  256. m_numPs3Tex = 0;
  257. this->m_Storage.PutInt(CBCMD_LENGTH);
  258. this->m_Storage.PutInt(0);
  259. this->m_Storage.PutInt(CBCMD_PS3TEX);
  260. for(int i = 0; i < CBCMD_MAX_PS3TEX; i++) this->m_Storage.PutInt(0);
  261. }
  262. FORCEINLINE int* GetPs3Textures()
  263. {
  264. return (int*) (this->m_Storage.Base() + sizeof(int) + 2*sizeof(int));
  265. }
  266. #endif
  267. FORCEINLINE void End( void )
  268. {
  269. this->m_Storage.PutInt( CBCMD_END );
  270. #ifdef _PS3
  271. uint32 len = this->m_Storage.Size();
  272. if ( (this->m_Storage.m_Data >= g_aDynECB) && (this->m_Storage.m_Data < &g_aDynECB[sizeof(g_aDynECB)]) )
  273. {
  274. gpGcmDrawState->CloseDynECB(len);
  275. }
  276. uint32* pLength = (uint32*)(this->m_Storage.m_Data + 4);
  277. if (pLength[-1] != CBCMD_LENGTH) Error("Length missing\n");
  278. *pLength = len;
  279. #endif
  280. }
  281. FORCEINLINE void SetPixelShaderConstants( int nFirstConstant, int nConstants )
  282. {
  283. this->m_Storage.PutInt( CBCMD_SET_PIXEL_SHADER_FLOAT_CONST );
  284. this->m_Storage.PutInt( nFirstConstant );
  285. this->m_Storage.PutInt( nConstants );
  286. }
  287. FORCEINLINE void OutputConstantData( float const *pSrcData )
  288. {
  289. this->m_Storage.PutFloat( pSrcData[0] );
  290. this->m_Storage.PutFloat( pSrcData[1] );
  291. this->m_Storage.PutFloat( pSrcData[2] );
  292. this->m_Storage.PutFloat( pSrcData[3] );
  293. }
  294. FORCEINLINE void OutputConstantData4( float flVal0, float flVal1, float flVal2, float flVal3 )
  295. {
  296. this->m_Storage.PutFloat( flVal0 );
  297. this->m_Storage.PutFloat( flVal1 );
  298. this->m_Storage.PutFloat( flVal2 );
  299. this->m_Storage.PutFloat( flVal3 );
  300. }
  301. FORCEINLINE void SetPixelShaderConstant( int nFirstConstant, float const *pSrcData, int nNumConstantsToSet )
  302. {
  303. SetPixelShaderConstants( nFirstConstant, nNumConstantsToSet );
  304. this->m_Storage.PutMemory( pSrcData, 4 * sizeof( float ) * nNumConstantsToSet );
  305. }
  306. FORCEINLINE void SetPixelShaderConstant( int nFirstConstant, int nVar )
  307. {
  308. SetPixelShaderConstant( nFirstConstant, this->Param( nVar )->GetVecValue() );
  309. }
  310. void SetPixelShaderConstantGammaToLinear( int pixelReg, int constantVar )
  311. {
  312. float val[4];
  313. this->Param(constantVar)->GetVecValue( val, 3 );
  314. val[0] = val[0] > 1.0f ? val[0] : GammaToLinear( val[0] );
  315. val[1] = val[1] > 1.0f ? val[1] : GammaToLinear( val[1] );
  316. val[2] = val[2] > 1.0f ? val[2] : GammaToLinear( val[2] );
  317. val[3] = 1.0;
  318. SetPixelShaderConstant( pixelReg, val );
  319. }
  320. FORCEINLINE void SetPixelShaderConstant( int nFirstConstant, float const *pSrcData )
  321. {
  322. SetPixelShaderConstants( nFirstConstant, 1 );
  323. OutputConstantData( pSrcData );
  324. }
  325. FORCEINLINE void SetPixelShaderConstant4( int nFirstConstant, float flVal0, float flVal1, float flVal2, float flVal3 )
  326. {
  327. SetPixelShaderConstants( nFirstConstant, 1 );
  328. OutputConstantData4( flVal0, flVal1, flVal2, flVal3 );
  329. }
  330. FORCEINLINE void SetPixelShaderConstant_W( int pixelReg, int constantVar, float fWValue )
  331. {
  332. if ( constantVar != -1 )
  333. {
  334. float val[3];
  335. this->Param(constantVar)->GetVecValue( val, 3);
  336. SetPixelShaderConstant4( pixelReg, val[0], val[1], val[2], fWValue );
  337. }
  338. }
  339. void SetPixelShaderTextureTransform( int vertexReg, int transformVar )
  340. {
  341. Vector4D transformation[2];
  342. IMaterialVar* pTransformationVar = ( transformVar >= 0 ) ? this->Param( transformVar ) : NULL;
  343. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  344. {
  345. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  346. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  347. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  348. }
  349. else
  350. {
  351. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  352. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  353. }
  354. SetPixelShaderConstant( vertexReg, transformation[0].Base(), 2 );
  355. }
  356. FORCEINLINE void SetVertexShaderConstant( int nFirstConstant, float const *pSrcData )
  357. {
  358. this->m_Storage.PutInt( CBCMD_SET_VERTEX_SHADER_FLOAT_CONST );
  359. this->m_Storage.PutInt( nFirstConstant );
  360. this->m_Storage.PutInt( 1 );
  361. OutputConstantData( pSrcData );
  362. }
  363. FORCEINLINE void SetVertexShaderConstant( int nFirstConstant, float const *pSrcData, int nConsts )
  364. {
  365. this->m_Storage.PutInt( CBCMD_SET_VERTEX_SHADER_FLOAT_CONST );
  366. this->m_Storage.PutInt( nFirstConstant );
  367. this->m_Storage.PutInt( nConsts );
  368. this->m_Storage.PutMemory( pSrcData, 4 * nConsts * sizeof( float ) );
  369. }
  370. FORCEINLINE void SetVertexShaderConstant4( int nFirstConstant, float flVal0, float flVal1, float flVal2, float flVal3 )
  371. {
  372. this->m_Storage.PutInt( CBCMD_SET_VERTEX_SHADER_FLOAT_CONST );
  373. this->m_Storage.PutInt( nFirstConstant );
  374. this->m_Storage.PutInt( 1 );
  375. this->m_Storage.PutFloat( flVal0 );
  376. this->m_Storage.PutFloat( flVal1 );
  377. this->m_Storage.PutFloat( flVal2 );
  378. this->m_Storage.PutFloat( flVal3 );
  379. }
  380. void SetVertexShaderTextureTransform( int vertexReg, int transformVar )
  381. {
  382. Vector4D transformation[2];
  383. IMaterialVar* pTransformationVar = ( transformVar >= 0 ) ? this->Param( transformVar ) : NULL;
  384. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  385. {
  386. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  387. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  388. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  389. }
  390. else
  391. {
  392. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  393. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  394. }
  395. SetVertexShaderConstant( vertexReg, transformation[0].Base(), 2 );
  396. }
  397. void SetVertexShaderTextureScaledTransformRotate( int vertexReg, int transformVar, int scaleVar, int rotateVar )
  398. {
  399. Vector2D scale( 1, 1 );
  400. IMaterialVar* pScaleVar = this->Param( scaleVar );
  401. if (pScaleVar)
  402. {
  403. if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  404. pScaleVar->GetVecValue( scale.Base(), 2 );
  405. else if (pScaleVar->IsDefined())
  406. scale[0] = scale[1] = pScaleVar->GetFloatValue();
  407. }
  408. float flRotateVar = 0.0f;
  409. IMaterialVar* pRotateVar = this->Param( rotateVar );
  410. if ( pRotateVar && pRotateVar->IsDefined() )
  411. {
  412. flRotateVar = pRotateVar->GetFloatValue();
  413. }
  414. Vector4D transformation[2];
  415. IMaterialVar* pTransformationVar = this->Param( transformVar );
  416. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  417. {
  418. VMatrix matRot = pTransformationVar->GetMatrixValue();
  419. MatrixTranslate( matRot, Vector( 0.5, 0.5, 0 ) );
  420. MatrixRotate( matRot, Vector( 0, 0, 1), flRotateVar );
  421. MatrixTranslate( matRot, Vector( -0.5 * scale[0], -0.5 * scale[1], 0 ) );
  422. matRot = matRot.Scale( Vector(scale[0], scale[1], 1) );
  423. transformation[0].Init( matRot[0][0], matRot[0][1], matRot[0][2], matRot[0][3] );
  424. transformation[1].Init( matRot[1][0], matRot[1][1], matRot[1][2], matRot[1][3] );
  425. SetVertexShaderConstant( vertexReg, transformation[0].Base(), 2 );
  426. }
  427. }
  428. void SetVertexShaderTextureScaledTransform( int vertexReg, int transformVar, int scaleVar )
  429. {
  430. Vector4D transformation[2];
  431. IMaterialVar* pTransformationVar = this->Param( transformVar );
  432. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  433. {
  434. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  435. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  436. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  437. }
  438. else
  439. {
  440. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  441. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  442. }
  443. Vector2D scale( 1, 1 );
  444. IMaterialVar* pScaleVar = this->Param( scaleVar );
  445. if (pScaleVar)
  446. {
  447. if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  448. pScaleVar->GetVecValue( scale.Base(), 2 );
  449. else if (pScaleVar->IsDefined())
  450. scale[0] = scale[1] = pScaleVar->GetFloatValue();
  451. }
  452. // Apply the scaling
  453. transformation[0][0] *= scale[0];
  454. transformation[0][1] *= scale[1];
  455. transformation[1][0] *= scale[0];
  456. transformation[1][1] *= scale[1];
  457. transformation[0][3] *= scale[0];
  458. transformation[1][3] *= scale[1];
  459. SetVertexShaderConstant( vertexReg, transformation[0].Base(), 2 );
  460. }
  461. FORCEINLINE void SetEnvMapTintPixelShaderDynamicState( int pixelReg, int tintVar )
  462. {
  463. if( g_pConfig->bShowSpecular && g_pConfig->nFullbright != 2 )
  464. {
  465. SetPixelShaderConstant( pixelReg, this->Param( tintVar)->GetVecValue() );
  466. }
  467. else
  468. {
  469. SetPixelShaderConstant4( pixelReg, 0.0, 0.0, 0.0, 0.0 );
  470. }
  471. }
  472. FORCEINLINE void SetEnvMapTintPixelShaderDynamicStateGammaToLinear( int pixelReg, int tintVar, float fAlphaVal = 1.0f )
  473. {
  474. if( g_pConfig->bShowSpecular && g_pConfig->nFullbright != 2 )
  475. {
  476. float color[4];
  477. color[3] = fAlphaVal;
  478. //this->Param( tintVar)->GetLinearVecValue( color, 3 );
  479. // (wills) converted this line to the following so that envmaptint can be over-driven beyond 0-1 range
  480. this->Param( tintVar)->GetVecValue( color, 3 );
  481. color[0] = GammaToLinearFullRange( color[0] );
  482. color[1] = GammaToLinearFullRange( color[1] );
  483. color[2] = GammaToLinearFullRange( color[2] );
  484. SetPixelShaderConstant( pixelReg, color );
  485. }
  486. else
  487. {
  488. SetPixelShaderConstant4( pixelReg, 0.0, 0.0, 0.0, fAlphaVal );
  489. }
  490. }
  491. FORCEINLINE void StoreEyePosInPixelShaderConstant( int nConst, float wValue = 1.0f )
  492. {
  493. this->m_Storage.PutInt( CBCMD_STORE_EYE_POS_IN_PSCONST );
  494. this->m_Storage.PutInt( nConst );
  495. this->m_Storage.PutFloat( wValue );
  496. }
  497. FORCEINLINE void SetPixelShaderFogParams( int nReg )
  498. {
  499. this->m_Storage.PutInt( CBCMD_SETPIXELSHADERFOGPARAMS );
  500. this->m_Storage.PutInt( nReg );
  501. }
  502. #ifndef _PS3
  503. FORCEINLINE void BindStandardTexture( Sampler_t nSampler, TextureBindFlags_t nBindFlags, StandardTextureId_t nTextureId )
  504. {
  505. this->m_Storage.PutInt( CBCMD_BIND_STANDARD_TEXTURE );
  506. this->m_Storage.PutInt( nSampler | nBindFlags );
  507. this->m_Storage.PutInt( nTextureId );
  508. }
  509. FORCEINLINE void BindTexture( CBaseShader *pShader, Sampler_t nSampler, TextureBindFlags_t nBindFlags, ITexture *pTexture, int nFrame )
  510. {
  511. ShaderAPITextureHandle_t hTexture = pShader->GetShaderAPITextureBindHandle( pTexture, nFrame );
  512. Assert( hTexture != INVALID_SHADERAPI_TEXTURE_HANDLE );
  513. this->m_Storage.PutInt( CBCMD_BIND_SHADERAPI_TEXTURE_HANDLE );
  514. this->m_Storage.PutInt( nSampler | nBindFlags );
  515. this->m_Storage.Put( hTexture );
  516. }
  517. FORCEINLINE void BindTexture( Sampler_t nSampler, TextureBindFlags_t nBindFlags, ShaderAPITextureHandle_t hTexture )
  518. {
  519. Assert( hTexture != INVALID_SHADERAPI_TEXTURE_HANDLE );
  520. this->m_Storage.PutInt( CBCMD_BIND_SHADERAPI_TEXTURE_HANDLE );
  521. this->m_Storage.PutInt( nSampler | nBindFlags );
  522. this->m_Storage.Put( hTexture );
  523. }
  524. #else
  525. FORCEINLINE void BindTexture( Sampler_t nSampler, TextureBindFlags_t nBindFlags, ShaderAPITextureHandle_t hTexture )
  526. {
  527. Assert( hTexture != INVALID_SHADERAPI_TEXTURE_HANDLE );
  528. if (m_numPs3Tex >= CBCMD_MAX_PS3TEX)
  529. {
  530. Error("Too many textures in single draw ECB\n");
  531. }
  532. int* pOffset = GetPs3Textures() + m_numPs3Tex;
  533. CPs3BindParams_t tex;
  534. tex.m_sampler = nSampler;
  535. tex.m_nBindFlags = nBindFlags >> 24; // Top byte only
  536. tex.m_hTexture = hTexture;
  537. tex.m_boundStd = -1;
  538. tex.m_nBindTexIndex = m_numPs3Tex;
  539. this->m_Storage.PutInt( CBCMD_BIND_PS3_TEXTURE );
  540. *pOffset = (this->m_Storage.m_pDataOut - this->m_Storage.m_Data);
  541. this->m_Storage.PutMemory(&tex, sizeof(tex)) ;
  542. m_numPs3Tex++;
  543. }
  544. FORCEINLINE void BindStandardTexture( Sampler_t nSampler, TextureBindFlags_t nBindFlags, StandardTextureId_t nTextureId )
  545. {
  546. if (m_numPs3Tex >= CBCMD_MAX_PS3TEX)
  547. {
  548. Error("Too many textures in single draw ECB\n");
  549. }
  550. int* pOffset = GetPs3Textures() + m_numPs3Tex;
  551. CPs3BindParams_t tex;
  552. tex.m_sampler = nSampler;
  553. tex.m_nBindFlags = nBindFlags >> 24;
  554. tex.m_boundStd = nTextureId;
  555. tex.m_hTexture = -1;
  556. tex.m_nBindTexIndex = m_numPs3Tex;
  557. this->m_Storage.PutInt( CBCMD_BIND_PS3_STANDARD_TEXTURE );
  558. *pOffset = (this->m_Storage.m_pDataOut - this->m_Storage.m_Data);
  559. this->m_Storage.PutMemory(&tex, sizeof(tex)) ;
  560. m_numPs3Tex++;
  561. }
  562. FORCEINLINE void BindTexture( CBaseShader *pShader, Sampler_t nSampler, TextureBindFlags_t nBindFlags, ITexture *pTexture, int nFrame )
  563. {
  564. ShaderAPITextureHandle_t hTexture = pShader->GetShaderAPITextureBindHandle( pTexture, nFrame );
  565. BindTexture(nSampler, nBindFlags, hTexture);
  566. }
  567. #endif
  568. FORCEINLINE void BindTexture( CBaseShader *pShader, Sampler_t nSampler, TextureBindFlags_t nBindFlags, int nTextureVar, int nFrameVar = -1 )
  569. {
  570. ShaderAPITextureHandle_t hTexture = pShader->GetShaderAPITextureBindHandle( nTextureVar, nFrameVar );
  571. BindTexture( nSampler, nBindFlags, hTexture );
  572. }
  573. // Same as BindTexture, except it checks to see if the texture handle is actually the "internal" env_cubemap. If so, it binds it as a standard texture so the proper texture bind flags are
  574. // recorded during instance rendering in CShaderAPIDX8.
  575. FORCEINLINE void BindEnvCubemapTexture( CBaseShader *pShader, Sampler_t nSampler, TextureBindFlags_t nBindFlags, int nTextureVar, int nFrameVar = -1 )
  576. {
  577. Assert( nTextureVar != -1 );
  578. Assert( CBaseShader::GetPPParams() );
  579. if ( CBaseShader::GetPPParams()[nTextureVar]->IsTextureValueInternalEnvCubemap() )
  580. {
  581. BindStandardTexture( nSampler, nBindFlags, TEXTURE_LOCAL_ENV_CUBEMAP );
  582. }
  583. else
  584. {
  585. ShaderAPITextureHandle_t hTexture = pShader->GetShaderAPITextureBindHandle( nTextureVar, nFrameVar );
  586. BindTexture( nSampler, nBindFlags, hTexture );
  587. }
  588. }
  589. FORCEINLINE void BindMultiTexture( CBaseShader *pShader, Sampler_t nSampler1, Sampler_t nSampler2, TextureBindFlags_t nBindFlags, int nTextureVar, int nFrameVar )
  590. {
  591. ShaderAPITextureHandle_t hTexture = pShader->GetShaderAPITextureBindHandle( nTextureVar, nFrameVar, 0 );
  592. BindTexture( nSampler1, nBindFlags, hTexture );
  593. hTexture = pShader->GetShaderAPITextureBindHandle( nTextureVar, nFrameVar, 1 );
  594. BindTexture( nSampler2, nBindFlags, hTexture );
  595. }
  596. FORCEINLINE void SetPixelShaderIndex( int nIndex )
  597. {
  598. this->m_Storage.PutInt( CBCMD_SET_PSHINDEX );
  599. this->m_Storage.PutInt( nIndex );
  600. }
  601. FORCEINLINE void SetVertexShaderIndex( int nIndex )
  602. {
  603. this->m_Storage.PutInt( CBCMD_SET_VSHINDEX );
  604. this->m_Storage.PutInt( nIndex );
  605. }
  606. FORCEINLINE void SetDepthFeatheringShaderConstants( int iConstant, float fDepthBlendScale )
  607. {
  608. this->m_Storage.PutInt( CBCMD_SET_DEPTH_FEATHERING_CONST );
  609. this->m_Storage.PutInt( iConstant );
  610. this->m_Storage.PutFloat( fDepthBlendScale );
  611. }
  612. FORCEINLINE void SetVertexShaderFlashlightState( int iConstant )
  613. {
  614. this->m_Storage.PutInt( CBCMD_SET_VERTEX_SHADER_FLASHLIGHT_STATE );
  615. this->m_Storage.PutInt( iConstant );
  616. }
  617. FORCEINLINE void SetPixelShaderFlashlightState( const CBCmdSetPixelShaderFlashlightState_t &state )
  618. {
  619. this->m_Storage.PutInt( CBCMD_SET_PIXEL_SHADER_FLASHLIGHT_STATE );
  620. this->m_Storage.PutInt( state.m_LightSampler );
  621. this->m_Storage.PutInt( state.m_DepthSampler );
  622. this->m_Storage.PutInt( state.m_ShadowNoiseSampler );
  623. this->m_Storage.PutInt( state.m_nColorConstant );
  624. this->m_Storage.PutInt( state.m_nAttenConstant );
  625. this->m_Storage.PutInt( state.m_nOriginConstant );
  626. this->m_Storage.PutInt( state.m_nDepthTweakConstant );
  627. this->m_Storage.PutInt( state.m_nScreenScaleConstant );
  628. this->m_Storage.PutInt( state.m_nWorldToTextureConstant );
  629. this->m_Storage.PutInt( state.m_bFlashlightNoLambert );
  630. this->m_Storage.PutInt( state.m_bSinglePassFlashlight );
  631. }
  632. FORCEINLINE void SetPixelShaderUberLightState( int iEdge0Const, int iEdge1Const, int iEdgeOOWConst, int iShearRoundConst, int iAABBConst, int iWorldToLightConst )
  633. {
  634. this->m_Storage.PutInt( CBCMD_SET_PIXEL_SHADER_UBERLIGHT_STATE );
  635. this->m_Storage.PutInt( iEdge0Const );
  636. this->m_Storage.PutInt( iEdge1Const );
  637. this->m_Storage.PutInt( iEdgeOOWConst );
  638. this->m_Storage.PutInt( iShearRoundConst );
  639. this->m_Storage.PutInt( iAABBConst );
  640. this->m_Storage.PutInt( iWorldToLightConst );
  641. }
  642. FORCEINLINE void Goto( uint8 *pCmdBuf )
  643. {
  644. this->m_Storage.PutInt( CBCMD_JUMP );
  645. this->m_Storage.PutPtr( pCmdBuf );
  646. }
  647. FORCEINLINE void Call( uint8 *pCmdBuf )
  648. {
  649. this->m_Storage.PutInt( CBCMD_JSR );
  650. this->m_Storage.PutPtr( pCmdBuf );
  651. }
  652. #ifndef _PS3
  653. FORCEINLINE void Reset( void )
  654. {
  655. this->m_Storage.Reset();
  656. }
  657. #endif
  658. FORCEINLINE size_t Size( void ) const
  659. {
  660. return this->m_Storage.Size();
  661. }
  662. FORCEINLINE uint8 *Base( void )
  663. {
  664. return this->m_Storage.Base();
  665. }
  666. FORCEINLINE void SetVertexShaderNearAndFarZ( int iRegNum )
  667. {
  668. this->m_Storage.PutInt( CBCMD_SET_VERTEX_SHADER_NEARZFARZ_STATE );
  669. this->m_Storage.PutInt( iRegNum );
  670. }
  671. };
  672. //-----------------------------------------------------------------------------
  673. // Builds a command buffer specifically for per-instance state setting
  674. //-----------------------------------------------------------------------------
  675. template<class S> class CInstanceCommandBufferBuilder : public CBaseCommandBufferBuilder< S >
  676. {
  677. typedef CBaseCommandBufferBuilder< S > PARENT;
  678. public:
  679. FORCEINLINE void End( void )
  680. {
  681. this->m_Storage.PutInt( CBICMD_END );
  682. }
  683. FORCEINLINE void SetPixelShaderLocalLighting( int nConst )
  684. {
  685. this->m_Storage.PutInt( CBICMD_SETPIXELSHADERLOCALLIGHTING );
  686. this->m_Storage.PutInt( nConst );
  687. }
  688. FORCEINLINE void SetPixelShaderAmbientLightCube( int nConst )
  689. {
  690. this->m_Storage.PutInt( CBICMD_SETPIXELSHADERAMBIENTLIGHTCUBE );
  691. this->m_Storage.PutInt( nConst );
  692. }
  693. FORCEINLINE void SetVertexShaderLocalLighting( )
  694. {
  695. this->m_Storage.PutInt( CBICMD_SETVERTEXSHADERLOCALLIGHTING );
  696. }
  697. FORCEINLINE void SetVertexShaderAmbientLightCube( void )
  698. {
  699. this->m_Storage.PutInt( CBICMD_SETVERTEXSHADERAMBIENTLIGHTCUBE );
  700. }
  701. FORCEINLINE void SetSkinningMatrices( void )
  702. {
  703. this->m_Storage.PutInt( CBICMD_SETSKINNINGMATRICES );
  704. }
  705. FORCEINLINE void SetPixelShaderAmbientLightCubeLuminance( int nConst )
  706. {
  707. this->m_Storage.PutInt( CBICMD_SETPIXELSHADERAMBIENTLIGHTCUBELUMINANCE );
  708. this->m_Storage.PutInt( nConst );
  709. }
  710. FORCEINLINE void SetPixelShaderGlintDamping( int nConst )
  711. {
  712. this->m_Storage.PutInt( CBICMD_SETPIXELSHADERGLINTDAMPING );
  713. this->m_Storage.PutInt( nConst );
  714. }
  715. FORCEINLINE void SetModulationPixelShaderDynamicState_LinearColorSpace_LinearScale( int nConst, const Vector &vecGammaSpaceColor2Factor, float scale )
  716. {
  717. this->m_Storage.PutInt( CBICMD_SETMODULATIONPIXELSHADERDYNAMICSTATE_LINEARCOLORSPACE_LINEARSCALE );
  718. this->m_Storage.PutInt( nConst );
  719. this->m_Storage.Put( vecGammaSpaceColor2Factor );
  720. this->m_Storage.PutFloat( 1.0 ); // pad for vector4
  721. this->m_Storage.PutFloat( scale );
  722. }
  723. FORCEINLINE void SetModulationPixelShaderDynamicState_LinearScale( int nConst, const Vector &vecGammaSpaceColor2Factor, float scale )
  724. {
  725. this->m_Storage.PutInt( CBICMD_SETMODULATIONPIXELSHADERDYNAMICSTATE_LINEARSCALE );
  726. this->m_Storage.PutInt( nConst );
  727. this->m_Storage.Put( vecGammaSpaceColor2Factor );
  728. this->m_Storage.PutFloat( 1.0 ); // alpha modulation wants 1 1.0 here for simd
  729. this->m_Storage.PutFloat( scale );
  730. }
  731. FORCEINLINE void SetModulationPixelShaderDynamicState_LinearScale_ScaleInW( int nConst, const Vector &vecGammaSpaceColor2Factor, float scale )
  732. {
  733. this->m_Storage.PutInt( CBICMD_SETMODULATIONPIXELSHADERDYNAMICSTATE_LINEARSCALE_SCALEINW );
  734. this->m_Storage.PutInt( nConst );
  735. this->m_Storage.Put( vecGammaSpaceColor2Factor );
  736. this->m_Storage.PutFloat( scale );
  737. }
  738. FORCEINLINE void SetModulationPixelShaderDynamicState_LinearColorSpace( int nConst, const Vector &vecGammaSpaceColor2Factor )
  739. {
  740. this->m_Storage.PutInt( CBICMD_SETMODULATIONPIXELSHADERDYNAMICSTATE_LINEARCOLORSPACE );
  741. this->m_Storage.PutInt( nConst );
  742. this->m_Storage.Put( vecGammaSpaceColor2Factor );
  743. this->m_Storage.PutFloat( 1.0 ); // pad with a 1 for vector4d simd access. Important that this be a 1 because alpha is multipled by it.
  744. }
  745. FORCEINLINE void SetModulationPixelShaderDynamicState( int nConst, const Vector &vecGammaSpaceColor2Factor )
  746. {
  747. this->m_Storage.PutInt( CBICMD_SETMODULATIONPIXELSHADERDYNAMICSTATE );
  748. this->m_Storage.PutInt( nConst );
  749. this->m_Storage.Put( vecGammaSpaceColor2Factor );
  750. }
  751. FORCEINLINE void SetModulationPixelShaderDynamicState_Identity( int nConst )
  752. {
  753. this->m_Storage.PutInt( CBICMD_SETMODULATIONPIXELSHADERDYNAMICSTATE_IDENTITY );
  754. this->m_Storage.PutInt( nConst );
  755. }
  756. FORCEINLINE void SetModulationVertexShaderDynamicState( int nConst, const Vector &vecGammaSpaceColor2Factor )
  757. {
  758. this->m_Storage.PutInt( CBICMD_SETMODULATIONVERTEXSHADERDYNAMICSTATE );
  759. this->m_Storage.PutInt( nConst );
  760. this->m_Storage.Put( vecGammaSpaceColor2Factor );
  761. }
  762. FORCEINLINE void SetModulationVertexShaderDynamicState_LinearScale( int nConst, const Vector &vecGammaSpaceColor2Factor, float flScale )
  763. {
  764. this->m_Storage.PutInt( CBICMD_SETMODULATIONVERTEXSHADERDYNAMICSTATE_LINEARSCALE );
  765. this->m_Storage.PutInt( nConst );
  766. this->m_Storage.Put( vecGammaSpaceColor2Factor );
  767. this->m_Storage.PutFloat( flScale );
  768. }
  769. };
  770. #endif // commandbuilder_h