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 from __future__
import division
35 VOID
, UNSIGNED
, SIGNED
, FIXED
, FLOAT
= range(5)
37 SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_0
, SWIZZLE_1
, SWIZZLE_NONE
, = range(7)
48 return (x
& (x
- 1)) == 0
51 VERY_LARGE
= 99999999999999999999999
55 '''Describe the channel of a color channel.'''
57 def __init__(self
, type, norm
, pure
, size
, name
= ''):
62 self
.sign
= type in (SIGNED
, FIXED
, FLOAT
)
74 def __eq__(self
, other
):
78 return self
.type == other
.type and self
.norm
== other
.norm
and self
.pure
== other
.pure
and self
.size
== other
.size
80 def __ne__(self
, other
):
81 return not self
== other
84 '''Maximum representable number.'''
85 if self
.type == FLOAT
:
87 if self
.type == FIXED
:
88 return (1 << (self
.size
// 2)) - 1
91 if self
.type == UNSIGNED
:
92 return (1 << self
.size
) - 1
93 if self
.type == SIGNED
:
94 return (1 << (self
.size
- 1)) - 1
98 '''Minimum representable number.'''
99 if self
.type == FLOAT
:
101 if self
.type == FIXED
:
102 return -(1 << (self
.size
// 2))
103 if self
.type == UNSIGNED
:
107 if self
.type == SIGNED
:
108 return -(1 << (self
.size
- 1))
113 '''Describe a pixel format.'''
115 def __init__(self
, name
, layout
, block_width
, block_height
, le_channels
, le_swizzles
, be_channels
, be_swizzles
, colorspace
):
118 self
.block_width
= block_width
119 self
.block_height
= block_height
120 self
.le_channels
= le_channels
121 self
.le_swizzles
= le_swizzles
122 self
.be_channels
= be_channels
123 self
.be_swizzles
= be_swizzles
125 self
.colorspace
= colorspace
130 def short_name(self
):
131 '''Make up a short norm for a format, suitable to be used as suffix in
135 if name
.startswith('PIPE_FORMAT_'):
136 name
= name
[len('PIPE_FORMAT_'):]
140 def block_size(self
):
142 for channel
in self
.le_channels
:
146 def nr_channels(self
):
148 for channel
in self
.le_channels
:
153 def array_element(self
):
154 if self
.layout
!= PLAIN
:
156 ref_channel
= self
.le_channels
[0]
157 if ref_channel
.type == VOID
:
158 ref_channel
= self
.le_channels
[1]
159 for channel
in self
.le_channels
:
160 if channel
.size
and (channel
.size
!= ref_channel
.size
or channel
.size
% 8):
162 if channel
.type != VOID
:
163 if channel
.type != ref_channel
.type:
165 if channel
.norm
!= ref_channel
.norm
:
167 if channel
.pure
!= ref_channel
.pure
:
172 return self
.array_element() != None
175 if self
.layout
!= PLAIN
:
177 ref_channel
= self
.le_channels
[0]
178 if ref_channel
.type == VOID
:
179 ref_channel
= self
.le_channels
[1]
180 for channel
in self
.le_channels
[1:]:
181 if channel
.type != VOID
:
182 if channel
.type != ref_channel
.type:
184 if channel
.norm
!= ref_channel
.norm
:
186 if channel
.pure
!= ref_channel
.pure
:
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]]
304 size
= int(field
[2:])
305 elif field
[1] == 'p':
308 size
= int(field
[2:])
312 size
= int(field
[1:])
318 channel
= Channel(type, norm
, pure
, size
, names
[i
])
319 channels
.append(channel
)
324 '''Parse the format description in CSV format in terms of the
325 Channel and Format classes above.'''
327 stream
= open(filename
)
331 comment
= line
.index('#')
335 line
= line
[:comment
]
340 fields
= [field
.strip() for field
in line
.split(',')]
341 if len (fields
) == 10:
342 fields
+= fields
[4:9]
343 assert len (fields
) == 15
347 block_width
, block_height
= map(int, fields
[2:4])
348 colorspace
= fields
[9]
350 le_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[8]]
351 le_channels
= _parse_channels(fields
[4:8], layout
, colorspace
, le_swizzles
)
353 be_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[14]]
354 be_channels
= _parse_channels(fields
[10:14], layout
, colorspace
, be_swizzles
)
357 for channel
in le_channels
:
358 channel
.shift
= le_shift
359 le_shift
+= channel
.size
362 for channel
in be_channels
[3::-1]:
363 channel
.shift
= be_shift
364 be_shift
+= channel
.size
366 assert le_shift
== be_shift
368 assert (le_swizzles
[i
] != SWIZZLE_NONE
) == (be_swizzles
[i
] != SWIZZLE_NONE
)
370 format
= Format(name
, layout
, block_width
, block_height
, le_channels
, le_swizzles
, be_channels
, be_swizzles
, colorspace
)
371 formats
.append(format
)