1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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 * Compute the expected number of mipmap levels in the texture given
48 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
49 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
50 * levels should be generated.
53 compute_num_levels(struct gl_context
*ctx
,
54 struct gl_texture_object
*texObj
,
57 const struct gl_texture_image
*baseImage
;
60 baseImage
= _mesa_get_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
62 numLevels
= texObj
->BaseLevel
+ baseImage
->MaxNumLevels
;
63 numLevels
= MIN2(numLevels
, (GLuint
) texObj
->MaxLevel
+ 1);
64 if (texObj
->Immutable
)
65 numLevels
= MIN2(numLevels
, texObj
->NumLevels
);
66 assert(numLevels
>= 1);
73 * Called via ctx->Driver.GenerateMipmap().
76 st_generate_mipmap(struct gl_context
*ctx
, GLenum target
,
77 struct gl_texture_object
*texObj
)
79 struct st_context
*st
= st_context(ctx
);
80 struct st_texture_object
*stObj
= st_texture_object(texObj
);
81 struct pipe_resource
*pt
= st_get_texobj_resource(texObj
);
82 const uint baseLevel
= texObj
->BaseLevel
;
83 uint lastLevel
, first_layer
, last_layer
;
89 /* not sure if this ultimately actually should work,
90 but we're not supporting multisampled textures yet. */
91 assert(pt
->nr_samples
< 2);
93 /* find expected last mipmap level to generate*/
94 lastLevel
= compute_num_levels(ctx
, texObj
, target
) - 1;
99 /* The texture isn't in a "complete" state yet so set the expected
100 * lastLevel here, since it won't get done in st_finalize_texture().
102 stObj
->lastLevel
= lastLevel
;
104 if (!texObj
->Immutable
) {
105 if (pt
->last_level
< lastLevel
) {
106 /* The current gallium texture doesn't have space for all the
107 * mipmap levels we need to generate. So allocate a new texture.
109 struct pipe_resource
*oldTex
= stObj
->pt
;
111 /* create new texture with space for more levels */
112 stObj
->pt
= st_texture_create(st
,
123 /* This will copy the old texture's base image into the new texture
124 * which we just allocated.
126 st_finalize_texture(ctx
, st
->pipe
, texObj
);
128 /* release the old tex (will likely be freed too) */
129 pipe_resource_reference(&oldTex
, NULL
);
130 st_texture_release_all_sampler_views(st
, stObj
);
133 /* Make sure that the base texture image data is present in the
136 st_finalize_texture(ctx
, st
->pipe
, texObj
);
142 assert(pt
->last_level
>= lastLevel
);
144 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
145 first_layer
= last_layer
= _mesa_tex_target_to_face(target
);
149 last_layer
= util_max_layer(pt
, baseLevel
);
152 /* First see if the driver supports hardware mipmap generation,
153 * if not then generate the mipmap by rendering/texturing.
154 * If that fails, use the software fallback.
156 if (!st
->pipe
->screen
->get_param(st
->pipe
->screen
,
157 PIPE_CAP_GENERATE_MIPMAP
) ||
158 !st
->pipe
->generate_mipmap(st
->pipe
, pt
, pt
->format
, baseLevel
,
159 lastLevel
, first_layer
, last_layer
)) {
161 if (!util_gen_mipmap(st
->pipe
, pt
, pt
->format
, baseLevel
, lastLevel
,
162 first_layer
, last_layer
, PIPE_TEX_FILTER_LINEAR
)) {
163 _mesa_generate_mipmap(ctx
, target
, texObj
);
167 /* Fill in the Mesa gl_texture_image fields */
168 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
169 const uint srcLevel
= dstLevel
- 1;
170 const struct gl_texture_image
*srcImage
171 = _mesa_get_tex_image(ctx
, texObj
, target
, srcLevel
);
172 struct gl_texture_image
*dstImage
;
173 struct st_texture_image
*stImage
;
174 uint border
= srcImage
->Border
;
175 uint dstWidth
, dstHeight
, dstDepth
;
177 dstWidth
= u_minify(pt
->width0
, dstLevel
);
178 if (texObj
->Target
== GL_TEXTURE_1D_ARRAY
) {
179 dstHeight
= pt
->array_size
;
182 dstHeight
= u_minify(pt
->height0
, dstLevel
);
184 if (texObj
->Target
== GL_TEXTURE_2D_ARRAY
||
185 texObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
186 dstDepth
= pt
->array_size
;
189 dstDepth
= u_minify(pt
->depth0
, dstLevel
);
192 dstImage
= _mesa_get_tex_image(ctx
, texObj
, target
, dstLevel
);
194 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
198 /* Free old image data */
199 ctx
->Driver
.FreeTextureImageBuffer(ctx
, dstImage
);
201 /* initialize new image */
202 _mesa_init_teximage_fields(ctx
, dstImage
, dstWidth
, dstHeight
,
203 dstDepth
, border
, srcImage
->InternalFormat
,
204 srcImage
->TexFormat
);
206 stImage
= st_texture_image(dstImage
);
208 pipe_resource_reference(&stImage
->pt
, pt
);