From c22fb0ec3a9bccf2f60896df1a3ce9bd8cd459ed Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sun, 1 Nov 2026 09:19:27 +0000 Subject: [PATCH] Add an Image to Channels GPU node for GPU rasters, since a multi-output struct can't be generic over its raster --- node-graph/nodes/raster/src/adjustments.rs | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/node-graph/nodes/raster/src/adjustments.rs b/node-graph/nodes/raster/src/adjustments.rs index 90c35e4382..77f9333a02 100644 --- a/node-graph/nodes/raster/src/adjustments.rs +++ b/node-graph/nodes/raster/src/adjustments.rs @@ -195,6 +195,55 @@ fn image_to_channels(_: impl Ctx, image: Item>) -> ImageChannels { ImageChannels { red, green, blue, alpha } } +/// The red, green, blue, and alpha channels of an image, split into separate node outputs. +#[derive(Debug, Clone, dyn_any::DynAny, node_macro::Destructure)] +pub struct ImageChannelsGpu { + /// The red channel of the image, as a grayscale image. + pub red: Item>, + /// The green channel of the image, as a grayscale image. + pub green: Item>, + /// The blue channel of the image, as a grayscale image. + pub blue: Item>, + /// The alpha channel of the image, as a grayscale image. + pub alpha: Item>, +} + +/// Separates an image into its red, green, blue, and alpha channels on the GPU, each provided as a grayscale image. +#[node_macro::node(category("Raster"), icon("NodeImageToChannels"), name("Image to Channels GPU"))] +fn image_to_channels_gpu(_: impl Ctx, image: Item>, #[scope(raster_kernel_pipeline::IDENTIFIER)] pipeline: Item) -> Result { + let [red, green, blue, alpha] = [0, 1, 2, 3].map(|channel| image.clone().adjust(&ExtractChannel { channel }, pipeline.element())); + + Ok(ImageChannelsGpu { + red: red?, + green: green?, + blue: blue?, + alpha: alpha?, + }) +} + +gpu_params! { + #[derive(Clone)] + struct ExtractChannel { + channel: u32, + } +} + +impl MapColor for ExtractChannel { + fn map_cpu(&self, color: Color) -> Color { + let value = [color.r(), color.g(), color.b(), color.a()][self.channel as usize]; + Color::from_rgbaf32_unchecked(value, value, value, 1.) + } + + fn wgsl() -> Wgsl { + Wgsl::body( + r#" + let value = color[channel]; + return vec4(value, value, value, 1.); + "#, + ) + } +} + #[node_macro::node(category("Raster: Adjustment"), icon("NodeMakeOpaque"), version(2))] fn make_opaque( _: impl Ctx,