4 /**************************************************************************
6 * Copyright 2009 VMware, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sub license, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
25 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 **************************************************************************/
33 VOID
, UNSIGNED
, SIGNED
, FIXED
, FLOAT
= range(5)
35 SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_0
, SWIZZLE_1
, SWIZZLE_NONE
, = range(7)
47 return (x
& (x
- 1)) == 0
50 VERY_LARGE
= 99999999999999999999999
54 '''Describe the channel of a color channel.'''
56 def __init__(self
, type, norm
, pure
, scaled
, size
, name
= ''):
62 self
.sign
= type in (SIGNED
, FIXED
, FLOAT
)
76 def __eq__(self
, other
):
77 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
80 '''Maximum representable number.'''
81 if self
.type == FLOAT
:
83 if self
.type == FIXED
:
84 return (1 << (self
.size
/2)) - 1
87 if self
.type == UNSIGNED
:
88 return (1 << self
.size
) - 1
89 if self
.type == SIGNED
:
90 return (1 << (self
.size
- 1)) - 1
94 '''Minimum representable number.'''
95 if self
.type == FLOAT
:
97 if self
.type == FIXED
:
98 return -(1 << (self
.size
/2))
99 if self
.type == UNSIGNED
:
103 if self
.type == SIGNED
:
104 return -(1 << (self
.size
- 1))
109 '''Describe a pixel format.'''
111 def __init__(self
, name
, layout
, block_width
, block_height
, le_channels
, le_swizzles
, be_channels
, be_swizzles
, colorspace
):
114 self
.block_width
= block_width
115 self
.block_height
= block_height
116 self
.le_channels
= le_channels
117 self
.le_swizzles
= le_swizzles
118 self
.be_channels
= be_channels
119 self
.be_swizzles
= be_swizzles
121 self
.colorspace
= colorspace
126 def short_name(self
):
127 '''Make up a short norm for a format, suitable to be used as suffix in
131 if name
.startswith('VK_FORMAT_'):
132 name
= name
[len('VK_FORMAT_'):]
136 def block_size(self
):
138 for channel
in self
.le_channels
:
142 def nr_channels(self
):
144 for channel
in self
.le_channels
:
149 def array_element(self
):
150 if self
.layout
!= PLAIN
:
152 ref_channel
= self
.le_channels
[0]
153 if ref_channel
.type == VOID
:
154 ref_channel
= self
.le_channels
[1]
155 for channel
in self
.le_channels
:
156 if channel
.size
and (channel
.size
!= ref_channel
.size
or channel
.size
% 8):
158 if channel
.type != VOID
:
159 if channel
.type != ref_channel
.type:
161 if channel
.norm
!= ref_channel
.norm
:
163 if channel
.pure
!= ref_channel
.pure
:
165 if channel
.scaled
!= ref_channel
.scaled
:
170 return self
.array_element() != None
173 if self
.layout
!= PLAIN
:
175 ref_channel
= self
.le_channels
[0]
176 if ref_channel
.type == VOID
:
177 ref_channel
= self
.le_channels
[1]
178 for channel
in self
.le_channels
[1:]:
179 if channel
.type != VOID
:
180 if channel
.type != ref_channel
.type:
182 if channel
.norm
!= ref_channel
.norm
:
184 if channel
.pure
!= ref_channel
.pure
:
186 if channel
.scaled
!= ref_channel
.scaled
:
191 return is_pot(self
.block_size())
194 if self
.layout
!= PLAIN
:
196 for channel
in self
.le_channels
:
197 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
202 if self
.layout
!= PLAIN
:
204 for channel
in self
.le_channels
:
205 if channel
.type not in (VOID
, FLOAT
):
209 def is_bitmask(self
):
210 if self
.layout
!= PLAIN
:
212 if self
.block_size() not in (8, 16, 32):
214 for channel
in self
.le_channels
:
215 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
219 def is_pure_color(self
):
220 if self
.layout
!= PLAIN
or self
.colorspace
== ZS
:
222 pures
= [channel
.pure
223 for channel
in self
.le_channels
224 if channel
.type != VOID
]
229 def channel_type(self
):
230 types
= [channel
.type
231 for channel
in self
.le_channels
232 if channel
.type != VOID
]
237 def is_pure_signed(self
):
238 return self
.is_pure_color() and self
.channel_type() == SIGNED
240 def is_pure_unsigned(self
):
241 return self
.is_pure_color() and self
.channel_type() == UNSIGNED
243 def has_channel(self
, id):
244 return self
.le_swizzles
[id] != SWIZZLE_NONE
247 return self
.colorspace
== ZS
and self
.has_channel(0)
249 def has_stencil(self
):
250 return self
.colorspace
== ZS
and self
.has_channel(1)
253 return self
.block_size()/8
265 _swizzle_parse_map
= {
275 def _parse_channels(fields
, layout
, colorspace
, swizzles
):
278 if colorspace
in (RGB
, SRGB
):
280 swizzle
= swizzles
[i
]
282 names
[swizzle
] += 'rgba'[i
]
283 elif colorspace
== ZS
:
285 swizzle
= swizzles
[i
]
287 names
[swizzle
] += 'zs'[i
]
294 names
= ['x', 'y', 'z', 'w']
297 for i
in range(0, 4):
300 type = _type_parse_map
[field
[0]]
305 size
= int(field
[2:])
306 elif field
[1] == 'p':
310 size
= int(field
[2:])
311 elif field
[1] == 's':
315 size
= int(field
[2:])
320 size
= int(field
[1:])
327 channel
= Channel(type, norm
, pure
, scaled
, size
, names
[i
])
328 channels
.append(channel
)
333 '''Parse the format description in CSV format in terms of the
334 Channel and Format classes above.'''
336 stream
= open(filename
)
340 comment
= line
.index('#')
344 line
= line
[:comment
]
349 fields
= [field
.strip() for field
in line
.split(',')]
350 if len (fields
) < 10:
352 if len (fields
) == 10:
353 fields
+= fields
[4:9]
354 assert len (fields
) == 15
358 block_width
, block_height
= map(int, fields
[2:4])
359 colorspace
= fields
[9]
361 le_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[8]]
362 le_channels
= _parse_channels(fields
[4:8], layout
, colorspace
, le_swizzles
)
364 be_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[14]]
365 be_channels
= _parse_channels(fields
[10:14], layout
, colorspace
, be_swizzles
)
368 for channel
in le_channels
:
369 channel
.shift
= le_shift
370 le_shift
+= channel
.size
373 for channel
in be_channels
[3::-1]:
374 channel
.shift
= be_shift
375 be_shift
+= channel
.size
377 assert le_shift
== be_shift
379 assert (le_swizzles
[i
] != SWIZZLE_NONE
) == (be_swizzles
[i
] != SWIZZLE_NONE
)
381 format
= Format(name
, layout
, block_width
, block_height
, le_channels
, le_swizzles
, be_channels
, be_swizzles
, colorspace
)
382 formats
.append(format
)