util.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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. bool IsValidEntry(const Tegra::MemoryManager& gpu_memory, const TICEntry& config) {
  672. const GPUVAddr address = config.Address();
  673. if (address == 0) {
  674. return false;
  675. }
  676. if (address >= (1ULL << 40)) {
  677. return false;
  678. }
  679. if (gpu_memory.GpuToCpuAddress(address).has_value()) {
  680. return true;
  681. }
  682. const ImageInfo info{config};
  683. const size_t guest_size_bytes = CalculateGuestSizeInBytes(info);
  684. return gpu_memory.GpuToCpuAddress(address, guest_size_bytes).has_value();
  685. }
  686. std::vector<BufferImageCopy> UnswizzleImage(Tegra::MemoryManager& gpu_memory, GPUVAddr gpu_addr,
  687. const ImageInfo& info, std::span<const u8> input,
  688. std::span<u8> output) {
  689. const size_t guest_size_bytes = input.size_bytes();
  690. const u32 bpp_log2 = BytesPerBlockLog2(info.format);
  691. const Extent3D size = info.size;
  692. if (info.type == ImageType::Linear) {
  693. gpu_memory.ReadBlockUnsafe(gpu_addr, output.data(), guest_size_bytes);
  694. ASSERT((info.pitch >> bpp_log2) << bpp_log2 == info.pitch);
  695. return {{
  696. .buffer_offset = 0,
  697. .buffer_size = guest_size_bytes,
  698. .buffer_row_length = info.pitch >> bpp_log2,
  699. .buffer_image_height = size.height,
  700. .image_subresource =
  701. {
  702. .base_level = 0,
  703. .base_layer = 0,
  704. .num_layers = 1,
  705. },
  706. .image_offset = {0, 0, 0},
  707. .image_extent = size,
  708. }};
  709. }
  710. const LevelInfo level_info = MakeLevelInfo(info);
  711. const s32 num_layers = info.resources.layers;
  712. const s32 num_levels = info.resources.levels;
  713. const Extent2D tile_size = DefaultBlockSize(info.format);
  714. const std::array level_sizes = CalculateLevelSizes(level_info, num_levels);
  715. const Extent2D gob = GobSize(bpp_log2, info.block.height, info.tile_width_spacing);
  716. const u32 layer_size = CalculateLevelBytes(level_sizes, num_levels);
  717. const u32 layer_stride = AlignLayerSize(layer_size, size, level_info.block, tile_size.height,
  718. info.tile_width_spacing);
  719. size_t guest_offset = 0;
  720. u32 host_offset = 0;
  721. std::vector<BufferImageCopy> copies(num_levels);
  722. for (s32 level = 0; level < num_levels; ++level) {
  723. const Extent3D level_size = AdjustMipSize(size, level);
  724. const u32 num_blocks_per_layer = NumBlocks(level_size, tile_size);
  725. const u32 host_bytes_per_layer = num_blocks_per_layer << bpp_log2;
  726. copies[level] = BufferImageCopy{
  727. .buffer_offset = host_offset,
  728. .buffer_size = static_cast<size_t>(host_bytes_per_layer) * num_layers,
  729. .buffer_row_length = Common::AlignUp(level_size.width, tile_size.width),
  730. .buffer_image_height = Common::AlignUp(level_size.height, tile_size.height),
  731. .image_subresource =
  732. {
  733. .base_level = level,
  734. .base_layer = 0,
  735. .num_layers = info.resources.layers,
  736. },
  737. .image_offset = {0, 0, 0},
  738. .image_extent = level_size,
  739. };
  740. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  741. const Extent3D block = AdjustMipBlockSize(num_tiles, level_info.block, level);
  742. const u32 stride_alignment = StrideAlignment(num_tiles, info.block, gob, bpp_log2);
  743. size_t guest_layer_offset = 0;
  744. for (s32 layer = 0; layer < info.resources.layers; ++layer) {
  745. const std::span<u8> dst = output.subspan(host_offset);
  746. const std::span<const u8> src = input.subspan(guest_offset + guest_layer_offset);
  747. UnswizzleTexture(dst, src, 1U << bpp_log2, num_tiles.width, num_tiles.height,
  748. num_tiles.depth, block.height, block.depth, stride_alignment);
  749. guest_layer_offset += layer_stride;
  750. host_offset += host_bytes_per_layer;
  751. }
  752. guest_offset += level_sizes[level];
  753. }
  754. return copies;
  755. }
  756. BufferCopy UploadBufferCopy(Tegra::MemoryManager& gpu_memory, GPUVAddr gpu_addr,
  757. const ImageBase& image, std::span<u8> output) {
  758. gpu_memory.ReadBlockUnsafe(gpu_addr, output.data(), image.guest_size_bytes);
  759. return BufferCopy{
  760. .src_offset = 0,
  761. .dst_offset = 0,
  762. .size = image.guest_size_bytes,
  763. };
  764. }
  765. void ConvertImage(std::span<const u8> input, const ImageInfo& info, std::span<u8> output,
  766. std::span<BufferImageCopy> copies) {
  767. u32 output_offset = 0;
  768. const Extent2D tile_size = DefaultBlockSize(info.format);
  769. for (BufferImageCopy& copy : copies) {
  770. const u32 level = copy.image_subresource.base_level;
  771. const Extent3D mip_size = AdjustMipSize(info.size, level);
  772. ASSERT(copy.image_offset == Offset3D{});
  773. ASSERT(copy.image_subresource.base_layer == 0);
  774. ASSERT(copy.image_extent == mip_size);
  775. ASSERT(copy.buffer_row_length == Common::AlignUp(mip_size.width, tile_size.width));
  776. ASSERT(copy.buffer_image_height == Common::AlignUp(mip_size.height, tile_size.height));
  777. if (IsPixelFormatASTC(info.format)) {
  778. ASSERT(copy.image_extent.depth == 1);
  779. Tegra::Texture::ASTC::Decompress(input.subspan(copy.buffer_offset),
  780. copy.image_extent.width, copy.image_extent.height,
  781. copy.image_subresource.num_layers, tile_size.width,
  782. tile_size.height, output.subspan(output_offset));
  783. } else {
  784. DecompressBC4(input.subspan(copy.buffer_offset), copy.image_extent,
  785. output.subspan(output_offset));
  786. }
  787. copy.buffer_offset = output_offset;
  788. copy.buffer_row_length = mip_size.width;
  789. copy.buffer_image_height = mip_size.height;
  790. output_offset += copy.image_extent.width * copy.image_extent.height *
  791. copy.image_subresource.num_layers * CONVERTED_BYTES_PER_BLOCK;
  792. }
  793. }
  794. std::vector<BufferImageCopy> FullDownloadCopies(const ImageInfo& info) {
  795. const Extent3D size = info.size;
  796. const u32 bytes_per_block = BytesPerBlock(info.format);
  797. if (info.type == ImageType::Linear) {
  798. ASSERT(info.pitch % bytes_per_block == 0);
  799. return {{
  800. .buffer_offset = 0,
  801. .buffer_size = static_cast<size_t>(info.pitch) * size.height,
  802. .buffer_row_length = info.pitch / bytes_per_block,
  803. .buffer_image_height = size.height,
  804. .image_subresource =
  805. {
  806. .base_level = 0,
  807. .base_layer = 0,
  808. .num_layers = 1,
  809. },
  810. .image_offset = {0, 0, 0},
  811. .image_extent = size,
  812. }};
  813. }
  814. UNIMPLEMENTED_IF(info.tile_width_spacing > 0);
  815. const s32 num_layers = info.resources.layers;
  816. const s32 num_levels = info.resources.levels;
  817. const Extent2D tile_size = DefaultBlockSize(info.format);
  818. u32 host_offset = 0;
  819. std::vector<BufferImageCopy> copies(num_levels);
  820. for (s32 level = 0; level < num_levels; ++level) {
  821. const Extent3D level_size = AdjustMipSize(size, level);
  822. const u32 num_blocks_per_layer = NumBlocks(level_size, tile_size);
  823. const u32 host_bytes_per_level = num_blocks_per_layer * bytes_per_block * num_layers;
  824. copies[level] = BufferImageCopy{
  825. .buffer_offset = host_offset,
  826. .buffer_size = host_bytes_per_level,
  827. .buffer_row_length = level_size.width,
  828. .buffer_image_height = level_size.height,
  829. .image_subresource =
  830. {
  831. .base_level = level,
  832. .base_layer = 0,
  833. .num_layers = info.resources.layers,
  834. },
  835. .image_offset = {0, 0, 0},
  836. .image_extent = level_size,
  837. };
  838. host_offset += host_bytes_per_level;
  839. }
  840. return copies;
  841. }
  842. Extent3D MipSize(Extent3D size, u32 level) {
  843. return AdjustMipSize(size, level);
  844. }
  845. Extent3D MipBlockSize(const ImageInfo& info, u32 level) {
  846. const LevelInfo level_info = MakeLevelInfo(info);
  847. const Extent2D tile_size = DefaultBlockSize(info.format);
  848. const Extent3D level_size = AdjustMipSize(info.size, level);
  849. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  850. return AdjustMipBlockSize(num_tiles, level_info.block, level);
  851. }
  852. std::vector<SwizzleParameters> FullUploadSwizzles(const ImageInfo& info) {
  853. const Extent2D tile_size = DefaultBlockSize(info.format);
  854. if (info.type == ImageType::Linear) {
  855. return std::vector{SwizzleParameters{
  856. .num_tiles = AdjustTileSize(info.size, tile_size),
  857. .block = {},
  858. .buffer_offset = 0,
  859. .level = 0,
  860. }};
  861. }
  862. const LevelInfo level_info = MakeLevelInfo(info);
  863. const Extent3D size = info.size;
  864. const s32 num_levels = info.resources.levels;
  865. u32 guest_offset = 0;
  866. std::vector<SwizzleParameters> params(num_levels);
  867. for (s32 level = 0; level < num_levels; ++level) {
  868. const Extent3D level_size = AdjustMipSize(size, level);
  869. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  870. const Extent3D block = AdjustMipBlockSize(num_tiles, level_info.block, level);
  871. params[level] = SwizzleParameters{
  872. .num_tiles = num_tiles,
  873. .block = block,
  874. .buffer_offset = guest_offset,
  875. .level = level,
  876. };
  877. guest_offset += CalculateLevelSize(level_info, level);
  878. }
  879. return params;
  880. }
  881. void SwizzleImage(Tegra::MemoryManager& gpu_memory, GPUVAddr gpu_addr, const ImageInfo& info,
  882. std::span<const BufferImageCopy> copies, std::span<const u8> memory,
  883. Common::ScratchBuffer<u8>& tmp_buffer) {
  884. const bool is_pitch_linear = info.type == ImageType::Linear;
  885. for (const BufferImageCopy& copy : copies) {
  886. if (is_pitch_linear) {
  887. SwizzlePitchLinearImage(gpu_memory, gpu_addr, info, copy, memory);
  888. } else {
  889. SwizzleBlockLinearImage(gpu_memory, gpu_addr, info, copy, memory, tmp_buffer);
  890. }
  891. }
  892. }
  893. bool IsBlockLinearSizeCompatible(const ImageInfo& lhs, const ImageInfo& rhs, u32 lhs_level,
  894. u32 rhs_level, bool strict_size) noexcept {
  895. ASSERT(lhs.type != ImageType::Linear);
  896. ASSERT(rhs.type != ImageType::Linear);
  897. if (strict_size) {
  898. const Extent3D lhs_size = AdjustMipSize(lhs.size, lhs_level);
  899. const Extent3D rhs_size = AdjustMipSize(rhs.size, rhs_level);
  900. return lhs_size.width == rhs_size.width && lhs_size.height == rhs_size.height;
  901. } else {
  902. const Extent3D lhs_size = BlockLinearAlignedSize(lhs, lhs_level);
  903. const Extent3D rhs_size = BlockLinearAlignedSize(rhs, rhs_level);
  904. return lhs_size.width == rhs_size.width && lhs_size.height == rhs_size.height;
  905. }
  906. }
  907. bool IsPitchLinearSameSize(const ImageInfo& lhs, const ImageInfo& rhs, bool strict_size) noexcept {
  908. ASSERT(lhs.type == ImageType::Linear);
  909. ASSERT(rhs.type == ImageType::Linear);
  910. if (strict_size) {
  911. return lhs.size.width == rhs.size.width && lhs.size.height == rhs.size.height;
  912. } else {
  913. const Extent2D lhs_size = PitchLinearAlignedSize(lhs);
  914. const Extent2D rhs_size = PitchLinearAlignedSize(rhs);
  915. return lhs_size == rhs_size;
  916. }
  917. }
  918. std::optional<OverlapResult> ResolveOverlap(const ImageInfo& new_info, GPUVAddr gpu_addr,
  919. VAddr cpu_addr, const ImageBase& overlap,
  920. bool strict_size, bool broken_views, bool native_bgr) {
  921. ASSERT(new_info.type != ImageType::Linear);
  922. ASSERT(overlap.info.type != ImageType::Linear);
  923. if (!IsLayerStrideCompatible(new_info, overlap.info)) {
  924. return std::nullopt;
  925. }
  926. if (!IsViewCompatible(overlap.info.format, new_info.format, broken_views, native_bgr)) {
  927. return std::nullopt;
  928. }
  929. if (gpu_addr == overlap.gpu_addr) {
  930. const std::optional solution = ResolveOverlapEqualAddress(new_info, overlap, strict_size);
  931. if (!solution) {
  932. return std::nullopt;
  933. }
  934. return OverlapResult{
  935. .gpu_addr = gpu_addr,
  936. .cpu_addr = cpu_addr,
  937. .resources = *solution,
  938. };
  939. }
  940. if (overlap.gpu_addr > gpu_addr) {
  941. return ResolveOverlapRightAddress(new_info, gpu_addr, cpu_addr, overlap, strict_size);
  942. }
  943. // if overlap.gpu_addr < gpu_addr
  944. return ResolveOverlapLeftAddress(new_info, gpu_addr, cpu_addr, overlap, strict_size);
  945. }
  946. bool IsLayerStrideCompatible(const ImageInfo& lhs, const ImageInfo& rhs) {
  947. // If either of the layer strides is zero, we can assume they are compatible
  948. // These images generally come from rendertargets
  949. if (lhs.layer_stride == 0) {
  950. return true;
  951. }
  952. if (rhs.layer_stride == 0) {
  953. return true;
  954. }
  955. // It's definitely compatible if the layer stride matches
  956. if (lhs.layer_stride == rhs.layer_stride) {
  957. return true;
  958. }
  959. // Although we also have to compare for cases where it can be unaligned
  960. // This can happen if the image doesn't have layers, so the stride is not aligned
  961. if (lhs.maybe_unaligned_layer_stride == rhs.maybe_unaligned_layer_stride) {
  962. return true;
  963. }
  964. return false;
  965. }
  966. std::optional<SubresourceBase> FindSubresource(const ImageInfo& candidate, const ImageBase& image,
  967. GPUVAddr candidate_addr, RelaxedOptions options,
  968. bool broken_views, bool native_bgr) {
  969. const std::optional<SubresourceBase> base = image.TryFindBase(candidate_addr);
  970. if (!base) {
  971. return std::nullopt;
  972. }
  973. const ImageInfo& existing = image.info;
  974. if (True(options & RelaxedOptions::Format)) {
  975. // Format checking is relaxed, but we still have to check for matching bytes per block.
  976. // This avoids creating a view for blits on UE4 titles where formats with different bytes
  977. // per block are aliased.
  978. if (BytesPerBlock(existing.format) != BytesPerBlock(candidate.format)) {
  979. return std::nullopt;
  980. }
  981. } else {
  982. // Format comaptibility is not relaxed, ensure we are creating a view on a compatible format
  983. if (!IsViewCompatible(existing.format, candidate.format, broken_views, native_bgr)) {
  984. return std::nullopt;
  985. }
  986. }
  987. if (!IsLayerStrideCompatible(existing, candidate)) {
  988. return std::nullopt;
  989. }
  990. if (existing.type != candidate.type) {
  991. return std::nullopt;
  992. }
  993. if (False(options & RelaxedOptions::Samples)) {
  994. if (existing.num_samples != candidate.num_samples) {
  995. return std::nullopt;
  996. }
  997. }
  998. if (existing.resources.levels < candidate.resources.levels + base->level) {
  999. return std::nullopt;
  1000. }
  1001. if (existing.type == ImageType::e3D) {
  1002. const u32 mip_depth = std::max(1U, existing.size.depth << base->level);
  1003. if (mip_depth < candidate.size.depth + base->layer) {
  1004. return std::nullopt;
  1005. }
  1006. } else {
  1007. if (existing.resources.layers < candidate.resources.layers + base->layer) {
  1008. return std::nullopt;
  1009. }
  1010. }
  1011. const bool strict_size = False(options & RelaxedOptions::Size);
  1012. if (!IsBlockLinearSizeCompatible(existing, candidate, base->level, 0, strict_size)) {
  1013. return std::nullopt;
  1014. }
  1015. // TODO: compare block sizes
  1016. return base;
  1017. }
  1018. bool IsSubresource(const ImageInfo& candidate, const ImageBase& image, GPUVAddr candidate_addr,
  1019. RelaxedOptions options, bool broken_views, bool native_bgr) {
  1020. return FindSubresource(candidate, image, candidate_addr, options, broken_views, native_bgr)
  1021. .has_value();
  1022. }
  1023. void DeduceBlitImages(ImageInfo& dst_info, ImageInfo& src_info, const ImageBase* dst,
  1024. const ImageBase* src) {
  1025. const auto original_dst_format = dst_info.format;
  1026. if (src && GetFormatType(src->info.format) != SurfaceType::ColorTexture) {
  1027. src_info.format = src->info.format;
  1028. }
  1029. if (dst && GetFormatType(dst->info.format) != SurfaceType::ColorTexture) {
  1030. dst_info.format = dst->info.format;
  1031. }
  1032. if (src && GetFormatType(src->info.format) != SurfaceType::ColorTexture) {
  1033. dst_info.format = src->info.format;
  1034. }
  1035. if (dst && GetFormatType(dst->info.format) != SurfaceType::ColorTexture) {
  1036. if (src) {
  1037. if (GetFormatType(src->info.format) == SurfaceType::ColorTexture) {
  1038. dst_info.format = original_dst_format;
  1039. }
  1040. } else {
  1041. src_info.format = dst->info.format;
  1042. }
  1043. }
  1044. }
  1045. u32 MapSizeBytes(const ImageBase& image) {
  1046. if (True(image.flags & ImageFlagBits::AcceleratedUpload)) {
  1047. return image.guest_size_bytes;
  1048. } else if (True(image.flags & ImageFlagBits::Converted)) {
  1049. return image.converted_size_bytes;
  1050. } else {
  1051. return image.unswizzled_size_bytes;
  1052. }
  1053. }
  1054. static_assert(CalculateLevelSize(LevelInfo{{1920, 1080, 1}, {0, 2, 0}, {1, 1}, 2, 0}, 0) ==
  1055. 0x7f8000);
  1056. static_assert(CalculateLevelSize(LevelInfo{{32, 32, 1}, {0, 0, 4}, {1, 1}, 4, 0}, 0) == 0x4000);
  1057. static_assert(CalculateLevelOffset(PixelFormat::R8_SINT, {1920, 1080, 1}, {0, 2, 0}, 0, 7) ==
  1058. 0x2afc00);
  1059. static_assert(CalculateLevelOffset(PixelFormat::ASTC_2D_12X12_UNORM, {8192, 4096, 1}, {0, 2, 0}, 0,
  1060. 12) == 0x50d200);
  1061. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 0) ==
  1062. 0);
  1063. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 1) ==
  1064. 0x400000);
  1065. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 2) ==
  1066. 0x500000);
  1067. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 3) ==
  1068. 0x540000);
  1069. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 4) ==
  1070. 0x550000);
  1071. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 5) ==
  1072. 0x554000);
  1073. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 6) ==
  1074. 0x555000);
  1075. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 7) ==
  1076. 0x555400);
  1077. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 8) ==
  1078. 0x555600);
  1079. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 9) ==
  1080. 0x555800);
  1081. constexpr u32 ValidateLayerSize(PixelFormat format, u32 width, u32 height, u32 block_height,
  1082. u32 tile_width_spacing, u32 level) {
  1083. const Extent3D size{width, height, 1};
  1084. const Extent3D block{0, block_height, 0};
  1085. const u32 offset = CalculateLevelOffset(format, size, block, tile_width_spacing, level);
  1086. return AlignLayerSize(offset, size, block, DefaultBlockHeight(format), tile_width_spacing);
  1087. }
  1088. static_assert(ValidateLayerSize(PixelFormat::ASTC_2D_12X12_UNORM, 8192, 4096, 2, 0, 12) ==
  1089. 0x50d800);
  1090. static_assert(ValidateLayerSize(PixelFormat::A8B8G8R8_UNORM, 1024, 1024, 2, 0, 10) == 0x556000);
  1091. static_assert(ValidateLayerSize(PixelFormat::BC3_UNORM, 128, 128, 2, 0, 8) == 0x6000);
  1092. static_assert(ValidateLayerSize(PixelFormat::A8B8G8R8_UNORM, 518, 572, 4, 3, 1) == 0x190000,
  1093. "Tile width spacing is not working");
  1094. static_assert(ValidateLayerSize(PixelFormat::BC5_UNORM, 1024, 1024, 3, 4, 11) == 0x160000,
  1095. "Compressed tile width spacing is not working");
  1096. } // namespace VideoCommon