[gnome-shell] scroll-view-fade: Put the shader in another file



commit b3fea016f1b4d5d0a1840b8faa80ccf26f653213
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Nov 28 16:07:25 2012 -0500

    scroll-view-fade: Put the shader in another file
    
    This doesn't make any changes to the shader outside of adding
    a header; it simply makes changes in the future easier to edit.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=689249

 .gitignore                      |    2 +
 src/Makefile-st.am              |   14 +++++++
 src/data-to-c.pl                |   39 ++++++++++++++++++++
 src/st/st-scroll-view-fade.c    |   58 +-----------------------------
 src/st/st-scroll-view-fade.glsl |   74 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 131 insertions(+), 56 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 9eb9209..eee501a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -85,6 +85,8 @@ src/test-theme
 src/st.h
 src/stamp-st.h
 src/stamp-st.h.tmp
+src/st-scroll-view-fade-generated.c
+src/stamp-st-scroll-view-fade-generated.c
 stamp-h1
 tests/run-test.sh
 xmldocs.make
diff --git a/src/Makefile-st.am b/src/Makefile-st.am
index 0ae490a..cb96024 100644
--- a/src/Makefile-st.am
+++ b/src/Makefile-st.am
@@ -87,6 +87,20 @@ stamp-st.h: Makefile
 BUILT_SOURCES += st.h
 CLEANFILES += stamp-st.h
 
+st-scroll-view-fade-generated.c: stamp-st-scroll-view-fade-generated.c
+	@true
+stamp-st-scroll-view-fade-generated.c: $(srcdir)/st/st-scroll-view-fade.glsl $(srcdir)/data-to-c.pl
+	$(AM_V_GEN) $(srcdir)/data-to-c.pl $(srcdir)/st/st-scroll-view-fade.glsl st_scroll_view_fade_glsl > $  tmp && \
+	(cmp -s $  tmp st-scroll-view-fade-generated.c || cp $  tmp st-scroll-view-fade-generated.c) && \
+	rm -f $  tmp &&				\
+	echo timestamp > $(@F)
+
+BUILT_SOURCES += st-scroll-view-fade-generated.c
+EXTRA_DIST += 					\
+	st/st-scroll-view-fade.glsl		\
+	data-to-c.pl				\
+	$(NULL)
+
 st_source_private_h =				\
 	st/st-private.h				\
 	st/st-table-private.h			\
diff --git a/src/data-to-c.pl b/src/data-to-c.pl
new file mode 100755
index 0000000..20ba2fb
--- /dev/null
+++ b/src/data-to-c.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+
+# Copyright  2011 Red Hat, Inc
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the licence, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+#
+# Author: Kalev Lember <kalevlember gmail com>
+
+
+if (@ARGV != 2) {
+    die "Usage: data-to-c.pl <filename> <variable>\n";
+}
+
+$file = $ARGV[0];
+
+open (FILE, $file) || die "Cannot open $file: $!\n";
+
+printf ("const char %s[] = \"", $ARGV[1]);
+while (my $line = <FILE>) {
+    foreach my $c (split //, $line) {
+        printf ("\\x%02x", ord ($c));
+    }
+}
+print "\";\n";
+
+close (FILE);
diff --git a/src/st/st-scroll-view-fade.c b/src/st/st-scroll-view-fade.c
index 36d3640..5093e7d 100644
--- a/src/st/st-scroll-view-fade.c
+++ b/src/st/st-scroll-view-fade.c
@@ -37,61 +37,7 @@ typedef struct _StScrollViewFadeClass  StScrollViewFadeClass;
 
 #define DEFAULT_FADE_OFFSET 68.0f
 
