0b9e0d424d9eab1904805941c18dd63bfb5af85c
1 from __future__
import print_function
3 from mako
.template
import Template
7 * Mesa 3-D graphics library
9 * Copyright (c) 2011 VMware, Inc.
10 * Copyright (c) 2014 Intel Corporation.
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
33 * Color, depth, stencil packing functions.
34 * Used to pack basic color, depth and stencil formats to specific
37 * There are both per-pixel and per-row packing functions:
38 * - The former will be used by swrast to write values to the color, depth,
39 * stencil buffers when drawing points, lines and masked spans.
40 * - The later will be used for image-oriented functions like glDrawPixels,
41 * glAccum, and glTexImage.
48 #include "format_pack.h"
49 #include "format_utils.h"
51 #include "util/format_rgb9e5.h"
52 #include "util/format_r11g11b10f.h"
53 #include "util/format_srgb.h"
55 #define UNPACK(SRC, OFFSET, BITS) (((SRC) >> (OFFSET)) & MAX_UINT(BITS))
56 #define PACK(SRC, OFFSET, BITS) (((SRC) & MAX_UINT(BITS)) << (OFFSET))
59 import format_parser as parser
61 formats = parser.parse(argv[1])
65 if f.name == 'MESA_FORMAT_NONE':
67 if f.colorspace not in ('rgb', 'srgb'):
73 /* ubyte packing functions */
75 %for f in rgb_formats:
76 %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
78 %elif f.is_compressed():
83 pack_ubyte_${f.short_name()}(const GLubyte src[4], void *dst)
85 %for (i, c) in enumerate(f.channels):
86 <% i = f.swizzle.inverse()[i] %>
91 ${c.datatype()} ${c.name} =
92 %if not f.is_normalized() and f.is_int():
93 %if c.type == parser.SIGNED:
94 _mesa_unsigned_to_signed(src[${i}], ${c.size});
96 _mesa_unsigned_to_unsigned(src[${i}], ${c.size});
98 %elif c.type == parser.UNSIGNED:
99 %if f.colorspace == 'srgb' and c.name in 'rgb':
100 <% assert c.size == 8 %>
101 util_format_linear_to_srgb_8unorm(src[${i}]);
103 _mesa_unorm_to_unorm(src[${i}], 8, ${c.size});
105 %elif c.type == parser.SIGNED:
106 _mesa_unorm_to_snorm(src[${i}], 8, ${c.size});
107 %elif c.type == parser.FLOAT:
109 _mesa_unorm_to_float(src[${i}], 8);
111 _mesa_unorm_to_half(src[${i}], 8);
120 %if f.layout == parser.ARRAY:
121 ${f.datatype()} *d = (${f.datatype()} *)dst;
122 %for (i, c) in enumerate(f.channels):
128 %elif f.layout == parser.PACKED:
129 ${f.datatype()} d = 0;
130 %for (i, c) in enumerate(f.channels):
134 d |= PACK(${c.name}, ${c.shift}, ${c.size});
136 (*(${f.datatype()} *)dst) = d;
144 pack_ubyte_r9g9b9e5_float(const GLubyte src[4], void *dst)
146 GLuint *d = (GLuint *) dst;
148 rgb[0] = _mesa_unorm_to_float(src[RCOMP], 8);
149 rgb[1] = _mesa_unorm_to_float(src[GCOMP], 8);
150 rgb[2] = _mesa_unorm_to_float(src[BCOMP], 8);
151 *d = float3_to_rgb9e5(rgb);
155 pack_ubyte_r11g11b10_float(const GLubyte src[4], void *dst)
157 GLuint *d = (GLuint *) dst;
159 rgb[0] = _mesa_unorm_to_float(src[RCOMP], 8);
160 rgb[1] = _mesa_unorm_to_float(src[GCOMP], 8);
161 rgb[2] = _mesa_unorm_to_float(src[BCOMP], 8);
162 *d = float3_to_r11g11b10f(rgb);
165 /* uint packing functions */
167 %for f in rgb_formats:
170 %elif f.is_normalized():
172 %elif f.is_compressed():
177 pack_uint_${f.short_name()}(const GLuint src[4], void *dst)
179 %for (i, c) in enumerate(f.channels):
180 <% i = f.swizzle.inverse()[i] %>
185 ${c.datatype()} ${c.name} =
186 %if c.type == parser.SIGNED:
187 _mesa_signed_to_signed(src[${i}], ${c.size});
188 %elif c.type == parser.UNSIGNED:
189 _mesa_unsigned_to_unsigned(src[${i}], ${c.size});
191 assert(!"Invalid type: only integer types are allowed");
195 %if f.layout == parser.ARRAY:
196 ${f.datatype()} *d = (${f.datatype()} *)dst;
197 %for (i, c) in enumerate(f.channels):
203 %elif f.layout == parser.PACKED:
204 ${f.datatype()} d = 0;
205 %for (i, c) in enumerate(f.channels):
209 d |= PACK(${c.name}, ${c.shift}, ${c.size});
211 (*(${f.datatype()} *)dst) = d;
218 /* float packing functions */
220 %for f in rgb_formats:
221 %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
223 %elif f.is_int() and not f.is_normalized():
225 %elif f.is_compressed():
230 pack_float_${f.short_name()}(const GLfloat src[4], void *dst)
232 %for (i, c) in enumerate(f.channels):
233 <% i = f.swizzle.inverse()[i] %>
238 ${c.datatype()} ${c.name} =
239 %if c.type == parser.UNSIGNED:
240 %if f.colorspace == 'srgb' and c.name in 'rgb':
241 <% assert c.size == 8 %>
242 util_format_linear_float_to_srgb_8unorm(src[${i}]);
244 _mesa_float_to_unorm(src[${i}], ${c.size});
246 %elif c.type == parser.SIGNED:
247 _mesa_float_to_snorm(src[${i}], ${c.size});
248 %elif c.type == parser.FLOAT:
252 _mesa_float_to_half(src[${i}]);
261 %if f.layout == parser.ARRAY:
262 ${f.datatype()} *d = (${f.datatype()} *)dst;
263 %for (i, c) in enumerate(f.channels):
269 %elif f.layout == parser.PACKED:
270 ${f.datatype()} d = 0;
271 %for (i, c) in enumerate(f.channels):
275 d |= PACK(${c.name}, ${c.shift}, ${c.size});
277 (*(${f.datatype()} *)dst) = d;
285 pack_float_r9g9b9e5_float(const GLfloat src[4], void *dst)
287 GLuint *d = (GLuint *) dst;
288 *d = float3_to_rgb9e5(src);
292 pack_float_r11g11b10_float(const GLfloat src[4], void *dst)
294 GLuint *d = (GLuint *) dst;
295 *d = float3_to_r11g11b10f(src);
299 * Return a function that can pack a GLubyte rgba[4] color.
301 gl_pack_ubyte_rgba_func
302 _mesa_get_pack_ubyte_rgba_function(mesa_format format)
305 %for f in rgb_formats:
306 %if f.is_compressed():
311 return pack_ubyte_${f.short_name()};
319 * Return a function that can pack a GLfloat rgba[4] color.
321 gl_pack_float_rgba_func
322 _mesa_get_pack_float_rgba_function(mesa_format format)
325 %for f in rgb_formats:
326 %if f.is_compressed():
328 %elif f.is_int() and not f.is_normalized():
333 return pack_float_${f.short_name()};
341 * Pack a row of GLubyte rgba[4] values to the destination.
344 _mesa_pack_ubyte_rgba_row(mesa_format format, GLuint n,
345 const GLubyte src[][4], void *dst)
351 %for f in rgb_formats:
352 %if f.is_compressed():
357 for (i = 0; i < n; ++i) {
358 pack_ubyte_${f.short_name()}(src[i], d);
359 d += ${f.block_size() // 8};
364 assert(!"Invalid format");
369 * Pack a row of GLuint rgba[4] values to the destination.
372 _mesa_pack_uint_rgba_row(mesa_format format, GLuint n,
373 const GLuint src[][4], void *dst)
379 %for f in rgb_formats:
382 %elif f.is_normalized():
384 %elif f.is_compressed():
389 for (i = 0; i < n; ++i) {
390 pack_uint_${f.short_name()}(src[i], d);
391 d += ${f.block_size() // 8};
396 assert(!"Invalid format");
401 * Pack a row of GLfloat rgba[4] values to the destination.
404 _mesa_pack_float_rgba_row(mesa_format format, GLuint n,
405 const GLfloat src[][4], void *dst)
411 %for f in rgb_formats:
412 %if f.is_compressed():
414 %elif f.is_int() and not f.is_normalized():
419 for (i = 0; i < n; ++i) {
420 pack_float_${f.short_name()}(src[i], d);
421 d += ${f.block_size() // 8};
426 assert(!"Invalid format");
431 * Pack a 2D image of ubyte RGBA pixels in the given format.
432 * \param srcRowStride source image row stride in bytes
433 * \param dstRowStride destination image row stride in bytes
436 _mesa_pack_ubyte_rgba_rect(mesa_format format, GLuint width, GLuint height,
437 const GLubyte *src, GLint srcRowStride,
438 void *dst, GLint dstRowStride)
440 GLubyte *dstUB = dst;
443 if (srcRowStride == width * 4 * sizeof(GLubyte) &&
444 dstRowStride == _mesa_format_row_stride(format, width)) {
445 /* do whole image at once */
446 _mesa_pack_ubyte_rgba_row(format, width * height,
447 (const GLubyte (*)[4]) src, dst);
451 for (i = 0; i < height; i++) {
452 _mesa_pack_ubyte_rgba_row(format, width,
453 (const GLubyte (*)[4]) src, dstUB);
455 dstUB += dstRowStride;
461 /** Helper struct for MESA_FORMAT_Z32_FLOAT_S8X24_UINT */
470 ** Pack float Z pixels
474 pack_float_S8_UINT_Z24_UNORM(const GLfloat *src, void *dst)
476 /* don't disturb the stencil values */
477 GLuint *d = ((GLuint *) dst);
478 const GLdouble scale = (GLdouble) 0xffffff;
479 GLuint s = *d & 0xff;
480 GLuint z = (GLuint) (*src * scale);
481 assert(z <= 0xffffff);
486 pack_float_Z24_UNORM_S8_UINT(const GLfloat *src, void *dst)
488 /* don't disturb the stencil values */
489 GLuint *d = ((GLuint *) dst);
490 const GLdouble scale = (GLdouble) 0xffffff;
491 GLuint s = *d & 0xff000000;
492 GLuint z = (GLuint) (*src * scale);
493 assert(z <= 0xffffff);
498 pack_float_Z_UNORM16(const GLfloat *src, void *dst)
500 GLushort *d = ((GLushort *) dst);
501 const GLfloat scale = (GLfloat) 0xffff;
502 *d = (GLushort) (*src * scale);
506 pack_float_Z_UNORM32(const GLfloat *src, void *dst)
508 GLuint *d = ((GLuint *) dst);
509 const GLdouble scale = (GLdouble) 0xffffffff;
510 *d = (GLuint) (*src * scale);
514 pack_float_Z_FLOAT32(const GLfloat *src, void *dst)
516 GLfloat *d = (GLfloat *) dst;
521 _mesa_get_pack_float_z_func(mesa_format format)
524 case MESA_FORMAT_S8_UINT_Z24_UNORM:
525 case MESA_FORMAT_X8_UINT_Z24_UNORM:
526 return pack_float_S8_UINT_Z24_UNORM;
527 case MESA_FORMAT_Z24_UNORM_S8_UINT:
528 case MESA_FORMAT_Z24_UNORM_X8_UINT:
529 return pack_float_Z24_UNORM_S8_UINT;
530 case MESA_FORMAT_Z_UNORM16:
531 return pack_float_Z_UNORM16;
532 case MESA_FORMAT_Z_UNORM32:
533 return pack_float_Z_UNORM32;
534 case MESA_FORMAT_Z_FLOAT32:
535 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
536 return pack_float_Z_FLOAT32;
539 "unexpected format in _mesa_get_pack_float_z_func()");
547 ** Pack uint Z pixels. The incoming src value is always in
548 ** the range [0, 2^32-1].
552 pack_uint_S8_UINT_Z24_UNORM(const GLuint *src, void *dst)
554 /* don't disturb the stencil values */
555 GLuint *d = ((GLuint *) dst);
556 GLuint s = *d & 0xff;
557 GLuint z = *src & 0xffffff00;
562 pack_uint_Z24_UNORM_S8_UINT(const GLuint *src, void *dst)
564 /* don't disturb the stencil values */
565 GLuint *d = ((GLuint *) dst);
566 GLuint s = *d & 0xff000000;
567 GLuint z = *src >> 8;
572 pack_uint_Z_UNORM16(const GLuint *src, void *dst)
574 GLushort *d = ((GLushort *) dst);
579 pack_uint_Z_UNORM32(const GLuint *src, void *dst)
581 GLuint *d = ((GLuint *) dst);
586 pack_uint_Z_FLOAT32(const GLuint *src, void *dst)
588 GLuint *d = ((GLuint *) dst);
589 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
590 *d = (GLuint) (*src * scale);
596 pack_uint_Z_FLOAT32_X24S8(const GLuint *src, void *dst)
598 GLfloat *d = ((GLfloat *) dst);
599 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
600 *d = (GLfloat) (*src * scale);
606 _mesa_get_pack_uint_z_func(mesa_format format)
609 case MESA_FORMAT_S8_UINT_Z24_UNORM:
610 case MESA_FORMAT_X8_UINT_Z24_UNORM:
611 return pack_uint_S8_UINT_Z24_UNORM;
612 case MESA_FORMAT_Z24_UNORM_S8_UINT:
613 case MESA_FORMAT_Z24_UNORM_X8_UINT:
614 return pack_uint_Z24_UNORM_S8_UINT;
615 case MESA_FORMAT_Z_UNORM16:
616 return pack_uint_Z_UNORM16;
617 case MESA_FORMAT_Z_UNORM32:
618 return pack_uint_Z_UNORM32;
619 case MESA_FORMAT_Z_FLOAT32:
620 return pack_uint_Z_FLOAT32;
621 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
622 return pack_uint_Z_FLOAT32_X24S8;
624 _mesa_problem(NULL, "unexpected format in _mesa_get_pack_uint_z_func()");
631 ** Pack ubyte stencil pixels
635 pack_ubyte_stencil_Z24_S8(const GLubyte *src, void *dst)
637 /* don't disturb the Z values */
638 GLuint *d = ((GLuint *) dst);
640 GLuint z = *d & 0xffffff00;
645 pack_ubyte_stencil_S8_Z24(const GLubyte *src, void *dst)
647 /* don't disturb the Z values */
648 GLuint *d = ((GLuint *) dst);
649 GLuint s = *src << 24;
650 GLuint z = *d & 0xffffff;
655 pack_ubyte_stencil_S8(const GLubyte *src, void *dst)
657 GLubyte *d = (GLubyte *) dst;
662 pack_ubyte_stencil_Z32_FLOAT_X24S8(const GLubyte *src, void *dst)
664 GLfloat *d = ((GLfloat *) dst);
669 gl_pack_ubyte_stencil_func
670 _mesa_get_pack_ubyte_stencil_func(mesa_format format)
673 case MESA_FORMAT_S8_UINT_Z24_UNORM:
674 return pack_ubyte_stencil_Z24_S8;
675 case MESA_FORMAT_Z24_UNORM_S8_UINT:
676 return pack_ubyte_stencil_S8_Z24;
677 case MESA_FORMAT_S_UINT8:
678 return pack_ubyte_stencil_S8;
679 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
680 return pack_ubyte_stencil_Z32_FLOAT_X24S8;
683 "unexpected format in _mesa_pack_ubyte_stencil_func()");
691 _mesa_pack_float_z_row(mesa_format format, GLuint n,
692 const GLfloat *src, void *dst)
695 case MESA_FORMAT_S8_UINT_Z24_UNORM:
696 case MESA_FORMAT_X8_UINT_Z24_UNORM:
698 /* don't disturb the stencil values */
699 GLuint *d = ((GLuint *) dst);
700 const GLdouble scale = (GLdouble) 0xffffff;
702 for (i = 0; i < n; i++) {
703 GLuint s = d[i] & 0xff;
704 GLuint z = (GLuint) (src[i] * scale);
705 assert(z <= 0xffffff);
710 case MESA_FORMAT_Z24_UNORM_S8_UINT:
711 case MESA_FORMAT_Z24_UNORM_X8_UINT:
713 /* don't disturb the stencil values */
714 GLuint *d = ((GLuint *) dst);
715 const GLdouble scale = (GLdouble) 0xffffff;
717 for (i = 0; i < n; i++) {
718 GLuint s = d[i] & 0xff000000;
719 GLuint z = (GLuint) (src[i] * scale);
720 assert(z <= 0xffffff);
725 case MESA_FORMAT_Z_UNORM16:
727 GLushort *d = ((GLushort *) dst);
728 const GLfloat scale = (GLfloat) 0xffff;
730 for (i = 0; i < n; i++) {
731 d[i] = (GLushort) (src[i] * scale);
735 case MESA_FORMAT_Z_UNORM32:
737 GLuint *d = ((GLuint *) dst);
738 const GLdouble scale = (GLdouble) 0xffffffff;
740 for (i = 0; i < n; i++) {
741 d[i] = (GLuint) (src[i] * scale);
745 case MESA_FORMAT_Z_FLOAT32:
746 memcpy(dst, src, n * sizeof(GLfloat));
748 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
750 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
752 for (i = 0; i < n; i++) {
758 _mesa_problem(NULL, "unexpected format in _mesa_pack_float_z_row()");
764 * The incoming Z values are always in the range [0, 0xffffffff].
767 _mesa_pack_uint_z_row(mesa_format format, GLuint n,
768 const GLuint *src, void *dst)
771 case MESA_FORMAT_S8_UINT_Z24_UNORM:
772 case MESA_FORMAT_X8_UINT_Z24_UNORM:
774 /* don't disturb the stencil values */
775 GLuint *d = ((GLuint *) dst);
777 for (i = 0; i < n; i++) {
778 GLuint s = d[i] & 0xff;
779 GLuint z = src[i] & 0xffffff00;
784 case MESA_FORMAT_Z24_UNORM_S8_UINT:
785 case MESA_FORMAT_Z24_UNORM_X8_UINT:
787 /* don't disturb the stencil values */
788 GLuint *d = ((GLuint *) dst);
790 for (i = 0; i < n; i++) {
791 GLuint s = d[i] & 0xff000000;
792 GLuint z = src[i] >> 8;
797 case MESA_FORMAT_Z_UNORM16:
799 GLushort *d = ((GLushort *) dst);
801 for (i = 0; i < n; i++) {
806 case MESA_FORMAT_Z_UNORM32:
807 memcpy(dst, src, n * sizeof(GLfloat));
809 case MESA_FORMAT_Z_FLOAT32:
811 GLuint *d = ((GLuint *) dst);
812 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
814 for (i = 0; i < n; i++) {
815 d[i] = (GLuint) (src[i] * scale);
816 assert(d[i] >= 0.0f);
817 assert(d[i] <= 1.0f);
821 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
823 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
824 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
826 for (i = 0; i < n; i++) {
827 d[i].z = (GLfloat) (src[i] * scale);
828 assert(d[i].z >= 0.0f);
829 assert(d[i].z <= 1.0f);
834 _mesa_problem(NULL, "unexpected format in _mesa_pack_uint_z_row()");
840 _mesa_pack_ubyte_stencil_row(mesa_format format, GLuint n,
841 const GLubyte *src, void *dst)
844 case MESA_FORMAT_S8_UINT_Z24_UNORM:
846 /* don't disturb the Z values */
847 GLuint *d = ((GLuint *) dst);
849 for (i = 0; i < n; i++) {
851 GLuint z = d[i] & 0xffffff00;
856 case MESA_FORMAT_Z24_UNORM_S8_UINT:
858 /* don't disturb the Z values */
859 GLuint *d = ((GLuint *) dst);
861 for (i = 0; i < n; i++) {
862 GLuint s = src[i] << 24;
863 GLuint z = d[i] & 0xffffff;
868 case MESA_FORMAT_S_UINT8:
869 memcpy(dst, src, n * sizeof(GLubyte));
871 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
873 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
875 for (i = 0; i < n; i++) {
881 _mesa_problem(NULL, "unexpected format in _mesa_pack_ubyte_stencil_row()");
887 * Incoming Z/stencil values are always in uint_24_8 format.
890 _mesa_pack_uint_24_8_depth_stencil_row(mesa_format format, GLuint n,
891 const GLuint *src, void *dst)
894 case MESA_FORMAT_S8_UINT_Z24_UNORM:
895 memcpy(dst, src, n * sizeof(GLuint));
897 case MESA_FORMAT_Z24_UNORM_S8_UINT:
899 GLuint *d = ((GLuint *) dst);
901 for (i = 0; i < n; i++) {
902 GLuint s = src[i] << 24;
903 GLuint z = src[i] >> 8;
908 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
910 const GLdouble scale = 1.0 / (GLdouble) 0xffffff;
911 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
913 for (i = 0; i < n; i++) {
914 GLfloat z = (GLfloat) ((src[i] >> 8) * scale);
921 _mesa_problem(NULL, "bad format %s in _mesa_pack_ubyte_s_row",
922 _mesa_get_format_name(format));
930 * Convert a boolean color mask to a packed color where each channel of
931 * the packed value at dst will be 0 or ~0 depending on the colorMask.
934 _mesa_pack_colormask(mesa_format format, const GLubyte colorMask[4], void *dst)
936 GLfloat maskColor[4];
938 switch (_mesa_get_format_datatype(format)) {
939 case GL_UNSIGNED_NORMALIZED:
940 /* simple: 1.0 will convert to ~0 in the right bit positions */
941 maskColor[0] = colorMask[0] ? 1.0f : 0.0f;
942 maskColor[1] = colorMask[1] ? 1.0f : 0.0f;
943 maskColor[2] = colorMask[2] ? 1.0f : 0.0f;
944 maskColor[3] = colorMask[3] ? 1.0f : 0.0f;
945 _mesa_pack_float_rgba_row(format, 1,
946 (const GLfloat (*)[4]) maskColor, dst);
948 case GL_SIGNED_NORMALIZED:
950 /* These formats are harder because it's hard to know the floating
951 * point values that will convert to ~0 for each color channel's bits.
952 * This solution just generates a non-zero value for each color channel
953 * then fixes up the non-zero values to be ~0.
954 * Note: we'll need to add special case code if we ever have to deal
955 * with formats with unequal color channel sizes, like R11_G11_B10.
956 * We issue a warning below for channel sizes other than 8,16,32.
959 GLuint bits = _mesa_get_format_max_bits(format); /* bits per chan */
960 GLuint bytes = _mesa_get_format_bytes(format);
963 /* this should put non-zero values into the channels of dst */
964 maskColor[0] = colorMask[0] ? -1.0f : 0.0f;
965 maskColor[1] = colorMask[1] ? -1.0f : 0.0f;
966 maskColor[2] = colorMask[2] ? -1.0f : 0.0f;
967 maskColor[3] = colorMask[3] ? -1.0f : 0.0f;
968 _mesa_pack_float_rgba_row(format, 1,
969 (const GLfloat (*)[4]) maskColor, dst);
971 /* fix-up the dst channels by converting non-zero values to ~0 */
973 GLubyte *d = (GLubyte *) dst;
974 for (i = 0; i < bytes; i++) {
975 d[i] = d[i] ? 0xff : 0x0;
978 else if (bits == 16) {
979 GLushort *d = (GLushort *) dst;
980 for (i = 0; i < bytes / 2; i++) {
981 d[i] = d[i] ? 0xffff : 0x0;
984 else if (bits == 32) {
985 GLuint *d = (GLuint *) dst;
986 for (i = 0; i < bytes / 4; i++) {
987 d[i] = d[i] ? 0xffffffffU : 0x0;
991 _mesa_problem(NULL, "unexpected size in _mesa_pack_colormask()");
997 _mesa_problem(NULL, "unexpected format data type in gen_color_mask()");
1003 template
= Template(string
, future_imports
=['division']);
1005 print(template
.render(argv
= argv
[0:]))