mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-01 15:53:26 -10:00
Render the Rasterize node with Vello
This commit is contained in:
1 parent
afd88799f3
commit
a7787d2825
5 files changed
+54
-18
No files matched your search
@@ -3556,7 +3556,6 @@ fn migrate_node(node_id: &NodeId, node: &DocumentNode, network_path: &[NodeId],
|
||||
graphene_std::raster_nodes::std_nodes::image::IDENTIFIER,
|
||||
graphene_std::platform_application_io::bytes_to_image::IDENTIFIER,
|
||||
graphene_std::platform_application_io::image_to_bytes::IDENTIFIER,
|
||||
#[cfg(target_family = "wasm")]
|
||||
graphene_std::platform_application_io::rasterize::IDENTIFIER,
|
||||
]
|
||||
};
|
||||
|
||||
@@ -75,12 +75,21 @@ impl WgpuExecutor {
|
||||
/// Renders the document scene `draw` draws, with its content placed by `view`, into a new texture of `size` with straight alpha.
|
||||
/// A render with more content than Vello GPU can hold calls `draw` again for each part it splits into.
|
||||
pub fn render_vello(&self, size: UVec2, view: DAffine2, draw: &mut dyn FnMut(&mut VelloSceneBuilder)) -> Result<Texture> {
|
||||
self.render_vello_with_format(size, view, draw, wgpu::TextureFormat::Rgba8Unorm)
|
||||
}
|
||||
|
||||
/// Renders like [`Self::render_vello`], but into a new [`RASTER_FORMAT`] texture holding linear-light values, as a GPU raster's.
|
||||
pub fn render_vello_raster(&self, size: UVec2, view: DAffine2, draw: &mut dyn FnMut(&mut VelloSceneBuilder)) -> Result<Texture> {
|
||||
self.render_vello_with_format(size, view, draw, RASTER_FORMAT)
|
||||
}
|
||||
|
||||
fn render_vello_with_format(&self, size: UVec2, view: DAffine2, draw: &mut dyn FnMut(&mut VelloSceneBuilder), format: wgpu::TextureFormat) -> Result<Texture> {
|
||||
let limit = self.context().device.limits().max_texture_dimension_2d;
|
||||
if size.x > limit || size.y > limit {
|
||||
anyhow::bail!("Render size {size} exceeds the GPU's texture size limit of {limit}");
|
||||
}
|
||||
|
||||
let texture = self.request_texture(size);
|
||||
let texture = self.request_texture_with_format(size, format);
|
||||
let view = vello_gpu::kurbo::Affine::new(view.to_cols_array());
|
||||
|
||||
let mut canvas = self.inner.document_canvas.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
|
||||
@@ -29,6 +29,16 @@ fn unpremultiply(@builtin(position) position: vec4<f32>) -> @location(0) vec4<f3
|
||||
return vec4<f32>(color.rgb / color.a, color.a);
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn unpremultiply_to_linear(@builtin(position) position: vec4<f32>) -> @location(0) vec4<f32> {
|
||||
let color = textureLoad(source, vec2<i32>(position.xy), 0);
|
||||
if color.a <= 0. {
|
||||
return vec4<f32>(0.);
|
||||
}
|
||||
|
||||
return vec4<f32>(srgb_to_linear(color.rgb / color.a), color.a);
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn premultiply(@builtin(position) position: vec4<f32>) -> @location(0) vec4<f32> {
|
||||
var color = textureLoad(source, vec2<i32>(position.xy), 0);
|
||||
@@ -44,3 +54,9 @@ fn linear_to_srgb(linear: vec3<f32>) -> vec3<f32> {
|
||||
let high = 1.055 * pow(linear, vec3<f32>(1. / 2.4)) - 0.055;
|
||||
return select(high, low, linear <= vec3<f32>(0.0031308));
|
||||
}
|
||||
|
||||
fn srgb_to_linear(srgb: vec3<f32>) -> vec3<f32> {
|
||||
let low = srgb / 12.92;
|
||||
let high = pow((srgb + 0.055) / 1.055, vec3<f32>(2.4));
|
||||
return select(high, low, srgb <= vec3<f32>(0.04045));
|
||||
}
|
||||
@@ -63,7 +63,7 @@ impl VelloRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders the scene `draw` draws on `canvas`, with its content placed by `view`, into `target` (an `Rgba8Unorm` texture of `size` with straight alpha).
|
||||
/// Renders the scene `draw` draws on `canvas`, with its content placed by `view`, into `target`, a texture of `size` with straight alpha that is either `Rgba8Unorm` holding sRGB values or [`crate::RASTER_FORMAT`] holding linear-light values.
|
||||
/// A render with more content than Vello GPU can hold calls `draw` again for each part it splits into.
|
||||
#[expect(clippy::too_many_arguments)]
|
||||
pub(crate) fn render(
|
||||
@@ -135,10 +135,12 @@ impl VelloRenderer {
|
||||
[(UVec2::ZERO, UVec2::new(size.x, top)), (UVec2::new(0, top), UVec2::new(size.x, size.y - top))]
|
||||
};
|
||||
let halves = halves.map(|(offset, half_size)| {
|
||||
let half_target = create_texture(
|
||||
// In the target's own format, so the half can be copied into it
|
||||
let half_target = create_texture_with_format(
|
||||
device,
|
||||
"vello_gpu_split_target",
|
||||
half_size,
|
||||
target.format(),
|
||||
wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST,
|
||||
);
|
||||
let half_view = Affine::translate((-(offset.x as f64), -(offset.y as f64))) * view;
|
||||
@@ -267,7 +269,7 @@ impl VelloRenderer {
|
||||
|
||||
if rendered.is_ok() {
|
||||
let target_view = target.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
self.alpha_conversion.unpremultiply(device, &mut encoder, &premultiplied_view, &target_view);
|
||||
self.alpha_conversion.unpremultiply(device, &mut encoder, &premultiplied_view, &target_view, target.format());
|
||||
}
|
||||
|
||||
// Submitted even after a failure, since it also prepares the textures of the scene's images
|
||||
@@ -483,6 +485,10 @@ fn clear_texture(encoder: &mut wgpu::CommandEncoder, view: &wgpu::TextureView) {
|
||||
}
|
||||
|
||||
fn create_texture(device: &wgpu::Device, label: &str, size: UVec2, usage: wgpu::TextureUsages) -> wgpu::Texture {
|
||||
create_texture_with_format(device, label, size, TEXTURE_FORMAT, usage)
|
||||
}
|
||||
|
||||
fn create_texture_with_format(device: &wgpu::Device, label: &str, size: UVec2, format: wgpu::TextureFormat, usage: wgpu::TextureUsages) -> wgpu::Texture {
|
||||
device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some(label),
|
||||
size: wgpu::Extent3d {
|
||||
@@ -493,7 +499,7 @@ fn create_texture(device: &wgpu::Device, label: &str, size: UVec2, usage: wgpu::
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: TEXTURE_FORMAT,
|
||||
format,
|
||||
usage,
|
||||
view_formats: &[],
|
||||
})
|
||||
@@ -503,6 +509,8 @@ fn create_texture(device: &wgpu::Device, label: &str, size: UVec2, usage: wgpu::
|
||||
struct AlphaConversion {
|
||||
bind_group_layout: wgpu::BindGroupLayout,
|
||||
unpremultiply_pipeline: wgpu::RenderPipeline,
|
||||
/// Unpremultiplies into a linear-light [`crate::RASTER_FORMAT`] target.
|
||||
unpremultiply_to_linear_pipeline: wgpu::RenderPipeline,
|
||||
premultiply_pipeline: wgpu::RenderPipeline,
|
||||
/// The parameters for a source whose format stores linear values.
|
||||
linear_source_parameters: wgpu::Buffer,
|
||||
@@ -547,7 +555,7 @@ impl AlphaConversion {
|
||||
immediate_size: 0,
|
||||
});
|
||||
|
||||
let create_pipeline = |label: &str, entry_point: &str| {
|
||||
let create_pipeline = |label: &str, entry_point: &str, format: wgpu::TextureFormat| {
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some(label),
|
||||
layout: Some(&pipeline_layout),
|
||||
@@ -562,7 +570,7 @@ impl AlphaConversion {
|
||||
entry_point: Some(entry_point),
|
||||
compilation_options: Default::default(),
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: TEXTURE_FORMAT,
|
||||
format,
|
||||
blend: None,
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
@@ -587,16 +595,22 @@ impl AlphaConversion {
|
||||
};
|
||||
|
||||
Self {
|
||||
unpremultiply_pipeline: create_pipeline("vello_unpremultiply_pipeline", "unpremultiply"),
|
||||
premultiply_pipeline: create_pipeline("vello_premultiply_pipeline", "premultiply"),
|
||||
unpremultiply_pipeline: create_pipeline("vello_unpremultiply_pipeline", "unpremultiply", TEXTURE_FORMAT),
|
||||
unpremultiply_to_linear_pipeline: create_pipeline("vello_unpremultiply_to_linear_pipeline", "unpremultiply_to_linear", crate::RASTER_FORMAT),
|
||||
premultiply_pipeline: create_pipeline("vello_premultiply_pipeline", "premultiply", TEXTURE_FORMAT),
|
||||
linear_source_parameters: create_parameters("vello_alpha_linear_source", 0),
|
||||
srgb_source_parameters: create_parameters("vello_alpha_srgb_source", 1),
|
||||
bind_group_layout,
|
||||
}
|
||||
}
|
||||
|
||||
fn unpremultiply(&self, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, source: &wgpu::TextureView, target: &wgpu::TextureView) {
|
||||
self.run(device, encoder, &self.unpremultiply_pipeline, &self.linear_source_parameters, source, target);
|
||||
fn unpremultiply(&self, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, source: &wgpu::TextureView, target: &wgpu::TextureView, target_format: wgpu::TextureFormat) {
|
||||
let pipeline = if target_format == crate::RASTER_FORMAT {
|
||||
&self.unpremultiply_to_linear_pipeline
|
||||
} else {
|
||||
&self.unpremultiply_pipeline
|
||||
};
|
||||
self.run(device, encoder, pipeline, &self.linear_source_parameters, source, target);
|
||||
}
|
||||
|
||||
fn premultiply(&self, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, source: &wgpu::TextureView, source_is_srgb: bool, target: &wgpu::TextureView) {
|
||||
|
||||
@@ -248,24 +248,22 @@ async fn rasterize_with_vello(data: List<Graphic>, footprint: Footprint, executo
|
||||
let aabb = Bbox::from_transform(footprint.transform).to_axis_aligned_bbox();
|
||||
let view = DAffine2::from_scale(resolution.as_dvec2() / aabb.size()) * DAffine2::from_translation(-aabb.start);
|
||||
let render_params = RenderParams {
|
||||
footprint,
|
||||
footprint: Footprint { transform: view, ..footprint },
|
||||
render_output_type: rendering::RenderOutputType::Vello,
|
||||
for_export: true,
|
||||
..Default::default()
|
||||
};
|
||||
let texture = match executor.render_vello(resolution, view, &mut |scene| data.render_to_vello(scene, DAffine2::IDENTITY, &render_params)) {
|
||||
let texture = match executor.render_vello_raster(resolution, view, &mut |scene| data.render_to_vello(scene, DAffine2::IDENTITY, &render_params)) {
|
||||
Ok(texture) => texture,
|
||||
Err(error) => {
|
||||
log::error!("Failed to rasterize: {error}");
|
||||
return List::new();
|
||||
}
|
||||
};
|
||||
// Downloaded and uploaded again since Vello renders the texture in an 8-bit format rather than the format of GPU rasters
|
||||
let raster = ConvertAsync::<Raster<CPU>, _>::convert(Raster::new_gpu(texture), Footprint::DEFAULT, executor.clone()).await;
|
||||
let raster = raster.convert(Footprint::DEFAULT, executor);
|
||||
|
||||
// The input's layers stay reachable so the renderer can expose their click targets to the editor's tools
|
||||
List::new_from_item(
|
||||
Item::new_from_element(raster)
|
||||
Item::new_from_element(Raster::new_gpu(texture))
|
||||
.with_attribute(ATTR_TRANSFORM, footprint.transform)
|
||||
.with_attribute(ATTR_EDITOR_MERGED_LAYERS, data),
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user