surface_base.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 <unordered_map>
  7. #include <vector>
  8. #include "common/assert.h"
  9. #include "common/common_types.h"
  10. #include "video_core/gpu.h"
  11. #include "video_core/morton.h"
  12. #include "video_core/texture_cache/copy_params.h"
  13. #include "video_core/texture_cache/surface_params.h"
  14. #include "video_core/texture_cache/surface_view.h"
  15. template <class ForwardIt, class T, class Compare = std::less<>>
  16. ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value, Compare comp = {}) {
  17. // Note: BOTH type T and the type after ForwardIt is dereferenced
  18. // must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
  19. // This is stricter than lower_bound requirement (see above)
  20. first = std::lower_bound(first, last, value, comp);
  21. return first != last && !comp(value, *first) ? first : last;
  22. }
  23. namespace Tegra {
  24. class MemoryManager;
  25. }
  26. namespace VideoCommon {
  27. using VideoCore::MortonSwizzleMode;
  28. using VideoCore::Surface::SurfaceTarget;
  29. enum class MatchStructureResult : u32 {
  30. FullMatch = 0,
  31. SemiMatch = 1,
  32. None = 2,
  33. };
  34. class SurfaceBaseImpl {
  35. public:
  36. void LoadBuffer(Tegra::MemoryManager& memory_manager, std::vector<u8>& staging_buffer);
  37. void FlushBuffer(Tegra::MemoryManager& memory_manager, std::vector<u8>& staging_buffer);
  38. GPUVAddr GetGpuAddr() const {
  39. return gpu_addr;
  40. }
  41. bool Overlaps(const CacheAddr start, const CacheAddr end) const {
  42. return (cache_addr < end) && (cache_addr_end > start);
  43. }
  44. // Use only when recycling a surface
  45. void SetGpuAddr(const GPUVAddr new_addr) {
  46. gpu_addr = new_addr;
  47. }
  48. VAddr GetCpuAddr() const {
  49. return cpu_addr;
  50. }
  51. void SetCpuAddr(const VAddr new_addr) {
  52. cpu_addr = new_addr;
  53. }
  54. CacheAddr GetCacheAddr() const {
  55. return cache_addr;
  56. }
  57. CacheAddr GetCacheAddrEnd() const {
  58. return cache_addr_end;
  59. }
  60. void SetCacheAddr(const CacheAddr new_addr) {
  61. cache_addr = new_addr;
  62. cache_addr_end = new_addr + guest_memory_size;
  63. }
  64. const SurfaceParams& GetSurfaceParams() const {
  65. return params;
  66. }
  67. std::size_t GetSizeInBytes() const {
  68. return guest_memory_size;
  69. }
  70. std::size_t GetHostSizeInBytes() const {
  71. return host_memory_size;
  72. }
  73. std::size_t GetMipmapSize(const u32 level) const {
  74. return mipmap_sizes[level];
  75. }
  76. bool MatchFormat(VideoCore::Surface::PixelFormat pixel_format) const {
  77. return params.pixel_format == pixel_format;
  78. }
  79. bool MatchTarget(VideoCore::Surface::SurfaceTarget target) const {
  80. return params.target == target;
  81. }
  82. bool MatchesTopology(const SurfaceParams& rhs) const {
  83. const u32 src_bpp{params.GetBytesPerPixel()};
  84. const u32 dst_bpp{rhs.GetBytesPerPixel()};
  85. return std::tie(src_bpp, params.is_tiled) == std::tie(dst_bpp, rhs.is_tiled);
  86. }
  87. MatchStructureResult MatchesStructure(const SurfaceParams& rhs) const {
  88. if (!params.is_tiled) {
  89. if (std::tie(params.width, params.height, params.pitch) ==
  90. std::tie(rhs.width, rhs.height, rhs.pitch)) {
  91. return MatchStructureResult::FullMatch;
  92. }
  93. return MatchStructureResult::None;
  94. }
  95. // Tiled surface
  96. if (std::tie(params.height, params.depth, params.block_width, params.block_height,
  97. params.block_depth, params.tile_width_spacing) ==
  98. std::tie(rhs.height, rhs.depth, rhs.block_width, rhs.block_height, rhs.block_depth,
  99. rhs.tile_width_spacing)) {
  100. if (params.width == rhs.width) {
  101. return MatchStructureResult::FullMatch;
  102. }
  103. if (params.GetBlockAlignedWidth() == rhs.GetBlockAlignedWidth()) {
  104. return MatchStructureResult::SemiMatch;
  105. }
  106. }
  107. return MatchStructureResult::None;
  108. }
  109. std::optional<std::pair<u32, u32>> GetLayerMipmap(const GPUVAddr candidate_gpu_addr) const {
  110. if (candidate_gpu_addr < gpu_addr) {
  111. return {};
  112. }
  113. const auto relative_address{static_cast<GPUVAddr>(candidate_gpu_addr - gpu_addr)};
  114. const auto layer{static_cast<u32>(relative_address / layer_size)};
  115. const GPUVAddr mipmap_address = relative_address - layer_size * layer;
  116. const auto mipmap_it =
  117. binary_find(mipmap_offsets.begin(), mipmap_offsets.end(), mipmap_address);
  118. if (mipmap_it == mipmap_offsets.end()) {
  119. return {};
  120. }
  121. const auto level{static_cast<u32>(std::distance(mipmap_offsets.begin(), mipmap_it))};
  122. return std::make_pair(layer, level);
  123. }
  124. std::vector<CopyParams> BreakDown(const SurfaceParams& in_params) const {
  125. return params.is_layered ? BreakDownLayered(in_params) : BreakDownNonLayered(in_params);
  126. }
  127. protected:
  128. explicit SurfaceBaseImpl(GPUVAddr gpu_addr, const SurfaceParams& params);
  129. ~SurfaceBaseImpl() = default;
  130. virtual void DecorateSurfaceName() = 0;
  131. const SurfaceParams params;
  132. const std::size_t layer_size;
  133. const std::size_t guest_memory_size;
  134. const std::size_t host_memory_size;
  135. GPUVAddr gpu_addr{};
  136. CacheAddr cache_addr{};
  137. CacheAddr cache_addr_end{};
  138. VAddr cpu_addr{};
  139. std::vector<std::size_t> mipmap_sizes;
  140. std::vector<std::size_t> mipmap_offsets;
  141. private:
  142. void SwizzleFunc(MortonSwizzleMode mode, u8* memory, const SurfaceParams& params, u8* buffer,
  143. u32 level);
  144. std::vector<CopyParams> BreakDownLayered(const SurfaceParams& in_params) const {
  145. const u32 layers{params.depth};
  146. const u32 mipmaps{params.num_levels};
  147. std::vector<CopyParams> result;
  148. result.reserve(static_cast<std::size_t>(layers) * static_cast<std::size_t>(mipmaps));
  149. for (u32 layer = 0; layer < layers; layer++) {
  150. for (u32 level = 0; level < mipmaps; level++) {
  151. const u32 width{std::min(params.GetMipWidth(level), in_params.GetMipWidth(level))};
  152. const u32 height{
  153. std::min(params.GetMipHeight(level), in_params.GetMipHeight(level))};
  154. result.emplace_back(width, height, layer, level);
  155. }
  156. }
  157. return result;
  158. }
  159. std::vector<CopyParams> BreakDownNonLayered(const SurfaceParams& in_params) const {
  160. const u32 mipmaps{params.num_levels};
  161. std::vector<CopyParams> result;
  162. result.reserve(mipmaps);
  163. for (u32 level = 0; level < mipmaps; level++) {
  164. const u32 width{std::min(params.GetMipWidth(level), in_params.GetMipWidth(level))};
  165. const u32 height{std::min(params.GetMipHeight(level), in_params.GetMipHeight(level))};
  166. const u32 depth{std::min(params.GetMipDepth(level), in_params.GetMipDepth(level))};
  167. result.emplace_back(width, height, depth, level);
  168. }
  169. return result;
  170. }
  171. };
  172. template <typename TView>
  173. class SurfaceBase : public SurfaceBaseImpl {
  174. public:
  175. virtual void UploadTexture(std::vector<u8>& staging_buffer) = 0;
  176. virtual void DownloadTexture(std::vector<u8>& staging_buffer) = 0;
  177. void MarkAsModified(const bool is_modified_, const u64 tick) {
  178. is_modified = is_modified_ || is_protected;
  179. modification_tick = tick;
  180. }
  181. void MarkAsProtected(const bool is_protected) {
  182. this->is_protected = is_protected;
  183. }
  184. void MarkAsPicked(const bool is_picked) {
  185. this->is_picked = is_picked;
  186. }
  187. bool IsModified() const {
  188. return is_modified;
  189. }
  190. bool IsProtected() const {
  191. return is_protected;
  192. }
  193. bool IsRegistered() const {
  194. return is_registered;
  195. }
  196. bool IsPicked() const {
  197. return is_picked;
  198. }
  199. void MarkAsRegistered(bool is_reg) {
  200. is_registered = is_reg;
  201. }
  202. u64 GetModificationTick() const {
  203. return modification_tick;
  204. }
  205. TView EmplaceOverview(const SurfaceParams& overview_params) {
  206. const u32 num_layers{params.is_layered && !overview_params.is_layered ? 1 : params.depth};
  207. const ViewParams view_params(overview_params.target, 0, num_layers, 0, params.num_levels);
  208. return GetView(view_params);
  209. }
  210. std::optional<TView> EmplaceView(const SurfaceParams& view_params, const GPUVAddr view_addr) {
  211. if (view_addr < gpu_addr || params.target == SurfaceTarget::Texture3D ||
  212. view_params.target == SurfaceTarget::Texture3D) {
  213. return {};
  214. }
  215. const std::size_t size{view_params.GetGuestSizeInBytes()};
  216. const auto layer_mipmap{GetLayerMipmap(view_addr)};
  217. if (!layer_mipmap) {
  218. return {};
  219. }
  220. const u32 layer{layer_mipmap->first};
  221. const u32 mipmap{layer_mipmap->second};
  222. if (GetMipmapSize(mipmap) != size) {
  223. // TODO: The view may cover many mimaps, this case can still go on.
  224. // This edge-case can be safely be ignored since it will just result in worse
  225. // performance.
  226. return {};
  227. }
  228. return GetView(ViewParams(params.target, layer, 1, mipmap, 1));
  229. }
  230. TView GetMainView() const {
  231. return main_view;
  232. }
  233. protected:
  234. explicit SurfaceBase(const GPUVAddr gpu_addr, const SurfaceParams& params)
  235. : SurfaceBaseImpl(gpu_addr, params) {}
  236. ~SurfaceBase() = default;
  237. virtual TView CreateView(const ViewParams& view_key) = 0;
  238. std::unordered_map<ViewParams, TView> views;
  239. TView main_view;
  240. private:
  241. TView GetView(const ViewParams& key) {
  242. const auto [entry, is_cache_miss] = views.try_emplace(key);
  243. auto& view{entry->second};
  244. if (is_cache_miss) {
  245. view = CreateView(key);
  246. }
  247. return view;
  248. }
  249. bool is_modified{};
  250. bool is_protected{};
  251. bool is_registered{};
  252. bool is_picked{};
  253. u64 modification_tick{};
  254. };
  255. } // namespace VideoCommon