u_gen_mipmap.c revision d2c2e9316d043ab584794a3524f22776deb4c777
1/**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28/**
29 * @file
30 * Mipmap generation utility
31 *
32 * @author Brian Paul
33 */
34
35
36#include "pipe/p_context.h"
37#include "pipe/p_debug.h"
38#include "pipe/p_defines.h"
39#include "pipe/p_inlines.h"
40#include "pipe/p_winsys.h"
41#include "pipe/p_shader_tokens.h"
42
43#include "util/u_memory.h"
44#include "util/u_draw_quad.h"
45#include "util/u_gen_mipmap.h"
46#include "util/u_simple_shaders.h"
47
48#include "tgsi/tgsi_build.h"
49#include "tgsi/tgsi_dump.h"
50#include "tgsi/tgsi_parse.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_viewport_state viewport;
65
66   struct pipe_shader_state vert_shader;
67   struct pipe_shader_state frag_shader;
68   void *vs;
69   void *fs;
70
71   struct pipe_buffer *vbuf;  /**< quad vertices */
72   unsigned vbuf_slot;
73
74   float vertices[4][2][4];   /**< vertex/texcoords for quad */
75};
76
77
78
79enum dtype
80{
81   UBYTE,
82   UBYTE_3_3_2,
83   USHORT,
84   USHORT_4_4_4_4,
85   USHORT_5_6_5,
86   USHORT_1_5_5_5_REV,
87   UINT,
88   FLOAT,
89   HALF_FLOAT
90};
91
92
93typedef ushort half_float;
94
95
96#if 0
97extern half_float
98float_to_half(float f);
99
100extern float
101half_to_float(half_float h);
102#endif
103
104
105/**
106 * Average together two rows of a source image to produce a single new
107 * row in the dest image.  It's legal for the two source rows to point
108 * to the same data.  The source width must be equal to either the
109 * dest width or two times the dest width.
110 * \param datatype  GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, GL_FLOAT, etc.
111 * \param comps  number of components per pixel (1..4)
112 */
113static void
114do_row(enum dtype datatype, uint comps, int srcWidth,
115       const void *srcRowA, const void *srcRowB,
116       int dstWidth, void *dstRow)
117{
118   const uint k0 = (srcWidth == dstWidth) ? 0 : 1;
119   const uint colStride = (srcWidth == dstWidth) ? 1 : 2;
120
121   assert(comps >= 1);
122   assert(comps <= 4);
123
124   /* This assertion is no longer valid with non-power-of-2 textures
125   assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth);
126   */
127
128   if (datatype == UBYTE && comps == 4) {
129      uint i, j, k;
130      const ubyte(*rowA)[4] = (const ubyte(*)[4]) srcRowA;
131      const ubyte(*rowB)[4] = (const ubyte(*)[4]) srcRowB;
132      ubyte(*dst)[4] = (ubyte(*)[4]) dstRow;
133      for (i = j = 0, k = k0; i < (uint) dstWidth;
134           i++, j += colStride, k += colStride) {
135         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
136         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
137         dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
138         dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
139      }
140   }
141   else if (datatype == UBYTE && comps == 3) {
142      uint i, j, k;
143      const ubyte(*rowA)[3] = (const ubyte(*)[3]) srcRowA;
144      const ubyte(*rowB)[3] = (const ubyte(*)[3]) srcRowB;
145      ubyte(*dst)[3] = (ubyte(*)[3]) dstRow;
146      for (i = j = 0, k = k0; i < (uint) dstWidth;
147           i++, j += colStride, k += colStride) {
148         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
149         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
150         dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
151      }
152   }
153   else if (datatype == UBYTE && comps == 2) {
154      uint i, j, k;
155      const ubyte(*rowA)[2] = (const ubyte(*)[2]) srcRowA;
156      const ubyte(*rowB)[2] = (const ubyte(*)[2]) srcRowB;
157      ubyte(*dst)[2] = (ubyte(*)[2]) dstRow;
158      for (i = j = 0, k = k0; i < (uint) dstWidth;
159           i++, j += colStride, k += colStride) {
160         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) >> 2;
161         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) >> 2;
162      }
163   }
164   else if (datatype == UBYTE && comps == 1) {
165      uint i, j, k;
166      const ubyte *rowA = (const ubyte *) srcRowA;
167      const ubyte *rowB = (const ubyte *) srcRowB;
168      ubyte *dst = (ubyte *) dstRow;
169      for (i = j = 0, k = k0; i < (uint) dstWidth;
170           i++, j += colStride, k += colStride) {
171         dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) >> 2;
172      }
173   }
174
175   else if (datatype == USHORT && comps == 4) {
176      uint i, j, k;
177      const ushort(*rowA)[4] = (const ushort(*)[4]) srcRowA;
178      const ushort(*rowB)[4] = (const ushort(*)[4]) srcRowB;
179      ushort(*dst)[4] = (ushort(*)[4]) dstRow;
180      for (i = j = 0, k = k0; i < (uint) dstWidth;
181           i++, j += colStride, k += colStride) {
182         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
183         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
184         dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
185         dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
186      }
187   }
188   else if (datatype == USHORT && comps == 3) {
189      uint i, j, k;
190      const ushort(*rowA)[3] = (const ushort(*)[3]) srcRowA;
191      const ushort(*rowB)[3] = (const ushort(*)[3]) srcRowB;
192      ushort(*dst)[3] = (ushort(*)[3]) dstRow;
193      for (i = j = 0, k = k0; i < (uint) dstWidth;
194           i++, j += colStride, k += colStride) {
195         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
196         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
197         dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
198      }
199   }
200   else if (datatype == USHORT && comps == 2) {
201      uint i, j, k;
202      const ushort(*rowA)[2] = (const ushort(*)[2]) srcRowA;
203      const ushort(*rowB)[2] = (const ushort(*)[2]) srcRowB;
204      ushort(*dst)[2] = (ushort(*)[2]) dstRow;
205      for (i = j = 0, k = k0; i < (uint) dstWidth;
206           i++, j += colStride, k += colStride) {
207         dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
208         dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
209      }
210   }
211   else if (datatype == USHORT && comps == 1) {
212      uint i, j, k;
213      const ushort *rowA = (const ushort *) srcRowA;
214      const ushort *rowB = (const ushort *) srcRowB;
215      ushort *dst = (ushort *) dstRow;
216      for (i = j = 0, k = k0; i < (uint) dstWidth;
217           i++, j += colStride, k += colStride) {
218         dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) / 4;
219      }
220   }
221
222   else if (datatype == FLOAT && comps == 4) {
223      uint i, j, k;
224      const float(*rowA)[4] = (const float(*)[4]) srcRowA;
225      const float(*rowB)[4] = (const float(*)[4]) srcRowB;
226      float(*dst)[4] = (float(*)[4]) dstRow;
227      for (i = j = 0, k = k0; i < (uint) dstWidth;
228           i++, j += colStride, k += colStride) {
229         dst[i][0] = (rowA[j][0] + rowA[k][0] +
230                      rowB[j][0] + rowB[k][0]) * 0.25F;
231         dst[i][1] = (rowA[j][1] + rowA[k][1] +
232                      rowB[j][1] + rowB[k][1]) * 0.25F;
233         dst[i][2] = (rowA[j][2] + rowA[k][2] +
234                      rowB[j][2] + rowB[k][2]) * 0.25F;
235         dst[i][3] = (rowA[j][3] + rowA[k][3] +
236                      rowB[j][3] + rowB[k][3]) * 0.25F;
237      }
238   }
239   else if (datatype == FLOAT && comps == 3) {
240      uint i, j, k;
241      const float(*rowA)[3] = (const float(*)[3]) srcRowA;
242      const float(*rowB)[3] = (const float(*)[3]) srcRowB;
243      float(*dst)[3] = (float(*)[3]) dstRow;
244      for (i = j = 0, k = k0; i < (uint) dstWidth;
245           i++, j += colStride, k += colStride) {
246         dst[i][0] = (rowA[j][0] + rowA[k][0] +
247                      rowB[j][0] + rowB[k][0]) * 0.25F;
248         dst[i][1] = (rowA[j][1] + rowA[k][1] +
249                      rowB[j][1] + rowB[k][1]) * 0.25F;
250         dst[i][2] = (rowA[j][2] + rowA[k][2] +
251                      rowB[j][2] + rowB[k][2]) * 0.25F;
252      }
253   }
254   else if (datatype == FLOAT && comps == 2) {
255      uint i, j, k;
256      const float(*rowA)[2] = (const float(*)[2]) srcRowA;
257      const float(*rowB)[2] = (const float(*)[2]) srcRowB;
258      float(*dst)[2] = (float(*)[2]) dstRow;
259      for (i = j = 0, k = k0; i < (uint) dstWidth;
260           i++, j += colStride, k += colStride) {
261         dst[i][0] = (rowA[j][0] + rowA[k][0] +
262                      rowB[j][0] + rowB[k][0]) * 0.25F;
263         dst[i][1] = (rowA[j][1] + rowA[k][1] +
264                      rowB[j][1] + rowB[k][1]) * 0.25F;
265      }
266   }
267   else if (datatype == FLOAT && comps == 1) {
268      uint i, j, k;
269      const float *rowA = (const float *) srcRowA;
270      const float *rowB = (const float *) srcRowB;
271      float *dst = (float *) dstRow;
272      for (i = j = 0, k = k0; i < (uint) dstWidth;
273           i++, j += colStride, k += colStride) {
274         dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) * 0.25F;
275      }
276   }
277
278#if 0
279   else if (datatype == HALF_FLOAT && comps == 4) {
280      uint i, j, k, comp;
281      const half_float(*rowA)[4] = (const half_float(*)[4]) srcRowA;
282      const half_float(*rowB)[4] = (const half_float(*)[4]) srcRowB;
283      half_float(*dst)[4] = (half_float(*)[4]) dstRow;
284      for (i = j = 0, k = k0; i < (uint) dstWidth;
285           i++, j += colStride, k += colStride) {
286         for (comp = 0; comp < 4; comp++) {
287            float aj, ak, bj, bk;
288            aj = half_to_float(rowA[j][comp]);
289            ak = half_to_float(rowA[k][comp]);
290            bj = half_to_float(rowB[j][comp]);
291            bk = half_to_float(rowB[k][comp]);
292            dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F);
293         }
294      }
295   }
296   else if (datatype == HALF_FLOAT && comps == 3) {
297      uint i, j, k, comp;
298      const half_float(*rowA)[3] = (const half_float(*)[3]) srcRowA;
299      const half_float(*rowB)[3] = (const half_float(*)[3]) srcRowB;
300      half_float(*dst)[3] = (half_float(*)[3]) dstRow;
301      for (i = j = 0, k = k0; i < (uint) dstWidth;
302           i++, j += colStride, k += colStride) {
303         for (comp = 0; comp < 3; comp++) {
304            float aj, ak, bj, bk;
305            aj = half_to_float(rowA[j][comp]);
306            ak = half_to_float(rowA[k][comp]);
307            bj = half_to_float(rowB[j][comp]);
308            bk = half_to_float(rowB[k][comp]);
309            dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F);
310         }
311      }
312   }
313   else if (datatype == HALF_FLOAT && comps == 2) {
314      uint i, j, k, comp;
315      const half_float(*rowA)[2] = (const half_float(*)[2]) srcRowA;
316      const half_float(*rowB)[2] = (const half_float(*)[2]) srcRowB;
317      half_float(*dst)[2] = (half_float(*)[2]) dstRow;
318      for (i = j = 0, k = k0; i < (uint) dstWidth;
319           i++, j += colStride, k += colStride) {
320         for (comp = 0; comp < 2; comp++) {
321            float aj, ak, bj, bk;
322            aj = half_to_float(rowA[j][comp]);
323            ak = half_to_float(rowA[k][comp]);
324            bj = half_to_float(rowB[j][comp]);
325            bk = half_to_float(rowB[k][comp]);
326            dst[i][comp] = float_to_half((aj + ak + bj + bk) * 0.25F);
327         }
328      }
329   }
330   else if (datatype == HALF_FLOAT && comps == 1) {
331      uint i, j, k;
332      const half_float *rowA = (const half_float *) srcRowA;
333      const half_float *rowB = (const half_float *) srcRowB;
334      half_float *dst = (half_float *) dstRow;
335      for (i = j = 0, k = k0; i < (uint) dstWidth;
336           i++, j += colStride, k += colStride) {
337         float aj, ak, bj, bk;
338         aj = half_to_float(rowA[j]);
339         ak = half_to_float(rowA[k]);
340         bj = half_to_float(rowB[j]);
341         bk = half_to_float(rowB[k]);
342         dst[i] = float_to_half((aj + ak + bj + bk) * 0.25F);
343      }
344   }
345#endif
346
347   else if (datatype == UINT && comps == 1) {
348      uint i, j, k;
349      const uint *rowA = (const uint *) srcRowA;
350      const uint *rowB = (const uint *) srcRowB;
351      uint *dst = (uint *) dstRow;
352      for (i = j = 0, k = k0; i < (uint) dstWidth;
353           i++, j += colStride, k += colStride) {
354         dst[i] = rowA[j] / 4 + rowA[k] / 4 + rowB[j] / 4 + rowB[k] / 4;
355      }
356   }
357
358   else if (datatype == USHORT_5_6_5 && comps == 3) {
359      uint i, j, k;
360      const ushort *rowA = (const ushort *) srcRowA;
361      const ushort *rowB = (const ushort *) srcRowB;
362      ushort *dst = (ushort *) dstRow;
363      for (i = j = 0, k = k0; i < (uint) dstWidth;
364           i++, j += colStride, k += colStride) {
365         const int rowAr0 = rowA[j] & 0x1f;
366         const int rowAr1 = rowA[k] & 0x1f;
367         const int rowBr0 = rowB[j] & 0x1f;
368         const int rowBr1 = rowB[k] & 0x1f;
369         const int rowAg0 = (rowA[j] >> 5) & 0x3f;
370         const int rowAg1 = (rowA[k] >> 5) & 0x3f;
371         const int rowBg0 = (rowB[j] >> 5) & 0x3f;
372         const int rowBg1 = (rowB[k] >> 5) & 0x3f;
373         const int rowAb0 = (rowA[j] >> 11) & 0x1f;
374         const int rowAb1 = (rowA[k] >> 11) & 0x1f;
375         const int rowBb0 = (rowB[j] >> 11) & 0x1f;
376         const int rowBb1 = (rowB[k] >> 11) & 0x1f;
377         const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
378         const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
379         const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
380         dst[i] = (blue << 11) | (green << 5) | red;
381      }
382   }
383   else if (datatype == USHORT_4_4_4_4 && comps == 4) {
384      uint i, j, k;
385      const ushort *rowA = (const ushort *) srcRowA;
386      const ushort *rowB = (const ushort *) srcRowB;
387      ushort *dst = (ushort *) dstRow;
388      for (i = j = 0, k = k0; i < (uint) dstWidth;
389           i++, j += colStride, k += colStride) {
390         const int rowAr0 = rowA[j] & 0xf;
391         const int rowAr1 = rowA[k] & 0xf;
392         const int rowBr0 = rowB[j] & 0xf;
393         const int rowBr1 = rowB[k] & 0xf;
394         const int rowAg0 = (rowA[j] >> 4) & 0xf;
395         const int rowAg1 = (rowA[k] >> 4) & 0xf;
396         const int rowBg0 = (rowB[j] >> 4) & 0xf;
397         const int rowBg1 = (rowB[k] >> 4) & 0xf;
398         const int rowAb0 = (rowA[j] >> 8) & 0xf;
399         const int rowAb1 = (rowA[k] >> 8) & 0xf;
400         const int rowBb0 = (rowB[j] >> 8) & 0xf;
401         const int rowBb1 = (rowB[k] >> 8) & 0xf;
402         const int rowAa0 = (rowA[j] >> 12) & 0xf;
403         const int rowAa1 = (rowA[k] >> 12) & 0xf;
404         const int rowBa0 = (rowB[j] >> 12) & 0xf;
405         const int rowBa1 = (rowB[k] >> 12) & 0xf;
406         const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
407         const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
408         const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
409         const int alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
410         dst[i] = (alpha << 12) | (blue << 8) | (green << 4) | red;
411      }
412   }
413   else if (datatype == USHORT_1_5_5_5_REV && comps == 4) {
414      uint i, j, k;
415      const ushort *rowA = (const ushort *) srcRowA;
416      const ushort *rowB = (const ushort *) srcRowB;
417      ushort *dst = (ushort *) dstRow;
418      for (i = j = 0, k = k0; i < (uint) dstWidth;
419           i++, j += colStride, k += colStride) {
420         const int rowAr0 = rowA[j] & 0x1f;
421         const int rowAr1 = rowA[k] & 0x1f;
422         const int rowBr0 = rowB[j] & 0x1f;
423         const int rowBr1 = rowB[k] & 0xf;
424         const int rowAg0 = (rowA[j] >> 5) & 0x1f;
425         const int rowAg1 = (rowA[k] >> 5) & 0x1f;
426         const int rowBg0 = (rowB[j] >> 5) & 0x1f;
427         const int rowBg1 = (rowB[k] >> 5) & 0x1f;
428         const int rowAb0 = (rowA[j] >> 10) & 0x1f;
429         const int rowAb1 = (rowA[k] >> 10) & 0x1f;
430         const int rowBb0 = (rowB[j] >> 10) & 0x1f;
431         const int rowBb1 = (rowB[k] >> 10) & 0x1f;
432         const int rowAa0 = (rowA[j] >> 15) & 0x1;
433         const int rowAa1 = (rowA[k] >> 15) & 0x1;
434         const int rowBa0 = (rowB[j] >> 15) & 0x1;
435         const int rowBa1 = (rowB[k] >> 15) & 0x1;
436         const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
437         const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
438         const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
439         const int alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
440         dst[i] = (alpha << 15) | (blue << 10) | (green << 5) | red;
441      }
442   }
443   else if (datatype == UBYTE_3_3_2 && comps == 3) {
444      uint i, j, k;
445      const ubyte *rowA = (const ubyte *) srcRowA;
446      const ubyte *rowB = (const ubyte *) srcRowB;
447      ubyte *dst = (ubyte *) dstRow;
448      for (i = j = 0, k = k0; i < (uint) dstWidth;
449           i++, j += colStride, k += colStride) {
450         const int rowAr0 = rowA[j] & 0x3;
451         const int rowAr1 = rowA[k] & 0x3;
452         const int rowBr0 = rowB[j] & 0x3;
453         const int rowBr1 = rowB[k] & 0x3;
454         const int rowAg0 = (rowA[j] >> 2) & 0x7;
455         const int rowAg1 = (rowA[k] >> 2) & 0x7;
456         const int rowBg0 = (rowB[j] >> 2) & 0x7;
457         const int rowBg1 = (rowB[k] >> 2) & 0x7;
458         const int rowAb0 = (rowA[j] >> 5) & 0x7;
459         const int rowAb1 = (rowA[k] >> 5) & 0x7;
460         const int rowBb0 = (rowB[j] >> 5) & 0x7;
461         const int rowBb1 = (rowB[k] >> 5) & 0x7;
462         const int red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
463         const int green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
464         const int blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
465         dst[i] = (blue << 5) | (green << 2) | red;
466      }
467   }
468   else {
469      debug_printf("bad format in do_row()");
470   }
471}
472
473
474static void
475format_to_type_comps(enum pipe_format pformat,
476                     enum dtype *datatype, uint *comps)
477{
478   switch (pformat) {
479   case PIPE_FORMAT_A8R8G8B8_UNORM:
480   case PIPE_FORMAT_X8R8G8B8_UNORM:
481   case PIPE_FORMAT_B8G8R8A8_UNORM:
482   case PIPE_FORMAT_B8G8R8X8_UNORM:
483      *datatype = UBYTE;
484      *comps = 4;
485      return;
486   case PIPE_FORMAT_A1R5G5B5_UNORM:
487      *datatype = USHORT_1_5_5_5_REV;
488      *comps = 4;
489      return;
490   case PIPE_FORMAT_A4R4G4B4_UNORM:
491      *datatype = USHORT_4_4_4_4;
492      *comps = 4;
493      return;
494   case PIPE_FORMAT_R5G6B5_UNORM:
495      *datatype = USHORT_5_6_5;
496      *comps = 3;
497      return;
498   case PIPE_FORMAT_L8_UNORM:
499   case PIPE_FORMAT_A8_UNORM:
500   case PIPE_FORMAT_I8_UNORM:
501      *datatype = UBYTE;
502      *comps = 1;
503      return;
504   case PIPE_FORMAT_A8L8_UNORM:
505      *datatype = UBYTE;
506      *comps = 2;
507      return;
508   default:
509      assert(0);
510      *datatype = UBYTE;
511      *comps = 0;
512      break;
513   }
514}
515
516
517static void
518reduce_1d(enum pipe_format pformat,
519          int srcWidth, const ubyte *srcPtr,
520          int dstWidth, ubyte *dstPtr)
521{
522   enum dtype datatype;
523   uint comps;
524
525   format_to_type_comps(pformat, &datatype, &comps);
526
527   /* we just duplicate the input row, kind of hack, saves code */
528   do_row(datatype, comps,
529          srcWidth, srcPtr, srcPtr,
530          dstWidth, dstPtr);
531}
532
533
534/**
535 * Strides are in bytes.  If zero, it'll be computed as width * bpp.
536 */
537static void
538reduce_2d(enum pipe_format pformat,
539          int srcWidth, int srcHeight,
540          int srcRowStride, const ubyte *srcPtr,
541          int dstWidth, int dstHeight,
542          int dstRowStride, ubyte *dstPtr)
543{
544   enum dtype datatype;
545   uint comps;
546   const int bpt = pf_get_size(pformat);
547   const ubyte *srcA, *srcB;
548   ubyte *dst;
549   int row;
550
551   format_to_type_comps(pformat, &datatype, &comps);
552
553   if (!srcRowStride)
554      srcRowStride = bpt * srcWidth;
555
556   if (!dstRowStride)
557      dstRowStride = bpt * dstWidth;
558
559   /* Compute src and dst pointers */
560   srcA = srcPtr;
561   if (srcHeight > 1)
562      srcB = srcA + srcRowStride;
563   else
564      srcB = srcA;
565   dst = dstPtr;
566
567   for (row = 0; row < dstHeight; row++) {
568      do_row(datatype, comps,
569             srcWidth, srcA, srcB,
570             dstWidth, dst);
571      srcA += 2 * srcRowStride;
572      srcB += 2 * srcRowStride;
573      dst += dstRowStride;
574   }
575}
576
577
578static void
579make_1d_mipmap(struct gen_mipmap_state *ctx,
580               struct pipe_texture *pt,
581               uint face, uint baseLevel, uint lastLevel)
582{
583   struct pipe_context *pipe = ctx->pipe;
584   struct pipe_screen *screen = pipe->screen;
585   const uint zslice = 0;
586   uint dstLevel;
587
588   for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
589      const uint srcLevel = dstLevel - 1;
590      struct pipe_surface *srcSurf, *dstSurf;
591      void *srcMap, *dstMap;
592
593      srcSurf = screen->get_tex_surface(screen, pt, face, srcLevel, zslice,
594                                        PIPE_BUFFER_USAGE_CPU_READ);
595
596      dstSurf = screen->get_tex_surface(screen, pt, face, dstLevel, zslice,
597                                        PIPE_BUFFER_USAGE_CPU_WRITE);
598
599      srcMap = ((ubyte *) pipe_buffer_map(screen, srcSurf->buffer,
600                                          PIPE_BUFFER_USAGE_CPU_READ)
601                + srcSurf->offset);
602      dstMap = ((ubyte *) pipe_buffer_map(screen, dstSurf->buffer,
603                                          PIPE_BUFFER_USAGE_CPU_WRITE)
604                + dstSurf->offset);
605
606      reduce_1d(pt->format,
607                srcSurf->width, srcMap,
608                dstSurf->width, dstMap);
609
610      pipe_buffer_unmap(screen, srcSurf->buffer);
611      pipe_buffer_unmap(screen, dstSurf->buffer);
612
613      pipe_surface_reference(&srcSurf, NULL);
614      pipe_surface_reference(&dstSurf, NULL);
615   }
616}
617
618
619static void
620make_2d_mipmap(struct gen_mipmap_state *ctx,
621               struct pipe_texture *pt,
622               uint face, uint baseLevel, uint lastLevel)
623{
624   struct pipe_context *pipe = ctx->pipe;
625   struct pipe_screen *screen = pipe->screen;
626   const uint zslice = 0;
627   uint dstLevel;
628
629   assert(pt->block.width == 1);
630   assert(pt->block.height == 1);
631
632   for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
633      const uint srcLevel = dstLevel - 1;
634      struct pipe_surface *srcSurf, *dstSurf;
635      ubyte *srcMap, *dstMap;
636
637      srcSurf = screen->get_tex_surface(screen, pt, face, srcLevel, zslice,
638                                        PIPE_BUFFER_USAGE_CPU_READ);
639      dstSurf = screen->get_tex_surface(screen, pt, face, dstLevel, zslice,
640                                        PIPE_BUFFER_USAGE_CPU_WRITE);
641
642      srcMap = ((ubyte *) pipe_buffer_map(screen, srcSurf->buffer,
643                                          PIPE_BUFFER_USAGE_CPU_READ)
644                + srcSurf->offset);
645      dstMap = ((ubyte *) pipe_buffer_map(screen, dstSurf->buffer,
646                                          PIPE_BUFFER_USAGE_CPU_WRITE)
647                + dstSurf->offset);
648
649      reduce_2d(pt->format,
650                srcSurf->width, srcSurf->height,
651                srcSurf->stride, srcMap,
652                dstSurf->width, dstSurf->height,
653                dstSurf->stride, dstMap);
654
655      pipe_buffer_unmap(screen, srcSurf->buffer);
656      pipe_buffer_unmap(screen, dstSurf->buffer);
657
658      pipe_surface_reference(&srcSurf, NULL);
659      pipe_surface_reference(&dstSurf, NULL);
660   }
661}
662
663
664static void
665make_3d_mipmap(struct gen_mipmap_state *ctx,
666               struct pipe_texture *pt,
667               uint face, uint baseLevel, uint lastLevel)
668{
669}
670
671
672static void
673fallback_gen_mipmap(struct gen_mipmap_state *ctx,
674                    struct pipe_texture *pt,
675                    uint face, uint baseLevel, uint lastLevel)
676{
677   switch (pt->target) {
678   case PIPE_TEXTURE_1D:
679      make_1d_mipmap(ctx, pt, face, baseLevel, lastLevel);
680      break;
681   case PIPE_TEXTURE_2D:
682   case PIPE_TEXTURE_CUBE:
683      make_2d_mipmap(ctx, pt, face, baseLevel, lastLevel);
684      break;
685   case PIPE_TEXTURE_3D:
686      make_3d_mipmap(ctx, pt, face, baseLevel, lastLevel);
687      break;
688   default:
689      assert(0);
690   }
691}
692
693
694/**
695 * Create a mipmap generation context.
696 * The idea is to create one of these and re-use it each time we need to
697 * generate a mipmap.
698 */
699struct gen_mipmap_state *
700util_create_gen_mipmap(struct pipe_context *pipe,
701                       struct cso_context *cso)
702{
703   struct gen_mipmap_state *ctx;
704   uint i;
705
706   ctx = CALLOC_STRUCT(gen_mipmap_state);
707   if (!ctx)
708      return NULL;
709
710   ctx->pipe = pipe;
711   ctx->cso = cso;
712
713   /* disabled blending/masking */
714   memset(&ctx->blend, 0, sizeof(ctx->blend));
715   ctx->blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
716   ctx->blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
717   ctx->blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
718   ctx->blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
719   ctx->blend.colormask = PIPE_MASK_RGBA;
720
721   /* no-op depth/stencil/alpha */
722   memset(&ctx->depthstencil, 0, sizeof(ctx->depthstencil));
723
724   /* rasterizer */
725   memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer));
726   ctx->rasterizer.front_winding = PIPE_WINDING_CW;
727   ctx->rasterizer.cull_mode = PIPE_WINDING_NONE;
728   ctx->rasterizer.bypass_clipping = 1;
729   /*ctx->rasterizer.bypass_vs = 1;*/
730   ctx->rasterizer.gl_rasterization_rules = 1;
731
732   /* sampler state */
733   memset(&ctx->sampler, 0, sizeof(ctx->sampler));
734   ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
735   ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
736   ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
737   ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
738   ctx->sampler.normalized_coords = 1;
739
740   /* viewport state (identity, verts are in wincoords) */
741   ctx->viewport.scale[0] = 1.0;
742   ctx->viewport.scale[1] = 1.0;
743   ctx->viewport.scale[2] = 1.0;
744   ctx->viewport.scale[3] = 1.0;
745   ctx->viewport.translate[0] = 0.0;
746   ctx->viewport.translate[1] = 0.0;
747   ctx->viewport.translate[2] = 0.0;
748   ctx->viewport.translate[3] = 0.0;
749
750   /* vertex shader */
751   {
752      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
753                                      TGSI_SEMANTIC_GENERIC };
754      const uint semantic_indexes[] = { 0, 0 };
755      ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
756                                                    semantic_indexes,
757                                                    &ctx->vert_shader);
758   }
759
760   /* fragment shader */
761   ctx->fs = util_make_fragment_tex_shader(pipe, &ctx->frag_shader);
762
763   ctx->vbuf = pipe_buffer_create(pipe->screen,
764                                  32,
765                                  PIPE_BUFFER_USAGE_VERTEX,
766                                  sizeof(ctx->vertices));
767   if (!ctx->vbuf) {
768      FREE(ctx);
769      return NULL;
770   }
771
772   /* vertex data that doesn't change */
773   for (i = 0; i < 4; i++) {
774      ctx->vertices[i][0][2] = 0.0f; /* z */
775      ctx->vertices[i][0][3] = 1.0f; /* w */
776      ctx->vertices[i][1][2] = 0.0f; /* r */
777      ctx->vertices[i][1][3] = 1.0f; /* q */
778   }
779
780   return ctx;
781}
782
783
784static unsigned get_next_slot( struct gen_mipmap_state *ctx )
785{
786   const unsigned max_slots = 4096 / sizeof ctx->vertices;
787
788   if (ctx->vbuf_slot >= max_slots)
789      util_gen_mipmap_flush( ctx );
790
791   if (!ctx->vbuf) {
792      ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
793                                     32,
794                                     PIPE_BUFFER_USAGE_VERTEX,
795                                     max_slots * sizeof ctx->vertices);
796   }
797
798   return ctx->vbuf_slot++ * sizeof ctx->vertices;
799}
800
801static unsigned
802set_vertex_data(struct gen_mipmap_state *ctx, float width, float height)
803{
804   void *buf;
805   unsigned offset;
806
807   ctx->vertices[0][0][0] = 0.0f; /*x*/
808   ctx->vertices[0][0][1] = 0.0f; /*y*/
809   ctx->vertices[0][1][0] = 0.0f; /*s*/
810   ctx->vertices[0][1][1] = 0.0f; /*t*/
811
812   ctx->vertices[1][0][0] = width;
813   ctx->vertices[1][0][1] = 0.0f;
814   ctx->vertices[1][1][0] = 1.0f;
815   ctx->vertices[1][1][1] = 0.0f;
816
817   ctx->vertices[2][0][0] = width;
818   ctx->vertices[2][0][1] = height;
819   ctx->vertices[2][1][0] = 1.0f;
820   ctx->vertices[2][1][1] = 1.0f;
821
822   ctx->vertices[3][0][0] = 0.0f;
823   ctx->vertices[3][0][1] = height;
824   ctx->vertices[3][1][0] = 0.0f;
825   ctx->vertices[3][1][1] = 1.0f;
826
827   offset = get_next_slot( ctx );
828
829   buf = pipe_buffer_map(ctx->pipe->screen, ctx->vbuf,
830                         PIPE_BUFFER_USAGE_CPU_WRITE);
831
832   memcpy((char *)buf + offset, ctx->vertices, sizeof(ctx->vertices));
833
834   pipe_buffer_unmap(ctx->pipe->screen, ctx->vbuf);
835
836   return offset;
837}
838
839
840
841/**
842 * Destroy a mipmap generation context
843 */
844void
845util_destroy_gen_mipmap(struct gen_mipmap_state *ctx)
846{
847   struct pipe_context *pipe = ctx->pipe;
848
849   pipe->delete_vs_state(pipe, ctx->vs);
850   pipe->delete_fs_state(pipe, ctx->fs);
851
852   FREE((void*) ctx->vert_shader.tokens);
853   FREE((void*) ctx->frag_shader.tokens);
854
855   pipe_buffer_reference(pipe->screen, &ctx->vbuf, NULL);
856
857   FREE(ctx);
858}
859
860
861
862/* Release vertex buffer at end of frame to avoid synchronous
863 * rendering.
864 */
865void util_gen_mipmap_flush( struct gen_mipmap_state *ctx )
866{
867   pipe_buffer_reference(ctx->pipe->screen, &ctx->vbuf, NULL);
868   ctx->vbuf_slot = 0;
869}
870
871
872/**
873 * Generate mipmap images.  It's assumed all needed texture memory is
874 * already allocated.
875 *
876 * \param pt  the texture to generate mipmap levels for
877 * \param face  which cube face to generate mipmaps for (0 for non-cube maps)
878 * \param baseLevel  the first mipmap level to use as a src
879 * \param lastLevel  the last mipmap level to generate
880 * \param filter  the minification filter used to generate mipmap levels with
881 * \param filter  one of PIPE_TEX_FILTER_LINEAR, PIPE_TEX_FILTER_NEAREST
882 */
883void
884util_gen_mipmap(struct gen_mipmap_state *ctx,
885                struct pipe_texture *pt,
886                uint face, uint baseLevel, uint lastLevel, uint filter)
887{
888   struct pipe_context *pipe = ctx->pipe;
889   struct pipe_screen *screen = pipe->screen;
890   struct pipe_framebuffer_state fb;
891   uint dstLevel;
892   uint zslice = 0;
893   uint offset;
894
895   /* check if we can render in the texture's format */
896   if (!screen->is_format_supported(screen, pt->format, PIPE_TEXTURE_2D,
897                                    PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
898      fallback_gen_mipmap(ctx, pt, face, baseLevel, lastLevel);
899      return;
900   }
901
902   /* save state (restored below) */
903   cso_save_blend(ctx->cso);
904   cso_save_depth_stencil_alpha(ctx->cso);
905   cso_save_rasterizer(ctx->cso);
906   cso_save_samplers(ctx->cso);
907   cso_save_sampler_textures(ctx->cso);
908   cso_save_framebuffer(ctx->cso);
909   cso_save_fragment_shader(ctx->cso);
910   cso_save_vertex_shader(ctx->cso);
911   cso_save_viewport(ctx->cso);
912
913   /* bind our state */
914   cso_set_blend(ctx->cso, &ctx->blend);
915   cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
916   cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
917   cso_set_viewport(ctx->cso, &ctx->viewport);
918
919   cso_set_fragment_shader_handle(ctx->cso, ctx->fs);
920   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
921
922   /* init framebuffer state */
923   memset(&fb, 0, sizeof(fb));
924   fb.num_cbufs = 1;
925
926   /* set min/mag to same filter for faster sw speed */
927   ctx->sampler.mag_img_filter = filter;
928   ctx->sampler.min_img_filter = filter;
929
930   /*
931    * XXX for small mipmap levels, it may be faster to use the software
932    * fallback path...
933    */
934   for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
935      const uint srcLevel = dstLevel - 1;
936
937      struct pipe_surface *surf =
938         screen->get_tex_surface(screen, pt, face, dstLevel, zslice,
939                                 PIPE_BUFFER_USAGE_GPU_WRITE);
940
941      /*
942       * Setup framebuffer / dest surface
943       */
944      fb.cbufs[0] = surf;
945      fb.width = pt->width[dstLevel];
946      fb.height = pt->height[dstLevel];
947      cso_set_framebuffer(ctx->cso, &fb);
948
949      /*
950       * Setup sampler state
951       * Note: we should only have to set the min/max LOD clamps to ensure
952       * we grab texels from the right mipmap level.  But some hardware
953       * has trouble with min clamping so we also set the lod_bias to
954       * try to work around that.
955       */
956      ctx->sampler.min_lod = ctx->sampler.max_lod = (float) srcLevel;
957      ctx->sampler.lod_bias = (float) srcLevel;
958      cso_single_sampler(ctx->cso, 0, &ctx->sampler);
959      cso_single_sampler_done(ctx->cso);
960
961      cso_set_sampler_textures(ctx->cso, 1, &pt);
962
963      /* quad coords in window coords (bypassing clipping, viewport mapping) */
964      offset = set_vertex_data(ctx,
965                               (float) pt->width[dstLevel],
966                               (float) pt->height[dstLevel]);
967
968      util_draw_vertex_buffer(ctx->pipe,
969                              ctx->vbuf,
970                              offset,
971                              PIPE_PRIM_TRIANGLE_FAN,
972                              4,  /* verts */
973                              2); /* attribs/vert */
974
975      pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
976
977      /* need to signal that the texture has changed _after_ rendering to it */
978      pipe_surface_reference( &surf, NULL );
979   }
980
981   /* restore state we changed */
982   cso_restore_blend(ctx->cso);
983   cso_restore_depth_stencil_alpha(ctx->cso);
984   cso_restore_rasterizer(ctx->cso);
985   cso_restore_samplers(ctx->cso);
986   cso_restore_sampler_textures(ctx->cso);
987   cso_restore_framebuffer(ctx->cso);
988   cso_restore_fragment_shader(ctx->cso);
989   cso_restore_vertex_shader(ctx->cso);
990   cso_restore_viewport(ctx->cso);
991}
992