Skip to content

Commit

Permalink
0.15-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Aug 31, 2024
1 parent ac8c638 commit 2055d1d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 39 additions & 17 deletions bevy_nannou_draw/src/draw/instanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy::{
prelude::*,
render::{
extract_component::{ExtractComponent, ExtractComponentPlugin},
mesh::{GpuBufferInfo, GpuMesh, MeshVertexBufferLayoutRef},
mesh::{RenderMesh, MeshVertexBufferLayoutRef},
render_asset::RenderAssets,
render_phase::{
AddRenderCommand, DrawFunctions, PhaseItem, PhaseItemExtraIndex, RenderCommand,
Expand All @@ -25,6 +25,8 @@ use bevy::{
Render, RenderApp, RenderSet,
},
};
use bevy::render::mesh::allocator::MeshAllocator;
use bevy::render::mesh::RenderMeshBufferInfo;
use bytemuck::{Pod, Zeroable};
use rayon::prelude::*;

Expand Down Expand Up @@ -155,20 +157,19 @@ pub struct InstanceData {
fn queue_instanced(
transparent_3d_draw_functions: Res<DrawFunctions<Transparent3d>>,
custom_pipeline: Res<InstancedDataPipeline>,
msaa: Res<Msaa>,
mut pipelines: ResMut<SpecializedMeshPipelines<InstancedDataPipeline>>,
pipeline_cache: Res<PipelineCache>,
meshes: Res<RenderAssets<GpuMesh>>,
meshes: Res<RenderAssets<RenderMesh>>,
render_mesh_instances: Res<RenderMeshInstances>,
material_meshes: Query<Entity, With<InstanceMaterialData>>,
mut transparent_render_phases: ResMut<ViewSortedRenderPhases<Transparent3d>>,
mut views: Query<(Entity, &ExtractedView)>,
mut views: Query<(Entity, &ExtractedView, &Msaa)>,
) {
let draw_custom = transparent_3d_draw_functions.read().id::<DrawInstanced>();

let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples());

for (view_entity, view) in &mut views {
for (view_entity, view, msaa) in &mut views {
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples());
let Some(transparent_phase) = transparent_render_phases.get_mut(&view_entity) else {
continue;
};
Expand Down Expand Up @@ -282,7 +283,11 @@ type DrawInstanced = (
struct DrawMeshInstanced;

impl<P: PhaseItem> RenderCommand<P> for DrawMeshInstanced {
type Param = (SRes<RenderAssets<GpuMesh>>, SRes<RenderMeshInstances>);
type Param = (
SRes<RenderAssets<RenderMesh>>,
SRes<RenderMeshInstances>,
SRes<MeshAllocator>,
);
type ViewQuery = ();
type ItemQuery = Read<InstanceBuffer>;

Expand All @@ -291,33 +296,50 @@ impl<P: PhaseItem> RenderCommand<P> for DrawMeshInstanced {
item: &P,
_view: (),
instance_buffer: Option<&'w InstanceBuffer>,
(meshes, render_mesh_instances): SystemParamItem<'w, '_, Self::Param>,
(meshes, render_mesh_instances, mesh_allocator): SystemParamItem<'w, '_, Self::Param>,
pass: &mut TrackedRenderPass<'w>,
) -> RenderCommandResult {
// A borrow check workaround.
let mesh_allocator = mesh_allocator.into_inner();

let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(item.entity())
else {
return RenderCommandResult::Failure;
return RenderCommandResult::Skip;
};
let Some(gpu_mesh) = meshes.into_inner().get(mesh_instance.mesh_asset_id) else {
return RenderCommandResult::Failure;
return RenderCommandResult::Skip;
};
let Some(instance_buffer) = instance_buffer else {
return RenderCommandResult::Failure;
return RenderCommandResult::Skip;
};
let Some(vertex_buffer_slice) =
mesh_allocator.mesh_vertex_slice(&mesh_instance.mesh_asset_id)
else {
return RenderCommandResult::Skip;
};

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

match &gpu_mesh.buffer_info {
GpuBufferInfo::Indexed {
buffer,
RenderMeshBufferInfo::Indexed {
index_format,
count,
} => {
pass.set_index_buffer(buffer.slice(..), 0, *index_format);
pass.draw_indexed(0..*count, 0, 0..instance_buffer.length as u32);
let Some(index_buffer_slice) =
mesh_allocator.mesh_index_slice(&mesh_instance.mesh_asset_id)
else {
return RenderCommandResult::Skip;
};

pass.set_index_buffer(index_buffer_slice.buffer.slice(..), 0, *index_format);
pass.draw_indexed(
index_buffer_slice.range.start..(index_buffer_slice.range.start + count),
vertex_buffer_slice.range.start as i32,
0..instance_buffer.length as u32,
);
}
GpuBufferInfo::NonIndexed => {
RenderMeshBufferInfo::NonIndexed => {
pass.draw(0..gpu_mesh.vertex_count, 0..instance_buffer.length as u32);
}
}
Expand Down
40 changes: 16 additions & 24 deletions nannou/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,50 +481,42 @@ where
}
}

impl<M> Reflect for ModelHolder<M>
impl<M> PartialReflect for ModelHolder<M>
where
M: Reflect + DynamicTypePath + Any + GetTypeRegistration + 'static,
M: PartialReflect + DynamicTypePath + Any + GetTypeRegistration + 'static,
{
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
self.0.get_represented_type_info()
}

fn into_any(self: Box<Self>) -> Box<dyn Any> {
Box::new(self.0).into_any()
fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
Box::new(ModelHolder(self.0))
}

fn as_any(&self) -> &dyn Any {
self.0.as_any()
fn as_partial_reflect(&self) -> &dyn PartialReflect {
self.0.as_partial_reflect()
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self.0.as_any_mut()
fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
self.0.as_partial_reflect_mut()
}

fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
Box::new(self.0).into_reflect()
fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
Box::new(self.0).try_into_reflect()
}

fn as_reflect(&self) -> &dyn Reflect {
self.0.as_reflect()
fn try_as_reflect(&self) -> Option<&dyn Reflect> {
self.0.try_as_reflect()
}

fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self.0.as_reflect_mut()
fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
self.0.try_as_reflect_mut()
}

fn apply(&mut self, value: &dyn Reflect) {
self.0.apply(value)
}

fn try_apply(&mut self, value: &dyn Reflect) -> Result<(), ApplyError> {
fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
self.0.try_apply(value)
}

fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
self.0.set(value)
}

fn reflect_ref(&self) -> ReflectRef {
self.0.reflect_ref()
}
Expand All @@ -537,7 +529,7 @@ where
Box::new(self.0).reflect_owned()
}

fn clone_value(&self) -> Box<dyn Reflect> {
fn clone_value(&self) -> Box<dyn PartialReflect> {
self.0.clone_value()
}
}
Expand Down
33 changes: 17 additions & 16 deletions nannou/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use std::path::{Path, PathBuf};
use bevy::input::mouse::MouseWheel;
use bevy::prelude::*;
use bevy::render::camera::RenderTarget;
use bevy::render::view::screenshot::ScreenshotManager;
use bevy::render::view::cursor::CursorIcon;
use bevy::render::view::RenderLayers;
use bevy::window::{Cursor, CursorGrabMode, PrimaryWindow, WindowLevel, WindowMode, WindowRef};
use bevy::render::view::screenshot::{save_to_disk, Screenshot};
use bevy::window::{CursorGrabMode, PrimaryWindow, WindowLevel, WindowMode, WindowRef};

use bevy_nannou::prelude::render::NannouCamera;
use bevy_nannou::prelude::MonitorSelection;
Expand Down Expand Up @@ -768,7 +769,7 @@ impl<'a, 'w> Window<'a, 'w> {
/// to fullscreen.
pub fn set_fullscreen(&self, fullscreen: bool) {
if fullscreen {
self.window_mut().mode = WindowMode::BorderlessFullscreen;
self.window_mut().mode = WindowMode::BorderlessFullscreen(MonitorSelection::Current);
} else {
self.window_mut().mode = WindowMode::Windowed;
}
Expand Down Expand Up @@ -840,8 +841,11 @@ impl<'a, 'w> Window<'a, 'w> {
///
/// - **iOS:** Has no effect.
/// - **Android:** Has no effect.
pub fn set_cursor_icon(&self, cursor: Cursor) {
self.window_mut().cursor = cursor;
pub fn set_cursor_icon(&self, cursor: CursorIcon) {
self.app
.component_world_mut()
.entity_mut(self.entity)
.insert(cursor);
}

/// Changes the position of the cursor in logical window coordinates.
Expand All @@ -864,7 +868,7 @@ impl<'a, 'w> Window<'a, 'w> {
/// - **iOS:** Always returns an Err.
/// - **Web:** Has no effect.
pub fn set_cursor_grab(&self, grab: bool) {
self.window_mut().cursor.grab_mode = if grab {
self.window_mut().cursor_options.grab_mode = if grab {
CursorGrabMode::Locked
} else {
CursorGrabMode::None
Expand All @@ -885,12 +889,12 @@ impl<'a, 'w> Window<'a, 'w> {
///
/// This has no effect on **Android** or **iOS**.
pub fn set_cursor_visible(&self, visible: bool) {
self.window_mut().cursor.visible = visible;
self.window_mut().cursor_options.visible = visible;
}

/// Attempts to determine whether or not the window is currently fullscreen.
pub fn is_fullscreen(&self) -> bool {
self.window_mut().mode == WindowMode::Fullscreen
self.window_mut().mode == WindowMode::Fullscreen(MonitorSelection::Current)
}

/// The rectangle representing the position and dimensions of the window.
Expand All @@ -907,13 +911,10 @@ impl<'a, 'w> Window<'a, 'w> {
}

/// Saves a screenshot of the window to the given path.
pub fn save_screenshot<P: AsRef<Path>>(&mut self, path: P) {
let mut world = self.app.resource_world_mut();
let mut screenshot_manager = world
.get_resource_mut::<ScreenshotManager>()
.expect("ScreenshotManager resource not found");
screenshot_manager
.save_screenshot_to_disk(self.entity, path)
.expect("Failed to save screenshot");
pub fn save_screenshot<P: AsRef<Path> + 'static>(&mut self, path: P) {
let mut world = self.app.component_world_mut();
world.spawn(Screenshot::window(self.entity))
.observe(save_to_disk(path));

}
}
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.27.0", default-features = false, features = ["rand"] }
glam = { version = "0.28.0", 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

0 comments on commit 2055d1d

Please sign in to comment.