[cogl/wip/rig: 24/33] support glsl >= 1.3 with varying/attribute deprecated



commit e952cc32ed7a634871a3f665fdc8cf931a3fdcaf
Author: Robert Bragg <robert bragg intel com>
Date:   Mon Apr 14 02:22:01 2014 +0100

    support glsl >= 1.3 with varying/attribute deprecated
    
    This adds support for glsl >= 1.3 where the varying and attribute
    keywords were removed. This makes cogl emit compatibility defines for
    the new in and out keywords so we can use these consistently across all
    versions of glsl.

 cogl/cogl-attribute.h                       |   12 +++---
 cogl/cogl-glsl-shader-boilerplate.h         |   13 +++----
 cogl/cogl-glsl-shader.c                     |   53 ++++++++++++++++++++++++++-
 cogl/cogl-snippet.h                         |   18 +++++-----
 cogl/driver/gl/cogl-pipeline-fragend-glsl.c |   19 ++++++++--
 cogl/driver/gl/cogl-pipeline-vertend-glsl.c |    6 ++--
 tests/conform/test-custom-attributes.c      |    4 +-
 tests/conform/test-gles2-context.c          |   10 +++---
 8 files changed, 96 insertions(+), 39 deletions(-)
---
diff --git a/cogl/cogl-attribute.h b/cogl/cogl-attribute.h
index d2e3290..d10dda0 100644
--- a/cogl/cogl-attribute.h
+++ b/cogl/cogl-attribute.h
@@ -195,7 +195,7 @@ cogl_attribute_new_const_1f (CoglContext *context,
  * code like:
  *
  * [|
- * attribute vec2 name;
+ * in vec2 name;
  * |]
  *
  * Return value: (transfer full): A newly allocated #CoglAttribute
@@ -224,7 +224,7 @@ cogl_attribute_new_const_2f (CoglContext *context,
  * declaration in GLSL code like:
  *
  * [|
- * attribute vec3 name;
+ * in vec3 name;
  * |]
  *
  * unless the built in name "cogl_normal_in" is being used where no
@@ -258,7 +258,7 @@ cogl_attribute_new_const_3f (CoglContext *context,
  * corresponding declaration in GLSL code like:
  *
  * [|
- * attribute vec4 name;
+ * in vec4 name;
  * |]
  *
  * unless one of the built in names "cogl_color_in",
@@ -291,7 +291,7 @@ cogl_attribute_new_const_4f (CoglContext *context,
  * like:
  *
  * [|
- * attribute vec2 name;
+ * in vec2 name;
  * |]
  *
  * Return value: (transfer full): A newly allocated #CoglAttribute
@@ -317,7 +317,7 @@ cogl_attribute_new_const_2fv (CoglContext *context,
  * declaration in GLSL code like:
  *
  * [|
- * attribute vec3 name;
+ * in vec3 name;
  * |]
  *
  * unless the built in name "cogl_normal_in" is being used where no
@@ -346,7 +346,7 @@ cogl_attribute_new_const_3fv (CoglContext *context,
  * declaration in GLSL code like:
  *
  * [|
- * attribute vec4 name;
+ * in vec4 name;
  * |]
  *
  * unless one of the built in names "cogl_color_in",
diff --git a/cogl/cogl-glsl-shader-boilerplate.h b/cogl/cogl-glsl-shader-boilerplate.h
index 6a6022e..2d360d5 100644
--- a/cogl/cogl-glsl-shader-boilerplate.h
+++ b/cogl/cogl-glsl-shader-boilerplate.h
@@ -48,14 +48,14 @@
 #define _COGL_VERTEX_SHADER_BOILERPLATE \
   _COGL_COMMON_SHADER_BOILERPLATE \
   "#define cogl_color_out _cogl_color\n" \
-  "varying vec4 _cogl_color;\n" \
+  "out vec4 _cogl_color;\n" \
   "#define cogl_position_out gl_Position\n" \
   "#define cogl_point_size_out gl_PointSize\n" \
   "\n" \
-  "attribute vec4 cogl_color_in;\n" \
-  "attribute vec4 cogl_position_in;\n" \
+  "in vec4 cogl_color_in;\n" \
+  "in vec4 cogl_position_in;\n" \
   "#define cogl_tex_coord_in cogl_tex_coord0_in;\n" \
-  "attribute vec3 cogl_normal_in;\n"
+  "in vec3 cogl_normal_in;\n"
 
 #define _COGL_FRAGMENT_SHADER_BOILERPLATE \
   "#ifdef GL_ES\n" \
@@ -63,13 +63,10 @@
   "#endif\n" \
   _COGL_COMMON_SHADER_BOILERPLATE \
   "\n" \
-  "varying vec4 _cogl_color;\n" \
+  "in vec4 _cogl_color;\n" \
   "\n" \
   "#define cogl_color_in _cogl_color\n" \
   "\n" \
-  "#define cogl_color_out gl_FragColor\n" \
-  "#define cogl_depth_out gl_FragDepth\n" \
-  "\n" \
   "#define cogl_front_facing gl_FrontFacing\n" \
   "\n" \
   "#define cogl_point_coord gl_PointCoord\n"
diff --git a/cogl/cogl-glsl-shader.c b/cogl/cogl-glsl-shader.c
index cd093c2..ada309e 100644
--- a/cogl/cogl-glsl-shader.c
+++ b/cogl/cogl-glsl-shader.c
@@ -56,8 +56,8 @@ _cogl_glsl_shader_set_source_with_boilerplate (CoglContext *ctx,
   const char *vertex_boilerplate;
   const char *fragment_boilerplate;
 
-  const char **strings = u_alloca (sizeof (char *) * (count_in + 4));
-  GLint *lengths = u_alloca (sizeof (GLint) * (count_in + 4));
+  const char **strings = u_alloca (sizeof (char *) * (count_in + 6));
+  GLint *lengths = u_alloca (sizeof (GLint) * (count_in + 6));
   char *version_string;
   int count = 0;
 
@@ -66,6 +66,7 @@ _cogl_glsl_shader_set_source_with_boilerplate (CoglContext *ctx,
 
   version_string = u_strdup_printf ("#version %i\n\n",
                                     ctx->glsl_version_to_use);
+
   strings[count] = version_string;
   lengths[count++] = -1;
 
@@ -80,13 +81,61 @@ _cogl_glsl_shader_set_source_with_boilerplate (CoglContext *ctx,
 
   if (shader_gl_type == GL_VERTEX_SHADER)
     {
+      if (ctx->glsl_version_to_use < 130)
+        {
+          strings[count] =
+            "#define in attribute\n"
+            "#define out varying\n";
+          lengths[count++] = -1;
+        }
+      else
+        {
+          /* To support source compatibility with glsl >= 1.3 which has replaced
+           * all of the texture sampler functions with one polymorphic texture()
+           * function we use the preprocessor to map the old names onto the new
+           * name...
+           */
+          strings[count] =
+            "#define texture2D texture\n"
+            "#define texture3D texture\n"
+            "#define textureRect texture\n";
+          lengths[count++] = -1;
+        }
+
       strings[count] = vertex_boilerplate;
       lengths[count++] = strlen (vertex_boilerplate);
     }
   else if (shader_gl_type == GL_FRAGMENT_SHADER)
     {
+      if (ctx->glsl_version_to_use < 130)
+        {
+          strings[count] = "#define in varying\n" \
+                            "\n" \
+                            "#define cogl_color_out gl_FragColor\n" \
+                            "#define cogl_depth_out gl_FragDepth\n";
+          lengths[count++] = -1;
+        }
+
       strings[count] = fragment_boilerplate;
       lengths[count++] = strlen (fragment_boilerplate);
+
+      if (ctx->glsl_version_to_use >= 130)
+        {
+          /* FIXME: Support cogl_depth_out. */
+          /* To support source compatibility with glsl >= 1.3 which has replaced
+           * all of the texture sampler functions with one polymorphic texture()
+           * function we use the preprocessor to map the old names onto the new
+           * name...
+           */
+          strings[count] =
+            "#define texture2D texture\n"
+            "#define texture3D texture\n"
+            "#define textureRect texture\n"
+            "\n"
+            "out vec4 cogl_color_out;\n";
+            //"out vec4 cogl_depth_out\n";
+          lengths[count++] = -1;
+        }
     }
 
   memcpy (strings + count, strings_in, sizeof (char *) * count_in);
diff --git a/cogl/cogl-snippet.h b/cogl/cogl-snippet.h
index 9296469..5ecd91d 100644
--- a/cogl/cogl-snippet.h
+++ b/cogl/cogl-snippet.h
@@ -154,21 +154,21 @@ COGL_BEGIN_DECLS
  *
  * <glosslist>
  *  <glossentry>
- *   <glossterm>attribute vec4
+ *   <glossterm>in vec4
  *         <emphasis>cogl_position_in</emphasis></glossterm>
  *   <glossdef><para>
  *    The incoming vertex position. This is equivalent to #gl_Vertex.
  *   </para></glossdef>
  *  </glossentry>
  *  <glossentry>
- *   <glossterm>attribute vec4
+ *   <glossterm>in vec4
  *         <emphasis>cogl_color_in</emphasis></glossterm>
  *   <glossdef><para>
  *    The incoming vertex color. This is equivalent to #gl_Color.
  *   </para></glossdef>
  *  </glossentry>
  *  <glossentry>
- *   <glossterm>attribute vec4
+ *   <glossterm>in vec4
  *         <emphasis>cogl_tex_coord_in</emphasis></glossterm>
  *   <glossdef><para>
  *    The texture coordinate for layer 0. This is an alternative name
@@ -176,7 +176,7 @@ COGL_BEGIN_DECLS
  *   </para></glossdef>
  *  </glossentry>
  *  <glossentry>
- *   <glossterm>attribute vec4
+ *   <glossterm>in vec4
  *         <emphasis>cogl_tex_coord0_in</emphasis></glossterm>
  *   <glossdef><para>
  *    The texture coordinate for the layer 0. This is equivalent to
@@ -185,7 +185,7 @@ COGL_BEGIN_DECLS
  *   </para></glossdef>
  *  </glossentry>
  *  <glossentry>
- *   <glossterm>attribute vec3
+ *   <glossterm>in vec3
  *         <emphasis>cogl_normal_in</emphasis></glossterm>
  *   <glossdef><para>
  *    The normal of the vertex. This is equivalent to #gl_Normal.
@@ -217,14 +217,14 @@ COGL_BEGIN_DECLS
  *   </para></glossdef>
  *  </glossentry>
  *  <glossentry>
- *   <glossterm>varying vec4
+ *   <glossterm>out vec4
  *         <emphasis>cogl_color_out</emphasis></glossterm>
  *   <glossdef><para>
  *    The calculated color of a vertex. This is equivalent to #gl_FrontColor.
  *   </para></glossdef>
  *  </glossentry>
  *  <glossentry>
- *   <glossterm>varying vec4
+ *   <glossterm>out vec4
  *         <emphasis>cogl_tex_coord0_out</emphasis></glossterm>
  *   <glossdef><para>
  *    The calculated texture coordinate for layer 0 of the pipeline.
@@ -240,13 +240,13 @@ COGL_BEGIN_DECLS
  *
  * <glosslist>
  *  <glossentry>
- *   <glossterm>varying vec4 <emphasis>cogl_color_in</emphasis></glossterm>
+ *   <glossterm>in vec4 <emphasis>cogl_color_in</emphasis></glossterm>
  *   <glossdef><para>
  *    The calculated color of a vertex. This is equivalent to #gl_FrontColor.
  *   </para></glossdef>
  *  </glossentry>
  *  <glossentry>
- *   <glossterm>varying vec4
+ *   <glossterm>in vec4
  *              <emphasis>cogl_tex_coord0_in</emphasis></glossterm>
  *   <glossdef><para>
  *    The texture coordinate for layer 0. This is equivalent to
diff --git a/cogl/driver/gl/cogl-pipeline-fragend-glsl.c b/cogl/driver/gl/cogl-pipeline-fragend-glsl.c
index 4391bba..85db8b4 100644
--- a/cogl/driver/gl/cogl-pipeline-fragend-glsl.c
+++ b/cogl/driver/gl/cogl-pipeline-fragend-glsl.c
@@ -241,7 +241,7 @@ add_layer_declaration_cb (CoglPipelineLayer *layer,
   _cogl_gl_util_get_texture_target_string (texture_type, &target_string, NULL);
 
   u_string_append_printf (shader_state->header,
-                          "varying vec4 _cogl_tex_coord%i;\n"
+                          "in vec4 _cogl_tex_coord%i;\n"
                           "#define cogl_tex_coord%i_in _cogl_tex_coord%i\n"
                           "uniform sampler%s cogl_sampler%i;\n",
                           layer->index,
@@ -456,9 +456,20 @@ ensure_texture_lookup_generated (CoglPipelineShaderState *shader_state,
         u_string_append (shader_state->header,
                          "vec4 (1.0, 1.0, 1.0, 1.0);\n");
       else
-        u_string_append_printf (shader_state->header,
-                                "texture%s (tex, coords.%s);\n",
-                                target_string, tex_coord_swizzle);
+        {
+          if (ctx->glsl_version_to_use >= 130)
+            {
+              u_string_append_printf (shader_state->header,
+                                      "texture (tex, coords.%s);\n",
+                                      tex_coord_swizzle);
+            }
+          else
+            {
+              u_string_append_printf (shader_state->header,
+                                      "texture%s (tex, coords.%s);\n",
+                                      target_string, tex_coord_swizzle);
+            }
+        }
 
       u_string_append (shader_state->header, "}\n");
     }
diff --git a/cogl/driver/gl/cogl-pipeline-vertend-glsl.c b/cogl/driver/gl/cogl-pipeline-vertend-glsl.c
index 1bb323f..067b9fa 100644
--- a/cogl/driver/gl/cogl-pipeline-vertend-glsl.c
+++ b/cogl/driver/gl/cogl-pipeline-vertend-glsl.c
@@ -177,8 +177,8 @@ add_layer_declaration_cb (CoglPipelineLayer *layer,
   _cogl_gl_util_get_texture_target_string (texture_type, &target_string, NULL);
 
   u_string_append_printf (shader_state->header,
-                          "attribute vec4 cogl_tex_coord%i_in;\n"
-                          "varying vec4 _cogl_tex_coord%i;\n"
+                          "in vec4 cogl_tex_coord%i_in;\n"
+                          "out vec4 _cogl_tex_coord%i;\n"
                           "#define cogl_tex_coord%i_out _cogl_tex_coord%i\n"
                           "uniform sampler%s cogl_sampler%i;\n",
                           layer->index,
@@ -303,7 +303,7 @@ _cogl_pipeline_vertend_glsl_start (CoglPipeline *pipeline,
 
   if (cogl_pipeline_get_per_vertex_point_size (pipeline))
     u_string_append (shader_state->header,
-                     "attribute float cogl_point_size_in;\n");
+                     "in float cogl_point_size_in;\n");
   else if (!_cogl_has_private_feature
            (ctx, COGL_PRIVATE_FEATURE_BUILTIN_POINT_SIZE_UNIFORM))
     {
diff --git a/tests/conform/test-custom-attributes.c b/tests/conform/test-custom-attributes.c
index 0c65c50..72f6721 100644
--- a/tests/conform/test-custom-attributes.c
+++ b/tests/conform/test-custom-attributes.c
@@ -233,7 +233,7 @@ test_short_verts (TestState *state, int offset_x, int offset_y)
    * be used internally... */
   pipeline2 = cogl_pipeline_new (test_ctx);
   snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_VERTEX,
-                              "attribute vec4 color;",
+                              "in vec4 color;",
                               "cogl_color_out = vec4 (0.0, 1.0, 0.0, 1.0);");
   cogl_pipeline_add_snippet (pipeline2, snippet);
 
@@ -287,7 +287,7 @@ test_custom_attributes (void)
 
   state.pipeline = cogl_pipeline_new (test_ctx);
   snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_VERTEX,
-                              "attribute vec4 color;",
+                              "in vec4 color;",
                               "cogl_color_out = color;");
   cogl_pipeline_add_snippet (state.pipeline, snippet);
 
diff --git a/tests/conform/test-gles2-context.c b/tests/conform/test-gles2-context.c
index 8494e60..1eadec9 100644
--- a/tests/conform/test-gles2-context.c
+++ b/tests/conform/test-gles2-context.c
@@ -634,7 +634,7 @@ void
 test_gles2_context_fbo (void)
 {
   static const char vertex_shader_source[] =
-    "attribute vec2 pos;\n"
+    "in vec2 pos;\n"
     "\n"
     "void\n"
     "main ()\n"
@@ -766,9 +766,9 @@ void
 test_gles2_context_copy_tex_image (void)
 {
   static const char vertex_shader_source[] =
-    "attribute vec2 pos;\n"
-    "attribute vec2 tex_coord_attrib;\n"
-    "varying vec2 tex_coord_varying;\n"
+    "in vec2 pos;\n"
+    "in vec2 tex_coord_attrib;\n"
+    "out vec2 tex_coord_varying;\n"
     "\n"
     "void\n"
     "main ()\n"
@@ -778,7 +778,7 @@ test_gles2_context_copy_tex_image (void)
     "}\n";
   static const char fragment_shader_source[] =
     "precision mediump float;\n"
-    "varying vec2 tex_coord_varying;\n"
+    "in vec2 tex_coord_varying;\n"
     "uniform sampler2D tex;\n"
     "\n"
     "void\n"


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]