From 181e7df607e5edfc7d95f971d3fedabb515ff13e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 28 May 2026 20:05:03 +0100 Subject: [PATCH] Route tube fields through reservoir resolve --- docs/hlsl-renderer.md | 4 +- docs/perfect-machine-architecture.md | 35 +++- docs/rendering-teardown-rebuild-protocol.md | 43 +++- ...stochastic-transparent-surface-pipeline.md | 2 +- docs/temporal-sdf-gaussian-field.md | 7 +- docs/temporal-spatial-evidence-reservoir.md | 89 ++++++-- src/Aquarium.Engine/Render/D3D12Renderer.cs | 29 ++- .../Render/Shaders/D3D12Post.hlsl | 5 +- .../Render/Shaders/D3D12TubeField.hlsl | 198 +++++++++++++----- 9 files changed, 304 insertions(+), 108 deletions(-) diff --git a/docs/hlsl-renderer.md b/docs/hlsl-renderer.md index c33f93f..4b4e720 100644 --- a/docs/hlsl-renderer.md +++ b/docs/hlsl-renderer.md @@ -79,7 +79,7 @@ Fensalir has a GPU-resident fractal/field lane: - Form/Appearance/Transport reservoirs keep stable temporal evidence; - opaque signed-distance splats and transparent density/extinction splats render through separate pipeline states over shared packed buffers; -- guide output follows the TAA schema: confidence, sample age, domain validity, +- guide output follows the reservoir guide schema: confidence, sample age, domain validity, invalidation code. The receipt harness lives in `tools/Aquarium.Fractal.Receipt`. @@ -92,7 +92,7 @@ normal, coverage, and control signals stay coherent. Debug modes include final color, raw current scene, history, history age, history weight, coverage/step ratio, field identity, bloom contribution, -exposed luminance, proxy identity, proxy step count, and reservoir/TAA guide +exposed luminance, proxy identity, proxy step count, and reservoir guide views. Startup mode can be set with `--render-debug` or `AQUARIUM_RENDER_DEBUG_MODE`. diff --git a/docs/perfect-machine-architecture.md b/docs/perfect-machine-architecture.md index c66a8bb..e014cd3 100644 --- a/docs/perfect-machine-architecture.md +++ b/docs/perfect-machine-architecture.md @@ -42,6 +42,15 @@ This architecture is not invented from the floorboards: friendly, but Aquarium should preserve deterministic summaries and bounds as safety authority. +Aquarium diverges from pure ReSTIR at the primary-visibility boundary. ReSTIR +usually reuses lighting or path candidates after a visible surface is known. +Aquarium/Fensalir often samples the visible field candidate itself. Local Form +producers therefore generate candidate visibility first, and the reservoir owns +the temporal-antialiasing-shaped decision about which field candidate persists +into presentation. There is no separate TAA organ in the target architecture: +reservoir resolve reconstructs smooth presentation directly from selected field +evidence. + ## Prime Invariants - Authored semantic domains are the source of truth. @@ -55,7 +64,8 @@ This architecture is not invented from the floorboards: visibility safety, or calibration safety. - Missing children render through parent summaries. A frame does not wait for SSD. -- TAA owns pixel history, not producer identity. +- Reservoir resolve owns temporal antialiasing and reconstruction; no separate + TAA pass owns producer identity or temporally reused field presentation. - Mimir sensor fusion and fractal rendering share the evidence machinery; they do not share client policy. @@ -73,7 +83,7 @@ Authored intent, resident mesh, direct SDF field, or live sensor input -> Residency/page scheduling -> Backend packet lowering -> direct SDF, mesh, volume, and/or field splat passes --> Temporal resolve guide buffers +-> Reservoir resolve guide buffers -> Debug/evidence telemetry ``` @@ -99,7 +109,7 @@ Aquarium.Engine.SensorFusion Aquarium.Engine.Render D3D12 resources, page tables, structured buffers, field splat passes, - surface/volume resolves, TAA guide buffers, debug visualization. + surface/volume resolves, reservoir guide buffers, debug visualization. Aquarium.Zyphos World policy, cube-sphere/tile roots, planet grammar seeds, setting-safe @@ -225,8 +235,9 @@ SampleAge DomainKey ``` -The reservoir owns resampling math. It does not own stable tracks, packet -lowering, or TAA history. +The reservoir owns resampling math. It does not own stable tracks or packet +lowering. Reservoir resolve history is the presentation side of the same +spatiotemporal organ, not a separate TAA owner. ### Occupancy Graph @@ -440,9 +451,9 @@ the direct representation has become too expensive. Future mesh lowering fits the same contract; it is another backend for the same evidence, not another authoring language. -### 10. TAA Guide Integration +### 10. Reservoir Resolve Integration -TAA consumes guide signals: +Reservoir resolve consumes guide signals: - reservoir confidence; - sample age; @@ -451,7 +462,11 @@ TAA consumes guide signals: - invalidation reason; - field id and motion. -TAA does not own reservoirs. It only decides how much pixel history may survive. +There is no separate TAA owner. Reservoir resolve decides how much temporal +history may survive after the field reservoir has selected and validated +presentation candidates. If downstream pixel filtering is required to make an +unstable field look stable, the Form producer or reservoir validation has +failed upstream. ## Resource Tradeoffs @@ -493,7 +508,7 @@ views. It must not walk the authored grammar tree. 10. Queue missing pages; keep parent summaries active. 11. Lower selected evidence to direct SDF, mesh, volume, or field-splat packets. 12. Render surface and/or transparent field passes into scene and guide targets. -13. Resolve with TAA guide buffers. +13. Resolve with reservoir guide buffers. 14. Emit debug telemetry and evidence logs. ``` @@ -543,7 +558,7 @@ Performance fixtures: ### Phase A: Architecture Freeze Deliver this document, the public thesis article, updated memory, and a cut -line for the TAA guide-buffer fork. +line for the reservoir guide-buffer fork. ### Phase B: Explicit Reservoir Guide Layout diff --git a/docs/rendering-teardown-rebuild-protocol.md b/docs/rendering-teardown-rebuild-protocol.md index f4fa11a..2a33b81 100644 --- a/docs/rendering-teardown-rebuild-protocol.md +++ b/docs/rendering-teardown-rebuild-protocol.md @@ -43,6 +43,10 @@ Owner: - `FieldEvidenceMachine` owns the decision that something is visible, reusable, resident, or temporally valid. +- The spatiotemporal field reservoir is the shared rendering organ for + reusable claims. Tube fields, direct SDF envelopes, meshes, heightfields, + volumes, splats, and sensor-fusion fields are claim producers or lowerings; + none of them own final composition as a private renderer. Inputs: @@ -58,6 +62,8 @@ Outputs: - canonical field-claim streams grouped by Form, Appearance, and Transport; - selected candidate reservoirs and stable track updates where persistent identity exists; +- temporally reused field presentation decisions for claims whose visible + surface or volume candidate is itself the sampled value; - residency/page requests and selected cuts; - backend packet streams for direct SDF, mesh, splat, height/tile, volume, tube, debug, and future paths; @@ -70,16 +76,20 @@ Derived state: postprocess targets are compiled lowerings or diagnostics. - The old `SDF/PBR/Radiosity` reservoir naming is backend vocabulary. The architectural layers are `Form`, `Appearance`, and `Transport`. -- TAA history is pixel memory. It is not producer identity and not stable - spatial evidence. +- Legacy TAA history is demoted to reservoir resolve history. It is not + producer identity, stable spatial evidence, or a separate owner of temporally + reused field presentation. Forbidden writers: - alpha blending, quad UVs, screen-space strips, billboard radii, or debug draw geometry deciding visible truth; - client repos submitting backend packets as if they were semantic claims; +- payload-specific paths such as TubeField naming themselves as the reservoir + renderer instead of feeding candidate claims into the shared field reservoir; - Mimir owning a render graph or a second stable evidence cache; -- TAA repairing missing identity after the fact; +- any downstream pixel-history/TAA pass repairing missing identity after the + fact; - backend-specific payload conventions when a typed lowering contract exists. Shared paths: @@ -89,6 +99,10 @@ Shared paths: density/extinction volumes, mesh geometry, sensor fusion, and debug overlays all use the same claim -> candidate -> validation -> lowering contract when they want temporal reuse. +- Payload-specific code may own intersection, envelope evaluation, geometry + expansion, material sampling, and proposal PDFs for its own claim family. It + may not own final field visibility or bypass the reservoir when the output is + meant to participate in temporally reused presentation. Deletion line: @@ -104,7 +118,7 @@ Deletion line: Authored intent / direct field / mesh / Mimir observation -> Domain binding -> Field claim normalization --> Candidate generation +-> Local visibility candidate generation -> Target evaluation -> Resampled-importance reservoir -> Temporal reuse validation @@ -113,7 +127,7 @@ Authored intent / direct field / mesh / Mimir observation -> Occupancy graph and residency/page scheduling -> Backend lowering -> Evidence-writing render/compute passes --> TAA guide validation +-> Reservoir resolve/reconstruction -> Presentation and debug telemetry ``` @@ -157,6 +171,19 @@ tempted to smuggle authority inside its vertex layout. Candidate generators propose work. They do not own final visibility. +For ReSTIR-style lighting, primary surface visibility is usually already known. +Fensalir's Form fields often do not have that luxury: the candidate being +sampled is the visible field surface or volume itself. In that case, the +producer must generate local visibility candidates first, with stable identity, +support bounds, depth/travel, coverage, material encoding, target value, and +source/proposal policy. The shared reservoir then owns reuse and final +presentation membership. + +This is the sanctioned divergence from pure ReSTIR and the reason there is no +separate TAA owner. Reservoir update and reservoir resolve own temporal +stability, antialiasing, and reconstruction. A downstream pixel-history pass +may not decide which field candidate existed. + Targets are scalar contribution estimates: projected form error, curvature, material delta, extinction contribution, radiance contribution, sensor information gain, visible coverage, or calibration confidence. Probabilistic @@ -211,10 +238,10 @@ depth when depth participates in validation If a lowering cannot provide these, it is a fallback draw. It can help humans debug. It cannot claim to be the Perfect Machine. -### 6. Resolve And Presentation +### 6. Reservoir Resolve And Presentation -TAA consumes guide data and validates pixel history. It does not recover truth -from broken producers. +Reservoir resolve consumes guide data and reconstructs presentation from the +selected field evidence. It does not recover truth from broken producers. Post, bloom, tonemapping, debug overlays, and output publication operate after the evidence lanes are coherent. Glow is allowed to make strong evidence look diff --git a/docs/stochastic-transparent-surface-pipeline.md b/docs/stochastic-transparent-surface-pipeline.md index 8c4a0a4..3fca39b 100644 --- a/docs/stochastic-transparent-surface-pipeline.md +++ b/docs/stochastic-transparent-surface-pipeline.md @@ -55,7 +55,7 @@ Fensalir owns a renderer-native blue-noise threshold tile. Shaders that make stochastic coverage decisions should sample that tile with frame-varying offsets instead of substituting hash noise. Hashes are acceptable for deterministic ids and cheap decorrelation, but not as the visual sampling distribution for -coverage that is meant to feed TAA or reservoirs. +coverage that is meant to feed reservoir resolve. Spline tubes follow this contract directly: CPU geometry emits conservative segment envelopes, while the spline shader samples the renderer blue-noise tile, diff --git a/docs/temporal-sdf-gaussian-field.md b/docs/temporal-sdf-gaussian-field.md index fbec2d3..0ac385d 100644 --- a/docs/temporal-sdf-gaussian-field.md +++ b/docs/temporal-sdf-gaussian-field.md @@ -59,8 +59,9 @@ contract for already-derived point claims. ## Invariants -- Field accumulation happens in world space before pixel history. TAA is the - resolver, not the owner of sensor identity. +- Field accumulation happens in world space before pixel history. Reservoir + resolve reconstructs presentation; no separate TAA pass owns sensor identity + or temporal smoothing. - Stable keys belong to the producer/accumulator boundary; shader packets are backend output and do not invent identity. - The compact support kernel has a finite bound. Renderer cost must scale from @@ -96,7 +97,7 @@ Mimir calibration/device metadata -> D3D12 GPU sensor fusion compute shader -> RWStructuredBuffer -> instanced SDF Gaussian draw --> TAA/resolve +-> reservoir resolve ``` Ownership: diff --git a/docs/temporal-spatial-evidence-reservoir.md b/docs/temporal-spatial-evidence-reservoir.md index aeae344..9fb49bb 100644 --- a/docs/temporal-spatial-evidence-reservoir.md +++ b/docs/temporal-spatial-evidence-reservoir.md @@ -11,7 +11,8 @@ Fensalir owns the shared temporal evidence machine for two customers: The old stable-key accumulator was useful, but it was not ReSTIR. The live architecture now splits the problem into a small resampled-importance core, a -spatial evidence track layer, typed lowerings, renderer passes, and TAA history. +spatial evidence track layer, typed lowerings, renderer passes, and reservoir +resolve history. ## Engine Identity @@ -31,6 +32,13 @@ color, let post handle the rest" is violating the engine body. That path may exist only as an explicitly named fallback/debug draw with no authority over field evidence. +The reservoir is the organ. Payload families are not. A TubeField path may bind +a rolling buffer, evaluate Catmull-Rom tube envelopes, generate proxy geometry, +march a tube SDF, sample a blackbody ramp, and emit candidate weights. Those +are TubeField producer responsibilities. Final visibility, temporal reuse, +spatial reuse, validation, contribution weight, and presentation membership +belong to the shared spatiotemporal field reservoir. + ## Research Spine - NVIDIA ReSTIR DI repeatedly resamples candidate light samples, then applies @@ -54,11 +62,36 @@ field evidence. https://github.com/NVIDIA-RTX/RTXDI/blob/main/Doc/RestirGI.md https://github.com/NVIDIA-RTX/RTXDI/blob/main/Doc/RestirPT.md +## Fensalir Divergence + +Pure ReSTIR usually starts after primary visibility is known: a ray or G-buffer +surface exists, then the reservoir chooses and reuses lighting or path +candidates for that surface. Fensalir cannot assume that order for field +claims. In many Fensalir scenes, the thing being sampled is the visible field +candidate itself: tube surface, SDF envelope, splat support, volume boundary, +sensor confidence surface, or mesh summary. + +That makes the shared field reservoir responsible for the job normally assigned +to TAA. Claim producers must first generate local visibility +candidates with stable identity, support, depth/travel, coverage, material +encoding, target value, and proposal policy. The reservoir then selects and +reuses those field candidates across time and space. Material and transport +evaluation ride on the selected field candidate; they do not repair missing +Form identity after the fact. + +There is no separate TAA organ in the target architecture. Any temporal +stability, history validation, antialiasing, or reconstruction happens inside +reservoir update and reservoir resolve. The final presentation pass samples or +filters the resolved reservoir; it does not repair it. If a field surface only +becomes stable because a downstream pixel-history pass smears unstable pixels +until they look acceptable, the field producer or reservoir validation is +wrong. + ## Pipeline Map ```text producer observations --> candidate generator +-> local visibility candidate generator -> target evaluator -> ResampledImportanceReservoir -> temporal reuse pass @@ -68,7 +101,7 @@ producer observations -> TemporalSpatialEvidenceLowering -> backend packet stream -> renderer/fusion passes --> TAA guide/history buffers +-> reservoir resolve/history buffers ``` ## Ownership @@ -87,6 +120,13 @@ tiles, or resident children. Mimir may propose visual/audio feature candidates from sensor confidence and calibration state. The reservoir does not know these domains; it only receives target and source-PDF values. +For opaque Form fields, local candidate generation owns primary visibility +discovery before reservoir reuse. It may be deterministic when sparse stochastic +sampling would leave holes in the visible surface. This is not a fallback to a +private renderer; it is the producer side of the shared reservoir contract. +TubeField's bounded tile-local candidate enumeration is the current concrete +example. + Every evidence sample should state its layer and encoding before lowering: Form, Appearance, or Transport; then SDF, height, density, extinction, material, phase, emission, radiance, feature, or confidence as appropriate. @@ -134,9 +174,11 @@ candidate selection when the output is a field with persistent identity: `TemporalSpatialEvidenceLowering` owns packet conversion. Consumers do not pack payload vectors by private convention when a lowering helper exists. -TAA owns pixel history validation and temporal guide buffers. It may consume -reservoir confidence, sample age, domain id, motion, and temporal-detail lanes, -but it does not own producer identity or stable spatial evidence. +Reservoir resolve owns temporal antialiasing and reconstruction over the +resolved field output. It consumes reservoir confidence, sample age, domain id, +motion, and temporal-detail lanes. No separate TAA pass owns producer identity, +stable spatial evidence, or the decision that a field candidate persists across +frames. ## Contributor Paths @@ -162,10 +204,16 @@ depth where depth participates in validation. The exact producer can be a compute pass, a proxy draw, a mesh draw, or a future volume pass. The invariant is the evidence contract, not the producer's religion. +When a contributor needs specialized candidate generation, the specialization +stops at the candidate boundary. `TubeField` is therefore a claim/candidate +producer for rolling-buffer tube surfaces, not a separate TubeField reservoir +renderer. The same rule applies to meshes, volumes, fractal probes, surface +pages, and future sensor-derived fields. + Traditional meshes therefore do not bypass the machine. They are accepted when they can provide stable identity, motion or previous-frame mapping where needed, -material/field encoding, conservative bounds, and guide data sufficient for TAA -and reservoir validation. A mesh that only paints pixels is a fallback draw. A +material/field encoding, conservative bounds, and guide data sufficient for +reservoir validation. A mesh that only paints pixels is a fallback draw. A mesh that emits field evidence is a first-class contributor. The `.aquafield` script surface follows the same boundary. A script declares @@ -222,15 +270,15 @@ Not built yet: - camera/disocclusion/material validation for fractal form/appearance reservoirs; - spatial neighbor reuse across screen tiles and cube-sphere neighbor domains; - GRIS-style domain shift mappings for nested `.aquageo` domains; -- expanded TAA guide-buffer storage for previous-frame reservoir confidence, +- expanded reservoir guide-buffer storage for previous-frame reservoir confidence, sample age, domain validity, and invalidation reason; - SSD/RAM residency queues driven by reservoir contribution estimates. -## TAA Guide Layout +## Reservoir Guide Layout -Reservoir/TAA guide data has a dedicated current scene target and ping-ponged -history target. It is deliberately separate from `history-control.w`, which -continues to own pixel history age. +Reservoir guide data has a dedicated current scene target and ping-ponged +history target. It is deliberately separate from legacy `history-control.w`. +Pixel history age is a reservoir resolve signal, not a separate TAA authority. ```text x: reservoir confidence @@ -242,9 +290,10 @@ w: invalidation code The first live producers are SDF surfaces and temporal Gaussian splats, but the schema is not surface-only. Resolve reads the current and previous guide textures, folds confidence and domain validity into history validation, then -writes the next history guide. Future ReSTIR/GRIS passes should extend the -producer side of this schema with explicit Form/Appearance/Transport fields -rather than packing more reservoir folklore into scene-control channels. +writes the next history guide. This resolve is the first reservoir resolve +implementation, not an independent TAA owner. Future ReSTIR/GRIS passes should +extend the producer side of this schema with explicit Form/Appearance/Transport +fields rather than packing more reservoir folklore into scene-control channels. ## Implementation Roadmap @@ -261,8 +310,8 @@ rather than packing more reservoir folklore into scene-control channels. surface encoding; density/extinction packets are the transparent/sensor volume encoding. Expose debug views for weight sum, selected target, candidate count, confidence, age, and invalidation reason. -7. Weave reservoir confidence and temporal detail into TAA guide buffers so the - history filter can distinguish stable reused evidence from fresh stochastic +7. Weave reservoir confidence and temporal detail into reservoir guide buffers + so reservoir resolve can distinguish stable reused evidence from fresh stochastic noise. The first pass uses current scene-control.w for reservoir confidence and keeps history-control.w as age; full previous-frame reservoir validity still needs an expanded guide layout. @@ -276,5 +325,5 @@ rather than packing more reservoir folklore into scene-control channels. If a new subsystem stores temporal candidates, it must state whether it owns raw producer retention, resampled candidate selection, stable resolved tracks, -packet lowering, or TAA history. If it cannot name that authority, it is not an -architecture. It is a decorative leak. +packet lowering, or reservoir resolve history. If it cannot name that authority, +it is not an architecture. It is a decorative leak. diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 7dac328..1850b32 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -128,13 +128,14 @@ public sealed class D3D12Renderer : IAquariumRenderer private const int RootTubeFieldRestirInitialReservoirs = 8; private const int RootTubeFieldRestirWriteReservoirs = 9; private const int RootTubeFieldRestirFieldCandidates = 10; + private const int RootTubeFieldRestirTileCountsRead = 11; private static readonly DebugUi.DebugUiOption[] RenderDebugOptions = [ new(0, "Final"), new(1, "Raw Scene"), - new(2, "History"), - new(3, "History Age"), - new(4, "History Weight"), + new(2, "Reservoir History"), + new(3, "Reservoir Age"), + new(4, "Reservoir Weight"), new(5, "Coverage/Steps"), new(6, "Lane Identity"), new(7, "Bloom"), @@ -142,7 +143,7 @@ public sealed class D3D12Renderer : IAquariumRenderer new(9, "SdfObject Identity"), new(10, "SdfObject Steps"), new(11, "Client Fractal Domains"), - new(12, "Reservoir/TAA Guide"), + new(12, "Reservoir Guide"), new(13, "Spline Envelope"), new(14, "Spline Distance"), new(15, "Spline Coverage/T"), @@ -264,6 +265,7 @@ public sealed class D3D12Renderer : IAquariumRenderer private D3D12StructuredBuffer tubeFieldRestirTileSegmentBuffer = null!; private D3D12StructuredBuffer tubeFieldRestirScratchReservoirBuffer = null!; private D3D12StructuredBuffer[] tubeFieldRestirReservoirBuffers = []; + private bool tubeFieldRestirReservoirsInitialized; private readonly D3D12StructuredBuffer tubeFieldStatsBuffer; private readonly D3D12StructuredBuffer tubeFieldDrawArgumentBuffer; private D3D12StructuredBuffer fieldReservoirCandidateBuffer = null!; @@ -2076,6 +2078,7 @@ public sealed class D3D12Renderer : IAquariumRenderer "Aquarium D3D12 TubeField ReSTIR Reservoir Buffer 1", allowUnorderedAccess: true), ]; + tubeFieldRestirReservoirsInitialized = false; } private void DisposeTubeFieldRestirBuffers() @@ -2718,6 +2721,15 @@ public sealed class D3D12Renderer : IAquariumRenderer activeCommandList.SetPipelineState(tubeFieldRestirClearReservoirsPipelineState); activeCommandList.Dispatch((uint)((pixelCount + 255) / 256), 1, 1); activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(currentReservoir.Resource)); + if (!tubeFieldRestirReservoirsInitialized) + { + previousReservoir.Transition(activeCommandList, ResourceStates.UnorderedAccess); + activeCommandList.SetComputeRootUnorderedAccessView(RootTubeFieldRestirInitialReservoirs, previousReservoir.Resource.GPUVirtualAddress); + activeCommandList.Dispatch((uint)((pixelCount + 255) / 256), 1, 1); + activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(previousReservoir.Resource)); + activeCommandList.SetComputeRootUnorderedAccessView(RootTubeFieldRestirInitialReservoirs, currentReservoir.Resource.GPUVirtualAddress); + tubeFieldRestirReservoirsInitialized = true; + } var restirConstants = new D3D12TubeFieldConstants( Vector4.Zero, @@ -2743,11 +2755,14 @@ public sealed class D3D12Renderer : IAquariumRenderer tubeFieldRestirTileSegmentBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess); activeCommandList.SetPipelineState(tubeFieldRestirBinSegmentsPipelineState); activeCommandList.Dispatch((uint)((activeTubeFieldDispatchedSegments + 255) / 256), 1, 1); + activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(tubeFieldRestirTileCountBuffer.Resource)); activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(tubeFieldRestirTileSegmentBuffer.Resource)); + tubeFieldRestirTileCountBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource); tubeFieldRestirTileSegmentBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource); currentReservoir.Transition(activeCommandList, ResourceStates.UnorderedAccess); activeCommandList.SetPipelineState(tubeFieldRestirInitialPipelineState); + activeCommandList.SetComputeRootShaderResourceView(RootTubeFieldRestirTileCountsRead, tubeFieldRestirTileCountBuffer.Resource.GPUVirtualAddress); activeCommandList.Dispatch((uint)((width + 7) / 8), (uint)((height + 7) / 8), 1); activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(currentReservoir.Resource)); @@ -2755,6 +2770,7 @@ public sealed class D3D12Renderer : IAquariumRenderer currentReservoir.Transition(activeCommandList, ResourceStates.UnorderedAccess); tubeFieldRestirScratchReservoirBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess); activeCommandList.SetComputeRootUnorderedAccessView(RootTubeFieldRestirInitialReservoirs, currentReservoir.Resource.GPUVirtualAddress); + activeCommandList.SetComputeRootShaderResourceView(RootTubeFieldRestirReadReservoirs, previousReservoir.Resource.GPUVirtualAddress); activeCommandList.SetComputeRootUnorderedAccessView(RootTubeFieldRestirWriteReservoirs, tubeFieldRestirScratchReservoirBuffer.Resource.GPUVirtualAddress); activeCommandList.SetPipelineState(tubeFieldRestirTemporalPipelineState); activeCommandList.Dispatch((uint)((width + 7) / 8), (uint)((height + 7) / 8), 1); @@ -3043,7 +3059,7 @@ public sealed class D3D12Renderer : IAquariumRenderer private void PresentBackBuffer(D3D12PassContext context, FrameResources frameResources) { - context.CommandList.BeginEvent("Present Pass"); + context.CommandList.BeginEvent("Reservoir Presentation Resolve"); try { var historyReadIndex = temporalFrameIndex & 1; @@ -4735,6 +4751,7 @@ public sealed class D3D12Renderer : IAquariumRenderer new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(19, 0), ShaderVisibility.All), new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(20, 0), ShaderVisibility.All), new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(21, 0), ShaderVisibility.All), + new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(50, 0), ShaderVisibility.All), }; var description = new RootSignatureDescription( RootSignatureFlags.None, @@ -5059,7 +5076,7 @@ public sealed class D3D12Renderer : IAquariumRenderer return CreateFullscreenPipelineState( path, "FullscreenTriangleVS", - "D3D12ResolvePS", + "D3D12ReservoirPresentationResolvePS", [Format.B8G8R8A8_UNorm, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat]); } diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl index aed57ed..758c02d 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl @@ -508,7 +508,10 @@ FieldReservoirResolveOut D3D12FieldReservoirResolvePS(VertexOut input) return output; } -ResolveOut D3D12ResolvePS(VertexOut input) +// Reservoir presentation resolve owns temporal reconstruction. The history +// textures below are cache lines for validated reservoir evidence, not a +// separate final-pixel TAA authority. +ResolveOut D3D12ReservoirPresentationResolvePS(VertexOut input) { float2 screenUv = float2(input.uv.x, 1.0 - input.uv.y); float2 pixel = screenUv * resolution; diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12TubeField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12TubeField.hlsl index 3f0257e..445e322 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12TubeField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12TubeField.hlsl @@ -102,6 +102,7 @@ StructuredBuffer RestirSegments : register(t46); StructuredBuffer RestirTileSegmentsRead : register(t47); StructuredBuffer RestirPreviousReservoirs : register(t48); StructuredBuffer RestirReadReservoirs : register(t49); +StructuredBuffer RestirTileCountsRead : register(t50); RWStructuredBuffer RestirTileCounts : register(u17); RWStructuredBuffer RestirTileSegments : register(u18); RWStructuredBuffer RestirInitialReservoirs : register(u19); @@ -110,8 +111,8 @@ RWStructuredBuffer RestirFieldReservoirCandidates : reg static const uint TubeFieldRestirTileSize = 16u; static const uint TubeFieldRestirMaxTileSegments = 128u; -static const uint TubeFieldRestirInitialCandidateCount = 2u; -static const uint TubeFieldRestirSpatialCandidateCount = 2u; +static const uint TubeFieldRestirInitialCandidateCount = TubeFieldRestirMaxTileSegments; +static const uint TubeFieldRestirSpatialCandidateCount = 4u; RWStructuredBuffer FieldReservoirCandidates : register(u14); RWByteAddressBuffer FieldReservoirLocks : register(u15); @@ -345,26 +346,25 @@ void cameraBasis(float3 camera, float3 target, out float3 forward, out float3 ri float3 rayDirectionForPixel(float2 pixel, float2 jitter, float3 camera, float3 target) { float2 uv = (pixel + jitter) / max(resolution, float2(1.0, 1.0)); - float2 ndc = uv * 2.0 - 1.0; - ndc.y = -ndc.y; + float2 slope = float2( + lerp(cameraFrustumXy.x, cameraFrustumXy.y, uv.x), + lerp(cameraFrustumXy.w, cameraFrustumXy.z, uv.y)); float3 forward; float3 right; float3 up; cameraBasis(camera, target, forward, right, up); - float aspect = resolution.x / max(resolution.y, 1.0); - float halfHeight = max(viewRadius, 0.0001); - float halfWidth = halfHeight * aspect; - float3 targetPoint = target + right * ndc.x * halfWidth + up * ndc.y * halfHeight; - return normalize(targetPoint - camera); + return normalize(forward + right * slope.x + up * slope.y); } float4 projectCameraSpace(float3 view) { float z = max(view.z, 0.0001); - float x = view.x / max(cameraFrustumXy.y * z, 0.0001); - float y = view.y / max(cameraFrustumXy.w * z, 0.0001); + float2 slope = view.xy / z; + float2 frustumMin = float2(cameraFrustumXy.x, cameraFrustumXy.z); + float2 frustumMax = float2(cameraFrustumXy.y, cameraFrustumXy.w); + float2 uv = (slope - frustumMin) / max(frustumMax - frustumMin, float2(0.0001, 0.0001)); float depth = saturate((z - cameraFrustumZ.x) / max(cameraFrustumZ.y - cameraFrustumZ.x, 0.0001)); - return float4(x, y, depth, 1.0); + return float4(uv * 2.0 - 1.0, depth, 1.0); } float2 ndcToPixel(float2 ndc) @@ -559,6 +559,26 @@ float restirRandom01(uint seed) return (float)(wangHash(seed) & 0x00ffffffu) / 16777216.0; } +bool finite1(float value) +{ + return !isnan(value) && !isinf(value); +} + +bool finite2(float2 value) +{ + return all(!isnan(value)) && all(!isinf(value)); +} + +bool finite3(float3 value) +{ + return all(!isnan(value)) && all(!isinf(value)); +} + +bool finite4(float4 value) +{ + return all(!isnan(value)) && all(!isinf(value)); +} + TubeFieldReservoir emptyTubeFieldReservoir() { TubeFieldReservoir reservoir; @@ -568,13 +588,17 @@ TubeFieldReservoir emptyTubeFieldReservoir() reservoir.reservoirGuide = float4(1.0, 0.0, 0.0, 0.0); reservoir.statistics = 0.0; reservoir.sampleData = 0.0; - reservoir.sampleKey = 0u; + reservoir.sampleKey = 0xffffffffu; return reservoir; } bool restirReservoirValid(TubeFieldReservoir reservoir) { + uint segmentBase = (uint)round(tubeDispatch.z); + uint dispatchSegments = (uint)round(tubeDispatch.w); return reservoir.sampleKey.x != 0xffffffffu && + reservoir.sampleKey.x >= segmentBase && + reservoir.sampleKey.x < segmentBase + dispatchSegments && reservoir.statistics.x > 0.0 && reservoir.statistics.y > 0.0 && reservoir.metadata.x > 0.5 && @@ -641,6 +665,31 @@ void restirFinalize(inout TubeFieldReservoir reservoir) reservoir.statistics.w = denominator > 0.0 ? reservoir.statistics.x / denominator : 0.0; } +bool restirSameField(TubeFieldReservoir a, TubeFieldReservoir b) +{ + return abs(a.metadata.x - b.metadata.x) < 0.001; +} + +bool restirCompatibleOpaqueShift(TubeFieldReservoir center, TubeFieldReservoir shifted) +{ + if (!restirReservoirValid(center) || !restirReservoirValid(shifted) || !restirSameField(center, shifted)) + { + return false; + } + + float centerCoverage = saturate(center.control.x); + float shiftedCoverage = saturate(shifted.control.x); + if (shiftedCoverage <= 0.004) + { + return false; + } + + float depthTolerance = max(0.025, center.control.z * 0.25); + bool sameSurface = abs(center.colorTravel.w - shifted.colorTravel.w) <= depthTolerance; + bool closerSurface = shifted.colorTravel.w <= center.colorTravel.w + depthTolerance; + return sameSurface || (closerSurface && shiftedCoverage >= centerCoverage * 0.25); +} + float4 projectWorldToPixelAndDepth(float3 worldPoint) { float3 forward; @@ -652,22 +701,6 @@ float4 projectWorldToPixelAndDepth(float3 worldPoint) return float4(ndcToPixel(projected.xy), projected.z, view.z); } -float2 projectWorldToPreviousPixel(float3 worldPoint) -{ - float3 forward; - float3 right; - float3 up; - cameraBasis(previousCameraPosition, previousCameraTarget, forward, right, up); - float3 delta = worldPoint - previousCameraPosition; - float z = max(dot(delta, forward), 0.0001); - float2 frustumMin = float2(cameraFrustumXy.x, cameraFrustumXy.z); - float2 frustumMax = float2(cameraFrustumXy.y, cameraFrustumXy.w); - float2 slope = float2(dot(delta, right), dot(delta, up)) / z; - float2 ndc = ((slope - frustumMin) / max(frustumMax - frustumMin, float2(0.0001, 0.0001))) * 2.0 - 1.0; - float2 pixel = (ndc * resolution + resolution) * 0.5 - previousJitterPixels; - return float2(pixel.x, resolution.y - pixel.y); -} - bool evaluateTubeFieldSegmentCandidate(uint segmentIndex, float2 pixel, out TubeFieldReservoir candidate, out float targetPdf) { candidate = emptyTubeFieldReservoir(); @@ -677,10 +710,19 @@ bool evaluateTubeFieldSegmentCandidate(uint segmentIndex, float2 pixel, out Tube float4 endProjected = projectWorldToPixelAndDepth(segment.endFeather.xyz); float4 previousProjected = projectWorldToPixelAndDepth(segment.previousRadius.xyz); float4 nextProjected = projectWorldToPixelAndDepth(segment.nextAlpha.xyz); + if (!finite4(startProjected) || !finite4(endProjected) || !finite4(previousProjected) || !finite4(nextProjected)) + { + return false; + } + float startRadiusPx = splineRadiusToPixels(segment.startRadius.w, startProjected.w); float endRadiusPx = splineRadiusToPixels(segment.previousRadius.w, endProjected.w); float previousRadiusPx = splineRadiusToPixels(segment.previousRadius.w, previousProjected.w); float nextRadiusPx = splineRadiusToPixels(segment.startRadius.w, nextProjected.w); + if (!finite1(startRadiusPx) || !finite1(endRadiusPx) || !finite1(previousRadiusPx) || !finite1(nextRadiusPx)) + { + return false; + } float sdf; float closestT; @@ -716,7 +758,7 @@ bool evaluateTubeFieldSegmentCandidate(uint segmentIndex, float2 pixel, out Tube float aa = max(0.75, radiusPx * max(segment.endFeather.w, 0.0)); float coverage = 1.0 - smoothstep(0.0, aa, sdf); float alpha = saturate(segment.nextAlpha.w * coverage); - if (alpha <= 0.004) + if (!finite1(sdf) || !finite1(radiusPx) || !finite2(normalPx) || alpha <= 0.004) { return false; } @@ -744,11 +786,16 @@ bool evaluateTubeFieldSegmentCandidate(uint segmentIndex, float2 pixel, out Tube float3 color = emissionColor * emission * glowFacing * claimCoverage; float travel = lerp(startProjected.w, endProjected.w, closestT); float3 worldPosition = lerp(segment.startRadius.xyz, segment.endFeather.xyz, closestT); + if (!finite3(color) || !finite3(worldPosition) || !finite1(travel) || !finite3(tubeNormal)) + { + return false; + } + candidate.colorTravel = float4(color, min(travel, farDistance + 1.0)); candidate.metadata = float4(segment.tubeData.w, tubeNormal); candidate.control = float4(claimCoverage, coverage, saturate(radiusPx / 32.0), value); candidate.reservoirGuide = float4(claimCoverage, 0.0, coverage, value); - candidate.statistics = float4(0.0, 1.0, 1.0, 0.0); + candidate.statistics = float4(1.0, 1.0, 1.0, 0.0); candidate.sampleData = float4(worldPosition, closestT); candidate.sampleKey = uint4(segmentIndex, 0u, 0u, 0u); targetPdf = max(0.0001, claimCoverage * (0.05 + dot(color, float3(0.2126, 0.7152, 0.0722)))); @@ -790,14 +837,47 @@ void D3D12TubeFieldRestirBinSegmentsCS(uint3 dispatchThreadId : SV_DispatchThrea TubeFieldSegment segment = RestirSegments[segmentIndex]; float4 startProjected = projectWorldToPixelAndDepth(segment.startRadius.xyz); float4 endProjected = projectWorldToPixelAndDepth(segment.endFeather.xyz); + if (!finite4(startProjected) || !finite4(endProjected)) + { + return; + } + + if (startProjected.w <= 0.0 && endProjected.w <= 0.0) + { + return; + } + float radiusPx = max( splineRadiusToPixels(segment.startRadius.w, startProjected.w), splineRadiusToPixels(segment.previousRadius.w, endProjected.w)); + radiusPx = min(radiusPx, 256.0); + if (!finite1(radiusPx)) + { + return; + } + float2 minPx = floor((min(startProjected.xy, endProjected.xy) - radiusPx * 1.5) / TubeFieldRestirTileSize); float2 maxPx = floor((max(startProjected.xy, endProjected.xy) + radiusPx * 1.5) / TubeFieldRestirTileSize); + if (!finite2(minPx) || !finite2(maxPx)) + { + return; + } + uint2 tileDims = restirTileDimensions(); + float2 tileLimit = (float2)tileDims; + if (maxPx.x < 0.0 || maxPx.y < 0.0 || minPx.x >= tileLimit.x || minPx.y >= tileLimit.y) + { + return; + } + uint2 tileMin = min((uint2)max(minPx, float2(0.0, 0.0)), tileDims - 1u); uint2 tileMax = min((uint2)max(maxPx, float2(0.0, 0.0)), tileDims - 1u); + uint2 tileSpan = tileMax - tileMin + 1u; + if (tileSpan.x * tileSpan.y > 256u) + { + return; + } + for (uint tileY = tileMin.y; tileY <= tileMax.y; tileY++) { for (uint tileX = tileMin.x; tileX <= tileMax.x; tileX++) @@ -827,7 +907,7 @@ void D3D12TubeFieldRestirInitialCS(uint3 dispatchThreadId : SV_DispatchThreadID) uint2 tileDims = restirTileDimensions(); uint2 tile = min(pixel / TubeFieldRestirTileSize, tileDims - 1u); uint tileIndex = tile.y * tileDims.x + tile.x; - uint count = min(RestirTileCounts[tileIndex], TubeFieldRestirMaxTileSegments); + uint count = min(RestirTileCountsRead[tileIndex], TubeFieldRestirMaxTileSegments); TubeFieldReservoir reservoir = RestirInitialReservoirs[pixelIndex]; float invSourcePdf = count > 0u ? (float)count : 0.0; uint proposalCount = min(count, TubeFieldRestirInitialCandidateCount); @@ -864,19 +944,12 @@ void D3D12TubeFieldRestirTemporalCS(uint3 dispatchThreadId : SV_DispatchThreadID TubeFieldReservoir reservoir = RestirInitialReservoirs[pixelIndex]; if (restirReservoirValid(reservoir)) { - float2 previousProjected = projectWorldToPreviousPixel(reservoir.sampleData.xyz); - uint2 previousPixel = min((uint2)max(previousProjected, float2(0.0, 0.0)), dimensions - 1u); - uint previousIndex = previousPixel.y * dimensions.x + previousPixel.x; - TubeFieldReservoir previousReservoir = RestirPreviousReservoirs[previousIndex]; - TubeFieldReservoir reprojected; - float targetPdf; + TubeFieldReservoir previousReservoir = RestirPreviousReservoirs[pixelIndex]; if (restirReservoirValid(previousReservoir) && - evaluateTubeFieldSegmentCandidate(previousReservoir.sampleKey.x, (float2)pixel + 0.5, reprojected, targetPdf) && - abs(previousReservoir.metadata.x - reprojected.metadata.x) < 0.001) + restirCompatibleOpaqueShift(reservoir, previousReservoir)) { - reprojected.statistics = previousReservoir.statistics; float randomValue = restirRandom01(pixelIndex * 747796405u + (uint)frameIndex * 2891336453u); - restirCombineReservoir(reservoir, reprojected, targetPdf, randomValue); + restirCombineReservoir(reservoir, previousReservoir, max(previousReservoir.statistics.y, 0.0001), randomValue); restirFinalize(reservoir); } } @@ -896,34 +969,45 @@ void D3D12TubeFieldRestirSpatialResolveCS(uint3 dispatchThreadId : SV_DispatchTh uint pixelIndex = pixel.y * dimensions.x + pixel.x; TubeFieldReservoir reservoir = RestirReadReservoirs[pixelIndex]; - static const int2 spatialOffsets[8] = + if (!restirReservoirValid(reservoir)) { - int2(-3, -1), - int2(3, 1), - int2(-1, 3), - int2(1, -3), - int2(-6, 2), - int2(6, -2), - int2(-2, -6), - int2(2, 6) + RestirWriteReservoirs[pixelIndex] = reservoir; + return; + } + + static const int2 spatialOffsets[16] = + { + int2(-1, 0), + int2(1, 0), + int2(0, -1), + int2(0, 1), + int2(-2, -1), + int2(2, 1), + int2(-1, 2), + int2(1, -2), + int2(-3, 1), + int2(3, -1), + int2(-1, -3), + int2(1, 3), + int2(-4, 2), + int2(4, -2), + int2(-2, 4), + int2(2, -4) }; [unroll] for (uint sampleIndex = 0u; sampleIndex < TubeFieldRestirSpatialCandidateCount; sampleIndex++) { - uint offsetIndex = wangHash(pixelIndex * 9781u + sampleIndex * 6271u + (uint)frameIndex * 1709u) & 7u; + uint offsetIndex = wangHash(pixelIndex * 9781u + sampleIndex * 6271u + (uint)frameIndex * 1709u) & 15u; int2 offset = spatialOffsets[offsetIndex]; uint2 neighbor = min((uint2)max(int2(pixel) + offset, int2(0, 0)), dimensions - 1u); uint neighborIndex = neighbor.y * dimensions.x + neighbor.x; TubeFieldReservoir neighborReservoir = RestirReadReservoirs[neighborIndex]; - TubeFieldReservoir shifted; - float targetPdf; if (restirReservoirValid(neighborReservoir) && - evaluateTubeFieldSegmentCandidate(neighborReservoir.sampleKey.x, (float2)pixel + 0.5, shifted, targetPdf)) + restirCompatibleOpaqueShift(reservoir, neighborReservoir)) { - shifted.statistics = neighborReservoir.statistics; float randomValue = restirRandom01(pixelIndex * 89173u + sampleIndex * 19349663u + (uint)frameIndex * 83492791u); - restirCombineReservoir(reservoir, shifted, targetPdf, randomValue); + restirCombineReservoir(reservoir, neighborReservoir, max(neighborReservoir.statistics.y, 0.0001), randomValue); } }