texture_cache.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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 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. }
  252. const SurfaceParams& GetSurfaceParams() const {
  253. return params;
  254. }
  255. TView* GetView(GPUVAddr view_addr, const SurfaceParams& view_params) {
  256. TView* view{TryGetView(view_addr, view_params)};
  257. ASSERT(view != nullptr);
  258. return view;
  259. }
  260. void Register(GPUVAddr gpu_addr_, VAddr cpu_addr_, u8* host_ptr_) {
  261. ASSERT(!is_registered);
  262. is_registered = true;
  263. gpu_addr = gpu_addr_;
  264. cpu_addr = cpu_addr_;
  265. host_ptr = host_ptr_;
  266. cache_addr = ToCacheAddr(host_ptr_);
  267. DecorateSurfaceName();
  268. }
  269. void Unregister() {
  270. ASSERT(is_registered);
  271. is_registered = false;
  272. }
  273. bool IsRegistered() const {
  274. return is_registered;
  275. }
  276. protected:
  277. explicit SurfaceBase(const SurfaceParams& params)
  278. : params{params}, view_offset_map{params.CreateViewOffsetMap()} {}
  279. ~SurfaceBase() = default;
  280. virtual void DecorateSurfaceName() = 0;
  281. virtual std::unique_ptr<TView> CreateView(const ViewKey& view_key) = 0;
  282. bool IsModified() const {
  283. return is_modified;
  284. }
  285. const SurfaceParams params;
  286. private:
  287. TView* GetView(u32 base_layer, u32 num_layers, u32 base_level, u32 num_levels) {
  288. const ViewKey key{base_layer, num_layers, base_level, num_levels};
  289. const auto [entry, is_cache_miss] = views.try_emplace(key);
  290. auto& view{entry->second};
  291. if (is_cache_miss) {
  292. view = CreateView(key);
  293. }
  294. return view.get();
  295. }
  296. const std::map<u64, std::pair<u32, u32>> view_offset_map;
  297. GPUVAddr gpu_addr{};
  298. VAddr cpu_addr{};
  299. u8* host_ptr{};
  300. CacheAddr cache_addr{};
  301. bool is_modified{};
  302. bool is_registered{};
  303. std::unordered_map<ViewKey, std::unique_ptr<TView>> views;
  304. };
  305. template <typename TSurface, typename TView, typename TExecutionContext>
  306. class TextureCache {
  307. static_assert(std::is_trivially_copyable_v<TExecutionContext>);
  308. using ResultType = std::tuple<TView*, TExecutionContext>;
  309. using IntervalMap = boost::icl::interval_map<CacheAddr, std::set<std::shared_ptr<TSurface>>>;
  310. using IntervalType = typename IntervalMap::interval_type;
  311. public:
  312. void InvalidateRegion(CacheAddr addr, std::size_t size) {
  313. for (const auto& surface : GetSurfacesInRegion(addr, size)) {
  314. if (!surface->IsRegistered()) {
  315. // Skip duplicates
  316. continue;
  317. }
  318. Unregister(surface);
  319. }
  320. }
  321. ResultType GetTextureSurface(TExecutionContext exctx,
  322. const Tegra::Texture::FullTextureInfo& config) {
  323. const auto gpu_addr{config.tic.Address()};
  324. if (!gpu_addr) {
  325. return {{}, exctx};
  326. }
  327. const auto params{SurfaceParams::CreateForTexture(system, config)};
  328. return GetSurfaceView(exctx, gpu_addr, params, true);
  329. }
  330. ResultType GetDepthBufferSurface(TExecutionContext exctx, bool preserve_contents) {
  331. const auto& regs{system.GPU().Maxwell3D().regs};
  332. const auto gpu_addr{regs.zeta.Address()};
  333. if (!gpu_addr || !regs.zeta_enable) {
  334. return {{}, exctx};
  335. }
  336. const auto depth_params{SurfaceParams::CreateForDepthBuffer(
  337. system, regs.zeta_width, regs.zeta_height, regs.zeta.format,
  338. regs.zeta.memory_layout.block_width, regs.zeta.memory_layout.block_height,
  339. regs.zeta.memory_layout.block_depth, regs.zeta.memory_layout.type)};
  340. return GetSurfaceView(exctx, gpu_addr, depth_params, preserve_contents);
  341. }
  342. ResultType GetColorBufferSurface(TExecutionContext exctx, std::size_t index,
  343. bool preserve_contents) {
  344. ASSERT(index < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets);
  345. const auto& regs{system.GPU().Maxwell3D().regs};
  346. if (index >= regs.rt_control.count || regs.rt[index].Address() == 0 ||
  347. regs.rt[index].format == Tegra::RenderTargetFormat::NONE) {
  348. return {{}, exctx};
  349. }
  350. auto& memory_manager{system.GPU().MemoryManager()};
  351. const auto& config{system.GPU().Maxwell3D().regs.rt[index]};
  352. const auto gpu_addr{config.Address() +
  353. config.base_layer * config.layer_stride * sizeof(u32)};
  354. if (!gpu_addr) {
  355. return {{}, exctx};
  356. }
  357. return GetSurfaceView(exctx, gpu_addr, SurfaceParams::CreateForFramebuffer(system, index),
  358. preserve_contents);
  359. }
  360. ResultType GetFermiSurface(TExecutionContext exctx,
  361. const Tegra::Engines::Fermi2D::Regs::Surface& config) {
  362. return GetSurfaceView(exctx, config.Address(),
  363. SurfaceParams::CreateForFermiCopySurface(config), true);
  364. }
  365. std::shared_ptr<TSurface> TryFindFramebufferSurface(const u8* host_ptr) const {
  366. const auto it{registered_surfaces.find(ToCacheAddr(host_ptr))};
  367. return it != registered_surfaces.end() ? *it->second.begin() : nullptr;
  368. }
  369. protected:
  370. TextureCache(Core::System& system, VideoCore::RasterizerInterface& rasterizer)
  371. : system{system}, rasterizer{rasterizer} {}
  372. ~TextureCache() = default;
  373. virtual ResultType TryFastGetSurfaceView(
  374. TExecutionContext exctx, GPUVAddr gpu_addr, VAddr cpu_addr, u8* host_ptr,
  375. const SurfaceParams& params, bool preserve_contents,
  376. const std::vector<std::shared_ptr<TSurface>>& overlaps) = 0;
  377. virtual std::shared_ptr<TSurface> CreateSurface(const SurfaceParams& params) = 0;
  378. void Register(std::shared_ptr<TSurface> surface, GPUVAddr gpu_addr, VAddr cpu_addr,
  379. u8* host_ptr) {
  380. surface->Register(gpu_addr, cpu_addr, host_ptr);
  381. registered_surfaces.add({GetSurfaceInterval(surface), {surface}});
  382. rasterizer.UpdatePagesCachedCount(surface->GetCpuAddr(), surface->GetSizeInBytes(), 1);
  383. }
  384. void Unregister(std::shared_ptr<TSurface> surface) {
  385. registered_surfaces.subtract({GetSurfaceInterval(surface), {surface}});
  386. rasterizer.UpdatePagesCachedCount(surface->GetCpuAddr(), surface->GetSizeInBytes(), -1);
  387. surface->Unregister();
  388. }
  389. std::shared_ptr<TSurface> GetUncachedSurface(const SurfaceParams& params) {
  390. if (const auto surface = TryGetReservedSurface(params); surface)
  391. return surface;
  392. // No reserved surface available, create a new one and reserve it
  393. auto new_surface{CreateSurface(params)};
  394. ReserveSurface(params, new_surface);
  395. return new_surface;
  396. }
  397. Core::System& system;
  398. private:
  399. ResultType GetSurfaceView(TExecutionContext exctx, GPUVAddr gpu_addr,
  400. const SurfaceParams& params, bool preserve_contents) {
  401. auto& memory_manager{system.GPU().MemoryManager()};
  402. const auto cpu_addr{memory_manager.GpuToCpuAddress(gpu_addr)};
  403. DEBUG_ASSERT(cpu_addr);
  404. const auto host_ptr{memory_manager.GetPointer(gpu_addr)};
  405. const auto cache_addr{ToCacheAddr(host_ptr)};
  406. const auto overlaps{GetSurfacesInRegion(cache_addr, params.GetGuestSizeInBytes())};
  407. if (overlaps.empty()) {
  408. return LoadSurfaceView(exctx, gpu_addr, *cpu_addr, host_ptr, params, preserve_contents);
  409. }
  410. if (overlaps.size() == 1) {
  411. if (TView* view = overlaps[0]->TryGetView(gpu_addr, params); view) {
  412. return {view, exctx};
  413. }
  414. }
  415. TView* fast_view;
  416. std::tie(fast_view, exctx) = TryFastGetSurfaceView(exctx, gpu_addr, *cpu_addr, host_ptr,
  417. params, preserve_contents, overlaps);
  418. if (!fast_view) {
  419. std::sort(overlaps.begin(), overlaps.end(), [](const auto& lhs, const auto& rhs) {
  420. return lhs->GetModificationTick() < rhs->GetModificationTick();
  421. });
  422. }
  423. for (const auto& surface : overlaps) {
  424. if (!fast_view) {
  425. // Flush even when we don't care about the contents, to preserve memory not written
  426. // by the new surface.
  427. exctx = surface->FlushBuffer(exctx);
  428. }
  429. Unregister(surface);
  430. }
  431. if (fast_view) {
  432. return {fast_view, exctx};
  433. }
  434. return LoadSurfaceView(exctx, gpu_addr, *cpu_addr, host_ptr, params, preserve_contents);
  435. }
  436. ResultType LoadSurfaceView(TExecutionContext exctx, GPUVAddr gpu_addr, VAddr cpu_addr,
  437. u8* host_ptr, const SurfaceParams& params, bool preserve_contents) {
  438. const auto new_surface{GetUncachedSurface(params)};
  439. Register(new_surface, gpu_addr, cpu_addr, host_ptr);
  440. if (preserve_contents) {
  441. exctx = LoadSurface(exctx, new_surface);
  442. }
  443. return {new_surface->GetView(gpu_addr, params), exctx};
  444. }
  445. TExecutionContext LoadSurface(TExecutionContext exctx,
  446. const std::shared_ptr<TSurface>& surface) {
  447. surface->LoadBuffer();
  448. exctx = surface->UploadTexture(exctx);
  449. surface->MarkAsModified(false);
  450. return exctx;
  451. }
  452. std::vector<std::shared_ptr<TSurface>> GetSurfacesInRegion(CacheAddr cache_addr,
  453. std::size_t size) const {
  454. if (size == 0) {
  455. return {};
  456. }
  457. const IntervalType interval{cache_addr, cache_addr + size};
  458. std::vector<std::shared_ptr<TSurface>> surfaces;
  459. for (auto& pair : boost::make_iterator_range(registered_surfaces.equal_range(interval))) {
  460. surfaces.push_back(*pair.second.begin());
  461. }
  462. return surfaces;
  463. }
  464. void ReserveSurface(const SurfaceParams& params, std::shared_ptr<TSurface> surface) {
  465. surface_reserve[params].push_back(std::move(surface));
  466. }
  467. std::shared_ptr<TSurface> TryGetReservedSurface(const SurfaceParams& params) {
  468. auto search{surface_reserve.find(params)};
  469. if (search == surface_reserve.end()) {
  470. return {};
  471. }
  472. for (auto& surface : search->second) {
  473. if (!surface->IsRegistered()) {
  474. return surface;
  475. }
  476. }
  477. return {};
  478. }
  479. IntervalType GetSurfaceInterval(std::shared_ptr<TSurface> surface) const {
  480. return IntervalType::right_open(surface->GetCacheAddr(),
  481. surface->GetCacheAddr() + surface->GetSizeInBytes());
  482. }
  483. VideoCore::RasterizerInterface& rasterizer;
  484. IntervalMap registered_surfaces;
  485. /// The surface reserve is a "backup" cache, this is where we put unique surfaces that have
  486. /// previously been used. This is to prevent surfaces from being constantly created and
  487. /// destroyed when used with different surface parameters.
  488. std::unordered_map<SurfaceParams, std::list<std::shared_ptr<TSurface>>> surface_reserve;
  489. };
  490. struct DummyExecutionContext {};
  491. template <typename TSurface, typename TView>
  492. class TextureCacheContextless : protected TextureCache<TSurface, TView, DummyExecutionContext> {
  493. using Base = TextureCache<TSurface, TView, DummyExecutionContext>;
  494. public:
  495. void InvalidateRegion(CacheAddr addr, std::size_t size) {
  496. Base::InvalidateRegion(addr, size);
  497. }
  498. TView* GetTextureSurface(const Tegra::Texture::FullTextureInfo& config) {
  499. return RemoveContext(Base::GetTextureSurface({}, config));
  500. }
  501. TView* GetDepthBufferSurface(bool preserve_contents) {
  502. return RemoveContext(Base::GetDepthBufferSurface({}, preserve_contents));
  503. }
  504. TView* GetColorBufferSurface(std::size_t index, bool preserve_contents) {
  505. return RemoveContext(Base::GetColorBufferSurface({}, index, preserve_contents));
  506. }
  507. TView* GetFermiSurface(const Tegra::Engines::Fermi2D::Regs::Surface& config) {
  508. return RemoveContext(Base::GetFermiSurface({}, config));
  509. }
  510. std::shared_ptr<TSurface> TryFindFramebufferSurface(const u8* host_ptr) const {
  511. return Base::TryFindFramebufferSurface(host_ptr);
  512. }
  513. protected:
  514. explicit TextureCacheContextless(Core::System& system,
  515. VideoCore::RasterizerInterface& rasterizer)
  516. : TextureCache<TSurface, TView, DummyExecutionContext>{system, rasterizer} {}
  517. virtual TView* TryFastGetSurfaceView(
  518. GPUVAddr gpu_addr, VAddr cpu_addr, u8* host_ptr, const SurfaceParams& params,
  519. bool preserve_contents, const std::vector<std::shared_ptr<TSurface>>& overlaps) = 0;
  520. private:
  521. std::tuple<TView*, DummyExecutionContext> TryFastGetSurfaceView(
  522. DummyExecutionContext, GPUVAddr gpu_addr, VAddr cpu_addr, u8* host_ptr,
  523. const SurfaceParams& params, bool preserve_contents,
  524. const std::vector<std::shared_ptr<TSurface>>& overlaps) {
  525. return {TryFastGetSurfaceView(gpu_addr, cpu_addr, host_ptr, params, preserve_contents,
  526. overlaps),
  527. {}};
  528. }
  529. TView* RemoveContext(std::tuple<TView*, DummyExecutionContext> return_value) {
  530. const auto [view, exctx] = return_value;
  531. return view;
  532. }
  533. };
  534. template <typename TView>
  535. class SurfaceBaseContextless : public SurfaceBase<TView, DummyExecutionContext> {
  536. public:
  537. DummyExecutionContext FlushBuffer(DummyExecutionContext) {
  538. FlushBufferImpl();
  539. return {};
  540. }
  541. DummyExecutionContext UploadTexture(DummyExecutionContext) {
  542. UploadTextureImpl();
  543. return {};
  544. }
  545. protected:
  546. explicit SurfaceBaseContextless(const SurfaceParams& params)
  547. : SurfaceBase<TView, DummyExecutionContext>{params} {}
  548. virtual void FlushBufferImpl() = 0;
  549. virtual void UploadTextureImpl() = 0;
  550. };
  551. } // namespace VideoCommon