util.cpp 56 KB

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