Bug 75251 - glTexSubImage2D call with bad parameters
Summary: glTexSubImage2D call with bad parameters
Status: RESOLVED FIXED
Alias: None
Product: Wayland
Classification: Unclassified
Component: weston (show other bugs)
Version: 1.4.0
Hardware: All All
: medium major
Assignee: Wayland bug list
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-02-20 10:11 UTC by Stanislav Vorobiov
Modified: 2014-04-07 05:41 UTC (History)
0 users

See Also:
i915 platform:
i915 features:


Attachments

Description Stanislav Vorobiov 2014-02-20 10:11:42 UTC
In gl-renderer.c:gl_renderer_attach_shm:

glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
	     gs->pitch, buffer->height, 0,
	     GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);

i.e. texture with GL_BGRA internal format is created for surface.
Later in gl-renderer.c:gl_renderer_flush_damage:

switch (wl_shm_buffer_get_format(buffer->shm_buffer)) {
...
case WL_SHM_FORMAT_RGB565:
	format = GL_RGB;
	pixel_type = GL_UNSIGNED_SHORT_5_6_5;
	break;
...
}

glTexSubImage2D(GL_TEXTURE_2D, 0, r.x1, r.y1,
		r.x2 - r.x1, r.y2 - r.y1,
		format, pixel_type, data);

i.e. glTexSubImage2D is called with format = GL_RGB and type = GL_UNSIGNED_SHORT_5_6_5, while texture's internal format is GL_BGRA.
Such combination is incorrect and it works only because of OpenGL driver implementor's good will
Comment 1 Neil Roberts 2014-04-04 15:26:11 UTC
Thanks for the bug report. I've posted a patch to the mailing list which could fix this:

http://lists.freedesktop.org/archives/wayland-devel/2014-April/014066.html
Comment 2 Kristian Høgsberg 2014-04-07 05:41:18 UTC
Committed on master:

commit 39a443ff9b42ac3a2f7ec3f58230051f2031af24
Author: Neil Roberts <neil@linux.intel.com>
Date:   Fri Apr 4 16:24:54 2014 +0100

    Always use glTexImage2D instead of glTexSubImage2D for first upload
    
    Previously when uploading SHM data we would initialise the texture
    with glTexImage2D and NULL data when the buffer is attached. Then if
    the GL_EXT_unpack_subimage extension is available we would always use
    glTexSubImage2D to upload the data. The problem with that is that the
    first glTexImage2D was always setting the internal format to
    GL_BGRA_EXT and then if a 16-bit texture is used we would later call
    glTexSubImage2D with a data format of GL_RGBA. Under GLES2 the
    internal format must always match the data format so this is
    technically invalid.
    
    This patch makes it so that it always calls glTexImage2D when flushing
    the damage for the first time. That way it will use the right internal
    format and we don't need to call glTexImage2D with NULL data.
    
    https://bugs.freedesktop.org/show_bug.cgi?id=75251


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.