st/mesa: add LATC and 3DC support
[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 texObj->Image[face][baseLevel]->TexFormat == MESA_FORMAT_SIGNED_L_LATC1 ||
210 texObj->Image[face][baseLevel]->TexFormat == MESA_FORMAT_SIGNED_LA_LATC2)
211 datatype = GL_FLOAT;
212 else
213 datatype = GL_UNSIGNED_BYTE;
214
215 comps = 4;
216 }
217 else {
218 _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat,
219 &datatype, &comps);
220 assert(comps > 0 && "bad texture format in fallback_generate_mipmap()");
221 }
222
223 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
224 const uint srcLevel = dstLevel - 1;
225 const uint srcWidth = u_minify(pt->width0, srcLevel);
226 const uint srcHeight = u_minify(pt->height0, srcLevel);
227 const uint srcDepth = u_minify(pt->depth0, srcLevel);
228 const uint dstWidth = u_minify(pt->width0, dstLevel);
229 const uint dstHeight = u_minify(pt->height0, dstLevel);
230 const uint dstDepth = u_minify(pt->depth0, dstLevel);
231 struct pipe_transfer *srcTrans, *dstTrans;
232 const ubyte *srcData;
233 ubyte *dstData;
234 int srcStride, dstStride;
235
236 srcTrans = pipe_get_transfer(st_context(ctx)->pipe, pt, srcLevel,
237 face,
238 PIPE_TRANSFER_READ, 0, 0,
239 srcWidth, srcHeight);
240
241 dstTrans = pipe_get_transfer(st_context(ctx)->pipe, pt, dstLevel,
242 face,
243 PIPE_TRANSFER_WRITE, 0, 0,
244 dstWidth, dstHeight);
245
246 srcData = (ubyte *) pipe_transfer_map(pipe, srcTrans);
247 dstData = (ubyte *) pipe_transfer_map(pipe, dstTrans);
248
249 srcStride = srcTrans->stride / util_format_get_blocksize(srcTrans->resource->format);
250 dstStride = dstTrans->stride / util_format_get_blocksize(dstTrans->resource->format);
251
252 /* this cannot work correctly for 3d since it does
253 not respect layerStride. */
254 if (compressed) {
255 const enum pipe_format format = pt->format;
256 const uint bw = util_format_get_blockwidth(format);
257 const uint bh = util_format_get_blockheight(format);
258 const uint srcWidth2 = align(srcWidth, bw);
259 const uint srcHeight2 = align(srcHeight, bh);
260 const uint dstWidth2 = align(dstWidth, bw);
261 const uint dstHeight2 = align(dstHeight, bh);
262 uint8_t *srcTemp, *dstTemp;
263
264 assert(comps == 4);
265
266 srcTemp = malloc(srcWidth2 * srcHeight2 * comps * (datatype == GL_FLOAT ? 4 : 1));
267 dstTemp = malloc(dstWidth2 * dstHeight2 * comps * (datatype == GL_FLOAT ? 4 : 1));
268
269 /* decompress the src image: srcData -> srcTemp */
270 decompress_image(format, datatype, srcData, srcTemp, srcWidth2, srcHeight2, srcTrans->stride);
271
272 _mesa_generate_mipmap_level(target, datatype, comps,
273 0 /*border*/,
274 srcWidth2, srcHeight2, srcDepth,
275 srcTemp,
276 srcWidth2, /* stride in texels */
277 dstWidth2, dstHeight2, dstDepth,
278 dstTemp,
279 dstWidth2); /* stride in texels */
280
281 /* compress the new image: dstTemp -> dstData */
282 compress_image(format, datatype, dstTemp, dstData, dstWidth2, dstHeight2, dstTrans->stride);
283
284 free(srcTemp);
285 free(dstTemp);
286 }
287 else {
288 _mesa_generate_mipmap_level(target, datatype, comps,
289 0 /*border*/,
290 srcWidth, srcHeight, srcDepth,
291 srcData,
292 srcStride, /* stride in texels */
293 dstWidth, dstHeight, dstDepth,
294 dstData,
295 dstStride); /* stride in texels */
296 }
297
298 pipe_transfer_unmap(pipe, srcTrans);
299 pipe_transfer_unmap(pipe, dstTrans);
300
301 pipe->transfer_destroy(pipe, srcTrans);
302 pipe->transfer_destroy(pipe, dstTrans);
303 }
304 }
305
306
307 /**
308 * Compute the expected number of mipmap levels in the texture given
309 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
310 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
311 * levels should be generated.
312 */
313 static GLuint
314 compute_num_levels(struct gl_context *ctx,
315 struct gl_texture_object *texObj,
316 GLenum target)
317 {
318 if (target == GL_TEXTURE_RECTANGLE_ARB) {
319 return 1;
320 }
321 else {
322 const struct gl_texture_image *baseImage =
323 _mesa_get_tex_image(ctx, texObj, target, texObj->BaseLevel);
324 GLuint size, numLevels;
325
326 size = MAX2(baseImage->Width2, baseImage->Height2);
327 size = MAX2(size, baseImage->Depth2);
328
329 numLevels = texObj->BaseLevel;
330
331 while (size > 0) {
332 numLevels++;
333 size >>= 1;
334 }
335
336 numLevels = MIN2(numLevels, texObj->MaxLevel + 1);
337
338 assert(numLevels >= 1);
339
340 return numLevels;
341 }
342 }
343
344
345 /**
346 * Called via ctx->Driver.GenerateMipmap().
347 */
348 void
349 st_generate_mipmap(struct gl_context *ctx, GLenum target,
350 struct gl_texture_object *texObj)
351 {
352 struct st_context *st = st_context(ctx);
353 struct st_texture_object *stObj = st_texture_object(texObj);
354 struct pipe_resource *pt = st_get_texobj_resource(texObj);
355 const uint baseLevel = texObj->BaseLevel;
356 uint lastLevel;
357 uint dstLevel;
358
359 if (!pt)
360 return;
361
362 /* not sure if this ultimately actually should work,
363 but we're not supporting multisampled textures yet. */
364 assert(pt->nr_samples < 2);
365
366 /* find expected last mipmap level to generate*/
367 lastLevel = compute_num_levels(ctx, texObj, target) - 1;
368
369 if (lastLevel == 0)
370 return;
371
372 /* The texture isn't in a "complete" state yet so set the expected
373 * lastLevel here, since it won't get done in st_finalize_texture().
374 */
375 stObj->lastLevel = lastLevel;
376
377 if (pt->last_level < lastLevel) {
378 /* The current gallium texture doesn't have space for all the
379 * mipmap levels we need to generate. So allocate a new texture.
380 */
381 struct pipe_resource *oldTex = stObj->pt;
382
383 /* create new texture with space for more levels */
384 stObj->pt = st_texture_create(st,
385 oldTex->target,
386 oldTex->format,
387 lastLevel,
388 oldTex->width0,
389 oldTex->height0,
390 oldTex->depth0,
391 oldTex->array_size,
392 oldTex->bind);
393
394 /* This will copy the old texture's base image into the new texture
395 * which we just allocated.
396 */
397 st_finalize_texture(ctx, st->pipe, texObj);
398
399 /* release the old tex (will likely be freed too) */
400 pipe_resource_reference(&oldTex, NULL);
401 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
402 }
403 else {
404 /* Make sure that the base texture image data is present in the
405 * texture buffer.
406 */
407 st_finalize_texture(ctx, st->pipe, texObj);
408 }
409
410 pt = stObj->pt;
411
412 assert(pt->last_level >= lastLevel);
413
414 /* Try to generate the mipmap by rendering/texturing. If that fails,
415 * use the software fallback.
416 */
417 if (!st_render_mipmap(st, target, stObj, baseLevel, lastLevel)) {
418 /* since the util code actually also has a fallback, should
419 probably make it never fail and kill this */
420 fallback_generate_mipmap(ctx, target, texObj);
421 }
422
423 /* Fill in the Mesa gl_texture_image fields */
424 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
425 const uint srcLevel = dstLevel - 1;
426 const struct gl_texture_image *srcImage
427 = _mesa_get_tex_image(ctx, texObj, target, srcLevel);
428 struct gl_texture_image *dstImage;
429 struct st_texture_image *stImage;
430 uint dstWidth = u_minify(pt->width0, dstLevel);
431 uint dstHeight = u_minify(pt->height0, dstLevel);
432 uint dstDepth = u_minify(pt->depth0, dstLevel);
433 uint border = srcImage->Border;
434
435 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel);
436 if (!dstImage) {
437 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
438 return;
439 }
440
441 /* Free old image data */
442 if (dstImage->Data)
443 ctx->Driver.FreeTexImageData(ctx, dstImage);
444
445 /* initialize new image */
446 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
447 dstDepth, border, srcImage->InternalFormat,
448 srcImage->TexFormat);
449
450 stImage = st_texture_image(dstImage);
451 stImage->level = dstLevel;
452
453 pipe_resource_reference(&stImage->pt, pt);
454 }
455 }