working POC
This commit is contained in:
@@ -72,9 +72,9 @@ geometry_output_count: SpvWord,
|
||||
geometry_input: SpvWord,
|
||||
geometry_output: SpvWord,
|
||||
|
||||
input_locations: std.AutoHashMap(SpvWord, []Value),
|
||||
output_locations: std.AutoHashMap(SpvWord, []Value),
|
||||
bindings: std.AutoHashMap(SpvBinding, []Value),
|
||||
input_locations: std.ArrayList(SpvWord),
|
||||
output_locations: std.ArrayList(SpvWord),
|
||||
bindings: std.AutoHashMap(SpvBinding, Value),
|
||||
push_constants: []Value,
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!Self {
|
||||
@@ -87,9 +87,9 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!S
|
||||
.local_size_x = 1,
|
||||
.local_size_y = 1,
|
||||
.local_size_z = 1,
|
||||
.input_locations = std.AutoHashMap(SpvWord, []Value).init(allocator),
|
||||
.output_locations = std.AutoHashMap(SpvWord, []Value).init(allocator),
|
||||
.bindings = std.AutoHashMap(SpvBinding, []Value).init(allocator),
|
||||
.input_locations = std.ArrayList(SpvWord).empty,
|
||||
.output_locations = std.ArrayList(SpvWord).empty,
|
||||
.bindings = std.AutoHashMap(SpvBinding, Value).init(allocator),
|
||||
});
|
||||
errdefer self.deinit(allocator);
|
||||
|
||||
@@ -120,7 +120,7 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!S
|
||||
_ = self.it.skip(); // Skip schema
|
||||
|
||||
try self.pass(allocator); // Setup pass
|
||||
try self.populateMaps();
|
||||
try self.populateMaps(allocator);
|
||||
|
||||
if (std.process.hasEnvVarConstant("SPIRV_INTERPRETER_DEBUG_LOGS")) {
|
||||
var capability_set_names: std.ArrayList([]const u8) = .empty;
|
||||
@@ -196,13 +196,12 @@ fn pass(self: *Self, allocator: std.mem.Allocator) ModuleError!void {
|
||||
}
|
||||
}
|
||||
|
||||
fn populateMaps(self: *Self) ModuleError!void {
|
||||
fn populateMaps(self: *Self, allocator: std.mem.Allocator) ModuleError!void {
|
||||
for (self.results, 0..) |result, id| {
|
||||
if (result.variant == null or std.meta.activeTag(result.variant.?) != .Variable) continue;
|
||||
const variable = result.variant.?.Variable;
|
||||
switch (variable.storage_class) {
|
||||
switch (result.variant.?.Variable.storage_class) {
|
||||
.Output => for (result.decorations.items) |decoration| switch (decoration.rtype) {
|
||||
.Location => self.output_locations.put(@intCast(id), variable.values) catch return ModuleError.OutOfMemory,
|
||||
.Location => self.output_locations.append(allocator, @intCast(id)) catch return ModuleError.OutOfMemory,
|
||||
else => {},
|
||||
},
|
||||
else => {},
|
||||
@@ -212,8 +211,8 @@ fn populateMaps(self: *Self) ModuleError!void {
|
||||
|
||||
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
|
||||
allocator.free(self.code);
|
||||
self.input_locations.deinit();
|
||||
self.output_locations.deinit();
|
||||
self.input_locations.deinit(allocator);
|
||||
self.output_locations.deinit(allocator);
|
||||
self.bindings.deinit();
|
||||
for (self.entry_points.items) |entry| {
|
||||
allocator.free(entry.name);
|
||||
|
||||
215
src/Result.zig
215
src/Result.zig
@@ -55,7 +55,7 @@ const Decoration = struct {
|
||||
};
|
||||
|
||||
pub const Value = union(Type) {
|
||||
Void: struct {},
|
||||
Void: noreturn,
|
||||
Bool: bool,
|
||||
Int: extern union {
|
||||
sint8: i8,
|
||||
@@ -77,69 +77,90 @@ pub const Value = union(Type) {
|
||||
Array: struct {},
|
||||
RuntimeArray: struct {},
|
||||
Structure: []Value,
|
||||
Function: struct {},
|
||||
Function: noreturn,
|
||||
Image: struct {},
|
||||
Sampler: struct {},
|
||||
SampledImage: struct {},
|
||||
Pointer: struct {},
|
||||
Pointer: noreturn,
|
||||
|
||||
fn initMembers(self: *Value, allocator: std.mem.Allocator, results: []const Self, target: SpvWord) RuntimeError!void {
|
||||
pub inline fn getCompositeDataOrNull(self: *const Value) ?[]Value {
|
||||
return switch (self.*) {
|
||||
.Vector => |v| v,
|
||||
.Matrix => |m| m,
|
||||
.Array => |_| unreachable,
|
||||
.Structure => |s| s,
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
fn init(allocator: std.mem.Allocator, results: []const Self, target: SpvWord) RuntimeError!Value {
|
||||
const resolved = results[target].resolveType(results);
|
||||
const member_count = resolved.getMemberCounts();
|
||||
|
||||
switch (resolved.variant.?) {
|
||||
return switch (resolved.variant.?) {
|
||||
.Type => |t| switch (t) {
|
||||
.Bool, .Int, .Float => std.debug.assert(member_count == 1),
|
||||
.Structure => |s| {
|
||||
self.* = .{
|
||||
.Structure = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory,
|
||||
};
|
||||
for (self.Structure, s.members) |*value, member| {
|
||||
value.* = switch (member) {
|
||||
inline else => |tag| @unionInit(Value, @tagName(tag), undefined),
|
||||
};
|
||||
}
|
||||
},
|
||||
.Matrix => |m| {
|
||||
self.* = .{
|
||||
.Matrix = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory,
|
||||
};
|
||||
for (self.Matrix) |*value| {
|
||||
value.* = switch (m.column_type) {
|
||||
inline else => |tag| @unionInit(Value, @tagName(tag), undefined),
|
||||
};
|
||||
}
|
||||
},
|
||||
.Array => |a| {
|
||||
_ = a;
|
||||
},
|
||||
.Vector => |v| {
|
||||
self.* = .{
|
||||
.Vector = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory,
|
||||
};
|
||||
.Bool => .{ .Bool = false },
|
||||
.Int => .{ .Int = .{ .uint64 = 0 } },
|
||||
.Float => .{ .Float = .{ .float64 = 0.0 } },
|
||||
.Vector => |v| blk: {
|
||||
var self: Value = .{ .Vector = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer self.deinit(allocator);
|
||||
|
||||
for (self.Vector) |*value| {
|
||||
value.* = switch (v.components_type) {
|
||||
inline else => |tag| @unionInit(Value, @tagName(tag), undefined),
|
||||
};
|
||||
value.* = try Value.init(allocator, results, v.components_type_word);
|
||||
}
|
||||
break :blk self;
|
||||
},
|
||||
else => {},
|
||||
.Matrix => |m| blk: {
|
||||
var self: Value = .{ .Matrix = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer self.deinit(allocator);
|
||||
|
||||
for (self.Matrix) |*value| {
|
||||
value.* = try Value.init(allocator, results, m.column_type_word);
|
||||
}
|
||||
break :blk self;
|
||||
},
|
||||
.Array => |_| {
|
||||
unreachable;
|
||||
},
|
||||
.Structure => |s| blk: {
|
||||
var self: Value = .{ .Structure = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer self.deinit(allocator);
|
||||
|
||||
for (self.Structure, s.members_type_word) |*value, member_type_word| {
|
||||
value.* = try Value.init(allocator, results, member_type_word);
|
||||
}
|
||||
break :blk self;
|
||||
},
|
||||
else => unreachable,
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
/// Performs a deep copy
|
||||
fn dupe(self: *const Value, allocator: std.mem.Allocator) RuntimeError!Value {
|
||||
pub fn dupe(self: *const Value, allocator: std.mem.Allocator) RuntimeError!Value {
|
||||
return switch (self.*) {
|
||||
.Vector => |v| .{
|
||||
.Vector = allocator.dupe(Value, v) catch return RuntimeError.OutOfMemory,
|
||||
.Vector = blk: {
|
||||
const values = allocator.dupe(Value, v) catch return RuntimeError.OutOfMemory;
|
||||
for (values, v) |*new_value, value| new_value.* = try value.dupe(allocator);
|
||||
break :blk values;
|
||||
},
|
||||
},
|
||||
.Matrix => |m| .{
|
||||
.Matrix = allocator.dupe(Value, m) catch return RuntimeError.OutOfMemory,
|
||||
.Matrix = blk: {
|
||||
const values = allocator.dupe(Value, m) catch return RuntimeError.OutOfMemory;
|
||||
for (values, m) |*new_value, value| new_value.* = try value.dupe(allocator);
|
||||
break :blk values;
|
||||
},
|
||||
},
|
||||
.Structure => |s| .{
|
||||
.Structure = allocator.dupe(Value, s) catch return RuntimeError.OutOfMemory,
|
||||
.Structure = blk: {
|
||||
const values = allocator.dupe(Value, s) catch return RuntimeError.OutOfMemory;
|
||||
for (values, s) |*new_value, value| new_value.* = try value.dupe(allocator);
|
||||
break :blk values;
|
||||
},
|
||||
},
|
||||
else => self.*,
|
||||
};
|
||||
@@ -147,9 +168,18 @@ pub const Value = union(Type) {
|
||||
|
||||
fn deinit(self: *Value, allocator: std.mem.Allocator) void {
|
||||
switch (self.*) {
|
||||
.Structure => |values| allocator.free(values),
|
||||
.Matrix => |values| allocator.free(values),
|
||||
.Vector => |values| allocator.free(values),
|
||||
.Vector => |values| {
|
||||
for (values) |*value| value.deinit(allocator);
|
||||
allocator.free(values);
|
||||
},
|
||||
.Matrix => |values| {
|
||||
for (values) |*value| value.deinit(allocator);
|
||||
allocator.free(values);
|
||||
},
|
||||
.Structure => |values| {
|
||||
for (values) |*value| value.deinit(allocator);
|
||||
allocator.free(values);
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
@@ -208,9 +238,9 @@ variant: ?union(Variant) {
|
||||
},
|
||||
Variable: struct {
|
||||
storage_class: spv.SpvStorageClass,
|
||||
values: []Value,
|
||||
value: Value,
|
||||
},
|
||||
Constant: []Value,
|
||||
Constant: Value,
|
||||
Function: struct {
|
||||
source_location: usize,
|
||||
return_type: SpvWord,
|
||||
@@ -219,7 +249,7 @@ variant: ?union(Variant) {
|
||||
},
|
||||
AccessChain: struct {
|
||||
target: SpvWord,
|
||||
values: []Value,
|
||||
value: Value,
|
||||
},
|
||||
FunctionParameter: struct {},
|
||||
Label: struct {
|
||||
@@ -254,14 +284,9 @@ pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
|
||||
},
|
||||
else => {},
|
||||
},
|
||||
.Constant => |values| {
|
||||
for (values) |*value| value.deinit(allocator);
|
||||
allocator.free(values);
|
||||
},
|
||||
.Variable => |v| {
|
||||
for (v.values) |*value| value.deinit(allocator);
|
||||
allocator.free(v.values);
|
||||
},
|
||||
.Constant => |*v| v.deinit(allocator),
|
||||
.Variable => |*v| v.value.deinit(allocator),
|
||||
//.AccessChain => |*a| a.value.deinit(allocator),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
@@ -310,24 +335,10 @@ pub fn dupe(self: *const Self, allocator: std.mem.Allocator) RuntimeError!Self {
|
||||
.Variable => |v| break :blk .{
|
||||
.Variable = .{
|
||||
.storage_class = v.storage_class,
|
||||
.values = blk2: {
|
||||
const values = allocator.dupe(Value, v.values) catch return RuntimeError.OutOfMemory;
|
||||
for (values, v.values) |*new_value, value| {
|
||||
new_value.* = try value.dupe(allocator);
|
||||
}
|
||||
break :blk2 values;
|
||||
},
|
||||
},
|
||||
},
|
||||
.Constant => |c| break :blk .{
|
||||
.Constant = blk2: {
|
||||
const values = allocator.dupe(Value, c) catch return RuntimeError.OutOfMemory;
|
||||
for (values, c) |*new_value, value| {
|
||||
new_value.* = try value.dupe(allocator);
|
||||
}
|
||||
break :blk2 values;
|
||||
.value = try v.value.dupe(allocator),
|
||||
},
|
||||
},
|
||||
.Constant => |c| break :blk .{ .Constant = try c.dupe(allocator) },
|
||||
.Function => |f| break :blk .{
|
||||
.Function = .{
|
||||
.source_location = f.source_location,
|
||||
@@ -336,9 +347,8 @@ pub fn dupe(self: *const Self, allocator: std.mem.Allocator) RuntimeError!Self {
|
||||
.params = allocator.dupe(SpvWord, f.params) catch return RuntimeError.OutOfMemory,
|
||||
},
|
||||
},
|
||||
else => {},
|
||||
else => break :blk variant,
|
||||
}
|
||||
break :blk variant;
|
||||
}
|
||||
break :blk null;
|
||||
},
|
||||
@@ -376,37 +386,42 @@ pub fn getMemberCounts(self: *const Self) usize {
|
||||
return 0;
|
||||
}
|
||||
|
||||
pub fn initValues(allocator: std.mem.Allocator, values: []Value, results: []const Self, resolved: *const Self) RuntimeError!void {
|
||||
switch (resolved.variant.?) {
|
||||
pub fn initValue(allocator: std.mem.Allocator, member_count: usize, results: []const Self, resolved: *const Self) RuntimeError!Value {
|
||||
return switch (resolved.variant.?) {
|
||||
.Type => |t| switch (t) {
|
||||
.Bool => values[0] = .{ .Bool = undefined },
|
||||
.Int => values[0] = .{ .Int = undefined },
|
||||
.Float => values[0] = .{ .Float = undefined },
|
||||
.Vector => |v| {
|
||||
for (values) |*value| {
|
||||
value.* = switch (v.components_type) {
|
||||
inline else => |tag| @unionInit(Value, @tagName(tag), undefined),
|
||||
};
|
||||
.Bool => .{ .Bool = false },
|
||||
.Int => .{ .Int = .{ .uint64 = 0 } },
|
||||
.Float => .{ .Float = .{ .float64 = 0.0 } },
|
||||
.Vector => |v| blk: {
|
||||
const value: Value = .{ .Vector = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer allocator.free(value.Vector);
|
||||
for (value.Vector) |*val| {
|
||||
val.* = try Value.init(allocator, results, v.components_type_word);
|
||||
}
|
||||
break :blk value;
|
||||
},
|
||||
.Matrix => |m| {
|
||||
for (values) |*value| {
|
||||
try value.initMembers(allocator, results, m.column_type_word);
|
||||
.Matrix => |m| blk: {
|
||||
const value: Value = .{ .Matrix = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer allocator.free(value.Matrix);
|
||||
for (value.Matrix) |*v| {
|
||||
v.* = try Value.init(allocator, results, m.column_type_word);
|
||||
}
|
||||
break :blk value;
|
||||
},
|
||||
.Array => |a| { // TODO
|
||||
_ = a;
|
||||
},
|
||||
.Structure => |s| {
|
||||
for (values, s.members_type_word) |*value, member_type_word| {
|
||||
try value.initMembers(allocator, results, member_type_word);
|
||||
.Array => |_| RuntimeError.ToDo,
|
||||
.Structure => |s| blk: {
|
||||
const value: Value = .{ .Structure = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer allocator.free(value.Structure);
|
||||
for (value.Structure, s.members_type_word) |*v, member_type_word| {
|
||||
v.* = try Value.init(allocator, results, member_type_word);
|
||||
}
|
||||
break :blk value;
|
||||
},
|
||||
.Image => {}, // TODO
|
||||
.Sampler => {}, // No op
|
||||
.SampledImage => {}, // TODO
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
.Image => RuntimeError.ToDo,
|
||||
.Sampler => RuntimeError.ToDo,
|
||||
.SampledImage => RuntimeError.ToDo,
|
||||
else => RuntimeError.InvalidSpirV,
|
||||
},
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
}
|
||||
else => RuntimeError.InvalidSpirV,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -123,11 +123,18 @@ pub fn callEntryPoint(self: *Self, allocator: std.mem.Allocator, entry_point_ind
|
||||
_ = it_tmp.skipN(word_count);
|
||||
self.it = it_tmp;
|
||||
}
|
||||
|
||||
//@import("pretty").print(allocator, self.results, .{
|
||||
// .tab_size = 4,
|
||||
// .max_depth = 0,
|
||||
// .struct_max_len = 0,
|
||||
// .array_max_len = 0,
|
||||
//}) catch return RuntimeError.OutOfMemory;
|
||||
}
|
||||
|
||||
pub fn readOutput(self: *const Self, comptime T: type, output: []T, result: SpvWord) error{NotFound}!void {
|
||||
if (self.mod.output_locations.get(result)) |out| {
|
||||
self.readValue(T, output, &out[0]);
|
||||
if (std.mem.indexOf(SpvWord, self.mod.output_locations.items, &.{result})) |_| {
|
||||
self.readValue(T, output, &self.results[result].variant.?.Variable.value);
|
||||
} else {
|
||||
return error.NotFound;
|
||||
}
|
||||
@@ -143,6 +150,8 @@ fn readValue(self: *const Self, comptime T: type, output: []T, value: *const Res
|
||||
.Bool => |b| {
|
||||
if (T == bool) {
|
||||
output[0] = b;
|
||||
} else {
|
||||
unreachable;
|
||||
}
|
||||
},
|
||||
.Int => |i| {
|
||||
|
||||
151
src/opcodes.zig
151
src/opcodes.zig
@@ -19,8 +19,7 @@ pub const SetupDispatcher = block: {
|
||||
@setEvalBranchQuota(65535);
|
||||
break :block std.EnumMap(spv.SpvOp, OpCodeFunc).init(.{
|
||||
.Capability = opCapability,
|
||||
.CompositeConstruct = opCompositeConstruct,
|
||||
.CompositeExtract = opCompositeExtract,
|
||||
.CompositeConstruct = opCompositeConstructSetup,
|
||||
.Constant = opConstant,
|
||||
.Decorate = opDecorate,
|
||||
.EntryPoint = opEntryPoint,
|
||||
@@ -52,6 +51,8 @@ pub const RuntimeDispatcher = block: {
|
||||
@setEvalBranchQuota(65535);
|
||||
break :block std.EnumMap(spv.SpvOp, OpCodeFunc).init(.{
|
||||
.AccessChain = opAccessChain,
|
||||
.CompositeConstruct = opCompositeConstruct,
|
||||
.CompositeExtract = opCompositeExtract,
|
||||
.Load = opLoad,
|
||||
.Return = opReturn,
|
||||
.Store = opStore,
|
||||
@@ -195,9 +196,7 @@ fn opMemoryModel(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!vo
|
||||
|
||||
fn opName(allocator: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
const id = try rt.it.next();
|
||||
if (id >= rt.mod.results.len) return RuntimeError.InvalidSpirV;
|
||||
var result = &rt.mod.results[id];
|
||||
result.* = Result.init();
|
||||
result.name = try readStringN(allocator, &rt.it, word_count - 1);
|
||||
}
|
||||
|
||||
@@ -375,7 +374,7 @@ fn opTypeFunction(allocator: std.mem.Allocator, word_count: SpvWord, rt: *Runtim
|
||||
fn opConstant(allocator: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
const target = try setupConstant(allocator, rt);
|
||||
// No check on null and sizes, absolute trust in this shit
|
||||
switch (target.variant.?.Constant[0]) {
|
||||
switch (target.variant.?.Constant) {
|
||||
.Int => |*i| {
|
||||
if (word_count - 2 != 1) {
|
||||
i.uint64 = @as(u64, try rt.it.next()) | (@as(u64, try rt.it.next()) >> 32);
|
||||
@@ -410,11 +409,9 @@ fn opVariable(allocator: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) R
|
||||
target.variant = .{
|
||||
.Variable = .{
|
||||
.storage_class = storage_class,
|
||||
.values = allocator.alloc(Result.Value, member_count) catch return RuntimeError.OutOfMemory,
|
||||
.value = try Result.initValue(allocator, member_count, rt.mod.results, resolved),
|
||||
},
|
||||
};
|
||||
errdefer allocator.free(target.variant.?.Variable.values);
|
||||
try Result.initValues(allocator, target.variant.?.Variable.values, rt.mod.results, resolved);
|
||||
|
||||
_ = initializer;
|
||||
}
|
||||
@@ -462,12 +459,36 @@ fn opLabel(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
};
|
||||
}
|
||||
|
||||
fn opCompositeConstruct(allocator: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
fn opCompositeConstructSetup(allocator: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
_ = try setupConstant(allocator, rt);
|
||||
}
|
||||
|
||||
fn opCompositeExtract(allocator: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
_ = try setupConstant(allocator, rt);
|
||||
fn opCompositeConstruct(_: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
_ = rt.it.skip();
|
||||
const id = try rt.it.next();
|
||||
|
||||
const index_count = word_count - 2;
|
||||
const target = (rt.results[id].variant orelse return RuntimeError.InvalidSpirV).Constant.getCompositeDataOrNull() orelse return RuntimeError.InvalidSpirV;
|
||||
for (target[0..index_count]) |*elem| {
|
||||
const value = (rt.results[try rt.it.next()].variant orelse return RuntimeError.InvalidSpirV).Constant;
|
||||
elem.* = value;
|
||||
}
|
||||
}
|
||||
|
||||
fn opCompositeExtract(allocator: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
_ = rt.it.skip();
|
||||
const id = try rt.it.next();
|
||||
const composite_id = try rt.it.next();
|
||||
|
||||
const index_count = word_count - 3;
|
||||
var composite = (rt.results[composite_id].variant orelse return RuntimeError.InvalidSpirV).Constant;
|
||||
for (0..index_count) |_| {
|
||||
const member_id = try rt.it.next();
|
||||
composite = (composite.getCompositeDataOrNull() orelse return RuntimeError.InvalidSpirV)[member_id];
|
||||
}
|
||||
rt.results[id].variant = .{
|
||||
.Constant = try composite.dupe(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
fn opFunctionEnd(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
@@ -479,71 +500,71 @@ fn opAccessChain(_: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) Runtim
|
||||
const id = try rt.it.next();
|
||||
const base_id = try rt.it.next();
|
||||
|
||||
const target = &rt.results[id];
|
||||
|
||||
target.variant = .{
|
||||
.AccessChain = .{
|
||||
.target = var_type,
|
||||
.values = undefined,
|
||||
},
|
||||
};
|
||||
|
||||
const base = &rt.results[base_id];
|
||||
const values = blk: {
|
||||
var value_ptr = blk: {
|
||||
if (base.variant) |variant| {
|
||||
switch (variant) {
|
||||
.Variable => |v| break :blk v.values,
|
||||
.Variable => |v| break :blk &v.value,
|
||||
.Constant => |v| break :blk &v,
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
return RuntimeError.InvalidSpirV;
|
||||
};
|
||||
var value_ptr = &values[0];
|
||||
|
||||
const index_count = word_count - 4;
|
||||
const index_count = word_count - 3;
|
||||
for (0..index_count) |_| {
|
||||
const member = &rt.results[try rt.it.next()];
|
||||
const member_value = switch (member.variant orelse return RuntimeError.InvalidSpirV) {
|
||||
.Constant => |c| &c[0],
|
||||
.Constant => |c| &c,
|
||||
.Variable => |v| &v.value,
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
};
|
||||
switch (member_value.*) {
|
||||
.Int => |i| {
|
||||
if (i.uint32 > values.len) return RuntimeError.InvalidSpirV;
|
||||
value_ptr = switch (value_ptr.*) {
|
||||
.Vector => |v| &v[i.uint32],
|
||||
.Matrix => |m| &m[i.uint32],
|
||||
switch (value_ptr.*) {
|
||||
.Vector => |v| {
|
||||
if (i.uint32 > v.len) return RuntimeError.InvalidSpirV;
|
||||
value_ptr = &v[i.uint32];
|
||||
},
|
||||
.Matrix => |m| {
|
||||
if (i.uint32 > m.len) return RuntimeError.InvalidSpirV;
|
||||
value_ptr = &m[i.uint32];
|
||||
},
|
||||
.Array => |_| return RuntimeError.ToDo,
|
||||
.Structure => |s| &s[i.uint32],
|
||||
.Structure => |s| {
|
||||
if (i.uint32 > s.len) return RuntimeError.InvalidSpirV;
|
||||
value_ptr = &s[i.uint32];
|
||||
},
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
};
|
||||
}
|
||||
},
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
}
|
||||
}
|
||||
target.variant.?.AccessChain.values = switch (value_ptr.*) {
|
||||
.Vector => |v| v,
|
||||
.Matrix => |m| m,
|
||||
.Array => |_| return RuntimeError.ToDo,
|
||||
.Structure => |s| s,
|
||||
else => @as([*]Result.Value, @ptrCast(value_ptr))[0..1],
|
||||
|
||||
rt.results[id].variant = .{
|
||||
.AccessChain = .{
|
||||
.target = var_type,
|
||||
.value = value_ptr.*,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fn opStore(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
const ptr_id = try rt.it.next();
|
||||
const val_id = try rt.it.next();
|
||||
copyValues(
|
||||
copyValue(
|
||||
switch (rt.results[ptr_id].variant orelse return RuntimeError.InvalidSpirV) {
|
||||
.Variable => |v| v.values,
|
||||
.Constant => |c| c,
|
||||
.AccessChain => |a| a.values,
|
||||
.Variable => |*v| &v.value,
|
||||
.Constant => |*c| c,
|
||||
.AccessChain => |*a| &a.value,
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
},
|
||||
switch (rt.results[val_id].variant orelse return RuntimeError.InvalidSpirV) {
|
||||
.Variable => |v| v.values,
|
||||
.Constant => |c| c,
|
||||
.AccessChain => |a| a.values,
|
||||
.Variable => |v| &v.value,
|
||||
.Constant => |c| &c,
|
||||
.AccessChain => |a| &a.value,
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
},
|
||||
);
|
||||
@@ -557,17 +578,17 @@ fn opLoad(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
_ = rt.it.skip();
|
||||
const id = try rt.it.next();
|
||||
const ptr_id = try rt.it.next();
|
||||
copyValues(
|
||||
copyValue(
|
||||
switch (rt.results[id].variant orelse return RuntimeError.InvalidSpirV) {
|
||||
.Variable => |v| v.values,
|
||||
.Constant => |c| c,
|
||||
.AccessChain => |a| a.values,
|
||||
.Variable => |*v| &v.value,
|
||||
.Constant => |*c| c,
|
||||
.AccessChain => |*a| &a.value,
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
},
|
||||
switch (rt.results[ptr_id].variant orelse return RuntimeError.InvalidSpirV) {
|
||||
.Variable => |v| v.values,
|
||||
.Constant => |c| c,
|
||||
.AccessChain => |a| a.values,
|
||||
.Variable => |v| &v.value,
|
||||
.Constant => |c| &c,
|
||||
.AccessChain => |a| &a.value,
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
},
|
||||
);
|
||||
@@ -584,7 +605,7 @@ fn opReturn(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
}
|
||||
}
|
||||
|
||||
inline fn setupConstant(allocator: std.mem.Allocator, rt: *Runtime) RuntimeError!*Result {
|
||||
fn setupConstant(allocator: std.mem.Allocator, rt: *Runtime) RuntimeError!*Result {
|
||||
const res_type = try rt.it.next();
|
||||
const id = try rt.it.next();
|
||||
const target = &rt.mod.results[id];
|
||||
@@ -594,9 +615,7 @@ inline fn setupConstant(allocator: std.mem.Allocator, rt: *Runtime) RuntimeError
|
||||
if (member_count == 0) {
|
||||
return RuntimeError.InvalidSpirV;
|
||||
}
|
||||
target.variant = .{ .Constant = allocator.alloc(Result.Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer allocator.free(target.variant.?.Constant);
|
||||
try Result.initValues(allocator, target.variant.?.Constant, rt.mod.results, resolved);
|
||||
target.variant = .{ .Constant = try Result.initValue(allocator, member_count, rt.mod.results, resolved) };
|
||||
return target;
|
||||
}
|
||||
|
||||
@@ -629,17 +648,15 @@ fn readStringN(allocator: std.mem.Allocator, it: *WordIterator, n: usize) Runtim
|
||||
}
|
||||
|
||||
fn copyValue(dst: *Result.Value, src: *const Result.Value) void {
|
||||
switch (src.*) {
|
||||
.Vector => |v| copyValues(dst.Vector, v),
|
||||
.Matrix => |m| copyValues(dst.Matrix, m),
|
||||
.Array => |_| unreachable,
|
||||
.Structure => |s| copyValues(dst.Structure, s),
|
||||
else => dst.* = src.*,
|
||||
}
|
||||
}
|
||||
|
||||
inline fn copyValues(dst: []Result.Value, src: []const Result.Value) void {
|
||||
for (dst, src) |*d, *s| {
|
||||
copyValue(d, s);
|
||||
if (src.getCompositeDataOrNull()) |src_slice| {
|
||||
if (dst.getCompositeDataOrNull()) |dst_slice| {
|
||||
for (0..@min(dst_slice.len, src_slice.len)) |i| {
|
||||
copyValue(&dst_slice[i], &src_slice[i]);
|
||||
}
|
||||
} else {
|
||||
unreachable;
|
||||
}
|
||||
} else {
|
||||
dst.* = src.*;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user