Skip to content

Commit

Permalink
use a smaller index to sort
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Jan 5, 2022
1 parent 7095163 commit d7a5c75
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
34 changes: 27 additions & 7 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ impl SpecializedPipeline for SpritePipeline {
}
}

pub struct ExtractedSpriteIndex {
pub handle: Handle<Image>,
pub w: f32,
}
pub struct ExtractedSprite {
pub transform: Mat4,
pub color: Color,
Expand All @@ -166,6 +170,7 @@ pub struct ExtractedSprite {
#[derive(Default)]
pub struct ExtractedSprites {
pub sprites: Vec<ExtractedSprite>,
pub indexes: Vec<ExtractedSpriteIndex>,
}

#[derive(Default)]
Expand Down Expand Up @@ -218,17 +223,19 @@ pub fn extract_sprites(
) {
let mut extracted_sprites = render_world.get_resource_mut::<ExtractedSprites>().unwrap();
extracted_sprites.sprites.clear();
extracted_sprites.indexes.clear();
for (computed_visibility, sprite, transform, handle) in sprite_query.iter() {
if !computed_visibility.is_visible {
continue;
}
if let Some(image) = images.get(handle) {
let size = image.texture_descriptor.size;

let transform = transform.compute_matrix();
extracted_sprites.sprites.push(ExtractedSprite {
atlas_size: None,
color: sprite.color,
transform: transform.compute_matrix(),
transform,
rect: Rect {
min: Vec2::ZERO,
max: sprite
Expand All @@ -239,6 +246,10 @@ pub fn extract_sprites(
flip_y: sprite.flip_y,
handle: handle.clone_weak(),
});
extracted_sprites.indexes.push(ExtractedSpriteIndex {
w: transform.w_axis[2],
handle: handle.clone_weak(),
});
};
}
for (computed_visibility, atlas_sprite, transform, texture_atlas_handle) in atlas_query.iter() {
Expand All @@ -248,15 +259,20 @@ pub fn extract_sprites(
if let Some(texture_atlas) = texture_atlases.get(texture_atlas_handle) {
if images.contains(&texture_atlas.texture) {
let rect = texture_atlas.textures[atlas_sprite.index as usize];
let transform = transform.compute_matrix();
extracted_sprites.sprites.push(ExtractedSprite {
atlas_size: Some(texture_atlas.size),
color: atlas_sprite.color,
transform: transform.compute_matrix(),
transform,
rect,
flip_x: atlas_sprite.flip_x,
flip_y: atlas_sprite.flip_y,
handle: texture_atlas.texture.clone_weak(),
});
extracted_sprites.indexes.push(ExtractedSpriteIndex {
w: transform.w_axis[2],
handle: texture_atlas.texture.clone_weak(),
});
}
}
}
Expand Down Expand Up @@ -322,12 +338,12 @@ pub fn prepare_sprites(

// sort first by z and then by handle. this ensures that, when possible, batches span multiple z layers
// batches won't span z-layers if there is another batch between them
extracted_sprites.sprites.sort_unstable_by(|a, b| {
match a.transform.w_axis[2].partial_cmp(&b.transform.w_axis[2]) {
extracted_sprites
.indexes
.sort_unstable_by(|a, b| match a.w.partial_cmp(&b.w) {
Some(Ordering::Equal) | None => a.handle.cmp(&b.handle),
Some(other) => other,
}
});
});

let mut start = 0;
let mut end = 0;
Expand All @@ -336,7 +352,11 @@ pub fn prepare_sprites(
let mut current_batch_handle: Option<Handle<Image>> = None;
let mut current_batch_colored = false;
let mut last_z = 0.0;
for extracted_sprite in extracted_sprites.sprites.iter() {
for (index, _) in extracted_sprites.indexes.iter().enumerate() {
let extracted_sprite = extracted_sprites
.sprites
.get(index)
.expect("an index was added without a matching sprite");
let colored = extracted_sprite.color != Color::WHITE;
if let Some(current_batch_handle) = &current_batch_handle {
if *current_batch_handle != extracted_sprite.handle || current_batch_colored != colored
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy_ecs::{
};
use bevy_math::{Mat4, Size, Vec3};
use bevy_render::{texture::Image, RenderWorld};
use bevy_sprite::{ExtractedSprite, ExtractedSprites, TextureAtlas};
use bevy_sprite::{ExtractedSprite, ExtractedSpriteIndex, ExtractedSprites, TextureAtlas};
use bevy_transform::prelude::{GlobalTransform, Transform};
use bevy_window::Windows;

Expand Down Expand Up @@ -92,11 +92,15 @@ pub fn extract_text2d_sprite(
transform,
color,
rect,
handle,
handle: handle.clone_weak(),
atlas_size,
flip_x: false,
flip_y: false,
});
extracted_sprites.indexes.push(ExtractedSpriteIndex {
w: transform.w_axis[2],
handle,
});
}
}
}
Expand Down

0 comments on commit d7a5c75

Please sign in to comment.