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.

271 lines
9.3 KiB

  1. //================ Copyright (c) Valve Corporation. All Rights Reserved. ===========================//================ Copyright (c) Valve Corporation. All Rights Reserved. ===========================
  2. //
  3. // Gcm renderer state and util functions
  4. //
  5. //==================================================================================================
  6. #ifndef INCLUDED_GCMSTATE_H
  7. #define INCLUDED_GCMSTATE_H
  8. #ifndef SPU
  9. #include "tier0/platform.h"
  10. #include "tier0/dbg.h"
  11. #include "cell\gcm.h"
  12. #include "gcmconfig.h"
  13. #include "ps3gcmmemory.h"
  14. #include "dxabstract_def.h"
  15. #include "spumgr_ppu.h"
  16. #else
  17. #include "spumgr_spu.h"
  18. #endif
  19. //--------------------------------------------------------------------------------------------------
  20. // Misc
  21. //--------------------------------------------------------------------------------------------------
  22. template <typename T>
  23. inline T Min( T a, T b )
  24. {
  25. return a < b ? a : b;
  26. }
  27. template <typename T>
  28. inline T Max( T a, T b )
  29. {
  30. return a > b ? a : b;
  31. }
  32. template <typename T>
  33. inline void Swap( T& a , T & b )
  34. {
  35. T c = a; a = b; b = c;
  36. }
  37. //--------------------------------------------------------------------------------------------------
  38. // Literals
  39. //--------------------------------------------------------------------------------------------------
  40. // IO Memory (page isze is 1MB, so make these add up to 1MB
  41. #define GCM_MAINPOOLSIZE (0 * 0x100000) // IO memory for main pool
  42. #define GCM_DEFCMDBUFFSIZE (1 * 0x200000) // Default command buff (must be pow 2)
  43. #define GCM_CALLCMDBUFFSIZE (2 * 0x10000) // 256 K of cmd buffer to call to
  44. // Used for DrawprimUP
  45. #define GCM_CALLCMDSEGSIZE 0x8000 // 32K segmentation
  46. #define GCM_PATCHBUFFSIZE ((2 * 0x100000) - GCM_CALLCMDBUFFSIZE)
  47. #define GCM_PATCHSEGSIZE 0x8000
  48. #define GCM_IOSIZE (GCM_MAINPOOLSIZE + GCM_DEFCMDBUFFSIZE + GCM_CALLCMDBUFFSIZE + GCM_PATCHBUFFSIZE)
  49. //--------------------------------------------------------------------------------------------------
  50. // Display Structure
  51. //--------------------------------------------------------------------------------------------------
  52. struct CPs3gcmDisplay
  53. {
  54. uint32 surfaceFlipIdx; // which scanout color buffer will be presented with flip
  55. enum EnumConst_t { SURFACE_COUNT = 2 };
  56. CPs3gcmLocalMemoryBlockSystemGlobal surfaceColor[SURFACE_COUNT]; // scanout color buffers for double-buffering
  57. // (need one more to avoid overwriting old buffer)
  58. CPs3gcmLocalMemoryBlockSystemGlobal surfaceDepth; // depth buffer
  59. void Flip()
  60. {
  61. surfaceFlipIdx = NextSurfaceIndex();
  62. }
  63. uint NextSurfaceIndex( int nFrame = 1 )const
  64. {
  65. return ( surfaceFlipIdx + nFrame ) % SURFACE_COUNT;
  66. }
  67. uint PrevSurfaceIndex( int nFrame )const
  68. {
  69. int nResult = int( surfaceFlipIdx + 1000000 * SURFACE_COUNT - nFrame ) % int( SURFACE_COUNT );
  70. Assert( uint( nResult ) < SURFACE_COUNT ); // if this is negative, it means we did ( ( something ) mod 2 ) mod 3, which makes no sense in this context
  71. return uint( nResult );
  72. }
  73. };
  74. //--------------------------------------------------------------------------------------------------
  75. // Global GCM state class
  76. //--------------------------------------------------------------------------------------------------
  77. struct CPs3gcmGlobalState
  78. {
  79. //--------------------------------------------------------------------------------------------------
  80. // Memory
  81. // RSX Local, plus one block of memory mapped into RSX (IO mem)
  82. // Main memory pool is within the IO mem and is used for textures until it fills...
  83. //--------------------------------------------------------------------------------------------------
  84. // RSX local memory
  85. void * m_pLocalBaseAddress; // RSX Local Memory Base Address
  86. uint32 m_nLocalBaseOffset; // cellGcmAddressToOffset( m_pLocalBaseAddress )
  87. uint32 m_nLocalSize; // RSX Local Memory Size
  88. // IO memory mapped into RSX
  89. void * m_pIoAddress; // RSX IO buffer, base address
  90. uint32 m_nIoSize; // RSX IO total size [including CMD buffer]
  91. uint32 m_nIoOffsetDelta; // add this to EA to get Io Offset
  92. // Call Cmd Buffer
  93. void* m_pCallCmdBuffer;
  94. uint32 m_nCallCmdBufferoffset;
  95. uint32 m_nCallWritePos; // Current posn (offset)
  96. uint32 m_nCallReadSegment;
  97. // main memory pool buffer
  98. void * m_pRsxMainMemoryPoolBuffer;
  99. uint32 m_nRsxMainMemoryPoolBufferSize;
  100. // Pointer to the draw states
  101. uint32 m_eaDrawStates;
  102. //--------------------------------------------------------------------------------------------------
  103. // SPU Task
  104. //--------------------------------------------------------------------------------------------------
  105. SpuTaskHandle m_spuHandle;
  106. //--------------------------------------------------------------------------------------------------
  107. // Patched Shaders
  108. //--------------------------------------------------------------------------------------------------
  109. uint8* m_pPatchBuff;
  110. uint32 m_nPatchIdx; // Write index for this frames patch buffer
  111. uint32 m_nPatchReadSeg;
  112. //--------------------------------------------------------------------------------------------------
  113. // Empty pixel shader
  114. //--------------------------------------------------------------------------------------------------
  115. CPs3gcmLocalMemoryBlock m_pShaderPsEmptyBuffer;
  116. CgBinaryProgram *m_pShaderPsEmpty; // empty pixel shader
  117. uint32 m_nPsEmptyShaderControl0;
  118. uint32 m_nPsEmptyAttributeInputMask;
  119. //--------------------------------------------------------------------------------------------------
  120. // Flip data
  121. //--------------------------------------------------------------------------------------------------
  122. uint32 m_flipMode; // Holds 30 or 60
  123. uint32 m_frameNo;
  124. uint32 m_finishIdx;
  125. bool m_fastFlip;
  126. //--------------------------------------------------------------------------------------------------
  127. // Display
  128. //--------------------------------------------------------------------------------------------------
  129. // Display size, aspect, pitch
  130. uint16 m_nRenderSize[2]; // with & height of the render buffer
  131. float m_flRenderAspect; // aspect ratio of the output device
  132. uint32 m_nSurfaceRenderPitch;
  133. CPs3gcmDisplay m_display;
  134. //--------------------------------------------------------------------------------------------------
  135. // Methods
  136. //--------------------------------------------------------------------------------------------------
  137. public:
  138. int32 Init();
  139. void Shutdown();
  140. void BeginScene();
  141. void EndScene();
  142. void Flip();
  143. void SetFastFlip(bool onoff);
  144. static int32_t CmdBufferFull(struct CellGcmContextData * pGcmContext, uint32_t size);
  145. // DrawPrimUP puts a drawprimup call into the call buffer, with a label and RET.
  146. // It's called from the gcmdrawstate which then sends a drawcall packet to the SPU
  147. uint32 DrawPrimitiveUP(D3DPRIMITIVETYPE nPrimitiveType,UINT nPrimitiveCount,
  148. CONST void *pVertexStreamZeroData, UINT nVertexStreamZeroStride );
  149. // GetRenderSurfaceBytes Note:
  150. // Height alignment must be 32 for tiled surfaces on RSX
  151. // 128 for Edge Post MLAA
  152. // 64 for Edge Post MLAA with EDGE_POST_MLAA_MODE_TRANSPOSE_64 flag set
  153. inline uint GetRenderSurfaceBytes( uint nHeightAlignment = 32 ) const ;
  154. private:
  155. int InitGcm();
  156. int InitVideo();
  157. void CreateRsxBuffers(); // Display buffers and defaut allocated RTs etc..
  158. void CreateIoBuffers(); // Allocs IO memory (mapped in Initgcm)
  159. };
  160. //--------------------------------------------------------------------------------------------------
  161. // Inlines
  162. //--------------------------------------------------------------------------------------------------
  163. inline uint CPs3gcmGlobalState::GetRenderSurfaceBytes( uint nHeightAlignment) const
  164. {
  165. return m_nSurfaceRenderPitch * AlignValue( m_nRenderSize[1], nHeightAlignment );
  166. }
  167. //--------------------------------------------------------------------------------------------------
  168. // Extern Globals
  169. //--------------------------------------------------------------------------------------------------
  170. extern CellGcmContextData gGcmContext;
  171. extern CellGcmContextData* gpGcmContext;
  172. extern CPs3gcmGlobalState g_ps3gcmGlobalState;
  173. extern CellGcmContextData gCallContext;
  174. extern CellGcmContextData* gpCallContext;
  175. //--------------------------------------------------------------------------------------------------
  176. // Memory block funcs that need access to g_ps3gcmGlobalState
  177. //--------------------------------------------------------------------------------------------------
  178. #ifndef SPU
  179. inline char * CPs3gcmLocalMemoryBlock::DataInLocalMemory() const
  180. {
  181. Assert( IsLocalMemory() );
  182. return
  183. ( m_nLocalMemoryOffset - g_ps3gcmGlobalState.m_nLocalBaseOffset ) +
  184. ( char * ) g_ps3gcmGlobalState.m_pLocalBaseAddress;
  185. }
  186. inline char * CPs3gcmLocalMemoryBlock::DataInMainMemory() const
  187. {
  188. Assert( !IsLocalMemory() && IsRsxMappedMemory() );
  189. return
  190. m_nLocalMemoryOffset +
  191. ( ( char * ) g_ps3gcmGlobalState.m_pIoAddress );
  192. }
  193. inline char * CPs3gcmLocalMemoryBlock::DataInMallocMemory() const
  194. {
  195. Assert( !IsLocalMemory() && !IsRsxMappedMemory() );
  196. return ( char * ) m_nLocalMemoryOffset;
  197. }
  198. inline char * CPs3gcmLocalMemoryBlock::DataInAnyMemory() const
  199. {
  200. switch ( PS3GCMALLOCATIONPOOL( m_uType ) )
  201. {
  202. default: return DataInLocalMemory();
  203. case kGcmAllocPoolMainMemory: return DataInMainMemory();
  204. case kGcmAllocPoolMallocMemory: return DataInMallocMemory();
  205. }
  206. }
  207. #endif
  208. #endif // INCLUDED_GCMSTATE_H