st_gen_mipmap.c revision 3293bcdc80cdfa20a2381aae2b94505bdf95d857
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#include "main/texformat.h" 33 34#include "pipe/p_context.h" 35#include "pipe/p_defines.h" 36#include "util/u_inlines.h" 37#include "util/u_format.h" 38#include "util/u_gen_mipmap.h" 39 40#include "st_debug.h" 41#include "st_context.h" 42#include "st_texture.h" 43#include "st_gen_mipmap.h" 44#include "st_cb_texture.h" 45 46 47/** 48 * one-time init for generate mipmap 49 * XXX Note: there may be other times we need no-op/simple state like this. 50 * In that case, some code refactoring would be good. 51 */ 52void 53st_init_generate_mipmap(struct st_context *st) 54{ 55 st->gen_mipmap = util_create_gen_mipmap(st->pipe, st->cso_context); 56} 57 58 59void 60st_destroy_generate_mipmap(struct st_context *st) 61{ 62 util_destroy_gen_mipmap(st->gen_mipmap); 63 st->gen_mipmap = NULL; 64} 65 66 67/** 68 * Generate mipmap levels using hardware rendering. 69 * \return TRUE if successful, FALSE if not possible 70 */ 71static boolean 72st_render_mipmap(struct st_context *st, 73 GLenum target, 74 struct st_texture_object *stObj, 75 uint baseLevel, uint lastLevel) 76{ 77 struct pipe_context *pipe = st->pipe; 78 struct pipe_screen *screen = pipe->screen; 79 struct pipe_sampler_view *psv = st_get_texture_sampler_view(stObj, pipe); 80 const uint face = _mesa_tex_target_to_face(target); 81 82 assert(psv->texture == stObj->pt); 83 assert(target != GL_TEXTURE_3D); /* not done yet */ 84 85 /* check if we can render in the texture's format */ 86 if (!screen->is_format_supported(screen, psv->format, psv->texture->target, 0, 87 PIPE_BIND_RENDER_TARGET, 0)) { 88 return FALSE; 89 } 90 91 util_gen_mipmap(st->gen_mipmap, psv, face, baseLevel, lastLevel, 92 PIPE_TEX_FILTER_LINEAR); 93 94 return TRUE; 95} 96 97 98/** 99 * Helper function to decompress an image. The result is a 32-bpp RGBA 100 * image with stride==width. 101 */ 102static void 103decompress_image(enum pipe_format format, 104 const uint8_t *src, uint8_t *dst, 105 unsigned width, unsigned height) 106{ 107 const struct util_format_description *desc = util_format_description(format); 108 const uint bw = util_format_get_blockwidth(format); 109 const uint bh = util_format_get_blockheight(format); 110 const uint dst_stride = 4 * MAX2(width, bw); 111 const uint src_stride = util_format_get_stride(format, width); 112 113 desc->unpack_rgba_8unorm(dst, dst_stride, src, src_stride, width, height); 114 115 if (width < bw || height < bh) { 116 /* We're decompressing an image smaller than the compression 117 * block size. We don't want garbage pixel values in the region 118 * outside (width x height) so replicate pixels from the (width 119 * x height) region to fill out the (bw x bh) block size. 120 */ 121 uint x, y; 122 for (y = 0; y < bh; y++) { 123 for (x = 0; x < bw; x++) { 124 if (x >= width || y >= height) { 125 uint p = (y * bw + x) * 4; 126 dst[p + 0] = dst[0]; 127 dst[p + 1] = dst[1]; 128 dst[p + 2] = dst[2]; 129 dst[p + 3] = dst[3]; 130 } 131 } 132 } 133 } 134} 135 136 137/** 138 * Helper function to compress an image. The source is a 32-bpp RGBA image 139 * with stride==width. 140 */ 141static void 142compress_image(enum pipe_format format, 143 const uint8_t *src, uint8_t *dst, 144 unsigned width, unsigned height) 145{ 146 const struct util_format_description *desc = util_format_description(format); 147 const uint dst_stride = util_format_get_stride(format, width); 148 const uint src_stride = 4 * width; 149 150 desc->pack_rgba_8unorm(dst, dst_stride, src, src_stride, width, height); 151} 152 153 154/** 155 * Software fallback for generate mipmap levels. 156 */ 157static void 158fallback_generate_mipmap(GLcontext *ctx, GLenum target, 159 struct gl_texture_object *texObj) 160{ 161 struct pipe_context *pipe = st_context(ctx)->pipe; 162 struct pipe_resource *pt = st_get_texobj_resource(texObj); 163 const uint baseLevel = texObj->BaseLevel; 164 const uint lastLevel = pt->last_level; 165 const uint face = _mesa_tex_target_to_face(target), zslice = 0; 166 uint dstLevel; 167 GLenum datatype; 168 GLuint comps; 169 GLboolean compressed; 170 171 if (ST_DEBUG & DEBUG_FALLBACK) 172 debug_printf("%s: fallback processing\n", __FUNCTION__); 173 174 assert(target != GL_TEXTURE_3D); /* not done yet */ 175 176 compressed = 177 _mesa_is_format_compressed(texObj->Image[face][baseLevel]->TexFormat); 178 179 if (compressed) { 180 datatype = GL_UNSIGNED_BYTE; 181 comps = 4; 182 } 183 else { 184 _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat, 185 &datatype, &comps); 186 assert(comps > 0 && "bad texture format in fallback_generate_mipmap()"); 187 } 188 189 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) { 190 const uint srcLevel = dstLevel - 1; 191 const uint srcWidth = u_minify(pt->width0, srcLevel); 192 const uint srcHeight = u_minify(pt->height0, srcLevel); 193 const uint srcDepth = u_minify(pt->depth0, srcLevel); 194 const uint dstWidth = u_minify(pt->width0, dstLevel); 195 const uint dstHeight = u_minify(pt->height0, dstLevel); 196 const uint dstDepth = u_minify(pt->depth0, dstLevel); 197 struct pipe_transfer *srcTrans, *dstTrans; 198 const ubyte *srcData; 199 ubyte *dstData; 200 int srcStride, dstStride; 201 202 srcTrans = pipe_get_transfer(st_context(ctx)->pipe, pt, face, 203 srcLevel, zslice, 204 PIPE_TRANSFER_READ, 0, 0, 205 srcWidth, srcHeight); 206 207 208 dstTrans = pipe_get_transfer(st_context(ctx)->pipe, pt, face, 209 dstLevel, zslice, 210 PIPE_TRANSFER_WRITE, 0, 0, 211 dstWidth, dstHeight); 212 213 srcData = (ubyte *) pipe_transfer_map(pipe, srcTrans); 214 dstData = (ubyte *) pipe_transfer_map(pipe, dstTrans); 215 216 srcStride = srcTrans->stride / util_format_get_blocksize(srcTrans->resource->format); 217 dstStride = dstTrans->stride / util_format_get_blocksize(dstTrans->resource->format); 218 219 if (compressed) { 220 const enum pipe_format format = pt->format; 221 const uint bw = util_format_get_blockwidth(format); 222 const uint bh = util_format_get_blockheight(format); 223 const uint srcWidth2 = align(srcWidth, bw); 224 const uint srcHeight2 = align(srcHeight, bh); 225 const uint dstWidth2 = align(dstWidth, bw); 226 const uint dstHeight2 = align(dstHeight, bh); 227 uint8_t *srcTemp, *dstTemp; 228 229 assert(comps == 4); 230 231 srcTemp = malloc(srcWidth2 * srcHeight2 * comps + 000); 232 dstTemp = malloc(dstWidth2 * dstHeight2 * comps + 000); 233 234 /* decompress the src image: srcData -> srcTemp */ 235 decompress_image(format, srcData, srcTemp, srcWidth, srcHeight); 236 237 _mesa_generate_mipmap_level(target, datatype, comps, 238 0 /*border*/, 239 srcWidth2, srcHeight2, srcDepth, 240 srcTemp, 241 srcWidth2, /* stride in texels */ 242 dstWidth2, dstHeight2, dstDepth, 243 dstTemp, 244 dstWidth2); /* stride in texels */ 245 246 /* compress the new image: dstTemp -> dstData */ 247 compress_image(format, dstTemp, dstData, dstWidth, dstHeight); 248 249 free(srcTemp); 250 free(dstTemp); 251 } 252 else { 253 _mesa_generate_mipmap_level(target, datatype, comps, 254 0 /*border*/, 255 srcWidth, srcHeight, srcDepth, 256 srcData, 257 srcStride, /* stride in texels */ 258 dstWidth, dstHeight, dstDepth, 259 dstData, 260 dstStride); /* stride in texels */ 261 } 262 263 pipe_transfer_unmap(pipe, srcTrans); 264 pipe_transfer_unmap(pipe, dstTrans); 265 266 pipe->transfer_destroy(pipe, srcTrans); 267 pipe->transfer_destroy(pipe, dstTrans); 268 } 269} 270 271 272/** 273 * Compute the expected number of mipmap levels in the texture given 274 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/ 275 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap 276 * levels should be generated. 277 */ 278static GLuint 279compute_num_levels(GLcontext *ctx, 280 struct gl_texture_object *texObj, 281 GLenum target) 282{ 283 if (target == GL_TEXTURE_RECTANGLE_ARB) { 284 return 1; 285 } 286 else { 287 const struct gl_texture_image *baseImage = 288 _mesa_get_tex_image(ctx, texObj, target, texObj->BaseLevel); 289 GLuint size, numLevels; 290 291 size = MAX2(baseImage->Width2, baseImage->Height2); 292 size = MAX2(size, baseImage->Depth2); 293 294 numLevels = texObj->BaseLevel; 295 296 while (size > 0) { 297 numLevels++; 298 size >>= 1; 299 } 300 301 numLevels = MIN2(numLevels, texObj->MaxLevel + 1); 302 303 assert(numLevels >= 1); 304 305 return numLevels; 306 } 307} 308 309 310/** 311 * Called via ctx->Driver.GenerateMipmap(). 312 */ 313void 314st_generate_mipmap(GLcontext *ctx, GLenum target, 315 struct gl_texture_object *texObj) 316{ 317 struct st_context *st = st_context(ctx); 318 struct st_texture_object *stObj = st_texture_object(texObj); 319 struct pipe_resource *pt = st_get_texobj_resource(texObj); 320 const uint baseLevel = texObj->BaseLevel; 321 uint lastLevel; 322 uint dstLevel; 323 324 if (!pt) 325 return; 326 327 /* not sure if this ultimately actually should work, 328 but we're not supporting multisampled textures yet. */ 329 assert(pt->nr_samples < 2); 330 331 /* find expected last mipmap level to generate*/ 332 lastLevel = compute_num_levels(ctx, texObj, target) - 1; 333 334 if (lastLevel == 0) 335 return; 336 337 if (pt->last_level < lastLevel) { 338 /* The current gallium texture doesn't have space for all the 339 * mipmap levels we need to generate. So allocate a new texture. 340 */ 341 struct pipe_resource *oldTex = stObj->pt; 342 343 /* create new texture with space for more levels */ 344 stObj->pt = st_texture_create(st, 345 oldTex->target, 346 oldTex->format, 347 lastLevel, 348 oldTex->width0, 349 oldTex->height0, 350 oldTex->depth0, 351 oldTex->bind); 352 353 /* The texture isn't in a "complete" state yet so set the expected 354 * lastLevel here, since it won't get done in st_finalize_texture(). 355 */ 356 stObj->lastLevel = lastLevel; 357 358 /* This will copy the old texture's base image into the new texture 359 * which we just allocated. 360 */ 361 st_finalize_texture(ctx, st->pipe, texObj); 362 363 /* release the old tex (will likely be freed too) */ 364 pipe_resource_reference(&oldTex, NULL); 365 pipe_sampler_view_reference(&stObj->sampler_view, NULL); 366 367 pt = stObj->pt; 368 } 369 370 assert(pt->last_level >= lastLevel); 371 372 /* Try to generate the mipmap by rendering/texturing. If that fails, 373 * use the software fallback. 374 */ 375 if (!st_render_mipmap(st, target, stObj, baseLevel, lastLevel)) { 376 fallback_generate_mipmap(ctx, target, texObj); 377 } 378 379 /* Fill in the Mesa gl_texture_image fields */ 380 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) { 381 const uint srcLevel = dstLevel - 1; 382 const struct gl_texture_image *srcImage 383 = _mesa_get_tex_image(ctx, texObj, target, srcLevel); 384 struct gl_texture_image *dstImage; 385 struct st_texture_image *stImage; 386 uint dstWidth = u_minify(pt->width0, dstLevel); 387 uint dstHeight = u_minify(pt->height0, dstLevel); 388 uint dstDepth = u_minify(pt->depth0, dstLevel); 389 uint border = srcImage->Border; 390 391 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel); 392 if (!dstImage) { 393 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps"); 394 return; 395 } 396 397 /* Free old image data */ 398 if (dstImage->Data) 399 ctx->Driver.FreeTexImageData(ctx, dstImage); 400 401 /* initialize new image */ 402 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight, 403 dstDepth, border, srcImage->InternalFormat); 404 405 dstImage->TexFormat = srcImage->TexFormat; 406 407 stImage = st_texture_image(dstImage); 408 stImage->level = dstLevel; 409 410 pipe_resource_reference(&stImage->pt, pt); 411 } 412} 413