[PATCH 2/4] compositor: factor out RGB shader.

Gwenole Beauchesne gb.devel at gmail.com
Fri Apr 20 08:17:28 PDT 2012


Simplify RGB shader code and split off common code that could be reused.
This is preparatory work for YUV shaders.

Signed-off-by: Gwenole Beauchesne <gwenole.beauchesne at intel.com>
---
 src/compositor-wayland.c |    2 +-
 src/compositor.c         |   46 +++++++++++++++++++++++++++++-----------------
 src/compositor.h         |    2 +-
 3 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/src/compositor-wayland.c b/src/compositor-wayland.c
index afd9121..d40b40b 100644
--- a/src/compositor-wayland.c
+++ b/src/compositor-wayland.c
@@ -171,7 +171,7 @@ draw_border(struct wayland_output *output)
 {
 	struct wayland_compositor *c =
 		(struct wayland_compositor *) output->base.compositor;
-	struct weston_shader *shader = &c->base.texture_shader;
+	struct weston_shader *shader = &c->base.texture_shader_rgba;
 	GLfloat *v;
 	int n;
 
diff --git a/src/compositor.c b/src/compositor.c
index 58cc894..53140a2 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -749,7 +749,7 @@ weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
 
 	if (wl_buffer_is_shm(buffer)) {
 		es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
-		es->shader = &ec->texture_shader;
+		es->shader = &ec->texture_shader_rgba;
 
 		ensure_textures(es, 1);
 		glBindTexture(GL_TEXTURE_2D, es->textures[0]);
@@ -768,7 +768,7 @@ weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
 		ec->image_target_texture_2d(GL_TEXTURE_2D, es->images[0]);
 
 		es->pitch = buffer->width;
-		es->shader = &ec->texture_shader;
+		es->shader = &ec->texture_shader_rgba;
 	}
 }
 
@@ -2219,25 +2219,37 @@ static const char vertex_shader[] =
 	"   v_texcoord = texcoord;\n"
 	"}\n";
 
-static const char texture_fragment_shader[] =
+/* Declare common fragment shader uniforms */
+#define FRAGMENT_SHADER_UNIFORMS		\
+	"uniform float alpha;\n"		\
+	"uniform float texwidth;\n"		\
+	"uniform float bright;\n"		\
+	"uniform float saturation;\n"
+
+/* Common fragment shader init code (check texture bounds) */
+#define FRAGMENT_SHADER_INIT						\
+	"   if (v_texcoord.x < 0.0 || v_texcoord.x > texwidth ||\n"	\
+	"       v_texcoord.y < 0.0 || v_texcoord.y > 1.0)\n"		\
+	"      discard;\n"
+
+/* Common fragment shader exit code (saturation, brightness, global alpha) */
+#define FRAGMENT_SHADER_EXIT						\
+	"   float gray = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114));\n" \
+	"   vec3 range = (gl_FragColor.rgb - vec3 (gray, gray, gray)) * saturation;\n" \
+	"   gl_FragColor = vec4(vec3(gray + range), gl_FragColor.a);\n" \
+	"   gl_FragColor = vec4(vec3(bright, bright, bright) * gl_FragColor.rgb, gl_FragColor.a);\n" \
+	"   gl_FragColor = alpha * gl_FragColor;\n"
+
+static const char texture_fragment_shader_rgba[] =
 	"precision mediump float;\n"
 	"varying vec2 v_texcoord;\n"
 	"uniform sampler2D tex;\n"
-	"uniform float alpha;\n"
-	"uniform float bright;\n"
-	"uniform float saturation;\n"
-	"uniform float texwidth;\n"
+	FRAGMENT_SHADER_UNIFORMS
 	"void main()\n"
 	"{\n"
-	"   if (v_texcoord.x < 0.0 || v_texcoord.x > texwidth ||\n"
-	"       v_texcoord.y < 0.0 || v_texcoord.y > 1.0)\n"
-	"      discard;\n"
+	FRAGMENT_SHADER_INIT
 	"   gl_FragColor = texture2D(tex, v_texcoord)\n;"
-	"   float gray = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114));\n"
-	"   vec3 range = (gl_FragColor.rgb - vec3 (gray, gray, gray)) * saturation;\n"
-	"   gl_FragColor = vec4(vec3(gray + range), gl_FragColor.a);\n"
-	"   gl_FragColor = vec4(vec3(bright, bright, bright) * gl_FragColor.rgb, gl_FragColor.a);\n"
-	"   gl_FragColor = alpha * gl_FragColor;\n"
+	FRAGMENT_SHADER_EXIT
 	"}\n";
 
 static const char solid_fragment_shader[] =
@@ -2502,8 +2514,8 @@ weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
 
 	glActiveTexture(GL_TEXTURE0);
 
-	if (weston_shader_init(&ec->texture_shader,
-			     vertex_shader, texture_fragment_shader) < 0)
+	if (weston_shader_init(&ec->texture_shader_rgba,
+			     vertex_shader, texture_fragment_shader_rgba) < 0)
 		return -1;
 	if (weston_shader_init(&ec->solid_shader,
 			     vertex_shader, solid_fragment_shader) < 0)
diff --git a/src/compositor.h b/src/compositor.h
index a5eea05..ac07a79 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -178,7 +178,7 @@ struct weston_compositor {
 	EGLContext context;
 	EGLConfig config;
 	GLuint fbo;
-	struct weston_shader texture_shader;
+	struct weston_shader texture_shader_rgba;
 	struct weston_shader solid_shader;
 	struct weston_shader *current_shader;
 	struct wl_display *wl_display;
-- 
1.7.5.4



More information about the wayland-devel mailing list