~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/util/format/u_format_pack.py

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

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