• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2from mako.template import Template
3from sys import argv
4
5string = """/*
6 * Mesa 3-D graphics library
7 *
8 * Copyright (c) 2011 VMware, Inc.
9 * Copyright (c) 2014 Intel Corporation.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31/**
32 * Color, depth, stencil packing functions.
33 * Used to pack basic color, depth and stencil formats to specific
34 * hardware formats.
35 *
36 * There are both per-pixel and per-row packing functions:
37 * - The former will be used by swrast to write values to the color, depth,
38 *   stencil buffers when drawing points, lines and masked spans.
39 * - The later will be used for image-oriented functions like glDrawPixels,
40 *   glAccum, and glTexImage.
41 */
42
43#include <stdint.h>
44
45#include "format_pack.h"
46#include "format_utils.h"
47#include "macros.h"
48#include "util/format_rgb9e5.h"
49#include "util/format_r11g11b10f.h"
50#include "util/format_srgb.h"
51
52#define UNPACK(SRC, OFFSET, BITS) (((SRC) >> (OFFSET)) & MAX_UINT(BITS))
53#define PACK(SRC, OFFSET, BITS) (((SRC) & MAX_UINT(BITS)) << (OFFSET))
54
55<%
56import format_parser as parser
57
58formats = parser.parse(argv[1])
59
60rgb_formats = []
61for f in formats:
62   if f.name == 'MESA_FORMAT_NONE':
63      continue
64   if f.colorspace not in ('rgb', 'srgb'):
65      continue
66
67   rgb_formats.append(f)
68%>
69
70/* ubyte packing functions */
71
72%for f in rgb_formats:
73   %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
74      <% continue %>
75   %elif f.is_compressed():
76      <% continue %>
77   %endif
78
79static inline void
80pack_ubyte_${f.short_name()}(const GLubyte src[4], void *dst)
81{
82   %for (i, c) in enumerate(f.channels):
83      <% i = f.swizzle.inverse()[i] %>
84      %if c.type == 'x':
85         <% continue %>
86      %endif
87
88      ${c.datatype()} ${c.name} =
89      %if not f.is_normalized() and f.is_int():
90          %if c.type == parser.SIGNED:
91              _mesa_unsigned_to_signed(src[${i}], ${c.size});
92          %else:
93              _mesa_unsigned_to_unsigned(src[${i}], ${c.size});
94          %endif
95      %elif c.type == parser.UNSIGNED:
96         %if f.colorspace == 'srgb' and c.name in 'rgb':
97            <% assert c.size == 8 %>
98            util_format_linear_to_srgb_8unorm(src[${i}]);
99         %else:
100            _mesa_unorm_to_unorm(src[${i}], 8, ${c.size});
101         %endif
102      %elif c.type == parser.SIGNED:
103         _mesa_unorm_to_snorm(src[${i}], 8, ${c.size});
104      %elif c.type == parser.FLOAT:
105         %if c.size == 32:
106            _mesa_unorm_to_float(src[${i}], 8);
107         %elif c.size == 16:
108            _mesa_unorm_to_half(src[${i}], 8);
109         %else:
110            <% assert False %>
111         %endif
112      %else:
113         <% assert False %>
114      %endif
115   %endfor
116
117   %if f.layout == parser.ARRAY:
118      ${f.datatype()} *d = (${f.datatype()} *)dst;
119      %for (i, c) in enumerate(f.channels):
120         %if c.type == 'x':
121            <% continue %>
122         %endif
123         d[${i}] = ${c.name};
124      %endfor
125   %elif f.layout == parser.PACKED:
126      ${f.datatype()} d = 0;
127      %for (i, c) in enumerate(f.channels):
128         %if c.type == 'x':
129            <% continue %>
130         %endif
131         d |= PACK(${c.name}, ${c.shift}, ${c.size});
132      %endfor
133      (*(${f.datatype()} *)dst) = d;
134   %else:
135      <% assert False %>
136   %endif
137}
138%endfor
139
140static inline void
141pack_ubyte_r9g9b9e5_float(const GLubyte src[4], void *dst)
142{
143   GLuint *d = (GLuint *) dst;
144   GLfloat rgb[3];
145   rgb[0] = _mesa_unorm_to_float(src[RCOMP], 8);
146   rgb[1] = _mesa_unorm_to_float(src[GCOMP], 8);
147   rgb[2] = _mesa_unorm_to_float(src[BCOMP], 8);
148   *d = float3_to_rgb9e5(rgb);
149}
150
151static inline void
152pack_ubyte_r11g11b10_float(const GLubyte src[4], void *dst)
153{
154   GLuint *d = (GLuint *) dst;
155   GLfloat rgb[3];
156   rgb[0] = _mesa_unorm_to_float(src[RCOMP], 8);
157   rgb[1] = _mesa_unorm_to_float(src[GCOMP], 8);
158   rgb[2] = _mesa_unorm_to_float(src[BCOMP], 8);
159   *d = float3_to_r11g11b10f(rgb);
160}
161
162/* uint packing functions */
163
164%for f in rgb_formats:
165   %if not f.is_int():
166      <% continue %>
167   %elif f.is_normalized():
168      <% continue %>
169   %elif f.is_compressed():
170      <% continue %>
171   %endif
172
173static inline void
174pack_uint_${f.short_name()}(const GLuint src[4], void *dst)
175{
176   %for (i, c) in enumerate(f.channels):
177      <% i = f.swizzle.inverse()[i] %>
178      %if c.type == 'x':
179         <% continue %>
180      %endif
181
182      ${c.datatype()} ${c.name} =
183      %if c.type == parser.SIGNED:
184         _mesa_signed_to_signed(src[${i}], ${c.size});
185      %elif c.type == parser.UNSIGNED:
186         _mesa_unsigned_to_unsigned(src[${i}], ${c.size});
187      %else:
188         assert(!"Invalid type: only integer types are allowed");
189      %endif
190   %endfor
191
192   %if f.layout == parser.ARRAY:
193      ${f.datatype()} *d = (${f.datatype()} *)dst;
194      %for (i, c) in enumerate(f.channels):
195         %if c.type == 'x':
196            <% continue %>
197         %endif
198         d[${i}] = ${c.name};
199      %endfor
200   %elif f.layout == parser.PACKED:
201      ${f.datatype()} d = 0;
202      %for (i, c) in enumerate(f.channels):
203         %if c.type == 'x':
204            <% continue %>
205         %endif
206         d |= PACK(${c.name}, ${c.shift}, ${c.size});
207      %endfor
208      (*(${f.datatype()} *)dst) = d;
209   %else:
210      <% assert False %>
211   %endif
212}
213%endfor
214
215/* float packing functions */
216
217%for f in rgb_formats:
218   %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
219      <% continue %>
220   %elif f.is_int() and not f.is_normalized():
221      <% continue %>
222   %elif f.is_compressed():
223      <% continue %>
224   %endif
225
226static inline void
227pack_float_${f.short_name()}(const GLfloat src[4], void *dst)
228{
229   %for (i, c) in enumerate(f.channels):
230      <% i = f.swizzle.inverse()[i] %>
231      %if c.type == 'x':
232         <% continue %>
233      %endif
234
235      ${c.datatype()} ${c.name} =
236      %if c.type == parser.UNSIGNED:
237         %if f.colorspace == 'srgb' and c.name in 'rgb':
238            <% assert c.size == 8 %>
239            util_format_linear_float_to_srgb_8unorm(src[${i}]);
240         %else:
241            _mesa_float_to_unorm(src[${i}], ${c.size});
242         %endif
243      %elif c.type == parser.SIGNED:
244         _mesa_float_to_snorm(src[${i}], ${c.size});
245      %elif c.type == parser.FLOAT:
246         %if c.size == 32:
247            src[${i}];
248         %elif c.size == 16:
249            _mesa_float_to_half(src[${i}]);
250         %else:
251            <% assert False %>
252         %endif
253      %else:
254         <% assert False %>
255      %endif
256   %endfor
257
258   %if f.layout == parser.ARRAY:
259      ${f.datatype()} *d = (${f.datatype()} *)dst;
260      %for (i, c) in enumerate(f.channels):
261         %if c.type == 'x':
262            <% continue %>
263         %endif
264         d[${i}] = ${c.name};
265      %endfor
266   %elif f.layout == parser.PACKED:
267      ${f.datatype()} d = 0;
268      %for (i, c) in enumerate(f.channels):
269         %if c.type == 'x':
270            <% continue %>
271         %endif
272         d |= PACK(${c.name}, ${c.shift}, ${c.size});
273      %endfor
274      (*(${f.datatype()} *)dst) = d;
275   %else:
276      <% assert False %>
277   %endif
278}
279%endfor
280
281static inline void
282pack_float_r9g9b9e5_float(const GLfloat src[4], void *dst)
283{
284   GLuint *d = (GLuint *) dst;
285   *d = float3_to_rgb9e5(src);
286}
287
288static inline void
289pack_float_r11g11b10_float(const GLfloat src[4], void *dst)
290{
291   GLuint *d = (GLuint *) dst;
292   *d = float3_to_r11g11b10f(src);
293}
294
295/**
296 * Return a function that can pack a GLubyte rgba[4] color.
297 */
298gl_pack_ubyte_rgba_func
299_mesa_get_pack_ubyte_rgba_function(mesa_format format)
300{
301   switch (format) {
302%for f in rgb_formats:
303   %if f.is_compressed():
304      <% continue %>
305   %endif
306
307   case ${f.name}:
308      return pack_ubyte_${f.short_name()};
309%endfor
310   default:
311      return NULL;
312   }
313}
314
315/**
316 * Return a function that can pack a GLfloat rgba[4] color.
317 */
318gl_pack_float_rgba_func
319_mesa_get_pack_float_rgba_function(mesa_format format)
320{
321   switch (format) {
322%for f in rgb_formats:
323   %if f.is_compressed():
324      <% continue %>
325   %elif f.is_int() and not f.is_normalized():
326      <% continue %>
327   %endif
328
329   case ${f.name}:
330      return pack_float_${f.short_name()};
331%endfor
332   default:
333      return NULL;
334   }
335}
336
337/**
338 * Pack a row of GLubyte rgba[4] values to the destination.
339 */
340void
341_mesa_pack_ubyte_rgba_row(mesa_format format, GLuint n,
342                          const GLubyte src[][4], void *dst)
343{
344   GLuint i;
345   GLubyte *d = dst;
346
347   switch (format) {
348%for f in rgb_formats:
349   %if f.is_compressed():
350      <% continue %>
351   %endif
352
353   case ${f.name}:
354      for (i = 0; i < n; ++i) {
355         pack_ubyte_${f.short_name()}(src[i], d);
356         d += ${f.block_size() / 8};
357      }
358      break;
359%endfor
360   default:
361      assert(!"Invalid format");
362   }
363}
364
365/**
366 * Pack a row of GLuint rgba[4] values to the destination.
367 */
368void
369_mesa_pack_uint_rgba_row(mesa_format format, GLuint n,
370                          const GLuint src[][4], void *dst)
371{
372   GLuint i;
373   GLubyte *d = dst;
374
375   switch (format) {
376%for f in rgb_formats:
377   %if not f.is_int():
378      <% continue %>
379   %elif f.is_normalized():
380      <% continue %>
381   %elif f.is_compressed():
382      <% continue %>
383   %endif
384
385   case ${f.name}:
386      for (i = 0; i < n; ++i) {
387         pack_uint_${f.short_name()}(src[i], d);
388         d += ${f.block_size() / 8};
389      }
390      break;
391%endfor
392   default:
393      assert(!"Invalid format");
394   }
395}
396
397/**
398 * Pack a row of GLfloat rgba[4] values to the destination.
399 */
400void
401_mesa_pack_float_rgba_row(mesa_format format, GLuint n,
402                          const GLfloat src[][4], void *dst)
403{
404   GLuint i;
405   GLubyte *d = dst;
406
407   switch (format) {
408%for f in rgb_formats:
409   %if f.is_compressed():
410      <% continue %>
411   %elif f.is_int() and not f.is_normalized():
412      <% continue %>
413   %endif
414
415   case ${f.name}:
416      for (i = 0; i < n; ++i) {
417         pack_float_${f.short_name()}(src[i], d);
418         d += ${f.block_size() / 8};
419      }
420      break;
421%endfor
422   default:
423      assert(!"Invalid format");
424   }
425}
426
427/**
428 * Pack a 2D image of ubyte RGBA pixels in the given format.
429 * \param srcRowStride  source image row stride in bytes
430 * \param dstRowStride  destination image row stride in bytes
431 */
432void
433_mesa_pack_ubyte_rgba_rect(mesa_format format, GLuint width, GLuint height,
434                           const GLubyte *src, GLint srcRowStride,
435                           void *dst, GLint dstRowStride)
436{
437   GLubyte *dstUB = dst;
438   GLuint i;
439
440   if (srcRowStride == width * 4 * sizeof(GLubyte) &&
441       dstRowStride == _mesa_format_row_stride(format, width)) {
442      /* do whole image at once */
443      _mesa_pack_ubyte_rgba_row(format, width * height,
444                                (const GLubyte (*)[4]) src, dst);
445   }
446   else {
447      /* row by row */
448      for (i = 0; i < height; i++) {
449         _mesa_pack_ubyte_rgba_row(format, width,
450                                   (const GLubyte (*)[4]) src, dstUB);
451         src += srcRowStride;
452         dstUB += dstRowStride;
453      }
454   }
455}
456
457
458/** Helper struct for MESA_FORMAT_Z32_FLOAT_S8X24_UINT */
459struct z32f_x24s8
460{
461   float z;
462   uint32_t x24s8;
463};
464
465
466/**
467 ** Pack float Z pixels
468 **/
469
470static void
471pack_float_S8_UINT_Z24_UNORM(const GLfloat *src, void *dst)
472{
473   /* don't disturb the stencil values */
474   GLuint *d = ((GLuint *) dst);
475   const GLdouble scale = (GLdouble) 0xffffff;
476   GLuint s = *d & 0xff;
477   GLuint z = (GLuint) (*src * scale);
478   assert(z <= 0xffffff);
479   *d = (z << 8) | s;
480}
481
482static void
483pack_float_Z24_UNORM_S8_UINT(const GLfloat *src, void *dst)
484{
485   /* don't disturb the stencil values */
486   GLuint *d = ((GLuint *) dst);
487   const GLdouble scale = (GLdouble) 0xffffff;
488   GLuint s = *d & 0xff000000;
489   GLuint z = (GLuint) (*src * scale);
490   assert(z <= 0xffffff);
491   *d = s | z;
492}
493
494static void
495pack_float_Z_UNORM16(const GLfloat *src, void *dst)
496{
497   GLushort *d = ((GLushort *) dst);
498   const GLfloat scale = (GLfloat) 0xffff;
499   *d = (GLushort) (*src * scale);
500}
501
502static void
503pack_float_Z_UNORM32(const GLfloat *src, void *dst)
504{
505   GLuint *d = ((GLuint *) dst);
506   const GLdouble scale = (GLdouble) 0xffffffff;
507   *d = (GLuint) (*src * scale);
508}
509
510static void
511pack_float_Z_FLOAT32(const GLfloat *src, void *dst)
512{
513   GLfloat *d = (GLfloat *) dst;
514   *d = *src;
515}
516
517gl_pack_float_z_func
518_mesa_get_pack_float_z_func(mesa_format format)
519{
520   switch (format) {
521   case MESA_FORMAT_S8_UINT_Z24_UNORM:
522   case MESA_FORMAT_X8_UINT_Z24_UNORM:
523      return pack_float_S8_UINT_Z24_UNORM;
524   case MESA_FORMAT_Z24_UNORM_S8_UINT:
525   case MESA_FORMAT_Z24_UNORM_X8_UINT:
526      return pack_float_Z24_UNORM_S8_UINT;
527   case MESA_FORMAT_Z_UNORM16:
528      return pack_float_Z_UNORM16;
529   case MESA_FORMAT_Z_UNORM32:
530      return pack_float_Z_UNORM32;
531   case MESA_FORMAT_Z_FLOAT32:
532   case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
533      return pack_float_Z_FLOAT32;
534   default:
535      _mesa_problem(NULL,
536                    "unexpected format in _mesa_get_pack_float_z_func()");
537      return NULL;
538   }
539}
540
541
542
543/**
544 ** Pack uint Z pixels.  The incoming src value is always in
545 ** the range [0, 2^32-1].
546 **/
547
548static void
549pack_uint_S8_UINT_Z24_UNORM(const GLuint *src, void *dst)
550{
551   /* don't disturb the stencil values */
552   GLuint *d = ((GLuint *) dst);
553   GLuint s = *d & 0xff;
554   GLuint z = *src & 0xffffff00;
555   *d = z | s;
556}
557
558static void
559pack_uint_Z24_UNORM_S8_UINT(const GLuint *src, void *dst)
560{
561   /* don't disturb the stencil values */
562   GLuint *d = ((GLuint *) dst);
563   GLuint s = *d & 0xff000000;
564   GLuint z = *src >> 8;
565   *d = s | z;
566}
567
568static void
569pack_uint_Z_UNORM16(const GLuint *src, void *dst)
570{
571   GLushort *d = ((GLushort *) dst);
572   *d = *src >> 16;
573}
574
575static void
576pack_uint_Z_UNORM32(const GLuint *src, void *dst)
577{
578   GLuint *d = ((GLuint *) dst);
579   *d = *src;
580}
581
582static void
583pack_uint_Z_FLOAT32(const GLuint *src, void *dst)
584{
585   GLuint *d = ((GLuint *) dst);
586   const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
587   *d = (GLuint) (*src * scale);
588   assert(*d >= 0.0f);
589   assert(*d <= 1.0f);
590}
591
592static void
593pack_uint_Z_FLOAT32_X24S8(const GLuint *src, void *dst)
594{
595   GLfloat *d = ((GLfloat *) dst);
596   const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
597   *d = (GLfloat) (*src * scale);
598   assert(*d >= 0.0f);
599   assert(*d <= 1.0f);
600}
601
602gl_pack_uint_z_func
603_mesa_get_pack_uint_z_func(mesa_format format)
604{
605   switch (format) {
606   case MESA_FORMAT_S8_UINT_Z24_UNORM:
607   case MESA_FORMAT_X8_UINT_Z24_UNORM:
608      return pack_uint_S8_UINT_Z24_UNORM;
609   case MESA_FORMAT_Z24_UNORM_S8_UINT:
610   case MESA_FORMAT_Z24_UNORM_X8_UINT:
611      return pack_uint_Z24_UNORM_S8_UINT;
612   case MESA_FORMAT_Z_UNORM16:
613      return pack_uint_Z_UNORM16;
614   case MESA_FORMAT_Z_UNORM32:
615      return pack_uint_Z_UNORM32;
616   case MESA_FORMAT_Z_FLOAT32:
617      return pack_uint_Z_FLOAT32;
618   case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
619      return pack_uint_Z_FLOAT32_X24S8;
620   default:
621      _mesa_problem(NULL, "unexpected format in _mesa_get_pack_uint_z_func()");
622      return NULL;
623   }
624}
625
626
627/**
628 ** Pack ubyte stencil pixels
629 **/
630
631static void
632pack_ubyte_stencil_Z24_S8(const GLubyte *src, void *dst)
633{
634   /* don't disturb the Z values */
635   GLuint *d = ((GLuint *) dst);
636   GLuint s = *src;
637   GLuint z = *d & 0xffffff00;
638   *d = z | s;
639}
640
641static void
642pack_ubyte_stencil_S8_Z24(const GLubyte *src, void *dst)
643{
644   /* don't disturb the Z values */
645   GLuint *d = ((GLuint *) dst);
646   GLuint s = *src << 24;
647   GLuint z = *d & 0xffffff;
648   *d = s | z;
649}
650
651static void
652pack_ubyte_stencil_S8(const GLubyte *src, void *dst)
653{
654   GLubyte *d = (GLubyte *) dst;
655   *d = *src;
656}
657
658static void
659pack_ubyte_stencil_Z32_FLOAT_X24S8(const GLubyte *src, void *dst)
660{
661   GLfloat *d = ((GLfloat *) dst);
662   d[1] = *src;
663}
664
665
666gl_pack_ubyte_stencil_func
667_mesa_get_pack_ubyte_stencil_func(mesa_format format)
668{
669   switch (format) {
670   case MESA_FORMAT_S8_UINT_Z24_UNORM:
671      return pack_ubyte_stencil_Z24_S8;
672   case MESA_FORMAT_Z24_UNORM_S8_UINT:
673      return pack_ubyte_stencil_S8_Z24;
674   case MESA_FORMAT_S_UINT8:
675      return pack_ubyte_stencil_S8;
676   case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
677      return pack_ubyte_stencil_Z32_FLOAT_X24S8;
678   default:
679      _mesa_problem(NULL,
680                    "unexpected format in _mesa_pack_ubyte_stencil_func()");
681      return NULL;
682   }
683}
684
685
686
687void
688_mesa_pack_float_z_row(mesa_format format, GLuint n,
689                       const GLfloat *src, void *dst)
690{
691   switch (format) {
692   case MESA_FORMAT_S8_UINT_Z24_UNORM:
693   case MESA_FORMAT_X8_UINT_Z24_UNORM:
694      {
695         /* don't disturb the stencil values */
696         GLuint *d = ((GLuint *) dst);
697         const GLdouble scale = (GLdouble) 0xffffff;
698         GLuint i;
699         for (i = 0; i < n; i++) {
700            GLuint s = d[i] & 0xff;
701            GLuint z = (GLuint) (src[i] * scale);
702            assert(z <= 0xffffff);
703            d[i] = (z << 8) | s;
704         }
705      }
706      break;
707   case MESA_FORMAT_Z24_UNORM_S8_UINT:
708   case MESA_FORMAT_Z24_UNORM_X8_UINT:
709      {
710         /* don't disturb the stencil values */
711         GLuint *d = ((GLuint *) dst);
712         const GLdouble scale = (GLdouble) 0xffffff;
713         GLuint i;
714         for (i = 0; i < n; i++) {
715            GLuint s = d[i] & 0xff000000;
716            GLuint z = (GLuint) (src[i] * scale);
717            assert(z <= 0xffffff);
718            d[i] = s | z;
719         }
720      }
721      break;
722   case MESA_FORMAT_Z_UNORM16:
723      {
724         GLushort *d = ((GLushort *) dst);
725         const GLfloat scale = (GLfloat) 0xffff;
726         GLuint i;
727         for (i = 0; i < n; i++) {
728            d[i] = (GLushort) (src[i] * scale);
729         }
730      }
731      break;
732   case MESA_FORMAT_Z_UNORM32:
733      {
734         GLuint *d = ((GLuint *) dst);
735         const GLdouble scale = (GLdouble) 0xffffffff;
736         GLuint i;
737         for (i = 0; i < n; i++) {
738            d[i] = (GLuint) (src[i] * scale);
739         }
740      }
741      break;
742   case MESA_FORMAT_Z_FLOAT32:
743      memcpy(dst, src, n * sizeof(GLfloat));
744      break;
745   case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
746      {
747         struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
748         GLuint i;
749         for (i = 0; i < n; i++) {
750            d[i].z = src[i];
751         }
752      }
753      break;
754   default:
755      _mesa_problem(NULL, "unexpected format in _mesa_pack_float_z_row()");
756   }
757}
758
759
760/**
761 * The incoming Z values are always in the range [0, 0xffffffff].
762 */
763void
764_mesa_pack_uint_z_row(mesa_format format, GLuint n,
765                      const GLuint *src, void *dst)
766{
767   switch (format) {
768   case MESA_FORMAT_S8_UINT_Z24_UNORM:
769   case MESA_FORMAT_X8_UINT_Z24_UNORM:
770      {
771         /* don't disturb the stencil values */
772         GLuint *d = ((GLuint *) dst);
773         GLuint i;
774         for (i = 0; i < n; i++) {
775            GLuint s = d[i] & 0xff;
776            GLuint z = src[i] & 0xffffff00;
777            d[i] = z | s;
778         }
779      }
780      break;
781   case MESA_FORMAT_Z24_UNORM_S8_UINT:
782   case MESA_FORMAT_Z24_UNORM_X8_UINT:
783      {
784         /* don't disturb the stencil values */
785         GLuint *d = ((GLuint *) dst);
786         GLuint i;
787         for (i = 0; i < n; i++) {
788            GLuint s = d[i] & 0xff000000;
789            GLuint z = src[i] >> 8;
790            d[i] = s | z;
791         }
792      }
793      break;
794   case MESA_FORMAT_Z_UNORM16:
795      {
796         GLushort *d = ((GLushort *) dst);
797         GLuint i;
798         for (i = 0; i < n; i++) {
799            d[i] = src[i] >> 16;
800         }
801      }
802      break;
803   case MESA_FORMAT_Z_UNORM32:
804      memcpy(dst, src, n * sizeof(GLfloat));
805      break;
806   case MESA_FORMAT_Z_FLOAT32:
807      {
808         GLuint *d = ((GLuint *) dst);
809         const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
810         GLuint i;
811         for (i = 0; i < n; i++) {
812            d[i] = (GLuint) (src[i] * scale);
813            assert(d[i] >= 0.0f);
814            assert(d[i] <= 1.0f);
815         }
816      }
817      break;
818   case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
819      {
820         struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
821         const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
822         GLuint i;
823         for (i = 0; i < n; i++) {
824            d[i].z = (GLfloat) (src[i] * scale);
825            assert(d[i].z >= 0.0f);
826            assert(d[i].z <= 1.0f);
827         }
828      }
829      break;
830   default:
831      _mesa_problem(NULL, "unexpected format in _mesa_pack_uint_z_row()");
832   }
833}
834
835
836void
837_mesa_pack_ubyte_stencil_row(mesa_format format, GLuint n,
838                             const GLubyte *src, void *dst)
839{
840   switch (format) {
841   case MESA_FORMAT_S8_UINT_Z24_UNORM:
842      {
843         /* don't disturb the Z values */
844         GLuint *d = ((GLuint *) dst);
845         GLuint i;
846         for (i = 0; i < n; i++) {
847            GLuint s = src[i];
848            GLuint z = d[i] & 0xffffff00;
849            d[i] = z | s;
850         }
851      }
852      break;
853   case MESA_FORMAT_Z24_UNORM_S8_UINT:
854      {
855         /* don't disturb the Z values */
856         GLuint *d = ((GLuint *) dst);
857         GLuint i;
858         for (i = 0; i < n; i++) {
859            GLuint s = src[i] << 24;
860            GLuint z = d[i] & 0xffffff;
861            d[i] = s | z;
862         }
863      }
864      break;
865   case MESA_FORMAT_S_UINT8:
866      memcpy(dst, src, n * sizeof(GLubyte));
867      break;
868   case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
869      {
870         struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
871         GLuint i;
872         for (i = 0; i < n; i++) {
873            d[i].x24s8 = src[i];
874         }
875      }
876      break;
877   default:
878      _mesa_problem(NULL, "unexpected format in _mesa_pack_ubyte_stencil_row()");
879   }
880}
881
882
883/**
884 * Incoming Z/stencil values are always in uint_24_8 format.
885 */
886void
887_mesa_pack_uint_24_8_depth_stencil_row(mesa_format format, GLuint n,
888                                       const GLuint *src, void *dst)
889{
890   switch (format) {
891   case MESA_FORMAT_S8_UINT_Z24_UNORM:
892      memcpy(dst, src, n * sizeof(GLuint));
893      break;
894   case MESA_FORMAT_Z24_UNORM_S8_UINT:
895      {
896         GLuint *d = ((GLuint *) dst);
897         GLuint i;
898         for (i = 0; i < n; i++) {
899            GLuint s = src[i] << 24;
900            GLuint z = src[i] >> 8;
901            d[i] = s | z;
902         }
903      }
904      break;
905   case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
906      {
907         const GLdouble scale = 1.0 / (GLdouble) 0xffffff;
908         struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
909         GLuint i;
910         for (i = 0; i < n; i++) {
911            GLfloat z = (GLfloat) ((src[i] >> 8) * scale);
912            d[i].z = z;
913            d[i].x24s8 = src[i];
914         }
915      }
916      break;
917   default:
918      _mesa_problem(NULL, "bad format %s in _mesa_pack_ubyte_s_row",
919                    _mesa_get_format_name(format));
920      return;
921   }
922}
923
924
925
926/**
927 * Convert a boolean color mask to a packed color where each channel of
928 * the packed value at dst will be 0 or ~0 depending on the colorMask.
929 */
930void
931_mesa_pack_colormask(mesa_format format, const GLubyte colorMask[4], void *dst)
932{
933   GLfloat maskColor[4];
934
935   switch (_mesa_get_format_datatype(format)) {
936   case GL_UNSIGNED_NORMALIZED:
937      /* simple: 1.0 will convert to ~0 in the right bit positions */
938      maskColor[0] = colorMask[0] ? 1.0f : 0.0f;
939      maskColor[1] = colorMask[1] ? 1.0f : 0.0f;
940      maskColor[2] = colorMask[2] ? 1.0f : 0.0f;
941      maskColor[3] = colorMask[3] ? 1.0f : 0.0f;
942      _mesa_pack_float_rgba_row(format, 1,
943                                (const GLfloat (*)[4]) maskColor, dst);
944      break;
945   case GL_SIGNED_NORMALIZED:
946   case GL_FLOAT:
947      /* These formats are harder because it's hard to know the floating
948       * point values that will convert to ~0 for each color channel's bits.
949       * This solution just generates a non-zero value for each color channel
950       * then fixes up the non-zero values to be ~0.
951       * Note: we'll need to add special case code if we ever have to deal
952       * with formats with unequal color channel sizes, like R11_G11_B10.
953       * We issue a warning below for channel sizes other than 8,16,32.
954       */
955      {
956         GLuint bits = _mesa_get_format_max_bits(format); /* bits per chan */
957         GLuint bytes = _mesa_get_format_bytes(format);
958         GLuint i;
959
960         /* this should put non-zero values into the channels of dst */
961         maskColor[0] = colorMask[0] ? -1.0f : 0.0f;
962         maskColor[1] = colorMask[1] ? -1.0f : 0.0f;
963         maskColor[2] = colorMask[2] ? -1.0f : 0.0f;
964         maskColor[3] = colorMask[3] ? -1.0f : 0.0f;
965         _mesa_pack_float_rgba_row(format, 1,
966                                   (const GLfloat (*)[4]) maskColor, dst);
967
968         /* fix-up the dst channels by converting non-zero values to ~0 */
969         if (bits == 8) {
970            GLubyte *d = (GLubyte *) dst;
971            for (i = 0; i < bytes; i++) {
972               d[i] = d[i] ? 0xff : 0x0;
973            }
974         }
975         else if (bits == 16) {
976            GLushort *d = (GLushort *) dst;
977            for (i = 0; i < bytes / 2; i++) {
978               d[i] = d[i] ? 0xffff : 0x0;
979            }
980         }
981         else if (bits == 32) {
982            GLuint *d = (GLuint *) dst;
983            for (i = 0; i < bytes / 4; i++) {
984               d[i] = d[i] ? 0xffffffffU : 0x0;
985            }
986         }
987         else {
988            _mesa_problem(NULL, "unexpected size in _mesa_pack_colormask()");
989            return;
990         }
991      }
992      break;
993   default:
994      _mesa_problem(NULL, "unexpected format data type in gen_color_mask()");
995      return;
996   }
997}
998"""
999
1000template = Template(string);
1001
1002print template.render(argv = argv[0:])
1003