TextureCompatibility.cs 39 KB

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