util.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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 u32 layer_stride = new_info.layer_stride;
  329. const s32 new_size = layer_stride * new_info.resources.layers;
  330. const s32 diff = static_cast<s32>(overlap.gpu_addr - gpu_addr);
  331. if (diff > new_size) {
  332. return std::nullopt;
  333. }
  334. const s32 base_layer = diff / layer_stride;
  335. const s32 mip_offset = 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. bool is_resolve = false;
  1052. if (src) {
  1053. is_resolve = src->info.num_samples > 1;
  1054. src_info.num_samples = src->info.num_samples;
  1055. src_info.size = src->info.size;
  1056. }
  1057. if (dst) {
  1058. dst_info.num_samples = dst->info.num_samples;
  1059. dst_info.size = dst->info.size;
  1060. }
  1061. ASSERT(!is_resolve || dst_info.format == src_info.format);
  1062. }
  1063. u32 MapSizeBytes(const ImageBase& image) {
  1064. if (True(image.flags & ImageFlagBits::AcceleratedUpload)) {
  1065. return image.guest_size_bytes;
  1066. } else if (True(image.flags & ImageFlagBits::Converted)) {
  1067. return image.converted_size_bytes;
  1068. } else {
  1069. return image.unswizzled_size_bytes;
  1070. }
  1071. }
  1072. static_assert(CalculateLevelSize(LevelInfo{{1920, 1080, 1}, {0, 2, 0}, {1, 1}, 2, 0}, 0) ==
  1073. 0x7f8000);
  1074. static_assert(CalculateLevelSize(LevelInfo{{32, 32, 1}, {0, 0, 4}, {1, 1}, 4, 0}, 0) == 0x4000);
  1075. static_assert(CalculateLevelOffset(PixelFormat::R8_SINT, {1920, 1080, 1}, {0, 2, 0}, 0, 7) ==
  1076. 0x2afc00);
  1077. static_assert(CalculateLevelOffset(PixelFormat::ASTC_2D_12X12_UNORM, {8192, 4096, 1}, {0, 2, 0}, 0,
  1078. 12) == 0x50d200);
  1079. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 0) ==
  1080. 0);
  1081. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 1) ==
  1082. 0x400000);
  1083. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 2) ==
  1084. 0x500000);
  1085. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 3) ==
  1086. 0x540000);
  1087. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 4) ==
  1088. 0x550000);
  1089. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 5) ==
  1090. 0x554000);
  1091. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 6) ==
  1092. 0x555000);
  1093. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 7) ==
  1094. 0x555400);
  1095. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 8) ==
  1096. 0x555600);
  1097. static_assert(CalculateLevelOffset(PixelFormat::A8B8G8R8_UNORM, {1024, 1024, 1}, {0, 4, 0}, 0, 9) ==
  1098. 0x555800);
  1099. constexpr u32 ValidateLayerSize(PixelFormat format, u32 width, u32 height, u32 block_height,
  1100. u32 tile_width_spacing, u32 level) {
  1101. const Extent3D size{width, height, 1};
  1102. const Extent3D block{0, block_height, 0};
  1103. const u32 offset = CalculateLevelOffset(format, size, block, tile_width_spacing, level);
  1104. return AlignLayerSize(offset, size, block, DefaultBlockHeight(format), tile_width_spacing);
  1105. }
  1106. static_assert(ValidateLayerSize(PixelFormat::ASTC_2D_12X12_UNORM, 8192, 4096, 2, 0, 12) ==
  1107. 0x50d800);
  1108. static_assert(ValidateLayerSize(PixelFormat::A8B8G8R8_UNORM, 1024, 1024, 2, 0, 10) == 0x556000);
  1109. static_assert(ValidateLayerSize(PixelFormat::BC3_UNORM, 128, 128, 2, 0, 8) == 0x6000);
  1110. static_assert(ValidateLayerSize(PixelFormat::A8B8G8R8_UNORM, 518, 572, 4, 3, 1) == 0x190000,
  1111. "Tile width spacing is not working");
  1112. static_assert(ValidateLayerSize(PixelFormat::BC5_UNORM, 1024, 1024, 3, 4, 11) == 0x160000,
  1113. "Compressed tile width spacing is not working");
  1114. } // namespace VideoCommon