Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/util/format/u_format_table.py
7099 views
1
from __future__ import print_function
2
3
CopyRight = '''
4
/**************************************************************************
5
*
6
* Copyright 2010 VMware, Inc.
7
* All Rights Reserved.
8
*
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:
16
*
17
* The above copyright notice and this permission notice (including the
18
* next paragraph) shall be included in all copies or substantial portions
19
* of the Software.
20
*
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.
28
*
29
**************************************************************************/
30
'''
31
32
33
import sys, os
34
35
from u_format_parse import *
36
import u_format_pack
37
38
39
def layout_map(layout):
40
return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper()
41
42
43
def colorspace_map(colorspace):
44
return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper()
45
46
47
colorspace_channels_map = {
48
'rgb': ['r', 'g', 'b', 'a'],
49
'srgb': ['sr', 'sg', 'sb', 'a'],
50
'zs': ['z', 's'],
51
'yuv': ['y', 'u', 'v'],
52
}
53
54
55
type_map = {
56
VOID: "UTIL_FORMAT_TYPE_VOID",
57
UNSIGNED: "UTIL_FORMAT_TYPE_UNSIGNED",
58
SIGNED: "UTIL_FORMAT_TYPE_SIGNED",
59
FIXED: "UTIL_FORMAT_TYPE_FIXED",
60
FLOAT: "UTIL_FORMAT_TYPE_FLOAT",
61
}
62
63
64
def bool_map(value):
65
if value:
66
return "TRUE"
67
else:
68
return "FALSE"
69
70
71
swizzle_map = {
72
SWIZZLE_X: "PIPE_SWIZZLE_X",
73
SWIZZLE_Y: "PIPE_SWIZZLE_Y",
74
SWIZZLE_Z: "PIPE_SWIZZLE_Z",
75
SWIZZLE_W: "PIPE_SWIZZLE_W",
76
SWIZZLE_0: "PIPE_SWIZZLE_0",
77
SWIZZLE_1: "PIPE_SWIZZLE_1",
78
SWIZZLE_NONE: "PIPE_SWIZZLE_NONE",
79
}
80
81
def has_access(format):
82
# We don't generate code for YUV formats, and many of the new ones lack
83
# pack/unpack functions for softpipe/llvmpipe.
84
noaccess_formats = [
85
'r1_unorm',
86
'yv12',
87
'yv16',
88
'iyuv',
89
'nv12',
90
'nv16',
91
'nv21',
92
'p010',
93
'p012',
94
'p016',
95
'y210',
96
'y212',
97
'y216',
98
'y410',
99
'y412',
100
'y416',
101
'xyuv',
102
'ayuv',
103
'r8g8_r8b8_unorm',
104
'g8r8_b8r8_unorm',
105
'g8r8_g8b8_unorm',
106
'y8_u8_v8_422_unorm',
107
'y8_u8v8_422_unorm',
108
'y8_u8_v8_444_unorm',
109
'y16_u16_v16_420_unorm',
110
'y16_u16_v16_422_unorm',
111
'y16_u16v16_422_unorm',
112
'y16_u16_v16_444_unorm',
113
'r8_g8b8_420_unorm',
114
]
115
if format.short_name() in noaccess_formats:
116
return False
117
if format.layout in ('astc', 'atc'):
118
return False
119
if format.layout == 'etc' and format.short_name() != 'etc1_rgb8':
120
return False
121
return True
122
123
def write_format_table_header(file):
124
print('/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */', file=file)
125
print(file=file)
126
# This will print the copyright message on the top of this file
127
print(CopyRight.strip(), file=file)
128
print(file=file)
129
print('#include "util/format/u_format.h"', file=file)
130
131
def write_format_table(formats):
132
write_format_table_header(sys.stdout)
133
print('#include "u_format_bptc.h"')
134
print('#include "u_format_fxt1.h"')
135
print('#include "u_format_s3tc.h"')
136
print('#include "u_format_rgtc.h"')
137
print('#include "u_format_latc.h"')
138
print('#include "u_format_etc.h"')
139
print()
140
141
write_format_table_header(sys.stdout2)
142
143
u_format_pack.generate(formats)
144
145
def do_channel_array(channels, swizzles):
146
print(" {")
147
for i in range(4):
148
channel = channels[i]
149
if i < 3:
150
sep = ","
151
else:
152
sep = ""
153
if channel.size:
154
print(" {%s, %s, %s, %u, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), bool_map(channel.pure), channel.size, channel.shift, sep, "xyzw"[i], channel.name))
155
else:
156
print(" {0, 0, 0, 0, 0}%s" % (sep,))
157
print(" },")
158
159
def do_swizzle_array(channels, swizzles):
160
print(" {")
161
for i in range(4):
162
swizzle = swizzles[i]
163
if i < 3:
164
sep = ","
165
else:
166
sep = ""
167
try:
168
comment = colorspace_channels_map[format.colorspace][i]
169
except (KeyError, IndexError):
170
comment = 'ignored'
171
print(" %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment))
172
print(" },")
173
174
def generate_table_getter(type):
175
suffix = ""
176
if type == "unpack_":
177
suffix = "_generic"
178
print("const struct util_format_%sdescription *" % type)
179
print("util_format_%sdescription%s(enum pipe_format format)" % (type, suffix))
180
print("{")
181
print(" if (format >= ARRAY_SIZE(util_format_%sdescriptions))" % (type))
182
print(" return NULL;")
183
print()
184
print(" return &util_format_%sdescriptions[format];" % (type))
185
print("}")
186
print()
187
188
def generate_function_getter(func):
189
print("util_format_%s_func_ptr" % func)
190
print("util_format_%s_func(enum pipe_format format)" % (func))
191
print("{")
192
print(" if (format >= ARRAY_SIZE(util_format_%s_table))" % (func))
193
print(" return NULL;")
194
print()
195
print(" return util_format_%s_table[format];" % (func))
196
print("}")
197
print()
198
199
print('static const struct util_format_description')
200
print('util_format_descriptions[] = {')
201
for format in formats:
202
sn = format.short_name()
203
204
print(" [%s] = {" % (format.name,))
205
print(" %s," % (format.name,))
206
print(" \"%s\"," % (format.name,))
207
print(" \"%s\"," % (sn,))
208
print(" {%u, %u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_depth, format.block_size()))
209
print(" %s," % (layout_map(format.layout),))
210
print(" %u,\t/* nr_channels */" % (format.nr_channels(),))
211
print(" %s,\t/* is_array */" % (bool_map(format.is_array()),))
212
print(" %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),))
213
print(" %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),))
214
print(" %s,\t/* is_unorm */" % (bool_map(format.is_unorm()),))
215
print(" %s,\t/* is_snorm */" % (bool_map(format.is_snorm()),))
216
u_format_pack.print_channels(format, do_channel_array)
217
u_format_pack.print_channels(format, do_swizzle_array)
218
print(" %s," % (colorspace_map(format.colorspace),))
219
print(" },")
220
print()
221
print("};")
222
print()
223
generate_table_getter("")
224
225
print('static const struct util_format_pack_description')
226
print('util_format_pack_descriptions[] = {')
227
for format in formats:
228
sn = format.short_name()
229
230
if not has_access(format):
231
print(" [%s] = { 0 }," % (format.name,))
232
continue
233
234
print(" [%s] = {" % (format.name,))
235
if format.colorspace != ZS and not format.is_pure_color():
236
print(" .pack_rgba_8unorm = &util_format_%s_pack_rgba_8unorm," % sn)
237
print(" .pack_rgba_float = &util_format_%s_pack_rgba_float," % sn)
238
239
if format.has_depth():
240
print(" .pack_z_32unorm = &util_format_%s_pack_z_32unorm," % sn)
241
print(" .pack_z_float = &util_format_%s_pack_z_float," % sn)
242
243
if format.has_stencil():
244
print(" .pack_s_8uint = &util_format_%s_pack_s_8uint," % sn)
245
246
if format.is_pure_unsigned() or format.is_pure_signed():
247
print(" .pack_rgba_uint = &util_format_%s_pack_unsigned," % sn)
248
print(" .pack_rgba_sint = &util_format_%s_pack_signed," % sn)
249
print(" },")
250
print()
251
print("};")
252
print()
253
generate_table_getter("pack_")
254
print('static const struct util_format_unpack_description')
255
print('util_format_unpack_descriptions[] = {')
256
for format in formats:
257
sn = format.short_name()
258
259
if not has_access(format):
260
print(" [%s] = { 0 }," % (format.name,))
261
continue
262
263
print(" [%s] = {" % (format.name,))
264
265
if format.colorspace != ZS and not format.is_pure_color():
266
if format.layout == 's3tc' or format.layout == 'rgtc':
267
print(" .fetch_rgba_8unorm = &util_format_%s_fetch_rgba_8unorm," % sn)
268
if format.block_width > 1:
269
print(
270
" .unpack_rgba_8unorm_rect = &util_format_%s_unpack_rgba_8unorm," % sn)
271
print(
272
" .unpack_rgba_rect = &util_format_%s_unpack_rgba_float," % sn)
273
else:
274
print(
275
" .unpack_rgba_8unorm = &util_format_%s_unpack_rgba_8unorm," % sn)
276
print(" .unpack_rgba = &util_format_%s_unpack_rgba_float," % sn)
277
278
if format.has_depth():
279
print(" .unpack_z_32unorm = &util_format_%s_unpack_z_32unorm," % sn)
280
print(" .unpack_z_float = &util_format_%s_unpack_z_float," % sn)
281
282
if format.has_stencil():
283
print(" .unpack_s_8uint = &util_format_%s_unpack_s_8uint," % sn)
284
285
if format.is_pure_unsigned():
286
print(" .unpack_rgba = &util_format_%s_unpack_unsigned," % sn)
287
elif format.is_pure_signed():
288
print(" .unpack_rgba = &util_format_%s_unpack_signed," % sn)
289
print(" },")
290
print("};")
291
print()
292
293
generate_table_getter("unpack_")
294
295
print('static const util_format_fetch_rgba_func_ptr util_format_fetch_rgba_table[] = {')
296
for format in formats:
297
sn = format.short_name()
298
299
if format.colorspace != ZS and has_access(format):
300
print(" [%s] = &util_format_%s_fetch_rgba," % (format.name, sn))
301
else:
302
print(" [%s] = NULL," % format.name)
303
304
print("};")
305
print()
306
307
generate_function_getter("fetch_rgba")
308
309
def main():
310
formats = []
311
312
sys.stdout2 = open(os.devnull, "w")
313
314
for arg in sys.argv[1:]:
315
if arg == '--header':
316
sys.stdout2 = sys.stdout
317
sys.stdout = open(os.devnull, "w")
318
continue
319
320
formats.extend(parse(arg))
321
322
write_format_table(formats)
323
324
if __name__ == '__main__':
325
main()
326
327