Roughly doubled the performance of the hexagon launcher. #11

Closed
MayaTheShy wants to merge 8 commits from dev into dev
2 changed files with 458 additions and 207 deletions

View File

@@ -6,7 +6,7 @@ use mint::{Quaternion, Vector3};
use protostar::xdg::{DesktopFile, get_desktop_files}; use protostar::xdg::{DesktopFile, get_desktop_files};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use single::{APP_SIZE, App, BTN_COLOR, BTN_SELECTED_COLOR, MODEL_SCALE}; use single::{APP_SIZE, App, BTN_COLOR, BTN_SELECTED_COLOR, MODEL_SCALE, DEFAULT_HEX_COLOR};
use stardust_xr_asteroids::{ use stardust_xr_asteroids::{
ClientState, CustomElement, Element, Migrate, Reify, Transformable, client, ClientState, CustomElement, Element, Migrate, Reify, Transformable, client,
elements::{Button, Grabbable, Model, ModelPart, PointerMode, Spatial}, elements::{Button, Grabbable, Model, ModelPart, PointerMode, Spatial},
@@ -17,13 +17,42 @@ use stardust_xr_fusion::{
project_local_resources, project_local_resources,
spatial::Transform, spatial::Transform,
}; };
use stardust_xr_fusion::values::ResourceID;
use std::path::PathBuf;
use std::f32::consts::{FRAC_PI_2, PI}; use std::f32::consts::{FRAC_PI_2, PI};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::atomic::AtomicU64;
use tokio::time::Duration;
static REIFY_COUNT: AtomicUsize = AtomicUsize::new(0);
static REIFY_TOTAL_NS: AtomicU64 = AtomicU64::new(0);
static APP_REIFY_COUNT: AtomicUsize = AtomicUsize::new(0);
static VISIBLE_LIMIT: AtomicUsize = AtomicUsize::new(0);
const VISIBLE_STEP: usize = 12;
use tracing_subscriber::{EnvFilter, Layer, layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{EnvFilter, Layer, layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() { async fn main() {
color_eyre::install().unwrap(); color_eyre::install().unwrap();
// spawn a background logger that prints reify calls per second
tokio::spawn(async {
loop {
tokio::time::sleep(Duration::from_secs(1)).await;
let v = REIFY_COUNT.swap(0, Ordering::Relaxed);
let total_ns = REIFY_TOTAL_NS.swap(0, Ordering::Relaxed);
let app_hits = APP_REIFY_COUNT.swap(0, Ordering::Relaxed);
let avg_ns = if v > 0 { total_ns / (v as u64) } else { 0 };
tracing::info!(
reify_per_sec = v,
avg_reify_ms = (avg_ns as f64) / 1_000_000.0,
app_reify_hits = app_hits,
"hexagon reify stats"
);
}
});
let registry = tracing_subscriber::registry(); let registry = tracing_subscriber::registry();
#[cfg(feature = "tracy")] #[cfg(feature = "tracy")]
let registry = registry.with({ let registry = registry.with({
@@ -50,6 +79,19 @@ pub struct HexagonLauncher {
#[serde(skip)] #[serde(skip)]
/// position in the vector is mapped to hex coordinates /// position in the vector is mapped to hex coordinates
apps: Vec<App>, apps: Vec<App>,
#[serde(skip)]
/// cached world coordinates for each app hex
positions: Vec<[f32; 3]>,
#[serde(skip)]
/// lightweight immutable snapshots for fast per-frame reify
snapshots: Vec<Snapshot>,
}
#[derive(Debug, Clone)]
struct Snapshot {
name: String,
cached_texture: Option<ResourceID>,
cached_gltf: Option<PathBuf>,
} }
impl Default for HexagonLauncher { impl Default for HexagonLauncher {
@@ -59,6 +101,8 @@ impl Default for HexagonLauncher {
pos: [0.0; 3].into(), pos: [0.0; 3].into(),
rot: Quat::IDENTITY.into(), rot: Quat::IDENTITY.into(),
apps: Vec::new(), apps: Vec::new(),
positions: Vec::new(),
snapshots: Vec::new(),
} }
} }
} }
@@ -77,20 +121,70 @@ impl ClientState for HexagonLauncher {
.filter_map(|d| App::new(d).ok()) .filter_map(|d| App::new(d).ok())
.collect(); .collect();
self.apps.par_iter().for_each(|app| {
app.load_icon();
});
// Sort by name // Sort by name
self.apps self.apps
.sort_by_key(|app| app.app.name().unwrap_or_default().to_string()); .sort_by_key(|app| app.app.name().unwrap_or_default().to_string());
// precompute coordinates for each app to avoid recomputing per-reify
self.positions = (0..self.apps.len())
.map(|i| Hex::spiral(i + 1).get_coords())
.collect();
// Preload icons/resources off the reify path so create_model() is cheap later.
// Use rayon to parallelize filesystem/processing work.
self.apps
.par_iter()
.for_each(|app| {
// idempotent: App::load_icon uses OnceLock internally
app.load_icon();
});
// Warm / prebuild heavy Model resources in parallel so the renderer
// doesn't pay parsing/creation cost during reify.
//
// We do this after load_icon above so cached_gltf / cached_texture are populated.
self.apps.par_iter().for_each(|app| {
// GLTF path warm: call Model::direct once (parser/cache warm-up)
if let Some(gltf_path) = app.cached_gltf.get() {
if let Ok(builder) = Model::direct(gltf_path.to_string_lossy().to_string()) {
let _ = CustomElement::<HexagonLauncher>::build(builder);
}
} else if let Some(tex) = app.cached_texture.get() {
// Raster icon path warm: build the lightweight namespaced model
// with the cached texture so material/texture creation happens now.
let builder = Model::namespaced("protostar", "hexagon/hexagon")
.part(ModelPart::new("Hex").mat_param(
"color",
MaterialParameter::Color(crate::BTN_COLOR),
))
.part(ModelPart::new("Icon").mat_param(
"diffuse",
MaterialParameter::Texture(tex.clone()),
));
let _ = CustomElement::<HexagonLauncher>::build(builder);
}
});
// build immutable lightweight snapshots used during reify
self.snapshots = self
.apps
.iter()
.map(|a| Snapshot {
name: a.app.name().unwrap_or_default().to_string(),
cached_texture: a.cached_texture.get().cloned(),
cached_gltf: a.cached_gltf.get().cloned(),
})
.collect();
} }
} }
impl Reify for HexagonLauncher { impl Reify for HexagonLauncher {
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
fn reify(&self) -> impl Element<Self> { fn reify(&self) -> impl Element<Self> {
// measure reify latency and count
let start = std::time::Instant::now();
// Build UI based on current state // Build UI based on current state
Grabbable::new( let elem = Grabbable::new(
Shape::Cylinder(CylinderShape { Shape::Cylinder(CylinderShape {
radius: APP_SIZE / 2.0, radius: APP_SIZE / 2.0,
length: 0.01, length: 0.01,
@@ -98,7 +192,15 @@ impl Reify for HexagonLauncher {
self.pos, self.pos,
self.rot, self.rot,
|state: &mut Self, pos, rot| { |state: &mut Self, pos, rot| {
// only update if changed enough to avoid constant reify
let dx = (state.pos.x - pos.x).abs();
let dy = (state.pos.y - pos.y).abs();
let dz = (state.pos.z - pos.z).abs();
if dx > 0.0005 || dy > 0.0005 || dz > 0.0005 {
tracing::trace!(?pos, "updating grab position");
state.pos = pos; state.pos = pos;
}
// rotation updates can also be debounced if noisy
state.rot = rot; state.rot = rot;
}, },
) )
@@ -109,6 +211,7 @@ impl Reify for HexagonLauncher {
.child( .child(
Button::new(|state: &mut HexagonLauncher| { Button::new(|state: &mut HexagonLauncher| {
state.open = !state.open; state.open = !state.open;
tracing::debug!(open = state.open, "toggled hexagon open");
}) })
.pos([0.0, 0.0, 0.005]) .pos([0.0, 0.0, 0.005])
.size([APP_SIZE / 2.0; 2]) .size([APP_SIZE / 2.0; 2])
@@ -120,6 +223,82 @@ impl Reify for HexagonLauncher {
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI), Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
[MODEL_SCALE; 3], [MODEL_SCALE; 3],
)) ))
.part(ModelPart::new("Hex").mat_param(
"color",
MaterialParameter::Color(DEFAULT_HEX_COLOR),
))
.build(),
)
// limit how many children we build per-frame to avoid reify explosion;
// increase if performance is acceptable, or implement a pager/virtualization.
.children({
// read configured maximum (fall back to all apps)
let env_max = std::env::var("HEX_MAX_VISIBLE")
.ok()
.and_then(|s| s.parse::<usize>().ok());
let configured_max = env_max.unwrap_or(self.apps.len());
// desired target: if open -> min(configured_max, apps.len()) else 0
let desired = if self.open {
std::cmp::min(configured_max, self.apps.len())
} else {
0
};
// nudge the global visible limit toward desired to spread creation cost
let current = VISIBLE_LIMIT.load(Ordering::Relaxed);
if desired == 0 {
// closing -> quickly collapse
if current != 0 {
VISIBLE_LIMIT.store(0, Ordering::Relaxed);
}
} else if current < desired {
let add = (desired - current).min(VISIBLE_STEP);
VISIBLE_LIMIT.fetch_add(add, Ordering::Relaxed);
} else if current > desired {
// clamp down if configured max reduced
VISIBLE_LIMIT.store(desired, Ordering::Relaxed);
}
let take_n = std::cmp::min(VISIBLE_LIMIT.load(Ordering::Relaxed), self.apps.len());
tracing::debug!(total_apps = self.apps.len(), configured_max, visible = take_n, desired, "building visible app children");
self.open
.then(|| {
self.apps
.iter()
.enumerate()
.take(take_n)
.map(|(i, _app)| {
// use snapshot instead of reify_substate (cheap, immutable)
let snap = self.snapshots[i].clone();
let pos = self.positions[i];
// start from a fresh spatial element
let base = Spatial::default().pos(pos).build();
// ensure both branches return the same element type by always
// attaching a Model child (either GLTF builder or namespaced fallback)
let with_model = match snap.cached_gltf {
Some(ref gltf) => {
if let Ok(builder) =
Model::direct(gltf.to_string_lossy().to_string())
{
base.child(
builder
.transform(Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0)
* Quat::from_rotation_y(PI),
[MODEL_SCALE; 3],
))
.build(),
)
} else {
// fallback to namespaced model if direct GLTF build fails
let mut mb = Model::namespaced("protostar", "hexagon/hexagon")
.transform(Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0)
* Quat::from_rotation_y(PI),
[MODEL_SCALE; 3],
))
.part(ModelPart::new("Hex").mat_param( .part(ModelPart::new("Hex").mat_param(
"color", "color",
MaterialParameter::Color(if self.open { MaterialParameter::Color(if self.open {
@@ -127,23 +306,56 @@ impl Reify for HexagonLauncher {
} else { } else {
BTN_COLOR BTN_COLOR
}), }),
));
if let Some(tex) = snap.cached_texture {
mb = mb.part(ModelPart::new("Icon").mat_param(
"diffuse",
MaterialParameter::Texture(tex),
));
}
base.child(mb.build())
}
}
None => {
let mut mb = Model::namespaced("protostar", "hexagon/hexagon")
.transform(Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0)
* Quat::from_rotation_y(PI),
[MODEL_SCALE; 3],
)) ))
.part(ModelPart::new("Hex").mat_param(
"color",
MaterialParameter::Color(DEFAULT_HEX_COLOR),
));
if let Some(tex) = snap.cached_texture {
mb = mb.part(ModelPart::new("Icon").mat_param(
"diffuse",
MaterialParameter::Texture(tex),
));
}
base.child(mb.build())
}
};
// attach a Button that mutates real state when used (captures index)
with_model.child(
Button::new(move |state: &mut HexagonLauncher| {
tracing::debug!(index = i, "app button pressed");
})
.pos([0.0, 0.0, 0.0])
.size([0.01; 2])
.build(), .build(),
) )
.children(
self.open
.then(|| {
self.apps.iter().enumerate().map(|(i, app)| {
Spatial::default()
.pos(Hex::spiral(i + 1).get_coords())
.build()
.child(app.reify_substate(move |state: &mut HexagonLauncher| {
state.apps.get_mut(i)
}))
}) })
}) })
.into_iter() .into_iter()
.flatten(), .flatten()
) })
;
let elapsed = start.elapsed().as_nanos() as u64;
REIFY_TOTAL_NS.fetch_add(elapsed, Ordering::Relaxed);
REIFY_COUNT.fetch_add(1, Ordering::Relaxed);
elem
} }
} }

