8acf6160494b87056628af6ded6e3d6de8f70dbe
[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(psv->texture == stObj->pt);
83 assert(target != GL_TEXTURE_3D); /* not done yet */
84
85 /* check if we can render in the texture's format */
86 if (!screen->is_format_supported(screen, psv->format, psv->texture->target,
87 PIPE_BIND_RENDER_TARGET, 0)) {
88 return FALSE;
89 }
90
91 util_gen_mipmap(st->gen_mipmap, psv, face, baseLevel, lastLevel,
92 PIPE_TEX_FILTER_LINEAR);
93
94 return TRUE;
95 }
96
97
98 /**
99 * Helper function to decompress an image. The result is a 32-bpp RGBA
100 * image with stride==width.
101 */
102 static void
103 decompress_image(enum pipe_format format,
104 const uint8_t *src, uint8_t *dst,
105 unsigned width, unsigned height)
106 {
107 const struct util_format_description *desc = util_format_description(format);
108 const uint dst_stride = 4 * width;
109 const uint src_stride = util_format_get_stride(format, width);
110
111 desc->unpack_rgba_8unorm(dst, dst_stride, src, src_stride, width, height);
112 }
113
114
115 /**
116 * Helper function to compress an image. The source is a 32-bpp RGBA image
117 * with stride==width.
118 */
119 static void
120 compress_image(enum pipe_format format,
121 const uint8_t *src, uint8_t *dst,
122 unsigned width, unsigned height)
123 {
124 const struct util_format_description *desc = util_format_description(format);
125 const uint dst_stride = util_format_get_stride(format, width);
126 const uint src_stride = 4 * width;
127
128 desc->pack_rgba_8unorm(dst, dst_stride, src, src_stride, width, height);
129 }
130
131
132 /**
133 * Software fallback for generate mipmap levels.
134 */
135 static void
136 fallback_generate_mipmap(GLcontext *ctx, GLenum target,
137 struct gl_texture_object *texObj)
138 {
139 struct pipe_context *pipe = st_context(ctx)->pipe;
140 struct pipe_resource *pt = st_get_texobj_resource(texObj);
141 const uint baseLevel = texObj->BaseLevel;
142 const uint lastLevel = pt->last_level;
143 const uint face = _mesa_tex_target_to_face(target), zslice = 0;
144 uint dstLevel;
145 GLenum datatype;
146 GLuint comps;
147 GLboolean compressed;
148
149 if (ST_DEBUG & DEBUG_FALLBACK)
150 debug_printf("%s: fallback processing\n", __FUNCTION__);
151
152 assert(target != GL_TEXTURE_3D); /* not done yet */
153
154 compressed =
155 _mesa_is_format_compressed(texObj->Image[face][baseLevel]->TexFormat);
156
157 if (compressed) {
158 datatype = GL_UNSIGNED_BYTE;
159 comps = 4;
160 }
161 else {
162 _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat,
163 &datatype, &comps);
164 assert(comps > 0 && "bad texture format in fallback_generate_mipmap()");
165 }
166
167 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
168 const uint srcLevel = dstLevel - 1;
169 const uint srcWidth = u_minify(pt->width0, srcLevel);
170 const uint srcHeight = u_minify(pt->height0, srcLevel);
171 const uint srcDepth = u_minify(pt->depth0, srcLevel);
172 const uint dstWidth = u_minify(pt->width0, dstLevel);
173 const uint dstHeight = u_minify(pt->height0, dstLevel);
174 const uint dstDepth = u_minify(pt->depth0, dstLevel);
175 struct pipe_transfer *srcTrans, *dstTrans;
176 const ubyte *srcData;
177 ubyte *dstData;
178 int srcStride, dstStride;
179
180 srcTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
181 srcLevel, zslice,
182 PIPE_TRANSFER_READ, 0, 0,
183 srcWidth, srcHeight);
184
185
186 dstTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
187 dstLevel, zslice,
188 PIPE_TRANSFER_WRITE, 0, 0,
189 dstWidth, dstHeight);
190
191 srcData = (ubyte *) pipe_transfer_map(pipe, srcTrans);
192 dstData = (ubyte *) pipe_transfer_map(pipe, dstTrans);
193
194 srcStride = srcTrans->stride / util_format_get_blocksize(srcTrans->resource->format);
195 dstStride = dstTrans->stride / util_format_get_blocksize(dstTrans->resource->format);
196
197 if (compressed) {
198 const enum pipe_format format = pt->format;
199 const uint bw = util_format_get_blockwidth(format);
200 const uint bh = util_format_get_blockheight(format);
201 const uint srcWidth2 = align(srcWidth, bw);
202 const uint srcHeight2 = align(srcHeight, bh);
203 const uint dstWidth2 = align(dstWidth, bw);
204 const uint dstHeight2 = align(dstHeight, bh);
205 uint8_t *srcTemp, *dstTemp;
206
207 assert(comps == 4);
208
209 srcTemp = malloc(srcWidth2 * srcHeight2 * comps + 000);
210 dstTemp = malloc(dstWidth2 * dstHeight2 * comps + 000);
211
212 /* decompress the src image: srcData -> srcTemp */
213 decompress_image(format, srcData, srcTemp, srcWidth, srcHeight);
214
215 _mesa_generate_mipmap_level(target, datatype, comps,
216 0 /*border*/,
217 srcWidth2, srcHeight2, srcDepth,
218 srcTemp,
219 srcWidth2, /* stride in texels */
220 dstWidth2, dstHeight2, dstDepth,
221 dstTemp,
222 dstWidth2); /* stride in texels */
223
224 /* compress the new image: dstTemp -> dstData */
225 compress_image(format, dstTemp, dstData, dstWidth2, dstHeight2);
226
227 free(srcTemp);
228 free(dstTemp);
229 }
230 else {
231 _mesa_generate_mipmap_level(target, datatype, comps,
232 0 /*border*/,
233 srcWidth, srcHeight, srcDepth,
234 srcData,
235 srcStride, /* stride in texels */
236 dstWidth, dstHeight, dstDepth,
237 dstData,
238 dstStride); /* stride in texels */
239 }
240
241 pipe_transfer_unmap(pipe, srcTrans);
242 pipe_transfer_unmap(pipe, dstTrans);
243
244 pipe->transfer_destroy(pipe, srcTrans);
245 pipe->transfer_destroy(pipe, dstTrans);
246 }
247 }
248
249
250 /**
251 * Compute the expected number of mipmap levels in the texture given
252 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
253 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
254 * levels should be generated.
255 */
256 static GLuint
257 compute_num_levels(GLcontext *ctx,
258 struct gl_texture_object *texObj,
259 GLenum target)
260 {
261 if (target == GL_TEXTURE_RECTANGLE_ARB) {
262 return 1;
263 }
264 else {
265 const struct gl_texture_image *baseImage =
266 _mesa_get_tex_image(ctx, texObj, target, texObj->BaseLevel);
267 GLuint size, numLevels;
268
269 size = MAX2(baseImage->Width2, baseImage->Height2);
270 size = MAX2(size, baseImage->Depth2);
271
272 numLevels = texObj->BaseLevel;
273
274 while (size > 0) {
275 numLevels++;
276 size >>= 1;
277 }
278
279 numLevels = MIN2(numLevels, texObj->MaxLevel + 1);
280
281 assert(numLevels >= 1);
282
283 return numLevels;
284 }
285 }
286
287
288 /**
289 * Called via ctx->Driver.GenerateMipmap().
290 */
291 void
292 st_generate_mipmap(GLcontext *ctx, GLenum target,
293 struct gl_texture_object *texObj)
294 {
295 struct st_context *st = st_context(ctx);
296 struct st_texture_object *stObj = st_texture_object(texObj);
297 struct pipe_resource *pt = st_get_texobj_resource(texObj);
298 const uint baseLevel = texObj->BaseLevel;
299 uint lastLevel;
300 uint dstLevel;
301
302 if (!pt)
303 return;
304
305 /* find expected last mipmap level to generate */
306 lastLevel = compute_num_levels(ctx, texObj, target) - 1;
307
308 if (lastLevel == 0)
309 return;
310
311 if (pt->last_level < lastLevel) {
312 /* The current gallium texture doesn't have space for all the
313 * mipmap levels we need to generate. So allocate a new texture.
314 */
315 struct pipe_resource *oldTex = stObj->pt;
316
317 /* create new texture with space for more levels */
318 stObj->pt = st_texture_create(st,
319 oldTex->target,
320 oldTex->format,
321 lastLevel,
322 oldTex->width0,
323 oldTex->height0,
324 oldTex->depth0,
325 oldTex->bind);
326
327 /* The texture isn't in a "complete" state yet so set the expected
328 * lastLevel here, since it won't get done in st_finalize_texture().
329 */
330 stObj->lastLevel = lastLevel;
331
332 /* This will copy the old texture's base image into the new texture
333 * which we just allocated.
334 */
335 st_finalize_texture(ctx, st->pipe, texObj);
336
337 /* release the old tex (will likely be freed too) */
338 pipe_resource_reference(&oldTex, NULL);
339 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
340
341 pt = stObj->pt;
342 }
343
344 assert(pt->last_level >= lastLevel);
345
346 /* Try to generate the mipmap by rendering/texturing. If that fails,
347 * use the software fallback.
348 */
349 if (!st_render_mipmap(st, target, stObj, baseLevel, lastLevel)) {
350 fallback_generate_mipmap(ctx, target, texObj);
351 }
352
353 /* Fill in the Mesa gl_texture_image fields */
354 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
355 const uint srcLevel = dstLevel - 1;
356 const struct gl_texture_image *srcImage
357 = _mesa_get_tex_image(ctx, texObj, target, srcLevel);
358 struct gl_texture_image *dstImage;
359 struct st_texture_image *stImage;
360 uint dstWidth = u_minify(pt->width0, dstLevel);
361 uint dstHeight = u_minify(pt->height0, dstLevel);
362 uint dstDepth = u_minify(pt->depth0, dstLevel);
363 uint border = srcImage->Border;
364
365 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel);
366 if (!dstImage) {
367 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
368 return;
369 }
370
371 /* Free old image data */
372 if (dstImage->Data)
373 ctx->Driver.FreeTexImageData(ctx, dstImage);
374
375 /* initialize new image */
376 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
377 dstDepth, border, srcImage->InternalFormat);
378
379 dstImage->TexFormat = srcImage->TexFormat;
380
381 stImage = st_texture_image(dstImage);
382 stImage->level = dstLevel;
383
384 pipe_resource_reference(&stImage->pt, pt);
385 }
386 }