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.

201 lines
6.1 KiB

  1. //========== Copyright � 2005, Valve Corporation, All rights reserved. ========
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef TEXTUREHEAP_H
  7. #define TEXTUREHEAP_H
  8. // Portal2 Console is not using due to amount of memory free on Xbox and RSX memory on PS3.
  9. // The desired console pattern is to have a similar footprint, because PS3 does not have a streaming solution and it has enough texture memory
  10. // the texture content choices for the consoles will be made to adapt.
  11. // Uncomment to allow system to operate
  12. //#define SUPPORTS_TEXTURE_STREAMING
  13. #if defined( _X360 )
  14. #include "locald3dtypes.h"
  15. #include "utllinkedlist.h"
  16. #include "filesystem.h"
  17. #include "materialsystem/itexture.h"
  18. typedef int TextureCacheHandle_t;
  19. #define INVALID_TEXTURECACHE_HANDLE 0
  20. class CTextureHeap
  21. {
  22. public:
  23. CTextureHeap();
  24. IDirect3DTexture *AllocTexture( int width, int height, int levels, DWORD usage, D3DFORMAT format, bool bNoD3DMemory, bool bCacheable );
  25. IDirect3DCubeTexture *AllocCubeTexture( int width, int levels, DWORD usage, D3DFORMAT format, bool bNoD3DMemory );
  26. IDirect3DVolumeTexture *AllocVolumeTexture( int width, int height, int depth, int levels, DWORD usage, D3DFORMAT format );
  27. IDirect3DSurface *AllocRenderTargetSurface( int width, int height, D3DFORMAT format, RTMultiSampleCount360_t multiSampleCount = RT_MULTISAMPLE_NONE, int base = -1 );
  28. // Perform the real d3d allocation, returns true if succesful, false otherwise.
  29. // Only valid for a texture created with no d3d bits, otherwise no-op.
  30. bool FixupAllocD3DMemory( IDirect3DBaseTexture *pTexture );
  31. // Release header and d3d bits.
  32. void FreeTexture( IDirect3DBaseTexture *pTexture );
  33. // Returns the total amount of memory needed or allocated for the entire texture.
  34. int GetSize( IDirect3DBaseTexture *pTexture );
  35. // Returns the amount of memory needed just for the cacheable component.
  36. int GetCacheableSize( IDirect3DBaseTexture *pTexture );
  37. // Crunch the heap.
  38. void Compact();
  39. // Get current backbuffer multisample type
  40. D3DMULTISAMPLE_TYPE GetBackBufferMultiSampleType();
  41. // Query to determine if the texture is managed by cacheing.
  42. bool IsTextureCacheManaged( IDirect3DBaseTexture *pD3DTexture );
  43. bool IsBaseAllocated( IDirect3DBaseTexture *pD3DTexture );
  44. bool IsTextureResident( IDirect3DBaseTexture *pD3DTexture );
  45. // update the lru for a texture. returns false if the high mipmpa is not valid
  46. bool TouchTexture( class CXboxTexture *pXboxTexture );
  47. void SetCacheableTextureParams( IDirect3DBaseTexture *pD3DTexture, const char *pFilename, int mipSkipCount );
  48. void FlushTextureCache();
  49. void SpewTextureCache();
  50. int GetCacheableHeapSize();
  51. private:
  52. bool RestoreCacheableTexture( IDirect3DBaseTexture *pD3DTexture );
  53. CUtlFixedLinkedList< IDirect3DBaseTexture* > m_TextureCache;
  54. };
  55. #if defined( SUPPORTS_TEXTURE_STREAMING )
  56. #define BASEPOOL1024_SIZE ( 12*1024*1024 ) // 1024x1024 DXT5
  57. #define BASEPOOL512_SIZE ( 12*1024*1024 ) // 1024x1024 DXT1
  58. #define BASEPOOL256_SIZE ( 16*1024*1024 ) // 512x512 DXT5
  59. #define BASEPOOL128_SIZE ( 16*1024*1024 ) // 512x512 DXT1
  60. #define BASEPOOL64_SIZE ( 4*1024*1024 ) // 256x256 DXT5
  61. #define BASEPOOL32_SIZE ( 4*1024*1024 ) // 256x256 DXT1
  62. #else
  63. #define BASEPOOL1024_SIZE 0
  64. #define BASEPOOL512_SIZE 0
  65. #define BASEPOOL256_SIZE 0
  66. #define BASEPOOL128_SIZE 0
  67. #define BASEPOOL64_SIZE 0
  68. #define BASEPOOL32_SIZE 0
  69. #endif
  70. enum TextureAllocator_t
  71. {
  72. TA_BASEPOOL_1024, // 1024K = 1024x1024 DXT5 Mip0
  73. TA_BASEPOOL_512, // 512K = 1024x1024 DXT1 Mip0
  74. TA_BASEPOOL_256, // 256K = 512x512 DXT5 Mip0
  75. TA_BASEPOOL_128, // 128K = 512x512 DXT1 Mip0
  76. TA_BASEPOOL_64, // 64K = 256x256 DXT5 Mip0
  77. TA_BASEPOOL_32, // 32K = 256x256 DXT1 Mip0
  78. TA_MIXED,
  79. TA_STANDARD,
  80. TA_MAX
  81. };
  82. enum TextureLoadError_t
  83. {
  84. TEXLOADERROR_NONE = 0,
  85. TEXLOADERROR_FILEOPEN = -1,
  86. TEXLOADERROR_READING = -2,
  87. };
  88. struct THBaseInfo_t
  89. {
  90. THBaseInfo_t()
  91. {
  92. m_tcHandle = INVALID_TEXTURECACHE_HANDLE;
  93. m_hFilename = NULL;
  94. m_hAsyncControl = NULL;
  95. m_nFrameCount = 0;
  96. m_nBaseSize = 0;
  97. m_nMipSize = 0;
  98. m_nMipSkipCount = 0;
  99. m_bBaseAllocated = false;
  100. m_bMipAllocated = false;
  101. m_BaseValid = 0;
  102. }
  103. TextureAllocator_t m_fAllocator;
  104. // for cacheable tetxures
  105. TextureCacheHandle_t m_tcHandle;
  106. FileNameHandle_t m_hFilename;
  107. FSAsyncControl_t m_hAsyncControl;
  108. // for age, tracks which frame a texture was touched, fastest GPU non-blocking technique
  109. int m_nFrameCount;
  110. // base and mip are both non-zero for cacheable textures, which require mips
  111. // a non-cacheable texture only has a non-zero base, regardless of its mips
  112. unsigned int m_nBaseSize;
  113. unsigned int m_nMipSize;
  114. // used for texture restoration, the top mip
  115. int m_nMipSkipCount;
  116. // tracks when valid for rendering, r/w across threads
  117. CInterlockedInt m_BaseValid;
  118. // tracks when valid for i/o loading
  119. bool m_bBaseAllocated : 1;
  120. // tracks when mip is a true seperate allocation, used to resolve offset or pointer
  121. bool m_bMipAllocated : 1;
  122. };
  123. //-----------------------------------------------------------------------------
  124. // Get Texture HW bases
  125. //-----------------------------------------------------------------------------
  126. inline void *GetD3DTextureBasePtr( IDirect3DBaseTexture* pTex )
  127. {
  128. return (void *)( (unsigned int)pTex->Format.BaseAddress << 12 );
  129. }
  130. inline void *GetD3DTextureMipPtr( IDirect3DBaseTexture* pTex )
  131. {
  132. // could be an offset or a pointer, state dependant
  133. return (void *)( (unsigned int)pTex->Format.MipAddress << 12 );
  134. }
  135. extern CTextureHeap g_TextureHeap;
  136. struct THInfo_t : public THBaseInfo_t
  137. {
  138. // Mixed heap info
  139. int nLogicalBytes;
  140. int nBytes;
  141. bool bFree:1;
  142. bool bNonTexture:1;
  143. THInfo_t *pPrev, *pNext;
  144. };
  145. class CXboxTexture : public IDirect3DTexture, public THInfo_t
  146. {
  147. public:
  148. CXboxTexture()
  149. : bImmobile( false )
  150. {
  151. }
  152. bool CanRelocate() { return ( !bImmobile && !IsBusy() ); }
  153. bool bImmobile;
  154. };
  155. class CXboxCubeTexture : public IDirect3DCubeTexture, public THBaseInfo_t
  156. {
  157. };
  158. class CXboxVolumeTexture : public IDirect3DVolumeTexture, public THBaseInfo_t
  159. {
  160. };
  161. #endif
  162. #endif // TEXTUREHEAP_H