1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (c) 2011 VMware, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25/**
26 * Color, depth, stencil packing functions.
27 * Used to pack basic color, depth and stencil formats to specific
28 * hardware formats.
29 *
30 * There are both per-pixel and per-row packing functions:
31 * - The former will be used by swrast to write values to the color, depth,
32 *   stencil buffers when drawing points, lines and masked spans.
33 * - The later will be used for image-oriented functions like glDrawPixels,
34 *   glAccum, and glTexImage.
35 */
36
37
38#include "colormac.h"
39#include "format_pack.h"
40#include "macros.h"
41#include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
42#include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
43
44
45/** Helper struct for MESA_FORMAT_Z32_FLOAT_X24S8 */
46struct z32f_x24s8
47{
48   float z;
49   uint32_t x24s8;
50};
51
52
53typedef void (*pack_ubyte_rgba_row_func)(GLuint n,
54                                         const GLubyte src[][4], void *dst);
55
56typedef void (*pack_float_rgba_row_func)(GLuint n,
57                                         const GLfloat src[][4], void *dst);
58
59
60
61static inline GLfloat
62linear_to_srgb(GLfloat cl)
63{
64   if (cl < 0.0f)
65      return 0.0f;
66   else if (cl < 0.0031308f)
67      return 12.92f * cl;
68   else if (cl < 1.0f)
69      return 1.055f * powf(cl, 0.41666f) - 0.055f;
70   else
71      return 1.0f;
72}
73
74
75static inline GLubyte
76linear_float_to_srgb_ubyte(GLfloat cl)
77{
78   GLubyte res = FLOAT_TO_UBYTE(linear_to_srgb(cl));
79   return res;
80}
81
82
83static inline GLubyte
84linear_ubyte_to_srgb_ubyte(GLubyte cl)
85{
86   GLubyte res = FLOAT_TO_UBYTE(linear_to_srgb(cl / 255.0f));
87   return res;
88}
89
90
91
92
93/*
94 * MESA_FORMAT_RGBA8888
95 */
96
97static void
98pack_ubyte_RGBA8888(const GLubyte src[4], void *dst)
99{
100   GLuint *d = ((GLuint *) dst);
101   *d = PACK_COLOR_8888(src[RCOMP], src[GCOMP], src[BCOMP], src[ACOMP]);
102}
103
104static void
105pack_float_RGBA8888(const GLfloat src[4], void *dst)
106{
107   GLubyte v[4];
108   _mesa_unclamped_float_rgba_to_ubyte(v, src);
109   pack_ubyte_RGBA8888(v, dst);
110}
111
112static void
113pack_row_ubyte_RGBA8888(GLuint n, const GLubyte src[][4], void *dst)
114{
115   GLuint *d = ((GLuint *) dst);
116   GLuint i;
117   for (i = 0; i < n; i++) {
118      d[i] = PACK_COLOR_8888(src[i][RCOMP], src[i][GCOMP],
119                             src[i][BCOMP], src[i][ACOMP]);
120   }
121}
122
123static void
124pack_row_float_RGBA8888(GLuint n, const GLfloat src[][4], void *dst)
125{
126   GLuint *d = ((GLuint *) dst);
127   GLuint i;
128   for (i = 0; i < n; i++) {
129      GLubyte v[4];
130      _mesa_unclamped_float_rgba_to_ubyte(v, src[i]);
131      pack_ubyte_RGBA8888(v, d + i);
132   }
133}
134
135
136
137/*
138 * MESA_FORMAT_RGBA8888_REV
139 */
140
141static void
142pack_ubyte_RGBA8888_REV(const GLubyte src[4], void *dst)
143{
144   GLuint *d = ((GLuint *) dst);
145   *d = PACK_COLOR_8888(src[ACOMP], src[BCOMP], src[GCOMP], src[RCOMP]);
146}
147
148static void
149pack_float_RGBA8888_REV(const GLfloat src[4], void *dst)
150{
151   GLubyte v[4];
152   _mesa_unclamped_float_rgba_to_ubyte(v, src);
153   pack_ubyte_RGBA8888_REV(v, dst);
154}
155
156static void
157pack_row_ubyte_RGBA8888_REV(GLuint n, const GLubyte src[][4], void *dst)
158{
159   GLuint *d = ((GLuint *) dst);
160   GLuint i;
161   for (i = 0; i < n; i++) {
162      d[i] = PACK_COLOR_8888(src[i][ACOMP], src[i][BCOMP],
163                             src[i][GCOMP], src[i][RCOMP]);
164   }
165}
166
167static void
168pack_row_float_RGBA8888_REV(GLuint n, const GLfloat src[][4], void *dst)
169{
170   GLuint *d = ((GLuint *) dst);
171   GLuint i;
172   for (i = 0; i < n; i++) {
173      GLubyte v[4];
174      _mesa_unclamped_float_rgba_to_ubyte(v, src[i]);
175      pack_ubyte_RGBA8888_REV(v, d + i);
176   }
177}
178
179
180/*
181 * MESA_FORMAT_ARGB8888
182 */
183
184static void
185pack_ubyte_ARGB8888(const GLubyte src[4], void *dst)
186{
187   GLuint *d = ((GLuint *) dst);
188   *d = PACK_COLOR_8888(src[ACOMP], src[RCOMP], src[GCOMP], src[BCOMP]);
189}
190
191static void
192pack_float_ARGB8888(const GLfloat src[4], void *dst)
193{
194   GLubyte v[4];
195   _mesa_unclamped_float_rgba_to_ubyte(v, src);
196   pack_ubyte_ARGB8888(v, dst);
197}
198
199static void
200pack_row_ubyte_ARGB8888(GLuint n, const GLubyte src[][4], void *dst)
201{
202   GLuint *d = ((GLuint *) dst);
203   GLuint i;
204   for (i = 0; i < n; i++) {
205      d[i] = PACK_COLOR_8888(src[i][ACOMP], src[i][RCOMP],
206                             src[i][GCOMP], src[i][BCOMP]);
207   }
208}
209
210static void
211pack_row_float_ARGB8888(GLuint n, const GLfloat src[][4], void *dst)
212{
213   GLuint *d = ((GLuint *) dst);
214   GLuint i;
215   for (i = 0; i < n; i++) {
216      GLubyte v[4];
217      _mesa_unclamped_float_rgba_to_ubyte(v, src[i]);
218      pack_ubyte_ARGB8888(v, d + i);
219   }
220}
221
222
223/*
224 * MESA_FORMAT_ARGB8888_REV
225 */
226
227static void
228pack_ubyte_ARGB8888_REV(const GLubyte src[4], void *dst)
229{
230   GLuint *d = ((GLuint *) dst);
231   *d = PACK_COLOR_8888(src[BCOMP], src[GCOMP], src[RCOMP], src[ACOMP]);
232}
233
234static void
235pack_float_ARGB8888_REV(const GLfloat src[4], void *dst)
236{
237   GLubyte v[4];
238   _mesa_unclamped_float_rgba_to_ubyte(v, src);
239   pack_ubyte_ARGB8888_REV(v, dst);
240}
241
242static void
243pack_row_ubyte_ARGB8888_REV(GLuint n, const GLubyte src[][4], void *dst)
244{
245   GLuint *d = ((GLuint *) dst);
246   GLuint i;
247   for (i = 0; i < n; i++) {
248      d[i] = PACK_COLOR_8888(src[i][BCOMP], src[i][GCOMP],
249                             src[i][RCOMP], src[i][ACOMP]);
250   }
251}
252
253static void
254pack_row_float_ARGB8888_REV(GLuint n, const GLfloat src[][4], void *dst)
255{
256   GLuint *d = ((GLuint *) dst);
257   GLuint i;
258   for (i = 0; i < n; i++) {
259      GLubyte v[4];
260      _mesa_unclamped_float_rgba_to_ubyte(v, src[i]);
261      pack_ubyte_ARGB8888_REV(v, d + i);
262   }
263}
264
265
266/*
267 * MESA_FORMAT_XRGB8888
268 */
269
270static void
271pack_ubyte_XRGB8888(const GLubyte src[4], void *dst)
272{
273   GLuint *d = ((GLuint *) dst);
274   *d = PACK_COLOR_8888(0x0, src[RCOMP], src[GCOMP], src[BCOMP]);
275}
276
277static void
278pack_float_XRGB8888(const GLfloat src[4], void *dst)
279{
280   GLubyte v[4];
281   _mesa_unclamped_float_rgba_to_ubyte(v, src);
282   pack_ubyte_XRGB8888(v, dst);
283}
284
285static void
286pack_row_ubyte_XRGB8888(GLuint n, const GLubyte src[][4], void *dst)
287{
288   GLuint *d = ((GLuint *) dst);
289   GLuint i;
290   for (i = 0; i < n; i++) {
291      d[i] = PACK_COLOR_8888(0, src[i][RCOMP], src[i][GCOMP], src[i][BCOMP]);
292   }
293}
294
295static void
296pack_row_float_XRGB8888(GLuint n, const GLfloat src[][4], void *dst)
297{
298   GLuint *d = ((GLuint *) dst);
299   GLuint i;
300   for (i = 0; i < n; i++) {
301      GLubyte v[4];
302      _mesa_unclamped_float_rgba_to_ubyte(v, src[i]);
303      pack_ubyte_XRGB8888(v, d + i);
304   }
305}
306
307
308/*
309 * MESA_FORMAT_XRGB8888_REV
310 */
311
312static void
313pack_ubyte_XRGB8888_REV(const GLubyte src[4], void *dst)
314{
315   GLuint *d = ((GLuint *) dst);
316   *d = PACK_COLOR_8888(src[BCOMP], src[GCOMP], src[RCOMP], 0);
317}
318
319static void
320pack_float_XRGB8888_REV(const GLfloat src[4], void *dst)
321{
322   GLubyte v[4];
323   _mesa_unclamped_float_rgba_to_ubyte(v, src);
324   pack_ubyte_XRGB8888_REV(v, dst);
325}
326
327static void
328pack_row_ubyte_XRGB8888_REV(GLuint n, const GLubyte src[][4], void *dst)
329{
330   GLuint *d = ((GLuint *) dst);
331   GLuint i;
332   for (i = 0; i < n; i++) {
333      d[i] = PACK_COLOR_8888(src[i][BCOMP], src[i][GCOMP], src[i][RCOMP], 0);
334   }
335}
336
337static void
338pack_row_float_XRGB8888_REV(GLuint n, const GLfloat src[][4], void *dst)
339{
340   GLuint *d = ((GLuint *) dst);
341   GLuint i;
342   for (i = 0; i < n; i++) {
343      GLubyte v[4];
344      _mesa_unclamped_float_rgba_to_ubyte(v, src[i]);
345      pack_ubyte_XRGB8888_REV(v, d + i);
346   }
347}
348
349
350/*
351 * MESA_FORMAT_RGB888
352 */
353
354static void
355pack_ubyte_RGB888(const GLubyte src[4], void *dst)
356{
357   GLubyte *d = ((GLubyte *) dst);
358   d[2] = src[RCOMP];
359   d[1] = src[GCOMP];
360   d[0] = src[BCOMP];
361}
362
363static void
364pack_float_RGB888(const GLfloat src[4], void *dst)
365{
366   GLubyte *d = ((GLubyte *) dst);
367   UNCLAMPED_FLOAT_TO_UBYTE(d[2], src[RCOMP]);
368   UNCLAMPED_FLOAT_TO_UBYTE(d[1], src[GCOMP]);
369   UNCLAMPED_FLOAT_TO_UBYTE(d[0], src[BCOMP]);
370}
371
372static void
373pack_row_ubyte_RGB888(GLuint n, const GLubyte src[][4], void *dst)
374{
375   GLubyte *d = ((GLubyte *) dst);
376   GLuint i;
377   for (i = 0; i < n; i++) {
378      d[i*3+2] = src[i][RCOMP];
379      d[i*3+1] = src[i][GCOMP];
380      d[i*3+0] = src[i][BCOMP];
381   }
382}
383
384static void
385pack_row_float_RGB888(GLuint n, const GLfloat src[][4], void *dst)
386{
387   GLubyte *d = ((GLubyte *) dst);
388   GLuint i;
389   for (i = 0; i < n; i++) {
390      GLubyte v[4];
391      _mesa_unclamped_float_rgba_to_ubyte(v, src[i]);
392      d[i*3+2] = v[RCOMP];
393      d[i*3+1] = v[GCOMP];
394      d[i*3+0] = v[BCOMP];
395   }
396}
397
398
399/*
400 * MESA_FORMAT_BGR888
401 */
402
403static void
404pack_ubyte_BGR888(const GLubyte src[4], void *dst)
405{
406   GLubyte *d = ((GLubyte *) dst);
407   d[2] = src[BCOMP];
408   d[1] = src[GCOMP];
409   d[0] = src[RCOMP];
410}
411
412static void
413pack_float_BGR888(const GLfloat src[4], void *dst)
414{
415   GLubyte *d = ((GLubyte *) dst);
416   UNCLAMPED_FLOAT_TO_UBYTE(d[2], src[BCOMP]);
417   UNCLAMPED_FLOAT_TO_UBYTE(d[1], src[GCOMP]);
418   UNCLAMPED_FLOAT_TO_UBYTE(d[0], src[RCOMP]);
419}
420
421static void
422pack_row_ubyte_BGR888(GLuint n, const GLubyte src[][4], void *dst)
423{
424   GLubyte *d = ((GLubyte *) dst);
425   GLuint i;
426   for (i = 0; i < n; i++) {
427      d[i*3+2] = src[i][BCOMP];
428      d[i*3+1] = src[i][GCOMP];
429      d[i*3+0] = src[i][RCOMP];
430   }
431}
432
433static void
434pack_row_float_BGR888(GLuint n, const GLfloat src[][4], void *dst)
435{
436   GLubyte *d = ((GLubyte *) dst);
437   GLuint i;
438   for (i = 0; i < n; i++) {
439      GLubyte v[4];
440      _mesa_unclamped_float_rgba_to_ubyte(v, src[i]);
441      d[i*3+2] = v[BCOMP];
442      d[i*3+1] = v[GCOMP];
443      d[i*3+0] = v[RCOMP];
444   }
445}
446
447
448/*
449 * MESA_FORMAT_RGB565
450 */
451
452static void
453pack_ubyte_RGB565(const GLubyte src[4], void *dst)
454{
455   GLushort *d = ((GLushort *) dst);
456   *d = PACK_COLOR_565(src[RCOMP], src[GCOMP], src[BCOMP]);
457}
458
459static void
460pack_float_RGB565(const GLfloat src[4], void *dst)
461{
462   GLubyte v[3];
463   UNCLAMPED_FLOAT_TO_UBYTE(v[0], src[RCOMP]);
464   UNCLAMPED_FLOAT_TO_UBYTE(v[1], src[GCOMP]);
465   UNCLAMPED_FLOAT_TO_UBYTE(v[2], src[BCOMP]);
466   pack_ubyte_RGB565(v, dst);
467}
468
469static void
470pack_row_ubyte_RGB565(GLuint n, const GLubyte src[][4], void *dst)
471{
472   GLushort *d = ((GLushort *) dst);
473   GLuint i;
474   for (i = 0; i < n; i++) {
475      pack_ubyte_RGB565(src[i], d + i);
476   }
477}
478
479static void
480pack_row_float_RGB565(GLuint n, const GLfloat src[][4], void *dst)
481{
482   GLushort *d = ((GLushort *) dst);
483   GLuint i;
484   for (i = 0; i < n; i++) {
485      GLubyte v[4];
486      _mesa_unclamped_float_rgba_to_ubyte(v, src[i]);
487      pack_ubyte_RGB565(v, d + i);
488   }
489}
490
491
492/*
493 * MESA_FORMAT_RGB565_REV
494 */
495
496static void
497pack_ubyte_RGB565_REV(const GLubyte src[4], void *dst)
498{
499   GLushort *d = ((GLushort *) dst);
500   *d = PACK_COLOR_565_REV(src[RCOMP], src[GCOMP], src[BCOMP]);
501}
502
503static void
504pack_float_RGB565_REV(const GLfloat src[4], void *dst)
505{
506   GLushort *d = ((GLushort *) dst);
507   GLubyte r, g, b;
508   UNCLAMPED_FLOAT_TO_UBYTE(r, src[RCOMP]);
509   UNCLAMPED_FLOAT_TO_UBYTE(g, src[GCOMP]);
510   UNCLAMPED_FLOAT_TO_UBYTE(b, src[BCOMP]);
511   *d = PACK_COLOR_565_REV(r, g, b);
512}
513
514static void
515pack_row_ubyte_RGB565_REV(GLuint n, const GLubyte src[][4], void *dst)
516{
517   GLushort *d = ((GLushort *) dst);
518   GLuint i;
519   for (i = 0; i < n; i++) {
520      pack_ubyte_RGB565_REV(src[i], d + i);
521   }
522}
523
524static void
525pack_row_float_RGB565_REV(GLuint n, const GLfloat src[][4], void *dst)
526{
527   GLushort *d = ((GLushort *) dst);
528   GLuint i;
529   for (i = 0; i < n; i++) {
530      GLubyte v[4];
531      _mesa_unclamped_float_rgba_to_ubyte(v, src[i]);
532      pack_ubyte_RGB565_REV(v, d + i);
533   }
534}
535
536
537/*
538 * MESA_FORMAT_ARGB4444
539 */
540
541static void
542pack_ubyte_ARGB4444(const GLubyte src[4], void *dst)
543{
544   GLushort *d = ((GLushort *) dst);
545   *d = PACK_COLOR_4444(src[ACOMP], src[RCOMP], src[GCOMP], src[BCOMP]);
546}
547
548static void
549pack_float_ARGB4444(const GLfloat src[4], void *dst)
550{
551   GLubyte v[4];
552   _mesa_unclamped_float_rgba_to_ubyte(v, src);
553   pack_ubyte_ARGB4444(v, dst);
554}
555
556/* use fallback row packing functions */
557
558
559/*
560 * MESA_FORMAT_ARGB4444_REV
561 */
562
563static void
564pack_ubyte_ARGB4444_REV(const GLubyte src[4], void *dst)
565{
566   GLushort *d = ((GLushort *) dst);
567   *d = PACK_COLOR_4444(src[GCOMP], src[BCOMP], src[ACOMP], src[RCOMP]);
568}
569
570static void
571pack_float_ARGB4444_REV(const GLfloat src[4], void *dst)
572{
573   GLubyte v[4];
574   _mesa_unclamped_float_rgba_to_ubyte(v, src);
575   pack_ubyte_ARGB4444_REV(v, dst);
576}
577
578/* use fallback row packing functions */
579
580
581/*
582 * MESA_FORMAT_RGBA5551
583 */
584
585static void
586pack_ubyte_RGBA5551(const GLubyte src[4], void *dst)
587{
588   GLushort *d = ((GLushort *) dst);
589   *d = PACK_COLOR_5551(src[RCOMP], src[GCOMP], src[BCOMP], src[ACOMP]);
590}
591
592static void
593pack_float_RGBA5551(const GLfloat src[4], void *dst)
594{
595   GLubyte v[4];
596   _mesa_unclamped_float_rgba_to_ubyte(v, src);
597   pack_ubyte_RGBA5551(v, dst);
598}
599
600/* use fallback row packing functions */
601
602
603/*
604 * MESA_FORMAT_ARGB1555
605 */
606
607static void
608pack_ubyte_ARGB1555(const GLubyte src[4], void *dst)
609{
610   GLushort *d = ((GLushort *) dst);
611   *d = PACK_COLOR_1555(src[ACOMP], src[RCOMP], src[GCOMP], src[BCOMP]);
612}
613
614static void
615pack_float_ARGB1555(const GLfloat src[4], void *dst)
616{
617   GLubyte v[4];
618   _mesa_unclamped_float_rgba_to_ubyte(v, src);
619   pack_ubyte_ARGB1555(v, dst);
620}
621
622
623/* MESA_FORMAT_ARGB1555_REV */
624
625static void
626pack_ubyte_ARGB1555_REV(const GLubyte src[4], void *dst)
627{
628   GLushort *d = ((GLushort *) dst), tmp;
629   tmp = PACK_COLOR_1555(src[ACOMP], src[RCOMP], src[GCOMP], src[BCOMP]);
630   *d = (tmp >> 8) | (tmp << 8);
631}
632
633static void
634pack_float_ARGB1555_REV(const GLfloat src[4], void *dst)
635{
636   GLubyte v[4];
637   _mesa_unclamped_float_rgba_to_ubyte(v, src);
638   pack_ubyte_ARGB1555_REV(v, dst);
639}
640
641
642/* MESA_FORMAT_AL44 */
643
644static void
645pack_ubyte_AL44(const GLubyte src[4], void *dst)
646{
647   GLubyte *d = ((GLubyte *) dst);
648   *d = PACK_COLOR_44(src[ACOMP], src[RCOMP]);
649}
650
651static void
652pack_float_AL44(const GLfloat src[4], void *dst)
653{
654   GLubyte v[4];
655   UNCLAMPED_FLOAT_TO_UBYTE(v[0], src[RCOMP]);
656   UNCLAMPED_FLOAT_TO_UBYTE(v[3], src[ACOMP]);
657   pack_ubyte_AL44(v, dst);
658}
659
660
661/* MESA_FORMAT_AL88 */
662
663static void
664pack_ubyte_AL88(const GLubyte src[4], void *dst)
665{
666   GLushort *d = ((GLushort *) dst);
667   *d = PACK_COLOR_88(src[ACOMP], src[RCOMP]);
668}
669
670static void
671pack_float_AL88(const GLfloat src[4], void *dst)
672{
673   GLubyte v[4];
674   UNCLAMPED_FLOAT_TO_UBYTE(v[0], src[RCOMP]);
675   UNCLAMPED_FLOAT_TO_UBYTE(v[3], src[ACOMP]);
676   pack_ubyte_AL88(v, dst);
677}
678
679
680/* MESA_FORMAT_AL88_REV */
681
682static void
683pack_ubyte_AL88_REV(const GLubyte src[4], void *dst)
684{
685   GLushort *d = ((GLushort *) dst);
686   *d = PACK_COLOR_88(src[RCOMP], src[ACOMP]);
687}
688
689static void
690pack_float_AL88_REV(const GLfloat src[4], void *dst)
691{
692   GLubyte v[4];
693   UNCLAMPED_FLOAT_TO_UBYTE(v[0], src[RCOMP]);
694   UNCLAMPED_FLOAT_TO_UBYTE(v[3], src[ACOMP]);
695   pack_ubyte_AL88_REV(v, dst);
696}
697
698
699/* MESA_FORMAT_AL1616 */
700
701static void
702pack_ubyte_AL1616(const GLubyte src[4], void *dst)
703{
704   GLuint *d = ((GLuint *) dst);
705   GLushort l = UBYTE_TO_USHORT(src[RCOMP]);
706   GLushort a = UBYTE_TO_USHORT(src[ACOMP]);
707   *d = PACK_COLOR_1616(a, l);
708}
709
710static void
711pack_float_AL1616(const GLfloat src[4], void *dst)
712{
713   GLuint *d = ((GLuint *) dst);
714   GLushort l, a;
715   UNCLAMPED_FLOAT_TO_USHORT(l, src[RCOMP]);
716   UNCLAMPED_FLOAT_TO_USHORT(a, src[ACOMP]);
717   *d = PACK_COLOR_1616(a, l);
718}
719
720
721/* MESA_FORMAT_AL1616_REV */
722
723static void
724pack_ubyte_AL1616_REV(const GLubyte src[4], void *dst)
725{
726   GLuint *d = ((GLuint *) dst);
727   GLushort l = UBYTE_TO_USHORT(src[RCOMP]);
728   GLushort a = UBYTE_TO_USHORT(src[ACOMP]);
729   *d = PACK_COLOR_1616(l, a);
730}
731
732static void
733pack_float_AL1616_REV(const GLfloat src[4], void *dst)
734{
735   GLuint *d = ((GLuint *) dst);
736   GLushort l, a;
737   UNCLAMPED_FLOAT_TO_USHORT(l, src[RCOMP]);
738   UNCLAMPED_FLOAT_TO_USHORT(a, src[ACOMP]);
739   *d = PACK_COLOR_1616(l, a);
740}
741
742
743/* MESA_FORMAT_RGB332 */
744
745static void
746pack_ubyte_RGB332(const GLubyte src[4], void *dst)
747{
748   GLubyte *d = ((GLubyte *) dst);
749   *d = PACK_COLOR_332(src[RCOMP], src[GCOMP], src[BCOMP]);
750}
751
752static void
753pack_float_RGB332(const GLfloat src[4], void *dst)
754{
755   GLubyte v[4];
756   UNCLAMPED_FLOAT_TO_UBYTE(v[0], src[RCOMP]);
757   UNCLAMPED_FLOAT_TO_UBYTE(v[1], src[GCOMP]);
758   UNCLAMPED_FLOAT_TO_UBYTE(v[2], src[BCOMP]);
759   pack_ubyte_RGB332(v, dst);
760}
761
762
763/* MESA_FORMAT_A8 */
764
765static void
766pack_ubyte_A8(const GLubyte src[4], void *dst)
767{
768   GLubyte *d = ((GLubyte *) dst);
769   *d = src[ACOMP];
770}
771
772static void
773pack_float_A8(const GLfloat src[4], void *dst)
774{
775   GLubyte *d = ((GLubyte *) dst);
776   UNCLAMPED_FLOAT_TO_UBYTE(d[0], src[ACOMP]);
777}
778
779
780/* MESA_FORMAT_A16 */
781
782static void
783pack_ubyte_A16(const GLubyte src[4], void *dst)
784{
785   GLushort *d = ((GLushort *) dst);
786   *d = UBYTE_TO_USHORT(src[ACOMP]);
787}
788
789static void
790pack_float_A16(const GLfloat src[4], void *dst)
791{
792   GLushort *d = ((GLushort *) dst);
793   UNCLAMPED_FLOAT_TO_USHORT(d[0], src[ACOMP]);
794}
795
796
797/* MESA_FORMAT_L8 */
798
799static void
800pack_ubyte_L8(const GLubyte src[4], void *dst)
801{
802   GLubyte *d = ((GLubyte *) dst);
803   *d = src[RCOMP];
804}
805
806static void
807pack_float_L8(const GLfloat src[4], void *dst)
808{
809   GLubyte *d = ((GLubyte *) dst);
810   UNCLAMPED_FLOAT_TO_UBYTE(d[0], src[RCOMP]);
811}
812
813
814/* MESA_FORMAT_L16 */
815
816static void
817pack_ubyte_L16(const GLubyte src[4], void *dst)
818{
819   GLushort *d = ((GLushort *) dst);
820   *d = UBYTE_TO_USHORT(src[RCOMP]);
821}
822
823static void
824pack_float_L16(const GLfloat src[4], void *dst)
825{
826   GLushort *d = ((GLushort *) dst);
827   UNCLAMPED_FLOAT_TO_USHORT(d[0], src[RCOMP]);
828}
829
830
831/* MESA_FORMAT_YCBCR */
832
833static void
834pack_ubyte_YCBCR(const GLubyte src[4], void *dst)
835{
836   /* todo */
837}
838
839static void
840pack_float_YCBCR(const GLfloat src[4], void *dst)
841{
842   /* todo */
843}
844
845
846/* MESA_FORMAT_YCBCR_REV */
847
848static void
849pack_ubyte_YCBCR_REV(const GLubyte src[4], void *dst)
850{
851   /* todo */
852}
853
854static void
855pack_float_YCBCR_REV(const GLfloat src[4], void *dst)
856{
857   /* todo */
858}
859
860
861/* MESA_FORMAT_R8 */
862
863static void
864pack_ubyte_R8(const GLubyte src[4], void *dst)
865{
866   GLubyte *d = ((GLubyte *) dst);
867   *d = src[RCOMP];
868}
869
870static void
871pack_float_R8(const GLfloat src[4], void *dst)
872{
873   GLubyte *d = ((GLubyte *) dst);
874   GLubyte r;
875   UNCLAMPED_FLOAT_TO_UBYTE(r, src[RCOMP]);
876   d[0] = r;
877}
878
879
880/* MESA_FORMAT_GR88 */
881
882static void
883pack_ubyte_GR88(const GLubyte src[4], void *dst)
884{
885   GLubyte *d = ((GLubyte *) dst);
886   *d = PACK_COLOR_88(src[GCOMP], src[RCOMP]);
887}
888
889static void
890pack_float_GR88(const GLfloat src[4], void *dst)
891{
892   GLushort *d = ((GLushort *) dst);
893   GLubyte r, g;
894   UNCLAMPED_FLOAT_TO_UBYTE(r, src[RCOMP]);
895   UNCLAMPED_FLOAT_TO_UBYTE(g, src[GCOMP]);
896   *d = PACK_COLOR_88(g, r);
897}
898
899
900/* MESA_FORMAT_RG88 */
901
902static void
903pack_ubyte_RG88(const GLubyte src[4], void *dst)
904{
905   GLubyte *d = ((GLubyte *) dst);
906   *d = PACK_COLOR_88(src[RCOMP], src[GCOMP]);
907}
908
909static void
910pack_float_RG88(const GLfloat src[4], void *dst)
911{
912   GLushort *d = ((GLushort *) dst);
913   GLubyte r, g;
914   UNCLAMPED_FLOAT_TO_UBYTE(r, src[RCOMP]);
915   UNCLAMPED_FLOAT_TO_UBYTE(g, src[GCOMP]);
916   *d = PACK_COLOR_88(r, g);
917}
918
919
920/* MESA_FORMAT_R16 */
921
922static void
923pack_ubyte_R16(const GLubyte src[4], void *dst)
924{
925   GLushort *d = ((GLushort *) dst);
926   *d = UBYTE_TO_USHORT(src[RCOMP]);
927}
928
929static void
930pack_float_R16(const GLfloat src[4], void *dst)
931{
932   GLushort *d = ((GLushort *) dst);
933   UNCLAMPED_FLOAT_TO_USHORT(d[0], src[RCOMP]);
934}
935
936
937/* MESA_FORMAT_RG1616 */
938
939static void
940pack_ubyte_RG1616(const GLubyte src[4], void *dst)
941{
942   GLuint *d = ((GLuint *) dst);
943   GLushort r = UBYTE_TO_USHORT(src[RCOMP]);
944   GLushort g = UBYTE_TO_USHORT(src[GCOMP]);
945   *d = PACK_COLOR_1616(g, r);
946}
947
948static void
949pack_float_RG1616(const GLfloat src[4], void *dst)
950{
951   GLuint *d = ((GLuint *) dst);
952   GLushort r, g;
953   UNCLAMPED_FLOAT_TO_USHORT(r, src[RCOMP]);
954   UNCLAMPED_FLOAT_TO_USHORT(g, src[GCOMP]);
955   *d = PACK_COLOR_1616(g, r);
956}
957
958
959/* MESA_FORMAT_RG1616_REV */
960
961static void
962pack_ubyte_RG1616_REV(const GLubyte src[4], void *dst)
963{
964   GLuint *d = ((GLuint *) dst);
965   GLushort r = UBYTE_TO_USHORT(src[RCOMP]);
966   GLushort g = UBYTE_TO_USHORT(src[GCOMP]);
967   *d = PACK_COLOR_1616(r, g);
968}
969
970
971static void
972pack_float_RG1616_REV(const GLfloat src[4], void *dst)
973{
974   GLuint *d = ((GLuint *) dst);
975   GLushort r, g;
976   UNCLAMPED_FLOAT_TO_USHORT(r, src[RCOMP]);
977   UNCLAMPED_FLOAT_TO_USHORT(g, src[GCOMP]);
978   *d = PACK_COLOR_1616(r, g);
979}
980
981
982/* MESA_FORMAT_ARGB2101010 */
983
984static void
985pack_ubyte_ARGB2101010(const GLubyte src[4], void *dst)
986{
987   GLuint *d = ((GLuint *) dst);
988   GLushort r = UBYTE_TO_USHORT(src[RCOMP]);
989   GLushort g = UBYTE_TO_USHORT(src[GCOMP]);
990   GLushort b = UBYTE_TO_USHORT(src[BCOMP]);
991   GLushort a = UBYTE_TO_USHORT(src[ACOMP]);
992   *d = PACK_COLOR_2101010_US(a, r, g, b);
993}
994
995static void
996pack_float_ARGB2101010(const GLfloat src[4], void *dst)
997{
998   GLuint *d = ((GLuint *) dst);
999   GLushort r, g, b, a;
1000   UNCLAMPED_FLOAT_TO_USHORT(r, src[RCOMP]);
1001   UNCLAMPED_FLOAT_TO_USHORT(g, src[GCOMP]);
1002   UNCLAMPED_FLOAT_TO_USHORT(b, src[BCOMP]);
1003   UNCLAMPED_FLOAT_TO_USHORT(a, src[ACOMP]);
1004   *d = PACK_COLOR_2101010_US(a, r, g, b);
1005}
1006
1007
1008/* MESA_FORMAT_ABGR2101010_UINT */
1009
1010static void
1011pack_ubyte_ABGR2101010_UINT(const GLubyte src[4], void *dst)
1012{
1013   GLuint *d = ((GLuint *) dst);
1014   GLushort r = UBYTE_TO_USHORT(src[RCOMP]);
1015   GLushort g = UBYTE_TO_USHORT(src[GCOMP]);
1016   GLushort b = UBYTE_TO_USHORT(src[BCOMP]);
1017   GLushort a = UBYTE_TO_USHORT(src[ACOMP]);
1018   *d = PACK_COLOR_2101010_US(a, b, g, r);
1019}
1020
1021static void
1022pack_float_ABGR2101010_UINT(const GLfloat src[4], void *dst)
1023{
1024   GLuint *d = ((GLuint *) dst);
1025   GLushort r, g, b, a;
1026   UNCLAMPED_FLOAT_TO_USHORT(r, src[RCOMP]);
1027   UNCLAMPED_FLOAT_TO_USHORT(g, src[GCOMP]);
1028   UNCLAMPED_FLOAT_TO_USHORT(b, src[BCOMP]);
1029   UNCLAMPED_FLOAT_TO_USHORT(a, src[ACOMP]);
1030   *d = PACK_COLOR_2101010_US(a, b, g, r);
1031}
1032
1033
1034/* MESA_FORMAT_SRGB8 */
1035
1036static void
1037pack_ubyte_SRGB8(const GLubyte src[4], void *dst)
1038{
1039   GLubyte *d = ((GLubyte *) dst);
1040   d[2] = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
1041   d[1] = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
1042   d[0] = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
1043}
1044
1045static void
1046pack_float_SRGB8(const GLfloat src[4], void *dst)
1047{
1048   GLubyte *d = ((GLubyte *) dst);
1049   d[2] = linear_float_to_srgb_ubyte(src[RCOMP]);
1050   d[1] = linear_float_to_srgb_ubyte(src[GCOMP]);
1051   d[0] = linear_float_to_srgb_ubyte(src[BCOMP]);
1052}
1053
1054
1055/* MESA_FORMAT_SRGBA8 */
1056
1057static void
1058pack_ubyte_SRGBA8(const GLubyte src[4], void *dst)
1059{
1060   GLuint *d = ((GLuint *) dst);
1061   GLubyte r = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
1062   GLubyte g = linear_ubyte_to_srgb_ubyte(src[GCOMP]);
1063   GLubyte b = linear_ubyte_to_srgb_ubyte(src[BCOMP]);
1064   *d = PACK_COLOR_8888(r, g, b, src[ACOMP]);
1065}
1066
1067static void
1068pack_float_SRGBA8(const GLfloat src[4], void *dst)
1069{
1070   GLuint *d = ((GLuint *) dst);
1071   GLubyte r, g, b, a;
1072   r = linear_float_to_srgb_ubyte(src[RCOMP]);
1073   g = linear_float_to_srgb_ubyte(src[GCOMP]);
1074   b = linear_float_to_srgb_ubyte(src[BCOMP]);
1075   UNCLAMPED_FLOAT_TO_UBYTE(a, src[ACOMP]);
1076   *d = PACK_COLOR_8888(r, g, b, a);
1077}
1078
1079
1080/* MESA_FORMAT_SARGB8 */
1081
1082static void
1083pack_ubyte_SARGB8(const GLubyte src[4], void *dst)
1084{
1085   GLuint *d = ((GLuint *) dst);
1086   GLubyte r = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
1087   GLubyte g = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
1088   GLubyte b = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
1089   *d = PACK_COLOR_8888(src[ACOMP], r, g, b);
1090}
1091
1092static void
1093pack_float_SARGB8(const GLfloat src[4], void *dst)
1094{
1095   GLuint *d = ((GLuint *) dst);
1096   GLubyte r, g, b, a;
1097   r = linear_float_to_srgb_ubyte(src[RCOMP]);
1098   g = linear_float_to_srgb_ubyte(src[GCOMP]);
1099   b = linear_float_to_srgb_ubyte(src[BCOMP]);
1100   UNCLAMPED_FLOAT_TO_UBYTE(a, src[ACOMP]);
1101   *d = PACK_COLOR_8888(a, r, g, b);
1102}
1103
1104
1105/* MESA_FORMAT_SL8 */
1106
1107static void
1108pack_ubyte_SL8(const GLubyte src[4], void *dst)
1109{
1110   GLubyte *d = ((GLubyte *) dst);
1111   *d = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
1112}
1113
1114static void
1115pack_float_SL8(const GLfloat src[4], void *dst)
1116{
1117   GLubyte *d = ((GLubyte *) dst);
1118   GLubyte l = linear_float_to_srgb_ubyte(src[RCOMP]);
1119   *d = l;
1120}
1121
1122
1123/* MESA_FORMAT_SLA8 */
1124
1125static void
1126pack_ubyte_SLA8(const GLubyte src[4], void *dst)
1127{
1128   GLushort *d = ((GLushort *) dst);
1129   GLubyte l = linear_ubyte_to_srgb_ubyte(src[RCOMP]);
1130   *d = PACK_COLOR_88(src[ACOMP], l);
1131}
1132
1133static void
1134pack_float_SLA8(const GLfloat src[4], void *dst)
1135{
1136   GLushort *d = ((GLushort *) dst);
1137   GLubyte a, l = linear_float_to_srgb_ubyte(src[RCOMP]);
1138   CLAMPED_FLOAT_TO_UBYTE(a, src[ACOMP]);
1139   *d = PACK_COLOR_88(a, l);
1140}
1141
1142
1143/* MESA_FORMAT_RGBA_FLOAT32 */
1144
1145static void
1146pack_ubyte_RGBA_FLOAT32(const GLubyte src[4], void *dst)
1147{
1148   GLfloat *d = ((GLfloat *) dst);
1149   d[0] = UBYTE_TO_FLOAT(src[0]);
1150   d[1] = UBYTE_TO_FLOAT(src[1]);
1151   d[2] = UBYTE_TO_FLOAT(src[2]);
1152   d[3] = UBYTE_TO_FLOAT(src[3]);
1153}
1154
1155static void
1156pack_float_RGBA_FLOAT32(const GLfloat src[4], void *dst)
1157{
1158   GLfloat *d = ((GLfloat *) dst);
1159   d[0] = src[0];
1160   d[1] = src[1];
1161   d[2] = src[2];
1162   d[3] = src[3];
1163}
1164
1165
1166/* MESA_FORMAT_RGBA_FLOAT16 */
1167
1168static void
1169pack_ubyte_RGBA_FLOAT16(const GLubyte src[4], void *dst)
1170{
1171   GLhalfARB *d = ((GLhalfARB *) dst);
1172   d[0] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[0]));
1173   d[1] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[1]));
1174   d[2] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[2]));
1175   d[3] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[3]));
1176}
1177
1178static void
1179pack_float_RGBA_FLOAT16(const GLfloat src[4], void *dst)
1180{
1181   GLhalfARB *d = ((GLhalfARB *) dst);
1182   d[0] = _mesa_float_to_half(src[0]);
1183   d[1] = _mesa_float_to_half(src[1]);
1184   d[2] = _mesa_float_to_half(src[2]);
1185   d[3] = _mesa_float_to_half(src[3]);
1186}
1187
1188
1189/* MESA_FORMAT_RGB_FLOAT32 */
1190
1191static void
1192pack_ubyte_RGB_FLOAT32(const GLubyte src[4], void *dst)
1193{
1194   GLfloat *d = ((GLfloat *) dst);
1195   d[0] = UBYTE_TO_FLOAT(src[0]);
1196   d[1] = UBYTE_TO_FLOAT(src[1]);
1197   d[2] = UBYTE_TO_FLOAT(src[2]);
1198}
1199
1200static void
1201pack_float_RGB_FLOAT32(const GLfloat src[4], void *dst)
1202{
1203   GLfloat *d = ((GLfloat *) dst);
1204   d[0] = src[0];
1205   d[1] = src[1];
1206   d[2] = src[2];
1207}
1208
1209
1210/* MESA_FORMAT_RGB_FLOAT16 */
1211
1212static void
1213pack_ubyte_RGB_FLOAT16(const GLubyte src[4], void *dst)
1214{
1215   GLhalfARB *d = ((GLhalfARB *) dst);
1216   d[0] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[0]));
1217   d[1] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[1]));
1218   d[2] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[2]));
1219}
1220
1221static void
1222pack_float_RGB_FLOAT16(const GLfloat src[4], void *dst)
1223{
1224   GLhalfARB *d = ((GLhalfARB *) dst);
1225   d[0] = _mesa_float_to_half(src[0]);
1226   d[1] = _mesa_float_to_half(src[1]);
1227   d[2] = _mesa_float_to_half(src[2]);
1228}
1229
1230
1231/* MESA_FORMAT_ALPHA_FLOAT32 */
1232
1233static void
1234pack_ubyte_ALPHA_FLOAT32(const GLubyte src[4], void *dst)
1235{
1236   GLfloat *d = ((GLfloat *) dst);
1237   d[0] = UBYTE_TO_FLOAT(src[ACOMP]);
1238}
1239
1240static void
1241pack_float_ALPHA_FLOAT32(const GLfloat src[4], void *dst)
1242{
1243   GLfloat *d = ((GLfloat *) dst);
1244   d[0] = src[ACOMP];
1245}
1246
1247
1248/* MESA_FORMAT_ALPHA_FLOAT16 */
1249
1250static void
1251pack_ubyte_ALPHA_FLOAT16(const GLubyte src[4], void *dst)
1252{
1253   GLhalfARB *d = ((GLhalfARB *) dst);
1254   d[0] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[ACOMP]));
1255}
1256
1257static void
1258pack_float_ALPHA_FLOAT16(const GLfloat src[4], void *dst)
1259{
1260   GLhalfARB *d = ((GLhalfARB *) dst);
1261   d[0] = _mesa_float_to_half(src[ACOMP]);
1262}
1263
1264
1265/* MESA_FORMAT_LUMINANCE_FLOAT32 (and INTENSITY_FLOAT32, R_FLOAT32) */
1266
1267static void
1268pack_ubyte_LUMINANCE_FLOAT32(const GLubyte src[4], void *dst)
1269{
1270   GLfloat *d = ((GLfloat *) dst);
1271   d[0] = UBYTE_TO_FLOAT(src[RCOMP]);
1272}
1273
1274static void
1275pack_float_LUMINANCE_FLOAT32(const GLfloat src[4], void *dst)
1276{
1277   GLfloat *d = ((GLfloat *) dst);
1278   d[0] = src[RCOMP];
1279}
1280
1281
1282/* MESA_FORMAT_LUMINANCE_FLOAT16 (and INTENSITY_FLOAT16, R_FLOAT32) */
1283
1284static void
1285pack_ubyte_LUMINANCE_FLOAT16(const GLubyte src[4], void *dst)
1286{
1287   GLhalfARB *d = ((GLhalfARB *) dst);
1288   d[0] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[RCOMP]));
1289}
1290
1291static void
1292pack_float_LUMINANCE_FLOAT16(const GLfloat src[4], void *dst)
1293{
1294   GLhalfARB *d = ((GLhalfARB *) dst);
1295   d[0] = _mesa_float_to_half(src[RCOMP]);
1296}
1297
1298
1299/* MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32 */
1300
1301static void
1302pack_ubyte_LUMINANCE_ALPHA_FLOAT32(const GLubyte src[4], void *dst)
1303{
1304   GLfloat *d = ((GLfloat *) dst);
1305   d[0] = UBYTE_TO_FLOAT(src[RCOMP]);
1306   d[1] = UBYTE_TO_FLOAT(src[ACOMP]);
1307}
1308
1309static void
1310pack_float_LUMINANCE_ALPHA_FLOAT32(const GLfloat src[4], void *dst)
1311{
1312   GLfloat *d = ((GLfloat *) dst);
1313   d[0] = src[RCOMP];
1314   d[1] = src[ACOMP];
1315}
1316
1317
1318/* MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16 */
1319
1320static void
1321pack_ubyte_LUMINANCE_ALPHA_FLOAT16(const GLubyte src[4], void *dst)
1322{
1323   GLhalfARB *d = ((GLhalfARB *) dst);
1324   d[0] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[RCOMP]));
1325   d[1] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[ACOMP]));
1326}
1327
1328static void
1329pack_float_LUMINANCE_ALPHA_FLOAT16(const GLfloat src[4], void *dst)
1330{
1331   GLhalfARB *d = ((GLhalfARB *) dst);
1332   d[0] = _mesa_float_to_half(src[RCOMP]);
1333   d[1] = _mesa_float_to_half(src[ACOMP]);
1334}
1335
1336
1337/* MESA_FORMAT_RG_FLOAT32 */
1338
1339static void
1340pack_ubyte_RG_FLOAT32(const GLubyte src[4], void *dst)
1341{
1342   GLfloat *d = ((GLfloat *) dst);
1343   d[0] = UBYTE_TO_FLOAT(src[RCOMP]);
1344   d[1] = UBYTE_TO_FLOAT(src[GCOMP]);
1345}
1346
1347static void
1348pack_float_RG_FLOAT32(const GLfloat src[4], void *dst)
1349{
1350   GLfloat *d = ((GLfloat *) dst);
1351   d[0] = src[RCOMP];
1352   d[1] = src[GCOMP];
1353}
1354
1355
1356/* MESA_FORMAT_RG_FLOAT16 */
1357
1358static void
1359pack_ubyte_RG_FLOAT16(const GLubyte src[4], void *dst)
1360{
1361   GLhalfARB *d = ((GLhalfARB *) dst);
1362   d[0] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[RCOMP]));
1363   d[1] = _mesa_float_to_half(UBYTE_TO_FLOAT(src[GCOMP]));
1364}
1365
1366static void
1367pack_float_RG_FLOAT16(const GLfloat src[4], void *dst)
1368{
1369   GLhalfARB *d = ((GLhalfARB *) dst);
1370   d[0] = _mesa_float_to_half(src[RCOMP]);
1371   d[1] = _mesa_float_to_half(src[GCOMP]);
1372}
1373
1374
1375/* MESA_FORMAT_DUDV8 */
1376
1377static void
1378pack_ubyte_DUDV8(const GLubyte src[4], void *dst)
1379{
1380   /* XXX is this ever used? */
1381   GLushort *d = ((GLushort *) dst);
1382   *d = PACK_COLOR_88(src[0], src[1]);
1383}
1384
1385static void
1386pack_float_DUDV8(const GLfloat src[4], void *dst)
1387{
1388   GLushort *d = ((GLushort *) dst);
1389   GLbyte du, dv;
1390   du = FLOAT_TO_BYTE(CLAMP(src[0], 0.0F, 1.0F));
1391   dv = FLOAT_TO_BYTE(CLAMP(src[1], 0.0F, 1.0F));
1392   *d = PACK_COLOR_88(du, dv);
1393}
1394
1395
1396/* MESA_FORMAT_RGBA_16 */
1397
1398static void
1399pack_ubyte_RGBA_16(const GLubyte src[4], void *dst)
1400{
1401   GLushort *d = ((GLushort *) dst);
1402   d[0] = UBYTE_TO_USHORT(src[RCOMP]);
1403   d[1] = UBYTE_TO_USHORT(src[GCOMP]);
1404   d[2] = UBYTE_TO_USHORT(src[BCOMP]);
1405   d[3] = UBYTE_TO_USHORT(src[ACOMP]);
1406}
1407
1408static void
1409pack_float_RGBA_16(const GLfloat src[4], void *dst)
1410{
1411   GLushort *d = ((GLushort *) dst);
1412   UNCLAMPED_FLOAT_TO_USHORT(d[0], src[RCOMP]);
1413   UNCLAMPED_FLOAT_TO_USHORT(d[1], src[GCOMP]);
1414   UNCLAMPED_FLOAT_TO_USHORT(d[2], src[BCOMP]);
1415   UNCLAMPED_FLOAT_TO_USHORT(d[3], src[ACOMP]);
1416}
1417
1418
1419
1420/*
1421 * MESA_FORMAT_SIGNED_R8
1422 */
1423
1424static void
1425pack_float_SIGNED_R8(const GLfloat src[4], void *dst)
1426{
1427   GLbyte *d = (GLbyte *) dst;
1428   *d = FLOAT_TO_BYTE(CLAMP(src[RCOMP], -1.0f, 1.0f));
1429}
1430
1431
1432/*
1433 * MESA_FORMAT_SIGNED_RG88_REV
1434 */
1435
1436static void
1437pack_float_SIGNED_RG88_REV(const GLfloat src[4], void *dst)
1438{
1439   GLushort *d = (GLushort *) dst;
1440   GLbyte r = FLOAT_TO_BYTE(CLAMP(src[RCOMP], -1.0f, 1.0f));
1441   GLbyte g = FLOAT_TO_BYTE(CLAMP(src[GCOMP], -1.0f, 1.0f));
1442   *d = (g << 8) | r;
1443}
1444
1445
1446/*
1447 * MESA_FORMAT_SIGNED_RGBX8888
1448 */
1449
1450static void
1451pack_float_SIGNED_RGBX8888(const GLfloat src[4], void *dst)
1452{
1453   GLuint *d = (GLuint *) dst;
1454   GLbyte r = FLOAT_TO_BYTE(CLAMP(src[RCOMP], -1.0f, 1.0f));
1455   GLbyte g = FLOAT_TO_BYTE(CLAMP(src[GCOMP], -1.0f, 1.0f));
1456   GLbyte b = FLOAT_TO_BYTE(CLAMP(src[BCOMP], -1.0f, 1.0f));
1457   GLbyte a = 127;
1458   *d = PACK_COLOR_8888(r, g, b, a);
1459}
1460
1461
1462/*
1463 * MESA_FORMAT_SIGNED_RGBA8888
1464 */
1465
1466static void
1467pack_float_SIGNED_RGBA8888(const GLfloat src[4], void *dst)
1468{
1469   GLuint *d = (GLuint *) dst;
1470   GLbyte r = FLOAT_TO_BYTE(CLAMP(src[RCOMP], -1.0f, 1.0f));
1471   GLbyte g = FLOAT_TO_BYTE(CLAMP(src[GCOMP], -1.0f, 1.0f));
1472   GLbyte b = FLOAT_TO_BYTE(CLAMP(src[BCOMP], -1.0f, 1.0f));
1473   GLbyte a = FLOAT_TO_BYTE(CLAMP(src[ACOMP], -1.0f, 1.0f));
1474   *d = PACK_COLOR_8888(r, g, b, a);
1475}
1476
1477
1478/*
1479 * MESA_FORMAT_SIGNED_RGBA8888_REV
1480 */
1481
1482static void
1483pack_float_SIGNED_RGBA8888_REV(const GLfloat src[4], void *dst)
1484{
1485   GLuint *d = (GLuint *) dst;
1486   GLbyte r = FLOAT_TO_BYTE(CLAMP(src[RCOMP], -1.0f, 1.0f));
1487   GLbyte g = FLOAT_TO_BYTE(CLAMP(src[GCOMP], -1.0f, 1.0f));
1488   GLbyte b = FLOAT_TO_BYTE(CLAMP(src[BCOMP], -1.0f, 1.0f));
1489   GLbyte a = FLOAT_TO_BYTE(CLAMP(src[ACOMP], -1.0f, 1.0f));
1490   *d = PACK_COLOR_8888(a, b, g, r);
1491}
1492
1493
1494/*
1495 * MESA_FORMAT_SIGNED_R16
1496 */
1497
1498static void
1499pack_float_SIGNED_R16(const GLfloat src[4], void *dst)
1500{
1501   GLshort *d = (GLshort *) dst;
1502   *d = FLOAT_TO_SHORT(CLAMP(src[RCOMP], -1.0f, 1.0f));
1503}
1504
1505
1506/*
1507 * MESA_FORMAT_SIGNED_GR1616
1508 */
1509
1510static void
1511pack_float_SIGNED_GR1616(const GLfloat src[4], void *dst)
1512{
1513   GLuint *d = (GLuint *) dst;
1514   GLshort r = FLOAT_TO_SHORT(CLAMP(src[RCOMP], -1.0f, 1.0f));
1515   GLshort g = FLOAT_TO_SHORT(CLAMP(src[GCOMP], -1.0f, 1.0f));
1516   *d = (g << 16) | (r & 0xffff);
1517}
1518
1519
1520/*
1521 * MESA_FORMAT_SIGNED_RGB_16
1522 */
1523
1524static void
1525pack_float_SIGNED_RGB_16(const GLfloat src[4], void *dst)
1526{
1527   GLshort *d = (GLshort *) dst;
1528   d[0] = FLOAT_TO_SHORT(CLAMP(src[RCOMP], -1.0f, 1.0f));
1529   d[1] = FLOAT_TO_SHORT(CLAMP(src[GCOMP], -1.0f, 1.0f));
1530   d[2] = FLOAT_TO_SHORT(CLAMP(src[BCOMP], -1.0f, 1.0f));
1531}
1532
1533
1534/*
1535 * MESA_FORMAT_SIGNED_RGBA_16
1536 */
1537
1538static void
1539pack_float_SIGNED_RGBA_16(const GLfloat src[4], void *dst)
1540{
1541   GLshort *d = (GLshort *) dst;
1542   d[0] = FLOAT_TO_SHORT(CLAMP(src[RCOMP], -1.0f, 1.0f));
1543   d[1] = FLOAT_TO_SHORT(CLAMP(src[GCOMP], -1.0f, 1.0f));
1544   d[2] = FLOAT_TO_SHORT(CLAMP(src[BCOMP], -1.0f, 1.0f));
1545   d[3] = FLOAT_TO_SHORT(CLAMP(src[ACOMP], -1.0f, 1.0f));
1546}
1547
1548
1549/*
1550 * MESA_FORMAT_SIGNED_A8
1551 */
1552
1553static void
1554pack_float_SIGNED_A8(const GLfloat src[4], void *dst)
1555{
1556   GLbyte *d = (GLbyte *) dst;
1557   *d = FLOAT_TO_BYTE(CLAMP(src[ACOMP], -1.0f, 1.0f));
1558}
1559
1560
1561/*
1562 * MESA_FORMAT_SIGNED_L8
1563 */
1564
1565static void
1566pack_float_SIGNED_L8(const GLfloat src[4], void *dst)
1567{
1568   GLbyte *d = (GLbyte *) dst;
1569   *d = FLOAT_TO_BYTE(CLAMP(src[RCOMP], -1.0f, 1.0f));
1570}
1571
1572
1573/*
1574 * MESA_FORMAT_SIGNED_AL88
1575 */
1576
1577static void
1578pack_float_SIGNED_AL88(const GLfloat src[4], void *dst)
1579{
1580   GLushort *d = (GLushort *) dst;
1581   GLbyte l = FLOAT_TO_BYTE(CLAMP(src[RCOMP], -1.0f, 1.0f));
1582   GLbyte a = FLOAT_TO_BYTE(CLAMP(src[ACOMP], -1.0f, 1.0f));
1583   *d = (a << 8) | l;
1584}
1585
1586
1587/*
1588 * MESA_FORMAT_SIGNED_A16
1589 */
1590
1591static void
1592pack_float_SIGNED_A16(const GLfloat src[4], void *dst)
1593{
1594   GLshort *d = (GLshort *) dst;
1595   *d = FLOAT_TO_SHORT(CLAMP(src[ACOMP], -1.0f, 1.0f));
1596}
1597
1598
1599/*
1600 * MESA_FORMAT_SIGNED_L16
1601 */
1602
1603static void
1604pack_float_SIGNED_L16(const GLfloat src[4], void *dst)
1605{
1606   GLshort *d = (GLshort *) dst;
1607   *d = FLOAT_TO_SHORT(CLAMP(src[RCOMP], -1.0f, 1.0f));
1608}
1609
1610
1611/*
1612 * MESA_FORMAT_SIGNED_AL1616
1613 */
1614
1615static void
1616pack_float_SIGNED_AL1616(const GLfloat src[4], void *dst)
1617{
1618   GLuint *d = (GLuint *) dst;
1619   GLshort l = FLOAT_TO_SHORT(CLAMP(src[RCOMP], -1.0f, 1.0f));
1620   GLshort a = FLOAT_TO_SHORT(CLAMP(src[ACOMP], -1.0f, 1.0f));
1621   *d = PACK_COLOR_1616(a, l);
1622}
1623
1624
1625/*
1626 * MESA_FORMAT_RGB9_E5_FLOAT;
1627 */
1628
1629static void
1630pack_float_RGB9_E5_FLOAT(const GLfloat src[4], void *dst)
1631{
1632   GLuint *d = (GLuint *) dst;
1633   *d = float3_to_rgb9e5(src);
1634}
1635
1636static void
1637pack_ubyte_RGB9_E5_FLOAT(const GLubyte src[4], void *dst)
1638{
1639   GLuint *d = (GLuint *) dst;
1640   GLfloat rgb[3];
1641   rgb[0] = UBYTE_TO_FLOAT(src[RCOMP]);
1642   rgb[1] = UBYTE_TO_FLOAT(src[GCOMP]);
1643   rgb[2] = UBYTE_TO_FLOAT(src[BCOMP]);
1644   *d = float3_to_rgb9e5(rgb);
1645}
1646
1647
1648
1649/*
1650 * MESA_FORMAT_R11_G11_B10_FLOAT;
1651 */
1652
1653static void
1654pack_ubyte_R11_G11_B10_FLOAT(const GLubyte src[4], void *dst)
1655{
1656   GLuint *d = (GLuint *) dst;
1657   GLfloat rgb[3];
1658   rgb[0] = UBYTE_TO_FLOAT(src[RCOMP]);
1659   rgb[1] = UBYTE_TO_FLOAT(src[GCOMP]);
1660   rgb[2] = UBYTE_TO_FLOAT(src[BCOMP]);
1661   *d = float3_to_r11g11b10f(rgb);
1662}
1663
1664static void
1665pack_float_R11_G11_B10_FLOAT(const GLfloat src[4], void *dst)
1666{
1667   GLuint *d = (GLuint *) dst;
1668   *d = float3_to_r11g11b10f(src);
1669}
1670
1671
1672
1673/**
1674 * Return a function that can pack a GLubyte rgba[4] color.
1675 */
1676gl_pack_ubyte_rgba_func
1677_mesa_get_pack_ubyte_rgba_function(gl_format format)
1678{
1679   static gl_pack_ubyte_rgba_func table[MESA_FORMAT_COUNT];
1680   static GLboolean initialized = GL_FALSE;
1681
1682   if (!initialized) {
1683      memset(table, 0, sizeof(table));
1684
1685      table[MESA_FORMAT_NONE] = NULL;
1686
1687      table[MESA_FORMAT_RGBA8888] = pack_ubyte_RGBA8888;
1688      table[MESA_FORMAT_RGBA8888_REV] = pack_ubyte_RGBA8888_REV;
1689      table[MESA_FORMAT_ARGB8888] = pack_ubyte_ARGB8888;
1690      table[MESA_FORMAT_ARGB8888_REV] = pack_ubyte_ARGB8888_REV;
1691      table[MESA_FORMAT_RGBX8888] = pack_ubyte_RGBA8888; /* reused */
1692      table[MESA_FORMAT_RGBX8888_REV] = pack_ubyte_RGBA8888_REV; /* reused */
1693      table[MESA_FORMAT_XRGB8888] = pack_ubyte_XRGB8888;
1694      table[MESA_FORMAT_XRGB8888_REV] = pack_ubyte_XRGB8888_REV;
1695      table[MESA_FORMAT_RGB888] = pack_ubyte_RGB888;
1696      table[MESA_FORMAT_BGR888] = pack_ubyte_BGR888;
1697      table[MESA_FORMAT_RGB565] = pack_ubyte_RGB565;
1698      table[MESA_FORMAT_RGB565_REV] = pack_ubyte_RGB565_REV;
1699      table[MESA_FORMAT_ARGB4444] = pack_ubyte_ARGB4444;
1700      table[MESA_FORMAT_ARGB4444_REV] = pack_ubyte_ARGB4444_REV;
1701      table[MESA_FORMAT_RGBA5551] = pack_ubyte_RGBA5551;
1702      table[MESA_FORMAT_ARGB1555] = pack_ubyte_ARGB1555;
1703      table[MESA_FORMAT_ARGB1555_REV] = pack_ubyte_ARGB1555_REV;
1704      table[MESA_FORMAT_AL44] = pack_ubyte_AL44;
1705      table[MESA_FORMAT_AL88] = pack_ubyte_AL88;
1706      table[MESA_FORMAT_AL88_REV] = pack_ubyte_AL88_REV;
1707      table[MESA_FORMAT_AL1616] = pack_ubyte_AL1616;
1708      table[MESA_FORMAT_AL1616_REV] = pack_ubyte_AL1616_REV;
1709      table[MESA_FORMAT_RGB332] = pack_ubyte_RGB332;
1710      table[MESA_FORMAT_A8] = pack_ubyte_A8;
1711      table[MESA_FORMAT_A16] = pack_ubyte_A16;
1712      table[MESA_FORMAT_L8] = pack_ubyte_L8;
1713      table[MESA_FORMAT_L16] = pack_ubyte_L16;
1714      table[MESA_FORMAT_I8] = pack_ubyte_L8; /* reuse pack_ubyte_L8 */
1715      table[MESA_FORMAT_I16] = pack_ubyte_L16; /* reuse pack_ubyte_L16 */
1716      table[MESA_FORMAT_YCBCR] = pack_ubyte_YCBCR;
1717      table[MESA_FORMAT_YCBCR_REV] = pack_ubyte_YCBCR_REV;
1718      table[MESA_FORMAT_R8] = pack_ubyte_R8;
1719      table[MESA_FORMAT_GR88] = pack_ubyte_GR88;
1720      table[MESA_FORMAT_RG88] = pack_ubyte_RG88;
1721      table[MESA_FORMAT_R16] = pack_ubyte_R16;
1722      table[MESA_FORMAT_RG1616] = pack_ubyte_RG1616;
1723      table[MESA_FORMAT_RG1616_REV] = pack_ubyte_RG1616_REV;
1724      table[MESA_FORMAT_ARGB2101010] = pack_ubyte_ARGB2101010;
1725      table[MESA_FORMAT_ABGR2101010_UINT] = pack_ubyte_ABGR2101010_UINT;
1726
1727      /* should never convert RGBA to these formats */
1728      table[MESA_FORMAT_Z24_S8] = NULL;
1729      table[MESA_FORMAT_S8_Z24] = NULL;
1730      table[MESA_FORMAT_Z16] = NULL;
1731      table[MESA_FORMAT_X8_Z24] = NULL;
1732      table[MESA_FORMAT_Z24_X8] = NULL;
1733      table[MESA_FORMAT_Z32] = NULL;
1734      table[MESA_FORMAT_S8] = NULL;
1735
1736      /* sRGB */
1737      table[MESA_FORMAT_SRGB8] = pack_ubyte_SRGB8;
1738      table[MESA_FORMAT_SRGBA8] = pack_ubyte_SRGBA8;
1739      table[MESA_FORMAT_SARGB8] = pack_ubyte_SARGB8;
1740      table[MESA_FORMAT_SL8] = pack_ubyte_SL8;
1741      table[MESA_FORMAT_SLA8] = pack_ubyte_SLA8;
1742
1743      /* n/a */
1744      table[MESA_FORMAT_SRGB_DXT1] = NULL; /* pack_ubyte_SRGB_DXT1; */
1745      table[MESA_FORMAT_SRGBA_DXT1] = NULL; /* pack_ubyte_SRGBA_DXT1; */
1746      table[MESA_FORMAT_SRGBA_DXT3] = NULL; /* pack_ubyte_SRGBA_DXT3; */
1747      table[MESA_FORMAT_SRGBA_DXT5] = NULL; /* pack_ubyte_SRGBA_DXT5; */
1748
1749      table[MESA_FORMAT_RGB_FXT1] = NULL; /* pack_ubyte_RGB_FXT1; */
1750      table[MESA_FORMAT_RGBA_FXT1] = NULL; /* pack_ubyte_RGBA_FXT1; */
1751      table[MESA_FORMAT_RGB_DXT1] = NULL; /* pack_ubyte_RGB_DXT1; */
1752      table[MESA_FORMAT_RGBA_DXT1] = NULL; /* pack_ubyte_RGBA_DXT1; */
1753      table[MESA_FORMAT_RGBA_DXT3] = NULL; /* pack_ubyte_RGBA_DXT3; */
1754      table[MESA_FORMAT_RGBA_DXT5] = NULL; /* pack_ubyte_RGBA_DXT5; */
1755
1756      table[MESA_FORMAT_RGBA_FLOAT32] = pack_ubyte_RGBA_FLOAT32;
1757      table[MESA_FORMAT_RGBA_FLOAT16] = pack_ubyte_RGBA_FLOAT16;
1758      table[MESA_FORMAT_RGB_FLOAT32] = pack_ubyte_RGB_FLOAT32;
1759      table[MESA_FORMAT_RGB_FLOAT16] = pack_ubyte_RGB_FLOAT16;
1760      table[MESA_FORMAT_ALPHA_FLOAT32] = pack_ubyte_ALPHA_FLOAT32;
1761      table[MESA_FORMAT_ALPHA_FLOAT16] = pack_ubyte_ALPHA_FLOAT16;
1762      table[MESA_FORMAT_LUMINANCE_FLOAT32] = pack_ubyte_LUMINANCE_FLOAT32;
1763      table[MESA_FORMAT_LUMINANCE_FLOAT16] = pack_ubyte_LUMINANCE_FLOAT16;
1764      table[MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32] = pack_ubyte_LUMINANCE_ALPHA_FLOAT32;
1765      table[MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16] = pack_ubyte_LUMINANCE_ALPHA_FLOAT16;
1766      table[MESA_FORMAT_INTENSITY_FLOAT32] = pack_ubyte_LUMINANCE_FLOAT32;
1767      table[MESA_FORMAT_INTENSITY_FLOAT16] = pack_ubyte_LUMINANCE_FLOAT16;
1768      table[MESA_FORMAT_R_FLOAT32] = pack_ubyte_LUMINANCE_FLOAT32;
1769      table[MESA_FORMAT_R_FLOAT16] = pack_ubyte_LUMINANCE_FLOAT16;
1770      table[MESA_FORMAT_RG_FLOAT32] = pack_ubyte_RG_FLOAT32;
1771      table[MESA_FORMAT_RG_FLOAT16] = pack_ubyte_RG_FLOAT16;
1772
1773      /* n/a */
1774      table[MESA_FORMAT_RGBA_INT8] = NULL; /* pack_ubyte_RGBA_INT8 */
1775      table[MESA_FORMAT_RGBA_INT16] = NULL; /* pack_ubyte_RGBA_INT16 */
1776      table[MESA_FORMAT_RGBA_INT32] = NULL; /* pack_ubyte_RGBA_INT32 */
1777      table[MESA_FORMAT_RGBA_UINT8] = NULL; /* pack_ubyte_RGBA_UINT8 */
1778      table[MESA_FORMAT_RGBA_UINT16] = NULL; /* pack_ubyte_RGBA_UINT16 */
1779      table[MESA_FORMAT_RGBA_UINT32] = NULL; /* pack_ubyte_RGBA_UINT32 */
1780
1781      table[MESA_FORMAT_DUDV8] = pack_ubyte_DUDV8;
1782
1783      table[MESA_FORMAT_RGBA_16] = pack_ubyte_RGBA_16;
1784
1785      /* n/a */
1786      table[MESA_FORMAT_SIGNED_R8] = NULL;
1787      table[MESA_FORMAT_SIGNED_RG88_REV] = NULL;
1788      table[MESA_FORMAT_SIGNED_RGBX8888] = NULL;
1789      table[MESA_FORMAT_SIGNED_RGBA8888] = NULL;
1790      table[MESA_FORMAT_SIGNED_RGBA8888_REV] = NULL;
1791      table[MESA_FORMAT_SIGNED_R16] = NULL;
1792      table[MESA_FORMAT_SIGNED_GR1616] = NULL;
1793      table[MESA_FORMAT_SIGNED_RGB_16] = NULL;
1794      table[MESA_FORMAT_SIGNED_RGBA_16] = NULL;
1795      table[MESA_FORMAT_SIGNED_A8] = NULL;
1796      table[MESA_FORMAT_SIGNED_L8] = NULL;
1797      table[MESA_FORMAT_SIGNED_AL88] = NULL;
1798      table[MESA_FORMAT_SIGNED_I8] = NULL;
1799      table[MESA_FORMAT_SIGNED_A16] = NULL;
1800      table[MESA_FORMAT_SIGNED_L16] = NULL;
1801      table[MESA_FORMAT_SIGNED_AL1616] = NULL;
1802      table[MESA_FORMAT_SIGNED_I16] = NULL;
1803
1804
1805      table[MESA_FORMAT_RGBA_16] = pack_ubyte_RGBA_16;
1806
1807      table[MESA_FORMAT_RGB9_E5_FLOAT] = pack_ubyte_RGB9_E5_FLOAT;
1808      table[MESA_FORMAT_R11_G11_B10_FLOAT] = pack_ubyte_R11_G11_B10_FLOAT;
1809
1810      initialized = GL_TRUE;
1811   }
1812
1813   return table[format];
1814}
1815
1816
1817
1818/**
1819 * Return a function that can pack a GLfloat rgba[4] color.
1820 */
1821gl_pack_float_rgba_func
1822_mesa_get_pack_float_rgba_function(gl_format format)
1823{
1824   static gl_pack_float_rgba_func table[MESA_FORMAT_COUNT];
1825   static GLboolean initialized = GL_FALSE;
1826
1827   if (!initialized) {
1828      memset(table, 0, sizeof(table));
1829
1830      table[MESA_FORMAT_NONE] = NULL;
1831
1832      table[MESA_FORMAT_RGBA8888] = pack_float_RGBA8888;
1833      table[MESA_FORMAT_RGBA8888_REV] = pack_float_RGBA8888_REV;
1834      table[MESA_FORMAT_ARGB8888] = pack_float_ARGB8888;
1835      table[MESA_FORMAT_ARGB8888_REV] = pack_float_ARGB8888_REV;
1836      table[MESA_FORMAT_RGBX8888] = pack_float_RGBA8888; /* reused */
1837      table[MESA_FORMAT_RGBX8888_REV] = pack_float_RGBA8888_REV; /* reused */
1838      table[MESA_FORMAT_XRGB8888] = pack_float_XRGB8888;
1839      table[MESA_FORMAT_XRGB8888_REV] = pack_float_XRGB8888_REV;
1840      table[MESA_FORMAT_RGB888] = pack_float_RGB888;
1841      table[MESA_FORMAT_BGR888] = pack_float_BGR888;
1842      table[MESA_FORMAT_RGB565] = pack_float_RGB565;
1843      table[MESA_FORMAT_RGB565_REV] = pack_float_RGB565_REV;
1844      table[MESA_FORMAT_ARGB4444] = pack_float_ARGB4444;
1845      table[MESA_FORMAT_ARGB4444_REV] = pack_float_ARGB4444_REV;
1846      table[MESA_FORMAT_RGBA5551] = pack_float_RGBA5551;
1847      table[MESA_FORMAT_ARGB1555] = pack_float_ARGB1555;
1848      table[MESA_FORMAT_ARGB1555_REV] = pack_float_ARGB1555_REV;
1849
1850      table[MESA_FORMAT_AL44] = pack_float_AL44;
1851      table[MESA_FORMAT_AL88] = pack_float_AL88;
1852      table[MESA_FORMAT_AL88_REV] = pack_float_AL88_REV;
1853      table[MESA_FORMAT_AL1616] = pack_float_AL1616;
1854      table[MESA_FORMAT_AL1616_REV] = pack_float_AL1616_REV;
1855      table[MESA_FORMAT_RGB332] = pack_float_RGB332;
1856      table[MESA_FORMAT_A8] = pack_float_A8;
1857      table[MESA_FORMAT_A16] = pack_float_A16;
1858      table[MESA_FORMAT_L8] = pack_float_L8;
1859      table[MESA_FORMAT_L16] = pack_float_L16;
1860      table[MESA_FORMAT_I8] = pack_float_L8; /* reuse pack_float_L8 */
1861      table[MESA_FORMAT_I16] = pack_float_L16; /* reuse pack_float_L16 */
1862      table[MESA_FORMAT_YCBCR] = pack_float_YCBCR;
1863      table[MESA_FORMAT_YCBCR_REV] = pack_float_YCBCR_REV;
1864      table[MESA_FORMAT_R8] = pack_float_R8;
1865      table[MESA_FORMAT_GR88] = pack_float_GR88;
1866      table[MESA_FORMAT_RG88] = pack_float_RG88;
1867      table[MESA_FORMAT_R16] = pack_float_R16;
1868      table[MESA_FORMAT_RG1616] = pack_float_RG1616;
1869      table[MESA_FORMAT_RG1616_REV] = pack_float_RG1616_REV;
1870      table[MESA_FORMAT_ARGB2101010] = pack_float_ARGB2101010;
1871      table[MESA_FORMAT_ABGR2101010_UINT] = pack_float_ABGR2101010_UINT;
1872
1873      /* should never convert RGBA to these formats */
1874      table[MESA_FORMAT_Z24_S8] = NULL;
1875      table[MESA_FORMAT_S8_Z24] = NULL;
1876      table[MESA_FORMAT_Z16] = NULL;
1877      table[MESA_FORMAT_X8_Z24] = NULL;
1878      table[MESA_FORMAT_Z24_X8] = NULL;
1879      table[MESA_FORMAT_Z32] = NULL;
1880      table[MESA_FORMAT_S8] = NULL;
1881
1882      table[MESA_FORMAT_SRGB8] = pack_float_SRGB8;
1883      table[MESA_FORMAT_SRGBA8] = pack_float_SRGBA8;
1884      table[MESA_FORMAT_SARGB8] = pack_float_SARGB8;
1885      table[MESA_FORMAT_SL8] = pack_float_SL8;
1886      table[MESA_FORMAT_SLA8] = pack_float_SLA8;
1887
1888      /* n/a */
1889      table[MESA_FORMAT_SRGB_DXT1] = NULL;
1890      table[MESA_FORMAT_SRGBA_DXT1] = NULL;
1891      table[MESA_FORMAT_SRGBA_DXT3] = NULL;
1892      table[MESA_FORMAT_SRGBA_DXT5] = NULL;
1893
1894      table[MESA_FORMAT_RGB_FXT1] = NULL;
1895      table[MESA_FORMAT_RGBA_FXT1] = NULL;
1896      table[MESA_FORMAT_RGB_DXT1] = NULL;
1897      table[MESA_FORMAT_RGBA_DXT1] = NULL;
1898      table[MESA_FORMAT_RGBA_DXT3] = NULL;
1899      table[MESA_FORMAT_RGBA_DXT5] = NULL;
1900
1901      table[MESA_FORMAT_RGBA_FLOAT32] = pack_float_RGBA_FLOAT32;
1902      table[MESA_FORMAT_RGBA_FLOAT16] = pack_float_RGBA_FLOAT16;
1903      table[MESA_FORMAT_RGB_FLOAT32] = pack_float_RGB_FLOAT32;
1904      table[MESA_FORMAT_RGB_FLOAT16] = pack_float_RGB_FLOAT16;
1905      table[MESA_FORMAT_ALPHA_FLOAT32] = pack_float_ALPHA_FLOAT32;
1906      table[MESA_FORMAT_ALPHA_FLOAT16] = pack_float_ALPHA_FLOAT16;
1907      table[MESA_FORMAT_LUMINANCE_FLOAT32] = pack_float_LUMINANCE_FLOAT32;
1908      table[MESA_FORMAT_LUMINANCE_FLOAT16] = pack_float_LUMINANCE_FLOAT16;
1909      table[MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32] = pack_float_LUMINANCE_ALPHA_FLOAT32;
1910      table[MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16] = pack_float_LUMINANCE_ALPHA_FLOAT16;
1911
1912      table[MESA_FORMAT_INTENSITY_FLOAT32] = pack_float_LUMINANCE_FLOAT32;
1913      table[MESA_FORMAT_INTENSITY_FLOAT16] = pack_float_LUMINANCE_FLOAT16;
1914      table[MESA_FORMAT_R_FLOAT32] = pack_float_LUMINANCE_FLOAT32;
1915      table[MESA_FORMAT_R_FLOAT16] = pack_float_LUMINANCE_FLOAT16;
1916      table[MESA_FORMAT_RG_FLOAT32] = pack_float_RG_FLOAT32;
1917      table[MESA_FORMAT_RG_FLOAT16] = pack_float_RG_FLOAT16;
1918
1919      /* n/a */
1920      table[MESA_FORMAT_RGBA_INT8] = NULL;
1921      table[MESA_FORMAT_RGBA_INT16] = NULL;
1922      table[MESA_FORMAT_RGBA_INT32] = NULL;
1923      table[MESA_FORMAT_RGBA_UINT8] = NULL;
1924      table[MESA_FORMAT_RGBA_UINT16] = NULL;
1925      table[MESA_FORMAT_RGBA_UINT32] = NULL;
1926
1927      table[MESA_FORMAT_DUDV8] = pack_float_DUDV8;
1928
1929      table[MESA_FORMAT_RGBA_16] = pack_float_RGBA_16;
1930
1931      table[MESA_FORMAT_SIGNED_R8] = pack_float_SIGNED_R8;
1932      table[MESA_FORMAT_SIGNED_RG88_REV] = pack_float_SIGNED_RG88_REV;
1933      table[MESA_FORMAT_SIGNED_RGBX8888] = pack_float_SIGNED_RGBX8888;
1934      table[MESA_FORMAT_SIGNED_RGBA8888] = pack_float_SIGNED_RGBA8888;
1935      table[MESA_FORMAT_SIGNED_RGBA8888_REV] = pack_float_SIGNED_RGBA8888_REV;
1936      table[MESA_FORMAT_SIGNED_R16] = pack_float_SIGNED_R16;
1937      table[MESA_FORMAT_SIGNED_GR1616] = pack_float_SIGNED_GR1616;
1938      table[MESA_FORMAT_SIGNED_RGB_16] = pack_float_SIGNED_RGB_16;
1939      table[MESA_FORMAT_SIGNED_RGBA_16] = pack_float_SIGNED_RGBA_16;
1940      table[MESA_FORMAT_SIGNED_A8] = pack_float_SIGNED_A8;
1941      table[MESA_FORMAT_SIGNED_L8] = pack_float_SIGNED_L8;
1942      table[MESA_FORMAT_SIGNED_AL88] = pack_float_SIGNED_AL88;
1943      table[MESA_FORMAT_SIGNED_I8] = pack_float_SIGNED_L8; /* reused */
1944      table[MESA_FORMAT_SIGNED_A16] = pack_float_SIGNED_A16;
1945      table[MESA_FORMAT_SIGNED_L16] = pack_float_SIGNED_L16;
1946      table[MESA_FORMAT_SIGNED_AL1616] = pack_float_SIGNED_AL1616;
1947      table[MESA_FORMAT_SIGNED_I16] = pack_float_SIGNED_L16; /* reused */
1948
1949      table[MESA_FORMAT_RGB9_E5_FLOAT] = pack_float_RGB9_E5_FLOAT;
1950      table[MESA_FORMAT_R11_G11_B10_FLOAT] = pack_float_R11_G11_B10_FLOAT;
1951
1952      initialized = GL_TRUE;
1953   }
1954
1955   return table[format];
1956}
1957
1958
1959
1960static pack_float_rgba_row_func
1961get_pack_float_rgba_row_function(gl_format format)
1962{
1963   static pack_float_rgba_row_func table[MESA_FORMAT_COUNT];
1964   static GLboolean initialized = GL_FALSE;
1965
1966   if (!initialized) {
1967      /* We don't need a special row packing function for each format.
1968       * There's a generic fallback which uses a per-pixel packing function.
1969       */
1970      memset(table, 0, sizeof(table));
1971
1972      table[MESA_FORMAT_RGBA8888] = pack_row_float_RGBA8888;
1973      table[MESA_FORMAT_RGBA8888_REV] = pack_row_float_RGBA8888_REV;
1974      table[MESA_FORMAT_ARGB8888] = pack_row_float_ARGB8888;
1975      table[MESA_FORMAT_ARGB8888_REV] = pack_row_float_ARGB8888_REV;
1976      table[MESA_FORMAT_RGBX8888] = pack_row_float_RGBA8888; /* reused */
1977      table[MESA_FORMAT_RGBX8888_REV] = pack_row_float_RGBA8888_REV; /* reused */
1978      table[MESA_FORMAT_XRGB8888] = pack_row_float_XRGB8888;
1979      table[MESA_FORMAT_XRGB8888_REV] = pack_row_float_XRGB8888_REV;
1980      table[MESA_FORMAT_RGB888] = pack_row_float_RGB888;
1981      table[MESA_FORMAT_BGR888] = pack_row_float_BGR888;
1982      table[MESA_FORMAT_RGB565] = pack_row_float_RGB565;
1983      table[MESA_FORMAT_RGB565_REV] = pack_row_float_RGB565_REV;
1984
1985      initialized = GL_TRUE;
1986   }
1987
1988   return table[format];
1989}
1990
1991
1992
1993static pack_ubyte_rgba_row_func
1994get_pack_ubyte_rgba_row_function(gl_format format)
1995{
1996   static pack_ubyte_rgba_row_func table[MESA_FORMAT_COUNT];
1997   static GLboolean initialized = GL_FALSE;
1998
1999   if (!initialized) {
2000      /* We don't need a special row packing function for each format.
2001       * There's a generic fallback which uses a per-pixel packing function.
2002       */
2003      memset(table, 0, sizeof(table));
2004
2005      table[MESA_FORMAT_RGBA8888] = pack_row_ubyte_RGBA8888;
2006      table[MESA_FORMAT_RGBA8888_REV] = pack_row_ubyte_RGBA8888_REV;
2007      table[MESA_FORMAT_ARGB8888] = pack_row_ubyte_ARGB8888;
2008      table[MESA_FORMAT_ARGB8888_REV] = pack_row_ubyte_ARGB8888_REV;
2009      table[MESA_FORMAT_RGBX8888] = pack_row_ubyte_RGBA8888; /* reused */
2010      table[MESA_FORMAT_RGBX8888_REV] = pack_row_ubyte_RGBA8888_REV; /* reused */
2011      table[MESA_FORMAT_XRGB8888] = pack_row_ubyte_XRGB8888;
2012      table[MESA_FORMAT_XRGB8888_REV] = pack_row_ubyte_XRGB8888_REV;
2013      table[MESA_FORMAT_RGB888] = pack_row_ubyte_RGB888;
2014      table[MESA_FORMAT_BGR888] = pack_row_ubyte_BGR888;
2015      table[MESA_FORMAT_RGB565] = pack_row_ubyte_RGB565;
2016      table[MESA_FORMAT_RGB565_REV] = pack_row_ubyte_RGB565_REV;
2017
2018      initialized = GL_TRUE;
2019   }
2020
2021   return table[format];
2022}
2023
2024
2025
2026/**
2027 * Pack a row of GLfloat rgba[4] values to the destination.
2028 */
2029void
2030_mesa_pack_float_rgba_row(gl_format format, GLuint n,
2031                          const GLfloat src[][4], void *dst)
2032{
2033   pack_float_rgba_row_func packrow = get_pack_float_rgba_row_function(format);
2034   if (packrow) {
2035      /* use "fast" function */
2036      packrow(n, src, dst);
2037   }
2038   else {
2039      /* slower fallback */
2040      gl_pack_float_rgba_func pack = _mesa_get_pack_float_rgba_function(format);
2041      GLuint dstStride = _mesa_get_format_bytes(format);
2042      GLubyte *dstPtr = (GLubyte *) dst;
2043      GLuint i;
2044
2045      assert(pack);
2046      if (!pack)
2047         return;
2048
2049      for (i = 0; i < n; i++) {
2050         pack(src[i], dstPtr);
2051         dstPtr += dstStride;
2052      }
2053   }
2054}
2055
2056
2057/**
2058 * Pack a row of GLubyte rgba[4] values to the destination.
2059 */
2060void
2061_mesa_pack_ubyte_rgba_row(gl_format format, GLuint n,
2062                          const GLubyte src[][4], void *dst)
2063{
2064   pack_ubyte_rgba_row_func packrow = get_pack_ubyte_rgba_row_function(format);
2065   if (packrow) {
2066      /* use "fast" function */
2067      packrow(n, src, dst);
2068   }
2069   else {
2070      /* slower fallback */
2071      gl_pack_ubyte_rgba_func pack = _mesa_get_pack_ubyte_rgba_function(format);
2072      const GLuint stride = _mesa_get_format_bytes(format);
2073      GLubyte *d = ((GLubyte *) dst);
2074      GLuint i;
2075
2076      assert(pack);
2077      if (!pack)
2078         return;
2079
2080      for (i = 0; i < n; i++) {
2081         pack(src[i], d);
2082         d += stride;
2083      }
2084   }
2085}
2086
2087
2088/**
2089 * Pack a 2D image of ubyte RGBA pixels in the given format.
2090 * \param srcRowStride  source image row stride in bytes
2091 * \param dstRowStride  destination image row stride in bytes
2092 */
2093void
2094_mesa_pack_ubyte_rgba_rect(gl_format format, GLuint width, GLuint height,
2095                           const GLubyte *src, GLint srcRowStride,
2096                           void *dst, GLint dstRowStride)
2097{
2098   pack_ubyte_rgba_row_func packrow = get_pack_ubyte_rgba_row_function(format);
2099   GLubyte *dstUB = (GLubyte *) dst;
2100   GLuint i;
2101
2102   if (packrow) {
2103      if (srcRowStride == width * 4 * sizeof(GLubyte) &&
2104          dstRowStride == _mesa_format_row_stride(format, width)) {
2105         /* do whole image at once */
2106         packrow(width * height, (const GLubyte (*)[4]) src, dst);
2107      }
2108      else {
2109         /* row by row */
2110         for (i = 0; i < height; i++) {
2111            packrow(width, (const GLubyte (*)[4]) src, dstUB);
2112            src += srcRowStride;
2113            dstUB += dstRowStride;
2114         }
2115      }
2116   }
2117   else {
2118      /* slower fallback */
2119      for (i = 0; i < height; i++) {
2120         _mesa_pack_ubyte_rgba_row(format, width,
2121                                   (const GLubyte (*)[4]) src, dstUB);
2122         src += srcRowStride;
2123         dstUB += dstRowStride;
2124      }
2125   }
2126}
2127
2128
2129
2130/**
2131 ** Pack float Z pixels
2132 **/
2133
2134static void
2135pack_float_z_Z24_S8(const GLfloat *src, void *dst)
2136{
2137   /* don't disturb the stencil values */
2138   GLuint *d = ((GLuint *) dst);
2139   const GLdouble scale = (GLdouble) 0xffffff;
2140   GLuint s = *d & 0xff;
2141   GLuint z = (GLuint) (*src * scale);
2142   assert(z <= 0xffffff);
2143   *d = (z << 8) | s;
2144}
2145
2146static void
2147pack_float_z_S8_Z24(const GLfloat *src, void *dst)
2148{
2149   /* don't disturb the stencil values */
2150   GLuint *d = ((GLuint *) dst);
2151   const GLdouble scale = (GLdouble) 0xffffff;
2152   GLuint s = *d & 0xff000000;
2153   GLuint z = (GLuint) (*src * scale);
2154   assert(z <= 0xffffff);
2155   *d = s | z;
2156}
2157
2158static void
2159pack_float_z_Z16(const GLfloat *src, void *dst)
2160{
2161   GLushort *d = ((GLushort *) dst);
2162   const GLfloat scale = (GLfloat) 0xffff;
2163   *d = (GLushort) (*src * scale);
2164}
2165
2166static void
2167pack_float_z_Z32(const GLfloat *src, void *dst)
2168{
2169   GLuint *d = ((GLuint *) dst);
2170   const GLdouble scale = (GLdouble) 0xffffffff;
2171   *d = (GLuint) (*src * scale);
2172}
2173
2174static void
2175pack_float_z_Z32_FLOAT(const GLfloat *src, void *dst)
2176{
2177   GLfloat *d = (GLfloat *) dst;
2178   *d = *src;
2179}
2180
2181gl_pack_float_z_func
2182_mesa_get_pack_float_z_func(gl_format format)
2183{
2184   switch (format) {
2185   case MESA_FORMAT_Z24_S8:
2186   case MESA_FORMAT_Z24_X8:
2187      return pack_float_z_Z24_S8;
2188   case MESA_FORMAT_S8_Z24:
2189   case MESA_FORMAT_X8_Z24:
2190      return pack_float_z_S8_Z24;
2191   case MESA_FORMAT_Z16:
2192      return pack_float_z_Z16;
2193   case MESA_FORMAT_Z32:
2194      return pack_float_z_Z32;
2195   case MESA_FORMAT_Z32_FLOAT:
2196   case MESA_FORMAT_Z32_FLOAT_X24S8:
2197      return pack_float_z_Z32_FLOAT;
2198   default:
2199      _mesa_problem(NULL,
2200                    "unexpected format in _mesa_get_pack_float_z_func()");
2201      return NULL;
2202   }
2203}
2204
2205
2206
2207/**
2208 ** Pack uint Z pixels.  The incoming src value is always in
2209 ** the range [0, 2^32-1].
2210 **/
2211
2212static void
2213pack_uint_z_Z24_S8(const GLuint *src, void *dst)
2214{
2215   /* don't disturb the stencil values */
2216   GLuint *d = ((GLuint *) dst);
2217   GLuint s = *d & 0xff;
2218   GLuint z = *src & 0xffffff00;
2219   *d = z | s;
2220}
2221
2222static void
2223pack_uint_z_S8_Z24(const GLuint *src, void *dst)
2224{
2225   /* don't disturb the stencil values */
2226   GLuint *d = ((GLuint *) dst);
2227   GLuint s = *d & 0xff000000;
2228   GLuint z = *src >> 8;
2229   *d = s | z;
2230}
2231
2232static void
2233pack_uint_z_Z16(const GLuint *src, void *dst)
2234{
2235   GLushort *d = ((GLushort *) dst);
2236   *d = *src >> 16;
2237}
2238
2239static void
2240pack_uint_z_Z32(const GLuint *src, void *dst)
2241{
2242   GLuint *d = ((GLuint *) dst);
2243   *d = *src;
2244}
2245
2246static void
2247pack_uint_z_Z32_FLOAT(const GLuint *src, void *dst)
2248{
2249   GLuint *d = ((GLuint *) dst);
2250   const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
2251   *d = *src * scale;
2252   assert(*d >= 0.0f);
2253   assert(*d <= 1.0f);
2254}
2255
2256static void
2257pack_uint_z_Z32_FLOAT_X24S8(const GLuint *src, void *dst)
2258{
2259   GLfloat *d = ((GLfloat *) dst);
2260   const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
2261   *d = *src * scale;
2262   assert(*d >= 0.0f);
2263   assert(*d <= 1.0f);
2264}
2265
2266gl_pack_uint_z_func
2267_mesa_get_pack_uint_z_func(gl_format format)
2268{
2269   switch (format) {
2270   case MESA_FORMAT_Z24_S8:
2271   case MESA_FORMAT_Z24_X8:
2272      return pack_uint_z_Z24_S8;
2273   case MESA_FORMAT_S8_Z24:
2274   case MESA_FORMAT_X8_Z24:
2275      return pack_uint_z_S8_Z24;
2276   case MESA_FORMAT_Z16:
2277      return pack_uint_z_Z16;
2278   case MESA_FORMAT_Z32:
2279      return pack_uint_z_Z32;
2280   case MESA_FORMAT_Z32_FLOAT:
2281      return pack_uint_z_Z32_FLOAT;
2282   case MESA_FORMAT_Z32_FLOAT_X24S8:
2283      return pack_uint_z_Z32_FLOAT_X24S8;
2284   default:
2285      _mesa_problem(NULL, "unexpected format in _mesa_get_pack_uint_z_func()");
2286      return NULL;
2287   }
2288}
2289
2290
2291/**
2292 ** Pack ubyte stencil pixels
2293 **/
2294
2295static void
2296pack_ubyte_stencil_Z24_S8(const GLubyte *src, void *dst)
2297{
2298   /* don't disturb the Z values */
2299   GLuint *d = ((GLuint *) dst);
2300   GLuint s = *src;
2301   GLuint z = *d & 0xffffff00;
2302   *d = z | s;
2303}
2304
2305static void
2306pack_ubyte_stencil_S8_Z24(const GLubyte *src, void *dst)
2307{
2308   /* don't disturb the Z values */
2309   GLuint *d = ((GLuint *) dst);
2310   GLuint s = *src << 24;
2311   GLuint z = *d & 0xffffff;
2312   *d = s | z;
2313}
2314
2315static void
2316pack_ubyte_stencil_S8(const GLubyte *src, void *dst)
2317{
2318   GLubyte *d = (GLubyte *) dst;
2319   *d = *src;
2320}
2321
2322static void
2323pack_ubyte_stencil_Z32_FLOAT_X24S8(const GLubyte *src, void *dst)
2324{
2325   GLfloat *d = ((GLfloat *) dst);
2326   d[1] = *src;
2327}
2328
2329
2330gl_pack_ubyte_stencil_func
2331_mesa_get_pack_ubyte_stencil_func(gl_format format)
2332{
2333   switch (format) {
2334   case MESA_FORMAT_Z24_S8:
2335      return pack_ubyte_stencil_Z24_S8;
2336   case MESA_FORMAT_S8_Z24:
2337      return pack_ubyte_stencil_S8_Z24;
2338   case MESA_FORMAT_S8:
2339      return pack_ubyte_stencil_S8;
2340   case MESA_FORMAT_Z32_FLOAT_X24S8:
2341      return pack_ubyte_stencil_Z32_FLOAT_X24S8;
2342   default:
2343      _mesa_problem(NULL,
2344                    "unexpected format in _mesa_pack_ubyte_stencil_func()");
2345      return NULL;
2346   }
2347}
2348
2349
2350
2351void
2352_mesa_pack_float_z_row(gl_format format, GLuint n,
2353                       const GLfloat *src, void *dst)
2354{
2355   switch (format) {
2356   case MESA_FORMAT_Z24_S8:
2357   case MESA_FORMAT_Z24_X8:
2358      {
2359         /* don't disturb the stencil values */
2360         GLuint *d = ((GLuint *) dst);
2361         const GLdouble scale = (GLdouble) 0xffffff;
2362         GLuint i;
2363         for (i = 0; i < n; i++) {
2364            GLuint s = d[i] & 0xff;
2365            GLuint z = (GLuint) (src[i] * scale);
2366            assert(z <= 0xffffff);
2367            d[i] = (z << 8) | s;
2368         }
2369      }
2370      break;
2371   case MESA_FORMAT_S8_Z24:
2372   case MESA_FORMAT_X8_Z24:
2373      {
2374         /* don't disturb the stencil values */
2375         GLuint *d = ((GLuint *) dst);
2376         const GLdouble scale = (GLdouble) 0xffffff;
2377         GLuint i;
2378         for (i = 0; i < n; i++) {
2379            GLuint s = d[i] & 0xff000000;
2380            GLuint z = (GLuint) (src[i] * scale);
2381            assert(z <= 0xffffff);
2382            d[i] = s | z;
2383         }
2384      }
2385      break;
2386   case MESA_FORMAT_Z16:
2387      {
2388         GLushort *d = ((GLushort *) dst);
2389         const GLfloat scale = (GLfloat) 0xffff;
2390         GLuint i;
2391         for (i = 0; i < n; i++) {
2392            d[i] = (GLushort) (src[i] * scale);
2393         }
2394      }
2395      break;
2396   case MESA_FORMAT_Z32:
2397      {
2398         GLuint *d = ((GLuint *) dst);
2399         const GLdouble scale = (GLdouble) 0xffffffff;
2400         GLuint i;
2401         for (i = 0; i < n; i++) {
2402            d[i] = (GLuint) (src[i] * scale);
2403         }
2404      }
2405      break;
2406   case MESA_FORMAT_Z32_FLOAT:
2407      memcpy(dst, src, n * sizeof(GLfloat));
2408      break;
2409   case MESA_FORMAT_Z32_FLOAT_X24S8:
2410      {
2411         struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
2412         GLuint i;
2413         for (i = 0; i < n; i++) {
2414            d[i].z = src[i];
2415         }
2416      }
2417      break;
2418   default:
2419      _mesa_problem(NULL, "unexpected format in _mesa_pack_float_z_row()");
2420   }
2421}
2422
2423
2424/**
2425 * The incoming Z values are always in the range [0, 0xffffffff].
2426 */
2427void
2428_mesa_pack_uint_z_row(gl_format format, GLuint n,
2429                      const GLuint *src, void *dst)
2430{
2431   switch (format) {
2432   case MESA_FORMAT_Z24_S8:
2433   case MESA_FORMAT_Z24_X8:
2434      {
2435         /* don't disturb the stencil values */
2436         GLuint *d = ((GLuint *) dst);
2437         GLuint i;
2438         for (i = 0; i < n; i++) {
2439            GLuint s = d[i] & 0xff;
2440            GLuint z = src[i] & 0xffffff00;
2441            d[i] = z | s;
2442         }
2443      }
2444      break;
2445   case MESA_FORMAT_S8_Z24:
2446   case MESA_FORMAT_X8_Z24:
2447      {
2448         /* don't disturb the stencil values */
2449         GLuint *d = ((GLuint *) dst);
2450         GLuint i;
2451         for (i = 0; i < n; i++) {
2452            GLuint s = d[i] & 0xff000000;
2453            GLuint z = src[i] >> 8;
2454            d[i] = s | z;
2455         }
2456      }
2457      break;
2458   case MESA_FORMAT_Z16:
2459      {
2460         GLushort *d = ((GLushort *) dst);
2461         GLuint i;
2462         for (i = 0; i < n; i++) {
2463            d[i] = src[i] >> 16;
2464         }
2465      }
2466      break;
2467   case MESA_FORMAT_Z32:
2468      memcpy(dst, src, n * sizeof(GLfloat));
2469      break;
2470   case MESA_FORMAT_Z32_FLOAT:
2471      {
2472         GLuint *d = ((GLuint *) dst);
2473         const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
2474         GLuint i;
2475         for (i = 0; i < n; i++) {
2476            d[i] = src[i] * scale;
2477            assert(d[i] >= 0.0f);
2478            assert(d[i] <= 1.0f);
2479         }
2480      }
2481      break;
2482   case MESA_FORMAT_Z32_FLOAT_X24S8:
2483      {
2484         struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
2485         const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
2486         GLuint i;
2487         for (i = 0; i < n; i++) {
2488            d[i].z = src[i] * scale;
2489            assert(d[i].z >= 0.0f);
2490            assert(d[i].z <= 1.0f);
2491         }
2492      }
2493      break;
2494   default:
2495      _mesa_problem(NULL, "unexpected format in _mesa_pack_uint_z_row()");
2496   }
2497}
2498
2499
2500void
2501_mesa_pack_ubyte_stencil_row(gl_format format, GLuint n,
2502                             const GLubyte *src, void *dst)
2503{
2504   switch (format) {
2505   case MESA_FORMAT_Z24_S8:
2506      {
2507         /* don't disturb the Z values */
2508         GLuint *d = ((GLuint *) dst);
2509         GLuint i;
2510         for (i = 0; i < n; i++) {
2511            GLuint s = src[i];
2512            GLuint z = d[i] & 0xffffff00;
2513            d[i] = z | s;
2514         }
2515      }
2516      break;
2517   case MESA_FORMAT_S8_Z24:
2518      {
2519         /* don't disturb the Z values */
2520         GLuint *d = ((GLuint *) dst);
2521         GLuint i;
2522         for (i = 0; i < n; i++) {
2523            GLuint s = src[i] << 24;
2524            GLuint z = d[i] & 0xffffff;
2525            d[i] = s | z;
2526         }
2527      }
2528      break;
2529   case MESA_FORMAT_S8:
2530      memcpy(dst, src, n * sizeof(GLubyte));
2531      break;
2532   case MESA_FORMAT_Z32_FLOAT_X24S8:
2533      {
2534         struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
2535         GLuint i;
2536         for (i = 0; i < n; i++) {
2537            d[i].x24s8 = src[i];
2538         }
2539      }
2540      break;
2541   default:
2542      _mesa_problem(NULL, "unexpected format in _mesa_pack_ubyte_stencil_row()");
2543   }
2544}
2545
2546
2547/**
2548 * Incoming Z/stencil values are always in uint_24_8 format.
2549 */
2550void
2551_mesa_pack_uint_24_8_depth_stencil_row(gl_format format, GLuint n,
2552                                       const GLuint *src, void *dst)
2553{
2554   switch (format) {
2555   case MESA_FORMAT_Z24_S8:
2556      memcpy(dst, src, n * sizeof(GLuint));
2557      break;
2558   case MESA_FORMAT_S8_Z24:
2559      {
2560         GLuint *d = ((GLuint *) dst);
2561         GLuint i;
2562         for (i = 0; i < n; i++) {
2563            GLuint s = src[i] << 24;
2564            GLuint z = src[i] >> 8;
2565            d[i] = s | z;
2566         }
2567      }
2568      break;
2569   case MESA_FORMAT_Z32_FLOAT_X24S8:
2570      {
2571         const GLdouble scale = 1.0 / (GLdouble) 0xffffff;
2572         struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
2573         GLint i;
2574         for (i = 0; i < n; i++) {
2575            GLfloat z = (src[i] >> 8) * scale;
2576            d[i].z = z;
2577            d[i].x24s8 = src[i];
2578         }
2579      }
2580      break;
2581   default:
2582      _mesa_problem(NULL, "bad format %s in _mesa_pack_ubyte_s_row",
2583                    _mesa_get_format_name(format));
2584      return;
2585   }
2586}
2587
2588
2589
2590/**
2591 * Convert a boolean color mask to a packed color where each channel of
2592 * the packed value at dst will be 0 or ~0 depending on the colorMask.
2593 */
2594void
2595_mesa_pack_colormask(gl_format format, const GLubyte colorMask[4], void *dst)
2596{
2597   GLfloat maskColor[4];
2598
2599   switch (_mesa_get_format_datatype(format)) {
2600   case GL_UNSIGNED_NORMALIZED:
2601      /* simple: 1.0 will convert to ~0 in the right bit positions */
2602      maskColor[0] = colorMask[0] ? 1.0 : 0.0;
2603      maskColor[1] = colorMask[1] ? 1.0 : 0.0;
2604      maskColor[2] = colorMask[2] ? 1.0 : 0.0;
2605      maskColor[3] = colorMask[3] ? 1.0 : 0.0;
2606      _mesa_pack_float_rgba_row(format, 1,
2607                                (const GLfloat (*)[4]) maskColor, dst);
2608      break;
2609   case GL_SIGNED_NORMALIZED:
2610   case GL_FLOAT:
2611      /* These formats are harder because it's hard to know the floating
2612       * point values that will convert to ~0 for each color channel's bits.
2613       * This solution just generates a non-zero value for each color channel
2614       * then fixes up the non-zero values to be ~0.
2615       * Note: we'll need to add special case code if we ever have to deal
2616       * with formats with unequal color channel sizes, like R11_G11_B10.
2617       * We issue a warning below for channel sizes other than 8,16,32.
2618       */
2619      {
2620         GLuint bits = _mesa_get_format_max_bits(format); /* bits per chan */
2621         GLuint bytes = _mesa_get_format_bytes(format);
2622         GLuint i;
2623
2624         /* this should put non-zero values into the channels of dst */
2625         maskColor[0] = colorMask[0] ? -1.0f : 0.0f;
2626         maskColor[1] = colorMask[1] ? -1.0f : 0.0f;
2627         maskColor[2] = colorMask[2] ? -1.0f : 0.0f;
2628         maskColor[3] = colorMask[3] ? -1.0f : 0.0f;
2629         _mesa_pack_float_rgba_row(format, 1,
2630                                   (const GLfloat (*)[4]) maskColor, dst);
2631
2632         /* fix-up the dst channels by converting non-zero values to ~0 */
2633         if (bits == 8) {
2634            GLubyte *d = (GLubyte *) dst;
2635            for (i = 0; i < bytes; i++) {
2636               d[i] = d[i] ? 0xff : 0x0;
2637            }
2638         }
2639         else if (bits == 16) {
2640            GLushort *d = (GLushort *) dst;
2641            for (i = 0; i < bytes / 2; i++) {
2642               d[i] = d[i] ? 0xffff : 0x0;
2643            }
2644         }
2645         else if (bits == 32) {
2646            GLuint *d = (GLuint *) dst;
2647            for (i = 0; i < bytes / 4; i++) {
2648               d[i] = d[i] ? 0xffffffffU : 0x0;
2649            }
2650         }
2651         else {
2652            _mesa_problem(NULL, "unexpected size in _mesa_pack_colormask()");
2653            return;
2654         }
2655      }
2656      break;
2657   default:
2658      _mesa_problem(NULL, "unexpected format data type in gen_color_mask()");
2659      return;
2660   }
2661}
2662