Use GL_ARB_texture_swizzle to emulate GL_ALPHA textures

The core profile of GL3 has removed support for component-alpha
textures. Previously the GL3 driver would just ignore this and try to
create them anyway. This would generate a GL error on Mesa.

To fix this the GL texture driver will now create a GL_RED texture
when GL_ALPHA textures are not supported natively. It will then set a
texture swizzle using the GL_ARB_texture_swizzle extension so that the
alpha component will be taken from the red component of the texture.
The swizzle is part of the texture object state so it only needs to be
set once when the texture is created.

The ‘gen’ virtual function of the texture driver has been changed to
also take the internal format as a parameter. The GL driver will now
set the swizzle as appropriate here.

The GL3 driver now reports an error if the texture swizzle extension
is not available because Cogl can't really work properly without out
it. The extension is part of GL 3.3 so it is quite likely that it has
wide support from drivers. Eventually we could get rid of this
requirement if we have our own GLSL front-end and we could generate
the swizzle ourselves.

When uploading or downloading texture data to or from a
component-alpha texture, we can no longer rely on GL to do the
conversion. The swizzle doesn't have any effect on the texture data
functions. In these cases Cogl will now force an intermediate buffer
to be used and it will manually do the conversion as it does for the
GLES drivers.

Reviewed-by: Robert Bragg <robert@linux.intel.com>

(cherry picked from commit 32bacf81ebaa3be21a8f26af07d8f6eed6607652)
This commit is contained in:
Neil Roberts 2012-11-19 17:28:52 +00:00 committed by Robert Bragg
parent 05ebbfdae7
commit ff11a2b207
10 changed files with 156 additions and 68 deletions

View File

