From 9868b34f922441bc1243a6b7fb5a8d0336cbc7fc Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Sun, 18 Jan 2026 18:18:21 +0100 Subject: [PATCH] adding loops tests --- example/main.zig | 4 +- example/shader.nzsl | 6 ++- example/shader.spv | Bin 1040 -> 1160 bytes example/shader.spv.txt | 100 ++++++++++++++++++++++------------------- test/loops.zig | 96 +++++++++++++++++++++++++++++++++++++++ test/root.zig | 1 + 6 files changed, 158 insertions(+), 49 deletions(-) create mode 100644 test/loops.zig diff --git a/example/main.zig b/example/main.zig index 7f778e5..169a6cc 100644 --- a/example/main.zig +++ b/example/main.zig @@ -17,8 +17,8 @@ pub fn main() !void { defer rt.deinit(allocator); try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main")); - var output: [4]i32 = undefined; - try rt.readOutput(i32, output[0..output.len], try rt.getResultByName("color")); + 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", .{}); diff --git a/example/shader.nzsl b/example/shader.nzsl index 08cb5eb..2c15105 100644 --- a/example/shader.nzsl +++ b/example/shader.nzsl @@ -10,8 +10,10 @@ struct FragOut [entry(frag)] fn main() -> FragOut { - let value: f32 = 0.0; - for i in 1 -> 20 { + let value: f32 = 1.0; + for i in 1 -> 5 { + if (i == 3) + continue; value *= f32(i); } let output: FragOut; diff --git a/example/shader.spv b/example/shader.spv index 860d8c9f8e6a6057c397ffcba71dcd261816ac85..0709c8c8f3a3ef741f465de18857f6e40ddef3b3 100644 GIT binary patch literal 1160 zcmZ9LO;1x%5QfK=f*{Z$Uy2B~6huB1NQ}`axB+(P#yv5FkSI5)x+f<58F&0S1QHUT zr@be+=}G3!yzhLT>1nmn8i;rhe;UiN6Wucw6_^P15PCGK9fw>!`lAp182NF0cCMoz zX%HhvF|29~qNA}#Y%RW?w11xE=kKzu%`NE~ta1D8vPWc ze68nh%zp!N9(U()4>x?r(Z+g*3B-J{Gfwv4sSmVGgSqZR+dS{9ZH{-lrPmX* zJ=GuR;r(Y3;}d?4ZO#?*x(9z(W8|;NnL$58Ja=IIPZ4=*Yo}TZ*w+4A)M{bNxzm5l zHjnmLM_-0C5$k9>v%SRfVspf5`?%Xh#QUedOW5|!3;zpjxl86Sei<=GKJ88}<-f!> z|AKgTw1UW`o$8Y-&*z-w9lMV--zs*R&;DyYc)bU26!D?OXDXuKx@1*4i#OR43l|DRjW{_Q{JiL2@8`QayM`M?#T^cM=ihE`sBaX$xzqk-e^6rVTlMXq zW**VQ{4(M>uJq^(L(wkxScOlGnEYo8b`d%D96j8{?bJEnM#WzyBTsXie8o*59d#bsxy+XS7baNi9cWwUe%jW!%U@>Ap`EGz%nbKk zLc}NkHMTXs;p^US5HZI8P_uwuL_GiS&Ceso?X7JuclHg)EA?K%c2>X4=h()6F-!a^ zVoi>uos6aWHEiqez{|aZ7)!gFGv=;xl#jZ)yF3%Wb?n5?`5Pnt+Y!H8`rr90{^NIu tJ**RJ{wv&k2k#O8kMF}>^u;^df4Yg7{{h}!TV)JY;C(m79(gr;$bTO$F@XR8 diff --git a/example/shader.spv.txt b/example/shader.spv.txt index 8fc9453..a4965a5 100644 --- a/example/shader.spv.txt +++ b/example/shader.spv.txt @@ -1,18 +1,18 @@ Version 1.0 Generator: 2560130 -Bound: 45 +Bound: 51 Schema: 0 OpCapability Capability(Shader) OpCapability Capability(Float64) OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450) - OpEntryPoint ExecutionModel(Fragment) %17 "main" %6 - OpExecutionMode %17 ExecutionMode(OriginUpperLeft) + OpEntryPoint ExecutionModel(Fragment) %18 "main" %6 + OpExecutionMode %18 ExecutionMode(OriginUpperLeft) OpSource SourceLanguage(NZSL) 4198400 OpSourceExtension "Version: 1.1" OpName %7 "FragOut" OpMemberName %7 0 "color" OpName %6 "color" - OpName %17 "main" + OpName %18 "main" OpDecorate %6 Decoration(Location) 0 OpMemberDecorate %7 0 Decoration(Offset) 0 %1 = OpTypeVoid @@ -21,55 +21,65 @@ Schema: 0 %4 = OpTypeVector %3 4 %5 = OpTypePointer StorageClass(Output) %4 %7 = OpTypeStruct %4 - %8 = OpConstant %3 f32(0) + %8 = OpConstant %3 f32(1) %9 = OpTypePointer StorageClass(Function) %3 %10 = OpTypeInt 32 1 %11 = OpConstant %10 i32(1) %12 = OpTypePointer StorageClass(Function) %10 -%13 = OpConstant %10 i32(20) +%13 = OpConstant %10 i32(5) %14 = OpTypeBool -%15 = OpTypePointer StorageClass(Function) %7 -%16 = OpConstant %10 i32(0) -%42 = OpTypePointer StorageClass(Function) %4 +%15 = OpConstant %10 i32(3) +%16 = OpTypePointer StorageClass(Function) %7 +%17 = OpConstant %10 i32(0) +%48 = OpTypePointer StorageClass(Function) %4 %6 = OpVariable %5 StorageClass(Output) -%17 = OpFunction %1 FunctionControl(0) %2 -%18 = OpLabel -%19 = OpVariable %9 StorageClass(Function) -%20 = OpVariable %12 StorageClass(Function) +%18 = OpFunction %1 FunctionControl(0) %2 +%19 = OpLabel +%20 = OpVariable %9 StorageClass(Function) %21 = OpVariable %12 StorageClass(Function) -%22 = OpVariable %15 StorageClass(Function) - OpStore %19 %8 - OpStore %20 %11 - OpStore %21 %13 - OpBranch %23 -%23 = OpLabel -%27 = OpLoad %10 %20 -%28 = OpLoad %10 %21 -%29 = OpSLessThan %14 %27 %28 - OpLoopMerge %25 %26 LoopControl(0) - OpBranchConditional %29 %24 %25 +%22 = OpVariable %12 StorageClass(Function) +%23 = OpVariable %16 StorageClass(Function) + OpStore %20 %8 + OpStore %21 %11 + OpStore %22 %13 + OpBranch %24 %24 = OpLabel -%30 = OpLoad %3 %19 -%31 = OpLoad %10 %20 -%32 = OpConvertSToF %3 %31 -%33 = OpFMul %3 %30 %32 - OpStore %19 %33 -%34 = OpLoad %10 %20 -%35 = OpIAdd %10 %34 %11 - OpStore %20 %35 - OpBranch %26 -%26 = OpLabel - OpBranch %23 +%28 = OpLoad %10 %21 +%29 = OpLoad %10 %22 +%30 = OpSLessThan %14 %28 %29 + OpLoopMerge %26 %27 LoopControl(0) + OpBranchConditional %30 %25 %26 %25 = OpLabel -%36 = OpLoad %3 %19 -%37 = OpLoad %3 %19 -%38 = OpLoad %3 %19 -%39 = OpLoad %3 %19 -%40 = OpCompositeConstruct %4 %36 %37 %38 %39 -%41 = OpAccessChain %42 %22 %16 - OpStore %41 %40 -%43 = OpLoad %7 %22 -%44 = OpCompositeExtract %4 %43 0 - OpStore %6 %44 +%34 = OpLoad %10 %21 +%35 = OpIEqual %14 %34 %15 + OpSelectionMerge %31 SelectionControl(0) + OpBranchConditional %35 %32 %33 +%32 = OpLabel + OpBranch %27 +%33 = OpLabel + OpBranch %31 +%31 = OpLabel +%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 OpFunctionEnd diff --git a/test/loops.zig b/test/loops.zig new file mode 100644 index 0000000..05e6714 --- /dev/null +++ b/test/loops.zig @@ -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 }); +} diff --git a/test/root.zig b/test/root.zig index becad8d..4741193 100644 --- a/test/root.zig +++ b/test/root.zig @@ -60,5 +60,6 @@ test { std.testing.refAllDecls(@import("branching.zig")); std.testing.refAllDecls(@import("casts.zig")); std.testing.refAllDecls(@import("functions.zig")); + std.testing.refAllDecls(@import("loops.zig")); std.testing.refAllDecls(@import("maths.zig")); }