Skip to content

Commit 76253c2

Browse files
committed
hlsl_generator: generate glsl.std extended instructions
1 parent c387d96 commit 76253c2

File tree

2 files changed

+348
-13
lines changed

2 files changed

+348
-13
lines changed

tools/hlsl_generator/gen.py

+29-13
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,12 @@
108108
#endif
109109
"""
110110

111-
def gen(grammer_path, output_path):
112-
grammer_raw = open(grammer_path, "r").read()
113-
grammer = json.loads(grammer_raw)
114-
del grammer_raw
115-
111+
def gen(core_grammer, glsl_grammer, output_path):
116112
output = open(output_path, "w", buffering=1024**2)
117113

118-
builtins = [x for x in grammer["operand_kinds"] if x["kind"] == "BuiltIn"][0]["enumerants"]
119-
execution_modes = [x for x in grammer["operand_kinds"] if x["kind"] == "ExecutionMode"][0]["enumerants"]
120-
group_operations = [x for x in grammer["operand_kinds"] if x["kind"] == "GroupOperation"][0]["enumerants"]
114+
builtins = [x for x in core_grammer["operand_kinds"] if x["kind"] == "BuiltIn"][0]["enumerants"]
115+
execution_modes = [x for x in core_grammer["operand_kinds"] if x["kind"] == "ExecutionMode"][0]["enumerants"]
116+
group_operations = [x for x in core_grammer["operand_kinds"] if x["kind"] == "GroupOperation"][0]["enumerants"]
121117

122118
with output as writer:
123119
writer.write(head)
@@ -194,7 +190,7 @@ def gen(grammer_path, output_path):
194190
writer.write("}\n")
195191

196192
writer.write("\n//! Instructions\n")
197-
for instruction in grammer["instructions"]:
193+
for instruction in core_grammer["instructions"]:
198194
if instruction["opname"].endswith("INTEL"): continue
199195

200196
match instruction["class"]:
@@ -219,6 +215,9 @@ def gen(grammer_path, output_path):
219215
processInst(writer, instruction, result_ty="uint32_t",prefered_op_ty="uint32_t4")
220216
case _: processInst(writer, instruction)
221217
case _: continue # TODO
218+
for instruction in glsl_grammer["instructions"]:
219+
instruction["operands"] = [{"kind": "IdResultType"}] + instruction["operands"]
220+
processInst(writer, instruction)
222221

223222
writer.write(foot)
224223

@@ -266,7 +265,7 @@ def processInst(writer: io.TextIOWrapper,
266265
conds.append("(is_same_v<float16_t, T> || is_same_v<float32_t, T> || is_same_v<float64_t, T>)")
267266
break
268267
else:
269-
if instruction["class"] == "Bit":
268+
if "class" in instruction and instruction["class"] == "Bit":
270269
conds.append("(is_signed_v<T> || is_unsigned_v<T>)")
271270

272271
if "operands" in instruction and instruction["operands"][0]["kind"] == "IdResultType":
@@ -321,7 +320,11 @@ def processInst(writer: io.TextIOWrapper,
321320
if (not "typename T" in final_templates) and (result_ty == "T" or op_ty == "T"):
322321
final_templates = ["typename T"] + final_templates
323322
args.append("[[vk::ext_reference]] " + op_ty + " " + operand_name)
324-
case "'Value'" | "'Object'" | "'Comparator'" | "'Base'" | "'Insert'":
323+
case ("'a'" | "'b'" | "'c'" | "'x'" | "'y'" | "'z'" | "'i'" | "'v'" |
324+
"'p'" | "'p0'" | "'p1'" | "'exp'" | "'minVal'" | "'maxVal'" | "'y_over_x'" |
325+
"'edge'" | "'edge0'" | "'edge1'" | "'I'" | "'N'" | "'eta'" | "'sample'" |
326+
"'degrees'" | "'radians'" | "'Nref'" | "'interpolant'" | "'offset'" |
327+
"'Value'" | "'Object'" | "'Comparator'" | "'Base'" | "'Insert'"):
325328
if (not "typename T" in final_templates) and (result_ty == "T" or op_ty == "T"):
326329
final_templates = ["typename T"] + final_templates
327330
args.append(op_ty + " " + operand_name)
@@ -366,8 +369,21 @@ def ignore(op_name):
366369

367370
parser = ArgumentParser(description="Generate HLSL from SPIR-V instructions")
368371
parser.add_argument("output", type=str, help="HLSL output file")
369-
parser.add_argument("--grammer", required=False, type=str, help="Input SPIR-V grammer JSON file", default=os.path.join(script_dir_path, "../../include/spirv/unified1/spirv.core.grammar.json"))
372+
parser.add_argument("--core-grammer", required=False, type=str,
373+
help="SPIR-V Core grammer JSON file",
374+
default=os.path.join(script_dir_path, "../../include/spirv/unified1/spirv.core.grammar.json"))
375+
parser.add_argument("--glsl-grammer", required=False, type=str,
376+
help="SPIR-V Extended GLSL.std.450 grammer JSON file",
377+
default=os.path.join(script_dir_path, "../../include/spirv/unified1/extinst.glsl.std.450.grammar.json"))
370378
args = parser.parse_args()
371379

372-
gen(args.grammer, args.output)
380+
grammer_raw = open(args.core_grammer, "r").read()
381+
core_grammer = json.loads(grammer_raw)
382+
del grammer_raw
383+
384+
grammer_raw = open(args.glsl_grammer, "r").read()
385+
glsl_grammer = json.loads(grammer_raw)
386+
del grammer_raw
387+
388+
gen(core_grammer, glsl_grammer, args.output)
373389

0 commit comments

Comments
 (0)