From 55537cccbd8129ed9fd3d00e8af07c05719516ac Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Fri, 22 Apr 2016 12:34:33 +0100 Subject: [PATCH] gdk: Move GLSL shaders into GResource Having the shaders inlined as C strings makes them harder to edit and maintain. --- gdk/Makefile.am | 46 +++++++- gdk/gdk.c | 4 + gdk/gdkgl.c | 112 +++++--------------- gdk/resources/glsl/gl2-texture-2d.fs.glsl | 9 ++ gdk/resources/glsl/gl2-texture-2d.vs.glsl | 13 +++ gdk/resources/glsl/gl2-texture-rect.fs.glsl | 9 ++ gdk/resources/glsl/gl2-texture-rect.vs.glsl | 13 +++ gdk/resources/glsl/gl3-texture-2d.fs.glsl | 11 ++ gdk/resources/glsl/gl3-texture-2d.vs.glsl | 13 +++ gdk/resources/glsl/gl3-texture-rect.fs.glsl | 9 ++ gdk/resources/glsl/gl3-texture-rect.vs.glsl | 13 +++ 11 files changed, 162 insertions(+), 90 deletions(-) create mode 100644 gdk/resources/glsl/gl2-texture-2d.fs.glsl create mode 100644 gdk/resources/glsl/gl2-texture-2d.vs.glsl create mode 100644 gdk/resources/glsl/gl2-texture-rect.fs.glsl create mode 100644 gdk/resources/glsl/gl2-texture-rect.vs.glsl create mode 100644 gdk/resources/glsl/gl3-texture-2d.fs.glsl create mode 100644 gdk/resources/glsl/gl3-texture-2d.vs.glsl create mode 100644 gdk/resources/glsl/gl3-texture-rect.fs.glsl create mode 100644 gdk/resources/glsl/gl3-texture-rect.vs.glsl diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 74e321f351..f33c8aec73 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -172,6 +172,8 @@ gdk_built_sources = \ gdkenumtypes.c \ gdkmarshalers.h \ gdkmarshalers.c \ + gdkresources.h \ + gdkresources.c \ gdkversionmacros.h # @@ -179,8 +181,8 @@ gdk_built_sources = \ # gdkincludedir = $(includedir)/gtk-3.0/gdk -gdkinclude_HEADERS = $(gdk_public_h_sources) gdkenumtypes.h gdkversionmacros.h -nodist_gdkinclude_HEADERS = gdkconfig.h +gdkinclude_HEADERS = $(gdk_public_h_sources) +nodist_gdkinclude_HEADERS = gdkconfig.h gdkenumtypes.h gdkversionmacros.h deprecatedincludedir = $(includedir)/gtk-3.0/gdk/deprecated deprecatedinclude_HEADERS = $(deprecated_h_sources) @@ -190,7 +192,9 @@ common_sources = \ $(gdk_c_sources) \ gdkenumtypes.c \ gdkmarshalers.c \ - gdkmarshalers.h + gdkmarshalers.h \ + gdkresources.h \ + gdkresources.c libgdk_3_la_SOURCES = $(common_sources) libgdk_3_la_CFLAGS = $(AM_CFLAGS) $(GDK_HIDDEN_VISIBILITY_CFLAGS) @@ -442,6 +446,42 @@ stamp-gc-h: $(top_builddir)/config.status $(AM_V_at) cd $(top_builddir) && $(SHELL) ./config.status gdk/gdkconfig.h \ && echo timestamp > gdk/$(@F) +# +# Resources +# +gdk.gresource.xml: Makefile.am + $(AM_V_GEN) echo "" > $@; \ + echo "" >> $@; \ + echo " " >> $@; \ + for f in $(srcdir)/resources/glsl/*.glsl; do \ + n=`basename $$f`; \ + echo " resources/glsl/$$n" >> $@; \ + done; \ + echo " " >> $@; \ + echo "" >> $@; + +glsl_sources = \ + resources/glsl/gl3-texture-2d.fs.glsl \ + resources/glsl/gl3-texture-2d.vs.glsl \ + resources/glsl/gl3-texture-rect.fs.glsl \ + resources/glsl/gl3-texture-rect.vs.glsl \ + resources/glsl/gl2-texture-2d.fs.glsl \ + resources/glsl/gl2-texture-2d.fs.glsl \ + resources/glsl/gl2-texture-rect.vs.glsl \ + resources/glsl/gl2-texture-rect.vs.glsl + +EXTRA_DIST += $(glsl_sources) +CLEANFILES += gdk.gresource.xml + +resource_files = $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(srcdir) --generate-dependencies $(builddir)/gdk.gresource.xml) + +gdkresources.h: gdk.gresource.xml + $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) $< --target=$@ \ + --sourcedir=$(srcdir) --c-name _gdk --generate-header --manual-register +gdkresources.c: gdk.gresource.xml $(resource_files) + $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) $< --target=$@ \ + --sourcedir=$(srcdir) --c-name _gdk --generate-source --manual-register + # ------------------- MSVC Build Items ---------------- MSVCPROJS = gdk-3 diff --git a/gdk/gdk.c b/gdk/gdk.c index c9c353669e..d57502a55f 100644 --- a/gdk/gdk.c +++ b/gdk/gdk.c @@ -30,6 +30,8 @@ #include "gdkinternals.h" #include "gdkintl.h" +#include "gdkresources.h" + #include "gdk-private.h" #ifndef HAVE_XCONVERTCASE @@ -274,6 +276,8 @@ gdk_pre_parse (void) gdk_initialized = TRUE; + _gdk_register_resource (); + /* We set the fallback program class here, rather than lazily in * gdk_get_program_class, since we don't want -name to override it. */ diff --git a/gdk/gdkgl.c b/gdk/gdkgl.c index e8ba770669..155e8cd344 100644 --- a/gdk/gdkgl.c +++ b/gdk/gdkgl.c @@ -86,17 +86,24 @@ create_shader (int type, static void make_program (GdkGLContextProgram *program, - const char *vertex_shader_code, - const char *fragment_shader_code) + const char *vertex_shader_path, + const char *fragment_shader_path) { guint vertex_shader, fragment_shader; + GBytes *source; int status; - vertex_shader = create_shader (GL_VERTEX_SHADER, vertex_shader_code); + source = g_resources_lookup_data (vertex_shader_path, 0, NULL); + g_assert (source != NULL); + vertex_shader = create_shader (GL_VERTEX_SHADER, g_bytes_get_data (source, NULL)); + g_bytes_unref (source); if (vertex_shader == 0) return; - fragment_shader = create_shader (GL_FRAGMENT_SHADER, fragment_shader_code); + source = g_resources_lookup_data (fragment_shader_path, 0, NULL); + g_assert (source != NULL); + fragment_shader = create_shader (GL_FRAGMENT_SHADER, g_bytes_get_data (source, NULL)); + g_bytes_unref (source); if (fragment_shader == 0) { glDeleteShader (vertex_shader); @@ -147,51 +154,16 @@ bind_vao (GdkGLContextPaintData *paint_data) static void use_texture_2d_program (GdkGLContextPaintData *paint_data) { - static const char *vertex_shader_code_150 = - "#version 150\n" - "uniform sampler2D map;" - "in vec2 position;\n" - "in vec2 uv;\n" - "out vec2 vUv;\n" - "void main() {\n" - " gl_Position = vec4(position, 0, 1);\n" - " vUv = uv;\n" - "}\n"; - static const char *fragment_shader_code_150 = - "#version 150\n" - "in vec2 vUv;\n" - "out vec4 vertexColor;\n" - "uniform sampler2D map;\n" - "void main() {\n" - " vertexColor = texture2D (map, vUv);\n" - "}\n"; - static const char *vertex_shader_code_130 = - "#version 130\n" - "uniform sampler2D map;" - "attribute vec2 position;\n" - "attribute vec2 uv;\n" - "varying vec2 vUv;\n" - "void main() {\n" - " gl_Position = vec4(position, 0, 1);\n" - " vUv = uv;\n" - "}\n"; - static const char *fragment_shader_code_130 = - "#version 130\n" - "varying vec2 vUv;\n" - "uniform sampler2D map;\n" - "void main() {\n" - " gl_FragColor = texture2D (map, vUv);\n" - "}\n"; + const char *vertex_shader_path = paint_data->is_legacy + ? "/org/gtk/libgdk/glsl/gl2-texture-2d.vs.glsl" + : "/org/gtk/libgdk/glsl/gl3-texture-2d.vs.glsl"; - const char *vertex_shader_code = paint_data->is_legacy - ? vertex_shader_code_130 - : vertex_shader_code_150; - const char *fragment_shader_code = paint_data->is_legacy - ? fragment_shader_code_130 - : fragment_shader_code_150; + const char *fragment_shader_path = paint_data->is_legacy + ? "/org/gtk/libgdk/glsl/gl2-texture-2d.fs.glsl" + : "/org/gtk/libgdk/glsl/gl3-texture-2d.fs.glsl"; if (paint_data->texture_2d_quad_program.program == 0) - make_program (&paint_data->texture_2d_quad_program, vertex_shader_code, fragment_shader_code); + make_program (&paint_data->texture_2d_quad_program, vertex_shader_path, fragment_shader_path); if (paint_data->current_program != &paint_data->texture_2d_quad_program) { @@ -203,50 +175,16 @@ use_texture_2d_program (GdkGLContextPaintData *paint_data) static void use_texture_rect_program (GdkGLContextPaintData *paint_data) { - static const char *vertex_shader_code_150 = - "#version 150\n" - "uniform sampler2DRect map;\n" - "attribute vec2 position;\n" - "attribute vec2 uv;\n" - "varying vec2 vUv;\n" - "void main() {\n" - " gl_Position = vec4(position, 0, 1);\n" - " vUv = uv;\n" - "}\n"; - static const char *fragment_shader_code_150 = - "#version 150\n" - "varying vec2 vUv;\n" - "uniform sampler2DRect map;\n" - "void main() {\n" - " gl_FragColor = texture2DRect (map, vUv);\n" - "}\n"; - static const char *vertex_shader_code_130 = - "#version 130\n" - "uniform sampler2DRect map;\n" - "attribute vec2 position;\n" - "attribute vec2 uv;\n" - "varying vec2 vUv;\n" - "void main() {\n" - " gl_Position = vec4(position, 0, 1);\n" - " vUv = uv;\n" - "}\n"; - static const char *fragment_shader_code_130 = - "#version 130\n" - "varying vec2 vUv;\n" - "uniform sampler2DRect map;\n" - "void main() {\n" - " gl_FragColor = texture2DRect (map, vUv);\n" - "}\n"; + const char *vertex_shader_path = paint_data->is_legacy + ? "/org/gtk/libgdk/glsl/gl2-texture-rect.vs.glsl" + : "/org/gtk/libgdk/glsl/gl3-texture-rect.vs.glsl"; - const char *vertex_shader_code = paint_data->is_legacy - ? vertex_shader_code_130 - : vertex_shader_code_150; - const char *fragment_shader_code = paint_data->is_legacy - ? fragment_shader_code_130 - : fragment_shader_code_150; + const char *fragment_shader_path = paint_data->is_legacy + ? "/org/gtk/libgdk/glsl/gl2-texture-rect.fs.glsl" + : "/org/gtk/libgdk/glsl/gl3-texture-rect.vs.glsl"; if (paint_data->texture_rect_quad_program.program == 0) - make_program (&paint_data->texture_rect_quad_program, vertex_shader_code, fragment_shader_code); + make_program (&paint_data->texture_rect_quad_program, vertex_shader_path, fragment_shader_path); if (paint_data->current_program != &paint_data->texture_rect_quad_program) { diff --git a/gdk/resources/glsl/gl2-texture-2d.fs.glsl b/gdk/resources/glsl/gl2-texture-2d.fs.glsl new file mode 100644 index 0000000000..d0bacfbdeb --- /dev/null +++ b/gdk/resources/glsl/gl2-texture-2d.fs.glsl @@ -0,0 +1,9 @@ +#version 130 + +varying vec2 vUv; + +uniform sampler2D map; + +void main() { + gl_FragColor = texture2D (map, vUv); +} diff --git a/gdk/resources/glsl/gl2-texture-2d.vs.glsl b/gdk/resources/glsl/gl2-texture-2d.vs.glsl new file mode 100644 index 0000000000..bf9fdbb108 --- /dev/null +++ b/gdk/resources/glsl/gl2-texture-2d.vs.glsl @@ -0,0 +1,13 @@ +#version 130 + +uniform sampler2D map; + +attribute vec2 position; +attribute vec2 uv; + +varying vec2 vUv; + +void main() { + gl_Position = vec4(position, 0, 1); + vUv = uv; +} diff --git a/gdk/resources/glsl/gl2-texture-rect.fs.glsl b/gdk/resources/glsl/gl2-texture-rect.fs.glsl new file mode 100644 index 0000000000..fe0b870599 --- /dev/null +++ b/gdk/resources/glsl/gl2-texture-rect.fs.glsl @@ -0,0 +1,9 @@ +#version 130 + +varying vec2 vUv; + +uniform sampler2DRect map; + +void main() { + gl_FragColor = texture2DRect (map, vUv); +} diff --git a/gdk/resources/glsl/gl2-texture-rect.vs.glsl b/gdk/resources/glsl/gl2-texture-rect.vs.glsl new file mode 100644 index 0000000000..834d31d973 --- /dev/null +++ b/gdk/resources/glsl/gl2-texture-rect.vs.glsl @@ -0,0 +1,13 @@ +#version 130 + +uniform sampler2DRect map; + +attribute vec2 position; +attribute vec2 uv; + +varying vec2 vUv; + +void main() { + gl_Position = vec4(position, 0, 1); + vUv = uv; +} diff --git a/gdk/resources/glsl/gl3-texture-2d.fs.glsl b/gdk/resources/glsl/gl3-texture-2d.fs.glsl new file mode 100644 index 0000000000..fe353a6bad --- /dev/null +++ b/gdk/resources/glsl/gl3-texture-2d.fs.glsl @@ -0,0 +1,11 @@ +#version 150 + +in vec2 vUv; + +out vec4 vertexColor; + +uniform sampler2D map; + +void main() { + vertexColor = texture2D (map, vUv); +} diff --git a/gdk/resources/glsl/gl3-texture-2d.vs.glsl b/gdk/resources/glsl/gl3-texture-2d.vs.glsl new file mode 100644 index 0000000000..2c61f05d87 --- /dev/null +++ b/gdk/resources/glsl/gl3-texture-2d.vs.glsl @@ -0,0 +1,13 @@ +#version 150 + +uniform sampler2D map; + +in vec2 position; +in vec2 uv; + +out vec2 vUv; + +void main() { + gl_Position = vec4(position, 0, 1); + vUv = uv; +} diff --git a/gdk/resources/glsl/gl3-texture-rect.fs.glsl b/gdk/resources/glsl/gl3-texture-rect.fs.glsl new file mode 100644 index 0000000000..bf6884e6c0 --- /dev/null +++ b/gdk/resources/glsl/gl3-texture-rect.fs.glsl @@ -0,0 +1,9 @@ +#version 150 + +varying vec2 vUv; + +uniform sampler2DRect map; + +void main() { + gl_FragColor = texture2DRect (map, vUv); +} diff --git a/gdk/resources/glsl/gl3-texture-rect.vs.glsl b/gdk/resources/glsl/gl3-texture-rect.vs.glsl new file mode 100644 index 0000000000..874ef6c214 --- /dev/null +++ b/gdk/resources/glsl/gl3-texture-rect.vs.glsl @@ -0,0 +1,13 @@ +#version 150 + +uniform sampler2DRect map; + +attribute vec2 position; +attribute vec2 uv; + +varying vec2 vUv; + +void main() { + gl_Position = vec4(position, 0, 1); + vUv = uv; +}