mesa/st: fix generate mipmap for signed compressed formats.
[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, int datatype,
107 const uint8_t *src, void *dst,
108 unsigned width, unsigned height, unsigned src_stride)
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 uint dst_stride = 4 * MAX2(width, bw);
114
115 if (datatype == GL_FLOAT) {
116 desc->unpack_rgba_float((float *)dst, dst_stride * sizeof(GLfloat), src, src_stride, width, height);
117 if (width < bw || height < bh) {
118 float *dst_p = (float *)dst;
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[p + 0] = dst_p[0];
130 dst_p[p + 1] = dst_p[1];
131 dst_p[p + 2] = dst_p[2];
132 dst_p[p + 3] = dst_p[3];
133 }
134 }
135 }
136 }
137 } else {
138 desc->unpack_rgba_8unorm((uint8_t *)dst, dst_stride, src, src_stride, width, height);
139 if (width < bw || height < bh) {
140 uint8_t *dst_p = (uint8_t *)dst;
141 /* We're decompressing an image smaller than the compression
142 * block size. We don't want garbage pixel values in the region
143 * outside (width x height) so replicate pixels from the (width
144 * x height) region to fill out the (bw x bh) block size.
145 */
146 uint x, y;
147 for (y = 0; y < bh; y++) {
148 for (x = 0; x < bw; x++) {
149 if (x >= width || y >= height) {
150 uint p = (y * bw + x) * 4;
151 dst_p[p + 0] = dst_p[0];
152 dst_p[p + 1] = dst_p[1];
153 dst_p[p + 2] = dst_p[2];
154 dst_p[p + 3] = dst_p[3];
155 }
156 }
157 }
158 }
159 }
160 }
161
162 /**
163 * Helper function to compress an image. The source is a 32-bpp RGBA image
164 * with stride==width.
165 */
166 static void
167 compress_image(enum pipe_format format, int datatype,
168 const void *src, uint8_t *dst,
169 unsigned width, unsigned height, unsigned dst_stride)
170 {
171 const struct util_format_description *desc = util_format_description(format);
172 const uint src_stride = 4 * width;
173
174 if (datatype == GL_FLOAT)
175 desc->pack_rgba_float(dst, dst_stride, (GLfloat *)src, src_stride * sizeof(GLfloat), width, height);
176 else
177 desc->pack_rgba_8unorm(dst, dst_stride, (uint8_t *)src, src_stride, width, height);
178 }
179
180
181 /**
182 * Software fallback for generate mipmap levels.
183 */
184 static void
185 fallback_generate_mipmap(struct gl_context *ctx, GLenum target,
186 struct gl_texture_object *texObj)
187 {
188 struct pipe_context *pipe = st_context(ctx)->pipe;
189 struct pipe_resource *pt = st_get_texobj_resource(texObj);
190 const uint baseLevel = texObj->BaseLevel;
191 const uint lastLevel = pt->last_level;
192 const uint face = _mesa_tex_target_to_face(target);
193 uint dstLevel;
194 GLenum datatype;
195 GLuint comps;
196 GLboolean compressed;
197
198 if (ST_DEBUG & DEBUG_FALLBACK)
199 debug_printf("%s: fallback processing\n", __FUNCTION__);
200
201 assert(target != GL_TEXTURE_3D); /* not done yet */
202
203 compressed =
204 _mesa_is_format_compressed(texObj->Image[face][baseLevel]->TexFormat);
205
206 if (compressed) {
207 if (texObj->Image[face][baseLevel]->TexFormat == MESA_FORMAT_SIGNED_RED_RGTC1 ||
208 texObj->Image[face][baseLevel]->TexFormat == MESA_FORMAT_SIGNED_RG_RGTC2)
209 datatype = GL_FLOAT;
210 else
211 datatype = GL_UNSIGNED_BYTE;
212
213 comps = 4;
214 }
215 else {
216 _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat,
217 &datatype, &comps);
218 assert(comps > 0 && "bad texture format in fallback_generate_mipmap()");
219 }
220
221 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
222 const uint srcLevel = dstLevel - 1;
223 const uint srcWidth = u_minify(pt->width0, srcLevel);
224 const uint srcHeight = u_minify(pt->height0, srcLevel);
225 const uint srcDepth = u_minify(pt->depth0, srcLevel);
226 const uint dstWidth = u_minify(pt->width0, dstLevel);
227 const uint dstHeight = u_minify(pt->height0, dstLevel);
228 const uint dstDepth = u_minify(pt->depth0, dstLevel);
229 struct pipe_transfer *srcTrans, *dstTrans;
230 const ubyte *srcData;
231 ubyte *dstData;
232 int srcStride, dstStride;
233
234 srcTrans = pipe_get_transfer(st_context(ctx)->pipe, pt, srcLevel,
235 face,
236 PIPE_TRANSFER_READ, 0, 0,
237 srcWidth, srcHeight);
238
239 dstTrans = pipe_get_transfer(st_context(ctx)->pipe, pt, dstLevel,
240 face,
241 PIPE_TRANSFER_WRITE, 0, 0,
242 dstWidth, dstHeight);
243
244 srcData = (ubyte *) pipe_transfer_map(pipe, srcTrans);
245 dstData = (ubyte *) pipe_transfer_map(pipe, dstTrans);
246
247 srcStride = srcTrans->stride / util_format_get_blocksize(srcTrans->resource->format);
248 dstStride = dstTrans->stride / util_format_get_blocksize(dstTrans->resource->format);
249
250 /* this cannot work correctly for 3d since it does
251 not respect layerStride. */
252 if (compressed) {
253 const enum pipe_format format = pt->format;
254 const uint bw = util_format_get_blockwidth(format);
255 const uint bh = util_format_get_blockheight(format);
256 const uint srcWidth2 = align(srcWidth, bw);
257 const uint srcHeight2 = align(srcHeight, bh);
258 const uint dstWidth2 = align(dstWidth, bw);
259 const uint dstHeight2 = align(dstHeight, bh);
260 uint8_t *srcTemp, *dstTemp;
261
262 assert(comps == 4);
263
264 srcTemp = malloc(srcWidth2 * srcHeight2 * comps * (datatype == GL_FLOAT ? 4 : 1));
265 dstTemp = malloc(dstWidth2 * dstHeight2 * comps * (datatype == GL_FLOAT ? 4 : 1));
266
267 /* decompress the src image: srcData -> srcTemp */
268 decompress_image(format, datatype, srcData, srcTemp, srcWidth, srcHeight, srcTrans->stride);
269
270 _mesa_generate_mipmap_level(target, datatype, comps,
271 0 /*border*/,
272 srcWidth2, srcHeight2, srcDepth,
273 srcTemp,
274 srcWidth2, /* stride in texels */
275 dstWidth2, dstHeight2, dstDepth,
276 dstTemp,
277 dstWidth2); /* stride in texels */
278
279 /* compress the new image: dstTemp -> dstData */
280 compress_image(format, datatype, dstTemp, dstData, dstWidth, dstHeight, dstTrans->stride);
281
282 free(srcTemp);
283 free(dstTemp);
284 }
285 else {
286 _mesa_generate_mipmap_level(target, datatype, comps,
287 0 /*border*/,
288 srcWidth, srcHeight, srcDepth,
289 srcData,
290 srcStride, /* stride in texels */
291 dstWidth, dstHeight, dstDepth,
292 dstData,
293 dstStride); /* stride in texels */
294 }
295
296 pipe_transfer_unmap(pipe, srcTrans);
297 pipe_transfer_unmap(pipe, dstTrans);
298
299 pipe->transfer_destroy(pipe, srcTrans);
300 pipe->transfer_destroy(pipe, dstTrans);
301 }
302 }
303
304
305 /**
306 * Compute the expected number of mipmap levels in the texture given
307 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
308 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
309 * levels should be generated.
310 */
311 static GLuint
312 compute_num_levels(struct gl_context *ctx,
313 struct gl_texture_object *texObj,
314 GLenum target)
315 {
316 if (target == GL_TEXTURE_RECTANGLE_ARB) {
317 return 1;
318 }
319 else {
320 const struct gl_texture_image *baseImage =
321 _mesa_get_tex_image(ctx, texObj, target, texObj->BaseLevel);
322 GLuint size, numLevels;
323
324 size = MAX2(baseImage->Width2, baseImage->Height2);
325 size = MAX2(size, baseImage->Depth2);
326
327 numLevels = texObj->BaseLevel;
328
329 while (size > 0) {
330 numLevels++;
331 size >>= 1;
332 }
333
334 numLevels = MIN2(numLevels, texObj->MaxLevel + 1);
335
336 assert(numLevels >= 1);
337
338 return numLevels;
339 }
340 }
341
342
343 /**
344 * Called via ctx->Driver.GenerateMipmap().
345 */
346 void
347 st_generate_mipmap(struct gl_context *ctx, GLenum target,
348 struct gl_texture_object *texObj)
349 {
350 struct st_context *st = st_context(ctx);
351 struct st_texture_object *stObj = st_texture_object(texObj);
352 struct pipe_resource *pt = st_get_texobj_resource(texObj);
353 const uint baseLevel = texObj->BaseLevel;
354 uint lastLevel;
355 uint dstLevel;
356
357 if (!pt)
358 return;
359
360 /* not sure if this ultimately actually should work,
361 but we're not supporting multisampled textures yet. */
362 assert(pt->nr_samples < 2);
363
364 /* find expected last mipmap level to generate*/
365 lastLevel = compute_num_levels(ctx, texObj, target) - 1;
366
367 if (lastLevel == 0)
368 return;
369
370 /* The texture isn't in a "complete" state yet so set the expected
371 * lastLevel here, since it won't get done in st_finalize_texture().
372 */
373 stObj->lastLevel = lastLevel;
374
375 if (pt->last_level < lastLevel) {
376 /* The current gallium texture doesn't have space for all the
377 * mipmap levels we need to generate. So allocate a new texture.
378 */
379 struct pipe_resource *oldTex = stObj->pt;
380
381 /* create new texture with space for more levels */
382 stObj->pt = st_texture_create(st,
383 oldTex->target,
384 oldTex->format,
385 lastLevel,
386 oldTex->width0,
387 oldTex->height0,
388 oldTex->depth0,
389 oldTex->array_size,
390 oldTex->bind);
391
392 /* This will copy the old texture's base image into the new texture
393 * which we just allocated.
394 */
395 st_finalize_texture(ctx, st->pipe, texObj);
396
397 /* release the old tex (will likely be freed too) */
398 pipe_resource_reference(&oldTex, NULL);
399 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
400 }
401 else {
402 /* Make sure that the base texture image data is present in the
403 * texture buffer.
404 */
405 st_finalize_texture(ctx, st->pipe, texObj);
406 }
407
408 pt = stObj->pt;
409
410 assert(pt->last_level >= lastLevel);
411
412 /* Try to generate the mipmap by rendering/texturing. If that fails,
413 * use the software fallback.
414 */
415 if (!st_render_mipmap(st, target, stObj, baseLevel, lastLevel)) {
416 /* since the util code actually also has a fallback, should
417 probably make it never fail and kill this */
418 fallback_generate_mipmap(ctx, target, texObj);
419 }
420
421 /* Fill in the Mesa gl_texture_image fields */
422 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
423 const uint srcLevel = dstLevel - 1;
424 const struct gl_texture_image *srcImage
425 = _mesa_get_tex_image(ctx, texObj, target, srcLevel);
426 struct gl_texture_image *dstImage;
427 struct st_texture_image *stImage;
428 uint dstWidth = u_minify(pt->width0, dstLevel);
429 uint dstHeight = u_minify(pt->height0, dstLevel);
430 uint dstDepth = u_minify(pt->depth0, dstLevel);
431 uint border = srcImage->Border;
432
433 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel);
434 if (!dstImage) {
435 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
436 return;
437 }
438
439 /* Free old image data */
440 if (dstImage->Data)
441 ctx->Driver.FreeTexImageData(ctx, dstImage);
442
443 /* initialize new image */
444 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
445 dstDepth, border, srcImage->InternalFormat,
446 srcImage->TexFormat);
447
448 stImage = st_texture_image(dstImage);
449 stImage->level = dstLevel;
450
451 pipe_resource_reference(&stImage->pt, pt);
452 }
453 }