adding loops tests
All checks were successful
Build / build (push) Successful in 2m2s
Test / build (push) Successful in 5m10s

This commit is contained in:
2026-01-18 18:18:21 +01:00
parent 04092e25c1
commit 9868b34f92
6 changed files with 158 additions and 49 deletions

View File

@@ -17,8 +17,8 @@ pub fn main() !void {
defer rt.deinit(allocator); defer rt.deinit(allocator);
try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main")); try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main"));
var output: [4]i32 = undefined; var output: [4]f32 = undefined;
try rt.readOutput(i32, output[0..output.len], try rt.getResultByName("color")); try rt.readOutput(f32, output[0..output.len], try rt.getResultByName("color"));
std.log.info("Output: Vec4{any}", .{output}); std.log.info("Output: Vec4{any}", .{output});
} }
std.log.info("Successfully executed", .{}); std.log.info("Successfully executed", .{});

View File

@@ -10,8 +10,10 @@ struct FragOut
[entry(frag)] [entry(frag)]
fn main() -> FragOut fn main() -> FragOut
{ {
let value: f32 = 0.0; let value: f32 = 1.0;
for i in 1 -> 20 { for i in 1 -> 5 {
if (i == 3)
continue;
value *= f32(i); value *= f32(i);
} }
let output: FragOut; let output: FragOut;

Binary file not shown.

View File

@@ -1,18 +1,18 @@
Version 1.0 Version 1.0
Generator: 2560130 Generator: 2560130
Bound: 45 Bound: 51
Schema: 0 Schema: 0
OpCapability Capability(Shader) OpCapability Capability(Shader)
OpCapability Capability(Float64) OpCapability Capability(Float64)
OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450) OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450)
OpEntryPoint ExecutionModel(Fragment) %17 "main" %6 OpEntryPoint ExecutionModel(Fragment) %18 "main" %6
OpExecutionMode %17 ExecutionMode(OriginUpperLeft) OpExecutionMode %18 ExecutionMode(OriginUpperLeft)
OpSource SourceLanguage(NZSL) 4198400 OpSource SourceLanguage(NZSL) 4198400
OpSourceExtension "Version: 1.1" OpSourceExtension "Version: 1.1"
OpName %7 "FragOut" OpName %7 "FragOut"
OpMemberName %7 0 "color" OpMemberName %7 0 "color"
OpName %6 "color" OpName %6 "color"
OpName %17 "main" OpName %18 "main"
OpDecorate %6 Decoration(Location) 0 OpDecorate %6 Decoration(Location) 0
OpMemberDecorate %7 0 Decoration(Offset) 0 OpMemberDecorate %7 0 Decoration(Offset) 0
%1 = OpTypeVoid %1 = OpTypeVoid
@@ -21,55 +21,65 @@ Schema: 0
%4 = OpTypeVector %3 4 %4 = OpTypeVector %3 4
%5 = OpTypePointer StorageClass(Output) %4 %5 = OpTypePointer StorageClass(Output) %4
%7 = OpTypeStruct %4 %7 = OpTypeStruct %4
%8 = OpConstant %3 f32(0) %8 = OpConstant %3 f32(1)
%9 = OpTypePointer StorageClass(Function) %3 %9 = OpTypePointer StorageClass(Function) %3
%10 = OpTypeInt 32 1 %10 = OpTypeInt 32 1
%11 = OpConstant %10 i32(1) %11 = OpConstant %10 i32(1)
%12 = OpTypePointer StorageClass(Function) %10 %12 = OpTypePointer StorageClass(Function) %10
%13 = OpConstant %10 i32(20) %13 = OpConstant %10 i32(5)
%14 = OpTypeBool %14 = OpTypeBool
%15 = OpTypePointer StorageClass(Function) %7 %15 = OpConstant %10 i32(3)
%16 = OpConstant %10 i32(0) %16 = OpTypePointer StorageClass(Function) %7
%42 = OpTypePointer StorageClass(Function) %4 %17 = OpConstant %10 i32(0)
%48 = OpTypePointer StorageClass(Function) %4
%6 = OpVariable %5 StorageClass(Output) %6 = OpVariable %5 StorageClass(Output)
%17 = OpFunction %1 FunctionControl(0) %2 %18 = OpFunction %1 FunctionControl(0) %2
%18 = OpLabel %19 = OpLabel
%19 = OpVariable %9 StorageClass(Function) %20 = OpVariable %9 StorageClass(Function)
%20 = OpVariable %12 StorageClass(Function)
%21 = OpVariable %12 StorageClass(Function) %21 = OpVariable %12 StorageClass(Function)
%22 = OpVariable %15 StorageClass(Function) %22 = OpVariable %12 StorageClass(Function)
OpStore %19 %8 %23 = OpVariable %16 StorageClass(Function)
OpStore %20 %11 OpStore %20 %8
OpStore %21 %13 OpStore %21 %11
OpBranch %23 OpStore %22 %13
%23 = OpLabel OpBranch %24
%27 = OpLoad %10 %20
%28 = OpLoad %10 %21
%29 = OpSLessThan %14 %27 %28
OpLoopMerge %25 %26 LoopControl(0)
OpBranchConditional %29 %24 %25
%24 = OpLabel %24 = OpLabel
%30 = OpLoad %3 %19 %28 = OpLoad %10 %21
%31 = OpLoad %10 %20 %29 = OpLoad %10 %22
%32 = OpConvertSToF %3 %31 %30 = OpSLessThan %14 %28 %29
%33 = OpFMul %3 %30 %32 OpLoopMerge %26 %27 LoopControl(0)
OpStore %19 %33 OpBranchConditional %30 %25 %26
%34 = OpLoad %10 %20
%35 = OpIAdd %10 %34 %11
OpStore %20 %35
OpBranch %26
%26 = OpLabel
OpBranch %23
%25 = OpLabel %25 = OpLabel
%36 = OpLoad %3 %19 %34 = OpLoad %10 %21
%37 = OpLoad %3 %19 %35 = OpIEqual %14 %34 %15
%38 = OpLoad %3 %19 OpSelectionMerge %31 SelectionControl(0)
%39 = OpLoad %3 %19 OpBranchConditional %35 %32 %33
%40 = OpCompositeConstruct %4 %36 %37 %38 %39 %32 = OpLabel
%41 = OpAccessChain %42 %22 %16 OpBranch %27
OpStore %41 %40 %33 = OpLabel
%43 = OpLoad %7 %22 OpBranch %31
%44 = OpCompositeExtract %4 %43 0 %31 = OpLabel
OpStore %6 %44 %36 = OpLoad %3 %20
%37 = OpLoad %10 %21
%38 = OpConvertSToF %3 %37
%39 = OpFMul %3 %36 %38
OpStore %20 %39
%40 = OpLoad %10 %21
%41 = OpIAdd %10 %40 %11
OpStore %21 %41
OpBranch %27
%27 = OpLabel
OpBranch %24
%26 = OpLabel
%42 = OpLoad %3 %20
%43 = OpLoad %3 %20
%44 = OpLoad %3 %20
%45 = OpLoad %3 %20
%46 = OpCompositeConstruct %4 %42 %43 %44 %45
%47 = OpAccessChain %48 %23 %17
OpStore %47 %46
%49 = OpLoad %7 %23
%50 = OpCompositeExtract %4 %49 0
OpStore %6 %50
OpReturn OpReturn
OpFunctionEnd OpFunctionEnd

96
test/loops.zig git.filemode.normal_file
View File

@@ -0,0 +1,96 @@
const std = @import("std");
const root = @import("root.zig");
const compileNzsl = root.compileNzsl;
const case = root.case;
test "Simple for 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});
\\ for i in 1 -> {d}
\\ {{
\\ value *= f32(i);
\\ }}
\\ 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 });
}
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 });
}

View File

@@ -60,5 +60,6 @@ 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("loops.zig"));
std.testing.refAllDecls(@import("maths.zig")); std.testing.refAllDecls(@import("maths.zig"));
} }