Skip to content

Commit

Permalink
Split color and mask atlases apart
Browse files Browse the repository at this point in the history
  • Loading branch information
grovesNL committed Nov 28, 2022
1 parent b45f1c6 commit d9194a0
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 123 deletions.
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@ mod text_render;
pub use error::{PrepareError, RenderError};
use recently_used::RecentlyUsedMap;
pub use text_atlas::TextAtlas;
use text_render::ContentType;
pub use text_render::TextRenderer;

pub use cosmic_text;

pub(crate) enum GpuCache {
InAtlas { x: u16, y: u16 },
pub(crate) enum GpuCacheStatus {
InAtlas {
x: u16,
y: u16,
content_type: ContentType,
},
SkipRasterization,
}

pub(crate) struct GlyphDetails {
width: u16,
height: u16,
gpu_cache: GpuCache,
gpu_cache: GpuCacheStatus,
atlas_id: Option<AllocId>,
top: i16,
left: i16,
Expand All @@ -33,6 +38,7 @@ pub(crate) struct GlyphToRender {
dim: [u16; 2],
uv: [u16; 2],
color: u32,
content_type: u32,
}

/// The screen resolution to use when rendering text.
Expand Down
36 changes: 33 additions & 3 deletions src/shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ struct VertexInput {
@location(1) dim: u32,
@location(2) uv: u32,
@location(3) color: u32,
@location(4) content_type: u32,
}

struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) color: vec4<f32>,
@location(1) uv: vec2<f32>,
@location(2) content_type: u32,
};

