mutter/cogl/driver/gles/cogl-texture-driver.c
Neil Roberts 5288f6d88d Add a Cogl texture 3D backend
This adds a publicly exposed experimental API for a 3D texture
backend. There is a feature flag which can be checked for whether 3D
textures are supported. Although we require OpenGL 1.2 which has 3D
textures in core, GLES only provides them through an extension so the
feature can be used to detect that.

The textures can be created with one of two new API functions :-

cogl_texture_3d_new_with_size

 and

cogl_texture_3d_new_from_data

There is also internally a new_from_bitmap function. new_from_data is
implemented in terms of this function.

The two constructors are effectively the only way to upload data to a
3D texture. It does not work to call glTexImage2D with the
GL_TEXTURE_3D target so the virtual for cogl_texture_set_region does
nothing. It would be possible to make cogl_texture_get_data do
something sensible like returning all of the images as a single long
image but this is not currently implemented and instead the virtual
just always fails. We may want to add API specific to the 3D texture
backend to get and set a sub region of the texture.

All of those three functions can throw a GError. This will happen if
the GPU does not support 3D textures or it does not support NPOTs and
an NPOT size is requested. It will also fail if the FBO extension is
not supported and the COGL_TEXTURE_NO_AUTO_MIPMAP flag is not
given. This could be avoided by copying the code for the
GL_GENERATE_MIPMAP TexParameter fallback, but in the interests of
keeping the code simple this is not yet done.

This adds a couple of functions to cogl-texture-driver for uploading
3D data and querying the 3D proxy
texture. prep_gl_for_pixels_upload_full now also takes sets the
GL_UNPACK_IMAGE_HEIGHT parameter so that 3D textures can have padding
between the images. Whenever 3D texture is uploading, both the height
of the images and the height of all of the data is specified (either
explicitly or implicilty from the CoglBitmap) so that the image height
can be deduced by dividing by the depth.
2010-07-13 14:28:52 +01:00

470 lines
15 KiB
C

