Skip to content

Commit

Permalink
Update to bevy rc.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Nov 16, 2024
1 parent d8df46a commit c0d1e9e
Show file tree
Hide file tree
Showing 20 changed files with 1,187 additions and 604 deletions.
1,325 changes: 777 additions & 548 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ members = [
resolver = "2"

[workspace.dependencies]
bevy = { git = "https://github.com/bevyengine/bevy", branch = "main" }
bevy_egui = { git = "https://github.com/tychedelia/bevy_egui", branch = "main" }
bevy-inspector-egui = { git = "https://github.com/tychedelia/bevy-inspector-egui", branch = "main" }
bevy = { version = "0.15.0-rc.3" }
bevy_egui = { git = "https://github.com/Vrixyz/bevy_egui", branch = "bevy_main" }
bevy-inspector-egui = { git = "https://github.com/Vrixyz/bevy-inspector-egui", branch = "bevy_0.15" }
image = "0.25"
rayon = "1.10"
bevy_common_assets = { git = "https://github.com/tychedelia/bevy_common_assets", branch = "main" }
bevy_common_assets = { git = "https://github.com/NiklasEi/bevy_common_assets", branch = "bevy-0-15" }
serde = "1"
serde_json = "1"
toml = "0.8"
serde_yaml = "0.9"
wgpu = "22.0"
wgpu = "23.0.0"

[patch.crates-io]
bevy_egui = { git = "https://github.com/Vrixyz/bevy_egui", branch = "bevy_main" }
47 changes: 32 additions & 15 deletions bevy_nannou_draw/src/draw/indirect.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! A shader that renders a mesh multiple times in one draw call.
use crate::render::RenderShaderModelInstances;
use crate::{
draw::{drawing::Drawing, primitive::Primitive, Draw, DrawCommand},
render::{
Expand Down Expand Up @@ -27,7 +28,6 @@ use bevy::{
};
use rayon::prelude::*;
use std::{hash::Hash, marker::PhantomData};
use crate::render::RenderShaderModelInstances;

pub struct Indirect<'a, SM>
where
Expand All @@ -36,6 +36,7 @@ where
draw: &'a Draw<SM>,
primitive_index: Option<usize>,
indirect_buffer: Option<Handle<ShaderStorageBuffer>>,
vertex_buffer: Option<Handle<ShaderStorageBuffer>>,
}

impl<'a, SM> Drop for Indirect<'a, SM>
Expand All @@ -44,7 +45,8 @@ where
{
fn drop(&mut self) {
if let Some((index, ssbo)) = self.primitive_index.take().zip(self.indirect_buffer.take()) {
self.insert_indirect_draw_command(index, ssbo);
let vertex_buffer = self.vertex_buffer.take();
self.insert_indirect_draw_command(index, ssbo, vertex_buffer);
}
}
}
Expand All @@ -57,6 +59,7 @@ where
draw,
primitive_index: None,
indirect_buffer: None,
vertex_buffer: None,
}
}

Expand Down Expand Up @@ -87,12 +90,15 @@ where
&self,
index: usize,
indirect_buffer: Handle<ShaderStorageBuffer>,
vertex_buffer: Option<Handle<ShaderStorageBuffer>>,
) {
let mut state = self.draw.state.write().unwrap();
let primitive = state.drawing.remove(&index).unwrap();
state
.draw_commands
.push(Some(DrawCommand::Indirect(primitive, indirect_buffer)));
state.draw_commands.push(Some(DrawCommand::Indirect(
primitive,
indirect_buffer,
vertex_buffer,
)));
}
}

Expand All @@ -102,6 +108,9 @@ pub struct IndirectMesh;
#[derive(Component, ExtractComponent, Clone)]
pub struct IndirectBuffer(pub Handle<ShaderStorageBuffer>);

#[derive(Component, ExtractComponent, Clone)]
pub struct IndirectVertexBuffer(pub Option<Handle<ShaderStorageBuffer>>);

pub struct IndirectShaderModelPlugin<SM>(PhantomData<SM>);

impl<SM> Default for IndirectShaderModelPlugin<SM>
Expand Down Expand Up @@ -180,13 +189,13 @@ impl<P: PhaseItem> RenderCommand<P> for DrawMeshIndirect {
SRes<RenderAssets<GpuShaderStorageBuffer>>,
);
type ViewQuery = ();
type ItemQuery = Read<IndirectBuffer>;
type ItemQuery = (Read<IndirectBuffer>, Read<IndirectVertexBuffer>);

