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.

851 lines
37 KiB

  1. //========== Copyright (c) 2005, Valve Corporation, All rights reserved. ========
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef CMATERIALRENDERSTATE_H
  7. #define CMATERIALRENDERSTATE_H
  8. #if defined( _WIN32 )
  9. #pragma once
  10. #endif
  11. #include "tier1/delegates.h"
  12. #include "tier1/utlstack.h"
  13. #include "bitvec.h"
  14. #include "materialsystem_global.h"
  15. #include "materialsystem/imaterialsystem.h"
  16. #include "materialsystem/ishaderapi.h"
  17. #include "imaterialinternal.h"
  18. #include "shadersystem.h"
  19. #include "imorphinternal.h"
  20. #include "isubdinternal.h"
  21. #include "imatrendercontextinternal.h"
  22. #include "occlusionquerymgr.h"
  23. #include "materialsystem/MaterialSystemUtil.h"
  24. #include "tier1/memstack.h"
  25. #ifndef MATSYS_INTERNAL
  26. #error "This file is private to the implementation of IMaterialSystem/IMaterialSystemInternal"
  27. #endif
  28. //-----------------------------------------------------------------------------
  29. // Forward declarations
  30. //-----------------------------------------------------------------------------
  31. class ITextureInternal;
  32. class CMaterialSystem;
  33. class CMatLightmaps;
  34. class CMatPaintmaps;
  35. typedef intp ShaderAPITextureHandle_t;
  36. class IMorphMgrRenderContext;
  37. class CMatCallQueue;
  38. //-----------------------------------------------------------------------------
  39. // Render targets
  40. //-----------------------------------------------------------------------------
  41. #if !defined( _X360 ) && !defined( _PS3 )
  42. #define MAX_RENDER_TARGETS 4
  43. #else
  44. #define MAX_RENDER_TARGETS 1
  45. #endif
  46. //-----------------------------------------------------------------------------
  47. //
  48. //-----------------------------------------------------------------------------
  49. class CMatRenderContextBase : public CRefCounted1<IMatRenderContextInternal>
  50. {
  51. public:
  52. virtual void InitializeFrom( CMatRenderContextBase *pInitialState );
  53. InitReturnVal_t Init();
  54. void Shutdown();
  55. void CompactMemory();
  56. void SetFrameTime( float frameTime ) { m_FrameTime = frameTime; }
  57. ICallQueue * GetCallQueue() { return NULL; }
  58. CMatCallQueue * GetCallQueueInternal() { return NULL; }
  59. ITexture * GetRenderTarget( void );
  60. ITexture * GetRenderTargetEx( int nRenderTargetID );
  61. IMaterialInternal* GetCurrentMaterialInternal() const { return m_pCurrentMaterial; }
  62. virtual void SetCurrentMaterialInternal(IMaterialInternal* pCurrentMaterial) {
  63. m_pCurrentMaterial = pCurrentMaterial;
  64. Assert( (m_pCurrentMaterial == NULL) || ((IMaterialInternal *)m_pCurrentMaterial)->IsRealTimeVersion() );
  65. }
  66. IMaterial * GetCurrentMaterial() { return GetCurrentMaterialInternal(); }
  67. virtual void * GetCurrentProxy() { return m_pCurrentProxyData; }
  68. virtual void SetCurrentProxy( void *pProxyData ) { m_pCurrentProxyData = pProxyData; }
  69. void Bind( IMaterial *material, void *proxyData = NULL );
  70. void BindLightmapPage( int lightmapPageID );
  71. void BindLocalCubemap( ITexture *pTexture );
  72. // matrix api
  73. void MatrixMode( MaterialMatrixMode_t);
  74. void PushMatrix();
  75. void PopMatrix();
  76. void LoadMatrix( const VMatrix& matrix );
  77. void LoadMatrix( const matrix3x4_t& matrix );
  78. void MultMatrix( const VMatrix& matrix );
  79. void MultMatrixLocal( const VMatrix& matrix );
  80. void MultMatrix( const matrix3x4_t& matrix );
  81. void MultMatrixLocal( const matrix3x4_t& matrix );
  82. void GetMatrix( MaterialMatrixMode_t matrixMode, VMatrix *pMatrix );
  83. void GetMatrix( MaterialMatrixMode_t matrixMode, matrix3x4_t *pMatrix );
  84. void LoadIdentity();
  85. void Ortho( double, double, double, double, double, double);
  86. void PerspectiveX( double, double, double, double);
  87. void PerspectiveOffCenterX( double, double, double, double, double, double, double, double );
  88. void PickMatrix( int, int, int, int);
  89. void Rotate( float, float, float, float);
  90. void Translate( float, float, float);
  91. void Scale( float, float, float);
  92. // end matrix api
  93. // Set the current texture that is a copy of the framebuffer.
  94. void SetFrameBufferCopyTexture( ITexture *pTexture, int textureIndex );
  95. ITexture * GetFrameBufferCopyTexture( int textureIndex );
  96. void SetHeightClipMode( MaterialHeightClipMode_t mode );
  97. MaterialHeightClipMode_t GetHeightClipMode( void );
  98. void SetHeightClipZ( float z );
  99. bool EnableClipping( bool bEnable );
  100. void SetRenderTarget( ITexture *pTexture );
  101. void SetRenderTargetEx( int nRenderTargetID, ITexture *pTexture );
  102. void Viewport( int x, int y, int width, int height );
  103. void PushRenderTargetAndViewport( );
  104. void PushRenderTargetAndViewport( ITexture *pTexture );
  105. void PushRenderTargetAndViewport( ITexture *pTexture, int nViewX, int nViewY, int nViewW, int nViewH );
  106. void PushRenderTargetAndViewport( ITexture *pTexture, ITexture *pDepthTexture, int nViewX, int nViewY, int nViewW, int nViewH );
  107. void PopRenderTargetAndViewport( void );
  108. void SyncMatrices();
  109. void SyncMatrix( MaterialMatrixMode_t );
  110. const VMatrix & AccessCurrentMatrix() const { return m_pCurMatrixItem->matrix; }
  111. ShaderAPITextureHandle_t GetLightmapTexture( int nLightmapPage );
  112. ShaderAPITextureHandle_t GetPaintmapTexture( int nLightmapPage );
  113. virtual void UpdateHeightClipUserClipPlane( void ) {}
  114. virtual void ApplyCustomClipPlanes( void ) {}
  115. float ComputePixelDiameterOfSphere( const Vector& origin, float flRadius );
  116. float ComputePixelWidthOfSphere( const Vector& origin, float flRadius ); // FIXME: REMOVE THIS FUNCTION!
  117. virtual void GetWorldSpaceCameraPosition( Vector *pCameraPos );
  118. virtual void GetWorldSpaceCameraVectors( Vector *pVecForward, Vector *pVecRight, Vector *pVecUp );
  119. Vector GetToneMappingScaleLinear();
  120. // Inherited from IMaterialSystemInternal
  121. int GetLightmapPage( void );
  122. virtual void * LockRenderData( int nSizeInBytes );
  123. virtual void UnlockRenderData( void *pData );
  124. virtual void AddRefRenderData();
  125. virtual void ReleaseRenderData();
  126. virtual bool IsRenderData( const void *pData ) const;
  127. void MarkRenderDataUnused( bool bFrameEnd );
  128. int RenderDataSizeUsed() const;
  129. // debugging
  130. virtual void PrintfVA( char *fmt, va_list vargs );
  131. virtual void Printf( char *fmt, ... );
  132. virtual float Knob( char *knobname, float *setvalue = NULL );
  133. protected:
  134. enum MatrixStackFlags_t
  135. {
  136. MSF_DIRTY = ( 1 << 0 ),
  137. MSF_IDENTITY = ( 1 << 1 ),
  138. };
  139. struct MatrixStackItem_t
  140. {
  141. VMatrix matrix;
  142. unsigned flags;
  143. };
  144. struct RenderTargetStackElement_t
  145. {
  146. // The render target
  147. ITexture *m_pRenderTargets[MAX_RENDER_TARGETS];
  148. // Optional depth texture (used for shadow mapping)
  149. ITexture *m_pDepthTexture;
  150. // Viewport dimensions
  151. int m_nViewX;
  152. int m_nViewY;
  153. int m_nViewW;
  154. int m_nViewH;
  155. };
  156. struct ScissorRectStackElement_t
  157. {
  158. int nLeft;
  159. int nTop;
  160. int nRight;
  161. int nBottom;
  162. };
  163. struct PlaneStackElement
  164. {
  165. float fValues[4];
  166. bool bHack_IsHeightClipPlane; //used to hack in compatibility between the user clip planes and the existing height clip plane code
  167. //I'm doing the hack this way to retain modder's flexibility to mess with clip plane ordering so that they can make special hacks on their side if necessary
  168. PlaneStackElement( void ) : bHack_IsHeightClipPlane( false ) { };
  169. };
  170. protected:
  171. CMatRenderContextBase( );
  172. virtual void CommitRenderTargetAndViewport( void ) {}
  173. void RecomputeViewState();
  174. void RecomputeViewProjState();
  175. void CurrentMatrixChanged();
  176. virtual void OnRenderDataUnreferenced() {}
  177. protected:
  178. IMaterialInternal * m_pCurrentMaterial;
  179. void* m_pCurrentProxyData;
  180. // The lightmap page
  181. int m_lightmapPageID;
  182. ITextureInternal * m_pUserDefinedLightmap;
  183. ITexture * m_pLocalCubemapTexture;
  184. ITexture * m_pCurrentFrameBufferCopyTexture[MAX_FB_TEXTURES];
  185. MaterialHeightClipMode_t m_HeightClipMode;
  186. float m_HeightClipZ;
  187. // The currently bound morph target
  188. IMorphInternal * m_pBoundMorph;
  189. IMorphMgrRenderContext *m_pMorphRenderContext;
  190. // Intially a stack of 32 elements allocated, growing by 16 on overflows
  191. CUtlStack< RenderTargetStackElement_t > m_RenderTargetStack;
  192. // Intially a stack of 32 elements allocated, growing by 16 on overflows
  193. CUtlStack< ScissorRectStackElement_t > m_ScissorRectStack;
  194. MaterialMatrixMode_t m_MatrixMode;
  195. MatrixStackItem_t * m_pCurMatrixItem;
  196. CUtlStack<MatrixStackItem_t> m_MatrixStacks[NUM_MATRIX_MODES];
  197. // View state
  198. VMatrix m_viewProjMatrix;
  199. Vector m_vecViewOrigin;
  200. Vector m_vecViewForward;
  201. Vector m_vecViewUp;
  202. Vector m_vecViewRight;
  203. float m_FrameTime;
  204. Vector m_LastSetToneMapScale;
  205. ShaderViewport_t m_Viewport;
  206. CMaterialSystem * m_pMaterialSystem;
  207. static CMemoryStack sm_RenderData[2];
  208. static int sm_nRenderLockCount;
  209. static int sm_nRenderStack;
  210. static MemoryStackMark_t sm_nRenderCurrentAllocPoint;
  211. static int sm_nInitializeCount;
  212. bool m_bFlashlightEnable : 1;
  213. bool m_bDirtyViewState : 1;
  214. bool m_bDirtyViewProjState : 1;
  215. bool m_bEnableClipping : 1;
  216. bool m_bFullFrameDepthIsValid : 1;
  217. bool m_bRenderingPaint : 1;
  218. bool m_bCullingEnabledForSinglePassFlashlight : 1;
  219. bool m_bSinglePassFlashlightMode : 1;
  220. bool m_bCascadedShadowMappingEnabled : 1;
  221. };
  222. //-----------------------------------------------------------------------------
  223. //
  224. //-----------------------------------------------------------------------------
  225. #if defined( _PS3 ) || defined( _OSX )
  226. #define g_pShaderAPI ShaderAPI()
  227. #endif
  228. class CMatRenderContext : public CMatRenderContextBase
  229. {
  230. typedef CMatRenderContextBase BaseClass;
  231. public:
  232. CMatRenderContext();
  233. InitReturnVal_t Init( CMaterialSystem *pMaterialSystem );
  234. void Shutdown();
  235. void BeginRender();
  236. void EndRender();
  237. void Flush( bool flushHardware = false );
  238. void SwapBuffers();
  239. void OnReleaseShaderObjects();
  240. DELEGATE_TO_OBJECT_0V( EvictManagedResources, g_pShaderAPI );
  241. // Set the current texture that is a copy of the framebuffer.
  242. void SetFrameBufferCopyTexture( ITexture *pTexture, int textureIndex );
  243. IMesh * CreateStaticMesh( VertexFormat_t vertexFormat, const char *pTextureBudgetGroup, IMaterial * pMaterial = NULL, VertexStreamSpec_t *pStreamSpec = NULL );
  244. DELEGATE_TO_OBJECT_1V( DestroyStaticMesh, IMesh *, g_pShaderDevice );
  245. IMesh * GetDynamicMesh( bool buffered, IMesh* pVertexOverride = 0, IMesh* pIndexOverride = 0, IMaterial *pAutoBind = 0 );
  246. virtual IMesh* GetDynamicMeshEx( VertexFormat_t vertexFormat, bool bBuffered = true, IMesh* pVertexOverride = 0, IMesh* pIndexOverride = 0, IMaterial *pAutoBind = 0 );
  247. DELEGATE_TO_OBJECT_0( IMesh *, GetFlexMesh, g_pShaderAPI );
  248. // ------------ New Vertex/Index Buffer interface ----------------------------
  249. IVertexBuffer *CreateStaticVertexBuffer( VertexFormat_t fmt, int nVertexCount, const char *pBudgetGroup )
  250. {
  251. return g_pShaderDevice->CreateVertexBuffer( SHADER_BUFFER_TYPE_STATIC, fmt, nVertexCount, pBudgetGroup );
  252. }
  253. IIndexBuffer *CreateStaticIndexBuffer( MaterialIndexFormat_t fmt, int nIndexCount, const char *pBudgetGroup )
  254. {
  255. return g_pShaderDevice->CreateIndexBuffer( SHADER_BUFFER_TYPE_STATIC, fmt, nIndexCount, pBudgetGroup );
  256. }
  257. DELEGATE_TO_OBJECT_1V( DestroyVertexBuffer, IVertexBuffer *, g_pShaderDevice );
  258. DELEGATE_TO_OBJECT_1V( DestroyIndexBuffer, IIndexBuffer *, g_pShaderDevice );
  259. DELEGATE_TO_OBJECT_3( IVertexBuffer *, GetDynamicVertexBuffer, int, VertexFormat_t, bool, g_pShaderDevice );
  260. DELEGATE_TO_OBJECT_0( IIndexBuffer *, GetDynamicIndexBuffer, g_pShaderDevice );
  261. DELEGATE_TO_OBJECT_7V( BindVertexBuffer, int, IVertexBuffer *, int, int, int, VertexFormat_t, int, g_pShaderAPI );
  262. DELEGATE_TO_OBJECT_2V( BindIndexBuffer, IIndexBuffer *, int, g_pShaderAPI );
  263. DELEGATE_TO_OBJECT_3V( Draw, MaterialPrimitiveType_t, int, int, g_pShaderAPI );
  264. // ------------ End ----------------------------
  265. void BindLocalCubemap( ITexture *pTexture );
  266. ITexture * GetLocalCubemap( void );
  267. void SetRenderTargetEx( int nRenderTargetID, ITexture *pTexture );
  268. void GetRenderTargetDimensions( int &width, int &height) const;
  269. // matrix api
  270. void MatrixMode( MaterialMatrixMode_t);
  271. void PushMatrix();
  272. void PopMatrix();
  273. void LoadMatrix( const VMatrix& matrix );
  274. void LoadMatrix( const matrix3x4_t& matrix );
  275. void MultMatrix( const VMatrix& matrix );
  276. void MultMatrixLocal( const VMatrix& matrix );
  277. void MultMatrix( const matrix3x4_t& matrix );
  278. void MultMatrixLocal( const matrix3x4_t& matrix );
  279. void GetMatrix( MaterialMatrixMode_t matrixMode, VMatrix *pMatrix );
  280. void GetMatrix( MaterialMatrixMode_t matrixMode, matrix3x4_t *pMatrix );
  281. void LoadIdentity();
  282. void Ortho( double, double, double, double, double, double);
  283. void PerspectiveX( double, double, double, double);
  284. void PerspectiveOffCenterX( double, double, double, double, double, double, double, double );
  285. void PickMatrix( int, int, int, int);
  286. void Rotate( float, float, float, float);
  287. void Translate( float, float, float);
  288. void Scale( float, float, float);
  289. // end matrix api
  290. void SyncMatrices();
  291. void SyncMatrix( MaterialMatrixMode_t );
  292. bool TestMatrixSync( MaterialMatrixMode_t );
  293. void ForceSyncMatrix( MaterialMatrixMode_t );
  294. ShaderAPITextureHandle_t GetLightmapTexture( int nLightmapPage );
  295. ShaderAPITextureHandle_t GetPaintmapTexture( int nLightmapPage );
  296. // Allows us to override the depth buffer setting of a material
  297. DELEGATE_TO_OBJECT_3V( OverrideDepthEnable, bool, bool, bool, g_pShaderAPI );
  298. DELEGATE_TO_OBJECT_2V( OverrideAlphaWriteEnable, bool, bool, g_pShaderAPI );
  299. DELEGATE_TO_OBJECT_2V( OverrideColorWriteEnable, bool, bool, g_pShaderAPI );
  300. // FIXME: This is a hack required for NVidia/XBox, can they fix in drivers?
  301. void DrawScreenSpaceQuad( IMaterial* pMaterial );
  302. int CompareMaterialCombos( IMaterial *pMaterial1, IMaterial *pMaterial2, int lightMapID1, int lightMapID2 );
  303. void Bind( IMaterial *material, void *proxyData = NULL );
  304. void BindLightmapPage( int lightmapPageID );
  305. void BindPaintTexture( ITexture *pTexture );
  306. DELEGATE_TO_OBJECT_2V( SetLights, int, const LightDesc_t *, g_pShaderAPI );
  307. DELEGATE_TO_OBJECT_1V( SetLightingOrigin, Vector, g_pShaderAPI );
  308. DELEGATE_TO_OBJECT_1V( SetAmbientLightCube, LightCube_t, g_pShaderAPI );
  309. DELEGATE_TO_OBJECT_0V( DisableAllLocalLights, g_pShaderAPI );
  310. DELEGATE_TO_OBJECT_1V( SetLightingState, const MaterialLightingState_t&, g_pShaderAPI );
  311. void CopyRenderTargetToTexture( ITexture *pTexture );
  312. void ClearBuffers( bool bClearColor, bool bClearDepth, bool bClearStencil );
  313. DELEGATE_TO_OBJECT_3V( ClearColor3ub, unsigned char, unsigned char, unsigned char, g_pShaderAPI );
  314. DELEGATE_TO_OBJECT_4V( ClearColor4ub, unsigned char, unsigned char, unsigned char, unsigned char, g_pShaderAPI );
  315. DELEGATE_TO_OBJECT_2V( ClearBuffersObeyStencil, bool, bool, g_pShaderAPI );
  316. DELEGATE_TO_OBJECT_3V( ClearBuffersObeyStencilEx, bool, bool, bool, g_pShaderAPI );
  317. DELEGATE_TO_OBJECT_0V( PerformFullScreenStencilOperation, g_pShaderAPI );
  318. // read to a unsigned char rgb image.
  319. DELEGATE_TO_OBJECT_6V( ReadPixels, int, int, int, int, unsigned char *, ImageFormat, g_pShaderAPI );
  320. DELEGATE_TO_OBJECT_7V( ReadPixels, int, int, int, int, unsigned char *, ImageFormat, ITexture *, g_pShaderAPI );
  321. DELEGATE_TO_OBJECT_6V( ReadPixelsAsync, int, int, int, int, unsigned char *, ImageFormat, g_pShaderAPI );
  322. DELEGATE_TO_OBJECT_7V( ReadPixelsAsync, int, int, int, int, unsigned char *, ImageFormat, ITexture *, g_pShaderAPI );
  323. DELEGATE_TO_OBJECT_8V( ReadPixelsAsync, int, int, int, int, unsigned char *, ImageFormat, ITexture *, CThreadEvent *, g_pShaderAPI );
  324. DELEGATE_TO_OBJECT_6V( ReadPixelsAsyncGetResult, int, int, int, int, unsigned char *, ImageFormat, g_pShaderAPI );
  325. DELEGATE_TO_OBJECT_7V( ReadPixelsAsyncGetResult, int, int, int, int, unsigned char *, ImageFormat, CThreadEvent *, g_pShaderAPI );
  326. void ReadPixelsAndStretch( Rect_t *pSrcRect, Rect_t *pDstRect, unsigned char *pBuffer, ImageFormat dstFormat, int nDstStride ) { g_pShaderAPI->ReadPixels( pSrcRect, pDstRect, pBuffer, dstFormat, nDstStride ); }
  327. // Gets/sets viewport
  328. void Viewport( int x, int y, int width, int height );
  329. void GetViewport( int& x, int& y, int& width, int& height ) const;
  330. virtual void DepthRange( float zNear, float zFar );
  331. // Selection mode methods
  332. DELEGATE_TO_OBJECT_1( int, SelectionMode, bool, g_pShaderAPI );
  333. DELEGATE_TO_OBJECT_2V( SelectionBuffer, unsigned int *, int, g_pShaderAPI );
  334. DELEGATE_TO_OBJECT_0V( ClearSelectionNames, g_pShaderAPI );
  335. DELEGATE_TO_OBJECT_1V( LoadSelectionName, int, g_pShaderAPI );
  336. DELEGATE_TO_OBJECT_1V( PushSelectionName, int, g_pShaderAPI );
  337. DELEGATE_TO_OBJECT_0V( PopSelectionName, g_pShaderAPI );
  338. // Sets the cull mode
  339. DELEGATE_TO_OBJECT_1V( CullMode, MaterialCullMode_t, g_pShaderAPI );
  340. DELEGATE_TO_OBJECT_0V( FlipCullMode, g_pShaderAPI );
  341. DELEGATE_TO_OBJECT_0V( BeginGeneratingCSMs, g_pShaderAPI );
  342. DELEGATE_TO_OBJECT_0V( EndGeneratingCSMs, g_pShaderAPI );
  343. DELEGATE_TO_OBJECT_3V( PerpareForCascadeDraw, int, float, float, g_pShaderAPI );
  344. // Sets the number of bones for skinning
  345. DELEGATE_TO_OBJECT_1V( SetNumBoneWeights, int, g_pShaderAPI );
  346. void LoadBoneMatrix( int boneIndex, const matrix3x4_t& matrix );
  347. DELEGATE_TO_OBJECT_3V( SetFlexWeights, int, int, const MorphWeight_t*, g_pShaderAPI );
  348. // Fog-related methods
  349. void FogMode( MaterialFogMode_t fogMode ) { g_pShaderAPI->SceneFogMode( fogMode ); }
  350. DELEGATE_TO_OBJECT_1V( FogStart, float, g_pShaderAPI );
  351. DELEGATE_TO_OBJECT_1V( FogEnd, float, g_pShaderAPI );
  352. DELEGATE_TO_OBJECT_1V( SetFogZ, float, g_pShaderAPI );
  353. void FogColor3f( float r, float g, float b );
  354. void FogColor3fv( const float* rgb );
  355. void FogColor3ub( unsigned char r, unsigned char g, unsigned char b ) { g_pShaderAPI->SceneFogColor3ub( r, g, b ); }
  356. void FogColor3ubv( unsigned char const* rgb );
  357. void FogMaxDensity( float flMaxDensity );
  358. MaterialFogMode_t GetFogMode( void ) { return g_pShaderAPI->GetSceneFogMode(); }
  359. void GetFogColor( unsigned char *rgb ) { g_pShaderAPI->GetSceneFogColor( rgb ); }
  360. DELEGATE_TO_OBJECT_3V( GetFogDistances, float *, float *, float *, g_pShaderAPI );
  361. int GetCurrentNumBones( ) const { return g_pShaderAPI->GetCurrentNumBones(); }
  362. // Bind standard textures
  363. void BindStandardTexture( Sampler_t sampler, TextureBindFlags_t nBindFlags, StandardTextureId_t id );
  364. ShaderAPITextureHandle_t GetStandardTexture( StandardTextureId_t id );
  365. virtual void BindStandardVertexTexture( VertexTextureSampler_t sampler, StandardTextureId_t id );
  366. virtual void GetStandardTextureDimensions( int *pWidth, int *pHeight, StandardTextureId_t id );
  367. virtual float GetSubDHeight();
  368. // By default, the material system applies the VIEW and PROJECTION matrices to the user clip
  369. // planes (which are specified in world space) to generate projection-space user clip planes
  370. // Occasionally (for the particle system in hl2, for example), we want to override that
  371. // behavior and explictly specify a ViewProj transform for user clip planes
  372. DELEGATE_TO_OBJECT_1V( EnableUserClipTransformOverride, bool, g_pShaderAPI );
  373. DELEGATE_TO_OBJECT_1V( UserClipTransform, const VMatrix &, g_pShaderAPI );
  374. // Gets the window size
  375. DELEGATE_TO_OBJECT_2VC( GetWindowSize, int &, int &, g_pShaderDevice );
  376. // Methods related to occlusion query
  377. OcclusionQueryObjectHandle_t CreateOcclusionQueryObject();
  378. DELEGATE_TO_OBJECT_1V( DestroyOcclusionQueryObject, OcclusionQueryObjectHandle_t, g_pOcclusionQueryMgr );
  379. DELEGATE_TO_OBJECT_1V( BeginOcclusionQueryDrawing, OcclusionQueryObjectHandle_t, g_pOcclusionQueryMgr );
  380. DELEGATE_TO_OBJECT_1V( EndOcclusionQueryDrawing, OcclusionQueryObjectHandle_t, g_pOcclusionQueryMgr );
  381. DELEGATE_TO_OBJECT_1V( ResetOcclusionQueryObject, OcclusionQueryObjectHandle_t, g_pOcclusionQueryMgr );
  382. int OcclusionQuery_GetNumPixelsRendered( OcclusionQueryObjectHandle_t h );
  383. bool InFlashlightMode() const;
  384. bool IsRenderingPaint() const;
  385. virtual void SetFlashlightMode( bool bEnable );
  386. virtual void SetRenderingPaint( bool bEnable );
  387. bool GetFlashlightMode( ) const;
  388. virtual bool IsCascadedShadowMapping() const;
  389. virtual void SetCascadedShadowMapping( bool bEnable );
  390. virtual void SetCascadedShadowMappingState( const CascadedShadowMappingState_t &state, ITexture *pDepthTextureAtlas );
  391. virtual bool IsCullingEnabledForSinglePassFlashlight() const;
  392. virtual void EnableCullingForSinglePassFlashlight( bool bEnable );
  393. void SetFlashlightState( const FlashlightState_t &state, const VMatrix &worldToTexture );
  394. void SetFlashlightStateEx( const FlashlightState_t &state, const VMatrix &worldToTexture, ITexture *pFlashlightDepthTexture );
  395. void PushScissorRect( const int nLeft, const int nTop, const int nRight, const int nBottom );
  396. void PopScissorRect();
  397. #if defined( _GAMECONSOLE )
  398. void BeginConsoleZPass( const WorldListIndicesInfo_t &indicesInfo ){ BeginConsoleZPass2( indicesInfo.m_nTotalIndices ); }
  399. #endif
  400. // Creates/destroys morph data associated w/ a particular material
  401. IMorph * CreateMorph( MorphFormat_t format, const char *pDebugName );
  402. void DestroyMorph( IMorph *pMorph );
  403. void BindMorph( IMorph *pMorph );
  404. // Gets the bound morph's vertex format; returns 0 if no morph is bound
  405. MorphFormat_t GetBoundMorphFormat();
  406. void DrawScreenSpaceRectangle( IMaterial *pMaterial,
  407. int destx, int desty,
  408. int width, int height,
  409. float src_texture_x0, float src_texture_y0, // which texel you want to appear at
  410. // destx/y
  411. float src_texture_x1, float src_texture_y1, // which texel you want to appear at
  412. // destx+width-1, desty+height-1
  413. int src_texture_width, int src_texture_height, // needed for fixup
  414. void *pClientRenderable = NULL,
  415. int nXDice = 1,
  416. int nYDice = 1 );
  417. // custom clip planes, beware that only the most recently pushed plane will actually be used in some hardware configurations
  418. void PushCustomClipPlane( const float *fPlane );
  419. void PopCustomClipPlane( void );
  420. void ApplyCustomClipPlanes( void ); //updates the clip planes based on how many are supported by the hardware using the top of the stack first, at the end of the stack, the height clip plane will be evaluated
  421. // Force writes only when z matches. . . useful for stenciling things out
  422. // by rendering the desired Z values ahead of time.
  423. DELEGATE_TO_OBJECT_1V( ForceDepthFuncEquals, bool, g_pShaderAPI );
  424. // Two methods of building lightmap bits from floating point src data
  425. // What are the lightmap dimensions?
  426. void GetLightmapDimensions( int *w, int *h );
  427. void DrawClearBufferQuad( unsigned char r, unsigned char g, unsigned char b, unsigned char a, bool bClearColor, bool bClearAlpha, bool bClearDepth );
  428. #ifdef _PS3
  429. void DrawReloadZcullQuad();
  430. #endif // _PS3
  431. void UpdateHeightClipUserClipPlane( void );
  432. // Private routine called by render target stack push and pop routines to commit new top of stack to the device
  433. void CommitRenderTargetAndViewport( void );
  434. void SyncToken( const char *pToken );
  435. void BindLightmapTexture( ITexture *pLightmapTexture );
  436. void CopyRenderTargetToTextureEx( ITexture *pTexture, int nRenderTargetID, Rect_t *pSrcRect, Rect_t *pDstRect = NULL );
  437. void CopyTextureToRenderTargetEx( int nRenderTargetID, ITexture *pTexture, Rect_t *pSrcRect, Rect_t *pDstRect = NULL );
  438. DELEGATE_TO_OBJECT_2V( SetFloatRenderingParameter, int, float, g_pShaderAPI );
  439. DELEGATE_TO_OBJECT_2V( SetIntRenderingParameter, int, int, g_pShaderAPI );
  440. DELEGATE_TO_OBJECT_2V( SetTextureRenderingParameter, int, ITexture *, g_pShaderAPI );
  441. DELEGATE_TO_OBJECT_2V( SetVectorRenderingParameter, int, const Vector &, g_pShaderAPI );
  442. DELEGATE_TO_OBJECT_1C( float, GetFloatRenderingParameter, int, g_pShaderAPI );
  443. DELEGATE_TO_OBJECT_1C( int, GetIntRenderingParameter, int, g_pShaderAPI );
  444. DELEGATE_TO_OBJECT_1C( ITexture *, GetTextureRenderingParameter, int, g_pShaderAPI );
  445. DELEGATE_TO_OBJECT_1C( Vector, GetVectorRenderingParameter, int, g_pShaderAPI );
  446. DELEGATE_TO_OBJECT_4V( GetMaxToRender, IMesh *, bool, int *, int *, g_pShaderAPI );
  447. DELEGATE_TO_OBJECT_1( int, GetMaxVerticesToRender, IMaterial *, g_pShaderAPI );
  448. DELEGATE_TO_OBJECT_0( int, GetMaxIndicesToRender, g_pShaderAPI );
  449. DELEGATE_TO_OBJECT_1V( SetStencilState, const ShaderStencilState_t &, g_pShaderAPI );
  450. DELEGATE_TO_OBJECT_5V( ClearStencilBufferRectangle, int, int, int, int, int, g_pShaderAPI );
  451. // PIX support
  452. DELEGATE_TO_OBJECT_2V( BeginPIXEvent, unsigned long, const char *, g_pShaderAPI );
  453. DELEGATE_TO_OBJECT_0V( EndPIXEvent, g_pShaderAPI );
  454. DELEGATE_TO_OBJECT_2V( SetPIXMarker, unsigned long, const char *, g_pShaderAPI );
  455. DELEGATE_TO_OBJECT_2V( SetShadowDepthBiasFactors, float, float, g_pShaderAPI );
  456. void BeginBatch( IMesh* pIndices );
  457. void BindBatch( IMesh* pVertices, IMaterial *pAutoBind = NULL );
  458. void DrawBatch( MaterialPrimitiveType_t primType, int firstIndex, int numIndices );
  459. void EndBatch();
  460. void SetToneMappingScaleLinear( const Vector &scale );
  461. bool OnDrawMesh( IMesh *pMesh, int firstIndex, int numIndices );
  462. bool OnDrawMesh( IMesh *pMesh, CPrimList *pLists, int nLists );
  463. bool OnDrawMeshModulated( IMesh *pMesh, const Vector4D &diffuseModulation, int firstIndex, int numIndices );
  464. bool OnSetFlexMesh( IMesh *pStaticMesh, IMesh *pMesh, int nVertexOffsetInBytes ) { return true; }
  465. bool OnSetColorMesh( IMesh *pStaticMesh, IMesh *pMesh, int nVertexOffsetInBytes ) { return true; }
  466. bool OnSetPrimitiveType( IMesh *pMesh, MaterialPrimitiveType_t type ) { return true; }
  467. CMaterialSystem *GetMaterialSystem() const;
  468. DELEGATE_TO_OBJECT_0V( ForceHardwareSync, g_pShaderAPI );
  469. DELEGATE_TO_OBJECT_0V( BeginFrame, g_pShaderAPI );
  470. DELEGATE_TO_OBJECT_0V( EndFrame, g_pShaderAPI );
  471. virtual void BeginMorphAccumulation();
  472. virtual void EndMorphAccumulation();
  473. virtual void AccumulateMorph( IMorph* pMorph, int nMorphCount, const MorphWeight_t* pWeights );
  474. virtual bool GetMorphAccumulatorTexCoord( Vector2D *pTexCoord, IMorph *pMorph, int nVertex );
  475. // Subdivision surface interface
  476. virtual int GetSubDBufferWidth();
  477. virtual float *LockSubDBuffer( int nNumRows );
  478. virtual void UnlockSubDBuffer();
  479. DELEGATE_TO_OBJECT_1V( PushDeformation, const DeformationBase_t *, g_pShaderAPI );
  480. DELEGATE_TO_OBJECT_0V( PopDeformation, g_pShaderAPI );
  481. DELEGATE_TO_OBJECT_0C( int, GetNumActiveDeformations, g_pShaderAPI );
  482. // Color correction related methods..
  483. DELEGATE_TO_OBJECT_1V( EnableColorCorrection, bool, g_pColorCorrectionSystem );
  484. DELEGATE_TO_OBJECT_1( ColorCorrectionHandle_t, AddLookup, const char *, g_pColorCorrectionSystem );
  485. DELEGATE_TO_OBJECT_1( bool, RemoveLookup, ColorCorrectionHandle_t, g_pColorCorrectionSystem );
  486. DELEGATE_TO_OBJECT_1V( LockLookup, ColorCorrectionHandle_t, g_pColorCorrectionSystem );
  487. DELEGATE_TO_OBJECT_2V( LoadLookup, ColorCorrectionHandle_t, const char *, g_pColorCorrectionSystem );
  488. DELEGATE_TO_OBJECT_1V( UnlockLookup, ColorCorrectionHandle_t, g_pColorCorrectionSystem );
  489. DELEGATE_TO_OBJECT_2V( SetLookupWeight, ColorCorrectionHandle_t, float, g_pColorCorrectionSystem );
  490. DELEGATE_TO_OBJECT_0V( ResetLookupWeights, g_pColorCorrectionSystem );
  491. DELEGATE_TO_OBJECT_2V( SetResetable, ColorCorrectionHandle_t, bool, g_pColorCorrectionSystem );
  492. virtual void SetFullScreenDepthTextureValidityFlag( bool bIsValid );
  493. #if defined( _X360 )
  494. DELEGATE_TO_OBJECT_1V( PushVertexShaderGPRAllocation, int, g_pShaderAPI );
  495. DELEGATE_TO_OBJECT_0V( PopVertexShaderGPRAllocation, g_pShaderAPI );
  496. DELEGATE_TO_OBJECT_0V( FlushHiStencil, g_pShaderAPI );
  497. #endif
  498. #if defined( _GAMECONSOLE )
  499. DELEGATE_TO_OBJECT_1V( BeginConsoleZPass2, int, g_pShaderAPI );
  500. DELEGATE_TO_OBJECT_0V( EndConsoleZPass, g_pShaderAPI );
  501. #endif
  502. #if defined( _PS3 )
  503. DELEGATE_TO_OBJECT_0V( FlushTextureCache, g_pShaderAPI );
  504. #endif
  505. DELEGATE_TO_OBJECT_1V( AntiAliasingHint, int, g_pShaderAPI );
  506. // A special path used to tick the front buffer while loading on the 360
  507. virtual void SetNonInteractiveLogoTexture( ITexture *pTexture, float flNormalizedX, float flNormalizedY, float flNormalizedW, float flNormalizedH );
  508. virtual void SetNonInteractivePacifierTexture( ITexture *pTexture, float flNormalizedX, float flNormalizedY, float flNormalizedSize );
  509. virtual void SetNonInteractiveTempFullscreenBuffer( ITexture *pTexture, MaterialNonInteractiveMode_t mode );
  510. virtual void EnableNonInteractiveMode( MaterialNonInteractiveMode_t mode );
  511. virtual void RefreshFrontBufferNonInteractive();
  512. DELEGATE_TO_OBJECT_1V( FlipCulling, bool, g_pShaderAPI );
  513. // DELEGATE_TO_OBJECT_1V( EnableSinglePassFlashlightMode, bool, g_pShaderAPI );
  514. virtual void EnableSinglePassFlashlightMode( bool bEnable );
  515. virtual bool SinglePassFlashlightModeEnabled() const;
  516. DELEGATE_TO_OBJECT_2V( DrawInstances, int, const MeshInstanceData_t *, g_pShaderAPI );
  517. DELEGATE_TO_OBJECT_1V( UpdateGameTime, float, g_pShaderAPI );
  518. //--------------------------------------------------------
  519. // debug logging - no-op in queued context
  520. //--------------------------------------------------------
  521. virtual void Printf( char *fmt, ... );
  522. virtual void PrintfVA( char *fmt, va_list vargs );;
  523. virtual float Knob( char *knobname, float *setvalue=NULL );
  524. #if defined( DX_TO_GL_ABSTRACTION ) && !defined( _GAMECONSOLE )
  525. void DoStartupShaderPreloading( void );
  526. #endif
  527. //---------------------------------------------------------
  528. #if defined( INCLUDE_SCALEFORM )
  529. //--------------------------------------------------------
  530. // scaleform interaction
  531. //--------------------------------------------------------
  532. void SetScaleformSlotViewport( int slot, int x, int y, int w, int h ) { ScaleformUI()->SetSlotViewport( slot, x, y, w, h ); }
  533. void RenderScaleformSlot( int slot ) { ScaleformUI()->RenderSlot( slot ); }
  534. void ForkRenderScaleformSlot( int slot ) { ScaleformUI()->ForkRenderSlot( slot ); }
  535. void JoinRenderScaleformSlot( int slot ) { ScaleformUI()->JoinRenderSlot( slot ); }
  536. void SetScaleformCursorViewport( int x, int y, int w, int h ) { ScaleformUI()->SetCursorViewport( x, y, w, h ); }
  537. void RenderScaleformCursor( void ) { ScaleformUI()->RenderCursor(); }
  538. void AdvanceAndRenderScaleformSlot( int slot ) { ScaleformUI()->AdvanceSlot( slot ); ScaleformUI()->RenderSlot( slot ); }
  539. void AdvanceAndRenderScaleformCursor() { ScaleformUI()->AdvanceCursor(); ScaleformUI()->RenderCursor(); }
  540. #endif // INCLUDE_SCALEFORM
  541. DELEGATE_TO_OBJECT_1( ColorCorrectionHandle_t, FindLookup, const char *, g_pColorCorrectionSystem );
  542. //---------------------------------------------------------
  543. protected:
  544. IMaterialInternal *GetMaterialInternal( MaterialHandle_t ) const;
  545. IMaterialInternal *GetDrawFlatMaterial();
  546. IMaterialInternal *GetRenderTargetBlitMaterial();
  547. IMaterialInternal *GetBufferClearObeyStencil( int i );
  548. IMaterialInternal *GetReloadZcullMaterial();
  549. ShaderAPITextureHandle_t GetFullbrightLightmapTextureHandle() const;
  550. ShaderAPITextureHandle_t GetFullbrightBumpedLightmapTextureHandle() const;
  551. ShaderAPITextureHandle_t GetBlackTextureHandle() const;
  552. ShaderAPITextureHandle_t GetBlackAlphaZeroTextureHandle() const;
  553. ShaderAPITextureHandle_t GetFlatNormalTextureHandle() const;
  554. ShaderAPITextureHandle_t GetFlatSSBumpTextureHandle() const;
  555. ShaderAPITextureHandle_t GetGreyTextureHandle() const;
  556. ShaderAPITextureHandle_t GetGreyAlphaZeroTextureHandle() const;
  557. ShaderAPITextureHandle_t GetWhiteTextureHandle() const;
  558. ShaderAPITextureHandle_t GetFrameBufferTextureHandle() const;
  559. ShaderAPITextureHandle_t GetMaxDepthTextureHandle() const;
  560. // Helper methods
  561. void BindLightmap( Sampler_t stage, TextureBindFlags_t nBindFlags );
  562. void BindBumpLightmap( Sampler_t stage, TextureBindFlags_t nBindFlags );
  563. void BindFullbrightLightmap( Sampler_t stage, TextureBindFlags_t nBindFlags );
  564. void BindBumpedFullbrightLightmap( Sampler_t stage, TextureBindFlags_t nBindFlags );
  565. void BindPaintTexture( Sampler_t stage, TextureBindFlags_t nBindFlags );
  566. virtual void OnRenderDataUnreferenced();
  567. const CMatLightmaps *GetLightmaps() const;
  568. CMatLightmaps *GetLightmaps();
  569. const CMatPaintmaps *GetPaintmaps() const;
  570. CMatPaintmaps *GetPaintmaps();
  571. CUtlVector<PlaneStackElement> m_CustomClipPlanes; //implemented as a vector so we can remove in special ways
  572. IMesh *m_pBatchIndices;
  573. IMesh *m_pBatchMesh;
  574. IIndexBuffer *m_pCurrentIndexBuffer;
  575. CTextureReference m_pNonInteractiveTempFullscreenBuffer[MATERIAL_NON_INTERACTIVE_MODE_COUNT];
  576. CTextureReference m_pNonInteractivePacifier;
  577. CTextureReference m_pNonInteractiveLogo;
  578. MaterialNonInteractiveMode_t m_NonInteractiveMode;
  579. float m_flNormalizedX;
  580. float m_flNormalizedY;
  581. float m_flNormalizedSize;
  582. float m_flLogoNormalizedX;
  583. float m_flLogoNormalizedY;
  584. float m_flLogoNormalizedW;
  585. float m_flLogoNormalizedH;
  586. };
  587. //-----------------------------------------------------------------------------
  588. inline void CMatRenderContext::LoadBoneMatrix( int boneIndex, const matrix3x4_t& matrix )
  589. {
  590. g_pShaderAPI->LoadBoneMatrix( boneIndex, matrix.Base() );
  591. }
  592. //-----------------------------------------------------------------------------
  593. inline bool CMatRenderContext::InFlashlightMode() const
  594. {
  595. return m_bFlashlightEnable;
  596. }
  597. inline bool CMatRenderContext::IsRenderingPaint() const
  598. {
  599. return m_bRenderingPaint;
  600. }
  601. inline void CMatRenderContext::SetFlashlightState( const FlashlightState_t &state, const VMatrix &worldToTexture )
  602. {
  603. SetFlashlightStateEx( state, worldToTexture, NULL );
  604. }
  605. inline bool CMatRenderContext::IsCascadedShadowMapping() const
  606. {
  607. return m_bCascadedShadowMappingEnabled;
  608. }
  609. inline void CMatRenderContext::SetCascadedShadowMapping( bool bEnable )
  610. {
  611. m_bCascadedShadowMappingEnabled = bEnable;
  612. }
  613. inline float CMatRenderContextBase::ComputePixelWidthOfSphere( const Vector& vecOrigin, float flRadius )
  614. {
  615. return ComputePixelDiameterOfSphere( vecOrigin, flRadius ) * 2.0f;
  616. }
  617. inline void CMatRenderContext::FogColor3ubv( unsigned char const* rgb )
  618. {
  619. g_pShaderAPI->SceneFogColor3ub( rgb[0], rgb[1], rgb[2] );
  620. }
  621. inline void CMatRenderContext::FogMaxDensity( float flMaxDensity )
  622. {
  623. g_pShaderAPI->FogMaxDensity( flMaxDensity );
  624. }
  625. inline ITexture *CMatRenderContext::GetLocalCubemap( void )
  626. {
  627. return m_pLocalCubemapTexture;
  628. }
  629. inline void CMatRenderContextBase::SetRenderTarget( ITexture *pTexture_ )
  630. {
  631. SetRenderTargetEx( 0, pTexture_ );
  632. }
  633. //-----------------------------------------------------------------------------
  634. // Gets the bound morph's vertex format; returns 0 if no morph is bound
  635. //-----------------------------------------------------------------------------
  636. inline MorphFormat_t CMatRenderContext::GetBoundMorphFormat()
  637. {
  638. return m_pBoundMorph ? m_pBoundMorph->GetMorphFormat() : 0;
  639. }
  640. //-----------------------------------------------------------------------------
  641. // Use this to create static vertex and index buffers
  642. //-----------------------------------------------------------------------------
  643. inline IMesh* CMatRenderContext::CreateStaticMesh( VertexFormat_t vertexFormat, const char *pTextureBudgetGroup, IMaterial * pMaterial, VertexStreamSpec_t *pStreamSpec )
  644. {
  645. return g_pShaderDevice->CreateStaticMesh( vertexFormat, pTextureBudgetGroup, pMaterial, pStreamSpec );
  646. }
  647. inline void CMatRenderContext::SyncToken( const char *pToken )
  648. {
  649. #if !defined( _PS3 ) && !defined( _OSX )
  650. if ( g_pShaderAPI )
  651. #endif
  652. {
  653. g_pShaderAPI->SyncToken( pToken );
  654. }
  655. }
  656. inline enum MaterialHeightClipMode_t CMatRenderContextBase::GetHeightClipMode( void )
  657. {
  658. return m_HeightClipMode;
  659. }
  660. inline int CMatRenderContextBase::GetLightmapPage( void )
  661. {
  662. return m_lightmapPageID;
  663. }
  664. inline CMaterialSystem *CMatRenderContext::GetMaterialSystem() const
  665. {
  666. return m_pMaterialSystem;
  667. }
  668. #if defined( _PS3 ) || defined( _OSX )
  669. #undef g_pShaderAPI
  670. #endif
  671. //-----------------------------------------------------------------------------
  672. #endif // CMATERIALRENDERSTATE_H