diff --git a/.vscode/settings.json b/.vscode/settings.json index 2e62bd7..dfb98d1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "rust-analyzer.cargo.target": "wasm32-unknown-unknown" + // "rust-analyzer.cargo.target": "wasm32-unknown-unknown", } diff --git a/cubeway/README.md b/cubeway/README.md index f7e2323..26aa28a 100644 --- a/cubeway/README.md +++ b/cubeway/README.md @@ -1,7 +1,7 @@ # 3D Conway Implementation - WASM Compatible ```bash -wasm-pack build --target web --out-dir app/pkg +wasm-pack build --target web ``` Add to vscode workspace json settings diff --git a/cubeway/src/compute.wgsl b/cubeway/src/compute.wgsl index 3432357..8c1f15d 100644 --- a/cubeway/src/compute.wgsl +++ b/cubeway/src/compute.wgsl @@ -5,6 +5,8 @@ struct SimParams { struct Instance { position: vec3, unused: f32, + velocity: vec3, + unused2: f32, rotation: vec4, } @@ -18,8 +20,16 @@ struct Instances { fn main(@builtin(global_invocation_id) GlobalInvocationID: vec3) { var index = GlobalInvocationID.x; + var force: vec3 = vec3(0.0, 0.0, 0.0); + for (var i = 0u; i < arrayLength(&instanceBuffer); i++) { + if i == index { + continue; + } - if index < 100u { - instanceBuffer[index].position.y -= 0.001; + var diff = instanceBuffer[i].position - instanceBuffer[index].position; + force += normalize(diff) * params.gravity / length(diff); } + + instanceBuffer[index].velocity += force; + instanceBuffer[index].position += instanceBuffer[index].velocity; } diff --git a/cubeway/src/lib.rs b/cubeway/src/lib.rs index 6d3fc2c..04f33e2 100644 --- a/cubeway/src/lib.rs +++ b/cubeway/src/lib.rs @@ -1,4 +1,4 @@ -use std::{iter, mem}; +use std::{f32::consts::E, iter, mem}; use camera::{Camera, CameraController, CameraUniform}; use cgmath::prelude::*; @@ -58,6 +58,8 @@ impl Vertex { struct Instance { position: [f32; 3], _pad: f32, + velocity: [f32; 3], + _pad2: f32, rotation: [f32; 4], } @@ -76,8 +78,13 @@ impl Instance { format: wgpu::VertexFormat::Float32x3, }, wgpu::VertexAttribute { - offset: 16 as wgpu::BufferAddress, + offset: 16, shader_location: 3, + format: wgpu::VertexFormat::Float32x3, + }, + wgpu::VertexAttribute { + offset: 32 as wgpu::BufferAddress, + shader_location: 4, format: wgpu::VertexFormat::Float32x4, }, ], @@ -130,29 +137,34 @@ impl State { let adapter = instance .request_adapter(&wgpu::RequestAdapterOptions { - power_preference: wgpu::PowerPreference::default(), + power_preference: wgpu::PowerPreference::HighPerformance, compatible_surface: Some(&surface), force_fallback_adapter: false, }) .await .unwrap(); + + println!("{:?}", adapter.get_info()); + log::warn!("{:?}", adapter.get_info()); + log::warn!("{:?}", adapter.get_downlevel_capabilities()); + + log::warn!("Getting device"); + let (device, queue) = adapter .request_device( &wgpu::DeviceDescriptor { label: None, features: wgpu::Features::empty(), - // WebGL doesn't support all of wgpu's features, so if - // we're building for the web we'll have to disable some. - limits: if cfg!(target_arch = "wasm32") { - wgpu::Limits::downlevel_webgl2_defaults() - } else { - wgpu::Limits::default() + limits: wgpu::Limits { + ..wgpu::Limits::downlevel_defaults() }, }, None, // Trace path ) .await - .unwrap(); + .expect("Device unable to get requested features"); + + log::warn!("Device Gotten"); let surface_caps = surface.get_capabilities(&adapter); // Shader code in this tutorial assumes an Srgb surface texture. Using a different @@ -164,6 +176,7 @@ impl State { .copied() .find(|f| f.is_srgb()) .unwrap_or(surface_caps.formats[0]); + let config = wgpu::SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, format: surface_format, @@ -173,6 +186,7 @@ impl State { alpha_mode: surface_caps.alpha_modes[0], view_formats: vec![], }; + surface.configure(&device, &config); let camera = Camera::new(config.width as f32, config.height as f32); @@ -201,6 +215,8 @@ impl State { Instance { position: position.into(), _pad: 0.0, + velocity: [0.0, 0.0, 0.0], + _pad2: 0.0, rotation: [1., 0., 0., 0.], } }) @@ -474,11 +490,12 @@ impl State { // Run Compute Pass encoder.push_debug_group("Particle System"); { - let mut cpass = - encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { label: None }); + let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { + label: Some("Compute Pass"), + }); cpass.set_pipeline(&self.compute_pipeline); cpass.set_bind_group(0, &self.particle_bind_groups[0], &[]); - cpass.dispatch_workgroups(2, 1, 1); + cpass.dispatch_workgroups(65535, 1, 1); } encoder.pop_debug_group(); diff --git a/cubeway/src/shader.wgsl b/cubeway/src/shader.wgsl index b2346a4..a3586b1 100644 --- a/cubeway/src/shader.wgsl +++ b/cubeway/src/shader.wgsl @@ -12,7 +12,8 @@ struct VertexInput { } struct InstanceInput { @location(2) model_position: vec3, - @location(3) model_rotation: vec4 + @location(3) model_velocity: vec3, + @location(4) model_rotation: vec4 } struct VertexOutput {