diff --git a/example/shader.nzsl b/example/shader.nzsl index 0058b30..08cb5eb 100644 --- a/example/shader.nzsl +++ b/example/shader.nzsl @@ -4,19 +4,17 @@ module; struct FragOut { - [location(0)] color: vec4[i32] -} - -fn fibonacci(n: i32) -> i32 -{ - if (n <= i32(1)) return n; - return fibonacci(n - i32(1)) + fibonacci(n - i32(2)); + [location(0)] color: vec4[f32] } [entry(frag)] fn main() -> FragOut { + let value: f32 = 0.0; + for i in 1 -> 20 { + value *= f32(i); + } let output: FragOut; - output.color = vec4[i32](fibonacci(2), fibonacci(2), fibonacci(2), fibonacci(2)); + output.color = vec4[f32](value, value, value, value); return output; } diff --git a/example/shader.spv b/example/shader.spv index bc61751..860d8c9 100644 Binary files a/example/shader.spv and b/example/shader.spv differ diff --git a/example/shader.spv.txt b/example/shader.spv.txt index 1a1ffb5..8fc9453 100644 --- a/example/shader.spv.txt +++ b/example/shader.spv.txt @@ -1,82 +1,75 @@ Version 1.0 Generator: 2560130 -Bound: 49 +Bound: 45 Schema: 0 OpCapability Capability(Shader) OpCapability Capability(Float64) OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450) - OpEntryPoint ExecutionModel(Fragment) %16 "main" %11 - OpExecutionMode %16 ExecutionMode(OriginUpperLeft) + OpEntryPoint ExecutionModel(Fragment) %17 "main" %6 + OpExecutionMode %17 ExecutionMode(OriginUpperLeft) OpSource SourceLanguage(NZSL) 4198400 OpSourceExtension "Version: 1.1" - OpName %12 "FragOut" - OpMemberName %12 0 "color" - OpName %11 "color" - OpName %15 "fibonacci" - OpName %16 "main" - OpDecorate %11 Decoration(Location) 0 - OpMemberDecorate %12 0 Decoration(Offset) 0 - %1 = OpTypeInt 32 1 - %2 = OpTypePointer StorageClass(Function) %1 - %3 = OpTypeFunction %1 %2 - %4 = OpConstant %1 i32(1) - %5 = OpTypeBool - %6 = OpConstant %1 i32(2) - %7 = OpTypeVoid - %8 = OpTypeFunction %7 - %9 = OpTypeVector %1 4 -%10 = OpTypePointer StorageClass(Output) %9 -%12 = OpTypeStruct %9 -%13 = OpTypePointer StorageClass(Function) %12 -%14 = OpConstant %1 i32(0) -%46 = OpTypePointer StorageClass(Function) %9 -%11 = OpVariable %10 StorageClass(Output) -%15 = OpFunction %1 FunctionControl(0) %3 -%17 = OpFunctionParameter %2 + OpName %7 "FragOut" + OpMemberName %7 0 "color" + OpName %6 "color" + OpName %17 "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 = OpConstant %3 f32(0) + %9 = OpTypePointer StorageClass(Function) %3 +%10 = OpTypeInt 32 1 +%11 = OpConstant %10 i32(1) +%12 = OpTypePointer StorageClass(Function) %10 +%13 = OpConstant %10 i32(20) +%14 = OpTypeBool +%15 = OpTypePointer StorageClass(Function) %7 +%16 = OpConstant %10 i32(0) +%42 = OpTypePointer StorageClass(Function) %4 + %6 = OpVariable %5 StorageClass(Output) +%17 = OpFunction %1 FunctionControl(0) %2 %18 = OpLabel -%19 = OpVariable %2 StorageClass(Function) -%20 = OpVariable %2 StorageClass(Function) -%24 = OpLoad %1 %17 -%25 = OpSLessThanEqual %5 %24 %4 - OpSelectionMerge %21 SelectionControl(0) - OpBranchConditional %25 %22 %23 -%22 = OpLabel -%26 = OpLoad %1 %17 - OpReturnValue %26 +%19 = OpVariable %9 StorageClass(Function) +%20 = OpVariable %12 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 - OpBranch %21 -%21 = OpLabel -%27 = OpLoad %1 %17 -%28 = OpISub %1 %27 %4 - OpStore %19 %28 -%29 = OpFunctionCall %1 %15 %19 -%30 = OpLoad %1 %17 -%31 = OpISub %1 %30 %6 - OpStore %20 %31 -%32 = OpFunctionCall %1 %15 %20 -%33 = OpIAdd %1 %29 %32 - OpReturnValue %33 - OpFunctionEnd -%16 = OpFunction %7 FunctionControl(0) %8 -%34 = OpLabel -%35 = OpVariable %13 StorageClass(Function) -%36 = OpVariable %2 StorageClass(Function) -%37 = OpVariable %2 StorageClass(Function) -%38 = OpVariable %2 StorageClass(Function) -%39 = OpVariable %2 StorageClass(Function) - OpStore %36 %6 -%40 = OpFunctionCall %1 %15 %36 - OpStore %37 %6 -%41 = OpFunctionCall %1 %15 %37 - OpStore %38 %6 -%42 = OpFunctionCall %1 %15 %38 - OpStore %39 %6 -%43 = OpFunctionCall %1 %15 %39 -%44 = OpCompositeConstruct %9 %40 %41 %42 %43 -%45 = OpAccessChain %46 %35 %14 - OpStore %45 %44 -%47 = OpLoad %12 %35 -%48 = OpCompositeExtract %9 %47 0 - OpStore %11 %48 +%27 = OpLoad %10 %20 +%28 = OpLoad %10 %21 +%29 = OpSLessThan %14 %27 %28 + OpLoopMerge %25 %26 LoopControl(0) + OpBranchConditional %29 %24 %25 +%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 +%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 OpReturn OpFunctionEnd diff --git a/test/functions.zig b/test/functions.zig index 967d2c4..96f3a39 100644 --- a/test/functions.zig +++ b/test/functions.zig @@ -98,65 +98,3 @@ test "Nested function calls" { try case.expectOutput(T, 4, code, "color", &.{ n, n, n, n }); } } - -test "Recursive function calls" { - const allocator = std.testing.allocator; - const types = [_]type{ i32, u32, f32, f64 }; - - inline for (types) |T| { - const iterations = 10; - - const fib = struct { - fn onacci(n: T) T { - if (n <= 0) return n; - return onacci(n - 1) + onacci(n - 2); - } - }; - const expected = fib.onacci(iterations); - - const shader = try std.fmt.allocPrint( - allocator, - \\ [nzsl_version("1.1")] - \\ [feature(float64)] - \\ module; - \\ - \\ struct FragOut - \\ {{ - \\ [location(0)] color: vec4[{s}] - \\ }} - \\ - \\ fn fibonacci(n: {s}) -> {s} - \\ {{ - \\ if (n <= {s}(1)) return n; - \\ return fibonacci(n - {s}(1)) + fibonacci(n - {s}(2)); - \\ }} - \\ - \\ [entry(frag)] - \\ fn main() -> FragOut - \\ {{ - \\ let output: FragOut; - \\ output.color = vec4[{s}](fibonacci({d}), fibonacci({d}), fibonacci({d}), fibonacci({d})); - \\ return output; - \\ }} - , - .{ - @typeName(T), - @typeName(T), - @typeName(T), - @typeName(T), - @typeName(T), - @typeName(T), - @typeName(T), - iterations, - iterations, - iterations, - iterations, - }, - ); - defer allocator.free(shader); - std.debug.print("{s}\n\n", .{shader}); - const code = try compileNzsl(allocator, shader); - defer allocator.free(code); - try case.expectOutput(T, 4, code, "color", &.{ expected, expected, expected, expected }); - } -}