Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/bevyengine/bevy into frustu…
Browse files Browse the repository at this point in the history
…m_culling
  • Loading branch information
Byteron committed Mar 21, 2021
2 parents 549b5f6 + 45b2db7 commit ffcaef3
Show file tree
Hide file tree
Showing 125 changed files with 4,227 additions and 1,897 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ path = "examples/3d/orthographic.rs"
name = "parenting"
path = "examples/3d/parenting.rs"

[[example]]
name = "pbr"
path = "examples/3d/pbr.rs"

[[example]]
name = "spawner"
path = "examples/3d/spawner.rs"
Expand Down Expand Up @@ -302,6 +306,10 @@ path = "examples/input/gamepad_input_events.rs"
name = "keyboard_input"
path = "examples/input/keyboard_input.rs"

[[example]]
name = "keyboard_modifiers"
path = "examples/input/keyboard_modifiers.rs"

[[example]]
name = "keyboard_input_events"
path = "examples/input/keyboard_input_events.rs"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ Before contributing or participating in discussions with the community, you shou
We recommend checking out [The Bevy Book](https://bevyengine.org/learn/book/introduction) for a full tutorial.

Follow the [Setup guide](https://bevyengine.org/learn/book/getting-started/setup/) to ensure your development environment is set up correctly.
Once set up, you can quickly try out the [examples](/examples) by cloning this repo and running the following command:
Once set up, you can quickly try out the [examples](https://github.com/bevyengine/bevy/tree/latest/examples) by cloning this repo and running the following commands:

```sh
# Switch to the correct version (latest release, default is main development branch)
git checkout latest
# Runs the "breakout" example
cargo run --example breakout
```
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/hot.vert
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

layout(location = 0) in vec3 Vertex_Position;

layout(set = 0, binding = 0) uniform Camera {
layout(set = 0, binding = 0) uniform CameraViewProj {
mat4 ViewProj;
};

Expand Down
39 changes: 12 additions & 27 deletions crates/bevy_app/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
use bevy_ecs::{
component::Component,
schedule::{
RunOnce, Schedule, Stage, StageLabel, StateStage, SystemDescriptor, SystemSet, SystemStage,
RunOnce, Schedule, Stage, StageLabel, State, SystemDescriptor, SystemSet, SystemStage,
},
system::{IntoExclusiveSystem, IntoSystem},
world::{FromWorld, World},
Expand Down Expand Up @@ -177,37 +177,18 @@ impl AppBuilder {
self
}

pub fn on_state_enter<T: Clone + Component>(
&mut self,
stage: impl StageLabel,
state: T,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
self.stage(stage, |stage: &mut StateStage<T>| {
stage.on_state_enter(state, system)
})
pub fn add_state<T: Component + Clone + Eq>(&mut self, initial: T) -> &mut Self {
self.insert_resource(State::new(initial))
.add_system_set(State::<T>::make_driver())
}

pub fn on_state_update<T: Clone + Component>(
pub fn add_state_to_stage<T: Component + Clone + Eq>(
&mut self,
stage: impl StageLabel,
state: T,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
self.stage(stage, |stage: &mut StateStage<T>| {
stage.on_state_update(state, system)
})
}

pub fn on_state_exit<T: Clone + Component>(
&mut self,
stage: impl StageLabel,
state: T,
system: impl Into<SystemDescriptor>,
initial: T,
) -> &mut Self {
self.stage(stage, |stage: &mut StateStage<T>| {
stage.on_state_exit(state, system)
})
self.insert_resource(State::new(initial))
.add_system_set_to_stage(stage, State::<T>::make_driver())
}

pub fn add_default_stages(&mut self) -> &mut Self {
Expand All @@ -226,6 +207,10 @@ impl AppBuilder {
.add_stage(CoreStage::Last, SystemStage::parallel())
}

/// Setup the application to manage events of type `T`.
///
/// This is done by adding a `Resource` of type `Events::<T>`,
/// and inserting a `Events::<T>::update_system` system into `CoreStage::First`.
pub fn add_event<T>(&mut self) -> &mut Self
where
T: Component,
Expand Down
26 changes: 20 additions & 6 deletions crates/bevy_app/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,23 @@ enum State {
}

/// An event collection that represents the events that occurred within the last two
/// [Events::update] calls. Events can be cheaply read using an [EventReader]. This collection is
/// meant to be paired with a system that calls [Events::update] exactly once per update/frame.
/// [Events::update_system] is a system that does this. [EventReader]s are expected to read events
/// from this collection at least once per update/frame. If events are not handled within one
/// frame/update, they will be dropped.
/// [`Events::update`] calls.
/// Events can be written to using an [`EventWriter`]
/// and are typically cheaply read using an [`EventReader`].
///
/// Each event can be consumed by multiple systems, in parallel,
/// with consumption tracked by the [`EventReader`] on a per-system basis.
///
/// This collection is meant to be paired with a system that calls
/// [`Events::update`] exactly once per update/frame.
///
/// [`Events::update_system`] is a system that does this, typically intialized automatically using
/// [`AppBuilder::add_event`]. [EventReader]s are expected to read events from this collection at
/// least once per loop/frame.
/// Events will persist across a single frame boundary and so ordering of event producers and
/// consumers is not critical (although poorly-planned ordering may cause accumulating lag).
/// If events are not handled by the end of the frame after they are updated, they will be
/// dropped silently.
///
/// # Example
/// ```
Expand Down Expand Up @@ -100,7 +112,9 @@ enum State {
/// The buffers in [Events] will grow indefinitely if [Events::update] is never called.
///
/// An alternative call pattern would be to call [Events::update] manually across frames to control
/// when events are cleared. However this complicates consumption
/// when events are cleared.
/// This complicates consumption and risks ever-expanding memory usage if not cleaned up,
/// but can be done by adding your event as a resource instead of using [`AppBuilder::add_event`].
#[derive(Debug)]
pub struct Events<T> {
events_a: Vec<EventInstance<T>>,
Expand Down
10 changes: 8 additions & 2 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,13 @@ impl AssetServer {
AssetPath::new_ref(&load_context.path, label.as_ref().map(|l| l.as_str()));
asset_lifecycle.create_asset(asset_path.into(), asset_value, load_context.version);
} else {
panic!("Failed to find AssetLifecycle for label {:?}, which has an asset type {:?}. Are you sure that is a registered asset type?", label, asset_value.type_uuid());
panic!(
"Failed to find AssetLifecycle for label '{:?}', which has an asset type {} (UUID {:?}). \
Are you sure this asset type has been added to your app builder?",
label,
asset_value.type_name(),
asset_value.type_uuid(),
);
}
}
}
Expand Down Expand Up @@ -465,7 +471,7 @@ impl AssetServer {
}
}

assets.set(result.id, result.asset);
let _ = assets.set(result.id, result.asset);
}
Ok(AssetLifecycleEvent::Free(handle_id)) => {
if let HandleId::AssetPathId(id) = handle_id {
Expand Down
12 changes: 2 additions & 10 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,10 @@ impl<T: Asset> Assets<T> {
self.get_handle(id)
}

#[must_use = "not using the returned strong handle may result in the unexpected release of the asset"]
pub fn set<H: Into<HandleId>>(&mut self, handle: H, asset: T) -> Handle<T> {
let id: HandleId = handle.into();
if self.assets.insert(id, asset).is_some() {
self.events.send(AssetEvent::Modified {
handle: Handle::weak(id),
});
} else {
self.events.send(AssetEvent::Created {
handle: Handle::weak(id),
});
}

self.set_untracked(id, asset);
self.get_handle(id)
}

Expand Down
9 changes: 3 additions & 6 deletions crates/bevy_core/src/task_pool_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl TaskPoolThreadAssignmentPolicy {
// Clamp by min_threads, max_threads. (This may result in us using more threads than are
// available, this is intended. An example case where this might happen is a device with
// <= 2 threads.
bevy_math::clamp(desired, self.min_threads, self.max_threads)
desired.clamp(self.min_threads, self.max_threads)
}
}

Expand Down Expand Up @@ -94,11 +94,8 @@ impl DefaultTaskPoolOptions {

/// Inserts the default thread pools into the given resource map based on the configured values
pub fn create_default_pools(&self, world: &mut World) {
let total_threads = bevy_math::clamp(
bevy_tasks::logical_core_count(),
self.min_total_threads,
self.max_total_threads,
);
let total_threads =
bevy_tasks::logical_core_count().clamp(self.min_total_threads, self.max_total_threads);
trace!("Assigning {} cores to default task pools", total_threads);

let mut remaining_threads = total_threads;
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_core/src/time/fixed_timestep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,8 @@ impl System for FixedTimestep {
);
}
}

fn check_change_tick(&mut self, change_tick: u32) {
self.internal_system.check_change_tick(change_tick);
}
}
8 changes: 5 additions & 3 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,12 @@ pub fn impl_query_set(_input: TokenStream) -> TokenStream {
#[inline]
unsafe fn get_param(
state: &'a mut Self,
_system_state: &'a SystemState,
system_state: &'a SystemState,
world: &'a World,
change_tick: u32,
) -> Self::Item {
let (#(#query,)*) = &state.0;
QuerySet((#(Query::new(world, #query),)*))
QuerySet((#(Query::new(world, #query, system_state.last_change_tick, change_tick),)*))
}
}

Expand Down Expand Up @@ -402,9 +403,10 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
state: &'a mut Self,
system_state: &'a #path::system::SystemState,
world: &'a #path::world::World,
change_tick: u32,
) -> Self::Item {
#struct_name {
#(#fields: <<#field_types as SystemParam>::Fetch as #path::system::SystemParamFetch>::get_param(&mut state.state.#field_indices, system_state, world),)*
#(#fields: <<#field_types as SystemParam>::Fetch as #path::system::SystemParamFetch>::get_param(&mut state.state.#field_indices, system_state, world, change_tick),)*
#(#ignored_fields: <#ignored_field_types>::default(),)*
}
}
Expand Down
52 changes: 31 additions & 21 deletions crates/bevy_ecs/src/archetype.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use crate::{
bundle::BundleId,
component::{ComponentFlags, ComponentId, StorageType},
component::{ComponentId, StorageType},
entity::{Entity, EntityLocation},
storage::{Column, SparseArray, SparseSet, SparseSetIndex, TableId},
};
use std::{borrow::Cow, collections::HashMap, hash::Hash};
use std::{
borrow::Cow,
collections::HashMap,
hash::Hash,
ops::{Index, IndexMut},
};

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ArchetypeId(usize);
Expand All @@ -31,9 +36,14 @@ impl ArchetypeId {
}
}

pub enum ComponentStatus {
Added,
Mutated,
}

pub struct FromBundle {
pub archetype_id: ArchetypeId,
pub bundle_flags: Vec<ComponentFlags>,
pub bundle_status: Vec<ComponentStatus>,
}

#[derive(Default)]
Expand Down Expand Up @@ -65,13 +75,13 @@ impl Edges {
&mut self,
bundle_id: BundleId,
archetype_id: ArchetypeId,
bundle_flags: Vec<ComponentFlags>,
bundle_status: Vec<ComponentStatus>,
) {
self.from_bundle.insert(
bundle_id,
FromBundle {
archetype_id,
bundle_flags,
bundle_status,
},
);
}
Expand Down Expand Up @@ -443,27 +453,11 @@ impl Archetypes {
self.archetypes.get(id.index())
}

/// # Safety
/// `id` must be valid
#[inline]
pub unsafe fn get_unchecked(&self, id: ArchetypeId) -> &Archetype {
debug_assert!(id.index() < self.archetypes.len());
self.archetypes.get_unchecked(id.index())
}

#[inline]
pub fn get_mut(&mut self, id: ArchetypeId) -> Option<&mut Archetype> {
self.archetypes.get_mut(id.index())
}

/// # Safety
/// `id` must be valid
#[inline]
pub unsafe fn get_unchecked_mut(&mut self, id: ArchetypeId) -> &mut Archetype {
debug_assert!(id.index() < self.archetypes.len());
self.archetypes.get_unchecked_mut(id.index())
}

#[inline]
pub fn iter(&self) -> impl Iterator<Item = &Archetype> {
self.archetypes.iter()
Expand Down Expand Up @@ -522,3 +516,19 @@ impl Archetypes {
self.archetype_component_count
}
}

impl Index<ArchetypeId> for Archetypes {
type Output = Archetype;

#[inline]
fn index(&self, index: ArchetypeId) -> &Self::Output {
&self.archetypes[index.index()]
}
}

impl IndexMut<ArchetypeId> for Archetypes {
#[inline]
fn index_mut(&mut self, index: ArchetypeId) -> &mut Self::Output {
&mut self.archetypes[index.index()]
}
}
Loading

0 comments on commit ffcaef3

Please sign in to comment.