Skip to content

Commit

Permalink
Use fast border algorithm, fix freezes
Browse files Browse the repository at this point in the history
  • Loading branch information
zmerp committed Nov 2, 2024
1 parent 96a8413 commit 314788f
Showing 1 changed file with 28 additions and 43 deletions.
71 changes: 28 additions & 43 deletions alvr/client_core/src/graphics/lobby.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{GraphicsContext, SDR_FORMAT};
use alvr_common::{
glam::{Mat4, Quat, UVec2, Vec3, Vec4},
glam::{IVec2, Mat4, Quat, UVec2, Vec3, Vec4},
Fov, Pose,
};
use glyph_brush_layout::{
Expand All @@ -25,16 +25,17 @@ const HUD_SIDE: f32 = 3.5;
const HUD_TEXTURE_SIDE: usize = 1024;
const FONT_SIZE: f32 = 50.0;

const OPACITY_KERNEL_SIDE: usize = 7;
const OPACITY_BORDER_KERNEL: [f32; OPACITY_KERNEL_SIDE * OPACITY_KERNEL_SIDE] = [
0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, //
0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, //
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, //
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, //
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, //
0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, //
0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, //
const FAST_BORDER_OFFSETS: [IVec2; 8] = [
IVec2::new(0, -3),
IVec2::new(2, -2),
IVec2::new(3, 0),
IVec2::new(2, 2),
IVec2::new(0, 3),
IVec2::new(-2, 2),
IVec2::new(-3, 0),
IVec2::new(-2, -2),
];
const MAX_BORDER_OFFSET: i32 = 3;

const HAND_SKELETON_BONES: [(usize, usize); 19] = [
// Thumb
Expand Down Expand Up @@ -314,51 +315,35 @@ impl LobbyRenderer {

let mut buffer = vec![0; HUD_TEXTURE_SIDE * HUD_TEXTURE_SIDE * 4];

// Render characters
for section_glyph in section_glyphs {
if let Some(outlined) = scaled_font.outline_glyph(section_glyph.glyph) {
let bounds = outlined.px_bounds();

outlined.draw(|x, y, alpha| {
let x = x as usize + bounds.min.x as usize;
let y = y as usize + bounds.min.y as usize;
if x < HUD_TEXTURE_SIDE && y < HUD_TEXTURE_SIDE {
let coord = (y * HUD_TEXTURE_SIDE + x) * 4;
let x = x as i32 + bounds.min.x as i32;
let y = y as i32 + bounds.min.y as i32;

if x >= MAX_BORDER_OFFSET
&& y >= MAX_BORDER_OFFSET
&& x < HUD_TEXTURE_SIDE as i32 - MAX_BORDER_OFFSET
&& y < HUD_TEXTURE_SIDE as i32 - MAX_BORDER_OFFSET
{
let coord = (y as usize * HUD_TEXTURE_SIDE + x as usize) * 4;
let value = (alpha * 255.0) as u8;

buffer[coord] = value;
buffer[coord + 1] = value;
buffer[coord + 2] = value;
}
});
}
}

// Render opacity
const KERNEL_HALF_SIDE: i32 = OPACITY_KERNEL_SIDE as i32 / 2;
for cy in 0..HUD_TEXTURE_SIDE as i32 {
for cx in 0..HUD_TEXTURE_SIDE as i32 {
let mut max_opacity = 0;
for off_y in 0..OPACITY_KERNEL_SIDE as i32 {
for off_x in 0..OPACITY_KERNEL_SIDE as i32 {
let tex_coord = ((cy + off_y - KERNEL_HALF_SIDE) * HUD_TEXTURE_SIDE as i32
+ (cx + off_x - KERNEL_HALF_SIDE))
* 4;
if tex_coord >= 0 && tex_coord < buffer.len() as i32 {
// copy opacity from red channel
let kernel_coord =
off_y as usize * OPACITY_KERNEL_SIDE + off_x as usize;
max_opacity = u8::max(
(buffer[tex_coord as usize] as f32
* OPACITY_BORDER_KERNEL[kernel_coord])
as u8,
max_opacity,
);
// Render opacity with border
for offset in &FAST_BORDER_OFFSETS {
let coord = ((y + offset.y) as usize * HUD_TEXTURE_SIDE
+ (x + offset.x) as usize)
* 4;
buffer[coord + 3] = u8::max(buffer[coord + 3], value);
}
}
}

let coord = (cy as usize * HUD_TEXTURE_SIDE + cx as usize) * 4;
buffer[coord as usize + 3] = max_opacity;
});
}
}

Expand Down

0 comments on commit 314788f

Please sign in to comment.