4 /**************************************************************************
6 * Copyright 2009-2010 VMware, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sub license, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
25 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 **************************************************************************/
33 * Pixel format packing and unpacking functions.
35 * @author Jose Fonseca <jfonseca@vmware.com>
40 from u_format_parse import *
43 def generate_format_type(format):
44 '''Generate a structure that describes the format.'''
46 assert format.layout == PLAIN
48 print 'union util_format_%s {' % format.short_name()
50 if format.block_size() in (8, 16, 32, 64):
51 print ' uint%u_t value;' % (format.block_size(),)
54 for channel in format.channels:
55 if channel.size % 8 or not is_pot(channel.size):
59 for channel in format.channels:
61 if channel.type == VOID:
63 print ' unsigned %s:%u;' % (channel.name, channel.size)
64 elif channel.type == UNSIGNED:
65 print ' unsigned %s:%u;' % (channel.name, channel.size)
66 elif channel.type in (SIGNED, FIXED):
67 print ' int %s:%u;' % (channel.name, channel.size)
68 elif channel.type == FLOAT:
69 if channel.size == 64:
70 print ' double %s;' % (channel.name)
71 elif channel.size == 32:
72 print ' float %s;' % (channel.name)
74 print ' unsigned %s:%u;' % (channel.name, channel.size)
78 assert channel.size % 8 == 0 and is_pot(channel.size)
79 if channel.type == VOID:
81 print ' uint%u_t %s;' % (channel.size, channel.name)
82 elif channel.type == UNSIGNED:
83 print ' uint%u_t %s;' % (channel.size, channel.name)
84 elif channel.type in (SIGNED, FIXED):
85 print ' int%u_t %s;' % (channel.size, channel.name)
86 elif channel.type == FLOAT:
87 if channel.size == 64:
88 print ' double %s;' % (channel.name)
89 elif channel.size == 32:
90 print ' float %s;' % (channel.name)
91 elif channel.size == 16:
92 print ' uint16_t %s;' % (channel.name)
102 def bswap_format(format):
103 '''Generate a structure that describes the format.'''
105 if format.is_bitmask() and not format.is_array() and format.block_size() > 8:
106 print '#ifdef PIPE_ARCH_BIG_ENDIAN'
107 print ' pixel.value = util_bswap%u(pixel.value);' % format.block_size()
111 def is_format_supported(format):
112 '''Determines whether we actually have the plumbing necessary to generate the
113 to read/write to/from this format.'''
115 # FIXME: Ideally we would support any format combination here.
117 if format.layout != PLAIN:
121 channel = format.channels[i]
122 if channel.type not in (VOID, UNSIGNED, SIGNED, FLOAT, FIXED):
124 if channel.type == FLOAT and channel.size not in (16, 32, 64):
129 def is_format_pure_unsigned(format):
131 channel = format.channels[i]
132 if channel.type not in (VOID, UNSIGNED):
134 if channel.type == UNSIGNED and channel.pure == False:
140 def is_format_pure_signed(format):
142 channel = format.channels[i]
143 if channel.type not in (VOID, SIGNED):
145 if channel.type == SIGNED and channel.pure == False:
150 def native_type(format):
151 '''Get the native appropriate for a format.'''
153 if format.name == 'PIPE_FORMAT_R11G11B10_FLOAT':
155 if format.name == 'PIPE_FORMAT_R9G9B9E5_FLOAT':
158 if format.layout == PLAIN:
159 if not format.is_array():
160 # For arithmetic pixel formats return the integer type that matches the whole pixel
161 return 'uint%u_t' % format.block_size()
163 # For array pixel formats return the integer type that matches the color channel
164 channel = format.channels[0]
165 if channel.type in (UNSIGNED, VOID):
166 return 'uint%u_t' % channel.size
167 elif channel.type in (SIGNED, FIXED):
168 return 'int%u_t' % channel.size
169 elif channel.type == FLOAT:
170 if channel.size == 16:
172 elif channel.size == 32:
174 elif channel.size == 64:
184 def intermediate_native_type(bits, sign):
185 '''Find a native type adequate to hold intermediate results of the request bit size.'''
187 bytes = 4 # don't use anything smaller than 32bits
188 while bytes * 8 < bits:
193 return 'int%u_t' % bits
195 return 'uint%u_t' % bits
198 def get_one_shift(type):
199 '''Get the number of the bit that matches unity for this type.'''
200 if type.type == 'FLOAT':
204 if type.type == UNSIGNED:
206 if type.type == SIGNED:
208 if type.type == FIXED:
213 def value_to_native(type, value):
214 '''Get the value of unity for this type.'''
215 if type.type == FLOAT:
217 if type.type == FIXED:
218 return int(value * (1 << (type.size/2)))
221 if type.type == UNSIGNED:
222 return int(value * ((1 << type.size) - 1))
223 if type.type == SIGNED:
224 return int(value * ((1 << (type.size - 1)) - 1))
228 def native_to_constant(type, value):
229 '''Get the value of unity for this type.'''
230 if type.type == FLOAT:
236 return str(int(value))
240 '''Get the value of unity for this type.'''
241 return value_to_native(type, 1)
244 def clamp_expr(src_channel, dst_channel, dst_native_type, value):
245 '''Generate the expression to clamp the value in the source type to the
246 destination type range.'''
248 if src_channel == dst_channel:
251 src_min = src_channel.min()
252 src_max = src_channel.max()
253 dst_min = dst_channel.min()
254 dst_max = dst_channel.max()
256 # Translate the destination range to the src native value
257 dst_min_native = value_to_native(src_channel, dst_min)
258 dst_max_native = value_to_native(src_channel, dst_max)
260 if src_min < dst_min and src_max > dst_max:
261 return 'CLAMP(%s, %s, %s)' % (value, dst_min_native, dst_max_native)
263 if src_max > dst_max:
264 return 'MIN2(%s, %s)' % (value, dst_max_native)
266 if src_min < dst_min:
267 return 'MAX2(%s, %s)' % (value, dst_min_native)
272 def conversion_expr(src_channel,
273 dst_channel, dst_native_type,
276 src_colorspace = RGB,
277 dst_colorspace = RGB):
278 '''Generate the expression to convert a value between two types.'''
280 if src_colorspace != dst_colorspace:
281 if src_colorspace == SRGB:
282 assert src_channel.type == UNSIGNED
283 assert src_channel.norm
284 assert src_channel.size == 8
285 assert dst_colorspace == RGB
286 if dst_channel.type == FLOAT:
287 return 'util_format_srgb_8unorm_to_linear_float(%s)' % value
289 assert dst_channel.type == UNSIGNED
290 assert dst_channel.norm
291 assert dst_channel.size == 8
292 return 'util_format_srgb_to_linear_8unorm(%s)' % value
293 elif dst_colorspace == SRGB:
294 assert dst_channel.type == UNSIGNED
295 assert dst_channel.norm
296 assert dst_channel.size == 8
297 assert src_colorspace == RGB
298 if src_channel.type == FLOAT:
299 return 'util_format_linear_float_to_srgb_8unorm(%s)' % value
301 assert src_channel.type == UNSIGNED
302 assert src_channel.norm
303 assert src_channel.size == 8
304 return 'util_format_linear_to_srgb_8unorm(%s)' % value
305 elif src_colorspace == ZS:
307 elif dst_colorspace == ZS:
312 if src_channel == dst_channel:
315 src_type = src_channel.type
316 src_size = src_channel.size
317 src_norm = src_channel.norm
318 src_pure = src_channel.pure
320 # Promote half to float
321 if src_type == FLOAT and src_size == 16:
322 value = 'util_half_to_float(%s)' % value
325 # Special case for float <-> ubytes for more accurate results
326 # Done before clamping since these functions already take care of that
327 if src_type == UNSIGNED and src_norm and src_size == 8 and dst_channel.type == FLOAT and dst_channel.size == 32:
328 return 'ubyte_to_float(%s)' % value
329 if src_type == FLOAT and src_size == 32 and dst_channel.type == UNSIGNED and dst_channel.norm and dst_channel.size == 8:
330 return 'float_to_ubyte(%s)' % value
333 if dst_channel.type != FLOAT or src_type != FLOAT:
334 value = clamp_expr(src_channel, dst_channel, dst_native_type, value)
336 if src_type in (SIGNED, UNSIGNED) and dst_channel.type in (SIGNED, UNSIGNED):
337 if not src_norm and not dst_channel.norm:
338 # neither is normalized -- just cast
339 return '(%s)%s' % (dst_native_type, value)
341 src_one = get_one(src_channel)
342 dst_one = get_one(dst_channel)
344 if src_one > dst_one and src_norm and dst_channel.norm:
345 # We can just bitshift
346 src_shift = get_one_shift(src_channel)
347 dst_shift = get_one_shift(dst_channel)
348 value = '(%s >> %s)' % (value, src_shift - dst_shift)
350 # We need to rescale using an intermediate type big enough to hold the multiplication of both
351 tmp_native_type = intermediate_native_type(src_size + dst_channel.size, src_channel.sign and dst_channel.sign)
352 value = '((%s)%s)' % (tmp_native_type, value)
353 value = '(%s * 0x%x / 0x%x)' % (value, dst_one, src_one)
354 value = '(%s)%s' % (dst_native_type, value)
357 # Promote to either float or double
358 if src_type != FLOAT:
359 if src_norm or src_type == FIXED:
360 one = get_one(src_channel)
362 value = '(%s * (1.0f/0x%x))' % (value, one)
363 if dst_channel.size <= 32:
364 value = '(float)%s' % value
367 # bigger than single precision mantissa, use double
368 value = '(%s * (1.0/0x%x))' % (value, one)
372 if src_size <= 23 or dst_channel.size <= 32:
373 value = '(float)%s' % value
376 # bigger than single precision mantissa, use double
377 value = '(double)%s' % value
381 # Convert double or float to non-float
382 if dst_channel.type != FLOAT:
383 if dst_channel.norm or dst_channel.type == FIXED:
384 dst_one = get_one(dst_channel)
385 if dst_channel.size <= 23:
386 value = '(%s * 0x%x)' % (value, dst_one)
388 # bigger than single precision mantissa, use double
389 value = '(%s * (double)0x%x)' % (value, dst_one)
390 value = '(%s)%s' % (dst_native_type, value)
392 # Cast double to float when converting to either half or float
393 if dst_channel.size <= 32 and src_size > 32:
394 value = '(float)%s' % value
397 if dst_channel.size == 16:
398 value = 'util_float_to_half(%s)' % value
399 elif dst_channel.size == 64 and src_size < 64:
400 value = '(double)%s' % value
405 def generate_unpack_kernel(format, dst_channel, dst_native_type):
407 if not is_format_supported(format):
410 assert format.layout == PLAIN
412 src_native_type = native_type(format)
414 if format.is_bitmask():
415 depth = format.block_size()
416 print ' uint%u_t value = *(const uint%u_t *)src;' % (depth, depth)
418 # Declare the intermediate variables
419 for i in range(format.nr_channels()):
420 src_channel = format.channels[i]
421 if src_channel.type == UNSIGNED:
422 print ' uint%u_t %s;' % (depth, src_channel.name)
423 elif src_channel.type == SIGNED:
424 print ' int%u_t %s;' % (depth, src_channel.name)
427 print '#ifdef PIPE_ARCH_BIG_ENDIAN'
428 print ' value = util_bswap%u(value);' % depth
431 # Compute the intermediate unshifted values
433 for i in range(format.nr_channels()):
434 src_channel = format.channels[i]
436 if src_channel.type == UNSIGNED:
438 value = '%s >> %u' % (value, shift)
439 if shift + src_channel.size < depth:
440 value = '(%s) & 0x%x' % (value, (1 << src_channel.size) - 1)
441 elif src_channel.type == SIGNED:
442 if shift + src_channel.size < depth:
444 lshift = depth - (shift + src_channel.size)
445 value = '%s << %u' % (value, lshift)
447 value = '(int%u_t)(%s) ' % (depth, value)
448 if src_channel.size < depth:
450 rshift = depth - src_channel.size
451 value = '(%s) >> %u' % (value, rshift)
455 if value is not None:
456 print ' %s = %s;' % (src_channel.name, value)
458 shift += src_channel.size
460 # Convert, swizzle, and store final values
462 swizzle = format.swizzles[i]
464 src_channel = format.channels[swizzle]
465 src_colorspace = format.colorspace
466 if src_colorspace == SRGB and i == 3:
467 # Alpha channel is linear
469 value = src_channel.name
470 value = conversion_expr(src_channel,
471 dst_channel, dst_native_type,
473 src_colorspace = src_colorspace)
474 elif swizzle == SWIZZLE_0:
476 elif swizzle == SWIZZLE_1:
477 value = get_one(dst_channel)
478 elif swizzle == SWIZZLE_NONE:
482 print ' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i])
485 print ' union util_format_%s pixel;' % format.short_name()
486 print ' memcpy(&pixel, src, sizeof pixel);'
490 swizzle = format.swizzles[i]
492 src_channel = format.channels[swizzle]
493 src_colorspace = format.colorspace
494 if src_colorspace == SRGB and i == 3:
495 # Alpha channel is linear
497 value = 'pixel.chan.%s' % src_channel.name
498 value = conversion_expr(src_channel,
499 dst_channel, dst_native_type,
501 src_colorspace = src_colorspace)
502 elif swizzle == SWIZZLE_0:
504 elif swizzle == SWIZZLE_1:
505 value = get_one(dst_channel)
506 elif swizzle == SWIZZLE_NONE:
510 print ' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i])
513 def generate_pack_kernel(format, src_channel, src_native_type):
515 if not is_format_supported(format):
518 dst_native_type = native_type(format)
520 assert format.layout == PLAIN
522 inv_swizzle = format.inv_swizzles()
524 if format.is_bitmask():
525 depth = format.block_size()
526 print ' uint%u_t value = 0;' % depth
530 dst_channel = format.channels[i]
531 if inv_swizzle[i] is not None:
532 value ='src[%u]' % inv_swizzle[i]
533 dst_colorspace = format.colorspace
534 if dst_colorspace == SRGB and inv_swizzle[i] == 3:
535 # Alpha channel is linear
537 value = conversion_expr(src_channel,
538 dst_channel, dst_native_type,
540 dst_colorspace = dst_colorspace)
541 if dst_channel.type in (UNSIGNED, SIGNED):
542 if shift + dst_channel.size < depth:
543 value = '(%s) & 0x%x' % (value, (1 << dst_channel.size) - 1)
545 value = '(%s) << %u' % (value, shift)
546 if dst_channel.type == SIGNED:
548 value = '(uint%u_t)(%s) ' % (depth, value)
551 if value is not None:
552 print ' value |= %s;' % (value)
554 shift += dst_channel.size
557 print '#ifdef PIPE_ARCH_BIG_ENDIAN'
558 print ' value = util_bswap%u(value);' % depth
561 print ' *(uint%u_t *)dst = value;' % depth
564 print ' union util_format_%s pixel;' % format.short_name()
567 dst_channel = format.channels[i]
568 width = dst_channel.size
569 if inv_swizzle[i] is None:
571 dst_colorspace = format.colorspace
572 if dst_colorspace == SRGB and inv_swizzle[i] == 3:
573 # Alpha channel is linear
575 value ='src[%u]' % inv_swizzle[i]
576 value = conversion_expr(src_channel,
577 dst_channel, dst_native_type,
579 dst_colorspace = dst_colorspace)
580 print ' pixel.chan.%s = %s;' % (dst_channel.name, value)
583 print ' memcpy(dst, &pixel, sizeof pixel);'
586 def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix):
587 '''Generate the function to unpack pixels from a particular format'''
589 name = format.short_name()
591 print 'static INLINE void'
592 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)
595 if is_format_supported(format):
596 print ' unsigned x, y;'
597 print ' for(y = 0; y < height; y += %u) {' % (format.block_height,)
598 print ' %s *dst = dst_row;' % (dst_native_type)
599 print ' const uint8_t *src = src_row;'
600 print ' for(x = 0; x < width; x += %u) {' % (format.block_width,)
602 generate_unpack_kernel(format, dst_channel, dst_native_type)
604 print ' src += %u;' % (format.block_size() / 8,)
607 print ' src_row += src_stride;'
608 print ' dst_row += dst_stride/sizeof(*dst_row);'
615 def generate_format_pack(format, src_channel, src_native_type, src_suffix):
616 '''Generate the function to pack pixels to a particular format'''
618 name = format.short_name()
620 print 'static INLINE void'
621 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)
624 if is_format_supported(format):
625 print ' unsigned x, y;'
626 print ' for(y = 0; y < height; y += %u) {' % (format.block_height,)
627 print ' const %s *src = src_row;' % (src_native_type)
628 print ' uint8_t *dst = dst_row;'
629 print ' for(x = 0; x < width; x += %u) {' % (format.block_width,)
631 generate_pack_kernel(format, src_channel, src_native_type)
634 print ' dst += %u;' % (format.block_size() / 8,)
636 print ' dst_row += dst_stride;'
637 print ' src_row += src_stride/sizeof(*src_row);'
644 def generate_format_fetch(format, dst_channel, dst_native_type, dst_suffix):
645 '''Generate the function to unpack pixels from a particular format'''
647 name = format.short_name()
649 print 'static INLINE void'
650 print 'util_format_%s_fetch_%s(%s *dst, const uint8_t *src, unsigned i, unsigned j)' % (name, dst_suffix, dst_native_type)
653 if is_format_supported(format):
654 generate_unpack_kernel(format, dst_channel, dst_native_type)
660 def is_format_hand_written(format):
661 return format.layout in ('s3tc', 'rgtc', 'etc', 'subsampled', 'other') or format.colorspace == ZS
664 def generate(formats):
666 print '#include "pipe/p_compiler.h"'
667 print '#include "u_math.h"'
668 print '#include "u_half.h"'
669 print '#include "u_format.h"'
670 print '#include "u_format_other.h"'
671 print '#include "u_format_srgb.h"'
672 print '#include "u_format_yuv.h"'
673 print '#include "u_format_zs.h"'
676 for format in formats:
677 if not is_format_hand_written(format):
679 if is_format_supported(format):
680 generate_format_type(format)
682 if is_format_pure_unsigned(format):
683 native_type = 'unsigned'
685 channel = Channel(UNSIGNED, False, True, 32)
687 generate_format_unpack(format, channel, native_type, suffix)
688 generate_format_pack(format, channel, native_type, suffix)
689 generate_format_fetch(format, channel, native_type, suffix)
691 channel = Channel(SIGNED, False, True, 32)
694 generate_format_unpack(format, channel, native_type, suffix)
695 generate_format_pack(format, channel, native_type, suffix)
696 elif is_format_pure_signed(format):
699 channel = Channel(SIGNED, False, True, 32)
701 generate_format_unpack(format, channel, native_type, suffix)
702 generate_format_pack(format, channel, native_type, suffix)
703 generate_format_fetch(format, channel, native_type, suffix)
705 native_type = 'unsigned'
707 channel = Channel(UNSIGNED, False, True, 32)
708 generate_format_unpack(format, channel, native_type, suffix)
709 generate_format_pack(format, channel, native_type, suffix)
711 channel = Channel(FLOAT, False, False, 32)
712 native_type = 'float'
713 suffix = 'rgba_float'
715 generate_format_unpack(format, channel, native_type, suffix)
716 generate_format_pack(format, channel, native_type, suffix)
717 generate_format_fetch(format, channel, native_type, suffix)
719 channel = Channel(UNSIGNED, True, False, 8)
720 native_type = 'uint8_t'
721 suffix = 'rgba_8unorm'
723 generate_format_unpack(format, channel, native_type, suffix)
724 generate_format_pack(format, channel, native_type, suffix)