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
);
82 assert(target
!= GL_TEXTURE_3D
); /* not done yet */
84 /* check if we can render in the texture's format */
85 if (!screen
->is_format_supported(screen
, psv
->format
, psv
->texture
->target
, 0,
86 PIPE_BIND_RENDER_TARGET
, 0)) {
90 util_gen_mipmap(st
->gen_mipmap
, psv
, face
, baseLevel
, lastLevel
,
91 PIPE_TEX_FILTER_LINEAR
);
98 * Helper function to decompress an image. The result is a 32-bpp RGBA
99 * image with stride==width.
102 decompress_image(enum pipe_format format
,
103 const uint8_t *src
, uint8_t *dst
,
104 unsigned width
, unsigned height
)
106 const struct util_format_description
*desc
= util_format_description(format
);
107 const uint bw
= util_format_get_blockwidth(format
);
108 const uint bh
= util_format_get_blockheight(format
);
109 const uint dst_stride
= 4 * MAX2(width
, bw
);
110 const uint src_stride
= util_format_get_stride(format
, width
);
112 desc
->unpack_rgba_8unorm(dst
, dst_stride
, src
, src_stride
, width
, height
);
114 if (width
< bw
|| height
< bh
) {
115 /* We're decompressing an image smaller than the compression
116 * block size. We don't want garbage pixel values in the region
117 * outside (width x height) so replicate pixels from the (width
118 * x height) region to fill out the (bw x bh) block size.
121 for (y
= 0; y
< bh
; y
++) {
122 for (x
= 0; x
< bw
; x
++) {
123 if (x
>= width
|| y
>= height
) {
124 uint p
= (y
* bw
+ x
) * 4;
137 * Helper function to compress an image. The source is a 32-bpp RGBA image
138 * with stride==width.
141 compress_image(enum pipe_format format
,
142 const uint8_t *src
, uint8_t *dst
,
143 unsigned width
, unsigned height
)
145 const struct util_format_description
*desc
= util_format_description(format
);
146 const uint dst_stride
= util_format_get_stride(format
, width
);
147 const uint src_stride
= 4 * width
;
149 desc
->pack_rgba_8unorm(dst
, dst_stride
, src
, src_stride
, width
, height
);
154 * Software fallback for generate mipmap levels.
157 fallback_generate_mipmap(struct gl_context
*ctx
, GLenum target
,
158 struct gl_texture_object
*texObj
)
160 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
161 struct pipe_resource
*pt
= st_get_texobj_resource(texObj
);
162 const uint baseLevel
= texObj
->BaseLevel
;
163 const uint lastLevel
= pt
->last_level
;
164 const uint face
= _mesa_tex_target_to_face(target
), zslice
= 0;
168 GLboolean compressed
;
170 if (ST_DEBUG
& DEBUG_FALLBACK
)
171 debug_printf("%s: fallback processing\n", __FUNCTION__
);
173 assert(target
!= GL_TEXTURE_3D
); /* not done yet */
176 _mesa_is_format_compressed(texObj
->Image
[face
][baseLevel
]->TexFormat
);
179 datatype
= GL_UNSIGNED_BYTE
;
183 _mesa_format_to_type_and_comps(texObj
->Image
[face
][baseLevel
]->TexFormat
,
185 assert(comps
> 0 && "bad texture format in fallback_generate_mipmap()");
188 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
189 const uint srcLevel
= dstLevel
- 1;
190 const uint srcWidth
= u_minify(pt
->width0
, srcLevel
);
191 const uint srcHeight
= u_minify(pt
->height0
, srcLevel
);
192 const uint srcDepth
= u_minify(pt
->depth0
, srcLevel
);
193 const uint dstWidth
= u_minify(pt
->width0
, dstLevel
);
194 const uint dstHeight
= u_minify(pt
->height0
, dstLevel
);
195 const uint dstDepth
= u_minify(pt
->depth0
, dstLevel
);
196 struct pipe_transfer
*srcTrans
, *dstTrans
;
197 const ubyte
*srcData
;
199 int srcStride
, dstStride
;
201 srcTrans
= pipe_get_transfer(st_context(ctx
)->pipe
, pt
, face
,
203 PIPE_TRANSFER_READ
, 0, 0,
204 srcWidth
, srcHeight
);
207 dstTrans
= pipe_get_transfer(st_context(ctx
)->pipe
, pt
, face
,
209 PIPE_TRANSFER_WRITE
, 0, 0,
210 dstWidth
, dstHeight
);
212 srcData
= (ubyte
*) pipe_transfer_map(pipe
, srcTrans
);
213 dstData
= (ubyte
*) pipe_transfer_map(pipe
, dstTrans
);
215 srcStride
= srcTrans
->stride
/ util_format_get_blocksize(srcTrans
->resource
->format
);
216 dstStride
= dstTrans
->stride
/ util_format_get_blocksize(dstTrans
->resource
->format
);
219 const enum pipe_format format
= pt
->format
;
220 const uint bw
= util_format_get_blockwidth(format
);
221 const uint bh
= util_format_get_blockheight(format
);
222 const uint srcWidth2
= align(srcWidth
, bw
);
223 const uint srcHeight2
= align(srcHeight
, bh
);
224 const uint dstWidth2
= align(dstWidth
, bw
);
225 const uint dstHeight2
= align(dstHeight
, bh
);
226 uint8_t *srcTemp
, *dstTemp
;
230 srcTemp
= malloc(srcWidth2
* srcHeight2
* comps
+ 000);
231 dstTemp
= malloc(dstWidth2
* dstHeight2
* comps
+ 000);
233 /* decompress the src image: srcData -> srcTemp */
234 decompress_image(format
, srcData
, srcTemp
, srcWidth
, srcHeight
);
236 _mesa_generate_mipmap_level(target
, datatype
, comps
,
238 srcWidth2
, srcHeight2
, srcDepth
,
240 srcWidth2
, /* stride in texels */
241 dstWidth2
, dstHeight2
, dstDepth
,
243 dstWidth2
); /* stride in texels */
245 /* compress the new image: dstTemp -> dstData */
246 compress_image(format
, dstTemp
, dstData
, dstWidth
, dstHeight
);
252 _mesa_generate_mipmap_level(target
, datatype
, comps
,
254 srcWidth
, srcHeight
, srcDepth
,
256 srcStride
, /* stride in texels */
257 dstWidth
, dstHeight
, dstDepth
,
259 dstStride
); /* stride in texels */
262 pipe_transfer_unmap(pipe
, srcTrans
);
263 pipe_transfer_unmap(pipe
, dstTrans
);
265 pipe
->transfer_destroy(pipe
, srcTrans
);
266 pipe
->transfer_destroy(pipe
, dstTrans
);
272 * Compute the expected number of mipmap levels in the texture given
273 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
274 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
275 * levels should be generated.
278 compute_num_levels(struct gl_context
*ctx
,
279 struct gl_texture_object
*texObj
,
282 if (target
== GL_TEXTURE_RECTANGLE_ARB
) {
286 const struct gl_texture_image
*baseImage
=
287 _mesa_get_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
288 GLuint size
, numLevels
;
290 size
= MAX2(baseImage
->Width2
, baseImage
->Height2
);
291 size
= MAX2(size
, baseImage
->Depth2
);
293 numLevels
= texObj
->BaseLevel
;
300 numLevels
= MIN2(numLevels
, texObj
->MaxLevel
+ 1);
302 assert(numLevels
>= 1);
310 * Called via ctx->Driver.GenerateMipmap().
313 st_generate_mipmap(struct gl_context
*ctx
, GLenum target
,
314 struct gl_texture_object
*texObj
)
316 struct st_context
*st
= st_context(ctx
);
317 struct st_texture_object
*stObj
= st_texture_object(texObj
);
318 struct pipe_resource
*pt
= st_get_texobj_resource(texObj
);
319 const uint baseLevel
= texObj
->BaseLevel
;
326 /* not sure if this ultimately actually should work,
327 but we're not supporting multisampled textures yet. */
328 assert(pt
->nr_samples
< 2);
330 /* find expected last mipmap level to generate*/
331 lastLevel
= compute_num_levels(ctx
, texObj
, target
) - 1;
336 if (pt
->last_level
< lastLevel
) {
337 /* The current gallium texture doesn't have space for all the
338 * mipmap levels we need to generate. So allocate a new texture.
340 struct pipe_resource
*oldTex
= stObj
->pt
;
342 /* create new texture with space for more levels */
343 stObj
->pt
= st_texture_create(st
,
352 /* The texture isn't in a "complete" state yet so set the expected
353 * lastLevel here, since it won't get done in st_finalize_texture().
355 stObj
->lastLevel
= lastLevel
;
357 /* This will copy the old texture's base image into the new texture
358 * which we just allocated.
360 st_finalize_texture(ctx
, st
->pipe
, texObj
);
362 /* release the old tex (will likely be freed too) */
363 pipe_resource_reference(&oldTex
, NULL
);
364 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
369 assert(pt
->last_level
>= lastLevel
);
371 /* Try to generate the mipmap by rendering/texturing. If that fails,
372 * use the software fallback.
374 if (!st_render_mipmap(st
, target
, stObj
, baseLevel
, lastLevel
)) {
375 fallback_generate_mipmap(ctx
, target
, texObj
);
378 /* Fill in the Mesa gl_texture_image fields */
379 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
380 const uint srcLevel
= dstLevel
- 1;
381 const struct gl_texture_image
*srcImage
382 = _mesa_get_tex_image(ctx
, texObj
, target
, srcLevel
);
383 struct gl_texture_image
*dstImage
;
384 struct st_texture_image
*stImage
;
385 uint dstWidth
= u_minify(pt
->width0
, dstLevel
);
386 uint dstHeight
= u_minify(pt
->height0
, dstLevel
);
387 uint dstDepth
= u_minify(pt
->depth0
, dstLevel
);
388 uint border
= srcImage
->Border
;
390 dstImage
= _mesa_get_tex_image(ctx
, texObj
, target
, dstLevel
);
392 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
396 /* Free old image data */
398 ctx
->Driver
.FreeTexImageData(ctx
, dstImage
);
400 /* initialize new image */
401 _mesa_init_teximage_fields(ctx
, target
, dstImage
, dstWidth
, dstHeight
,
402 dstDepth
, border
, srcImage
->InternalFormat
,
403 srcImage
->TexFormat
);
405 stImage
= st_texture_image(dstImage
);
406 stImage
->level
= dstLevel
;
408 pipe_resource_reference(&stImage
->pt
, pt
);