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