Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / auxiliary / util / u_format_pack.py
1 #!/usr/bin/env python
2
3 '''
4 /**************************************************************************
5  *
6  * Copyright 2009-2010 VMware, Inc.
7  * All Rights Reserved.
8  *
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:
16  *
17  * The above copyright notice and this permission notice (including the
18  * next paragraph) shall be included in all copies or substantial portions
19  * of the Software.
20  *
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.
28  *
29  **************************************************************************/
30
31 /**
32  * @file
33  * Pixel format packing and unpacking functions.
34  *
35  * @author Jose Fonseca <jfonseca@vmware.com>
36  */
37 '''
38
39
40 from u_format_parse import *
41
42
43 def generate_format_type(format):
44     '''Generate a structure that describes the format.'''
45
46     assert format.layout == PLAIN
47     
48     print 'union util_format_%s {' % format.short_name()
49     
50     if format.block_size() in (8, 16, 32, 64):
51         print '   uint%u_t value;' % (format.block_size(),)
52
53     use_bitfields = False
54     for channel in format.channels:
55         if channel.size % 8 or not is_pot(channel.size):
56             use_bitfields = True
57
58     print '   struct {'
59     for channel in format.channels:
60         if use_bitfields:
61             if channel.type == VOID:
62                 if channel.size:
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)
73                 else:
74                     print '      unsigned %s:%u;' % (channel.name, channel.size)
75             else:
76                 assert 0
77         else:
78             assert channel.size % 8 == 0 and is_pot(channel.size)
79             if channel.type == VOID:
80                 if channel.size:
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)
93                 else:
94                     assert 0
95             else:
96                 assert 0
97     print '   } chan;'
98     print '};'
99     print
100
101
102 def bswap_format(format):
103     '''Generate a structure that describes the format.'''
104
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()
108         print '#endif'
109
110
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.'''
114
115     # FIXME: Ideally we would support any format combination here.
116
117     if format.layout != PLAIN:
118         return False
119
120     for i in range(4):
121         channel = format.channels[i]
122         if channel.type not in (VOID, UNSIGNED, SIGNED, FLOAT, FIXED):
123             return False
124         if channel.type == FLOAT and channel.size not in (16, 32, 64):
125             return False
126
127     return True
128
129
130 def native_type(format):
131     '''Get the native appropriate for a format.'''
132
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()
137         else:
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:
146                     return 'uint16_t'
147                 elif channel.size == 32:
148                     return 'float'
149                 elif channel.size == 64:
150                     return 'double'
151                 else:
152                     assert False
153             else:
154                 assert False
155     else:
156         assert False
157
158
159 def intermediate_native_type(bits, sign):
160     '''Find a native type adequate to hold intermediate results of the request bit size.'''
161
162     bytes = 4 # don't use anything smaller than 32bits
163     while bytes * 8 < bits:
164         bytes *= 2
165     bits = bytes*8
166
167     if sign:
168         return 'int%u_t' % bits
169     else:
170         return 'uint%u_t' % bits
171
172
173 def get_one_shift(type):
174     '''Get the number of the bit that matches unity for this type.'''
175     if type.type == 'FLOAT':
176         assert False
177     if not type.norm:
178         return 0
179     if type.type == UNSIGNED:
180         return type.size
181     if type.type == SIGNED:
182         return type.size - 1
183     if type.type == FIXED:
184         return type.size / 2
185     assert False
186
187
188 def value_to_native(type, value):
189     '''Get the value of unity for this type.'''
190     if type.type == FLOAT:
191         return value
192     if type.type == FIXED:
193         return int(value * (1 << (type.size/2)))
194     if not type.norm:
195         return int(value)
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))
200     assert False
201
202
203 def native_to_constant(type, value):
204     '''Get the value of unity for this type.'''
205     if type.type == FLOAT:
206         if type.size <= 32:
207             return "%ff" % value 
208         else:
209             return "%ff" % value 
210     else:
211         return str(int(value))
212
213
214 def get_one(type):
215     '''Get the value of unity for this type.'''
216     return value_to_native(type, 1)
217
218
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.'''
222
223     if src_channel == dst_channel:
224         return value
225
226     src_min = src_channel.min()
227     src_max = src_channel.max()
228     dst_min = dst_channel.min()
229     dst_max = dst_channel.max()
230     
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)
234
235     if src_min < dst_min and src_max > dst_max:
236         return 'CLAMP(%s, %s, %s)' % (value, dst_min_native, dst_max_native)
237
238     if src_max > dst_max:
239         return 'MIN2(%s, %s)' % (value, dst_max_native)
240         
241     if src_min < dst_min:
242         return 'MAX2(%s, %s)' % (value, dst_min_native)
243
244     return value
245
246
247 def conversion_expr(src_channel, 
248                     dst_channel, dst_native_type, 
249                     value, 
250                     clamp=True, 
251                     src_colorspace = RGB, 
252                     dst_colorspace = RGB):
253     '''Generate the expression to convert a value between two types.'''
254
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
263             else:
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
275             else:
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:
281             pass
282         elif dst_colorspace == ZS:
283             pass
284         else:
285             assert 0
286
287     if src_channel == dst_channel:
288         return value
289
290     src_type = src_channel.type
291     src_size = src_channel.size
292     src_norm = src_channel.norm
293
294     # Promote half to float
295     if src_type == FLOAT and src_size == 16:
296         value = 'util_half_to_float(%s)' % value
297         src_size = 32
298
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
305
306     if clamp:
307         if dst_channel.type != FLOAT or src_type != FLOAT:
308             value = clamp_expr(src_channel, dst_channel, dst_native_type, value)
309
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)
314
315         src_one = get_one(src_channel)
316         dst_one = get_one(dst_channel)
317
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)
323         else:
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)
329         return value
330
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)
335             if src_size <= 23:
336                 value = '(%s * (1.0f/0x%x))' % (value, one)
337                 if dst_channel.size <= 32:
338                     value = '(float)%s' % value
339                 src_size = 32
340             else:
341                 # bigger than single precision mantissa, use double
342                 value = '(%s * (1.0/0x%x))' % (value, one)
343                 src_size = 64
344             src_norm = False
345         else:
346             if src_size <= 23 or dst_channel.size <= 32:
347                 value = '(float)%s' % value
348                 src_size = 32
349             else:
350                 # bigger than single precision mantissa, use double
351                 value = '(double)%s' % value
352                 src_size = 64
353         src_type = FLOAT
354
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)
361             else:
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)
365     else:
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
369             src_size = 32
370
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
375
376     return value
377
378
379 def generate_unpack_kernel(format, dst_channel, dst_native_type):
380
381     if not is_format_supported(format):
382         return
383     
384     assert format.layout == PLAIN
385
386     src_native_type = native_type(format)
387
388     if format.is_bitmask():
389         depth = format.block_size()
390         print '         uint%u_t value = *(const uint%u_t *)src;' % (depth, depth) 
391
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)
399
400         if depth > 8:
401             print '#ifdef PIPE_ARCH_BIG_ENDIAN'
402             print '         value = util_bswap%u(value);' % depth
403             print '#endif'
404
405         # Compute the intermediate unshifted values 
406         shift = 0
407         for i in range(format.nr_channels()):
408             src_channel = format.channels[i]
409             value = 'value'
410             if src_channel.type == UNSIGNED:
411                 if shift:
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:
417                     # Align the sign bit
418                     lshift = depth - (shift + src_channel.size)
419                     value = '%s << %u' % (value, lshift)
420                 # Cast to signed
421                 value = '(int%u_t)(%s) ' % (depth, value)
422                 if src_channel.size < depth:
423                     # Align the LSB bit
424                     rshift = depth - src_channel.size
425                     value = '(%s) >> %u' % (value, rshift)
426             else:
427                 value = None
428                 
429             if value is not None:
430                 print '         %s = %s;' % (src_channel.name, value)
431                 
432             shift += src_channel.size
433
434         # Convert, swizzle, and store final values
435         for i in range(4):
436             swizzle = format.swizzles[i]
437             if swizzle < 4:
438                 src_channel = format.channels[swizzle]
439                 src_colorspace = format.colorspace
440                 if src_colorspace == SRGB and i == 3:
441                     # Alpha channel is linear
442                     src_colorspace = RGB
443                 value = src_channel.name 
444                 value = conversion_expr(src_channel, 
445                                         dst_channel, dst_native_type, 
446                                         value,
447                                         src_colorspace = src_colorspace)
448             elif swizzle == SWIZZLE_0:
449                 value = '0'
450             elif swizzle == SWIZZLE_1:
451                 value = get_one(dst_channel)
452             elif swizzle == SWIZZLE_NONE:
453                 value = '0'
454             else:
455                 assert False
456             print '         dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i])
457         
458     else:
459         print '         union util_format_%s pixel;' % format.short_name()
460         print '         memcpy(&pixel, src, sizeof pixel);'
461         bswap_format(format)
462     
463         for i in range(4):
464             swizzle = format.swizzles[i]
465             if swizzle < 4:
466                 src_channel = format.channels[swizzle]
467                 src_colorspace = format.colorspace
468                 if src_colorspace == SRGB and i == 3:
469                     # Alpha channel is linear
470                     src_colorspace = RGB
471                 value = 'pixel.chan.%s' % src_channel.name 
472                 value = conversion_expr(src_channel, 
473                                         dst_channel, dst_native_type, 
474                                         value,
475                                         src_colorspace = src_colorspace)
476             elif swizzle == SWIZZLE_0:
477                 value = '0'
478             elif swizzle == SWIZZLE_1:
479                 value = get_one(dst_channel)
480             elif swizzle == SWIZZLE_NONE:
481                 value = '0'
482             else:
483                 assert False
484             print '         dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i])
485     
486
487 def generate_pack_kernel(format, src_channel, src_native_type):
488
489     if not is_format_supported(format):
490         return
491     
492     dst_native_type = native_type(format)
493
494     assert format.layout == PLAIN
495
496     inv_swizzle = format.inv_swizzles()
497
498     if format.is_bitmask():
499         depth = format.block_size()
500         print '         uint%u_t value = 0;' % depth 
501
502         shift = 0
503         for i in range(4):
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
510                     dst_colorspace = RGB
511                 value = conversion_expr(src_channel, 
512                                         dst_channel, dst_native_type, 
513                                         value,
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)
518                     if shift:
519                         value = '(%s) << %u' % (value, shift)
520                     if dst_channel.type == SIGNED:
521                         # Cast to unsigned
522                         value = '(uint%u_t)(%s) ' % (depth, value)
523                 else:
524                     value = None
525                 if value is not None:
526                     print '         value |= %s;' % (value)
527                 
528             shift += dst_channel.size
529
530         if depth > 8:
531             print '#ifdef PIPE_ARCH_BIG_ENDIAN'
532             print '         value = util_bswap%u(value);' % depth
533             print '#endif'
534         
535         print '         *(uint%u_t *)dst = value;' % depth 
536
537     else:
538         print '         union util_format_%s pixel;' % format.short_name()
539     
540         for i in range(4):
541             dst_channel = format.channels[i]
542             width = dst_channel.size
543             if inv_swizzle[i] is None:
544                 continue
545             dst_colorspace = format.colorspace
546             if dst_colorspace == SRGB and inv_swizzle[i] == 3:
547                 # Alpha channel is linear
548                 dst_colorspace = RGB
549             value ='src[%u]' % inv_swizzle[i]
550             value = conversion_expr(src_channel, 
551                                     dst_channel, dst_native_type, 
552                                     value, 
553                                     dst_colorspace = dst_colorspace)
554             print '         pixel.chan.%s = %s;' % (dst_channel.name, value)
555     
556         bswap_format(format)
557         print '         memcpy(dst, &pixel, sizeof pixel);'
558     
559
560 def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix):
561     '''Generate the function to unpack pixels from a particular format'''
562
563     name = format.short_name()
564
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)
567     print '{'
568
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,)
575         
576         generate_unpack_kernel(format, dst_channel, dst_native_type)
577     
578         print '         src += %u;' % (format.block_size() / 8,)
579         print '         dst += 4;'
580         print '      }'
581         print '      src_row += src_stride;'
582         print '      dst_row += dst_stride/sizeof(*dst_row);'
583         print '   }'
584
585     print '}'
586     print
587     
588
589 def generate_format_pack(format, src_channel, src_native_type, src_suffix):
590     '''Generate the function to pack pixels to a particular format'''
591
592     name = format.short_name()
593
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)
596     print '{'
597     
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,)
604     
605         generate_pack_kernel(format, src_channel, src_native_type)
606             
607         print '         src += 4;'
608         print '         dst += %u;' % (format.block_size() / 8,)
609         print '      }'
610         print '      dst_row += dst_stride;'
611         print '      src_row += src_stride/sizeof(*src_row);'
612         print '   }'
613         
614     print '}'
615     print
616     
617
618 def generate_format_fetch(format, dst_channel, dst_native_type, dst_suffix):
619     '''Generate the function to unpack pixels from a particular format'''
620
621     name = format.short_name()
622
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)
625     print '{'
626
627     if is_format_supported(format):
628         generate_unpack_kernel(format, dst_channel, dst_native_type)
629
630     print '}'
631     print
632
633
634 def is_format_hand_written(format):
635     return format.layout in ('s3tc', 'rgtc', 'subsampled', 'other') or format.colorspace == ZS
636
637
638 def generate(formats):
639     print
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"'
648     print
649
650     for format in formats:
651         if not is_format_hand_written(format):
652             
653             if is_format_supported(format):
654                 generate_format_type(format)
655
656             channel = Channel(FLOAT, False, 32)
657             native_type = 'float'
658             suffix = 'rgba_float'
659
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)
663
664             channel = Channel(UNSIGNED, True, 8)
665             native_type = 'uint8_t'
666             suffix = 'rgba_8unorm'
667
668             generate_format_unpack(format, channel, native_type, suffix)
669             generate_format_pack(format, channel, native_type, suffix)
670