TextureCompatibility.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Texture;
  4. using System;
  5. namespace Ryujinx.Graphics.Gpu.Image
  6. {
  7. /// <summary>
  8. /// Texture format compatibility checks.
  9. /// </summary>
  10. static class TextureCompatibility
  11. {
  12. private enum FormatClass
  13. {
  14. Unclassified,
  15. BCn64,
  16. BCn128,
  17. Bc1Rgb,
  18. Bc1Rgba,
  19. Bc2,
  20. Bc3,
  21. Bc4,
  22. Bc5,
  23. Bc6,
  24. Bc7
  25. }
  26. /// <summary>
  27. /// Checks if a format is host incompatible.
  28. /// </summary>
  29. /// <remarks>
  30. /// Host incompatible formats can't be used directly, the texture data needs to be converted
  31. /// to a compatible format first.
  32. /// </remarks>
  33. /// <param name="info">Texture information</param>
  34. /// <param name="caps">Host GPU capabilities</param>
  35. /// <returns>True if the format is incompatible, false otherwise</returns>
  36. public static bool IsFormatHostIncompatible(TextureInfo info, Capabilities caps)
  37. {
  38. Format originalFormat = info.FormatInfo.Format;
  39. return ToHostCompatibleFormat(info, caps).Format != originalFormat;
  40. }
  41. /// <summary>
  42. /// Converts a incompatible format to a host compatible format, or return the format directly
  43. /// if it is already host compatible.
  44. /// </summary>
  45. /// <remarks>
  46. /// This can be used to convert a incompatible compressed format to the decompressor
  47. /// output format.
  48. /// </remarks>
  49. /// <param name="info">Texture information</param>
  50. /// <param name="caps">Host GPU capabilities</param>
  51. /// <returns>A host compatible format</returns>
  52. public static FormatInfo ToHostCompatibleFormat(TextureInfo info, Capabilities caps)
  53. {
  54. if (!caps.SupportsAstcCompression)
  55. {
  56. if (info.FormatInfo.Format.IsAstcUnorm())
  57. {
  58. return new FormatInfo(Format.R8G8B8A8Unorm, 1, 1, 4, 4);
  59. }
  60. else if (info.FormatInfo.Format.IsAstcSrgb())
  61. {
  62. return new FormatInfo(Format.R8G8B8A8Srgb, 1, 1, 4, 4);
  63. }
  64. }
  65. if (info.Target == Target.Texture3D)
  66. {
  67. // The host API does not support 3D BC4/BC5 compressed formats.
  68. // We assume software decompression will be done for those textures,
  69. // and so we adjust the format here to match the decompressor output.
  70. switch (info.FormatInfo.Format)
  71. {
  72. case Format.Bc4Unorm:
  73. return new FormatInfo(Format.R8Unorm, 1, 1, 1, 1);
  74. case Format.Bc4Snorm:
  75. return new FormatInfo(Format.R8Snorm, 1, 1, 1, 1);
  76. case Format.Bc5Unorm:
  77. return new FormatInfo(Format.R8G8Unorm, 1, 1, 2, 2);
  78. case Format.Bc5Snorm:
  79. return new FormatInfo(Format.R8G8Snorm, 1, 1, 2, 2);
  80. }
  81. }
  82. return info.FormatInfo;
  83. }
  84. /// <summary>
  85. /// Checks if two formats are compatible, according to the host API copy format compatibility rules.
  86. /// </summary>
  87. /// <param name="lhs">First comparand</param>
  88. /// <param name="rhs">Second comparand</param>
  89. /// <returns>True if the formats are compatible, false otherwise</returns>
  90. public static bool FormatCompatible(FormatInfo lhs, FormatInfo rhs)
  91. {
  92. if (lhs.Format.IsDepthOrStencil() || rhs.Format.IsDepthOrStencil())
  93. {
  94. return lhs.Format == rhs.Format;
  95. }
  96. if (lhs.Format.IsAstc() || rhs.Format.IsAstc())
  97. {
  98. return lhs.Format == rhs.Format;
  99. }
  100. if (lhs.IsCompressed && rhs.IsCompressed)
  101. {
  102. FormatClass lhsClass = GetFormatClass(lhs.Format);
  103. FormatClass rhsClass = GetFormatClass(rhs.Format);
  104. return lhsClass == rhsClass;
  105. }
  106. else
  107. {
  108. return lhs.BytesPerPixel == rhs.BytesPerPixel;
  109. }
  110. }
  111. /// <summary>
  112. /// Checks if the texture format matches with the specified texture information.
  113. /// </summary>
  114. /// <param name="lhs">Texture information to compare</param>
  115. /// <param name="rhs">Texture information to compare with</param>
  116. /// <param name="forSampler">Indicates that the texture will be used for shader sampling</param>
  117. /// <param name="forCopy">Indicates that the texture will be used as copy source or target</param>
  118. /// <returns>True if the format matches, with the given comparison rules</returns>
  119. public static bool FormatMatches(TextureInfo lhs, TextureInfo rhs, bool forSampler, bool forCopy)
  120. {
  121. // D32F and R32F texture have the same representation internally,
  122. // however the R32F format is used to sample from depth textures.
  123. if (lhs.FormatInfo.Format == Format.D32Float && rhs.FormatInfo.Format == Format.R32Float && (forSampler || forCopy))
  124. {
  125. return true;
  126. }
  127. if (forCopy)
  128. {
  129. // The 2D engine does not support depth-stencil formats, so it will instead
  130. // use equivalent color formats. We must also consider them as compatible.
  131. if (lhs.FormatInfo.Format == Format.S8Uint && rhs.FormatInfo.Format == Format.R8Unorm)
  132. {
  133. return true;
  134. }
  135. if (lhs.FormatInfo.Format == Format.D16Unorm && rhs.FormatInfo.Format == Format.R16Unorm)
  136. {
  137. return true;
  138. }
  139. if ((lhs.FormatInfo.Format == Format.D24UnormS8Uint ||
  140. lhs.FormatInfo.Format == Format.D24X8Unorm) && rhs.FormatInfo.Format == Format.B8G8R8A8Unorm)
  141. {
  142. return true;
  143. }
  144. }
  145. return lhs.FormatInfo.Format == rhs.FormatInfo.Format;
  146. }
  147. /// <summary>
  148. /// Checks if the texture layout specified matches with this texture layout.
  149. /// The layout information is composed of the Stride for linear textures, or GOB block size
  150. /// for block linear textures.
  151. /// </summary>
  152. /// <param name="lhs">Texture information to compare</param>
  153. /// <param name="rhs">Texture information to compare with</param>
  154. /// <returns>True if the layout matches, false otherwise</returns>
  155. public static bool LayoutMatches(TextureInfo lhs, TextureInfo rhs)
  156. {
  157. if (lhs.IsLinear != rhs.IsLinear)
  158. {
  159. return false;
  160. }
  161. // For linear textures, gob block sizes are ignored.
  162. // For block linear textures, the stride is ignored.
  163. if (rhs.IsLinear)
  164. {
  165. return lhs.Stride == rhs.Stride;
  166. }
  167. else
  168. {
  169. return lhs.GobBlocksInY == rhs.GobBlocksInY &&
  170. lhs.GobBlocksInZ == rhs.GobBlocksInZ;
  171. }
  172. }
  173. /// <summary>
  174. /// Obtain the minimum compatibility level of two provided view compatibility results.
  175. /// </summary>
  176. /// <param name="first">The first compatibility level</param>
  177. /// <param name="second">The second compatibility level</param>
  178. /// <returns>The minimum compatibility level of two provided view compatibility results</returns>
  179. public static TextureViewCompatibility PropagateViewCompatibility(TextureViewCompatibility first, TextureViewCompatibility second)
  180. {
  181. if (first == TextureViewCompatibility.Incompatible || second == TextureViewCompatibility.Incompatible)
  182. {
  183. return TextureViewCompatibility.Incompatible;
  184. }
  185. else if (first == TextureViewCompatibility.CopyOnly || second == TextureViewCompatibility.CopyOnly)
  186. {
  187. return TextureViewCompatibility.CopyOnly;
  188. }
  189. else
  190. {
  191. return TextureViewCompatibility.Full;
  192. }
  193. }
  194. /// <summary>
  195. /// Checks if the sizes of two given textures are view compatible.
  196. /// </summary>
  197. /// <param name="lhs">Texture information of the texture view</param>
  198. /// <param name="rhs">Texture information of the texture view to match against</param>
  199. /// <param name="level">Mipmap level of the texture view in relation to this texture</param>
  200. /// <returns>True if the sizes are compatible, false otherwise</returns>
  201. public static TextureViewCompatibility ViewSizeMatches(TextureInfo lhs, TextureInfo rhs, int level)
  202. {
  203. Size size = GetAlignedSize(lhs, level);
  204. Size otherSize = GetAlignedSize(rhs);
  205. TextureViewCompatibility result = TextureViewCompatibility.Full;
  206. // For copies, we can copy a subset of the 3D texture slices,
  207. // so the depth may be different in this case.
  208. if (rhs.Target == Target.Texture3D && size.Depth != otherSize.Depth)
  209. {
  210. result = TextureViewCompatibility.CopyOnly;
  211. }
  212. return (size.Width == otherSize.Width &&
  213. size.Height == otherSize.Height) ? result : TextureViewCompatibility.Incompatible;
  214. }
  215. /// <summary>
  216. /// Checks if the texture sizes of the supplied texture informations match.
  217. /// </summary>
  218. /// <param name="lhs">Texture information to compare</param>
  219. /// <param name="rhs">Texture information to compare with</param>
  220. /// <returns>True if the size matches, false otherwise</returns>
  221. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs)
  222. {
  223. return SizeMatches(lhs, rhs, alignSizes: false);
  224. }
  225. /// <summary>
  226. /// Checks if the texture sizes of the supplied texture informations match the given level
  227. /// </summary>
  228. /// <param name="lhs">Texture information to compare</param>
  229. /// <param name="rhs">Texture information to compare with</param>
  230. /// <param name="level">Mipmap level of this texture to compare with</param>
  231. /// <returns>True if the size matches with the level, false otherwise</returns>
  232. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs, int level)
  233. {
  234. return Math.Max(1, lhs.Width >> level) == rhs.Width &&
  235. Math.Max(1, lhs.Height >> level) == rhs.Height &&
  236. Math.Max(1, lhs.GetDepth() >> level) == rhs.GetDepth();
  237. }
  238. /// <summary>
  239. /// Checks if the texture sizes of the supplied texture informations match.
  240. /// </summary>
  241. /// <param name="lhs">Texture information to compare</param>
  242. /// <param name="rhs">Texture information to compare with</param>
  243. /// <param name="alignSizes">True to align the sizes according to the texture layout for comparison</param>
  244. /// <returns>True if the sizes matches, false otherwise</returns>
  245. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs, bool alignSizes)
  246. {
  247. if (lhs.GetLayers() != rhs.GetLayers())
  248. {
  249. return false;
  250. }
  251. bool isTextureBuffer = lhs.Target == Target.TextureBuffer || rhs.Target == Target.TextureBuffer;
  252. if (alignSizes && !isTextureBuffer)
  253. {
  254. Size size0 = GetAlignedSize(lhs);
  255. Size size1 = GetAlignedSize(rhs);
  256. return size0.Width == size1.Width &&
  257. size0.Height == size1.Height &&
  258. size0.Depth == size1.Depth;
  259. }
  260. else
  261. {
  262. return lhs.Width == rhs.Width &&
  263. lhs.Height == rhs.Height &&
  264. lhs.GetDepth() == rhs.GetDepth();
  265. }
  266. }
  267. /// <summary>
  268. /// Gets the aligned sizes of the specified texture information.
  269. /// The alignment depends on the texture layout and format bytes per pixel.
  270. /// </summary>
  271. /// <param name="info">Texture information to calculate the aligned size from</param>
  272. /// <param name="level">Mipmap level for texture views</param>
  273. /// <returns>The aligned texture size</returns>
  274. public static Size GetAlignedSize(TextureInfo info, int level = 0)
  275. {
  276. int width = Math.Max(1, info.Width >> level);
  277. int height = Math.Max(1, info.Height >> level);
  278. if (info.IsLinear)
  279. {
  280. return SizeCalculator.GetLinearAlignedSize(
  281. width,
  282. height,
  283. info.FormatInfo.BlockWidth,
  284. info.FormatInfo.BlockHeight,
  285. info.FormatInfo.BytesPerPixel);
  286. }
  287. else
  288. {
  289. int depth = Math.Max(1, info.GetDepth() >> level);
  290. return SizeCalculator.GetBlockLinearAlignedSize(
  291. width,
  292. height,
  293. depth,
  294. info.FormatInfo.BlockWidth,
  295. info.FormatInfo.BlockHeight,
  296. info.FormatInfo.BytesPerPixel,
  297. info.GobBlocksInY,
  298. info.GobBlocksInZ,
  299. info.GobBlocksInTileX);
  300. }
  301. }
  302. /// <summary>
  303. /// Check if it's possible to create a view with the layout of the second texture information from the first.
  304. /// The layout information is composed of the Stride for linear textures, or GOB block size
  305. /// for block linear textures.
  306. /// </summary>
  307. /// <param name="lhs">Texture information of the texture view</param>
  308. /// <param name="rhs">Texture information of the texture view to compare against</param>
  309. /// <param name="level">Start level of the texture view, in relation with the first texture</param>
  310. /// <returns>True if the layout is compatible, false otherwise</returns>
  311. public static bool ViewLayoutCompatible(TextureInfo lhs, TextureInfo rhs, int level)
  312. {
  313. if (lhs.IsLinear != rhs.IsLinear)
  314. {
  315. return false;
  316. }
  317. // For linear textures, gob block sizes are ignored.
  318. // For block linear textures, the stride is ignored.
  319. if (rhs.IsLinear)
  320. {
  321. int width = Math.Max(1, lhs.Width >> level);
  322. int stride = width * lhs.FormatInfo.BytesPerPixel;
  323. stride = BitUtils.AlignUp(stride, 32);
  324. return stride == rhs.Stride;
  325. }
  326. else
  327. {
  328. int height = Math.Max(1, lhs.Height >> level);
  329. int depth = Math.Max(1, lhs.GetDepth() >> level);
  330. (int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  331. height,
  332. depth,
  333. lhs.FormatInfo.BlockHeight,
  334. lhs.GobBlocksInY,
  335. lhs.GobBlocksInZ);
  336. return gobBlocksInY == rhs.GobBlocksInY &&
  337. gobBlocksInZ == rhs.GobBlocksInZ;
  338. }
  339. }
  340. /// <summary>
  341. /// Checks if the view format of the first texture format is compatible with the format of the second.
  342. /// In general, the formats are considered compatible if the bytes per pixel values are equal,
  343. /// but there are more complex rules for some formats, like compressed or depth-stencil formats.
  344. /// This follows the host API copy compatibility rules.
  345. /// </summary>
  346. /// <param name="lhs">Texture information of the texture view</param>
  347. /// <param name="rhs">Texture information of the texture view</param>
  348. /// <returns>True if the formats are compatible, false otherwise</returns>
  349. public static bool ViewFormatCompatible(TextureInfo lhs, TextureInfo rhs)
  350. {
  351. return FormatCompatible(lhs.FormatInfo, rhs.FormatInfo);
  352. }
  353. /// <summary>
  354. /// Check if the target of the first texture view information is compatible with the target of the second texture view information.
  355. /// This follows the host API target compatibility rules.
  356. /// </summary>
  357. /// <param name="lhs">Texture information of the texture view</param
  358. /// <param name="rhs">Texture information of the texture view</param>
  359. /// <param name="isCopy">True to check for copy rather than view compatibility</param>
  360. /// <returns>True if the targets are compatible, false otherwise</returns>
  361. public static TextureViewCompatibility ViewTargetCompatible(TextureInfo lhs, TextureInfo rhs)
  362. {
  363. bool result = false;
  364. switch (lhs.Target)
  365. {
  366. case Target.Texture1D:
  367. case Target.Texture1DArray:
  368. result = rhs.Target == Target.Texture1D ||
  369. rhs.Target == Target.Texture1DArray;
  370. break;
  371. case Target.Texture2D:
  372. result = rhs.Target == Target.Texture2D ||
  373. rhs.Target == Target.Texture2DArray;
  374. break;
  375. case Target.Texture2DArray:
  376. case Target.Cubemap:
  377. case Target.CubemapArray:
  378. result = rhs.Target == Target.Texture2D ||
  379. rhs.Target == Target.Texture2DArray ||
  380. rhs.Target == Target.Cubemap ||
  381. rhs.Target == Target.CubemapArray;
  382. break;
  383. case Target.Texture2DMultisample:
  384. case Target.Texture2DMultisampleArray:
  385. result = rhs.Target == Target.Texture2DMultisample ||
  386. rhs.Target == Target.Texture2DMultisampleArray;
  387. break;
  388. case Target.Texture3D:
  389. if (rhs.Target == Target.Texture2D)
  390. {
  391. return TextureViewCompatibility.CopyOnly;
  392. }
  393. result = rhs.Target == Target.Texture3D;
  394. break;
  395. }
  396. return result ? TextureViewCompatibility.Full : TextureViewCompatibility.Incompatible;
  397. }
  398. /// <summary>
  399. /// Checks if a swizzle component in two textures functionally match, taking into account if the components are defined.
  400. /// </summary>
  401. /// <param name="lhs">Texture information to compare</param>
  402. /// <param name="rhs">Texture information to compare with</param>
  403. /// <param name="swizzleLhs">Swizzle component for the first texture</param>
  404. /// <param name="swizzleRhs">Swizzle component for the second texture</param>
  405. /// <param name="component">Component index, starting at 0 for red</param>
  406. /// <returns>True if the swizzle components functionally match, false othersize</returns>
  407. private static bool SwizzleComponentMatches(TextureInfo lhs, TextureInfo rhs, SwizzleComponent swizzleLhs, SwizzleComponent swizzleRhs, int component)
  408. {
  409. int lhsComponents = lhs.FormatInfo.Components;
  410. int rhsComponents = rhs.FormatInfo.Components;
  411. if (lhsComponents == 4 && rhsComponents == 4)
  412. {
  413. return swizzleLhs == swizzleRhs;
  414. }
  415. // Swizzles after the number of components a format defines are "undefined".
  416. // We allow these to not be equal under certain circumstances.
  417. // This can only happen when there are less than 4 components in a format.
  418. // It tends to happen when float depth textures are sampled.
  419. bool lhsDefined = (swizzleLhs - SwizzleComponent.Red) < lhsComponents;
  420. bool rhsDefined = (swizzleRhs - SwizzleComponent.Red) < rhsComponents;
  421. if (lhsDefined == rhsDefined)
  422. {
  423. // If both are undefined, return true. Otherwise just check if they're equal.
  424. return lhsDefined ? swizzleLhs == swizzleRhs : true;
  425. }
  426. else
  427. {
  428. SwizzleComponent defined = lhsDefined ? swizzleLhs : swizzleRhs;
  429. SwizzleComponent undefined = lhsDefined ? swizzleRhs : swizzleLhs;
  430. // Undefined swizzle can be matched by a forced value (0, 1), exact equality, or expected value.
  431. // For example, R___ matches R001, RGBA but not RBGA.
  432. return defined == undefined || defined < SwizzleComponent.Red || defined == SwizzleComponent.Red + component;
  433. }
  434. }
  435. /// <summary>
  436. /// Checks if the texture shader sampling parameters of two texture informations match.
  437. /// </summary>
  438. /// <param name="lhs">Texture information to compare</param>
  439. /// <param name="rhs">Texture information to compare with</param>
  440. /// <returns>True if the texture shader sampling parameters matches, false otherwise</returns>
  441. public static bool SamplerParamsMatches(TextureInfo lhs, TextureInfo rhs)
  442. {
  443. return lhs.DepthStencilMode == rhs.DepthStencilMode &&
  444. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleR, rhs.SwizzleR, 0) &&
  445. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleG, rhs.SwizzleG, 1) &&
  446. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleB, rhs.SwizzleB, 2) &&
  447. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleA, rhs.SwizzleA, 3);
  448. }
  449. /// <summary>
  450. /// Check if the texture target and samples count (for multisampled textures) matches.
  451. /// </summary>
  452. /// <param name="first">Texture information to compare with</param>
  453. /// <param name="rhs">Texture information to compare with</param>
  454. /// <returns>True if the texture target and samples count matches, false otherwise</returns>
  455. public static bool TargetAndSamplesCompatible(TextureInfo lhs, TextureInfo rhs)
  456. {
  457. return lhs.Target == rhs.Target &&
  458. lhs.SamplesInX == rhs.SamplesInX &&
  459. lhs.SamplesInY == rhs.SamplesInY;
  460. }
  461. /// <summary>
  462. /// Gets the texture format class, for compressed textures, or Unclassified otherwise.
  463. /// </summary>
  464. /// <param name="format">The format</param>
  465. /// <returns>Format class</returns>
  466. private static FormatClass GetFormatClass(Format format)
  467. {
  468. switch (format)
  469. {
  470. case Format.Bc1RgbSrgb:
  471. case Format.Bc1RgbUnorm:
  472. return FormatClass.Bc1Rgb;
  473. case Format.Bc1RgbaSrgb:
  474. case Format.Bc1RgbaUnorm:
  475. return FormatClass.Bc1Rgba;
  476. case Format.Bc2Srgb:
  477. case Format.Bc2Unorm:
  478. return FormatClass.Bc2;
  479. case Format.Bc3Srgb:
  480. case Format.Bc3Unorm:
  481. return FormatClass.Bc3;
  482. case Format.Bc4Snorm:
  483. case Format.Bc4Unorm:
  484. return FormatClass.Bc4;
  485. case Format.Bc5Snorm:
  486. case Format.Bc5Unorm:
  487. return FormatClass.Bc5;
  488. case Format.Bc6HSfloat:
  489. case Format.Bc6HUfloat:
  490. return FormatClass.Bc6;
  491. case Format.Bc7Srgb:
  492. case Format.Bc7Unorm:
  493. return FormatClass.Bc7;
  494. }
  495. return FormatClass.Unclassified;
  496. }
  497. }
  498. }