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.

720 lines
32 KiB

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