TextureCompatibility.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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>A value indicating how well the formats match</returns>
  119. public static TextureMatchQuality 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 TextureMatchQuality.FormatAlias;
  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 TextureMatchQuality.FormatAlias;
  134. }
  135. if (lhs.FormatInfo.Format == Format.D16Unorm && rhs.FormatInfo.Format == Format.R16Unorm)
  136. {
  137. return TextureMatchQuality.FormatAlias;
  138. }
  139. if ((lhs.FormatInfo.Format == Format.D24UnormS8Uint ||
  140. lhs.FormatInfo.Format == Format.D24X8Unorm) && rhs.FormatInfo.Format == Format.B8G8R8A8Unorm)
  141. {
  142. return TextureMatchQuality.FormatAlias;
  143. }
  144. }
  145. return lhs.FormatInfo.Format == rhs.FormatInfo.Format ? TextureMatchQuality.Perfect : TextureMatchQuality.NoMatch;
  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>The view compatibility level of the view sizes</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. if (size.Width == otherSize.Width && size.Height == otherSize.Height)
  213. {
  214. if (level > 0 && result == TextureViewCompatibility.Full)
  215. {
  216. // A resize should not change the aligned size of the largest mip.
  217. // If it would, then create a copy dependency rather than a full view.
  218. Size mip0SizeLhs = GetAlignedSize(lhs);
  219. Size mip0SizeRhs = GetLargestAlignedSize(rhs, level);
  220. if (mip0SizeLhs.Width != mip0SizeRhs.Width || mip0SizeLhs.Height != mip0SizeRhs.Height)
  221. {
  222. result = TextureViewCompatibility.CopyOnly;
  223. }
  224. }
  225. return result;
  226. }
  227. else if (lhs.IsLinear && rhs.IsLinear)
  228. {
  229. // Copy between linear textures with matching stride.
  230. int stride = BitUtils.AlignUp(Math.Max(1, lhs.Stride >> level), Constants.StrideAlignment);
  231. return stride == rhs.Stride ? TextureViewCompatibility.CopyOnly : TextureViewCompatibility.Incompatible;
  232. }
  233. else
  234. {
  235. return TextureViewCompatibility.Incompatible;
  236. }
  237. }
  238. /// <summary>
  239. /// Checks if the potential child texture fits within the level and layer bounds of the parent.
  240. /// </summary>
  241. /// <param name="parent">Texture information for the parent</param>
  242. /// <param name="child">Texture information for the child</param>
  243. /// <param name="layer">Base layer of the child texture</param>
  244. /// <param name="level">Base level of the child texture</param>
  245. /// <returns>Full compatiblity if the child's layer and level count fit within the parent, incompatible otherwise</returns>
  246. public static TextureViewCompatibility ViewSubImagesInBounds(TextureInfo parent, TextureInfo child, int layer, int level)
  247. {
  248. if (level + child.Levels <= parent.Levels &&
  249. layer + child.GetSlices() <= parent.GetSlices())
  250. {
  251. return TextureViewCompatibility.Full;
  252. }
  253. else
  254. {
  255. return TextureViewCompatibility.Incompatible;
  256. }
  257. }
  258. /// <summary>
  259. /// Checks if the texture sizes of the supplied texture informations match.
  260. /// </summary>
  261. /// <param name="lhs">Texture information to compare</param>
  262. /// <param name="rhs">Texture information to compare with</param>
  263. /// <returns>True if the size matches, false otherwise</returns>
  264. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs)
  265. {
  266. return SizeMatches(lhs, rhs, alignSizes: false);
  267. }
  268. /// <summary>
  269. /// Checks if the texture sizes of the supplied texture informations match the given level
  270. /// </summary>
  271. /// <param name="lhs">Texture information to compare</param>
  272. /// <param name="rhs">Texture information to compare with</param>
  273. /// <param name="level">Mipmap level of this texture to compare with</param>
  274. /// <returns>True if the size matches with the level, false otherwise</returns>
  275. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs, int level)
  276. {
  277. return Math.Max(1, lhs.Width >> level) == rhs.Width &&
  278. Math.Max(1, lhs.Height >> level) == rhs.Height &&
  279. Math.Max(1, lhs.GetDepth() >> level) == rhs.GetDepth();
  280. }
  281. /// <summary>
  282. /// Checks if the texture sizes of the supplied texture informations match.
  283. /// </summary>
  284. /// <param name="lhs">Texture information to compare</param>
  285. /// <param name="rhs">Texture information to compare with</param>
  286. /// <param name="alignSizes">True to align the sizes according to the texture layout for comparison</param>
  287. /// <param name="lhsLevel">Mip level of the lhs texture. Aligned sizes are compared for the largest mip</param>
  288. /// <returns>True if the sizes matches, false otherwise</returns>
  289. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs, bool alignSizes, int lhsLevel = 0)
  290. {
  291. if (lhs.GetLayers() != rhs.GetLayers())
  292. {
  293. return false;
  294. }
  295. bool isTextureBuffer = lhs.Target == Target.TextureBuffer || rhs.Target == Target.TextureBuffer;
  296. if (alignSizes && !isTextureBuffer)
  297. {
  298. Size size0 = GetLargestAlignedSize(lhs, lhsLevel);
  299. Size size1 = GetLargestAlignedSize(rhs, lhsLevel);
  300. return size0.Width == size1.Width &&
  301. size0.Height == size1.Height &&
  302. size0.Depth == size1.Depth;
  303. }
  304. else
  305. {
  306. return lhs.Width == rhs.Width &&
  307. lhs.Height == rhs.Height &&
  308. lhs.GetDepth() == rhs.GetDepth();
  309. }
  310. }
  311. /// <summary>
  312. /// Gets the aligned sizes for the given dimensions, using the specified texture information.
  313. /// The alignment depends on the texture layout and format bytes per pixel.
  314. /// </summary>
  315. /// <param name="info">Texture information to calculate the aligned size from</param>
  316. /// <param name="width">The width to be aligned</param>
  317. /// <param name="height">The height to be aligned</param>
  318. /// <param name="depth">The depth to be aligned</param>
  319. /// <returns>The aligned texture size</returns>
  320. private static Size GetAlignedSize(TextureInfo info, int width, int height, int depth)
  321. {
  322. if (info.IsLinear)
  323. {
  324. return SizeCalculator.GetLinearAlignedSize(
  325. width,
  326. height,
  327. info.FormatInfo.BlockWidth,
  328. info.FormatInfo.BlockHeight,
  329. info.FormatInfo.BytesPerPixel);
  330. }
  331. else
  332. {
  333. return SizeCalculator.GetBlockLinearAlignedSize(
  334. width,
  335. height,
  336. depth,
  337. info.FormatInfo.BlockWidth,
  338. info.FormatInfo.BlockHeight,
  339. info.FormatInfo.BytesPerPixel,
  340. info.GobBlocksInY,
  341. info.GobBlocksInZ,
  342. info.GobBlocksInTileX);
  343. }
  344. }
  345. /// <summary>
  346. /// Gets the aligned sizes of the specified texture information, shifted to the largest mip from a given level.
  347. /// The alignment depends on the texture layout and format bytes per pixel.
  348. /// </summary>
  349. /// <param name="info">Texture information to calculate the aligned size from</param>
  350. /// <param name="level">Mipmap level for texture views. Shifts the aligned size to represent the largest mip level</param>
  351. /// <returns>The aligned texture size of the largest mip level</returns>
  352. public static Size GetLargestAlignedSize(TextureInfo info, int level)
  353. {
  354. int width = info.Width << level;
  355. int height = info.Height << level;
  356. int depth = info.GetDepth() << level;
  357. return GetAlignedSize(info, width, height, depth);
  358. }
  359. /// <summary>
  360. /// Gets the aligned sizes of the specified texture information.
  361. /// The alignment depends on the texture layout and format bytes per pixel.
  362. /// </summary>
  363. /// <param name="info">Texture information to calculate the aligned size from</param>
  364. /// <param name="level">Mipmap level for texture views</param>
  365. /// <returns>The aligned texture size</returns>
  366. public static Size GetAlignedSize(TextureInfo info, int level = 0)
  367. {
  368. int width = Math.Max(1, info.Width >> level);
  369. int height = Math.Max(1, info.Height >> level);
  370. int depth = Math.Max(1, info.GetDepth() >> level);
  371. return GetAlignedSize(info, width, height, depth);
  372. }
  373. /// <summary>
  374. /// Check if it's possible to create a view with the layout of the second texture information from the first.
  375. /// The layout information is composed of the Stride for linear textures, or GOB block size
  376. /// for block linear textures.
  377. /// </summary>
  378. /// <param name="lhs">Texture information of the texture view</param>
  379. /// <param name="rhs">Texture information of the texture view to compare against</param>
  380. /// <param name="level">Start level of the texture view, in relation with the first texture</param>
  381. /// <returns>True if the layout is compatible, false otherwise</returns>
  382. public static bool ViewLayoutCompatible(TextureInfo lhs, TextureInfo rhs, int level)
  383. {
  384. if (lhs.IsLinear != rhs.IsLinear)
  385. {
  386. return false;
  387. }
  388. // For linear textures, gob block sizes are ignored.
  389. // For block linear textures, the stride is ignored.
  390. if (rhs.IsLinear)
  391. {
  392. int stride = Math.Max(1, lhs.Stride >> level);
  393. stride = BitUtils.AlignUp(stride, 32);
  394. return stride == rhs.Stride;
  395. }
  396. else
  397. {
  398. int height = Math.Max(1, lhs.Height >> level);
  399. int depth = Math.Max(1, lhs.GetDepth() >> level);
  400. (int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  401. height,
  402. depth,
  403. lhs.FormatInfo.BlockHeight,
  404. lhs.GobBlocksInY,
  405. lhs.GobBlocksInZ);
  406. return gobBlocksInY == rhs.GobBlocksInY &&
  407. gobBlocksInZ == rhs.GobBlocksInZ;
  408. }
  409. }
  410. /// <summary>
  411. /// Checks if the view format of the first texture format is compatible with the format of the second.
  412. /// In general, the formats are considered compatible if the bytes per pixel values are equal,
  413. /// but there are more complex rules for some formats, like compressed or depth-stencil formats.
  414. /// This follows the host API copy compatibility rules.
  415. /// </summary>
  416. /// <param name="lhs">Texture information of the texture view</param>
  417. /// <param name="rhs">Texture information of the texture view</param>
  418. /// <returns>The view compatibility level of the texture formats</returns>
  419. public static TextureViewCompatibility ViewFormatCompatible(TextureInfo lhs, TextureInfo rhs)
  420. {
  421. if (FormatCompatible(lhs.FormatInfo, rhs.FormatInfo))
  422. {
  423. if (lhs.FormatInfo.IsCompressed != rhs.FormatInfo.IsCompressed)
  424. {
  425. return TextureViewCompatibility.CopyOnly;
  426. }
  427. else
  428. {
  429. return TextureViewCompatibility.Full;
  430. }
  431. }
  432. return TextureViewCompatibility.Incompatible;
  433. }
  434. /// <summary>
  435. /// Check if the target of the first texture view information is compatible with the target of the second texture view information.
  436. /// This follows the host API target compatibility rules.
  437. /// </summary>
  438. /// <param name="lhs">Texture information of the texture view</param
  439. /// <param name="rhs">Texture information of the texture view</param>
  440. /// <param name="isCopy">True to check for copy rather than view compatibility</param>
  441. /// <returns>True if the targets are compatible, false otherwise</returns>
  442. public static TextureViewCompatibility ViewTargetCompatible(TextureInfo lhs, TextureInfo rhs)
  443. {
  444. bool result = false;
  445. switch (lhs.Target)
  446. {
  447. case Target.Texture1D:
  448. case Target.Texture1DArray:
  449. result = rhs.Target == Target.Texture1D ||
  450. rhs.Target == Target.Texture1DArray;
  451. break;
  452. case Target.Texture2D:
  453. result = rhs.Target == Target.Texture2D ||
  454. rhs.Target == Target.Texture2DArray;
  455. break;
  456. case Target.Texture2DArray:
  457. case Target.Cubemap:
  458. case Target.CubemapArray:
  459. result = rhs.Target == Target.Texture2D ||
  460. rhs.Target == Target.Texture2DArray ||
  461. rhs.Target == Target.Cubemap ||
  462. rhs.Target == Target.CubemapArray;
  463. break;
  464. case Target.Texture2DMultisample:
  465. case Target.Texture2DMultisampleArray:
  466. result = rhs.Target == Target.Texture2DMultisample ||
  467. rhs.Target == Target.Texture2DMultisampleArray;
  468. break;
  469. case Target.Texture3D:
  470. if (rhs.Target == Target.Texture2D)
  471. {
  472. return TextureViewCompatibility.CopyOnly;
  473. }
  474. result = rhs.Target == Target.Texture3D;
  475. break;
  476. }
  477. return result ? TextureViewCompatibility.Full : TextureViewCompatibility.Incompatible;
  478. }
  479. /// <summary>
  480. /// Checks if a swizzle component in two textures functionally match, taking into account if the components are defined.
  481. /// </summary>
  482. /// <param name="lhs">Texture information to compare</param>
  483. /// <param name="rhs">Texture information to compare with</param>
  484. /// <param name="swizzleLhs">Swizzle component for the first texture</param>
  485. /// <param name="swizzleRhs">Swizzle component for the second texture</param>
  486. /// <param name="component">Component index, starting at 0 for red</param>
  487. /// <returns>True if the swizzle components functionally match, false othersize</returns>
  488. private static bool SwizzleComponentMatches(TextureInfo lhs, TextureInfo rhs, SwizzleComponent swizzleLhs, SwizzleComponent swizzleRhs, int component)
  489. {
  490. int lhsComponents = lhs.FormatInfo.Components;
  491. int rhsComponents = rhs.FormatInfo.Components;
  492. if (lhsComponents == 4 && rhsComponents == 4)
  493. {
  494. return swizzleLhs == swizzleRhs;
  495. }
  496. // Swizzles after the number of components a format defines are "undefined".
  497. // We allow these to not be equal under certain circumstances.
  498. // This can only happen when there are less than 4 components in a format.
  499. // It tends to happen when float depth textures are sampled.
  500. bool lhsDefined = (swizzleLhs - SwizzleComponent.Red) < lhsComponents;
  501. bool rhsDefined = (swizzleRhs - SwizzleComponent.Red) < rhsComponents;
  502. if (lhsDefined == rhsDefined)
  503. {
  504. // If both are undefined, return true. Otherwise just check if they're equal.
  505. return lhsDefined ? swizzleLhs == swizzleRhs : true;
  506. }
  507. else
  508. {
  509. SwizzleComponent defined = lhsDefined ? swizzleLhs : swizzleRhs;
  510. SwizzleComponent undefined = lhsDefined ? swizzleRhs : swizzleLhs;
  511. // Undefined swizzle can be matched by a forced value (0, 1), exact equality, or expected value.
  512. // For example, R___ matches R001, RGBA but not RBGA.
  513. return defined == undefined || defined < SwizzleComponent.Red || defined == SwizzleComponent.Red + component;
  514. }
  515. }
  516. /// <summary>
  517. /// Checks if the texture shader sampling parameters of two texture informations match.
  518. /// </summary>
  519. /// <param name="lhs">Texture information to compare</param>
  520. /// <param name="rhs">Texture information to compare with</param>
  521. /// <returns>True if the texture shader sampling parameters matches, false otherwise</returns>
  522. public static bool SamplerParamsMatches(TextureInfo lhs, TextureInfo rhs)
  523. {
  524. return lhs.DepthStencilMode == rhs.DepthStencilMode &&
  525. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleR, rhs.SwizzleR, 0) &&
  526. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleG, rhs.SwizzleG, 1) &&
  527. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleB, rhs.SwizzleB, 2) &&
  528. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleA, rhs.SwizzleA, 3);
  529. }
  530. /// <summary>
  531. /// Check if the texture target and samples count (for multisampled textures) matches.
  532. /// </summary>
  533. /// <param name="first">Texture information to compare with</param>
  534. /// <param name="rhs">Texture information to compare with</param>
  535. /// <returns>True if the texture target and samples count matches, false otherwise</returns>
  536. public static bool TargetAndSamplesCompatible(TextureInfo lhs, TextureInfo rhs)
  537. {
  538. return lhs.Target == rhs.Target &&
  539. lhs.SamplesInX == rhs.SamplesInX &&
  540. lhs.SamplesInY == rhs.SamplesInY;
  541. }
  542. /// <summary>
  543. /// Gets the texture format class, for compressed textures, or Unclassified otherwise.
  544. /// </summary>
  545. /// <param name="format">The format</param>
  546. /// <returns>Format class</returns>
  547. private static FormatClass GetFormatClass(Format format)
  548. {
  549. switch (format)
  550. {
  551. case Format.Bc1RgbSrgb:
  552. case Format.Bc1RgbUnorm:
  553. return FormatClass.Bc1Rgb;
  554. case Format.Bc1RgbaSrgb:
  555. case Format.Bc1RgbaUnorm:
  556. return FormatClass.Bc1Rgba;
  557. case Format.Bc2Srgb:
  558. case Format.Bc2Unorm:
  559. return FormatClass.Bc2;
  560. case Format.Bc3Srgb:
  561. case Format.Bc3Unorm:
  562. return FormatClass.Bc3;
  563. case Format.Bc4Snorm:
  564. case Format.Bc4Unorm:
  565. return FormatClass.Bc4;
  566. case Format.Bc5Snorm:
  567. case Format.Bc5Unorm:
  568. return FormatClass.Bc5;
  569. case Format.Bc6HSfloat:
  570. case Format.Bc6HUfloat:
  571. return FormatClass.Bc6;
  572. case Format.Bc7Srgb:
  573. case Format.Bc7Unorm:
  574. return FormatClass.Bc7;
  575. }
  576. return FormatClass.Unclassified;
  577. }
  578. }
  579. }