updating spv parser

This commit is contained in:
Peter Li 2025-04-16 22:56:00 -07:00
parent b95ef81492
commit 69df1ea67e
8 changed files with 235 additions and 44 deletions

View File

@ -3,6 +3,14 @@
using namespace metal; using namespace metal;
struct type_TransformBuffer
{
float4x4 modelMatrix;
float4x4 viewMatrix;
float4x4 projectionMatrix;
float time;
};
struct Scene struct Scene
{ {
float4 color; float4 color;
@ -13,8 +21,19 @@ struct type_StructuredBuffer_Scene
Scene _m0[1]; Scene _m0[1];
}; };
constant float2 _30 = {}; struct Scene2
constant float4 _31 = {}; {
float4 color;
float4 lmfao;
};
struct type_StructuredBuffer_Scene2
{
Scene2 _m0[1];
};
constant float2 _40 = {};
constant float4 _41 = {};
struct main0_out struct main0_out
{ {
@ -22,45 +41,45 @@ struct main0_out
float4 gl_Position [[position]]; float4 gl_Position [[position]];
}; };
vertex main0_out main0(const device type_StructuredBuffer_Scene& test [[buffer(0)]], uint gl_VertexIndex [[vertex_id]]) vertex main0_out main0(constant type_TransformBuffer& TransformBuffer [[buffer(0)]], const device type_StructuredBuffer_Scene& test [[buffer(1)]], const device type_StructuredBuffer_Scene2& test2 [[buffer(2)]], uint gl_VertexIndex [[vertex_id]])
{ {
main0_out out = {}; main0_out out = {};
float4 _58; float4 _68;
float2 _59; float2 _69;
if (gl_VertexIndex == 0u) if (gl_VertexIndex == 0u)
{ {
_58 = test._m0[0u].color; _68 = test._m0[0u].color;
_59 = float2(-1.0); _69 = float2(-1.0);
} }
else else
{ {
float4 _56; float4 _66;
float2 _57; float2 _67;
if (gl_VertexIndex == 1u) if (gl_VertexIndex == 1u)
{ {
_56 = test._m0[1u].color; _66 = test2._m0[1u].color;
_57 = float2(1.0, -1.0); _67 = float2(1.0, -1.0);
} }
else else
{ {
bool _48 = gl_VertexIndex == 2u; bool _58 = gl_VertexIndex == 2u;
float4 _53; float4 _63;
if (_48) if (_58)
{ {
_53 = test._m0[2u].color; _63 = test._m0[2u].color;
} }
else else
{ {
_53 = _31; _63 = _41;
} }
_56 = _53; _66 = _63;
_57 = select(_30, float2(0.0, 1.0), bool2(_48)); _67 = select(_40, float2(0.0, 1.0), bool2(_58));
} }
_58 = _56; _68 = _66;
_59 = _57; _69 = _67;
} }
out.out_var_TEXCOORD0 = _58; out.out_var_TEXCOORD0 = _68;
out.gl_Position = float4(_59, 0.0, 1.0); out.gl_Position = TransformBuffer.modelMatrix * float4(_69, 0.0, 1.0);
return out; return out;
} }

View File

@ -9,12 +9,27 @@ struct Output
float4 Position : SV_Position; float4 Position : SV_Position;
}; };
struct Scene2
{
float4 color;
float4 lmfao;
};
struct Scene struct Scene
{ {
float4 color; float4 color;
}; };
cbuffer TransformBuffer : register(b0, space1)
{
float4x4 modelMatrix;
float4x4 viewMatrix;
float4x4 projectionMatrix;
float time;
};
StructuredBuffer<Scene> test: register(t0, space0); StructuredBuffer<Scene> test: register(t0, space0);
StructuredBuffer<Scene2> test2: register(t1, space0);
Output main(Input input) Output main(Input input)
{ {
@ -30,7 +45,7 @@ Output main(Input input)
if (input.VertexIndex == 1) if (input.VertexIndex == 1)
{ {
pos = float2(1.0f, -1.0f); pos = float2(1.0f, -1.0f);
output.Color = test[1].color; output.Color = test2[1].color;
} }
else else
{ {
@ -41,6 +56,6 @@ Output main(Input input)
} }
} }
} }
output.Position = float4(pos, 0.0f, 1.0f); output.Position = mul(modelMatrix, float4(pos, 0.0f, 1.0f));
return output; return output;
} }

View File

