gl-renderer: Assume default GL texture unit

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 <loic.molinari@collabora.com>
This commit is contained in:
Loïc Molinari 2024-07-23 15:23:23 +02:00 committed by Daniel Stone
parent b755345a46
commit fb4cccc290
4 changed files with 14 additions and 9 deletions

View File

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

View File

@ -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,

View File

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

View File

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