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 <robert@linux.intel.com>
(cherry picked from commit e4dfe8b07e8af111ecbcb0da20ff2a2875a2b5d0)

Conflicts:
	cogl/driver/gl/gl/cogl-driver-gl.c
This commit is contained in:
Neil Roberts 2013-08-25 01:59:31 +01:00
parent e67e487daf
commit cb178b7a3a
8 changed files with 25 additions and 36 deletions

View File

@ -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 */

View File

@ -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,

View File

@ -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);
}

View File

@ -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,

View File

@ -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 */

View File

@ -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 */

View File

@ -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);

View File

@ -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);