surface_base.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 std::nullopt;
  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. if (layer >= params.depth) {
  111. return std::nullopt;
  112. }
  113. const GPUVAddr mipmap_address = relative_address - layer_size * layer;
  114. const auto mipmap_it =
  115. Common::BinaryFind(mipmap_offsets.begin(), mipmap_offsets.end(), mipmap_address);
  116. if (mipmap_it == mipmap_offsets.end()) {
  117. return std::nullopt;
  118. }
  119. const auto level{static_cast<u32>(std::distance(mipmap_offsets.begin(), mipmap_it))};
  120. return std::make_pair(layer, level);
  121. }
  122. std::vector<CopyParams> SurfaceBaseImpl::BreakDownLayered(const SurfaceParams& in_params) const {
  123. const u32 layers{params.depth};
  124. const u32 mipmaps{params.num_levels};
  125. std::vector<CopyParams> result;
  126. result.reserve(static_cast<std::size_t>(layers) * static_cast<std::size_t>(mipmaps));
  127. for (u32 layer = 0; layer < layers; layer++) {
  128. for (u32 level = 0; level < mipmaps; level++) {
  129. const u32 width = SurfaceParams::IntersectWidth(params, in_params, level, level);
  130. const u32 height = SurfaceParams::IntersectHeight(params, in_params, level, level);
  131. result.emplace_back(0, 0, layer, 0, 0, layer, level, level, width, height, 1);
  132. }
  133. }
  134. return result;
  135. }
  136. std::vector<CopyParams> SurfaceBaseImpl::BreakDownNonLayered(const SurfaceParams& in_params) const {
  137. const u32 mipmaps{params.num_levels};
  138. std::vector<CopyParams> result;
  139. result.reserve(mipmaps);
  140. for (u32 level = 0; level < mipmaps; level++) {
  141. const u32 width = SurfaceParams::IntersectWidth(params, in_params, level, level);
  142. const u32 height = SurfaceParams::IntersectHeight(params, in_params, level, level);
  143. const u32 depth{std::min(params.GetMipDepth(level), in_params.GetMipDepth(level))};
  144. result.emplace_back(width, height, depth, level);
  145. }
  146. return result;
  147. }
  148. void SurfaceBaseImpl::SwizzleFunc(MortonSwizzleMode mode, u8* memory,
  149. const SurfaceParams& surface_params, u8* buffer, u32 level) {
  150. const u32 width{surface_params.GetMipWidth(level)};
  151. const u32 height{surface_params.GetMipHeight(level)};
  152. const u32 block_height{surface_params.GetMipBlockHeight(level)};
  153. const u32 block_depth{surface_params.GetMipBlockDepth(level)};
  154. std::size_t guest_offset{mipmap_offsets[level]};
  155. if (surface_params.is_layered) {
  156. std::size_t host_offset = 0;
  157. const std::size_t guest_stride = layer_size;
  158. const std::size_t host_stride = surface_params.GetHostLayerSize(level);
  159. for (u32 layer = 0; layer < surface_params.depth; ++layer) {
  160. MortonSwizzle(mode, surface_params.pixel_format, width, block_height, height,
  161. block_depth, 1, surface_params.tile_width_spacing, buffer + host_offset,
  162. memory + guest_offset);
  163. guest_offset += guest_stride;
  164. host_offset += host_stride;
  165. }
  166. } else {
  167. MortonSwizzle(mode, surface_params.pixel_format, width, block_height, height, block_depth,
  168. surface_params.GetMipDepth(level), surface_params.tile_width_spacing, buffer,
  169. memory + guest_offset);
  170. }
  171. }
  172. void SurfaceBaseImpl::LoadBuffer(Tegra::MemoryManager& memory_manager,
  173. StagingCache& staging_cache) {
  174. MICROPROFILE_SCOPE(GPU_Load_Texture);
  175. auto& staging_buffer = staging_cache.GetBuffer(0);
  176. u8* host_ptr;
  177. // Use an extra temporal buffer
  178. auto& tmp_buffer = staging_cache.GetBuffer(1);
  179. tmp_buffer.resize(guest_memory_size);
  180. host_ptr = tmp_buffer.data();
  181. memory_manager.ReadBlockUnsafe(gpu_addr, host_ptr, guest_memory_size);
  182. if (params.is_tiled) {
  183. ASSERT_MSG(params.block_width == 0, "Block width is defined as {} on texture target {}",
  184. params.block_width, static_cast<u32>(params.target));
  185. for (u32 level = 0; level < params.num_levels; ++level) {
  186. const std::size_t host_offset{params.GetHostMipmapLevelOffset(level, false)};
  187. SwizzleFunc(MortonSwizzleMode::MortonToLinear, host_ptr, params,
  188. staging_buffer.data() + host_offset, level);
  189. }
  190. } else {
  191. ASSERT_MSG(params.num_levels == 1, "Linear mipmap loading is not implemented");
  192. const u32 bpp{params.GetBytesPerPixel()};
  193. const u32 block_width{params.GetDefaultBlockWidth()};
  194. const u32 block_height{params.GetDefaultBlockHeight()};
  195. const u32 width{(params.width + block_width - 1) / block_width};
  196. const u32 height{(params.height + block_height - 1) / block_height};
  197. const u32 copy_size{width * bpp};
  198. if (params.pitch == copy_size) {
  199. std::memcpy(staging_buffer.data(), host_ptr, params.GetHostSizeInBytes(false));
  200. } else {
  201. const u8* start{host_ptr};
  202. u8* write_to{staging_buffer.data()};
  203. for (u32 h = height; h > 0; --h) {
  204. std::memcpy(write_to, start, copy_size);
  205. start += params.pitch;
  206. write_to += copy_size;
  207. }
  208. }
  209. }
  210. if (!is_converted && params.pixel_format != PixelFormat::S8_UINT_D24_UNORM) {
  211. return;
  212. }
  213. for (u32 level = params.num_levels; level--;) {
  214. const std::size_t in_host_offset{params.GetHostMipmapLevelOffset(level, false)};
  215. const std::size_t out_host_offset{params.GetHostMipmapLevelOffset(level, is_converted)};
  216. u8* const in_buffer = staging_buffer.data() + in_host_offset;
  217. u8* const out_buffer = staging_buffer.data() + out_host_offset;
  218. ConvertFromGuestToHost(in_buffer, out_buffer, params.pixel_format,
  219. params.GetMipWidth(level), params.GetMipHeight(level),
  220. params.GetMipDepth(level), true, true);
  221. }
  222. }
  223. void SurfaceBaseImpl::FlushBuffer(Tegra::MemoryManager& memory_manager,
  224. StagingCache& staging_cache) {
  225. MICROPROFILE_SCOPE(GPU_Flush_Texture);
  226. auto& staging_buffer = staging_cache.GetBuffer(0);
  227. u8* host_ptr;
  228. // Use an extra temporal buffer
  229. auto& tmp_buffer = staging_cache.GetBuffer(1);
  230. tmp_buffer.resize(guest_memory_size);
  231. host_ptr = tmp_buffer.data();
  232. if (params.target == SurfaceTarget::Texture3D) {
  233. // Special case for 3D texture segments
  234. memory_manager.ReadBlockUnsafe(gpu_addr, host_ptr, guest_memory_size);
  235. }
  236. if (params.is_tiled) {
  237. ASSERT_MSG(params.block_width == 0, "Block width is defined as {}", params.block_width);
  238. for (u32 level = 0; level < params.num_levels; ++level) {
  239. const std::size_t host_offset{params.GetHostMipmapLevelOffset(level, false)};
  240. SwizzleFunc(MortonSwizzleMode::LinearToMorton, host_ptr, params,
  241. staging_buffer.data() + host_offset, level);
  242. }
  243. } else if (params.IsBuffer()) {
  244. // Buffers don't have pitch or any fancy layout property. We can just memcpy them to guest
  245. // memory.
  246. std::memcpy(host_ptr, staging_buffer.data(), guest_memory_size);
  247. } else {
  248. ASSERT(params.target == SurfaceTarget::Texture2D);
  249. ASSERT(params.num_levels == 1);
  250. const u32 bpp{params.GetBytesPerPixel()};
  251. const u32 copy_size{params.width * bpp};
  252. if (params.pitch == copy_size) {
  253. std::memcpy(host_ptr, staging_buffer.data(), guest_memory_size);
  254. } else {
  255. u8* start{host_ptr};
  256. const u8* read_to{staging_buffer.data()};
  257. for (u32 h = params.height; h > 0; --h) {
  258. std::memcpy(start, read_to, copy_size);
  259. start += params.pitch;
  260. read_to += copy_size;
  261. }
  262. }
  263. }
  264. memory_manager.WriteBlockUnsafe(gpu_addr, host_ptr, guest_memory_size);
  265. }
  266. } // namespace VideoCommon