@ -6,7 +6,38 @@
} }
], ],
"types" : { "types" : {
"_6" : { "_5" : {
"name" : "type.TransformBuffer",
"members" : [
{
"name" : "modelMatrix",
"type" : "mat4",
"offset" : 0,
"matrix_stride" : 16,
"row_major" : true
},
{
"name" : "viewMatrix",
"type" : "mat4",
"offset" : 64,
"matrix_stride" : 16,
"row_major" : true
},
{
"name" : "projectionMatrix",
"type" : "mat4",
"offset" : 128,
"matrix_stride" : 16,
"row_major" : true
},
{
"name" : "time",
"type" : "float",
"offset" : 192
}
]
},
"_8" : {
"name" : "Scene", "name" : "Scene",
"members" : [ "members" : [
{ {
@ -16,12 +47,12 @@
} }
] ]
}, },
"_5" : { "_7" : {
"name" : "type.StructuredBuffer.Scene", "name" : "type.StructuredBuffer.Scene",
"members" : [ "members" : [
{ {
"name" : "_m0", "name" : "_m0",
"type" : "_6", "type" : "_8",
"array" : [ "array" : [
0 0
], ],
@ -32,6 +63,38 @@
"array_stride" : 16 "array_stride" : 16
} }
] ]
},
"_11" : {
"name" : "Scene2",
"members" : [
{
"name" : "color",
"type" : "vec4",
"offset" : 0
},
{
"name" : "lmfao",
"type" : "vec4",
"offset" : 16
}
]
},
"_10" : {
"name" : "type.StructuredBuffer.Scene2",
"members" : [
{
"name" : "_m0",
"type" : "_11",
"array" : [
0
],
"array_size_is_literal" : [
true
],
"offset" : 0,
"array_stride" : 32
}
]
} }
}, },
"outputs" : [ "outputs" : [
@ -43,12 +106,29 @@
], ],
"ssbos" : [ "ssbos" : [
{ {
"type" : "_5", "type" : "_7",
"name" : "test", "name" : "test",
"readonly" : true, "readonly" : true,
"block_size" : 0, "block_size" : 0,
"set" : 0, "set" : 0,
"binding" : 0 "binding" : 0
},
{
"type" : "_10",
"name" : "test2",
"readonly" : true,
"block_size" : 0,
"set" : 0,
"binding" : 1
}
],
"ubos" : [
{
"type" : "_5",
"name" : "type.TransformBuffer",
"block_size" : 196,
"set" : 1,
"binding" : 0
} }
] ]
} }

View File

@ -92,3 +92,17 @@ pub const FieldDetail = struct {
size: usize, size: usize,
offset: usize, offset: usize,
}; };
pub const ShaderLoadArgs = struct {
num_samplers: u32, // The number of samplers defined in the shader.
num_storage_textures: u32, // The number of storage textures defined in the shader.
num_storage_buffers: u32, // The number of storage buffers defined in the shader.
num_uniform_buffers: u32, // The number of uniform buffers defined in the shader.
};
pub const BufferInfo = union(enum(usize)) {
sampler: usize, // sampler
texture: usize, // storage texture
storage: usize, // storate buffer object
uniform: usize, // uniform/push constant
};

View File

@ -10,6 +10,32 @@ pub const vec4 = shaderTypes.vec4;
pub const mat4 = shaderTypes.mat4; pub const mat4 = shaderTypes.mat4;
pub const float = shaderTypes.float; pub const float = shaderTypes.float;
pub const BufferInfo = shaderTypes.BufferInfo;
pub const Scene = struct { pub const Scene = struct {
color: vec4, color: vec4,
pub const Buffer: BufferInfo = .{ .storage = 0 };
};
pub const Scene2 = struct {
color: vec4,
lmfao: vec4,
pub const Buffer: BufferInfo = .{ .storage = 1 };
};
pub const TransformBuffer = struct {
modelMatrix: mat4,
viewMatrix: mat4,
projectionMatrix: mat4,
time: float,
pub const Buffer: BufferInfo = .{ .uniform = 0 };
};
pub const LoadArgs = shaderTypes.ShaderLoadArgs{
.num_samplers = 0, // The number of samplers defined in the shader.
.num_storage_textures = 0, // The number of storage textures defined in the shader.
.num_storage_buffers = 2, // The number of storage buffers defined in the shader.
.num_uniform_buffers = 0, // The number of uniform buffers defined in the shader.
}; };

View File

@ -27,7 +27,7 @@ def generate():
with open(infile) as f: with open(infile) as f:
reflect = json.load(f) reflect = json.load(f)
print(reflect) print(json.dumps(reflect, indent=2))
ostring = "pub const shaderTypes = @import(\"shaderTypes\");\n" ostring = "pub const shaderTypes = @import(\"shaderTypes\");\n"
@ -42,35 +42,72 @@ pub const vec4 = shaderTypes.vec4;
pub const mat4 = shaderTypes.mat4; pub const mat4 = shaderTypes.mat4;
pub const float = shaderTypes.float; pub const float = shaderTypes.float;
pub const BufferInfo = shaderTypes.BufferInfo;
""" """
types = reflect['types'] types = reflect['types']
for t in types:
name = types[t]['name'];
if 'type.StructuredBuffer' in name: storageIndex = 0
continue for ssbo in reflect['ssbos']:
zs = parseTypeToZig(types, ssbo['type'], "storage", storageIndex)
storageIndex += 1
ostring += zs + "\n"
if name.startswith("type.StructuredBuffer"): uniformIndex = 0
name = name[len("type.StructuredBuffer"):] for uniform in reflect['ubos']:
zs = parseTypeToZig(types, uniform['type'], "uniform", uniformIndex)
ostring += zs + "\n"
zs = f"pub const {name} = " + 'struct {\n' zs = "pub const LoadArgs = shaderTypes.ShaderLoadArgs{"
for member in types[t]['members']: samplerIndex = 0
zs += member['name'] + ':' + member['type'] + ',\n' textureIndex = 0
zs += "};\n" zs += f"""
.num_samplers = {samplerIndex}, // The number of samplers defined in the shader.
print(zs) .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 ostring += zs
with open(outfile, 'w', newline='\n') as f:
with open(outfile, 'w') as f:
f.write(ostring) f.write(ostring)
subprocess.run(['zig', 'fmt', outfile]) 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__': if __name__ == '__main__':
generate() generate()