Backlog/lib/sdl3/spvreflect/spvreflect.py

78 lines
1.7 KiB
Python

import os
import argparse
import subprocess
import json
# quick and dirty dependency-free script to
# build and output reflected zig files for
# creating definitions for use with the json defs created
# from spirv-cross
# usage:
# python spvreflect defs.json
def generate():
print()
parser = argparse.ArgumentParser()
parser.add_argument('input', type=str, help="input file")
parser.add_argument('-o', '--output', type=str, help="output file")
args = parser.parse_args()
infile = os.path.abspath(args.input)
outfile = 'reflected.zig'
if args.output is not None:
outfile = args.output
reflect = None
with open(infile) as f:
reflect = json.load(f)
print(reflect)
ostring = "pub const shaderTypes = @import(\"shaderTypes\");\n"
ostring += """
pub const int = shaderTypes.int;
pub const uint = shaderTypes.uint;
pub const vec2 = shaderTypes.vec2;
pub const u8vec4 = shaderTypes.u8vec4;
pub const vec3 = shaderTypes.vec3;
pub const vec4 = shaderTypes.vec4;
pub const mat4 = shaderTypes.mat4;
pub const float = shaderTypes.float;
"""
types = reflect['types']
for t in types:
name = types[t]['name'];
if 'type.StructuredBuffer' in name:
continue
if name.startswith("type.StructuredBuffer"):
name = name[len("type.StructuredBuffer"):]
zs = f"pub const {name} = " + 'struct {\n'
for member in types[t]['members']:
zs += member['name'] + ':' + member['type'] + ',\n'
zs += "};\n"
print(zs)
ostring += zs
with open(outfile, 'w') as f:
f.write(ostring)
subprocess.run(['zig', 'fmt', outfile])
if __name__ == '__main__':
generate()
pass