mesa: Autogenerate format_unpack.c
[mesa.git] / src / mesa / main / format_unpack.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_unpack.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
56 <%
57 import format_parser as parser
58
59 formats = parser.parse(argv[1])
60
61 rgb_formats = []
62 for f in formats:
63 if f.name == 'MESA_FORMAT_NONE':
64 continue
65 if f.colorspace not in ('rgb', 'srgb'):
66 continue
67
68 rgb_formats.append(f)
69 %>
70
71 /* float unpacking functions */
72
73 %for f in rgb_formats:
74 %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
75 <% continue %>
76 %elif f.is_int() and not f.is_normalized():
77 <% continue %>
78 %elif f.is_compressed():
79 <% continue %>
80 %endif
81
82 static inline void
83 unpack_float_${f.short_name()}(const void *void_src, GLfloat dst[4])
84 {
85 ${f.datatype()} *src = (${f.datatype()} *)void_src;
86 %if f.layout == parser.PACKED:
87 %for c in f.channels:
88 %if c.type != 'x':
89 ${c.datatype()} ${c.name} = UNPACK(*src, ${c.shift}, ${c.size});
90 %endif
91 %endfor
92 %elif f.layout == parser.ARRAY:
93 %for (i, c) in enumerate(f.channels):
94 %if c.type != 'x':
95 ${c.datatype()} ${c.name} = src[${i}];
96 %endif
97 %endfor
98 %else:
99 <% assert False %>
100 %endif
101
102 %for i in range(4):
103 <% s = f.swizzle[i] %>
104 %if 0 <= s and s <= parser.Swizzle.SWIZZLE_W:
105 <% c = f.channels[s] %>
106 %if c.type == parser.UNSIGNED:
107 %if f.colorspace == 'srgb' and c.name in 'rgb':
108 <% assert c.size == 8 %>
109 dst[${i}] = util_format_srgb_8unorm_to_linear_float(${c.name});
110 %else:
111 dst[${i}] = _mesa_unorm_to_float(${c.name}, ${c.size});
112 %endif
113 %elif c.type == parser.SIGNED:
114 dst[${i}] = _mesa_snorm_to_float(${c.name}, ${c.size});
115 %elif c.type == parser.FLOAT:
116 %if c.size == 32:
117 dst[${i}] = ${c.name};
118 %elif c.size == 16:
119 dst[${i}] = _mesa_half_to_float(${c.name});
120 %else:
121 <% assert False %>
122 %endif
123 %else:
124 <% assert False %>
125 %endif
126 %elif s == parser.Swizzle.SWIZZLE_ZERO:
127 dst[${i}] = 0.0f;
128 %elif s == parser.Swizzle.SWIZZLE_ONE:
129 dst[${i}] = 1.0f;
130 %else:
131 <% assert False %>
132 %endif
133 %endfor
134 }
135 %endfor
136
137 static void
138 unpack_float_r9g9b9e5_float(const void *src, GLfloat dst[4])
139 {
140 rgb9e5_to_float3(*(const GLuint *)src, dst);
141 dst[3] = 1.0f;
142 }
143
144 static void
145 unpack_float_r11g11b10_float(const void *src, GLfloat dst[4])
146 {
147 r11g11b10f_to_float3(*(const GLuint *)src, dst);
148 dst[3] = 1.0f;
149 }
150
151 static void
152 unpack_float_ycbcr(const void *src, GLfloat dst[][4], GLuint n)
153 {
154 GLuint i;
155 for (i = 0; i < n; i++) {
156 const GLushort *src0 = ((const GLushort *) src) + i * 2; /* even */
157 const GLushort *src1 = src0 + 1; /* odd */
158 const GLubyte y0 = (*src0 >> 8) & 0xff; /* luminance */
159 const GLubyte cb = *src0 & 0xff; /* chroma U */
160 const GLubyte y1 = (*src1 >> 8) & 0xff; /* luminance */
161 const GLubyte cr = *src1 & 0xff; /* chroma V */
162 const GLubyte y = (i & 1) ? y1 : y0; /* choose even/odd luminance */
163 GLfloat r = 1.164F * (y - 16) + 1.596F * (cr - 128);
164 GLfloat g = 1.164F * (y - 16) - 0.813F * (cr - 128) - 0.391F * (cb - 128);
165 GLfloat b = 1.164F * (y - 16) + 2.018F * (cb - 128);
166 r *= (1.0F / 255.0F);
167 g *= (1.0F / 255.0F);
168 b *= (1.0F / 255.0F);
169 dst[i][0] = CLAMP(r, 0.0F, 1.0F);
170 dst[i][1] = CLAMP(g, 0.0F, 1.0F);
171 dst[i][2] = CLAMP(b, 0.0F, 1.0F);
172 dst[i][3] = 1.0F;
173 }
174 }
175
176 static void
177 unpack_float_ycbcr_rev(const void *src, GLfloat dst[][4], GLuint n)
178 {
179 GLuint i;
180 for (i = 0; i < n; i++) {
181 const GLushort *src0 = ((const GLushort *) src) + i * 2; /* even */
182 const GLushort *src1 = src0 + 1; /* odd */
183 const GLubyte y0 = *src0 & 0xff; /* luminance */
184 const GLubyte cr = (*src0 >> 8) & 0xff; /* chroma V */
185 const GLubyte y1 = *src1 & 0xff; /* luminance */
186 const GLubyte cb = (*src1 >> 8) & 0xff; /* chroma U */
187 const GLubyte y = (i & 1) ? y1 : y0; /* choose even/odd luminance */
188 GLfloat r = 1.164F * (y - 16) + 1.596F * (cr - 128);
189 GLfloat g = 1.164F * (y - 16) - 0.813F * (cr - 128) - 0.391F * (cb - 128);
190 GLfloat b = 1.164F * (y - 16) + 2.018F * (cb - 128);
191 r *= (1.0F / 255.0F);
192 g *= (1.0F / 255.0F);
193 b *= (1.0F / 255.0F);
194 dst[i][0] = CLAMP(r, 0.0F, 1.0F);
195 dst[i][1] = CLAMP(g, 0.0F, 1.0F);
196 dst[i][2] = CLAMP(b, 0.0F, 1.0F);
197 dst[i][3] = 1.0F;
198 }
199 }
200
201 /* ubyte packing functions */
202
203 %for f in rgb_formats:
204 %if not f.is_normalized():
205 <% continue %>
206 %endif
207
208 static inline void
209 unpack_ubyte_${f.short_name()}(const void *void_src, GLubyte dst[4])
210 {
211 ${f.datatype()} *src = (${f.datatype()} *)void_src;
212 %if f.layout == parser.PACKED:
213 %for c in f.channels:
214 %if c.type != 'x':
215 ${c.datatype()} ${c.name} = UNPACK(*src, ${c.shift}, ${c.size});
216 %endif
217 %endfor
218 %elif f.layout == parser.ARRAY:
219 %for (i, c) in enumerate(f.channels):
220 %if c.type != 'x':
221 ${c.datatype()} ${c.name} = src[${i}];
222 %endif
223 %endfor
224 %else:
225 <% assert False %>
226 %endif
227
228 %for i in range(4):
229 <% s = f.swizzle[i] %>
230 %if 0 <= s and s <= parser.Swizzle.SWIZZLE_W:
231 <% c = f.channels[s] %>
232 %if c.type == parser.UNSIGNED:
233 %if f.colorspace == 'srgb' and c.name in 'rgb':
234 <% assert c.size == 8 %>
235 dst[${i}] = util_format_srgb_to_linear_8unorm(${c.name});
236 %else:
237 dst[${i}] = _mesa_unorm_to_unorm(${c.name}, ${c.size}, 8);
238 %endif
239 %elif c.type == parser.SIGNED:
240 dst[${i}] = _mesa_snorm_to_unorm(${c.name}, ${c.size}, 8);
241 %elif c.type == parser.FLOAT:
242 %if c.size == 32:
243 dst[${i}] = _mesa_float_to_unorm(${c.name}, 8);
244 %elif c.size == 16:
245 dst[${i}] = _mesa_half_to_unorm(${c.name}, 8);
246 %else:
247 <% assert False %>
248 %endif
249 %else:
250 <% assert False %>
251 %endif
252 %elif s == parser.Swizzle.SWIZZLE_ZERO:
253 dst[${i}] = 0;
254 %elif s == parser.Swizzle.SWIZZLE_ONE:
255 dst[${i}] = 255;
256 %else:
257 <% assert False %>
258 %endif
259 %endfor
260 }
261 %endfor
262
263
264 /* integer packing functions */
265
266 %for f in rgb_formats:
267 %if not f.is_int():
268 <% continue %>
269 %elif f.is_normalized():
270 <% continue %>
271 %endif
272
273 static inline void
274 unpack_int_${f.short_name()}(const void *void_src, GLuint dst[4])
275 {
276 ${f.datatype()} *src = (${f.datatype()} *)void_src;
277 %if f.layout == parser.PACKED:
278 %for c in f.channels:
279 %if c.type != 'x':
280 ${c.datatype()} ${c.name} = UNPACK(*src, ${c.shift}, ${c.size});
281 %endif
282 %endfor
283 %elif f.layout == parser.ARRAY:
284 %for (i, c) in enumerate(f.channels):
285 %if c.type != 'x':
286 ${c.datatype()} ${c.name} = src[${i}];
287 %endif
288 %endfor
289 %else:
290 <% assert False %>
291 %endif
292
293 %for i in range(4):
294 <% s = f.swizzle[i] %>
295 %if 0 <= s and s <= parser.Swizzle.SWIZZLE_W:
296 dst[${i}] = ${f.channels[s].name};
297 %elif s == parser.Swizzle.SWIZZLE_ZERO:
298 dst[${i}] = 0;
299 %elif s == parser.Swizzle.SWIZZLE_ONE:
300 dst[${i}] = 1;
301 %else:
302 <% assert False %>
303 %endif
304 %endfor
305 }
306 %endfor
307
308
309 void
310 _mesa_unpack_rgba_row(mesa_format format, GLuint n,
311 const void *src, GLfloat dst[][4])
312 {
313 GLubyte *s = (GLubyte *)src;
314 GLuint i;
315
316 switch (format) {
317 %for f in rgb_formats:
318 %if f.is_compressed():
319 <% continue %>
320 %elif f.is_int() and not f.is_normalized():
321 <% continue %>
322 %endif
323 case ${f.name}:
324 for (i = 0; i < n; ++i) {
325 unpack_float_${f.short_name()}(s, dst[i]);
326 s += ${f.block_size() / 8};
327 }
328 break;
329 %endfor
330 case MESA_FORMAT_YCBCR:
331 unpack_float_ycbcr(src, dst, n);
332 break;
333 case MESA_FORMAT_YCBCR_REV:
334 unpack_float_ycbcr_rev(src, dst, n);
335 break;
336 default:
337 _mesa_problem(NULL, "%s: bad format %s", __FUNCTION__,
338 _mesa_get_format_name(format));
339 return;
340 }
341 }
342
343 void
344 _mesa_unpack_ubyte_rgba_row(mesa_format format, GLuint n,
345 const void *src, GLubyte dst[][4])
346 {
347 GLubyte *s = (GLubyte *)src;
348 GLuint i;
349
350 switch (format) {
351 %for f in rgb_formats:
352 %if not f.is_normalized():
353 <% continue %>
354 %endif
355
356 case ${f.name}:
357 for (i = 0; i < n; ++i) {
358 unpack_ubyte_${f.short_name()}(s, dst[i]);
359 s += ${f.block_size() / 8};
360 }
361 break;
362 %endfor
363 default:
364 /* get float values, convert to ubyte */
365 {
366 GLfloat *tmp = malloc(n * 4 * sizeof(GLfloat));
367 if (tmp) {
368 GLuint i;
369 _mesa_unpack_rgba_row(format, n, src, (GLfloat (*)[4]) tmp);
370 for (i = 0; i < n; i++) {
371 dst[i][0] = _mesa_float_to_unorm(tmp[i*4+0], 8);
372 dst[i][1] = _mesa_float_to_unorm(tmp[i*4+1], 8);
373 dst[i][2] = _mesa_float_to_unorm(tmp[i*4+2], 8);
374 dst[i][3] = _mesa_float_to_unorm(tmp[i*4+3], 8);
375 }
376 free(tmp);
377 }
378 }
379 break;
380 }
381 }
382
383 void
384 _mesa_unpack_uint_rgba_row(mesa_format format, GLuint n,
385 const void *src, GLuint dst[][4])
386 {
387 GLubyte *s = (GLubyte *)src;
388 GLuint i;
389
390 switch (format) {
391 %for f in rgb_formats:
392 %if not f.is_int():
393 <% continue %>
394 %elif f.is_normalized():
395 <% continue %>
396 %endif
397
398 case ${f.name}:
399 for (i = 0; i < n; ++i) {
400 unpack_int_${f.short_name()}(s, dst[i]);
401 s += ${f.block_size() / 8};
402 }
403 break;
404 %endfor
405 default:
406 _mesa_problem(NULL, "%s: bad format %s", __FUNCTION__,
407 _mesa_get_format_name(format));
408 return;
409 }
410 }
411
412 /**
413 * Unpack a 2D rect of pixels returning float RGBA colors.
414 * \param format the source image format
415 * \param src start address of the source image
416 * \param srcRowStride source image row stride in bytes
417 * \param dst start address of the dest image
418 * \param dstRowStride dest image row stride in bytes
419 * \param x source image start X pos
420 * \param y source image start Y pos
421 * \param width width of rect region to convert
422 * \param height height of rect region to convert
423 */
424 void
425 _mesa_unpack_rgba_block(mesa_format format,
426 const void *src, GLint srcRowStride,
427 GLfloat dst[][4], GLint dstRowStride,
428 GLuint x, GLuint y, GLuint width, GLuint height)
429 {
430 const GLuint srcPixStride = _mesa_get_format_bytes(format);
431 const GLuint dstPixStride = 4 * sizeof(GLfloat);
432 const GLubyte *srcRow;
433 GLubyte *dstRow;
434 GLuint i;
435
436 /* XXX needs to be fixed for compressed formats */
437
438 srcRow = ((const GLubyte *) src) + srcRowStride * y + srcPixStride * x;
439 dstRow = ((GLubyte *) dst) + dstRowStride * y + dstPixStride * x;
440
441 for (i = 0; i < height; i++) {
442 _mesa_unpack_rgba_row(format, width, srcRow, (GLfloat (*)[4]) dstRow);
443
444 dstRow += dstRowStride;
445 srcRow += srcRowStride;
446 }
447 }
448
449 /** Helper struct for MESA_FORMAT_Z32_FLOAT_S8X24_UINT */
450 struct z32f_x24s8
451 {
452 float z;
453 uint32_t x24s8;
454 };
455
456 typedef void (*unpack_float_z_func)(GLuint n, const void *src, GLfloat *dst);
457
458 static void
459 unpack_float_z_X8_UINT_Z24_UNORM(GLuint n, const void *src, GLfloat *dst)
460 {
461 /* only return Z, not stencil data */
462 const GLuint *s = ((const GLuint *) src);
463 const GLdouble scale = 1.0 / (GLdouble) 0xffffff;
464 GLuint i;
465 for (i = 0; i < n; i++) {
466 dst[i] = (GLfloat) ((s[i] >> 8) * scale);
467 ASSERT(dst[i] >= 0.0F);
468 ASSERT(dst[i] <= 1.0F);
469 }
470 }
471
472 static void
473 unpack_float_z_Z24_UNORM_X8_UINT(GLuint n, const void *src, GLfloat *dst)
474 {
475 /* only return Z, not stencil data */
476 const GLuint *s = ((const GLuint *) src);
477 const GLdouble scale = 1.0 / (GLdouble) 0xffffff;
478 GLuint i;
479 for (i = 0; i < n; i++) {
480 dst[i] = (GLfloat) ((s[i] & 0x00ffffff) * scale);
481 ASSERT(dst[i] >= 0.0F);
482 ASSERT(dst[i] <= 1.0F);
483 }
484 }
485
486 static void
487 unpack_float_Z_UNORM16(GLuint n, const void *src, GLfloat *dst)
488 {
489 const GLushort *s = ((const GLushort *) src);
490 GLuint i;
491 for (i = 0; i < n; i++) {
492 dst[i] = s[i] * (1.0F / 65535.0F);
493 }
494 }
495
496 static void
497 unpack_float_Z_UNORM32(GLuint n, const void *src, GLfloat *dst)
498 {
499 const GLuint *s = ((const GLuint *) src);
500 GLuint i;
501 for (i = 0; i < n; i++) {
502 dst[i] = s[i] * (1.0F / 0xffffffff);
503 }
504 }
505
506 static void
507 unpack_float_Z_FLOAT32(GLuint n, const void *src, GLfloat *dst)
508 {
509 memcpy(dst, src, n * sizeof(float));
510 }
511
512 static void
513 unpack_float_z_Z32X24S8(GLuint n, const void *src, GLfloat *dst)
514 {
515 const struct z32f_x24s8 *s = (const struct z32f_x24s8 *) src;
516 GLuint i;
517 for (i = 0; i < n; i++) {
518 dst[i] = s[i].z;
519 }
520 }
521
522
523
524 /**
525 * Unpack Z values.
526 * The returned values will always be in the range [0.0, 1.0].
527 */
528 void
529 _mesa_unpack_float_z_row(mesa_format format, GLuint n,
530 const void *src, GLfloat *dst)
531 {
532 unpack_float_z_func unpack;
533
534 switch (format) {
535 case MESA_FORMAT_S8_UINT_Z24_UNORM:
536 case MESA_FORMAT_X8_UINT_Z24_UNORM:
537 unpack = unpack_float_z_X8_UINT_Z24_UNORM;
538 break;
539 case MESA_FORMAT_Z24_UNORM_S8_UINT:
540 case MESA_FORMAT_Z24_UNORM_X8_UINT:
541 unpack = unpack_float_z_Z24_UNORM_X8_UINT;
542 break;
543 case MESA_FORMAT_Z_UNORM16:
544 unpack = unpack_float_Z_UNORM16;
545 break;
546 case MESA_FORMAT_Z_UNORM32:
547 unpack = unpack_float_Z_UNORM32;
548 break;
549 case MESA_FORMAT_Z_FLOAT32:
550 unpack = unpack_float_Z_FLOAT32;
551 break;
552 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
553 unpack = unpack_float_z_Z32X24S8;
554 break;
555 default:
556 _mesa_problem(NULL, "bad format %s in _mesa_unpack_float_z_row",
557 _mesa_get_format_name(format));
558 return;
559 }
560
561 unpack(n, src, dst);
562 }
563
564
565
566 typedef void (*unpack_uint_z_func)(const void *src, GLuint *dst, GLuint n);
567
568 static void
569 unpack_uint_z_X8_UINT_Z24_UNORM(const void *src, GLuint *dst, GLuint n)
570 {
571 /* only return Z, not stencil data */
572 const GLuint *s = ((const GLuint *) src);
573 GLuint i;
574 for (i = 0; i < n; i++) {
575 dst[i] = (s[i] & 0xffffff00) | (s[i] >> 24);
576 }
577 }
578
579 static void
580 unpack_uint_z_Z24_UNORM_X8_UINT(const void *src, GLuint *dst, GLuint n)
581 {
582 /* only return Z, not stencil data */
583 const GLuint *s = ((const GLuint *) src);
584 GLuint i;
585 for (i = 0; i < n; i++) {
586 dst[i] = (s[i] << 8) | ((s[i] >> 16) & 0xff);
587 }
588 }
589
590 static void
591 unpack_uint_Z_UNORM16(const void *src, GLuint *dst, GLuint n)
592 {
593 const GLushort *s = ((const GLushort *)src);
594 GLuint i;
595 for (i = 0; i < n; i++) {
596 dst[i] = (s[i] << 16) | s[i];
597 }
598 }
599
600 static void
601 unpack_uint_Z_UNORM32(const void *src, GLuint *dst, GLuint n)
602 {
603 memcpy(dst, src, n * sizeof(GLuint));
604 }
605
606 static void
607 unpack_uint_Z_FLOAT32(const void *src, GLuint *dst, GLuint n)
608 {
609 const float *s = (const float *)src;
610 GLuint i;
611 for (i = 0; i < n; i++) {
612 dst[i] = FLOAT_TO_UINT(CLAMP(s[i], 0.0F, 1.0F));
613 }
614 }
615
616 static void
617 unpack_uint_Z_FLOAT32_X24S8(const void *src, GLuint *dst, GLuint n)
618 {
619 const struct z32f_x24s8 *s = (const struct z32f_x24s8 *) src;
620 GLuint i;
621
622 for (i = 0; i < n; i++) {
623 dst[i] = FLOAT_TO_UINT(CLAMP(s[i].z, 0.0F, 1.0F));
624 }
625 }
626
627
628 /**
629 * Unpack Z values.
630 * The returned values will always be in the range [0, 0xffffffff].
631 */
632 void
633 _mesa_unpack_uint_z_row(mesa_format format, GLuint n,
634 const void *src, GLuint *dst)
635 {
636 unpack_uint_z_func unpack;
637 const GLubyte *srcPtr = (GLubyte *) src;
638
639 switch (format) {
640 case MESA_FORMAT_S8_UINT_Z24_UNORM:
641 case MESA_FORMAT_X8_UINT_Z24_UNORM:
642 unpack = unpack_uint_z_X8_UINT_Z24_UNORM;
643 break;
644 case MESA_FORMAT_Z24_UNORM_S8_UINT:
645 case MESA_FORMAT_Z24_UNORM_X8_UINT:
646 unpack = unpack_uint_z_Z24_UNORM_X8_UINT;
647 break;
648 case MESA_FORMAT_Z_UNORM16:
649 unpack = unpack_uint_Z_UNORM16;
650 break;
651 case MESA_FORMAT_Z_UNORM32:
652 unpack = unpack_uint_Z_UNORM32;
653 break;
654 case MESA_FORMAT_Z_FLOAT32:
655 unpack = unpack_uint_Z_FLOAT32;
656 break;
657 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
658 unpack = unpack_uint_Z_FLOAT32_X24S8;
659 break;
660 default:
661 _mesa_problem(NULL, "bad format %s in _mesa_unpack_uint_z_row",
662 _mesa_get_format_name(format));
663 return;
664 }
665
666 unpack(srcPtr, dst, n);
667 }
668
669
670 static void
671 unpack_ubyte_s_S_UINT8(const void *src, GLubyte *dst, GLuint n)
672 {
673 memcpy(dst, src, n);
674 }
675
676 static void
677 unpack_ubyte_s_S8_UINT_Z24_UNORM(const void *src, GLubyte *dst, GLuint n)
678 {
679 GLuint i;
680 const GLuint *src32 = src;
681
682 for (i = 0; i < n; i++)
683 dst[i] = src32[i] & 0xff;
684 }
685
686 static void
687 unpack_ubyte_s_Z24_UNORM_S8_UINT(const void *src, GLubyte *dst, GLuint n)
688 {
689 GLuint i;
690 const GLuint *src32 = src;
691
692 for (i = 0; i < n; i++)
693 dst[i] = src32[i] >> 24;
694 }
695
696 static void
697 unpack_ubyte_s_Z32_FLOAT_S8X24_UINT(const void *src, GLubyte *dst, GLuint n)
698 {
699 GLuint i;
700 const struct z32f_x24s8 *s = (const struct z32f_x24s8 *) src;
701
702 for (i = 0; i < n; i++)
703 dst[i] = s[i].x24s8 & 0xff;
704 }
705
706 void
707 _mesa_unpack_ubyte_stencil_row(mesa_format format, GLuint n,
708 const void *src, GLubyte *dst)
709 {
710 switch (format) {
711 case MESA_FORMAT_S_UINT8:
712 unpack_ubyte_s_S_UINT8(src, dst, n);
713 break;
714 case MESA_FORMAT_S8_UINT_Z24_UNORM:
715 unpack_ubyte_s_S8_UINT_Z24_UNORM(src, dst, n);
716 break;
717 case MESA_FORMAT_Z24_UNORM_S8_UINT:
718 unpack_ubyte_s_Z24_UNORM_S8_UINT(src, dst, n);
719 break;
720 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
721 unpack_ubyte_s_Z32_FLOAT_S8X24_UINT(src, dst, n);
722 break;
723 default:
724 _mesa_problem(NULL, "bad format %s in _mesa_unpack_ubyte_s_row",
725 _mesa_get_format_name(format));
726 return;
727 }
728 }
729
730 static void
731 unpack_uint_24_8_depth_stencil_Z24_UNORM_S8_UINT(const GLuint *src, GLuint *dst, GLuint n)
732 {
733 GLuint i;
734
735 for (i = 0; i < n; i++) {
736 GLuint val = src[i];
737 dst[i] = val >> 24 | val << 8;
738 }
739 }
740
741 static void
742 unpack_uint_24_8_depth_stencil_Z32_S8X24(const GLuint *src,
743 GLuint *dst, GLuint n)
744 {
745 GLuint i;
746
747 for (i = 0; i < n; i++) {
748 /* 8 bytes per pixel (float + uint32) */
749 GLfloat zf = ((GLfloat *) src)[i * 2 + 0];
750 GLuint z24 = (GLuint) (zf * (GLfloat) 0xffffff);
751 GLuint s = src[i * 2 + 1] & 0xff;
752 dst[i] = (z24 << 8) | s;
753 }
754 }
755
756 static void
757 unpack_uint_24_8_depth_stencil_S8_UINT_Z24_UNORM(const GLuint *src, GLuint *dst, GLuint n)
758 {
759 memcpy(dst, src, n * 4);
760 }
761
762 /**
763 * Unpack depth/stencil returning as GL_UNSIGNED_INT_24_8.
764 * \param format the source data format
765 */
766 void
767 _mesa_unpack_uint_24_8_depth_stencil_row(mesa_format format, GLuint n,
768 const void *src, GLuint *dst)
769 {
770 switch (format) {
771 case MESA_FORMAT_S8_UINT_Z24_UNORM:
772 unpack_uint_24_8_depth_stencil_S8_UINT_Z24_UNORM(src, dst, n);
773 break;
774 case MESA_FORMAT_Z24_UNORM_S8_UINT:
775 unpack_uint_24_8_depth_stencil_Z24_UNORM_S8_UINT(src, dst, n);
776 break;
777 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
778 unpack_uint_24_8_depth_stencil_Z32_S8X24(src, dst, n);
779 break;
780 default:
781 _mesa_problem(NULL,
782 "bad format %s in _mesa_unpack_uint_24_8_depth_stencil_row",
783 _mesa_get_format_name(format));
784 return;
785 }
786 }
787
788 static void
789 unpack_float_32_uint_24_8_Z24_UNORM_S8_UINT(const GLuint *src,
790 GLuint *dst, GLuint n)
791 {
792 GLuint i;
793 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
794 const GLdouble scale = 1.0 / (GLdouble) 0xffffff;
795
796 for (i = 0; i < n; i++) {
797 const GLuint z24 = src[i] & 0xffffff;
798 d[i].z = z24 * scale;
799 d[i].x24s8 = src[i] >> 24;
800 assert(d[i].z >= 0.0f);
801 assert(d[i].z <= 1.0f);
802 }
803 }
804
805 static void
806 unpack_float_32_uint_24_8_Z32_FLOAT_S8X24_UINT(const GLuint *src,
807 GLuint *dst, GLuint n)
808 {
809 memcpy(dst, src, n * sizeof(struct z32f_x24s8));
810 }
811
812 static void
813 unpack_float_32_uint_24_8_S8_UINT_Z24_UNORM(const GLuint *src,
814 GLuint *dst, GLuint n)
815 {
816 GLuint i;
817 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
818 const GLdouble scale = 1.0 / (GLdouble) 0xffffff;
819
820 for (i = 0; i < n; i++) {
821 const GLuint z24 = src[i] >> 8;
822 d[i].z = z24 * scale;
823 d[i].x24s8 = src[i] & 0xff;
824 assert(d[i].z >= 0.0f);
825 assert(d[i].z <= 1.0f);
826 }
827 }
828
829 /**
830 * Unpack depth/stencil returning as GL_FLOAT_32_UNSIGNED_INT_24_8_REV.
831 * \param format the source data format
832 *
833 * In GL_FLOAT_32_UNSIGNED_INT_24_8_REV lower 4 bytes contain float
834 * component and higher 4 bytes contain packed 24-bit and 8-bit
835 * components.
836 *
837 * 31 30 29 28 ... 4 3 2 1 0 31 30 29 ... 9 8 7 6 5 ... 2 1 0
838 * +-------------------------+ +--------------------------------+
839 * | Float Component | | Unused | 8 bit stencil |
840 * +-------------------------+ +--------------------------------+
841 * lower 4 bytes higher 4 bytes
842 */
843 void
844 _mesa_unpack_float_32_uint_24_8_depth_stencil_row(mesa_format format, GLuint n,
845 const void *src, GLuint *dst)
846 {
847 switch (format) {
848 case MESA_FORMAT_S8_UINT_Z24_UNORM:
849 unpack_float_32_uint_24_8_S8_UINT_Z24_UNORM(src, dst, n);
850 break;
851 case MESA_FORMAT_Z24_UNORM_S8_UINT:
852 unpack_float_32_uint_24_8_Z24_UNORM_S8_UINT(src, dst, n);
853 break;
854 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
855 unpack_float_32_uint_24_8_Z32_FLOAT_S8X24_UINT(src, dst, n);
856 break;
857 default:
858 _mesa_problem(NULL,
859 "bad format %s in _mesa_unpack_uint_24_8_depth_stencil_row",
860 _mesa_get_format_name(format));
861 return;
862 }
863 }
864
865 /**
866 * Unpack depth/stencil
867 * \param format the source data format
868 * \param type the destination data type
869 */
870 void
871 _mesa_unpack_depth_stencil_row(mesa_format format, GLuint n,
872 const void *src, GLenum type,
873 GLuint *dst)
874 {
875 assert(type == GL_UNSIGNED_INT_24_8 ||
876 type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
877
878 switch (type) {
879 case GL_UNSIGNED_INT_24_8:
880 _mesa_unpack_uint_24_8_depth_stencil_row(format, n, src, dst);
881 break;
882 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
883 _mesa_unpack_float_32_uint_24_8_depth_stencil_row(format, n, src, dst);
884 break;
885 default:
886 _mesa_problem(NULL,
887 "bad type 0x%x in _mesa_unpack_depth_stencil_row",
888 type);
889 return;
890 }
891 }
892 """
893
894 template = Template(string);
895
896 print template.render(argv = argv[0:])