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
;
79 const uint face
= _mesa_tex_target_to_face(target
);
82 assert(target
!= GL_TEXTURE_3D
); /* implemented but untested */
85 /* check if we can render in the texture's format */
86 /* XXX should probably kill this and always use util_gen_mipmap
87 since this implements a sw fallback as well */
88 if (!screen
->is_format_supported(screen
, stObj
->pt
->format
,
90 0, PIPE_BIND_RENDER_TARGET
)) {
94 psv
= st_create_texture_sampler_view(pipe
, stObj
->pt
);
96 util_gen_mipmap(st
->gen_mipmap
, psv
, face
, baseLevel
, lastLevel
,
97 PIPE_TEX_FILTER_LINEAR
);
99 pipe_sampler_view_reference(&psv
, NULL
);
105 * Compute the expected number of mipmap levels in the texture given
106 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
107 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
108 * levels should be generated.
111 compute_num_levels(struct gl_context
*ctx
,
112 struct gl_texture_object
*texObj
,
115 const struct gl_texture_image
*baseImage
;
118 baseImage
= _mesa_get_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
120 numLevels
= texObj
->BaseLevel
+ baseImage
->MaxNumLevels
;
121 numLevels
= MIN2(numLevels
, texObj
->MaxLevel
+ 1);
122 assert(numLevels
>= 1);
129 * Called via ctx->Driver.GenerateMipmap().
132 st_generate_mipmap(struct gl_context
*ctx
, GLenum target
,
133 struct gl_texture_object
*texObj
)
135 struct st_context
*st
= st_context(ctx
);
136 struct st_texture_object
*stObj
= st_texture_object(texObj
);
137 struct pipe_resource
*pt
= st_get_texobj_resource(texObj
);
138 const uint baseLevel
= texObj
->BaseLevel
;
145 /* not sure if this ultimately actually should work,
146 but we're not supporting multisampled textures yet. */
147 assert(pt
->nr_samples
< 2);
149 /* find expected last mipmap level to generate*/
150 lastLevel
= compute_num_levels(ctx
, texObj
, target
) - 1;
155 /* The texture isn't in a "complete" state yet so set the expected
156 * lastLevel here, since it won't get done in st_finalize_texture().
158 stObj
->lastLevel
= lastLevel
;
160 if (pt
->last_level
< lastLevel
) {
161 /* The current gallium texture doesn't have space for all the
162 * mipmap levels we need to generate. So allocate a new texture.
164 struct pipe_resource
*oldTex
= stObj
->pt
;
166 /* create new texture with space for more levels */
167 stObj
->pt
= st_texture_create(st
,
177 /* This will copy the old texture's base image into the new texture
178 * which we just allocated.
180 st_finalize_texture(ctx
, st
->pipe
, texObj
);
182 /* release the old tex (will likely be freed too) */
183 pipe_resource_reference(&oldTex
, NULL
);
184 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
187 /* Make sure that the base texture image data is present in the
190 st_finalize_texture(ctx
, st
->pipe
, texObj
);
195 assert(pt
->last_level
>= lastLevel
);
197 /* Try to generate the mipmap by rendering/texturing. If that fails,
198 * use the software fallback.
200 if (!st_render_mipmap(st
, target
, stObj
, baseLevel
, lastLevel
)) {
201 /* since the util code actually also has a fallback, should
202 probably make it never fail and kill this */
203 _mesa_generate_mipmap(ctx
, target
, texObj
);
206 /* Fill in the Mesa gl_texture_image fields */
207 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
208 const uint srcLevel
= dstLevel
- 1;
209 const struct gl_texture_image
*srcImage
210 = _mesa_get_tex_image(ctx
, texObj
, target
, srcLevel
);
211 struct gl_texture_image
*dstImage
;
212 struct st_texture_image
*stImage
;
213 uint border
= srcImage
->Border
;
214 uint dstWidth
, dstHeight
, dstDepth
;
216 dstWidth
= u_minify(pt
->width0
, dstLevel
);
217 if (texObj
->Target
== GL_TEXTURE_1D_ARRAY
) {
218 dstHeight
= pt
->array_size
;
221 dstHeight
= u_minify(pt
->height0
, dstLevel
);
223 if (texObj
->Target
== GL_TEXTURE_2D_ARRAY
) {
224 dstDepth
= pt
->array_size
;
227 dstDepth
= u_minify(pt
->depth0
, dstLevel
);
230 dstImage
= _mesa_get_tex_image(ctx
, texObj
, target
, dstLevel
);
232 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
236 /* Free old image data */
237 ctx
->Driver
.FreeTextureImageBuffer(ctx
, dstImage
);
239 /* initialize new image */
240 _mesa_init_teximage_fields(ctx
, dstImage
, dstWidth
, dstHeight
,
241 dstDepth
, border
, srcImage
->InternalFormat
,
242 srcImage
->TexFormat
);
244 stImage
= st_texture_image(dstImage
);
246 pipe_resource_reference(&stImage
->pt
, pt
);