TextureCompatibility.cs 36 KB

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