util.cpp 52 KB

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