HwCapabilities.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using OpenTK.Graphics.OpenGL;
  2. using System;
  3. namespace Ryujinx.Graphics.OpenGL
  4. {
  5. static class HwCapabilities
  6. {
  7. private static readonly Lazy<bool> _supportsAstcCompression = new Lazy<bool>(() => HasExtension("GL_KHR_texture_compression_astc_ldr"));
  8. private static readonly Lazy<bool> _supportsDrawTexture = new Lazy<bool>(() => HasExtension("GL_NV_draw_texture"));
  9. private static readonly Lazy<bool> _supportsFragmentShaderInterlock = new Lazy<bool>(() => HasExtension("GL_ARB_fragment_shader_interlock"));
  10. private static readonly Lazy<bool> _supportsFragmentShaderOrdering = new Lazy<bool>(() => HasExtension("GL_INTEL_fragment_shader_ordering"));
  11. private static readonly Lazy<bool> _supportsImageLoadFormatted = new Lazy<bool>(() => HasExtension("GL_EXT_shader_image_load_formatted"));
  12. private static readonly Lazy<bool> _supportsIndirectParameters = new Lazy<bool>(() => HasExtension("GL_ARB_indirect_parameters"));
  13. private static readonly Lazy<bool> _supportsParallelShaderCompile = new Lazy<bool>(() => HasExtension("GL_ARB_parallel_shader_compile"));
  14. private static readonly Lazy<bool> _supportsPolygonOffsetClamp = new Lazy<bool>(() => HasExtension("GL_EXT_polygon_offset_clamp"));
  15. private static readonly Lazy<bool> _supportsQuads = new Lazy<bool>(SupportsQuadsCheck);
  16. private static readonly Lazy<bool> _supportsSeamlessCubemapPerTexture = new Lazy<bool>(() => HasExtension("GL_ARB_seamless_cubemap_per_texture"));
  17. private static readonly Lazy<bool> _supportsShaderBallot = new Lazy<bool>(() => HasExtension("GL_ARB_shader_ballot"));
  18. private static readonly Lazy<bool> _supportsTextureShadowLod = new Lazy<bool>(() => HasExtension("GL_EXT_texture_shadow_lod"));
  19. private static readonly Lazy<bool> _supportsViewportSwizzle = new Lazy<bool>(() => HasExtension("GL_NV_viewport_swizzle"));
  20. private static readonly Lazy<int> _maximumComputeSharedMemorySize = new Lazy<int>(() => GetLimit(All.MaxComputeSharedMemorySize));
  21. private static readonly Lazy<int> _storageBufferOffsetAlignment = new Lazy<int>(() => GetLimit(All.ShaderStorageBufferOffsetAlignment));
  22. public enum GpuVendor
  23. {
  24. Unknown,
  25. AmdWindows,
  26. AmdUnix,
  27. IntelWindows,
  28. IntelUnix,
  29. Nvidia
  30. }
  31. private static readonly Lazy<GpuVendor> _gpuVendor = new Lazy<GpuVendor>(GetGpuVendor);
  32. private static bool _isAMD => _gpuVendor.Value == GpuVendor.AmdWindows || _gpuVendor.Value == GpuVendor.AmdUnix;
  33. private static bool _isIntel => _gpuVendor.Value == GpuVendor.IntelWindows || _gpuVendor.Value == GpuVendor.IntelUnix;
  34. public static GpuVendor Vendor => _gpuVendor.Value;
  35. private static Lazy<float> _maxSupportedAnisotropy = new Lazy<float>(GL.GetFloat((GetPName)All.MaxTextureMaxAnisotropy));
  36. public static bool UsePersistentBufferForFlush => _gpuVendor.Value == GpuVendor.AmdWindows || _gpuVendor.Value == GpuVendor.Nvidia;
  37. public static bool SupportsAstcCompression => _supportsAstcCompression.Value;
  38. public static bool SupportsDrawTexture => _supportsDrawTexture.Value;
  39. public static bool SupportsFragmentShaderInterlock => _supportsFragmentShaderInterlock.Value;
  40. public static bool SupportsFragmentShaderOrdering => _supportsFragmentShaderOrdering.Value;
  41. public static bool SupportsImageLoadFormatted => _supportsImageLoadFormatted.Value;
  42. public static bool SupportsIndirectParameters => _supportsIndirectParameters.Value;
  43. public static bool SupportsParallelShaderCompile => _supportsParallelShaderCompile.Value;
  44. public static bool SupportsPolygonOffsetClamp => _supportsPolygonOffsetClamp.Value;
  45. public static bool SupportsQuads => _supportsQuads.Value;
  46. public static bool SupportsSeamlessCubemapPerTexture => _supportsSeamlessCubemapPerTexture.Value;
  47. public static bool SupportsShaderBallot => _supportsShaderBallot.Value;
  48. public static bool SupportsTextureShadowLod => _supportsTextureShadowLod.Value;
  49. public static bool SupportsViewportSwizzle => _supportsViewportSwizzle.Value;
  50. public static bool SupportsMismatchingViewFormat => _gpuVendor.Value != GpuVendor.AmdWindows && _gpuVendor.Value != GpuVendor.IntelWindows;
  51. public static bool SupportsNonConstantTextureOffset => _gpuVendor.Value == GpuVendor.Nvidia;
  52. public static bool RequiresSyncFlush => _gpuVendor.Value == GpuVendor.AmdWindows || _isIntel;
  53. public static int MaximumComputeSharedMemorySize => _maximumComputeSharedMemorySize.Value;
  54. public static int StorageBufferOffsetAlignment => _storageBufferOffsetAlignment.Value;
  55. public static float MaximumSupportedAnisotropy => _maxSupportedAnisotropy.Value;
  56. private static bool HasExtension(string name)
  57. {
  58. int numExtensions = GL.GetInteger(GetPName.NumExtensions);
  59. for (int extension = 0; extension < numExtensions; extension++)
  60. {
  61. if (GL.GetString(StringNameIndexed.Extensions, extension) == name)
  62. {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. private static int GetLimit(All name)
  69. {
  70. return GL.GetInteger((GetPName)name);
  71. }
  72. private static GpuVendor GetGpuVendor()
  73. {
  74. string vendor = GL.GetString(StringName.Vendor).ToLower();
  75. if (vendor == "nvidia corporation")
  76. {
  77. return GpuVendor.Nvidia;
  78. }
  79. else if (vendor == "intel")
  80. {
  81. string renderer = GL.GetString(StringName.Renderer).ToLower();
  82. return renderer.Contains("mesa") ? GpuVendor.IntelUnix : GpuVendor.IntelWindows;
  83. }
  84. else if (vendor == "ati technologies inc." || vendor == "advanced micro devices, inc.")
  85. {
  86. return GpuVendor.AmdWindows;
  87. }
  88. else if (vendor == "amd" || vendor == "x.org")
  89. {
  90. return GpuVendor.AmdUnix;
  91. }
  92. else
  93. {
  94. return GpuVendor.Unknown;
  95. }
  96. }
  97. private static bool SupportsQuadsCheck()
  98. {
  99. GL.GetError(); // Clear any existing error.
  100. GL.Begin(PrimitiveType.Quads);
  101. GL.End();
  102. return GL.GetError() == ErrorCode.NoError;
  103. }
  104. }
  105. }