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

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 {
var prng: std.Random.DefaultPrng = .init(@intCast(std.time.microTimestamp()));
const rand = prng.random();
@@ -86,6 +115,7 @@ test {
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"));
}