From cb178b7a3af3bd60380efb7eb92552e4d021d4c2 Mon Sep 17 00:00:00 2001 From: Neil Roberts Date: Sun, 25 Aug 2013 01:59:31 +0100 Subject: [PATCH] Always add the #version pragma to shaders Previously we would only add the #version pragma to shaders when point sprite texture coordinates are enabled for a layer so that we can access the gl_PointCoord builtin. However I don't think there's any good reason not to just always request GLSL version 1.2 if it's available. That way applications can always use gl_PointCoord without having to enable point sprite texture coordinates. This adds a glsl_version_to_use member to CoglContext which is used to generate the #version pragma as part of the shader boilerplate. On desktop GL this is set to 120 if version 1.2 is available, otherwise it is left at 110. On GLES it is always left as 100. Reviewed-by: Robert Bragg (cherry picked from commit e4dfe8b07e8af111ecbcb0da20ff2a2875a2b5d0) Conflicts: cogl/driver/gl/gl/cogl-driver-gl.c --- cogl/cogl-context-private.h | 8 ++++++++ cogl/cogl-glsl-shader-private.h | 1 - cogl/cogl-glsl-shader.c | 13 +++++++------ cogl/cogl-shader.c | 12 ------------ cogl/driver/gl/cogl-pipeline-fragend-glsl.c | 18 ++---------------- cogl/driver/gl/cogl-pipeline-vertend-glsl.c | 1 - cogl/driver/gl/gl/cogl-driver-gl.c | 7 +++++++ cogl/driver/gl/gles/cogl-driver-gles.c | 1 + 8 files changed, 25 insertions(+), 36 deletions(-) diff --git a/cogl/cogl-context-private.h b/cogl/cogl-context-private.h index 3d7ab6daa..91a264a6b 100644 --- a/cogl/cogl-context-private.h +++ b/cogl/cogl-context-private.h @@ -81,6 +81,14 @@ struct _CoglContext int glsl_major; int glsl_minor; + /* This is the GLSL version that we will claim that snippets are + * written against using the #version pragma. This will be the + * largest version that is less than or equal to the version + * provided by the driver without massively altering the syntax. Eg, + * we wouldn't use version 1.3 even if it is available because that + * removes the ‘attribute’ and ‘varying’ keywords. */ + int glsl_version_to_use; + /* Features cache */ unsigned long features[COGL_FLAGS_N_LONGS_FOR_SIZE (_COGL_N_FEATURE_IDS)]; CoglFeatureFlags feature_flags; /* legacy/deprecated feature flags */ diff --git a/cogl/cogl-glsl-shader-private.h b/cogl/cogl-glsl-shader-private.h index 816aec243..c3e6a6352 100644 --- a/cogl/cogl-glsl-shader-private.h +++ b/cogl/cogl-glsl-shader-private.h @@ -25,7 +25,6 @@ void _cogl_glsl_shader_set_source_with_boilerplate (CoglContext *ctx, - const char *version_string, GLuint shader_gl_handle, GLenum shader_gl_type, CoglPipeline *pipeline, diff --git a/cogl/cogl-glsl-shader.c b/cogl/cogl-glsl-shader.c index d7779d896..66474bf1a 100644 --- a/cogl/cogl-glsl-shader.c +++ b/cogl/cogl-glsl-shader.c @@ -71,7 +71,6 @@ add_layer_fragment_boilerplate_cb (CoglPipelineLayer *layer, void _cogl_glsl_shader_set_source_with_boilerplate (CoglContext *ctx, - const char *version_string, GLuint shader_gl_handle, GLenum shader_gl_type, CoglPipeline *pipeline, @@ -84,6 +83,7 @@ _cogl_glsl_shader_set_source_with_boilerplate (CoglContext *ctx, const char **strings = g_alloca (sizeof (char *) * (count_in + 4)); GLint *lengths = g_alloca (sizeof (GLint) * (count_in + 4)); + char *version_string; int count = 0; int n_layers; @@ -91,11 +91,10 @@ _cogl_glsl_shader_set_source_with_boilerplate (CoglContext *ctx, vertex_boilerplate = _COGL_VERTEX_SHADER_BOILERPLATE; fragment_boilerplate = _COGL_FRAGMENT_SHADER_BOILERPLATE; - if (version_string) - { - strings[count] = version_string; - lengths[count++] = -1; - } + version_string = g_strdup_printf ("#version %i\n\n", + ctx->glsl_version_to_use); + strings[count] = version_string; + lengths[count++] = -1; if (ctx->private_feature_flags & COGL_PRIVATE_FEATURE_GL_EMBEDDED && cogl_has_feature (ctx, COGL_FEATURE_ID_TEXTURE_3D)) @@ -182,4 +181,6 @@ _cogl_glsl_shader_set_source_with_boilerplate (CoglContext *ctx, GE( ctx, glShaderSource (shader_gl_handle, count, (const char **) strings, lengths) ); + + g_free (version_string); } diff --git a/cogl/cogl-shader.c b/cogl/cogl-shader.c index 31674e348..c28d4a223 100644 --- a/cogl/cogl-shader.c +++ b/cogl/cogl-shader.c @@ -185,7 +185,6 @@ _cogl_shader_compile_real (CoglHandle handle, CoglPipeline *pipeline) { CoglShader *shader = handle; - const char *version; _COGL_GET_CONTEXT (ctx, NO_RETVAL); @@ -263,18 +262,7 @@ _cogl_shader_compile_real (CoglHandle handle, shader->gl_handle = ctx->glCreateShader (gl_type); - if ((ctx->driver == COGL_DRIVER_GL || - ctx->driver == COGL_DRIVER_GL3 ) && - ctx->glsl_major == 1 && - ctx->glsl_minor >= 2) - { - version = "#version 120\n"; - } - else - version = NULL; - _cogl_glsl_shader_set_source_with_boilerplate (ctx, - version, shader->gl_handle, gl_type, pipeline, diff --git a/cogl/driver/gl/cogl-pipeline-fragend-glsl.c b/cogl/driver/gl/cogl-pipeline-fragend-glsl.c index 069a1fee4..2073a9234 100644 --- a/cogl/driver/gl/cogl-pipeline-fragend-glsl.c +++ b/cogl/driver/gl/cogl-pipeline-fragend-glsl.c @@ -90,8 +90,6 @@ typedef struct GString *header, *source; UnitState *unit_state; - CoglBool ref_point_coord; - /* List of layers that we haven't generated code for yet. These are in reverse order. As soon as we're about to generate code for layer we'll remove it from the list so we don't generate it @@ -113,7 +111,6 @@ shader_state_new (int n_layers) shader_state = g_slice_new0 (CoglPipelineShaderState); shader_state->ref_count = 1; shader_state->unit_state = g_new0 (UnitState, n_layers); - shader_state->ref_point_coord = FALSE; return shader_state; } @@ -423,11 +420,8 @@ ensure_texture_lookup_generated (CoglPipelineShaderState *shader_state, if (cogl_pipeline_get_layer_point_sprite_coords_enabled (pipeline, layer->index)) - { - shader_state->ref_point_coord = TRUE; - g_string_append_printf (shader_state->source, - "vec4 (gl_PointCoord, 0.0, 1.0)"); - } + g_string_append_printf (shader_state->source, + "vec4 (gl_PointCoord, 0.0, 1.0)"); else g_string_append_printf (shader_state->source, "cogl_tex_coord%i_in", @@ -990,7 +984,6 @@ _cogl_pipeline_fragend_glsl_end (CoglPipeline *pipeline, GLint compile_status; GLuint shader; CoglPipelineSnippetData snippet_data; - const char *version_string; COGL_STATIC_COUNTER (fragend_glsl_compile_counter, "glsl fragment compile counter", @@ -1053,14 +1046,7 @@ _cogl_pipeline_fragend_glsl_end (CoglPipeline *pipeline, lengths[1] = shader_state->source->len; source_strings[1] = shader_state->source->str; - if (shader_state->ref_point_coord && - !(ctx->private_feature_flags & COGL_PRIVATE_FEATURE_GL_EMBEDDED)) - version_string = "#version 120\n"; - else - version_string = NULL; - _cogl_glsl_shader_set_source_with_boilerplate (ctx, - version_string, shader, GL_FRAGMENT_SHADER, pipeline, 2, /* count */ diff --git a/cogl/driver/gl/cogl-pipeline-vertend-glsl.c b/cogl/driver/gl/cogl-pipeline-vertend-glsl.c index 57b62653d..e4f91d954 100644 --- a/cogl/driver/gl/cogl-pipeline-vertend-glsl.c +++ b/cogl/driver/gl/cogl-pipeline-vertend-glsl.c @@ -507,7 +507,6 @@ _cogl_pipeline_vertend_glsl_end (CoglPipeline *pipeline, source_strings[1] = shader_state->source->str; _cogl_glsl_shader_set_source_with_boilerplate (ctx, - NULL, shader, GL_VERTEX_SHADER, pipeline, 2, /* count */ diff --git a/cogl/driver/gl/gl/cogl-driver-gl.c b/cogl/driver/gl/gl/cogl-driver-gl.c index 5dc783bb1..5f615f56e 100644 --- a/cogl/driver/gl/gl/cogl-driver-gl.c +++ b/cogl/driver/gl/gl/cogl-driver-gl.c @@ -414,6 +414,13 @@ _cogl_driver_update_features (CoglContext *ctx, parse_gl_version (glsl_version, &ctx->glsl_major, &ctx->glsl_minor); } + if (COGL_CHECK_GL_VERSION (ctx->glsl_major, ctx->glsl_minor, 1, 2)) + /* We want to use version 120 if it is available so that the + * gl_PointCoord can be used. */ + ctx->glsl_version_to_use = 120; + else + ctx->glsl_version_to_use = 110; + flags = (COGL_FEATURE_TEXTURE_READ_PIXELS | COGL_FEATURE_UNSIGNED_INT_INDICES | COGL_FEATURE_DEPTH_RANGE); diff --git a/cogl/driver/gl/gles/cogl-driver-gles.c b/cogl/driver/gl/gles/cogl-driver-gles.c index ad9f8d1ba..8e798aff9 100644 --- a/cogl/driver/gl/gles/cogl-driver-gles.c +++ b/cogl/driver/gl/gles/cogl-driver-gles.c @@ -224,6 +224,7 @@ _cogl_driver_update_features (CoglContext *context, context->glsl_major = 1; context->glsl_minor = 0; + context->glsl_version_to_use = 100; _cogl_gpu_info_init (context, &context->gpu);