Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement font rendering for wgpu backend #18

Merged
merged 1 commit into from
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ features = ["opengl", "debug"]
[features]
default = []
opengl = ["gfx", "gfx_core", "glutin", "gfx_device_gl", "gfx_window_glutin", "gfx_glyph", "gfx_winit"]
vulkan = ["wgpu", "wgpu/vulkan"]
metal = ["wgpu", "wgpu/metal"]
dx11 = ["wgpu", "wgpu/dx11"]
dx12 = ["wgpu", "wgpu/dx12"]
vulkan = ["wgpu", "wgpu/vulkan", "wgpu_glyph"]
metal = ["wgpu", "wgpu/metal", "wgpu_glyph"]
dx11 = ["wgpu", "wgpu/dx11", "wgpu_glyph"]
dx12 = ["wgpu", "wgpu/dx12", "wgpu_glyph"]
debug = []

[dependencies]
Expand All @@ -42,6 +42,7 @@ gfx_winit = { package = "winit", version = "0.19", optional = true }

# wgpu (Vulkan, Metal, D3D)
wgpu = { version = "0.2", optional = true }
wgpu_glyph = { version = "0.1", optional = true }

[dev-dependencies]
# Example dependencies
Expand Down
2 changes: 2 additions & 0 deletions src/graphics/backend_gfx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ impl Gpu {
font: &mut Font,
target: &TargetView,
depth: &DepthView,
_target_width: u32,
_target_height: u32,
Copy link
Owner Author

@hecrj hecrj Apr 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think glyph_brush can be improved to be target size agnostic. I will try to make a PR to glyph_brush with the idea, but there will be breaking changes.

If all goes well, it should allow us to support font rendering on any Target, with custom Transformation support.

Copy link
Owner Author

@hecrj hecrj Apr 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related issue: alexheretic/glyph-brush#63.

) {
font.draw(&mut self.encoder, target, depth);
}
Expand Down
38 changes: 34 additions & 4 deletions src/graphics/backend_wgpu/font.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
use crate::graphics::gpu::TargetView;
use crate::graphics::Text;

pub struct Font {
//glyphs: gfx_glyph::GlyphBrush<'static, gl::Resources, gl::Factory>,
glyphs: wgpu_glyph::GlyphBrush<'static>,
}

impl Font {
pub fn from_bytes(_bytes: &'static [u8]) -> Font {
Font {}
pub fn from_bytes(device: &mut wgpu::Device, bytes: &'static [u8]) -> Font {
Font {
glyphs: wgpu_glyph::GlyphBrushBuilder::using_font_bytes(bytes)
.texture_filter_method(wgpu::FilterMode::Nearest)
.build(device),
}
}

pub fn add(&mut self, _text: Text) {}
pub fn add(&mut self, text: Text) {
self.glyphs.queue(wgpu_glyph::Section {
text: &text.content,
screen_position: (text.position.x, text.position.y),
scale: wgpu_glyph::Scale {
x: text.size,
y: text.size,
},
color: text.color.into(),
bounds: text.bounds,
..Default::default()
});
}

pub fn draw(
&mut self,
device: &mut wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
target: &TargetView,
target_width: u32,
target_height: u32,
) {
self.glyphs
.draw_queued(device, encoder, target, target_width, target_height)
.expect("Draw font");
}
}
15 changes: 12 additions & 3 deletions src/graphics/backend_wgpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Gpu {
}

pub(super) fn upload_font(&mut self, bytes: &'static [u8]) -> Font {
Font::from_bytes(bytes)
Font::from_bytes(&mut self.device, bytes)
}

pub(super) fn draw_texture_quads(
Expand All @@ -120,9 +120,18 @@ impl Gpu {

pub(super) fn draw_font(
&mut self,
_font: &mut Font,
_target: &TargetView,
font: &mut Font,
target: &TargetView,
_depth: &DepthView,
target_width: u32,
target_height: u32,
) {
font.draw(
&mut self.device,
&mut self.encoder,
target,
target_width,
target_height,
);
}
}
2 changes: 2 additions & 0 deletions src/graphics/window/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ impl<'a> Frame<'a> {
font,
&self.window.surface.target(),
&self.window.surface.depth(),
self.width().round() as u32,
self.height().round() as u32,
);
}
}