1 from __future__
import print_function
3 from mako
.template
import Template
7 * Mesa 3-D graphics library
9 * Copyright (c) 2011 VMware, Inc.
10 * Copyright (c) 2014 Intel Corporation.
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:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
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.
33 * Color, depth, stencil packing functions.
34 * Used to pack basic color, depth and stencil formats to specific
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.
46 #include "format_pack.h"
47 #include "format_utils.h"
49 #include "util/format_rgb9e5.h"
50 #include "util/format_r11g11b10f.h"
51 #include "util/format_srgb.h"
53 #define UNPACK(SRC, OFFSET, BITS) (((SRC) >> (OFFSET)) & MAX_UINT(BITS))
54 #define PACK(SRC, OFFSET, BITS) (((SRC) & MAX_UINT(BITS)) << (OFFSET))
57 import format_parser as parser
59 formats = parser.parse(argv[1])
63 if f.name == 'MESA_FORMAT_NONE':
65 if f.colorspace not in ('rgb', 'srgb'):
71 /* ubyte packing functions */
73 %for f in rgb_formats:
74 %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
76 %elif f.is_compressed():
81 pack_ubyte_${f.short_name()}(const uint8_t src[4], void *dst)
83 %for (i, c) in enumerate(f.channels):
84 <% i = f.swizzle.inverse()[i] %>
89 ${c.datatype()} ${c.name} =
90 %if not f.is_normalized() and f.is_int():
91 %if c.type == parser.SIGNED:
92 _mesa_unsigned_to_signed(src[${i}], ${c.size});
94 _mesa_unsigned_to_unsigned(src[${i}], ${c.size});
96 %elif c.type == parser.UNSIGNED:
97 %if f.colorspace == 'srgb' and c.name in 'rgb':
98 <% assert c.size == 8 %>
99 util_format_linear_to_srgb_8unorm(src[${i}]);
101 _mesa_unorm_to_unorm(src[${i}], 8, ${c.size});
103 %elif c.type == parser.SIGNED:
104 _mesa_unorm_to_snorm(src[${i}], 8, ${c.size});
105 %elif c.type == parser.FLOAT:
107 _mesa_unorm_to_float(src[${i}], 8);
109 _mesa_unorm_to_half(src[${i}], 8);
118 %if f.layout == parser.ARRAY:
119 ${f.datatype()} *d = (${f.datatype()} *)dst;
120 %for (i, c) in enumerate(f.channels):
126 %elif f.layout == parser.PACKED:
127 ${f.datatype()} d = 0;
128 %for (i, c) in enumerate(f.channels):
132 d |= PACK(${c.name}, ${c.shift}, ${c.size});
134 (*(${f.datatype()} *)dst) = d;
142 pack_ubyte_r9g9b9e5_float(const uint8_t src[4], void *dst)
144 uint32_t *d = (uint32_t *) dst;
146 rgb[0] = _mesa_unorm_to_float(src[0], 8);
147 rgb[1] = _mesa_unorm_to_float(src[1], 8);
148 rgb[2] = _mesa_unorm_to_float(src[2], 8);
149 *d = float3_to_rgb9e5(rgb);
153 pack_ubyte_r11g11b10_float(const uint8_t src[4], void *dst)
155 uint32_t *d = (uint32_t *) dst;
157 rgb[0] = _mesa_unorm_to_float(src[0], 8);
158 rgb[1] = _mesa_unorm_to_float(src[1], 8);
159 rgb[2] = _mesa_unorm_to_float(src[2], 8);
160 *d = float3_to_r11g11b10f(rgb);
163 /* uint packing functions */
165 %for f in rgb_formats:
168 %elif f.is_normalized():
170 %elif f.is_compressed():
175 pack_uint_${f.short_name()}(const uint32_t src[4], void *dst)
177 %for (i, c) in enumerate(f.channels):
178 <% i = f.swizzle.inverse()[i] %>
183 ${c.datatype()} ${c.name} =
184 %if c.type == parser.SIGNED:
185 _mesa_signed_to_signed(src[${i}], ${c.size});
186 %elif c.type == parser.UNSIGNED:
187 _mesa_unsigned_to_unsigned(src[${i}], ${c.size});
189 assert(!"Invalid type: only integer types are allowed");
193 %if f.layout == parser.ARRAY:
194 ${f.datatype()} *d = (${f.datatype()} *)dst;
195 %for (i, c) in enumerate(f.channels):
201 %elif f.layout == parser.PACKED:
202 ${f.datatype()} d = 0;
203 %for (i, c) in enumerate(f.channels):
207 d |= PACK(${c.name}, ${c.shift}, ${c.size});
209 (*(${f.datatype()} *)dst) = d;
216 /* float packing functions */
218 %for f in rgb_formats:
219 %if f.name in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):
221 %elif f.is_int() and not f.is_normalized():
223 %elif f.is_compressed():
228 pack_float_${f.short_name()}(const float src[4], void *dst)
230 %for (i, c) in enumerate(f.channels):
231 <% i = f.swizzle.inverse()[i] %>
236 ${c.datatype()} ${c.name} =
237 %if c.type == parser.UNSIGNED:
238 %if f.colorspace == 'srgb' and c.name in 'rgb':
239 <% assert c.size == 8 %>
240 util_format_linear_float_to_srgb_8unorm(src[${i}]);
242 _mesa_float_to_unorm(src[${i}], ${c.size});
244 %elif c.type == parser.SIGNED:
245 _mesa_float_to_snorm(src[${i}], ${c.size});
246 %elif c.type == parser.FLOAT:
250 _mesa_float_to_half(src[${i}]);
259 %if f.layout == parser.ARRAY:
260 ${f.datatype()} *d = (${f.datatype()} *)dst;
261 %for (i, c) in enumerate(f.channels):
267 %elif f.layout == parser.PACKED:
268 ${f.datatype()} d = 0;
269 %for (i, c) in enumerate(f.channels):
273 d |= PACK(${c.name}, ${c.shift}, ${c.size});
275 (*(${f.datatype()} *)dst) = d;
283 pack_float_r9g9b9e5_float(const float src[4], void *dst)
285 uint32_t *d = (uint32_t *) dst;
286 *d = float3_to_rgb9e5(src);
290 pack_float_r11g11b10_float(const float src[4], void *dst)
292 uint32_t *d = (uint32_t *) dst;
293 *d = float3_to_r11g11b10f(src);
297 * Return a function that can pack a uint8_t rgba[4] color.
299 mesa_pack_ubyte_rgba_func
300 _mesa_get_pack_ubyte_rgba_function(mesa_format format)
303 %for f in rgb_formats:
304 %if f.is_compressed():
309 return pack_ubyte_${f.short_name()};
317 * Return a function that can pack a float rgba[4] color.
319 mesa_pack_float_rgba_func
320 _mesa_get_pack_float_rgba_function(mesa_format format)
323 %for f in rgb_formats:
324 %if f.is_compressed():
326 %elif f.is_int() and not f.is_normalized():
331 return pack_float_${f.short_name()};
339 * Pack a row of uint8_t rgba[4] values to the destination.
342 _mesa_pack_ubyte_rgba_row(mesa_format format, uint32_t n,
343 const uint8_t src[][4], void *dst)
349 %for f in rgb_formats:
350 %if f.is_compressed():
355 for (i = 0; i < n; ++i) {
356 pack_ubyte_${f.short_name()}(src[i], d);
357 d += ${f.block_size() // 8};
362 assert(!"Invalid format");
367 * Pack a row of uint32_t rgba[4] values to the destination.
370 _mesa_pack_uint_rgba_row(mesa_format format, uint32_t n,
371 const uint32_t src[][4], void *dst)
377 %for f in rgb_formats:
380 %elif f.is_normalized():
382 %elif f.is_compressed():
387 for (i = 0; i < n; ++i) {
388 pack_uint_${f.short_name()}(src[i], d);
389 d += ${f.block_size() // 8};
394 assert(!"Invalid format");
399 * Pack a row of float rgba[4] values to the destination.
402 _mesa_pack_float_rgba_row(mesa_format format, uint32_t n,
403 const float src[][4], void *dst)
409 %for f in rgb_formats:
410 %if f.is_compressed():
412 %elif f.is_int() and not f.is_normalized():
417 for (i = 0; i < n; ++i) {
418 pack_float_${f.short_name()}(src[i], d);
419 d += ${f.block_size() // 8};
424 assert(!"Invalid format");
429 * Pack a 2D image of ubyte RGBA pixels in the given format.
430 * \param srcRowStride source image row stride in bytes
431 * \param dstRowStride destination image row stride in bytes
434 _mesa_pack_ubyte_rgba_rect(mesa_format format, uint32_t width, uint32_t height,
435 const uint8_t *src, int32_t srcRowStride,
436 void *dst, int32_t dstRowStride)
438 uint8_t *dstUB = dst;
441 if (srcRowStride == width * 4 * sizeof(uint8_t) &&
442 dstRowStride == _mesa_format_row_stride(format, width)) {
443 /* do whole image at once */
444 _mesa_pack_ubyte_rgba_row(format, width * height,
445 (const uint8_t (*)[4]) src, dst);
449 for (i = 0; i < height; i++) {
450 _mesa_pack_ubyte_rgba_row(format, width,
451 (const uint8_t (*)[4]) src, dstUB);
453 dstUB += dstRowStride;
459 /** Helper struct for MESA_FORMAT_Z32_FLOAT_S8X24_UINT */
468 ** Pack float Z pixels
472 pack_float_S8_UINT_Z24_UNORM(const float *src, void *dst)
474 /* don't disturb the stencil values */
475 uint32_t *d = ((uint32_t *) dst);
476 const double scale = (double) 0xffffff;
477 uint32_t s = *d & 0xff;
478 uint32_t z = (uint32_t) (*src * scale);
479 assert(z <= 0xffffff);
484 pack_float_Z24_UNORM_S8_UINT(const float *src, void *dst)
486 /* don't disturb the stencil values */
487 uint32_t *d = ((uint32_t *) dst);
488 const double scale = (double) 0xffffff;
489 uint32_t s = *d & 0xff000000;
490 uint32_t z = (uint32_t) (*src * scale);
491 assert(z <= 0xffffff);
496 pack_float_Z_UNORM16(const float *src, void *dst)
498 uint16_t *d = ((uint16_t *) dst);
499 const float scale = (float) 0xffff;
500 *d = (uint16_t) (*src * scale);
504 pack_float_Z_UNORM32(const float *src, void *dst)
506 uint32_t *d = ((uint32_t *) dst);
507 const double scale = (double) 0xffffffff;
508 *d = (uint32_t) (*src * scale);
512 ** Pack float to Z_FLOAT32 or Z_FLOAT32_X24S8.
516 pack_float_Z_FLOAT32(const float *src, void *dst)
518 float *d = (float *) dst;
522 mesa_pack_float_z_func
523 _mesa_get_pack_float_z_func(mesa_format format)
526 case MESA_FORMAT_S8_UINT_Z24_UNORM:
527 case MESA_FORMAT_X8_UINT_Z24_UNORM:
528 return pack_float_S8_UINT_Z24_UNORM;
529 case MESA_FORMAT_Z24_UNORM_S8_UINT:
530 case MESA_FORMAT_Z24_UNORM_X8_UINT:
531 return pack_float_Z24_UNORM_S8_UINT;
532 case MESA_FORMAT_Z_UNORM16:
533 return pack_float_Z_UNORM16;
534 case MESA_FORMAT_Z_UNORM32:
535 return pack_float_Z_UNORM32;
536 case MESA_FORMAT_Z_FLOAT32:
537 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
538 return pack_float_Z_FLOAT32;
540 unreachable("unexpected format in _mesa_get_pack_float_z_func()");
547 ** Pack uint Z pixels. The incoming src value is always in
548 ** the range [0, 2^32-1].
552 pack_uint_S8_UINT_Z24_UNORM(const uint32_t *src, void *dst)
554 /* don't disturb the stencil values */
555 uint32_t *d = ((uint32_t *) dst);
556 uint32_t s = *d & 0xff;
557 uint32_t z = *src & 0xffffff00;
562 pack_uint_Z24_UNORM_S8_UINT(const uint32_t *src, void *dst)
564 /* don't disturb the stencil values */
565 uint32_t *d = ((uint32_t *) dst);
566 uint32_t s = *d & 0xff000000;
567 uint32_t z = *src >> 8;
572 pack_uint_Z_UNORM16(const uint32_t *src, void *dst)
574 uint16_t *d = ((uint16_t *) dst);
579 pack_uint_Z_UNORM32(const uint32_t *src, void *dst)
581 uint32_t *d = ((uint32_t *) dst);
586 ** Pack uint to Z_FLOAT32 or Z_FLOAT32_X24S8.
590 pack_uint_Z_FLOAT32(const uint32_t *src, void *dst)
592 float *d = ((float *) dst);
593 const double scale = 1.0 / (double) 0xffffffff;
594 *d = (float) (*src * scale);
599 mesa_pack_uint_z_func
600 _mesa_get_pack_uint_z_func(mesa_format format)
603 case MESA_FORMAT_S8_UINT_Z24_UNORM:
604 case MESA_FORMAT_X8_UINT_Z24_UNORM:
605 return pack_uint_S8_UINT_Z24_UNORM;
606 case MESA_FORMAT_Z24_UNORM_S8_UINT:
607 case MESA_FORMAT_Z24_UNORM_X8_UINT:
608 return pack_uint_Z24_UNORM_S8_UINT;
609 case MESA_FORMAT_Z_UNORM16:
610 return pack_uint_Z_UNORM16;
611 case MESA_FORMAT_Z_UNORM32:
612 return pack_uint_Z_UNORM32;
613 case MESA_FORMAT_Z_FLOAT32:
614 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
615 return pack_uint_Z_FLOAT32;
617 unreachable("unexpected format in _mesa_get_pack_uint_z_func()");
623 ** Pack ubyte stencil pixels
627 pack_ubyte_stencil_Z24_S8(const uint8_t *src, void *dst)
629 /* don't disturb the Z values */
630 uint32_t *d = ((uint32_t *) dst);
632 uint32_t z = *d & 0xffffff00;
637 pack_ubyte_stencil_S8_Z24(const uint8_t *src, void *dst)
639 /* don't disturb the Z values */
640 uint32_t *d = ((uint32_t *) dst);
641 uint32_t s = *src << 24;
642 uint32_t z = *d & 0xffffff;
647 pack_ubyte_stencil_S8(const uint8_t *src, void *dst)
649 uint8_t *d = (uint8_t *) dst;
654 pack_ubyte_stencil_Z32_FLOAT_X24S8(const uint8_t *src, void *dst)
656 float *d = ((float *) dst);
661 mesa_pack_ubyte_stencil_func
662 _mesa_get_pack_ubyte_stencil_func(mesa_format format)
665 case MESA_FORMAT_S8_UINT_Z24_UNORM:
666 return pack_ubyte_stencil_Z24_S8;
667 case MESA_FORMAT_Z24_UNORM_S8_UINT:
668 return pack_ubyte_stencil_S8_Z24;
669 case MESA_FORMAT_S_UINT8:
670 return pack_ubyte_stencil_S8;
671 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
672 return pack_ubyte_stencil_Z32_FLOAT_X24S8;
674 unreachable("unexpected format in _mesa_pack_ubyte_stencil_func()");
681 _mesa_pack_float_z_row(mesa_format format, uint32_t n,
682 const float *src, void *dst)
685 case MESA_FORMAT_S8_UINT_Z24_UNORM:
686 case MESA_FORMAT_X8_UINT_Z24_UNORM:
688 /* don't disturb the stencil values */
689 uint32_t *d = ((uint32_t *) dst);
690 const double scale = (double) 0xffffff;
692 for (i = 0; i < n; i++) {
693 uint32_t s = d[i] & 0xff;
694 uint32_t z = (uint32_t) (src[i] * scale);
695 assert(z <= 0xffffff);
700 case MESA_FORMAT_Z24_UNORM_S8_UINT:
701 case MESA_FORMAT_Z24_UNORM_X8_UINT:
703 /* don't disturb the stencil values */
704 uint32_t *d = ((uint32_t *) dst);
705 const double scale = (double) 0xffffff;
707 for (i = 0; i < n; i++) {
708 uint32_t s = d[i] & 0xff000000;
709 uint32_t z = (uint32_t) (src[i] * scale);
710 assert(z <= 0xffffff);
715 case MESA_FORMAT_Z_UNORM16:
717 uint16_t *d = ((uint16_t *) dst);
718 const float scale = (float) 0xffff;
720 for (i = 0; i < n; i++) {
721 d[i] = (uint16_t) (src[i] * scale);
725 case MESA_FORMAT_Z_UNORM32:
727 uint32_t *d = ((uint32_t *) dst);
728 const double scale = (double) 0xffffffff;
730 for (i = 0; i < n; i++) {
731 d[i] = (uint32_t) (src[i] * scale);
735 case MESA_FORMAT_Z_FLOAT32:
736 memcpy(dst, src, n * sizeof(float));
738 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
740 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
742 for (i = 0; i < n; i++) {
748 unreachable("unexpected format in _mesa_pack_float_z_row()");
754 * The incoming Z values are always in the range [0, 0xffffffff].
757 _mesa_pack_uint_z_row(mesa_format format, uint32_t n,
758 const uint32_t *src, void *dst)
761 case MESA_FORMAT_S8_UINT_Z24_UNORM:
762 case MESA_FORMAT_X8_UINT_Z24_UNORM:
764 /* don't disturb the stencil values */
765 uint32_t *d = ((uint32_t *) dst);
767 for (i = 0; i < n; i++) {
768 uint32_t s = d[i] & 0xff;
769 uint32_t z = src[i] & 0xffffff00;
774 case MESA_FORMAT_Z24_UNORM_S8_UINT:
775 case MESA_FORMAT_Z24_UNORM_X8_UINT:
777 /* don't disturb the stencil values */
778 uint32_t *d = ((uint32_t *) dst);
780 for (i = 0; i < n; i++) {
781 uint32_t s = d[i] & 0xff000000;
782 uint32_t z = src[i] >> 8;
787 case MESA_FORMAT_Z_UNORM16:
789 uint16_t *d = ((uint16_t *) dst);
791 for (i = 0; i < n; i++) {
796 case MESA_FORMAT_Z_UNORM32:
797 memcpy(dst, src, n * sizeof(float));
799 case MESA_FORMAT_Z_FLOAT32:
801 uint32_t *d = ((uint32_t *) dst);
802 const double scale = 1.0 / (double) 0xffffffff;
804 for (i = 0; i < n; i++) {
805 d[i] = (uint32_t) (src[i] * scale);
806 assert(d[i] >= 0.0f);
807 assert(d[i] <= 1.0f);
811 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
813 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
814 const double scale = 1.0 / (double) 0xffffffff;
816 for (i = 0; i < n; i++) {
817 d[i].z = (float) (src[i] * scale);
818 assert(d[i].z >= 0.0f);
819 assert(d[i].z <= 1.0f);
824 unreachable("unexpected format in _mesa_pack_uint_z_row()");
830 _mesa_pack_ubyte_stencil_row(mesa_format format, uint32_t n,
831 const uint8_t *src, void *dst)
834 case MESA_FORMAT_S8_UINT_Z24_UNORM:
836 /* don't disturb the Z values */
837 uint32_t *d = ((uint32_t *) dst);
839 for (i = 0; i < n; i++) {
841 uint32_t z = d[i] & 0xffffff00;
846 case MESA_FORMAT_Z24_UNORM_S8_UINT:
848 /* don't disturb the Z values */
849 uint32_t *d = ((uint32_t *) dst);
851 for (i = 0; i < n; i++) {
852 uint32_t s = src[i] << 24;
853 uint32_t z = d[i] & 0xffffff;
858 case MESA_FORMAT_S_UINT8:
859 memcpy(dst, src, n * sizeof(uint8_t));
861 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
863 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
865 for (i = 0; i < n; i++) {
871 unreachable("unexpected format in _mesa_pack_ubyte_stencil_row()");
877 * Incoming Z/stencil values are always in uint_24_8 format.
880 _mesa_pack_uint_24_8_depth_stencil_row(mesa_format format, uint32_t n,
881 const uint32_t *src, void *dst)
884 case MESA_FORMAT_S8_UINT_Z24_UNORM:
885 memcpy(dst, src, n * sizeof(uint32_t));
887 case MESA_FORMAT_Z24_UNORM_S8_UINT:
889 uint32_t *d = ((uint32_t *) dst);
891 for (i = 0; i < n; i++) {
892 uint32_t s = src[i] << 24;
893 uint32_t z = src[i] >> 8;
898 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
900 const double scale = 1.0 / (double) 0xffffff;
901 struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
903 for (i = 0; i < n; i++) {
904 float z = (float) ((src[i] >> 8) * scale);
911 unreachable("bad format in _mesa_pack_ubyte_s_row");
917 template
= Template(string
, future_imports
=['division']);
919 print(template
.render(argv
= argv
[0:]))