TextureCompatibility.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 (!caps.SupportsR4G4Format && info.FormatInfo.Format == Format.R4G4Unorm)
  66. {
  67. return new FormatInfo(Format.R4G4B4A4Unorm, 1, 1, 2, 4);
  68. }
  69. if (info.Target == Target.Texture3D)
  70. {
  71. // The host API does not support 3D BC4/BC5 compressed formats.
  72. // We assume software decompression will be done for those textures,
  73. // and so we adjust the format here to match the decompressor output.
  74. switch (info.FormatInfo.Format)
  75. {
  76. case Format.Bc4Unorm:
  77. return new FormatInfo(Format.R8Unorm, 1, 1, 1, 1);
  78. case Format.Bc4Snorm:
  79. return new FormatInfo(Format.R8Snorm, 1, 1, 1, 1);
  80. case Format.Bc5Unorm:
  81. return new FormatInfo(Format.R8G8Unorm, 1, 1, 2, 2);
  82. case Format.Bc5Snorm:
  83. return new FormatInfo(Format.R8G8Snorm, 1, 1, 2, 2);
  84. }
  85. }
  86. return info.FormatInfo;
  87. }
  88. /// <summary>
  89. /// Checks if two formats are compatible, according to the host API copy format compatibility rules.
  90. /// </summary>
  91. /// <param name="lhs">First comparand</param>
  92. /// <param name="rhs">Second comparand</param>
  93. /// <returns>True if the formats are compatible, false otherwise</returns>
  94. public static bool FormatCompatible(FormatInfo lhs, FormatInfo rhs)
  95. {
  96. if (lhs.Format.IsDepthOrStencil() || rhs.Format.IsDepthOrStencil())
  97. {
  98. return lhs.Format == rhs.Format;
  99. }
  100. if (lhs.Format.IsAstc() || rhs.Format.IsAstc())
  101. {
  102. return lhs.Format == rhs.Format;
  103. }
  104. if (lhs.IsCompressed && rhs.IsCompressed)
  105. {
  106. FormatClass lhsClass = GetFormatClass(lhs.Format);
  107. FormatClass rhsClass = GetFormatClass(rhs.Format);
  108. return lhsClass == rhsClass;
  109. }
  110. else
  111. {
  112. return lhs.BytesPerPixel == rhs.BytesPerPixel;
  113. }
  114. }
  115. /// <summary>
  116. /// Checks if the texture format matches with the specified texture information.
  117. /// </summary>
  118. /// <param name="lhs">Texture information to compare</param>
  119. /// <param name="rhs">Texture information to compare with</param>
  120. /// <param name="forSampler">Indicates that the texture will be used for shader sampling</param>
  121. /// <param name="forCopy">Indicates that the texture will be used as copy source or target</param>
  122. /// <returns>A value indicating how well the formats match</returns>
  123. public static TextureMatchQuality FormatMatches(TextureInfo lhs, TextureInfo rhs, bool forSampler, bool forCopy)
  124. {
  125. // D32F and R32F texture have the same representation internally,
  126. // however the R32F format is used to sample from depth textures.
  127. if (lhs.FormatInfo.Format == Format.D32Float && rhs.FormatInfo.Format == Format.R32Float && (forSampler || forCopy))
  128. {
  129. return TextureMatchQuality.FormatAlias;
  130. }
  131. if (forCopy)
  132. {
  133. // The 2D engine does not support depth-stencil formats, so it will instead
  134. // use equivalent color formats. We must also consider them as compatible.
  135. if (lhs.FormatInfo.Format == Format.S8Uint && rhs.FormatInfo.Format == Format.R8Unorm)
  136. {
  137. return TextureMatchQuality.FormatAlias;
  138. }
  139. if (lhs.FormatInfo.Format == Format.D16Unorm && rhs.FormatInfo.Format == Format.R16Unorm)
  140. {
  141. return TextureMatchQuality.FormatAlias;
  142. }
  143. if ((lhs.FormatInfo.Format == Format.D24UnormS8Uint ||
  144. lhs.FormatInfo.Format == Format.D24X8Unorm) && rhs.FormatInfo.Format == Format.B8G8R8A8Unorm)
  145. {
  146. return TextureMatchQuality.FormatAlias;
  147. }
  148. }
  149. return lhs.FormatInfo.Format == rhs.FormatInfo.Format ? TextureMatchQuality.Perfect : TextureMatchQuality.NoMatch;
  150. }
  151. /// <summary>
  152. /// Checks if the texture layout specified matches with this texture layout.
  153. /// The layout information is composed of the Stride for linear textures, or GOB block size
  154. /// for block linear textures.
  155. /// </summary>
  156. /// <param name="lhs">Texture information to compare</param>
  157. /// <param name="rhs">Texture information to compare with</param>
  158. /// <returns>True if the layout matches, false otherwise</returns>
  159. public static bool LayoutMatches(TextureInfo lhs, TextureInfo rhs)
  160. {
  161. if (lhs.IsLinear != rhs.IsLinear)
  162. {
  163. return false;
  164. }
  165. // For linear textures, gob block sizes are ignored.
  166. // For block linear textures, the stride is ignored.
  167. if (rhs.IsLinear)
  168. {
  169. return lhs.Stride == rhs.Stride;
  170. }
  171. else
  172. {
  173. return lhs.GobBlocksInY == rhs.GobBlocksInY &&
  174. lhs.GobBlocksInZ == rhs.GobBlocksInZ;
  175. }
  176. }
  177. /// <summary>
  178. /// Obtain the minimum compatibility level of two provided view compatibility results.
  179. /// </summary>
  180. /// <param name="first">The first compatibility level</param>
  181. /// <param name="second">The second compatibility level</param>
  182. /// <returns>The minimum compatibility level of two provided view compatibility results</returns>
  183. public static TextureViewCompatibility PropagateViewCompatibility(TextureViewCompatibility first, TextureViewCompatibility second)
  184. {
  185. if (first == TextureViewCompatibility.Incompatible || second == TextureViewCompatibility.Incompatible)
  186. {
  187. return TextureViewCompatibility.Incompatible;
  188. }
  189. else if (first == TextureViewCompatibility.CopyOnly || second == TextureViewCompatibility.CopyOnly)
  190. {
  191. return TextureViewCompatibility.CopyOnly;
  192. }
  193. else
  194. {
  195. return TextureViewCompatibility.Full;
  196. }
  197. }
  198. /// <summary>
  199. /// Checks if the sizes of two given textures are view compatible.
  200. /// </summary>
  201. /// <param name="lhs">Texture information of the texture view</param>
  202. /// <param name="rhs">Texture information of the texture view to match against</param>
  203. /// <param name="level">Mipmap level of the texture view in relation to this texture</param>
  204. /// <returns>The view compatibility level of the view sizes</returns>
  205. public static TextureViewCompatibility ViewSizeMatches(TextureInfo lhs, TextureInfo rhs, int level)
  206. {
  207. Size size = GetAlignedSize(lhs, level);
  208. Size otherSize = GetAlignedSize(rhs);
  209. TextureViewCompatibility result = TextureViewCompatibility.Full;
  210. // For copies, we can copy a subset of the 3D texture slices,
  211. // so the depth may be different in this case.
  212. if (rhs.Target == Target.Texture3D && size.Depth != otherSize.Depth)
  213. {
  214. result = TextureViewCompatibility.CopyOnly;
  215. }
  216. if (size.Width == otherSize.Width && size.Height == otherSize.Height)
  217. {
  218. if (level > 0 && result == TextureViewCompatibility.Full)
  219. {
  220. // A resize should not change the aligned size of the largest mip.
  221. // If it would, then create a copy dependency rather than a full view.
  222. Size mip0SizeLhs = GetAlignedSize(lhs);
  223. Size mip0SizeRhs = GetLargestAlignedSize(rhs, level);
  224. if (mip0SizeLhs.Width != mip0SizeRhs.Width || mip0SizeLhs.Height != mip0SizeRhs.Height)
  225. {
  226. result = TextureViewCompatibility.CopyOnly;
  227. }
  228. }
  229. return result;
  230. }
  231. else if (lhs.IsLinear && rhs.IsLinear)
  232. {
  233. // Copy between linear textures with matching stride.
  234. int stride = BitUtils.AlignUp(Math.Max(1, lhs.Stride >> level), Constants.StrideAlignment);
  235. return stride == rhs.Stride ? TextureViewCompatibility.CopyOnly : TextureViewCompatibility.Incompatible;
  236. }
  237. else
  238. {
  239. return TextureViewCompatibility.Incompatible;
  240. }
  241. }
  242. /// <summary>
  243. /// Checks if the potential child texture fits within the level and layer bounds of the parent.
  244. /// </summary>
  245. /// <param name="parent">Texture information for the parent</param>
  246. /// <param name="child">Texture information for the child</param>
  247. /// <param name="layer">Base layer of the child texture</param>
  248. /// <param name="level">Base level of the child texture</param>
  249. /// <returns>Full compatiblity if the child's layer and level count fit within the parent, incompatible otherwise</returns>
  250. public static TextureViewCompatibility ViewSubImagesInBounds(TextureInfo parent, TextureInfo child, int layer, int level)
  251. {
  252. if (level + child.Levels <= parent.Levels &&
  253. layer + child.GetSlices() <= parent.GetSlices())
  254. {
  255. return TextureViewCompatibility.Full;
  256. }
  257. else
  258. {
  259. return TextureViewCompatibility.Incompatible;
  260. }
  261. }
  262. /// <summary>
  263. /// Checks if the texture sizes of the supplied texture informations match.
  264. /// </summary>
  265. /// <param name="lhs">Texture information to compare</param>
  266. /// <param name="rhs">Texture information to compare with</param>
  267. /// <returns>True if the size matches, false otherwise</returns>
  268. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs)
  269. {
  270. return SizeMatches(lhs, rhs, alignSizes: false);
  271. }
  272. /// <summary>
  273. /// Checks if the texture sizes of the supplied texture informations match the given level
  274. /// </summary>
  275. /// <param name="lhs">Texture information to compare</param>
  276. /// <param name="rhs">Texture information to compare with</param>
  277. /// <param name="level">Mipmap level of this texture to compare with</param>
  278. /// <returns>True if the size matches with the level, false otherwise</returns>
  279. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs, int level)
  280. {
  281. return Math.Max(1, lhs.Width >> level) == rhs.Width &&
  282. Math.Max(1, lhs.Height >> level) == rhs.Height &&
  283. Math.Max(1, lhs.GetDepth() >> level) == rhs.GetDepth();
  284. }
  285. /// <summary>
  286. /// Checks if the texture sizes of the supplied texture informations match.
  287. /// </summary>
  288. /// <param name="lhs">Texture information to compare</param>
  289. /// <param name="rhs">Texture information to compare with</param>
  290. /// <param name="alignSizes">True to align the sizes according to the texture layout for comparison</param>
  291. /// <param name="lhsLevel">Mip level of the lhs texture. Aligned sizes are compared for the largest mip</param>
  292. /// <returns>True if the sizes matches, false otherwise</returns>
  293. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs, bool alignSizes, int lhsLevel = 0)
  294. {
  295. if (lhs.GetLayers() != rhs.GetLayers())
  296. {
  297. return false;
  298. }
  299. bool isTextureBuffer = lhs.Target == Target.TextureBuffer || rhs.Target == Target.TextureBuffer;
  300. if (alignSizes && !isTextureBuffer)
  301. {
  302. Size size0 = GetLargestAlignedSize(lhs, lhsLevel);
  303. Size size1 = GetLargestAlignedSize(rhs, lhsLevel);
  304. return size0.Width == size1.Width &&
  305. size0.Height == size1.Height &&
  306. size0.Depth == size1.Depth;
  307. }
  308. else
  309. {
  310. return lhs.Width == rhs.Width &&
  311. lhs.Height == rhs.Height &&
  312. lhs.GetDepth() == rhs.GetDepth();
  313. }
  314. }
  315. /// <summary>
  316. /// Gets the aligned sizes for the given dimensions, using the specified texture information.
  317. /// The alignment depends on the texture layout and format bytes per pixel.
  318. /// </summary>
  319. /// <param name="info">Texture information to calculate the aligned size from</param>
  320. /// <param name="width">The width to be aligned</param>
  321. /// <param name="height">The height to be aligned</param>
  322. /// <param name="depth">The depth to be aligned</param>
  323. /// <returns>The aligned texture size</returns>
  324. private static Size GetAlignedSize(TextureInfo info, int width, int height, int depth)
  325. {
  326. if (info.IsLinear)
  327. {
  328. return SizeCalculator.GetLinearAlignedSize(
  329. width,
  330. height,
  331. info.FormatInfo.BlockWidth,
  332. info.FormatInfo.BlockHeight,
  333. info.FormatInfo.BytesPerPixel);
  334. }
  335. else
  336. {
  337. return SizeCalculator.GetBlockLinearAlignedSize(
  338. width,
  339. height,
  340. depth,
  341. info.FormatInfo.BlockWidth,
  342. info.FormatInfo.BlockHeight,
  343. info.FormatInfo.BytesPerPixel,
  344. info.GobBlocksInY,
  345. info.GobBlocksInZ,
  346. info.GobBlocksInTileX);
  347. }
  348. }
  349. /// <summary>
  350. /// Gets the aligned sizes of the specified texture information, shifted to the largest mip from a given level.
  351. /// The alignment depends on the texture layout and format bytes per pixel.
  352. /// </summary>
  353. /// <param name="info">Texture information to calculate the aligned size from</param>
  354. /// <param name="level">Mipmap level for texture views. Shifts the aligned size to represent the largest mip level</param>
  355. /// <returns>The aligned texture size of the largest mip level</returns>
  356. public static Size GetLargestAlignedSize(TextureInfo info, int level)
  357. {
  358. int width = info.Width << level;
  359. int height = info.Height << level;
  360. int depth = info.GetDepth() << level;
  361. return GetAlignedSize(info, width, height, depth);
  362. }
  363. /// <summary>
  364. /// Gets the aligned sizes of the specified texture information.
  365. /// The alignment depends on the texture layout and format bytes per pixel.
  366. /// </summary>
  367. /// <param name="info">Texture information to calculate the aligned size from</param>
  368. /// <param name="level">Mipmap level for texture views</param>
  369. /// <returns>The aligned texture size</returns>
  370. public static Size GetAlignedSize(TextureInfo info, int level = 0)
  371. {
  372. int width = Math.Max(1, info.Width >> level);
  373. int height = Math.Max(1, info.Height >> level);
  374. int depth = Math.Max(1, info.GetDepth() >> level);
  375. return GetAlignedSize(info, width, height, depth);
  376. }
  377. /// <summary>
  378. /// Check if it's possible to create a view with the layout of the second texture information from the first.
  379. /// The layout information is composed of the Stride for linear textures, or GOB block size
  380. /// for block linear textures.
  381. /// </summary>
  382. /// <param name="lhs">Texture information of the texture view</param>
  383. /// <param name="rhs">Texture information of the texture view to compare against</param>
  384. /// <param name="level">Start level of the texture view, in relation with the first texture</param>
  385. /// <returns>True if the layout is compatible, false otherwise</returns>
  386. public static bool ViewLayoutCompatible(TextureInfo lhs, TextureInfo rhs, int level)
  387. {
  388. if (lhs.IsLinear != rhs.IsLinear)
  389. {
  390. return false;
  391. }
  392. // For linear textures, gob block sizes are ignored.
  393. // For block linear textures, the stride is ignored.
  394. if (rhs.IsLinear)
  395. {
  396. int stride = Math.Max(1, lhs.Stride >> level);
  397. stride = BitUtils.AlignUp(stride, 32);
  398. return stride == rhs.Stride;
  399. }
  400. else
  401. {
  402. int height = Math.Max(1, lhs.Height >> level);
  403. int depth = Math.Max(1, lhs.GetDepth() >> level);
  404. (int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  405. height,
  406. depth,
  407. lhs.FormatInfo.BlockHeight,
  408. lhs.GobBlocksInY,
  409. lhs.GobBlocksInZ);
  410. return gobBlocksInY == rhs.GobBlocksInY &&
  411. gobBlocksInZ == rhs.GobBlocksInZ;
  412. }
  413. }
  414. /// <summary>
  415. /// Checks if the view format of the first texture format is compatible with the format of the second.
  416. /// In general, the formats are considered compatible if the bytes per pixel values are equal,
  417. /// but there are more complex rules for some formats, like compressed or depth-stencil formats.
  418. /// This follows the host API copy compatibility rules.
  419. /// </summary>
  420. /// <param name="lhs">Texture information of the texture view</param>
  421. /// <param name="rhs">Texture information of the texture view</param>
  422. /// <returns>The view compatibility level of the texture formats</returns>
  423. public static TextureViewCompatibility ViewFormatCompatible(TextureInfo lhs, TextureInfo rhs)
  424. {
  425. if (FormatCompatible(lhs.FormatInfo, rhs.FormatInfo))
  426. {
  427. if (lhs.FormatInfo.IsCompressed != rhs.FormatInfo.IsCompressed)
  428. {
  429. return TextureViewCompatibility.CopyOnly;
  430. }
  431. else
  432. {
  433. return TextureViewCompatibility.Full;
  434. }
  435. }
  436. return TextureViewCompatibility.Incompatible;
  437. }
  438. /// <summary>
  439. /// Check if the target of the first texture view information is compatible with the target of the second texture view information.
  440. /// This follows the host API target compatibility rules.
  441. /// </summary>
  442. /// <param name="lhs">Texture information of the texture view</param
  443. /// <param name="rhs">Texture information of the texture view</param>
  444. /// <param name="isCopy">True to check for copy rather than view compatibility</param>
  445. /// <returns>True if the targets are compatible, false otherwise</returns>
  446. public static TextureViewCompatibility ViewTargetCompatible(TextureInfo lhs, TextureInfo rhs)
  447. {
  448. bool result = false;
  449. switch (lhs.Target)
  450. {
  451. case Target.Texture1D:
  452. case Target.Texture1DArray:
  453. result = rhs.Target == Target.Texture1D ||
  454. rhs.Target == Target.Texture1DArray;
  455. break;
  456. case Target.Texture2D:
  457. result = rhs.Target == Target.Texture2D ||
  458. rhs.Target == Target.Texture2DArray;
  459. break;
  460. case Target.Texture2DArray:
  461. case Target.Cubemap:
  462. case Target.CubemapArray:
  463. result = rhs.Target == Target.Texture2D ||
  464. rhs.Target == Target.Texture2DArray ||
  465. rhs.Target == Target.Cubemap ||
  466. rhs.Target == Target.CubemapArray;
  467. break;
  468. case Target.Texture2DMultisample:
  469. case Target.Texture2DMultisampleArray:
  470. result = rhs.Target == Target.Texture2DMultisample ||
  471. rhs.Target == Target.Texture2DMultisampleArray;
  472. break;
  473. case Target.Texture3D:
  474. if (rhs.Target == Target.Texture2D)
  475. {
  476. return TextureViewCompatibility.CopyOnly;
  477. }
  478. result = rhs.Target == Target.Texture3D;
  479. break;
  480. }
  481. return result ? TextureViewCompatibility.Full : TextureViewCompatibility.Incompatible;
  482. }
  483. /// <summary>
  484. /// Checks if a swizzle component in two textures functionally match, taking into account if the components are defined.
  485. /// </summary>
  486. /// <param name="lhs">Texture information to compare</param>
  487. /// <param name="rhs">Texture information to compare with</param>
  488. /// <param name="swizzleLhs">Swizzle component for the first texture</param>
  489. /// <param name="swizzleRhs">Swizzle component for the second texture</param>
  490. /// <param name="component">Component index, starting at 0 for red</param>
  491. /// <returns>True if the swizzle components functionally match, false othersize</returns>
  492. private static bool SwizzleComponentMatches(TextureInfo lhs, TextureInfo rhs, SwizzleComponent swizzleLhs, SwizzleComponent swizzleRhs, int component)
  493. {
  494. int lhsComponents = lhs.FormatInfo.Components;
  495. int rhsComponents = rhs.FormatInfo.Components;
  496. if (lhsComponents == 4 && rhsComponents == 4)
  497. {
  498. return swizzleLhs == swizzleRhs;
  499. }
  500. // Swizzles after the number of components a format defines are "undefined".
  501. // We allow these to not be equal under certain circumstances.
  502. // This can only happen when there are less than 4 components in a format.
  503. // It tends to happen when float depth textures are sampled.
  504. bool lhsDefined = (swizzleLhs - SwizzleComponent.Red) < lhsComponents;
  505. bool rhsDefined = (swizzleRhs - SwizzleComponent.Red) < rhsComponents;
  506. if (lhsDefined == rhsDefined)
  507. {
  508. // If both are undefined, return true. Otherwise just check if they're equal.
  509. return lhsDefined ? swizzleLhs == swizzleRhs : true;
  510. }
  511. else
  512. {
  513. SwizzleComponent defined = lhsDefined ? swizzleLhs : swizzleRhs;
  514. SwizzleComponent undefined = lhsDefined ? swizzleRhs : swizzleLhs;
  515. // Undefined swizzle can be matched by a forced value (0, 1), exact equality, or expected value.
  516. // For example, R___ matches R001, RGBA but not RBGA.
  517. return defined == undefined || defined < SwizzleComponent.Red || defined == SwizzleComponent.Red + component;
  518. }
  519. }
  520. /// <summary>
  521. /// Checks if the texture shader sampling parameters of two texture informations match.
  522. /// </summary>
  523. /// <param name="lhs">Texture information to compare</param>
  524. /// <param name="rhs">Texture information to compare with</param>
  525. /// <returns>True if the texture shader sampling parameters matches, false otherwise</returns>
  526. public static bool SamplerParamsMatches(TextureInfo lhs, TextureInfo rhs)
  527. {
  528. return lhs.DepthStencilMode == rhs.DepthStencilMode &&
  529. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleR, rhs.SwizzleR, 0) &&
  530. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleG, rhs.SwizzleG, 1) &&
  531. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleB, rhs.SwizzleB, 2) &&
  532. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleA, rhs.SwizzleA, 3);
  533. }
  534. /// <summary>
  535. /// Check if the texture target and samples count (for multisampled textures) matches.
  536. /// </summary>
  537. /// <param name="first">Texture information to compare with</param>
  538. /// <param name="rhs">Texture information to compare with</param>
  539. /// <returns>True if the texture target and samples count matches, false otherwise</returns>
  540. public static bool TargetAndSamplesCompatible(TextureInfo lhs, TextureInfo rhs)
  541. {
  542. return lhs.Target == rhs.Target &&
  543. lhs.SamplesInX == rhs.SamplesInX &&
  544. lhs.SamplesInY == rhs.SamplesInY;
  545. }
  546. /// <summary>
  547. /// Gets the texture format class, for compressed textures, or Unclassified otherwise.
  548. /// </summary>
  549. /// <param name="format">The format</param>
  550. /// <returns>Format class</returns>
  551. private static FormatClass GetFormatClass(Format format)
  552. {
  553. switch (format)
  554. {
  555. case Format.Bc1RgbSrgb:
  556. case Format.Bc1RgbUnorm:
  557. return FormatClass.Bc1Rgb;
  558. case Format.Bc1RgbaSrgb:
  559. case Format.Bc1RgbaUnorm:
  560. return FormatClass.Bc1Rgba;
  561. case Format.Bc2Srgb:
  562. case Format.Bc2Unorm:
  563. return FormatClass.Bc2;
  564. case Format.Bc3Srgb:
  565. case Format.Bc3Unorm:
  566. return FormatClass.Bc3;
  567. case Format.Bc4Snorm:
  568. case Format.Bc4Unorm:
  569. return FormatClass.Bc4;
  570. case Format.Bc5Snorm:
  571. case Format.Bc5Unorm:
  572. return FormatClass.Bc5;
  573. case Format.Bc6HSfloat:
  574. case Format.Bc6HUfloat:
  575. return FormatClass.Bc6;
  576. case Format.Bc7Srgb:
  577. case Format.Bc7Unorm:
  578. return FormatClass.Bc7;
  579. }
  580. return FormatClass.Unclassified;
  581. }
  582. }
  583. }