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.

382 lines
13 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef IRENDERCONTEXT_H
  9. #define IRENDERCONTEXT_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "rendersystem/irenderdevice.h"
  14. #include "mathlib/vector4d.h"
  15. #include "tier1/generichash.h"
  16. #include "rendersystem/renderstate.h"
  17. #include "mathlib/vmatrix.h"
  18. #include "rendersystem/schema/renderable.g.h"
  19. //-----------------------------------------------------------------------------
  20. // forward declarations
  21. //-----------------------------------------------------------------------------
  22. enum RenderBufferStride_t
  23. {
  24. RENDER_BUFFER_STRIDE_INVALID = -1,
  25. };
  26. //-----------------------------------------------------------------------------
  27. // Index/vertex buffer lock info
  28. //-----------------------------------------------------------------------------
  29. struct LockDesc_t
  30. {
  31. void *m_pMemory;
  32. };
  33. //-----------------------------------------------------------------------------
  34. // Viewport structure
  35. //-----------------------------------------------------------------------------
  36. #define RENDER_VIEWPORT_VERSION 1
  37. struct RenderViewport_t
  38. {
  39. int m_nVersion;
  40. int m_nTopLeftX;
  41. int m_nTopLeftY;
  42. int m_nWidth;
  43. int m_nHeight;
  44. float m_flMinZ;
  45. float m_flMaxZ;
  46. RenderViewport_t() : m_nVersion( RENDER_VIEWPORT_VERSION ) {}
  47. void Init()
  48. {
  49. memset( this, 0, sizeof(RenderViewport_t) );
  50. m_nVersion = RENDER_VIEWPORT_VERSION;
  51. }
  52. void Init( int x, int y, int nWidth, int nHeight, float flMinZ = 0.0f, float flMaxZ = 1.0f )
  53. {
  54. m_nVersion = RENDER_VIEWPORT_VERSION;
  55. m_nTopLeftX = x; m_nTopLeftY = y; m_nWidth = nWidth; m_nHeight = nHeight;
  56. m_flMinZ = flMinZ;
  57. m_flMaxZ = flMaxZ;
  58. }
  59. };
  60. // clear flags
  61. enum RenderClearFlags_t
  62. {
  63. RENDER_CLEAR_FLAGS_CLEAR_DEPTH = 0x1,
  64. RENDER_CLEAR_FLAGS_CLEAR_STENCIL = 0x2
  65. };
  66. enum StandardTextureID_t
  67. {
  68. STDTEXTURE_NORMAL_DEPTH_BUFFER = 0,
  69. STDTEXTURE_LIGHTPASS_OUTPUT,
  70. STDTEXTURE_NUMBER_OF_IDS
  71. };
  72. class CDisplayList // subclasses in devices
  73. {
  74. public:
  75. CDisplayList *m_pNext;
  76. };
  77. //-----------------------------------------------------------------------------
  78. // Data for the material system
  79. //-----------------------------------------------------------------------------
  80. #define RENDER_MATERIAL_NUM_WORLD_MATRICES 53
  81. struct RenderMaterialData_t
  82. {
  83. int nMode;
  84. float flTime;
  85. float mWorldArray[ RENDER_MATERIAL_NUM_WORLD_MATRICES ][3][4];
  86. VMatrix mView;
  87. VMatrix mProjection;
  88. VMatrix mVP;
  89. VMatrix mWVP;
  90. Vector vCameraPosition;
  91. Vector vCameraForwardVec;
  92. Vector vCameraUpVec;
  93. float flNearPlane;
  94. float flFarPlane;
  95. float vViewportSize[2];
  96. HRenderTexture customTextures[2];
  97. void Init()
  98. {
  99. nMode = 0;
  100. flTime = 0.0f;
  101. float m4x3Identity[3][4] = { 1.0f, 0.0f, 0.0f, 0.0f,
  102. 0.0f, 1.0f, 0.0f, 0.0f,
  103. 0.0f, 0.0f, 1.0f, 0.0f };
  104. for ( int i = 0; i < RENDER_MATERIAL_NUM_WORLD_MATRICES; i++ )
  105. {
  106. memcpy( mWorldArray[i], m4x3Identity, sizeof( m4x3Identity ) );
  107. }
  108. mView.Identity();
  109. mProjection.Identity();
  110. mVP.Identity();
  111. mWVP.Identity();
  112. vCameraPosition.Init( 0.0f, 0.0f, 0.0f );
  113. vCameraForwardVec.Init( 0.0f, 0.0f, 0.0f );
  114. vCameraUpVec.Init( 0.0f, 0.0f, 0.0f );
  115. flNearPlane = 0.0f;
  116. flFarPlane = 1.0f;
  117. vViewportSize[0] = 0.0f;
  118. vViewportSize[1] = 0.0f;
  119. customTextures[0] = RENDER_TEXTURE_HANDLE_INVALID;
  120. customTextures[1] = RENDER_TEXTURE_HANDLE_INVALID;
  121. }
  122. };
  123. //-----------------------------------------------------------------------------
  124. // Render context interface
  125. //-----------------------------------------------------------------------------
  126. abstract_class IRenderContext
  127. {
  128. // rendering functions. in flux
  129. public:
  130. virtual void SetSwapChain( SwapChainHandle_t hSwapChain ) = 0;
  131. // Called when the context is handed to a thread.
  132. virtual void AttachToCurrentThread() = 0;
  133. virtual void Clear( const Vector4D &vecRGBAColor, int nFlags = 0 ) = 0; // any RENDER_CLEAR_FLAGS_CLEAR_xxx flags
  134. virtual void SetViewports( int nCount, const RenderViewport_t* pViewports, bool setImmediately = false ) = 0;
  135. virtual void GetViewport( RenderViewport_t *pViewport, int nViewport ) = 0;
  136. // Restriction: This can only be called as the first call after a
  137. // context is created, or the first call after a Submit.
  138. virtual void BindRenderTargets( RenderTargetBinding_t hRenderTargetBinding ) = 0;
  139. // set this to actually draw, and clear display list data
  140. virtual void Submit( void ) =0;
  141. // use this interface to finish a command list but not submit it until later. This call resets
  142. // the rendercontext
  143. virtual CDisplayList *DetachCommandList ( void ) =0;
  144. // add items to the next submitted batches. Doing a submit resets this
  145. virtual void DependsOn( CDependencyDescriptor *pDesc ) = 0;
  146. virtual void Satisfies( CDependencyDescriptor *pDesc ) = 0;
  147. // Creates/destroys vertex + index buffers
  148. // For CreateDynamicIndexBuffer, nMaxInstanceCount == 0 means we don't expect
  149. // the buffer to be used w/ instanced rendering.
  150. // mNaxInstanceCount == INT_MAX means we have no idea how many instances will be rendered
  151. virtual HRenderBuffer CreateDynamicVertexBuffer( const BufferDesc_t& desc ) = 0;
  152. virtual void DestroyDynamicVertexBuffer( HRenderBuffer hVertexBuffer ) = 0;
  153. virtual HRenderBuffer CreateDynamicIndexBuffer( const BufferDesc_t& desc, int nMaxInstanceCount = 0 ) = 0;
  154. virtual void DestroyDynamicIndexBuffer( HRenderBuffer hIndexBuffer ) = 0;
  155. // Use this to read or write buffers, whether lock is for read or write
  156. // depends on the type of buffer (dynamic, staging, semistatic, etc)
  157. virtual bool LockIndexBuffer( HRenderBuffer hIndexBuffer, int nMaxSizeInBytes, LockDesc_t *pDesc ) = 0;
  158. virtual void UnlockIndexBuffer( HRenderBuffer hIndexBuffer, int nWrittenSizeInBytes, LockDesc_t *pDesc ) = 0;
  159. virtual bool LockVertexBuffer( HRenderBuffer hVertexBuffer, int nMaxSizeInBytes, LockDesc_t *pDesc ) = 0;
  160. virtual void UnlockVertexBuffer( HRenderBuffer hVertexBuffer, int nWrittenSizeInBytes, LockDesc_t *pDesc ) = 0;
  161. // Binds an vertex/index buffer
  162. virtual bool BindIndexBuffer( HRenderBuffer hIndexBuffer, int nOffset ) = 0;
  163. virtual bool BindVertexBuffer( int nSlot, HRenderBuffer hVertexBuffer, int nOffset, int nStride = RENDER_BUFFER_STRIDE_INVALID ) = 0;
  164. // Binds a vertex shader
  165. virtual void BindVertexShader( RenderShaderHandle_t hVertexShader, RenderInputLayout_t hInputLayout ) = 0;
  166. // Binds all other shader types
  167. virtual void BindShader( RenderShaderType_t nType, RenderShaderHandle_t hShader ) = 0;
  168. // textures
  169. virtual void BindTexture( int nSamplerIndex, HRenderTexture hTexture, RenderShaderType_t nTargetPipelineStage = RENDER_PIXEL_SHADER ) = 0;
  170. virtual void SetSamplerStatePS( int nSamplerIndex, RsFilter_t eFilterMode, RsTextureAddressMode_t eUWrapMode = RS_TEXTURE_ADDRESS_WRAP,
  171. RsTextureAddressMode_t eVWrapMode = RS_TEXTURE_ADDRESS_WRAP, RsTextureAddressMode_t eWWrapMode = RS_TEXTURE_ADDRESS_WRAP,
  172. RsComparison_t eTextureComparisonMode = RS_CMP_LESS ) = 0;
  173. virtual void SetSamplerStateVS( int nSamplerIndex, RsFilter_t eFilterMode, RsTextureAddressMode_t eUWrapMode = RS_TEXTURE_ADDRESS_WRAP,
  174. RsTextureAddressMode_t eVWrapMode = RS_TEXTURE_ADDRESS_WRAP, RsTextureAddressMode_t eWWrapMode = RS_TEXTURE_ADDRESS_WRAP,
  175. RsComparison_t eTextureComparisonMode = RS_CMP_LESS ) = 0;
  176. virtual void SetSamplerState( int32 nSamplerIndex, const CSamplerStateDesc *pSamplerDesc, RenderShaderType_t nTargetPipelineStage = RENDER_PIXEL_SHADER ) = 0;
  177. // download data into a texture. It is acceptable to pass NULL for pData, in which case a d3d
  178. // texture will be allocated and set up, but with unset bits (this is useful for getting a
  179. // render target into memory for instance).
  180. // NOTE: This doesn't work on file-backed textures
  181. virtual void SetTextureData( HRenderTexture hTexture, const TextureDesc_t *pDataDesc, const void *pData, int nDataSize, bool bIsPreTiled, int nSpecificMipLevelToSet = -1, Rect_t const *pSubRectToUpdate = NULL ) = 0;
  182. // Draws stuff!
  183. // If nMaxVertexCount == 0, then calculate max vertex count from the size of the vertex buffer
  184. virtual void Draw( RenderPrimitiveType_t type, int nFirstVertex, int nVertexCount ) = 0;
  185. virtual void DrawInstanced( RenderPrimitiveType_t type, int nFirstVertex, int nVertexCountPerInstance, int nInstanceCount ) = 0;
  186. virtual void DrawIndexed( RenderPrimitiveType_t type, int nFirstIndex, int nIndexCount, int nMaxVertexCount = 0 ) = 0;
  187. virtual void DrawIndexedInstanced( RenderPrimitiveType_t type, int nFirstIndex, int nIndexCountPerInstance, int nInstanceCount, int nMaxVertexCount = 0 ) = 0;
  188. // misc state setting
  189. virtual void SetCullMode( RenderCullMode_t eCullMode ) = 0;
  190. virtual void SetBlendMode( RenderBlendMode_t eBlendMode, float const *pBlendFactor = NULL ) = 0;
  191. virtual void SetZBufferMode( RenderZBufferMode_t eZBufferMode ) = 0;
  192. virtual RsRasterizerStateHandle_t FindOrCreateRasterizerState( const RsRasterizerStateDesc_t *pRsDesc ) = 0;
  193. virtual void SetRasterizerState( RsRasterizerStateHandle_t rasterizerState ) = 0;
  194. virtual RsDepthStencilStateHandle_t FindOrCreateDepthStencilState( const RsDepthStencilStateDesc_t *pRsDesc ) = 0;
  195. virtual void SetDepthStencilState( RsDepthStencilStateHandle_t rasterizerState, uint32 nStencilRef = 0 ) = 0;
  196. virtual RsBlendStateHandle_t FindOrCreateBlendState( const RsBlendStateDesc_t *pBlendDesc ) = 0;
  197. virtual void SetBlendState( RsBlendStateHandle_t blendState, float const *pBlendFactor = NULL, uint32 nSampleMask = 0xFFFFFFFF ) = 0;
  198. // set the data in a constant buffer
  199. virtual void SetConstantBufferData( ConstantBufferHandle_t hConstantBuffer, void const *pData, int nDataSize ) = 0;
  200. // bind constant buffers
  201. virtual void BindConstantBuffer( RenderShaderType_t nType, ConstantBufferHandle_t hConstantBuffer, int nSlot, int nRegisterBaseForDx9 ) = 0;
  202. // get access to per-frame pooled constant buffers.
  203. virtual ConstantBufferHandle_t GetDynamicConstantBuffer( int nSize, void const *pData = NULL ) =0;
  204. // Get the input layout associated with this renderbuffer. Returns RENDER_INPUT_LAYOUT_INVALID if none is associated.
  205. virtual RenderInputLayout_t GetInputLayoutForVertexBuffer( HRenderBuffer hBuffer, InputLayoutVariation_t nVariation = INPUT_LAYOUT_VARIATION_DEFAULT ) = 0;
  206. // Blocks the thread until the render thread completely empties
  207. virtual void Flush() = 0;
  208. // Forces a device lost
  209. virtual void ForceDeviceLost() = 0;
  210. // Returns the device associated w/ the context
  211. virtual IRenderDevice *GetDevice() = 0;
  212. // PIX events for debugging/perf analysis
  213. virtual void BeginPixEvent( color32 c, const char *pEventName ) = 0;
  214. virtual void EndPixEvent( ) = 0;
  215. virtual void PixSetMarker( color32 c, const char *pEventName ) = 0;
  216. // "standard" texture support.
  217. virtual void BindStandardTexture( int nSamplerIndex, StandardTextureID_t nTextureID, RenderShaderType_t nTargetPipelineStage = RENDER_PIXEL_SHADER ) = 0;
  218. virtual void SetStandardTexture( StandardTextureID_t nTextureID, HRenderTexture hTexture ) =0;
  219. virtual HRenderTexture GetStandardTexture( StandardTextureID_t nTextureID ) =0;
  220. // Material system data
  221. virtual RenderMaterialData_t &GetMaterialData() = 0;
  222. protected:
  223. // Don't allow delete calls on an IRenderContext
  224. virtual ~IRenderContext() {}
  225. };
  226. //-----------------------------------------------------------------------------
  227. // simple helper class with all inlines
  228. //-----------------------------------------------------------------------------
  229. class CRenderContextPtr
  230. {
  231. public:
  232. CRenderContextPtr( IRenderDevice *pDevice, RenderTargetBinding_t hRenderTargetBinding = RENDER_TARGET_BINDING_INVALID );
  233. ~CRenderContextPtr( void );
  234. IRenderContext *operator->( void ) const;
  235. operator IRenderContext*() const;
  236. void Release( );
  237. protected:
  238. IRenderContext *m_pContext;
  239. IRenderDevice *m_pDevice;
  240. };
  241. FORCEINLINE CRenderContextPtr::CRenderContextPtr( IRenderDevice *pDevice, RenderTargetBinding_t hRenderTargetBinding )
  242. {
  243. m_pDevice = pDevice;
  244. m_pContext = pDevice->GetRenderContext( );
  245. m_pContext->BindRenderTargets( hRenderTargetBinding );
  246. }
  247. FORCEINLINE CRenderContextPtr::~CRenderContextPtr( void )
  248. {
  249. Release();
  250. }
  251. FORCEINLINE void CRenderContextPtr::Release( )
  252. {
  253. if ( m_pContext )
  254. {
  255. m_pContext->Submit( );
  256. m_pDevice->ReleaseRenderContext( m_pContext );
  257. m_pContext = NULL;
  258. m_pDevice = NULL;
  259. }
  260. }
  261. // delegate to context via -> override
  262. FORCEINLINE IRenderContext *CRenderContextPtr::operator->( void ) const
  263. {
  264. return m_pContext;
  265. }
  266. // Cast operator (to pass to other methods)
  267. FORCEINLINE CRenderContextPtr::operator IRenderContext*() const
  268. {
  269. return m_pContext;
  270. }
  271. //-----------------------------------------------------------------------------
  272. // Pix measurement helper class
  273. //-----------------------------------------------------------------------------
  274. class CRenderPixEvent
  275. {
  276. public:
  277. CRenderPixEvent( IRenderContext *pRenderContext, color32 c, const char *pName )
  278. {
  279. m_pContext = pRenderContext;
  280. m_pContext->BeginPixEvent( c, pName );
  281. }
  282. ~CRenderPixEvent()
  283. {
  284. Release();
  285. }
  286. void Release()
  287. {
  288. if ( m_pContext )
  289. {
  290. m_pContext->EndPixEvent();
  291. m_pContext = NULL;
  292. }
  293. }
  294. private:
  295. IRenderContext *m_pContext;
  296. };
  297. #endif // IRENDERCONTEXT_H