Skip to content

Commit

Permalink
Merge pull request #6 from narasan49/refactor-render-resource-definition
Browse files Browse the repository at this point in the history
Refactor render resource definition
  • Loading branch information
narasan49 authored Aug 31, 2024
2 parents d698184 + 1f27fef commit 718fe03
Show file tree
Hide file tree
Showing 19 changed files with 580 additions and 372 deletions.
6 changes: 3 additions & 3 deletions assets/shaders/add_force.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

@group(0) @binding(0) var<storage, read> force: array<vec2<f32>>;
@group(0) @binding(1) var<storage, read> position: array<vec2<f32>>;
@group(0) @binding(2) var u: texture_storage_2d<r32float, read_write>;
@group(0) @binding(3) var v: texture_storage_2d<r32float, read_write>;
@group(1) @binding(0) var u: texture_storage_2d<r32float, read_write>;
@group(1) @binding(1) var v: texture_storage_2d<r32float, read_write>;

@group(1) @binding(0) var<uniform> constants: SimulationUniform;
@group(2) @binding(0) var<uniform> constants: SimulationUniform;

@compute
@workgroup_size(1, 64, 1)
Expand Down
23 changes: 13 additions & 10 deletions assets/shaders/advection.wgsl
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#import bevy_fluid::fluid_uniform::SimulationUniform;

@group(0) @binding(0) var u_in: texture_storage_2d<r32float, read_write>;
@group(0) @binding(1) var u_out: texture_storage_2d<r32float, read_write>;
@group(0) @binding(2) var v_in: texture_storage_2d<r32float, read_write>;
@group(0) @binding(3) var v_out: texture_storage_2d<r32float, read_write>;
@group(0) @binding(1) var v_in: texture_storage_2d<r32float, read_write>;
@group(1) @binding(0) var u_out: texture_storage_2d<r32float, read_write>;
@group(1) @binding(1) var v_out: texture_storage_2d<r32float, read_write>;

@group(1) @binding(0) var<uniform> constants: SimulationUniform;
@group(2) @binding(0) var<uniform> constants: SimulationUniform;

@group(2) @binding(0) var grid_label: texture_storage_2d<r32uint, read_write>;
@group(2) @binding(1) var u_solid: texture_storage_2d<r32float, read_write>;
@group(2) @binding(2) var v_solid: texture_storage_2d<r32float, read_write>;
@group(3) @binding(0) var grid_label: texture_storage_2d<r32uint, read_write>;
@group(3) @binding(1) var u_solid: texture_storage_2d<r32float, read_write>;
@group(3) @binding(2) var v_solid: texture_storage_2d<r32float, read_write>;