#[inline]
fn render<'w>(
item: &P,
_view: (),
indirect_buffer: Option<&'w IndirectBuffer>,
item_q: Option<(&'w IndirectBuffer, &'w IndirectVertexBuffer)>,
(meshes, render_mesh_instances, mesh_allocator, ssbos): SystemParamItem<
'w,
'_,
Expand All @@ -195,27 +204,35 @@ impl<P: PhaseItem> RenderCommand<P> for DrawMeshIndirect {
pass: &mut TrackedRenderPass<'w>,
) -> RenderCommandResult {
let mesh_allocator = mesh_allocator.into_inner();
let meshes = meshes.into_inner();
let ssbos = ssbos.into_inner();

let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(item.main_entity())
else {
return RenderCommandResult::Skip;
};
let Some(gpu_mesh) = meshes.into_inner().get(mesh_instance.mesh_asset_id) else {
let Some(gpu_mesh) = meshes.get(mesh_instance.mesh_asset_id) else {
return RenderCommandResult::Skip;
};
let Some(indirect_buffer) = indirect_buffer else {
let Some((indirect_buffer, vertex_buffer)) = item_q else {
return RenderCommandResult::Skip;
};
let Some(indirect_buffer) = ssbos.into_inner().get(&indirect_buffer.0) else {
let Some(indirect_buffer) = ssbos.get(&indirect_buffer.0) else {
return RenderCommandResult::Skip;
};
let Some(vertex_buffer_slice) =
mesh_allocator.mesh_vertex_slice(&mesh_instance.mesh_asset_id)
else {
return RenderCommandResult::Skip;

let vertex_buffer = match &vertex_buffer.0 {
Some(vertex_buffer) => match ssbos.get(vertex_buffer) {
Some(vertex_buffer) => vertex_buffer.buffer.slice(..),
None => return RenderCommandResult::Skip,
},
None => match mesh_allocator.mesh_vertex_slice(&mesh_instance.mesh_asset_id) {
Some(vertex_buffer_slice) => vertex_buffer_slice.buffer.slice(..),
None => return RenderCommandResult::Skip,
},
};

pass.set_vertex_buffer(0, vertex_buffer_slice.buffer.slice(..));
pass.set_vertex_buffer(0, vertex_buffer);

match &gpu_mesh.buffer_info {
RenderMeshBufferInfo::Indexed { index_format, .. } => {
Expand Down
1 change: 1 addition & 0 deletions bevy_nannou_draw/src/draw/instanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use bevy::{
};
use rayon::prelude::*;
use std::{hash::Hash, marker::PhantomData, ops::Range};
use bevy::render::render_phase::DrawFunctions;
use crate::render::RenderShaderModelInstances;

pub struct Instanced<'a, SM>
Expand Down
2 changes: 1 addition & 1 deletion bevy_nannou_draw/src/draw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub enum DrawCommand {
/// Draw an instanced primitive
Instanced(Primitive, Range<u32>),
/// Draw a primitive using an indirect buffer.
Indirect(Primitive, Handle<ShaderStorageBuffer>),
Indirect(Primitive, Handle<ShaderStorageBuffer>, Option<Handle<ShaderStorageBuffer>>),
/// A change in the rendering context occurred.
Context(DrawContext),
/// A change in the shader model occurred.
Expand Down
33 changes: 19 additions & 14 deletions bevy_nannou_draw/src/render.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::draw::indirect::{IndirectBuffer, IndirectVertexBuffer};
use crate::{
draw::{
indirect::{IndirectMesh, IndirectShaderModelPlugin},
Expand All @@ -9,6 +10,7 @@ use crate::{
DrawHolder,
};
use bevy::render::storage::ShaderStorageBuffer;
use bevy::render::sync_world::{MainEntity, MainEntityHashMap, RenderEntity};
use bevy::render::Extract;
use bevy::{
asset::{load_internal_asset, Asset, UntypedAssetId},
Expand Down Expand Up @@ -54,13 +56,11 @@ use bevy::{
};
use lyon::lyon_tessellation::{FillTessellator, StrokeTessellator};
use std::{any::TypeId, hash::Hash, marker::PhantomData};
use bevy::render::sync_world::{MainEntity, MainEntityHashMap, RenderEntity};
use crate::draw::indirect::IndirectBuffer;

pub const DEFAULT_NANNOU_SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(3086880141013591);

pub trait ShaderModel:
Asset + AsBindGroup + Clone + Default + Sized + Send + Sync + 'static
Asset + AsBindGroup + Clone + Default + Sized + Send + Sync + 'static
{
/// Returns this shader model's vertex shader. If [`ShaderRef::Default`] is returned, the default mesh vertex shader
/// will be used.
Expand Down Expand Up @@ -220,7 +220,7 @@ impl<SM: ShaderModel> RenderAsset for PreparedShaderModel<SM> {
/// Sets the bind group for a given [`ShaderModel`] at the configured `I` index.
pub struct SetShaderModelBindGroup<SM: ShaderModel, const I: usize>(PhantomData<SM>);
impl<P: PhaseItem, SM: ShaderModel, const I: usize> RenderCommand<P>
for SetShaderModelBindGroup<SM, I>
for SetShaderModelBindGroup<SM, I>
{
type Param = (
SRes<RenderAssets<PreparedShaderModel<SM>>>,
Expand Down Expand Up @@ -324,8 +324,9 @@ impl From<&NannouShaderModel> for NannouBindGroupData {
}
}

fn clear_shader_model_instances<SM>(mut shader_model_instances: ResMut<RenderShaderModelInstances<SM>>)
where
fn clear_shader_model_instances<SM>(
mut shader_model_instances: ResMut<RenderShaderModelInstances<SM>>,
) where
SM: ShaderModel,
SM::Data: PartialEq + Eq + Hash + Clone,
{
Expand All @@ -334,7 +335,7 @@ where

fn extract_shader_models<SM>(
mut material_instances: ResMut<RenderShaderModelInstances<SM>>,
query: Extract<Query<(Entity, &ViewVisibility, &ShaderModelHandle<SM>), With<ShaderModelMesh>>>,
query: Extract<Query<(Entity, &ViewVisibility, &ShaderModelHandle<SM>)>>,
) where
SM: ShaderModel,
SM::Data: PartialEq + Eq + Hash + Clone,
Expand Down Expand Up @@ -390,7 +391,8 @@ pub(crate) fn queue_shader_model<SM, QF, RC>(
let Some(shader_model) = shader_models.get(*shader_model) else {
continue;
};
let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(*main_entity) else {
let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(*main_entity)
else {
continue;
};
let Some(mesh) = meshes.get(mesh_instance.mesh_asset_id) else {
Expand All @@ -405,7 +407,6 @@ pub(crate) fn queue_shader_model<SM, QF, RC>(
let pipeline = pipelines
.specialize(&pipeline_cache, &custom_pipeline, key, &mesh.layout)
.unwrap();

phase.add(Transparent3d {
distance: draw_idx.0 as f32,
pipeline,
Expand Down Expand Up @@ -694,14 +695,15 @@ fn update_draw_mesh(
InstanceRange(range),
UntypedShaderModelId(model_id),
Mesh3d(mesh.clone()),
SpatialBundle::INHERITED_IDENTITY,
Transform::IDENTITY,
Visibility::default(),
NannouTransient,
NoFrustumCulling,
DrawIndex(idx),
window_layers.clone(),
));
}
DrawCommand::Indirect(prim, indirect_buffer) => {
DrawCommand::Indirect(prim, indirect_buffer, vertex_buffer) => {
// Info required during rendering.
let ctxt = RenderContext {
intermediary_mesh: &intermediary_state.intermediary_mesh,
Expand All @@ -725,9 +727,11 @@ fn update_draw_mesh(
commands.spawn((
IndirectMesh,
IndirectBuffer(indirect_buffer),
IndirectVertexBuffer(vertex_buffer),
UntypedShaderModelId(model_id),
Mesh3d(mesh.clone()),
SpatialBundle::INHERITED_IDENTITY,
Transform::IDENTITY,
Visibility::default(),
NannouTransient,
NoFrustumCulling,
DrawIndex(idx),
Expand All @@ -745,7 +749,8 @@ fn update_draw_mesh(
UntypedShaderModelId(model_id),
ShaderModelMesh,
Mesh3d(mesh.clone()),
SpatialBundle::INHERITED_IDENTITY,
Transform::IDENTITY,
Visibility::default(),
NannouTransient,
NoFrustumCulling,
DrawIndex(idx),
Expand Down Expand Up @@ -817,4 +822,4 @@ pub mod blend {
dst_factor: wgpu::BlendFactor::One,
operation: wgpu::BlendOperation::Max,
};
}
}
3 changes: 3 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ path = "compute/particle_mouse.rs"
[[example]]
name = "particle_sdf"
path = "compute/particle_sdf.rs"
[[example]]
name = "particle_volume"
path = "compute/vertex_pulling.rs"

# Draw
[[example]]
Expand Down
9 changes: 0 additions & 9 deletions examples/assets/draw_fragment_shader.wgsl

This file was deleted.

7 changes: 3 additions & 4 deletions examples/assets/shaders/particle_sdf_compute.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct DrawIndirectArgs {
@group(0) @binding(2) var<uniform> sphere_radius: f32;
@group(0) @binding(3) var<uniform> scaling_factor: u32;
@group(0) @binding(4) var<uniform> resolution: vec2<u32>;
@group(0) @binding(5) var<storage, read_write> indirect_args: array<DrawIndirectArgs>;
@group(0) @binding(5) var<storage, read_write> indirect_args: DrawIndirectArgs;

fn sdf_sphere(point: vec3<f32>, center: vec3<f32>, radius: f32) -> f32 {
return length(point - center) - radius;
Expand Down Expand Up @@ -48,7 +48,6 @@ fn raymarch(ro: vec3<f32>, rd: vec3<f32>, max_steps: u32, max_dist: f32) -> f32

fn closest_point_on_sphere(point: vec2<f32>) -> vec3<f32> {
let aspect = f32(resolution.x) / f32(resolution.y);
// Adjust for the new coordinate system
let point3d = vec3(point.x * aspect, point.y, 0.0);
let to_sphere = normalize(point3d - sphere_center.xyz);
let surface_point = sphere_center.xyz + to_sphere * sphere_radius;
Expand All @@ -62,7 +61,7 @@ fn update(@builtin(global_invocation_id) global_id: vec3<u32>) {

/// Update our indirect params if we are thread 0
if (global_id.x == 0u && global_id.y == 0u && global_id.z == 0u) {
atomicStore(&indirect_args[0].instance_count, particle_count);
atomicStore(&indirect_args.instance_count, particle_count);
}

let index = global_id.x;
Expand Down Expand Up @@ -181,7 +180,7 @@ fn init(@builtin(global_invocation_id) global_id: vec3<u32>) {

/// Update our indirect params if we are thread 0
if (global_id.x == 0u && global_id.y == 0u && global_id.z == 0u) {
atomicStore(&indirect_args[0].instance_count, particle_count);
atomicStore(&indirect_args.instance_count, particle_count);
}

let index = global_id.x;
Expand Down
32 changes: 32 additions & 0 deletions examples/assets/shaders/vertex_pulling_compute.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
struct Vertex {
position: vec3<f32>,
normal: vec3<f32>,
color: vec4<f32>,
};

struct DrawIndirectArgs {
vertex_count: u32,
instance_count: atomic<u32>,
first_vertex: u32,
first_instance: u32,
}

@group(0) @binding(0) var<storage, read_write> vertices: array<Vertex>;
@group(0) @binding(1) var<uniform> sphere_center: vec4<f32>;
@group(0) @binding(2) var<uniform> sphere_radius: f32;
@group(0) @binding(3) var<uniform> cube_resolution: vec3<u32>;
@group(0) @binding(4) var<storage, read_write> indirect_args: DrawIndirectArgs;

fn sdf_sphere(point: vec3<f32>, center: vec3<f32>, radius: f32) -> f32 {
return length(point - center) - radius;
}

@compute @workgroup_size(64)
fn update(@builtin(global_invocation_id) global_id: vec3<u32>) {

}

@compute @workgroup_size(64)
fn init(@builtin(global_invocation_id) global_id: vec3<u32>) {

}
Loading

0 comments on commit c0d1e9e

Please sign in to comment.