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
, block_depth
, le_channels
, le_swizzles
, be_channels
, be_swizzles
, colorspace
):
118 self
.block_width
= block_width
119 self
.block_height
= block_height
120 self
.block_depth
= block_depth
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('PIPE_FORMAT_'):
137 name
= name
[len('PIPE_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
:
173 return self
.array_element() != None
176 if self
.layout
!= PLAIN
:
178 ref_channel
= self
.le_channels
[0]
179 if ref_channel
.type == VOID
:
180 ref_channel
= self
.le_channels
[1]
181 for channel
in self
.le_channels
[1:]:
182 if channel
.type != VOID
:
183 if channel
.type != ref_channel
.type:
185 if channel
.norm
!= ref_channel
.norm
:
187 if channel
.pure
!= ref_channel
.pure
:
191 def is_compressed(self
):
192 for channel
in self
.le_channels
:
193 if channel
.type != VOID
:
198 # Non-compressed formats all have unorm or srgb in their name.
199 for keyword
in ['_UNORM', '_SRGB']:
200 if keyword
in self
.name
:
203 # All the compressed formats in GLES3.2 and GL4.6 ("Table 8.14: Generic
204 # and specific compressed internal formats.") that aren't snorm for
205 # border colors are unorm, other than BPTC_*_FLOAT.
206 return self
.is_compressed() and not ('FLOAT' in self
.name
or self
.is_snorm())
209 return '_SNORM' in self
.name
212 return is_pot(self
.block_size())
215 if self
.layout
!= PLAIN
:
217 for channel
in self
.le_channels
:
218 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
223 if self
.layout
!= PLAIN
:
225 for channel
in self
.le_channels
:
226 if channel
.type not in (VOID
, FLOAT
):
230 def is_bitmask(self
):
231 if self
.layout
!= PLAIN
:
233 if self
.block_size() not in (8, 16, 32):
235 for channel
in self
.le_channels
:
236 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
240 def is_pure_color(self
):
241 if self
.layout
!= PLAIN
or self
.colorspace
== ZS
:
243 pures
= [channel
.pure
244 for channel
in self
.le_channels
245 if channel
.type != VOID
]
250 def channel_type(self
):
251 types
= [channel
.type
252 for channel
in self
.le_channels
253 if channel
.type != VOID
]
258 def is_pure_signed(self
):
259 return self
.is_pure_color() and self
.channel_type() == SIGNED
261 def is_pure_unsigned(self
):
262 return self
.is_pure_color() and self
.channel_type() == UNSIGNED
264 def has_channel(self
, id):
265 return self
.le_swizzles
[id] != SWIZZLE_NONE
268 return self
.colorspace
== ZS
and self
.has_channel(0)
270 def has_stencil(self
):
271 return self
.colorspace
== ZS
and self
.has_channel(1)
274 return self
.block_size()/8
286 _swizzle_parse_map
= {
296 def _parse_channels(fields
, layout
, colorspace
, swizzles
):
299 if colorspace
in (RGB
, SRGB
):
301 swizzle
= swizzles
[i
]
303 names
[swizzle
] += 'rgba'[i
]
304 elif colorspace
== ZS
:
306 swizzle
= swizzles
[i
]
308 names
[swizzle
] += 'zs'[i
]
315 names
= ['x', 'y', 'z', 'w']
318 for i
in range(0, 4):
321 type = _type_parse_map
[field
[0]]
325 size
= int(field
[2:])
326 elif field
[1] == 'p':
329 size
= int(field
[2:])
333 size
= int(field
[1:])
339 channel
= Channel(type, norm
, pure
, size
, names
[i
])
340 channels
.append(channel
)
345 '''Parse the format description in CSV format in terms of the
346 Channel and Format classes above.'''
348 stream
= open(filename
)
352 comment
= line
.index('#')
356 line
= line
[:comment
]
361 fields
= [field
.strip() for field
in line
.split(',')]
362 if len (fields
) == 11:
363 fields
+= fields
[5:10]
364 assert len (fields
) == 16
368 block_width
, block_height
, block_depth
= map(int, fields
[2:5])
369 colorspace
= fields
[10]
371 le_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[9]]
372 le_channels
= _parse_channels(fields
[5:9], layout
, colorspace
, le_swizzles
)
374 be_swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[15]]
375 be_channels
= _parse_channels(fields
[11:15], layout
, colorspace
, be_swizzles
)
378 for channel
in le_channels
:
379 channel
.shift
= le_shift
380 le_shift
+= channel
.size
383 assert (le_swizzles
[i
] != SWIZZLE_NONE
) == (be_swizzles
[i
] != SWIZZLE_NONE
)
385 format
= Format(name
, layout
, block_width
, block_height
, block_depth
, le_channels
, le_swizzles
, be_channels
, be_swizzles
, colorspace
)
387 if format
.is_array() and not format
.is_bitmask():
388 # Formats accessed as arrays by the pack functions (R32G32_FLOAT or
389 # R8G8B8_UNORM, for example) should not be channel-ordering-reversed
391 # Note that __eq__ on channels ignores .shift!
392 assert(format
.be_channels
== format
.le_channels
)
393 assert(format
.be_swizzles
== format
.le_swizzles
)
394 format
.be_channels
= format
.le_channels
397 for channel
in format
.be_channels
[3::-1]:
398 channel
.shift
= be_shift
399 be_shift
+= channel
.size
401 assert le_shift
== be_shift
403 formats
.append(format
)