From de8a6314f12854e748de515afb487af56eaac845 Mon Sep 17 00:00:00 2001 From: Damien Lespiau Date: Mon, 25 Jan 2010 11:21:05 +0000 Subject: [PATCH] cogl: new textures sould have GL_TEXTURE_MIN_FILTER set to GL_LINEAR The only way the user has to set the mipmap filters is through the material/layer API. This API defaults to GL_LINEAR/GL_LINEAR for the max and min filters. With the main use case of cogl being 2D interfaces, it makes sense do default to GL_LINEAR for the min filter. When creating new textures, we did not set any filter on them, using OpenGL defaults': GL_NEAREST_MIPMAP_LINEAR for the min filter and GL_LINEAR for the max filter. This will make the driver allocate memory for the mipmap tree, memory that will not be used in the nominal case (as the material API defaults to GL_LINEAR). This patch tries to ensure that the min filter is set to GL_LINEAR before any glTexImage*() call is done on the texture by setting the filter when generating new OpenGL handles. --- clutter/cogl/cogl/cogl-texture-2d-sliced.c | 9 ++++---- clutter/cogl/cogl/cogl-texture-2d.c | 9 ++++---- clutter/cogl/cogl/cogl-texture-driver.h | 9 ++++++++ clutter/cogl/cogl/driver/gl/cogl-texture-driver.c | 27 ++++++++++++++++++++++ .../cogl/cogl/driver/gles/cogl-texture-driver.c | 27 ++++++++++++++++++++++ 5 files changed, 72 insertions(+), 9 deletions(-) diff --git a/clutter/cogl/cogl/cogl-texture-2d-sliced.c b/clutter/cogl/cogl/cogl-texture-2d-sliced.c index 0cfef1f..b7a7bd7 100644 --- a/clutter/cogl/cogl/cogl-texture-2d-sliced.c +++ b/clutter/cogl/cogl/cogl-texture-2d-sliced.c @@ -829,8 +829,7 @@ _cogl_texture_2d_sliced_slices_create (CoglTexture2DSliced *tex_2ds, * re-binding between textures inside a set) */ gl_handles = (GLuint*) tex_2ds->slice_gl_handles->data; - GE( glGenTextures (n_slices, gl_handles) ); - + _cogl_texture_driver_gen (GL_TEXTURE_2D, n_slices, gl_handles); /* Init each GL texture object */ for (y = 0; y < n_y_slices; ++y) @@ -917,9 +916,9 @@ _cogl_texture_2d_sliced_upload_from_data tex_2ds->slice_y_spans = NULL; tex_2ds->slice_gl_handles = NULL; - /* Unknown filter */ - tex_2ds->min_filter = GL_FALSE; - tex_2ds->mag_filter = GL_FALSE; + /* We default to GL_LINEAR for both filters */ + tex_2ds->min_filter = GL_LINEAR; + tex_2ds->mag_filter = GL_LINEAR; if (bmp->data) { diff --git a/clutter/cogl/cogl/cogl-texture-2d.c b/clutter/cogl/cogl/cogl-texture-2d.c index 8d32e31..b700eb1 100644 --- a/clutter/cogl/cogl/cogl-texture-2d.c +++ b/clutter/cogl/cogl/cogl-texture-2d.c @@ -225,9 +225,9 @@ _cogl_texture_2d_create_base (unsigned int width, tex_2d->mipmaps_dirty = TRUE; tex_2d->auto_mipmap = (flags & COGL_TEXTURE_NO_AUTO_MIPMAP) == 0; - /* Unknown filter */ - tex_2d->min_filter = GL_FALSE; - tex_2d->mag_filter = GL_FALSE; + /* We default to GL_LINEAR for both filters */ + tex_2d->min_filter = GL_LINEAR; + tex_2d->mag_filter = GL_LINEAR; /* Wrap mode not yet set */ tex_2d->wrap_mode = GL_FALSE; @@ -263,6 +263,7 @@ _cogl_texture_2d_new_with_size (unsigned int width, tex_2d = _cogl_texture_2d_create_base (width, height, flags, internal_format); GE( glGenTextures (1, &tex_2d->gl_texture) ); + GE( glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); GE( glBindTexture (GL_TEXTURE_2D, tex_2d->gl_texture) ); GE( glTexImage2D (GL_TEXTURE_2D, 0, gl_intformat, width, height, 0, gl_format, gl_type, NULL) ); @@ -300,7 +301,7 @@ _cogl_texture_2d_new_from_bitmap (CoglHandle bmp_handle, flags, internal_format); - GE( glGenTextures (1, &tex_2d->gl_texture) ); + _cogl_texture_driver_gen (GL_TEXTURE_2D, 1, &tex_2d->gl_texture); _cogl_texture_driver_upload_to_gl (GL_TEXTURE_2D, tex_2d->gl_texture, &dst_bmp, diff --git a/clutter/cogl/cogl/cogl-texture-driver.h b/clutter/cogl/cogl/cogl-texture-driver.h index 6961659..5f08cfb 100644 --- a/clutter/cogl/cogl/cogl-texture-driver.h +++ b/clutter/cogl/cogl/cogl-texture-driver.h @@ -25,6 +25,15 @@ #define __COGL_TEXTURE_DRIVER_H /* + * A very small wrapper around glGenTextures() that ensures we default to + * non-mipmap filters when creating textures. This is to save some memory as + * the driver will not allocate room for the mipmap tree. + */ +void +_cogl_texture_driver_gen (GLenum gl_target, + GLsizei n, + GLuint *textures); +/* * Basically just a wrapper around glBindTexture, but the GLES2 backend * for example also wants to know about the internal format so it can * identify when alpha only textures are bound. diff --git a/clutter/cogl/cogl/driver/gl/cogl-texture-driver.c b/clutter/cogl/cogl/driver/gl/cogl-texture-driver.c index f1fe59a..5d7c9f8 100644 --- a/clutter/cogl/cogl/driver/gl/cogl-texture-driver.c +++ b/clutter/cogl/cogl/driver/gl/cogl-texture-driver.c @@ -48,6 +48,33 @@ #define glGenerateMipmap ctx->drv.pf_glGenerateMipmap void +_cogl_texture_driver_gen (GLenum gl_target, + GLsizei n, + GLuint *textures) +{ + guint i; + + GE (glGenTextures (n, textures)); + + for (i = 0; i < n; i++) + { + GE (glBindTexture (gl_target, textures[i])); + + switch (gl_target) + { + case GL_TEXTURE_2D: + /* GL_TEXTURE_MAG_FILTER defaults to GL_LINEAR, no need to set it */ + GE( glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); + break; + + default: + g_assert_not_reached(); + } + + } +} + +void _cogl_texture_driver_bind (GLenum gl_target, GLuint gl_handle, GLenum gl_intformat) diff --git a/clutter/cogl/cogl/driver/gles/cogl-texture-driver.c b/clutter/cogl/cogl/driver/gles/cogl-texture-driver.c index 62324f5..41cc9a8 100644 --- a/clutter/cogl/cogl/driver/gles/cogl-texture-driver.c +++ b/clutter/cogl/cogl/driver/gles/cogl-texture-driver.c @@ -47,6 +47,33 @@ #include "cogl-gles2-wrapper.h" +void +_cogl_texture_driver_gen (GLenum gl_target, + GLsizei n, + GLuint *textures) +{ + guint i; + + GE (glGenTextures (n, textures)); + + for (i = 0; i < n; i++) + { + GE (glBindTexture (gl_target, textures[i])); + + switch (gl_target) + { + case GL_TEXTURE_2D: + /* GL_TEXTURE_MAG_FILTER defaults to GL_LINEAR, no need to set it */ + GE( glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); + break; + + default: + g_assert_not_reached(); + } + + } +} + void _cogl_texture_driver_bind (GLenum gl_target, -- 2.7.4