Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / util / u_gen_mipmap.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2008 VMware, Inc. All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /**
30 * @file
31 * Mipmap generation utility
32 *
33 * @author Brian Paul
34 */
35
36
37 #include "pipe/p_context.h"
38 #include "util/u_debug.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_inlines.h"
41 #include "pipe/p_shader_tokens.h"
42 #include "pipe/p_state.h"
43
44 #include "util/u_format.h"
45 #include "util/u_memory.h"
46 #include "util/u_draw_quad.h"
47 #include "util/u_gen_mipmap.h"
48 #include "util/u_simple_shaders.h"
49 #include "util/u_math.h"
50 #include "util/u_texture.h"
51
52 #include "cso_cache/cso_context.h"
53
54
55 struct gen_mipmap_state
56 {
57 struct pipe_context *pipe;
58 struct cso_context *cso;
59
60 struct pipe_blend_state blend;
61 struct pipe_depth_stencil_alpha_state depthstencil;
62 struct pipe_rasterizer_state rasterizer;
63 struct pipe_sampler_state sampler;
64
65 void *vs;
66 void *fs2d, *fsCube;
67
68 struct pipe_buffer *vbuf; /**< quad vertices */
69 unsigned vbuf_slot;
70
71 float vertices[4][2][4]; /**< vertex/texcoords for quad */
72 };
73
74
75
76 enum dtype
77 {
78 DTYPE_UBYTE,
79 DTYPE_UBYTE_3_3_2,
80 DTYPE_USHORT,
81 DTYPE_USHORT_4_4_4_4,
82 DTYPE_USHORT_5_6_5,
83 DTYPE_USHORT_1_5_5_5_REV,
84 DTYPE_UINT,
85 DTYPE_FLOAT,
86 DTYPE_HALF_FLOAT
87 };
88
89
90 typedef ushort half_float;
91
92
93 static half_float
94 float_to_half(float f)
95 {
96 /* XXX fix this */
97 return 0;
98 }
99
100 static float
101 half_to_float(half_float h)
102 {
103 /* XXX fix this */
104 return 0.0f;
105 }
106
107
108
109
110 /**
111 * \name Support macros for do_row and do_row_3d
112 *
113 * The macro madness is here for two reasons. First, it compacts the code
114 * slightly. Second, it makes it much easier to adjust the specifics of the
115 * filter to tune the rounding characteristics.
116 */
117 /*@{*/
118 #define DECLARE_ROW_POINTERS(t, e) \
119 const t(*rowA)[e] = (const t(*)[e]) srcRowA; \
120 const t(*rowB)[e] = (const t(*)[e]) srcRowB; \
121 const t(*rowC)[e] = (const t(*)[e]) srcRowC; \
122 const t(*rowD)[e] = (const t(*)[e]) srcRowD; \
123 t(*dst)[e] = (t(*)[e]) dstRow
124
125 #define DECLARE_ROW_POINTERS0(t) \
126 const t *rowA = (const t *) srcRowA; \
127 const t *rowB = (const t *) srcRowB; \
128 const t *rowC = (const t *) srcRowC; \
129 const t *rowD = (const t *) srcRowD; \
130 t *dst = (t *) dstRow
131
132 #define FILTER_SUM_3D(Aj, Ak, Bj, Bk, Cj, Ck, Dj, Dk) \
133 ((unsigned) Aj + (unsigned) Ak \
134 + (unsigned) Bj + (unsigned) Bk \
135 + (unsigned) Cj + (unsigned) Ck \
136 + (unsigned) Dj + (unsigned) Dk \
137 + 4) >> 3
138
139 #define FILTER_3D(e) \
140 do { \
141 dst[i][e] = FILTER_SUM_3D(rowA[j][e], rowA[k][e], \
142 rowB[j][e], rowB[k][e], \
143 rowC[j][e], rowC[k][e], \
144 rowD[j][e], rowD[k][e]); \
145 } while(0)
146
147 #define FILTER_F_3D(e) \
148 do { \
149 dst[i][e] = (rowA[j][e] + rowA[k][e] \
150 + rowB[j][e] + rowB[k][e] \
151 + rowC[j][e] + rowC[k][e] \
152 + rowD[j][e] + rowD[k][e]) * 0.125F; \
153 } while(0)
154
155 #define FILTER_HF_3D(e) \
156 do { \
157 const float aj = half_to_float(rowA[j][e]); \
158 const float ak = half_to_float(rowA[k][e]); \
159 const float bj = half_to_float(rowB[j][e]); \
160 const float bk = half_to_float(rowB[k][e]); \
161 const float cj = half_to_float(rowC[j][e]); \
162 const float ck = half_to_float(rowC[k][e]); \
163 const float dj = half_to_float(rowD[j][e]); \
164 const float dk = half_to_float(rowD[k][e]); \
165 dst[i][e] = float_to_half((aj + ak + bj + bk + cj + ck + dj + dk) \
166 * 0.125F); \
167 } while(0)
168 /*@}*/
169
170
171 /**
172 * Average together two rows of a source image to produce a single new
173 * row in the dest image. It's legal for the two source rows to point
174 * to the same data. The source width must be equal to either the
175 * dest width or two times the dest width.
176 * \param datatype GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, GL_FLOAT, etc.
177 * \param comps number of components per pixel (1..4)
178 */
179 static void
180 do_row(enum dtype datatype, uint comps, int srcWidth,
181 const void *srcRowA, const void *srcRowB,
182 int dstWidth, void *dstRow)
183 {
184 const uint k0 = (srcWidth == dstWidth) ? 0 : 1;
185 const uint colStride = (srcWidth == dstWidth) ? 1 : 2;
186
187 assert(comps >= 1);
188 assert(comps <= 4);
189
190 /* This assertion is no longer valid with non-power-of-2 textures
191 assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth);
192 */
193
194 if (datatype == DTYPE_UBYTE && comps == 4) {
195 uint i, j, k;
196 const ubyte(*rowA)[4] = (const ubyte(*)[4]) srcRowA;
197 const ubyte(*rowB)[4] = (const ubyte(*)[4]) srcRowB;
198 ubyte(*dst)[4] = (ubyte(*)[4]) dstRow;
199 for (i = j = 0, k = k0; i < (uint) dstWidth;
200 i++, j += colStride, k += colStride) {
201 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
202 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
203 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
204 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
205 }
206 }
207 else if (datatype == DTYPE_UBYTE && comps == 3) {
208 uint i, j, k;
209 const ubyte(*rowA)[3] = (const ubyte(*)[3]) srcRowA;
210 const ubyte(*rowB)[3] = (const ubyte(*)[3]) srcRowB;
211 ubyte(*dst)[3] = (ubyte(*)[3]) dstRow;
212 for (i = j = 0, k = k0; i < (uint) dstWidth;
213 i++, j += colStride, k += colStride) {
214 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
215 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
216 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
217 }
218 }
219 else if (datatype == DTYPE_UBYTE && comps == 2) {
220 uint i, j, k;
221 const ubyte(*rowA)[2] = (const ubyte(*)[2]) srcRowA;
222 const ubyte(*rowB)[2] = (const ubyte(*)[2]) srcRowB;
223 ubyte(*dst)[2] = (ubyte(*)[2]) dstRow;
224 for (i = j = 0, k = k0; i < (uint) dstWidth;
225 i++, j += colStride, k += colStride) {
226 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) >> 2;
227 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) >> 2;
228 }
229 }
230 else if (datatype == DTYPE_UBYTE && comps == 1) {
231 uint i, j, k;
232 const ubyte *rowA = (const ubyte *) srcRowA;
233 const ubyte *rowB = (const ubyte *) srcRowB;
234 ubyte *dst = (ubyte *) dstRow;
235 for (i = j = 0, k = k0; i < (uint) dstWidth;
236 i++, j += colStride, k += colStride) {
237 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) >> 2;
238 }
239 }
240
241 else if (datatype == DTYPE_USHORT && comps == 4) {
242 uint i, j, k;
243 const ushort(*rowA)[4] = (const ushort(*)[4]) srcRowA;
244 const ushort(*rowB)[4] = (const ushort(*)[4]) srcRowB;
245 ushort(*dst)[4] = (ushort(*)[4]) dstRow;
246 for (i = j = 0, k = k0; i < (uint) dstWidth;
247 i++, j += colStride, k += colStride) {
248 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
249 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
250 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
251 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
252 }
253 }
254 else if (datatype == DTYPE_USHORT && comps == 3) {
255 uint i, j, k;
256 const ushort(*rowA)[3] = (const ushort(*)[3]) srcRowA;
257 const ushort(*rowB)[3] = (const ushort(*)[3]) srcRowB;
258 ushort(*dst)[3] = (ushort(*)[3]) dstRow;
259 for (i = j = 0, k = k0; i < (uint) dstWidth;
260 i++, j += colStride, k += colStride) {
261 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
262 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
263 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
264 }
265 }
266 else if (datatype == DTYPE_USHORT && comps == 2) {
267 uint i, j, k;
268 const ushort(*rowA)[2] = (const ushort(*)[2]) srcRowA;
269 const ushort(*rowB)[2] = (const ushort(*)[2]) srcRowB;
270 ushort(*dst)[2] = (ushort(*)[2]) dstRow;
271 for (i = j = 0, k = k0; i < (uint) dstWidth;
272 i++, j += colStride, k += colStride) {
273 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
274 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
275 }
276 }
277 else if (datatype == DTYPE_USHORT && comps == 1) {
278 uint i, j, k;
279 const ushort *rowA = (const ushort *) srcRowA;
280 const ushort *rowB = (const ushort *) srcRowB;
281 ushort *dst = (ushort *) dstRow;
282 for (i = j = 0, k = k0; i < (uint) dstWidth;
283 i++, j += colStride, k += colStride) {
284 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) / 4;
285 }
286 }
287
288 else if (datatype == DTYPE_FLOAT && comps == 4) {
289 uint i, j, k;
290 const float(*rowA)[4] = (const float(*)[4]) srcRowA;
291 const float(*rowB)[4] = (const float(*)[4]) srcRowB;
292 float(*dst)[4] = (float(*)[4]) dstRow;
293 for (i = j = 0, k = k0; i < (uint) dstWidth;
294 i++, j += colStride, k += colStride) {
295 dst[i][0] = (rowA[j][0] + rowA[k][0] +
296 rowB[j][0] + rowB[k][0]) * 0.25F;
297 dst[i][1] = (rowA[j][1] + rowA[k][1] +
298 rowB[j][1] + rowB[k][1]) * 0.25F;
299 dst[i][2] = (rowA[j][2] + rowA[k][2] +
300 rowB[j][2] + rowB[k][2]) * 0.25F;
301 dst[i][3] = (rowA[j][3] + rowA[k][3] +
302 rowB[j][3] + rowB[k][3]) * 0.25F;
303 }
304 }
305 else if (datatype == DTYPE_FLOAT && comps == 3) {
306 uint i, j, k;
307 const float(*rowA)[3] = (const float(*)[3]) srcRowA;
308 const float(*rowB)[3] = (const float(*)[3]) srcRowB;
309 float(*dst)[3] = (float(*)[3]) dstRow;
310 for (i = j = 0, k = k0; i < (uint) dstWidth;
311 i++, j += colStride, k += colStride) {
312 dst[i][0] = (rowA[j][0] + rowA[k][0] +
313 rowB[j][0] + rowB[k][0]) * 0.25F;
314 dst[i][1] = (rowA[j][1] + rowA[k][1] +
315 rowB[j][1] + rowB[k][1]) * 0.25F;
316 dst[i][2] = (rowA[j][2] + rowA[k][2] +
317 rowB[j][2] + rowB[k][2]) * 0.25F;
318 }
319 }
320 else if (datatype == DTYPE_FLOAT && comps == 2) {
321 uint i, j, k;
322 const float(*rowA)[2] = (const float(*)[2]) srcRowA;
323 const float(*rowB)[2] = (const float(*)[2]) srcRowB;
324 float(*dst)[2] = (float(*)[2]) dstRow;
325 for (i = j = 0, k = k0; i < (uint) dstWidth;
326 i++, j += colStride, k += colStride) {
327 dst[i][0] = (rowA[j][0] + rowA[k][0] +
328 rowB[j][0] + rowB[k][0]) * 0.25F;
329 dst[i][1] = (rowA[j][1] + rowA[k][1] +
330 rowB[j][1] + rowB[k][1]) * 0.25F;
331 }
332 }
333 else if (datatype == DTYPE_FLOAT && comps == 1) {
334 uint i, j, k;
335 const float *rowA = (const float *) srcRowA;
336 const float *rowB = (const float *) srcRowB;
337 float *dst = (float *) dstRow;
338 for (i = j = 0, k = k0; i < (uint) dstWidth;
339 i++, j += colStride, k += colStride) {
340 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) * 0.25F;
341 }
342 }
343
344 #if 0
345 else if (datatype == HALF_DTYPE_FLOAT && comps == 4) {
346 uint i, j, k, comp;
347 const half_float(*rowA)[4] = (const half_float(*)[4]) srcRowA;
348 const half_float(*rowB)[4] = (const half_float(*)[4]) srcRowB;
349 half_float(*dst)[4] = (half_float(*)[4]) dstRow;
350 for (i = j = 0, k = k0; i < (uint) dstWidth;
351 i++, j += colStride, k += colStride) {
352 for (comp = 0; comp < 4; comp++) {
353 float aj, ak, bj, bk;
354 aj = half_to_float(rowA[j][comp]);
355 ak = half_to_float(rowA[k][comp]);
356 bj = half_to_float(rowB[j][comp]);
357 bk = half_to_float(rowB[k][comp]);
358 dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F);
359 }
360 }
361 }
362 else if (datatype == DTYPE_HALF_FLOAT && comps == 3) {
363 uint i, j, k, comp;
364 const half_float(*rowA)[3] = (const half_float(*)[3]) srcRowA;
365 const half_float(*rowB)[3] = (const half_float(*)[3]) srcRowB;
366 half_float(*dst)[3] = (half_float(*)[3]) dstRow;
367 for (i = j = 0, k = k0; i < (uint) dstWidth;
368 i++, j += colStride, k += colStride) {
369 for (comp = 0; comp < 3; comp++) {
370 float aj, ak, bj, bk;
371 aj = half_to_float(rowA[j][comp]);
372 ak = half_to_float(rowA[k][comp]);
373 bj = half_to_float(rowB[j][comp]);
374 bk = half_to_float(rowB[k][comp]);
375 dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F);
376 }
377 }
378 }
379 else if (datatype == DTYPE_HALF_FLOAT && comps == 2) {
380 uint i, j, k, comp;
381 const half_float(*rowA)[2] = (const half_float(*)[2]) srcRowA;
382 const half_float(*rowB)[2] = (const half_float(*)[2]) srcRowB;
383 half_float(*dst)[2] = (half_float(*)[2]) dstRow;
384 for (i = j = 0, k = k0; i < (uint) dstWidth;
385 i++, j += colStride, k += colStride) {
386 for (comp = 0; comp < 2; comp++) {
387 float aj, ak, bj, bk;
388 aj = half_to_float(rowA[j][comp]);
389 ak = half_to_float(rowA[k][comp]);
390 bj = half_to_float(rowB[j][comp]);
391 bk = half_to_float(rowB[k][comp]);
392 dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F);
393 }
394 }
395 }
396 else if (datatype == DTYPE_HALF_FLOAT && comps == 1) {
397 uint i, j, k;
398 const half_float *rowA = (const half_float *) srcRowA;
399 const half_float *rowB = (const half_float *) srcRowB;
400 half_float *dst = (half_float *) dstRow;
401 for (i = j = 0, k = k0; i < (uint) dstWidth;
402 i++, j += colStride, k += colStride) {
403 float aj, ak, bj, bk;
404 aj = half_to_float(rowA[j]);
405 ak = half_to_float(rowA[k]);
406 bj = half_to_float(rowB[j]);
407 bk = half_to_float(rowB[k]);
408 dst[i] = float_to_half((aj + ak + bj + bk) * 0.25F);
409 }
410 }
411 #endif
412
413 else if (datatype == DTYPE_UINT && comps == 1) {
414 uint i, j, k;
415 const uint *rowA = (const uint *) srcRowA;
416 const uint *rowB = (const uint *) srcRowB;
417 uint *dst = (uint *) dstRow;
418 for (i = j = 0, k = k0; i < (uint) dstWidth;
419 i++, j += colStride, k += colStride) {
420 dst[i] = rowA[j] / 4 + rowA[k] / 4 + rowB[j] / 4 + rowB[k] / 4;
421 }
422 }
423
424 else if (datatype == DTYPE_USHORT_5_6_5 && comps == 3) {
425 uint i, j, k;
426 const ushort *rowA = (const ushort *) srcRowA;
427 const ushort *rowB = (const ushort *) srcRowB;
428 ushort *dst = (ushort *) dstRow;
429 for (i = j = 0, k = k0; i < (uint) dstWidth;
430 i++, j += colStride, k += colStride) {
431 const int rowAr0 = rowA[j] & 0x1f;
432 const int rowAr1 = rowA[k] & 0x1f;
433 const int rowBr0 = rowB[j] & 0x1f;
434 const int rowBr1 = rowB[k] & 0x1f;
435 const int rowAg0 = (rowA[j] >> 5) & 0x3f;
436 const int rowAg1 = (rowA[k] >> 5) & 0x3f;
437 const int rowBg0 = (rowB[j] >> 5) & 0x3f;
438 const int rowBg1 = (rowB[k] >> 5) & 0x3f;
439 const int rowAb0 = (rowA[j] >> 11) & 0x1f;
440 const int rowAb1 = (rowA[k] >> 11) & 0x1f;
441 const int rowBb0 = (rowB[j] >> 11) & 0x1f;
442 const int rowBb1 = (rowB[k] >> 11) & 0x1f;
443 const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
444 const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
445 const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
446 dst[i] = (blue << 11) | (green << 5) | red;
447 }
448 }
449 else if (datatype == DTYPE_USHORT_4_4_4_4 && comps == 4) {
450 uint i, j, k;
451 const ushort *rowA = (const ushort *) srcRowA;
452 const ushort *rowB = (const ushort *) srcRowB;
453 ushort *dst = (ushort *) dstRow;
454 for (i = j = 0, k = k0; i < (uint) dstWidth;
455 i++, j += colStride, k += colStride) {
456 const int rowAr0 = rowA[j] & 0xf;
457 const int rowAr1 = rowA[k] & 0xf;
458 const int rowBr0 = rowB[j] & 0xf;
459 const int rowBr1 = rowB[k] & 0xf;
460 const int rowAg0 = (rowA[j] >> 4) & 0xf;
461 const int rowAg1 = (rowA[k] >> 4) & 0xf;
462 const int rowBg0 = (rowB[j] >> 4) & 0xf;
463 const int rowBg1 = (rowB[k] >> 4) & 0xf;
464 const int rowAb0 = (rowA[j] >> 8) & 0xf;
465 const int rowAb1 = (rowA[k] >> 8) & 0xf;
466 const int rowBb0 = (rowB[j] >> 8) & 0xf;
467 const int rowBb1 = (rowB[k] >> 8) & 0xf;
468 const int rowAa0 = (rowA[j] >> 12) & 0xf;
469 const int rowAa1 = (rowA[k] >> 12) & 0xf;
470 const int rowBa0 = (rowB[j] >> 12) & 0xf;
471 const int rowBa1 = (rowB[k] >> 12) & 0xf;
472 const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
473 const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
474 const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
475 const int alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
476 dst[i] = (alpha << 12) | (blue << 8) | (green << 4) | red;
477 }
478 }
479 else if (datatype == DTYPE_USHORT_1_5_5_5_REV && comps == 4) {
480 uint i, j, k;
481 const ushort *rowA = (const ushort *) srcRowA;
482 const ushort *rowB = (const ushort *) srcRowB;
483 ushort *dst = (ushort *) dstRow;
484 for (i = j = 0, k = k0; i < (uint) dstWidth;
485 i++, j += colStride, k += colStride) {
486 const int rowAr0 = rowA[j] & 0x1f;
487 const int rowAr1 = rowA[k] & 0x1f;
488 const int rowBr0 = rowB[j] & 0x1f;
489 const int rowBr1 = rowB[k] & 0x1f;
490 const int rowAg0 = (rowA[j] >> 5) & 0x1f;
491 const int rowAg1 = (rowA[k] >> 5) & 0x1f;
492 const int rowBg0 = (rowB[j] >> 5) & 0x1f;
493 const int rowBg1 = (rowB[k] >> 5) & 0x1f;
494 const int rowAb0 = (rowA[j] >> 10) & 0x1f;
495 const int rowAb1 = (rowA[k] >> 10) & 0x1f;
496 const int rowBb0 = (rowB[j] >> 10) & 0x1f;
497 const int rowBb1 = (rowB[k] >> 10) & 0x1f;
498 const int rowAa0 = (rowA[j] >> 15) & 0x1;
499 const int rowAa1 = (rowA[k] >> 15) & 0x1;
500 const int rowBa0 = (rowB[j] >> 15) & 0x1;
501 const int rowBa1 = (rowB[k] >> 15) & 0x1;
502 const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
503 const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
504 const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
505 const int alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
506 dst[i] = (alpha << 15) | (blue << 10) | (green << 5) | red;
507 }
508 }
509 else if (datatype == DTYPE_UBYTE_3_3_2 && comps == 3) {
510 uint i, j, k;
511 const ubyte *rowA = (const ubyte *) srcRowA;
512 const ubyte *rowB = (const ubyte *) srcRowB;
513 ubyte *dst = (ubyte *) dstRow;
514 for (i = j = 0, k = k0; i < (uint) dstWidth;
515 i++, j += colStride, k += colStride) {
516 const int rowAr0 = rowA[j] & 0x3;
517 const int rowAr1 = rowA[k] & 0x3;
518 const int rowBr0 = rowB[j] & 0x3;
519 const int rowBr1 = rowB[k] & 0x3;
520 const int rowAg0 = (rowA[j] >> 2) & 0x7;
521 const int rowAg1 = (rowA[k] >> 2) & 0x7;
522 const int rowBg0 = (rowB[j] >> 2) & 0x7;
523 const int rowBg1 = (rowB[k] >> 2) & 0x7;
524 const int rowAb0 = (rowA[j] >> 5) & 0x7;
525 const int rowAb1 = (rowA[k] >> 5) & 0x7;
526 const int rowBb0 = (rowB[j] >> 5) & 0x7;
527 const int rowBb1 = (rowB[k] >> 5) & 0x7;
528 const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
529 const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
530 const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
531 dst[i] = (blue << 5) | (green << 2) | red;
532 }
533 }
534 else {
535 debug_printf("bad format in do_row()");
536 }
537 }
538
539
540 /**
541 * Average together four rows of a source image to produce a single new
542 * row in the dest image. It's legal for the two source rows to point
543 * to the same data. The source width must be equal to either the
544 * dest width or two times the dest width.
545 *
546 * \param datatype GL pixel type \c GL_UNSIGNED_BYTE, \c GL_UNSIGNED_SHORT,
547 * \c GL_FLOAT, etc.
548 * \param comps number of components per pixel (1..4)
549 * \param srcWidth Width of a row in the source data
550 * \param srcRowA Pointer to one of the rows of source data
551 * \param srcRowB Pointer to one of the rows of source data
552 * \param srcRowC Pointer to one of the rows of source data
553 * \param srcRowD Pointer to one of the rows of source data
554 * \param dstWidth Width of a row in the destination data
555 * \param srcRowA Pointer to the row of destination data
556 */
557 static void
558 do_row_3D(enum dtype datatype, uint comps, int srcWidth,
559 const void *srcRowA, const void *srcRowB,
560 const void *srcRowC, const void *srcRowD,
561 int dstWidth, void *dstRow)
562 {
563 const uint k0 = (srcWidth == dstWidth) ? 0 : 1;
564 const uint colStride = (srcWidth == dstWidth) ? 1 : 2;
565 uint i, j, k;
566
567 assert(comps >= 1);
568 assert(comps <= 4);
569
570 if ((datatype == DTYPE_UBYTE) && (comps == 4)) {
571 DECLARE_ROW_POINTERS(ubyte, 4);
572
573 for (i = j = 0, k = k0; i < (uint) dstWidth;
574 i++, j += colStride, k += colStride) {
575 FILTER_3D(0);
576 FILTER_3D(1);
577 FILTER_3D(2);
578 FILTER_3D(3);
579 }
580 }
581 else if ((datatype == DTYPE_UBYTE) && (comps == 3)) {
582 DECLARE_ROW_POINTERS(ubyte, 3);
583
584 for (i = j = 0, k = k0; i < (uint) dstWidth;
585 i++, j += colStride, k += colStride) {
586 FILTER_3D(0);
587 FILTER_3D(1);
588 FILTER_3D(2);
589 }
590 }
591 else if ((datatype == DTYPE_UBYTE) && (comps == 2)) {
592 DECLARE_ROW_POINTERS(ubyte, 2);
593
594 for (i = j = 0, k = k0; i < (uint) dstWidth;
595 i++, j += colStride, k += colStride) {
596 FILTER_3D(0);
597 FILTER_3D(1);
598 }
599 }
600 else if ((datatype == DTYPE_UBYTE) && (comps == 1)) {
601 DECLARE_ROW_POINTERS(ubyte, 1);
602
603 for (i = j = 0, k = k0; i < (uint) dstWidth;
604 i++, j += colStride, k += colStride) {
605 FILTER_3D(0);
606 }
607 }
608 else if ((datatype == DTYPE_USHORT) && (comps == 4)) {
609 DECLARE_ROW_POINTERS(ushort, 4);
610
611 for (i = j = 0, k = k0; i < (uint) dstWidth;
612 i++, j += colStride, k += colStride) {
613 FILTER_3D(0);
614 FILTER_3D(1);
615 FILTER_3D(2);
616 FILTER_3D(3);
617 }
618 }
619 else if ((datatype == DTYPE_USHORT) && (comps == 3)) {
620 DECLARE_ROW_POINTERS(ushort, 3);
621
622 for (i = j = 0, k = k0; i < (uint) dstWidth;
623 i++, j += colStride, k += colStride) {
624 FILTER_3D(0);
625 FILTER_3D(1);
626 FILTER_3D(2);
627 }
628 }
629 else if ((datatype == DTYPE_USHORT) && (comps == 2)) {
630 DECLARE_ROW_POINTERS(ushort, 2);
631
632 for (i = j = 0, k = k0; i < (uint) dstWidth;
633 i++, j += colStride, k += colStride) {
634 FILTER_3D(0);
635 FILTER_3D(1);
636 }
637 }
638 else if ((datatype == DTYPE_USHORT) && (comps == 1)) {
639 DECLARE_ROW_POINTERS(ushort, 1);
640
641 for (i = j = 0, k = k0; i < (uint) dstWidth;
642 i++, j += colStride, k += colStride) {
643 FILTER_3D(0);
644 }
645 }
646 else if ((datatype == DTYPE_FLOAT) && (comps == 4)) {
647 DECLARE_ROW_POINTERS(float, 4);
648
649 for (i = j = 0, k = k0; i < (uint) dstWidth;
650 i++, j += colStride, k += colStride) {
651 FILTER_F_3D(0);
652 FILTER_F_3D(1);
653 FILTER_F_3D(2);
654 FILTER_F_3D(3);
655 }
656 }
657 else if ((datatype == DTYPE_FLOAT) && (comps == 3)) {
658 DECLARE_ROW_POINTERS(float, 3);
659
660 for (i = j = 0, k = k0; i < (uint) dstWidth;
661 i++, j += colStride, k += colStride) {
662 FILTER_F_3D(0);
663 FILTER_F_3D(1);
664 FILTER_F_3D(2);
665 }
666 }
667 else if ((datatype == DTYPE_FLOAT) && (comps == 2)) {
668 DECLARE_ROW_POINTERS(float, 2);
669
670 for (i = j = 0, k = k0; i < (uint) dstWidth;
671 i++, j += colStride, k += colStride) {
672 FILTER_F_3D(0);
673 FILTER_F_3D(1);
674 }
675 }
676 else if ((datatype == DTYPE_FLOAT) && (comps == 1)) {
677 DECLARE_ROW_POINTERS(float, 1);
678
679 for (i = j = 0, k = k0; i < (uint) dstWidth;
680 i++, j += colStride, k += colStride) {
681 FILTER_F_3D(0);
682 }
683 }
684 else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 4)) {
685 DECLARE_ROW_POINTERS(half_float, 4);
686
687 for (i = j = 0, k = k0; i < (uint) dstWidth;
688 i++, j += colStride, k += colStride) {
689 FILTER_HF_3D(0);
690 FILTER_HF_3D(1);
691 FILTER_HF_3D(2);
692 FILTER_HF_3D(3);
693 }
694 }
695 else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 3)) {
696 DECLARE_ROW_POINTERS(half_float, 4);
697
698 for (i = j = 0, k = k0; i < (uint) dstWidth;
699 i++, j += colStride, k += colStride) {
700 FILTER_HF_3D(0);
701 FILTER_HF_3D(1);
702 FILTER_HF_3D(2);
703 }
704 }
705 else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 2)) {
706 DECLARE_ROW_POINTERS(half_float, 4);
707
708 for (i = j = 0, k = k0; i < (uint) dstWidth;
709 i++, j += colStride, k += colStride) {
710 FILTER_HF_3D(0);
711 FILTER_HF_3D(1);
712 }
713 }
714 else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 1)) {
715 DECLARE_ROW_POINTERS(half_float, 4);
716
717 for (i = j = 0, k = k0; i < (uint) dstWidth;
718 i++, j += colStride, k += colStride) {
719 FILTER_HF_3D(0);
720 }
721 }
722 else if ((datatype == DTYPE_UINT) && (comps == 1)) {
723 const uint *rowA = (const uint *) srcRowA;
724 const uint *rowB = (const uint *) srcRowB;
725 const uint *rowC = (const uint *) srcRowC;
726 const uint *rowD = (const uint *) srcRowD;
727 float *dst = (float *) dstRow;
728
729 for (i = j = 0, k = k0; i < (uint) dstWidth;
730 i++, j += colStride, k += colStride) {
731 const uint64_t tmp = (((uint64_t) rowA[j] + (uint64_t) rowA[k])
732 + ((uint64_t) rowB[j] + (uint64_t) rowB[k])
733 + ((uint64_t) rowC[j] + (uint64_t) rowC[k])
734 + ((uint64_t) rowD[j] + (uint64_t) rowD[k]));
735 dst[i] = (float)((double) tmp * 0.125);
736 }
737 }
738 else if ((datatype == DTYPE_USHORT_5_6_5) && (comps == 3)) {
739 DECLARE_ROW_POINTERS0(ushort);
740
741 for (i = j = 0, k = k0; i < (uint) dstWidth;
742 i++, j += colStride, k += colStride) {
743 const int rowAr0 = rowA[j] & 0x1f;
744 const int rowAr1 = rowA[k] & 0x1f;
745 const int rowBr0 = rowB[j] & 0x1f;
746 const int rowBr1 = rowB[k] & 0x1f;
747 const int rowCr0 = rowC[j] & 0x1f;
748 const int rowCr1 = rowC[k] & 0x1f;
749 const int rowDr0 = rowD[j] & 0x1f;
750 const int rowDr1 = rowD[k] & 0x1f;
751 const int rowAg0 = (rowA[j] >> 5) & 0x3f;
752 const int rowAg1 = (rowA[k] >> 5) & 0x3f;
753 const int rowBg0 = (rowB[j] >> 5) & 0x3f;
754 const int rowBg1 = (rowB[k] >> 5) & 0x3f;
755 const int rowCg0 = (rowC[j] >> 5) & 0x3f;
756 const int rowCg1 = (rowC[k] >> 5) & 0x3f;
757 const int rowDg0 = (rowD[j] >> 5) & 0x3f;
758 const int rowDg1 = (rowD[k] >> 5) & 0x3f;
759 const int rowAb0 = (rowA[j] >> 11) & 0x1f;
760 const int rowAb1 = (rowA[k] >> 11) & 0x1f;
761 const int rowBb0 = (rowB[j] >> 11) & 0x1f;
762 const int rowBb1 = (rowB[k] >> 11) & 0x1f;
763 const int rowCb0 = (rowC[j] >> 11) & 0x1f;
764 const int rowCb1 = (rowC[k] >> 11) & 0x1f;
765 const int rowDb0 = (rowD[j] >> 11) & 0x1f;
766 const int rowDb1 = (rowD[k] >> 11) & 0x1f;
767 const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
768 rowCr0, rowCr1, rowDr0, rowDr1);
769 const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
770 rowCg0, rowCg1, rowDg0, rowDg1);
771 const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
772 rowCb0, rowCb1, rowDb0, rowDb1);
773 dst[i] = (b << 11) | (g << 5) | r;
774 }
775 }
776 else if ((datatype == DTYPE_USHORT_4_4_4_4) && (comps == 4)) {
777 DECLARE_ROW_POINTERS0(ushort);
778
779 for (i = j = 0, k = k0; i < (uint) dstWidth;
780 i++, j += colStride, k += colStride) {
781 const int rowAr0 = rowA[j] & 0xf;
782 const int rowAr1 = rowA[k] & 0xf;
783 const int rowBr0 = rowB[j] & 0xf;
784 const int rowBr1 = rowB[k] & 0xf;
785 const int rowCr0 = rowC[j] & 0xf;
786 const int rowCr1 = rowC[k] & 0xf;
787 const int rowDr0 = rowD[j] & 0xf;
788 const int rowDr1 = rowD[k] & 0xf;
789 const int rowAg0 = (rowA[j] >> 4) & 0xf;
790 const int rowAg1 = (rowA[k] >> 4) & 0xf;
791 const int rowBg0 = (rowB[j] >> 4) & 0xf;
792 const int rowBg1 = (rowB[k] >> 4) & 0xf;
793 const int rowCg0 = (rowC[j] >> 4) & 0xf;
794 const int rowCg1 = (rowC[k] >> 4) & 0xf;
795 const int rowDg0 = (rowD[j] >> 4) & 0xf;
796 const int rowDg1 = (rowD[k] >> 4) & 0xf;
797 const int rowAb0 = (rowA[j] >> 8) & 0xf;
798 const int rowAb1 = (rowA[k] >> 8) & 0xf;
799 const int rowBb0 = (rowB[j] >> 8) & 0xf;
800 const int rowBb1 = (rowB[k] >> 8) & 0xf;
801 const int rowCb0 = (rowC[j] >> 8) & 0xf;
802 const int rowCb1 = (rowC[k] >> 8) & 0xf;
803 const int rowDb0 = (rowD[j] >> 8) & 0xf;
804 const int rowDb1 = (rowD[k] >> 8) & 0xf;
805 const int rowAa0 = (rowA[j] >> 12) & 0xf;
806 const int rowAa1 = (rowA[k] >> 12) & 0xf;
807 const int rowBa0 = (rowB[j] >> 12) & 0xf;
808 const int rowBa1 = (rowB[k] >> 12) & 0xf;
809 const int rowCa0 = (rowC[j] >> 12) & 0xf;
810 const int rowCa1 = (rowC[k] >> 12) & 0xf;
811 const int rowDa0 = (rowD[j] >> 12) & 0xf;
812 const int rowDa1 = (rowD[k] >> 12) & 0xf;
813 const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
814 rowCr0, rowCr1, rowDr0, rowDr1);
815 const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
816 rowCg0, rowCg1, rowDg0, rowDg1);
817 const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
818 rowCb0, rowCb1, rowDb0, rowDb1);
819 const int a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
820 rowCa0, rowCa1, rowDa0, rowDa1);
821
822 dst[i] = (a << 12) | (b << 8) | (g << 4) | r;
823 }
824 }
825 else if ((datatype == DTYPE_USHORT_1_5_5_5_REV) && (comps == 4)) {
826 DECLARE_ROW_POINTERS0(ushort);
827
828 for (i = j = 0, k = k0; i < (uint) dstWidth;
829 i++, j += colStride, k += colStride) {
830 const int rowAr0 = rowA[j] & 0x1f;
831 const int rowAr1 = rowA[k] & 0x1f;
832 const int rowBr0 = rowB[j] & 0x1f;
833 const int rowBr1 = rowB[k] & 0x1f;
834 const int rowCr0 = rowC[j] & 0x1f;
835 const int rowCr1 = rowC[k] & 0x1f;
836 const int rowDr0 = rowD[j] & 0x1f;
837 const int rowDr1 = rowD[k] & 0x1f;
838 const int rowAg0 = (rowA[j] >> 5) & 0x1f;
839 const int rowAg1 = (rowA[k] >> 5) & 0x1f;
840 const int rowBg0 = (rowB[j] >> 5) & 0x1f;
841 const int rowBg1 = (rowB[k] >> 5) & 0x1f;
842 const int rowCg0 = (rowC[j] >> 5) & 0x1f;
843 const int rowCg1 = (rowC[k] >> 5) & 0x1f;
844 const int rowDg0 = (rowD[j] >> 5) & 0x1f;
845 const int rowDg1 = (rowD[k] >> 5) & 0x1f;
846 const int rowAb0 = (rowA[j] >> 10) & 0x1f;
847 const int rowAb1 = (rowA[k] >> 10) & 0x1f;
848 const int rowBb0 = (rowB[j] >> 10) & 0x1f;
849 const int rowBb1 = (rowB[k] >> 10) & 0x1f;
850 const int rowCb0 = (rowC[j] >> 10) & 0x1f;
851 const int rowCb1 = (rowC[k] >> 10) & 0x1f;
852 const int rowDb0 = (rowD[j] >> 10) & 0x1f;
853 const int rowDb1 = (rowD[k] >> 10) & 0x1f;
854 const int rowAa0 = (rowA[j] >> 15) & 0x1;
855 const int rowAa1 = (rowA[k] >> 15) & 0x1;
856 const int rowBa0 = (rowB[j] >> 15) & 0x1;
857 const int rowBa1 = (rowB[k] >> 15) & 0x1;
858 const int rowCa0 = (rowC[j] >> 15) & 0x1;
859 const int rowCa1 = (rowC[k] >> 15) & 0x1;
860 const int rowDa0 = (rowD[j] >> 15) & 0x1;
861 const int rowDa1 = (rowD[k] >> 15) & 0x1;
862 const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
863 rowCr0, rowCr1, rowDr0, rowDr1);
864 const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
865 rowCg0, rowCg1, rowDg0, rowDg1);
866 const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
867 rowCb0, rowCb1, rowDb0, rowDb1);
868 const int a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
869 rowCa0, rowCa1, rowDa0, rowDa1);
870
871 dst[i] = (a << 15) | (b << 10) | (g << 5) | r;
872 }
873 }
874 else if ((datatype == DTYPE_UBYTE_3_3_2) && (comps == 3)) {
875 DECLARE_ROW_POINTERS0(ushort);
876
877 for (i = j = 0, k = k0; i < (uint) dstWidth;
878 i++, j += colStride, k += colStride) {
879 const int rowAr0 = rowA[j] & 0x3;
880 const int rowAr1 = rowA[k] & 0x3;
881 const int rowBr0 = rowB[j] & 0x3;
882 const int rowBr1 = rowB[k] & 0x3;
883 const int rowCr0 = rowC[j] & 0x3;
884 const int rowCr1 = rowC[k] & 0x3;
885 const int rowDr0 = rowD[j] & 0x3;
886 const int rowDr1 = rowD[k] & 0x3;
887 const int rowAg0 = (rowA[j] >> 2) & 0x7;
888 const int rowAg1 = (rowA[k] >> 2) & 0x7;
889 const int rowBg0 = (rowB[j] >> 2) & 0x7;
890 const int rowBg1 = (rowB[k] >> 2) & 0x7;
891 const int rowCg0 = (rowC[j] >> 2) & 0x7;
892 const int rowCg1 = (rowC[k] >> 2) & 0x7;
893 const int rowDg0 = (rowD[j] >> 2) & 0x7;
894 const int rowDg1 = (rowD[k] >> 2) & 0x7;
895 const int rowAb0 = (rowA[j] >> 5) & 0x7;
896 const int rowAb1 = (rowA[k] >> 5) & 0x7;
897 const int rowBb0 = (rowB[j] >> 5) & 0x7;
898 const int rowBb1 = (rowB[k] >> 5) & 0x7;
899 const int rowCb0 = (rowC[j] >> 5) & 0x7;
900 const int rowCb1 = (rowC[k] >> 5) & 0x7;
901 const int rowDb0 = (rowD[j] >> 5) & 0x7;
902 const int rowDb1 = (rowD[k] >> 5) & 0x7;
903 const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
904 rowCr0, rowCr1, rowDr0, rowDr1);
905 const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
906 rowCg0, rowCg1, rowDg0, rowDg1);
907 const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
908 rowCb0, rowCb1, rowDb0, rowDb1);
909 dst[i] = (b << 5) | (g << 2) | r;
910 }
911 }
912 else {
913 debug_printf("bad format in do_row_3D()");
914 }
915 }
916
917
918
919 static void
920 format_to_type_comps(enum pipe_format pformat,
921 enum dtype *datatype, uint *comps)
922 {
923 /* XXX I think this could be implemented in terms of the pf_*() functions */
924 switch (pformat) {
925 case PIPE_FORMAT_A8R8G8B8_UNORM:
926 case PIPE_FORMAT_X8R8G8B8_UNORM:
927 case PIPE_FORMAT_B8G8R8A8_UNORM:
928 case PIPE_FORMAT_B8G8R8X8_UNORM:
929 case PIPE_FORMAT_R8G8B8A8_SRGB:
930 case PIPE_FORMAT_R8G8B8X8_SRGB:
931 case PIPE_FORMAT_A8R8G8B8_SRGB:
932 case PIPE_FORMAT_X8R8G8B8_SRGB:
933 case PIPE_FORMAT_B8G8R8A8_SRGB:
934 case PIPE_FORMAT_B8G8R8X8_SRGB:
935 case PIPE_FORMAT_R8G8B8_SRGB:
936 *datatype = DTYPE_UBYTE;
937 *comps = 4;
938 return;
939 case PIPE_FORMAT_A1R5G5B5_UNORM:
940 *datatype = DTYPE_USHORT_1_5_5_5_REV;
941 *comps = 4;
942 return;
943 case PIPE_FORMAT_A4R4G4B4_UNORM:
944 *datatype = DTYPE_USHORT_4_4_4_4;
945 *comps = 4;
946 return;
947 case PIPE_FORMAT_R5G6B5_UNORM:
948 *datatype = DTYPE_USHORT_5_6_5;
949 *comps = 3;
950 return;
951 case PIPE_FORMAT_L8_UNORM:
952 case PIPE_FORMAT_L8_SRGB:
953 case PIPE_FORMAT_A8_UNORM:
954 case PIPE_FORMAT_I8_UNORM:
955 *datatype = DTYPE_UBYTE;
956 *comps = 1;
957 return;
958 case PIPE_FORMAT_A8L8_UNORM:
959 case PIPE_FORMAT_A8L8_SRGB:
960 *datatype = DTYPE_UBYTE;
961 *comps = 2;
962 return;
963 default:
964 assert(0);
965 *datatype = DTYPE_UBYTE;
966 *comps = 0;
967 break;
968 }
969 }
970
971
972 static void
973 reduce_1d(enum pipe_format pformat,
974 int srcWidth, const ubyte *srcPtr,
975 int dstWidth, ubyte *dstPtr)
976 {
977 enum dtype datatype;
978 uint comps;
979
980 format_to_type_comps(pformat, &datatype, &comps);
981
982 /* we just duplicate the input row, kind of hack, saves code */
983 do_row(datatype, comps,
984 srcWidth, srcPtr, srcPtr,
985 dstWidth, dstPtr);
986 }
987
988
989 /**
990 * Strides are in bytes. If zero, it'll be computed as width * bpp.
991 */
992 static void
993 reduce_2d(enum pipe_format pformat,
994 int srcWidth, int srcHeight,
995 int srcRowStride, const ubyte *srcPtr,
996 int dstWidth, int dstHeight,
997 int dstRowStride, ubyte *dstPtr)
998 {
999 enum dtype datatype;
1000 uint comps;
1001 const int bpt = util_format_get_blocksize(pformat);
1002 const ubyte *srcA, *srcB;
1003 ubyte *dst;
1004 int row;
1005
1006 format_to_type_comps(pformat, &datatype, &comps);
1007
1008 if (!srcRowStride)
1009 srcRowStride = bpt * srcWidth;
1010
1011 if (!dstRowStride)
1012 dstRowStride = bpt * dstWidth;
1013
1014 /* Compute src and dst pointers */
1015 srcA = srcPtr;
1016 if (srcHeight > 1)
1017 srcB = srcA + srcRowStride;
1018 else
1019 srcB = srcA;
1020 dst = dstPtr;
1021
1022 for (row = 0; row < dstHeight; row++) {
1023 do_row(datatype, comps,
1024 srcWidth, srcA, srcB,
1025 dstWidth, dst);
1026 srcA += 2 * srcRowStride;
1027 srcB += 2 * srcRowStride;
1028 dst += dstRowStride;
1029 }
1030 }
1031
1032
1033 static void
1034 reduce_3d(enum pipe_format pformat,
1035 int srcWidth, int srcHeight, int srcDepth,
1036 int srcRowStride, const ubyte *srcPtr,
1037 int dstWidth, int dstHeight, int dstDepth,
1038 int dstRowStride, ubyte *dstPtr)
1039 {
1040 const int bpt = util_format_get_blocksize(pformat);
1041 const int border = 0;
1042 int img, row;
1043 int bytesPerSrcImage, bytesPerDstImage;
1044 int bytesPerSrcRow, bytesPerDstRow;
1045 int srcImageOffset, srcRowOffset;
1046 enum dtype datatype;
1047 uint comps;
1048
1049 format_to_type_comps(pformat, &datatype, &comps);
1050
1051 bytesPerSrcImage = srcWidth * srcHeight * bpt;
1052 bytesPerDstImage = dstWidth * dstHeight * bpt;
1053
1054 bytesPerSrcRow = srcWidth * bpt;
1055 bytesPerDstRow = dstWidth * bpt;
1056
1057 /* Offset between adjacent src images to be averaged together */
1058 srcImageOffset = (srcDepth == dstDepth) ? 0 : bytesPerSrcImage;
1059
1060 /* Offset between adjacent src rows to be averaged together */
1061 srcRowOffset = (srcHeight == dstHeight) ? 0 : srcWidth * bpt;
1062
1063 /*
1064 * Need to average together up to 8 src pixels for each dest pixel.
1065 * Break that down into 3 operations:
1066 * 1. take two rows from source image and average them together.
1067 * 2. take two rows from next source image and average them together.
1068 * 3. take the two averaged rows and average them for the final dst row.
1069 */
1070
1071 /*
1072 _mesa_printf("mip3d %d x %d x %d -> %d x %d x %d\n",
1073 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
1074 */
1075
1076 for (img = 0; img < dstDepth; img++) {
1077 /* first source image pointer, skipping border */
1078 const ubyte *imgSrcA = srcPtr
1079 + (bytesPerSrcImage + bytesPerSrcRow + border) * bpt * border
1080 + img * (bytesPerSrcImage + srcImageOffset);
1081 /* second source image pointer, skipping border */
1082 const ubyte *imgSrcB = imgSrcA + srcImageOffset;
1083 /* address of the dest image, skipping border */
1084 ubyte *imgDst = dstPtr
1085 + (bytesPerDstImage + bytesPerDstRow + border) * bpt * border
1086 + img * bytesPerDstImage;
1087
1088 /* setup the four source row pointers and the dest row pointer */
1089 const ubyte *srcImgARowA = imgSrcA;
1090 const ubyte *srcImgARowB = imgSrcA + srcRowOffset;
1091 const ubyte *srcImgBRowA = imgSrcB;
1092 const ubyte *srcImgBRowB = imgSrcB + srcRowOffset;
1093 ubyte *dstImgRow = imgDst;
1094
1095 for (row = 0; row < dstHeight; row++) {
1096 do_row_3D(datatype, comps, srcWidth,
1097 srcImgARowA, srcImgARowB,
1098 srcImgBRowA, srcImgBRowB,
1099 dstWidth, dstImgRow);
1100
1101 /* advance to next rows */
1102 srcImgARowA += bytesPerSrcRow + srcRowOffset;
1103 srcImgARowB += bytesPerSrcRow + srcRowOffset;
1104 srcImgBRowA += bytesPerSrcRow + srcRowOffset;
1105 srcImgBRowB += bytesPerSrcRow + srcRowOffset;
1106 dstImgRow += bytesPerDstRow;
1107 }
1108 }
1109 }
1110
1111
1112
1113
1114 static void
1115 make_1d_mipmap(struct gen_mipmap_state *ctx,
1116 struct pipe_texture *pt,
1117 uint face, uint baseLevel, uint lastLevel)
1118 {
1119 struct pipe_context *pipe = ctx->pipe;
1120 struct pipe_screen *screen = pipe->screen;
1121 const uint zslice = 0;
1122 uint dstLevel;
1123
1124 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
1125 const uint srcLevel = dstLevel - 1;
1126 struct pipe_transfer *srcTrans, *dstTrans;
1127 void *srcMap, *dstMap;
1128
1129 srcTrans = screen->get_tex_transfer(screen, pt, face, srcLevel, zslice,
1130 PIPE_TRANSFER_READ, 0, 0,
1131 u_minify(pt->width0, srcLevel),
1132 u_minify(pt->height0, srcLevel));
1133 dstTrans = screen->get_tex_transfer(screen, pt, face, dstLevel, zslice,
1134 PIPE_TRANSFER_WRITE, 0, 0,
1135 u_minify(pt->width0, dstLevel),
1136 u_minify(pt->height0, dstLevel));
1137
1138 srcMap = (ubyte *) screen->transfer_map(screen, srcTrans);
1139 dstMap = (ubyte *) screen->transfer_map(screen, dstTrans);
1140
1141 reduce_1d(pt->format,
1142 srcTrans->width, srcMap,
1143 dstTrans->width, dstMap);
1144
1145 screen->transfer_unmap(screen, srcTrans);
1146 screen->transfer_unmap(screen, dstTrans);
1147
1148 screen->tex_transfer_destroy(srcTrans);
1149 screen->tex_transfer_destroy(dstTrans);
1150 }
1151 }
1152
1153
1154 static void
1155 make_2d_mipmap(struct gen_mipmap_state *ctx,
1156 struct pipe_texture *pt,
1157 uint face, uint baseLevel, uint lastLevel)
1158 {
1159 struct pipe_context *pipe = ctx->pipe;
1160 struct pipe_screen *screen = pipe->screen;
1161 const uint zslice = 0;
1162 uint dstLevel;
1163
1164 assert(util_format_get_blockwidth(pt->format) == 1);
1165 assert(util_format_get_blockheight(pt->format) == 1);
1166
1167 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
1168 const uint srcLevel = dstLevel - 1;
1169 struct pipe_transfer *srcTrans, *dstTrans;
1170 ubyte *srcMap, *dstMap;
1171
1172 srcTrans = screen->get_tex_transfer(screen, pt, face, srcLevel, zslice,
1173 PIPE_TRANSFER_READ, 0, 0,
1174 u_minify(pt->width0, srcLevel),
1175 u_minify(pt->height0, srcLevel));
1176 dstTrans = screen->get_tex_transfer(screen, pt, face, dstLevel, zslice,
1177 PIPE_TRANSFER_WRITE, 0, 0,
1178 u_minify(pt->width0, dstLevel),
1179 u_minify(pt->height0, dstLevel));
1180
1181 srcMap = (ubyte *) screen->transfer_map(screen, srcTrans);
1182 dstMap = (ubyte *) screen->transfer_map(screen, dstTrans);
1183
1184 reduce_2d(pt->format,
1185 srcTrans->width, srcTrans->height,
1186 srcTrans->stride, srcMap,
1187 dstTrans->width, dstTrans->height,
1188 dstTrans->stride, dstMap);
1189
1190 screen->transfer_unmap(screen, srcTrans);
1191 screen->transfer_unmap(screen, dstTrans);
1192
1193 screen->tex_transfer_destroy(srcTrans);
1194 screen->tex_transfer_destroy(dstTrans);
1195 }
1196 }
1197
1198
1199 static void
1200 make_3d_mipmap(struct gen_mipmap_state *ctx,
1201 struct pipe_texture *pt,
1202 uint face, uint baseLevel, uint lastLevel)
1203 {
1204 #if 0
1205 struct pipe_context *pipe = ctx->pipe;
1206 struct pipe_screen *screen = pipe->screen;
1207 uint dstLevel, zslice = 0;
1208
1209 assert(util_format_get_blockwidth(pt->format) == 1);
1210 assert(util_format_get_blockheight(pt->format) == 1);
1211
1212 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
1213 const uint srcLevel = dstLevel - 1;
1214 struct pipe_transfer *srcTrans, *dstTrans;
1215 ubyte *srcMap, *dstMap;
1216
1217 srcTrans = screen->get_tex_transfer(screen, pt, face, srcLevel, zslice,
1218 PIPE_TRANSFER_READ, 0, 0,
1219 u_minify(pt->width0, srcLevel),
1220 u_minify(pt->height0, srcLevel));
1221 dstTrans = screen->get_tex_transfer(screen, pt, face, dstLevel, zslice,
1222 PIPE_TRANSFER_WRITE, 0, 0,
1223 u_minify(pt->width0, dstLevel),
1224 u_minify(pt->height0, dstLevel));
1225
1226 srcMap = (ubyte *) screen->transfer_map(screen, srcTrans);
1227 dstMap = (ubyte *) screen->transfer_map(screen, dstTrans);
1228
1229 reduce_3d(pt->format,
1230 srcTrans->width, srcTrans->height,
1231 srcTrans->stride, srcMap,
1232 dstTrans->width, dstTrans->height,
1233 dstTrans->stride, dstMap);
1234
1235 screen->transfer_unmap(screen, srcTrans);
1236 screen->transfer_unmap(screen, dstTrans);
1237
1238 screen->tex_transfer_destroy(srcTrans);
1239 screen->tex_transfer_destroy(dstTrans);
1240 }
1241 #else
1242 (void) reduce_3d;
1243 #endif
1244 }
1245
1246
1247 static void
1248 fallback_gen_mipmap(struct gen_mipmap_state *ctx,
1249 struct pipe_texture *pt,
1250 uint face, uint baseLevel, uint lastLevel)
1251 {
1252 switch (pt->target) {
1253 case PIPE_TEXTURE_1D:
1254 make_1d_mipmap(ctx, pt, face, baseLevel, lastLevel);
1255 break;
1256 case PIPE_TEXTURE_2D:
1257 case PIPE_TEXTURE_CUBE:
1258 make_2d_mipmap(ctx, pt, face, baseLevel, lastLevel);
1259 break;
1260 case PIPE_TEXTURE_3D:
1261 make_3d_mipmap(ctx, pt, face, baseLevel, lastLevel);
1262 break;
1263 default:
1264 assert(0);
1265 }
1266 }
1267
1268
1269 /**
1270 * Create a mipmap generation context.
1271 * The idea is to create one of these and re-use it each time we need to
1272 * generate a mipmap.
1273 */
1274 struct gen_mipmap_state *
1275 util_create_gen_mipmap(struct pipe_context *pipe,
1276 struct cso_context *cso)
1277 {
1278 struct gen_mipmap_state *ctx;
1279 uint i;
1280
1281 ctx = CALLOC_STRUCT(gen_mipmap_state);
1282 if (!ctx)
1283 return NULL;
1284
1285 ctx->pipe = pipe;
1286 ctx->cso = cso;
1287
1288 /* disabled blending/masking */
1289 memset(&ctx->blend, 0, sizeof(ctx->blend));
1290 ctx->blend.colormask = PIPE_MASK_RGBA;
1291
1292 /* no-op depth/stencil/alpha */
1293 memset(&ctx->depthstencil, 0, sizeof(ctx->depthstencil));
1294
1295 /* rasterizer */
1296 memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer));
1297 ctx->rasterizer.front_winding = PIPE_WINDING_CW;
1298 ctx->rasterizer.cull_mode = PIPE_WINDING_NONE;
1299 ctx->rasterizer.bypass_vs_clip_and_viewport = 1;
1300 ctx->rasterizer.gl_rasterization_rules = 1;
1301
1302 /* sampler state */
1303 memset(&ctx->sampler, 0, sizeof(ctx->sampler));
1304 ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
1305 ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
1306 ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
1307 ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
1308 ctx->sampler.normalized_coords = 1;
1309
1310 /* vertex shader - still needed to specify mapping from fragment
1311 * shader input semantics to vertex elements
1312 */
1313 {
1314 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
1315 TGSI_SEMANTIC_GENERIC };
1316 const uint semantic_indexes[] = { 0, 0 };
1317 ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
1318 semantic_indexes);
1319 }
1320
1321 /* fragment shader */
1322 ctx->fs2d = util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_2D);
1323 ctx->fsCube = util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_CUBE);
1324
1325 /* vertex data that doesn't change */
1326 for (i = 0; i < 4; i++) {
1327 ctx->vertices[i][0][2] = 0.0f; /* z */
1328 ctx->vertices[i][0][3] = 1.0f; /* w */
1329 ctx->vertices[i][1][3] = 1.0f; /* q */
1330 }
1331
1332 /* Note: the actual vertex buffer is allocated as needed below */
1333
1334 return ctx;
1335 }
1336
1337
1338 /**
1339 * Get next "slot" of vertex space in the vertex buffer.
1340 * We're allocating one large vertex buffer and using it piece by piece.
1341 */
1342 static unsigned
1343 get_next_slot(struct gen_mipmap_state *ctx)
1344 {
1345 const unsigned max_slots = 4096 / sizeof ctx->vertices;
1346
1347 if (ctx->vbuf_slot >= max_slots)
1348 util_gen_mipmap_flush( ctx );
1349
1350 if (!ctx->vbuf) {
1351 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
1352 32,
1353 PIPE_BUFFER_USAGE_VERTEX,
1354 max_slots * sizeof ctx->vertices);
1355 }
1356
1357 return ctx->vbuf_slot++ * sizeof ctx->vertices;
1358 }
1359
1360
1361 static unsigned
1362 set_vertex_data(struct gen_mipmap_state *ctx,
1363 enum pipe_texture_target tex_target,
1364 uint face, float width, float height)
1365 {
1366 unsigned offset;
1367
1368 /* vert[0].position */
1369 ctx->vertices[0][0][0] = 0.0f; /*x*/
1370 ctx->vertices[0][0][1] = 0.0f; /*y*/
1371
1372 /* vert[1].position */
1373 ctx->vertices[1][0][0] = width;
1374 ctx->vertices[1][0][1] = 0.0f;
1375
1376 /* vert[2].position */
1377 ctx->vertices[2][0][0] = width;
1378 ctx->vertices[2][0][1] = height;
1379
1380 /* vert[3].position */
1381 ctx->vertices[3][0][0] = 0.0f;
1382 ctx->vertices[3][0][1] = height;
1383
1384 /* Setup vertex texcoords. This is a little tricky for cube maps. */
1385 if (tex_target == PIPE_TEXTURE_CUBE) {
1386 static const float st[4][2] = {
1387 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
1388 };
1389
1390 util_map_texcoords2d_onto_cubemap(face, &st[0][0], 2,
1391 &ctx->vertices[0][1][0], 8);
1392 }
1393 else {
1394 /* 1D/2D */
1395 ctx->vertices[0][1][0] = 0.0f; /*s*/
1396 ctx->vertices[0][1][1] = 0.0f; /*t*/
1397 ctx->vertices[0][1][2] = 0.0f; /*r*/
1398
1399 ctx->vertices[1][1][0] = 1.0f;
1400 ctx->vertices[1][1][1] = 0.0f;
1401 ctx->vertices[1][1][2] = 0.0f;
1402
1403 ctx->vertices[2][1][0] = 1.0f;
1404 ctx->vertices[2][1][1] = 1.0f;
1405 ctx->vertices[2][1][2] = 0.0f;
1406
1407 ctx->vertices[3][1][0] = 0.0f;
1408 ctx->vertices[3][1][1] = 1.0f;
1409 ctx->vertices[3][1][2] = 0.0f;
1410 }
1411
1412 offset = get_next_slot( ctx );
1413
1414 pipe_buffer_write_nooverlap(ctx->pipe->screen, ctx->vbuf,
1415 offset, sizeof(ctx->vertices), ctx->vertices);
1416
1417 return offset;
1418 }
1419
1420
1421
1422 /**
1423 * Destroy a mipmap generation context
1424 */
1425 void
1426 util_destroy_gen_mipmap(struct gen_mipmap_state *ctx)
1427 {
1428 struct pipe_context *pipe = ctx->pipe;
1429
1430 pipe->delete_vs_state(pipe, ctx->vs);
1431 pipe->delete_fs_state(pipe, ctx->fs2d);
1432 pipe->delete_fs_state(pipe, ctx->fsCube);
1433
1434 pipe_buffer_reference(&ctx->vbuf, NULL);
1435
1436 FREE(ctx);
1437 }
1438
1439
1440
1441 /* Release vertex buffer at end of frame to avoid synchronous
1442 * rendering.
1443 */
1444 void util_gen_mipmap_flush( struct gen_mipmap_state *ctx )
1445 {
1446 pipe_buffer_reference(&ctx->vbuf, NULL);
1447 ctx->vbuf_slot = 0;
1448 }
1449
1450
1451 /**
1452 * Generate mipmap images. It's assumed all needed texture memory is
1453 * already allocated.
1454 *
1455 * \param pt the texture to generate mipmap levels for
1456 * \param face which cube face to generate mipmaps for (0 for non-cube maps)
1457 * \param baseLevel the first mipmap level to use as a src
1458 * \param lastLevel the last mipmap level to generate
1459 * \param filter the minification filter used to generate mipmap levels with
1460 * \param filter one of PIPE_TEX_FILTER_LINEAR, PIPE_TEX_FILTER_NEAREST
1461 */
1462 void
1463 util_gen_mipmap(struct gen_mipmap_state *ctx,
1464 struct pipe_texture *pt,
1465 uint face, uint baseLevel, uint lastLevel, uint filter)
1466 {
1467 struct pipe_context *pipe = ctx->pipe;
1468 struct pipe_screen *screen = pipe->screen;
1469 struct pipe_framebuffer_state fb;
1470 void *fs = (pt->target == PIPE_TEXTURE_CUBE) ? ctx->fsCube : ctx->fs2d;
1471 uint dstLevel;
1472 uint zslice = 0;
1473 uint offset;
1474
1475 /* The texture object should have room for the levels which we're
1476 * about to generate.
1477 */
1478 assert(lastLevel <= pt->last_level);
1479
1480 /* If this fails, why are we here? */
1481 assert(lastLevel > baseLevel);
1482
1483 assert(filter == PIPE_TEX_FILTER_LINEAR ||
1484 filter == PIPE_TEX_FILTER_NEAREST);
1485
1486 /* check if we can render in the texture's format */
1487 if (!screen->is_format_supported(screen, pt->format, PIPE_TEXTURE_2D,
1488 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
1489 fallback_gen_mipmap(ctx, pt, face, baseLevel, lastLevel);
1490 return;
1491 }
1492
1493 /* save state (restored below) */
1494 cso_save_blend(ctx->cso);
1495 cso_save_depth_stencil_alpha(ctx->cso);
1496 cso_save_rasterizer(ctx->cso);
1497 cso_save_samplers(ctx->cso);
1498 cso_save_sampler_textures(ctx->cso);
1499 cso_save_framebuffer(ctx->cso);
1500 cso_save_fragment_shader(ctx->cso);
1501 cso_save_vertex_shader(ctx->cso);
1502
1503 /* bind our state */
1504 cso_set_blend(ctx->cso, &ctx->blend);
1505 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
1506 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
1507
1508 cso_set_fragment_shader_handle(ctx->cso, fs);
1509 cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
1510
1511 /* init framebuffer state */
1512 memset(&fb, 0, sizeof(fb));
1513 fb.nr_cbufs = 1;
1514
1515 /* set min/mag to same filter for faster sw speed */
1516 ctx->sampler.mag_img_filter = filter;
1517 ctx->sampler.min_img_filter = filter;
1518
1519 /*
1520 * XXX for small mipmap levels, it may be faster to use the software
1521 * fallback path...
1522 */
1523 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
1524 const uint srcLevel = dstLevel - 1;
1525
1526 struct pipe_surface *surf =
1527 screen->get_tex_surface(screen, pt, face, dstLevel, zslice,
1528 PIPE_BUFFER_USAGE_GPU_WRITE);
1529
1530 /*
1531 * Setup framebuffer / dest surface
1532 */
1533 fb.cbufs[0] = surf;
1534 fb.width = u_minify(pt->width0, dstLevel);
1535 fb.height = u_minify(pt->height0, dstLevel);
1536 cso_set_framebuffer(ctx->cso, &fb);
1537
1538 /*
1539 * Setup sampler state
1540 * Note: we should only have to set the min/max LOD clamps to ensure
1541 * we grab texels from the right mipmap level. But some hardware
1542 * has trouble with min clamping so we also set the lod_bias to
1543 * try to work around that.
1544 */
1545 ctx->sampler.min_lod = ctx->sampler.max_lod = (float) srcLevel;
1546 ctx->sampler.lod_bias = (float) srcLevel;
1547 cso_single_sampler(ctx->cso, 0, &ctx->sampler);
1548 cso_single_sampler_done(ctx->cso);
1549
1550 cso_set_sampler_textures(ctx->cso, 1, &pt);
1551
1552 /* quad coords in window coords (bypassing vs, clip and viewport) */
1553 offset = set_vertex_data(ctx,
1554 pt->target,
1555 face,
1556 (float) u_minify(pt->width0, dstLevel),
1557 (float) u_minify(pt->height0, dstLevel));
1558
1559 util_draw_vertex_buffer(ctx->pipe,
1560 ctx->vbuf,
1561 offset,
1562 PIPE_PRIM_TRIANGLE_FAN,
1563 4, /* verts */
1564 2); /* attribs/vert */
1565
1566 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
1567
1568 /* need to signal that the texture has changed _after_ rendering to it */
1569 pipe_surface_reference( &surf, NULL );
1570 }
1571
1572 /* restore state we changed */
1573 cso_restore_blend(ctx->cso);
1574 cso_restore_depth_stencil_alpha(ctx->cso);
1575 cso_restore_rasterizer(ctx->cso);
1576 cso_restore_samplers(ctx->cso);
1577 cso_restore_sampler_textures(ctx->cso);
1578 cso_restore_framebuffer(ctx->cso);
1579 cso_restore_fragment_shader(ctx->cso);
1580 cso_restore_vertex_shader(ctx->cso);
1581 }