Skip to content
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
25 changes: 21 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions librashader-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions librashader-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod viewport;
#[doc(hidden)]
pub mod map;
pub mod shader_features;
pub mod string;

pub use viewport::Viewport;

Expand Down
4 changes: 2 additions & 2 deletions librashader-common/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
pub type FastHashMap<K, V> =
halfbrown::SizedHashMap<K, V, core::hash::BuildHasherDefault<rustc_hash::FxHasher>, 32>;

/// A string with small string optimizations up to 23 bytes.
pub type ShortString = smartstring::SmartString<smartstring::LazyCompact>;
/// Common string type for parameters and uniform names with small string optimizations up to 23 bytes.
pub type ShortString = crate::string::ParamString;
149 changes: 149 additions & 0 deletions librashader-common/src/string.rs
Original file line number Diff line number Diff line change
@@ -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<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 Display for ParamString {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl AsRef<str> for ParamString {
#[inline]
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}

impl Borrow<str> 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<str> for ParamString {
fn eq(&self, other: &str) -> bool {
self.0.as_str() == other
}
}

impl PartialEq<String> for ParamString {
fn eq(&self, other: &String) -> bool {
self.0.as_str() == other
}
}

impl PartialEq<ParamString> for &str {
fn eq(&self, other: &ParamString) -> bool {
*self == other.as_ref()
}
}

impl PartialEq<ParamString> for String {
fn eq(&self, other: &ParamString) -> bool {
*self == other.as_ref()
}
}

impl PartialEq<ParamString> for &String {
fn eq(&self, other: &ParamString) -> bool {
*self == other.as_ref()
}
}


impl Eq for ParamString {}

impl PartialOrd<String> for ParamString {
fn partial_cmp(&self, other: &String) -> Option<Ordering> {
self.0.partial_cmp(other)
}
}

impl PartialOrd<str> for ParamString {
fn partial_cmp(&self, other: &str) -> Option<Ordering> {
self.0.partial_cmp(other)
}
}

impl PartialOrd<&str> for ParamString {
fn partial_cmp(&self, other: &&str) -> Option<Ordering> {
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);
}
}
2 changes: 0 additions & 2 deletions librashader-runtime/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down