util.cpp 51 KB

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