working on restuls

This commit is contained in:
2026-01-07 19:13:10 +01:00
parent 02b8c69838
commit 52c1f96040
3 changed files with 80 additions and 18 deletions

View File

@@ -17,7 +17,7 @@ const Result = @import("Result.zig");
const Runtime = @import("Runtime.zig"); const Runtime = @import("Runtime.zig");
const WordIterator = @import("WordIterator.zig"); const WordIterator = @import("WordIterator.zig");
const SpvValue = Result.SpvValue; const Value = Result.Value;
const Self = @This(); const Self = @This();
@@ -74,10 +74,10 @@ geometry_output_count: SpvWord,
geometry_input: SpvWord, geometry_input: SpvWord,
geometry_output: SpvWord, geometry_output: SpvWord,
input_locations: std.AutoHashMap(SpvWord, *SpvValue), input_locations: std.AutoHashMap(SpvWord, *Value),
output_locations: std.AutoHashMap(SpvWord, *SpvValue), output_locations: std.AutoHashMap(SpvWord, *Value),
bindings: std.AutoHashMap(SpvBinding, *SpvValue), bindings: std.AutoHashMap(SpvBinding, *Value),
push_constants: []SpvValue, push_constants: []Value,
pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!Self { pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!Self {
var self: Self = std.mem.zeroInit(Self, .{ var self: Self = std.mem.zeroInit(Self, .{
@@ -90,9 +90,9 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!S
.local_size_x = 1, .local_size_x = 1,
.local_size_y = 1, .local_size_y = 1,
.local_size_z = 1, .local_size_z = 1,
.input_locations = std.AutoHashMap(SpvWord, *SpvValue).init(allocator), .input_locations = std.AutoHashMap(SpvWord, *Value).init(allocator),
.output_locations = std.AutoHashMap(SpvWord, *SpvValue).init(allocator), .output_locations = std.AutoHashMap(SpvWord, *Value).init(allocator),
.bindings = std.AutoHashMap(SpvBinding, *SpvValue).init(allocator), .bindings = std.AutoHashMap(SpvBinding, *Value).init(allocator),
}); });
errdefer self.deinit(allocator); errdefer self.deinit(allocator);

View File

@@ -1,6 +1,8 @@
const std = @import("std"); const std = @import("std");
const spv = @import("spv.zig"); const spv = @import("spv.zig");
const RuntimeError = @import("Runtime.zig").RuntimeError;
const SpvVoid = spv.SpvVoid; const SpvVoid = spv.SpvVoid;
const SpvByte = spv.SpvByte; const SpvByte = spv.SpvByte;
const SpvWord = spv.SpvWord; const SpvWord = spv.SpvWord;
@@ -52,7 +54,7 @@ const Decoration = struct {
index: SpvWord, index: SpvWord,
}; };
pub const SpvValue = union(Type) { pub const Value = union(Type) {
Void: noreturn, Void: noreturn,
Bool: bool, Bool: bool,
Int: union { Int: union {
@@ -70,8 +72,8 @@ pub const SpvValue = union(Type) {
float32: f32, float32: f32,
float64: f64, float64: f64,
}, },
Vector: noreturn, Vector: []Value,
Matrix: noreturn, Matrix: []Value,
Array: struct {}, Array: struct {},
RuntimeArray: struct {}, RuntimeArray: struct {},
Structure: struct {}, Structure: struct {},
@@ -134,10 +136,15 @@ variant: ?union(Variant) {
}, },
Variable: struct { Variable: struct {
storage_class: spv.SpvStorageClass, storage_class: spv.SpvStorageClass,
value: SpvValue, value: Value,
},
Constant: Value,
Function: struct {
return_type: SpvWord,
function_type: SpvWord,
/// Allocated array
params: []const SpvWord,
}, },
Constant: SpvValue,
Function: struct {},
AccessChain: struct {}, AccessChain: struct {},
FunctionParameter: struct {}, FunctionParameter: struct {},
Label: struct {}, Label: struct {},
@@ -188,3 +195,18 @@ pub fn resolveType(self: *const Self, results: []const Self) *const Self {
else else
self; self;
} }
pub fn initConstantValue(self: *Self, results: []const Self, target: SpvWord) RuntimeError!void {
_ = self;
const resolved = results[target].resolveType(results);
if (resolved.variant) |variant| {
switch (variant) {
.Type => |t| switch (t) {
.Structure => |s|
else => return RuntimeError.InvalidSpirV,
},
else => return RuntimeError.InvalidSpirV,
}
}
return RuntimeError.InvalidSpirV;
}

View File

@@ -15,9 +15,6 @@ const SpvBool = spv.SpvBool;
// DUMB INDEV OPCODES TODO : // DUMB INDEV OPCODES TODO :
// OpVariable X // OpVariable X
// OpFunction X
// OpLabel X
// OpCompositeConstruct X
// OpAccessChain X // OpAccessChain X
// OpStore X // OpStore X
// OpLoad X // OpLoad X
@@ -51,6 +48,9 @@ pub const SetupDispatcher = block: {
.TypeVector = opTypeVector, .TypeVector = opTypeVector,
.TypeVoid = opTypeVoid, .TypeVoid = opTypeVoid,
.Variable = opVariable, .Variable = opVariable,
.Function = opFunction,
.Label = opLabel,
.CompositeConstruct = opCompositeConstruct,
}); });
}; };
@@ -334,7 +334,7 @@ fn opConstant(_: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) RuntimeEr
.Int => { .Int => {
break :value if (word_count - 2 != 1) .{ break :value if (word_count - 2 != 1) .{
.Int = .{ .Int = .{
.uint64 = try rt.it.next() | (@as(u64, try rt.it.next()) >> 32), .uint64 = @as(u64, try rt.it.next()) | (@as(u64, try rt.it.next()) >> 32),
}, },
} else .{ } else .{
.Int = .{ .Int = .{
@@ -382,6 +382,46 @@ fn opVariable(_: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) RuntimeEr
}; };
} }
fn opFunction(allocator: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
const return_type = try rt.it.next();
const id = try rt.it.next();
_ = rt.it.skip(); // Skip function control
const function_type_id = try rt.it.next();
rt.mod.results.items[id].variant = .{
.Function = .{
.return_type = return_type,
.function_type = function_type_id,
.params = params: {
if (rt.mod.results.items[function_type_id].variant) |variant| {
const params_count = switch (variant) {
.Type => |t| switch (t) {
.Function => |f| f.params.len,
else => return RuntimeError.InvalidSpirV,
},
else => return RuntimeError.InvalidSpirV,
};
break :params allocator.alloc(SpvWord, params_count) catch return RuntimeError.OutOfMemory;
}
return RuntimeError.InvalidSpirV;
},
},
};
rt.current_function = &rt.mod.results.items[id];
}
fn opLabel(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
const id = try rt.it.next();
rt.mod.results.items[id].variant = .{
.Label = .{},
};
}
fn opCompositeConstruct(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
_ = rt;
}
fn readString(allocator: std.mem.Allocator, it: *WordIterator) RuntimeError![]const u8 { fn readString(allocator: std.mem.Allocator, it: *WordIterator) RuntimeError![]const u8 {
var str: std.ArrayList(u8) = .empty; var str: std.ArrayList(u8) = .empty;
while (it.nextOrNull()) |word| { while (it.nextOrNull()) |word| {