Allow depth to be attached to text using metadata

This commit is contained in:
grovesNL 2023-02-14 14:44:09 -03:30 committed by Josh Groves
parent 74e9aa37a1
commit c25f394c16
4 changed files with 45 additions and 8 deletions

View file

@ -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.

View file

@ -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<f32>,
@invariant @builtin(position) position: vec4<f32>,
@location(0) color: vec4<f32>,
@location(1) uv: vec2<f32>,
@location(2) content_type: u32,
@ -62,7 +63,7 @@ fn vs_main(in_vert: VertexInput) -> VertexOutput {
vert_output.position = vec4<f32>(
2.0 * vec2<f32>(pos) / vec2<f32>(params.screen_resolution) - 1.0,
0.0,
in_vert.depth,
1.0,
);

View file

@ -151,6 +151,11 @@ impl TextAtlas {
offset: size_of::<u32>() as u64 * 5,
shader_location: 4,
},
wgpu::VertexAttribute {
format: VertexFormat::Float32,
offset: size_of::<u32>() as u64 * 6,
shader_location: 5,
},
],
}];

View file

@ -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
}