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.

344 lines
9.8 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef TRANSITION_TABLE_H
  9. #define TRANSITION_TABLE_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "utlvector.h"
  14. #include "shadershadowdx8.h"
  15. #include "utlsortvector.h"
  16. #include "checksum_crc.h"
  17. #include "shaderapi/ishaderapi.h"
  18. // Required for DEBUG_BOARD_STATE
  19. #include "shaderapidx8_global.h"
  20. //-----------------------------------------------------------------------------
  21. // Forward declarations
  22. //-----------------------------------------------------------------------------
  23. struct IDirect3DStateBlock9;
  24. //-----------------------------------------------------------------------------
  25. // Types related to transition table entries
  26. //-----------------------------------------------------------------------------
  27. typedef void (*ApplyStateFunc_t)( const ShadowState_t& shadowState, int arg );
  28. //-----------------------------------------------------------------------------
  29. // The DX8 implementation of the transition table
  30. //-----------------------------------------------------------------------------
  31. class CTransitionTable
  32. {
  33. public:
  34. struct CurrentSamplerState_t
  35. {
  36. bool m_SRGBReadEnable;
  37. bool m_Fetch4Enable;
  38. bool m_ShadowFilterEnable;
  39. };
  40. struct CurrentState_t
  41. {
  42. // Everything in this 'CurrentState' structure is a state whose value we don't care about
  43. // under certain circumstances, (which therefore can diverge from the shadow state),
  44. // or states which we override in the dynamic pass.
  45. // Depth testing states
  46. union
  47. {
  48. DepthTestState_t m_DepthTestState;
  49. DepthTestState_t::UIntAlias m_nDepthTestStateAsInt;
  50. };
  51. // alpha testijng and misc state
  52. union
  53. {
  54. AlphaTestAndMiscState_t m_AlphaTestAndMiscState;
  55. AlphaTestAndMiscState_t::UIntAlias m_nAlphaTestAndMiscStateAsInt;
  56. };
  57. union
  58. {
  59. FogAndMiscState_t m_FogAndMiscState;
  60. FogAndMiscState_t::UIntAlias m_nFogAndMiscStateAsInt;
  61. };
  62. union
  63. {
  64. AlphaBlendState_t m_AlphaBlendState;
  65. AlphaBlendState_t::UIntAlias m_nAlphaBlendStateAsInt;
  66. };
  67. bool m_ForceDepthFuncEquals:1;
  68. bool m_bOverrideDepthEnable:1;
  69. bool m_bOverrideAlphaWriteEnable:1;
  70. bool m_bOverrideColorWriteEnable:1;
  71. bool m_bOverriddenAlphaWriteValue:1;
  72. bool m_bOverriddenColorWriteValue:1;
  73. DWORD m_ColorWriteEnable;
  74. bool m_OverrideZWriteEnable;
  75. bool m_OverrideZTestEnable;
  76. bool m_bLinearColorSpaceFrameBufferEnable;
  77. // Texture stage state
  78. CurrentSamplerState_t m_SamplerState[MAX_SAMPLERS];
  79. };
  80. public:
  81. // constructor, destructor
  82. CTransitionTable( );
  83. virtual ~CTransitionTable();
  84. // Initialization, shutdown
  85. bool Init( );
  86. void Shutdown( );
  87. // Resets the snapshots...
  88. void Reset();
  89. // Takes a snapshot
  90. StateSnapshot_t TakeSnapshot( );
  91. // Take startup snapshot
  92. void TakeDefaultStateSnapshot( );
  93. ShadowShaderState_t *GetShaderShadowState( StateSnapshot_t snapshotId )
  94. {
  95. return &m_SnapshotList[snapshotId].m_ShaderState;
  96. }
  97. // Makes the board state match the snapshot
  98. void UseSnapshot( StateSnapshot_t snapshotId, bool bApplyShaderState = true );
  99. // Cause the board to match the default state snapshot
  100. void UseDefaultState();
  101. // Snapshotted state overrides
  102. void ForceDepthFuncEquals( bool bEnable );
  103. void OverrideDepthEnable( bool bEnable, bool bDepthWriteEnable, bool bDepthTestEnable );
  104. void OverrideAlphaWriteEnable( bool bOverrideEnable, bool bAlphaWriteEnable );
  105. void OverrideColorWriteEnable( bool bOverrideEnable, bool bColorWriteEnable );
  106. void EnableLinearColorSpaceFrameBuffer( bool bEnable );
  107. // Returns a particular snapshot
  108. const ShadowState_t &GetSnapshot( StateSnapshot_t snapshotId ) const;
  109. const ShadowShaderState_t &GetSnapshotShader( StateSnapshot_t snapshotId ) const;
  110. // Gets the current shadow state
  111. const ShadowState_t *CurrentShadowState() const;
  112. const ShadowShaderState_t *CurrentShadowShaderState() const;
  113. // Return the current shapshot
  114. int CurrentSnapshot() const { return m_CurrentSnapshotId; }
  115. CurrentState_t& CurrentState() { return m_CurrentState; }
  116. void SetShadowDepthBiasValuesDirty( bool bDirty ) { m_bShadowDepthBiasValuesDirty = bDirty; }
  117. #ifdef DEBUG_BOARD_STATE
  118. ShadowState_t& BoardState() { return m_BoardState; }
  119. ShadowShaderState_t& BoardShaderState() { return m_BoardShaderState; }
  120. #endif
  121. // The following are meant to be used by the transition table only
  122. public:
  123. // Applies alpha blending
  124. void ApplyAlphaBlend( const ShadowState_t& state );
  125. // Separate alpha blend
  126. void ApplySeparateAlphaBlend( const ShadowState_t& state );
  127. void ApplyAlphaTest( const ShadowState_t& state );
  128. void ApplyDepthTest( const ShadowState_t& state );
  129. // Applies alpha texture op
  130. void ApplyColorTextureStage( const ShadowState_t& state, int stage );
  131. void ApplyAlphaTextureStage( const ShadowState_t& state, int stage );
  132. void ApplySRGBWriteEnable( const ShadowState_t& state );
  133. private:
  134. enum
  135. {
  136. INVALID_TRANSITION_OP = 0xFFFFFF
  137. };
  138. typedef short ShadowStateId_t;
  139. // For the transition table
  140. struct TransitionList_t
  141. {
  142. unsigned int m_FirstOperation : 24;
  143. unsigned int m_NumOperations : 8;
  144. };
  145. union TransitionOp_t
  146. {
  147. unsigned char m_nBits;
  148. struct
  149. {
  150. unsigned char m_nOpCode : 7;
  151. unsigned char m_bIsTextureCode : 1;
  152. } m_nInfo;
  153. };
  154. struct SnapshotShaderState_t
  155. {
  156. ShadowShaderState_t m_ShaderState;
  157. ShadowStateId_t m_ShadowStateId;
  158. unsigned short m_nReserved; // Pad to 2 ints
  159. unsigned int m_nReserved2;
  160. };
  161. struct ShadowStateDictEntry_t
  162. {
  163. CRC32_t m_nChecksum;
  164. ShadowStateId_t m_nShadowStateId;
  165. };
  166. struct SnapshotDictEntry_t
  167. {
  168. CRC32_t m_nChecksum;
  169. StateSnapshot_t m_nSnapshot;
  170. };
  171. class ShadowStateDictLessFunc
  172. {
  173. public:
  174. bool Less( const ShadowStateDictEntry_t &src1, const ShadowStateDictEntry_t &src2, void *pCtx );
  175. };
  176. class SnapshotDictLessFunc
  177. {
  178. public:
  179. bool Less( const SnapshotDictEntry_t &src1, const SnapshotDictEntry_t &src2, void *pCtx );
  180. };
  181. class UniqueSnapshotLessFunc
  182. {
  183. public:
  184. bool Less( const TransitionList_t &src1, const TransitionList_t &src2, void *pCtx );
  185. };
  186. CurrentSamplerState_t &SamplerState( int stage ) { return m_CurrentState.m_SamplerState[stage]; }
  187. const CurrentSamplerState_t &SamplerState( int stage ) const { return m_CurrentState.m_SamplerState[stage]; }
  188. // creates state snapshots
  189. ShadowStateId_t CreateShadowState( const ShadowState_t &currentState );
  190. StateSnapshot_t CreateStateSnapshot( ShadowStateId_t shadowStateId, const ShadowShaderState_t& currentShaderState );
  191. // finds state snapshots
  192. ShadowStateId_t FindShadowState( const ShadowState_t& currentState ) const;
  193. StateSnapshot_t FindStateSnapshot( ShadowStateId_t id, const ShadowShaderState_t& currentState ) const;
  194. // Finds identical transition lists
  195. unsigned int FindIdenticalTransitionList( unsigned int firstElem,
  196. unsigned short numOps, unsigned int nFirstTest ) const;
  197. // Checks if a state is valid
  198. bool TestShadowState( const ShadowState_t& state, const ShadowShaderState_t &shaderState );
  199. // Perform state block overrides
  200. void PerformShadowStateOverrides( );
  201. // Apply shader state (stuff that doesn't lie in the transition table)
  202. void ApplyShaderState( const ShadowState_t &shadowState, const ShadowShaderState_t &shaderState );
  203. // State setting methods
  204. void SetZEnable( D3DZBUFFERTYPE nEnable );
  205. void SetZFunc( D3DCMPFUNC nCmpFunc );
  206. void SetBoardStateFromShadowState( ShadowState_t const &shadowState );
  207. private:
  208. // Sets up the default state
  209. StateSnapshot_t m_DefaultStateSnapshot;
  210. TransitionList_t m_DefaultTransition;
  211. ShadowState_t m_DefaultShadowState;
  212. // The current snapshot id
  213. ShadowStateId_t m_CurrentShadowId;
  214. StateSnapshot_t m_CurrentSnapshotId;
  215. // Maintains a list of all used snapshot transition states
  216. CUtlVector< ShadowState_t > m_ShadowStateList;
  217. // Lookup table for fast snapshot finding
  218. CUtlSortVector< ShadowStateDictEntry_t, ShadowStateDictLessFunc > m_ShadowStateDict;
  219. // The snapshot transition table
  220. CUtlVector< CUtlVector< TransitionList_t > > m_TransitionTable;
  221. // List of unique transitions
  222. CUtlSortVector< TransitionList_t, UniqueSnapshotLessFunc > m_UniqueTransitions;
  223. // Stores all state transition operations
  224. CUtlVector< TransitionOp_t > m_TransitionOps;
  225. // Stores all state for a particular snapshot
  226. CUtlVector< SnapshotShaderState_t > m_SnapshotList;
  227. // Lookup table for fast snapshot finding
  228. CUtlSortVector< SnapshotDictEntry_t, SnapshotDictLessFunc > m_SnapshotDict;
  229. // The current board state.
  230. CurrentState_t m_CurrentState;
  231. bool m_bShadowDepthBiasValuesDirty;
  232. #ifdef DEBUG_BOARD_STATE
  233. // Maintains the total shadow state
  234. ShadowState_t m_BoardState;
  235. ShadowShaderState_t m_BoardShaderState;
  236. #endif
  237. };
  238. //-----------------------------------------------------------------------------
  239. // Inline methods
  240. //-----------------------------------------------------------------------------
  241. inline const ShadowState_t &CTransitionTable::GetSnapshot( StateSnapshot_t snapshotId ) const
  242. {
  243. Assert( (snapshotId >= 0) && (snapshotId < m_SnapshotList.Count()) );
  244. return m_ShadowStateList[m_SnapshotList[snapshotId].m_ShadowStateId];
  245. }
  246. inline const ShadowShaderState_t &CTransitionTable::GetSnapshotShader( StateSnapshot_t snapshotId ) const
  247. {
  248. Assert( (snapshotId >= 0) && (snapshotId < m_SnapshotList.Count()) );
  249. return m_SnapshotList[snapshotId].m_ShaderState;
  250. }
  251. inline const ShadowState_t *CTransitionTable::CurrentShadowState() const
  252. {
  253. if ( m_CurrentShadowId == -1 )
  254. return NULL;
  255. Assert( (m_CurrentShadowId >= 0) && (m_CurrentShadowId < m_ShadowStateList.Count()) );
  256. return &m_ShadowStateList[m_CurrentShadowId];
  257. }
  258. inline const ShadowShaderState_t *CTransitionTable::CurrentShadowShaderState() const
  259. {
  260. if ( m_CurrentShadowId == -1 )
  261. return NULL;
  262. Assert( (m_CurrentShadowId >= 0) && (m_CurrentShadowId < m_ShadowStateList.Count()) );
  263. return &m_SnapshotList[m_CurrentShadowId].m_ShaderState;
  264. }
  265. #endif // TRANSITION_TABLE_H