3 /**************************************************************************
5 * Copyright 2009-2010 VMware, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
32 * Pixel format packing and unpacking functions.
34 * @author Jose Fonseca <jfonseca@vmware.com>
39 from u_format_parse
import *
42 def inv_swizzles(swizzles
):
43 '''Return an array[4] of inverse swizzle terms'''
44 '''Only pick the first matching value to avoid l8 getting blue and i8 getting alpha'''
45 inv_swizzle
= [None]*4
48 if swizzle
< 4 and inv_swizzle
[swizzle
] == None:
49 inv_swizzle
[swizzle
] = i
52 def print_channels(format
, func
):
53 if format
.nr_channels() <= 1:
54 func(format
.le_channels
, format
.le_swizzles
)
56 print '#ifdef PIPE_ARCH_BIG_ENDIAN'
57 func(format
.be_channels
, format
.be_swizzles
)
59 func(format
.le_channels
, format
.le_swizzles
)
62 def generate_format_type(format
):
63 '''Generate a structure that describes the format.'''
65 assert format
.layout
== PLAIN
67 def generate_bitfields(channels
, swizzles
):
68 for channel
in channels
:
69 if channel
.type == VOID
:
71 print ' unsigned %s:%u;' % (channel
.name
, channel
.size
)
72 elif channel
.type == UNSIGNED
:
73 print ' unsigned %s:%u;' % (channel
.name
, channel
.size
)
74 elif channel
.type in (SIGNED
, FIXED
):
75 print ' int %s:%u;' % (channel
.name
, channel
.size
)
76 elif channel
.type == FLOAT
:
77 if channel
.size
== 64:
78 print ' double %s;' % (channel
.name
)
79 elif channel
.size
== 32:
80 print ' float %s;' % (channel
.name
)
82 print ' unsigned %s:%u;' % (channel
.name
, channel
.size
)
86 def generate_full_fields(channels
, swizzles
):
87 for channel
in channels
:
88 assert channel
.size
% 8 == 0 and is_pot(channel
.size
)
89 if channel
.type == VOID
:
91 print ' uint%u_t %s;' % (channel
.size
, channel
.name
)
92 elif channel
.type == UNSIGNED
:
93 print ' uint%u_t %s;' % (channel
.size
, channel
.name
)
94 elif channel
.type in (SIGNED
, FIXED
):
95 print ' int%u_t %s;' % (channel
.size
, channel
.name
)
96 elif channel
.type == FLOAT
:
97 if channel
.size
== 64:
98 print ' double %s;' % (channel
.name
)
99 elif channel
.size
== 32:
100 print ' float %s;' % (channel
.name
)
101 elif channel
.size
== 16:
102 print ' uint16_t %s;' % (channel
.name
)
108 print 'union util_format_%s {' % format
.short_name()
110 if format
.block_size() in (8, 16, 32, 64):
111 print ' uint%u_t value;' % (format
.block_size(),)
113 use_bitfields
= False
114 for channel
in format
.le_channels
:
115 if channel
.size
% 8 or not is_pot(channel
.size
):
120 print_channels(format
, generate_bitfields
)
122 print_channels(format
, generate_full_fields
)
128 def is_format_supported(format
):
129 '''Determines whether we actually have the plumbing necessary to generate the
130 to read/write to/from this format.'''
132 # FIXME: Ideally we would support any format combination here.
134 if format
.layout
!= PLAIN
:
138 channel
= format
.le_channels
[i
]
139 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
, FLOAT
, FIXED
):
141 if channel
.type == FLOAT
and channel
.size
not in (16, 32, 64):
146 def native_type(format
):
147 '''Get the native appropriate for a format.'''
149 if format
.name
== 'PIPE_FORMAT_R11G11B10_FLOAT':
151 if format
.name
== 'PIPE_FORMAT_R9G9B9E5_FLOAT':
154 if format
.layout
== PLAIN
:
155 if not format
.is_array():
156 # For arithmetic pixel formats return the integer type that matches the whole pixel
157 return 'uint%u_t' % format
.block_size()
159 # For array pixel formats return the integer type that matches the color channel
160 channel
= format
.array_element()
161 if channel
.type in (UNSIGNED
, VOID
):
162 return 'uint%u_t' % channel
.size
163 elif channel
.type in (SIGNED
, FIXED
):
164 return 'int%u_t' % channel
.size
165 elif channel
.type == FLOAT
:
166 if channel
.size
== 16:
168 elif channel
.size
== 32:
170 elif channel
.size
== 64:
180 def intermediate_native_type(bits
, sign
):
181 '''Find a native type adequate to hold intermediate results of the request bit size.'''
183 bytes
= 4 # don't use anything smaller than 32bits
184 while bytes
* 8 < bits
:
189 return 'int%u_t' % bits
191 return 'uint%u_t' % bits
194 def get_one_shift(type):
195 '''Get the number of the bit that matches unity for this type.'''
196 if type.type == 'FLOAT':
200 if type.type == UNSIGNED
:
202 if type.type == SIGNED
:
204 if type.type == FIXED
:
209 def truncate_mantissa(x
, bits
):
210 '''Truncate an integer so it can be represented exactly with a floating
213 assert isinstance(x
, (int, long))
220 # We can represent integers up to mantissa + 1 bits exactly
221 mask
= (1 << (bits
+ 1)) - 1
223 # Slide the mask until the MSB matches
225 while (x
>> shift
) & ~mask
:
233 def value_to_native(type, value
):
234 '''Get the value of unity for this type.'''
235 if type.type == FLOAT
:
237 and isinstance(value
, (int, long)):
238 return truncate_mantissa(value
, 23)
240 if type.type == FIXED
:
241 return int(value
* (1 << (type.size
/2)))
244 if type.type == UNSIGNED
:
245 return int(value
* ((1 << type.size
) - 1))
246 if type.type == SIGNED
:
247 return int(value
* ((1 << (type.size
- 1)) - 1))
251 def native_to_constant(type, value
):
252 '''Get the value of unity for this type.'''
253 if type.type == FLOAT
:
255 return "%.1ff" % float(value
)
257 return "%.1f" % float(value
)
259 return str(int(value
))
263 '''Get the value of unity for this type.'''
264 return value_to_native(type, 1)
267 def clamp_expr(src_channel
, dst_channel
, dst_native_type
, value
):
268 '''Generate the expression to clamp the value in the source type to the
269 destination type range.'''
271 if src_channel
== dst_channel
:
274 src_min
= src_channel
.min()
275 src_max
= src_channel
.max()
276 dst_min
= dst_channel
.min()
277 dst_max
= dst_channel
.max()
279 # Translate the destination range to the src native value
280 dst_min_native
= native_to_constant(src_channel
, value_to_native(src_channel
, dst_min
))
281 dst_max_native
= native_to_constant(src_channel
, value_to_native(src_channel
, dst_max
))
283 if src_min
< dst_min
and src_max
> dst_max
:
284 return 'CLAMP(%s, %s, %s)' % (value
, dst_min_native
, dst_max_native
)
286 if src_max
> dst_max
:
287 return 'MIN2(%s, %s)' % (value
, dst_max_native
)
289 if src_min
< dst_min
:
290 return 'MAX2(%s, %s)' % (value
, dst_min_native
)
295 def conversion_expr(src_channel
,
296 dst_channel
, dst_native_type
,
299 src_colorspace
= RGB
,
300 dst_colorspace
= RGB
):
301 '''Generate the expression to convert a value between two types.'''
303 if src_colorspace
!= dst_colorspace
:
304 if src_colorspace
== SRGB
:
305 assert src_channel
.type == UNSIGNED
306 assert src_channel
.norm
307 assert src_channel
.size
<= 8
308 assert src_channel
.size
>= 4
309 assert dst_colorspace
== RGB
310 if src_channel
.size
< 8:
311 value
= '%s << %x | %s >> %x' % (value
, 8 - src_channel
.size
, value
, 2 * src_channel
.size
- 8)
312 if dst_channel
.type == FLOAT
:
313 return 'util_format_srgb_8unorm_to_linear_float(%s)' % value
315 assert dst_channel
.type == UNSIGNED
316 assert dst_channel
.norm
317 assert dst_channel
.size
== 8
318 return 'util_format_srgb_to_linear_8unorm(%s)' % value
319 elif dst_colorspace
== SRGB
:
320 assert dst_channel
.type == UNSIGNED
321 assert dst_channel
.norm
322 assert dst_channel
.size
<= 8
323 assert src_colorspace
== RGB
324 if src_channel
.type == FLOAT
:
325 value
= 'util_format_linear_float_to_srgb_8unorm(%s)' % value
327 assert src_channel
.type == UNSIGNED
328 assert src_channel
.norm
329 assert src_channel
.size
== 8
330 value
= 'util_format_linear_to_srgb_8unorm(%s)' % value
331 # XXX rounding is all wrong.
332 if dst_channel
.size
< 8:
333 return '%s >> %x' % (value
, 8 - dst_channel
.size
)
336 elif src_colorspace
== ZS
:
338 elif dst_colorspace
== ZS
:
343 if src_channel
== dst_channel
:
346 src_type
= src_channel
.type
347 src_size
= src_channel
.size
348 src_norm
= src_channel
.norm
349 src_pure
= src_channel
.pure
351 # Promote half to float
352 if src_type
== FLOAT
and src_size
== 16:
353 value
= 'util_half_to_float(%s)' % value
356 # Special case for float <-> ubytes for more accurate results
357 # Done before clamping since these functions already take care of that
358 if src_type
== UNSIGNED
and src_norm
and src_size
== 8 and dst_channel
.type == FLOAT
and dst_channel
.size
== 32:
359 return 'ubyte_to_float(%s)' % value
360 if src_type
== FLOAT
and src_size
== 32 and dst_channel
.type == UNSIGNED
and dst_channel
.norm
and dst_channel
.size
== 8:
361 return 'float_to_ubyte(%s)' % value
364 if dst_channel
.type != FLOAT
or src_type
!= FLOAT
:
365 value
= clamp_expr(src_channel
, dst_channel
, dst_native_type
, value
)
367 if src_type
in (SIGNED
, UNSIGNED
) and dst_channel
.type in (SIGNED
, UNSIGNED
):
368 if not src_norm
and not dst_channel
.norm
:
369 # neither is normalized -- just cast
370 return '(%s)%s' % (dst_native_type
, value
)
372 src_one
= get_one(src_channel
)
373 dst_one
= get_one(dst_channel
)
375 if src_one
> dst_one
and src_norm
and dst_channel
.norm
:
376 # We can just bitshift
377 src_shift
= get_one_shift(src_channel
)
378 dst_shift
= get_one_shift(dst_channel
)
379 value
= '(%s >> %s)' % (value
, src_shift
- dst_shift
)
381 # We need to rescale using an intermediate type big enough to hold the multiplication of both
382 tmp_native_type
= intermediate_native_type(src_size
+ dst_channel
.size
, src_channel
.sign
and dst_channel
.sign
)
383 value
= '((%s)%s)' % (tmp_native_type
, value
)
384 value
= '(%s * 0x%x / 0x%x)' % (value
, dst_one
, src_one
)
385 value
= '(%s)%s' % (dst_native_type
, value
)
388 # Promote to either float or double
389 if src_type
!= FLOAT
:
390 if src_norm
or src_type
== FIXED
:
391 one
= get_one(src_channel
)
393 value
= '(%s * (1.0f/0x%x))' % (value
, one
)
394 if dst_channel
.size
<= 32:
395 value
= '(float)%s' % value
398 # bigger than single precision mantissa, use double
399 value
= '(%s * (1.0/0x%x))' % (value
, one
)
403 if src_size
<= 23 or dst_channel
.size
<= 32:
404 value
= '(float)%s' % value
407 # bigger than single precision mantissa, use double
408 value
= '(double)%s' % value
412 # Convert double or float to non-float
413 if dst_channel
.type != FLOAT
:
414 if dst_channel
.norm
or dst_channel
.type == FIXED
:
415 dst_one
= get_one(dst_channel
)
416 if dst_channel
.size
<= 23:
417 value
= 'util_iround(%s * 0x%x)' % (value
, dst_one
)
419 # bigger than single precision mantissa, use double
420 value
= '(%s * (double)0x%x)' % (value
, dst_one
)
421 value
= '(%s)%s' % (dst_native_type
, value
)
423 # Cast double to float when converting to either half or float
424 if dst_channel
.size
<= 32 and src_size
> 32:
425 value
= '(float)%s' % value
428 if dst_channel
.size
== 16:
429 value
= 'util_float_to_half(%s)' % value
430 elif dst_channel
.size
== 64 and src_size
< 64:
431 value
= '(double)%s' % value
436 def generate_unpack_kernel(format
, dst_channel
, dst_native_type
):
438 if not is_format_supported(format
):
441 assert format
.layout
== PLAIN
443 src_native_type
= native_type(format
)
445 def unpack_from_bitmask(channels
, swizzles
):
446 depth
= format
.block_size()
447 print ' uint%u_t value = *(const uint%u_t *)src;' % (depth
, depth
)
449 # Declare the intermediate variables
450 for i
in range(format
.nr_channels()):
451 src_channel
= channels
[i
]
452 if src_channel
.type == UNSIGNED
:
453 print ' uint%u_t %s;' % (depth
, src_channel
.name
)
454 elif src_channel
.type == SIGNED
:
455 print ' int%u_t %s;' % (depth
, src_channel
.name
)
457 # Compute the intermediate unshifted values
458 for i
in range(format
.nr_channels()):
459 src_channel
= channels
[i
]
461 shift
= src_channel
.shift
462 if src_channel
.type == UNSIGNED
:
464 value
= '%s >> %u' % (value
, shift
)
465 if shift
+ src_channel
.size
< depth
:
466 value
= '(%s) & 0x%x' % (value
, (1 << src_channel
.size
) - 1)
467 elif src_channel
.type == SIGNED
:
468 if shift
+ src_channel
.size
< depth
:
470 lshift
= depth
- (shift
+ src_channel
.size
)
471 value
= '%s << %u' % (value
, lshift
)
473 value
= '(int%u_t)(%s) ' % (depth
, value
)
474 if src_channel
.size
< depth
:
476 rshift
= depth
- src_channel
.size
477 value
= '(%s) >> %u' % (value
, rshift
)
481 if value
is not None:
482 print ' %s = %s;' % (src_channel
.name
, value
)
484 # Convert, swizzle, and store final values
486 swizzle
= swizzles
[i
]
488 src_channel
= channels
[swizzle
]
489 src_colorspace
= format
.colorspace
490 if src_colorspace
== SRGB
and i
== 3:
491 # Alpha channel is linear
493 value
= src_channel
.name
494 value
= conversion_expr(src_channel
,
495 dst_channel
, dst_native_type
,
497 src_colorspace
= src_colorspace
)
498 elif swizzle
== SWIZZLE_0
:
500 elif swizzle
== SWIZZLE_1
:
501 value
= get_one(dst_channel
)
502 elif swizzle
== SWIZZLE_NONE
:
506 print ' dst[%u] = %s; /* %s */' % (i
, value
, 'rgba'[i
])
508 def unpack_from_union(channels
, swizzles
):
509 print ' union util_format_%s pixel;' % format
.short_name()
510 print ' memcpy(&pixel, src, sizeof pixel);'
513 swizzle
= swizzles
[i
]
515 src_channel
= channels
[swizzle
]
516 src_colorspace
= format
.colorspace
517 if src_colorspace
== SRGB
and i
== 3:
518 # Alpha channel is linear
520 value
= 'pixel.chan.%s' % src_channel
.name
521 value
= conversion_expr(src_channel
,
522 dst_channel
, dst_native_type
,
524 src_colorspace
= src_colorspace
)
525 elif swizzle
== SWIZZLE_0
:
527 elif swizzle
== SWIZZLE_1
:
528 value
= get_one(dst_channel
)
529 elif swizzle
== SWIZZLE_NONE
:
533 print ' dst[%u] = %s; /* %s */' % (i
, value
, 'rgba'[i
])
535 if format
.is_bitmask():
536 print_channels(format
, unpack_from_bitmask
)
538 print_channels(format
, unpack_from_union
)
541 def generate_pack_kernel(format
, src_channel
, src_native_type
):
543 if not is_format_supported(format
):
546 dst_native_type
= native_type(format
)
548 assert format
.layout
== PLAIN
550 def pack_into_bitmask(channels
, swizzles
):
551 inv_swizzle
= inv_swizzles(swizzles
)
553 depth
= format
.block_size()
554 print ' uint%u_t value = 0;' % depth
557 dst_channel
= channels
[i
]
558 shift
= dst_channel
.shift
559 if inv_swizzle
[i
] is not None:
560 value
='src[%u]' % inv_swizzle
[i
]
561 dst_colorspace
= format
.colorspace
562 if dst_colorspace
== SRGB
and inv_swizzle
[i
] == 3:
563 # Alpha channel is linear
565 value
= conversion_expr(src_channel
,
566 dst_channel
, dst_native_type
,
568 dst_colorspace
= dst_colorspace
)
569 if dst_channel
.type in (UNSIGNED
, SIGNED
):
570 if shift
+ dst_channel
.size
< depth
:
571 value
= '(%s) & 0x%x' % (value
, (1 << dst_channel
.size
) - 1)
573 value
= '(%s) << %u' % (value
, shift
)
574 if dst_channel
.type == SIGNED
:
576 value
= '(uint%u_t)(%s) ' % (depth
, value
)
579 if value
is not None:
580 print ' value |= %s;' % (value
)
582 print ' *(uint%u_t *)dst = value;' % depth
584 def pack_into_union(channels
, swizzles
):
585 inv_swizzle
= inv_swizzles(swizzles
)
587 print ' union util_format_%s pixel;' % format
.short_name()
590 dst_channel
= channels
[i
]
591 width
= dst_channel
.size
592 if inv_swizzle
[i
] is None:
594 dst_colorspace
= format
.colorspace
595 if dst_colorspace
== SRGB
and inv_swizzle
[i
] == 3:
596 # Alpha channel is linear
598 value
='src[%u]' % inv_swizzle
[i
]
599 value
= conversion_expr(src_channel
,
600 dst_channel
, dst_native_type
,
602 dst_colorspace
= dst_colorspace
)
603 print ' pixel.chan.%s = %s;' % (dst_channel
.name
, value
)
605 print ' memcpy(dst, &pixel, sizeof pixel);'
607 if format
.is_bitmask():
608 print_channels(format
, pack_into_bitmask
)
610 print_channels(format
, pack_into_union
)
613 def generate_format_unpack(format
, dst_channel
, dst_native_type
, dst_suffix
):
614 '''Generate the function to unpack pixels from a particular format'''
616 name
= format
.short_name()
618 print 'static inline void'
619 print 'util_format_%s_unpack_%s(%s *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name
, dst_suffix
, dst_native_type
)
622 if is_format_supported(format
):
623 print ' unsigned x, y;'
624 print ' for(y = 0; y < height; y += %u) {' % (format
.block_height
,)
625 print ' %s *dst = dst_row;' % (dst_native_type
)
626 print ' const uint8_t *src = src_row;'
627 print ' for(x = 0; x < width; x += %u) {' % (format
.block_width
,)
629 generate_unpack_kernel(format
, dst_channel
, dst_native_type
)
631 print ' src += %u;' % (format
.block_size() / 8,)
634 print ' src_row += src_stride;'
635 print ' dst_row += dst_stride/sizeof(*dst_row);'
642 def generate_format_pack(format
, src_channel
, src_native_type
, src_suffix
):
643 '''Generate the function to pack pixels to a particular format'''
645 name
= format
.short_name()
647 print 'static inline void'
648 print 'util_format_%s_pack_%s(uint8_t *dst_row, unsigned dst_stride, const %s *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name
, src_suffix
, src_native_type
)
651 if is_format_supported(format
):
652 print ' unsigned x, y;'
653 print ' for(y = 0; y < height; y += %u) {' % (format
.block_height
,)
654 print ' const %s *src = src_row;' % (src_native_type
)
655 print ' uint8_t *dst = dst_row;'
656 print ' for(x = 0; x < width; x += %u) {' % (format
.block_width
,)
658 generate_pack_kernel(format
, src_channel
, src_native_type
)
661 print ' dst += %u;' % (format
.block_size() / 8,)
663 print ' dst_row += dst_stride;'
664 print ' src_row += src_stride/sizeof(*src_row);'
671 def generate_format_fetch(format
, dst_channel
, dst_native_type
, dst_suffix
):
672 '''Generate the function to unpack pixels from a particular format'''
674 name
= format
.short_name()
676 print 'static inline void'
677 print 'util_format_%s_fetch_%s(%s *dst, const uint8_t *src, unsigned i, unsigned j)' % (name
, dst_suffix
, dst_native_type
)
680 if is_format_supported(format
):
681 generate_unpack_kernel(format
, dst_channel
, dst_native_type
)
687 def is_format_hand_written(format
):
688 return format
.layout
in ('s3tc', 'rgtc', 'etc', 'bptc', 'astc', 'subsampled', 'other') or format
.colorspace
== ZS
691 def generate(formats
):
693 print '#include "pipe/p_compiler.h"'
694 print '#include "u_math.h"'
695 print '#include "u_half.h"'
696 print '#include "u_format.h"'
697 print '#include "u_format_other.h"'
698 print '#include "util/format_srgb.h"'
699 print '#include "u_format_yuv.h"'
700 print '#include "u_format_zs.h"'
703 for format
in formats
:
704 if not is_format_hand_written(format
):
706 if is_format_supported(format
):
707 generate_format_type(format
)
709 if format
.is_pure_unsigned():
710 native_type
= 'unsigned'
712 channel
= Channel(UNSIGNED
, False, True, 32)
714 generate_format_unpack(format
, channel
, native_type
, suffix
)
715 generate_format_pack(format
, channel
, native_type
, suffix
)
716 generate_format_fetch(format
, channel
, native_type
, suffix
)
718 channel
= Channel(SIGNED
, False, True, 32)
721 generate_format_unpack(format
, channel
, native_type
, suffix
)
722 generate_format_pack(format
, channel
, native_type
, suffix
)
723 elif format
.is_pure_signed():
726 channel
= Channel(SIGNED
, False, True, 32)
728 generate_format_unpack(format
, channel
, native_type
, suffix
)
729 generate_format_pack(format
, channel
, native_type
, suffix
)
730 generate_format_fetch(format
, channel
, native_type
, suffix
)
732 native_type
= 'unsigned'
734 channel
= Channel(UNSIGNED
, False, True, 32)
735 generate_format_unpack(format
, channel
, native_type
, suffix
)
736 generate_format_pack(format
, channel
, native_type
, suffix
)
738 channel
= Channel(FLOAT
, False, False, 32)
739 native_type
= 'float'
740 suffix
= 'rgba_float'
742 generate_format_unpack(format
, channel
, native_type
, suffix
)
743 generate_format_pack(format
, channel
, native_type
, suffix
)
744 generate_format_fetch(format
, channel
, native_type
, suffix
)
746 channel
= Channel(UNSIGNED
, True, False, 8)
747 native_type
= 'uint8_t'
748 suffix
= 'rgba_8unorm'
750 generate_format_unpack(format
, channel
, native_type
, suffix
)
751 generate_format_pack(format
, channel
, native_type
, suffix
)