//! A stand-in for the indirect light the title traces rays for. //! //! With the ray-tracing structure builder off (`crate::pipeline::builder_runs`) //! the title's rays find nothing. Its program that resolves the diffuse light //! of its screen probes (`CS_ScreenProbes_TemporalResolve_*`) then leaves the //! sky's light outdoors, nothing indoors (walls are black where no lamp or //! sun reaches) and, in blocks of the picture's tiles, what the probes' //! memory held. Right after that program the light is raised to an estimate //! and bounded by a multiple of it. //! //! That a stand-in can be put there, and in which of the program's images, //! is beloooo's find (VIctory-Belobug-AMD, 06031237, 4f45ab88, cd9f78ce, //! GPL-3.0-or-later), which puts a constant there, shaded in creases from the //! scene's depth. The estimate here is worked out from the frame instead, so //! it follows the hour, the room and its lamps by itself: the light the //! visible surfaces send out (the title keeps it for its own screen traces: //! `CS_CacheScreenSpaceRadiance`, the frame before) is averaged in 64x36 //! cells, and every point gets the mean of the cells weighted by the share of //! its surroundings they cover, as a directional value (first-order harmonics //! at 160x90 points, taken between them by depth). //! //! What it assumes: what is not on screen sends out what is (with a slow //! mean of the last seconds, so that turning into a dark corner is not black //! at once). No occlusion but the creases'. //! //! `VICTORY_AMBIENT`: //! - unset or `0`/`off`: the title's own output. //! - a number: that share of the estimate (1: all of it). //! - `constant:`: the constant, as beloooo's fork has it. //! //! `VICTORY_AMBIENT_SHADE=,` sets the creases. //! `VICTORY_AMBIENT_ONCE=1`: one bounce of the title's own light, without the //! stand-in's light coming back to it ([`once`]). //! //! The air of a room is lit by the same probes: without rays they hold the //! sky's light indoors too, a brown veil at sunset. `VICTORY_AMBIENT_AIR` //! bounds what the air scatters by a multiple of the light around. //! //! Reflections are traced too, at half the picture's size: rays the //! title's screen-space march resolves (`CS_ResolveScreenSpace`) take the //! light seen on screen; the others go to the structures, find nothing, and //! `CS_RayMiss` gives each the sky's light from the sky's cube maps (in a //! cutscene frame, capture haze2: 10 to 30 against 0.01 to 0.1 for the room //! the eyes and floors are in, and blue). `VICTORY_AMBIENT_SPECULAR` puts in, //! right after that program, the same estimate taken along the reflected //! direction ([`Role::Miss`]); the screen-space resolve that comes next //! still writes what it found over it. use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering}; use std::sync::{Mutex, OnceLock}; use windows::Win32::Graphics::Direct3D12::*; use windows::core::s; use crate::host::{HostList, host}; /// Programs of the title the stand-in follows. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub(crate) enum Role { /// Resolves the diffuse light. Resolve, /// Keeps the light visible surfaces send out. Radiance, /// Adds the lamps' light to what the air scatters (the last program /// that writes that volume before it is summed up along the view). Air, /// Gives the reflection rays that found nothing the sky's light /// (`CS_RayMiss`, not `CS_RayMiss_Volumetrics` nor the screen probes'). Miss, } /// The program's part in this, by its name in the container. pub(crate) fn role(container: &[u8]) -> Option { let has = |name: &[u8]| container.windows(name.len()).any(|w| w == name); if has(b"CS_ScreenProbes_TemporalResolve") { Some(Role::Resolve) } else if has(b"CS_CacheScreenSpaceRadiance") { Some(Role::Radiance) } else if has(b"CS_AccumulateLocalLightInscatteringVolume") { Some(Role::Air) } else if is_miss(container) { Some(Role::Miss) } else { None } } /// `CS_RayMiss_` and the eight hex digits of a variant's hash. fn is_miss(container: &[u8]) -> bool { const NAME: &[u8] = b"CS_RayMiss_"; container.windows(NAME.len() + 9).any(|w| { w.starts_with(NAME) && w[NAME.len()..NAME.len() + 8].iter().all(u8::is_ascii_hexdigit) && w[NAME.len() + 8] == b'_' }) } /// Where the resolve writes the diffuse light (R9G9B9E5 as 32-bit integers) /// and reads the scene's depth, and where the other writes the light of /// visible surfaces: root parameter and place in its table, in dwords. pub(crate) const LIGHT: (u8, u32) = (1, 176); pub(crate) const DEPTH: (u8, u32) = (0, 464); pub(crate) const RADIANCE: (u8, u32) = (1, 16); /// Where the light the air scatters is. pub(crate) const AIR: (u8, u32) = (1, 32); /// Where the miss program writes the reflected light (R11G11B10, half the /// picture's size) and where it reads the depth it is given (a 32-bit depth /// of the picture's size). pub(crate) const MISS_LIGHT: (u8, u32) = (1, 104); pub(crate) const MISS_DEPTH: (u8, u32) = (0, 1000); /// The miss program's first root constant: which surfaces its rays are of. pub(crate) const MISS_OPAQUE: u32 = 0; pub(crate) const MISS_ALPHA: u32 = 1; #[derive(Clone, Copy, PartialEq, Debug)] enum Set { Off, Estimate(f32), Constant(f32), } fn parse(set: &str) -> Set { let set = set.trim(); let number = |t: &str| t.trim().parse::().ok().filter(|v| v.is_finite() && *v > 0.0); match set.strip_prefix("constant:") { Some(level) => number(level).map_or(Set::Off, Set::Constant), None => number(set).map_or(Set::Off, Set::Estimate), } } fn set() -> Set { static SET: OnceLock = OnceLock::new(); *SET.get_or_init(|| parse(&std::env::var("VICTORY_AMBIENT").unwrap_or_default())) } /// Whether a stand-in is on (for the diffuse light or for reflections): /// set, and the title's rays find nothing. pub(crate) fn on() -> bool { (set() != Set::Off || specular().is_some()) && !crate::pipeline::builder_runs() } /// `VICTORY_AMBIENT_SPECULAR=[,]`: reflections whose rays found /// nothing take `share` of the estimate along the reflected direction /// instead of the sky's light; `sky` (0 to 1, 0 if not given) of them stays /// the title's sky, made no brighter than the estimate. Unset or 0: the /// title's sky. #[derive(Clone, Copy, PartialEq, Debug)] struct Specular { share: f32, sky: f32, } fn parse_specular(set: &str) -> Option { let mut parts = set.split(',').map(|t| t.trim().parse::().ok().filter(|v| v.is_finite())); let share = parts.next().flatten().filter(|v| *v > 0.0)?; let sky = parts.next().flatten().map_or(0.0, |v| v.clamp(0.0, 1.0)); Some(Specular { share, sky }) } fn specular() -> Option { static SET: OnceLock> = OnceLock::new(); *SET.get_or_init(|| parse_specular(&std::env::var("VICTORY_AMBIENT_SPECULAR").unwrap_or_default())) } /// `VICTORY_AMBIENT_ONCE=1`: the estimate is one bounce of the light the /// title's own lighting put there, not of the stand-in's too. /// /// The light the visible surfaces send out (the estimate's source) is that of /// the frame before, which the stand-in had raised: its own light comes back /// to it, reflected. With a share `level` of what surfaces send out, and /// surfaces sending out a share `a` of the light they get (`e` their own), /// the light settles at `level * e / (1 - level * a)` instead of /// `level * (a * l + e)`: at 0.8 and walls that send out 0.6, 1.9 times /// one bounce, more near pale surfaces and lamps (the overexposed rooms). /// With this switch the raise notes, at the 25 places of each cell that the /// next keep reads, which share of the light there is the title's own; the /// keep counts each place's light by that share. Light that is the /// surface's own (lamps) is counted down as well where the stand-in raised /// it: shortcut, no albedo to tell the two apart. fn once() -> bool { static ON: OnceLock = OnceLock::new(); *ON.get_or_init(|| std::env::var_os("VICTORY_AMBIENT_ONCE").is_some_and(|v| v == "1")) } /// The most the air scatters, as a multiple of the light around /// (`VICTORY_AMBIENT_AIR=`; unset or 0: what the title has). fn air() -> f32 { static SET: OnceLock = OnceLock::new(); *SET.get_or_init(|| { let set = std::env::var("VICTORY_AMBIENT_AIR").unwrap_or_default(); set.trim().parse().ok().filter(|v: &f32| v.is_finite() && *v > 0.0).unwrap_or(0.0) }) } /// Strength and radius of the creases' shade. fn shade() -> [f32; 2] { static SET: OnceLock<[f32; 2]> = OnceLock::new(); *SET.get_or_init(|| { let mut shade = [1.0, 0.6]; let set = std::env::var("VICTORY_AMBIENT_SHADE").unwrap_or_default(); for (to, v) in shade.iter_mut().zip(set.split(',')) { if let Ok(v) = v.trim().parse() { *to = v; } } shade }) } const SOURCE: &str = r#" #define RS "RootConstants(num32BitConstants=12, b0), DescriptorTable(SRV(t0)), DescriptorTable(UAV(u0)), UAV(u1), UAV(u2)" #define CX 64 #define CY 36 #define CELLS (CX * CY) #define PX 160 #define PY 90 cbuffer Set : register(b0) { float level; float constant; float strength; float radius; float near; float tan_y; uint width; uint height; float follow; float most; float air; float once; }; Texture2D depth : register(t0); // Cells: the light they send out (then, at CELLS, the slow mean), from // CELLS + 1 on where they are, from SHARES on the share of the title's own // light at each of the 25 places of a cell the keep reads (x; y: written). #define SHARES (2 * CELLS + 1) RWStructuredBuffer cells : register(u1); // Three a point: red, green and blue, each a mean and a direction. RWStructuredBuffer points : register(u2); static const float3 LUMA = float3(0.2126, 0.7152, 0.0722); // The point seen at a texel, in metres from the eye (x right, y up, z away). float3 seen(int2 p, float2 size) { float d = depth.Load(int3(clamp(p, int2(0, 0), int2(size) - 1), 0)); float z = min(near / max(d, 1e-7), 1000.0); float2 ndc = (float2(p) + 0.5) / size * 2.0 - 1.0; return float3(ndc.x * tan_y * size.x / size.y * z, -ndc.y * tan_y * z, z); } #ifdef KEEP RWTexture2D radiance : register(u0); [RootSignature(RS)] [numthreads(8, 4, 1)] void keep(uint3 at : SV_DispatchThreadID) { uint w, h; radiance.GetDimensions(w, h); float3 sum = 0; for (uint y = 0; y < 5; y++) for (uint x = 0; x < 5; x++) { uint2 p = uint2((float2(at.xy) + (float2(x, y) + 0.5) / 5.0) * float2(w, h) / float2(CX, CY)); float3 c = max(radiance[p], 0.0); c = any(isnan(c)) || any(isinf(c)) ? 0.0 : c; // Lamps and glints count, but not for everything. c /= max(1.0, dot(c, LUMA) / 20.0); // Not the stand-in's own light, reflected (`once`). float4 share = cells[SHARES + (at.y * CX + at.x) * 25 + y * 5 + x]; sum += once > 0.0 && share.y > 0.0 ? c * saturate(share.x) : c; } cells[at.y * CX + at.x] = float4(sum / 25.0, 1.0); } [RootSignature(RS)] [numthreads(1, 1, 1)] void mean() { float3 sum = 0; for (uint i = 0; i < CELLS; i++) sum += cells[i].rgb; float4 was = cells[CELLS]; float3 now = sum / CELLS; cells[CELLS] = float4(was.a > 0.0 ? lerp(was.rgb, now, follow) : now, 1.0); } #endif #ifdef PLACE [RootSignature(RS)] [numthreads(8, 4, 1)] void place(uint3 at : SV_DispatchThreadID) { float2 size = float2(width, height); float3 p = seen(int2((float2(at.xy) + 0.5) * size / float2(CX, CY)), size); cells[CELLS + 1 + at.y * CX + at.x] = float4(p, p.z * p.z); } #endif #ifdef GATHER [RootSignature(RS)] [numthreads(8, 2, 1)] void gather(uint3 at : SV_DispatchThreadID) { float2 size = float2(width, height); float3 p = seen(int2((float2(at.xy) + 0.5) * size / float2(PX, PY)), size); float r2 = 0.25 + 0.0025 * p.z * p.z; float whole = 0; float3 toward = 0, light = 0, red = 0, green = 0, blue = 0; for (uint i = 0; i < CELLS; i++) { float4 c = cells[CELLS + 1 + i]; float3 L = cells[i].rgb; float3 d = c.xyz - p; float dd = dot(d, d); float w = c.w / (dd + r2); float3 dir = d / sqrt(dd + 0.25 * r2); whole += w; toward += w * dir; light += w * L; red += w * L.r * dir; green += w * L.g * dir; blue += w * L.b * dir; } whole = max(whole, 1e-6); light /= whole; toward /= whole; uint at0 = (at.y * PX + at.x) * 3; points[at0] = float4(red / whole - light.r * toward, light.r); points[at0 + 1] = float4(green / whole - light.g * toward, light.g); points[at0 + 2] = float4(blue / whole - light.b * toward, light.b); } #endif #ifdef AIR RWTexture3D scattered : register(u0); // No more than a multiple of the light around: of the room as the last // seconds showed it, and of the surface seen in this direction (towards a // window the air outside may be as bright as the sky is). [RootSignature(RS)] [numthreads(8, 8, 2)] void clear_air(uint3 at : SV_DispatchThreadID) { uint w, h, d; scattered.GetDimensions(w, h, d); if (at.x >= w || at.y >= h || at.z >= d) return; uint2 q = min(uint2((float2(at.xy) + 0.5) / float2(w, h) * float2(PX, PY)), uint2(PX - 1, PY - 1)); uint at0 = (q.y * PX + q.x) * 3; float3 seen_there = float3(points[at0].w, points[at0 + 1].w, points[at0 + 2].w); float far = float(at.z) / float(d); float cap = air * lerp(dot(cells[CELLS].rgb, LUMA), dot(seen_there, LUMA), far); float3 c = scattered[at]; uint3 bits = asuint(c); bool3 bad = (bits & 0x7f800000u) == 0x7f800000u; bool fixed_any = any(bad); if (fixed_any) { c = bad ? (bits == 0x7f800000u ? 64512.0 : 0.0) : c; } float has = dot(c, LUMA); if (has > cap) scattered[at] = c * (cap / has); else if (fixed_any) scattered[at] = c; } #endif #ifdef REFLECT RWTexture2D reflected : register(u0); // Each texel of the reflections' image (half the picture's size): the light // the points around send along the reflected direction, as `raise` takes it // along the normal; `level` is the share, `constant` the share of the // title's sky kept, made no brighter than that. [RootSignature(RS)] [numthreads(8, 8, 1)] void reflect_light(uint3 at : SV_DispatchThreadID) { uint w, h; reflected.GetDimensions(w, h); if (at.x >= w || at.y >= h) return; float2 size = float2(width, height); int2 p = int2((float2(at.xy) + 0.5) * size / float2(w, h)); float3 c = seen(p, size); // Nothing is reflected by the sky. if (c.z >= 999.0) return; float3 l = seen(p + int2(-1, 0), size), r = seen(p + int2(1, 0), size); float3 u = seen(p + int2(0, -1), size), d = seen(p + int2(0, 1), size); float3 dx = abs(l.z - c.z) < abs(r.z - c.z) ? c - l : r - c; float3 dy = abs(u.z - c.z) < abs(d.z - c.z) ? c - u : d - c; float3 n = normalize(cross(dy, dx) + float3(0, 0, -1e-9)); if (dot(n, c) > 0.0) n = -n; float3 v = normalize(c); float3 ray = v - 2.0 * dot(v, n) * n; float2 g = (float2(p) + 0.5) / size * float2(PX, PY) - 0.5; int2 g0 = int2(floor(g)); float2 f = g - float2(g0); float4 sr = 0, sg = 0, sb = 0; float sum = 0; for (int k = 0; k < 4; k++) { int2 q = clamp(g0 + int2(k & 1, k >> 1), int2(0, 0), int2(PX, PY) - 1); float z = seen(int2((float2(q) + 0.5) * size / float2(PX, PY)), size).z; float2 b = float2(k & 1 ? f.x : 1 - f.x, k >> 1 ? f.y : 1 - f.y); float wgt = (b.x * b.y + 1e-3) / (0.02 + abs(z - c.z) / c.z); uint at0 = (q.y * PX + q.x) * 3; sr += wgt * points[at0]; sg += wgt * points[at0 + 1]; sb += wgt * points[at0 + 2]; sum += wgt; } float3 all = float3(sr.w, sg.w, sb.w) / sum; // Radiance from the first moments: three times the direction's share // for an even spread of directions, a little less for a smooth image. float3 found = all + 2.0 * float3(dot(sr.xyz, ray), dot(sg.xyz, ray), dot(sb.xyz, ray)) / sum; found = clamp(found, 0.1 * all, 3.0 * all); found = lerp(found, cells[CELLS].rgb, 0.2) * level; found = any(isnan(found)) || any(isinf(found)) ? 0.0 : max(found, 0.0); float3 was = reflected[at.xy]; was = any(isnan(was)) || any(isinf(was)) ? 0.0 : max(was, 0.0); float has = dot(was, LUMA); was *= min(1.0, dot(found, LUMA) / max(has, 1e-6)); reflected[at.xy] = lerp(found, was, constant); } #endif #ifdef RAISE RWTexture2D light : register(u0); float3 unpack(uint v) { return float3(v & 511, (v >> 9) & 511, (v >> 18) & 511) * exp2(float(v >> 27) - 24.0); } uint pack(float3 c) { c = clamp(c, 0.0, 65408.0); float top = max(c.r, max(c.g, c.b)); int e = clamp(int(floor(log2(max(top, 1e-9)))) + 16, 0, 31); float scale = exp2(float(24 - e)); if (top * scale >= 511.5) { e = min(e + 1, 31); scale = exp2(float(24 - e)); } uint3 m = min(uint3(c * scale + 0.5), 511u); return m.r | (m.g << 9) | (m.b << 18) | (uint(e) << 27); } [RootSignature(RS)] [numthreads(8, 8, 1)] void raise(uint3 at : SV_DispatchThreadID) { float2 size = float2(width, height); if (at.x >= width || at.y >= height) return; int2 p = int2(at.xy); float3 c = seen(p, size); float3 l = seen(p + int2(-1, 0), size), r = seen(p + int2(1, 0), size); float3 u = seen(p + int2(0, -1), size), d = seen(p + int2(0, 1), size); float3 dx = abs(l.z - c.z) < abs(r.z - c.z) ? c - l : r - c; float3 dy = abs(u.z - c.z) < abs(d.z - c.z) ? c - u : d - c; float3 n = normalize(cross(dy, dx) + float3(0, 0, -1e-9)); if (dot(n, c) > 0.0) n = -n; // How open it is (as in beloooo's cd9f78ce). float open = 1.0; float px = min(radius * size.y / (2.0 * tan_y * c.z), 128.0); float noise = frac(52.9829189 * frac(dot(float2(p), float2(0.06711056, 0.00583715)))); if (strength > 0.0 && px >= 1.5 && c.z < 400.0) { float sum = 0.0; const int N = 12; for (int i = 0; i < N; i++) { float a = i * 2.399963 + noise * 6.2831853; float2 o = float2(cos(a), sin(a)) * (sqrt((i + 0.5) / N) * px); float3 v = seen(p + int2(round(o)), size) - c; float vv = dot(v, v); float over = dot(v, n) - 0.004 * c.z - 0.005; sum += max(over, 0.0) / sqrt(vv + 1e-6) * saturate(1.0 - vv / (radius * radius)); } open = saturate(1.0 - strength * 2.0 * sum / N); } float3 found; if (constant > 0.0) { found = float3(1.0, 0.98, 0.94) * constant * open; light[p] = pack(found); return; } // Between the four points around, those at its own depth first; which // four is dithered a little. float2 g = (float2(p) + 0.5) / size * float2(PX, PY) - 0.5 + (noise - 0.5) * 0.5; int2 g0 = int2(floor(g)); float2 f = g - float2(g0); float4 sr = 0, sg = 0, sb = 0; float sum = 0; for (int k = 0; k < 4; k++) { int2 q = clamp(g0 + int2(k & 1, k >> 1), int2(0, 0), int2(PX, PY) - 1); float z = seen(int2((float2(q) + 0.5) * size / float2(PX, PY)), size).z; float2 b = float2(k & 1 ? f.x : 1 - f.x, k >> 1 ? f.y : 1 - f.y); float w = (b.x * b.y + 1e-3) / (0.02 + abs(z - c.z) / c.z); uint at0 = (q.y * PX + q.x) * 3; sr += w * points[at0]; sg += w * points[at0 + 1]; sb += w * points[at0 + 2]; sum += w; } float3 all = float3(sr.w, sg.w, sb.w) / sum; found = all + 1.2 * float3(dot(sr.xyz, n), dot(sg.xyz, n), dot(sb.xyz, n)) / sum; found = clamp(found, 0.2 * all, 2.5 * all); found = lerp(found, cells[CELLS].rgb, 0.2) * level * open; float3 was = unpack(light[p]); // Magenta/violet garbage the probes' memory held (green far under red and // blue): dropped, as VIctory-Belobug-AMD does (ambient.rs, "Filter magenta // garbage probes"). Seen on an RTX 5080 as a purple-blue glow on a lit // sheet of paper in the prison cell; not an AMD-only fault. if (was.g < 0.5 * min(was.r, was.b) && max(was.r, was.b) > 0.02) was = 0.0; // And the bluer kind of it (blue over 2.5x green and over red): left as // a faint violet by the line above. The sky's light keeps green above // 0.4x blue, sunsets keep blue low, so neither is touched. if (was.b > 2.5 * was.g && was.b > was.r && was.b > 0.02) was = 0.0; // What the program left counts as far as it can be true: the sky's light // outdoors, not the blocks of other memory. float top = max(dot(found, LUMA) * most, 1e-6); float has = dot(was, LUMA); was *= min(1.0, top / max(has, 1e-6)); float3 now = max(was, found); light[p] = pack(now); if (once > 0.0) { // At the places the next keep reads, the share of the light that is // the title's own. float2 gc = (float2(p) + 0.5) / size * float2(CX, CY); uint2 cell = min(uint2(gc), uint2(CX - 1, CY - 1)); uint2 k = min(uint2((gc - float2(cell)) * 5.0), uint2(4, 4)); uint2 q = uint2((float2(cell) + (float2(k) + 0.5) / 5.0) * size / float2(CX, CY)); if (q.x == uint(p.x) && q.y == uint(p.y)) { float share = dot(was, LUMA) / max(dot(now, LUMA), 1e-6); cells[SHARES + (cell.y * CX + cell.x) * 25 + k.y * 5 + k.x] = float4(share, 1.0, 0.0, 0.0); } } } #endif "#; /// Bytes of the cells' buffer and of the points'. const CELLS: u64 = 64 * 36; const CELL_BYTES: u64 = (2 * CELLS + 1 + 25 * CELLS) * 16; const POINT_BYTES: u64 = 160 * 90 * 48; struct Pass { root: ID3D12RootSignature, keep: ID3D12PipelineState, mean: ID3D12PipelineState, place: ID3D12PipelineState, gather: ID3D12PipelineState, raise: ID3D12PipelineState, air: ID3D12PipelineState, reflect: ID3D12PipelineState, cells: ID3D12Resource, points: ID3D12Resource, } // SAFETY: D3D12 objects are free-threaded. unsafe impl Send for Pass {} // SAFETY: as above. unsafe impl Sync for Pass {} fn make(device: &ID3D12Device, buffer: impl Fn(u64) -> Option) -> windows::core::Result { let mut root: Option = None; let mut pipeline = |part: &str, entry| -> windows::core::Result { let code = crate::host::compile(&format!("#define {part}\n{SOURCE}"), entry, s!("cs_5_1"))?; // SAFETY: the root signature is inside the shader; plain creations. unsafe { if root.is_none() { root = Some(device.CreateRootSignature(0, &code)?); } let desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { pRootSignature: std::mem::transmute_copy(&root), CS: D3D12_SHADER_BYTECODE { pShaderBytecode: code.as_ptr().cast(), BytecodeLength: code.len() }, ..Default::default() }; device.CreateComputePipelineState(&desc) } }; let keep = pipeline("KEEP", s!("keep"))?; let mean = pipeline("KEEP", s!("mean"))?; let place = pipeline("PLACE", s!("place"))?; let gather = pipeline("GATHER", s!("gather"))?; let raise = pipeline("RAISE", s!("raise"))?; let air = pipeline("AIR", s!("clear_air"))?; let reflect = pipeline("REFLECT", s!("reflect_light"))?; let made = |bytes| buffer(bytes).ok_or_else(windows::core::Error::empty); Ok(Pass { root: root.ok_or_else(windows::core::Error::empty)?, keep, mean, place, gather, raise, air, reflect, cells: made(CELL_BYTES)?, points: made(POINT_BYTES)?, }) } fn pass() -> Option<&'static Pass> { static PASS: OnceLock> = OnceLock::new(); PASS.get_or_init(|| { let host = host()?; make(&host.device, |bytes| host.uav_buffer(bytes)) .inspect_err(|e| tracing::warn!(error = %e, "no stand-in for the indirect light")) .ok() }) .as_ref() } fn uav_barrier(list: &ID3D12GraphicsCommandList) { let barrier = D3D12_RESOURCE_BARRIER { Type: D3D12_RESOURCE_BARRIER_TYPE_UAV, ..Default::default() }; // SAFETY: an open list; the barrier names no resource. unsafe { list.ResourceBarrier(&[barrier]) }; } /// Whether the light of a frame has been kept: the estimate has something /// to begin with. static KEPT: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); /// What the passes read and write, as views in the heap the list has set. pub(crate) struct Views { /// The image the title's program just wrote. pub(crate) image: D3D12_GPU_DESCRIPTOR_HANDLE, /// The scene's depth, for [`Role::Resolve`]. pub(crate) depth: D3D12_GPU_DESCRIPTOR_HANDLE, /// The size of the image, and how many slices it has. pub(crate) size: [u32; 2], pub(crate) slices: u32, /// The size of the depth (the image's but for [`Role::Miss`]). pub(crate) depth_size: [u32; 2], } /// For the line every ten seconds: what the stand-in for reflections did. struct Said { /// Dispatches of the miss program it followed, frames they were in, /// and those it left as they were: of water, and before a frame's /// light was kept (or no tables). ran: AtomicU64, frames: AtomicU64, water: AtomicU64, not_ready: AtomicU64, last_frame: AtomicU64, /// Bit 0 opaque surfaces, 1 alpha, 2 water, 3 others. layers: AtomicU64, } static SAID_MISS: Said = Said { ran: AtomicU64::new(0), frames: AtomicU64::new(0), water: AtomicU64::new(0), not_ready: AtomicU64::new(0), last_frame: AtomicU64::new(u64::MAX), layers: AtomicU64::new(0), }; /// What came of a dispatch of the miss program, counted for the line. #[derive(Clone, Copy, PartialEq, Debug)] pub(crate) enum MissNote { Ran(u32), Water, NotReady, } /// Counts what came of a dispatch of the miss program; every ten seconds an /// info line says it, so a run shows that the stand-in ran and where. pub(crate) fn note_miss(note: MissNote) { use AtomicOrdering::Relaxed; if specular().is_none() { return; } let s = &SAID_MISS; match note { MissNote::Ran(layer) => { s.ran.fetch_add(1, Relaxed); s.layers.fetch_or(1 << layer.min(3), Relaxed); let frame = crate::frame::current(); if s.last_frame.swap(frame, Relaxed) != frame { s.frames.fetch_add(1, Relaxed); } } MissNote::Water => { s.water.fetch_add(1, Relaxed); } MissNote::NotReady => { s.not_ready.fetch_add(1, Relaxed); } } static LAST: Mutex> = Mutex::new(None); let mut last = LAST.lock().unwrap_or_else(|e| e.into_inner()); let now = std::time::Instant::now(); let Some(since) = *last else { *last = Some(now); return; }; if now.duration_since(since).as_secs_f32() < 10.0 { return; } *last = Some(now); let layers = s.layers.swap(0, Relaxed); let named: Vec<&str> = [(1, "opaque"), (2, "alpha"), (4, "water"), (8, "other")] .iter() .filter(|(bit, _)| layers & bit != 0) .map(|(_, name)| *name) .collect(); let Specular { share, sky } = specular().unwrap_or(Specular { share: 0.0, sky: 0.0 }); tracing::info!( "a stand-in for reflections (VICTORY_AMBIENT_SPECULAR={share},{sky}) in the last {:.0} s: \ {} frames, {} dispatches of CS_RayMiss followed (surfaces: {}), each by an estimate along \ the reflected direction put in right after it (before CS_ResolveScreenSpace); {} of water or the alpha layer \ and {} before a frame's light was kept left as the title had them", now.duration_since(since).as_secs_f32(), s.frames.swap(0, Relaxed), s.ran.swap(0, Relaxed), if named.is_empty() { "none".to_owned() } else { named.join(", ") }, s.water.swap(0, Relaxed), s.not_ready.swap(0, Relaxed), ); } /// After the title's program of that role, in `list` with the program's view /// heap set. Replaces the list's compute root signature and pipeline; false /// if it recorded nothing. pub(crate) fn after(list: &HostList, role: Role, views: &Views) -> bool { use std::sync::atomic::Ordering; let Some(pass) = pass() else { return false; }; // The share of the estimate, and the constant (for reflections: the // share of the title's sky kept); the keep runs for either stand-in. let (level, constant) = match (role, set(), specular()) { (Role::Miss, _, Some(s)) => (s.share, s.sky), (Role::Miss, _, None) => return false, (Role::Radiance, Set::Off, Some(_)) => (0.0, 0.0), (_, Set::Off, _) => return false, (_, Set::Estimate(level), _) => (level, 0.0f32), (_, Set::Constant(level), _) => (0.0, level), }; let estimate = role == Role::Miss || constant == 0.0; if role != Role::Radiance && estimate && !KEPT.load(Ordering::Relaxed) { if role == Role::Miss { note_miss(MissNote::NotReady); } return false; } if role == Role::Air && (constant > 0.0 || air() == 0.0) { return false; } static SAID: Mutex = Mutex::new(0); { let mut said = SAID.lock().unwrap_or_else(|e| e.into_inner()); if *said < 5 { *said += 1; tracing::info!(?role, size = ?views.size, level, constant, "a stand-in for the indirect light"); } } list.keep(&pass.cells); list.keep(&pass.points); let [strength, radius] = shade(); let floats = [level, constant, strength, radius, 0.1, 0.45]; let mut words = [0u32; 12]; for (to, v) in words.iter_mut().zip(floats) { *to = v.to_bits(); } words[6] = views.depth_size[0]; words[7] = views.depth_size[1]; words[8] = 0.04f32.to_bits(); words[9] = 4.0f32.to_bits(); words[10] = air().to_bits(); words[11] = if once() { 1.0f32 } else { 0.0 }.to_bits(); let l = &list.list; // SAFETY: an open list with the heap of the views set; the title's // program has just used both views, so their textures are in the states // these views need; buffers the list keeps. unsafe { uav_barrier(l); l.SetComputeRootSignature(&pass.root); l.SetComputeRoot32BitConstants(0, 12, words.as_ptr().cast(), 0); l.SetComputeRootDescriptorTable(1, views.depth); l.SetComputeRootDescriptorTable(2, views.image); l.SetComputeRootUnorderedAccessView(3, pass.cells.GetGPUVirtualAddress()); l.SetComputeRootUnorderedAccessView(4, pass.points.GetGPUVirtualAddress()); let run = |pso: &ID3D12PipelineState, x, y, z| { l.SetPipelineState(pso); l.Dispatch(x, y, z); uav_barrier(l); }; match role { Role::Radiance => { run(&pass.keep, 8, 9, 1); run(&pass.mean, 1, 1, 1); KEPT.store(true, Ordering::Relaxed); } Role::Air => run(&pass.air, views.size[0].div_ceil(8), views.size[1].div_ceil(8), views.slices.div_ceil(2)), Role::Resolve => { if constant == 0.0 { run(&pass.place, 8, 9, 1); run(&pass.gather, 20, 45, 1); } run(&pass.raise, views.size[0].div_ceil(8), views.size[1].div_ceil(8), 1); } Role::Miss => { // The points afresh, from this frame's depth: the diffuse // stand-in makes them later in the frame. run(&pass.place, 8, 9, 1); run(&pass.gather, 20, 45, 1); run(&pass.reflect, views.size[0].div_ceil(8), views.size[1].div_ceil(8), 1); } } } true } #[cfg(test)] mod tests { use super::*; #[test] fn settings() { assert_eq!(parse(""), Set::Off); assert_eq!(parse("off"), Set::Off); assert_eq!(parse("0"), Set::Off); assert_eq!(parse("0.8"), Set::Estimate(0.8)); assert_eq!(parse("constant:0.3"), Set::Constant(0.3)); assert_eq!(parse("constant:nan"), Set::Off); } #[test] fn specular_settings() { assert_eq!(parse_specular(""), None); assert_eq!(parse_specular("0"), None); assert_eq!(parse_specular("off"), None); assert_eq!(parse_specular("1"), Some(Specular { share: 1.0, sky: 0.0 })); assert_eq!(parse_specular("0.6, 0.25"), Some(Specular { share: 0.6, sky: 0.25 })); assert_eq!(parse_specular("0.6,7"), Some(Specular { share: 0.6, sky: 1.0 })); assert_eq!(parse_specular("0.6,x"), Some(Specular { share: 0.6, sky: 0.0 })); } #[test] fn roles_by_name() { assert_eq!(role(b"..CS_ScreenProbes_TemporalResolve_a911b49c_Wrapped.."), Some(Role::Resolve)); assert_eq!(role(b"..CS_CacheScreenSpaceRadiance_eed2fc35.."), Some(Role::Radiance)); assert_eq!(role(b"CS_AccumulateLocalLightInscatteringVolume_7e2f5eef"), Some(Role::Air)); assert_eq!(role(b"CS_TemporalResolve"), None); assert_eq!(role(b"..CS_RayMiss_de6b91a6_Wrapped.."), Some(Role::Miss)); assert_eq!(role(b"..CS_RayMiss_Volumetrics_3c5d8c00_Wrapped.."), None); assert_eq!(role(b"..CS_ScreenProbe_RayMiss_80a88544_Wrapped.."), None); } /// The keep and raise as they act on the light, frame after frame, for /// surfaces that send out `a` of the light they get and the title's own /// light `title` on each (`VICTORY_AMBIENT_ONCE`'s model): the light it /// settles at on each surface. fn settle(level: f32, a: f32, title: &[f32], once: bool) -> Vec { let mut light = title.to_vec(); let mut share = vec![1.0f32; title.len()]; for _ in 0..500 { // Keep: what the surfaces sent out (the frame before), counted by // the share of the title's own light there with `once`. let sent: f32 = light .iter() .zip(&share) .map(|(l, s)| a * l * if once { *s } else { 1.0 }) .sum::() / title.len() as f32; // Raise: never below the title's own light. for (i, t) in title.iter().enumerate() { light[i] = t.max(level * sent); share[i] = t / light[i].max(1e-6); } } light } /// Without it the stand-in's own light comes back to it: a lit wall and a /// dark one settle at `level a t / 2 / (1 - level a / 2)` on the dark one, /// a third more than one bounce at walls that send out 0.6, three /// quarters more at 0.9. With it, one bounce of the title's light. #[test] fn once_is_one_bounce_of_the_titles_light() { for a in [0.6f32, 0.9] { let one_bounce = 0.8 * a * 1.0 / 2.0; let fed = settle(0.8, a, &[1.0, 0.0], false)[1]; let once = settle(0.8, a, &[1.0, 0.0], true)[1]; assert!( (fed - one_bounce / (1.0 - 0.8 * a / 2.0)).abs() < 1e-4, "a={a}: fed back {fed}" ); assert!((once - one_bounce).abs() < 1e-4, "a={a}: once {once}"); assert!(fed > 1.3 * once); } // Where the title's light is above the estimate nothing changes. assert_eq!(settle(0.8, 0.6, &[1.0, 1.0], true), vec![1.0, 1.0]); } /// The passes compile with the system's compiler and the host takes them. #[test] fn the_passes_build() { let Some(host) = host() else { return; }; make(&host.device, |bytes| host.uav_buffer(bytes)).expect("the passes"); } }