Skip to content

core: Remove main_window in favor of users creating their own windo… #1316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions examples/core-custom-entrypoint/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub const mach_module = .app;

pub const mach_systems = .{ .main, .init, .deinit, .tick };

window: mach.ObjectID,
title_timer: mach.time.Timer,
pipeline: *gpu.RenderPipeline,

Expand All @@ -28,18 +29,33 @@ pub fn init(
core.on_tick = app_mod.id.tick;
core.on_exit = app_mod.id.deinit;

const main_window = core.windows.getValue(core.main_window);
const window = try core.windows.new(.{ .title = "core-custom-entrypoint" });

// Store our render pipeline in our module's state, so we can access it later on.
app.* = .{
.window = window,
.title_timer = try mach.time.Timer.start(),
.pipeline = undefined,
};

// TODO(object): window-title
// try updateWindowTitle(core);
}

fn setupPipeline(core: *mach.Core, app: *App, window_id: mach.ObjectID) !void {
_ = window_id; // autofix
const window = core.windows.getValue(app.window);

// Create our shader module
const shader_module = main_window.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
const shader_module = window.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
defer shader_module.release();

// Blend state describes how rendered colors get blended
const blend = gpu.BlendState{};

// Color target describes e.g. the pixel format of the window we are rendering to.
const color_target = gpu.ColorTargetState{
.format = main_window.framebuffer_format,
.format = window.framebuffer_format,
.blend = &blend,
};

Expand All @@ -60,36 +76,31 @@ pub fn init(
.entry_point = "vertex_main",
},
};
const pipeline = main_window.device.createRenderPipeline(&pipeline_descriptor);

// Store our render pipeline in our module's state, so we can access it later on.
app.* = .{
.title_timer = try mach.time.Timer.start(),
.pipeline = pipeline,
};

// TODO(object): window-title
// try updateWindowTitle(core);
app.pipeline = window.device.createRenderPipeline(&pipeline_descriptor);
}

