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
This commit is contained in:
Jasper St. Pierre 2012-11-28 16:07:25 -05:00
parent 7c93639e59
commit b3fea016f1
5 changed files with 131 additions and 56 deletions

2
.gitignore vendored
View File

@ -85,6 +85,8 @@ src/test-theme
src/st.h src/st.h
src/stamp-st.h src/stamp-st.h
src/stamp-st.h.tmp src/stamp-st.h.tmp
src/st-scroll-view-fade-generated.c
src/stamp-st-scroll-view-fade-generated.c
stamp-h1 stamp-h1
tests/run-test.sh tests/run-test.sh
xmldocs.make xmldocs.make

View File

@ -87,6 +87,20 @@ stamp-st.h: Makefile
BUILT_SOURCES += st.h BUILT_SOURCES += st.h
CLEANFILES += stamp-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_source_private_h = \
st/st-private.h \ st/st-private.h \
st/st-table-private.h \ st/st-table-private.h \

39
src/data-to-c.pl Executable file
View File

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

View File

@ -37,61 +37,7 @@ typedef struct _StScrollViewFadeClass StScrollViewFadeClass;
#define DEFAULT_FADE_OFFSET 68.0f #define DEFAULT_FADE_OFFSET 68.0f
static const gchar *fade_glsl_shader = #include "st-scroll-view-fade-generated.c"
"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"
"}";
struct _StScrollViewFade struct _StScrollViewFade
{ {
@ -549,7 +495,7 @@ st_scroll_view_fade_init (StScrollViewFade *self)
if (clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL)) if (clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
{ {
shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT); 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); cogl_shader_compile (shader);
if (!cogl_shader_is_compiled (shader)) if (!cogl_shader_is_compiled (shader))
{ {

View File

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