st/mesa: ignore gl_texture_object::BaseLevel when allocating gallium textures

Previously, when we created a gallium texture for a corresponding Mesa
texture we'd only allocate space for mipmap levels >= BaseLevel.

This patch undoes that mechanism.  This fixes a render-to-texture bug
when rendering to level 0 when BaseLevel=1.

Also, it makes sense to allocate the whole texture object memory when
BaseLevel > 0 since a common use of GL_TEXTURE_BASE_LEVEL is to
progressively load/render mipmaps.  Eventually, the app almost always
fills in the level=0 mipmap image.

Finally, the texture image code is bit easier to understand now.
This commit is contained in:
Brian Paul
2010-04-29 15:32:36 -06:00
parent e9bf09a98a
commit e648d4a1d1
7 changed files with 122 additions and 81 deletions

View File

@@ -194,9 +194,12 @@ update_samplers(struct st_context *st)
sampler->normalized_coords = 1; sampler->normalized_coords = 1;
sampler->lod_bias = st->ctx->Texture.Unit[su].LodBias; sampler->lod_bias = st->ctx->Texture.Unit[su].LodBias;
sampler->min_lod = MAX2(0.0f, texobj->MinLod);
sampler->max_lod = MIN2(texobj->MaxLevel - texobj->BaseLevel, sampler->min_lod = texobj->BaseLevel + texobj->MinLod;
texobj->MaxLod); if (sampler->min_lod < texobj->BaseLevel)
sampler->min_lod = texobj->BaseLevel;
sampler->max_lod = MIN2((GLfloat) texobj->MaxLevel, texobj->MaxLod);
if (sampler->max_lod < sampler->min_lod) { if (sampler->max_lod < sampler->min_lod) {
/* The GL spec doesn't seem to specify what to do in this case. /* The GL spec doesn't seem to specify what to do in this case.
* Swap the values. * Swap the values.

View File

@@ -138,7 +138,6 @@ finalize_textures(struct st_context *st)
const GLuint texUnit = fprog->Base.SamplerUnits[su]; const GLuint texUnit = fprog->Base.SamplerUnits[su];
struct gl_texture_object *texObj struct gl_texture_object *texObj
= st->ctx->Texture.Unit[texUnit]._Current; = st->ctx->Texture.Unit[texUnit]._Current;
struct st_texture_object *stObj = st_texture_object(texObj);
if (texObj) { if (texObj) {
GLboolean flush, retval; GLboolean flush, retval;
@@ -149,8 +148,6 @@ finalize_textures(struct st_context *st)
st->missing_textures = GL_TRUE; st->missing_textures = GL_TRUE;
continue; continue;
} }
stObj->teximage_realloc = TRUE;
} }
} }
} }

View File

@@ -321,15 +321,13 @@ st_render_texture(GLcontext *ctx,
struct pipe_resource *pt = st_get_texobj_resource(att->Texture); struct pipe_resource *pt = st_get_texobj_resource(att->Texture);
struct st_texture_object *stObj; struct st_texture_object *stObj;
const struct gl_texture_image *texImage; const struct gl_texture_image *texImage;
GLint pt_level;
/* When would this fail? Perhaps assert? */ /* When would this fail? Perhaps assert? */
if (!pt) if (!pt)
return; return;
/* The first gallium texture level = Mesa BaseLevel */ /* get pointer to texture image we're rendeing to */
pt_level = MAX2(0, (GLint) att->TextureLevel - att->Texture->BaseLevel); texImage = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
texImage = att->Texture->Image[att->CubeMapFace][pt_level];
/* create new renderbuffer which wraps the texture image */ /* create new renderbuffer which wraps the texture image */
rb = st_new_renderbuffer(ctx, 0); rb = st_new_renderbuffer(ctx, 0);
@@ -350,7 +348,7 @@ st_render_texture(GLcontext *ctx,
/* point renderbuffer at texobject */ /* point renderbuffer at texobject */
strb->rtt = stObj; strb->rtt = stObj;
strb->rtt_level = pt_level; strb->rtt_level = att->TextureLevel;
strb->rtt_face = att->CubeMapFace; strb->rtt_face = att->CubeMapFace;
strb->rtt_slice = att->Zoffset; strb->rtt_slice = att->Zoffset;

View File

@@ -231,70 +231,86 @@ default_bindings(struct st_context *st, enum pipe_format format)
} }
/** Return number of image dimensions (1, 2 or 3) for a texture target. */
static GLuint
get_texture_dims(GLenum target)
{
switch (target) {
case GL_TEXTURE_1D:
case GL_TEXTURE_1D_ARRAY_EXT:
return 1;
case GL_TEXTURE_2D:
case GL_TEXTURE_CUBE_MAP_ARB:
case GL_TEXTURE_RECTANGLE_NV:
case GL_TEXTURE_2D_ARRAY_EXT:
return 2;
case GL_TEXTURE_3D:
return 3;
default:
assert(0 && "invalid texture target in get_texture_dims()");
return 1;
}
}
/** /**
* Allocate a pipe_resource object for the given st_texture_object using * Try to allocate a pipe_resource object for the given st_texture_object.
* the given st_texture_image to guess the mipmap size/levels.
* *
* [comments...] * We use the given st_texture_image as a clue to determine the size of the
* Otherwise, store it in memory if (Border != 0) or (any dimension == * mipmap image at level=0.
* 1).
* *
* Otherwise, if max_level >= level >= min_level, create texture with
* space for images from min_level down to max_level.
*
* Otherwise, create texture with space for images from (level 0)..(1x1).
* Consider pruning this texture at a validation if the saving is worth it.
*/ */
static void static void
guess_and_alloc_texture(struct st_context *st, guess_and_alloc_texture(struct st_context *st,
struct st_texture_object *stObj, struct st_texture_object *stObj,
const struct st_texture_image *stImage) const struct st_texture_image *stImage)
{ {
GLuint firstLevel; const GLuint dims = get_texture_dims(stObj->base.Target);
GLuint lastLevel; GLuint level, lastLevel, width, height, depth;
GLuint width = stImage->base.Width2; /* size w/out border */ GLuint bindings;
GLuint height = stImage->base.Height2;
GLuint depth = stImage->base.Depth2;
GLuint i, bindings;
enum pipe_format fmt; enum pipe_format fmt;
DBG("%s\n", __FUNCTION__); DBG("%s\n", __FUNCTION__);
assert(!stObj->pt); assert(!stObj->pt);
if (stObj->pt && level = stImage->level;
(GLint) stImage->level > stObj->base.BaseLevel && width = stImage->base.Width2; /* size w/out border */
(stImage->base.Width == 1 || height = stImage->base.Height2;
(stObj->base.Target != GL_TEXTURE_1D && depth = stImage->base.Depth2;
stImage->base.Height == 1) ||
(stObj->base.Target == GL_TEXTURE_3D && assert(width > 0);
stImage->base.Depth == 1))) assert(height > 0);
assert(depth > 0);
/* Depending on the image's size, we can't always make a guess here.
*/
if (level > 0) {
if ( (dims >= 1 && width == 1) ||
(dims >= 2 && height == 1) ||
(dims >= 3 && depth == 1) ) {
/* we can't determine the image size at level=0 */
stObj->width0 = stObj->height0 = stObj->depth0 = 0;
return; return;
}
}
/* If this image disrespects BaseLevel, allocate from level zero. /* grow the image size until we hit level = 0 */
* Usually BaseLevel == 0, so it's unlikely to happen. while (level > 0) {
*/
if ((GLint) stImage->level < stObj->base.BaseLevel)
firstLevel = 0;
else
firstLevel = stObj->base.BaseLevel;
/* Figure out image dimensions at start level.
*/
for (i = stImage->level; i > firstLevel; i--) {
if (width != 1) if (width != 1)
width <<= 1; width <<= 1;
if (height != 1) if (height != 1)
height <<= 1; height <<= 1;
if (depth != 1) if (depth != 1)
depth <<= 1; depth <<= 1;
level--;
} }
if (width == 0 || height == 0 || depth == 0) { assert(level == 0);
/* no texture needed */
return; /* At this point, (width x height x depth) is the expected size of
} * the level=0 mipmap image.
*/
/* Guess a reasonable value for lastLevel. This is probably going /* Guess a reasonable value for lastLevel. This is probably going
* to be wrong fairly often and might mean that we have to look at * to be wrong fairly often and might mean that we have to look at
@@ -306,18 +322,23 @@ guess_and_alloc_texture(struct st_context *st,
stImage->base._BaseFormat == GL_DEPTH_COMPONENT || stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) && stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
!stObj->base.GenerateMipmap && !stObj->base.GenerateMipmap &&
stImage->level == firstLevel) { stImage->level == 0) {
/* only alloc space for a single mipmap level */ /* only alloc space for a single mipmap level */
lastLevel = firstLevel; lastLevel = 0;
} }
else { else {
/* alloc space for a full mipmap */ /* alloc space for a full mipmap */
GLuint l2width = util_logbase2(width); GLuint l2width = util_logbase2(width);
GLuint l2height = util_logbase2(height); GLuint l2height = util_logbase2(height);
GLuint l2depth = util_logbase2(depth); GLuint l2depth = util_logbase2(depth);
lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth); lastLevel = MAX2(MAX2(l2width, l2height), l2depth);
} }
/* Save the level=0 dimensions */
stObj->width0 = width;
stObj->height0 = height;
stObj->depth0 = depth;
fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat); fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
bindings = default_bindings(st, fmt); bindings = default_bindings(st, fmt);
@@ -581,15 +602,13 @@ st_TexImage(GLcontext * ctx,
* mipmap. If so, free the old mipmap. * mipmap. If so, free the old mipmap.
*/ */
if (stObj->pt) { if (stObj->pt) {
if (stObj->teximage_realloc || if (level > (GLint) stObj->pt->last_level ||
level > (GLint) stObj->pt->last_level ||
!st_texture_match_image(stObj->pt, &stImage->base, !st_texture_match_image(stObj->pt, &stImage->base,
stImage->face, stImage->level)) { stImage->face, stImage->level)) {
DBG("release it\n"); DBG("release it\n");
pipe_resource_reference(&stObj->pt, NULL); pipe_resource_reference(&stObj->pt, NULL);
assert(!stObj->pt); assert(!stObj->pt);
pipe_sampler_view_reference(&stObj->sampler_view, NULL); pipe_sampler_view_reference(&stObj->sampler_view, NULL);
stObj->teximage_realloc = FALSE;
} }
} }
@@ -1757,12 +1776,26 @@ st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level,
} }
/**
* Copy image data from stImage into the texture object 'stObj' at level
* 'dstLevel'.
*/
static void static void
copy_image_data_to_texture(struct st_context *st, copy_image_data_to_texture(struct st_context *st,
struct st_texture_object *stObj, struct st_texture_object *stObj,
GLuint dstLevel, GLuint dstLevel,
struct st_texture_image *stImage) struct st_texture_image *stImage)
{ {
/* debug checks */
{
const struct gl_texture_image *dstImage =
stObj->base.Image[stImage->face][stImage->level];
assert(dstImage);
assert(dstImage->Width == stImage->base.Width);
assert(dstImage->Height == stImage->base.Height);
assert(dstImage->Depth == stImage->base.Depth);
}
if (stImage->pt) { if (stImage->pt) {
/* Copy potentially with the blitter: /* Copy potentially with the blitter:
*/ */
@@ -1811,8 +1844,9 @@ st_finalize_texture(GLcontext *ctx,
struct st_context *st = st_context(ctx); struct st_context *st = st_context(ctx);
struct st_texture_object *stObj = st_texture_object(tObj); struct st_texture_object *stObj = st_texture_object(tObj);
const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1; const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
GLuint blockSize, face; GLuint face;
struct st_texture_image *firstImage; struct st_texture_image *firstImage;
enum pipe_format firstImageFormat;
*needFlush = GL_FALSE; *needFlush = GL_FALSE;
@@ -1827,10 +1861,11 @@ st_finalize_texture(GLcontext *ctx,
stObj->base.MinFilter == GL_NEAREST) stObj->base.MinFilter == GL_NEAREST)
stObj->lastLevel = stObj->base.BaseLevel; stObj->lastLevel = stObj->base.BaseLevel;
else else
stObj->lastLevel = stObj->base._MaxLevel - stObj->base.BaseLevel; stObj->lastLevel = stObj->base._MaxLevel;
} }
firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]); firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
assert(firstImage);
/* If both firstImage and stObj point to a texture which can contain /* If both firstImage and stObj point to a texture which can contain
* all active images, favour firstImage. Note that because of the * all active images, favour firstImage. Note that because of the
@@ -1844,22 +1879,23 @@ st_finalize_texture(GLcontext *ctx,
pipe_sampler_view_reference(&stObj->sampler_view, NULL); pipe_sampler_view_reference(&stObj->sampler_view, NULL);
} }
/* bytes per pixel block (blocks are usually 1x1) */ /* Find gallium format for the Mesa texture */
blockSize = _mesa_get_format_bytes(firstImage->base.TexFormat); firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
/* If we already have a gallium texture, check that it matches the texture /* If we already have a gallium texture, check that it matches the texture
* object's format, target, size, num_levels, etc. * object's format, target, size, num_levels, etc.
*/ */
if (stObj->pt) { if (stObj->pt) {
const enum pipe_format fmt =
st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) || if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
stObj->pt->format != fmt || stObj->pt->format != firstImageFormat ||
stObj->pt->last_level < stObj->lastLevel || stObj->pt->last_level != stObj->lastLevel ||
stObj->pt->width0 != firstImage->base.Width2 || stObj->pt->width0 != stObj->width0 ||
stObj->pt->height0 != firstImage->base.Height2 || stObj->pt->height0 != stObj->height0 ||
stObj->pt->depth0 != firstImage->base.Depth2) stObj->pt->depth0 != stObj->depth0)
{ {
/* The gallium texture does not match the Mesa texture so delete the
* gallium texture now. We'll make a new one below.
*/
pipe_resource_reference(&stObj->pt, NULL); pipe_resource_reference(&stObj->pt, NULL);
pipe_sampler_view_reference(&stObj->sampler_view, NULL); pipe_sampler_view_reference(&stObj->sampler_view, NULL);
st->dirty.st |= ST_NEW_FRAMEBUFFER; st->dirty.st |= ST_NEW_FRAMEBUFFER;
@@ -1869,17 +1905,15 @@ st_finalize_texture(GLcontext *ctx,
/* May need to create a new gallium texture: /* May need to create a new gallium texture:
*/ */
if (!stObj->pt) { if (!stObj->pt) {
const enum pipe_format fmt = GLuint bindings = default_bindings(st, firstImageFormat);
st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
GLuint bindings = default_bindings(st, fmt);
stObj->pt = st_texture_create(st, stObj->pt = st_texture_create(st,
gl_target_to_pipe(stObj->base.Target), gl_target_to_pipe(stObj->base.Target),
fmt, firstImageFormat,
stObj->lastLevel, stObj->lastLevel,
firstImage->base.Width2, stObj->width0,
firstImage->base.Height2, stObj->height0,
firstImage->base.Depth2, stObj->depth0,
bindings); bindings);
if (!stObj->pt) { if (!stObj->pt) {
@@ -1894,7 +1928,7 @@ st_finalize_texture(GLcontext *ctx,
GLuint level; GLuint level;
for (level = 0; level <= stObj->lastLevel; level++) { for (level = 0; level <= stObj->lastLevel; level++) {
struct st_texture_image *stImage = struct st_texture_image *stImage =
st_texture_image(stObj->base.Image[face][stObj->base.BaseLevel + level]); st_texture_image(stObj->base.Image[face][level]);
/* Need to import images in main memory or held in other textures. /* Need to import images in main memory or held in other textures.
*/ */

View File

@@ -342,10 +342,10 @@ st_generate_mipmap(GLcontext *ctx, GLenum target,
assert(lastLevel <= pt->last_level); assert(lastLevel <= pt->last_level);
/* Recall that the Mesa BaseLevel image is stored in the gallium /* Try to generate the mipmap by rendering/texturing. If that fails,
* texture's level[0] position. So pass baseLevel=0 here. * use the software fallback.
*/ */
if (!st_render_mipmap(st, target, stObj, 0, lastLevel)) { if (!st_render_mipmap(st, target, stObj, baseLevel, lastLevel)) {
fallback_generate_mipmap(ctx, target, texObj); fallback_generate_mipmap(ctx, target, texObj);
} }

View File

@@ -269,6 +269,9 @@ st_texture_image_copy(struct pipe_context *pipe,
struct pipe_surface *dst_surface; struct pipe_surface *dst_surface;
GLuint i; GLuint i;
assert(src->width0 == dst->width0);
assert(src->height0 == dst->height0);
for (i = 0; i < depth; i++) { for (i = 0; i < depth; i++) {
GLuint srcLevel; GLuint srcLevel;

View File

@@ -38,6 +38,9 @@
struct pipe_resource; struct pipe_resource;
/**
* Subclass of gl_texure_image.
*/
struct st_texture_image struct st_texture_image
{ {
struct gl_texture_image base; struct gl_texture_image base;
@@ -57,7 +60,9 @@ struct st_texture_image
}; };
/**
* Subclass of gl_texure_object.
*/
struct st_texture_object struct st_texture_object
{ {
struct gl_texture_object base; /* The "parent" object */ struct gl_texture_object base; /* The "parent" object */
@@ -66,6 +71,9 @@ struct st_texture_object
*/ */
GLuint lastLevel; GLuint lastLevel;
/** The size of the level=0 mipmap image */
GLuint width0, height0, depth0;
/* On validation any active images held in main memory or in other /* On validation any active images held in main memory or in other
* textures will be copied to this texture and the old storage freed. * textures will be copied to this texture and the old storage freed.
*/ */
@@ -76,8 +84,6 @@ struct st_texture_object
*/ */
struct pipe_sampler_view *sampler_view; struct pipe_sampler_view *sampler_view;
GLboolean teximage_realloc;
/* True if there is/was a surface bound to this texture object. It helps /* True if there is/was a surface bound to this texture object. It helps
* track whether the texture object is surface based or not. * track whether the texture object is surface based or not.
*/ */