Files
photocraft/crates/gpu/src/kit.rs
T
aiongg c55c0b7ce8 gpu: build each compositor pipeline on first use (#15439)
Every compositor built all ~30 pipelines when created. On the Windows CI
runners (WARP, FXC) that took 3 to 7.5 s in every GPU test process, and a
test that draws one layer needs two or three of them.

The pipelines now sit in one table per compositor (shared with its
siblings), one entry per kernel, each built the first time a draw needs
it. try_new_with_format_threads(_classified), which the app uses for its
background build, still builds every pipeline up front in parallel
through the same table, so the app keeps its start-up error and its
cached deterministic-failure path. A pipeline that fails on first use
makes that render Unsupported (CPU fallback); a failed effect-map
pipeline turns layer effects to the CPU as before.

The #9173 rules hold: each kernel has its own vertex entry point, a
kernel's formats are built on one thread, and every build in the process
takes the same lock.

every_compositor_pipeline_builds builds all pipelines through the
threaded build, so FXC still compiles every shader on Windows CI. New
count test: one layer builds a small share of the pipelines, and a second
render or a sibling builds none.
2026-11-18 18:51:38 +00:00

149 lines
6.4 KiB
Rust

use super::consts::{CHUNK_UNIFORM, OP_UNIFORM, STRIDE};
use super::convert::{lut_texture, words};
use super::pipelines::Pipelines;
use super::textures::{ResKey, ResidentRef};
use super::{Arc, I, Kernel, NonZeroU64, Rect, Unsupported};
/// Pipelines and layouts, shared by cheap clones (wgpu handles are reference counted; a sibling
/// compositor uses the pipelines any of them built).
#[derive(Clone)]
pub(crate) struct Kit {
pub(crate) pipelines: Arc<Pipelines>,
pub(crate) bgl0: wgpu::BindGroupLayout,
pub(crate) bgl1: wgpu::BindGroupLayout,
pub(crate) dummy: wgpu::TextureView,
}
/// One draw of an effect-map kernel (outside the chunk loop).
pub(crate) struct MapDraw {
pub(crate) kernel: Kernel,
pub(crate) format: wgpu::TextureFormat,
pub(crate) target: wgpu::TextureView,
/// Target-local scissor (x, y, w, h).
pub(crate) scissor: [u32; 4],
/// Chunk record: origin and size of the target's space.
pub(crate) space: [i32; 4],
pub(crate) op: Vec<u8>,
pub(crate) views: [Option<wgpu::TextureView>; 8],
pub(crate) lut: Option<Arc<Vec<f32>>>,
}
impl Kit {
/// Record `draws` (each its own render pass) into `encoder`.
pub(crate) fn run(&self, device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, draws: &[MapDraw]) -> Result<(), Unsupported> {
if draws.is_empty() {
return Ok(());
}
let mut data = vec![0u8; draws.len() * 2 * STRIDE as usize];
for (i, d) in draws.iter().enumerate() {
let w = words(&[I(d.space[0]), I(d.space[1]), I(d.space[2]), I(d.space[3])]);
data[2 * i * STRIDE as usize..][..w.len()].copy_from_slice(&w);
data[(2 * i + 1) * STRIDE as usize..][..d.op.len()].copy_from_slice(&d.op);
}
let ubuf = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("pc_fx_uniforms"),
size: data.len() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
queue.write_buffer(&ubuf, 0, &data);
let bg0 = uniform_group(device, &self.bgl0, &ubuf);
for (i, d) in draws.iter().enumerate() {
let pipeline = self.pipelines.get(device, d.kernel, d.format)?;
let lut = d.lut.as_ref().map(|l| {
let mut row = [0.0f32; 4096];
row[..l.len().min(4096)].copy_from_slice(&l[..l.len().min(4096)]);
lut_texture(device, queue, &[row])
});
let mut views: Vec<&wgpu::TextureView> = d.views.iter().map(|v| v.as_ref().unwrap_or(&self.dummy)).collect();
if let Some((_, lv)) = &lut {
views[4] = lv;
}
let bg1 = texture_group(device, &self.bgl1, &views);
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("pc_fx_map"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &d.target,
resolve_target: None,
depth_slice: None,
ops: wgpu::Operations { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store },
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_pipeline(pipeline);
pass.set_scissor_rect(d.scissor[0], d.scissor[1], d.scissor[2], d.scissor[3]);
pass.set_bind_group(0, &bg0, &[(2 * i as u64 * STRIDE) as u32, ((2 * i + 1) as u64 * STRIDE) as u32]);
pass.set_bind_group(1, &bg1, &[]);
pass.draw(0..3, 0..1);
}
Ok(())
}
}
pub(crate) fn uniform_group(device: &wgpu::Device, layout: &wgpu::BindGroupLayout, ubuf: &wgpu::Buffer) -> wgpu::BindGroup {
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("pc_compose_uniforms"),
layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { buffer: ubuf, offset: 0, size: NonZeroU64::new(CHUNK_UNIFORM) }),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { buffer: ubuf, offset: 0, size: NonZeroU64::new(OP_UNIFORM) }),
},
],
})
}
pub(crate) fn texture_group(device: &wgpu::Device, layout: &wgpu::BindGroupLayout, views: &[&wgpu::TextureView]) -> wgpu::BindGroup {
let e: Vec<wgpu::BindGroupEntry> =
views.iter().enumerate().map(|(b, v)| wgpu::BindGroupEntry { binding: b as u32, resource: wgpu::BindingResource::TextureView(v) }).collect();
device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("pc_compose_pass"), layout, entries: &e })
}
pub(crate) fn tex_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
sample_type: wgpu::TextureSampleType::Float { filterable: false },
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
}
}
pub(crate) fn uniform_entry(binding: u32, size: u64) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Uniform, has_dynamic_offset: true, min_binding_size: NonZeroU64::new(size) },
count: None,
}
}
/// The compositor's WGSL source (exposed for validation in tests).
pub const SHADER: &str = include_str!("compose.wgsl");
/// Pass-level resources resolved for one page cell: resident pages (texture, mask) and effect
/// maps (view and region) per pass.
pub(crate) struct Bound {
pub(crate) views: Vec<(Option<ResidentRef>, Option<ResidentRef>)>,
pub(crate) keys: Vec<ResKey>,
pub(crate) maps: Vec<Option<(wgpu::TextureView, Rect)>>,
}
/// Pass-level resources shared by every cell of a frame.
pub(crate) struct FrameRes {
/// Tile area of each pass's (texture, mask) surface within the canvas tile grid.
pub(crate) areas: Vec<(Rect, Rect)>,
pub(crate) luts: Vec<Option<(wgpu::Texture, wgpu::TextureView)>>,
pub(crate) patterns: Vec<Option<wgpu::TextureView>>,
}