// ToDo: Move to a separate file
@compute @workgroup_size(1, 64, 1)
@compute
@workgroup_size(1, 64, 1)
fn initialize(
@builtin(global_invocation_id) invocation_id: vec3<u32>,
) {
Expand All @@ -26,7 +27,8 @@ fn initialize(
textureStore(v_out, x_v, vec4<f32>(speed, 0.0, 0.0, 0.0));
}

@compute @workgroup_size(1, 64, 1)
@compute
@workgroup_size(1, 64, 1)
fn advection(
@builtin(global_invocation_id) invocation_id: vec3<u32>,
) {
Expand Down Expand Up @@ -66,7 +68,8 @@ fn advection(
}

// ToDo: Move to a separate file
@compute @workgroup_size(1, 64, 1)
@compute
@workgroup_size(1, 64, 1)
fn swap(
@builtin(global_invocation_id) invocation_id: vec3<u32>,
) {
Expand Down
13 changes: 7 additions & 6 deletions assets/shaders/divergence.wgsl
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#import bevy_fluid::coordinate::{left, right, bottom, top};

@group(0) @binding(0) var u_in: texture_storage_2d<r32float, read_write>;
@group(0) @binding(1) var v_in: texture_storage_2d<r32float, read_write>;
@group(0) @binding(2) var div: texture_storage_2d<r32float, read_write>;
@group(0) @binding(0) var div: texture_storage_2d<r32float, read_write>;

@group(1) @binding(0) var grid_label: texture_storage_2d<r32uint, read_write>;
@group(1) @binding(1) var u_solid: texture_storage_2d<r32float, read_write>;
@group(1) @binding(2) var v_solid: texture_storage_2d<r32float, read_write>;
@group(1) @binding(0) var u_in: texture_storage_2d<r32float, read_write>;
@group(1) @binding(1) var v_in: texture_storage_2d<r32float, read_write>;

@group(2) @binding(0) var grid_label: texture_storage_2d<r32uint, read_write>;
@group(2) @binding(1) var u_solid: texture_storage_2d<r32float, read_write>;
@group(2) @binding(2) var v_solid: texture_storage_2d<r32float, read_write>;

@compute @workgroup_size(8, 8, 1)
fn divergence(@builtin(global_invocation_id) invocation_id: vec3<u32>) {
Expand Down
12 changes: 5 additions & 7 deletions assets/shaders/jacobi_iteration.wgsl
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#import bevy_fluid::fluid_uniform::SimulationUniform;
#import bevy_fluid::coordinate::{left, right, bottom, top};

@group(0) @binding(0) var div: texture_storage_2d<r32float, read_write>;
@group(0) @binding(1) var p_in: texture_storage_2d<r32float, read_write>;
@group(0) @binding(2) var p_out: texture_storage_2d<r32float, read_write>;

@group(1) @binding(0) var<uniform> constants: SimulationUniform;

@group(2) @binding(0) var grid_label: texture_storage_2d<r32uint, read_write>;
@group(0) @binding(0) var p_in: texture_storage_2d<r32float, read_write>;
@group(1) @binding(0) var p_out: texture_storage_2d<r32float, read_write>;
@group(2) @binding(0) var<uniform> constants: SimulationUniform;
@group(3) @binding(0) var grid_label: texture_storage_2d<r32uint, read_write>;
@group(4) @binding(0) var div: texture_storage_2d<r32float, read_write>;

@compute @workgroup_size(8, 8, 1)
fn jacobi_iteration(@builtin(global_invocation_id) invocation_id: vec3<u32>) {
Expand Down
16 changes: 9 additions & 7 deletions assets/shaders/solve_pressure.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

@group(0) @binding(0) var u_in: texture_storage_2d<r32float, read_write>;
@group(0) @binding(1) var v_in: texture_storage_2d<r32float, read_write>;
@group(0) @binding(2) var u_out: texture_storage_2d<r32float, read_write>;
@group(0) @binding(3) var v_out: texture_storage_2d<r32float, read_write>;
@group(0) @binding(4) var pressure: texture_storage_2d<r32float, read_write>;

@group(1) @binding(0) var<uniform> constants: SimulationUniform;
@group(1) @binding(0) var u_out: texture_storage_2d<r32float, read_write>;
@group(1) @binding(1) var v_out: texture_storage_2d<r32float, read_write>;

@group(2) @binding(0) var grid_label: texture_storage_2d<r32uint, read_write>;
@group(2) @binding(1) var u_solid: texture_storage_2d<r32float, read_write>;
@group(2) @binding(2) var v_solid: texture_storage_2d<r32float, read_write>;
@group(2) @binding(0) var pressure: texture_storage_2d<r32float, read_write>;

@group(3) @binding(0) var<uniform> constants: SimulationUniform;

@group(4) @binding(0) var grid_label: texture_storage_2d<r32uint, read_write>;
@group(4) @binding(1) var u_solid: texture_storage_2d<r32float, read_write>;
@group(4) @binding(2) var v_solid: texture_storage_2d<r32float, read_write>;

@compute @workgroup_size(1, 64, 1)
fn solve_pressure(@builtin(global_invocation_id) invocation_id: vec3<u32>) {
Expand Down
10 changes: 5 additions & 5 deletions examples/demo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use bevy::{
};

use bevy_fluid::euler_fluid::{
advection::AdvectionMaterial,
fluid_material::VelocityMaterial,
geometry::{self},
materials::staggered_velocity::StaggeredVelocityMaterial,
uniform::SimulationUniform,
FluidPlugin,
};
Expand Down Expand Up @@ -115,20 +115,20 @@ fn setup_scene(mut commands: Commands) {

fn on_advection_initialized(
mut commands: Commands,
advection: Option<Res<AdvectionMaterial>>,
velocity: Option<Res<StaggeredVelocityMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<VelocityMaterial>>,
) {
if let Some(advection) = advection {
if let Some(advection) = velocity {
if advection.is_changed() {
// spwan plane to visualize advection
let mesh =
meshes.add(Mesh::from(Plane3d::default()).translated_by(Vec3::new(-1.0, 0.0, 0.0)));
let material = materials.add(VelocityMaterial {
offset: 0.5,
scale: 0.1,
u: Some(advection.u_in.clone()),
v: Some(advection.v_in.clone()),
u: Some(advection.u.clone()),
v: Some(advection.v.clone()),
});
commands.spawn((
Name::new("advection"),
Expand Down
46 changes: 29 additions & 17 deletions examples/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ use bevy::{
};

use bevy_fluid::euler_fluid::{
add_force::AddForceMaterial, advection::AdvectionMaterial, fluid_material::VelocityMaterial,
uniform::SimulationUniform, FluidPlugin,
fluid_material::VelocityMaterial,
materials::{local_force::LocalForceMaterial, staggered_velocity::StaggeredVelocityMaterial},
uniform::SimulationUniform,
FluidPlugin,
};

const WIDTH: f32 = 1280.0;
Expand Down Expand Up @@ -79,20 +81,20 @@ fn setup_scene(mut commands: Commands) {

fn on_advection_initialized(
mut commands: Commands,
advection: Option<Res<AdvectionMaterial>>,
velocity: Option<Res<StaggeredVelocityMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<VelocityMaterial>>,
) {
if let Some(advection) = advection {
if let Some(advection) = velocity {
if advection.is_changed() {
info!("prepare velocity texture");
// spwan plane to visualize advection
let mesh = meshes.add(Rectangle::default());
let material = materials.add(VelocityMaterial {
offset: 0.5,
scale: 0.1,
u: Some(advection.u_in.clone()),
v: Some(advection.v_in.clone()),
u: Some(advection.u.clone()),
v: Some(advection.v.clone()),
});

commands
Expand All @@ -117,7 +119,7 @@ fn mouse_motion(
mouse_button_input: Res<ButtonInput<MouseButton>>,
mut mouse_motion: EventReader<MouseMotion>,
touches: Res<Touches>,
mut force_material: ResMut<AddForceMaterial>,
mut force_material: ResMut<LocalForceMaterial>,
q_window: Query<&Window, With<PrimaryWindow>>,
q_camera: Query<&OrthographicProjection, With<Camera2d>>,
) {
Expand All @@ -129,23 +131,33 @@ fn mouse_motion(
.map(|mouse| mouse.delta)
.collect::<Vec<_>>();

let position = screen_to_mesh_coordinate(cursor_position, window, q_camera.single(), Vec2::splat(512.));
let position = screen_to_mesh_coordinate(
cursor_position,
window,
q_camera.single(),
Vec2::splat(512.),
);
let position = vec![position; force.len()];
force_material.force = force;
force_material.position = position;

return;
}
}

let touch_forces = touches.iter()
.map(|touch| {
touch.delta()
})

let touch_forces = touches
.iter()
.map(|touch| touch.delta())
.collect::<Vec<_>>();
let touch_position = touches.iter()
let touch_position = touches
.iter()
.map(|touch| {
screen_to_mesh_coordinate(touch.position(), q_window.single(), q_camera.single(), Vec2::splat(512.))
screen_to_mesh_coordinate(
touch.position(),
q_window.single(),
q_camera.single(),
Vec2::splat(512.),
)
})
.collect::<Vec<_>>();
force_material.force = touch_forces;
Expand All @@ -161,7 +173,7 @@ fn screen_to_mesh_coordinate(
let window_size = window.size();
let normalized_position = 2.0 * (position - window_size) / window_size + 1.0;
let inv_proj = projection.get_clip_from_view().inverse();

let position_on_mesh = inv_proj.mul_vec4(Vec4::new(
normalized_position.x,
normalized_position.y,
Expand All @@ -170,4 +182,4 @@ fn screen_to_mesh_coordinate(
));

position_on_mesh.xy() + 0.5 * scale
}
}
Loading

0 comments on commit 718fe03

Please sign in to comment.