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)
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
, size
, name
= ''):
60 self
.sign
= type in (SIGNED
, FIXED
, FLOAT
)
72 def __eq__(self
, other
):
73 return self
.type == other
.type and self
.norm
== other
.norm
and self
.pure
== other
.pure
and self
.size
== other
.size
76 '''Maximum representable number.'''
77 if self
.type == FLOAT
:
79 if self
.type == FIXED
:
80 return (1 << (self
.size
/2)) - 1
83 if self
.type == UNSIGNED
:
84 return (1 << self
.size
) - 1
85 if self
.type == SIGNED
:
86 return (1 << (self
.size
- 1)) - 1
90 '''Minimum representable number.'''
91 if self
.type == FLOAT
:
93 if self
.type == FIXED
:
94 return -(1 << (self
.size
/2))
95 if self
.type == UNSIGNED
:
99 if self
.type == SIGNED
:
100 return -(1 << (self
.size
- 1))
105 '''Describe a pixel format.'''
107 def __init__(self
, name
, layout
, block_width
, block_height
, le_channels
, le_swizzles
, be_channels
, be_swizzles
, colorspace
):
110 self
.block_width
= block_width
111 self
.block_height
= block_height
112 self
.le_channels
= le_channels
113 self
.le_swizzles
= le_swizzles
114 self
.be_channels
= be_channels
115 self
.be_swizzles
= be_swizzles
117 self
.colorspace
= colorspace
122 def short_name(self
):
123 '''Make up a short norm for a format, suitable to be used as suffix in
127 if name
.startswith('PIPE_FORMAT_'):
128 name
= name
[len('PIPE_FORMAT_'):]
132 def block_size(self
):
134 for channel
in self
.le_channels
:
138 def nr_channels(self
):
140 for channel
in self
.le_channels
:
145 def array_element(self
):
146 if self
.layout
!= PLAIN
:
148 ref_channel
= self
.le_channels
[0]
149 if ref_channel
.type == VOID
:
150 ref_channel
= self
.le_channels
[1]
151 for channel
in self
.le_channels
:
152 if channel
.size
and (channel
.size
!= ref_channel
.size
or channel
.size
% 8):
154 if channel
.type != VOID
:
155 if channel
.type != ref_channel
.type:
157 if channel
.norm
!= ref_channel
.norm
:
159 if channel
.pure
!= ref_channel
.pure
:
164 return self
.array_element() != None
167 if self
.layout
!= PLAIN
:
169 ref_channel
= self
.le_channels
[0]
170 if ref_channel
.type == VOID
:
171 ref_channel
= self
.le_channels
[1]
172 for channel
in self
.le_channels
[1:]:
173 if channel
.type != VOID
:
174 if channel
.type != ref_channel
.type:
176 if channel
.norm
!= ref_channel
.norm
:
178 if channel
.pure
!= ref_channel
.pure
:
183 return is_pot(self
.block_size())
186 if self
.layout
!= PLAIN
:
188 for channel
in self
.le_channels
:
189 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
194 if self
.layout
!= PLAIN
:
196 for channel
in self
.le_channels
:
197 if channel
.type not in (VOID
, FLOAT
):
201 def is_bitmask(self
):
202 if self
.layout
!= PLAIN
:
204 if self
.block_size() not in (8, 16, 32):
206 for channel
in self
.le_channels
:
207 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
211 def is_pure_color(self
):
212 if self
.layout
!= PLAIN
or self
.colorspace
== ZS
:
214 pures
= [channel
.pure
215 for channel
in self
.le_channels
216 if channel
.type != VOID
]
221 def channel_type(self
):
222 types
= [channel
.type
223 for channel
in self
.le_channels
224 if channel
.type != VOID
]
229 def is_pure_signed(self
):
230 return self
.is_pure_color() and self
.channel_type() == SIGNED
232 def is_pure_unsigned(self
):
233 return self
.is_pure_color() and self
.channel_type() == UNSIGNED
235 def has_channel(self
, id):
236 return self
.le_swizzles
[id] != SWIZZLE_NONE
239 return self
.colorspace
== ZS
and self
.has_channel(0)
241 def has_stencil(self
):
242 return self
.colorspace
== ZS
and self
.has_channel(1)
245 return self
.block_size()/8
257 _swizzle_parse_map
= {
267 def _parse_channels(fields
, layout
, colorspace
, swizzles
):
270 if colorspace
in (RGB
, SRGB
):
272 swizzle
= swizzles
[i
]
274 names
[swizzle
] += 'rgba'[i
]
275 elif colorspace
== ZS
:
277 swizzle
= swizzles
[i
]
279 names
[swizzle
] += 'zs'[i
]
286 names
= ['x', 'y', 'z', 'w']
289 for i
in range(0, 4):
292 type = _type_parse_map
[field
[0]]
296 size
= int(field
[2:])
297 elif field
[1] == 'p':
300 size
= int(field
[2:])
304 size
= int(field
[1:])
310 channel
= Channel(type, norm
, pure
, size
, names
[i
])
311 channels
.append(channel
)
316 '''Parse the format description in CSV format in terms of the
317 Channel and Format classes above.'''
319 stream
= open(filename
)
323 comment
= line
.index('#')
327 line
= line
[:comment
]
332 fields
= [field
.strip() for field
in line
.split(',')]
333 if len (fields
) == 10:
334 fields
+= fields
[4:9]
335 assert len (fields
) == 15
339 block_width
, block_height
= map(int, fields
[2:4])
340 colorspace
= fields
[9]
342 le_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[8]]
343 le_channels
= _parse_channels(fields
[4:8], layout
, colorspace
, le_swizzles
)
345 be_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[14]]
346 be_channels
= _parse_channels(fields
[10:14], layout
, colorspace
, be_swizzles
)
349 for channel
in le_channels
:
350 channel
.shift
= le_shift
351 le_shift
+= channel
.size
354 for channel
in be_channels
[3::-1]:
355 channel
.shift
= be_shift
356 be_shift
+= channel
.size
358 assert le_shift
== be_shift
360 assert (le_swizzles
[i
] != SWIZZLE_NONE
) == (be_swizzles
[i
] != SWIZZLE_NONE
)
362 format
= Format(name
, layout
, block_width
, block_height
, le_channels
, le_swizzles
, be_channels
, be_swizzles
, colorspace
)
363 formats
.append(format
)