From fb4cccc2900dcb7dcd04a73137c9065f3120689e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Molinari?= Date: Tue, 23 Jul 2024 15:23:23 +0200 Subject: [PATCH] gl-renderer: Assume default GL texture unit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Revert active unit to default value right after use so that other functions can assume the default state. A best practices section is added to the internal header for reference. Signed-off-by: Loïc Molinari --- libweston/renderer-gl/gl-renderer-internal.h | 9 +++++++++ libweston/renderer-gl/gl-renderer.c | 10 +++------- .../gl-shader-config-color-transformation.c | 2 -- libweston/renderer-gl/gl-shaders.c | 2 ++ 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/libweston/renderer-gl/gl-renderer-internal.h b/libweston/renderer-gl/gl-renderer-internal.h index 495b99ca..330355fa 100644 --- a/libweston/renderer-gl/gl-renderer-internal.h +++ b/libweston/renderer-gl/gl-renderer-internal.h @@ -26,6 +26,15 @@ * SOFTWARE. */ +/* + * GL renderer best practices: + * + * 1. Texture units + * 1. Fixed allocation using the gl_tex_unit enumeration. + * 2. Any functions changing the active unit must restore it to 0 before + * return so that other functions can assume a default value. + */ + #ifndef GL_RENDERER_INTERNAL_H #define GL_RENDERER_INTERNAL_H diff --git a/libweston/renderer-gl/gl-renderer.c b/libweston/renderer-gl/gl-renderer.c index 34f83360..c6f9262a 100644 --- a/libweston/renderer-gl/gl-renderer.c +++ b/libweston/renderer-gl/gl-renderer.c @@ -574,7 +574,6 @@ gl_fbo_texture_init(struct gl_fbo_texture *fbotex, GLuint shadow_fbo; GLuint shadow_tex; - glActiveTexture(GL_TEXTURE0); glGenTextures(1, &shadow_tex); glBindTexture(GL_TEXTURE_2D, shadow_tex); glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, @@ -1834,6 +1833,8 @@ update_wireframe_tex(struct gl_renderer *gr, glTexImage2D(GL_TEXTURE_2D, i, GL_LUMINANCE, new_size, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, buffer); free(buffer); + + glActiveTexture(GL_TEXTURE0); } static void @@ -1986,7 +1987,6 @@ draw_output_borders(struct weston_output *output, weston_matrix_scale(&sconf.projection, 2.0 / fb->width, go->y_flip * 2.0 / fb->height, 1); - glActiveTexture(GL_TEXTURE0); glEnableVertexAttribArray(SHADER_ATTRIB_LOC_POSITION); glEnableVertexAttribArray(SHADER_ATTRIB_LOC_TEXCOORD); @@ -2547,8 +2547,6 @@ gl_renderer_flush_damage(struct weston_paint_node *pnode) data = wl_shm_buffer_get_data(buffer->shm_buffer); - glActiveTexture(GL_TEXTURE0); - if (gb->needs_full_upload || quirks->gl_force_full_upload) { wl_shm_buffer_begin_access(buffer->shm_buffer); @@ -2650,8 +2648,6 @@ ensure_textures(struct gl_buffer_state *gb, GLenum target, int num_textures) assert(gb->num_textures == 0); - glActiveTexture(GL_TEXTURE0); - for (i = 0; i < num_textures; i++) { glGenTextures(1, &gb->textures[i]); glBindTexture(target, gb->textures[i]); @@ -3458,6 +3454,7 @@ gl_renderer_attach_buffer(struct weston_surface *surface, glBindTexture(target, gb->textures[i]); gr->image_target_texture_2d(target, gb->images[i]); } + glActiveTexture(GL_TEXTURE0); } static const struct weston_drm_format_array * @@ -3710,7 +3707,6 @@ gl_renderer_surface_copy_content(struct weston_surface *surface, gl_shader_config_set_input_textures(&sconf, gs); - glActiveTexture(GL_TEXTURE0); glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cw, ch, diff --git a/libweston/renderer-gl/gl-shader-config-color-transformation.c b/libweston/renderer-gl/gl-shader-config-color-transformation.c index 127a0f5a..244276f6 100644 --- a/libweston/renderer-gl/gl-shader-config-color-transformation.c +++ b/libweston/renderer-gl/gl-shader-config-color-transformation.c @@ -201,7 +201,6 @@ gl_color_curve_lut_3x1d(struct gl_renderer *gr, curve->u.lut_3x1d.fill_in(xform, lut, lut_len); - glActiveTexture(GL_TEXTURE0); glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); @@ -238,7 +237,6 @@ gl_3d_lut(struct gl_renderer *gr, xform->mapping.u.lut3d.fill_in(xform, lut, dim_size); - glActiveTexture(GL_TEXTURE0); glGenTextures(1, &tex3d); glBindTexture(GL_TEXTURE_3D, tex3d); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); diff --git a/libweston/renderer-gl/gl-shaders.c b/libweston/renderer-gl/gl-shaders.c index b49b4eaa..007e107e 100644 --- a/libweston/renderer-gl/gl-shaders.c +++ b/libweston/renderer-gl/gl-shaders.c @@ -767,6 +767,8 @@ gl_shader_load_config(struct gl_shader *shader, if (sconf->req.wireframe) glUniform1i(shader->tex_uniform_wireframe, TEX_UNIT_WIREFRAME); + + glActiveTexture(GL_TEXTURE0); } bool