/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2007,2008,2009 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
*
* Authors:
* Matthew Allum <mallum@openedhand.com>
* Neil Roberts <neil@linux.intel.com>
* Robert Bragg <robert@linux.intel.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cogl.h"
#include "cogl-internal.h"
#include "cogl-util.h"
#include "cogl-bitmap.h"
#include "cogl-bitmap-private.h"
#include "cogl-texture-private.h"
#include "cogl-material.h"
#include "cogl-material-private.h"
#include "cogl-context.h"
#include "cogl-handle.h"
#include "cogl-primitives.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "cogl-gles2-wrapper.h"
#define glTexImage3D ctx->drv.pf_glTexImage3D
#define glTexSubImage3D ctx->drv.pf_glTexSubImage3D
#ifndef GL_TEXTURE_3D
#define GL_TEXTURE_3D 0x806F
#endif
#ifndef GL_MAX_3D_TEXTURE_SIZE_OES
#define GL_MAX_3D_TEXTURE_SIZE_OES 0x8073
#endif
void
_cogl_texture_driver_gen (GLenum gl_target,
GLsizei n,
GLuint *textures)
{
unsigned int i;
GE (glGenTextures (n, textures));
for (i = 0; i < n; i++)
{
_cogl_bind_gl_texture_transient (gl_target, textures[i], FALSE);
switch (gl_target)
{
case GL_TEXTURE_2D:
case GL_TEXTURE_3D:
/* GL_TEXTURE_MAG_FILTER defaults to GL_LINEAR, no need to set it */
GE( glTexParameteri (gl_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR) );
break;
default:
g_assert_not_reached();
}
}
}
void
_cogl_texture_driver_prep_gl_for_pixels_upload (int pixels_rowstride,
int pixels_bpp)
{
_cogl_texture_prep_gl_alignment_for_pixels_upload (pixels_rowstride);
}
void
_cogl_texture_driver_prep_gl_for_pixels_download (int pixels_rowstride,
int pixels_bpp)
{
_cogl_texture_prep_gl_alignment_for_pixels_download (pixels_rowstride);
}
void
_cogl_texture_driver_upload_subregion_to_gl (GLenum gl_target,
GLuint gl_handle,
gboolean is_foreign,
int src_x,
int src_y,
int dst_x,
int dst_y,
int width,
int height,
CoglBitmap *source_bmp,
GLuint source_gl_format,
GLuint source_gl_type)
{
int bpp = _cogl_get_format_bpp (source_bmp->format);
CoglBitmap slice_bmp;
/* NB: GLES doesn't support the GL_UNPACK_ROW_LENGTH, GL_UNPACK_SKIP_PIXELS
* or GL_UNPACK_SKIP_ROWS pixel store options so we can't directly source a
* sub-region from source_bmp, we need to use a transient bitmap instead. */
/* FIXME: optimize by not copying to intermediate slice bitmap when source
* rowstride = bpp * width and the texture image is not sliced */
/* Setup temp bitmap for slice subregion */
slice_bmp.format = source_bmp->format;
slice_bmp.width = width;
slice_bmp.height = height;
slice_bmp.rowstride = bpp * slice_bmp.width;
slice_bmp.data = g_malloc (slice_bmp.rowstride * slice_bmp.height);
/* Setup gl alignment to match rowstride and top-left corner */
_cogl_texture_driver_prep_gl_for_pixels_upload (slice_bmp.rowstride,
bpp);
/* Copy subregion data */
_cogl_bitmap_copy_subregion (source_bmp,
&slice_bmp,
src_x,
src_y,
0, 0,
slice_bmp.width,
slice_bmp.height);
_cogl_bind_gl_texture_transient (gl_target, gl_handle, is_foreign);
GE( glTexSubImage2D (gl_target, 0,
dst_x, dst_y,
width, height,
source_gl_format,
source_gl_type,
slice_bmp.data) );
/* Free temp bitmap */
g_free (slice_bmp.data);
}
void
_cogl_texture_driver_upload_to_gl (GLenum gl_target,
GLuint gl_handle,
gboolean is_foreign,
CoglBitmap *source_bmp,
GLint internal_gl_format,
GLuint source_gl_format,
GLuint source_gl_type)
{
int bpp = _cogl_get_format_bpp (source_bmp->format);
CoglBitmap bmp = *source_bmp;
gboolean bmp_owner = FALSE;
/* If the rowstride can't be specified with just GL_ALIGNMENT alone
then we need to copy the bitmap because there is no GL_ROW_LENGTH */
if (source_bmp->rowstride / bpp != source_bmp->width)
{
bmp.rowstride = bpp * bmp.width;
bmp.data = g_malloc (bmp.rowstride * bmp.height);
bmp_owner = TRUE;
_cogl_bitmap_copy_subregion (source_bmp,
&bmp,
0, 0, 0, 0,
bmp.width,
bmp.height);
}
/* Setup gl alignment to match rowstride and top-left corner */
_cogl_texture_driver_prep_gl_for_pixels_upload (bmp.rowstride,
bpp);
_cogl_bind_gl_texture_transient (gl_target, gl_handle, is_foreign);
GE( glTexImage2D (gl_target, 0,
internal_gl_format,
bmp.width, bmp.height,
0,
source_gl_format,
source_gl_type,
bmp.data) );
if (bmp_owner)
g_free (bmp.data);
}
void
_cogl_texture_driver_upload_to_gl_3d (GLenum gl_target,
GLuint gl_handle,
gboolean is_foreign,
GLint height,
GLint depth,
CoglBitmap *source_bmp,
GLint internal_gl_format,
GLuint source_gl_format,
GLuint source_gl_type)
{
int bpp = _cogl_get_format_bpp (source_bmp->format);
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
_cogl_bind_gl_texture_transient (gl_target, gl_handle, is_foreign);
/* If the rowstride or image height can't be specified with just
GL_ALIGNMENT alone then we need to copy the bitmap because there
is no GL_ROW_LENGTH */
if (source_bmp->rowstride / bpp != source_bmp->width ||
height != source_bmp->height / depth)
{
int image_height = source_bmp->height / depth;
int i;
_cogl_texture_driver_prep_gl_for_pixels_upload (source_bmp->width * bpp,
bpp);
/* Initialize the texture with empty data and then upload each
image with a sub-region update */
GE( glTexImage3D (gl_target,
0, /* level */
internal_gl_format,
source_bmp->width,
height,
depth,
0,
source_gl_format,
source_gl_type,
NULL) );
for (i = 0; i < depth; i++)
{
CoglBitmap bmp = *source_bmp;
bmp.rowstride = bpp * bmp.width;
bmp.height = height;
bmp.data = g_malloc (bmp.rowstride * bmp.height);
_cogl_bitmap_copy_subregion (source_bmp,
&bmp,
0, image_height * i,
0, 0,
bmp.width,
bmp.height);
GE( glTexSubImage3D (gl_target,
0, /* level */
0, /* xoffset */
0, /* yoffset */
i, /* zoffset */
bmp.width, /* width */
height, /* height */
1, /* depth */
source_gl_format,
source_gl_type,
bmp.data) );
g_free (bmp.data);
}
}
else
{
_cogl_texture_driver_prep_gl_for_pixels_upload (source_bmp->rowstride,
bpp);
GE( glTexImage3D (gl_target,
0, /* level */
internal_gl_format,
source_bmp->width,
height,
depth,
0,
source_gl_format,
source_gl_type,
source_bmp->data) );
}
}
/* NB: GLES doesn't support glGetTexImage2D, so cogl-texture will instead
* fallback to a generic render + readpixels approach to downloading
* texture data. (See _cogl_texture_draw_and_read() ) */
gboolean
_cogl_texture_driver_gl_get_tex_image (GLenum gl_target,
GLenum dest_gl_format,
GLenum dest_gl_type,
guint8 *dest)
{
return FALSE;
}
gboolean
_cogl_texture_driver_size_supported_3d (GLenum gl_target,
GLenum gl_format,
GLenum gl_type,
int width,
int height,
int depth)
{
GLint max_size;
/* GLES doesn't support a proxy texture target so let's at least
check whether the size is greater than
GL_MAX_3D_TEXTURE_SIZE_OES */
GE( glGetIntegerv (GL_MAX_3D_TEXTURE_SIZE_OES, &max_size) );
return width <= max_size && height <= max_size && depth <= max_size;
}
gboolean
_cogl_texture_driver_size_supported (GLenum gl_target,
GLenum gl_format,
GLenum gl_type,
int width,
int height)
{
GLint max_size;
/* GLES doesn't support a proxy texture target so let's at least
check whether the size is greater than GL_MAX_TEXTURE_SIZE */
GE( glGetIntegerv (GL_MAX_TEXTURE_SIZE, &max_size) );
return width <= max_size && height <= max_size;
}
void
_cogl_texture_driver_try_setting_gl_border_color (
GLuint gl_target,
const GLfloat *transparent_color)
{
/* FAIL! */
}
gboolean
_cogl_pixel_format_from_gl_internal (GLenum gl_int_format,
CoglPixelFormat *out_format)
{
return TRUE;
}
CoglPixelFormat
_cogl_pixel_format_to_gl (CoglPixelFormat format,
GLenum *out_glintformat,
GLenum *out_glformat,
GLenum *out_gltype)
{
CoglPixelFormat required_format;
GLenum glintformat = 0;
GLenum glformat = 0;
GLenum gltype = 0;
/* FIXME: check YUV support */
required_format = format;
/* Find GL equivalents */
switch (format & COGL_UNPREMULT_MASK)
{
case COGL_PIXEL_FORMAT_A_8:
glintformat = GL_ALPHA;
glformat = GL_ALPHA;
gltype = GL_UNSIGNED_BYTE;
break;
case COGL_PIXEL_FORMAT_G_8:
glintformat = GL_LUMINANCE;
glformat = GL_LUMINANCE;
gltype = GL_UNSIGNED_BYTE;
break;
/* Just one 24-bit ordering supported */
case COGL_PIXEL_FORMAT_RGB_888:
case COGL_PIXEL_FORMAT_BGR_888:
glintformat = GL_RGB;
glformat = GL_RGB;
gltype = GL_UNSIGNED_BYTE;
required_format = COGL_PIXEL_FORMAT_RGB_888;
break;
/* Just one 32-bit ordering supported */
case COGL_PIXEL_FORMAT_RGBA_8888:
case COGL_PIXEL_FORMAT_BGRA_8888:
case COGL_PIXEL_FORMAT_ARGB_8888:
case COGL_PIXEL_FORMAT_ABGR_8888:
glintformat = GL_RGBA;
glformat = GL_RGBA;
gltype = GL_UNSIGNED_BYTE;
required_format = COGL_PIXEL_FORMAT_RGBA_8888;
required_format |= (format & COGL_PREMULT_BIT);
break;
/* The following three types of channel ordering
* are always defined using system word byte
* ordering (even according to GLES spec) */
case COGL_PIXEL_FORMAT_RGB_565:
glintformat = GL_RGB;
glformat = GL_RGB;
gltype = GL_UNSIGNED_SHORT_5_6_5;
break;
case COGL_PIXEL_FORMAT_RGBA_4444:
glintformat = GL_RGBA;
glformat = GL_RGBA;
gltype = GL_UNSIGNED_SHORT_4_4_4_4;
break;
case COGL_PIXEL_FORMAT_RGBA_5551:
glintformat = GL_RGBA;
glformat = GL_RGBA;
gltype = GL_UNSIGNED_SHORT_5_5_5_1;
break;
/* FIXME: check extensions for YUV support */
default:
break;
}
if (out_glintformat != NULL)
*out_glintformat = glintformat;
if (out_glformat != NULL)
*out_glformat = glformat;
if (out_gltype != NULL)
*out_gltype = gltype;
return required_format;
}
gboolean
_cogl_texture_driver_allows_foreign_gl_target (GLenum gl_target)
{
/* Allow 2-dimensional textures only */
if (gl_target != GL_TEXTURE_2D)
return FALSE;
return TRUE;
}
void
_cogl_texture_driver_gl_generate_mipmaps (GLenum gl_target)
{
GE( _cogl_wrap_glGenerateMipmap (gl_target) );
}
CoglPixelFormat
_cogl_texture_driver_find_best_gl_get_data_format (
CoglPixelFormat format,
GLenum *closest_gl_format,
GLenum *closest_gl_type)
{
/* Find closest format that's supported by GL
(Can't use _cogl_pixel_format_to_gl since available formats
when reading pixels on GLES are severely limited) */
*closest_gl_format = GL_RGBA;
*closest_gl_type = GL_UNSIGNED_BYTE;
return COGL_PIXEL_FORMAT_RGBA_8888;
}