From b3cb5a36c7230070419afc71704800b380549a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guido=20G=C3=BCnther?= Date: Tue, 20 Mar 2018 13:06:53 +0100 Subject: [PATCH 1/3] gles2: allow to specify texture target type Allow to set the texture target type when generating/binding the texture. This allows us to attach the texture type to the texture so we don't have to keep the logic elsewhere. --- include/render/gles2.h | 1 + render/gles2/texture.c | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/render/gles2.h b/include/render/gles2.h index e8124050..0a4fb49f 100644 --- a/include/render/gles2.h +++ b/include/render/gles2.h @@ -37,6 +37,7 @@ struct wlr_gles2_texture { GLuint tex_id; const struct pixel_format *pixel_format; EGLImageKHR image; + GLenum target; }; struct shaders { diff --git a/render/gles2/texture.c b/render/gles2/texture.c index c5f79ba8..b3035826 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -22,14 +22,16 @@ static struct pixel_format external_pixel_format = { .shader = &shaders.external }; -static void gles2_texture_ensure_texture(struct wlr_gles2_texture *texture) { +static void gles2_texture_ensure_texture(struct wlr_gles2_texture *texture, + GLenum target) { if (texture->tex_id) { return; } + texture->target = target; GL_CALL(glGenTextures(1, &texture->tex_id)); - GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id)); - GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); - GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); + GL_CALL(glBindTexture(target, texture->tex_id)); + GL_CALL(glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); + GL_CALL(glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); } static bool gles2_texture_upload_pixels(struct wlr_texture *_texture, @@ -47,7 +49,7 @@ static bool gles2_texture_upload_pixels(struct wlr_texture *_texture, texture->wlr_texture.format = format; texture->pixel_format = fmt; - gles2_texture_ensure_texture(texture); + gles2_texture_ensure_texture(texture, GL_TEXTURE_2D); GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id)); GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride)); GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, fmt->gl_format, width, height, 0, @@ -100,7 +102,7 @@ static bool gles2_texture_upload_shm(struct wlr_texture *_texture, texture->wlr_texture.format = format; texture->pixel_format = fmt; - gles2_texture_ensure_texture(texture); + gles2_texture_ensure_texture(texture, GL_TEXTURE_2D); GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id)); GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, pitch)); GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0)); @@ -185,9 +187,8 @@ static bool gles2_texture_upload_drm(struct wlr_texture *_tex, return false; } - gles2_texture_ensure_texture(tex); + gles2_texture_ensure_texture(tex, target); GL_CALL(glBindTexture(GL_TEXTURE_2D, tex->tex_id)); - EGLint attribs[] = { EGL_WAYLAND_PLANE_WL, 0, EGL_NONE }; if (tex->image) { @@ -220,7 +221,7 @@ static bool gles2_texture_upload_eglimage(struct wlr_texture *wlr_tex, tex->wlr_texture.width = width; tex->wlr_texture.height = height; - gles2_texture_ensure_texture(tex); + gles2_texture_ensure_texture(tex, GL_TEXTURE_2D); GL_CALL(glActiveTexture(GL_TEXTURE0)); GL_CALL(glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex->tex_id)); @@ -254,7 +255,7 @@ static bool gles2_texture_upload_dmabuf(struct wlr_texture *_tex, GLenum target = GL_TEXTURE_2D; const struct pixel_format *pf = gl_format_for_wl_format(WL_SHM_FORMAT_ARGB8888); - gles2_texture_ensure_texture(tex); + gles2_texture_ensure_texture(tex, target); GL_CALL(glBindTexture(target, tex->tex_id)); tex->image = wlr_egl_create_image_from_dmabuf(tex->egl, &dmabuf->attributes); GL_CALL(glActiveTexture(GL_TEXTURE0)); From 13e4ba4867c0b5ab2193728d6a4192007fe8276b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guido=20G=C3=BCnther?= Date: Tue, 20 Mar 2018 13:19:14 +0100 Subject: [PATCH 2/3] gles2_texture_bind: use texture's target type Hardcoding GL_TEXTURE_2D leads to rendering errors when using GL_TEXTURE_EXTERNAL_OES textures. --- render/gles2/texture.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/render/gles2/texture.c b/render/gles2/texture.c index b3035826..99f153b8 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -306,9 +306,10 @@ static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct static void gles2_texture_bind(struct wlr_texture *_texture) { struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; - GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id)); - GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); - GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); + + GL_CALL(glBindTexture(texture->target, texture->tex_id)); + GL_CALL(glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); + GL_CALL(glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); GL_CALL(glUseProgram(*texture->pixel_format->shader)); } From 2d01663b2559c6d6a121c18b9496248dce8ad1a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guido=20G=C3=BCnther?= Date: Mon, 19 Mar 2018 15:45:34 +0100 Subject: [PATCH 3/3] gles2: Honor alpha in fragment_src_external By using the same vertex shader and adding alpha to the fragment shader for external textures we can: - use alpha blending - have wlr_gles2_render_texture_with_matrix work with the GL_TEXTURE_EXTERNAL_OES. So far this failed since we passed in alpha which was unknown by fragment_src_external --- render/gles2/renderer.c | 2 +- render/gles2/shaders.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 55797805..f525869d 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -90,7 +90,7 @@ static void init_default_shaders() { goto error; } if (glEGLImageTargetTexture2DOES) { - if (!compile_program(quad_vertex_src, fragment_src_external, + if (!compile_program(vertex_src, fragment_src_external, &shaders.external)) { goto error; } diff --git a/render/gles2/shaders.c b/render/gles2/shaders.c index 2e9386d0..30e12e4c 100644 --- a/render/gles2/shaders.c +++ b/render/gles2/shaders.c @@ -80,8 +80,9 @@ const GLchar fragment_src_external[] = "precision mediump float;" "varying vec2 v_texcoord;" "uniform samplerExternalOES texture0;" +"uniform float alpha;" "" "void main() {" " vec4 col = texture2D(texture0, v_texcoord);" -" gl_FragColor = vec4(col.rgb, col.a);" +" gl_FragColor = vec4(col.rgb, col.a * alpha);" "}";