use crate::instance::push_instance;
use crate::plan::{Batch, GlyphQuad, Plan, Resolved};
use crate::view::pixel_zoom;
use crate::{Camera2D, FrameStats, SpriteDraw};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum Pipe {
Sprite,
Text,
}
#[derive(Debug)]
pub struct SpriteBatcher<K> {
plan: Plan<(K, Pipe)>,
packed: Vec<f32>,
}
impl<K: Copy + Eq> SpriteBatcher<K> {
pub fn new() -> Self {
Self {
plan: Plan::new(),
packed: Vec::new(),
}
}
pub fn build(
&mut self,
sprites: &[SpriteDraw],
camera: &Camera2D,
texture: impl FnMut(&SpriteDraw) -> Option<(K, bool)>,
) {
self.build_with(sprites, &[], camera, texture);
}
pub(crate) fn build_with(
&mut self,
sprites: &[SpriteDraw],
glyphs: &[GlyphQuad<(K, Pipe)>],
camera: &Camera2D,
mut texture: impl FnMut(&SpriteDraw) -> Option<(K, bool)>,
) {
self.plan
.build_with(sprites, glyphs, |sprite| match texture(sprite) {
None => Resolved::Skip,
Some((texture, false)) => Resolved::Draw((texture, Pipe::Sprite)),
Some((texture, true)) => Resolved::Placeholder((texture, Pipe::Sprite)),
});
let zoom = pixel_zoom(camera.zoom);
self.packed.clear();
for drawn in &self.plan.order {
let index = drawn.sprite as usize;
let sprite = match sprites.get(index) {
Some(sprite) => sprite,
None => &glyphs[index - sprites.len()].sprite,
};
push_instance(&mut self.packed, sprite, drawn.placeholder, Some(zoom));
}
}
pub fn instances(&self) -> &[f32] {
&self.packed
}
pub fn stats(&self) -> FrameStats {
FrameStats {
sprites: self.plan.order.len() as u32 - self.plan.glyphs,
skipped: self.plan.skipped,
batches: self.plan.batches.len() as u32,
tiles: 0,
chunks: 0,
texts: 0,
glyphs: self.plan.glyphs,
skipped_texts: 0,
ui_sprites: 0,
ui_glyphs: 0,
ui_texts: 0,
ui_batches: 0,
}
}
pub(crate) fn batches(&self) -> &[Batch<(K, Pipe)>] {
&self.plan.batches
}
pub(crate) fn position(&self, layer: u8, depth: u32) -> u32 {
self.plan.position(layer, depth)
}
}
impl<K: Copy + Eq> Default for SpriteBatcher<K> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use nadir_assets::Handle;
use nadir_core::{GenIndex, Vec2};
use super::*;
use crate::SPRITE_INSTANCE_FLOATS;
fn sprite(texture: u32, y: f32) -> SpriteDraw {
let id = GenIndex::new(texture, std::num::NonZeroU32::MIN);
SpriteDraw::new(Handle::new(id), Vec2::new(10.0, y), Vec2::splat(8.0))
}
#[test]
fn it_skips_what_is_not_ready_and_packs_the_rest_in_draw_order() {
let sprites = [
sprite(0, 30.0),
sprite(1, 10.0),
sprite(2, 20.0),
sprite(0, 5.0),
];
let mut batcher = SpriteBatcher::new();
batcher.build(&sprites, &Camera2D::default(), |sprite| {
match sprite.texture.id().index() {
1 => None,
texture => Some((texture, texture == 2)),
}
});
assert_eq!(
batcher.stats(),
FrameStats {
sprites: 3,
skipped: 1,
batches: 3,
..FrameStats::default()
}
);
let order: Vec<u32> = batcher
.batches()
.iter()
.map(|batch| batch.texture.0)
.collect();
assert_eq!(order, [0, 2, 0], "by depth: y 5, then 20, then 30");
assert!(
batcher
.batches()
.iter()
.all(|batch| batch.texture.1 == Pipe::Sprite)
);
assert_eq!(batcher.instances().len(), 3 * SPRITE_INSTANCE_FLOATS);
batcher.build(&sprites[..1], &Camera2D::default(), |_| Some((0, false)));
assert_eq!(batcher.stats().sprites, 1);
assert_eq!(batcher.instances().len(), SPRITE_INSTANCE_FLOATS);
}
#[test]
fn glyphs_are_packed_after_sprites_at_a_tie_and_split_batches_by_program() {
let mut panel = sprite(0, 5.0);
panel.sort = crate::SortMode::Fixed(3);
let mut quad = panel;
quad.position = Vec2::new(40.0, 7.0);
quad.pivot = Vec2::ZERO;
let glyph = |pipe| GlyphQuad {
sprite: quad,
text: 0,
texture: (0, pipe),
placeholder: false,
};
let glyphs = [glyph(Pipe::Text), glyph(Pipe::Sprite)];
let mut batcher = SpriteBatcher::new();
batcher.build_with(&[panel], &glyphs, &Camera2D::default(), |_| {
Some((0, false))
});
let stats = batcher.stats();
assert_eq!((stats.sprites, stats.glyphs, stats.batches), (1, 2, 3));
let batches: Vec<(u32, Pipe)> = batcher.batches().iter().map(|b| b.texture).collect();
assert_eq!(
batches,
[(0, Pipe::Sprite), (0, Pipe::Text), (0, Pipe::Sprite)]
);
let second = &batcher.instances()[SPRITE_INSTANCE_FLOATS..];
assert_eq!(second[..2], [40.0, 7.0]);
}
}