pub fn tick(core: *mach.Core, app: *App) !void {
while (core.nextEvent()) |event| {
switch (event) {
.window_open => |ev| {
try setupPipeline(core, app, ev.window_id);
},
.close => core.exit(),
else => {},
}
}

const main_window = core.windows.getValue(core.main_window);
const window = core.windows.getValue(app.window);

// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = main_window.swap_chain.getCurrentTextureView().?;
const back_buffer_view = window.swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();

// Create a command encoder
const label = @tagName(mach_module) ++ ".tick";
const encoder = main_window.device.createCommandEncoder(&.{ .label = label });
const encoder = window.device.createCommandEncoder(&.{ .label = label });
defer encoder.release();

// Begin render pass
Expand All @@ -116,7 +127,7 @@ pub fn tick(core: *mach.Core, app: *App) !void {
// Submit our commands to the queue
var command = encoder.finish(&.{ .label = label });
defer command.release();
main_window.queue.submit(&[_]*gpu.CommandBuffer{command});
window.queue.submit(&[_]*gpu.CommandBuffer{command});

// update the window title every second
if (app.title_timer.read() >= 1.0) {
Expand Down
48 changes: 30 additions & 18 deletions examples/core-triangle/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub const main = mach.schedule(.{
.{ mach.Core, .main },
});

window: mach.ObjectID,
title_timer: mach.time.Timer,
pipeline: *gpu.RenderPipeline,

Expand All @@ -25,18 +26,32 @@ pub fn init(
core.on_tick = app_mod.id.tick;
core.on_exit = app_mod.id.deinit;

const main_window = core.windows.getValue(core.main_window);
const window = try core.windows.new(.{
.title = "core-triangle",
});

// Store our render pipeline in our module's state, so we can access it later on.
app.* = .{
.window = window,
.title_timer = try mach.time.Timer.start(),
.pipeline = undefined,
};
}

fn setupPipeline(core: *mach.Core, app: *App, window_id: mach.ObjectID) !void {
var window = core.windows.getValue(window_id);
defer core.windows.setValueRaw(window_id, window);

// Create our shader module
const shader_module = main_window.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
const shader_module = window.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
defer shader_module.release();

// Blend state describes how rendered colors get blended
const blend = gpu.BlendState{};

// Color target describes e.g. the pixel format of the window we are rendering to.
const color_target = gpu.ColorTargetState{
.format = main_window.framebuffer_format,
.format = window.framebuffer_format,
.blend = &blend,
};

Expand All @@ -57,13 +72,7 @@ pub fn init(
.entry_point = "vertex_main",
},
};
const pipeline = main_window.device.createRenderPipeline(&pipeline_descriptor);

// Store our render pipeline in our module's state, so we can access it later on.
app.* = .{
.title_timer = try mach.time.Timer.start(),
.pipeline = pipeline,
};
app.pipeline = window.device.createRenderPipeline(&pipeline_descriptor);
}

// TODO(object): window-title
Expand All @@ -72,19 +81,22 @@ pub fn init(
pub fn tick(app: *App, core: *mach.Core) void {
while (core.nextEvent()) |event| {
switch (event) {
.window_open => |ev| {
try setupPipeline(core, app, ev.window_id);
},
.key_press => |ev| {
switch (ev.key) {
.right => {
core.windows.set(core.main_window, .width, core.windows.get(core.main_window, .width) + 10);
core.windows.set(app.window, .width, core.windows.get(app.window, .width) + 10);
},
.left => {
core.windows.set(core.main_window, .width, core.windows.get(core.main_window, .width) - 10);
core.windows.set(app.window, .width, core.windows.get(app.window, .width) - 10);
},
.up => {
core.windows.set(core.main_window, .height, core.windows.get(core.main_window, .height) + 10);
core.windows.set(app.window, .height, core.windows.get(app.window, .height) + 10);
},
.down => {
core.windows.set(core.main_window, .height, core.windows.get(core.main_window, .height) - 10);
core.windows.set(app.window, .height, core.windows.get(app.window, .height) - 10);
},
else => {},
}
Expand All @@ -94,17 +106,17 @@ pub fn tick(app: *App, core: *mach.Core) void {
}
}

const main_window = core.windows.getValue(core.main_window);
const window = core.windows.getValue(app.window);

// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = main_window.swap_chain.getCurrentTextureView().?;
const back_buffer_view = window.swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();

// Create a command encoder
const label = @tagName(mach_module) ++ ".tick";

const encoder = main_window.device.createCommandEncoder(&.{ .label = label });
const encoder = window.device.createCommandEncoder(&.{ .label = label });
defer encoder.release();

// Begin render pass
Expand All @@ -131,7 +143,7 @@ pub fn tick(app: *App, core: *mach.Core) void {
// Submit our commands to the queue
var command = encoder.finish(&.{ .label = label });
defer command.release();
main_window.queue.submit(&[_]*gpu.CommandBuffer{command});
window.queue.submit(&[_]*gpu.CommandBuffer{command});

// update the window title every second
// if (app.title_timer.read() >= 1.0) {
Expand Down
15 changes: 10 additions & 5 deletions examples/play-opus/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub const deinit = mach.schedule(.{
.{ mach.Audio, .deinit },
});

window: mach.ObjectID,
/// Tag object we set as a child of mach.Audio objects to indicate they are background music.
// TODO(object): consider adding a better object 'tagging' system?
bgm: mach.Objects(.{}, struct {}),
Expand All @@ -46,6 +47,10 @@ pub fn init(
core.on_tick = app_mod.id.tick;
core.on_exit = app_mod.id.deinit;

const window = try core.windows.new(.{
.title = "play-opus",
});

// Configure the audio module to send our app's .audio_state_change event when an entity's sound
// finishes playing.
audio.on_state_change = app_mod.id.audioStateChange;
Expand All @@ -61,7 +66,7 @@ pub fn init(
const sfx = try mach.Audio.Opus.decodeStream(allocator, sfx_sound_stream);

// Initialize module state
app.* = .{ .sfx = sfx, .bgm = app.bgm };
app.* = .{ .sfx = sfx, .bgm = app.bgm, .window = window };

const bgm_buffer = blk: {
audio.buffers.lock();
Expand Down Expand Up @@ -138,16 +143,16 @@ pub fn tick(
}
}

var main_window = core.windows.getValue(core.main_window);
var window = core.windows.getValue(app.window);

// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = main_window.swap_chain.getCurrentTextureView().?;
const back_buffer_view = window.swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();

// Create a command encoder
const label = @tagName(mach_module) ++ ".tick";
const encoder = main_window.device.createCommandEncoder(&.{ .label = label });
const encoder = window.device.createCommandEncoder(&.{ .label = label });
defer encoder.release();

// Begin render pass
Expand All @@ -172,5 +177,5 @@ pub fn tick(
// Submit our commands to the queue
var command = encoder.finish(&.{ .label = label });
defer command.release();
main_window.queue.submit(&[_]*gpu.CommandBuffer{command});
window.queue.submit(&[_]*gpu.CommandBuffer{command});
}
16 changes: 5 additions & 11 deletions src/Core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ on_tick: ?mach.FunctionID = null,
/// Callback system invoked when application is exiting
on_exit: ?mach.FunctionID = null,

/// Main window of the application
main_window: mach.ObjectID,

/// Current state of the application
state: enum {
running,
Expand All @@ -132,8 +129,6 @@ pub fn init(core: *Core) !void {
// TODO: fix all leaks and use options.allocator
try mach.sysgpu.Impl.init(allocator, .{});

const main_window = try core.windows.new(.{});

var events = EventQueue.init(allocator);
try events.ensureTotalCapacity(8192);

Expand All @@ -142,19 +137,13 @@ pub fn init(core: *Core) !void {
.windows = core.windows,

.allocator = allocator,
.main_window = main_window,
.events = events,
.input_state = .{},

.input = .{ .target = 0 },
.frame = .{ .target = 1 },
};

// Tick the platform so that the platform can grab the newly created window
// and perform initialization
// TODO: consider removing `main_window` and then this wont be necessary
try Platform.tick(core);

try core.frame.start();
try core.input.start();
}
Expand Down Expand Up @@ -229,6 +218,8 @@ pub fn initWindow(core: *Core, window_id: mach.ObjectID) !void {
core_window.framebuffer_format = core_window.swap_chain_descriptor.format;
core_window.framebuffer_width = core_window.swap_chain_descriptor.width;
core_window.framebuffer_height = core_window.swap_chain_descriptor.height;

core.pushEvent(.{ .window_open = .{ .window_id = window_id } });
}

pub fn tick(core: *Core, core_mod: mach.Mod(Core)) !void {
Expand Down Expand Up @@ -686,6 +677,9 @@ pub const Event = union(enum) {
yoffset: f32,
},
window_resize: ResizeEvent,
window_open: struct {
window_id: mach.ObjectID,
},
focus_gained: struct {
window_id: mach.ObjectID,
},
Expand Down
Loading