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.

239 lines
7.5 KiB

  1. //===== Copyright � 1996-2005, 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. // The Base implementation of the shader device
  33. //-----------------------------------------------------------------------------
  34. class CShaderDeviceMgrBase : public CBaseAppSystem< IShaderDeviceMgr >
  35. {
  36. public:
  37. // constructor, destructor
  38. CShaderDeviceMgrBase();
  39. virtual ~CShaderDeviceMgrBase();
  40. // Methods of IAppSystem
  41. virtual bool Connect( CreateInterfaceFn factory );
  42. virtual void Disconnect();
  43. virtual void *QueryInterface( const char *pInterfaceName );
  44. // Methods of IShaderDeviceMgr
  45. virtual bool GetRecommendedConfigurationInfo( int nAdapter, int nDXLevel, KeyValues *pCongifuration );
  46. virtual void AddModeChangeCallback( ShaderModeChangeCallbackFunc_t func );
  47. virtual void RemoveModeChangeCallback( ShaderModeChangeCallbackFunc_t func );
  48. virtual bool GetRecommendedVideoConfig( int nAdapter, KeyValues *pConfiguration );
  49. virtual void AddDeviceDependentObject( IShaderDeviceDependentObject *pObject );
  50. virtual void RemoveDeviceDependentObject( IShaderDeviceDependentObject *pObject );
  51. virtual void InvokeDeviceLostNotifications( void );
  52. virtual void InvokeDeviceResetNotifications( IDirect3DDevice9 *pDevice, D3DPRESENT_PARAMETERS *pPresentParameters, void *pHWnd );
  53. // Reads in the hardware caps from the dxsupport.cfg file
  54. void ReadHardwareCaps( HardwareCaps_t &caps, int nDxLevel );
  55. // Reads in the max + preferred DX support level
  56. void ReadDXSupportLevels( HardwareCaps_t &caps );
  57. // Returns the hardware caps for a particular adapter
  58. const HardwareCaps_t& GetHardwareCaps( int nAdapter ) const;
  59. // Invokes mode change callbacks
  60. void InvokeModeChangeCallbacks( int screenWidth, int screenHeight );
  61. // Factory to return from SetMode
  62. static void* ShaderInterfaceFactory( const char *pInterfaceName, int *pReturnCode );
  63. // Returns only valid dx levels
  64. int GetClosestActualDXLevel( int nDxLevel ) const;
  65. protected:
  66. struct AdapterInfo_t
  67. {
  68. HardwareCaps_t m_ActualCaps;
  69. };
  70. private:
  71. // Reads in the dxsupport.cfg keyvalues
  72. KeyValues *ReadDXSupportKeyValues();
  73. // Reads in ConVars + config variables
  74. void LoadConfig( KeyValues *pKeyValues, KeyValues *pConfiguration );
  75. // Loads the hardware caps, for cases in which the D3D caps lie or where we need to augment the caps
  76. void LoadHardwareCaps( KeyValues *pGroup, HardwareCaps_t &caps );
  77. // Gets the recommended configuration associated with a particular dx level
  78. bool GetRecommendedVideoConfig( int nAdapter, int nVendorID, int nDeviceID, KeyValues *pConfiguration );
  79. bool GetRecommendedConfigurationInfo( int nAdapter, int nDXLevel, int nVendorID, int nDeviceID, KeyValues *pConfiguration );
  80. // Returns the amount of video memory in bytes for a particular adapter
  81. virtual int GetVidMemBytes( int nAdapter ) const = 0;
  82. // Returns the physical screen desktop resolution
  83. virtual void GetDesktopResolution( int *pWidth, int *pHeight, int nAdapter ) const = 0;
  84. // Looks for override keyvalues in the dxsupport cfg keyvalues
  85. KeyValues *FindDXLevelSpecificConfig( KeyValues *pKeyValues, int nDxLevel );
  86. KeyValues *FindDXLevelAndVendorSpecificConfig( KeyValues *pKeyValues, int nDxLevel, int nVendorID );
  87. KeyValues *FindCPUSpecificConfig( KeyValues *pKeyValues, int nCPUMhz, bool bAMD );
  88. KeyValues *FindMemorySpecificConfig( KeyValues *pKeyValues, int nSystemRamMB );
  89. KeyValues *FindVidMemSpecificConfig( KeyValues *pKeyValues, int nVideoRamMB );
  90. KeyValues *FindCardSpecificConfig( KeyValues *pKeyValues, int nVendorID, int nDeviceID );
  91. protected:
  92. // Stores adapter info for all adapters
  93. CUtlVector<AdapterInfo_t> m_Adapters;
  94. // Installed mode change callbacks
  95. CUtlVector< ShaderModeChangeCallbackFunc_t > m_ModeChangeCallbacks;
  96. CUtlVector< IShaderDeviceDependentObject* > m_DeviceDependentObjects;
  97. KeyValues *m_pDXSupport;
  98. };
  99. //-----------------------------------------------------------------------------
  100. // The Base implementation of the shader device
  101. //-----------------------------------------------------------------------------
  102. class CShaderDeviceBase : public IShaderDevice
  103. {
  104. public:
  105. enum IPCMessage_t
  106. {
  107. RELEASE_MESSAGE = 0x5E740DE0,
  108. REACQUIRE_MESSAGE = 0x5E740DE1,
  109. EVICT_MESSAGE = 0x5E740DE2,
  110. };
  111. // Methods of IShaderDevice
  112. public:
  113. virtual ImageFormat GetBackBufferFormat() const;
  114. virtual int StencilBufferBits() const;
  115. virtual bool IsAAEnabled() const;
  116. virtual bool AddView( void* hWnd );
  117. virtual void RemoveView( void* hWnd );
  118. virtual void SetView( void* hWnd );
  119. virtual void GetWindowSize( int& nWidth, int& nHeight ) const;
  120. // Methods exposed to the rest of shader api
  121. virtual bool InitDevice( void *hWnd, int nAdapter, const ShaderDeviceInfo_t& mode ) = 0;
  122. virtual void ShutdownDevice() = 0;
  123. virtual bool IsDeactivated() const = 0;
  124. public:
  125. // constructor, destructor
  126. CShaderDeviceBase();
  127. virtual ~CShaderDeviceBase();
  128. virtual void OtherAppInitializing( bool initializing ) {}
  129. virtual void EvictManagedResourcesInternal() {}
  130. void* GetIPCHWnd();
  131. void SendIPCMessage( IPCMessage_t message );
  132. protected:
  133. // IPC communication for multiple shaderapi apps
  134. void InstallWindowHook( void *hWnd );
  135. void RemoveWindowHook( void *hWnd );
  136. void SetCurrentThreadAsOwner();
  137. void RemoveThreadOwner();
  138. bool ThreadOwnsDevice();
  139. // Finds a child window
  140. int FindView( void* hWnd ) const;
  141. int m_nAdapter;
  142. void *m_hWnd;
  143. void* m_hWndCookie;
  144. bool m_bInitialized : 1;
  145. bool m_bIsMinimized : 1;
  146. // The current view hwnd
  147. void* m_ViewHWnd;
  148. int m_nWindowWidth;
  149. int m_nWindowHeight;
  150. ThreadId_t m_dwThreadId;
  151. };
  152. //-----------------------------------------------------------------------------
  153. // Inline methods
  154. //-----------------------------------------------------------------------------
  155. inline void* CShaderDeviceBase::GetIPCHWnd()
  156. {
  157. return m_hWndCookie;
  158. }
  159. //-----------------------------------------------------------------------------
  160. // Helper class to reduce code related to shader buffers
  161. //-----------------------------------------------------------------------------
  162. template< class T >
  163. class CShaderBuffer : public IShaderBuffer
  164. {
  165. public:
  166. CShaderBuffer( T *pBlob ) : m_pBlob( pBlob ) {}
  167. virtual size_t GetSize() const
  168. {
  169. return m_pBlob ? m_pBlob->GetBufferSize() : 0;
  170. }
  171. virtual const void* GetBits() const
  172. {
  173. return m_pBlob ? m_pBlob->GetBufferPointer() : NULL;
  174. }
  175. virtual void Release()
  176. {
  177. if ( m_pBlob )
  178. {
  179. m_pBlob->Release();
  180. }
  181. delete this;
  182. }
  183. private:
  184. T *m_pBlob;
  185. };
  186. #endif // SHADERDEVICEBASE_H