struct Params {
Expand All @@ -21,9 +23,12 @@ struct Params {
var<uniform> params: Params;

@group(0) @binding(1)
var atlas_texture: texture_2d<f32>;
var color_atlas_texture: texture_2d<f32>;

@group(0) @binding(2)
var mask_atlas_texture: texture_2d<f32>;

@group(0) @binding(3)
var atlas_sampler: sampler;

@vertex
Expand Down Expand Up @@ -70,12 +75,37 @@ fn vs_main(in_vert: VertexInput) -> VertexOutput {
f32((color & 0xff000000u) >> 24u),
) / 255.0;

vert_output.uv = vec2<f32>(uv) / vec2<f32>(textureDimensions(atlas_texture).xy);
var dim = vec2<u32>(0);
switch in_vert.content_type {
case 0u: {
dim = textureDimensions(color_atlas_texture);
break;
}
case 1u: {
dim = textureDimensions(mask_atlas_texture);
break;
}
default: {}
}

vert_output.content_type = in_vert.content_type;

vert_output.uv = vec2<f32>(uv) / vec2<f32>(dim);

return vert_output;
}

@fragment
fn fs_main(in_frag: VertexOutput) -> @location(0) vec4<f32> {
return in_frag.color * textureSample(atlas_texture, atlas_sampler, in_frag.uv);
switch in_frag.content_type {
case 0u: {
return textureSampleLevel(color_atlas_texture, atlas_sampler, in_frag.uv, 0.0);
}
case 1u: {
return in_frag.color * textureSampleLevel(mask_atlas_texture, atlas_sampler, in_frag.uv, 0.0);
}
default: {
return vec4<f32>(0.0);
}
}
}
166 changes: 127 additions & 39 deletions src/text_atlas.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
use crate::{
text_render::ContentType, GlyphDetails, GlyphToRender, Params, RecentlyUsedMap, Resolution,
};
use cosmic_text::CacheKey;
use etagere::{size2, BucketedAtlasAllocator};
use etagere::{size2, Allocation, BucketedAtlasAllocator};
use std::{borrow::Cow, mem::size_of, num::NonZeroU64, sync::Arc};
use wgpu::{
BindGroup, BindGroupEntry, BindGroupLayoutEntry, BindingResource, BindingType, BlendState,
Buffer, BufferBindingType, BufferDescriptor, BufferUsages, ColorTargetState, ColorWrites,
Device, Extent3d, FilterMode, FragmentState, MultisampleState, PipelineLayoutDescriptor,
PrimitiveState, Queue, RenderPipeline, RenderPipelineDescriptor, SamplerBindingType,
SamplerDescriptor, ShaderModuleDescriptor, ShaderSource, ShaderStages, Texture,
TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType, TextureUsages,
TextureViewDescriptor, TextureViewDimension, VertexFormat, VertexState,
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayoutEntry, BindingResource,
BindingType, BlendState, Buffer, BufferBindingType, BufferDescriptor, BufferUsages,
ColorTargetState, ColorWrites, Device, Extent3d, FilterMode, FragmentState, MultisampleState,
PipelineLayoutDescriptor, PrimitiveState, Queue, RenderPipeline, RenderPipelineDescriptor,
SamplerBindingType, SamplerDescriptor, ShaderModuleDescriptor, ShaderSource, ShaderStages,
Texture, TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType, TextureUsages,
TextureView, TextureViewDescriptor, TextureViewDimension, VertexFormat, VertexState,
};

use crate::{GlyphDetails, GlyphToRender, Params, RecentlyUsedMap, Resolution};

pub(crate) const NUM_ATLAS_CHANNELS: usize = 4usize;

/// An atlas containing a cache of rasterized glyphs that can be rendered.
pub struct TextAtlas {
pub(crate) texture_pending: Vec<u8>,
pub(crate) texture: Texture,
pub(crate) packer: BucketedAtlasAllocator,
pub(crate) width: u32,
pub(crate) height: u32,
pub(crate) glyph_cache: RecentlyUsedMap<CacheKey, GlyphDetails>,
pub(crate) params: Params,
pub(crate) params_buffer: Buffer,
pub(crate) pipeline: Arc<RenderPipeline>,
pub(crate) bind_group: Arc<BindGroup>,
pub(crate) struct InnerAtlas {
pub texture_pending: Vec<u8>,
pub texture: Texture,
pub texture_view: TextureView,
pub packer: BucketedAtlasAllocator,
pub width: u32,
pub height: u32,
pub glyph_cache: RecentlyUsedMap<CacheKey, GlyphDetails>,
pub num_atlas_channels: usize,
}

impl TextAtlas {
/// Creates a new `TextAtlas`.
pub fn new(device: &Device, _queue: &Queue, format: TextureFormat) -> Self {
impl InnerAtlas {
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 packer = BucketedAtlasAllocator::new(size2(width as i32, height as i32));

// Create a texture to use for our atlas
let texture_pending = vec![0; (width * height) as usize * NUM_ATLAS_CHANNELS];
let texture_pending = vec![0; (width * height) as usize * num_atlas_channels];
let texture = device.create_texture(&TextureDescriptor {
label: Some("glyphon atlas"),
size: Extent3d {
Expand All @@ -50,10 +45,61 @@ impl TextAtlas {
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Rgba8Unorm,
format: match num_atlas_channels {
1 => TextureFormat::R8Unorm,
4 => TextureFormat::Rgba8Unorm,
_ => panic!("unexpected number of channels"),
},
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
});

let texture_view = texture.create_view(&TextureViewDescriptor::default());

let glyph_cache = RecentlyUsedMap::new();

Self {
texture_pending,
texture,
texture_view,
packer,
width,
height,
glyph_cache,
num_atlas_channels,
}
}

pub(crate) fn try_allocate(&mut self, width: usize, height: usize) -> Option<Allocation> {
let size = size2(width as i32, height as i32);

loop {
let allocation = self.packer.allocate(size);
if allocation.is_some() {
return allocation;
}

// Try to free least recently used allocation
let (key, value) = self.glyph_cache.pop()?;
self.packer
.deallocate(value.atlas_id.expect("cache corrupt"));
self.glyph_cache.remove(&key);
}
}
}

/// An atlas containing a cache of rasterized glyphs that can be rendered.
pub struct TextAtlas {
pub(crate) params: Params,
pub(crate) params_buffer: Buffer,
pub(crate) pipeline: Arc<RenderPipeline>,
pub(crate) bind_group: Arc<BindGroup>,
pub(crate) color_atlas: InnerAtlas,
pub(crate) mask_atlas: InnerAtlas,
}

impl TextAtlas {
/// Creates a new `TextAtlas`.
pub fn new(device: &Device, queue: &Queue, format: TextureFormat) -> Self {
let sampler = device.create_sampler(&SamplerDescriptor {
label: Some("glyphon sampler"),
min_filter: FilterMode::Nearest,
Expand All @@ -64,8 +110,6 @@ impl TextAtlas {
..Default::default()
});

let glyph_cache = RecentlyUsedMap::new();

// Create a render pipeline to use for rendering later
let shader = device.create_shader_module(ShaderModuleDescriptor {
label: Some("glyphon shader"),
Expand Down Expand Up @@ -96,6 +140,11 @@ impl TextAtlas {
offset: size_of::<u32>() as u64 * 4,
shader_location: 3,
},
wgpu::VertexAttribute {
format: VertexFormat::Uint32,
offset: size_of::<u32>() as u64 * 5,
shader_location: 4,
}
],
}];

Expand Down Expand Up @@ -123,6 +172,16 @@ impl TextAtlas {
},
BindGroupLayoutEntry {
binding: 2,
visibility: ShaderStages::VERTEX | ShaderStages::FRAGMENT,
ty: BindingType::Texture {
multisampled: false,
view_dimension: TextureViewDimension::D2,
sample_type: TextureSampleType::Float { filterable: true },
},
count: None,
},
BindGroupLayoutEntry {
binding: 3,
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Sampler(SamplerBindingType::Filtering),
count: None,
Expand All @@ -146,7 +205,10 @@ impl TextAtlas {
mapped_at_creation: false,
});

let bind_group = Arc::new(device.create_bind_group(&wgpu::BindGroupDescriptor {
let color_atlas = InnerAtlas::new(device, queue, 4);
let mask_atlas = InnerAtlas::new(device, queue, 1);

let bind_group = Arc::new(device.create_bind_group(&BindGroupDescriptor {
layout: &bind_group_layout,
entries: &[
BindGroupEntry {
Expand All @@ -155,10 +217,14 @@ impl TextAtlas {
},
BindGroupEntry {
binding: 1,
resource: BindingResource::TextureView(&texture_view),
resource: BindingResource::TextureView(&color_atlas.texture_view),
},
BindGroupEntry {
binding: 2,
resource: BindingResource::TextureView(&mask_atlas.texture_view),
},
BindGroupEntry {
binding: 3,
resource: BindingResource::Sampler(&sampler),
},
],
Expand Down Expand Up @@ -199,16 +265,38 @@ impl TextAtlas {
}));

Self {
texture_pending,
texture,
packer,
width,
height,
glyph_cache,
params,
params_buffer,
pipeline,
bind_group,
color_atlas,
mask_atlas,
}
}

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

pub(crate) fn glyph(&self, glyph: &CacheKey) -> Option<&GlyphDetails> {
self.mask_atlas
.glyph_cache
.get(glyph)
.or_else(|| self.color_atlas.glyph_cache.get(glyph))
}

pub(crate) fn inner_for_content(&self, content_type: ContentType) -> &InnerAtlas {
match content_type {
ContentType::Color => &self.color_atlas,
ContentType::Mask => &self.mask_atlas,
}
}

pub(crate) fn inner_for_content_mut(&mut self, content_type: ContentType) -> &mut InnerAtlas {
match content_type {
ContentType::Color => &mut self.color_atlas,
ContentType::Mask => &mut self.mask_atlas,
}
}
}
Loading

0 comments on commit d9194a0

Please sign in to comment.