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
):
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
131 def short_name(self
):
132 '''Make up a short norm for a format, suitable to be used as suffix in
136 if name
.startswith('VK_FORMAT_'):
137 name
= name
[len('VK_FORMAT_'):]
141 def block_size(self
):
143 for channel
in self
.le_channels
:
147 def nr_channels(self
):
149 for channel
in self
.le_channels
:
154 def array_element(self
):
155 if self
.layout
!= PLAIN
:
157 ref_channel
= self
.le_channels
[0]
158 if ref_channel
.type == VOID
:
159 ref_channel
= self
.le_channels
[1]
160 for channel
in self
.le_channels
:
161 if channel
.size
and (channel
.size
!= ref_channel
.size
or channel
.size
% 8):
163 if channel
.type != VOID
:
164 if channel
.type != ref_channel
.type:
166 if channel
.norm
!= ref_channel
.norm
:
168 if channel
.pure
!= ref_channel
.pure
:
170 if channel
.scaled
!= ref_channel
.scaled
:
175 return self
.array_element() != None
178 if self
.layout
!= PLAIN
:
180 ref_channel
= self
.le_channels
[0]
181 if ref_channel
.type == VOID
:
182 ref_channel
= self
.le_channels
[1]
183 for channel
in self
.le_channels
[1:]:
184 if channel
.type != VOID
:
185 if channel
.type != ref_channel
.type:
187 if channel
.norm
!= ref_channel
.norm
:
189 if channel
.pure
!= ref_channel
.pure
:
191 if channel
.scaled
!= ref_channel
.scaled
:
196 return is_pot(self
.block_size())
199 if self
.layout
!= PLAIN
:
201 for channel
in self
.le_channels
:
202 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
207 if self
.layout
!= PLAIN
:
209 for channel
in self
.le_channels
:
210 if channel
.type not in (VOID
, FLOAT
):
214 def is_bitmask(self
):
215 if self
.layout
!= PLAIN
:
217 if self
.block_size() not in (8, 16, 32):
219 for channel
in self
.le_channels
:
220 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
224 def is_pure_color(self
):
225 if self
.layout
!= PLAIN
or self
.colorspace
== ZS
:
227 pures
= [channel
.pure
228 for channel
in self
.le_channels
229 if channel
.type != VOID
]
234 def channel_type(self
):
235 types
= [channel
.type
236 for channel
in self
.le_channels
237 if channel
.type != VOID
]
242 def is_pure_signed(self
):
243 return self
.is_pure_color() and self
.channel_type() == SIGNED
245 def is_pure_unsigned(self
):
246 return self
.is_pure_color() and self
.channel_type() == UNSIGNED
248 def has_channel(self
, id):
249 return self
.le_swizzles
[id] != SWIZZLE_NONE
252 return self
.colorspace
== ZS
and self
.has_channel(0)
254 def has_stencil(self
):
255 return self
.colorspace
== ZS
and self
.has_channel(1)
258 return self
.block_size()/8
270 _swizzle_parse_map
= {
280 def _parse_channels(fields
, layout
, colorspace
, swizzles
):
283 if colorspace
in (RGB
, SRGB
):
285 swizzle
= swizzles
[i
]
287 names
[swizzle
] += 'rgba'[i
]
288 elif colorspace
== ZS
:
290 swizzle
= swizzles
[i
]
292 names
[swizzle
] += 'zs'[i
]
299 names
= ['x', 'y', 'z', 'w']
302 for i
in range(0, 4):
305 type = _type_parse_map
[field
[0]]
310 size
= int(field
[2:])
311 elif field
[1] == 'p':
315 size
= int(field
[2:])
316 elif field
[1] == 's':
320 size
= int(field
[2:])
325 size
= int(field
[1:])
332 channel
= Channel(type, norm
, pure
, scaled
, size
, names
[i
])
333 channels
.append(channel
)
338 '''Parse the format description in CSV format in terms of the
339 Channel and Format classes above.'''
341 stream
= open(filename
)
345 comment
= line
.index('#')
349 line
= line
[:comment
]
354 fields
= [field
.strip() for field
in line
.split(',')]
355 if len (fields
) < 10:
357 if len (fields
) == 10:
358 fields
+= fields
[4:9]
359 assert len (fields
) == 15
363 block_width
, block_height
= map(int, fields
[2:4])
364 colorspace
= fields
[9]
366 le_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[8]]
367 le_channels
= _parse_channels(fields
[4:8], layout
, colorspace
, le_swizzles
)
369 be_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[14]]
370 be_channels
= _parse_channels(fields
[10:14], layout
, colorspace
, be_swizzles
)
373 for channel
in le_channels
:
374 channel
.shift
= le_shift
375 le_shift
+= channel
.size
378 for channel
in be_channels
[3::-1]:
379 channel
.shift
= be_shift
380 be_shift
+= channel
.size
382 assert le_shift
== be_shift
384 assert (le_swizzles
[i
] != SWIZZLE_NONE
) == (be_swizzles
[i
] != SWIZZLE_NONE
)
386 format
= Format(name
, layout
, block_width
, block_height
, le_channels
, le_swizzles
, be_channels
, be_swizzles
, colorspace
)
387 formats
.append(format
)