• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2'''
3/**************************************************************************
4 *
5 * Copyright 2009-2010 VMware, Inc.
6 * All Rights Reserved.
7 *
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:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
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.
27 *
28 **************************************************************************/
29
30/**
31 * @file
32 * Pixel format packing and unpacking functions.
33 *
34 * @author Jose Fonseca <jfonseca@vmware.com>
35 */
36'''
37
38
39from __future__ import division, print_function
40
41import sys
42
43from u_format_parse import *
44
45
46if sys.version_info < (3, 0):
47    integer_types = (int, long)
48
49else:
50    integer_types = (int, )
51
52def inv_swizzles(swizzles):
53    '''Return an array[4] of inverse swizzle terms'''
54    '''Only pick the first matching value to avoid l8 getting blue and i8 getting alpha'''
55    inv_swizzle = [None]*4
56    for i in range(4):
57        swizzle = swizzles[i]
58        if swizzle < 4 and inv_swizzle[swizzle] == None:
59            inv_swizzle[swizzle] = i
60    return inv_swizzle
61
62def print_channels(format, func):
63    if format.nr_channels() <= 1:
64        func(format.le_channels, format.le_swizzles)
65    else:
66        if (format.le_channels == format.be_channels and
67            [c.shift for c in format.le_channels] ==
68            [c.shift for c in format.be_channels] and
69            format.le_swizzles == format.be_swizzles):
70            func(format.le_channels, format.le_swizzles)
71        else:
72            print('#if UTIL_ARCH_BIG_ENDIAN')
73            func(format.be_channels, format.be_swizzles)
74            print('#else')
75            func(format.le_channels, format.le_swizzles)
76            print('#endif')
77
78def generate_format_type(format):
79    '''Generate a structure that describes the format.'''
80
81    assert format.layout == PLAIN
82
83    def generate_bitfields(channels, swizzles):
84        for channel in channels:
85            if channel.type == VOID:
86                if channel.size:
87                    print('   unsigned %s:%u;' % (channel.name, channel.size))
88            elif channel.type == UNSIGNED:
89                print('   unsigned %s:%u;' % (channel.name, channel.size))
90            elif channel.type in (SIGNED, FIXED):
91                print('   int %s:%u;' % (channel.name, channel.size))
92            elif channel.type == FLOAT:
93                if channel.size == 64:
94                    print('   double %s;' % (channel.name))
95                elif channel.size == 32:
96                    print('   float %s;' % (channel.name))
97                else:
98                    print('   unsigned %s:%u;' % (channel.name, channel.size))
99            else:
100                assert 0
101
102    def generate_full_fields(channels, swizzles):
103        for channel in channels:
104            assert channel.size % 8 == 0 and is_pot(channel.size)
105            if channel.type == VOID:
106                if channel.size:
107                    print('   uint%u_t %s;' % (channel.size, channel.name))
108            elif channel.type == UNSIGNED:
109                print('   uint%u_t %s;' % (channel.size, channel.name))
110            elif channel.type in (SIGNED, FIXED):
111                print('   int%u_t %s;' % (channel.size, channel.name))
112            elif channel.type == FLOAT:
113                if channel.size == 64:
114                    print('   double %s;' % (channel.name))
115                elif channel.size == 32:
116                    print('   float %s;' % (channel.name))
117                elif channel.size == 16:
118                    print('   uint16_t %s;' % (channel.name))
119                else:
120                    assert 0
121            else:
122                assert 0
123
124    use_bitfields = False
125    for channel in format.le_channels:
126        if channel.size % 8 or not is_pot(channel.size):
127            use_bitfields = True
128
129    print('struct util_format_%s {' % format.short_name())
130    if use_bitfields:
131        print_channels(format, generate_bitfields)
132    else:
133        print_channels(format, generate_full_fields)
134    print('};')
135    print()
136
137
138def is_format_supported(format):
139    '''Determines whether we actually have the plumbing necessary to generate the
140    to read/write to/from this format.'''
141
142    # FIXME: Ideally we would support any format combination here.
143
144    if format.layout != PLAIN:
145        return False
146
147    for i in range(4):
148        channel = format.le_channels[i]
149        if channel.type not in (VOID, UNSIGNED, SIGNED, FLOAT, FIXED):
150            return False
151        if channel.type == FLOAT and channel.size not in (16, 32, 64):
152            return False
153
154    return True
155
156def native_type(format):
157    '''Get the native appropriate for a format.'''
158
159    if format.name == 'PIPE_FORMAT_R11G11B10_FLOAT':
160        return 'uint32_t'
161    if format.name == 'PIPE_FORMAT_R9G9B9E5_FLOAT':
162        return 'uint32_t'
163
164    if format.layout == PLAIN:
165        if not format.is_array():
166            # For arithmetic pixel formats return the integer type that matches the whole pixel
167            return 'uint%u_t' % format.block_size()
168        else:
169            # For array pixel formats return the integer type that matches the color channel
170            channel = format.array_element()
171            if channel.type in (UNSIGNED, VOID):
172                return 'uint%u_t' % channel.size
173            elif channel.type in (SIGNED, FIXED):
174                return 'int%u_t' % channel.size
175            elif channel.type == FLOAT:
176                if channel.size == 16:
177                    return 'uint16_t'
178                elif channel.size == 32:
179                    return 'float'
180                elif channel.size == 64:
181                    return 'double'
182                else:
183                    assert False
184            else:
185                assert False
186    else:
187        assert False
188
189
190def intermediate_native_type(bits, sign):
191    '''Find a native type adequate to hold intermediate results of the request bit size.'''
192
193    bytes = 4 # don't use anything smaller than 32bits
194    while bytes * 8 < bits:
195        bytes *= 2
196    bits = bytes*8
197
198    if sign:
199        return 'int%u_t' % bits
200    else:
201        return 'uint%u_t' % bits
202
203
204def get_one_shift(type):
205    '''Get the number of the bit that matches unity for this type.'''
206    if type.type == 'FLOAT':
207        assert False
208    if not type.norm:
209        return 0
210    if type.type == UNSIGNED:
211        return type.size
212    if type.type == SIGNED:
213        return type.size - 1
214    if type.type == FIXED:
215        return type.size / 2
216    assert False
217
218
219def truncate_mantissa(x, bits):
220    '''Truncate an integer so it can be represented exactly with a floating
221    point mantissa'''
222
223    assert isinstance(x, integer_types)
224
225    s = 1
226    if x < 0:
227        s = -1
228        x = -x
229
230    # We can represent integers up to mantissa + 1 bits exactly
231    mask = (1 << (bits + 1)) - 1
232
233    # Slide the mask until the MSB matches
234    shift = 0
235    while (x >> shift) & ~mask:
236        shift += 1
237
238    x &= mask << shift
239    x *= s
240    return x
241
242
243def value_to_native(type, value):
244    '''Get the value of unity for this type.'''
245    if type.type == FLOAT:
246        if type.size <= 32 \
247            and isinstance(value, integer_types):
248            return truncate_mantissa(value, 23)
249        return value
250    if type.type == FIXED:
251        return int(value * (1 << (type.size // 2)))
252    if not type.norm:
253        return int(value)
254    if type.type == UNSIGNED:
255        return int(value * ((1 << type.size) - 1))
256    if type.type == SIGNED:
257        return int(value * ((1 << (type.size - 1)) - 1))
258    assert False
259
260
261def native_to_constant(type, value):
262    '''Get the value of unity for this type.'''
263    if type.type == FLOAT:
264        if type.size <= 32:
265            return "%.1ff" % float(value)
266        else:
267            return "%.1f" % float(value)
268    else:
269        return str(int(value))
270
271
272def get_one(type):
273    '''Get the value of unity for this type.'''
274    return value_to_native(type, 1)
275
276
277def clamp_expr(src_channel, dst_channel, dst_native_type, value):
278    '''Generate the expression to clamp the value in the source type to the
279    destination type range.'''
280
281    if src_channel == dst_channel:
282        return value
283
284    src_min = src_channel.min()
285    src_max = src_channel.max()
286    dst_min = dst_channel.min()
287    dst_max = dst_channel.max()
288
289    # Translate the destination range to the src native value
290    dst_min_native = native_to_constant(src_channel, value_to_native(src_channel, dst_min))
291    dst_max_native = native_to_constant(src_channel, value_to_native(src_channel, dst_max))
292
293    if src_min < dst_min and src_max > dst_max:
294        return 'CLAMP(%s, %s, %s)' % (value, dst_min_native, dst_max_native)
295
296    if src_max > dst_max:
297        return 'MIN2(%s, %s)' % (value, dst_max_native)
298
299    if src_min < dst_min:
300        return 'MAX2(%s, %s)' % (value, dst_min_native)
301
302    return value
303
304
305def conversion_expr(src_channel,
306                    dst_channel, dst_native_type,
307                    value,
308                    clamp=True,
309                    src_colorspace = RGB,
310                    dst_colorspace = RGB):
311    '''Generate the expression to convert a value between two types.'''
312
313    if src_colorspace != dst_colorspace:
314        if src_colorspace == SRGB:
315            assert src_channel.type == UNSIGNED
316            assert src_channel.norm
317            assert src_channel.size <= 8
318            assert src_channel.size >= 4
319            assert dst_colorspace == RGB
320            if src_channel.size < 8:
321                value = '%s << %x | %s >> %x' % (value, 8 - src_channel.size, value, 2 * src_channel.size - 8)
322            if dst_channel.type == FLOAT:
323                return 'util_format_srgb_8unorm_to_linear_float(%s)' % value
324            else:
325                assert dst_channel.type == UNSIGNED
326                assert dst_channel.norm
327                assert dst_channel.size == 8
328                return 'util_format_srgb_to_linear_8unorm(%s)' % value
329        elif dst_colorspace == SRGB:
330            assert dst_channel.type == UNSIGNED
331            assert dst_channel.norm
332            assert dst_channel.size <= 8
333            assert src_colorspace == RGB
334            if src_channel.type == FLOAT:
335                value =  'util_format_linear_float_to_srgb_8unorm(%s)' % value
336            else:
337                assert src_channel.type == UNSIGNED
338                assert src_channel.norm
339                assert src_channel.size == 8
340                value = 'util_format_linear_to_srgb_8unorm(%s)' % value
341            # XXX rounding is all wrong.
342            if dst_channel.size < 8:
343                return '%s >> %x' % (value, 8 - dst_channel.size)
344            else:
345                return value
346        elif src_colorspace == ZS:
347            pass
348        elif dst_colorspace == ZS:
349            pass
350        else:
351            assert 0
352
353    if src_channel == dst_channel:
354        return value
355
356    src_type = src_channel.type
357    src_size = src_channel.size
358    src_norm = src_channel.norm
359    src_pure = src_channel.pure
360
361    # Promote half to float
362    if src_type == FLOAT and src_size == 16:
363        value = '_mesa_half_to_float(%s)' % value
364        src_size = 32
365
366    # Special case for float <-> ubytes for more accurate results
367    # Done before clamping since these functions already take care of that
368    if src_type == UNSIGNED and src_norm and src_size == 8 and dst_channel.type == FLOAT and dst_channel.size == 32:
369        return 'ubyte_to_float(%s)' % value
370    if src_type == FLOAT and src_size == 32 and dst_channel.type == UNSIGNED and dst_channel.norm and dst_channel.size == 8:
371        return 'float_to_ubyte(%s)' % value
372
373    if clamp:
374        if dst_channel.type != FLOAT or src_type != FLOAT:
375            value = clamp_expr(src_channel, dst_channel, dst_native_type, value)
376
377    if src_type in (SIGNED, UNSIGNED) and dst_channel.type in (SIGNED, UNSIGNED):
378        if not src_norm and not dst_channel.norm:
379            # neither is normalized -- just cast
380            return '(%s)%s' % (dst_native_type, value)
381
382        src_one = get_one(src_channel)
383        dst_one = get_one(dst_channel)
384
385        if src_one > dst_one and src_norm and dst_channel.norm:
386            # We can just bitshift
387            src_shift = get_one_shift(src_channel)
388            dst_shift = get_one_shift(dst_channel)
389            value = '(%s >> %s)' % (value, src_shift - dst_shift)
390        else:
391            # We need to rescale using an intermediate type big enough to hold the multiplication of both
392            tmp_native_type = intermediate_native_type(src_size + dst_channel.size, src_channel.sign and dst_channel.sign)
393            value = '((%s)%s)' % (tmp_native_type, value)
394            value = '(%s * 0x%x / 0x%x)' % (value, dst_one, src_one)
395        value = '(%s)%s' % (dst_native_type, value)
396        return value
397
398    # Promote to either float or double
399    if src_type != FLOAT:
400        if src_norm or src_type == FIXED:
401            one = get_one(src_channel)
402            if src_size <= 23:
403                value = '(%s * (1.0f/0x%x))' % (value, one)
404                if dst_channel.size <= 32:
405                    value = '(float)%s' % value
406                src_size = 32
407            else:
408                # bigger than single precision mantissa, use double
409                value = '(%s * (1.0/0x%x))' % (value, one)
410                src_size = 64
411            src_norm = False
412        else:
413            if src_size <= 23 or dst_channel.size <= 32:
414                value = '(float)%s' % value
415                src_size = 32
416            else:
417                # bigger than single precision mantissa, use double
418                value = '(double)%s' % value
419                src_size = 64
420        src_type = FLOAT
421
422    # Convert double or float to non-float
423    if dst_channel.type != FLOAT:
424        if dst_channel.norm or dst_channel.type == FIXED:
425            dst_one = get_one(dst_channel)
426            if dst_channel.size <= 23:
427                value = 'util_iround(%s * 0x%x)' % (value, dst_one)
428            else:
429                # bigger than single precision mantissa, use double
430                value = '(%s * (double)0x%x)' % (value, dst_one)
431        value = '(%s)%s' % (dst_native_type, value)
432    else:
433        # Cast double to float when converting to either half or float
434        if dst_channel.size <= 32 and src_size > 32:
435            value = '(float)%s' % value
436            src_size = 32
437
438        if dst_channel.size == 16:
439            value = '_mesa_float_to_float16_rtz(%s)' % value
440        elif dst_channel.size == 64 and src_size < 64:
441            value = '(double)%s' % value
442
443    return value
444
445
446def generate_unpack_kernel(format, dst_channel, dst_native_type):
447
448    if not is_format_supported(format):
449        return
450
451    assert format.layout == PLAIN
452
453    def unpack_from_bitmask(channels, swizzles):
454        depth = format.block_size()
455        print('         uint%u_t value = *(const uint%u_t *)src;' % (depth, depth))
456
457        # Declare the intermediate variables
458        for i in range(format.nr_channels()):
459            src_channel = channels[i]
460            if src_channel.type == UNSIGNED:
461                print('         uint%u_t %s;' % (depth, src_channel.name))
462            elif src_channel.type == SIGNED:
463                print('         int%u_t %s;' % (depth, src_channel.name))
464
465        # Compute the intermediate unshifted values
466        for i in range(format.nr_channels()):
467            src_channel = channels[i]
468            value = 'value'
469            shift = src_channel.shift
470            if src_channel.type == UNSIGNED:
471                if shift:
472                    value = '%s >> %u' % (value, shift)
473                if shift + src_channel.size < depth:
474                    value = '(%s) & 0x%x' % (value, (1 << src_channel.size) - 1)
475            elif src_channel.type == SIGNED:
476                if shift + src_channel.size < depth:
477                    # Align the sign bit
478                    lshift = depth - (shift + src_channel.size)
479                    value = '%s << %u' % (value, lshift)
480                # Cast to signed
481                value = '(int%u_t)(%s) ' % (depth, value)
482                if src_channel.size < depth:
483                    # Align the LSB bit
484                    rshift = depth - src_channel.size
485                    value = '(%s) >> %u' % (value, rshift)
486            else:
487                value = None
488
489            if value is not None:
490                print('         %s = %s;' % (src_channel.name, value))
491
492        # Convert, swizzle, and store final values
493        for i in range(4):
494            swizzle = swizzles[i]
495            if swizzle < 4:
496                src_channel = channels[swizzle]
497                src_colorspace = format.colorspace
498                if src_colorspace == SRGB and i == 3:
499                    # Alpha channel is linear
500                    src_colorspace = RGB
501                value = src_channel.name
502                value = conversion_expr(src_channel,
503                                        dst_channel, dst_native_type,
504                                        value,
505                                        src_colorspace = src_colorspace)
506            elif swizzle == SWIZZLE_0:
507                value = '0'
508            elif swizzle == SWIZZLE_1:
509                value = get_one(dst_channel)
510            elif swizzle == SWIZZLE_NONE:
511                value = '0'
512            else:
513                assert False
514            print('         dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]))
515
516    def unpack_from_struct(channels, swizzles):
517        print('         struct util_format_%s pixel;' % format.short_name())
518        print('         memcpy(&pixel, src, sizeof pixel);')
519
520        for i in range(4):
521            swizzle = swizzles[i]
522            if swizzle < 4:
523                src_channel = channels[swizzle]
524                src_colorspace = format.colorspace
525                if src_colorspace == SRGB and i == 3:
526                    # Alpha channel is linear
527                    src_colorspace = RGB
528                value = 'pixel.%s' % src_channel.name
529                value = conversion_expr(src_channel,
530                                        dst_channel, dst_native_type,
531                                        value,
532                                        src_colorspace = src_colorspace)
533            elif swizzle == SWIZZLE_0:
534                value = '0'
535            elif swizzle == SWIZZLE_1:
536                value = get_one(dst_channel)
537            elif swizzle == SWIZZLE_NONE:
538                value = '0'
539            else:
540                assert False
541            print('         dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]))
542
543    if format.is_bitmask():
544        print_channels(format, unpack_from_bitmask)
545    else:
546        print_channels(format, unpack_from_struct)
547
548
549def generate_pack_kernel(format, src_channel, src_native_type):
550
551    if not is_format_supported(format):
552        return
553
554    dst_native_type = native_type(format)
555
556    assert format.layout == PLAIN
557
558    def pack_into_bitmask(channels, swizzles):
559        inv_swizzle = inv_swizzles(swizzles)
560
561        depth = format.block_size()
562        print('         uint%u_t value = 0;' % depth)
563
564        for i in range(4):
565            dst_channel = channels[i]
566            shift = dst_channel.shift
567            if inv_swizzle[i] is not None:
568                value ='src[%u]' % inv_swizzle[i]
569                dst_colorspace = format.colorspace
570                if dst_colorspace == SRGB and inv_swizzle[i] == 3:
571                    # Alpha channel is linear
572                    dst_colorspace = RGB
573                value = conversion_expr(src_channel,
574                                        dst_channel, dst_native_type,
575                                        value,
576                                        dst_colorspace = dst_colorspace)
577                if dst_channel.type in (UNSIGNED, SIGNED):
578                    if shift + dst_channel.size < depth:
579                        value = '(%s) & 0x%x' % (value, (1 << dst_channel.size) - 1)
580                    if shift:
581                        value = '(uint32_t)(%s) << %u' % (value, shift)
582                    if dst_channel.type == SIGNED:
583                        # Cast to unsigned
584                        value = '(uint%u_t)(%s) ' % (depth, value)
585                else:
586                    value = None
587                if value is not None:
588                    print('         value |= %s;' % (value))
589
590        print('         *(uint%u_t *)dst = value;' % depth)
591
592    def pack_into_struct(channels, swizzles):
593        inv_swizzle = inv_swizzles(swizzles)
594
595        print('         struct util_format_%s pixel = {0};' % format.short_name())
596
597        for i in range(4):
598            dst_channel = channels[i]
599            width = dst_channel.size
600            if inv_swizzle[i] is None:
601                continue
602            dst_colorspace = format.colorspace
603            if dst_colorspace == SRGB and inv_swizzle[i] == 3:
604                # Alpha channel is linear
605                dst_colorspace = RGB
606            value ='src[%u]' % inv_swizzle[i]
607            value = conversion_expr(src_channel,
608                                    dst_channel, dst_native_type,
609                                    value,
610                                    dst_colorspace = dst_colorspace)
611            print('         pixel.%s = %s;' % (dst_channel.name, value))
612
613        print('         memcpy(dst, &pixel, sizeof pixel);')
614
615    if format.is_bitmask():
616        print_channels(format, pack_into_bitmask)
617    else:
618        print_channels(format, pack_into_struct)
619
620
621def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix):
622    '''Generate the function to unpack pixels from a particular format'''
623
624    name = format.short_name()
625
626    if "8unorm" in dst_suffix:
627        dst_proto_type = dst_native_type
628    else:
629        dst_proto_type = 'void'
630
631    proto = '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_proto_type)
632    print('void %s;' % proto, file=sys.stdout2)
633
634    print('void')
635    print(proto)
636    print('{')
637
638    if is_format_supported(format):
639        print('   unsigned x, y;')
640        print('   for(y = 0; y < height; y += %u) {' % (format.block_height,))
641        print('      %s *dst = dst_row;' % (dst_native_type))
642        print('      const uint8_t *src = src_row;')
643        print('      for(x = 0; x < width; x += %u) {' % (format.block_width,))
644
645        generate_unpack_kernel(format, dst_channel, dst_native_type)
646
647        print('         src += %u;' % (format.block_size() / 8,))
648        print('         dst += 4;')
649        print('      }')
650        print('      src_row += src_stride;')
651        print('      dst_row = (uint8_t *)dst_row + dst_stride;')
652        print('   }')
653
654    print('}')
655    print()
656
657
658def generate_format_pack(format, src_channel, src_native_type, src_suffix):
659    '''Generate the function to pack pixels to a particular format'''
660
661    name = format.short_name()
662
663    print('void')
664    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))
665    print('{')
666
667    print('void 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), file=sys.stdout2)
668
669    if is_format_supported(format):
670        print('   unsigned x, y;')
671        print('   for(y = 0; y < height; y += %u) {' % (format.block_height,))
672        print('      const %s *src = src_row;' % (src_native_type))
673        print('      uint8_t *dst = dst_row;')
674        print('      for(x = 0; x < width; x += %u) {' % (format.block_width,))
675
676        generate_pack_kernel(format, src_channel, src_native_type)
677
678        print('         src += 4;')
679        print('         dst += %u;' % (format.block_size() / 8,))
680        print('      }')
681        print('      dst_row += dst_stride;')
682        print('      src_row += src_stride/sizeof(*src_row);')
683        print('   }')
684
685    print('}')
686    print()
687
688
689def generate_format_fetch(format, dst_channel, dst_native_type):
690    '''Generate the function to unpack pixels from a particular format'''
691
692    name = format.short_name()
693
694    proto = 'util_format_%s_fetch_rgba(void *in_dst, const uint8_t *src, UNUSED unsigned i, UNUSED unsigned j)' % (name)
695    print('void %s;' % proto, file=sys.stdout2)
696
697    print('void')
698    print(proto)
699
700    print('{')
701    print('   %s *dst = in_dst;' % dst_native_type)
702
703    if is_format_supported(format):
704        generate_unpack_kernel(format, dst_channel, dst_native_type)
705
706    print('}')
707    print()
708
709
710def is_format_hand_written(format):
711    return format.layout != PLAIN or format.colorspace == ZS
712
713
714def generate(formats):
715    print()
716    print('#include "pipe/p_compiler.h"')
717    print('#include "util/u_math.h"')
718    print('#include "util/half_float.h"')
719    print('#include "u_format.h"')
720    print('#include "u_format_other.h"')
721    print('#include "util/format_srgb.h"')
722    print('#include "u_format_yuv.h"')
723    print('#include "u_format_zs.h"')
724    print('#include "u_format_pack.h"')
725    print()
726
727    for format in formats:
728        if not is_format_hand_written(format):
729
730            if is_format_supported(format) and not format.is_bitmask():
731                generate_format_type(format)
732
733            if format.is_pure_unsigned():
734                native_type = 'unsigned'
735                suffix = 'unsigned'
736                channel = Channel(UNSIGNED, False, True, 32)
737
738                generate_format_unpack(format, channel, native_type, suffix)
739                generate_format_pack(format, channel, native_type, suffix)
740                generate_format_fetch(format, channel, native_type)
741
742                channel = Channel(SIGNED, False, True, 32)
743                native_type = 'int'
744                suffix = 'signed'
745                generate_format_pack(format, channel, native_type, suffix)
746            elif format.is_pure_signed():
747                native_type = 'int'
748                suffix = 'signed'
749                channel = Channel(SIGNED, False, True, 32)
750
751                generate_format_unpack(format, channel, native_type, suffix)
752                generate_format_pack(format, channel, native_type, suffix)
753                generate_format_fetch(format, channel, native_type)
754
755                native_type = 'unsigned'
756                suffix = 'unsigned'
757                channel = Channel(UNSIGNED, False, True, 32)
758                generate_format_pack(format, channel, native_type, suffix)
759            else:
760                channel = Channel(FLOAT, False, False, 32)
761                native_type = 'float'
762                suffix = 'rgba_float'
763
764                generate_format_unpack(format, channel, native_type, suffix)
765                generate_format_pack(format, channel, native_type, suffix)
766                generate_format_fetch(format, channel, native_type)
767
768                channel = Channel(UNSIGNED, True, False, 8)
769                native_type = 'uint8_t'
770                suffix = 'rgba_8unorm'
771
772                generate_format_unpack(format, channel, native_type, suffix)
773                generate_format_pack(format, channel, native_type, suffix)
774