mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
Instance tables refactor part 7: Remove RasterDataType and add Raster<CPU>/Raster<GPU>
This commit is contained in:
@@ -6,8 +6,9 @@ use graphene_core::instances::Instance;
|
||||
use graphene_core::raster::adjustments::blend_colors;
|
||||
use graphene_core::raster::bbox::{AxisAlignedBbox, Bbox};
|
||||
use graphene_core::raster::brush_cache::BrushCache;
|
||||
use graphene_core::raster::image::{Image, RasterDataTable};
|
||||
use graphene_core::raster::image::Image;
|
||||
use graphene_core::raster::{Alpha, BitmapMut, BlendMode, Color, Pixel, Sample};
|
||||
use graphene_core::raster_types::{CPU, Raster, RasterDataTable};
|
||||
use graphene_core::renderer::GraphicElementRendered;
|
||||
use graphene_core::transform::Transform;
|
||||
use graphene_core::value::ClonedNode;
|
||||
@@ -80,11 +81,10 @@ fn brush_stamp_generator(diameter: f64, color: Color, hardness: f64, flow: f64)
|
||||
}
|
||||
|
||||
#[node_macro::node(skip_impl)]
|
||||
fn blit<P, BlendFn>(mut target: RasterDataTable<P>, texture: Image<P>, positions: Vec<DVec2>, blend_mode: BlendFn) -> RasterDataTable<P>
|
||||
fn blit<BlendFn>(mut target: RasterDataTable<CPU>, texture: Raster<CPU>, positions: Vec<DVec2>, blend_mode: BlendFn) -> RasterDataTable<CPU>
|
||||
where
|
||||
P: Pixel + Alpha + std::fmt::Debug,
|
||||
BlendFn: for<'any_input> Node<'any_input, (P, P), Output = P>,
|
||||
GraphicElement: From<Image<P>>,
|
||||
BlendFn: for<'any_input> Node<'any_input, (Color, Color), Output = Color>,
|
||||
GraphicElement: From<Raster<CPU>>,
|
||||
{
|
||||
if positions.is_empty() {
|
||||
return target;
|
||||
@@ -122,7 +122,7 @@ where
|
||||
for y in blit_area_offset.y..blit_area_offset.y + blit_area_dimensions.y {
|
||||
for x in blit_area_offset.x..blit_area_offset.x + blit_area_dimensions.x {
|
||||
let src_pixel = texture.data[texture_index(x, y)];
|
||||
let dst_pixel = &mut target_instance.instance.data[target_index(x + clamp_start.x, y + clamp_start.y)];
|
||||
let dst_pixel = &mut target_instance.instance.data_mut().data[target_index(x + clamp_start.x, y + clamp_start.y)];
|
||||
*dst_pixel = blend_mode.eval((src_pixel, *dst_pixel));
|
||||
}
|
||||
}
|
||||
@@ -132,7 +132,7 @@ where
|
||||
target
|
||||
}
|
||||
|
||||
pub async fn create_brush_texture(brush_style: &BrushStyle) -> Image<Color> {
|
||||
pub async fn create_brush_texture(brush_style: &BrushStyle) -> Raster<CPU> {
|
||||
let stamp = brush_stamp_generator(brush_style.diameter, brush_style.color, brush_style.hardness, brush_style.flow);
|
||||
let transform = DAffine2::from_scale_angle_translation(DVec2::splat(brush_style.diameter), 0., -DVec2::splat(brush_style.diameter / 2.));
|
||||
let blank_texture = empty_image((), transform, Color::TRANSPARENT).instance_iter().next().unwrap_or_default();
|
||||
@@ -141,7 +141,7 @@ pub async fn create_brush_texture(brush_style: &BrushStyle) -> Image<Color> {
|
||||
image.instance
|
||||
}
|
||||
|
||||
pub fn blend_with_mode(background: Instance<Image<Color>>, foreground: Instance<Image<Color>>, blend_mode: BlendMode, opacity: f64) -> Instance<Image<Color>> {
|
||||
pub fn blend_with_mode(background: Instance<Raster<CPU>>, foreground: Instance<Raster<CPU>>, blend_mode: BlendMode, opacity: f64) -> Instance<Raster<CPU>> {
|
||||
let opacity = opacity / 100.;
|
||||
match std::hint::black_box(blend_mode) {
|
||||
// Normal group
|
||||
@@ -184,12 +184,12 @@ pub fn blend_with_mode(background: Instance<Image<Color>>, foreground: Instance<
|
||||
}
|
||||
|
||||
#[node_macro::node(category("Raster"))]
|
||||
async fn brush(_: impl Ctx, mut image_frame_table: RasterDataTable<Color>, strokes: Vec<BrushStroke>, cache: BrushCache) -> RasterDataTable<Color> {
|
||||
async fn brush(_: impl Ctx, mut image_frame_table: RasterDataTable<CPU>, strokes: Vec<BrushStroke>, cache: BrushCache) -> RasterDataTable<CPU> {
|
||||
if image_frame_table.is_empty() {
|
||||
image_frame_table.push(Instance::default());
|
||||
}
|
||||
// TODO: Find a way to handle more than one instance
|
||||
let Some(image_frame_instance) = image_frame_table.instance_ref_iter().next() else {
|
||||
return RasterDataTable::default();
|
||||
};
|
||||
let image_frame_instance = image_frame_instance.to_instance_cloned();
|
||||
let image_frame_instance = image_frame_table.instance_ref_iter().next().expect("Expected the one instance we just pushed").to_instance_cloned();
|
||||
|
||||
let [start, end] = image_frame_instance.clone().to_table().bounding_box(DAffine2::IDENTITY, false).unwrap_or([DVec2::ZERO, DVec2::ZERO]);
|
||||
let image_bbox = AxisAlignedBbox { start, end };
|
||||
@@ -268,7 +268,7 @@ async fn brush(_: impl Ctx, mut image_frame_table: RasterDataTable<Color>, strok
|
||||
if has_erase_strokes {
|
||||
let opaque_image = Image::new(bbox.size().x as u32, bbox.size().y as u32, Color::WHITE);
|
||||
let mut erase_restore_mask = Instance {
|
||||
instance: opaque_image,
|
||||
instance: Raster::new_cpu(opaque_image),
|
||||
transform: background_bounds,
|
||||
..Default::default()
|
||||
};
|
||||
@@ -320,7 +320,7 @@ async fn brush(_: impl Ctx, mut image_frame_table: RasterDataTable<Color>, strok
|
||||
image_frame_table
|
||||
}
|
||||
|
||||
pub fn blend_image_closure(foreground: Instance<Image<Color>>, mut background: Instance<Image<Color>>, map_fn: impl Fn(Color, Color) -> Color) -> Instance<Image<Color>> {
|
||||
pub fn blend_image_closure(foreground: Instance<Raster<CPU>>, mut background: Instance<Raster<CPU>>, map_fn: impl Fn(Color, Color) -> Color) -> Instance<Raster<CPU>> {
|
||||
let foreground_size = DVec2::new(foreground.instance.width as f64, foreground.instance.height as f64);
|
||||
let background_size = DVec2::new(background.instance.width as f64, background.instance.height as f64);
|
||||
|
||||
@@ -340,7 +340,7 @@ pub fn blend_image_closure(foreground: Instance<Image<Color>>, mut background: I
|
||||
let foreground_point = background_to_foreground.transform_point2(background_point);
|
||||
|
||||
let source_pixel = foreground.instance.sample(foreground_point);
|
||||
let Some(destination_pixel) = background.instance.get_pixel_mut(x, y) else { continue };
|
||||
let Some(destination_pixel) = background.instance.data_mut().get_pixel_mut(x, y) else { continue };
|
||||
|
||||
*destination_pixel = map_fn(source_pixel, *destination_pixel);
|
||||
}
|
||||
@@ -349,7 +349,7 @@ pub fn blend_image_closure(foreground: Instance<Image<Color>>, mut background: I
|
||||
background
|
||||
}
|
||||
|
||||
pub fn blend_stamp_closure(foreground: BrushStampGenerator<Color>, mut background: Instance<Image<Color>>, map_fn: impl Fn(Color, Color) -> Color) -> Instance<Image<Color>> {
|
||||
pub fn blend_stamp_closure(foreground: BrushStampGenerator<Color>, mut background: Instance<Raster<CPU>>, map_fn: impl Fn(Color, Color) -> Color) -> Instance<Raster<CPU>> {
|
||||
let background_size = DVec2::new(background.instance.width as f64, background.instance.height as f64);
|
||||
|
||||
// Transforms a point from the background image to the foreground image
|
||||
@@ -369,7 +369,7 @@ pub fn blend_stamp_closure(foreground: BrushStampGenerator<Color>, mut backgroun
|
||||
let foreground_point = background_to_foreground.transform_point2(background_point);
|
||||
|
||||
let Some(source_pixel) = foreground.sample(foreground_point, area) else { continue };
|
||||
let Some(destination_pixel) = background.instance.get_pixel_mut(x, y) else { continue };
|
||||
let Some(destination_pixel) = background.instance.data_mut().get_pixel_mut(x, y) else { continue };
|
||||
|
||||
*destination_pixel = map_fn(source_pixel, *destination_pixel);
|
||||
}
|
||||
@@ -397,7 +397,7 @@ mod test {
|
||||
async fn test_brush_output_size() {
|
||||
let image = brush(
|
||||
(),
|
||||
RasterDataTable::<Color>::new(Image::<Color>::default()),
|
||||
RasterDataTable::<CPU>::new(Raster::new_cpu(Image::<Color>::default())),
|
||||
vec![BrushStroke {
|
||||
trace: vec![crate::vector::brush_stroke::BrushInputSample { position: DVec2::ZERO }],
|
||||
style: BrushStyle {
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
use graph_craft::proto::types::Percentage;
|
||||
use graphene_core::raster::image::{Image, RasterDataTable};
|
||||
use graphene_core::{Color, Ctx};
|
||||
use graphene_core::Ctx;
|
||||
use graphene_core::raster::image::Image;
|
||||
use graphene_core::raster_types::{CPU, Raster, RasterDataTable};
|
||||
use image::{DynamicImage, GenericImage, GenericImageView, GrayImage, ImageBuffer, Luma, Rgba, RgbaImage};
|
||||
use ndarray::{Array2, ArrayBase, Dim, OwnedRepr};
|
||||
use std::cmp::{max, min};
|
||||
|
||||
#[node_macro::node(category("Raster"))]
|
||||
async fn dehaze(_: impl Ctx, image_frame: RasterDataTable<Color>, strength: Percentage) -> RasterDataTable<Color> {
|
||||
async fn dehaze(_: impl Ctx, image_frame: RasterDataTable<CPU>, strength: Percentage) -> RasterDataTable<CPU> {
|
||||
let mut result_table = RasterDataTable::default();
|
||||
|
||||
for mut image_frame_instance in image_frame.instance_iter() {
|
||||
@@ -29,7 +30,7 @@ async fn dehaze(_: impl Ctx, image_frame: RasterDataTable<Color>, strength: Perc
|
||||
base64_string: None,
|
||||
};
|
||||
|
||||
image_frame_instance.instance = dehazed_image;
|
||||
image_frame_instance.instance = Raster::new_cpu(dehazed_image);
|
||||
image_frame_instance.source_node_id = None;
|
||||
result_table.push(image_frame_instance);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use graph_craft::proto::types::PixelLength;
|
||||
use graphene_core::raster::image::{Image, RasterDataTable};
|
||||
use graphene_core::raster::image::Image;
|
||||
use graphene_core::raster::{Bitmap, BitmapMut};
|
||||
use graphene_core::raster_types::{CPU, Raster, RasterDataTable};
|
||||
use graphene_core::{Color, Ctx};
|
||||
|
||||
/// Blurs the image with a Gaussian or blur kernel filter.
|
||||
@@ -8,7 +9,7 @@ use graphene_core::{Color, Ctx};
|
||||
async fn blur(
|
||||
_: impl Ctx,
|
||||
/// The image to be blurred.
|
||||
image_frame: RasterDataTable<Color>,
|
||||
image_frame: RasterDataTable<CPU>,
|
||||
/// The radius of the blur kernel.
|
||||
#[range((0., 100.))]
|
||||
#[hard_min(0.)]
|
||||
@@ -17,7 +18,7 @@ async fn blur(
|
||||
box_blur: bool,
|
||||
/// Opt to incorrectly apply the filter with color calculations in gamma space for compatibility with the results from other software.
|
||||
gamma: bool,
|
||||
) -> RasterDataTable<Color> {
|
||||
) -> RasterDataTable<CPU> {
|
||||
let mut result_table = RasterDataTable::default();
|
||||
|
||||
for mut image_instance in image_frame.instance_iter() {
|
||||
@@ -28,9 +29,9 @@ async fn blur(
|
||||
// Minimum blur radius
|
||||
image.clone()
|
||||
} else if box_blur {
|
||||
box_blur_algorithm(image, radius, gamma)
|
||||
Raster::new_cpu(box_blur_algorithm(image.into_data(), radius, gamma))
|
||||
} else {
|
||||
gaussian_blur_algorithm(image, radius, gamma)
|
||||
Raster::new_cpu(gaussian_blur_algorithm(image.into_data(), radius, gamma))
|
||||
};
|
||||
|
||||
image_instance.instance = blurred_image;
|
||||
|
||||
@@ -1,455 +0,0 @@
|
||||
use glam::{DAffine2, DVec2, Mat2, Vec2};
|
||||
use gpu_executor::{ComputePassDimensions, StorageBufferOptions};
|
||||
use graph_craft::document::value::TaggedValue;
|
||||
use graph_craft::document::*;
|
||||
use graph_craft::proto::*;
|
||||
use graphene_core::raster::BlendMode;
|
||||
use graphene_core::raster::image::{Image, RasterDataTable};
|
||||
use graphene_core::*;
|
||||
use std::sync::Arc;
|
||||
use wgpu_executor::{Bindgroup, PipelineLayout, Shader, ShaderIO, ShaderInput, WgpuExecutor};
|
||||
|
||||
// TODO: Move to graph-craft
|
||||
#[node_macro::node(category("Debug: GPU"))]
|
||||
async fn compile_gpu<'a: 'n>(_: impl Ctx, node: &'a DocumentNode, typing_context: TypingContext, io: ShaderIO) -> Result<compilation_client::Shader, String> {
|
||||
let mut typing_context = typing_context;
|
||||
let compiler = graph_craft::graphene_compiler::Compiler {};
|
||||
let DocumentNodeImplementation::Network(ref network) = node.implementation else { panic!() };
|
||||
let proto_networks: Result<Vec<_>, _> = compiler.compile(network.clone()).collect();
|
||||
let proto_networks = proto_networks?;
|
||||
|
||||
for network in proto_networks.iter() {
|
||||
typing_context.update(network).expect("Failed to type check network");
|
||||
}
|
||||
// TODO: do a proper union
|
||||
let input_types = proto_networks[0]
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|id| typing_context.type_of(*id).unwrap())
|
||||
.map(|node_io| node_io.return_value.clone())
|
||||
.collect();
|
||||
let output_types = proto_networks.iter().map(|network| typing_context.type_of(network.output).unwrap().return_value.clone()).collect();
|
||||
|
||||
Ok(compilation_client::compile(proto_networks, input_types, output_types, io).await.unwrap())
|
||||
}
|
||||
|
||||
#[node_macro::node(category("Debug: GPU"))]
|
||||
async fn blend_gpu_image(_: impl Ctx, foreground: RasterDataTable<Color>, background: RasterDataTable<Color>, blend_mode: BlendMode, opacity: f64) -> RasterDataTable<Color> {
|
||||
let mut result_table = RasterDataTable::default();
|
||||
|
||||
for (foreground_instance, mut background_instance) in foreground.instance_iter().zip(background.instance_iter()) {
|
||||
let foreground_transform = foreground_instance.transform;
|
||||
let background_transform = background_instance.transform;
|
||||
|
||||
let foreground = foreground_instance.instance;
|
||||
let background = background_instance.instance;
|
||||
|
||||
let foreground_size = DVec2::new(foreground.width as f64, foreground.height as f64);
|
||||
let background_size = DVec2::new(background.width as f64, background.height as f64);
|
||||
|
||||
// Transforms a point from the background image to the foreground image
|
||||
let bg_to_fg = DAffine2::from_scale(foreground_size) * foreground_transform.inverse() * background_transform * DAffine2::from_scale(1. / background_size);
|
||||
|
||||
let transform_matrix: Mat2 = bg_to_fg.matrix2.as_mat2();
|
||||
let translation: Vec2 = bg_to_fg.translation.as_vec2();
|
||||
|
||||
log::debug!("Executing gpu blend node!");
|
||||
let compiler = graph_craft::graphene_compiler::Compiler {};
|
||||
|
||||
let network = NodeNetwork {
|
||||
exports: vec![NodeInput::node(NodeId(0), 0)],
|
||||
nodes: [DocumentNode {
|
||||
inputs: vec![NodeInput::Inline(InlineRust::new(
|
||||
format!(
|
||||
r#"graphene_core::raster::adjustments::BlendNode::new(
|
||||
graphene_core::value::CopiedNode::new({}),
|
||||
graphene_core::value::CopiedNode::new({}),
|
||||
).eval((
|
||||
{{
|
||||
let bg_point = Vec2::new(_global_index.x as f32, _global_index.y as f32);
|
||||
let fg_point = (*i4) * bg_point + (*i5);
|
||||
|
||||
if !((fg_point.cmpge(Vec2::ZERO) & bg_point.cmpge(Vec2::ZERO)) == BVec2::new(true, true)) {{
|
||||
Color::from_rgbaf32_unchecked(0., 0., 0., 0.)
|
||||
}} else {{
|
||||
i2[((fg_point.y as u32) * i3 + (fg_point.x as u32)) as usize]
|
||||
}}
|
||||
}},
|
||||
i1[(_global_index.y * i0 + _global_index.x) as usize],
|
||||
))"#,
|
||||
TaggedValue::BlendMode(blend_mode).to_primitive_string(),
|
||||
TaggedValue::F64(opacity).to_primitive_string(),
|
||||
),
|
||||
concrete![Color],
|
||||
))],
|
||||
implementation: DocumentNodeImplementation::ProtoNode("graphene_core::value::CopiedNode".into()),
|
||||
..Default::default()
|
||||
}]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(id, node)| (NodeId(id as u64), node))
|
||||
.collect(),
|
||||
..Default::default()
|
||||
};
|
||||
log::debug!("compiling network");
|
||||
let proto_networks: Result<Vec<_>, _> = compiler.compile(network.clone()).collect();
|
||||
let Ok(proto_networks_result) = proto_networks else {
|
||||
log::error!("Error compiling network in 'blend_gpu_image()");
|
||||
return RasterDataTable::default();
|
||||
};
|
||||
let proto_networks = proto_networks_result;
|
||||
log::debug!("compiling shader");
|
||||
|
||||
let shader = compilation_client::compile(
|
||||
proto_networks,
|
||||
vec![
|
||||
concrete!(u32),
|
||||
concrete!(Color),
|
||||
concrete!(Color),
|
||||
concrete!(u32),
|
||||
concrete_with_name!(Mat2, "Mat2"),
|
||||
concrete_with_name!(Vec2, "Vec2"),
|
||||
],
|
||||
vec![concrete!(Color)],
|
||||
ShaderIO {
|
||||
inputs: vec![
|
||||
ShaderInput::UniformBuffer((), concrete!(u32)), // width of the output image
|
||||
ShaderInput::StorageBuffer((), concrete!(Color)), // background image
|
||||
ShaderInput::StorageBuffer((), concrete!(Color)), // foreground image
|
||||
ShaderInput::UniformBuffer((), concrete!(u32)), // width of the foreground image
|
||||
ShaderInput::UniformBuffer((), concrete_with_name!(Mat2, "Mat2")), // bg_to_fg.matrix2
|
||||
ShaderInput::UniformBuffer((), concrete_with_name!(Vec2, "Vec2")), // bg_to_fg.translation
|
||||
ShaderInput::OutputBuffer((), concrete!(Color)),
|
||||
],
|
||||
output: ShaderInput::OutputBuffer((), concrete!(Color)),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let len = background.data.len();
|
||||
|
||||
let executor = WgpuExecutor::new()
|
||||
.await
|
||||
.expect("Failed to create wgpu executor. Please make sure that webgpu is enabled for your browser.");
|
||||
log::debug!("creating buffer");
|
||||
let width_uniform = executor.create_uniform_buffer(background.width).unwrap();
|
||||
let bg_storage_buffer = executor
|
||||
.create_storage_buffer(
|
||||
background.data.clone(),
|
||||
StorageBufferOptions {
|
||||
cpu_writable: false,
|
||||
gpu_writable: true,
|
||||
cpu_readable: false,
|
||||
storage: true,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let fg_storage_buffer = executor
|
||||
.create_storage_buffer(
|
||||
foreground.data.clone(),
|
||||
StorageBufferOptions {
|
||||
cpu_writable: false,
|
||||
gpu_writable: true,
|
||||
cpu_readable: false,
|
||||
storage: true,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let fg_width_uniform = executor.create_uniform_buffer(foreground.width).unwrap();
|
||||
let transform_uniform = executor.create_uniform_buffer(transform_matrix).unwrap();
|
||||
let translation_uniform = executor.create_uniform_buffer(translation).unwrap();
|
||||
let width_uniform = Arc::new(width_uniform);
|
||||
let bg_storage_buffer = Arc::new(bg_storage_buffer);
|
||||
let fg_storage_buffer = Arc::new(fg_storage_buffer);
|
||||
let fg_width_uniform = Arc::new(fg_width_uniform);
|
||||
let transform_uniform = Arc::new(transform_uniform);
|
||||
let translation_uniform = Arc::new(translation_uniform);
|
||||
let output_buffer = executor.create_output_buffer(len, concrete!(Color), false).unwrap();
|
||||
let output_buffer = Arc::new(output_buffer);
|
||||
let readback_buffer = executor.create_output_buffer(len, concrete!(Color), true).unwrap();
|
||||
let readback_buffer = Arc::new(readback_buffer);
|
||||
log::debug!("created buffer");
|
||||
let bind_group = Bindgroup {
|
||||
buffers: vec![
|
||||
width_uniform.clone(),
|
||||
bg_storage_buffer.clone(),
|
||||
fg_storage_buffer.clone(),
|
||||
fg_width_uniform.clone(),
|
||||
transform_uniform.clone(),
|
||||
translation_uniform.clone(),
|
||||
],
|
||||
};
|
||||
|
||||
let shader = Shader {
|
||||
source: shader.spirv_binary.into(),
|
||||
name: "gpu::eval",
|
||||
io: shader.io,
|
||||
};
|
||||
log::debug!("loading shader");
|
||||
log::debug!("shader: {:?}", shader.source);
|
||||
let shader = executor.load_shader(shader).unwrap();
|
||||
log::debug!("loaded shader");
|
||||
let pipeline = PipelineLayout {
|
||||
shader: shader.into(),
|
||||
entry_point: "eval".to_string(),
|
||||
bind_group: bind_group.into(),
|
||||
output_buffer: output_buffer.clone(),
|
||||
};
|
||||
log::debug!("created pipeline");
|
||||
let compute_pass = executor
|
||||
.create_compute_pass(&pipeline, Some(readback_buffer.clone()), ComputePassDimensions::XY(background.width, background.height))
|
||||
.unwrap();
|
||||
executor.execute_compute_pipeline(compute_pass).unwrap();
|
||||
log::debug!("executed pipeline");
|
||||
log::debug!("reading buffer");
|
||||
let result = executor.read_output_buffer(readback_buffer).await.unwrap();
|
||||
let colors = bytemuck::pod_collect_to_vec::<u8, Color>(result.as_slice());
|
||||
|
||||
let created_image = Image {
|
||||
data: colors,
|
||||
width: background.width,
|
||||
height: background.height,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
background_instance.instance = created_image;
|
||||
background_instance.source_node_id = None;
|
||||
result_table.push(background_instance);
|
||||
}
|
||||
|
||||
result_table
|
||||
}
|
||||
|
||||
// struct ComputePass {
|
||||
// pipeline_layout: PipelineLayout,
|
||||
// readback_buffer: Option<Arc<WgpuShaderInput>>,
|
||||
// }
|
||||
|
||||
// impl Clone for ComputePass {
|
||||
// fn clone(&self) -> Self {
|
||||
// Self {
|
||||
// pipeline_layout: self.pipeline_layout.clone(),
|
||||
// readback_buffer: self.readback_buffer.clone(),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// pub struct MapGpuNode<Node, EditorApi> {
|
||||
// node: Node,
|
||||
// editor_api: EditorApi,
|
||||
// cache: Mutex<HashMap<String, ComputePass>>,
|
||||
// }
|
||||
|
||||
// #[node_macro::old_node_impl(MapGpuNode)]
|
||||
// async fn map_gpu<'a: 'input>(image: RasterDataTable<Color>, node: DocumentNode, editor_api: &'a graphene_core::application_io::EditorApi<WasmApplicationIo>) -> RasterDataTable<Color> {
|
||||
// let image_frame_table = ℑ
|
||||
// let image = image.instance_ref_iter().next().unwrap().instance;
|
||||
|
||||
// log::debug!("Executing gpu node");
|
||||
// let executor = &editor_api.application_io.as_ref().and_then(|io| io.gpu_executor()).unwrap();
|
||||
|
||||
// #[cfg(feature = "image-compare")]
|
||||
// let img: image::DynamicImage = image::Rgba32FImage::from_raw(image.width, image.height, bytemuck::cast_vec(image.data.clone())).unwrap().into();
|
||||
|
||||
// // TODO: The cache should be based on the network topology not the node name
|
||||
// let compute_pass_descriptor = if self.cache.lock().as_ref().unwrap().contains_key("placeholder") {
|
||||
// self.cache.lock().as_ref().unwrap().get("placeholder").unwrap().clone()
|
||||
// } else {
|
||||
// let name = "placeholder".to_string();
|
||||
// let Ok(compute_pass_descriptor) = create_compute_pass_descriptor(node, image_frame_table, executor).await else {
|
||||
// log::error!("Error creating compute pass descriptor in 'map_gpu()");
|
||||
// return RasterDataTable::default();
|
||||
// };
|
||||
// self.cache.lock().as_mut().unwrap().insert(name, compute_pass_descriptor.clone());
|
||||
// log::error!("created compute pass");
|
||||
// compute_pass_descriptor
|
||||
// };
|
||||
|
||||
// let compute_pass = executor
|
||||
// .create_compute_pass(
|
||||
// &compute_pass_descriptor.pipeline_layout,
|
||||
// compute_pass_descriptor.readback_buffer.clone(),
|
||||
// ComputePassDimensions::XY(image.width / 12 + 1, image.height / 8 + 1),
|
||||
// )
|
||||
// .unwrap();
|
||||
// executor.execute_compute_pipeline(compute_pass).unwrap();
|
||||
// log::debug!("executed pipeline");
|
||||
// log::debug!("reading buffer");
|
||||
// let result = executor.read_output_buffer(compute_pass_descriptor.readback_buffer.clone().unwrap()).await.unwrap();
|
||||
// let colors = bytemuck::pod_collect_to_vec::<u8, Color>(result.as_slice());
|
||||
// log::debug!("first color: {:?}", colors[0]);
|
||||
|
||||
// #[cfg(feature = "image-compare")]
|
||||
// let img2: image::DynamicImage = image::Rgba32FImage::from_raw(image.width, image.height, bytemuck::cast_vec(colors.clone())).unwrap().into();
|
||||
// #[cfg(feature = "image-compare")]
|
||||
// let score = image_compare::rgb_hybrid_compare(&img.into_rgb8(), &img2.into_rgb8()).unwrap();
|
||||
// #[cfg(feature = "image-compare")]
|
||||
// log::debug!("score: {:?}", score.score);
|
||||
|
||||
// let new_image = Image {
|
||||
// data: colors,
|
||||
// width: image.width,
|
||||
// height: image.height,
|
||||
// ..Default::default()
|
||||
// };
|
||||
// let mut result = RasterDataTable::new(new_image);
|
||||
// *result.transform_mut() = image_frame_table.transform();
|
||||
// *result.instance_mut_iter().next().unwrap().alpha_blending = *image_frame_table.instance_ref_iter().next().unwrap().alpha_blending;
|
||||
|
||||
// result
|
||||
// }
|
||||
|
||||
// impl<Node, EditorApi> MapGpuNode<Node, EditorApi> {
|
||||
// pub fn new(node: Node, editor_api: EditorApi) -> Self {
|
||||
// Self {
|
||||
// node,
|
||||
// editor_api,
|
||||
// cache: Mutex::new(HashMap::new()),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// async fn create_compute_pass_descriptor<T: Clone + Pixel + StaticTypeSized>(node: DocumentNode, image: &RasterDataTable<T>, executor: &&WgpuExecutor) -> Result<ComputePass, String>
|
||||
// where
|
||||
// GraphicElement: From<Image<T>>,
|
||||
// T::Static: Pixel,
|
||||
// {
|
||||
// let image = image.instance_ref_iter().next().unwrap().instance;
|
||||
|
||||
// let compiler = graph_craft::graphene_compiler::Compiler {};
|
||||
// let inner_network = NodeNetwork::value_network(node);
|
||||
|
||||
// log::debug!("inner_network: {inner_network:?}");
|
||||
// let network = NodeNetwork {
|
||||
// exports: vec![NodeInput::node(NodeId(2), 0)],
|
||||
// nodes: [
|
||||
// DocumentNode {
|
||||
// inputs: vec![NodeInput::Inline(InlineRust::new("i1[(_global_index.y * i0 + _global_index.x) as usize]".into(), concrete![Color]))],
|
||||
// implementation: DocumentNodeImplementation::ProtoNode("graphene_core::value::CopiedNode".into()),
|
||||
// ..Default::default()
|
||||
// },
|
||||
// DocumentNode {
|
||||
// inputs: vec![NodeInput::network(concrete!(u32), 0)],
|
||||
// implementation: DocumentNodeImplementation::ProtoNode("graphene_core::ops::IdentityNode".into()),
|
||||
// ..Default::default()
|
||||
// },
|
||||
// // DocumentNode {
|
||||
// // name: "Index".into(),
|
||||
// // // inputs: vec![NodeInput::Network(concrete!(UVec3))],
|
||||
// // inputs: vec![NodeInput::Inline(InlineRust::new("i1.x as usize".into(), concrete![u32]))],
|
||||
// // implementation: DocumentNodeImplementation::ProtoNode("graphene_core::value::CopiedNode".into()),
|
||||
// // ..Default::default()
|
||||
// // },
|
||||
// // DocumentNode {
|
||||
// // name: "Get Node".into(),
|
||||
// // inputs: vec![NodeInput::node(NodeId(1), 0), NodeInput::node(NodeId(0), 0)],
|
||||
// // implementation: DocumentNodeImplementation::ProtoNode("graphene_core::storage::GetNode".into()),
|
||||
// // ..Default::default()
|
||||
// // },
|
||||
// DocumentNode {
|
||||
// inputs: vec![NodeInput::node(NodeId(0), 0)],
|
||||
// implementation: DocumentNodeImplementation::Network(inner_network),
|
||||
// ..Default::default()
|
||||
// },
|
||||
// // DocumentNode {
|
||||
// // name: "Save Node".into(),
|
||||
// // inputs: vec![
|
||||
// // NodeInput::node(NodeId(5), 0),
|
||||
// // NodeInput::Inline(InlineRust::new(
|
||||
// // "|x| o0[(_global_index.y * i1 + _global_index.x) as usize] = x".into(),
|
||||
// // // "|x|()".into(),
|
||||
// // Type::Fn(Box::new(concrete!(PackedPixel)), Box::new(concrete!(()))),
|
||||
// // )),
|
||||
// // ],
|
||||
// // implementation: DocumentNodeImplementation::ProtoNode("graphene_core::generic::FnMutNode".into()),
|
||||
// // ..Default::default()
|
||||
// // },
|
||||
// ]
|
||||
// .into_iter()
|
||||
// .enumerate()
|
||||
// .map(|(id, node)| (NodeId(id as u64), node))
|
||||
// .collect(),
|
||||
// ..Default::default()
|
||||
// };
|
||||
// log::debug!("compiling network");
|
||||
// let proto_networks: Result<Vec<_>, _> = compiler.compile(network.clone()).collect();
|
||||
// log::debug!("compiling shader");
|
||||
// let shader = compilation_client::compile(
|
||||
// proto_networks?,
|
||||
// vec![concrete!(u32), concrete!(Color)],
|
||||
// vec![concrete!(Color)],
|
||||
// ShaderIO {
|
||||
// inputs: vec![
|
||||
// ShaderInput::UniformBuffer((), concrete!(u32)),
|
||||
// ShaderInput::StorageBuffer((), concrete!(Color)),
|
||||
// ShaderInput::OutputBuffer((), concrete!(Color)),
|
||||
// ],
|
||||
// output: ShaderInput::OutputBuffer((), concrete!(Color)),
|
||||
// },
|
||||
// )
|
||||
// .await
|
||||
// .unwrap();
|
||||
|
||||
// let len: usize = image.data.len();
|
||||
|
||||
// let storage_buffer = executor
|
||||
// .create_storage_buffer(
|
||||
// image.data.clone(),
|
||||
// StorageBufferOptions {
|
||||
// cpu_writable: false,
|
||||
// gpu_writable: true,
|
||||
// cpu_readable: false,
|
||||
// storage: true,
|
||||
// },
|
||||
// )
|
||||
// .unwrap();
|
||||
|
||||
// // let canvas = editor_api.application_io.create_surface();
|
||||
|
||||
// // let surface = unsafe { executor.create_surface(canvas) }.unwrap();
|
||||
// // let surface_id = surface.surface_id;
|
||||
|
||||
// // let texture = executor.create_texture_buffer(image.clone(), TextureBufferOptions::Texture).unwrap();
|
||||
|
||||
// // // executor.create_render_pass(texture, surface).unwrap();
|
||||
|
||||
// // let frame = SurfaceFrame {
|
||||
// // surface_id,
|
||||
// // transform: image.transform,
|
||||
// // };
|
||||
// // return frame;
|
||||
|
||||
// log::debug!("creating buffer");
|
||||
// let width_uniform = executor.create_uniform_buffer(image.width).unwrap();
|
||||
|
||||
// let storage_buffer = Arc::new(storage_buffer);
|
||||
// let output_buffer = executor.create_output_buffer(len, concrete!(Color), false).unwrap();
|
||||
// let output_buffer = Arc::new(output_buffer);
|
||||
// let readback_buffer = executor.create_output_buffer(len, concrete!(Color), true).unwrap();
|
||||
// let readback_buffer = Arc::new(readback_buffer);
|
||||
// log::debug!("created buffer");
|
||||
// let bind_group = Bindgroup {
|
||||
// buffers: vec![width_uniform.into(), storage_buffer],
|
||||
// };
|
||||
|
||||
// let shader = Shader {
|
||||
// source: shader.spirv_binary.into(),
|
||||
// name: "gpu::eval",
|
||||
// io: shader.io,
|
||||
// };
|
||||
// log::debug!("loading shader");
|
||||
// let shader = executor.load_shader(shader).unwrap();
|
||||
// log::debug!("loaded shader");
|
||||
// let pipeline = PipelineLayout {
|
||||
// shader: shader.into(),
|
||||
// entry_point: "eval".to_string(),
|
||||
// bind_group: bind_group.into(),
|
||||
// output_buffer,
|
||||
// };
|
||||
// log::debug!("created pipeline");
|
||||
|
||||
// Ok(ComputePass {
|
||||
// pipeline_layout: pipeline,
|
||||
// readback_buffer: Some(readback_buffer),
|
||||
// })
|
||||
// }
|
||||
@@ -1,10 +1,10 @@
|
||||
use graphene_core::raster::image::RasterDataTable;
|
||||
use graphene_core::raster_types::{CPU, RasterDataTable};
|
||||
use graphene_core::{Color, Ctx};
|
||||
|
||||
#[node_macro::node(category("Raster"))]
|
||||
async fn image_color_palette(
|
||||
_: impl Ctx,
|
||||
image: RasterDataTable<Color>,
|
||||
image: RasterDataTable<CPU>,
|
||||
#[hard_min(1.)]
|
||||
#[soft_max(28.)]
|
||||
max_size: u32,
|
||||
@@ -64,18 +64,19 @@ async fn image_color_palette(
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use graphene_core::raster::image::{Image, RasterDataTable};
|
||||
use graphene_core::raster::image::Image;
|
||||
use graphene_core::raster_types::{Raster, RasterDataTable};
|
||||
|
||||
#[test]
|
||||
fn test_image_color_palette() {
|
||||
let result = image_color_palette(
|
||||
(),
|
||||
RasterDataTable::new(Image {
|
||||
RasterDataTable::new(Raster::new_cpu(Image {
|
||||
width: 100,
|
||||
height: 100,
|
||||
data: vec![Color::from_rgbaf32(0., 0., 0., 1.).unwrap(); 10000],
|
||||
base64_string: None,
|
||||
}),
|
||||
})),
|
||||
1,
|
||||
);
|
||||
assert_eq!(futures::executor::block_on(result), [Color::from_rgbaf32(0., 0., 0., 1.).unwrap()]);
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
pub mod any;
|
||||
#[cfg(feature = "gpu")]
|
||||
pub mod gpu_nodes;
|
||||
pub mod http;
|
||||
pub mod raster;
|
||||
pub mod text;
|
||||
|
||||
@@ -3,8 +3,10 @@ use fastnoise_lite;
|
||||
use glam::{DAffine2, DVec2, Vec2};
|
||||
use graphene_core::instances::Instance;
|
||||
use graphene_core::raster::bbox::Bbox;
|
||||
use graphene_core::raster::image::{Image, RasterDataTable};
|
||||
use graphene_core::raster::{Alpha, AlphaMut, Bitmap, BitmapMut, CellularDistanceFunction, CellularReturnType, Channel, DomainWarpType, FractalType, LinearChannel, Luminance, NoiseType, RGBMut};
|
||||
use graphene_core::raster::{
|
||||
Alpha, AlphaMut, Bitmap, BitmapMut, CellularDistanceFunction, CellularReturnType, Channel, DomainWarpType, FractalType, Image, LinearChannel, Luminance, NoiseType, RGBMut,
|
||||
};
|
||||
use graphene_core::raster_types::{CPU, Raster, RasterDataTable};
|
||||
use graphene_core::transform::Transform;
|
||||
use graphene_core::{AlphaBlending, Color, Ctx, ExtractFootprint};
|
||||
use rand::prelude::*;
|
||||
@@ -25,7 +27,7 @@ impl From<std::io::Error> for Error {
|
||||
}
|
||||
|
||||
#[node_macro::node(category("Debug: Raster"))]
|
||||
fn sample_image(ctx: impl ExtractFootprint + Clone + Send, image_frame: RasterDataTable<Color>) -> RasterDataTable<Color> {
|
||||
fn sample_image(ctx: impl ExtractFootprint + Clone + Send, image_frame: RasterDataTable<CPU>) -> RasterDataTable<CPU> {
|
||||
let mut result_table = RasterDataTable::default();
|
||||
|
||||
for mut image_frame_instance in image_frame.instance_iter() {
|
||||
@@ -84,7 +86,7 @@ fn sample_image(ctx: impl ExtractFootprint + Clone + Send, image_frame: RasterDa
|
||||
|
||||
image_frame_instance.transform = new_transform;
|
||||
image_frame_instance.source_node_id = None;
|
||||
image_frame_instance.instance = image;
|
||||
image_frame_instance.instance = Raster::new_cpu(image);
|
||||
result_table.push(image_frame_instance)
|
||||
}
|
||||
|
||||
@@ -95,11 +97,11 @@ fn sample_image(ctx: impl ExtractFootprint + Clone + Send, image_frame: RasterDa
|
||||
fn combine_channels(
|
||||
_: impl Ctx,
|
||||
_primary: (),
|
||||
#[expose] red: RasterDataTable<Color>,
|
||||
#[expose] green: RasterDataTable<Color>,
|
||||
#[expose] blue: RasterDataTable<Color>,
|
||||
#[expose] alpha: RasterDataTable<Color>,
|
||||
) -> RasterDataTable<Color> {
|
||||
#[expose] red: RasterDataTable<CPU>,
|
||||
#[expose] green: RasterDataTable<CPU>,
|
||||
#[expose] blue: RasterDataTable<CPU>,
|
||||
#[expose] alpha: RasterDataTable<CPU>,
|
||||
) -> RasterDataTable<CPU> {
|
||||
let mut result_table = RasterDataTable::default();
|
||||
|
||||
let max_len = red.len().max(green.len()).max(blue.len()).max(alpha.len());
|
||||
@@ -170,7 +172,7 @@ fn combine_channels(
|
||||
|
||||
// Add this instance to the result table
|
||||
result_table.push(Instance {
|
||||
instance: image,
|
||||
instance: Raster::new_cpu(image),
|
||||
transform,
|
||||
alpha_blending,
|
||||
source_node_id: None,
|
||||
@@ -184,11 +186,11 @@ fn combine_channels(
|
||||
fn mask(
|
||||
_: impl Ctx,
|
||||
/// The image to be masked.
|
||||
image: RasterDataTable<Color>,
|
||||
image: RasterDataTable<CPU>,
|
||||
/// The stencil to be used for masking.
|
||||
#[expose]
|
||||
stencil: RasterDataTable<Color>,
|
||||
) -> RasterDataTable<Color> {
|
||||
stencil: RasterDataTable<CPU>,
|
||||
) -> RasterDataTable<CPU> {
|
||||
// TODO: Support multiple stencil instances
|
||||
let Some(stencil_instance) = stencil.instance_iter().next() else {
|
||||
// No stencil provided so we return the original image
|
||||
@@ -218,7 +220,7 @@ fn mask(
|
||||
let mask_point = stencil_instance.transform.transform_point2(local_mask_point.clamp(DVec2::ZERO, DVec2::ONE));
|
||||
let mask_point = (DAffine2::from_scale(stencil_size) * stencil_instance.transform.inverse()).transform_point2(mask_point);
|
||||
|
||||
let image_pixel = image_instance.instance.get_pixel_mut(x, y).unwrap();
|
||||
let image_pixel = image_instance.instance.data_mut().get_pixel_mut(x, y).unwrap();
|
||||
let mask_pixel = stencil_instance.instance.sample(mask_point);
|
||||
*image_pixel = image_pixel.multiplied_alpha(mask_pixel.l().cast_linear_channel());
|
||||
}
|
||||
@@ -231,7 +233,7 @@ fn mask(
|
||||
}
|
||||
|
||||
#[node_macro::node(category(""))]
|
||||
fn extend_image_to_bounds(_: impl Ctx, image: RasterDataTable<Color>, bounds: DAffine2) -> RasterDataTable<Color> {
|
||||
fn extend_image_to_bounds(_: impl Ctx, image: RasterDataTable<CPU>, bounds: DAffine2) -> RasterDataTable<CPU> {
|
||||
let mut result_table = RasterDataTable::default();
|
||||
|
||||
for mut image_instance in image.instance_iter() {
|
||||
@@ -242,7 +244,7 @@ fn extend_image_to_bounds(_: impl Ctx, image: RasterDataTable<Color>, bounds: DA
|
||||
continue;
|
||||
}
|
||||
|
||||
let image_data = image_instance.instance.data;
|
||||
let image_data = &image_instance.instance.data;
|
||||
let (image_width, image_height) = (image_instance.instance.width, image_instance.instance.height);
|
||||
if image_width == 0 || image_height == 0 {
|
||||
for image_instance in empty_image((), bounds, Color::TRANSPARENT).instance_iter() {
|
||||
@@ -274,7 +276,7 @@ fn extend_image_to_bounds(_: impl Ctx, image: RasterDataTable<Color>, bounds: DA
|
||||
// let layer_to_new_texture_space = (DAffine2::from_scale(1. / new_scale) * DAffine2::from_translation(new_start) * layer_to_image_space).inverse();
|
||||
let new_texture_to_layer_space = image_instance.transform * DAffine2::from_scale(1. / orig_image_scale) * DAffine2::from_translation(new_start) * DAffine2::from_scale(new_scale);
|
||||
|
||||
image_instance.instance = new_image;
|
||||
image_instance.instance = Raster::new_cpu(new_image);
|
||||
image_instance.transform = new_texture_to_layer_space;
|
||||
image_instance.source_node_id = None;
|
||||
result_table.push(image_instance);
|
||||
@@ -284,13 +286,13 @@ fn extend_image_to_bounds(_: impl Ctx, image: RasterDataTable<Color>, bounds: DA
|
||||
}
|
||||
|
||||
#[node_macro::node(category("Debug: Raster"))]
|
||||
fn empty_image(_: impl Ctx, transform: DAffine2, color: Color) -> RasterDataTable<Color> {
|
||||
fn empty_image(_: impl Ctx, transform: DAffine2, color: Color) -> RasterDataTable<CPU> {
|
||||
let width = transform.transform_vector2(DVec2::new(1., 0.)).length() as u32;
|
||||
let height = transform.transform_vector2(DVec2::new(0., 1.)).length() as u32;
|
||||
|
||||
let image = Image::new(width, height, color);
|
||||
|
||||
let mut result_table = RasterDataTable::new(image);
|
||||
let mut result_table = RasterDataTable::new(Raster::new_cpu(image));
|
||||
let image_instance = result_table.get_mut(0).unwrap();
|
||||
*image_instance.transform = transform;
|
||||
*image_instance.alpha_blending = AlphaBlending::default();
|
||||
@@ -301,7 +303,7 @@ fn empty_image(_: impl Ctx, transform: DAffine2, color: Color) -> RasterDataTabl
|
||||
|
||||
/// Constructs a raster image.
|
||||
#[node_macro::node(category(""))]
|
||||
fn image(_: impl Ctx, _primary: (), image: RasterDataTable<Color>) -> RasterDataTable<Color> {
|
||||
fn image(_: impl Ctx, _primary: (), image: RasterDataTable<CPU>) -> RasterDataTable<CPU> {
|
||||
image
|
||||
}
|
||||
|
||||
@@ -424,7 +426,7 @@ fn noise_pattern(
|
||||
cellular_distance_function: CellularDistanceFunction,
|
||||
cellular_return_type: CellularReturnType,
|
||||
cellular_jitter: f64,
|
||||
) -> RasterDataTable<Color> {
|
||||
) -> RasterDataTable<CPU> {
|
||||
let footprint = ctx.footprint();
|
||||
let viewport_bounds = footprint.viewport_bounds_in_local_space();
|
||||
|
||||
@@ -488,7 +490,7 @@ fn noise_pattern(
|
||||
|
||||
let mut result = RasterDataTable::default();
|
||||
result.push(Instance {
|
||||
instance: image,
|
||||
instance: Raster::new_cpu(image),
|
||||
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
|
||||
..Default::default()
|
||||
});
|
||||
@@ -553,7 +555,7 @@ fn noise_pattern(
|
||||
|
||||
let mut result = RasterDataTable::default();
|
||||
result.push(Instance {
|
||||
instance: image,
|
||||
instance: Raster::new_cpu(image),
|
||||
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
|
||||
..Default::default()
|
||||
});
|
||||
@@ -562,7 +564,7 @@ fn noise_pattern(
|
||||
}
|
||||
|
||||
#[node_macro::node(category("Raster"))]
|
||||
fn mandelbrot(ctx: impl ExtractFootprint + Send) -> RasterDataTable<Color> {
|
||||
fn mandelbrot(ctx: impl ExtractFootprint + Send) -> RasterDataTable<CPU> {
|
||||
let footprint = ctx.footprint();
|
||||
let viewport_bounds = footprint.viewport_bounds_in_local_space();
|
||||
|
||||
@@ -604,7 +606,7 @@ fn mandelbrot(ctx: impl ExtractFootprint + Send) -> RasterDataTable<Color> {
|
||||
};
|
||||
let mut result = RasterDataTable::default();
|
||||
result.push(Instance {
|
||||
instance: image,
|
||||
instance: Raster::new_cpu(image),
|
||||
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use bezier_rs::{ManipulatorGroup, Subpath};
|
||||
use glam::{DAffine2, DVec2};
|
||||
use graphene_core::RasterDataType;
|
||||
use graphene_core::instances::{Instance, InstanceRef};
|
||||
use graphene_core::vector::misc::BooleanOperation;
|
||||
use graphene_core::vector::style::Fill;
|
||||
@@ -203,7 +202,7 @@ fn flatten_vector_data(graphic_group_table: &GraphicGroupTable) -> VectorDataTab
|
||||
result_table.push(sub_vector_data);
|
||||
}
|
||||
}
|
||||
GraphicElement::RasterDataType(image) => {
|
||||
GraphicElement::RasterDataCPU(image) => {
|
||||
let make_instance = |transform| {
|
||||
// Convert the image frame into a rectangular subpath with the image's transform
|
||||
let mut subpath = Subpath::new_rect(DVec2::ZERO, DVec2::ONE);
|
||||
@@ -217,17 +216,26 @@ fn flatten_vector_data(graphic_group_table: &GraphicGroupTable) -> VectorDataTab
|
||||
};
|
||||
|
||||
// Apply the parent group's transform to each element of raster data
|
||||
match image {
|
||||
RasterDataType::RasterData(image) => {
|
||||
for instance in image.instance_ref_iter() {
|
||||
result_table.push(make_instance(*element.transform * *instance.transform));
|
||||
}
|
||||
}
|
||||
RasterDataType::TextureData(image) => {
|
||||
for instance in image.instance_ref_iter() {
|
||||
result_table.push(make_instance(*element.transform * *instance.transform));
|
||||
}
|
||||
}
|
||||
for instance in image.instance_ref_iter() {
|
||||
result_table.push(make_instance(*element.transform * *instance.transform));
|
||||
}
|
||||
}
|
||||
GraphicElement::RasterDataGPU(image) => {
|
||||
let make_instance = |transform| {
|
||||
// Convert the image frame into a rectangular subpath with the image's transform
|
||||
let mut subpath = Subpath::new_rect(DVec2::ZERO, DVec2::ONE);
|
||||
subpath.apply_transform(transform);
|
||||
|
||||
// Create a vector data table row from the rectangular subpath, with a default black fill
|
||||
let mut instance = VectorData::from_subpath(subpath);
|
||||
instance.style.set_fill(Fill::Solid(Color::BLACK));
|
||||
|
||||
Instance { instance, ..Default::default() }
|
||||
};
|
||||
|
||||
// Apply the parent group's transform to each element of raster data
|
||||
for instance in image.instance_ref_iter() {
|
||||
result_table.push(make_instance(*element.transform * *instance.transform));
|
||||
}
|
||||
}
|
||||
GraphicElement::GraphicGroup(mut graphic_group) => {
|
||||
|
||||
@@ -8,7 +8,8 @@ use graphene_core::application_io::{ApplicationIo, ExportFormat, RenderConfig};
|
||||
use graphene_core::instances::Instances;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use graphene_core::raster::bbox::Bbox;
|
||||
use graphene_core::raster::image::{Image, RasterDataTable};
|
||||
use graphene_core::raster::image::Image;
|
||||
use graphene_core::raster_types::{CPU, Raster, RasterDataTable};
|
||||
use graphene_core::renderer::RenderMetadata;
|
||||
use graphene_core::renderer::{GraphicElementRendered, RenderParams, RenderSvgSegmentList, SvgRender, format_transform_matrix};
|
||||
use graphene_core::transform::Footprint;
|
||||
@@ -76,7 +77,7 @@ async fn load_resource<'a: 'n>(_: impl Ctx, _primary: (), #[scope("editor-api")]
|
||||
}
|
||||
|
||||
#[node_macro::node(category("Network"))]
|
||||
fn decode_image(_: impl Ctx, data: Arc<[u8]>) -> RasterDataTable<Color> {
|
||||
fn decode_image(_: impl Ctx, data: Arc<[u8]>) -> RasterDataTable<CPU> {
|
||||
let Some(image) = image::load_from_memory(data.as_ref()).ok() else {
|
||||
return RasterDataTable::default();
|
||||
};
|
||||
@@ -91,7 +92,7 @@ fn decode_image(_: impl Ctx, data: Arc<[u8]>) -> RasterDataTable<Color> {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
RasterDataTable::new(image)
|
||||
RasterDataTable::new(Raster::new_cpu(image))
|
||||
}
|
||||
|
||||
fn render_svg(data: impl GraphicElementRendered, mut render: SvgRender, render_params: RenderParams, footprint: Footprint) -> RenderOutputType {
|
||||
@@ -165,13 +166,13 @@ async fn rasterize<T: WasmNotSend + 'n>(
|
||||
_: impl Ctx,
|
||||
#[implementations(
|
||||
VectorDataTable,
|
||||
RasterDataTable<Color>,
|
||||
RasterDataTable<CPU>,
|
||||
GraphicGroupTable,
|
||||
)]
|
||||
mut data: Instances<T>,
|
||||
footprint: Footprint,
|
||||
surface_handle: Arc<SurfaceHandle<HtmlCanvasElement>>,
|
||||
) -> RasterDataTable<Color>
|
||||
) -> RasterDataTable<CPU>
|
||||
where
|
||||
Instances<T>: GraphicElementRendered,
|
||||
{
|
||||
@@ -219,8 +220,9 @@ where
|
||||
let rasterized = context.get_image_data(0., 0., resolution.x as f64, resolution.y as f64).unwrap();
|
||||
|
||||
let mut result = RasterDataTable::default();
|
||||
let image = Image::from_image_data(&rasterized.data().0, resolution.x as u32, resolution.y as u32);
|
||||
result.push(Instance {
|
||||
instance: Image::from_image_data(&rasterized.data().0, resolution.x as u32, resolution.y as u32),
|
||||
instance: Raster::new_cpu(image),
|
||||
transform: footprint.transform,
|
||||
..Default::default()
|
||||
});
|
||||
@@ -234,7 +236,7 @@ async fn render<'a: 'n, T: 'n + GraphicElementRendered + WasmNotSend>(
|
||||
editor_api: impl Node<Context<'static>, Output = &'a WasmEditorApi>,
|
||||
#[implementations(
|
||||
Context -> VectorDataTable,
|
||||
Context -> RasterDataTable<Color>,
|
||||
Context -> RasterDataTable<CPU>,
|
||||
Context -> GraphicGroupTable,
|
||||
Context -> graphene_core::Artboard,
|
||||
Context -> graphene_core::ArtboardGroupTable,
|
||||
|
||||
Reference in New Issue
Block a user