TextureCacheBase.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // Copyright 2010 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <filesystem>
  6. #include <fmt/format.h>
  7. #include <map>
  8. #include <memory>
  9. #include <optional>
  10. #include <string>
  11. #include <string_view>
  12. #include <tuple>
  13. #include <unordered_map>
  14. #include <unordered_set>
  15. #include <vector>
  16. #include "Common/BitSet.h"
  17. #include "Common/CommonTypes.h"
  18. #include "Common/Flag.h"
  19. #include "Common/MathUtil.h"
  20. #include "VideoCommon/AbstractTexture.h"
  21. #include "VideoCommon/Assets/CustomAsset.h"
  22. #include "VideoCommon/BPMemory.h"
  23. #include "VideoCommon/TextureConfig.h"
  24. #include "VideoCommon/TextureDecoder.h"
  25. #include "VideoCommon/TextureInfo.h"
  26. #include "VideoCommon/TextureUtils.h"
  27. #include "VideoCommon/VideoEvents.h"
  28. class AbstractFramebuffer;
  29. class AbstractStagingTexture;
  30. class PointerWrap;
  31. struct SamplerState;
  32. struct VideoConfig;
  33. namespace VideoCommon
  34. {
  35. class CustomTextureData;
  36. class GameTextureAsset;
  37. } // namespace VideoCommon
  38. constexpr std::string_view EFB_DUMP_PREFIX = "efb1";
  39. constexpr std::string_view XFB_DUMP_PREFIX = "xfb1";
  40. static constexpr int FRAMECOUNT_INVALID = 0;
  41. struct TextureAndTLUTFormat
  42. {
  43. TextureAndTLUTFormat(TextureFormat texfmt_ = TextureFormat::I4,
  44. TLUTFormat tlutfmt_ = TLUTFormat::IA8)
  45. : texfmt(texfmt_), tlutfmt(tlutfmt_)
  46. {
  47. }
  48. bool operator==(const TextureAndTLUTFormat& other) const
  49. {
  50. if (IsColorIndexed(texfmt))
  51. return texfmt == other.texfmt && tlutfmt == other.tlutfmt;
  52. return texfmt == other.texfmt;
  53. }
  54. TextureFormat texfmt;
  55. TLUTFormat tlutfmt;
  56. };
  57. struct EFBCopyParams
  58. {
  59. EFBCopyParams(PixelFormat efb_format_, EFBCopyFormat copy_format_, bool depth_, bool yuv_,
  60. bool all_copy_filter_coefs_needed_, bool copy_filter_can_overflow_,
  61. bool apply_gamma_)
  62. : efb_format(efb_format_), copy_format(copy_format_), depth(depth_), yuv(yuv_),
  63. all_copy_filter_coefs_needed(all_copy_filter_coefs_needed_),
  64. copy_filter_can_overflow(copy_filter_can_overflow_), apply_gamma(apply_gamma_)
  65. {
  66. }
  67. bool operator<(const EFBCopyParams& rhs) const
  68. {
  69. return std::tie(efb_format, copy_format, depth, yuv, all_copy_filter_coefs_needed,
  70. copy_filter_can_overflow,
  71. apply_gamma) < std::tie(rhs.efb_format, rhs.copy_format, rhs.depth, rhs.yuv,
  72. rhs.all_copy_filter_coefs_needed,
  73. rhs.copy_filter_can_overflow, rhs.apply_gamma);
  74. }
  75. PixelFormat efb_format;
  76. EFBCopyFormat copy_format;
  77. bool depth;
  78. bool yuv;
  79. bool all_copy_filter_coefs_needed;
  80. bool copy_filter_can_overflow;
  81. bool apply_gamma;
  82. };
  83. template <>
  84. struct fmt::formatter<EFBCopyParams>
  85. {
  86. constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
  87. template <typename FormatContext>
  88. auto format(const EFBCopyParams& uid, FormatContext& ctx) const
  89. {
  90. std::string copy_format;
  91. if (uid.copy_format == EFBCopyFormat::XFB)
  92. copy_format = "XFB";
  93. else
  94. copy_format = fmt::to_string(uid.copy_format);
  95. return fmt::format_to(ctx.out(),
  96. "format: {}, copy format: {}, depth: {}, yuv: {}, apply_gamma: {}, "
  97. "all_copy_filter_coefs_needed: {}, copy_filter_can_overflow: {}",
  98. uid.efb_format, copy_format, uid.depth, uid.yuv, uid.apply_gamma,
  99. uid.all_copy_filter_coefs_needed, uid.copy_filter_can_overflow);
  100. }
  101. };
  102. struct TCacheEntry
  103. {
  104. // common members
  105. std::unique_ptr<AbstractTexture> texture;
  106. std::unique_ptr<AbstractFramebuffer> framebuffer;
  107. u32 addr = 0;
  108. u32 size_in_bytes = 0;
  109. u64 base_hash = 0;
  110. u64 hash = 0; // for paletted textures, hash = base_hash ^ palette_hash
  111. TextureAndTLUTFormat format;
  112. u32 memory_stride = 0;
  113. bool is_efb_copy = false;
  114. bool is_custom_tex = false;
  115. bool may_have_overlapping_textures = true;
  116. // indicates that the mips in this texture are arbitrary content, aren't just downscaled
  117. bool has_arbitrary_mips = false;
  118. bool should_force_safe_hashing = false; // for XFB
  119. bool is_xfb_copy = false;
  120. bool is_xfb_container = false;
  121. u64 id = 0;
  122. u32 content_semaphore = 0; // Counts up
  123. // Indicates that this TCacheEntry has been invalided from m_textures_by_address
  124. bool invalidated = false;
  125. bool reference_changed = false; // used by xfb to determine when a reference xfb changed
  126. // Texture dimensions from the GameCube's point of view
  127. u32 native_width = 0;
  128. u32 native_height = 0;
  129. u32 native_levels = 0;
  130. // used to delete textures which haven't been used for TEXTURE_KILL_THRESHOLD frames
  131. int frameCount = FRAMECOUNT_INVALID;
  132. // Keep an iterator to the entry in m_textures_by_hash, so it does not need to be searched when
  133. // removing the cache entry
  134. std::multimap<u64, std::shared_ptr<TCacheEntry>>::iterator textures_by_hash_iter;
  135. // This is used to keep track of both:
  136. // * efb copies used by this partially updated texture
  137. // * partially updated textures which refer to this efb copy
  138. std::unordered_set<TCacheEntry*> references;
  139. // Pending EFB copy
  140. std::unique_ptr<AbstractStagingTexture> pending_efb_copy;
  141. u32 pending_efb_copy_width = 0;
  142. u32 pending_efb_copy_height = 0;
  143. std::string texture_info_name = "";
  144. std::vector<VideoCommon::CachedAsset<VideoCommon::GameTextureAsset>> linked_game_texture_assets;
  145. std::vector<VideoCommon::CachedAsset<VideoCommon::CustomAsset>> linked_asset_dependencies;
  146. explicit TCacheEntry(std::unique_ptr<AbstractTexture> tex,
  147. std::unique_ptr<AbstractFramebuffer> fb);
  148. ~TCacheEntry();
  149. void SetGeneralParameters(u32 _addr, u32 _size, TextureAndTLUTFormat _format,
  150. bool force_safe_hashing)
  151. {
  152. addr = _addr;
  153. size_in_bytes = _size;
  154. format = _format;
  155. should_force_safe_hashing = force_safe_hashing;
  156. }
  157. void SetDimensions(unsigned int _native_width, unsigned int _native_height,
  158. unsigned int _native_levels)
  159. {
  160. native_width = _native_width;
  161. native_height = _native_height;
  162. native_levels = _native_levels;
  163. memory_stride = _native_width;
  164. }
  165. void SetHashes(u64 _base_hash, u64 _hash)
  166. {
  167. base_hash = _base_hash;
  168. hash = _hash;
  169. }
  170. // This texture entry is used by the other entry as a sub-texture
  171. void CreateReference(TCacheEntry* other_entry)
  172. {
  173. // References are two-way, so they can easily be destroyed later
  174. this->references.emplace(other_entry);
  175. other_entry->references.emplace(this);
  176. }
  177. // Acquiring a content lock will lock the current contents and prevent texture cache from
  178. // reusing the same entry for a newer version of the texture.
  179. void AcquireContentLock() { content_semaphore++; }
  180. void ReleaseContentLock() { content_semaphore--; }
  181. // Can this be mutated?
  182. bool IsLocked() const { return content_semaphore > 0; }
  183. void SetXfbCopy(u32 stride);
  184. void SetEfbCopy(u32 stride);
  185. void SetNotCopy();
  186. bool OverlapsMemoryRange(u32 range_address, u32 range_size) const;
  187. bool IsEfbCopy() const { return is_efb_copy; }
  188. bool IsCopy() const { return is_xfb_copy || is_efb_copy; }
  189. u32 NumBlocksX() const;
  190. u32 NumBlocksY() const;
  191. u32 BytesPerRow() const;
  192. u64 CalculateHash() const;
  193. int HashSampleSize() const;
  194. u32 GetWidth() const { return texture->GetConfig().width; }
  195. u32 GetHeight() const { return texture->GetConfig().height; }
  196. u32 GetNumLevels() const { return texture->GetConfig().levels; }
  197. u32 GetNumLayers() const { return texture->GetConfig().layers; }
  198. AbstractTextureFormat GetFormat() const { return texture->GetConfig().format; }
  199. void DoState(PointerWrap& p);
  200. };
  201. using RcTcacheEntry = std::shared_ptr<TCacheEntry>;
  202. class TextureCacheBase
  203. {
  204. public:
  205. // Minimal version of TCacheEntry just for TexPool
  206. struct TexPoolEntry
  207. {
  208. std::unique_ptr<AbstractTexture> texture;
  209. std::unique_ptr<AbstractFramebuffer> framebuffer;
  210. int frameCount = FRAMECOUNT_INVALID;
  211. TexPoolEntry(std::unique_ptr<AbstractTexture> tex, std::unique_ptr<AbstractFramebuffer> fb);
  212. };
  213. struct TextureCreationInfo
  214. {
  215. u64 base_hash;
  216. u64 full_hash;
  217. u32 bytes_per_block;
  218. u32 palette_size;
  219. };
  220. TextureCacheBase();
  221. virtual ~TextureCacheBase();
  222. bool Initialize();
  223. void Shutdown();
  224. void OnConfigChanged(const VideoConfig& config);
  225. // Removes textures which aren't used for more than TEXTURE_KILL_THRESHOLD frames,
  226. // frameCount is the current frame number.
  227. void Cleanup(int _frameCount);
  228. void Invalidate();
  229. void ReleaseToPool(TCacheEntry* entry);
  230. TCacheEntry* Load(const TextureInfo& texture_info);
  231. RcTcacheEntry GetTexture(const int textureCacheSafetyColorSampleSize,
  232. const TextureInfo& texture_info);
  233. RcTcacheEntry GetXFBTexture(u32 address, u32 width, u32 height, u32 stride,
  234. MathUtil::Rectangle<int>* display_rect);
  235. virtual void BindTextures(BitSet32 used_textures, const std::array<SamplerState, 8>& samplers);
  236. void CopyRenderTargetToTexture(u32 dstAddr, EFBCopyFormat dstFormat, u32 width, u32 height,
  237. u32 dstStride, bool is_depth_copy,
  238. const MathUtil::Rectangle<int>& srcRect, bool isIntensity,
  239. bool scaleByHalf, float y_scale, float gamma, bool clamp_top,
  240. bool clamp_bottom,
  241. const CopyFilterCoefficients::Values& filter_coefficients);
  242. void ScaleTextureCacheEntryTo(RcTcacheEntry& entry, u32 new_width, u32 new_height);
  243. // Flushes all pending EFB copies to emulated RAM.
  244. void FlushEFBCopies();
  245. // Flush any Bound textures that can't be reused
  246. void FlushStaleBinds();
  247. // Texture Serialization
  248. void SerializeTexture(AbstractTexture* tex, const TextureConfig& config, PointerWrap& p);
  249. std::optional<TexPoolEntry> DeserializeTexture(PointerWrap& p);
  250. // Save States
  251. void DoState(PointerWrap& p);
  252. static bool AllCopyFilterCoefsNeeded(const std::array<u32, 3>& coefficients);
  253. static bool CopyFilterCanOverflow(const std::array<u32, 3>& coefficients);
  254. // Get a new sampler state
  255. static SamplerState GetSamplerState(u32 index, float custom_tex_scale, bool custom_tex,
  256. bool has_arbitrary_mips);
  257. protected:
  258. // Decodes the specified data to the GPU texture specified by entry.
  259. // Returns false if the configuration is not supported.
  260. // width, height are the size of the image in pixels.
  261. // aligned_width, aligned_height are the size of the image in pixels, aligned to the block size.
  262. // row_stride is the number of bytes for a row of blocks, not pixels.
  263. bool DecodeTextureOnGPU(RcTcacheEntry& entry, u32 dst_level, const u8* data, u32 data_size,
  264. TextureFormat format, u32 width, u32 height, u32 aligned_width,
  265. u32 aligned_height, u32 row_stride, const u8* palette,
  266. TLUTFormat palette_format);
  267. virtual void CopyEFB(AbstractStagingTexture* dst, const EFBCopyParams& params, u32 native_width,
  268. u32 bytes_per_row, u32 num_blocks_y, u32 memory_stride,
  269. const MathUtil::Rectangle<int>& src_rect, bool scale_by_half,
  270. bool linear_filter, float y_scale, float gamma, bool clamp_top,
  271. bool clamp_bottom, const std::array<u32, 3>& filter_coefficients);
  272. virtual void CopyEFBToCacheEntry(RcTcacheEntry& entry, bool is_depth_copy,
  273. const MathUtil::Rectangle<int>& src_rect, bool scale_by_half,
  274. bool linear_filter, EFBCopyFormat dst_format, bool is_intensity,
  275. float gamma, bool clamp_top, bool clamp_bottom,
  276. const std::array<u32, 3>& filter_coefficients);
  277. alignas(16) u8* m_temp = nullptr;
  278. size_t m_temp_size = 0;
  279. private:
  280. using TexAddrCache = std::multimap<u32, RcTcacheEntry>;
  281. using TexHashCache = std::multimap<u64, RcTcacheEntry>;
  282. using TexPool = std::unordered_multimap<TextureConfig, TexPoolEntry>;
  283. static bool DidLinkedAssetsChange(const TCacheEntry& entry);
  284. TCacheEntry* LoadImpl(const TextureInfo& texture_info, bool force_reload);
  285. bool CreateUtilityTextures();
  286. void SetBackupConfig(const VideoConfig& config);
  287. RcTcacheEntry
  288. CreateTextureEntry(const TextureCreationInfo& creation_info, const TextureInfo& texture_info,
  289. int safety_color_sample_size,
  290. std::vector<std::shared_ptr<VideoCommon::TextureData>> assets_data,
  291. bool custom_arbitrary_mipmaps, bool skip_texture_dump);
  292. RcTcacheEntry GetXFBFromCache(u32 address, u32 width, u32 height, u32 stride);
  293. RcTcacheEntry ApplyPaletteToEntry(RcTcacheEntry& entry, const u8* palette, TLUTFormat tlutfmt);
  294. RcTcacheEntry ReinterpretEntry(const RcTcacheEntry& existing_entry, TextureFormat new_format);
  295. RcTcacheEntry DoPartialTextureUpdates(RcTcacheEntry& entry_to_update, const u8* palette,
  296. TLUTFormat tlutfmt);
  297. void StitchXFBCopy(RcTcacheEntry& entry_to_update);
  298. void CheckTempSize(size_t required_size);
  299. RcTcacheEntry AllocateCacheEntry(const TextureConfig& config);
  300. std::optional<TexPoolEntry> AllocateTexture(const TextureConfig& config);
  301. TexPool::iterator FindMatchingTextureFromPool(const TextureConfig& config);
  302. TexAddrCache::iterator GetTexCacheIter(TCacheEntry* entry);
  303. // Return all possible overlapping textures. As addr+size of the textures is not
  304. // indexed, this may return false positives.
  305. std::pair<TexAddrCache::iterator, TexAddrCache::iterator>
  306. FindOverlappingTextures(u32 addr, u32 size_in_bytes);
  307. // Removes and unlinks texture from texture cache and returns it to the pool
  308. TexAddrCache::iterator InvalidateTexture(TexAddrCache::iterator t_iter,
  309. bool discard_pending_efb_copy = false);
  310. void UninitializeEFBMemory(u8* dst, u32 stride, u32 bytes_per_row, u32 num_blocks_y);
  311. void UninitializeXFBMemory(u8* dst, u32 stride, u32 bytes_per_row, u32 num_blocks_y);
  312. // Precomputing the coefficients for the previous, current, and next lines for the copy filter.
  313. static std::array<u32, 3>
  314. GetRAMCopyFilterCoefficients(const CopyFilterCoefficients::Values& coefficients);
  315. static std::array<u32, 3>
  316. GetVRAMCopyFilterCoefficients(const CopyFilterCoefficients::Values& coefficients);
  317. // Flushes a pending EFB copy to RAM from the host to the guest RAM.
  318. void WriteEFBCopyToRAM(u8* dst_ptr, u32 width, u32 height, u32 stride,
  319. std::unique_ptr<AbstractStagingTexture> staging_texture);
  320. void FlushEFBCopy(TCacheEntry* entry);
  321. // Returns a staging texture of the maximum EFB copy size.
  322. std::unique_ptr<AbstractStagingTexture> GetEFBCopyStagingTexture();
  323. // Returns an EFB copy staging texture to the pool, so it can be re-used.
  324. void ReleaseEFBCopyStagingTexture(std::unique_ptr<AbstractStagingTexture> tex);
  325. bool CheckReadbackTexture(u32 width, u32 height, AbstractTextureFormat format);
  326. void DoSaveState(PointerWrap& p);
  327. void DoLoadState(PointerWrap& p);
  328. // m_textures_by_address is the authoritive version of what's actually "in" the texture cache
  329. // but it's possible for invalidated TCache entries to live on elsewhere
  330. TexAddrCache m_textures_by_address;
  331. // m_textures_by_hash is an alternative view of the texture cache
  332. // All textures in here will also be in m_textures_by_address
  333. TexHashCache m_textures_by_hash;
  334. // m_bound_textures are actually active in the current draw
  335. // It's valid for textures to be in here after they've been invalidated
  336. std::array<RcTcacheEntry, 8> m_bound_textures{};
  337. TexPool m_texture_pool;
  338. u64 m_last_entry_id = 0;
  339. // Backup configuration values
  340. struct BackupConfig
  341. {
  342. int color_samples;
  343. bool texfmt_overlay;
  344. bool texfmt_overlay_center;
  345. bool hires_textures;
  346. bool cache_hires_textures;
  347. bool copy_cache_enable;
  348. bool stereo_3d;
  349. bool efb_mono_depth;
  350. bool gpu_texture_decoding;
  351. bool disable_vram_copies;
  352. bool arbitrary_mipmap_detection;
  353. bool graphics_mods;
  354. u32 graphics_mod_change_count;
  355. };
  356. BackupConfig m_backup_config = {};
  357. // Encoding texture used for EFB copies to RAM.
  358. std::unique_ptr<AbstractTexture> m_efb_encoding_texture;
  359. std::unique_ptr<AbstractFramebuffer> m_efb_encoding_framebuffer;
  360. // Decoding texture used for GPU texture decoding.
  361. std::unique_ptr<AbstractTexture> m_decoding_texture;
  362. // Pool of readback textures used for deferred EFB copies.
  363. std::vector<std::unique_ptr<AbstractStagingTexture>> m_efb_copy_staging_texture_pool;
  364. // List of pending EFB copies. It is important that the order is preserved for these,
  365. // so that overlapping textures are written to guest RAM in the order they are issued.
  366. // It's valid for textures to live be in here after they've been invalidated
  367. std::vector<RcTcacheEntry> m_pending_efb_copies;
  368. // Staging texture used for readbacks.
  369. // We store this in the class so that the same staging texture can be used for multiple
  370. // readbacks, saving the overhead of allocating a new buffer every time.
  371. std::unique_ptr<AbstractStagingTexture> m_readback_texture;
  372. void OnFrameEnd();
  373. Common::EventHook m_frame_event =
  374. AfterFrameEvent::Register([this](Core::System&) { OnFrameEnd(); }, "TextureCache");
  375. VideoCommon::TextureUtils::TextureDumper m_texture_dumper;
  376. };
  377. extern std::unique_ptr<TextureCacheBase> g_texture_cache;