[PATCH v4 07/10] Moved shader state into gles2-renderer.

John Kåre Alsaker john.kare.alsaker at gmail.com
Mon Oct 29 14:17:15 PDT 2012


---
 src/compositor.h     | 18 ----------
 src/gles2-renderer.c | 93 +++++++++++++++++++++++++++++++++-------------------
 2 files changed, 59 insertions(+), 52 deletions(-)

diff --git a/src/compositor.h b/src/compositor.h
index f3dc829..d8332fc 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -249,15 +249,6 @@ struct weston_seat {
 	struct input_method *input_method;
 };
 
-struct weston_shader {
-	GLuint program;
-	GLuint vertex_shader, fragment_shader;
-	GLint proj_uniform;
-	GLint tex_uniforms[3];
-	GLint alpha_uniform;
-	GLint color_uniform;
-};
-
 enum {
 	WESTON_COMPOSITOR_ACTIVE,
 	WESTON_COMPOSITOR_IDLE,		/* shell->unlock called on activity */
@@ -293,14 +284,6 @@ struct weston_renderer {
 struct weston_compositor {
 	struct wl_signal destroy_signal;
 
-	struct weston_shader texture_shader_rgba;
-	struct weston_shader texture_shader_rgbx;
-	struct weston_shader texture_shader_egl_external;
-	struct weston_shader texture_shader_y_uv;
-	struct weston_shader texture_shader_y_u_v;
-	struct weston_shader texture_shader_y_xuxv;
-	struct weston_shader solid_shader;
-	struct weston_shader *current_shader;
 	struct wl_display *wl_display;
 	struct weston_shell_interface shell_interface;
 
@@ -419,7 +402,6 @@ struct weston_surface {
 	int32_t pitch;
 	struct wl_list link;
 	struct wl_list layer_link;
-	struct weston_shader *shader;
 	float alpha;
 	struct weston_plane *plane;
 
diff --git a/src/gles2-renderer.c b/src/gles2-renderer.c
index fc9e06e..820921f 100644
--- a/src/gles2-renderer.c
+++ b/src/gles2-renderer.c
@@ -30,12 +30,22 @@
 
 #include "compositor.h"
 
+struct gles2_shader {
+	GLuint program;
+	GLuint vertex_shader, fragment_shader;
+	GLint proj_uniform;
+	GLint tex_uniforms[3];
+	GLint alpha_uniform;
+	GLint color_uniform;
+};
+
 struct gles2_output_state {
 	EGLSurface egl_surface;
 };
 
 struct gles2_surface_state {
 	GLfloat color[4];
+	struct gles2_shader *shader;
 };
 
 struct gles2_renderer {
@@ -50,6 +60,16 @@ struct gles2_renderer {
 		GLuint texture;
 		int32_t width, height;
 	} border;
+
+	struct gles2_shader texture_shader_rgba;
+	struct gles2_shader texture_shader_rgbx;
+	struct gles2_shader texture_shader_egl_external;
+	struct gles2_shader texture_shader_y_uv;
+	struct gles2_shader texture_shader_y_u_v;
+	struct gles2_shader texture_shader_y_xuxv;
+	struct gles2_shader invert_color_shader;
+	struct gles2_shader solid_shader;
+	struct gles2_shader *current_shader;
 };
 
 static inline struct gles2_output_state *
@@ -537,6 +557,7 @@ static void
 triangle_fan_debug(struct weston_surface *surface, int first, int count)
 {
 	struct weston_compositor *compositor = surface->compositor;
+	struct gles2_renderer *gr = get_renderer(compositor);
 	int i;
 	GLushort *buffer;
 	GLushort *index;
@@ -564,11 +585,11 @@ triangle_fan_debug(struct weston_surface *surface, int first, int count)
 		*index++ = first + i;
 	}
 
-	glUseProgram(compositor->solid_shader.program);
-	glUniform4fv(compositor->solid_shader.color_uniform, 1,
+	glUseProgram(gr->solid_shader.program);
+	glUniform4fv(gr->solid_shader.color_uniform, 1,
 			color[color_idx++ % ARRAY_LENGTH(color)]);
 	glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
-	glUseProgram(compositor->current_shader->program);
+	glUseProgram(gr->current_shader->program);
 	free(buffer);
 }
 
@@ -640,18 +661,18 @@ use_output(struct weston_output *output)
 }
 
 static void
-use_shader(struct weston_compositor *compositor,
-			     struct weston_shader *shader)
+use_shader(struct gles2_renderer *gr,
+			     struct gles2_shader *shader)
 {
-	if (compositor->current_shader == shader)
+	if (gr->current_shader == shader)
 		return;
 
 	glUseProgram(shader->program);
-	compositor->current_shader = shader;
+	gr->current_shader = shader;
 }
 
 static void
-shader_uniforms(struct weston_shader *shader,
+shader_uniforms(struct gles2_shader *shader,
 		       struct weston_surface *surface,
 		       struct weston_output *output)
 {
@@ -672,6 +693,8 @@ draw_surface(struct weston_surface *es, struct weston_output *output,
 	     pixman_region32_t *damage) /* in global coordinates */
 {
 	struct weston_compositor *ec = es->compositor;
+	struct gles2_renderer *gr = get_renderer(ec);
+	struct gles2_surface_state *gs = get_surface_state(es);
 	/* repaint bounding region in global coordinates: */
 	pixman_region32_t repaint;
 	/* non-opaque region in surface coordinates: */
@@ -694,12 +717,12 @@ draw_surface(struct weston_surface *es, struct weston_output *output,
 	glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
 
 	if (ec->fan_debug) {
-		use_shader(ec, &ec->solid_shader);
-		shader_uniforms(&ec->solid_shader, es, output);
+		use_shader(gr, &gr->solid_shader);
+		shader_uniforms(&gr->solid_shader, es, output);
 	}
 
-	use_shader(ec, es->shader);
-	shader_uniforms(es->shader, es, output);
+	use_shader(gr, gs->shader);
+	shader_uniforms(gs->shader, es, output);
 
 	if (es->transform.enabled || output->zoom.active)
 		filter = GL_LINEAR;
@@ -719,14 +742,14 @@ draw_surface(struct weston_surface *es, struct weston_output *output,
 	pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
 
 	if (pixman_region32_not_empty(&es->opaque)) {
-		if (es->shader == &ec->texture_shader_rgba) {
+		if (gs->shader == &gr->texture_shader_rgba) {
 			/* Special case for RGBA textures with possibly
 			 * bad data in alpha channel: use the shader
 			 * that forces texture alpha = 1.0.
 			 * Xwayland surfaces need this.
 			 */
-			use_shader(ec, &ec->texture_shader_rgbx);
-			shader_uniforms(&ec->texture_shader_rgbx, es, output);
+			use_shader(gr, &gr->texture_shader_rgbx);
+			shader_uniforms(&gr->texture_shader_rgbx, es, output);
 		}
 
 		if (es->alpha < 1.0)
@@ -738,7 +761,7 @@ draw_surface(struct weston_surface *es, struct weston_output *output,
 	}
 
 	if (pixman_region32_not_empty(&surface_blend)) {
-		use_shader(ec, es->shader);
+		use_shader(gr, gs->shader);
 		glEnable(GL_BLEND);
 		repaint_region(es, &repaint, &surface_blend);
 	}
@@ -842,12 +865,12 @@ draw_border(struct weston_output *output)
 {
 	struct weston_compositor *ec = output->compositor;
 	struct gles2_renderer *gr = get_renderer(ec);
-	struct weston_shader *shader = &ec->texture_shader_rgba;
+	struct gles2_shader *shader = &gr->texture_shader_rgba;
 	GLfloat *v;
 	int n;
 
 	glDisable(GL_BLEND);
-	use_shader(ec, shader);
+	use_shader(gr, shader);
 
 	glUniformMatrix4fv(shader->proj_uniform,
 			   1, GL_FALSE, output->matrix.d);
@@ -1028,6 +1051,7 @@ gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
 {
 	struct weston_compositor *ec = es->compositor;
 	struct gles2_renderer *gr = get_renderer(ec);
+	struct gles2_surface_state *gs = get_surface_state(es);
 	EGLint attribs[3], format;
 	int i, num_planes;
 
@@ -1052,9 +1076,9 @@ gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
 			     es->pitch, buffer->height, 0,
 			     GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
 		if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
-			es->shader = &ec->texture_shader_rgbx;
+			gs->shader = &gr->texture_shader_rgbx;
 		else
-			es->shader = &ec->texture_shader_rgba;
+			gs->shader = &gr->texture_shader_rgba;
 	} else if (ec->query_buffer(gr->egl_display, buffer,
 				    EGL_TEXTURE_FORMAT, &format)) {
 		for (i = 0; i < es->num_images; i++)
@@ -1066,24 +1090,24 @@ gles2_renderer_attach(struct weston_surface *es, struct wl_buffer *buffer)
 		case EGL_TEXTURE_RGBA:
 		default:
 			num_planes = 1;
-			es->shader = &ec->texture_shader_rgba;
+			gs->shader = &gr->texture_shader_rgba;
 			break;
 		case EGL_TEXTURE_EXTERNAL_WL:
 			num_planes = 1;
 			es->target = GL_TEXTURE_EXTERNAL_OES;
-			es->shader = &ec->texture_shader_egl_external;
+			gs->shader = &gr->texture_shader_egl_external;
 			break;
 		case EGL_TEXTURE_Y_UV_WL:
 			num_planes = 2;
-			es->shader = &ec->texture_shader_y_uv;
+			gs->shader = &gr->texture_shader_y_uv;
 			break;
 		case EGL_TEXTURE_Y_U_V_WL:
 			num_planes = 3;
-			es->shader = &ec->texture_shader_y_u_v;
+			gs->shader = &gr->texture_shader_y_u_v;
 			break;
 		case EGL_TEXTURE_Y_XUXV_WL:
 			num_planes = 2;
-			es->shader = &ec->texture_shader_y_xuxv;
+			gs->shader = &gr->texture_shader_y_xuxv;
 			break;
 		}
 
@@ -1119,13 +1143,14 @@ gles2_renderer_surface_set_color(struct weston_surface *surface,
 		 float red, float green, float blue, float alpha)
 {
 	struct gles2_surface_state *gs = get_surface_state(surface);
+	struct gles2_renderer *gr = get_renderer(surface->compositor);
 
 	gs->color[0] = red;
 	gs->color[1] = green;
 	gs->color[2] = blue;
 	gs->color[3] = alpha;
 
-	surface->shader = &surface->compositor->solid_shader;
+	gs->shader = &gr->solid_shader;
 }
 
 static int
@@ -1282,7 +1307,7 @@ compile_shader(GLenum type, const char *source)
 }
 
 static int
-shader_init(struct weston_shader *shader,
+shader_init(struct gles2_shader *shader,
 		   const char *vertex_source, const char *fragment_source)
 {
 	char msg[512];
@@ -1664,26 +1689,26 @@ gles2_renderer_setup(struct weston_compositor *ec, EGLSurface egl_surface)
 
 	glActiveTexture(GL_TEXTURE0);
 
-	if (shader_init(&ec->texture_shader_rgba,
+	if (shader_init(&gr->texture_shader_rgba,
 			     vertex_shader, texture_fragment_shader_rgba) < 0)
 		return -1;
-	if (shader_init(&ec->texture_shader_rgbx,
+	if (shader_init(&gr->texture_shader_rgbx,
 			     vertex_shader, texture_fragment_shader_rgbx) < 0)
 		return -1;
 	if (has_egl_image_external &&
-			shader_init(&ec->texture_shader_egl_external,
+			shader_init(&gr->texture_shader_egl_external,
 				vertex_shader, texture_fragment_shader_egl_external) < 0)
 		return -1;
-	if (shader_init(&ec->texture_shader_y_uv,
+	if (shader_init(&gr->texture_shader_y_uv,
 			       vertex_shader, texture_fragment_shader_y_uv) < 0)
 		return -1;
-	if (shader_init(&ec->texture_shader_y_u_v,
+	if (shader_init(&gr->texture_shader_y_u_v,
 			       vertex_shader, texture_fragment_shader_y_u_v) < 0)
 		return -1;
-	if (shader_init(&ec->texture_shader_y_xuxv,
+	if (shader_init(&gr->texture_shader_y_xuxv,
 			       vertex_shader, texture_fragment_shader_y_xuxv) < 0)
 		return -1;
-	if (shader_init(&ec->solid_shader,
+	if (shader_init(&gr->solid_shader,
 			     vertex_shader, solid_fragment_shader) < 0)
 		return -1;
 
-- 
1.7.12.4



More information about the wayland-devel mailing list