diff --git a/Cargo.toml b/Cargo.toml index 9bfb0d2..790e03f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ etagere = "0.2.10" cosmic-text = "0.11" lru = { version = "0.12.1", default-features = false } rustc-hash = "1.1" +once_cell = "1.19" [dev-dependencies] winit = { version = "0.29.10", features = ["rwh_05"] } diff --git a/examples/hello-world.rs b/examples/hello-world.rs index 5e81b81..08e4b36 100644 --- a/examples/hello-world.rs +++ b/examples/hello-world.rs @@ -1,6 +1,6 @@ use glyphon::{ - Attrs, Buffer, Color, Family, FontSystem, Metrics, Pipeline, Resolution, Shaping, SwashCache, - TextArea, TextAtlas, TextBounds, TextRenderer, + Attrs, Buffer, Color, Family, FontSystem, Metrics, Resolution, Shaping, SwashCache, TextArea, + TextAtlas, TextBounds, TextRenderer, }; use wgpu::{ CommandEncoderDescriptor, CompositeAlphaMode, DeviceDescriptor, Features, Instance, @@ -72,8 +72,7 @@ async fn run() { // Set up text renderer let mut font_system = FontSystem::new(); let mut cache = SwashCache::new(); - let pipeline = Pipeline::new(&device); - let mut atlas = TextAtlas::new(&device, &queue, &pipeline, swapchain_format); + let mut atlas = TextAtlas::new(&device, &queue, swapchain_format); let mut text_renderer = TextRenderer::new(&mut atlas, &device, MultisampleState::default(), None); let mut buffer = Buffer::new(&mut font_system, Metrics::new(30.0, 42.0)); diff --git a/src/lib.rs b/src/lib.rs index f6ea439..ed9d5a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,10 +10,10 @@ mod text_atlas; mod text_render; pub use error::{PrepareError, RenderError}; -pub use pipeline::Pipeline; pub use text_atlas::{ColorMode, TextAtlas}; pub use text_render::TextRenderer; +use pipeline::Pipeline; use text_render::ContentType; // Re-export all top-level types from `cosmic-text` for convenience. diff --git a/src/text_atlas.rs b/src/text_atlas.rs index 83f7edd..c5c6d8b 100644 --- a/src/text_atlas.rs +++ b/src/text_atlas.rs @@ -4,6 +4,7 @@ use crate::{ }; use etagere::{size2, Allocation, BucketedAtlasAllocator}; use lru::LruCache; +use once_cell::sync::OnceCell; use rustc_hash::FxHasher; use std::{collections::HashSet, hash::BuildHasherDefault, sync::Arc}; use wgpu::{ @@ -262,18 +263,20 @@ pub struct TextAtlas { impl TextAtlas { /// Creates a new [`TextAtlas`]. - pub fn new(device: &Device, queue: &Queue, pipeline: &Pipeline, format: TextureFormat) -> Self { - Self::with_color_mode(device, queue, pipeline, format, ColorMode::Accurate) + pub fn new(device: &Device, queue: &Queue, format: TextureFormat) -> Self { + Self::with_color_mode(device, queue, format, ColorMode::Accurate) } /// Creates a new [`TextAtlas`] with the given [`ColorMode`]. pub fn with_color_mode( device: &Device, queue: &Queue, - pipeline: &Pipeline, format: TextureFormat, color_mode: ColorMode, ) -> Self { + static PIPELINE: OnceCell = OnceCell::new(); + let pipeline = PIPELINE.get_or_init(|| Pipeline::new(device)); + let color_atlas = InnerAtlas::new( device, queue,