texture_cache.h 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <array>
  7. #include <memory>
  8. #include <mutex>
  9. #include <set>
  10. #include <tuple>
  11. #include <unordered_map>
  12. #include <vector>
  13. #include <boost/icl/interval_map.hpp>
  14. #include <boost/range/iterator_range.hpp>
  15. #include "common/assert.h"
  16. #include "common/common_types.h"
  17. #include "common/math_util.h"
  18. #include "core/core.h"
  19. #include "core/memory.h"
  20. #include "core/settings.h"
  21. #include "video_core/engines/fermi_2d.h"
  22. #include "video_core/engines/maxwell_3d.h"
  23. #include "video_core/gpu.h"
  24. #include "video_core/memory_manager.h"
  25. #include "video_core/rasterizer_interface.h"
  26. #include "video_core/surface.h"
  27. #include "video_core/texture_cache/copy_params.h"
  28. #include "video_core/texture_cache/format_lookup_table.h"
  29. #include "video_core/texture_cache/surface_base.h"
  30. #include "video_core/texture_cache/surface_params.h"
  31. #include "video_core/texture_cache/surface_view.h"
  32. namespace Tegra::Texture {
  33. struct FullTextureInfo;
  34. }
  35. namespace VideoCore {
  36. class RasterizerInterface;
  37. }
  38. namespace VideoCommon {
  39. using VideoCore::Surface::PixelFormat;
  40. using VideoCore::Surface::SurfaceTarget;
  41. using RenderTargetConfig = Tegra::Engines::Maxwell3D::Regs::RenderTargetConfig;
  42. template <typename TSurface, typename TView>
  43. class TextureCache {
  44. using IntervalMap = boost::icl::interval_map<CacheAddr, std::set<TSurface>>;
  45. using IntervalType = typename IntervalMap::interval_type;
  46. public:
  47. void InvalidateRegion(CacheAddr addr, std::size_t size) {
  48. std::lock_guard lock{mutex};
  49. for (const auto& surface : GetSurfacesInRegion(addr, size)) {
  50. Unregister(surface);
  51. }
  52. }
  53. /**
  54. * Guarantees that rendertargets don't unregister themselves if the
  55. * collide. Protection is currently only done on 3D slices.
  56. */
  57. void GuardRenderTargets(bool new_guard) {
  58. guard_render_targets = new_guard;
  59. }
  60. void GuardSamplers(bool new_guard) {
  61. guard_samplers = new_guard;
  62. }
  63. void FlushRegion(CacheAddr addr, std::size_t size) {
  64. std::lock_guard lock{mutex};
  65. auto surfaces = GetSurfacesInRegion(addr, size);
  66. if (surfaces.empty()) {
  67. return;
  68. }
  69. std::sort(surfaces.begin(), surfaces.end(), [](const TSurface& a, const TSurface& b) {
  70. return a->GetModificationTick() < b->GetModificationTick();
  71. });
  72. for (const auto& surface : surfaces) {
  73. FlushSurface(surface);
  74. }
  75. }
  76. TView GetTextureSurface(const Tegra::Texture::TICEntry& tic,
  77. const VideoCommon::Shader::Sampler& entry) {
  78. std::lock_guard lock{mutex};
  79. const auto gpu_addr{tic.Address()};
  80. if (!gpu_addr) {
  81. return GetNullSurface(SurfaceParams::ExpectedTarget(entry));
  82. }
  83. const auto host_ptr{system.GPU().MemoryManager().GetPointer(gpu_addr)};
  84. const auto cache_addr{ToCacheAddr(host_ptr)};
  85. if (!cache_addr) {
  86. return GetNullSurface(SurfaceParams::ExpectedTarget(entry));
  87. }
  88. const auto params{SurfaceParams::CreateForTexture(format_lookup_table, tic, entry)};
  89. const auto [surface, view] = GetSurface(gpu_addr, cache_addr, params, true, false);
  90. if (guard_samplers) {
  91. sampled_textures.push_back(surface);
  92. }
  93. return view;
  94. }
  95. TView GetImageSurface(const Tegra::Texture::TICEntry& tic,
  96. const VideoCommon::Shader::Image& entry) {
  97. std::lock_guard lock{mutex};
  98. const auto gpu_addr{tic.Address()};
  99. if (!gpu_addr) {
  100. return GetNullSurface(SurfaceParams::ExpectedTarget(entry));
  101. }
  102. const auto host_ptr{system.GPU().MemoryManager().GetPointer(gpu_addr)};
  103. const auto cache_addr{ToCacheAddr(host_ptr)};
  104. if (!cache_addr) {
  105. return GetNullSurface(SurfaceParams::ExpectedTarget(entry));
  106. }
  107. const auto params{SurfaceParams::CreateForImage(format_lookup_table, tic, entry)};
  108. const auto [surface, view] = GetSurface(gpu_addr, cache_addr, params, true, false);
  109. if (guard_samplers) {
  110. sampled_textures.push_back(surface);
  111. }
  112. return view;
  113. }
  114. bool TextureBarrier() {
  115. const bool any_rt =
  116. std::any_of(sampled_textures.begin(), sampled_textures.end(),
  117. [](const auto& surface) { return surface->IsRenderTarget(); });
  118. sampled_textures.clear();
  119. return any_rt;
  120. }
  121. TView GetDepthBufferSurface(bool preserve_contents) {
  122. std::lock_guard lock{mutex};
  123. auto& maxwell3d = system.GPU().Maxwell3D();
  124. if (!maxwell3d.dirty.depth_buffer) {
  125. return depth_buffer.view;
  126. }
  127. maxwell3d.dirty.depth_buffer = false;
  128. const auto& regs{maxwell3d.regs};
  129. const auto gpu_addr{regs.zeta.Address()};
  130. if (!gpu_addr || !regs.zeta_enable) {
  131. SetEmptyDepthBuffer();
  132. return {};
  133. }
  134. const auto host_ptr{system.GPU().MemoryManager().GetPointer(gpu_addr)};
  135. const auto cache_addr{ToCacheAddr(host_ptr)};
  136. if (!cache_addr) {
  137. SetEmptyDepthBuffer();
  138. return {};
  139. }
  140. const auto depth_params{SurfaceParams::CreateForDepthBuffer(
  141. system, regs.zeta_width, regs.zeta_height, regs.zeta.format,
  142. regs.zeta.memory_layout.block_width, regs.zeta.memory_layout.block_height,
  143. regs.zeta.memory_layout.block_depth, regs.zeta.memory_layout.type)};
  144. auto surface_view = GetSurface(gpu_addr, cache_addr, depth_params, preserve_contents, true);
  145. if (depth_buffer.target)
  146. depth_buffer.target->MarkAsRenderTarget(false, NO_RT);
  147. depth_buffer.target = surface_view.first;
  148. depth_buffer.view = surface_view.second;
  149. if (depth_buffer.target)
  150. depth_buffer.target->MarkAsRenderTarget(true, DEPTH_RT);
  151. return surface_view.second;
  152. }
  153. TView GetColorBufferSurface(std::size_t index, bool preserve_contents) {
  154. std::lock_guard lock{mutex};
  155. ASSERT(index < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets);
  156. auto& maxwell3d = system.GPU().Maxwell3D();
  157. if (!maxwell3d.dirty.render_target[index]) {
  158. return render_targets[index].view;
  159. }
  160. maxwell3d.dirty.render_target[index] = false;
  161. const auto& regs{maxwell3d.regs};
  162. if (index >= regs.rt_control.count || regs.rt[index].Address() == 0 ||
  163. regs.rt[index].format == Tegra::RenderTargetFormat::NONE) {
  164. SetEmptyColorBuffer(index);
  165. return {};
  166. }
  167. const auto& config{regs.rt[index]};
  168. const auto gpu_addr{config.Address()};
  169. if (!gpu_addr) {
  170. SetEmptyColorBuffer(index);
  171. return {};
  172. }
  173. const auto host_ptr{system.GPU().MemoryManager().GetPointer(gpu_addr)};
  174. const auto cache_addr{ToCacheAddr(host_ptr)};
  175. if (!cache_addr) {
  176. SetEmptyColorBuffer(index);
  177. return {};
  178. }
  179. auto surface_view =
  180. GetSurface(gpu_addr, cache_addr, SurfaceParams::CreateForFramebuffer(system, index),
  181. preserve_contents, true);
  182. if (render_targets[index].target)
  183. render_targets[index].target->MarkAsRenderTarget(false, NO_RT);
  184. render_targets[index].target = surface_view.first;
  185. render_targets[index].view = surface_view.second;
  186. if (render_targets[index].target)
  187. render_targets[index].target->MarkAsRenderTarget(true, static_cast<u32>(index));
  188. return surface_view.second;
  189. }
  190. void MarkColorBufferInUse(std::size_t index) {
  191. if (auto& render_target = render_targets[index].target) {
  192. render_target->MarkAsModified(true, Tick());
  193. }
  194. }
  195. void MarkDepthBufferInUse() {
  196. if (depth_buffer.target) {
  197. depth_buffer.target->MarkAsModified(true, Tick());
  198. }
  199. }
  200. void SetEmptyDepthBuffer() {
  201. if (depth_buffer.target == nullptr) {
  202. return;
  203. }
  204. depth_buffer.target->MarkAsRenderTarget(false, NO_RT);
  205. depth_buffer.target = nullptr;
  206. depth_buffer.view = nullptr;
  207. }
  208. void SetEmptyColorBuffer(std::size_t index) {
  209. if (render_targets[index].target == nullptr) {
  210. return;
  211. }
  212. render_targets[index].target->MarkAsRenderTarget(false, NO_RT);
  213. render_targets[index].target = nullptr;
  214. render_targets[index].view = nullptr;
  215. }
  216. void DoFermiCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src_config,
  217. const Tegra::Engines::Fermi2D::Regs::Surface& dst_config,
  218. const Tegra::Engines::Fermi2D::Config& copy_config) {
  219. std::lock_guard lock{mutex};
  220. SurfaceParams src_params = SurfaceParams::CreateForFermiCopySurface(src_config);
  221. SurfaceParams dst_params = SurfaceParams::CreateForFermiCopySurface(dst_config);
  222. const GPUVAddr src_gpu_addr = src_config.Address();
  223. const GPUVAddr dst_gpu_addr = dst_config.Address();
  224. DeduceBestBlit(src_params, dst_params, src_gpu_addr, dst_gpu_addr);
  225. const auto dst_host_ptr{system.GPU().MemoryManager().GetPointer(dst_gpu_addr)};
  226. const auto dst_cache_addr{ToCacheAddr(dst_host_ptr)};
  227. const auto src_host_ptr{system.GPU().MemoryManager().GetPointer(src_gpu_addr)};
  228. const auto src_cache_addr{ToCacheAddr(src_host_ptr)};
  229. std::pair<TSurface, TView> dst_surface =
  230. GetSurface(dst_gpu_addr, dst_cache_addr, dst_params, true, false);
  231. std::pair<TSurface, TView> src_surface =
  232. GetSurface(src_gpu_addr, src_cache_addr, src_params, true, false);
  233. ImageBlit(src_surface.second, dst_surface.second, copy_config);
  234. dst_surface.first->MarkAsModified(true, Tick());
  235. }
  236. TSurface TryFindFramebufferSurface(const u8* host_ptr) {
  237. const CacheAddr cache_addr = ToCacheAddr(host_ptr);
  238. if (!cache_addr) {
  239. return nullptr;
  240. }
  241. const CacheAddr page = cache_addr >> registry_page_bits;
  242. std::vector<TSurface>& list = registry[page];
  243. for (auto& surface : list) {
  244. if (surface->GetCacheAddr() == cache_addr) {
  245. return surface;
  246. }
  247. }
  248. return nullptr;
  249. }
  250. u64 Tick() {
  251. return ++ticks;
  252. }
  253. protected:
  254. TextureCache(Core::System& system, VideoCore::RasterizerInterface& rasterizer)
  255. : system{system}, rasterizer{rasterizer} {
  256. for (std::size_t i = 0; i < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets; i++) {
  257. SetEmptyColorBuffer(i);
  258. }
  259. SetEmptyDepthBuffer();
  260. staging_cache.SetSize(2);
  261. const auto make_siblings = [this](PixelFormat a, PixelFormat b) {
  262. siblings_table[static_cast<std::size_t>(a)] = b;
  263. siblings_table[static_cast<std::size_t>(b)] = a;
  264. };
  265. std::fill(siblings_table.begin(), siblings_table.end(), PixelFormat::Invalid);
  266. make_siblings(PixelFormat::Z16, PixelFormat::R16U);
  267. make_siblings(PixelFormat::Z32F, PixelFormat::R32F);
  268. make_siblings(PixelFormat::Z32FS8, PixelFormat::RG32F);
  269. sampled_textures.reserve(64);
  270. }
  271. ~TextureCache() = default;
  272. virtual TSurface CreateSurface(GPUVAddr gpu_addr, const SurfaceParams& params) = 0;
  273. virtual void ImageCopy(TSurface& src_surface, TSurface& dst_surface,
  274. const CopyParams& copy_params) = 0;
  275. virtual void ImageBlit(TView& src_view, TView& dst_view,
  276. const Tegra::Engines::Fermi2D::Config& copy_config) = 0;
  277. // Depending on the backend, a buffer copy can be slow as it means deoptimizing the texture
  278. // and reading it from a separate buffer.
  279. virtual void BufferCopy(TSurface& src_surface, TSurface& dst_surface) = 0;
  280. void ManageRenderTargetUnregister(TSurface& surface) {
  281. auto& maxwell3d = system.GPU().Maxwell3D();
  282. const u32 index = surface->GetRenderTarget();
  283. if (index == DEPTH_RT) {
  284. maxwell3d.dirty.depth_buffer = true;
  285. } else {
  286. maxwell3d.dirty.render_target[index] = true;
  287. }
  288. maxwell3d.dirty.render_settings = true;
  289. }
  290. void Register(TSurface surface) {
  291. const GPUVAddr gpu_addr = surface->GetGpuAddr();
  292. const CacheAddr cache_ptr = ToCacheAddr(system.GPU().MemoryManager().GetPointer(gpu_addr));
  293. const std::size_t size = surface->GetSizeInBytes();
  294. const std::optional<VAddr> cpu_addr =
  295. system.GPU().MemoryManager().GpuToCpuAddress(gpu_addr);
  296. if (!cache_ptr || !cpu_addr) {
  297. LOG_CRITICAL(HW_GPU, "Failed to register surface with unmapped gpu_address 0x{:016x}",
  298. gpu_addr);
  299. return;
  300. }
  301. const bool continuous = system.GPU().MemoryManager().IsBlockContinuous(gpu_addr, size);
  302. surface->MarkAsContinuous(continuous);
  303. surface->SetCacheAddr(cache_ptr);
  304. surface->SetCpuAddr(*cpu_addr);
  305. RegisterInnerCache(surface);
  306. surface->MarkAsRegistered(true);
  307. rasterizer.UpdatePagesCachedCount(*cpu_addr, size, 1);
  308. }
  309. void Unregister(TSurface surface) {
  310. if (guard_render_targets && surface->IsProtected()) {
  311. return;
  312. }
  313. if (!guard_render_targets && surface->IsRenderTarget()) {
  314. ManageRenderTargetUnregister(surface);
  315. }
  316. const std::size_t size = surface->GetSizeInBytes();
  317. const VAddr cpu_addr = surface->GetCpuAddr();
  318. rasterizer.UpdatePagesCachedCount(cpu_addr, size, -1);
  319. UnregisterInnerCache(surface);
  320. surface->MarkAsRegistered(false);
  321. ReserveSurface(surface->GetSurfaceParams(), surface);
  322. }
  323. TSurface GetUncachedSurface(const GPUVAddr gpu_addr, const SurfaceParams& params) {
  324. if (const auto surface = TryGetReservedSurface(params); surface) {
  325. surface->SetGpuAddr(gpu_addr);
  326. return surface;
  327. }
  328. // No reserved surface available, create a new one and reserve it
  329. auto new_surface{CreateSurface(gpu_addr, params)};
  330. return new_surface;
  331. }
  332. Core::System& system;
  333. private:
  334. enum class RecycleStrategy : u32 {
  335. Ignore = 0,
  336. Flush = 1,
  337. BufferCopy = 3,
  338. };
  339. enum class DeductionType : u32 {
  340. DeductionComplete,
  341. DeductionIncomplete,
  342. DeductionFailed,
  343. };
  344. struct Deduction {
  345. DeductionType type{DeductionType::DeductionFailed};
  346. TSurface surface{};
  347. bool Failed() const {
  348. return type == DeductionType::DeductionFailed;
  349. }
  350. bool Incomplete() const {
  351. return type == DeductionType::DeductionIncomplete;
  352. }
  353. bool IsDepth() const {
  354. return surface->GetSurfaceParams().IsPixelFormatZeta();
  355. }
  356. };
  357. /**
  358. * Takes care of selecting a proper strategy to deal with a texture recycle.
  359. *
  360. * @param overlaps The overlapping surfaces registered in the cache.
  361. * @param params The parameters on the new surface.
  362. * @param gpu_addr The starting address of the new surface.
  363. * @param untopological Indicates to the recycler that the texture has no way
  364. * to match the overlaps due to topological reasons.
  365. **/
  366. RecycleStrategy PickStrategy(std::vector<TSurface>& overlaps, const SurfaceParams& params,
  367. const GPUVAddr gpu_addr, const MatchTopologyResult untopological) {
  368. if (Settings::values.use_accurate_gpu_emulation) {
  369. return RecycleStrategy::Flush;
  370. }
  371. // 3D Textures decision
  372. if (params.block_depth > 1 || params.target == SurfaceTarget::Texture3D) {
  373. return RecycleStrategy::Flush;
  374. }
  375. for (const auto& s : overlaps) {
  376. const auto& s_params = s->GetSurfaceParams();
  377. if (s_params.block_depth > 1 || s_params.target == SurfaceTarget::Texture3D) {
  378. return RecycleStrategy::Flush;
  379. }
  380. }
  381. // Untopological decision
  382. if (untopological == MatchTopologyResult::CompressUnmatch) {
  383. return RecycleStrategy::Flush;
  384. }
  385. if (untopological == MatchTopologyResult::FullMatch && !params.is_tiled) {
  386. return RecycleStrategy::Flush;
  387. }
  388. return RecycleStrategy::Ignore;
  389. }
  390. /**
  391. * Used to decide what to do with textures we can't resolve in the cache It has 2 implemented
  392. * strategies: Ignore and Flush.
  393. *
  394. * - Ignore: Just unregisters all the overlaps and loads the new texture.
  395. * - Flush: Flushes all the overlaps into memory and loads the new surface from that data.
  396. *
  397. * @param overlaps The overlapping surfaces registered in the cache.
  398. * @param params The parameters for the new surface.
  399. * @param gpu_addr The starting address of the new surface.
  400. * @param preserve_contents Indicates that the new surface should be loaded from memory or left
  401. * blank.
  402. * @param untopological Indicates to the recycler that the texture has no way to match the
  403. * overlaps due to topological reasons.
  404. **/
  405. std::pair<TSurface, TView> RecycleSurface(std::vector<TSurface>& overlaps,
  406. const SurfaceParams& params, const GPUVAddr gpu_addr,
  407. const bool preserve_contents,
  408. const MatchTopologyResult untopological) {
  409. const bool do_load = preserve_contents && Settings::values.use_accurate_gpu_emulation;
  410. for (auto& surface : overlaps) {
  411. Unregister(surface);
  412. }
  413. switch (PickStrategy(overlaps, params, gpu_addr, untopological)) {
  414. case RecycleStrategy::Ignore: {
  415. return InitializeSurface(gpu_addr, params, do_load);
  416. }
  417. case RecycleStrategy::Flush: {
  418. std::sort(overlaps.begin(), overlaps.end(),
  419. [](const TSurface& a, const TSurface& b) -> bool {
  420. return a->GetModificationTick() < b->GetModificationTick();
  421. });
  422. for (auto& surface : overlaps) {
  423. FlushSurface(surface);
  424. }
  425. return InitializeSurface(gpu_addr, params, preserve_contents);
  426. }
  427. case RecycleStrategy::BufferCopy: {
  428. auto new_surface = GetUncachedSurface(gpu_addr, params);
  429. BufferCopy(overlaps[0], new_surface);
  430. return {new_surface, new_surface->GetMainView()};
  431. }
  432. default: {
  433. UNIMPLEMENTED_MSG("Unimplemented Texture Cache Recycling Strategy!");
  434. return InitializeSurface(gpu_addr, params, do_load);
  435. }
  436. }
  437. }
  438. /**
  439. * Takes a single surface and recreates into another that may differ in
  440. * format, target or width alignment.
  441. *
  442. * @param current_surface The registered surface in the cache which we want to convert.
  443. * @param params The new surface params which we'll use to recreate the surface.
  444. * @param is_render Whether or not the surface is a render target.
  445. **/
  446. std::pair<TSurface, TView> RebuildSurface(TSurface current_surface, const SurfaceParams& params,
  447. bool is_render) {
  448. const auto gpu_addr = current_surface->GetGpuAddr();
  449. const auto& cr_params = current_surface->GetSurfaceParams();
  450. TSurface new_surface;
  451. if (cr_params.pixel_format != params.pixel_format && !is_render &&
  452. GetSiblingFormat(cr_params.pixel_format) == params.pixel_format) {
  453. SurfaceParams new_params = params;
  454. new_params.pixel_format = cr_params.pixel_format;
  455. new_params.type = cr_params.type;
  456. new_surface = GetUncachedSurface(gpu_addr, new_params);
  457. } else {
  458. new_surface = GetUncachedSurface(gpu_addr, params);
  459. }
  460. const auto& final_params = new_surface->GetSurfaceParams();
  461. if (cr_params.type != final_params.type) {
  462. BufferCopy(current_surface, new_surface);
  463. } else {
  464. std::vector<CopyParams> bricks = current_surface->BreakDown(final_params);
  465. for (auto& brick : bricks) {
  466. ImageCopy(current_surface, new_surface, brick);
  467. }
  468. }
  469. Unregister(current_surface);
  470. Register(new_surface);
  471. new_surface->MarkAsModified(current_surface->IsModified(), Tick());
  472. return {new_surface, new_surface->GetMainView()};
  473. }
  474. /**
  475. * Takes a single surface and checks with the new surface's params if it's an exact
  476. * match, we return the main view of the registered surface. If its formats don't
  477. * match, we rebuild the surface. We call this last method a `Mirage`. If formats
  478. * match but the targets don't, we create an overview View of the registered surface.
  479. *
  480. * @param current_surface The registered surface in the cache which we want to convert.
  481. * @param params The new surface params which we want to check.
  482. * @param is_render Whether or not the surface is a render target.
  483. **/
  484. std::pair<TSurface, TView> ManageStructuralMatch(TSurface current_surface,
  485. const SurfaceParams& params, bool is_render) {
  486. const bool is_mirage = !current_surface->MatchFormat(params.pixel_format);
  487. const bool matches_target = current_surface->MatchTarget(params.target);
  488. const auto match_check = [&]() -> std::pair<TSurface, TView> {
  489. if (matches_target) {
  490. return {current_surface, current_surface->GetMainView()};
  491. }
  492. return {current_surface, current_surface->EmplaceOverview(params)};
  493. };
  494. if (!is_mirage) {
  495. return match_check();
  496. }
  497. if (!is_render && GetSiblingFormat(current_surface->GetFormat()) == params.pixel_format) {
  498. return match_check();
  499. }
  500. return RebuildSurface(current_surface, params, is_render);
  501. }
  502. /**
  503. * Unlike RebuildSurface where we know whether or not registered surfaces match the candidate
  504. * in some way, we have no guarantees here. We try to see if the overlaps are sublayers/mipmaps
  505. * of the new surface, if they all match we end up recreating a surface for them,
  506. * else we return nothing.
  507. *
  508. * @param overlaps The overlapping surfaces registered in the cache.
  509. * @param params The parameters on the new surface.
  510. * @param gpu_addr The starting address of the new surface.
  511. **/
  512. std::optional<std::pair<TSurface, TView>> TryReconstructSurface(std::vector<TSurface>& overlaps,
  513. const SurfaceParams& params,
  514. const GPUVAddr gpu_addr) {
  515. if (params.target == SurfaceTarget::Texture3D) {
  516. return {};
  517. }
  518. bool modified = false;
  519. TSurface new_surface = GetUncachedSurface(gpu_addr, params);
  520. u32 passed_tests = 0;
  521. for (auto& surface : overlaps) {
  522. const SurfaceParams& src_params = surface->GetSurfaceParams();
  523. if (src_params.is_layered || src_params.num_levels > 1) {
  524. // We send this cases to recycle as they are more complex to handle
  525. return {};
  526. }
  527. const std::size_t candidate_size = surface->GetSizeInBytes();
  528. auto mipmap_layer{new_surface->GetLayerMipmap(surface->GetGpuAddr())};
  529. if (!mipmap_layer) {
  530. continue;
  531. }
  532. const auto [layer, mipmap] = *mipmap_layer;
  533. if (new_surface->GetMipmapSize(mipmap) != candidate_size) {
  534. continue;
  535. }
  536. modified |= surface->IsModified();
  537. // Now we got all the data set up
  538. const u32 width = SurfaceParams::IntersectWidth(src_params, params, 0, mipmap);
  539. const u32 height = SurfaceParams::IntersectHeight(src_params, params, 0, mipmap);
  540. const CopyParams copy_params(0, 0, 0, 0, 0, layer, 0, mipmap, width, height, 1);
  541. passed_tests++;
  542. ImageCopy(surface, new_surface, copy_params);
  543. }
  544. if (passed_tests == 0) {
  545. return {};
  546. // In Accurate GPU all tests should pass, else we recycle
  547. } else if (Settings::values.use_accurate_gpu_emulation && passed_tests != overlaps.size()) {
  548. return {};
  549. }
  550. for (const auto& surface : overlaps) {
  551. Unregister(surface);
  552. }
  553. new_surface->MarkAsModified(modified, Tick());
  554. Register(new_surface);
  555. return {{new_surface, new_surface->GetMainView()}};
  556. }
  557. /**
  558. * Takes care of managing 3D textures and its slices. Does some HLE methods when possible.
  559. * Fallsback to LLE when it isn't possible.
  560. *
  561. * @param overlaps The overlapping surfaces registered in the cache.
  562. * @param params The parameters on the new surface.
  563. * @param gpu_addr The starting address of the new surface.
  564. * @param cache_addr The starting address of the new surface on physical memory.
  565. * @param preserve_contents Indicates that the new surface should be loaded from memory or
  566. * left blank.
  567. */
  568. std::optional<std::pair<TSurface, TView>> Manage3DSurfaces(std::vector<TSurface>& overlaps,
  569. const SurfaceParams& params,
  570. const GPUVAddr gpu_addr,
  571. const CacheAddr cache_addr,
  572. bool preserve_contents) {
  573. if (params.target == SurfaceTarget::Texture3D) {
  574. bool failed = false;
  575. if (params.num_levels > 1) {
  576. // We can't handle mipmaps in 3D textures yet, better fallback to LLE approach
  577. return std::nullopt;
  578. }
  579. TSurface new_surface = GetUncachedSurface(gpu_addr, params);
  580. bool modified = false;
  581. for (auto& surface : overlaps) {
  582. const SurfaceParams& src_params = surface->GetSurfaceParams();
  583. if (src_params.target != SurfaceTarget::Texture2D) {
  584. failed = true;
  585. break;
  586. }
  587. if (src_params.height != params.height) {
  588. failed = true;
  589. break;
  590. }
  591. if (src_params.block_depth != params.block_depth ||
  592. src_params.block_height != params.block_height) {
  593. failed = true;
  594. break;
  595. }
  596. const u32 offset = static_cast<u32>(surface->GetCacheAddr() - cache_addr);
  597. const auto [x, y, z] = params.GetBlockOffsetXYZ(offset);
  598. modified |= surface->IsModified();
  599. const CopyParams copy_params(0, 0, 0, 0, 0, z, 0, 0, params.width, params.height,
  600. 1);
  601. ImageCopy(surface, new_surface, copy_params);
  602. }
  603. if (failed) {
  604. return std::nullopt;
  605. }
  606. for (const auto& surface : overlaps) {
  607. Unregister(surface);
  608. }
  609. new_surface->MarkAsModified(modified, Tick());
  610. Register(new_surface);
  611. auto view = new_surface->GetMainView();
  612. return {{std::move(new_surface), view}};
  613. } else {
  614. for (const auto& surface : overlaps) {
  615. if (!surface->MatchTarget(params.target)) {
  616. if (overlaps.size() == 1 && surface->GetCacheAddr() == cache_addr) {
  617. if (Settings::values.use_accurate_gpu_emulation) {
  618. return std::nullopt;
  619. }
  620. Unregister(surface);
  621. return InitializeSurface(gpu_addr, params, preserve_contents);
  622. }
  623. return std::nullopt;
  624. }
  625. if (surface->GetCacheAddr() != cache_addr) {
  626. continue;
  627. }
  628. if (surface->MatchesStructure(params) == MatchStructureResult::FullMatch) {
  629. return {{surface, surface->GetMainView()}};
  630. }
  631. }
  632. return InitializeSurface(gpu_addr, params, preserve_contents);
  633. }
  634. }
  635. /**
  636. * Gets the starting address and parameters of a candidate surface and tries
  637. * to find a matching surface within the cache. This is done in 3 big steps:
  638. *
  639. * 1. Check the 1st Level Cache in order to find an exact match, if we fail, we move to step 2.
  640. *
  641. * 2. Check if there are any overlaps at all, if there are none, we just load the texture from
  642. * memory else we move to step 3.
  643. *
  644. * 3. Consists of figuring out the relationship between the candidate texture and the
  645. * overlaps. We divide the scenarios depending if there's 1 or many overlaps. If
  646. * there's many, we just try to reconstruct a new surface out of them based on the
  647. * candidate's parameters, if we fail, we recycle. When there's only 1 overlap then we
  648. * have to check if the candidate is a view (layer/mipmap) of the overlap or if the
  649. * registered surface is a mipmap/layer of the candidate. In this last case we reconstruct
  650. * a new surface.
  651. *
  652. * @param gpu_addr The starting address of the candidate surface.
  653. * @param params The parameters on the candidate surface.
  654. * @param preserve_contents Indicates that the new surface should be loaded from memory or
  655. * left blank.
  656. * @param is_render Whether or not the surface is a render target.
  657. **/
  658. std::pair<TSurface, TView> GetSurface(const GPUVAddr gpu_addr, const CacheAddr cache_addr,
  659. const SurfaceParams& params, bool preserve_contents,
  660. bool is_render) {
  661. // Step 1
  662. // Check Level 1 Cache for a fast structural match. If candidate surface
  663. // matches at certain level we are pretty much done.
  664. if (const auto iter = l1_cache.find(cache_addr); iter != l1_cache.end()) {
  665. TSurface& current_surface = iter->second;
  666. const auto topological_result = current_surface->MatchesTopology(params);
  667. if (topological_result != MatchTopologyResult::FullMatch) {
  668. std::vector<TSurface> overlaps{current_surface};
  669. return RecycleSurface(overlaps, params, gpu_addr, preserve_contents,
  670. topological_result);
  671. }
  672. const auto struct_result = current_surface->MatchesStructure(params);
  673. if (struct_result != MatchStructureResult::None &&
  674. (params.target != SurfaceTarget::Texture3D ||
  675. current_surface->MatchTarget(params.target))) {
  676. if (struct_result == MatchStructureResult::FullMatch) {
  677. return ManageStructuralMatch(current_surface, params, is_render);
  678. } else {
  679. return RebuildSurface(current_surface, params, is_render);
  680. }
  681. }
  682. }
  683. // Step 2
  684. // Obtain all possible overlaps in the memory region
  685. const std::size_t candidate_size = params.GetGuestSizeInBytes();
  686. auto overlaps{GetSurfacesInRegion(cache_addr, candidate_size)};
  687. // If none are found, we are done. we just load the surface and create it.
  688. if (overlaps.empty()) {
  689. return InitializeSurface(gpu_addr, params, preserve_contents);
  690. }
  691. // Step 3
  692. // Now we need to figure the relationship between the texture and its overlaps
  693. // we do a topological test to ensure we can find some relationship. If it fails
  694. // immediately recycle the texture
  695. for (const auto& surface : overlaps) {
  696. const auto topological_result = surface->MatchesTopology(params);
  697. if (topological_result != MatchTopologyResult::FullMatch) {
  698. return RecycleSurface(overlaps, params, gpu_addr, preserve_contents,
  699. topological_result);
  700. }
  701. }
  702. // Look if it's a 3D texture
  703. if (params.block_depth > 0) {
  704. auto surface =
  705. Manage3DSurfaces(overlaps, params, gpu_addr, cache_addr, preserve_contents);
  706. if (surface) {
  707. return *surface;
  708. }
  709. }
  710. // Split cases between 1 overlap or many.
  711. if (overlaps.size() == 1) {
  712. TSurface current_surface = overlaps[0];
  713. // First check if the surface is within the overlap. If not, it means
  714. // two things either the candidate surface is a supertexture of the overlap
  715. // or they don't match in any known way.
  716. if (!current_surface->IsInside(gpu_addr, gpu_addr + candidate_size)) {
  717. if (current_surface->GetGpuAddr() == gpu_addr) {
  718. std::optional<std::pair<TSurface, TView>> view =
  719. TryReconstructSurface(overlaps, params, gpu_addr);
  720. if (view) {
  721. return *view;
  722. }
  723. }
  724. return RecycleSurface(overlaps, params, gpu_addr, preserve_contents,
  725. MatchTopologyResult::FullMatch);
  726. }
  727. // Now we check if the candidate is a mipmap/layer of the overlap
  728. std::optional<TView> view =
  729. current_surface->EmplaceView(params, gpu_addr, candidate_size);
  730. if (view) {
  731. const bool is_mirage = !current_surface->MatchFormat(params.pixel_format);
  732. if (is_mirage) {
  733. // On a mirage view, we need to recreate the surface under this new view
  734. // and then obtain a view again.
  735. SurfaceParams new_params = current_surface->GetSurfaceParams();
  736. const u32 wh = SurfaceParams::ConvertWidth(
  737. new_params.width, new_params.pixel_format, params.pixel_format);
  738. const u32 hh = SurfaceParams::ConvertHeight(
  739. new_params.height, new_params.pixel_format, params.pixel_format);
  740. new_params.width = wh;
  741. new_params.height = hh;
  742. new_params.pixel_format = params.pixel_format;
  743. std::pair<TSurface, TView> pair =
  744. RebuildSurface(current_surface, new_params, is_render);
  745. std::optional<TView> mirage_view =
  746. pair.first->EmplaceView(params, gpu_addr, candidate_size);
  747. if (mirage_view)
  748. return {pair.first, *mirage_view};
  749. return RecycleSurface(overlaps, params, gpu_addr, preserve_contents,
  750. MatchTopologyResult::FullMatch);
  751. }
  752. return {current_surface, *view};
  753. }
  754. } else {
  755. // If there are many overlaps, odds are they are subtextures of the candidate
  756. // surface. We try to construct a new surface based on the candidate parameters,
  757. // using the overlaps. If a single overlap fails, this will fail.
  758. std::optional<std::pair<TSurface, TView>> view =
  759. TryReconstructSurface(overlaps, params, gpu_addr);
  760. if (view) {
  761. return *view;
  762. }
  763. }
  764. // We failed all the tests, recycle the overlaps into a new texture.
  765. return RecycleSurface(overlaps, params, gpu_addr, preserve_contents,
  766. MatchTopologyResult::FullMatch);
  767. }
  768. /**
  769. * Gets the starting address and parameters of a candidate surface and tries to find a
  770. * matching surface within the cache that's similar to it. If there are many textures
  771. * or the texture found if entirely incompatible, it will fail. If no texture is found, the
  772. * blit will be unsuccessful.
  773. *
  774. * @param gpu_addr The starting address of the candidate surface.
  775. * @param params The parameters on the candidate surface.
  776. **/
  777. Deduction DeduceSurface(const GPUVAddr gpu_addr, const SurfaceParams& params) {
  778. const auto host_ptr{system.GPU().MemoryManager().GetPointer(gpu_addr)};
  779. const auto cache_addr{ToCacheAddr(host_ptr)};
  780. if (!cache_addr) {
  781. Deduction result{};
  782. result.type = DeductionType::DeductionFailed;
  783. return result;
  784. }
  785. if (const auto iter = l1_cache.find(cache_addr); iter != l1_cache.end()) {
  786. TSurface& current_surface = iter->second;
  787. const auto topological_result = current_surface->MatchesTopology(params);
  788. if (topological_result != MatchTopologyResult::FullMatch) {
  789. Deduction result{};
  790. result.type = DeductionType::DeductionFailed;
  791. return result;
  792. }
  793. const auto struct_result = current_surface->MatchesStructure(params);
  794. if (struct_result != MatchStructureResult::None &&
  795. current_surface->MatchTarget(params.target)) {
  796. Deduction result{};
  797. result.type = DeductionType::DeductionComplete;
  798. result.surface = current_surface;
  799. return result;
  800. }
  801. }
  802. const std::size_t candidate_size = params.GetGuestSizeInBytes();
  803. auto overlaps{GetSurfacesInRegion(cache_addr, candidate_size)};
  804. if (overlaps.empty()) {
  805. Deduction result{};
  806. result.type = DeductionType::DeductionIncomplete;
  807. return result;
  808. }
  809. if (overlaps.size() > 1) {
  810. Deduction result{};
  811. result.type = DeductionType::DeductionFailed;
  812. return result;
  813. } else {
  814. Deduction result{};
  815. result.type = DeductionType::DeductionComplete;
  816. result.surface = overlaps[0];
  817. return result;
  818. }
  819. }
  820. /**
  821. * Gets a null surface based on a target texture.
  822. * @param target The target of the null surface.
  823. */
  824. TView GetNullSurface(SurfaceTarget target) {
  825. const u32 i_target = static_cast<u32>(target);
  826. if (const auto it = invalid_cache.find(i_target); it != invalid_cache.end()) {
  827. return it->second->GetMainView();
  828. }
  829. SurfaceParams params{};
  830. params.target = target;
  831. params.is_tiled = false;
  832. params.srgb_conversion = false;
  833. params.is_layered = false;
  834. params.block_width = 0;
  835. params.block_height = 0;
  836. params.block_depth = 0;
  837. params.tile_width_spacing = 1;
  838. params.width = 1;
  839. params.height = 1;
  840. params.depth = 1;
  841. params.pitch = 4;
  842. params.num_levels = 1;
  843. params.emulated_levels = 1;
  844. params.pixel_format = VideoCore::Surface::PixelFormat::RGBA16F;
  845. params.type = VideoCore::Surface::SurfaceType::ColorTexture;
  846. auto surface = CreateSurface(0ULL, params);
  847. invalid_memory.clear();
  848. invalid_memory.resize(surface->GetHostSizeInBytes(), 0U);
  849. surface->UploadTexture(invalid_memory);
  850. surface->MarkAsModified(false, Tick());
  851. invalid_cache.emplace(i_target, surface);
  852. return surface->GetMainView();
  853. }
  854. /**
  855. * Gets the a source and destination starting address and parameters,
  856. * and tries to deduce if they are supposed to be depth textures. If so, their
  857. * parameters are modified and fixed into so.
  858. *
  859. * @param src_params The parameters of the candidate surface.
  860. * @param dst_params The parameters of the destination surface.
  861. * @param src_gpu_addr The starting address of the candidate surface.
  862. * @param dst_gpu_addr The starting address of the destination surface.
  863. **/
  864. void DeduceBestBlit(SurfaceParams& src_params, SurfaceParams& dst_params,
  865. const GPUVAddr src_gpu_addr, const GPUVAddr dst_gpu_addr) {
  866. auto deduced_src = DeduceSurface(src_gpu_addr, src_params);
  867. auto deduced_dst = DeduceSurface(src_gpu_addr, src_params);
  868. if (deduced_src.Failed() || deduced_dst.Failed()) {
  869. return;
  870. }
  871. const bool incomplete_src = deduced_src.Incomplete();
  872. const bool incomplete_dst = deduced_dst.Incomplete();
  873. if (incomplete_src && incomplete_dst) {
  874. return;
  875. }
  876. const bool any_incomplete = incomplete_src || incomplete_dst;
  877. if (!any_incomplete) {
  878. if (!(deduced_src.IsDepth() && deduced_dst.IsDepth())) {
  879. return;
  880. }
  881. } else {
  882. if (incomplete_src && !(deduced_dst.IsDepth())) {
  883. return;
  884. }
  885. if (incomplete_dst && !(deduced_src.IsDepth())) {
  886. return;
  887. }
  888. }
  889. const auto inherit_format = [](SurfaceParams& to, TSurface from) {
  890. const SurfaceParams& params = from->GetSurfaceParams();
  891. to.pixel_format = params.pixel_format;
  892. to.type = params.type;
  893. };
  894. // Now we got the cases where one or both is Depth and the other is not known
  895. if (!incomplete_src) {
  896. inherit_format(src_params, deduced_src.surface);
  897. } else {
  898. inherit_format(src_params, deduced_dst.surface);
  899. }
  900. if (!incomplete_dst) {
  901. inherit_format(dst_params, deduced_dst.surface);
  902. } else {
  903. inherit_format(dst_params, deduced_src.surface);
  904. }
  905. }
  906. std::pair<TSurface, TView> InitializeSurface(GPUVAddr gpu_addr, const SurfaceParams& params,
  907. bool preserve_contents) {
  908. auto new_surface{GetUncachedSurface(gpu_addr, params)};
  909. Register(new_surface);
  910. if (preserve_contents) {
  911. LoadSurface(new_surface);
  912. }
  913. return {new_surface, new_surface->GetMainView()};
  914. }
  915. void LoadSurface(const TSurface& surface) {
  916. staging_cache.GetBuffer(0).resize(surface->GetHostSizeInBytes());
  917. surface->LoadBuffer(system.GPU().MemoryManager(), staging_cache);
  918. surface->UploadTexture(staging_cache.GetBuffer(0));
  919. surface->MarkAsModified(false, Tick());
  920. }
  921. void FlushSurface(const TSurface& surface) {
  922. if (!surface->IsModified()) {
  923. return;
  924. }
  925. staging_cache.GetBuffer(0).resize(surface->GetHostSizeInBytes());
  926. surface->DownloadTexture(staging_cache.GetBuffer(0));
  927. surface->FlushBuffer(system.GPU().MemoryManager(), staging_cache);
  928. surface->MarkAsModified(false, Tick());
  929. }
  930. void RegisterInnerCache(TSurface& surface) {
  931. const CacheAddr cache_addr = surface->GetCacheAddr();
  932. CacheAddr start = cache_addr >> registry_page_bits;
  933. const CacheAddr end = (surface->GetCacheAddrEnd() - 1) >> registry_page_bits;
  934. l1_cache[cache_addr] = surface;
  935. while (start <= end) {
  936. registry[start].push_back(surface);
  937. start++;
  938. }
  939. }
  940. void UnregisterInnerCache(TSurface& surface) {
  941. const CacheAddr cache_addr = surface->GetCacheAddr();
  942. CacheAddr start = cache_addr >> registry_page_bits;
  943. const CacheAddr end = (surface->GetCacheAddrEnd() - 1) >> registry_page_bits;
  944. l1_cache.erase(cache_addr);
  945. while (start <= end) {
  946. auto& reg{registry[start]};
  947. reg.erase(std::find(reg.begin(), reg.end(), surface));
  948. start++;
  949. }
  950. }
  951. std::vector<TSurface> GetSurfacesInRegion(const CacheAddr cache_addr, const std::size_t size) {
  952. if (size == 0) {
  953. return {};
  954. }
  955. const CacheAddr cache_addr_end = cache_addr + size;
  956. CacheAddr start = cache_addr >> registry_page_bits;
  957. const CacheAddr end = (cache_addr_end - 1) >> registry_page_bits;
  958. std::vector<TSurface> surfaces;
  959. while (start <= end) {
  960. std::vector<TSurface>& list = registry[start];
  961. for (auto& surface : list) {
  962. if (!surface->IsPicked() && surface->Overlaps(cache_addr, cache_addr_end)) {
  963. surface->MarkAsPicked(true);
  964. surfaces.push_back(surface);
  965. }
  966. }
  967. start++;
  968. }
  969. for (auto& surface : surfaces) {
  970. surface->MarkAsPicked(false);
  971. }
  972. return surfaces;
  973. }
  974. void ReserveSurface(const SurfaceParams& params, TSurface surface) {
  975. surface_reserve[params].push_back(std::move(surface));
  976. }
  977. TSurface TryGetReservedSurface(const SurfaceParams& params) {
  978. auto search{surface_reserve.find(params)};
  979. if (search == surface_reserve.end()) {
  980. return {};
  981. }
  982. for (auto& surface : search->second) {
  983. if (!surface->IsRegistered()) {
  984. return surface;
  985. }
  986. }
  987. return {};
  988. }
  989. constexpr PixelFormat GetSiblingFormat(PixelFormat format) const {
  990. return siblings_table[static_cast<std::size_t>(format)];
  991. }
  992. struct FramebufferTargetInfo {
  993. TSurface target;
  994. TView view;
  995. };
  996. VideoCore::RasterizerInterface& rasterizer;
  997. FormatLookupTable format_lookup_table;
  998. u64 ticks{};
  999. // Guards the cache for protection conflicts.
  1000. bool guard_render_targets{};
  1001. bool guard_samplers{};
  1002. // The siblings table is for formats that can inter exchange with one another
  1003. // without causing issues. This is only valid when a conflict occurs on a non
  1004. // rendering use.
  1005. std::array<PixelFormat, static_cast<std::size_t>(PixelFormat::Max)> siblings_table;
  1006. // The internal Cache is different for the Texture Cache. It's based on buckets
  1007. // of 1MB. This fits better for the purpose of this cache as textures are normaly
  1008. // large in size.
  1009. static constexpr u64 registry_page_bits{20};
  1010. static constexpr u64 registry_page_size{1 << registry_page_bits};
  1011. std::unordered_map<CacheAddr, std::vector<TSurface>> registry;
  1012. static constexpr u32 DEPTH_RT = 8;
  1013. static constexpr u32 NO_RT = 0xFFFFFFFF;
  1014. // The L1 Cache is used for fast texture lookup before checking the overlaps
  1015. // This avoids calculating size and other stuffs.
  1016. std::unordered_map<CacheAddr, TSurface> l1_cache;
  1017. /// The surface reserve is a "backup" cache, this is where we put unique surfaces that have
  1018. /// previously been used. This is to prevent surfaces from being constantly created and
  1019. /// destroyed when used with different surface parameters.
  1020. std::unordered_map<SurfaceParams, std::vector<TSurface>> surface_reserve;
  1021. std::array<FramebufferTargetInfo, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets>
  1022. render_targets;
  1023. FramebufferTargetInfo depth_buffer;
  1024. std::vector<TSurface> sampled_textures;
  1025. /// This cache stores null surfaces in order to be used as a placeholder
  1026. /// for invalid texture calls.
  1027. std::unordered_map<u32, TSurface> invalid_cache;
  1028. std::vector<u8> invalid_memory;
  1029. StagingCache staging_cache;
  1030. std::recursive_mutex mutex;
  1031. };
  1032. } // namespace VideoCommon