surface_base.cpp 12 KB

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