Nvida Flag, dashmap and Always Export Stage #39

Merged
Schmarni-Dev merged 3 commits from last_minute_patches into dev 2025-03-19 14:46:55 -04:00
7 changed files with 114 additions and 59 deletions

15
Cargo.lock generated
View File

@@ -755,6 +755,20 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991" checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991"
[[package]]
name = "dashmap"
version = "6.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf"
dependencies = [
"cfg-if",
"crossbeam-utils",
"hashbrown 0.14.5",
"lock_api",
"once_cell",
"parking_lot_core 0.9.10",
]
[[package]] [[package]]
name = "directories" name = "directories"
version = "5.0.1" version = "5.0.1"
@@ -2653,6 +2667,7 @@ dependencies = [
"clap", "clap",
"color-eyre", "color-eyre",
"console-subscriber", "console-subscriber",
"dashmap",
"directories", "directories",
"glam", "glam",
"global_counter", "global_counter",

View File

@@ -92,6 +92,7 @@ xkbcommon-rs = "0.1.0"
# wayland # wayland
wayland-backend = { version = "0.3.7", optional = true, default-features = false } wayland-backend = { version = "0.3.7", optional = true, default-features = false }
wayland-scanner = { version = "0.31.4", optional = true } wayland-scanner = { version = "0.31.4", optional = true }
dashmap = "6.1.0"
[dependencies.smithay] [dependencies.smithay]
git = "https://github.com/smithay/smithay.git" git = "https://github.com/smithay/smithay.git"

View File

@@ -1,19 +1,18 @@
#![allow(dead_code)] #![allow(dead_code)]
use dashmap::DashMap;
use parking_lot::{MappedMutexGuard, Mutex, MutexGuard, const_mutex}; use parking_lot::{MappedMutexGuard, Mutex, MutexGuard, const_mutex};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use std::ops::Deref;
use std::ptr; use std::ptr;
use std::sync::{Arc, Weak}; use std::sync::{Arc, LazyLock, Weak};
#[derive(Debug)] #[derive(Debug)]
pub struct Registry<T: Send + Sync + ?Sized>(Mutex<Option<FxHashMap<usize, Weak<T>>>>); pub struct Registry<T: Send + Sync + ?Sized>(MaybeLazy<DashMap<usize, Weak<T>>>);
impl<T: Send + Sync + ?Sized> Registry<T> { impl<T: Send + Sync + ?Sized> Registry<T> {
pub const fn new() -> Self { pub const fn new() -> Self {
Registry(const_mutex(None)) Registry(MaybeLazy::Lazy(LazyLock::new(DashMap::default)))
}
fn lock(&self) -> MappedMutexGuard<FxHashMap<usize, Weak<T>>> {
MutexGuard::map(self.0.lock(), |r| r.get_or_insert_with(FxHashMap::default))
} }
pub fn add(&self, t: T) -> Arc<T> pub fn add(&self, t: T) -> Arc<T>
where where
@@ -24,30 +23,29 @@ impl<T: Send + Sync + ?Sized> Registry<T> {
t_arc t_arc
} }
pub fn add_raw(&self, t: &Arc<T>) { pub fn add_raw(&self, t: &Arc<T>) {
self.lock() self.0
.insert(Arc::as_ptr(t) as *const () as usize, Arc::downgrade(t)); .insert(Arc::as_ptr(t) as *const () as usize, Arc::downgrade(t));
} }
pub fn contains(&self, t: &T) -> bool { pub fn contains(&self, t: &T) -> bool {
self.lock() self.0
.contains_key(&(ptr::addr_of!(*t) as *const () as usize)) .contains_key(&(ptr::addr_of!(*t) as *const () as usize))
} }
pub fn get_changes(old: &Registry<T>, new: &Registry<T>) -> (Vec<Arc<T>>, Vec<Arc<T>>) { pub fn get_changes(old: &Registry<T>, new: &Registry<T>) -> (Vec<Arc<T>>, Vec<Arc<T>>) {
let old = old.lock();
let new = new.lock();
let mut added = Vec::new(); let mut added = Vec::new();
let mut removed = Vec::new(); let mut removed = Vec::new();
for (id, entry) in new.iter() { for pair in new.0.iter() {
let (id, entry) = pair.pair();
if let Some(entry) = entry.upgrade() { if let Some(entry) = entry.upgrade() {
if !old.contains_key(id) { if !old.0.contains_key(id) {
added.push(entry); added.push(entry);
} }
} }
} }
for (id, entry) in old.iter() { for pair in old.0.iter() {
let (id, entry) = pair.pair();
if let Some(entry) = entry.upgrade() { if let Some(entry) = entry.upgrade() {
if !new.contains_key(id) { if !new.0.contains_key(id) {
removed.push(entry); removed.push(entry);
} }
} }
@@ -55,52 +53,48 @@ impl<T: Send + Sync + ?Sized> Registry<T> {
(added, removed) (added, removed)
} }
pub fn get_valid_contents(&self) -> Vec<Arc<T>> { pub fn get_valid_contents(&self) -> Vec<Arc<T>> {
self.lock() self.0
.iter() .iter()
.filter_map(|pair| pair.1.upgrade()) .filter_map(|pair| pair.value().upgrade())
.collect() .collect()
} }
pub fn set(&self, other: &Registry<T>) { pub fn set(&self, other: &Registry<T>) {
self.lock().clone_from(&other.lock()); self.clear();
for (key, value) in other.0.deref().clone().into_iter() {
self.0.insert(key, value);
}
} }
pub fn take_valid_contents(&self) -> Vec<Arc<T>> { pub fn take_valid_contents(&self) -> Vec<Arc<T>> {
self.0 let contents = self.get_valid_contents();
.lock() self.0.clear();
.take() contents
.unwrap_or_default()
.into_iter()
.filter_map(|pair| pair.1.upgrade())
.collect()
} }
pub fn retain<F: Fn(&Arc<T>) -> bool>(&self, f: F) { pub fn retain<F: Fn(&Arc<T>) -> bool>(&self, f: F) {
self.lock().retain(|_, v| { self.0.retain(|_, v| {
let Some(v) = v.upgrade() else { let Some(v) = v.upgrade() else {
// why would we want to retain things we can't upgrade?
return true; return true;
}; };
(f)(&v) (f)(&v)
}) })
} }
pub fn remove(&self, t: &T) { pub fn remove(&self, t: &T) {
self.lock() self.0.remove(&(ptr::addr_of!(*t) as *const () as usize));
.remove(&(ptr::addr_of!(*t) as *const () as usize));
} }
pub fn clear(&self) { pub fn clear(&self) {
self.lock().clear(); self.0.clear();
} }
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
let registry = self.0.lock(); if self.0.is_empty() {
let Some(registry) = &*registry else {
return true;
};
if registry.is_empty() {
return true; return true;
} }
registry.values().all(|v| v.strong_count() == 0) self.0.iter().all(|v| v.value().strong_count() == 0)
} }
} }
impl<T: Send + Sync + ?Sized> Clone for Registry<T> { impl<T: Send + Sync + ?Sized> Clone for Registry<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self(Mutex::new(self.0.lock().clone())) Self(self.0.clone())
} }
} }
impl<T: Send + Sync + ?Sized> Default for Registry<T> { impl<T: Send + Sync + ?Sized> Default for Registry<T> {
@@ -109,6 +103,30 @@ impl<T: Send + Sync + ?Sized> Default for Registry<T> {
} }
} }
#[derive(Debug)]
enum MaybeLazy<T> {
Lazy(LazyLock<T>),
NonLazy(T),
}
impl<T: Clone> Clone for MaybeLazy<T> {
fn clone(&self) -> Self {
match self {
MaybeLazy::Lazy(lazy_lock) => Self::NonLazy(lazy_lock.deref().clone()),
MaybeLazy::NonLazy(v) => Self::NonLazy(v.clone()),
}
}
}
impl<T> Deref for MaybeLazy<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
match self {
MaybeLazy::Lazy(lazy_lock) => lazy_lock,
MaybeLazy::NonLazy(v) => v,
}
}
}
pub struct OwnedRegistry<T: Send + Sync + ?Sized>(Mutex<Option<FxHashMap<usize, Arc<T>>>>); pub struct OwnedRegistry<T: Send + Sync + ?Sized>(Mutex<Option<FxHashMap<usize, Arc<T>>>>);
impl<T: Send + Sync + ?Sized> OwnedRegistry<T> { impl<T: Send + Sync + ?Sized> OwnedRegistry<T> {

View File

@@ -67,12 +67,16 @@ struct CliArgs {
/// Restore the session with the given ID (or `latest`), ignoring the startup script. Sessions are stored in directories at `~/.local/state/stardust/`. /// Restore the session with the given ID (or `latest`), ignoring the startup script. Sessions are stored in directories at `~/.local/state/stardust/`.
#[clap(id = "SESSION_ID", long = "restore", action)] #[clap(id = "SESSION_ID", long = "restore", action)]
restore: Option<String>, restore: Option<String>,
/// this should fix nvidia issues, it'll only help on driver 565+
/// and only if running under wayland, probably
#[clap(long)]
nvidia: bool,
} }
static STARDUST_INSTANCE: OnceLock<String> = OnceLock::new(); static STARDUST_INSTANCE: OnceLock<String> = OnceLock::new();
// #[tokio::main] // #[tokio::main(flavor = "current_thread")]
#[tokio::main(flavor = "current_thread")] #[tokio::main]
async fn main() { async fn main() {
color_eyre::install().unwrap(); color_eyre::install().unwrap();
@@ -98,6 +102,20 @@ async fn main() {
let cli_args = CliArgs::parse(); let cli_args = CliArgs::parse();
if cli_args.nvidia && !cli_args.flatscreen {
// Only call this while singlethreaded since it can/will cause raceconditions with other
// functions reading or writing from the env
unsafe {
std::env::set_var("__GLX_VENDOR_LIBRARY_NAME", "mesa");
std::env::set_var(
"__EGL_VENDOR_LIBRARY_FILENAMES",
"/usr/share/glvnd/egl_vendor.d/50_mesa.json",
);
std::env::set_var("MESA_LOADER_DRIVER_OVERRIDE", "zink");
std::env::set_var("GALLIUM_DRIVER", "zink");
}
}
let socket_path = let socket_path =
server::get_free_socket_path().expect("Unable to find a free stardust socket path"); server::get_free_socket_path().expect("Unable to find a free stardust socket path");
STARDUST_INSTANCE.set(socket_path.file_name().unwrap().to_string_lossy().into_owned()).expect("Someone hasn't done their job, yell at Nova because how is this set multiple times what the hell"); STARDUST_INSTANCE.set(socket_path.file_name().unwrap().to_string_lossy().into_owned()).expect("Someone hasn't done their job, yell at Nova because how is this set multiple times what the hell");

View File

@@ -12,8 +12,7 @@ use crate::core::client::Client;
use crate::core::error::{Result, ServerError}; use crate::core::error::{Result, ServerError};
use crate::core::registry::Registry; use crate::core::registry::Registry;
use crate::core::scenegraph::MethodResponseSender; use crate::core::scenegraph::MethodResponseSender;
use parking_lot::Mutex; use dashmap::DashMap;
use rustc_hash::FxHashMap;
use serde::{Serialize, de::DeserializeOwned}; use serde::{Serialize, de::DeserializeOwned};
use spatial::Spatial; use spatial::Spatial;
use stardust_xr::messenger::MessageSenderHandle; use stardust_xr::messenger::MessageSenderHandle;
@@ -190,7 +189,6 @@ impl Node {
let aspect = self let aspect = self
.aspects .aspects
.0 .0
.lock()
.get(&aspect_id) .get(&aspect_id)
.ok_or(ScenegraphError::AspectNotFound)? .ok_or(ScenegraphError::AspectNotFound)?
.clone(); .clone();
@@ -229,7 +227,7 @@ impl Node {
response, response,
) )
} else { } else {
let Some(aspect) = self.aspects.0.lock().get(&aspect_id).cloned() else { let Some(aspect) = self.aspects.0.get(&aspect_id).map(|v| v.clone()) else {
response.send(Err(ScenegraphError::AspectNotFound)); response.send(Err(ScenegraphError::AspectNotFound));
return; return;
}; };
@@ -324,7 +322,7 @@ pub trait Aspect: Any + Send + Sync + 'static {
} }
#[derive(Default)] #[derive(Default)]
struct Aspects(Mutex<FxHashMap<u64, Arc<dyn Aspect>>>); struct Aspects(DashMap<u64, Arc<dyn Aspect>>);
impl Aspects { impl Aspects {
fn add<A: AspectIdentifier>(&self, t: A) -> Arc<A> { fn add<A: AspectIdentifier>(&self, t: A) -> Arc<A> {
let aspect = Arc::new(t); let aspect = Arc::new(t);
@@ -332,13 +330,13 @@ impl Aspects {
aspect aspect
} }
fn add_raw<A: AspectIdentifier>(&self, aspect: Arc<A>) { fn add_raw<A: AspectIdentifier>(&self, aspect: Arc<A>) {
self.0.lock().insert(A::ID, aspect); self.0.insert(A::ID, aspect);
} }
fn get<A: Aspect + AspectIdentifier>(&self) -> Result<Arc<A>> { fn get<A: Aspect + AspectIdentifier>(&self) -> Result<Arc<A>> {
self.0 self.0
.lock()
.get(&A::ID) .get(&A::ID)
.cloned() // .cloned doesn't work for some reason
.map(|v| v.clone())
.map(|a| a.as_any()) .map(|a| a.as_any())
.and_then(|a| Arc::downcast(a).ok()) .and_then(|a| Arc::downcast(a).ok())
.ok_or(ServerError::NoAspect(TypeId::of::<A>())) .ok_or(ServerError::NoAspect(TypeId::of::<A>()))
@@ -346,6 +344,7 @@ impl Aspects {
} }
impl Drop for Aspects { impl Drop for Aspects {
fn drop(&mut self) { fn drop(&mut self) {
self.0.lock().clear() // why would this be needed? do drop impls not run otherwise?
self.0.clear()
} }
} }

View File

@@ -17,7 +17,7 @@ use play_space::PlaySpaceBounds;
use stardust_xr::schemas::dbus::object_registry::ObjectRegistry; use stardust_xr::schemas::dbus::object_registry::ObjectRegistry;
use std::{ use std::{
marker::PhantomData, marker::PhantomData,
sync::{atomic::Ordering, Arc}, sync::{Arc, atomic::Ordering},
}; };
use stereokit_rust::{ use stereokit_rust::{
material::Material, material::Material,
@@ -65,10 +65,7 @@ impl ServerObjects {
) -> ServerObjects { ) -> ServerObjects {
let hmd = SpatialRef::create(&connection, "/org/stardustxr/HMD"); let hmd = SpatialRef::create(&connection, "/org/stardustxr/HMD");
let play_space = (World::has_bounds() let play_space = Some(SpatialRef::create(&connection, "/org/stardustxr/PlaySpace"));
&& World::get_bounds_size().x != 0.0
&& World::get_bounds_size().y != 0.0)
.then(|| SpatialRef::create(&connection, "/org/stardustxr/PlaySpace"));
if play_space.is_some() { if play_space.is_some() {
let dbus_connection = connection.clone(); let dbus_connection = connection.clone();
tokio::task::spawn(async move { tokio::task::spawn(async move {

View File

@@ -15,12 +15,19 @@ impl PlaySpaceBounds {
impl PlaySpaceBounds { impl PlaySpaceBounds {
#[zbus(property)] #[zbus(property)]
fn bounds(&self) -> Vec<(f64, f64)> { fn bounds(&self) -> Vec<(f64, f64)> {
let bounds = World::get_bounds_size(); if (World::has_bounds()
vec![ && World::get_bounds_size().x != 0.0
((bounds.x).into(), (bounds.y).into()), && World::get_bounds_size().y != 0.0)
((bounds.x).into(), (-bounds.y).into()), {
((-bounds.x).into(), (-bounds.y).into()), let bounds = World::get_bounds_size();
((-bounds.x).into(), (bounds.y).into()), vec![
] ((bounds.x).into(), (bounds.y).into()),
((bounds.x).into(), (-bounds.y).into()),
((-bounds.x).into(), (-bounds.y).into()),
((-bounds.x).into(), (bounds.y).into()),
]
} else {
vec![]
}
} }
} }