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.

254 lines
8.8 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef SHADERDEVICEDX10_H
  9. #define SHADERDEVICEDX10_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "shaderdevicebase.h"
  14. #include "tier1/utlvector.h"
  15. #include "tier1/utlrbtree.h"
  16. #include "tier1/utllinkedlist.h"
  17. //-----------------------------------------------------------------------------
  18. // Forward declaration
  19. //-----------------------------------------------------------------------------
  20. struct IDXGIFactory;
  21. struct IDXGIAdapter;
  22. struct IDXGIOutput;
  23. struct IDXGISwapChain;
  24. struct ID3D10Device;
  25. struct ID3D10RenderTargetView;
  26. struct ID3D10VertexShader;
  27. struct ID3D10PixelShader;
  28. struct ID3D10GeometryShader;
  29. struct ID3D10InputLayout;
  30. struct ID3D10ShaderReflection;
  31. //-----------------------------------------------------------------------------
  32. // The Base implementation of the shader device
  33. //-----------------------------------------------------------------------------
  34. class CShaderDeviceMgrDx10 : public CShaderDeviceMgrBase
  35. {
  36. typedef CShaderDeviceMgrBase BaseClass;
  37. public:
  38. // constructor, destructor
  39. CShaderDeviceMgrDx10();
  40. virtual ~CShaderDeviceMgrDx10();
  41. // Methods of IAppSystem
  42. virtual bool Connect( CreateInterfaceFn factory );
  43. virtual void Disconnect();
  44. virtual InitReturnVal_t Init();
  45. virtual void Shutdown();
  46. // Methods of IShaderDeviceMgr
  47. virtual int GetAdapterCount() const;
  48. virtual void GetAdapterInfo( int adapter, MaterialAdapterInfo_t& info ) const;
  49. virtual int GetModeCount( int nAdapter ) const;
  50. virtual void GetModeInfo( ShaderDisplayMode_t* pInfo, int nAdapter, int mode ) const;
  51. virtual void GetCurrentModeInfo( ShaderDisplayMode_t* pInfo, int nAdapter ) const;
  52. virtual bool SetAdapter( int nAdapter, int nFlags );
  53. virtual CreateInterfaceFn SetMode( void *hWnd, int nAdapter, const ShaderDeviceInfo_t& mode );
  54. private:
  55. // Returns the screen resolution
  56. virtual void GetDesktopResolution( int *pWidth, int *pHeight, int nAdapter ) const;
  57. // Initialize adapter information
  58. void InitAdapterInfo();
  59. // Determines hardware caps from D3D
  60. bool ComputeCapsFromD3D( HardwareCaps_t *pCaps, IDXGIAdapter *pAdapter, IDXGIOutput *pOutput );
  61. // Returns the amount of video memory in bytes for a particular adapter
  62. virtual int GetVidMemBytes( int nAdapter ) const;
  63. // Returns the appropriate adapter output to use
  64. IDXGIOutput* GetAdapterOutput( int nAdapter ) const;
  65. // Returns the adapter interface for a particular adapter
  66. IDXGIAdapter* GetAdapter( int nAdapter ) const;
  67. // Used to enumerate adapters, attach to windows
  68. IDXGIFactory *m_pDXGIFactory;
  69. friend class CShaderDeviceDx10;
  70. };
  71. //-----------------------------------------------------------------------------
  72. // The Dx10 implementation of the shader device
  73. //-----------------------------------------------------------------------------
  74. class CShaderDeviceDx10 : public CShaderDeviceBase
  75. {
  76. public:
  77. // constructor, destructor
  78. CShaderDeviceDx10();
  79. virtual ~CShaderDeviceDx10();
  80. public:
  81. // Methods of IShaderDevice
  82. virtual bool IsUsingGraphics() const;
  83. virtual int GetCurrentAdapter() const;
  84. virtual ImageFormat GetBackBufferFormat() const;
  85. virtual void GetBackBufferDimensions( int& width, int& height ) const;
  86. virtual void SpewDriverInfo() const;
  87. virtual void Present();
  88. virtual IShaderBuffer* CompileShader( const char *pProgram, size_t nBufLen, const char *pShaderVersion );
  89. virtual VertexShaderHandle_t CreateVertexShader( IShaderBuffer *pShader );
  90. virtual void DestroyVertexShader( VertexShaderHandle_t hShader );
  91. virtual GeometryShaderHandle_t CreateGeometryShader( IShaderBuffer* pShaderBuffer );
  92. virtual void DestroyGeometryShader( GeometryShaderHandle_t hShader );
  93. virtual PixelShaderHandle_t CreatePixelShader( IShaderBuffer* pShaderBuffer );
  94. virtual void DestroyPixelShader( PixelShaderHandle_t hShader );
  95. virtual void ReleaseResources( bool bReleaseManagedResources = true ) {}
  96. virtual void ReacquireResources() {}
  97. virtual IMesh* CreateStaticMesh( VertexFormat_t format, const char *pTextureBudgetGroup, IMaterial * pMaterial, VertexStreamSpec_t *pStreamSpec );
  98. virtual void DestroyStaticMesh( IMesh* mesh );
  99. virtual IVertexBuffer *CreateVertexBuffer( ShaderBufferType_t type, VertexFormat_t fmt, int nVertexCount, const char *pTextureBudgetGroup );
  100. virtual void DestroyVertexBuffer( IVertexBuffer *pVertexBuffer );
  101. virtual IIndexBuffer *CreateIndexBuffer( ShaderBufferType_t type, MaterialIndexFormat_t fmt, int nIndexCount, const char *pTextureBudgetGroup );
  102. virtual void DestroyIndexBuffer( IIndexBuffer *pIndexBuffer );
  103. virtual IVertexBuffer *GetDynamicVertexBuffer( int nStreamID, VertexFormat_t vertexFormat, bool bBuffered = true );
  104. virtual IIndexBuffer *GetDynamicIndexBuffer();
  105. virtual void SetHardwareGammaRamp( float fGamma, float fGammaTVRangeMin, float fGammaTVRangeMax, float fGammaTVExponent, bool bTVEnabled );
  106. // A special path used to tick the front buffer while loading on the 360
  107. virtual void EnableNonInteractiveMode( MaterialNonInteractiveMode_t mode, ShaderNonInteractiveInfo_t *pInfo ) {}
  108. virtual void RefreshFrontBufferNonInteractive( ) {}
  109. virtual void HandleThreadEvent( uint32 threadEvent ) {}
  110. public:
  111. // Methods of CShaderDeviceBase
  112. virtual bool InitDevice( void *hWnd, int nAdapter, const ShaderDeviceInfo_t& mode );
  113. virtual void ShutdownDevice();
  114. virtual bool IsDeactivated() const { return false; }
  115. // Other public methods
  116. ID3D10VertexShader* GetVertexShader( VertexShaderHandle_t hShader ) const;
  117. ID3D10GeometryShader* GetGeometryShader( GeometryShaderHandle_t hShader ) const;
  118. ID3D10PixelShader* GetPixelShader( PixelShaderHandle_t hShader ) const;
  119. ID3D10InputLayout* GetInputLayout( VertexShaderHandle_t hShader, VertexFormat_t format );
  120. private:
  121. struct InputLayout_t
  122. {
  123. ID3D10InputLayout *m_pInputLayout;
  124. VertexFormat_t m_VertexFormat;
  125. };
  126. typedef CUtlRBTree< InputLayout_t, unsigned short > InputLayoutDict_t;
  127. static bool InputLayoutLessFunc( const InputLayout_t &lhs, const InputLayout_t &rhs )
  128. {
  129. return ( lhs.m_VertexFormat < rhs.m_VertexFormat );
  130. }
  131. struct VertexShader_t
  132. {
  133. ID3D10VertexShader *m_pShader;
  134. ID3D10ShaderReflection *m_pInfo;
  135. void *m_pByteCode;
  136. size_t m_nByteCodeLen;
  137. InputLayoutDict_t m_InputLayouts;
  138. VertexShader_t() : m_InputLayouts( 0, 0, InputLayoutLessFunc ) {}
  139. };
  140. struct GeometryShader_t
  141. {
  142. ID3D10GeometryShader *m_pShader;
  143. ID3D10ShaderReflection *m_pInfo;
  144. };
  145. struct PixelShader_t
  146. {
  147. ID3D10PixelShader *m_pShader;
  148. ID3D10ShaderReflection *m_pInfo;
  149. };
  150. typedef CUtlFixedLinkedList< VertexShader_t >::IndexType_t VertexShaderIndex_t;
  151. typedef CUtlFixedLinkedList< GeometryShader_t >::IndexType_t GeometryShaderIndex_t;
  152. typedef CUtlFixedLinkedList< PixelShader_t >::IndexType_t PixelShaderIndex_t;
  153. void SetupHardwareCaps();
  154. void ReleaseInputLayouts( VertexShaderIndex_t nIndex );
  155. IDXGIOutput *m_pOutput;
  156. ID3D10Device *m_pDevice;
  157. IDXGISwapChain *m_pSwapChain;
  158. ID3D10RenderTargetView *m_pRenderTargetView;
  159. CUtlFixedLinkedList< VertexShader_t > m_VertexShaderDict;
  160. CUtlFixedLinkedList< GeometryShader_t > m_GeometryShaderDict;
  161. CUtlFixedLinkedList< PixelShader_t > m_PixelShaderDict;
  162. friend ID3D10Device *D3D10Device();
  163. friend IDXGISwapChain *D3D10SwapChain();
  164. friend ID3D10RenderTargetView *D3D10RenderTargetView();
  165. };
  166. //-----------------------------------------------------------------------------
  167. // Inline methods of CShaderDeviceDx10
  168. //-----------------------------------------------------------------------------
  169. inline ID3D10VertexShader* CShaderDeviceDx10::GetVertexShader( VertexShaderHandle_t hShader ) const
  170. {
  171. if ( hShader != VERTEX_SHADER_HANDLE_INVALID )
  172. return m_VertexShaderDict[ (VertexShaderIndex_t)hShader ].m_pShader;
  173. return NULL;
  174. }
  175. inline ID3D10GeometryShader* CShaderDeviceDx10::GetGeometryShader( GeometryShaderHandle_t hShader ) const
  176. {
  177. if ( hShader != GEOMETRY_SHADER_HANDLE_INVALID )
  178. return m_GeometryShaderDict[ (GeometryShaderIndex_t)hShader ].m_pShader;
  179. return NULL;
  180. }
  181. inline ID3D10PixelShader* CShaderDeviceDx10::GetPixelShader( PixelShaderHandle_t hShader ) const
  182. {
  183. if ( hShader != PIXEL_SHADER_HANDLE_INVALID )
  184. return m_PixelShaderDict[ (PixelShaderIndex_t)hShader ].m_pShader;
  185. return NULL;
  186. }
  187. //-----------------------------------------------------------------------------
  188. // Singleton
  189. //-----------------------------------------------------------------------------
  190. extern CShaderDeviceDx10* g_pShaderDeviceDx10;
  191. //-----------------------------------------------------------------------------
  192. // Utility methods
  193. //-----------------------------------------------------------------------------
  194. inline ID3D10Device *D3D10Device()
  195. {
  196. return g_pShaderDeviceDx10->m_pDevice;
  197. }
  198. inline IDXGISwapChain *D3D10SwapChain()
  199. {
  200. return g_pShaderDeviceDx10->m_pSwapChain;
  201. }
  202. inline ID3D10RenderTargetView *D3D10RenderTargetView()
  203. {
  204. return g_pShaderDeviceDx10->m_pRenderTargetView;
  205. }
  206. #endif // SHADERDEVICEDX10_H