util.cpp 56 KB

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