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/macros.h"
31 #include "main/mipmap.h"
32 #include "main/teximage.h"
33 #include "main/texformat.h"
35 #include "shader/prog_instruction.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "util/u_inlines.h"
40 #include "util/u_format.h"
41 #include "util/u_gen_mipmap.h"
42 #include "util/u_math.h"
44 #include "cso_cache/cso_cache.h"
45 #include "cso_cache/cso_context.h"
48 #include "st_context.h"
49 #include "st_gen_mipmap.h"
50 #include "st_texture.h"
51 #include "st_cb_texture.h"
52 #include "st_inlines.h"
56 * one-time init for generate mipmap
57 * XXX Note: there may be other times we need no-op/simple state like this.
58 * In that case, some code refactoring would be good.
61 st_init_generate_mipmap(struct st_context *st)
63 st->gen_mipmap = util_create_gen_mipmap(st->pipe, st->cso_context);
68 st_destroy_generate_mipmap(struct st_context *st)
70 util_destroy_gen_mipmap(st->gen_mipmap);
71 st->gen_mipmap = NULL;
76 * Generate mipmap levels using hardware rendering.
77 * \return TRUE if successful, FALSE if not possible
80 st_render_mipmap(struct st_context *st,
82 struct st_texture_object *stObj,
83 uint baseLevel, uint lastLevel)
85 struct pipe_context *pipe = st->pipe;
86 struct pipe_screen *screen = pipe->screen;
87 struct pipe_sampler_view *psv = st_get_stobj_sampler_view(stObj);
88 const uint face = _mesa_tex_target_to_face(target);
90 assert(target != GL_TEXTURE_3D); /* not done yet */
92 /* check if we can render in the texture's format */
93 if (!screen->is_format_supported(screen, psv->format, psv->texture->target,
94 PIPE_BIND_RENDER_TARGET, 0)) {
98 util_gen_mipmap(st->gen_mipmap, psv, face, baseLevel, lastLevel,
99 PIPE_TEX_FILTER_LINEAR);
106 fallback_generate_mipmap(GLcontext *ctx, GLenum target,
107 struct gl_texture_object *texObj)
109 struct pipe_context *pipe = ctx->st->pipe;
110 struct pipe_resource *pt = st_get_texobj_texture(texObj);
111 const uint baseLevel = texObj->BaseLevel;
112 const uint lastLevel = pt->last_level;
113 const uint face = _mesa_tex_target_to_face(target), zslice = 0;
118 if (ST_DEBUG & DEBUG_FALLBACK)
119 debug_printf("%s: fallback processing\n", __FUNCTION__);
121 assert(target != GL_TEXTURE_3D); /* not done yet */
123 _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat,
126 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
127 const uint srcLevel = dstLevel - 1;
128 struct pipe_transfer *srcTrans, *dstTrans;
129 const ubyte *srcData;
131 int srcStride, dstStride;
133 srcTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
135 PIPE_TRANSFER_READ, 0, 0,
136 u_minify(pt->width0, srcLevel),
137 u_minify(pt->height0, srcLevel));
139 dstTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
141 PIPE_TRANSFER_WRITE, 0, 0,
142 u_minify(pt->width0, dstLevel),
143 u_minify(pt->height0, dstLevel));
145 srcData = (ubyte *) pipe_transfer_map(pipe, srcTrans);
146 dstData = (ubyte *) pipe_transfer_map(pipe, dstTrans);
148 srcStride = srcTrans->stride / util_format_get_blocksize(srcTrans->resource->format);
149 dstStride = dstTrans->stride / util_format_get_blocksize(dstTrans->resource->format);
151 _mesa_generate_mipmap_level(target, datatype, comps,
153 u_minify(pt->width0, srcLevel),
154 u_minify(pt->height0, srcLevel),
155 u_minify(pt->depth0, srcLevel),
157 srcStride, /* stride in texels */
158 u_minify(pt->width0, dstLevel),
159 u_minify(pt->height0, dstLevel),
160 u_minify(pt->depth0, dstLevel),
162 dstStride); /* stride in texels */
164 pipe_transfer_unmap(pipe, srcTrans);
165 pipe_transfer_unmap(pipe, dstTrans);
167 pipe->transfer_destroy(pipe, srcTrans);
168 pipe->transfer_destroy(pipe, dstTrans);
174 * Compute the expected number of mipmap levels in the texture given
175 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
176 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
177 * level should be generated.
180 compute_num_levels(GLcontext *ctx,
181 struct gl_texture_object *texObj,
184 if (target == GL_TEXTURE_RECTANGLE_ARB) {
188 const GLuint maxLevels = texObj->MaxLevel - texObj->BaseLevel + 1;
189 const struct gl_texture_image *baseImage =
190 _mesa_get_tex_image(ctx, texObj, target, texObj->BaseLevel);
191 GLuint size, numLevels;
193 size = MAX2(baseImage->Width2, baseImage->Height2);
194 size = MAX2(size, baseImage->Depth2);
203 numLevels = MIN2(numLevels, maxLevels);
211 st_generate_mipmap(GLcontext *ctx, GLenum target,
212 struct gl_texture_object *texObj)
214 struct st_context *st = ctx->st;
215 struct st_texture_object *stObj = st_texture_object(texObj);
216 struct pipe_resource *pt = st_get_texobj_texture(texObj);
217 const uint baseLevel = texObj->BaseLevel;
224 /* find expected last mipmap level */
225 lastLevel = compute_num_levels(ctx, texObj, target) - 1;
230 if (pt->last_level < lastLevel) {
231 /* The current gallium texture doesn't have space for all the
232 * mipmap levels we need to generate. So allocate a new texture.
234 struct pipe_resource *oldTex = stObj->pt;
237 /* create new texture with space for more levels */
238 stObj->pt = st_texture_create(st,
247 /* The texture isn't in a "complete" state yet so set the expected
248 * lastLevel here, since it won't get done in st_finalize_texture().
250 stObj->lastLevel = lastLevel;
252 /* This will copy the old texture's base image into the new texture
253 * which we just allocated.
255 st_finalize_texture(ctx, st->pipe, texObj, &needFlush);
257 /* release the old tex (will likely be freed too) */
258 pipe_resource_reference(&oldTex, NULL);
259 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
264 assert(lastLevel <= pt->last_level);
266 /* Recall that the Mesa BaseLevel image is stored in the gallium
267 * texture's level[0] position. So pass baseLevel=0 here.
269 if (!st_render_mipmap(st, target, stObj, 0, lastLevel)) {
270 fallback_generate_mipmap(ctx, target, texObj);
273 /* Fill in the Mesa gl_texture_image fields */
274 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
275 const uint srcLevel = dstLevel - 1;
276 const struct gl_texture_image *srcImage
277 = _mesa_get_tex_image(ctx, texObj, target, srcLevel);
278 struct gl_texture_image *dstImage;
279 struct st_texture_image *stImage;
280 uint dstWidth = u_minify(pt->width0, dstLevel);
281 uint dstHeight = u_minify(pt->height0, dstLevel);
282 uint dstDepth = u_minify(pt->depth0, dstLevel);
283 uint border = srcImage->Border;
285 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel);
287 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
291 /* Free old image data */
293 ctx->Driver.FreeTexImageData(ctx, dstImage);
295 /* initialize new image */
296 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
297 dstDepth, border, srcImage->InternalFormat);
299 dstImage->TexFormat = srcImage->TexFormat;
301 stImage = (struct st_texture_image *) dstImage;
302 pipe_resource_reference(&stImage->pt, pt);