st_gen_mipmap.c revision e0304180c32227342dbb67b707bfae446543bb48
1/************************************************************************** 2 * 3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28 29#include "main/imports.h" 30#include "main/mipmap.h" 31#include "main/teximage.h" 32 33#include "pipe/p_context.h" 34#include "pipe/p_defines.h" 35#include "util/u_inlines.h" 36#include "util/u_format.h" 37#include "util/u_gen_mipmap.h" 38 39#include "st_debug.h" 40#include "st_context.h" 41#include "st_texture.h" 42#include "st_gen_mipmap.h" 43#include "st_cb_texture.h" 44 45 46/** 47 * one-time init for generate mipmap 48 * XXX Note: there may be other times we need no-op/simple state like this. 49 * In that case, some code refactoring would be good. 50 */ 51void 52st_init_generate_mipmap(struct st_context *st) 53{ 54 st->gen_mipmap = util_create_gen_mipmap(st->pipe, st->cso_context); 55} 56 57 58void 59st_destroy_generate_mipmap(struct st_context *st) 60{ 61 util_destroy_gen_mipmap(st->gen_mipmap); 62 st->gen_mipmap = NULL; 63} 64 65 66/** 67 * Generate mipmap levels using hardware rendering. 68 * \return TRUE if successful, FALSE if not possible 69 */ 70static boolean 71st_render_mipmap(struct st_context *st, 72 GLenum target, 73 struct st_texture_object *stObj, 74 uint baseLevel, uint lastLevel) 75{ 76 struct pipe_context *pipe = st->pipe; 77 struct pipe_screen *screen = pipe->screen; 78 struct pipe_sampler_view *psv = st_get_texture_sampler_view(stObj, pipe); 79 const uint face = _mesa_tex_target_to_face(target); 80 81 assert(psv->texture == stObj->pt); 82#if 0 83 assert(target != GL_TEXTURE_3D); /* implemented but untested */ 84#endif 85 86 /* check if we can render in the texture's format */ 87 /* XXX should probably kill this and always use util_gen_mipmap 88 since this implements a sw fallback as well */ 89 if (!screen->is_format_supported(screen, psv->format, psv->texture->target, 90 0, PIPE_BIND_RENDER_TARGET)) { 91 return FALSE; 92 } 93 94 /* Disable conditional rendering. */ 95 if (st->render_condition) { 96 pipe->render_condition(pipe, NULL, 0); 97 } 98 99 util_gen_mipmap(st->gen_mipmap, psv, face, baseLevel, lastLevel, 100 PIPE_TEX_FILTER_LINEAR); 101 102 if (st->render_condition) { 103 pipe->render_condition(pipe, st->render_condition, st->condition_mode); 104 } 105 106 return TRUE; 107} 108 109/** 110 * Compute the expected number of mipmap levels in the texture given 111 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/ 112 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap 113 * levels should be generated. 114 */ 115static GLuint 116compute_num_levels(struct gl_context *ctx, 117 struct gl_texture_object *texObj, 118 GLenum target) 119{ 120 if (target == GL_TEXTURE_RECTANGLE_ARB) { 121 return 1; 122 } 123 else { 124 const struct gl_texture_image *baseImage = 125 _mesa_get_tex_image(ctx, texObj, target, texObj->BaseLevel); 126 GLuint size, numLevels; 127 128 size = MAX2(baseImage->Width2, baseImage->Height2); 129 size = MAX2(size, baseImage->Depth2); 130 131 numLevels = texObj->BaseLevel; 132 133 while (size > 0) { 134 numLevels++; 135 size >>= 1; 136 } 137 138 numLevels = MIN2(numLevels, texObj->MaxLevel + 1); 139 140 assert(numLevels >= 1); 141 142 return numLevels; 143 } 144} 145 146 147/** 148 * Called via ctx->Driver.GenerateMipmap(). 149 */ 150void 151st_generate_mipmap(struct gl_context *ctx, GLenum target, 152 struct gl_texture_object *texObj) 153{ 154 struct st_context *st = st_context(ctx); 155 struct st_texture_object *stObj = st_texture_object(texObj); 156 struct pipe_resource *pt = st_get_texobj_resource(texObj); 157 const uint baseLevel = texObj->BaseLevel; 158 uint lastLevel; 159 uint dstLevel; 160 161 if (!pt) 162 return; 163 164 /* not sure if this ultimately actually should work, 165 but we're not supporting multisampled textures yet. */ 166 assert(pt->nr_samples < 2); 167 168 /* find expected last mipmap level to generate*/ 169 lastLevel = compute_num_levels(ctx, texObj, target) - 1; 170 171 if (lastLevel == 0) 172 return; 173 174 /* The texture isn't in a "complete" state yet so set the expected 175 * lastLevel here, since it won't get done in st_finalize_texture(). 176 */ 177 stObj->lastLevel = lastLevel; 178 179 if (pt->last_level < lastLevel) { 180 /* The current gallium texture doesn't have space for all the 181 * mipmap levels we need to generate. So allocate a new texture. 182 */ 183 struct pipe_resource *oldTex = stObj->pt; 184 185 /* create new texture with space for more levels */ 186 stObj->pt = st_texture_create(st, 187 oldTex->target, 188 oldTex->format, 189 lastLevel, 190 oldTex->width0, 191 oldTex->height0, 192 oldTex->depth0, 193 oldTex->array_size, 194 oldTex->bind); 195 196 /* This will copy the old texture's base image into the new texture 197 * which we just allocated. 198 */ 199 st_finalize_texture(ctx, st->pipe, texObj); 200 201 /* release the old tex (will likely be freed too) */ 202 pipe_resource_reference(&oldTex, NULL); 203 pipe_sampler_view_reference(&stObj->sampler_view, NULL); 204 } 205 else { 206 /* Make sure that the base texture image data is present in the 207 * texture buffer. 208 */ 209 st_finalize_texture(ctx, st->pipe, texObj); 210 } 211 212 pt = stObj->pt; 213 214 assert(pt->last_level >= lastLevel); 215 216 /* Try to generate the mipmap by rendering/texturing. If that fails, 217 * use the software fallback. 218 */ 219 if (!st_render_mipmap(st, target, stObj, baseLevel, lastLevel)) { 220 /* since the util code actually also has a fallback, should 221 probably make it never fail and kill this */ 222 _mesa_generate_mipmap(ctx, target, texObj); 223 } 224 225 /* Fill in the Mesa gl_texture_image fields */ 226 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) { 227 const uint srcLevel = dstLevel - 1; 228 const struct gl_texture_image *srcImage 229 = _mesa_get_tex_image(ctx, texObj, target, srcLevel); 230 struct gl_texture_image *dstImage; 231 struct st_texture_image *stImage; 232 uint dstWidth = u_minify(pt->width0, dstLevel); 233 uint dstHeight = u_minify(pt->height0, dstLevel); 234 uint dstDepth = u_minify(pt->depth0, dstLevel); 235 uint border = srcImage->Border; 236 237 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel); 238 if (!dstImage) { 239 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps"); 240 return; 241 } 242 243 /* Free old image data */ 244 ctx->Driver.FreeTextureImageBuffer(ctx, dstImage); 245 246 /* initialize new image */ 247 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight, 248 dstDepth, border, srcImage->InternalFormat, 249 srcImage->TexFormat); 250 251 stImage = st_texture_image(dstImage); 252 253 pipe_resource_reference(&stImage->pt, pt); 254 } 255} 256