summaryrefslogtreecommitdiff
path: root/examples/shaders/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 21:28:20 -0300
committerGabriel Schneider <[email protected]>2026-11-16 21:28:20 -0300
commit6933f99ddb2d7103515d26838496309a807bba26 (patch)
tree991c2fe7d4a57957222876e190fa4d16a0fb889c /examples/shaders/src
parent82fca5c9f29124f37f6dcd08e73e6999ce44a69d (diff)
downloadsketchbook-6933f99ddb2d7103515d26838496309a807bba26.tar.gz
sketchbook-6933f99ddb2d7103515d26838496309a807bba26.zip
Move standalone sketches into sketchbook examples
Diffstat (limited to 'examples/shaders/src')
-rw-r--r--examples/shaders/src/main.zig92
-rw-r--r--examples/shaders/src/uniforms.zig21
2 files changed, 113 insertions, 0 deletions
diff --git a/examples/shaders/src/main.zig b/examples/shaders/src/main.zig
new file mode 100644
index 0000000..bcbe160
--- /dev/null
+++ b/examples/shaders/src/main.zig
@@ -0,0 +1,92 @@
+//! The Book of Shaders, a few examples, each in Zig and in GLSL: left and
+//! right (or a click) change example, `l` the language. The pictures should
+//! match.
+const std = @import("std");
+const sk = @import("sketch");
+const gpu = sk.abi.gpu;
+const Uniforms = @import("uniforms").Uniforms;
+const zig = @import("zig_shaders");
+const glsl = @import("glsl_shaders");
+
+pub const name = "shaders";
+pub const width = 600;
+pub const height = 600;
+
+const examples = .{ "expo", "hsb_wheel", "polar", "truchet", "clouds" };
+
+pub const Language = enum { zig, glsl };
+
+pub const State = struct {
+ example: u8 = 0,
+ language: Language = .zig,
+ /// Seconds, from recorded ticks: replays see the same time.
+ time: f32 = 0,
+};
+
+pub const Session = struct {
+ image: sk.Image = .none,
+ size: [2]u32 = .{ 0, 0 },
+};
+
+pub fn update(s: *State, in: *const sk.Input) void {
+ const n: u8 = examples.len;
+ for (in.events) |e| switch (e.kind) {
+ .tick => s.time += e.x,
+ .mouse_down => s.example = (s.example + 1) % n,
+ else => {},
+ };
+ if (in.pressed(sk.Key.right)) s.example = (s.example + 1) % n;
+ if (in.pressed(sk.Key.left)) s.example = (s.example + n - 1) % n;
+ if (in.pressed(sk.Key.l)) s.language = if (s.language == .zig) .glsl else .zig;
+}
+
+pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
+ const in = f.in;
+ if (!f.host.gpu()) {
+ f.clear(0x000000);
+ f.text(8, 8, 2, "no GPU here", 0xffffff);
+ return;
+ }
+ const size: [2]u32 = .{ @intFromFloat(in.width), @intFromFloat(in.height) };
+ if (ss.image == .none or !std.meta.eql(ss.size, size)) {
+ if (ss.image != .none) f.host.freeImage(ss.image);
+ ss.image = f.host.newImage(size[0], size[1]);
+ ss.size = size;
+ }
+ const uniforms: Uniforms = .{
+ .resolution = .{ in.width, in.height },
+ .mouse = .{ in.mouse_x, in.height - in.mouse_y },
+ .time = s.time,
+ };
+ f.gpu(.{
+ .label = "book of shaders",
+ .image = ss.image,
+ .width = size[0],
+ .height = size[1],
+ .pipelines = &.{pipelines[@intFromEnum(s.language)][s.example]},
+ .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, uniforms) }},
+ });
+ f.image(ss.image, .{ .x = 0, .y = 0, .w = in.width, .h = in.height }, .{});
+ f.print(8, 8, 2, 0xff8040, "{s} in {t}", .{ names[s.example], s.language });
+}
+
+const names: [examples.len][]const u8 = examples;
+
+const pipelines = blk: {
+ var p: [2][examples.len]gpu.Pipeline = undefined;
+ for (examples, 0..) |example, i| {
+ p[@intFromEnum(Language.zig)][i] = pipeline(zig.fullscreen, @field(zig, example));
+ p[@intFromEnum(Language.glsl)][i] = pipeline(glsl.fullscreen_vert, @field(glsl, example ++ "_frag"));
+ }
+ break :blk p;
+};
+
+fn pipeline(vertex: []const u8, fragment: []const u8) gpu.Pipeline {
+ return .{
+ .vertex = .{ .code = vertex },
+ .fragment = .{ .code = fragment, .uniform_buffers = 1 },
+ .stride = 0,
+ .attributes = &.{},
+ .blend = .none,
+ };
+}
diff --git a/examples/shaders/src/uniforms.zig b/examples/shaders/src/uniforms.zig
new file mode 100644
index 0000000..5810e46
--- /dev/null
+++ b/examples/shaders/src/uniforms.zig
@@ -0,0 +1,21 @@
+//! What every fragment shader here reads: the one definition, imported by
+//! the sketch, by the Zig shaders, and written out as GLSL for the others.
+const builtin = @import("builtin");
+
+/// GLSL's vec2. On the GPU a vector, which SPIR-V lays out as vec2 (an
+/// array in a uniform block gets a 16-byte stride under std140); on the
+/// CPU an array, since extern structs cannot hold vectors there.
+pub const vec2 = if (builtin.cpu.arch.isSpirV()) @Vector(2, f32) else [2]f32;
+
+pub const Uniforms = extern struct {
+ /// Pixels.
+ resolution: vec2,
+ /// Pixels, y up as in GL.
+ mouse: vec2,
+ /// Seconds.
+ time: f32,
+ pad0: f32 = 0,
+ pad1: vec2 = .{ 0, 0 },
+
+ pub const glsl = .{ .uniform = .{ .set = 3, .instance = "u" } };
+};