/src/materialsystem/stdshaders/commandbuilder.h

https://github.com/SwissCheeseKnife/Protocol-7 · C Header · 402 lines · 327 code · 67 blank · 8 comment · 19 complexity · aab375ee37542e3cb2b84dd615da1c86 MD5 · raw file

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