// Copyright 2008 Dolphin Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later // OpenGL Backend Documentation /* 1.1 Display settings Internal and fullscreen resolution: Since the only internal resolutions allowed are also fullscreen resolution allowed by the system there is only need for one resolution setting that applies to both the internal resolution and the fullscreen resolution. - Apparently no, someone else doesn't agree Todo: Make the internal resolution option apply instantly, currently only the native and 2x option applies instantly. To do this we need to be able to change the reinitialize FramebufferManager:Init() while a game is running. 1.2 Screenshots The screenshots should be taken from the internal representation of the picture regardless of what the current window size is. Since AA and wireframe is applied together with the picture resizing this rule is not currently applied to AA or wireframe pictures, they are instead taken from whatever the window size is. Todo: Render AA and wireframe to a separate picture used for the screenshot in addition to the one for display. 1.3 AA Make AA apply instantly during gameplay if possible */ #include "VideoBackends/OGL/VideoBackend.h" #include #include #include #include "Common/Common.h" #include "Common/GL/GLContext.h" #include "Common/GL/GLUtil.h" #include "Common/MsgHandler.h" #include "Core/Config/GraphicsSettings.h" #include "VideoBackends/OGL/OGLBoundingBox.h" #include "VideoBackends/OGL/OGLConfig.h" #include "VideoBackends/OGL/OGLGfx.h" #include "VideoBackends/OGL/OGLPerfQuery.h" #include "VideoBackends/OGL/OGLVertexManager.h" #include "VideoBackends/OGL/ProgramShaderCache.h" #include "VideoBackends/OGL/SamplerCache.h" #ifdef ENABLE_VR #include "Common/Logging/Log.h" #include "VideoBackends/OGL/OGLOpenXR.h" #endif #include "VideoCommon/TextureCacheBase.h" #include "VideoCommon/VideoCommon.h" #include "VideoCommon/VideoConfig.h" namespace OGL { std::string VideoBackend::GetConfigName() const { return CONFIG_NAME; } std::string VideoBackend::GetDisplayName() const { if (g_ogl_config.bIsES) return _trans("OpenGL ES"); else return _trans("OpenGL"); } void VideoBackend::InitBackendInfo(const WindowSystemInfo& wsi) { std::unique_ptr temp_gl_context = GLContext::Create(wsi, g_Config.stereo_mode == StereoMode::QuadBuffer, true, false, Config::Get(Config::GFX_PREFER_GLES)); if (!temp_gl_context) return; FillBackendInfo(temp_gl_context.get()); } bool VideoBackend::InitializeGLExtensions(GLContext* context) { // Init extension support. if (!GLExtensions::Init(context)) { // OpenGL 2.0 is required for all shader based drawings. There is no way to get this by // extensions PanicAlertFmtT("GPU: OGL ERROR: Does your video card support OpenGL 2.0?"); return false; } if (GLExtensions::Version() < 300) { // integer vertex attributes require a gl3 only function PanicAlertFmtT("GPU: OGL ERROR: Need OpenGL version 3.\n" "GPU: Does your video card support OpenGL 3?"); return false; } return true; } bool VideoBackend::FillBackendInfo(GLContext* context) { if (!InitializeGLExtensions(context)) return false; g_backend_info.api_type = APIType::OpenGL; g_backend_info.MaxTextureSize = 16384; g_backend_info.bUsesLowerLeftOrigin = true; g_backend_info.bSupportsExclusiveFullscreen = false; g_backend_info.bSupportsGeometryShaders = true; g_backend_info.bSupportsComputeShaders = false; g_backend_info.bSupports3DVision = false; g_backend_info.bSupportsPostProcessing = true; g_backend_info.bSupportsSSAA = true; #ifdef ENABLE_VR // In OpenXR mode, take the inverted-depth-range (Reverse-Z trick) path that D3D11 and // Quest Vulkan use instead of GL's native reversed depth range. With the native path, // inverted game viewports (zRange < 0 — most titles) make UseVertexDepthRange() false, // so the VR geometry shader emits raw depth and relies on the per-draw hardware depth // slice — a pairing the VR clip-space rebuild was never validated on (SADX/SA2/MKDD // render nothing with Fast Depth Calculation enabled). The D3D convention bakes the // full depth mapping into the shader via depth_params and is fully supported by GL. g_backend_info.bSupportsReversedDepthRange = g_Config.stereo_mode != StereoMode::OpenXR; #else g_backend_info.bSupportsReversedDepthRange = true; #endif g_backend_info.bSupportsLogicOp = true; g_backend_info.bSupportsMultithreading = false; g_backend_info.bSupportsCopyToVram = true; g_backend_info.bSupportsLargePoints = true; g_backend_info.bSupportsDepthReadback = true; g_backend_info.bSupportsPartialDepthCopies = true; g_backend_info.bSupportsShaderBinaries = false; g_backend_info.bSupportsPipelineCacheData = false; g_backend_info.bSupportsLodBiasInSampler = true; g_backend_info.bSupportsPartialMultisampleResolve = true; // Unnecessary since OGL doesn't use pipelines g_backend_info.bSupportsDynamicVertexLoader = false; // TODO: There is a bug here, if texel buffers or SSBOs/atomics are not supported the graphics // options will show the option when it is not supported. The only way around this would be // creating a context when calling this function to determine what is available. g_backend_info.bSupportsGPUTextureDecoding = true; g_backend_info.bSupportsBBox = true; // Overwritten in OGLConfig.cpp later g_backend_info.bSupportsDualSourceBlend = true; g_backend_info.bSupportsPrimitiveRestart = true; g_backend_info.bSupportsPaletteConversion = true; g_backend_info.bSupportsClipControl = true; g_backend_info.bSupportsDepthClamp = true; g_backend_info.bSupportsST3CTextures = false; g_backend_info.bSupportsBPTCTextures = false; g_backend_info.bSupportsCoarseDerivatives = false; g_backend_info.bSupportsTextureQueryLevels = false; g_backend_info.bSupportsSettingObjectNames = false; g_backend_info.bUsesExplictQuadBuffering = true; g_backend_info.Adapters.clear(); // aamodes - 1 is to stay consistent with D3D (means no AA) g_backend_info.AAModes = {1, 2, 4, 8}; // check for the max vertex attributes GLint numvertexattribs = 0; glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &numvertexattribs); if (numvertexattribs < 16) { PanicAlertFmtT("GPU: OGL ERROR: Number of attributes {0} not enough.\n" "GPU: Does your video card support OpenGL 2.x?", numvertexattribs); return false; } // check the max texture width and height GLint max_texture_size = 0; glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size); g_backend_info.MaxTextureSize = static_cast(max_texture_size); if (max_texture_size < 1024) { PanicAlertFmtT("GL_MAX_TEXTURE_SIZE is {0} - must be at least 1024.", max_texture_size); return false; } if (!PopulateConfig(context)) { // Not all needed extensions are supported, so we have to stop here. // Else some of the next calls might crash. return false; } // TODO: Move the remaining fields from the Renderer constructor here. return true; } bool VideoBackend::Initialize(const WindowSystemInfo& wsi) { WindowSystemInfo gl_wsi = wsi; #if defined(ANDROID) && defined(ENABLE_VR) if (g_Config.VRSessionActive()) { // Meta's GLES OpenXR path (inherited from VrApi) requires that the app's EGL // context is NOT current on an Android window surface — the compositor owns the // display, and the runtime keeps the session parked in XR_SESSION_STATE_IDLE // (never READY) while the context is bound to a real window. Create a headless // context (surfaceless or pbuffer) instead, like Meta's own GLES samples do. // Presentation goes exclusively through the XR swapchains on this path. gl_wsi.type = WindowSystemType::Headless; gl_wsi.render_window = nullptr; gl_wsi.render_surface = nullptr; } #endif std::unique_ptr main_gl_context = GLContext::Create(gl_wsi, g_Config.stereo_mode == StereoMode::QuadBuffer, true, false, Config::Get(Config::GFX_PREFER_GLES)); if (!main_gl_context) return false; if (!FillBackendInfo(main_gl_context.get())) return false; auto gfx = std::make_unique(std::move(main_gl_context), wsi.render_surface_scale); ProgramShaderCache::Init(); g_sampler_cache = std::make_unique(); auto vertex_manager = std::make_unique(); auto perf_query = GetPerfQuery(gfx->IsGLES()); auto bounding_box = std::make_unique(); if (!InitializeShared(std::move(gfx), std::move(vertex_manager), std::move(perf_query), std::move(bounding_box))) { return false; } #ifdef ENABLE_VR // OpenXR init must run after InitializeShared(): creating the eye-swapchain wrappers // goes through OGLFramebuffer::Create() / ~OGLTexture(), which dereference the g_gfx // global (null until InitializeShared assigns it). if (g_Config.VRSessionActive()) { auto openxr = std::make_unique(); if (!openxr->Initialize(GetOGLGfx()->GetMainGLContext())) { WARN_LOG_FMT(VIDEO, "OpenXR initialization failed; continuing without VR."); } else { g_openxr_gl = std::move(openxr); } } #endif return true; } void VideoBackend::Shutdown() { #ifdef ENABLE_VR // Shut down VR while the GL context is still alive and current on this thread: // g_openxr_gl first (releases the OGLTexture/OGLFramebuffer wrappers, then destroys // swapchains/session/instance), then a defensive g_openxr reset. g_openxr_gl.reset(); VR::g_openxr.reset(); #endif ShutdownShared(); ProgramShaderCache::Shutdown(); g_sampler_cache.reset(); } } // namespace OGL