HwCapabilities.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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> _supportsAlphaToCoverageDitherControl = new(() => HasExtension("GL_NV_alpha_to_coverage_dither_control"));
  8. private static readonly Lazy<bool> _supportsAstcCompression = new(() => HasExtension("GL_KHR_texture_compression_astc_ldr"));
  9. private static readonly Lazy<bool> _supportsBlendEquationAdvanced = new(() => HasExtension("GL_NV_blend_equation_advanced"));
  10. private static readonly Lazy<bool> _supportsDrawTexture = new(() => HasExtension("GL_NV_draw_texture"));
  11. private static readonly Lazy<bool> _supportsFragmentShaderInterlock = new(() => HasExtension("GL_ARB_fragment_shader_interlock"));
  12. private static readonly Lazy<bool> _supportsFragmentShaderOrdering = new(() => HasExtension("GL_INTEL_fragment_shader_ordering"));
  13. private static readonly Lazy<bool> _supportsGeometryShaderPassthrough = new(() => HasExtension("GL_NV_geometry_shader_passthrough"));
  14. private static readonly Lazy<bool> _supportsImageLoadFormatted = new(() => HasExtension("GL_EXT_shader_image_load_formatted"));
  15. private static readonly Lazy<bool> _supportsIndirectParameters = new(() => HasExtension("GL_ARB_indirect_parameters"));
  16. private static readonly Lazy<bool> _supportsParallelShaderCompile = new(() => HasExtension("GL_ARB_parallel_shader_compile"));
  17. private static readonly Lazy<bool> _supportsPolygonOffsetClamp = new(() => HasExtension("GL_EXT_polygon_offset_clamp"));
  18. private static readonly Lazy<bool> _supportsQuads = new(SupportsQuadsCheck);
  19. private static readonly Lazy<bool> _supportsSeamlessCubemapPerTexture = new(() => HasExtension("GL_ARB_seamless_cubemap_per_texture"));
  20. private static readonly Lazy<bool> _supportsShaderBallot = new(() => HasExtension("GL_ARB_shader_ballot"));
  21. private static readonly Lazy<bool> _supportsShaderViewportLayerArray = new(() => HasExtension("GL_ARB_shader_viewport_layer_array"));
  22. private static readonly Lazy<bool> _supportsViewportArray2 = new(() => HasExtension("GL_NV_viewport_array2"));
  23. private static readonly Lazy<bool> _supportsTextureCompressionBptc = new(() => HasExtension("GL_EXT_texture_compression_bptc"));
  24. private static readonly Lazy<bool> _supportsTextureCompressionRgtc = new(() => HasExtension("GL_EXT_texture_compression_rgtc"));
  25. private static readonly Lazy<bool> _supportsTextureCompressionS3tc = new(() => HasExtension("GL_EXT_texture_compression_s3tc"));
  26. private static readonly Lazy<bool> _supportsTextureShadowLod = new(() => HasExtension("GL_EXT_texture_shadow_lod"));
  27. private static readonly Lazy<bool> _supportsViewportSwizzle = new(() => HasExtension("GL_NV_viewport_swizzle"));
  28. private static readonly Lazy<int> _maximumComputeSharedMemorySize = new(() => GetLimit(All.MaxComputeSharedMemorySize));
  29. private static readonly Lazy<int> _storageBufferOffsetAlignment = new(() => GetLimit(All.ShaderStorageBufferOffsetAlignment));
  30. private static readonly Lazy<int> _textureBufferOffsetAlignment = new(() => GetLimit(All.TextureBufferOffsetAlignment));
  31. public enum GpuVendor
  32. {
  33. Unknown,
  34. AmdWindows,
  35. AmdUnix,
  36. IntelWindows,
  37. IntelUnix,
  38. Nvidia,
  39. }
  40. private static readonly Lazy<GpuVendor> _gpuVendor = new(GetGpuVendor);
  41. private static bool IsIntel => _gpuVendor.Value == GpuVendor.IntelWindows || _gpuVendor.Value == GpuVendor.IntelUnix;
  42. public static GpuVendor Vendor => _gpuVendor.Value;
  43. private static readonly Lazy<float> _maxSupportedAnisotropy = new(GL.GetFloat((GetPName)All.MaxTextureMaxAnisotropy));
  44. public static bool UsePersistentBufferForFlush => _gpuVendor.Value == GpuVendor.AmdWindows || _gpuVendor.Value == GpuVendor.Nvidia;
  45. public static bool SupportsAlphaToCoverageDitherControl => _supportsAlphaToCoverageDitherControl.Value;
  46. public static bool SupportsAstcCompression => _supportsAstcCompression.Value;
  47. public static bool SupportsBlendEquationAdvanced => _supportsBlendEquationAdvanced.Value;
  48. public static bool SupportsDrawTexture => _supportsDrawTexture.Value;
  49. public static bool SupportsFragmentShaderInterlock => _supportsFragmentShaderInterlock.Value;
  50. public static bool SupportsFragmentShaderOrdering => _supportsFragmentShaderOrdering.Value;
  51. public static bool SupportsGeometryShaderPassthrough => _supportsGeometryShaderPassthrough.Value;
  52. public static bool SupportsImageLoadFormatted => _supportsImageLoadFormatted.Value;
  53. public static bool SupportsIndirectParameters => _supportsIndirectParameters.Value;
  54. public static bool SupportsParallelShaderCompile => _supportsParallelShaderCompile.Value;
  55. public static bool SupportsPolygonOffsetClamp => _supportsPolygonOffsetClamp.Value;
  56. public static bool SupportsQuads => _supportsQuads.Value;
  57. public static bool SupportsSeamlessCubemapPerTexture => _supportsSeamlessCubemapPerTexture.Value;
  58. public static bool SupportsShaderBallot => _supportsShaderBallot.Value;
  59. public static bool SupportsShaderViewportLayerArray => _supportsShaderViewportLayerArray.Value;
  60. public static bool SupportsViewportArray2 => _supportsViewportArray2.Value;
  61. public static bool SupportsTextureCompressionBptc => _supportsTextureCompressionBptc.Value;
  62. public static bool SupportsTextureCompressionRgtc => _supportsTextureCompressionRgtc.Value;
  63. public static bool SupportsTextureCompressionS3tc => _supportsTextureCompressionS3tc.Value;
  64. public static bool SupportsTextureShadowLod => _supportsTextureShadowLod.Value;
  65. public static bool SupportsViewportSwizzle => _supportsViewportSwizzle.Value;
  66. public static bool SupportsMismatchingViewFormat => _gpuVendor.Value != GpuVendor.AmdWindows && _gpuVendor.Value != GpuVendor.IntelWindows;
  67. public static bool SupportsNonConstantTextureOffset => _gpuVendor.Value == GpuVendor.Nvidia;
  68. public static bool RequiresSyncFlush => _gpuVendor.Value == GpuVendor.AmdWindows || IsIntel;
  69. public static int MaximumComputeSharedMemorySize => _maximumComputeSharedMemorySize.Value;
  70. public static int StorageBufferOffsetAlignment => _storageBufferOffsetAlignment.Value;
  71. public static int TextureBufferOffsetAlignment => _textureBufferOffsetAlignment.Value;
  72. public static float MaximumSupportedAnisotropy => _maxSupportedAnisotropy.Value;
  73. private static bool HasExtension(string name)
  74. {
  75. int numExtensions = GL.GetInteger(GetPName.NumExtensions);
  76. for (int extension = 0; extension < numExtensions; extension++)
  77. {
  78. if (GL.GetString(StringNameIndexed.Extensions, extension) == name)
  79. {
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. private static int GetLimit(All name)
  86. {
  87. return GL.GetInteger((GetPName)name);
  88. }
  89. private static GpuVendor GetGpuVendor()
  90. {
  91. string vendor = GL.GetString(StringName.Vendor).ToLowerInvariant();
  92. if (vendor == "nvidia corporation")
  93. {
  94. return GpuVendor.Nvidia;
  95. }
  96. else if (vendor == "intel")
  97. {
  98. string renderer = GL.GetString(StringName.Renderer).ToLowerInvariant();
  99. return renderer.Contains("mesa") ? GpuVendor.IntelUnix : GpuVendor.IntelWindows;
  100. }
  101. else if (vendor == "ati technologies inc." || vendor == "advanced micro devices, inc.")
  102. {
  103. return GpuVendor.AmdWindows;
  104. }
  105. else if (vendor == "amd" || vendor == "x.org")
  106. {
  107. return GpuVendor.AmdUnix;
  108. }
  109. else
  110. {
  111. return GpuVendor.Unknown;
  112. }
  113. }
  114. private static bool SupportsQuadsCheck()
  115. {
  116. GL.GetError(); // Clear any existing error.
  117. GL.Begin(PrimitiveType.Quads);
  118. GL.End();
  119. return GL.GetError() == ErrorCode.NoError;
  120. }
  121. }
  122. }