1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 #include "main/imports.h"
30 #include "main/mipmap.h"
31 #include "main/teximage.h"
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"
40 #include "st_context.h"
41 #include "st_texture.h"
42 #include "st_gen_mipmap.h"
43 #include "st_cb_texture.h"
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.
52 st_init_generate_mipmap(struct st_context
*st
)
54 st
->gen_mipmap
= util_create_gen_mipmap(st
->pipe
, st
->cso_context
);
59 st_destroy_generate_mipmap(struct st_context
*st
)
61 util_destroy_gen_mipmap(st
->gen_mipmap
);
62 st
->gen_mipmap
= NULL
;
67 * Generate mipmap levels using hardware rendering.
68 * \return TRUE if successful, FALSE if not possible
71 st_render_mipmap(struct st_context
*st
,
73 struct st_texture_object
*stObj
,
74 uint baseLevel
, uint lastLevel
)
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
);
81 assert(psv
->texture
== stObj
->pt
);
83 assert(target
!= GL_TEXTURE_3D
); /* implemented but untested */
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)) {
94 util_gen_mipmap(st
->gen_mipmap
, psv
, face
, baseLevel
, lastLevel
,
95 PIPE_TEX_FILTER_LINEAR
);
102 * Helper function to decompress an image. The result is a 32-bpp RGBA
103 * image with stride==width.
106 decompress_image(enum pipe_format format
, int datatype
,
107 const uint8_t *src
, void *dst
,
108 unsigned width
, unsigned height
, unsigned src_stride
)
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 uint dst_stride
= 4 * MAX2(width
, bw
);
115 if (datatype
== GL_FLOAT
) {
116 desc
->unpack_rgba_float((float *)dst
, dst_stride
* sizeof(GLfloat
), src
, src_stride
, width
, height
);
117 if (width
< bw
|| height
< bh
) {
118 float *dst_p
= (float *)dst
;
119 /* We're decompressing an image smaller than the compression
120 * block size. We don't want garbage pixel values in the region
121 * outside (width x height) so replicate pixels from the (width
122 * x height) region to fill out the (bw x bh) block size.
125 for (y
= 0; y
< bh
; y
++) {
126 for (x
= 0; x
< bw
; x
++) {
127 if (x
>= width
|| y
>= height
) {
128 uint p
= (y
* bw
+ x
) * 4;
129 dst_p
[p
+ 0] = dst_p
[0];
130 dst_p
[p
+ 1] = dst_p
[1];
131 dst_p
[p
+ 2] = dst_p
[2];
132 dst_p
[p
+ 3] = dst_p
[3];
138 desc
->unpack_rgba_8unorm((uint8_t *)dst
, dst_stride
, src
, src_stride
, width
, height
);
139 if (width
< bw
|| height
< bh
) {
140 uint8_t *dst_p
= (uint8_t *)dst
;
141 /* We're decompressing an image smaller than the compression
142 * block size. We don't want garbage pixel values in the region
143 * outside (width x height) so replicate pixels from the (width
144 * x height) region to fill out the (bw x bh) block size.
147 for (y
= 0; y
< bh
; y
++) {
148 for (x
= 0; x
< bw
; x
++) {
149 if (x
>= width
|| y
>= height
) {
150 uint p
= (y
* bw
+ x
) * 4;
151 dst_p
[p
+ 0] = dst_p
[0];
152 dst_p
[p
+ 1] = dst_p
[1];
153 dst_p
[p
+ 2] = dst_p
[2];
154 dst_p
[p
+ 3] = dst_p
[3];
163 * Helper function to compress an image. The source is a 32-bpp RGBA image
164 * with stride==width.
167 compress_image(enum pipe_format format
, int datatype
,
168 const void *src
, uint8_t *dst
,
169 unsigned width
, unsigned height
, unsigned dst_stride
)
171 const struct util_format_description
*desc
= util_format_description(format
);
172 const uint src_stride
= 4 * width
;
174 if (datatype
== GL_FLOAT
)
175 desc
->pack_rgba_float(dst
, dst_stride
, (GLfloat
*)src
, src_stride
* sizeof(GLfloat
), width
, height
);
177 desc
->pack_rgba_8unorm(dst
, dst_stride
, (uint8_t *)src
, src_stride
, width
, height
);
182 * Software fallback for generate mipmap levels.
185 fallback_generate_mipmap(struct gl_context
*ctx
, GLenum target
,
186 struct gl_texture_object
*texObj
)
188 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
189 struct pipe_resource
*pt
= st_get_texobj_resource(texObj
);
190 const uint baseLevel
= texObj
->BaseLevel
;
191 const uint lastLevel
= pt
->last_level
;
192 const uint face
= _mesa_tex_target_to_face(target
);
196 GLboolean compressed
;
198 if (ST_DEBUG
& DEBUG_FALLBACK
)
199 debug_printf("%s: fallback processing\n", __FUNCTION__
);
201 assert(target
!= GL_TEXTURE_3D
); /* not done yet */
204 _mesa_is_format_compressed(texObj
->Image
[face
][baseLevel
]->TexFormat
);
207 if (texObj
->Image
[face
][baseLevel
]->TexFormat
== MESA_FORMAT_SIGNED_RED_RGTC1
||
208 texObj
->Image
[face
][baseLevel
]->TexFormat
== MESA_FORMAT_SIGNED_RG_RGTC2
)
211 datatype
= GL_UNSIGNED_BYTE
;
216 _mesa_format_to_type_and_comps(texObj
->Image
[face
][baseLevel
]->TexFormat
,
218 assert(comps
> 0 && "bad texture format in fallback_generate_mipmap()");
221 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
222 const uint srcLevel
= dstLevel
- 1;
223 const uint srcWidth
= u_minify(pt
->width0
, srcLevel
);
224 const uint srcHeight
= u_minify(pt
->height0
, srcLevel
);
225 const uint srcDepth
= u_minify(pt
->depth0
, srcLevel
);
226 const uint dstWidth
= u_minify(pt
->width0
, dstLevel
);
227 const uint dstHeight
= u_minify(pt
->height0
, dstLevel
);
228 const uint dstDepth
= u_minify(pt
->depth0
, dstLevel
);
229 struct pipe_transfer
*srcTrans
, *dstTrans
;
230 const ubyte
*srcData
;
232 int srcStride
, dstStride
;
234 srcTrans
= pipe_get_transfer(st_context(ctx
)->pipe
, pt
, srcLevel
,
236 PIPE_TRANSFER_READ
, 0, 0,
237 srcWidth
, srcHeight
);
239 dstTrans
= pipe_get_transfer(st_context(ctx
)->pipe
, pt
, dstLevel
,
241 PIPE_TRANSFER_WRITE
, 0, 0,
242 dstWidth
, dstHeight
);
244 srcData
= (ubyte
*) pipe_transfer_map(pipe
, srcTrans
);
245 dstData
= (ubyte
*) pipe_transfer_map(pipe
, dstTrans
);
247 srcStride
= srcTrans
->stride
/ util_format_get_blocksize(srcTrans
->resource
->format
);
248 dstStride
= dstTrans
->stride
/ util_format_get_blocksize(dstTrans
->resource
->format
);
250 /* this cannot work correctly for 3d since it does
251 not respect layerStride. */
253 const enum pipe_format format
= pt
->format
;
254 const uint bw
= util_format_get_blockwidth(format
);
255 const uint bh
= util_format_get_blockheight(format
);
256 const uint srcWidth2
= align(srcWidth
, bw
);
257 const uint srcHeight2
= align(srcHeight
, bh
);
258 const uint dstWidth2
= align(dstWidth
, bw
);
259 const uint dstHeight2
= align(dstHeight
, bh
);
260 uint8_t *srcTemp
, *dstTemp
;
264 srcTemp
= malloc(srcWidth2
* srcHeight2
* comps
* (datatype
== GL_FLOAT
? 4 : 1));
265 dstTemp
= malloc(dstWidth2
* dstHeight2
* comps
* (datatype
== GL_FLOAT
? 4 : 1));
267 /* decompress the src image: srcData -> srcTemp */
268 decompress_image(format
, datatype
, srcData
, srcTemp
, srcWidth2
, srcHeight2
, srcTrans
->stride
);
270 _mesa_generate_mipmap_level(target
, datatype
, comps
,
272 srcWidth2
, srcHeight2
, srcDepth
,
274 srcWidth2
, /* stride in texels */
275 dstWidth2
, dstHeight2
, dstDepth
,
277 dstWidth2
); /* stride in texels */
279 /* compress the new image: dstTemp -> dstData */
280 compress_image(format
, datatype
, dstTemp
, dstData
, dstWidth2
, dstHeight2
, dstTrans
->stride
);
286 _mesa_generate_mipmap_level(target
, datatype
, comps
,
288 srcWidth
, srcHeight
, srcDepth
,
290 srcStride
, /* stride in texels */
291 dstWidth
, dstHeight
, dstDepth
,
293 dstStride
); /* stride in texels */
296 pipe_transfer_unmap(pipe
, srcTrans
);
297 pipe_transfer_unmap(pipe
, dstTrans
);
299 pipe
->transfer_destroy(pipe
, srcTrans
);
300 pipe
->transfer_destroy(pipe
, dstTrans
);
306 * Compute the expected number of mipmap levels in the texture given
307 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
308 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
309 * levels should be generated.
312 compute_num_levels(struct gl_context
*ctx
,
313 struct gl_texture_object
*texObj
,
316 if (target
== GL_TEXTURE_RECTANGLE_ARB
) {
320 const struct gl_texture_image
*baseImage
=
321 _mesa_get_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
322 GLuint size
, numLevels
;
324 size
= MAX2(baseImage
->Width2
, baseImage
->Height2
);
325 size
= MAX2(size
, baseImage
->Depth2
);
327 numLevels
= texObj
->BaseLevel
;
334 numLevels
= MIN2(numLevels
, texObj
->MaxLevel
+ 1);
336 assert(numLevels
>= 1);
344 * Called via ctx->Driver.GenerateMipmap().
347 st_generate_mipmap(struct gl_context
*ctx
, GLenum target
,
348 struct gl_texture_object
*texObj
)
350 struct st_context
*st
= st_context(ctx
);
351 struct st_texture_object
*stObj
= st_texture_object(texObj
);
352 struct pipe_resource
*pt
= st_get_texobj_resource(texObj
);
353 const uint baseLevel
= texObj
->BaseLevel
;
360 /* not sure if this ultimately actually should work,
361 but we're not supporting multisampled textures yet. */
362 assert(pt
->nr_samples
< 2);
364 /* find expected last mipmap level to generate*/
365 lastLevel
= compute_num_levels(ctx
, texObj
, target
) - 1;
370 /* The texture isn't in a "complete" state yet so set the expected
371 * lastLevel here, since it won't get done in st_finalize_texture().
373 stObj
->lastLevel
= lastLevel
;
375 if (pt
->last_level
< lastLevel
) {
376 /* The current gallium texture doesn't have space for all the
377 * mipmap levels we need to generate. So allocate a new texture.
379 struct pipe_resource
*oldTex
= stObj
->pt
;
381 /* create new texture with space for more levels */
382 stObj
->pt
= st_texture_create(st
,
392 /* This will copy the old texture's base image into the new texture
393 * which we just allocated.
395 st_finalize_texture(ctx
, st
->pipe
, texObj
);
397 /* release the old tex (will likely be freed too) */
398 pipe_resource_reference(&oldTex
, NULL
);
399 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
402 /* Make sure that the base texture image data is present in the
405 st_finalize_texture(ctx
, st
->pipe
, texObj
);
410 assert(pt
->last_level
>= lastLevel
);
412 /* Try to generate the mipmap by rendering/texturing. If that fails,
413 * use the software fallback.
415 if (!st_render_mipmap(st
, target
, stObj
, baseLevel
, lastLevel
)) {
416 /* since the util code actually also has a fallback, should
417 probably make it never fail and kill this */
418 fallback_generate_mipmap(ctx
, target
, texObj
);
421 /* Fill in the Mesa gl_texture_image fields */
422 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
423 const uint srcLevel
= dstLevel
- 1;
424 const struct gl_texture_image
*srcImage
425 = _mesa_get_tex_image(ctx
, texObj
, target
, srcLevel
);
426 struct gl_texture_image
*dstImage
;
427 struct st_texture_image
*stImage
;
428 uint dstWidth
= u_minify(pt
->width0
, dstLevel
);
429 uint dstHeight
= u_minify(pt
->height0
, dstLevel
);
430 uint dstDepth
= u_minify(pt
->depth0
, dstLevel
);
431 uint border
= srcImage
->Border
;
433 dstImage
= _mesa_get_tex_image(ctx
, texObj
, target
, dstLevel
);
435 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
439 /* Free old image data */
441 ctx
->Driver
.FreeTexImageData(ctx
, dstImage
);
443 /* initialize new image */
444 _mesa_init_teximage_fields(ctx
, target
, dstImage
, dstWidth
, dstHeight
,
445 dstDepth
, border
, srcImage
->InternalFormat
,
446 srcImage
->TexFormat
);
448 stImage
= st_texture_image(dstImage
);
449 stImage
->level
= dstLevel
;
451 pipe_resource_reference(&stImage
->pt
, pt
);