Skip to content

Commit adf9bef

Browse files
This is a cry for help
1 parent cfd9fc8 commit adf9bef

12 files changed

Lines changed: 156 additions & 33 deletions

File tree

examples/custom-renderer/Renderer.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn init(
5656
const uniform_buffer = device.createBuffer(&.{
5757
.label = label ++ " uniform buffer",
5858
.usage = .{ .copy_dst = true, .uniform = true },
59-
.size = @sizeOf(UniformBufferObject) * uniform_offset * num_bind_groups,
59+
.size = ((@sizeOf(UniformBufferObject) / uniform_offset) + 1) * uniform_offset * num_bind_groups,
6060
.mapped_at_creation = .false,
6161
});
6262

@@ -122,8 +122,9 @@ pub fn renderFrame(
122122

123123
// Grab the back buffer of the swapchain
124124
// TODO(Core)
125-
const back_buffer_view = window.swap_chain.getCurrentTextureView().?;
126-
defer back_buffer_view.release();
125+
const back_buffer_view = window.swap_chain.getCurrentTextureView();
126+
if (back_buffer_view == null) return;
127+
defer back_buffer_view.?.release();
127128

128129
// Create a command encoder
129130
const label = @tagName(mach_module) ++ ".tick";
@@ -146,7 +147,7 @@ pub fn renderFrame(
146147
// Begin render pass
147148
const sky_blue_background = gpu.Color{ .r = 0.776, .g = 0.988, .b = 1, .a = 1 };
148149
const color_attachments = [_]gpu.RenderPassColorAttachment{.{
149-
.view = back_buffer_view,
150+
.view = back_buffer_view.?,
150151
.clear_value = sky_blue_background,
151152
.load_op = .clear,
152153
.store_op = .store,

src/Core.zig

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const EventQueue = std.fifo.LinearFifo(Event, .Dynamic);
1212

1313
pub const mach_module = .mach_core;
1414

15-
pub const mach_systems = .{ .main, .init, .tick, .presentFrame, .deinit };
15+
pub const mach_systems = .{ .main, .init, .tick, .presentFrame, .renewSwapChain, .deinit };
1616

1717
// Set track_fields to true so that when these field values change, we know about it
1818
// and can update the platform windows.
@@ -249,6 +249,27 @@ pub fn presentFrame(core: *Core, core_mod: mach.Mod(Core)) !void {
249249
.deinitializing => {},
250250
.exited => @panic("application not running"),
251251
}
252+
core_mod.call(.renewSwapChain);
253+
}
254+
255+
pub fn renewSwapChain(core: *Core) !void {
256+
var windows = core.windows.slice();
257+
while (windows.next()) |window_id| {
258+
var core_window = core.windows.getValue(window_id);
259+
if (core_window.swap_chain.isStale() or
260+
core.windows.updated(window_id, .width) or
261+
core.windows.updated(window_id, .height))
262+
{
263+
core_window.framebuffer_width = core_window.width;
264+
core_window.framebuffer_height = core_window.height;
265+
core_window.swap_chain_descriptor.height = core_window.framebuffer_height;
266+
core_window.swap_chain_descriptor.width = core_window.framebuffer_width;
267+
268+
core_window.swap_chain.destroy();
269+
core_window.swap_chain = core_window.device.createSwapChain(core_window.surface, &core_window.swap_chain_descriptor);
270+
core.windows.setValueRaw(window_id, core_window);
271+
}
272+
}
252273
}
253274

254275
pub fn main(core: *Core, core_mod: mach.Mod(Core)) !void {

src/core/Linux.zig

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ pub fn tick(core: *Core) !void {
7272
if (core.windows.updated(window_id, .title)) {
7373
setTitle(&native, core_window.title);
7474
}
75+
if (core.windows.updated(window_id, .display_mode) or core.windows.updated(window_id, .decorated)) {
76+
setDisplayMode(&native, core_window.display_mode, core_window.decorated);
77+
setBorder(&native, core_window.decorated);
78+
}
7579
// check for display server events
7680
switch (native) {
7781
.x11 => try X11.tick(window_id),
@@ -178,16 +182,14 @@ fn setTitle(native: *const Native, title: [:0]const u8) void {
178182
}
179183
}
180184

181-
pub fn setDisplayMode(linux: *Linux, display_mode: DisplayMode) void {
182-
// const old_display_mode = linux.display_mode;
183-
linux.display_mode = display_mode;
184-
switch (linux.backend) {
185-
.wayland => linux.backend.wayland.setDisplayMode(display_mode),
186-
.x11 => linux.backend.x11.setDisplayMode(linux, display_mode),
185+
fn setDisplayMode(native: *const Native, display_mode: DisplayMode, decorated: bool) void {
186+
switch (native.*) {
187+
.wayland => Wayland.setDisplayMode(&native.wayland, display_mode),
188+
.x11 => X11.setDisplayMode(&native.x11, display_mode, decorated),
187189
}
188190
}
189191

190-
pub fn setBorder(_: *Linux, _: bool) void {
192+
fn setBorder(_: *const Native, _: bool) void {
191193
return;
192194
}
193195

src/core/linux/Wayland.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ pub fn setTitle(wl: *const Native, title: [:0]const u8) void {
240240
c.xdg_toplevel_set_title(wl.toplevel, title);
241241
}
242242

243-
pub fn setDisplayMode(wl: *Wayland, display_mode: DisplayMode) void {
243+
pub fn setDisplayMode(wl: *const Native, display_mode: DisplayMode) void {
244244
_ = wl;
245245
_ = display_mode;
246246
}

src/core/linux/X11.zig

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,20 +266,20 @@ pub fn setDisplayMode(x11: *const Native, display_mode: DisplayMode, border: boo
266266
@ptrCast(atoms.slice()),
267267
@intCast(atoms.len),
268268
);
269-
x11.setFullscreen(false);
270-
x11.setDecorated(border);
271-
x11.setFloating(false);
269+
setFullscreen(x11, false);
270+
setDecorated(x11, border);
271+
setFloating(x11, false);
272272
_ = libx11.?.XMapWindow(x11.display, x11.window);
273273
_ = libx11.?.XFlush(x11.display);
274274
},
275275
.fullscreen => {
276-
x11.setFullscreen(true);
276+
setFullscreen(x11, true);
277277
_ = libx11.?.XFlush(x11.display);
278278
},
279279
.fullscreen_borderless => {
280-
x11.setDecorated(false);
281-
x11.setFloating(true);
282-
x11.setFullscreen(false);
280+
setDecorated(x11, false);
281+
setFloating(x11, true);
282+
setFullscreen(x11, false);
283283
_ = libx11.?.XResizeWindow(
284284
x11.display,
285285
x11.window,
@@ -294,7 +294,7 @@ pub fn setDisplayMode(x11: *const Native, display_mode: DisplayMode, border: boo
294294
fn setFullscreen(x11: *const Native, enabled: bool) void {
295295
const wm_state = libx11.?.XInternAtom(x11.display, "_NET_WM_STATE", c.False);
296296
const wm_fullscreen = libx11.?.XInternAtom(x11.display, "_NET_WM_STATE_FULLSCREEN", c.False);
297-
x11.sendEventToWM(wm_state, &.{ @intFromBool(enabled), @intCast(wm_fullscreen), 0, 1 });
297+
sendEventToWM(x11, wm_state, &.{ @intFromBool(enabled), @intCast(wm_fullscreen), 0, 1 });
298298
// Force composition OFF to reduce overhead
299299
const compositing_disable_on: c_long = @intFromBool(enabled);
300300
const bypass_compositor = libx11.?.XInternAtom(x11.display, "_NET_WM_BYPASS_COMPOSITOR", c.False);
@@ -318,7 +318,7 @@ fn setFloating(x11: *const Native, enabled: bool) void {
318318
const net_wm_state_remove = 0;
319319
const net_wm_state_add = 1;
320320
const action: c_long = if (enabled) net_wm_state_add else net_wm_state_remove;
321-
x11.sendEventToWM(wm_state, &.{ action, @intCast(wm_above), 0, 1 });
321+
sendEventToWM(x11, wm_state, &.{ action, @intCast(wm_above), 0, 1 });
322322
}
323323

324324
fn sendEventToWM(x11: *const Native, message_type: c.Atom, data: []const c_long) void {
@@ -703,9 +703,6 @@ fn processEvent(window_id: mach.ObjectID, event: *c.XEvent) void {
703703
core_window.width = @intCast(event.xconfigure.width);
704704
core_window.height = @intCast(event.xconfigure.height);
705705

706-
// FIX: What is the Mach Object System way of doing this?
707-
// core_ptr.swap_chain_update.set();
708-
709706
core_ptr.pushEvent(.{
710707
.window_resize = .{
711708
.size = Core.Size{

src/sysgpu/d3d12.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,15 @@ pub const SwapChain = struct {
15331533
try queue.signal();
15341534
swapchain.fence_values[swapchain.buffer_index] = queue.fence_value;
15351535
}
1536+
1537+
pub fn isStale(sc: *SwapChain) bool {
1538+
_ = sc;
1539+
return false;
1540+
}
1541+
1542+
pub fn setStale(sc: *SwapChain) void {
1543+
_ = sc;
1544+
}
15361545
};
15371546

15381547
pub const Resource = struct {

src/sysgpu/main.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,11 @@ pub const Impl = sysgpu.Interface(struct {
12501250
@panic("unimplemented");
12511251
}
12521252

1253+
pub inline fn swapChainDestroy(swap_chain_raw: *sysgpu.SwapChain) void {
1254+
const swap_chain: *impl.SwapChain = @ptrCast(@alignCast(swap_chain_raw));
1255+
swap_chain.deinit();
1256+
}
1257+
12531258
pub inline fn swapChainGetCurrentTexture(swap_chain: *sysgpu.SwapChain) ?*sysgpu.Texture {
12541259
_ = swap_chain;
12551260
@panic("unimplemented");
@@ -1261,6 +1266,11 @@ pub const Impl = sysgpu.Interface(struct {
12611266
return @ptrCast(texture_view);
12621267
}
12631268

1269+
pub inline fn swapChainIsStale(swap_chain_raw: *sysgpu.SwapChain) bool {
1270+
const swap_chain: *impl.SwapChain = @ptrCast(@alignCast(swap_chain_raw));
1271+
return swap_chain.isStale();
1272+
}
1273+
12641274
pub inline fn swapChainPresent(swap_chain_raw: *sysgpu.SwapChain) void {
12651275
const swap_chain: *impl.SwapChain = @ptrCast(@alignCast(swap_chain_raw));
12661276
swap_chain.present() catch @panic("api error");
@@ -1276,6 +1286,11 @@ pub const Impl = sysgpu.Interface(struct {
12761286
swap_chain.manager.release();
12771287
}
12781288

1289+
pub inline fn swapChainSetStale(swap_chain_raw: *sysgpu.SwapChain) void {
1290+
const swap_chain: *impl.SwapChain = @ptrCast(@alignCast(swap_chain_raw));
1291+
return swap_chain.setStale();
1292+
}
1293+
12791294
pub inline fn textureCreateView(texture_raw: *sysgpu.Texture, descriptor: ?*const sysgpu.TextureView.Descriptor) *sysgpu.TextureView {
12801295
const texture: *impl.Texture = @ptrCast(@alignCast(texture_raw));
12811296
const texture_view = texture.createView(descriptor orelse &sysgpu.TextureView.Descriptor{}) catch @panic("api error");

src/sysgpu/metal.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ pub const Device = struct {
269269
return Texture.init(device, desc);
270270
}
271271

272+
pub fn destroySwapChain(swapchain: *const sysgpu.SwapChain.Descriptor) void {
273+
return SwapChain.deinit(swapchain);
274+
}
275+
272276
pub fn getQueue(device: *Device) !*Queue {
273277
if (device.queue == null) {
274278
device.queue = try Queue.init(device);
@@ -493,6 +497,15 @@ pub const SwapChain = struct {
493497
}
494498
}
495499
}
500+
501+
pub fn isStale(sc: *SwapChain) bool {
502+
_ = sc;
503+
return false;
504+
}
505+
506+
pub fn setStale(sc: *SwapChain) void {
507+
_ = sc;
508+
}
496509
};
497510

498511
pub const Buffer = struct {

src/sysgpu/opengl.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,15 @@ pub const SwapChain = struct {
636636
if (c.SwapBuffers(swapchain.hdc) == c.FALSE)
637637
return error.SwapBuffersFailed;
638638
}
639+
640+
pub fn isStale(sc: *SwapChain) bool {
641+
_ = sc;
642+
return false;
643+
}
644+
645+
pub fn setStale(sc: *SwapChain) void {
646+
_ = sc;
647+
}
639648
};
640649

641650
pub const Buffer = struct {

src/sysgpu/sysgpu/interface.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ pub fn Interface(comptime T: type) type {
275275
assertDecl(T, "swapChainPresent", fn (swap_chain: *sysgpu.SwapChain) callconv(.Inline) void);
276276
assertDecl(T, "swapChainReference", fn (swap_chain: *sysgpu.SwapChain) callconv(.Inline) void);
277277
assertDecl(T, "swapChainRelease", fn (swap_chain: *sysgpu.SwapChain) callconv(.Inline) void);
278+
assertDecl(T, "swapChainDestroy", fn (swap_chain: *sysgpu.SwapChain) callconv(.Inline) void);
278279

279280
// sysgpu.Texture
280281
assertDecl(T, "textureCreateView", fn (texture: *sysgpu.Texture, descriptor: ?*const sysgpu.TextureView.Descriptor) callconv(.Inline) *sysgpu.TextureView);
@@ -1271,6 +1272,11 @@ pub fn Export(comptime T: type) type {
12711272
T.surfaceRelease(surface);
12721273
}
12731274

1275+
// SYSGPU_EXPORT WGPUSwapChain sysgpuSwapChainDestroy(WGPUSwapChain const * swapchain);
1276+
export fn sysgpuSwapChainDestroy(swap_chain: *sysgpu.SwapChain) void {
1277+
return T.swapChainDestroy(swap_chain);
1278+
}
1279+
12741280
// SYSGPU_EXPORT WGPUTexture sysgpuSwapChainGetCurrentTexture(WGPUSwapChain swapChain);
12751281
export fn sysgpuSwapChainGetCurrentTexture(swap_chain: *sysgpu.SwapChain) ?*sysgpu.Texture {
12761282
return T.swapChainGetCurrentTexture(swap_chain);
@@ -2581,6 +2587,11 @@ pub const StubInterface = Interface(struct {
25812587
unreachable;
25822588
}
25832589

2590+
pub inline fn swapChainDestroy(swap_chain: *sysgpu.SwapChain) void {
2591+
_ = swap_chain;
2592+
unreachable;
2593+
}
2594+
25842595
pub inline fn swapChainGetCurrentTexture(swap_chain: *sysgpu.SwapChain) ?*sysgpu.Texture {
25852596
_ = swap_chain;
25862597
unreachable;
@@ -2591,6 +2602,11 @@ pub const StubInterface = Interface(struct {
25912602
unreachable;
25922603
}
25932604

2605+
pub inline fn swapChainIsStale(swap_chain: *sysgpu.SwapChain) bool {
2606+
_ = swap_chain;
2607+
unreachable;
2608+
}
2609+
25942610
pub inline fn swapChainPresent(swap_chain: *sysgpu.SwapChain) void {
25952611
_ = swap_chain;
25962612
unreachable;
@@ -2606,6 +2622,11 @@ pub const StubInterface = Interface(struct {
26062622
unreachable;
26072623
}
26082624

2625+
pub inline fn swapChainSetStale(swap_chain: *sysgpu.SwapChain) void {
2626+
_ = swap_chain;
2627+
unreachable;
2628+
}
2629+
26092630
pub inline fn textureCreateView(texture: *sysgpu.Texture, descriptor: ?*const sysgpu.TextureView.Descriptor) *sysgpu.TextureView {
26102631
_ = texture;
26112632
_ = descriptor;

0 commit comments

Comments
 (0)