diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 24c29d4..060e5d5 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -27,6 +27,7 @@ public sealed class D3D12Renderer : IAquariumRenderer private const int MediumFroxelAtlasColumns = 8; private const int MediumFroxelAtlasRows = 4; private const int MediumFroxelSliceCount = MediumFroxelAtlasColumns * MediumFroxelAtlasRows; + private const int MediumLightPropagationIterationCount = 4; private const int ViewFroxelPrimitiveSlotCount = 2; private const int CursorPrimitiveId = PlanetCount + 1; private const Format MediumVolumeFormat = Format.R16G16B16A16_Float; @@ -100,6 +101,8 @@ public sealed class D3D12Renderer : IAquariumRenderer private D3D12RenderTarget mediumTransportRenderTarget; private D3D12RenderTarget mediumLightInjectionRenderTarget; private D3D12RenderTarget mediumLightDirectionInjectionRenderTarget; + private D3D12RenderTarget mediumLightScratchRenderTarget; + private D3D12RenderTarget mediumLightDirectionScratchRenderTarget; private D3D12RenderTarget mediumLightRenderTarget; private D3D12RenderTarget mediumLightDirectionRenderTarget; private D3D12RenderTarget sceneRenderTarget; @@ -218,6 +221,8 @@ public sealed class D3D12Renderer : IAquariumRenderer mediumTransportRenderTarget = CreateMediumVolumeRenderTarget("medium-transport-target", "Aquarium D3D12 Medium Transport Target"); mediumLightInjectionRenderTarget = CreateMediumVolumeRenderTarget("medium-light-injection-target", "Aquarium D3D12 Medium Light Injection Target"); mediumLightDirectionInjectionRenderTarget = CreateMediumVolumeRenderTarget("medium-light-direction-injection-target", "Aquarium D3D12 Medium Light Direction Injection Target"); + mediumLightScratchRenderTarget = CreateMediumVolumeRenderTarget("medium-light-scratch-target", "Aquarium D3D12 Medium Light Scratch Target"); + mediumLightDirectionScratchRenderTarget = CreateMediumVolumeRenderTarget("medium-light-direction-scratch-target", "Aquarium D3D12 Medium Light Direction Scratch Target"); mediumLightRenderTarget = CreateMediumVolumeRenderTarget("medium-light-target", "Aquarium D3D12 Propagated Medium Light Target"); mediumLightDirectionRenderTarget = CreateMediumVolumeRenderTarget("medium-light-direction-target", "Aquarium D3D12 Propagated Medium Light Direction Target"); sceneRenderTarget = CreateSceneRenderTarget(); @@ -375,6 +380,14 @@ public sealed class D3D12Renderer : IAquariumRenderer mediumLightInjectionRenderTarget.CreateShaderResourceView(device, frameResources.MediumInjectionLightDescriptor); frameResources.MediumInjectionLightDirectionDescriptor = frameResources.TransientShaderDescriptors.Allocate(); mediumLightDirectionInjectionRenderTarget.CreateShaderResourceView(device, frameResources.MediumInjectionLightDirectionDescriptor); + frameResources.MediumScratchTargetsDescriptor = frameResources.TransientShaderDescriptors.Allocate(); + mediumVolumeRenderTarget.CreateShaderResourceView(device, frameResources.MediumScratchTargetsDescriptor); + frameResources.MediumScratchTransportDescriptor = frameResources.TransientShaderDescriptors.Allocate(); + mediumTransportRenderTarget.CreateShaderResourceView(device, frameResources.MediumScratchTransportDescriptor); + frameResources.MediumScratchLightDescriptor = frameResources.TransientShaderDescriptors.Allocate(); + mediumLightScratchRenderTarget.CreateShaderResourceView(device, frameResources.MediumScratchLightDescriptor); + frameResources.MediumScratchLightDirectionDescriptor = frameResources.TransientShaderDescriptors.Allocate(); + mediumLightDirectionScratchRenderTarget.CreateShaderResourceView(device, frameResources.MediumScratchLightDirectionDescriptor); frameResources.MediumTargetsDescriptor = frameResources.TransientShaderDescriptors.Allocate(); mediumVolumeRenderTarget.CreateShaderResourceView(device, frameResources.MediumTargetsDescriptor); frameResources.MediumTransportDescriptor = frameResources.TransientShaderDescriptors.Allocate(); @@ -533,6 +546,8 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneEventMetadataRenderTarget.Dispose(); mediumLightDirectionRenderTarget.Dispose(); mediumLightRenderTarget.Dispose(); + mediumLightDirectionScratchRenderTarget.Dispose(); + mediumLightScratchRenderTarget.Dispose(); mediumLightDirectionInjectionRenderTarget.Dispose(); mediumLightInjectionRenderTarget.Dispose(); mediumTransportRenderTarget.Dispose(); @@ -715,6 +730,8 @@ public sealed class D3D12Renderer : IAquariumRenderer resourceRegistry.RemoveRenderTarget("medium-transport-target"); resourceRegistry.RemoveRenderTarget("medium-light-injection-target"); resourceRegistry.RemoveRenderTarget("medium-light-direction-injection-target"); + resourceRegistry.RemoveRenderTarget("medium-light-scratch-target"); + resourceRegistry.RemoveRenderTarget("medium-light-direction-scratch-target"); resourceRegistry.RemoveRenderTarget("medium-light-target"); resourceRegistry.RemoveRenderTarget("medium-light-direction-target"); resourceRegistry.RemoveRenderTarget("medium-volume-target"); @@ -731,6 +748,8 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneEventMetadataRenderTarget.Dispose(); mediumLightDirectionRenderTarget.Dispose(); mediumLightRenderTarget.Dispose(); + mediumLightDirectionScratchRenderTarget.Dispose(); + mediumLightScratchRenderTarget.Dispose(); mediumLightDirectionInjectionRenderTarget.Dispose(); mediumLightInjectionRenderTarget.Dispose(); mediumTransportRenderTarget.Dispose(); @@ -760,6 +779,8 @@ public sealed class D3D12Renderer : IAquariumRenderer mediumTransportRenderTarget = CreateMediumVolumeRenderTarget("medium-transport-target", "Aquarium D3D12 Medium Transport Target"); mediumLightInjectionRenderTarget = CreateMediumVolumeRenderTarget("medium-light-injection-target", "Aquarium D3D12 Medium Light Injection Target"); mediumLightDirectionInjectionRenderTarget = CreateMediumVolumeRenderTarget("medium-light-direction-injection-target", "Aquarium D3D12 Medium Light Direction Injection Target"); + mediumLightScratchRenderTarget = CreateMediumVolumeRenderTarget("medium-light-scratch-target", "Aquarium D3D12 Medium Light Scratch Target"); + mediumLightDirectionScratchRenderTarget = CreateMediumVolumeRenderTarget("medium-light-direction-scratch-target", "Aquarium D3D12 Medium Light Direction Scratch Target"); mediumLightRenderTarget = CreateMediumVolumeRenderTarget("medium-light-target", "Aquarium D3D12 Propagated Medium Light Target"); mediumLightDirectionRenderTarget = CreateMediumVolumeRenderTarget("medium-light-direction-target", "Aquarium D3D12 Propagated Medium Light Direction Target"); froxelPrimitiveBuffer = CreateViewFroxelPrimitiveBuffer(); @@ -1279,26 +1300,43 @@ public sealed class D3D12Renderer : IAquariumRenderer mediumTransportRenderTarget.Transition(activeCommandList, ResourceStates.PixelShaderResource); mediumLightInjectionRenderTarget.Transition(activeCommandList, ResourceStates.PixelShaderResource); mediumLightDirectionInjectionRenderTarget.Transition(activeCommandList, ResourceStates.PixelShaderResource); - mediumLightRenderTarget.Transition(activeCommandList, ResourceStates.RenderTarget); - mediumLightDirectionRenderTarget.Transition(activeCommandList, ResourceStates.RenderTarget); - activeCommandList.ClearRenderTargetView(mediumLightRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); - activeCommandList.ClearRenderTargetView(mediumLightDirectionRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); activeCommandList.SetDescriptorHeaps(frameResources.TransientShaderDescriptors.Heap); activeCommandList.SetPipelineState(mediumLightPropagationPipelineState!); activeCommandList.SetGraphicsRootSignature(fullscreenRootSignature); activeCommandList.SetGraphicsRootDescriptorTable(0, frameResources.FrameConstantsDescriptor.Gpu); - activeCommandList.SetGraphicsRootDescriptorTable(5, frameResources.MediumInjectionTargetsDescriptor.Gpu); - activeCommandList.SetGraphicsRootDescriptorTable(21, frameResources.MediumInjectionLightDirectionDescriptor.Gpu); activeCommandList.RSSetViewports(mediumViewport); activeCommandList.RSSetScissorRects(mediumScissorRect); - activeCommandList.OMSetRenderTargets( - [ - mediumLightRenderTarget.RenderTargetView.Cpu, - mediumLightDirectionRenderTarget.RenderTargetView.Cpu, - ], - null); activeCommandList.IASetPrimitiveTopology(PrimitiveTopology.TriangleList); - activeCommandList.DrawInstanced(3, 1, 0, 0); + + DrawMediumLightPropagationIteration( + activeCommandList, + frameResources.MediumInjectionTargetsDescriptor, + frameResources.MediumInjectionLightDirectionDescriptor, + mediumLightScratchRenderTarget, + mediumLightDirectionScratchRenderTarget); + + for (var iteration = 1; iteration < MediumLightPropagationIterationCount; iteration++) + { + var sourceTargets = (iteration & 1) == 1 + ? frameResources.MediumScratchTargetsDescriptor + : frameResources.MediumTargetsDescriptor; + var sourceDirection = (iteration & 1) == 1 + ? frameResources.MediumScratchLightDirectionDescriptor + : frameResources.MediumLightDirectionDescriptor; + var targetLight = (iteration & 1) == 1 + ? mediumLightRenderTarget + : mediumLightScratchRenderTarget; + var targetDirection = (iteration & 1) == 1 + ? mediumLightDirectionRenderTarget + : mediumLightDirectionScratchRenderTarget; + + DrawMediumLightPropagationIteration( + activeCommandList, + sourceTargets, + sourceDirection, + targetLight, + targetDirection); + } } finally { @@ -1306,6 +1344,30 @@ public sealed class D3D12Renderer : IAquariumRenderer } } + private void DrawMediumLightPropagationIteration( + ID3D12GraphicsCommandList activeCommandList, + D3D12DescriptorSlot sourceTargetsDescriptor, + D3D12DescriptorSlot sourceLightDirectionDescriptor, + D3D12RenderTarget targetLight, + D3D12RenderTarget targetLightDirection) + { + targetLight.Transition(activeCommandList, ResourceStates.RenderTarget); + targetLightDirection.Transition(activeCommandList, ResourceStates.RenderTarget); + activeCommandList.ClearRenderTargetView(targetLight.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); + activeCommandList.ClearRenderTargetView(targetLightDirection.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); + activeCommandList.SetGraphicsRootDescriptorTable(5, sourceTargetsDescriptor.Gpu); + activeCommandList.SetGraphicsRootDescriptorTable(21, sourceLightDirectionDescriptor.Gpu); + activeCommandList.OMSetRenderTargets( + [ + targetLight.RenderTargetView.Cpu, + targetLightDirection.RenderTargetView.Cpu, + ], + null); + activeCommandList.DrawInstanced(3, 1, 0, 0); + targetLight.Transition(activeCommandList, ResourceStates.PixelShaderResource); + targetLightDirection.Transition(activeCommandList, ResourceStates.PixelShaderResource); + } + private void ClearMediumVolume(ID3D12GraphicsCommandList activeCommandList) { activeCommandList.BeginEvent("Clear Medium Volume"); @@ -1315,11 +1377,15 @@ public sealed class D3D12Renderer : IAquariumRenderer mediumTransportRenderTarget.Transition(activeCommandList, ResourceStates.RenderTarget); mediumLightInjectionRenderTarget.Transition(activeCommandList, ResourceStates.RenderTarget); mediumLightDirectionInjectionRenderTarget.Transition(activeCommandList, ResourceStates.RenderTarget); + mediumLightScratchRenderTarget.Transition(activeCommandList, ResourceStates.RenderTarget); + mediumLightDirectionScratchRenderTarget.Transition(activeCommandList, ResourceStates.RenderTarget); mediumLightRenderTarget.Transition(activeCommandList, ResourceStates.RenderTarget); mediumLightDirectionRenderTarget.Transition(activeCommandList, ResourceStates.RenderTarget); activeCommandList.ClearRenderTargetView(mediumVolumeRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 1.0f, 0.0f, 0.0f)); activeCommandList.ClearRenderTargetView(mediumTransportRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); activeCommandList.ClearRenderTargetView(mediumLightInjectionRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); + activeCommandList.ClearRenderTargetView(mediumLightScratchRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); + activeCommandList.ClearRenderTargetView(mediumLightDirectionScratchRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); activeCommandList.ClearRenderTargetView(mediumLightRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); activeCommandList.ClearRenderTargetView(mediumLightDirectionInjectionRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); activeCommandList.ClearRenderTargetView(mediumLightDirectionRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); @@ -2564,6 +2630,14 @@ public sealed class D3D12Renderer : IAquariumRenderer public D3D12DescriptorSlot MediumInjectionLightDirectionDescriptor { get; set; } + public D3D12DescriptorSlot MediumScratchTargetsDescriptor { get; set; } + + public D3D12DescriptorSlot MediumScratchTransportDescriptor { get; set; } + + public D3D12DescriptorSlot MediumScratchLightDescriptor { get; set; } + + public D3D12DescriptorSlot MediumScratchLightDirectionDescriptor { get; set; } + public D3D12DescriptorSlot MediumTargetsDescriptor { get; set; } public D3D12DescriptorSlot MediumTransportDescriptor { get; set; } diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 713569a..1676b6f 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -137,3 +137,4 @@ {"timestamp":"2026-05-10T16:25:00+01:00","topic":"d3d12-physical-medium-coefficients","lesson":"Aquarium medium math should justify where light comes from before beauty tuning. D3D12 froxels now carry explicit density, extinction sigma_t, scattering sigma_s, albedo, slice transmittance, and raw emitter irradiance. Scene integration uses Beer-Lambert transmittance and isotropic single scattering sigma_s * irradiance / 4pi; direct ray transmittance debug uses the same coefficient evaluator. mediumTerms now means x extinction-per-density, y scattering albedo, z reserved phase/aniso, w density scale.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Smoke.hlsl","docs/hlsl-renderer.md","docs/sdf-field-renderer-plan.md","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx --no-restore",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 90"]} {"timestamp":"2026-05-10T16:38:53.8578765+01:00","topic":"D3D12 directional medium lighting","lesson":"Medium lighting needs explicit directional/probe data once phase and glossy surfaces matter. Aquarium now carries a luminance-weighted dominant-light direction moment beside propagated froxel irradiance, uses it for Henyey-Greenstein scattering, and shades solids from the same field instead of private direct emitter loops.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/D3D12Renderer.cs"]} {"timestamp":"2026-05-10T16:49:14.2607265+01:00","topic":"Dominant-light direction limitation","lesson":"A single froxel light direction is a lossy artistic approximation. Intersecting beams average, and opposed beams can cancel to low confidence and isotropic fallback. Keep the smear for now because it may look good, but future full transport needs multi-lobe, probe, or reservoir direction storage.","files":["state/memory.json","state/map.yaml","state/scratch.md"]} +{"timestamp":"2026-05-10T17:03:06.9352794+01:00","topic":"D3D12 iterative medium light propagation","lesson":"The propagated medium light field now uses explicit scratch/final froxel targets and four ping-ponged propagation iterations for irradiance plus dominant direction, replacing the single one-pass neighbor spread. This remains a compact diffusion approximation, but propagation state and cost are explicit.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","state/memory.json","state/map.yaml","state/scratch.md"]} diff --git a/state/map.yaml b/state/map.yaml index 55b5595..82a8c19 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -63,7 +63,7 @@ renderer_state: - DirectWrite overlay text after scene rendering. - Direct2D/DirectWrite debug UI panel with code-first controls bound to renderer state. - Global GraphicsSettings document feeding render debug, exposure, bloom intensity, bloom veil, and medium composite intensity. - - Field instance structured buffer feeding D3D12 medium density, transport, raw light injection, propagated froxel light, and propagated dominant-light direction passes. + - Field instance structured buffer feeding D3D12 medium density, transport, raw light injection, and four-iteration ping-ponged froxel propagation for irradiance plus dominant-light direction. - D3D12 default renderer behind IAquariumRenderer with device/queue/swapchain, per-frame command allocators, fences, static/transient descriptor arenas, upload ring constants, RTV/SRV/UAV render targets, structured field buffers, tracked barriers, command events, resize descriptor-arena rebuild, migrated Grid height brushes, view-froxel solid bins, HDR/bloom/present, temporal history lanes, and DirectWrite overlay bridge. - D3D12 Grid linework is direct-traced as a non-terminating stochastic event lane before the nearest solid; the failed transparent-froxel Grid bin experiment is dead and must stay out of live code. - Splash screen with bundled app icon and GameCult typefaces. diff --git a/state/memory.json b/state/memory.json index ba0a2c7..1a69bae 100644 --- a/state/memory.json +++ b/state/memory.json @@ -52,7 +52,7 @@ "HDR/PBR presentation should follow research/rendering/hdr-pbr-presentation.md: scene-linear lighting, explicit exposure, pre-tonemap bloom/veil, then a display transform. Do not reach for 2000s threshold bloom when the frame needs dynamic range.", "Aquarium's current HDR presentation pass uses fixed manual exposure plus a three-level half/quarter/eighth pre-tonemap bloom pyramid with firefly-safe downsampling and separable blur. Next HDR work is persisted exposure/display controls, not threshold glow.", "Future volumetric lighting should be a field sampled consistently by terrain, planets, fog, particles, and diegetic UI. Do not fake only the planet term if the scene is supposed to bounce light.", - "Volumetric density must not self-illuminate through baked color. D3D12 medium lighting now evaluates physical-ish coefficients first: density for inspection, extinction sigma_t, scattering sigma_s, and albedo. Field mediumTerms mean x = extinction per density, y = scattering albedo, z = reserved phase/aniso, w = density scale. Raw irradiance from FieldFlags.Emitter instances is injected using emitter colorIntensity.rgb, finite solid angle, and cheap depth/radial light-path attenuation; a separate propagation pass transports both irradiance and a luminance-weighted dominant-light direction moment. Medium integration uses Beer-Lambert transmittance and Henyey-Greenstein phase from that direction field; solids consume the propagated froxel light/direction for diffuse and cursor GGX specular. A single direction moment is intentionally lossy: crossing beams smear or cancel to low confidence and fall back toward isotropic scattering. Keep it for now if the smear is useful, but do not mistake it for final transport; future richer lighting needs multi-lobe/probe/reservoir direction storage. Do not compute unused direct-scattering color into transport targets or private per-surface emitter loops just because an earlier pass did. Self, cursor, nebulae, and future emissive SDFs must all use this field-emitter path; do not reintroduce point-light constants or ambient fog glow.", + "Volumetric density must not self-illuminate through baked color. D3D12 medium lighting now evaluates physical-ish coefficients first: density for inspection, extinction sigma_t, scattering sigma_s, and albedo. Field mediumTerms mean x = extinction per density, y = scattering albedo, z = reserved phase/aniso, w = density scale. Raw irradiance from FieldFlags.Emitter instances is injected using emitter colorIntensity.rgb, finite solid angle, and cheap depth/radial light-path attenuation; propagation ping-pongs four low-resolution froxel iterations through scratch/final irradiance and luminance-weighted dominant-light direction targets. Medium integration uses Beer-Lambert transmittance and Henyey-Greenstein phase from that direction field; solids consume the propagated froxel light/direction for diffuse and cursor GGX specular. A single direction moment is intentionally lossy: crossing beams smear or cancel to low confidence and fall back toward isotropic scattering. Keep it for now if the smear is useful, but do not mistake it for final transport; future richer lighting needs multi-lobe/probe/reservoir direction storage. Do not compute unused direct-scattering color into transport targets or private per-surface emitter loops just because an earlier pass did. Self, cursor, nebulae, and future emissive SDFs must all use this field-emitter path; do not reintroduce point-light constants or ambient fog glow.", "Do not blend D3D12 medium color history into final presentation until the renderer owns a proper light-volume/medium reprojection contract. Medium packets can keep opacity/density/travel age for diagnostics, but resolved fog color history smears badly when injected lighting changes with camera/emitter geometry.", "D3D12 medium radiance has its own temporal lane: scene emits `sceneMediumColor`, resolve keeps surface history surface-only, reprojects medium packet travel into `historyMediumColor`, validates by travel/opacity/density, and composites resolved surface + resolved medium + events. Medium lighting is split into raw emitter injection and a propagated froxel light target; medium integration and solid diffuse shading consume the propagated light field instead of private direct diffuse loops. This is single-step froxel propagation plus reprojection; full multi-bounce GI and directional/probe glossy lighting remain future work.", "Volumetric design must start from research/volumetrics/synthesis.md. The first pass should follow Wronski/Hillaire production shape: explicit low-resolution frustum volume, injected density/extinction/scattering, Self as first emitter, front-to-back transmittance/in-scattering integration, then consistent composition.", diff --git a/state/scratch.md b/state/scratch.md index 9e387af..e86fbaa 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -518,3 +518,10 @@ reusing field identity/travel/control. isotropic scatter. Let that artistic smear stand for now; future proper transport should use multi-lobe/probe/reservoir direction storage when this becomes the limiting artifact. +- D3D12 iterative medium propagation pass: the propagated medium light field no + longer comes from one neighbor spread. D3D12 now owns scratch light and + scratch direction targets and runs four ping-ponged low-resolution froxel + propagation iterations from injection through scratch/final targets. This is + still a compact diffusion-style approximation, not full multi-bounce + transport, but the cost and state are explicit and the scene consumes only the + final propagated irradiance/direction targets.