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):
130 def native_type(format
):
131 '''Get the native appropriate for a format.'''
133 if format
.layout
== PLAIN
:
134 if not format
.is_array():
135 # For arithmetic pixel formats return the integer type that matches the whole pixel
136 return 'uint%u_t' % format
.block_size()
138 # For array pixel formats return the integer type that matches the color channel
139 channel
= format
.channels
[0]
140 if channel
.type in (UNSIGNED
, VOID
):
141 return 'uint%u_t' % channel
.size
142 elif channel
.type in (SIGNED
, FIXED
):
143 return 'int%u_t' % channel
.size
144 elif channel
.type == FLOAT
:
145 if channel
.size
== 16:
147 elif channel
.size
== 32:
149 elif channel
.size
== 64:
159 def intermediate_native_type(bits
, sign
):
160 '''Find a native type adequate to hold intermediate results of the request bit size.'''
162 bytes
= 4 # don't use anything smaller than 32bits
163 while bytes
* 8 < bits
:
168 return 'int%u_t' % bits
170 return 'uint%u_t' % bits
173 def get_one_shift(type):
174 '''Get the number of the bit that matches unity for this type.'''
175 if type.type == 'FLOAT':
179 if type.type == UNSIGNED
:
181 if type.type == SIGNED
:
183 if type.type == FIXED
:
188 def value_to_native(type, value
):
189 '''Get the value of unity for this type.'''
190 if type.type == FLOAT
:
192 if type.type == FIXED
:
193 return int(value
* (1 << (type.size
/2)))
196 if type.type == UNSIGNED
:
197 return int(value
* ((1 << type.size
) - 1))
198 if type.type == SIGNED
:
199 return int(value
* ((1 << (type.size
- 1)) - 1))
203 def native_to_constant(type, value
):
204 '''Get the value of unity for this type.'''
205 if type.type == FLOAT
:
211 return str(int(value
))
215 '''Get the value of unity for this type.'''
216 return value_to_native(type, 1)
219 def clamp_expr(src_channel
, dst_channel
, dst_native_type
, value
):
220 '''Generate the expression to clamp the value in the source type to the
221 destination type range.'''
223 if src_channel
== dst_channel
:
226 src_min
= src_channel
.min()
227 src_max
= src_channel
.max()
228 dst_min
= dst_channel
.min()
229 dst_max
= dst_channel
.max()
231 # Translate the destination range to the src native value
232 dst_min_native
= value_to_native(src_channel
, dst_min
)
233 dst_max_native
= value_to_native(src_channel
, dst_max
)
235 if src_min
< dst_min
and src_max
> dst_max
:
236 return 'CLAMP(%s, %s, %s)' % (value
, dst_min_native
, dst_max_native
)
238 if src_max
> dst_max
:
239 return 'MIN2(%s, %s)' % (value
, dst_max_native
)
241 if src_min
< dst_min
:
242 return 'MAX2(%s, %s)' % (value
, dst_min_native
)
247 def conversion_expr(src_channel
,
248 dst_channel
, dst_native_type
,
251 src_colorspace
= RGB
,
252 dst_colorspace
= RGB
):
253 '''Generate the expression to convert a value between two types.'''
255 if src_colorspace
!= dst_colorspace
:
256 if src_colorspace
== SRGB
:
257 assert src_channel
.type == UNSIGNED
258 assert src_channel
.norm
259 assert src_channel
.size
== 8
260 assert dst_colorspace
== RGB
261 if dst_channel
.type == FLOAT
:
262 return 'util_format_srgb_8unorm_to_linear_float(%s)' % value
264 assert dst_channel
.type == UNSIGNED
265 assert dst_channel
.norm
266 assert dst_channel
.size
== 8
267 return 'util_format_srgb_to_linear_8unorm(%s)' % value
268 elif dst_colorspace
== SRGB
:
269 assert dst_channel
.type == UNSIGNED
270 assert dst_channel
.norm
271 assert dst_channel
.size
== 8
272 assert src_colorspace
== RGB
273 if src_channel
.type == FLOAT
:
274 return 'util_format_linear_float_to_srgb_8unorm(%s)' % value
276 assert src_channel
.type == UNSIGNED
277 assert src_channel
.norm
278 assert src_channel
.size
== 8
279 return 'util_format_linear_to_srgb_8unorm(%s)' % value
280 elif src_colorspace
== ZS
:
282 elif dst_colorspace
== ZS
:
287 if src_channel
== dst_channel
:
290 src_type
= src_channel
.type
291 src_size
= src_channel
.size
292 src_norm
= src_channel
.norm
294 # Promote half to float
295 if src_type
== FLOAT
and src_size
== 16:
296 value
= 'util_half_to_float(%s)' % value
299 # Special case for float <-> ubytes for more accurate results
300 # Done before clamping since these functions already take care of that
301 if src_type
== UNSIGNED
and src_norm
and src_size
== 8 and dst_channel
.type == FLOAT
and dst_channel
.size
== 32:
302 return 'ubyte_to_float(%s)' % value
303 if src_type
== FLOAT
and src_size
== 32 and dst_channel
.type == UNSIGNED
and dst_channel
.norm
and dst_channel
.size
== 8:
304 return 'float_to_ubyte(%s)' % value
307 if dst_channel
.type != FLOAT
or src_type
!= FLOAT
:
308 value
= clamp_expr(src_channel
, dst_channel
, dst_native_type
, value
)
310 if src_type
in (SIGNED
, UNSIGNED
) and dst_channel
.type in (SIGNED
, UNSIGNED
):
311 if not src_norm
and not dst_channel
.norm
:
312 # neither is normalized -- just cast
313 return '(%s)%s' % (dst_native_type
, value
)
315 src_one
= get_one(src_channel
)
316 dst_one
= get_one(dst_channel
)
318 if src_one
> dst_one
and src_norm
and dst_channel
.norm
:
319 # We can just bitshift
320 src_shift
= get_one_shift(src_channel
)
321 dst_shift
= get_one_shift(dst_channel
)
322 value
= '(%s >> %s)' % (value
, src_shift
- dst_shift
)
324 # We need to rescale using an intermediate type big enough to hold the multiplication of both
325 tmp_native_type
= intermediate_native_type(src_size
+ dst_channel
.size
, src_channel
.sign
and dst_channel
.sign
)
326 value
= '((%s)%s)' % (tmp_native_type
, value
)
327 value
= '(%s * 0x%x / 0x%x)' % (value
, dst_one
, src_one
)
328 value
= '(%s)%s' % (dst_native_type
, value
)
331 # Promote to either float or double
332 if src_type
!= FLOAT
:
333 if src_norm
or src_type
== FIXED
:
334 one
= get_one(src_channel
)
336 value
= '(%s * (1.0f/0x%x))' % (value
, one
)
337 if dst_channel
.size
<= 32:
338 value
= '(float)%s' % value
341 # bigger than single precision mantissa, use double
342 value
= '(%s * (1.0/0x%x))' % (value
, one
)
346 if src_size
<= 23 or dst_channel
.size
<= 32:
347 value
= '(float)%s' % value
350 # bigger than single precision mantissa, use double
351 value
= '(double)%s' % value
355 # Convert double or float to non-float
356 if dst_channel
.type != FLOAT
:
357 if dst_channel
.norm
or dst_channel
.type == FIXED
:
358 dst_one
= get_one(dst_channel
)
359 if dst_channel
.size
<= 23:
360 value
= '(%s * 0x%x)' % (value
, dst_one
)
362 # bigger than single precision mantissa, use double
363 value
= '(%s * (double)0x%x)' % (value
, dst_one
)
364 value
= '(%s)%s' % (dst_native_type
, value
)
366 # Cast double to float when converting to either half or float
367 if dst_channel
.size
<= 32 and src_size
> 32:
368 value
= '(float)%s' % value
371 if dst_channel
.size
== 16:
372 value
= 'util_float_to_half(%s)' % value
373 elif dst_channel
.size
== 64 and src_size
< 64:
374 value
= '(double)%s' % value
379 def generate_unpack_kernel(format
, dst_channel
, dst_native_type
):
381 if not is_format_supported(format
):
384 assert format
.layout
== PLAIN
386 src_native_type
= native_type(format
)
388 if format
.is_bitmask():
389 depth
= format
.block_size()
390 print ' uint%u_t value = *(const uint%u_t *)src;' % (depth
, depth
)
392 # Declare the intermediate variables
393 for i
in range(format
.nr_channels()):
394 src_channel
= format
.channels
[i
]
395 if src_channel
.type == UNSIGNED
:
396 print ' uint%u_t %s;' % (depth
, src_channel
.name
)
397 elif src_channel
.type == SIGNED
:
398 print ' int%u_t %s;' % (depth
, src_channel
.name
)
401 print '#ifdef PIPE_ARCH_BIG_ENDIAN'
402 print ' value = util_bswap%u(value);' % depth
405 # Compute the intermediate unshifted values
407 for i
in range(format
.nr_channels()):
408 src_channel
= format
.channels
[i
]
410 if src_channel
.type == UNSIGNED
:
412 value
= '%s >> %u' % (value
, shift
)
413 if shift
+ src_channel
.size
< depth
:
414 value
= '(%s) & 0x%x' % (value
, (1 << src_channel
.size
) - 1)
415 elif src_channel
.type == SIGNED
:
416 if shift
+ src_channel
.size
< depth
:
418 lshift
= depth
- (shift
+ src_channel
.size
)
419 value
= '%s << %u' % (value
, lshift
)
421 value
= '(int%u_t)(%s) ' % (depth
, value
)
422 if src_channel
.size
< depth
:
424 rshift
= depth
- src_channel
.size
425 value
= '(%s) >> %u' % (value
, rshift
)
429 if value
is not None:
430 print ' %s = %s;' % (src_channel
.name
, value
)
432 shift
+= src_channel
.size
434 # Convert, swizzle, and store final values
436 swizzle
= format
.swizzles
[i
]
438 src_channel
= format
.channels
[swizzle
]
439 src_colorspace
= format
.colorspace
440 if src_colorspace
== SRGB
and i
== 3:
441 # Alpha channel is linear
443 value
= src_channel
.name
444 value
= conversion_expr(src_channel
,
445 dst_channel
, dst_native_type
,
447 src_colorspace
= src_colorspace
)
448 elif swizzle
== SWIZZLE_0
:
450 elif swizzle
== SWIZZLE_1
:
451 value
= get_one(dst_channel
)
452 elif swizzle
== SWIZZLE_NONE
:
456 print ' dst[%u] = %s; /* %s */' % (i
, value
, 'rgba'[i
])
459 print ' union util_format_%s pixel;' % format
.short_name()
460 print ' memcpy(&pixel, src, sizeof pixel);'
464 swizzle
= format
.swizzles
[i
]
466 src_channel
= format
.channels
[swizzle
]
467 src_colorspace
= format
.colorspace
468 if src_colorspace
== SRGB
and i
== 3:
469 # Alpha channel is linear
471 value
= 'pixel.chan.%s' % src_channel
.name
472 value
= conversion_expr(src_channel
,
473 dst_channel
, dst_native_type
,
475 src_colorspace
= src_colorspace
)
476 elif swizzle
== SWIZZLE_0
:
478 elif swizzle
== SWIZZLE_1
:
479 value
= get_one(dst_channel
)
480 elif swizzle
== SWIZZLE_NONE
:
484 print ' dst[%u] = %s; /* %s */' % (i
, value
, 'rgba'[i
])
487 def generate_pack_kernel(format
, src_channel
, src_native_type
):
489 if not is_format_supported(format
):
492 dst_native_type
= native_type(format
)
494 assert format
.layout
== PLAIN
496 inv_swizzle
= format
.inv_swizzles()
498 if format
.is_bitmask():
499 depth
= format
.block_size()
500 print ' uint%u_t value = 0;' % depth
504 dst_channel
= format
.channels
[i
]
505 if inv_swizzle
[i
] is not None:
506 value
='src[%u]' % inv_swizzle
[i
]
507 dst_colorspace
= format
.colorspace
508 if dst_colorspace
== SRGB
and inv_swizzle
[i
] == 3:
509 # Alpha channel is linear
511 value
= conversion_expr(src_channel
,
512 dst_channel
, dst_native_type
,
514 dst_colorspace
= dst_colorspace
)
515 if dst_channel
.type in (UNSIGNED
, SIGNED
):
516 if shift
+ dst_channel
.size
< depth
:
517 value
= '(%s) & 0x%x' % (value
, (1 << dst_channel
.size
) - 1)
519 value
= '(%s) << %u' % (value
, shift
)
520 if dst_channel
.type == SIGNED
:
522 value
= '(uint%u_t)(%s) ' % (depth
, value
)
525 if value
is not None:
526 print ' value |= %s;' % (value
)
528 shift
+= dst_channel
.size
531 print '#ifdef PIPE_ARCH_BIG_ENDIAN'
532 print ' value = util_bswap%u(value);' % depth
535 print ' *(uint%u_t *)dst = value;' % depth
538 print ' union util_format_%s pixel;' % format
.short_name()
541 dst_channel
= format
.channels
[i
]
542 width
= dst_channel
.size
543 if inv_swizzle
[i
] is None:
545 dst_colorspace
= format
.colorspace
546 if dst_colorspace
== SRGB
and inv_swizzle
[i
] == 3:
547 # Alpha channel is linear
549 value
='src[%u]' % inv_swizzle
[i
]
550 value
= conversion_expr(src_channel
,
551 dst_channel
, dst_native_type
,
553 dst_colorspace
= dst_colorspace
)
554 print ' pixel.chan.%s = %s;' % (dst_channel
.name
, value
)
557 print ' memcpy(dst, &pixel, sizeof pixel);'
560 def generate_format_unpack(format
, dst_channel
, dst_native_type
, dst_suffix
):
561 '''Generate the function to unpack pixels from a particular format'''
563 name
= format
.short_name()
565 print 'static INLINE void'
566 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
)
569 if is_format_supported(format
):
570 print ' unsigned x, y;'
571 print ' for(y = 0; y < height; y += %u) {' % (format
.block_height
,)
572 print ' %s *dst = dst_row;' % (dst_native_type
)
573 print ' const uint8_t *src = src_row;'
574 print ' for(x = 0; x < width; x += %u) {' % (format
.block_width
,)
576 generate_unpack_kernel(format
, dst_channel
, dst_native_type
)
578 print ' src += %u;' % (format
.block_size() / 8,)
581 print ' src_row += src_stride;'
582 print ' dst_row += dst_stride/sizeof(*dst_row);'
589 def generate_format_pack(format
, src_channel
, src_native_type
, src_suffix
):
590 '''Generate the function to pack pixels to a particular format'''
592 name
= format
.short_name()
594 print 'static INLINE void'
595 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
)
598 if is_format_supported(format
):
599 print ' unsigned x, y;'
600 print ' for(y = 0; y < height; y += %u) {' % (format
.block_height
,)
601 print ' const %s *src = src_row;' % (src_native_type
)
602 print ' uint8_t *dst = dst_row;'
603 print ' for(x = 0; x < width; x += %u) {' % (format
.block_width
,)
605 generate_pack_kernel(format
, src_channel
, src_native_type
)
608 print ' dst += %u;' % (format
.block_size() / 8,)
610 print ' dst_row += dst_stride;'
611 print ' src_row += src_stride/sizeof(*src_row);'
618 def generate_format_fetch(format
, dst_channel
, dst_native_type
, dst_suffix
):
619 '''Generate the function to unpack pixels from a particular format'''
621 name
= format
.short_name()
623 print 'static INLINE void'
624 print 'util_format_%s_fetch_%s(%s *dst, const uint8_t *src, unsigned i, unsigned j)' % (name
, dst_suffix
, dst_native_type
)
627 if is_format_supported(format
):
628 generate_unpack_kernel(format
, dst_channel
, dst_native_type
)
634 def is_format_hand_written(format
):
635 return format
.layout
in ('s3tc', 'subsampled', 'other') or format
.colorspace
== ZS
638 def generate(formats
):
640 print '#include "pipe/p_compiler.h"'
641 print '#include "u_math.h"'
642 print '#include "u_half.h"'
643 print '#include "u_format.h"'
644 print '#include "u_format_other.h"'
645 print '#include "u_format_srgb.h"'
646 print '#include "u_format_yuv.h"'
647 print '#include "u_format_zs.h"'
650 for format
in formats
:
651 if not is_format_hand_written(format
):
653 if is_format_supported(format
):
654 generate_format_type(format
)
656 channel
= Channel(FLOAT
, False, 32)
657 native_type
= 'float'
658 suffix
= 'rgba_float'
660 generate_format_unpack(format
, channel
, native_type
, suffix
)
661 generate_format_pack(format
, channel
, native_type
, suffix
)
662 generate_format_fetch(format
, channel
, native_type
, suffix
)
664 channel
= Channel(UNSIGNED
, True, 8)
665 native_type
= 'uint8_t'
666 suffix
= 'rgba_8unorm'
668 generate_format_unpack(format
, channel
, native_type
, suffix
)
669 generate_format_pack(format
, channel
, native_type
, suffix
)