FormatCapabilities.cs 8.1 KB

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