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