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

Improve perf of text in keyboard renderer #197

Merged
merged 1 commit into from
Sep 14, 2024
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
57 changes: 36 additions & 21 deletions neothesia-core/src/render/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct KeyboardRenderer {
layout: piano_math::KeyboardLayout,

cache: Vec<QuadInstance>,
text_cache: Vec<super::text::TextArea>,
}

impl KeyboardRenderer {
Expand All @@ -37,6 +38,7 @@ impl KeyboardRenderer {

layout,
cache,
text_cache: Vec::new(),
}
}

Expand Down Expand Up @@ -82,11 +84,12 @@ impl KeyboardRenderer {

pub fn invalidate_cache(&mut self) {
self.cache.clear();
self.text_cache.clear();
}

/// Reupload instances to GPU
#[profiling::function]
fn reupload(&mut self) {
fn rebuild_quad_cache(&mut self) {
let instances = &mut self.cache;

// black_background
Expand Down Expand Up @@ -118,19 +121,7 @@ impl KeyboardRenderer {
}

#[profiling::function]
pub fn update(&mut self, quads: &mut QuadPipeline, layer: usize, text: &mut TextRenderer) {
if self.cache.is_empty() {
self.reupload();
}

{
profiling::scope!("push from cache");
for quad in self.cache.iter() {
quads.instances(layer).push(*quad);
}
}

profiling::scope!("push text");
fn rebuild_text_cache(&mut self, font_system: &mut glyphon::FontSystem) {
let range_start = self.layout.range.start() as usize;
for key in self.layout.keys.iter().filter(|key| key.note_id() == 0) {
let x = self.pos.x + key.x();
Expand All @@ -143,20 +134,19 @@ impl KeyboardRenderer {

let oct_number = (key.id() + range_start) / 12;

let mut buffer =
glyphon::Buffer::new(text.font_system(), glyphon::Metrics::new(size, size));
buffer.set_size(text.font_system(), Some(w), Some(h));
buffer.set_wrap(text.font_system(), glyphon::Wrap::None);
let mut buffer = glyphon::Buffer::new(font_system, glyphon::Metrics::new(size, size));
buffer.set_size(font_system, Some(w), Some(h));
buffer.set_wrap(font_system, glyphon::Wrap::None);
buffer.set_text(
text.font_system(),
font_system,
&format!("C{}", oct_number as i8 - 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(), false);
buffer.shape_until_scroll(font_system, false);

text.queue(super::text::TextArea {
self.text_cache.push(super::text::TextArea {
buffer,
left: x,
top: y + h - size * 1.2,
Expand All @@ -171,4 +161,29 @@ impl KeyboardRenderer {
});
}
}

#[profiling::function]
pub fn update(&mut self, quads: &mut QuadPipeline, layer: usize, text: &mut TextRenderer) {
if self.cache.is_empty() {
self.rebuild_quad_cache();
}

if self.text_cache.is_empty() {
self.rebuild_text_cache(text.font_system());
}

{
profiling::scope!("push quads from cache");
for quad in self.cache.iter() {
quads.instances(layer).push(*quad);
}
}

{
profiling::scope!("push text from cache");
for buffer in self.text_cache.iter() {
text.queue(buffer.clone());
}
}
}
}
1 change: 1 addition & 0 deletions neothesia-core/src/render/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use wgpu_jumpstart::Gpu;

pub use glyphon;

#[derive(Debug, Clone)]
pub struct TextArea {
pub buffer: glyphon::Buffer,
/// The left edge of the buffer.
Expand Down
Loading