Give the Vello renderer the executor and pool textures with mip levels

This commit is contained in:
Timon committed 2026-12-03 23:05:49 +00:00
1 parent e30bf2e987
commit acd7726dee
3 files changed
+57 -48

No files matched your search

@@ -84,9 +84,7 @@ impl WgpuExecutor {
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);
self.inner
.vello_renderer
.render(&mut canvas, &self.context().device, &self.context().queue, draw, view, size, &texture)?;
self.inner.vello_renderer.render(self, &mut canvas, draw, view, size, &texture)?;
Ok(texture)
}
@@ -95,7 +93,7 @@ impl WgpuExecutor {
pub fn render_vello_scene_on_canvas(&self, canvas: &mut VelloCanvas, scene: VelloSceneBuilder) -> Result<Texture> {
let texture = self.request_texture(UVec2::new(scene.width() as u32, scene.height() as u32));
self.inner.vello_renderer.render_scene(canvas, &self.context().device, &self.context().queue, scene, &texture);
self.inner.vello_renderer.render_scene(self, canvas, scene, &texture);
Ok(texture)
}
@@ -165,7 +163,11 @@ impl WgpuExecutor {
}
pub fn request_texture_with_format(&self, size: UVec2, format: wgpu::TextureFormat) -> Texture {
self.inner.texture_cache.lock().unwrap().request_texture(&self.context().device, size, format)
self.request_texture_with_mips(size, format, 1)
}
pub fn request_texture_with_mips(&self, size: UVec2, format: wgpu::TextureFormat, mip_level_count: u32) -> Texture {
self.inner.texture_cache.lock().unwrap().request_texture(self.context(), size, format, mip_level_count)
}
pub fn create_buffer(&self, desc: &wgpu::BufferDescriptor) -> Buffer {
@@ -1,3 +1,4 @@
use crate::WgpuContext;
use glam::UVec2;
use raster_types::Texture;
use std::collections::VecDeque;
@@ -16,24 +17,23 @@ impl TextureCache {
}
}
pub fn request_texture(&mut self, device: &wgpu::Device, size: UVec2, format: wgpu::TextureFormat) -> Texture {
pub fn request_texture(&mut self, context: &WgpuContext, size: UVec2, format: wgpu::TextureFormat, mip_level_count: u32) -> Texture {
let size = size.max(UVec2::ONE);
if let Some(pos) = self
.textures
.iter()
.position(|texture| UVec2::new(texture.width(), texture.height()) == size && texture.format() == format && !texture.is_shared() && !texture.is_weakly_shared())
{
if let Some(pos) = self.textures.iter().position(|texture| {
UVec2::new(texture.width(), texture.height()) == size && texture.format() == format && texture.mip_level_count() == mip_level_count && !texture.is_shared() && !texture.is_weakly_shared()
}) {
let entry = self.textures.remove(pos).unwrap();
let texture = entry.clone();
self.textures.push_back(entry);
return texture;
}
let incoming_bytes = size.x as u64 * size.y as u64 * format.block_copy_size(None).unwrap_or(4) as u64;
self.evict_until_fits(incoming_bytes);
self.evict_until_fits(memory_size_estimate(size, format, mip_level_count));
let texture: Texture = device
let usage = wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT;
let texture: Texture = context
.device
.create_texture(&wgpu::TextureDescriptor {
label: Some(&format!("cached_{}x{}", size.x, size.y)),
size: wgpu::Extent3d {
@@ -41,11 +41,11 @@ impl TextureCache {
height: size.y,
depth_or_array_layers: 1,
},
mip_level_count: 1,
mip_level_count,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT,
usage: usage & context.adapter.get_texture_format_features(format).allowed_usages,
view_formats: &[],
})
.into();
@@ -83,12 +83,20 @@ impl TextureCache {
}
}
fn memory_size_estimate(size: UVec2, format: wgpu::TextureFormat, mip_level_count: u32) -> u64 {
let texel_bytes = format.block_copy_size(None).unwrap_or(4) as u64;
(0..mip_level_count)
.map(|level| (size >> level).max(UVec2::ONE))
.map(|level_size| level_size.x as u64 * level_size.y as u64 * texel_bytes)
.sum()
}
trait TextureMemoryCostEstimateExt {
fn memory_size_estimate(&self) -> u64;
}
impl TextureMemoryCostEstimateExt for wgpu::Texture {
fn memory_size_estimate(&self) -> u64 {
self.width() as u64 * self.height() as u64 * self.format().block_copy_size(None).unwrap_or(4) as u64
memory_size_estimate(UVec2::new(self.width(), self.height()), self.format(), self.mip_level_count())
}
}
@@ -1,3 +1,4 @@
use crate::WgpuExecutor;
use crate::vello_adjustments::adjustment_run_hash;
use crate::vello_effects::{EffectPasses, EffectTextures};
use crate::vello_generators::GeneratorPasses;
@@ -71,8 +72,7 @@ enum TargetAlpha {
struct RenderContext<'a> {
renderer: &'a VelloRenderer,
canvas: &'a mut VelloCanvas,
device: &'a wgpu::Device,
queue: &'a Queue,
executor: &'a WgpuExecutor,
}
impl VelloRenderer {
@@ -121,12 +121,10 @@ 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).
/// 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,
executor: &WgpuExecutor,
canvas: &mut VelloCanvas,
device: &wgpu::Device,
queue: &Queue,
draw: &mut dyn FnMut(&mut VelloSceneBuilder),
view: Affine,
size: UVec2,
@@ -137,12 +135,7 @@ impl VelloRenderer {
}
canvas.images.render_count += 1;
let mut context = RenderContext {
renderer: self,
canvas,
device,
queue,
};
let mut context = RenderContext { renderer: self, canvas, executor };
context.render_area(draw, view, size, target, TargetAlpha::Straight);
canvas.images.free_unused();
canvas.effect_textures.finish_render();
@@ -152,25 +145,30 @@ impl VelloRenderer {
/// 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) {
pub(crate) fn render_scene(&self, executor: &WgpuExecutor, canvas: &mut VelloCanvas, 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 mut context = RenderContext { renderer: self, canvas, executor };
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);
clear(&executor.context().device, &executor.context().queue, target);
}
canvas.images.free_unused();
canvas.effect_textures.finish_render();
}
}
impl<'a> RenderContext<'a> {
fn device(&self) -> &'a wgpu::Device {
&self.executor.context().device
}
fn queue(&self) -> &'a Queue {
&self.executor.context().queue
}
}
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) {
@@ -182,7 +180,7 @@ impl RenderContext<'_> {
/// 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);
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!(
@@ -254,7 +252,7 @@ impl RenderContext<'_> {
/// 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_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));
@@ -267,7 +265,7 @@ impl RenderContext<'_> {
let (scene, images, _) = self.draw_scene(draw, grown_view, grown_size, max_texture_size, 0);
let grown_target = create_texture(
self.device,
self.device(),
"vello_gpu_grown_target",
grown_size,
target.format(),
@@ -276,7 +274,7 @@ impl RenderContext<'_> {
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") });
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 },
@@ -289,7 +287,7 @@ impl RenderContext<'_> {
depth_or_array_layers: 1,
},
);
self.queue.submit([encoder.finish()]);
self.queue().submit([encoder.finish()]);
return rendered;
}
@@ -334,12 +332,12 @@ impl RenderContext<'_> {
/// 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 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();
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") });
@@ -445,24 +443,25 @@ impl EffectsRenderer for RenderContext<'_> {
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);
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);
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 device = self.device();
let queue = self.queue().lock();
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,
device,
&queue,
effect_textures,
&content_texture,
@@ -484,7 +483,7 @@ impl EffectsRenderer for RenderContext<'_> {
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);
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) {