util.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. // This files contains code from Ryujinx
  5. // A copy of the code can be obtained from https://github.com/Ryujinx/Ryujinx
  6. // The sections using code from Ryujinx are marked with a link to the original version
  7. // MIT License
  8. //
  9. // Copyright (c) Ryujinx Team and Contributors
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  12. // associated documentation files (the "Software"), to deal in the Software without restriction,
  13. // including without limitation the rights to use, copy, modify, merge, publish, distribute,
  14. // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in all copies or
  18. // substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  21. // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  23. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. #include <algorithm>
  27. #include <array>
  28. #include <numeric>
  29. #include <optional>
  30. #include <span>
  31. #include <vector>
  32. #include "common/alignment.h"
  33. #include "common/assert.h"
  34. #include "common/bit_util.h"
  35. #include "common/common_types.h"
  36. #include "common/div_ceil.h"
  37. #include "video_core/compatible_formats.h"
  38. #include "video_core/engines/maxwell_3d.h"
  39. #include "video_core/memory_manager.h"
  40. #include "video_core/surface.h"
  41. #include "video_core/texture_cache/decode_bc4.h"
  42. #include "video_core/texture_cache/format_lookup_table.h"
  43. #include "video_core/texture_cache/formatter.h"
  44. #include "video_core/texture_cache/samples_helper.h"
  45. #include "video_core/texture_cache/util.h"
  46. #include "video_core/textures/astc.h"
  47. #include "video_core/textures/decoders.h"
  48. namespace VideoCommon {
  49. namespace {
  50. using Tegra::Texture::GOB_SIZE;
  51. using Tegra::Texture::GOB_SIZE_SHIFT;
  52. using Tegra::Texture::GOB_SIZE_X;
  53. using Tegra::Texture::GOB_SIZE_X_SHIFT;
  54. using Tegra::Texture::GOB_SIZE_Y;
  55. using Tegra::Texture::GOB_SIZE_Y_SHIFT;
  56. using Tegra::Texture::GOB_SIZE_Z;
  57. using Tegra::Texture::GOB_SIZE_Z_SHIFT;
  58. using Tegra::Texture::MsaaMode;
  59. using Tegra::Texture::SwizzleTexture;
  60. using Tegra::Texture::TextureFormat;
  61. using Tegra::Texture::TextureType;
  62. using Tegra::Texture::TICEntry;
  63. using Tegra::Texture::UnswizzleTexture;
  64. using VideoCore::Surface::BytesPerBlock;
  65. using VideoCore::Surface::DefaultBlockHeight;
  66. using VideoCore::Surface::DefaultBlockWidth;
  67. using VideoCore::Surface::IsCopyCompatible;
  68. using VideoCore::Surface::IsPixelFormatASTC;
  69. using VideoCore::Surface::IsViewCompatible;
  70. using VideoCore::Surface::PixelFormatFromDepthFormat;
  71. using VideoCore::Surface::PixelFormatFromRenderTargetFormat;
  72. using VideoCore::Surface::SurfaceType;
  73. constexpr u32 CONVERTED_BYTES_PER_BLOCK = BytesPerBlock(PixelFormat::A8B8G8R8_UNORM);
  74. struct LevelInfo {
  75. Extent3D size;
  76. Extent3D block;
  77. Extent2D tile_size;
  78. u32 bpp_log2;
  79. u32 tile_width_spacing;
  80. };
  81. [[nodiscard]] constexpr u32 AdjustTileSize(u32 shift, u32 unit_factor, u32 dimension) {
  82. if (shift == 0) {
  83. return 0;
  84. }
  85. u32 x = unit_factor << (shift - 1);
  86. if (x >= dimension) {
  87. while (--shift) {
  88. x >>= 1;
  89. if (x < dimension) {
  90. break;
  91. }
  92. }
  93. }
  94. return shift;
  95. }
  96. [[nodiscard]] constexpr u32 AdjustMipSize(u32 size, u32 level) {
  97. return std::max<u32>(size >> level, 1);
  98. }
  99. [[nodiscard]] constexpr Extent3D AdjustMipSize(Extent3D size, s32 level) {
  100. return Extent3D{
  101. .width = AdjustMipSize(size.width, level),
  102. .height = AdjustMipSize(size.height, level),
  103. .depth = AdjustMipSize(size.depth, level),
  104. };
  105. }
  106. [[nodiscard]] Extent3D AdjustSamplesSize(Extent3D size, s32 num_samples) {
  107. const auto [samples_x, samples_y] = SamplesLog2(num_samples);
  108. return Extent3D{
  109. .width = size.width >> samples_x,
  110. .height = size.height >> samples_y,
  111. .depth = size.depth,
  112. };
  113. }
  114. template <u32 GOB_EXTENT>
  115. [[nodiscard]] constexpr u32 AdjustMipBlockSize(u32 num_tiles, u32 block_size, u32 level) {
  116. do {
  117. while (block_size > 0 && num_tiles <= (1U << (block_size - 1)) * GOB_EXTENT) {
  118. --block_size;
  119. }
  120. } while (level--);
  121. return block_size;
  122. }
  123. [[nodiscard]] constexpr Extent3D AdjustMipBlockSize(Extent3D num_tiles, Extent3D block_size,
  124. u32 level) {
  125. return {
  126. .width = AdjustMipBlockSize<GOB_SIZE_X>(num_tiles.width, block_size.width, level),
  127. .height = AdjustMipBlockSize<GOB_SIZE_Y>(num_tiles.height, block_size.height, level),
  128. .depth = AdjustMipBlockSize<GOB_SIZE_Z>(num_tiles.depth, block_size.depth, level),
  129. };
  130. }
  131. [[nodiscard]] constexpr Extent3D AdjustTileSize(Extent3D size, Extent2D tile_size) {
  132. return {
  133. .width = Common::DivCeil(size.width, tile_size.width),
  134. .height = Common::DivCeil(size.height, tile_size.height),
  135. .depth = size.depth,
  136. };
  137. }
  138. [[nodiscard]] constexpr u32 BytesPerBlockLog2(u32 bytes_per_block) {
  139. return std::countl_zero(bytes_per_block) ^ 0x1F;
  140. }
  141. [[nodiscard]] constexpr u32 BytesPerBlockLog2(PixelFormat format) {
  142. return BytesPerBlockLog2(BytesPerBlock(format));
  143. }
  144. [[nodiscard]] constexpr u32 NumBlocks(Extent3D size, Extent2D tile_size) {
  145. const Extent3D num_blocks = AdjustTileSize(size, tile_size);
  146. return num_blocks.width * num_blocks.height * num_blocks.depth;
  147. }
  148. [[nodiscard]] constexpr u32 AdjustSize(u32 size, u32 level, u32 block_size) {
  149. return Common::DivCeil(AdjustMipSize(size, level), block_size);
  150. }
  151. [[nodiscard]] constexpr u32 LayerSize(const TICEntry& config, PixelFormat format) {
  152. return config.Width() * config.Height() * BytesPerBlock(format);
  153. }
  154. [[nodiscard]] constexpr bool HasTwoDimsPerLayer(TextureType type) {
  155. switch (type) {
  156. case TextureType::Texture2D:
  157. case TextureType::Texture2DArray:
  158. case TextureType::Texture2DNoMipmap:
  159. case TextureType::Texture3D:
  160. case TextureType::TextureCubeArray:
  161. case TextureType::TextureCubemap:
  162. return true;
  163. case TextureType::Texture1D:
  164. case TextureType::Texture1DArray:
  165. case TextureType::Texture1DBuffer:
  166. return false;
  167. }
  168. return false;
  169. }
  170. [[nodiscard]] constexpr bool HasTwoDimsPerLayer(ImageType type) {
  171. switch (type) {
  172. case ImageType::e2D:
  173. case ImageType::e3D:
  174. case ImageType::Linear:
  175. return true;
  176. case ImageType::e1D:
  177. case ImageType::Buffer:
  178. return false;
  179. }
  180. UNREACHABLE_MSG("Invalid image type={}", static_cast<int>(type));
  181. }
  182. [[nodiscard]] constexpr std::pair<int, int> Samples(int num_samples) {
  183. switch (num_samples) {
  184. case 1:
  185. return {1, 1};
  186. case 2:
  187. return {2, 1};
  188. case 4:
  189. return {2, 2};
  190. case 8:
  191. return {4, 2};
  192. case 16:
  193. return {4, 4};
  194. }
  195. UNREACHABLE_MSG("Invalid number of samples={}", num_samples);
  196. return {1, 1};
  197. }
  198. [[nodiscard]] constexpr Extent2D DefaultBlockSize(PixelFormat format) {
  199. return {DefaultBlockWidth(format), DefaultBlockHeight(format)};
  200. }
  201. [[nodiscard]] constexpr Extent3D NumLevelBlocks(const LevelInfo& info, u32 level) {
  202. return Extent3D{
  203. .width = AdjustSize(info.size.width, level, info.tile_size.width) << info.bpp_log2,
  204. .height = AdjustSize(info.size.height, level, info.tile_size.height),
  205. .depth = AdjustMipSize(info.size.depth, level),
  206. };
  207. }
  208. [[nodiscard]] constexpr Extent3D TileShift(const LevelInfo& info, u32 level) {
  209. const Extent3D blocks = NumLevelBlocks(info, level);
  210. return Extent3D{
  211. .width = AdjustTileSize(info.block.width, GOB_SIZE_X, blocks.width),
  212. .height = AdjustTileSize(info.block.height, GOB_SIZE_Y, blocks.height),
  213. .depth = AdjustTileSize(info.block.depth, GOB_SIZE_Z, blocks.depth),
  214. };
  215. }
  216. [[nodiscard]] constexpr Extent2D GobSize(u32 bpp_log2, u32 block_height, u32 tile_width_spacing) {
  217. return Extent2D{
  218. .width = GOB_SIZE_X_SHIFT - bpp_log2 + tile_width_spacing,
  219. .height = GOB_SIZE_Y_SHIFT + block_height,
  220. };
  221. }
  222. [[nodiscard]] constexpr bool IsSmallerThanGobSize(Extent3D num_tiles, Extent2D gob,
  223. u32 block_depth) {
  224. return num_tiles.width <= (1U << gob.width) || num_tiles.height <= (1U << gob.height) ||
  225. num_tiles.depth < (1U << block_depth);
  226. }
  227. [[nodiscard]] constexpr u32 StrideAlignment(Extent3D num_tiles, Extent3D block, Extent2D gob,
  228. u32 bpp_log2) {
  229. if (IsSmallerThanGobSize(num_tiles, gob, block.depth)) {
  230. return GOB_SIZE_X_SHIFT - bpp_log2;
  231. } else {
  232. return gob.width;
  233. }
  234. }
  235. [[nodiscard]] constexpr u32 StrideAlignment(Extent3D num_tiles, Extent3D block, u32 bpp_log2,
  236. u32 tile_width_spacing) {
  237. const Extent2D gob = GobSize(bpp_log2, block.height, tile_width_spacing);
  238. return StrideAlignment(num_tiles, block, gob, bpp_log2);
  239. }
  240. [[nodiscard]] constexpr Extent2D NumGobs(const LevelInfo& info, u32 level) {
  241. const Extent3D blocks = NumLevelBlocks(info, level);
  242. const Extent2D gobs{
  243. .width = Common::DivCeilLog2(blocks.width, GOB_SIZE_X_SHIFT),
  244. .height = Common::DivCeilLog2(blocks.height, GOB_SIZE_Y_SHIFT),
  245. };
  246. const Extent2D gob = GobSize(info.bpp_log2, info.block.height, info.tile_width_spacing);
  247. const bool is_small = IsSmallerThanGobSize(blocks, gob, info.block.depth);
  248. const u32 alignment = is_small ? 0 : info.tile_width_spacing;
  249. return Extent2D{
  250. .width = Common::AlignUpLog2(gobs.width, alignment),
  251. .height = gobs.height,
  252. };
  253. }
  254. [[nodiscard]] constexpr Extent3D LevelTiles(const LevelInfo& info, u32 level) {
  255. const Extent3D blocks = NumLevelBlocks(info, level);
  256. const Extent3D tile_shift = TileShift(info, level);
  257. const Extent2D gobs = NumGobs(info, level);
  258. return Extent3D{
  259. .width = Common::DivCeilLog2(gobs.width, tile_shift.width),
  260. .height = Common::DivCeilLog2(gobs.height, tile_shift.height),
  261. .depth = Common::DivCeilLog2(blocks.depth, tile_shift.depth),
  262. };
  263. }
  264. [[nodiscard]] constexpr u32 CalculateLevelSize(const LevelInfo& info, u32 level) {
  265. const Extent3D tile_shift = TileShift(info, level);
  266. const Extent3D tiles = LevelTiles(info, level);
  267. const u32 num_tiles = tiles.width * tiles.height * tiles.depth;
  268. const u32 shift = GOB_SIZE_SHIFT + tile_shift.width + tile_shift.height + tile_shift.depth;
  269. return num_tiles << shift;
  270. }
  271. [[nodiscard]] constexpr std::array<u32, MAX_MIP_LEVELS> CalculateLevelSizes(const LevelInfo& info,
  272. u32 num_levels) {
  273. ASSERT(num_levels <= MAX_MIP_LEVELS);
  274. std::array<u32, MAX_MIP_LEVELS> sizes{};
  275. for (u32 level = 0; level < num_levels; ++level) {
  276. sizes[level] = CalculateLevelSize(info, level);
  277. }
  278. return sizes;
  279. }
  280. [[nodiscard]] constexpr LevelInfo MakeLevelInfo(PixelFormat format, Extent3D size, Extent3D block,
  281. u32 num_samples, u32 tile_width_spacing) {
  282. const auto [samples_x, samples_y] = Samples(num_samples);
  283. const u32 bytes_per_block = BytesPerBlock(format);
  284. return {
  285. .size =
  286. {
  287. .width = size.width * samples_x,
  288. .height = size.height * samples_y,
  289. .depth = size.depth,
  290. },
  291. .block = block,
  292. .tile_size = DefaultBlockSize(format),
  293. .bpp_log2 = BytesPerBlockLog2(bytes_per_block),
  294. .tile_width_spacing = tile_width_spacing,
  295. };
  296. }
  297. [[nodiscard]] constexpr LevelInfo MakeLevelInfo(const ImageInfo& info) {
  298. return MakeLevelInfo(info.format, info.size, info.block, info.num_samples,
  299. info.tile_width_spacing);
  300. }
  301. [[nodiscard]] constexpr u32 CalculateLevelOffset(PixelFormat format, Extent3D size, Extent3D block,
  302. u32 num_samples, u32 tile_width_spacing,
  303. u32 level) {
  304. const LevelInfo info = MakeLevelInfo(format, size, block, num_samples, tile_width_spacing);
  305. u32 offset = 0;
  306. for (u32 current_level = 0; current_level < level; ++current_level) {
  307. offset += CalculateLevelSize(info, current_level);
  308. }
  309. return offset;
  310. }
  311. [[nodiscard]] constexpr u32 AlignLayerSize(u32 size_bytes, Extent3D size, Extent3D block,
  312. u32 tile_size_y, u32 tile_width_spacing) {
  313. // https://github.com/Ryujinx/Ryujinx/blob/1c9aba6de1520aea5480c032e0ff5664ac1bb36f/Ryujinx.Graphics.Texture/SizeCalculator.cs#L134
  314. if (tile_width_spacing > 0) {
  315. const u32 alignment_log2 = GOB_SIZE_SHIFT + tile_width_spacing + block.height + block.depth;
  316. return Common::AlignUpLog2(size_bytes, alignment_log2);
  317. }
  318. const u32 aligned_height = Common::AlignUp(size.height, tile_size_y);
  319. while (block.height != 0 && aligned_height <= (1U << (block.height - 1)) * GOB_SIZE_Y) {
  320. --block.height;
  321. }
  322. while (block.depth != 0 && size.depth <= (1U << (block.depth - 1))) {
  323. --block.depth;
  324. }
  325. const u32 block_shift = GOB_SIZE_SHIFT + block.height + block.depth;
  326. const u32 num_blocks = size_bytes >> block_shift;
  327. if (size_bytes != num_blocks << block_shift) {
  328. return (num_blocks + 1) << block_shift;
  329. }
  330. return size_bytes;
  331. }
  332. [[nodiscard]] std::optional<SubresourceExtent> ResolveOverlapEqualAddress(const ImageInfo& new_info,
  333. const ImageBase& overlap,
  334. bool strict_size) {
  335. const ImageInfo& info = overlap.info;
  336. if (!IsBlockLinearSizeCompatible(new_info, info, 0, 0, strict_size)) {
  337. return std::nullopt;
  338. }
  339. if (new_info.block != info.block) {
  340. return std::nullopt;
  341. }
  342. const SubresourceExtent resources = new_info.resources;
  343. return SubresourceExtent{
  344. .levels = std::max(resources.levels, info.resources.levels),
  345. .layers = std::max(resources.layers, info.resources.layers),
  346. };
  347. }
  348. [[nodiscard]] std::optional<SubresourceExtent> ResolveOverlapRightAddress3D(
  349. const ImageInfo& new_info, GPUVAddr gpu_addr, const ImageBase& overlap, bool strict_size) {
  350. const std::vector<u32> slice_offsets = CalculateSliceOffsets(new_info);
  351. const u32 diff = static_cast<u32>(overlap.gpu_addr - gpu_addr);
  352. const auto it = std::ranges::find(slice_offsets, diff);
  353. if (it == slice_offsets.end()) {
  354. return std::nullopt;
  355. }
  356. const std::vector subresources = CalculateSliceSubresources(new_info);
  357. const SubresourceBase base = subresources[std::distance(slice_offsets.begin(), it)];
  358. const ImageInfo& info = overlap.info;
  359. if (!IsBlockLinearSizeCompatible(new_info, info, base.level, 0, strict_size)) {
  360. return std::nullopt;
  361. }
  362. const u32 mip_depth = std::max(1U, new_info.size.depth << base.level);
  363. if (mip_depth < info.size.depth + base.layer) {
  364. return std::nullopt;
  365. }
  366. if (MipBlockSize(new_info, base.level) != info.block) {
  367. return std::nullopt;
  368. }
  369. return SubresourceExtent{
  370. .levels = std::max(new_info.resources.levels, info.resources.levels + base.level),
  371. .layers = 1,
  372. };
  373. }
  374. [[nodiscard]] std::optional<SubresourceExtent> ResolveOverlapRightAddress2D(
  375. const ImageInfo& new_info, GPUVAddr gpu_addr, const ImageBase& overlap, bool strict_size) {
  376. const u32 layer_stride = new_info.layer_stride;
  377. const s32 new_size = layer_stride * new_info.resources.layers;
  378. const s32 diff = static_cast<s32>(overlap.gpu_addr - gpu_addr);
  379. if (diff > new_size) {
  380. return std::nullopt;
  381. }
  382. const s32 base_layer = diff / layer_stride;
  383. const s32 mip_offset = diff % layer_stride;
  384. const std::array offsets = CalculateMipLevelOffsets(new_info);
  385. const auto end = offsets.begin() + new_info.resources.levels;
  386. const auto it = std::find(offsets.begin(), end, mip_offset);
  387. if (it == end) {
  388. // Mipmap is not aligned to any valid size
  389. return std::nullopt;
  390. }
  391. const SubresourceBase base{
  392. .level = static_cast<s32>(std::distance(offsets.begin(), it)),
  393. .layer = base_layer,
  394. };
  395. const ImageInfo& info = overlap.info;
  396. if (!IsBlockLinearSizeCompatible(new_info, info, base.level, 0, strict_size)) {
  397. return std::nullopt;
  398. }
  399. if (MipBlockSize(new_info, base.level) != info.block) {
  400. return std::nullopt;
  401. }
  402. return SubresourceExtent{
  403. .levels = std::max(new_info.resources.levels, info.resources.levels + base.level),
  404. .layers = std::max(new_info.resources.layers, info.resources.layers + base.layer),
  405. };
  406. }
  407. [[nodiscard]] std::optional<OverlapResult> ResolveOverlapRightAddress(const ImageInfo& new_info,
  408. GPUVAddr gpu_addr,
  409. VAddr cpu_addr,
  410. const ImageBase& overlap,
  411. bool strict_size) {
  412. std::optional<SubresourceExtent> resources;
  413. if (new_info.type != ImageType::e3D) {
  414. resources = ResolveOverlapRightAddress2D(new_info, gpu_addr, overlap, strict_size);
  415. } else {
  416. resources = ResolveOverlapRightAddress3D(new_info, gpu_addr, overlap, strict_size);
  417. }
  418. if (!resources) {
  419. return std::nullopt;
  420. }
  421. return OverlapResult{
  422. .gpu_addr = gpu_addr,
  423. .cpu_addr = cpu_addr,
  424. .resources = *resources,
  425. };
  426. }
  427. [[nodiscard]] std::optional<OverlapResult> ResolveOverlapLeftAddress(const ImageInfo& new_info,
  428. GPUVAddr gpu_addr,
  429. VAddr cpu_addr,
  430. const ImageBase& overlap,
  431. bool strict_size) {
  432. const std::optional<SubresourceBase> base = overlap.TryFindBase(gpu_addr);
  433. if (!base) {
  434. return std::nullopt;
  435. }
  436. const ImageInfo& info = overlap.info;
  437. if (!IsBlockLinearSizeCompatible(new_info, info, base->level, 0, strict_size)) {
  438. return std::nullopt;
  439. }
  440. if (new_info.block != MipBlockSize(info, base->level)) {
  441. return std::nullopt;
  442. }
  443. const SubresourceExtent resources = new_info.resources;
  444. s32 layers = 1;
  445. if (info.type != ImageType::e3D) {
  446. layers = std::max(resources.layers, info.resources.layers + base->layer);
  447. }
  448. return OverlapResult{
  449. .gpu_addr = overlap.gpu_addr,
  450. .cpu_addr = overlap.cpu_addr,
  451. .resources =
  452. {
  453. .levels = std::max(resources.levels + base->level, info.resources.levels),
  454. .layers = layers,
  455. },
  456. };
  457. }
  458. [[nodiscard]] Extent2D PitchLinearAlignedSize(const ImageInfo& info) {
  459. // https://github.com/Ryujinx/Ryujinx/blob/1c9aba6de1520aea5480c032e0ff5664ac1bb36f/Ryujinx.Graphics.Texture/SizeCalculator.cs#L212
  460. static constexpr u32 STRIDE_ALIGNMENT = 32;
  461. ASSERT(info.type == ImageType::Linear);
  462. const Extent2D num_tiles{
  463. .width = Common::DivCeil(info.size.width, DefaultBlockWidth(info.format)),
  464. .height = Common::DivCeil(info.size.height, DefaultBlockHeight(info.format)),
  465. };
  466. const u32 width_alignment = STRIDE_ALIGNMENT / BytesPerBlock(info.format);
  467. return Extent2D{
  468. .width = Common::AlignUp(num_tiles.width, width_alignment),
  469. .height = num_tiles.height,
  470. };
  471. }
  472. [[nodiscard]] Extent3D BlockLinearAlignedSize(const ImageInfo& info, u32 level) {
  473. // https://github.com/Ryujinx/Ryujinx/blob/1c9aba6de1520aea5480c032e0ff5664ac1bb36f/Ryujinx.Graphics.Texture/SizeCalculator.cs#L176
  474. ASSERT(info.type != ImageType::Linear);
  475. const Extent3D size = AdjustMipSize(info.size, level);
  476. const Extent3D num_tiles{
  477. .width = Common::DivCeil(size.width, DefaultBlockWidth(info.format)),
  478. .height = Common::DivCeil(size.height, DefaultBlockHeight(info.format)),
  479. .depth = size.depth,
  480. };
  481. const u32 bpp_log2 = BytesPerBlockLog2(info.format);
  482. const u32 alignment = StrideAlignment(num_tiles, info.block, bpp_log2, info.tile_width_spacing);
  483. const Extent3D mip_block = AdjustMipBlockSize(num_tiles, info.block, 0);
  484. return Extent3D{
  485. .width = Common::AlignUpLog2(num_tiles.width, alignment),
  486. .height = Common::AlignUpLog2(num_tiles.height, GOB_SIZE_Y_SHIFT + mip_block.height),
  487. .depth = Common::AlignUpLog2(num_tiles.depth, GOB_SIZE_Z_SHIFT + mip_block.depth),
  488. };
  489. }
  490. [[nodiscard]] constexpr u32 NumBlocksPerLayer(const ImageInfo& info, Extent2D tile_size) noexcept {
  491. u32 num_blocks = 0;
  492. for (s32 level = 0; level < info.resources.levels; ++level) {
  493. const Extent3D mip_size = AdjustMipSize(info.size, level);
  494. num_blocks += NumBlocks(mip_size, tile_size);
  495. }
  496. return num_blocks;
  497. }
  498. [[nodiscard]] u32 NumSlices(const ImageInfo& info) noexcept {
  499. ASSERT(info.type == ImageType::e3D);
  500. u32 num_slices = 0;
  501. for (s32 level = 0; level < info.resources.levels; ++level) {
  502. num_slices += AdjustMipSize(info.size.depth, level);
  503. }
  504. return num_slices;
  505. }
  506. void SwizzlePitchLinearImage(Tegra::MemoryManager& gpu_memory, GPUVAddr gpu_addr,
  507. const ImageInfo& info, const BufferImageCopy& copy,
  508. std::span<const u8> memory) {
  509. ASSERT(copy.image_offset.z == 0);
  510. ASSERT(copy.image_extent.depth == 1);
  511. ASSERT(copy.image_subresource.base_level == 0);
  512. ASSERT(copy.image_subresource.base_layer == 0);
  513. ASSERT(copy.image_subresource.num_layers == 1);
  514. const u32 bytes_per_block = BytesPerBlock(info.format);
  515. const u32 row_length = copy.image_extent.width * bytes_per_block;
  516. const u32 guest_offset_x = copy.image_offset.x * bytes_per_block;
  517. for (u32 line = 0; line < copy.image_extent.height; ++line) {
  518. const u32 host_offset_y = line * info.pitch;
  519. const u32 guest_offset_y = (copy.image_offset.y + line) * info.pitch;
  520. const u32 guest_offset = guest_offset_x + guest_offset_y;
  521. gpu_memory.WriteBlockUnsafe(gpu_addr + guest_offset, memory.data() + host_offset_y,
  522. row_length);
  523. }
  524. }
  525. void SwizzleBlockLinearImage(Tegra::MemoryManager& gpu_memory, GPUVAddr gpu_addr,
  526. const ImageInfo& info, const BufferImageCopy& copy,
  527. std::span<const u8> input) {
  528. const Extent3D size = info.size;
  529. const LevelInfo level_info = MakeLevelInfo(info);
  530. const Extent2D tile_size = DefaultBlockSize(info.format);
  531. const u32 bytes_per_block = BytesPerBlock(info.format);
  532. const s32 level = copy.image_subresource.base_level;
  533. const Extent3D level_size = AdjustMipSize(size, level);
  534. const u32 num_blocks_per_layer = NumBlocks(level_size, tile_size);
  535. const u32 host_bytes_per_layer = num_blocks_per_layer * bytes_per_block;
  536. UNIMPLEMENTED_IF(info.tile_width_spacing > 0);
  537. UNIMPLEMENTED_IF(copy.image_offset.x != 0);
  538. UNIMPLEMENTED_IF(copy.image_offset.y != 0);
  539. UNIMPLEMENTED_IF(copy.image_offset.z != 0);
  540. UNIMPLEMENTED_IF(copy.image_extent != level_size);
  541. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  542. const Extent3D block = AdjustMipBlockSize(num_tiles, level_info.block, level);
  543. size_t host_offset = copy.buffer_offset;
  544. const u32 num_levels = info.resources.levels;
  545. const std::array sizes = CalculateLevelSizes(level_info, num_levels);
  546. size_t guest_offset = std::reduce(sizes.begin(), sizes.begin() + level, 0);
  547. const size_t layer_stride =
  548. AlignLayerSize(std::reduce(sizes.begin(), sizes.begin() + num_levels, 0), size,
  549. level_info.block, tile_size.height, info.tile_width_spacing);
  550. const size_t subresource_size = sizes[level];
  551. const auto dst_data = std::make_unique<u8[]>(subresource_size);
  552. const std::span<u8> dst(dst_data.get(), subresource_size);
  553. for (s32 layer = 0; layer < info.resources.layers; ++layer) {
  554. const std::span<const u8> src = input.subspan(host_offset);
  555. SwizzleTexture(dst, src, bytes_per_block, num_tiles.width, num_tiles.height,
  556. num_tiles.depth, block.height, block.depth);
  557. gpu_memory.WriteBlockUnsafe(gpu_addr + guest_offset, dst.data(), dst.size_bytes());
  558. host_offset += host_bytes_per_layer;
  559. guest_offset += layer_stride;
  560. }
  561. ASSERT(host_offset - copy.buffer_offset == copy.buffer_size);
  562. }
  563. } // Anonymous namespace
  564. u32 CalculateGuestSizeInBytes(const ImageInfo& info) noexcept {
  565. if (info.type == ImageType::Buffer) {
  566. return info.size.width * BytesPerBlock(info.format);
  567. }
  568. if (info.type == ImageType::Linear) {
  569. return info.pitch * Common::DivCeil(info.size.height, DefaultBlockHeight(info.format));
  570. }
  571. if (info.resources.layers > 1) {
  572. ASSERT(info.layer_stride != 0);
  573. return info.layer_stride * info.resources.layers;
  574. } else {
  575. return CalculateLayerSize(info);
  576. }
  577. }
  578. u32 CalculateUnswizzledSizeBytes(const ImageInfo& info) noexcept {
  579. if (info.type == ImageType::Buffer) {
  580. return info.size.width * BytesPerBlock(info.format);
  581. }
  582. if (info.num_samples > 1) {
  583. // Multisample images can't be uploaded or downloaded to the host
  584. return 0;
  585. }
  586. if (info.type == ImageType::Linear) {
  587. return info.pitch * Common::DivCeil(info.size.height, DefaultBlockHeight(info.format));
  588. }
  589. const Extent2D tile_size = DefaultBlockSize(info.format);
  590. return NumBlocksPerLayer(info, tile_size) * info.resources.layers * BytesPerBlock(info.format);
  591. }
  592. u32 CalculateConvertedSizeBytes(const ImageInfo& info) noexcept {
  593. if (info.type == ImageType::Buffer) {
  594. return info.size.width * BytesPerBlock(info.format);
  595. }
  596. static constexpr Extent2D TILE_SIZE{1, 1};
  597. return NumBlocksPerLayer(info, TILE_SIZE) * info.resources.layers * CONVERTED_BYTES_PER_BLOCK;
  598. }
  599. u32 CalculateLayerStride(const ImageInfo& info) noexcept {
  600. ASSERT(info.type != ImageType::Linear);
  601. const u32 layer_size = CalculateLayerSize(info);
  602. const Extent3D size = info.size;
  603. const Extent3D block = info.block;
  604. const u32 tile_size_y = DefaultBlockHeight(info.format);
  605. return AlignLayerSize(layer_size, size, block, tile_size_y, info.tile_width_spacing);
  606. }
  607. u32 CalculateLayerSize(const ImageInfo& info) noexcept {
  608. ASSERT(info.type != ImageType::Linear);
  609. return CalculateLevelOffset(info.format, info.size, info.block, info.num_samples,
  610. info.tile_width_spacing, info.resources.levels);
  611. }
  612. std::array<u32, MAX_MIP_LEVELS> CalculateMipLevelOffsets(const ImageInfo& info) noexcept {
  613. ASSERT(info.resources.levels <= static_cast<s32>(MAX_MIP_LEVELS));
  614. const LevelInfo level_info = MakeLevelInfo(info);
  615. std::array<u32, MAX_MIP_LEVELS> offsets{};
  616. u32 offset = 0;
  617. for (s32 level = 0; level < info.resources.levels; ++level) {
  618. offsets[level] = offset;
  619. offset += CalculateLevelSize(level_info, level);
  620. }
  621. return offsets;
  622. }
  623. std::vector<u32> CalculateSliceOffsets(const ImageInfo& info) {
  624. ASSERT(info.type == ImageType::e3D);
  625. std::vector<u32> offsets;
  626. offsets.reserve(NumSlices(info));
  627. const LevelInfo level_info = MakeLevelInfo(info);
  628. u32 mip_offset = 0;
  629. for (s32 level = 0; level < info.resources.levels; ++level) {
  630. const Extent3D tile_shift = TileShift(level_info, level);
  631. const Extent3D tiles = LevelTiles(level_info, level);
  632. const u32 gob_size_shift = tile_shift.height + GOB_SIZE_SHIFT;
  633. const u32 slice_size = (tiles.width * tiles.height) << gob_size_shift;
  634. const u32 z_mask = (1U << tile_shift.depth) - 1;
  635. const u32 depth = AdjustMipSize(info.size.depth, level);
  636. for (u32 slice = 0; slice < depth; ++slice) {
  637. const u32 z_low = slice & z_mask;
  638. const u32 z_high = slice & ~z_mask;
  639. offsets.push_back(mip_offset + (z_low << gob_size_shift) + (z_high * slice_size));
  640. }
  641. mip_offset += CalculateLevelSize(level_info, level);
  642. }
  643. return offsets;
  644. }
  645. std::vector<SubresourceBase> CalculateSliceSubresources(const ImageInfo& info) {
  646. ASSERT(info.type == ImageType::e3D);
  647. std::vector<SubresourceBase> subresources;
  648. subresources.reserve(NumSlices(info));
  649. for (s32 level = 0; level < info.resources.levels; ++level) {
  650. const s32 depth = AdjustMipSize(info.size.depth, level);
  651. for (s32 slice = 0; slice < depth; ++slice) {
  652. subresources.emplace_back(SubresourceBase{
  653. .level = level,
  654. .layer = slice,
  655. });
  656. }
  657. }
  658. return subresources;
  659. }
  660. u32 CalculateLevelStrideAlignment(const ImageInfo& info, u32 level) {
  661. const Extent2D tile_size = DefaultBlockSize(info.format);
  662. const Extent3D level_size = AdjustMipSize(info.size, level);
  663. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  664. const Extent3D block = AdjustMipBlockSize(num_tiles, info.block, level);
  665. const u32 bpp_log2 = BytesPerBlockLog2(info.format);
  666. return StrideAlignment(num_tiles, block, bpp_log2, info.tile_width_spacing);
  667. }
  668. PixelFormat PixelFormatFromTIC(const TICEntry& config) noexcept {
  669. return PixelFormatFromTextureInfo(config.format, config.r_type, config.g_type, config.b_type,
  670. config.a_type, config.srgb_conversion);
  671. }
  672. ImageViewType RenderTargetImageViewType(const ImageInfo& info) noexcept {
  673. switch (info.type) {
  674. case ImageType::e2D:
  675. return info.resources.layers > 1 ? ImageViewType::e2DArray : ImageViewType::e2D;
  676. case ImageType::e3D:
  677. return ImageViewType::e2DArray;
  678. case ImageType::Linear:
  679. return ImageViewType::e2D;
  680. default:
  681. UNIMPLEMENTED_MSG("Unimplemented image type={}", static_cast<int>(info.type));
  682. return ImageViewType{};
  683. }
  684. }
  685. std::vector<ImageCopy> MakeShrinkImageCopies(const ImageInfo& dst, const ImageInfo& src,
  686. SubresourceBase base) {
  687. ASSERT(dst.resources.levels >= src.resources.levels);
  688. ASSERT(dst.num_samples == src.num_samples);
  689. const bool is_dst_3d = dst.type == ImageType::e3D;
  690. if (is_dst_3d) {
  691. ASSERT(src.type == ImageType::e3D);
  692. ASSERT(src.resources.levels == 1);
  693. }
  694. std::vector<ImageCopy> copies;
  695. copies.reserve(src.resources.levels);
  696. for (s32 level = 0; level < src.resources.levels; ++level) {
  697. ImageCopy& copy = copies.emplace_back();
  698. copy.src_subresource = SubresourceLayers{
  699. .base_level = level,
  700. .base_layer = 0,
  701. .num_layers = src.resources.layers,
  702. };
  703. copy.dst_subresource = SubresourceLayers{
  704. .base_level = base.level + level,
  705. .base_layer = is_dst_3d ? 0 : base.layer,
  706. .num_layers = is_dst_3d ? 1 : src.resources.layers,
  707. };
  708. copy.src_offset = Offset3D{
  709. .x = 0,
  710. .y = 0,
  711. .z = 0,
  712. };
  713. copy.dst_offset = Offset3D{
  714. .x = 0,
  715. .y = 0,
  716. .z = is_dst_3d ? base.layer : 0,
  717. };
  718. const Extent3D mip_size = AdjustMipSize(dst.size, base.level + level);
  719. copy.extent = AdjustSamplesSize(mip_size, dst.num_samples);
  720. if (is_dst_3d) {
  721. copy.extent.depth = src.size.depth;
  722. }
  723. }
  724. return copies;
  725. }
  726. bool IsValidAddress(const Tegra::MemoryManager& gpu_memory, const TICEntry& config) {
  727. if (config.Address() == 0) {
  728. return false;
  729. }
  730. if (config.Address() > (u64(1) << 48)) {
  731. return false;
  732. }
  733. return gpu_memory.GpuToCpuAddress(config.Address()).has_value();
  734. }
  735. std::vector<BufferImageCopy> UnswizzleImage(Tegra::MemoryManager& gpu_memory, GPUVAddr gpu_addr,
  736. const ImageInfo& info, std::span<u8> output) {
  737. const size_t guest_size_bytes = CalculateGuestSizeInBytes(info);
  738. const u32 bpp_log2 = BytesPerBlockLog2(info.format);
  739. const Extent3D size = info.size;
  740. if (info.type == ImageType::Linear) {
  741. gpu_memory.ReadBlockUnsafe(gpu_addr, output.data(), guest_size_bytes);
  742. ASSERT((info.pitch >> bpp_log2) << bpp_log2 == info.pitch);
  743. return {{
  744. .buffer_offset = 0,
  745. .buffer_size = guest_size_bytes,
  746. .buffer_row_length = info.pitch >> bpp_log2,
  747. .buffer_image_height = size.height,
  748. .image_subresource =
  749. {
  750. .base_level = 0,
  751. .base_layer = 0,
  752. .num_layers = 1,
  753. },
  754. .image_offset = {0, 0, 0},
  755. .image_extent = size,
  756. }};
  757. }
  758. const auto input_data = std::make_unique<u8[]>(guest_size_bytes);
  759. gpu_memory.ReadBlockUnsafe(gpu_addr, input_data.get(), guest_size_bytes);
  760. const std::span<const u8> input(input_data.get(), guest_size_bytes);
  761. const LevelInfo level_info = MakeLevelInfo(info);
  762. const s32 num_layers = info.resources.layers;
  763. const s32 num_levels = info.resources.levels;
  764. const Extent2D tile_size = DefaultBlockSize(info.format);
  765. const std::array level_sizes = CalculateLevelSizes(level_info, num_levels);
  766. const Extent2D gob = GobSize(bpp_log2, info.block.height, info.tile_width_spacing);
  767. const u32 layer_size = std::reduce(level_sizes.begin(), level_sizes.begin() + num_levels, 0);
  768. const u32 layer_stride = AlignLayerSize(layer_size, size, level_info.block, tile_size.height,
  769. info.tile_width_spacing);
  770. size_t guest_offset = 0;
  771. u32 host_offset = 0;
  772. std::vector<BufferImageCopy> copies(num_levels);
  773. for (s32 level = 0; level < num_levels; ++level) {
  774. const Extent3D level_size = AdjustMipSize(size, level);
  775. const u32 num_blocks_per_layer = NumBlocks(level_size, tile_size);
  776. const u32 host_bytes_per_layer = num_blocks_per_layer << bpp_log2;
  777. copies[level] = BufferImageCopy{
  778. .buffer_offset = host_offset,
  779. .buffer_size = static_cast<size_t>(host_bytes_per_layer) * num_layers,
  780. .buffer_row_length = Common::AlignUp(level_size.width, tile_size.width),
  781. .buffer_image_height = Common::AlignUp(level_size.height, tile_size.height),
  782. .image_subresource =
  783. {
  784. .base_level = level,
  785. .base_layer = 0,
  786. .num_layers = info.resources.layers,
  787. },
  788. .image_offset = {0, 0, 0},
  789. .image_extent = level_size,
  790. };
  791. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  792. const Extent3D block = AdjustMipBlockSize(num_tiles, level_info.block, level);
  793. const u32 stride_alignment = StrideAlignment(num_tiles, info.block, gob, bpp_log2);
  794. size_t guest_layer_offset = 0;
  795. for (s32 layer = 0; layer < info.resources.layers; ++layer) {
  796. const std::span<u8> dst = output.subspan(host_offset);
  797. const std::span<const u8> src = input.subspan(guest_offset + guest_layer_offset);
  798. UnswizzleTexture(dst, src, 1U << bpp_log2, num_tiles.width, num_tiles.height,
  799. num_tiles.depth, block.height, block.depth, stride_alignment);
  800. guest_layer_offset += layer_stride;
  801. host_offset += host_bytes_per_layer;
  802. }
  803. guest_offset += level_sizes[level];
  804. }
  805. return copies;
  806. }
  807. BufferCopy UploadBufferCopy(Tegra::MemoryManager& gpu_memory, GPUVAddr gpu_addr,
  808. const ImageBase& image, std::span<u8> output) {
  809. gpu_memory.ReadBlockUnsafe(gpu_addr, output.data(), image.guest_size_bytes);
  810. return BufferCopy{
  811. .src_offset = 0,
  812. .dst_offset = 0,
  813. .size = image.guest_size_bytes,
  814. };
  815. }
  816. void ConvertImage(std::span<const u8> input, const ImageInfo& info, std::span<u8> output,
  817. std::span<BufferImageCopy> copies) {
  818. u32 output_offset = 0;
  819. const Extent2D tile_size = DefaultBlockSize(info.format);
  820. for (BufferImageCopy& copy : copies) {
  821. const u32 level = copy.image_subresource.base_level;
  822. const Extent3D mip_size = AdjustMipSize(info.size, level);
  823. ASSERT(copy.image_offset == Offset3D{});
  824. ASSERT(copy.image_subresource.base_layer == 0);
  825. ASSERT(copy.image_extent == mip_size);
  826. ASSERT(copy.buffer_row_length == Common::AlignUp(mip_size.width, tile_size.width));
  827. ASSERT(copy.buffer_image_height == Common::AlignUp(mip_size.height, tile_size.height));
  828. if (IsPixelFormatASTC(info.format)) {
  829. ASSERT(copy.image_extent.depth == 1);
  830. Tegra::Texture::ASTC::Decompress(input.subspan(copy.buffer_offset),
  831. copy.image_extent.width, copy.image_extent.height,
  832. copy.image_subresource.num_layers, tile_size.width,
  833. tile_size.height, output.subspan(output_offset));
  834. } else {
  835. DecompressBC4(input.subspan(copy.buffer_offset), copy.image_extent,
  836. output.subspan(output_offset));
  837. }
  838. copy.buffer_offset = output_offset;
  839. copy.buffer_row_length = mip_size.width;
  840. copy.buffer_image_height = mip_size.height;
  841. output_offset += copy.image_extent.width * copy.image_extent.height *
  842. copy.image_subresource.num_layers * CONVERTED_BYTES_PER_BLOCK;
  843. }
  844. }
  845. std::vector<BufferImageCopy> FullDownloadCopies(const ImageInfo& info) {
  846. const Extent3D size = info.size;
  847. const u32 bytes_per_block = BytesPerBlock(info.format);
  848. if (info.type == ImageType::Linear) {
  849. ASSERT(info.pitch % bytes_per_block == 0);
  850. return {{
  851. .buffer_offset = 0,
  852. .buffer_size = static_cast<size_t>(info.pitch) * size.height,
  853. .buffer_row_length = info.pitch / bytes_per_block,
  854. .buffer_image_height = size.height,
  855. .image_subresource =
  856. {
  857. .base_level = 0,
  858. .base_layer = 0,
  859. .num_layers = 1,
  860. },
  861. .image_offset = {0, 0, 0},
  862. .image_extent = size,
  863. }};
  864. }
  865. UNIMPLEMENTED_IF(info.tile_width_spacing > 0);
  866. const s32 num_layers = info.resources.layers;
  867. const s32 num_levels = info.resources.levels;
  868. const Extent2D tile_size = DefaultBlockSize(info.format);
  869. u32 host_offset = 0;
  870. std::vector<BufferImageCopy> copies(num_levels);
  871. for (s32 level = 0; level < num_levels; ++level) {
  872. const Extent3D level_size = AdjustMipSize(size, level);
  873. const u32 num_blocks_per_layer = NumBlocks(level_size, tile_size);
  874. const u32 host_bytes_per_level = num_blocks_per_layer * bytes_per_block * num_layers;
  875. copies[level] = BufferImageCopy{
  876. .buffer_offset = host_offset,
  877. .buffer_size = host_bytes_per_level,
  878. .buffer_row_length = level_size.width,
  879. .buffer_image_height = level_size.height,
  880. .image_subresource =
  881. {
  882. .base_level = level,
  883. .base_layer = 0,
  884. .num_layers = info.resources.layers,
  885. },
  886. .image_offset = {0, 0, 0},
  887. .image_extent = level_size,
  888. };
  889. host_offset += host_bytes_per_level;
  890. }
  891. return copies;
  892. }
  893. Extent3D MipSize(Extent3D size, u32 level) {
  894. return AdjustMipSize(size, level);
  895. }
  896. Extent3D MipBlockSize(const ImageInfo& info, u32 level) {
  897. const LevelInfo level_info = MakeLevelInfo(info);
  898. const Extent2D tile_size = DefaultBlockSize(info.format);
  899. const Extent3D level_size = AdjustMipSize(info.size, level);
  900. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  901. return AdjustMipBlockSize(num_tiles, level_info.block, level);
  902. }
  903. std::vector<SwizzleParameters> FullUploadSwizzles(const ImageInfo& info) {
  904. const Extent2D tile_size = DefaultBlockSize(info.format);
  905. if (info.type == ImageType::Linear) {
  906. return std::vector{SwizzleParameters{
  907. .num_tiles = AdjustTileSize(info.size, tile_size),
  908. .block = {},
  909. .buffer_offset = 0,
  910. .level = 0,
  911. }};
  912. }
  913. const LevelInfo level_info = MakeLevelInfo(info);
  914. const Extent3D size = info.size;
  915. const s32 num_levels = info.resources.levels;
  916. u32 guest_offset = 0;
  917. std::vector<SwizzleParameters> params(num_levels);
  918. for (s32 level = 0; level < num_levels; ++level) {
  919. const Extent3D level_size = AdjustMipSize(size, level);
  920. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  921. const Extent3D block = AdjustMipBlockSize(num_tiles, level_info.block, level);
  922. params[level] = SwizzleParameters{
  923. .num_tiles = num_tiles,
  924. .block = block,
  925. .buffer_offset = guest_offset,
  926. .level = level,
  927. };
  928. guest_offset += CalculateLevelSize(level_info, level);
  929. }
  930. return params;
  931. }
  932. void SwizzleImage(Tegra::MemoryManager& gpu_memory, GPUVAddr gpu_addr, const ImageInfo& info,
  933. std::span<const BufferImageCopy> copies, std::span<const u8> memory) {
  934. const bool is_pitch_linear = info.type == ImageType::Linear;
  935. for (const BufferImageCopy& copy : copies) {
  936. if (is_pitch_linear) {
  937. SwizzlePitchLinearImage(gpu_memory, gpu_addr, info, copy, memory);
  938. } else {
  939. SwizzleBlockLinearImage(gpu_memory, gpu_addr, info, copy, memory);
  940. }
  941. }
  942. }
  943. bool IsBlockLinearSizeCompatible(const ImageInfo& lhs, const ImageInfo& rhs, u32 lhs_level,
  944. u32 rhs_level, bool strict_size) noexcept {
  945. ASSERT(lhs.type != ImageType::Linear);
  946. ASSERT(rhs.type != ImageType::Linear);
  947. if (strict_size) {
  948. const Extent3D lhs_size = AdjustMipSize(lhs.size, lhs_level);
  949. const Extent3D rhs_size = AdjustMipSize(rhs.size, rhs_level);
  950. return lhs_size.width == rhs_size.width && lhs_size.height == rhs_size.height;
  951. } else {
  952. const Extent3D lhs_size = BlockLinearAlignedSize(lhs, lhs_level);
  953. const Extent3D rhs_size = BlockLinearAlignedSize(rhs, rhs_level);
  954. return lhs_size.width == rhs_size.width && lhs_size.height == rhs_size.height;
  955. }
  956. }
  957. bool IsPitchLinearSameSize(const ImageInfo& lhs, const ImageInfo& rhs, bool strict_size) noexcept {
  958. ASSERT(lhs.type == ImageType::Linear);
  959. ASSERT(rhs.type == ImageType::Linear);
  960. if (strict_size) {
  961. return lhs.size.width == rhs.size.width && lhs.size.height == rhs.size.height;
  962. } else {
  963. const Extent2D lhs_size = PitchLinearAlignedSize(lhs);
  964. const Extent2D rhs_size = PitchLinearAlignedSize(rhs);
  965. return lhs_size == rhs_size;
  966. }
  967. }
  968. std::optional<OverlapResult> ResolveOverlap(const ImageInfo& new_info, GPUVAddr gpu_addr,
  969. VAddr cpu_addr, const ImageBase& overlap,
  970. bool strict_size, bool broken_views) {
  971. ASSERT(new_info.type != ImageType::Linear);
  972. ASSERT(overlap.info.type != ImageType::Linear);
  973. if (!IsLayerStrideCompatible(new_info, overlap.info)) {
  974. return std::nullopt;
  975. }
  976. if (!IsViewCompatible(overlap.info.format, new_info.format, broken_views)) {
  977. return std::nullopt;
  978. }
  979. if (gpu_addr == overlap.gpu_addr) {
  980. const std::optional solution = ResolveOverlapEqualAddress(new_info, overlap, strict_size);
  981. if (!solution) {
  982. return std::nullopt;
  983. }
  984. return OverlapResult{
  985. .gpu_addr = gpu_addr,
  986. .cpu_addr = cpu_addr,
  987. .resources = *solution,
  988. };
  989. }
  990. if (overlap.gpu_addr > gpu_addr) {
  991. return ResolveOverlapRightAddress(new_info, gpu_addr, cpu_addr, overlap, strict_size);
  992. }
  993. // if overlap.gpu_addr < gpu_addr
  994. return ResolveOverlapLeftAddress(new_info, gpu_addr, cpu_addr, overlap, strict_size);
  995. }
  996. bool IsLayerStrideCompatible(const ImageInfo& lhs, const ImageInfo& rhs) {
  997. // If either of the layer strides is zero, we can assume they are compatible
  998. // These images generally come from rendertargets
  999. if (lhs.layer_stride == 0) {
  1000. return true;
  1001. }
  1002. if (rhs.layer_stride == 0) {
  1003. return true;
  1004. }
  1005. // It's definitely compatible if the layer stride matches
  1006. if (lhs.layer_stride == rhs.layer_stride) {
  1007. return true;
  1008. }
  1009. // Although we also have to compare for cases where it can be unaligned
  1010. // This can happen if the image doesn't have layers, so the stride is not aligned
  1011. if (lhs.maybe_unaligned_layer_stride == rhs.maybe_unaligned_layer_stride) {
  1012. return true;
  1013. }
  1014. return false;
  1015. }
  1016. std::optional<SubresourceBase> FindSubresource(const ImageInfo& candidate, const ImageBase& image,
  1017. GPUVAddr candidate_addr, RelaxedOptions options,
  1018. bool broken_views) {
  1019. const std::optional<SubresourceBase> base = image.TryFindBase(candidate_addr);
  1020. if (!base) {
  1021. return std::nullopt;
  1022. }
  1023. const ImageInfo& existing = image.info;
  1024. if (False(options & RelaxedOptions::Format)) {
  1025. if (!IsViewCompatible(existing.format, candidate.format, broken_views)) {
  1026. return std::nullopt;
  1027. }
  1028. }
  1029. if (!IsLayerStrideCompatible(existing, candidate)) {
  1030. return std::nullopt;
  1031. }
  1032. if (existing.type != candidate.type) {
  1033. return std::nullopt;
  1034. }
  1035. if (False(options & RelaxedOptions::Samples)) {
  1036. if (existing.num_samples != candidate.num_samples) {
  1037. return std::nullopt;
  1038. }
  1039. }
  1040. if (existing.resources.levels < candidate.resources.levels + base->level) {
  1041. return std::nullopt;
  1042. }
  1043. if (existing.type == ImageType::e3D) {
  1044. const u32 mip_depth = std::max(1U, existing.size.depth << base->level);
  1045. if (mip_depth < candidate.size.depth + base->layer) {
  1046. return std::nullopt;
  1047. }
  1048. } else {
  1049. if (existing.resources.layers < candidate.resources.layers + base->layer) {
  1050. return std::nullopt;
  1051. }
  1052. }
  1053. const bool strict_size = False(options & RelaxedOptions::Size);
  1054. if (!IsBlockLinearSizeCompatible(existing, candidate, base->level, 0, strict_size)) {
  1055. return std::nullopt;
  1056. }
  1057. // TODO: compare block sizes
  1058. return base;
  1059. }
  1060. bool IsSubresource(const ImageInfo& candidate, const ImageBase& image, GPUVAddr candidate_addr,
  1061. RelaxedOptions options, bool broken_views) {
  1062. return FindSubresource(candidate, image, candidate_addr, options, broken_views).has_value();
  1063. }
  1064. void DeduceBlitImages(ImageInfo& dst_info, ImageInfo& src_info, const ImageBase* dst,
  1065. const ImageBase* src) {
  1066. if (src && GetFormatType(src->info.format) != SurfaceType::ColorTexture) {
  1067. src_info.format = src->info.format;
  1068. }
  1069. if (dst && GetFormatType(dst->info.format) != SurfaceType::ColorTexture) {
  1070. dst_info.format = dst->info.format;
  1071. }
  1072. if (!dst && src && GetFormatType(src->info.format) != SurfaceType::ColorTexture) {
  1073. dst_info.format = src->info.format;
  1074. }
  1075. if (!src && dst && GetFormatType(dst->info.format) != SurfaceType::ColorTexture) {
  1076. src_info.format = src->info.format;
  1077. }
  1078. }
  1079. u32 MapSizeBytes(const ImageBase& image) {
  1080. if (True(image.flags & ImageFlagBits::AcceleratedUpload)) {
  1081. return image.guest_size_bytes;
  1082. } else if (True(image.flags & ImageFlagBits::Converted)) {
  1083. return image.converted_size_bytes;
  1084. } else {
  1085. return image.unswizzled_size_bytes;
  1086. }
  1087. }
  1088. static_assert(CalculateLevelSize(LevelInfo{{1920, 1080, 1}, {0, 2, 0}, {1, 1}, 2, 0}, 0) ==
  1089. 0x7f8000);
  1090. static_assert(CalculateLevelSize(LevelInfo{{32, 32, 1}, {0, 0, 4}, {1, 1}, 4, 0}, 0) == 0x4000);
  1091. static_assert(CalculateLevelOffset(PixelFormat::R8_SINT, {1920, 1080, 1}, {0, 2, 0}, 1, 0, 7) ==
  1092. 0x2afc00);
  1093. static_assert(CalculateLevelOffset(PixelFormat::ASTC_2D_12X12_UNORM, {8192, 4096, 1}, {0, 2, 0}, 1,
  1094. 0, 12) == 0x50d200);
  1095. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 1, 0,
  1096. 0) == 0);
  1097. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 1, 0,
  1098. 1) == 0x400000);
  1099. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 1, 0,
  1100. 2) == 0x500000);
  1101. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 1, 0,
  1102. 3) == 0x540000);
  1103. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 1, 0,
  1104. 4) == 0x550000);
  1105. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 1, 0,
  1106. 5) == 0x554000);
  1107. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 1, 0,
  1108. 6) == 0x555000);
  1109. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 1, 0,
  1110. 7) == 0x555400);
  1111. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 1, 0,
  1112. 8) == 0x555600);
  1113. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 1, 0,
  1114. 9) == 0x555800);
  1115. constexpr u32 ValidateLayerSize(PixelFormat format, u32 width, u32 height, u32 block_height,
  1116. u32 tile_width_spacing, u32 level) {
  1117. const Extent3D size{width, height, 1};
  1118. const Extent3D block{0, block_height, 0};
  1119. const u32 offset = CalculateLevelOffset(format, size, block, 1, tile_width_spacing, level);
  1120. return AlignLayerSize(offset, size, block, DefaultBlockHeight(format), tile_width_spacing);
  1121. }
  1122. static_assert(ValidateLayerSize(PixelFormat::ASTC_2D_12X12_UNORM, 8192, 4096, 2, 0, 12) ==
  1123. 0x50d800);
  1124. static_assert(ValidateLayerSize(PixelFormat::A8B8G8R8_UNORM, 1024, 1024, 2, 0, 10) == 0x556000);
  1125. static_assert(ValidateLayerSize(PixelFormat::BC3_UNORM, 128, 128, 2, 0, 8) == 0x6000);
  1126. static_assert(ValidateLayerSize(PixelFormat::A8B8G8R8_UNORM, 518, 572, 4, 3, 1) == 0x190000,
  1127. "Tile width spacing is not working");
  1128. static_assert(ValidateLayerSize(PixelFormat::BC5_UNORM, 1024, 1024, 3, 4, 11) == 0x160000,
  1129. "Compressed tile width spacing is not working");
  1130. } // namespace VideoCommon