M13.22: GPU compositing of Advanced 3D runs with blend modes, track mattes (2D and 3D), Preserve Transparency, and environment backgrounds (Classic and Advanced 3D skies)
render: three_d::adv::SplitRun (Renderer::split_adv_run) hands draw_run's split path to an accelerator as data (main scene, special layers far to near, 3D mattes drawn solo); Renderer::sky_draw / three_d::SkyDraw for Environment Light Background layers. gpu: the walk draws skies (adv_sky kernel), resolves each special layer, hides it behind the nearer main scene (occlude kernel) and composites it through the 2D path without readbacks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a909de43b7
commit
0c4e533460
@@ -1606,6 +1606,25 @@ impl<'a> Renderer<'a> {
|
||||
three_d::adv::prepare_run(self, ctx, run, out)
|
||||
}
|
||||
|
||||
/// A run of consecutive 3D layers of an Advanced 3D comp whose layers need the 2D
|
||||
/// compositing path (blend mode, track matte, Preserve Transparency), split for an
|
||||
/// accelerator's compositor ([`three_d::adv::SplitRun`]). `None` = [`Self::prepare_adv_run`]
|
||||
/// covers it, or it isn't an Advanced 3D run.
|
||||
pub fn split_adv_run<'p>(&'p self, ctx: &'p EvalCtx<'a>, run: &'p [&'p Layer], out: (u32, u32)) -> Option<three_d::adv::SplitRun<'p, 'a>> {
|
||||
three_d::adv::split_run(self, ctx, run, out)
|
||||
}
|
||||
|
||||
/// Whether the comp's 3D layers draw with the Advanced 3D renderer.
|
||||
pub fn is_adv3d(&self, ctx: &EvalCtx) -> bool {
|
||||
three_d::adv::active(self, ctx)
|
||||
}
|
||||
|
||||
/// An Environment Light Background layer ready to draw on an accelerator
|
||||
/// ([`three_d::SkyDraw`]; the CPU draws it in `draw_3d_run`). `None` = nothing to draw.
|
||||
pub fn sky_draw(&self, ctx: &EvalCtx, layer: &Layer) -> Option<three_d::SkyDraw> {
|
||||
three_d::compose::sky_draw(self, ctx, layer)
|
||||
}
|
||||
|
||||
/// The pixels a Wireframe-quality layer sets (white, opaque) on a `size` canvas: its
|
||||
/// bounds' outline, one pixel wide. Empty when the layer has no bounds.
|
||||
pub fn wireframe_pixels(&self, ctx: &EvalCtx, layer: &Layer, size: (u32, u32)) -> Vec<(u32, u32)> {
|
||||
|
||||
@@ -7,7 +7,9 @@
|
||||
//! [`crate::Accelerator::raster_3d`], otherwise by the software rasteriser ([`raster`]), at 2×2
|
||||
//! supersampling. The resolve (box filter), depth of field (a gather blur by each pixel's
|
||||
//! circle of confusion, shaped by the camera's iris: [`dof`]) and the conversion back to the working encoding run on the CPU for
|
||||
//! both paths, and the result is composited over the layers below the run.
|
||||
//! both paths, and the result is composited over the layers below the run. An accelerator's
|
||||
//! compositor runs all of it on the device: [`Prepared`] for whole runs, [`SplitRun`] for runs
|
||||
//! with layers on the 2D compositing path (`draw_run`'s steps as data).
|
||||
|
||||
pub mod dof;
|
||||
pub mod raster;
|
||||
@@ -217,7 +219,7 @@ pub(crate) fn render_layers(r: &Renderer, ctx: &EvalCtx, layers: &[&Layer], out:
|
||||
|
||||
/// The run as one prepared scene for an accelerator's compositor when it can be composited
|
||||
/// directly over the canvas (Normal-mode layers without track mattes or Preserve Transparency;
|
||||
/// those go through [`draw_run`]'s 2D path; environment backgrounds draw their sky first). `None`
|
||||
/// those go through `draw_run`'s 2D path; environment backgrounds draw their sky first). `None`
|
||||
/// when the comp isn't Advanced 3D.
|
||||
pub(crate) fn prepare_run<'p, 'a>(r: &'p Renderer<'a>, ctx: &'p EvalCtx<'a>, run: &'p [&'p Layer], out: (u32, u32)) -> Option<Prepared<'p, 'a>> {
|
||||
if !active(r, ctx) || run.iter().any(|l| l.environment_background || needs_2d_composite(ctx, l)) {
|
||||
@@ -226,6 +228,102 @@ pub(crate) fn prepare_run<'p, 'a>(r: &'p Renderer<'a>, ctx: &'p EvalCtx<'a>, run
|
||||
Some(Prepared::new(r, ctx, run, out))
|
||||
}
|
||||
|
||||
/// A run of an Advanced 3D comp whose layers need the 2D compositing path (blend modes, track
|
||||
/// mattes, Preserve Transparency), prepared for an accelerator's compositor: `draw_run`'s
|
||||
/// steps as data. The plain layers render as one scene ([`SplitRun::main`]) composited over the
|
||||
/// canvas; then each special layer, from the farthest to the nearest, renders on its own
|
||||
/// ([`SplitRun::special`]), is hidden where the main scene is nearer, and goes through the 2D
|
||||
/// path with its track matte (a 3D matte layer drawn through the camera:
|
||||
/// [`SplitRun::with_matte3d`]).
|
||||
pub struct SplitRun<'p, 'a> {
|
||||
r: &'p Renderer<'a>,
|
||||
ctx: &'p EvalCtx<'a>,
|
||||
out: (u32, u32),
|
||||
plain: Vec<&'p Layer>,
|
||||
/// Far to near.
|
||||
special: Vec<&'p Layer>,
|
||||
/// Per special layer: its 3D track matte drawn solo (and whether it is active now).
|
||||
mattes: Vec<Option<(Layer, bool)>>,
|
||||
}
|
||||
|
||||
impl<'p, 'a> SplitRun<'p, 'a> {
|
||||
/// The plain layers as one scene.
|
||||
pub fn main(&self) -> Prepared<'_, 'a> {
|
||||
Prepared::new(self.r, self.ctx, &self.plain, self.out)
|
||||
}
|
||||
|
||||
/// Number of special layers.
|
||||
pub fn len(&self) -> usize {
|
||||
self.special.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.special.is_empty()
|
||||
}
|
||||
|
||||
/// Special layer `i` (far to near).
|
||||
pub fn layer(&self, i: usize) -> &'p Layer {
|
||||
self.special[i]
|
||||
}
|
||||
|
||||
/// Special layer `i` rendered on its own.
|
||||
pub fn special(&self, i: usize) -> Prepared<'_, 'a> {
|
||||
Prepared::new(self.r, self.ctx, std::slice::from_ref(&self.special[i]), self.out)
|
||||
}
|
||||
|
||||
/// `f` with special layer `i`'s 3D track matte as a prepared run (`None` when the matte
|
||||
/// layer isn't active: a transparent matte). `None` when the layer has no 3D matte (its
|
||||
/// matte, if any, is placed in 2D as usual).
|
||||
pub fn with_matte3d<R>(&self, i: usize, f: impl FnOnce(Option<&Prepared<'_, 'a>>) -> R) -> Option<R> {
|
||||
let (solo, active) = self.mattes[i].as_ref()?;
|
||||
if !*active {
|
||||
return Some(f(None));
|
||||
}
|
||||
let layers = [solo];
|
||||
let p = Prepared::new(self.r, self.ctx, &layers, self.out);
|
||||
Some(f(Some(&p)))
|
||||
}
|
||||
}
|
||||
|
||||
/// The run split for an accelerator when some layer needs the 2D compositing path (`None` when
|
||||
/// the comp isn't Advanced 3D, a layer is an environment background, or no layer needs it:
|
||||
/// `prepare_run` then covers the run).
|
||||
pub(crate) fn split_run<'p, 'a>(r: &'p Renderer<'a>, ctx: &'p EvalCtx<'a>, run: &'p [&'p Layer], out: (u32, u32)) -> Option<SplitRun<'p, 'a>> {
|
||||
if !active(r, ctx) || run.iter().any(|l| l.environment_background) || !run.iter().any(|l| needs_2d_composite(ctx, l)) {
|
||||
return None;
|
||||
}
|
||||
let (special, plain): (Vec<&Layer>, Vec<&Layer>) = run.iter().copied().partition(|l| needs_2d_composite(ctx, l));
|
||||
let special = far_to_near(r, ctx, special);
|
||||
let mattes = special.iter().map(|l| solo_matte(ctx, l)).collect();
|
||||
Some(SplitRun { r, ctx, out, plain, special, mattes })
|
||||
}
|
||||
|
||||
/// Special layers sorted by their anchor's camera depth, farthest first.
|
||||
fn far_to_near<'p>(r: &Renderer, ctx: &EvalCtx, special: Vec<&'p Layer>) -> Vec<&'p Layer> {
|
||||
let cam = camera_for(r, ctx);
|
||||
let mut order: Vec<(f64, &Layer)> = special
|
||||
.into_iter()
|
||||
.map(|l| {
|
||||
let anchor = l.transform().map_or([0.0; 3], |tr| ctx.v3(l, tr, "anchor", [0.0; 3]));
|
||||
(cam.depth(ctx.world_matrix(l).apply(effectcraft_geom::Vec3::from(anchor))), l)
|
||||
})
|
||||
.collect();
|
||||
order.sort_by(|a, b| b.0.total_cmp(&a.0));
|
||||
order.into_iter().map(|(_, l)| l).collect()
|
||||
}
|
||||
|
||||
/// A layer's 3D track matte, drawn through the camera on its own (no matte, Normal mode, no
|
||||
/// Preserve Transparency, visible), and whether it is active now.
|
||||
fn solo_matte(ctx: &EvalCtx, l: &Layer) -> Option<(Layer, bool)> {
|
||||
let m = l.track_matte.and_then(|tm| ctx.comp.layer(tm.layer).filter(|m| m.id != l.id && m.is_3d()))?;
|
||||
let mut solo = m.clone();
|
||||
solo.track_matte = None;
|
||||
solo.blend_mode = effectcraft_color::BlendMode::Normal;
|
||||
solo.preserve_transparency = false;
|
||||
solo.switches.video = true;
|
||||
Some((solo, m.is_active_at(ctx.time)))
|
||||
}
|
||||
|
||||
/// Layers whose blend mode, track matte or Preserve Transparency needs the 2D compositing path.
|
||||
fn needs_2d_composite(ctx: &EvalCtx, l: &Layer) -> bool {
|
||||
l.blend_mode != effectcraft_color::BlendMode::Normal
|
||||
@@ -252,16 +350,7 @@ pub(crate) fn draw_run(r: &Renderer, ctx: &EvalCtx, run: &[&Layer], canvas: &mut
|
||||
if special.is_empty() {
|
||||
return;
|
||||
}
|
||||
let cam = camera_for(r, ctx);
|
||||
let mut order: Vec<(f64, &Layer)> = special
|
||||
.into_iter()
|
||||
.map(|l| {
|
||||
let anchor = l.transform().map_or([0.0; 3], |tr| ctx.v3(l, tr, "anchor", [0.0; 3]));
|
||||
(cam.depth(ctx.world_matrix(l).apply(effectcraft_geom::Vec3::from(anchor))), l)
|
||||
})
|
||||
.collect();
|
||||
order.sort_by(|a, b| b.0.total_cmp(&a.0));
|
||||
for (_, l) in order {
|
||||
for l in far_to_near(r, ctx, special) {
|
||||
let Some((mut iso, depth)) = render_layers(r, ctx, &[l], out) else { continue };
|
||||
if let Some((m, md)) = &main {
|
||||
iso.data.par_iter_mut().zip(depth.par_iter()).zip(m.data.par_iter().zip(md.par_iter())).for_each(|((p, z), (q, mz))| {
|
||||
@@ -274,17 +363,8 @@ pub(crate) fn draw_run(r: &Renderer, ctx: &EvalCtx, run: &[&Layer], canvas: &mut
|
||||
});
|
||||
}
|
||||
// A 3D matte layer is drawn through the camera too.
|
||||
let matte3d = l.track_matte.and_then(|tm| ctx.comp.layer(tm.layer).filter(|m| m.id != l.id && m.is_3d())).map(|m| {
|
||||
let mut solo = m.clone();
|
||||
solo.track_matte = None;
|
||||
solo.blend_mode = effectcraft_color::BlendMode::Normal;
|
||||
solo.preserve_transparency = false;
|
||||
solo.switches.video = true;
|
||||
if m.is_active_at(ctx.time) {
|
||||
render_layers(r, ctx, &[&solo], out).map(|(i, _)| i).unwrap_or_else(|| Image::new(out.0, out.1))
|
||||
} else {
|
||||
Image::new(out.0, out.1)
|
||||
}
|
||||
let matte3d = solo_matte(ctx, l).map(|(solo, active)| {
|
||||
if active { render_layers(r, ctx, &[&solo], out).map(|(i, _)| i).unwrap_or_else(|| Image::new(out.0, out.1)) } else { Image::new(out.0, out.1) }
|
||||
});
|
||||
r.composite_iso_with(ctx, l, iso, canvas, 1.0, matte3d);
|
||||
}
|
||||
|
||||
@@ -971,30 +971,71 @@ pub(crate) fn environment_rotation(ctx: &EvalCtx) -> f32 {
|
||||
.to_radians() as f32
|
||||
}
|
||||
|
||||
/// Draw an Environment Light Background layer: its (equirectangular) image looked up by each
|
||||
/// pixel's view direction through the camera, as an infinitely distant backdrop.
|
||||
pub(crate) fn draw_sky(r: &Renderer, ctx: &EvalCtx, layer: &Layer, canvas: &mut Image) {
|
||||
let Some(buf) = r.blend_layer_buf(ctx, layer) else { return };
|
||||
let img = &buf.img;
|
||||
if img.width == 0 || img.height == 0 {
|
||||
return;
|
||||
/// An Environment Light Background layer ready to draw (see [`draw_sky`]): the layer's
|
||||
/// buffer in the blending space, its opacity and the camera's view rays. Also handed to an
|
||||
/// accelerator's compositor (`Renderer::sky_draw`).
|
||||
#[derive(Clone)]
|
||||
pub struct SkyDraw {
|
||||
pub buf: Arc<Buf>,
|
||||
pub opacity: f32,
|
||||
/// Camera basis: forward, right, down.
|
||||
pub fwd: [f64; 3],
|
||||
pub right: [f64; 3],
|
||||
pub down: [f64; 3],
|
||||
pub zoom: f64,
|
||||
pub ortho: bool,
|
||||
/// View size (comp pixels), region-of-interest offset (output pixels), output scale.
|
||||
pub view: (f64, f64),
|
||||
pub roi: (f64, f64),
|
||||
pub scale: f64,
|
||||
/// The Environment light's rotation (radians).
|
||||
pub rotation: f32,
|
||||
}
|
||||
|
||||
pub(crate) fn sky_draw(r: &Renderer, ctx: &EvalCtx, layer: &Layer) -> Option<SkyDraw> {
|
||||
let buf = r.blend_layer_buf(ctx, layer)?;
|
||||
if buf.img.width == 0 || buf.img.height == 0 {
|
||||
return None;
|
||||
}
|
||||
let op = (ctx.opacity(layer) as f32 * r.opacity_mul()).clamp(0.0, 1.0);
|
||||
if op <= 0.0 {
|
||||
return;
|
||||
return None;
|
||||
}
|
||||
let cam = camera_for(r, ctx);
|
||||
let (fwd, right, down) = (cam.forward(), cam.right(), cam.down());
|
||||
let s = r.opts.scale.max(1e-9);
|
||||
let (cw, ch) = view_size(r, ctx);
|
||||
let (ox, oy) = r.roi_offset().map_or((0.0, 0.0), |(x, y, _, _)| (x, y));
|
||||
let rot = environment_rotation(ctx);
|
||||
let v3 = |v: Vec3| [v.x, v.y, v.z];
|
||||
Some(SkyDraw {
|
||||
buf,
|
||||
opacity: op,
|
||||
fwd: v3(cam.forward()),
|
||||
right: v3(cam.right()),
|
||||
down: v3(cam.down()),
|
||||
zoom: cam.zoom,
|
||||
ortho: cam.ortho,
|
||||
view: view_size(r, ctx),
|
||||
roi: r.roi_offset().map_or((0.0, 0.0), |(x, y, _, _)| (x, y)),
|
||||
scale: r.opts.scale.max(1e-9),
|
||||
rotation: environment_rotation(ctx),
|
||||
})
|
||||
}
|
||||
|
||||
/// Draw an Environment Light Background layer: its (equirectangular) image looked up by each
|
||||
/// pixel's view direction through the camera, as an infinitely distant backdrop.
|
||||
pub(crate) fn draw_sky(r: &Renderer, ctx: &EvalCtx, layer: &Layer, canvas: &mut Image) {
|
||||
let Some(sky) = sky_draw(r, ctx, layer) else { return };
|
||||
let img = &sky.buf.img;
|
||||
let op = sky.opacity;
|
||||
let v3 = |v: [f64; 3]| vec3(v[0], v[1], v[2]);
|
||||
let (fwd, right, down) = (v3(sky.fwd), v3(sky.right), v3(sky.down));
|
||||
let s = sky.scale;
|
||||
let (cw, ch) = sky.view;
|
||||
let (ox, oy) = sky.roi;
|
||||
let rot = sky.rotation;
|
||||
let (iw, ih) = (img.width as f64, img.height as f64);
|
||||
canvas.rows_mut().for_each(|(y, row)| {
|
||||
let cy = (y as f64 + 0.5 + oy) / s;
|
||||
for (x, d) in row.iter_mut().enumerate() {
|
||||
let cx = (x as f64 + 0.5 + ox) / s;
|
||||
let dir = if cam.ortho { fwd } else { (fwd * cam.zoom + right * (cx - cw / 2.0) + down * (cy - ch / 2.0)).normalize() };
|
||||
let dir = if sky.ortho { fwd } else { (fwd * sky.zoom + right * (cx - cw / 2.0) + down * (cy - ch / 2.0)).normalize() };
|
||||
let (u, v) = super::adv::shade::equirect_uv([dir.x as f32, dir.y as f32, dir.z as f32], rot);
|
||||
// Wrap horizontally across the seam.
|
||||
let sx = (u as f64 * iw).clamp(0.5, iw - 0.5);
|
||||
|
||||
@@ -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, PlaneDof, Run3d};
|
||||
pub use compose::{Geo as PlaneGeo, Plane3d, PlaneDof, Run3d, SkyDraw};
|
||||
pub use light::{LightState, Material, lights_at};
|
||||
|
||||
use effectcraft_geom::Mat3;
|
||||
|
||||
Reference in New Issue
Block a user