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.

233 lines
7.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef SHADERDEVICEBASE_H
  9. #define SHADERDEVICEBASE_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "togl/rendermechanism.h"
  14. #include "shaderapi/IShaderDevice.h"
  15. #include "IHardwareConfigInternal.h"
  16. #include "bitmap/imageformat.h"
  17. #include "materialsystem/imaterialsystem.h"
  18. #include "hardwareconfig.h"
  19. //-----------------------------------------------------------------------------
  20. // Forward declarations
  21. //-----------------------------------------------------------------------------
  22. class KeyValues;
  23. //-----------------------------------------------------------------------------
  24. // define this if you want to run with NVPERFHUD
  25. //-----------------------------------------------------------------------------
  26. //#define NVPERFHUD 1
  27. //-----------------------------------------------------------------------------
  28. // Uncomment this to activate the reference rasterizer
  29. //-----------------------------------------------------------------------------
  30. //#define USE_REFERENCE_RASTERIZER 1
  31. //-----------------------------------------------------------------------------
  32. // Uncomment to check for -nulldevice on command line and use D3DDEVTYPE_NULLREF.
  33. //-----------------------------------------------------------------------------
  34. #define ENABLE_NULLREF_DEVICE_SUPPORT
  35. //-----------------------------------------------------------------------------
  36. // The Base implementation of the shader device
  37. //-----------------------------------------------------------------------------
  38. class CShaderDeviceMgrBase : public IShaderDeviceMgr
  39. {
  40. public:
  41. // constructor, destructor
  42. CShaderDeviceMgrBase();
  43. virtual ~CShaderDeviceMgrBase();
  44. // Methods of IAppSystem
  45. virtual bool Connect( CreateInterfaceFn factory );
  46. virtual void Disconnect();
  47. virtual void *QueryInterface( const char *pInterfaceName );
  48. // Methods of IShaderDeviceMgr
  49. virtual bool GetRecommendedConfigurationInfo( int nAdapter, int nDXLevel, KeyValues *pCongifuration );
  50. virtual void AddModeChangeCallback( ShaderModeChangeCallbackFunc_t func );
  51. virtual void RemoveModeChangeCallback( ShaderModeChangeCallbackFunc_t func );
  52. // Reads in the hardware caps from the dxsupport.cfg file
  53. void ReadHardwareCaps( HardwareCaps_t &caps, int nDxLevel );
  54. // Reads in the max + preferred DX support level
  55. void ReadDXSupportLevels( HardwareCaps_t &caps );
  56. // Returns the hardware caps for a particular adapter
  57. const HardwareCaps_t& GetHardwareCaps( int nAdapter ) const;
  58. // Invokes mode change callbacks
  59. void InvokeModeChangeCallbacks();
  60. // Factory to return from SetMode
  61. static void* ShaderInterfaceFactory( const char *pInterfaceName, int *pReturnCode );
  62. // Returns only valid dx levels
  63. int GetClosestActualDXLevel( int nDxLevel ) const;
  64. protected:
  65. struct AdapterInfo_t
  66. {
  67. HardwareCaps_t m_ActualCaps;
  68. };
  69. private:
  70. // Reads in the dxsupport.cfg keyvalues
  71. KeyValues *ReadDXSupportKeyValues();
  72. // Reads in ConVars + config variables
  73. void LoadConfig( KeyValues *pKeyValues, KeyValues *pConfiguration );
  74. // Loads the hardware caps, for cases in which the D3D caps lie or where we need to augment the caps
  75. void LoadHardwareCaps( KeyValues *pGroup, HardwareCaps_t &caps );
  76. // Gets the recommended configuration associated with a particular dx level
  77. bool GetRecommendedConfigurationInfo( int nAdapter, int nDXLevel, int nVendorID, int nDeviceID, KeyValues *pConfiguration );
  78. // Returns the amount of video memory in bytes for a particular adapter
  79. virtual int GetVidMemBytes( int nAdapter ) const = 0;
  80. // Looks for override keyvalues in the dxsupport cfg keyvalues
  81. KeyValues *FindDXLevelSpecificConfig( KeyValues *pKeyValues, int nDxLevel );
  82. KeyValues *FindDXLevelAndVendorSpecificConfig( KeyValues *pKeyValues, int nDxLevel, int nVendorID );
  83. KeyValues *FindCPUSpecificConfig( KeyValues *pKeyValues, int nCPUMhz, bool bAMD );
  84. KeyValues *FindMemorySpecificConfig( KeyValues *pKeyValues, int nSystemRamMB );
  85. KeyValues *FindVidMemSpecificConfig( KeyValues *pKeyValues, int nVideoRamMB );
  86. KeyValues *FindCardSpecificConfig( KeyValues *pKeyValues, int nVendorID, int nDeviceID );
  87. protected:
  88. // Stores adapter info for all adapters
  89. CUtlVector<AdapterInfo_t> m_Adapters;
  90. // Installed mode change callbacks
  91. CUtlVector< ShaderModeChangeCallbackFunc_t > m_ModeChangeCallbacks;
  92. KeyValues *m_pDXSupport;
  93. };
  94. //-----------------------------------------------------------------------------
  95. // The Base implementation of the shader device
  96. //-----------------------------------------------------------------------------
  97. class CShaderDeviceBase : public IShaderDevice
  98. {
  99. public:
  100. enum IPCMessage_t
  101. {
  102. RELEASE_MESSAGE = 0x5E740DE0,
  103. REACQUIRE_MESSAGE = 0x5E740DE1,
  104. EVICT_MESSAGE = 0x5E740DE2,
  105. };
  106. // Methods of IShaderDevice
  107. public:
  108. virtual ImageFormat GetBackBufferFormat() const;
  109. virtual int StencilBufferBits() const;
  110. virtual bool IsAAEnabled() const;
  111. virtual bool AddView( void* hWnd );
  112. virtual void RemoveView( void* hWnd );
  113. virtual void SetView( void* hWnd );
  114. virtual void GetWindowSize( int& nWidth, int& nHeight ) const;
  115. // Methods exposed to the rest of shader api
  116. virtual bool InitDevice( void *hWnd, int nAdapter, const ShaderDeviceInfo_t& mode ) = 0;
  117. virtual void ShutdownDevice() = 0;
  118. virtual bool IsDeactivated() const = 0;
  119. public:
  120. // constructor, destructor
  121. CShaderDeviceBase();
  122. virtual ~CShaderDeviceBase();
  123. virtual void OtherAppInitializing( bool initializing ) {}
  124. virtual void EvictManagedResourcesInternal() {}
  125. void* GetIPCHWnd();
  126. void SendIPCMessage( IPCMessage_t message );
  127. protected:
  128. // IPC communication for multiple shaderapi apps
  129. void InstallWindowHook( void *hWnd );
  130. void RemoveWindowHook( void *hWnd );
  131. void SetCurrentThreadAsOwner();
  132. void RemoveThreadOwner();
  133. bool ThreadOwnsDevice();
  134. // Finds a child window
  135. int FindView( void* hWnd ) const;
  136. int m_nAdapter;
  137. void *m_hWnd;
  138. void* m_hWndCookie;
  139. bool m_bInitialized : 1;
  140. bool m_bIsMinimized : 1;
  141. // The current view hwnd
  142. void* m_ViewHWnd;
  143. int m_nWindowWidth;
  144. int m_nWindowHeight;
  145. uint32 m_dwThreadId;
  146. };
  147. //-----------------------------------------------------------------------------
  148. // Inline methods
  149. //-----------------------------------------------------------------------------
  150. inline void* CShaderDeviceBase::GetIPCHWnd()
  151. {
  152. return m_hWndCookie;
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Helper class to reduce code related to shader buffers
  156. //-----------------------------------------------------------------------------
  157. template< class T >
  158. class CShaderBuffer : public IShaderBuffer
  159. {
  160. public:
  161. CShaderBuffer( T *pBlob ) : m_pBlob( pBlob ) {}
  162. virtual size_t GetSize() const
  163. {
  164. return m_pBlob ? m_pBlob->GetBufferSize() : 0;
  165. }
  166. virtual const void* GetBits() const
  167. {
  168. return m_pBlob ? m_pBlob->GetBufferPointer() : NULL;
  169. }
  170. virtual void Release()
  171. {
  172. if ( m_pBlob )
  173. {
  174. m_pBlob->Release();
  175. }
  176. delete this;
  177. }
  178. private:
  179. T *m_pBlob;
  180. };
  181. #endif // SHADERDEVICEBASE_H