Merge main into M13.11 (bokeh kernel_spans/blur_levels, Essential Graphics as_type controls with mirrors and links)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -190,32 +190,15 @@ pub fn boost_highlights(img: &mut Image, h: &Highlight) {
|
||||
});
|
||||
}
|
||||
|
||||
/// Gather blur of a premultiplied image with a bokeh kernel of radius `r` (pixels). Pixels
|
||||
/// beyond the image are transparent.
|
||||
pub fn bokeh_blur(img: &Image, iris: &Iris, r: f64) -> Image {
|
||||
if r < 0.3 {
|
||||
return img.clone();
|
||||
}
|
||||
if iris.sides == 0 {
|
||||
// Fast Rectangle: a separable box of the iris' width and height.
|
||||
let a = iris.aspect.clamp(0.01, 100.0).sqrt();
|
||||
let rx = (r * a).round().max(0.0) as usize;
|
||||
let ry = (r / a).round().max(0.0) as usize;
|
||||
return effectcraft_raster::box_blur(img, rx, ry, 1, false);
|
||||
}
|
||||
match kernel(iris, r) {
|
||||
Some((kernels, norm)) => span_gather(img, &kernels, norm),
|
||||
None => img.clone(),
|
||||
}
|
||||
}
|
||||
/// Weighted row-span kernels and their normalisation.
|
||||
pub type Kernels = (Vec<(f32, Vec<(i32, f64, f64)>)>, f32);
|
||||
|
||||
/// Weighted row-span kernels (see [`Iris::spans`]).
|
||||
pub type SpanKernels = Vec<(f32, Vec<(i32, f64, f64)>)>;
|
||||
|
||||
/// [`bokeh_blur`]'s gather kernel at radius `r` as weighted row spans and their normaliser
|
||||
/// (accelerators run the same gather). Fast Rectangle is the box of the iris' width and height
|
||||
/// (what [`bokeh_blur`]'s separable box computes). `None` = the identity (no blur).
|
||||
pub fn kernel(iris: &Iris, r: f64) -> Option<(SpanKernels, f32)> {
|
||||
/// The bokeh kernel of radius `r` as weighted row spans (see [`Iris::spans`]) and the sum of
|
||||
/// weights; `None` when the blur does nothing (radius below 0.3 px). Fast Rectangle is the box
|
||||
/// of the iris' width and height (one span per row covering whole pixels); the diffraction
|
||||
/// fringe adds the rim as the full iris minus a shrunken one. The GPU gathers with exactly these
|
||||
/// spans.
|
||||
pub fn kernel_spans(iris: &Iris, r: f64) -> Option<Kernels> {
|
||||
if r < 0.3 {
|
||||
return None;
|
||||
}
|
||||
@@ -229,8 +212,6 @@ pub fn kernel(iris: &Iris, r: f64) -> Option<(SpanKernels, f32)> {
|
||||
let spans = (-ry..=ry).map(|dy| (dy, -rx as f64 - 0.5, rx as f64 + 0.5)).collect();
|
||||
return Some((vec![(1.0, spans)], ((2 * rx + 1) * (2 * ry + 1)) as f32));
|
||||
}
|
||||
// The iris as row spans (prefix-sum gather: cost ∝ kernel height, not area). The
|
||||
// diffraction fringe adds the rim: the full iris minus a shrunken one.
|
||||
let outer = iris.spans(r);
|
||||
let area = |sp: &[(i32, f64, f64)]| sp.iter().map(|s| (s.2 - s.1) as f32).sum::<f32>();
|
||||
let mut kernels = vec![(1.0f32, outer)];
|
||||
@@ -245,10 +226,30 @@ pub fn kernel(iris: &Iris, r: f64) -> Option<(SpanKernels, f32)> {
|
||||
(norm > 0.0).then_some((kernels, norm))
|
||||
}
|
||||
|
||||
/// [`progressive_blur`]'s blur levels for a radius range: `None` = no blur at all; one level
|
||||
/// when the range is narrow (every pixel takes it); else up to eight spanning `lo..=hi`
|
||||
/// (each pixel blends the two around its own radius).
|
||||
pub fn progressive_levels(lo: f32, hi: f32) -> Option<Vec<f32>> {
|
||||
/// Gather blur of a premultiplied image with a bokeh kernel of radius `r` (pixels). Pixels
|
||||
/// beyond the image are transparent.
|
||||
pub fn bokeh_blur(img: &Image, iris: &Iris, r: f64) -> Image {
|
||||
if r < 0.3 {
|
||||
return img.clone();
|
||||
}
|
||||
if iris.sides == 0 {
|
||||
// Fast Rectangle: a separable box of the iris' width and height (the same as its spans).
|
||||
let a = iris.aspect.clamp(0.01, 100.0).sqrt();
|
||||
let rx = (r * a).round().max(0.0) as usize;
|
||||
let ry = (r / a).round().max(0.0) as usize;
|
||||
return effectcraft_raster::box_blur(img, rx, ry, 1, false);
|
||||
}
|
||||
// The iris as row spans (prefix-sum gather: cost ∝ kernel height, not area).
|
||||
match kernel_spans(iris, r) {
|
||||
Some((kernels, norm)) => span_gather(img, &kernels, norm),
|
||||
None => img.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The blur levels of a depth-varying blur ([`progressive_blur`]): `None` when nothing blurs,
|
||||
/// one level for a near-constant radius, else up to eight radii spanning the range.
|
||||
pub fn blur_levels(radius: &[f32]) -> Option<Vec<f32>> {
|
||||
let (lo, hi) = radius.iter().fold((f32::INFINITY, 0.0f32), |(a, b), &r| (a.min(r), b.max(r)));
|
||||
if !lo.is_finite() || hi < 0.3 {
|
||||
return None;
|
||||
}
|
||||
@@ -259,16 +260,35 @@ pub fn progressive_levels(lo: f32, hi: f32) -> Option<Vec<f32>> {
|
||||
Some((0..n).map(|i| lo + (hi - lo) * i as f32 / (n - 1) as f32).collect())
|
||||
}
|
||||
|
||||
/// How much level `k` of `levels` contributes at radius `r` (the two levels bracketing `r`
|
||||
/// are blended linearly).
|
||||
pub fn level_weight(levels: &[f32], k: usize, r: f32) -> f32 {
|
||||
let n = levels.len();
|
||||
if n <= 1 {
|
||||
return 1.0;
|
||||
}
|
||||
let (lo, hi) = (levels[0], levels[n - 1]);
|
||||
let f = ((r - lo) / (hi - lo) * (n - 1) as f32).clamp(0.0, (n - 1) as f32);
|
||||
let kk = (f.floor() as usize).min(n - 2);
|
||||
let t = f - kk as f32;
|
||||
if k == kk {
|
||||
1.0 - t
|
||||
} else if k == kk + 1 {
|
||||
t
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Depth-varying blur: `radius` gives each pixel's blur radius. The image is blurred at up to
|
||||
/// eight radii spanning the range and each pixel interpolates between the two levels around
|
||||
/// its own radius.
|
||||
pub fn progressive_blur(img: &Image, iris: &Iris, radius: &[f32]) -> Image {
|
||||
let (lo, hi) = radius.iter().fold((f32::INFINITY, 0.0f32), |(a, b), &r| (a.min(r), b.max(r)));
|
||||
let Some(levels) = progressive_levels(lo, hi) else { return img.clone() };
|
||||
let n = levels.len();
|
||||
if n == 1 {
|
||||
let Some(levels) = blur_levels(radius) else { return img.clone() };
|
||||
if levels.len() == 1 {
|
||||
return bokeh_blur(img, iris, levels[0] as f64);
|
||||
}
|
||||
let (lo, hi, n) = (levels[0], levels[levels.len() - 1], levels.len());
|
||||
let blurred: Vec<Image> = levels.par_iter().map(|&r| bokeh_blur(img, iris, r as f64)).collect();
|
||||
let mut out = Image::new(img.width, img.height);
|
||||
out.data.par_iter_mut().enumerate().for_each(|(i, o)| {
|
||||
|
||||
@@ -62,6 +62,21 @@ pub(crate) struct Item<'a> {
|
||||
winv: Mat4,
|
||||
bounds: [f64; 4],
|
||||
bbox: [i64; 4],
|
||||
/// Depth of field left to an accelerator (`buf` is then unblurred and unpadded).
|
||||
dof: Option<PlaneDof>,
|
||||
}
|
||||
|
||||
/// Depth of field of a plane for an accelerator to apply ([`Plane3d::dof`]): pad the buffer by
|
||||
/// `pad` transparent pixels, boost its highlights, then blur it at each of `levels` with
|
||||
/// [`super::bokeh::kernel_spans`] and blend the levels by each pixel's `radius` (padded buffer
|
||||
/// pixels, row-major) with [`super::bokeh::level_weight`] — what the CPU's depth of field does.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PlaneDof {
|
||||
pub pad: u32,
|
||||
pub iris: super::bokeh::Iris,
|
||||
pub highlight: super::bokeh::Highlight,
|
||||
pub radius: Arc<Vec<f32>>,
|
||||
pub levels: Vec<f32>,
|
||||
}
|
||||
|
||||
impl Item<'_> {
|
||||
@@ -218,10 +233,20 @@ fn prepare_with<'a>(
|
||||
let world = outer * ctx.world_matrix(layer) * local;
|
||||
// Depth of field: a bokeh blur by each buffer pixel's circle of confusion (constant for a
|
||||
// layer facing the camera, progressive for a tilted one).
|
||||
let mut plane_dof = None;
|
||||
let mut bounds = buf_bounds(&buf);
|
||||
if let Some(dof) = cam.dof.filter(|_| !ctx.comp.draft_3d && !r.opts.draft) {
|
||||
buf = dof_blur(&cam, &dof, world, buf);
|
||||
if r.defer_dof {
|
||||
// An accelerator blurs: hand it the radius map of the padded buffer.
|
||||
if let Some((pd, padded_bounds)) = dof_plan(&cam, &dof, world, &buf) {
|
||||
plane_dof = Some(pd);
|
||||
bounds = padded_bounds;
|
||||
}
|
||||
} else {
|
||||
buf = dof_blur(&cam, &dof, world, buf);
|
||||
bounds = buf_bounds(&buf);
|
||||
}
|
||||
}
|
||||
let bounds = buf_bounds(&buf);
|
||||
let n = if in_run { mb_samples(r, ctx, layer) } else { 1 };
|
||||
let geos: Vec<Geo> = if n <= 1 {
|
||||
geo(&cam, world, bounds, comp, s, out, r.out_offset()).into_iter().collect()
|
||||
@@ -260,16 +285,20 @@ fn prepare_with<'a>(
|
||||
winv: world.inverse().unwrap_or(Mat4::IDENTITY),
|
||||
bounds,
|
||||
bbox,
|
||||
dof: plane_dof,
|
||||
})
|
||||
}
|
||||
|
||||
/// Blur radius (buffer pixels) of every buffer pixel of a plane at `world` (layer → world), from
|
||||
/// its camera depth: the circle of confusion over the on-screen size of one layer pixel there.
|
||||
fn dof_radii(cam: &CameraState, dof: &super::camera::Dof, world: Mat4, buf: &Buf) -> Vec<f32> {
|
||||
let s = buf.scale.max(1e-9);
|
||||
dof_radii_at(cam, dof, world, (buf.img.width as usize, buf.img.height as usize), buf.offset, buf.scale)
|
||||
}
|
||||
|
||||
fn dof_radii_at(cam: &CameraState, dof: &super::camera::Dof, world: Mat4, (w, h): (usize, usize), offset: [f64; 2], scale: f64) -> Vec<f32> {
|
||||
let s = scale.max(1e-9);
|
||||
let sx = world.apply_vec(vec3(1.0, 0.0, 0.0)).length().max(1e-9);
|
||||
let (w, h) = (buf.img.width as usize, buf.img.height as usize);
|
||||
let at = |x: f64, y: f64| cam.depth(world.apply(vec3((x - buf.offset[0]) / s, (y - buf.offset[1]) / s, 0.0)));
|
||||
let at = |x: f64, y: f64| cam.depth(world.apply(vec3((x - offset[0]) / s, (y - offset[1]) / s, 0.0)));
|
||||
// Camera depth is affine across a plane.
|
||||
let z0 = at(0.5, 0.5);
|
||||
let (zx, zy) = (at(1.5, 0.5) - z0, at(0.5, 1.5) - z0);
|
||||
@@ -287,6 +316,30 @@ fn dof_radii(cam: &CameraState, dof: &super::camera::Dof, world: Mat4, buf: &Buf
|
||||
out
|
||||
}
|
||||
|
||||
/// The padding [`dof_blur`] gives a buffer whose largest blur radius is `rmax`.
|
||||
fn dof_pad(dof: &super::camera::Dof, rmax: f64) -> u32 {
|
||||
let a = dof.iris.aspect.clamp(0.01, 100.0).sqrt().max(1.0 / dof.iris.aspect.clamp(0.01, 100.0).sqrt());
|
||||
(rmax * a).ceil() as u32 + 2
|
||||
}
|
||||
|
||||
/// [`dof_blur`] planned for an accelerator: the padding, radius map and levels, and the padded
|
||||
/// buffer's layer-space bounds. `None` when nothing blurs.
|
||||
fn dof_plan(cam: &CameraState, dof: &super::camera::Dof, world: Mat4, buf: &Buf) -> Option<(PlaneDof, [f64; 4])> {
|
||||
let radii = dof_radii(cam, dof, world, buf);
|
||||
let rmax = radii.iter().copied().fold(0.0f32, f32::max) as f64;
|
||||
if rmax <= 0.3 {
|
||||
return None;
|
||||
}
|
||||
let pad = dof_pad(dof, rmax);
|
||||
let (w, h) = (buf.img.width as usize + 2 * pad as usize, buf.img.height as usize + 2 * pad as usize);
|
||||
let offset = [buf.offset[0] + pad as f64, buf.offset[1] + pad as f64];
|
||||
let radius = dof_radii_at(cam, dof, world, (w, h), offset, buf.scale);
|
||||
let levels = super::bokeh::blur_levels(&radius)?;
|
||||
let s = buf.scale.max(1e-9);
|
||||
let bounds = [-offset[0] / s, -offset[1] / s, (w as f64 - offset[0]) / s, (h as f64 - offset[1]) / s];
|
||||
Some((PlaneDof { pad, iris: dof.iris, highlight: dof.highlight, radius: Arc::new(radius), levels }, bounds))
|
||||
}
|
||||
|
||||
/// Depth of field for one plane: highlights boosted, then a bokeh-shaped blur by depth.
|
||||
fn dof_blur(cam: &CameraState, dof: &super::camera::Dof, world: Mat4, buf: Arc<Buf>) -> Arc<Buf> {
|
||||
let radii = dof_radii(cam, dof, world, &buf);
|
||||
@@ -294,8 +347,7 @@ fn dof_blur(cam: &CameraState, dof: &super::camera::Dof, world: Mat4, buf: Arc<B
|
||||
if rmax <= 0.3 {
|
||||
return buf;
|
||||
}
|
||||
let a = dof.iris.aspect.clamp(0.01, 100.0).sqrt().max(1.0 / dof.iris.aspect.clamp(0.01, 100.0).sqrt());
|
||||
let pad = (rmax * a).ceil() as u32 + 2;
|
||||
let pad = dof_pad(dof, rmax);
|
||||
let mut padded = buf.img.padded(pad);
|
||||
super::bokeh::boost_highlights(&mut padded, &dof.highlight);
|
||||
let pbuf = Buf { img: padded, offset: [buf.offset[0] + pad as f64, buf.offset[1] + pad as f64], scale: buf.scale };
|
||||
@@ -666,8 +718,11 @@ pub struct Run3d {
|
||||
/// One plane of a [`Run3d`].
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Plane3d {
|
||||
/// The layer buffer (blending space; depth of field already applied).
|
||||
/// The layer buffer (blending space; depth of field already applied unless `dof` is set).
|
||||
pub buf: Arc<Buf>,
|
||||
/// Depth of field for the accelerator to apply to `buf` (`bounds` are then the padded
|
||||
/// buffer's).
|
||||
pub dof: Option<PlaneDof>,
|
||||
pub bicubic: bool,
|
||||
/// One per motion-blur sub-sample (equal weights).
|
||||
pub geos: Vec<Geo>,
|
||||
@@ -697,7 +752,9 @@ pub(crate) fn gpu_run(r: &Renderer, ctx: &EvalCtx, run: &[&Layer], out: (u32, u3
|
||||
if super::adv::active(r, ctx) {
|
||||
return None;
|
||||
}
|
||||
let g = gather(r, ctx, run, out);
|
||||
// Depth of field is left to the accelerator (its blur runs where the planes are drawn).
|
||||
let rd = Renderer { defer_dof: true, ..*r };
|
||||
let g = gather(&rd, ctx, run, out);
|
||||
let planes: Vec<Plane3d> = g
|
||||
.items
|
||||
.into_iter()
|
||||
@@ -706,6 +763,7 @@ pub(crate) fn gpu_run(r: &Renderer, ctx: &EvalCtx, run: &[&Layer], out: (u32, u3
|
||||
layer_id: it.layer.id.0 as u32,
|
||||
mode: it.layer.blend_mode,
|
||||
buf: it.buf,
|
||||
dof: it.dof,
|
||||
bicubic: it.bicubic,
|
||||
geos: it.geos,
|
||||
mat: it.mat,
|
||||
|
||||
@@ -17,7 +17,7 @@ mod tests;
|
||||
mod tests_orient;
|
||||
|
||||
pub use camera::{CameraState, Dof, Rig, View3D, ViewCam, Views3D, active_camera, default_camera, default_view_cam};
|
||||
pub use compose::{Geo as PlaneGeo, Plane3d, Run3d};
|
||||
pub use compose::{Geo as PlaneGeo, Plane3d, PlaneDof, Run3d};
|
||||
pub use light::{LightState, Material, lights_at};
|
||||
|
||||
use effectcraft_geom::Mat3;
|
||||
|
||||
Reference in New Issue
Block a user