TextureCompatibility.cs 23 KB

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