summaryrefslogtreecommitdiff
path: root/examples/shaders/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'examples/shaders/src/main.zig')
-rw-r--r--examples/shaders/src/main.zig92
1 files changed, 92 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,
+ };
+}