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