3 /**************************************************************************
5 * Copyright 2009 VMware, Inc.
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:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
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.
28 **************************************************************************/
32 VOID
, UNSIGNED
, SIGNED
, FIXED
, FLOAT
= range(5)
34 SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_0
, SWIZZLE_1
, SWIZZLE_NONE
, = range(7)
46 return (x
& (x
- 1)) == 0
49 VERY_LARGE
= 99999999999999999999999
53 '''Describe the channel of a color channel.'''
55 def __init__(self
, type, norm
, pure
, scaled
, size
, name
= ''):
61 self
.sign
= type in (SIGNED
, FIXED
, FLOAT
)
75 def __eq__(self
, other
):
79 return self
.type == other
.type and self
.norm
== other
.norm
and self
.pure
== other
.pure
and self
.size
== other
.size
and self
.scaled
== other
.scaled
81 def __ne__(self
, other
):
82 return not self
== other
85 '''Maximum representable number.'''
86 if self
.type == FLOAT
:
88 if self
.type == FIXED
:
89 return (1 << (self
.size
/2)) - 1
92 if self
.type == UNSIGNED
:
93 return (1 << self
.size
) - 1
94 if self
.type == SIGNED
:
95 return (1 << (self
.size
- 1)) - 1
99 '''Minimum representable number.'''
100 if self
.type == FLOAT
:
102 if self
.type == FIXED
:
103 return -(1 << (self
.size
/2))
104 if self
.type == UNSIGNED
:
108 if self
.type == SIGNED
:
109 return -(1 << (self
.size
- 1))
114 '''Describe a pixel format.'''
116 def __init__(self
, name
, layout
, block_width
, block_height
, le_channels
, le_swizzles
, be_channels
, be_swizzles
, colorspace
, width_divisor
, height_divisor
, plane_formats
):
119 self
.block_width
= block_width
120 self
.block_height
= block_height
121 self
.le_channels
= le_channels
122 self
.le_swizzles
= le_swizzles
123 self
.be_channels
= be_channels
124 self
.be_swizzles
= be_swizzles
126 self
.colorspace
= colorspace
127 self
.plane_count
= len(plane_formats
)
128 self
.width_divisor
= width_divisor
129 self
.height_divisor
= height_divisor
130 self
.plane_formats
= plane_formats
132 while len(self
.plane_formats
) < 3:
133 self
.plane_formats
.append("VK_FORMAT_UNDEFINED")
138 def short_name(self
):
139 '''Make up a short norm for a format, suitable to be used as suffix in
143 if name
.startswith('VK_FORMAT_'):
144 name
= name
[len('VK_FORMAT_'):]
148 def block_size(self
):
150 for channel
in self
.le_channels
:
154 def nr_channels(self
):
156 for channel
in self
.le_channels
:
161 def array_element(self
):
162 if self
.layout
!= PLAIN
:
164 ref_channel
= self
.le_channels
[0]
165 if ref_channel
.type == VOID
:
166 ref_channel
= self
.le_channels
[1]
167 for channel
in self
.le_channels
:
168 if channel
.size
and (channel
.size
!= ref_channel
.size
or channel
.size
% 8):
170 if channel
.type != VOID
:
171 if channel
.type != ref_channel
.type:
173 if channel
.norm
!= ref_channel
.norm
:
175 if channel
.pure
!= ref_channel
.pure
:
177 if channel
.scaled
!= ref_channel
.scaled
:
182 return self
.array_element() != None
185 if self
.layout
!= PLAIN
:
187 ref_channel
= self
.le_channels
[0]
188 if ref_channel
.type == VOID
:
189 ref_channel
= self
.le_channels
[1]
190 for channel
in self
.le_channels
[1:]:
191 if channel
.type != VOID
:
192 if channel
.type != ref_channel
.type:
194 if channel
.norm
!= ref_channel
.norm
:
196 if channel
.pure
!= ref_channel
.pure
:
198 if channel
.scaled
!= ref_channel
.scaled
:
203 return is_pot(self
.block_size())
206 if self
.layout
!= PLAIN
:
208 for channel
in self
.le_channels
:
209 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
214 if self
.layout
!= PLAIN
:
216 for channel
in self
.le_channels
:
217 if channel
.type not in (VOID
, FLOAT
):
221 def is_bitmask(self
):
222 if self
.layout
!= PLAIN
:
224 if self
.block_size() not in (8, 16, 32):
226 for channel
in self
.le_channels
:
227 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
231 def is_pure_color(self
):
232 if self
.layout
!= PLAIN
or self
.colorspace
== ZS
:
234 pures
= [channel
.pure
235 for channel
in self
.le_channels
236 if channel
.type != VOID
]
241 def channel_type(self
):
242 types
= [channel
.type
243 for channel
in self
.le_channels
244 if channel
.type != VOID
]
249 def is_pure_signed(self
):
250 return self
.is_pure_color() and self
.channel_type() == SIGNED
252 def is_pure_unsigned(self
):
253 return self
.is_pure_color() and self
.channel_type() == UNSIGNED
255 def has_channel(self
, id):
256 return self
.le_swizzles
[id] != SWIZZLE_NONE
259 return self
.colorspace
== ZS
and self
.has_channel(0)
261 def has_stencil(self
):
262 return self
.colorspace
== ZS
and self
.has_channel(1)
265 return self
.block_size()/8
277 _swizzle_parse_map
= {
287 def _parse_channels(fields
, layout
, colorspace
, swizzles
):
290 if colorspace
in (RGB
, SRGB
):
292 swizzle
= swizzles
[i
]
294 names
[swizzle
] += 'rgba'[i
]
295 elif colorspace
== ZS
:
297 swizzle
= swizzles
[i
]
299 names
[swizzle
] += 'zs'[i
]
306 names
= ['x', 'y', 'z', 'w']
309 for i
in range(0, 4):
312 type = _type_parse_map
[field
[0]]
317 size
= int(field
[2:])
318 elif field
[1] == 'p':
322 size
= int(field
[2:])
323 elif field
[1] == 's':
327 size
= int(field
[2:])
332 size
= int(field
[1:])
339 channel
= Channel(type, norm
, pure
, scaled
, size
, names
[i
])
340 channels
.append(channel
)
344 def parse_plane_divisor(format
):
347 elif format
== '422':
349 elif format
== '420':
355 '''Parse the format description in CSV format in terms of the
356 Channel and Format classes above.'''
358 stream
= open(filename
)
362 comment
= line
.index('#')
366 line
= line
[:comment
]
371 fields
= [field
.strip() for field
in line
.split(',')]
372 if len (fields
) < 10:
375 be_fields
= fields
[4:9]
379 block_width
, block_height
= map(int, fields
[2:4])
380 colorspace
= fields
[9]
382 le_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[8]]
383 le_channels
= _parse_channels(fields
[4:8], layout
, colorspace
, le_swizzles
)
385 be_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in be_fields
[4]]
386 be_channels
= _parse_channels(be_fields
, layout
, colorspace
, be_swizzles
)
389 for channel
in le_channels
:
390 channel
.shift
= le_shift
391 le_shift
+= channel
.size
394 for channel
in be_channels
[3::-1]:
395 channel
.shift
= be_shift
396 be_shift
+= channel
.size
398 assert le_shift
== be_shift
400 assert (le_swizzles
[i
] != SWIZZLE_NONE
) == (be_swizzles
[i
] != SWIZZLE_NONE
)
404 plane_formats
= [name
]
405 if layout
== "multiplane":
407 (width_divisor
, height_divisor
) = parse_plane_divisor(fields
[10])
409 for i
in range(11, len(fields
)):
410 plane_formats
.append(fields
[i
])
411 assert (len(plane_formats
) > 1)
413 format
= Format(name
, layout
, block_width
, block_height
, le_channels
, le_swizzles
, be_channels
, be_swizzles
, colorspace
, width_divisor
, height_divisor
, plane_formats
)
414 formats
.append(format
)