From e122943bcd82133146e750eec6c30951ff038b94 Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Wed, 31 Jul 2024 13:55:53 +0200 Subject: [PATCH] cogl/gl: Check GLSL version and unify GL context version checks This adds a check which makes sure that the required GLSL/GLSL ES versions are supported. It also splits out the GLES version check into its own function, just like GL does. Part-of: --- cogl/cogl/driver/gl/gl/cogl-driver-gl.c | 71 +++++++++---- cogl/cogl/driver/gl/gles/cogl-driver-gles.c | 105 ++++++++++++++++---- 2 files changed, 138 insertions(+), 38 deletions(-) diff --git a/cogl/cogl/driver/gl/gl/cogl-driver-gl.c b/cogl/cogl/driver/gl/gl/cogl-driver-gl.c index e6fdd89bf..586d07285 100644 --- a/cogl/cogl/driver/gl/gl/cogl-driver-gl.c +++ b/cogl/cogl/driver/gl/gl/cogl-driver-gl.c @@ -374,8 +374,8 @@ _cogl_driver_get_read_pixels_format (CoglContext *context, static gboolean _cogl_get_gl_version (CoglContext *ctx, - int *major_out, - int *minor_out) + int *major_out, + int *minor_out) { const char *version_string; @@ -387,9 +387,8 @@ _cogl_get_gl_version (CoglContext *ctx, } static gboolean -check_gl_version (CoglContext *ctx, - char **gl_extensions, - GError **error) +check_gl_version (CoglContext *ctx, + GError **error) { int major, minor; @@ -415,13 +414,51 @@ check_gl_version (CoglContext *ctx, } static gboolean -_cogl_driver_update_features (CoglContext *ctx, - GError **error) +_cogl_get_glsl_version (CoglContext *ctx, + int *major_out, + int *minor_out) +{ + const char *version_string; + + version_string = (char *)ctx->glGetString (GL_SHADING_LANGUAGE_VERSION); + return _cogl_gl_util_parse_gl_version (version_string, major_out, minor_out); +} + +static gboolean +check_glsl_version (CoglContext *ctx, + GError **error) +{ + int major, minor; + + if (!_cogl_get_glsl_version (ctx, &major, &minor)) + { + g_set_error (error, + COGL_DRIVER_ERROR, + COGL_DRIVER_ERROR_UNKNOWN_VERSION, + "The supported GLSL version could not be determined"); + return FALSE; + } + + if (!COGL_CHECK_GL_VERSION (major, minor, ctx->glsl_major, ctx->glsl_minor)) + { + g_set_error (error, + COGL_DRIVER_ERROR, + COGL_DRIVER_ERROR_INVALID_VERSION, + "GLSL %d%d0 or better is required", + ctx->glsl_major, ctx->glsl_minor); + return FALSE; + } + + return TRUE; +} + +static gboolean +_cogl_driver_update_features (CoglContext *ctx, + GError **error) { unsigned long private_features [COGL_FLAGS_N_LONGS_FOR_SIZE (COGL_N_PRIVATE_FEATURES)] = { 0 }; g_auto (GStrv) gl_extensions = 0; - const char *glsl_version; int gl_major = 0, gl_minor = 0; int i; @@ -440,7 +477,14 @@ _cogl_driver_update_features (CoglContext *ctx, gl_extensions = _cogl_context_get_gl_extensions (ctx); - if (!check_gl_version (ctx, gl_extensions, error)) + if (!check_gl_version (ctx, error)) + return FALSE; + + ctx->glsl_major = 1; + ctx->glsl_minor = 40; + ctx->glsl_version_to_use = 140; + + if (!check_glsl_version (ctx, error)) return FALSE; if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_WINSYS))) @@ -461,15 +505,6 @@ _cogl_driver_update_features (CoglContext *ctx, _cogl_get_gl_version (ctx, &gl_major, &gl_minor); - ctx->glsl_major = 1; - ctx->glsl_minor = 2; - ctx->glsl_version_to_use = 140; - - glsl_version = (char *)ctx->glGetString (GL_SHADING_LANGUAGE_VERSION); - _cogl_gl_util_parse_gl_version (glsl_version, - &ctx->glsl_major, - &ctx->glsl_minor); - COGL_FLAGS_SET (ctx->features, COGL_FEATURE_ID_UNSIGNED_INT_INDICES, TRUE); diff --git a/cogl/cogl/driver/gl/gles/cogl-driver-gles.c b/cogl/cogl/driver/gl/gles/cogl-driver-gles.c index dda16bc73..ad8fc65bd 100644 --- a/cogl/cogl/driver/gl/gles/cogl-driver-gles.c +++ b/cogl/cogl/driver/gl/gles/cogl-driver-gles.c @@ -565,8 +565,80 @@ _cogl_get_gl_version (CoglContext *ctx, } static gboolean -_cogl_driver_update_features (CoglContext *context, - GError **error) +check_gl_version (CoglContext *ctx, + GError **error) +{ + int major, minor; + + if (!_cogl_get_gl_version (ctx, &major, &minor)) + { + g_set_error (error, + COGL_DRIVER_ERROR, + COGL_DRIVER_ERROR_UNKNOWN_VERSION, + "The GLES version could not be determined"); + return FALSE; + } + + if (!COGL_CHECK_GL_VERSION (major, minor, 2, 0)) + { + g_set_error (error, + COGL_DRIVER_ERROR, + COGL_DRIVER_ERROR_INVALID_VERSION, + "OpenGL ES 2.0 or better is required"); + return FALSE; + } + + return TRUE; +} + +static gboolean +_cogl_get_glsl_version (CoglContext *ctx, + int *major_out, + int *minor_out) +{ + const char *version_string; + + version_string = (char *)ctx->glGetString (GL_SHADING_LANGUAGE_VERSION); + + if (!g_str_has_prefix (version_string, "OpenGL ES GLSL ES ")) + return FALSE; + + return _cogl_gl_util_parse_gl_version (version_string + 18, + major_out, + minor_out); +} + +static gboolean +check_glsl_version (CoglContext *ctx, + GError **error) +{ + int major, minor; + + if (!_cogl_get_glsl_version (ctx, &major, &minor)) + { + g_set_error (error, + COGL_DRIVER_ERROR, + COGL_DRIVER_ERROR_UNKNOWN_VERSION, + "The supported GLSL version could not be determined"); + return FALSE; + } + + if (!COGL_CHECK_GL_VERSION (major, minor, ctx->glsl_major, ctx->glsl_minor)) + { + g_set_error (error, + COGL_DRIVER_ERROR, + COGL_DRIVER_ERROR_INVALID_VERSION, + "GLSL ES %d%d0 or better is required", + ctx->glsl_major, ctx->glsl_minor); + return FALSE; + } + + return TRUE; +} + +static gboolean +_cogl_driver_update_features (CoglContext *context, + GError **error) { unsigned long private_features [COGL_FLAGS_N_LONGS_FOR_SIZE (COGL_N_PRIVATE_FEATURES)] = { 0 }; @@ -586,6 +658,16 @@ _cogl_driver_update_features (CoglContext *context, gl_extensions = _cogl_context_get_gl_extensions (context); + if (!check_gl_version (context, error)) + return FALSE; + + context->glsl_major = 1; + context->glsl_minor = 0; + context->glsl_version_to_use = 100; + + if (!check_glsl_version (context, error)) + return FALSE; + if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_WINSYS))) { g_autofree char *all_extensions = g_strjoinv (" ", gl_extensions); @@ -602,24 +684,7 @@ _cogl_driver_update_features (CoglContext *context, all_extensions); } - context->glsl_major = 1; - context->glsl_minor = 0; - context->glsl_version_to_use = 100; - - if (!_cogl_get_gl_version (context, &gl_major, &gl_minor)) - { - gl_major = 1; - gl_minor = 1; - } - - if (!COGL_CHECK_GL_VERSION (gl_major, gl_minor, 2, 0)) - { - g_set_error (error, - COGL_DRIVER_ERROR, - COGL_DRIVER_ERROR_INVALID_VERSION, - "OpenGL ES 2.0 or better is required"); - return FALSE; - } + _cogl_get_gl_version (context, &gl_major, &gl_minor); _cogl_feature_check_ext_functions (context, gl_major,