diff --git a/build.zig b/build.zig index 214a7be..8dfe6a2 100644 --- a/build.zig +++ b/build.zig @@ -64,7 +64,7 @@ pub fn build(b: *std.Build) void { // Zig sandbox setup const sandbox_exe = b.addExecutable(.{ - .name = "spirv_interpreter_sandbpx", + .name = "spirv_interpreter_sandbox", .root_module = b.createModule(.{ .root_source_file = b.path("sandbox/main.zig"), .target = target, diff --git a/sandbox/main.zig b/sandbox/main.zig index eaabab5..03a23f0 100644 --- a/sandbox/main.zig +++ b/sandbox/main.zig @@ -26,24 +26,21 @@ pub fn main() !void { var ssbo: SSBO = .{}; - try rt.writeDescriptorSet(allocator, std.mem.asBytes(&ssbo), 0, 0); - for (0..16) |i| { for (0..16) |x| { - for (0..16) |y| { - const global_invocation_indices = [3]i32{ - @as(i32, @intCast(i * 16 + x)), - @as(i32, @intCast(y)), - 1, - }; + const global_invocation_indices = [3]i32{ + @as(i32, @intCast(i * 16 + x)), + 1, + 1, + }; - try rt.writeBuiltIn(std.mem.asBytes(&global_invocation_indices), .GlobalInvocationId); - rt.callEntryPoint(allocator, entry) catch |err| switch (err) { - spv.Runtime.RuntimeError.OutOfBounds => continue, - else => return err, - }; - try rt.readDescriptorSet(std.mem.asBytes(&ssbo), 0, 0); - } + try rt.writeBuiltIn(std.mem.asBytes(&global_invocation_indices), .GlobalInvocationId); + try rt.writeDescriptorSet(allocator, std.mem.asBytes(&ssbo), 0, 0); + rt.callEntryPoint(allocator, entry) catch |err| switch (err) { + spv.Runtime.RuntimeError.OutOfBounds => continue, + else => return err, + }; + try rt.readDescriptorSet(std.mem.asBytes(&ssbo), 0, 0); } } diff --git a/sandbox/shader.nzsl b/sandbox/shader.nzsl index 6dbc17b..7ac2ab0 100644 --- a/sandbox/shader.nzsl +++ b/sandbox/shader.nzsl @@ -18,8 +18,8 @@ external } [entry(compute)] -[workgroup(16, 16, 1)] +[workgroup(16, 1, 1)] fn main(input: Input) { - ssbo.data[input.indices.x * input.indices.y] = i32(input.indices.x * input.indices.y); + ssbo.data[input.indices.x] = i32(input.indices.x); } diff --git a/sandbox/shader.spv b/sandbox/shader.spv index bc1bdc8..8f72e42 100644 Binary files a/sandbox/shader.spv and b/sandbox/shader.spv differ diff --git a/sandbox/shader.spv.txt b/sandbox/shader.spv.txt index 784b4e1..a1a6cc4 100644 --- a/sandbox/shader.spv.txt +++ b/sandbox/shader.spv.txt @@ -1,11 +1,11 @@ Version 1.0 Generator: 2560130 -Bound: 41 +Bound: 32 Schema: 0 OpCapability Capability(Shader) OpMemoryModel AddressingModel(Logical) MemoryModel(GLSL450) - OpEntryPoint ExecutionModel(GLCompute) %18 "main" %11 - OpExecutionMode %18 ExecutionMode(LocalSize) 16 16 1 + OpEntryPoint ExecutionModel(GLCompute) %17 "main" %11 + OpExecutionMode %17 ExecutionMode(LocalSize) 16 1 1 OpSource SourceLanguage(NZSL) 4198400 OpSourceExtension "Version: 1.1" OpName %3 "SSBO" @@ -14,7 +14,7 @@ Schema: 0 OpMemberName %14 0 "indices" OpName %5 "ssbo" OpName %11 "global_invocation_indices" - OpName %18 "main" + OpName %17 "main" OpDecorate %5 Decoration(Binding) 0 OpDecorate %5 Decoration(DescriptorSet) 0 OpDecorate %11 Decoration(BuiltIn) BuiltIn(GlobalInvocationId) @@ -36,33 +36,24 @@ Schema: 0 %14 = OpTypeStruct %9 %15 = OpTypePointer StorageClass(Function) %14 %16 = OpTypeRuntimeArray %1 -%17 = OpConstant %1 i32(1) -%31 = OpTypePointer StorageClass(Uniform) %2 -%40 = OpTypePointer StorageClass(Uniform) %1 +%26 = OpTypePointer StorageClass(Uniform) %2 +%31 = OpTypePointer StorageClass(Uniform) %1 %5 = OpVariable %4 StorageClass(Uniform) %11 = OpVariable %10 StorageClass(Input) -%18 = OpFunction %6 FunctionControl(0) %7 -%19 = OpLabel -%20 = OpVariable %15 StorageClass(Function) -%21 = OpAccessChain %13 %20 %12 - OpCopyMemory %21 %11 -%22 = OpAccessChain %13 %20 %12 -%23 = OpLoad %9 %22 -%24 = OpCompositeExtract %8 %23 0 -%25 = OpAccessChain %13 %20 %12 -%26 = OpLoad %9 %25 -%27 = OpCompositeExtract %8 %26 1 -%28 = OpIMul %8 %24 %27 -%29 = OpBitcast %1 %28 -%30 = OpAccessChain %31 %5 %12 -%32 = OpAccessChain %13 %20 %12 -%33 = OpLoad %9 %32 -%34 = OpCompositeExtract %8 %33 0 -%35 = OpAccessChain %13 %20 %12 -%36 = OpLoad %9 %35 -%37 = OpCompositeExtract %8 %36 1 -%38 = OpIMul %8 %34 %37 -%39 = OpAccessChain %40 %30 %38 - OpStore %39 %29 +%17 = OpFunction %6 FunctionControl(0) %7 +%18 = OpLabel +%19 = OpVariable %15 StorageClass(Function) +%20 = OpAccessChain %13 %19 %12 + OpCopyMemory %20 %11 +%21 = OpAccessChain %13 %19 %12 +%22 = OpLoad %9 %21 +%23 = OpCompositeExtract %8 %22 0 +%24 = OpBitcast %1 %23 +%25 = OpAccessChain %26 %5 %12 +%27 = OpAccessChain %13 %19 %12 +%28 = OpLoad %9 %27 +%29 = OpCompositeExtract %8 %28 0 +%30 = OpAccessChain %31 %25 %29 + OpStore %30 %24 OpReturn OpFunctionEnd diff --git a/src/Module.zig b/src/Module.zig index df18c6d..575985a 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -212,7 +212,8 @@ fn populateMaps(self: *Self) ModuleError!void { if (result.variant == null or std.meta.activeTag(result.variant.?) != .Variable) continue; - var current_set: usize = 0; + var set: ?usize = null; + var binding: ?usize = null; for (result.decorations.items) |decoration| { switch (result.variant.?.Variable.storage_class) { @@ -235,14 +236,17 @@ fn populateMaps(self: *Self) ModuleError!void { .UniformConstant, => { switch (decoration.rtype) { - .Binding => self.bindings[current_set][decoration.literal_1] = @intCast(id), - .DescriptorSet => current_set = decoration.literal_1, + .Binding => binding = decoration.literal_1, + .DescriptorSet => set = decoration.literal_1, else => {}, } }, else => {}, } } + if (set != null and binding != null) { + self.bindings[set.?][binding.?] = @intCast(id); + } } }