PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Fix corrupted reduced canvas previews with separate mip render targets (#892)
Wil1e1d3dcad493
diff --git a/crates/ui-egui/src/gpu_canvas.rs b/crates/ui-egui/src/gpu_canvas.rsindex 693de94b7..5197836d5 100644--- a/crates/ui-egui/src/gpu_canvas.rs+++ b/crates/ui-egui/src/gpu_canvas.rs@@ -127,6 +127,7 @@ impl GpuCanvas {let health = photocraft_gpu::DeviceHealth::watch(&rs.device);let mut res = Resources::new(&rs.device, &rs.queue, rs.target_format, high != HighPolicy::Off);res.health = health.clone();+ res.separate_mip_targets = rs.adapter.get_info().backend == wgpu::Backend::Dx12;rs.renderer.write().callback_resources.insert(res);log::info!("gpu canvas: target {:?}, max texture {max}, tile {tile}, 16F canvas {high:?}", rs.target_format);Self { rs: rs.clone(), tile, high, health }@@ -1101,6 +1102,9 @@ struct Resources {docs: HashMap<u64, DocTextures>,views: HashMap<u64, ViewGpu>,out_linear: bool,+ /// Build each mip level in a scratch texture and copy it back instead of rendering into+ /// the level directly (DX12 only: works around reduced-preview corruption on Intel drivers).+ separate_mip_targets: bool,/// The wgpu layer compositor (created on first use).compositor: Option<photocraft_gpu::Compositor>,/// Why the wgpu compositor couldn't be created (then the CPU compositor is used).@@ -1369,6 +1373,7 @@ impl Resources {docs: HashMap::new(),views: HashMap::new(),out_linear: target.is_srgb(),+ separate_mip_targets: false,compositor: None,compositor_failed: None,health: photocraft_gpu::DeviceHealth::new(),@@ -1497,13 +1502,36 @@ impl DocTextures {wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&res.mip_sampler) },],});+ // Keep the sampled source and render attachment in separate resources on DX12+ // (`Resources::separate_mip_targets`): some Intel DX12 drivers corrupt reduced+ // previews when both are mip levels of the same texture, even though the+ // subresources do not overlap. Elsewhere render straight into the level, which+ // spares a scratch texture and a copy per level on every edit.+ let scratch = res.separate_mip_targets.then(|| {+ let scratch = device.create_texture(&wgpu::TextureDescriptor {+ label: Some("pc_mip_scratch"),+ size: wgpu::Extent3d { width: lw, height: lh, depth_or_array_layers: 1 },+ mip_level_count: 1,+ sample_count: 1,+ dimension: wgpu::TextureDimension::D2,+ format: self.format,+ usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,+ view_formats: &[],+ });+ let view = scratch.create_view(&Default::default());+ (scratch, view)+ });+ let (target, load) = match &scratch {+ Some((_, view)) => (view, wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT)),+ None => (&t.levels[level], wgpu::LoadOp::Load),+ };let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {label: Some("pc_mip"),color_attachments: &[Some(wgpu::RenderPassColorAttachment {- view: &t.levels[level],+ view: target,resolve_target: None,depth_slice: None,- ops: wgpu::Operations { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store },+ ops: wgpu::Operations { load, store: wgpu::StoreOp::Store },})],depth_stencil_attachment: None,timestamp_writes: None,@@ -1514,6 +1542,15 @@ impl DocTextures {pass.set_bind_group(0, &bg, &[]);pass.set_scissor_rect(x0, y0, x1 - x0, y1 - y0);pass.draw(0..3, 0..1);+ drop(pass);+ if let Some((scratch, _)) = &scratch {+ let origin = wgpu::Origin3d { x: x0, y: y0, z: 0 };+ encoder.copy_texture_to_texture(+ wgpu::TexelCopyTextureInfo { texture: scratch, mip_level: 0, origin, aspect: wgpu::TextureAspect::All },+ wgpu::TexelCopyTextureInfo { texture: &t.texture, mip_level: level as u32, origin, aspect: wgpu::TextureAspect::All },+ wgpu::Extent3d { width: x1 - x0, height: y1 - y0, depth_or_array_layers: 1 },+ );+ }}}queue.submit([encoder.finish()]);@@ -1878,6 +1915,10 @@ fn fs(in: V) -> @location(0) vec4<f32> {}"#;+#[cfg(test)]+#[path = "gpu_canvas_mip_tests.rs"]+mod mip_tests;+#[cfg(test)]mod tests {use super::*;diff --git a/crates/ui-egui/src/gpu_canvas_mip_tests.rs b/crates/ui-egui/src/gpu_canvas_mip_tests.rsnew file mode 100644index 000000000..a1c3e8ee1--- /dev/null+++ b/crates/ui-egui/src/gpu_canvas_mip_tests.rs@@ -0,0 +1,103 @@+//! Read every mip, rather than only level zero: a clean 100% view does not prove that+//! reduced previews are valid. Keep all cases on one device (some drivers dislike concurrent+//! test devices), and use synthetic pixels instead of event photographs.++use super::*;+use photocraft_compose::Buffer;+use photocraft_doc::SampleType;+use photocraft_geom::Rect;++fn read_mips(gpu: &GpuCanvas, key: u64) -> Vec<Vec<[f32; 4]>> {+ let renderer = gpu.rs.renderer.read();+ let doc = &renderer.callback_resources.get::<Resources>().unwrap().docs[&key];+ assert_eq!(doc.tiles.len(), 1);+ let tile = &doc.tiles[0];+ let (device, queue) = (&gpu.rs.device, &gpu.rs.queue);+ let bpp = texel_bytes(doc.format) as usize;+ (0..tile.levels.len())+ .map(|level| {+ let (w, h) = ((doc.size[0] >> level).max(1), (doc.size[1] >> level).max(1));+ let row = (w as usize * bpp).next_multiple_of(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize);+ let buffer = device.create_buffer(&wgpu::BufferDescriptor {+ label: Some("mip_test_readback"),+ size: (row * h as usize) as u64,+ usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,+ mapped_at_creation: false,+ });+ let mut encoder = device.create_command_encoder(&Default::default());+ encoder.copy_texture_to_buffer(+ wgpu::TexelCopyTextureInfo { texture: &tile.texture, mip_level: level as u32, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All },+ wgpu::TexelCopyBufferInfo {+ buffer: &buffer,+ layout: wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(row as u32), rows_per_image: Some(h) },+ },+ wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },+ );+ queue.submit([encoder.finish()]);+ buffer.slice(..).map_async(wgpu::MapMode::Read, |result| result.unwrap());+ assert!(gpu.health.wait(device, None));+ let data = buffer.slice(..).get_mapped_range().unwrap();+ let mut pixels = Vec::new();+ for y in 0..h as usize {+ for x in 0..w as usize {+ let offset = y * row + x * bpp;+ pixels.push(texel_to_f32(doc.format, &data[offset..offset + bpp]));+ }+ }+ pixels+ })+ .collect()+}++#[test]+fn canvas_mips_preserve_opaque_pixels_and_partial_updates() {+ let rs = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| egui_kittest::wgpu::create_render_state(wgpu_setup(), Default::default())));+ let Ok(rs) = rs else {+ eprintln!("skipping mip readback: no GPU adapter");+ return;+ };+ eprintln!("mip readback adapter: {:?}", rs.adapter.get_info());+ let gpu = GpuCanvas::new(&rs);+ // Both mip paths: straight into the level, and the DX12 scratch-texture workaround.+ for separate in [false, true] {+ gpu.rs.renderer.write().callback_resources.get_mut::<Resources>().unwrap().separate_mip_targets = separate;+ check_mips(&gpu);+ }+}++fn check_mips(gpu: &GpuCanvas) {+ for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {+ for (w, h) in [(1791, 1083), (513, 1), (1, 513)] {+ let mut full = Buffer::filled(Rect::from_xywh(0, 0, w, h), [0.25, 0.5, 0.75, 1.0]);+ // Reusing the key also covers replacing textures after a size or depth change.+ gpu.upload_buffer_full(1, &full, depth);+ for (level, pixels) in read_mips(gpu, 1).iter().enumerate() {+ for pixel in pixels {+ assert!((pixel[3] - 1.0).abs() < 0.001, "{depth:?} {w}x{h} mip {level}: {pixel:?}");+ for (actual, expected) in pixel.iter().zip([0.25, 0.5, 0.75, 1.0]) {+ assert!((actual - expected).abs() < 0.005, "{depth:?} {w}x{h} mip {level}: {pixel:?}");+ }+ }+ }+ let patch = Rect::from_xywh((w / 2) as i32, (h / 2) as i32, (w / 3).max(1), (h / 3).max(1));+ let changed = Buffer::filled(patch, [1.0, 0.0, 0.0, 1.0]);+ assert!(gpu.upload_buffer_rect(1, &changed));+ for y in patch.y0..patch.y1 {+ for x in patch.x0..patch.x1 {+ full.px[y as usize * w as usize + x as usize] = [1.0, 0.0, 0.0, 1.0];+ }+ }+ gpu.upload_buffer_full(2, &full, depth);+ let partial = read_mips(gpu, 1);+ let rebuilt = read_mips(gpu, 2);+ for (level, (actual, expected)) in partial.iter().zip(&rebuilt).enumerate() {+ for (a, b) in actual.iter().zip(expected) {+ assert!((a[3] - 1.0).abs() < 0.001, "partial {depth:?} mip {level}: {a:?}");+ for (a, b) in a.iter().zip(b) {+ assert!((a - b).abs() < 0.005, "partial/full mismatch {depth:?} {w}x{h} mip {level}: {a} != {b}");+ }+ }+ }+ }+ }+}