Clear the path atlases on every frame and fix spelling

@as-cii you were right about the plural of "atlas" after all.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo 2021-03-30 16:56:11 -06:00
parent 8dabb15248
commit 3103d28285
3 changed files with 30 additions and 29 deletions

View File

@ -7,8 +7,8 @@ use objc::{msg_send, sel, sel_impl};
pub struct AtlasAllocator {
device: Device,
texture_descriptor: TextureDescriptor,
atlasses: Vec<Atlas>,
free_atlasses: Vec<Atlas>,
atlases: Vec<Atlas>,
free_atlases: Vec<Atlas>,
}
impl AtlasAllocator {
@ -16,11 +16,11 @@ impl AtlasAllocator {
let mut me = Self {
device,
texture_descriptor,
atlasses: Vec::new(),
free_atlasses: Vec::new(),
atlases: Vec::new(),
free_atlases: Vec::new(),
};
let atlas = me.new_atlas(Vector2I::zero());
me.atlasses.push(atlas);
me.atlases.push(atlas);
me
}
@ -33,36 +33,36 @@ impl AtlasAllocator {
pub fn allocate(&mut self, requested_size: Vector2I) -> anyhow::Result<(usize, Vector2I)> {
let origin = self
.atlasses
.atlases
.last_mut()
.unwrap()
.allocate(requested_size)
.unwrap_or_else(|| {
let mut atlas = self.new_atlas(requested_size);
let origin = atlas.allocate(requested_size).unwrap();
self.atlasses.push(atlas);
self.atlases.push(atlas);
origin
});
Ok((self.atlasses.len() - 1, origin))
Ok((self.atlases.len() - 1, origin))
}
pub fn clear(&mut self) {
for atlas in &mut self.atlasses {
for atlas in &mut self.atlases {
atlas.clear();
}
self.free_atlasses.extend(self.atlasses.drain(1..));
self.free_atlases.extend(self.atlases.drain(1..));
}
pub fn texture(&self, atlas_id: usize) -> Option<&metal::TextureRef> {
self.atlasses.get(atlas_id).map(|a| a.texture.as_ref())
self.atlases.get(atlas_id).map(|a| a.texture.as_ref())
}
fn new_atlas(&mut self, required_size: Vector2I) -> Atlas {
if let Some(i) = self.free_atlasses.iter().rposition(|atlas| {
if let Some(i) = self.free_atlases.iter().rposition(|atlas| {
atlas.size().x() >= required_size.x() && atlas.size().y() >= required_size.y()
}) {
self.free_atlasses.remove(i)
self.free_atlases.remove(i)
} else {
let size = self.default_atlas_size().max(required_size);
let texture = if size.x() as u64 > self.texture_descriptor.width()

View File

@ -22,7 +22,7 @@ const INSTANCE_BUFFER_SIZE: usize = 1024 * 1024; // This is an arbitrary decisio
pub struct Renderer {
device: metal::Device,
sprite_cache: SpriteCache,
path_atlasses: AtlasAllocator,
path_atlases: AtlasAllocator,
quad_pipeline_state: metal::RenderPipelineState,
shadow_pipeline_state: metal::RenderPipelineState,
sprite_pipeline_state: metal::RenderPipelineState,
@ -66,7 +66,7 @@ impl Renderer {
);
let sprite_cache = SpriteCache::new(device.clone(), vec2i(1024, 768), fonts);
let path_atlasses = build_path_atlas_allocator(pixel_format, &device);
let path_atlases = build_path_atlas_allocator(pixel_format, &device);
let quad_pipeline_state = build_pipeline_state(
&device,
&library,
@ -102,7 +102,7 @@ impl Renderer {
Ok(Self {
device,
sprite_cache,
path_atlasses,
path_atlases: path_atlases,
quad_pipeline_state,
shadow_pipeline_state,
sprite_pipeline_state,
@ -137,6 +137,7 @@ impl Renderer {
offset: &mut usize,
command_buffer: &metal::CommandBufferRef,
) -> Vec<PathSprite> {
self.path_atlases.clear();
let mut stencils = Vec::new();
let mut vertices = Vec::<shaders::GPUIPathVertex>::new();
let mut current_atlas_id = None;
@ -146,7 +147,7 @@ impl Renderer {
let origin = path.bounds.origin() * scene.scale_factor();
let size = (path.bounds.size() * scene.scale_factor()).ceil();
let (atlas_id, atlas_origin) =
self.path_atlasses.allocate(size.ceil().to_i32()).unwrap();
self.path_atlases.allocate(size.ceil().to_i32()).unwrap();
let atlas_origin = atlas_origin.to_f32();
stencils.push(PathSprite {
layer_id,
@ -210,7 +211,7 @@ impl Renderer {
.color_attachments()
.object_at(0)
.unwrap();
let texture = self.path_atlasses.texture(atlas_id).unwrap();
let texture = self.path_atlases.texture(atlas_id).unwrap();
color_attachment.set_texture(Some(texture));
color_attachment.set_load_action(metal::MTLLoadAction::Clear);
color_attachment.set_store_action(metal::MTLStoreAction::Store);
@ -618,7 +619,7 @@ impl Renderer {
*offset as u64,
);
let texture = self.path_atlasses.texture(atlas_id).unwrap();
let texture = self.path_atlases.texture(atlas_id).unwrap();
command_encoder.set_vertex_bytes(
shaders::GPUISpriteVertexInputIndex_GPUISpriteVertexInputIndexAtlasSize as u64,
mem::size_of::<shaders::vector_float2>() as u64,
@ -663,8 +664,8 @@ fn build_path_atlas_allocator(
path_stencil_descriptor
.set_usage(metal::MTLTextureUsage::RenderTarget | metal::MTLTextureUsage::ShaderRead);
path_stencil_descriptor.set_storage_mode(metal::MTLStorageMode::Private);
let path_atlasses = AtlasAllocator::new(device.clone(), path_stencil_descriptor);
path_atlasses
let path_atlases = AtlasAllocator::new(device.clone(), path_stencil_descriptor);
path_atlases
}
fn align_offset(offset: &mut usize) {

View File

@ -31,7 +31,7 @@ pub struct SpriteCache {
device: metal::Device,
atlas_size: Vector2I,
fonts: Arc<dyn platform::FontSystem>,
atlasses: Vec<Atlas>,
atlases: Vec<Atlas>,
glyphs: HashMap<GlyphDescriptor, Option<GlyphSprite>>,
}
@ -41,12 +41,12 @@ impl SpriteCache {
size: Vector2I,
fonts: Arc<dyn platform::FontSystem>,
) -> Self {
let atlasses = vec![Atlas::new(&device, size)];
let atlases = vec![Atlas::new(&device, size)];
Self {
device,
atlas_size: size,
fonts,
atlasses,
atlases,
glyphs: Default::default(),
}
}
@ -67,7 +67,7 @@ impl SpriteCache {
let target_position = target_position * scale_factor;
let fonts = &self.fonts;
let atlasses = &mut self.atlasses;
let atlases = &mut self.atlases;
let atlas_size = self.atlas_size;
let device = &self.device;
let subpixel_variant = (
@ -98,19 +98,19 @@ impl SpriteCache {
assert!(glyph_bounds.width() < atlas_size.x());
assert!(glyph_bounds.height() < atlas_size.y());
let atlas_bounds = atlasses
let atlas_bounds = atlases
.last_mut()
.unwrap()
.try_insert(glyph_bounds.size(), &mask)
.unwrap_or_else(|| {
let mut atlas = Atlas::new(device, atlas_size);
let bounds = atlas.try_insert(glyph_bounds.size(), &mask).unwrap();
atlasses.push(atlas);
atlases.push(atlas);
bounds
});
Some(GlyphSprite {
atlas_id: atlasses.len() - 1,
atlas_id: atlases.len() - 1,
atlas_origin: atlas_bounds.origin(),
offset: glyph_bounds.origin(),
size: glyph_bounds.size(),
@ -120,7 +120,7 @@ impl SpriteCache {
}
pub fn atlas_texture(&self, atlas_id: usize) -> Option<&metal::TextureRef> {
self.atlasses.get(atlas_id).map(|a| a.texture.as_ref())
self.atlases.get(atlas_id).map(|a| a.texture.as_ref())
}
}