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