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.

249 lines
8.0 KiB

  1. //================ Copyright (c) Valve Corporation. All Rights Reserved. ===========================
  2. //
  3. // Texture Layout, CPs3gcmTexture, and CPs3gcmTextureData_t
  4. //
  5. //==================================================================================================
  6. #ifndef INCLUDED_GCMTEXTURE_H
  7. #define INCLUDED_GCMTEXTURE_H
  8. #include "ps3/ps3_platform.h"
  9. #include "ps3gcmmemory.h"
  10. #include "gcmstate.h"
  11. //--------------------------------------------------------------------------------------------------
  12. // Literals
  13. //--------------------------------------------------------------------------------------------------
  14. #define PS3_TEX_MAX_FORMAT_COUNT 48
  15. #define PS3_TEX_CANONICAL_FORMAT_COUNT 19
  16. //--------------------------------------------------------------------------------------------------
  17. // Texture layout, texture etc..
  18. //--------------------------------------------------------------------------------------------------
  19. struct ALIGN16 CPs3gcmTextureLayout
  20. {
  21. #ifndef _CERT
  22. char *m_layoutSummary; // for debug visibility
  23. #endif
  24. // format mapping description
  25. struct ALIGN16 Format_t
  26. {
  27. #ifndef _CERT
  28. char *m_formatSummary; // for debug visibility
  29. #endif
  30. enum GcmCaps_t
  31. {
  32. kCapSRGB = (1<<0), // GCM can sample it as SRGB
  33. kCap4xBlocks = (1<<1), // Pitch is referring to 4 texel blocks and not single texel blocks (DXT)
  34. };
  35. D3DFORMAT m_d3dFormat; // what D3D knows it as; see public/bitmap/imageformat.h
  36. uint32 m_gcmRemap; // GCM remap mask
  37. uint16 m_gcmPitchPer4X; // GCM pitch multiplier per every 4 pixels of width
  38. uint8 m_gcmFormat; // GCM format
  39. uint8 m_gcmCaps; // GCM caps of this texture
  40. }
  41. ALIGN16_POST;
  42. // const inputs used for hashing
  43. struct Key_t
  44. {
  45. D3DFORMAT m_texFormat; // D3D texel format
  46. uint16 m_size[3]; // dimensions of the base mip
  47. uint8 m_texFlags; // mipped, autogen mips, render target, ... ?
  48. uint8 m_nActualMipCount; // Actual number of mips; on console builds, we typically drop the smallest (highest index)
  49. // mips to save space (they waste a lot of space for page-alignment reasons)
  50. // high-bit 0x80 indicates cubemap
  51. };
  52. // layout flags
  53. enum Flags_t
  54. {
  55. kfDynamicNoSwizzle = (1<<0), // Indicates whether this texture needs to keep a backing store for incremental updates.
  56. // (On PS3 this will prevent texture from being swizzled to allow CPU writes at subrect offsets)
  57. kfMip = (1<<1),
  58. kfMipAuto = (1<<2),
  59. kfTypeRenderable = (1<<3),
  60. kfTypeDepthStencil = (1<<4),
  61. kfTypeCubeMap = (1<<5),
  62. kfSrgbEnabled = (1<<6),
  63. kfNoD3DMemory = (1<<7), // Allocation of storage for the bits has been deferred (call IDirect3DDevice9::AllocateTextureStorage to do the allocation)
  64. // -!!--!!- DO NOT ADD MORE FLAGS -!!--!!- (m_texFlags is only 8 bits)
  65. };
  66. // slice information
  67. struct Slice_t
  68. {
  69. uint32 m_storageOffset; //where in the storage slab does this slice live
  70. uint32 m_storageSize; //how much storage does this slice occupy
  71. uint16 m_size[3]; //texel dimensions of this slice
  72. };
  73. //
  74. // Structure definition
  75. //
  76. Key_t m_key; // key of the layout
  77. int32 mutable m_refCount; // refcount
  78. uint32 m_storageTotalSize; // size of storage slab required
  79. uint16 m_nFormat; // format specific info; index in g_ps3texFormats table
  80. uint8 m_mipCount; // derived by starting at base size and working down towards 1x1
  81. CPs3gcmAllocationType_t mutable m_gcmAllocType; // type of GCM allocation to determine pool/alignment/etc.
  82. #ifndef SPU
  83. // slice array
  84. Slice_t m_slices[0]; // dynamically allocated 2-d array [faces][mips]
  85. public:
  86. inline int SlicePitch( int iSlice ) const;
  87. inline int DefaultPitch() const;
  88. inline const Format_t * GetFormatPtr()const;
  89. #endif
  90. public:
  91. inline bool IsSwizzled() const { return !( m_key.m_texFlags & ( kfDynamicNoSwizzle | kfTypeRenderable ) ) && IsPowerOfTwo( m_key.m_size[0] ) && IsPowerOfTwo( m_key.m_size[1] ) && IsPowerOfTwo( m_key.m_size[2] ); }
  92. inline bool IsCubeMap() const { return !!(m_key.m_texFlags & kfTypeCubeMap); }
  93. inline bool IsVolumeTex() const { return !!(m_key.m_size[2] > 1); }
  94. inline bool IsTiledMemory() const { return (m_key.m_texFlags & ( kfTypeRenderable | kfDynamicNoSwizzle )) == kfTypeRenderable; }
  95. inline int FaceCount() const { return ( !IsCubeMap() ) ? 1 : 6; }
  96. inline int MipCount() const { return ( m_key.m_texFlags & kfMip ) ? m_key.m_nActualMipCount : 1; }
  97. inline int SlicePitch2( int iSlice, const Slice_t* pSlices, const Format_t *pTexFormats ) const{ return !IsTiledMemory() ? ( ( IsSwizzled() ? pSlices[iSlice].m_size[0] : m_key.m_size[0] ) * pTexFormats[m_nFormat].m_gcmPitchPer4X / 4 ) : pTexFormats[m_nFormat].m_gcmPitchPer4X; }
  98. inline int DefaultPitch2( const Format_t *pTexFormats ) const { return !IsTiledMemory() ? m_key.m_size[0] * pTexFormats[m_nFormat].m_gcmPitchPer4X / 4 : pTexFormats[m_nFormat].m_gcmPitchPer4X; }
  99. inline int SliceIndex( int face, int mip ) const { return mip + ( face * MipCount() ); }
  100. public:
  101. #ifndef SPU
  102. static CPs3gcmTextureLayout const * New( Key_t const &k );
  103. void Release() const;
  104. #endif
  105. }
  106. ALIGN16_POST;
  107. extern CPs3gcmTextureLayout::Format_t g_ps3texFormats[PS3_TEX_MAX_FORMAT_COUNT];
  108. extern uint g_nPs3texFormatCount;
  109. #ifndef SPU
  110. // convenience functions on PPU that use implicit tables always accessible on PPU
  111. inline int CPs3gcmTextureLayout::SlicePitch( int iSlice ) const
  112. {
  113. return SlicePitch2( iSlice, &m_slices[0], g_ps3texFormats );
  114. }
  115. inline int CPs3gcmTextureLayout::DefaultPitch() const
  116. {
  117. return DefaultPitch2( g_ps3texFormats );
  118. }
  119. inline const CPs3gcmTextureLayout::Format_t * CPs3gcmTextureLayout::GetFormatPtr()const
  120. {
  121. return &g_ps3texFormats[ m_nFormat ];
  122. }
  123. #endif
  124. struct ALIGN16 CPs3gcmTexture
  125. {
  126. CPs3gcmTextureLayout const *m_layout; // this structure persists. see CPs3gcmTextureLayout::Release( it asserts if refcount goes down to zero )
  127. ALIGN16 CPs3gcmLocalMemoryBlock m_lmBlock ALIGN16_POST; // this structure has the Offset, and the texture bits at that offset persist until all Draw calls are made that use it
  128. inline uint32 Offset()const { Assert( m_lmBlock.Size() ); return m_lmBlock.Offset(); }
  129. #ifndef SPU
  130. inline char * Data() { Assert( m_lmBlock.Size() ); return m_lmBlock.DataInAnyMemory(); }
  131. #endif
  132. public:
  133. #ifndef SPU
  134. static CPs3gcmTexture * New( CPs3gcmTextureLayout::Key_t const &key );
  135. void Release();
  136. bool Allocate();
  137. #endif
  138. }
  139. ALIGN16_POST;
  140. struct CPs3gcmTextureData_t
  141. {
  142. // CPs3gcmTextureLayout const *m_eaLayout
  143. uint32 m_eaLayout; // this structure persists. see CPs3gcmTextureLayout::Release( it asserts if refcount goes down to zero )
  144. uint32 m_nLocalOffset; // the offset of the texture bits
  145. void Assign( const CPs3gcmTexture * pThat )
  146. {
  147. if( pThat )
  148. {
  149. m_eaLayout = ( uint32 )pThat->m_layout;
  150. m_nLocalOffset = pThat->Offset();
  151. Assert( m_eaLayout ? !( 15 & ( uintp( m_eaLayout ) | m_nLocalOffset ) ) && m_nLocalOffset : !m_nLocalOffset );
  152. }
  153. else
  154. {
  155. Reset();
  156. }
  157. }
  158. inline uint32 Offset()const { return m_nLocalOffset; }
  159. void Reset()
  160. {
  161. m_eaLayout = 0;
  162. m_nLocalOffset = 0;
  163. }
  164. bool IsNull()const
  165. {
  166. return !NotNull();
  167. }
  168. bool NotNull()const
  169. {
  170. // either both are null, or none is null
  171. Assert( ( m_eaLayout == 0 ) == ( m_nLocalOffset == 0 ) );
  172. return m_eaLayout != 0;
  173. }
  174. operator bool() const { return NotNull(); }
  175. };
  176. //
  177. // CPs3BindTexture_t : Everything we need to bind a texture
  178. //
  179. // This is what the SPU needs to bind the texture
  180. struct CPs3BindTexture_t
  181. {
  182. uint8 m_sampler;
  183. uint8 m_nBindFlags;
  184. uint8 m_UWrap;
  185. uint8 m_VWrap;
  186. uint8 m_WWrap;
  187. uint8 m_minFilter;
  188. uint8 m_magFilter;
  189. uint8 m_mipFilter;
  190. uint32 m_nLayout;
  191. CPs3gcmLocalMemoryBlock *m_pLmBlock;
  192. int m_boundStd;
  193. int m_hTexture;
  194. };
  195. // This is what we store when asked to bind a texture
  196. // When the cmd buffer is executed, at this time we lookup
  197. // the remaining fields and pack some CPs3BindTexture_t to actually use on the SPU
  198. struct CPs3BindParams_t
  199. {
  200. uint16 m_nBindTexIndex;
  201. uint8 m_sampler;
  202. uint8 m_nBindFlags;
  203. int m_boundStd;
  204. int m_hTexture;
  205. };
  206. #endif // INCLUDED_GCMTEXTURE_H