mirror of
https://github.com/brl/mutter.git
synced 2025-08-02 22:54:44 +00:00
cogl-shader: Prepend boilerplate for portable shaders
We now prepend a set of defines to any given GLSL shader so that we can define builtin uniforms/attributes within the "cogl" namespace that we can use to provide compatibility across a range of the earlier versions of GLSL. This updates test-cogl-shader-glsl.c and test-shader.c so they no longer needs to special case GLES vs GL when splicing together its shaders as well as the blur, colorize and desaturate effects. To get a feel for the new, portable uniform/attribute names here are the defines for OpenGL vertex shaders: #define cogl_position_in gl_Vertex #define cogl_color_in gl_Color #define cogl_tex_coord_in gl_MultiTexCoord0 #define cogl_tex_coord0_in gl_MultiTexCoord0 #define cogl_tex_coord1_in gl_MultiTexCoord1 #define cogl_tex_coord2_in gl_MultiTexCoord2 #define cogl_tex_coord3_in gl_MultiTexCoord3 #define cogl_tex_coord4_in gl_MultiTexCoord4 #define cogl_tex_coord5_in gl_MultiTexCoord5 #define cogl_tex_coord6_in gl_MultiTexCoord6 #define cogl_tex_coord7_in gl_MultiTexCoord7 #define cogl_normal_in gl_Normal #define cogl_position_out gl_Position #define cogl_point_size_out gl_PointSize #define cogl_color_out gl_FrontColor #define cogl_tex_coord_out gl_TexCoord #define cogl_modelview_matrix gl_ModelViewMatrix #define cogl_modelview_projection_matrix gl_ModelViewProjectionMatrix #define cogl_projection_matrix gl_ProjectionMatrix #define cogl_texture_matrix gl_TextureMatrix And for fragment shaders we have: #define cogl_color_in gl_Color #define cogl_tex_coord_in gl_TexCoord #define cogl_color_out gl_FragColor #define cogl_depth_out gl_FragDepth #define cogl_front_facing gl_FrontFacing
This commit is contained in:
@@ -7,8 +7,8 @@ precision highp float;
|
||||
/*** _cogl_fixed_fragment_shader_inputs ***/
|
||||
|
||||
/* Inputs from the vertex shader */
|
||||
varying vec4 frag_color;
|
||||
varying float fog_amount;
|
||||
varying vec4 _cogl_color;
|
||||
varying float _cogl_fog_amount;
|
||||
|
||||
/*** _cogl_fixed_fragment_shader_texturing_options ***/
|
||||
|
||||
@@ -17,10 +17,10 @@ varying float fog_amount;
|
||||
/*** _cogl_fixed_fragment_shader_fogging_options ***/
|
||||
|
||||
/* Fogging options */
|
||||
uniform vec4 fog_color;
|
||||
uniform vec4 _cogl_fog_color;
|
||||
|
||||
/* Alpha test options */
|
||||
uniform float alpha_test_ref;
|
||||
uniform float _cogl_alpha_test_ref;
|
||||
|
||||
/*** _cogl_fixed_fragment_shader_main_declare ***/
|
||||
|
||||
@@ -33,30 +33,32 @@ main (void)
|
||||
/*** _cogl_fixed_fragment_shader_fog ***/
|
||||
|
||||
/* Mix the calculated color with the fog color */
|
||||
gl_FragColor.rgb = mix (fog_color.rgb, gl_FragColor.rgb, fog_amount);
|
||||
gl_FragColor.rgb = mix (_cogl_fog_color.rgb, gl_FragColor.rgb,
|
||||
_cogl_fog_amount);
|
||||
|
||||
/* Alpha testing */
|
||||
|
||||
/*** _cogl_fixed_fragment_shader_alpha_never ***/
|
||||
discard;
|
||||
/*** _cogl_fixed_fragment_shader_alpha_less ***/
|
||||
if (gl_FragColor.a >= alpha_test_ref)
|
||||
if (gl_FragColor.a >= _cogl_alpha_test_ref)
|
||||
discard;
|
||||
/*** _cogl_fixed_fragment_shader_alpha_equal ***/
|
||||
if (gl_FragColor.a != alpha_test_ref)
|
||||
if (gl_FragColor.a != _cogl_alpha_test_ref)
|
||||
discard;
|
||||
/*** _cogl_fixed_fragment_shader_alpha_lequal ***/
|
||||
if (gl_FragColor.a > alpha_test_ref)
|
||||
if (gl_FragColor.a > _cogl_alpha_test_ref)
|
||||
discard;
|
||||
/*** _cogl_fixed_fragment_shader_alpha_greater ***/
|
||||
if (gl_FragColor.a <= alpha_test_ref)
|
||||
if (gl_FragColor.a <= _cogl_alpha_test_ref)
|
||||
discard;
|
||||
/*** _cogl_fixed_fragment_shader_alpha_notequal ***/
|
||||
if (gl_FragColor.a == alpha_test_ref)
|
||||
if (gl_FragColor.a == _cogl_alpha_test_ref)
|
||||
discard;
|
||||
/*** _cogl_fixed_fragment_shader_alpha_gequal ***/
|
||||
if (gl_FragColor.a < alpha_test_ref)
|
||||
if (gl_FragColor.a < _cogl_alpha_test_ref)
|
||||
discard;
|
||||
|
||||
/*** _cogl_fixed_fragment_shader_end ***/
|
||||
}
|
||||
|
||||
|
@@ -1,30 +1,30 @@
|
||||
/*** _cogl_fixed_vertex_shader_per_vertex_attribs ***/
|
||||
|
||||
/* Per vertex attributes */
|
||||
attribute vec4 vertex_attrib;
|
||||
attribute vec4 color_attrib;
|
||||
attribute vec4 cogl_position_in;
|
||||
attribute vec4 cogl_color_in;
|
||||
|
||||
/*** _cogl_fixed_vertex_shader_transform_matrices ***/
|
||||
|
||||
/* Transformation matrices */
|
||||
uniform mat4 modelview_matrix;
|
||||
uniform mat4 mvp_matrix; /* combined modelview and projection matrix */
|
||||
uniform mat4 cogl_modelview_matrix;
|
||||
uniform mat4 cogl_modelview_projection_matrix; /* combined modelview and projection matrix */
|
||||
|
||||
/*** _cogl_fixed_vertex_shader_output_variables ***/
|
||||
|
||||
/* Outputs to the fragment shader */
|
||||
varying vec4 frag_color;
|
||||
varying float fog_amount;
|
||||
varying vec4 _cogl_color;
|
||||
varying float _cogl_fog_amount;
|
||||
|
||||
/*** _cogl_fixed_vertex_shader_fogging_options ***/
|
||||
|
||||
/* Fogging options */
|
||||
uniform float fog_density;
|
||||
uniform float fog_start;
|
||||
uniform float fog_end;
|
||||
uniform float _cogl_fog_density;
|
||||
uniform float _cogl_fog_start;
|
||||
uniform float _cogl_fog_end;
|
||||
|
||||
/* Point options */
|
||||
uniform float point_size;
|
||||
uniform float cogl_point_size_in;
|
||||
|
||||
/*** _cogl_fixed_vertex_shader_main_start ***/
|
||||
|
||||
@@ -34,38 +34,40 @@ main (void)
|
||||
vec4 transformed_tex_coord;
|
||||
|
||||
/* Calculate the transformed position */
|
||||
gl_Position = mvp_matrix * vertex_attrib;
|
||||
gl_Position = cogl_modelview_projection_matrix * cogl_position_in;
|
||||
|
||||
/* Copy across the point size from the uniform */
|
||||
gl_PointSize = point_size;
|
||||
gl_PointSize = cogl_point_size_in;
|
||||
|
||||
/* Calculate the transformed texture coordinate */
|
||||
|
||||
/*** _cogl_fixed_vertex_shader_frag_color_start ***/
|
||||
|
||||
/* Pass the interpolated vertex color on to the fragment shader */
|
||||
frag_color = color_attrib;
|
||||
_cogl_color = cogl_color_in;
|
||||
|
||||
/*** _cogl_fixed_vertex_shader_fog_start ***/
|
||||
|
||||
/* Estimate the distance from the eye using just the z-coordinate to
|
||||
use as the fog coord */
|
||||
vec4 eye_coord = modelview_matrix * vertex_attrib;
|
||||
vec4 eye_coord = cogl_modelview_matrix * cogl_position_in;
|
||||
float fog_coord = abs (eye_coord.z / eye_coord.w);
|
||||
|
||||
/* Calculate the fog amount per-vertex and interpolate it for the
|
||||
fragment shader */
|
||||
|
||||
/*** _cogl_fixed_vertex_shader_fog_exp ***/
|
||||
fog_amount = exp (-fog_density * fog_coord);
|
||||
_cogl_fog_amount = exp (-fog_density * fog_coord);
|
||||
/*** _cogl_fixed_vertex_shader_fog_exp2 ***/
|
||||
fog_amount = exp (-fog_density * fog_coord
|
||||
* fog_density * fog_coord);
|
||||
_cogl_fog_amount = exp (-_cogl_fog_density * fog_coord
|
||||
* _cogl_fog_density * fog_coord);
|
||||
/*** _cogl_fixed_vertex_shader_fog_linear ***/
|
||||
fog_amount = (fog_end - fog_coord) / (fog_end - fog_start);
|
||||
_cogl_fog_amount = (_cogl_fog_end - fog_coord) /
|
||||
(_cogl_fog_end - _cogl_fog_start);
|
||||
|
||||
/*** _cogl_fixed_vertex_shader_fog_end ***/
|
||||
fog_amount = clamp (fog_amount, 0.0, 1.0);
|
||||
_cogl_fog_amount = clamp (_cogl_fog_amount, 0.0, 1.0);
|
||||
|
||||
/*** _cogl_fixed_vertex_shader_end ***/
|
||||
}
|
||||
|
||||
|
@@ -318,7 +318,7 @@ cogl_gles2_get_vertex_shader (const CoglGles2WrapperSettings *settings)
|
||||
for (i = 0; i < COGL_GLES2_MAX_TEXTURE_UNITS; i++)
|
||||
if (COGL_GLES2_TEXTURE_UNIT_IS_ENABLED (settings->texture_units, i))
|
||||
g_string_append_printf (shader_source,
|
||||
"attribute vec4 multi_tex_coord_attrib%d;\n",
|
||||
"attribute vec4 cogl_tex_coord%d_in;\n",
|
||||
i);
|
||||
|
||||
/* Find the biggest enabled texture unit index */
|
||||
@@ -332,11 +332,11 @@ cogl_gles2_get_vertex_shader (const CoglGles2WrapperSettings *settings)
|
||||
if (n_texture_units > 0)
|
||||
{
|
||||
g_string_append_printf (shader_source,
|
||||
"uniform mat4 texture_matrix[%d];\n",
|
||||
"uniform mat4 cogl_texture_matrix[%d];\n",
|
||||
n_texture_units);
|
||||
|
||||
g_string_append_printf (shader_source,
|
||||
"varying vec2 tex_coord[%d];",
|
||||
"varying vec2 _cogl_tex_coord[%d];",
|
||||
n_texture_units);
|
||||
}
|
||||
|
||||
@@ -349,11 +349,11 @@ cogl_gles2_get_vertex_shader (const CoglGles2WrapperSettings *settings)
|
||||
{
|
||||
g_string_append_printf (shader_source,
|
||||
"transformed_tex_coord = "
|
||||
"texture_matrix[%d] "
|
||||
" * multi_tex_coord_attrib%d;\n",
|
||||
"cogl_texture_matrix[%d] "
|
||||
" * cogl_tex_coord%d_in;\n",
|
||||
i, i);
|
||||
g_string_append_printf (shader_source,
|
||||
"tex_coord[%d] = transformed_tex_coord.st "
|
||||
"_cogl_tex_coord[%d] = transformed_tex_coord.st "
|
||||
" / transformed_tex_coord.q;\n",
|
||||
i);
|
||||
}
|
||||
@@ -410,19 +410,21 @@ cogl_gles2_add_texture_lookup (int unit,
|
||||
_COGL_GET_GLES2_WRAPPER (w, NO_RETVAL);
|
||||
|
||||
if (w->settings.tex_env[unit].texture_target == GL_TEXTURE_3D_OES)
|
||||
g_string_append_printf (shader_source, "texture3D (texture_unit[%d], ",
|
||||
g_string_append_printf (shader_source,
|
||||
"texture3D (_cogl_texture_unit[%d], ",
|
||||
unit);
|
||||
else
|
||||
g_string_append_printf (shader_source, "texture2D (texture_unit[%d], ",
|
||||
g_string_append_printf (shader_source,
|
||||
"texture2D (_cogl_texture_unit[%d], ",
|
||||
unit);
|
||||
|
||||
/* If point sprite coord generation is being used then divert to the
|
||||
built-in varying var for that instead of the texture
|
||||
coordinates */
|
||||
if (w->settings.tex_env[unit].point_sprite_coords)
|
||||
g_string_append (shader_source, "gl_PointCoord");
|
||||
g_string_append (shader_source, "_cogl_point_coord");
|
||||
else
|
||||
g_string_append_printf (shader_source, "tex_coord[%d]", unit);
|
||||
g_string_append_printf (shader_source, "_cogl_tex_coord[%d]", unit);
|
||||
|
||||
g_string_append_printf (shader_source, ").%s", swizzle);
|
||||
}
|
||||
@@ -458,7 +460,7 @@ cogl_gles2_add_arg (int unit,
|
||||
break;
|
||||
|
||||
case GL_CONSTANT:
|
||||
g_string_append_printf (shader_source, "combine_constant[%d].%s",
|
||||
g_string_append_printf (shader_source, "_cogl_combine_constant[%d].%s",
|
||||
unit, swizzle);
|
||||
break;
|
||||
|
||||
@@ -470,7 +472,7 @@ cogl_gles2_add_arg (int unit,
|
||||
}
|
||||
/* flow through */
|
||||
case GL_PRIMARY_COLOR:
|
||||
g_string_append_printf (shader_source, "frag_color.%s", swizzle);
|
||||
g_string_append_printf (shader_source, "_cogl_color.%s", swizzle);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -635,13 +637,13 @@ cogl_gles2_get_fragment_shader (const CoglGles2WrapperSettings *settings)
|
||||
if (n_texture_units > 0)
|
||||
{
|
||||
g_string_append_printf (shader_source,
|
||||
"varying vec2 tex_coord[%d];\n",
|
||||
"varying vec2 _cogl_tex_coord[%d];\n",
|
||||
n_texture_units);
|
||||
|
||||
g_string_append (shader_source,
|
||||
_cogl_fixed_fragment_shader_texturing_options);
|
||||
g_string_append_printf (shader_source,
|
||||
"uniform sampler2D texture_unit[%d];\n",
|
||||
"uniform sampler2D _cogl_texture_unit[%d];\n",
|
||||
n_texture_units);
|
||||
}
|
||||
|
||||
@@ -655,12 +657,12 @@ cogl_gles2_get_fragment_shader (const CoglGles2WrapperSettings *settings)
|
||||
apparent bug in the PowerVR drivers. Without it the alpha
|
||||
blending seems to stop working */
|
||||
g_string_append (shader_source,
|
||||
"vec4 frag_color_copy = frag_color;\n");
|
||||
"vec4 frag_color_copy = _cogl_color;\n");
|
||||
|
||||
/* If there are no textures units enabled then we can just directly
|
||||
use the color from the vertex shader */
|
||||
if (n_texture_units == 0)
|
||||
g_string_append (shader_source, "gl_FragColor = frag_color;\n");
|
||||
g_string_append (shader_source, "gl_FragColor = _cogl_color;\n");
|
||||
else
|
||||
/* Otherwise we need to calculate the value based on the layer
|
||||
combine settings */
|
||||
@@ -769,17 +771,17 @@ cogl_gles2_wrapper_get_locations (GLuint program,
|
||||
int i;
|
||||
|
||||
uniforms->mvp_matrix_uniform
|
||||
= glGetUniformLocation (program, "mvp_matrix");
|
||||
= glGetUniformLocation (program, "cogl_modelview_projection_matrix");
|
||||
uniforms->modelview_matrix_uniform
|
||||
= glGetUniformLocation (program, "modelview_matrix");
|
||||
= glGetUniformLocation (program, "cogl_modelview_matrix");
|
||||
|
||||
for (i = 0; i < COGL_GLES2_MAX_TEXTURE_UNITS; i++)
|
||||
if (COGL_GLES2_TEXTURE_UNIT_IS_ENABLED (settings->texture_units, i))
|
||||
{
|
||||
char *matrix_var_name = g_strdup_printf ("texture_matrix[%d]", i);
|
||||
char *sampler_var_name = g_strdup_printf ("texture_unit[%d]", i);
|
||||
char *matrix_var_name = g_strdup_printf ("cogl_texture_matrix[%d]", i);
|
||||
char *sampler_var_name = g_strdup_printf ("_cogl_texture_unit[%d]", i);
|
||||
char *tex_coord_var_name =
|
||||
g_strdup_printf ("multi_tex_coord_attrib%d", i);
|
||||
g_strdup_printf ("cogl_tex_coord%d_in", i);
|
||||
|
||||
uniforms->texture_matrix_uniforms[i]
|
||||
= glGetUniformLocation (program, matrix_var_name);
|
||||
@@ -800,30 +802,30 @@ cogl_gles2_wrapper_get_locations (GLuint program,
|
||||
}
|
||||
|
||||
uniforms->fog_density_uniform
|
||||
= glGetUniformLocation (program, "fog_density");
|
||||
= glGetUniformLocation (program, "_cogl_fog_density");
|
||||
uniforms->fog_start_uniform
|
||||
= glGetUniformLocation (program, "fog_start");
|
||||
= glGetUniformLocation (program, "_cogl_fog_start");
|
||||
uniforms->fog_end_uniform
|
||||
= glGetUniformLocation (program, "fog_end");
|
||||
= glGetUniformLocation (program, "_cogl_fog_end");
|
||||
uniforms->fog_color_uniform
|
||||
= glGetUniformLocation (program, "fog_color");
|
||||
= glGetUniformLocation (program, "_cogl_fog_color");
|
||||
|
||||
uniforms->alpha_test_ref_uniform
|
||||
= glGetUniformLocation (program, "alpha_test_ref");
|
||||
= glGetUniformLocation (program, "_cogl_alpha_test_ref");
|
||||
|
||||
uniforms->point_size_uniform
|
||||
= glGetUniformLocation (program, "point_size");
|
||||
= glGetUniformLocation (program, "cogl_point_size_in");
|
||||
}
|
||||
|
||||
static void
|
||||
cogl_gles2_wrapper_bind_attributes (GLuint program)
|
||||
{
|
||||
glBindAttribLocation (program, COGL_GLES2_WRAPPER_VERTEX_ATTRIB,
|
||||
"vertex_attrib");
|
||||
"cogl_position_in");
|
||||
glBindAttribLocation (program, COGL_GLES2_WRAPPER_COLOR_ATTRIB,
|
||||
"color_attrib");
|
||||
"cogl_color_in");
|
||||
glBindAttribLocation (program, COGL_GLES2_WRAPPER_NORMAL_ATTRIB,
|
||||
"normal_attrib");
|
||||
"cogl_normal_in");
|
||||
}
|
||||
|
||||
static CoglGles2WrapperProgram *
|
||||
|
Reference in New Issue
Block a user