TextureCompatibility.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Texture;
  4. using System;
  5. using System.Numerics;
  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. Etc2Rgb,
  27. Etc2Rgba,
  28. Astc4x4,
  29. Astc5x4,
  30. Astc5x5,
  31. Astc6x5,
  32. Astc6x6,
  33. Astc8x5,
  34. Astc8x6,
  35. Astc8x8,
  36. Astc10x5,
  37. Astc10x6,
  38. Astc10x8,
  39. Astc10x10,
  40. Astc12x10,
  41. Astc12x12
  42. }
  43. /// <summary>
  44. /// Checks if a format is host incompatible.
  45. /// </summary>
  46. /// <remarks>
  47. /// Host incompatible formats can't be used directly, the texture data needs to be converted
  48. /// to a compatible format first.
  49. /// </remarks>
  50. /// <param name="info">Texture information</param>
  51. /// <param name="caps">Host GPU capabilities</param>
  52. /// <returns>True if the format is incompatible, false otherwise</returns>
  53. public static bool IsFormatHostIncompatible(TextureInfo info, Capabilities caps)
  54. {
  55. Format originalFormat = info.FormatInfo.Format;
  56. return ToHostCompatibleFormat(info, caps).Format != originalFormat;
  57. }
  58. /// <summary>
  59. /// Converts a incompatible format to a host compatible format, or return the format directly
  60. /// if it is already host compatible.
  61. /// </summary>
  62. /// <remarks>
  63. /// This can be used to convert a incompatible compressed format to the decompressor
  64. /// output format.
  65. /// </remarks>
  66. /// <param name="info">Texture information</param>
  67. /// <param name="caps">Host GPU capabilities</param>
  68. /// <returns>A host compatible format</returns>
  69. public static FormatInfo ToHostCompatibleFormat(TextureInfo info, Capabilities caps)
  70. {
  71. if (!caps.SupportsAstcCompression)
  72. {
  73. if (info.FormatInfo.Format.IsAstcUnorm())
  74. {
  75. return new FormatInfo(Format.R8G8B8A8Unorm, 1, 1, 4, 4);
  76. }
  77. else if (info.FormatInfo.Format.IsAstcSrgb())
  78. {
  79. return new FormatInfo(Format.R8G8B8A8Srgb, 1, 1, 4, 4);
  80. }
  81. }
  82. if (!caps.SupportsR4G4Format && info.FormatInfo.Format == Format.R4G4Unorm)
  83. {
  84. return new FormatInfo(Format.R4G4B4A4Unorm, 1, 1, 2, 4);
  85. }
  86. if (info.Target == Target.Texture3D)
  87. {
  88. // The host API does not support 3D BC4/BC5 compressed formats.
  89. // We assume software decompression will be done for those textures,
  90. // and so we adjust the format here to match the decompressor output.
  91. switch (info.FormatInfo.Format)
  92. {
  93. case Format.Bc4Unorm:
  94. return new FormatInfo(Format.R8Unorm, 1, 1, 1, 1);
  95. case Format.Bc4Snorm:
  96. return new FormatInfo(Format.R8Snorm, 1, 1, 1, 1);
  97. case Format.Bc5Unorm:
  98. return new FormatInfo(Format.R8G8Unorm, 1, 1, 2, 2);
  99. case Format.Bc5Snorm:
  100. return new FormatInfo(Format.R8G8Snorm, 1, 1, 2, 2);
  101. }
  102. }
  103. return info.FormatInfo;
  104. }
  105. /// <summary>
  106. /// Determines whether a texture can flush its data back to guest memory.
  107. /// </summary>
  108. /// <param name="info">Texture information</param>
  109. /// <param name="caps">Host GPU Capabilities</param>
  110. /// <returns>True if the texture can flush, false otherwise</returns>
  111. public static bool CanTextureFlush(TextureInfo info, Capabilities caps)
  112. {
  113. if (IsFormatHostIncompatible(info, caps))
  114. {
  115. return false; // Flushing this format is not supported, as it may have been converted to another host format.
  116. }
  117. if (info.Target == Target.Texture2DMultisample ||
  118. info.Target == Target.Texture2DMultisampleArray)
  119. {
  120. return false; // Flushing multisample textures is not supported, the host does not allow getting their data.
  121. }
  122. return true;
  123. }
  124. /// <summary>
  125. /// Checks if two formats are compatible, according to the host API copy format compatibility rules.
  126. /// </summary>
  127. /// <param name="lhsFormat">First comparand</param>
  128. /// <param name="rhsFormat">Second comparand</param>
  129. /// <param name="caps">Host GPU capabilities</param>
  130. /// <returns>True if the formats are compatible, false otherwise</returns>
  131. public static bool FormatCompatible(TextureInfo lhs, TextureInfo rhs, Capabilities caps)
  132. {
  133. FormatInfo lhsFormat = lhs.FormatInfo;
  134. FormatInfo rhsFormat = rhs.FormatInfo;
  135. if (lhsFormat.Format.IsDepthOrStencil() || rhsFormat.Format.IsDepthOrStencil())
  136. {
  137. return lhsFormat.Format == rhsFormat.Format;
  138. }
  139. if (IsFormatHostIncompatible(lhs, caps) || IsFormatHostIncompatible(rhs, caps))
  140. {
  141. return lhsFormat.Format == rhsFormat.Format;
  142. }
  143. if (lhsFormat.IsCompressed && rhsFormat.IsCompressed)
  144. {
  145. FormatClass lhsClass = GetFormatClass(lhsFormat.Format);
  146. FormatClass rhsClass = GetFormatClass(rhsFormat.Format);
  147. return lhsClass == rhsClass;
  148. }
  149. else
  150. {
  151. return lhsFormat.BytesPerPixel == rhsFormat.BytesPerPixel;
  152. }
  153. }
  154. /// <summary>
  155. /// Checks if the texture format matches with the specified texture information.
  156. /// </summary>
  157. /// <param name="lhs">Texture information to compare</param>
  158. /// <param name="rhs">Texture information to compare with</param>
  159. /// <param name="forSampler">Indicates that the texture will be used for shader sampling</param>
  160. /// <param name="forCopy">Indicates that the texture will be used as copy source or target</param>
  161. /// <returns>A value indicating how well the formats match</returns>
  162. public static TextureMatchQuality FormatMatches(TextureInfo lhs, TextureInfo rhs, bool forSampler, bool forCopy)
  163. {
  164. // D32F and R32F texture have the same representation internally,
  165. // however the R32F format is used to sample from depth textures.
  166. if (lhs.FormatInfo.Format == Format.D32Float && rhs.FormatInfo.Format == Format.R32Float && (forSampler || forCopy))
  167. {
  168. return TextureMatchQuality.FormatAlias;
  169. }
  170. if (forCopy)
  171. {
  172. // The 2D engine does not support depth-stencil formats, so it will instead
  173. // use equivalent color formats. We must also consider them as compatible.
  174. if (lhs.FormatInfo.Format == Format.S8Uint && rhs.FormatInfo.Format == Format.R8Unorm)
  175. {
  176. return TextureMatchQuality.FormatAlias;
  177. }
  178. if (lhs.FormatInfo.Format == Format.D16Unorm && rhs.FormatInfo.Format == Format.R16Unorm)
  179. {
  180. return TextureMatchQuality.FormatAlias;
  181. }
  182. if ((lhs.FormatInfo.Format == Format.D24UnormS8Uint ||
  183. lhs.FormatInfo.Format == Format.D24X8Unorm) && rhs.FormatInfo.Format == Format.B8G8R8A8Unorm)
  184. {
  185. return TextureMatchQuality.FormatAlias;
  186. }
  187. }
  188. return lhs.FormatInfo.Format == rhs.FormatInfo.Format ? TextureMatchQuality.Perfect : TextureMatchQuality.NoMatch;
  189. }
  190. /// <summary>
  191. /// Checks if the texture layout specified matches with this texture layout.
  192. /// The layout information is composed of the Stride for linear textures, or GOB block size
  193. /// for block linear textures.
  194. /// </summary>
  195. /// <param name="lhs">Texture information to compare</param>
  196. /// <param name="rhs">Texture information to compare with</param>
  197. /// <returns>True if the layout matches, false otherwise</returns>
  198. public static bool LayoutMatches(TextureInfo lhs, TextureInfo rhs)
  199. {
  200. if (lhs.IsLinear != rhs.IsLinear)
  201. {
  202. return false;
  203. }
  204. // For linear textures, gob block sizes are ignored.
  205. // For block linear textures, the stride is ignored.
  206. if (rhs.IsLinear)
  207. {
  208. return lhs.Stride == rhs.Stride;
  209. }
  210. else
  211. {
  212. return lhs.GobBlocksInY == rhs.GobBlocksInY &&
  213. lhs.GobBlocksInZ == rhs.GobBlocksInZ;
  214. }
  215. }
  216. /// <summary>
  217. /// Obtain the minimum compatibility level of two provided view compatibility results.
  218. /// </summary>
  219. /// <param name="first">The first compatibility level</param>
  220. /// <param name="second">The second compatibility level</param>
  221. /// <returns>The minimum compatibility level of two provided view compatibility results</returns>
  222. public static TextureViewCompatibility PropagateViewCompatibility(TextureViewCompatibility first, TextureViewCompatibility second)
  223. {
  224. if (first == TextureViewCompatibility.Incompatible || second == TextureViewCompatibility.Incompatible)
  225. {
  226. return TextureViewCompatibility.Incompatible;
  227. }
  228. else if (first == TextureViewCompatibility.LayoutIncompatible || second == TextureViewCompatibility.LayoutIncompatible)
  229. {
  230. return TextureViewCompatibility.LayoutIncompatible;
  231. }
  232. else if (first == TextureViewCompatibility.CopyOnly || second == TextureViewCompatibility.CopyOnly)
  233. {
  234. return TextureViewCompatibility.CopyOnly;
  235. }
  236. else
  237. {
  238. return TextureViewCompatibility.Full;
  239. }
  240. }
  241. /// <summary>
  242. /// Checks if the sizes of two texture levels are copy compatible.
  243. /// </summary>
  244. /// <param name="lhs">Texture information of the texture view</param>
  245. /// <param name="rhs">Texture information of the texture view to match against</param>
  246. /// <param name="lhsLevel">Mipmap level of the texture view in relation to this texture</param>
  247. /// <param name="rhsLevel">Mipmap level of the texture view in relation to the second texture</param>
  248. /// <returns>True if both levels are view compatible</returns>
  249. public static bool CopySizeMatches(TextureInfo lhs, TextureInfo rhs, int lhsLevel, int rhsLevel)
  250. {
  251. Size size = GetAlignedSize(lhs, lhsLevel);
  252. Size otherSize = GetAlignedSize(rhs, rhsLevel);
  253. if (size.Width == otherSize.Width && size.Height == otherSize.Height)
  254. {
  255. return true;
  256. }
  257. else if (lhs.IsLinear && rhs.IsLinear)
  258. {
  259. // Copy between linear textures with matching stride.
  260. int stride = BitUtils.AlignUp(Math.Max(1, lhs.Stride >> lhsLevel), Constants.StrideAlignment);
  261. return stride == rhs.Stride;
  262. }
  263. else
  264. {
  265. return false;
  266. }
  267. }
  268. /// <summary>
  269. /// Checks if the sizes of two given textures are view compatible.
  270. /// </summary>
  271. /// <param name="lhs">Texture information of the texture view</param>
  272. /// <param name="rhs">Texture information of the texture view to match against</param>
  273. /// <param name="level">Mipmap level of the texture view in relation to this texture</param>
  274. /// <returns>The view compatibility level of the view sizes</returns>
  275. public static TextureViewCompatibility ViewSizeMatches(TextureInfo lhs, TextureInfo rhs, int level)
  276. {
  277. Size size = GetAlignedSize(lhs, level);
  278. Size otherSize = GetAlignedSize(rhs);
  279. TextureViewCompatibility result = TextureViewCompatibility.Full;
  280. // For copies, we can copy a subset of the 3D texture slices,
  281. // so the depth may be different in this case.
  282. if (rhs.Target == Target.Texture3D && size.Depth != otherSize.Depth)
  283. {
  284. result = TextureViewCompatibility.CopyOnly;
  285. }
  286. if (size.Width == otherSize.Width && size.Height == otherSize.Height)
  287. {
  288. if (level > 0 && result == TextureViewCompatibility.Full)
  289. {
  290. // A resize should not change the aligned size of the largest mip.
  291. // If it would, then create a copy dependency rather than a full view.
  292. Size mip0SizeLhs = GetAlignedSize(lhs);
  293. Size mip0SizeRhs = GetLargestAlignedSize(rhs, level);
  294. if (mip0SizeLhs.Width != mip0SizeRhs.Width || mip0SizeLhs.Height != mip0SizeRhs.Height)
  295. {
  296. result = TextureViewCompatibility.CopyOnly;
  297. }
  298. }
  299. return result;
  300. }
  301. else if (lhs.IsLinear && rhs.IsLinear)
  302. {
  303. // Copy between linear textures with matching stride.
  304. int stride = BitUtils.AlignUp(Math.Max(1, lhs.Stride >> level), Constants.StrideAlignment);
  305. return stride == rhs.Stride ? TextureViewCompatibility.CopyOnly : TextureViewCompatibility.LayoutIncompatible;
  306. }
  307. else
  308. {
  309. return TextureViewCompatibility.LayoutIncompatible;
  310. }
  311. }
  312. /// <summary>
  313. /// Checks if the potential child texture fits within the level and layer bounds of the parent.
  314. /// </summary>
  315. /// <param name="parent">Texture information for the parent</param>
  316. /// <param name="child">Texture information for the child</param>
  317. /// <param name="layer">Base layer of the child texture</param>
  318. /// <param name="level">Base level of the child texture</param>
  319. /// <returns>Full compatiblity if the child's layer and level count fit within the parent, incompatible otherwise</returns>
  320. public static TextureViewCompatibility ViewSubImagesInBounds(TextureInfo parent, TextureInfo child, int layer, int level)
  321. {
  322. if (level + child.Levels <= parent.Levels &&
  323. layer + child.GetSlices() <= parent.GetSlices())
  324. {
  325. return TextureViewCompatibility.Full;
  326. }
  327. else
  328. {
  329. return TextureViewCompatibility.LayoutIncompatible;
  330. }
  331. }
  332. /// <summary>
  333. /// Checks if the texture sizes of the supplied texture informations match.
  334. /// </summary>
  335. /// <param name="lhs">Texture information to compare</param>
  336. /// <param name="rhs">Texture information to compare with</param>
  337. /// <returns>True if the size matches, false otherwise</returns>
  338. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs)
  339. {
  340. return SizeMatches(lhs, rhs, alignSizes: false);
  341. }
  342. /// <summary>
  343. /// Checks if the texture sizes of the supplied texture informations match the given level
  344. /// </summary>
  345. /// <param name="lhs">Texture information to compare</param>
  346. /// <param name="rhs">Texture information to compare with</param>
  347. /// <param name="level">Mipmap level of this texture to compare with</param>
  348. /// <returns>True if the size matches with the level, false otherwise</returns>
  349. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs, int level)
  350. {
  351. return Math.Max(1, lhs.Width >> level) == rhs.Width &&
  352. Math.Max(1, lhs.Height >> level) == rhs.Height &&
  353. Math.Max(1, lhs.GetDepth() >> level) == rhs.GetDepth();
  354. }
  355. /// <summary>
  356. /// Checks if the texture sizes of the supplied texture informations match.
  357. /// </summary>
  358. /// <param name="lhs">Texture information to compare</param>
  359. /// <param name="rhs">Texture information to compare with</param>
  360. /// <param name="alignSizes">True to align the sizes according to the texture layout for comparison</param>
  361. /// <param name="lhsLevel">Mip level of the lhs texture. Aligned sizes are compared for the largest mip</param>
  362. /// <returns>True if the sizes matches, false otherwise</returns>
  363. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs, bool alignSizes, int lhsLevel = 0)
  364. {
  365. if (lhs.GetLayers() != rhs.GetLayers())
  366. {
  367. return false;
  368. }
  369. bool isTextureBuffer = lhs.Target == Target.TextureBuffer || rhs.Target == Target.TextureBuffer;
  370. if (alignSizes && !isTextureBuffer)
  371. {
  372. Size size0 = GetLargestAlignedSize(lhs, lhsLevel);
  373. Size size1 = GetLargestAlignedSize(rhs, lhsLevel);
  374. return size0.Width == size1.Width &&
  375. size0.Height == size1.Height &&
  376. size0.Depth == size1.Depth;
  377. }
  378. else
  379. {
  380. return lhs.Width == rhs.Width &&
  381. lhs.Height == rhs.Height &&
  382. lhs.GetDepth() == rhs.GetDepth();
  383. }
  384. }
  385. /// <summary>
  386. /// Gets the aligned sizes for the given dimensions, using the specified texture information.
  387. /// The alignment depends on the texture layout and format bytes per pixel.
  388. /// </summary>
  389. /// <param name="info">Texture information to calculate the aligned size from</param>
  390. /// <param name="width">The width to be aligned</param>
  391. /// <param name="height">The height to be aligned</param>
  392. /// <param name="depth">The depth to be aligned</param>
  393. /// <returns>The aligned texture size</returns>
  394. private static Size GetAlignedSize(TextureInfo info, int width, int height, int depth)
  395. {
  396. if (info.IsLinear)
  397. {
  398. return SizeCalculator.GetLinearAlignedSize(
  399. width,
  400. height,
  401. info.FormatInfo.BlockWidth,
  402. info.FormatInfo.BlockHeight,
  403. info.FormatInfo.BytesPerPixel);
  404. }
  405. else
  406. {
  407. return SizeCalculator.GetBlockLinearAlignedSize(
  408. width,
  409. height,
  410. depth,
  411. info.FormatInfo.BlockWidth,
  412. info.FormatInfo.BlockHeight,
  413. info.FormatInfo.BytesPerPixel,
  414. info.GobBlocksInY,
  415. info.GobBlocksInZ,
  416. info.GobBlocksInTileX);
  417. }
  418. }
  419. /// <summary>
  420. /// Gets the aligned sizes of the specified texture information, shifted to the largest mip from a given level.
  421. /// The alignment depends on the texture layout and format bytes per pixel.
  422. /// </summary>
  423. /// <param name="info">Texture information to calculate the aligned size from</param>
  424. /// <param name="level">Mipmap level for texture views. Shifts the aligned size to represent the largest mip level</param>
  425. /// <returns>The aligned texture size of the largest mip level</returns>
  426. public static Size GetLargestAlignedSize(TextureInfo info, int level)
  427. {
  428. int width = info.Width << level;
  429. int height = info.Height << level;
  430. int depth = info.GetDepth() << level;
  431. return GetAlignedSize(info, width, height, depth);
  432. }
  433. /// <summary>
  434. /// Gets the aligned sizes of the specified texture information.
  435. /// The alignment depends on the texture layout and format bytes per pixel.
  436. /// </summary>
  437. /// <param name="info">Texture information to calculate the aligned size from</param>
  438. /// <param name="level">Mipmap level for texture views</param>
  439. /// <returns>The aligned texture size</returns>
  440. public static Size GetAlignedSize(TextureInfo info, int level = 0)
  441. {
  442. int width = Math.Max(1, info.Width >> level);
  443. int height = Math.Max(1, info.Height >> level);
  444. int depth = Math.Max(1, info.GetDepth() >> level);
  445. return GetAlignedSize(info, width, height, depth);
  446. }
  447. /// <summary>
  448. /// Check if it's possible to create a view with the layout of the second texture information from the first.
  449. /// The layout information is composed of the Stride for linear textures, or GOB block size
  450. /// for block linear textures.
  451. /// </summary>
  452. /// <param name="lhs">Texture information of the texture view</param>
  453. /// <param name="rhs">Texture information of the texture view to compare against</param>
  454. /// <param name="level">Start level of the texture view, in relation with the first texture</param>
  455. /// <returns>True if the layout is compatible, false otherwise</returns>
  456. public static bool ViewLayoutCompatible(TextureInfo lhs, TextureInfo rhs, int level)
  457. {
  458. if (lhs.IsLinear != rhs.IsLinear)
  459. {
  460. return false;
  461. }
  462. // For linear textures, gob block sizes are ignored.
  463. // For block linear textures, the stride is ignored.
  464. if (rhs.IsLinear)
  465. {
  466. int stride = Math.Max(1, lhs.Stride >> level);
  467. stride = BitUtils.AlignUp(stride, Constants.StrideAlignment);
  468. return stride == rhs.Stride;
  469. }
  470. else
  471. {
  472. int height = Math.Max(1, lhs.Height >> level);
  473. int depth = Math.Max(1, lhs.GetDepth() >> level);
  474. (int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  475. height,
  476. depth,
  477. lhs.FormatInfo.BlockHeight,
  478. lhs.GobBlocksInY,
  479. lhs.GobBlocksInZ);
  480. return gobBlocksInY == rhs.GobBlocksInY &&
  481. gobBlocksInZ == rhs.GobBlocksInZ;
  482. }
  483. }
  484. /// <summary>
  485. /// Check if it's possible to create a view with the layout of the second texture information from the first.
  486. /// The layout information is composed of the Stride for linear textures, or GOB block size
  487. /// for block linear textures.
  488. /// </summary>
  489. /// <param name="lhs">Texture information of the texture view</param>
  490. /// <param name="rhs">Texture information of the texture view to compare against</param>
  491. /// <param name="lhsLevel">Start level of the texture view, in relation with the first texture</param>
  492. /// <param name="rhsLevel">Start level of the texture view, in relation with the second texture</param>
  493. /// <returns>True if the layout is compatible, false otherwise</returns>
  494. public static bool ViewLayoutCompatible(TextureInfo lhs, TextureInfo rhs, int lhsLevel, int rhsLevel)
  495. {
  496. if (lhs.IsLinear != rhs.IsLinear)
  497. {
  498. return false;
  499. }
  500. // For linear textures, gob block sizes are ignored.
  501. // For block linear textures, the stride is ignored.
  502. if (rhs.IsLinear)
  503. {
  504. int lhsStride = Math.Max(1, lhs.Stride >> lhsLevel);
  505. lhsStride = BitUtils.AlignUp(lhsStride, Constants.StrideAlignment);
  506. int rhsStride = Math.Max(1, rhs.Stride >> rhsLevel);
  507. rhsStride = BitUtils.AlignUp(rhsStride, Constants.StrideAlignment);
  508. return lhsStride == rhsStride;
  509. }
  510. else
  511. {
  512. int lhsHeight = Math.Max(1, lhs.Height >> lhsLevel);
  513. int lhsDepth = Math.Max(1, lhs.GetDepth() >> lhsLevel);
  514. (int lhsGobBlocksInY, int lhsGobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  515. lhsHeight,
  516. lhsDepth,
  517. lhs.FormatInfo.BlockHeight,
  518. lhs.GobBlocksInY,
  519. lhs.GobBlocksInZ);
  520. int rhsHeight = Math.Max(1, rhs.Height >> rhsLevel);
  521. int rhsDepth = Math.Max(1, rhs.GetDepth() >> rhsLevel);
  522. (int rhsGobBlocksInY, int rhsGobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  523. rhsHeight,
  524. rhsDepth,
  525. rhs.FormatInfo.BlockHeight,
  526. rhs.GobBlocksInY,
  527. rhs.GobBlocksInZ);
  528. return lhsGobBlocksInY == rhsGobBlocksInY &&
  529. lhsGobBlocksInZ == rhsGobBlocksInZ;
  530. }
  531. }
  532. /// <summary>
  533. /// Checks if the view format of the first texture format is compatible with the format of the second.
  534. /// In general, the formats are considered compatible if the bytes per pixel values are equal,
  535. /// but there are more complex rules for some formats, like compressed or depth-stencil formats.
  536. /// This follows the host API copy compatibility rules.
  537. /// </summary>
  538. /// <param name="lhs">Texture information of the texture view</param>
  539. /// <param name="rhs">Texture information of the texture view</param>
  540. /// <param name="caps">Host GPU capabilities</param>
  541. /// <returns>The view compatibility level of the texture formats</returns>
  542. public static TextureViewCompatibility ViewFormatCompatible(TextureInfo lhs, TextureInfo rhs, Capabilities caps)
  543. {
  544. if (FormatCompatible(lhs, rhs, caps))
  545. {
  546. if (lhs.FormatInfo.IsCompressed != rhs.FormatInfo.IsCompressed)
  547. {
  548. return TextureViewCompatibility.CopyOnly;
  549. }
  550. else
  551. {
  552. return TextureViewCompatibility.Full;
  553. }
  554. }
  555. return TextureViewCompatibility.Incompatible;
  556. }
  557. /// <summary>
  558. /// Check if the target of the first texture view information is compatible with the target of the second texture view information.
  559. /// This follows the host API target compatibility rules.
  560. /// </summary>
  561. /// <param name="lhs">Texture information of the texture view</param
  562. /// <param name="rhs">Texture information of the texture view</param>
  563. /// <param name="isCopy">True to check for copy rather than view compatibility</param>
  564. /// <returns>True if the targets are compatible, false otherwise</returns>
  565. public static TextureViewCompatibility ViewTargetCompatible(TextureInfo lhs, TextureInfo rhs)
  566. {
  567. bool result = false;
  568. switch (lhs.Target)
  569. {
  570. case Target.Texture1D:
  571. case Target.Texture1DArray:
  572. result = rhs.Target == Target.Texture1D ||
  573. rhs.Target == Target.Texture1DArray;
  574. break;
  575. case Target.Texture2D:
  576. result = rhs.Target == Target.Texture2D ||
  577. rhs.Target == Target.Texture2DArray;
  578. break;
  579. case Target.Texture2DArray:
  580. case Target.Cubemap:
  581. case Target.CubemapArray:
  582. result = rhs.Target == Target.Texture2D ||
  583. rhs.Target == Target.Texture2DArray ||
  584. rhs.Target == Target.Cubemap ||
  585. rhs.Target == Target.CubemapArray;
  586. break;
  587. case Target.Texture2DMultisample:
  588. case Target.Texture2DMultisampleArray:
  589. result = rhs.Target == Target.Texture2DMultisample ||
  590. rhs.Target == Target.Texture2DMultisampleArray;
  591. break;
  592. case Target.Texture3D:
  593. if (rhs.Target == Target.Texture2D)
  594. {
  595. return TextureViewCompatibility.CopyOnly;
  596. }
  597. result = rhs.Target == Target.Texture3D;
  598. break;
  599. }
  600. return result ? TextureViewCompatibility.Full : TextureViewCompatibility.Incompatible;
  601. }
  602. /// <summary>
  603. /// Checks if a swizzle component in two textures functionally match, taking into account if the components are defined.
  604. /// </summary>
  605. /// <param name="lhs">Texture information to compare</param>
  606. /// <param name="rhs">Texture information to compare with</param>
  607. /// <param name="swizzleLhs">Swizzle component for the first texture</param>
  608. /// <param name="swizzleRhs">Swizzle component for the second texture</param>
  609. /// <param name="component">Component index, starting at 0 for red</param>
  610. /// <returns>True if the swizzle components functionally match, false othersize</returns>
  611. private static bool SwizzleComponentMatches(TextureInfo lhs, TextureInfo rhs, SwizzleComponent swizzleLhs, SwizzleComponent swizzleRhs, int component)
  612. {
  613. int lhsComponents = lhs.FormatInfo.Components;
  614. int rhsComponents = rhs.FormatInfo.Components;
  615. if (lhsComponents == 4 && rhsComponents == 4)
  616. {
  617. return swizzleLhs == swizzleRhs;
  618. }
  619. // Swizzles after the number of components a format defines are "undefined".
  620. // We allow these to not be equal under certain circumstances.
  621. // This can only happen when there are less than 4 components in a format.
  622. // It tends to happen when float depth textures are sampled.
  623. bool lhsDefined = (swizzleLhs - SwizzleComponent.Red) < lhsComponents;
  624. bool rhsDefined = (swizzleRhs - SwizzleComponent.Red) < rhsComponents;
  625. if (lhsDefined == rhsDefined)
  626. {
  627. // If both are undefined, return true. Otherwise just check if they're equal.
  628. return lhsDefined ? swizzleLhs == swizzleRhs : true;
  629. }
  630. else
  631. {
  632. SwizzleComponent defined = lhsDefined ? swizzleLhs : swizzleRhs;
  633. SwizzleComponent undefined = lhsDefined ? swizzleRhs : swizzleLhs;
  634. // Undefined swizzle can be matched by a forced value (0, 1), exact equality, or expected value.
  635. // For example, R___ matches R001, RGBA but not RBGA.
  636. return defined == undefined || defined < SwizzleComponent.Red || defined == SwizzleComponent.Red + component;
  637. }
  638. }
  639. /// <summary>
  640. /// Checks if the texture shader sampling parameters of two texture informations match.
  641. /// </summary>
  642. /// <param name="lhs">Texture information to compare</param>
  643. /// <param name="rhs">Texture information to compare with</param>
  644. /// <returns>True if the texture shader sampling parameters matches, false otherwise</returns>
  645. public static bool SamplerParamsMatches(TextureInfo lhs, TextureInfo rhs)
  646. {
  647. return lhs.DepthStencilMode == rhs.DepthStencilMode &&
  648. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleR, rhs.SwizzleR, 0) &&
  649. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleG, rhs.SwizzleG, 1) &&
  650. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleB, rhs.SwizzleB, 2) &&
  651. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleA, rhs.SwizzleA, 3);
  652. }
  653. /// <summary>
  654. /// Check if the texture target and samples count (for multisampled textures) matches.
  655. /// </summary>
  656. /// <param name="first">Texture information to compare with</param>
  657. /// <param name="rhs">Texture information to compare with</param>
  658. /// <returns>True if the texture target and samples count matches, false otherwise</returns>
  659. public static bool TargetAndSamplesCompatible(TextureInfo lhs, TextureInfo rhs)
  660. {
  661. return lhs.Target == rhs.Target &&
  662. lhs.SamplesInX == rhs.SamplesInX &&
  663. lhs.SamplesInY == rhs.SamplesInY;
  664. }
  665. /// <summary>
  666. /// Gets the texture format class, for compressed textures, or Unclassified otherwise.
  667. /// </summary>
  668. /// <param name="format">The format</param>
  669. /// <returns>Format class</returns>
  670. private static FormatClass GetFormatClass(Format format)
  671. {
  672. switch (format)
  673. {
  674. case Format.Bc1RgbSrgb:
  675. case Format.Bc1RgbUnorm:
  676. return FormatClass.Bc1Rgb;
  677. case Format.Bc1RgbaSrgb:
  678. case Format.Bc1RgbaUnorm:
  679. return FormatClass.Bc1Rgba;
  680. case Format.Bc2Srgb:
  681. case Format.Bc2Unorm:
  682. return FormatClass.Bc2;
  683. case Format.Bc3Srgb:
  684. case Format.Bc3Unorm:
  685. return FormatClass.Bc3;
  686. case Format.Bc4Snorm:
  687. case Format.Bc4Unorm:
  688. return FormatClass.Bc4;
  689. case Format.Bc5Snorm:
  690. case Format.Bc5Unorm:
  691. return FormatClass.Bc5;
  692. case Format.Bc6HSfloat:
  693. case Format.Bc6HUfloat:
  694. return FormatClass.Bc6;
  695. case Format.Bc7Srgb:
  696. case Format.Bc7Unorm:
  697. return FormatClass.Bc7;
  698. case Format.Etc2RgbSrgb:
  699. case Format.Etc2RgbUnorm:
  700. return FormatClass.Etc2Rgb;
  701. case Format.Etc2RgbaSrgb:
  702. case Format.Etc2RgbaUnorm:
  703. return FormatClass.Etc2Rgba;
  704. case Format.Astc4x4Srgb:
  705. case Format.Astc4x4Unorm:
  706. return FormatClass.Astc4x4;
  707. case Format.Astc5x4Srgb:
  708. case Format.Astc5x4Unorm:
  709. return FormatClass.Astc5x4;
  710. case Format.Astc5x5Srgb:
  711. case Format.Astc5x5Unorm:
  712. return FormatClass.Astc5x5;
  713. case Format.Astc6x5Srgb:
  714. case Format.Astc6x5Unorm:
  715. return FormatClass.Astc6x5;
  716. case Format.Astc6x6Srgb:
  717. case Format.Astc6x6Unorm:
  718. return FormatClass.Astc6x6;
  719. case Format.Astc8x5Srgb:
  720. case Format.Astc8x5Unorm:
  721. return FormatClass.Astc8x5;
  722. case Format.Astc8x6Srgb:
  723. case Format.Astc8x6Unorm:
  724. return FormatClass.Astc8x6;
  725. case Format.Astc8x8Srgb:
  726. case Format.Astc8x8Unorm:
  727. return FormatClass.Astc8x8;
  728. case Format.Astc10x5Srgb:
  729. case Format.Astc10x5Unorm:
  730. return FormatClass.Astc10x5;
  731. case Format.Astc10x6Srgb:
  732. case Format.Astc10x6Unorm:
  733. return FormatClass.Astc10x6;
  734. case Format.Astc10x8Srgb:
  735. case Format.Astc10x8Unorm:
  736. return FormatClass.Astc10x8;
  737. case Format.Astc10x10Srgb:
  738. case Format.Astc10x10Unorm:
  739. return FormatClass.Astc10x10;
  740. case Format.Astc12x10Srgb:
  741. case Format.Astc12x10Unorm:
  742. return FormatClass.Astc12x10;
  743. case Format.Astc12x12Srgb:
  744. case Format.Astc12x12Unorm:
  745. return FormatClass.Astc12x12;
  746. }
  747. return FormatClass.Unclassified;
  748. }
  749. }
  750. }