@ -114,7 +114,9 @@ typedef enum
COGL_PRIVATE_FEATURE_BLEND_CONSTANT = 1L<<18, COGL_PRIVATE_FEATURE_BLEND_CONSTANT = 1L<<18,
COGL_PRIVATE_FEATURE_QUERY_FRAMEBUFFER_BITS = 1L<<19, COGL_PRIVATE_FEATURE_QUERY_FRAMEBUFFER_BITS = 1L<<19,
COGL_PRIVATE_FEATURE_BUILTIN_POINT_SIZE_UNIFORM = 1L<<20, COGL_PRIVATE_FEATURE_BUILTIN_POINT_SIZE_UNIFORM = 1L<<20,
COGL_PRIVATE_FEATURE_QUERY_TEXTURE_PARAMETERS = 1L<<21 COGL_PRIVATE_FEATURE_QUERY_TEXTURE_PARAMETERS = 1L<<21,
COGL_PRIVATE_FEATURE_ALPHA_TEXTURES = 1L<<22,
COGL_PRIVATE_FEATURE_TEXTURE_SWIZZLE = 1L<<23
} CoglPrivateFeatureFlags; } CoglPrivateFeatureFlags;
/* Sometimes when evaluating pipelines, either during comparisons or /* Sometimes when evaluating pipelines, either during comparisons or

View File

@ -234,7 +234,8 @@ cogl_texture_3d_new_with_size (CoglContext *ctx,
width, height, depth, width, height, depth,
internal_format); internal_format);
ctx->texture_driver->gen (ctx, GL_TEXTURE_3D, 1, &tex_3d->gl_texture); tex_3d->gl_texture =
ctx->texture_driver->gen (ctx, GL_TEXTURE_3D, internal_format);
_cogl_bind_gl_texture_transient (GL_TEXTURE_3D, _cogl_bind_gl_texture_transient (GL_TEXTURE_3D,
tex_3d->gl_texture, tex_3d->gl_texture,
FALSE); FALSE);
@ -308,7 +309,8 @@ cogl_texture_3d_new_from_bitmap (CoglBitmap *bmp,
_cogl_bitmap_unmap (dst_bmp); _cogl_bitmap_unmap (dst_bmp);
} }
ctx->texture_driver->gen (ctx, GL_TEXTURE_3D, 1, &tex_3d->gl_texture); tex_3d->gl_texture =
ctx->texture_driver->gen (ctx, GL_TEXTURE_3D, internal_format);
ctx->texture_driver->upload_to_gl_3d (ctx, ctx->texture_driver->upload_to_gl_3d (ctx,
GL_TEXTURE_3D, GL_TEXTURE_3D,

View File

@ -33,11 +33,10 @@ struct _CoglTextureDriver
* non-mipmap filters when creating textures. This is to save some memory as * non-mipmap filters when creating textures. This is to save some memory as
* the driver will not allocate room for the mipmap tree. * the driver will not allocate room for the mipmap tree.
*/ */
void GLuint
(* gen) (CoglContext *ctx, (* gen) (CoglContext *ctx,
GLenum gl_target, GLenum gl_target,
GLsizei n, CoglPixelFormat internal_format);
GLuint *textures);
/* /*
* This sets up the glPixelStore state for an upload to a destination with * This sets up the glPixelStore state for an upload to a destination with

View File

@ -219,10 +219,10 @@ cogl_texture_rectangle_new_with_size (CoglContext *ctx,
width, height, width, height,
internal_format); internal_format);
tex_rect->gl_texture =
ctx->texture_driver->gen (ctx, ctx->texture_driver->gen (ctx,
GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_RECTANGLE_ARB,
1, /* num textures */ internal_format);
&tex_rect->gl_texture);
_cogl_bind_gl_texture_transient (GL_TEXTURE_RECTANGLE_ARB, _cogl_bind_gl_texture_transient (GL_TEXTURE_RECTANGLE_ARB,
tex_rect->gl_texture, tex_rect->gl_texture,
tex_rect->is_foreign); tex_rect->is_foreign);
@ -274,10 +274,10 @@ cogl_texture_rectangle_new_from_bitmap (CoglBitmap *bmp,
cogl_bitmap_get_height (bmp), cogl_bitmap_get_height (bmp),
internal_format); internal_format);
tex_rect->gl_texture =
ctx->texture_driver->gen (ctx, ctx->texture_driver->gen (ctx,
GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_RECTANGLE_ARB,
1, /* num textures */ internal_format);
&tex_rect->gl_texture);
ctx->texture_driver->upload_to_gl (ctx, ctx->texture_driver->upload_to_gl (ctx,
GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_RECTANGLE_ARB,
tex_rect->gl_texture, tex_rect->gl_texture,

View File

@ -206,7 +206,15 @@ _cogl_texture_prepare_for_upload (CoglBitmap *src_bmp,
limited number of formats so we must convert using the Cogl limited number of formats so we must convert using the Cogl
bitmap code instead */ bitmap code instead */
if ((ctx->private_feature_flags & COGL_PRIVATE_FEATURE_FORMAT_CONVERSION)) /* If the driver doesn't natively support alpha textures then it
* won't work correctly to convert to/from component-alpha
* textures */
if ((ctx->private_feature_flags & COGL_PRIVATE_FEATURE_FORMAT_CONVERSION) &&
((ctx->private_feature_flags & COGL_PRIVATE_FEATURE_ALPHA_TEXTURES) ||
(src_format != COGL_PIXEL_FORMAT_A_8 &&
dst_format != COGL_PIXEL_FORMAT_A_8) ||
src_format == dst_format))
{ {
/* If the source format does not have the same premult flag as the /* If the source format does not have the same premult flag as the
dst format then we need to copy and convert it */ dst format then we need to copy and convert it */
@ -1202,6 +1210,34 @@ cogl_texture_get_data (CoglTexture *texture,
closest_format = ((closest_format & ~COGL_PREMULT_BIT) | closest_format = ((closest_format & ~COGL_PREMULT_BIT) |
(texture_format & COGL_PREMULT_BIT)); (texture_format & COGL_PREMULT_BIT));
/* If the application is requesting a conversion from a
* component-alpha texture and the driver doesn't support them
* natively then we can only read into an alpha-format buffer. In
* this case the driver will be faking the alpha textures with a
* red-component texture and it won't swizzle to the correct format
* while reading */
if ((ctx->private_feature_flags & COGL_PRIVATE_FEATURE_ALPHA_TEXTURES) == 0)
{
if (texture_format == COGL_PIXEL_FORMAT_A_8)
{
closest_format = COGL_PIXEL_FORMAT_A_8;
closest_gl_format = GL_RED;
closest_gl_type = GL_UNSIGNED_BYTE;
}
else if (format == COGL_PIXEL_FORMAT_A_8)
{
/* If we are converting to a component-alpha texture then we
* need to read all of the components to a temporary buffer
* because there is no way to get just the 4th component.
* Note: it doesn't matter whether the texture is
* pre-multiplied here because we're only going to look at
* the alpha component */
closest_format = COGL_PIXEL_FORMAT_RGBA_8888;
closest_gl_format = GL_RGBA;
closest_gl_type = GL_UNSIGNED_BYTE;
}
}
/* Is the requested format supported? */ /* Is the requested format supported? */
if (closest_format == format) if (closest_format == format)
/* Target user data directly */ /* Target user data directly */

View File

@ -108,7 +108,8 @@ _cogl_texture_2d_gl_new_with_size (CoglContext *ctx,
width, height, width, height,
internal_format); internal_format);
ctx->texture_driver->gen (ctx, GL_TEXTURE_2D, 1, &tex_2d->gl_texture); tex_2d->gl_texture =
ctx->texture_driver->gen (ctx, GL_TEXTURE_2D, internal_format);
_cogl_bind_gl_texture_transient (GL_TEXTURE_2D, _cogl_bind_gl_texture_transient (GL_TEXTURE_2D,
tex_2d->gl_texture, tex_2d->gl_texture,
tex_2d->is_foreign); tex_2d->is_foreign);
@ -164,7 +165,8 @@ _cogl_texture_2d_gl_new_from_bitmap (CoglBitmap *bmp,
_cogl_bitmap_unmap (dst_bmp); _cogl_bitmap_unmap (dst_bmp);
} }
ctx->texture_driver->gen (ctx, GL_TEXTURE_2D, 1, &tex_2d->gl_texture); tex_2d->gl_texture =
ctx->texture_driver->gen (ctx, GL_TEXTURE_2D, internal_format);
ctx->texture_driver->upload_to_gl (ctx, ctx->texture_driver->upload_to_gl (ctx,
GL_TEXTURE_2D, GL_TEXTURE_2D,
tex_2d->gl_texture, tex_2d->gl_texture,
@ -198,7 +200,8 @@ _cogl_egl_texture_2d_gl_new_from_image (CoglContext *ctx,
width, height, width, height,
format); format);
ctx->texture_driver->gen (ctx, GL_TEXTURE_2D, 1, &tex_2d->gl_texture); tex_2d->gl_texture =
ctx->texture_driver->gen (ctx, GL_TEXTURE_2D, format);
_cogl_bind_gl_texture_transient (GL_TEXTURE_2D, _cogl_bind_gl_texture_transient (GL_TEXTURE_2D,
tex_2d->gl_texture, tex_2d->gl_texture,
FALSE); FALSE);

View File

@ -54,6 +54,10 @@ _cogl_driver_pixel_format_from_gl_internal (CoglContext *context,
{ {
case GL_ALPHA: case GL_ALPHA4: case GL_ALPHA8: case GL_ALPHA: case GL_ALPHA4: case GL_ALPHA8:
case GL_ALPHA12: case GL_ALPHA16: case GL_ALPHA12: case GL_ALPHA16:
/* Cogl only supports one single-component texture so if we have
* ended up with a red texture then it is probably being used as
* a component-alpha texture */
case GL_RED:
*out_format = COGL_PIXEL_FORMAT_A_8; *out_format = COGL_PIXEL_FORMAT_A_8;
return TRUE; return TRUE;
@ -98,8 +102,20 @@ _cogl_driver_pixel_format_to_gl (CoglContext *context,
switch (format) switch (format)
{ {
case COGL_PIXEL_FORMAT_A_8: case COGL_PIXEL_FORMAT_A_8:
/* If the driver doesn't natively support alpha textures then we
* will use a red component texture with a swizzle to implement
* the texture */
if ((context->private_feature_flags &
COGL_PRIVATE_FEATURE_ALPHA_TEXTURES) == 0)
{
glintformat = GL_RED;
glformat = GL_RED;
}
else
{
glintformat = GL_ALPHA; glintformat = GL_ALPHA;
glformat = GL_ALPHA; glformat = GL_ALPHA;
}
gltype = GL_UNSIGNED_BYTE; gltype = GL_UNSIGNED_BYTE;
break; break;
case COGL_PIXEL_FORMAT_G_8: case COGL_PIXEL_FORMAT_G_8:
@ -564,11 +580,17 @@ _cogl_driver_update_features (CoglContext *ctx,
if (ctx->glGenSamplers) if (ctx->glGenSamplers)
private_flags |= COGL_PRIVATE_FEATURE_SAMPLER_OBJECTS; private_flags |= COGL_PRIVATE_FEATURE_SAMPLER_OBJECTS;
if (COGL_CHECK_GL_VERSION (gl_major, gl_minor, 3, 3) ||
_cogl_check_extension ("GL_ARB_texture_swizzle", gl_extensions) ||
_cogl_check_extension ("GL_EXT_texture_swizzle", gl_extensions))
private_flags |= COGL_PRIVATE_FEATURE_TEXTURE_SWIZZLE;
if (ctx->driver == COGL_DRIVER_GL) if (ctx->driver == COGL_DRIVER_GL)
/* Features which are not available in GL 3 */ /* Features which are not available in GL 3 */
private_flags |= (COGL_PRIVATE_FEATURE_FIXED_FUNCTION | private_flags |= (COGL_PRIVATE_FEATURE_FIXED_FUNCTION |
COGL_PRIVATE_FEATURE_ALPHA_TEST | COGL_PRIVATE_FEATURE_ALPHA_TEST |
COGL_PRIVATE_FEATURE_QUADS); COGL_PRIVATE_FEATURE_QUADS |
COGL_PRIVATE_FEATURE_ALPHA_TEXTURES);
private_flags |= (COGL_PRIVATE_FEATURE_READ_PIXELS_ANY_FORMAT | private_flags |= (COGL_PRIVATE_FEATURE_READ_PIXELS_ANY_FORMAT |
COGL_PRIVATE_FEATURE_ANY_GL | COGL_PRIVATE_FEATURE_ANY_GL |
@ -583,6 +605,17 @@ _cogl_driver_update_features (CoglContext *ctx,
g_strfreev (gl_extensions); g_strfreev (gl_extensions);
if ((private_flags & (COGL_PRIVATE_FEATURE_ALPHA_TEXTURES |
COGL_PRIVATE_FEATURE_TEXTURE_SWIZZLE)) == 0)
{
_cogl_set_error (error,
COGL_DRIVER_ERROR,
COGL_DRIVER_ERROR_NO_SUITABLE_DRIVER_FOUND,
"The GL_ARB_texture_swizzle extension is required "
"to use the GL3 driver");
return FALSE;
}
return TRUE; return TRUE;
} }

View File

@ -45,21 +45,20 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
static void #ifndef GL_TEXTURE_SWIZZLE_RGBA
#define GL_TEXTURE_SWIZZLE_RGBA 0x8E46
#endif
static GLuint
_cogl_texture_driver_gen (CoglContext *ctx, _cogl_texture_driver_gen (CoglContext *ctx,
GLenum gl_target, GLenum gl_target,
GLsizei n, CoglPixelFormat internal_format)
GLuint *textures)
{ {
unsigned int i; GLuint tex;
GE (ctx, glGenTextures (n, textures)); GE (ctx, glGenTextures (1, &tex));
for (i = 0; i < n; i++) _cogl_bind_gl_texture_transient (gl_target, tex, FALSE);
{
_cogl_bind_gl_texture_transient (gl_target,
textures[i],
FALSE);
switch (gl_target) switch (gl_target)
{ {
@ -79,7 +78,22 @@ _cogl_texture_driver_gen (CoglContext *ctx,
default: default:
g_assert_not_reached(); g_assert_not_reached();
} }
/* If the driver doesn't support alpha textures directly then we'll
* fake them by setting the swizzle parameters */
if (internal_format == COGL_PIXEL_FORMAT_A_8 &&
(ctx->private_feature_flags & (COGL_PRIVATE_FEATURE_ALPHA_TEXTURES |
COGL_PRIVATE_FEATURE_TEXTURE_SWIZZLE)) ==
COGL_PRIVATE_FEATURE_TEXTURE_SWIZZLE)
{
static const GLint red_swizzle[] = { GL_ZERO, GL_ZERO, GL_ZERO, GL_RED };
GE( ctx, glTexParameteriv (gl_target,
GL_TEXTURE_SWIZZLE_RGBA,
red_swizzle) );
} }
return tex;
} }
/* OpenGL - unlike GLES - can upload a sub region of pixel data from a larger /* OpenGL - unlike GLES - can upload a sub region of pixel data from a larger

View File

@ -271,7 +271,8 @@ _cogl_driver_update_features (CoglContext *context,
COGL_PRIVATE_FEATURE_BUILTIN_POINT_SIZE_UNIFORM); COGL_PRIVATE_FEATURE_BUILTIN_POINT_SIZE_UNIFORM);
private_flags |= (COGL_PRIVATE_FEATURE_VBOS | private_flags |= (COGL_PRIVATE_FEATURE_VBOS |
COGL_PRIVATE_FEATURE_ANY_GL); COGL_PRIVATE_FEATURE_ANY_GL |
COGL_PRIVATE_FEATURE_ALPHA_TEXTURES);
/* Both GLES 1.1 and GLES 2.0 support point sprites in core */ /* Both GLES 1.1 and GLES 2.0 support point sprites in core */
flags |= COGL_FEATURE_POINT_SPRITE; flags |= COGL_FEATURE_POINT_SPRITE;

View File

@ -64,19 +64,16 @@
#define GL_UNPACK_SKIP_PIXELS 0x0CF4 #define GL_UNPACK_SKIP_PIXELS 0x0CF4
#endif #endif
static void static GLuint
_cogl_texture_driver_gen (CoglContext *ctx, _cogl_texture_driver_gen (CoglContext *ctx,
GLenum gl_target, GLenum gl_target,
GLsizei n, CoglPixelFormat internal_format)
GLuint *textures)
{ {
unsigned int i; GLuint tex;
GE (ctx, glGenTextures (n, textures)); GE (ctx, glGenTextures (1, &tex));
for (i = 0; i < n; i++) _cogl_bind_gl_texture_transient (gl_target, tex, FALSE);
{
_cogl_bind_gl_texture_transient (gl_target, textures[i], FALSE);
switch (gl_target) switch (gl_target)
{ {
@ -91,7 +88,8 @@ _cogl_texture_driver_gen (CoglContext *ctx,
default: default:
g_assert_not_reached(); g_assert_not_reached();
} }
}
return tex;
} }
static void static void