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.

430 lines
16 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef ISHADERDEVICE_H
  9. #define ISHADERDEVICE_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "tier1/interface.h"
  14. #include "appframework/iappsystem.h"
  15. #include "bitmap/imageformat.h"
  16. #include "tier1/utlbuffer.h"
  17. #include "materialsystem/imaterial.h"
  18. #include "shaderapi/ishaderdynamic.h"
  19. //-----------------------------------------------------------------------------
  20. // forward declarations
  21. //-----------------------------------------------------------------------------
  22. struct MaterialAdapterInfo_t;
  23. class IMesh;
  24. class KeyValues;
  25. //-----------------------------------------------------------------------------
  26. // Describes how to set the mode
  27. //-----------------------------------------------------------------------------
  28. #define SHADER_DISPLAY_MODE_VERSION 1
  29. struct ShaderDisplayMode_t
  30. {
  31. ShaderDisplayMode_t() { memset( this, 0, sizeof(ShaderDisplayMode_t) ); m_nVersion = SHADER_DISPLAY_MODE_VERSION; }
  32. int m_nVersion;
  33. int m_nWidth; // 0 when running windowed means use desktop resolution
  34. int m_nHeight;
  35. ImageFormat m_Format; // use ImageFormats (ignored for windowed mode)
  36. int m_nRefreshRateNumerator; // Refresh rate. Use 0 in numerator + denominator for a default setting.
  37. int m_nRefreshRateDenominator; // Refresh rate = numerator / denominator.
  38. #ifdef _PS3
  39. float m_flAspectRatio;
  40. #endif
  41. };
  42. //-----------------------------------------------------------------------------
  43. // Describes how to set the device
  44. //-----------------------------------------------------------------------------
  45. #define SHADER_DEVICE_INFO_VERSION 1
  46. struct ShaderDeviceInfo_t
  47. {
  48. ShaderDeviceInfo_t() { memset( this, 0, sizeof(ShaderDeviceInfo_t) ); m_nVersion = SHADER_DEVICE_INFO_VERSION; m_DisplayMode.m_nVersion = SHADER_DISPLAY_MODE_VERSION; }
  49. int m_nVersion;
  50. ShaderDisplayMode_t m_DisplayMode;
  51. int m_nBackBufferCount; // valid values are 1 or 2 [2 results in triple buffering]
  52. int m_nAASamples; // Number of AA samples to use
  53. int m_nAAQuality; // AA quality level
  54. int m_nDXLevel; // 0 means use recommended DX level for this adapter
  55. int m_nWindowedSizeLimitWidth; // Used if m_bLimitWindowedSize is set, defines max bounds for the back buffer
  56. int m_nWindowedSizeLimitHeight;
  57. bool m_bWindowed : 1;
  58. bool m_bResizing : 1; // Only is meaningful when using windowed mode; means the window can be resized.
  59. bool m_bUseStencil : 1;
  60. bool m_bLimitWindowedSize : 1; // In windowed mode, should we prevent the back buffer from getting too large?
  61. bool m_bWaitForVSync : 1; // Would we not present until vsync?
  62. bool m_bScaleToOutputResolution : 1; // 360 ONLY: sets up hardware scaling
  63. bool m_bProgressive : 1; // 360 ONLY: interlaced or progressive
  64. bool m_bUsingMultipleWindows : 1; // Forces D3DPresent to use _COPY instead
  65. };
  66. //-----------------------------------------------------------------------------
  67. // Info for non-interactive mode
  68. //-----------------------------------------------------------------------------
  69. struct ShaderNonInteractiveInfo_t
  70. {
  71. // background
  72. ShaderAPITextureHandle_t m_hTempFullscreenTexture;
  73. // logo
  74. ShaderAPITextureHandle_t m_hLogoTexture;
  75. float m_flLogoNormalizedX;
  76. float m_flLogoNormalizedY;
  77. float m_flLogoNormalizedW;
  78. float m_flLogoNormalizedH;
  79. // pacifier
  80. int m_nPacifierCount;
  81. ShaderAPITextureHandle_t m_pPacifierTextures[64];
  82. float m_flNormalizedX;
  83. float m_flNormalizedY;
  84. float m_flNormalizedSize;
  85. };
  86. //-----------------------------------------------------------------------------
  87. // For vertex/index buffers. What type is it?
  88. // (NOTE: mirror this with a similarly named enum at the material system level for backwards compatability.)
  89. //-----------------------------------------------------------------------------
  90. enum ShaderBufferType_t
  91. {
  92. SHADER_BUFFER_TYPE_STATIC = 0,
  93. SHADER_BUFFER_TYPE_DYNAMIC,
  94. SHADER_BUFFER_TYPE_STATIC_TEMP,
  95. SHADER_BUFFER_TYPE_DYNAMIC_TEMP,
  96. SHADER_BUFFER_TYPE_COUNT,
  97. };
  98. inline bool IsDynamicBufferType( ShaderBufferType_t type )
  99. {
  100. return ( ( type == SHADER_BUFFER_TYPE_DYNAMIC ) || ( type == SHADER_BUFFER_TYPE_DYNAMIC_TEMP ) );
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Handle to a vertex, pixel, and geometry shader
  104. //-----------------------------------------------------------------------------
  105. DECLARE_POINTER_HANDLE( VertexShaderHandle_t );
  106. DECLARE_POINTER_HANDLE( GeometryShaderHandle_t );
  107. DECLARE_POINTER_HANDLE( PixelShaderHandle_t );
  108. #define VERTEX_SHADER_HANDLE_INVALID ( (VertexShaderHandle_t)0 )
  109. #define GEOMETRY_SHADER_HANDLE_INVALID ( (GeometryShaderHandle_t)0 )
  110. #define PIXEL_SHADER_HANDLE_INVALID ( (PixelShaderHandle_t)0 )
  111. //-----------------------------------------------------------------------------
  112. // A shader buffer returns a block of memory which must be released when done with it
  113. //-----------------------------------------------------------------------------
  114. abstract_class IShaderBuffer
  115. {
  116. public:
  117. virtual size_t GetSize() const = 0;
  118. virtual const void* GetBits() const = 0;
  119. virtual void Release() = 0;
  120. };
  121. //-----------------------------------------------------------------------------
  122. // Mode chance callback
  123. //-----------------------------------------------------------------------------
  124. typedef void (*ShaderModeChangeCallbackFunc_t)( void );
  125. //-----------------------------------------------------------------------------
  126. // Used by the scaleform UI to manage when then device is lost or reset, or when
  127. // the mode changes
  128. //-----------------------------------------------------------------------------
  129. class IShaderDevice;
  130. abstract_class IShaderDeviceDependentObject
  131. {
  132. public:
  133. virtual void DeviceLost( void ) = 0;
  134. virtual void DeviceReset( void *pDevice, void *pPresentParameters, void *pHWnd ) = 0;
  135. virtual void ScreenSizeChanged( int width, int height ) = 0;
  136. };
  137. //-----------------------------------------------------------------------------
  138. // Methods related to discovering and selecting devices
  139. //-----------------------------------------------------------------------------
  140. #define SHADER_DEVICE_MGR_INTERFACE_VERSION "ShaderDeviceMgr001"
  141. abstract_class IShaderDeviceMgr : public IAppSystem
  142. {
  143. public:
  144. // Gets the number of adapters...
  145. virtual int GetAdapterCount() const = 0;
  146. // Returns info about each adapter
  147. virtual void GetAdapterInfo( int nAdapter, MaterialAdapterInfo_t& info ) const = 0;
  148. // Gets recommended congifuration for a particular adapter at a particular dx level
  149. virtual bool GetRecommendedConfigurationInfo( int nAdapter, int nDXLevel, KeyValues *pConfiguration ) = 0;
  150. // Returns the number of modes
  151. virtual int GetModeCount( int nAdapter ) const = 0;
  152. // Returns mode information..
  153. virtual void GetModeInfo( ShaderDisplayMode_t* pInfo, int nAdapter, int nMode ) const = 0;
  154. // Returns the current mode info for the requested adapter
  155. virtual void GetCurrentModeInfo( ShaderDisplayMode_t* pInfo, int nAdapter ) const = 0;
  156. // Initialization, shutdown
  157. virtual bool SetAdapter( int nAdapter, int nFlags ) = 0;
  158. // Sets the mode
  159. // Use the returned factory to get at an IShaderDevice and an IShaderRender
  160. // and any other interfaces we decide to create.
  161. // A returned factory of NULL indicates the mode was not set properly.
  162. virtual CreateInterfaceFn SetMode( void *hWnd, int nAdapter, const ShaderDeviceInfo_t& mode ) = 0;
  163. // Installs a callback to get called
  164. virtual void AddModeChangeCallback( ShaderModeChangeCallbackFunc_t func ) = 0;
  165. virtual void RemoveModeChangeCallback( ShaderModeChangeCallbackFunc_t func ) = 0;
  166. virtual bool GetRecommendedVideoConfig( int nAdapter, KeyValues *pConfiguration ) = 0;
  167. virtual void AddDeviceDependentObject( IShaderDeviceDependentObject *pObject ) = 0;
  168. virtual void RemoveDeviceDependentObject( IShaderDeviceDependentObject *pObject ) = 0;
  169. };
  170. //-----------------------------------------------------------------------------
  171. // Methods related to control of the device
  172. //-----------------------------------------------------------------------------
  173. #define SHADER_DEVICE_INTERFACE_VERSION "ShaderDevice001"
  174. abstract_class IShaderDevice
  175. {
  176. public:
  177. // Releases/reloads resources when other apps want some memory
  178. virtual void ReleaseResources( bool bReleaseManagedResources = true ) = 0;
  179. virtual void ReacquireResources() = 0;
  180. // returns the backbuffer format and dimensions
  181. virtual ImageFormat GetBackBufferFormat() const = 0;
  182. virtual void GetBackBufferDimensions( int& width, int& height ) const = 0;
  183. virtual const AspectRatioInfo_t &GetAspectRatioInfo( void ) const = 0;
  184. // Returns the current adapter in use
  185. virtual int GetCurrentAdapter() const = 0;
  186. // Are we using graphics?
  187. virtual bool IsUsingGraphics() const = 0;
  188. // Use this to spew information about the 3D layer
  189. virtual void SpewDriverInfo() const = 0;
  190. // What's the bit depth of the stencil buffer?
  191. virtual int StencilBufferBits() const = 0;
  192. // Are we using a mode that uses MSAA
  193. virtual bool IsAAEnabled() const = 0;
  194. // Does a page flip
  195. virtual void Present() = 0;
  196. // Returns the window size
  197. virtual void GetWindowSize( int &nWidth, int &nHeight ) const = 0;
  198. // Gamma ramp control
  199. virtual void SetHardwareGammaRamp( float fGamma, float fGammaTVRangeMin, float fGammaTVRangeMax, float fGammaTVExponent, bool bTVEnabled ) = 0;
  200. // Creates/ destroys a child window
  201. virtual bool AddView( void* hWnd ) = 0;
  202. virtual void RemoveView( void* hWnd ) = 0;
  203. // Activates a view
  204. virtual void SetView( void* hWnd ) = 0;
  205. // Shader compilation
  206. virtual IShaderBuffer* CompileShader( const char *pProgram, size_t nBufLen, const char *pShaderVersion ) = 0;
  207. // Shader creation, destruction
  208. virtual VertexShaderHandle_t CreateVertexShader( IShaderBuffer* pShaderBuffer ) = 0;
  209. virtual void DestroyVertexShader( VertexShaderHandle_t hShader ) = 0;
  210. virtual GeometryShaderHandle_t CreateGeometryShader( IShaderBuffer* pShaderBuffer ) = 0;
  211. virtual void DestroyGeometryShader( GeometryShaderHandle_t hShader ) = 0;
  212. virtual PixelShaderHandle_t CreatePixelShader( IShaderBuffer* pShaderBuffer ) = 0;
  213. virtual void DestroyPixelShader( PixelShaderHandle_t hShader ) = 0;
  214. // Utility methods to make shader creation simpler
  215. // NOTE: For the utlbuffer version, use a binary buffer for a compiled shader
  216. // and a text buffer for a source-code (.fxc) shader
  217. VertexShaderHandle_t CreateVertexShader( const char *pProgram, size_t nBufLen, const char *pShaderVersion );
  218. VertexShaderHandle_t CreateVertexShader( CUtlBuffer &buf, const char *pShaderVersion = NULL );
  219. GeometryShaderHandle_t CreateGeometryShader( const char *pProgram, size_t nBufLen, const char *pShaderVersion );
  220. GeometryShaderHandle_t CreateGeometryShader( CUtlBuffer &buf, const char *pShaderVersion = NULL );
  221. PixelShaderHandle_t CreatePixelShader( const char *pProgram, size_t nBufLen, const char *pShaderVersion );
  222. PixelShaderHandle_t CreatePixelShader( CUtlBuffer &buf, const char *pShaderVersion = NULL );
  223. // NOTE: Deprecated!! Use CreateVertexBuffer/CreateIndexBuffer instead
  224. // Creates/destroys Mesh
  225. virtual IMesh* CreateStaticMesh( VertexFormat_t vertexFormat, const char *pTextureBudgetGroup, IMaterial * pMaterial = NULL, VertexStreamSpec_t *pStreamSpec = NULL ) = 0;
  226. virtual void DestroyStaticMesh( IMesh* mesh ) = 0;
  227. // Creates/destroys static vertex + index buffers
  228. virtual IVertexBuffer *CreateVertexBuffer( ShaderBufferType_t type, VertexFormat_t fmt, int nVertexCount, const char *pBudgetGroup ) = 0;
  229. virtual void DestroyVertexBuffer( IVertexBuffer *pVertexBuffer ) = 0;
  230. virtual IIndexBuffer *CreateIndexBuffer( ShaderBufferType_t bufferType, MaterialIndexFormat_t fmt, int nIndexCount, const char *pBudgetGroup ) = 0;
  231. virtual void DestroyIndexBuffer( IIndexBuffer *pIndexBuffer ) = 0;
  232. // Do we need to specify the stream here in the case of locking multiple dynamic VBs on different streams?
  233. virtual IVertexBuffer *GetDynamicVertexBuffer( int nStreamID, VertexFormat_t vertexFormat, bool bBuffered = true ) = 0;
  234. virtual IIndexBuffer *GetDynamicIndexBuffer() = 0;
  235. // A special path used to tick the front buffer while loading on the 360
  236. virtual void EnableNonInteractiveMode( MaterialNonInteractiveMode_t mode, ShaderNonInteractiveInfo_t *pInfo = NULL ) = 0;
  237. virtual void RefreshFrontBufferNonInteractive( ) = 0;
  238. virtual void HandleThreadEvent( uint32 threadEvent ) = 0;
  239. #if defined( DX_TO_GL_ABSTRACTION ) && !defined( _GAMECONSOLE )
  240. virtual void DoStartupShaderPreloading( void ) = 0;
  241. #endif
  242. #ifdef _CERT
  243. static
  244. #else
  245. virtual
  246. #endif
  247. void OnDebugEvent( const char * pEvent = "" ){}
  248. };
  249. //-----------------------------------------------------------------------------
  250. // Helper wrapper for IShaderBuffer for reading precompiled shader files
  251. // NOTE: This is meant to be instanced on the stack; so don't call Release!
  252. //-----------------------------------------------------------------------------
  253. class CUtlShaderBuffer : public IShaderBuffer
  254. {
  255. public:
  256. CUtlShaderBuffer( CUtlBuffer &buf ) : m_pBuf( &buf ) {}
  257. virtual size_t GetSize() const
  258. {
  259. return m_pBuf->TellMaxPut();
  260. }
  261. virtual const void* GetBits() const
  262. {
  263. return m_pBuf->Base();
  264. }
  265. virtual void Release()
  266. {
  267. Assert( 0 );
  268. }
  269. private:
  270. CUtlBuffer *m_pBuf;
  271. };
  272. //-----------------------------------------------------------------------------
  273. // Inline methods of IShaderDevice
  274. //-----------------------------------------------------------------------------
  275. inline VertexShaderHandle_t IShaderDevice::CreateVertexShader( CUtlBuffer &buf, const char *pShaderVersion )
  276. {
  277. // NOTE: Text buffers are assumed to have source-code shader files
  278. // Binary buffers are assumed to have compiled shader files
  279. if ( buf.IsText() )
  280. {
  281. Assert( pShaderVersion );
  282. return CreateVertexShader( (const char *)buf.Base(), buf.TellMaxPut(), pShaderVersion );
  283. }
  284. CUtlShaderBuffer shaderBuffer( buf );
  285. return CreateVertexShader( &shaderBuffer );
  286. }
  287. inline VertexShaderHandle_t IShaderDevice::CreateVertexShader( const char *pProgram, size_t nBufLen, const char *pShaderVersion )
  288. {
  289. VertexShaderHandle_t hVertexShader = VERTEX_SHADER_HANDLE_INVALID;
  290. IShaderBuffer* pShaderBuffer = CompileShader( pProgram, nBufLen, pShaderVersion );
  291. if ( pShaderBuffer )
  292. {
  293. hVertexShader = CreateVertexShader( pShaderBuffer );
  294. pShaderBuffer->Release();
  295. }
  296. return hVertexShader;
  297. }
  298. inline GeometryShaderHandle_t IShaderDevice::CreateGeometryShader( CUtlBuffer &buf, const char *pShaderVersion )
  299. {
  300. // NOTE: Text buffers are assumed to have source-code shader files
  301. // Binary buffers are assumed to have compiled shader files
  302. if ( buf.IsText() )
  303. {
  304. Assert( pShaderVersion );
  305. return CreateGeometryShader( (const char *)buf.Base(), buf.TellMaxPut(), pShaderVersion );
  306. }
  307. CUtlShaderBuffer shaderBuffer( buf );
  308. return CreateGeometryShader( &shaderBuffer );
  309. }
  310. inline GeometryShaderHandle_t IShaderDevice::CreateGeometryShader( const char *pProgram, size_t nBufLen, const char *pShaderVersion )
  311. {
  312. GeometryShaderHandle_t hGeometryShader = GEOMETRY_SHADER_HANDLE_INVALID;
  313. IShaderBuffer* pShaderBuffer = CompileShader( pProgram, nBufLen, pShaderVersion );
  314. if ( pShaderBuffer )
  315. {
  316. hGeometryShader = CreateGeometryShader( pShaderBuffer );
  317. pShaderBuffer->Release();
  318. }
  319. return hGeometryShader;
  320. }
  321. inline PixelShaderHandle_t IShaderDevice::CreatePixelShader( CUtlBuffer &buf, const char *pShaderVersion )
  322. {
  323. // NOTE: Text buffers are assumed to have source-code shader files
  324. // Binary buffers are assumed to have compiled shader files
  325. if ( buf.IsText() )
  326. {
  327. Assert( pShaderVersion );
  328. return CreatePixelShader( (const char *)buf.Base(), buf.TellMaxPut(), pShaderVersion );
  329. }
  330. CUtlShaderBuffer shaderBuffer( buf );
  331. return CreatePixelShader( &shaderBuffer );
  332. }
  333. inline PixelShaderHandle_t IShaderDevice::CreatePixelShader( const char *pProgram, size_t nBufLen, const char *pShaderVersion )
  334. {
  335. PixelShaderHandle_t hPixelShader = PIXEL_SHADER_HANDLE_INVALID;
  336. IShaderBuffer* pShaderBuffer = CompileShader( pProgram, nBufLen, pShaderVersion );
  337. if ( pShaderBuffer )
  338. {
  339. hPixelShader = CreatePixelShader( pShaderBuffer );
  340. pShaderBuffer->Release();
  341. }
  342. return hPixelShader;
  343. }
  344. #endif // ISHADERDEVICE_H