Skip to content

obj: Move Platform and InitOptions fields into core.windows #1309

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 7 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
191 changes: 114 additions & 77 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,43 +25,49 @@ 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{
.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,
};
if (core.windows.getAll(core.main_window)) |cw| {
const core_window = cw;
if (core_window.device) |device| {

// Create our shader module
const shader_module = 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{
.module = shader_module,
.entry_point = "vertex_main",
},
};
const pipeline = 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);
Expand All @@ -69,53 +76,83 @@ pub fn init(
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.getAll(core.main_window).?;
w.width = w.width + 10;
core.windows.setAll(core.main_window, w);
},
.left => {
var w = core.windows.getAll(core.main_window).?;
w.width = w.width - 10;
core.windows.setAll(core.main_window, w);
},
.up => {
if (core.windows.get(core.main_window, .height)) |height| core.windows.set(core.main_window, .height, height + 10);
},
.down => {
if (core.windows.get(core.main_window, .height)) |height| core.windows.set(core.main_window, .height, height - 10);
},
else => {},
}
},
.close => core.exit(),
else => {},
}
}

// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = core.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 });
defer encoder.release();

// Begin render pass
const sky_blue_background = gpu.Color{ .r = 0.776, .g = 0.988, .b = 1, .a = 1 };
const color_attachments = [_]gpu.RenderPassColorAttachment{.{
.view = back_buffer_view,
.clear_value = sky_blue_background,
.load_op = .clear,
.store_op = .store,
}};
const render_pass = encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
.label = label,
.color_attachments = &color_attachments,
}));
defer render_pass.release();

// Draw
render_pass.setPipeline(app.pipeline);
render_pass.draw(3, 1, 0, 0);

// Finish render pass
render_pass.end();

// Submit our commands to the queue
var command = encoder.finish(&.{ .label = label });
defer command.release();
core.queue.submit(&[_]*gpu.CommandBuffer{command});
if (core.windows.getAll(core.main_window)) |mw| {
var main_window = mw;

// Window is ready when device is not null
if (main_window.device) |device| {

// Grab the back buffer of the swapchain
// TODO(Core)
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 = device.createCommandEncoder(&.{ .label = label });
defer encoder.release();

// Begin render pass
const sky_blue_background = gpu.Color{ .r = 0.776, .g = 0.988, .b = 1, .a = 1 };
const color_attachments = [_]gpu.RenderPassColorAttachment{.{
.view = back_buffer_view,
.clear_value = sky_blue_background,
.load_op = .clear,
.store_op = .store,
}};
const render_pass = encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
.label = label,
.color_attachments = &color_attachments,
}));
defer render_pass.release();

// Draw
render_pass.setPipeline(app.pipeline);
render_pass.draw(3, 1, 0, 0);

// Finish render pass
render_pass.end();

// Submit our commands to the queue
var command = encoder.finish(&.{ .label = label });
defer command.release();
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
Loading