HwCapabilities.cs 5.0 KB

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