Skip to content

Commit

Permalink
Compute.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Sep 6, 2024
1 parent 04329e4 commit 0439a1e
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 193 deletions.
4 changes: 2 additions & 2 deletions bevy_nannou_draw/src/draw/render/instancing.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import bevy_pbr::mesh_functions::{get_model_matrix, mesh_position_local_to_clip}
#import bevy_pbr::mesh_functions::{get_world_from_local, mesh_position_local_to_clip}

struct Vertex {
@location(0) position: vec3<f32>,
Expand All @@ -23,7 +23,7 @@ fn vertex(vertex: Vertex) -> VertexOutput {
// index in the Mesh array. This index could be passed in via another
// uniform instead but it's unnecessary for the example.
out.clip_position = mesh_position_local_to_clip(
get_model_matrix(0u),
get_world_from_local(0u),
vec4<f32>(position, 1.0)
);
out.color = vec4(1.0, 0.0, 0.0, 1.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,6 @@ struct Particle {
@group(0) @binding(1) var<uniform> mouse: vec2<f32>;
@group(0) @binding(2) var<uniform> resolution: vec2<f32>;


struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) color: vec4<f32>,
};

@vertex
fn vertex(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
let particle = particles[vertex_index];

var out: VertexOutput;
out.clip_position = vec4<f32>(particle.position, 0.0, 1.0);
out.color = particle.color;
return out;
}

@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
return in.color;
}

fn random(seed: vec2<f32>) -> f32 {
return fract(sin(dot(seed, vec2(12.9898, 78.233))) * 43758.5453);
}
Expand Down
34 changes: 34 additions & 0 deletions examples/assets/shaders/particle_mouse_material.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#import bevy_pbr::mesh_functions::{get_world_from_local, mesh_position_local_to_clip}
#import bevy_pbr::forward_io::{Vertex}

struct Particle {
position: vec2<f32>,
velocity: vec2<f32>,
color: vec4<f32>,
};

@group(2) @binding(0) var<storage, read> particles: array<Particle>;

struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) color: vec4<f32>,
};

@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
let particle = particles[vertex.instance_index];
var out: VertexOutput;
let position = vec4<f32>(particle.position, 0.0, 1.0);;
out.clip_position = mesh_position_local_to_clip(
get_world_from_local(0u),
position
);

out.color = particle.color;
return out;
}

@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
return in.color;
}
2 changes: 1 addition & 1 deletion examples/compute/game_of_life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Compute for ComputeModel {
"shaders/game_of_life.wgsl".into()
}

