Compare commits
42 Commits
8a79e8316d
...
improved-t
| Author | SHA1 | Date | |
|---|---|---|---|
|
e09a41754f
|
|||
|
2409ec7269
|
|||
|
35686c3012
|
|||
|
2ea707ea57
|
|||
|
ac4f41ddd4
|
|||
|
61513f20d5
|
|||
|
1974afd6d7
|
|||
|
57de432d0b
|
|||
|
1ad7b644c4
|
|||
|
e21d26d997
|
|||
|
c43ffc136e
|
|||
|
5d296743ee
|
|||
|
dbf963a3c9
|
|||
|
96ad7f12f9
|
|||
|
37da19ed43
|
|||
|
27172539e5
|
|||
|
bb866f1312
|
|||
|
66eb4ba578
|
|||
|
8a08b96777
|
|||
|
c175224a01
|
|||
|
19687251b0
|
|||
|
45adad727d
|
|||
|
c5225e3a45
|
|||
|
df711a196a
|
|||
| 35099b33e1 | |||
|
9fb8cbea05
|
|||
| 0f35c35fd1 | |||
| 9868b34f92 | |||
|
04092e25c1
|
|||
| bb40e5b33f | |||
|
8bdea7b1fc
|
|||
|
8b0b0a72ae
|
|||
| db82448ac0 | |||
|
076abf5d6a
|
|||
|
5466cbcced
|
|||
|
81f5e78863
|
|||
|
32e76945ec
|
|||
| e570b7f19d | |||
|
88e847e2d9
|
|||
|
b8a564e135
|
|||
|
dacd67b858
|
|||
|
14e802709c
|
@@ -23,10 +23,10 @@ jobs:
|
||||
node-version: 24
|
||||
|
||||
- name: Building
|
||||
run: zig build
|
||||
run: zig build -Dno-example=true
|
||||
|
||||
- name: Generating docs
|
||||
run: zig build docs
|
||||
run: zig build docs -Dno-example=true
|
||||
|
||||
- name: Publish to Cloudflare Pages
|
||||
uses: cloudflare/wrangler-action@v3
|
||||
|
||||
@@ -20,4 +20,4 @@ jobs:
|
||||
- uses: mlugg/setup-zig@v2
|
||||
|
||||
- name: Test
|
||||
run: zig build test
|
||||
run: zig build test -Dno-example=true
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,3 +2,5 @@
|
||||
zig-out/
|
||||
.gdb_history
|
||||
*.o
|
||||
vgcore*
|
||||
callgrind*
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# SPIR-V Interpreter <a href="https://git.kbz8.me/kbz_8/SPIRV-Interpreter/actions?workflows=build.yml"><img src="https://git.kbz8.me/kbz_8/SPIRV-Interpreter/actions/workflows/build.yml/badge.svg"></a> <a href="https://git.kbz8.me/kbz_8/SPIRV-Interpreter/actions?workflows=test.yml"><img src="https://git.kbz8.me/kbz_8/SPIRV-Interpreter/actions/workflows/test.yml/badge.svg"></a>
|
||||
|
||||
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.
|
||||
A small footprint SPIR-V interpreter to execute SPIR-V shaders on the CPU. It is designed to be used with multiple runtimes concurrently.
|
||||
|
||||
```zig
|
||||
const std = @import("std");
|
||||
@@ -15,7 +15,7 @@ pub fn main() !void {
|
||||
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
var module = try spv.Module.init(allocator, @ptrCast(@alignCast(shader_source)));
|
||||
var module = try spv.Module.init(allocator, @ptrCast(@alignCast(shader_source)), .{});
|
||||
defer module.deinit(allocator);
|
||||
|
||||
var rt = try spv.Runtime.init(allocator, &module);
|
||||
@@ -24,7 +24,7 @@ pub fn main() !void {
|
||||
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[{d}, {d}, {d}, {d}]", .{ output[0], output[1], output[2], output[3] });
|
||||
std.log.info("Output: Vec4{any}", .{output});
|
||||
}
|
||||
std.log.info("Successfully executed", .{});
|
||||
}
|
||||
|
||||
93
build.zig
93
build.zig
@@ -4,12 +4,17 @@ pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const mod = b.createModule(.{
|
||||
const use_llvm = b.option(bool, "use-llvm", "use llvm") orelse (b.release_mode != .off);
|
||||
|
||||
const mod = b.addModule("spv", .{
|
||||
.root_source_file = b.path("src/lib.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const zmath = b.dependency("zmath", .{});
|
||||
mod.addImport("zmath", zmath.module("root"));
|
||||
|
||||
const pretty = b.dependency("pretty", .{ .target = target, .optimize = optimize });
|
||||
mod.addImport("pretty", pretty.module("pretty"));
|
||||
|
||||
@@ -17,39 +22,97 @@ pub fn build(b: *std.Build) void {
|
||||
.name = "spirv_interpreter",
|
||||
.root_module = mod,
|
||||
.linkage = .dynamic,
|
||||
.use_llvm = true,
|
||||
.use_llvm = use_llvm,
|
||||
});
|
||||
const lib_install = b.addInstallArtifact(lib, .{});
|
||||
|
||||
// Zig example setup
|
||||
|
||||
const example_exe = b.addExecutable(.{
|
||||
.name = "spirv_interpreter_example",
|
||||
const no_example = b.option(bool, "no-example", "skips example dependencies fetch") orelse false;
|
||||
|
||||
if (!no_example) {
|
||||
const sdl3 = b.lazyDependency("sdl3", .{ .target = target, .optimize = optimize }) orelse return;
|
||||
const example_exe = b.addExecutable(.{
|
||||
.name = "spirv_interpreter_example",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("example/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.imports = &.{
|
||||
.{ .name = "spv", .module = mod },
|
||||
.{ .name = "sdl3", .module = sdl3.module("sdl3") },
|
||||
//.{ .name = "pretty", .module = pretty.module("pretty") },
|
||||
},
|
||||
}),
|
||||
.use_llvm = use_llvm,
|
||||
});
|
||||
|
||||
const example_install = b.addInstallArtifact(example_exe, .{});
|
||||
example_install.step.dependOn(&lib_install.step);
|
||||
|
||||
const run_example = b.addRunArtifact(example_exe);
|
||||
run_example.step.dependOn(&example_install.step);
|
||||
|
||||
const run_example_step = b.step("example", "Run the example");
|
||||
run_example_step.dependOn(&run_example.step);
|
||||
|
||||
const compile_shader_cmd = b.addSystemCommand(&[_][]const u8{ "nzslc", "example/shader.nzsl", "--compile=spv,spv-dis", "-o", "example" });
|
||||
const compile_shader_step = b.step("example-shader", "Compiles example's shader (needs nzslc installed)");
|
||||
compile_shader_step.dependOn(&compile_shader_cmd.step);
|
||||
}
|
||||
|
||||
// Zig sandbox setup
|
||||
|
||||
const sandbox_exe = b.addExecutable(.{
|
||||
.name = "spirv_interpreter_sandbox",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("example/main.zig"),
|
||||
.root_source_file = b.path("sandbox/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.imports = &.{
|
||||
.{ .name = "spv", .module = mod },
|
||||
//.{ .name = "pretty", .module = pretty.module("pretty") },
|
||||
},
|
||||
}),
|
||||
.use_llvm = use_llvm,
|
||||
});
|
||||
|
||||
const example_install = b.addInstallArtifact(example_exe, .{});
|
||||
example_install.step.dependOn(&lib_install.step);
|
||||
const sandbox_install = b.addInstallArtifact(sandbox_exe, .{});
|
||||
sandbox_install.step.dependOn(&lib_install.step);
|
||||
|
||||
const run_example = b.addRunArtifact(example_exe);
|
||||
run_example.step.dependOn(&example_install.step);
|
||||
const run_sandbox = b.addRunArtifact(sandbox_exe);
|
||||
run_sandbox.step.dependOn(&sandbox_install.step);
|
||||
|
||||
const run_example_step = b.step("example", "Run the basic example");
|
||||
run_example_step.dependOn(&run_example.step);
|
||||
const run_sandbox_step = b.step("sandbox", "Run the sandbox");
|
||||
run_sandbox_step.dependOn(&run_sandbox.step);
|
||||
|
||||
const compile_shader_cmd = b.addSystemCommand(&[_][]const u8{ "nzslc", "sandbox/shader.nzsl", "--compile=spv,spv-dis", "-o", "sandbox" });
|
||||
const compile_shader_step = b.step("sandbox-shader", "Compiles sandbox's shader (needs nzslc installed)");
|
||||
compile_shader_step.dependOn(&compile_shader_cmd.step);
|
||||
|
||||
// Zig unit tests setup
|
||||
|
||||
const lib_tests = b.addTest(.{ .root_module = mod });
|
||||
const run_tests = b.addRunArtifact(lib_tests);
|
||||
const test_step = b.step("test", "Run Zig unit tests");
|
||||
test_step.dependOn(&run_tests.step);
|
||||
const no_test = b.option(bool, "no-test", "skips unit test dependencies fetch") orelse false;
|
||||
|
||||
if (!no_test) {
|
||||
const nzsl = b.lazyDependency("NZSL", .{ .target = target, .optimize = optimize }) orelse return;
|
||||
const lib_tests = b.addTest(.{
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("test/root.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.imports = &.{
|
||||
.{ .name = "spv", .module = mod },
|
||||
.{ .name = "nzsl", .module = nzsl.module("nzigsl") },
|
||||
.{ .name = "zmath", .module = zmath.module("root") },
|
||||
},
|
||||
}),
|
||||
.test_runner = .{ .path = b.path("test/test_runner.zig"), .mode = .simple },
|
||||
});
|
||||
const run_tests = b.addRunArtifact(lib_tests);
|
||||
const test_step = b.step("test", "Run Zig unit tests");
|
||||
test_step.dependOn(&run_tests.step);
|
||||
}
|
||||
|
||||
// Docs generation
|
||||
|
||||
|
||||
@@ -2,10 +2,24 @@
|
||||
.name = .SPIRV_Interpreter,
|
||||
.version = "0.0.1",
|
||||
.dependencies = .{
|
||||
.zmath = .{
|
||||
.url = "git+https://github.com/zig-gamedev/zmath.git#3a5955b2b72cd081563fbb084eff05bffd1e3fbb",
|
||||
.hash = "zmath-0.11.0-dev-wjwivdMsAwD-xaLj76YHUq3t9JDH-X16xuMTmnDzqbu2",
|
||||
},
|
||||
.pretty = .{ // For debugging purposes
|
||||
.url = "git+https://github.com/Kbz-8/pretty#117674465efd4d07d5ae9d9d8ca59c2c323a65ba",
|
||||
.hash = "pretty-0.10.6-Tm65r99UAQDEJMgZysD10qE8dinBHr064fPM6YkxVPfB",
|
||||
},
|
||||
.NZSL = .{ // For unit tests
|
||||
.url = "git+https://git.kbz8.me/kbz_8/NZigSL#5377dbdf9935b0de767f76ea4650e7aba4516b32",
|
||||
.hash = "NZSL-1.1.2-N0xSVMt6AAC1ncQHA_RafnclWolDA477iTnFmZgdvxd-",
|
||||
.lazy = true,
|
||||
},
|
||||
.sdl3 = .{
|
||||
.url = "git+https://codeberg.org/7Games/zig-sdl3?ref=v0.1.6#9c1842246c59f03f87ba59b160ca7e3d5e5ce972",
|
||||
.hash = "sdl3-0.1.6-NmT1Q5sQJgCzT6hLj7WOSrwxE0Qsef1wIkDopbOOFru0",
|
||||
.lazy = true,
|
||||
},
|
||||
},
|
||||
.minimum_zig_version = "0.15.2",
|
||||
.paths = .{
|
||||
|
||||
136
example/main.zig
136
example/main.zig
@@ -1,25 +1,141 @@
|
||||
const std = @import("std");
|
||||
const sdl3 = @import("sdl3");
|
||||
const spv = @import("spv");
|
||||
|
||||
const shader_source = @embedFile("shader.spv");
|
||||
|
||||
const screen_width = 300;
|
||||
const screen_height = 300;
|
||||
|
||||
pub fn main() !void {
|
||||
{
|
||||
var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||
defer _ = gpa.deinit();
|
||||
//var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||
//defer _ = gpa.deinit();
|
||||
|
||||
const allocator = gpa.allocator();
|
||||
defer sdl3.shutdown();
|
||||
const init_flags = sdl3.InitFlags{ .video = true, .events = true };
|
||||
try sdl3.init(init_flags);
|
||||
defer sdl3.quit(init_flags);
|
||||
|
||||
var module = try spv.Module.init(allocator, @ptrCast(@alignCast(shader_source)));
|
||||
const window = try sdl3.video.Window.init("Hello triangle", screen_width, screen_height, .{});
|
||||
defer window.deinit();
|
||||
|
||||
const surface = try window.getSurface();
|
||||
|
||||
const allocator = std.heap.smp_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);
|
||||
var runner_cache: std.ArrayList(Runner) = try .initCapacity(allocator, screen_height);
|
||||
defer {
|
||||
for (runner_cache.items) |*runner| {
|
||||
runner.rt.deinit(allocator);
|
||||
}
|
||||
runner_cache.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[{d}, {d}, {d}, {d}]", .{ output[0], output[1], output[2], output[3] });
|
||||
for (0..screen_height) |_| {
|
||||
var rt = try spv.Runtime.init(allocator, &module);
|
||||
(try runner_cache.addOne(allocator)).* = .{
|
||||
.allocator = allocator,
|
||||
.surface = surface,
|
||||
.rt = rt,
|
||||
.entry = try rt.getEntryPointByName("main"),
|
||||
.color = try rt.getResultByName("color"),
|
||||
.time = try rt.getResultByName("time"),
|
||||
.pos = try rt.getResultByName("pos"),
|
||||
.res = try rt.getResultByName("res"),
|
||||
};
|
||||
}
|
||||
|
||||
var thread_pool: std.Thread.Pool = undefined;
|
||||
try thread_pool.init(.{ .allocator = allocator });
|
||||
|
||||
var timer = try std.time.Timer.start();
|
||||
|
||||
var quit = false;
|
||||
while (!quit) {
|
||||
try surface.clear(.{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 1.0 });
|
||||
|
||||
while (sdl3.events.poll()) |event|
|
||||
switch (event) {
|
||||
.quit => quit = true,
|
||||
.terminating => quit = true,
|
||||
else => {},
|
||||
};
|
||||
|
||||
{
|
||||
try surface.lock();
|
||||
defer surface.unlock();
|
||||
|
||||
const pixel_map: [*]u32 = @as([*]u32, @ptrCast(@alignCast((surface.getPixels() orelse return).ptr)));
|
||||
|
||||
const delta: f32 = @as(f32, @floatFromInt(timer.read())) / std.time.ns_per_s;
|
||||
|
||||
var frame_timer = try std.time.Timer.start();
|
||||
defer {
|
||||
const ns = frame_timer.lap();
|
||||
const ms = @as(f32, @floatFromInt(ns)) / std.time.ns_per_s;
|
||||
std.log.info("Took {d:.3}s - {d:.3}fps to render", .{ ms, 1.0 / ms });
|
||||
}
|
||||
|
||||
var wait_group: std.Thread.WaitGroup = .{};
|
||||
for (0..screen_height) |y| {
|
||||
const runner = &runner_cache.items[y];
|
||||
thread_pool.spawnWg(&wait_group, Runner.runWrapper, .{ runner, y, pixel_map, delta });
|
||||
}
|
||||
thread_pool.waitAndWork(&wait_group);
|
||||
}
|
||||
|
||||
try window.updateSurface();
|
||||
}
|
||||
}
|
||||
std.log.info("Successfully executed", .{});
|
||||
}
|
||||
|
||||
const Runner = struct {
|
||||
const Self = @This();
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
surface: sdl3.surface.Surface,
|
||||
rt: spv.Runtime,
|
||||
entry: spv.SpvWord,
|
||||
color: spv.SpvWord,
|
||||
time: spv.SpvWord,
|
||||
pos: spv.SpvWord,
|
||||
res: spv.SpvWord,
|
||||
|
||||
fn runWrapper(self: *Self, y: usize, pixel_map: [*]u32, timer: f32) void {
|
||||
@call(.always_inline, Self.run, .{ self, y, pixel_map, timer }) catch |err| {
|
||||
std.log.err("{s}", .{@errorName(err)});
|
||||
if (@errorReturnTrace()) |trace| {
|
||||
std.debug.dumpStackTrace(trace.*);
|
||||
}
|
||||
std.process.abort();
|
||||
};
|
||||
}
|
||||
|
||||
fn run(self: *Self, y: usize, pixel_map: [*]u32, timer: f32) !void {
|
||||
var rt = self.rt; // Copy to avoid pointer access of `self` at runtime. Okay as Runtime contains only pointers and trivially copyable fields
|
||||
|
||||
var output: [4]f32 = undefined;
|
||||
|
||||
for (0..screen_width) |x| {
|
||||
try rt.writeInput(&.{timer}, self.time);
|
||||
try rt.writeInput(&.{ @floatFromInt(screen_width), @floatFromInt(screen_height) }, self.res);
|
||||
try rt.writeInput(&.{ @floatFromInt(x), @floatFromInt(y) }, self.pos);
|
||||
try rt.callEntryPoint(self.allocator, self.entry);
|
||||
try rt.readOutput(output[0..], self.color);
|
||||
|
||||
const rgba = self.surface.mapRgba(
|
||||
@intCast(@max(@min(@as(i32, @intFromFloat(output[0] * 255.0)), 255), 0)),
|
||||
@intCast(@max(@min(@as(i32, @intFromFloat(output[1] * 255.0)), 255), 0)),
|
||||
@intCast(@max(@min(@as(i32, @intFromFloat(output[2] * 255.0)), 255), 0)),
|
||||
@intCast(@max(@min(@as(i32, @intFromFloat(output[3] * 255.0)), 255), 0)),
|
||||
);
|
||||
|
||||
pixel_map[(y * self.surface.getWidth()) + x] = rgba.value;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
6
example/mangohud.conf
git.filemode.normal_file
6
example/mangohud.conf
git.filemode.normal_file
@@ -0,0 +1,6 @@
|
||||
gpu_stats=0
|
||||
font_size=16
|
||||
resolution
|
||||
hud_compact
|
||||
background_alpha=0
|
||||
width=140
|
||||
@@ -1,15 +1,68 @@
|
||||
[nzsl_version("1.1")]
|
||||
module;
|
||||
|
||||
struct FragIn
|
||||
{
|
||||
[location(0)] time: f32,
|
||||
[location(1)] res: vec2[f32],
|
||||
[location(2)] pos: vec2[f32],
|
||||
}
|
||||
|
||||
struct FragOut
|
||||
{
|
||||
[location(0)] color: vec4[f32]
|
||||
}
|
||||
|
||||
[entry(frag)]
|
||||
fn main() -> FragOut
|
||||
fn main(input: FragIn) -> FragOut
|
||||
{
|
||||
const I: i32 = 128;
|
||||
const A: f32 = 7.5;
|
||||
const MA: f32 = 100.0;
|
||||
const MI: f32 = 0.001;
|
||||
|
||||
let uv0 = input.pos / input.res * 2.0 - vec2[f32](1.0, 1.0);
|
||||
let uv = vec2[f32](uv0.x * (input.res.x / input.res.y), uv0.y);
|
||||
|
||||
let col = vec3[f32](0.0, 0.0, 0.0);
|
||||
let ro = vec3[f32](0.0, 0.0, -2.0);
|
||||
let rd = vec3[f32](uv.x, uv.y, 1.0);
|
||||
let dt = 0.0;
|
||||
let ds = 0.0;
|
||||
let dm = -1.0;
|
||||
let p = ro;
|
||||
let c = vec3[f32](0.0, 0.0, 0.0);
|
||||
|
||||
let l = vec3[f32](0.0, sin(input.time * 0.2) * 4.0, cos(input.time * 0.2) * 4.0);
|
||||
|
||||
for i in 0 -> I
|
||||
{
|
||||
p = ro + rd * dt;
|
||||
ds = length(c - p) - 1.0;
|
||||
dt += ds;
|
||||
|
||||
if (dm == -1.0 || ds < dm)
|
||||
dm = ds;
|
||||
|
||||
if (ds <= MI)
|
||||
{
|
||||
let value = max(dot(normalize(c - p), normalize(p - l)) - 0.35, 0.0);
|
||||
col = vec3[f32](value, value, value);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ds >= MA)
|
||||
{
|
||||
if (dot(normalize(rd), normalize(l - ro)) <= 1.0)
|
||||
{
|
||||
let value = max(dot(normalize(rd), normalize(l - ro)) + 0.15, 0.05)/ 1.15 * (1.0 - dm * A);
|
||||
col = vec3[f32](value, value, value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let output: FragOut;
|
||||
output.color = vec4[f32](4.0, 3.0, 2.0, 1.0);
|
||||
output.color = vec4[f32](col.x, col.y, col.z, 1.0);
|
||||
return output;
|
||||
}
|
||||
|
||||
Binary file not shown.
281
example/shader.spv.txt
git.filemode.normal_file
281
example/shader.spv.txt
git.filemode.normal_file
@@ -0,0 +1,281 @@
|
||||
Version 1.0
|
||||
Generator: 2560130
|
||||
Bound: 203
|
||||
Schema: 0
|
||||
OpCapability Capability(Shader)
|
||||
%42 = OpExtInstImport "GLSL.std.450"
|
||||
OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450)
|
||||
OpEntryPoint ExecutionModel(Fragment) %43 "main" %5 %11 %14 %20
|
||||
OpExecutionMode %43 ExecutionMode(OriginUpperLeft)
|
||||
OpSource SourceLanguage(NZSL) 4198400
|
||||
OpSourceExtension "Version: 1.1"
|
||||
OpName %16 "FragIn"
|
||||
OpMemberName %16 0 "time"
|
||||
OpMemberName %16 1 "res"
|
||||
OpMemberName %16 2 "pos"
|
||||
OpName %21 "FragOut"
|
||||
OpMemberName %21 0 "color"
|
||||
OpName %5 "time"
|
||||
OpName %11 "res"
|
||||
OpName %14 "pos"
|
||||
OpName %20 "color"
|
||||
OpName %43 "main"
|
||||
OpDecorate %5 Decoration(Location) 0
|
||||
OpDecorate %11 Decoration(Location) 1
|
||||
OpDecorate %14 Decoration(Location) 2
|
||||
OpDecorate %20 Decoration(Location) 0
|
||||
OpMemberDecorate %16 0 Decoration(Offset) 0
|
||||
OpMemberDecorate %16 1 Decoration(Offset) 8
|
||||
OpMemberDecorate %16 2 Decoration(Offset) 16
|
||||
OpMemberDecorate %21 0 Decoration(Offset) 0
|
||||
%1 = OpTypeVoid
|
||||
%2 = OpTypeFunction %1
|
||||
%3 = OpTypeFloat 32
|
||||
%4 = OpTypePointer StorageClass(Input) %3
|
||||
%6 = OpTypeInt 32 1
|
||||
%7 = OpConstant %6 i32(0)
|
||||
%8 = OpTypePointer StorageClass(Function) %3
|
||||
%9 = OpTypeVector %3 2
|
||||
%10 = OpTypePointer StorageClass(Input) %9
|
||||
%12 = OpConstant %6 i32(1)
|
||||
%13 = OpTypePointer StorageClass(Function) %9
|
||||
%15 = OpConstant %6 i32(2)
|
||||
%16 = OpTypeStruct %3 %9 %9
|
||||
%17 = OpTypePointer StorageClass(Function) %16
|
||||
%18 = OpTypeVector %3 4
|
||||
%19 = OpTypePointer StorageClass(Output) %18
|
||||
%21 = OpTypeStruct %18
|
||||
%22 = OpConstant %3 f32(2)
|
||||
%23 = OpConstant %3 f32(1)
|
||||
%24 = OpConstant %3 f32(0)
|
||||
%25 = OpTypeVector %3 3
|
||||
%26 = OpTypePointer StorageClass(Function) %25
|
||||
%27 = OpConstant %3 f32(-2)
|
||||
%28 = OpConstant %3 f32(-1)
|
||||
%29 = OpConstant %3 f32(0.2)
|
||||
%30 = OpConstant %3 f32(4)
|
||||
%31 = OpTypePointer StorageClass(Function) %6
|
||||
%32 = OpConstant %6 i32(128)
|
||||
%33 = OpTypeBool
|
||||
%34 = OpConstant %3 f32(0.001)
|
||||
%35 = OpConstant %3 f32(0.35)
|
||||
%36 = OpConstant %3 f32(100)
|
||||
%37 = OpConstant %3 f32(0.15)
|
||||
%38 = OpConstant %3 f32(0.05)
|
||||
%39 = OpConstant %3 f32(1.15)
|
||||
%40 = OpConstant %3 f32(7.5)
|
||||
%41 = OpTypePointer StorageClass(Function) %21
|
||||
%200 = OpTypePointer StorageClass(Function) %18
|
||||
%5 = OpVariable %4 StorageClass(Input)
|
||||
%11 = OpVariable %10 StorageClass(Input)
|
||||
%14 = OpVariable %10 StorageClass(Input)
|
||||
%20 = OpVariable %19 StorageClass(Output)
|
||||
%43 = OpFunction %1 FunctionControl(0) %2
|
||||
%44 = OpLabel
|
||||
%45 = OpVariable %13 StorageClass(Function)
|
||||
%46 = OpVariable %13 StorageClass(Function)
|
||||
%47 = OpVariable %26 StorageClass(Function)
|
||||
%48 = OpVariable %26 StorageClass(Function)
|
||||
%49 = OpVariable %26 StorageClass(Function)
|
||||
%50 = OpVariable %8 StorageClass(Function)
|
||||
%51 = OpVariable %8 StorageClass(Function)
|
||||
%52 = OpVariable %8 StorageClass(Function)
|
||||
%53 = OpVariable %26 StorageClass(Function)
|
||||
%54 = OpVariable %26 StorageClass(Function)
|
||||
%55 = OpVariable %26 StorageClass(Function)
|
||||
%56 = OpVariable %31 StorageClass(Function)
|
||||
%57 = OpVariable %31 StorageClass(Function)
|
||||
%58 = OpVariable %8 StorageClass(Function)
|
||||
%59 = OpVariable %8 StorageClass(Function)
|
||||
%60 = OpVariable %41 StorageClass(Function)
|
||||
%61 = OpVariable %17 StorageClass(Function)
|
||||
%62 = OpAccessChain %8 %61 %7
|
||||
OpCopyMemory %62 %5
|
||||
%63 = OpAccessChain %13 %61 %12
|
||||
OpCopyMemory %63 %11
|
||||
%64 = OpAccessChain %13 %61 %15
|
||||
OpCopyMemory %64 %14
|
||||
%65 = OpAccessChain %13 %61 %15
|
||||
%66 = OpLoad %9 %65
|
||||
%67 = OpAccessChain %13 %61 %12
|
||||
%68 = OpLoad %9 %67
|
||||
%69 = OpFDiv %9 %66 %68
|
||||
%70 = OpVectorTimesScalar %9 %69 %22
|
||||
%71 = OpCompositeConstruct %9 %23 %23
|
||||
%72 = OpFSub %9 %70 %71
|
||||
OpStore %45 %72
|
||||
%73 = OpLoad %9 %45
|
||||
%74 = OpCompositeExtract %3 %73 0
|
||||
%75 = OpAccessChain %13 %61 %12
|
||||
%76 = OpLoad %9 %75
|
||||
%77 = OpCompositeExtract %3 %76 0
|
||||
%78 = OpAccessChain %13 %61 %12
|
||||
%79 = OpLoad %9 %78
|
||||
%80 = OpCompositeExtract %3 %79 1
|
||||
%81 = OpFDiv %3 %77 %80
|
||||
%82 = OpFMul %3 %74 %81
|
||||
%83 = OpLoad %9 %45
|
||||
%84 = OpCompositeExtract %3 %83 1
|
||||
%85 = OpCompositeConstruct %9 %82 %84
|
||||
OpStore %46 %85
|
||||
%86 = OpCompositeConstruct %25 %24 %24 %24
|
||||
OpStore %47 %86
|
||||
%87 = OpCompositeConstruct %25 %24 %24 %27
|
||||
OpStore %48 %87
|
||||
%88 = OpLoad %9 %46
|
||||
%89 = OpCompositeExtract %3 %88 0
|
||||
%90 = OpLoad %9 %46
|
||||
%91 = OpCompositeExtract %3 %90 1
|
||||
%92 = OpCompositeConstruct %25 %89 %91 %23
|
||||
OpStore %49 %92
|
||||
OpStore %50 %24
|
||||
OpStore %51 %24
|
||||
OpStore %52 %28
|
||||
%93 = OpLoad %25 %48
|
||||
OpStore %53 %93
|
||||
%94 = OpCompositeConstruct %25 %24 %24 %24
|
||||
OpStore %54 %94
|
||||
%95 = OpAccessChain %8 %61 %7
|
||||
%96 = OpLoad %3 %95
|
||||
%97 = OpFMul %3 %96 %29
|
||||
%98 = OpExtInst %3 GLSLstd450 Sin %97
|
||||
%99 = OpFMul %3 %98 %30
|
||||
%100 = OpAccessChain %8 %61 %7
|
||||
%101 = OpLoad %3 %100
|
||||
%102 = OpFMul %3 %101 %29
|
||||
%103 = OpExtInst %3 GLSLstd450 Cos %102
|
||||
%104 = OpFMul %3 %103 %30
|
||||
%105 = OpCompositeConstruct %25 %24 %99 %104
|
||||
OpStore %55 %105
|
||||
OpStore %56 %7
|
||||
OpStore %57 %32
|
||||
OpBranch %106
|
||||
%106 = OpLabel
|
||||
%110 = OpLoad %6 %56
|
||||
%111 = OpLoad %6 %57
|
||||
%112 = OpSLessThan %33 %110 %111
|
||||
OpLoopMerge %108 %109 LoopControl(0)
|
||||
OpBranchConditional %112 %107 %108
|
||||
%107 = OpLabel
|
||||
%113 = OpLoad %25 %48
|
||||
%114 = OpLoad %25 %49
|
||||
%115 = OpLoad %3 %50
|
||||
%116 = OpVectorTimesScalar %25 %114 %115
|
||||
%117 = OpFAdd %25 %113 %116
|
||||
OpStore %53 %117
|
||||
%118 = OpLoad %25 %54
|
||||
%119 = OpLoad %25 %53
|
||||
%120 = OpFSub %25 %118 %119
|
||||
%121 = OpExtInst %3 GLSLstd450 Length %120
|
||||
%122 = OpFSub %3 %121 %23
|
||||
OpStore %51 %122
|
||||
%123 = OpLoad %3 %50
|
||||
%124 = OpLoad %3 %51
|
||||
%125 = OpFAdd %3 %123 %124
|
||||
OpStore %50 %125
|
||||
%129 = OpLoad %3 %52
|
||||
%130 = OpFOrdEqual %33 %129 %28
|
||||
%131 = OpLoad %3 %51
|
||||
%132 = OpLoad %3 %52
|
||||
%133 = OpFOrdLessThan %33 %131 %132
|
||||
%134 = OpLogicalOr %33 %130 %133
|
||||
OpSelectionMerge %126 SelectionControl(0)
|
||||
OpBranchConditional %134 %127 %128
|
||||
%127 = OpLabel
|
||||
%135 = OpLoad %3 %51
|
||||
OpStore %52 %135
|
||||
OpBranch %126
|
||||
%128 = OpLabel
|
||||
OpBranch %126
|
||||
%126 = OpLabel
|
||||
%139 = OpLoad %3 %51
|
||||
%140 = OpFOrdLessThanEqual %33 %139 %34
|
||||
OpSelectionMerge %136 SelectionControl(0)
|
||||
OpBranchConditional %140 %137 %138
|
||||
%137 = OpLabel
|
||||
%141 = OpLoad %25 %54
|
||||
%142 = OpLoad %25 %53
|
||||
%143 = OpFSub %25 %141 %142
|
||||
%144 = OpExtInst %25 GLSLstd450 Normalize %143
|
||||
%145 = OpLoad %25 %53
|
||||
%146 = OpLoad %25 %55
|
||||
%147 = OpFSub %25 %145 %146
|
||||
%148 = OpExtInst %25 GLSLstd450 Normalize %147
|
||||
%149 = OpDot %3 %144 %148
|
||||
%150 = OpFSub %3 %149 %35
|
||||
%151 = OpExtInst %3 GLSLstd450 FMax %150 %24
|
||||
OpStore %58 %151
|
||||
%152 = OpLoad %3 %58
|
||||
%153 = OpLoad %3 %58
|
||||
%154 = OpLoad %3 %58
|
||||
%155 = OpCompositeConstruct %25 %152 %153 %154
|
||||
OpStore %47 %155
|
||||
OpBranch %108
|
||||
%138 = OpLabel
|
||||
OpBranch %136
|
||||
%136 = OpLabel
|
||||
%159 = OpLoad %3 %51
|
||||
%160 = OpFOrdGreaterThanEqual %33 %159 %36
|
||||
OpSelectionMerge %156 SelectionControl(0)
|
||||
OpBranchConditional %160 %157 %158
|
||||
%157 = OpLabel
|
||||
%164 = OpLoad %25 %49
|
||||
%165 = OpExtInst %25 GLSLstd450 Normalize %164
|
||||
%166 = OpLoad %25 %55
|
||||
%167 = OpLoad %25 %48
|
||||
%168 = OpFSub %25 %166 %167
|
||||
%169 = OpExtInst %25 GLSLstd450 Normalize %168
|
||||
%170 = OpDot %3 %165 %169
|
||||
%171 = OpFOrdLessThanEqual %33 %170 %23
|
||||
OpSelectionMerge %161 SelectionControl(0)
|
||||
OpBranchConditional %171 %162 %163
|
||||
%162 = OpLabel
|
||||
%172 = OpLoad %25 %49
|
||||
%173 = OpExtInst %25 GLSLstd450 Normalize %172
|
||||
%174 = OpLoad %25 %55
|
||||
%175 = OpLoad %25 %48
|
||||
%176 = OpFSub %25 %174 %175
|
||||
%177 = OpExtInst %25 GLSLstd450 Normalize %176
|
||||
%178 = OpDot %3 %173 %177
|
||||
%179 = OpFAdd %3 %178 %37
|
||||
%180 = OpExtInst %3 GLSLstd450 FMax %179 %38
|
||||
%181 = OpFDiv %3 %180 %39
|
||||
%182 = OpLoad %3 %52
|
||||
%183 = OpFMul %3 %182 %40
|
||||
%184 = OpFSub %3 %23 %183
|
||||
%185 = OpFMul %3 %181 %184
|
||||
OpStore %59 %185
|
||||
%186 = OpLoad %3 %59
|
||||
%187 = OpLoad %3 %59
|
||||
%188 = OpLoad %3 %59
|
||||
%189 = OpCompositeConstruct %25 %186 %187 %188
|
||||
OpStore %47 %189
|
||||
OpBranch %161
|
||||
%163 = OpLabel
|
||||
OpBranch %161
|
||||
%161 = OpLabel
|
||||
OpBranch %108
|
||||
%158 = OpLabel
|
||||
OpBranch %156
|
||||
%156 = OpLabel
|
||||
%190 = OpLoad %6 %56
|
||||
%191 = OpIAdd %6 %190 %12
|
||||
OpStore %56 %191
|
||||
OpBranch %109
|
||||
%109 = OpLabel
|
||||
OpBranch %106
|
||||
%108 = OpLabel
|
||||
%192 = OpLoad %25 %47
|
||||
%193 = OpCompositeExtract %3 %192 0
|
||||
%194 = OpLoad %25 %47
|
||||
%195 = OpCompositeExtract %3 %194 1
|
||||
%196 = OpLoad %25 %47
|
||||
%197 = OpCompositeExtract %3 %196 2
|
||||
%198 = OpCompositeConstruct %18 %193 %195 %197 %23
|
||||
%199 = OpAccessChain %200 %60 %7
|
||||
OpStore %199 %198
|
||||
%201 = OpLoad %21 %60
|
||||
%202 = OpCompositeExtract %18 %201 0
|
||||
OpStore %20 %202
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
@@ -1,43 +0,0 @@
|
||||
; SPIR-V
|
||||
; Version: 1.0
|
||||
; Generator: SirLynix Nazara ShaderLang Compiler; 4226
|
||||
; Bound: 23
|
||||
; Schema: 0
|
||||
OpCapability Shader
|
||||
OpMemoryModel Logical GLSL450
|
||||
OpEntryPoint Fragment %main "main" %color
|
||||
OpExecutionMode %main OriginUpperLeft
|
||||
OpSource NZSL 4198400
|
||||
OpSourceExtension "Version: 1.1"
|
||||
OpName %FragOut "FragOut"
|
||||
OpMemberName %FragOut 0 "color"
|
||||
OpName %color "color"
|
||||
OpName %main "main"
|
||||
OpDecorate %color Location 0
|
||||
OpMemberDecorate %FragOut 0 Offset 0
|
||||
%void = OpTypeVoid
|
||||
%2 = OpTypeFunction %void
|
||||
%float = OpTypeFloat 32
|
||||
%v4float = OpTypeVector %float 4
|
||||
%_ptr_Output_v4float = OpTypePointer Output %v4float
|
||||
%FragOut = OpTypeStruct %v4float
|
||||
%_ptr_Function_FragOut = OpTypePointer Function %FragOut
|
||||
%int = OpTypeInt 32 1
|
||||
%int_0 = OpConstant %int 0
|
||||
%float_4 = OpConstant %float 4
|
||||
%float_3 = OpConstant %float 3
|
||||
%float_2 = OpConstant %float 2
|
||||
%float_1 = OpConstant %float 1
|
||||
%_ptr_Function_v4float = OpTypePointer Function %v4float
|
||||
%color = OpVariable %_ptr_Output_v4float Output
|
||||
%main = OpFunction %void None %2
|
||||
%16 = OpLabel
|
||||
%17 = OpVariable %_ptr_Function_FragOut Function
|
||||
%18 = OpCompositeConstruct %v4float %float_4 %float_3 %float_2 %float_1
|
||||
%19 = OpAccessChain %_ptr_Function_v4float %17 %int_0
|
||||
OpStore %19 %18
|
||||
%21 = OpLoad %FragOut %17
|
||||
%22 = OpCompositeExtract %v4float %21 0
|
||||
OpStore %color %22
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
52
sandbox/main.zig
git.filemode.normal_file
52
sandbox/main.zig
git.filemode.normal_file
@@ -0,0 +1,52 @@
|
||||
const std = @import("std");
|
||||
const spv = @import("spv");
|
||||
|
||||
const shader_source = @embedFile("shader.spv");
|
||||
|
||||
const SSBO = struct {
|
||||
value: [256]i32 = [_]i32{0} ** 256,
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
{
|
||||
var gpa: std.heap.DebugAllocator(.{
|
||||
.enable_memory_limit = true,
|
||||
}) = .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);
|
||||
|
||||
const entry = try rt.getEntryPointByName("main");
|
||||
|
||||
var ssbo: SSBO = .{};
|
||||
|
||||
for (0..16) |i| {
|
||||
for (0..16) |x| {
|
||||
const global_invocation_indices = [3]i32{
|
||||
@as(i32, @intCast(i * 16 + x)),
|
||||
1,
|
||||
1,
|
||||
};
|
||||
|
||||
try rt.writeBuiltIn(std.mem.asBytes(&global_invocation_indices), .GlobalInvocationId);
|
||||
try rt.writeDescriptorSet(allocator, std.mem.asBytes(&ssbo), 0, 0);
|
||||
rt.callEntryPoint(allocator, entry) catch |err| switch (err) {
|
||||
spv.Runtime.RuntimeError.OutOfBounds => continue,
|
||||
else => return err,
|
||||
};
|
||||
try rt.readDescriptorSet(std.mem.asBytes(&ssbo), 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
std.log.info("Output: {any}", .{ssbo});
|
||||
|
||||
std.log.info("Total memory used: {d:.3} KB\n", .{@as(f32, @floatFromInt(gpa.total_requested_bytes)) / 1000.0});
|
||||
}
|
||||
std.log.info("Successfully executed", .{});
|
||||
}
|
||||
25
sandbox/shader.nzsl
git.filemode.normal_file
25
sandbox/shader.nzsl
git.filemode.normal_file
@@ -0,0 +1,25 @@
|
||||
[nzsl_version("1.1")]
|
||||
module;
|
||||
|
||||
struct Input
|
||||
{
|
||||
[builtin(global_invocation_indices)] indices: vec3[u32]
|
||||
}
|
||||
|
||||
[layout(std430)]
|
||||
struct SSBO
|
||||
{
|
||||
data: dyn_array[i32]
|
||||
}
|
||||
|
||||
external
|
||||
{
|
||||
[set(0), binding(0)] ssbo: storage[SSBO],
|
||||
}
|
||||
|
||||
[entry(compute)]
|
||||
[workgroup(16, 1, 1)]
|
||||
fn main(input: Input)
|
||||
{
|
||||
ssbo.data[input.indices.x] = i32(input.indices.x);
|
||||
}
|
||||
BIN
sandbox/shader.spv
git.filemode.normal_file
BIN
sandbox/shader.spv
git.filemode.normal_file
Binary file not shown.
59
sandbox/shader.spv.txt
git.filemode.normal_file
59
sandbox/shader.spv.txt
git.filemode.normal_file
@@ -0,0 +1,59 @@
|
||||
Version 1.0
|
||||
Generator: 2560130
|
||||
Bound: 32
|
||||
Schema: 0
|
||||
OpCapability Capability(Shader)
|
||||
OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450)
|
||||
OpEntryPoint ExecutionModel(GLCompute) %17 "main" %11
|
||||
OpExecutionMode %17 ExecutionMode(LocalSize) 16 1 1
|
||||
OpSource SourceLanguage(NZSL) 4198400
|
||||
OpSourceExtension "Version: 1.1"
|
||||
OpName %3 "SSBO"
|
||||
OpMemberName %3 0 "data"
|
||||
OpName %14 "Input"
|
||||
OpMemberName %14 0 "indices"
|
||||
OpName %5 "ssbo"
|
||||
OpName %11 "global_invocation_indices"
|
||||
OpName %17 "main"
|
||||
OpDecorate %5 Decoration(Binding) 0
|
||||
OpDecorate %5 Decoration(DescriptorSet) 0
|
||||
OpDecorate %11 Decoration(BuiltIn) BuiltIn(GlobalInvocationId)
|
||||
OpDecorate %2 Decoration(ArrayStride) 4
|
||||
OpDecorate %3 Decoration(BufferBlock)
|
||||
OpMemberDecorate %3 0 Decoration(Offset) 0
|
||||
OpMemberDecorate %14 0 Decoration(Offset) 0
|
||||
%1 = OpTypeInt 32 1
|
||||
%2 = OpTypeRuntimeArray %1
|
||||
%3 = OpTypeStruct %2
|
||||
%4 = OpTypePointer StorageClass(Uniform) %3
|
||||
%6 = OpTypeVoid
|
||||
%7 = OpTypeFunction %6
|
||||
%8 = OpTypeInt 32 0
|
||||
%9 = OpTypeVector %8 3
|
||||
%10 = OpTypePointer StorageClass(Input) %9
|
||||
%12 = OpConstant %1 i32(0)
|
||||
%13 = OpTypePointer StorageClass(Function) %9
|
||||
%14 = OpTypeStruct %9
|
||||
%15 = OpTypePointer StorageClass(Function) %14
|
||||
%16 = OpTypeRuntimeArray %1
|
||||
%26 = OpTypePointer StorageClass(Uniform) %2
|
||||
%31 = OpTypePointer StorageClass(Uniform) %1
|
||||
%5 = OpVariable %4 StorageClass(Uniform)
|
||||
%11 = OpVariable %10 StorageClass(Input)
|
||||
%17 = OpFunction %6 FunctionControl(0) %7
|
||||
%18 = OpLabel
|
||||
%19 = OpVariable %15 StorageClass(Function)
|
||||
%20 = OpAccessChain %13 %19 %12
|
||||
OpCopyMemory %20 %11
|
||||
%21 = OpAccessChain %13 %19 %12
|
||||
%22 = OpLoad %9 %21
|
||||
%23 = OpCompositeExtract %8 %22 0
|
||||
%24 = OpBitcast %1 %23
|
||||
%25 = OpAccessChain %26 %5 %12
|
||||
%27 = OpAccessChain %13 %19 %12
|
||||
%28 = OpLoad %9 %27
|
||||
%29 = OpCompositeExtract %8 %28 0
|
||||
%30 = OpAccessChain %31 %25 %29
|
||||
OpStore %30 %24
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
91
src/GLSL_std_450/GLSL_std_450.zig
git.filemode.normal_file
91
src/GLSL_std_450/GLSL_std_450.zig
git.filemode.normal_file
@@ -0,0 +1,91 @@
|
||||
//! A jam file of translated GLSL std450 header's enums and utils
|
||||
|
||||
pub const GLSLstd450Version: u32 = 100;
|
||||
pub const GLSLstd450Revision: u32 = 3;
|
||||
|
||||
pub const GLSLOp = enum(u32) {
|
||||
Bad = 0,
|
||||
Round = 1,
|
||||
RoundEven = 2,
|
||||
Trunc = 3,
|
||||
FAbs = 4,
|
||||
SAbs = 5,
|
||||
FSign = 6,
|
||||
SSign = 7,
|
||||
Floor = 8,
|
||||
Ceil = 9,
|
||||
Fract = 10,
|
||||
Radians = 11,
|
||||
Degrees = 12,
|
||||
Sin = 13,
|
||||
Cos = 14,
|
||||
Tan = 15,
|
||||
Asin = 16,
|
||||
Acos = 17,
|
||||
Atan = 18,
|
||||
Sinh = 19,
|
||||
Cosh = 20,
|
||||
Tanh = 21,
|
||||
Asinh = 22,
|
||||
Acosh = 23,
|
||||
Atanh = 24,
|
||||
Atan2 = 25,
|
||||
Pow = 26,
|
||||
Exp = 27,
|
||||
Log = 28,
|
||||
Exp2 = 29,
|
||||
Log2 = 30,
|
||||
Sqrt = 31,
|
||||
InverseSqrt = 32,
|
||||
Determinant = 33,
|
||||
MatrixInverse = 34,
|
||||
Modf = 35,
|
||||
ModfStruct = 36,
|
||||
FMin = 37,
|
||||
UMin = 38,
|
||||
SMin = 39,
|
||||
FMax = 40,
|
||||
UMax = 41,
|
||||
SMax = 42,
|
||||
FClamp = 43,
|
||||
UClamp = 44,
|
||||
SClamp = 45,
|
||||
FMix = 46,
|
||||
IMix = 47,
|
||||
Step = 48,
|
||||
SmoothStep = 49,
|
||||
Fma = 50,
|
||||
Frexp = 51,
|
||||
FrexpStruct = 52,
|
||||
Ldexp = 53,
|
||||
PackSnorm4x8 = 54,
|
||||
PackUnorm4x8 = 55,
|
||||
PackSnorm2x16 = 56,
|
||||
PackUnorm2x16 = 57,
|
||||
PackHalf2x16 = 58,
|
||||
PackDouble2x32 = 59,
|
||||
UnpackSnorm2x16 = 60,
|
||||
UnpackUnorm2x16 = 61,
|
||||
UnpackHalf2x16 = 62,
|
||||
UnpackSnorm4x8 = 63,
|
||||
UnpackUnorm4x8 = 64,
|
||||
UnpackDouble2x32 = 65,
|
||||
Length = 66,
|
||||
Distance = 67,
|
||||
Cross = 68,
|
||||
Normalize = 69,
|
||||
FaceForward = 70,
|
||||
Reflect = 71,
|
||||
Refract = 72,
|
||||
FindILsb = 73,
|
||||
FindSMsb = 74,
|
||||
FindUMsb = 75,
|
||||
InterpolateAtCentroid = 76,
|
||||
InterpolateAtSample = 77,
|
||||
InterpolateAtOffset = 78,
|
||||
NMin = 79,
|
||||
NMax = 80,
|
||||
NClamp = 81,
|
||||
};
|
||||
|
||||
pub const GLSLOpMaxValue: usize = 82;
|
||||
330
src/GLSL_std_450/opcodes.zig
git.filemode.normal_file
330
src/GLSL_std_450/opcodes.zig
git.filemode.normal_file
@@ -0,0 +1,330 @@
|
||||
const std = @import("std");
|
||||
const spv = @import("../spv.zig");
|
||||
const ext = @import("GLSL_std_450.zig");
|
||||
const opc = @import("../opcodes.zig");
|
||||
const zm = @import("zmath");
|
||||
|
||||
const Module = @import("../Module.zig");
|
||||
const Runtime = @import("../Runtime.zig");
|
||||
const Result = @import("../Result.zig");
|
||||
const WordIterator = @import("../WordIterator.zig");
|
||||
|
||||
const RuntimeError = Runtime.RuntimeError;
|
||||
const ValueType = opc.ValueType;
|
||||
|
||||
const getValuePrimitiveField = opc.getValuePrimitiveField;
|
||||
const getValuePrimitiveFieldType = opc.getValuePrimitiveFieldType;
|
||||
|
||||
const SpvVoid = spv.SpvVoid;
|
||||
const SpvByte = spv.SpvByte;
|
||||
const SpvWord = spv.SpvWord;
|
||||
const SpvBool = spv.SpvBool;
|
||||
|
||||
const MathOp = enum {
|
||||
Acos,
|
||||
Acosh,
|
||||
Asin,
|
||||
Asinh,
|
||||
Atan,
|
||||
Atan2,
|
||||
Atanh,
|
||||
Ceil,
|
||||
Cos,
|
||||
Cosh,
|
||||
Determinant,
|
||||
Exp,
|
||||
Exp2,
|
||||
FAbs,
|
||||
FClamp,
|
||||
FMax,
|
||||
FMin,
|
||||
FMix,
|
||||
FSign,
|
||||
Floor,
|
||||
Fract,
|
||||
IMix,
|
||||
InverseSqrt,
|
||||
Log,
|
||||
Log2,
|
||||
Modf,
|
||||
Pow,
|
||||
Round,
|
||||
RoundEven,
|
||||
SAbs,
|
||||
SClamp,
|
||||
SMax,
|
||||
SMin,
|
||||
SSign,
|
||||
Sin,
|
||||
Sinh,
|
||||
Sqrt,
|
||||
Tan,
|
||||
Tanh,
|
||||
Trunc,
|
||||
UClamp,
|
||||
UMax,
|
||||
UMin,
|
||||
};
|
||||
|
||||
pub const OpCodeExtFunc = opc.OpCodeExtFunc;
|
||||
|
||||
/// Not an EnumMap as it is way too slow for this purpose
|
||||
pub var runtime_dispatcher = [_]?OpCodeExtFunc{null} ** ext.GLSLOpMaxValue;
|
||||
|
||||
pub fn initRuntimeDispatcher() void {
|
||||
// zig fmt: off
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Ceil)] = MathEngine(.Float, .Ceil).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Cos)] = MathEngine(.Float, .Cos).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Exp)] = MathEngine(.Float, .Exp).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Exp2)] = MathEngine(.Float, .Exp2).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.FAbs)] = MathEngine(.Float, .FAbs).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.FMax)] = MathEngine(.Float, .FMax).opDoubleOperators;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Floor)] = MathEngine(.Float, .Floor).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Length)] = opLength;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Log)] = MathEngine(.Float, .Log).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Log2)] = MathEngine(.Float, .Log2).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Normalize)] = opNormalize;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Round)] = MathEngine(.Float, .Round).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.SAbs)] = MathEngine(.SInt, .SAbs).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Sin)] = MathEngine(.Float, .Sin).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Sqrt)] = MathEngine(.Float, .Sqrt).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Tan)] = MathEngine(.Float, .Tan).opSingleOperator;
|
||||
runtime_dispatcher[@intFromEnum(ext.GLSLOp.Trunc)] = MathEngine(.Float, .Trunc).opSingleOperator;
|
||||
// zig fmt: on
|
||||
}
|
||||
|
||||
fn MathEngine(comptime T: ValueType, comptime Op: MathOp) type {
|
||||
return struct {
|
||||
fn opSingleOperator(_: std.mem.Allocator, target_type_id: SpvWord, id: SpvWord, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
const target_type = (try rt.results[target_type_id].getVariant()).Type;
|
||||
const dst = try rt.results[id].getValue();
|
||||
const src = try rt.results[try rt.it.next()].getValue();
|
||||
|
||||
const lane_bits = try Result.resolveLaneBitWidth(target_type, rt);
|
||||
|
||||
const operator = struct {
|
||||
fn operation(comptime TT: type, x: TT) RuntimeError!TT {
|
||||
return switch (Op) {
|
||||
.Ceil => @ceil(x),
|
||||
.Cos => @cos(x),
|
||||
.Exp => @exp(x),
|
||||
.Exp2 => @exp2(x),
|
||||
.FAbs => @abs(x),
|
||||
.Floor => @floor(x),
|
||||
.Log => @log(x),
|
||||
.Log2 => @log2(x),
|
||||
.Round => @round(x),
|
||||
.SAbs => if (comptime @typeInfo(TT) == .int) @intCast(@abs(x)) else return RuntimeError.InvalidSpirV,
|
||||
.Sin => @sin(x),
|
||||
.Sqrt => @sqrt(x),
|
||||
.Tan => @tan(x),
|
||||
.Trunc => @trunc(x),
|
||||
else => RuntimeError.InvalidSpirV,
|
||||
};
|
||||
}
|
||||
|
||||
fn applyScalar(bit_count: SpvWord, d: *Result.Value, s: *const Result.Value) RuntimeError!void {
|
||||
switch (bit_count) {
|
||||
inline 8, 16, 32, 64 => |bits| {
|
||||
if (bits == 8 and T == .Float) return RuntimeError.InvalidSpirV;
|
||||
|
||||
const ScalarT = getValuePrimitiveFieldType(T, bits);
|
||||
const d_field = try getValuePrimitiveField(T, bits, d);
|
||||
const s_field = try getValuePrimitiveField(T, bits, @constCast(s));
|
||||
d_field.* = try operation(ScalarT, s_field.*);
|
||||
},
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
switch (dst.*) {
|
||||
.Int, .Float => try operator.applyScalar(lane_bits, dst, src),
|
||||
|
||||
.Vector => |dst_vec| for (dst_vec, src.Vector) |*d_lane, s_lane| {
|
||||
try operator.applyScalar(lane_bits, d_lane, &s_lane);
|
||||
},
|
||||
|
||||
.Vector4f32 => |*d| d.* = try operator.operation(@Vector(4, f32), src.Vector4f32),
|
||||
.Vector3f32 => |*d| d.* = try operator.operation(@Vector(3, f32), src.Vector3f32),
|
||||
.Vector2f32 => |*d| d.* = try operator.operation(@Vector(2, f32), src.Vector2f32),
|
||||
|
||||
//.Vector4i32 => |*d| d.* = try operator.operation(@Vector(4, i32), src.Vector4i32),
|
||||
//.Vector3i32 => |*d| d.* = try operator.operation(@Vector(3, i32), src.Vector3i32),
|
||||
//.Vector2i32 => |*d| d.* = try operator.operation(@Vector(2, i32), src.Vector2i32),
|
||||
|
||||
//.Vector4u32 => |*d| d.* = try operator.operation(@Vector(4, u32), src.Vector4u32),
|
||||
//.Vector3u32 => |*d| d.* = try operator.operation(@Vector(3, u32), src.Vector3u32),
|
||||
//.Vector2u32 => |*d| d.* = try operator.operation(@Vector(2, u32), src.Vector2u32),
|
||||
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
}
|
||||
}
|
||||
|
||||
fn opDoubleOperators(_: std.mem.Allocator, target_type_id: SpvWord, id: SpvWord, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
const target_type = (try rt.results[target_type_id].getVariant()).Type;
|
||||
const dst = try rt.results[id].getValue();
|
||||
const lhs = try rt.results[try rt.it.next()].getValue();
|
||||
const rhs = try rt.results[try rt.it.next()].getValue();
|
||||
|
||||
const lane_bits = try Result.resolveLaneBitWidth(target_type, rt);
|
||||
|
||||
const operator = struct {
|
||||
fn operation(comptime TT: type, l: TT, r: TT) RuntimeError!TT {
|
||||
return switch (Op) {
|
||||
.FMax => @max(l, r),
|
||||
else => RuntimeError.InvalidSpirV,
|
||||
};
|
||||
}
|
||||
|
||||
fn applyScalar(bit_count: SpvWord, d: *Result.Value, l: *const Result.Value, r: *const Result.Value) RuntimeError!void {
|
||||
switch (bit_count) {
|
||||
inline 8, 16, 32, 64 => |bits| {
|
||||
if (bits == 8 and T == .Float) return RuntimeError.InvalidSpirV;
|
||||
|
||||
const ScalarT = getValuePrimitiveFieldType(T, bits);
|
||||
const d_field = try getValuePrimitiveField(T, bits, d);
|
||||
const l_field = try getValuePrimitiveField(T, bits, @constCast(l));
|
||||
const r_field = try getValuePrimitiveField(T, bits, @constCast(r));
|
||||
d_field.* = try operation(ScalarT, l_field.*, r_field.*);
|
||||
},
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
switch (dst.*) {
|
||||
.Int, .Float => try operator.applyScalar(lane_bits, dst, lhs, rhs),
|
||||
|
||||
.Vector => |dst_vec| for (dst_vec, lhs.Vector, rhs.Vector) |*d_lane, l_lane, r_lane| {
|
||||
try operator.applyScalar(lane_bits, d_lane, &l_lane, &r_lane);
|
||||
},
|
||||
|
||||
.Vector4f32 => |*d| d.* = try operator.operation(@Vector(4, f32), lhs.Vector4f32, rhs.Vector4f32),
|
||||
.Vector3f32 => |*d| d.* = try operator.operation(@Vector(3, f32), lhs.Vector3f32, rhs.Vector3f32),
|
||||
.Vector2f32 => |*d| d.* = try operator.operation(@Vector(2, f32), lhs.Vector2f32, rhs.Vector2f32),
|
||||
|
||||
.Vector4i32 => |*d| d.* = try operator.operation(@Vector(4, i32), lhs.Vector4i32, rhs.Vector4i32),
|
||||
.Vector3i32 => |*d| d.* = try operator.operation(@Vector(3, i32), lhs.Vector3i32, rhs.Vector3i32),
|
||||
.Vector2i32 => |*d| d.* = try operator.operation(@Vector(2, i32), lhs.Vector2i32, rhs.Vector2i32),
|
||||
|
||||
.Vector4u32 => |*d| d.* = try operator.operation(@Vector(4, u32), lhs.Vector4u32, rhs.Vector4u32),
|
||||
.Vector3u32 => |*d| d.* = try operator.operation(@Vector(3, u32), lhs.Vector3u32, rhs.Vector3u32),
|
||||
.Vector2u32 => |*d| d.* = try operator.operation(@Vector(2, u32), lhs.Vector2u32, rhs.Vector2u32),
|
||||
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn opLength(_: std.mem.Allocator, target_type_id: SpvWord, id: SpvWord, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
const target_type = (try rt.results[target_type_id].getVariant()).Type;
|
||||
const dst = try rt.results[id].getValue();
|
||||
const src = try rt.results[try rt.it.next()].getValue();
|
||||
|
||||
const lane_bits = try Result.resolveLaneBitWidth(target_type, rt);
|
||||
|
||||
switch (lane_bits) {
|
||||
inline 16, 32, 64 => |bits| {
|
||||
var sum: std.meta.Float(bits) = 0.0;
|
||||
const d_field = try getValuePrimitiveField(.Float, bits, dst);
|
||||
|
||||
if (bits == 32) { // More likely to be SIMD if f32
|
||||
switch (src.*) {
|
||||
.Vector4f32 => |src_vec| {
|
||||
d_field.* = zm.length4(src_vec)[0];
|
||||
return;
|
||||
},
|
||||
.Vector3f32 => |src_vec| {
|
||||
d_field.* = zm.length3(zm.f32x4(src_vec[0], src_vec[1], src_vec[2], 0.0))[0];
|
||||
return;
|
||||
},
|
||||
.Vector2f32 => |src_vec| {
|
||||
d_field.* = zm.length2(zm.f32x4(src_vec[0], src_vec[1], 0.0, 0.0))[0];
|
||||
return;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
switch (src.*) {
|
||||
.Float => {
|
||||
// Fast path
|
||||
const s_field = try getValuePrimitiveField(.Float, bits, src);
|
||||
d_field.* = s_field.*;
|
||||
return;
|
||||
},
|
||||
.Vector => |src_vec| for (src_vec) |*s_lane| {
|
||||
const s_field = try getValuePrimitiveField(.Float, bits, s_lane);
|
||||
sum += s_field.*;
|
||||
},
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
}
|
||||
|
||||
d_field.* = @sqrt(sum);
|
||||
},
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
}
|
||||
}
|
||||
|
||||
fn opNormalize(_: std.mem.Allocator, target_type_id: SpvWord, id: SpvWord, _: SpvWord, rt: *Runtime) RuntimeError!void {
|
||||
const target_type = (try rt.results[target_type_id].getVariant()).Type;
|
||||
const dst = try rt.results[id].getValue();
|
||||
const src = try rt.results[try rt.it.next()].getValue();
|
||||
|
||||
const lane_bits = try Result.resolveLaneBitWidth(target_type, rt);
|
||||
|
||||
switch (lane_bits) {
|
||||
inline 16, 32, 64 => |bits| {
|
||||
if (bits == 32) { // More likely to be SIMD if f32
|
||||
switch (src.*) {
|
||||
.Vector4f32 => |src_vec| {
|
||||
dst.Vector4f32 = zm.normalize4(src_vec);
|
||||
return;
|
||||
},
|
||||
.Vector3f32 => |src_vec| {
|
||||
const normed = zm.normalize3(zm.f32x4(src_vec[0], src_vec[1], src_vec[2], 0.0));
|
||||
dst.Vector3f32[0] = normed[0];
|
||||
dst.Vector3f32[1] = normed[1];
|
||||
dst.Vector3f32[2] = normed[2];
|
||||
return;
|
||||
},
|
||||
.Vector2f32 => |src_vec| {
|
||||
const normed = zm.normalize2(zm.f32x4(src_vec[0], src_vec[1], 0.0, 0.0));
|
||||
dst.Vector2f32[0] = normed[0];
|
||||
dst.Vector2f32[1] = normed[1];
|
||||
return;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
var sum: std.meta.Float(bits) = 0.0;
|
||||
|
||||
switch (src.*) {
|
||||
.Float => {
|
||||
const s_field = try getValuePrimitiveField(.Float, bits, src);
|
||||
sum = s_field.*;
|
||||
},
|
||||
.Vector => |src_vec| for (src_vec) |*s_lane| {
|
||||
const s_field = try getValuePrimitiveField(.Float, bits, s_lane);
|
||||
sum += s_field.*;
|
||||
},
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
}
|
||||
|
||||
sum = @sqrt(sum);
|
||||
|
||||
switch (dst.*) {
|
||||
.Vector => |dst_vec| for (dst_vec, src.Vector) |*d_lane, *s_lane| {
|
||||
const d_field = try getValuePrimitiveField(.Float, bits, d_lane);
|
||||
const s_field = try getValuePrimitiveField(.Float, bits, s_lane);
|
||||
d_field.* = s_field.* / sum;
|
||||
},
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
}
|
||||
},
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
}
|
||||
}
|
||||
101
src/Module.zig
101
src/Module.zig
@@ -19,6 +19,10 @@ const Value = Result.Value;
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub const ModuleOptions = struct {
|
||||
use_simd_vectors_specializations: bool = true,
|
||||
};
|
||||
|
||||
const SpvEntryPoint = struct {
|
||||
exec_model: spv.SpvExecutionModel,
|
||||
id: SpvWord,
|
||||
@@ -26,20 +30,16 @@ const SpvEntryPoint = struct {
|
||||
globals: []SpvWord,
|
||||
};
|
||||
|
||||
const SpvSource = struct {
|
||||
file_name: []const u8,
|
||||
lang: spv.SpvSourceLanguage,
|
||||
lang_version: SpvWord,
|
||||
source: []const u8,
|
||||
};
|
||||
|
||||
const ModuleError = error{
|
||||
pub const ModuleError = error{
|
||||
InvalidSpirV,
|
||||
InvalidMagic,
|
||||
UnsupportedEndianness,
|
||||
UnsupportedExtension,
|
||||
OutOfMemory,
|
||||
};
|
||||
|
||||
options: ModuleOptions,
|
||||
|
||||
it: WordIterator,
|
||||
|
||||
version_major: SpvByte,
|
||||
@@ -55,7 +55,6 @@ code: []const SpvWord,
|
||||
addressing: spv.SpvAddressingModel,
|
||||
memory_model: spv.SpvMemoryModel,
|
||||
|
||||
files: std.ArrayList(SpvSource),
|
||||
extensions: std.ArrayList([]const u8),
|
||||
|
||||
results: []Result,
|
||||
@@ -72,26 +71,26 @@ geometry_output_count: SpvWord,
|
||||
geometry_input: SpvWord,
|
||||
geometry_output: SpvWord,
|
||||
|
||||
input_locations: std.ArrayList(SpvWord),
|
||||
output_locations: std.ArrayList(SpvWord),
|
||||
bindings: std.AutoHashMap(SpvBinding, Value),
|
||||
input_locations: [lib.SPIRV_MAX_INPUT_LOCATIONS]SpvWord,
|
||||
output_locations: [lib.SPIRV_MAX_OUTPUT_LOCATIONS]SpvWord,
|
||||
bindings: [lib.SPIRV_MAX_SET][lib.SPIRV_MAX_SET_BINDINGS]SpvWord,
|
||||
builtins: std.EnumMap(spv.SpvBuiltIn, SpvWord),
|
||||
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, options: ModuleOptions) ModuleError!Self {
|
||||
var self: Self = std.mem.zeroInit(Self, .{
|
||||
.options = options,
|
||||
.code = allocator.dupe(SpvWord, source) catch return ModuleError.OutOfMemory,
|
||||
.files = std.ArrayList(SpvSource).empty,
|
||||
.extensions = std.ArrayList([]const u8).empty,
|
||||
.entry_points = std.ArrayList(SpvEntryPoint).empty,
|
||||
.capabilities = std.EnumSet(spv.SpvCapability).initEmpty(),
|
||||
.local_size_x = 1,
|
||||
.local_size_y = 1,
|
||||
.local_size_z = 1,
|
||||
.input_locations = std.ArrayList(SpvWord).empty,
|
||||
.output_locations = std.ArrayList(SpvWord).empty,
|
||||
.bindings = std.AutoHashMap(SpvBinding, Value).init(allocator),
|
||||
});
|
||||
errdefer self.deinit(allocator);
|
||||
errdefer allocator.free(self.code);
|
||||
|
||||
op.initRuntimeDispatcher();
|
||||
|
||||
self.it = WordIterator.init(self.code);
|
||||
|
||||
@@ -113,14 +112,21 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!S
|
||||
|
||||
self.bound = self.it.next() catch return ModuleError.InvalidSpirV;
|
||||
self.results = allocator.alloc(Result, self.bound) catch return ModuleError.OutOfMemory;
|
||||
errdefer allocator.free(self.results);
|
||||
|
||||
for (self.results) |*result| {
|
||||
result.* = Result.init();
|
||||
}
|
||||
errdefer {
|
||||
for (self.results) |*result| {
|
||||
result.deinit(allocator);
|
||||
}
|
||||
}
|
||||
|
||||
_ = self.it.skip(); // Skip schema
|
||||
|
||||
try self.pass(allocator); // Setup pass
|
||||
try self.populateMaps(allocator);
|
||||
try self.populateMaps();
|
||||
|
||||
if (std.process.hasEnvVarConstant("SPIRV_INTERPRETER_DEBUG_LOGS")) {
|
||||
var capability_set_names: std.ArrayList([]const u8) = .empty;
|
||||
@@ -159,10 +165,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 }) 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;
|
||||
}
|
||||
|
||||
@@ -196,30 +207,56 @@ fn pass(self: *Self, allocator: std.mem.Allocator) ModuleError!void {
|
||||
}
|
||||
}
|
||||
|
||||
fn populateMaps(self: *Self, allocator: std.mem.Allocator) ModuleError!void {
|
||||
fn populateMaps(self: *Self) 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) {
|
||||
.Output => for (result.decorations.items) |decoration| switch (decoration.rtype) {
|
||||
.Location => self.output_locations.append(allocator, @intCast(id)) catch return ModuleError.OutOfMemory,
|
||||
if (result.variant == null or std.meta.activeTag(result.variant.?) != .Variable)
|
||||
continue;
|
||||
|
||||
var set: ?usize = null;
|
||||
var binding: ?usize = null;
|
||||
|
||||
for (result.decorations.items) |decoration| {
|
||||
switch (result.variant.?.Variable.storage_class) {
|
||||
.Input => {
|
||||
switch (decoration.rtype) {
|
||||
.BuiltIn => self.builtins.put(
|
||||
std.enums.fromInt(spv.SpvBuiltIn, decoration.literal_1) orelse return ModuleError.InvalidSpirV,
|
||||
@intCast(id),
|
||||
),
|
||||
.Location => self.input_locations[decoration.literal_1] = @intCast(id),
|
||||
else => {},
|
||||
}
|
||||
},
|
||||
.Output => {
|
||||
if (decoration.rtype == .Location)
|
||||
self.output_locations[decoration.literal_1] = @intCast(id);
|
||||
},
|
||||
.StorageBuffer,
|
||||
.Uniform,
|
||||
.UniformConstant,
|
||||
=> {
|
||||
switch (decoration.rtype) {
|
||||
.Binding => binding = decoration.literal_1,
|
||||
.DescriptorSet => set = decoration.literal_1,
|
||||
else => {},
|
||||
}
|
||||
},
|
||||
else => {},
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
if (set != null and binding != null) {
|
||||
self.bindings[set.?][binding.?] = @intCast(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
|
||||
allocator.free(self.code);
|
||||
self.input_locations.deinit(allocator);
|
||||
self.output_locations.deinit(allocator);
|
||||
self.bindings.deinit();
|
||||
for (self.entry_points.items) |entry| {
|
||||
allocator.free(entry.name);
|
||||
allocator.free(entry.globals);
|
||||
}
|
||||
self.entry_points.deinit(allocator);
|
||||
self.files.deinit(allocator);
|
||||
|
||||
for (self.extensions.items) |ext| {
|
||||
allocator.free(ext);
|
||||
|
||||
447
src/Result.zig
447
src/Result.zig
@@ -1,13 +1,27 @@
|
||||
const std = @import("std");
|
||||
const spv = @import("spv.zig");
|
||||
const op = @import("opcodes.zig");
|
||||
|
||||
const RuntimeError = @import("Runtime.zig").RuntimeError;
|
||||
const Runtime = @import("Runtime.zig");
|
||||
const RuntimeError = Runtime.RuntimeError;
|
||||
|
||||
const SpvVoid = spv.SpvVoid;
|
||||
const SpvByte = spv.SpvByte;
|
||||
const SpvWord = spv.SpvWord;
|
||||
const SpvBool = spv.SpvBool;
|
||||
|
||||
pub const Vec4f32 = @Vector(4, f32);
|
||||
pub const Vec3f32 = @Vector(3, f32);
|
||||
pub const Vec2f32 = @Vector(2, f32);
|
||||
|
||||
pub const Vec4i32 = @Vector(4, i32);
|
||||
pub const Vec3i32 = @Vector(3, i32);
|
||||
pub const Vec2i32 = @Vector(2, i32);
|
||||
|
||||
pub const Vec4u32 = @Vector(4, u32);
|
||||
pub const Vec3u32 = @Vector(3, u32);
|
||||
pub const Vec2u32 = @Vector(2, u32);
|
||||
|
||||
pub const Variant = enum {
|
||||
String,
|
||||
Extension,
|
||||
@@ -26,6 +40,15 @@ pub const Type = enum {
|
||||
Int,
|
||||
Float,
|
||||
Vector,
|
||||
Vector4f32,
|
||||
Vector3f32,
|
||||
Vector2f32,
|
||||
Vector4i32,
|
||||
Vector3i32,
|
||||
Vector2i32,
|
||||
Vector4u32,
|
||||
Vector3u32,
|
||||
Vector2u32,
|
||||
Matrix,
|
||||
Array,
|
||||
RuntimeArray,
|
||||
@@ -55,40 +78,58 @@ const Decoration = struct {
|
||||
};
|
||||
|
||||
pub const Value = union(Type) {
|
||||
Void: noreturn,
|
||||
Void: struct {},
|
||||
Bool: bool,
|
||||
Int: extern union {
|
||||
sint8: i8,
|
||||
sint16: i16,
|
||||
sint32: i32,
|
||||
sint64: i64,
|
||||
uint8: u8,
|
||||
uint16: u16,
|
||||
uint32: u32,
|
||||
uint64: u64,
|
||||
Int: struct {
|
||||
bit_count: usize,
|
||||
value: extern union {
|
||||
sint8: i8,
|
||||
sint16: i16,
|
||||
sint32: i32,
|
||||
sint64: i64,
|
||||
uint8: u8,
|
||||
uint16: u16,
|
||||
uint32: u32,
|
||||
uint64: u64,
|
||||
},
|
||||
},
|
||||
Float: extern union {
|
||||
float16: f16,
|
||||
float32: f32,
|
||||
float64: f64,
|
||||
Float: struct {
|
||||
bit_count: usize,
|
||||
value: extern union {
|
||||
float16: f16,
|
||||
float32: f32,
|
||||
float64: f64,
|
||||
},
|
||||
},
|
||||
Vector: []Value,
|
||||
Vector4f32: Vec4f32,
|
||||
Vector3f32: Vec3f32,
|
||||
Vector2f32: Vec2f32,
|
||||
Vector4i32: Vec4i32,
|
||||
Vector3i32: Vec3i32,
|
||||
Vector2i32: Vec2i32,
|
||||
Vector4u32: Vec4u32,
|
||||
Vector3u32: Vec3u32,
|
||||
Vector2u32: Vec2u32,
|
||||
Matrix: []Value,
|
||||
Array: struct {},
|
||||
RuntimeArray: struct {},
|
||||
Array: []Value,
|
||||
RuntimeArray: ?[]Value,
|
||||
Structure: []Value,
|
||||
Function: noreturn,
|
||||
Image: struct {},
|
||||
Sampler: struct {},
|
||||
SampledImage: struct {},
|
||||
Pointer: noreturn,
|
||||
Pointer: union(enum) {
|
||||
common: *Value,
|
||||
f32_ptr: *f32,
|
||||
i32_ptr: *i32, //< For vector specializations
|
||||
u32_ptr: *u32,
|
||||
},
|
||||
|
||||
pub inline fn getCompositeDataOrNull(self: *const Value) ?[]Value {
|
||||
return switch (self.*) {
|
||||
.Vector => |v| v,
|
||||
.Matrix => |m| m,
|
||||
.Array => |_| unreachable,
|
||||
.Structure => |s| s,
|
||||
.Vector, .Matrix, .Array, .Structure => |v| v,
|
||||
.RuntimeArray => |v| v,
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
@@ -100,8 +141,14 @@ pub const Value = union(Type) {
|
||||
return switch (resolved.variant.?) {
|
||||
.Type => |t| switch (t) {
|
||||
.Bool => .{ .Bool = false },
|
||||
.Int => .{ .Int = .{ .uint64 = 0 } },
|
||||
.Float => .{ .Float = .{ .float64 = 0.0 } },
|
||||
.Int => |i| .{ .Int = .{
|
||||
.bit_count = i.bit_length,
|
||||
.value = .{ .uint64 = 0 },
|
||||
} },
|
||||
.Float => |f| .{ .Float = .{
|
||||
.bit_count = f.bit_length,
|
||||
.value = .{ .float64 = 0 },
|
||||
} },
|
||||
.Vector => |v| blk: {
|
||||
var self: Value = .{ .Vector = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer self.deinit(allocator);
|
||||
@@ -111,6 +158,15 @@ pub const Value = union(Type) {
|
||||
}
|
||||
break :blk self;
|
||||
},
|
||||
.Vector4f32 => .{ .Vector4f32 = Vec4f32{ 0.0, 0.0, 0.0, 0.0 } },
|
||||
.Vector3f32 => .{ .Vector3f32 = Vec3f32{ 0.0, 0.0, 0.0 } },
|
||||
.Vector2f32 => .{ .Vector2f32 = Vec2f32{ 0.0, 0.0 } },
|
||||
.Vector4i32 => .{ .Vector4i32 = Vec4i32{ 0, 0, 0, 0 } },
|
||||
.Vector3i32 => .{ .Vector3i32 = Vec3i32{ 0, 0, 0 } },
|
||||
.Vector2i32 => .{ .Vector2i32 = Vec2i32{ 0, 0 } },
|
||||
.Vector4u32 => .{ .Vector4u32 = Vec4u32{ 0, 0, 0, 0 } },
|
||||
.Vector3u32 => .{ .Vector3u32 = Vec3u32{ 0, 0, 0 } },
|
||||
.Vector2u32 => .{ .Vector2u32 = Vec2u32{ 0, 0 } },
|
||||
.Matrix => |m| blk: {
|
||||
var self: Value = .{ .Matrix = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer self.deinit(allocator);
|
||||
@@ -120,8 +176,14 @@ pub const Value = union(Type) {
|
||||
}
|
||||
break :blk self;
|
||||
},
|
||||
.Array => |_| {
|
||||
unreachable;
|
||||
.Array => |a| blk: {
|
||||
var self: Value = .{ .Array = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer self.deinit(allocator);
|
||||
|
||||
for (self.Array) |*value| {
|
||||
value.* = try Value.init(allocator, results, a.components_type_word);
|
||||
}
|
||||
break :blk self;
|
||||
},
|
||||
.Structure => |s| blk: {
|
||||
var self: Value = .{ .Structure = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
@@ -132,6 +194,7 @@ pub const Value = union(Type) {
|
||||
}
|
||||
break :blk self;
|
||||
},
|
||||
.RuntimeArray => .{ .RuntimeArray = null },
|
||||
else => unreachable,
|
||||
},
|
||||
else => unreachable,
|
||||
@@ -155,6 +218,24 @@ pub const Value = union(Type) {
|
||||
break :blk values;
|
||||
},
|
||||
},
|
||||
.Array => |a| .{
|
||||
.Array = blk: {
|
||||
const values = allocator.dupe(Value, a) catch return RuntimeError.OutOfMemory;
|
||||
for (values, a) |*new_value, value| new_value.* = try value.dupe(allocator);
|
||||
break :blk values;
|
||||
},
|
||||
},
|
||||
.RuntimeArray => |opt_a| .{
|
||||
.RuntimeArray = blk: {
|
||||
if (opt_a) |a| {
|
||||
const values = allocator.dupe(Value, a) catch return RuntimeError.OutOfMemory;
|
||||
for (values, a) |*new_value, value| new_value.* = try value.dupe(allocator);
|
||||
break :blk values;
|
||||
} else {
|
||||
break :blk null;
|
||||
}
|
||||
},
|
||||
},
|
||||
.Structure => |s| .{
|
||||
.Structure = blk: {
|
||||
const values = allocator.dupe(Value, s) catch return RuntimeError.OutOfMemory;
|
||||
@@ -168,15 +249,11 @@ pub const Value = union(Type) {
|
||||
|
||||
fn deinit(self: *Value, allocator: std.mem.Allocator) void {
|
||||
switch (self.*) {
|
||||
.Vector => |values| {
|
||||
.Vector, .Matrix, .Array, .Structure => |values| {
|
||||
for (values) |*value| value.deinit(allocator);
|
||||
allocator.free(values);
|
||||
},
|
||||
.Matrix => |values| {
|
||||
for (values) |*value| value.deinit(allocator);
|
||||
allocator.free(values);
|
||||
},
|
||||
.Structure => |values| {
|
||||
.RuntimeArray => |opt_values| if (opt_values) |values| {
|
||||
for (values) |*value| value.deinit(allocator);
|
||||
allocator.free(values);
|
||||
},
|
||||
@@ -185,82 +262,132 @@ pub const Value = union(Type) {
|
||||
}
|
||||
};
|
||||
|
||||
const Self = @This();
|
||||
|
||||
name: ?[]const u8,
|
||||
|
||||
decorations: std.ArrayList(Decoration),
|
||||
|
||||
parent: ?*const Self,
|
||||
|
||||
variant: ?union(Variant) {
|
||||
String: []const u8,
|
||||
Extension: struct {},
|
||||
Type: union(Type) {
|
||||
Void: struct {},
|
||||
Bool: struct {},
|
||||
Int: struct {
|
||||
bit_length: SpvWord,
|
||||
is_signed: bool,
|
||||
},
|
||||
Float: struct {
|
||||
bit_length: SpvWord,
|
||||
},
|
||||
Vector: struct {
|
||||
components_type_word: SpvWord,
|
||||
components_type: Type,
|
||||
member_count: SpvWord,
|
||||
},
|
||||
Matrix: struct {
|
||||
column_type_word: SpvWord,
|
||||
column_type: Type,
|
||||
member_count: SpvWord,
|
||||
},
|
||||
Array: struct {},
|
||||
RuntimeArray: struct {},
|
||||
Structure: struct {
|
||||
members_type_word: []const SpvWord,
|
||||
members: []Type,
|
||||
member_names: std.ArrayList([]const u8),
|
||||
},
|
||||
Function: struct {
|
||||
source_location: usize,
|
||||
return_type: SpvWord,
|
||||
params: []const SpvWord,
|
||||
},
|
||||
Image: struct {},
|
||||
Sampler: struct {},
|
||||
SampledImage: struct {},
|
||||
Pointer: struct {
|
||||
storage_class: spv.SpvStorageClass,
|
||||
target: SpvWord,
|
||||
},
|
||||
pub const TypeData = union(Type) {
|
||||
Void: struct {},
|
||||
Bool: struct {},
|
||||
Int: struct {
|
||||
bit_length: SpvWord,
|
||||
is_signed: bool,
|
||||
},
|
||||
Float: struct {
|
||||
bit_length: SpvWord,
|
||||
},
|
||||
Vector: struct {
|
||||
components_type_word: SpvWord,
|
||||
components_type: Type,
|
||||
member_count: SpvWord,
|
||||
},
|
||||
Vector4f32: struct {},
|
||||
Vector3f32: struct {},
|
||||
Vector2f32: struct {},
|
||||
Vector4i32: struct {},
|
||||
Vector3i32: struct {},
|
||||
Vector2i32: struct {},
|
||||
Vector4u32: struct {},
|
||||
Vector3u32: struct {},
|
||||
Vector2u32: struct {},
|
||||
Matrix: struct {
|
||||
column_type_word: SpvWord,
|
||||
column_type: Type,
|
||||
member_count: SpvWord,
|
||||
},
|
||||
Array: struct {
|
||||
components_type_word: SpvWord,
|
||||
components_type: Type,
|
||||
member_count: SpvWord,
|
||||
},
|
||||
RuntimeArray: struct {
|
||||
components_type_word: SpvWord,
|
||||
components_type: Type,
|
||||
},
|
||||
Structure: struct {
|
||||
members_type_word: []const SpvWord,
|
||||
member_names: std.ArrayList([]const u8),
|
||||
},
|
||||
Function: struct {
|
||||
source_location: usize,
|
||||
return_type: SpvWord,
|
||||
params: []const SpvWord,
|
||||
},
|
||||
Image: struct {},
|
||||
Sampler: struct {},
|
||||
SampledImage: struct {},
|
||||
Pointer: struct {
|
||||
storage_class: spv.SpvStorageClass,
|
||||
target: SpvWord,
|
||||
},
|
||||
|
||||
pub fn getSize(self: *const TypeData, results: []const Self) usize {
|
||||
return switch (self.*) {
|
||||
.Bool => 1,
|
||||
.Int => |i| @divExact(i.bit_length, 8),
|
||||
.Float => |f| @divExact(f.bit_length, 8),
|
||||
.Vector => |v| results[v.components_type_word].variant.?.Type.getSize(results),
|
||||
.Array => |a| results[a.components_type_word].variant.?.Type.getSize(results),
|
||||
.Matrix => |m| results[m.column_type_word].variant.?.Type.getSize(results),
|
||||
.RuntimeArray => |a| results[a.components_type_word].variant.?.Type.getSize(results),
|
||||
.Structure => |s| blk: {
|
||||
var total: usize = 0;
|
||||
for (s.members_type_word) |type_word| {
|
||||
total += results[type_word].variant.?.Type.getSize(results);
|
||||
}
|
||||
break :blk total;
|
||||
},
|
||||
.Vector4f32, .Vector4i32, .Vector4u32 => 4 * 4,
|
||||
.Vector3f32, .Vector3i32, .Vector3u32 => 3 * 4,
|
||||
.Vector2f32, .Vector2i32, .Vector2u32 => 2 * 4,
|
||||
else => 0,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub const VariantData = union(Variant) {
|
||||
String: []const u8,
|
||||
Extension: struct {
|
||||
/// Should not be allocated but rather a pointer to a static array
|
||||
dispatcher: []?op.OpCodeExtFunc,
|
||||
},
|
||||
Type: TypeData,
|
||||
Variable: struct {
|
||||
storage_class: spv.SpvStorageClass,
|
||||
type_word: SpvWord,
|
||||
type: Type,
|
||||
value: Value,
|
||||
},
|
||||
Constant: struct {
|
||||
type_word: SpvWord,
|
||||
type: Type,
|
||||
value: Value,
|
||||
},
|
||||
Constant: Value,
|
||||
Function: struct {
|
||||
source_location: usize,
|
||||
return_type: SpvWord,
|
||||
function_type: SpvWord,
|
||||
params: []const SpvWord,
|
||||
params: []SpvWord,
|
||||
},
|
||||
AccessChain: struct {
|
||||
target: SpvWord,
|
||||
value: Value,
|
||||
},
|
||||
FunctionParameter: struct {},
|
||||
FunctionParameter: struct {
|
||||
type_word: SpvWord,
|
||||
type: Type,
|
||||
value_ptr: ?*Value,
|
||||
},
|
||||
Label: struct {
|
||||
source_location: usize,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const Self = @This();
|
||||
|
||||
name: ?[]const u8,
|
||||
decorations: std.ArrayList(Decoration),
|
||||
variant: ?VariantData,
|
||||
|
||||
pub fn init() Self {
|
||||
return .{
|
||||
.name = null,
|
||||
.parent = null,
|
||||
.decorations = .empty,
|
||||
.variant = null,
|
||||
};
|
||||
@@ -276,7 +403,6 @@ pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
|
||||
.Function => |data| allocator.free(data.params),
|
||||
.Structure => |*data| {
|
||||
allocator.free(data.members_type_word);
|
||||
allocator.free(data.members);
|
||||
for (data.member_names.items) |name| {
|
||||
allocator.free(name);
|
||||
}
|
||||
@@ -284,21 +410,67 @@ pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
|
||||
},
|
||||
else => {},
|
||||
},
|
||||
.Constant => |*v| v.deinit(allocator),
|
||||
.Constant => |*c| c.value.deinit(allocator),
|
||||
.Variable => |*v| v.value.deinit(allocator),
|
||||
//.AccessChain => |*a| a.value.deinit(allocator),
|
||||
.Function => |f| allocator.free(f.params),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
self.decorations.deinit(allocator);
|
||||
}
|
||||
|
||||
pub inline fn getValueTypeWord(self: *Self) RuntimeError!SpvWord {
|
||||
return switch ((try self.getVariant()).*) {
|
||||
.Variable => |v| v.type_word,
|
||||
.Constant => |c| c.type_word,
|
||||
.AccessChain => |a| a.target,
|
||||
.FunctionParameter => |p| p.type_word,
|
||||
else => RuntimeError.InvalidSpirV,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn getValueType(self: *Self) RuntimeError!Type {
|
||||
return switch ((try self.getVariant()).*) {
|
||||
.Variable => |v| v.type,
|
||||
.Constant => |c| c.type,
|
||||
.FunctionParameter => |p| p.type,
|
||||
else => RuntimeError.InvalidSpirV,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn getValue(self: *Self) RuntimeError!*Value {
|
||||
return switch ((try self.getVariant()).*) {
|
||||
.Variable => |*v| &v.value,
|
||||
.Constant => |*c| &c.value,
|
||||
.AccessChain => |*a| &a.value,
|
||||
.FunctionParameter => |*p| p.value_ptr orelse return RuntimeError.InvalidSpirV,
|
||||
else => RuntimeError.InvalidSpirV,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn getConstValue(self: *Self) RuntimeError!*const Value {
|
||||
return switch ((try self.getVariant()).*) {
|
||||
.Variable => |v| &v.value,
|
||||
.Constant => |c| &c.value,
|
||||
.AccessChain => |a| &a.value,
|
||||
.FunctionParameter => |p| p.value_ptr orelse return RuntimeError.InvalidSpirV,
|
||||
else => RuntimeError.InvalidSpirV,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn getVariant(self: *Self) RuntimeError!*VariantData {
|
||||
return &(self.variant orelse return RuntimeError.InvalidSpirV);
|
||||
}
|
||||
|
||||
pub inline fn getConstVariant(self: *const Self) RuntimeError!*const VariantData {
|
||||
return &(self.variant orelse return RuntimeError.InvalidSpirV);
|
||||
}
|
||||
|
||||
/// Performs a deep copy
|
||||
pub fn dupe(self: *const Self, allocator: std.mem.Allocator) RuntimeError!Self {
|
||||
return .{
|
||||
.name = if (self.name) |name| allocator.dupe(u8, name) catch return RuntimeError.OutOfMemory else null,
|
||||
.decorations = self.decorations.clone(allocator) catch return RuntimeError.OutOfMemory,
|
||||
.parent = self.parent,
|
||||
.variant = blk: {
|
||||
if (self.variant) |variant| {
|
||||
switch (variant) {
|
||||
@@ -310,7 +482,6 @@ pub fn dupe(self: *const Self, allocator: std.mem.Allocator) RuntimeError!Self {
|
||||
.Type = .{
|
||||
.Structure = .{
|
||||
.members_type_word = allocator.dupe(SpvWord, s.members_type_word) catch return RuntimeError.OutOfMemory,
|
||||
.members = allocator.dupe(Type, s.members) catch return RuntimeError.OutOfMemory,
|
||||
.member_names = blk2: {
|
||||
const member_names = s.member_names.clone(allocator) catch return RuntimeError.OutOfMemory;
|
||||
for (member_names.items, s.member_names.items) |*new_name, name| {
|
||||
@@ -330,15 +501,23 @@ pub fn dupe(self: *const Self, allocator: std.mem.Allocator) RuntimeError!Self {
|
||||
},
|
||||
},
|
||||
},
|
||||
else => {},
|
||||
else => break :blk .{ .Type = t },
|
||||
},
|
||||
.Variable => |v| break :blk .{
|
||||
.Variable = .{
|
||||
.storage_class = v.storage_class,
|
||||
.type_word = v.type_word,
|
||||
.type = v.type,
|
||||
.value = try v.value.dupe(allocator),
|
||||
},
|
||||
},
|
||||
.Constant => |c| break :blk .{ .Constant = try c.dupe(allocator) },
|
||||
.Constant => |c| break :blk .{
|
||||
.Constant = .{
|
||||
.type_word = c.type_word,
|
||||
.type = c.type,
|
||||
.value = try c.value.dupe(allocator),
|
||||
},
|
||||
},
|
||||
.Function => |f| break :blk .{
|
||||
.Function = .{
|
||||
.source_location = f.source_location,
|
||||
@@ -355,6 +534,40 @@ pub fn dupe(self: *const Self, allocator: std.mem.Allocator) RuntimeError!Self {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn resolveLaneBitWidth(target_type: TypeData, rt: *const Runtime) RuntimeError!SpvWord {
|
||||
return sw: switch (target_type) {
|
||||
.Bool => 8,
|
||||
.Float => |f| f.bit_length,
|
||||
.Int => |i| i.bit_length,
|
||||
.Vector => |v| continue :sw (try rt.results[v.components_type_word].getVariant()).Type,
|
||||
.Vector4f32,
|
||||
.Vector3f32,
|
||||
.Vector2f32,
|
||||
.Vector4i32,
|
||||
.Vector3i32,
|
||||
.Vector2i32,
|
||||
.Vector4u32,
|
||||
.Vector3u32,
|
||||
.Vector2u32,
|
||||
=> return 32,
|
||||
else => return RuntimeError.InvalidSpirV,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn resolveSign(target_type: TypeData, rt: *const Runtime) RuntimeError!enum { signed, unsigned } {
|
||||
return sw: switch (target_type) {
|
||||
.Int => |i| if (i.is_signed) .signed else .unsigned,
|
||||
.Vector => |v| continue :sw (try rt.results[v.components_type_word].getVariant()).Type,
|
||||
.Vector4i32 => .signed,
|
||||
.Vector3i32 => .signed,
|
||||
.Vector2i32 => .signed,
|
||||
.Vector4u32 => .unsigned,
|
||||
.Vector3u32 => .unsigned,
|
||||
.Vector2u32 => .unsigned,
|
||||
else => .unsigned,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn resolveType(self: *const Self, results: []const Self) *const Self {
|
||||
return if (self.variant) |variant|
|
||||
switch (variant) {
|
||||
@@ -374,9 +587,13 @@ pub fn getMemberCounts(self: *const Self) usize {
|
||||
.Type => |t| switch (t) {
|
||||
.Bool, .Int, .Float, .Image, .Sampler => return 1,
|
||||
.Vector => |v| return v.member_count,
|
||||
.Vector4f32, .Vector4i32, .Vector4u32 => return 4,
|
||||
.Vector3f32, .Vector3i32, .Vector3u32 => return 3,
|
||||
.Vector2f32, .Vector2i32, .Vector2u32 => return 2,
|
||||
.Matrix => |m| return m.member_count,
|
||||
.Array => |a| return a.member_count,
|
||||
.SampledImage => return 2,
|
||||
.Structure => |s| return s.members.len,
|
||||
.Structure => |s| return s.members_type_word.len,
|
||||
.Function => |f| return f.params.len,
|
||||
else => {},
|
||||
},
|
||||
@@ -389,9 +606,16 @@ pub fn getMemberCounts(self: *const Self) usize {
|
||||
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) {
|
||||
.Void => .{ .Void = .{} },
|
||||
.Bool => .{ .Bool = false },
|
||||
.Int => .{ .Int = .{ .uint64 = 0 } },
|
||||
.Float => .{ .Float = .{ .float64 = 0.0 } },
|
||||
.Int => |i| .{ .Int = .{
|
||||
.bit_count = i.bit_length,
|
||||
.value = .{ .uint64 = 0 },
|
||||
} },
|
||||
.Float => |f| .{ .Float = .{
|
||||
.bit_count = f.bit_length,
|
||||
.value = .{ .float64 = 0 },
|
||||
} },
|
||||
.Vector => |v| blk: {
|
||||
const value: Value = .{ .Vector = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer allocator.free(value.Vector);
|
||||
@@ -400,6 +624,15 @@ pub fn initValue(allocator: std.mem.Allocator, member_count: usize, results: []c
|
||||
}
|
||||
break :blk value;
|
||||
},
|
||||
.Vector4f32 => .{ .Vector4f32 = Vec4f32{ 0.0, 0.0, 0.0, 0.0 } },
|
||||
.Vector3f32 => .{ .Vector3f32 = Vec3f32{ 0.0, 0.0, 0.0 } },
|
||||
.Vector2f32 => .{ .Vector2f32 = Vec2f32{ 0.0, 0.0 } },
|
||||
.Vector4i32 => .{ .Vector4i32 = Vec4i32{ 0, 0, 0, 0 } },
|
||||
.Vector3i32 => .{ .Vector3i32 = Vec3i32{ 0, 0, 0 } },
|
||||
.Vector2i32 => .{ .Vector2i32 = Vec2i32{ 0, 0 } },
|
||||
.Vector4u32 => .{ .Vector4u32 = Vec4u32{ 0, 0, 0, 0 } },
|
||||
.Vector3u32 => .{ .Vector3u32 = Vec3u32{ 0, 0, 0 } },
|
||||
.Vector2u32 => .{ .Vector2u32 = Vec2u32{ 0, 0 } },
|
||||
.Matrix => |m| blk: {
|
||||
const value: Value = .{ .Matrix = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer allocator.free(value.Matrix);
|
||||
@@ -408,7 +641,25 @@ pub fn initValue(allocator: std.mem.Allocator, member_count: usize, results: []c
|
||||
}
|
||||
break :blk value;
|
||||
},
|
||||
.Array => |_| RuntimeError.ToDo,
|
||||
.Array => |a| blk: {
|
||||
const value: Value = .{ .Array = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer allocator.free(value.Array);
|
||||
for (value.Array) |*val| {
|
||||
val.* = try Value.init(allocator, results, a.components_type_word);
|
||||
}
|
||||
break :blk value;
|
||||
},
|
||||
.RuntimeArray => |a| blk: {
|
||||
if (member_count == 0) {
|
||||
break :blk Value{ .RuntimeArray = null };
|
||||
}
|
||||
const value: Value = .{ .RuntimeArray = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer allocator.free(value.RuntimeArray.?);
|
||||
for (value.RuntimeArray.?) |*val| {
|
||||
val.* = try Value.init(allocator, results, a.components_type_word);
|
||||
}
|
||||
break :blk value;
|
||||
},
|
||||
.Structure => |s| blk: {
|
||||
const value: Value = .{ .Structure = allocator.alloc(Value, member_count) catch return RuntimeError.OutOfMemory };
|
||||
errdefer allocator.free(value.Structure);
|
||||
|
||||
343
src/Runtime.zig
343
src/Runtime.zig
@@ -1,6 +1,9 @@
|
||||
//! A runtime meant for actual shader invocations.
|
||||
|
||||
const std = @import("std");
|
||||
const spv = @import("spv.zig");
|
||||
const op = @import("opcodes.zig");
|
||||
const lib = @import("lib.zig");
|
||||
|
||||
const SpvVoid = spv.SpvVoid;
|
||||
const SpvByte = spv.SpvByte;
|
||||
@@ -14,18 +17,24 @@ const WordIterator = @import("WordIterator.zig");
|
||||
const Self = @This();
|
||||
|
||||
pub const RuntimeError = error{
|
||||
InvalidSpirV,
|
||||
UnsupportedSpirV,
|
||||
OutOfMemory,
|
||||
Unreachable,
|
||||
Killed,
|
||||
DivisionByZero,
|
||||
InvalidEntryPoint,
|
||||
InvalidSpirV,
|
||||
InvalidValueType,
|
||||
Killed,
|
||||
NotFound,
|
||||
OutOfMemory,
|
||||
OutOfBounds,
|
||||
ToDo,
|
||||
Unreachable,
|
||||
UnsupportedSpirV,
|
||||
UnsupportedExtension,
|
||||
};
|
||||
|
||||
pub const Function = struct {
|
||||
source_location: usize,
|
||||
result: *Result,
|
||||
ret: *Result,
|
||||
};
|
||||
|
||||
mod: *Module,
|
||||
@@ -34,6 +43,7 @@ it: WordIterator,
|
||||
/// Local deep copy of module's results to be able to run multiple runtimes concurrently
|
||||
results: []Result,
|
||||
|
||||
current_parameter_index: SpvWord,
|
||||
current_function: ?*Result,
|
||||
function_stack: std.ArrayList(Function),
|
||||
|
||||
@@ -48,6 +58,7 @@ pub fn init(allocator: std.mem.Allocator, module: *Module) RuntimeError!Self {
|
||||
}
|
||||
break :blk results;
|
||||
},
|
||||
.current_parameter_index = 0,
|
||||
.current_function = null,
|
||||
.function_stack = .empty,
|
||||
};
|
||||
@@ -102,6 +113,11 @@ pub fn callEntryPoint(self: *Self, allocator: std.mem.Allocator, entry_point_ind
|
||||
switch (variant) {
|
||||
.Function => |f| {
|
||||
if (!self.it.jumpToSourceLocation(f.source_location)) return RuntimeError.InvalidEntryPoint;
|
||||
self.function_stack.append(allocator, .{
|
||||
.source_location = f.source_location,
|
||||
.result = entry_point_result,
|
||||
.ret = &self.results[f.return_type],
|
||||
}) catch return RuntimeError.OutOfMemory;
|
||||
},
|
||||
else => return RuntimeError.InvalidEntryPoint,
|
||||
}
|
||||
@@ -110,18 +126,21 @@ pub fn callEntryPoint(self: *Self, allocator: std.mem.Allocator, entry_point_ind
|
||||
}
|
||||
}
|
||||
|
||||
self.it.did_jump = false; // To reset function jump
|
||||
while (self.it.nextOrNull()) |opcode_data| {
|
||||
const word_count = ((opcode_data & (~spv.SpvOpCodeMask)) >> spv.SpvWordCountShift) - 1;
|
||||
const opcode = (opcode_data & spv.SpvOpCodeMask);
|
||||
|
||||
var it_tmp = self.it; // Save because operations may iter on this iterator
|
||||
if (std.enums.fromInt(spv.SpvOp, opcode)) |spv_op| {
|
||||
if (op.RuntimeDispatcher.get(spv_op)) |pfn| {
|
||||
try pfn(allocator, word_count, self);
|
||||
}
|
||||
if (op.runtime_dispatcher[opcode]) |pfn| {
|
||||
try pfn(allocator, word_count, self);
|
||||
}
|
||||
if (!self.it.did_jump) {
|
||||
_ = it_tmp.skipN(word_count);
|
||||
self.it = it_tmp;
|
||||
} else {
|
||||
self.it.did_jump = false;
|
||||
}
|
||||
_ = it_tmp.skipN(word_count);
|
||||
self.it = it_tmp;
|
||||
}
|
||||
|
||||
//@import("pretty").print(allocator, self.results, .{
|
||||
@@ -132,11 +151,69 @@ 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 {
|
||||
if (std.mem.indexOf(SpvWord, self.mod.output_locations.items, &.{result})) |_| {
|
||||
self.readValue(T, output, &self.results[result].variant.?.Variable.value);
|
||||
pub fn readDescriptorSet(self: *const Self, output: []u8, set: SpvWord, binding: SpvWord) RuntimeError!void {
|
||||
if (set < lib.SPIRV_MAX_SET and binding < lib.SPIRV_MAX_SET_BINDINGS) {
|
||||
_ = try self.readValue(output, &self.results[self.mod.bindings[set][binding]].variant.?.Variable.value);
|
||||
} else {
|
||||
return error.NotFound;
|
||||
return RuntimeError.NotFound;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn writeDescriptorSet(self: *const Self, allocator: std.mem.Allocator, input: []const u8, set: SpvWord, binding: SpvWord) RuntimeError!void {
|
||||
if (set < lib.SPIRV_MAX_SET and binding < lib.SPIRV_MAX_SET_BINDINGS) {
|
||||
const variable = &self.results[self.mod.bindings[set][binding]].variant.?.Variable;
|
||||
|
||||
const helper = struct {
|
||||
fn init(allocator2: std.mem.Allocator, len: usize, value: *Result.Value, type_word: SpvWord, results: []Result) RuntimeError!void {
|
||||
const resolved = results[type_word].resolveType(results);
|
||||
|
||||
switch (value.*) {
|
||||
.RuntimeArray => |a| if (a == null) {
|
||||
const elem_size = resolved.variant.?.Type.getSize(results);
|
||||
value.* = try Result.initValue(allocator2, std.math.divCeil(usize, len, elem_size) catch unreachable, results, resolved);
|
||||
},
|
||||
.Structure => |*s| for (s.*, 0..) |*elem, i| {
|
||||
try @This().init(allocator2, len, elem, resolved.variant.?.Type.Structure.members_type_word[i], results);
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
};
|
||||
try helper.init(allocator, input.len, &variable.value, variable.type_word, self.results);
|
||||
|
||||
//@import("pretty").print(allocator, variable, .{
|
||||
// .tab_size = 4,
|
||||
// .max_depth = 0,
|
||||
// .struct_max_len = 0,
|
||||
// .array_max_len = 0,
|
||||
//}) catch return RuntimeError.OutOfMemory;
|
||||
_ = try self.writeValue(input, &variable.value);
|
||||
} else {
|
||||
return RuntimeError.NotFound;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn readOutput(self: *const Self, output: []u8, result: SpvWord) RuntimeError!void {
|
||||
if (std.mem.indexOfScalar(SpvWord, &self.mod.output_locations, result)) |_| {
|
||||
_ = try self.readValue(output, &self.results[result].variant.?.Variable.value);
|
||||
} else {
|
||||
return RuntimeError.NotFound;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn writeInput(self: *const Self, input: []const u8, result: SpvWord) RuntimeError!void {
|
||||
if (std.mem.indexOfScalar(SpvWord, &self.mod.input_locations, result)) |_| {
|
||||
_ = try self.writeValue(input, &self.results[result].variant.?.Variable.value);
|
||||
} else {
|
||||
return RuntimeError.NotFound;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn writeBuiltIn(self: *const Self, input: []const u8, builtin: spv.SpvBuiltIn) RuntimeError!void {
|
||||
if (self.mod.builtins.get(builtin)) |result| {
|
||||
_ = try self.writeValue(input, &self.results[result].variant.?.Variable.value);
|
||||
} else {
|
||||
return RuntimeError.NotFound;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,40 +222,224 @@ 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, output: []u8, value: *const Result.Value) RuntimeError!usize {
|
||||
switch (value.*) {
|
||||
.Bool => |b| {
|
||||
if (T == bool) {
|
||||
output[0] = b;
|
||||
} else {
|
||||
unreachable;
|
||||
}
|
||||
output[0] = if (b == true) 1 else 0;
|
||||
return 1;
|
||||
},
|
||||
.Int => |i| {
|
||||
switch (T) {
|
||||
i8 => output[0] = i.sint8,
|
||||
i16 => output[0] = i.sint16,
|
||||
i32 => output[0] = i.sint32,
|
||||
i64 => output[0] = i.sint64,
|
||||
u8 => output[0] = i.uint8,
|
||||
u16 => output[0] = i.uint16,
|
||||
u32 => output[0] = i.uint32,
|
||||
u64 => output[0] = i.uint64,
|
||||
inline else => unreachable,
|
||||
switch (i.bit_count) {
|
||||
8 => output[0] = @bitCast(i.value.uint8),
|
||||
16 => std.mem.copyForwards(u8, output[0..], std.mem.asBytes(&i.value.uint16)),
|
||||
32 => std.mem.copyForwards(u8, output[0..], std.mem.asBytes(&i.value.uint32)),
|
||||
64 => std.mem.copyForwards(u8, output[0..], std.mem.asBytes(&i.value.uint64)),
|
||||
else => return RuntimeError.InvalidValueType,
|
||||
}
|
||||
return @divExact(i.bit_count, 8);
|
||||
},
|
||||
.Float => |f| {
|
||||
switch (T) {
|
||||
f16 => output[0] = f.float16,
|
||||
f32 => output[0] = f.float32,
|
||||
f64 => output[0] = f.float64,
|
||||
inline else => unreachable,
|
||||
switch (f.bit_count) {
|
||||
16 => std.mem.copyForwards(u8, output[0..], std.mem.asBytes(&f.value.float16)),
|
||||
32 => std.mem.copyForwards(u8, output[0..], std.mem.asBytes(&f.value.float32)),
|
||||
64 => std.mem.copyForwards(u8, output[0..], std.mem.asBytes(&f.value.float64)),
|
||||
else => return RuntimeError.InvalidValueType,
|
||||
}
|
||||
return @divExact(f.bit_count, 8);
|
||||
},
|
||||
.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,
|
||||
.Vector4f32 => |vec| {
|
||||
inline for (0..4) |i| {
|
||||
std.mem.copyForwards(u8, output[(i * 4)..], std.mem.asBytes(&vec[i]));
|
||||
}
|
||||
return 4 * 4;
|
||||
},
|
||||
.Vector3f32 => |vec| {
|
||||
inline for (0..3) |i| {
|
||||
std.mem.copyForwards(u8, output[(i * 4)..], std.mem.asBytes(&vec[i]));
|
||||
}
|
||||
return 3 * 4;
|
||||
},
|
||||
.Vector2f32 => |vec| {
|
||||
inline for (0..2) |i| {
|
||||
std.mem.copyForwards(u8, output[(i * 4)..], std.mem.asBytes(&vec[i]));
|
||||
}
|
||||
return 2 * 4;
|
||||
},
|
||||
.Vector4i32 => |vec| {
|
||||
inline for (0..4) |i| {
|
||||
std.mem.copyForwards(u8, output[(i * 4)..], std.mem.asBytes(&vec[i]));
|
||||
}
|
||||
return 4 * 4;
|
||||
},
|
||||
.Vector3i32 => |vec| {
|
||||
inline for (0..3) |i| {
|
||||
std.mem.copyForwards(u8, output[(i * 4)..], std.mem.asBytes(&vec[i]));
|
||||
}
|
||||
return 3 * 4;
|
||||
},
|
||||
.Vector2i32 => |vec| {
|
||||
inline for (0..2) |i| {
|
||||
std.mem.copyForwards(u8, output[(i * 4)..], std.mem.asBytes(&vec[i]));
|
||||
}
|
||||
return 2 * 4;
|
||||
},
|
||||
.Vector4u32 => |vec| {
|
||||
inline for (0..4) |i| {
|
||||
std.mem.copyForwards(u8, output[(i * 4)..], std.mem.asBytes(&vec[i]));
|
||||
}
|
||||
return 4 * 4;
|
||||
},
|
||||
.Vector3u32 => |vec| {
|
||||
inline for (0..3) |i| {
|
||||
std.mem.copyForwards(u8, output[(i * 4)..], std.mem.asBytes(&vec[i]));
|
||||
}
|
||||
return 3 * 4;
|
||||
},
|
||||
.Vector2u32 => |vec| {
|
||||
inline for (0..2) |i| {
|
||||
std.mem.copyForwards(u8, output[(i * 4)..], std.mem.asBytes(&vec[i]));
|
||||
}
|
||||
return 2 * 4;
|
||||
},
|
||||
.Vector,
|
||||
.Matrix,
|
||||
.Array,
|
||||
.Structure,
|
||||
=> |values| {
|
||||
var offset: usize = 0;
|
||||
for (values) |v| {
|
||||
offset += try self.readValue(output[offset..], &v);
|
||||
}
|
||||
return offset;
|
||||
},
|
||||
.RuntimeArray => |opt_values| if (opt_values) |values| {
|
||||
var offset: usize = 0;
|
||||
for (values) |v| {
|
||||
offset += try self.readValue(output[offset..], &v);
|
||||
}
|
||||
return offset;
|
||||
},
|
||||
else => return RuntimeError.InvalidValueType,
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
fn writeValue(self: *const Self, input: []const u8, value: *Result.Value) RuntimeError!usize {
|
||||
switch (value.*) {
|
||||
.Bool => |*b| {
|
||||
b.* = if (input[0] != 0) true else false;
|
||||
return 1;
|
||||
},
|
||||
.Int => |*i| {
|
||||
switch (i.bit_count) {
|
||||
8 => i.value.uint8 = @bitCast(input[0]),
|
||||
16 => std.mem.copyForwards(u8, std.mem.asBytes(&i.value.uint16), input[0..2]),
|
||||
32 => std.mem.copyForwards(u8, std.mem.asBytes(&i.value.uint32), input[0..4]),
|
||||
64 => std.mem.copyForwards(u8, std.mem.asBytes(&i.value.uint64), input[0..8]),
|
||||
else => return RuntimeError.InvalidValueType,
|
||||
}
|
||||
return @divExact(i.bit_count, 8);
|
||||
},
|
||||
.Float => |*f| {
|
||||
switch (f.bit_count) {
|
||||
16 => std.mem.copyForwards(u8, std.mem.asBytes(&f.value.float16), input[0..2]),
|
||||
32 => std.mem.copyForwards(u8, std.mem.asBytes(&f.value.float32), input[0..4]),
|
||||
64 => std.mem.copyForwards(u8, std.mem.asBytes(&f.value.float64), input[0..8]),
|
||||
else => return RuntimeError.InvalidValueType,
|
||||
}
|
||||
return @divExact(f.bit_count, 8);
|
||||
},
|
||||
.Vector4f32 => |*vec| {
|
||||
inline for (0..4) |i| {
|
||||
const start = i * 4;
|
||||
const end = (i + 1) * 4;
|
||||
std.mem.copyForwards(u8, std.mem.asBytes(&vec[i]), input[start..end]);
|
||||
}
|
||||
return 4 * 4;
|
||||
},
|
||||
.Vector3f32 => |*vec| {
|
||||
inline for (0..3) |i| {
|
||||
const start = i * 4;
|
||||
const end = (i + 1) * 4;
|
||||
std.mem.copyForwards(u8, std.mem.asBytes(&vec[i]), input[start..end]);
|
||||
}
|
||||
return 3 * 4;
|
||||
},
|
||||
.Vector2f32 => |*vec| {
|
||||
inline for (0..2) |i| {
|
||||
const start = i * 4;
|
||||
const end = (i + 1) * 4;
|
||||
std.mem.copyForwards(u8, std.mem.asBytes(&vec[i]), input[start..end]);
|
||||
}
|
||||
return 2 * 4;
|
||||
},
|
||||
.Vector4i32 => |*vec| {
|
||||
inline for (0..4) |i| {
|
||||
const start = i * 4;
|
||||
const end = (i + 1) * 4;
|
||||
std.mem.copyForwards(u8, std.mem.asBytes(&vec[i]), input[start..end]);
|
||||
}
|
||||
return 4 * 4;
|
||||
},
|
||||
.Vector3i32 => |*vec| {
|
||||
inline for (0..3) |i| {
|
||||
const start = i * 4;
|
||||
const end = (i + 1) * 4;
|
||||
std.mem.copyForwards(u8, std.mem.asBytes(&vec[i]), input[start..end]);
|
||||
}
|
||||
return 3 * 4;
|
||||
},
|
||||
.Vector2i32 => |*vec| {
|
||||
inline for (0..2) |i| {
|
||||
const start = i * 4;
|
||||
const end = (i + 1) * 4;
|
||||
std.mem.copyForwards(u8, std.mem.asBytes(&vec[i]), input[start..end]);
|
||||
}
|
||||
return 2 * 4;
|
||||
},
|
||||
.Vector4u32 => |*vec| {
|
||||
inline for (0..4) |i| {
|
||||
const start = i * 4;
|
||||
const end = (i + 1) * 4;
|
||||
std.mem.copyForwards(u8, std.mem.asBytes(&vec[i]), input[start..end]);
|
||||
}
|
||||
return 4 * 4;
|
||||
},
|
||||
.Vector3u32 => |*vec| {
|
||||
inline for (0..3) |i| {
|
||||
const start = i * 4;
|
||||
const end = (i + 1) * 4;
|
||||
std.mem.copyForwards(u8, std.mem.asBytes(&vec[i]), input[start..end]);
|
||||
}
|
||||
return 3 * 4;
|
||||
},
|
||||
.Vector2u32 => |*vec| {
|
||||
inline for (0..2) |i| {
|
||||
const start = i * 4;
|
||||
const end = (i + 1) * 4;
|
||||
std.mem.copyForwards(u8, std.mem.asBytes(&vec[i]), input[start..end]);
|
||||
}
|
||||
return 2 * 4;
|
||||
},
|
||||
.Vector,
|
||||
.Matrix,
|
||||
.Array,
|
||||
.Structure,
|
||||
=> |*values| {
|
||||
var offset: usize = 0;
|
||||
for (values.*) |*v| {
|
||||
offset += try self.writeValue(input[offset..], v);
|
||||
}
|
||||
return offset;
|
||||
},
|
||||
.RuntimeArray => |opt_values| if (opt_values) |*values| {
|
||||
var offset: usize = 0;
|
||||
for (values.*) |*v| {
|
||||
offset += try self.writeValue(input[offset..], v);
|
||||
}
|
||||
return offset;
|
||||
},
|
||||
else => return RuntimeError.InvalidValueType,
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -9,15 +9,17 @@ const Self = @This();
|
||||
|
||||
buffer: []const SpvWord,
|
||||
index: usize,
|
||||
did_jump: bool,
|
||||
|
||||
pub fn init(buffer: []const SpvWord) Self {
|
||||
return .{
|
||||
.buffer = buffer,
|
||||
.index = 0,
|
||||
.did_jump = false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn nextOrNull(self: *Self) ?SpvWord {
|
||||
pub inline fn nextOrNull(self: *Self) ?SpvWord {
|
||||
const word = self.peek() orelse return null;
|
||||
self.index += 1;
|
||||
return word;
|
||||
@@ -31,15 +33,15 @@ pub inline fn next(self: *Self) RuntimeError!SpvWord {
|
||||
return self.nextOrNull() orelse return RuntimeError.InvalidSpirV;
|
||||
}
|
||||
|
||||
pub fn nextAs(self: *Self, comptime E: type) RuntimeError!E {
|
||||
pub inline fn nextAs(self: *Self, comptime E: type) RuntimeError!E {
|
||||
return self.nextAsOrNull(E) orelse return RuntimeError.InvalidSpirV;
|
||||
}
|
||||
|
||||
pub fn peek(self: *const Self) ?SpvWord {
|
||||
pub inline fn peek(self: *const Self) ?SpvWord {
|
||||
return if (self.index >= self.buffer.len) null else self.buffer[self.index];
|
||||
}
|
||||
|
||||
pub fn skip(self: *Self) bool {
|
||||
pub inline fn skip(self: *Self) bool {
|
||||
if (self.index >= self.buffer.len) {
|
||||
return false;
|
||||
}
|
||||
@@ -47,7 +49,7 @@ pub fn skip(self: *Self) bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn skipN(self: *Self, count: usize) bool {
|
||||
pub inline fn skipN(self: *Self, count: usize) bool {
|
||||
if (self.index >= self.buffer.len) {
|
||||
return false;
|
||||
}
|
||||
@@ -55,7 +57,7 @@ pub fn skipN(self: *Self, count: usize) bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn skipToEnd(self: *Self) void {
|
||||
pub inline fn skipToEnd(self: *Self) void {
|
||||
self.index = self.buffer.len;
|
||||
}
|
||||
|
||||
@@ -66,5 +68,6 @@ pub inline fn emitSourceLocation(self: *const Self) usize {
|
||||
pub inline fn jumpToSourceLocation(self: *Self, source_location: usize) bool {
|
||||
if (source_location > self.buffer.len) return false;
|
||||
self.index = source_location;
|
||||
self.did_jump = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
55
src/lib.zig
55
src/lib.zig
@@ -1,3 +1,33 @@
|
||||
//! A small footprint SPIR-V interpreter 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");
|
||||
@@ -7,10 +37,21 @@ 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);
|
||||
}
|
||||
pub const SpvVoid = spv.SpvVoid;
|
||||
pub const SpvByte = spv.SpvByte;
|
||||
pub const SpvWord = spv.SpvWord;
|
||||
pub const SpvBool = spv.SpvBool;
|
||||
|
||||
pub const GLSL_std_450 = @import("GLSL_std_450/opcodes.zig");
|
||||
|
||||
/// Maximum number of input locations per module
|
||||
pub const SPIRV_MAX_INPUT_LOCATIONS: usize = 32;
|
||||
|
||||
/// Maximum number of output locations per module
|
||||
pub const SPIRV_MAX_OUTPUT_LOCATIONS: usize = 32;
|
||||
|
||||
/// Maximum number of descriptor set per module
|
||||
pub const SPIRV_MAX_SET: usize = 32;
|
||||
|
||||
/// Maximum number of bindings per descriptor set
|
||||
pub const SPIRV_MAX_SET_BINDINGS: usize = 32;
|
||||
|
||||
2095
src/opcodes.zig
2095
src/opcodes.zig
File diff suppressed because it is too large
Load Diff
@@ -2391,3 +2391,5 @@ pub const SpvOp = enum(u32) {
|
||||
ConvertHandleToSampledImageINTEL = 6531,
|
||||
Max = 0x7fffffff,
|
||||
};
|
||||
|
||||
pub const SpvOpMaxValue: comptime_int = @intFromEnum(SpvOp.ConvertHandleToSampledImageINTEL);
|
||||
|
||||
30
test/arrays.zig
git.filemode.normal_file
30
test/arrays.zig
git.filemode.normal_file
@@ -0,0 +1,30 @@
|
||||
const std = @import("std");
|
||||
const root = @import("root.zig");
|
||||
const compileNzsl = root.compileNzsl;
|
||||
const case = root.case;
|
||||
|
||||
test "Simple array" {
|
||||
const allocator = std.testing.allocator;
|
||||
const shader =
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {
|
||||
\\ [location(0)] color: vec4[f32]
|
||||
\\ }
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {
|
||||
\\ let value = array[f32](4.0, 3.0, 2.0, 1.0);
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec4[f32](value[0], value[1], value[2], value[3]);
|
||||
\\ return output;
|
||||
\\ }
|
||||
;
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
|
||||
try case.expectOutput(f32, 4, code, "color", &.{ 4, 3, 2, 1 });
|
||||
}
|
||||
29
test/basics.zig
git.filemode.normal_file
29
test/basics.zig
git.filemode.normal_file
@@ -0,0 +1,29 @@
|
||||
const std = @import("std");
|
||||
const root = @import("root.zig");
|
||||
const compileNzsl = root.compileNzsl;
|
||||
const case = root.case;
|
||||
|
||||
test "Simple fragment shader" {
|
||||
const allocator = std.testing.allocator;
|
||||
const shader =
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {
|
||||
\\ [location(0)] color: vec4[f32]
|
||||
\\ }
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec4[f32](4.0, 3.0, 2.0, 1.0);
|
||||
\\ return output;
|
||||
\\ }
|
||||
;
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
|
||||
try case.expectOutput(f32, 4, code, "color", &.{ 4, 3, 2, 1 });
|
||||
}
|
||||
149
test/bitwise.zig
git.filemode.normal_file
149
test/bitwise.zig
git.filemode.normal_file
@@ -0,0 +1,149 @@
|
||||
const std = @import("std");
|
||||
const root = @import("root.zig");
|
||||
const compileNzsl = root.compileNzsl;
|
||||
const case = root.case;
|
||||
|
||||
const Operations = enum {
|
||||
BitwiseAnd,
|
||||
BitwiseOr,
|
||||
BitwiseXor,
|
||||
ShiftLeft,
|
||||
ShiftRight,
|
||||
ShiftRightArithmetic,
|
||||
};
|
||||
|
||||
test "Bitwise primitives" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_]type{ i32, u32 };
|
||||
var operations = std.EnumMap(Operations, []const u8).init(.{
|
||||
.BitwiseAnd = "&",
|
||||
.BitwiseOr = "|",
|
||||
.BitwiseXor = "^",
|
||||
.ShiftLeft = "<<",
|
||||
.ShiftRight = ">>",
|
||||
.ShiftRightArithmetic = ">>",
|
||||
});
|
||||
|
||||
var it = operations.iterator();
|
||||
while (it.next()) |op| {
|
||||
inline for (types) |T| {
|
||||
const op1: T = case.random(T);
|
||||
const op2: T = @mod(case.random(T), @bitSizeOf(T));
|
||||
const expected = switch (op.key) {
|
||||
.BitwiseAnd => op1 & op2,
|
||||
.BitwiseOr => op1 | op2,
|
||||
.BitwiseXor => op1 ^ op2,
|
||||
.ShiftLeft => op1 << @intCast(op2),
|
||||
.ShiftRight, .ShiftRightArithmetic => op1 >> @intCast(op2),
|
||||
};
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec4[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let op1: {s} = {d};
|
||||
\\ let op2: {s} = {d};
|
||||
\\ let color = op1 {s} op2;
|
||||
\\
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec4[{s}](color, color, color, color);
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
@typeName(T),
|
||||
@typeName(T),
|
||||
op1,
|
||||
@typeName(T),
|
||||
op2,
|
||||
op.value.*,
|
||||
@typeName(T),
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expectOutput(T, 4, code, "color", &.{ expected, expected, expected, expected });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test "Bitwise vectors" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_]type{ i32, u32 };
|
||||
var operations = std.EnumMap(Operations, []const u8).init(.{
|
||||
.BitwiseAnd = "&",
|
||||
.BitwiseOr = "|",
|
||||
.BitwiseXor = "^",
|
||||
.ShiftLeft = "<<",
|
||||
.ShiftRight = ">>",
|
||||
.ShiftRightArithmetic = ">>",
|
||||
});
|
||||
|
||||
var it = operations.iterator();
|
||||
while (it.next()) |op| {
|
||||
inline for (2..5) |L| {
|
||||
inline for (types) |T| {
|
||||
const op1: case.Vec(L, T) = .{ .val = case.random(@Vector(L, T)) };
|
||||
var op2: case.Vec(L, T) = .{ .val = case.random(@Vector(L, T)) };
|
||||
for (0..L) |i| op2.val[i] = @mod(op2.val[i], @bitSizeOf(T));
|
||||
const expected = switch (op.key) {
|
||||
.BitwiseAnd => op1.val & op2.val,
|
||||
.BitwiseOr => op1.val | op2.val,
|
||||
.BitwiseXor => op1.val ^ op2.val,
|
||||
.ShiftLeft => op1.val << @intCast(op2.val),
|
||||
.ShiftRight, .ShiftRightArithmetic => op1.val >> @intCast(op2.val),
|
||||
};
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec{d}[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let op1 = vec{d}[{s}]({f});
|
||||
\\ let op2 = vec{d}[{s}]({f});
|
||||
\\
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = op1 {s} op2;
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
L,
|
||||
@typeName(T),
|
||||
L,
|
||||
@typeName(T),
|
||||
op1,
|
||||
L,
|
||||
@typeName(T),
|
||||
op2,
|
||||
op.value.*,
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expectOutput(T, L, code, "color", &@as([L]T, expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
100
test/branching.zig
git.filemode.normal_file
100
test/branching.zig
git.filemode.normal_file
@@ -0,0 +1,100 @@
|
||||
const std = @import("std");
|
||||
const root = @import("root.zig");
|
||||
const compileNzsl = root.compileNzsl;
|
||||
const case = root.case;
|
||||
|
||||
const Operations = enum {
|
||||
Equal,
|
||||
NotEqual,
|
||||
Greater,
|
||||
GreaterEqual,
|
||||
Less,
|
||||
LessEqual,
|
||||
};
|
||||
|
||||
test "Simple branching" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_]type{ f32, f64, i32, u32 };
|
||||
var operations = std.EnumMap(Operations, []const u8).init(.{
|
||||
.Equal = "==",
|
||||
.NotEqual = "!=",
|
||||
.Greater = ">",
|
||||
.GreaterEqual = ">=",
|
||||
.Less = "<",
|
||||
.LessEqual = "<=",
|
||||
});
|
||||
|
||||
var it = operations.iterator();
|
||||
while (it.next()) |op| {
|
||||
inline for (types) |T| {
|
||||
const values = [_][2]T{
|
||||
[2]T{ std.math.lossyCast(T, 0), std.math.lossyCast(T, 9) },
|
||||
[2]T{ std.math.lossyCast(T, 1), std.math.lossyCast(T, 8) },
|
||||
[2]T{ std.math.lossyCast(T, 2), std.math.lossyCast(T, 7) },
|
||||
[2]T{ std.math.lossyCast(T, 3), std.math.lossyCast(T, 6) },
|
||||
[2]T{ std.math.lossyCast(T, 4), std.math.lossyCast(T, 5) },
|
||||
[2]T{ std.math.lossyCast(T, 5), std.math.lossyCast(T, 4) },
|
||||
[2]T{ std.math.lossyCast(T, 6), std.math.lossyCast(T, 3) },
|
||||
[2]T{ std.math.lossyCast(T, 7), std.math.lossyCast(T, 2) },
|
||||
[2]T{ std.math.lossyCast(T, 8), std.math.lossyCast(T, 1) },
|
||||
[2]T{ std.math.lossyCast(T, 9), std.math.lossyCast(T, 0) },
|
||||
[2]T{ std.math.lossyCast(T, 0), std.math.lossyCast(T, 0) },
|
||||
};
|
||||
for (values) |v| {
|
||||
const op1: T = v[0];
|
||||
const op2: T = v[1];
|
||||
const expected = switch (op.key) {
|
||||
.Equal => if (op1 == op2) op1 else op2,
|
||||
.NotEqual => if (op1 != op2) op1 else op2,
|
||||
.Greater => if (op1 > op2) op1 else op2,
|
||||
.GreaterEqual => if (op1 >= op2) op1 else op2,
|
||||
.Less => if (op1 < op2) op1 else op2,
|
||||
.LessEqual => if (op1 <= op2) op1 else op2,
|
||||
};
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec4[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let op1 = {s}({d});
|
||||
\\ let op2 = {s}({d});
|
||||
\\ let color: {s};
|
||||
\\ if (op1 {s} op2)
|
||||
\\ color = op1;
|
||||
\\ else
|
||||
\\ color = op2;
|
||||
\\
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec4[{s}](color, color, color, color);
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
@typeName(T),
|
||||
@typeName(T),
|
||||
op1,
|
||||
@typeName(T),
|
||||
op2,
|
||||
@typeName(T),
|
||||
op.value.*,
|
||||
@typeName(T),
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expectOutput(T, 4, code, "color", &.{ expected, expected, expected, expected });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
108
test/casts.zig
git.filemode.normal_file
108
test/casts.zig
git.filemode.normal_file
@@ -0,0 +1,108 @@
|
||||
const std = @import("std");
|
||||
const root = @import("root.zig");
|
||||
const compileNzsl = root.compileNzsl;
|
||||
const case = root.case;
|
||||
|
||||
test "Primitives casts" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_][2]type{
|
||||
[2]type{ f32, u32 },
|
||||
[2]type{ f32, i32 },
|
||||
[2]type{ u32, f32 },
|
||||
[2]type{ i32, f32 },
|
||||
[2]type{ f32, f64 },
|
||||
[2]type{ f64, f32 },
|
||||
[2]type{ f64, u32 },
|
||||
[2]type{ f64, i32 },
|
||||
[2]type{ u32, f64 },
|
||||
[2]type{ i32, f64 },
|
||||
};
|
||||
|
||||
inline for (types) |T| {
|
||||
const base = case.random(T[0]);
|
||||
const expected = std.math.lossyCast(T[1], base);
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec4[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let base = {s}({d});
|
||||
\\ let color = {s}(base);
|
||||
\\
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec4[{s}](color, color, color, color);
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
@typeName(T[1]),
|
||||
@typeName(T[0]),
|
||||
base,
|
||||
@typeName(T[1]),
|
||||
@typeName(T[1]),
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expectOutput(T[1], 4, code, "color", &.{ expected, expected, expected, expected });
|
||||
}
|
||||
}
|
||||
|
||||
test "Primitives bitcasts" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_][2]type{
|
||||
[2]type{ u32, i32 },
|
||||
[2]type{ i32, u32 },
|
||||
};
|
||||
|
||||
inline for (types) |T| {
|
||||
const base = case.random(T[0]);
|
||||
const expected = @as(T[1], @bitCast(base));
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec4[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let base = {s}({d});
|
||||
\\ let color = {s}(base);
|
||||
\\
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec4[{s}](color, color, color, color);
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
@typeName(T[1]),
|
||||
@typeName(T[0]),
|
||||
base,
|
||||
@typeName(T[1]),
|
||||
@typeName(T[1]),
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expectOutput(T[1], 4, code, "color", &.{ expected, expected, expected, expected });
|
||||
}
|
||||
}
|
||||
100
test/functions.zig
git.filemode.normal_file
100
test/functions.zig
git.filemode.normal_file
@@ -0,0 +1,100 @@
|
||||
const std = @import("std");
|
||||
const root = @import("root.zig");
|
||||
const compileNzsl = root.compileNzsl;
|
||||
const case = root.case;
|
||||
|
||||
test "Simple function calls" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_]type{ i32, u32, f32, f64 };
|
||||
|
||||
inline for (types) |T| {
|
||||
const n = case.random(T);
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec4[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ fn value() -> {s}
|
||||
\\ {{
|
||||
\\ return {d};
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec4[{s}](value(), value(), value(), value());
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
@typeName(T),
|
||||
@typeName(T),
|
||||
n,
|
||||
@typeName(T),
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expectOutput(T, 4, code, "color", &.{ n, n, n, n });
|
||||
}
|
||||
}
|
||||
|
||||
test "Nested function calls" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_]type{ i32, u32, f32, f64 };
|
||||
|
||||
inline for (types) |T| {
|
||||
const n = case.random(T);
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec4[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ fn deepValue() -> {s}
|
||||
\\ {{
|
||||
\\ return {d};
|
||||
\\ }}
|
||||
\\
|
||||
\\ fn value() -> {s}
|
||||
\\ {{
|
||||
\\ return deepValue();
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec4[{s}](value(), value(), value(), value());
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
@typeName(T),
|
||||
@typeName(T),
|
||||
n,
|
||||
@typeName(T),
|
||||
@typeName(T),
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expectOutput(T, 4, code, "color", &.{ n, n, n, n });
|
||||
}
|
||||
}
|
||||
51
test/inputs.zig
git.filemode.normal_file
51
test/inputs.zig
git.filemode.normal_file
@@ -0,0 +1,51 @@
|
||||
const std = @import("std");
|
||||
const root = @import("root.zig");
|
||||
const compileNzsl = root.compileNzsl;
|
||||
const case = root.case;
|
||||
|
||||
test "Inputs" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_]type{ f64, f32, i32, u32 };
|
||||
|
||||
inline for (2..5) |L| {
|
||||
inline for (types) |T| {
|
||||
const input: case.Vec(L, T) = .{ .val = case.random(@Vector(L, T)) };
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragIn
|
||||
\\ {{
|
||||
\\ [location(0)] pos: vec{d}[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec{d}[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main(input: FragIn) -> FragOut
|
||||
\\ {{
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = input.pos;
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
L,
|
||||
@typeName(T),
|
||||
L,
|
||||
@typeName(T),
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expectOutputWithInput(T, L, code, "color", &@as([L]T, input.val), "pos", &@as([L]T, input.val));
|
||||
}
|
||||
}
|
||||
}
|
||||
51
test/loops.zig
git.filemode.normal_file
51
test/loops.zig
git.filemode.normal_file
@@ -0,0 +1,51 @@
|
||||
const std = @import("std");
|
||||
const root = @import("root.zig");
|
||||
const compileNzsl = root.compileNzsl;
|
||||
const case = root.case;
|
||||
|
||||
test "Simple while loop" {
|
||||
const allocator = std.testing.allocator;
|
||||
const base = @mod(case.random(f32), 5.0);
|
||||
const iterations = 5;
|
||||
|
||||
var expected = base;
|
||||
for (1..iterations) |i| {
|
||||
expected *= @floatFromInt(i);
|
||||
}
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec4[f32]
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let value = f32({d});
|
||||
\\ let i = 1;
|
||||
\\ while (i < {d})
|
||||
\\ {{
|
||||
\\ value *= f32(i);
|
||||
\\ i += 1;
|
||||
\\ }}
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec4[f32](value, value, value, value);
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
base,
|
||||
iterations,
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expectOutput(f32, 4, code, "color", &.{ expected, expected, expected, expected });
|
||||
}
|
||||
146
test/maths.zig
git.filemode.normal_file
146
test/maths.zig
git.filemode.normal_file
@@ -0,0 +1,146 @@
|
||||
const std = @import("std");
|
||||
const root = @import("root.zig");
|
||||
const compileNzsl = root.compileNzsl;
|
||||
const case = root.case;
|
||||
|
||||
const Operations = enum {
|
||||
Add,
|
||||
Sub,
|
||||
Mul,
|
||||
Div,
|
||||
Mod,
|
||||
};
|
||||
|
||||
// Tests all mathematical operation on all NZSL supported primitive types
|
||||
test "Maths primitives" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_]type{ f32, f64, i32, u32 };
|
||||
var operations = std.EnumMap(Operations, u8).init(.{
|
||||
.Add = '+',
|
||||
.Sub = '-',
|
||||
.Mul = '*',
|
||||
.Div = '/',
|
||||
.Mod = '%',
|
||||
});
|
||||
|
||||
var it = operations.iterator();
|
||||
while (it.next()) |op| {
|
||||
inline for (types) |T| {
|
||||
const base: T = case.random(T);
|
||||
const ratio: T = case.random(T);
|
||||
const expected = switch (op.key) {
|
||||
.Add => if (@typeInfo(T) == .int) @addWithOverflow(base, ratio)[0] else base + ratio,
|
||||
.Sub => if (@typeInfo(T) == .int) @subWithOverflow(base, ratio)[0] else base - ratio,
|
||||
.Mul => if (@typeInfo(T) == .int) @mulWithOverflow(base, ratio)[0] else base * ratio,
|
||||
.Div => if (@typeInfo(T) == .int) @divTrunc(base, ratio) else base / ratio,
|
||||
.Mod => @mod(base, ratio),
|
||||
};
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec4[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let ratio: {s} = {d};
|
||||
\\ let base: {s} = {d};
|
||||
\\ let color = base {c} ratio;
|
||||
\\
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec4[{s}](color, color, color, color);
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
@typeName(T),
|
||||
@typeName(T),
|
||||
ratio,
|
||||
@typeName(T),
|
||||
base,
|
||||
op.value.*,
|
||||
@typeName(T),
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expectOutput(T, 4, code, "color", &.{ expected, expected, expected, expected });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tests all mathematical operation on vec2/3/4 with all NZSL supported primitive types
|
||||
test "Maths vectors" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_]type{ f32, f64, i32, u32 };
|
||||
var operations = std.EnumMap(Operations, u8).init(.{
|
||||
.Add = '+',
|
||||
.Sub = '-',
|
||||
.Mul = '*',
|
||||
.Div = '/',
|
||||
.Mod = '%',
|
||||
});
|
||||
|
||||
var it = operations.iterator();
|
||||
while (it.next()) |op| {
|
||||
inline for (2..5) |L| {
|
||||
inline for (types) |T| {
|
||||
const base_color: case.Vec(L, T) = .{ .val = case.random(@Vector(L, T)) };
|
||||
const ratio: case.Vec(L, T) = .{ .val = case.random(@Vector(L, T)) };
|
||||
const expected = switch (op.key) {
|
||||
.Add => if (@typeInfo(T) == .int) @addWithOverflow(base_color.val, ratio.val)[0] else base_color.val + ratio.val,
|
||||
.Sub => if (@typeInfo(T) == .int) @subWithOverflow(base_color.val, ratio.val)[0] else base_color.val - ratio.val,
|
||||
.Mul => if (@typeInfo(T) == .int) @mulWithOverflow(base_color.val, ratio.val)[0] else base_color.val * ratio.val,
|
||||
.Div => if (@typeInfo(T) == .int) @divTrunc(base_color.val, ratio.val) else base_color.val / ratio.val,
|
||||
.Mod => @mod(base_color.val, ratio.val),
|
||||
};
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec{d}[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let ratio = vec{d}[{s}]({f});
|
||||
\\
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec{d}[{s}]({f}) {c} ratio;
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
L,
|
||||
@typeName(T),
|
||||
L,
|
||||
@typeName(T),
|
||||
ratio,
|
||||
L,
|
||||
@typeName(T),
|
||||
base_color,
|
||||
op.value.*,
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expectOutput(T, L, code, "color", &@as([L]T, expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
122
test/root.zig
git.filemode.normal_file
122
test/root.zig
git.filemode.normal_file
@@ -0,0 +1,122 @@
|
||||
const std = @import("std");
|
||||
const spv = @import("spv");
|
||||
const nzsl = @import("nzsl");
|
||||
|
||||
pub fn compileNzsl(allocator: std.mem.Allocator, source: []const u8) ![]const u32 {
|
||||
const module = try nzsl.parser.parseSource(source);
|
||||
defer module.deinit();
|
||||
|
||||
const params = try nzsl.BackendParameters.init();
|
||||
defer params.deinit();
|
||||
params.setDebugLevel(.full);
|
||||
|
||||
const writer = try nzsl.SpirvWriter.init();
|
||||
defer writer.deinit();
|
||||
|
||||
const output = try writer.generate(module, params);
|
||||
defer output.deinit();
|
||||
|
||||
return allocator.dupe(u32, output.getCode());
|
||||
}
|
||||
|
||||
pub const case = struct {
|
||||
pub fn expectOutput(comptime T: type, comptime len: usize, source: []const u32, output_name: []const u8, expected: []const T) !void {
|
||||
const allocator = std.testing.allocator;
|
||||
|
||||
const module_options = [_]spv.Module.ModuleOptions{
|
||||
.{
|
||||
.use_simd_vectors_specializations = true,
|
||||
},
|
||||
.{
|
||||
.use_simd_vectors_specializations = false,
|
||||
},
|
||||
};
|
||||
|
||||
for (module_options) |opt| {
|
||||
var module = try spv.Module.init(allocator, source, opt);
|
||||
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: [len]T = undefined;
|
||||
try rt.readOutput(std.mem.sliceAsBytes(output[0..]), try rt.getResultByName(output_name));
|
||||
|
||||
try std.testing.expectEqualSlices(T, expected, &output);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expectOutputWithInput(comptime T: type, comptime len: usize, source: []const u32, output_name: []const u8, expected: []const T, input_name: []const u8, input: []const T) !void {
|
||||
const allocator = std.testing.allocator;
|
||||
|
||||
// To test with all important module options
|
||||
const module_options = [_]spv.Module.ModuleOptions{
|
||||
.{
|
||||
.use_simd_vectors_specializations = true,
|
||||
},
|
||||
.{
|
||||
.use_simd_vectors_specializations = false,
|
||||
},
|
||||
};
|
||||
|
||||
for (module_options) |opt| {
|
||||
var module = try spv.Module.init(allocator, source, opt);
|
||||
defer module.deinit(allocator);
|
||||
|
||||
var rt = try spv.Runtime.init(allocator, &module);
|
||||
defer rt.deinit(allocator);
|
||||
|
||||
try rt.writeInput(std.mem.sliceAsBytes(input[0..len]), try rt.getResultByName(input_name));
|
||||
|
||||
try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main"));
|
||||
var output: [len]T = undefined;
|
||||
try rt.readOutput(std.mem.sliceAsBytes(output[0..]), try rt.getResultByName(output_name));
|
||||
|
||||
try std.testing.expectEqualSlices(T, expected, &output);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn random(comptime T: type) T {
|
||||
var prng: std.Random.DefaultPrng = .init(@intCast(std.time.microTimestamp()));
|
||||
const rand = prng.random();
|
||||
|
||||
return switch (@typeInfo(T)) {
|
||||
.int => rand.int(T),
|
||||
.float => rand.float(T),
|
||||
.vector => |v| blk: {
|
||||
var vec: @Vector(v.len, v.child) = undefined;
|
||||
for (0..v.len) |i| {
|
||||
vec[i] = random(v.child);
|
||||
}
|
||||
break :blk vec;
|
||||
},
|
||||
inline else => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn Vec(comptime len: usize, comptime T: type) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
val: @Vector(len, T),
|
||||
pub fn format(self: *const Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
|
||||
inline for (0..len) |i| {
|
||||
try w.print("{d}", .{self.val[i]});
|
||||
if (i < len - 1) try w.writeAll(", ");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
test {
|
||||
std.testing.refAllDecls(@import("arrays.zig"));
|
||||
std.testing.refAllDecls(@import("basics.zig"));
|
||||
std.testing.refAllDecls(@import("bitwise.zig"));
|
||||
std.testing.refAllDecls(@import("branching.zig"));
|
||||
std.testing.refAllDecls(@import("casts.zig"));
|
||||
std.testing.refAllDecls(@import("functions.zig"));
|
||||
std.testing.refAllDecls(@import("inputs.zig"));
|
||||
std.testing.refAllDecls(@import("loops.zig"));
|
||||
std.testing.refAllDecls(@import("maths.zig"));
|
||||
}
|
||||
289
test/test_runner.zig
git.filemode.normal_file
289
test/test_runner.zig
git.filemode.normal_file
@@ -0,0 +1,289 @@
|
||||
// See https://gist.github.com/karlseguin/c6bea5b35e4e8d26af6f81c22cb5d76b/1f317ebc9cd09bc50fd5591d09c34255e15d1d85
|
||||
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const BORDER = "=" ** 80;
|
||||
|
||||
// use in custom panic handler
|
||||
var current_test: ?[]const u8 = null;
|
||||
|
||||
pub fn main() !void {
|
||||
var mem: [8192]u8 = undefined;
|
||||
var fba = std.heap.FixedBufferAllocator.init(&mem);
|
||||
|
||||
const allocator = fba.allocator();
|
||||
|
||||
const env = Env.init(allocator);
|
||||
defer env.deinit(allocator);
|
||||
|
||||
var slowest = SlowTracker.init(allocator, 5);
|
||||
defer slowest.deinit();
|
||||
|
||||
var pass: usize = 0;
|
||||
var fail: usize = 0;
|
||||
var skip: usize = 0;
|
||||
var leak: usize = 0;
|
||||
|
||||
Printer.fmt("\r\x1b[0K", .{}); // beginning of line and clear to end of line
|
||||
|
||||
for (builtin.test_functions) |t| {
|
||||
if (isSetup(t)) {
|
||||
t.func() catch |err| {
|
||||
Printer.status(.fail, "\nsetup \"{s}\" failed: {}\n", .{ t.name, err });
|
||||
return err;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
for (builtin.test_functions) |t| {
|
||||
if (isSetup(t) or isTeardown(t)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var status = Status.pass;
|
||||
slowest.startTiming();
|
||||
|
||||
const is_unnamed_test = isUnnamed(t);
|
||||
if (env.filter) |f| {
|
||||
if (!is_unnamed_test and std.mem.indexOf(u8, t.name, f) == null) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const friendly_name = blk: {
|
||||
const name = t.name;
|
||||
var it = std.mem.splitScalar(u8, name, '.');
|
||||
while (it.next()) |value| {
|
||||
if (std.mem.eql(u8, value, "test")) {
|
||||
const rest = it.rest();
|
||||
break :blk if (rest.len > 0) rest else name;
|
||||
}
|
||||
}
|
||||
break :blk name;
|
||||
};
|
||||
|
||||
current_test = friendly_name;
|
||||
std.testing.allocator_instance = .{};
|
||||
const result = t.func();
|
||||
current_test = null;
|
||||
|
||||
const ns_taken = slowest.endTiming(friendly_name);
|
||||
|
||||
if (std.testing.allocator_instance.deinit() == .leak) {
|
||||
leak += 1;
|
||||
Printer.status(.fail, "\n{s}\n\"{s}\" - Memory Leak\n{s}\n", .{ BORDER, friendly_name, BORDER });
|
||||
}
|
||||
|
||||
if (result) |_| {
|
||||
pass += 1;
|
||||
} else |err| switch (err) {
|
||||
error.SkipZigTest => {
|
||||
skip += 1;
|
||||
status = .skip;
|
||||
},
|
||||
else => {
|
||||
status = .fail;
|
||||
fail += 1;
|
||||
Printer.status(.fail, "\n{s}\n\"{s}\" - {s}\n{s}\n", .{ BORDER, friendly_name, @errorName(err), BORDER });
|
||||
if (@errorReturnTrace()) |trace| {
|
||||
std.debug.dumpStackTrace(trace.*);
|
||||
}
|
||||
if (env.fail_first) {
|
||||
break;
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
if (env.verbose) {
|
||||
const ms = @as(f64, @floatFromInt(ns_taken)) / 1_000_000.0;
|
||||
Printer.status(status, "\x1b[35m[{d: >10.2} ms]\x1b[0m {s: <30}", .{ ms, friendly_name });
|
||||
} else {
|
||||
Printer.status(status, ".", .{});
|
||||
}
|
||||
}
|
||||
|
||||
for (builtin.test_functions) |t| {
|
||||
if (isTeardown(t)) {
|
||||
t.func() catch |err| {
|
||||
Printer.status(.fail, "\nteardown \"{s}\" failed: {}\n", .{ t.name, err });
|
||||
return err;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const total_tests = pass + fail;
|
||||
const status = if (fail == 0) Status.pass else Status.fail;
|
||||
Printer.status(status, "\n{d} of {d} test{s} passed\n", .{ pass, total_tests, if (total_tests != 1) "s" else "" });
|
||||
if (skip > 0) {
|
||||
Printer.status(.skip, "{d} test{s} skipped\n", .{ skip, if (skip != 1) "s" else "" });
|
||||
}
|
||||
if (leak > 0) {
|
||||
Printer.status(.fail, "{d} test{s} leaked\n", .{ leak, if (leak != 1) "s" else "" });
|
||||
}
|
||||
Printer.fmt("\n", .{});
|
||||
try slowest.display();
|
||||
Printer.fmt("\n", .{});
|
||||
std.posix.exit(if (fail == 0) 0 else 1);
|
||||
}
|
||||
|
||||
const Printer = struct {
|
||||
fn fmt(comptime format: []const u8, args: anytype) void {
|
||||
std.debug.print(format, args);
|
||||
}
|
||||
|
||||
fn status(s: Status, comptime format: []const u8, args: anytype) void {
|
||||
switch (s) {
|
||||
.pass => std.debug.print(format ++ "\x1b[32mâś“\x1b[0m\n", args),
|
||||
.fail => std.debug.print(format ++ "\x1b[31mâś—\x1b[0m\n", args),
|
||||
.skip => std.debug.print(format ++ "\x1b[33m \x1b[0m\n", args),
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const Status = enum {
|
||||
pass,
|
||||
fail,
|
||||
skip,
|
||||
text,
|
||||
};
|
||||
|
||||
const SlowTracker = struct {
|
||||
const SlowestQueue = std.PriorityDequeue(TestInfo, void, compareTiming);
|
||||
max: usize,
|
||||
slowest: SlowestQueue,
|
||||
timer: std.time.Timer,
|
||||
|
||||
fn init(allocator: Allocator, count: u32) SlowTracker {
|
||||
const timer = std.time.Timer.start() catch @panic("failed to start timer");
|
||||
var slowest = SlowestQueue.init(allocator, {});
|
||||
slowest.ensureTotalCapacity(count) catch @panic("OOM");
|
||||
return .{
|
||||
.max = count,
|
||||
.timer = timer,
|
||||
.slowest = slowest,
|
||||
};
|
||||
}
|
||||
|
||||
const TestInfo = struct {
|
||||
ns: u64,
|
||||
name: []const u8,
|
||||
};
|
||||
|
||||
fn deinit(self: SlowTracker) void {
|
||||
self.slowest.deinit();
|
||||
}
|
||||
|
||||
fn startTiming(self: *SlowTracker) void {
|
||||
self.timer.reset();
|
||||
}
|
||||
|
||||
fn endTiming(self: *SlowTracker, test_name: []const u8) u64 {
|
||||
var timer = self.timer;
|
||||
const ns = timer.lap();
|
||||
|
||||
var slowest = &self.slowest;
|
||||
|
||||
if (slowest.count() < self.max) {
|
||||
// Capacity is fixed to the # of slow tests we want to track
|
||||
// If we've tracked fewer tests than this capacity, than always add
|
||||
slowest.add(TestInfo{ .ns = ns, .name = test_name }) catch @panic("failed to track test timing");
|
||||
return ns;
|
||||
}
|
||||
|
||||
{
|
||||
// Optimization to avoid shifting the dequeue for the common case
|
||||
// where the test isn't one of our slowest.
|
||||
const fastest_of_the_slow = slowest.peekMin() orelse unreachable;
|
||||
if (fastest_of_the_slow.ns > ns) {
|
||||
// the test was faster than our fastest slow test, don't add
|
||||
return ns;
|
||||
}
|
||||
}
|
||||
|
||||
// the previous fastest of our slow tests, has been pushed off.
|
||||
_ = slowest.removeMin();
|
||||
slowest.add(TestInfo{ .ns = ns, .name = test_name }) catch @panic("failed to track test timing");
|
||||
return ns;
|
||||
}
|
||||
|
||||
fn display(self: *SlowTracker) !void {
|
||||
var slowest = self.slowest;
|
||||
const count = slowest.count();
|
||||
Printer.fmt("Slowest {d} test{s}: \n", .{ count, if (count != 1) "s" else "" });
|
||||
while (slowest.removeMinOrNull()) |info| {
|
||||
const ms = @as(f64, @floatFromInt(info.ns)) / 1_000_000.0;
|
||||
Printer.fmt(" {d:.2}ms\t{s}\n", .{ ms, info.name });
|
||||
}
|
||||
}
|
||||
|
||||
fn compareTiming(context: void, a: TestInfo, b: TestInfo) std.math.Order {
|
||||
_ = context;
|
||||
return std.math.order(a.ns, b.ns);
|
||||
}
|
||||
};
|
||||
|
||||
const Env = struct {
|
||||
verbose: bool,
|
||||
fail_first: bool,
|
||||
filter: ?[]const u8,
|
||||
|
||||
fn init(allocator: Allocator) Env {
|
||||
return .{
|
||||
.verbose = readEnvBool(allocator, "TEST_VERBOSE", true),
|
||||
.fail_first = readEnvBool(allocator, "TEST_FAIL_FIRST", false),
|
||||
.filter = readEnv(allocator, "TEST_FILTER"),
|
||||
};
|
||||
}
|
||||
|
||||
fn deinit(self: Env, allocator: Allocator) void {
|
||||
if (self.filter) |f| {
|
||||
allocator.free(f);
|
||||
}
|
||||
}
|
||||
|
||||
fn readEnv(allocator: Allocator, key: []const u8) ?[]const u8 {
|
||||
const v = std.process.getEnvVarOwned(allocator, key) catch |err| {
|
||||
if (err == error.EnvironmentVariableNotFound) {
|
||||
return null;
|
||||
}
|
||||
std.log.warn("failed to get env var {s} due to err {}", .{ key, err });
|
||||
return null;
|
||||
};
|
||||
return v;
|
||||
}
|
||||
|
||||
fn readEnvBool(allocator: Allocator, key: []const u8, deflt: bool) bool {
|
||||
const value = readEnv(allocator, key) orelse return deflt;
|
||||
defer allocator.free(value);
|
||||
return std.ascii.eqlIgnoreCase(value, "true");
|
||||
}
|
||||
};
|
||||
|
||||
pub const panic = std.debug.FullPanic(struct {
|
||||
pub fn panicFn(msg: []const u8, first_trace_addr: ?usize) noreturn {
|
||||
if (current_test) |ct| {
|
||||
std.debug.print("\x1b[31m{s}\npanic running \"{s}\"\n{s}\x1b[0m\n", .{ BORDER, ct, BORDER });
|
||||
}
|
||||
std.debug.defaultPanic(msg, first_trace_addr);
|
||||
}
|
||||
}.panicFn);
|
||||
|
||||
fn isUnnamed(t: std.builtin.TestFn) bool {
|
||||
const marker = ".test_";
|
||||
const test_name = t.name;
|
||||
const index = std.mem.indexOf(u8, test_name, marker) orelse return false;
|
||||
_ = std.fmt.parseInt(u32, test_name[index + marker.len ..], 10) catch return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
fn isSetup(t: std.builtin.TestFn) bool {
|
||||
return std.mem.endsWith(u8, t.name, "tests:beforeAll");
|
||||
}
|
||||
|
||||
fn isTeardown(t: std.builtin.TestFn) bool {
|
||||
return std.mem.endsWith(u8, t.name, "tests:afterAll");
|
||||
}
|
||||
Reference in New Issue
Block a user