Get boids working

This commit is contained in:
Joshua Groves
2020-04-04 00:38:44 -02:30
committed by Josh Groves
parent e1eaa6ca26
commit 2ea9e01e5c
7 changed files with 15 additions and 30 deletions

View File

@@ -53,7 +53,7 @@ cgmath = "0.17"
log = "0.4"
png = "0.15"
winit = { version = "0.22", features = ["web-sys"] }
rand = "0.7.2"
rand = { version = "0.7.2", features = ["wasm-bindgen"] }
bytemuck = "1"
futures = "0.3"

View File

@@ -1,7 +1,8 @@
#version 450
// this shader expects NUM_PARTICLES and PARTICLES_PER_GROUP
// defines to be inserted by main.rs
// These should match the Rust constants defined in main.rs
#define NUM_PARTICLES 1500
#define PARTICLES_PER_GROUP 64
layout(local_size_x = PARTICLES_PER_GROUP) in;

Binary file not shown.

View File

@@ -6,8 +6,6 @@ extern crate rand;
#[path = "../framework.rs"]
mod framework;
use std::fmt::Write;
use wgpu::vertex_attr_array;
// number of boid particles to simulate
@@ -35,34 +33,19 @@ impl framework::Example for Example {
sc_desc: &wgpu::SwapChainDescriptor,
device: &wgpu::Device,
) -> (Self, Option<wgpu::CommandBuffer>) {
// loads comp shader source and adds shared constants as defines to comp shader
const BOIDS_SOURCE: &str = include_str!("boids.comp");
const HEADER: &str = "#version 450";
assert_eq!(BOIDS_SOURCE.lines().next(), Some(HEADER));
let mut boids_source_str = String::from(HEADER);
write!(
boids_source_str,
"\n#define NUM_PARTICLES {}\n#define PARTICLES_PER_GROUP {}",
NUM_PARTICLES, PARTICLES_PER_GROUP
)
.unwrap();
boids_source_str += &BOIDS_SOURCE[HEADER.len()..];
// load (and compile) shaders and create shader modules
let boids = framework::load_glsl(&boids_source_str, framework::ShaderStage::Compute);
let boids_module = device.create_shader_module(&boids);
let boids = include_bytes!("boids.comp.spv");
let boids_module = device
.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&boids[..])).unwrap());
let vs = framework::load_glsl(include_str!("shader.vert"), framework::ShaderStage::Vertex);
let vs_module = device.create_shader_module(&vs);
let vs = include_bytes!("shader.vert.spv");
let vs_module =
device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&vs[..])).unwrap());
let fs = framework::load_glsl(
include_str!("shader.frag"),
framework::ShaderStage::Fragment,
);
let fs_module = device.create_shader_module(&fs);
let fs = include_bytes!("shader.frag.spv");
let fs_module =
device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&fs[..])).unwrap());
// create compute bind layout group and compute pipeline layout

Binary file not shown.

Binary file not shown.

View File

@@ -94,7 +94,8 @@ async fn run_async<E: Example>(event_loop: EventLoop<()>, window: Window) {
*control_flow = if cfg!(feature = "metal-auto-capture") {
ControlFlow::Exit
} else {
ControlFlow::Poll
// TODO: ControlFlow::Poll
ControlFlow::Wait
};
match event {
event::Event::MainEventsCleared => window.request_redraw(),