examples: remove global state workaround in advanced-gen-texture-light

This commit is contained in:
iddev5 2022-05-09 13:00:38 +05:30 committed by Stephen Gutekanst
parent 1f95bd48df
commit 9106a9839d

View file

@ -18,19 +18,15 @@ const Quat = zm.Quat;
const App = @This(); const App = @This();
ctx: FrameParams = .{}, queue: gpu.Queue,
cube: Cube,
var global_params: *FrameParams = undefined; camera: Camera,
light: Light,
const FrameParams = struct { depth: Texture,
queue: gpu.Queue, depth_size: glfw.Window.Size,
cube: Cube, keys: u8 = 0,
camera: Camera,
light: Light,
depth: Texture,
depth_size: glfw.Window.Size,
keys: u8 = 0,
const Dir = struct {
const up: u8 = 0b0001; const up: u8 = 0b0001;
const down: u8 = 0b0010; const down: u8 = 0b0010;
const left: u8 = 0b0100; const left: u8 = 0b0100;
@ -60,16 +56,12 @@ pub fn init(app: *App, engine: *mach.Engine) !void {
const size = try engine.core.internal.window.getFramebufferSize(); const size = try engine.core.internal.window.getFramebufferSize();
const aspect_ratio = @intToFloat(f32, size.width) / @intToFloat(f32, size.height); const aspect_ratio = @intToFloat(f32, size.width) / @intToFloat(f32, size.height);
app.ctx = FrameParams{ app.queue = engine.gpu_driver.device.getQueue();
.queue = engine.gpu_driver.device.getQueue(), app.cube = Cube.init(engine);
.cube = Cube.init(engine), app.light = Light.init(engine);
.light = Light.init(engine), app.depth = Texture.depth(engine.gpu_driver.device, size.width, size.height);
.depth = Texture.depth(engine.gpu_driver.device, size.width, size.height), app.depth_size = size;
.depth_size = size, app.camera = Camera.init(engine.gpu_driver.device, eye, target, vec3(0.0, 1.0, 0.0), aspect_ratio, 45.0, 0.1, 100.0);
.camera = Camera.init(engine.gpu_driver.device, eye, target, vec3(0.0, 1.0, 0.0), aspect_ratio, 45.0, 0.1, 100.0),
};
global_params = &app.ctx; // TODO ugly hack to use ctx from glfw callbacks
} }
pub fn deinit(_: *App, _: *mach.Engine) void {} pub fn deinit(_: *App, _: *mach.Engine) void {}
@ -77,29 +69,29 @@ pub fn deinit(_: *App, _: *mach.Engine) void {}
pub fn update(app: *App, engine: *mach.Engine) !bool { pub fn update(app: *App, engine: *mach.Engine) !bool {
// If window is resized, recreate depth buffer otherwise we cannot use it. // If window is resized, recreate depth buffer otherwise we cannot use it.
const size = engine.core.internal.window.getFramebufferSize() catch unreachable; // TODO: return type inference can't handle this const size = engine.core.internal.window.getFramebufferSize() catch unreachable; // TODO: return type inference can't handle this
if (size.width != app.ctx.depth_size.width or size.height != app.ctx.depth_size.height) { if (size.width != app.depth_size.width or size.height != app.depth_size.height) {
app.ctx.depth = Texture.depth(engine.gpu_driver.device, size.width, size.height); app.depth = Texture.depth(engine.gpu_driver.device, size.width, size.height);
app.ctx.depth_size = size; app.depth_size = size;
} }
// move camera // move camera
const speed = zm.f32x4s(@floatCast(f32, engine.delta_time * 5)); const speed = zm.f32x4s(@floatCast(f32, engine.delta_time * 5));
const fwd = zm.normalize3(app.ctx.camera.target - app.ctx.camera.eye); const fwd = zm.normalize3(app.camera.target - app.camera.eye);
const right = zm.normalize3(zm.cross3(fwd, app.ctx.camera.up)); const right = zm.normalize3(zm.cross3(fwd, app.camera.up));
if (app.ctx.keys & FrameParams.up != 0) if (app.keys & Dir.up != 0)
app.ctx.camera.eye += fwd * speed; app.camera.eye += fwd * speed;
if (app.ctx.keys & FrameParams.down != 0) if (app.keys & Dir.down != 0)
app.ctx.camera.eye -= fwd * speed; app.camera.eye -= fwd * speed;
if (app.ctx.keys & FrameParams.right != 0) app.ctx.camera.eye += right * speed else if (app.ctx.keys & FrameParams.left != 0) app.ctx.camera.eye -= right * speed else app.ctx.camera.eye += right * (speed * @Vector(4, f32){ 0.5, 0.5, 0.5, 0.5 }); if (app.keys & Dir.right != 0) app.camera.eye += right * speed else if (app.keys & Dir.left != 0) app.camera.eye -= right * speed else app.camera.eye += right * (speed * @Vector(4, f32){ 0.5, 0.5, 0.5, 0.5 });
app.ctx.camera.update(app.ctx.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, engine.delta_time * 2.5);
app.ctx.light.update(app.ctx.queue, light_speed); app.light.update(app.queue, light_speed);
const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView(); const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView();
defer back_buffer_view.release(); defer back_buffer_view.release();
@ -117,7 +109,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
const render_pass_descriptor = gpu.RenderPassEncoder.Descriptor{ const render_pass_descriptor = gpu.RenderPassEncoder.Descriptor{
.color_attachments = &.{color_attachment}, .color_attachments = &.{color_attachment},
.depth_stencil_attachment = &.{ .depth_stencil_attachment = &.{
.view = app.ctx.depth.view, .view = app.depth.view,
.depth_load_op = .clear, .depth_load_op = .clear,
.depth_store_op = .store, .depth_store_op = .store,
.stencil_load_op = .none, .stencil_load_op = .none,
@ -130,24 +122,24 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
defer pass.release(); defer pass.release();
// brick cubes // brick cubes
pass.setPipeline(app.ctx.cube.pipeline); pass.setPipeline(app.cube.pipeline);
pass.setBindGroup(0, app.ctx.camera.bind_group, &.{}); pass.setBindGroup(0, app.camera.bind_group, &.{});
pass.setBindGroup(1, app.ctx.cube.texture.bind_group, &.{}); pass.setBindGroup(1, app.cube.texture.bind_group, &.{});
pass.setBindGroup(2, app.ctx.light.bind_group, &.{}); pass.setBindGroup(2, app.light.bind_group, &.{});
pass.setVertexBuffer(0, app.ctx.cube.mesh.buffer, 0, app.ctx.cube.mesh.size); pass.setVertexBuffer(0, app.cube.mesh.buffer, 0, app.cube.mesh.size);
pass.setVertexBuffer(1, app.ctx.cube.instance.buffer, 0, app.ctx.cube.instance.size); pass.setVertexBuffer(1, app.cube.instance.buffer, 0, app.cube.instance.size);
pass.draw(4, app.ctx.cube.instance.len, 0, 0); pass.draw(4, app.cube.instance.len, 0, 0);
pass.draw(4, app.ctx.cube.instance.len, 4, 0); pass.draw(4, app.cube.instance.len, 4, 0);
pass.draw(4, app.ctx.cube.instance.len, 8, 0); pass.draw(4, app.cube.instance.len, 8, 0);
pass.draw(4, app.ctx.cube.instance.len, 12, 0); pass.draw(4, app.cube.instance.len, 12, 0);
pass.draw(4, app.ctx.cube.instance.len, 16, 0); pass.draw(4, app.cube.instance.len, 16, 0);
pass.draw(4, app.ctx.cube.instance.len, 20, 0); pass.draw(4, app.cube.instance.len, 20, 0);
// light source // light source
pass.setPipeline(app.ctx.light.pipeline); pass.setPipeline(app.light.pipeline);
pass.setBindGroup(0, app.ctx.camera.bind_group, &.{}); pass.setBindGroup(0, app.camera.bind_group, &.{});
pass.setBindGroup(1, app.ctx.light.bind_group, &.{}); pass.setBindGroup(1, app.light.bind_group, &.{});
pass.setVertexBuffer(0, app.ctx.cube.mesh.buffer, 0, app.ctx.cube.mesh.size); pass.setVertexBuffer(0, app.cube.mesh.buffer, 0, app.cube.mesh.size);
pass.draw(4, 1, 0, 0); pass.draw(4, 1, 0, 0);
pass.draw(4, 1, 4, 0); pass.draw(4, 1, 4, 0);
pass.draw(4, 1, 8, 0); pass.draw(4, 1, 8, 0);
@ -160,7 +152,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
var command = encoder.finish(null); var command = encoder.finish(null);
defer command.release(); defer command.release();
app.ctx.queue.submit(&.{command}); app.queue.submit(&.{command});
engine.gpu_driver.swap_chain.?.present(); engine.gpu_driver.swap_chain.?.present();
return true; return true;
@ -879,37 +871,37 @@ const Instance = struct {
} }
}; };
fn keyCallback(_: *App, engine: *mach.Engine, key: mach.Key, action: mach.Action) void { fn keyCallback(app: *App, engine: *mach.Engine, key: mach.Key, action: mach.Action) void {
if (action == .press) { if (action == .press) {
switch (key) { switch (key) {
.q, .escape, .space => engine.core.internal.window.setShouldClose(true), .q, .escape, .space => engine.core.internal.window.setShouldClose(true),
.w, .up => { .w, .up => {
global_params.keys |= FrameParams.up; app.keys |= Dir.up;
}, },
.s, .down => { .s, .down => {
global_params.keys |= FrameParams.down; app.keys |= Dir.down;
}, },
.a, .left => { .a, .left => {
global_params.keys |= FrameParams.left; app.keys |= Dir.left;
}, },
.d, .right => { .d, .right => {
global_params.keys |= FrameParams.right; app.keys |= Dir.right;
}, },
else => {}, else => {},
} }
} else if (action == .release) { } else if (action == .release) {
switch (key) { switch (key) {
.w, .up => { .w, .up => {
global_params.keys &= ~FrameParams.up; app.keys &= ~Dir.up;
}, },
.s, .down => { .s, .down => {
global_params.keys &= ~FrameParams.down; app.keys &= ~Dir.down;
}, },
.a, .left => { .a, .left => {
global_params.keys &= ~FrameParams.left; app.keys &= ~Dir.left;
}, },
.d, .right => { .d, .right => {
global_params.keys &= ~FrameParams.right; app.keys &= ~Dir.right;
}, },
else => {}, else => {},
} }