Merge remote branch 'origin/master' into nvc0-new
[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
33 #include "pipe/p_context.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_inlines.h"
36 #include "util/u_format.h"
37 #include "util/u_gen_mipmap.h"
38
39 #include "st_debug.h"
40 #include "st_context.h"
41 #include "st_texture.h"
42 #include "st_gen_mipmap.h"
43 #include "st_cb_texture.h"
44
45
46 /**
47 * one-time init for generate mipmap
48 * XXX Note: there may be other times we need no-op/simple state like this.
49 * In that case, some code refactoring would be good.
50 */
51 void
52 st_init_generate_mipmap(struct st_context *st)
53 {
54 st->gen_mipmap = util_create_gen_mipmap(st->pipe, st->cso_context);
55 }
56
57
58 void
59 st_destroy_generate_mipmap(struct st_context *st)
60 {
61 util_destroy_gen_mipmap(st->gen_mipmap);
62 st->gen_mipmap = NULL;
63 }
64
65
66 /**
67 * Generate mipmap levels using hardware rendering.
68 * \return TRUE if successful, FALSE if not possible
69 */
70 static boolean
71 st_render_mipmap(struct st_context *st,
72 GLenum target,
73 struct st_texture_object *stObj,
74 uint baseLevel, uint lastLevel)
75 {
76 struct pipe_context *pipe = st->pipe;
77 struct pipe_screen *screen = pipe->screen;
78 struct pipe_sampler_view *psv = st_get_texture_sampler_view(stObj, pipe);
79 const uint face = _mesa_tex_target_to_face(target);
80
81 assert(psv->texture == stObj->pt);
82 #if 0
83 assert(target != GL_TEXTURE_3D); /* implemented but untested */
84 #endif
85
86 /* check if we can render in the texture's format */
87 /* XXX should probably kill this and always use util_gen_mipmap
88 since this implements a sw fallback as well */
89 if (!screen->is_format_supported(screen, psv->format, psv->texture->target,
90 0, PIPE_BIND_RENDER_TARGET, 0)) {
91 return FALSE;
92 }
93
94 util_gen_mipmap(st->gen_mipmap, psv, face, baseLevel, lastLevel,
95 PIPE_TEX_FILTER_LINEAR);
96
97 return TRUE;
98 }
99
100
101 /**
102 * Helper function to decompress an image. The result is a 32-bpp RGBA
103 * image with stride==width.
104 */
105 static void
106 decompress_image(enum pipe_format format,
107 const uint8_t *src, uint8_t *dst,
108 unsigned width, unsigned height)
109 {
110 const struct util_format_description *desc = util_format_description(format);
111 const uint bw = util_format_get_blockwidth(format);
112 const uint bh = util_format_get_blockheight(format);
113 const uint dst_stride = 4 * MAX2(width, bw);
114 const uint src_stride = util_format_get_stride(format, width);
115
116 desc->unpack_rgba_8unorm(dst, dst_stride, src, src_stride, width, height);
117
118 if (width < bw || height < bh) {
119 /* We're decompressing an image smaller than the compression
120 * block size. We don't want garbage pixel values in the region
121 * outside (width x height) so replicate pixels from the (width
122 * x height) region to fill out the (bw x bh) block size.
123 */
124 uint x, y;
125 for (y = 0; y < bh; y++) {
126 for (x = 0; x < bw; x++) {
127 if (x >= width || y >= height) {
128 uint p = (y * bw + x) * 4;
129 dst[p + 0] = dst[0];
130 dst[p + 1] = dst[1];
131 dst[p + 2] = dst[2];
132 dst[p + 3] = dst[3];
133 }
134 }
135 }
136 }
137 }
138
139
140 /**
141 * Helper function to compress an image. The source is a 32-bpp RGBA image
142 * with stride==width.
143 */
144 static void
145 compress_image(enum pipe_format format,
146 const uint8_t *src, uint8_t *dst,
147 unsigned width, unsigned height)
148 {
149 const struct util_format_description *desc = util_format_description(format);
150 const uint dst_stride = util_format_get_stride(format, width);
151 const uint src_stride = 4 * width;
152
153 desc->pack_rgba_8unorm(dst, dst_stride, src, src_stride, width, height);
154 }
155
156
157 /**
158 * Software fallback for generate mipmap levels.
159 */
160 static void
161 fallback_generate_mipmap(struct gl_context *ctx, GLenum target,
162 struct gl_texture_object *texObj)
163 {
164 struct pipe_context *pipe = st_context(ctx)->pipe;
165 struct pipe_resource *pt = st_get_texobj_resource(texObj);
166 const uint baseLevel = texObj->BaseLevel;
167 const uint lastLevel = pt->last_level;
168 const uint face = _mesa_tex_target_to_face(target);
169 uint dstLevel;
170 GLenum datatype;
171 GLuint comps;
172 GLboolean compressed;
173
174 if (ST_DEBUG & DEBUG_FALLBACK)
175 debug_printf("%s: fallback processing\n", __FUNCTION__);
176
177 assert(target != GL_TEXTURE_3D); /* not done yet */
178
179 compressed =
180 _mesa_is_format_compressed(texObj->Image[face][baseLevel]->TexFormat);
181
182 if (compressed) {
183 datatype = GL_UNSIGNED_BYTE;
184 comps = 4;
185 }
186 else {
187 _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat,
188 &datatype, &comps);
189 assert(comps > 0 && "bad texture format in fallback_generate_mipmap()");
190 }
191
192 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
193 const uint srcLevel = dstLevel - 1;
194 const uint srcWidth = u_minify(pt->width0, srcLevel);
195 const uint srcHeight = u_minify(pt->height0, srcLevel);
196 const uint srcDepth = u_minify(pt->depth0, srcLevel);
197 const uint dstWidth = u_minify(pt->width0, dstLevel);
198 const uint dstHeight = u_minify(pt->height0, dstLevel);
199 const uint dstDepth = u_minify(pt->depth0, dstLevel);
200 struct pipe_transfer *srcTrans, *dstTrans;
201 const ubyte *srcData;
202 ubyte *dstData;
203 int srcStride, dstStride;
204
205 srcTrans = pipe_get_transfer(st_context(ctx)->pipe, pt, srcLevel,
206 face,
207 PIPE_TRANSFER_READ, 0, 0,
208 srcWidth, srcHeight);
209
210 dstTrans = pipe_get_transfer(st_context(ctx)->pipe, pt, dstLevel,
211 face,
212 PIPE_TRANSFER_WRITE, 0, 0,
213 dstWidth, dstHeight);
214
215 srcData = (ubyte *) pipe_transfer_map(pipe, srcTrans);
216 dstData = (ubyte *) pipe_transfer_map(pipe, dstTrans);
217
218 srcStride = srcTrans->stride / util_format_get_blocksize(srcTrans->resource->format);
219 dstStride = dstTrans->stride / util_format_get_blocksize(dstTrans->resource->format);
220
221 /* this cannot work correctly for 3d since it does
222 not respect layerStride. */
223 if (compressed) {
224 const enum pipe_format format = pt->format;
225 const uint bw = util_format_get_blockwidth(format);
226 const uint bh = util_format_get_blockheight(format);
227 const uint srcWidth2 = align(srcWidth, bw);
228 const uint srcHeight2 = align(srcHeight, bh);
229 const uint dstWidth2 = align(dstWidth, bw);
230 const uint dstHeight2 = align(dstHeight, bh);
231 uint8_t *srcTemp, *dstTemp;
232
233 assert(comps == 4);
234
235 srcTemp = malloc(srcWidth2 * srcHeight2 * comps + 000);
236 dstTemp = malloc(dstWidth2 * dstHeight2 * comps + 000);
237
238 /* decompress the src image: srcData -> srcTemp */
239 decompress_image(format, srcData, srcTemp, srcWidth, srcHeight);
240
241 _mesa_generate_mipmap_level(target, datatype, comps,
242 0 /*border*/,
243 srcWidth2, srcHeight2, srcDepth,
244 srcTemp,
245 srcWidth2, /* stride in texels */
246 dstWidth2, dstHeight2, dstDepth,
247 dstTemp,
248 dstWidth2); /* stride in texels */
249
250 /* compress the new image: dstTemp -> dstData */
251 compress_image(format, dstTemp, dstData, dstWidth, dstHeight);
252
253 free(srcTemp);
254 free(dstTemp);
255 }
256 else {
257 _mesa_generate_mipmap_level(target, datatype, comps,
258 0 /*border*/,
259 srcWidth, srcHeight, srcDepth,
260 srcData,
261 srcStride, /* stride in texels */
262 dstWidth, dstHeight, dstDepth,
263 dstData,
264 dstStride); /* stride in texels */
265 }
266
267 pipe_transfer_unmap(pipe, srcTrans);
268 pipe_transfer_unmap(pipe, dstTrans);
269
270 pipe->transfer_destroy(pipe, srcTrans);
271 pipe->transfer_destroy(pipe, dstTrans);
272 }
273 }
274
275
276 /**
277 * Compute the expected number of mipmap levels in the texture given
278 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
279 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
280 * levels should be generated.
281 */
282 static GLuint
283 compute_num_levels(struct gl_context *ctx,
284 struct gl_texture_object *texObj,
285 GLenum target)
286 {
287 if (target == GL_TEXTURE_RECTANGLE_ARB) {
288 return 1;
289 }
290 else {
291 const struct gl_texture_image *baseImage =
292 _mesa_get_tex_image(ctx, texObj, target, texObj->BaseLevel);
293 GLuint size, numLevels;
294
295 size = MAX2(baseImage->Width2, baseImage->Height2);
296 size = MAX2(size, baseImage->Depth2);
297
298 numLevels = texObj->BaseLevel;
299
300 while (size > 0) {
301 numLevels++;
302 size >>= 1;
303 }
304
305 numLevels = MIN2(numLevels, texObj->MaxLevel + 1);
306
307 assert(numLevels >= 1);
308
309 return numLevels;
310 }
311 }
312
313
314 /**
315 * Called via ctx->Driver.GenerateMipmap().
316 */
317 void
318 st_generate_mipmap(struct gl_context *ctx, GLenum target,
319 struct gl_texture_object *texObj)
320 {
321 struct st_context *st = st_context(ctx);
322 struct st_texture_object *stObj = st_texture_object(texObj);
323 struct pipe_resource *pt = st_get_texobj_resource(texObj);
324 const uint baseLevel = texObj->BaseLevel;
325 uint lastLevel;
326 uint dstLevel;
327
328 if (!pt)
329 return;
330
331 /* not sure if this ultimately actually should work,
332 but we're not supporting multisampled textures yet. */
333 assert(pt->nr_samples < 2);
334
335 /* find expected last mipmap level to generate*/
336 lastLevel = compute_num_levels(ctx, texObj, target) - 1;
337
338 if (lastLevel == 0)
339 return;
340
341 if (pt->last_level < lastLevel) {
342 /* The current gallium texture doesn't have space for all the
343 * mipmap levels we need to generate. So allocate a new texture.
344 */
345 struct pipe_resource *oldTex = stObj->pt;
346
347 /* create new texture with space for more levels */
348 stObj->pt = st_texture_create(st,
349 oldTex->target,
350 oldTex->format,
351 lastLevel,
352 oldTex->width0,
353 oldTex->height0,
354 oldTex->depth0,
355 oldTex->bind);
356
357 /* The texture isn't in a "complete" state yet so set the expected
358 * lastLevel here, since it won't get done in st_finalize_texture().
359 */
360 stObj->lastLevel = lastLevel;
361
362 /* This will copy the old texture's base image into the new texture
363 * which we just allocated.
364 */
365 st_finalize_texture(ctx, st->pipe, texObj);
366
367 /* release the old tex (will likely be freed too) */
368 pipe_resource_reference(&oldTex, NULL);
369 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
370
371 pt = stObj->pt;
372 }
373 else {
374 /* Make sure that the base texture image data is present in the
375 * texture buffer.
376 */
377 st_finalize_texture(ctx, st->pipe, texObj);
378 }
379
380 assert(pt->last_level >= lastLevel);
381
382 /* Try to generate the mipmap by rendering/texturing. If that fails,
383 * use the software fallback.
384 */
385 if (!st_render_mipmap(st, target, stObj, baseLevel, lastLevel)) {
386 /* since the util code actually also has a fallback, should
387 probably make it never fail and kill this */
388 fallback_generate_mipmap(ctx, target, texObj);
389 }
390
391 /* Fill in the Mesa gl_texture_image fields */
392 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
393 const uint srcLevel = dstLevel - 1;
394 const struct gl_texture_image *srcImage
395 = _mesa_get_tex_image(ctx, texObj, target, srcLevel);
396 struct gl_texture_image *dstImage;
397 struct st_texture_image *stImage;
398 uint dstWidth = u_minify(pt->width0, dstLevel);
399 uint dstHeight = u_minify(pt->height0, dstLevel);
400 uint dstDepth = u_minify(pt->depth0, dstLevel);
401 uint border = srcImage->Border;
402
403 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel);
404 if (!dstImage) {
405 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
406 return;
407 }
408
409 /* Free old image data */
410 if (dstImage->Data)
411 ctx->Driver.FreeTexImageData(ctx, dstImage);
412
413 /* initialize new image */
414 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
415 dstDepth, border, srcImage->InternalFormat,
416 srcImage->TexFormat);
417
418 stImage = st_texture_image(dstImage);
419 stImage->level = dstLevel;
420
421 pipe_resource_reference(&stImage->pt, pt);
422 }
423 }