-static const gchar *fade_glsl_shader =
-"uniform sampler2D tex;\n"
-"uniform float height;\n"
-"uniform float width;\n"
-"uniform float offset_bottom;\n"
-"uniform float offset_top;\n"
-"uniform float offset_right;\n"
-"uniform float offset_left;\n"
-/*
- * Used to pass the fade area to the shader
- *
- * [0][0] = x1
- * [0][1] = y1
- * [1][0] = x2
- * [1][1] = y2
- *
- */
-"uniform mat2 fade_area;\n"
-"\n"
-"void main ()\n"
-"{\n"
-" vec4 color = cogl_color_in * texture2D (tex, vec2 (cogl_tex_coord_in[0].xy));\n"
-" float y = height * cogl_tex_coord_in[0].y;\n"
-" float x = width * cogl_tex_coord_in[0].x;\n"
-" float ratio = 1.0;\n"
-" float fade_bottom_start = fade_area[1][1] - offset_bottom;\n"
-" float fade_right_start = fade_area[1][0] - offset_right;\n"
-" float ratio_top = y / offset_top;\n"
-" float ratio_bottom = (fade_area[1][1] - y)/(fade_area[1][1] - fade_bottom_start);\n"
-" float ratio_left = x / offset_left;\n"
-" float ratio_right = (fade_area[1][0] - x)/(fade_area[1][0] - fade_right_start);\n"
-" bool in_scroll_area = fade_area[0][0] <= x && fade_area[1][0] >= x;\n"
-" bool fade_top = y < offset_top && in_scroll_area && (y >= fade_area[0][1]);\n"
-" bool fade_bottom = y > fade_bottom_start && in_scroll_area && (y <= fade_area[1][1]);\n"
-" bool fade_left = x < offset_left && in_scroll_area && (x >= fade_area[0][0]);\n"
-" bool fade_right = x > fade_right_start && in_scroll_area && (x <= fade_area[1][0]);\n"
-"\n"
-" if (fade_top) {\n"
-"  ratio *= ratio_top;\n"
-" }\n"
-"\n"
-" if (fade_bottom) {\n"
-"  ratio *= ratio_bottom;\n"
-" }\n"
-"\n"
-" if (fade_left) {\n"
-"  ratio *= ratio_left;\n"
-" }\n"
-"\n"
-" if (fade_right) {\n"
-"  ratio *= ratio_right;\n"
-" }\n"
-"\n"
-"  cogl_color_out = color * ratio;\n"
-"}";
+#include "st-scroll-view-fade-generated.c"
 
 struct _StScrollViewFade
 {
@@ -549,7 +495,7 @@ st_scroll_view_fade_init (StScrollViewFade *self)
       if (clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
         {
           shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
-          cogl_shader_source (shader, fade_glsl_shader);
+          cogl_shader_source (shader, (const char *) st_scroll_view_fade_glsl);
           cogl_shader_compile (shader);
           if (!cogl_shader_is_compiled (shader))
             {
diff --git a/src/st/st-scroll-view-fade.glsl b/src/st/st-scroll-view-fade.glsl
new file mode 100644
index 0000000..2e40420
--- /dev/null
+++ b/src/st/st-scroll-view-fade.glsl
@@ -0,0 +1,74 @@
+/*
+ * st-scroll-view-fade.glsl: Edge fade effect for StScrollView
+ *
+ * Copyright 2010 Intel Corporation.
+ * Copyright 2011 Adel Gadllah
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+uniform sampler2D tex;
+uniform float height;
+uniform float width;
+uniform float offset_bottom;
+uniform float offset_top;
+uniform float offset_right;
+uniform float offset_left;
+
+/*
+ * Used to pass the fade area to the shader
+ *
+ * [0][0] = x1
+ * [0][1] = y1
+ * [1][0] = x2
+ * [1][1] = y2
+ *
+ */
+uniform mat2 fade_area;
+
+void main ()
+{
+    vec4 color = cogl_color_in * texture2D (tex, vec2 (cogl_tex_coord_in[0].xy));
+    float y = height * cogl_tex_coord_in[0].y;
+    float x = width * cogl_tex_coord_in[0].x;
+    float ratio = 1.0;
+    float fade_bottom_start = fade_area[1][1] - offset_bottom;
+    float fade_right_start = fade_area[1][0] - offset_right;
+    float ratio_top = y / offset_top;
+    float ratio_bottom = (fade_area[1][1] - y)/(fade_area[1][1] - fade_bottom_start);
+    float ratio_left = x / offset_left;
+    float ratio_right = (fade_area[1][0] - x)/(fade_area[1][0] - fade_right_start);
+    bool in_scroll_area = fade_area[0][0] <= x && fade_area[1][0] >= x;
+    bool fade_top = y < offset_top && in_scroll_area && (y >= fade_area[0][1]);
+    bool fade_bottom = y > fade_bottom_start && in_scroll_area && (y <= fade_area[1][1]);
+    bool fade_left = x < offset_left && in_scroll_area && (x >= fade_area[0][0]);
+    bool fade_right = x > fade_right_start && in_scroll_area && (x <= fade_area[1][0]);
+
+    if (fade_top) {
+        ratio *= ratio_top;
+    }
+
+    if (fade_bottom) {
+        ratio *= ratio_bottom;
+    }
+
+    if (fade_left) {
+        ratio *= ratio_left;
+    }
+
+    if (fade_right) {
+        ratio *= ratio_right;
+    }
+
+    cogl_color_out = color * ratio;
+}



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