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