diff --git a/Cargo.lock b/Cargo.lock index d5928b52..50d80cfc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1257,6 +1257,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8588661a8607108a5ca69cab034063441a0413a0b041c13618a7dd348021ef6f" dependencies = [ "hashbrown 0.14.5", +] + +[[package]] +name = "halfbrown" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c7ed2f2edad8a14c8186b847909a41fbb9c3eafa44f88bd891114ed5019da09" +dependencies = [ + "hashbrown 0.16.1", "serde", ] @@ -1501,7 +1510,7 @@ version = "0.10.1" dependencies = [ "ash", "glob", - "halfbrown", + "halfbrown 0.2.5", "librashader-cache", "librashader-common", "librashader-pack", @@ -1603,13 +1612,13 @@ dependencies = [ "ash", "bitflags 2.10.0", "glow", - "halfbrown", + "halfbrown 0.4.0", "num-traits", "objc2 0.6.3", "objc2-metal", "rustc-hash 2.1.1", "serde", - "smartstring", + "strumbra", "wgpu-types", "windows", ] @@ -3140,7 +3149,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29" dependencies = [ "autocfg", - "serde", "static_assertions", "version_check", ] @@ -3319,6 +3327,15 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "strumbra" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b498044485e2789b38e047fdb4c7ac827bd72d66407af9e5d15626f9c45364e" +dependencies = [ + "serde", +] + [[package]] name = "syn" version = "2.0.114" diff --git a/librashader-common/Cargo.toml b/librashader-common/Cargo.toml index c25afbf9..f53626e0 100644 --- a/librashader-common/Cargo.toml +++ b/librashader-common/Cargo.toml @@ -21,12 +21,13 @@ dxgi = ["windows"] vulkan = ["ash"] wgpu = ["wgpu-types"] metal = ["objc2", "objc2-metal"] -serde = ["dep:serde", "serde/derive", "smartstring/serde", "halfbrown/serde"] +serde = ["dep:serde", "serde/derive", "strumbra/serde", "halfbrown/serde"] [dependencies] num-traits = "0.2.15" rustc-hash = "2.0.0" -halfbrown = "0.2.4" -smartstring = "1.0" +halfbrown = "0.4.0" +strumbra = "0.6.0" + bitflags = { version = "2", features = ["serde"] } glow = { workspace = true, optional = true } diff --git a/librashader-common/src/lib.rs b/librashader-common/src/lib.rs index eda099fb..1f5fc0b0 100644 --- a/librashader-common/src/lib.rs +++ b/librashader-common/src/lib.rs @@ -36,6 +36,7 @@ mod viewport; #[doc(hidden)] pub mod map; pub mod shader_features; +pub mod string; pub use viewport::Viewport; diff --git a/librashader-common/src/map.rs b/librashader-common/src/map.rs index 1fdb01c4..14e46d53 100644 --- a/librashader-common/src/map.rs +++ b/librashader-common/src/map.rs @@ -4,5 +4,5 @@ pub type FastHashMap = halfbrown::SizedHashMap, 32>; -/// A string with small string optimizations up to 23 bytes. -pub type ShortString = smartstring::SmartString; +/// Common string type for parameters and uniform names with small string optimizations up to 23 bytes. +pub type ShortString = crate::string::ParamString; diff --git a/librashader-common/src/string.rs b/librashader-common/src/string.rs new file mode 100644 index 00000000..3e95b0a7 --- /dev/null +++ b/librashader-common/src/string.rs @@ -0,0 +1,149 @@ +use std::borrow::Borrow; +use std::cmp::Ordering; +use std::fmt::{Display, Formatter}; +use std::ops::Deref; +use strumbra::SharedString; + +#[repr(transparent)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Hash)] +pub struct ParamString(SharedString); + +impl Deref for ParamString { + type Target = str; + + #[inline] + fn deref(&self) -> &Self::Target { + self.0.as_ref() + } +} + +impl From<&str> for ParamString { + #[inline] + fn from(s: &str) -> Self { + ParamString(SharedString::try_from(s) + .expect("ParamString with more than 4294967295 characters. Parameter too large.")) + } +} + +impl From<&String> for ParamString { + #[inline] + fn from(s: &String) -> Self { + ParamString(SharedString::try_from(s) + .expect("ParamString with more than 4294967295 characters. Parameter too large.")) + } +} + + +impl From for ParamString { + #[inline] + fn from(s: String) -> Self { + ParamString(SharedString::try_from(s) + .expect("ParamString with more than 4294967295 characters. Parameter too large.")) + } +} + +impl Display for ParamString { + #[inline] + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + +impl AsRef for ParamString { + #[inline] + fn as_ref(&self) -> &str { + self.0.as_ref() + } +} + +impl Borrow for ParamString { + fn borrow(&self) -> &str { + self.0.as_ref() + } +} + +impl PartialEq<&str> for ParamString { + fn eq(&self, other: &&str) -> bool { + self.0.as_str() == *other + } +} + +impl PartialEq for ParamString { + fn eq(&self, other: &str) -> bool { + self.0.as_str() == other + } +} + +impl PartialEq for ParamString { + fn eq(&self, other: &String) -> bool { + self.0.as_str() == other + } +} + +impl PartialEq for &str { + fn eq(&self, other: &ParamString) -> bool { + *self == other.as_ref() + } +} + +impl PartialEq for String { + fn eq(&self, other: &ParamString) -> bool { + *self == other.as_ref() + } +} + +impl PartialEq for &String { + fn eq(&self, other: &ParamString) -> bool { + *self == other.as_ref() + } +} + + +impl Eq for ParamString {} + +impl PartialOrd for ParamString { + fn partial_cmp(&self, other: &String) -> Option { + self.0.partial_cmp(other) + } +} + +impl PartialOrd for ParamString { + fn partial_cmp(&self, other: &str) -> Option { + self.0.partial_cmp(other) + } +} + +impl PartialOrd<&str> for ParamString { + fn partial_cmp(&self, other: &&str) -> Option { + self.0.partial_cmp(*other) + } +} + + +impl ParamString { + /// Pushes a new string onto the `ParamString`. + /// + /// This could incur up to two allocations. + pub fn push_str(&mut self, s: &str) { + let new= format!("{self}{s}"); + self.0 = SharedString::try_from(new) + .expect("ParamString with more than 4294967295 characters."); + } + + // Extracts a string slice containing the entire `ParamString`. + pub fn as_str(&self) -> &str { + self.0.as_str() + } +} + +#[cfg(test)] +mod test { + use super::*; + #[test] + pub fn test_push_str() { + let mut string = ParamString::from("Hello"); + string.push_str(" World"); + assert_eq!("Hello World", string); + } +} \ No newline at end of file diff --git a/librashader-runtime/src/binding.rs b/librashader-runtime/src/binding.rs index 1e301622..e82c5e50 100644 --- a/librashader-runtime/src/binding.rs +++ b/librashader-runtime/src/binding.rs @@ -370,8 +370,6 @@ where _ => None, }) { - let id = id.as_str(); - let default = parameter_defaults.get(id).map_or(0f32, |f| f.initial); let value = *runtime_parameters.get(id).unwrap_or(&default);