TextureCompatibility.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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="forCopy">Indicates that the texture will be used as copy source or target</param>
  208. /// <returns>A value indicating how well the formats match</returns>
  209. public static TextureMatchQuality FormatMatches(TextureInfo lhs, TextureInfo rhs, bool forSampler, bool forCopy)
  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 (lhs.FormatInfo.Format == Format.D32Float && rhs.FormatInfo.Format == Format.R32Float && (forSampler || forCopy))
  214. {
  215. return TextureMatchQuality.FormatAlias;
  216. }
  217. if (forCopy)
  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. if (lhs.FormatInfo.Format == Format.D16Unorm && rhs.FormatInfo.Format == Format.R16Unorm)
  226. {
  227. return TextureMatchQuality.FormatAlias;
  228. }
  229. if ((lhs.FormatInfo.Format == Format.D24UnormS8Uint ||
  230. lhs.FormatInfo.Format == Format.S8UintD24Unorm) && rhs.FormatInfo.Format == Format.B8G8R8A8Unorm)
  231. {
  232. return TextureMatchQuality.FormatAlias;
  233. }
  234. }
  235. return lhs.FormatInfo.Format == rhs.FormatInfo.Format ? TextureMatchQuality.Perfect : TextureMatchQuality.NoMatch;
  236. }
  237. /// <summary>
  238. /// Checks if the texture layout specified matches with this texture layout.
  239. /// The layout information is composed of the Stride for linear textures, or GOB block size
  240. /// for block linear textures.
  241. /// </summary>
  242. /// <param name="lhs">Texture information to compare</param>
  243. /// <param name="rhs">Texture information to compare with</param>
  244. /// <returns>True if the layout matches, false otherwise</returns>
  245. public static bool LayoutMatches(TextureInfo lhs, TextureInfo rhs)
  246. {
  247. if (lhs.IsLinear != rhs.IsLinear)
  248. {
  249. return false;
  250. }
  251. // For linear textures, gob block sizes are ignored.
  252. // For block linear textures, the stride is ignored.
  253. if (rhs.IsLinear)
  254. {
  255. return lhs.Stride == rhs.Stride;
  256. }
  257. else
  258. {
  259. return lhs.GobBlocksInY == rhs.GobBlocksInY &&
  260. lhs.GobBlocksInZ == rhs.GobBlocksInZ;
  261. }
  262. }
  263. /// <summary>
  264. /// Obtain the minimum compatibility level of two provided view compatibility results.
  265. /// </summary>
  266. /// <param name="first">The first compatibility level</param>
  267. /// <param name="second">The second compatibility level</param>
  268. /// <returns>The minimum compatibility level of two provided view compatibility results</returns>
  269. public static TextureViewCompatibility PropagateViewCompatibility(TextureViewCompatibility first, TextureViewCompatibility second)
  270. {
  271. if (first == TextureViewCompatibility.Incompatible || second == TextureViewCompatibility.Incompatible)
  272. {
  273. return TextureViewCompatibility.Incompatible;
  274. }
  275. else if (first == TextureViewCompatibility.LayoutIncompatible || second == TextureViewCompatibility.LayoutIncompatible)
  276. {
  277. return TextureViewCompatibility.LayoutIncompatible;
  278. }
  279. else if (first == TextureViewCompatibility.CopyOnly || second == TextureViewCompatibility.CopyOnly)
  280. {
  281. return TextureViewCompatibility.CopyOnly;
  282. }
  283. else
  284. {
  285. return TextureViewCompatibility.Full;
  286. }
  287. }
  288. /// <summary>
  289. /// Checks if the sizes of two texture levels are copy compatible.
  290. /// </summary>
  291. /// <param name="lhs">Texture information of the texture view</param>
  292. /// <param name="rhs">Texture information of the texture view to match against</param>
  293. /// <param name="lhsLevel">Mipmap level of the texture view in relation to this texture</param>
  294. /// <param name="rhsLevel">Mipmap level of the texture view in relation to the second texture</param>
  295. /// <returns>True if both levels are view compatible</returns>
  296. public static bool CopySizeMatches(TextureInfo lhs, TextureInfo rhs, int lhsLevel, int rhsLevel)
  297. {
  298. Size size = GetAlignedSize(lhs, lhsLevel);
  299. Size otherSize = GetAlignedSize(rhs, rhsLevel);
  300. if (size.Width == otherSize.Width && size.Height == otherSize.Height)
  301. {
  302. return true;
  303. }
  304. else if (lhs.IsLinear && rhs.IsLinear)
  305. {
  306. // Copy between linear textures with matching stride.
  307. int stride = BitUtils.AlignUp(Math.Max(1, lhs.Stride >> lhsLevel), Constants.StrideAlignment);
  308. return stride == rhs.Stride;
  309. }
  310. else
  311. {
  312. return false;
  313. }
  314. }
  315. /// <summary>
  316. /// Checks if the sizes of two given textures are view compatible.
  317. /// </summary>
  318. /// <param name="lhs">Texture information of the texture view</param>
  319. /// <param name="rhs">Texture information of the texture view to match against</param>
  320. /// <param name="exact">Indicates if the sizes must be exactly equal</param>
  321. /// <param name="level">Mipmap level of the texture view in relation to this texture</param>
  322. /// <returns>The view compatibility level of the view sizes</returns>
  323. public static TextureViewCompatibility ViewSizeMatches(TextureInfo lhs, TextureInfo rhs, bool exact, int level)
  324. {
  325. Size lhsAlignedSize = GetAlignedSize(lhs, level);
  326. Size rhsAlignedSize = GetAlignedSize(rhs);
  327. Size lhsSize = GetSizeInBlocks(lhs, level);
  328. Size rhsSize = GetSizeInBlocks(rhs);
  329. bool alignedWidthMatches = lhsAlignedSize.Width == rhsAlignedSize.Width;
  330. if (lhs.FormatInfo.BytesPerPixel != rhs.FormatInfo.BytesPerPixel && IsIncompatibleFormatAliasingAllowed(lhs.FormatInfo, rhs.FormatInfo))
  331. {
  332. alignedWidthMatches = lhsSize.Width * lhs.FormatInfo.BytesPerPixel == rhsSize.Width * rhs.FormatInfo.BytesPerPixel;
  333. }
  334. TextureViewCompatibility result = TextureViewCompatibility.Full;
  335. // For copies, we can copy a subset of the 3D texture slices,
  336. // so the depth may be different in this case.
  337. if (rhs.Target == Target.Texture3D && lhsSize.Depth != rhsSize.Depth)
  338. {
  339. result = TextureViewCompatibility.CopyOnly;
  340. }
  341. // Some APIs align the width for copy and render target textures,
  342. // so the width may not match in this case for different uses of the same texture.
  343. // To account for this, we compare the aligned width here.
  344. // We expect height to always match exactly, if the texture is the same.
  345. if (alignedWidthMatches && lhsSize.Height == rhsSize.Height)
  346. {
  347. return (exact && lhsSize.Width != rhsSize.Width) || lhsSize.Width < rhsSize.Width
  348. ? TextureViewCompatibility.CopyOnly
  349. : result;
  350. }
  351. else if (lhs.IsLinear && rhs.IsLinear && lhsSize.Height == rhsSize.Height)
  352. {
  353. // Copy between linear textures with matching stride.
  354. int stride = BitUtils.AlignUp(Math.Max(1, lhs.Stride >> level), Constants.StrideAlignment);
  355. return stride == rhs.Stride ? TextureViewCompatibility.CopyOnly : TextureViewCompatibility.LayoutIncompatible;
  356. }
  357. else
  358. {
  359. return TextureViewCompatibility.LayoutIncompatible;
  360. }
  361. }
  362. /// <summary>
  363. /// Checks if the potential child texture fits within the level and layer bounds of the parent.
  364. /// </summary>
  365. /// <param name="parent">Texture information for the parent</param>
  366. /// <param name="child">Texture information for the child</param>
  367. /// <param name="layer">Base layer of the child texture</param>
  368. /// <param name="level">Base level of the child texture</param>
  369. /// <returns>Full compatiblity if the child's layer and level count fit within the parent, incompatible otherwise</returns>
  370. public static TextureViewCompatibility ViewSubImagesInBounds(TextureInfo parent, TextureInfo child, int layer, int level)
  371. {
  372. if (level + child.Levels <= parent.Levels &&
  373. layer + child.GetSlices() <= parent.GetSlices())
  374. {
  375. return TextureViewCompatibility.Full;
  376. }
  377. else
  378. {
  379. return TextureViewCompatibility.LayoutIncompatible;
  380. }
  381. }
  382. /// <summary>
  383. /// Checks if the texture sizes of the supplied texture informations match.
  384. /// </summary>
  385. /// <param name="lhs">Texture information to compare</param>
  386. /// <param name="rhs">Texture information to compare with</param>
  387. /// <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>
  388. /// <returns>True if the sizes matches, false otherwise</returns>
  389. public static bool SizeMatches(TextureInfo lhs, TextureInfo rhs, bool exact)
  390. {
  391. if (lhs.GetLayers() != rhs.GetLayers())
  392. {
  393. return false;
  394. }
  395. Size lhsSize = GetSizeInBlocks(lhs);
  396. Size rhsSize = GetSizeInBlocks(rhs);
  397. if (exact || lhs.IsLinear || rhs.IsLinear)
  398. {
  399. return lhsSize.Width == rhsSize.Width &&
  400. lhsSize.Height == rhsSize.Height &&
  401. lhsSize.Depth == rhsSize.Depth;
  402. }
  403. else
  404. {
  405. Size lhsAlignedSize = GetAlignedSize(lhs);
  406. Size rhsAlignedSize = GetAlignedSize(rhs);
  407. return lhsAlignedSize.Width == rhsAlignedSize.Width &&
  408. lhsSize.Width >= rhsSize.Width &&
  409. lhsSize.Height == rhsSize.Height &&
  410. lhsSize.Depth == rhsSize.Depth;
  411. }
  412. }
  413. /// <summary>
  414. /// Gets the aligned sizes for the given dimensions, using the specified texture information.
  415. /// The alignment depends on the texture layout and format bytes per pixel.
  416. /// </summary>
  417. /// <param name="info">Texture information to calculate the aligned size from</param>
  418. /// <param name="width">The width to be aligned</param>
  419. /// <param name="height">The height to be aligned</param>
  420. /// <param name="depth">The depth to be aligned</param>
  421. /// <returns>The aligned texture size</returns>
  422. private static Size GetAlignedSize(TextureInfo info, int width, int height, int depth)
  423. {
  424. if (info.IsLinear)
  425. {
  426. return SizeCalculator.GetLinearAlignedSize(
  427. width,
  428. height,
  429. info.FormatInfo.BlockWidth,
  430. info.FormatInfo.BlockHeight,
  431. info.FormatInfo.BytesPerPixel);
  432. }
  433. else
  434. {
  435. return SizeCalculator.GetBlockLinearAlignedSize(
  436. width,
  437. height,
  438. depth,
  439. info.FormatInfo.BlockWidth,
  440. info.FormatInfo.BlockHeight,
  441. info.FormatInfo.BytesPerPixel,
  442. info.GobBlocksInY,
  443. info.GobBlocksInZ,
  444. info.GobBlocksInTileX);
  445. }
  446. }
  447. /// <summary>
  448. /// Gets the aligned sizes of the specified texture information.
  449. /// The alignment depends on the texture layout and format bytes per pixel.
  450. /// </summary>
  451. /// <param name="info">Texture information to calculate the aligned size from</param>
  452. /// <param name="level">Mipmap level for texture views</param>
  453. /// <returns>The aligned texture size</returns>
  454. public static Size GetAlignedSize(TextureInfo info, int level = 0)
  455. {
  456. int width = Math.Max(1, info.Width >> level);
  457. int height = Math.Max(1, info.Height >> level);
  458. int depth = Math.Max(1, info.GetDepth() >> level);
  459. return GetAlignedSize(info, width, height, depth);
  460. }
  461. /// <summary>
  462. /// Gets the size in blocks for the given texture information.
  463. /// For non-compressed formats, that's the same as the regular size.
  464. /// </summary>
  465. /// <param name="info">Texture information to calculate the aligned size from</param>
  466. /// <param name="level">Mipmap level for texture views</param>
  467. /// <returns>The texture size in blocks</returns>
  468. public static Size GetSizeInBlocks(TextureInfo info, int level = 0)
  469. {
  470. int width = Math.Max(1, info.Width >> level);
  471. int height = Math.Max(1, info.Height >> level);
  472. int depth = Math.Max(1, info.GetDepth() >> level);
  473. return new Size(
  474. BitUtils.DivRoundUp(width, info.FormatInfo.BlockWidth),
  475. BitUtils.DivRoundUp(height, info.FormatInfo.BlockHeight),
  476. depth);
  477. }
  478. /// <summary>
  479. /// Check if it's possible to create a view with the layout of the second texture information from the first.
  480. /// The layout information is composed of the Stride for linear textures, or GOB block size
  481. /// for block linear textures.
  482. /// </summary>
  483. /// <param name="lhs">Texture information of the texture view</param>
  484. /// <param name="rhs">Texture information of the texture view to compare against</param>
  485. /// <param name="level">Start level of the texture view, in relation with the first texture</param>
  486. /// <returns>True if the layout is compatible, false otherwise</returns>
  487. public static bool ViewLayoutCompatible(TextureInfo lhs, TextureInfo rhs, int level)
  488. {
  489. if (lhs.IsLinear != rhs.IsLinear)
  490. {
  491. return false;
  492. }
  493. // For linear textures, gob block sizes are ignored.
  494. // For block linear textures, the stride is ignored.
  495. if (rhs.IsLinear)
  496. {
  497. int stride = Math.Max(1, lhs.Stride >> level);
  498. stride = BitUtils.AlignUp(stride, Constants.StrideAlignment);
  499. return stride == rhs.Stride;
  500. }
  501. else
  502. {
  503. int height = Math.Max(1, lhs.Height >> level);
  504. int depth = Math.Max(1, lhs.GetDepth() >> level);
  505. (int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  506. height,
  507. depth,
  508. lhs.FormatInfo.BlockHeight,
  509. lhs.GobBlocksInY,
  510. lhs.GobBlocksInZ);
  511. return gobBlocksInY == rhs.GobBlocksInY &&
  512. gobBlocksInZ == rhs.GobBlocksInZ;
  513. }
  514. }
  515. /// <summary>
  516. /// Check if it's possible to create a view with the layout of the second texture information from the first.
  517. /// The layout information is composed of the Stride for linear textures, or GOB block size
  518. /// for block linear textures.
  519. /// </summary>
  520. /// <param name="lhs">Texture information of the texture view</param>
  521. /// <param name="rhs">Texture information of the texture view to compare against</param>
  522. /// <param name="lhsLevel">Start level of the texture view, in relation with the first texture</param>
  523. /// <param name="rhsLevel">Start level of the texture view, in relation with the second texture</param>
  524. /// <returns>True if the layout is compatible, false otherwise</returns>
  525. public static bool ViewLayoutCompatible(TextureInfo lhs, TextureInfo rhs, int lhsLevel, int rhsLevel)
  526. {
  527. if (lhs.IsLinear != rhs.IsLinear)
  528. {
  529. return false;
  530. }
  531. // For linear textures, gob block sizes are ignored.
  532. // For block linear textures, the stride is ignored.
  533. if (rhs.IsLinear)
  534. {
  535. int lhsStride = Math.Max(1, lhs.Stride >> lhsLevel);
  536. lhsStride = BitUtils.AlignUp(lhsStride, Constants.StrideAlignment);
  537. int rhsStride = Math.Max(1, rhs.Stride >> rhsLevel);
  538. rhsStride = BitUtils.AlignUp(rhsStride, Constants.StrideAlignment);
  539. return lhsStride == rhsStride;
  540. }
  541. else
  542. {
  543. int lhsHeight = Math.Max(1, lhs.Height >> lhsLevel);
  544. int lhsDepth = Math.Max(1, lhs.GetDepth() >> lhsLevel);
  545. (int lhsGobBlocksInY, int lhsGobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  546. lhsHeight,
  547. lhsDepth,
  548. lhs.FormatInfo.BlockHeight,
  549. lhs.GobBlocksInY,
  550. lhs.GobBlocksInZ);
  551. int rhsHeight = Math.Max(1, rhs.Height >> rhsLevel);
  552. int rhsDepth = Math.Max(1, rhs.GetDepth() >> rhsLevel);
  553. (int rhsGobBlocksInY, int rhsGobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  554. rhsHeight,
  555. rhsDepth,
  556. rhs.FormatInfo.BlockHeight,
  557. rhs.GobBlocksInY,
  558. rhs.GobBlocksInZ);
  559. return lhsGobBlocksInY == rhsGobBlocksInY &&
  560. lhsGobBlocksInZ == rhsGobBlocksInZ;
  561. }
  562. }
  563. /// <summary>
  564. /// Checks if the view format of the first texture format is compatible with the format of the second.
  565. /// In general, the formats are considered compatible if the bytes per pixel values are equal,
  566. /// but there are more complex rules for some formats, like compressed or depth-stencil formats.
  567. /// This follows the host API copy compatibility rules.
  568. /// </summary>
  569. /// <param name="lhs">Texture information of the texture view</param>
  570. /// <param name="rhs">Texture information of the texture view</param>
  571. /// <param name="caps">Host GPU capabilities</param>
  572. /// <returns>The view compatibility level of the texture formats</returns>
  573. public static TextureViewCompatibility ViewFormatCompatible(TextureInfo lhs, TextureInfo rhs, Capabilities caps)
  574. {
  575. FormatInfo lhsFormat = lhs.FormatInfo;
  576. FormatInfo rhsFormat = rhs.FormatInfo;
  577. if (lhsFormat.Format.IsDepthOrStencil() || rhsFormat.Format.IsDepthOrStencil())
  578. {
  579. return lhsFormat.Format == rhsFormat.Format ? TextureViewCompatibility.Full : TextureViewCompatibility.Incompatible;
  580. }
  581. if (IsFormatHostIncompatible(lhs, caps) || IsFormatHostIncompatible(rhs, caps))
  582. {
  583. return lhsFormat.Format == rhsFormat.Format ? TextureViewCompatibility.Full : TextureViewCompatibility.Incompatible;
  584. }
  585. if (lhsFormat.IsCompressed && rhsFormat.IsCompressed)
  586. {
  587. FormatClass lhsClass = GetFormatClass(lhsFormat.Format);
  588. FormatClass rhsClass = GetFormatClass(rhsFormat.Format);
  589. return lhsClass == rhsClass ? TextureViewCompatibility.Full : TextureViewCompatibility.Incompatible;
  590. }
  591. else if (lhsFormat.BytesPerPixel == rhsFormat.BytesPerPixel)
  592. {
  593. return lhs.FormatInfo.IsCompressed == rhs.FormatInfo.IsCompressed
  594. ? TextureViewCompatibility.Full
  595. : TextureViewCompatibility.CopyOnly;
  596. }
  597. else if (IsIncompatibleFormatAliasingAllowed(lhsFormat, rhsFormat))
  598. {
  599. return TextureViewCompatibility.CopyOnly;
  600. }
  601. return TextureViewCompatibility.Incompatible;
  602. }
  603. /// <summary>
  604. /// Checks if aliasing of two formats that would normally be considered incompatible be allowed,
  605. /// using copy dependencies.
  606. /// </summary>
  607. /// <param name="lhsFormat">Format information of the first texture</param
  608. /// <param name="rhsFormat">Format information of the second texture</param>
  609. /// <returns>True if aliasing should be allowed, false otherwise</returns>
  610. private static bool IsIncompatibleFormatAliasingAllowed(FormatInfo lhsFormat, FormatInfo rhsFormat)
  611. {
  612. // Some games will try to alias textures with incompatible foramts, with different BPP (bytes per pixel).
  613. // We allow that in some cases as long Width * BPP is equal on both textures.
  614. // This is very conservative right now as we want to avoid copies as much as possible,
  615. // so we only consider the formats we have seen being aliased.
  616. if (rhsFormat.BytesPerPixel < lhsFormat.BytesPerPixel)
  617. {
  618. (lhsFormat, rhsFormat) = (rhsFormat, lhsFormat);
  619. }
  620. return lhsFormat.Format == Format.R8Unorm && rhsFormat.Format == Format.R8G8B8A8Unorm;
  621. }
  622. /// <summary>
  623. /// Check if the target of the first texture view information is compatible with the target of the second texture view information.
  624. /// This follows the host API target compatibility rules.
  625. /// </summary>
  626. /// <param name="lhs">Texture information of the texture view</param
  627. /// <param name="rhs">Texture information of the texture view</param>
  628. /// <param name="caps">Host GPU capabilities</param>
  629. /// <returns>True if the targets are compatible, false otherwise</returns>
  630. public static TextureViewCompatibility ViewTargetCompatible(TextureInfo lhs, TextureInfo rhs, ref Capabilities caps)
  631. {
  632. bool result = false;
  633. switch (lhs.Target)
  634. {
  635. case Target.Texture1D:
  636. case Target.Texture1DArray:
  637. result = rhs.Target == Target.Texture1D ||
  638. rhs.Target == Target.Texture1DArray;
  639. break;
  640. case Target.Texture2D:
  641. result = rhs.Target == Target.Texture2D ||
  642. rhs.Target == Target.Texture2DArray;
  643. break;
  644. case Target.Texture2DArray:
  645. result = rhs.Target == Target.Texture2D ||
  646. rhs.Target == Target.Texture2DArray;
  647. if (rhs.Target == Target.Cubemap || rhs.Target == Target.CubemapArray)
  648. {
  649. return caps.SupportsCubemapView ? TextureViewCompatibility.Full : TextureViewCompatibility.CopyOnly;
  650. }
  651. break;
  652. case Target.Cubemap:
  653. case Target.CubemapArray:
  654. result = rhs.Target == Target.Cubemap ||
  655. rhs.Target == Target.CubemapArray;
  656. if (rhs.Target == Target.Texture2D || rhs.Target == Target.Texture2DArray)
  657. {
  658. return caps.SupportsCubemapView ? TextureViewCompatibility.Full : TextureViewCompatibility.CopyOnly;
  659. }
  660. break;
  661. case Target.Texture2DMultisample:
  662. case Target.Texture2DMultisampleArray:
  663. // We don't support copy between multisample and non-multisample depth-stencil textures
  664. // because there's no way to emulate that since most GPUs don't support writing a
  665. // custom stencil value into the texture, among several other API limitations.
  666. if ((rhs.Target == Target.Texture2D || rhs.Target == Target.Texture2DArray) &&
  667. !rhs.FormatInfo.Format.IsDepthOrStencil())
  668. {
  669. return TextureViewCompatibility.CopyOnly;
  670. }
  671. result = rhs.Target == Target.Texture2DMultisample ||
  672. rhs.Target == Target.Texture2DMultisampleArray;
  673. break;
  674. case Target.Texture3D:
  675. if (rhs.Target == Target.Texture2D)
  676. {
  677. return TextureViewCompatibility.CopyOnly;
  678. }
  679. result = rhs.Target == Target.Texture3D;
  680. break;
  681. }
  682. return result ? TextureViewCompatibility.Full : TextureViewCompatibility.Incompatible;
  683. }
  684. /// <summary>
  685. /// Checks if a swizzle component in two textures functionally match, taking into account if the components are defined.
  686. /// </summary>
  687. /// <param name="lhs">Texture information to compare</param>
  688. /// <param name="rhs">Texture information to compare with</param>
  689. /// <param name="swizzleLhs">Swizzle component for the first texture</param>
  690. /// <param name="swizzleRhs">Swizzle component for the second texture</param>
  691. /// <param name="component">Component index, starting at 0 for red</param>
  692. /// <returns>True if the swizzle components functionally match, false othersize</returns>
  693. private static bool SwizzleComponentMatches(TextureInfo lhs, TextureInfo rhs, SwizzleComponent swizzleLhs, SwizzleComponent swizzleRhs, int component)
  694. {
  695. int lhsComponents = lhs.FormatInfo.Components;
  696. int rhsComponents = rhs.FormatInfo.Components;
  697. if (lhsComponents == 4 && rhsComponents == 4)
  698. {
  699. return swizzleLhs == swizzleRhs;
  700. }
  701. // Swizzles after the number of components a format defines are "undefined".
  702. // We allow these to not be equal under certain circumstances.
  703. // This can only happen when there are less than 4 components in a format.
  704. // It tends to happen when float depth textures are sampled.
  705. bool lhsDefined = (swizzleLhs - SwizzleComponent.Red) < lhsComponents;
  706. bool rhsDefined = (swizzleRhs - SwizzleComponent.Red) < rhsComponents;
  707. if (lhsDefined == rhsDefined)
  708. {
  709. // If both are undefined, return true. Otherwise just check if they're equal.
  710. return lhsDefined ? swizzleLhs == swizzleRhs : true;
  711. }
  712. else
  713. {
  714. SwizzleComponent defined = lhsDefined ? swizzleLhs : swizzleRhs;
  715. SwizzleComponent undefined = lhsDefined ? swizzleRhs : swizzleLhs;
  716. // Undefined swizzle can be matched by a forced value (0, 1), exact equality, or expected value.
  717. // For example, R___ matches R001, RGBA but not RBGA.
  718. return defined == undefined || defined < SwizzleComponent.Red || defined == SwizzleComponent.Red + component;
  719. }
  720. }
  721. /// <summary>
  722. /// Checks if the texture shader sampling parameters of two texture informations match.
  723. /// </summary>
  724. /// <param name="lhs">Texture information to compare</param>
  725. /// <param name="rhs">Texture information to compare with</param>
  726. /// <returns>True if the texture shader sampling parameters matches, false otherwise</returns>
  727. public static bool SamplerParamsMatches(TextureInfo lhs, TextureInfo rhs)
  728. {
  729. return lhs.DepthStencilMode == rhs.DepthStencilMode &&
  730. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleR, rhs.SwizzleR, 0) &&
  731. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleG, rhs.SwizzleG, 1) &&
  732. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleB, rhs.SwizzleB, 2) &&
  733. SwizzleComponentMatches(lhs, rhs, lhs.SwizzleA, rhs.SwizzleA, 3);
  734. }
  735. /// <summary>
  736. /// Check if the texture target and samples count (for multisampled textures) matches.
  737. /// </summary>
  738. /// <param name="first">Texture information to compare with</param>
  739. /// <param name="rhs">Texture information to compare with</param>
  740. /// <returns>True if the texture target and samples count matches, false otherwise</returns>
  741. public static bool TargetAndSamplesCompatible(TextureInfo lhs, TextureInfo rhs)
  742. {
  743. return lhs.Target == rhs.Target &&
  744. lhs.SamplesInX == rhs.SamplesInX &&
  745. lhs.SamplesInY == rhs.SamplesInY;
  746. }
  747. /// <summary>
  748. /// Gets the texture format class, for compressed textures, or Unclassified otherwise.
  749. /// </summary>
  750. /// <param name="format">The format</param>
  751. /// <returns>Format class</returns>
  752. private static FormatClass GetFormatClass(Format format)
  753. {
  754. switch (format)
  755. {
  756. case Format.Bc1RgbaSrgb:
  757. case Format.Bc1RgbaUnorm:
  758. return FormatClass.Bc1Rgba;
  759. case Format.Bc2Srgb:
  760. case Format.Bc2Unorm:
  761. return FormatClass.Bc2;
  762. case Format.Bc3Srgb:
  763. case Format.Bc3Unorm:
  764. return FormatClass.Bc3;
  765. case Format.Bc4Snorm:
  766. case Format.Bc4Unorm:
  767. return FormatClass.Bc4;
  768. case Format.Bc5Snorm:
  769. case Format.Bc5Unorm:
  770. return FormatClass.Bc5;
  771. case Format.Bc6HSfloat:
  772. case Format.Bc6HUfloat:
  773. return FormatClass.Bc6;
  774. case Format.Bc7Srgb:
  775. case Format.Bc7Unorm:
  776. return FormatClass.Bc7;
  777. case Format.Etc2RgbSrgb:
  778. case Format.Etc2RgbUnorm:
  779. return FormatClass.Etc2Rgb;
  780. case Format.Etc2RgbaSrgb:
  781. case Format.Etc2RgbaUnorm:
  782. return FormatClass.Etc2Rgba;
  783. case Format.Astc4x4Srgb:
  784. case Format.Astc4x4Unorm:
  785. return FormatClass.Astc4x4;
  786. case Format.Astc5x4Srgb:
  787. case Format.Astc5x4Unorm:
  788. return FormatClass.Astc5x4;
  789. case Format.Astc5x5Srgb:
  790. case Format.Astc5x5Unorm:
  791. return FormatClass.Astc5x5;
  792. case Format.Astc6x5Srgb:
  793. case Format.Astc6x5Unorm:
  794. return FormatClass.Astc6x5;
  795. case Format.Astc6x6Srgb:
  796. case Format.Astc6x6Unorm:
  797. return FormatClass.Astc6x6;
  798. case Format.Astc8x5Srgb:
  799. case Format.Astc8x5Unorm:
  800. return FormatClass.Astc8x5;
  801. case Format.Astc8x6Srgb:
  802. case Format.Astc8x6Unorm:
  803. return FormatClass.Astc8x6;
  804. case Format.Astc8x8Srgb:
  805. case Format.Astc8x8Unorm:
  806. return FormatClass.Astc8x8;
  807. case Format.Astc10x5Srgb:
  808. case Format.Astc10x5Unorm:
  809. return FormatClass.Astc10x5;
  810. case Format.Astc10x6Srgb:
  811. case Format.Astc10x6Unorm:
  812. return FormatClass.Astc10x6;
  813. case Format.Astc10x8Srgb:
  814. case Format.Astc10x8Unorm:
  815. return FormatClass.Astc10x8;
  816. case Format.Astc10x10Srgb:
  817. case Format.Astc10x10Unorm:
  818. return FormatClass.Astc10x10;
  819. case Format.Astc12x10Srgb:
  820. case Format.Astc12x10Unorm:
  821. return FormatClass.Astc12x10;
  822. case Format.Astc12x12Srgb:
  823. case Format.Astc12x12Unorm:
  824. return FormatClass.Astc12x12;
  825. }
  826. return FormatClass.Unclassified;
  827. }
  828. }
  829. }