surface_base.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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::IsPixelFormatASTC;
  18. using VideoCore::Surface::PixelFormat;
  19. StagingCache::StagingCache() = default;
  20. StagingCache::~StagingCache() = default;
  21. SurfaceBaseImpl::SurfaceBaseImpl(GPUVAddr gpu_addr, const SurfaceParams& params,
  22. bool is_astc_supported)
  23. : params{params}, gpu_addr{gpu_addr}, mipmap_sizes(params.num_levels),
  24. mipmap_offsets(params.num_levels) {
  25. is_converted = IsPixelFormatASTC(params.pixel_format) && !is_astc_supported;
  26. host_memory_size = params.GetHostSizeInBytes(is_converted);
  27. std::size_t offset = 0;
  28. for (u32 level = 0; level < params.num_levels; ++level) {
  29. const std::size_t mipmap_size{params.GetGuestMipmapSize(level)};
  30. mipmap_sizes[level] = mipmap_size;
  31. mipmap_offsets[level] = offset;
  32. offset += mipmap_size;
  33. }
  34. layer_size = offset;
  35. if (params.is_layered) {
  36. if (params.is_tiled) {
  37. layer_size =
  38. SurfaceParams::AlignLayered(layer_size, params.block_height, params.block_depth);
  39. }
  40. guest_memory_size = layer_size * params.depth;
  41. } else {
  42. guest_memory_size = layer_size;
  43. }
  44. }
  45. MatchTopologyResult SurfaceBaseImpl::MatchesTopology(const SurfaceParams& rhs) const {
  46. const u32 src_bpp{params.GetBytesPerPixel()};
  47. const u32 dst_bpp{rhs.GetBytesPerPixel()};
  48. const bool ib1 = params.IsBuffer();
  49. const bool ib2 = rhs.IsBuffer();
  50. if (std::tie(src_bpp, params.is_tiled, ib1) == std::tie(dst_bpp, rhs.is_tiled, ib2)) {
  51. const bool cb1 = params.IsCompressed();
  52. const bool cb2 = rhs.IsCompressed();
  53. if (cb1 == cb2) {
  54. return MatchTopologyResult::FullMatch;
  55. }
  56. return MatchTopologyResult::CompressUnmatch;
  57. }
  58. return MatchTopologyResult::None;
  59. }
  60. MatchStructureResult SurfaceBaseImpl::MatchesStructure(const SurfaceParams& rhs) const {
  61. // Buffer surface Check
  62. if (params.IsBuffer()) {
  63. const std::size_t wd1 = params.width * params.GetBytesPerPixel();
  64. const std::size_t wd2 = rhs.width * rhs.GetBytesPerPixel();
  65. if (wd1 == wd2) {
  66. return MatchStructureResult::FullMatch;
  67. }
  68. return MatchStructureResult::None;
  69. }
  70. // Linear Surface check
  71. if (!params.is_tiled) {
  72. if (std::tie(params.height, params.pitch) == std::tie(rhs.height, rhs.pitch)) {
  73. if (params.width == rhs.width) {
  74. return MatchStructureResult::FullMatch;
  75. } else {
  76. return MatchStructureResult::SemiMatch;
  77. }
  78. }
  79. return MatchStructureResult::None;
  80. }
  81. // Tiled Surface check
  82. if (std::tie(params.depth, params.block_width, params.block_height, params.block_depth,
  83. params.tile_width_spacing, params.num_levels) ==
  84. std::tie(rhs.depth, rhs.block_width, rhs.block_height, rhs.block_depth,
  85. rhs.tile_width_spacing, rhs.num_levels)) {
  86. if (std::tie(params.width, params.height) == std::tie(rhs.width, rhs.height)) {
  87. return MatchStructureResult::FullMatch;
  88. }
  89. const u32 ws = SurfaceParams::ConvertWidth(rhs.GetBlockAlignedWidth(), params.pixel_format,
  90. rhs.pixel_format);
  91. const u32 hs =
  92. SurfaceParams::ConvertHeight(rhs.height, params.pixel_format, rhs.pixel_format);
  93. const u32 w1 = params.GetBlockAlignedWidth();
  94. if (std::tie(w1, params.height) == std::tie(ws, hs)) {
  95. return MatchStructureResult::SemiMatch;
  96. }
  97. }
  98. return MatchStructureResult::None;
  99. }
  100. std::optional<std::pair<u32, u32>> SurfaceBaseImpl::GetLayerMipmap(
  101. const GPUVAddr candidate_gpu_addr) const {
  102. if (gpu_addr == candidate_gpu_addr) {
  103. return {{0, 0}};
  104. }
  105. if (candidate_gpu_addr < gpu_addr) {
  106. return {};
  107. }
  108. const auto relative_address{static_cast<GPUVAddr>(candidate_gpu_addr - gpu_addr)};
  109. const auto layer{static_cast<u32>(relative_address / layer_size)};
  110. const GPUVAddr mipmap_address = relative_address - layer_size * layer;
  111. const auto mipmap_it =
  112. Common::BinaryFind(mipmap_offsets.begin(), mipmap_offsets.end(), mipmap_address);
  113. if (mipmap_it == mipmap_offsets.end()) {
  114. return {};
  115. }
  116. const auto level{static_cast<u32>(std::distance(mipmap_offsets.begin(), mipmap_it))};
  117. return std::make_pair(layer, level);
  118. }
  119. std::vector<CopyParams> SurfaceBaseImpl::BreakDownLayered(const SurfaceParams& in_params) const {
  120. const u32 layers{params.depth};
  121. const u32 mipmaps{params.num_levels};
  122. std::vector<CopyParams> result;
  123. result.reserve(static_cast<std::size_t>(layers) * static_cast<std::size_t>(mipmaps));
  124. for (u32 layer = 0; layer < layers; layer++) {
  125. for (u32 level = 0; level < mipmaps; level++) {
  126. const u32 width = SurfaceParams::IntersectWidth(params, in_params, level, level);
  127. const u32 height = SurfaceParams::IntersectHeight(params, in_params, level, level);
  128. result.emplace_back(0, 0, layer, 0, 0, layer, level, level, width, height, 1);
  129. }
  130. }
  131. return result;
  132. }
  133. std::vector<CopyParams> SurfaceBaseImpl::BreakDownNonLayered(const SurfaceParams& in_params) const {
  134. const u32 mipmaps{params.num_levels};
  135. std::vector<CopyParams> result;
  136. result.reserve(mipmaps);
  137. for (u32 level = 0; level < mipmaps; level++) {
  138. const u32 width = SurfaceParams::IntersectWidth(params, in_params, level, level);
  139. const u32 height = SurfaceParams::IntersectHeight(params, in_params, level, level);
  140. const u32 depth{std::min(params.GetMipDepth(level), in_params.GetMipDepth(level))};
  141. result.emplace_back(width, height, depth, level);
  142. }
  143. return result;
  144. }
  145. void SurfaceBaseImpl::SwizzleFunc(MortonSwizzleMode mode, u8* memory, const SurfaceParams& params,
  146. u8* buffer, u32 level) {
  147. const u32 width{params.GetMipWidth(level)};
  148. const u32 height{params.GetMipHeight(level)};
  149. const u32 block_height{params.GetMipBlockHeight(level)};
  150. const u32 block_depth{params.GetMipBlockDepth(level)};
  151. std::size_t guest_offset{mipmap_offsets[level]};
  152. if (params.is_layered) {
  153. std::size_t host_offset = 0;
  154. const std::size_t guest_stride = layer_size;
  155. const std::size_t host_stride = params.GetHostLayerSize(level);
  156. for (u32 layer = 0; layer < params.depth; ++layer) {
  157. MortonSwizzle(mode, params.pixel_format, width, block_height, height, block_depth, 1,
  158. params.tile_width_spacing, buffer + host_offset, memory + guest_offset);
  159. guest_offset += guest_stride;
  160. host_offset += host_stride;
  161. }
  162. } else {
  163. MortonSwizzle(mode, params.pixel_format, width, block_height, height, block_depth,
  164. params.GetMipDepth(level), params.tile_width_spacing, buffer,
  165. memory + guest_offset);
  166. }
  167. }
  168. void SurfaceBaseImpl::LoadBuffer(Tegra::MemoryManager& memory_manager,
  169. StagingCache& staging_cache) {
  170. MICROPROFILE_SCOPE(GPU_Load_Texture);
  171. auto& staging_buffer = staging_cache.GetBuffer(0);
  172. u8* host_ptr;
  173. is_continuous = memory_manager.IsBlockContinuous(gpu_addr, guest_memory_size);
  174. // Handle continuouty
  175. if (is_continuous) {
  176. // Use physical memory directly
  177. host_ptr = memory_manager.GetPointer(gpu_addr);
  178. if (!host_ptr) {
  179. return;
  180. }
  181. } else {
  182. // Use an extra temporal buffer
  183. auto& tmp_buffer = staging_cache.GetBuffer(1);
  184. tmp_buffer.resize(guest_memory_size);
  185. host_ptr = tmp_buffer.data();
  186. memory_manager.ReadBlockUnsafe(gpu_addr, host_ptr, guest_memory_size);
  187. }
  188. if (params.is_tiled) {
  189. ASSERT_MSG(params.block_width == 0, "Block width is defined as {} on texture target {}",
  190. params.block_width, static_cast<u32>(params.target));
  191. for (u32 level = 0; level < params.num_levels; ++level) {
  192. const std::size_t host_offset{params.GetHostMipmapLevelOffset(level, false)};
  193. SwizzleFunc(MortonSwizzleMode::MortonToLinear, host_ptr, params,
  194. staging_buffer.data() + host_offset, level);
  195. }
  196. } else {
  197. ASSERT_MSG(params.num_levels == 1, "Linear mipmap loading is not implemented");
  198. const u32 bpp{params.GetBytesPerPixel()};
  199. const u32 block_width{params.GetDefaultBlockWidth()};
  200. const u32 block_height{params.GetDefaultBlockHeight()};
  201. const u32 width{(params.width + block_width - 1) / block_width};
  202. const u32 height{(params.height + block_height - 1) / block_height};
  203. const u32 copy_size{width * bpp};
  204. if (params.pitch == copy_size) {
  205. std::memcpy(staging_buffer.data(), host_ptr, params.GetHostSizeInBytes(false));
  206. } else {
  207. const u8* start{host_ptr};
  208. u8* write_to{staging_buffer.data()};
  209. for (u32 h = height; h > 0; --h) {
  210. std::memcpy(write_to, start, copy_size);
  211. start += params.pitch;
  212. write_to += copy_size;
  213. }
  214. }
  215. }
  216. if (!is_converted && params.pixel_format != PixelFormat::S8Z24) {
  217. return;
  218. }
  219. for (u32 level = params.num_levels; level--;) {
  220. const std::size_t in_host_offset{params.GetHostMipmapLevelOffset(level, false)};
  221. const std::size_t out_host_offset{params.GetHostMipmapLevelOffset(level, is_converted)};
  222. u8* const in_buffer = staging_buffer.data() + in_host_offset;
  223. u8* const 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, false)};
  251. SwizzleFunc(MortonSwizzleMode::LinearToMorton, host_ptr, params,
  252. staging_buffer.data() + host_offset, level);
  253. }
  254. } else if (params.IsBuffer()) {
  255. // Buffers don't have pitch or any fancy layout property. We can just memcpy them to guest
  256. // memory.
  257. std::memcpy(host_ptr, staging_buffer.data(), guest_memory_size);
  258. } else {
  259. ASSERT(params.target == SurfaceTarget::Texture2D);
  260. ASSERT(params.num_levels == 1);
  261. const u32 bpp{params.GetBytesPerPixel()};
  262. const u32 copy_size{params.width * bpp};
  263. if (params.pitch == copy_size) {
  264. std::memcpy(host_ptr, staging_buffer.data(), guest_memory_size);
  265. } else {
  266. u8* start{host_ptr};
  267. const u8* read_to{staging_buffer.data()};
  268. for (u32 h = params.height; h > 0; --h) {
  269. std::memcpy(start, read_to, copy_size);
  270. start += params.pitch;
  271. read_to += copy_size;
  272. }
  273. }
  274. }
  275. if (!is_continuous) {
  276. memory_manager.WriteBlockUnsafe(gpu_addr, host_ptr, guest_memory_size);
  277. }
  278. }
  279. } // namespace VideoCommon