glyphon/src/lib.rs

641 lines
21 KiB
Rust
Raw Normal View History

2022-05-09 08:49:10 -04:00
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
error::Error,
fmt::{self, Display, Formatter},
2022-05-09 10:22:38 -04:00
iter,
mem::size_of,
2022-05-09 08:49:10 -04:00
num::{NonZeroU32, NonZeroU64},
slice,
2022-05-14 22:32:00 -04:00
sync::{Arc, RwLock},
2022-05-09 08:49:10 -04:00
};
use etagere::{size2, AllocId, Allocation, BucketedAtlasAllocator};
use fontdue::{
layout::{GlyphRasterConfig, Layout},
Font,
};
use wgpu::{
BindGroup, BindGroupEntry, BindGroupLayoutEntry, BindingResource, BindingType, BlendState,
Buffer, BufferBindingType, BufferDescriptor, BufferUsages, ColorTargetState, ColorWrites,
Device, Extent3d, FilterMode, FragmentState, ImageCopyTexture, ImageDataLayout, IndexFormat,
MultisampleState, Origin3d, PipelineLayoutDescriptor, PrimitiveState, Queue, RenderPass,
RenderPipeline, RenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor,
ShaderModuleDescriptor, ShaderSource, ShaderStages, Texture, TextureAspect, TextureDescriptor,
TextureDimension, TextureFormat, TextureSampleType, TextureUsages, TextureViewDescriptor,
2022-05-10 07:18:28 -04:00
TextureViewDimension, VertexFormat, VertexState, COPY_BUFFER_ALIGNMENT,
2022-05-09 08:49:10 -04:00
};
2022-05-09 10:22:38 -04:00
pub use fontdue;
#[repr(C)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
pub trait HasColor: Copy {
fn color(&self) -> Color;
2022-05-09 21:10:00 -04:00
}
2022-05-09 08:49:10 -04:00
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PrepareError {
AtlasFull,
}
impl Display for PrepareError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "prepare error")
}
}
impl Error for PrepareError {}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RenderError {}
impl Display for RenderError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "render error")
}
}
enum GpuCache {
InAtlas { x: u16, y: u16 },
SkipRasterization,
}
struct GlyphDetails {
2022-05-09 10:22:38 -04:00
width: u16,
height: u16,
2022-05-09 08:49:10 -04:00
gpu_cache: GpuCache,
atlas_id: Option<AllocId>,
}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
struct GlyphToRender {
2022-05-09 10:22:38 -04:00
pos: [u32; 2],
dim: [u16; 2],
uv: [u16; 2],
2022-05-09 08:49:10 -04:00
color: [u8; 4],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Resolution {
pub width: u32,
pub height: u32,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Params {
screen_resolution: Resolution,
}
fn try_allocate(
2022-05-14 22:32:00 -04:00
atlas: &mut InnerAtlas,
layout: &Layout<impl HasColor>,
2022-05-09 08:49:10 -04:00
width: usize,
height: usize,
) -> Option<Allocation> {
let size = size2(width as i32, height as i32);
2022-05-14 22:32:00 -04:00
let allocation = atlas.packer.allocate(size);
2022-05-09 08:49:10 -04:00
if allocation.is_some() {
return allocation;
}
// Try to free any allocations not used in the current layout
2022-05-14 22:32:00 -04:00
// TODO: use LRU instead
2022-05-09 08:49:10 -04:00
let used_glyphs = layout
.glyphs()
.iter()
.map(|gp| gp.key)
.collect::<HashSet<_>>();
2022-05-14 22:32:00 -04:00
atlas.glyph_cache.retain(|key, details| {
2022-05-09 08:49:10 -04:00
if used_glyphs.contains(&key) {
true
} else {
if let Some(atlas_id) = details.atlas_id {
2022-05-14 22:32:00 -04:00
atlas.packer.deallocate(atlas_id)
2022-05-09 08:49:10 -04:00
}
false
}
});
// Attempt to reallocate
2022-05-14 22:32:00 -04:00
atlas.packer.allocate(size)
2022-05-09 08:49:10 -04:00
}
2022-05-14 22:32:00 -04:00
struct InnerAtlas {
texture_pending: Vec<u8>,
texture: Texture,
packer: BucketedAtlasAllocator,
width: u32,
height: u32,
2022-05-09 08:49:10 -04:00
glyph_cache: HashMap<GlyphRasterConfig, GlyphDetails>,
params: Params,
params_buffer: Buffer,
}
2022-05-14 22:32:00 -04:00
#[derive(Clone)]
pub struct TextAtlas {
inner: Arc<RwLock<InnerAtlas>>,
pipeline: Arc<RenderPipeline>,
bind_group: Arc<BindGroup>,
}
impl TextAtlas {
2022-05-09 10:59:23 -04:00
pub fn new(device: &Device, _queue: &Queue, format: TextureFormat) -> Self {
2022-05-09 08:49:10 -04:00
let max_texture_dimension_2d = device.limits().max_texture_dimension_2d;
2022-05-14 22:32:00 -04:00
let width = max_texture_dimension_2d;
let height = max_texture_dimension_2d;
2022-05-09 08:49:10 -04:00
2022-05-14 22:32:00 -04:00
let packer = BucketedAtlasAllocator::new(size2(width as i32, height as i32));
2022-05-09 08:49:10 -04:00
// Create a texture to use for our atlas
2022-05-14 22:32:00 -04:00
let texture_pending = vec![0; (width * height) as usize];
let texture = device.create_texture(&TextureDescriptor {
2022-05-09 08:49:10 -04:00
label: Some("glyphon atlas"),
size: Extent3d {
2022-05-14 22:32:00 -04:00
width,
height,
2022-05-09 08:49:10 -04:00
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::R8Unorm,
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
});
2022-05-14 22:32:00 -04:00
let texture_view = texture.create_view(&TextureViewDescriptor::default());
let sampler = device.create_sampler(&SamplerDescriptor {
2022-05-09 08:49:10 -04:00
label: Some("glyphon sampler"),
min_filter: FilterMode::Nearest,
mag_filter: FilterMode::Nearest,
mipmap_filter: FilterMode::Nearest,
lod_min_clamp: 0f32,
lod_max_clamp: 0f32,
..Default::default()
});
2022-05-14 22:32:00 -04:00
let glyph_cache = HashMap::new();
2022-05-09 08:49:10 -04:00
// Create a render pipeline to use for rendering later
let shader = device.create_shader_module(&ShaderModuleDescriptor {
label: Some("glyphon shader"),
source: ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
});
let vertex_buffers = [wgpu::VertexBufferLayout {
2022-05-09 10:22:38 -04:00
array_stride: size_of::<GlyphToRender>() as wgpu::BufferAddress,
2022-05-09 08:49:10 -04:00
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
2022-05-09 10:22:38 -04:00
format: VertexFormat::Uint32x2,
2022-05-09 08:49:10 -04:00
offset: 0,
shader_location: 0,
},
wgpu::VertexAttribute {
2022-05-09 10:22:38 -04:00
format: VertexFormat::Uint32,
offset: size_of::<u32>() as u64 * 2,
2022-05-09 08:49:10 -04:00
shader_location: 1,
},
wgpu::VertexAttribute {
format: VertexFormat::Uint32,
2022-05-09 10:22:38 -04:00
offset: size_of::<u32>() as u64 * 3,
2022-05-09 08:49:10 -04:00
shader_location: 2,
},
2022-05-09 10:22:38 -04:00
wgpu::VertexAttribute {
format: VertexFormat::Uint32,
offset: size_of::<u32>() as u64 * 4,
shader_location: 3,
},
2022-05-09 08:49:10 -04:00
],
}];
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::VERTEX,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
2022-05-09 10:22:38 -04:00
min_binding_size: NonZeroU64::new(size_of::<Params>() as u64),
2022-05-09 08:49:10 -04:00
},
count: None,
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::VERTEX | ShaderStages::FRAGMENT,
ty: BindingType::Texture {
multisampled: false,
view_dimension: TextureViewDimension::D2,
sample_type: TextureSampleType::Float { filterable: true },
},
count: None,
},
BindGroupLayoutEntry {
binding: 2,
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Sampler(SamplerBindingType::Filtering),
count: None,
},
],
label: Some("glyphon bind group layout"),
});
2022-05-09 10:59:23 -04:00
let params = Params {
screen_resolution: Resolution {
width: 0,
height: 0,
},
2022-05-09 08:49:10 -04:00
};
2022-05-09 10:59:23 -04:00
let params_buffer = device.create_buffer(&BufferDescriptor {
2022-05-09 08:49:10 -04:00
label: Some("glyphon params"),
2022-05-09 10:59:23 -04:00
size: size_of::<Params>() as u64,
2022-05-09 08:49:10 -04:00
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
2022-05-09 10:59:23 -04:00
mapped_at_creation: false,
2022-05-09 08:49:10 -04:00
});
2022-05-14 22:32:00 -04:00
let bind_group = Arc::new(device.create_bind_group(&wgpu::BindGroupDescriptor {
2022-05-09 08:49:10 -04:00
layout: &bind_group_layout,
entries: &[
BindGroupEntry {
binding: 0,
resource: params_buffer.as_entire_binding(),
},
BindGroupEntry {
binding: 1,
2022-05-14 22:32:00 -04:00
resource: BindingResource::TextureView(&texture_view),
2022-05-09 08:49:10 -04:00
},
BindGroupEntry {
binding: 2,
2022-05-14 22:32:00 -04:00
resource: BindingResource::Sampler(&sampler),
2022-05-09 08:49:10 -04:00
},
],
label: Some("glyphon bind group"),
2022-05-14 22:32:00 -04:00
}));
2022-05-09 08:49:10 -04:00
let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});
2022-05-14 22:32:00 -04:00
let pipeline = Arc::new(device.create_render_pipeline(&RenderPipelineDescriptor {
2022-05-09 08:49:10 -04:00
label: Some("glyphon pipeline"),
layout: Some(&pipeline_layout),
vertex: VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &vertex_buffers,
},
fragment: Some(FragmentState {
module: &shader,
entry_point: "fs_main",
targets: &[ColorTargetState {
format,
blend: Some(BlendState::ALPHA_BLENDING),
write_mask: ColorWrites::default(),
}],
}),
primitive: PrimitiveState::default(),
depth_stencil: None,
multisample: MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
2022-05-14 22:32:00 -04:00
}));
Self {
inner: Arc::new(RwLock::new(InnerAtlas {
texture_pending,
texture,
packer,
width,
height,
glyph_cache,
params,
params_buffer,
})),
pipeline,
bind_group,
}
}
}
pub struct TextRenderer {
vertex_buffer: Buffer,
vertex_buffer_size: u64,
index_buffer: Buffer,
index_buffer_size: u64,
vertices_to_render: u32,
atlas: TextAtlas,
}
2022-05-09 08:49:10 -04:00
2022-05-14 22:32:00 -04:00
impl TextRenderer {
pub fn new(device: &Device, _queue: &Queue, atlas: &TextAtlas) -> Self {
2022-05-10 07:18:28 -04:00
let vertex_buffer_size = next_copy_buffer_size(4096);
2022-05-09 08:49:10 -04:00
let vertex_buffer = device.create_buffer(&BufferDescriptor {
label: Some("glyphon vertices"),
size: vertex_buffer_size,
usage: BufferUsages::VERTEX | BufferUsages::COPY_DST,
mapped_at_creation: false,
});
2022-05-10 07:18:28 -04:00
let index_buffer_size = next_copy_buffer_size(4096);
2022-05-09 08:49:10 -04:00
let index_buffer = device.create_buffer(&BufferDescriptor {
label: Some("glyphon indices"),
size: index_buffer_size,
usage: BufferUsages::INDEX | BufferUsages::COPY_DST,
mapped_at_creation: false,
});
Self {
vertex_buffer,
vertex_buffer_size,
index_buffer,
index_buffer_size,
vertices_to_render: 0,
2022-05-14 22:32:00 -04:00
atlas: atlas.clone(),
2022-05-09 08:49:10 -04:00
}
}
pub fn prepare(
&mut self,
device: &Device,
queue: &Queue,
screen_resolution: Resolution,
fonts: &[Font],
2022-05-10 07:18:28 -04:00
layouts: &[Layout<impl HasColor>],
2022-05-09 08:49:10 -04:00
) -> Result<(), PrepareError> {
2022-05-14 22:32:00 -04:00
let current_resolution = {
let atlas = self.atlas.inner.read().expect("atlas locked");
atlas.params.screen_resolution
};
if screen_resolution != current_resolution {
let mut atlas = self.atlas.inner.write().expect("atlas locked");
atlas.params.screen_resolution = screen_resolution;
queue.write_buffer(&atlas.params_buffer, 0, unsafe {
2022-05-09 08:49:10 -04:00
slice::from_raw_parts(
2022-05-14 22:32:00 -04:00
&atlas.params as *const Params as *const u8,
2022-05-09 10:22:38 -04:00
size_of::<Params>(),
2022-05-09 08:49:10 -04:00
)
});
}
struct UploadBounds {
x_min: usize,
x_max: usize,
y_min: usize,
y_max: usize,
}
let mut upload_bounds = None::<UploadBounds>;
for layout in layouts.iter() {
for glyph in layout.glyphs() {
2022-05-14 22:32:00 -04:00
let already_on_gpu = self
.atlas
.inner
.read()
.expect("atlas locked")
.glyph_cache
.contains_key(&glyph.key);
2022-05-09 08:49:10 -04:00
if already_on_gpu {
continue;
}
let font = &fonts[glyph.font_index];
let (metrics, bitmap) = font.rasterize_config(glyph.key);
2022-05-14 22:32:00 -04:00
let mut atlas = self.atlas.inner.write().expect("atlas locked");
2022-05-09 08:49:10 -04:00
let (gpu_cache, atlas_id) = if glyph.char_data.rasterize() {
// Find a position in the packer
2022-05-14 22:32:00 -04:00
let allocation =
match try_allocate(&mut atlas, layout, metrics.width, metrics.height) {
Some(a) => a,
None => return Err(PrepareError::AtlasFull),
};
2022-05-09 08:49:10 -04:00
let atlas_min = allocation.rectangle.min;
let atlas_max = allocation.rectangle.max;
for row in 0..metrics.height {
let y_offset = atlas_min.y as usize;
let x_offset =
2022-05-14 22:32:00 -04:00
(y_offset + row) * atlas.width as usize + atlas_min.x as usize;
2022-05-09 08:49:10 -04:00
let bitmap_row = &bitmap[row * metrics.width..(row + 1) * metrics.width];
2022-05-14 22:32:00 -04:00
atlas.texture_pending[x_offset..x_offset + metrics.width]
2022-05-09 08:49:10 -04:00
.copy_from_slice(bitmap_row);
}
match upload_bounds.as_mut() {
Some(ub) => {
ub.x_min = ub.x_min.min(atlas_min.x as usize);
ub.x_max = ub.x_max.max(atlas_max.x as usize);
ub.y_min = ub.y_min.min(atlas_min.y as usize);
ub.y_max = ub.y_max.max(atlas_max.y as usize);
}
None => {
upload_bounds = Some(UploadBounds {
x_min: atlas_min.x as usize,
x_max: atlas_max.x as usize,
y_min: atlas_min.y as usize,
y_max: atlas_max.y as usize,
});
}
}
(
GpuCache::InAtlas {
x: atlas_min.x as u16,
y: atlas_min.y as u16,
},
Some(allocation.id),
)
} else {
(GpuCache::SkipRasterization, None)
};
2022-05-14 22:32:00 -04:00
atlas.glyph_cache.insert(
2022-05-09 08:49:10 -04:00
glyph.key,
GlyphDetails {
2022-05-09 10:22:38 -04:00
width: metrics.width as u16,
height: metrics.height as u16,
2022-05-09 08:49:10 -04:00
gpu_cache,
atlas_id,
},
);
}
}
if let Some(ub) = upload_bounds {
2022-05-14 22:32:00 -04:00
let atlas = self.atlas.inner.read().expect("atlas locked");
2022-05-09 08:49:10 -04:00
queue.write_texture(
ImageCopyTexture {
2022-05-14 22:32:00 -04:00
texture: &atlas.texture,
2022-05-09 08:49:10 -04:00
mip_level: 0,
origin: Origin3d {
x: ub.x_min as u32,
y: ub.y_min as u32,
z: 0,
},
aspect: TextureAspect::All,
},
2022-05-14 22:32:00 -04:00
&atlas.texture_pending[ub.y_min * atlas.width as usize + ub.x_min..],
2022-05-09 08:49:10 -04:00
ImageDataLayout {
offset: 0,
2022-05-14 22:32:00 -04:00
bytes_per_row: NonZeroU32::new(atlas.width as u32),
rows_per_image: NonZeroU32::new(atlas.height as u32),
2022-05-09 08:49:10 -04:00
},
Extent3d {
width: (ub.x_max - ub.x_min) as u32,
height: (ub.y_max - ub.y_min) as u32,
depth_or_array_layers: 1,
},
);
}
let mut glyph_vertices = Vec::new();
let mut glyph_indices = Vec::new();
let mut glyphs_added = 0;
for layout in layouts.iter() {
for glyph in layout.glyphs() {
2022-05-14 22:32:00 -04:00
let atlas = self.atlas.inner.read().expect("atlas locked");
let details = atlas.glyph_cache.get(&glyph.key).unwrap();
2022-05-09 08:49:10 -04:00
let (atlas_x, atlas_y) = match details.gpu_cache {
GpuCache::InAtlas { x, y } => (x, y),
GpuCache::SkipRasterization => continue,
};
let color = glyph.user_data.color();
2022-05-09 08:49:10 -04:00
glyph_vertices.extend(
iter::repeat(GlyphToRender {
2022-05-09 10:22:38 -04:00
// Note: subpixel positioning is not currently handled, so we always use
// the nearest pixel.
pos: [glyph.x.round() as u32, glyph.y.round() as u32],
dim: [details.width, details.height],
uv: [atlas_x, atlas_y],
color: [color.r, color.g, color.b, color.a],
2022-05-09 08:49:10 -04:00
})
.take(4),
);
let start = 4 * glyphs_added as u32;
glyph_indices.extend([start, start + 1, start + 2, start, start + 2, start + 3]);
glyphs_added += 1;
}
}
const VERTICES_PER_GLYPH: u32 = 6;
self.vertices_to_render = glyphs_added as u32 * VERTICES_PER_GLYPH;
let will_render = glyphs_added > 0;
if !will_render {
return Ok(());
}
let vertices = glyph_vertices.as_slice();
let vertices_raw = unsafe {
slice::from_raw_parts(
vertices as *const _ as *const u8,
2022-05-09 10:22:38 -04:00
size_of::<GlyphToRender>() * vertices.len(),
2022-05-09 08:49:10 -04:00
)
};
if self.vertex_buffer_size >= vertices_raw.len() as u64 {
queue.write_buffer(&self.vertex_buffer, 0, vertices_raw);
} else {
self.vertex_buffer.destroy();
2022-05-10 07:18:28 -04:00
let (buffer, buffer_size) = create_oversized_buffer(
device,
Some("glyphon vertices"),
vertices_raw,
BufferUsages::VERTEX | BufferUsages::COPY_DST,
);
self.vertex_buffer = buffer;
self.vertex_buffer_size = buffer_size;
2022-05-09 08:49:10 -04:00
}
let indices = glyph_indices.as_slice();
let indices_raw = unsafe {
slice::from_raw_parts(
indices as *const _ as *const u8,
2022-05-09 10:22:38 -04:00
size_of::<u32>() * indices.len(),
2022-05-09 08:49:10 -04:00
)
};
if self.index_buffer_size >= indices_raw.len() as u64 {
queue.write_buffer(&self.index_buffer, 0, indices_raw);
} else {
self.index_buffer.destroy();
2022-05-10 07:18:28 -04:00
let (buffer, buffer_size) = create_oversized_buffer(
device,
Some("glyphon indices"),
indices_raw,
BufferUsages::INDEX | BufferUsages::COPY_DST,
);
self.index_buffer = buffer;
self.index_buffer_size = buffer_size;
2022-05-09 08:49:10 -04:00
}
Ok(())
}
pub fn render<'pass>(&'pass mut self, pass: &mut RenderPass<'pass>) -> Result<(), ()> {
if self.vertices_to_render == 0 {
return Ok(());
}
2022-05-14 22:32:00 -04:00
pass.set_pipeline(&self.atlas.pipeline);
pass.set_bind_group(0, &self.atlas.bind_group, &[]);
2022-05-09 08:49:10 -04:00
pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
pass.set_index_buffer(self.index_buffer.slice(..), IndexFormat::Uint32);
pass.draw_indexed(0..self.vertices_to_render, 0, 0..1);
Ok(())
}
}
2022-05-10 07:18:28 -04:00
fn next_copy_buffer_size(size: u64) -> u64 {
let next_power_of_2 = size.next_power_of_two() as u64;
let align_mask = COPY_BUFFER_ALIGNMENT - 1;
let padded_size = ((next_power_of_2 + align_mask) & !align_mask).max(COPY_BUFFER_ALIGNMENT);
padded_size
}
fn create_oversized_buffer(
device: &Device,
label: Option<&str>,
contents: &[u8],
usage: BufferUsages,
) -> (Buffer, u64) {
let size = next_copy_buffer_size(contents.len() as u64);
let buffer = device.create_buffer(&BufferDescriptor {
label,
size,
usage,
mapped_at_creation: true,
});
buffer.slice(..).get_mapped_range_mut()[..contents.len()].copy_from_slice(contents);
buffer.unmap();
(buffer, size)
}