adding sandbox
All checks were successful
Build / build (push) Successful in 1m38s
Test / build (push) Successful in 5m58s

This commit is contained in:
2026-01-21 23:13:02 +01:00
parent 66eb4ba578
commit bb866f1312
10 changed files with 196 additions and 8 deletions

View File

@@ -20,8 +20,4 @@ jobs:
- uses: mlugg/setup-zig@v2 - uses: mlugg/setup-zig@v2
- name: Test - name: Test
run: | run: zig build test -Dno-example=true
zig build test -Dno-example=true
zig build test -Dno-example=true --release=fast
zig build test -Dno-example=true --release=safe
zig build test -Dno-example=true --release=small

View File

@@ -15,7 +15,7 @@ pub fn main() !void {
const allocator = gpa.allocator(); 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); defer module.deinit(allocator);
var rt = try spv.Runtime.init(allocator, &module); var rt = try spv.Runtime.init(allocator, &module);

View File

@@ -4,6 +4,8 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const use_llvm = b.option(bool, "use-llvm", "use llvm") orelse false;
const mod = b.createModule(.{ const mod = b.createModule(.{
.root_source_file = b.path("src/lib.zig"), .root_source_file = b.path("src/lib.zig"),
.target = target, .target = target,
@@ -22,7 +24,7 @@ pub fn build(b: *std.Build) void {
.name = "spirv_interpreter", .name = "spirv_interpreter",
.root_module = mod, .root_module = mod,
.linkage = .dynamic, .linkage = .dynamic,
//.use_llvm = true, .use_llvm = use_llvm,
}); });
const lib_install = b.addInstallArtifact(lib, .{}); const lib_install = b.addInstallArtifact(lib, .{});
@@ -44,6 +46,7 @@ pub fn build(b: *std.Build) void {
//.{ .name = "pretty", .module = pretty.module("pretty") }, //.{ .name = "pretty", .module = pretty.module("pretty") },
}, },
}), }),
.use_llvm = use_llvm,
}); });
const example_install = b.addInstallArtifact(example_exe, .{}); const example_install = b.addInstallArtifact(example_exe, .{});
@@ -60,6 +63,35 @@ pub fn build(b: *std.Build) void {
compile_shader_step.dependOn(&compile_shader_cmd.step); compile_shader_step.dependOn(&compile_shader_cmd.step);
} }
// Zig sandbox setup
const sandbox_exe = b.addExecutable(.{
.name = "spirv_interpreter_sandbpx",
.root_module = b.createModule(.{
.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 sandbox_install = b.addInstallArtifact(sandbox_exe, .{});
sandbox_install.step.dependOn(&lib_install.step);
const run_sandbox = b.addRunArtifact(sandbox_exe);
run_sandbox.step.dependOn(&sandbox_install.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 // Zig unit tests setup
const nzsl = b.lazyDependency("NZSL", .{ .target = target, .optimize = optimize }) orelse return; const nzsl = b.lazyDependency("NZSL", .{ .target = target, .optimize = optimize }) orelse return;

25
sandbox/main.zig git.filemode.normal_file
View File

@@ -0,0 +1,25 @@
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", .{});
}

15
sandbox/shader.nzsl git.filemode.normal_file
View File

@@ -0,0 +1,15 @@
[nzsl_version("1.1")]
module;
struct FragOut
{
[location(0)] color: vec4[f32]
}
[entry(frag)]
fn main() -> FragOut
{
let output: FragOut;
output.color = vec4[f32](1.0, 1.0, 1.0, 1.0);
return output;
}

BIN
sandbox/shader.spv git.filemode.normal_file

Binary file not shown.

39
sandbox/shader.spv.txt git.filemode.normal_file
View File

@@ -0,0 +1,39 @@
Version 1.0
Generator: 2560130
Bound: 20
Schema: 0
OpCapability Capability(Shader)
OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450)
OpEntryPoint ExecutionModel(Fragment) %12 "main" %6
OpExecutionMode %12 ExecutionMode(OriginUpperLeft)
OpSource SourceLanguage(NZSL) 4198400
OpSourceExtension "Version: 1.1"
OpName %7 "FragOut"
OpMemberName %7 0 "color"
OpName %6 "color"
OpName %12 "main"
OpDecorate %6 Decoration(Location) 0
OpMemberDecorate %7 0 Decoration(Offset) 0
%1 = OpTypeVoid
%2 = OpTypeFunction %1
%3 = OpTypeFloat 32
%4 = OpTypeVector %3 4
%5 = OpTypePointer StorageClass(Output) %4
%7 = OpTypeStruct %4
%8 = OpTypePointer StorageClass(Function) %7
%9 = OpTypeInt 32 1
%10 = OpConstant %9 i32(0)
%11 = OpConstant %3 f32(1)
%17 = OpTypePointer StorageClass(Function) %4
%6 = OpVariable %5 StorageClass(Output)
%12 = OpFunction %1 FunctionControl(0) %2
%13 = OpLabel
%14 = OpVariable %8 StorageClass(Function)
%15 = OpCompositeConstruct %4 %11 %11 %11 %11
%16 = OpAccessChain %17 %14 %10
OpStore %16 %15
%18 = OpLoad %7 %14
%19 = OpCompositeExtract %4 %18 0
OpStore %6 %19
OpReturn
OpFunctionEnd

View File

@@ -13,7 +13,7 @@
//! //!
//! const allocator = gpa.allocator(); //! 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); //! defer module.deinit(allocator);
//! //!
//! var rt = try spv.Runtime.init(allocator, &module); //! var rt = try spv.Runtime.init(allocator, &module);

51
test/inputs.zig git.filemode.normal_file
View 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));
}
}
}

View File

@@ -47,6 +47,35 @@ pub const case = struct {
} }
} }
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;
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(T, input[0..len], try rt.getResultByName(input_name));
try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main"));
var output: [len]T = undefined;
try rt.readOutput(T, output[0..len], try rt.getResultByName(output_name));
try std.testing.expectEqualSlices(T, expected, &output);
}
}
pub fn random(comptime T: type) T { pub fn random(comptime T: type) T {
var prng: std.Random.DefaultPrng = .init(@intCast(std.time.microTimestamp())); var prng: std.Random.DefaultPrng = .init(@intCast(std.time.microTimestamp()));
const rand = prng.random(); const rand = prng.random();
@@ -86,6 +115,7 @@ test {
std.testing.refAllDecls(@import("branching.zig")); std.testing.refAllDecls(@import("branching.zig"));
std.testing.refAllDecls(@import("casts.zig")); std.testing.refAllDecls(@import("casts.zig"));
std.testing.refAllDecls(@import("functions.zig")); std.testing.refAllDecls(@import("functions.zig"));
std.testing.refAllDecls(@import("inputs.zig"));
std.testing.refAllDecls(@import("loops.zig")); std.testing.refAllDecls(@import("loops.zig"));
std.testing.refAllDecls(@import("maths.zig")); std.testing.refAllDecls(@import("maths.zig"));
} }