use crate::{
render_phase::TrackedRenderPass,
render_resource::{CachedRenderPipelineId, PipelineCache},
};
use bevy_app::App;
use bevy_ecs::{
all_tuples,
entity::Entity,
system::{
lifetimeless::SRes, ReadOnlySystemParamFetch, Resource, SystemParam, SystemParamItem,
SystemState,
},
world::World,
};
use bevy_utils::HashMap;
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{any::TypeId, fmt::Debug, hash::Hash, ops::Range};
pub trait Draw<P: PhaseItem>: Send + Sync + 'static {
fn draw<'w>(
&mut self,
world: &'w World,
pass: &mut TrackedRenderPass<'w>,
view: Entity,
item: &P,
);
}
pub trait PhaseItem: Sized + Send + Sync + 'static {
type SortKey: Ord;
fn sort_key(&self) -> Self::SortKey;
fn draw_function(&self) -> DrawFunctionId;
#[inline]
fn sort(items: &mut [Self]) {
items.sort_unstable_by_key(|item| item.sort_key());
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct DrawFunctionId(usize);
pub struct DrawFunctionsInternal<P: PhaseItem> {
pub draw_functions: Vec<Box<dyn Draw<P>>>,
pub indices: HashMap<TypeId, DrawFunctionId>,
}
impl<P: PhaseItem> DrawFunctionsInternal<P> {
pub fn add<T: Draw<P>>(&mut self, draw_function: T) -> DrawFunctionId {
self.add_with::<T, T>(draw_function)
}
pub fn add_with<T: 'static, D: Draw<P>>(&mut self, draw_function: D) -> DrawFunctionId {
self.draw_functions.push(Box::new(draw_function));
let id = DrawFunctionId(self.draw_functions.len() - 1);
self.indices.insert(TypeId::of::<T>(), id);
id
}
pub fn get_mut(&mut self, id: DrawFunctionId) -> Option<&mut dyn Draw<P>> {
self.draw_functions.get_mut(id.0).map(|f| &mut **f)
}
pub fn get_id<T: 'static>(&self) -> Option<DrawFunctionId> {
self.indices.get(&TypeId::of::<T>()).copied()
}
}
#[derive(Resource)]
pub struct DrawFunctions<P: PhaseItem> {
internal: RwLock<DrawFunctionsInternal<P>>,
}
impl<P: PhaseItem> Default for DrawFunctions<P> {
fn default() -> Self {
Self {
internal: RwLock::new(DrawFunctionsInternal {
draw_functions: Vec::new(),
indices: HashMap::default(),
}),
}
}
}
impl<P: PhaseItem> DrawFunctions<P> {
pub fn read(&self) -> RwLockReadGuard<'_, DrawFunctionsInternal<P>> {
self.internal.read()
}
pub fn write(&self) -> RwLockWriteGuard<'_, DrawFunctionsInternal<P>> {
self.internal.write()
}
}
pub trait RenderCommand<P: PhaseItem> {
type Param: SystemParam + 'static;
fn render<'w>(
view: Entity,
item: &P,
param: SystemParamItem<'w, '_, Self::Param>,
pass: &mut TrackedRenderPass<'w>,
) -> RenderCommandResult;
}
pub enum RenderCommandResult {
Success,
Failure,
}
pub trait EntityRenderCommand {
type Param: SystemParam + 'static;
fn render<'w>(
view: Entity,
item: Entity,
param: SystemParamItem<'w, '_, Self::Param>,
pass: &mut TrackedRenderPass<'w>,
) -> RenderCommandResult;
}
pub trait EntityPhaseItem: PhaseItem {
fn entity(&self) -> Entity;
}
pub trait CachedRenderPipelinePhaseItem: PhaseItem {
fn cached_pipeline(&self) -> CachedRenderPipelineId;
}
pub trait BatchedPhaseItem: EntityPhaseItem {
fn batch_range(&self) -> &Option<Range<u32>>;
fn batch_range_mut(&mut self) -> &mut Option<Range<u32>>;
#[inline]
fn add_to_batch(&mut self, other: &Self) -> BatchResult {
let self_entity = self.entity();
if let (Some(self_batch_range), Some(other_batch_range)) = (
self.batch_range_mut().as_mut(),
other.batch_range().as_ref(),
) {
if self_entity == other.entity() {
if self_batch_range.end == other_batch_range.start {
self_batch_range.end = other_batch_range.end;
return BatchResult::Success;
} else if self_batch_range.start == other_batch_range.end {
self_batch_range.start = other_batch_range.start;
return BatchResult::Success;
}
}
}
BatchResult::IncompatibleItems
}
}
pub enum BatchResult {
Success,
IncompatibleItems,
}
impl<P: EntityPhaseItem, E: EntityRenderCommand> RenderCommand<P> for E
where
E::Param: 'static,
{
type Param = E::Param;
#[inline]
fn render<'w>(
view: Entity,
item: &P,
param: SystemParamItem<'w, '_, Self::Param>,
pass: &mut TrackedRenderPass<'w>,
) -> RenderCommandResult {
<E as EntityRenderCommand>::render(view, item.entity(), param, pass)
}
}
pub struct SetItemPipeline;
impl<P: CachedRenderPipelinePhaseItem> RenderCommand<P> for SetItemPipeline {
type Param = SRes<PipelineCache>;
#[inline]
fn render<'w>(
_view: Entity,
item: &P,
pipeline_cache: SystemParamItem<'w, '_, Self::Param>,
pass: &mut TrackedRenderPass<'w>,
) -> RenderCommandResult {
if let Some(pipeline) = pipeline_cache
.into_inner()
.get_render_pipeline(item.cached_pipeline())
{
pass.set_render_pipeline(pipeline);
RenderCommandResult::Success
} else {
RenderCommandResult::Failure
}
}
}
macro_rules! render_command_tuple_impl {
($($name: ident),*) => {
impl<P: PhaseItem, $($name: RenderCommand<P>),*> RenderCommand<P> for ($($name,)*) {
type Param = ($($name::Param,)*);
#[allow(non_snake_case)]
fn render<'w>(
_view: Entity,
_item: &P,
($($name,)*): SystemParamItem<'w, '_, Self::Param>,
_pass: &mut TrackedRenderPass<'w>,
) -> RenderCommandResult{
$(if let RenderCommandResult::Failure = $name::render(_view, _item, $name, _pass) {
return RenderCommandResult::Failure;
})*
RenderCommandResult::Success
}
}
};
}
all_tuples!(render_command_tuple_impl, 0, 15, C);
pub struct RenderCommandState<P: PhaseItem + 'static, C: RenderCommand<P>> {
state: SystemState<C::Param>,
}
impl<P: PhaseItem, C: RenderCommand<P>> RenderCommandState<P, C> {
pub fn new(world: &mut World) -> Self {
Self {
state: SystemState::new(world),
}
}
}
impl<P: PhaseItem, C: RenderCommand<P> + Send + Sync + 'static> Draw<P> for RenderCommandState<P, C>
where
<C::Param as SystemParam>::Fetch: ReadOnlySystemParamFetch,
{
fn draw<'w>(
&mut self,
world: &'w World,
pass: &mut TrackedRenderPass<'w>,
view: Entity,
item: &P,
) {
let param = self.state.get(world);
C::render(view, item, param, pass);
}
}
pub trait AddRenderCommand {
fn add_render_command<P: PhaseItem, C: RenderCommand<P> + Send + Sync + 'static>(
&mut self,
) -> &mut Self
where
<C::Param as SystemParam>::Fetch: ReadOnlySystemParamFetch;
}
impl AddRenderCommand for App {
fn add_render_command<P: PhaseItem, C: RenderCommand<P> + Send + Sync + 'static>(
&mut self,
) -> &mut Self
where
<C::Param as SystemParam>::Fetch: ReadOnlySystemParamFetch,
{
let draw_function = RenderCommandState::<P, C>::new(&mut self.world);
let draw_functions = self
.world
.get_resource::<DrawFunctions<P>>()
.unwrap_or_else(|| {
panic!(
"DrawFunctions<{}> must be added to the world as a resource \
before adding render commands to it",
std::any::type_name::<P>(),
);
});
draw_functions.write().add_with::<C, _>(draw_function);
self
}
}