HwCapabilities.cs 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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> _supportsPolygonOffsetClamp = new Lazy<bool>(() => HasExtension("GL_EXT_polygon_offset_clamp"));
  10. private static readonly Lazy<bool> _supportsViewportSwizzle = new Lazy<bool>(() => HasExtension("GL_NV_viewport_swizzle"));
  11. private static readonly Lazy<bool> _supportsSeamlessCubemapPerTexture = new Lazy<bool>(() => HasExtension("GL_ARB_seamless_cubemap_per_texture"));
  12. private static readonly Lazy<bool> _supportsParallelShaderCompile = new Lazy<bool>(() => HasExtension("GL_ARB_parallel_shader_compile"));
  13. private static readonly Lazy<int> _maximumComputeSharedMemorySize = new Lazy<int>(() => GetLimit(All.MaxComputeSharedMemorySize));
  14. private static readonly Lazy<int> _storageBufferOffsetAlignment = new Lazy<int>(() => GetLimit(All.ShaderStorageBufferOffsetAlignment));
  15. public enum GpuVendor
  16. {
  17. Unknown,
  18. Amd,
  19. IntelWindows,
  20. IntelUnix,
  21. Nvidia
  22. }
  23. private static readonly Lazy<GpuVendor> _gpuVendor = new Lazy<GpuVendor>(GetGpuVendor);
  24. public static GpuVendor Vendor => _gpuVendor.Value;
  25. private static Lazy<float> _maxSupportedAnisotropy = new Lazy<float>(GL.GetFloat((GetPName)All.MaxTextureMaxAnisotropy));
  26. public static bool SupportsAstcCompression => _supportsAstcCompression.Value;
  27. public static bool SupportsImageLoadFormatted => _supportsImageLoadFormatted.Value;
  28. public static bool SupportsPolygonOffsetClamp => _supportsPolygonOffsetClamp.Value;
  29. public static bool SupportsViewportSwizzle => _supportsViewportSwizzle.Value;
  30. public static bool SupportsSeamlessCubemapPerTexture => _supportsSeamlessCubemapPerTexture.Value;
  31. public static bool SupportsParallelShaderCompile => _supportsParallelShaderCompile.Value;
  32. public static bool SupportsNonConstantTextureOffset => _gpuVendor.Value == GpuVendor.Nvidia;
  33. public static bool RequiresSyncFlush => _gpuVendor.Value == GpuVendor.Amd || _gpuVendor.Value == GpuVendor.IntelWindows || _gpuVendor.Value == GpuVendor.IntelUnix;
  34. public static int MaximumComputeSharedMemorySize => _maximumComputeSharedMemorySize.Value;
  35. public static int StorageBufferOffsetAlignment => _storageBufferOffsetAlignment.Value;
  36. public static float MaximumSupportedAnisotropy => _maxSupportedAnisotropy.Value;
  37. private static bool HasExtension(string name)
  38. {
  39. int numExtensions = GL.GetInteger(GetPName.NumExtensions);
  40. for (int extension = 0; extension < numExtensions; extension++)
  41. {
  42. if (GL.GetString(StringNameIndexed.Extensions, extension) == name)
  43. {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. private static int GetLimit(All name)
  50. {
  51. return GL.GetInteger((GetPName)name);
  52. }
  53. private static GpuVendor GetGpuVendor()
  54. {
  55. string vendor = GL.GetString(StringName.Vendor).ToLower();
  56. if (vendor == "nvidia corporation")
  57. {
  58. return GpuVendor.Nvidia;
  59. }
  60. else if (vendor == "intel")
  61. {
  62. string renderer = GL.GetString(StringName.Renderer).ToLower();
  63. return renderer.Contains("mesa") ? GpuVendor.IntelUnix : GpuVendor.IntelWindows;
  64. }
  65. else if (vendor == "ati technologies inc." || vendor == "advanced micro devices, inc.")
  66. {
  67. return GpuVendor.Amd;
  68. }
  69. else
  70. {
  71. return GpuVendor.Unknown;
  72. }
  73. }
  74. }
  75. }