Skip to content

Commit

Permalink
obj: Move Platform and InitOptions fields into core.windows (#1309
Browse files Browse the repository at this point in the history
)

* obj: Make field tracking use a single bitset

* obj: module: fix comment

* obj: Move `Platform` state and `InitOptions` fields into `core.windows`, initial push, only triangle example working on macos currently

* obj: `get` and `getValue` (renamed `getAll`) now do not return optionals, comment revisions, `device` is no longer optional, `native` is optional

* core: Lots of cleanup of unnecessary comments

* core: `Event`s now all contain `window_id`, darwin/windows: event functions now send window id

* core: comments, examples: fix `core-custom-entrypoint`
  • Loading branch information
foxnne authored Nov 30, 2024
1 parent b4e2da1 commit 1fe47b2
Show file tree
Hide file tree
Showing 7 changed files with 669 additions and 628 deletions.
16 changes: 10 additions & 6 deletions examples/core-custom-entrypoint/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ 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);

// Create our shader module
const shader_module = core.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
const shader_module = main_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 = core.windows.get(core.main_window, .framebuffer_format).?,
.format = main_window.framebuffer_format,
.blend = &blend,
};

Expand All @@ -58,7 +60,7 @@ pub fn init(
.entry_point = "vertex_main",
},
};
const pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
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.* = .{
Expand All @@ -78,14 +80,16 @@ pub fn tick(core: *mach.Core, app: *App) !void {
}
}

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

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

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

// Begin render pass
Expand All @@ -112,7 +116,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();
core.queue.submit(&[_]*gpu.CommandBuffer{command});
main_window.queue.submit(&[_]*gpu.CommandBuffer{command});

// update the window title every second
if (app.title_timer.read() >= 1.0) {
Expand Down
126 changes: 79 additions & 47 deletions examples/core-triangle/App.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const std = @import("std");
const mach = @import("mach");
const gpu = mach.gpu;

Expand All @@ -24,64 +25,95 @@ pub fn init(
core.on_tick = app_mod.id.tick;
core.on_exit = app_mod.id.deinit;

// Create our shader module
const shader_module = core.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 = core.windows.get(core.main_window, .framebuffer_format).?,
.blend = &blend,
};

// Fragment state describes which shader and entrypoint to use for rendering fragments.
const fragment = gpu.FragmentState.init(.{
.module = shader_module,
.entry_point = "frag_main",
.targets = &.{color_target},
});

// Create our render pipeline that will ultimately get pixels onto the screen.
const label = @tagName(mach_module) ++ ".init";
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.label = label,
.fragment = &fragment,
.vertex = gpu.VertexState{
const main_window = core.windows.getValue(core.main_window);
if (main_window.native != null) {
// if window.native is not null, the window is initialized

// Create our shader module
const shader_module = main_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 = core.windows.get(core.main_window, .framebuffer_format),
.blend = &blend,
};

// Fragment state describes which shader and entrypoint to use for rendering fragments.
const fragment = gpu.FragmentState.init(.{
.module = shader_module,
.entry_point = "vertex_main",
},
};
const pipeline = core.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);
.entry_point = "frag_main",
.targets = &.{color_target},
});

// Create our render pipeline that will ultimately get pixels onto the screen.
const label = @tagName(mach_module) ++ ".init";
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.label = label,
.fragment = &fragment,
.vertex = gpu.VertexState{
.module = shader_module,
.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);

pub fn tick(app: *App, core: *mach.Core) void {
while (core.nextEvent()) |event| {
switch (event) {
.key_press => |ev| {
switch (ev.key) {
.right => {
var w = core.windows.getValue(core.main_window);
w.width = w.width + 10;
core.windows.setValue(core.main_window, w);
},
.left => {
var w = core.windows.getValue(core.main_window);
w.width = w.width - 10;
core.windows.setValue(core.main_window, w);
},
.up => {
core.windows.set(core.main_window, .height, core.windows.get(core.main_window, .height) + 10);
},
.down => {
core.windows.set(core.main_window, .height, core.windows.get(core.main_window, .height) - 10);
},
else => {},
}
},
.close => core.exit(),
else => {},
}
}

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

// Window is ready when device is not null

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

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

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

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

// update the window title every second
if (app.title_timer.read() >= 1.0) {
app.title_timer.reset();
// TODO(object): window-title
// try updateWindowTitle(core);
}
// if (app.title_timer.read() >= 1.0) {
// app.title_timer.reset();
// // TODO(object): window-title
// // try updateWindowTitle(core);
// }
}

pub fn deinit(app: *App) void {
Expand Down
Loading

0 comments on commit 1fe47b2

Please sign in to comment.