
This implements the following: * **Sprite Batching**: Collects sprites in a vertex buffer to draw many sprites with a single draw call. Sprites are batched by their `Handle<Image>` within a specific z-level. When possible, sprites are opportunistically batched _across_ z-levels (when no sprites with a different texture exist between two sprites with the same texture on different z levels). With these changes, I can now get ~130,000 sprites at 60fps on the `bevymark_pipelined` example. * **Sprite Color Tints**: The `Sprite` type now has a `color` field. Non-white color tints result in a specialized render pipeline that passes the color in as a vertex attribute. I chose to specialize this because passing vertex colors has a measurable price (without colors I get ~130,000 sprites on bevymark, with colors I get ~100,000 sprites). "Colored" sprites cannot be batched with "uncolored" sprites, but I think this is fine because the chance of a "colored" sprite needing to batch with other "colored" sprites is generally probably way higher than an "uncolored" sprite needing to batch with a "colored" sprite. * **Sprite Flipping**: Sprites can be flipped on their x or y axis using `Sprite::flip_x` and `Sprite::flip_y`. This is also true for `TextureAtlasSprite`. * **Simpler BufferVec/UniformVec/DynamicUniformVec Clearing**: improved the clearing interface by removing the need to know the size of the final buffer at the initial clear.  Note that this moves sprites away from entity-driven rendering and back to extracted lists. We _could_ use entities here, but it necessitates that an intermediate list is allocated / populated to collect and sort extracted sprites. This redundant copy, combined with the normal overhead of spawning extracted sprite entities, brings bevymark down to ~80,000 sprites at 60fps. I think making sprites a bit more fixed (by default) is worth it. I view this as acceptable because batching makes normal entity-driven rendering pretty useless anyway (and we would want to batch most custom materials too). We can still support custom shaders with custom bindings, we'll just need to define a specific interface for it.
162 lines
4.2 KiB
Rust
162 lines
4.2 KiB
Rust
use crate::{
|
|
render_resource::DynamicUniformVec,
|
|
renderer::{RenderDevice, RenderQueue},
|
|
RenderApp, RenderStage,
|
|
};
|
|
use bevy_app::{App, Plugin};
|
|
use bevy_asset::{Asset, Handle};
|
|
use bevy_ecs::{
|
|
component::Component,
|
|
prelude::*,
|
|
query::{FilterFetch, QueryItem, ReadOnlyFetch, WorldQuery},
|
|
system::{
|
|
lifetimeless::{Read, SCommands, SQuery},
|
|
RunSystem, SystemParamItem,
|
|
},
|
|
};
|
|
use crevice::std140::AsStd140;
|
|
use std::{marker::PhantomData, ops::Deref};
|
|
|
|
pub struct DynamicUniformIndex<C: Component> {
|
|
index: u32,
|
|
marker: PhantomData<C>,
|
|
}
|
|
|
|
impl<C: Component> DynamicUniformIndex<C> {
|
|
#[inline]
|
|
pub fn index(&self) -> u32 {
|
|
self.index
|
|
}
|
|
}
|
|
|
|
pub trait ExtractComponent: Component {
|
|
type Query: WorldQuery;
|
|
type Filter: WorldQuery;
|
|
fn extract_component(item: QueryItem<Self::Query>) -> Self;
|
|
}
|
|
|
|
/// Extracts assets into gpu-usable data
|
|
pub struct UniformComponentPlugin<C>(PhantomData<fn() -> C>);
|
|
|
|
impl<C> Default for UniformComponentPlugin<C> {
|
|
fn default() -> Self {
|
|
Self(PhantomData)
|
|
}
|
|
}
|
|
|
|
impl<C: Component + AsStd140 + Clone> Plugin for UniformComponentPlugin<C> {
|
|
fn build(&self, app: &mut App) {
|
|
app.sub_app(RenderApp)
|
|
.insert_resource(ComponentUniforms::<C>::default())
|
|
.add_system_to_stage(
|
|
RenderStage::Prepare,
|
|
prepare_uniform_components::<C>.system(),
|
|
);
|
|
}
|
|
}
|
|
|
|
pub struct ComponentUniforms<C: Component + AsStd140> {
|
|
uniforms: DynamicUniformVec<C>,
|
|
}
|
|
|
|
impl<C: Component + AsStd140> Deref for ComponentUniforms<C> {
|
|
type Target = DynamicUniformVec<C>;
|
|
|
|
#[inline]
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.uniforms
|
|
}
|
|
}
|
|
|
|
impl<C: Component + AsStd140> ComponentUniforms<C> {
|
|
#[inline]
|
|
pub fn uniforms(&self) -> &DynamicUniformVec<C> {
|
|
&self.uniforms
|
|
}
|
|
}
|
|
|
|
impl<C: Component + AsStd140> Default for ComponentUniforms<C> {
|
|
fn default() -> Self {
|
|
Self {
|
|
uniforms: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn prepare_uniform_components<C: Component>(
|
|
mut commands: Commands,
|
|
render_device: Res<RenderDevice>,
|
|
render_queue: Res<RenderQueue>,
|
|
mut component_uniforms: ResMut<ComponentUniforms<C>>,
|
|
components: Query<(Entity, &C)>,
|
|
) where
|
|
C: AsStd140 + Clone,
|
|
{
|
|
component_uniforms.uniforms.clear();
|
|
for (entity, component) in components.iter() {
|
|
commands
|
|
.get_or_spawn(entity)
|
|
.insert(DynamicUniformIndex::<C> {
|
|
index: component_uniforms.uniforms.push(component.clone()),
|
|
marker: PhantomData,
|
|
});
|
|
}
|
|
|
|
component_uniforms
|
|
.uniforms
|
|
.write_buffer(&render_device, &render_queue);
|
|
}
|
|
|
|
pub struct ExtractComponentPlugin<C, F = ()>(PhantomData<fn() -> (C, F)>);
|
|
|
|
impl<C, F> Default for ExtractComponentPlugin<C, F> {
|
|
fn default() -> Self {
|
|
Self(PhantomData)
|
|
}
|
|
}
|
|
|
|
impl<C: ExtractComponent> Plugin for ExtractComponentPlugin<C>
|
|
where
|
|
<C::Query as WorldQuery>::Fetch: ReadOnlyFetch,
|
|
<C::Filter as WorldQuery>::Fetch: FilterFetch,
|
|
{
|
|
fn build(&self, app: &mut App) {
|
|
let system = ExtractComponentSystem::<C>::system(&mut app.world);
|
|
let render_app = app.sub_app(RenderApp);
|
|
render_app.add_system_to_stage(RenderStage::Extract, system);
|
|
}
|
|
}
|
|
|
|
impl<T: Asset> ExtractComponent for Handle<T> {
|
|
type Query = Read<Handle<T>>;
|
|
type Filter = ();
|
|
|
|
#[inline]
|
|
fn extract_component(handle: QueryItem<Self::Query>) -> Self {
|
|
handle.clone_weak()
|
|
}
|
|
}
|
|
|
|
pub struct ExtractComponentSystem<C: ExtractComponent>(PhantomData<C>);
|
|
|
|
impl<C: ExtractComponent> RunSystem for ExtractComponentSystem<C>
|
|
where
|
|
<C::Filter as WorldQuery>::Fetch: FilterFetch,
|
|
<C::Query as WorldQuery>::Fetch: ReadOnlyFetch,
|
|
{
|
|
type Param = (
|
|
SCommands,
|
|
Local<'static, usize>,
|
|
SQuery<(Entity, C::Query), C::Filter>,
|
|
);
|
|
|
|
fn run((mut commands, mut previous_len, query): SystemParamItem<Self::Param>) {
|
|
let mut values = Vec::with_capacity(*previous_len);
|
|
for (entity, query_item) in query.iter() {
|
|
values.push((entity, (C::extract_component(query_item),)));
|
|
}
|
|
*previous_len = values.len();
|
|
commands.insert_or_spawn_batch(values);
|
|
}
|
|
}
|