Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/util/format/u_format_parse.py
7075 views
1
2
'''
3
/**************************************************************************
4
*
5
* Copyright 2009 VMware, Inc.
6
* All Rights Reserved.
7
*
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:
15
*
16
* The above copyright notice and this permission notice (including the
17
* next paragraph) shall be included in all copies or substantial portions
18
* of the Software.
19
*
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.
27
*
28
**************************************************************************/
29
'''
30
31
32
from __future__ import division
33
import copy
34
35
VOID, UNSIGNED, SIGNED, FIXED, FLOAT = range(5)
36
37
SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_0, SWIZZLE_1, SWIZZLE_NONE, = range(7)
38
39
PLAIN = 'plain'
40
41
RGB = 'rgb'
42
SRGB = 'srgb'
43
YUV = 'yuv'
44
ZS = 'zs'
45
46
47
def is_pot(x):
48
return (x & (x - 1)) == 0
49
50
51
VERY_LARGE = 99999999999999999999999
52
53
54
class Channel:
55
'''Describe the channel of a color channel.'''
56
57
def __init__(self, type, norm, pure, size, name=''):
58
self.type = type
59
self.norm = norm
60
self.pure = pure
61
self.size = size
62
self.sign = type in (SIGNED, FIXED, FLOAT)
63
self.name = name
64
65
def __str__(self):
66
s = str(self.type)
67
if self.norm:
68
s += 'n'
69
if self.pure:
70
s += 'p'
71
s += str(self.size)
72
return s
73
74
def __repr__(self):
75
return "Channel({})".format(self.__str__())
76
77
def __eq__(self, other):
78
if other is None:
79
return False
80
81
return self.type == other.type and self.norm == other.norm and self.pure == other.pure and self.size == other.size
82
83
def __ne__(self, other):
84
return not self == other
85
86
def max(self):
87
'''Maximum representable number.'''
88
if self.type == FLOAT:
89
return VERY_LARGE
90
if self.type == FIXED:
91
return (1 << (self.size // 2)) - 1
92
if self.norm:
93
return 1
94
if self.type == UNSIGNED:
95
return (1 << self.size) - 1
96
if self.type == SIGNED:
97
return (1 << (self.size - 1)) - 1
98
assert False
99
100
def min(self):
101
'''Minimum representable number.'''
102
if self.type == FLOAT:
103
return -VERY_LARGE
104
if self.type == FIXED:
105
return -(1 << (self.size // 2))
106
if self.type == UNSIGNED:
107
return 0
108
if self.norm:
109
return -1
110
if self.type == SIGNED:
111
return -(1 << (self.size - 1))
112
assert False
113
114
115
class Format:
116
'''Describe a pixel format.'''
117
118
def __init__(self, name, layout, block_width, block_height, block_depth, le_channels, le_swizzles, be_channels, be_swizzles, colorspace):
119
self.name = name
120
self.layout = layout
121
self.block_width = block_width
122
self.block_height = block_height
123
self.block_depth = block_depth
124
self.colorspace = colorspace
125
126
self.le_channels = le_channels
127
self.le_swizzles = le_swizzles
128
129
le_shift = 0
130
for channel in self.le_channels:
131
channel.shift = le_shift
132
le_shift += channel.size
133
134
if be_channels:
135
if self.is_array():
136
print(
137
"{} is an array format and should not include BE swizzles in the CSV".format(self.name))
138
exit(1)
139
if self.is_bitmask():
140
print(
141
"{} is a bitmask format and should not include BE swizzles in the CSV".format(self.name))
142
exit(1)
143
self.be_channels = be_channels
144
self.be_swizzles = be_swizzles
145
elif self.is_bitmask() and not self.is_array():
146
# Bitmask formats are "load a word the size of the block and
147
# bitshift channels out of it." However, the channel shifts
148
# defined in u_format_table.c are numbered right-to-left on BE
149
# for some historical reason (see below), which is hard to
150
# change due to llvmpipe, so we also have to flip the channel
151
# order and the channel-to-rgba swizzle values to read
152
# right-to-left from the defined (non-VOID) channels so that the
153
# correct shifts happen.
154
#
155
# This is nonsense, but it's the nonsense that makes
156
# u_format_test pass and you get the right colors in softpipe at
157
# least.
158
chans = self.nr_channels()
159
self.be_channels = self.le_channels[chans -
160
1::-1] + self.le_channels[chans:4]
161
162
xyzw = [SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W]
163
chan_map = {SWIZZLE_X: xyzw[chans - 1] if chans >= 1 else SWIZZLE_X,
164
SWIZZLE_Y: xyzw[chans - 2] if chans >= 2 else SWIZZLE_X,
165
SWIZZLE_Z: xyzw[chans - 3] if chans >= 3 else SWIZZLE_X,
166
SWIZZLE_W: xyzw[chans - 4] if chans >= 4 else SWIZZLE_X,
167
SWIZZLE_1: SWIZZLE_1,
168
SWIZZLE_0: SWIZZLE_0,
169
SWIZZLE_NONE: SWIZZLE_NONE}
170
self.be_swizzles = [chan_map[s] for s in self.le_swizzles]
171
else:
172
self.be_channels = copy.deepcopy(le_channels)
173
self.be_swizzles = le_swizzles
174
175
be_shift = 0
176
for channel in reversed(self.be_channels):
177
channel.shift = be_shift
178
be_shift += channel.size
179
180
assert le_shift == be_shift
181
for i in range(4):
182
assert (self.le_swizzles[i] != SWIZZLE_NONE) == (
183
self.be_swizzles[i] != SWIZZLE_NONE)
184
185
def __str__(self):
186
return self.name
187
188
def short_name(self):
189
'''Make up a short norm for a format, suitable to be used as suffix in
190
function names.'''
191
192
name = self.name
193
if name.startswith('PIPE_FORMAT_'):
194
name = name[len('PIPE_FORMAT_'):]
195
name = name.lower()
196
return name
197
198
def block_size(self):
199
size = 0
200
for channel in self.le_channels:
201
size += channel.size
202
return size
203
204
def nr_channels(self):
205
nr_channels = 0
206
for channel in self.le_channels:
207
if channel.size:
208
nr_channels += 1
209
return nr_channels
210
211
def array_element(self):
212
if self.layout != PLAIN:
213
return None
214
ref_channel = self.le_channels[0]
215
if ref_channel.type == VOID:
216
ref_channel = self.le_channels[1]
217
for channel in self.le_channels:
218
if channel.size and (channel.size != ref_channel.size or channel.size % 8):
219
return None
220
if channel.type != VOID:
221
if channel.type != ref_channel.type:
222
return None
223
if channel.norm != ref_channel.norm:
224
return None
225
if channel.pure != ref_channel.pure:
226
return None
227
return ref_channel
228
229
def is_array(self):
230
return self.array_element() != None
231
232
def is_mixed(self):
233
if self.layout != PLAIN:
234
return False
235
ref_channel = self.le_channels[0]
236
if ref_channel.type == VOID:
237
ref_channel = self.le_channels[1]
238
for channel in self.le_channels[1:]:
239
if channel.type != VOID:
240
if channel.type != ref_channel.type:
241
return True
242
if channel.norm != ref_channel.norm:
243
return True
244
if channel.pure != ref_channel.pure:
245
return True
246
return False
247
248
def is_compressed(self):
249
for channel in self.le_channels:
250
if channel.type != VOID:
251
return False
252
return True
253
254
def is_unorm(self):
255
# Non-compressed formats all have unorm or srgb in their name.
256
for keyword in ['_UNORM', '_SRGB']:
257
if keyword in self.name:
258
return True
259
260
# All the compressed formats in GLES3.2 and GL4.6 ("Table 8.14: Generic
261
# and specific compressed internal formats.") that aren't snorm for
262
# border colors are unorm, other than BPTC_*_FLOAT.
263
return self.is_compressed() and not ('FLOAT' in self.name or self.is_snorm())
264
265
def is_snorm(self):
266
return '_SNORM' in self.name
267
268
def is_pot(self):
269
return is_pot(self.block_size())
270
271
def is_int(self):
272
if self.layout != PLAIN:
273
return False
274
for channel in self.le_channels:
275
if channel.type not in (VOID, UNSIGNED, SIGNED):
276
return False
277
return True
278
279
def is_float(self):
280
if self.layout != PLAIN:
281
return False
282
for channel in self.le_channels:
283
if channel.type not in (VOID, FLOAT):
284
return False
285
return True
286
287
def is_bitmask(self):
288
if self.layout != PLAIN:
289
return False
290
if self.block_size() not in (8, 16, 32):
291
return False
292
for channel in self.le_channels:
293
if channel.type not in (VOID, UNSIGNED, SIGNED):
294
return False
295
return True
296
297
def is_pure_color(self):
298
if self.layout != PLAIN or self.colorspace == ZS:
299
return False
300
pures = [channel.pure
301
for channel in self.le_channels
302
if channel.type != VOID]
303
for x in pures:
304
assert x == pures[0]
305
return pures[0]
306
307
def channel_type(self):
308
types = [channel.type
309
for channel in self.le_channels
310
if channel.type != VOID]
311
for x in types:
312
assert x == types[0]
313
return types[0]
314
315
def is_pure_signed(self):
316
return self.is_pure_color() and self.channel_type() == SIGNED
317
318
def is_pure_unsigned(self):
319
return self.is_pure_color() and self.channel_type() == UNSIGNED
320
321
def has_channel(self, id):
322
return self.le_swizzles[id] != SWIZZLE_NONE
323
324
def has_depth(self):
325
return self.colorspace == ZS and self.has_channel(0)
326
327
def has_stencil(self):
328
return self.colorspace == ZS and self.has_channel(1)
329
330
def stride(self):
331
return self.block_size()/8
332
333
334
_type_parse_map = {
335
'': VOID,
336
'x': VOID,
337
'u': UNSIGNED,
338
's': SIGNED,
339
'h': FIXED,
340
'f': FLOAT,
341
}
342
343
_swizzle_parse_map = {
344
'x': SWIZZLE_X,
345
'y': SWIZZLE_Y,
346
'z': SWIZZLE_Z,
347
'w': SWIZZLE_W,
348
'0': SWIZZLE_0,
349
'1': SWIZZLE_1,
350
'_': SWIZZLE_NONE,
351
}
352
353
354
def _parse_channels(fields, layout, colorspace, swizzles):
355
if layout == PLAIN:
356
names = ['']*4
357
if colorspace in (RGB, SRGB):
358
for i in range(4):
359
swizzle = swizzles[i]
360
if swizzle < 4:
361
names[swizzle] += 'rgba'[i]
362
elif colorspace == ZS:
363
for i in range(4):
364
swizzle = swizzles[i]
365
if swizzle < 4:
366
names[swizzle] += 'zs'[i]
367
else:
368
assert False
369
for i in range(4):
370
if names[i] == '':
371
names[i] = 'x'
372
else:
373
names = ['x', 'y', 'z', 'w']
374
375
channels = []
376
for i in range(0, 4):
377
field = fields[i]
378
if field:
379
type = _type_parse_map[field[0]]
380
if field[1] == 'n':
381
norm = True
382
pure = False
383
size = int(field[2:])
384
elif field[1] == 'p':
385
pure = True
386
norm = False
387
size = int(field[2:])
388
else:
389
norm = False
390
pure = False
391
size = int(field[1:])
392
else:
393
type = VOID
394
norm = False
395
pure = False
396
size = 0
397
channel = Channel(type, norm, pure, size, names[i])
398
channels.append(channel)
399
400
return channels
401
402
403
def parse(filename):
404
'''Parse the format description in CSV format in terms of the
405
Channel and Format classes above.'''
406
407
stream = open(filename)
408
formats = []
409
for line in stream:
410
try:
411
comment = line.index('#')
412
except ValueError:
413
pass
414
else:
415
line = line[:comment]
416
line = line.strip()
417
if not line:
418
continue
419
420
fields = [field.strip() for field in line.split(',')]
421
assert(len(fields) == 11 or len(fields) == 16)
422
423
name = fields[0]
424
layout = fields[1]
425
block_width, block_height, block_depth = map(int, fields[2:5])
426
colorspace = fields[10]
427
428
le_swizzles = [_swizzle_parse_map[swizzle] for swizzle in fields[9]]
429
le_channels = _parse_channels(fields[5:9], layout, colorspace, le_swizzles)
430
431
be_swizzles = None
432
be_channels = None
433
if len(fields) == 16:
434
be_swizzles = [_swizzle_parse_map[swizzle]
435
436
for swizzle in fields[15]]
437
be_channels = _parse_channels(
438
439
fields[11:15], layout, colorspace, be_swizzles)
440
441
format = Format(name, layout, block_width, block_height, block_depth, le_channels, le_swizzles, be_channels, be_swizzles, colorspace)
442
formats.append(format)
443
return formats
444
445