1
0
Fork 0

texture-2d-sliced: make CoglTexture2DSliced public

This make the CoglTexture2DSliced type public and adds
cogl_texture_2d_sliced_new_with_size() as an experimental API that can
be used to construct a sliced texture without any initial data.

Reviewed-by: Neil Roberts <neil@linux.intel.com>
This commit is contained in:
Robert Bragg 2011-10-08 11:53:20 +01:00
parent f6daed62e3
commit 6253b09c69
7 changed files with 251 additions and 143 deletions

View file

@ -77,6 +77,7 @@ cogl_public_h = \
$(srcdir)/cogl-texture.h \ $(srcdir)/cogl-texture.h \
$(srcdir)/cogl-texture-3d.h \ $(srcdir)/cogl-texture-3d.h \
$(srcdir)/cogl-texture-2d.h \ $(srcdir)/cogl-texture-2d.h \
$(srcdir)/cogl-texture-2d-sliced.h \
$(srcdir)/cogl-types.h \ $(srcdir)/cogl-types.h \
$(srcdir)/cogl-vertex-buffer.h \ $(srcdir)/cogl-vertex-buffer.h \
$(srcdir)/cogl-index-buffer.h \ $(srcdir)/cogl-index-buffer.h \

View file

@ -21,17 +21,15 @@
* *
*/ */
#ifndef __COGL_TEXTURE_2D_SLICED_H #ifndef __COGL_TEXTURE_2D_SLICED_PRIVATE_H
#define __COGL_TEXTURE_2D_SLICED_H #define __COGL_TEXTURE_2D_SLICED_PRIVATE_H
#include "cogl-bitmap-private.h" #include "cogl-bitmap-private.h"
#include "cogl-handle.h"
#include "cogl-pipeline-private.h" #include "cogl-pipeline-private.h"
#include "cogl-texture-private.h" #include "cogl-texture-private.h"
#include "cogl-texture-2d-sliced.h"
#define COGL_TEXTURE_2D_SLICED(tex) ((CoglTexture2DSliced *)tex) #include <glib.h>
typedef struct _CoglTexture2DSliced CoglTexture2DSliced;
struct _CoglTexture2DSliced struct _CoglTexture2DSliced
{ {
@ -48,13 +46,7 @@ struct _CoglTexture2DSliced
GQuark GQuark
_cogl_handle_texture_2d_sliced_get_type (void); _cogl_handle_texture_2d_sliced_get_type (void);
CoglHandle CoglTexture2DSliced *
_cogl_texture_2d_sliced_new_with_size (unsigned int width,
unsigned int height,
CoglTextureFlags flags,
CoglPixelFormat internal_format);
CoglHandle
_cogl_texture_2d_sliced_new_from_foreign (GLuint gl_handle, _cogl_texture_2d_sliced_new_from_foreign (GLuint gl_handle,
GLenum gl_target, GLenum gl_target,
GLuint width, GLuint width,
@ -63,9 +55,9 @@ _cogl_texture_2d_sliced_new_from_foreign (GLuint gl_handle,
GLuint y_pot_waste, GLuint y_pot_waste,
CoglPixelFormat format); CoglPixelFormat format);
CoglHandle CoglTexture2DSliced *
_cogl_texture_2d_sliced_new_from_bitmap (CoglBitmap *bmp, _cogl_texture_2d_sliced_new_from_bitmap (CoglBitmap *bmp,
CoglTextureFlags flags, CoglTextureFlags flags,
CoglPixelFormat internal_format); CoglPixelFormat internal_format);
#endif /* __COGL_TEXTURE_2D_SLICED_H */ #endif /* __COGL_TEXTURE_2D_SLICED_PRIVATE_H */

View file

@ -52,7 +52,7 @@
static void _cogl_texture_2d_sliced_free (CoglTexture2DSliced *tex_2ds); static void _cogl_texture_2d_sliced_free (CoglTexture2DSliced *tex_2ds);
COGL_TEXTURE_INTERNAL_DEFINE (Texture2DSliced, texture_2d_sliced); COGL_TEXTURE_DEFINE (Texture2DSliced, texture_2d_sliced);
static const CoglTextureVtable cogl_texture_2d_sliced_vtable; static const CoglTextureVtable cogl_texture_2d_sliced_vtable;
@ -179,7 +179,7 @@ _cogl_texture_2d_sliced_foreach_sub_texture_in_region (
float x_intersect_end = iter_x.intersect_end; float x_intersect_end = iter_x.intersect_end;
float slice_tx1; float slice_tx1;
float slice_tx2; float slice_tx2;
CoglHandle slice_tex; CoglTexture2D *slice_tex;
/* Discard slices out of rectangle early */ /* Discard slices out of rectangle early */
if (!iter_x.intersects) if (!iter_x.intersects)
@ -202,12 +202,12 @@ _cogl_texture_2d_sliced_foreach_sub_texture_in_region (
data.x_span = iter_x.span; data.x_span = iter_x.span;
/* Pluck out the cogl texture for this slice */ /* Pluck out the cogl texture for this slice */
slice_tex = g_array_index (tex_2ds->slice_textures, CoglHandle, slice_tex = g_array_index (tex_2ds->slice_textures, CoglTexture2D *,
iter_y.index * iter_x.array->len + iter_y.index * iter_x.array->len +
iter_x.index); iter_x.index);
_cogl_texture_foreach_sub_texture_in_region _cogl_texture_foreach_sub_texture_in_region
(slice_tex, (COGL_TEXTURE (slice_tex),
slice_tx1, slice_ty1, slice_tx2, slice_ty2, slice_tx1, slice_ty1, slice_tx2, slice_ty2,
_cogl_texture_2d_sliced_foreach_cb, _cogl_texture_2d_sliced_foreach_cb,
&data); &data);
@ -248,7 +248,7 @@ _cogl_texture_2d_sliced_allocate_waste_buffer (CoglTexture2DSliced *tex_2ds,
static void static void
_cogl_texture_2d_sliced_set_waste (CoglTexture2DSliced *tex_2ds, _cogl_texture_2d_sliced_set_waste (CoglTexture2DSliced *tex_2ds,
CoglBitmap *source_bmp, CoglBitmap *source_bmp,
CoglHandle slice_tex, CoglTexture2D *slice_tex,
guint8 *waste_buf, guint8 *waste_buf,
CoglSpan *x_span, CoglSpan *x_span,
CoglSpan *y_span, CoglSpan *y_span,
@ -318,7 +318,7 @@ _cogl_texture_2d_sliced_set_waste (CoglTexture2DSliced *tex_2ds,
NULL, NULL,
NULL); NULL);
cogl_texture_set_region_from_bitmap (slice_tex, cogl_texture_set_region_from_bitmap (COGL_TEXTURE (slice_tex),
0, /* src_x */ 0, /* src_x */
0, /* src_y */ 0, /* src_y */
/* dst_x */ /* dst_x */
@ -374,7 +374,7 @@ _cogl_texture_2d_sliced_set_waste (CoglTexture2DSliced *tex_2ds,
NULL, NULL,
NULL); NULL);
cogl_texture_set_region_from_bitmap (slice_tex, cogl_texture_set_region_from_bitmap (COGL_TEXTURE (slice_tex),
0, /* src_x */ 0, /* src_x */
0, /* src_y */ 0, /* src_y */
/* dst_x */ /* dst_x */
@ -399,7 +399,7 @@ _cogl_texture_2d_sliced_upload_to_gl (CoglTexture2DSliced *tex_2ds,
{ {
CoglSpan *x_span; CoglSpan *x_span;
CoglSpan *y_span; CoglSpan *y_span;
CoglHandle slice_tex; CoglTexture2D *slice_tex;
int x, y; int x, y;
guint8 *waste_buf; guint8 *waste_buf;
CoglPixelFormat bmp_format; CoglPixelFormat bmp_format;
@ -424,9 +424,9 @@ _cogl_texture_2d_sliced_upload_to_gl (CoglTexture2DSliced *tex_2ds,
/* Pick the gl texture object handle */ /* Pick the gl texture object handle */
slice_tex = g_array_index (tex_2ds->slice_textures, slice_tex = g_array_index (tex_2ds->slice_textures,
CoglHandle, slice_num); CoglTexture2D *, slice_num);
cogl_texture_set_region_from_bitmap (slice_tex, cogl_texture_set_region_from_bitmap (COGL_TEXTURE (slice_tex),
x_span->start, /* src x */ x_span->start, /* src x */
y_span->start, /* src y */ y_span->start, /* src y */
0, /* dst x */ 0, /* dst x */
@ -485,7 +485,7 @@ _cogl_texture_2d_sliced_upload_subregion_to_gl (CoglTexture2DSliced *tex_2ds,
CoglSpan *y_span; CoglSpan *y_span;
CoglSpanIter x_iter; CoglSpanIter x_iter;
CoglSpanIter y_iter; CoglSpanIter y_iter;
CoglHandle slice_tex; CoglTexture2D *slice_tex;
int source_x = 0, source_y = 0; int source_x = 0, source_y = 0;
int inter_w = 0, inter_h = 0; int inter_w = 0, inter_h = 0;
int local_x = 0, local_y = 0; int local_x = 0, local_y = 0;
@ -557,9 +557,9 @@ _cogl_texture_2d_sliced_upload_subregion_to_gl (CoglTexture2DSliced *tex_2ds,
/* Pick slice texture */ /* Pick slice texture */
slice_tex = g_array_index (tex_2ds->slice_textures, slice_tex = g_array_index (tex_2ds->slice_textures,
CoglHandle, slice_num); CoglTexture2D *, slice_num);
cogl_texture_set_region_from_bitmap (slice_tex, cogl_texture_set_region_from_bitmap (COGL_TEXTURE (slice_tex),
source_x, source_x,
source_y, source_y,
local_x, /* dst x */ local_x, /* dst x */
@ -692,11 +692,11 @@ _cogl_texture_2d_sliced_set_wrap_mode_parameters (CoglTexture *tex,
/* Pass the set wrap mode on to all of the child textures */ /* Pass the set wrap mode on to all of the child textures */
for (i = 0; i < tex_2ds->slice_textures->len; i++) for (i = 0; i < tex_2ds->slice_textures->len; i++)
{ {
CoglHandle slice_tex = g_array_index (tex_2ds->slice_textures, CoglTexture2D *slice_tex = g_array_index (tex_2ds->slice_textures,
CoglHandle, CoglTexture2D *,
i); i);
_cogl_texture_set_wrap_mode_parameters (slice_tex, _cogl_texture_set_wrap_mode_parameters (COGL_TEXTURE (slice_tex),
wrap_mode_s, wrap_mode_s,
wrap_mode_t, wrap_mode_t,
wrap_mode_p); wrap_mode_p);
@ -704,27 +704,25 @@ _cogl_texture_2d_sliced_set_wrap_mode_parameters (CoglTexture *tex,
} }
static gboolean static gboolean
_cogl_texture_2d_sliced_slices_create (CoglTexture2DSliced *tex_2ds, _cogl_texture_2d_sliced_slices_create (CoglContext *ctx,
CoglTexture2DSliced *tex_2ds,
int width, int height, int width, int height,
CoglPixelFormat format, CoglPixelFormat format)
CoglTextureFlags flags)
{ {
int max_width; int max_width;
int max_height; int max_height;
CoglHandle *slice_textures; CoglTexture2D **slice_textures;
int n_x_slices; int n_x_slices;
int n_y_slices; int n_y_slices;
int n_slices; int n_slices;
int x, y; int x, y;
CoglSpan *x_span; CoglSpan *x_span;
CoglSpan *y_span; CoglSpan *y_span;
GLenum gl_intformat; GLenum gl_intformat;
GLenum gl_type; GLenum gl_type;
int (*slices_for_size) (int, int, int, GArray*); int (*slices_for_size) (int, int, int, GArray*);
_COGL_GET_CONTEXT (ctx, FALSE);
/* Initialize size of largest slice according to supported features */ /* Initialize size of largest slice according to supported features */
if (cogl_features_available (COGL_FEATURE_TEXTURE_NPOT)) if (cogl_features_available (COGL_FEATURE_TEXTURE_NPOT))
{ {
@ -832,15 +830,12 @@ _cogl_texture_2d_sliced_slices_create (CoglTexture2DSliced *tex_2ds,
n_slices = n_x_slices * n_y_slices; n_slices = n_x_slices * n_y_slices;
tex_2ds->slice_textures = g_array_sized_new (FALSE, FALSE, tex_2ds->slice_textures = g_array_sized_new (FALSE, FALSE,
sizeof (CoglHandle), sizeof (CoglTexture2D *),
n_slices); n_slices);
g_array_set_size (tex_2ds->slice_textures, n_slices); g_array_set_size (tex_2ds->slice_textures, n_slices);
/* Generate a "working set" of GL texture objects slice_textures = (CoglTexture2D **) tex_2ds->slice_textures->data;
* (some implementations might supported faster
* re-binding between textures inside a set) */
slice_textures = (CoglHandle *) tex_2ds->slice_textures->data;
/* Init each GL texture object */ /* Init each GL texture object */
for (y = 0; y < n_y_slices; ++y) for (y = 0; y < n_y_slices; ++y)
@ -849,6 +844,7 @@ _cogl_texture_2d_sliced_slices_create (CoglTexture2DSliced *tex_2ds,
for (x = 0; x < n_x_slices; ++x) for (x = 0; x < n_x_slices; ++x)
{ {
GError *error = NULL;
x_span = &g_array_index (tex_2ds->slice_x_spans, CoglSpan, x); x_span = &g_array_index (tex_2ds->slice_x_spans, CoglSpan, x);
COGL_NOTE (SLICING, "CREATE SLICE (%d,%d)\tsize (%d,%d)", COGL_NOTE (SLICING, "CREATE SLICE (%d,%d)\tsize (%d,%d)",
@ -857,9 +853,14 @@ _cogl_texture_2d_sliced_slices_create (CoglTexture2DSliced *tex_2ds,
y_span->size - y_span->waste); y_span->size - y_span->waste);
slice_textures[y * n_x_slices + x] = slice_textures[y * n_x_slices + x] =
cogl_texture_new_with_size (x_span->size, y_span->size, cogl_texture_2d_new_with_size (ctx, x_span->size, y_span->size,
COGL_TEXTURE_NO_ATLAS | flags, format, &error);
format); if (!slice_textures[y * n_x_slices + x])
{
g_array_set_size (tex_2ds->slice_textures, y * n_x_slices + x);
g_error_free (error);
return FALSE;
}
} }
} }
@ -881,8 +882,8 @@ _cogl_texture_2d_sliced_slices_free (CoglTexture2DSliced *tex_2ds)
for (i = 0; i < tex_2ds->slice_textures->len; i++) for (i = 0; i < tex_2ds->slice_textures->len; i++)
{ {
CoglHandle slice_tex = CoglTexture2D *slice_tex =
g_array_index (tex_2ds->slice_textures, CoglHandle, i); g_array_index (tex_2ds->slice_textures, CoglTexture2D *, i);
cogl_handle_unref (slice_tex); cogl_handle_unref (slice_tex);
} }
@ -900,11 +901,11 @@ _cogl_texture_2d_sliced_free (CoglTexture2DSliced *tex_2ds)
} }
static gboolean static gboolean
_cogl_texture_2d_sliced_init_base (CoglTexture2DSliced *tex_2ds, _cogl_texture_2d_sliced_init_base (CoglContext *ctx,
CoglTexture2DSliced *tex_2ds,
int width, int width,
int height, int height,
CoglPixelFormat internal_format, CoglPixelFormat internal_format)
CoglTextureFlags flags)
{ {
CoglTexture *tex = COGL_TEXTURE (tex_2ds); CoglTexture *tex = COGL_TEXTURE (tex_2ds);
@ -915,11 +916,11 @@ _cogl_texture_2d_sliced_init_base (CoglTexture2DSliced *tex_2ds,
tex_2ds->slice_textures = NULL; tex_2ds->slice_textures = NULL;
/* Create slices for the given format and size */ /* Create slices for the given format and size */
if (!_cogl_texture_2d_sliced_slices_create (tex_2ds, if (!_cogl_texture_2d_sliced_slices_create (ctx,
tex_2ds,
width, width,
height, height,
internal_format, internal_format))
flags))
return FALSE; return FALSE;
tex_2ds->width = width; tex_2ds->width = width;
@ -928,11 +929,13 @@ _cogl_texture_2d_sliced_init_base (CoglTexture2DSliced *tex_2ds,
return TRUE; return TRUE;
} }
CoglHandle CoglTexture2DSliced *
_cogl_texture_2d_sliced_new_with_size (unsigned int width, cogl_texture_2d_sliced_new_with_size (CoglContext *ctx,
unsigned int height, unsigned int width,
CoglTextureFlags flags, unsigned int height,
CoglPixelFormat internal_format) int max_waste,
CoglPixelFormat internal_format,
GError **error)
{ {
CoglTexture2DSliced *tex_2ds; CoglTexture2DSliced *tex_2ds;
@ -943,24 +946,25 @@ _cogl_texture_2d_sliced_new_with_size (unsigned int width,
/* Init texture with empty bitmap */ /* Init texture with empty bitmap */
tex_2ds = g_new (CoglTexture2DSliced, 1); tex_2ds = g_new (CoglTexture2DSliced, 1);
if ((flags & COGL_TEXTURE_NO_SLICING)) tex_2ds->max_waste = max_waste;
tex_2ds->max_waste = -1;
else
tex_2ds->max_waste = COGL_TEXTURE_MAX_WASTE;
if (!_cogl_texture_2d_sliced_init_base (tex_2ds, if (!_cogl_texture_2d_sliced_init_base (ctx,
tex_2ds,
width, height, width, height,
internal_format, internal_format))
flags))
{ {
_cogl_texture_2d_sliced_free (tex_2ds); _cogl_texture_2d_sliced_free (tex_2ds);
return COGL_INVALID_HANDLE; g_set_error (error,
COGL_ERROR,
COGL_ERROR_NO_MEMORY,
"Not enough memory to allocate texture slices");
return NULL;
} }
return _cogl_texture_2d_sliced_handle_new (tex_2ds); return _cogl_texture_2d_sliced_handle_new (tex_2ds);
} }
CoglHandle CoglTexture2DSliced *
_cogl_texture_2d_sliced_new_from_bitmap (CoglBitmap *bmp, _cogl_texture_2d_sliced_new_from_bitmap (CoglBitmap *bmp,
CoglTextureFlags flags, CoglTextureFlags flags,
CoglPixelFormat internal_format) CoglPixelFormat internal_format)
@ -972,7 +976,9 @@ _cogl_texture_2d_sliced_new_from_bitmap (CoglBitmap *bmp,
GLenum gl_type; GLenum gl_type;
int width, height; int width, height;
g_return_val_if_fail (cogl_is_bitmap (bmp), COGL_INVALID_HANDLE); _COGL_GET_CONTEXT (ctx, NULL);
g_return_val_if_fail (cogl_is_bitmap (bmp), NULL);
width = _cogl_bitmap_get_width (bmp); width = _cogl_bitmap_get_width (bmp);
height = _cogl_bitmap_get_height (bmp); height = _cogl_bitmap_get_height (bmp);
@ -985,30 +991,22 @@ _cogl_texture_2d_sliced_new_from_bitmap (CoglBitmap *bmp,
else else
tex_2ds->max_waste = COGL_TEXTURE_MAX_WASTE; tex_2ds->max_waste = COGL_TEXTURE_MAX_WASTE;
/* FIXME: If upload fails we should set some kind of
* error flag but still return texture handle if the
* user decides to destroy another texture and upload
* this one instead (reloading from file is not needed
* in that case). As a rule then, everytime a valid
* CoglHandle is returned, it should also be destroyed
* with cogl_handle_unref at some point! */
dst_bmp = _cogl_texture_prepare_for_upload (bmp, dst_bmp = _cogl_texture_prepare_for_upload (bmp,
internal_format, internal_format,
&internal_format, &internal_format,
&gl_intformat, &gl_intformat,
&gl_format, &gl_format,
&gl_type); &gl_type);
if (dst_bmp == COGL_INVALID_HANDLE) if (dst_bmp == NULL)
{ {
_cogl_texture_2d_sliced_free (tex_2ds); _cogl_texture_2d_sliced_free (tex_2ds);
return COGL_INVALID_HANDLE; return NULL;
} }
if (!_cogl_texture_2d_sliced_init_base (tex_2ds, if (!_cogl_texture_2d_sliced_init_base (ctx,
tex_2ds,
width, height, width, height,
internal_format, internal_format))
flags))
goto error; goto error;
if (!_cogl_texture_2d_sliced_upload_to_gl (tex_2ds, if (!_cogl_texture_2d_sliced_upload_to_gl (tex_2ds,
@ -1022,10 +1020,10 @@ _cogl_texture_2d_sliced_new_from_bitmap (CoglBitmap *bmp,
error: error:
cogl_object_unref (dst_bmp); cogl_object_unref (dst_bmp);
_cogl_texture_2d_sliced_free (tex_2ds); _cogl_texture_2d_sliced_free (tex_2ds);
return COGL_INVALID_HANDLE; return NULL;
} }
CoglHandle CoglTexture2DSliced *
_cogl_texture_2d_sliced_new_from_foreign (GLuint gl_handle, _cogl_texture_2d_sliced_new_from_foreign (GLuint gl_handle,
GLenum gl_target, GLenum gl_target,
GLuint width, GLuint width,
@ -1044,9 +1042,9 @@ _cogl_texture_2d_sliced_new_from_foreign (GLuint gl_handle,
CoglTexture *tex; CoglTexture *tex;
CoglSpan x_span; CoglSpan x_span;
CoglSpan y_span; CoglSpan y_span;
CoglHandle tex_2d; CoglTexture2D *tex_2d;
_COGL_GET_CONTEXT (ctx, COGL_INVALID_HANDLE); _COGL_GET_CONTEXT (ctx, NULL);
/* This should only be called when the texture target is 2D. If a /* This should only be called when the texture target is 2D. If a
rectangle texture is used then _cogl_texture_new_from_foreign rectangle texture is used then _cogl_texture_new_from_foreign
@ -1059,7 +1057,7 @@ _cogl_texture_2d_sliced_new_from_foreign (GLuint gl_handle,
/* Validate pot waste */ /* Validate pot waste */
if (x_pot_waste < 0 || x_pot_waste >= width || if (x_pot_waste < 0 || x_pot_waste >= width ||
y_pot_waste < 0 || y_pot_waste >= height) y_pot_waste < 0 || y_pot_waste >= height)
return COGL_INVALID_HANDLE; return NULL;
tex_2d = cogl_texture_2d_new_from_foreign (ctx, tex_2d = cogl_texture_2d_new_from_foreign (ctx,
gl_target, gl_target,
@ -1069,12 +1067,12 @@ _cogl_texture_2d_sliced_new_from_foreign (GLuint gl_handle,
NULL); NULL);
if (!tex_2d) if (!tex_2d)
return COGL_INVALID_HANDLE; return NULL;
/* The texture 2d backend may use a different pixel format if it /* The texture 2d backend may use a different pixel format if it
queries the actual texture so we'll refetch the format it queries the actual texture so we'll refetch the format it
actually used */ actually used */
format = cogl_texture_get_format (tex_2d); format = cogl_texture_get_format (COGL_TEXTURE (tex_2d));
/* Create new texture */ /* Create new texture */
tex_2ds = g_new0 (CoglTexture2DSliced, 1); tex_2ds = g_new0 (CoglTexture2DSliced, 1);
@ -1097,7 +1095,7 @@ _cogl_texture_2d_sliced_new_from_foreign (GLuint gl_handle,
tex_2ds->slice_textures = tex_2ds->slice_textures =
g_array_sized_new (FALSE, FALSE, g_array_sized_new (FALSE, FALSE,
sizeof (CoglHandle), 1); sizeof (CoglTexture2D *), 1);
/* Store info for a single slice */ /* Store info for a single slice */
x_span.start = 0; x_span.start = 0;
@ -1119,15 +1117,15 @@ static gboolean
_cogl_texture_2d_sliced_is_foreign (CoglTexture *tex) _cogl_texture_2d_sliced_is_foreign (CoglTexture *tex)
{ {
CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex); CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex);
CoglHandle slice_tex; CoglTexture2D *slice_tex;
/* Make sure slices were created */ /* Make sure slices were created */
if (tex_2ds->slice_textures == NULL) if (tex_2ds->slice_textures == NULL)
return FALSE; return FALSE;
/* Pass the call on to the first slice */ /* Pass the call on to the first slice */
slice_tex = g_array_index (tex_2ds->slice_textures, CoglHandle, 0); slice_tex = g_array_index (tex_2ds->slice_textures, CoglTexture2D *, 0);
return _cogl_texture_is_foreign (slice_tex); return _cogl_texture_is_foreign (COGL_TEXTURE (slice_tex));
} }
static int static int
@ -1156,7 +1154,7 @@ static gboolean
_cogl_texture_2d_sliced_can_hardware_repeat (CoglTexture *tex) _cogl_texture_2d_sliced_can_hardware_repeat (CoglTexture *tex)
{ {
CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex); CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex);
CoglHandle slice_tex; CoglTexture2D *slice_tex;
CoglSpan *x_span; CoglSpan *x_span;
CoglSpan *y_span; CoglSpan *y_span;
@ -1171,8 +1169,8 @@ _cogl_texture_2d_sliced_can_hardware_repeat (CoglTexture *tex)
return FALSE; return FALSE;
/* Otherwise pass the query on to the single slice texture */ /* Otherwise pass the query on to the single slice texture */
slice_tex = g_array_index (tex_2ds->slice_textures, CoglHandle, 0); slice_tex = g_array_index (tex_2ds->slice_textures, CoglTexture2D *, 0);
return _cogl_texture_can_hardware_repeat (slice_tex); return _cogl_texture_can_hardware_repeat (COGL_TEXTURE (slice_tex));
} }
static void static void
@ -1183,7 +1181,7 @@ _cogl_texture_2d_sliced_transform_coords_to_gl (CoglTexture *tex,
CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex); CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex);
CoglSpan *x_span; CoglSpan *x_span;
CoglSpan *y_span; CoglSpan *y_span;
CoglHandle slice_tex; CoglTexture2D *slice_tex;
g_assert (!_cogl_texture_2d_sliced_is_sliced (tex)); g_assert (!_cogl_texture_2d_sliced_is_sliced (tex));
@ -1195,8 +1193,8 @@ _cogl_texture_2d_sliced_transform_coords_to_gl (CoglTexture *tex,
*t *= tex_2ds->height / (float)y_span->size; *t *= tex_2ds->height / (float)y_span->size;
/* Let the child texture further transform the coords */ /* Let the child texture further transform the coords */
slice_tex = g_array_index (tex_2ds->slice_textures, CoglHandle, 0); slice_tex = g_array_index (tex_2ds->slice_textures, CoglTexture2D *, 0);
_cogl_texture_transform_coords_to_gl (slice_tex, s, t); _cogl_texture_transform_coords_to_gl (COGL_TEXTURE (slice_tex), s, t);
} }
static CoglTransformResult static CoglTransformResult
@ -1234,7 +1232,7 @@ _cogl_texture_2d_sliced_get_gl_texture (CoglTexture *tex,
GLenum *out_gl_target) GLenum *out_gl_target)
{ {
CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex); CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex);
CoglHandle slice_tex; CoglTexture2D *slice_tex;
if (tex_2ds->slice_textures == NULL) if (tex_2ds->slice_textures == NULL)
return FALSE; return FALSE;
@ -1242,9 +1240,10 @@ _cogl_texture_2d_sliced_get_gl_texture (CoglTexture *tex,
if (tex_2ds->slice_textures->len < 1) if (tex_2ds->slice_textures->len < 1)
return FALSE; return FALSE;
slice_tex = g_array_index (tex_2ds->slice_textures, CoglHandle, 0); slice_tex = g_array_index (tex_2ds->slice_textures, CoglTexture2D *, 0);
return cogl_texture_get_gl_texture (slice_tex, out_gl_handle, out_gl_target); return cogl_texture_get_gl_texture (COGL_TEXTURE (slice_tex),
out_gl_handle, out_gl_target);
} }
static void static void
@ -1253,7 +1252,7 @@ _cogl_texture_2d_sliced_set_filters (CoglTexture *tex,
GLenum mag_filter) GLenum mag_filter)
{ {
CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex); CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex);
CoglHandle slice_tex; CoglTexture2D *slice_tex;
int i; int i;
/* Make sure slices were created */ /* Make sure slices were created */
@ -1265,8 +1264,9 @@ _cogl_texture_2d_sliced_set_filters (CoglTexture *tex,
GL */ GL */
for (i = 0; i < tex_2ds->slice_textures->len; i++) for (i = 0; i < tex_2ds->slice_textures->len; i++)
{ {
slice_tex = g_array_index (tex_2ds->slice_textures, CoglHandle, i); slice_tex = g_array_index (tex_2ds->slice_textures, CoglTexture2D *, i);
_cogl_texture_set_filters (slice_tex, min_filter, mag_filter); _cogl_texture_set_filters (COGL_TEXTURE (slice_tex),
min_filter, mag_filter);
} }
} }
@ -1284,9 +1284,9 @@ _cogl_texture_2d_sliced_pre_paint (CoglTexture *tex,
/* Pass the pre-paint on to every slice */ /* Pass the pre-paint on to every slice */
for (i = 0; i < tex_2ds->slice_textures->len; i++) for (i = 0; i < tex_2ds->slice_textures->len; i++)
{ {
CoglHandle slice_tex = g_array_index (tex_2ds->slice_textures, CoglTexture2D *slice_tex = g_array_index (tex_2ds->slice_textures,
CoglHandle, i); CoglTexture2D *, i);
_cogl_texture_pre_paint (slice_tex, flags); _cogl_texture_pre_paint (COGL_TEXTURE (slice_tex), flags);
} }
} }
@ -1303,9 +1303,9 @@ _cogl_texture_2d_sliced_ensure_non_quad_rendering (CoglTexture *tex)
/* Pass the call on to every slice */ /* Pass the call on to every slice */
for (i = 0; i < tex_2ds->slice_textures->len; i++) for (i = 0; i < tex_2ds->slice_textures->len; i++)
{ {
CoglHandle slice_tex = g_array_index (tex_2ds->slice_textures, CoglTexture2D *slice_tex = g_array_index (tex_2ds->slice_textures,
CoglHandle, i); CoglTexture2D *, i);
_cogl_texture_ensure_non_quad_rendering (slice_tex); _cogl_texture_ensure_non_quad_rendering (COGL_TEXTURE (slice_tex));
} }
} }
@ -1350,30 +1350,30 @@ static CoglPixelFormat
_cogl_texture_2d_sliced_get_format (CoglTexture *tex) _cogl_texture_2d_sliced_get_format (CoglTexture *tex)
{ {
CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex); CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex);
CoglHandle slice_tex; CoglTexture2D *slice_tex;
/* Make sure slices were created */ /* Make sure slices were created */
if (tex_2ds->slice_textures == NULL) if (tex_2ds->slice_textures == NULL)
return 0; return 0;
/* Pass the call on to the first slice */ /* Pass the call on to the first slice */
slice_tex = g_array_index (tex_2ds->slice_textures, CoglHandle, 0); slice_tex = g_array_index (tex_2ds->slice_textures, CoglTexture2D *, 0);
return cogl_texture_get_format (slice_tex); return cogl_texture_get_format (COGL_TEXTURE (slice_tex));
} }
static GLenum static GLenum
_cogl_texture_2d_sliced_get_gl_format (CoglTexture *tex) _cogl_texture_2d_sliced_get_gl_format (CoglTexture *tex)
{ {
CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex); CoglTexture2DSliced *tex_2ds = COGL_TEXTURE_2D_SLICED (tex);
CoglHandle slice_tex; CoglTexture2D *slice_tex;
/* Make sure slices were created */ /* Make sure slices were created */
if (tex_2ds->slice_textures == NULL) if (tex_2ds->slice_textures == NULL)
return 0; return 0;
/* Pass the call on to the first slice */ /* Pass the call on to the first slice */
slice_tex = g_array_index (tex_2ds->slice_textures, CoglHandle, 0); slice_tex = g_array_index (tex_2ds->slice_textures, CoglTexture2D *, 0);
return _cogl_texture_get_gl_format (slice_tex); return _cogl_texture_get_gl_format (COGL_TEXTURE (slice_tex));
} }
static int static int

View file

@ -0,0 +1,104 @@
/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2011 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:
* Robert Bragg <robert@linux.intel.com>
*/
#ifndef __COGL_TEXURE_2D_SLICED_H
#define __COGL_TEXURE_2D_SLICED_H
#include "cogl-context.h"
#include "cogl-types.h"
#include <glib.h>
/**
* SECTION:cogl-texture-2d-sliced
* @short_description: Functions for creating and manipulating 2D meta
* textures that may internally be comprised of
* multiple 2D textures with power-of-two sizes.
*
* These functions allow high-level meta textures (See the
* #CoglMetaTexture interface) to be allocated that may internally be
* comprised of multiple 2D texture "slices" with power-of-two sizes.
*
* This API can be useful when working with GPUs that don't have
* native support for non-power-of-two textures or if you want to load
* a texture that is larger than the GPUs maximum texture size limits.
*
* The algorithm for slicing works by first trying to map a virtual
* size to the next larger power-of-two size and then seeing how many
* wasted pixels that would result in. For example if you have a
* virtual texture that's 259 texels wide, the next pot size = 512 and
* the amount of waste would be 253 texels. If the amount of waste is
* above a max-waste threshold then we would next slice that texture
* into one that's 256 texels and then looking at how many more texels
* remain unallocated after that we choose the next power-of-two size.
* For the example of a 259 texel image that would mean having a 256
* texel wide texture, leaving 3 texels unallocated so we'd then
* create a 4 texel wide texture - now there is only one texel of
* waste. The algorithm continues to slice the right most textures
* until the amount of waste is less than or equal to a specfied
* max-waste threshold. The same logic for slicing from left to right
* is also applied from top to bottom.
*/
typedef struct _CoglTexture2DSliced CoglTexture2DSliced;
#define COGL_TEXTURE_2D_SLICED(X) ((CoglTexture2DSliced *)X)
#define cogl_texture_2d_sliced_new_with_size \
cogl_texture_2d_sliced_new_with_size_EXP
/**
* cogl_texture_2d_sliced_new_with_size:
* @ctx: A #CoglContext
* @width: The virtual width of your sliced texture.
* @height: The virtual height of your sliced texture.
* @max_waste: The threshold of how wide a strip of wasted texels
* are allowed in the non-power-of-two textures before
* they must be sliced to reduce the amount of waste.
* @internal_format: The format of the texture
* @error: A #GError for exceptions.
*
* Creates a #CoglTexture2DSliced that may internally be comprised of
* 1 or more #CoglTexture2D textures with power-of-two sizes.
* @max_waste is used as a threshold for recursively slicing the
* right-most or bottom-most slices into smaller power-of-two sizes
* until the wasted padding at the bottom and right of the
* power-of-two textures is less than specified.
*
* Returns: A newly allocated #CoglTexture2DSliced or if there was
* an error allocating any of the internal slices %NULL is
* returned and @error is updated.
*
* Since: 1.10
* Stability: unstable
*/
CoglTexture2DSliced *
cogl_texture_2d_sliced_new_with_size (CoglContext *ctx,
unsigned int width,
unsigned int height,
int max_waste,
CoglPixelFormat internal_format,
GError **error);
#endif /* __COGL_TEXURE_2D_SLICED_H */

View file

@ -405,10 +405,15 @@ cogl_texture_new_with_size (unsigned int width,
/* If it fails resort to sliced textures */ /* If it fails resort to sliced textures */
if (tex == NULL) if (tex == NULL)
tex = COGL_TEXTURE (_cogl_texture_2d_sliced_new_with_size (width, {
height, int max_waste = flags & COGL_TEXTURE_NO_SLICING ? -1 : COGL_TEXTURE_MAX_WASTE;
flags, tex = COGL_TEXTURE (cogl_texture_2d_sliced_new_with_size (ctx,
internal_format)); width,
height,
max_waste,
internal_format,
NULL));
}
return tex; return tex;
} }
@ -471,9 +476,10 @@ cogl_texture_new_from_bitmap (CoglBitmap *bitmap,
return tex; return tex;
/* Otherwise create a sliced texture */ /* Otherwise create a sliced texture */
return _cogl_texture_2d_sliced_new_from_bitmap (bitmap, return
flags, COGL_TEXTURE (_cogl_texture_2d_sliced_new_from_bitmap (bitmap,
internal_format); flags,
internal_format));
} }
CoglTexture * CoglTexture *
@ -538,13 +544,13 @@ cogl_texture_new_from_foreign (GLuint gl_handle,
#endif #endif
if (x_pot_waste != 0 || y_pot_waste != 0) if (x_pot_waste != 0 || y_pot_waste != 0)
return _cogl_texture_2d_sliced_new_from_foreign (gl_handle, return COGL_TEXTURE (_cogl_texture_2d_sliced_new_from_foreign (gl_handle,
gl_target, gl_target,
width, width,
height, height,
x_pot_waste, x_pot_waste,
y_pot_waste, y_pot_waste,
format); format));
else else
{ {
_COGL_GET_CONTEXT (ctx, COGL_INVALID_HANDLE); _COGL_GET_CONTEXT (ctx, COGL_INVALID_HANDLE);

View file

@ -471,6 +471,8 @@ cogl_blend_string_error_quark (void);
* CoglError: * CoglError:
* @COGL_ERROR_UNSUPPORTED: You tried to use a feature or * @COGL_ERROR_UNSUPPORTED: You tried to use a feature or
* configuration not currently available. * configuration not currently available.
* @COGL_ERROR_NO_MEMORY: You tried to allocate a resource
* such as a texture and there wasn't enough memory.
* *
* Error enumeration for Cogl * Error enumeration for Cogl
* *
@ -480,7 +482,7 @@ cogl_blend_string_error_quark (void);
* <itemizedlist> * <itemizedlist>
* <listitem><para>You've tried to use a feature that is not * <listitem><para>You've tried to use a feature that is not
* advertised by cogl_get_features(). This could happen if you create * advertised by cogl_get_features(). This could happen if you create
* a non-sliced texture with a non-power-of-two size when * a 2d texture with a non-power-of-two size when
* %COGL_FEATURE_TEXTURE_NPOT is not advertised.</para></listitem> * %COGL_FEATURE_TEXTURE_NPOT is not advertised.</para></listitem>
* <listitem><para>The GPU can not handle the configuration you have * <listitem><para>The GPU can not handle the configuration you have
* requested. An example might be if you try to use too many texture * requested. An example might be if you try to use too many texture
@ -493,9 +495,11 @@ cogl_blend_string_error_quark (void);
* this enum should also be considered experimental. * this enum should also be considered experimental.
* *
* Since: 1.4 * Since: 1.4
* Stability: unstable
*/ */
typedef enum { /*< prefix=COGL_ERROR >*/ typedef enum { /*< prefix=COGL_ERROR >*/
COGL_ERROR_UNSUPPORTED COGL_ERROR_UNSUPPORTED,
COGL_ERROR_NO_MEMORY
} CoglError; } CoglError;
GQuark GQuark

View file

@ -81,6 +81,7 @@ typedef struct _CoglFramebuffer CoglFramebuffer;
#include <cogl/cogl-quaternion.h> #include <cogl/cogl-quaternion.h>
#include <cogl/cogl-texture-2d.h> #include <cogl/cogl-texture-2d.h>
#include <cogl/cogl-texture-3d.h> #include <cogl/cogl-texture-3d.h>
#include <cogl/cogl-texture-2d-sliced.h>
#include <cogl/cogl-index-buffer.h> #include <cogl/cogl-index-buffer.h>
#include <cogl/cogl-attribute-buffer.h> #include <cogl/cogl-attribute-buffer.h>
#include <cogl/cogl-indices.h> #include <cogl/cogl-indices.h>