mach: fundamental changes
- Core doesn't depend to `App` anymore - `setOptions` has replaced with some new functions (`setTitle`, `setSize`, etc) - and more
This commit is contained in:
parent
91a53807ab
commit
1d7cd4be80
26 changed files with 2306 additions and 1999 deletions
|
|
@ -10,8 +10,11 @@ const UniformBufferObject = struct {
|
|||
time: f32,
|
||||
};
|
||||
|
||||
var timer: std.time.Timer = undefined;
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
core: mach.Core,
|
||||
timer: mach.Timer,
|
||||
pipeline: *gpu.RenderPipeline,
|
||||
queue: *gpu.Queue,
|
||||
uniform_buffer: *gpu.Buffer,
|
||||
|
|
@ -21,8 +24,8 @@ fragment_shader_file: std.fs.File,
|
|||
fragment_shader_code: [:0]const u8,
|
||||
last_mtime: i128,
|
||||
|
||||
pub fn init(app: *App, core: *mach.Core) !void {
|
||||
timer = try std.time.Timer.start();
|
||||
pub fn init(app: *App) !void {
|
||||
app.core = try mach.Core.init(allocator, .{ .title = "shaderexp" });
|
||||
|
||||
var fragment_file: std.fs.File = undefined;
|
||||
var last_mtime: i128 = undefined;
|
||||
|
|
@ -40,21 +43,21 @@ pub fn init(app: *App, core: *mach.Core) !void {
|
|||
std.debug.print("Something went wrong when attempting to open file: {}\n", .{e});
|
||||
return;
|
||||
}
|
||||
var code = try fragment_file.readToEndAllocOptions(core.allocator, std.math.maxInt(u16), null, 1, 0);
|
||||
var code = try fragment_file.readToEndAllocOptions(allocator, std.math.maxInt(u16), null, 1, 0);
|
||||
|
||||
const queue = core.device.getQueue();
|
||||
const queue = app.core.device().getQueue();
|
||||
|
||||
// We need a bgl to bind the UniformBufferObject, but it is also needed for creating
|
||||
// the RenderPipeline, so we pass it to recreatePipeline as a pointer
|
||||
var bgl: *gpu.BindGroupLayout = undefined;
|
||||
const pipeline = recreatePipeline(core, code, &bgl);
|
||||
const pipeline = recreatePipeline(&app.core, code, &bgl);
|
||||
|
||||
const uniform_buffer = core.device.createBuffer(&.{
|
||||
const uniform_buffer = app.core.device().createBuffer(&.{
|
||||
.usage = .{ .copy_dst = true, .uniform = true },
|
||||
.size = @sizeOf(UniformBufferObject),
|
||||
.mapped_at_creation = false,
|
||||
});
|
||||
const bind_group = core.device.createBindGroup(
|
||||
const bind_group = app.core.device().createBindGroup(
|
||||
&gpu.BindGroup.Descriptor.init(.{
|
||||
.layout = bgl,
|
||||
.entries = &.{
|
||||
|
|
@ -63,6 +66,8 @@ pub fn init(app: *App, core: *mach.Core) !void {
|
|||
}),
|
||||
);
|
||||
|
||||
app.timer = try mach.Timer.start();
|
||||
|
||||
app.pipeline = pipeline;
|
||||
app.queue = queue;
|
||||
app.uniform_buffer = uniform_buffer;
|
||||
|
|
@ -75,21 +80,24 @@ pub fn init(app: *App, core: *mach.Core) !void {
|
|||
bgl.release();
|
||||
}
|
||||
|
||||
pub fn deinit(app: *App, core: *mach.Core) void {
|
||||
pub fn deinit(app: *App) void {
|
||||
defer _ = gpa.deinit();
|
||||
defer app.core.deinit();
|
||||
|
||||
app.fragment_shader_file.close();
|
||||
core.allocator.free(app.fragment_shader_code);
|
||||
allocator.free(app.fragment_shader_code);
|
||||
|
||||
app.uniform_buffer.release();
|
||||
app.bind_group.release();
|
||||
}
|
||||
|
||||
pub fn update(app: *App, core: *mach.Core) !void {
|
||||
while (core.pollEvent()) |event| {
|
||||
pub fn update(app: *App) !bool {
|
||||
while (app.core.pollEvents()) |event| {
|
||||
switch (event) {
|
||||
.key_press => |ev| {
|
||||
if (ev.key == .space)
|
||||
core.close();
|
||||
if (ev.key == .space) return true;
|
||||
},
|
||||
.close => return true,
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
|
@ -99,17 +107,17 @@ pub fn update(app: *App, core: *mach.Core) !void {
|
|||
std.log.info("The fragment shader has been changed", .{});
|
||||
app.last_mtime = stat.mtime;
|
||||
app.fragment_shader_file.seekTo(0) catch unreachable;
|
||||
app.fragment_shader_code = app.fragment_shader_file.readToEndAllocOptions(core.allocator, std.math.maxInt(u32), null, 1, 0) catch |err| {
|
||||
app.fragment_shader_code = app.fragment_shader_file.readToEndAllocOptions(allocator, std.math.maxInt(u32), null, 1, 0) catch |err| {
|
||||
std.log.err("Err: {}", .{err});
|
||||
return core.close();
|
||||
return true;
|
||||
};
|
||||
app.pipeline = recreatePipeline(core, app.fragment_shader_code, null);
|
||||
app.pipeline = recreatePipeline(&app.core, app.fragment_shader_code, null);
|
||||
}
|
||||
} else |err| {
|
||||
std.log.err("Something went wrong when attempting to stat file: {}\n", .{err});
|
||||
}
|
||||
|
||||
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||
const back_buffer_view = app.core.swapChain().getCurrentTextureView();
|
||||
const color_attachment = gpu.RenderPassColorAttachment{
|
||||
.view = back_buffer_view,
|
||||
.clear_value = std.mem.zeroes(gpu.Color),
|
||||
|
|
@ -117,14 +125,14 @@ pub fn update(app: *App, core: *mach.Core) !void {
|
|||
.store_op = .store,
|
||||
};
|
||||
|
||||
const encoder = core.device.createCommandEncoder(null);
|
||||
const encoder = app.core.device().createCommandEncoder(null);
|
||||
const render_pass_info = gpu.RenderPassDescriptor.init(.{
|
||||
.color_attachments = &.{color_attachment},
|
||||
});
|
||||
|
||||
const time = @intToFloat(f32, timer.read()) / @as(f32, std.time.ns_per_s);
|
||||
const time = app.timer.read() / @as(f32, std.time.ns_per_s);
|
||||
const ubo = UniformBufferObject{
|
||||
.resolution = .{ @intToFloat(f32, core.current_desc.width), @intToFloat(f32, core.current_desc.height) },
|
||||
.resolution = .{ @intToFloat(f32, app.core.descriptor().width), @intToFloat(f32, app.core.descriptor().height) },
|
||||
.time = time,
|
||||
};
|
||||
encoder.writeBuffer(app.uniform_buffer, 0, &[_]UniformBufferObject{ubo});
|
||||
|
|
@ -141,21 +149,23 @@ pub fn update(app: *App, core: *mach.Core) !void {
|
|||
|
||||
app.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
command.release();
|
||||
core.swap_chain.?.present();
|
||||
app.core.swapChain().present();
|
||||
back_buffer_view.release();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fn recreatePipeline(core: *mach.Core, fragment_shader_code: [:0]const u8, bgl: ?**gpu.BindGroupLayout) *gpu.RenderPipeline {
|
||||
const vs_module = core.device.createShaderModuleWGSL("vert.wgsl", @embedFile("vert.wgsl"));
|
||||
const vs_module = core.device().createShaderModuleWGSL("vert.wgsl", @embedFile("vert.wgsl"));
|
||||
defer vs_module.release();
|
||||
|
||||
// Check wether the fragment shader code compiled successfully, if not
|
||||
// print the validation layer error and show a black screen
|
||||
core.device.pushErrorScope(.validation);
|
||||
var fs_module = core.device.createShaderModuleWGSL("fragment shader", fragment_shader_code);
|
||||
core.device().pushErrorScope(.validation);
|
||||
var fs_module = core.device().createShaderModuleWGSL("fragment shader", fragment_shader_code);
|
||||
var error_occurred: bool = false;
|
||||
// popErrorScope() returns always true, (unless maybe it fails to capture the error scope?)
|
||||
_ = core.device.popErrorScope(&error_occurred, struct {
|
||||
_ = core.device().popErrorScope(&error_occurred, struct {
|
||||
inline fn callback(ctx: *bool, typ: gpu.ErrorType, message: [*:0]const u8) void {
|
||||
if (typ != .no_error) {
|
||||
std.debug.print("🔴🔴🔴🔴:\n{s}\n", .{message});
|
||||
|
|
@ -164,7 +174,7 @@ fn recreatePipeline(core: *mach.Core, fragment_shader_code: [:0]const u8, bgl: ?
|
|||
}
|
||||
}.callback);
|
||||
if (error_occurred) {
|
||||
fs_module = core.device.createShaderModuleWGSL(
|
||||
fs_module = core.device().createShaderModuleWGSL(
|
||||
"black_screen_frag.wgsl",
|
||||
@embedFile("black_screen_frag.wgsl"),
|
||||
);
|
||||
|
|
@ -173,7 +183,7 @@ fn recreatePipeline(core: *mach.Core, fragment_shader_code: [:0]const u8, bgl: ?
|
|||
|
||||
const blend = gpu.BlendState{};
|
||||
const color_target = gpu.ColorTargetState{
|
||||
.format = core.swap_chain_format,
|
||||
.format = core.descriptor().format,
|
||||
.blend = &blend,
|
||||
.write_mask = gpu.ColorWriteMaskFlags.all,
|
||||
};
|
||||
|
|
@ -185,7 +195,7 @@ fn recreatePipeline(core: *mach.Core, fragment_shader_code: [:0]const u8, bgl: ?
|
|||
|
||||
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .fragment = true }, .uniform, true, 0);
|
||||
// bgl is needed outside, for the creation of the uniform_buffer in main
|
||||
const bgl_tmp = core.device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor.init(.{
|
||||
const bgl_tmp = core.device().createBindGroupLayout(&gpu.BindGroupLayout.Descriptor.init(.{
|
||||
.entries = &.{bgle},
|
||||
}));
|
||||
defer {
|
||||
|
|
@ -198,7 +208,7 @@ fn recreatePipeline(core: *mach.Core, fragment_shader_code: [:0]const u8, bgl: ?
|
|||
}
|
||||
|
||||
const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl_tmp};
|
||||
const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
|
||||
const pipeline_layout = core.device().createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
|
||||
.bind_group_layouts = &bind_group_layouts,
|
||||
}));
|
||||
defer pipeline_layout.release();
|
||||
|
|
@ -214,10 +224,10 @@ fn recreatePipeline(core: *mach.Core, fragment_shader_code: [:0]const u8, bgl: ?
|
|||
|
||||
// Create the render pipeline. Even if the shader compilation succeeded, this could fail if the
|
||||
// shader is missing a `main` entrypoint.
|
||||
core.device.pushErrorScope(.validation);
|
||||
const pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||
core.device().pushErrorScope(.validation);
|
||||
const pipeline = core.device().createRenderPipeline(&pipeline_descriptor);
|
||||
// popErrorScope() returns always true, (unless maybe it fails to capture the error scope?)
|
||||
_ = core.device.popErrorScope(&error_occurred, struct {
|
||||
_ = core.device().popErrorScope(&error_occurred, struct {
|
||||
inline fn callback(ctx: *bool, typ: gpu.ErrorType, message: [*:0]const u8) void {
|
||||
if (typ != .no_error) {
|
||||
std.debug.print("🔴🔴🔴🔴:\n{s}\n", .{message});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue