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

Update wgpu & iced #88

Merged
merged 3 commits into from
Sep 28, 2023
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
223 changes: 88 additions & 135 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ default-members = ["neothesia"]
resolver = "2"

[workspace.dependencies]
wgpu = "0.16.1"
wgpu = "0.17.1"
glyphon = { git = "https://github.com/grovesNL/glyphon.git", rev = "20f0f8fa80e0d0df4c63634ce9176fa489546ca9" }
log = "0.4"
bytemuck = { version = "1.5", features = ["derive"] }
env_logger = "0.10"
Expand Down
5 changes: 2 additions & 3 deletions neothesia-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ impl Recorder {
self.waterfall
.update(&self.gpu.queue, time_without_lead_in(&self.playback));

self.keyboard
.update(&self.gpu.queue, self.text.glyph_brush());
self.keyboard.update(&self.gpu.queue, &mut self.text);
}

fn render(
Expand Down Expand Up @@ -151,7 +150,7 @@ impl Recorder {
}

self.text
.render((self.width as f32, self.height as f32), &mut self.gpu, view);
.render((self.width, self.height), &mut self.gpu, view);

{
let u32_size = std::mem::size_of::<u32>() as u32;
Expand Down
2 changes: 1 addition & 1 deletion neothesia-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ serde = { version = "1.0", features = ["serde_derive"] }

bytemuck = { workspace = true }
wgpu = { workspace = true }
wgpu_glyph = "0.20.0"
glyphon = { workspace = true }
wgpu-jumpstart = { workspace = true }

piano-math = { workspace = true }
Expand Down
41 changes: 29 additions & 12 deletions neothesia-core/src/render/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use crate::{
};

use piano_math::range::KeyboardRange;
use wgpu_glyph::{GlyphBrush, Section};

mod key_state;
pub use key_state::KeyState;
use wgpu_jumpstart::Gpu;

use super::TextRenderer;

pub struct KeyboardRenderer {
pos: Point<f32>,

Expand Down Expand Up @@ -126,7 +127,7 @@ impl KeyboardRenderer {
self.should_reupload = false;
}

pub fn update(&mut self, queue: &wgpu::Queue, brush: &mut GlyphBrush<()>) {
pub fn update(&mut self, queue: &wgpu::Queue, text: &mut TextRenderer) {
if self.should_reupload {
self.reupload(queue);
}
Expand All @@ -146,16 +147,32 @@ impl KeyboardRenderer {

let size = w * 0.7;

brush.queue(Section {
screen_position: (x + w / 2.0, y + h - size * 1.2),
text: vec![wgpu_glyph::Text::new(&format!("C{}", id + 1))
.with_color([0.6, 0.6, 0.6, 1.0])
.with_scale(size)],
bounds: (w, f32::INFINITY),
layout: wgpu_glyph::Layout::default()
.h_align(wgpu_glyph::HorizontalAlign::Center)
.v_align(wgpu_glyph::VerticalAlign::Top),
})
let mut buffer =
glyphon::Buffer::new(text.font_system(), glyphon::Metrics::new(size, size));
buffer.set_size(text.font_system(), w, h);
buffer.set_wrap(text.font_system(), glyphon::Wrap::None);
buffer.set_text(
text.font_system(),
&format!("C{}", id + 1),
glyphon::Attrs::new().family(glyphon::Family::SansSerif),
glyphon::Shaping::Basic,
);
buffer.lines[0].set_align(Some(glyphon::cosmic_text::Align::Center));
buffer.shape_until_scroll(text.font_system());

text.queue(super::text::TextArea {
buffer,
left: x,
top: y + h - size * 1.2,
scale: 1.0,
bounds: glyphon::TextBounds {
left: x.round() as i32,
top: y.round() as i32,
right: x.round() as i32 + w.round() as i32,
bottom: y.round() as i32 + h.round() as i32,
},
default_color: glyphon::Color::rgb(153, 153, 153),
});
}
}

Expand Down
174 changes: 138 additions & 36 deletions neothesia-core/src/render/text/mod.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,163 @@
use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, Section};
use std::sync::Arc;

use wgpu_jumpstart::Gpu;

pub use glyphon;

pub struct TextArea {
pub buffer: glyphon::Buffer,
/// The left edge of the buffer.
pub left: f32,
/// The top edge of the buffer.
pub top: f32,
/// The scaling to apply to the buffer.
pub scale: f32,
/// 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: glyphon::TextBounds,
// The default color of the text area.
pub default_color: glyphon::Color,
}

pub struct TextRenderer {
glyph_brush: GlyphBrush<()>,
font_system: glyphon::FontSystem,
cache: glyphon::SwashCache,
atlas: glyphon::TextAtlas,
text_renderer: glyphon::TextRenderer,

queue: Vec<TextArea>,
}

impl TextRenderer {
pub fn new(gpu: &Gpu) -> Self {
let font =
wgpu_glyph::ab_glyph::FontArc::try_from_slice(include_bytes!("./Roboto-Regular.ttf"))
.expect("Load font");
let glyph_brush =
GlyphBrushBuilder::using_font(font).build(&gpu.device, gpu.texture_format);
let font_system = glyphon::FontSystem::new_with_fonts(
[glyphon::fontdb::Source::Binary(Arc::new(include_bytes!(
"./Roboto-Regular.ttf"
)))]
.into_iter(),
);

let cache = glyphon::SwashCache::new();
let mut atlas = glyphon::TextAtlas::new(&gpu.device, &gpu.queue, gpu.texture_format);
let text_renderer = glyphon::TextRenderer::new(
&mut atlas,
&gpu.device,
wgpu::MultisampleState::default(),
None,
);

Self { glyph_brush }
Self {
font_system,
cache,
atlas,
text_renderer,
queue: Vec::new(),
}
}

pub fn glyph_brush(&mut self) -> &mut GlyphBrush<()> {
&mut self.glyph_brush
pub fn font_system(&mut self) -> &mut glyphon::FontSystem {
&mut self.font_system
}

pub fn queue_text(&mut self, section: Section) {
self.glyph_brush.queue(section);
pub fn atlas(&mut self) -> &mut glyphon::TextAtlas {
&mut self.atlas
}

pub fn queue_fps(&mut self, fps: f64) {
let s = format!("FPS: {}", fps.round() as u32);
let text = vec![wgpu_glyph::Text::new(&s)
.with_color([1.0, 1.0, 1.0, 1.0])
.with_scale(20.0)];
pub fn queue(&mut self, area: TextArea) {
self.queue.push(area);
}

self.queue_text(Section {
pub fn queue_text(&mut self, text: &str) {
let mut buffer =
glyphon::Buffer::new(&mut self.font_system, glyphon::Metrics::new(15.0, 15.0));
buffer.set_size(&mut self.font_system, f32::MAX, f32::MAX);
buffer.set_text(
&mut self.font_system,
text,
screen_position: (0.0, 5.0),
layout: wgpu_glyph::Layout::Wrap {
line_breaker: Default::default(),
h_align: wgpu_glyph::HorizontalAlign::Left,
v_align: wgpu_glyph::VerticalAlign::Top,
},
..Default::default()
glyphon::Attrs::new().family(glyphon::Family::SansSerif),
glyphon::Shaping::Basic,
);
buffer.shape_until_scroll(&mut self.font_system);

#[cfg(debug_assertions)]
let top = 20.0;
#[cfg(not(debug_assertions))]
let top = 5.0;

self.queue(TextArea {
buffer,
left: 0.0,
top,
scale: 1.0,
bounds: glyphon::TextBounds::default(),
default_color: glyphon::Color::rgb(255, 255, 255),
});
}

pub fn render(&mut self, logical_size: (f32, f32), gpu: &mut Gpu, view: &wgpu::TextureView) {
let encoder = &mut gpu.encoder;
pub fn queue_fps(&mut self, fps: f64) {
let text = format!("FPS: {}", fps.round() as u32);
let mut buffer =
glyphon::Buffer::new(&mut self.font_system, glyphon::Metrics::new(15.0, 15.0));
buffer.set_size(&mut self.font_system, f32::MAX, f32::MAX);
buffer.set_text(
&mut self.font_system,
&text,
glyphon::Attrs::new().family(glyphon::Family::SansSerif),
glyphon::Shaping::Basic,
);
buffer.shape_until_scroll(&mut self.font_system);

let (window_w, window_h) = logical_size;
self.queue(TextArea {
buffer,
left: 0.0,
top: 5.0,
scale: 1.0,
bounds: glyphon::TextBounds::default(),
default_color: glyphon::Color::rgb(255, 255, 255),
});
}

self.glyph_brush
.draw_queued(
pub fn render(&mut self, logical_size: (u32, u32), gpu: &mut Gpu, view: &wgpu::TextureView) {
let elements = self.queue.iter().map(|area| glyphon::TextArea {
buffer: &area.buffer,
left: area.left,
top: area.top,
scale: area.scale,
bounds: area.bounds,
default_color: area.default_color,
});

self.text_renderer
.prepare(
&gpu.device,
&mut gpu.staging_belt,
encoder,
view,
window_w.round() as u32,
window_h.round() as u32,
&gpu.queue,
&mut self.font_system,
&mut self.atlas,
glyphon::Resolution {
width: logical_size.0,
height: logical_size.1,
},
elements,
&mut self.cache,
)
.expect("glyph_brush");
.unwrap();

// TODO: Use single pass, now that this is posible thanks to glyphon
let mut pass = gpu.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("glyphon text"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: true,
},
})],
depth_stencil_attachment: None,
});

self.text_renderer.render(&self.atlas, &mut pass).unwrap();

self.queue.clear();
}
}
13 changes: 6 additions & 7 deletions neothesia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ serde = { version = "1.0", features = ["serde_derive"] }
num = "0.4"

wgpu = { workspace = true }
wgpu_glyph = "0.20.0"
wgpu-jumpstart = { workspace = true }

neothesia-core = { workspace = true }
Expand All @@ -39,12 +38,12 @@ oxisynth = { version = "0.0.3", optional = true }
midi-file = { workspace = true }
midi-io = { path = "../midi-io" }

iced_style = "0.9"
iced_graphics = "0.9"
iced_core = "0.10"
iced_runtime = "0.1.0"
iced_wgpu = { version = "0.11", features = ["image"] }
iced_widget = { version = "0.1", features = ["image"] }
iced_style = {git = "https://github.com/iced-rs/iced.git", rev="bc9bb28b1ccd1248d63ccdfef2f57d7aa837abbb"}
iced_graphics = {git = "https://github.com/iced-rs/iced.git", rev="bc9bb28b1ccd1248d63ccdfef2f57d7aa837abbb"}
iced_core = {git = "https://github.com/iced-rs/iced.git", rev="bc9bb28b1ccd1248d63ccdfef2f57d7aa837abbb"}
iced_runtime = {git = "https://github.com/iced-rs/iced.git", rev="bc9bb28b1ccd1248d63ccdfef2f57d7aa837abbb"}
iced_wgpu = { git = "https://github.com/iced-rs/iced.git", rev="bc9bb28b1ccd1248d63ccdfef2f57d7aa837abbb", features = ["image"] }
iced_widget = { git = "https://github.com/iced-rs/iced.git", rev="bc9bb28b1ccd1248d63ccdfef2f57d7aa837abbb", features = ["image"] }

[[bin]]
name = "neothesia"
11 changes: 5 additions & 6 deletions neothesia/src/iced_utils/iced_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ impl IcedManager {

let settings = iced_wgpu::Settings::default();

let renderer = iced_wgpu::Renderer::new(iced_wgpu::Backend::new(
device,
queue,
settings,
texture_format,
));
let renderer = iced_wgpu::Renderer::new(
iced_wgpu::Backend::new(device, queue, settings, texture_format),
iced_core::Font::default(),
iced_core::Pixels(16.0),
);

let viewport = iced_wgpu::graphics::Viewport::with_physical_size(
iced_core::Size::new(physical_size.0, physical_size.1),
Expand Down
6 changes: 4 additions & 2 deletions neothesia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,17 @@ impl Neothesia {

self.target.text_renderer.render(
(
self.target.window_state.logical_size.width,
self.target.window_state.logical_size.height,
self.target.window_state.physical_size.width,
self.target.window_state.physical_size.height,
),
&mut self.target.gpu,
view,
);

self.target.gpu.submit();
frame.present();

self.target.text_renderer.atlas().trim();
}
}

Expand Down
Loading
Loading