Skip to content

Commit a744499

Browse files
authored
Shaders: more graster-nodes no-std fixups (#3090)
* gcore-shaders: fix missing `num-traits/libm` features * graster-nodes: fix missing cfg on use statements * shaders: use unchecked Color constructors * graster-nodes: remove async from shader nodes not needing it * gcore-shaders: remove explicit fn pointer * graster-nodes: make kurbo std-only * graster-nodes: replace glam reexport with normal dep * gcore: impl Display for ProtoNodeIdentifier * unify glam workspace dep
1 parent c5991c6 commit a744499

File tree

11 files changed

+44
-33
lines changed

11 files changed

+44
-33
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ usvg = "0.44"
126126
rand = { version = "0.9", default-features = false, features = ["std_rng"] }
127127
rand_chacha = "0.9"
128128
glam = { version = "0.29", default-features = false, features = [
129-
"serde",
129+
"nostd-libm",
130130
"scalar-math",
131-
"debug-glam-assert",
131+
"bytemuck",
132132
] }
133133
base64 = "0.22"
134134
image = { version = "0.25", default-features = false, features = [
@@ -142,7 +142,7 @@ pretty_assertions = "1.4.1"
142142
fern = { version = "0.7", features = ["colored"] }
143143
num_enum = "0.7"
144144
num-derive = "0.4"
145-
num-traits = { version = "0.2", default-features = false, features = ["i128"] }
145+
num-traits = { version = "0.2", default-features = false, features = ["libm"] }
146146
specta = { version = "2.0.0-rc.22", features = [
147147
"glam",
148148
"derive",

node-graph/gcore-shaders/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ 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", "half/std", "half/serde"]
10+
std = ["dep:dyn-any", "dep:serde", "dep:specta", "dep:log", "glam/debug-glam-assert", "glam/std", "glam/serde", "half/std", "half/serde", "num-traits/std"]
1111

1212
[dependencies]
1313
# Local std dependencies
1414
dyn-any = { workspace = true, optional = true }
1515

1616
# Workspace dependencies
1717
bytemuck = { workspace = true }
18-
glam = { version = "0.29", default-features = false, features = ["nostd-libm", "scalar-math"] }
18+
glam = { workspace = true }
1919
half = { workspace = true, default-features = false }
2020
num-derive = { workspace = true }
2121
num-traits = { workspace = true }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::fmt::Display;
22
use core::hash::{Hash, Hasher};
3-
#[cfg(target_arch = "spirv")]
3+
#[cfg(not(feature = "std"))]
44
use num_traits::float::Float;
55

66
#[derive(Debug, Clone, Copy, PartialEq)]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bytemuck::{Pod, Zeroable};
33
use core::fmt::Debug;
44
use glam::DVec2;
55
use num_derive::*;
6-
#[cfg(target_arch = "spirv")]
6+
#[cfg(not(feature = "std"))]
77
use num_traits::float::Float;
88

99
pub trait Linear {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use bytemuck::{Pod, Zeroable};
44
use core::fmt::Debug;
55
use core::hash::Hash;
66
use half::f16;
7-
#[cfg(target_arch = "spirv")]
7+
#[cfg(not(feature = "std"))]
88
use num_traits::Euclid;
9-
#[cfg(target_arch = "spirv")]
9+
#[cfg(not(feature = "std"))]
1010
use num_traits::float::Float;
1111

1212
#[repr(C)]
@@ -439,9 +439,9 @@ impl Color {
439439
lightness + saturation - lightness * saturation
440440
};
441441
let temp2 = 2. * lightness - temp1;
442-
#[cfg(not(target_arch = "spirv"))]
442+
#[cfg(feature = "std")]
443443
let rem = |x: f32| x.rem_euclid(1.);
444-
#[cfg(target_arch = "spirv")]
444+
#[cfg(not(feature = "std"))]
445445
let rem = |x: f32| x.rem_euclid(&1.);
446446

447447
let mut red = rem(hue + 1. / 3.);
@@ -700,7 +700,7 @@ impl Color {
700700
if c_s <= 0.5 {
701701
c_b - (1. - 2. * c_s) * c_b * (1. - c_b)
702702
} else {
703-
let d: fn(f32) -> f32 = |x| if x <= 0.25 { ((16. * x - 12.) * x + 4.) * x } else { x.sqrt() };
703+
let d = |x: f32| if x <= 0.25 { ((16. * x - 12.) * x + 4.) * x } else { x.sqrt() };
704704
c_b + (2. * c_s - 1.) * (d(c_b) - c_b)
705705
}
706706
}
@@ -892,9 +892,9 @@ impl Color {
892892
} else {
893893
4. + (self.red - self.green) / (max_channel - min_channel)
894894
} / 6.;
895-
#[cfg(not(target_arch = "spirv"))]
895+
#[cfg(feature = "std")]
896896
let hue = hue.rem_euclid(1.);
897-
#[cfg(target_arch = "spirv")]
897+
#[cfg(not(feature = "std"))]
898898
let hue = hue.rem_euclid(&1.);
899899

900900
[hue, saturation, lightness, self.alpha]

node-graph/gcore/src/types.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::any::TypeId;
22

33
pub use std::borrow::Cow;
4+
use std::fmt::{Display, Formatter};
45
use std::ops::Deref;
56

67
#[macro_export]
@@ -160,6 +161,12 @@ impl Deref for ProtoNodeIdentifier {
160161
}
161162
}
162163

164+
impl Display for ProtoNodeIdentifier {
165+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
166+
f.debug_tuple("ProtoNodeIdentifier").field(&self.name).finish()
167+
}
168+
}
169+
163170
fn migrate_type_descriptor_names<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Cow<'static, str>, D::Error> {
164171
use serde::Deserialize;
165172

node-graph/graster-nodes/Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ std = [
1818
"dep:fastnoise-lite",
1919
"dep:serde",
2020
"dep:specta",
21-
"dep:glam",
21+
"dep:kurbo",
22+
"glam/debug-glam-assert",
23+
"glam/serde",
2224
]
2325

2426
[dependencies]
@@ -32,8 +34,8 @@ graphene-core = { workspace = true, optional = true }
3234

3335
# Workspace dependencies
3436
bytemuck = { workspace = true }
35-
# glam is reexported from gcore-shaders in no_std mode
36-
glam = { workspace = true, optional = true }
37+
glam = { workspace = true }
38+
num-traits = { workspace = true }
3739

3840
# Workspace std dependencies
3941
specta = { workspace = true, optional = true }
@@ -43,7 +45,7 @@ rand = { workspace = true, optional = true }
4345
rand_chacha = { workspace = true, optional = true }
4446
fastnoise-lite = { workspace = true, optional = true }
4547
serde = { workspace = true, optional = true }
46-
kurbo = { workspace = true }
48+
kurbo = { workspace = true, optional = true }
4749

4850
[dev-dependencies]
4951
tokio = { workspace = true }

node-graph/graster-nodes/src/adjustments.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ use core::fmt::Debug;
77
use graphene_core::gradient::GradientStops;
88
#[cfg(feature = "std")]
99
use graphene_core::raster_types::{CPU, Raster};
10+
#[cfg(feature = "std")]
1011
use graphene_core::table::Table;
1112
use graphene_core_shaders::color::Color;
1213
use graphene_core_shaders::context::Ctx;
1314
use graphene_core_shaders::registry::types::{Angle, Percentage, SignedPercentage};
15+
#[cfg(not(feature = "std"))]
16+
use num_traits::float::Float;
1417

1518
// TODO: Implement the following:
1619
// Color Balance
@@ -125,7 +128,7 @@ fn make_opaque<T: Adjust<Color>>(
125128
if color.a() == 0. {
126129
return color.with_alpha(1.);
127130
}
128-
Color::from_rgbaf32(color.r() / color.a(), color.g() / color.a(), color.b() / color.a(), 1.).unwrap()
131+
Color::from_rgbaf32_unchecked(color.r() / color.a(), color.g() / color.a(), color.b() / color.a(), 1.)
129132
});
130133
input
131134
}
@@ -295,7 +298,7 @@ fn levels<T: Adjust<Color>>(
295298
// https://stackoverflow.com/a/55233732/775283
296299
// Works the same for gamma and linear color
297300
#[node_macro::node(name("Black & White"), category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
298-
async fn black_and_white<T: Adjust<Color>>(
301+
fn black_and_white<T: Adjust<Color>>(
299302
_: impl Ctx,
300303
#[implementations(
301304
Table<Raster<CPU>>,
@@ -360,7 +363,7 @@ async fn black_and_white<T: Adjust<Color>>(
360363
// TODO: Fix "Color" blend mode implementation so it matches the expected behavior perfectly (it's currently close)
361364
let color = tint.with_luminance(luminance);
362365

363-
let color = Color::from_rgbaf32(color.r(), color.g(), color.b(), alpha_part).unwrap();
366+
let color = Color::from_rgbaf32_unchecked(color.r(), color.g(), color.b(), alpha_part);
364367

365368
color.to_linear_srgb()
366369
});
@@ -371,7 +374,7 @@ async fn black_and_white<T: Adjust<Color>>(
371374
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=%27hue%20%27%20%3D%20Old,saturation%2C%20Photoshop%205.0
372375
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=0%20%3D%20Use%20other.-,Hue/Saturation,-Hue/Saturation%20settings
373376
#[node_macro::node(name("Hue/Saturation"), category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
374-
async fn hue_saturation<T: Adjust<Color>>(
377+
fn hue_saturation<T: Adjust<Color>>(
375378
_: impl Ctx,
376379
#[implementations(
377380
Table<Raster<CPU>>,
@@ -406,7 +409,7 @@ async fn hue_saturation<T: Adjust<Color>>(
406409
// Aims for interoperable compatibility with:
407410
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=%27%20%3D%20Color%20Lookup-,%27nvrt%27%20%3D%20Invert,-%27post%27%20%3D%20Posterize
408411
#[node_macro::node(category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
409-
async fn invert<T: Adjust<Color>>(
412+
fn invert<T: Adjust<Color>>(
410413
_: impl Ctx,
411414
#[implementations(
412415
Table<Raster<CPU>>,
@@ -429,7 +432,7 @@ async fn invert<T: Adjust<Color>>(
429432
// Aims for interoperable compatibility with:
430433
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=post%27%20%3D%20Posterize-,%27thrs%27%20%3D%20Threshold,-%27grdm%27%20%3D%20Gradient
431434
#[node_macro::node(category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
432-
async fn threshold<T: Adjust<Color>>(
435+
fn threshold<T: Adjust<Color>>(
433436
_: impl Ctx,
434437
#[implementations(
435438
Table<Raster<CPU>>,
@@ -475,7 +478,7 @@ async fn threshold<T: Adjust<Color>>(
475478
// When both parameters are set, it is equivalent to running this adjustment twice, with only vibrance set and then only saturation set.
476479
// (Except for some noise probably due to rounding error.)
477480
#[node_macro::node(category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
478-
async fn vibrance<T: Adjust<Color>>(
481+
fn vibrance<T: Adjust<Color>>(
479482
_: impl Ctx,
480483
#[implementations(
481484
Table<Raster<CPU>>,
@@ -641,7 +644,7 @@ pub enum DomainWarpType {
641644
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=%27mixr%27%20%3D%20Channel%20Mixer
642645
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=Lab%20color%20only-,Channel%20Mixer,-Key%20is%20%27mixr
643646
#[node_macro::node(category("Raster: Adjustment"), properties("channel_mixer_properties"), shader_node(PerPixelAdjust))]
644-
async fn channel_mixer<T: Adjust<Color>>(
647+
fn channel_mixer<T: Adjust<Color>>(
645648
_: impl Ctx,
646649
#[implementations(
647650
Table<Raster<CPU>>,
@@ -770,7 +773,7 @@ pub enum SelectiveColorChoice {
770773
// Algorithm based on:
771774
// https://blog.pkh.me/p/22-understanding-selective-coloring-in-adobe-photoshop.html
772775
#[node_macro::node(category("Raster: Adjustment"), properties("selective_color_properties"), shader_node(PerPixelAdjust))]
773-
async fn selective_color<T: Adjust<Color>>(
776+
fn selective_color<T: Adjust<Color>>(
774777
_: impl Ctx,
775778
#[implementations(
776779
Table<Raster<CPU>>,
@@ -913,7 +916,7 @@ async fn selective_color<T: Adjust<Color>>(
913916
// https://www.axiomx.com/posterize.htm
914917
// This algorithm produces fully accurate output in relation to the industry standard.
915918
#[node_macro::node(category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
916-
async fn posterize<T: Adjust<Color>>(
919+
fn posterize<T: Adjust<Color>>(
917920
_: impl Ctx,
918921
#[implementations(
919922
Table<Raster<CPU>>,
@@ -947,7 +950,7 @@ async fn posterize<T: Adjust<Color>>(
947950
// Algorithm based on:
948951
// https://geraldbakker.nl/psnumbers/exposure.html
949952
#[node_macro::node(category("Raster: Adjustment"), properties("exposure_properties"), shader_node(PerPixelAdjust))]
950-
async fn exposure<T: Adjust<Color>>(
953+
fn exposure<T: Adjust<Color>>(
951954
_: impl Ctx,
952955
#[implementations(
953956
Table<Raster<CPU>>,

node-graph/graster-nodes/src/blending_nodes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::adjust::Adjust;
33
use graphene_core::gradient::GradientStops;
44
#[cfg(feature = "std")]
55
use graphene_core::raster_types::{CPU, Raster};
6+
#[cfg(feature = "std")]
67
use graphene_core::table::Table;
78
use graphene_core_shaders::Ctx;
89
use graphene_core_shaders::blending::BlendMode;
@@ -132,7 +133,7 @@ pub fn apply_blend_mode(foreground: Color, background: Color, blend_mode: BlendM
132133
}
133134

134135
#[node_macro::node(category("Raster"), shader_node(PerPixelAdjust))]
135-
async fn blend<T: Blend<Color> + Send>(
136+
fn blend<T: Blend<Color> + Send>(
136137
_: impl Ctx,
137138
#[implementations(
138139
Table<Raster<CPU>>,

0 commit comments

Comments
 (0)