Backlog/lib/sdl3/spvreflect/spvreflect.py

124 lines
3.3 KiB
Python

import os
import argparse
import subprocess
import json
# THIS IS OLD, DO NOT USE
# 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(json.dumps(reflect, indent=2))
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;
pub const BufferInfo = shaderTypes.BufferInfo;
"""
storageIndex = 0
uniformIndex = 0
samplerIndex = 0
textureIndex = 0
if 'types' in reflect:
types = reflect['types']
if 'ssbos' in reflect:
for ssbo in reflect['ssbos']:
zs = parseTypeToZig(types, ssbo['type'], "storage", ssbo['binding'])
storageIndex += 1
ostring += zs + "\n"
if 'ubos' in reflect:
for uniform in reflect['ubos']:
uniformIndex += 1
zs = parseTypeToZig(types, uniform['type'], "uniform", uniform['binding'])
ostring += zs + "\n"
if 'separate_samplers' in reflect:
samplerIndex += len(reflect['separate_samplers'])
zs = "pub const LoadArgs = shaderTypes.ShaderLoadArgs{"
zs += f"""
.num_samplers = {samplerIndex}, // The number of samplers defined in the shader.
.num_storage_textures = {textureIndex}, // The number of storage textures defined in the shader.
.num_storage_buffers = {storageIndex}, // The number of storage buffers defined in the shader.
.num_uniform_buffers = {uniformIndex}, // The number of uniform buffers defined in the shader.
"""
zs += "};"
ostring += zs
with open(outfile, 'w', newline='\n') as f:
f.write(ostring)
subprocess.run(['zig', 'fmt', outfile])
def parseTypeToZig(types, typeId, bufferType, bufferIndex):
displayName = types[typeId]['name']
isStructured = False
typeInner = types[typeId]
arrayStride = None
if "type.StructuredBuffer." in displayName:
displayName = displayName[len("type.StructuredBuffer."):]
isStructured = True
arrayStride = typeInner['members'][0]['array_stride']
typeInner = types[typeInner['members'][0]['type']]
if "type." in displayName:
displayName = displayName[len("type."):]
zs = "pub const " + displayName + " = struct {"
for t in typeInner['members']:
zs += t['name'] + ':' + t['type'] + ','
zs += '\n\npub const Buffer: BufferInfo = .{ .' + bufferType + ' = ' + str(bufferIndex) + ' };'
zs += "};\n"
return zs
if __name__ == '__main__':
generate()
pass