1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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,
};
}
|