mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-05 19:12:08 -10:00
Park image uploads and adjusted copies in the texture pool (fixup #4807, #4855, #4908, #4997, #5037)
This commit is contained in:
1 parent
239df7af7c
commit
dee8681459
1 file changed
+86
-109
@@ -26,9 +26,6 @@ pub(crate) const TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba
|
||||
/// 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;
|
||||
|
||||
/// 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;
|
||||
|
||||
@@ -351,7 +348,7 @@ impl RenderContext<'_> {
|
||||
let effects = (&self.renderer.effect_passes, &mut self.canvas.effect_textures);
|
||||
self.canvas
|
||||
.images
|
||||
.prepare_adjusted(self.executor, &queue, effects, raster, adjustments, max_texture_size, self.renderer.precise_format)
|
||||
.prepare_adjusted(self.executor, &queue, effects, raster, adjustments, max_texture_size, self.renderer.precise_format, &mut held)
|
||||
}),
|
||||
_ => view,
|
||||
};
|
||||
@@ -506,25 +503,24 @@ enum ImageKey {
|
||||
|
||||
/// A premultiplied copy of an image's pixels that Vello GPU samples as an external texture.
|
||||
struct ImageTexture {
|
||||
texture: wgpu::Texture,
|
||||
texture: TextureWeakRef,
|
||||
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>,
|
||||
adjustment_source: Option<TextureWeakRef>,
|
||||
}
|
||||
|
||||
/// A copy of an image's texture with a run of adjustments carried out on it, made over in place for another run.
|
||||
/// A copy of an image's texture with a run of adjustments carried out on it, made over 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>,
|
||||
texture: TextureWeakRef,
|
||||
run_hash: u64,
|
||||
last_used: u64,
|
||||
}
|
||||
|
||||
/// The textures of the images recent renders drew, each freed once it goes unused for long enough.
|
||||
/// The textures of the images recent renders drew, parked in the texture pool between renders.
|
||||
#[derive(Default)]
|
||||
struct ImageTextures {
|
||||
textures: HashMap<ImageKey, ImageTexture>,
|
||||
@@ -571,41 +567,46 @@ impl ImageTextures {
|
||||
SceneImage::Texture(_) => return None,
|
||||
};
|
||||
|
||||
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;
|
||||
let texture = match self.textures.get(&key).and_then(|image_texture| image_texture.texture.upgrade()) {
|
||||
Some(texture) => texture,
|
||||
None => {
|
||||
let texture = match image {
|
||||
SceneImage::Pixels(data) => {
|
||||
if data.format != peniko::ImageFormat::Rgba8 {
|
||||
log::error!("Unsupported image format {:?}", data.format);
|
||||
return None;
|
||||
}
|
||||
|
||||
upload_pixels(executor, 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(executor, queue, &pixels, width, height, peniko::ImageAlphaType::Alpha, max_texture_size)?
|
||||
}
|
||||
SceneImage::Rendered(_) | SceneImage::Texture(_) | SceneImage::GeneratorTile { .. } => return None,
|
||||
};
|
||||
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: texture.downgrade(),
|
||||
last_used: 0,
|
||||
raster,
|
||||
adjusted: Vec::new(),
|
||||
adjustment_source: None,
|
||||
},
|
||||
);
|
||||
texture
|
||||
}
|
||||
};
|
||||
|
||||
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::Rendered(_) | SceneImage::Texture(_) | SceneImage::GeneratorTile { .. } => return None,
|
||||
};
|
||||
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;
|
||||
Some(image_texture.texture.create_view(&wgpu::TextureViewDescriptor::default()))
|
||||
self.textures.get_mut(&key)?.last_used = self.render_count;
|
||||
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
held.push(texture);
|
||||
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`.
|
||||
@@ -619,6 +620,7 @@ impl ImageTextures {
|
||||
run: &[Adjustment],
|
||||
max_texture_size: u32,
|
||||
precise_format: wgpu::TextureFormat,
|
||||
held: &mut Vec<Texture>,
|
||||
) -> Option<wgpu::TextureView> {
|
||||
let device = &executor.context().device;
|
||||
let (effect_passes, effect_textures) = effects;
|
||||
@@ -626,17 +628,24 @@ impl ImageTextures {
|
||||
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()))?;
|
||||
let image = image_texture.texture.upgrade()?;
|
||||
|
||||
// A run seen before has its copy
|
||||
if let Some(copy) = image_texture.adjusted.iter_mut().find(|copy| copy.run_hash == Some(run_hash)) {
|
||||
let seen = (image_texture.adjusted.iter_mut()).find(|copy| copy.run_hash == run_hash).and_then(|copy| {
|
||||
copy.last_used = render_count;
|
||||
return Some(copy.texture.create_view(&wgpu::TextureViewDescriptor::default()));
|
||||
copy.texture.upgrade()
|
||||
});
|
||||
if let Some(copy) = seen {
|
||||
let view = copy.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
held.push(copy);
|
||||
return Some(view);
|
||||
}
|
||||
image_texture.adjusted.retain(|copy| copy.run_hash != run_hash);
|
||||
|
||||
// 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()));
|
||||
return Some(image.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
|
||||
@@ -644,60 +653,50 @@ impl ImageTextures {
|
||||
.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
|
||||
}
|
||||
};
|
||||
if let Some(index) = reusable {
|
||||
image_texture.adjusted.swap_remove(index);
|
||||
}
|
||||
let copy = executor.request_texture_with_mips(UVec2::new(image.width(), image.height()), precise_format, image.mip_level_count());
|
||||
|
||||
// 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 {
|
||||
let source = match image_texture.adjustment_source.as_ref().and_then(TextureWeakRef::upgrade) {
|
||||
Some(source) => Some(source),
|
||||
None => {
|
||||
let source = upload_adjustment_source(executor, queue, raster.data(), max_texture_size);
|
||||
image_texture.adjustment_source = source.as_ref().map(Texture::downgrade);
|
||||
source
|
||||
}
|
||||
};
|
||||
let (source, straight_linear) = match &source {
|
||||
Some(source) => (source, true),
|
||||
None => (&image_texture.texture, false),
|
||||
None => (&image, false),
|
||||
};
|
||||
|
||||
let copy = &mut image_texture.adjusted[index];
|
||||
effect_passes.adjust_image(executor, queue, effect_textures, source, straight_linear, &prepared, ©.texture);
|
||||
copy.run_hash = Some(run_hash);
|
||||
copy.last_used = render_count;
|
||||
effect_passes.adjust_image(executor, queue, effect_textures, source, straight_linear, &prepared, ©);
|
||||
image_texture.adjusted.push(AdjustedTexture {
|
||||
texture: copy.downgrade(),
|
||||
run_hash,
|
||||
last_used: render_count,
|
||||
});
|
||||
|
||||
Some(copy.texture.create_view(&wgpu::TextureViewDescriptor::default()))
|
||||
let view = copy.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
held.push(copy);
|
||||
Some(view)
|
||||
}
|
||||
|
||||
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::Raster(_) => image_texture.last_used == render_count || image_texture.raster.as_ref().is_some_and(WeakCPU::is_alive),
|
||||
};
|
||||
|
||||
self.textures.retain(|key, image_texture| kept(key, image_texture));
|
||||
self.textures.retain(|key, image_texture| kept(key, image_texture) && image_texture.texture.upgrade().is_some());
|
||||
self.generator_tiles.retain(|_, texture| texture.upgrade().is_some());
|
||||
|
||||
self.textures.values_mut().for_each(|image_texture| {
|
||||
image_texture.adjusted.retain(|copy| in_use(copy.last_used));
|
||||
image_texture.adjusted.retain(|copy| copy.texture.upgrade().is_some());
|
||||
if image_texture.adjusted.is_empty() {
|
||||
image_texture.adjustment_source = None;
|
||||
}
|
||||
@@ -706,7 +705,7 @@ impl ImageTextures {
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
fn upload_pixels(executor: &WgpuExecutor, queue: &wgpu::Queue, pixels: &[u8], width: u32, height: u32, alpha_type: peniko::ImageAlphaType, max_texture_size: u32) -> Option<Texture> {
|
||||
if width == 0 || height == 0 {
|
||||
return None;
|
||||
}
|
||||
@@ -735,20 +734,7 @@ fn upload_pixels(device: &wgpu::Device, queue: &wgpu::Queue, pixels: &[u8], widt
|
||||
|
||||
// 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 texture = executor.request_texture_with_mips(UVec2::new(texture_width, texture_height), TEXTURE_FORMAT, level_count);
|
||||
|
||||
let (mut level_pixels, mut level_width, mut level_height) = (pixels, texture_width, texture_height);
|
||||
for level in 0..level_count {
|
||||
@@ -839,7 +825,7 @@ fn box_downscale<Channel: Copy + Into<f32>>(pixels: &[Channel], width: u32, heig
|
||||
|
||||
/// 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> {
|
||||
fn upload_adjustment_source(executor: &WgpuExecutor, queue: &wgpu::Queue, image: &Image<core_types::Color>, max_texture_size: u32) -> Option<Texture> {
|
||||
let (width, height) = (image.width, image.height);
|
||||
if width == 0 || height == 0 {
|
||||
return None;
|
||||
@@ -876,16 +862,7 @@ fn upload_adjustment_source(device: &wgpu::Device, queue: &wgpu::Queue, image: &
|
||||
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: &[],
|
||||
});
|
||||
let texture = executor.request_texture_with_format(UVec2::new(texture_width, texture_height), format);
|
||||
queue.write_texture(
|
||||
wgpu::TexelCopyTextureInfo {
|
||||
texture: &texture,
|
||||
|
||||
Reference in new issue
Block a user