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.

253 lines
8.7 KiB

  1. //========= Copyright 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. // Initialize adapter information
  56. void InitAdapterInfo();
  57. // Determines hardware caps from D3D
  58. bool ComputeCapsFromD3D( HardwareCaps_t *pCaps, IDXGIAdapter *pAdapter, IDXGIOutput *pOutput );
  59. // Returns the amount of video memory in bytes for a particular adapter
  60. virtual int GetVidMemBytes( int nAdapter ) const;
  61. // Returns the appropriate adapter output to use
  62. IDXGIOutput* GetAdapterOutput( int nAdapter ) const;
  63. // Returns the adapter interface for a particular adapter
  64. IDXGIAdapter* GetAdapter( int nAdapter ) const;
  65. // Used to enumerate adapters, attach to windows
  66. IDXGIFactory *m_pDXGIFactory;
  67. bool m_bObeyDxCommandlineOverride: 1;
  68. friend class CShaderDeviceDx10;
  69. };
  70. //-----------------------------------------------------------------------------
  71. // The Dx10 implementation of the shader device
  72. //-----------------------------------------------------------------------------
  73. class CShaderDeviceDx10 : public CShaderDeviceBase
  74. {
  75. public:
  76. // constructor, destructor
  77. CShaderDeviceDx10();
  78. virtual ~CShaderDeviceDx10();
  79. public:
  80. // Methods of IShaderDevice
  81. virtual bool IsUsingGraphics() const;
  82. virtual int GetCurrentAdapter() const;
  83. virtual ImageFormat GetBackBufferFormat() const;
  84. virtual void GetBackBufferDimensions( int& width, int& height ) const;
  85. virtual void SpewDriverInfo() const;
  86. virtual void Present();
  87. virtual IShaderBuffer* CompileShader( const char *pProgram, size_t nBufLen, const char *pShaderVersion );
  88. virtual VertexShaderHandle_t CreateVertexShader( IShaderBuffer *pShader );
  89. virtual void DestroyVertexShader( VertexShaderHandle_t hShader );
  90. virtual GeometryShaderHandle_t CreateGeometryShader( IShaderBuffer* pShaderBuffer );
  91. virtual void DestroyGeometryShader( GeometryShaderHandle_t hShader );
  92. virtual PixelShaderHandle_t CreatePixelShader( IShaderBuffer* pShaderBuffer );
  93. virtual void DestroyPixelShader( PixelShaderHandle_t hShader );
  94. virtual void ReleaseResources() {}
  95. virtual void ReacquireResources() {}
  96. virtual IMesh* CreateStaticMesh( VertexFormat_t format, const char *pTextureBudgetGroup, IMaterial * pMaterial );
  97. virtual void DestroyStaticMesh( IMesh* mesh );
  98. virtual IVertexBuffer *CreateVertexBuffer( ShaderBufferType_t type, VertexFormat_t fmt, int nVertexCount, const char *pTextureBudgetGroup );
  99. virtual void DestroyVertexBuffer( IVertexBuffer *pVertexBuffer );
  100. virtual IIndexBuffer *CreateIndexBuffer( ShaderBufferType_t type, MaterialIndexFormat_t fmt, int nIndexCount, const char *pTextureBudgetGroup );
  101. virtual void DestroyIndexBuffer( IIndexBuffer *pIndexBuffer );
  102. virtual IVertexBuffer *GetDynamicVertexBuffer( int nStreamID, VertexFormat_t vertexFormat, bool bBuffered = true );
  103. virtual IIndexBuffer *GetDynamicIndexBuffer( MaterialIndexFormat_t fmt, bool bBuffered = true );
  104. virtual void SetHardwareGammaRamp( float fGamma, float fGammaTVRangeMin, float fGammaTVRangeMax, float fGammaTVExponent, bool bTVEnabled );
  105. // A special path used to tick the front buffer while loading on the 360
  106. virtual void EnableNonInteractiveMode( MaterialNonInteractiveMode_t mode, ShaderNonInteractiveInfo_t *pInfo ) {}
  107. virtual void RefreshFrontBufferNonInteractive( ) {}
  108. virtual void HandleThreadEvent( uint32 threadEvent ) {}
  109. public:
  110. // Methods of CShaderDeviceBase
  111. virtual bool InitDevice( void *hWnd, int nAdapter, const ShaderDeviceInfo_t& mode );
  112. virtual void ShutdownDevice();
  113. virtual bool IsDeactivated() const { return false; }
  114. // Other public methods
  115. ID3D10VertexShader* GetVertexShader( VertexShaderHandle_t hShader ) const;
  116. ID3D10GeometryShader* GetGeometryShader( GeometryShaderHandle_t hShader ) const;
  117. ID3D10PixelShader* GetPixelShader( PixelShaderHandle_t hShader ) const;
  118. ID3D10InputLayout* GetInputLayout( VertexShaderHandle_t hShader, VertexFormat_t format );
  119. private:
  120. struct InputLayout_t
  121. {
  122. ID3D10InputLayout *m_pInputLayout;
  123. VertexFormat_t m_VertexFormat;
  124. };
  125. typedef CUtlRBTree< InputLayout_t, unsigned short > InputLayoutDict_t;
  126. static bool InputLayoutLessFunc( const InputLayout_t &lhs, const InputLayout_t &rhs )
  127. {
  128. return ( lhs.m_VertexFormat < rhs.m_VertexFormat );
  129. }
  130. struct VertexShader_t
  131. {
  132. ID3D10VertexShader *m_pShader;
  133. ID3D10ShaderReflection *m_pInfo;
  134. void *m_pByteCode;
  135. size_t m_nByteCodeLen;
  136. InputLayoutDict_t m_InputLayouts;
  137. VertexShader_t() : m_InputLayouts( 0, 0, InputLayoutLessFunc ) {}
  138. };
  139. struct GeometryShader_t
  140. {
  141. ID3D10GeometryShader *m_pShader;
  142. ID3D10ShaderReflection *m_pInfo;
  143. };
  144. struct PixelShader_t
  145. {
  146. ID3D10PixelShader *m_pShader;
  147. ID3D10ShaderReflection *m_pInfo;
  148. };
  149. typedef CUtlFixedLinkedList< VertexShader_t >::IndexType_t VertexShaderIndex_t;
  150. typedef CUtlFixedLinkedList< GeometryShader_t >::IndexType_t GeometryShaderIndex_t;
  151. typedef CUtlFixedLinkedList< PixelShader_t >::IndexType_t PixelShaderIndex_t;
  152. void SetupHardwareCaps();
  153. void ReleaseInputLayouts( VertexShaderIndex_t nIndex );
  154. IDXGIOutput *m_pOutput;
  155. ID3D10Device *m_pDevice;
  156. IDXGISwapChain *m_pSwapChain;
  157. ID3D10RenderTargetView *m_pRenderTargetView;
  158. CUtlFixedLinkedList< VertexShader_t > m_VertexShaderDict;
  159. CUtlFixedLinkedList< GeometryShader_t > m_GeometryShaderDict;
  160. CUtlFixedLinkedList< PixelShader_t > m_PixelShaderDict;
  161. friend ID3D10Device *D3D10Device();
  162. friend IDXGISwapChain *D3D10SwapChain();
  163. friend ID3D10RenderTargetView *D3D10RenderTargetView();
  164. };
  165. //-----------------------------------------------------------------------------
  166. // Inline methods of CShaderDeviceDx10
  167. //-----------------------------------------------------------------------------
  168. inline ID3D10VertexShader* CShaderDeviceDx10::GetVertexShader( VertexShaderHandle_t hShader ) const
  169. {
  170. if ( hShader != VERTEX_SHADER_HANDLE_INVALID )
  171. return m_VertexShaderDict[ (VertexShaderIndex_t)hShader ].m_pShader;
  172. return NULL;
  173. }
  174. inline ID3D10GeometryShader* CShaderDeviceDx10::GetGeometryShader( GeometryShaderHandle_t hShader ) const
  175. {
  176. if ( hShader != GEOMETRY_SHADER_HANDLE_INVALID )
  177. return m_GeometryShaderDict[ (GeometryShaderIndex_t)hShader ].m_pShader;
  178. return NULL;
  179. }
  180. inline ID3D10PixelShader* CShaderDeviceDx10::GetPixelShader( PixelShaderHandle_t hShader ) const
  181. {
  182. if ( hShader != PIXEL_SHADER_HANDLE_INVALID )
  183. return m_PixelShaderDict[ (PixelShaderIndex_t)hShader ].m_pShader;
  184. return NULL;
  185. }
  186. //-----------------------------------------------------------------------------
  187. // Singleton
  188. //-----------------------------------------------------------------------------
  189. extern CShaderDeviceDx10* g_pShaderDeviceDx10;
  190. //-----------------------------------------------------------------------------
  191. // Utility methods
  192. //-----------------------------------------------------------------------------
  193. inline ID3D10Device *D3D10Device()
  194. {
  195. return g_pShaderDeviceDx10->m_pDevice;
  196. }
  197. inline IDXGISwapChain *D3D10SwapChain()
  198. {
  199. return g_pShaderDeviceDx10->m_pSwapChain;
  200. }
  201. inline ID3D10RenderTargetView *D3D10RenderTargetView()
  202. {
  203. return g_pShaderDeviceDx10->m_pRenderTargetView;
  204. }
  205. #endif // SHADERDEVICEDX10_H