Skip to content

Commit

Permalink
Implement growing logic for TextAtlas
Browse files Browse the repository at this point in the history
The `TextAtlas` will have an initial size of `256` (we could make this
configurable) and `try_allocate` will keep track of the glyphs in use
in the current frame, returning `None` when the LRU glyph is in use.

In that case, `TextRenderer::prepare` will return
`PrepareError::AtlasFull` with the `ContentType` of the atlas that is
full. The user of the library can then call `TextAtlas::grow` with the
provided `ContentType` to obtain a bigger atlas (by `256`).

A `TextAtlas::grow` call clears the whole atlas and, as a result, all of
the `prepare` calls need to be repeated in a frame until they all
succeed. Overall, the atlas will rarely need to grow and so the calls
will not need to be repated often.

Finally, the user needs to call `TextAtlas::trim` at the end of the
frame. This allows us to clear the glyphs in use collection in the atlas. Maybe
there is a better way to model this in an API that forces the user to
trim the atlas (e.g. make `trim` return a new type and changing `prepare` and `render` to take that type instead).
  • Loading branch information
hecrj committed Jun 15, 2023
1 parent 80e8465 commit b41b75b
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 59 deletions.
2 changes: 2 additions & 0 deletions examples/hello-world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ async fn run() {

queue.submit(Some(encoder.finish()));
frame.present();

atlas.trim();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
3 changes: 2 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ContentType;
use std::{
error::Error,
fmt::{self, Display, Formatter},
Expand All @@ -6,7 +7,7 @@ use std::{
/// An error that occurred while preparing text for rendering.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PrepareError {
AtlasFull,
AtlasFull(ContentType),
}

impl Display for PrepareError {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod text_render;

pub use error::{PrepareError, RenderError};
pub use text_atlas::TextAtlas;
use text_render::ContentType;
pub use text_render::ContentType;
pub use text_render::TextRenderer;

// Re-export all top-level types from `cosmic-text` for convenience.
Expand Down
160 changes: 137 additions & 23 deletions src/text_atlas.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
use crate::{text_render::ContentType, CacheKey, GlyphDetails, GlyphToRender, Params, Resolution};
use etagere::{size2, Allocation, BucketedAtlasAllocator};
use lru::LruCache;
use std::{borrow::Cow, mem::size_of, num::NonZeroU64, sync::Arc};
use std::{borrow::Cow, collections::HashSet, mem::size_of, num::NonZeroU64, sync::Arc};
use wgpu::{
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayoutEntry, BindingResource,
BindingType, BlendState, Buffer, BufferBindingType, BufferDescriptor, BufferUsages,
ColorTargetState, ColorWrites, DepthStencilState, Device, Extent3d, FilterMode, FragmentState,
MultisampleState, PipelineLayout, PipelineLayoutDescriptor, PrimitiveState, Queue,
RenderPipeline, RenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor, ShaderModule,
ShaderModuleDescriptor, ShaderSource, ShaderStages, Texture, TextureDescriptor,
TextureDimension, TextureFormat, TextureSampleType, TextureUsages, TextureView,
TextureViewDescriptor, TextureViewDimension, VertexFormat, VertexState,
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutEntry,
BindingResource, BindingType, BlendState, Buffer, BufferBindingType, BufferDescriptor,
BufferUsages, ColorTargetState, ColorWrites, DepthStencilState, Device, Extent3d, FilterMode,
FragmentState, MultisampleState, PipelineLayout, PipelineLayoutDescriptor, PrimitiveState,
Queue, RenderPipeline, RenderPipelineDescriptor, Sampler, SamplerBindingType,
SamplerDescriptor, ShaderModule, ShaderModuleDescriptor, ShaderSource, ShaderStages, Texture,
TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType, TextureUsages,
TextureView, TextureViewDescriptor, TextureViewDimension, VertexFormat, VertexState,
};

#[allow(dead_code)]
pub(crate) struct InnerAtlas {
pub texture: Texture,
pub texture_view: TextureView,
pub packer: BucketedAtlasAllocator,
pub width: u32,
pub height: u32,
pub size: u32,
pub glyph_cache: LruCache<CacheKey, GlyphDetails>,
pub glyphs_in_use: HashSet<CacheKey>,
pub num_atlas_channels: usize,
pub max_texture_dimension_2d: u32,
}

impl InnerAtlas {
const INITIAL_SIZE: u32 = 256;

fn new(device: &Device, _queue: &Queue, num_atlas_channels: usize) -> Self {
let max_texture_dimension_2d = device.limits().max_texture_dimension_2d;
let width = max_texture_dimension_2d;
let height = max_texture_dimension_2d;
let size = Self::INITIAL_SIZE.min(max_texture_dimension_2d);

let packer = BucketedAtlasAllocator::new(size2(width as i32, height as i32));
let packer = BucketedAtlasAllocator::new(size2(size as i32, size as i32));

// Create a texture to use for our atlas
let texture = device.create_texture(&TextureDescriptor {
label: Some("glyphon atlas"),
size: Extent3d {
width,
height,
width: size,
height: size,
depth_or_array_layers: 1,
},
mip_level_count: 1,
Expand All @@ -55,15 +57,17 @@ impl InnerAtlas {
let texture_view = texture.create_view(&TextureViewDescriptor::default());

let glyph_cache = LruCache::unbounded();
let glyphs_in_use = HashSet::new();

Self {
texture,
texture_view,
packer,
width,
height,
size,
glyph_cache,
glyphs_in_use,
num_atlas_channels,
max_texture_dimension_2d,
}
}

Expand All @@ -72,15 +76,81 @@ impl InnerAtlas {

loop {
let allocation = self.packer.allocate(size);

if allocation.is_some() {
return allocation;
}

// Try to free least recently used allocation
let (_, value) = self.glyph_cache.pop_lru()?;
self.packer
.deallocate(value.atlas_id.expect("cache corrupt"));
let (_, mut value) = self.glyph_cache.peek_lru()?;

while value.atlas_id.is_none() {
let _ = self.glyph_cache.pop_lru();

(_, value) = self.glyph_cache.peek_lru()?;
}

let (key, value) = self.glyph_cache.pop_lru().unwrap();

if self.glyphs_in_use.contains(&key) {
return None;
}

self.packer.deallocate(value.atlas_id.unwrap());
}
}

pub(crate) fn promote(&mut self, glyph: CacheKey) {
self.glyph_cache.promote(&glyph);
self.glyphs_in_use.insert(glyph);
}

pub(crate) fn put(&mut self, glyph: CacheKey, details: GlyphDetails) {
self.glyph_cache.put(glyph, details);
self.glyphs_in_use.insert(glyph);
}

pub(crate) fn grow(&mut self, device: &wgpu::Device) -> bool {
if self.size >= self.max_texture_dimension_2d {
return false;
}

// TODO: Better resizing logic (?)
let new_size = (self.size + Self::INITIAL_SIZE).min(self.max_texture_dimension_2d);

self.packer = BucketedAtlasAllocator::new(size2(new_size as i32, new_size as i32));

// Create a texture to use for our atlas
self.texture = device.create_texture(&TextureDescriptor {
label: Some("glyphon atlas"),
size: Extent3d {
width: new_size,
height: new_size,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: match self.num_atlas_channels {
1 => TextureFormat::R8Unorm,
4 => TextureFormat::Rgba8UnormSrgb,
_ => panic!("unexpected number of channels"),
},
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
view_formats: &[],
});

self.texture_view = self.texture.create_view(&TextureViewDescriptor::default());
self.size = new_size;

self.glyph_cache.clear();
self.glyphs_in_use.clear();

true
}

fn trim(&mut self) {
self.glyphs_in_use.clear();
}
}

Expand All @@ -94,6 +164,8 @@ pub struct TextAtlas {
Arc<RenderPipeline>,
)>,
pub(crate) bind_group: Arc<BindGroup>,
pub(crate) bind_group_layout: BindGroupLayout,
pub(crate) sampler: Sampler,
pub(crate) color_atlas: InnerAtlas,
pub(crate) mask_atlas: InnerAtlas,
pub(crate) pipeline_layout: PipelineLayout,
Expand Down Expand Up @@ -252,6 +324,8 @@ impl TextAtlas {
params_buffer,
cached_pipelines: Vec::new(),
bind_group,
bind_group_layout,
sampler,
color_atlas,
mask_atlas,
pipeline_layout,
Expand All @@ -261,8 +335,23 @@ impl TextAtlas {
}
}

pub(crate) fn contains_cached_glyph(&self, glyph: &CacheKey) -> bool {
self.mask_atlas.glyph_cache.contains(glyph) || self.color_atlas.glyph_cache.contains(glyph)
pub fn trim(&mut self) {
self.mask_atlas.trim();
self.color_atlas.trim();
}

pub fn grow(&mut self, device: &wgpu::Device, content_type: ContentType) -> bool {
let did_grow = match content_type {
ContentType::Mask => self.mask_atlas.grow(device),
ContentType::Color => self.color_atlas.grow(device),
};

if did_grow {
self.rebind(device);
true
} else {
false
}
}

pub(crate) fn glyph(&self, glyph: &CacheKey) -> Option<&GlyphDetails> {
Expand Down Expand Up @@ -318,4 +407,29 @@ impl TextAtlas {
pipeline
})
}

fn rebind(&mut self, device: &wgpu::Device) {
self.bind_group = Arc::new(device.create_bind_group(&BindGroupDescriptor {
layout: &self.bind_group_layout,
entries: &[
BindGroupEntry {
binding: 0,
resource: self.params_buffer.as_entire_binding(),
},
BindGroupEntry {
binding: 1,
resource: BindingResource::TextureView(&self.color_atlas.texture_view),
},
BindGroupEntry {
binding: 2,
resource: BindingResource::TextureView(&self.mask_atlas.texture_view),
},
BindGroupEntry {
binding: 3,
resource: BindingResource::Sampler(&self.sampler),
},
],
label: Some("glyphon bind group"),
}));
}
}
55 changes: 21 additions & 34 deletions src/text_render.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
CacheKey, FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, Params, PrepareError,
RenderError, Resolution, SwashCache, SwashContent, TextArea, TextAtlas,
FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, Params, PrepareError, RenderError,
Resolution, SwashCache, SwashContent, TextArea, TextAtlas,
};
use std::{collections::HashSet, iter, mem::size_of, slice, sync::Arc};
use std::{iter, mem::size_of, slice, sync::Arc};
use wgpu::{
Buffer, BufferDescriptor, BufferUsages, DepthStencilState, Device, Extent3d, ImageCopyTexture,
ImageDataLayout, IndexFormat, MultisampleState, Origin3d, Queue, RenderPass, RenderPipeline,
Expand All @@ -16,7 +16,6 @@ pub struct TextRenderer {
index_buffer: Buffer,
index_buffer_size: u64,
vertices_to_render: u32,
glyphs_in_use: HashSet<CacheKey>,
screen_resolution: Resolution,
pipeline: Arc<RenderPipeline>,
}
Expand Down Expand Up @@ -53,7 +52,6 @@ impl TextRenderer {
index_buffer,
index_buffer_size,
vertices_to_render: 0,
glyphs_in_use: HashSet::new(),
screen_resolution: Resolution {
width: 0,
height: 0,
Expand Down Expand Up @@ -88,20 +86,16 @@ impl TextRenderer {
});
}

self.glyphs_in_use.clear();

for text_area in text_areas.iter() {
for run in text_area.buffer.layout_runs() {
for glyph in run.glyphs.iter() {
self.glyphs_in_use.insert(glyph.cache_key);

if atlas.mask_atlas.glyph_cache.contains(&glyph.cache_key) {
atlas.mask_atlas.glyph_cache.promote(&glyph.cache_key);
atlas.mask_atlas.promote(glyph.cache_key);
continue;
}

if atlas.color_atlas.glyph_cache.contains(&glyph.cache_key) {
atlas.color_atlas.glyph_cache.promote(&glyph.cache_key);
atlas.color_atlas.promote(glyph.cache_key);
continue;
}

Expand Down Expand Up @@ -129,7 +123,9 @@ impl TextRenderer {
// Find a position in the packer
let allocation = match inner.try_allocate(width, height) {
Some(a) => a,
None => return Err(PrepareError::AtlasFull),
None => {
return Err(PrepareError::AtlasFull(content_type));
}
};
let atlas_min = allocation.rectangle.min;

Expand Down Expand Up @@ -171,19 +167,17 @@ impl TextRenderer {
(GpuCacheStatus::SkipRasterization, None, inner)
};

if !inner.glyph_cache.contains(&glyph.cache_key) {
inner.glyph_cache.put(
glyph.cache_key,
GlyphDetails {
width: width as u16,
height: height as u16,
gpu_cache,
atlas_id,
top: image.placement.top as i16,
left: image.placement.left as i16,
},
);
}
inner.put(
glyph.cache_key,
GlyphDetails {
width: width as u16,
height: height as u16,
gpu_cache,
atlas_id,
top: image.placement.top as i16,
left: image.placement.left as i16,
},
);
}
}
}
Expand Down Expand Up @@ -383,13 +377,6 @@ impl TextRenderer {
}

{
// Validate that glyphs haven't been evicted from cache since `prepare`
for glyph in self.glyphs_in_use.iter() {
if !atlas.contains_cached_glyph(glyph) {
return Err(RenderError::RemovedFromAtlas);
}
}

// Validate that screen resolution hasn't changed since `prepare`
if self.screen_resolution != atlas.params.screen_resolution {
return Err(RenderError::ScreenResolutionChanged);
Expand All @@ -407,8 +394,8 @@ impl TextRenderer {
}

#[repr(u32)]
#[derive(Clone, Copy, Eq, PartialEq)]
pub(crate) enum ContentType {
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ContentType {
Color = 0,
Mask = 1,
}
Expand Down

0 comments on commit b41b75b

Please sign in to comment.