Skip to content

Commit

Permalink
Update to bevy 0.15.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Dec 11, 2024
1 parent ac306b4 commit e5f4c41
Show file tree
Hide file tree
Showing 11 changed files with 707 additions and 544 deletions.
1,125 changes: 632 additions & 493 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ 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 = "0.15.0"
bevy_egui = "0.31.1"
bevy-inspector-egui = "0.28.0"
image = "0.25"
rayon = "1.10"
bevy_common_assets = { git = "https://github.com/tychedelia/bevy_common_assets", branch = "main" }
bevy_common_assets = "0.12.0"
serde = "1"
serde_json = "1"
toml = "0.8"
serde_yaml = "0.9"
wgpu = "22.0"
wgpu = "23.0.1"
24 changes: 12 additions & 12 deletions bevy_nannou_draw/src/draw/indirect.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//! A shader that renders a mesh multiple times in one draw call.
use crate::render::{ShaderModelHandle, ShaderStorageBufferHandle};
use crate::{
draw::{drawing::Drawing, primitive::Primitive, Draw, DrawCommand},
render::{
queue_shader_model, DrawShaderModel, PreparedShaderModel, ShaderModel, ShaderModelMesh,
},
render::{queue_shader_model, PreparedShaderModel, ShaderModel},
};
use bevy::pbr::RenderMaterialInstances;
use bevy::render::extract_instances::ExtractedInstances;
use bevy::{
core_pipeline::core_3d::Transparent3d,
ecs::system::{lifetimeless::*, SystemParamItem},
pbr::{RenderMeshInstances, SetMeshBindGroup, SetMeshViewBindGroup},
prelude::*,
render::{
extract_component::{ExtractComponent, ExtractComponentPlugin},
extract_instances::ExtractedInstances,
extract_component::ExtractComponent,
mesh::{allocator::MeshAllocator, RenderMesh, RenderMeshBufferInfo},
render_asset::{prepare_assets, RenderAsset, RenderAssets},
render_phase::{
Expand Down Expand Up @@ -140,7 +140,7 @@ impl<P: PhaseItem, SM: ShaderModel, const I: usize> RenderCommand<P>
{
type Param = (
SRes<RenderAssets<PreparedShaderModel<SM>>>,
SRes<ExtractedInstances<AssetId<SM>>>,
SRes<ExtractedInstances<ShaderModelHandle<SM>>>,
);
type ViewQuery = ();
type ItemQuery = ();
Expand All @@ -156,10 +156,10 @@ impl<P: PhaseItem, SM: ShaderModel, const I: usize> RenderCommand<P>
let models = models.into_inner();
let instances = instances.into_inner();

let Some(asset_id) = instances.get(&item.entity()) else {
let Some(handle) = instances.get(&item.main_entity()) else {
return RenderCommandResult::Skip;
};
let Some(model) = models.get(*asset_id) else {
let Some(model) = models.get(handle.0.id()) else {
return RenderCommandResult::Skip;
};
pass.set_bind_group(I, &model.bind_group, &[]);
Expand All @@ -176,13 +176,13 @@ impl<P: PhaseItem> RenderCommand<P> for DrawMeshIndirect {
SRes<RenderAssets<GpuShaderStorageBuffer>>,
);
type ViewQuery = ();
type ItemQuery = Read<Handle<ShaderStorageBuffer>>;
type ItemQuery = Read<ShaderStorageBufferHandle>;

#[inline]
fn render<'w>(
item: &P,
_view: (),
indirect_buffer: Option<&'w Handle<ShaderStorageBuffer>>,
indirect_buffer: Option<&'w ShaderStorageBufferHandle>,
(meshes, render_mesh_instances, mesh_allocator, ssbos): SystemParamItem<
'w,
'_,
Expand All @@ -192,7 +192,7 @@ impl<P: PhaseItem> RenderCommand<P> for DrawMeshIndirect {
) -> RenderCommandResult {
let mesh_allocator = mesh_allocator.into_inner();

let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(item.entity())
let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(item.main_entity())
else {
return RenderCommandResult::Skip;
};
Expand All @@ -202,7 +202,7 @@ impl<P: PhaseItem> RenderCommand<P> for DrawMeshIndirect {
let Some(indirect_buffer) = indirect_buffer else {
return RenderCommandResult::Skip;
};
let Some(indirect_buffer) = ssbos.into_inner().get(indirect_buffer) else {
let Some(indirect_buffer) = ssbos.into_inner().get(&indirect_buffer.0) else {
return RenderCommandResult::Skip;
};
let Some(vertex_buffer_slice) =
Expand Down
9 changes: 5 additions & 4 deletions bevy_nannou_draw/src/draw/instanced.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::ShaderModelHandle;
use crate::{
draw::{drawing::Drawing, primitive::Primitive, Draw, DrawCommand},
render::{queue_shader_model, PreparedShaderModel, ShaderModel},
Expand Down Expand Up @@ -142,7 +143,7 @@ impl<P: PhaseItem, SM: ShaderModel, const I: usize> RenderCommand<P>
{
type Param = (
SRes<RenderAssets<PreparedShaderModel<SM>>>,
SRes<ExtractedInstances<AssetId<SM>>>,
SRes<ExtractedInstances<ShaderModelHandle<SM>>>,
);
type ViewQuery = ();
type ItemQuery = ();
Expand All @@ -158,10 +159,10 @@ impl<P: PhaseItem, SM: ShaderModel, const I: usize> RenderCommand<P>
let models = models.into_inner();
let instances = instances.into_inner();

let Some(asset_id) = instances.get(&item.entity()) else {
let Some(handle) = instances.get(&item.main_entity()) else {
return RenderCommandResult::Skip;
};
let Some(shader_model) = models.get(*asset_id) else {
let Some(shader_model) = models.get(&handle.0) else {
return RenderCommandResult::Skip;
};
pass.set_bind_group(I, &shader_model.bind_group, &[]);
Expand Down Expand Up @@ -194,7 +195,7 @@ impl<P: PhaseItem> RenderCommand<P> for DrawMeshInstanced {
) -> RenderCommandResult {
let mesh_allocator = mesh_allocator.into_inner();

let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(item.entity())
let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(item.main_entity())
else {
return RenderCommandResult::Skip;
};
Expand Down
57 changes: 40 additions & 17 deletions bevy_nannou_draw/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use crate::{
},
DrawHolder,
};
use bevy::ecs::query::QueryItem;
use bevy::ecs::system::lifetimeless::Read;
use bevy::render::extract_instances::ExtractInstance;
use bevy::render::storage::ShaderStorageBuffer;
use bevy::render::sync_world::MainEntity;
use bevy::{
asset::{load_internal_asset, Asset, UntypedAssetId},
core_pipeline::core_3d::Transparent3d,
Expand Down Expand Up @@ -83,6 +87,21 @@ pub trait ShaderModel:
}
}

#[derive(Component, Clone)]
pub struct ShaderModelHandle<SM: ShaderModel>(pub(crate) Handle<SM>);

impl<SM> ExtractInstance for ShaderModelHandle<SM>
where
SM: ShaderModel,
{
type QueryData = Read<ShaderModelHandle<SM>>;
type QueryFilter = ();

fn extract(item: QueryItem<'_, Self::QueryData>) -> Option<Self> {
Some(item.clone())
}
}

pub struct NannouRenderPlugin;

impl Plugin for NannouRenderPlugin {
Expand All @@ -102,8 +121,8 @@ impl Plugin for NannouRenderPlugin {
ExtractComponentPlugin::<IndirectMesh>::default(),
ExtractComponentPlugin::<InstancedMesh>::default(),
ExtractComponentPlugin::<DrawIndex>::default(),
ExtractComponentPlugin::<Handle<ShaderStorageBuffer>>::default(),
ExtractComponentPlugin::<InstanceRange>::default(),
ExtractComponentPlugin::<ShaderStorageBufferHandle>::default(),
ExtractResourcePlugin::<DefaultTextureHandle>::default(),
NannouShaderModelPlugin::<DefaultNannouShaderModel>::default(),
))
Expand All @@ -130,7 +149,7 @@ where
fn build(&self, app: &mut App) {
app.init_asset::<SM>()
.add_plugins((
ExtractInstancesPlugin::<AssetId<SM>>::extract_visible(),
ExtractInstancesPlugin::<ShaderModelHandle<SM>>::extract_visible(),
RenderAssetPlugin::<PreparedShaderModel<SM>>::default(),
IndirectShaderModelPlugin::<SM>::default(),
InstancedShaderModelPlugin::<SM>::default(),
Expand Down Expand Up @@ -203,7 +222,7 @@ impl<P: PhaseItem, SM: ShaderModel, const I: usize> RenderCommand<P>
{
type Param = (
SRes<RenderAssets<PreparedShaderModel<SM>>>,
SRes<ExtractedInstances<AssetId<SM>>>,
SRes<ExtractedInstances<ShaderModelHandle<SM>>>,
);
type ViewQuery = ();
type ItemQuery = ();
Expand All @@ -219,10 +238,10 @@ impl<P: PhaseItem, SM: ShaderModel, const I: usize> RenderCommand<P>
let models = models.into_inner();
let model_instances = model_instances.into_inner();

let Some(shader_model_asset_id) = model_instances.get(&item.entity()) else {
let Some(handle) = model_instances.get(&item.main_entity()) else {
return RenderCommandResult::Skip;
};
let Some(model) = models.get(*shader_model_asset_id) else {
let Some(model) = models.get(&handle.0) else {
return RenderCommandResult::Skip;
};
pass.set_bind_group(I, &model.bind_group, &[]);
Expand Down Expand Up @@ -319,11 +338,11 @@ pub(crate) fn queue_shader_model<SM, QF, RC>(
extracted_instances,
): (
Res<RenderMeshInstances>,
Query<(Entity, &DrawIndex), QF>,
Query<(Entity, &MainEntity, &DrawIndex), QF>,
ResMut<ViewSortedRenderPhases<Transparent3d>>,
Query<(Entity, &ExtractedView, &Msaa)>,
Res<RenderAssets<PreparedShaderModel<SM>>>,
Res<ExtractedInstances<AssetId<SM>>>,
Res<ExtractedInstances<ShaderModelHandle<SM>>>,
),
) where
SM: ShaderModel,
Expand All @@ -340,14 +359,15 @@ pub(crate) fn queue_shader_model<SM, QF, RC>(
};

let view_key = msaa_key | MeshPipelineKey::from_hdr(view.hdr);
for (entity, draw_idx) in &nannou_meshes {
let Some(shader_model) = extracted_instances.get(&entity) else {
for (entity, main_entity, draw_idx) in &nannou_meshes {
let Some(handle) = extracted_instances.get(main_entity) else {
continue;
};
let Some(shader_model) = shader_models.get(*shader_model) else {
let Some(shader_model) = shader_models.get(&handle.0) else {
continue;
};
let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(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 @@ -366,7 +386,7 @@ pub(crate) fn queue_shader_model<SM, QF, RC>(
phase.add(Transparent3d {
distance: draw_idx.0 as f32,
pipeline,
entity,
entity: (entity, *main_entity),
draw_function,
batch_range: Default::default(),
extra_index: PhaseItemExtraIndex(0),
Expand Down Expand Up @@ -562,7 +582,7 @@ fn update_shader_model<SM>(
if id.type_id() == TypeId::of::<SM>() {
commands
.entity(entity)
.insert(Handle::Weak(id.typed::<SM>()));
.insert(ShaderModelHandle(Handle::Weak(id.typed::<SM>())));
}
}
}
Expand Down Expand Up @@ -650,7 +670,7 @@ fn update_draw_mesh(
InstancedMesh,
InstanceRange(range),
UntypedShaderModelId(model_id),
mesh.clone(),
Mesh3d(mesh.clone()),
Transform::default(),
GlobalTransform::default(),
Visibility::default(),
Expand Down Expand Up @@ -685,9 +705,9 @@ fn update_draw_mesh(
last_shader_model.expect("No shader model set for instanced draw command");
commands.spawn((
IndirectMesh,
indirect_buffer,
ShaderStorageBufferHandle(indirect_buffer),
UntypedShaderModelId(model_id),
mesh.clone(),
Mesh3d(mesh.clone()),
Transform::default(),
GlobalTransform::default(),
Visibility::default(),
Expand All @@ -708,7 +728,7 @@ fn update_draw_mesh(
mesh = meshes.add(Mesh::init());
commands.spawn((
UntypedShaderModelId(model_id),
mesh.clone(),
Mesh3d(mesh.clone()),
Transform::default(),
GlobalTransform::default(),
Visibility::default(),
Expand Down Expand Up @@ -788,3 +808,6 @@ pub mod blend {

#[derive(Component)]
pub struct NannouCamera;

#[derive(Component, ExtractComponent, Clone)]
pub struct ShaderStorageBufferHandle(pub Handle<ShaderStorageBuffer>);
4 changes: 2 additions & 2 deletions nannou/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,13 +734,13 @@ impl<'w> App<'w> {
/// Get the elapsed seconds since startup.
pub fn time(&self) -> f32 {
let time = self.resource::<Time>();
time.elapsed_seconds()
time.elapsed_secs()
}

/// Get the elapsed seconds since the last frame.
pub fn time_delta(&self) -> f32 {
let time = self.resource::<Time>();
time.delta_seconds()
time.delta_secs()
}

// Create a new `App`.
Expand Down
1 change: 1 addition & 0 deletions nannou/src/render/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ where
shader: self.shader.clone(),
shader_defs: vec![],
entry_point: Cow::from(key.shader_entry),
zero_initialize_workgroup_memory: false,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions nannou/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ impl<'w> RenderApp<'w> {
/// Get the elapsed seconds since startup.
pub fn time(&self) -> f32 {
let time = self.world.resource::<Time>();
time.elapsed_seconds()
time.elapsed_secs()
}

/// Get the elapsed seconds since the last frame.
pub fn time_delta(&self) -> f32 {
let time = self.world.resource::<Time>();
time.delta_seconds()
time.delta_secs()
}
}

Expand Down
11 changes: 5 additions & 6 deletions nannou/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ use std::fmt;
use std::ops::Deref;
use std::path::{Path, PathBuf};

use crate::geom::Point2;
use crate::glam::Vec2;
use crate::prelude::WindowResizeConstraints;
use crate::App;
use bevy::input::mouse::MouseWheel;
use bevy::prelude::*;
use bevy::render::camera::RenderTarget;
use bevy::render::extract_component::ExtractComponent;
use bevy::render::renderer::{RenderDevice, RenderQueue};
use bevy::render::view::cursor::CursorIcon;
use bevy::render::view::screenshot::{save_to_disk, Screenshot};
use bevy::render::view::RenderLayers;
use bevy::window::{CursorGrabMode, PrimaryWindow, WindowLevel, WindowMode, WindowRef};

use crate::geom::Point2;
use crate::glam::Vec2;
use crate::prelude::WindowResizeConstraints;
use crate::App;
use bevy::winit::cursor::CursorIcon;
use bevy_nannou::prelude::render::NannouCamera;
use bevy_nannou::prelude::MonitorSelection;
use nannou_core::geom;
Expand Down
2 changes: 1 addition & 1 deletion nannou_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ homepage = "https://nannou.cc"
edition = "2018"

[dependencies]
glam = { version = "0.28.0", default-features = false, features = ["rand"] }
glam = { version = "0.29.2", default-features = false, features = ["rand"] }
# TODO: Awaiting `no-std` to be published.
# noise = 0.6
num-traits = { version = "0.2.14", default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions nannou_wgpu/src/render_pipeline_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ fn build(

let vertex = wgpu::VertexState {
module: &vs_mod,
entry_point: vs_entry_point,
entry_point: Some(vs_entry_point),
compilation_options: Default::default(),
buffers: &vertex_buffers[..],
};
Expand All @@ -546,7 +546,7 @@ fn build(
let fragment = match (fs_mod, color_states.is_empty()) {
(Some(fs_mod), false) => Some(wgpu::FragmentState {
module: &fs_mod,
entry_point: fs_entry_point,
entry_point: Some(fs_entry_point),
compilation_options: Default::default(),
targets: color_states,
}),
Expand Down

0 comments on commit e5f4c41

Please sign in to comment.