fn shader_entry(state: &Self::State) -> &'static str {
fn entry(state: &Self::State) -> &'static str {
match state {
State::Init => "init",
State::Update(_) => "update",
Expand Down
58 changes: 31 additions & 27 deletions examples/compute/particle_mouse.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use bytemuck::{Pod, Zeroable};
use nannou::prelude::bevy_render::renderer::RenderDevice;
use nannou::prelude::bevy_render::storage::ShaderStorageBuffer;
use nannou::prelude::*;
use std::sync::Arc;

const NUM_PARTICLES: u32 = 10000;
const NUM_PARTICLES: u32 = 1000;
const WORKGROUP_SIZE: u32 = 64;

fn main() {
nannou::app(model).compute(compute).update(update).run();
}

struct Model {
particles: Buffer,
particles: Handle<ShaderStorageBuffer>,
}

#[derive(Default, Clone, Copy, Pod, Zeroable)]
#[repr(C)]
#[derive(ShaderType, Copy, Clone, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)]
struct Particle {
position: Vec2,
velocity: Vec2,
Expand All @@ -31,8 +31,8 @@ enum State {

#[derive(AsBindGroup, Clone)]
struct ComputeModel {
#[storage(0, buffer, visibility(compute, vertex))]
particles: Buffer,
#[storage(0, visibility(compute))]
particles: Handle<ShaderStorageBuffer>,
#[uniform(1)]
mouse: Vec2,
#[uniform(2)]
Expand All @@ -46,7 +46,7 @@ impl Compute for ComputeModel {
"shaders/particle_mouse.wgsl".into()
}

fn shader_entry(state: &Self::State) -> &'static str {
fn entry(state: &Self::State) -> &'static str {
match state {
State::Init => "init",
State::Update => "update",
Expand All @@ -58,14 +58,12 @@ impl Compute for ComputeModel {
}
}

#[derive(AsBindGroup, Asset, TypePath, Clone)]
#[derive(AsBindGroup, Asset, TypePath, Clone, Default)]
struct DrawMaterial {
#[storage(0, buffer, visibility(compute, vertex))]
particles: Buffer,
#[storage(0, visibility(vertex))]
particles: Handle<ShaderStorageBuffer>,
}



impl Material for DrawMaterial {
fn vertex_shader() -> ShaderRef {
"shaders/particle_mouse.wgsl".into()
Expand All @@ -83,18 +81,23 @@ fn model(app: &App) -> Model {
.size(1024, 768)
.view(view)
.build();
let device = app.resource_mut::<RenderDevice>();

let particles = device.create_buffer_with_data(&BufferInitDescriptor {
label: Some("ParticleBuffer"),
contents: bytemuck::cast_slice(&vec![Particle::default(); NUM_PARTICLES as usize]),
usage: BufferUsages::STORAGE | BufferUsages::VERTEX,
});
// Create a buffer to store the particles.
let particle_size = Particle::min_size().get() as usize;
let mut particles = ShaderStorageBuffer::with_size(
NUM_PARTICLES as usize * particle_size,
RenderAssetUsages::RENDER_WORLD,
);
particles.buffer_description.usage |= BufferUsages::STORAGE | BufferUsages::VERTEX;

let particles = app.assets_mut().add(particles.clone());

Model { particles }
}

fn update(app: &App, model: &mut Model) {}
fn update(app: &App, model: &mut Model) {

}

fn compute(app: &App, model: &Model, state: State, view: Entity) -> (State, ComputeModel) {
let window = app.main_window();
Expand All @@ -114,12 +117,13 @@ fn compute(app: &App, model: &Model, state: State, view: Entity) -> (State, Comp
}

fn view(app: &App, model: &Model) {
let draw = app.draw()
.material(DrawMaterial {
particles: model.particles.clone(),
});
draw.background()
.color(BLACK);
draw.polyline()
.points(vec![Vec2::ZERO; NUM_PARTICLES as usize]);
let draw = app.draw().material(DrawMaterial {
particles: model.particles.clone(),
});
draw.background().color(BLACK);

for _ in 0..NUM_PARTICLES {
draw.rect()
.w_h(1.0, 1.0);
}
}
7 changes: 3 additions & 4 deletions nannou/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ use crate::prelude::bevy_ecs::system::SystemState;
use crate::prelude::render::NannouMesh;
use crate::prelude::NannouMaterialPlugin;
use crate::render::{
Compute, ComputeModel, ComputePlugin, ComputeShaderHandle, ComputeState, NannouRenderNode,
RenderApp, RenderPlugin,
compute::{Compute, ComputeModel, ComputePlugin, ComputeShaderHandle, ComputeState},
NannouRenderNode, RenderApp, RenderPlugin
};
use crate::window::WindowUserFunctions;
use crate::{camera, geom, light, window};
Expand Down Expand Up @@ -258,8 +258,7 @@ where
primary_window: None,
exit_condition: ExitCondition::OnAllClosed,
..default()
})
.set(ImagePlugin::default_nearest()),
}),
#[cfg(feature = "egui")]
bevy_egui::EguiPlugin,
NannouPlugin,
Expand Down
1 change: 1 addition & 0 deletions nannou/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use bevy_egui::egui;

pub use crate::frame::*;
pub use crate::render::*;
pub use crate::render::compute::*;
pub use crate::wgpu;
pub use crate::wgpu::util::{BufferInitDescriptor, DeviceExt};
pub use bevy_nannou::prelude::*;
Expand Down
Loading

0 comments on commit 0439a1e

Please sign in to comment.