texture_cache.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include "video_core/texture_cache/surface_base.h"
  23. #include "video_core/texture_cache/surface_params.h"
  24. #include "video_core/texture_cache/surface_view.h"
  25. namespace Core {
  26. class System;
  27. }
  28. namespace Tegra::Texture {
  29. struct FullTextureInfo;
  30. }
  31. namespace VideoCore {
  32. class RasterizerInterface;
  33. }
  34. namespace VideoCommon {
  35. template <typename TSurface, typename TView, typename TExecutionContext>
  36. class TextureCache {
  37. static_assert(std::is_trivially_copyable_v<TExecutionContext>);
  38. using ResultType = std::tuple<TView*, TExecutionContext>;
  39. using IntervalMap = boost::icl::interval_map<CacheAddr, std::set<std::shared_ptr<TSurface>>>;
  40. using IntervalType = typename IntervalMap::interval_type;
  41. public:
  42. void InvalidateRegion(CacheAddr addr, std::size_t size) {
  43. for (const auto& surface : GetSurfacesInRegion(addr, size)) {
  44. if (!surface->IsRegistered()) {
  45. // Skip duplicates
  46. continue;
  47. }
  48. Unregister(surface);
  49. }
  50. }
  51. ResultType GetTextureSurface(TExecutionContext exctx,
  52. const Tegra::Texture::FullTextureInfo& config) {
  53. const auto gpu_addr{config.tic.Address()};
  54. if (!gpu_addr) {
  55. return {{}, exctx};
  56. }
  57. const auto params{SurfaceParams::CreateForTexture(system, config)};
  58. return GetSurfaceView(exctx, gpu_addr, params, true);
  59. }
  60. ResultType GetDepthBufferSurface(TExecutionContext exctx, bool preserve_contents) {
  61. const auto& regs{system.GPU().Maxwell3D().regs};
  62. const auto gpu_addr{regs.zeta.Address()};
  63. if (!gpu_addr || !regs.zeta_enable) {
  64. return {{}, exctx};
  65. }
  66. const auto depth_params{SurfaceParams::CreateForDepthBuffer(
  67. system, regs.zeta_width, regs.zeta_height, regs.zeta.format,
  68. regs.zeta.memory_layout.block_width, regs.zeta.memory_layout.block_height,
  69. regs.zeta.memory_layout.block_depth, regs.zeta.memory_layout.type)};
  70. return GetSurfaceView(exctx, gpu_addr, depth_params, preserve_contents);
  71. }
  72. ResultType GetColorBufferSurface(TExecutionContext exctx, std::size_t index,
  73. bool preserve_contents) {
  74. ASSERT(index < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets);
  75. const auto& regs{system.GPU().Maxwell3D().regs};
  76. if (index >= regs.rt_control.count || regs.rt[index].Address() == 0 ||
  77. regs.rt[index].format == Tegra::RenderTargetFormat::NONE) {
  78. return {{}, exctx};
  79. }
  80. auto& memory_manager{system.GPU().MemoryManager()};
  81. const auto& config{system.GPU().Maxwell3D().regs.rt[index]};
  82. const auto gpu_addr{config.Address() +
  83. config.base_layer * config.layer_stride * sizeof(u32)};
  84. if (!gpu_addr) {
  85. return {{}, exctx};
  86. }
  87. return GetSurfaceView(exctx, gpu_addr, SurfaceParams::CreateForFramebuffer(system, index),
  88. preserve_contents);
  89. }
  90. ResultType GetFermiSurface(TExecutionContext exctx,
  91. const Tegra::Engines::Fermi2D::Regs::Surface& config) {
  92. return GetSurfaceView(exctx, config.Address(),
  93. SurfaceParams::CreateForFermiCopySurface(config), true);
  94. }
  95. std::shared_ptr<TSurface> TryFindFramebufferSurface(const u8* host_ptr) const {
  96. const auto it{registered_surfaces.find(ToCacheAddr(host_ptr))};
  97. return it != registered_surfaces.end() ? *it->second.begin() : nullptr;
  98. }
  99. u64 Tick() {
  100. return ++ticks;
  101. }
  102. protected:
  103. TextureCache(Core::System& system, VideoCore::RasterizerInterface& rasterizer)
  104. : system{system}, rasterizer{rasterizer} {}
  105. ~TextureCache() = default;
  106. virtual ResultType TryFastGetSurfaceView(
  107. TExecutionContext exctx, GPUVAddr gpu_addr, VAddr cpu_addr, u8* host_ptr,
  108. const SurfaceParams& params, bool preserve_contents,
  109. const std::vector<std::shared_ptr<TSurface>>& overlaps) = 0;
  110. virtual std::shared_ptr<TSurface> CreateSurface(const SurfaceParams& params) = 0;
  111. void Register(std::shared_ptr<TSurface> surface, GPUVAddr gpu_addr, VAddr cpu_addr,
  112. u8* host_ptr) {
  113. surface->Register(gpu_addr, cpu_addr, host_ptr);
  114. registered_surfaces.add({GetSurfaceInterval(surface), {surface}});
  115. rasterizer.UpdatePagesCachedCount(surface->GetCpuAddr(), surface->GetSizeInBytes(), 1);
  116. }
  117. void Unregister(std::shared_ptr<TSurface> surface) {
  118. registered_surfaces.subtract({GetSurfaceInterval(surface), {surface}});
  119. rasterizer.UpdatePagesCachedCount(surface->GetCpuAddr(), surface->GetSizeInBytes(), -1);
  120. surface->Unregister();
  121. }
  122. std::shared_ptr<TSurface> GetUncachedSurface(const SurfaceParams& params) {
  123. if (const auto surface = TryGetReservedSurface(params); surface)
  124. return surface;
  125. // No reserved surface available, create a new one and reserve it
  126. auto new_surface{CreateSurface(params)};
  127. ReserveSurface(params, new_surface);
  128. return new_surface;
  129. }
  130. Core::System& system;
  131. private:
  132. ResultType GetSurfaceView(TExecutionContext exctx, GPUVAddr gpu_addr,
  133. const SurfaceParams& params, bool preserve_contents) {
  134. auto& memory_manager{system.GPU().MemoryManager()};
  135. const auto cpu_addr{memory_manager.GpuToCpuAddress(gpu_addr)};
  136. DEBUG_ASSERT(cpu_addr);
  137. const auto host_ptr{memory_manager.GetPointer(gpu_addr)};
  138. const auto cache_addr{ToCacheAddr(host_ptr)};
  139. auto overlaps{GetSurfacesInRegion(cache_addr, params.GetGuestSizeInBytes())};
  140. if (overlaps.empty()) {
  141. return LoadSurfaceView(exctx, gpu_addr, *cpu_addr, host_ptr, params, preserve_contents);
  142. }
  143. if (overlaps.size() == 1) {
  144. if (TView* view = overlaps[0]->TryGetView(gpu_addr, params); view) {
  145. return {view, exctx};
  146. }
  147. }
  148. TView* fast_view;
  149. std::tie(fast_view, exctx) = TryFastGetSurfaceView(exctx, gpu_addr, *cpu_addr, host_ptr,
  150. params, preserve_contents, overlaps);
  151. if (!fast_view) {
  152. std::sort(overlaps.begin(), overlaps.end(), [](const auto& lhs, const auto& rhs) {
  153. return lhs->GetModificationTick() < rhs->GetModificationTick();
  154. });
  155. }
  156. for (const auto& surface : overlaps) {
  157. if (!fast_view) {
  158. // Flush even when we don't care about the contents, to preserve memory not
  159. // written by the new surface.
  160. exctx = FlushSurface(exctx, surface);
  161. }
  162. Unregister(surface);
  163. }
  164. if (fast_view) {
  165. return {fast_view, exctx};
  166. }
  167. return LoadSurfaceView(exctx, gpu_addr, *cpu_addr, host_ptr, params, preserve_contents);
  168. }
  169. ResultType LoadSurfaceView(TExecutionContext exctx, GPUVAddr gpu_addr, VAddr cpu_addr,
  170. u8* host_ptr, const SurfaceParams& params, bool preserve_contents) {
  171. const auto new_surface{GetUncachedSurface(params)};
  172. Register(new_surface, gpu_addr, cpu_addr, host_ptr);
  173. if (preserve_contents) {
  174. exctx = LoadSurface(exctx, new_surface);
  175. }
  176. return {new_surface->GetView(gpu_addr, params), exctx};
  177. }
  178. TExecutionContext LoadSurface(TExecutionContext exctx,
  179. const std::shared_ptr<TSurface>& surface) {
  180. surface->LoadBuffer();
  181. exctx = surface->UploadTexture(exctx);
  182. surface->MarkAsModified(false);
  183. return exctx;
  184. }
  185. TExecutionContext FlushSurface(TExecutionContext exctx,
  186. const std::shared_ptr<TSurface>& surface) {
  187. if (!surface->IsModified()) {
  188. return exctx;
  189. }
  190. exctx = surface->DownloadTexture(exctx);
  191. surface->FlushBuffer();
  192. return exctx;
  193. }
  194. std::vector<std::shared_ptr<TSurface>> GetSurfacesInRegion(CacheAddr cache_addr,
  195. std::size_t size) const {
  196. if (size == 0) {
  197. return {};
  198. }
  199. const IntervalType interval{cache_addr, cache_addr + size};
  200. std::vector<std::shared_ptr<TSurface>> surfaces;
  201. for (auto& pair : boost::make_iterator_range(registered_surfaces.equal_range(interval))) {
  202. surfaces.push_back(*pair.second.begin());
  203. }
  204. return surfaces;
  205. }
  206. void ReserveSurface(const SurfaceParams& params, std::shared_ptr<TSurface> surface) {
  207. surface_reserve[params].push_back(std::move(surface));
  208. }
  209. std::shared_ptr<TSurface> TryGetReservedSurface(const SurfaceParams& params) {
  210. auto search{surface_reserve.find(params)};
  211. if (search == surface_reserve.end()) {
  212. return {};
  213. }
  214. for (auto& surface : search->second) {
  215. if (!surface->IsRegistered()) {
  216. return surface;
  217. }
  218. }
  219. return {};
  220. }
  221. IntervalType GetSurfaceInterval(std::shared_ptr<TSurface> surface) const {
  222. return IntervalType::right_open(surface->GetCacheAddr(),
  223. surface->GetCacheAddr() + surface->GetSizeInBytes());
  224. }
  225. VideoCore::RasterizerInterface& rasterizer;
  226. u64 ticks{};
  227. IntervalMap registered_surfaces;
  228. /// The surface reserve is a "backup" cache, this is where we put unique surfaces that have
  229. /// previously been used. This is to prevent surfaces from being constantly created and
  230. /// destroyed when used with different surface parameters.
  231. std::unordered_map<SurfaceParams, std::list<std::shared_ptr<TSurface>>> surface_reserve;
  232. };
  233. } // namespace VideoCommon