TextureCompatibility.cs 40 KB

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