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