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.

407 lines
10 KiB

  1. //========= Copyright 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. #ifndef COMMANDBUFFER_H
  11. #include "shaderapi/commandbuffer.h"
  12. #endif
  13. #include "BaseVSShader.h"
  14. #include "shaderapi/ishaderapi.h"
  15. #ifdef _WIN32
  16. #pragma once
  17. #endif
  18. extern ConVar my_mat_fullbright;
  19. template<int N> class CFixedCommandStorageBuffer
  20. {
  21. public:
  22. uint8 m_Data[N];
  23. uint8 *m_pDataOut;
  24. #ifdef DBGFLAG_ASSERT
  25. size_t m_nNumBytesRemaining;
  26. #endif
  27. FORCEINLINE CFixedCommandStorageBuffer( void )
  28. {
  29. m_pDataOut = m_Data;
  30. #ifdef DBGFLAG_ASSERT
  31. m_nNumBytesRemaining = N;
  32. #endif
  33. }
  34. FORCEINLINE void EnsureCapacity( size_t sz )
  35. {
  36. Assert( m_nNumBytesRemaining >= sz );
  37. }
  38. template<class T> FORCEINLINE void Put( T const &nValue )
  39. {
  40. EnsureCapacity( sizeof( T ) );
  41. *( reinterpret_cast<T *>( m_pDataOut ) ) = nValue;
  42. m_pDataOut += sizeof( nValue );
  43. #ifdef DBGFLAG_ASSERT
  44. m_nNumBytesRemaining -= sizeof( nValue );
  45. #endif
  46. }
  47. FORCEINLINE void PutInt( int nValue )
  48. {
  49. Put( nValue );
  50. }
  51. FORCEINLINE void PutFloat( float nValue )
  52. {
  53. Put( nValue );
  54. }
  55. FORCEINLINE void PutPtr( void * pPtr )
  56. {
  57. Put( pPtr );
  58. }
  59. FORCEINLINE void PutMemory( const void *pMemory, size_t nBytes )
  60. {
  61. EnsureCapacity( nBytes );
  62. memcpy( m_pDataOut, pMemory, nBytes );
  63. m_pDataOut += nBytes;
  64. }
  65. FORCEINLINE uint8 *Base( void )
  66. {
  67. return m_Data;
  68. }
  69. FORCEINLINE void Reset( void )
  70. {
  71. m_pDataOut = m_Data;
  72. #ifdef DBGFLAG_ASSERT
  73. m_nNumBytesRemaining = N;
  74. #endif
  75. }
  76. FORCEINLINE size_t Size( void ) const
  77. {
  78. return m_pDataOut - m_Data;
  79. }
  80. };
  81. template<class S> class CCommandBufferBuilder
  82. {
  83. public:
  84. S m_Storage;
  85. FORCEINLINE void End( void )
  86. {
  87. m_Storage.PutInt( CBCMD_END );
  88. }
  89. FORCEINLINE IMaterialVar *Param( int nVar ) const
  90. {
  91. return CBaseShader::s_ppParams[nVar];
  92. }
  93. FORCEINLINE void SetPixelShaderConstants( int nFirstConstant, int nConstants )
  94. {
  95. m_Storage.PutInt( CBCMD_SET_PIXEL_SHADER_FLOAT_CONST );
  96. m_Storage.PutInt( nFirstConstant );
  97. m_Storage.PutInt( nConstants );
  98. }
  99. FORCEINLINE void OutputConstantData( float const *pSrcData )
  100. {
  101. m_Storage.PutFloat( pSrcData[0] );
  102. m_Storage.PutFloat( pSrcData[1] );
  103. m_Storage.PutFloat( pSrcData[2] );
  104. m_Storage.PutFloat( pSrcData[3] );
  105. }
  106. FORCEINLINE void OutputConstantData4( float flVal0, float flVal1, float flVal2, float flVal3 )
  107. {
  108. m_Storage.PutFloat( flVal0 );
  109. m_Storage.PutFloat( flVal1 );
  110. m_Storage.PutFloat( flVal2 );
  111. m_Storage.PutFloat( flVal3 );
  112. }
  113. FORCEINLINE void SetPixelShaderConstant( int nFirstConstant, float const *pSrcData, int nNumConstantsToSet )
  114. {
  115. SetPixelShaderConstants( nFirstConstant, nNumConstantsToSet );
  116. m_Storage.PutMemory( pSrcData, 4 * sizeof( float ) * nNumConstantsToSet );
  117. }
  118. FORCEINLINE void SetPixelShaderConstant( int nFirstConstant, int nVar )
  119. {
  120. SetPixelShaderConstant( nFirstConstant, Param( nVar )->GetVecValue() );
  121. }
  122. void SetPixelShaderConstantGammaToLinear( int pixelReg, int constantVar )
  123. {
  124. float val[4];
  125. Param(constantVar)->GetVecValue( val, 3 );
  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. val[3] = 1.0;
  130. SetPixelShaderConstant( pixelReg, val );
  131. }
  132. FORCEINLINE void SetPixelShaderConstant( int nFirstConstant, float const *pSrcData )
  133. {
  134. SetPixelShaderConstants( nFirstConstant, 1 );
  135. OutputConstantData( pSrcData );
  136. }
  137. FORCEINLINE void SetPixelShaderConstant4( int nFirstConstant, float flVal0, float flVal1, float flVal2, float flVal3 )
  138. {
  139. SetPixelShaderConstants( nFirstConstant, 1 );
  140. OutputConstantData4( flVal0, flVal1, flVal2, flVal3 );
  141. }
  142. FORCEINLINE void SetPixelShaderConstant_W( int pixelReg, int constantVar, float fWValue )
  143. {
  144. if ( constantVar != -1 )
  145. {
  146. float val[3];
  147. Param(constantVar)->GetVecValue( val, 3);
  148. SetPixelShaderConstant4( pixelReg, val[0], val[1], val[2], fWValue );
  149. }
  150. }
  151. FORCEINLINE void SetVertexShaderConstant( int nFirstConstant, float const *pSrcData )
  152. {
  153. m_Storage.PutInt( CBCMD_SET_VERTEX_SHADER_FLOAT_CONST );
  154. m_Storage.PutInt( nFirstConstant );
  155. m_Storage.PutInt( 1 );
  156. OutputConstantData( pSrcData );
  157. }
  158. FORCEINLINE void SetVertexShaderConstant( int nFirstConstant, float const *pSrcData, int nConsts )
  159. {
  160. m_Storage.PutInt( CBCMD_SET_VERTEX_SHADER_FLOAT_CONST );
  161. m_Storage.PutInt( nFirstConstant );
  162. m_Storage.PutInt( nConsts );
  163. m_Storage.PutMemory( pSrcData, 4 * nConsts * sizeof( float ) );
  164. }
  165. FORCEINLINE void SetVertexShaderConstant4( int nFirstConstant, float flVal0, float flVal1, float flVal2, float flVal3 )
  166. {
  167. m_Storage.PutInt( CBCMD_SET_VERTEX_SHADER_FLOAT_CONST );
  168. m_Storage.PutInt( nFirstConstant );
  169. m_Storage.PutInt( 1 );
  170. m_Storage.PutFloat( flVal0 );
  171. m_Storage.PutFloat( flVal1 );
  172. m_Storage.PutFloat( flVal2 );
  173. m_Storage.PutFloat( flVal3 );
  174. }
  175. void SetVertexShaderTextureTransform( int vertexReg, int transformVar )
  176. {
  177. Vector4D transformation[2];
  178. IMaterialVar* pTransformationVar = Param( transformVar );
  179. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  180. {
  181. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  182. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  183. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  184. }
  185. else
  186. {
  187. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  188. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  189. }
  190. SetVertexShaderConstant( vertexReg, transformation[0].Base(), 2 );
  191. }
  192. void SetVertexShaderTextureScaledTransform( int vertexReg, int transformVar, int scaleVar )
  193. {
  194. Vector4D transformation[2];
  195. IMaterialVar* pTransformationVar = Param( transformVar );
  196. if (pTransformationVar && (pTransformationVar->GetType() == MATERIAL_VAR_TYPE_MATRIX))
  197. {
  198. const VMatrix &mat = pTransformationVar->GetMatrixValue();
  199. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  200. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  201. }
  202. else
  203. {
  204. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  205. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  206. }
  207. Vector2D scale( 1, 1 );
  208. IMaterialVar* pScaleVar = Param( scaleVar );
  209. if (pScaleVar)
  210. {
  211. if (pScaleVar->GetType() == MATERIAL_VAR_TYPE_VECTOR)
  212. pScaleVar->GetVecValue( scale.Base(), 2 );
  213. else if (pScaleVar->IsDefined())
  214. scale[0] = scale[1] = pScaleVar->GetFloatValue();
  215. }
  216. // Apply the scaling
  217. transformation[0][0] *= scale[0];
  218. transformation[0][1] *= scale[1];
  219. transformation[1][0] *= scale[0];
  220. transformation[1][1] *= scale[1];
  221. transformation[0][3] *= scale[0];
  222. transformation[1][3] *= scale[1];
  223. SetVertexShaderConstant( vertexReg, transformation[0].Base(), 2 );
  224. }
  225. FORCEINLINE void SetEnvMapTintPixelShaderDynamicState( int pixelReg, int tintVar )
  226. {
  227. if( g_pConfig->bShowSpecular && my_mat_fullbright.GetInt() != 2 )
  228. {
  229. SetPixelShaderConstant( pixelReg, Param( tintVar)->GetVecValue() );
  230. }
  231. else
  232. {
  233. SetPixelShaderConstant4( pixelReg, 0.0, 0.0, 0.0, 0.0 );
  234. }
  235. }
  236. FORCEINLINE void SetEnvMapTintPixelShaderDynamicStateGammaToLinear( int pixelReg, int tintVar, float flAlphaValue = 1.0 )
  237. {
  238. if( ( tintVar != -1 ) && g_pConfig->bShowSpecular && my_mat_fullbright.GetInt() != 2 )
  239. {
  240. float color[4];
  241. color[3] = flAlphaValue;
  242. Param( tintVar)->GetLinearVecValue( color, 3 );
  243. SetPixelShaderConstant( pixelReg, color );
  244. }
  245. else
  246. {
  247. SetPixelShaderConstant4( pixelReg, 0.0, 0.0, 0.0, flAlphaValue );
  248. }
  249. }
  250. FORCEINLINE void StoreEyePosInPixelShaderConstant( int nConst )
  251. {
  252. m_Storage.PutInt( CBCMD_STORE_EYE_POS_IN_PSCONST );
  253. m_Storage.PutInt( nConst );
  254. }
  255. FORCEINLINE void CommitPixelShaderLighting( int nConst )
  256. {
  257. m_Storage.PutInt( CBCMD_COMMITPIXELSHADERLIGHTING );
  258. m_Storage.PutInt( nConst );
  259. }
  260. FORCEINLINE void SetPixelShaderStateAmbientLightCube( int nConst )
  261. {
  262. m_Storage.PutInt( CBCMD_SETPIXELSHADERSTATEAMBIENTLIGHTCUBE );
  263. m_Storage.PutInt( nConst );
  264. }
  265. FORCEINLINE void SetAmbientCubeDynamicStateVertexShader( void )
  266. {
  267. m_Storage.PutInt( CBCMD_SETAMBIENTCUBEDYNAMICSTATEVERTEXSHADER );
  268. }
  269. FORCEINLINE void SetPixelShaderFogParams( int nReg )
  270. {
  271. m_Storage.PutInt( CBCMD_SETPIXELSHADERFOGPARAMS );
  272. m_Storage.PutInt( nReg );
  273. }
  274. FORCEINLINE void BindStandardTexture( Sampler_t nSampler, StandardTextureId_t nTextureId )
  275. {
  276. m_Storage.PutInt( CBCMD_BIND_STANDARD_TEXTURE );
  277. m_Storage.PutInt( nSampler );
  278. m_Storage.PutInt( nTextureId );
  279. }
  280. FORCEINLINE void BindTexture( Sampler_t nSampler, ShaderAPITextureHandle_t hTexture )
  281. {
  282. Assert( hTexture != INVALID_SHADERAPI_TEXTURE_HANDLE );
  283. if ( hTexture != INVALID_SHADERAPI_TEXTURE_HANDLE )
  284. {
  285. m_Storage.PutInt( CBCMD_BIND_SHADERAPI_TEXTURE_HANDLE );
  286. m_Storage.PutInt( nSampler );
  287. m_Storage.PutInt( hTexture );
  288. }
  289. }
  290. FORCEINLINE void BindTexture( CBaseVSShader *pShader, Sampler_t nSampler, int nTextureVar, int nFrameVar )
  291. {
  292. ShaderAPITextureHandle_t hTexture = pShader->GetShaderAPITextureBindHandle( nTextureVar, nFrameVar );
  293. BindTexture( nSampler, hTexture );
  294. }
  295. FORCEINLINE void BindMultiTexture( CBaseVSShader *pShader, Sampler_t nSampler1, Sampler_t nSampler2, int nTextureVar, int nFrameVar )
  296. {
  297. ShaderAPITextureHandle_t hTexture = pShader->GetShaderAPITextureBindHandle( nTextureVar, nFrameVar, 0 );
  298. BindTexture( nSampler1, hTexture );
  299. hTexture = pShader->GetShaderAPITextureBindHandle( nTextureVar, nFrameVar, 1 );
  300. BindTexture( nSampler2, hTexture );
  301. }
  302. FORCEINLINE void SetPixelShaderIndex( int nIndex )
  303. {
  304. m_Storage.PutInt( CBCMD_SET_PSHINDEX );
  305. m_Storage.PutInt( nIndex );
  306. }
  307. FORCEINLINE void SetVertexShaderIndex( int nIndex )
  308. {
  309. m_Storage.PutInt( CBCMD_SET_VSHINDEX );
  310. m_Storage.PutInt( nIndex );
  311. }
  312. FORCEINLINE void SetDepthFeatheringPixelShaderConstant( int iConstant, float fDepthBlendScale )
  313. {
  314. m_Storage.PutInt( CBCMD_SET_DEPTH_FEATHERING_CONST );
  315. m_Storage.PutInt( iConstant );
  316. m_Storage.PutFloat( fDepthBlendScale );
  317. }
  318. FORCEINLINE void Goto( uint8 *pCmdBuf )
  319. {
  320. m_Storage.PutInt( CBCMD_JUMP );
  321. m_Storage.PutPtr( pCmdBuf );
  322. }
  323. FORCEINLINE void Call( uint8 *pCmdBuf )
  324. {
  325. m_Storage.PutInt( CBCMD_JSR );
  326. m_Storage.PutPtr( pCmdBuf );
  327. }
  328. FORCEINLINE void Reset( void )
  329. {
  330. m_Storage.Reset();
  331. }
  332. FORCEINLINE size_t Size( void ) const
  333. {
  334. return m_Storage.Size();
  335. }
  336. FORCEINLINE uint8 *Base( void )
  337. {
  338. return m_Storage.Base();
  339. }
  340. };
  341. #endif // commandbuilder_h