mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-05 19:12:08 -10:00
Premultiply textures drawn as images into texture pool copies (fixup #4807)
This commit is contained in:
1 parent
dd996adbcb
commit
506c413cd0
1 file changed
+20
-42
@@ -331,11 +331,22 @@ impl RenderContext<'_> {
|
||||
|
||||
// Supply the textures of the images the scene draws, under the IDs it drew them with
|
||||
let mut texture_bindings = TextureBindings::new();
|
||||
let mut held = Vec::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);
|
||||
let view = match image {
|
||||
SceneImage::Texture(source) => {
|
||||
let premultiplied = self.executor.request_texture(UVec2::new(source.width(), source.height()));
|
||||
let view = premultiplied.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let source_view = source.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
self.renderer.alpha_conversion.premultiply(device, &mut encoder, &source_view, source.format().is_srgb(), &view);
|
||||
held.push(premultiplied);
|
||||
Some(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 {
|
||||
@@ -490,12 +501,11 @@ fn clear(device: &wgpu::Device, queue: &Queue, target: &wgpu::Texture) {
|
||||
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.
|
||||
/// Identifies the pixels behind an image texture: a blob of CPU pixels, a CPU raster, or a tile of a generator's pattern.
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
enum ImageKey {
|
||||
Blob(u64),
|
||||
Raster(u64),
|
||||
Texture(wgpu::Texture),
|
||||
GeneratorTile(GeneratorTile),
|
||||
}
|
||||
|
||||
@@ -530,7 +540,6 @@ struct ImageTextures {
|
||||
|
||||
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,
|
||||
@@ -545,10 +554,10 @@ impl ImageTextures {
|
||||
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())),
|
||||
SceneImage::Texture(_) => return None,
|
||||
};
|
||||
|
||||
if !self.textures.contains_key(&key) {
|
||||
@@ -572,14 +581,7 @@ impl ImageTextures {
|
||||
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,
|
||||
),
|
||||
SceneImage::Rendered(_) | SceneImage::Texture(_) => return None,
|
||||
};
|
||||
let raster = match image {
|
||||
SceneImage::Raster { raster, .. } => Some(raster.downgrade()),
|
||||
@@ -599,14 +601,7 @@ impl ImageTextures {
|
||||
|
||||
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)
|
||||
Some(image_texture.texture.create_view(&wgpu::TextureViewDescriptor::default()))
|
||||
}
|
||||
|
||||
/// 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`.
|
||||
@@ -691,7 +686,7 @@ impl ImageTextures {
|
||||
// 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),
|
||||
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
|
||||
@@ -940,23 +935,6 @@ fn clear_texture(encoder: &mut wgpu::CommandEncoder, view: &wgpu::TextureView) {
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user