gl_device.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <cstddef>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <limits>
  10. #include <optional>
  11. #include <span>
  12. #include <stdexcept>
  13. #include <vector>
  14. #include <glad/glad.h>
  15. #include "common/logging/log.h"
  16. #include "common/scope_exit.h"
  17. #include "common/settings.h"
  18. #include "shader_recompiler/stage.h"
  19. #include "video_core/renderer_opengl/gl_device.h"
  20. #include "video_core/renderer_opengl/gl_resource_manager.h"
  21. namespace OpenGL {
  22. namespace {
  23. constexpr std::array LIMIT_UBOS = {
  24. GL_MAX_VERTEX_UNIFORM_BLOCKS, GL_MAX_TESS_CONTROL_UNIFORM_BLOCKS,
  25. GL_MAX_TESS_EVALUATION_UNIFORM_BLOCKS, GL_MAX_GEOMETRY_UNIFORM_BLOCKS,
  26. GL_MAX_FRAGMENT_UNIFORM_BLOCKS, GL_MAX_COMPUTE_UNIFORM_BLOCKS,
  27. };
  28. template <typename T>
  29. T GetInteger(GLenum pname) {
  30. GLint temporary;
  31. glGetIntegerv(pname, &temporary);
  32. return static_cast<T>(temporary);
  33. }
  34. bool TestProgram(const GLchar* glsl) {
  35. const GLuint shader{glCreateShaderProgramv(GL_VERTEX_SHADER, 1, &glsl)};
  36. GLint link_status;
  37. glGetProgramiv(shader, GL_LINK_STATUS, &link_status);
  38. glDeleteProgram(shader);
  39. return link_status == GL_TRUE;
  40. }
  41. std::vector<std::string_view> GetExtensions() {
  42. GLint num_extensions;
  43. glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions);
  44. std::vector<std::string_view> extensions;
  45. extensions.reserve(num_extensions);
  46. for (GLint index = 0; index < num_extensions; ++index) {
  47. extensions.push_back(
  48. reinterpret_cast<const char*>(glGetStringi(GL_EXTENSIONS, static_cast<GLuint>(index))));
  49. }
  50. return extensions;
  51. }
  52. bool HasExtension(std::span<const std::string_view> extensions, std::string_view extension) {
  53. return std::ranges::find(extensions, extension) != extensions.end();
  54. }
  55. std::array<u32, Shader::MaxStageTypes> BuildMaxUniformBuffers() noexcept {
  56. std::array<u32, Shader::MaxStageTypes> max;
  57. std::ranges::transform(LIMIT_UBOS, max.begin(), &GetInteger<u32>);
  58. return max;
  59. }
  60. bool IsASTCSupported() {
  61. static constexpr std::array targets{
  62. GL_TEXTURE_2D,
  63. GL_TEXTURE_2D_ARRAY,
  64. };
  65. static constexpr std::array formats{
  66. GL_COMPRESSED_RGBA_ASTC_4x4_KHR, GL_COMPRESSED_RGBA_ASTC_5x4_KHR,
  67. GL_COMPRESSED_RGBA_ASTC_5x5_KHR, GL_COMPRESSED_RGBA_ASTC_6x5_KHR,
  68. GL_COMPRESSED_RGBA_ASTC_6x6_KHR, GL_COMPRESSED_RGBA_ASTC_8x5_KHR,
  69. GL_COMPRESSED_RGBA_ASTC_8x6_KHR, GL_COMPRESSED_RGBA_ASTC_8x8_KHR,
  70. GL_COMPRESSED_RGBA_ASTC_10x5_KHR, GL_COMPRESSED_RGBA_ASTC_10x6_KHR,
  71. GL_COMPRESSED_RGBA_ASTC_10x8_KHR, GL_COMPRESSED_RGBA_ASTC_10x10_KHR,
  72. GL_COMPRESSED_RGBA_ASTC_12x10_KHR, GL_COMPRESSED_RGBA_ASTC_12x12_KHR,
  73. GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
  74. GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
  75. GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
  76. GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
  77. GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
  78. GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
  79. GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
  80. };
  81. static constexpr std::array required_support{
  82. GL_VERTEX_TEXTURE, GL_TESS_CONTROL_TEXTURE, GL_TESS_EVALUATION_TEXTURE,
  83. GL_GEOMETRY_TEXTURE, GL_FRAGMENT_TEXTURE, GL_COMPUTE_TEXTURE,
  84. };
  85. for (const GLenum target : targets) {
  86. for (const GLenum format : formats) {
  87. for (const GLenum support : required_support) {
  88. GLint value;
  89. glGetInternalformativ(target, format, support, 1, &value);
  90. if (value != GL_FULL_SUPPORT) {
  91. return false;
  92. }
  93. }
  94. }
  95. }
  96. return true;
  97. }
  98. [[nodiscard]] bool IsDebugToolAttached(std::span<const std::string_view> extensions) {
  99. const bool nsight = std::getenv("NVTX_INJECTION64_PATH") || std::getenv("NSIGHT_LAUNCHED");
  100. return nsight || HasExtension(extensions, "GL_EXT_debug_tool");
  101. }
  102. } // Anonymous namespace
  103. Device::Device() {
  104. if (!GLAD_GL_VERSION_4_6) {
  105. LOG_ERROR(Render_OpenGL, "OpenGL 4.6 is not available");
  106. throw std::runtime_error{"Insufficient version"};
  107. }
  108. vendor_name = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
  109. const std::string_view version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
  110. const std::vector extensions = GetExtensions();
  111. const bool is_nvidia = vendor_name == "NVIDIA Corporation";
  112. const bool is_amd = vendor_name == "ATI Technologies Inc.";
  113. const bool is_intel = vendor_name == "Intel";
  114. #ifdef __unix__
  115. const bool is_linux = true;
  116. #else
  117. const bool is_linux = false;
  118. #endif
  119. bool disable_fast_buffer_sub_data = false;
  120. if (is_nvidia && version == "4.6.0 NVIDIA 443.24") {
  121. LOG_WARNING(
  122. Render_OpenGL,
  123. "Beta driver 443.24 is known to have issues. There might be performance issues.");
  124. disable_fast_buffer_sub_data = true;
  125. }
  126. max_uniform_buffers = BuildMaxUniformBuffers();
  127. uniform_buffer_alignment = GetInteger<size_t>(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);
  128. shader_storage_alignment = GetInteger<size_t>(GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT);
  129. max_vertex_attributes = GetInteger<u32>(GL_MAX_VERTEX_ATTRIBS);
  130. max_varyings = GetInteger<u32>(GL_MAX_VARYING_VECTORS);
  131. max_compute_shared_memory_size = GetInteger<u32>(GL_MAX_COMPUTE_SHARED_MEMORY_SIZE);
  132. max_glasm_storage_buffer_blocks = GetInteger<u32>(GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS);
  133. has_warp_intrinsics = GLAD_GL_NV_gpu_shader5 && GLAD_GL_NV_shader_thread_group &&
  134. GLAD_GL_NV_shader_thread_shuffle;
  135. has_shader_ballot = GLAD_GL_ARB_shader_ballot;
  136. has_vertex_viewport_layer = GLAD_GL_ARB_shader_viewport_layer_array;
  137. has_image_load_formatted = HasExtension(extensions, "GL_EXT_shader_image_load_formatted");
  138. has_texture_shadow_lod = HasExtension(extensions, "GL_EXT_texture_shadow_lod");
  139. has_astc = IsASTCSupported();
  140. has_variable_aoffi = TestVariableAoffi();
  141. has_component_indexing_bug = is_amd;
  142. has_precise_bug = TestPreciseBug();
  143. has_broken_texture_view_formats = is_amd || (!is_linux && is_intel);
  144. has_nv_viewport_array2 = GLAD_GL_NV_viewport_array2;
  145. has_derivative_control = GLAD_GL_ARB_derivative_control;
  146. has_vertex_buffer_unified_memory = GLAD_GL_NV_vertex_buffer_unified_memory;
  147. has_debugging_tool_attached = IsDebugToolAttached(extensions);
  148. has_depth_buffer_float = HasExtension(extensions, "GL_NV_depth_buffer_float");
  149. has_geometry_shader_passthrough = GLAD_GL_NV_geometry_shader_passthrough;
  150. has_nv_gpu_shader_5 = GLAD_GL_NV_gpu_shader5;
  151. has_shader_int64 = HasExtension(extensions, "GL_ARB_gpu_shader_int64");
  152. has_amd_shader_half_float = GLAD_GL_AMD_gpu_shader_half_float;
  153. has_sparse_texture_2 = GLAD_GL_ARB_sparse_texture2;
  154. warp_size_potentially_larger_than_guest = !is_nvidia && !is_intel;
  155. need_fastmath_off = is_nvidia;
  156. // At the moment of writing this, only Nvidia's driver optimizes BufferSubData on exclusive
  157. // uniform buffers as "push constants"
  158. has_fast_buffer_sub_data = is_nvidia && !disable_fast_buffer_sub_data;
  159. shader_backend = Settings::values.shader_backend.GetValue();
  160. use_assembly_shaders = shader_backend == Settings::ShaderBackend::GLASM &&
  161. GLAD_GL_NV_gpu_program5 && GLAD_GL_NV_compute_program5 &&
  162. GLAD_GL_NV_transform_feedback && GLAD_GL_NV_transform_feedback2;
  163. if (shader_backend == Settings::ShaderBackend::GLASM && !use_assembly_shaders) {
  164. LOG_ERROR(Render_OpenGL, "Assembly shaders enabled but not supported");
  165. shader_backend = Settings::ShaderBackend::GLSL;
  166. }
  167. if (shader_backend == Settings::ShaderBackend::GLSL && is_nvidia) {
  168. const std::string_view driver_version = version.substr(13);
  169. const int version_major =
  170. std::atoi(driver_version.substr(0, driver_version.find(".")).data());
  171. if (version_major >= 495) {
  172. has_cbuf_ftou_bug = true;
  173. has_bool_ref_bug = true;
  174. }
  175. }
  176. // Blocks AMD and Intel OpenGL drivers on Windows from using asynchronous shader compilation.
  177. use_asynchronous_shaders = Settings::values.use_asynchronous_shaders.GetValue() &&
  178. !(is_amd || (is_intel && !is_linux));
  179. use_driver_cache = is_nvidia;
  180. LOG_INFO(Render_OpenGL, "Renderer_VariableAOFFI: {}", has_variable_aoffi);
  181. LOG_INFO(Render_OpenGL, "Renderer_ComponentIndexingBug: {}", has_component_indexing_bug);
  182. LOG_INFO(Render_OpenGL, "Renderer_PreciseBug: {}", has_precise_bug);
  183. LOG_INFO(Render_OpenGL, "Renderer_BrokenTextureViewFormats: {}",
  184. has_broken_texture_view_formats);
  185. if (Settings::values.use_asynchronous_shaders.GetValue() && !use_asynchronous_shaders) {
  186. LOG_WARNING(Render_OpenGL, "Asynchronous shader compilation enabled but not supported");
  187. }
  188. }
  189. std::string Device::GetVendorName() const {
  190. if (vendor_name == "NVIDIA Corporation") {
  191. return "NVIDIA";
  192. }
  193. if (vendor_name == "ATI Technologies Inc.") {
  194. return "AMD";
  195. }
  196. if (vendor_name == "Intel") {
  197. // For Mesa, `Intel` is an overloaded vendor string that could mean crocus or iris.
  198. // Simply return `INTEL` for those as well as the Windows driver.
  199. return "INTEL";
  200. }
  201. if (vendor_name == "Intel Open Source Technology Center") {
  202. return "I965";
  203. }
  204. if (vendor_name == "Mesa Project") {
  205. return "I915";
  206. }
  207. if (vendor_name == "Mesa/X.org") {
  208. // This vendor string is overloaded between llvmpipe, softpipe, and virgl, so just return
  209. // MESA instead of one of those driver names.
  210. return "MESA";
  211. }
  212. if (vendor_name == "AMD") {
  213. return "RADEONSI";
  214. }
  215. if (vendor_name == "nouveau") {
  216. return "NOUVEAU";
  217. }
  218. if (vendor_name == "X.Org") {
  219. return "R600";
  220. }
  221. if (vendor_name == "Collabora Ltd") {
  222. return "ZINK";
  223. }
  224. if (vendor_name == "Intel Corporation") {
  225. return "OPENSWR";
  226. }
  227. if (vendor_name == "Microsoft Corporation") {
  228. return "D3D12";
  229. }
  230. if (vendor_name == "NVIDIA") {
  231. // Mesa's tegra driver reports `NVIDIA`. Only present in this list because the default
  232. // strategy would have returned `NVIDIA` here for this driver, the same result as the
  233. // proprietary driver.
  234. return "TEGRA";
  235. }
  236. return vendor_name;
  237. }
  238. bool Device::TestVariableAoffi() {
  239. return TestProgram(R"(#version 430 core
  240. // This is a unit test, please ignore me on apitrace bug reports.
  241. uniform sampler2D tex;
  242. uniform ivec2 variable_offset;
  243. out vec4 output_attribute;
  244. void main() {
  245. output_attribute = textureOffset(tex, vec2(0), variable_offset);
  246. })");
  247. }
  248. bool Device::TestPreciseBug() {
  249. return !TestProgram(R"(#version 430 core
  250. in vec3 coords;
  251. out float out_value;
  252. uniform sampler2DShadow tex;
  253. void main() {
  254. precise float tmp_value = vec4(texture(tex, coords)).x;
  255. out_value = tmp_value;
  256. })");
  257. }
  258. } // namespace OpenGL