| 1 | /* |
| 2 | * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 | * It is copyright by its individual contributors, as recorded in the |
| 4 | * project's Git history. See COPYING.txt at the top level for license |
| 5 | * terms and a link to the Git history. |
| 6 | */ |
| 7 | |
| 8 | /* OpenGL extensions: |
| 9 | * We use global function pointer for any extension function we want to use. |
| 10 | */ |
| 11 | |
| 12 | #include <string.h> |
| 13 | #include <SDL.h> |
| 14 | |
| 15 | #include "ogl_extensions.h" |
| 16 | #include "console.h" |
| 17 | |
| 18 | #include "dxxsconf.h" |
| 19 | #include "dsx-ns.h" |
| 20 | #include <array> |
| 21 | |
| 22 | namespace dcx { |
| 23 | |
| 24 | /* GL_ARB_sync */ |
| 25 | bool ogl_have_ARB_sync = false; |
| 26 | PFNGLFENCESYNCPROC glFenceSyncFunc = NULL; |
| 27 | PFNGLDELETESYNCPROC glDeleteSyncFunc = NULL; |
| 28 | PFNGLCLIENTWAITSYNCPROC glClientWaitSyncFunc = NULL; |
| 29 | |
| 30 | /* GL_EXT_texture_filter_anisotropic */ |
| 31 | GLfloat ogl_maxanisotropy = 0.0f; |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | static std::array<long, 2> parse_version_str(const char *v) |
| 36 | { |
| 37 | std::array<long, 2> version; |
| 38 | version[0]=1; |
| 39 | version[1]=0; |
| 40 | if (v) { |
| 41 | char *ptr; |
| 42 | if (v[0] == 'O') { |
| 43 | // OpenGL ES uses the format "OpenGL ES-xx major.minor" |
| 44 | const auto &prefix_gles = "OpenGL ES-"; |
| 45 | if (!strncmp(v, prefix_gles, sizeof(prefix_gles)-1)) { |
| 46 | // skip the prefix |
| 47 | v += sizeof(prefix_gles)-1; |
| 48 | // skip the profile marker |
| 49 | if (v[0] && v[1]) { |
| 50 | v +=2; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | version[0]=strtol(v,&ptr,10); |
| 55 | if (ptr[0]) |
| 56 | version[1]=strtol(ptr+1,NULL,10); |
| 57 | } |
| 58 | return version; |
| 59 | } |
| 60 | |
| 61 | static bool is_ext_supported(const char *extensions, const std::span<const char> name) |
| 62 | { |
| 63 | if (extensions) |
| 64 | { |
| 65 | const auto found = strstr(extensions, name.data()); |
| 66 | if (found) { |
| 67 | // check that the name is actually complete */ |
| 68 | const char c = found[name.size() - 1]; |
| 69 | if (c == ' ' || c == 0) |
| 70 | return true; |
| 71 | } |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | enum support_mode { |
| 77 | NO_SUPPORT=0, |
| 78 | SUPPORT_CORE=1, |
| 79 | SUPPORT_EXT=2 |
| 80 | }; |
| 81 | |
| 82 | static support_mode is_supported(const char *extensions, const std::array<long, 2> &version, const std::span<const char> name, long major, long minor, long major_es, long minor_es) |
| 83 | { |
| 84 | #if DXX_USE_OGLES |
| 85 | static_cast<void>(major); |
| 86 | static_cast<void>(minor); |
| 87 | if ( (major_es > 0) && ((version[0] > major_es) || (version[0] == major_es && version[1] >= minor_es)) ) |
| 88 | return SUPPORT_CORE; |
| 89 | #else |
| 90 | static_cast<void>(major_es); |
| 91 | static_cast<void>(minor_es); |
| 92 | if ( (major > 0) && ((version[0] > major) || (version[0] == major && version[1] >= minor)) ) |
| 93 | return SUPPORT_CORE; |
| 94 | #endif |
| 95 | |
| 96 | if (is_ext_supported(extensions, name)) |
| 97 | return SUPPORT_EXT; |
| 98 | return NO_SUPPORT; |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |
| 103 | void ogl_extensions_init() |
| 104 | { |
| 105 | const auto version_str = reinterpret_cast<const char *>(glGetString(GL_VERSION)); |
| 106 | if (!version_str) { |
| 107 | con_puts(CON_URGENT, "DXX-Rebirth: no valid OpenGL context when querying GL extensions!"); |
| 108 | return; |
| 109 | } |
| 110 | const auto version = parse_version_str(version_str); |
| 111 | const auto extension_str = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)); |
| 112 | |
| 113 | #if DXX_USE_OGLES |
| 114 | #define DXX_OGL_STRING " ES" |
| 115 | #else |
| 116 | #define DXX_OGL_STRING "" |
| 117 | #endif |
| 118 | con_printf(CON_VERBOSE, "DXX-Rebirth: OpenGL" DXX_OGL_STRING ": version %ld.%ld (%s)", version[0], version[1], version_str); |
| 119 | #undef DXX_OGL_STRING |
| 120 | |
| 121 | /* GL_EXT_texture_filter_anisotropic */ |
| 122 | if (is_supported(extension_str, version, "GL_EXT_texture_filter_anisotropic", -1, -1, -1, -1)) { |
| 123 | glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &ogl_maxanisotropy); |
| 124 | con_printf(CON_VERBOSE, "DXX-Rebirth: OpenGL: GL_EXT_texture_filter_anisotropic available, max anisotropy: %f", ogl_maxanisotropy); |
| 125 | } else { |
| 126 | ogl_maxanisotropy=0.0f; |
| 127 | con_puts(CON_VERBOSE, "DXX-Rebirth: OpenGL: GL_EXT_texture_filter_anisotropic not available"); |
| 128 | } |
| 129 | |
| 130 | /* GL_ARB_sync */ |
| 131 | if (is_supported(extension_str, version, "GL_ARB_sync", 3, 2, 3, 0)) { |
| 132 | glFenceSyncFunc = reinterpret_cast<PFNGLFENCESYNCPROC>(SDL_GL_GetProcAddress("glFenceSync")); |
| 133 | glDeleteSyncFunc = reinterpret_cast<PFNGLDELETESYNCPROC>(SDL_GL_GetProcAddress("glDeleteSync")); |
| 134 | glClientWaitSyncFunc = reinterpret_cast<PFNGLCLIENTWAITSYNCPROC>(SDL_GL_GetProcAddress("glClientWaitSync")); |
| 135 | |
| 136 | } |
| 137 | const auto s = (glFenceSyncFunc && glDeleteSyncFunc && glClientWaitSyncFunc) |
| 138 | ? (ogl_have_ARB_sync = true, std::span<const char>{"DXX-Rebirth: OpenGL: GL_ARB_sync available"}) |
| 139 | : std::span<const char>{"DXX-Rebirth: OpenGL: GL_ARB_sync not available"}; |
| 140 | con_puts(CON_VERBOSE, s); |
| 141 | } |
| 142 | |
| 143 | } |