LCOV - code coverage report
Current view: top level - source/renderer/backend/gl - Device.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 383 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 35 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "Device.h"
      21             : 
      22             : #include "lib/alignment.h"
      23             : #include "lib/code_generation.h"
      24             : #include "lib/config2.h"
      25             : #include "lib/debug.h"
      26             : #include "lib/external_libraries/libsdl.h"
      27             : #include "lib/secure_crt.h"
      28             : #include "ps/CLogger.h"
      29             : #include "ps/ConfigDB.h"
      30             : #include "ps/Profile.h"
      31             : #include "ps/strings/StringBuilder.h"
      32             : #include "renderer/backend/Format.h"
      33             : #include "renderer/backend/gl/Buffer.h"
      34             : #include "renderer/backend/gl/DeviceCommandContext.h"
      35             : #include "renderer/backend/gl/DeviceForward.h"
      36             : #include "renderer/backend/gl/Framebuffer.h"
      37             : #include "renderer/backend/gl/PipelineState.h"
      38             : #include "renderer/backend/gl/ShaderProgram.h"
      39             : #include "renderer/backend/gl/SwapChain.h"
      40             : #include "renderer/backend/gl/Texture.h"
      41             : #include "scriptinterface/Object.h"
      42             : 
      43             : #if CONFIG2_GLES
      44             : #include "external_libraries/opengles2_wrapper.h"
      45             : #else
      46             : #include <glad/gl.h>
      47             : #endif
      48             : 
      49             : #include <SDL_config.h>
      50             : #include <SDL_error.h>
      51             : #include <SDL_version.h>
      52             : #include <SDL_video.h>
      53             : #include <algorithm>
      54             : #include <boost/algorithm/string/classification.hpp>
      55             : #include <boost/algorithm/string/constants.hpp>
      56             : #include <boost/algorithm/string/split.hpp>
      57             : #include <climits>
      58             : #include <cstring>
      59             : #include <iterator>
      60             : #include <js/RootingAPI.h>
      61             : #include <limits>
      62             : #include <utility>
      63             : 
      64             : namespace Renderer
      65             : {
      66             : 
      67             : namespace Backend
      68             : {
      69             : 
      70             : namespace GL
      71             : {
      72             : 
      73             : namespace
      74             : {
      75             : 
      76           0 : std::string GetNameImpl()
      77             : {
      78             :     // GL_VENDOR+GL_RENDERER are good enough here, so we don't use WMI to detect the cards.
      79             :     // On top of that WMI can cause crashes with Nvidia Optimus and some netbooks
      80             :     // see https://gitea.wildfiregames.com/0ad/0ad/issues/1952
      81             :     //     https://gitea.wildfiregames.com/0ad/0ad/issues/1575
      82           0 :     char cardName[128];
      83           0 :     const char* vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
      84           0 :     const char* renderer = reinterpret_cast<const char*>(glGetString(GL_RENDERER));
      85             :     // Happens if called before GL initialization.
      86           0 :     if (!vendor || !renderer)
      87           0 :         return {};
      88           0 :     sprintf_s(cardName, std::size(cardName), "%s %s", vendor, renderer);
      89             : 
      90             :     // Remove crap from vendor names. (don't dare touch the model name -
      91             :     // it's too risky, there are too many different strings).
      92             : #define SHORTEN(what, charsToKeep) \
      93             :     if (!strncmp(cardName, what, std::size(what) - 1)) \
      94             :         memmove(cardName + charsToKeep, cardName + std::size(what) - 1, (strlen(cardName) - (std::size(what) - 1) + 1) * sizeof(char));
      95           0 :     SHORTEN("ATI Technologies Inc.", 3);
      96           0 :     SHORTEN("NVIDIA Corporation", 6);
      97           0 :     SHORTEN("S3 Graphics", 2);                    // returned by EnumDisplayDevices
      98           0 :     SHORTEN("S3 Graphics, Incorporated", 2);  // returned by GL_VENDOR
      99             : #undef SHORTEN
     100             : 
     101           0 :     return cardName;
     102             : }
     103             : 
     104           0 : std::string GetVersionImpl()
     105             : {
     106           0 :     return reinterpret_cast<const char*>(glGetString(GL_VERSION));
     107             : }
     108             : 
     109           0 : std::string GetDriverInformationImpl()
     110             : {
     111             :     // Usually GL_VERSION contains both OpenGL and driver versions.
     112           0 :     return reinterpret_cast<const char*>(glGetString(GL_VERSION));
     113             : }
     114             : 
     115           0 : std::vector<std::string> GetExtensionsImpl()
     116             : {
     117           0 :     std::vector<std::string> extensions;
     118           0 :     const std::string exts{reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS))};
     119           0 :     boost::split(extensions, exts, boost::algorithm::is_space(), boost::token_compress_on);
     120           0 :     std::sort(extensions.begin(), extensions.end());
     121           0 :     return extensions;
     122           0 : }
     123             : 
     124           0 : void GLAD_API_PTR OnDebugMessage(
     125             :     GLenum source, GLenum type, GLuint id, GLenum severity,
     126             :     GLsizei /*length*/, const GLchar* message, const void* /*user_param*/)
     127             : {
     128           0 :     const char* debugSource = "unknown";
     129           0 :     const char* debugType = "unknown";
     130           0 :     const char* debugSeverity = "unknown";
     131             : 
     132           0 :     switch (source)
     133             :     {
     134           0 :     case GL_DEBUG_SOURCE_API:
     135           0 :         debugSource = "the API";
     136           0 :         break;
     137           0 :     case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
     138           0 :         debugSource = "the window system";
     139           0 :         break;
     140           0 :     case GL_DEBUG_SOURCE_SHADER_COMPILER:
     141           0 :         debugSource = "the shader compiler";
     142           0 :         break;
     143           0 :     case GL_DEBUG_SOURCE_THIRD_PARTY:
     144           0 :         debugSource = "a third party";
     145           0 :         break;
     146           0 :     case GL_DEBUG_SOURCE_APPLICATION:
     147           0 :         debugSource = "the application";
     148           0 :         break;
     149           0 :     case GL_DEBUG_SOURCE_OTHER:
     150           0 :         debugSource = "somewhere";
     151           0 :         break;
     152             :     }
     153             : 
     154           0 :     switch (type)
     155             :     {
     156           0 :     case GL_DEBUG_TYPE_ERROR:
     157           0 :         debugType = "error";
     158           0 :         break;
     159           0 :     case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
     160           0 :         debugType = "deprecated behaviour";
     161           0 :         break;
     162           0 :     case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
     163           0 :         debugType = "undefined behaviour";
     164           0 :         break;
     165           0 :     case GL_DEBUG_TYPE_PORTABILITY:
     166           0 :         debugType = "portability";
     167           0 :         break;
     168           0 :     case GL_DEBUG_TYPE_PERFORMANCE:
     169           0 :         debugType = "performance";
     170           0 :         break;
     171           0 :     case GL_DEBUG_TYPE_OTHER:
     172           0 :         debugType = "other";
     173           0 :         break;
     174           0 :     case GL_DEBUG_TYPE_MARKER:
     175           0 :         debugType = "marker";
     176           0 :         break;
     177           0 :     case GL_DEBUG_TYPE_PUSH_GROUP:
     178           0 :         debugType = "push group";
     179           0 :         break;
     180           0 :     case GL_DEBUG_TYPE_POP_GROUP:
     181           0 :         debugType = "pop group";
     182           0 :         break;
     183             :     }
     184             : 
     185           0 :     switch (severity)
     186             :     {
     187           0 :     case GL_DEBUG_SEVERITY_HIGH:
     188           0 :         debugSeverity = "high";
     189           0 :         break;
     190           0 :     case GL_DEBUG_SEVERITY_MEDIUM:
     191           0 :         debugSeverity = "medium";
     192           0 :         break;
     193           0 :     case GL_DEBUG_SEVERITY_LOW:
     194           0 :         debugSeverity = "low";
     195           0 :         break;
     196           0 :     case GL_DEBUG_SEVERITY_NOTIFICATION:
     197           0 :         debugSeverity = "notification";
     198           0 :         break;
     199             :     }
     200             : 
     201           0 :     if (severity == GL_DEBUG_SEVERITY_NOTIFICATION)
     202             :     {
     203           0 :         debug_printf(
     204             :             "OpenGL | %s: %s source: %s id %u: %s\n", debugSeverity, debugType, debugSource, id, message);
     205             :     }
     206             :     else
     207             :     {
     208           0 :         LOGWARNING( "OpenGL | {}: {} source: {} id {}: {}\n", debugSeverity, debugType,
     209             :             debugSource, id, message);
     210             :     }
     211           0 : }
     212             : 
     213           0 : void ResetGLError()
     214             : {
     215           0 :     do {} while (glGetError() != GL_NO_ERROR);
     216             : }
     217             : 
     218             : template<typename ParameterType, size_t parameterCount, bool doQueryCounterBits = false>
     219           0 : void ReportParameter(
     220             :     const Script::Request& rq, JS::HandleValue settings,
     221             :     const GLenum parameter, const char* paremeterName)
     222             : {
     223             :     static_assert(parameterCount > 0);
     224             : 
     225           0 :     const char* errorString = "(error)";
     226             : 
     227             :     ParameterType values[parameterCount];
     228             : 
     229             :     if constexpr (std::is_same_v<ParameterType, GLint>)
     230             :     {
     231           0 :         std::fill(std::begin(values), std::end(values), -1);
     232             :         if constexpr (doQueryCounterBits)
     233           0 :             glGetQueryiv(parameter, GL_QUERY_COUNTER_BITS, values);
     234             :         else
     235           0 :             glGetIntegerv(parameter, values);
     236             :     }
     237             :     else if constexpr (std::is_same_v<ParameterType, GLfloat>)
     238             :     {
     239           0 :         std::fill(std::begin(values), std::end(values), std::numeric_limits<GLfloat>::quiet_NaN());
     240           0 :         glGetFloatv(parameter, values);
     241             :     }
     242             :     else if constexpr (std::is_same_v<ParameterType, const char*>)
     243             :     {
     244           0 :         std::fill(std::begin(values), std::end(values), "");
     245             :         // In the newer GL versions we have glGetStringi but currently we get
     246             :         // only one string.
     247             :         static_assert(parameterCount == 1);
     248           0 :         values[0] = reinterpret_cast<const char*>(glGetString(parameter));
     249           0 :         if (!values[0])
     250           0 :             values[0] = errorString;
     251             :     }
     252             :     else
     253             :     {
     254             :         debug_warn("Unsupported type");
     255             :     }
     256             : 
     257           0 :     const bool errorHappened{glGetError() != GL_NO_ERROR};
     258             :     ResetGLError();
     259             : 
     260             :     char buffer[1024];
     261           0 :     for (size_t index{0}; index < parameterCount; ++index)
     262             :     {
     263           0 :         PS::StringBuilder stringBuilder{{std::begin(buffer), std::end(buffer)}};
     264           0 :         stringBuilder.Append(paremeterName);
     265             :         if constexpr (parameterCount > 1)
     266             :         {
     267           0 :             stringBuilder.Append('[');
     268           0 :             stringBuilder.Append(index);
     269           0 :             stringBuilder.Append(']');
     270             :         }
     271           0 :         if (errorHappened)
     272           0 :             Script::SetProperty(rq, settings, stringBuilder.Str().data(), errorString);
     273             :         else
     274           0 :             Script::SetProperty(rq, settings, stringBuilder.Str().data(), values[index]);
     275             :     }
     276           0 : }
     277             : 
     278             : } // anonymous namespace
     279             : 
     280             : // static
     281           0 : std::unique_ptr<IDevice> CDevice::Create(SDL_Window* window)
     282             : {
     283           0 :     std::unique_ptr<CDevice> device(new CDevice());
     284             : 
     285           0 :     if (window)
     286             :     {
     287             :         // According to https://wiki.libsdl.org/SDL_CreateWindow we don't need to
     288             :         // call SDL_GL_LoadLibrary if we have a window with SDL_WINDOW_OPENGL,
     289             :         // because it'll be called internally for the first created window.
     290           0 :         device->m_Window = window;
     291           0 :         device->m_Context = SDL_GL_CreateContext(device->m_Window);
     292           0 :         if (!device->m_Context)
     293             :         {
     294           0 :             LOGERROR("SDL_GL_CreateContext failed: '{}'", SDL_GetError());
     295           0 :             return nullptr;
     296             :         }
     297             :     }
     298             : 
     299             : #if CONFIG2_GLES
     300             :     const int version{gladLoadGLES2(reinterpret_cast<GLADloadfunc>(SDL_GL_GetProcAddress))};
     301             : #else
     302           0 :     const int version{gladLoadGL(reinterpret_cast<GLADloadfunc>(SDL_GL_GetProcAddress))};
     303             : #endif
     304           0 :     if (!version)
     305             :     {
     306           0 :         DEBUG_DISPLAY_FATAL_ERROR(L"GLAD failed to load GL");
     307           0 :         return nullptr;
     308             :     }
     309             : 
     310           0 :     device->m_Name = GetNameImpl();
     311           0 :     device->m_Version = GetVersionImpl();
     312           0 :     device->m_DriverInformation = GetDriverInformationImpl();
     313           0 :     device->m_Extensions = GetExtensionsImpl();
     314             : 
     315           0 :     LOGMESSAGE("GL device name: {}", device->m_Name);
     316           0 :     LOGMESSAGE("GL version: {}", device->m_Version);
     317             : 
     318             : #if CONFIG2_GLES
     319             :     const bool minimumRequiredGLVersionSupported{version >= GLAD_MAKE_VERSION(2, 1)};
     320             : #else
     321           0 :     const bool minimumRequiredGLVersionSupported{version >= GLAD_MAKE_VERSION(2, 0)};
     322             : #endif
     323             : 
     324           0 :     if (!minimumRequiredGLVersionSupported || !GLAD_GL_EXT_framebuffer_object)
     325             :     {
     326             :         // It doesn't make sense to continue working here, because we're not
     327             :         // able to display anything.
     328           0 :         DEBUG_DISPLAY_FATAL_ERROR(
     329             :             L"Your graphics card doesn't appear to be fully compatible with OpenGL shaders."
     330             :             L" The game does not support pre-shader graphics cards."
     331             :             L" You are advised to try installing newer drivers and/or upgrade your graphics card."
     332             :             L" For more information, please see http://www.wildfiregames.com/forum/index.php?showtopic=16734"
     333             :         );
     334             :     }
     335             : 
     336             :     // Set packing parameters for uploading and downloading data.
     337           0 :     glPixelStorei(GL_PACK_ALIGNMENT, 1);
     338           0 :     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
     339             : 
     340             : #if !CONFIG2_GLES
     341             :     // If a driver supports rendering to a floating point framebuffer then we
     342             :     // need to remove clamping to have a full available range.
     343           0 :     if (GLAD_GL_ARB_color_buffer_float)
     344             :     {
     345           0 :         glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE);
     346             :     }
     347             : #endif
     348             : 
     349             :     // Some drivers might invalidate an incorrect surface which leads to artifacts.
     350           0 :     if (g_ConfigDB.Get("renderer.backend.gl.enableframebufferinvalidating", false))
     351             :     {
     352             : #if CONFIG2_GLES
     353             :         device->m_UseFramebufferInvalidating = GLAD_GL_EXT_discard_framebuffer;
     354             : #else
     355           0 :         device->m_UseFramebufferInvalidating = GLAD_GL_ARB_invalidate_subdata;
     356             : #endif
     357             :     }
     358             : 
     359           0 :     Capabilities& capabilities = device->m_Capabilities;
     360           0 :     capabilities.computeShaders = version >= GLAD_MAKE_VERSION(4, 3);
     361             : #if CONFIG2_GLES
     362             :     // Some GLES implementations have GL_EXT_texture_compression_dxt1
     363             :     // but that only supports DXT1 so we can't use it.
     364             :     capabilities.S3TC = GLAD_GL_EXT_texture_compression_s3tc && GLAD_GL_EXT_texture_compression_s3tc_srgb;
     365             : #else
     366             :     // Note: we don't bother checking for GL_S3_s3tc - it is incompatible
     367             :     // and irrelevant (was never widespread).
     368           0 :     capabilities.S3TC = GLAD_GL_EXT_texture_compression_s3tc && GLAD_GL_EXT_texture_sRGB;
     369             : #endif
     370             : #if CONFIG2_GLES
     371             :     capabilities.multisampling = false;
     372             :     capabilities.maxSampleCount = 1;
     373             : #else
     374           0 :     capabilities.multisampling =
     375           0 :         version >= GLAD_MAKE_VERSION(3, 3) && GLAD_GL_ARB_texture_multisample;
     376           0 :     if (capabilities.multisampling)
     377             :     {
     378             :         // By default GL_MULTISAMPLE should be enabled, but enable it for buggy drivers.
     379           0 :         glEnable(GL_MULTISAMPLE);
     380           0 :         GLint maxSamples = 1;
     381           0 :         glGetIntegerv(GL_MAX_SAMPLES, &maxSamples);
     382           0 :         capabilities.maxSampleCount = maxSamples;
     383             :     }
     384             : #endif
     385           0 :     capabilities.anisotropicFiltering = GLAD_GL_EXT_texture_filter_anisotropic;
     386           0 :     if (capabilities.anisotropicFiltering)
     387             :     {
     388           0 :         GLfloat maxAnisotropy = 1.0f;
     389           0 :         glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy);
     390           0 :         capabilities.maxAnisotropy = maxAnisotropy;
     391             :     }
     392           0 :     GLint maxTextureSize = 1024;
     393           0 :     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
     394           0 :     capabilities.maxTextureSize = maxTextureSize;
     395             : 
     396             : #if CONFIG2_GLES
     397             :     const bool hasDebug = GLAD_GL_KHR_debug;
     398             : #else
     399           0 :     const bool hasDebug = GLAD_GL_KHR_debug;
     400             : #endif
     401           0 :     if (hasDebug)
     402             :     {
     403             : #ifdef NDEBUG
     404           0 :         const bool enableDebugMessages{g_ConfigDB.Get("renderer.backend.debugmessages", false)};
     405           0 :         capabilities.debugLabels = g_ConfigDB.Get("renderer.backend.debuglabels", false);
     406           0 :         capabilities.debugScopedLabels = g_ConfigDB.Get("renderer.backend.debugscopedlabels", false);
     407             : #else
     408             :         const bool enableDebugMessages = true;
     409             :         capabilities.debugLabels = true;
     410             :         capabilities.debugScopedLabels = true;
     411             : #endif
     412           0 :         if (enableDebugMessages)
     413             :         {
     414           0 :             glEnable(GL_DEBUG_OUTPUT);
     415             : #if !CONFIG2_GLES
     416           0 :             glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
     417             : #else
     418             : #warning GLES without GL_DEBUG_OUTPUT_SYNCHRONOUS might call the callback from different threads which might be unsafe.
     419             : #endif
     420           0 :             glDebugMessageCallback(OnDebugMessage, nullptr);
     421             : 
     422             :             // Filter out our own debug group messages
     423           0 :             const GLuint id = 0x0AD;
     424           0 :             glDebugMessageControl(
     425             :                 GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PUSH_GROUP, GL_DONT_CARE, 1, &id, GL_FALSE);
     426           0 :             glDebugMessageControl(
     427             :                 GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_POP_GROUP, GL_DONT_CARE, 1, &id, GL_FALSE);
     428             :         }
     429             :     }
     430             : 
     431             : #if CONFIG2_GLES
     432             :     capabilities.instancing = false;
     433             :     capabilities.storage = false;
     434             :     capabilities.timestamps = false;
     435             : #else
     436           0 :     capabilities.instancing =
     437           0 :         (version >= GLAD_MAKE_VERSION(3, 3) ||
     438           0 :         (GLAD_GL_ARB_draw_instanced && GLAD_GL_ARB_instanced_arrays));
     439           0 :     GLint maxStorageBufferSize{0};
     440           0 :     if (GLAD_GL_ARB_shader_storage_buffer_object)
     441           0 :         glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &maxStorageBufferSize);
     442             :     // Storage buffers might not work correctly on some Mesa drivers or might have
     443             :     // decreased performance. We need to investigate it further but for now we
     444             :     // disable storage buffers on Mesa.
     445           0 :     const bool disableStorageForMesa{
     446           0 :         device->m_Name.find("Mesa") != std::string::npos
     447           0 :         || device->m_DriverInformation.find("Mesa") != std::string::npos};
     448           0 :     capabilities.storage =
     449           0 :         capabilities.computeShaders && maxStorageBufferSize > 0
     450           0 :         && static_cast<size_t>(maxStorageBufferSize) >= 128 * MiB
     451           0 :         && !disableStorageForMesa
     452           0 :         && GLAD_GL_ARB_uniform_buffer_object
     453           0 :         && GLAD_GL_ARB_shader_storage_buffer_object
     454           0 :         && GLAD_GL_ARB_half_float_vertex
     455           0 :         && GLAD_GL_ARB_program_interface_query;
     456           0 :     capabilities.timestamps = GLAD_GL_ARB_timer_query;
     457           0 :     if (capabilities.timestamps)
     458           0 :         capabilities.timestampMultiplier = 1.0 / 1e9;
     459             : #endif
     460             : 
     461           0 :     device->m_R8Supported = version >= GLAD_MAKE_VERSION(3, 0);
     462             : 
     463           0 :     return device;
     464           0 : }
     465             : 
     466           0 : CDevice::CDevice() = default;
     467             : 
     468           0 : CDevice::~CDevice()
     469             : {
     470             : #if !CONFIG2_GLES
     471           0 :     for (Query& query : m_Queries)
     472             :     {
     473           0 :         ENSURE(!query.occupied);
     474           0 :         glDeleteQueries(1, &query.query);
     475             :     }
     476             : #endif
     477             : 
     478           0 :     if (m_Context)
     479           0 :         SDL_GL_DeleteContext(m_Context);
     480           0 : }
     481             : 
     482           0 : void CDevice::Report(const Script::Request& rq, JS::HandleValue settings)
     483             : {
     484           0 :     Script::SetProperty(rq, settings, "name", "gl");
     485             : 
     486             :     // We need to reset all previous errors because we don't call glGetError
     487             :     // by default.
     488           0 :     ResetGLError();
     489             : 
     490             : #define INTEGER(NAME) ReportParameter<GLint, 1>(rq, settings, GL_##NAME, "GL_" #NAME)
     491             : #define INTEGER2(NAME) ReportParameter<GLint, 2>(rq, settings, GL_##NAME, "GL_" #NAME)
     492             : 
     493             : #define BOOL(NAME) INTEGER(NAME)
     494             : 
     495             : #define FLOAT(NAME) ReportParameter<GLfloat, 1>(rq, settings, GL_##NAME, "GL_" #NAME)
     496             : #define FLOAT2(NAME) ReportParameter<GLfloat, 2>(rq, settings, GL_##NAME, "GL_" #NAME)
     497             : 
     498             : #define STRING(NAME) ReportParameter<const char*, 1>(rq, settings, GL_##NAME, "GL_" #NAME)
     499             : 
     500             : #define QUERY_COUNTER_BITS(NAME) ReportParameter<GLint, 1, true>(rq, settings, GL_##NAME, "GL_" #NAME ".GL_QUERY_COUNTER_BITS")
     501             : 
     502             :     // Core OpenGL 1.3:
     503             :     // (We don't bother checking extension strings for anything older than 1.3;
     504             :     // it'll just produce harmless warnings)
     505           0 :     STRING(VERSION);
     506           0 :     STRING(VENDOR);
     507           0 :     STRING(RENDERER);
     508           0 :     STRING(EXTENSIONS);
     509             : 
     510             : #if !CONFIG2_GLES
     511           0 :     INTEGER(MAX_CLIP_PLANES);
     512             : #endif
     513           0 :     INTEGER(SUBPIXEL_BITS);
     514             : #if !CONFIG2_GLES
     515           0 :     INTEGER(MAX_3D_TEXTURE_SIZE);
     516             : #endif
     517           0 :     INTEGER(MAX_TEXTURE_SIZE);
     518           0 :     INTEGER(MAX_CUBE_MAP_TEXTURE_SIZE);
     519           0 :     INTEGER2(MAX_VIEWPORT_DIMS);
     520             : 
     521             : #if !CONFIG2_GLES
     522           0 :     BOOL(RGBA_MODE);
     523           0 :     BOOL(INDEX_MODE);
     524           0 :     BOOL(DOUBLEBUFFER);
     525           0 :     BOOL(STEREO);
     526             : #endif
     527             : 
     528           0 :     FLOAT2(ALIASED_POINT_SIZE_RANGE);
     529           0 :     FLOAT2(ALIASED_LINE_WIDTH_RANGE);
     530             : #if !CONFIG2_GLES
     531           0 :     INTEGER(MAX_ELEMENTS_INDICES);
     532           0 :     INTEGER(MAX_ELEMENTS_VERTICES);
     533           0 :     INTEGER(MAX_TEXTURE_UNITS);
     534             : #endif
     535           0 :     INTEGER(SAMPLE_BUFFERS);
     536           0 :     INTEGER(SAMPLES);
     537             :     // TODO: compressed texture formats
     538           0 :     INTEGER(RED_BITS);
     539           0 :     INTEGER(GREEN_BITS);
     540           0 :     INTEGER(BLUE_BITS);
     541           0 :     INTEGER(ALPHA_BITS);
     542             : #if !CONFIG2_GLES
     543           0 :     INTEGER(INDEX_BITS);
     544             : #endif
     545           0 :     INTEGER(DEPTH_BITS);
     546           0 :     INTEGER(STENCIL_BITS);
     547             : 
     548             : #if !CONFIG2_GLES
     549             : 
     550             :     // Core OpenGL 2.1:
     551             : 
     552           0 :     FLOAT(MAX_TEXTURE_LOD_BIAS);
     553             : 
     554           0 :     QUERY_COUNTER_BITS(SAMPLES_PASSED);
     555             : 
     556           0 :     STRING(SHADING_LANGUAGE_VERSION);
     557             : 
     558           0 :     INTEGER(MAX_VERTEX_ATTRIBS);
     559           0 :     INTEGER(MAX_VERTEX_UNIFORM_COMPONENTS);
     560           0 :     INTEGER(MAX_VARYING_FLOATS);
     561           0 :     INTEGER(MAX_COMBINED_TEXTURE_IMAGE_UNITS);
     562           0 :     INTEGER(MAX_VERTEX_TEXTURE_IMAGE_UNITS);
     563             : 
     564           0 :     INTEGER(MAX_FRAGMENT_UNIFORM_COMPONENTS);
     565             : 
     566           0 :     INTEGER(MAX_TEXTURE_IMAGE_UNITS);
     567           0 :     INTEGER(MAX_TEXTURE_COORDS);
     568             : 
     569           0 :     INTEGER(MAX_DRAW_BUFFERS);
     570             : 
     571             :     // Core OpenGL 3.0:
     572             : 
     573           0 :     if (GLAD_GL_EXT_gpu_shader4)
     574             :     {
     575           0 :         INTEGER(MIN_PROGRAM_TEXEL_OFFSET_EXT); // no _EXT version of these in glext.h
     576           0 :         INTEGER(MAX_PROGRAM_TEXEL_OFFSET_EXT);
     577             :     }
     578             : 
     579           0 :     INTEGER(MAX_COLOR_ATTACHMENTS_EXT);
     580           0 :     INTEGER(MAX_RENDERBUFFER_SIZE_EXT);
     581             : 
     582           0 :     if (GLAD_GL_EXT_framebuffer_multisample)
     583             :     {
     584           0 :         INTEGER(MAX_SAMPLES_EXT);
     585             :     }
     586             : 
     587           0 :     if (GLAD_GL_EXT_texture_array)
     588             :     {
     589           0 :         INTEGER(MAX_ARRAY_TEXTURE_LAYERS_EXT);
     590             :     }
     591             : 
     592             :     // Other interesting extensions:
     593             : 
     594           0 :     if (GLAD_GL_ARB_timer_query)
     595             :     {
     596           0 :         QUERY_COUNTER_BITS(TIME_ELAPSED);
     597             :     }
     598             : 
     599           0 :     if (GLAD_GL_ARB_timer_query)
     600             :     {
     601           0 :         QUERY_COUNTER_BITS(TIMESTAMP);
     602             :     }
     603             : 
     604           0 :     if (GLAD_GL_EXT_texture_filter_anisotropic)
     605             :     {
     606           0 :         FLOAT(MAX_TEXTURE_MAX_ANISOTROPY_EXT);
     607             :     }
     608             : 
     609           0 :     if (GLAD_GL_ARB_texture_rectangle)
     610             :     {
     611           0 :         INTEGER(MAX_RECTANGLE_TEXTURE_SIZE_ARB);
     612             :     }
     613             : 
     614           0 :     if (GLAD_GL_ARB_geometry_shader4)
     615             :     {
     616           0 :         INTEGER(MAX_GEOMETRY_TEXTURE_IMAGE_UNITS_ARB);
     617           0 :         INTEGER(MAX_GEOMETRY_OUTPUT_VERTICES_ARB);
     618           0 :         INTEGER(MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS_ARB);
     619           0 :         INTEGER(MAX_GEOMETRY_UNIFORM_COMPONENTS_ARB);
     620           0 :         INTEGER(MAX_GEOMETRY_VARYING_COMPONENTS_ARB);
     621           0 :         INTEGER(MAX_VERTEX_VARYING_COMPONENTS_ARB);
     622             :     }
     623             : 
     624           0 :     if (GLAD_GL_ARB_uniform_buffer_object)
     625             :     {
     626           0 :         INTEGER(MAX_UNIFORM_BLOCK_SIZE);
     627           0 :         INTEGER(MAX_UNIFORM_BUFFER_BINDINGS);
     628             :     }
     629             : 
     630           0 :     if (GLAD_GL_ARB_shader_storage_buffer_object)
     631             :     {
     632           0 :         INTEGER(MAX_SHADER_STORAGE_BLOCK_SIZE);
     633           0 :         INTEGER(MAX_SHADER_STORAGE_BUFFER_BINDINGS);
     634             :     }
     635             : 
     636             : #else // CONFIG2_GLES
     637             : 
     638             :     // Core OpenGL ES 2.0:
     639             : 
     640             :     STRING(SHADING_LANGUAGE_VERSION);
     641             :     INTEGER(MAX_VERTEX_ATTRIBS);
     642             :     INTEGER(MAX_VERTEX_UNIFORM_VECTORS);
     643             :     INTEGER(MAX_VARYING_VECTORS);
     644             :     INTEGER(MAX_COMBINED_TEXTURE_IMAGE_UNITS);
     645             :     INTEGER(MAX_VERTEX_TEXTURE_IMAGE_UNITS);
     646             :     INTEGER(MAX_FRAGMENT_UNIFORM_VECTORS);
     647             :     INTEGER(MAX_TEXTURE_IMAGE_UNITS);
     648             :     INTEGER(MAX_RENDERBUFFER_SIZE);
     649             : 
     650             : #endif // CONFIG2_GLES
     651             : 
     652             : 
     653             : // TODO: Support OpenGL platforms which don't use GLX as well.
     654           0 : }
     655             : 
     656           0 : std::unique_ptr<IDeviceCommandContext> CDevice::CreateCommandContext()
     657             : {
     658           0 :     std::unique_ptr<CDeviceCommandContext> commandContet = CDeviceCommandContext::Create(this);
     659           0 :     m_ActiveCommandContext = commandContet.get();
     660           0 :     return commandContet;
     661           0 : }
     662             : 
     663           0 : std::unique_ptr<ISwapChain> CDevice::CreateSwapChain(
     664             :     [[maybe_unused]] const char* name, SDL_Window* window,
     665             :     int surfaceDrawableWidth, int surfaceDrawableHeight,
     666             :     const bool vsync, std::unique_ptr<ISwapChain> oldSwapChain)
     667             : {
     668           0 :     oldSwapChain.reset();
     669           0 :     ENSURE(window == m_Window);
     670           0 :     if (window)
     671           0 :         SDL_GL_GetDrawableSize(window, &surfaceDrawableWidth, &surfaceDrawableHeight);
     672           0 :     return CSwapChain::Create(
     673           0 :         this, window, surfaceDrawableWidth, surfaceDrawableHeight, vsync);
     674             : }
     675             : 
     676           0 : void CDevice::WaitUntilIdle()
     677             : {
     678             :     // Technically we don't need to call glFinish since a driver ensures safety
     679             :     // itself. Though it might give a hint to a driver and it might reduce a
     680             :     // memory spike during recreating resources.
     681           0 :     glFinish();
     682           0 : }
     683             : 
     684           0 : std::unique_ptr<IGraphicsPipelineState> CDevice::CreateGraphicsPipelineState(
     685             :     const SGraphicsPipelineStateDesc& pipelineStateDesc)
     686             : {
     687           0 :     return CGraphicsPipelineState::Create(this, pipelineStateDesc);
     688             : }
     689             : 
     690           0 : std::unique_ptr<IComputePipelineState> CDevice::CreateComputePipelineState(
     691             :     const SComputePipelineStateDesc& pipelineStateDesc)
     692             : {
     693           0 :     return CComputePipelineState::Create(this, pipelineStateDesc);
     694             : }
     695             : 
     696           0 : std::unique_ptr<IVertexInputLayout> CDevice::CreateVertexInputLayout(
     697             :     const std::span<const SVertexAttributeFormat> attributes)
     698             : {
     699           0 :     return std::make_unique<CVertexInputLayout>(this, attributes);
     700             : }
     701             : 
     702           0 : std::unique_ptr<ITexture> CDevice::CreateTexture(
     703             :     const char* name, const ITexture::Type type, const uint32_t usage,
     704             :     const Format format, const uint32_t width, const uint32_t height,
     705             :     const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount)
     706             : {
     707           0 :     return CTexture::Create(this, name, type, usage,
     708           0 :         format, width, height, defaultSamplerDesc, MIPLevelCount, sampleCount);
     709             : }
     710             : 
     711           0 : std::unique_ptr<ITexture> CDevice::CreateTexture2D(
     712             :     const char* name, const uint32_t usage,
     713             :     const Format format, const uint32_t width, const uint32_t height,
     714             :     const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount)
     715             : {
     716           0 :     return CreateTexture(name, CTexture::Type::TEXTURE_2D, usage,
     717           0 :         format, width, height, defaultSamplerDesc, MIPLevelCount, sampleCount);
     718             : }
     719             : 
     720           0 : std::unique_ptr<IFramebuffer> CDevice::CreateFramebuffer(
     721             :     const char* name, SColorAttachment* colorAttachment,
     722             :     SDepthStencilAttachment* depthStencilAttachment)
     723             : {
     724           0 :     return CFramebuffer::Create(
     725           0 :         this, name, colorAttachment, depthStencilAttachment);
     726             : }
     727             : 
     728           0 : std::unique_ptr<IBuffer> CDevice::CreateBuffer(
     729             :     const char* name, const IBuffer::Type type, const uint32_t size, const uint32_t usage)
     730             : {
     731           0 :     return CBuffer::Create(this, name, type, size, usage);
     732             : }
     733             : 
     734           0 : std::unique_ptr<IShaderProgram> CDevice::CreateShaderProgram(
     735             :     const CStr& name, const CShaderDefines& defines)
     736             : {
     737           0 :     return CShaderProgram::Create(this, name, defines);
     738             : }
     739             : 
     740           0 : bool CDevice::IsTextureFormatSupported(const Format format) const
     741             : {
     742           0 :     bool supported = false;
     743           0 :     switch (format)
     744             :     {
     745             :     case Format::UNDEFINED:
     746             :         break;
     747             : 
     748             :     case Format::R8G8B8_UNORM:
     749             :     case Format::R8G8B8A8_UNORM:
     750             :     case Format::R8G8B8A8_SRGB:
     751             :     case Format::A8_UNORM:
     752             :     case Format::L8_UNORM:
     753           0 :         supported = true;
     754             :         break;
     755             : 
     756             :     case Format::R32_SFLOAT:
     757             :     case Format::R32G32_SFLOAT:
     758             :     case Format::R32G32B32_SFLOAT:
     759             :         break;
     760             : 
     761             :     case Format::D16_UNORM:
     762             :     case Format::D24_UNORM:
     763             :     case Format::D32_SFLOAT:
     764           0 :         supported = true;
     765             :         break;
     766             :     case Format::D24_UNORM_S8_UINT:
     767             : #if !CONFIG2_GLES
     768           0 :         supported = true;
     769             : #endif
     770             :         break;
     771             : 
     772             :     case Format::D32_SFLOAT_S8_UINT:
     773             :         break;
     774             : 
     775           0 :     case Format::BC1_RGB_UNORM:
     776           0 :     case Format::BC1_RGB_SRGB:
     777           0 :     case Format::BC1_RGBA_UNORM:
     778           0 :     case Format::BC1_RGBA_SRGB:
     779           0 :     case Format::BC2_UNORM:
     780           0 :     case Format::BC2_SRGB:
     781           0 :     case Format::BC3_UNORM:
     782           0 :     case Format::BC3_SRGB:
     783           0 :         supported = m_Capabilities.S3TC;
     784           0 :         break;
     785             : 
     786             : #if !CONFIG2_GLES
     787           0 :     case Format::R16G16B16A16_SFLOAT:
     788           0 :     case Format::R32G32B32A32_SFLOAT:
     789           0 :         supported = GLAD_GL_ARB_texture_float;
     790           0 :         break;
     791             : #endif
     792             : 
     793             :     default:
     794             :         break;
     795             :     }
     796           0 :     return supported;
     797             : }
     798             : 
     799           0 : bool CDevice::IsFramebufferFormatSupported(const Format format) const
     800             : {
     801           0 :     bool supported = false;
     802           0 :     switch (format)
     803             :     {
     804             :     case Format::UNDEFINED:
     805             :         break;
     806             : #if !CONFIG2_GLES
     807           0 :     case Format::R8_UNORM:
     808           0 :         supported = m_R8Supported;
     809           0 :         break;
     810           0 :     case Format::B10G11R11_UFLOAT:
     811           0 :         supported = GLAD_GL_EXT_packed_float && GLAD_GL_ARB_color_buffer_float;
     812             :         break;
     813           0 :     case Format::R16G16B16A16_SFLOAT:
     814           0 :     case Format::R32G32B32A32_SFLOAT:
     815           0 :         supported = GLAD_GL_ARB_texture_float && GLAD_GL_ARB_color_buffer_float;
     816             :         break;
     817             : #endif
     818             :     case Format::R8G8B8A8_UNORM:
     819           0 :         supported = true;
     820             :         break;
     821             :     default:
     822             :         break;
     823             :     }
     824           0 :     return supported;
     825             : }
     826             : 
     827           0 : Format CDevice::GetPreferredDepthStencilFormat(
     828             :     const uint32_t /*usage*/, const bool depth, const bool stencil) const
     829             : {
     830           0 :     ENSURE(depth || stencil);
     831           0 :     if (stencil)
     832             : #if CONFIG2_GLES
     833             :         return Format::UNDEFINED;
     834             : #else
     835             :         return Format::D24_UNORM_S8_UINT;
     836             : #endif
     837             :     else
     838           0 :         return Format::D24_UNORM;
     839             : }
     840             : 
     841           0 : uint32_t CDevice::AllocateQuery()
     842             : {
     843           0 :     auto it = std::find_if(
     844           0 :         m_Queries.begin(), m_Queries.end(), [](const CDevice::Query& query)
     845             :         {
     846           0 :             return !query.occupied;
     847             :         });
     848           0 :     if (it == m_Queries.end())
     849             :     {
     850           0 :         m_Queries.emplace_back();
     851             : #if !CONFIG2_GLES
     852           0 :         glGenQueries(1, &m_Queries.back().query);
     853             : #endif
     854           0 :         it = prev(m_Queries.end());
     855             :     }
     856           0 :     it->occupied = true;
     857           0 :     return std::distance(m_Queries.begin(), it);
     858             : }
     859             : 
     860           0 : void CDevice::FreeQuery(const uint32_t handle)
     861             : {
     862           0 :     ENSURE(handle < m_Queries.size());
     863           0 :     ENSURE(m_Queries[handle].occupied);
     864           0 :     m_Queries[handle].occupied = false;
     865           0 : }
     866             : 
     867           0 : bool CDevice::IsQueryResultAvailable(const uint32_t handle) const
     868             : {
     869           0 :     ENSURE(handle < m_Queries.size());
     870             : 
     871           0 :     GLint available{};
     872             : #if !CONFIG2_GLES
     873           0 :     glGetQueryObjectiv(m_Queries[handle].query, GL_QUERY_RESULT_AVAILABLE, &available);
     874             : #endif
     875           0 :     return available;
     876             : }
     877             : 
     878           0 : uint64_t CDevice::GetQueryResult(const uint32_t handle)
     879             : {
     880           0 :     ENSURE(handle < m_Queries.size());
     881             :     // TODO: maybe we should check QUERY_COUNTER_BITS to ensure it's
     882             :     // high enough (but apparently it might trigger GL errors on ATI)
     883             : 
     884           0 :     GLuint64 queryTimestamp{};
     885             : #if !CONFIG2_GLES
     886             :     // Use the non-suffixed function here, as defined by GL_ARB_timer_query.
     887           0 :     glGetQueryObjectui64v(m_Queries[handle].query, GL_QUERY_RESULT, &queryTimestamp);
     888             : #endif
     889           0 :     return queryTimestamp;
     890             : }
     891             : 
     892           0 : void CDevice::InsertTimestampQuery(const uint32_t handle)
     893             : {
     894           0 :     ENSURE(handle < m_Queries.size());
     895             : #if !CONFIG2_GLES
     896           0 :     glQueryCounter(m_Queries[handle].query, GL_TIMESTAMP);
     897             : #endif
     898           0 : }
     899             : 
     900           0 : void CDevice::CollectStatistics(StatisticsVector& statistics) const
     901             : {
     902           0 :     statistics.emplace_back("Query count", "", static_cast<uint32_t>(m_Queries.size()));
     903           0 : }
     904             : 
     905           0 : std::unique_ptr<IDevice> CreateDevice(SDL_Window* window)
     906             : {
     907           0 :     return GL::CDevice::Create(window);
     908             : }
     909             : 
     910             : } // namespace GL
     911             : 
     912             : } // namespace Backend
     913             : 
     914             : } // namespace Renderer

Generated by: LCOV version 1.16