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