st/mesa: minor improvements in fallback_generate_mipmap()
[mesa.git] / src / mesa / state_tracker / st_gen_mipmap.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #include "main/imports.h"
30 #include "main/mipmap.h"
31 #include "main/teximage.h"
32 #include "main/texformat.h"
33
34 #include "pipe/p_context.h"
35 #include "pipe/p_defines.h"
36 #include "util/u_inlines.h"
37 #include "util/u_format.h"
38 #include "util/u_gen_mipmap.h"
39
40 #include "st_debug.h"
41 #include "st_context.h"
42 #include "st_gen_mipmap.h"
43 #include "st_cb_texture.h"
44 #include "st_inlines.h"
45
46
47 /**
48 * one-time init for generate mipmap
49 * XXX Note: there may be other times we need no-op/simple state like this.
50 * In that case, some code refactoring would be good.
51 */
52 void
53 st_init_generate_mipmap(struct st_context *st)
54 {
55 st->gen_mipmap = util_create_gen_mipmap(st->pipe, st->cso_context);
56 }
57
58
59 void
60 st_destroy_generate_mipmap(struct st_context *st)
61 {
62 util_destroy_gen_mipmap(st->gen_mipmap);
63 st->gen_mipmap = NULL;
64 }
65
66
67 /**
68 * Generate mipmap levels using hardware rendering.
69 * \return TRUE if successful, FALSE if not possible
70 */
71 static boolean
72 st_render_mipmap(struct st_context *st,
73 GLenum target,
74 struct st_texture_object *stObj,
75 uint baseLevel, uint lastLevel)
76 {
77 struct pipe_context *pipe = st->pipe;
78 struct pipe_screen *screen = pipe->screen;
79 struct pipe_sampler_view *psv = st_get_texture_sampler_view(stObj, pipe);
80 const uint face = _mesa_tex_target_to_face(target);
81
82 assert(target != GL_TEXTURE_3D); /* not done yet */
83
84 /* check if we can render in the texture's format */
85 if (!screen->is_format_supported(screen, psv->format, psv->texture->target,
86 PIPE_BIND_RENDER_TARGET, 0)) {
87 return FALSE;
88 }
89
90 util_gen_mipmap(st->gen_mipmap, psv, face, baseLevel, lastLevel,
91 PIPE_TEX_FILTER_LINEAR);
92
93 return TRUE;
94 }
95
96
97 /**
98 * Software fallback for generate mipmap levels.
99 */
100 static void
101 fallback_generate_mipmap(GLcontext *ctx, GLenum target,
102 struct gl_texture_object *texObj)
103 {
104 struct pipe_context *pipe = st_context(ctx)->pipe;
105 struct pipe_resource *pt = st_get_texobj_resource(texObj);
106 const uint baseLevel = texObj->BaseLevel;
107 const uint lastLevel = pt->last_level;
108 const uint face = _mesa_tex_target_to_face(target), zslice = 0;
109 uint dstLevel;
110 GLenum datatype;
111 GLuint comps;
112
113 if (ST_DEBUG & DEBUG_FALLBACK)
114 debug_printf("%s: fallback processing\n", __FUNCTION__);
115
116 assert(target != GL_TEXTURE_3D); /* not done yet */
117
118 _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat,
119 &datatype, &comps);
120
121 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
122 const uint srcLevel = dstLevel - 1;
123 const uint srcWidth = u_minify(pt->width0, srcLevel);
124 const uint srcHeight = u_minify(pt->height0, srcLevel);
125 const uint srcDepth = u_minify(pt->depth0, srcLevel);
126 const uint dstWidth = u_minify(pt->width0, dstLevel);
127 const uint dstHeight = u_minify(pt->height0, dstLevel);
128 const uint dstDepth = u_minify(pt->depth0, dstLevel);
129 struct pipe_transfer *srcTrans, *dstTrans;
130 const ubyte *srcData;
131 ubyte *dstData;
132 int srcStride, dstStride;
133
134 srcTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
135 srcLevel, zslice,
136 PIPE_TRANSFER_READ, 0, 0,
137 srcWidth, srcHeight);
138
139
140 dstTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
141 dstLevel, zslice,
142 PIPE_TRANSFER_WRITE, 0, 0,
143 dstWidth, dstHeight);
144
145 srcData = (ubyte *) pipe_transfer_map(pipe, srcTrans);
146 dstData = (ubyte *) pipe_transfer_map(pipe, dstTrans);
147
148 srcStride = srcTrans->stride / util_format_get_blocksize(srcTrans->resource->format);
149 dstStride = dstTrans->stride / util_format_get_blocksize(dstTrans->resource->format);
150
151 _mesa_generate_mipmap_level(target, datatype, comps,
152 0 /*border*/,
153 srcWidth, srcHeight, srcDepth,
154 srcData,
155 srcStride, /* stride in texels */
156 dstWidth, dstHeight, dstDepth,
157 dstData,
158 dstStride); /* stride in texels */
159
160 pipe_transfer_unmap(pipe, srcTrans);
161 pipe_transfer_unmap(pipe, dstTrans);
162
163 pipe->transfer_destroy(pipe, srcTrans);
164 pipe->transfer_destroy(pipe, dstTrans);
165 }
166 }
167
168
169 /**
170 * Compute the expected number of mipmap levels in the texture given
171 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
172 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
173 * levels should be generated.
174 */
175 static GLuint
176 compute_num_levels(GLcontext *ctx,
177 struct gl_texture_object *texObj,
178 GLenum target)
179 {
180 if (target == GL_TEXTURE_RECTANGLE_ARB) {
181 return 1;
182 }
183 else {
184 const GLuint maxLevels = texObj->MaxLevel - texObj->BaseLevel + 1;
185 const struct gl_texture_image *baseImage =
186 _mesa_get_tex_image(ctx, texObj, target, texObj->BaseLevel);
187 GLuint size, numLevels;
188
189 size = MAX2(baseImage->Width2, baseImage->Height2);
190 size = MAX2(size, baseImage->Depth2);
191
192 numLevels = 0;
193
194 while (size > 0) {
195 numLevels++;
196 size >>= 1;
197 }
198
199 numLevels = MIN2(numLevels, maxLevels);
200
201 return numLevels;
202 }
203 }
204
205
206 /**
207 * Called via ctx->Driver.GenerateMipmap().
208 */
209 void
210 st_generate_mipmap(GLcontext *ctx, GLenum target,
211 struct gl_texture_object *texObj)
212 {
213 struct st_context *st = st_context(ctx);
214 struct st_texture_object *stObj = st_texture_object(texObj);
215 struct pipe_resource *pt = st_get_texobj_resource(texObj);
216 const uint baseLevel = texObj->BaseLevel;
217 uint lastLevel;
218 uint dstLevel;
219
220 if (!pt)
221 return;
222
223 /* find expected last mipmap level */
224 lastLevel = compute_num_levels(ctx, texObj, target) - 1;
225
226 if (lastLevel == 0)
227 return;
228
229 if (pt->last_level < lastLevel) {
230 /* The current gallium texture doesn't have space for all the
231 * mipmap levels we need to generate. So allocate a new texture.
232 */
233 struct pipe_resource *oldTex = stObj->pt;
234 GLboolean needFlush;
235
236 /* create new texture with space for more levels */
237 stObj->pt = st_texture_create(st,
238 oldTex->target,
239 oldTex->format,
240 lastLevel,
241 oldTex->width0,
242 oldTex->height0,
243 oldTex->depth0,
244 oldTex->bind);
245
246 /* The texture isn't in a "complete" state yet so set the expected
247 * lastLevel here, since it won't get done in st_finalize_texture().
248 */
249 stObj->lastLevel = lastLevel;
250
251 /* This will copy the old texture's base image into the new texture
252 * which we just allocated.
253 */
254 st_finalize_texture(ctx, st->pipe, texObj, &needFlush);
255
256 /* release the old tex (will likely be freed too) */
257 pipe_resource_reference(&oldTex, NULL);
258 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
259
260 pt = stObj->pt;
261 }
262
263 assert(lastLevel <= pt->last_level);
264
265 /* Recall that the Mesa BaseLevel image is stored in the gallium
266 * texture's level[0] position. So pass baseLevel=0 here.
267 */
268 if (!st_render_mipmap(st, target, stObj, 0, lastLevel)) {
269 fallback_generate_mipmap(ctx, target, texObj);
270 }
271
272 /* Fill in the Mesa gl_texture_image fields */
273 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
274 const uint srcLevel = dstLevel - 1;
275 const struct gl_texture_image *srcImage
276 = _mesa_get_tex_image(ctx, texObj, target, srcLevel);
277 struct gl_texture_image *dstImage;
278 struct st_texture_image *stImage;
279 uint dstWidth = u_minify(pt->width0, dstLevel);
280 uint dstHeight = u_minify(pt->height0, dstLevel);
281 uint dstDepth = u_minify(pt->depth0, dstLevel);
282 uint border = srcImage->Border;
283
284 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel);
285 if (!dstImage) {
286 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
287 return;
288 }
289
290 /* Free old image data */
291 if (dstImage->Data)
292 ctx->Driver.FreeTexImageData(ctx, dstImage);
293
294 /* initialize new image */
295 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
296 dstDepth, border, srcImage->InternalFormat);
297
298 dstImage->TexFormat = srcImage->TexFormat;
299
300 stImage = (struct st_texture_image *) dstImage;
301 pipe_resource_reference(&stImage->pt, pt);
302 }
303 }