Skip to content

Commit 263abc6

Browse files
committed
gcore-shaders: no_std fixups
1 parent ecbbd45 commit 263abc6

File tree

8 files changed

+33
-15
lines changed

8 files changed

+33
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ kurbo = { version = "0.11.0", features = ["serde"] }
152152
petgraph = { version = "0.7.1", default-features = false, features = [
153153
"graphmap",
154154
] }
155-
half = { version = "2.4.1", default-features = false, features = ["bytemuck", "serde"] }
155+
half = { version = "2.4.1", default-features = false, features = ["bytemuck"] }
156156
tinyvec = { version = "1", features = ["std"] }
157157
criterion = { version = "0.5", features = ["html_reports"] }
158158
iai-callgrind = { version = "0.12.3" }

node-graph/gcore-shaders/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors = ["Graphite Authors <[email protected]>"]
77
license = "MIT OR Apache-2.0"
88

99
[features]
10-
std = ["dep:dyn-any", "dep:serde", "dep:specta", "dep:log"]
10+
std = ["dep:dyn-any", "dep:serde", "dep:specta", "dep:log", "half/std", "half/serde"]
1111

1212
[dependencies]
1313
# Local std dependencies
@@ -16,7 +16,7 @@ dyn-any = { workspace = true, optional = true }
1616
# Workspace dependencies
1717
bytemuck = { workspace = true }
1818
glam = { version = "0.29", default-features = false, features = ["nostd-libm", "scalar-math"] }
19-
half = { workspace = true }
19+
half = { workspace = true, default-features = false }
2020
num-derive = { workspace = true }
2121
num-traits = { workspace = true }
2222

node-graph/gcore-shaders/src/blending.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use core::fmt::Display;
22
use core::hash::{Hash, Hasher};
3+
#[cfg(target_arch = "spirv")]
4+
use num_traits::float::Float;
35

