Skip to content

Commit

Permalink
Upgraded Rollup examples to production version of Zigar
Browse files Browse the repository at this point in the history
  • Loading branch information
chung-leong committed Jun 1, 2024
1 parent a0d42ae commit e5dbcf6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 121 deletions.
24 changes: 12 additions & 12 deletions rollup-plugin-zigar/demos/rollup/filter/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rollup-plugin-zigar/demos/rollup/filter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"@rollup/plugin-node-resolve": "^15.2.3",
"http-server": "^14.1.1",
"rollup": "^4.16.4",
"rollup-plugin-zigar": "^0.11.2-alpha.2"
"rollup-plugin-zigar": "^0.12.0"
}
}
99 changes: 17 additions & 82 deletions rollup-plugin-zigar/demos/rollup/filter/zig/sepia.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ pub const kernel = struct {
const ar = @typeInfo(@TypeOf(m1)).Array;
var t1: @TypeOf(m1) = undefined;
inline for (m1, 0..) |column, c| {
comptime var r = 0;
inline while (r < ar.len) : (r += 1) {
inline for (0..ar.len) |r| {
t1[r][c] = column[r];
}
}
Expand All @@ -125,9 +124,6 @@ pub const Input = KernelInput(u8, kernel);
pub const Output = KernelOutput(u8, kernel);
pub const Parameters = KernelParameters(kernel);

// support both 0.11 and 0.12
const enum_auto = if (@hasField(std.builtin.Type.ContainerLayout, "Auto")) .Auto else .auto;

pub fn createOutput(allocator: std.mem.Allocator, width: u32, height: u32, input: Input, params: Parameters) !Output {
return createPartialOutput(allocator, width, height, 0, height, input, params);
}
Expand Down Expand Up @@ -207,37 +203,12 @@ pub fn Image(comptime T: type, comptime len: comptime_int, comptime writable: bo
}

fn pbPixelFromIntPixel(pixel: Pixel) FPixel {
const numerator: FPixel = switch (@hasDecl(std.math, "fabs")) {
// Zig 0.12.0
false => switch (len) {
1 => @floatFromInt(@shuffle(T, pixel, undefined, @Vector(1, i32){0})),
2 => @floatFromInt(@shuffle(T, pixel, undefined, @Vector(2, i32){ 0, 3 })),
3 => @floatFromInt(@shuffle(T, pixel, undefined, @Vector(3, i32){ 0, 1, 2 })),
4 => @floatFromInt(pixel),
else => @compileError("Unsupported number of channels: " ++ len),
},
// Zig 0.11.0
true => switch (len) {
1 => .{
@floatFromInt(pixel[0]),
},
2 => .{
@floatFromInt(pixel[0]),
@floatFromInt(pixel[3]),
},
3 => .{
@floatFromInt(pixel[0]),
@floatFromInt(pixel[1]),
@floatFromInt(pixel[2]),
},
4 => .{
@floatFromInt(pixel[0]),
@floatFromInt(pixel[1]),
@floatFromInt(pixel[2]),
@floatFromInt(pixel[3]),
},
else => @compileError("Unsupported number of channels: " ++ len),
},
const numerator: FPixel = switch (len) {
1 => @floatFromInt(@shuffle(T, pixel, undefined, @Vector(1, i32){0})),
2 => @floatFromInt(@shuffle(T, pixel, undefined, @Vector(2, i32){ 0, 3 })),
3 => @floatFromInt(@shuffle(T, pixel, undefined, @Vector(3, i32){ 0, 1, 2 })),
4 => @floatFromInt(pixel),
else => @compileError("Unsupported number of channels: " ++ len),
};
const denominator: FPixel = @splat(@floatFromInt(std.math.maxInt(T)));
return numerator / denominator;
Expand All @@ -248,43 +219,12 @@ pub fn Image(comptime T: type, comptime len: comptime_int, comptime writable: bo
const multiplier: FPixel = @splat(max);
const product: FPixel = constrain(pixel * multiplier, 0, max);
const maxAlpha: @Vector(1, f32) = .{std.math.maxInt(T)};
return switch (@hasDecl(std.math, "fabs")) {
// Zig 0.12.0
false => switch (len) {
1 => @intFromFloat(@shuffle(f32, product, maxAlpha, @Vector(4, i32){ 0, 0, 0, -1 })),
2 => @intFromFloat(@shuffle(f32, product, undefined, @Vector(4, i32){ 0, 0, 0, 1 })),
3 => @intFromFloat(@shuffle(f32, product, maxAlpha, @Vector(4, i32){ 0, 1, 2, -1 })),
4 => @intFromFloat(product),
else => @compileError("Unsupported number of channels: " ++ len),
},
// Zig 0.11.0
true => switch (len) {
1 => .{
@intFromFloat(product[0]),
@intFromFloat(product[0]),
@intFromFloat(product[0]),
maxAlpha[0],
},
2 => .{
@intFromFloat(product[0]),
@intFromFloat(product[0]),
@intFromFloat(product[0]),
@intFromFloat(product[1]),
},
3 => .{
@intFromFloat(product[0]),
@intFromFloat(product[1]),
@intFromFloat(product[2]),
maxAlpha[0],
},
4 => .{
@intFromFloat(product[0]),
@intFromFloat(product[1]),
@intFromFloat(product[2]),
@intFromFloat(product[3]),
},
else => @compileError("Unsupported number of channels: " ++ len),
},
return switch (len) {
1 => @intFromFloat(@shuffle(f32, product, maxAlpha, @Vector(4, i32){ 0, 0, 0, -1 })),
2 => @intFromFloat(@shuffle(f32, product, undefined, @Vector(4, i32){ 0, 0, 0, 1 })),
3 => @intFromFloat(@shuffle(f32, product, maxAlpha, @Vector(4, i32){ 0, 1, 2, -1 })),
4 => @intFromFloat(product),
else => @compileError("Unsupported number of channels: " ++ len),
};
}

Expand Down Expand Up @@ -326,12 +266,7 @@ pub fn Image(comptime T: type, comptime len: comptime_int, comptime writable: bo
const left_top: @Vector(2, f32) = .{ 0, 0 };
const bottom_right: @Vector(2, f32) = .{ @floatFromInt(self.width - 1), @floatFromInt(self.height - 1) };
if (@reduce(.And, coord >= left_top) and @reduce(.And, coord <= bottom_right)) {
const ic: @Vector(2, u32) = switch (@hasDecl(std.math, "fabs")) {
// Zig 0.12.0
false => @intFromFloat(coord),
// Zig 0.11.0
true => .{ @intFromFloat(coord[0]), @intFromFloat(coord[1]) },
};
const ic: @Vector(2, u32) = @intFromFloat(coord);
return self.getPixel(ic[0], ic[1]);
} else {
return @splat(0);
Expand Down Expand Up @@ -385,7 +320,7 @@ pub fn KernelInput(comptime T: type, comptime Kernel: type) type {
}
return @Type(.{
.Struct = .{
.layout = enum_auto,
.layout = .auto,
.fields = &struct_fields,
.decls = &.{},
.is_tuple = false,
Expand All @@ -410,7 +345,7 @@ pub fn KernelOutput(comptime T: type, comptime Kernel: type) type {
}
return @Type(.{
.Struct = .{
.layout = enum_auto,
.layout = .auto,
.fields = &struct_fields,
.decls = &.{},
.is_tuple = false,
Expand Down Expand Up @@ -444,7 +379,7 @@ pub fn KernelParameters(comptime Kernel: type) type {
}
return @Type(.{
.Struct = .{
.layout = enum_auto,
.layout = .auto,
.fields = &struct_fields,
.decls = &.{},
.is_tuple = false,
Expand Down
24 changes: 12 additions & 12 deletions rollup-plugin-zigar/demos/rollup/hello/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rollup-plugin-zigar/demos/rollup/hello/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"rollup": "^4.13.0",
"rollup-plugin-zigar": "^0.11.2-alpha.2"
"rollup-plugin-zigar": "^0.12.0"
}
}
24 changes: 12 additions & 12 deletions rollup-plugin-zigar/demos/rollup/sha1/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rollup-plugin-zigar/demos/rollup/sha1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"rollup": "^4.13.0",
"rollup-plugin-zigar": "^0.11.2-alpha.2"
"rollup-plugin-zigar": "^0.12.0"
}
}

0 comments on commit e5dbcf6

Please sign in to comment.