u_gen_mipmap.c revision 2998cad9ce0c2c60078a28e6a0f3f3bbda3a6535
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
55struct 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
66   void *vs;
67   void *fs2d, *fsCube;
68
69   struct pipe_buffer *vbuf;  /**< quad vertices */
70   unsigned vbuf_slot;
71
72   float vertices[4][2][4];   /**< vertex/texcoords for quad */
73};
74
75
76
77enum dtype
78{
79   DTYPE_UBYTE,
80   DTYPE_UBYTE_3_3_2,
81   DTYPE_USHORT,
82   DTYPE_USHORT_4_4_4_4,
83   DTYPE_USHORT_5_6_5,
84   DTYPE_USHORT_1_5_5_5_REV,
85   DTYPE_UINT,
86   DTYPE_FLOAT,
87   DTYPE_HALF_FLOAT
88};
89
90
91typedef ushort half_float;
92
93
94static half_float
95float_to_half(float f)
96{
97   /* XXX fix this */
98   return 0;
99}
100
101static float
102half_to_float(half_float h)
103{
104   /* XXX fix this */
105   return 0.0f;
106}
107
108
109
110
111/**
112 * \name Support macros for do_row and do_row_3d
113 *
114 * The macro madness is here for two reasons.  First, it compacts the code
115 * slightly.  Second, it makes it much easier to adjust the specifics of the
116 * filter to tune the rounding characteristics.
117 */
118/*@{*/
119#define DECLARE_ROW_POINTERS(t, e) \
120      const t(*rowA)[e] = (const t(*)[e]) srcRowA; \
121      const t(*rowB)[e] = (const t(*)[e]) srcRowB; \
122      const t(*rowC)[e] = (const t(*)[e]) srcRowC; \
123      const t(*rowD)[e] = (const t(*)[e]) srcRowD; \
124      t(*dst)[e] = (t(*)[e]) dstRow
125
126#define DECLARE_ROW_POINTERS0(t) \
127      const t *rowA = (const t *) srcRowA; \
128      const t *rowB = (const t *) srcRowB; \
129      const t *rowC = (const t *) srcRowC; \
130      const t *rowD = (const t *) srcRowD; \
131      t *dst = (t *) dstRow
132
133#define FILTER_SUM_3D(Aj, Ak, Bj, Bk, Cj, Ck, Dj, Dk) \
134   ((unsigned) Aj + (unsigned) Ak \
135    + (unsigned) Bj + (unsigned) Bk \
136    + (unsigned) Cj + (unsigned) Ck \
137    + (unsigned) Dj + (unsigned) Dk \
138    + 4) >> 3
139
140#define FILTER_3D(e) \
141   do { \
142      dst[i][e] = FILTER_SUM_3D(rowA[j][e], rowA[k][e], \
143                                rowB[j][e], rowB[k][e], \
144                                rowC[j][e], rowC[k][e], \
145                                rowD[j][e], rowD[k][e]); \
146   } while(0)
147
148#define FILTER_F_3D(e) \
149   do { \
150      dst[i][e] = (rowA[j][e] + rowA[k][e] \
151                   + rowB[j][e] + rowB[k][e] \
152                   + rowC[j][e] + rowC[k][e] \
153                   + rowD[j][e] + rowD[k][e]) * 0.125F; \
154   } while(0)
155
156#define FILTER_HF_3D(e) \
157   do { \
158      const float aj = half_to_float(rowA[j][e]); \
159      const float ak = half_to_float(rowA[k][e]); \
160      const float bj = half_to_float(rowB[j][e]); \
161      const float bk = half_to_float(rowB[k][e]); \
162      const float cj = half_to_float(rowC[j][e]); \
163      const float ck = half_to_float(rowC[k][e]); \
164      const float dj = half_to_float(rowD[j][e]); \
165      const float dk = half_to_float(rowD[k][e]); \
166      dst[i][e] = float_to_half((aj + ak + bj + bk + cj + ck + dj + dk) \
167                                      * 0.125F); \
168   } while(0)
169/*@}*/
170
171
172/**
173 * Average together two rows of a source image to produce a single new
174 * row in the dest image.  It's legal for the two source rows to point
175 * to the same data.  The source width must be equal to either the
176 * dest width or two times the dest width.
177 * \param datatype  GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, GL_FLOAT, etc.
178 * \param comps  number of components per pixel (1..4)
179 */
180static void
181do_row(enum dtype datatype, uint comps, int srcWidth,
182       const void *srcRowA, const void *srcRowB,
183       int dstWidth, void *dstRow)
184{
185   const uint k0 = (srcWidth == dstWidth) ? 0 : 1;
186   const uint colStride = (srcWidth == dstWidth) ? 1 : 2;
187
188   assert(comps >= 1);
189   assert(comps <= 4);
190
191   /* This assertion is no longer valid with non-power-of-2 textures
192   assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth);
193   */
194
195   if (datatype == DTYPE_UBYTE && comps == 4) {
196      uint i, j, k;
197      const ubyte(*rowA)[4] = (const ubyte(*)[4]) srcRowA;
198      const ubyte(*rowB)[4] = (const ubyte(*)[4]) srcRowB;
199      ubyte(*dst)[4] = (ubyte(*)[4]) dstRow;
200      for (i = j = 0, k = k0; i < (uint) dstWidth;
201           i++, j += colStride, k += colStride) {
202         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
203         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
204         dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
205         dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
206      }
207   }
208   else if (datatype == DTYPE_UBYTE && comps == 3) {
209      uint i, j, k;
210      const ubyte(*rowA)[3] = (const ubyte(*)[3]) srcRowA;
211      const ubyte(*rowB)[3] = (const ubyte(*)[3]) srcRowB;
212      ubyte(*dst)[3] = (ubyte(*)[3]) dstRow;
213      for (i = j = 0, k = k0; i < (uint) dstWidth;
214           i++, j += colStride, k += colStride) {
215         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
216         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
217         dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
218      }
219   }
220   else if (datatype == DTYPE_UBYTE && comps == 2) {
221      uint i, j, k;
222      const ubyte(*rowA)[2] = (const ubyte(*)[2]) srcRowA;
223      const ubyte(*rowB)[2] = (const ubyte(*)[2]) srcRowB;
224      ubyte(*dst)[2] = (ubyte(*)[2]) dstRow;
225      for (i = j = 0, k = k0; i < (uint) dstWidth;
226           i++, j += colStride, k += colStride) {
227         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) >> 2;
228         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) >> 2;
229      }
230   }
231   else if (datatype == DTYPE_UBYTE && comps == 1) {
232      uint i, j, k;
233      const ubyte *rowA = (const ubyte *) srcRowA;
234      const ubyte *rowB = (const ubyte *) srcRowB;
235      ubyte *dst = (ubyte *) dstRow;
236      for (i = j = 0, k = k0; i < (uint) dstWidth;
237           i++, j += colStride, k += colStride) {
238         dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) >> 2;
239      }
240   }
241
242   else if (datatype == DTYPE_USHORT && comps == 4) {
243      uint i, j, k;
244      const ushort(*rowA)[4] = (const ushort(*)[4]) srcRowA;
245      const ushort(*rowB)[4] = (const ushort(*)[4]) srcRowB;
246      ushort(*dst)[4] = (ushort(*)[4]) dstRow;
247      for (i = j = 0, k = k0; i < (uint) dstWidth;
248           i++, j += colStride, k += colStride) {
249         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
250         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
251         dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
252         dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
253      }
254   }
255   else if (datatype == DTYPE_USHORT && comps == 3) {
256      uint i, j, k;
257      const ushort(*rowA)[3] = (const ushort(*)[3]) srcRowA;
258      const ushort(*rowB)[3] = (const ushort(*)[3]) srcRowB;
259      ushort(*dst)[3] = (ushort(*)[3]) dstRow;
260      for (i = j = 0, k = k0; i < (uint) dstWidth;
261           i++, j += colStride, k += colStride) {
262         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
263         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
264         dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
265      }
266   }
267   else if (datatype == DTYPE_USHORT && comps == 2) {
268      uint i, j, k;
269      const ushort(*rowA)[2] = (const ushort(*)[2]) srcRowA;
270      const ushort(*rowB)[2] = (const ushort(*)[2]) srcRowB;
271      ushort(*dst)[2] = (ushort(*)[2]) dstRow;
272      for (i = j = 0, k = k0; i < (uint) dstWidth;
273           i++, j += colStride, k += colStride) {
274         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
275         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
276      }
277   }
278   else if (datatype == DTYPE_USHORT && comps == 1) {
279      uint i, j, k;
280      const ushort *rowA = (const ushort *) srcRowA;
281      const ushort *rowB = (const ushort *) srcRowB;
282      ushort *dst = (ushort *) dstRow;
283      for (i = j = 0, k = k0; i < (uint) dstWidth;
284           i++, j += colStride, k += colStride) {
285         dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) / 4;
286      }
287   }
288
289   else if (datatype == DTYPE_FLOAT && comps == 4) {
290      uint i, j, k;
291      const float(*rowA)[4] = (const float(*)[4]) srcRowA;
292      const float(*rowB)[4] = (const float(*)[4]) srcRowB;
293      float(*dst)[4] = (float(*)[4]) dstRow;
294      for (i = j = 0, k = k0; i < (uint) dstWidth;
295           i++, j += colStride, k += colStride) {
296         dst[i][0] = (rowA[j][0] + rowA[k][0] +
297                      rowB[j][0] + rowB[k][0]) * 0.25F;
298         dst[i][1] = (rowA[j][1] + rowA[k][1] +
299                      rowB[j][1] + rowB[k][1]) * 0.25F;
300         dst[i][2] = (rowA[j][2] + rowA[k][2] +
301                      rowB[j][2] + rowB[k][2]) * 0.25F;
302         dst[i][3] = (rowA[j][3] + rowA[k][3] +
303                      rowB[j][3] + rowB[k][3]) * 0.25F;
304      }
305   }
306   else if (datatype == DTYPE_FLOAT && comps == 3) {
307      uint i, j, k;
308      const float(*rowA)[3] = (const float(*)[3]) srcRowA;
309      const float(*rowB)[3] = (const float(*)[3]) srcRowB;
310      float(*dst)[3] = (float(*)[3]) dstRow;
311      for (i = j = 0, k = k0; i < (uint) dstWidth;
312           i++, j += colStride, k += colStride) {
313         dst[i][0] = (rowA[j][0] + rowA[k][0] +
314                      rowB[j][0] + rowB[k][0]) * 0.25F;
315         dst[i][1] = (rowA[j][1] + rowA[k][1] +
316                      rowB[j][1] + rowB[k][1]) * 0.25F;
317         dst[i][2] = (rowA[j][2] + rowA[k][2] +
318                      rowB[j][2] + rowB[k][2]) * 0.25F;
319      }
320   }
321   else if (datatype == DTYPE_FLOAT && comps == 2) {
322      uint i, j, k;
323      const float(*rowA)[2] = (const float(*)[2]) srcRowA;
324      const float(*rowB)[2] = (const float(*)[2]) srcRowB;
325      float(*dst)[2] = (float(*)[2]) dstRow;
326      for (i = j = 0, k = k0; i < (uint) dstWidth;
327           i++, j += colStride, k += colStride) {
328         dst[i][0] = (rowA[j][0] + rowA[k][0] +
329                      rowB[j][0] + rowB[k][0]) * 0.25F;
330         dst[i][1] = (rowA[j][1] + rowA[k][1] +
331                      rowB[j][1] + rowB[k][1]) * 0.25F;
332      }
333   }
334   else if (datatype == DTYPE_FLOAT && comps == 1) {
335      uint i, j, k;
336      const float *rowA = (const float *) srcRowA;
337      const float *rowB = (const float *) srcRowB;
338      float *dst = (float *) dstRow;
339      for (i = j = 0, k = k0; i < (uint) dstWidth;
340           i++, j += colStride, k += colStride) {
341         dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) * 0.25F;
342      }
343   }
344
345#if 0
346   else if (datatype == HALF_DTYPE_FLOAT && comps == 4) {
347      uint i, j, k, comp;
348      const half_float(*rowA)[4] = (const half_float(*)[4]) srcRowA;
349      const half_float(*rowB)[4] = (const half_float(*)[4]) srcRowB;
350      half_float(*dst)[4] = (half_float(*)[4]) dstRow;
351      for (i = j = 0, k = k0; i < (uint) dstWidth;
352           i++, j += colStride, k += colStride) {
353         for (comp = 0; comp < 4; comp++) {
354            float aj, ak, bj, bk;
355            aj = half_to_float(rowA[j][comp]);
356            ak = half_to_float(rowA[k][comp]);
357            bj = half_to_float(rowB[j][comp]);
358            bk = half_to_float(rowB[k][comp]);
359            dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F);
360         }
361      }
362   }
363   else if (datatype == DTYPE_HALF_FLOAT && comps == 3) {
364      uint i, j, k, comp;
365      const half_float(*rowA)[3] = (const half_float(*)[3]) srcRowA;
366      const half_float(*rowB)[3] = (const half_float(*)[3]) srcRowB;
367      half_float(*dst)[3] = (half_float(*)[3]) dstRow;
368      for (i = j = 0, k = k0; i < (uint) dstWidth;
369           i++, j += colStride, k += colStride) {
370         for (comp = 0; comp < 3; comp++) {
371            float aj, ak, bj, bk;
372            aj = half_to_float(rowA[j][comp]);
373            ak = half_to_float(rowA[k][comp]);
374            bj = half_to_float(rowB[j][comp]);
375            bk = half_to_float(rowB[k][comp]);
376            dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F);
377         }
378      }
379   }
380   else if (datatype == DTYPE_HALF_FLOAT && comps == 2) {
381      uint i, j, k, comp;
382      const half_float(*rowA)[2] = (const half_float(*)[2]) srcRowA;
383      const half_float(*rowB)[2] = (const half_float(*)[2]) srcRowB;
384      half_float(*dst)[2] = (half_float(*)[2]) dstRow;
385      for (i = j = 0, k = k0; i < (uint) dstWidth;
386           i++, j += colStride, k += colStride) {
387         for (comp = 0; comp < 2; comp++) {
388            float aj, ak, bj, bk;
389            aj = half_to_float(rowA[j][comp]);
390            ak = half_to_float(rowA[k][comp]);
391            bj = half_to_float(rowB[j][comp]);
392            bk = half_to_float(rowB[k][comp]);
393            dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F);
394         }
395      }
396   }
397   else if (datatype == DTYPE_HALF_FLOAT && comps == 1) {
398      uint i, j, k;
399      const half_float *rowA = (const half_float *) srcRowA;
400      const half_float *rowB = (const half_float *) srcRowB;
401      half_float *dst = (half_float *) dstRow;
402      for (i = j = 0, k = k0; i < (uint) dstWidth;
403           i++, j += colStride, k += colStride) {
404         float aj, ak, bj, bk;
405         aj = half_to_float(rowA[j]);
406         ak = half_to_float(rowA[k]);
407         bj = half_to_float(rowB[j]);
408         bk = half_to_float(rowB[k]);
409         dst[i] = float_to_half((aj + ak + bj + bk) * 0.25F);
410      }
411   }
412#endif
413
414   else if (datatype == DTYPE_UINT && comps == 1) {
415      uint i, j, k;
416      const uint *rowA = (const uint *) srcRowA;
417      const uint *rowB = (const uint *) srcRowB;
418      uint *dst = (uint *) dstRow;
419      for (i = j = 0, k = k0; i < (uint) dstWidth;
420           i++, j += colStride, k += colStride) {
421         dst[i] = rowA[j] / 4 + rowA[k] / 4 + rowB[j] / 4 + rowB[k] / 4;
422      }
423   }
424
425   else if (datatype == DTYPE_USHORT_5_6_5 && comps == 3) {
426      uint i, j, k;
427      const ushort *rowA = (const ushort *) srcRowA;
428      const ushort *rowB = (const ushort *) srcRowB;
429      ushort *dst = (ushort *) dstRow;
430      for (i = j = 0, k = k0; i < (uint) dstWidth;
431           i++, j += colStride, k += colStride) {
432         const int rowAr0 = rowA[j] & 0x1f;
433         const int rowAr1 = rowA[k] & 0x1f;
434         const int rowBr0 = rowB[j] & 0x1f;
435         const int rowBr1 = rowB[k] & 0x1f;
436         const int rowAg0 = (rowA[j] >> 5) & 0x3f;
437         const int rowAg1 = (rowA[k] >> 5) & 0x3f;
438         const int rowBg0 = (rowB[j] >> 5) & 0x3f;
439         const int rowBg1 = (rowB[k] >> 5) & 0x3f;
440         const int rowAb0 = (rowA[j] >> 11) & 0x1f;
441         const int rowAb1 = (rowA[k] >> 11) & 0x1f;
442         const int rowBb0 = (rowB[j] >> 11) & 0x1f;
443         const int rowBb1 = (rowB[k] >> 11) & 0x1f;
444         const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
445         const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
446         const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
447         dst[i] = (blue << 11) | (green << 5) | red;
448      }
449   }
450   else if (datatype == DTYPE_USHORT_4_4_4_4 && comps == 4) {
451      uint i, j, k;
452      const ushort *rowA = (const ushort *) srcRowA;
453      const ushort *rowB = (const ushort *) srcRowB;
454      ushort *dst = (ushort *) dstRow;
455      for (i = j = 0, k = k0; i < (uint) dstWidth;
456           i++, j += colStride, k += colStride) {
457         const int rowAr0 = rowA[j] & 0xf;
458         const int rowAr1 = rowA[k] & 0xf;
459         const int rowBr0 = rowB[j] & 0xf;
460         const int rowBr1 = rowB[k] & 0xf;
461         const int rowAg0 = (rowA[j] >> 4) & 0xf;
462         const int rowAg1 = (rowA[k] >> 4) & 0xf;
463         const int rowBg0 = (rowB[j] >> 4) & 0xf;
464         const int rowBg1 = (rowB[k] >> 4) & 0xf;
465         const int rowAb0 = (rowA[j] >> 8) & 0xf;
466         const int rowAb1 = (rowA[k] >> 8) & 0xf;
467         const int rowBb0 = (rowB[j] >> 8) & 0xf;
468         const int rowBb1 = (rowB[k] >> 8) & 0xf;
469         const int rowAa0 = (rowA[j] >> 12) & 0xf;
470         const int rowAa1 = (rowA[k] >> 12) & 0xf;
471         const int rowBa0 = (rowB[j] >> 12) & 0xf;
472         const int rowBa1 = (rowB[k] >> 12) & 0xf;
473         const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
474         const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
475         const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
476         const int alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
477         dst[i] = (alpha << 12) | (blue << 8) | (green << 4) | red;
478      }
479   }
480   else if (datatype == DTYPE_USHORT_1_5_5_5_REV && comps == 4) {
481      uint i, j, k;
482      const ushort *rowA = (const ushort *) srcRowA;
483      const ushort *rowB = (const ushort *) srcRowB;
484      ushort *dst = (ushort *) dstRow;
485      for (i = j = 0, k = k0; i < (uint) dstWidth;
486           i++, j += colStride, k += colStride) {
487         const int rowAr0 = rowA[j] & 0x1f;
488         const int rowAr1 = rowA[k] & 0x1f;
489         const int rowBr0 = rowB[j] & 0x1f;
490         const int rowBr1 = rowB[k] & 0x1f;
491         const int rowAg0 = (rowA[j] >> 5) & 0x1f;
492         const int rowAg1 = (rowA[k] >> 5) & 0x1f;
493         const int rowBg0 = (rowB[j] >> 5) & 0x1f;
494         const int rowBg1 = (rowB[k] >> 5) & 0x1f;
495         const int rowAb0 = (rowA[j] >> 10) & 0x1f;
496         const int rowAb1 = (rowA[k] >> 10) & 0x1f;
497         const int rowBb0 = (rowB[j] >> 10) & 0x1f;
498         const int rowBb1 = (rowB[k] >> 10) & 0x1f;
499         const int rowAa0 = (rowA[j] >> 15) & 0x1;
500         const int rowAa1 = (rowA[k] >> 15) & 0x1;
501         const int rowBa0 = (rowB[j] >> 15) & 0x1;
502         const int rowBa1 = (rowB[k] >> 15) & 0x1;
503         const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
504         const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
505         const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
506         const int alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
507         dst[i] = (alpha << 15) | (blue << 10) | (green << 5) | red;
508      }
509   }
510   else if (datatype == DTYPE_UBYTE_3_3_2 && comps == 3) {
511      uint i, j, k;
512      const ubyte *rowA = (const ubyte *) srcRowA;
513      const ubyte *rowB = (const ubyte *) srcRowB;
514      ubyte *dst = (ubyte *) dstRow;
515      for (i = j = 0, k = k0; i < (uint) dstWidth;
516           i++, j += colStride, k += colStride) {
517         const int rowAr0 = rowA[j] & 0x3;
518         const int rowAr1 = rowA[k] & 0x3;
519         const int rowBr0 = rowB[j] & 0x3;
520         const int rowBr1 = rowB[k] & 0x3;
521         const int rowAg0 = (rowA[j] >> 2) & 0x7;
522         const int rowAg1 = (rowA[k] >> 2) & 0x7;
523         const int rowBg0 = (rowB[j] >> 2) & 0x7;
524         const int rowBg1 = (rowB[k] >> 2) & 0x7;
525         const int rowAb0 = (rowA[j] >> 5) & 0x7;
526         const int rowAb1 = (rowA[k] >> 5) & 0x7;
527         const int rowBb0 = (rowB[j] >> 5) & 0x7;
528         const int rowBb1 = (rowB[k] >> 5) & 0x7;
529         const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
530         const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
531         const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
532         dst[i] = (blue << 5) | (green << 2) | red;
533      }
534   }
535   else {
536      debug_printf("bad format in do_row()");
537   }
538}
539
540
541/**
542 * Average together four rows of a source image to produce a single new
543 * row in the dest image.  It's legal for the two source rows to point
544 * to the same data.  The source width must be equal to either the
545 * dest width or two times the dest width.
546 *
547 * \param datatype  GL pixel type \c GL_UNSIGNED_BYTE, \c GL_UNSIGNED_SHORT,
548 *                  \c GL_FLOAT, etc.
549 * \param comps     number of components per pixel (1..4)
550 * \param srcWidth  Width of a row in the source data
551 * \param srcRowA   Pointer to one of the rows of source data
552 * \param srcRowB   Pointer to one of the rows of source data
553 * \param srcRowC   Pointer to one of the rows of source data
554 * \param srcRowD   Pointer to one of the rows of source data
555 * \param dstWidth  Width of a row in the destination data
556 * \param srcRowA   Pointer to the row of destination data
557 */
558static void
559do_row_3D(enum dtype datatype, uint comps, int srcWidth,
560          const void *srcRowA, const void *srcRowB,
561          const void *srcRowC, const void *srcRowD,
562          int dstWidth, void *dstRow)
563{
564   const uint k0 = (srcWidth == dstWidth) ? 0 : 1;
565   const uint colStride = (srcWidth == dstWidth) ? 1 : 2;
566   uint i, j, k;
567
568   assert(comps >= 1);
569   assert(comps <= 4);
570
571   if ((datatype == DTYPE_UBYTE) && (comps == 4)) {
572      DECLARE_ROW_POINTERS(ubyte, 4);
573
574      for (i = j = 0, k = k0; i < (uint) dstWidth;
575           i++, j += colStride, k += colStride) {
576         FILTER_3D(0);
577         FILTER_3D(1);
578         FILTER_3D(2);
579         FILTER_3D(3);
580      }
581   }
582   else if ((datatype == DTYPE_UBYTE) && (comps == 3)) {
583      DECLARE_ROW_POINTERS(ubyte, 3);
584
585      for (i = j = 0, k = k0; i < (uint) dstWidth;
586           i++, j += colStride, k += colStride) {
587         FILTER_3D(0);
588         FILTER_3D(1);
589         FILTER_3D(2);
590      }
591   }
592   else if ((datatype == DTYPE_UBYTE) && (comps == 2)) {
593      DECLARE_ROW_POINTERS(ubyte, 2);
594
595      for (i = j = 0, k = k0; i < (uint) dstWidth;
596           i++, j += colStride, k += colStride) {
597         FILTER_3D(0);
598         FILTER_3D(1);
599      }
600   }
601   else if ((datatype == DTYPE_UBYTE) && (comps == 1)) {
602      DECLARE_ROW_POINTERS(ubyte, 1);
603
604      for (i = j = 0, k = k0; i < (uint) dstWidth;
605           i++, j += colStride, k += colStride) {
606         FILTER_3D(0);
607      }
608   }
609   else if ((datatype == DTYPE_USHORT) && (comps == 4)) {
610      DECLARE_ROW_POINTERS(ushort, 4);
611
612      for (i = j = 0, k = k0; i < (uint) dstWidth;
613           i++, j += colStride, k += colStride) {
614         FILTER_3D(0);
615         FILTER_3D(1);
616         FILTER_3D(2);
617         FILTER_3D(3);
618      }
619   }
620   else if ((datatype == DTYPE_USHORT) && (comps == 3)) {
621      DECLARE_ROW_POINTERS(ushort, 3);
622
623      for (i = j = 0, k = k0; i < (uint) dstWidth;
624           i++, j += colStride, k += colStride) {
625         FILTER_3D(0);
626         FILTER_3D(1);
627         FILTER_3D(2);
628      }
629   }
630   else if ((datatype == DTYPE_USHORT) && (comps == 2)) {
631      DECLARE_ROW_POINTERS(ushort, 2);
632
633      for (i = j = 0, k = k0; i < (uint) dstWidth;
634           i++, j += colStride, k += colStride) {
635         FILTER_3D(0);
636         FILTER_3D(1);
637      }
638   }
639   else if ((datatype == DTYPE_USHORT) && (comps == 1)) {
640      DECLARE_ROW_POINTERS(ushort, 1);
641
642      for (i = j = 0, k = k0; i < (uint) dstWidth;
643           i++, j += colStride, k += colStride) {
644         FILTER_3D(0);
645      }
646   }
647   else if ((datatype == DTYPE_FLOAT) && (comps == 4)) {
648      DECLARE_ROW_POINTERS(float, 4);
649
650      for (i = j = 0, k = k0; i < (uint) dstWidth;
651           i++, j += colStride, k += colStride) {
652         FILTER_F_3D(0);
653         FILTER_F_3D(1);
654         FILTER_F_3D(2);
655         FILTER_F_3D(3);
656      }
657   }
658   else if ((datatype == DTYPE_FLOAT) && (comps == 3)) {
659      DECLARE_ROW_POINTERS(float, 3);
660
661      for (i = j = 0, k = k0; i < (uint) dstWidth;
662           i++, j += colStride, k += colStride) {
663         FILTER_F_3D(0);
664         FILTER_F_3D(1);
665         FILTER_F_3D(2);
666      }
667   }
668   else if ((datatype == DTYPE_FLOAT) && (comps == 2)) {
669      DECLARE_ROW_POINTERS(float, 2);
670
671      for (i = j = 0, k = k0; i < (uint) dstWidth;
672           i++, j += colStride, k += colStride) {
673         FILTER_F_3D(0);
674         FILTER_F_3D(1);
675      }
676   }
677   else if ((datatype == DTYPE_FLOAT) && (comps == 1)) {
678      DECLARE_ROW_POINTERS(float, 1);
679
680      for (i = j = 0, k = k0; i < (uint) dstWidth;
681           i++, j += colStride, k += colStride) {
682         FILTER_F_3D(0);
683      }
684   }
685   else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 4)) {
686      DECLARE_ROW_POINTERS(half_float, 4);
687
688      for (i = j = 0, k = k0; i < (uint) dstWidth;
689           i++, j += colStride, k += colStride) {
690         FILTER_HF_3D(0);
691         FILTER_HF_3D(1);
692         FILTER_HF_3D(2);
693         FILTER_HF_3D(3);
694      }
695   }
696   else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 3)) {
697      DECLARE_ROW_POINTERS(half_float, 4);
698
699      for (i = j = 0, k = k0; i < (uint) dstWidth;
700           i++, j += colStride, k += colStride) {
701         FILTER_HF_3D(0);
702         FILTER_HF_3D(1);
703         FILTER_HF_3D(2);
704      }
705   }
706   else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 2)) {
707      DECLARE_ROW_POINTERS(half_float, 4);
708
709      for (i = j = 0, k = k0; i < (uint) dstWidth;
710           i++, j += colStride, k += colStride) {
711         FILTER_HF_3D(0);
712         FILTER_HF_3D(1);
713      }
714   }
715   else if ((datatype == DTYPE_HALF_FLOAT) && (comps == 1)) {
716      DECLARE_ROW_POINTERS(half_float, 4);
717
718      for (i = j = 0, k = k0; i < (uint) dstWidth;
719           i++, j += colStride, k += colStride) {
720         FILTER_HF_3D(0);
721      }
722   }
723   else if ((datatype == DTYPE_UINT) && (comps == 1)) {
724      const uint *rowA = (const uint *) srcRowA;
725      const uint *rowB = (const uint *) srcRowB;
726      const uint *rowC = (const uint *) srcRowC;
727      const uint *rowD = (const uint *) srcRowD;
728      float *dst = (float *) dstRow;
729
730      for (i = j = 0, k = k0; i < (uint) dstWidth;
731           i++, j += colStride, k += colStride) {
732         const uint64_t tmp = (((uint64_t) rowA[j] + (uint64_t) rowA[k])
733                               + ((uint64_t) rowB[j] + (uint64_t) rowB[k])
734                               + ((uint64_t) rowC[j] + (uint64_t) rowC[k])
735                               + ((uint64_t) rowD[j] + (uint64_t) rowD[k]));
736         dst[i] = (float)((double) tmp * 0.125);
737      }
738   }
739   else if ((datatype == DTYPE_USHORT_5_6_5) && (comps == 3)) {
740      DECLARE_ROW_POINTERS0(ushort);
741
742      for (i = j = 0, k = k0; i < (uint) dstWidth;
743           i++, j += colStride, k += colStride) {
744         const int rowAr0 = rowA[j] & 0x1f;
745         const int rowAr1 = rowA[k] & 0x1f;
746         const int rowBr0 = rowB[j] & 0x1f;
747         const int rowBr1 = rowB[k] & 0x1f;
748         const int rowCr0 = rowC[j] & 0x1f;
749         const int rowCr1 = rowC[k] & 0x1f;
750         const int rowDr0 = rowD[j] & 0x1f;
751         const int rowDr1 = rowD[k] & 0x1f;
752         const int rowAg0 = (rowA[j] >> 5) & 0x3f;
753         const int rowAg1 = (rowA[k] >> 5) & 0x3f;
754         const int rowBg0 = (rowB[j] >> 5) & 0x3f;
755         const int rowBg1 = (rowB[k] >> 5) & 0x3f;
756         const int rowCg0 = (rowC[j] >> 5) & 0x3f;
757         const int rowCg1 = (rowC[k] >> 5) & 0x3f;
758         const int rowDg0 = (rowD[j] >> 5) & 0x3f;
759         const int rowDg1 = (rowD[k] >> 5) & 0x3f;
760         const int rowAb0 = (rowA[j] >> 11) & 0x1f;
761         const int rowAb1 = (rowA[k] >> 11) & 0x1f;
762         const int rowBb0 = (rowB[j] >> 11) & 0x1f;
763         const int rowBb1 = (rowB[k] >> 11) & 0x1f;
764         const int rowCb0 = (rowC[j] >> 11) & 0x1f;
765         const int rowCb1 = (rowC[k] >> 11) & 0x1f;
766         const int rowDb0 = (rowD[j] >> 11) & 0x1f;
767         const int rowDb1 = (rowD[k] >> 11) & 0x1f;
768         const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
769                                       rowCr0, rowCr1, rowDr0, rowDr1);
770         const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
771                                       rowCg0, rowCg1, rowDg0, rowDg1);
772         const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
773                                       rowCb0, rowCb1, rowDb0, rowDb1);
774         dst[i] = (b << 11) | (g << 5) | r;
775      }
776   }
777   else if ((datatype == DTYPE_USHORT_4_4_4_4) && (comps == 4)) {
778      DECLARE_ROW_POINTERS0(ushort);
779
780      for (i = j = 0, k = k0; i < (uint) dstWidth;
781           i++, j += colStride, k += colStride) {
782         const int rowAr0 = rowA[j] & 0xf;
783         const int rowAr1 = rowA[k] & 0xf;
784         const int rowBr0 = rowB[j] & 0xf;
785         const int rowBr1 = rowB[k] & 0xf;
786         const int rowCr0 = rowC[j] & 0xf;
787         const int rowCr1 = rowC[k] & 0xf;
788         const int rowDr0 = rowD[j] & 0xf;
789         const int rowDr1 = rowD[k] & 0xf;
790         const int rowAg0 = (rowA[j] >> 4) & 0xf;
791         const int rowAg1 = (rowA[k] >> 4) & 0xf;
792         const int rowBg0 = (rowB[j] >> 4) & 0xf;
793         const int rowBg1 = (rowB[k] >> 4) & 0xf;
794         const int rowCg0 = (rowC[j] >> 4) & 0xf;
795         const int rowCg1 = (rowC[k] >> 4) & 0xf;
796         const int rowDg0 = (rowD[j] >> 4) & 0xf;
797         const int rowDg1 = (rowD[k] >> 4) & 0xf;
798         const int rowAb0 = (rowA[j] >> 8) & 0xf;
799         const int rowAb1 = (rowA[k] >> 8) & 0xf;
800         const int rowBb0 = (rowB[j] >> 8) & 0xf;
801         const int rowBb1 = (rowB[k] >> 8) & 0xf;
802         const int rowCb0 = (rowC[j] >> 8) & 0xf;
803         const int rowCb1 = (rowC[k] >> 8) & 0xf;
804         const int rowDb0 = (rowD[j] >> 8) & 0xf;
805         const int rowDb1 = (rowD[k] >> 8) & 0xf;
806         const int rowAa0 = (rowA[j] >> 12) & 0xf;
807         const int rowAa1 = (rowA[k] >> 12) & 0xf;
808         const int rowBa0 = (rowB[j] >> 12) & 0xf;
809         const int rowBa1 = (rowB[k] >> 12) & 0xf;
810         const int rowCa0 = (rowC[j] >> 12) & 0xf;
811         const int rowCa1 = (rowC[k] >> 12) & 0xf;
812         const int rowDa0 = (rowD[j] >> 12) & 0xf;
813         const int rowDa1 = (rowD[k] >> 12) & 0xf;
814         const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
815                                       rowCr0, rowCr1, rowDr0, rowDr1);
816         const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
817                                       rowCg0, rowCg1, rowDg0, rowDg1);
818         const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
819                                       rowCb0, rowCb1, rowDb0, rowDb1);
820         const int a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
821                                       rowCa0, rowCa1, rowDa0, rowDa1);
822
823         dst[i] = (a << 12) | (b << 8) | (g << 4) | r;
824      }
825   }
826   else if ((datatype == DTYPE_USHORT_1_5_5_5_REV) && (comps == 4)) {
827      DECLARE_ROW_POINTERS0(ushort);
828
829      for (i = j = 0, k = k0; i < (uint) dstWidth;
830           i++, j += colStride, k += colStride) {
831         const int rowAr0 = rowA[j] & 0x1f;
832         const int rowAr1 = rowA[k] & 0x1f;
833         const int rowBr0 = rowB[j] & 0x1f;
834         const int rowBr1 = rowB[k] & 0x1f;
835         const int rowCr0 = rowC[j] & 0x1f;
836         const int rowCr1 = rowC[k] & 0x1f;
837         const int rowDr0 = rowD[j] & 0x1f;
838         const int rowDr1 = rowD[k] & 0x1f;
839         const int rowAg0 = (rowA[j] >> 5) & 0x1f;
840         const int rowAg1 = (rowA[k] >> 5) & 0x1f;
841         const int rowBg0 = (rowB[j] >> 5) & 0x1f;
842         const int rowBg1 = (rowB[k] >> 5) & 0x1f;
843         const int rowCg0 = (rowC[j] >> 5) & 0x1f;
844         const int rowCg1 = (rowC[k] >> 5) & 0x1f;
845         const int rowDg0 = (rowD[j] >> 5) & 0x1f;
846         const int rowDg1 = (rowD[k] >> 5) & 0x1f;
847         const int rowAb0 = (rowA[j] >> 10) & 0x1f;
848         const int rowAb1 = (rowA[k] >> 10) & 0x1f;
849         const int rowBb0 = (rowB[j] >> 10) & 0x1f;
850         const int rowBb1 = (rowB[k] >> 10) & 0x1f;
851         const int rowCb0 = (rowC[j] >> 10) & 0x1f;
852         const int rowCb1 = (rowC[k] >> 10) & 0x1f;
853         const int rowDb0 = (rowD[j] >> 10) & 0x1f;
854         const int rowDb1 = (rowD[k] >> 10) & 0x1f;
855         const int rowAa0 = (rowA[j] >> 15) & 0x1;
856         const int rowAa1 = (rowA[k] >> 15) & 0x1;
857         const int rowBa0 = (rowB[j] >> 15) & 0x1;
858         const int rowBa1 = (rowB[k] >> 15) & 0x1;
859         const int rowCa0 = (rowC[j] >> 15) & 0x1;
860         const int rowCa1 = (rowC[k] >> 15) & 0x1;
861         const int rowDa0 = (rowD[j] >> 15) & 0x1;
862         const int rowDa1 = (rowD[k] >> 15) & 0x1;
863         const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
864                                       rowCr0, rowCr1, rowDr0, rowDr1);
865         const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
866                                       rowCg0, rowCg1, rowDg0, rowDg1);
867         const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
868                                       rowCb0, rowCb1, rowDb0, rowDb1);
869         const int a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
870                                       rowCa0, rowCa1, rowDa0, rowDa1);
871
872         dst[i] = (a << 15) | (b << 10) | (g << 5) | r;
873      }
874   }
875   else if ((datatype == DTYPE_UBYTE_3_3_2) && (comps == 3)) {
876      DECLARE_ROW_POINTERS0(ushort);
877
878      for (i = j = 0, k = k0; i < (uint) dstWidth;
879           i++, j += colStride, k += colStride) {
880         const int rowAr0 = rowA[j] & 0x3;
881         const int rowAr1 = rowA[k] & 0x3;
882         const int rowBr0 = rowB[j] & 0x3;
883         const int rowBr1 = rowB[k] & 0x3;
884         const int rowCr0 = rowC[j] & 0x3;
885         const int rowCr1 = rowC[k] & 0x3;
886         const int rowDr0 = rowD[j] & 0x3;
887         const int rowDr1 = rowD[k] & 0x3;
888         const int rowAg0 = (rowA[j] >> 2) & 0x7;
889         const int rowAg1 = (rowA[k] >> 2) & 0x7;
890         const int rowBg0 = (rowB[j] >> 2) & 0x7;
891         const int rowBg1 = (rowB[k] >> 2) & 0x7;
892         const int rowCg0 = (rowC[j] >> 2) & 0x7;
893         const int rowCg1 = (rowC[k] >> 2) & 0x7;
894         const int rowDg0 = (rowD[j] >> 2) & 0x7;
895         const int rowDg1 = (rowD[k] >> 2) & 0x7;
896         const int rowAb0 = (rowA[j] >> 5) & 0x7;
897         const int rowAb1 = (rowA[k] >> 5) & 0x7;
898         const int rowBb0 = (rowB[j] >> 5) & 0x7;
899         const int rowBb1 = (rowB[k] >> 5) & 0x7;
900         const int rowCb0 = (rowC[j] >> 5) & 0x7;
901         const int rowCb1 = (rowC[k] >> 5) & 0x7;
902         const int rowDb0 = (rowD[j] >> 5) & 0x7;
903         const int rowDb1 = (rowD[k] >> 5) & 0x7;
904         const int r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
905                                       rowCr0, rowCr1, rowDr0, rowDr1);
906         const int g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
907                                       rowCg0, rowCg1, rowDg0, rowDg1);
908         const int b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
909                                       rowCb0, rowCb1, rowDb0, rowDb1);
910         dst[i] = (b << 5) | (g << 2) | r;
911      }
912   }
913   else {
914      debug_printf("bad format in do_row_3D()");
915   }
916}
917
918
919
920static void
921format_to_type_comps(enum pipe_format pformat,
922                     enum dtype *datatype, uint *comps)
923{
924   /* XXX I think this could be implemented in terms of the pf_*() functions */
925   switch (pformat) {
926   case PIPE_FORMAT_B8G8R8A8_UNORM:
927   case PIPE_FORMAT_B8G8R8X8_UNORM:
928   case PIPE_FORMAT_A8R8G8B8_UNORM:
929   case PIPE_FORMAT_X8R8G8B8_UNORM:
930   case PIPE_FORMAT_A8B8G8R8_SRGB:
931   case PIPE_FORMAT_X8B8G8R8_SRGB:
932   case PIPE_FORMAT_B8G8R8A8_SRGB:
933   case PIPE_FORMAT_B8G8R8X8_SRGB:
934   case PIPE_FORMAT_A8R8G8B8_SRGB:
935   case PIPE_FORMAT_X8R8G8B8_SRGB:
936   case PIPE_FORMAT_R8G8B8_SRGB:
937      *datatype = DTYPE_UBYTE;
938      *comps = 4;
939      return;
940   case PIPE_FORMAT_B5G5R5A1_UNORM:
941      *datatype = DTYPE_USHORT_1_5_5_5_REV;
942      *comps = 4;
943      return;
944   case PIPE_FORMAT_B4G4R4A4_UNORM:
945      *datatype = DTYPE_USHORT_4_4_4_4;
946      *comps = 4;
947      return;
948   case PIPE_FORMAT_B5G6R5_UNORM:
949      *datatype = DTYPE_USHORT_5_6_5;
950      *comps = 3;
951      return;
952   case PIPE_FORMAT_L8_UNORM:
953   case PIPE_FORMAT_L8_SRGB:
954   case PIPE_FORMAT_A8_UNORM:
955   case PIPE_FORMAT_I8_UNORM:
956      *datatype = DTYPE_UBYTE;
957      *comps = 1;
958      return;
959   case PIPE_FORMAT_L8A8_UNORM:
960   case PIPE_FORMAT_L8A8_SRGB:
961      *datatype = DTYPE_UBYTE;
962      *comps = 2;
963      return;
964   default:
965      assert(0);
966      *datatype = DTYPE_UBYTE;
967      *comps = 0;
968      break;
969   }
970}
971
972
973static void
974reduce_1d(enum pipe_format pformat,
975          int srcWidth, const ubyte *srcPtr,
976          int dstWidth, ubyte *dstPtr)
977{
978   enum dtype datatype;
979   uint comps;
980
981   format_to_type_comps(pformat, &datatype, &comps);
982
983   /* we just duplicate the input row, kind of hack, saves code */
984   do_row(datatype, comps,
985          srcWidth, srcPtr, srcPtr,
986          dstWidth, dstPtr);
987}
988
989
990/**
991 * Strides are in bytes.  If zero, it'll be computed as width * bpp.
992 */
993static void
994reduce_2d(enum pipe_format pformat,
995          int srcWidth, int srcHeight,
996          int srcRowStride, const ubyte *srcPtr,
997          int dstWidth, int dstHeight,
998          int dstRowStride, ubyte *dstPtr)
999{
1000   enum dtype datatype;
1001   uint comps;
1002   const int bpt = util_format_get_blocksize(pformat);
1003   const ubyte *srcA, *srcB;
1004   ubyte *dst;
1005   int row;
1006
1007   format_to_type_comps(pformat, &datatype, &comps);
1008
1009   if (!srcRowStride)
1010      srcRowStride = bpt * srcWidth;
1011
1012   if (!dstRowStride)
1013      dstRowStride = bpt * dstWidth;
1014
1015   /* Compute src and dst pointers */
1016   srcA = srcPtr;
1017   if (srcHeight > 1)
1018      srcB = srcA + srcRowStride;
1019   else
1020      srcB = srcA;
1021   dst = dstPtr;
1022
1023   for (row = 0; row < dstHeight; row++) {
1024      do_row(datatype, comps,
1025             srcWidth, srcA, srcB,
1026             dstWidth, dst);
1027      srcA += 2 * srcRowStride;
1028      srcB += 2 * srcRowStride;
1029      dst += dstRowStride;
1030   }
1031}
1032
1033
1034static void
1035reduce_3d(enum pipe_format pformat,
1036          int srcWidth, int srcHeight, int srcDepth,
1037          int srcRowStride, const ubyte *srcPtr,
1038          int dstWidth, int dstHeight, int dstDepth,
1039          int dstRowStride, ubyte *dstPtr)
1040{
1041   const int bpt = util_format_get_blocksize(pformat);
1042   const int border = 0;
1043   int img, row;
1044   int bytesPerSrcImage, bytesPerDstImage;
1045   int bytesPerSrcRow, bytesPerDstRow;
1046   int srcImageOffset, srcRowOffset;
1047   enum dtype datatype;
1048   uint comps;
1049
1050   format_to_type_comps(pformat, &datatype, &comps);
1051
1052   bytesPerSrcImage = srcWidth * srcHeight * bpt;
1053   bytesPerDstImage = dstWidth * dstHeight * bpt;
1054
1055   bytesPerSrcRow = srcWidth * bpt;
1056   bytesPerDstRow = dstWidth * bpt;
1057
1058   /* Offset between adjacent src images to be averaged together */
1059   srcImageOffset = (srcDepth == dstDepth) ? 0 : bytesPerSrcImage;
1060
1061   /* Offset between adjacent src rows to be averaged together */
1062   srcRowOffset = (srcHeight == dstHeight) ? 0 : srcWidth * bpt;
1063
1064   /*
1065    * Need to average together up to 8 src pixels for each dest pixel.
1066    * Break that down into 3 operations:
1067    *   1. take two rows from source image and average them together.
1068    *   2. take two rows from next source image and average them together.
1069    *   3. take the two averaged rows and average them for the final dst row.
1070    */
1071
1072   /*
1073   printf("mip3d %d x %d x %d  ->  %d x %d x %d\n",
1074          srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
1075   */
1076
1077   for (img = 0; img < dstDepth; img++) {
1078      /* first source image pointer, skipping border */
1079      const ubyte *imgSrcA = srcPtr
1080         + (bytesPerSrcImage + bytesPerSrcRow + border) * bpt * border
1081         + img * (bytesPerSrcImage + srcImageOffset);
1082      /* second source image pointer, skipping border */
1083      const ubyte *imgSrcB = imgSrcA + srcImageOffset;
1084      /* address of the dest image, skipping border */
1085      ubyte *imgDst = dstPtr
1086         + (bytesPerDstImage + bytesPerDstRow + border) * bpt * border
1087         + img * bytesPerDstImage;
1088
1089      /* setup the four source row pointers and the dest row pointer */
1090      const ubyte *srcImgARowA = imgSrcA;
1091      const ubyte *srcImgARowB = imgSrcA + srcRowOffset;
1092      const ubyte *srcImgBRowA = imgSrcB;
1093      const ubyte *srcImgBRowB = imgSrcB + srcRowOffset;
1094      ubyte *dstImgRow = imgDst;
1095
1096      for (row = 0; row < dstHeight; row++) {
1097         do_row_3D(datatype, comps, srcWidth,
1098                   srcImgARowA, srcImgARowB,
1099                   srcImgBRowA, srcImgBRowB,
1100                   dstWidth, dstImgRow);
1101
1102         /* advance to next rows */
1103         srcImgARowA += bytesPerSrcRow + srcRowOffset;
1104         srcImgARowB += bytesPerSrcRow + srcRowOffset;
1105         srcImgBRowA += bytesPerSrcRow + srcRowOffset;
1106         srcImgBRowB += bytesPerSrcRow + srcRowOffset;
1107         dstImgRow += bytesPerDstRow;
1108      }
1109   }
1110}
1111
1112
1113
1114
1115static void
1116make_1d_mipmap(struct gen_mipmap_state *ctx,
1117               struct pipe_texture *pt,
1118               uint face, uint baseLevel, uint lastLevel)
1119{
1120   struct pipe_context *pipe = ctx->pipe;
1121   struct pipe_screen *screen = pipe->screen;
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 = screen->get_tex_transfer(screen, pt, face, srcLevel, zslice,
1131                                          PIPE_TRANSFER_READ, 0, 0,
1132                                          u_minify(pt->width0, srcLevel),
1133                                          u_minify(pt->height0, srcLevel));
1134      dstTrans = screen->get_tex_transfer(screen, 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 *) screen->transfer_map(screen, srcTrans);
1140      dstMap = (ubyte *) screen->transfer_map(screen, dstTrans);
1141
1142      reduce_1d(pt->format,
1143                srcTrans->width, srcMap,
1144                dstTrans->width, dstMap);
1145
1146      screen->transfer_unmap(screen, srcTrans);
1147      screen->transfer_unmap(screen, dstTrans);
1148
1149      screen->tex_transfer_destroy(srcTrans);
1150      screen->tex_transfer_destroy(dstTrans);
1151   }
1152}
1153
1154
1155static void
1156make_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   struct pipe_screen *screen = pipe->screen;
1162   const uint zslice = 0;
1163   uint dstLevel;
1164
1165   assert(util_format_get_blockwidth(pt->format) == 1);
1166   assert(util_format_get_blockheight(pt->format) == 1);
1167
1168   for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
1169      const uint srcLevel = dstLevel - 1;
1170      struct pipe_transfer *srcTrans, *dstTrans;
1171      ubyte *srcMap, *dstMap;
1172
1173      srcTrans = screen->get_tex_transfer(screen, pt, face, srcLevel, zslice,
1174                                          PIPE_TRANSFER_READ, 0, 0,
1175                                          u_minify(pt->width0, srcLevel),
1176                                          u_minify(pt->height0, srcLevel));
1177      dstTrans = screen->get_tex_transfer(screen, pt, face, dstLevel, zslice,
1178                                          PIPE_TRANSFER_WRITE, 0, 0,
1179                                          u_minify(pt->width0, dstLevel),
1180                                          u_minify(pt->height0, dstLevel));
1181
1182      srcMap = (ubyte *) screen->transfer_map(screen, srcTrans);
1183      dstMap = (ubyte *) screen->transfer_map(screen, dstTrans);
1184
1185      reduce_2d(pt->format,
1186                srcTrans->width, srcTrans->height,
1187                srcTrans->stride, srcMap,
1188                dstTrans->width, dstTrans->height,
1189                dstTrans->stride, dstMap);
1190
1191      screen->transfer_unmap(screen, srcTrans);
1192      screen->transfer_unmap(screen, dstTrans);
1193
1194      screen->tex_transfer_destroy(srcTrans);
1195      screen->tex_transfer_destroy(dstTrans);
1196   }
1197}
1198
1199
1200static void
1201make_3d_mipmap(struct gen_mipmap_state *ctx,
1202               struct pipe_texture *pt,
1203               uint face, uint baseLevel, uint lastLevel)
1204{
1205#if 0
1206   struct pipe_context *pipe = ctx->pipe;
1207   struct pipe_screen *screen = pipe->screen;
1208   uint dstLevel, zslice = 0;
1209
1210   assert(util_format_get_blockwidth(pt->format) == 1);
1211   assert(util_format_get_blockheight(pt->format) == 1);
1212
1213   for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
1214      const uint srcLevel = dstLevel - 1;
1215      struct pipe_transfer *srcTrans, *dstTrans;
1216      ubyte *srcMap, *dstMap;
1217
1218      srcTrans = screen->get_tex_transfer(screen, pt, face, srcLevel, zslice,
1219                                          PIPE_TRANSFER_READ, 0, 0,
1220                                          u_minify(pt->width0, srcLevel),
1221                                          u_minify(pt->height0, srcLevel));
1222      dstTrans = screen->get_tex_transfer(screen, pt, face, dstLevel, zslice,
1223                                          PIPE_TRANSFER_WRITE, 0, 0,
1224                                          u_minify(pt->width0, dstLevel),
1225                                          u_minify(pt->height0, dstLevel));
1226
1227      srcMap = (ubyte *) screen->transfer_map(screen, srcTrans);
1228      dstMap = (ubyte *) screen->transfer_map(screen, dstTrans);
1229
1230      reduce_3d(pt->format,
1231                srcTrans->width, srcTrans->height,
1232                srcTrans->stride, srcMap,
1233                dstTrans->width, dstTrans->height,
1234                dstTrans->stride, dstMap);
1235
1236      screen->transfer_unmap(screen, srcTrans);
1237      screen->transfer_unmap(screen, dstTrans);
1238
1239      screen->tex_transfer_destroy(srcTrans);
1240      screen->tex_transfer_destroy(dstTrans);
1241   }
1242#else
1243   (void) reduce_3d;
1244#endif
1245}
1246
1247
1248static void
1249fallback_gen_mipmap(struct gen_mipmap_state *ctx,
1250                    struct pipe_texture *pt,
1251                    uint face, uint baseLevel, uint lastLevel)
1252{
1253   switch (pt->target) {
1254   case PIPE_TEXTURE_1D:
1255      make_1d_mipmap(ctx, pt, face, baseLevel, lastLevel);
1256      break;
1257   case PIPE_TEXTURE_2D:
1258   case PIPE_TEXTURE_CUBE:
1259      make_2d_mipmap(ctx, pt, face, baseLevel, lastLevel);
1260      break;
1261   case PIPE_TEXTURE_3D:
1262      make_3d_mipmap(ctx, pt, face, baseLevel, lastLevel);
1263      break;
1264   default:
1265      assert(0);
1266   }
1267}
1268
1269
1270/**
1271 * Create a mipmap generation context.
1272 * The idea is to create one of these and re-use it each time we need to
1273 * generate a mipmap.
1274 */
1275struct gen_mipmap_state *
1276util_create_gen_mipmap(struct pipe_context *pipe,
1277                       struct cso_context *cso)
1278{
1279   struct gen_mipmap_state *ctx;
1280   uint i;
1281
1282   ctx = CALLOC_STRUCT(gen_mipmap_state);
1283   if (!ctx)
1284      return NULL;
1285
1286   ctx->pipe = pipe;
1287   ctx->cso = cso;
1288
1289   /* disabled blending/masking */
1290   memset(&ctx->blend, 0, sizeof(ctx->blend));
1291   ctx->blend.rt[0].colormask = PIPE_MASK_RGBA;
1292
1293   /* no-op depth/stencil/alpha */
1294   memset(&ctx->depthstencil, 0, sizeof(ctx->depthstencil));
1295
1296   /* rasterizer */
1297   memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer));
1298   ctx->rasterizer.front_winding = PIPE_WINDING_CW;
1299   ctx->rasterizer.cull_mode = PIPE_WINDING_NONE;
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 */
1342static unsigned
1343get_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
1361static unsigned
1362set_vertex_data(struct gen_mipmap_state *ctx,
1363                enum pipe_texture_target tex_target,
1364                uint face)
1365{
1366   unsigned offset;
1367
1368   /* vert[0].position */
1369   ctx->vertices[0][0][0] = -1.0f; /*x*/
1370   ctx->vertices[0][0][1] = -1.0f; /*y*/
1371
1372   /* vert[1].position */
1373   ctx->vertices[1][0][0] = 1.0f;
1374   ctx->vertices[1][0][1] = -1.0f;
1375
1376   /* vert[2].position */
1377   ctx->vertices[2][0][0] = 1.0f;
1378   ctx->vertices[2][0][1] = 1.0f;
1379
1380   /* vert[3].position */
1381   ctx->vertices[3][0][0] = -1.0f;
1382   ctx->vertices[3][0][1] = 1.0f;
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 */
1425void
1426util_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 */
1444void 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 */
1462void
1463util_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   cso_save_viewport(ctx->cso);
1503   cso_save_clip(ctx->cso);
1504
1505   /* bind our state */
1506   cso_set_blend(ctx->cso, &ctx->blend);
1507   cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
1508   cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
1509   cso_set_clip(ctx->cso, &ctx->clip);
1510
1511   cso_set_fragment_shader_handle(ctx->cso, fs);
1512   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
1513
1514   /* init framebuffer state */
1515   memset(&fb, 0, sizeof(fb));
1516   fb.nr_cbufs = 1;
1517
1518   /* set min/mag to same filter for faster sw speed */
1519   ctx->sampler.mag_img_filter = filter;
1520   ctx->sampler.min_img_filter = filter;
1521
1522   /*
1523    * XXX for small mipmap levels, it may be faster to use the software
1524    * fallback path...
1525    */
1526   for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
1527      const uint srcLevel = dstLevel - 1;
1528      struct pipe_viewport_state vp;
1529
1530      struct pipe_surface *surf =
1531         screen->get_tex_surface(screen, pt, face, dstLevel, zslice,
1532                                 PIPE_BUFFER_USAGE_GPU_WRITE);
1533
1534      /*
1535       * Setup framebuffer / dest surface
1536       */
1537      fb.cbufs[0] = surf;
1538      fb.width = u_minify(pt->width0, dstLevel);
1539      fb.height = u_minify(pt->height0, dstLevel);
1540      cso_set_framebuffer(ctx->cso, &fb);
1541
1542      /* viewport */
1543      vp.scale[0] = 0.5f * fb.width;
1544      vp.scale[1] = 0.5f * fb.height;
1545      vp.scale[2] = 1.0f;
1546      vp.scale[3] = 1.0f;
1547      vp.translate[0] = 0.5f * fb.width;
1548      vp.translate[1] = 0.5f * fb.height;
1549      vp.translate[2] = 0.0f;
1550      vp.translate[3] = 0.0f;
1551      cso_set_viewport(ctx->cso, &vp);
1552
1553      /*
1554       * Setup sampler state
1555       * Note: we should only have to set the min/max LOD clamps to ensure
1556       * we grab texels from the right mipmap level.  But some hardware
1557       * has trouble with min clamping so we also set the lod_bias to
1558       * try to work around that.
1559       */
1560      ctx->sampler.min_lod = ctx->sampler.max_lod = (float) srcLevel;
1561      ctx->sampler.lod_bias = (float) srcLevel;
1562      cso_single_sampler(ctx->cso, 0, &ctx->sampler);
1563      cso_single_sampler_done(ctx->cso);
1564
1565      cso_set_sampler_textures(ctx->cso, 1, &pt);
1566
1567      /* quad coords in clip coords */
1568      offset = set_vertex_data(ctx,
1569                               pt->target,
1570                               face);
1571
1572      util_draw_vertex_buffer(ctx->pipe,
1573                              ctx->vbuf,
1574                              offset,
1575                              PIPE_PRIM_TRIANGLE_FAN,
1576                              4,  /* verts */
1577                              2); /* attribs/vert */
1578
1579      pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
1580
1581      /* need to signal that the texture has changed _after_ rendering to it */
1582      pipe_surface_reference( &surf, NULL );
1583   }
1584
1585   /* restore state we changed */
1586   cso_restore_blend(ctx->cso);
1587   cso_restore_depth_stencil_alpha(ctx->cso);
1588   cso_restore_rasterizer(ctx->cso);
1589   cso_restore_samplers(ctx->cso);
1590   cso_restore_sampler_textures(ctx->cso);
1591   cso_restore_framebuffer(ctx->cso);
1592   cso_restore_fragment_shader(ctx->cso);
1593   cso_restore_vertex_shader(ctx->cso);
1594   cso_restore_viewport(ctx->cso);
1595   cso_restore_clip(ctx->cso);
1596}
1597