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