surface_base.cpp 12 KB

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