Skip to content

Commit 65276a1

Browse files
committed
Grow internal packer and re-upload glyphs automatically
1 parent 3bf107b commit 65276a1

File tree

5 files changed

+86
-30
lines changed

5 files changed

+86
-30
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0 OR Zlib"
99

1010
[dependencies]
1111
wgpu = "0.16"
12-
etagere = "0.2.6"
12+
etagere = { git = "https://github.com/hecrj/etagere.git", rev = "4ee873d5c412d31237cd51efdcfdc7a0afd1953b" }
1313
cosmic-text = "0.8"
1414
lru = "0.9"
1515

src/error.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::ContentType;
21
use std::{
32
error::Error,
43
fmt::{self, Display, Formatter},
@@ -7,7 +6,7 @@ use std::{
76
/// An error that occurred while preparing text for rendering.
87
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
98
pub enum PrepareError {
10-
AtlasFull(ContentType),
9+
AtlasFull,
1110
}
1211

1312
impl Display for PrepareError {

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ mod text_render;
1010

1111
pub use error::{PrepareError, RenderError};
1212
pub use text_atlas::{ColorMode, TextAtlas};
13-
pub use text_render::ContentType;
1413
pub use text_render::TextRenderer;
1514

15+
use text_render::ContentType;
16+
1617
// Re-export all top-level types from `cosmic-text` for convenience.
1718
pub use cosmic_text::{
1819
self, fontdb, Action, Affinity, Attrs, AttrsList, AttrsOwned, Buffer, BufferLine, CacheKey,

src/text_atlas.rs

+70-21
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
use crate::{text_render::ContentType, CacheKey, GlyphDetails, GlyphToRender, Params, Resolution};
1+
use crate::{
2+
text_render::ContentType, CacheKey, FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus,
3+
Params, Resolution, SwashCache,
4+
};
25
use etagere::{size2, Allocation, BucketedAtlasAllocator};
36
use lru::LruCache;
47
use std::{borrow::Cow, collections::HashSet, mem::size_of, num::NonZeroU64, sync::Arc};
58
use wgpu::{
69
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutEntry,
710
BindingResource, BindingType, BlendState, Buffer, BufferBindingType, BufferDescriptor,
811
BufferUsages, ColorTargetState, ColorWrites, DepthStencilState, Device, Extent3d, FilterMode,
9-
FragmentState, MultisampleState, PipelineLayout, PipelineLayoutDescriptor, PrimitiveState,
10-
Queue, RenderPipeline, RenderPipelineDescriptor, Sampler, SamplerBindingType,
11-
SamplerDescriptor, ShaderModule, ShaderModuleDescriptor, ShaderSource, ShaderStages, Texture,
12-
TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType, TextureUsages,
13-
TextureView, TextureViewDescriptor, TextureViewDimension, VertexFormat, VertexState,
12+
FragmentState, ImageCopyTexture, ImageDataLayout, MultisampleState, Origin3d, PipelineLayout,
13+
PipelineLayoutDescriptor, PrimitiveState, Queue, RenderPipeline, RenderPipelineDescriptor,
14+
Sampler, SamplerBindingType, SamplerDescriptor, ShaderModule, ShaderModuleDescriptor,
15+
ShaderSource, ShaderStages, Texture, TextureAspect, TextureDescriptor, TextureDimension,
16+
TextureFormat, TextureSampleType, TextureUsages, TextureView, TextureViewDescriptor,
17+
TextureViewDimension, VertexFormat, VertexState,
1418
};
1519

1620
#[allow(dead_code)]
@@ -78,20 +82,19 @@ impl InnerAtlas {
7882
}
7983

8084
// Try to free least recently used allocation
81-
let (_, mut value) = self.glyph_cache.peek_lru()?;
85+
let (mut key, mut value) = self.glyph_cache.peek_lru()?;
8286

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

86-
(_, value) = self.glyph_cache.peek_lru()?;
90+
(key, value) = self.glyph_cache.peek_lru()?;
8791
}
8892

89-
let (key, value) = self.glyph_cache.pop_lru().unwrap();
90-
9193
if self.glyphs_in_use.contains(&key) {
9294
return None;
9395
}
9496

97+
let (_, value) = self.glyph_cache.pop_lru().unwrap();
9598
self.packer.deallocate(value.atlas_id.unwrap());
9699
}
97100
}
@@ -110,15 +113,21 @@ impl InnerAtlas {
110113
self.glyphs_in_use.insert(glyph);
111114
}
112115

113-
pub(crate) fn grow(&mut self, device: &wgpu::Device) -> bool {
116+
pub(crate) fn grow(
117+
&mut self,
118+
device: &wgpu::Device,
119+
queue: &wgpu::Queue,
120+
font_system: &mut FontSystem,
121+
cache: &mut SwashCache,
122+
) -> bool {
114123
if self.size >= self.max_texture_dimension_2d {
115124
return false;
116125
}
117126

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

121-
self.packer = BucketedAtlasAllocator::new(size2(new_size as i32, new_size as i32));
130+
self.packer.grow(size2(new_size as i32, new_size as i32));
122131

123132
// Create a texture to use for our atlas
124133
self.texture = device.create_texture(&TextureDescriptor {
@@ -136,12 +145,46 @@ impl InnerAtlas {
136145
view_formats: &[],
137146
});
138147

148+
// Re-upload glyphs
149+
for (&cache_key, glyph) in &self.glyph_cache {
150+
let (x, y) = match glyph.gpu_cache {
151+
GpuCacheStatus::InAtlas { x, y, .. } => (x, y),
152+
GpuCacheStatus::SkipRasterization => continue,
153+
};
154+
155+
let image = cache.get_image_uncached(font_system, cache_key).unwrap();
156+
157+
let width = image.placement.width as usize;
158+
let height = image.placement.height as usize;
159+
160+
queue.write_texture(
161+
ImageCopyTexture {
162+
texture: &self.texture,
163+
mip_level: 0,
164+
origin: Origin3d {
165+
x: x as u32,
166+
y: y as u32,
167+
z: 0,
168+
},
169+
aspect: TextureAspect::All,
170+
},
171+
&image.data,
172+
ImageDataLayout {
173+
offset: 0,
174+
bytes_per_row: Some(width as u32 * self.kind.num_channels() as u32),
175+
rows_per_image: None,
176+
},
177+
Extent3d {
178+
width: width as u32,
179+
height: height as u32,
180+
depth_or_array_layers: 1,
181+
},
182+
);
183+
}
184+
139185
self.texture_view = self.texture.create_view(&TextureViewDescriptor::default());
140186
self.size = new_size;
141187

142-
self.glyph_cache.clear();
143-
self.glyphs_in_use.clear();
144-
145188
true
146189
}
147190

@@ -406,18 +449,24 @@ impl TextAtlas {
406449
self.color_atlas.trim();
407450
}
408451

409-
pub fn grow(&mut self, device: &wgpu::Device, content_type: ContentType) -> bool {
452+
pub(crate) fn grow(
453+
&mut self,
454+
device: &wgpu::Device,
455+
queue: &wgpu::Queue,
456+
font_system: &mut FontSystem,
457+
cache: &mut SwashCache,
458+
content_type: ContentType,
459+
) -> bool {
410460
let did_grow = match content_type {
411-
ContentType::Mask => self.mask_atlas.grow(device),
412-
ContentType::Color => self.color_atlas.grow(device),
461+
ContentType::Mask => self.mask_atlas.grow(device, queue, font_system, cache),
462+
ContentType::Color => self.color_atlas.grow(device, queue, font_system, cache),
413463
};
414464

415465
if did_grow {
416466
self.rebind(device);
417-
true
418-
} else {
419-
false
420467
}
468+
469+
did_grow
421470
}
422471

423472
pub(crate) fn glyph(&self, glyph: &CacheKey) -> Option<&GlyphDetails> {

src/text_render.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,20 @@ impl TextRenderer {
118118
let should_rasterize = width > 0 && height > 0;
119119

120120
let (gpu_cache, atlas_id, inner) = if should_rasterize {
121-
let inner = atlas.inner_for_content_mut(content_type);
121+
let mut inner = atlas.inner_for_content_mut(content_type);
122122

123123
// Find a position in the packer
124-
let allocation = match inner.try_allocate(width, height) {
125-
Some(a) => a,
126-
None => {
127-
return Err(PrepareError::AtlasFull(content_type));
124+
let allocation = loop {
125+
match inner.try_allocate(width, height) {
126+
Some(a) => break a,
127+
None => {
128+
if !atlas.grow(device, queue, font_system, cache, content_type)
129+
{
130+
return Err(PrepareError::AtlasFull);
131+
}
132+
133+
inner = atlas.inner_for_content_mut(content_type);
134+
}
128135
}
129136
};
130137
let atlas_min = allocation.rectangle.min;

0 commit comments

Comments
 (0)