examples: texture-light: zig fmt

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-04-21 04:23:32 -07:00 committed by Stephen Gutekanst
parent 9ec8553cf8
commit 417ec654df

View file

@ -96,9 +96,7 @@ fn frame(app: *App, params: *FrameParams) !void {
if (params.keys & FrameParams.down != 0)
params.camera.eye -= fwd * speed;
if (params.keys & FrameParams.right != 0) params.camera.eye += right * speed
else if (params.keys & FrameParams.left != 0) params.camera.eye -= right * speed
else params.camera.eye += right * (speed * @Vector(4, f32){0.5, 0.5, 0.5, 0.5});
if (params.keys & FrameParams.right != 0) params.camera.eye += right * speed else if (params.keys & FrameParams.left != 0) params.camera.eye -= right * speed else params.camera.eye += right * (speed * @Vector(4, f32){ 0.5, 0.5, 0.5, 0.5 });
params.camera.update(params.queue);
@ -114,15 +112,13 @@ fn frame(app: *App, params: *FrameParams) !void {
const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view,
.clear_value = gpu.Color{.r=0.0, .g=0.0, .b=0.4, .a=1.0},
.clear_value = gpu.Color{ .r = 0.0, .g = 0.0, .b = 0.4, .a = 1.0 },
.load_op = .clear,
.store_op = .store,
};
const render_pass_descriptor = gpu.RenderPassEncoder.Descriptor{
.color_attachments = &.{
color_attachment
},
.color_attachments = &.{color_attachment},
.depth_stencil_attachment = &.{
.view = params.depth.view,
.depth_load_op = .clear,
@ -210,7 +206,7 @@ const Camera = struct {
};
const buffer = .{
.buffer = initBuffer(device, .{.uniform=true}, &@bitCast([20]f32, uniform)),
.buffer = initBuffer(device, .{ .uniform = true }, &@bitCast([20]f32, uniform)),
.size = @sizeOf(@TypeOf(uniform)),
};
@ -277,7 +273,7 @@ const Cube = struct {
const texture = Brick.texture(device);
// instance buffer
var ibuf: [IPR*IPR*16]f32 = undefined;
var ibuf: [IPR * IPR * 16]f32 = undefined;
var z: usize = 0;
while (z < IPR) : (z += 1) {
@ -300,13 +296,13 @@ const Cube = struct {
}
}
const instance = Buffer {
.buffer = initBuffer(device, .{.vertex=true}, &ibuf),
const instance = Buffer{
.buffer = initBuffer(device, .{ .vertex = true }, &ibuf),
.len = IPR * IPR,
.size = @sizeOf(@TypeOf(ibuf)),
};
return Self {
return Self{
.mesh = mesh(device),
.texture = texture,
.instance = instance,
@ -426,8 +422,8 @@ const Cube = struct {
1, 0, 1, 0, -1, 0, v, 0,
});
return Buffer {
.buffer = initBuffer(device, .{.vertex=true}, &buf),
return Buffer{
.buffer = initBuffer(device, .{ .vertex = true }, &buf),
.size = @sizeOf(@TypeOf(buf)),
};
}
@ -510,8 +506,8 @@ const Brick = struct {
return Texture.fromData(device, W, H, slice);
}
fn data() [W*H*4]u8 {
comptime var out: [W*H*4]u8 = undefined;
fn data() [W * H * 4]u8 {
comptime var out: [W * H * 4]u8 = undefined;
// fill all the texture with brick color
comptime var i = 0;
@ -579,7 +575,7 @@ const Texture = struct {
}
fn fromData(device: gpu.Device, width: u32, height: u32, data: anytype) Self {
const extent = gpu.Extent3D {
const extent = gpu.Extent3D{
.width = width,
.height = height,
};
@ -617,9 +613,11 @@ const Texture = struct {
});
device.getQueue().writeTexture(
&gpu.ImageCopyTexture{ .texture = texture, },
&gpu.ImageCopyTexture{
.texture = texture,
},
data,
&gpu.Texture.DataLayout {
&gpu.Texture.DataLayout{
.bytes_per_row = 4 * width,
.rows_per_image = height,
},
@ -635,7 +633,7 @@ const Texture = struct {
},
});
return Self {
return Self{
.view = view,
.texture = texture,
.sampler = sampler,
@ -644,7 +642,7 @@ const Texture = struct {
}
fn depth(device: gpu.Device, width: u32, height: u32) Self {
const extent = gpu.Extent3D {
const extent = gpu.Extent3D{
.width = width,
.height = height,
};
@ -684,7 +682,7 @@ const Texture = struct {
.max_anisotropy = 1,
});
return Self {
return Self{
.texture = texture,
.view = view,
.sampler = sampler,
@ -725,18 +723,18 @@ const Light = struct {
};
const buffer = .{
.buffer = initBuffer(device, .{.uniform = true}, &@bitCast([8]f32, uniform)),
.buffer = initBuffer(device, .{ .uniform = true }, &@bitCast([8]f32, uniform)),
.size = @sizeOf(@TypeOf(uniform)),
};
const bind_group = device.createBindGroup(&gpu.BindGroup.Descriptor{
.layout = Self.bindGroupLayout(device),
.entries = &[_]gpu.BindGroup.Entry {
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, buffer.buffer, 0, buffer.size),
},
});
return Self {
return Self{
.buffer = buffer,
.uniform = uniform,
.bind_group = bind_group,
@ -746,7 +744,7 @@ const Light = struct {
fn update(self: *Self, queue: gpu.Queue, delta: f32) void {
const old = self.uniform;
const new = Light.Uniform {
const new = Light.Uniform{
.position = zm.qmul(zm.quatFromAxisAngle(vec3u(0, 1, 0), delta), old.position),
.color = old.color,
};
@ -890,20 +888,35 @@ fn keyCallback(window: glfw.Window, key: glfw.Key, scancode: i32, action: glfw.A
if (action == .press) {
switch (key) {
.q, .escape, .space => window.setShouldClose(true),
.w, .up => { global_params.keys |= FrameParams.up; },
.s, .down => { global_params.keys |= FrameParams.down; },
.a, .left => { global_params.keys |= FrameParams.left; },
.d, .right => { global_params.keys |= FrameParams.right; },
.w, .up => {
global_params.keys |= FrameParams.up;
},
.s, .down => {
global_params.keys |= FrameParams.down;
},
.a, .left => {
global_params.keys |= FrameParams.left;
},
.d, .right => {
global_params.keys |= FrameParams.right;
},
else => {},
}
} else if (action == .release) {
switch (key) {
.w, .up => { global_params.keys &= ~FrameParams.up; },
.s, .down => { global_params.keys &= ~FrameParams.down; },
.a, .left => { global_params.keys &= ~FrameParams.left; },
.d, .right => { global_params.keys &= ~FrameParams.right; },
.w, .up => {
global_params.keys &= ~FrameParams.up;
},
.s, .down => {
global_params.keys &= ~FrameParams.down;
},
.a, .left => {
global_params.keys &= ~FrameParams.left;
},
.d, .right => {
global_params.keys &= ~FrameParams.right;
},
else => {},
}
}
}