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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
const std = @import("std");
const ig = @import("cimgui_docking");
const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sglue = sokol.glue;
const sfetch = sokol.fetch;
const simgui = sokol.imgui;
const stbi = @cImport({
@cInclude("stb_image.h");
});
const shd = @import("shd/quad.glsl.zig");
const state = struct {
const MaxSprites = 100;
const MaxLights = 100;
var pass_action: sg.PassAction = .{};
var bind: sg.Bindings = .{};
var pip: sg.Pipeline = .{};
var pip_shadow: sg.Pipeline = .{};
var bind_light: sg.Bindings = .{};
var pip_light: sg.Pipeline = .{};
var img_buf: [1024 * 1024]u8 = undefined;
var cam = [2]f32{ -300, -300 };
var sprites_buf: [MaxSprites]Sprite = undefined;
var sprites = std.ArrayListUnmanaged(Sprite).initBuffer(&state.sprites_buf);
var lights_buf: [MaxLights]Light = undefined;
var lights = std.ArrayListUnmanaged(Light).initBuffer(&state.lights_buf);
};
const Sprite = struct {
texid: usize = shd.IMG_tex,
pos: [2]f32 = [_]f32{ 0, 0 },
size: [2]i32 = [_]i32{ 300, 300 },
};
const Light = struct {
pos: [2]f32 = [_]f32{ 0, 0 },
};
export fn init() void {
// initialize sokol-gfx
sg.setup(.{
.buffer_pool_size = 8,
.image_pool_size = 4,
.shader_pool_size = 8,
.pipeline_pool_size = 8,
.attachments_pool_size = 1,
.environment = sglue.environment(),
.logger = .{ .func = slog.func },
});
sfetch.setup(.{
.max_requests = 2,
.num_channels = 2,
.num_lanes = 1,
.logger = .{ .func = slog.func },
});
// initialize sokol-imgui
simgui.setup(.{
.logger = .{ .func = slog.func },
});
ig.igGetIO().*.ConfigFlags |= ig.ImGuiConfigFlags_DockingEnable;
// initial clear color
state.pass_action.colors[0] = .{
.load_action = .CLEAR,
.clear_value = .{ .r = 0.0, .g = 0.5, .b = 1.0, .a = 1.0 },
};
// default pass
state.bind.images[shd.IMG_tex] = sg.allocImage();
state.bind.samplers[shd.SMP_smp] = sg.makeSampler(.{
.min_filter = sg.Filter.NEAREST,
.mag_filter = sg.Filter.NEAREST,
});
// dynamic vertex buffer for instancing data
state.bind.vertex_buffers[0] = sg.makeBuffer(.{
.usage = .{ .stream_update = true },
.size = state.MaxSprites * @sizeOf(Sprite),
});
// a shader and pipeline state object
state.pip = sg.makePipeline(.{
.shader = sg.makeShader(shd.quadShaderDesc(sg.queryBackend())),
.layout = init: {
var l = sg.VertexLayoutState{};
l.buffers[0].step_func = .PER_INSTANCE;
l.attrs[shd.ATTR_quad_inst_pos] = .{ .format = .FLOAT2, .offset = @offsetOf(Sprite, "pos") };
l.attrs[shd.ATTR_quad_inst_size] = .{ .format = .INT2, .offset = @offsetOf(Sprite, "size") };
l.buffers[0].stride = @sizeOf(Sprite);
break :init l;
},
});
// shadow pass
state.pip_shadow = sg.makePipeline(.{
.shader = sg.makeShader(shd.shadowShaderDesc(sg.queryBackend())),
.layout = init: {
var l = sg.VertexLayoutState{};
l.buffers[0].step_func = .PER_INSTANCE;
l.attrs[shd.ATTR_shadow_inst_pos] = .{ .format = .FLOAT2, .offset = @offsetOf(Sprite, "pos") };
l.attrs[shd.ATTR_shadow_inst_size] = .{ .format = .INT2, .offset = @offsetOf(Sprite, "size") };
l.buffers[0].stride = @sizeOf(Sprite);
break :init l;
},
});
// light pass
state.bind_light.vertex_buffers[0] = sg.makeBuffer(.{
.usage = .{ .stream_update = true },
.size = state.MaxLights * @sizeOf(Light),
});
state.pip_light = sg.makePipeline(.{
.shader = sg.makeShader(shd.lightShaderDesc(sg.queryBackend())),
.layout = init: {
var l = sg.VertexLayoutState{};
l.buffers[0].step_func = .PER_INSTANCE;
l.attrs[shd.ATTR_light_light_pos] = .{ .format = .FLOAT2, .offset = @offsetOf(Light, "pos") };
l.buffers[0].stride = @sizeOf(Light);
break :init l;
},
});
// fetch resources
const FileCb = struct {
fn img(res: [*c]const sfetch.Response) callconv(.c) void {
if (res.*.finished) {
var w: c_int = 0;
var h: c_int = 0;
var n: c_int = 0;
const pixels = stbi.stbi_load_from_memory(@ptrCast(res.*.data.ptr.?), @intCast(res.*.data.size), &w, &h, &n, 4);
if (pixels != null) {
sg.initImage(state.bind.images[shd.IMG_tex], .{
.width = w,
.height = h,
.pixel_format = sg.PixelFormat.RGBA8,
.data = init: {
var i = sg.ImageData{};
i.subimage[0][0] = .{
.ptr = pixels,
.size = @intCast(w * h * 4),
};
break :init i;
},
});
}
}
}
};
_ = sfetch.send(.{
.path = "img/test.png",
.callback = FileCb.img,
.buffer = .{ .ptr = &state.img_buf, .size = state.img_buf.len },
});
state.sprites.appendAssumeCapacity(.{});
state.lights.appendAssumeCapacity(.{ .pos = [_]f32{ 150, -300 } });
}
fn ui() void {
if (ig.igBegin("Hello Dear ImGui!", null, ig.ImGuiWindowFlags_None)) {
_ = ig.igColorEdit3("Background", &state.pass_action.colors[0].clear_value.r, ig.ImGuiColorEditFlags_None);
_ = ig.igDragFloat2("cam", &state.cam);
{
ig.igPushIDPtr(state.sprites.items.ptr);
if (ig.igButton("+")) {
_ = state.sprites.appendAssumeCapacity(.{});
}
var i: u16 = 0;
while (i < state.sprites.items.len) {
var sprite = &state.sprites.items[i];
i += 1;
ig.igPushIDInt(i);
ig.igPushItemWidth(100);
_ = ig.igDragFloat2("pos", &sprite.pos);
ig.igSameLine();
_ = ig.igDragInt2("size", &sprite.size);
ig.igSameLine();
if (ig.igButton("-")) {
i -= 1;
_ = state.sprites.swapRemove(i);
}
ig.igPopID();
}
ig.igPopID();
}
{
ig.igPushIDPtr(state.lights.items.ptr);
if (ig.igButton("+")) {
_ = state.lights.appendAssumeCapacity(.{});
}
var i: u16 = 0;
while (i < state.lights.items.len) {
var light = &state.lights.items[i];
i += 1;
ig.igPushIDInt(i);
ig.igPushItemWidth(100);
_ = ig.igDragFloat2("pos", &light.pos);
ig.igSameLine();
if (ig.igButton("-")) {
i -= 1;
_ = state.lights.swapRemove(i);
}
ig.igPopID();
}
ig.igPopID();
}
ig.igEnd();
}
}
export fn frame() void {
sfetch.dowork();
const width = sapp.width();
const height = sapp.height();
const widthf = sapp.widthf();
const heightf = sapp.heightf();
// call simgui.newFrame() before any ImGui calls
simgui.newFrame(.{
.width = width,
.height = height,
.delta_time = sapp.frameDuration(),
.dpi_scale = sapp.dpiScale(),
});
ui();
// update instance vertex buffer
sg.updateBuffer(state.bind.vertex_buffers[0], sg.asRange(state.sprites.items));
sg.updateBuffer(state.bind_light.vertex_buffers[0], sg.asRange(state.lights.items));
sg.beginPass(.{ .action = state.pass_action, .swapchain = sglue.swapchain() });
// shadow pass
sg.applyPipeline(state.pip_shadow);
sg.applyBindings(state.bind);
sg.applyUniforms(shd.UB_Game, sg.asRange(&shd.Game{
.screen = [_]f32{ widthf, heightf },
.cam = state.cam,
}));
for (state.lights.items) |light| {
sg.applyUniforms(shd.UB_Light, sg.asRange(&shd.Light{
.light_pos = light.pos,
}));
sg.draw(0, 6, @truncate(state.sprites.items.len));
}
// default pass
sg.applyPipeline(state.pip);
sg.applyBindings(state.bind);
sg.applyUniforms(shd.UB_Game, sg.asRange(&shd.Game{
.screen = [_]f32{ widthf, heightf },
.cam = state.cam,
}));
sg.draw(0, 6, @truncate(state.sprites.items.len));
// light pass
sg.applyPipeline(state.pip_light);
sg.applyBindings(state.bind_light);
sg.applyUniforms(shd.UB_Game, sg.asRange(&shd.Game{
.screen = [_]f32{ widthf, heightf },
.cam = state.cam,
}));
sg.draw(0, 6, @truncate(state.lights.items.len));
simgui.render();
sg.endPass();
sg.commit();
}
export fn cleanup() void {
simgui.shutdown();
sfetch.shutdown();
sg.shutdown();
}
export fn event(ev: [*c]const sapp.Event) void {
// forward input events to sokol-imgui
_ = simgui.handleEvent(ev.*);
}
pub fn main() void {
sapp.run(.{
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.event_cb = event,
.window_title = "sokol-zig + Dear Imgui",
.width = 800,
.height = 600,
.icon = .{ .sokol_default = true },
.logger = .{ .func = slog.func },
});
}
|