1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# Copyright © 2022 Intel Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# Converts GLSL shader to SPIR-V library
import argparse
import subprocess
import sys
import os
import typing as T
if T.TYPE_CHECKING:
class Arguments(T.Protocol):
input: str
output: str
glslang: str
create_entry: T.Optional[str]
glsl_ver: T.Optional[str]
Olib: bool
extra: T.Optional[str]
vn: str
stage: str
includes: T.List[str]
defines: T.List[str]
depfile: T.Optional[str]
def get_args() -> 'Arguments':
parser = argparse.ArgumentParser()
parser.add_argument('input', help="Name of input file.")
parser.add_argument('output', help="Name of output file.")
parser.add_argument('glslang', help="path to glslangValidator")
parser.add_argument("--create-entry",
dest="create_entry",
help="Create a new entry point and put to the end of a file.")
parser.add_argument('--glsl-version',
dest="glsl_ver",
choices=['100', '110', '120', '130', '140', '150', '300es', '310es', '330', '400', '410', '420', '430', '440', '450', '460'],
help="Override GLSL #version declaration in source.")
parser.add_argument("-Olib",
action='store_true',
help="Any optimizations are disabled and unused functions are saved.")
parser.add_argument("--extra-flags",
dest="extra",
help="Pass additional flags to glslangValidator.")
parser.add_argument("--vn",
dest="vn",
required=True,
help="Variable name. Creates a C header file that contains a uint32_t array.")
parser.add_argument("--stage",
default="vert",
choices=['vert', 'tesc', 'tese', 'geom', 'frag', 'comp'],
help="Uses specified stage rather than parsing the file extension")
parser.add_argument("-I",
dest="includes",
default=[],
action='append',
help="Include directory")
parser.add_argument("-D",
dest="defines",
default=[],
action='append',
help="Defines")
parser.add_argument('--depfile',
dest="depfile",
default=None,
action='store',
help='Where glslangValidator should write its depfile, if unset no depfile will be written.')
args = parser.parse_args()
return args
def create_include_guard(lines: T.List[str], filename: str) -> T.List[str]:
filename = filename.replace('.', '_')
upper_name = filename.upper()
guard_head = [f"#ifndef {upper_name}\n",
f"#define {upper_name}\n"]
guard_tail = [f"\n#endif // {upper_name}\n"]
# remove '#pragma once'
for idx, l in enumerate(lines):
if '#pragma once' in l:
lines.pop(idx)
break
return guard_head + lines + guard_tail
def convert_to_static_variable(lines: T.List[str], varname: str) -> T.List[str]:
for idx, l in enumerate(lines):
if varname in l:
lines[idx] = f"static {l}"
return lines
raise RuntimeError(f'Did not find {varname}, this is unexpected')
def override_version(lines: T.List[str], glsl_version: str) -> T.List[str]:
for idx, l in enumerate(lines):
if '#version ' in l:
lines[idx] = f"#version {glsl_version}\n"
return lines
raise RuntimeError('Did not find #version directive, this is unexpected')
def postprocess_file(args: 'Arguments') -> None:
with open(args.output, "r") as r:
lines = r.readlines()
# glslangValidator creates a variable without the static modifier.
lines = convert_to_static_variable(lines, args.vn)
# '#pragma once' is unstandardised.
lines = create_include_guard(lines, os.path.basename(r.name))
with open(args.output, "w") as w:
w.writelines(lines)
def preprocess_file(args: 'Arguments', origin_file: T.TextIO, directory: os.PathLike, filemap: T.Dict[str, str]) -> str:
if args.create_entry is None and args.glsl_ver is None:
return origin_file.name
with open(os.path.join(directory, os.path.basename(origin_file.name)), "w") as copy_file:
lines = origin_file.readlines()
if args.create_entry is not None:
lines.append(f"\nvoid {args.create_entry}() {{}}\n")
if args.glsl_ver is not None:
override_version(lines, args.glsl_ver)
copy_file.writelines(lines)
filemap[copy_file.name] = origin_file.name
return copy_file.name
def fixup_depfile(depfile: str, filemap: T.Mapping[str, str]) -> None:
"""Replace generated file references in the depfile with their source.
"""
depends: T.List[str] = []
out: T.Optional[str] = None
with open(depfile, 'r') as f:
for line in f.readlines():
if ':' in line:
out, line = line.split(':', 1)
depends.extend(l.strip() for l in line.split())
with open(depfile, 'w') as f:
f.write(out)
f.write(': ')
for dep in depends:
f.write(filemap.get(dep, dep))
f.write(' ')
f.write('\n')
def process_file(args: 'Arguments') -> None:
filemap: T.Dict[str, str] = {}
with open(args.input, "r") as infile:
copy_file = preprocess_file(
args, infile, os.path.dirname(args.output), filemap)
cmd_list = [args.glslang]
if args.Olib:
cmd_list.append("--keep-uncalled")
if args.vn is not None:
cmd_list.extend(["--variable-name", args.vn])
if args.extra is not None:
cmd_list.append(args.extra)
if args.create_entry is not None:
cmd_list.extend(["--entry-point", args.create_entry])
if args.depfile is not None:
cmd_list.extend(['--depfile', args.depfile])
for f in args.includes:
cmd_list.append('-I' + f)
for d in args.defines:
cmd_list.append('-D' + d)
cmd_list.extend([
'-V',
'-o', args.output,
'-S', args.stage,
copy_file,
])
ret = subprocess.run(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=30)
if ret.returncode != 0:
print(ret.stdout)
print(ret.stderr, file=sys.stderr)
sys.exit(1)
if args.vn is not None:
postprocess_file(args)
if args.create_entry is not None:
os.remove(copy_file)
if args.depfile is not None:
fixup_depfile(args.depfile, filemap)
def main() -> None:
args = get_args()
process_file(args)
if __name__ == "__main__":
main()
|