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.

846 lines
34 KiB

  1. //===== Copyright (c) Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef ISHADERAPI_H
  9. #define ISHADERAPI_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "mathlib/vector4d.h"
  14. #include "shaderapi/ishaderdynamic.h"
  15. #include "shaderapi/IShaderDevice.h"
  16. #include "materialsystem/deformations.h"
  17. #include "shaderlib/shadercombosemantics.h"
  18. //-----------------------------------------------------------------------------
  19. // forward declarations
  20. //-----------------------------------------------------------------------------
  21. class IShaderUtil;
  22. class IFileSystem;
  23. class CPixelWriter;
  24. class CMeshBuilder;
  25. struct MaterialVideoMode_t;
  26. class IMesh;
  27. class IVertexBuffer;
  28. class IIndexBuffer;
  29. struct MeshDesc_t;
  30. enum MaterialCullMode_t;
  31. class IDataCache;
  32. struct MorphWeight_t;
  33. struct MeshInstanceData_t;
  34. #ifdef _X360
  35. enum RTMultiSampleCount360_t;
  36. #endif
  37. struct ShaderComboInformation_t;
  38. //-----------------------------------------------------------------------------
  39. // This must match the definition in playback.cpp!
  40. //-----------------------------------------------------------------------------
  41. enum ShaderRenderTarget_t
  42. {
  43. SHADER_RENDERTARGET_BACKBUFFER = -1,
  44. SHADER_RENDERTARGET_DEPTHBUFFER = -1,
  45. // GR - no RT, used to disable depth buffer
  46. SHADER_RENDERTARGET_NONE = -2,
  47. };
  48. //-----------------------------------------------------------------------------
  49. // The state snapshot handle
  50. //-----------------------------------------------------------------------------
  51. typedef short StateSnapshot_t;
  52. //-----------------------------------------------------------------------------
  53. // The state snapshot handle
  54. //-----------------------------------------------------------------------------
  55. typedef int ShaderDLL_t;
  56. //-----------------------------------------------------------------------------
  57. // Texture creation
  58. //-----------------------------------------------------------------------------
  59. enum CreateTextureFlags_t
  60. {
  61. TEXTURE_CREATE_CUBEMAP = 0x00001,
  62. TEXTURE_CREATE_RENDERTARGET = 0x00002,
  63. TEXTURE_CREATE_MANAGED = 0x00004,
  64. TEXTURE_CREATE_DEPTHBUFFER = 0x00008,
  65. TEXTURE_CREATE_DYNAMIC = 0x00010,
  66. TEXTURE_CREATE_AUTOMIPMAP = 0x00020,
  67. TEXTURE_CREATE_VERTEXTEXTURE = 0x00040,
  68. TEXTURE_CREATE_CACHEABLE = 0x00080, // 360: texture may be subject to streaming
  69. TEXTURE_CREATE_NOD3DMEMORY = 0x00100, // CONSOLE: real allocation needs to occur later
  70. TEXTURE_CREATE_REDUCED = 0x00200, // CONSOLE: true dimensions forced smaller (i.e. exclusion)
  71. TEXTURE_CREATE_EXCLUDED = 0x00400, // CONSOLE: marked as excluded
  72. TEXTURE_CREATE_DEFAULT_POOL = 0x00800,
  73. TEXTURE_CREATE_UNFILTERABLE_OK = 0x01000,
  74. TEXTURE_CREATE_CANCONVERTFORMAT = 0x02000, // 360: allow format conversions at load
  75. TEXTURE_CREATE_PWLCORRECTED = 0x04000, // 360: texture is pwl corrected
  76. TEXTURE_CREATE_ERROR = 0x08000, // CONSOLE: texture was forced to checkerboard
  77. TEXTURE_CREATE_SYSMEM = 0x10000,
  78. TEXTURE_CREATE_SRGB = 0x20000, // Posix/GL only, for textures which are SRGB-readable
  79. TEXTURE_CREATE_ANISOTROPIC = 0x40000, // Posix/GL only, for textures which are flagged to use max aniso
  80. TEXTURE_CREATE_REUSEHANDLES = 0x80000, // hint to re-use supplied texture handles
  81. };
  82. //-----------------------------------------------------------------------------
  83. // Viewport structure
  84. //-----------------------------------------------------------------------------
  85. #define SHADER_VIEWPORT_VERSION 1
  86. struct ShaderViewport_t
  87. {
  88. int m_nVersion;
  89. int m_nTopLeftX;
  90. int m_nTopLeftY;
  91. int m_nWidth;
  92. int m_nHeight;
  93. float m_flMinZ;
  94. float m_flMaxZ;
  95. ShaderViewport_t() : m_nVersion( SHADER_VIEWPORT_VERSION ) {}
  96. void Init()
  97. {
  98. memset( this, 0, sizeof(ShaderViewport_t) );
  99. m_nVersion = SHADER_VIEWPORT_VERSION;
  100. }
  101. void Init( int x, int y, int nWidth, int nHeight, float flMinZ = 0.0f, float flMaxZ = 1.0f )
  102. {
  103. m_nVersion = SHADER_VIEWPORT_VERSION;
  104. m_nTopLeftX = x; m_nTopLeftY = y; m_nWidth = nWidth; m_nHeight = nHeight;
  105. m_flMinZ = flMinZ;
  106. m_flMaxZ = flMaxZ;
  107. }
  108. };
  109. //-----------------------------------------------------------------------------
  110. // Fill modes
  111. //-----------------------------------------------------------------------------
  112. enum ShaderFillMode_t
  113. {
  114. SHADER_FILL_SOLID = 0,
  115. SHADER_FILL_WIREFRAME,
  116. };
  117. //-----------------------------------------------------------------------------
  118. // Rasterization state object
  119. //-----------------------------------------------------------------------------
  120. struct ShaderRasterState_t
  121. {
  122. ShaderFillMode_t m_FillMode;
  123. MaterialCullMode_t m_CullMode;
  124. bool m_bCullEnable : 1;
  125. bool m_bDepthBias : 1;
  126. bool m_bScissorEnable : 1;
  127. bool m_bMultisampleEnable : 1;
  128. };
  129. //-----------------------------------------------------------------------------
  130. // allowed stencil operations. These match the d3d operations
  131. //-----------------------------------------------------------------------------
  132. enum ShaderStencilOp_t
  133. {
  134. #if !defined( _X360 )
  135. SHADER_STENCILOP_KEEP = 1,
  136. SHADER_STENCILOP_ZERO = 2,
  137. SHADER_STENCILOP_SET_TO_REFERENCE = 3,
  138. SHADER_STENCILOP_INCREMENT_CLAMP = 4,
  139. SHADER_STENCILOP_DECREMENT_CLAMP = 5,
  140. SHADER_STENCILOP_INVERT = 6,
  141. SHADER_STENCILOP_INCREMENT_WRAP = 7,
  142. SHADER_STENCILOP_DECREMENT_WRAP = 8,
  143. #else
  144. SHADER_STENCILOP_KEEP = D3DSTENCILOP_KEEP,
  145. SHADER_STENCILOP_ZERO = D3DSTENCILOP_ZERO,
  146. SHADER_STENCILOP_SET_TO_REFERENCE = D3DSTENCILOP_REPLACE,
  147. SHADER_STENCILOP_INCREMENT_CLAMP = D3DSTENCILOP_INCRSAT,
  148. SHADER_STENCILOP_DECREMENT_CLAMP = D3DSTENCILOP_DECRSAT,
  149. SHADER_STENCILOP_INVERT = D3DSTENCILOP_INVERT,
  150. SHADER_STENCILOP_INCREMENT_WRAP = D3DSTENCILOP_INCR,
  151. SHADER_STENCILOP_DECREMENT_WRAP = D3DSTENCILOP_DECR,
  152. #endif
  153. SHADER_STENCILOP_FORCE_DWORD = 0x7fffffff
  154. };
  155. enum ShaderStencilFunc_t
  156. {
  157. #if !defined( _X360 )
  158. SHADER_STENCILFUNC_NEVER = 1,
  159. SHADER_STENCILFUNC_LESS = 2,
  160. SHADER_STENCILFUNC_EQUAL = 3,
  161. SHADER_STENCILFUNC_LEQUAL = 4,
  162. SHADER_STENCILFUNC_GREATER = 5,
  163. SHADER_STENCILFUNC_NOTEQUAL = 6,
  164. SHADER_STENCILFUNC_GEQUAL = 7,
  165. SHADER_STENCILFUNC_ALWAYS = 8,
  166. #else
  167. SHADER_STENCILFUNC_NEVER = D3DCMP_NEVER,
  168. SHADER_STENCILFUNC_LESS = D3DCMP_LESS,
  169. SHADER_STENCILFUNC_EQUAL = D3DCMP_EQUAL,
  170. SHADER_STENCILFUNC_LEQUAL = D3DCMP_LESSEQUAL,
  171. SHADER_STENCILFUNC_GREATER = D3DCMP_GREATER,
  172. SHADER_STENCILFUNC_NOTEQUAL = D3DCMP_NOTEQUAL,
  173. SHADER_STENCILFUNC_GEQUAL = D3DCMP_GREATEREQUAL,
  174. SHADER_STENCILFUNC_ALWAYS = D3DCMP_ALWAYS,
  175. #endif
  176. SHADER_STENCILFUNC_FORCE_DWORD = 0x7fffffff
  177. };
  178. #if defined( _X360 )
  179. enum ShaderHiStencilFunc_t
  180. {
  181. SHADER_HI_STENCILFUNC_EQUAL = D3DHSCMP_EQUAL,
  182. SHADER_HI_STENCILFUNC_NOTEQUAL = D3DHSCMP_NOTEQUAL,
  183. SHADER_HI_STENCILFUNC_FORCE_DWORD = 0x7fffffff
  184. };
  185. #endif
  186. //-----------------------------------------------------------------------------
  187. // Stencil state
  188. //-----------------------------------------------------------------------------
  189. struct ShaderStencilState_t
  190. {
  191. bool m_bEnable;
  192. ShaderStencilOp_t m_FailOp;
  193. ShaderStencilOp_t m_ZFailOp;
  194. ShaderStencilOp_t m_PassOp;
  195. ShaderStencilFunc_t m_CompareFunc;
  196. int m_nReferenceValue;
  197. uint32 m_nTestMask;
  198. uint32 m_nWriteMask;
  199. #if defined( _X360 )
  200. bool m_bHiStencilEnable;
  201. bool m_bHiStencilWriteEnable;
  202. ShaderHiStencilFunc_t m_HiStencilCompareFunc;
  203. int m_nHiStencilReferenceValue;
  204. #endif
  205. ShaderStencilState_t()
  206. {
  207. m_bEnable = false;
  208. m_PassOp = m_FailOp = m_ZFailOp = SHADER_STENCILOP_KEEP;
  209. m_CompareFunc = SHADER_STENCILFUNC_ALWAYS;
  210. m_nReferenceValue = 0;
  211. m_nTestMask = m_nWriteMask = 0xFFFFFFFF;
  212. #if defined( _X360 )
  213. m_bHiStencilEnable = false;
  214. m_bHiStencilWriteEnable = false;
  215. m_HiStencilCompareFunc = SHADER_HI_STENCILFUNC_EQUAL;
  216. m_nHiStencilReferenceValue = 0;
  217. #endif
  218. }
  219. };
  220. //-----------------------------------------------------------------------------
  221. // Used for occlusion queries
  222. //-----------------------------------------------------------------------------
  223. DECLARE_POINTER_HANDLE( ShaderAPIOcclusionQuery_t );
  224. #define INVALID_SHADERAPI_OCCLUSION_QUERY_HANDLE ( (ShaderAPIOcclusionQuery_t)0 )
  225. enum ShaderAPIOcclusionQueryResult_t
  226. {
  227. OCCLUSION_QUERY_RESULT_PENDING = -1,
  228. OCCLUSION_QUERY_RESULT_ERROR = -2
  229. };
  230. #define OCCLUSION_QUERY_FINISHED( iQueryResult ) ( ( iQueryResult ) != OCCLUSION_QUERY_RESULT_PENDING )
  231. //-----------------------------------------------------------------------------
  232. // Used on the 360 to create meshes from data in write-combined memory
  233. //-----------------------------------------------------------------------------
  234. struct ExternalMeshInfo_t
  235. {
  236. IMaterial *m_pMaterial;
  237. VertexFormat_t m_VertexFormat;
  238. bool m_bFlexMesh;
  239. IMesh* m_pVertexOverride;
  240. IMesh* m_pIndexOverride;
  241. };
  242. struct ExternalMeshData_t
  243. {
  244. uint8 *m_pVertexData;
  245. int m_nVertexCount;
  246. int m_nVertexSizeInBytes;
  247. uint16 *m_pIndexData;
  248. int m_nIndexCount;
  249. };
  250. //-----------------------------------------------------------------------------
  251. // This is what the material system gets to see.
  252. //-----------------------------------------------------------------------------
  253. #define SHADERAPI_INTERFACE_VERSION "ShaderApi029"
  254. abstract_class IShaderAPI : public IShaderDynamicAPI
  255. {
  256. public:
  257. //
  258. // NOTE: These methods have been ported to DX10
  259. //
  260. // Viewport methods
  261. virtual void SetViewports( int nCount, const ShaderViewport_t* pViewports, bool setImmediately = false ) = 0;
  262. virtual int GetViewports( ShaderViewport_t* pViewports, int nMax ) const = 0;
  263. // Buffer clearing
  264. virtual void ClearBuffers( bool bClearColor, bool bClearDepth, bool bClearStencil, int renderTargetWidth, int renderTargetHeight ) = 0;
  265. virtual void ClearColor3ub( unsigned char r, unsigned char g, unsigned char b ) = 0;
  266. virtual void ClearColor4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a ) = 0;
  267. // Methods related to binding shaders
  268. virtual void BindVertexShader( VertexShaderHandle_t hVertexShader ) = 0;
  269. virtual void BindGeometryShader( GeometryShaderHandle_t hGeometryShader ) = 0;
  270. virtual void BindPixelShader( PixelShaderHandle_t hPixelShader ) = 0;
  271. // Methods related to state objects
  272. virtual void SetRasterState( const ShaderRasterState_t& state ) = 0;
  273. //
  274. // NOTE: These methods have not yet been ported to DX10
  275. //
  276. // Sets the mode...
  277. virtual bool SetMode( void* hwnd, int nAdapter, const ShaderDeviceInfo_t &info ) = 0;
  278. virtual void ChangeVideoMode( const ShaderDeviceInfo_t &info ) = 0;
  279. // Returns the snapshot id for the shader state
  280. virtual StateSnapshot_t TakeSnapshot( ) = 0;
  281. virtual void TexMinFilter( ShaderTexFilterMode_t texFilterMode ) = 0;
  282. virtual void TexMagFilter( ShaderTexFilterMode_t texFilterMode ) = 0;
  283. virtual void TexWrap( ShaderTexCoordComponent_t coord, ShaderTexWrapMode_t wrapMode ) = 0;
  284. virtual void CopyRenderTargetToTexture( ShaderAPITextureHandle_t textureHandle ) = 0;
  285. // Binds a particular material to render with
  286. virtual void Bind( IMaterial* pMaterial ) = 0;
  287. // Gets the dynamic mesh; note that you've got to render the mesh
  288. // before calling this function a second time. Clients should *not*
  289. // call DestroyStaticMesh on the mesh returned by this call.
  290. virtual IMesh* GetDynamicMesh( IMaterial* pMaterial, int nHWSkinBoneCount, bool bBuffered = true,
  291. IMesh* pVertexOverride = 0, IMesh* pIndexOverride = 0) = 0;
  292. virtual IMesh* GetDynamicMeshEx( IMaterial* pMaterial, VertexFormat_t vertexFormat, int nHWSkinBoneCount,
  293. bool bBuffered = true, IMesh* pVertexOverride = 0, IMesh* pIndexOverride = 0 ) = 0;
  294. // Methods to ask about particular state snapshots
  295. virtual bool IsTranslucent( StateSnapshot_t id ) const = 0;
  296. virtual bool IsAlphaTested( StateSnapshot_t id ) const = 0;
  297. virtual bool UsesVertexAndPixelShaders( StateSnapshot_t id ) const = 0;
  298. virtual bool IsDepthWriteEnabled( StateSnapshot_t id ) const = 0;
  299. // Gets the vertex format for a set of snapshot ids
  300. virtual VertexFormat_t ComputeVertexFormat( int numSnapshots, StateSnapshot_t* pIds ) const = 0;
  301. // What fields in the vertex do we actually use?
  302. virtual VertexFormat_t ComputeVertexUsage( int numSnapshots, StateSnapshot_t* pIds ) const = 0;
  303. // Begins a rendering pass
  304. virtual void BeginPass( StateSnapshot_t snapshot ) = 0;
  305. // Renders a single pass of a material
  306. virtual void RenderPass( const unsigned char *pInstanceCommandBuffer, int nPass, int nPassCount ) = 0;
  307. // Set the number of bone weights
  308. virtual void SetNumBoneWeights( int numBones ) = 0;
  309. // Sets the lights
  310. virtual void SetLights( int nCount, const LightDesc_t *pDesc ) = 0;
  311. // Lighting origin for the current model
  312. virtual void SetLightingOrigin( Vector vLightingOrigin ) = 0;
  313. virtual void SetLightingState( const MaterialLightingState_t& state ) = 0;
  314. virtual void SetAmbientLightCube( Vector4D cube[6] ) = 0;
  315. // The shade mode
  316. virtual void ShadeMode( ShaderShadeMode_t mode ) = 0;
  317. // The cull mode
  318. virtual void CullMode( MaterialCullMode_t cullMode ) = 0;
  319. virtual void FlipCullMode( void ) = 0; //CW->CCW or CCW->CW, intended for mirror support where the view matrix is flipped horizontally
  320. virtual void BeginGeneratingCSMs()= 0;
  321. virtual void EndGeneratingCSMs() = 0;
  322. virtual void PerpareForCascadeDraw( int cascade, float fShadowSlopeScaleDepthBias, float fShadowDepthBias ) = 0;
  323. // Force writes only when z matches. . . useful for stenciling things out
  324. // by rendering the desired Z values ahead of time.
  325. virtual void ForceDepthFuncEquals( bool bEnable ) = 0;
  326. // Forces Z buffering to be on or off
  327. virtual void OverrideDepthEnable( bool bEnable, bool bDepthWriteEnable, bool bDepthTestEnable = true ) = 0;
  328. virtual void SetHeightClipZ( float z ) = 0;
  329. virtual void SetHeightClipMode( enum MaterialHeightClipMode_t heightClipMode ) = 0;
  330. virtual void SetClipPlane( int index, const float *pPlane ) = 0;
  331. virtual void EnableClipPlane( int index, bool bEnable ) = 0;
  332. // Returns the nearest supported format
  333. virtual ImageFormat GetNearestSupportedFormat( ImageFormat fmt, bool bFilteringRequired = true ) const = 0;
  334. virtual ImageFormat GetNearestRenderTargetFormat( ImageFormat fmt ) const = 0;
  335. // When AA is enabled, render targets are not AA and require a separate
  336. // depth buffer.
  337. virtual bool DoRenderTargetsNeedSeparateDepthBuffer() const = 0;
  338. // Texture management methods
  339. // For CreateTexture also see CreateTextures below
  340. virtual ShaderAPITextureHandle_t CreateTexture(
  341. int width,
  342. int height,
  343. int depth,
  344. ImageFormat dstImageFormat,
  345. int numMipLevels,
  346. int numCopies,
  347. int flags,
  348. const char *pDebugName,
  349. const char *pTextureGroupName ) = 0;
  350. virtual void DeleteTexture( ShaderAPITextureHandle_t textureHandle ) = 0;
  351. virtual ShaderAPITextureHandle_t CreateDepthTexture(
  352. ImageFormat renderTargetFormat,
  353. int width,
  354. int height,
  355. const char *pDebugName,
  356. bool bTexture,
  357. bool bAliasDepthSurfaceOverColorX360 = false ) = 0;
  358. virtual bool IsTexture( ShaderAPITextureHandle_t textureHandle ) = 0;
  359. virtual bool IsTextureResident( ShaderAPITextureHandle_t textureHandle ) = 0;
  360. // Indicates we're going to be modifying this texture
  361. // TexImage2D, TexSubImage2D, TexWrap, TexMinFilter, and TexMagFilter
  362. // all use the texture specified by this function.
  363. virtual void ModifyTexture( ShaderAPITextureHandle_t textureHandle ) = 0;
  364. virtual void TexImage2D(
  365. int level,
  366. int cubeFaceID,
  367. ImageFormat dstFormat,
  368. int zOffset,
  369. int width,
  370. int height,
  371. ImageFormat srcFormat,
  372. bool bSrcIsTiled, // NOTE: for X360 only
  373. void *imageData ) = 0;
  374. virtual void TexSubImage2D(
  375. int level,
  376. int cubeFaceID,
  377. int xOffset,
  378. int yOffset,
  379. int zOffset,
  380. int width,
  381. int height,
  382. ImageFormat srcFormat,
  383. int srcStride,
  384. bool bSrcIsTiled, // NOTE: for X360 only
  385. void *imageData ) = 0;
  386. // An alternate (and faster) way of writing image data
  387. // (locks the current Modify Texture). Use the pixel writer to write the data
  388. // after Lock is called
  389. // Doesn't work for compressed textures
  390. virtual bool TexLock( int level, int cubeFaceID, int xOffset, int yOffset,
  391. int width, int height, CPixelWriter& writer ) = 0;
  392. virtual void TexUnlock( ) = 0;
  393. // Copy sysmem surface to default pool surface asynchronously
  394. virtual void UpdateTexture( int xOffset, int yOffset, int w, int h, ShaderAPITextureHandle_t hDstTexture, ShaderAPITextureHandle_t hSrcTexture ) = 0;
  395. virtual void *LockTex( ShaderAPITextureHandle_t hTexture ) = 0;
  396. virtual void UnlockTex( ShaderAPITextureHandle_t hTexture ) = 0;
  397. // These are bound to the texture
  398. virtual void TexSetPriority( int priority ) = 0;
  399. // Sets the texture state
  400. virtual void BindTexture( Sampler_t sampler, TextureBindFlags_t nBindFlags, ShaderAPITextureHandle_t textureHandle ) = 0;
  401. // Set the render target to a texID.
  402. // Set to SHADER_RENDERTARGET_BACKBUFFER if you want to use the regular framebuffer.
  403. // Set to SHADER_RENDERTARGET_DEPTHBUFFER if you want to use the regular z buffer.
  404. virtual void SetRenderTarget( ShaderAPITextureHandle_t colorTextureHandle = SHADER_RENDERTARGET_BACKBUFFER,
  405. ShaderAPITextureHandle_t depthTextureHandle = SHADER_RENDERTARGET_DEPTHBUFFER ) = 0;
  406. // stuff that isn't to be used from within a shader
  407. virtual void ClearBuffersObeyStencil( bool bClearColor, bool bClearDepth ) = 0;
  408. virtual void ReadPixels( int x, int y, int width, int height, unsigned char *data, ImageFormat dstFormat, ITexture *pRenderTargetTexture = NULL ) = 0;
  409. virtual void ReadPixelsAsync( int x, int y, int width, int height, unsigned char *data, ImageFormat dstFormat, ITexture *pRenderTargetTexture = NULL, CThreadEvent *pPixelsReadEvent = NULL ) = 0;
  410. virtual void ReadPixelsAsyncGetResult( int x, int y, int width, int height, unsigned char *data, ImageFormat dstFormat, CThreadEvent *pGetResultEvent = NULL ) = 0;
  411. virtual void ReadPixels( Rect_t *pSrcRect, Rect_t *pDstRect, unsigned char *data, ImageFormat dstFormat, int nDstStride ) = 0;
  412. virtual void FlushHardware() = 0;
  413. // Use this to begin and end the frame
  414. virtual void BeginFrame() = 0;
  415. virtual void EndFrame() = 0;
  416. // Selection mode methods
  417. virtual int SelectionMode( bool selectionMode ) = 0;
  418. virtual void SelectionBuffer( unsigned int* pBuffer, int size ) = 0;
  419. virtual void ClearSelectionNames( ) = 0;
  420. virtual void LoadSelectionName( int name ) = 0;
  421. virtual void PushSelectionName( int name ) = 0;
  422. virtual void PopSelectionName() = 0;
  423. // Force the hardware to finish whatever it's doing
  424. virtual void ForceHardwareSync() = 0;
  425. // Used to clear the transition table when we know it's become invalid.
  426. virtual void ClearSnapshots() = 0;
  427. virtual void FogStart( float fStart ) = 0;
  428. virtual void FogEnd( float fEnd ) = 0;
  429. virtual void SetFogZ( float fogZ ) = 0;
  430. // Scene fog state.
  431. virtual void SceneFogColor3ub( unsigned char r, unsigned char g, unsigned char b ) = 0;
  432. virtual void GetSceneFogColor( unsigned char *rgb ) = 0;
  433. virtual void SceneFogMode( MaterialFogMode_t fogMode ) = 0;
  434. // Can we download textures?
  435. virtual bool CanDownloadTextures() const = 0;
  436. virtual void ResetRenderState( bool bFullReset = true ) = 0;
  437. // We use smaller dynamic VBs during level transitions, to free up memory
  438. virtual int GetCurrentDynamicVBSize( void ) = 0;
  439. virtual void DestroyVertexBuffers( bool bExitingLevel = false ) = 0;
  440. virtual void EvictManagedResources() = 0;
  441. // Get stats on GPU memory usage
  442. virtual void GetGPUMemoryStats( GPUMemoryStats &stats ) = 0;
  443. // Level of anisotropic filtering
  444. virtual void SetAnisotropicLevel( int nAnisotropyLevel ) = 0;
  445. // For debugging and building recording files. This will stuff a token into the recording file,
  446. // then someone doing a playback can watch for the token.
  447. virtual void SyncToken( const char *pToken ) = 0;
  448. // Setup standard vertex shader constants (that don't change)
  449. // This needs to be called anytime that overbright changes.
  450. virtual void SetStandardVertexShaderConstants( float fOverbright ) = 0;
  451. //
  452. // Occlusion query support
  453. //
  454. // Allocate and delete query objects.
  455. virtual ShaderAPIOcclusionQuery_t CreateOcclusionQueryObject( void ) = 0;
  456. virtual void DestroyOcclusionQueryObject( ShaderAPIOcclusionQuery_t ) = 0;
  457. // Bracket drawing with begin and end so that we can get counts next frame.
  458. virtual void BeginOcclusionQueryDrawing( ShaderAPIOcclusionQuery_t ) = 0;
  459. virtual void EndOcclusionQueryDrawing( ShaderAPIOcclusionQuery_t ) = 0;
  460. // OcclusionQuery_GetNumPixelsRendered
  461. // Get the number of pixels rendered between begin and end on an earlier frame.
  462. // Calling this in the same frame is a huge perf hit!
  463. // Returns iQueryResult:
  464. // iQueryResult >= 0 - iQueryResult is the number of pixels rendered
  465. // OCCLUSION_QUERY_RESULT_PENDING - query results are not available yet
  466. // OCCLUSION_QUERY_RESULT_ERROR - query failed
  467. // Use OCCLUSION_QUERY_FINISHED( iQueryResult ) to test if query finished.
  468. virtual int OcclusionQuery_GetNumPixelsRendered( ShaderAPIOcclusionQuery_t hQuery, bool bFlush = false ) = 0;
  469. virtual void SetFlashlightState( const FlashlightState_t &state, const VMatrix &worldToTexture ) = 0;
  470. virtual bool IsCascadedShadowMapping() const = 0;
  471. virtual void SetCascadedShadowMappingState( const CascadedShadowMappingState_t &state, ITexture *pDepthTextureAtlas ) = 0;
  472. virtual const CascadedShadowMappingState_t &GetCascadedShadowMappingState( ITexture **pDepthTextureAtlas, bool bLightMapScale = false ) const = 0;
  473. virtual void ClearVertexAndPixelShaderRefCounts() = 0;
  474. virtual void PurgeUnusedVertexAndPixelShaders() = 0;
  475. // Called when the dx support level has changed
  476. virtual void DXSupportLevelChanged( int nDXLevel ) = 0;
  477. // By default, the material system applies the VIEW and PROJECTION matrices to the user clip
  478. // planes (which are specified in world space) to generate projection-space user clip planes
  479. // Occasionally (for the particle system in hl2, for example), we want to override that
  480. // behavior and explictly specify a View transform for user clip planes. The PROJECTION
  481. // will be mutliplied against this instead of the normal VIEW matrix.
  482. virtual void EnableUserClipTransformOverride( bool bEnable ) = 0;
  483. virtual void UserClipTransform( const VMatrix &worldToView ) = 0;
  484. // ----------------------------------------------------------------------------------
  485. // Everything after this point added after HL2 shipped.
  486. // ----------------------------------------------------------------------------------
  487. // Set the render target to a texID.
  488. // Set to SHADER_RENDERTARGET_BACKBUFFER if you want to use the regular framebuffer.
  489. // Set to SHADER_RENDERTARGET_DEPTHBUFFER if you want to use the regular z buffer.
  490. virtual void SetRenderTargetEx( int nRenderTargetID,
  491. ShaderAPITextureHandle_t colorTextureHandle = SHADER_RENDERTARGET_BACKBUFFER,
  492. ShaderAPITextureHandle_t depthTextureHandle = SHADER_RENDERTARGET_DEPTHBUFFER ) = 0;
  493. virtual void CopyRenderTargetToTextureEx( ShaderAPITextureHandle_t textureHandle, int nRenderTargetID, Rect_t *pSrcRect = NULL, Rect_t *pDstRect = NULL ) = 0;
  494. virtual void CopyTextureToRenderTargetEx( int nRenderTargetID, ShaderAPITextureHandle_t textureHandle, Rect_t *pSrcRect = NULL, Rect_t *pDstRect = NULL ) = 0;
  495. // For dealing with device lost in cases where SwapBuffers isn't called all the time (Hammer)
  496. virtual void HandleDeviceLost() = 0;
  497. virtual void EnableLinearColorSpaceFrameBuffer( bool bEnable ) = 0;
  498. // Lets the shader know about the full-screen texture so it can
  499. virtual void SetFullScreenTextureHandle( ShaderAPITextureHandle_t h ) = 0;
  500. // Rendering parameters control special drawing modes withing the material system, shader
  501. // system, shaders, and engine. renderparm.h has their definitions.
  502. virtual void SetFloatRenderingParameter(int parm_number, float value) = 0;
  503. virtual void SetIntRenderingParameter(int parm_number, int value) = 0;
  504. virtual void SetVectorRenderingParameter(int parm_number, Vector const &value) = 0;
  505. virtual float GetFloatRenderingParameter(int parm_number) const = 0;
  506. virtual int GetIntRenderingParameter(int parm_number) const = 0;
  507. virtual Vector GetVectorRenderingParameter(int parm_number) const = 0;
  508. virtual void SetFastClipPlane( const float *pPlane ) = 0;
  509. virtual void EnableFastClip( bool bEnable ) = 0;
  510. // Returns the number of vertices + indices we can render using the dynamic mesh
  511. // Passing true in the second parameter will return the max # of vertices + indices
  512. // we can use before a flush is provoked and may return different values
  513. // if called multiple times in succession.
  514. // Passing false into the second parameter will return
  515. // the maximum possible vertices + indices that can be rendered in a single batch
  516. virtual void GetMaxToRender( IMesh *pMesh, bool bMaxUntilFlush, int *pMaxVerts, int *pMaxIndices ) = 0;
  517. // Returns the max number of vertices we can render for a given material
  518. virtual int GetMaxVerticesToRender( IMaterial *pMaterial ) = 0;
  519. virtual int GetMaxIndicesToRender( ) = 0;
  520. // stencil methods
  521. virtual void SetStencilState( const ShaderStencilState_t& state ) = 0;
  522. virtual void ClearStencilBufferRectangle(int xmin, int ymin, int xmax, int ymax, int value) = 0;
  523. // disables all local lights
  524. virtual void DisableAllLocalLights() = 0;
  525. virtual int CompareSnapshots( StateSnapshot_t snapshot0, StateSnapshot_t snapshot1 ) = 0;
  526. virtual IMesh *GetFlexMesh() = 0;
  527. virtual void SetFlashlightStateEx( const FlashlightState_t &state, const VMatrix &worldToTexture, ITexture *pFlashlightDepthTexture ) = 0;
  528. virtual bool SupportsMSAAMode( int nMSAAMode ) = 0;
  529. #if defined( _GAMECONSOLE )
  530. virtual bool PostQueuedTexture( const void *pData, int nSize, ShaderAPITextureHandle_t *pHandles, int nHandles, int nWidth, int nHeight, int nDepth, int nMips, int *pRefCount ) = 0;
  531. #endif // _GAMECONSOLE
  532. #if defined( _X360 )
  533. virtual HXUIFONT OpenTrueTypeFont( const char *pFontname, int tall, int style ) = 0;
  534. virtual void CloseTrueTypeFont( HXUIFONT hFont ) = 0;
  535. virtual bool GetTrueTypeFontMetrics( HXUIFONT hFont, wchar_t wchFirst, wchar_t wchLast, XUIFontMetrics *pFontMetrics, XUICharMetrics *pCharMetrics ) = 0;
  536. // Render a sequence of characters and extract the data into a buffer
  537. // For each character, provide the width+height of the font texture subrect,
  538. // an offset to apply when rendering the glyph, and an offset into a buffer to receive the RGBA data
  539. virtual bool GetTrueTypeGlyphs( HXUIFONT hFont, int numChars, wchar_t *pWch, int *pOffsetX, int *pOffsetY, int *pWidth, int *pHeight, unsigned char *pRGBA, int *pRGBAOffset ) = 0;
  540. virtual ShaderAPITextureHandle_t CreateRenderTargetSurface( int width, int height, ImageFormat format, RTMultiSampleCount360_t multiSampleCount, const char *pDebugName, const char *pTextureGroupName ) = 0;
  541. virtual void PersistDisplay() = 0;
  542. virtual void *GetD3DDevice() = 0;
  543. virtual void PushVertexShaderGPRAllocation( int iVertexShaderCount = 64 ) = 0;
  544. virtual void PopVertexShaderGPRAllocation( void ) = 0;
  545. // 360 allows us to bypass vsync blocking up to 60 fps without creating a new device
  546. virtual void EnableVSync_360( bool bEnable ) = 0;
  547. virtual void SetCacheableTextureParams( ShaderAPITextureHandle_t *pHandles, int count, const char *pFilename, int mipSkipCount ) = 0;
  548. virtual void FlushHiStencil() = 0;
  549. #endif
  550. #if defined( _GAMECONSOLE )
  551. virtual void BeginConsoleZPass2( int nNumDynamicIndicesNeeded ) = 0;
  552. virtual void EndConsoleZPass() = 0;
  553. virtual unsigned int GetConsoleZPassCounter() const = 0;
  554. #endif
  555. #if defined( _PS3 )
  556. virtual void FlushTextureCache() = 0;
  557. #endif
  558. virtual void AntiAliasingHint( int nHint ) = 0;
  559. virtual bool OwnGPUResources( bool bEnable ) = 0;
  560. //get fog distances entered with FogStart(), FogEnd(), and SetFogZ()
  561. virtual void GetFogDistances( float *fStart, float *fEnd, float *fFogZ ) = 0;
  562. // Hooks for firing PIX events from outside the Material System...
  563. virtual void BeginPIXEvent( unsigned long color, const char *szName ) = 0;
  564. virtual void EndPIXEvent() = 0;
  565. virtual void SetPIXMarker( unsigned long color, const char *szName ) = 0;
  566. // Enables and disables for Alpha To Coverage
  567. virtual void EnableAlphaToCoverage() = 0;
  568. virtual void DisableAlphaToCoverage() = 0;
  569. // Computes the vertex buffer pointers
  570. virtual void ComputeVertexDescription( unsigned char* pBuffer, VertexFormat_t vertexFormat, MeshDesc_t& desc ) const = 0;
  571. virtual int VertexFormatSize( VertexFormat_t vertexFormat ) const = 0;
  572. virtual void SetDisallowAccess( bool ) = 0;
  573. virtual void EnableShaderShaderMutex( bool ) = 0;
  574. virtual void ShaderLock() = 0;
  575. virtual void ShaderUnlock() = 0;
  576. virtual void SetShadowDepthBiasFactors( float fShadowSlopeScaleDepthBias, float fShadowDepthBias ) = 0;
  577. // ------------ New Vertex/Index Buffer interface ----------------------------
  578. virtual void BindVertexBuffer( int nStreamID, IVertexBuffer *pVertexBuffer, int nOffsetInBytes, int nFirstVertex, int nVertexCount, VertexFormat_t fmt, int nRepetitions = 1 ) = 0;
  579. virtual void BindIndexBuffer( IIndexBuffer *pIndexBuffer, int nOffsetInBytes ) = 0;
  580. virtual void Draw( MaterialPrimitiveType_t primitiveType, int nFirstIndex, int nIndexCount ) = 0;
  581. // ------------ End ----------------------------
  582. // Apply stencil operations to every pixel on the screen without disturbing depth or color buffers
  583. virtual void PerformFullScreenStencilOperation( void ) = 0;
  584. virtual void SetScissorRect( const int nLeft, const int nTop, const int nRight, const int nBottom, const bool bEnableScissor ) = 0;
  585. // nVidia CSAA modes, different from SupportsMSAAMode()
  586. virtual bool SupportsCSAAMode( int nNumSamples, int nQualityLevel ) = 0;
  587. //Notifies the shaderapi to invalidate the current set of delayed constants because we just finished a draw pass. Either actual or not.
  588. virtual void InvalidateDelayedShaderConstants( void ) = 0;
  589. // Gamma<->Linear conversions according to the video hardware we're running on
  590. virtual float GammaToLinear_HardwareSpecific( float fGamma ) const =0;
  591. virtual float LinearToGamma_HardwareSpecific( float fLinear ) const =0;
  592. //Set's the linear->gamma conversion textures to use for this hardware for both srgb writes enabled and disabled(identity)
  593. virtual void SetLinearToGammaConversionTextures( ShaderAPITextureHandle_t hSRGBWriteEnabledTexture, ShaderAPITextureHandle_t hIdentityTexture ) = 0;
  594. virtual void BindVertexTexture( VertexTextureSampler_t nSampler, ShaderAPITextureHandle_t textureHandle ) = 0;
  595. // Enables hardware morphing
  596. virtual void EnableHWMorphing( bool bEnable ) = 0;
  597. // Sets flexweights for rendering
  598. virtual void SetFlexWeights( int nFirstWeight, int nCount, const MorphWeight_t* pWeights ) = 0;
  599. virtual void FogMaxDensity( float flMaxDensity ) = 0;
  600. virtual void *GetD3DTexturePtr( ShaderAPITextureHandle_t hTexture ) = 0;
  601. #ifdef _PS3
  602. virtual void GetPs3Texture(void* tex, ShaderAPITextureHandle_t hTexture ) = 0;
  603. virtual void GetPs3Texture(void* tex, StandardTextureId_t nTextureId ) = 0;
  604. #endif
  605. virtual bool IsStandardTextureHandleValid( StandardTextureId_t textureId ) = 0;
  606. // Create a multi-frame texture (equivalent to calling "CreateTexture" multiple times, but more efficient)
  607. virtual void CreateTextures(
  608. ShaderAPITextureHandle_t *pHandles,
  609. int count,
  610. int width,
  611. int height,
  612. int depth,
  613. ImageFormat dstImageFormat,
  614. int numMipLevels,
  615. int numCopies,
  616. int flags,
  617. const char *pDebugName,
  618. const char *pTextureGroupName ) = 0;
  619. virtual void AcquireThreadOwnership() = 0;
  620. virtual void ReleaseThreadOwnership() = 0;
  621. // Only does anything on XBox360. This is useful to eliminate stalls
  622. virtual void EnableBuffer2FramesAhead( bool bEnable ) = 0;
  623. virtual void FlipCulling( bool bFlipCulling ) = 0;
  624. virtual void SetTextureRenderingParameter(int parm_number, ITexture *pTexture) = 0;
  625. //only actually sets a bool that can be read in from shaders, doesn't do any of the legwork
  626. virtual void EnableSinglePassFlashlightMode( bool bEnable ) = 0;
  627. // stuff related to matrix stacks
  628. virtual void MatrixMode( MaterialMatrixMode_t matrixMode ) = 0;
  629. virtual void PushMatrix() = 0;
  630. virtual void PopMatrix() = 0;
  631. virtual void LoadMatrix( float *m ) = 0;
  632. virtual void MultMatrix( float *m ) = 0;
  633. virtual void MultMatrixLocal( float *m ) = 0;
  634. virtual void LoadIdentity( void ) = 0;
  635. virtual void LoadCameraToWorld( void ) = 0;
  636. virtual void Ortho( double left, double right, double bottom, double top, double zNear, double zFar ) = 0;
  637. virtual void PerspectiveX( double fovx, double aspect, double zNear, double zFar ) = 0;
  638. virtual void PickMatrix( int x, int y, int width, int height ) = 0;
  639. virtual void Rotate( float angle, float x, float y, float z ) = 0;
  640. virtual void Translate( float x, float y, float z ) = 0;
  641. virtual void Scale( float x, float y, float z ) = 0;
  642. virtual void ScaleXY( float x, float y ) = 0;
  643. virtual void PerspectiveOffCenterX( double fovx, double aspect, double zNear, double zFar, double bottom, double top, double left, double right ) = 0;
  644. virtual void LoadBoneMatrix( int boneIndex, const float *m ) = 0;
  645. // interface for mat system to tell shaderapi about standard texture handles
  646. virtual void SetStandardTextureHandle( StandardTextureId_t nId, ShaderAPITextureHandle_t nHandle ) =0;
  647. virtual void DrawInstances( int nInstanceCount, const MeshInstanceData_t *pInstance ) = 0;
  648. // Allows us to override the color/alpha write settings of a material
  649. virtual void OverrideAlphaWriteEnable( bool bOverrideEnable, bool bAlphaWriteEnable ) = 0;
  650. virtual void OverrideColorWriteEnable( bool bOverrideEnable, bool bColorWriteEnable ) = 0;
  651. //extended clear buffers function with alpha independent from color
  652. virtual void ClearBuffersObeyStencilEx( bool bClearColor, bool bClearAlpha, bool bClearDepth ) = 0;
  653. virtual void OnPresent( void ) = 0;
  654. virtual void UpdateGameTime( float flTime ) = 0;
  655. #ifdef _GAMECONSOLE
  656. // Backdoor used by the queued context to directly use write-combined memory
  657. virtual IMesh *GetExternalMesh( const ExternalMeshInfo_t& info ) = 0;
  658. virtual void SetExternalMeshData( IMesh *pMesh, const ExternalMeshData_t &data ) = 0;
  659. virtual IIndexBuffer *GetExternalIndexBuffer( int nIndexCount, uint16 *pIndexData ) = 0;
  660. virtual void FlushGPUCache( void *pBaseAddr, size_t nSizeInBytes ) = 0;
  661. #endif
  662. virtual bool IsStereoSupported() const = 0;
  663. virtual void UpdateStereoTexture( ShaderAPITextureHandle_t texHandle, bool *pStereoActiveThisFrame ) = 0;
  664. virtual void SetSRGBWrite( bool bState ) = 0;
  665. // debug logging
  666. // only implemented in some subclasses
  667. virtual void PrintfVA( char *fmt, va_list vargs ) = 0;
  668. virtual void Printf( char *fmt, ... ) = 0;
  669. virtual float Knob( char *knobname, float *setvalue = NULL ) = 0;
  670. virtual void AddShaderComboInformation( const ShaderComboSemantics_t *pSemantics ) = 0;
  671. virtual float GetLightMapScaleFactor() const = 0;
  672. virtual ShaderAPITextureHandle_t FindTexture( const char *pDebugName ) = 0;
  673. virtual void GetTextureDimensions( ShaderAPITextureHandle_t hTexture, int &nWidth, int &nHeight, int &nDepth ) = 0;
  674. virtual ShaderAPITextureHandle_t GetStandardTextureHandle(StandardTextureId_t id) = 0;
  675. };
  676. #endif // ISHADERAPI_H