Merge pull request #8734 from ebosido444-ai/fix/fxc-blend-channel

fix(gpu): return once at the end of blend_channel so FXC can build the compositor
This commit is contained in:
Tanish Manku authored and GitHub committed 2026-11-10 23:55:35 -07:00
commit 952be70074
3 files changed
+92 -37

No files matched your search

+37 -37
View File
@@ -166,61 +166,61 @@ fn vivid_light_generic(cb: f32, cs: f32) -> f32 {
// WebGPU compiler rejects it, which left the web app without a GPU compositor.
const F32_MAX: f32 = 0x1.fffffep+127f;
// FXC (the DX12 compiler between naga's HLSL and the driver) stops following a switch this size
// once every case returns: "error X3507: 'blend_channel': Not all control paths return a value",
// and then no compositor pipeline builds — the whole session composites on the CPU. Each case
// assigns instead; the one return at the end is what FXC's flow analysis can follow.
fn blend_channel(mode: i32, cb: f32, cs: f32) -> f32 {
// Linear Dodge / Divide clip at 1 at integer depths, at f32::MAX in 32-bit documents.
let hi = select(1.0, F32_MAX, (op.flags & F_HDR) != 0u);
var r = cs; // Normal, Dissolve, PassThrough
switch mode {
case 3: { return min(cb, cs); } // Darken
case 4: { return cb * cs; } // Multiply
case 5: { return color_burn(cb, cs); } // ColorBurn
case 6: { return max(cb + cs - 1.0, 0.0); } // LinearBurn
case 8: { return max(cb, cs); } // Lighten
case 9: { return cb + cs - cb * cs; } // Screen
case 10: { return color_dodge(cb, cs); } // ColorDodge
case 11: { return min(cb + cs, hi); } // LinearDodge
case 13: { return hard_light(cs, cb); } // Overlay
case 14: { return soft_light_ps(cb, cs); } // SoftLight
case 15: { return hard_light(cb, cs); } // HardLight
case 16: { return vivid_light_ps(cb, cs); } // VividLight (Photoshop)
case 17: { return clamp(cb + 2.0 * cs - 1.0, 0.0, 1.0); } // LinearLight
case 3: { r = min(cb, cs); } // Darken
case 4: { r = cb * cs; } // Multiply
case 5: { r = color_burn(cb, cs); } // ColorBurn
case 6: { r = max(cb + cs - 1.0, 0.0); } // LinearBurn
case 8: { r = max(cb, cs); } // Lighten
case 9: { r = cb + cs - cb * cs; } // Screen
case 10: { r = color_dodge(cb, cs); } // ColorDodge
case 11: { r = min(cb + cs, hi); } // LinearDodge
case 13: { r = hard_light(cs, cb); } // Overlay
case 14: { r = soft_light_ps(cb, cs); } // SoftLight
case 15: { r = hard_light(cb, cs); } // HardLight
case 16: { r = vivid_light_ps(cb, cs); } // VividLight (Photoshop)
case 17: { r = clamp(cb + 2.0 * cs - 1.0, 0.0, 1.0); } // LinearLight
case 18: { // PinLight
if (cs <= 0.5) { return min(cb, 2.0 * cs); }
return max(cb, 2.0 * cs - 1.0);
if (cs <= 0.5) { r = min(cb, 2.0 * cs); } else { r = max(cb, 2.0 * cs - 1.0); }
}
case 19: { // HardMix (Photoshop)
return select(0.0, 1.0, vivid_light_generic(cb, cs) >= 0.5 - 1e-6);
r = select(0.0, 1.0, vivid_light_generic(cb, cs) >= 0.5 - 1e-6);
}
case 20: { return abs(cb - cs); } // Difference
case 21: { return cb + cs - 2.0 * cb * cs; } // Exclusion
case 22: { return max(cb - cs, 0.0); } // Subtract
case 20: { r = abs(cb - cs); } // Difference
case 21: { r = cb + cs - 2.0 * cb * cs; } // Exclusion
case 22: { r = max(cb - cs, 0.0); } // Subtract
case 23: { // Divide
if (cs <= 0.0) { return select(1.0, 0.0, cb <= 0.0); }
return min(cb / cs, hi);
if (cs <= 0.0) { r = select(1.0, 0.0, cb <= 0.0); } else { r = min(cb / cs, hi); }
}
case 28: { // Reflect
if (cs >= 1.0) { return 1.0; }
return min(cb * cb / (1.0 - cs), 1.0);
case 28: { // Reflect
if (cs >= 1.0) { r = 1.0; } else { r = min(cb * cb / (1.0 - cs), 1.0); }
}
case 29: { // Glow
if (cb >= 1.0) { return 1.0; }
return min(cs * cs / (1.0 - cb), 1.0);
case 29: { // Glow
if (cb >= 1.0) { r = 1.0; } else { r = min(cs * cs / (1.0 - cb), 1.0); }
}
case 30: { return 1.0 - abs(1.0 - cb - cs); } // Negation
case 31: { // XOR (8-bit channel operation)
case 30: { r = 1.0 - abs(1.0 - cb - cs); } // Negation
case 31: { // XOR (8-bit channel operation)
let b = u32(floor(clamp(cb, 0.0, 1.0) * 255.0 + 0.5));
let s = u32(floor(clamp(cs, 0.0, 1.0) * 255.0 + 0.5));
return f32(b ^ s) / 255.0;
r = f32(b ^ s) / 255.0;
}
case 32: { // Paint.NET Color Burn
if (cs <= 0.0) { return 0.0; }
return max(1.0 - (1.0 - cb) / cs, 0.0);
case 32: { // Paint.NET Color Burn
if (cs <= 0.0) { r = 0.0; } else { r = max(1.0 - (1.0 - cb) / cs, 0.0); }
}
case 33: { // Paint.NET Color Dodge
if (cs >= 1.0) { return 1.0; }
return min(cb / (1.0 - cs), 1.0);
case 33: { // Paint.NET Color Dodge
if (cs >= 1.0) { r = 1.0; } else { r = min(cb / (1.0 - cs), 1.0); }
}
default: { return cs; } // Normal, Dissolve, PassThrough
default: { r = cs; }
}
return r;
}
fn lum(c: vec3<f32>) -> f32 { return 0.3 * c.r + 0.59 * c.g + 0.11 * c.b; }
+1
View File
@@ -9,6 +9,7 @@ mod camera_raw;
mod device_loss;
mod parity;
mod pipeline_errors;
mod pipelines;
mod timing;
mod uniform_limits;
mod vector_mask;
+54
View File
@@ -0,0 +1,54 @@
//! The compositor's WGSL must compile with the platform's own shader compiler, not only with naga.
//!
//! On DX12 wgpu translates the WGSL to HLSL and compiles it with FXC, unless `dxcompiler.dll`
//! happens to be on `PATH`, where it prefers DXC — which is why the first instance below forces
//! FXC. FXC's flow analysis stops following shapes WGSL allows (a switch whose every case returns,
//! past thirteen cases) and reports `error X3507: 'blend_channel': Not all control paths return a
//! value`. naga validates that same shader, so nothing caught it but the real compiler: no
//! compositor pipeline built, and DX12 users composited every document on the CPU for the session.
//!
//! The Windows CI runners have no GPU: their adapter is WARP over DX12, so this runs FXC there.
use crate::GPU_LOCK;
#[test]
fn every_compositor_pipeline_builds() {
let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
// FXC first: it is the compiler that rejected the shader, and the lowest shader model wgpu has.
// Every backend then covers the rest of their own translations (Vulkan, Metal, DX12 through DXC).
// `InstanceFlags::DEBUG` (the debug-build default) makes wgpu pass `D3DCOMPILE_DEBUG |
// D3DCOMPILE_SKIP_OPTIMIZATION`, and FXC then accepts what it rejects in a release build, so
// the flags are cleared here: a release build is what ships.
let describe = |backends: wgpu::Backends, compiler: wgpu::Dx12Compiler| {
let mut desc = wgpu::InstanceDescriptor { backends, flags: wgpu::InstanceFlags::empty(), ..wgpu::InstanceDescriptor::new_without_display_handle() };
desc.backend_options.dx12.shader_compiler = compiler;
desc
};
// One instance per backend: `Backends::all()` only enables the others on one instance, and
// `request_adapter` then returns the single default adapter, so the other backends' own
// translation went untested. One adapter per backend keeps a second, unrelated adapter of the
// same backend (a dual-GPU laptop's integrated GPU) out of the run.
for (backends, compiler) in [(wgpu::Backends::DX12, wgpu::Dx12Compiler::Fxc), (wgpu::Backends::all(), wgpu::Dx12Compiler::Auto)] {
for backend in backends.iter() {
let instance = wgpu::Instance::new(describe(backend, compiler.clone()));
// The Windows runners always have a WARP adapter on DX12, so a missing one there means
// the check this test exists for silently did not run. Any other backend is a plain
// skip: `Backends::all()` also names backends this platform does not provide.
let Ok(adapter) = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) else {
#[cfg(windows)]
assert!(backend != wgpu::Backends::DX12, "no adapter on Windows: the DX12/FXC pipeline check did not run");
continue;
};
let Ok((device, _queue)) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())) else {
#[cfg(windows)]
assert!(backend != wgpu::Backends::DX12, "no device on Windows: the DX12/FXC pipeline check did not run");
continue;
};
let info = adapter.get_info();
let format = photocraft_gpu::Compositor::preferred_acc_format(&adapter);
if let Err(e) = photocraft_gpu::Compositor::try_new_with_format(&device, format) {
panic!("{:?} adapter {} (driver {}): {e}", info.backend, info.name, info.driver_info);
}
}
}
}