texture_cache.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 <list>
  6. #include <memory>
  7. #include <set>
  8. #include <tuple>
  9. #include <type_traits>
  10. #include <unordered_map>
  11. #include <boost/icl/interval_map.hpp>
  12. #include <boost/range/iterator_range.hpp>
  13. #include "common/assert.h"
  14. #include "common/common_types.h"
  15. #include "core/memory.h"
  16. #include "video_core/engines/fermi_2d.h"
  17. #include "video_core/engines/maxwell_3d.h"
  18. #include "video_core/gpu.h"
  19. #include "video_core/memory_manager.h"
  20. #include "video_core/rasterizer_interface.h"
  21. #include "video_core/surface.h"
  22. namespace Core {
  23. class System;
  24. }
  25. namespace Tegra::Texture {
  26. struct FullTextureInfo;
  27. }
  28. namespace VideoCore {
  29. class RasterizerInterface;
  30. }
  31. namespace VideoCommon {
  32. class HasheableSurfaceParams {
  33. public:
  34. std::size_t Hash() const;
  35. bool operator==(const HasheableSurfaceParams& rhs) const;
  36. bool operator!=(const HasheableSurfaceParams& rhs) const {
  37. return !operator==(rhs);
  38. }
  39. protected:
  40. // Avoid creation outside of a managed environment.
  41. HasheableSurfaceParams() = default;
  42. bool is_tiled;
  43. bool srgb_conversion;
  44. u32 block_width;
  45. u32 block_height;
  46. u32 block_depth;
  47. u32 tile_width_spacing;
  48. u32 width;
  49. u32 height;
  50. u32 depth;
  51. u32 pitch;
  52. u32 unaligned_height;
  53. u32 num_levels;
  54. VideoCore::Surface::PixelFormat pixel_format;
  55. VideoCore::Surface::ComponentType component_type;
  56. VideoCore::Surface::SurfaceType type;
  57. VideoCore::Surface::SurfaceTarget target;
  58. };
  59. class SurfaceParams final : public HasheableSurfaceParams {
  60. public:
  61. /// Creates SurfaceCachedParams from a texture configuration.
  62. static SurfaceParams CreateForTexture(Core::System& system,
  63. const Tegra::Texture::FullTextureInfo& config);
  64. /// Creates SurfaceCachedParams for a depth buffer configuration.
  65. static SurfaceParams CreateForDepthBuffer(
  66. Core::System& system, u32 zeta_width, u32 zeta_height, Tegra::DepthFormat format,
  67. u32 block_width, u32 block_height, u32 block_depth,
  68. Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout type);
  69. /// Creates SurfaceCachedParams from a framebuffer configuration.
  70. static SurfaceParams CreateForFramebuffer(Core::System& system, std::size_t index);
  71. /// Creates SurfaceCachedParams from a Fermi2D surface configuration.
  72. static SurfaceParams CreateForFermiCopySurface(
  73. const Tegra::Engines::Fermi2D::Regs::Surface& config);
  74. bool IsTiled() const {
  75. return is_tiled;
  76. }
  77. bool GetSrgbConversion() const {
  78. return srgb_conversion;
  79. }
  80. u32 GetBlockWidth() const {
  81. return block_width;
  82. }
  83. u32 GetTileWidthSpacing() const {
  84. return tile_width_spacing;
  85. }
  86. u32 GetWidth() const {
  87. return width;
  88. }
  89. u32 GetHeight() const {
  90. return height;
  91. }
  92. u32 GetDepth() const {
  93. return depth;
  94. }
  95. u32 GetPitch() const {
  96. return pitch;
  97. }
  98. u32 GetNumLevels() const {
  99. return num_levels;
  100. }
  101. VideoCore::Surface::PixelFormat GetPixelFormat() const {
  102. return pixel_format;
  103. }
  104. VideoCore::Surface::ComponentType GetComponentType() const {
  105. return component_type;
  106. }
  107. VideoCore::Surface::SurfaceTarget GetTarget() const {
  108. return target;
  109. }
  110. VideoCore::Surface::SurfaceType GetType() const {
  111. return type;
  112. }
  113. std::size_t GetGuestSizeInBytes() const {
  114. return guest_size_in_bytes;
  115. }
  116. std::size_t GetHostSizeInBytes() const {
  117. return host_size_in_bytes;
  118. }
  119. u32 GetNumLayers() const {
  120. return num_layers;
  121. }
  122. /// Returns the width of a given mipmap level.
  123. u32 GetMipWidth(u32 level) const;
  124. /// Returns the height of a given mipmap level.
  125. u32 GetMipHeight(u32 level) const;
  126. /// Returns the depth of a given mipmap level.
  127. u32 GetMipDepth(u32 level) const;
  128. /// Returns true if these parameters are from a layered surface.
  129. bool IsLayered() const;
  130. /// Returns the block height of a given mipmap level.
  131. u32 GetMipBlockHeight(u32 level) const;
  132. /// Returns the block depth of a given mipmap level.
  133. u32 GetMipBlockDepth(u32 level) const;
  134. /// Returns the offset in bytes in guest memory of a given mipmap level.
  135. std::size_t GetGuestMipmapLevelOffset(u32 level) const;
  136. /// Returns the offset in bytes in host memory (linear) of a given mipmap level.
  137. std::size_t GetHostMipmapLevelOffset(u32 level) const;
  138. /// Returns the size in bytes in host memory (linear) of a given mipmap level.
  139. std::size_t GetHostMipmapSize(u32 level) const;
  140. /// Returns the size of a layer in bytes in guest memory.
  141. std::size_t GetGuestLayerSize() const;
  142. /// Returns the size of a layer in bytes in host memory for a given mipmap level.
  143. std::size_t GetHostLayerSize(u32 level) const;
  144. /// Returns the default block width.
  145. u32 GetDefaultBlockWidth() const;
  146. /// Returns the default block height.
  147. u32 GetDefaultBlockHeight() const;
  148. /// Returns the bits per pixel.
  149. u32 GetBitsPerPixel() const;
  150. /// Returns the bytes per pixel.
  151. u32 GetBytesPerPixel() const;
  152. /// Returns true if another surface can be familiar with this. This is a loosely defined term
  153. /// that reflects the possibility of these two surface parameters potentially being part of a
  154. /// bigger superset.
  155. bool IsFamiliar(const SurfaceParams& view_params) const;
  156. /// Returns true if the pixel format is a depth and/or stencil format.
  157. bool IsPixelFormatZeta() const;
  158. /// Creates a map that redirects an address difference to a layer and mipmap level.
  159. std::map<u64, std::pair<u32, u32>> CreateViewOffsetMap() const;
  160. /// Returns true if the passed surface view parameters is equal or a valid subset of this.
  161. bool IsViewValid(const SurfaceParams& view_params, u32 layer, u32 level) const;
  162. private:
  163. /// Calculates values that can be deduced from HasheableSurfaceParams.
  164. void CalculateCachedValues();
  165. /// Returns the size of a given mipmap level inside a layer.
  166. std::size_t GetInnerMipmapMemorySize(u32 level, bool as_host_size, bool uncompressed) const;
  167. /// Returns the size of all mipmap levels and aligns as needed.
  168. std::size_t GetInnerMemorySize(bool as_host_size, bool layer_only, bool uncompressed) const;
  169. /// Returns the size of a layer
  170. std::size_t GetLayerSize(bool as_host_size, bool uncompressed) const;
  171. /// Returns true if the passed view width and height match the size of this params in a given
  172. /// mipmap level.
  173. bool IsDimensionValid(const SurfaceParams& view_params, u32 level) const;
  174. /// Returns true if the passed view depth match the size of this params in a given mipmap level.
  175. bool IsDepthValid(const SurfaceParams& view_params, u32 level) const;
  176. /// Returns true if the passed view layers and mipmap levels are in bounds.
  177. bool IsInBounds(const SurfaceParams& view_params, u32 layer, u32 level) const;
  178. std::size_t guest_size_in_bytes;
  179. std::size_t host_size_in_bytes;
  180. u32 num_layers;
  181. };
  182. struct ViewKey {
  183. std::size_t Hash() const;
  184. bool operator==(const ViewKey& rhs) const;
  185. u32 base_layer{};
  186. u32 num_layers{};
  187. u32 base_level{};
  188. u32 num_levels{};
  189. };
  190. } // namespace VideoCommon
  191. namespace std {
  192. template <>
  193. struct hash<VideoCommon::SurfaceParams> {
  194. std::size_t operator()(const VideoCommon::SurfaceParams& k) const noexcept {
  195. return k.Hash();
  196. }
  197. };
  198. template <>
  199. struct hash<VideoCommon::ViewKey> {
  200. std::size_t operator()(const VideoCommon::ViewKey& k) const noexcept {
  201. return k.Hash();
  202. }
  203. };
  204. } // namespace std
  205. namespace VideoCommon {
  206. template <typename TTextureCache, typename TView, typename TExecutionContext>
  207. class SurfaceBase {
  208. static_assert(std::is_trivially_copyable_v<TExecutionContext>);
  209. public:
  210. virtual void LoadBuffer() = 0;
  211. virtual TExecutionContext FlushBuffer(TExecutionContext exctx) = 0;
  212. virtual TExecutionContext UploadTexture(TExecutionContext exctx) = 0;
  213. TView* TryGetView(GPUVAddr view_addr, const SurfaceParams& view_params) {
  214. if (view_addr < gpu_addr || !params.IsFamiliar(view_params)) {
  215. // It can't be a view if it's in a prior address.
  216. return {};
  217. }
  218. const auto relative_offset{static_cast<u64>(view_addr - gpu_addr)};
  219. const auto it{view_offset_map.find(relative_offset)};
  220. if (it == view_offset_map.end()) {
  221. // Couldn't find an aligned view.
  222. return {};
  223. }
  224. const auto [layer, level] = it->second;
  225. if (!params.IsViewValid(view_params, layer, level)) {
  226. return {};
  227. }
  228. return GetView(layer, view_params.GetNumLayers(), level, view_params.GetNumLevels());
  229. }
  230. GPUVAddr GetGpuAddr() const {
  231. ASSERT(is_registered);
  232. return gpu_addr;
  233. }
  234. VAddr GetCpuAddr() const {
  235. ASSERT(is_registered);
  236. return cpu_addr;
  237. }
  238. u8* GetHostPtr() const {
  239. ASSERT(is_registered);
  240. return host_ptr;
  241. }
  242. CacheAddr GetCacheAddr() const {
  243. ASSERT(is_registered);
  244. return cache_addr;
  245. }
  246. std::size_t GetSizeInBytes() const {
  247. return params.GetGuestSizeInBytes();
  248. }
  249. void MarkAsModified(bool is_modified_) {
  250. is_modified = is_modified_;
  251. if (is_modified_) {
  252. modification_tick = texture_cache.Tick();
  253. }
  254. }
  255. const SurfaceParams& GetSurfaceParams() const {
  256. return params;
  257. }
  258. TView* GetView(GPUVAddr view_addr, const SurfaceParams& view_params) {
  259. TView* view{TryGetView(view_addr, view_params)};
  260. ASSERT(view != nullptr);
  261. return view;
  262. }
  263. void Register(GPUVAddr gpu_addr_, VAddr cpu_addr_, u8* host_ptr_) {
  264. ASSERT(!is_registered);
  265. is_registered = true;
  266. gpu_addr = gpu_addr_;
  267. cpu_addr = cpu_addr_;
  268. host_ptr = host_ptr_;
  269. cache_addr = ToCacheAddr(host_ptr_);
  270. DecorateSurfaceName();
  271. }
  272. void Unregister() {
  273. ASSERT(is_registered);
  274. is_registered = false;
  275. }
  276. u64 GetModificationTick() const {
  277. return modification_tick;
  278. }
  279. bool IsRegistered() const {
  280. return is_registered;
  281. }
  282. protected:
  283. explicit SurfaceBase(TTextureCache& texture_cache, const SurfaceParams& params)
  284. : params{params}, texture_cache{texture_cache}, view_offset_map{
  285. params.CreateViewOffsetMap()} {}
  286. ~SurfaceBase() = default;
  287. virtual void DecorateSurfaceName() = 0;
  288. virtual std::unique_ptr<TView> CreateView(const ViewKey& view_key) = 0;
  289. bool IsModified() const {
  290. return is_modified;
  291. }
  292. const SurfaceParams params;
  293. private:
  294. TView* GetView(u32 base_layer, u32 num_layers, u32 base_level, u32 num_levels) {
  295. const ViewKey key{base_layer, num_layers, base_level, num_levels};
  296. const auto [entry, is_cache_miss] = views.try_emplace(key);
  297. auto& view{entry->second};
  298. if (is_cache_miss) {
  299. view = CreateView(key);
  300. }
  301. return view.get();
  302. }
  303. TTextureCache& texture_cache;
  304. const std::map<u64, std::pair<u32, u32>> view_offset_map;
  305. GPUVAddr gpu_addr{};
  306. VAddr cpu_addr{};
  307. u8* host_ptr{};
  308. CacheAddr cache_addr{};
  309. u64 modification_tick{};
  310. bool is_modified{};
  311. bool is_registered{};
  312. std::unordered_map<ViewKey, std::unique_ptr<TView>> views;
  313. };
  314. template <typename TSurface, typename TView, typename TExecutionContext>
  315. class TextureCache {
  316. static_assert(std::is_trivially_copyable_v<TExecutionContext>);
  317. using ResultType = std::tuple<TView*, TExecutionContext>;
  318. using IntervalMap = boost::icl::interval_map<CacheAddr, std::set<std::shared_ptr<TSurface>>>;
  319. using IntervalType = typename IntervalMap::interval_type;
  320. public:
  321. void InvalidateRegion(CacheAddr addr, std::size_t size) {
  322. for (const auto& surface : GetSurfacesInRegion(addr, size)) {
  323. if (!surface->IsRegistered()) {
  324. // Skip duplicates
  325. continue;
  326. }
  327. Unregister(surface);
  328. }
  329. }
  330. ResultType GetTextureSurface(TExecutionContext exctx,
  331. const Tegra::Texture::FullTextureInfo& config) {
  332. const auto gpu_addr{config.tic.Address()};
  333. if (!gpu_addr) {
  334. return {{}, exctx};
  335. }
  336. const auto params{SurfaceParams::CreateForTexture(system, config)};
  337. return GetSurfaceView(exctx, gpu_addr, params, true);
  338. }
  339. ResultType GetDepthBufferSurface(TExecutionContext exctx, bool preserve_contents) {
  340. const auto& regs{system.GPU().Maxwell3D().regs};
  341. const auto gpu_addr{regs.zeta.Address()};
  342. if (!gpu_addr || !regs.zeta_enable) {
  343. return {{}, exctx};
  344. }
  345. const auto depth_params{SurfaceParams::CreateForDepthBuffer(
  346. system, regs.zeta_width, regs.zeta_height, regs.zeta.format,
  347. regs.zeta.memory_layout.block_width, regs.zeta.memory_layout.block_height,
  348. regs.zeta.memory_layout.block_depth, regs.zeta.memory_layout.type)};
  349. return GetSurfaceView(exctx, gpu_addr, depth_params, preserve_contents);
  350. }
  351. ResultType GetColorBufferSurface(TExecutionContext exctx, std::size_t index,
  352. bool preserve_contents) {
  353. ASSERT(index < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets);
  354. const auto& regs{system.GPU().Maxwell3D().regs};
  355. if (index >= regs.rt_control.count || regs.rt[index].Address() == 0 ||
  356. regs.rt[index].format == Tegra::RenderTargetFormat::NONE) {
  357. return {{}, exctx};
  358. }
  359. auto& memory_manager{system.GPU().MemoryManager()};
  360. const auto& config{system.GPU().Maxwell3D().regs.rt[index]};
  361. const auto gpu_addr{config.Address() +
  362. config.base_layer * config.layer_stride * sizeof(u32)};
  363. if (!gpu_addr) {
  364. return {{}, exctx};
  365. }
  366. return GetSurfaceView(exctx, gpu_addr, SurfaceParams::CreateForFramebuffer(system, index),
  367. preserve_contents);
  368. }
  369. ResultType GetFermiSurface(TExecutionContext exctx,
  370. const Tegra::Engines::Fermi2D::Regs::Surface& config) {
  371. return GetSurfaceView(exctx, config.Address(),
  372. SurfaceParams::CreateForFermiCopySurface(config), true);
  373. }
  374. std::shared_ptr<TSurface> TryFindFramebufferSurface(const u8* host_ptr) const {
  375. const auto it{registered_surfaces.find(ToCacheAddr(host_ptr))};
  376. return it != registered_surfaces.end() ? *it->second.begin() : nullptr;
  377. }
  378. u64 Tick() {
  379. return ++ticks;
  380. }
  381. protected:
  382. TextureCache(Core::System& system, VideoCore::RasterizerInterface& rasterizer)
  383. : system{system}, rasterizer{rasterizer} {}
  384. ~TextureCache() = default;
  385. virtual ResultType TryFastGetSurfaceView(
  386. TExecutionContext exctx, GPUVAddr gpu_addr, VAddr cpu_addr, u8* host_ptr,
  387. const SurfaceParams& params, bool preserve_contents,
  388. const std::vector<std::shared_ptr<TSurface>>& overlaps) = 0;
  389. virtual std::shared_ptr<TSurface> CreateSurface(const SurfaceParams& params) = 0;
  390. void Register(std::shared_ptr<TSurface> surface, GPUVAddr gpu_addr, VAddr cpu_addr,
  391. u8* host_ptr) {
  392. surface->Register(gpu_addr, cpu_addr, host_ptr);
  393. registered_surfaces.add({GetSurfaceInterval(surface), {surface}});
  394. rasterizer.UpdatePagesCachedCount(surface->GetCpuAddr(), surface->GetSizeInBytes(), 1);
  395. }
  396. void Unregister(std::shared_ptr<TSurface> surface) {
  397. registered_surfaces.subtract({GetSurfaceInterval(surface), {surface}});
  398. rasterizer.UpdatePagesCachedCount(surface->GetCpuAddr(), surface->GetSizeInBytes(), -1);
  399. surface->Unregister();
  400. }
  401. std::shared_ptr<TSurface> GetUncachedSurface(const SurfaceParams& params) {
  402. if (const auto surface = TryGetReservedSurface(params); surface)
  403. return surface;
  404. // No reserved surface available, create a new one and reserve it
  405. auto new_surface{CreateSurface(params)};
  406. ReserveSurface(params, new_surface);
  407. return new_surface;
  408. }
  409. Core::System& system;
  410. private:
  411. ResultType GetSurfaceView(TExecutionContext exctx, GPUVAddr gpu_addr,
  412. const SurfaceParams& params, bool preserve_contents) {
  413. auto& memory_manager{system.GPU().MemoryManager()};
  414. const auto cpu_addr{memory_manager.GpuToCpuAddress(gpu_addr)};
  415. DEBUG_ASSERT(cpu_addr);
  416. const auto host_ptr{memory_manager.GetPointer(gpu_addr)};
  417. const auto cache_addr{ToCacheAddr(host_ptr)};
  418. auto overlaps{GetSurfacesInRegion(cache_addr, params.GetGuestSizeInBytes())};
  419. if (overlaps.empty()) {
  420. return LoadSurfaceView(exctx, gpu_addr, *cpu_addr, host_ptr, params, preserve_contents);
  421. }
  422. if (overlaps.size() == 1) {
  423. if (TView* view = overlaps[0]->TryGetView(gpu_addr, params); view) {
  424. return {view, exctx};
  425. }
  426. }
  427. TView* fast_view;
  428. std::tie(fast_view, exctx) = TryFastGetSurfaceView(exctx, gpu_addr, *cpu_addr, host_ptr,
  429. params, preserve_contents, overlaps);
  430. if (!fast_view) {
  431. std::sort(overlaps.begin(), overlaps.end(), [](const auto& lhs, const auto& rhs) {
  432. return lhs->GetModificationTick() < rhs->GetModificationTick();
  433. });
  434. }
  435. for (const auto& surface : overlaps) {
  436. if (!fast_view) {
  437. // Flush even when we don't care about the contents, to preserve memory not
  438. // written by the new surface.
  439. exctx = surface->FlushBuffer(exctx);
  440. }
  441. Unregister(surface);
  442. }
  443. if (fast_view) {
  444. return {fast_view, exctx};
  445. }
  446. return LoadSurfaceView(exctx, gpu_addr, *cpu_addr, host_ptr, params, preserve_contents);
  447. }
  448. ResultType LoadSurfaceView(TExecutionContext exctx, GPUVAddr gpu_addr, VAddr cpu_addr,
  449. u8* host_ptr, const SurfaceParams& params, bool preserve_contents) {
  450. const auto new_surface{GetUncachedSurface(params)};
  451. Register(new_surface, gpu_addr, cpu_addr, host_ptr);
  452. if (preserve_contents) {
  453. exctx = LoadSurface(exctx, new_surface);
  454. }
  455. return {new_surface->GetView(gpu_addr, params), exctx};
  456. }
  457. TExecutionContext LoadSurface(TExecutionContext exctx,
  458. const std::shared_ptr<TSurface>& surface) {
  459. surface->LoadBuffer();
  460. exctx = surface->UploadTexture(exctx);
  461. surface->MarkAsModified(false);
  462. return exctx;
  463. }
  464. std::vector<std::shared_ptr<TSurface>> GetSurfacesInRegion(CacheAddr cache_addr,
  465. std::size_t size) const {
  466. if (size == 0) {
  467. return {};
  468. }
  469. const IntervalType interval{cache_addr, cache_addr + size};
  470. std::vector<std::shared_ptr<TSurface>> surfaces;
  471. for (auto& pair : boost::make_iterator_range(registered_surfaces.equal_range(interval))) {
  472. surfaces.push_back(*pair.second.begin());
  473. }
  474. return surfaces;
  475. }
  476. void ReserveSurface(const SurfaceParams& params, std::shared_ptr<TSurface> surface) {
  477. surface_reserve[params].push_back(std::move(surface));
  478. }
  479. std::shared_ptr<TSurface> TryGetReservedSurface(const SurfaceParams& params) {
  480. auto search{surface_reserve.find(params)};
  481. if (search == surface_reserve.end()) {
  482. return {};
  483. }
  484. for (auto& surface : search->second) {
  485. if (!surface->IsRegistered()) {
  486. return surface;
  487. }
  488. }
  489. return {};
  490. }
  491. IntervalType GetSurfaceInterval(std::shared_ptr<TSurface> surface) const {
  492. return IntervalType::right_open(surface->GetCacheAddr(),
  493. surface->GetCacheAddr() + surface->GetSizeInBytes());
  494. }
  495. VideoCore::RasterizerInterface& rasterizer;
  496. u64 ticks{};
  497. IntervalMap registered_surfaces;
  498. /// The surface reserve is a "backup" cache, this is where we put unique surfaces that have
  499. /// previously been used. This is to prevent surfaces from being constantly created and
  500. /// destroyed when used with different surface parameters.
  501. std::unordered_map<SurfaceParams, std::list<std::shared_ptr<TSurface>>> surface_reserve;
  502. };
  503. struct DummyExecutionContext {};
  504. template <typename TSurface, typename TView>
  505. class TextureCacheContextless : protected TextureCache<TSurface, TView, DummyExecutionContext> {
  506. using Base = TextureCache<TSurface, TView, DummyExecutionContext>;
  507. public:
  508. void InvalidateRegion(CacheAddr addr, std::size_t size) {
  509. Base::InvalidateRegion(addr, size);
  510. }
  511. TView* GetTextureSurface(const Tegra::Texture::FullTextureInfo& config) {
  512. return RemoveContext(Base::GetTextureSurface({}, config));
  513. }
  514. TView* GetDepthBufferSurface(bool preserve_contents) {
  515. return RemoveContext(Base::GetDepthBufferSurface({}, preserve_contents));
  516. }
  517. TView* GetColorBufferSurface(std::size_t index, bool preserve_contents) {
  518. return RemoveContext(Base::GetColorBufferSurface({}, index, preserve_contents));
  519. }
  520. TView* GetFermiSurface(const Tegra::Engines::Fermi2D::Regs::Surface& config) {
  521. return RemoveContext(Base::GetFermiSurface({}, config));
  522. }
  523. std::shared_ptr<TSurface> TryFindFramebufferSurface(const u8* host_ptr) const {
  524. return Base::TryFindFramebufferSurface(host_ptr);
  525. }
  526. u64 Tick() {
  527. return Base::Tick();
  528. }
  529. protected:
  530. explicit TextureCacheContextless(Core::System& system,
  531. VideoCore::RasterizerInterface& rasterizer)
  532. : TextureCache<TSurface, TView, DummyExecutionContext>{system, rasterizer} {}
  533. virtual TView* TryFastGetSurfaceView(
  534. GPUVAddr gpu_addr, VAddr cpu_addr, u8* host_ptr, const SurfaceParams& params,
  535. bool preserve_contents, const std::vector<std::shared_ptr<TSurface>>& overlaps) = 0;
  536. private:
  537. std::tuple<TView*, DummyExecutionContext> TryFastGetSurfaceView(
  538. DummyExecutionContext, GPUVAddr gpu_addr, VAddr cpu_addr, u8* host_ptr,
  539. const SurfaceParams& params, bool preserve_contents,
  540. const std::vector<std::shared_ptr<TSurface>>& overlaps) {
  541. return {TryFastGetSurfaceView(gpu_addr, cpu_addr, host_ptr, params, preserve_contents,
  542. overlaps),
  543. {}};
  544. }
  545. TView* RemoveContext(std::tuple<TView*, DummyExecutionContext> return_value) {
  546. const auto [view, exctx] = return_value;
  547. return view;
  548. }
  549. };
  550. template <typename TTextureCache, typename TView>
  551. class SurfaceBaseContextless : public SurfaceBase<TTextureCache, TView, DummyExecutionContext> {
  552. public:
  553. DummyExecutionContext FlushBuffer(DummyExecutionContext) {
  554. FlushBufferImpl();
  555. return {};
  556. }
  557. DummyExecutionContext UploadTexture(DummyExecutionContext) {
  558. UploadTextureImpl();
  559. return {};
  560. }
  561. protected:
  562. explicit SurfaceBaseContextless(TTextureCache& texture_cache, const SurfaceParams& params)
  563. : SurfaceBase<TTextureCache, TView, DummyExecutionContext>{texture_cache, params} {}
  564. virtual void FlushBufferImpl() = 0;
  565. virtual void UploadTextureImpl() = 0;
  566. };
  567. } // namespace VideoCommon