examples: zig fmt

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-04-20 23:01:30 -07:00
parent 3b86aa08cc
commit 8137f1a914

View file

@ -1,6 +1,5 @@
/// A port of Austin Eng's "computeBoids" webgpu sample. /// A port of Austin Eng's "computeBoids" webgpu sample.
/// https://github.com/austinEng/webgpu-samples/blob/main/src/sample/computeBoids/main.ts /// https://github.com/austinEng/webgpu-samples/blob/main/src/sample/computeBoids/main.ts
const std = @import("std"); const std = @import("std");
const mach = @import("mach"); const mach = @import("mach");
const gpu = @import("gpu"); const gpu = @import("gpu");
@ -18,13 +17,13 @@ const App = mach.App(*FrameParams, .{});
const num_particle = 1500; const num_particle = 1500;
var sim_params = [_]f32 { var sim_params = [_]f32{
0.04, // .delta_T 0.04, // .delta_T
0.1, // .rule_1_distance 0.1, // .rule_1_distance
0.025, // .rule_2_distance 0.025, // .rule_2_distance
0.025, // .rule_3_distance 0.025, // .rule_3_distance
0.02, // .rule_1_scale 0.02, // .rule_1_scale
0.05, // .rule_2_scale 0.05, // .rule_2_scale
0.005, // .rule_3_scale 0.005, // .rule_3_scale
}; };
@ -53,7 +52,7 @@ pub fn main() !void {
.format = .float32x2, .format = .float32x2,
}, },
.{ .{
// instance velocity // instance velocity
.shader_location = 1, .shader_location = 1,
.offset = 2 * 4, .offset = 2 * 4,
.format = .float32x2, .format = .float32x2,
@ -76,60 +75,54 @@ pub fn main() !void {
.buffers = &[_]gpu.VertexBufferLayout{ .buffers = &[_]gpu.VertexBufferLayout{
.{ .{
// instanced particles buffer // instanced particles buffer
.array_stride = 4*4, .array_stride = 4 * 4,
.step_mode = .instance, .step_mode = .instance,
.attribute_count = instanced_particles_attributes.len, .attribute_count = instanced_particles_attributes.len,
.attributes = &instanced_particles_attributes, .attributes = &instanced_particles_attributes,
}, },
.{ .{
// vertex buffer // vertex buffer
.array_stride = 2*4, .array_stride = 2 * 4,
.step_mode = .vertex, .step_mode = .vertex,
.attribute_count = vertex_buffer_attributes.len, .attribute_count = vertex_buffer_attributes.len,
.attributes = &vertex_buffer_attributes, .attributes = &vertex_buffer_attributes,
}, },
}, },
}, },
.fragment = &gpu.FragmentState{ .fragment = &gpu.FragmentState{ .module = sprite_shader_module, .entry_point = "frag_main", .targets = &[_]gpu.ColorTargetState{
.module = sprite_shader_module, .{
.entry_point = "frag_main", .format = app.swap_chain_format,
.targets = &[_]gpu.ColorTargetState{ },
.{ } },
.format = app.swap_chain_format,
},
}
},
}); });
const compute_pipeline = app.device.createComputePipeline(&gpu.ComputePipeline.Descriptor{ const compute_pipeline = app.device.createComputePipeline(&gpu.ComputePipeline.Descriptor{ .compute = gpu.ProgrammableStageDescriptor{
.compute = gpu.ProgrammableStageDescriptor{ .module = update_sprite_shader_module,
.module = update_sprite_shader_module, .entry_point = "main",
.entry_point = "main", } });
}
});
const vert_buffer_data = [_]f32{ const vert_buffer_data = [_]f32{
-0.01, -0.02, 0.01, -0.01, -0.02, 0.01,
-0.02, 0.0, 0.02, -0.02, 0.0, 0.02,
}; };
const sprite_vertex_buffer = app.device.createBuffer(&gpu.Buffer.Descriptor{ const sprite_vertex_buffer = app.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),
}); });
app.device.getQueue().writeBuffer(sprite_vertex_buffer, 0, f32, &vert_buffer_data); app.device.getQueue().writeBuffer(sprite_vertex_buffer, 0, f32, &vert_buffer_data);
const sim_param_buffer = app.device.createBuffer(&gpu.Buffer.Descriptor{ const sim_param_buffer = app.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),
}); });
app.device.getQueue().writeBuffer(sim_param_buffer, 0, f32, &sim_params); app.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);
const random = rng.random(); const random = rng.random();
var i:usize = 0; var i: usize = 0;
while(i < num_particle): (i += 1) { while (i < num_particle) : (i += 1) {
initial_particle_data[4 * i + 0] = 2 * (random.float(f32) - 0.5); initial_particle_data[4 * i + 0] = 2 * (random.float(f32) - 0.5);
initial_particle_data[4 * i + 1] = 2 * (random.float(f32) - 0.5); initial_particle_data[4 * i + 1] = 2 * (random.float(f32) - 0.5);
initial_particle_data[4 * i + 2] = 2 * (random.float(f32) - 0.5) * 0.1; initial_particle_data[4 * i + 2] = 2 * (random.float(f32) - 0.5) * 0.1;
@ -139,24 +132,25 @@ pub fn main() !void {
var particle_buffers: [2]gpu.Buffer = undefined; var particle_buffers: [2]gpu.Buffer = undefined;
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] = app.device.createBuffer(&gpu.Buffer.Descriptor{ particle_buffers[i] = app.device.createBuffer(&gpu.Buffer.Descriptor{
.usage = .{.vertex = true, .copy_dst = true, .storage = true, }, .usage = .{
.vertex = true,
.copy_dst = true,
.storage = true,
},
.size = initial_particle_data.len * @sizeOf(f32), .size = initial_particle_data.len * @sizeOf(f32),
}); });
app.device.getQueue().writeBuffer(particle_buffers[i], 0, f32, &initial_particle_data); app.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] = app.device.createBindGroup(&gpu.BindGroup.Descriptor{ particle_bind_groups[i] = app.device.createBindGroup(&gpu.BindGroup.Descriptor{ .layout = compute_pipeline.getBindGroupLayout(0), .entries = &[_]gpu.BindGroup.Entry{
.layout = compute_pipeline.getBindGroupLayout(0), gpu.BindGroup.Entry.buffer(0, sim_param_buffer, 0, sim_params.len * @sizeOf(f32)),
.entries = &[_]gpu.BindGroup.Entry { gpu.BindGroup.Entry.buffer(1, particle_buffers[i], 0, initial_particle_data.len * @sizeOf(f32)),
gpu.BindGroup.Entry.buffer(0, sim_param_buffer, 0, sim_params.len * @sizeOf(f32)), gpu.BindGroup.Entry.buffer(2, particle_buffers[(i + 1) % 2], 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)),
}
});
} }
ctx.* = FrameParams{ ctx.* = FrameParams{
@ -182,11 +176,9 @@ fn frame(app: *App, params: *FrameParams) !void {
.store_op = .store, .store_op = .store,
}; };
const render_pass_descriptor = gpu.RenderPassEncoder.Descriptor{ const render_pass_descriptor = gpu.RenderPassEncoder.Descriptor{ .color_attachments = &[_]gpu.RenderPassColorAttachment{
.color_attachments = &[_]gpu.RenderPassColorAttachment { color_attachment,
color_attachment, } };
}
};
sim_params[0] = @floatCast(f32, app.delta_time); sim_params[0] = @floatCast(f32, app.delta_time);
app.device.getQueue().writeBuffer(params.sim_param_buffer, 0, f32, &sim_params); app.device.getQueue().writeBuffer(params.sim_param_buffer, 0, f32, &sim_params);
@ -203,15 +195,15 @@ fn frame(app: *App, params: *FrameParams) !void {
{ {
const pass_encoder = command_encoder.beginRenderPass(&render_pass_descriptor); const pass_encoder = command_encoder.beginRenderPass(&render_pass_descriptor);
pass_encoder.setPipeline(params.render_pipeline); pass_encoder.setPipeline(params.render_pipeline);
pass_encoder.setVertexBuffer(0, params.particle_buffers[(params.frame_counter + 1) % 2], 0, num_particle*4*@sizeOf(f32)); pass_encoder.setVertexBuffer(0, params.particle_buffers[(params.frame_counter + 1) % 2], 0, num_particle * 4 * @sizeOf(f32));
pass_encoder.setVertexBuffer(1, params.sprite_vertex_buffer, 0, 6*@sizeOf(f32)); pass_encoder.setVertexBuffer(1, params.sprite_vertex_buffer, 0, 6 * @sizeOf(f32));
pass_encoder.draw(3, num_particle, 0, 0); pass_encoder.draw(3, num_particle, 0, 0);
pass_encoder.end(); pass_encoder.end();
pass_encoder.release(); pass_encoder.release();
} }
params.frame_counter += 1; params.frame_counter += 1;
if(params.frame_counter % 60 == 0) { if (params.frame_counter % 60 == 0) {
std.debug.print("Frame {}\n", .{params.frame_counter}); std.debug.print("Frame {}\n", .{params.frame_counter});
} }
@ -222,4 +214,4 @@ fn frame(app: *App, params: *FrameParams) !void {
app.swap_chain.?.present(); app.swap_chain.?.present();
back_buffer_view.release(); back_buffer_view.release();
} }