From 1e6f844d91bfe188855db62a787216fd1124c2a4 Mon Sep 17 00:00:00 2001 From: Fabio Arnold Date: Fri, 17 May 2024 11:19:52 +0200 Subject: [PATCH 1/2] Add plasma effect --- showcase/carts/plasma/build.zig | 19 ++++++ showcase/carts/plasma/build.zig.zon | 12 ++++ showcase/carts/plasma/src/plasma.zig | 99 ++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 showcase/carts/plasma/build.zig create mode 100644 showcase/carts/plasma/build.zig.zon create mode 100644 showcase/carts/plasma/src/plasma.zig diff --git a/showcase/carts/plasma/build.zig b/showcase/carts/plasma/build.zig new file mode 100644 index 0000000..88bed9d --- /dev/null +++ b/showcase/carts/plasma/build.zig @@ -0,0 +1,19 @@ +const std = @import("std"); +const sycl_badge = @import("sycl_badge"); + +pub const author_name = "Fabio Arnold"; +pub const author_handle = "fabioarnold"; +pub const cart_title = "plasma"; +pub const description = "Plasma effect - computes plasma blobs and cycles their hue"; + +pub fn build(b: *std.Build) void { + const optimize = b.standardOptimizeOption(.{}); + const sycl_badge_dep = b.dependency("sycl_badge", .{}); + + const cart = sycl_badge.add_cart(sycl_badge_dep, b, .{ + .name = "plasma", + .optimize = optimize, + .root_source_file = b.path("src/plasma.zig"), + }); + cart.install(b); +} diff --git a/showcase/carts/plasma/build.zig.zon b/showcase/carts/plasma/build.zig.zon new file mode 100644 index 0000000..f048245 --- /dev/null +++ b/showcase/carts/plasma/build.zig.zon @@ -0,0 +1,12 @@ +.{ + .name = "plasma", + .version = "0.0.0", + .dependencies = .{ + .sycl_badge = .{ .path = "../../.." }, + }, + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + }, +} diff --git a/showcase/carts/plasma/src/plasma.zig b/showcase/carts/plasma/src/plasma.zig new file mode 100644 index 0000000..aa23b9b --- /dev/null +++ b/showcase/carts/plasma/src/plasma.zig @@ -0,0 +1,99 @@ +const std = @import("std"); +const cart = @import("cart-api"); +const gfx = @import("gfx"); + +export fn start() void {} + +export fn update() void { + updatePlasma(); +} + +const hue_colors = init_hue_colors: { + var initial_hue_colors: [256]cart.DisplayColor = undefined; + for (0..initial_hue_colors.len) |i| { + const fi: f32 = @floatFromInt(i); + const rgb = hsv2rgb(fi * 360.0 / 256.0, 1, 1); + initial_hue_colors[i] = .{ + .r = @intFromFloat(rgb.r * 31), + .g = @intFromFloat(rgb.g * 63), + .b = @intFromFloat(rgb.b * 31), + }; + } + break :init_hue_colors initial_hue_colors; +}; + +var plasma_buffer = init_plasma_buffer: { + @setEvalBranchQuota(100000); + var initial_plasma_buffer: [cart.framebuffer.len]u8 = undefined; + var y: usize = 0; + while (y < cart.screen_height) : (y += 1) { + var x: usize = 0; + while (x < cart.screen_width) : (x += 1) { + const fx: f32 = @floatFromInt(x); + const fy: f32 = @floatFromInt(y); + var value: f32 = @sin(fx / 16.0); + value += @sin(fy / 8.0); + value += @sin((fx + fy) / 16.0); + value += @sin(@sqrt(fx * fx + fy * fy) / 8.0); + // shift range from -4 .. 4 to 0 .. 255 + value = std.math.clamp((value + 4) * 32, 0, 255); + initial_plasma_buffer[y * cart.screen_width + x] = @intFromFloat(value); + } + } + break :init_plasma_buffer initial_plasma_buffer; +}; + +fn updatePlasma() void { + for (0..cart.framebuffer.len) |i| { + plasma_buffer[i] +%= 2; + cart.framebuffer[i] = hue_colors[plasma_buffer[i]]; + } +} + +const RGB = struct { + r: f32, + g: f32, + b: f32, +}; + +pub fn hsv2rgb(h: f32, s: f32, v: f32) RGB { + const c = v * s; + const x = c * (1 - @abs(@mod(h / 60.0, 2) - 1)); + const m = v - c; + + var r: f32 = 0.0; + var g: f32 = 0.0; + var b: f32 = 0.0; + + if (h < 60.0) { + r = c; + g = x; + b = 0.0; + } else if (h < 120.0) { + r = x; + g = c; + b = 0.0; + } else if (h < 180.0) { + r = 0.0; + g = c; + b = x; + } else if (h < 240.0) { + r = 0.0; + g = x; + b = c; + } else if (h < 300.0) { + r = x; + g = 0.0; + b = c; + } else { + r = c; + g = 0.0; + b = x; + } + + return RGB{ + .r = r + m, + .g = g + m, + .b = b + m, + }; +} From 250e50a6dd6d4dc45cca4d4e8bcd75acfc11cd32 Mon Sep 17 00:00:00 2001 From: Fabio Arnold Date: Fri, 17 May 2024 11:27:44 +0200 Subject: [PATCH 2/2] Add plasma to build.zig and build.zig.zon --- showcase/build.zig | 1 + showcase/build.zig.zon | 1 + 2 files changed, 2 insertions(+) diff --git a/showcase/build.zig b/showcase/build.zig index 194cca3..7561461 100644 --- a/showcase/build.zig +++ b/showcase/build.zig @@ -6,6 +6,7 @@ const root = @import("root"); const carts = .{ .{ "zeroman", @import("zeroman") }, .{ "blobs", @import("blobs") }, + .{ "plasma", @import("plasma") }, }; pub fn build(b: *std.Build) void { diff --git a/showcase/build.zig.zon b/showcase/build.zig.zon index 8872b81..fe133dc 100644 --- a/showcase/build.zig.zon +++ b/showcase/build.zig.zon @@ -12,6 +12,7 @@ // Carts go here .zeroman = .{ .path = "carts/zeroman" }, .blobs = .{ .path = "carts/blobs" }, + .plasma = .{ .path = "carts/plasma" }, }, .paths = .{ "README.md",