Skip to content

Commit 95ef8a5

Browse files
Change Table<Color> node inputs to Color where only one value is used so GPU nodes work (#3096)
* graster-nodes: change `Table<Color>` params to `Color` where only one value is used * Re-add support for Color and Option<Color> * Add warning when a default value isn't parsed --------- Co-authored-by: hypercube <[email protected]>
1 parent 9987112 commit 95ef8a5

File tree

8 files changed

+46
-23
lines changed

8 files changed

+46
-23
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.

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ pub(crate) fn property_from_type(
173173
Some(x) if x == TypeId::of::<String>() => text_widget(default_info).into(),
174174
Some(x) if x == TypeId::of::<DVec2>() => vec2_widget(default_info, "X", "Y", "", None, false),
175175
Some(x) if x == TypeId::of::<DAffine2>() => transform_widget(default_info, &mut extra_widgets),
176+
Some(x) if x == TypeId::of::<Color>() => color_widget(default_info, ColorInput::default()),
177+
Some(x) if x == TypeId::of::<Option<Color>>() => color_widget(default_info, ColorInput::default()),
176178
// ==========================
177179
// PRIMITIVE COLLECTION TYPES
178180
// ==========================
@@ -926,6 +928,22 @@ pub fn color_widget(parameter_widgets_info: ParameterWidgetsInfo, color_button:
926928

927929
// Add the color input
928930
match &**tagged_value {
931+
TaggedValue::ColorNotInTable(color) => widgets.push(
932+
color_button
933+
.value(FillChoice::Solid(*color))
934+
.allow_none(false)
935+
.on_update(update_value(|input: &ColorInput| TaggedValue::ColorNotInTable(input.value.as_solid().unwrap()), node_id, index))
936+
.on_commit(commit_value)
937+
.widget_holder(),
938+
),
939+
TaggedValue::OptionalColorNotInTable(color) => widgets.push(
940+
color_button
941+
.value(color.map_or(FillChoice::None, FillChoice::Solid))
942+
.allow_none(true)
943+
.on_update(update_value(|input: &ColorInput| TaggedValue::OptionalColorNotInTable(input.value.as_solid()), node_id, index))
944+
.on_commit(commit_value)
945+
.widget_holder(),
946+
),
929947
TaggedValue::Color(color_table) => widgets.push(
930948
color_button
931949
.value(match color_table.iter().next() {
@@ -965,7 +983,7 @@ pub fn color_widget(parameter_widgets_info: ParameterWidgetsInfo, color_button:
965983
.on_commit(commit_value)
966984
.widget_holder(),
967985
),
968-
_ => {}
986+
x => warn!("Colour {x:?}"),
969987
}
970988

971989
LayoutGroup::Row { widgets }

node-graph/graph-craft/src/document/value.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ tagged_value! {
171171
Bool(bool),
172172
String(String),
173173
OptionalF64(Option<f64>),
174+
ColorNotInTable(Color),
175+
OptionalColorNotInTable(Option<Color>),
174176
// ========================
175177
// LISTS OF PRIMITIVE TYPES
176178
// ========================
@@ -358,6 +360,8 @@ impl TaggedValue {
358360
x if x == TypeId::of::<DVec2>() => to_dvec2(string).map(TaggedValue::DVec2)?,
359361
x if x == TypeId::of::<bool>() => FromStr::from_str(string).map(TaggedValue::Bool).ok()?,
360362
x if x == TypeId::of::<Table<Color>>() => to_color(string).map(|color| TaggedValue::Color(Table::new_from_element(color)))?,
363+
x if x == TypeId::of::<Color>() => to_color(string).map(|color| TaggedValue::ColorNotInTable(color))?,
364+
x if x == TypeId::of::<Option<Color>>() => TaggedValue::ColorNotInTable(to_color(string)?),
361365
x if x == TypeId::of::<Fill>() => to_color(string).map(|color| TaggedValue::Fill(Fill::solid(color)))?,
362366
x if x == TypeId::of::<ReferencePoint>() => to_reference_point(string).map(TaggedValue::ReferencePoint)?,
363367
_ => return None,
@@ -512,3 +516,8 @@ mod fake_hash {
512516
}
513517
}
514518
}
519+
520+
#[test]
521+
fn can_construct_color() {
522+
assert_eq!(TaggedValue::from_type(&concrete!(Color)).unwrap(), TaggedValue::ColorNotInTable(Color::default()));
523+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fn black_and_white<T: Adjust<Color>>(
307307
GradientStops,
308308
)]
309309
mut image: T,
310-
#[default(Color::BLACK)] tint: Table<Color>,
310+
#[default(Color::BLACK)] tint: Color,
311311
#[default(40.)]
312312
#[range((-200., 300.))]
313313
reds: PercentageF32,
@@ -327,9 +327,6 @@ fn black_and_white<T: Adjust<Color>>(
327327
#[range((-200., 300.))]
328328
magentas: PercentageF32,
329329
) -> T {
330-
let tint: Option<Color> = tint.into();
331-
let tint = tint.unwrap_or(Color::BLACK);
332-
333330
image.adjust(|color| {
334331
let color = color.to_gamma_srgb();
335332

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,12 @@ fn color_overlay<T: Adjust<Color>>(
166166
GradientStops,
167167
)]
168168
mut image: T,
169-
#[default(Color::BLACK)] color: Table<Color>,
169+
#[default(Color::BLACK)] color: Color,
170170
blend_mode: BlendMode,
171171
#[default(100.)] opacity: PercentageF32,
172172
) -> T {
173173
let opacity = (opacity as f32 / 100.).clamp(0., 1.);
174174

175-
let color: Option<Color> = color.into();
176-
let color = color.unwrap_or(Color::BLACK);
177-
178175
image.adjust(|pixel| {
179176
let image = pixel.map_rgb(|channel| channel * (1. - opacity));
180177

@@ -206,13 +203,7 @@ mod test {
206203
// 100% of the output should come from the multiplied value
207204
let opacity = 100.;
208205

209-
let result = super::color_overlay(
210-
(),
211-
Table::new_from_element(Raster::new_cpu(image.clone())),
212-
Table::new_from_element(overlay_color),
213-
BlendMode::Multiply,
214-
opacity,
215-
);
206+
let result = super::color_overlay((), Table::new_from_element(Raster::new_cpu(image.clone())), overlay_color, BlendMode::Multiply, opacity);
216207
let result = result.iter().next().unwrap().element;
217208

218209
// The output should just be the original green and alpha channels (as we multiply them by 1 and other channels by 0)

node-graph/gstd/src/text.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ fn text<'i: 'n>(
1717
#[unit(" px")]
1818
#[default(0.)]
1919
character_spacing: f64,
20-
#[unit(" px")]
21-
#[default(None)]
22-
max_width: Option<f64>,
23-
#[unit(" px")]
24-
#[default(None)]
25-
max_height: Option<f64>,
20+
#[unit(" px")] max_width: Option<f64>,
21+
#[unit(" px")] max_height: Option<f64>,
2622
/// Faux italic.
2723
#[unit("°")]
2824
#[default(0.)]

node-graph/preprocessor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license = "MIT OR Apache-2.0"
77
[features]
88

99
[dependencies]
10+
log = { workspace = true }
1011

1112
# Workspace dependencies
1213
graphene-std = { workspace = true, features = ["gpu"] }

node-graph/preprocessor/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[macro_use]
2+
extern crate log;
3+
14
use graph_craft::document::value::*;
25
use graph_craft::document::*;
36
use graph_craft::proto::RegistryValueSource;
@@ -150,7 +153,14 @@ pub fn node_inputs(fields: &[registry::FieldMetadata], first_node_io: &NodeIOTyp
150153

151154
match field.value_source {
152155
RegistryValueSource::None => {}
153-
RegistryValueSource::Default(data) => return NodeInput::value(TaggedValue::from_primitive_string(data, ty).unwrap_or(TaggedValue::None), exposed),
156+
RegistryValueSource::Default(data) => {
157+
if let Some(custom_default) = TaggedValue::from_primitive_string(data, ty) {
158+
return NodeInput::value(custom_default, exposed);
159+
} else {
160+
// It is incredibly useful to get a warning when the default type cannot be parsed rather than defaulting to `()`.
161+
warn!("Failed to parse default value for type {ty:?} with data {data}");
162+
}
163+
}
154164
RegistryValueSource::Scope(data) => return NodeInput::scope(Cow::Borrowed(data)),
155165
};
156166

0 commit comments

Comments
 (0)