TextureCompatibility.cs 36 KB

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