mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
Merge pull request #4353 from dotCooCoo/fix/raster3d-resolve-large-blocks
craft-raster3d: average resolve blocks of more than 256 subsamples in f64
This commit is contained in:
2 files changed
+30
-7
No files matched your search
@@ -86,7 +86,7 @@ Building a scene from an app's document (layers, cameras, models, text) stays in
|
||||
- **Resolve and encoding.** `try_resolve(&target, k)` averages each `k × k` block of premultiplied
|
||||
pixels with equal weights, and takes the mean depth of the block's covered pixels, infinity where
|
||||
none is covered; a pixel whose `f32` sums overflow, and every pixel when a block holds more than
|
||||
2^24 subsamples, is averaged in `f64`. `try_encode(&mut image)` divides each color by alpha, applies the sRGB transfer
|
||||
256 subsamples (`k` over 16), is averaged in `f64`. `try_encode(&mut image)` divides each color by alpha, applies the sRGB transfer
|
||||
function of CSS Color 4 (<https://www.w3.org/TR/css-color-4/#color-conversion-code>), a linear
|
||||
segment up to a magnitude of 0.0031308 and a 1/2.4 power above it, with a negative color keeping
|
||||
its sign, multiplies by alpha again and leaves alpha linear. `srgb_to_linear` decodes a negative
|
||||
@@ -140,7 +140,7 @@ far plane, where depth-range crossings lie within 1/4096 of an edge from its ins
|
||||
texture coordinates times the texture's width or height reach 2^23 in magnitude, where normals'
|
||||
squared lengths, squared distances from fragments to point and spot lights, or casters'
|
||||
light-space coordinates times a map's focal length overflow `f32`, where shadow-casting spot
|
||||
lights' outer cones are over 1.40 radians wide, where resolve blocks hold more than 2^24
|
||||
lights' outer cones are over 1.40 radians wide, where resolve blocks hold more than 256
|
||||
subsamples, where shadows-only materials do not receive shadows, where fragments under discarded
|
||||
opaque pixels leave the depth range, where blended casters have no base texture, where
|
||||
opaque-mode and masked casters have an opacity below 0.5, where casters reach within 1 unit of
|
||||
@@ -198,8 +198,10 @@ Changes from those files:
|
||||
- A spot light whose outer cone's half angle plus 0.05 radians is over 1.45 radians gets six maps,
|
||||
as a point light does. EffectCraft gave it one map 1.45 radians wide, so receivers in the rest
|
||||
of the cone were not shadowed.
|
||||
- A resolve block of more than 2^24 subsamples is averaged in `f64`. EffectCraft's `f32` count of
|
||||
covered subsamples stopped at 2^24, which raised the mean depth.
|
||||
- A resolve block of more than 256 subsamples is averaged in `f64`. EffectCraft summed every block
|
||||
in `f32`, which dropped the low bits of each sample as the sum grew (a 1024 × 1024 block of 0.7
|
||||
resolved to 0.6927), and its `f32` count of covered subsamples stopped at 2^24, which raised the
|
||||
mean depth.
|
||||
- A clipped corner whose `1/w` overflows `f32` (a clip transform scaled near `f32`'s smallest
|
||||
values) is projected in `f64` instead of dropping its triangle, and a drawn pixel whose camera
|
||||
depth overflows is an error.
|
||||
|
||||
@@ -18,6 +18,9 @@ pub struct Image {
|
||||
/// Alpha above 1 that [`try_encode`] accepts as rounding from blending.
|
||||
const ALPHA_SLACK: f32 = 1e-4;
|
||||
|
||||
/// The largest block [`try_resolve`] sums in `f32`.
|
||||
const F32_SAMPLES: u64 = 256;
|
||||
|
||||
/// `w × h` as a length, when it fits `usize`.
|
||||
fn area(w: u32, h: u32) -> Option<usize> {
|
||||
usize::try_from(u64::from(w) * u64::from(h)).ok()
|
||||
@@ -26,7 +29,7 @@ fn area(w: u32, h: u32) -> Option<usize> {
|
||||
/// Box-filter a supersampled target down by `k` per side: each output pixel is the mean of its
|
||||
/// `k × k` premultiplied colors, and its depth is the mean of the covered subsamples' depths
|
||||
/// (infinity where none is covered). A pixel whose `f32` sums overflow, and every pixel when a
|
||||
/// block holds more than 2^24 subsamples, is averaged in `f64`.
|
||||
/// block holds more than 256 subsamples (`k` over 16), is averaged in `f64`.
|
||||
///
|
||||
/// Returns [`RenderError::BadFactor`] when `k` is zero or does not divide both sides, and
|
||||
/// [`RenderError::BadDataLength`] when the color or depth buffer does not hold width × height
|
||||
@@ -50,8 +53,9 @@ pub fn try_resolve(t: &Target, k: u32) -> Result<(Image, Vec<f32>), RenderError>
|
||||
return Ok((img, depth));
|
||||
}
|
||||
let n = (u64::from(k) * u64::from(k)) as f32;
|
||||
// An f32 count stops at 2^24, so a larger block is averaged in f64.
|
||||
let wide = u64::from(k) * u64::from(k) > 1 << 24;
|
||||
// An f32 sum drops the low bits of each sample as it grows, so a block of more than
|
||||
// `F32_SAMPLES` subsamples is averaged in f64.
|
||||
let wide = u64::from(k) * u64::from(k) > F32_SAMPLES;
|
||||
img.data.par_chunks_mut(w as usize).zip(depth.par_chunks_mut(w as usize)).enumerate().for_each(|(y, (row, drow))| {
|
||||
for x in 0..w as usize {
|
||||
if wide {
|
||||
@@ -158,3 +162,20 @@ pub fn try_render_cpu(s: &Scene, limits: &Limits) -> Result<(Image, Vec<f32>), R
|
||||
}
|
||||
try_resolve(&try_render(s, limits)?, k)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Target, try_resolve};
|
||||
|
||||
#[test]
|
||||
fn a_large_block_of_one_color_resolves_to_that_color() {
|
||||
// An f32 sum of 17 × 17 samples of 0.7 drifts past the tolerance; 17 is the first factor
|
||||
// averaged in f64.
|
||||
let n = 17u32;
|
||||
let len = (n * n) as usize;
|
||||
let t = Target { width: n, height: n, color: vec![[0.7; 4]; len], depth: vec![0.5; len] };
|
||||
let (img, depth) = try_resolve(&t, n).unwrap_or_else(|e| panic!("{e:?}"));
|
||||
assert!(img.data[0].iter().all(|&c| (c - 0.7).abs() < 1e-6), "{:?}", img.data[0]);
|
||||
assert!((depth[0] - 0.5).abs() < 1e-6, "{}", depth[0]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user