adding some operators, working on example
This commit is contained in:
@@ -159,15 +159,15 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!S
|
||||
capabilities,
|
||||
entry_points,
|
||||
});
|
||||
|
||||
//@import("pretty").print(allocator, self.results, .{
|
||||
// .tab_size = 4,
|
||||
// .max_depth = 0,
|
||||
// .struct_max_len = 0,
|
||||
// .array_max_len = 0,
|
||||
//}) catch return ModuleError.OutOfMemory;
|
||||
}
|
||||
|
||||
//@import("pretty").print(allocator, self.results, .{
|
||||
// .tab_size = 4,
|
||||
// .max_depth = 0,
|
||||
// .struct_max_len = 0,
|
||||
// .array_max_len = 0,
|
||||
//}) catch return ModuleError.OutOfMemory;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
@@ -205,6 +205,10 @@ 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;
|
||||
switch (result.variant.?.Variable.storage_class) {
|
||||
.Input => for (result.decorations.items) |decoration| switch (decoration.rtype) {
|
||||
.Location => self.input_locations.append(allocator, @intCast(id)) catch return ModuleError.OutOfMemory,
|
||||
else => {},
|
||||
},
|
||||
.Output => for (result.decorations.items) |decoration| switch (decoration.rtype) {
|
||||
.Location => self.output_locations.append(allocator, @intCast(id)) catch return ModuleError.OutOfMemory,
|
||||
else => {},
|
||||
|
||||
@@ -22,6 +22,12 @@ pub const RuntimeError = error{
|
||||
InvalidEntryPoint,
|
||||
ToDo,
|
||||
DivisionByZero,
|
||||
InvalidValueType,
|
||||
};
|
||||
|
||||
pub const ReadOutputError = error{
|
||||
NotFound,
|
||||
InvalidValueType,
|
||||
};
|
||||
|
||||
pub const Function = struct {
|
||||
@@ -147,11 +153,11 @@ pub fn callEntryPoint(self: *Self, allocator: std.mem.Allocator, entry_point_ind
|
||||
//}) catch return RuntimeError.OutOfMemory;
|
||||
}
|
||||
|
||||
pub fn readOutput(self: *const Self, comptime T: type, output: []T, result: SpvWord) error{NotFound}!void {
|
||||
pub fn readOutput(self: *const Self, comptime T: type, output: []T, result: SpvWord) ReadOutputError!void {
|
||||
if (std.mem.indexOf(SpvWord, self.mod.output_locations.items, &.{result})) |_| {
|
||||
self.readValue(T, output, &self.results[result].variant.?.Variable.value);
|
||||
try self.readValue(T, output, &self.results[result].variant.?.Variable.value);
|
||||
} else {
|
||||
return error.NotFound;
|
||||
return ReadOutputError.NotFound;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,13 +166,13 @@ fn reset(self: *Self) void {
|
||||
self.current_function = null;
|
||||
}
|
||||
|
||||
fn readValue(self: *const Self, comptime T: type, output: []T, value: *const Result.Value) void {
|
||||
fn readValue(self: *const Self, comptime T: type, output: []T, value: *const Result.Value) ReadOutputError!void {
|
||||
switch (value.*) {
|
||||
.Bool => |b| {
|
||||
if (T == bool) {
|
||||
output[0] = b;
|
||||
} else {
|
||||
unreachable; // Wanted value may not be composed of booleans
|
||||
return ReadOutputError.InvalidValueType;
|
||||
}
|
||||
},
|
||||
.Int => |i| {
|
||||
@@ -179,7 +185,7 @@ fn readValue(self: *const Self, comptime T: type, output: []T, value: *const Res
|
||||
u16 => output[0] = i.uint16,
|
||||
u32 => output[0] = i.uint32,
|
||||
u64 => output[0] = i.uint64,
|
||||
inline else => unreachable, // Wanted value may not be composed of ints
|
||||
inline else => return ReadOutputError.InvalidValueType,
|
||||
}
|
||||
},
|
||||
.Float => |f| {
|
||||
@@ -187,13 +193,13 @@ fn readValue(self: *const Self, comptime T: type, output: []T, value: *const Res
|
||||
f16 => output[0] = f.float16,
|
||||
f32 => output[0] = f.float32,
|
||||
f64 => output[0] = f.float64,
|
||||
inline else => unreachable, // Wanted value may not be composed of floats
|
||||
inline else => return ReadOutputError.InvalidValueType,
|
||||
}
|
||||
},
|
||||
.Vector => |values| for (values, 0..) |v, i| self.readValue(T, output[i..], &v),
|
||||
.Matrix => |values| for (values, 0..) |v, i| self.readValue(T, output[i..], &v),
|
||||
.Array => unreachable, // TODO
|
||||
.Structure => |values| for (values, 0..) |v, i| self.readValue(T, output[i..], &v),
|
||||
else => unreachable,
|
||||
.Vector => |values| for (values, 0..) |v, i| try self.readValue(T, output[i..], &v),
|
||||
.Matrix => |values| for (values, 0..) |v, i| try self.readValue(T, output[i..], &v),
|
||||
.Array => |values| for (values, 0..) |v, i| try self.readValue(T, output[i..], &v), // Doubt if this is allowed
|
||||
.Structure => |values| for (values, 0..) |v, i| try self.readValue(T, output[i..], &v),
|
||||
else => return ReadOutputError.InvalidValueType,
|
||||
}
|
||||
}
|
||||
|
||||
38
src/lib.zig
38
src/lib.zig
@@ -1,3 +1,33 @@
|
||||
//! A small footprint SPIR-V interpreter with zero dependencies to execute SPIR-V shaders on the CPU. It is designed to be used with multiple runtimes concurrently.
|
||||
//!
|
||||
//! ```zig
|
||||
//! const std = @import("std");
|
||||
//! const spv = @import("spv");
|
||||
//!
|
||||
//! const shader_source = @embedFile("shader.spv");
|
||||
//!
|
||||
//! pub fn main() !void {
|
||||
//! {
|
||||
//! var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||
//! defer _ = gpa.deinit();
|
||||
//!
|
||||
//! const allocator = gpa.allocator();
|
||||
//!
|
||||
//! var module = try spv.Module.init(allocator, @ptrCast(@alignCast(shader_source)));
|
||||
//! defer module.deinit(allocator);
|
||||
//!
|
||||
//! var rt = try spv.Runtime.init(allocator, &module);
|
||||
//! defer rt.deinit(allocator);
|
||||
//!
|
||||
//! try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main"));
|
||||
//! var output: [4]f32 = undefined;
|
||||
//! try rt.readOutput(f32, output[0..output.len], try rt.getResultByName("color"));
|
||||
//! std.log.info("Output: Vec4{any}", .{output});
|
||||
//! }
|
||||
//! std.log.info("Successfully executed", .{});
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
pub const Image = @import("Image.zig");
|
||||
@@ -6,11 +36,3 @@ pub const Runtime = @import("Runtime.zig");
|
||||
|
||||
const opcodes = @import("opcodes.zig");
|
||||
const spv = @import("spv.zig");
|
||||
|
||||
test {
|
||||
std.testing.refAllDecls(Image);
|
||||
std.testing.refAllDecls(Module);
|
||||
std.testing.refAllDecls(Runtime);
|
||||
std.testing.refAllDecls(opcodes);
|
||||
std.testing.refAllDecls(spv);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ const MathOp = enum {
|
||||
Div,
|
||||
Mod,
|
||||
Mul,
|
||||
Rem,
|
||||
Sub,
|
||||
};
|
||||
|
||||
@@ -463,10 +464,8 @@ fn MathEngine(comptime T: ValueType, comptime Op: MathOp) type {
|
||||
if (op2 == 0) return RuntimeError.DivisionByZero;
|
||||
break :blk if (@typeInfo(TT) == .int) @divTrunc(op1, op2) else op1 / op2;
|
||||
},
|
||||
.Mod => blk: {
|
||||
if (op2 == 0) return RuntimeError.DivisionByZero;
|
||||
break :blk @mod(op1, op2);
|
||||
},
|
||||
.Mod => if (op2 == 0) return RuntimeError.DivisionByZero else @mod(op1, op2),
|
||||
.Rem => if (op2 == 0) return RuntimeError.DivisionByZero else @rem(op1, op2),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user