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