util.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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) {
  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(info.tile_width_spacing > 0);
  470. UNIMPLEMENTED_IF(copy.image_offset.x != 0);
  471. UNIMPLEMENTED_IF(copy.image_offset.y != 0);
  472. UNIMPLEMENTED_IF(copy.image_offset.z != 0);
  473. UNIMPLEMENTED_IF(copy.image_extent != level_size);
  474. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  475. const Extent3D block = AdjustMipBlockSize(num_tiles, level_info.block, level);
  476. size_t host_offset = copy.buffer_offset;
  477. const u32 num_levels = info.resources.levels;
  478. const std::array sizes = CalculateLevelSizes(level_info, num_levels);
  479. size_t guest_offset = CalculateLevelBytes(sizes, level);
  480. const size_t layer_stride =
  481. AlignLayerSize(CalculateLevelBytes(sizes, num_levels), size, level_info.block,
  482. tile_size.height, info.tile_width_spacing);
  483. const size_t subresource_size = sizes[level];
  484. const auto dst_data = std::make_unique<u8[]>(subresource_size);
  485. const std::span<u8> dst(dst_data.get(), subresource_size);
  486. for (s32 layer = 0; layer < info.resources.layers; ++layer) {
  487. const std::span<const u8> src = input.subspan(host_offset);
  488. gpu_memory.ReadBlockUnsafe(gpu_addr + guest_offset, dst.data(), dst.size_bytes());
  489. SwizzleTexture(dst, src, bytes_per_block, num_tiles.width, num_tiles.height,
  490. num_tiles.depth, block.height, block.depth);
  491. gpu_memory.WriteBlockUnsafe(gpu_addr + guest_offset, dst.data(), dst.size_bytes());
  492. host_offset += host_bytes_per_layer;
  493. guest_offset += layer_stride;
  494. }
  495. ASSERT(host_offset - copy.buffer_offset == copy.buffer_size);
  496. }
  497. } // Anonymous namespace
  498. u32 CalculateGuestSizeInBytes(const ImageInfo& info) noexcept {
  499. if (info.type == ImageType::Buffer) {
  500. return info.size.width * BytesPerBlock(info.format);
  501. }
  502. if (info.type == ImageType::Linear) {
  503. return info.pitch * Common::DivCeil(info.size.height, DefaultBlockHeight(info.format));
  504. }
  505. if (info.resources.layers > 1) {
  506. ASSERT(info.layer_stride != 0);
  507. return info.layer_stride * info.resources.layers;
  508. } else {
  509. return CalculateLayerSize(info);
  510. }
  511. }
  512. u32 CalculateUnswizzledSizeBytes(const ImageInfo& info) noexcept {
  513. if (info.type == ImageType::Buffer) {
  514. return info.size.width * BytesPerBlock(info.format);
  515. }
  516. if (info.num_samples > 1) {
  517. // Multisample images can't be uploaded or downloaded to the host
  518. return 0;
  519. }
  520. if (info.type == ImageType::Linear) {
  521. return info.pitch * Common::DivCeil(info.size.height, DefaultBlockHeight(info.format));
  522. }
  523. const Extent2D tile_size = DefaultBlockSize(info.format);
  524. return NumBlocksPerLayer(info, tile_size) * info.resources.layers * BytesPerBlock(info.format);
  525. }
  526. u32 CalculateConvertedSizeBytes(const ImageInfo& info) noexcept {
  527. if (info.type == ImageType::Buffer) {
  528. return info.size.width * BytesPerBlock(info.format);
  529. }
  530. static constexpr Extent2D TILE_SIZE{1, 1};
  531. return NumBlocksPerLayer(info, TILE_SIZE) * info.resources.layers * CONVERTED_BYTES_PER_BLOCK;
  532. }
  533. u32 CalculateLayerStride(const ImageInfo& info) noexcept {
  534. ASSERT(info.type != ImageType::Linear);
  535. const u32 layer_size = CalculateLayerSize(info);
  536. const Extent3D size = info.size;
  537. const Extent3D block = info.block;
  538. const u32 tile_size_y = DefaultBlockHeight(info.format);
  539. return AlignLayerSize(layer_size, size, block, tile_size_y, info.tile_width_spacing);
  540. }
  541. u32 CalculateLayerSize(const ImageInfo& info) noexcept {
  542. ASSERT(info.type != ImageType::Linear);
  543. return CalculateLevelOffset(info.format, info.size, info.block, info.tile_width_spacing,
  544. info.resources.levels);
  545. }
  546. LevelArray CalculateMipLevelOffsets(const ImageInfo& info) noexcept {
  547. if (info.type == ImageType::Linear) {
  548. return {};
  549. }
  550. ASSERT(info.resources.levels <= static_cast<s32>(MAX_MIP_LEVELS));
  551. const LevelInfo level_info = MakeLevelInfo(info);
  552. LevelArray offsets{};
  553. u32 offset = 0;
  554. for (s32 level = 0; level < info.resources.levels; ++level) {
  555. offsets[level] = offset;
  556. offset += CalculateLevelSize(level_info, level);
  557. }
  558. return offsets;
  559. }
  560. LevelArray CalculateMipLevelSizes(const ImageInfo& info) noexcept {
  561. const u32 num_levels = info.resources.levels;
  562. const LevelInfo level_info = MakeLevelInfo(info);
  563. LevelArray sizes{};
  564. for (u32 level = 0; level < num_levels; ++level) {
  565. sizes[level] = CalculateLevelSize(level_info, level);
  566. }
  567. return sizes;
  568. }
  569. std::vector<u32> CalculateSliceOffsets(const ImageInfo& info) {
  570. ASSERT(info.type == ImageType::e3D);
  571. std::vector<u32> offsets;
  572. offsets.reserve(NumSlices(info));
  573. const LevelInfo level_info = MakeLevelInfo(info);
  574. u32 mip_offset = 0;
  575. for (s32 level = 0; level < info.resources.levels; ++level) {
  576. const Extent3D tile_shift = TileShift(level_info, level);
  577. const Extent3D tiles = LevelTiles(level_info, level);
  578. const u32 gob_size_shift = tile_shift.height + GOB_SIZE_SHIFT;
  579. const u32 slice_size = (tiles.width * tiles.height) << gob_size_shift;
  580. const u32 z_mask = (1U << tile_shift.depth) - 1;
  581. const u32 depth = AdjustMipSize(info.size.depth, level);
  582. for (u32 slice = 0; slice < depth; ++slice) {
  583. const u32 z_low = slice & z_mask;
  584. const u32 z_high = slice & ~z_mask;
  585. offsets.push_back(mip_offset + (z_low << gob_size_shift) + (z_high * slice_size));
  586. }
  587. mip_offset += CalculateLevelSize(level_info, level);
  588. }
  589. return offsets;
  590. }
  591. std::vector<SubresourceBase> CalculateSliceSubresources(const ImageInfo& info) {
  592. ASSERT(info.type == ImageType::e3D);
  593. std::vector<SubresourceBase> subresources;
  594. subresources.reserve(NumSlices(info));
  595. for (s32 level = 0; level < info.resources.levels; ++level) {
  596. const s32 depth = AdjustMipSize(info.size.depth, level);
  597. for (s32 slice = 0; slice < depth; ++slice) {
  598. subresources.emplace_back(SubresourceBase{
  599. .level = level,
  600. .layer = slice,
  601. });
  602. }
  603. }
  604. return subresources;
  605. }
  606. u32 CalculateLevelStrideAlignment(const ImageInfo& info, u32 level) {
  607. const Extent2D tile_size = DefaultBlockSize(info.format);
  608. const Extent3D level_size = AdjustMipSize(info.size, level);
  609. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  610. const Extent3D block = AdjustMipBlockSize(num_tiles, info.block, level);
  611. const u32 bpp_log2 = BytesPerBlockLog2(info.format);
  612. return StrideAlignment(num_tiles, block, bpp_log2, info.tile_width_spacing);
  613. }
  614. PixelFormat PixelFormatFromTIC(const TICEntry& config) noexcept {
  615. return PixelFormatFromTextureInfo(config.format, config.r_type, config.g_type, config.b_type,
  616. config.a_type, config.srgb_conversion);
  617. }
  618. ImageViewType RenderTargetImageViewType(const ImageInfo& info) noexcept {
  619. switch (info.type) {
  620. case ImageType::e2D:
  621. return info.resources.layers > 1 ? ImageViewType::e2DArray : ImageViewType::e2D;
  622. case ImageType::e3D:
  623. return ImageViewType::e2DArray;
  624. case ImageType::Linear:
  625. return ImageViewType::e2D;
  626. default:
  627. UNIMPLEMENTED_MSG("Unimplemented image type={}", static_cast<int>(info.type));
  628. return ImageViewType{};
  629. }
  630. }
  631. std::vector<ImageCopy> MakeShrinkImageCopies(const ImageInfo& dst, const ImageInfo& src,
  632. SubresourceBase base, u32 up_scale, u32 down_shift) {
  633. ASSERT(dst.resources.levels >= src.resources.levels);
  634. ASSERT(dst.num_samples == src.num_samples);
  635. const bool is_dst_3d = dst.type == ImageType::e3D;
  636. if (is_dst_3d) {
  637. ASSERT(src.type == ImageType::e3D);
  638. ASSERT(src.resources.levels == 1);
  639. }
  640. const bool both_2d{src.type == ImageType::e2D && dst.type == ImageType::e2D};
  641. std::vector<ImageCopy> copies;
  642. copies.reserve(src.resources.levels);
  643. for (s32 level = 0; level < src.resources.levels; ++level) {
  644. ImageCopy& copy = copies.emplace_back();
  645. copy.src_subresource = SubresourceLayers{
  646. .base_level = level,
  647. .base_layer = 0,
  648. .num_layers = src.resources.layers,
  649. };
  650. copy.dst_subresource = SubresourceLayers{
  651. .base_level = base.level + level,
  652. .base_layer = is_dst_3d ? 0 : base.layer,
  653. .num_layers = is_dst_3d ? 1 : src.resources.layers,
  654. };
  655. copy.src_offset = Offset3D{
  656. .x = 0,
  657. .y = 0,
  658. .z = 0,
  659. };
  660. copy.dst_offset = Offset3D{
  661. .x = 0,
  662. .y = 0,
  663. .z = is_dst_3d ? base.layer : 0,
  664. };
  665. const Extent3D mip_size = AdjustMipSize(dst.size, base.level + level);
  666. copy.extent = AdjustSamplesSize(mip_size, dst.num_samples);
  667. if (is_dst_3d) {
  668. copy.extent.depth = src.size.depth;
  669. }
  670. copy.extent.width = std::max<u32>((copy.extent.width * up_scale) >> down_shift, 1);
  671. if (both_2d) {
  672. copy.extent.height = std::max<u32>((copy.extent.height * up_scale) >> down_shift, 1);
  673. }
  674. }
  675. return copies;
  676. }
  677. bool IsValidEntry(const Tegra::MemoryManager& gpu_memory, const TICEntry& config) {
  678. const GPUVAddr address = config.Address();
  679. if (address == 0) {
  680. return false;
  681. }
  682. if (address >= (1ULL << 40)) {
  683. return false;
  684. }
  685. if (gpu_memory.GpuToCpuAddress(address).has_value()) {
  686. return true;
  687. }
  688. const ImageInfo info{config};
  689. const size_t guest_size_bytes = CalculateGuestSizeInBytes(info);
  690. return gpu_memory.GpuToCpuAddress(address, guest_size_bytes).has_value();
  691. }
  692. std::vector<BufferImageCopy> UnswizzleImage(Tegra::MemoryManager& gpu_memory, GPUVAddr gpu_addr,
  693. const ImageInfo& info, std::span<u8> output) {
  694. const size_t guest_size_bytes = CalculateGuestSizeInBytes(info);
  695. const u32 bpp_log2 = BytesPerBlockLog2(info.format);
  696. const Extent3D size = info.size;
  697. if (info.type == ImageType::Linear) {
  698. gpu_memory.ReadBlockUnsafe(gpu_addr, output.data(), guest_size_bytes);
  699. ASSERT((info.pitch >> bpp_log2) << bpp_log2 == info.pitch);
  700. return {{
  701. .buffer_offset = 0,
  702. .buffer_size = guest_size_bytes,
  703. .buffer_row_length = info.pitch >> bpp_log2,
  704. .buffer_image_height = size.height,
  705. .image_subresource =
  706. {
  707. .base_level = 0,
  708. .base_layer = 0,
  709. .num_layers = 1,
  710. },
  711. .image_offset = {0, 0, 0},
  712. .image_extent = size,
  713. }};
  714. }
  715. const auto input_data = std::make_unique<u8[]>(guest_size_bytes);
  716. gpu_memory.ReadBlockUnsafe(gpu_addr, input_data.get(), guest_size_bytes);
  717. const std::span<const u8> input(input_data.get(), guest_size_bytes);
  718. const LevelInfo level_info = MakeLevelInfo(info);
  719. const s32 num_layers = info.resources.layers;
  720. const s32 num_levels = info.resources.levels;
  721. const Extent2D tile_size = DefaultBlockSize(info.format);
  722. const std::array level_sizes = CalculateLevelSizes(level_info, num_levels);
  723. const Extent2D gob = GobSize(bpp_log2, info.block.height, info.tile_width_spacing);
  724. const u32 layer_size = CalculateLevelBytes(level_sizes, num_levels);
  725. const u32 layer_stride = AlignLayerSize(layer_size, size, level_info.block, tile_size.height,
  726. info.tile_width_spacing);
  727. size_t guest_offset = 0;
  728. u32 host_offset = 0;
  729. std::vector<BufferImageCopy> copies(num_levels);
  730. for (s32 level = 0; level < num_levels; ++level) {
  731. const Extent3D level_size = AdjustMipSize(size, level);
  732. const u32 num_blocks_per_layer = NumBlocks(level_size, tile_size);
  733. const u32 host_bytes_per_layer = num_blocks_per_layer << bpp_log2;
  734. copies[level] = BufferImageCopy{
  735. .buffer_offset = host_offset,
  736. .buffer_size = static_cast<size_t>(host_bytes_per_layer) * num_layers,
  737. .buffer_row_length = Common::AlignUp(level_size.width, tile_size.width),
  738. .buffer_image_height = Common::AlignUp(level_size.height, tile_size.height),
  739. .image_subresource =
  740. {
  741. .base_level = level,
  742. .base_layer = 0,
  743. .num_layers = info.resources.layers,
  744. },
  745. .image_offset = {0, 0, 0},
  746. .image_extent = level_size,
  747. };
  748. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  749. const Extent3D block = AdjustMipBlockSize(num_tiles, level_info.block, level);
  750. const u32 stride_alignment = StrideAlignment(num_tiles, info.block, gob, bpp_log2);
  751. size_t guest_layer_offset = 0;
  752. for (s32 layer = 0; layer < info.resources.layers; ++layer) {
  753. const std::span<u8> dst = output.subspan(host_offset);
  754. const std::span<const u8> src = input.subspan(guest_offset + guest_layer_offset);
  755. UnswizzleTexture(dst, src, 1U << bpp_log2, num_tiles.width, num_tiles.height,
  756. num_tiles.depth, block.height, block.depth, stride_alignment);
  757. guest_layer_offset += layer_stride;
  758. host_offset += host_bytes_per_layer;
  759. }
  760. guest_offset += level_sizes[level];
  761. }
  762. return copies;
  763. }
  764. BufferCopy UploadBufferCopy(Tegra::MemoryManager& gpu_memory, GPUVAddr gpu_addr,
  765. const ImageBase& image, std::span<u8> output) {
  766. gpu_memory.ReadBlockUnsafe(gpu_addr, output.data(), image.guest_size_bytes);
  767. return BufferCopy{
  768. .src_offset = 0,
  769. .dst_offset = 0,
  770. .size = image.guest_size_bytes,
  771. };
  772. }
  773. void ConvertImage(std::span<const u8> input, const ImageInfo& info, std::span<u8> output,
  774. std::span<BufferImageCopy> copies) {
  775. u32 output_offset = 0;
  776. const Extent2D tile_size = DefaultBlockSize(info.format);
  777. for (BufferImageCopy& copy : copies) {
  778. const u32 level = copy.image_subresource.base_level;
  779. const Extent3D mip_size = AdjustMipSize(info.size, level);
  780. ASSERT(copy.image_offset == Offset3D{});
  781. ASSERT(copy.image_subresource.base_layer == 0);
  782. ASSERT(copy.image_extent == mip_size);
  783. ASSERT(copy.buffer_row_length == Common::AlignUp(mip_size.width, tile_size.width));
  784. ASSERT(copy.buffer_image_height == Common::AlignUp(mip_size.height, tile_size.height));
  785. if (IsPixelFormatASTC(info.format)) {
  786. ASSERT(copy.image_extent.depth == 1);
  787. Tegra::Texture::ASTC::Decompress(input.subspan(copy.buffer_offset),
  788. copy.image_extent.width, copy.image_extent.height,
  789. copy.image_subresource.num_layers, tile_size.width,
  790. tile_size.height, output.subspan(output_offset));
  791. } else {
  792. DecompressBC4(input.subspan(copy.buffer_offset), copy.image_extent,
  793. output.subspan(output_offset));
  794. }
  795. copy.buffer_offset = output_offset;
  796. copy.buffer_row_length = mip_size.width;
  797. copy.buffer_image_height = mip_size.height;
  798. output_offset += copy.image_extent.width * copy.image_extent.height *
  799. copy.image_subresource.num_layers * CONVERTED_BYTES_PER_BLOCK;
  800. }
  801. }
  802. std::vector<BufferImageCopy> FullDownloadCopies(const ImageInfo& info) {
  803. const Extent3D size = info.size;
  804. const u32 bytes_per_block = BytesPerBlock(info.format);
  805. if (info.type == ImageType::Linear) {
  806. ASSERT(info.pitch % bytes_per_block == 0);
  807. return {{
  808. .buffer_offset = 0,
  809. .buffer_size = static_cast<size_t>(info.pitch) * size.height,
  810. .buffer_row_length = info.pitch / bytes_per_block,
  811. .buffer_image_height = size.height,
  812. .image_subresource =
  813. {
  814. .base_level = 0,
  815. .base_layer = 0,
  816. .num_layers = 1,
  817. },
  818. .image_offset = {0, 0, 0},
  819. .image_extent = size,
  820. }};
  821. }
  822. UNIMPLEMENTED_IF(info.tile_width_spacing > 0);
  823. const s32 num_layers = info.resources.layers;
  824. const s32 num_levels = info.resources.levels;
  825. const Extent2D tile_size = DefaultBlockSize(info.format);
  826. u32 host_offset = 0;
  827. std::vector<BufferImageCopy> copies(num_levels);
  828. for (s32 level = 0; level < num_levels; ++level) {
  829. const Extent3D level_size = AdjustMipSize(size, level);
  830. const u32 num_blocks_per_layer = NumBlocks(level_size, tile_size);
  831. const u32 host_bytes_per_level = num_blocks_per_layer * bytes_per_block * num_layers;
  832. copies[level] = BufferImageCopy{
  833. .buffer_offset = host_offset,
  834. .buffer_size = host_bytes_per_level,
  835. .buffer_row_length = level_size.width,
  836. .buffer_image_height = level_size.height,
  837. .image_subresource =
  838. {
  839. .base_level = level,
  840. .base_layer = 0,
  841. .num_layers = info.resources.layers,
  842. },
  843. .image_offset = {0, 0, 0},
  844. .image_extent = level_size,
  845. };
  846. host_offset += host_bytes_per_level;
  847. }
  848. return copies;
  849. }
  850. Extent3D MipSize(Extent3D size, u32 level) {
  851. return AdjustMipSize(size, level);
  852. }
  853. Extent3D MipBlockSize(const ImageInfo& info, u32 level) {
  854. const LevelInfo level_info = MakeLevelInfo(info);
  855. const Extent2D tile_size = DefaultBlockSize(info.format);
  856. const Extent3D level_size = AdjustMipSize(info.size, level);
  857. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  858. return AdjustMipBlockSize(num_tiles, level_info.block, level);
  859. }
  860. std::vector<SwizzleParameters> FullUploadSwizzles(const ImageInfo& info) {
  861. const Extent2D tile_size = DefaultBlockSize(info.format);
  862. if (info.type == ImageType::Linear) {
  863. return std::vector{SwizzleParameters{
  864. .num_tiles = AdjustTileSize(info.size, tile_size),
  865. .block = {},
  866. .buffer_offset = 0,
  867. .level = 0,
  868. }};
  869. }
  870. const LevelInfo level_info = MakeLevelInfo(info);
  871. const Extent3D size = info.size;
  872. const s32 num_levels = info.resources.levels;
  873. u32 guest_offset = 0;
  874. std::vector<SwizzleParameters> params(num_levels);
  875. for (s32 level = 0; level < num_levels; ++level) {
  876. const Extent3D level_size = AdjustMipSize(size, level);
  877. const Extent3D num_tiles = AdjustTileSize(level_size, tile_size);
  878. const Extent3D block = AdjustMipBlockSize(num_tiles, level_info.block, level);
  879. params[level] = SwizzleParameters{
  880. .num_tiles = num_tiles,
  881. .block = block,
  882. .buffer_offset = guest_offset,
  883. .level = level,
  884. };
  885. guest_offset += CalculateLevelSize(level_info, level);
  886. }
  887. return params;
  888. }
  889. void SwizzleImage(Tegra::MemoryManager& gpu_memory, GPUVAddr gpu_addr, const ImageInfo& info,
  890. std::span<const BufferImageCopy> copies, std::span<const u8> memory) {
  891. const bool is_pitch_linear = info.type == ImageType::Linear;
  892. for (const BufferImageCopy& copy : copies) {
  893. if (is_pitch_linear) {
  894. SwizzlePitchLinearImage(gpu_memory, gpu_addr, info, copy, memory);
  895. } else {
  896. SwizzleBlockLinearImage(gpu_memory, gpu_addr, info, copy, memory);
  897. }
  898. }
  899. }
  900. bool IsBlockLinearSizeCompatible(const ImageInfo& lhs, const ImageInfo& rhs, u32 lhs_level,
  901. u32 rhs_level, bool strict_size) noexcept {
  902. ASSERT(lhs.type != ImageType::Linear);
  903. ASSERT(rhs.type != ImageType::Linear);
  904. if (strict_size) {
  905. const Extent3D lhs_size = AdjustMipSize(lhs.size, lhs_level);
  906. const Extent3D rhs_size = AdjustMipSize(rhs.size, rhs_level);
  907. return lhs_size.width == rhs_size.width && lhs_size.height == rhs_size.height;
  908. } else {
  909. const Extent3D lhs_size = BlockLinearAlignedSize(lhs, lhs_level);
  910. const Extent3D rhs_size = BlockLinearAlignedSize(rhs, rhs_level);
  911. return lhs_size.width == rhs_size.width && lhs_size.height == rhs_size.height;
  912. }
  913. }
  914. bool IsPitchLinearSameSize(const ImageInfo& lhs, const ImageInfo& rhs, bool strict_size) noexcept {
  915. ASSERT(lhs.type == ImageType::Linear);
  916. ASSERT(rhs.type == ImageType::Linear);
  917. if (strict_size) {
  918. return lhs.size.width == rhs.size.width && lhs.size.height == rhs.size.height;
  919. } else {
  920. const Extent2D lhs_size = PitchLinearAlignedSize(lhs);
  921. const Extent2D rhs_size = PitchLinearAlignedSize(rhs);
  922. return lhs_size == rhs_size;
  923. }
  924. }
  925. std::optional<OverlapResult> ResolveOverlap(const ImageInfo& new_info, GPUVAddr gpu_addr,
  926. VAddr cpu_addr, const ImageBase& overlap,
  927. bool strict_size, bool broken_views, bool native_bgr) {
  928. ASSERT(new_info.type != ImageType::Linear);
  929. ASSERT(overlap.info.type != ImageType::Linear);
  930. if (!IsLayerStrideCompatible(new_info, overlap.info)) {
  931. return std::nullopt;
  932. }
  933. if (!IsViewCompatible(overlap.info.format, new_info.format, broken_views, native_bgr)) {
  934. return std::nullopt;
  935. }
  936. if (gpu_addr == overlap.gpu_addr) {
  937. const std::optional solution = ResolveOverlapEqualAddress(new_info, overlap, strict_size);
  938. if (!solution) {
  939. return std::nullopt;
  940. }
  941. return OverlapResult{
  942. .gpu_addr = gpu_addr,
  943. .cpu_addr = cpu_addr,
  944. .resources = *solution,
  945. };
  946. }
  947. if (overlap.gpu_addr > gpu_addr) {
  948. return ResolveOverlapRightAddress(new_info, gpu_addr, cpu_addr, overlap, strict_size);
  949. }
  950. // if overlap.gpu_addr < gpu_addr
  951. return ResolveOverlapLeftAddress(new_info, gpu_addr, cpu_addr, overlap, strict_size);
  952. }
  953. bool IsLayerStrideCompatible(const ImageInfo& lhs, const ImageInfo& rhs) {
  954. // If either of the layer strides is zero, we can assume they are compatible
  955. // These images generally come from rendertargets
  956. if (lhs.layer_stride == 0) {
  957. return true;
  958. }
  959. if (rhs.layer_stride == 0) {
  960. return true;
  961. }
  962. // It's definitely compatible if the layer stride matches
  963. if (lhs.layer_stride == rhs.layer_stride) {
  964. return true;
  965. }
  966. // Although we also have to compare for cases where it can be unaligned
  967. // This can happen if the image doesn't have layers, so the stride is not aligned
  968. if (lhs.maybe_unaligned_layer_stride == rhs.maybe_unaligned_layer_stride) {
  969. return true;
  970. }
  971. return false;
  972. }
  973. std::optional<SubresourceBase> FindSubresource(const ImageInfo& candidate, const ImageBase& image,
  974. GPUVAddr candidate_addr, RelaxedOptions options,
  975. bool broken_views, bool native_bgr) {
  976. const std::optional<SubresourceBase> base = image.TryFindBase(candidate_addr);
  977. if (!base) {
  978. return std::nullopt;
  979. }
  980. const ImageInfo& existing = image.info;
  981. if (True(options & RelaxedOptions::Format)) {
  982. // Format checking is relaxed, but we still have to check for matching bytes per block.
  983. // This avoids creating a view for blits on UE4 titles where formats with different bytes
  984. // per block are aliased.
  985. if (BytesPerBlock(existing.format) != BytesPerBlock(candidate.format)) {
  986. return std::nullopt;
  987. }
  988. } else {
  989. // Format comaptibility is not relaxed, ensure we are creating a view on a compatible format
  990. if (!IsViewCompatible(existing.format, candidate.format, broken_views, native_bgr)) {
  991. return std::nullopt;
  992. }
  993. }
  994. if (!IsLayerStrideCompatible(existing, candidate)) {
  995. return std::nullopt;
  996. }
  997. if (existing.type != candidate.type) {
  998. return std::nullopt;
  999. }
  1000. if (False(options & RelaxedOptions::Samples)) {
  1001. if (existing.num_samples != candidate.num_samples) {
  1002. return std::nullopt;
  1003. }
  1004. }
  1005. if (existing.resources.levels < candidate.resources.levels + base->level) {
  1006. return std::nullopt;
  1007. }
  1008. if (existing.type == ImageType::e3D) {
  1009. const u32 mip_depth = std::max(1U, existing.size.depth << base->level);
  1010. if (mip_depth < candidate.size.depth + base->layer) {
  1011. return std::nullopt;
  1012. }
  1013. } else {
  1014. if (existing.resources.layers < candidate.resources.layers + base->layer) {
  1015. return std::nullopt;
  1016. }
  1017. }
  1018. const bool strict_size = False(options & RelaxedOptions::Size);
  1019. if (!IsBlockLinearSizeCompatible(existing, candidate, base->level, 0, strict_size)) {
  1020. return std::nullopt;
  1021. }
  1022. // TODO: compare block sizes
  1023. return base;
  1024. }
  1025. bool IsSubresource(const ImageInfo& candidate, const ImageBase& image, GPUVAddr candidate_addr,
  1026. RelaxedOptions options, bool broken_views, bool native_bgr) {
  1027. return FindSubresource(candidate, image, candidate_addr, options, broken_views, native_bgr)
  1028. .has_value();
  1029. }
  1030. void DeduceBlitImages(ImageInfo& dst_info, ImageInfo& src_info, const ImageBase* dst,
  1031. const ImageBase* src) {
  1032. const auto original_dst_format = dst_info.format;
  1033. if (src && GetFormatType(src->info.format) != SurfaceType::ColorTexture) {
  1034. src_info.format = src->info.format;
  1035. }
  1036. if (dst && GetFormatType(dst->info.format) != SurfaceType::ColorTexture) {
  1037. dst_info.format = dst->info.format;
  1038. }
  1039. if (src && GetFormatType(src->info.format) != SurfaceType::ColorTexture) {
  1040. dst_info.format = src->info.format;
  1041. }
  1042. if (dst && GetFormatType(dst->info.format) != SurfaceType::ColorTexture) {
  1043. if (src) {
  1044. if (GetFormatType(src->info.format) == SurfaceType::ColorTexture) {
  1045. dst_info.format = original_dst_format;
  1046. }
  1047. } else {
  1048. src_info.format = dst->info.format;
  1049. }
  1050. }
  1051. }
  1052. u32 MapSizeBytes(const ImageBase& image) {
  1053. if (True(image.flags & ImageFlagBits::AcceleratedUpload)) {
  1054. return image.guest_size_bytes;
  1055. } else if (True(image.flags & ImageFlagBits::Converted)) {
  1056. return image.converted_size_bytes;
  1057. } else {
  1058. return image.unswizzled_size_bytes;
  1059. }
  1060. }
  1061. static_assert(CalculateLevelSize(LevelInfo{{1920, 1080, 1}, {0, 2, 0}, {1, 1}, 2, 0}, 0) ==
  1062. 0x7f8000);
  1063. static_assert(CalculateLevelSize(LevelInfo{{32, 32, 1}, {0, 0, 4}, {1, 1}, 4, 0}, 0) == 0x4000);
  1064. static_assert(CalculateLevelOffset(PixelFormat::R8_SINT, {1920, 1080, 1}, {0, 2, 0}, 0, 7) ==
  1065. 0x2afc00);
  1066. static_assert(CalculateLevelOffset(PixelFormat::ASTC_2D_12X12_UNORM, {8192, 4096, 1}, {0, 2, 0}, 0,
  1067. 12) == 0x50d200);
  1068. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 0) ==
  1069. 0);
  1070. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 1) ==
  1071. 0x400000);
  1072. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 2) ==
  1073. 0x500000);
  1074. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 3) ==
  1075. 0x540000);
  1076. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 4) ==
  1077. 0x550000);
  1078. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 5) ==
  1079. 0x554000);
  1080. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 6) ==
  1081. 0x555000);
  1082. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 7) ==
  1083. 0x555400);
  1084. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 8) ==
  1085. 0x555600);
  1086. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 9) ==
  1087. 0x555800);
  1088. constexpr u32 ValidateLayerSize(PixelFormat format, u32 width, u32 height, u32 block_height,
  1089. u32 tile_width_spacing, u32 level) {
  1090. const Extent3D size{width, height, 1};
  1091. const Extent3D block{0, block_height, 0};
  1092. const u32 offset = CalculateLevelOffset(format, size, block, tile_width_spacing, level);
  1093. return AlignLayerSize(offset, size, block, DefaultBlockHeight(format), tile_width_spacing);
  1094. }
  1095. static_assert(ValidateLayerSize(PixelFormat::ASTC_2D_12X12_UNORM, 8192, 4096, 2, 0, 12) ==
  1096. 0x50d800);
  1097. static_assert(ValidateLayerSize(PixelFormat::A8B8G8R8_UNORM, 1024, 1024, 2, 0, 10) == 0x556000);
  1098. static_assert(ValidateLayerSize(PixelFormat::BC3_UNORM, 128, 128, 2, 0, 8) == 0x6000);
  1099. static_assert(ValidateLayerSize(PixelFormat::A8B8G8R8_UNORM, 518, 572, 4, 3, 1) == 0x190000,
  1100. "Tile width spacing is not working");
  1101. static_assert(ValidateLayerSize(PixelFormat::BC5_UNORM, 1024, 1024, 3, 4, 11) == 0x160000,
  1102. "Compressed tile width spacing is not working");
  1103. } // namespace VideoCommon