surface_base.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/algorithm.h"
  5. #include "common/assert.h"
  6. #include "common/common_types.h"
  7. #include "common/microprofile.h"
  8. #include "video_core/memory_manager.h"
  9. #include "video_core/texture_cache/surface_base.h"
  10. #include "video_core/texture_cache/surface_params.h"
  11. #include "video_core/textures/convert.h"
  12. namespace VideoCommon {
  13. MICROPROFILE_DEFINE(GPU_Load_Texture, "GPU", "Texture Load", MP_RGB(128, 192, 128));
  14. MICROPROFILE_DEFINE(GPU_Flush_Texture, "GPU", "Texture Flush", MP_RGB(128, 192, 128));
  15. using Tegra::Texture::ConvertFromGuestToHost;
  16. using VideoCore::MortonSwizzleMode;
  17. using VideoCore::Surface::SurfaceCompression;
  18. StagingCache::StagingCache() = default;
  19. StagingCache::~StagingCache() = default;
  20. SurfaceBaseImpl::SurfaceBaseImpl(GPUVAddr gpu_addr, const SurfaceParams& params)
  21. : params{params}, host_memory_size{params.GetHostSizeInBytes()}, gpu_addr{gpu_addr},
  22. mipmap_sizes(params.num_levels), mipmap_offsets(params.num_levels) {
  23. std::size_t offset = 0;
  24. for (u32 level = 0; level < params.num_levels; ++level) {
  25. const std::size_t mipmap_size{params.GetGuestMipmapSize(level)};
  26. mipmap_sizes[level] = mipmap_size;
  27. mipmap_offsets[level] = offset;
  28. offset += mipmap_size;
  29. }
  30. layer_size = offset;
  31. if (params.is_layered) {
  32. if (params.is_tiled) {
  33. layer_size =
  34. SurfaceParams::AlignLayered(layer_size, params.block_height, params.block_depth);
  35. }
  36. guest_memory_size = layer_size * params.depth;
  37. } else {
  38. guest_memory_size = layer_size;
  39. }
  40. }
  41. MatchTopologyResult SurfaceBaseImpl::MatchesTopology(const SurfaceParams& rhs) const {
  42. const u32 src_bpp{params.GetBytesPerPixel()};
  43. const u32 dst_bpp{rhs.GetBytesPerPixel()};
  44. const bool ib1 = params.IsBuffer();
  45. const bool ib2 = rhs.IsBuffer();
  46. if (std::tie(src_bpp, params.is_tiled, ib1) == std::tie(dst_bpp, rhs.is_tiled, ib2)) {
  47. const bool cb1 = params.IsCompressed();
  48. const bool cb2 = rhs.IsCompressed();
  49. if (cb1 == cb2) {
  50. return MatchTopologyResult::FullMatch;
  51. }
  52. return MatchTopologyResult::CompressUnmatch;
  53. }
  54. return MatchTopologyResult::None;
  55. }
  56. MatchStructureResult SurfaceBaseImpl::MatchesStructure(const SurfaceParams& rhs) const {
  57. // Buffer surface Check
  58. if (params.IsBuffer()) {
  59. const std::size_t wd1 = params.width * params.GetBytesPerPixel();
  60. const std::size_t wd2 = rhs.width * rhs.GetBytesPerPixel();
  61. if (wd1 == wd2) {
  62. return MatchStructureResult::FullMatch;
  63. }
  64. return MatchStructureResult::None;
  65. }
  66. // Linear Surface check
  67. if (!params.is_tiled) {
  68. if (std::tie(params.height, params.pitch) == std::tie(rhs.height, rhs.pitch)) {
  69. if (params.width == rhs.width) {
  70. return MatchStructureResult::FullMatch;
  71. } else {
  72. return MatchStructureResult::SemiMatch;
  73. }
  74. }
  75. return MatchStructureResult::None;
  76. }
  77. // Tiled Surface check
  78. if (std::tie(params.depth, params.block_width, params.block_height, params.block_depth,
  79. params.tile_width_spacing, params.num_levels) ==
  80. std::tie(rhs.depth, rhs.block_width, rhs.block_height, rhs.block_depth,
  81. rhs.tile_width_spacing, rhs.num_levels)) {
  82. if (std::tie(params.width, params.height) == std::tie(rhs.width, rhs.height)) {
  83. return MatchStructureResult::FullMatch;
  84. }
  85. const u32 ws = SurfaceParams::ConvertWidth(rhs.GetBlockAlignedWidth(), params.pixel_format,
  86. rhs.pixel_format);
  87. const u32 hs =
  88. SurfaceParams::ConvertHeight(rhs.height, params.pixel_format, rhs.pixel_format);
  89. const u32 w1 = params.GetBlockAlignedWidth();
  90. if (std::tie(w1, params.height) == std::tie(ws, hs)) {
  91. return MatchStructureResult::SemiMatch;
  92. }
  93. }
  94. return MatchStructureResult::None;
  95. }
  96. std::optional<std::pair<u32, u32>> SurfaceBaseImpl::GetLayerMipmap(
  97. const GPUVAddr candidate_gpu_addr) const {
  98. if (gpu_addr == candidate_gpu_addr) {
  99. return {{0, 0}};
  100. }
  101. if (candidate_gpu_addr < gpu_addr) {
  102. return {};
  103. }
  104. const auto relative_address{static_cast<GPUVAddr>(candidate_gpu_addr - gpu_addr)};
  105. const auto layer{static_cast<u32>(relative_address / layer_size)};
  106. const GPUVAddr mipmap_address = relative_address - layer_size * layer;
  107. const auto mipmap_it =
  108. Common::BinaryFind(mipmap_offsets.begin(), mipmap_offsets.end(), mipmap_address);
  109. if (mipmap_it == mipmap_offsets.end()) {
  110. return {};
  111. }
  112. const auto level{static_cast<u32>(std::distance(mipmap_offsets.begin(), mipmap_it))};
  113. return std::make_pair(layer, level);
  114. }
  115. std::vector<CopyParams> SurfaceBaseImpl::BreakDownLayered(const SurfaceParams& in_params) const {
  116. const u32 layers{params.depth};
  117. const u32 mipmaps{params.num_levels};
  118. std::vector<CopyParams> result;
  119. result.reserve(static_cast<std::size_t>(layers) * static_cast<std::size_t>(mipmaps));
  120. for (u32 layer = 0; layer < layers; layer++) {
  121. for (u32 level = 0; level < mipmaps; level++) {
  122. const u32 width = SurfaceParams::IntersectWidth(params, in_params, level, level);
  123. const u32 height = SurfaceParams::IntersectHeight(params, in_params, level, level);
  124. result.emplace_back(width, height, layer, level);
  125. }
  126. }
  127. return result;
  128. }
  129. std::vector<CopyParams> SurfaceBaseImpl::BreakDownNonLayered(const SurfaceParams& in_params) const {
  130. const u32 mipmaps{params.num_levels};
  131. std::vector<CopyParams> result;
  132. result.reserve(mipmaps);
  133. for (u32 level = 0; level < mipmaps; level++) {
  134. const u32 width = SurfaceParams::IntersectWidth(params, in_params, level, level);
  135. const u32 height = SurfaceParams::IntersectHeight(params, in_params, level, level);
  136. const u32 depth{std::min(params.GetMipDepth(level), in_params.GetMipDepth(level))};
  137. result.emplace_back(width, height, depth, level);
  138. }
  139. return result;
  140. }
  141. void SurfaceBaseImpl::SwizzleFunc(MortonSwizzleMode mode, u8* memory, const SurfaceParams& params,
  142. u8* buffer, u32 level) {
  143. const u32 width{params.GetMipWidth(level)};
  144. const u32 height{params.GetMipHeight(level)};
  145. const u32 block_height{params.GetMipBlockHeight(level)};
  146. const u32 block_depth{params.GetMipBlockDepth(level)};
  147. std::size_t guest_offset{mipmap_offsets[level]};
  148. if (params.is_layered) {
  149. std::size_t host_offset{0};
  150. const std::size_t guest_stride = layer_size;
  151. const std::size_t host_stride = params.GetHostLayerSize(level);
  152. for (u32 layer = 0; layer < params.depth; ++layer) {
  153. MortonSwizzle(mode, params.pixel_format, width, block_height, height, block_depth, 1,
  154. params.tile_width_spacing, buffer + host_offset, memory + guest_offset);
  155. guest_offset += guest_stride;
  156. host_offset += host_stride;
  157. }
  158. } else {
  159. MortonSwizzle(mode, params.pixel_format, width, block_height, height, block_depth,
  160. params.GetMipDepth(level), params.tile_width_spacing, buffer,
  161. memory + guest_offset);
  162. }
  163. }
  164. void SurfaceBaseImpl::LoadBuffer(Tegra::MemoryManager& memory_manager,
  165. StagingCache& staging_cache) {
  166. MICROPROFILE_SCOPE(GPU_Load_Texture);
  167. auto& staging_buffer = staging_cache.GetBuffer(0);
  168. u8* host_ptr;
  169. is_continuous = memory_manager.IsBlockContinuous(gpu_addr, guest_memory_size);
  170. // Handle continuouty
  171. if (is_continuous) {
  172. // Use physical memory directly
  173. host_ptr = memory_manager.GetPointer(gpu_addr);
  174. if (!host_ptr) {
  175. return;
  176. }
  177. } else {
  178. // Use an extra temporal buffer
  179. auto& tmp_buffer = staging_cache.GetBuffer(1);
  180. tmp_buffer.resize(guest_memory_size);
  181. host_ptr = tmp_buffer.data();
  182. memory_manager.ReadBlockUnsafe(gpu_addr, host_ptr, guest_memory_size);
  183. }
  184. if (params.is_tiled) {
  185. ASSERT_MSG(params.block_width == 0, "Block width is defined as {} on texture target {}",
  186. params.block_width, static_cast<u32>(params.target));
  187. for (u32 level = 0; level < params.num_levels; ++level) {
  188. const std::size_t host_offset{params.GetHostMipmapLevelOffset(level)};
  189. SwizzleFunc(MortonSwizzleMode::MortonToLinear, host_ptr, params,
  190. staging_buffer.data() + host_offset, level);
  191. }
  192. } else {
  193. ASSERT_MSG(params.num_levels == 1, "Linear mipmap loading is not implemented");
  194. const u32 bpp{params.GetBytesPerPixel()};
  195. const u32 block_width{params.GetDefaultBlockWidth()};
  196. const u32 block_height{params.GetDefaultBlockHeight()};
  197. const u32 width{(params.width + block_width - 1) / block_width};
  198. const u32 height{(params.height + block_height - 1) / block_height};
  199. const u32 copy_size{width * bpp};
  200. if (params.pitch == copy_size) {
  201. std::memcpy(staging_buffer.data(), host_ptr, params.GetHostSizeInBytes());
  202. } else {
  203. const u8* start{host_ptr};
  204. u8* write_to{staging_buffer.data()};
  205. for (u32 h = height; h > 0; --h) {
  206. std::memcpy(write_to, start, copy_size);
  207. start += params.pitch;
  208. write_to += copy_size;
  209. }
  210. }
  211. }
  212. auto compression_type = params.GetCompressionType();
  213. if (compression_type == SurfaceCompression::None ||
  214. compression_type == SurfaceCompression::Compressed)
  215. return;
  216. for (u32 level_up = params.num_levels; level_up > 0; --level_up) {
  217. const u32 level = level_up - 1;
  218. const std::size_t in_host_offset{params.GetHostMipmapLevelOffset(level)};
  219. const std::size_t out_host_offset = compression_type == SurfaceCompression::Rearranged
  220. ? in_host_offset
  221. : params.GetConvertedMipmapOffset(level);
  222. u8* in_buffer = staging_buffer.data() + in_host_offset;
  223. u8* out_buffer = staging_buffer.data() + out_host_offset;
  224. ConvertFromGuestToHost(in_buffer, out_buffer, params.pixel_format,
  225. params.GetMipWidth(level), params.GetMipHeight(level),
  226. params.GetMipDepth(level), true, true);
  227. }
  228. }
  229. void SurfaceBaseImpl::FlushBuffer(Tegra::MemoryManager& memory_manager,
  230. StagingCache& staging_cache) {
  231. MICROPROFILE_SCOPE(GPU_Flush_Texture);
  232. auto& staging_buffer = staging_cache.GetBuffer(0);
  233. u8* host_ptr;
  234. // Handle continuouty
  235. if (is_continuous) {
  236. // Use physical memory directly
  237. host_ptr = memory_manager.GetPointer(gpu_addr);
  238. if (!host_ptr) {
  239. return;
  240. }
  241. } else {
  242. // Use an extra temporal buffer
  243. auto& tmp_buffer = staging_cache.GetBuffer(1);
  244. tmp_buffer.resize(guest_memory_size);
  245. host_ptr = tmp_buffer.data();
  246. }
  247. if (params.is_tiled) {
  248. ASSERT_MSG(params.block_width == 0, "Block width is defined as {}", params.block_width);
  249. for (u32 level = 0; level < params.num_levels; ++level) {
  250. const std::size_t host_offset{params.GetHostMipmapLevelOffset(level)};
  251. SwizzleFunc(MortonSwizzleMode::LinearToMorton, host_ptr, params,
  252. staging_buffer.data() + host_offset, level);
  253. }
  254. } else {
  255. ASSERT(params.target == SurfaceTarget::Texture2D);
  256. ASSERT(params.num_levels == 1);
  257. const u32 bpp{params.GetBytesPerPixel()};
  258. const u32 copy_size{params.width * bpp};
  259. if (params.pitch == copy_size) {
  260. std::memcpy(host_ptr, staging_buffer.data(), guest_memory_size);
  261. } else {
  262. u8* start{host_ptr};
  263. const u8* read_to{staging_buffer.data()};
  264. for (u32 h = params.height; h > 0; --h) {
  265. std::memcpy(start, read_to, copy_size);
  266. start += params.pitch;
  267. read_to += copy_size;
  268. }
  269. }
  270. }
  271. if (!is_continuous) {
  272. memory_manager.WriteBlockUnsafe(gpu_addr, host_ptr, guest_memory_size);
  273. }
  274. }
  275. } // namespace VideoCommon