TextureCompatibility.cs 39 KB

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