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