46
#[derive(Debug, Clone, Copy, PartialEq)]
57
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
@@ -24,7 +26,7 @@ impl Hash for AlphaBlending {
2426
}
2527
}
2628
impl Display for AlphaBlending {
27-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2830
let round = |x: f32| (x * 1e3).round() / 1e3;
2931
write!(
3032
f,
@@ -203,7 +205,7 @@ impl BlendMode {
203205
}
204206

205207
impl Display for BlendMode {
206-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
208+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
207209
match self {
208210
// Normal group
209211
BlendMode::Normal => write!(f, "Normal"),

node-graph/gcore-shaders/src/color/color.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use super::color_traits::{Alpha, AlphaMut, AssociatedAlpha, Luminance, LuminanceMut, Pixel, RGB, RGBMut, Rec709Primaries, SRGB};
22
use super::discrete_srgb::{float_to_srgb_u8, srgb_u8_to_float};
33
use bytemuck::{Pod, Zeroable};
4+
use core::fmt::Debug;
45
use core::hash::Hash;
56
use half::f16;
67
#[cfg(target_arch = "spirv")]
7-
use spirv_std::num_traits::Euclid;
8+
use num_traits::Euclid;
89
#[cfg(target_arch = "spirv")]
9-
use spirv_std::num_traits::float::Float;
10+
use num_traits::float::Float;
1011

1112
#[repr(C)]
12-
#[derive(Debug, Default, Clone, Copy, PartialEq, Pod, Zeroable)]
13+
#[derive(Default, Clone, Copy, PartialEq, Pod, Zeroable)]
14+
#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]
1315
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, serde::Serialize, serde::Deserialize))]
1416
pub struct RGBA16F {
1517
red: f16,
@@ -18,6 +20,14 @@ pub struct RGBA16F {
1820
alpha: f16,
1921
}
2022

23+
/// hack around half still masking out impl Debug for f16 on spirv
24+
#[cfg(target_arch = "spirv")]
25+
impl core::fmt::Debug for RGBA16F {
26+
fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
27+
Ok(())
28+
}
29+
}
30+
2131
impl From<Color> for RGBA16F {
2232
#[inline(always)]
2333
fn from(c: Color) -> Self {
@@ -215,7 +225,7 @@ pub struct Color {
215225

216226
#[allow(clippy::derived_hash_with_manual_eq)]
217227
impl Hash for Color {
218-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
228+
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
219229
self.red.to_bits().hash(state);
220230
self.green.to_bits().hash(state);
221231
self.blue.to_bits().hash(state);
@@ -256,7 +266,7 @@ impl AlphaMut for Color {
256266
}
257267

258268
impl Pixel for Color {
259-
#[cfg(not(target_arch = "spirv"))]
269+
#[cfg(feature = "std")]
260270
fn to_bytes(&self) -> Vec<u8> {
261271
self.to_rgba8_srgb().to_vec()
262272
}
@@ -793,6 +803,7 @@ impl Color {
793803
/// let color = Color::from_rgba8_srgb(0x52, 0x67, 0xFA, 0x61); // Premultiplied alpha
794804
/// assert_eq!("3240a261", color.to_rgba_hex_srgb()); // Equivalent hex incorporating premultiplied alpha
795805
/// ```
806+
#[cfg(feature = "std")]
796807
pub fn to_rgba_hex_srgb(&self) -> String {
797808
let gamma = self.to_gamma_srgb();
798809
format!(
@@ -810,6 +821,7 @@ impl Color {
810821
/// let color = Color::from_rgba8_srgb(0x52, 0x67, 0xFA, 0x61); // Premultiplied alpha
811822
/// assert_eq!("3240a2", color.to_rgb_hex_srgb()); // Equivalent hex incorporating premultiplied alpha
812823
/// ```
824+
#[cfg(feature = "std")]
813825
pub fn to_rgb_hex_srgb(&self) -> String {
814826
self.to_gamma_srgb().to_rgb_hex_srgb_from_gamma()
815827
}
@@ -820,6 +832,7 @@ impl Color {
820832
/// let color = Color::from_rgba8_srgb(0x52, 0x67, 0xFA, 0x61); // Premultiplied alpha
821833
/// assert_eq!("3240a2", color.to_rgb_hex_srgb()); // Equivalent hex incorporating premultiplied alpha
822834
/// ```
835+
#[cfg(feature = "std")]
823836
pub fn to_rgb_hex_srgb_from_gamma(&self) -> String {
824837
format!("{:02x?}{:02x?}{:02x?}", (self.r() * 255.) as u8, (self.g() * 255.) as u8, (self.b() * 255.) as u8)
825838
}

node-graph/gcore-shaders/src/color/color_traits.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ pub trait Linear {
1414
fn lerp(self, other: Self, value: Self) -> Self
1515
where
1616
Self: Sized + Copy,
17-
Self: std::ops::Sub<Self, Output = Self>,
18-
Self: std::ops::Mul<Self, Output = Self>,
19-
Self: std::ops::Add<Self, Output = Self>,
17+
Self: core::ops::Sub<Self, Output = Self>,
18+
Self: core::ops::Mul<Self, Output = Self>,
19+
Self: core::ops::Add<Self, Output = Self>,
2020
{
2121
self + (other - self) * value
2222
}
@@ -97,7 +97,7 @@ pub trait SRGB: Rec709Primaries {}
9797

9898
// TODO: Come up with a better name for this trait
9999
pub trait Pixel: Clone + Pod + Zeroable + Default {
100-
#[cfg(not(target_arch = "spirv"))]
100+
#[cfg(feature = "std")]
101101
fn to_bytes(&self) -> Vec<u8> {
102102
bytemuck::bytes_of(self).to_vec()
103103
}

node-graph/gcore-shaders/src/color/discrete_srgb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn float_to_srgb_u8(mut f: f32) -> u8 {
6969
// We clamped f to [0, 1], and the integer representations
7070
// of the positive finite non-NaN floats are monotonic.
7171
// This makes the later LUT lookup panicless.
72-
unsafe { std::hint::unreachable_unchecked() }
72+
unsafe { core::hint::unreachable_unchecked() }
7373
}
7474

7575
// Compute a piecewise linear interpolation that is always

node-graph/gcore-shaders/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(not(feature = "std"), no_std)]
2+
13
pub mod blending;
24
pub mod choice_type;
35
pub mod color;

node-graph/gcore-shaders/src/registry.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ pub mod types {
2020
/// DVec2 with px unit
2121
pub type PixelSize = glam::DVec2;
2222
/// String with one or more than one line
23+
#[cfg(feature = "std")]
2324
pub type TextArea = String;
2425
}

0 commit comments

Comments
 (0)