FormatCapabilities.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Silk.NET.Vulkan;
  4. using System;
  5. using Format = Ryujinx.Graphics.GAL.Format;
  6. using VkFormat = Silk.NET.Vulkan.Format;
  7. namespace Ryujinx.Graphics.Vulkan
  8. {
  9. class FormatCapabilities
  10. {
  11. private static readonly GAL.Format[] _scaledFormats =
  12. [
  13. GAL.Format.R8Uscaled,
  14. GAL.Format.R8Sscaled,
  15. GAL.Format.R16Uscaled,
  16. GAL.Format.R16Sscaled,
  17. GAL.Format.R8G8Uscaled,
  18. GAL.Format.R8G8Sscaled,
  19. GAL.Format.R16G16Uscaled,
  20. GAL.Format.R16G16Sscaled,
  21. GAL.Format.R8G8B8Uscaled,
  22. GAL.Format.R8G8B8Sscaled,
  23. GAL.Format.R16G16B16Uscaled,
  24. GAL.Format.R16G16B16Sscaled,
  25. GAL.Format.R8G8B8A8Uscaled,
  26. GAL.Format.R8G8B8A8Sscaled,
  27. GAL.Format.R16G16B16A16Uscaled,
  28. GAL.Format.R16G16B16A16Sscaled,
  29. GAL.Format.R10G10B10A2Uscaled,
  30. GAL.Format.R10G10B10A2Sscaled
  31. ];
  32. private static readonly GAL.Format[] _intFormats =
  33. [
  34. GAL.Format.R8Uint,
  35. GAL.Format.R8Sint,
  36. GAL.Format.R16Uint,
  37. GAL.Format.R16Sint,
  38. GAL.Format.R8G8Uint,
  39. GAL.Format.R8G8Sint,
  40. GAL.Format.R16G16Uint,
  41. GAL.Format.R16G16Sint,
  42. GAL.Format.R8G8B8Uint,
  43. GAL.Format.R8G8B8Sint,
  44. GAL.Format.R16G16B16Uint,
  45. GAL.Format.R16G16B16Sint,
  46. GAL.Format.R8G8B8A8Uint,
  47. GAL.Format.R8G8B8A8Sint,
  48. GAL.Format.R16G16B16A16Uint,
  49. GAL.Format.R16G16B16A16Sint,
  50. GAL.Format.R10G10B10A2Uint,
  51. GAL.Format.R10G10B10A2Sint
  52. ];
  53. private readonly FormatFeatureFlags[] _bufferTable;
  54. private readonly FormatFeatureFlags[] _optimalTable;
  55. private readonly Vk _api;
  56. private readonly PhysicalDevice _physicalDevice;
  57. public FormatCapabilities(Vk api, PhysicalDevice physicalDevice)
  58. {
  59. _api = api;
  60. _physicalDevice = physicalDevice;
  61. int totalFormats = Enum.GetNames<Format>().Length;
  62. _bufferTable = new FormatFeatureFlags[totalFormats];
  63. _optimalTable = new FormatFeatureFlags[totalFormats];
  64. }
  65. public bool BufferFormatsSupport(FormatFeatureFlags flags, params ReadOnlySpan<Format> formats)
  66. {
  67. foreach (Format format in formats)
  68. {
  69. if (!BufferFormatSupports(flags, format))
  70. {
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. public bool OptimalFormatsSupport(FormatFeatureFlags flags, params ReadOnlySpan<Format> formats)
  77. {
  78. foreach (Format format in formats)
  79. {
  80. if (!OptimalFormatSupports(flags, format))
  81. {
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. public bool BufferFormatSupports(FormatFeatureFlags flags, Format format)
  88. {
  89. FormatFeatureFlags formatFeatureFlags = _bufferTable[(int)format];
  90. if (formatFeatureFlags == 0)
  91. {
  92. _api.GetPhysicalDeviceFormatProperties(_physicalDevice, FormatTable.GetFormat(format), out FormatProperties fp);
  93. formatFeatureFlags = fp.BufferFeatures;
  94. _bufferTable[(int)format] = formatFeatureFlags;
  95. }
  96. return (formatFeatureFlags & flags) == flags;
  97. }
  98. public bool SupportsScaledVertexFormats()
  99. {
  100. // We want to check is all scaled formats are supported,
  101. // but if the integer variant is not supported either,
  102. // then the format is likely not supported at all,
  103. // we ignore formats that are entirely unsupported here.
  104. for (int i = 0; i < _scaledFormats.Length; i++)
  105. {
  106. if (!BufferFormatSupports(FormatFeatureFlags.VertexBufferBit, _scaledFormats[i]) &&
  107. BufferFormatSupports(FormatFeatureFlags.VertexBufferBit, _intFormats[i]))
  108. {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. public bool BufferFormatSupports(FormatFeatureFlags flags, VkFormat format)
  115. {
  116. _api.GetPhysicalDeviceFormatProperties(_physicalDevice, format, out FormatProperties fp);
  117. return (fp.BufferFeatures & flags) == flags;
  118. }
  119. public bool OptimalFormatSupports(FormatFeatureFlags flags, Format format)
  120. {
  121. FormatFeatureFlags formatFeatureFlags = _optimalTable[(int)format];
  122. if (formatFeatureFlags == 0)
  123. {
  124. _api.GetPhysicalDeviceFormatProperties(_physicalDevice, FormatTable.GetFormat(format), out FormatProperties fp);
  125. formatFeatureFlags = fp.OptimalTilingFeatures;
  126. _optimalTable[(int)format] = formatFeatureFlags;
  127. }
  128. return (formatFeatureFlags & flags) == flags;
  129. }
  130. public VkFormat ConvertToVkFormat(Format srcFormat, bool storageFeatureFlagRequired)
  131. {
  132. VkFormat format = FormatTable.GetFormat(srcFormat);
  133. FormatFeatureFlags requiredFeatures = FormatFeatureFlags.SampledImageBit |
  134. FormatFeatureFlags.TransferSrcBit |
  135. FormatFeatureFlags.TransferDstBit;
  136. if (srcFormat.IsDepthOrStencil())
  137. {
  138. requiredFeatures |= FormatFeatureFlags.DepthStencilAttachmentBit;
  139. }
  140. else if (srcFormat.IsRtColorCompatible())
  141. {
  142. requiredFeatures |= FormatFeatureFlags.ColorAttachmentBit;
  143. }
  144. if (srcFormat.IsImageCompatible() && storageFeatureFlagRequired)
  145. {
  146. requiredFeatures |= FormatFeatureFlags.StorageImageBit;
  147. }
  148. if (!OptimalFormatSupports(requiredFeatures, srcFormat) || (IsD24S8(srcFormat) && VulkanConfiguration.ForceD24S8Unsupported))
  149. {
  150. // The format is not supported. Can we convert it to a higher precision format?
  151. if (IsD24S8(srcFormat))
  152. {
  153. format = VkFormat.D32SfloatS8Uint;
  154. }
  155. else if (srcFormat == Format.R4G4B4A4Unorm)
  156. {
  157. format = VkFormat.R4G4B4A4UnormPack16;
  158. }
  159. else
  160. {
  161. Logger.Error?.Print(LogClass.Gpu, $"Format {srcFormat} is not supported by the host.");
  162. }
  163. }
  164. return format;
  165. }
  166. public VkFormat ConvertToVertexVkFormat(Format srcFormat)
  167. {
  168. VkFormat format = FormatTable.GetFormat(srcFormat);
  169. if (!BufferFormatSupports(FormatFeatureFlags.VertexBufferBit, srcFormat) ||
  170. (IsRGB16IntFloat(srcFormat) && VulkanConfiguration.ForceRGB16IntFloatUnsupported))
  171. {
  172. // The format is not supported. Can we convert it to an alternative format?
  173. switch (srcFormat)
  174. {
  175. case Format.R16G16B16Float:
  176. format = VkFormat.R16G16B16A16Sfloat;
  177. break;
  178. case Format.R16G16B16Sint:
  179. format = VkFormat.R16G16B16A16Sint;
  180. break;
  181. case Format.R16G16B16Uint:
  182. format = VkFormat.R16G16B16A16Uint;
  183. break;
  184. default:
  185. Logger.Error?.Print(LogClass.Gpu, $"Format {srcFormat} is not supported by the host.");
  186. break;
  187. }
  188. }
  189. return format;
  190. }
  191. public static bool IsD24S8(Format format)
  192. {
  193. return format == Format.D24UnormS8Uint || format == Format.S8UintD24Unorm || format == Format.X8UintD24Unorm;
  194. }
  195. private static bool IsRGB16IntFloat(Format format)
  196. {
  197. return format == Format.R16G16B16Float ||
  198. format == Format.R16G16B16Sint ||
  199. format == Format.R16G16B16Uint;
  200. }
  201. }
  202. }