diff --git a/Cargo.toml b/Cargo.toml index 8b158ed..e0d50d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,18 +1,18 @@ [package] name = "glyphon" description = "Fast, simple 2D text rendering for wgpu" -version = "0.5.0" +version = "0.3.0" edition = "2021" homepage = "https://github.com/grovesNL/glyphon.git" repository = "https://github.com/grovesNL/glyphon" license = "MIT OR Apache-2.0 OR Zlib" [dependencies] -wgpu = "0.19" +wgpu = "0.18" etagere = "0.2.10" -lru = "0.12.1" -cosmic-text = "0.11.2" +cosmic-text = "0.10" +lru = "0.11" [dev-dependencies] -winit = { version = "0.29.10", features = ["rwh_05"] } +winit = "0.28.7" pollster = "0.3.0" diff --git a/examples/hello-world.rs b/examples/hello-world.rs index daafbb3..1d4d2b7 100644 --- a/examples/hello-world.rs +++ b/examples/hello-world.rs @@ -11,12 +11,10 @@ use wgpu::{ use winit::{ dpi::LogicalSize, event::{Event, WindowEvent}, - event_loop::EventLoop, + event_loop::{ControlFlow, EventLoop}, window::WindowBuilder, }; -use std::sync::Arc; - fn main() { pollster::block_on(run()); } @@ -24,12 +22,12 @@ fn main() { async fn run() { // Set up window let (width, height) = (800, 600); - let event_loop = EventLoop::new().unwrap(); - let window = Arc::new(WindowBuilder::new() + let event_loop = EventLoop::new(); + let window = WindowBuilder::new() .with_inner_size(LogicalSize::new(width as f64, height as f64)) .with_title("glyphon hello world") .build(&event_loop) - .unwrap()); + .unwrap(); let size = window.inner_size(); let scale_factor = window.scale_factor(); @@ -43,15 +41,14 @@ async fn run() { .request_device( &DeviceDescriptor { label: None, - required_features: Features::empty(), - required_limits: Limits::downlevel_defaults(), + features: Features::empty(), + limits: Limits::downlevel_defaults(), }, None, ) .await .unwrap(); - - let surface = instance.create_surface(window.clone()).expect("Create surface"); + let surface = unsafe { instance.create_surface(&window) }.expect("Create surface"); let swapchain_format = TextureFormat::Bgra8UnormSrgb; let mut config = SurfaceConfiguration { usage: TextureUsages::RENDER_ATTACHMENT, @@ -61,7 +58,6 @@ async fn run() { present_mode: PresentMode::Fifo, alpha_mode: CompositeAlphaMode::Opaque, view_formats: vec![], - desired_maximum_frame_latency: 2, }; surface.configure(&device, &config); @@ -80,80 +76,84 @@ async fn run() { buffer.set_text(&mut font_system, "Hello world! 👋\nThis is rendered with 🦅 glyphon 🦁\nThe text below should be partially clipped.\na b c d e f g h i j k l m n o p q r s t u v w x y z", Attrs::new().family(Family::SansSerif), Shaping::Advanced); buffer.shape_until_scroll(&mut font_system); - event_loop - .run(move |event, target| { - if let Event::WindowEvent { - window_id: _, - event, - } = event - { - match event { - WindowEvent::Resized(size) => { - config.width = size.width; - config.height = size.height; - surface.configure(&device, &config); - window.request_redraw(); - } - WindowEvent::RedrawRequested => { - text_renderer - .prepare( - &device, - &queue, - &mut font_system, - &mut atlas, - Resolution { - width: config.width, - height: config.height, - }, - [TextArea { - buffer: &buffer, - left: 10.0, - top: 10.0, - scale: 1.0, - bounds: TextBounds { - left: 0, - top: 0, - right: 600, - bottom: 160, - }, - default_color: Color::rgb(255, 255, 255), - }], - &mut cache, - ) - .unwrap(); + event_loop.run(move |event, _, control_flow| { + let _ = (&instance, &adapter); - let frame = surface.get_current_texture().unwrap(); - let view = frame.texture.create_view(&TextureViewDescriptor::default()); - let mut encoder = device - .create_command_encoder(&CommandEncoderDescriptor { label: None }); - { - let mut pass = encoder.begin_render_pass(&RenderPassDescriptor { - label: None, - color_attachments: &[Some(RenderPassColorAttachment { - view: &view, - resolve_target: None, - ops: Operations { - load: LoadOp::Clear(wgpu::Color::BLACK), - store: wgpu::StoreOp::Store, - }, - })], - depth_stencil_attachment: None, - timestamp_writes: None, - occlusion_query_set: None, - }); - - text_renderer.render(&atlas, &mut pass).unwrap(); - } - - queue.submit(Some(encoder.finish())); - frame.present(); - - atlas.trim(); - } - WindowEvent::CloseRequested => target.exit(), - _ => {} - } + *control_flow = ControlFlow::Poll; + match event { + Event::WindowEvent { + event: WindowEvent::Resized(size), + .. + } => { + config.width = size.width; + config.height = size.height; + surface.configure(&device, &config); + window.request_redraw(); } - }) - .unwrap(); + Event::RedrawRequested(_) => { + text_renderer + .prepare( + &device, + &queue, + &mut font_system, + &mut atlas, + Resolution { + width: config.width, + height: config.height, + }, + [TextArea { + buffer: &buffer, + left: 10.0, + top: 10.0, + scale: 1.0, + bounds: TextBounds { + left: 0, + top: 0, + right: 600, + bottom: 160, + }, + default_color: Color::rgb(255, 255, 255), + }], + &mut cache, + ) + .unwrap(); + + let frame = surface.get_current_texture().unwrap(); + let view = frame.texture.create_view(&TextureViewDescriptor::default()); + let mut encoder = + device.create_command_encoder(&CommandEncoderDescriptor { label: None }); + { + let mut pass = encoder.begin_render_pass(&RenderPassDescriptor { + label: None, + color_attachments: &[Some(RenderPassColorAttachment { + view: &view, + resolve_target: None, + ops: Operations { + load: LoadOp::Clear(wgpu::Color::BLACK), + store: wgpu::StoreOp::Store, + }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + }); + + text_renderer.render(&atlas, &mut pass).unwrap(); + } + + queue.submit(Some(encoder.finish())); + frame.present(); + + atlas.trim(); + } + Event::WindowEvent { + event: WindowEvent::CloseRequested, + .. + } => *control_flow = ControlFlow::Exit, + Event::MainEventsCleared => { + window.request_redraw(); + } + _ => {} + } + }); } diff --git a/src/lib.rs b/src/lib.rs index 5b4dc51..64373ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,7 +51,7 @@ pub(crate) struct GlyphToRender { dim: [u16; 2], uv: [u16; 2], color: u32, - content_type_with_srgb: [u16; 2], + content_type: u32, depth: f32, } @@ -111,8 +111,6 @@ pub struct TextArea<'a> { /// The visible bounds of the text area. This is used to clip the text and doesn't have to /// match the `left` and `top` values. pub bounds: TextBounds, - /// The default color of the text area. + // The default color of the text area. pub default_color: Color, - /// The opacity to set the text area to (in gamma space) - pub opacity: f32, } diff --git a/src/shader.wgsl b/src/shader.wgsl index d7f7d86..53f78ce 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -4,7 +4,7 @@ struct VertexInput { @location(1) dim: u32, @location(2) uv: u32, @location(3) color: u32, - @location(4) content_type_with_srgb: u32, + @location(4) content_type: u32, @location(5) depth: f32, } @@ -32,14 +32,6 @@ var mask_atlas_texture: texture_2d; @group(0) @binding(3) var atlas_sampler: sampler; -fn srgb_to_linear(c: f32) -> f32 { - if c <= 0.04045 { - return c / 12.92; - } else { - return pow((c + 0.055) / 1.055, 2.4); - } -} - @vertex fn vs_main(in_vert: VertexInput) -> VertexOutput { var pos = in_vert.pos; @@ -77,31 +69,15 @@ fn vs_main(in_vert: VertexInput) -> VertexOutput { vert_output.position.y *= -1.0; - let content_type = in_vert.content_type_with_srgb & 0xffffu; - let srgb = (in_vert.content_type_with_srgb & 0xffff0000u) >> 16u; - - switch srgb { - case 0u: { - vert_output.color = vec4( - f32((color & 0x00ff0000u) >> 16u) / 255.0, - f32((color & 0x0000ff00u) >> 8u) / 255.0, - f32(color & 0x000000ffu) / 255.0, - f32((color & 0xff000000u) >> 24u) / 255.0, - ); - } - case 1u: { - vert_output.color = vec4( - srgb_to_linear(f32((color & 0x00ff0000u) >> 16u) / 255.0), - srgb_to_linear(f32((color & 0x0000ff00u) >> 8u) / 255.0), - srgb_to_linear(f32(color & 0x000000ffu) / 255.0), - f32((color & 0xff000000u) >> 24u) / 255.0, - ); - } - default: {} - } + vert_output.color = vec4( + f32((color & 0x00ff0000u) >> 16u), + f32((color & 0x0000ff00u) >> 8u), + f32(color & 0x000000ffu), + f32((color & 0xff000000u) >> 24u), + ) / 255.0; var dim: vec2 = vec2(0u); - switch content_type { + switch in_vert.content_type { case 0u: { dim = textureDimensions(color_atlas_texture); break; @@ -113,7 +89,7 @@ fn vs_main(in_vert: VertexInput) -> VertexOutput { default: {} } - vert_output.content_type = content_type; + vert_output.content_type = in_vert.content_type; vert_output.uv = vec2(uv) / vec2(dim); diff --git a/src/text_atlas.rs b/src/text_atlas.rs index 4110554..6bec57b 100644 --- a/src/text_atlas.rs +++ b/src/text_atlas.rs @@ -131,10 +131,8 @@ impl InnerAtlas { return false; } - // Grow each dimension by a factor of 2. The growth factor was chosen to match the growth - // factor of `Vec`.` - const GROWTH_FACTOR: u32 = 2; - let new_size = (self.size * GROWTH_FACTOR).min(self.max_texture_dimension_2d); + // TODO: Better resizing logic (?) + let new_size = (self.size + Self::INITIAL_SIZE).min(self.max_texture_dimension_2d); self.packer.grow(size2(new_size as i32, new_size as i32)); @@ -271,7 +269,6 @@ pub struct TextAtlas { pub(crate) shader: ShaderModule, pub(crate) vertex_buffers: [wgpu::VertexBufferLayout<'static>; 1], pub(crate) format: TextureFormat, - pub(crate) color_mode: ColorMode, } impl TextAtlas { @@ -451,7 +448,6 @@ impl TextAtlas { shader, vertex_buffers, format, - color_mode, } } diff --git a/src/text_render.rs b/src/text_render.rs index 8a1a595..a1395d3 100644 --- a/src/text_render.rs +++ b/src/text_render.rs @@ -1,6 +1,6 @@ use crate::{ - ColorMode, FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, Params, PrepareError, - RenderError, Resolution, SwashCache, SwashContent, TextArea, TextAtlas, + FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, Params, PrepareError, RenderError, + Resolution, SwashCache, SwashContent, TextArea, TextAtlas, }; use std::{iter, mem::size_of, slice, sync::Arc}; use wgpu::{ @@ -268,13 +268,6 @@ impl TextRenderer { None => text_area.default_color, }; - let color = cosmic_text::Color::rgba( - (color.r() as f32 * text_area.opacity + 0.5) as u8, - (color.g() as f32 * text_area.opacity + 0.5) as u8, - (color.b() as f32 * text_area.opacity + 0.5) as u8, - (color.a() as f32 * text_area.opacity + 0.5) as u8, - ); - let depth = metadata_to_depth(glyph.metadata); glyph_vertices.extend( @@ -283,13 +276,7 @@ impl TextRenderer { dim: [width as u16, height as u16], uv: [atlas_x, atlas_y], color: color.0, - content_type_with_srgb: [ - content_type as u16, - match atlas.color_mode { - ColorMode::Accurate => TextColorConversion::ConvertToLinear, - ColorMode::Web => TextColorConversion::None, - } as u16, - ], + content_type: content_type as u32, depth, }) .take(4), @@ -418,20 +405,13 @@ impl TextRenderer { } } -#[repr(u16)] +#[repr(u32)] #[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum ContentType { Color = 0, Mask = 1, } -#[repr(u16)] -#[derive(Debug, Clone, Copy, Eq, PartialEq)] -enum TextColorConversion { - None = 0, - ConvertToLinear = 1, -} - fn next_copy_buffer_size(size: u64) -> u64 { let align_mask = COPY_BUFFER_ALIGNMENT - 1; ((size.next_power_of_two() + align_mask) & !align_mask).max(COPY_BUFFER_ALIGNMENT)