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.

166 lines
6.6 KiB

  1. //================ Copyright (c) Valve Corporation. All Rights Reserved. ===========================
  2. //
  3. // Local memory manager
  4. //
  5. //==================================================================================================
  6. #ifndef INCLUDED_PS3GCMMEMORY_H
  7. #define INCLUDED_PS3GCMMEMORY_H
  8. #ifndef SPU
  9. #include "tier1/strtools.h"
  10. #include "shaderapi/gpumemorystats.h"
  11. #include "cell/gcm.h"
  12. #include "gcmconfig.h"
  13. #else
  14. #endif
  15. //--------------------------------------------------------------------------------------------------
  16. // Externals
  17. //--------------------------------------------------------------------------------------------------
  18. #ifndef SPU
  19. extern void GetGPUMemoryStats( GPUMemoryStats &stats );
  20. extern void Ps3gcmLocalMemoryAllocator_Init();
  21. #endif
  22. //--------------------------------------------------------------------------------------------------
  23. // Memory Pools, Types and LocalMemoryBlock
  24. //--------------------------------------------------------------------------------------------------
  25. enum CPs3gcmAllocationPool_t
  26. {
  27. kGcmAllocPoolDefault,
  28. kGcmAllocPoolDynamicNewPath,
  29. kGcmAllocPoolDynamic,
  30. kGcmAllocPoolTiledColorFB, // Frame-buffer tiled color memory (should be first preset tiled region)
  31. kGcmAllocPoolTiledColorFBQ, // Quarter-frame-buffer tiled color memory
  32. kGcmAllocPoolTiledColor512, // 512x512 tiled color memory
  33. kGcmAllocPoolTiledColorMisc, // Last tiled color region
  34. kGcmAllocPoolTiledD24S8,
  35. kGcmAllocPoolMainMemory, // Pool in the main RSX-mapped IO memory
  36. kGcmAllocPoolMallocMemory, // Pool in malloc-backed non-RSX-mapped memory
  37. kGcmAllocPoolCount
  38. };
  39. #define PS3GCMALLOCATIONPOOL( uType ) ( (CPs3gcmAllocationPool_t)( ( ((uint32)(uType)) >> 28 ) & 0xF ) )
  40. #define PS3GCMALLOCATIONALIGN( uType ) ( ((uint32)(uType)) & 0xFFFFFF )
  41. #define PS3GCMALLOCATIONTYPE( uAlign, ePool, iType ) (((uint32)(uAlign))&0xFFFFFF) | ( (((uint32)(iType))&0xF) << 24 ) | ( (((uint32)(ePool))&0xF) << 28 )
  42. enum CPs3gcmAllocationType_t
  43. {
  44. // Default pool
  45. kAllocPs3gcmTextureData0 = PS3GCMALLOCATIONTYPE( 128, kGcmAllocPoolMainMemory, 0 ),
  46. kAllocPs3gcmTextureData = PS3GCMALLOCATIONTYPE( 128, kGcmAllocPoolDefault, 1 ),
  47. kAllocPs3GcmVertexBuffer = PS3GCMALLOCATIONTYPE( 32, kGcmAllocPoolDefault, 2 ),
  48. kAllocPs3GcmIndexBuffer = PS3GCMALLOCATIONTYPE( 32, kGcmAllocPoolDefault, 3 ),
  49. kAllocPs3GcmShader = PS3GCMALLOCATIONTYPE( 128, kGcmAllocPoolDefault, 4 ),
  50. kAllocPs3GcmEdgeGeomBuffer = PS3GCMALLOCATIONTYPE( 128, kGcmAllocPoolDefault, 5 ),
  51. // Dynamic pool
  52. kAllocPs3GcmVertexBufferDynamic = PS3GCMALLOCATIONTYPE( 32, kGcmAllocPoolDynamic, 1 ),
  53. kAllocPs3GcmIndexBufferDynamic = PS3GCMALLOCATIONTYPE( 32, kGcmAllocPoolDynamic, 2 ),
  54. kAllocPs3GcmDynamicBufferPool = PS3GCMALLOCATIONTYPE( 32, kGcmAllocPoolDynamicNewPath, 1 ),
  55. // Malloc memory pool
  56. kAllocPs3GcmVertexBufferDma = PS3GCMALLOCATIONTYPE( 32, kGcmAllocPoolMallocMemory, 1 ),
  57. kAllocPs3GcmIndexBufferDma = PS3GCMALLOCATIONTYPE( 32, kGcmAllocPoolMallocMemory, 2 ),
  58. // Tiled pools
  59. kAllocPs3gcmColorBufferFB = PS3GCMALLOCATIONTYPE( 64, kGcmAllocPoolTiledColorFB, 1 ),
  60. kAllocPs3gcmColorBufferFBQ = PS3GCMALLOCATIONTYPE( 64, kGcmAllocPoolTiledColorFBQ, 1 ),
  61. kAllocPs3gcmColorBuffer512 = PS3GCMALLOCATIONTYPE( 64, kGcmAllocPoolTiledColor512, 1 ),
  62. kAllocPs3gcmColorBufferMisc = PS3GCMALLOCATIONTYPE( 64*1024, kGcmAllocPoolTiledColorMisc,1 ),
  63. kAllocPs3gcmDepthBuffer = PS3GCMALLOCATIONTYPE( 64*1024, kGcmAllocPoolTiledD24S8, 1 ),
  64. };
  65. struct CPs3gcmLocalMemoryBlockSystemGlobal;
  66. struct ALIGN16 CPs3gcmLocalMemoryBlock
  67. {
  68. public:
  69. CPs3gcmLocalMemoryBlock() {}
  70. #if 0
  71. #define GCMLOCALMEMORYBLOCKDEBUG
  72. uint64 m_dbgGuardCookie; // Debug cookie used to guard when calling code let block go out of scope without freeing it
  73. #endif
  74. protected:
  75. uint32 m_nLocalMemoryOffset; // Offset in RSX local memory
  76. uint32 m_uiSize; // Actual allocation size, might be larger than requested allocation size
  77. CPs3gcmAllocationType_t m_uType; // Allocation type with required alignment
  78. uint32 m_uiIndex; // Index of the allocation in allocation tracking system
  79. bool Alloc(); // Internal implementation of Local Memory Allocator
  80. // Prevent copying (since patch-back mechanism needs to access the allocated blocks)
  81. CPs3gcmLocalMemoryBlock( CPs3gcmLocalMemoryBlock const &x ) { V_memcpy( this, &x, sizeof( CPs3gcmLocalMemoryBlock ) ); }
  82. CPs3gcmLocalMemoryBlock& operator =( CPs3gcmLocalMemoryBlock const &x ) { V_memcpy( this, &x, sizeof( CPs3gcmLocalMemoryBlock ) ); return *this; }
  83. public:
  84. inline void Assign( CPs3gcmLocalMemoryBlockSystemGlobal const &x ) { V_memcpy( this, &x, sizeof( CPs3gcmLocalMemoryBlock ) ); }
  85. inline bool Alloc( CPs3gcmAllocationType_t uType, uint32 uiSize ) { m_uType = uType; m_uiSize = uiSize; return Alloc(); }
  86. inline void AttachToExternalMemory( CPs3gcmAllocationType_t uType, uint32 nOffset, uint32 uiSize ) { m_uType = uType; m_uiSize = uiSize; m_nLocalMemoryOffset = nOffset; m_uiIndex = ~0; }
  87. void Free();
  88. void FreeAndAllocNew() { Free(); Alloc(); }
  89. inline uint32 Offset() const { return m_nLocalMemoryOffset; }
  90. inline uint32 Size() const { return m_uiSize; }
  91. inline bool IsLocalMemory() const { return PS3GCMALLOCATIONPOOL( m_uType ) < kGcmAllocPoolMainMemory; }
  92. inline bool IsRsxMappedMemory() const { return PS3GCMALLOCATIONPOOL( m_uType ) < kGcmAllocPoolMallocMemory; }
  93. inline uint8 GcmMemoryLocation() const { return IsLocalMemory() ? CELL_GCM_LOCATION_LOCAL : CELL_GCM_LOCATION_MAIN; }
  94. #ifndef SPU
  95. char * DataInLocalMemory() const;
  96. char * DataInMainMemory() const;
  97. char * DataInMallocMemory() const;
  98. char * DataInAnyMemory() const;
  99. #endif
  100. // Tiled memory access
  101. uint32 TiledMemoryTagAreaBase() const;
  102. uint32 TiledMemoryIndex() const;
  103. // Zcull memory access
  104. uint32 ZcullMemoryIndex() const;
  105. uint32 ZcullMemoryStart() const;
  106. } ALIGN16_POST;
  107. struct CPs3gcmLocalMemoryBlockSystemGlobal : public CPs3gcmLocalMemoryBlock
  108. {
  109. public:
  110. CPs3gcmLocalMemoryBlockSystemGlobal() {}
  111. private:
  112. // Prevent copying (since patch-back mechanism needs to access the allocated blocks)
  113. CPs3gcmLocalMemoryBlockSystemGlobal( CPs3gcmLocalMemoryBlock const &x );
  114. CPs3gcmLocalMemoryBlockSystemGlobal& operator =( CPs3gcmLocalMemoryBlockSystemGlobal const &x );
  115. };
  116. //--------------------------------------------------------------------------------------------------
  117. // Buffer (used by IB and VBs)
  118. //--------------------------------------------------------------------------------------------------
  119. struct CPs3gcmBuffer
  120. {
  121. CPs3gcmLocalMemoryBlock m_lmBlock;
  122. public:
  123. inline uint32 Offset() { return m_lmBlock.Offset(); }
  124. public:
  125. #ifndef SPU
  126. static CPs3gcmBuffer * New( uint32 uiSize, CPs3gcmAllocationType_t uType );
  127. void Release();
  128. #endif
  129. };
  130. #endif // INCLUDED_PS3GCMMEMORY_H