mesa: Remove _mesa_pack_int_rgba_row() and auxiliary functions
[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 /* uint packing functions */
165
166 %for f in rgb_formats:
167 %if not f.is_int():
168 <% continue %>
169 %elif f.is_normalized():
170 <% continue %>
171 %elif f.is_compressed():
172 <% continue %>
173 %endif
174
175 static inline void
176 pack_uint_${f.short_name()}(const GLuint src[4], void *dst)
177 {
178 %for (i, c) in enumerate(f.channels):
179 <% i = f.swizzle.inverse()[i] %>
180 %if c.type == 'x':
181 <% continue %>
182 %endif
183
184 ${c.datatype()} ${c.name} =
185 %if c.type == parser.SIGNED:
186 _mesa_signed_to_signed(src[${i}], ${c.size});
187 %elif c.type == parser.UNSIGNED:
188 _mesa_unsigned_to_unsigned(src[${i}], ${c.size});
189 %else:
190 assert(!"Invalid type: only integer types are allowed");
191 %endif
192 %endfor
193
194 %if f.layout == parser.ARRAY:
195 ${f.datatype()} *d = (${f.datatype()} *)dst;
196 %for (i, c) in enumerate(f.channels):
197 %if c.type == 'x':
198 <% continue %>
199 %endif
200 d[${i}] = ${c.name};
201 %endfor
202 %elif f.layout == parser.PACKED:
203 ${f.datatype()} d = 0;
204 %for (i, c) in enumerate(f.channels):
205 %if c.type == 'x':
206 <% continue %>
207 %endif
208 d |= PACK(${c.name}, ${c.shift}, ${c.size});
209 %endfor
210 (*(${f.datatype()} *)dst) = d;
211 %else:
212 <% assert False %>
213 %endif
214 }
215 %endfor
216
217 /* float packing functions */
218
219 %for f in rgb_formats:
220 %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
221 <% continue %>
222 %elif f.is_int() and not f.is_normalized():
223 <% continue %>
224 %elif f.is_compressed():
225 <% continue %>
226 %endif
227
228 static inline void
229 pack_float_${f.short_name()}(const GLfloat src[4], void *dst)
230 {
231 %for (i, c) in enumerate(f.channels):
232 <% i = f.swizzle.inverse()[i] %>
233 %if c.type == 'x':
234 <% continue %>
235 %endif
236
237 ${c.datatype()} ${c.name} =
238 %if c.type == parser.UNSIGNED:
239 %if f.colorspace == 'srgb' and c.name in 'rgb':
240 <% assert c.size == 8 %>
241 util_format_linear_float_to_srgb_8unorm(src[${i}]);
242 %else:
243 _mesa_float_to_unorm(src[${i}], ${c.size});
244 %endif
245 %elif c.type == parser.SIGNED:
246 _mesa_float_to_snorm(src[${i}], ${c.size});
247 %elif c.type == parser.FLOAT:
248 %if c.size == 32:
249 src[${i}];
250 %elif c.size == 16:
251 _mesa_float_to_half(src[${i}]);
252 %else:
253 <% assert False %>
254 %endif
255 %else:
256 <% assert False %>
257 %endif
258 %endfor
259
260 %if f.layout == parser.ARRAY:
261 ${f.datatype()} *d = (${f.datatype()} *)dst;
262 %for (i, c) in enumerate(f.channels):
263 %if c.type == 'x':
264 <% continue %>
265 %endif
266 d[${i}] = ${c.name};
267 %endfor
268 %elif f.layout == parser.PACKED:
269 ${f.datatype()} d = 0;
270 %for (i, c) in enumerate(f.channels):
271 %if c.type == 'x':
272 <% continue %>
273 %endif
274 d |= PACK(${c.name}, ${c.shift}, ${c.size});
275 %endfor
276 (*(${f.datatype()} *)dst) = d;
277 %else:
278 <% assert False %>
279 %endif
280 }
281 %endfor
282
283 static inline void
284 pack_float_r9g9b9e5_float(const GLfloat src[4], void *dst)
285 {
286 GLuint *d = (GLuint *) dst;
287 *d = float3_to_rgb9e5(src);
288 }
289
290 static inline void
291 pack_float_r11g11b10_float(const GLfloat src[4], void *dst)
292 {
293 GLuint *d = (GLuint *) dst;
294 *d = float3_to_r11g11b10f(src);
295 }
296
297 /**
298 * Return a function that can pack a GLubyte rgba[4] color.
299 */
300 gl_pack_ubyte_rgba_func
301 _mesa_get_pack_ubyte_rgba_function(mesa_format format)
302 {
303 switch (format) {
304 %for f in rgb_formats:
305 %if f.is_compressed():
306 <% continue %>
307 %endif
308
309 case ${f.name}:
310 return pack_ubyte_${f.short_name()};
311 %endfor
312 default:
313 return NULL;
314 }
315 }
316
317 /**
318 * Return a function that can pack a GLfloat rgba[4] color.
319 */
320 gl_pack_float_rgba_func
321 _mesa_get_pack_float_rgba_function(mesa_format format)
322 {
323 switch (format) {
324 %for f in rgb_formats:
325 %if f.is_compressed():
326 <% continue %>
327 %elif f.is_int() and not f.is_normalized():
328 <% continue %>
329 %endif
330
331 case ${f.name}:
332 return pack_float_${f.short_name()};
333 %endfor
334 default:
335 return NULL;
336 }
337 }
338
339 /**
340 * Pack a row of GLubyte rgba[4] values to the destination.
341 */
342 void
343 _mesa_pack_ubyte_rgba_row(mesa_format format, GLuint n,
344 const GLubyte src[][4], void *dst)
345 {
346 GLuint i;
347 GLubyte *d = dst;
348
349 switch (format) {
350 %for f in rgb_formats:
351 %if f.is_compressed():
352 <% continue %>
353 %endif
354
355 case ${f.name}:
356 for (i = 0; i < n; ++i) {
357 pack_ubyte_${f.short_name()}(src[i], d);
358 d += ${f.block_size() / 8};
359 }
360 break;
361 %endfor
362 default:
363 assert(!"Invalid format");
364 }
365 }
366
367 /**
368 * Pack a row of GLuint rgba[4] values to the destination.
369 */
370 void
371 _mesa_pack_uint_rgba_row(mesa_format format, GLuint n,
372 const GLuint src[][4], void *dst)
373 {
374 GLuint i;
375 GLubyte *d = dst;
376
377 switch (format) {
378 %for f in rgb_formats:
379 %if not f.is_int():
380 <% continue %>
381 %elif f.is_normalized():
382 <% continue %>
383 %elif f.is_compressed():
384 <% continue %>
385 %endif
386
387 case ${f.name}:
388 for (i = 0; i < n; ++i) {
389 pack_uint_${f.short_name()}(src[i], d);
390 d += ${f.block_size() / 8};
391 }
392 break;
393 %endfor
394 default:
395 assert(!"Invalid format");
396 }
397 }
398
399 /**
400 * Pack a row of GLfloat rgba[4] values to the destination.
401 */
402 void
403 _mesa_pack_float_rgba_row(mesa_format format, GLuint n,
404 const GLfloat src[][4], void *dst)
405 {
406 GLuint i;
407 GLubyte *d = dst;
408
409 switch (format) {
410 %for f in rgb_formats:
411 %if f.is_compressed():
412 <% continue %>
413 %elif f.is_int() and not f.is_normalized():
414 <% continue %>
415 %endif
416
417 case ${f.name}:
418 for (i = 0; i < n; ++i) {
419 pack_float_${f.short_name()}(src[i], d);
420 d += ${f.block_size() / 8};
421 }
422 break;
423 %endfor
424 default:
425 assert(!"Invalid format");
426 }
427 }
428
429 /**
430 * Pack a 2D image of ubyte RGBA pixels in the given format.
431 * \param srcRowStride source image row stride in bytes
432 * \param dstRowStride destination image row stride in bytes
433 */
434 void
435 _mesa_pack_ubyte_rgba_rect(mesa_format format, GLuint width, GLuint height,
436 const GLubyte *src, GLint srcRowStride,
437 void *dst, GLint dstRowStride)
438 {
439 GLubyte *dstUB = dst;
440 GLuint i;
441
442 if (srcRowStride == width * 4 * sizeof(GLubyte) &&
443 dstRowStride == _mesa_format_row_stride(format, width)) {
444 /* do whole image at once */
445 _mesa_pack_ubyte_rgba_row(format, width * height,
446 (const GLubyte (*)[4]) src, dst);
447 }
448 else {
449 /* row by row */
450 for (i = 0; i < height; i++) {
451 _mesa_pack_ubyte_rgba_row(format, width,
452 (const GLubyte (*)[4]) src, dstUB);
453 src += srcRowStride;
454 dstUB += dstRowStride;
455 }
456 }
457 }
458
459
460 /** Helper struct for MESA_FORMAT_Z32_FLOAT_S8X24_UINT */
461 struct z32f_x24s8
462 {
463 float z;
464 uint32_t x24s8;
465 };
466
467
468 /**
469 ** Pack float Z pixels
470 **/
471
472 static void
473 pack_float_S8_UINT_Z24_UNORM(const GLfloat *src, void *dst)
474 {
475 /* don't disturb the stencil values */
476 GLuint *d = ((GLuint *) dst);
477 const GLdouble scale = (GLdouble) 0xffffff;
478 GLuint s = *d & 0xff;
479 GLuint z = (GLuint) (*src * scale);
480 assert(z <= 0xffffff);
481 *d = (z << 8) | s;
482 }
483
484 static void
485 pack_float_Z24_UNORM_S8_UINT(const GLfloat *src, void *dst)
486 {
487 /* don't disturb the stencil values */
488 GLuint *d = ((GLuint *) dst);
489 const GLdouble scale = (GLdouble) 0xffffff;
490 GLuint s = *d & 0xff000000;
491 GLuint z = (GLuint) (*src * scale);
492 assert(z <= 0xffffff);
493 *d = s | z;
494 }
495
496 static void
497 pack_float_Z_UNORM16(const GLfloat *src, void *dst)
498 {
499 GLushort *d = ((GLushort *) dst);
500 const GLfloat scale = (GLfloat) 0xffff;
501 *d = (GLushort) (*src * scale);
502 }
503
504 static void
505 pack_float_Z_UNORM32(const GLfloat *src, void *dst)
506 {
507 GLuint *d = ((GLuint *) dst);
508 const GLdouble scale = (GLdouble) 0xffffffff;
509 *d = (GLuint) (*src * scale);
510 }
511
512 static void
513 pack_float_Z_FLOAT32(const GLfloat *src, void *dst)
514 {
515 GLfloat *d = (GLfloat *) dst;
516 *d = *src;
517 }
518
519 gl_pack_float_z_func
520 _mesa_get_pack_float_z_func(mesa_format format)
521 {
522 switch (format) {
523 case MESA_FORMAT_S8_UINT_Z24_UNORM:
524 case MESA_FORMAT_X8_UINT_Z24_UNORM:
525 return pack_float_S8_UINT_Z24_UNORM;
526 case MESA_FORMAT_Z24_UNORM_S8_UINT:
527 case MESA_FORMAT_Z24_UNORM_X8_UINT:
528 return pack_float_Z24_UNORM_S8_UINT;
529 case MESA_FORMAT_Z_UNORM16:
530 return pack_float_Z_UNORM16;
531 case MESA_FORMAT_Z_UNORM32:
532 return pack_float_Z_UNORM32;
533 case MESA_FORMAT_Z_FLOAT32:
534 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
535 return pack_float_Z_FLOAT32;
536 default:
537 _mesa_problem(NULL,
538 "unexpected format in _mesa_get_pack_float_z_func()");
539 return NULL;
540 }
541 }
542
543
544
545 /**
546 ** Pack uint Z pixels. The incoming src value is always in
547 ** the range [0, 2^32-1].
548 **/
549
550 static void
551 pack_uint_S8_UINT_Z24_UNORM(const GLuint *src, void *dst)
552 {
553 /* don't disturb the stencil values */
554 GLuint *d = ((GLuint *) dst);
555 GLuint s = *d & 0xff;
556 GLuint z = *src & 0xffffff00;
557 *d = z | s;
558 }
559
560 static void
561 pack_uint_Z24_UNORM_S8_UINT(const GLuint *src, void *dst)
562 {
563 /* don't disturb the stencil values */
564 GLuint *d = ((GLuint *) dst);
565 GLuint s = *d & 0xff000000;
566 GLuint z = *src >> 8;
567 *d = s | z;
568 }
569
570 static void
571 pack_uint_Z_UNORM16(const GLuint *src, void *dst)
572 {
573 GLushort *d = ((GLushort *) dst);
574 *d = *src >> 16;
575 }
576
577 static void
578 pack_uint_Z_UNORM32(const GLuint *src, void *dst)
579 {
580 GLuint *d = ((GLuint *) dst);
581 *d = *src;
582 }
583
584 static void
585 pack_uint_Z_FLOAT32(const GLuint *src, void *dst)
586 {
587 GLuint *d = ((GLuint *) dst);
588 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
589 *d = (GLuint) (*src * scale);
590 assert(*d >= 0.0f);
591 assert(*d <= 1.0f);
592 }
593
594 static void
595 pack_uint_Z_FLOAT32_X24S8(const GLuint *src, void *dst)
596 {
597 GLfloat *d = ((GLfloat *) dst);
598 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
599 *d = (GLfloat) (*src * scale);
600 assert(*d >= 0.0f);
601 assert(*d <= 1.0f);
602 }
603
604 gl_pack_uint_z_func
605 _mesa_get_pack_uint_z_func(mesa_format format)
606 {
607 switch (format) {
608 case MESA_FORMAT_S8_UINT_Z24_UNORM:
609 case MESA_FORMAT_X8_UINT_Z24_UNORM:
610 return pack_uint_S8_UINT_Z24_UNORM;
611 case MESA_FORMAT_Z24_UNORM_S8_UINT:
612 case MESA_FORMAT_Z24_UNORM_X8_UINT:
613 return pack_uint_Z24_UNORM_S8_UINT;
614 case MESA_FORMAT_Z_UNORM16:
615 return pack_uint_Z_UNORM16;
616 case MESA_FORMAT_Z_UNORM32:
617 return pack_uint_Z_UNORM32;
618 case MESA_FORMAT_Z_FLOAT32:
619 return pack_uint_Z_FLOAT32;
620 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
621 return pack_uint_Z_FLOAT32_X24S8;
622 default:
623 _mesa_problem(NULL, "unexpected format in _mesa_get_pack_uint_z_func()");
624 return NULL;
625 }
626 }
627
628
629 /**
630 ** Pack ubyte stencil pixels
631 **/
632
633 static void
634 pack_ubyte_stencil_Z24_S8(const GLubyte *src, void *dst)
635 {
636 /* don't disturb the Z values */
637 GLuint *d = ((GLuint *) dst);
638 GLuint s = *src;
639 GLuint z = *d & 0xffffff00;
640 *d = z | s;
641 }
642
643 static void
644 pack_ubyte_stencil_S8_Z24(const GLubyte *src, void *dst)
645 {
646 /* don't disturb the Z values */
647 GLuint *d = ((GLuint *) dst);
648 GLuint s = *src << 24;
649 GLuint z = *d & 0xffffff;
650 *d = s | z;
651 }
652
653 static void
654 pack_ubyte_stencil_S8(const GLubyte *src, void *dst)
655 {
656 GLubyte *d = (GLubyte *) dst;
657 *d = *src;
658 }
659
660 static void
661 pack_ubyte_stencil_Z32_FLOAT_X24S8(const GLubyte *src, void *dst)
662 {
663 GLfloat *d = ((GLfloat *) dst);
664 d[1] = *src;
665 }
666
667
668 gl_pack_ubyte_stencil_func
669 _mesa_get_pack_ubyte_stencil_func(mesa_format format)
670 {
671 switch (format) {
672 case MESA_FORMAT_S8_UINT_Z24_UNORM:
673 return pack_ubyte_stencil_Z24_S8;
674 case MESA_FORMAT_Z24_UNORM_S8_UINT:
675 return pack_ubyte_stencil_S8_Z24;
676 case MESA_FORMAT_S_UINT8:
677 return pack_ubyte_stencil_S8;
678 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
679 return pack_ubyte_stencil_Z32_FLOAT_X24S8;
680 default:
681 _mesa_problem(NULL,
682 "unexpected format in _mesa_pack_ubyte_stencil_func()");
683 return NULL;
684 }
685 }
686
687
688
689 void
690 _mesa_pack_float_z_row(mesa_format format, GLuint n,
691 const GLfloat *src, void *dst)
692 {
693 switch (format) {
694 case MESA_FORMAT_S8_UINT_Z24_UNORM:
695 case MESA_FORMAT_X8_UINT_Z24_UNORM:
696 {
697 /* don't disturb the stencil values */
698 GLuint *d = ((GLuint *) dst);
699 const GLdouble scale = (GLdouble) 0xffffff;
700 GLuint i;
701 for (i = 0; i < n; i++) {
702 GLuint s = d[i] & 0xff;
703 GLuint z = (GLuint) (src[i] * scale);
704 assert(z <= 0xffffff);
705 d[i] = (z << 8) | s;
706 }
707 }
708 break;
709 case MESA_FORMAT_Z24_UNORM_S8_UINT:
710 case MESA_FORMAT_Z24_UNORM_X8_UINT:
711 {
712 /* don't disturb the stencil values */
713 GLuint *d = ((GLuint *) dst);
714 const GLdouble scale = (GLdouble) 0xffffff;
715 GLuint i;
716 for (i = 0; i < n; i++) {
717 GLuint s = d[i] & 0xff000000;
718 GLuint z = (GLuint) (src[i] * scale);
719 assert(z <= 0xffffff);
720 d[i] = s | z;
721 }
722 }
723 break;
724 case MESA_FORMAT_Z_UNORM16:
725 {
726 GLushort *d = ((GLushort *) dst);
727 const GLfloat scale = (GLfloat) 0xffff;
728 GLuint i;
729 for (i = 0; i < n; i++) {
730 d[i] = (GLushort) (src[i] * scale);
731 }
732 }
733 break;
734 case MESA_FORMAT_Z_UNORM32:
735 {
736 GLuint *d = ((GLuint *) dst);
737 const GLdouble scale = (GLdouble) 0xffffffff;
738 GLuint i;
739 for (i = 0; i < n; i++) {
740 d[i] = (GLuint) (src[i] * scale);
741 }
742 }
743 break;
744 case MESA_FORMAT_Z_FLOAT32:
745 memcpy(dst, src, n * sizeof(GLfloat));
746 break;
747 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
748 {
749 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
750 GLuint i;
751 for (i = 0; i < n; i++) {
752 d[i].z = src[i];
753 }
754 }
755 break;
756 default:
757 _mesa_problem(NULL, "unexpected format in _mesa_pack_float_z_row()");
758 }
759 }
760
761
762 /**
763 * The incoming Z values are always in the range [0, 0xffffffff].
764 */
765 void
766 _mesa_pack_uint_z_row(mesa_format format, GLuint n,
767 const GLuint *src, void *dst)
768 {
769 switch (format) {
770 case MESA_FORMAT_S8_UINT_Z24_UNORM:
771 case MESA_FORMAT_X8_UINT_Z24_UNORM:
772 {
773 /* don't disturb the stencil values */
774 GLuint *d = ((GLuint *) dst);
775 GLuint i;
776 for (i = 0; i < n; i++) {
777 GLuint s = d[i] & 0xff;
778 GLuint z = src[i] & 0xffffff00;
779 d[i] = z | s;
780 }
781 }
782 break;
783 case MESA_FORMAT_Z24_UNORM_S8_UINT:
784 case MESA_FORMAT_Z24_UNORM_X8_UINT:
785 {
786 /* don't disturb the stencil values */
787 GLuint *d = ((GLuint *) dst);
788 GLuint i;
789 for (i = 0; i < n; i++) {
790 GLuint s = d[i] & 0xff000000;
791 GLuint z = src[i] >> 8;
792 d[i] = s | z;
793 }
794 }
795 break;
796 case MESA_FORMAT_Z_UNORM16:
797 {
798 GLushort *d = ((GLushort *) dst);
799 GLuint i;
800 for (i = 0; i < n; i++) {
801 d[i] = src[i] >> 16;
802 }
803 }
804 break;
805 case MESA_FORMAT_Z_UNORM32:
806 memcpy(dst, src, n * sizeof(GLfloat));
807 break;
808 case MESA_FORMAT_Z_FLOAT32:
809 {
810 GLuint *d = ((GLuint *) dst);
811 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
812 GLuint i;
813 for (i = 0; i < n; i++) {
814 d[i] = (GLuint) (src[i] * scale);
815 assert(d[i] >= 0.0f);
816 assert(d[i] <= 1.0f);
817 }
818 }
819 break;
820 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
821 {
822 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
823 const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;
824 GLuint i;
825 for (i = 0; i < n; i++) {
826 d[i].z = (GLfloat) (src[i] * scale);
827 assert(d[i].z >= 0.0f);
828 assert(d[i].z <= 1.0f);
829 }
830 }
831 break;
832 default:
833 _mesa_problem(NULL, "unexpected format in _mesa_pack_uint_z_row()");
834 }
835 }
836
837
838 void
839 _mesa_pack_ubyte_stencil_row(mesa_format format, GLuint n,
840 const GLubyte *src, void *dst)
841 {
842 switch (format) {
843 case MESA_FORMAT_S8_UINT_Z24_UNORM:
844 {
845 /* don't disturb the Z values */
846 GLuint *d = ((GLuint *) dst);
847 GLuint i;
848 for (i = 0; i < n; i++) {
849 GLuint s = src[i];
850 GLuint z = d[i] & 0xffffff00;
851 d[i] = z | s;
852 }
853 }
854 break;
855 case MESA_FORMAT_Z24_UNORM_S8_UINT:
856 {
857 /* don't disturb the Z values */
858 GLuint *d = ((GLuint *) dst);
859 GLuint i;
860 for (i = 0; i < n; i++) {
861 GLuint s = src[i] << 24;
862 GLuint z = d[i] & 0xffffff;
863 d[i] = s | z;
864 }
865 }
866 break;
867 case MESA_FORMAT_S_UINT8:
868 memcpy(dst, src, n * sizeof(GLubyte));
869 break;
870 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
871 {
872 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
873 GLuint i;
874 for (i = 0; i < n; i++) {
875 d[i].x24s8 = src[i];
876 }
877 }
878 break;
879 default:
880 _mesa_problem(NULL, "unexpected format in _mesa_pack_ubyte_stencil_row()");
881 }
882 }
883
884
885 /**
886 * Incoming Z/stencil values are always in uint_24_8 format.
887 */
888 void
889 _mesa_pack_uint_24_8_depth_stencil_row(mesa_format format, GLuint n,
890 const GLuint *src, void *dst)
891 {
892 switch (format) {
893 case MESA_FORMAT_S8_UINT_Z24_UNORM:
894 memcpy(dst, src, n * sizeof(GLuint));
895 break;
896 case MESA_FORMAT_Z24_UNORM_S8_UINT:
897 {
898 GLuint *d = ((GLuint *) dst);
899 GLuint i;
900 for (i = 0; i < n; i++) {
901 GLuint s = src[i] << 24;
902 GLuint z = src[i] >> 8;
903 d[i] = s | z;
904 }
905 }
906 break;
907 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
908 {
909 const GLdouble scale = 1.0 / (GLdouble) 0xffffff;
910 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
911 GLuint i;
912 for (i = 0; i < n; i++) {
913 GLfloat z = (GLfloat) ((src[i] >> 8) * scale);
914 d[i].z = z;
915 d[i].x24s8 = src[i];
916 }
917 }
918 break;
919 default:
920 _mesa_problem(NULL, "bad format %s in _mesa_pack_ubyte_s_row",
921 _mesa_get_format_name(format));
922 return;
923 }
924 }
925
926
927
928 /**
929 * Convert a boolean color mask to a packed color where each channel of
930 * the packed value at dst will be 0 or ~0 depending on the colorMask.
931 */
932 void
933 _mesa_pack_colormask(mesa_format format, const GLubyte colorMask[4], void *dst)
934 {
935 GLfloat maskColor[4];
936
937 switch (_mesa_get_format_datatype(format)) {
938 case GL_UNSIGNED_NORMALIZED:
939 /* simple: 1.0 will convert to ~0 in the right bit positions */
940 maskColor[0] = colorMask[0] ? 1.0f : 0.0f;
941 maskColor[1] = colorMask[1] ? 1.0f : 0.0f;
942 maskColor[2] = colorMask[2] ? 1.0f : 0.0f;
943 maskColor[3] = colorMask[3] ? 1.0f : 0.0f;
944 _mesa_pack_float_rgba_row(format, 1,
945 (const GLfloat (*)[4]) maskColor, dst);
946 break;
947 case GL_SIGNED_NORMALIZED:
948 case GL_FLOAT:
949 /* These formats are harder because it's hard to know the floating
950 * point values that will convert to ~0 for each color channel's bits.
951 * This solution just generates a non-zero value for each color channel
952 * then fixes up the non-zero values to be ~0.
953 * Note: we'll need to add special case code if we ever have to deal
954 * with formats with unequal color channel sizes, like R11_G11_B10.
955 * We issue a warning below for channel sizes other than 8,16,32.
956 */
957 {
958 GLuint bits = _mesa_get_format_max_bits(format); /* bits per chan */
959 GLuint bytes = _mesa_get_format_bytes(format);
960 GLuint i;
961
962 /* this should put non-zero values into the channels of dst */
963 maskColor[0] = colorMask[0] ? -1.0f : 0.0f;
964 maskColor[1] = colorMask[1] ? -1.0f : 0.0f;
965 maskColor[2] = colorMask[2] ? -1.0f : 0.0f;
966 maskColor[3] = colorMask[3] ? -1.0f : 0.0f;
967 _mesa_pack_float_rgba_row(format, 1,
968 (const GLfloat (*)[4]) maskColor, dst);
969
970 /* fix-up the dst channels by converting non-zero values to ~0 */
971 if (bits == 8) {
972 GLubyte *d = (GLubyte *) dst;
973 for (i = 0; i < bytes; i++) {
974 d[i] = d[i] ? 0xff : 0x0;
975 }
976 }
977 else if (bits == 16) {
978 GLushort *d = (GLushort *) dst;
979 for (i = 0; i < bytes / 2; i++) {
980 d[i] = d[i] ? 0xffff : 0x0;
981 }
982 }
983 else if (bits == 32) {
984 GLuint *d = (GLuint *) dst;
985 for (i = 0; i < bytes / 4; i++) {
986 d[i] = d[i] ? 0xffffffffU : 0x0;
987 }
988 }
989 else {
990 _mesa_problem(NULL, "unexpected size in _mesa_pack_colormask()");
991 return;
992 }
993 }
994 break;
995 default:
996 _mesa_problem(NULL, "unexpected format data type in gen_color_mask()");
997 return;
998 }
999 }
1000 """
1001
1002 template = Template(string);
1003
1004 print template.render(argv = argv[0:])