[gtk+/wip/ebassi/gles: 5/10] demo: Add GLES support to the GtkGLArea demo



commit 872b5115ea1ee2335ea5277de66c813a7cd4959b
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Sat Apr 23 13:46:05 2016 +0100

    demo: Add GLES support to the GtkGLArea demo
    
    We need new shaders, and we need to select the correct shader when
    building the program we use to render the triangle.

 demos/gtk-demo/demo.gresource.xml                  |    6 +++-
 .../{glarea-fragment.glsl => glarea-gl.fs.glsl}    |    0
 .../{glarea-vertex.glsl => glarea-gl.vs.glsl}      |    0
 demos/gtk-demo/glarea-gles.fs.glsl                 |    7 +++++
 .../{glarea-vertex.glsl => glarea-gles.vs.glsl}    |    3 +-
 demos/gtk-demo/glarea.c                            |   26 ++++++++++++++++---
 6 files changed, 34 insertions(+), 8 deletions(-)
---
diff --git a/demos/gtk-demo/demo.gresource.xml b/demos/gtk-demo/demo.gresource.xml
index ea87863..654bcf0 100644
--- a/demos/gtk-demo/demo.gresource.xml
+++ b/demos/gtk-demo/demo.gresource.xml
@@ -199,8 +199,10 @@
     <file>popover.ui</file>
   </gresource>
   <gresource prefix="/glarea">
-    <file>glarea-fragment.glsl</file>
-    <file>glarea-vertex.glsl</file>
+    <file>glarea-gl.fs.glsl</file>
+    <file>glarea-gl.vs.glsl</file>
+    <file>glarea-gles.fs.glsl</file>
+    <file>glarea-gles.vs.glsl</file>
   </gresource>
   <gresource prefix="/font_features">
     <file>font-features.ui</file>
diff --git a/demos/gtk-demo/glarea-fragment.glsl b/demos/gtk-demo/glarea-gl.fs.glsl
similarity index 100%
rename from demos/gtk-demo/glarea-fragment.glsl
rename to demos/gtk-demo/glarea-gl.fs.glsl
diff --git a/demos/gtk-demo/glarea-vertex.glsl b/demos/gtk-demo/glarea-gl.vs.glsl
similarity index 100%
copy from demos/gtk-demo/glarea-vertex.glsl
copy to demos/gtk-demo/glarea-gl.vs.glsl
diff --git a/demos/gtk-demo/glarea-gles.fs.glsl b/demos/gtk-demo/glarea-gles.fs.glsl
new file mode 100644
index 0000000..39f7143
--- /dev/null
+++ b/demos/gtk-demo/glarea-gles.fs.glsl
@@ -0,0 +1,7 @@
+precision highp float;
+
+void main() {
+  float lerpVal = gl_FragCoord.y / 500.0f;
+
+  gl_FragColor = mix(vec4(1.0f, 0.85f, 0.35f, 1.0f), vec4(0.2f, 0.2f, 0.2f, 1.0f), lerpVal);
+}
diff --git a/demos/gtk-demo/glarea-vertex.glsl b/demos/gtk-demo/glarea-gles.vs.glsl
similarity index 56%
rename from demos/gtk-demo/glarea-vertex.glsl
rename to demos/gtk-demo/glarea-gles.vs.glsl
index 163f6ef..63064da 100644
--- a/demos/gtk-demo/glarea-vertex.glsl
+++ b/demos/gtk-demo/glarea-gles.vs.glsl
@@ -1,6 +1,5 @@
-#version 330
+attribute vec4 position;
 
-layout(location = 0) in vec4 position;
 uniform mat4 mvp;
 
 void main() {
diff --git a/demos/gtk-demo/glarea.c b/demos/gtk-demo/glarea.c
index 3aba729..715b067 100644
--- a/demos/gtk-demo/glarea.c
+++ b/demos/gtk-demo/glarea.c
@@ -93,7 +93,9 @@ create_shader (int         type,
 
 /* Initialize the shaders and link them into a program */
 static void
-init_shaders (GLuint *program_out,
+init_shaders (const char *vertex_path,
+              const char *fragment_path,
+              GLuint *program_out,
               GLuint *mvp_out)
 {
   GLuint vertex, fragment;
@@ -102,7 +104,7 @@ init_shaders (GLuint *program_out,
   int status;
   GBytes *source;
 
-  source = g_resources_lookup_data ("/glarea/glarea-vertex.glsl", 0, NULL);
+  source = g_resources_lookup_data (vertex_path, 0, NULL);
   vertex = create_shader (GL_VERTEX_SHADER, g_bytes_get_data (source, NULL));
   g_bytes_unref (source);
 
@@ -112,7 +114,7 @@ init_shaders (GLuint *program_out,
       return;
     }
 
-  source = g_resources_lookup_data ("/glarea/glarea-fragment.glsl", 0, NULL);
+  source = g_resources_lookup_data (fragment_path, 0, NULL);
   fragment = create_shader (GL_FRAGMENT_SHADER, g_bytes_get_data (source, NULL));
   g_bytes_unref (source);
 
@@ -218,13 +220,29 @@ static GLuint mvp_location;
 static void
 realize (GtkWidget *widget)
 {
+  const char *vertex_path, *fragment_path;
+  GdkGLContext *context;
+
   gtk_gl_area_make_current (GTK_GL_AREA (widget));
 
   if (gtk_gl_area_get_error (GTK_GL_AREA (widget)) != NULL)
     return;
 
+  context = gtk_gl_area_get_context (GTK_GL_AREA (widget));
+
+  if (gdk_gl_context_get_use_es (context))
+    {
+      vertex_path = "/glarea/glarea-gles.vs.glsl";
+      fragment_path = "/glarea/glarea-gles.fs.glsl";
+    }
+  else
+    {
+      vertex_path = "/glarea/glarea-gl.vs.glsl";
+      fragment_path = "/glarea/glarea-gl.fs.glsl";
+    }
+
   init_buffers (&position_buffer, NULL);
-  init_shaders (&program, &mvp_location);
+  init_shaders (vertex_path, fragment_path, &program, &mvp_location);
 }
 
 /* We should tear down the state when unrealizing */


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