Skip to content

Commit

Permalink
Fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Sep 27, 2024
1 parent 3a808f4 commit c4df15c
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 156 deletions.
30 changes: 15 additions & 15 deletions bevy_nannou_draw/src/draw/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ where
draw: DrawRef<'a, SM>,
// The draw command index of the primitive being drawn.
pub(crate) index: usize,
// The draw command index of the material being used.
pub(crate) material_index: usize,
// The draw command index of the shader model being used.
pub(crate) shader_model_index: usize,
// Whether the **Drawing** should attempt to finish the drawing on drop.
pub(crate) finish_on_drop: bool,
// The node type currently being drawn.
Expand All @@ -56,7 +56,7 @@ pub struct DrawingContext<'a> {
}

/// Construct a new **Drawing** instance.
pub fn new<T, SM: ShaderModel>(draw: &Draw<SM>, index: usize, material_index: usize) -> Drawing<T, SM>
pub fn new<T, SM: ShaderModel>(draw: &Draw<SM>, index: usize, model_index: usize) -> Drawing<T, SM>
where
T: Into<Primitive>,
SM: ShaderModel + Default,
Expand All @@ -66,7 +66,7 @@ where
Drawing {
draw: DrawRef::Borrowed(draw),
index,
material_index,
shader_model_index: model_index,
finish_on_drop,
_ty,
}
Expand Down Expand Up @@ -117,10 +117,10 @@ where
// If we are "Owned", that means we mutated our material and so need to
// spawn a new entity just for this primitive.
DrawRef::Owned(draw) => {
let id = draw.material.clone();
let id = draw.shader_model.clone();
let material_cmd = state
.draw_commands
.get_mut(self.material_index)
.get_mut(self.shader_model_index)
.expect("Expected a valid material index");
if let None = material_cmd {
*material_cmd = Some(DrawCommand::ShaderModel(id));
Expand Down Expand Up @@ -151,12 +151,12 @@ where
let Drawing {
ref draw,
index,
material_index,
shader_model_index: material_index,
..
} = self;

let state = draw.state.clone();
let material = state.read().unwrap().shader_models[&self.draw.material]
let material = state.read().unwrap().shader_models[&self.draw.shader_model]
.downcast_ref::<SM>()
.unwrap()
.clone();
Expand All @@ -171,20 +171,20 @@ where
state.shader_models.insert(new_id.clone(), Box::new(material));
// Mark the last material as the new material so that further drawings use the same material
// as the parent draw ref.
state.last_material = Some(new_id.clone());
state.last_shader_model = Some(new_id.clone());

let draw = Draw {
state: draw.state.clone(),
context: draw.context.clone(),
material: new_id.clone(),
shader_model: new_id.clone(),
window: draw.window,
_material: Default::default(),
};

Drawing {
draw: DrawRef::Owned(draw),
index,
material_index,
shader_model_index: material_index,
finish_on_drop: true,
_ty: PhantomData,
}
Expand All @@ -208,13 +208,13 @@ where
let Drawing {
ref draw,
index,
material_index,
shader_model_index: material_index,
..
} = self;
Drawing {
draw: draw.clone(),
index,
material_index,
shader_model_index: material_index,
finish_on_drop: true,
_ty: PhantomData,
}
Expand Down Expand Up @@ -242,13 +242,13 @@ where
let Drawing {
ref draw,
index,
material_index,
shader_model_index: material_index,
..
} = self;
Drawing {
draw: draw.clone(),
index,
material_index,
shader_model_index: material_index,
finish_on_drop: true,
_ty: PhantomData,
}
Expand Down
53 changes: 27 additions & 26 deletions bevy_nannou_draw/src/draw/indirect.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
//! A shader that renders a mesh multiple times in one draw call.
use crate::draw::drawing::Drawing;
use crate::draw::primitive::Primitive;
use crate::draw::{Draw, DrawCommand};
use crate::render::{PreparedShaderModel, ShaderModel};
use bevy::render::extract_instances::ExtractedInstances;
use bevy::render::mesh::allocator::MeshAllocator;
use bevy::render::mesh::RenderMeshBufferInfo;
use bevy::render::render_asset::RenderAsset;
use bevy::render::storage::{GpuShaderStorageBuffer, ShaderStorageBuffer};
use crate::{
draw::{drawing::Drawing, primitive::Primitive, Draw, DrawCommand},
render::{
queue_shader_model, DrawShaderModel, PreparedShaderModel, ShaderModel, ShaderModelMesh,
},
};
use bevy::{
core_pipeline::core_3d::Opaque3d,
core_pipeline::core_3d::Transparent3d,
ecs::system::{lifetimeless::*, SystemParamItem},
pbr::{
RenderMeshInstances, SetMeshBindGroup, SetMeshViewBindGroup,
},
pbr::{RenderMeshInstances, SetMeshBindGroup, SetMeshViewBindGroup},
prelude::*,
render::{
extract_component::ExtractComponent,
mesh::RenderMesh,
render_asset::RenderAssets,
extract_component::{ExtractComponent, ExtractComponentPlugin},
extract_instances::ExtractedInstances,
mesh::{allocator::MeshAllocator, RenderMesh, RenderMeshBufferInfo},
render_asset::{prepare_assets, RenderAsset, RenderAssets},
render_phase::{
AddRenderCommand, PhaseItem, RenderCommand,
RenderCommandResult, SetItemPipeline, TrackedRenderPass,
AddRenderCommand, DrawFunctions, PhaseItem, RenderCommand, RenderCommandResult,
SetItemPipeline, TrackedRenderPass,
},
render_resource::*


, RenderApp,
render_resource::*,
storage::{GpuShaderStorageBuffer, ShaderStorageBuffer},
Render, RenderApp, RenderSet,
},
};
use rayon::prelude::*;
use std::hash::Hash;
use std::marker::PhantomData;
use std::{hash::Hash, marker::PhantomData};

pub struct Indirect<'a, SM>
where
Expand Down Expand Up @@ -121,8 +115,15 @@ where
SM::Data: PartialEq + Eq + Hash + Clone,
{
fn build(&self, app: &mut App) {
app.sub_app_mut(RenderApp)
.add_render_command::<Opaque3d, DrawIndirectMaterial<SM>>();
app
.sub_app_mut(RenderApp)
.add_render_command::<Transparent3d, DrawIndirectMaterial<SM>>()
.add_systems(
Render,
queue_shader_model::<SM, With<IndirectMesh>, DrawIndirectMaterial<SM>>
.after(prepare_assets::<PreparedShaderModel<SM>>)
.in_set(RenderSet::QueueMeshes),
);
}
}

Expand Down
56 changes: 29 additions & 27 deletions bevy_nannou_draw/src/draw/instanced.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
//! A shader that renders a mesh multiple times in one draw call.
use crate::draw::drawing::Drawing;
use crate::draw::primitive::Primitive;
use crate::draw::{Draw, DrawCommand};
use crate::render::{PreparedShaderModel, ShaderModel};
use bevy::core_pipeline::core_3d::Opaque3dBinKey;
use bevy::pbr::{MaterialPipeline, MaterialPipelineKey, PreparedMaterial};
use bevy::render::extract_component::ExtractComponentPlugin;
use bevy::render::extract_instances::ExtractedInstances;
use bevy::render::mesh::allocator::MeshAllocator;
use bevy::render::mesh::RenderMeshBufferInfo;
use bevy::render::render_asset::prepare_assets;
use bevy::render::render_phase::{BinnedRenderPhaseType, ViewBinnedRenderPhases};
use bevy::render::storage::GpuShaderStorageBuffer;
use bevy::render::view;
use bevy::render::view::VisibilitySystems;
use crate::{
draw::{drawing::Drawing, primitive::Primitive, Draw, DrawCommand},
render::{queue_shader_model, PreparedShaderModel, ShaderModel},
};
use bevy::{
core_pipeline::core_3d::Opaque3d,
core_pipeline::core_3d::Transparent3d,
ecs::system::{lifetimeless::*, SystemParamItem},
pbr::{MeshPipeline, MeshPipelineKey, RenderMeshInstances, SetMeshViewBindGroup},
pbr::{
MaterialPipeline, MaterialPipelineKey, MeshPipeline, MeshPipelineKey, PreparedMaterial,
RenderMeshInstances, SetMeshBindGroup, SetMeshViewBindGroup,
},
prelude::*,
render::{
extract_component::ExtractComponent,
mesh::{MeshVertexBufferLayoutRef, RenderMesh},
render_asset::RenderAssets,
extract_component::{ExtractComponent, ExtractComponentPlugin},
extract_instances::ExtractedInstances,
mesh::{
allocator::MeshAllocator, MeshVertexBufferLayoutRef, RenderMesh, RenderMeshBufferInfo,
},
render_asset::{prepare_assets, RenderAssets},
render_phase::{
AddRenderCommand, DrawFunctions, PhaseItem, RenderCommand, RenderCommandResult,
SetItemPipeline, TrackedRenderPass,
AddRenderCommand, BinnedRenderPhaseType, DrawFunctions, PhaseItem, RenderCommand,
RenderCommandResult, SetItemPipeline, TrackedRenderPass, ViewBinnedRenderPhases,
},
render_resource::*,
renderer::RenderDevice,
view::ExtractedView,
storage::GpuShaderStorageBuffer,
view,
view::{ExtractedView, VisibilitySystems},
Render, RenderApp, RenderSet,
},
};
use rayon::prelude::*;
use std::hash::Hash;
use std::marker::PhantomData;
use std::ops::Range;
use std::{hash::Hash, marker::PhantomData, ops::Range};

pub struct Instanced<'a, SM>
where
Expand Down Expand Up @@ -126,13 +121,20 @@ where
{
fn build(&self, app: &mut App) {
app.sub_app_mut(RenderApp)
.add_render_command::<Opaque3d, DrawInstancedMaterial<SM>>();
.add_render_command::<Transparent3d, DrawInstancedMaterial<SM>>()
.add_systems(
Render,
queue_shader_model::<SM, With<InstancedMesh>, DrawInstancedMaterial<SM>>
.after(prepare_assets::<PreparedShaderModel<SM>>)
.in_set(RenderSet::QueueMeshes),
);
}
}

type DrawInstancedMaterial<SM> = (
SetItemPipeline,
SetMeshViewBindGroup<0>,
SetMeshBindGroup<1>,
SetShaderModelBindGroup<SM, 2>,
DrawMeshInstanced,
);
Expand Down
Loading

0 comments on commit c4df15c

Please sign in to comment.