intel: Add a metaops version of glGenerateMipmapEXT/SGIS_generate_mipmaps.

In addition to being HW accelerated, it avoids the incorrect
(black) rendering of the mipmaps that SW was doing in fbo-generatemipmap.
Improves the performance of the mipmap generation and drawing in
fbo-generatemipmap by 30%.
This commit is contained in:
Eric Anholt
2009-05-07 22:50:19 -07:00
parent b6e94f71c2
commit 1d663ae292
10 changed files with 303 additions and 76 deletions
+1
View File
@@ -0,0 +1 @@
../intel/intel_generatemipmap.c
+1
View File
@@ -14,6 +14,7 @@ DRIVER_SOURCES = \
intel_decode.c \
intel_extensions.c \
intel_fbo.c \
intel_generatemipmap.c \
intel_mipmap_tree.c \
intel_regions.c \
intel_screen.c \
+1
View File
@@ -0,0 +1 @@
../intel/intel_generatemipmap.c
@@ -168,6 +168,8 @@ struct intel_context
struct gl_vertex_program *saved_vp;
GLboolean saved_vp_enable;
struct gl_fragment_program *tex2d_fp;
GLboolean saved_texcoord_enable;
struct gl_buffer_object *saved_array_vbo, *saved_texcoord_vbo;
GLenum saved_texcoord_type;
@@ -0,0 +1,283 @@
/*
* Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
* Copyright © 2009 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*
*/
#include "main/glheader.h"
#include "main/enums.h"
#include "main/image.h"
#include "main/mtypes.h"
#include "main/macros.h"
#include "main/bufferobj.h"
#include "main/teximage.h"
#include "main/texenv.h"
#include "main/texobj.h"
#include "main/texstate.h"
#include "main/texparam.h"
#include "main/varray.h"
#include "main/attrib.h"
#include "main/enable.h"
#include "main/buffers.h"
#include "main/fbobject.h"
#include "main/framebuffer.h"
#include "main/renderbuffer.h"
#include "main/depth.h"
#include "main/hash.h"
#include "main/mipmap.h"
#include "main/blend.h"
#include "glapi/dispatch.h"
#include "swrast/swrast.h"
#include "intel_screen.h"
#include "intel_context.h"
#include "intel_batchbuffer.h"
#include "intel_pixel.h"
#include "intel_tex.h"
#include "intel_mipmap_tree.h"
static const char *intel_fp_tex2d =
"!!ARBfp1.0\n"
"TEX result.color, fragment.texcoord[0], texture[0], 2D;\n"
"END\n";
static GLboolean
intel_generate_mipmap_level(GLcontext *ctx, GLuint tex_name,
int level, int width, int height)
{
struct intel_context *intel = intel_context(ctx);
GLfloat vertices[4][2];
GLint status;
/* Set to source from the previous level */
_mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, level - 1);
_mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, level - 1);
/* Set to draw into the current level */
_mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D,
tex_name,
level);
/* Choose to render to the color attachment. */
_mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
return GL_FALSE;
intel_meta_set_passthrough_transform(intel);
/* XXX: Doing it right would involve setting up the transformation to do
* 0-1 mapping or something, and not changing the vertex data.
*/
vertices[0][0] = 0;
vertices[0][1] = 0;
vertices[1][0] = width;
vertices[1][1] = 0;
vertices[2][0] = width;
vertices[2][1] = height;
vertices[3][0] = 0;
vertices[3][1] = height;
_mesa_VertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &vertices);
_mesa_Enable(GL_VERTEX_ARRAY);
intel_meta_set_default_texrect(intel);
CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
intel_meta_restore_texcoords(intel);
intel_meta_restore_transform(intel);
return GL_TRUE;
}
static GLboolean
intel_generate_mipmap_2d(GLcontext *ctx,
GLenum target,
struct gl_texture_object *texObj)
{
struct intel_context *intel = intel_context(ctx);
GLint old_active_texture;
int level, max_levels, start_level, end_level;
GLuint fb_name;
GLboolean success = GL_FALSE;
struct gl_framebuffer *saved_fbo = NULL;
_mesa_PushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT |
GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
_mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
old_active_texture = ctx->Texture.CurrentUnit;
_mesa_reference_framebuffer(&saved_fbo, ctx->DrawBuffer);
_mesa_Disable(GL_POLYGON_STIPPLE);
_mesa_Disable(GL_DEPTH_TEST);
_mesa_Disable(GL_STENCIL_TEST);
_mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
_mesa_DepthMask(GL_FALSE);
/* Bind the given texture to GL_TEXTURE_2D with linear filtering for our
* minification.
*/
_mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
_mesa_Enable(GL_TEXTURE_2D);
_mesa_BindTexture(GL_TEXTURE_2D, texObj->Name);
_mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST);
_mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* Bind the new renderbuffer to the color attachment point. */
_mesa_GenFramebuffersEXT(1, &fb_name);
_mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb_name);
intel_meta_set_fragment_program(intel, &intel->meta.tex2d_fp,
intel_fp_tex2d);
intel_meta_set_passthrough_vertex_program(intel);
max_levels = _mesa_max_texture_levels(ctx, texObj->Target);
start_level = texObj->BaseLevel;
end_level = texObj->MaxLevel;
/* Loop generating level+1 from level. */
for (level = start_level; level < end_level && level < max_levels - 1; level++) {
const struct gl_texture_image *srcImage;
int width, height;
srcImage = _mesa_select_tex_image(ctx, texObj, target, level);
if (srcImage->Border != 0)
goto fail;
width = srcImage->Width / 2;
if (width < 1)
width = 1;
height = srcImage->Height / 2;
if (height < 1)
height = 1;
if (width == srcImage->Width &&
height == srcImage->Height) {
/* Neither _mesa_max_texture_levels nor texObj->MaxLevel are the
* maximum texture level for the object, so break out when we've gone
* over the edge.
*/
break;
}
/* Make sure that there's space allocated for the target level.
* We could skip this if there's already space allocated and save some
* time.
*/
_mesa_TexImage2D(GL_TEXTURE_2D, level + 1, srcImage->InternalFormat,
width, height, 0,
GL_RGBA, GL_UNSIGNED_INT, NULL);
if (!intel_generate_mipmap_level(ctx, texObj->Name, level + 1,
width, height))
goto fail;
}
success = GL_TRUE;
fail:
intel_meta_restore_fragment_program(intel);
intel_meta_restore_vertex_program(intel);
_mesa_DeleteFramebuffersEXT(1, &fb_name);
_mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
if (saved_fbo)
_mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, saved_fbo->Name);
_mesa_reference_framebuffer(&saved_fbo, NULL);
_mesa_PopClientAttrib();
_mesa_PopAttrib();
return success;
}
/**
* Generate new mipmap data from BASE+1 to BASE+p (the minimally-sized mipmap
* level).
*
* The texture object's miptree must be mapped.
*
* It would be really nice if this was just called by Mesa whenever mipmaps
* needed to be regenerated, rather than us having to remember to do so in
* each texture image modification path.
*
* This function should also include an accelerated path.
*/
void
intel_generate_mipmap(GLcontext *ctx, GLenum target,
struct gl_texture_object *texObj)
{
struct intel_context *intel = intel_context(ctx);
struct intel_texture_object *intelObj = intel_texture_object(texObj);
GLuint nr_faces = (intelObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
int face, i;
/* HW path */
if (target == GL_TEXTURE_2D &&
ctx->Extensions.EXT_framebuffer_object &&
ctx->Extensions.ARB_fragment_program &&
ctx->Extensions.ARB_vertex_program) {
GLboolean success;
/* We'll be accessing this texture using GL entrypoints, which should
* be resilient against other access to this texture.
*/
_mesa_unlock_texture(ctx, texObj);
success = intel_generate_mipmap_2d(ctx, target, texObj);
_mesa_lock_texture(ctx, texObj);
if (success)
return;
}
/* SW path */
intel_tex_map_level_images(intel, intelObj, texObj->BaseLevel);
_mesa_generate_mipmap(ctx, target, texObj);
intel_tex_unmap_level_images(intel, intelObj, texObj->BaseLevel);
/* Update the level information in our private data in the new images, since
* it didn't get set as part of a normal TexImage path.
*/
for (face = 0; face < nr_faces; face++) {
for (i = texObj->BaseLevel + 1; i < texObj->MaxLevel; i++) {
struct intel_texture_image *intelImage;
intelImage = intel_texture_image(texObj->Image[face][i]);
if (intelImage == NULL)
break;
intelImage->level = i;
intelImage->face = face;
/* Unreference the miptree to signal that the new Data is a bare
* pointer from mesa.
*/
intel_miptree_release(intel, &intelImage->mt);
}
}
}
+1
View File
@@ -437,6 +437,7 @@ intel_free_pixel_state(struct intel_context *intel)
_mesa_reference_vertprog(ctx, &intel->meta.passthrough_vp, NULL);
_mesa_reference_fragprog(ctx, &intel->meta.bitmap_fp, NULL);
_mesa_reference_fragprog(ctx, &intel->meta.tex2d_fp, NULL);
_mesa_reference_buffer_object(ctx, &intel->meta.texcoord_vbo, NULL);
}
+1 -55
View File
@@ -158,60 +158,6 @@ timed_memcpy(void *dest, const void *src, size_t n)
}
#endif /* DO_DEBUG */
/**
* Generate new mipmap data from BASE+1 to BASE+p (the minimally-sized mipmap
* level).
*
* The texture object's miptree must be mapped.
*
* It would be really nice if this was just called by Mesa whenever mipmaps
* needed to be regenerated, rather than us having to remember to do so in
* each texture image modification path.
*
* This function should also include an accelerated path.
*/
void
intel_generate_mipmap(GLcontext *ctx, GLenum target,
struct gl_texture_object *texObj)
{
struct intel_context *intel = intel_context(ctx);
struct intel_texture_object *intelObj = intel_texture_object(texObj);
GLuint nr_faces = (intelObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
int face, i;
_mesa_generate_mipmap(ctx, target, texObj);
/* Update the level information in our private data in the new images, since
* it didn't get set as part of a normal TexImage path.
*/
for (face = 0; face < nr_faces; face++) {
for (i = texObj->BaseLevel + 1; i < texObj->MaxLevel; i++) {
struct intel_texture_image *intelImage;
intelImage = intel_texture_image(texObj->Image[face][i]);
if (intelImage == NULL)
break;
intelImage->level = i;
intelImage->face = face;
/* Unreference the miptree to signal that the new Data is a bare
* pointer from mesa.
*/
intel_miptree_release(intel, &intelImage->mt);
}
}
}
static void intelGenerateMipmap(GLcontext *ctx, GLenum target, struct gl_texture_object *texObj)
{
struct intel_context *intel = intel_context(ctx);
struct intel_texture_object *intelObj = intel_texture_object(texObj);
intel_tex_map_level_images(intel, intelObj, texObj->BaseLevel);
intel_generate_mipmap(ctx, target, texObj);
intel_tex_unmap_level_images(intel, intelObj, texObj->BaseLevel);
}
void
intelInitTextureFuncs(struct dd_function_table *functions)
{
@@ -227,7 +173,7 @@ intelInitTextureFuncs(struct dd_function_table *functions)
functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
functions->GetTexImage = intelGetTexImage;
functions->GenerateMipmap = intelGenerateMipmap;
functions->GenerateMipmap = intel_generate_mipmap;
/* compressed texture functions */
functions->CompressedTexImage2D = intelCompressedTexImage2D;
+1 -1
View File
@@ -158,7 +158,7 @@ do_copy_texsubimage(struct intel_context *intel,
/* GL_SGIS_generate_mipmap */
if (intelImage->level == texObj->BaseLevel && texObj->GenerateMipmap) {
ctx->Driver.GenerateMipmap(ctx, target, texObj);
intel_generate_mipmap(ctx, target, texObj);
}
return GL_TRUE;
+7 -15
View File
@@ -316,7 +316,6 @@ intelTexImage(GLcontext * ctx,
GLint postConvHeight = height;
GLint texelBytes, sizeInBytes;
GLuint dstRowStride = 0, srcRowStride = texImage->RowStride;
GLboolean needs_map;
DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
_mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
@@ -482,15 +481,8 @@ intelTexImage(GLcontext * ctx,
LOCK_HARDWARE(intel);
/* Two cases where we need a mapping of the miptree: when the user supplied
* data is mapped as well (non-PBO, memcpy upload) or when we're going to do
* (software) mipmap generation.
*/
needs_map = (pixels != NULL) || (level == texObj->BaseLevel &&
texObj->GenerateMipmap);
if (intelImage->mt) {
if (needs_map)
if (pixels != NULL)
texImage->Data = intel_miptree_image_map(intel,
intelImage->mt,
intelImage->face,
@@ -547,22 +539,22 @@ intelTexImage(GLcontext * ctx,
format, type, pixels, unpack)) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
}
/* GL_SGIS_generate_mipmap */
if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
intel_generate_mipmap(ctx, target, texObj);
}
}
_mesa_unmap_teximage_pbo(ctx, unpack);
if (intelImage->mt) {
if (needs_map)
if (pixels != NULL)
intel_miptree_image_unmap(intel, intelImage->mt);
texImage->Data = NULL;
}
UNLOCK_HARDWARE(intel);
/* GL_SGIS_generate_mipmap */
if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
intel_generate_mipmap(ctx, target, texObj);
}
}
void
@@ -101,11 +101,6 @@ intelTexSubimage(GLcontext * ctx,
_mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
}
/* GL_SGIS_generate_mipmap */
if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
intel_generate_mipmap(ctx, target, texObj);
}
_mesa_unmap_teximage_pbo(ctx, packing);
if (intelImage->mt) {
@@ -114,6 +109,11 @@ intelTexSubimage(GLcontext * ctx,
}
UNLOCK_HARDWARE(intel);
/* GL_SGIS_generate_mipmap */
if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
intel_generate_mipmap(ctx, target, texObj);
}
}