View File

@@ -7,14 +7,12 @@ use stardust_xr_asteroids::elements::{
Grabbable, Lines, Model, ModelPart, PointerMode, Text, line_from_points, Grabbable, Lines, Model, ModelPart, PointerMode, Text, line_from_points,
}; };
use stardust_xr_asteroids::{CustomElement, Element, Reify, Transformable}; use stardust_xr_asteroids::{CustomElement, Element, Reify, Transformable};
use stardust_xr_fusion::drawable::{TextBounds, TextFit}; use stardust_xr_fusion::drawable::{TextBounds, TextFit, MaterialParameter, XAlign, YAlign};
use stardust_xr_fusion::node::NodeError; use stardust_xr_fusion::fields::{CylinderShape, Shape};
use stardust_xr_fusion::spatial::Transform;
use stardust_xr_fusion::values::ResourceID; use stardust_xr_fusion::values::ResourceID;
use stardust_xr_fusion::{ use stardust_xr_fusion::node::NodeError;
drawable::{MaterialParameter, XAlign, YAlign}, use std::path::PathBuf;
fields::{CylinderShape, Shape},
spatial::Transform,
};
use std::f32::consts::{FRAC_PI_2, PI}; use std::f32::consts::{FRAC_PI_2, PI};
use std::sync::OnceLock; use std::sync::OnceLock;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
@@ -27,6 +25,12 @@ pub struct App {
pub app: Application, pub app: Application,
#[serde(skip)] #[serde(skip)]
icon: OnceLock<Icon>, icon: OnceLock<Icon>,
// cached lightweight handles derived from the icon:
// - for PNG/raster icons we cache a ResourceID::Direct for the texture
#[serde(skip)]
pub cached_texture: OnceLock<ResourceID>,
#[serde(skip)]
pub cached_gltf: OnceLock<PathBuf>,
pos: Vector3<f32>, pos: Vector3<f32>,
rot: Quaternion<f32>, rot: Quaternion<f32>,
#[serde(skip)] #[serde(skip)]
@@ -38,6 +42,8 @@ impl App {
Ok(App { Ok(App {
app, app,
icon: OnceLock::default(), icon: OnceLock::default(),
cached_texture: OnceLock::default(),
cached_gltf: OnceLock::default(),
pos: [0.0; 3].into(), pos: [0.0; 3].into(),
rot: Quat::IDENTITY.into(), rot: Quat::IDENTITY.into(),
launched: AtomicBool::new(false), launched: AtomicBool::new(false),
@@ -45,47 +51,76 @@ impl App {
} }
pub fn load_icon(&self) { pub fn load_icon(&self) {
if self.icon.get().is_none() // Only attempt to load/process the icon once
&& let Some(icon) = self if self.icon.get().is_none() {
if let Some(icon) = self
.app .app
.icon(64, true) .icon(64, true)
.and_then(|i| i.cached_process(64).ok()) .and_then(|i| i.cached_process(64).ok())
{ {
let _ = self.icon.set(icon); // store raw icon
let _ = self.icon.set(icon.clone());
// cache lightweight handles derived from the icon so reify can be cheap
match &icon.icon_type {
IconType::Gltf => {
// cache the gltf path (PathBuf) so we don't call Model::direct() discovery per-reify
let _ = self.cached_gltf.set(icon.path.clone());
}
IconType::Png => {
// cache a ResourceID::Direct for the texture
let _ = self
.cached_texture
.set(ResourceID::Direct(icon.path.clone()));
}
_ => {}
}
}
} }
} }
// Helper functions for creating app components // Helper functions for creating app components
fn create_model(&self) -> impl Element<Self> { fn create_model(&self) -> impl Element<Self> {
match self.icon.get().as_ref().map(|i| (i.icon_type.clone(), i)) { // prefer cached gltf path if present (avoids repeated Model::direct parsing)
Some((IconType::Gltf, icon)) => Model::direct(icon.path.clone()) if let Some(gltf_path) = self.cached_gltf.get() {
.unwrap() // Model::direct accepts a string/path — convert PathBuf to String here to be safe
if let Ok(builder) = Model::direct(gltf_path.to_string_lossy().to_string()) {
return builder
.transform(Transform::from_rotation_scale( .transform(Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI), Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
[MODEL_SCALE; 3], [MODEL_SCALE; 3],
)) ))
.build(), .build();
other => { }
let model = Model::namespaced("protostar", "hexagon/hexagon") }
// fallback / raster icon path: use a namespaced hex model and attach a cached texture
let mut model = Model::namespaced("protostar", "hexagon/hexagon")
.transform(Transform::from_rotation_scale( .transform(Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI), Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
[APP_SIZE / 2.0; 3], [APP_SIZE / 2.0; 3],
)) ))
.part( .part(ModelPart::new("Hex").mat_param(
ModelPart::new("Hex") "color",
.mat_param("color", MaterialParameter::Color(DEFAULT_HEX_COLOR)), MaterialParameter::Color(DEFAULT_HEX_COLOR),
); ));
match other { if let Some(tex) = self.cached_texture.get() {
Some((IconType::Png, icon)) => model.part(ModelPart::new("Icon").mat_param( model = model.part(ModelPart::new("Icon").mat_param(
"diffuse",
MaterialParameter::Texture(tex.clone()),
));
} else if let Some(icon) = self.icon.get() {
// fallback to using icon path directly if caching didn't happen for some reason
if let IconType::Png = icon.icon_type {
model = model.part(ModelPart::new("Icon").mat_param(
"diffuse", "diffuse",
MaterialParameter::Texture(ResourceID::Direct(icon.path.clone())), MaterialParameter::Texture(ResourceID::Direct(icon.path.clone())),
)), ));
_ => model,
}
.build()
} }
} }
model.build()
} }
} }
impl Reify for App { impl Reify for App {
@@ -101,6 +136,10 @@ impl Reify for App {
let length = converted.length(); let length = converted.length();
let direction = converted.normalize_or_zero(); let direction = converted.normalize_or_zero();
// cull models that are far away to avoid heavy model builds every frame
const CULL_DISTANCE: f32 = 4.0;
let should_render_model = length <= CULL_DISTANCE;
Lines::new([line_from_points(vec![ Lines::new([line_from_points(vec![
Vec3::from([0.0; 3]), Vec3::from([0.0; 3]),
(length < ACTIVATION_DISTANCE) as u32 as f32 (length < ACTIVATION_DISTANCE) as u32 as f32
@@ -121,7 +160,6 @@ impl Reify for App {
move |state: &mut Self| { move |state: &mut Self| {
let pos_vec = Vec3::from(state.pos); let pos_vec = Vec3::from(state.pos);
if pos_vec.length() > ACTIVATION_DISTANCE { if pos_vec.length() > ACTIVATION_DISTANCE {
// state.app.launch(launch_space)
state.launched.store(true, Ordering::Relaxed); state.launched.store(true, Ordering::Relaxed);
} else { } else {
state.pos = [0.0; 3].into(); state.pos = [0.0; 3].into();
@@ -134,7 +172,8 @@ impl Reify for App {
.max_distance(0.05) .max_distance(0.05)
.reparentable(false) .reparentable(false)
.build() .build()
.child(self.create_model()) // only build the (potentially expensive) model element when the hex is close enough
.children(should_render_model.then(|| self.create_model()).into_iter())
.children(self.launched.load(Ordering::Relaxed).then(|| { .children(self.launched.load(Ordering::Relaxed).then(|| {
AppLauncher::new(&self.app) AppLauncher::new(&self.app)
.done(|state: &mut Self| { .done(|state: &mut Self| {