Accept atlas in prepare
and render
This commit is contained in:
parent
857f8c72c0
commit
73b2562179
2 changed files with 27 additions and 46 deletions
|
@ -66,8 +66,8 @@ async fn run() {
|
||||||
};
|
};
|
||||||
surface.configure(&device, &config);
|
surface.configure(&device, &config);
|
||||||
|
|
||||||
let atlas = TextAtlas::new(&device, &queue, swapchain_format);
|
let mut atlas = TextAtlas::new(&device, &queue, swapchain_format);
|
||||||
let mut text_renderer = TextRenderer::new(&device, &queue, &atlas);
|
let mut text_renderer = TextRenderer::new(&device, &queue);
|
||||||
|
|
||||||
let font = include_bytes!("./Inter-Bold.ttf") as &[u8];
|
let font = include_bytes!("./Inter-Bold.ttf") as &[u8];
|
||||||
let font = Font::from_bytes(font, FontSettings::default()).unwrap();
|
let font = Font::from_bytes(font, FontSettings::default()).unwrap();
|
||||||
|
@ -110,6 +110,7 @@ async fn run() {
|
||||||
.prepare(
|
.prepare(
|
||||||
&device,
|
&device,
|
||||||
&queue,
|
&queue,
|
||||||
|
&mut atlas,
|
||||||
Resolution {
|
Resolution {
|
||||||
width: config.width,
|
width: config.width,
|
||||||
height: config.height,
|
height: config.height,
|
||||||
|
@ -137,7 +138,7 @@ async fn run() {
|
||||||
depth_stencil_attachment: None,
|
depth_stencil_attachment: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
text_renderer.render(&mut pass).unwrap();
|
text_renderer.render(&atlas, &mut pass).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
queue.submit(Some(encoder.finish()));
|
queue.submit(Some(encoder.finish()));
|
||||||
|
|
50
src/lib.rs
50
src/lib.rs
|
@ -7,7 +7,7 @@ use std::{
|
||||||
mem::size_of,
|
mem::size_of,
|
||||||
num::{NonZeroU32, NonZeroU64},
|
num::{NonZeroU32, NonZeroU64},
|
||||||
slice,
|
slice,
|
||||||
sync::{Arc, RwLock},
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use etagere::{size2, AllocId, Allocation, BucketedAtlasAllocator};
|
use etagere::{size2, AllocId, Allocation, BucketedAtlasAllocator};
|
||||||
|
@ -102,7 +102,7 @@ pub struct Params {
|
||||||
screen_resolution: Resolution,
|
screen_resolution: Resolution,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_allocate(atlas: &mut InnerAtlas, width: usize, height: usize) -> Option<Allocation> {
|
fn try_allocate(atlas: &mut TextAtlas, width: usize, height: usize) -> Option<Allocation> {
|
||||||
let size = size2(width as i32, height as i32);
|
let size = size2(width as i32, height as i32);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
@ -120,7 +120,7 @@ fn try_allocate(atlas: &mut InnerAtlas, width: usize, height: usize) -> Option<A
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct InnerAtlas {
|
pub struct TextAtlas {
|
||||||
texture_pending: Vec<u8>,
|
texture_pending: Vec<u8>,
|
||||||
texture: Texture,
|
texture: Texture,
|
||||||
packer: BucketedAtlasAllocator,
|
packer: BucketedAtlasAllocator,
|
||||||
|
@ -129,11 +129,6 @@ struct InnerAtlas {
|
||||||
glyph_cache: RecentlyUsedMap<GlyphRasterConfig, GlyphDetails>,
|
glyph_cache: RecentlyUsedMap<GlyphRasterConfig, GlyphDetails>,
|
||||||
params: Params,
|
params: Params,
|
||||||
params_buffer: Buffer,
|
params_buffer: Buffer,
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct TextAtlas {
|
|
||||||
inner: Arc<RwLock<InnerAtlas>>,
|
|
||||||
pipeline: Arc<RenderPipeline>,
|
pipeline: Arc<RenderPipeline>,
|
||||||
bind_group: Arc<BindGroup>,
|
bind_group: Arc<BindGroup>,
|
||||||
}
|
}
|
||||||
|
@ -305,7 +300,6 @@ impl TextAtlas {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
inner: Arc::new(RwLock::new(InnerAtlas {
|
|
||||||
texture_pending,
|
texture_pending,
|
||||||
texture,
|
texture,
|
||||||
packer,
|
packer,
|
||||||
|
@ -314,7 +308,6 @@ impl TextAtlas {
|
||||||
glyph_cache,
|
glyph_cache,
|
||||||
params,
|
params,
|
||||||
params_buffer,
|
params_buffer,
|
||||||
})),
|
|
||||||
pipeline,
|
pipeline,
|
||||||
bind_group,
|
bind_group,
|
||||||
}
|
}
|
||||||
|
@ -327,13 +320,12 @@ pub struct TextRenderer {
|
||||||
index_buffer: Buffer,
|
index_buffer: Buffer,
|
||||||
index_buffer_size: u64,
|
index_buffer_size: u64,
|
||||||
vertices_to_render: u32,
|
vertices_to_render: u32,
|
||||||
atlas: TextAtlas,
|
|
||||||
glyphs_in_use: HashSet<GlyphRasterConfig>,
|
glyphs_in_use: HashSet<GlyphRasterConfig>,
|
||||||
screen_resolution: Resolution,
|
screen_resolution: Resolution,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextRenderer {
|
impl TextRenderer {
|
||||||
pub fn new(device: &Device, _queue: &Queue, atlas: &TextAtlas) -> Self {
|
pub fn new(device: &Device, _queue: &Queue) -> Self {
|
||||||
let vertex_buffer_size = next_copy_buffer_size(4096);
|
let vertex_buffer_size = next_copy_buffer_size(4096);
|
||||||
let vertex_buffer = device.create_buffer(&BufferDescriptor {
|
let vertex_buffer = device.create_buffer(&BufferDescriptor {
|
||||||
label: Some("glyphon vertices"),
|
label: Some("glyphon vertices"),
|
||||||
|
@ -356,7 +348,6 @@ impl TextRenderer {
|
||||||
index_buffer,
|
index_buffer,
|
||||||
index_buffer_size,
|
index_buffer_size,
|
||||||
vertices_to_render: 0,
|
vertices_to_render: 0,
|
||||||
atlas: atlas.clone(),
|
|
||||||
glyphs_in_use: HashSet::new(),
|
glyphs_in_use: HashSet::new(),
|
||||||
screen_resolution: Resolution {
|
screen_resolution: Resolution {
|
||||||
width: 0,
|
width: 0,
|
||||||
|
@ -369,19 +360,16 @@ impl TextRenderer {
|
||||||
&mut self,
|
&mut self,
|
||||||
device: &Device,
|
device: &Device,
|
||||||
queue: &Queue,
|
queue: &Queue,
|
||||||
|
atlas: &mut TextAtlas,
|
||||||
screen_resolution: Resolution,
|
screen_resolution: Resolution,
|
||||||
fonts: &[Font],
|
fonts: &[Font],
|
||||||
layouts: &[Layout<impl HasColor>],
|
layouts: &[Layout<impl HasColor>],
|
||||||
) -> Result<(), PrepareError> {
|
) -> Result<(), PrepareError> {
|
||||||
self.screen_resolution = screen_resolution;
|
self.screen_resolution = screen_resolution;
|
||||||
|
|
||||||
let atlas_current_resolution = {
|
let atlas_current_resolution = { atlas.params.screen_resolution };
|
||||||
let atlas = self.atlas.inner.read().expect("atlas locked");
|
|
||||||
atlas.params.screen_resolution
|
|
||||||
};
|
|
||||||
|
|
||||||
if screen_resolution != atlas_current_resolution {
|
if screen_resolution != atlas_current_resolution {
|
||||||
let mut atlas = self.atlas.inner.write().expect("atlas locked");
|
|
||||||
atlas.params.screen_resolution = screen_resolution;
|
atlas.params.screen_resolution = screen_resolution;
|
||||||
queue.write_buffer(&atlas.params_buffer, 0, unsafe {
|
queue.write_buffer(&atlas.params_buffer, 0, unsafe {
|
||||||
slice::from_raw_parts(
|
slice::from_raw_parts(
|
||||||
|
@ -405,13 +393,7 @@ impl TextRenderer {
|
||||||
for glyph in layout.glyphs() {
|
for glyph in layout.glyphs() {
|
||||||
self.glyphs_in_use.insert(glyph.key);
|
self.glyphs_in_use.insert(glyph.key);
|
||||||
|
|
||||||
let already_on_gpu = self
|
let already_on_gpu = atlas.glyph_cache.contains_key(&glyph.key);
|
||||||
.atlas
|
|
||||||
.inner
|
|
||||||
.read()
|
|
||||||
.expect("atlas locked")
|
|
||||||
.glyph_cache
|
|
||||||
.contains_key(&glyph.key);
|
|
||||||
|
|
||||||
if already_on_gpu {
|
if already_on_gpu {
|
||||||
continue;
|
continue;
|
||||||
|
@ -420,11 +402,9 @@ impl TextRenderer {
|
||||||
let font = &fonts[glyph.font_index];
|
let font = &fonts[glyph.font_index];
|
||||||
let (metrics, bitmap) = font.rasterize_config(glyph.key);
|
let (metrics, bitmap) = font.rasterize_config(glyph.key);
|
||||||
|
|
||||||
let mut atlas = self.atlas.inner.write().expect("atlas locked");
|
|
||||||
|
|
||||||
let (gpu_cache, atlas_id) = if glyph.char_data.rasterize() {
|
let (gpu_cache, atlas_id) = if glyph.char_data.rasterize() {
|
||||||
// Find a position in the packer
|
// Find a position in the packer
|
||||||
let allocation = match try_allocate(&mut atlas, metrics.width, metrics.height) {
|
let allocation = match try_allocate(atlas, metrics.width, metrics.height) {
|
||||||
Some(a) => a,
|
Some(a) => a,
|
||||||
None => return Err(PrepareError::AtlasFull),
|
None => return Err(PrepareError::AtlasFull),
|
||||||
};
|
};
|
||||||
|
@ -483,7 +463,6 @@ impl TextRenderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ub) = upload_bounds {
|
if let Some(ub) = upload_bounds {
|
||||||
let atlas = self.atlas.inner.read().expect("atlas locked");
|
|
||||||
queue.write_texture(
|
queue.write_texture(
|
||||||
ImageCopyTexture {
|
ImageCopyTexture {
|
||||||
texture: &atlas.texture,
|
texture: &atlas.texture,
|
||||||
|
@ -515,7 +494,6 @@ impl TextRenderer {
|
||||||
|
|
||||||
for layout in layouts.iter() {
|
for layout in layouts.iter() {
|
||||||
for glyph in layout.glyphs() {
|
for glyph in layout.glyphs() {
|
||||||
let atlas = self.atlas.inner.read().expect("atlas locked");
|
|
||||||
let details = atlas.glyph_cache.get(&glyph.key).unwrap();
|
let details = atlas.glyph_cache.get(&glyph.key).unwrap();
|
||||||
let (atlas_x, atlas_y) = match details.gpu_cache {
|
let (atlas_x, atlas_y) = match details.gpu_cache {
|
||||||
GpuCache::InAtlas { x, y } => (x, y),
|
GpuCache::InAtlas { x, y } => (x, y),
|
||||||
|
@ -602,14 +580,16 @@ impl TextRenderer {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render<'pass>(&'pass mut self, pass: &mut RenderPass<'pass>) -> Result<(), RenderError> {
|
pub fn render<'pass>(
|
||||||
|
&'pass mut self,
|
||||||
|
atlas: &'pass TextAtlas,
|
||||||
|
pass: &mut RenderPass<'pass>,
|
||||||
|
) -> Result<(), RenderError> {
|
||||||
if self.vertices_to_render == 0 {
|
if self.vertices_to_render == 0 {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let atlas = self.atlas.inner.read().expect("atlas locked");
|
|
||||||
|
|
||||||
// Validate that glyphs haven't been evicted from cache since `prepare`
|
// Validate that glyphs haven't been evicted from cache since `prepare`
|
||||||
for glyph in self.glyphs_in_use.iter() {
|
for glyph in self.glyphs_in_use.iter() {
|
||||||
if !atlas.glyph_cache.contains_key(glyph) {
|
if !atlas.glyph_cache.contains_key(glyph) {
|
||||||
|
@ -623,8 +603,8 @@ impl TextRenderer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pass.set_pipeline(&self.atlas.pipeline);
|
pass.set_pipeline(&atlas.pipeline);
|
||||||
pass.set_bind_group(0, &self.atlas.bind_group, &[]);
|
pass.set_bind_group(0, &atlas.bind_group, &[]);
|
||||||
pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
||||||
pass.set_index_buffer(self.index_buffer.slice(..), IndexFormat::Uint32);
|
pass.set_index_buffer(self.index_buffer.slice(..), IndexFormat::Uint32);
|
||||||
pass.draw_indexed(0..self.vertices_to_render, 0, 0..1);
|
pass.draw_indexed(0..self.vertices_to_render, 0, 0..1);
|
||||||
|
|
Loading…
Reference in a new issue