mesa: Autogenerate most of format_pack.c
[mesa.git] / src / mesa / main / format_pack.py
1 #!/usr/bin/env python
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 "colormac.h"
47 #include "format_pack.h"
48 #include "format_utils.h"
49 #include "macros.h"
50 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
51 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
52 #include "util/format_srgb.h"
53
54 #define UNPACK(SRC, OFFSET, BITS) (((SRC) >> (OFFSET)) & MAX_UINT(BITS))
55 #define PACK(SRC, OFFSET, BITS) (((SRC) & MAX_UINT(BITS)) << (OFFSET))
56
57 <%
58 import format_parser as parser
59
60 formats = parser.parse(argv[1])
61
62 rgb_formats = []
63 for f in formats:
64 if f.name == 'MESA_FORMAT_NONE':
65 continue
66 if f.colorspace not in ('rgb', 'srgb'):
67 continue
68
69 rgb_formats.append(f)
70 %>
71
72 /* ubyte packing functions */
73
74 %for f in rgb_formats:
75 %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
76 <% continue %>
77 %elif f.is_compressed():
78 <% continue %>
79 %endif
80
81 static inline void
82 pack_ubyte_${f.short_name()}(const GLubyte src[4], void *dst)
83 {
84 %for (i, c) in enumerate(f.channels):
85 <% i = f.swizzle.inverse()[i] %>
86 %if c.type == 'x':
87 <% continue %>
88 %endif
89
90 ${c.datatype()} ${c.name} =
91 %if not f.is_normalized() and f.is_int():
92 %if c.type == parser.SIGNED:
93 _mesa_unsigned_to_signed(src[${i}], ${c.size});
94 %else:
95 _mesa_unsigned_to_unsigned(src[${i}], ${c.size});
96 %endif
97 %elif c.type == parser.UNSIGNED:
98 %if f.colorspace == 'srgb' and c.name in 'rgb':
99 <% assert c.size == 8 %>
100 util_format_linear_to_srgb_8unorm(src[${i}]);
101 %else:
102 _mesa_unorm_to_unorm(src[${i}], 8, ${c.size});
103 %endif
104 %elif c.type == parser.SIGNED:
105 _mesa_unorm_to_snorm(src[${i}], 8, ${c.size});
106 %elif c.type == parser.FLOAT:
107 %if c.size == 32:
108 _mesa_unorm_to_float(src[${i}], 8);
109 %elif c.size == 16:
110 _mesa_unorm_to_half(src[${i}], 8);
111 %else:
112 <% assert False %>
113 %endif
114 %else:
115 <% assert False %>
116 %endif
117 %endfor
118
119 %if f.layout == parser.ARRAY:
120 ${f.datatype()} *d = (${f.datatype()} *)dst;
121 %for (i, c) in enumerate(f.channels):
122 %if c.type == 'x':
123 <% continue %>
124 %endif
125 d[${i}] = ${c.name};
126 %endfor
127 %elif f.layout == parser.PACKED:
128 ${f.datatype()} d = 0;
129 %for (i, c) in enumerate(f.channels):
130 %if c.type == 'x':
131 <% continue %>
132 %endif
133 d |= PACK(${c.name}, ${c.shift}, ${c.size});
134 %endfor
135 (*(${f.datatype()} *)dst) = d;
136 %else:
137 <% assert False %>
138 %endif
139 }
140 %endfor
141
142 static inline void
143 pack_ubyte_r9g9b9e5_float(const GLubyte src[4], void *dst)
144 {
145 GLuint *d = (GLuint *) dst;
146 GLfloat rgb[3];
147 rgb[0] = _mesa_unorm_to_float(src[RCOMP], 8);
148 rgb[1] = _mesa_unorm_to_float(src[GCOMP], 8);
149 rgb[2] = _mesa_unorm_to_float(src[BCOMP], 8);
150 *d = float3_to_rgb9e5(rgb);
151 }
152
153 static inline void
154 pack_ubyte_r11g11b10_float(const GLubyte src[4], void *dst)
155 {
156 GLuint *d = (GLuint *) dst;
157 GLfloat rgb[3];
158 rgb[0] = _mesa_unorm_to_float(src[RCOMP], 8);
159 rgb[1] = _mesa_unorm_to_float(src[GCOMP], 8);
160 rgb[2] = _mesa_unorm_to_float(src[BCOMP], 8);
161 *d = float3_to_r11g11b10f(rgb);
162 }
163
164
165 /* float packing functions */
166
167 %for f in rgb_formats:
168 %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
169 <% continue %>
170 %elif f.is_int() and not f.is_normalized():
171 <% continue %>
172 %elif f.is_compressed():
173 <% continue %>
174 %endif
175
176 static inline void
177 pack_float_${f.short_name()}(const GLfloat 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.UNSIGNED:
187 %if f.colorspace == 'srgb' and c.name in 'rgb':
188 <% assert c.size == 8 %>
189 util_format_linear_float_to_srgb_8unorm(src[${i}]);
190 %else:
191 _mesa_float_to_unorm(src[${i}], ${c.size});
192 %endif
193 %elif c.type == parser.SIGNED:
194 _mesa_float_to_snorm(src[${i}], ${c.size});
195 %elif c.type == parser.FLOAT:
196 %if c.size == 32:
197 src[${i}];
198 %elif c.size == 16:
199 _mesa_float_to_half(src[${i}]);
200 %else:
201 <% assert False %>
202 %endif
203 %else:
204 <% assert False %>
205 %endif
206 %endfor
207
208 %if f.layout == parser.ARRAY:
209 ${f.datatype()} *d = (${f.datatype()} *)dst;
210 %for (i, c) in enumerate(f.channels):
211 %if c.type == 'x':
212 <% continue %>
213 %endif
214 d[${i}] = ${c.name};
215 %endfor
216 %elif f.layout == parser.PACKED:
217 ${f.datatype()} d = 0;
218 %for (i, c) in enumerate(f.channels):
219 %if c.type == 'x':
220 <% continue %>
221 %endif
222 d |= PACK(${c.name}, ${c.shift}, ${c.size});
223 %endfor
224 (*(${f.datatype()} *)dst) = d;
225 %else:
226 <% assert False %>
227 %endif
228 }
229 %endfor
230
231 static inline void
232 pack_float_r9g9b9e5_float(const GLfloat src[4], void *dst)
233 {
234 GLuint *d = (GLuint *) dst;
235 *d = float3_to_rgb9e5(src);
236 }
237
238 static inline void
239 pack_float_r11g11b10_float(const GLfloat src[4], void *dst)
240 {
241 GLuint *d = (GLuint *) dst;
242 *d = float3_to_r11g11b10f(src);
243 }
244
245 /**
246 * Return a function that can pack a GLubyte rgba[4] color.
247 */
248 gl_pack_ubyte_rgba_func
249 _mesa_get_pack_ubyte_rgba_function(mesa_format format)
250 {
251 switch (format) {
252 %for f in rgb_formats:
253 %if f.is_compressed():
254 <% continue %>
255 %endif
256
257 case ${f.name}:
258 return pack_ubyte_${f.short_name()};
259 %endfor
260 default:
261 return NULL;
262 }
263 }
264
265 /**
266 * Return a function that can pack a GLfloat rgba[4] color.
267 */
268 gl_pack_float_rgba_func
269 _mesa_get_pack_float_rgba_function(mesa_format format)
270 {
271 switch (format) {
272 %for f in rgb_formats:
273 %if f.is_compressed():
274 <% continue %>
275 %elif f.is_int() and not f.is_normalized():
276 <% continue %>
277 %endif
278
279 case ${f.name}:
280 return pack_float_${f.short_name()};
281 %endfor
282 default:
283 return NULL;
284 }
285 }
286
287 /**
288 * Pack a row of GLubyte rgba[4] values to the destination.
289 */
290 void
291 _mesa_pack_ubyte_rgba_row(mesa_format format, GLuint n,
292 const GLubyte src[][4], void *dst)
293 {
294 GLuint i;
295 GLubyte *d = dst;
296
297 switch (format) {
298 %for f in rgb_formats:
299 %if f.is_compressed():
300 <% continue %>
301 %endif
302
303 case ${f.name}:
304 for (i = 0; i < n; ++i) {
305 pack_ubyte_${f.short_name()}(src[i], d);
306 d += ${f.block_size() / 8};
307 }
308 break;
309 %endfor
310 default:
311 assert(!"Invalid format");
312 }
313 }
314
315 /**
316 * Pack a row of GLfloat rgba[4] values to the destination.
317 */
318 void
319 _mesa_pack_float_rgba_row(mesa_format format, GLuint n,
320 const GLfloat src[][4], void *dst)
321 {
322 GLuint i;
323 GLubyte *d = dst;
324
325 switch (format) {
326 %for f in rgb_formats:
327 %if f.is_compressed():
328 <% continue %>
329 %elif f.is_int() and not f.is_normalized():
330 <% continue %>
331 %endif
332
333 case ${f.name}:
334 for (i = 0; i < n; ++i) {
335 pack_float_${f.short_name()}(src[i], d);
336 d += ${f.block_size() / 8};
337 }
338 break;
339 %endfor
340 default:
341 assert(!"Invalid format");
342 }
343 }
344
345 /**
346 * Pack a 2D image of ubyte RGBA pixels in the given format.
347 * \param srcRowStride source image row stride in bytes
348 * \param dstRowStride destination image row stride in bytes
349 */
350 void
351 _mesa_pack_ubyte_rgba_rect(mesa_format format, GLuint width, GLuint height,
352 const GLubyte *src, GLint srcRowStride,
353 void *dst, GLint dstRowStride)
354 {
355 GLubyte *dstUB = dst;
356 GLuint i;
357
358 if (srcRowStride == width * 4 * sizeof(GLubyte) &&
359 dstRowStride == _mesa_format_row_stride(format, width)) {
360 /* do whole image at once */
361 _mesa_pack_ubyte_rgba_row(format, width * height,
362 (const GLubyte (*)[4]) src, dst);
363 }
364 else {
365 /* row by row */
366 for (i = 0; i < height; i++) {
367 _mesa_pack_ubyte_rgba_row(format, width,
368 (const GLubyte (*)[4]) src, dstUB);
369 src += srcRowStride;
370 dstUB += dstRowStride;
371 }
372 }
373 }
374
375
376 /** Helper struct for MESA_FORMAT_Z32_FLOAT_S8X24_UINT */
377 struct z32f_x24s8
378 {
379 float z;
380 uint32_t x24s8;
381 };
382
383
384 /**
385 ** Pack float Z pixels
386 **/
387
388 static void
389 pack_float_S8_UINT_Z24_UNORM(const GLfloat *src, void *dst)
390 {
391 /* don't disturb the stencil values */
392 GLuint *d = ((GLuint *) dst);
393 const GLdouble scale = (GLdouble) 0xffffff;
394 GLuint s = *d & 0xff;
395 GLuint z = (GLuint) (*src * scale);
396 assert(z <= 0xffffff);
397 *d = (z << 8) | s;
398 }
399
400 static void
401 pack_float_Z24_UNORM_S8_UINT(const GLfloat *src, void *dst)
402 {
403 /* don't disturb the stencil values */
404 GLuint *d = ((GLuint *) dst);
405 const GLdouble scale = (GLdouble) 0xffffff;
406 GLuint s = *d & 0xff000000;
407 GLuint z = (GLuint) (*src * scale);
408 assert(z <= 0xffffff);
409 *d = s | z;
410 }
411
412 static void
413 pack_float_Z_UNORM16(const GLfloat *src, void *dst)
414 {
415 GLushort *d = ((GLushort *) dst);
416 const GLfloat scale = (GLfloat) 0xffff;
417 *d = (GLushort) (*src * scale);
418 }
419
420 static void
421 pack_float_Z_UNORM32(const GLfloat *src, void *dst)
422 {
423 GLuint *d = ((GLuint *) dst);
424 const GLdouble scale = (GLdouble) 0xffffffff;
425 *d = (GLuint) (*src * scale);
426 }
427
428 static void
429 pack_float_Z_FLOAT32(const GLfloat *src, void *dst)
430 {
431 GLfloat *d = (GLfloat *) dst;
432 *d = *src;
433 }
434
435 gl_pack_float_z_func
436 _mesa_get_pack_float_z_func(mesa_format format)
437 {
438 switch (format) {
439 case MESA_FORMAT_S8_UINT_Z24_UNORM:
440 case MESA_FORMAT_X8_UINT_Z24_UNORM:
441 return pack_float_S8_UINT_Z24_UNORM;
442 case MESA_FORMAT_Z24_UNORM_S8_UINT:
443 case MESA_FORMAT_Z24_UNORM_X8_UINT:
444 return pack_float_Z24_UNORM_S8_UINT;
445 case MESA_FORMAT_Z_UNORM16:
446 return pack_float_Z_UNORM16;
447 case MESA_FORMAT_Z_UNORM32:
448 return pack_float_Z_UNORM32;
449 case MESA_FORMAT_Z_FLOAT32:
450 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
451 return pack_float_Z_FLOAT32;
452 default:
453 _mesa_problem(NULL,
454 "unexpected format in _mesa_get_pack_float_z_func()");
455 return NULL;
456 }
457 }
458
459
460
461 /**
462 ** Pack uint Z pixels. The incoming src value is always in
463 ** the range [0, 2^32-1].
464 **/
465
466 static void
467 pack_uint_S8_UINT_Z24_UNORM(const GLuint *src, void *dst)
468 {
469 /* don't disturb the stencil values */
470 GLuint *d = ((GLuint *) dst);
471 GLuint s = *d & 0xff;
472 GLuint z = *src & 0xffffff00;
473 *d = z | s;
474 }
475
476 static void
477 pack_uint_Z24_UNORM_S8_UINT(const GLuint *src, void *dst)
478 {
479 /* don't disturb the stencil values */
480 GLuint *d = ((GLuint *) dst);
481 GLuint s = *d & 0xff000000;
482 GLuint z = *src >> 8;
483 *d = s | z;
484 }
485
486 static void
487 pack_uint_Z_UNORM16(const GLuint *src, void *dst)
488 {
489 GLushort *d = ((GLushort *) dst);
490 *d = *src >> 16;
491 }
492
493 static void
494 pack_uint_Z_UNORM32(const GLuint *src, void *dst)
495 {
496 GLuint *d = ((GLuint *) dst);
497 *d = *src;
498 }
499
500 static void
501 pack_uint_Z_FLOAT32(const GLuint *src, void *dst)
502 {
503 GLuint *d = ((GLuint *) dst);
504 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
505 *d = (GLuint) (*src * scale);
506 assert(*d >= 0.0f);
507 assert(*d <= 1.0f);
508 }
509
510 static void
511 pack_uint_Z_FLOAT32_X24S8(const GLuint *src, void *dst)
512 {
513 GLfloat *d = ((GLfloat *) dst);
514 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
515 *d = (GLfloat) (*src * scale);
516 assert(*d >= 0.0f);
517 assert(*d <= 1.0f);
518 }
519
520 gl_pack_uint_z_func
521 _mesa_get_pack_uint_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_uint_S8_UINT_Z24_UNORM;
527 case MESA_FORMAT_Z24_UNORM_S8_UINT:
528 case MESA_FORMAT_Z24_UNORM_X8_UINT:
529 return pack_uint_Z24_UNORM_S8_UINT;
530 case MESA_FORMAT_Z_UNORM16:
531 return pack_uint_Z_UNORM16;
532 case MESA_FORMAT_Z_UNORM32:
533 return pack_uint_Z_UNORM32;
534 case MESA_FORMAT_Z_FLOAT32:
535 return pack_uint_Z_FLOAT32;
536 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
537 return pack_uint_Z_FLOAT32_X24S8;
538 default:
539 _mesa_problem(NULL, "unexpected format in _mesa_get_pack_uint_z_func()");
540 return NULL;
541 }
542 }
543
544
545 /**
546 ** Pack ubyte stencil pixels
547 **/
548
549 static void
550 pack_ubyte_stencil_Z24_S8(const GLubyte *src, void *dst)
551 {
552 /* don't disturb the Z values */
553 GLuint *d = ((GLuint *) dst);
554 GLuint s = *src;
555 GLuint z = *d & 0xffffff00;
556 *d = z | s;
557 }
558
559 static void
560 pack_ubyte_stencil_S8_Z24(const GLubyte *src, void *dst)
561 {
562 /* don't disturb the Z values */
563 GLuint *d = ((GLuint *) dst);
564 GLuint s = *src << 24;
565 GLuint z = *d & 0xffffff;
566 *d = s | z;
567 }
568
569 static void
570 pack_ubyte_stencil_S8(const GLubyte *src, void *dst)
571 {
572 GLubyte *d = (GLubyte *) dst;
573 *d = *src;
574 }
575
576 static void
577 pack_ubyte_stencil_Z32_FLOAT_X24S8(const GLubyte *src, void *dst)
578 {
579 GLfloat *d = ((GLfloat *) dst);
580 d[1] = *src;
581 }
582
583
584 gl_pack_ubyte_stencil_func
585 _mesa_get_pack_ubyte_stencil_func(mesa_format format)
586 {
587 switch (format) {
588 case MESA_FORMAT_S8_UINT_Z24_UNORM:
589 return pack_ubyte_stencil_Z24_S8;
590 case MESA_FORMAT_Z24_UNORM_S8_UINT:
591 return pack_ubyte_stencil_S8_Z24;
592 case MESA_FORMAT_S_UINT8:
593 return pack_ubyte_stencil_S8;
594 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
595 return pack_ubyte_stencil_Z32_FLOAT_X24S8;
596 default:
597 _mesa_problem(NULL,
598 "unexpected format in _mesa_pack_ubyte_stencil_func()");
599 return NULL;
600 }
601 }
602
603
604
605 void
606 _mesa_pack_float_z_row(mesa_format format, GLuint n,
607 const GLfloat *src, void *dst)
608 {
609 switch (format) {
610 case MESA_FORMAT_S8_UINT_Z24_UNORM:
611 case MESA_FORMAT_X8_UINT_Z24_UNORM:
612 {
613 /* don't disturb the stencil values */
614 GLuint *d = ((GLuint *) dst);
615 const GLdouble scale = (GLdouble) 0xffffff;
616 GLuint i;
617 for (i = 0; i < n; i++) {
618 GLuint s = d[i] & 0xff;
619 GLuint z = (GLuint) (src[i] * scale);
620 assert(z <= 0xffffff);
621 d[i] = (z << 8) | s;
622 }
623 }
624 break;
625 case MESA_FORMAT_Z24_UNORM_S8_UINT:
626 case MESA_FORMAT_Z24_UNORM_X8_UINT:
627 {
628 /* don't disturb the stencil values */
629 GLuint *d = ((GLuint *) dst);
630 const GLdouble scale = (GLdouble) 0xffffff;
631 GLuint i;
632 for (i = 0; i < n; i++) {
633 GLuint s = d[i] & 0xff000000;
634 GLuint z = (GLuint) (src[i] * scale);
635 assert(z <= 0xffffff);
636 d[i] = s | z;
637 }
638 }
639 break;
640 case MESA_FORMAT_Z_UNORM16:
641 {
642 GLushort *d = ((GLushort *) dst);
643 const GLfloat scale = (GLfloat) 0xffff;
644 GLuint i;
645 for (i = 0; i < n; i++) {
646 d[i] = (GLushort) (src[i] * scale);
647 }
648 }
649 break;
650 case MESA_FORMAT_Z_UNORM32:
651 {
652 GLuint *d = ((GLuint *) dst);
653 const GLdouble scale = (GLdouble) 0xffffffff;
654 GLuint i;
655 for (i = 0; i < n; i++) {
656 d[i] = (GLuint) (src[i] * scale);
657 }
658 }
659 break;
660 case MESA_FORMAT_Z_FLOAT32:
661 memcpy(dst, src, n * sizeof(GLfloat));
662 break;
663 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
664 {
665 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
666 GLuint i;
667 for (i = 0; i < n; i++) {
668 d[i].z = src[i];
669 }
670 }
671 break;
672 default:
673 _mesa_problem(NULL, "unexpected format in _mesa_pack_float_z_row()");
674 }
675 }
676
677
678 /**
679 * The incoming Z values are always in the range [0, 0xffffffff].
680 */
681 void
682 _mesa_pack_uint_z_row(mesa_format format, GLuint n,
683 const GLuint *src, void *dst)
684 {
685 switch (format) {
686 case MESA_FORMAT_S8_UINT_Z24_UNORM:
687 case MESA_FORMAT_X8_UINT_Z24_UNORM:
688 {
689 /* don't disturb the stencil values */
690 GLuint *d = ((GLuint *) dst);
691 GLuint i;
692 for (i = 0; i < n; i++) {
693 GLuint s = d[i] & 0xff;
694 GLuint z = src[i] & 0xffffff00;
695 d[i] = z | s;
696 }
697 }
698 break;
699 case MESA_FORMAT_Z24_UNORM_S8_UINT:
700 case MESA_FORMAT_Z24_UNORM_X8_UINT:
701 {
702 /* don't disturb the stencil values */
703 GLuint *d = ((GLuint *) dst);
704 GLuint i;
705 for (i = 0; i < n; i++) {
706 GLuint s = d[i] & 0xff000000;
707 GLuint z = src[i] >> 8;
708 d[i] = s | z;
709 }
710 }
711 break;
712 case MESA_FORMAT_Z_UNORM16:
713 {
714 GLushort *d = ((GLushort *) dst);
715 GLuint i;
716 for (i = 0; i < n; i++) {
717 d[i] = src[i] >> 16;
718 }
719 }
720 break;
721 case MESA_FORMAT_Z_UNORM32:
722 memcpy(dst, src, n * sizeof(GLfloat));
723 break;
724 case MESA_FORMAT_Z_FLOAT32:
725 {
726 GLuint *d = ((GLuint *) dst);
727 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
728 GLuint i;
729 for (i = 0; i < n; i++) {
730 d[i] = (GLuint) (src[i] * scale);
731 assert(d[i] >= 0.0f);
732 assert(d[i] <= 1.0f);
733 }
734 }
735 break;
736 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
737 {
738 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
739 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
740 GLuint i;
741 for (i = 0; i < n; i++) {
742 d[i].z = (GLfloat) (src[i] * scale);
743 assert(d[i].z >= 0.0f);
744 assert(d[i].z <= 1.0f);
745 }
746 }
747 break;
748 default:
749 _mesa_problem(NULL, "unexpected format in _mesa_pack_uint_z_row()");
750 }
751 }
752
753
754 void
755 _mesa_pack_ubyte_stencil_row(mesa_format format, GLuint n,
756 const GLubyte *src, void *dst)
757 {
758 switch (format) {
759 case MESA_FORMAT_S8_UINT_Z24_UNORM:
760 {
761 /* don't disturb the Z values */
762 GLuint *d = ((GLuint *) dst);
763 GLuint i;
764 for (i = 0; i < n; i++) {
765 GLuint s = src[i];
766 GLuint z = d[i] & 0xffffff00;
767 d[i] = z | s;
768 }
769 }
770 break;
771 case MESA_FORMAT_Z24_UNORM_S8_UINT:
772 {
773 /* don't disturb the Z values */
774 GLuint *d = ((GLuint *) dst);
775 GLuint i;
776 for (i = 0; i < n; i++) {
777 GLuint s = src[i] << 24;
778 GLuint z = d[i] & 0xffffff;
779 d[i] = s | z;
780 }
781 }
782 break;
783 case MESA_FORMAT_S_UINT8:
784 memcpy(dst, src, n * sizeof(GLubyte));
785 break;
786 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
787 {
788 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
789 GLuint i;
790 for (i = 0; i < n; i++) {
791 d[i].x24s8 = src[i];
792 }
793 }
794 break;
795 default:
796 _mesa_problem(NULL, "unexpected format in _mesa_pack_ubyte_stencil_row()");
797 }
798 }
799
800
801 /**
802 * Incoming Z/stencil values are always in uint_24_8 format.
803 */
804 void
805 _mesa_pack_uint_24_8_depth_stencil_row(mesa_format format, GLuint n,
806 const GLuint *src, void *dst)
807 {
808 switch (format) {
809 case MESA_FORMAT_S8_UINT_Z24_UNORM:
810 memcpy(dst, src, n * sizeof(GLuint));
811 break;
812 case MESA_FORMAT_Z24_UNORM_S8_UINT:
813 {
814 GLuint *d = ((GLuint *) dst);
815 GLuint i;
816 for (i = 0; i < n; i++) {
817 GLuint s = src[i] << 24;
818 GLuint z = src[i] >> 8;
819 d[i] = s | z;
820 }
821 }
822 break;
823 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
824 {
825 const GLdouble scale = 1.0 / (GLdouble) 0xffffff;
826 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
827 GLuint i;
828 for (i = 0; i < n; i++) {
829 GLfloat z = (GLfloat) ((src[i] >> 8) * scale);
830 d[i].z = z;
831 d[i].x24s8 = src[i];
832 }
833 }
834 break;
835 default:
836 _mesa_problem(NULL, "bad format %s in _mesa_pack_ubyte_s_row",
837 _mesa_get_format_name(format));
838 return;
839 }
840 }
841
842
843
844 /**
845 * Convert a boolean color mask to a packed color where each channel of
846 * the packed value at dst will be 0 or ~0 depending on the colorMask.
847 */
848 void
849 _mesa_pack_colormask(mesa_format format, const GLubyte colorMask[4], void *dst)
850 {
851 GLfloat maskColor[4];
852
853 switch (_mesa_get_format_datatype(format)) {
854 case GL_UNSIGNED_NORMALIZED:
855 /* simple: 1.0 will convert to ~0 in the right bit positions */
856 maskColor[0] = colorMask[0] ? 1.0f : 0.0f;
857 maskColor[1] = colorMask[1] ? 1.0f : 0.0f;
858 maskColor[2] = colorMask[2] ? 1.0f : 0.0f;
859 maskColor[3] = colorMask[3] ? 1.0f : 0.0f;
860 _mesa_pack_float_rgba_row(format, 1,
861 (const GLfloat (*)[4]) maskColor, dst);
862 break;
863 case GL_SIGNED_NORMALIZED:
864 case GL_FLOAT:
865 /* These formats are harder because it's hard to know the floating
866 * point values that will convert to ~0 for each color channel's bits.
867 * This solution just generates a non-zero value for each color channel
868 * then fixes up the non-zero values to be ~0.
869 * Note: we'll need to add special case code if we ever have to deal
870 * with formats with unequal color channel sizes, like R11_G11_B10.
871 * We issue a warning below for channel sizes other than 8,16,32.
872 */
873 {
874 GLuint bits = _mesa_get_format_max_bits(format); /* bits per chan */
875 GLuint bytes = _mesa_get_format_bytes(format);
876 GLuint i;
877
878 /* this should put non-zero values into the channels of dst */
879 maskColor[0] = colorMask[0] ? -1.0f : 0.0f;
880 maskColor[1] = colorMask[1] ? -1.0f : 0.0f;
881 maskColor[2] = colorMask[2] ? -1.0f : 0.0f;
882 maskColor[3] = colorMask[3] ? -1.0f : 0.0f;
883 _mesa_pack_float_rgba_row(format, 1,
884 (const GLfloat (*)[4]) maskColor, dst);
885
886 /* fix-up the dst channels by converting non-zero values to ~0 */
887 if (bits == 8) {
888 GLubyte *d = (GLubyte *) dst;
889 for (i = 0; i < bytes; i++) {
890 d[i] = d[i] ? 0xff : 0x0;
891 }
892 }
893 else if (bits == 16) {
894 GLushort *d = (GLushort *) dst;
895 for (i = 0; i < bytes / 2; i++) {
896 d[i] = d[i] ? 0xffff : 0x0;
897 }
898 }
899 else if (bits == 32) {
900 GLuint *d = (GLuint *) dst;
901 for (i = 0; i < bytes / 4; i++) {
902 d[i] = d[i] ? 0xffffffffU : 0x0;
903 }
904 }
905 else {
906 _mesa_problem(NULL, "unexpected size in _mesa_pack_colormask()");
907 return;
908 }
909 }
910 break;
911 default:
912 _mesa_problem(NULL, "unexpected format data type in gen_color_mask()");
913 return;
914 }
915 }
916 """
917
918 template = Template(string);
919
920 print template.render(argv = argv[0:])