mesa: add ASTC 2D LDR decoder
[mesa.git] / src / mesa / main / format_pack.py
1 from __future__ import print_function
2
3 from mako.template import Template
4 from sys import argv
5
6 string = """/*
7 * Mesa 3-D graphics library
8 *
9 * Copyright (c) 2011 VMware, Inc.
10 * Copyright (c) 2014 Intel Corporation.
11 *
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:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
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.
29 */
30
31
32 /**
33 * Color, depth, stencil packing functions.
34 * Used to pack basic color, depth and stencil formats to specific
35 * hardware formats.
36 *
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.
42 */
43
44 #include <stdint.h>
45
46 #include "config.h"
47 #include "errors.h"
48 #include "format_pack.h"
49 #include "format_utils.h"
50 #include "macros.h"
51 #include "util/format_rgb9e5.h"
52 #include "util/format_r11g11b10f.h"
53 #include "util/format_srgb.h"
54
55 #define UNPACK(SRC, OFFSET, BITS) (((SRC) >> (OFFSET)) & MAX_UINT(BITS))
56 #define PACK(SRC, OFFSET, BITS) (((SRC) & MAX_UINT(BITS)) << (OFFSET))
57
58 <%
59 import format_parser as parser
60
61 formats = parser.parse(argv[1])
62
63 rgb_formats = []
64 for f in formats:
65 if f.name == 'MESA_FORMAT_NONE':
66 continue
67 if f.colorspace not in ('rgb', 'srgb'):
68 continue
69
70 rgb_formats.append(f)
71 %>
72
73 /* ubyte packing functions */
74
75 %for f in rgb_formats:
76 %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
77 <% continue %>
78 %elif f.is_compressed():
79 <% continue %>
80 %endif
81
82 static inline void
83 pack_ubyte_${f.short_name()}(const GLubyte src[4], void *dst)
84 {
85 %for (i, c) in enumerate(f.channels):
86 <% i = f.swizzle.inverse()[i] %>
87 %if c.type == 'x':
88 <% continue %>
89 %endif
90
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});
95 %else:
96 _mesa_unsigned_to_unsigned(src[${i}], ${c.size});
97 %endif
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}]);
102 %else:
103 _mesa_unorm_to_unorm(src[${i}], 8, ${c.size});
104 %endif
105 %elif c.type == parser.SIGNED:
106 _mesa_unorm_to_snorm(src[${i}], 8, ${c.size});
107 %elif c.type == parser.FLOAT:
108 %if c.size == 32:
109 _mesa_unorm_to_float(src[${i}], 8);
110 %elif c.size == 16:
111 _mesa_unorm_to_half(src[${i}], 8);
112 %else:
113 <% assert False %>
114 %endif
115 %else:
116 <% assert False %>
117 %endif
118 %endfor
119
120 %if f.layout == parser.ARRAY:
121 ${f.datatype()} *d = (${f.datatype()} *)dst;
122 %for (i, c) in enumerate(f.channels):
123 %if c.type == 'x':
124 <% continue %>
125 %endif
126 d[${i}] = ${c.name};
127 %endfor
128 %elif f.layout == parser.PACKED:
129 ${f.datatype()} d = 0;
130 %for (i, c) in enumerate(f.channels):
131 %if c.type == 'x':
132 <% continue %>
133 %endif
134 d |= PACK(${c.name}, ${c.shift}, ${c.size});
135 %endfor
136 (*(${f.datatype()} *)dst) = d;
137 %else:
138 <% assert False %>
139 %endif
140 }
141 %endfor
142
143 static inline void
144 pack_ubyte_r9g9b9e5_float(const GLubyte src[4], void *dst)
145 {
146 GLuint *d = (GLuint *) dst;
147 GLfloat rgb[3];
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);
152 }
153
154 static inline void
155 pack_ubyte_r11g11b10_float(const GLubyte src[4], void *dst)
156 {
157 GLuint *d = (GLuint *) dst;
158 GLfloat rgb[3];
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);
163 }
164
165 /* uint packing functions */
166
167 %for f in rgb_formats:
168 %if not f.is_int():
169 <% continue %>
170 %elif f.is_normalized():
171 <% continue %>
172 %elif f.is_compressed():
173 <% continue %>
174 %endif
175
176 static inline void
177 pack_uint_${f.short_name()}(const GLuint src[4], void *dst)
178 {
179 %for (i, c) in enumerate(f.channels):
180 <% i = f.swizzle.inverse()[i] %>
181 %if c.type == 'x':
182 <% continue %>
183 %endif
184
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});
190 %else:
191 assert(!"Invalid type: only integer types are allowed");
192 %endif
193 %endfor
194
195 %if f.layout == parser.ARRAY:
196 ${f.datatype()} *d = (${f.datatype()} *)dst;
197 %for (i, c) in enumerate(f.channels):
198 %if c.type == 'x':
199 <% continue %>
200 %endif
201 d[${i}] = ${c.name};
202 %endfor
203 %elif f.layout == parser.PACKED:
204 ${f.datatype()} d = 0;
205 %for (i, c) in enumerate(f.channels):
206 %if c.type == 'x':
207 <% continue %>
208 %endif
209 d |= PACK(${c.name}, ${c.shift}, ${c.size});
210 %endfor
211 (*(${f.datatype()} *)dst) = d;
212 %else:
213 <% assert False %>
214 %endif
215 }
216 %endfor
217
218 /* float packing functions */
219
220 %for f in rgb_formats:
221 %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
222 <% continue %>
223 %elif f.is_int() and not f.is_normalized():
224 <% continue %>
225 %elif f.is_compressed():
226 <% continue %>
227 %endif
228
229 static inline void
230 pack_float_${f.short_name()}(const GLfloat src[4], void *dst)
231 {
232 %for (i, c) in enumerate(f.channels):
233 <% i = f.swizzle.inverse()[i] %>
234 %if c.type == 'x':
235 <% continue %>
236 %endif
237
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}]);
243 %else:
244 _mesa_float_to_unorm(src[${i}], ${c.size});
245 %endif
246 %elif c.type == parser.SIGNED:
247 _mesa_float_to_snorm(src[${i}], ${c.size});
248 %elif c.type == parser.FLOAT:
249 %if c.size == 32:
250 src[${i}];
251 %elif c.size == 16:
252 _mesa_float_to_half(src[${i}]);
253 %else:
254 <% assert False %>
255 %endif
256 %else:
257 <% assert False %>
258 %endif
259 %endfor
260
261 %if f.layout == parser.ARRAY:
262 ${f.datatype()} *d = (${f.datatype()} *)dst;
263 %for (i, c) in enumerate(f.channels):
264 %if c.type == 'x':
265 <% continue %>
266 %endif
267 d[${i}] = ${c.name};
268 %endfor
269 %elif f.layout == parser.PACKED:
270 ${f.datatype()} d = 0;
271 %for (i, c) in enumerate(f.channels):
272 %if c.type == 'x':
273 <% continue %>
274 %endif
275 d |= PACK(${c.name}, ${c.shift}, ${c.size});
276 %endfor
277 (*(${f.datatype()} *)dst) = d;
278 %else:
279 <% assert False %>
280 %endif
281 }
282 %endfor
283
284 static inline void
285 pack_float_r9g9b9e5_float(const GLfloat src[4], void *dst)
286 {
287 GLuint *d = (GLuint *) dst;
288 *d = float3_to_rgb9e5(src);
289 }
290
291 static inline void
292 pack_float_r11g11b10_float(const GLfloat src[4], void *dst)
293 {
294 GLuint *d = (GLuint *) dst;
295 *d = float3_to_r11g11b10f(src);
296 }
297
298 /**
299 * Return a function that can pack a GLubyte rgba[4] color.
300 */
301 gl_pack_ubyte_rgba_func
302 _mesa_get_pack_ubyte_rgba_function(mesa_format format)
303 {
304 switch (format) {
305 %for f in rgb_formats:
306 %if f.is_compressed():
307 <% continue %>
308 %endif
309
310 case ${f.name}:
311 return pack_ubyte_${f.short_name()};
312 %endfor
313 default:
314 return NULL;
315 }
316 }
317
318 /**
319 * Return a function that can pack a GLfloat rgba[4] color.
320 */
321 gl_pack_float_rgba_func
322 _mesa_get_pack_float_rgba_function(mesa_format format)
323 {
324 switch (format) {
325 %for f in rgb_formats:
326 %if f.is_compressed():
327 <% continue %>
328 %elif f.is_int() and not f.is_normalized():
329 <% continue %>
330 %endif
331
332 case ${f.name}:
333 return pack_float_${f.short_name()};
334 %endfor
335 default:
336 return NULL;
337 }
338 }
339
340 /**
341 * Pack a row of GLubyte rgba[4] values to the destination.
342 */
343 void
344 _mesa_pack_ubyte_rgba_row(mesa_format format, GLuint n,
345 const GLubyte src[][4], void *dst)
346 {
347 GLuint i;
348 GLubyte *d = dst;
349
350 switch (format) {
351 %for f in rgb_formats:
352 %if f.is_compressed():
353 <% continue %>
354 %endif
355
356 case ${f.name}:
357 for (i = 0; i < n; ++i) {
358 pack_ubyte_${f.short_name()}(src[i], d);
359 d += ${f.block_size() / 8};
360 }
361 break;
362 %endfor
363 default:
364 assert(!"Invalid format");
365 }
366 }
367
368 /**
369 * Pack a row of GLuint rgba[4] values to the destination.
370 */
371 void
372 _mesa_pack_uint_rgba_row(mesa_format format, GLuint n,
373 const GLuint src[][4], void *dst)
374 {
375 GLuint i;
376 GLubyte *d = dst;
377
378 switch (format) {
379 %for f in rgb_formats:
380 %if not f.is_int():
381 <% continue %>
382 %elif f.is_normalized():
383 <% continue %>
384 %elif f.is_compressed():
385 <% continue %>
386 %endif
387
388 case ${f.name}:
389 for (i = 0; i < n; ++i) {
390 pack_uint_${f.short_name()}(src[i], d);
391 d += ${f.block_size() / 8};
392 }
393 break;
394 %endfor
395 default:
396 assert(!"Invalid format");
397 }
398 }
399
400 /**
401 * Pack a row of GLfloat rgba[4] values to the destination.
402 */
403 void
404 _mesa_pack_float_rgba_row(mesa_format format, GLuint n,
405 const GLfloat src[][4], void *dst)
406 {
407 GLuint i;
408 GLubyte *d = dst;
409
410 switch (format) {
411 %for f in rgb_formats:
412 %if f.is_compressed():
413 <% continue %>
414 %elif f.is_int() and not f.is_normalized():
415 <% continue %>
416 %endif
417
418 case ${f.name}:
419 for (i = 0; i < n; ++i) {
420 pack_float_${f.short_name()}(src[i], d);
421 d += ${f.block_size() / 8};
422 }
423 break;
424 %endfor
425 default:
426 assert(!"Invalid format");
427 }
428 }
429
430 /**
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
434 */
435 void
436 _mesa_pack_ubyte_rgba_rect(mesa_format format, GLuint width, GLuint height,
437 const GLubyte *src, GLint srcRowStride,
438 void *dst, GLint dstRowStride)
439 {
440 GLubyte *dstUB = dst;
441 GLuint i;
442
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);
448 }
449 else {
450 /* row by row */
451 for (i = 0; i < height; i++) {
452 _mesa_pack_ubyte_rgba_row(format, width,
453 (const GLubyte (*)[4]) src, dstUB);
454 src += srcRowStride;
455 dstUB += dstRowStride;
456 }
457 }
458 }
459
460
461 /** Helper struct for MESA_FORMAT_Z32_FLOAT_S8X24_UINT */
462 struct z32f_x24s8
463 {
464 float z;
465 uint32_t x24s8;
466 };
467
468
469 /**
470 ** Pack float Z pixels
471 **/
472
473 static void
474 pack_float_S8_UINT_Z24_UNORM(const GLfloat *src, void *dst)
475 {
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);
482 *d = (z << 8) | s;
483 }
484
485 static void
486 pack_float_Z24_UNORM_S8_UINT(const GLfloat *src, void *dst)
487 {
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);
494 *d = s | z;
495 }
496
497 static void
498 pack_float_Z_UNORM16(const GLfloat *src, void *dst)
499 {
500 GLushort *d = ((GLushort *) dst);
501 const GLfloat scale = (GLfloat) 0xffff;
502 *d = (GLushort) (*src * scale);
503 }
504
505 static void
506 pack_float_Z_UNORM32(const GLfloat *src, void *dst)
507 {
508 GLuint *d = ((GLuint *) dst);
509 const GLdouble scale = (GLdouble) 0xffffffff;
510 *d = (GLuint) (*src * scale);
511 }
512
513 static void
514 pack_float_Z_FLOAT32(const GLfloat *src, void *dst)
515 {
516 GLfloat *d = (GLfloat *) dst;
517 *d = *src;
518 }
519
520 gl_pack_float_z_func
521 _mesa_get_pack_float_z_func(mesa_format format)
522 {
523 switch (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;
537 default:
538 _mesa_problem(NULL,
539 "unexpected format in _mesa_get_pack_float_z_func()");
540 return NULL;
541 }
542 }
543
544
545
546 /**
547 ** Pack uint Z pixels. The incoming src value is always in
548 ** the range [0, 2^32-1].
549 **/
550
551 static void
552 pack_uint_S8_UINT_Z24_UNORM(const GLuint *src, void *dst)
553 {
554 /* don't disturb the stencil values */
555 GLuint *d = ((GLuint *) dst);
556 GLuint s = *d & 0xff;
557 GLuint z = *src & 0xffffff00;
558 *d = z | s;
559 }
560
561 static void
562 pack_uint_Z24_UNORM_S8_UINT(const GLuint *src, void *dst)
563 {
564 /* don't disturb the stencil values */
565 GLuint *d = ((GLuint *) dst);
566 GLuint s = *d & 0xff000000;
567 GLuint z = *src >> 8;
568 *d = s | z;
569 }
570
571 static void
572 pack_uint_Z_UNORM16(const GLuint *src, void *dst)
573 {
574 GLushort *d = ((GLushort *) dst);
575 *d = *src >> 16;
576 }
577
578 static void
579 pack_uint_Z_UNORM32(const GLuint *src, void *dst)
580 {
581 GLuint *d = ((GLuint *) dst);
582 *d = *src;
583 }
584
585 static void
586 pack_uint_Z_FLOAT32(const GLuint *src, void *dst)
587 {
588 GLuint *d = ((GLuint *) dst);
589 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
590 *d = (GLuint) (*src * scale);
591 assert(*d >= 0.0f);
592 assert(*d <= 1.0f);
593 }
594
595 static void
596 pack_uint_Z_FLOAT32_X24S8(const GLuint *src, void *dst)
597 {
598 GLfloat *d = ((GLfloat *) dst);
599 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
600 *d = (GLfloat) (*src * scale);
601 assert(*d >= 0.0f);
602 assert(*d <= 1.0f);
603 }
604
605 gl_pack_uint_z_func
606 _mesa_get_pack_uint_z_func(mesa_format format)
607 {
608 switch (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;
623 default:
624 _mesa_problem(NULL, "unexpected format in _mesa_get_pack_uint_z_func()");
625 return NULL;
626 }
627 }
628
629
630 /**
631 ** Pack ubyte stencil pixels
632 **/
633
634 static void
635 pack_ubyte_stencil_Z24_S8(const GLubyte *src, void *dst)
636 {
637 /* don't disturb the Z values */
638 GLuint *d = ((GLuint *) dst);
639 GLuint s = *src;
640 GLuint z = *d & 0xffffff00;
641 *d = z | s;
642 }
643
644 static void
645 pack_ubyte_stencil_S8_Z24(const GLubyte *src, void *dst)
646 {
647 /* don't disturb the Z values */
648 GLuint *d = ((GLuint *) dst);
649 GLuint s = *src << 24;
650 GLuint z = *d & 0xffffff;
651 *d = s | z;
652 }
653
654 static void
655 pack_ubyte_stencil_S8(const GLubyte *src, void *dst)
656 {
657 GLubyte *d = (GLubyte *) dst;
658 *d = *src;
659 }
660
661 static void
662 pack_ubyte_stencil_Z32_FLOAT_X24S8(const GLubyte *src, void *dst)
663 {
664 GLfloat *d = ((GLfloat *) dst);
665 d[1] = *src;
666 }
667
668
669 gl_pack_ubyte_stencil_func
670 _mesa_get_pack_ubyte_stencil_func(mesa_format format)
671 {
672 switch (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;
681 default:
682 _mesa_problem(NULL,
683 "unexpected format in _mesa_pack_ubyte_stencil_func()");
684 return NULL;
685 }
686 }
687
688
689
690 void
691 _mesa_pack_float_z_row(mesa_format format, GLuint n,
692 const GLfloat *src, void *dst)
693 {
694 switch (format) {
695 case MESA_FORMAT_S8_UINT_Z24_UNORM:
696 case MESA_FORMAT_X8_UINT_Z24_UNORM:
697 {
698 /* don't disturb the stencil values */
699 GLuint *d = ((GLuint *) dst);
700 const GLdouble scale = (GLdouble) 0xffffff;
701 GLuint i;
702 for (i = 0; i < n; i++) {
703 GLuint s = d[i] & 0xff;
704 GLuint z = (GLuint) (src[i] * scale);
705 assert(z <= 0xffffff);
706 d[i] = (z << 8) | s;
707 }
708 }
709 break;
710 case MESA_FORMAT_Z24_UNORM_S8_UINT:
711 case MESA_FORMAT_Z24_UNORM_X8_UINT:
712 {
713 /* don't disturb the stencil values */
714 GLuint *d = ((GLuint *) dst);
715 const GLdouble scale = (GLdouble) 0xffffff;
716 GLuint i;
717 for (i = 0; i < n; i++) {
718 GLuint s = d[i] & 0xff000000;
719 GLuint z = (GLuint) (src[i] * scale);
720 assert(z <= 0xffffff);
721 d[i] = s | z;
722 }
723 }
724 break;
725 case MESA_FORMAT_Z_UNORM16:
726 {
727 GLushort *d = ((GLushort *) dst);
728 const GLfloat scale = (GLfloat) 0xffff;
729 GLuint i;
730 for (i = 0; i < n; i++) {
731 d[i] = (GLushort) (src[i] * scale);
732 }
733 }
734 break;
735 case MESA_FORMAT_Z_UNORM32:
736 {
737 GLuint *d = ((GLuint *) dst);
738 const GLdouble scale = (GLdouble) 0xffffffff;
739 GLuint i;
740 for (i = 0; i < n; i++) {
741 d[i] = (GLuint) (src[i] * scale);
742 }
743 }
744 break;
745 case MESA_FORMAT_Z_FLOAT32:
746 memcpy(dst, src, n * sizeof(GLfloat));
747 break;
748 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
749 {
750 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
751 GLuint i;
752 for (i = 0; i < n; i++) {
753 d[i].z = src[i];
754 }
755 }
756 break;
757 default:
758 _mesa_problem(NULL, "unexpected format in _mesa_pack_float_z_row()");
759 }
760 }
761
762
763 /**
764 * The incoming Z values are always in the range [0, 0xffffffff].
765 */
766 void
767 _mesa_pack_uint_z_row(mesa_format format, GLuint n,
768 const GLuint *src, void *dst)
769 {
770 switch (format) {
771 case MESA_FORMAT_S8_UINT_Z24_UNORM:
772 case MESA_FORMAT_X8_UINT_Z24_UNORM:
773 {
774 /* don't disturb the stencil values */
775 GLuint *d = ((GLuint *) dst);
776 GLuint i;
777 for (i = 0; i < n; i++) {
778 GLuint s = d[i] & 0xff;
779 GLuint z = src[i] & 0xffffff00;
780 d[i] = z | s;
781 }
782 }
783 break;
784 case MESA_FORMAT_Z24_UNORM_S8_UINT:
785 case MESA_FORMAT_Z24_UNORM_X8_UINT:
786 {
787 /* don't disturb the stencil values */
788 GLuint *d = ((GLuint *) dst);
789 GLuint i;
790 for (i = 0; i < n; i++) {
791 GLuint s = d[i] & 0xff000000;
792 GLuint z = src[i] >> 8;
793 d[i] = s | z;
794 }
795 }
796 break;
797 case MESA_FORMAT_Z_UNORM16:
798 {
799 GLushort *d = ((GLushort *) dst);
800 GLuint i;
801 for (i = 0; i < n; i++) {
802 d[i] = src[i] >> 16;
803 }
804 }
805 break;
806 case MESA_FORMAT_Z_UNORM32:
807 memcpy(dst, src, n * sizeof(GLfloat));
808 break;
809 case MESA_FORMAT_Z_FLOAT32:
810 {
811 GLuint *d = ((GLuint *) dst);
812 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
813 GLuint i;
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);
818 }
819 }
820 break;
821 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
822 {
823 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
824 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
825 GLuint i;
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);
830 }
831 }
832 break;
833 default:
834 _mesa_problem(NULL, "unexpected format in _mesa_pack_uint_z_row()");
835 }
836 }
837
838
839 void
840 _mesa_pack_ubyte_stencil_row(mesa_format format, GLuint n,
841 const GLubyte *src, void *dst)
842 {
843 switch (format) {
844 case MESA_FORMAT_S8_UINT_Z24_UNORM:
845 {
846 /* don't disturb the Z values */
847 GLuint *d = ((GLuint *) dst);
848 GLuint i;
849 for (i = 0; i < n; i++) {
850 GLuint s = src[i];
851 GLuint z = d[i] & 0xffffff00;
852 d[i] = z | s;
853 }
854 }
855 break;
856 case MESA_FORMAT_Z24_UNORM_S8_UINT:
857 {
858 /* don't disturb the Z values */
859 GLuint *d = ((GLuint *) dst);
860 GLuint i;
861 for (i = 0; i < n; i++) {
862 GLuint s = src[i] << 24;
863 GLuint z = d[i] & 0xffffff;
864 d[i] = s | z;
865 }
866 }
867 break;
868 case MESA_FORMAT_S_UINT8:
869 memcpy(dst, src, n * sizeof(GLubyte));
870 break;
871 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
872 {
873 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
874 GLuint i;
875 for (i = 0; i < n; i++) {
876 d[i].x24s8 = src[i];
877 }
878 }
879 break;
880 default:
881 _mesa_problem(NULL, "unexpected format in _mesa_pack_ubyte_stencil_row()");
882 }
883 }
884
885
886 /**
887 * Incoming Z/stencil values are always in uint_24_8 format.
888 */
889 void
890 _mesa_pack_uint_24_8_depth_stencil_row(mesa_format format, GLuint n,
891 const GLuint *src, void *dst)
892 {
893 switch (format) {
894 case MESA_FORMAT_S8_UINT_Z24_UNORM:
895 memcpy(dst, src, n * sizeof(GLuint));
896 break;
897 case MESA_FORMAT_Z24_UNORM_S8_UINT:
898 {
899 GLuint *d = ((GLuint *) dst);
900 GLuint i;
901 for (i = 0; i < n; i++) {
902 GLuint s = src[i] << 24;
903 GLuint z = src[i] >> 8;
904 d[i] = s | z;
905 }
906 }
907 break;
908 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
909 {
910 const GLdouble scale = 1.0 / (GLdouble) 0xffffff;
911 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
912 GLuint i;
913 for (i = 0; i < n; i++) {
914 GLfloat z = (GLfloat) ((src[i] >> 8) * scale);
915 d[i].z = z;
916 d[i].x24s8 = src[i];
917 }
918 }
919 break;
920 default:
921 _mesa_problem(NULL, "bad format %s in _mesa_pack_ubyte_s_row",
922 _mesa_get_format_name(format));
923 return;
924 }
925 }
926
927
928
929 /**
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.
932 */
933 void
934 _mesa_pack_colormask(mesa_format format, const GLubyte colorMask[4], void *dst)
935 {
936 GLfloat maskColor[4];
937
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);
947 break;
948 case GL_SIGNED_NORMALIZED:
949 case GL_FLOAT:
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.
957 */
958 {
959 GLuint bits = _mesa_get_format_max_bits(format); /* bits per chan */
960 GLuint bytes = _mesa_get_format_bytes(format);
961 GLuint i;
962
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);
970
971 /* fix-up the dst channels by converting non-zero values to ~0 */
972 if (bits == 8) {
973 GLubyte *d = (GLubyte *) dst;
974 for (i = 0; i < bytes; i++) {
975 d[i] = d[i] ? 0xff : 0x0;
976 }
977 }
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;
982 }
983 }
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;
988 }
989 }
990 else {
991 _mesa_problem(NULL, "unexpected size in _mesa_pack_colormask()");
992 return;
993 }
994 }
995 break;
996 default:
997 _mesa_problem(NULL, "unexpected format data type in gen_color_mask()");
998 return;
999 }
1000 }
1001 """
1002
1003 template = Template(string);
1004
1005 print(template.render(argv = argv[0:]))