{examples,shaderexp}: rename mach.Engine -> mach.Core
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
e79c9e075a
commit
1b97c9f1e5
10 changed files with 263 additions and 263 deletions
|
|
@ -32,33 +32,33 @@ const Dir = struct {
|
||||||
const right: u8 = 0b1000;
|
const right: u8 = 0b1000;
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn init(app: *App, engine: *mach.Engine) !void {
|
pub fn init(app: *App, core: *mach.Core) !void {
|
||||||
try engine.setOptions(.{
|
try core.setOptions(.{
|
||||||
.size_min = .{ .width = 20, .height = 20 },
|
.size_min = .{ .width = 20, .height = 20 },
|
||||||
});
|
});
|
||||||
|
|
||||||
const eye = vec3(5.0, 7.0, 5.0);
|
const eye = vec3(5.0, 7.0, 5.0);
|
||||||
const target = vec3(0.0, 0.0, 0.0);
|
const target = vec3(0.0, 0.0, 0.0);
|
||||||
|
|
||||||
const size = engine.getFramebufferSize();
|
const size = core.getFramebufferSize();
|
||||||
const aspect_ratio = @intToFloat(f32, size.width) / @intToFloat(f32, size.height);
|
const aspect_ratio = @intToFloat(f32, size.width) / @intToFloat(f32, size.height);
|
||||||
|
|
||||||
app.queue = engine.device.getQueue();
|
app.queue = core.device.getQueue();
|
||||||
app.cube = Cube.init(engine);
|
app.cube = Cube.init(core);
|
||||||
app.light = Light.init(engine);
|
app.light = Light.init(core);
|
||||||
app.depth = null;
|
app.depth = null;
|
||||||
app.camera = Camera.init(engine.device, eye, target, vec3(0.0, 1.0, 0.0), aspect_ratio, 45.0, 0.1, 100.0);
|
app.camera = Camera.init(core.device, eye, target, vec3(0.0, 1.0, 0.0), aspect_ratio, 45.0, 0.1, 100.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(app: *App, _: *mach.Engine) void {
|
pub fn deinit(app: *App, _: *mach.Core) void {
|
||||||
app.depth.?.release();
|
app.depth.?.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(app: *App, engine: *mach.Engine) !void {
|
pub fn update(app: *App, core: *mach.Core) !void {
|
||||||
while (engine.pollEvent()) |event| {
|
while (core.pollEvent()) |event| {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key_press => |ev| switch (ev.key) {
|
.key_press => |ev| switch (ev.key) {
|
||||||
.q, .escape, .space => engine.setShouldClose(true),
|
.q, .escape, .space => core.setShouldClose(true),
|
||||||
.w, .up => {
|
.w, .up => {
|
||||||
app.keys |= Dir.up;
|
app.keys |= Dir.up;
|
||||||
},
|
},
|
||||||
|
|
@ -93,7 +93,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
}
|
}
|
||||||
|
|
||||||
// move camera
|
// move camera
|
||||||
const speed = zm.f32x4s(@floatCast(f32, engine.delta_time * 5));
|
const speed = zm.f32x4s(@floatCast(f32, core.delta_time * 5));
|
||||||
const fwd = zm.normalize3(app.camera.target - app.camera.eye);
|
const fwd = zm.normalize3(app.camera.target - app.camera.eye);
|
||||||
const right = zm.normalize3(zm.cross3(fwd, app.camera.up));
|
const right = zm.normalize3(zm.cross3(fwd, app.camera.up));
|
||||||
|
|
||||||
|
|
@ -108,13 +108,13 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
app.camera.update(app.queue);
|
app.camera.update(app.queue);
|
||||||
|
|
||||||
// move light
|
// move light
|
||||||
const light_speed = @floatCast(f32, engine.delta_time * 2.5);
|
const light_speed = @floatCast(f32, core.delta_time * 2.5);
|
||||||
app.light.update(app.queue, light_speed);
|
app.light.update(app.queue, light_speed);
|
||||||
|
|
||||||
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
|
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||||
defer back_buffer_view.release();
|
defer back_buffer_view.release();
|
||||||
|
|
||||||
const encoder = engine.device.createCommandEncoder(null);
|
const encoder = core.device.createCommandEncoder(null);
|
||||||
defer encoder.release();
|
defer encoder.release();
|
||||||
|
|
||||||
const color_attachment = gpu.RenderPassColorAttachment{
|
const color_attachment = gpu.RenderPassColorAttachment{
|
||||||
|
|
@ -171,16 +171,16 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
defer command.release();
|
defer command.release();
|
||||||
|
|
||||||
app.queue.submit(&.{command});
|
app.queue.submit(&.{command});
|
||||||
engine.swap_chain.?.present();
|
core.swap_chain.?.present();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
|
pub fn resize(app: *App, core: *mach.Core, width: u32, height: u32) !void {
|
||||||
// If window is resized, recreate depth buffer otherwise we cannot use it.
|
// If window is resized, recreate depth buffer otherwise we cannot use it.
|
||||||
if (app.depth != null) {
|
if (app.depth != null) {
|
||||||
app.depth.?.release();
|
app.depth.?.release();
|
||||||
}
|
}
|
||||||
// It also recreates the sampler, which is a waste, but for an example it's ok
|
// It also recreates the sampler, which is a waste, but for an example it's ok
|
||||||
app.depth = Texture.depth(engine.device, width, height);
|
app.depth = Texture.depth(core.device, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Camera = struct {
|
const Camera = struct {
|
||||||
|
|
@ -283,8 +283,8 @@ const Cube = struct {
|
||||||
const SPACING = 2; // spacing between cubes
|
const SPACING = 2; // spacing between cubes
|
||||||
const DISPLACEMENT = vec3u(IPR * SPACING / 2, 0, IPR * SPACING / 2);
|
const DISPLACEMENT = vec3u(IPR * SPACING / 2, 0, IPR * SPACING / 2);
|
||||||
|
|
||||||
fn init(engine: *mach.Engine) Self {
|
fn init(core: *mach.Core) Self {
|
||||||
const device = engine.device;
|
const device = core.device;
|
||||||
|
|
||||||
const texture = Brick.texture(device);
|
const texture = Brick.texture(device);
|
||||||
|
|
||||||
|
|
@ -322,12 +322,12 @@ const Cube = struct {
|
||||||
.mesh = mesh(device),
|
.mesh = mesh(device),
|
||||||
.texture = texture,
|
.texture = texture,
|
||||||
.instance = instance,
|
.instance = instance,
|
||||||
.pipeline = pipeline(engine),
|
.pipeline = pipeline(core),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pipeline(engine: *mach.Engine) gpu.RenderPipeline {
|
fn pipeline(core: *mach.Core) gpu.RenderPipeline {
|
||||||
const device = engine.device;
|
const device = core.device;
|
||||||
|
|
||||||
const layout_descriptor = gpu.PipelineLayout.Descriptor{
|
const layout_descriptor = gpu.PipelineLayout.Descriptor{
|
||||||
.bind_group_layouts = &.{
|
.bind_group_layouts = &.{
|
||||||
|
|
@ -359,7 +359,7 @@ const Cube = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
const color_target = gpu.ColorTargetState{
|
const color_target = gpu.ColorTargetState{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.write_mask = gpu.ColorWriteMask.all,
|
.write_mask = gpu.ColorWriteMask.all,
|
||||||
.blend = &blend,
|
.blend = &blend,
|
||||||
};
|
};
|
||||||
|
|
@ -732,8 +732,8 @@ const Light = struct {
|
||||||
color: Vec,
|
color: Vec,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn init(engine: *mach.Engine) Self {
|
fn init(core: *mach.Core) Self {
|
||||||
const device = engine.device;
|
const device = core.device;
|
||||||
const uniform = .{
|
const uniform = .{
|
||||||
.color = vec3u(1, 1, 1),
|
.color = vec3u(1, 1, 1),
|
||||||
.position = vec3u(3, 7, 2),
|
.position = vec3u(3, 7, 2),
|
||||||
|
|
@ -755,7 +755,7 @@ const Light = struct {
|
||||||
.buffer = buffer,
|
.buffer = buffer,
|
||||||
.uniform = uniform,
|
.uniform = uniform,
|
||||||
.bind_group = bind_group,
|
.bind_group = bind_group,
|
||||||
.pipeline = Self.pipeline(engine),
|
.pipeline = Self.pipeline(core),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -779,8 +779,8 @@ const Light = struct {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pipeline(engine: *mach.Engine) gpu.RenderPipeline {
|
fn pipeline(core: *mach.Core) gpu.RenderPipeline {
|
||||||
const device = engine.device;
|
const device = core.device;
|
||||||
|
|
||||||
const layout_descriptor = gpu.PipelineLayout.Descriptor{
|
const layout_descriptor = gpu.PipelineLayout.Descriptor{
|
||||||
.bind_group_layouts = &.{
|
.bind_group_layouts = &.{
|
||||||
|
|
@ -811,7 +811,7 @@ const Light = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
const color_target = gpu.ColorTargetState{
|
const color_target = gpu.ColorTargetState{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.write_mask = gpu.ColorWriteMask.all,
|
.write_mask = gpu.ColorWriteMask.all,
|
||||||
.blend = &blend,
|
.blend = &blend,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -26,13 +26,13 @@ var sim_params = [_]f32{
|
||||||
0.005, // .rule_3_scale
|
0.005, // .rule_3_scale
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn init(app: *App, engine: *mach.Engine) !void {
|
pub fn init(app: *App, core: *mach.Core) !void {
|
||||||
const sprite_shader_module = engine.device.createShaderModule(&.{
|
const sprite_shader_module = core.device.createShaderModule(&.{
|
||||||
.label = "sprite shader module",
|
.label = "sprite shader module",
|
||||||
.code = .{ .wgsl = @embedFile("sprite.wgsl") },
|
.code = .{ .wgsl = @embedFile("sprite.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
||||||
const update_sprite_shader_module = engine.device.createShaderModule(&.{
|
const update_sprite_shader_module = core.device.createShaderModule(&.{
|
||||||
.label = "update sprite shader module",
|
.label = "update sprite shader module",
|
||||||
.code = .{ .wgsl = @embedFile("updateSprites.wgsl") },
|
.code = .{ .wgsl = @embedFile("updateSprites.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -61,7 +61,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const render_pipeline = engine.device.createRenderPipeline(&gpu.RenderPipeline.Descriptor{
|
const render_pipeline = core.device.createRenderPipeline(&gpu.RenderPipeline.Descriptor{
|
||||||
.vertex = .{
|
.vertex = .{
|
||||||
.module = sprite_shader_module,
|
.module = sprite_shader_module,
|
||||||
.entry_point = "vert_main",
|
.entry_point = "vert_main",
|
||||||
|
|
@ -84,12 +84,12 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
.fragment = &gpu.FragmentState{ .module = sprite_shader_module, .entry_point = "frag_main", .targets = &[_]gpu.ColorTargetState{
|
.fragment = &gpu.FragmentState{ .module = sprite_shader_module, .entry_point = "frag_main", .targets = &[_]gpu.ColorTargetState{
|
||||||
.{
|
.{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
},
|
},
|
||||||
} },
|
} },
|
||||||
});
|
});
|
||||||
|
|
||||||
const compute_pipeline = engine.device.createComputePipeline(&gpu.ComputePipeline.Descriptor{ .compute = gpu.ProgrammableStageDescriptor{
|
const compute_pipeline = core.device.createComputePipeline(&gpu.ComputePipeline.Descriptor{ .compute = gpu.ProgrammableStageDescriptor{
|
||||||
.module = update_sprite_shader_module,
|
.module = update_sprite_shader_module,
|
||||||
.entry_point = "main",
|
.entry_point = "main",
|
||||||
} });
|
} });
|
||||||
|
|
@ -99,17 +99,17 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
-0.02, 0.0, 0.02,
|
-0.02, 0.0, 0.02,
|
||||||
};
|
};
|
||||||
|
|
||||||
const sprite_vertex_buffer = engine.device.createBuffer(&gpu.Buffer.Descriptor{
|
const sprite_vertex_buffer = core.device.createBuffer(&gpu.Buffer.Descriptor{
|
||||||
.usage = .{ .vertex = true, .copy_dst = true },
|
.usage = .{ .vertex = true, .copy_dst = true },
|
||||||
.size = vert_buffer_data.len * @sizeOf(f32),
|
.size = vert_buffer_data.len * @sizeOf(f32),
|
||||||
});
|
});
|
||||||
engine.device.getQueue().writeBuffer(sprite_vertex_buffer, 0, f32, &vert_buffer_data);
|
core.device.getQueue().writeBuffer(sprite_vertex_buffer, 0, f32, &vert_buffer_data);
|
||||||
|
|
||||||
const sim_param_buffer = engine.device.createBuffer(&gpu.Buffer.Descriptor{
|
const sim_param_buffer = core.device.createBuffer(&gpu.Buffer.Descriptor{
|
||||||
.usage = .{ .uniform = true, .copy_dst = true },
|
.usage = .{ .uniform = true, .copy_dst = true },
|
||||||
.size = sim_params.len * @sizeOf(f32),
|
.size = sim_params.len * @sizeOf(f32),
|
||||||
});
|
});
|
||||||
engine.device.getQueue().writeBuffer(sim_param_buffer, 0, f32, &sim_params);
|
core.device.getQueue().writeBuffer(sim_param_buffer, 0, f32, &sim_params);
|
||||||
|
|
||||||
var initial_particle_data: [num_particle * 4]f32 = undefined;
|
var initial_particle_data: [num_particle * 4]f32 = undefined;
|
||||||
var rng = std.rand.DefaultPrng.init(0);
|
var rng = std.rand.DefaultPrng.init(0);
|
||||||
|
|
@ -126,7 +126,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
var particle_bind_groups: [2]gpu.BindGroup = undefined;
|
var particle_bind_groups: [2]gpu.BindGroup = undefined;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < 2) : (i += 1) {
|
while (i < 2) : (i += 1) {
|
||||||
particle_buffers[i] = engine.device.createBuffer(&gpu.Buffer.Descriptor{
|
particle_buffers[i] = core.device.createBuffer(&gpu.Buffer.Descriptor{
|
||||||
.usage = .{
|
.usage = .{
|
||||||
.vertex = true,
|
.vertex = true,
|
||||||
.copy_dst = true,
|
.copy_dst = true,
|
||||||
|
|
@ -134,12 +134,12 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
.size = initial_particle_data.len * @sizeOf(f32),
|
.size = initial_particle_data.len * @sizeOf(f32),
|
||||||
});
|
});
|
||||||
engine.device.getQueue().writeBuffer(particle_buffers[i], 0, f32, &initial_particle_data);
|
core.device.getQueue().writeBuffer(particle_buffers[i], 0, f32, &initial_particle_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < 2) : (i += 1) {
|
while (i < 2) : (i += 1) {
|
||||||
particle_bind_groups[i] = engine.device.createBindGroup(&gpu.BindGroup.Descriptor{ .layout = compute_pipeline.getBindGroupLayout(0), .entries = &[_]gpu.BindGroup.Entry{
|
particle_bind_groups[i] = core.device.createBindGroup(&gpu.BindGroup.Descriptor{ .layout = compute_pipeline.getBindGroupLayout(0), .entries = &[_]gpu.BindGroup.Entry{
|
||||||
gpu.BindGroup.Entry.buffer(0, sim_param_buffer, 0, sim_params.len * @sizeOf(f32)),
|
gpu.BindGroup.Entry.buffer(0, sim_param_buffer, 0, sim_params.len * @sizeOf(f32)),
|
||||||
gpu.BindGroup.Entry.buffer(1, particle_buffers[i], 0, initial_particle_data.len * @sizeOf(f32)),
|
gpu.BindGroup.Entry.buffer(1, particle_buffers[i], 0, initial_particle_data.len * @sizeOf(f32)),
|
||||||
gpu.BindGroup.Entry.buffer(2, particle_buffers[(i + 1) % 2], 0, initial_particle_data.len * @sizeOf(f32)),
|
gpu.BindGroup.Entry.buffer(2, particle_buffers[(i + 1) % 2], 0, initial_particle_data.len * @sizeOf(f32)),
|
||||||
|
|
@ -155,10 +155,10 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
app.frame_counter = 0;
|
app.frame_counter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(_: *App, _: *mach.Engine) void {}
|
pub fn deinit(_: *App, _: *mach.Core) void {}
|
||||||
|
|
||||||
pub fn update(app: *App, engine: *mach.Engine) !void {
|
pub fn update(app: *App, core: *mach.Core) !void {
|
||||||
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
|
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||||
const color_attachment = gpu.RenderPassColorAttachment{
|
const color_attachment = gpu.RenderPassColorAttachment{
|
||||||
.view = back_buffer_view,
|
.view = back_buffer_view,
|
||||||
.resolve_target = null,
|
.resolve_target = null,
|
||||||
|
|
@ -171,10 +171,10 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
color_attachment,
|
color_attachment,
|
||||||
} };
|
} };
|
||||||
|
|
||||||
sim_params[0] = @floatCast(f32, engine.delta_time);
|
sim_params[0] = @floatCast(f32, core.delta_time);
|
||||||
engine.device.getQueue().writeBuffer(app.sim_param_buffer, 0, f32, &sim_params);
|
core.device.getQueue().writeBuffer(app.sim_param_buffer, 0, f32, &sim_params);
|
||||||
|
|
||||||
const command_encoder = engine.device.createCommandEncoder(null);
|
const command_encoder = core.device.createCommandEncoder(null);
|
||||||
{
|
{
|
||||||
const pass_encoder = command_encoder.beginComputePass(null);
|
const pass_encoder = command_encoder.beginComputePass(null);
|
||||||
pass_encoder.setPipeline(app.compute_pipeline);
|
pass_encoder.setPipeline(app.compute_pipeline);
|
||||||
|
|
@ -200,9 +200,9 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
var command = command_encoder.finish(null);
|
var command = command_encoder.finish(null);
|
||||||
command_encoder.release();
|
command_encoder.release();
|
||||||
engine.device.getQueue().submit(&.{command});
|
core.device.getQueue().submit(&.{command});
|
||||||
command.release();
|
command.release();
|
||||||
|
|
||||||
engine.swap_chain.?.present();
|
core.swap_chain.?.present();
|
||||||
back_buffer_view.release();
|
back_buffer_view.release();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@
|
||||||
//! We also need a second texture to use on the cube, that after the render pass
|
//! We also need a second texture to use on the cube, that after the render pass
|
||||||
//! needs to copy the other texture. We can't use the same texture since
|
//! needs to copy the other texture. We can't use the same texture since
|
||||||
//! it would interfere with the sincronization on the gpu during the render pass.
|
//! it would interfere with the sincronization on the gpu during the render pass.
|
||||||
//! This demo currently does not work on opengl, because engine.current_desc.width/height,
|
//! This demo currently does not work on opengl, because core.current_desc.width/height,
|
||||||
//! are set to 0 after engine.init() and because webgpu does not implement copyTextureToTexture,
|
//! are set to 0 after core.init() and because webgpu does not implement copyTextureToTexture,
|
||||||
//! for opengl
|
//! for opengl
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
@ -38,14 +38,14 @@ cube_texture_view_render: gpu.TextureView,
|
||||||
sampler: gpu.Sampler,
|
sampler: gpu.Sampler,
|
||||||
bgl: gpu.BindGroupLayout,
|
bgl: gpu.BindGroupLayout,
|
||||||
|
|
||||||
pub fn init(app: *App, engine: *mach.Engine) !void {
|
pub fn init(app: *App, core: *mach.Core) !void {
|
||||||
timer = try mach.Timer.start();
|
timer = try mach.Timer.start();
|
||||||
|
|
||||||
try engine.setOptions(.{
|
try core.setOptions(.{
|
||||||
.size_min = .{ .width = 20, .height = 20 },
|
.size_min = .{ .width = 20, .height = 20 },
|
||||||
});
|
});
|
||||||
|
|
||||||
const vs_module = engine.device.createShaderModule(&.{
|
const vs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my vertex shader",
|
.label = "my vertex shader",
|
||||||
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -61,7 +61,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
.attributes = &vertex_attributes,
|
.attributes = &vertex_attributes,
|
||||||
};
|
};
|
||||||
|
|
||||||
const fs_module = engine.device.createShaderModule(&.{
|
const fs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my fragment shader",
|
.label = "my fragment shader",
|
||||||
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -79,7 +79,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const color_target = gpu.ColorTargetState{
|
const color_target = gpu.ColorTargetState{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.blend = &blend,
|
.blend = &blend,
|
||||||
.write_mask = gpu.ColorWriteMask.all,
|
.write_mask = gpu.ColorWriteMask.all,
|
||||||
};
|
};
|
||||||
|
|
@ -93,14 +93,14 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
const bgle_buffer = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
|
const bgle_buffer = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
|
||||||
const bgle_sampler = gpu.BindGroupLayout.Entry.sampler(1, .{ .fragment = true }, .filtering);
|
const bgle_sampler = gpu.BindGroupLayout.Entry.sampler(1, .{ .fragment = true }, .filtering);
|
||||||
const bgle_textureview = gpu.BindGroupLayout.Entry.texture(2, .{ .fragment = true }, .float, .dimension_2d, false);
|
const bgle_textureview = gpu.BindGroupLayout.Entry.texture(2, .{ .fragment = true }, .float, .dimension_2d, false);
|
||||||
const bgl = engine.device.createBindGroupLayout(
|
const bgl = core.device.createBindGroupLayout(
|
||||||
&gpu.BindGroupLayout.Descriptor{
|
&gpu.BindGroupLayout.Descriptor{
|
||||||
.entries = &.{ bgle_buffer, bgle_sampler, bgle_textureview },
|
.entries = &.{ bgle_buffer, bgle_sampler, bgle_textureview },
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
|
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
|
||||||
const pipeline_layout = engine.device.createPipelineLayout(&.{
|
const pipeline_layout = core.device.createPipelineLayout(&.{
|
||||||
.bind_group_layouts = &bind_group_layouts,
|
.bind_group_layouts = &bind_group_layouts,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -130,7 +130,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const vertex_buffer = engine.device.createBuffer(&.{
|
const vertex_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .vertex = true },
|
.usage = .{ .vertex = true },
|
||||||
.size = @sizeOf(Vertex) * vertices.len,
|
.size = @sizeOf(Vertex) * vertices.len,
|
||||||
.mapped_at_creation = true,
|
.mapped_at_creation = true,
|
||||||
|
|
@ -139,44 +139,44 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
std.mem.copy(Vertex, vertex_mapped, vertices[0..]);
|
std.mem.copy(Vertex, vertex_mapped, vertices[0..]);
|
||||||
vertex_buffer.unmap();
|
vertex_buffer.unmap();
|
||||||
|
|
||||||
const uniform_buffer = engine.device.createBuffer(&.{
|
const uniform_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .copy_dst = true, .uniform = true },
|
.usage = .{ .copy_dst = true, .uniform = true },
|
||||||
.size = @sizeOf(UniformBufferObject),
|
.size = @sizeOf(UniformBufferObject),
|
||||||
.mapped_at_creation = false,
|
.mapped_at_creation = false,
|
||||||
});
|
});
|
||||||
|
|
||||||
// The texture to put on the cube
|
// The texture to put on the cube
|
||||||
const cube_texture = engine.device.createTexture(&gpu.Texture.Descriptor{
|
const cube_texture = core.device.createTexture(&gpu.Texture.Descriptor{
|
||||||
.usage = .{ .texture_binding = true, .copy_dst = true },
|
.usage = .{ .texture_binding = true, .copy_dst = true },
|
||||||
.size = .{ .width = engine.current_desc.width, .height = engine.current_desc.height },
|
.size = .{ .width = core.current_desc.width, .height = core.current_desc.height },
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
});
|
});
|
||||||
// The texture on which we render
|
// The texture on which we render
|
||||||
const cube_texture_render = engine.device.createTexture(&gpu.Texture.Descriptor{
|
const cube_texture_render = core.device.createTexture(&gpu.Texture.Descriptor{
|
||||||
.usage = .{ .render_attachment = true, .copy_src = true },
|
.usage = .{ .render_attachment = true, .copy_src = true },
|
||||||
.size = .{ .width = engine.current_desc.width, .height = engine.current_desc.height },
|
.size = .{ .width = core.current_desc.width, .height = core.current_desc.height },
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
});
|
});
|
||||||
|
|
||||||
const sampler = engine.device.createSampler(&gpu.Sampler.Descriptor{
|
const sampler = core.device.createSampler(&gpu.Sampler.Descriptor{
|
||||||
.mag_filter = .linear,
|
.mag_filter = .linear,
|
||||||
.min_filter = .linear,
|
.min_filter = .linear,
|
||||||
});
|
});
|
||||||
|
|
||||||
const cube_texture_view = cube_texture.createView(&gpu.TextureView.Descriptor{
|
const cube_texture_view = cube_texture.createView(&gpu.TextureView.Descriptor{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.dimension = .dimension_2d,
|
.dimension = .dimension_2d,
|
||||||
.mip_level_count = 1,
|
.mip_level_count = 1,
|
||||||
.array_layer_count = 1,
|
.array_layer_count = 1,
|
||||||
});
|
});
|
||||||
const cube_texture_view_render = cube_texture_render.createView(&gpu.TextureView.Descriptor{
|
const cube_texture_view_render = cube_texture_render.createView(&gpu.TextureView.Descriptor{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.dimension = .dimension_2d,
|
.dimension = .dimension_2d,
|
||||||
.mip_level_count = 1,
|
.mip_level_count = 1,
|
||||||
.array_layer_count = 1,
|
.array_layer_count = 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
const bind_group = engine.device.createBindGroup(
|
const bind_group = core.device.createBindGroup(
|
||||||
&gpu.BindGroup.Descriptor{
|
&gpu.BindGroup.Descriptor{
|
||||||
.layout = bgl,
|
.layout = bgl,
|
||||||
.entries = &.{
|
.entries = &.{
|
||||||
|
|
@ -187,8 +187,8 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
app.pipeline = engine.device.createRenderPipeline(&pipeline_descriptor);
|
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||||
app.queue = engine.device.getQueue();
|
app.queue = core.device.getQueue();
|
||||||
app.vertex_buffer = vertex_buffer;
|
app.vertex_buffer = vertex_buffer;
|
||||||
app.uniform_buffer = uniform_buffer;
|
app.uniform_buffer = uniform_buffer;
|
||||||
app.bind_group = bind_group;
|
app.bind_group = bind_group;
|
||||||
|
|
@ -206,7 +206,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
pipeline_layout.release();
|
pipeline_layout.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(app: *App, _: *mach.Engine) void {
|
pub fn deinit(app: *App, _: *mach.Core) void {
|
||||||
app.bgl.release();
|
app.bgl.release();
|
||||||
app.vertex_buffer.release();
|
app.vertex_buffer.release();
|
||||||
app.uniform_buffer.release();
|
app.uniform_buffer.release();
|
||||||
|
|
@ -220,19 +220,19 @@ pub fn deinit(app: *App, _: *mach.Engine) void {
|
||||||
app.depth_texture_view.release();
|
app.depth_texture_view.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(app: *App, engine: *mach.Engine) !void {
|
pub fn update(app: *App, core: *mach.Core) !void {
|
||||||
while (engine.pollEvent()) |event| {
|
while (core.pollEvent()) |event| {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key_press => |ev| {
|
.key_press => |ev| {
|
||||||
if (ev.key == .space)
|
if (ev.key == .space)
|
||||||
engine.setShouldClose(true);
|
core.setShouldClose(true);
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const cube_view = app.cube_texture_view_render;
|
const cube_view = app.cube_texture_view_render;
|
||||||
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
|
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||||
|
|
||||||
const cube_color_attachment = gpu.RenderPassColorAttachment{
|
const cube_color_attachment = gpu.RenderPassColorAttachment{
|
||||||
.view = cube_view,
|
.view = cube_view,
|
||||||
|
|
@ -258,7 +258,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
.stencil_store_op = .none,
|
.stencil_store_op = .none,
|
||||||
};
|
};
|
||||||
|
|
||||||
const encoder = engine.device.createCommandEncoder(null);
|
const encoder = core.device.createCommandEncoder(null);
|
||||||
const cube_render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
const cube_render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
||||||
.color_attachments = &.{cube_color_attachment},
|
.color_attachments = &.{cube_color_attachment},
|
||||||
.depth_stencil_attachment = &depth_stencil_attachment,
|
.depth_stencil_attachment = &depth_stencil_attachment,
|
||||||
|
|
@ -278,7 +278,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
);
|
);
|
||||||
const proj = zm.perspectiveFovRh(
|
const proj = zm.perspectiveFovRh(
|
||||||
(std.math.pi * 2.0 / 5.0),
|
(std.math.pi * 2.0 / 5.0),
|
||||||
@intToFloat(f32, engine.current_desc.width) / @intToFloat(f32, engine.current_desc.height),
|
@intToFloat(f32, core.current_desc.width) / @intToFloat(f32, core.current_desc.height),
|
||||||
1,
|
1,
|
||||||
100,
|
100,
|
||||||
);
|
);
|
||||||
|
|
@ -303,7 +303,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
&gpu.ImageCopyTexture{
|
&gpu.ImageCopyTexture{
|
||||||
.texture = app.cube_texture,
|
.texture = app.cube_texture,
|
||||||
},
|
},
|
||||||
&.{ .width = engine.current_desc.width, .height = engine.current_desc.height },
|
&.{ .width = core.current_desc.width, .height = core.current_desc.height },
|
||||||
);
|
);
|
||||||
|
|
||||||
const cube_pass = encoder.beginRenderPass(&cube_render_pass_info);
|
const cube_pass = encoder.beginRenderPass(&cube_render_pass_info);
|
||||||
|
|
@ -319,30 +319,30 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
app.queue.submit(&.{command});
|
app.queue.submit(&.{command});
|
||||||
command.release();
|
command.release();
|
||||||
engine.swap_chain.?.present();
|
core.swap_chain.?.present();
|
||||||
back_buffer_view.release();
|
back_buffer_view.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
|
pub fn resize(app: *App, core: *mach.Core, width: u32, height: u32) !void {
|
||||||
if (app.depth_texture != null) {
|
if (app.depth_texture != null) {
|
||||||
app.depth_texture.?.release();
|
app.depth_texture.?.release();
|
||||||
app.depth_texture = engine.device.createTexture(&gpu.Texture.Descriptor{
|
app.depth_texture = core.device.createTexture(&gpu.Texture.Descriptor{
|
||||||
.usage = .{ .render_attachment = true },
|
.usage = .{ .render_attachment = true },
|
||||||
.size = .{ .width = width, .height = height },
|
.size = .{ .width = width, .height = height },
|
||||||
.format = .depth24_plus,
|
.format = .depth24_plus,
|
||||||
});
|
});
|
||||||
|
|
||||||
app.cube_texture.release();
|
app.cube_texture.release();
|
||||||
app.cube_texture = engine.device.createTexture(&gpu.Texture.Descriptor{
|
app.cube_texture = core.device.createTexture(&gpu.Texture.Descriptor{
|
||||||
.usage = .{ .texture_binding = true, .copy_dst = true },
|
.usage = .{ .texture_binding = true, .copy_dst = true },
|
||||||
.size = .{ .width = width, .height = height },
|
.size = .{ .width = width, .height = height },
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
});
|
});
|
||||||
app.cube_texture_render.release();
|
app.cube_texture_render.release();
|
||||||
app.cube_texture_render = engine.device.createTexture(&gpu.Texture.Descriptor{
|
app.cube_texture_render = core.device.createTexture(&gpu.Texture.Descriptor{
|
||||||
.usage = .{ .render_attachment = true, .copy_src = true },
|
.usage = .{ .render_attachment = true, .copy_src = true },
|
||||||
.size = .{ .width = width, .height = height },
|
.size = .{ .width = width, .height = height },
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
});
|
});
|
||||||
|
|
||||||
app.depth_texture_view.release();
|
app.depth_texture_view.release();
|
||||||
|
|
@ -355,21 +355,21 @@ pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
|
||||||
|
|
||||||
app.cube_texture_view.release();
|
app.cube_texture_view.release();
|
||||||
app.cube_texture_view = app.cube_texture.createView(&gpu.TextureView.Descriptor{
|
app.cube_texture_view = app.cube_texture.createView(&gpu.TextureView.Descriptor{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.dimension = .dimension_2d,
|
.dimension = .dimension_2d,
|
||||||
.mip_level_count = 1,
|
.mip_level_count = 1,
|
||||||
.array_layer_count = 1,
|
.array_layer_count = 1,
|
||||||
});
|
});
|
||||||
app.cube_texture_view_render.release();
|
app.cube_texture_view_render.release();
|
||||||
app.cube_texture_view_render = app.cube_texture_render.createView(&gpu.TextureView.Descriptor{
|
app.cube_texture_view_render = app.cube_texture_render.createView(&gpu.TextureView.Descriptor{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.dimension = .dimension_2d,
|
.dimension = .dimension_2d,
|
||||||
.mip_level_count = 1,
|
.mip_level_count = 1,
|
||||||
.array_layer_count = 1,
|
.array_layer_count = 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
app.bind_group.release();
|
app.bind_group.release();
|
||||||
app.bind_group = engine.device.createBindGroup(
|
app.bind_group = core.device.createBindGroup(
|
||||||
&gpu.BindGroup.Descriptor{
|
&gpu.BindGroup.Descriptor{
|
||||||
.layout = app.bgl,
|
.layout = app.bgl,
|
||||||
.entries = &.{
|
.entries = &.{
|
||||||
|
|
@ -380,7 +380,7 @@ pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
app.depth_texture = engine.device.createTexture(&gpu.Texture.Descriptor{
|
app.depth_texture = core.device.createTexture(&gpu.Texture.Descriptor{
|
||||||
.usage = .{ .render_attachment = true },
|
.usage = .{ .render_attachment = true },
|
||||||
.size = .{ .width = width, .height = height },
|
.size = .{ .width = width, .height = height },
|
||||||
.format = .depth24_plus,
|
.format = .depth24_plus,
|
||||||
|
|
|
||||||
|
|
@ -31,21 +31,21 @@ update_frag_uniform_buffer: bool,
|
||||||
bind_group: gpu.BindGroup,
|
bind_group: gpu.BindGroup,
|
||||||
texture_atlas_data: AtlasRGB8,
|
texture_atlas_data: AtlasRGB8,
|
||||||
|
|
||||||
pub fn init(app: *App, engine: *mach.Engine) !void {
|
pub fn init(app: *App, core: *mach.Core) !void {
|
||||||
try engine.setOptions(.{
|
try core.setOptions(.{
|
||||||
.width = 640,
|
.width = 640,
|
||||||
.height = 480,
|
.height = 480,
|
||||||
.size_min = .{ .width = 20, .height = 20 },
|
.size_min = .{ .width = 20, .height = 20 },
|
||||||
});
|
});
|
||||||
|
|
||||||
const queue = engine.device.getQueue();
|
const queue = core.device.getQueue();
|
||||||
|
|
||||||
// TODO: Refactor texture atlas size number
|
// TODO: Refactor texture atlas size number
|
||||||
app.texture_atlas_data = try AtlasRGB8.init(engine.allocator, 1280);
|
app.texture_atlas_data = try AtlasRGB8.init(core.allocator, 1280);
|
||||||
const atlas_size = gpu.Extent3D{ .width = app.texture_atlas_data.size, .height = app.texture_atlas_data.size };
|
const atlas_size = gpu.Extent3D{ .width = app.texture_atlas_data.size, .height = app.texture_atlas_data.size };
|
||||||
const atlas_float_size = @intToFloat(f32, app.texture_atlas_data.size);
|
const atlas_float_size = @intToFloat(f32, app.texture_atlas_data.size);
|
||||||
|
|
||||||
const texture = engine.device.createTexture(&.{
|
const texture = core.device.createTexture(&.{
|
||||||
.size = atlas_size,
|
.size = atlas_size,
|
||||||
.format = .rgba8_unorm,
|
.format = .rgba8_unorm,
|
||||||
.usage = .{
|
.usage = .{
|
||||||
|
|
@ -59,35 +59,35 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
.rows_per_image = @intCast(u32, atlas_size.height),
|
.rows_per_image = @intCast(u32, atlas_size.height),
|
||||||
};
|
};
|
||||||
|
|
||||||
const img = try zigimg.Image.fromMemory(engine.allocator, @embedFile("../assets/gotta-go-fast.png"));
|
const img = try zigimg.Image.fromMemory(core.allocator, @embedFile("../assets/gotta-go-fast.png"));
|
||||||
defer img.deinit();
|
defer img.deinit();
|
||||||
|
|
||||||
const atlas_img_region = try app.texture_atlas_data.reserve(engine.allocator, @truncate(u32, img.width), @truncate(u32, img.height));
|
const atlas_img_region = try app.texture_atlas_data.reserve(core.allocator, @truncate(u32, img.width), @truncate(u32, img.height));
|
||||||
const img_uv_data = atlas_img_region.getUVData(atlas_float_size);
|
const img_uv_data = atlas_img_region.getUVData(atlas_float_size);
|
||||||
|
|
||||||
switch (img.pixels.?) {
|
switch (img.pixels.?) {
|
||||||
.Rgba32 => |pixels| app.texture_atlas_data.set(atlas_img_region, pixels),
|
.Rgba32 => |pixels| app.texture_atlas_data.set(atlas_img_region, pixels),
|
||||||
.Rgb24 => |pixels| {
|
.Rgb24 => |pixels| {
|
||||||
const data = try rgb24ToRgba32(engine.allocator, pixels);
|
const data = try rgb24ToRgba32(core.allocator, pixels);
|
||||||
defer data.deinit(engine.allocator);
|
defer data.deinit(core.allocator);
|
||||||
app.texture_atlas_data.set(atlas_img_region, data.Rgba32);
|
app.texture_atlas_data.set(atlas_img_region, data.Rgba32);
|
||||||
},
|
},
|
||||||
else => @panic("unsupported image color format"),
|
else => @panic("unsupported image color format"),
|
||||||
}
|
}
|
||||||
|
|
||||||
const white_tex_scale = 80;
|
const white_tex_scale = 80;
|
||||||
var atlas_white_region = try app.texture_atlas_data.reserve(engine.allocator, white_tex_scale, white_tex_scale);
|
var atlas_white_region = try app.texture_atlas_data.reserve(core.allocator, white_tex_scale, white_tex_scale);
|
||||||
atlas_white_region.x += 1;
|
atlas_white_region.x += 1;
|
||||||
atlas_white_region.y += 1;
|
atlas_white_region.y += 1;
|
||||||
atlas_white_region.width -= 2;
|
atlas_white_region.width -= 2;
|
||||||
atlas_white_region.height -= 2;
|
atlas_white_region.height -= 2;
|
||||||
const white_texture_uv_data = atlas_white_region.getUVData(atlas_float_size);
|
const white_texture_uv_data = atlas_white_region.getUVData(atlas_float_size);
|
||||||
var white_tex_data = try engine.allocator.alloc(zigimg.color.Rgba32, white_tex_scale * white_tex_scale);
|
var white_tex_data = try core.allocator.alloc(zigimg.color.Rgba32, white_tex_scale * white_tex_scale);
|
||||||
std.mem.set(zigimg.color.Rgba32, white_tex_data, zigimg.color.Rgba32.initRGB(0xff, 0xff, 0xff));
|
std.mem.set(zigimg.color.Rgba32, white_tex_data, zigimg.color.Rgba32.initRGB(0xff, 0xff, 0xff));
|
||||||
app.texture_atlas_data.set(atlas_white_region, white_tex_data);
|
app.texture_atlas_data.set(atlas_white_region, white_tex_data);
|
||||||
|
|
||||||
app.vertices = try std.ArrayList(draw.Vertex).initCapacity(engine.allocator, 9);
|
app.vertices = try std.ArrayList(draw.Vertex).initCapacity(core.allocator, 9);
|
||||||
app.fragment_uniform_list = try std.ArrayList(draw.FragUniform).initCapacity(engine.allocator, 3);
|
app.fragment_uniform_list = try std.ArrayList(draw.FragUniform).initCapacity(core.allocator, 3);
|
||||||
|
|
||||||
// Quick test for using freetype
|
// Quick test for using freetype
|
||||||
const lib = try ft.Library.init();
|
const lib = try ft.Library.init();
|
||||||
|
|
@ -95,13 +95,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
const size_multiplier = 5;
|
const size_multiplier = 5;
|
||||||
const character = "è";
|
const character = "è";
|
||||||
var label = try Label.init(lib, "freetype/upstream/assets/FiraSans-Regular.ttf", 0, 110 * size_multiplier, engine.allocator);
|
var label = try Label.init(lib, "freetype/upstream/assets/FiraSans-Regular.ttf", 0, 110 * size_multiplier, core.allocator);
|
||||||
defer label.deinit();
|
defer label.deinit();
|
||||||
// try label.print(app, "All your game's bases are belong to us èçòà", .{}, @Vector(2, f32){ 0, 420 }, @Vector(4, f32){ 1, 1, 1, 1 });
|
// try label.print(app, "All your game's bases are belong to us èçòà", .{}, @Vector(2, f32){ 0, 420 }, @Vector(4, f32){ 1, 1, 1, 1 });
|
||||||
try label.print(app, character, .{}, @Vector(2, f32){ 50 * size_multiplier, 40 }, @Vector(4, f32){ 1, 1, 1, 1 });
|
try label.print(app, character, .{}, @Vector(2, f32){ 50 * size_multiplier, 40 }, @Vector(4, f32){ 1, 1, 1, 1 });
|
||||||
|
|
||||||
var resizable_label: ResizableLabel = undefined;
|
var resizable_label: ResizableLabel = undefined;
|
||||||
try resizable_label.init(lib, "freetype/upstream/assets/FiraSans-Regular.ttf", 0, engine.allocator, white_texture_uv_data);
|
try resizable_label.init(lib, "freetype/upstream/assets/FiraSans-Regular.ttf", 0, core.allocator, white_texture_uv_data);
|
||||||
defer resizable_label.deinit();
|
defer resizable_label.deinit();
|
||||||
try resizable_label.print(app, character, .{}, @Vector(4, f32){ 0, 40, 0, 0 }, @Vector(4, f32){ 1, 1, 1, 1 }, 80 * size_multiplier);
|
try resizable_label.print(app, character, .{}, @Vector(4, f32){ 0, 40, 0, 0 }, @Vector(4, f32){ 1, 1, 1, 1 }, 80 * size_multiplier);
|
||||||
|
|
||||||
|
|
@ -113,7 +113,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
app.texture_atlas_data.data,
|
app.texture_atlas_data.data,
|
||||||
);
|
);
|
||||||
|
|
||||||
const wsize = engine.getWindowSize();
|
const wsize = core.getWindowSize();
|
||||||
const window_width = @intToFloat(f32, wsize.width);
|
const window_width = @intToFloat(f32, wsize.width);
|
||||||
const window_height = @intToFloat(f32, wsize.height);
|
const window_height = @intToFloat(f32, wsize.height);
|
||||||
const triangle_scale = 250;
|
const triangle_scale = 250;
|
||||||
|
|
@ -128,12 +128,12 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
// try draw.quad(app, .{ 0, 0 }, .{ 480, 480 }, .{}, .{ .bottom_left = .{ 0, 0 }, .width_and_height = .{ 1, 1 } });
|
// try draw.quad(app, .{ 0, 0 }, .{ 480, 480 }, .{}, .{ .bottom_left = .{ 0, 0 }, .width_and_height = .{ 1, 1 } });
|
||||||
// try draw.circle(app, .{ window_width / 2, window_height / 2 }, window_height / 2 - 10, .{ 0, 0.5, 0.75, 1.0 }, white_texture_uv_data);
|
// try draw.circle(app, .{ window_width / 2, window_height / 2 }, window_height / 2 - 10, .{ 0, 0.5, 0.75, 1.0 }, white_texture_uv_data);
|
||||||
|
|
||||||
const vs_module = engine.device.createShaderModule(&.{
|
const vs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my vertex shader",
|
.label = "my vertex shader",
|
||||||
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
||||||
const fs_module = engine.device.createShaderModule(&.{
|
const fs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my fragment shader",
|
.label = "my fragment shader",
|
||||||
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -152,7 +152,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
};
|
};
|
||||||
|
|
||||||
const color_target = gpu.ColorTargetState{
|
const color_target = gpu.ColorTargetState{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.blend = &blend,
|
.blend = &blend,
|
||||||
.write_mask = gpu.ColorWriteMask.all,
|
.write_mask = gpu.ColorWriteMask.all,
|
||||||
};
|
};
|
||||||
|
|
@ -167,13 +167,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
const fbgle = gpu.BindGroupLayout.Entry.buffer(1, .{ .fragment = true }, .read_only_storage, true, 0);
|
const fbgle = gpu.BindGroupLayout.Entry.buffer(1, .{ .fragment = true }, .read_only_storage, true, 0);
|
||||||
const sbgle = gpu.BindGroupLayout.Entry.sampler(2, .{ .fragment = true }, .filtering);
|
const sbgle = gpu.BindGroupLayout.Entry.sampler(2, .{ .fragment = true }, .filtering);
|
||||||
const tbgle = gpu.BindGroupLayout.Entry.texture(3, .{ .fragment = true }, .float, .dimension_2d, false);
|
const tbgle = gpu.BindGroupLayout.Entry.texture(3, .{ .fragment = true }, .float, .dimension_2d, false);
|
||||||
const bgl = engine.device.createBindGroupLayout(
|
const bgl = core.device.createBindGroupLayout(
|
||||||
&gpu.BindGroupLayout.Descriptor{
|
&gpu.BindGroupLayout.Descriptor{
|
||||||
.entries = &.{ vbgle, fbgle, sbgle, tbgle },
|
.entries = &.{ vbgle, fbgle, sbgle, tbgle },
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
|
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
|
||||||
const pipeline_layout = engine.device.createPipelineLayout(&.{
|
const pipeline_layout = core.device.createPipelineLayout(&.{
|
||||||
.bind_group_layouts = &bind_group_layouts,
|
.bind_group_layouts = &bind_group_layouts,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -199,30 +199,30 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const vertex_buffer = engine.device.createBuffer(&.{
|
const vertex_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .copy_dst = true, .vertex = true },
|
.usage = .{ .copy_dst = true, .vertex = true },
|
||||||
.size = @sizeOf(draw.Vertex) * app.vertices.items.len,
|
.size = @sizeOf(draw.Vertex) * app.vertices.items.len,
|
||||||
.mapped_at_creation = false,
|
.mapped_at_creation = false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const vertex_uniform_buffer = engine.device.createBuffer(&.{
|
const vertex_uniform_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .copy_dst = true, .uniform = true },
|
.usage = .{ .copy_dst = true, .uniform = true },
|
||||||
.size = @sizeOf(draw.VertexUniform),
|
.size = @sizeOf(draw.VertexUniform),
|
||||||
.mapped_at_creation = false,
|
.mapped_at_creation = false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const frag_uniform_buffer = engine.device.createBuffer(&.{
|
const frag_uniform_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .copy_dst = true, .storage = true },
|
.usage = .{ .copy_dst = true, .storage = true },
|
||||||
.size = @sizeOf(draw.FragUniform) * app.fragment_uniform_list.items.len,
|
.size = @sizeOf(draw.FragUniform) * app.fragment_uniform_list.items.len,
|
||||||
.mapped_at_creation = false,
|
.mapped_at_creation = false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const sampler = engine.device.createSampler(&.{
|
const sampler = core.device.createSampler(&.{
|
||||||
// .mag_filter = .linear,
|
// .mag_filter = .linear,
|
||||||
// .min_filter = .linear,
|
// .min_filter = .linear,
|
||||||
});
|
});
|
||||||
|
|
||||||
const bind_group = engine.device.createBindGroup(
|
const bind_group = core.device.createBindGroup(
|
||||||
&gpu.BindGroup.Descriptor{
|
&gpu.BindGroup.Descriptor{
|
||||||
.layout = bgl,
|
.layout = bgl,
|
||||||
.entries = &.{
|
.entries = &.{
|
||||||
|
|
@ -234,7 +234,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
app.pipeline = engine.device.createRenderPipeline(&pipeline_descriptor);
|
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||||
app.queue = queue;
|
app.queue = queue;
|
||||||
app.vertex_buffer = vertex_buffer;
|
app.vertex_buffer = vertex_buffer;
|
||||||
app.vertex_uniform_buffer = vertex_uniform_buffer;
|
app.vertex_uniform_buffer = vertex_uniform_buffer;
|
||||||
|
|
@ -250,28 +250,28 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
bgl.release();
|
bgl.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(app: *App, engine: *mach.Engine) void {
|
pub fn deinit(app: *App, core: *mach.Core) void {
|
||||||
app.vertex_buffer.release();
|
app.vertex_buffer.release();
|
||||||
app.vertex_uniform_buffer.release();
|
app.vertex_uniform_buffer.release();
|
||||||
app.frag_uniform_buffer.release();
|
app.frag_uniform_buffer.release();
|
||||||
app.bind_group.release();
|
app.bind_group.release();
|
||||||
app.vertices.deinit();
|
app.vertices.deinit();
|
||||||
app.fragment_uniform_list.deinit();
|
app.fragment_uniform_list.deinit();
|
||||||
app.texture_atlas_data.deinit(engine.allocator);
|
app.texture_atlas_data.deinit(core.allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(app: *App, engine: *mach.Engine) !void {
|
pub fn update(app: *App, core: *mach.Core) !void {
|
||||||
while (engine.pollEvent()) |event| {
|
while (core.pollEvent()) |event| {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key_press => |ev| {
|
.key_press => |ev| {
|
||||||
if (ev.key == .space)
|
if (ev.key == .space)
|
||||||
engine.setShouldClose(true);
|
core.setShouldClose(true);
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
|
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||||
const color_attachment = gpu.RenderPassColorAttachment{
|
const color_attachment = gpu.RenderPassColorAttachment{
|
||||||
.view = back_buffer_view,
|
.view = back_buffer_view,
|
||||||
.resolve_target = null,
|
.resolve_target = null,
|
||||||
|
|
@ -280,7 +280,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
.store_op = .store,
|
.store_op = .store,
|
||||||
};
|
};
|
||||||
|
|
||||||
const encoder = engine.device.createCommandEncoder(null);
|
const encoder = core.device.createCommandEncoder(null);
|
||||||
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
||||||
.color_attachments = &.{color_attachment},
|
.color_attachments = &.{color_attachment},
|
||||||
};
|
};
|
||||||
|
|
@ -295,7 +295,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
app.update_frag_uniform_buffer = false;
|
app.update_frag_uniform_buffer = false;
|
||||||
}
|
}
|
||||||
if (app.update_vertex_uniform_buffer) {
|
if (app.update_vertex_uniform_buffer) {
|
||||||
encoder.writeBuffer(app.vertex_uniform_buffer, 0, draw.VertexUniform, &.{try getVertexUniformBufferObject(engine)});
|
encoder.writeBuffer(app.vertex_uniform_buffer, 0, draw.VertexUniform, &.{try getVertexUniformBufferObject(core)});
|
||||||
app.update_vertex_uniform_buffer = false;
|
app.update_vertex_uniform_buffer = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -313,11 +313,11 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
app.queue.submit(&.{command});
|
app.queue.submit(&.{command});
|
||||||
command.release();
|
command.release();
|
||||||
engine.swap_chain.?.present();
|
core.swap_chain.?.present();
|
||||||
back_buffer_view.release();
|
back_buffer_view.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resize(app: *App, _: *mach.Engine, _: u32, _: u32) !void {
|
pub fn resize(app: *App, _: *mach.Core, _: u32, _: u32) !void {
|
||||||
app.update_vertex_uniform_buffer = true;
|
app.update_vertex_uniform_buffer = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -331,12 +331,12 @@ fn rgb24ToRgba32(allocator: std.mem.Allocator, in: []zigimg.color.Rgb24) !zigimg
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move to draw.zig
|
// Move to draw.zig
|
||||||
pub fn getVertexUniformBufferObject(engine: *mach.Engine) !draw.VertexUniform {
|
pub fn getVertexUniformBufferObject(core: *mach.Core) !draw.VertexUniform {
|
||||||
// Note: We use window width/height here, not framebuffer width/height.
|
// Note: We use window width/height here, not framebuffer width/height.
|
||||||
// On e.g. macOS, window size may be 640x480 while framebuffer size may be
|
// On e.g. macOS, window size may be 640x480 while framebuffer size may be
|
||||||
// 1280x960 (subpixels.) Doing this lets us use a pixel, not subpixel,
|
// 1280x960 (subpixels.) Doing this lets us use a pixel, not subpixel,
|
||||||
// coordinate system.
|
// coordinate system.
|
||||||
const window_size = engine.getWindowSize();
|
const window_size = core.getWindowSize();
|
||||||
const proj = zm.orthographicRh(
|
const proj = zm.orthographicRh(
|
||||||
@intToFloat(f32, window_size.width),
|
@intToFloat(f32, window_size.width),
|
||||||
@intToFloat(f32, window_size.height),
|
@intToFloat(f32, window_size.height),
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,14 @@ bind_group: gpu.BindGroup,
|
||||||
|
|
||||||
const App = @This();
|
const App = @This();
|
||||||
|
|
||||||
pub fn init(app: *App, engine: *mach.Engine) !void {
|
pub fn init(app: *App, core: *mach.Core) !void {
|
||||||
timer = try mach.Timer.start();
|
timer = try mach.Timer.start();
|
||||||
|
|
||||||
try engine.setOptions(.{
|
try core.setOptions(.{
|
||||||
.size_min = .{ .width = 20, .height = 20 },
|
.size_min = .{ .width = 20, .height = 20 },
|
||||||
});
|
});
|
||||||
|
|
||||||
const vs_module = engine.device.createShaderModule(&.{
|
const vs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my vertex shader",
|
.label = "my vertex shader",
|
||||||
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -43,13 +43,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
.attributes = &vertex_attributes,
|
.attributes = &vertex_attributes,
|
||||||
};
|
};
|
||||||
|
|
||||||
const fs_module = engine.device.createShaderModule(&.{
|
const fs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my fragment shader",
|
.label = "my fragment shader",
|
||||||
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
||||||
const color_target = gpu.ColorTargetState{
|
const color_target = gpu.ColorTargetState{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.blend = null,
|
.blend = null,
|
||||||
.write_mask = gpu.ColorWriteMask.all,
|
.write_mask = gpu.ColorWriteMask.all,
|
||||||
};
|
};
|
||||||
|
|
@ -61,14 +61,14 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
};
|
};
|
||||||
|
|
||||||
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
|
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
|
||||||
const bgl = engine.device.createBindGroupLayout(
|
const bgl = core.device.createBindGroupLayout(
|
||||||
&gpu.BindGroupLayout.Descriptor{
|
&gpu.BindGroupLayout.Descriptor{
|
||||||
.entries = &.{bgle},
|
.entries = &.{bgle},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
|
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
|
||||||
const pipeline_layout = engine.device.createPipelineLayout(&.{
|
const pipeline_layout = core.device.createPipelineLayout(&.{
|
||||||
.bind_group_layouts = &bind_group_layouts,
|
.bind_group_layouts = &bind_group_layouts,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -94,7 +94,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const vertex_buffer = engine.device.createBuffer(&.{
|
const vertex_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .vertex = true },
|
.usage = .{ .vertex = true },
|
||||||
.size = @sizeOf(Vertex) * vertices.len,
|
.size = @sizeOf(Vertex) * vertices.len,
|
||||||
.mapped_at_creation = true,
|
.mapped_at_creation = true,
|
||||||
|
|
@ -107,12 +107,12 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
const y_count = 4;
|
const y_count = 4;
|
||||||
const num_instances = x_count * y_count;
|
const num_instances = x_count * y_count;
|
||||||
|
|
||||||
const uniform_buffer = engine.device.createBuffer(&.{
|
const uniform_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .copy_dst = true, .uniform = true },
|
.usage = .{ .copy_dst = true, .uniform = true },
|
||||||
.size = @sizeOf(UniformBufferObject) * num_instances,
|
.size = @sizeOf(UniformBufferObject) * num_instances,
|
||||||
.mapped_at_creation = false,
|
.mapped_at_creation = false,
|
||||||
});
|
});
|
||||||
const bind_group = engine.device.createBindGroup(
|
const bind_group = core.device.createBindGroup(
|
||||||
&gpu.BindGroup.Descriptor{
|
&gpu.BindGroup.Descriptor{
|
||||||
.layout = bgl,
|
.layout = bgl,
|
||||||
.entries = &.{
|
.entries = &.{
|
||||||
|
|
@ -121,8 +121,8 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
app.pipeline = engine.device.createRenderPipeline(&pipeline_descriptor);
|
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||||
app.queue = engine.device.getQueue();
|
app.queue = core.device.getQueue();
|
||||||
app.vertex_buffer = vertex_buffer;
|
app.vertex_buffer = vertex_buffer;
|
||||||
app.uniform_buffer = uniform_buffer;
|
app.uniform_buffer = uniform_buffer;
|
||||||
app.bind_group = bind_group;
|
app.bind_group = bind_group;
|
||||||
|
|
@ -133,24 +133,24 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
bgl.release();
|
bgl.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(app: *App, _: *mach.Engine) void {
|
pub fn deinit(app: *App, _: *mach.Core) void {
|
||||||
app.vertex_buffer.release();
|
app.vertex_buffer.release();
|
||||||
app.bind_group.release();
|
app.bind_group.release();
|
||||||
app.uniform_buffer.release();
|
app.uniform_buffer.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(app: *App, engine: *mach.Engine) !void {
|
pub fn update(app: *App, core: *mach.Core) !void {
|
||||||
while (engine.pollEvent()) |event| {
|
while (core.pollEvent()) |event| {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key_press => |ev| {
|
.key_press => |ev| {
|
||||||
if (ev.key == .space)
|
if (ev.key == .space)
|
||||||
engine.setShouldClose(true);
|
core.setShouldClose(true);
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
|
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||||
const color_attachment = gpu.RenderPassColorAttachment{
|
const color_attachment = gpu.RenderPassColorAttachment{
|
||||||
.view = back_buffer_view,
|
.view = back_buffer_view,
|
||||||
.resolve_target = null,
|
.resolve_target = null,
|
||||||
|
|
@ -159,7 +159,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
.store_op = .store,
|
.store_op = .store,
|
||||||
};
|
};
|
||||||
|
|
||||||
const encoder = engine.device.createCommandEncoder(null);
|
const encoder = core.device.createCommandEncoder(null);
|
||||||
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
||||||
.color_attachments = &.{color_attachment},
|
.color_attachments = &.{color_attachment},
|
||||||
};
|
};
|
||||||
|
|
@ -167,7 +167,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
{
|
{
|
||||||
const proj = zm.perspectiveFovRh(
|
const proj = zm.perspectiveFovRh(
|
||||||
(std.math.pi / 3.0),
|
(std.math.pi / 3.0),
|
||||||
@intToFloat(f32, engine.current_desc.width) / @intToFloat(f32, engine.current_desc.height),
|
@intToFloat(f32, core.current_desc.width) / @intToFloat(f32, core.current_desc.height),
|
||||||
10,
|
10,
|
||||||
30,
|
30,
|
||||||
);
|
);
|
||||||
|
|
@ -207,6 +207,6 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
app.queue.submit(&.{command});
|
app.queue.submit(&.{command});
|
||||||
command.release();
|
command.release();
|
||||||
engine.swap_chain.?.present();
|
core.swap_chain.?.present();
|
||||||
back_buffer_view.release();
|
back_buffer_view.release();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,14 @@ vertex_buffer: gpu.Buffer,
|
||||||
uniform_buffer: gpu.Buffer,
|
uniform_buffer: gpu.Buffer,
|
||||||
bind_group: gpu.BindGroup,
|
bind_group: gpu.BindGroup,
|
||||||
|
|
||||||
pub fn init(app: *App, engine: *mach.Engine) !void {
|
pub fn init(app: *App, core: *mach.Core) !void {
|
||||||
timer = try mach.Timer.start();
|
timer = try mach.Timer.start();
|
||||||
|
|
||||||
try engine.setOptions(.{
|
try core.setOptions(.{
|
||||||
.size_min = .{ .width = 20, .height = 20 },
|
.size_min = .{ .width = 20, .height = 20 },
|
||||||
});
|
});
|
||||||
|
|
||||||
const vs_module = engine.device.createShaderModule(&.{
|
const vs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my vertex shader",
|
.label = "my vertex shader",
|
||||||
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -43,7 +43,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
.attributes = &vertex_attributes,
|
.attributes = &vertex_attributes,
|
||||||
};
|
};
|
||||||
|
|
||||||
const fs_module = engine.device.createShaderModule(&.{
|
const fs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my fragment shader",
|
.label = "my fragment shader",
|
||||||
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -61,7 +61,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const color_target = gpu.ColorTargetState{
|
const color_target = gpu.ColorTargetState{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.blend = &blend,
|
.blend = &blend,
|
||||||
.write_mask = gpu.ColorWriteMask.all,
|
.write_mask = gpu.ColorWriteMask.all,
|
||||||
};
|
};
|
||||||
|
|
@ -73,14 +73,14 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
};
|
};
|
||||||
|
|
||||||
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
|
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
|
||||||
const bgl = engine.device.createBindGroupLayout(
|
const bgl = core.device.createBindGroupLayout(
|
||||||
&gpu.BindGroupLayout.Descriptor{
|
&gpu.BindGroupLayout.Descriptor{
|
||||||
.entries = &.{bgle},
|
.entries = &.{bgle},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
|
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
|
||||||
const pipeline_layout = engine.device.createPipelineLayout(&.{
|
const pipeline_layout = core.device.createPipelineLayout(&.{
|
||||||
.bind_group_layouts = &bind_group_layouts,
|
.bind_group_layouts = &bind_group_layouts,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -106,7 +106,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const vertex_buffer = engine.device.createBuffer(&.{
|
const vertex_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .vertex = true },
|
.usage = .{ .vertex = true },
|
||||||
.size = @sizeOf(Vertex) * vertices.len,
|
.size = @sizeOf(Vertex) * vertices.len,
|
||||||
.mapped_at_creation = true,
|
.mapped_at_creation = true,
|
||||||
|
|
@ -115,12 +115,12 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
std.mem.copy(Vertex, vertex_mapped, vertices[0..]);
|
std.mem.copy(Vertex, vertex_mapped, vertices[0..]);
|
||||||
vertex_buffer.unmap();
|
vertex_buffer.unmap();
|
||||||
|
|
||||||
const uniform_buffer = engine.device.createBuffer(&.{
|
const uniform_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .copy_dst = true, .uniform = true },
|
.usage = .{ .copy_dst = true, .uniform = true },
|
||||||
.size = @sizeOf(UniformBufferObject),
|
.size = @sizeOf(UniformBufferObject),
|
||||||
.mapped_at_creation = false,
|
.mapped_at_creation = false,
|
||||||
});
|
});
|
||||||
const bind_group = engine.device.createBindGroup(
|
const bind_group = core.device.createBindGroup(
|
||||||
&gpu.BindGroup.Descriptor{
|
&gpu.BindGroup.Descriptor{
|
||||||
.layout = bgl,
|
.layout = bgl,
|
||||||
.entries = &.{
|
.entries = &.{
|
||||||
|
|
@ -129,8 +129,8 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
app.pipeline = engine.device.createRenderPipeline(&pipeline_descriptor);
|
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||||
app.queue = engine.device.getQueue();
|
app.queue = core.device.getQueue();
|
||||||
app.vertex_buffer = vertex_buffer;
|
app.vertex_buffer = vertex_buffer;
|
||||||
app.uniform_buffer = uniform_buffer;
|
app.uniform_buffer = uniform_buffer;
|
||||||
app.bind_group = bind_group;
|
app.bind_group = bind_group;
|
||||||
|
|
@ -141,24 +141,24 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
bgl.release();
|
bgl.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(app: *App, _: *mach.Engine) void {
|
pub fn deinit(app: *App, _: *mach.Core) void {
|
||||||
app.vertex_buffer.release();
|
app.vertex_buffer.release();
|
||||||
app.uniform_buffer.release();
|
app.uniform_buffer.release();
|
||||||
app.bind_group.release();
|
app.bind_group.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(app: *App, engine: *mach.Engine) !void {
|
pub fn update(app: *App, core: *mach.Core) !void {
|
||||||
while (engine.pollEvent()) |event| {
|
while (core.pollEvent()) |event| {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key_press => |ev| {
|
.key_press => |ev| {
|
||||||
if (ev.key == .space)
|
if (ev.key == .space)
|
||||||
engine.setShouldClose(true);
|
core.setShouldClose(true);
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
|
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||||
const color_attachment = gpu.RenderPassColorAttachment{
|
const color_attachment = gpu.RenderPassColorAttachment{
|
||||||
.view = back_buffer_view,
|
.view = back_buffer_view,
|
||||||
.resolve_target = null,
|
.resolve_target = null,
|
||||||
|
|
@ -167,7 +167,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
.store_op = .store,
|
.store_op = .store,
|
||||||
};
|
};
|
||||||
|
|
||||||
const encoder = engine.device.createCommandEncoder(null);
|
const encoder = core.device.createCommandEncoder(null);
|
||||||
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
||||||
.color_attachments = &.{color_attachment},
|
.color_attachments = &.{color_attachment},
|
||||||
.depth_stencil_attachment = null,
|
.depth_stencil_attachment = null,
|
||||||
|
|
@ -183,7 +183,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
);
|
);
|
||||||
const proj = zm.perspectiveFovRh(
|
const proj = zm.perspectiveFovRh(
|
||||||
(std.math.pi / 4.0),
|
(std.math.pi / 4.0),
|
||||||
@intToFloat(f32, engine.current_desc.width) / @intToFloat(f32, engine.current_desc.height),
|
@intToFloat(f32, core.current_desc.width) / @intToFloat(f32, core.current_desc.height),
|
||||||
0.1,
|
0.1,
|
||||||
10,
|
10,
|
||||||
);
|
);
|
||||||
|
|
@ -207,6 +207,6 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
app.queue.submit(&.{command});
|
app.queue.submit(&.{command});
|
||||||
command.release();
|
command.release();
|
||||||
engine.swap_chain.?.present();
|
core.swap_chain.?.present();
|
||||||
back_buffer_view.release();
|
back_buffer_view.release();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,14 +23,14 @@ depth_texture_view: gpu.TextureView,
|
||||||
|
|
||||||
const App = @This();
|
const App = @This();
|
||||||
|
|
||||||
pub fn init(app: *App, engine: *mach.Engine) !void {
|
pub fn init(app: *App, core: *mach.Core) !void {
|
||||||
timer = try mach.Timer.start();
|
timer = try mach.Timer.start();
|
||||||
|
|
||||||
try engine.setOptions(.{
|
try core.setOptions(.{
|
||||||
.size_min = .{ .width = 20, .height = 20 },
|
.size_min = .{ .width = 20, .height = 20 },
|
||||||
});
|
});
|
||||||
|
|
||||||
const vs_module = engine.device.createShaderModule(&.{
|
const vs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my vertex shader",
|
.label = "my vertex shader",
|
||||||
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -46,7 +46,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
.attributes = &vertex_attributes,
|
.attributes = &vertex_attributes,
|
||||||
};
|
};
|
||||||
|
|
||||||
const fs_module = engine.device.createShaderModule(&.{
|
const fs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my fragment shader",
|
.label = "my fragment shader",
|
||||||
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -64,7 +64,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const color_target = gpu.ColorTargetState{
|
const color_target = gpu.ColorTargetState{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.blend = &blend,
|
.blend = &blend,
|
||||||
.write_mask = gpu.ColorWriteMask.all,
|
.write_mask = gpu.ColorWriteMask.all,
|
||||||
};
|
};
|
||||||
|
|
@ -98,9 +98,9 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
.cull_mode = .back,
|
.cull_mode = .back,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const pipeline = engine.device.createRenderPipeline(&pipeline_descriptor);
|
const pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||||
|
|
||||||
const vertex_buffer = engine.device.createBuffer(&.{
|
const vertex_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .vertex = true },
|
.usage = .{ .vertex = true },
|
||||||
.size = @sizeOf(Vertex) * vertices.len,
|
.size = @sizeOf(Vertex) * vertices.len,
|
||||||
.mapped_at_creation = true,
|
.mapped_at_creation = true,
|
||||||
|
|
@ -110,15 +110,15 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
vertex_buffer.unmap();
|
vertex_buffer.unmap();
|
||||||
|
|
||||||
// Create a sampler with linear filtering for smooth interpolation.
|
// Create a sampler with linear filtering for smooth interpolation.
|
||||||
const sampler = engine.device.createSampler(&.{
|
const sampler = core.device.createSampler(&.{
|
||||||
.mag_filter = .linear,
|
.mag_filter = .linear,
|
||||||
.min_filter = .linear,
|
.min_filter = .linear,
|
||||||
});
|
});
|
||||||
const queue = engine.device.getQueue();
|
const queue = core.device.getQueue();
|
||||||
const img = try zigimg.Image.fromMemory(engine.allocator, @embedFile("../assets/gotta-go-fast.png"));
|
const img = try zigimg.Image.fromMemory(core.allocator, @embedFile("../assets/gotta-go-fast.png"));
|
||||||
defer img.deinit();
|
defer img.deinit();
|
||||||
const img_size = gpu.Extent3D{ .width = @intCast(u32, img.width), .height = @intCast(u32, img.height) };
|
const img_size = gpu.Extent3D{ .width = @intCast(u32, img.width), .height = @intCast(u32, img.height) };
|
||||||
const cube_texture = engine.device.createTexture(&.{
|
const cube_texture = core.device.createTexture(&.{
|
||||||
.size = img_size,
|
.size = img_size,
|
||||||
.format = .rgba8_unorm,
|
.format = .rgba8_unorm,
|
||||||
.usage = .{
|
.usage = .{
|
||||||
|
|
@ -134,20 +134,20 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
switch (img.pixels.?) {
|
switch (img.pixels.?) {
|
||||||
.Rgba32 => |pixels| queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, zigimg.color.Rgba32, pixels),
|
.Rgba32 => |pixels| queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, zigimg.color.Rgba32, pixels),
|
||||||
.Rgb24 => |pixels| {
|
.Rgb24 => |pixels| {
|
||||||
const data = try rgb24ToRgba32(engine.allocator, pixels);
|
const data = try rgb24ToRgba32(core.allocator, pixels);
|
||||||
defer data.deinit(engine.allocator);
|
defer data.deinit(core.allocator);
|
||||||
queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, zigimg.color.Rgba32, data.Rgba32);
|
queue.writeTexture(&.{ .texture = cube_texture }, &data_layout, &img_size, zigimg.color.Rgba32, data.Rgba32);
|
||||||
},
|
},
|
||||||
else => @panic("unsupported image color format"),
|
else => @panic("unsupported image color format"),
|
||||||
}
|
}
|
||||||
|
|
||||||
const uniform_buffer = engine.device.createBuffer(&.{
|
const uniform_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .copy_dst = true, .uniform = true },
|
.usage = .{ .copy_dst = true, .uniform = true },
|
||||||
.size = @sizeOf(UniformBufferObject),
|
.size = @sizeOf(UniformBufferObject),
|
||||||
.mapped_at_creation = false,
|
.mapped_at_creation = false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const bind_group = engine.device.createBindGroup(
|
const bind_group = core.device.createBindGroup(
|
||||||
&gpu.BindGroup.Descriptor{
|
&gpu.BindGroup.Descriptor{
|
||||||
.layout = pipeline.getBindGroupLayout(0),
|
.layout = pipeline.getBindGroupLayout(0),
|
||||||
.entries = &.{
|
.entries = &.{
|
||||||
|
|
@ -170,7 +170,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
fs_module.release();
|
fs_module.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(app: *App, _: *mach.Engine) void {
|
pub fn deinit(app: *App, _: *mach.Core) void {
|
||||||
app.vertex_buffer.release();
|
app.vertex_buffer.release();
|
||||||
app.uniform_buffer.release();
|
app.uniform_buffer.release();
|
||||||
app.bind_group.release();
|
app.bind_group.release();
|
||||||
|
|
@ -178,18 +178,18 @@ pub fn deinit(app: *App, _: *mach.Engine) void {
|
||||||
app.depth_texture_view.release();
|
app.depth_texture_view.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(app: *App, engine: *mach.Engine) !void {
|
pub fn update(app: *App, core: *mach.Core) !void {
|
||||||
while (engine.pollEvent()) |event| {
|
while (core.pollEvent()) |event| {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key_press => |ev| {
|
.key_press => |ev| {
|
||||||
if (ev.key == .space)
|
if (ev.key == .space)
|
||||||
engine.setShouldClose(true);
|
core.setShouldClose(true);
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
|
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||||
const color_attachment = gpu.RenderPassColorAttachment{
|
const color_attachment = gpu.RenderPassColorAttachment{
|
||||||
.view = back_buffer_view,
|
.view = back_buffer_view,
|
||||||
.clear_value = .{ .r = 0.5, .g = 0.5, .b = 0.5, .a = 0.0 },
|
.clear_value = .{ .r = 0.5, .g = 0.5, .b = 0.5, .a = 0.0 },
|
||||||
|
|
@ -197,7 +197,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
.store_op = .store,
|
.store_op = .store,
|
||||||
};
|
};
|
||||||
|
|
||||||
const encoder = engine.device.createCommandEncoder(null);
|
const encoder = core.device.createCommandEncoder(null);
|
||||||
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
||||||
.color_attachments = &.{color_attachment},
|
.color_attachments = &.{color_attachment},
|
||||||
.depth_stencil_attachment = &.{
|
.depth_stencil_attachment = &.{
|
||||||
|
|
@ -218,7 +218,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
);
|
);
|
||||||
const proj = zm.perspectiveFovRh(
|
const proj = zm.perspectiveFovRh(
|
||||||
(std.math.pi / 4.0),
|
(std.math.pi / 4.0),
|
||||||
@intToFloat(f32, engine.current_desc.width) / @intToFloat(f32, engine.current_desc.height),
|
@intToFloat(f32, core.current_desc.width) / @intToFloat(f32, core.current_desc.height),
|
||||||
0.1,
|
0.1,
|
||||||
10,
|
10,
|
||||||
);
|
);
|
||||||
|
|
@ -242,17 +242,17 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
app.queue.submit(&.{command});
|
app.queue.submit(&.{command});
|
||||||
command.release();
|
command.release();
|
||||||
engine.swap_chain.?.present();
|
core.swap_chain.?.present();
|
||||||
back_buffer_view.release();
|
back_buffer_view.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resize(app: *App, engine: *mach.Engine, width: u32, height: u32) !void {
|
pub fn resize(app: *App, core: *mach.Core, width: u32, height: u32) !void {
|
||||||
// If window is resized, recreate depth buffer otherwise we cannot use it.
|
// If window is resized, recreate depth buffer otherwise we cannot use it.
|
||||||
if (app.depth_texture != null) {
|
if (app.depth_texture != null) {
|
||||||
app.depth_texture.?.release();
|
app.depth_texture.?.release();
|
||||||
app.depth_texture_view.release();
|
app.depth_texture_view.release();
|
||||||
}
|
}
|
||||||
app.depth_texture = engine.device.createTexture(&gpu.Texture.Descriptor{
|
app.depth_texture = core.device.createTexture(&gpu.Texture.Descriptor{
|
||||||
.size = gpu.Extent3D{
|
.size = gpu.Extent3D{
|
||||||
.width = width,
|
.width = width,
|
||||||
.height = height,
|
.height = height,
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,13 @@ const App = @This();
|
||||||
pipeline: gpu.RenderPipeline,
|
pipeline: gpu.RenderPipeline,
|
||||||
queue: gpu.Queue,
|
queue: gpu.Queue,
|
||||||
|
|
||||||
pub fn init(app: *App, engine: *mach.Engine) !void {
|
pub fn init(app: *App, core: *mach.Core) !void {
|
||||||
const vs_module = engine.device.createShaderModule(&.{
|
const vs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my vertex shader",
|
.label = "my vertex shader",
|
||||||
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
||||||
const fs_module = engine.device.createShaderModule(&.{
|
const fs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my fragment shader",
|
.label = "my fragment shader",
|
||||||
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -32,7 +32,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const color_target = gpu.ColorTargetState{
|
const color_target = gpu.ColorTargetState{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.blend = &blend,
|
.blend = &blend,
|
||||||
.write_mask = gpu.ColorWriteMask.all,
|
.write_mask = gpu.ColorWriteMask.all,
|
||||||
};
|
};
|
||||||
|
|
@ -64,17 +64,17 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
app.pipeline = engine.device.createRenderPipeline(&pipeline_descriptor);
|
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||||
app.queue = engine.device.getQueue();
|
app.queue = core.device.getQueue();
|
||||||
|
|
||||||
vs_module.release();
|
vs_module.release();
|
||||||
fs_module.release();
|
fs_module.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(_: *App, _: *mach.Engine) void {}
|
pub fn deinit(_: *App, _: *mach.Core) void {}
|
||||||
|
|
||||||
pub fn update(app: *App, engine: *mach.Engine) !void {
|
pub fn update(app: *App, core: *mach.Core) !void {
|
||||||
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
|
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||||
const color_attachment = gpu.RenderPassColorAttachment{
|
const color_attachment = gpu.RenderPassColorAttachment{
|
||||||
.view = back_buffer_view,
|
.view = back_buffer_view,
|
||||||
.resolve_target = null,
|
.resolve_target = null,
|
||||||
|
|
@ -83,7 +83,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
.store_op = .store,
|
.store_op = .store,
|
||||||
};
|
};
|
||||||
|
|
||||||
const encoder = engine.device.createCommandEncoder(null);
|
const encoder = core.device.createCommandEncoder(null);
|
||||||
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
||||||
.color_attachments = &.{color_attachment},
|
.color_attachments = &.{color_attachment},
|
||||||
.depth_stencil_attachment = null,
|
.depth_stencil_attachment = null,
|
||||||
|
|
@ -99,6 +99,6 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
app.queue.submit(&.{command});
|
app.queue.submit(&.{command});
|
||||||
command.release();
|
command.release();
|
||||||
engine.swap_chain.?.present();
|
core.swap_chain.?.present();
|
||||||
back_buffer_view.release();
|
back_buffer_view.release();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,14 +21,14 @@ bind_group2: gpu.BindGroup,
|
||||||
|
|
||||||
const App = @This();
|
const App = @This();
|
||||||
|
|
||||||
pub fn init(app: *App, engine: *mach.Engine) !void {
|
pub fn init(app: *App, core: *mach.Core) !void {
|
||||||
timer = try mach.Timer.start();
|
timer = try mach.Timer.start();
|
||||||
|
|
||||||
try engine.setOptions(.{
|
try core.setOptions(.{
|
||||||
.size_min = .{ .width = 20, .height = 20 },
|
.size_min = .{ .width = 20, .height = 20 },
|
||||||
});
|
});
|
||||||
|
|
||||||
const vs_module = engine.device.createShaderModule(&.{
|
const vs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my vertex shader",
|
.label = "my vertex shader",
|
||||||
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -44,7 +44,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
.attributes = &vertex_attributes,
|
.attributes = &vertex_attributes,
|
||||||
};
|
};
|
||||||
|
|
||||||
const fs_module = engine.device.createShaderModule(&.{
|
const fs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my fragment shader",
|
.label = "my fragment shader",
|
||||||
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -62,7 +62,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const color_target = gpu.ColorTargetState{
|
const color_target = gpu.ColorTargetState{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.blend = &blend,
|
.blend = &blend,
|
||||||
.write_mask = gpu.ColorWriteMask.all,
|
.write_mask = gpu.ColorWriteMask.all,
|
||||||
};
|
};
|
||||||
|
|
@ -74,14 +74,14 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
};
|
};
|
||||||
|
|
||||||
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
|
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
|
||||||
const bgl = engine.device.createBindGroupLayout(
|
const bgl = core.device.createBindGroupLayout(
|
||||||
&gpu.BindGroupLayout.Descriptor{
|
&gpu.BindGroupLayout.Descriptor{
|
||||||
.entries = &.{bgle},
|
.entries = &.{bgle},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
|
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl};
|
||||||
const pipeline_layout = engine.device.createPipelineLayout(&.{
|
const pipeline_layout = core.device.createPipelineLayout(&.{
|
||||||
.bind_group_layouts = &bind_group_layouts,
|
.bind_group_layouts = &bind_group_layouts,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -107,9 +107,9 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const queue = engine.device.getQueue();
|
const queue = core.device.getQueue();
|
||||||
|
|
||||||
const vertex_buffer = engine.device.createBuffer(&.{
|
const vertex_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .vertex = true },
|
.usage = .{ .vertex = true },
|
||||||
.size = @sizeOf(Vertex) * vertices.len,
|
.size = @sizeOf(Vertex) * vertices.len,
|
||||||
.mapped_at_creation = true,
|
.mapped_at_creation = true,
|
||||||
|
|
@ -120,13 +120,13 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
// uniformBindGroup offset must be 256-byte aligned
|
// uniformBindGroup offset must be 256-byte aligned
|
||||||
const uniform_offset = 256;
|
const uniform_offset = 256;
|
||||||
const uniform_buffer = engine.device.createBuffer(&.{
|
const uniform_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .uniform = true, .copy_dst = true },
|
.usage = .{ .uniform = true, .copy_dst = true },
|
||||||
.size = @sizeOf(UniformBufferObject) + uniform_offset,
|
.size = @sizeOf(UniformBufferObject) + uniform_offset,
|
||||||
.mapped_at_creation = false,
|
.mapped_at_creation = false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const bind_group1 = engine.device.createBindGroup(
|
const bind_group1 = core.device.createBindGroup(
|
||||||
&gpu.BindGroup.Descriptor{
|
&gpu.BindGroup.Descriptor{
|
||||||
.layout = bgl,
|
.layout = bgl,
|
||||||
.entries = &.{
|
.entries = &.{
|
||||||
|
|
@ -135,7 +135,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const bind_group2 = engine.device.createBindGroup(
|
const bind_group2 = core.device.createBindGroup(
|
||||||
&gpu.BindGroup.Descriptor{
|
&gpu.BindGroup.Descriptor{
|
||||||
.layout = bgl,
|
.layout = bgl,
|
||||||
.entries = &.{
|
.entries = &.{
|
||||||
|
|
@ -144,7 +144,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
app.pipeline = engine.device.createRenderPipeline(&pipeline_descriptor);
|
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||||
app.queue = queue;
|
app.queue = queue;
|
||||||
app.vertex_buffer = vertex_buffer;
|
app.vertex_buffer = vertex_buffer;
|
||||||
app.uniform_buffer = uniform_buffer;
|
app.uniform_buffer = uniform_buffer;
|
||||||
|
|
@ -157,25 +157,25 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
bgl.release();
|
bgl.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(app: *App, _: *mach.Engine) void {
|
pub fn deinit(app: *App, _: *mach.Core) void {
|
||||||
app.vertex_buffer.release();
|
app.vertex_buffer.release();
|
||||||
app.uniform_buffer.release();
|
app.uniform_buffer.release();
|
||||||
app.bind_group1.release();
|
app.bind_group1.release();
|
||||||
app.bind_group2.release();
|
app.bind_group2.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(app: *App, engine: *mach.Engine) !void {
|
pub fn update(app: *App, core: *mach.Core) !void {
|
||||||
while (engine.pollEvent()) |event| {
|
while (core.pollEvent()) |event| {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key_press => |ev| {
|
.key_press => |ev| {
|
||||||
if (ev.key == .space)
|
if (ev.key == .space)
|
||||||
engine.setShouldClose(true);
|
core.setShouldClose(true);
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
|
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||||
const color_attachment = gpu.RenderPassColorAttachment{
|
const color_attachment = gpu.RenderPassColorAttachment{
|
||||||
.view = back_buffer_view,
|
.view = back_buffer_view,
|
||||||
.resolve_target = null,
|
.resolve_target = null,
|
||||||
|
|
@ -184,7 +184,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
.store_op = .store,
|
.store_op = .store,
|
||||||
};
|
};
|
||||||
|
|
||||||
const encoder = engine.device.createCommandEncoder(null);
|
const encoder = core.device.createCommandEncoder(null);
|
||||||
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
||||||
.color_attachments = &.{color_attachment},
|
.color_attachments = &.{color_attachment},
|
||||||
.depth_stencil_attachment = null,
|
.depth_stencil_attachment = null,
|
||||||
|
|
@ -203,7 +203,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
);
|
);
|
||||||
const proj = zm.perspectiveFovRh(
|
const proj = zm.perspectiveFovRh(
|
||||||
(2.0 * std.math.pi / 5.0),
|
(2.0 * std.math.pi / 5.0),
|
||||||
@intToFloat(f32, engine.current_desc.width) / @intToFloat(f32, engine.current_desc.height),
|
@intToFloat(f32, core.current_desc.width) / @intToFloat(f32, core.current_desc.height),
|
||||||
1,
|
1,
|
||||||
100,
|
100,
|
||||||
);
|
);
|
||||||
|
|
@ -239,6 +239,6 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
app.queue.submit(&.{command});
|
app.queue.submit(&.{command});
|
||||||
command.release();
|
command.release();
|
||||||
engine.swap_chain.?.present();
|
core.swap_chain.?.present();
|
||||||
back_buffer_view.release();
|
back_buffer_view.release();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,13 +36,13 @@ fragment_shader_file: std.fs.File,
|
||||||
fragment_shader_code: [:0]const u8,
|
fragment_shader_code: [:0]const u8,
|
||||||
last_mtime: i128,
|
last_mtime: i128,
|
||||||
|
|
||||||
pub fn init(app: *App, engine: *mach.Engine) !void {
|
pub fn init(app: *App, core: *mach.Core) !void {
|
||||||
timer = try std.time.Timer.start();
|
timer = try std.time.Timer.start();
|
||||||
|
|
||||||
// On linux if we don't set a minimum size, you can squish the window to 0 pixels of width and height,
|
// On linux if we don't set a minimum size, you can squish the window to 0 pixels of width and height,
|
||||||
// this makes some strange effects when that happens, so it's better to leave a minimum size to avoid that,
|
// this makes some strange effects when that happens, so it's better to leave a minimum size to avoid that,
|
||||||
// this doesn't prevent you from minimizing the window.
|
// this doesn't prevent you from minimizing the window.
|
||||||
try engine.setOptions(.{
|
try core.setOptions(.{
|
||||||
.size_min = .{ .width = 20, .height = 20 },
|
.size_min = .{ .width = 20, .height = 20 },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -60,11 +60,11 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
std.debug.print("Something went wrong when attempting to open file: {}\n", .{e});
|
std.debug.print("Something went wrong when attempting to open file: {}\n", .{e});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var code = try fragment_file.readToEndAllocOptions(engine.allocator, std.math.maxInt(u16), null, 1, 0);
|
var code = try fragment_file.readToEndAllocOptions(core.allocator, std.math.maxInt(u16), null, 1, 0);
|
||||||
|
|
||||||
const queue = engine.device.getQueue();
|
const queue = core.device.getQueue();
|
||||||
|
|
||||||
const vertex_buffer = engine.device.createBuffer(&.{
|
const vertex_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .vertex = true },
|
.usage = .{ .vertex = true },
|
||||||
.size = @sizeOf(Vertex) * vertices.len,
|
.size = @sizeOf(Vertex) * vertices.len,
|
||||||
.mapped_at_creation = true,
|
.mapped_at_creation = true,
|
||||||
|
|
@ -73,7 +73,7 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
std.mem.copy(Vertex, vertex_mapped, vertices[0..]);
|
std.mem.copy(Vertex, vertex_mapped, vertices[0..]);
|
||||||
vertex_buffer.unmap();
|
vertex_buffer.unmap();
|
||||||
|
|
||||||
const index_buffer = engine.device.createBuffer(&.{
|
const index_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .index = true },
|
.usage = .{ .index = true },
|
||||||
.size = @sizeOf(u16) * indices.len,
|
.size = @sizeOf(u16) * indices.len,
|
||||||
.mapped_at_creation = true,
|
.mapped_at_creation = true,
|
||||||
|
|
@ -85,14 +85,14 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
// We need a bgl to bind the UniformBufferObject, but it is also needed for creating
|
// 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
|
// the RenderPipeline, so we pass it to recreatePipeline as a pointer
|
||||||
var bgl: gpu.BindGroupLayout = undefined;
|
var bgl: gpu.BindGroupLayout = undefined;
|
||||||
const pipeline = recreatePipeline(engine, code, &bgl);
|
const pipeline = recreatePipeline(core, code, &bgl);
|
||||||
|
|
||||||
const uniform_buffer = engine.device.createBuffer(&.{
|
const uniform_buffer = core.device.createBuffer(&.{
|
||||||
.usage = .{ .copy_dst = true, .uniform = true },
|
.usage = .{ .copy_dst = true, .uniform = true },
|
||||||
.size = @sizeOf(UniformBufferObject),
|
.size = @sizeOf(UniformBufferObject),
|
||||||
.mapped_at_creation = false,
|
.mapped_at_creation = false,
|
||||||
});
|
});
|
||||||
const bind_group = engine.device.createBindGroup(
|
const bind_group = core.device.createBindGroup(
|
||||||
&gpu.BindGroup.Descriptor{
|
&gpu.BindGroup.Descriptor{
|
||||||
.layout = bgl,
|
.layout = bgl,
|
||||||
.entries = &.{
|
.entries = &.{
|
||||||
|
|
@ -115,9 +115,9 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
|
||||||
bgl.release();
|
bgl.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(app: *App, engine: *mach.Engine) void {
|
pub fn deinit(app: *App, core: *mach.Core) void {
|
||||||
app.fragment_shader_file.close();
|
app.fragment_shader_file.close();
|
||||||
engine.allocator.free(app.fragment_shader_code);
|
core.allocator.free(app.fragment_shader_code);
|
||||||
|
|
||||||
app.vertex_buffer.release();
|
app.vertex_buffer.release();
|
||||||
app.index_buffer.release();
|
app.index_buffer.release();
|
||||||
|
|
@ -125,12 +125,12 @@ pub fn deinit(app: *App, engine: *mach.Engine) void {
|
||||||
app.bind_group.release();
|
app.bind_group.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(app: *App, engine: *mach.Engine) !void {
|
pub fn update(app: *App, core: *mach.Core) !void {
|
||||||
while (engine.pollEvent()) |event| {
|
while (core.pollEvent()) |event| {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key_press => |ev| {
|
.key_press => |ev| {
|
||||||
if (ev.key == .space)
|
if (ev.key == .space)
|
||||||
engine.setShouldClose(true);
|
core.setShouldClose(true);
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
@ -141,17 +141,17 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
std.log.info("The fragment shader has been changed", .{});
|
std.log.info("The fragment shader has been changed", .{});
|
||||||
app.last_mtime = stat.mtime;
|
app.last_mtime = stat.mtime;
|
||||||
app.fragment_shader_file.seekTo(0) catch unreachable;
|
app.fragment_shader_file.seekTo(0) catch unreachable;
|
||||||
app.fragment_shader_code = app.fragment_shader_file.readToEndAllocOptions(engine.allocator, std.math.maxInt(u32), null, 1, 0) catch |err| {
|
app.fragment_shader_code = app.fragment_shader_file.readToEndAllocOptions(core.allocator, std.math.maxInt(u32), null, 1, 0) catch |err| {
|
||||||
std.log.err("Err: {}", .{err});
|
std.log.err("Err: {}", .{err});
|
||||||
return engine.setShouldClose(true);
|
return core.setShouldClose(true);
|
||||||
};
|
};
|
||||||
app.pipeline = recreatePipeline(engine, app.fragment_shader_code, null);
|
app.pipeline = recreatePipeline(core, app.fragment_shader_code, null);
|
||||||
}
|
}
|
||||||
} else |err| {
|
} else |err| {
|
||||||
std.log.err("Something went wrong when attempting to stat file: {}\n", .{err});
|
std.log.err("Something went wrong when attempting to stat file: {}\n", .{err});
|
||||||
}
|
}
|
||||||
|
|
||||||
const back_buffer_view = engine.swap_chain.?.getCurrentTextureView();
|
const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
|
||||||
const color_attachment = gpu.RenderPassColorAttachment{
|
const color_attachment = gpu.RenderPassColorAttachment{
|
||||||
.view = back_buffer_view,
|
.view = back_buffer_view,
|
||||||
.resolve_target = null,
|
.resolve_target = null,
|
||||||
|
|
@ -160,7 +160,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
.store_op = .store,
|
.store_op = .store,
|
||||||
};
|
};
|
||||||
|
|
||||||
const encoder = engine.device.createCommandEncoder(null);
|
const encoder = core.device.createCommandEncoder(null);
|
||||||
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
||||||
.color_attachments = &.{color_attachment},
|
.color_attachments = &.{color_attachment},
|
||||||
.depth_stencil_attachment = null,
|
.depth_stencil_attachment = null,
|
||||||
|
|
@ -168,7 +168,7 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
const time = @intToFloat(f32, timer.read()) / @as(f32, std.time.ns_per_s);
|
const time = @intToFloat(f32, timer.read()) / @as(f32, std.time.ns_per_s);
|
||||||
const ubo = UniformBufferObject{
|
const ubo = UniformBufferObject{
|
||||||
.resolution = .{ @intToFloat(f32, engine.current_desc.width), @intToFloat(f32, engine.current_desc.height) },
|
.resolution = .{ @intToFloat(f32, core.current_desc.width), @intToFloat(f32, core.current_desc.height) },
|
||||||
.time = time,
|
.time = time,
|
||||||
};
|
};
|
||||||
encoder.writeBuffer(app.uniform_buffer, 0, UniformBufferObject, &.{ubo});
|
encoder.writeBuffer(app.uniform_buffer, 0, UniformBufferObject, &.{ubo});
|
||||||
|
|
@ -187,12 +187,12 @@ pub fn update(app: *App, engine: *mach.Engine) !void {
|
||||||
|
|
||||||
app.queue.submit(&.{command});
|
app.queue.submit(&.{command});
|
||||||
command.release();
|
command.release();
|
||||||
engine.swap_chain.?.present();
|
core.swap_chain.?.present();
|
||||||
back_buffer_view.release();
|
back_buffer_view.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn recreatePipeline(engine: *mach.Engine, fragment_shader_code: [:0]const u8, bgl: ?*gpu.BindGroupLayout) gpu.RenderPipeline {
|
fn recreatePipeline(core: *mach.Core, fragment_shader_code: [:0]const u8, bgl: ?*gpu.BindGroupLayout) gpu.RenderPipeline {
|
||||||
const vs_module = engine.device.createShaderModule(&.{
|
const vs_module = core.device.createShaderModule(&.{
|
||||||
.label = "my vertex shader",
|
.label = "my vertex shader",
|
||||||
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -210,14 +210,14 @@ fn recreatePipeline(engine: *mach.Engine, fragment_shader_code: [:0]const u8, bg
|
||||||
|
|
||||||
// Check wether the fragment shader code compiled successfully, if not
|
// Check wether the fragment shader code compiled successfully, if not
|
||||||
// print the validation layer error and show a black screen
|
// print the validation layer error and show a black screen
|
||||||
engine.device.pushErrorScope(.validation);
|
core.device.pushErrorScope(.validation);
|
||||||
var fs_module = engine.device.createShaderModule(&gpu.ShaderModule.Descriptor{
|
var fs_module = core.device.createShaderModule(&gpu.ShaderModule.Descriptor{
|
||||||
.label = "my fragment shader",
|
.label = "my fragment shader",
|
||||||
.code = .{ .wgsl = fragment_shader_code },
|
.code = .{ .wgsl = fragment_shader_code },
|
||||||
});
|
});
|
||||||
var error_occurred: bool = false;
|
var error_occurred: bool = false;
|
||||||
// popErrorScope() returns always true, (unless maybe it fails to capture the error scope?)
|
// popErrorScope() returns always true, (unless maybe it fails to capture the error scope?)
|
||||||
_ = engine.device.popErrorScope(&gpu.ErrorCallback.init(*bool, &error_occurred, struct {
|
_ = core.device.popErrorScope(&gpu.ErrorCallback.init(*bool, &error_occurred, struct {
|
||||||
fn callback(ctx: *bool, typ: gpu.ErrorType, message: [*:0]const u8) void {
|
fn callback(ctx: *bool, typ: gpu.ErrorType, message: [*:0]const u8) void {
|
||||||
if (typ != .noError) {
|
if (typ != .noError) {
|
||||||
std.debug.print("🔴🔴🔴🔴:\n{s}\n", .{message});
|
std.debug.print("🔴🔴🔴🔴:\n{s}\n", .{message});
|
||||||
|
|
@ -226,7 +226,7 @@ fn recreatePipeline(engine: *mach.Engine, fragment_shader_code: [:0]const u8, bg
|
||||||
}
|
}
|
||||||
}.callback));
|
}.callback));
|
||||||
if (error_occurred) {
|
if (error_occurred) {
|
||||||
fs_module = engine.device.createShaderModule(&gpu.ShaderModule.Descriptor{
|
fs_module = core.device.createShaderModule(&gpu.ShaderModule.Descriptor{
|
||||||
.label = "my fragment shader",
|
.label = "my fragment shader",
|
||||||
.code = .{ .wgsl = @embedFile("black_screen_frag.wgsl") },
|
.code = .{ .wgsl = @embedFile("black_screen_frag.wgsl") },
|
||||||
});
|
});
|
||||||
|
|
@ -246,7 +246,7 @@ fn recreatePipeline(engine: *mach.Engine, fragment_shader_code: [:0]const u8, bg
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const color_target = gpu.ColorTargetState{
|
const color_target = gpu.ColorTargetState{
|
||||||
.format = engine.swap_chain_format,
|
.format = core.swap_chain_format,
|
||||||
.blend = &blend,
|
.blend = &blend,
|
||||||
.write_mask = gpu.ColorWriteMask.all,
|
.write_mask = gpu.ColorWriteMask.all,
|
||||||
};
|
};
|
||||||
|
|
@ -259,7 +259,7 @@ fn recreatePipeline(engine: *mach.Engine, fragment_shader_code: [:0]const u8, bg
|
||||||
|
|
||||||
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .fragment = true }, .uniform, true, 0);
|
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
|
// bgl is needed outside, for the creation of the uniform_buffer in main
|
||||||
const bgl_tmp = engine.device.createBindGroupLayout(
|
const bgl_tmp = core.device.createBindGroupLayout(
|
||||||
&gpu.BindGroupLayout.Descriptor{
|
&gpu.BindGroupLayout.Descriptor{
|
||||||
.entries = &.{bgle},
|
.entries = &.{bgle},
|
||||||
},
|
},
|
||||||
|
|
@ -274,7 +274,7 @@ fn recreatePipeline(engine: *mach.Engine, fragment_shader_code: [:0]const u8, bg
|
||||||
}
|
}
|
||||||
|
|
||||||
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl_tmp};
|
const bind_group_layouts = [_]gpu.BindGroupLayout{bgl_tmp};
|
||||||
const pipeline_layout = engine.device.createPipelineLayout(&.{
|
const pipeline_layout = core.device.createPipelineLayout(&.{
|
||||||
.bind_group_layouts = &bind_group_layouts,
|
.bind_group_layouts = &bind_group_layouts,
|
||||||
});
|
});
|
||||||
defer pipeline_layout.release();
|
defer pipeline_layout.release();
|
||||||
|
|
@ -303,10 +303,10 @@ fn recreatePipeline(engine: *mach.Engine, fragment_shader_code: [:0]const u8, bg
|
||||||
|
|
||||||
// Create the render pipeline. Even if the shader compilation succeeded, this could fail if the
|
// Create the render pipeline. Even if the shader compilation succeeded, this could fail if the
|
||||||
// shader is missing a `main` entrypoint.
|
// shader is missing a `main` entrypoint.
|
||||||
engine.device.pushErrorScope(.validation);
|
core.device.pushErrorScope(.validation);
|
||||||
const pipeline = engine.device.createRenderPipeline(&pipeline_descriptor);
|
const pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||||
// popErrorScope() returns always true, (unless maybe it fails to capture the error scope?)
|
// popErrorScope() returns always true, (unless maybe it fails to capture the error scope?)
|
||||||
_ = engine.device.popErrorScope(&gpu.ErrorCallback.init(*bool, &error_occurred, struct {
|
_ = core.device.popErrorScope(&gpu.ErrorCallback.init(*bool, &error_occurred, struct {
|
||||||
fn callback(ctx: *bool, typ: gpu.ErrorType, message: [*:0]const u8) void {
|
fn callback(ctx: *bool, typ: gpu.ErrorType, message: [*:0]const u8) void {
|
||||||
if (typ != .noError) {
|
if (typ != .noError) {
|
||||||
std.debug.print("🔴🔴🔴🔴:\n{s}\n", .{message});
|
std.debug.print("🔴🔴🔴🔴:\n{s}\n", .{message});
|
||||||
|
|
@ -316,7 +316,7 @@ fn recreatePipeline(engine: *mach.Engine, fragment_shader_code: [:0]const u8, bg
|
||||||
}.callback));
|
}.callback));
|
||||||
if (error_occurred) {
|
if (error_occurred) {
|
||||||
// Retry with black_screen_frag which we know will work.
|
// Retry with black_screen_frag which we know will work.
|
||||||
return recreatePipeline(engine, @embedFile("black_screen_frag.wgsl"), bgl);
|
return recreatePipeline(core, @embedFile("black_screen_frag.wgsl"), bgl);
|
||||||
}
|
}
|
||||||
return pipeline;
|
return pipeline;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue