Skip to content

Commit 4fec248

Browse files
authored
Shaders: add gcore-shaders and make graster-nodes no-std (#2925)
* gcore-shaders: add crate, move `color` and `blending` from gcore * gcore-shaders: move `AsU32` * gcore-shaders: move `ChoiceType`, switch `Cow` for `&str`, adjust node macro * gcore-shaders: move `registry::types` * gcore-shaders: move `context::Ctx` * raster-nodes: make it `no_std` with `std` feature * gcore-shaders: fix doctest
1 parent 4d5a1a6 commit 4fec248

File tree

28 files changed

+360
-243
lines changed

28 files changed

+360
-243
lines changed

Cargo.lock

Lines changed: 19 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ members = [
77
"node-graph/gapplication-io",
88
"node-graph/gbrush",
99
"node-graph/gcore",
10+
"node-graph/gcore-shaders",
1011
"node-graph/gstd",
1112
"node-graph/gmath-nodes",
1213
"node-graph/gpath-bool",
@@ -28,6 +29,7 @@ default-members = [
2829
"frontend/wasm",
2930
"node-graph/gbrush",
3031
"node-graph/gcore",
32+
"node-graph/gcore-shaders",
3133
"node-graph/gstd",
3234
"node-graph/gmath-nodes",
3335
"node-graph/gpath-bool",
@@ -50,6 +52,7 @@ path-bool = { path = "libraries/path-bool" }
5052
graphene-application-io = { path = "node-graph/gapplication-io" }
5153
graphene-brush = { path = "node-graph/gbrush" }
5254
graphene-core = { path = "node-graph/gcore" }
55+
graphene-core-shaders = { path = "node-graph/gcore-shaders" }
5356
graphene-math-nodes = { path = "node-graph/gmath-nodes" }
5457
graphene-path-bool = { path = "node-graph/gpath-bool" }
5558
graph-craft = { path = "node-graph/graph-craft" }
@@ -166,6 +169,7 @@ opt-level = 1
166169

167170
[profile.dev.package]
168171
graphite-editor = { opt-level = 1 }
172+
graphene-core-shaders = { opt-level = 1 }
169173
graphene-core = { opt-level = 1 }
170174
graphene-std = { opt-level = 1 }
171175
interpreted-executor = { opt-level = 1 } # This is a mitigation for https://github.com/rustwasm/wasm-pack/issues/981 which is needed because the node_registry function is too large

editor/src/messages/portfolio/document/node_graph/node_properties.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,7 +1938,7 @@ pub mod choice {
19381938
use super::ParameterWidgetsInfo;
19391939
use crate::messages::tool::tool_messages::tool_prelude::*;
19401940
use graph_craft::document::value::TaggedValue;
1941-
use graphene_std::registry::{ChoiceTypeStatic, ChoiceWidgetHint};
1941+
use graphene_std::choice_type::{ChoiceTypeStatic, ChoiceWidgetHint};
19421942
use std::marker::PhantomData;
19431943

19441944
pub trait WidgetFactory {
@@ -1998,10 +1998,7 @@ pub mod choice {
19981998
.map(|(item, metadata)| {
19991999
let updater = updater_factory();
20002000
let committer = committer_factory();
2001-
MenuListEntry::new(metadata.name.as_ref())
2002-
.label(metadata.label.as_ref())
2003-
.on_update(move |_| updater(item))
2004-
.on_commit(committer)
2001+
MenuListEntry::new(metadata.name).label(metadata.label).on_update(move |_| updater(item)).on_commit(committer)
20052002
})
20062003
.collect()
20072004
})
@@ -2020,11 +2017,11 @@ pub mod choice {
20202017
.map(|(item, var_meta)| {
20212018
let updater = updater_factory();
20222019
let committer = committer_factory();
2023-
let entry = RadioEntryData::new(var_meta.name.as_ref()).on_update(move |_| updater(item)).on_commit(committer);
2020+
let entry = RadioEntryData::new(var_meta.name).on_update(move |_| updater(item)).on_commit(committer);
20242021
match (var_meta.icon.as_deref(), var_meta.docstring.as_deref()) {
2025-
(None, None) => entry.label(var_meta.label.as_ref()),
2026-
(None, Some(doc)) => entry.label(var_meta.label.as_ref()).tooltip(doc),
2027-
(Some(icon), None) => entry.icon(icon).tooltip(var_meta.label.as_ref()),
2022+
(None, None) => entry.label(var_meta.label),
2023+
(None, Some(doc)) => entry.label(var_meta.label).tooltip(doc),
2024+
(Some(icon), None) => entry.icon(icon).tooltip(var_meta.label),
20282025
(Some(icon), Some(doc)) => entry.icon(icon).tooltip(format!("{}\n\n{}", var_meta.label, doc)),
20292026
}
20302027
})

editor/src/messages/portfolio/menu_bar/menu_bar_message_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl LayoutHolder for MenuBarMessageHandler {
419419
action: MenuBarEntry::no_action(),
420420
disabled: no_active_document || !has_selected_layers,
421421
children: MenuBarEntryChildren(vec![{
422-
let list = <BooleanOperation as graphene_std::registry::ChoiceTypeStatic>::list();
422+
let list = <BooleanOperation as graphene_std::choice_type::ChoiceTypeStatic>::list();
423423
list.iter()
424424
.flat_map(|i| i.iter())
425425
.map(move |(operation, info)| MenuBarEntry {

editor/src/messages/tool/tool_messages/select_tool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl SelectTool {
187187
}
188188

189189
fn boolean_widgets(&self, selected_count: usize) -> impl Iterator<Item = WidgetHolder> + use<> {
190-
let list = <BooleanOperation as graphene_std::registry::ChoiceTypeStatic>::list();
190+
let list = <BooleanOperation as graphene_std::choice_type::ChoiceTypeStatic>::list();
191191
list.iter().flat_map(|i| i.iter()).map(move |(operation, info)| {
192192
let mut tooltip = info.label.to_string();
193193
if let Some(doc) = info.docstring.as_deref() {

node-graph/gcore-shaders/Cargo.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[package]
2+
name = "graphene-core-shaders"
3+
version = "0.1.0"
4+
edition = "2024"
5+
description = "no_std API definitions for Graphene"
6+
authors = ["Graphite Authors <[email protected]>"]
7+
license = "MIT OR Apache-2.0"
8+
9+
[features]
10+
std = ["dep:dyn-any", "dep:serde", "dep:specta", "dep:log"]
11+
12+
[dependencies]
13+
# Local std dependencies
14+
dyn-any = { workspace = true, optional = true }
15+
16+
# Workspace dependencies
17+
bytemuck = { workspace = true }
18+
glam = { version = "0.29", default-features = false, features = ["nostd-libm", "scalar-math"] }
19+
half = { workspace = true }
20+
num-derive = { workspace = true }
21+
num-traits = { workspace = true }
22+
23+
# Workspace std dependencies
24+
serde = { workspace = true, optional = true }
25+
specta = { workspace = true, optional = true }
26+
log = { workspace = true, optional = true }
27+
28+
[dev-dependencies]
29+
graphene-core = { workspace = true }
30+
31+
[lints.rust]
32+
# the spirv target is not in the list of common cfgs so must be added manually
33+
unexpected_cfgs = { level = "warn", check-cfg = [
34+
'cfg(target_arch, values("spirv"))',
35+
] }

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use dyn_any::DynAny;
2-
use std::hash::Hash;
1+
use core::fmt::Display;
2+
use core::hash::{Hash, Hasher};
33

4-
#[derive(Copy, Clone, Debug, PartialEq, DynAny, specta::Type, serde::Serialize, serde::Deserialize)]
5-
#[serde(default)]
4+
#[derive(Debug, Clone, Copy, PartialEq)]
5+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
6+
#[cfg_attr(feature = "std", serde(default))]
67
pub struct AlphaBlending {
78
pub blend_mode: BlendMode,
89
pub opacity: f32,
@@ -15,14 +16,14 @@ impl Default for AlphaBlending {
1516
}
1617
}
1718
impl Hash for AlphaBlending {
18-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
19+
fn hash<H: Hasher>(&self, state: &mut H) {
1920
self.opacity.to_bits().hash(state);
2021
self.fill.to_bits().hash(state);
2122
self.blend_mode.hash(state);
2223
self.clip.hash(state);
2324
}
2425
}
25-
impl std::fmt::Display for AlphaBlending {
26+
impl Display for AlphaBlending {
2627
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2728
let round = |x: f32| (x * 1e3).round() / 1e3;
2829
write!(
@@ -62,9 +63,9 @@ impl AlphaBlending {
6263
}
6364
}
6465

65-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
66-
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, DynAny, Hash, specta::Type)]
6766
#[repr(i32)]
67+
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
68+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
6869
pub enum BlendMode {
6970
// Basic group
7071
#[default]
@@ -189,18 +190,19 @@ impl BlendMode {
189190
}
190191

191192
/// Renders the blend mode CSS style declaration.
193+
#[cfg(feature = "std")]
192194
pub fn render(&self) -> String {
193195
format!(
194196
r#" mix-blend-mode: {};"#,
195197
self.to_svg_style_name().unwrap_or_else(|| {
196-
warn!("Unsupported blend mode {self:?}");
198+
log::warn!("Unsupported blend mode {self:?}");
197199
"normal"
198200
})
199201
)
200202
}
201203
}
202204

203-
impl std::fmt::Display for BlendMode {
205+
impl Display for BlendMode {
204206
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
205207
match self {
206208
// Normal group
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pub trait ChoiceTypeStatic: Sized + Copy + crate::AsU32 + Send + Sync {
2+
const WIDGET_HINT: ChoiceWidgetHint;
3+
const DESCRIPTION: Option<&'static str>;
4+
fn list() -> &'static [&'static [(Self, VariantMetadata)]];
5+
}
6+
7+
pub enum ChoiceWidgetHint {
8+
Dropdown,
9+
RadioButtons,
10+
}
11+
12+
/// Translation struct between macro and definition.
13+
#[derive(Clone, Debug)]
14+
pub struct VariantMetadata {
15+
/// Name as declared in source code.
16+
pub name: &'static str,
17+
18+
/// Name to be displayed in UI.
19+
pub label: &'static str,
20+
21+
/// User-facing documentation text.
22+
pub docstring: Option<&'static str>,
23+
24+
/// Name of icon to display in radio buttons and such.
25+
pub icon: Option<&'static str>,
26+
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
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 dyn_any::DynAny;
4+
use core::hash::Hash;
55
use half::f16;
66
#[cfg(target_arch = "spirv")]
77
use spirv_std::num_traits::Euclid;
88
#[cfg(target_arch = "spirv")]
99
use spirv_std::num_traits::float::Float;
10-
use std::hash::Hash;
1110

1211
#[repr(C)]
13-
#[derive(Debug, Default, Clone, Copy, PartialEq, DynAny, Pod, Zeroable, serde::Serialize, serde::Deserialize)]
12+
#[derive(Debug, Default, Clone, Copy, PartialEq, Pod, Zeroable)]
13+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, serde::Serialize, serde::Deserialize))]
1414
pub struct RGBA16F {
1515
red: f16,
1616
green: f16,
@@ -82,7 +82,8 @@ impl Alpha for RGBA16F {
8282
impl Pixel for RGBA16F {}
8383

8484
#[repr(C)]
85-
#[derive(Debug, Default, Clone, Copy, PartialEq, DynAny, Pod, Zeroable, specta::Type, serde::Serialize, serde::Deserialize)]
85+
#[derive(Debug, Default, Clone, Copy, PartialEq, Pod, Zeroable)]
86+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
8687
pub struct SRGBA8 {
8788
red: u8,
8889
green: u8,
@@ -162,7 +163,8 @@ impl Alpha for SRGBA8 {
162163
impl Pixel for SRGBA8 {}
163164

164165
#[repr(C)]
165-
#[derive(Debug, Default, Clone, Copy, PartialEq, DynAny, Pod, Zeroable, specta::Type, serde::Serialize, serde::Deserialize)]
166+
#[derive(Debug, Default, Clone, Copy, PartialEq, Pod, Zeroable)]
167+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
166168
pub struct Luma(pub f32);
167169

168170
impl Luminance for Luma {
@@ -202,7 +204,8 @@ impl Pixel for Luma {}
202204
/// The other components (RGB) are stored as `f32` that range from `0.0` up to `f32::MAX`,
203205
/// the values encode the brightness of each channel proportional to the light intensity in cd/m² (nits) in HDR, and `0.0` (black) to `1.0` (white) in SDR color.
204206
#[repr(C)]
205-
#[derive(Debug, Default, Clone, Copy, PartialEq, DynAny, Pod, Zeroable, specta::Type, serde::Serialize, serde::Deserialize)]
207+
#[derive(Debug, Default, Clone, Copy, PartialEq, Pod, Zeroable)]
208+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
206209
pub struct Color {
207210
red: f32,
208211
green: f32,

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
pub use crate::blending::*;
12
use bytemuck::{Pod, Zeroable};
3+
use core::fmt::Debug;
24
use glam::DVec2;
3-
use std::fmt::Debug;
4-
5+
use num_derive::*;
56
#[cfg(target_arch = "spirv")]
6-
use spirv_std::num_traits::float::Float;
7-
8-
pub use crate::blending::*;
7+
use num_traits::float::Float;
98

109
pub trait Linear {
1110
fn from_f32(x: f32) -> Self;
@@ -64,7 +63,6 @@ impl<T: Linear + Debug + Copy> Channel for T {
6463

6564
impl<T: Linear + Debug + Copy> LinearChannel for T {}
6665

67-
use num_derive::*;
6866
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Num, NumCast, NumOps, One, Zero, ToPrimitive, FromPrimitive)]
6967
pub struct SRGBGammaFloat(f32);
7068

@@ -97,14 +95,6 @@ impl<T: Rec709Primaries> RGBPrimaries for T {
9795

9896
pub trait SRGB: Rec709Primaries {}
9997

100-
pub trait Serde: serde::Serialize + for<'a> serde::Deserialize<'a> {}
101-
#[cfg(not(feature = "serde"))]
102-
pub trait Serde {}
103-
104-
impl<T: serde::Serialize + for<'a> serde::Deserialize<'a>> Serde for T {}
105-
#[cfg(not(feature = "serde"))]
106-
impl<T> Serde for T {}
107-
10898
// TODO: Come up with a better name for this trait
10999
pub trait Pixel: Clone + Pod + Zeroable + Default {
110100
#[cfg(not(target_arch = "spirv"))]

0 commit comments

Comments
 (0)