From c25f394c1648db228f3967b09d580ead49b3b7e5 Mon Sep 17 00:00:00 2001 From: grovesNL Date: Tue, 14 Feb 2023 14:44:09 -0330 Subject: [PATCH] Allow depth to be attached to text using metadata --- src/lib.rs | 1 + src/shader.wgsl | 5 +++-- src/text_atlas.rs | 5 +++++ src/text_render.rs | 42 ++++++++++++++++++++++++++++++++++++------ 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e15900c..13baf4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,6 +44,7 @@ pub(crate) struct GlyphToRender { uv: [u16; 2], color: u32, content_type: u32, + depth: f32, } /// The screen resolution to use when rendering text. diff --git a/src/shader.wgsl b/src/shader.wgsl index e69763b..bcb03ec 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -5,10 +5,11 @@ struct VertexInput { @location(2) uv: u32, @location(3) color: u32, @location(4) content_type: u32, + @location(5) depth: f32, } struct VertexOutput { - @builtin(position) position: vec4, + @invariant @builtin(position) position: vec4, @location(0) color: vec4, @location(1) uv: vec2, @location(2) content_type: u32, @@ -62,7 +63,7 @@ fn vs_main(in_vert: VertexInput) -> VertexOutput { vert_output.position = vec4( 2.0 * vec2(pos) / vec2(params.screen_resolution) - 1.0, - 0.0, + in_vert.depth, 1.0, ); diff --git a/src/text_atlas.rs b/src/text_atlas.rs index ea18a3f..075cff0 100644 --- a/src/text_atlas.rs +++ b/src/text_atlas.rs @@ -151,6 +151,11 @@ impl TextAtlas { offset: size_of::() as u64 * 5, shader_location: 4, }, + wgpu::VertexAttribute { + format: VertexFormat::Float32, + offset: size_of::() as u64 * 6, + shader_location: 5, + }, ], }]; diff --git a/src/text_render.rs b/src/text_render.rs index c8ba554..aab8b4e 100644 --- a/src/text_render.rs +++ b/src/text_render.rs @@ -63,7 +63,7 @@ impl TextRenderer { } /// Prepares all of the provided text areas for rendering. - pub fn prepare<'a, 'b: 'a>( + pub fn prepare_with_depth<'a, 'b: 'a>( &mut self, device: &Device, queue: &Queue, @@ -72,6 +72,7 @@ impl TextRenderer { text_areas: &[TextArea<'a, 'b>], default_color: Color, cache: &mut SwashCache, + mut metadata_to_depth: impl FnMut(usize) -> f32, ) -> Result<(), PrepareError> { self.screen_resolution = screen_resolution; @@ -258,11 +259,6 @@ impl TextRenderer { let line_y = run.line_y; for glyph in run.glyphs.iter() { - let color = match glyph.color_opt { - Some(some) => some, - None => default_color, - }; - let details = atlas.glyph(&glyph.cache_key).unwrap(); let mut x = glyph.x_int + details.left as i32 + text_area.left; @@ -321,6 +317,13 @@ impl TextRenderer { height = bounds_max_y - y; } + let color = match glyph.color_opt { + Some(some) => some, + None => default_color, + }; + + let depth = metadata_to_depth(glyph.metadata); + glyph_vertices.extend( iter::repeat(GlyphToRender { pos: [x, y], @@ -328,6 +331,7 @@ impl TextRenderer { uv: [atlas_x, atlas_y], color: color.0, content_type: content_type as u32, + depth, }) .take(4), ); @@ -406,6 +410,28 @@ impl TextRenderer { Ok(()) } + pub fn prepare<'a, 'b: 'a>( + &mut self, + device: &Device, + queue: &Queue, + atlas: &mut TextAtlas, + screen_resolution: Resolution, + text_areas: &[TextArea<'a, 'b>], + default_color: Color, + cache: &mut SwashCache, + ) -> Result<(), PrepareError> { + self.prepare_with_depth( + device, + queue, + atlas, + screen_resolution, + text_areas, + default_color, + cache, + zero_depth, + ) + } + /// Renders all layouts that were previously provided to `prepare`. pub fn render<'pass>( &'pass mut self, @@ -469,3 +495,7 @@ fn create_oversized_buffer( buffer.unmap(); (buffer, size) } + +fn zero_depth(_: usize) -> f32 { + 0f32 +}