HwCapabilities.cs 5.9 KB

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