Files
Graphite/node-graph/libraries/wgpu-executor/src/vello_render.rs
T
Keavon Chambers 9517b006fa New node: 'Background Blur' (#5063)
Add a Background Blur effect that blurs what lies beneath the content
2026-12-03 02:35:26 -08:00

1130 lines
45 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use crate::vello_adjustments::adjustment_run_hash;
use crate::vello_effects::{EffectPasses, EffectTextures};
use crate::vello_generators::GeneratorPasses;
use core_types::blending::BlendMode;
use glam::{DAffine2, UVec2};
use graphic_types::effect_layers::EffectLayers;
use graphic_types::effects::EffectCapture;
use graphic_types::raster_types::{CPU, Image, Raster, WeakCPU};
use graphic_types::{Adjustment, Effects};
use rendering::vello_scene_builder::{EffectsRenderer, GeneratorTile, SceneImage, VelloSceneBuilder, image_downscale_factor, image_texture_size};
use std::collections::HashMap;
use std::sync::{Mutex, PoisonError};
use vello_gpu::kurbo::Affine;
use vello_gpu::peniko;
use vello_gpu::{
ClearSettings, IntermediateTextureError, LayersConfig, MemorySettings, RenderError, RenderSettings, RenderSize, RenderTargetConfig, Renderer, Resources, SizeU16, TargetInit, TextureBindings,
TextureId,
};
use wgpu_sync::Queue;
/// The format of the textures Vello GPU renders the canvas into, of the image textures it samples, and of the textures handed back.
pub(crate) const TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;
/// Formats for content processed further, by effects or by blending from a layer, of which the first the GPU supports is used so steep changes don't bring out 8-bit banding.
/// 16-bit unorm comes first for holding every 8-bit level exactly, unlike half floats.
const PRECISE_FORMATS: &[wgpu::TextureFormat] = &[wgpu::TextureFormat::Rgba16Unorm, wgpu::TextureFormat::Rgba16Float];
/// How many renders a GPU texture's premultiplied copy survives without being drawn before it is freed.
const UNUSED_IMAGE_RENDER_LIMIT: u64 = 60;
/// About the most tiles of generators kept between renders, past which the least recently drawn are freed. Tiles drawn in the same render are kept or freed together.
const MAX_CACHED_GENERATOR_TILES: usize = 256;
/// How many times a render with too much content for Vello GPU is halved, reaching areas 1/4096 the size of the original.
const MAX_SPLIT_DEPTH: u32 = 12;
/// The widest or tallest area a single render can draw an isolated layer across, since that is the limit of the intermediate textures Vello GPU draws layers into.
/// These grow to fit the largest layer and stay that size, so a larger limit keeps more memory after one large render. Renders larger than this are split.
pub const MAX_LAYER_RENDER_SIZE: u32 = 4096;
/// Vello GPU's renderer and GPU resources, shared by every [`VelloCanvas`] so their memory and pipelines exist once.
pub(crate) struct VelloRenderer {
/// Locked only while a scene is rendered and submitted, so one canvas drawing its scene on the CPU doesn't hold up another's render.
gpu: Mutex<(Renderer, Resources)>,
alpha_conversion: AlphaConversion,
effect_passes: EffectPasses,
generator_passes: GeneratorPasses,
/// The format of content rendered for its effects and of the layers Vello GPU draws in, one of [`PRECISE_FORMATS`] or else [`TEXTURE_FORMAT`].
precise_format: wgpu::TextureFormat,
}
/// A stream of frames rendered through the shared Vello renderer, such as the document or the overlays, keeping what its frames reuse.
#[derive(Default)]
pub struct VelloCanvas {
/// Scenes to draw into, one for each depth of effects groups nested in a render, reused between renders to keep their allocations.
spare_scenes: Vec<vello_gpu::Scene>,
/// The premultiplied texture Vello GPU renders into before conversion to straight alpha, kept while the render size stays the same.
premultiplied_target: Option<wgpu::Texture>,
images: ImageTextures,
effect_textures: EffectTextures,
}
/// Whether a render leaves its target with the straight alpha the rest of Graphite expects, or the premultiplied alpha the effect passes take.
#[derive(Clone, Copy, PartialEq, Eq)]
enum TargetAlpha {
Straight,
Premultiplied,
}
/// A render in progress: the shared renderer and GPU, and the canvas whose resources it draws with.
struct RenderContext<'a> {
renderer: &'a VelloRenderer,
canvas: &'a mut VelloCanvas,
device: &'a wgpu::Device,
queue: &'a Queue,
}
impl VelloRenderer {
pub(crate) fn new(device: &wgpu::Device, adapter: &wgpu::Adapter) -> Self {
let render_target_config = RenderTargetConfig {
format: TEXTURE_FORMAT,
width: 1,
height: 1,
};
// Rendering into, blending, sampling with filtering, and copying are what the format needs, which the device allows beyond
// the guarantees for the format only with adapter-specific format features enabled
let device_features = device.features();
let usages = wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST;
let precise_format = (PRECISE_FORMATS.iter().copied())
.find(|format| {
let format_features = match device_features.contains(wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES) {
true => adapter.get_texture_format_features(*format),
false => format.guaranteed_format_features(device_features),
};
device_features.contains(format.required_features())
&& format_features.allowed_usages.contains(usages)
&& format_features.flags.contains(wgpu::TextureFormatFeatureFlags::BLENDABLE | wgpu::TextureFormatFeatureFlags::FILTERABLE)
})
.unwrap_or(TEXTURE_FORMAT);
let settings = RenderSettings::default()
.with_memory_settings(MemorySettings {
layers_config: LayersConfig {
max_texture_size: SizeU16::new(MAX_LAYER_RENDER_SIZE as u16),
..Default::default()
},
..Default::default()
})
.with_additional_target_formats([precise_format])
.with_layer_format(precise_format);
Self {
gpu: Mutex::new(Renderer::new_with(device, &render_target_config, settings)),
alpha_conversion: AlphaConversion::new(device),
effect_passes: EffectPasses::new(device, adapter),
generator_passes: GeneratorPasses::default(),
precise_format,
}
}
/// Renders the scene `draw` draws on `canvas`, with its content placed by `view`, into `target` (an `Rgba8Unorm` 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.
#[expect(clippy::too_many_arguments)]
pub(crate) fn render(
&self,
canvas: &mut VelloCanvas,
device: &wgpu::Device,
queue: &Queue,
draw: &mut dyn FnMut(&mut VelloSceneBuilder),
view: Affine,
size: UVec2,
target: &wgpu::Texture,
) -> anyhow::Result<()> {
if u16::try_from(size.x).is_err() || u16::try_from(size.y).is_err() {
anyhow::bail!("Render size {size} exceeds Vello GPU's limit");
}
canvas.images.render_count += 1;
let mut context = RenderContext {
renderer: self,
canvas,
device,
queue,
};
context.render_area(draw, view, size, target, TargetAlpha::Straight);
canvas.images.free_unused();
canvas.effect_textures.finish_render();
Ok(())
}
/// Renders a `scene` drawn beforehand on `canvas` into `target` (an `Rgba8Unorm` texture of the scene's size with straight alpha).
/// Unlike [`Self::render`], it can't split the scene, so one with more content than Vello GPU can hold renders transparent.
pub(crate) fn render_scene(&self, canvas: &mut VelloCanvas, device: &wgpu::Device, queue: &Queue, scene: VelloSceneBuilder, target: &wgpu::Texture) {
canvas.images.render_count += 1;
let max_texture_size = scene.max_texture_size();
let mut context = RenderContext {
renderer: self,
canvas,
device,
queue,
};
let (scene, images) = scene.finish();
if let Err(error) = context.submit(&scene, images, max_texture_size, target, TargetAlpha::Straight, true) {
log::error!("Vello GPU failed to render: {error}");
clear(device, queue, target);
}
canvas.images.free_unused();
canvas.effect_textures.finish_render();
}
}
impl RenderContext<'_> {
/// Renders the scene `draw` draws into `target`, a texture of `size`, splitting its area into halves while it holds more content than one render can.
fn render_area(&mut self, draw: &mut dyn FnMut(&mut VelloSceneBuilder), view: Affine, size: UVec2, target: &wgpu::Texture, target_alpha: TargetAlpha) {
if let Err(error) = self.render_once(draw, view, size, target, target_alpha) {
self.render_halves(draw, view, size, target, target_alpha, error, 1);
}
}
/// Renders each half of the area of `target`, a texture of `size`, after rendering it whole failed with `error`, splitting again each half that still holds too much.
#[expect(clippy::too_many_arguments)]
fn render_halves(&mut self, draw: &mut dyn FnMut(&mut VelloSceneBuilder), view: Affine, size: UVec2, target: &wgpu::Texture, target_alpha: TargetAlpha, error: RenderError, split_depth: u32) {
let (device, queue) = (self.device, self.queue);
// Each half covers less of the scene, so it has less content and smaller layers to hold
let too_much_to_hold = matches!(
error,
RenderError::ResourceTextureOverflow { .. } | RenderError::IntermediateTexture(IntermediateTextureError::TooLarge { .. })
);
if !too_much_to_hold || split_depth > MAX_SPLIT_DEPTH || size.max_element() <= 1 {
// A failed render leaves its area transparent rather than taking down the editor
log::error!("Vello GPU failed to render: {error}");
clear(device, queue, target);
return;
}
let halves = if size.x >= size.y {
let left = size.x / 2;
[(UVec2::ZERO, UVec2::new(left, size.y)), (UVec2::new(left, 0), UVec2::new(size.x - left, size.y))]
} else {
let top = size.y / 2;
[(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(
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;
let rendered = self.render_once(draw, half_view, half_size, &half_target, target_alpha);
(offset, half_size, half_view, half_target, rendered)
});
// Content that each half still holds all of, like paints stacked across the whole area, can't be split apart
let stalled = |rendered: &Result<(), RenderError>| match (rendered, &error) {
(
Err(RenderError::ResourceTextureOverflow { resource, required_height, .. }),
RenderError::ResourceTextureOverflow {
resource: whole_resource,
required_height: whole_required_height,
..
},
) => resource == whole_resource && required_height >= whole_required_height,
_ => false,
};
if halves.iter().all(|(.., rendered)| stalled(rendered)) {
log::error!("Vello GPU failed to render: {error}");
clear(device, queue, target);
return;
}
for (offset, half_size, half_view, half_target, rendered) in halves {
if let Err(half_error) = rendered {
self.render_halves(draw, half_view, half_size, &half_target, target_alpha, half_error, split_depth + 1);
}
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("vello_gpu_split_copy") });
encoder.copy_texture_to_texture(
half_target.as_image_copy(),
wgpu::TexelCopyTextureInfo {
origin: wgpu::Origin3d { x: offset.x, y: offset.y, z: 0 },
..target.as_image_copy()
},
half_target.size(),
);
queue.submit([encoder.finish()]);
}
}
/// Renders the scene `draw` draws into `target`, a texture of `size`, in a single Vello GPU render.
fn render_once(&mut self, draw: &mut dyn FnMut(&mut VelloSceneBuilder), view: Affine, size: UVec2, target: &wgpu::Texture, target_alpha: TargetAlpha) -> Result<(), RenderError> {
let max_texture_size = self.device.limits().max_texture_dimension_2d;
let max_size = max_texture_size.min(u16::MAX as u32);
let (mut scene, mut images, apron) = self.draw_scene(draw, view, size, max_texture_size, largest_apron(size, max_size));
// Content reading what lies beneath it past the scene's edges stops its drawing for one grown by as much, of which the target takes the middle
if let Some(apron) = apron
&& let Some(grown_size) = grown_render_size(size, apron, max_size)
{
self.discard_scene(scene, images);
let grown_view = Affine::translate((apron as f64, apron as f64)) * view;
let (scene, images, _) = self.draw_scene(draw, grown_view, grown_size, max_texture_size, 0);
let grown_target = create_texture(
self.device,
"vello_gpu_grown_target",
grown_size,
target.format(),
wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_SRC,
);
let rendered = self.submit(&scene, images, max_texture_size, &grown_target, target_alpha, true);
self.canvas.spare_scenes.push(scene);
let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("vello_gpu_grown_copy") });
encoder.copy_texture_to_texture(
wgpu::TexelCopyTextureInfo {
origin: wgpu::Origin3d { x: apron, y: apron, z: 0 },
..grown_target.as_image_copy()
},
target.as_image_copy(),
wgpu::Extent3d {
width: size.x,
height: size.y,
depth_or_array_layers: 1,
},
);
self.queue.submit([encoder.finish()]);
return rendered;
}
// Later content reading farther past the edges than a grown render holds leaves the scene to draw again, reading only what it holds
if apron.is_some() {
self.discard_scene(scene, images);
(scene, images, _) = self.draw_scene(draw, view, size, max_texture_size, 0);
}
let rendered = self.submit(&scene, images, max_texture_size, target, target_alpha, true);
// Once the render that draws it is submitted, the scene is free to reuse
self.canvas.spare_scenes.push(scene);
rendered
}
/// Draws the scene `draw` draws for a render of `size` placed by `view`, which renders its effects groups as they're drawn.
/// It stops at the first content reading what lies beneath it past its edges by at most `largest_apron` pixels, then returns how far past them all of it reads.
fn draw_scene(&mut self, draw: &mut dyn FnMut(&mut VelloSceneBuilder), view: Affine, size: UVec2, max_texture_size: u32, largest_apron: u32) -> (vello_gpu::Scene, Vec<SceneImage>, Option<u32>) {
let (width, height) = (size.x as u16, size.y as u16);
let mut scene = self.canvas.spare_scenes.pop().unwrap_or_else(|| vello_gpu::Scene::new(width, height));
scene.reset_and_resize(width, height);
let mut builder = VelloSceneBuilder::from_vello(scene, view, max_texture_size)
.with_effects_renderer(self)
.stopping_for_an_apron_up_to(largest_apron);
draw(&mut builder);
let apron = (!builder.drew_everything()).then(|| builder.backdrop_apron());
let (scene, images) = builder.finish();
(scene, images, apron)
}
/// Sets aside a drawn scene without rendering it, freeing the effects results it draws for reuse.
fn discard_scene(&mut self, scene: vello_gpu::Scene, images: Vec<SceneImage>) {
for image in images {
if let SceneImage::Rendered(texture) = image {
self.canvas.effect_textures.release(texture);
}
}
self.canvas.spare_scenes.push(scene);
}
/// Renders a drawn scene, with the images it draws, into `target`, a texture of the scene's size, in a single Vello GPU render.
/// The effects results it draws are freed for reuse after it, even if it fails when `release_failed`.
fn submit(&mut self, scene: &vello_gpu::Scene, images: Vec<SceneImage>, max_texture_size: u32, target: &wgpu::Texture, target_alpha: TargetAlpha, release_failed: bool) -> Result<(), RenderError> {
let device = self.device;
let (width, height) = (scene.width(), scene.height());
let size = UVec2::new(width as u32, height as u32);
// Locked from here to the submission, so drawing a scene doesn't hold up other uses of the queue
let queue = self.queue.lock();
// Each render submits its own encoder, since Vello GPU stages its uploads for the next submission
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("vello_gpu_render") });
// Supply the textures of the images the scene draws, under the IDs it drew them with
let mut texture_bindings = TextureBindings::new();
for (id, image) in images.iter().enumerate() {
let view = self
.canvas
.images
.prepare(device, &queue, &mut encoder, &mut self.canvas.effect_textures, self.renderer, image, max_texture_size);
// Adjustments draw a copy of the texture they're carried out on
let view = match image {
SceneImage::Raster { raster, adjustments } if !adjustments.is_empty() => view.and_then(|_| {
let effects = (&self.renderer.effect_passes, &mut self.canvas.effect_textures);
self.canvas
.images
.prepare_adjusted(device, &queue, effects, raster, adjustments, max_texture_size, self.renderer.precise_format)
}),
_ => view,
};
if let Some(view) = view {
texture_bindings.insert(TextureId(id as u64), view);
}
}
// Vello GPU renders premultiplied, which is converted to straight alpha unless the target wants it premultiplied
let target_view = target.create_view(&wgpu::TextureViewDescriptor::default());
let premultiplied_view = match (target_alpha, &self.canvas.premultiplied_target) {
(TargetAlpha::Premultiplied, _) => target_view.clone(),
(TargetAlpha::Straight, Some(texture)) if texture.width() == size.x && texture.height() == size.y => texture.create_view(&wgpu::TextureViewDescriptor::default()),
(TargetAlpha::Straight, _) => {
let texture = create_texture(
device,
"vello_gpu_premultiplied_target",
size,
TEXTURE_FORMAT,
wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
);
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
self.canvas.premultiplied_target = Some(texture);
view
}
};
let target_init = TargetInit::Clear(ClearSettings::Viewport { color: peniko::Color::TRANSPARENT });
// Vello GPU stages this render's uploads into resources every render shares, so another can't stage its own until this one is submitted
let mut gpu = self.renderer.gpu.lock().unwrap_or_else(PoisonError::into_inner);
let (renderer, resources) = &mut *gpu;
let rendered = renderer.render(
scene,
resources,
device,
&queue,
&mut encoder,
&RenderSize { width, height },
&premultiplied_view,
None,
&texture_bindings,
target_init,
);
if rendered.is_ok() && target_alpha == TargetAlpha::Straight {
self.renderer.alpha_conversion.unpremultiply(device, &mut encoder, &premultiplied_view, &target_view);
}
// Submitted even after a failure, since it also prepares the textures of the scene's images
queue.submit([encoder.finish()]);
drop((gpu, queue));
// Once the render that draws them is submitted, its effects results are free to reuse, unless a failed render leaves them still to draw
if rendered.is_ok() || release_failed {
for image in images {
if let SceneImage::Rendered(texture) = image {
self.canvas.effect_textures.release(texture);
}
}
}
rendered
}
}
impl EffectsRenderer for RenderContext<'_> {
fn renders_effects(&self) -> bool {
self.renderer.effect_passes.renders_working_formats
}
fn render_effects(
&mut self,
size: UVec2,
view: Affine,
effects: &Effects,
layers: &EffectLayers,
fill: f32,
opacity: f32,
effect_to_pixels: DAffine2,
frame: Option<graphic_types::resampling::EffectFrame>,
backdrop: Option<(&wgpu::Texture, Affine)>,
capture: Option<EffectCapture>,
draw_paint: &mut dyn FnMut(usize, &mut VelloSceneBuilder),
draw: &mut dyn FnMut(&mut VelloSceneBuilder),
) -> Vec<(wgpu::Texture, BlendMode)> {
let content_texture = self.canvas.effect_textures.acquire(self.device, size, self.renderer.precise_format);
self.render_area(draw, view, size, &content_texture, TargetAlpha::Premultiplied);
// The effects' drawn paints, each rendered over the same pixels as the content
let paint_count = effects.operations().map(|operation| operation.drawn_paints().count()).sum();
let paint_textures: Vec<wgpu::Texture> = (0..paint_count)
.map(|index| {
let paint_texture = self.canvas.effect_textures.acquire(self.device, size, self.renderer.precise_format);
self.render_area(&mut |scene| draw_paint(index, scene), view, size, &paint_texture, TargetAlpha::Premultiplied);
paint_texture
})
.collect();
let effect_textures = &mut self.canvas.effect_textures;
let queue = self.queue.lock();
let effect_passes = &self.renderer.effect_passes;
let results = effect_passes.apply(
self.device,
&queue,
effect_textures,
&content_texture,
effects,
layers,
fill,
opacity,
effect_to_pixels,
frame,
&paint_textures,
backdrop,
capture,
);
effect_textures.release(content_texture);
paint_textures.into_iter().for_each(|paint_texture| effect_textures.release(paint_texture));
results
}
fn render_backdrop(&mut self, scene: &vello_gpu::Scene, images: &[SceneImage], max_texture_size: u32) -> Option<wgpu::Texture> {
let size = UVec2::new(scene.width() as u32, scene.height() as u32);
let backdrop = self.canvas.effect_textures.acquire(self.device, size, self.renderer.precise_format);
// A failed render leaves the scene to go on drawing what it drew, so the results it draws stay in use
match self.submit(scene, images.to_vec(), max_texture_size, &backdrop, TargetAlpha::Premultiplied, false) {
Ok(()) => Some(backdrop),
Err(error) => {
log::error!("Vello GPU failed to render what lies beneath content reading it: {error}");
self.canvas.effect_textures.release(backdrop);
None
}
}
}
}
/// The widest apron of pixels past each edge that a render of `size` can grow by and still fit a texture of `max_size`.
fn largest_apron(size: UVec2, max_size: u32) -> u32 {
max_size.saturating_sub(size.max_element()) / 2
}
/// The size of a render of `size` grown by an `apron` of pixels past each edge, or `None` without one or if it's too large for a texture of `max_size`.
fn grown_render_size(size: UVec2, apron: u32, max_size: u32) -> Option<UVec2> {
(apron > 0 && apron <= largest_apron(size, max_size)).then(|| size + UVec2::splat(2 * apron))
}
/// Leaves `target` transparent.
fn clear(device: &wgpu::Device, queue: &Queue, target: &wgpu::Texture) {
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("vello_gpu_clear") });
clear_texture(&mut encoder, &target.create_view(&wgpu::TextureViewDescriptor::default()));
queue.submit([encoder.finish()]);
}
/// Identifies the pixels behind an image texture: a blob of CPU pixels, a GPU texture, or a tile of a generator's pattern.
#[derive(Clone, PartialEq, Eq, Hash)]
enum ImageKey {
Blob(u64),
Raster(u64),
Texture(wgpu::Texture),
GeneratorTile(GeneratorTile),
}
/// A premultiplied copy of an image's pixels that Vello GPU samples as an external texture.
struct ImageTexture {
texture: wgpu::Texture,
last_used: u64,
/// The CPU raster pixels copied into the texture, which is freed once they're gone since they can't be drawn again.
raster: Option<WeakCPU>,
/// Copies with runs of adjustments carried out on them, one for each run the image is drawn with.
adjusted: Vec<AdjustedTexture>,
/// The raster's straight linear colors in 32-bit floats that adjustments are carried out from, kept while any adjusted copy is.
adjustment_source: Option<wgpu::Texture>,
}
/// A copy of an image's texture with a run of adjustments carried out on it, made over in place for another run.
struct AdjustedTexture {
texture: wgpu::Texture,
/// The hash of the run carried out on it, or `None` until one is.
run_hash: Option<u64>,
last_used: u64,
}
/// The textures of the images recent renders drew, each freed once it goes unused for long enough.
#[derive(Default)]
struct ImageTextures {
textures: HashMap<ImageKey, ImageTexture>,
/// The textures of the generator tiles the last render freed, for its next render's new tiles to take over, as when changing a generator's settings replaces every tile.
spare_generator_tiles: Vec<wgpu::Texture>,
render_count: u64,
}
impl ImageTextures {
/// A view of the premultiplied texture for `image`, uploading CPU pixels or rendering a generator's tile the first time they're drawn.
/// GPU textures are converted again at every render since the texture cache hands their textures out again with new contents.
#[expect(clippy::too_many_arguments)]
fn prepare(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
effect_textures: &mut EffectTextures,
renderer: &VelloRenderer,
image: &SceneImage,
max_texture_size: u32,
) -> Option<wgpu::TextureView> {
let key = match image {
SceneImage::Pixels(data) => ImageKey::Blob(data.data.id()),
SceneImage::Raster { raster, .. } => ImageKey::Raster(raster.buffer_id()),
SceneImage::Texture(texture) => ImageKey::Texture((**texture).clone()),
SceneImage::GeneratorTile { tile, .. } => ImageKey::GeneratorTile(*tile),
// Effects results are premultiplied already
SceneImage::Rendered(texture) => return Some(texture.create_view(&wgpu::TextureViewDescriptor::default())),
};
if !self.textures.contains_key(&key) {
let texture = match image {
SceneImage::Pixels(data) => {
if data.format != peniko::ImageFormat::Rgba8 {
log::error!("Unsupported image format {:?}", data.format);
return None;
}
upload_pixels(device, queue, data.data.data(), data.width, data.height, data.alpha_type, max_texture_size)?
}
SceneImage::Raster { raster, .. } => {
let (pixels, width, height) = raster.to_flat_u8();
upload_pixels(device, queue, &pixels, width, height, peniko::ImageAlphaType::Alpha, max_texture_size)?
}
SceneImage::GeneratorTile { pattern, tile } => {
let texture = (self.spare_generator_tiles.pop()).unwrap_or_else(|| GeneratorPasses::tile_texture(device, GeneratorTile::SAMPLES));
let (corner, texel) = tile.corner_and_texel();
renderer.generator_passes.render_tile(device, queue, encoder, &texture, pattern, corner, texel);
renderer.effect_passes.record_mipmaps(device, encoder, effect_textures, &texture);
texture
}
SceneImage::Rendered(_) => return None,
SceneImage::Texture(texture) => create_texture(
device,
"vello_gpu_premultiplied_image",
UVec2::new(texture.width(), texture.height()),
TEXTURE_FORMAT,
wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT,
),
};
let raster = match image {
SceneImage::Raster { raster, .. } => Some(raster.downgrade()),
SceneImage::Pixels(_) | SceneImage::Texture(_) | SceneImage::Rendered(_) | SceneImage::GeneratorTile { .. } => None,
};
self.textures.insert(
key.clone(),
ImageTexture {
texture,
last_used: 0,
raster,
adjusted: Vec::new(),
adjustment_source: None,
},
);
}
let image_texture = self.textures.get_mut(&key)?;
image_texture.last_used = self.render_count;
let view = image_texture.texture.create_view(&wgpu::TextureViewDescriptor::default());
if let SceneImage::Texture(source) = image {
let source_view = source.create_view(&wgpu::TextureViewDescriptor::default());
renderer.alpha_conversion.premultiply(device, encoder, &source_view, source.format().is_srgb(), &view);
}
Some(view)
}
/// A view of the copy of the texture of the `raster` already prepared, with the `run` of adjustments carried out on it, in the `precise_format`.
#[expect(clippy::too_many_arguments)]
fn prepare_adjusted(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
effects: (&EffectPasses, &mut EffectTextures),
raster: &Raster<CPU>,
run: &[Adjustment],
max_texture_size: u32,
precise_format: wgpu::TextureFormat,
) -> Option<wgpu::TextureView> {
let (effect_passes, effect_textures) = effects;
let run: Vec<&Adjustment> = run.iter().collect();
let run_hash = adjustment_run_hash(&run);
let render_count = self.render_count;
let image_texture = self.textures.get_mut(&ImageKey::Raster(raster.buffer_id()))?;
// A run seen before has its copy
if let Some(copy) = image_texture.adjusted.iter_mut().find(|copy| copy.run_hash == Some(run_hash)) {
copy.last_used = render_count;
return Some(copy.texture.create_view(&wgpu::TextureViewDescriptor::default()));
}
// A run that changes no color draws the texture itself
let prepared = effect_textures.prepare_adjustments(device, queue, &run);
if prepared.is_empty() {
return Some(image_texture.texture.create_view(&wgpu::TextureViewDescriptor::default()));
}
// A new run takes over the copy longest unused, as when dragging a slider makes a new run every render
let reusable = (image_texture.adjusted.iter().enumerate())
.filter(|(_, copy)| copy.last_used < render_count)
.min_by_key(|(_, copy)| copy.last_used)
.map(|(index, _)| index);
let index = match reusable {
Some(index) => index,
None => {
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("vello_gpu_adjusted_image"),
size: image_texture.texture.size(),
mip_level_count: image_texture.texture.mip_level_count(),
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: precise_format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
image_texture.adjusted.push(AdjustedTexture {
texture,
run_hash: None,
last_used: render_count,
});
image_texture.adjusted.len() - 1
}
};
// Adjusting the raster's own colors, rather than its 8-bit premultiplied texture, keeps faint colors from banding and steps landing as on the CPU
if image_texture.adjustment_source.is_none() {
image_texture.adjustment_source = upload_adjustment_source(device, queue, raster.data(), max_texture_size);
}
let (source, straight_linear) = match &image_texture.adjustment_source {
Some(source) => (source, true),
None => (&image_texture.texture, false),
};
let copy = &mut image_texture.adjusted[index];
effect_passes.adjust_image(device, queue, effect_textures, source, straight_linear, &prepared, &copy.texture);
copy.run_hash = Some(run_hash);
copy.last_used = render_count;
Some(copy.texture.create_view(&wgpu::TextureViewDescriptor::default()))
}
fn free_unused(&mut self) {
let render_count = self.render_count;
let in_use = |last_used: u64| render_count - last_used < UNUSED_IMAGE_RENDER_LIMIT;
let kept = |key: &ImageKey, image_texture: &ImageTexture| match key {
// CPU pixels get a new blob ID at every render, so a blob this render didn't draw is unlikely to return
ImageKey::Blob(_) => image_texture.last_used == render_count,
ImageKey::Raster(_) => image_texture.last_used == render_count || (in_use(image_texture.last_used) && image_texture.raster.as_ref().is_some_and(WeakCPU::is_alive)),
ImageKey::Texture(_) | ImageKey::GeneratorTile(_) => in_use(image_texture.last_used),
};
// Spares this render didn't take over are freed, and the tiles it freed become the next render's spares
self.spare_generator_tiles.clear();
let unused = self.textures.extract_if(|key, image_texture| !kept(key, image_texture));
let unused_tiles = unused.filter(|(key, _)| matches!(key, ImageKey::GeneratorTile(_))).map(|(_, image_texture)| image_texture.texture);
self.spare_generator_tiles.extend(unused_tiles);
self.textures.values_mut().for_each(|image_texture| {
image_texture.adjusted.retain(|copy| in_use(copy.last_used));
if image_texture.adjusted.is_empty() {
image_texture.adjustment_source = None;
}
});
// Panning and zooming across a generator leaves tiles behind faster than they go unused for long, so the least recent beyond a limit are freed too
let mut tile_ages: Vec<u64> = (self.textures.iter())
.filter(|(key, _)| matches!(key, ImageKey::GeneratorTile(_)))
.map(|(_, image_texture)| image_texture.last_used)
.collect();
if tile_ages.len() > MAX_CACHED_GENERATOR_TILES {
let excess = tile_ages.len() - MAX_CACHED_GENERATOR_TILES;
let (_, &mut oldest_kept, _) = tile_ages.select_nth_unstable(excess);
let least_recent = (self.textures).extract_if(|key, image_texture| matches!(key, ImageKey::GeneratorTile(_)) && image_texture.last_used < oldest_kept);
self.spare_generator_tiles.extend(least_recent.map(|(_, image_texture)| image_texture.texture));
}
}
}
/// Uploads CPU pixels as a premultiplied texture, downscaling them by a whole factor if they exceed `max_texture_size`.
fn upload_pixels(device: &wgpu::Device, queue: &wgpu::Queue, pixels: &[u8], width: u32, height: u32, alpha_type: peniko::ImageAlphaType, max_texture_size: u32) -> Option<wgpu::Texture> {
if width == 0 || height == 0 {
return None;
}
let premultiplied: Vec<u8> = match alpha_type {
peniko::ImageAlphaType::Alpha => pixels
.as_chunks::<4>()
.0
.iter()
.flat_map(|pixel| {
let alpha = pixel[3] as u32;
let premultiply = |channel: u8| ((channel as u32 * alpha + 127) / 255) as u8;
[premultiply(pixel[0]), premultiply(pixel[1]), premultiply(pixel[2]), pixel[3]]
})
.collect(),
peniko::ImageAlphaType::AlphaPremultiplied => pixels.to_vec(),
};
// Box-average whole blocks of pixels down until both sides fit, which is exact to average since the pixels are premultiplied
let factor = image_downscale_factor(width, height, max_texture_size);
let (texture_width, texture_height) = image_texture_size(width, height, max_texture_size);
let pixels = match factor {
1 => premultiplied,
_ => box_downscale(&premultiplied, width, height, factor).into_iter().map(|average| average.round() as u8).collect(),
};
// Each mipmap level halves the one before it, so a minified image averages its pixels rather than skipping over them
let level_count = texture_width.max(texture_height).ilog2() + 1;
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("vello_gpu_image"),
size: wgpu::Extent3d {
width: texture_width,
height: texture_height,
depth_or_array_layers: 1,
},
mip_level_count: level_count,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: TEXTURE_FORMAT,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
let (mut level_pixels, mut level_width, mut level_height) = (pixels, texture_width, texture_height);
for level in 0..level_count {
if level > 0 {
(level_pixels, level_width, level_height) = halve_for_mipmap(&level_pixels, level_width, level_height);
}
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: level,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
&level_pixels,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(4 * level_width),
rows_per_image: Some(level_height),
},
wgpu::Extent3d {
width: level_width,
height: level_height,
depth_or_array_layers: 1,
},
);
}
Some(texture)
}
/// Halves premultiplied RGBA8 pixels to the next mipmap level's size, which rounds down, so the last pixel of each row and column of an odd side averages three.
/// O(width × height).
fn halve_for_mipmap(pixels: &[u8], width: u32, height: u32) -> (Vec<u8>, u32, u32) {
let (half_width, half_height) = ((width / 2).max(1), (height / 2).max(1));
// The source pixels along one side that an output pixel averages
let span = |index: u32, half: u32, full: u32| {
let start = index * 2;
let end = if index + 1 == half { full } else { start + 2 };
start..end.clamp(start + 1, full)
};
let mut output = Vec::with_capacity((half_width * half_height * 4) as usize);
for output_y in 0..half_height {
for output_x in 0..half_width {
let mut sums = [0_u32; 4];
let mut count = 0;
for y in span(output_y, half_height, height) {
for x in span(output_x, half_width, width) {
let index = ((y * width + x) * 4) as usize;
sums.iter_mut().zip(&pixels[index..index + 4]).for_each(|(sum, channel)| *sum += *channel as u32);
count += 1;
}
}
output.extend(sums.map(|sum| ((sum + count / 2) / count) as u8));
}
}
(output, half_width, half_height)
}
/// Averages each `factor` by `factor` block of RGBA pixels, letting blocks at the right and bottom edges average only the pixels they have.
fn box_downscale<Channel: Copy + Into<f32>>(pixels: &[Channel], width: u32, height: u32, factor: u32) -> Vec<f32> {
let (output_width, output_height) = (width.div_ceil(factor), height.div_ceil(factor));
let mut output = Vec::with_capacity((output_width * output_height * 4) as usize);
for output_y in 0..output_height {
for output_x in 0..output_width {
let mut sums = [0_f32; 4];
let mut count = 0;
for y in (output_y * factor)..((output_y + 1) * factor).min(height) {
for x in (output_x * factor)..((output_x + 1) * factor).min(width) {
let index = ((y * width + x) * 4) as usize;
for (sum, &channel) in sums.iter_mut().zip(&pixels[index..index + 4]) {
*sum += channel.into();
}
count += 1;
}
}
output.extend(sums.map(|sum| sum / count as f32));
}
}
output
}
/// Uploads a raster's straight linear colors as one level of 32-bit floats, the size [`upload_pixels`] makes them, so adjustments read the CPU's exact values.
/// A raster too large for one texture is averaged down to fit on premultiplied gamma-encoded channels, as [`upload_pixels`] averages them.
fn upload_adjustment_source(device: &wgpu::Device, queue: &wgpu::Queue, image: &Image<core_types::Color>, max_texture_size: u32) -> Option<wgpu::Texture> {
let (width, height) = (image.width, image.height);
if width == 0 || height == 0 {
return None;
}
let factor = image_downscale_factor(width, height, max_texture_size);
let (texture_width, texture_height) = image_texture_size(width, height, max_texture_size);
let downscaled: Vec<f32>;
let texels: &[f32] = if factor == 1 {
bytemuck::cast_slice(&image.data)
} else {
let premultiplied: Vec<f32> = (image.data.iter())
.flat_map(|color| {
let [r, g, b, a] = color.to_gamma_srgb_channels();
[r * a, g * a, b * a, a]
})
.collect();
let averaged = box_downscale(&premultiplied, width, height, factor);
downscaled = (averaged.as_chunks::<4>().0.iter())
.flat_map(|&[r, g, b, a]| {
if a <= 0. {
return [0.; 4];
}
let color = core_types::Color::from_gamma_srgb_channels(r / a, g / a, b / a, a);
[color.r(), color.g(), color.b(), a]
})
.collect();
&downscaled
};
let format = wgpu::TextureFormat::Rgba32Float;
let size = wgpu::Extent3d {
width: texture_width,
height: texture_height,
depth_or_array_layers: 1,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("vello_gpu_adjustment_source"),
size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
bytemuck::cast_slice(texels),
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(16 * texture_width),
rows_per_image: Some(texture_height),
},
size,
);
Some(texture)
}
fn clear_texture(encoder: &mut wgpu::CommandEncoder, view: &wgpu::TextureView) {
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("vello_gpu_clear_pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
}
fn create_texture(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 {
width: size.x.max(1),
height: size.y.max(1),
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage,
view_formats: &[],
})
}
/// Full-target passes converting between straight and premultiplied alpha.
struct AlphaConversion {
bind_group_layout: wgpu::BindGroupLayout,
unpremultiply_pipeline: wgpu::RenderPipeline,
premultiply_pipeline: wgpu::RenderPipeline,
/// The parameters for a source whose format stores linear values.
linear_source_parameters: wgpu::Buffer,
/// The parameters for a source whose format decodes sRGB on load.
srgb_source_parameters: wgpu::Buffer,
}
impl AlphaConversion {
fn new(device: &wgpu::Device) -> Self {
use wgpu::util::DeviceExt;
let shader = device.create_shader_module(wgpu::include_wgsl!("vello_alpha.wgsl"));
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("vello_alpha_bind_group_layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: false },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("vello_alpha_pipeline_layout"),
bind_group_layouts: &[Some(&bind_group_layout)],
immediate_size: 0,
});
let create_pipeline = |label: &str, entry_point: &str| {
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some(label),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vertex"),
compilation_options: Default::default(),
buffers: &[],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some(entry_point),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
format: TEXTURE_FORMAT,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview_mask: None,
cache: None,
})
};
let create_parameters = |label: &str, source_is_srgb: u32| {
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some(label),
contents: bytemuck::bytes_of(&[source_is_srgb, 0, 0, 0]),
usage: wgpu::BufferUsages::UNIFORM,
})
};
Self {
unpremultiply_pipeline: create_pipeline("vello_unpremultiply_pipeline", "unpremultiply"),
premultiply_pipeline: create_pipeline("vello_premultiply_pipeline", "premultiply"),
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 premultiply(&self, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, source: &wgpu::TextureView, source_is_srgb: bool, target: &wgpu::TextureView) {
let parameters = if source_is_srgb { &self.srgb_source_parameters } else { &self.linear_source_parameters };
self.run(device, encoder, &self.premultiply_pipeline, parameters, source, target);
}
fn run(&self, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, pipeline: &wgpu::RenderPipeline, parameters: &wgpu::Buffer, source: &wgpu::TextureView, target: &wgpu::TextureView) {
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("vello_alpha_bind_group"),
layout: &self.bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(source),
},
wgpu::BindGroupEntry {
binding: 1,
resource: parameters.as_entire_binding(),
},
],
});
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("vello_alpha_pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: target,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_pipeline(pipeline);
pass.set_bind_group(0, &bind_group, &[]);
pass.draw(0..3, 0..1);
}
}
#[cfg(test)]
mod tests;