Merge agent/image2: pipelines with static samplers kept out of the pipeline library; ambient stand-in without feedback (switch)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
readmet3xt-labandClaude Opus 5.5 committed 2026-11-26 18:27:31 +05:30
commit 02b255d25b
4 files changed
+882 -6

No files matched your search

+92 -5
View File
@@ -29,6 +29,8 @@
//! - `constant:<level>`: the constant, as beloooo's fork has it.
//!
//! `VICTORY_AMBIENT_SHADE=<strength>,<radius in metres>` 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`
@@ -102,6 +104,26 @@ pub(crate) fn on() -> bool {
set() != Set::Off && !crate::pipeline::builder_runs()
}
/// `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<bool> = 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=<multiple>`; unset or 0: what the title has).
fn air() -> f32 {
@@ -137,11 +159,13 @@ const SOURCE: &str = r#"
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 unused;
float follow; float most; float air; float once;
};
Texture2D<float> depth : register(t0);
// Cells: the light they send out (then, at CELLS, the slow mean), from
// CELLS + 1 on where they are.
// 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<float4> cells : register(u1);
// Three a point: red, green and blue, each a mean and a direction.
RWStructuredBuffer<float4> points : register(u2);
@@ -168,7 +192,10 @@ void keep(uint3 at : SV_DispatchThreadID) {
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.
sum += c / max(1.0, dot(c, LUMA) / 20.0);
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);
}
@@ -326,14 +353,27 @@ void raise(uint3 at : SV_DispatchThreadID) {
float top = max(dot(found, LUMA) * most, 1e-6);
float has = dot(was, LUMA);
was *= min(1.0, top / max(has, 1e-6));
light[p] = pack(max(was, found));
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) * 16;
const CELL_BYTES: u64 = (2 * CELLS + 1 + 25 * CELLS) * 16;
const POINT_BYTES: u64 = 160 * 90 * 48;
struct Pass {
@@ -461,6 +501,7 @@ pub(crate) fn after(list: &HostList, role: Role, views: &Views) -> bool {
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
@@ -519,6 +560,52 @@ mod tests {
assert_eq!(role(b"CS_TemporalResolve"), 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<f32> {
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::<f32>()
/ 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() {
+3
View File
@@ -599,6 +599,9 @@ impl HostPipeline {
return None;
}
};
// Not from the library if it has static samplers (their places in the
// driver's table differ between runs: `library::with_static_samplers`).
let _samplers = super::library::with_static_samplers(!samplers.is_empty());
let code = |i: usize| {
stages.get(i).and_then(|(s, _)| s.translated()).map_or(
D3D12_SHADER_BYTECODE::default(),
+76 -1
View File
@@ -255,6 +255,55 @@ static CALLS: Mutex<Vec<(u32, &'static str, String, u64)>> = Mutex::new(Vec::new
thread_local! {
/// The pipeline this thread is making (its shaders), for [`CALLS`].
static LABEL: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
/// Whether the pipeline this thread makes has static samplers
/// ([`with_static_samplers`]).
static STATIC_SAMPLERS: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
/// The pipelines this thread makes while the guard lives have static
/// samplers (`samplers`): they are made, never loaded from or stored in the
/// library, unless `VICTORY_LIBRARY_STATIC_SAMPLERS=1`.
///
/// Why: the card's driver (NVIDIA, 4070 SUPER) keeps static samplers in a
/// table of its own and puts their places in that table into the compiled
/// pipeline. A pipeline loaded from the library in another process keeps the
/// places of the process that stored it; where that process made other
/// static samplers first, they are other samplers. Measured
/// (`examples/lut3d_probe.rs`): a pipeline whose root signature has a point,
/// a linear and a trilinear sampler, stored, then loaded in a process that
/// made ten other static samplers before its own, sampled a 3D table
/// through the linear one unfiltered (every result a table entry); made in
/// that process instead, filtered. In the title, PS_CompositeNMB loaded so
/// read its colour-grading tables unfiltered: posterized light in steps of
/// 0.44 stops with green and magenta fringes (2026-11-26 runs; made in the
/// run, the image was smooth). Every loaded pipeline with static samplers
/// may read through another one's: other filters, address modes, levels
/// and comparisons.
/// shortcut: they are compiled every run (the driver's own cache on disk
/// makes that quick); a fixed order of static samplers made before any
/// pipeline would let the library keep them again.
pub(crate) fn with_static_samplers(samplers: bool) -> StaticSamplers {
StaticSamplers(STATIC_SAMPLERS.replace(samplers && !static_samplers_kept()))
}
/// While it lives, the pipelines this thread makes skip the library if they
/// have static samplers ([`with_static_samplers`]); the setting before it
/// comes back when it is dropped.
pub(crate) struct StaticSamplers(bool);
impl Drop for StaticSamplers {
fn drop(&mut self) {
STATIC_SAMPLERS.set(self.0);
}
}
/// `VICTORY_LIBRARY_STATIC_SAMPLERS=1`: pipelines with static samplers are
/// loaded from and stored in the library too, as before.
fn static_samplers_kept() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| {
std::env::var_os("VICTORY_LIBRARY_STATIC_SAMPLERS").is_some_and(|v| v == "1")
})
}
/// Names the pipeline this thread makes next.
@@ -520,7 +569,7 @@ pub(crate) fn pipeline(
create: impl FnOnce() -> windows::core::Result<ID3D12PipelineState>,
) -> windows::core::Result<ID3D12PipelineState> {
let wide = HSTRING::from(name);
let Some(library) = library() else {
let Some(library) = library().filter(|_| !STATIC_SAMPLERS.get()) else {
MADE.fetch_add(1, Ordering::Relaxed);
return create();
};
@@ -796,6 +845,32 @@ mod tests {
assert_ne!(a, name("graphics", b"root", &[b"vsp", b"s"], "state"));
}
/// A pipeline with static samplers is made, not looked up in the library
/// (with or without a host); the guard gives the setting back.
#[test]
fn pipelines_with_static_samplers_skip_the_library() {
let looked = std::cell::Cell::new(false);
{
let _guard = with_static_samplers(true);
let made = pipeline(
"with static samplers",
|_, _| {
looked.set(true);
Err(windows::core::Error::empty())
},
|| Err(windows::core::Error::empty()),
);
assert!(made.is_err());
assert!(!looked.get(), "the library was asked");
{
let _inner = with_static_samplers(false);
assert!(!STATIC_SAMPLERS.get());
}
assert!(STATIC_SAMPLERS.get());
}
assert!(!STATIC_SAMPLERS.get());
}
#[test]
fn a_rejected_blob_gives_an_empty_library() {
let Some(device) = crate::host::host().and_then(|h| h.device.cast::<ID3D12Device1>().ok()) else {