diff --git a/crates/victory-gpu/examples/lut3d_probe.rs b/crates/victory-gpu/examples/lut3d_probe.rs new file mode 100644 index 0000000..917e822 --- /dev/null +++ b/crates/victory-gpu/examples/lut3d_probe.rs @@ -0,0 +1,711 @@ +//! Does the card filter a 33^3 RGBA16F Texture3D (render target and unordered +//! access, written by a compute pass) through a linear static sampler, as +//! PS_CompositeNMB reads its colour-grading tables? Prints 64 samples along +//! the diagonal of three cells, with the linear and the nearest value. +//! +//! cargo run --release -p victory-gpu --example lut3d_probe +//! +//! Switches (environment), each a way the layer's use differs from the plain case: +//! - `SM66=1`: the sampling program as the layer's are, SM 6.6, the texture read +//! from the heap by index, the root flags of the layer's root signatures and the +//! composite's three static samplers (point, linear, trilinear; compiled with +//! `dxc.exe`, `DXC=` or the Windows SDK's). +//! - `LIB66_STORE=` / `LIB66_LOAD=` (with `SM66`): that pipeline stored +//! in a pipeline library written to the file / loaded from it. +//! - `SHIFT=`: n other static samplers made (and kept) first. `SWAP66=1`: a root +//! signature with the composite's samplers in another order made first. +//! - `SAMPLERS=`, `PAST=`: a bound heap of n point samplers, k written past it. +//! - `KEEP_UAV=1`: the texture left in the unordered access state. +//! - `MANY_ROOTS=` (`MANY_SAME`, `MANY_DROP`, `MANY_TWIN`, `BIND_MANY`): n more +//! root signatures with static samplers (the card's table holds 2,032 distinct +//! ones: the 2,032nd removes the device with DXGI_ERROR_DRIVER_INTERNAL_ERROR). +//! - `BG_COMMIT=1`: the driver's background recompiles finished first. +//! +//! Found with it (2026-11-26, 4070 SUPER): every case filters, except a pipeline +//! loaded from a library in a process that made other static samplers first +//! (`SM66=1 LIB66_STORE=a.lib`, then `SM66=1 SHIFT=10 LIB66_LOAD=a.lib`): every +//! sample is a table entry. See `pipeline::library::with_static_samplers`. +use windows::Win32::Graphics::Direct3D::Fxc::D3DCompile; +use windows::Win32::Graphics::Direct3D::{D3D_FEATURE_LEVEL_12_0, ID3DBlob}; +use windows::Win32::Graphics::Direct3D12::*; +use windows::Win32::Graphics::Dxgi::Common::*; +use windows::Win32::Graphics::Dxgi::*; +use windows::Win32::System::Threading::{CreateEventW, INFINITE, WaitForSingleObject}; +use windows::core::{Interface, PCSTR, s}; + +const SRC: &str = r#" +#define RS "DescriptorTable(UAV(u0), SRV(t0)), UAV(u1), RootConstants(num32BitConstants=4, b0), StaticSampler(s0, filter=FILTER_MIN_MAG_LINEAR_MIP_POINT, addressU=TEXTURE_ADDRESS_CLAMP, addressV=TEXTURE_ADDRESS_CLAMP, addressW=TEXTURE_ADDRESS_CLAMP, maxLOD=12)" +RWTexture3D lut : register(u0); +Texture3D tex : register(t0); +SamplerState smp : register(s0); +RWStructuredBuffer outb : register(u1); +cbuffer C : register(b0) { uint mode; uint a; uint b; uint c; }; +[RootSignature(RS)] [numthreads(1, 1, 1)] +void fill(uint3 id : SV_DispatchThreadID) { lut[id] = float4(id.x / 32.0, id.y / 32.0, id.z / 32.0, 1); } +[RootSignature(RS)] [numthreads(64, 1, 1)] +void probe(uint3 id : SV_DispatchThreadID) { + float u = (id.x + 0.5) / 64.0 * (3.0 / 33.0) + 10.0 / 33.0; + outb[id.x] = tex.SampleLevel(smp, float3(u, u, u), 0); +} +"#; + +/// The composite's way: SM 6.6, the texture from the heap (slot 1), its three +/// static samplers alike but for the filter. +const SRC66: &str = r#" +#define SS(r, f) "StaticSampler(" r ", filter=" f ", addressU=TEXTURE_ADDRESS_CLAMP, addressV=TEXTURE_ADDRESS_CLAMP, addressW=TEXTURE_ADDRESS_CLAMP, maxAnisotropy=1, comparisonFunc=COMPARISON_NEVER, borderColor=STATIC_BORDER_COLOR_TRANSPARENT_BLACK, minLOD=0, maxLOD=12)" +#define RS "RootFlags(CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | SAMPLER_HEAP_DIRECTLY_INDEXED), UAV(u1), " SS("s0", "FILTER_MIN_MAG_MIP_POINT") ", " SS("s1", "FILTER_MIN_MAG_LINEAR_MIP_POINT") ", " SS("s2", "FILTER_MIN_MAG_MIP_LINEAR") +SamplerState p0 : register(s0); +SamplerState p1 : register(s1); +SamplerState p2 : register(s2); +RWStructuredBuffer outb : register(u1); +[RootSignature(RS)] [numthreads(64, 1, 1)] +void probe(uint3 id : SV_DispatchThreadID) { + Texture3D tex = ResourceDescriptorHeap[1]; + float u = (id.x + 0.5) / 64.0 * (3.0 / 33.0) + 10.0 / 33.0; + float4 a = tex.SampleLevel(p0, float3(0.5, 0.5, 0.5), 0); + float4 b = tex.SampleLevel(p1, float3(u, u, u), 0); + float4 c = tex.SampleLevel(p2, float3(0.5, u, 0.5), 0); + outb[id.x] = float4(b.x, b.y, b.z, a.x * 0.0 + c.y * 0.0 + b.w); +} +"#; + +/// `source` compiled by dxc.exe for compute, entry `probe`. +fn dxc(source: &str) -> Vec { + let exe = std::env::var_os("DXC").map_or_else( + || { + std::path::PathBuf::from( + r"C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x64\dxc.exe", + ) + }, + std::path::PathBuf::from, + ); + let dir = std::env::temp_dir(); + let (hlsl, cso) = ( + dir.join("lut3d_probe66.hlsl"), + dir.join("lut3d_probe66.cso"), + ); + std::fs::write(&hlsl, source).expect("hlsl written"); + let out = std::process::Command::new(exe) + .args(["-T", "cs_6_6", "-E", "probe", "-Fo"]) + .arg(&cso) + .arg(&hlsl) + .output() + .expect("dxc.exe runs"); + assert!( + out.status.success(), + "{}", + String::from_utf8_lossy(&out.stderr) + ); + std::fs::read(&cso).expect("cso") +} + +fn compile(entry: PCSTR) -> Vec { + let mut code: Option = None; + let mut errors: Option = None; + unsafe { + let r = D3DCompile( + SRC.as_ptr().cast(), + SRC.len(), + s!("probe"), + None, + None, + entry, + s!("cs_5_1"), + 0, + 0, + &mut code, + Some(&mut errors), + ); + if r.is_err() { + let e = errors.unwrap(); + panic!( + "{}", + String::from_utf8_lossy(std::slice::from_raw_parts( + e.GetBufferPointer().cast::(), + e.GetBufferSize() + )) + ); + } + let c = code.unwrap(); + std::slice::from_raw_parts(c.GetBufferPointer().cast::(), c.GetBufferSize()).to_vec() + } +} + +fn main() -> windows::core::Result<()> { + unsafe { + let factory: IDXGIFactory4 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0))?; + let mut device: Option = None; + for i in 0.. { + let Ok(adapter) = factory.EnumAdapters1(i) else { + break; + }; + let desc = adapter.GetDesc1()?; + let name = String::from_utf16_lossy(&desc.Description); + if name.contains("NVIDIA") { + println!("adapter {}", name.trim_end_matches('\0')); + D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_12_0, &mut device)?; + break; + } + } + let device = device.expect("an NVIDIA adapter"); + let fill = compile(s!("fill")); + let probe = compile(s!("probe")); + // SHIFT=: n root signatures with static point samplers of their own made (and + // kept) before the probe's, so its sampler lands elsewhere in the card's table. + let mut shifted: Vec = Vec::new(); + let shift = std::env::var("SHIFT") + .ok() + .and_then(|v| v.parse::().ok()) + .unwrap_or(0); + for i in 0..shift { + let sampler = D3D12_STATIC_SAMPLER_DESC { + Filter: D3D12_FILTER_MIN_MAG_MIP_POINT, + AddressU: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + AddressV: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + AddressW: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + MipLODBias: i as f32 / 64.0, + MaxAnisotropy: 1, + MaxLOD: 12.0, + ..Default::default() + }; + let desc = D3D12_VERSIONED_ROOT_SIGNATURE_DESC { + Version: D3D_ROOT_SIGNATURE_VERSION_1_1, + Anonymous: D3D12_VERSIONED_ROOT_SIGNATURE_DESC_0 { + Desc_1_1: D3D12_ROOT_SIGNATURE_DESC1 { + NumStaticSamplers: 1, + pStaticSamplers: &sampler, + ..Default::default() + }, + }, + }; + let mut blob: Option = None; + D3D12SerializeVersionedRootSignature(&desc, &mut blob, None)?; + let blob = blob.unwrap(); + let bytes = std::slice::from_raw_parts( + blob.GetBufferPointer().cast::(), + blob.GetBufferSize(), + ); + shifted.push(device.CreateRootSignature(0, bytes)?); + } + if shift > 0 { + println!("{shift} root signatures with other samplers made first"); + } + let root: ID3D12RootSignature = device.CreateRootSignature(0, &fill)?; + let pso = |code: &[u8]| -> windows::core::Result { + device.CreateComputePipelineState(&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() + }) + }; + let fill_pso = pso(&fill)?; + // LIB_STORE=: the probe's pipeline stored in a pipeline library written + // there; LIB_LOAD=: the probe's pipeline loaded from such a library. + let device1: ID3D12Device1 = device.cast()?; + let probe_pso = if let Some(path) = std::env::var_os("LIB_LOAD") { + let bytes = std::fs::read(&path).expect("library file"); + let library: ID3D12PipelineLibrary = device1.CreatePipelineLibrary(&bytes)?; + let desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: std::mem::transmute_copy(&root), + CS: D3D12_SHADER_BYTECODE { + pShaderBytecode: probe.as_ptr().cast(), + BytecodeLength: probe.len(), + }, + ..Default::default() + }; + let p: ID3D12PipelineState = + library.LoadComputePipeline(windows::core::w!("probe"), &desc)?; + println!("probe pipeline loaded from the library"); + std::mem::forget(bytes); // the library points into them + std::mem::forget(library); + p + } else { + let p = pso(&probe)?; + if let Some(path) = std::env::var_os("LIB_STORE") { + let library: ID3D12PipelineLibrary = device1.CreatePipelineLibrary(&[])?; + library.StorePipeline(windows::core::w!("probe"), &p)?; + let mut bytes = vec![0u8; library.GetSerializedSize()]; + library.Serialize(&mut bytes)?; + std::fs::write(&path, &bytes).expect("library written"); + println!("probe pipeline stored ({} bytes)", bytes.len()); + } + p + }; + // MANY_ROOTS=: n more root signatures, each with one static point sampler of + // its own (a distinct LOD bias), and a pipeline for each; MANY_SAME=1: all alike. + let mut many: Vec<(ID3D12RootSignature, ID3D12PipelineState)> = Vec::new(); + if let Some(n) = std::env::var("MANY_ROOTS") + .ok() + .and_then(|v| v.parse::().ok()) + { + let same = std::env::var_os("MANY_SAME").is_some(); + let nop_src = "[numthreads(1, 1, 1)] void nop() {}"; + let mut code: Option = None; + D3DCompile( + nop_src.as_ptr().cast(), + nop_src.len(), + s!("nop"), + None, + None, + s!("nop"), + s!("cs_5_1"), + 0, + 0, + &mut code, + None, + )?; + let code = code.unwrap(); + let nop = std::slice::from_raw_parts( + code.GetBufferPointer().cast::(), + code.GetBufferSize(), + ) + .to_vec(); + // MANY_TWIN=k: first k root signatures with a sampler just like the probe's + // (as the HLSL string gives it), made and let go. + let twins = std::env::var("MANY_TWIN") + .ok() + .and_then(|v| v.parse::().ok()) + .unwrap_or(0); + for _ in 0..twins { + let sampler = D3D12_STATIC_SAMPLER_DESC { + Filter: D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT, + AddressU: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + AddressV: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + AddressW: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + MipLODBias: 0.0, + MaxAnisotropy: 16, + ComparisonFunc: D3D12_COMPARISON_FUNC_LESS_EQUAL, + BorderColor: D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE, + MinLOD: 0.0, + MaxLOD: 12.0, + ShaderRegister: 0, + RegisterSpace: 0, + ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, + }; + let desc = D3D12_VERSIONED_ROOT_SIGNATURE_DESC { + Version: D3D_ROOT_SIGNATURE_VERSION_1_1, + Anonymous: D3D12_VERSIONED_ROOT_SIGNATURE_DESC_0 { + Desc_1_1: D3D12_ROOT_SIGNATURE_DESC1 { + NumStaticSamplers: 1, + pStaticSamplers: &sampler, + ..Default::default() + }, + }, + }; + let mut blob: Option = None; + D3D12SerializeVersionedRootSignature(&desc, &mut blob, None)?; + let blob = blob.unwrap(); + let bytes = std::slice::from_raw_parts( + blob.GetBufferPointer().cast::(), + blob.GetBufferSize(), + ); + let twin: ID3D12RootSignature = device.CreateRootSignature(0, bytes)?; + drop(twin); + } + if twins > 0 { + println!("made and let go {twins} root signatures with the probe's sampler"); + } + for i in 0..n { + let sampler = D3D12_STATIC_SAMPLER_DESC { + Filter: D3D12_FILTER_MIN_MAG_MIP_POINT, + AddressU: D3D12_TEXTURE_ADDRESS_MODE_WRAP, + AddressV: D3D12_TEXTURE_ADDRESS_MODE_WRAP, + AddressW: D3D12_TEXTURE_ADDRESS_MODE_WRAP, + MipLODBias: if same { 0.0 } else { (i % 1000) as f32 / 100.0 }, + MaxAnisotropy: 1, + MinLOD: if same { 0.0 } else { (i / 1000) as f32 }, + MaxLOD: 12.0, + ShaderRegister: 0, + ..Default::default() + }; + let desc = D3D12_VERSIONED_ROOT_SIGNATURE_DESC { + Version: D3D_ROOT_SIGNATURE_VERSION_1_1, + Anonymous: D3D12_VERSIONED_ROOT_SIGNATURE_DESC_0 { + Desc_1_1: D3D12_ROOT_SIGNATURE_DESC1 { + NumStaticSamplers: 1, + pStaticSamplers: &sampler, + ..Default::default() + }, + }, + }; + let mut blob: Option = None; + D3D12SerializeVersionedRootSignature(&desc, &mut blob, None)?; + let blob = blob.unwrap(); + let bytes = std::slice::from_raw_parts( + blob.GetBufferPointer().cast::(), + blob.GetBufferSize(), + ); + let r: ID3D12RootSignature = match device.CreateRootSignature(0, bytes) { + Ok(r) => r, + Err(e) => { + println!( + "root signature {i} refused: {e:?} removed {:?}", + device.GetDeviceRemovedReason() + ); + return Ok(()); + } + }; + let p: ID3D12PipelineState = + match device.CreateComputePipelineState(&D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: std::mem::transmute_copy(&r), + CS: D3D12_SHADER_BYTECODE { + pShaderBytecode: nop.as_ptr().cast(), + BytecodeLength: nop.len(), + }, + ..Default::default() + }) { + Ok(p) => p, + Err(e) => { + println!( + "pipeline {i} refused: {e:?} removed {:?}", + device.GetDeviceRemovedReason() + ); + return Ok(()); + } + }; + // MANY_DROP=1: each released at once (only made and let go). + if std::env::var_os("MANY_DROP").is_none() { + many.push((r, p)); + } + } + println!("made {n} more root signatures with static point samplers (alike: {same})"); + } + let heap_props = |t| D3D12_HEAP_PROPERTIES { + Type: t, + ..Default::default() + }; + let mut tex: Option = None; + device.CreateCommittedResource( + &heap_props(D3D12_HEAP_TYPE_DEFAULT), + D3D12_HEAP_FLAG_NONE, + &D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE3D, + Width: 33, + Height: 33, + DepthOrArraySize: 33, + MipLevels: 1, + Format: DXGI_FORMAT_R16G16B16A16_FLOAT, + SampleDesc: DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + Flags: D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET + | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, + ..Default::default() + }, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + None, + &mut tex, + )?; + let tex = tex.unwrap(); + let buffer = |t, bytes: u64, flags, state| -> windows::core::Result { + let mut b: Option = None; + device.CreateCommittedResource( + &heap_props(t), + D3D12_HEAP_FLAG_NONE, + &D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, + Width: bytes, + Height: 1, + DepthOrArraySize: 1, + MipLevels: 1, + SampleDesc: DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, + Flags: flags, + ..Default::default() + }, + state, + None, + &mut b, + )?; + Ok(b.unwrap()) + }; + let out = buffer( + D3D12_HEAP_TYPE_DEFAULT, + 64 * 16, + D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + )?; + let back = buffer( + D3D12_HEAP_TYPE_READBACK, + 64 * 16, + D3D12_RESOURCE_FLAG_NONE, + D3D12_RESOURCE_STATE_COPY_DEST, + )?; + let heap: ID3D12DescriptorHeap = + device.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { + Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + NumDescriptors: 2, + Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, + ..Default::default() + })?; + let step = device.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) + as usize; + let cpu = heap.GetCPUDescriptorHandleForHeapStart(); + let mut uav = D3D12_UNORDERED_ACCESS_VIEW_DESC { + Format: DXGI_FORMAT_R16G16B16A16_FLOAT, + ViewDimension: D3D12_UAV_DIMENSION_TEXTURE3D, + ..Default::default() + }; + uav.Anonymous.Texture3D = D3D12_TEX3D_UAV { + MipSlice: 0, + FirstWSlice: 0, + WSize: 33, + }; + device.CreateUnorderedAccessView(&tex, None, Some(&uav), cpu); + let mut srv = D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: DXGI_FORMAT_R16G16B16A16_FLOAT, + ViewDimension: D3D12_SRV_DIMENSION_TEXTURE3D, + Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + ..Default::default() + }; + srv.Anonymous.Texture3D = D3D12_TEX3D_SRV { + MostDetailedMip: 0, + MipLevels: 1, + ResourceMinLODClamp: 0.0, + }; + device.CreateShaderResourceView( + &tex, + Some(&srv), + D3D12_CPU_DESCRIPTOR_HANDLE { + ptr: cpu.ptr + step, + }, + ); + let queue: ID3D12CommandQueue = device.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { + Type: D3D12_COMMAND_LIST_TYPE_DIRECT, + ..Default::default() + })?; + let alloc: ID3D12CommandAllocator = + device.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT)?; + let list: ID3D12GraphicsCommandList = + device.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None)?; + // SAMPLERS=: also bind a shader-visible sampler heap of n point samplers. + let samplers: Option = match std::env::var("SAMPLERS") + .ok() + .and_then(|v| v.parse::().ok()) + { + Some(n) => { + let h: ID3D12DescriptorHeap = + device.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { + Type: D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, + NumDescriptors: n, + Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, + ..Default::default() + })?; + let sstep = device + .GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER) + as usize; + let start = h.GetCPUDescriptorHandleForHeapStart().ptr; + // PAST=: also k point samplers written past the heap's end (as a + // handle computed beyond it would). + let past = std::env::var("PAST") + .ok() + .and_then(|v| v.parse::().ok()) + .unwrap_or(0); + for i in 0..n as usize + past { + device.CreateSampler( + &D3D12_SAMPLER_DESC { + Filter: D3D12_FILTER_MIN_MAG_MIP_POINT, + AddressU: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + AddressV: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + AddressW: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + MaxLOD: 12.0, + ..Default::default() + }, + D3D12_CPU_DESCRIPTOR_HANDLE { + ptr: start + i * sstep, + }, + ); + } + println!("bound a sampler heap of {n} point samplers"); + Some(h) + } + None => None, + }; + match &samplers { + Some(s) => list.SetDescriptorHeaps(&[Some(heap.clone()), Some(s.clone())]), + None => list.SetDescriptorHeaps(&[Some(heap.clone())]), + } + // BIND_MANY=1: each of those bound and run once before the probe. + if std::env::var_os("BIND_MANY").is_some() { + for (r, p) in &many { + list.SetComputeRootSignature(r); + list.SetPipelineState(p); + list.Dispatch(1, 1, 1); + } + } + list.SetComputeRootSignature(&root); + list.SetComputeRootDescriptorTable(0, heap.GetGPUDescriptorHandleForHeapStart()); + list.SetComputeRootUnorderedAccessView(1, out.GetGPUVirtualAddress()); + list.SetPipelineState(&fill_pso); + list.Dispatch(33, 33, 33); + let transition = D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Anonymous: D3D12_RESOURCE_BARRIER_0 { + Transition: std::mem::ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: std::mem::transmute_copy(&tex), + Subresource: D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, + StateBefore: D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + StateAfter: D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, + }), + }, + ..Default::default() + }; + if std::env::var_os("KEEP_UAV").is_none() { + list.ResourceBarrier(&[transition]); + } else { + list.ResourceBarrier(&[D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_UAV, + ..Default::default() + }]); + } + // SM66=: the probe as an SM 6.6 shader reading the SRV from the heap by index + // (slot 1), with the root flags the layer's root signatures have. + let sm66 = std::env::var_os("SM66").map(|_| dxc(SRC66)); + let _keep66; + if let Some(code) = &sm66 { + // SWAP66=1: first a root signature with the same three samplers in the other + // order (s1's and s2's kinds made before s0's), kept. + let _swapped = if std::env::var_os("SWAP66").is_some() { + let mk = |reg: u32, filter: D3D12_FILTER| D3D12_STATIC_SAMPLER_DESC { + Filter: filter, + AddressU: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + AddressV: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + AddressW: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, + MaxAnisotropy: 1, + ComparisonFunc: D3D12_COMPARISON_FUNC_NEVER, + BorderColor: D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK, + MaxLOD: 12.0, + ShaderRegister: reg, + ..Default::default() + }; + let ss = [ + mk(0, D3D12_FILTER_MIN_MAG_MIP_LINEAR), + mk(1, D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT), + mk(2, D3D12_FILTER_MIN_MAG_MIP_POINT), + ]; + let desc = D3D12_VERSIONED_ROOT_SIGNATURE_DESC { + Version: D3D_ROOT_SIGNATURE_VERSION_1_1, + Anonymous: D3D12_VERSIONED_ROOT_SIGNATURE_DESC_0 { + Desc_1_1: D3D12_ROOT_SIGNATURE_DESC1 { + NumStaticSamplers: 3, + pStaticSamplers: ss.as_ptr(), + ..Default::default() + }, + }, + }; + let mut blob: Option = None; + D3D12SerializeVersionedRootSignature(&desc, &mut blob, None)?; + let blob = blob.unwrap(); + let bytes = std::slice::from_raw_parts( + blob.GetBufferPointer().cast::(), + blob.GetBufferSize(), + ); + println!("samplers made in the other order first"); + Some(device.CreateRootSignature::(0, bytes)?) + } else { + None + }; + let root66: ID3D12RootSignature = device.CreateRootSignature(0, code)?; + let desc66 = D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: std::mem::transmute_copy(&root66), + CS: D3D12_SHADER_BYTECODE { + pShaderBytecode: code.as_ptr().cast(), + BytecodeLength: code.len(), + }, + ..Default::default() + }; + let device1: ID3D12Device1 = device.cast()?; + let pso66: ID3D12PipelineState = if let Some(path) = std::env::var_os("LIB66_LOAD") { + let bytes = std::fs::read(&path).expect("library file"); + let library: ID3D12PipelineLibrary = device1.CreatePipelineLibrary(&bytes)?; + let p = library.LoadComputePipeline(windows::core::w!("probe66"), &desc66)?; + println!("probe66 loaded from the library"); + std::mem::forget(bytes); + std::mem::forget(library); + p + } else { + let p: ID3D12PipelineState = device.CreateComputePipelineState(&desc66)?; + if let Some(path) = std::env::var_os("LIB66_STORE") { + let library: ID3D12PipelineLibrary = device1.CreatePipelineLibrary(&[])?; + library.StorePipeline(windows::core::w!("probe66"), &p)?; + let mut bytes = vec![0u8; library.GetSerializedSize()]; + library.Serialize(&mut bytes)?; + std::fs::write(&path, &bytes).expect("library written"); + println!("probe66 stored"); + } + p + }; + // BG_COMMIT=1: the driver's background recompiles of the pipelines made so + // far are finished and put in place before the probe runs. + if std::env::var_os("BG_COMMIT").is_some() { + let device6: ID3D12Device6 = device.cast()?; + let mut further = windows::core::BOOL(0); + device6.SetBackgroundProcessingMode( + D3D12_BACKGROUND_PROCESSING_MODE_ALLOWED, + D3D12_MEASUREMENTS_ACTION_COMMIT_RESULTS_HIGH_PRIORITY, + None, + Some(&mut further), + )?; + println!( + "background work committed (further measurements wanted: {})", + further.as_bool() + ); + } + list.SetComputeRootSignature(&root66); + list.SetComputeRootUnorderedAccessView(0, out.GetGPUVirtualAddress()); + list.SetPipelineState(&pso66); + println!("SM 6.6 heap-indexed probe"); + _keep66 = Some((root66, pso66)); + } else { + list.SetPipelineState(&probe_pso); + _keep66 = None; + } + list.Dispatch(1, 1, 1); + let to_copy = D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Anonymous: D3D12_RESOURCE_BARRIER_0 { + Transition: std::mem::ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: std::mem::transmute_copy(&out), + Subresource: D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, + StateBefore: D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + StateAfter: D3D12_RESOURCE_STATE_COPY_SOURCE, + }), + }, + ..Default::default() + }; + list.ResourceBarrier(&[to_copy]); + list.CopyResource(&back, &out); + list.Close()?; + queue.ExecuteCommandLists(&[Some(list.cast()?)]); + let fence: ID3D12Fence = device.CreateFence(0, D3D12_FENCE_FLAG_NONE)?; + queue.Signal(&fence, 1)?; + let event = CreateEventW(None, false, false, None)?; + fence.SetEventOnCompletion(1, event)?; + WaitForSingleObject(event, INFINITE); + let mut data = std::ptr::null_mut(); + back.Map(0, None, Some(&mut data))?; + let v = std::slice::from_raw_parts(data.cast::<[f32; 4]>(), 64); + for (i, t) in v.iter().enumerate() { + let u = (i as f32 + 0.5) / 64.0 * (3.0 / 33.0) + 10.0 / 33.0; + let linear = (u * 33.0 - 0.5) / 32.0; + let nearest = (u * 33.0).floor() / 32.0; + println!( + "{i:2} u*33={:.3} got {:.4} {:.4} {:.4} linear {:.4} nearest {:.4}", + u * 33.0, + t[0], + t[1], + t[2], + linear, + nearest + ); + } + } + Ok(()) +} diff --git a/crates/victory-gpu/src/ambient.rs b/crates/victory-gpu/src/ambient.rs index 8a3d46e..2b5650c 100644 --- a/crates/victory-gpu/src/ambient.rs +++ b/crates/victory-gpu/src/ambient.rs @@ -29,6 +29,8 @@ //! - `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` @@ -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 = 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 { @@ -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 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 cells : register(u1); // Three a point: red, green and blue, each a mean and a direction. RWStructuredBuffer 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 { + 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() { diff --git a/crates/victory-gpu/src/pipeline/host.rs b/crates/victory-gpu/src/pipeline/host.rs index 9c68fd6..2e2d0bd 100644 --- a/crates/victory-gpu/src/pipeline/host.rs +++ b/crates/victory-gpu/src/pipeline/host.rs @@ -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(), diff --git a/crates/victory-gpu/src/pipeline/library.rs b/crates/victory-gpu/src/pipeline/library.rs index a7e974b..f81db57 100644 --- a/crates/victory-gpu/src/pipeline/library.rs +++ b/crates/victory-gpu/src/pipeline/library.rs @@ -255,6 +255,55 @@ static CALLS: Mutex> = Mutex::new(Vec::new thread_local! { /// The pipeline this thread is making (its shaders), for [`CALLS`]. static LABEL: std::cell::RefCell = 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 = 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 = 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, ) -> windows::core::Result { 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::().ok()) else {