diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index b1694c1..450ce7c 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -17,10 +17,10 @@ jobs: steps: - uses: actions/checkout@v4 - - uses: https://codeberg.org/mlugg/setup-zig@v2 - - uses: actions/setup-node@v6 with: - node-version: 24 + fetch-depth: 0 + + - uses: https://codeberg.org/mlugg/setup-zig@v2 - name: Building run: zig build -Dno-example=true @@ -28,10 +28,16 @@ jobs: - name: Generating docs run: zig build docs -Dno-example=true - - name: Publish to Cloudflare Pages - uses: cloudflare/wrangler-action@v3 + - name: Deploying docs + if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }} + uses: milanmk/actions-file-deployer@master with: - apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} - accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - command: pages deploy zig-out/docs --project-name=spirv-interpreter-docs - gitHubToken: ${{ secrets.GITHUB_TOKEN }} + remote-protocol: sftp + remote-host: ${{ secrets.SFTP_HOST_DOCS }} + remote-user: ${{ secrets.SFTP_USER_DOCS }} + remote-password: ${{ secrets.SFTP_PASSWORD_DOCS }} + remote-port: 6969 + local-path: "./zig-out/docs" + remote-path: "/www" + sync: full + diff --git a/src/Module.zig b/src/Module.zig index 2937a67..8884912 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -9,8 +9,6 @@ const SpvByte = spv.SpvByte; const SpvWord = spv.SpvWord; const SpvBool = spv.SpvBool; -const SpvBinding = spv.SpvBinding; - const Result = @import("Result.zig"); const Runtime = @import("Runtime.zig"); const Value = @import("Value.zig").Value; @@ -30,6 +28,12 @@ const SpvEntryPoint = struct { globals: []SpvWord, }; +const BindingEntry = struct { + set: SpvWord, + binding: SpvWord, + result: SpvWord, +}; + pub const ModuleError = error{ InvalidSpirV, InvalidMagic, @@ -73,7 +77,7 @@ geometry_output: SpvWord, input_locations: [lib.SPIRV_MAX_INPUT_LOCATIONS]SpvWord, output_locations: [lib.SPIRV_MAX_OUTPUT_LOCATIONS]SpvWord, -bindings: [lib.SPIRV_MAX_SET][lib.SPIRV_MAX_SET_BINDINGS]SpvWord, +bindings: std.ArrayList(BindingEntry), builtins: std.EnumMap(spv.SpvBuiltIn, SpvWord), pub fn init(allocator: std.mem.Allocator, source: []const SpvWord, options: ModuleOptions) ModuleError!Self { @@ -82,12 +86,14 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord, options: Modu .code = allocator.dupe(SpvWord, source) catch return ModuleError.OutOfMemory, .extensions = std.ArrayList([]const u8).empty, .entry_points = std.ArrayList(SpvEntryPoint).empty, + .bindings = std.ArrayList(BindingEntry).empty, .capabilities = std.EnumSet(spv.SpvCapability).initEmpty(), .local_size_x = 1, .local_size_y = 1, .local_size_z = 1, }); errdefer allocator.free(self.code); + errdefer self.bindings.deinit(allocator); op.initRuntimeDispatcher(); @@ -125,7 +131,7 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord, options: Modu _ = self.it.skip(); // Skip schema try self.pass(allocator); // Setup pass - try self.applyDecorations(); + try self.applyDecorations(allocator); return self; } @@ -256,13 +262,13 @@ fn applyStructMemberInterfaceDecorations(self: *Self, storage_class: spv.SpvStor } } -fn applyDecorations(self: *Self) ModuleError!void { +fn applyDecorations(self: *Self, allocator: std.mem.Allocator) ModuleError!void { for (self.results, 0..) |result, id| { if (result.variant == null) continue; - var set: ?usize = null; - var binding: ?usize = null; + var set: ?SpvWord = null; + var binding: ?SpvWord = null; for (result.decorations.items) |decoration| { if (result.variant) |*variant| switch (variant.*) { @@ -320,11 +326,24 @@ fn applyDecorations(self: *Self) ModuleError!void { }; if (set != null and binding != null) { - self.bindings[set.?][binding.?] = @intCast(id); + self.bindings.append(allocator, .{ + .set = set.?, + .binding = binding.?, + .result = @intCast(id), + }) catch return ModuleError.OutOfMemory; } } } +pub fn getBindingResult(self: *const Self, set: SpvWord, binding: SpvWord) ?SpvWord { + for (self.bindings.items) |entry| { + if (entry.set == set and entry.binding == binding) { + return entry.result; + } + } + return null; +} + pub fn deinit(self: *Self, allocator: std.mem.Allocator) void { allocator.free(self.code); for (self.entry_points.items) |entry| { @@ -337,6 +356,7 @@ pub fn deinit(self: *Self, allocator: std.mem.Allocator) void { allocator.free(ext); } self.extensions.deinit(allocator); + self.bindings.deinit(allocator); for (self.results) |*result| { result.deinit(allocator); diff --git a/src/Runtime.zig b/src/Runtime.zig index 07a49d7..ad7ee16 100644 --- a/src/Runtime.zig +++ b/src/Runtime.zig @@ -246,7 +246,9 @@ pub fn continueEntryPoint(self: *Self, allocator: std.mem.Allocator) RuntimeErro fn pass(self: *Self, allocator: std.mem.Allocator, op_set: ?std.EnumSet(spv.SpvOp)) RuntimeError!void { self.it.did_jump = false; // To reset function jump while (self.it.nextOrNull()) |opcode_data| { - const word_count = ((opcode_data & (~spv.SpvOpCodeMask)) >> spv.SpvWordCountShift) - 1; + const word_count_with_header = (opcode_data & (~spv.SpvOpCodeMask)) >> spv.SpvWordCountShift; + if (word_count_with_header == 0) return RuntimeError.InvalidSpirV; + const word_count = word_count_with_header - 1; const opcode = (opcode_data & spv.SpvOpCodeMask); if (op_set) |set| { @@ -282,22 +284,19 @@ pub fn populatePushConstants(self: *Self, blob: []const u8) RuntimeError!void { } pub fn writeDescriptorSet(self: *const Self, input: []const u8, set: SpvWord, binding: SpvWord, descriptor_index: SpvWord) RuntimeError!void { - if (set < lib.SPIRV_MAX_SET and binding < lib.SPIRV_MAX_SET_BINDINGS) { - const value = &(self.results[self.mod.bindings[set][binding]].variant orelse return).Variable.value; - switch (value.*) { - .Array => |arr| { - if (descriptor_index >= arr.values.len) - return RuntimeError.NotFound; - _ = try arr.values[descriptor_index].write(input); - }, - else => { - if (descriptor_index != 0) - return RuntimeError.NotFound; - _ = try value.write(input); - }, - } - } else { - return RuntimeError.NotFound; + const result = self.mod.getBindingResult(set, binding) orelse return RuntimeError.NotFound; + const value = &(self.results[result].variant orelse return).Variable.value; + switch (value.*) { + .Array => |arr| { + if (descriptor_index >= arr.values.len) + return RuntimeError.NotFound; + _ = try arr.values[descriptor_index].write(input); + }, + else => { + if (descriptor_index != 0) + return RuntimeError.NotFound; + _ = try value.write(input); + }, } } diff --git a/src/Value.zig b/src/Value.zig index db90d37..9a691d6 100644 --- a/src/Value.zig +++ b/src/Value.zig @@ -113,7 +113,9 @@ pub const Value = union(Type) { type_word: SpvWord, driver_image: *anyopaque, }, - Sampler: struct {}, + Sampler: struct { + driver_sampler: *anyopaque, + }, SampledImage: struct { type_word: SpvWord, driver_image: *anyopaque, @@ -255,7 +257,11 @@ pub const Value = union(Type) { .driver_image = undefined, }, }, - .Sampler => RuntimeError.ToDo, + .Sampler => .{ + .Sampler = .{ + .driver_sampler = undefined, + }, + }, .SampledImage => .{ .SampledImage = .{ .type_word = target_type, @@ -487,6 +493,7 @@ pub const Value = union(Type) { }, .RuntimeArray => |*arr| arr.data = @constCast(input[0..]), .Image => |*img| img.driver_image = @ptrFromInt(std.mem.bytesToValue(usize, input[0..])), + .Sampler => |*sampler| sampler.driver_sampler = @ptrFromInt(std.mem.bytesToValue(usize, input[0..])), .SampledImage => |*img| { img.driver_image = @ptrFromInt(std.mem.bytesToValue(usize, input[0..])); img.driver_sampler = @ptrFromInt(std.mem.bytesToValue(usize, input[@sizeOf(usize)..])); diff --git a/src/opcodes.zig b/src/opcodes.zig index ccdcf57..5e90d87 100644 --- a/src/opcodes.zig +++ b/src/opcodes.zig @@ -217,6 +217,7 @@ pub const SetupDispatcher = block: { .SMulExtended = autoSetupConstant, .SNegate = autoSetupConstant, .SRem = autoSetupConstant, + .SampledImage = autoSetupConstant, .SatConvertSToU = autoSetupConstant, .SatConvertUToS = autoSetupConstant, .Select = autoSetupConstant, @@ -229,6 +230,7 @@ pub const SetupDispatcher = block: { .TypeFloat = opTypeFloat, .TypeFunction = opTypeFunction, .TypeImage = opTypeImage, + .TypeSampler = opTypeSampler, .TypeSampledImage = opTypeSampledImage, .TypeInt = opTypeInt, .TypeMatrix = opTypeMatrix, @@ -334,6 +336,7 @@ pub fn initRuntimeDispatcher() void { runtime_dispatcher[@intFromEnum(spv.SpvOp.ImageSampleImplicitLod)] = ImageEngine(.SampleImplicitLod).op; runtime_dispatcher[@intFromEnum(spv.SpvOp.ImageTexelPointer)] = opImageTexelPointer; runtime_dispatcher[@intFromEnum(spv.SpvOp.ImageWrite)] = ImageEngine(.Write).op; + runtime_dispatcher[@intFromEnum(spv.SpvOp.SampledImage)] = opSampledImage; runtime_dispatcher[@intFromEnum(spv.SpvOp.InBoundsAccessChain)] = opAccessChain; runtime_dispatcher[@intFromEnum(spv.SpvOp.IsFinite)] = CondEngine(.Float, .IsNan).op; runtime_dispatcher[@intFromEnum(spv.SpvOp.IsInf)] = CondEngine(.Float, .IsInf).op; @@ -1134,6 +1137,14 @@ fn ImageEngine(comptime Op: ImageOp) type { fn readSampleCoordLane(coord: *const Value, lane_index: usize) RuntimeError!f32 { return switch (coord.*) { + .Float => |f| { + if (lane_index != 0) return RuntimeError.OutOfBounds; + return f.value.float32; + }, + .Int => |i| { + if (lane_index != 0) return RuntimeError.OutOfBounds; + return if (i.is_signed) @floatFromInt(i.value.sint32) else @floatFromInt(i.value.uint32); + }, .Vector => |lanes| { if (lane_index >= lanes.len) return RuntimeError.OutOfBounds; return readSampleCoordLane(&lanes[lane_index], 0); @@ -3790,6 +3801,36 @@ fn opTypeSampledImage(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeErr }; } +fn opTypeSampler(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void { + const id = try rt.it.next(); + rt.results[id].variant = .{ + .Type = .{ + .Sampler = .{}, + }, + }; +} + +fn opSampledImage(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void { + const type_word = try rt.it.next(); + const dst = try rt.results[try rt.it.next()].getValue(); + const image = try rt.results[try rt.it.next()].getValue(); + const sampler = try rt.results[try rt.it.next()].getValue(); + + dst.* = .{ + .SampledImage = .{ + .type_word = type_word, + .driver_image = switch (image.*) { + .Image => |img| img.driver_image, + else => return RuntimeError.InvalidSpirV, + }, + .driver_sampler = switch (sampler.*) { + .Sampler => |s| s.driver_sampler, + else => return RuntimeError.InvalidSpirV, + }, + }, + }; +} + fn opTypeInt(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void { const id = try rt.it.next(); rt.results[id].variant = .{ @@ -3979,7 +4020,7 @@ fn opVariable(allocator: std.mem.Allocator, word_count: SpvWord, rt: *Runtime) R .storage_class = storage_class, .type_word = resolved_word, .type = resolved_type, - .value = try Value.init(allocator, rt.results, resolved_word, is_externally_visible), + .value = try Value.init(allocator, rt.results, resolved_word, is_externally_visible and resolved_type != .Array), }, };