From 0ce8a45849dac4189bce662eed7d8df8fbac3019 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 10 May 2026 08:38:51 +0100 Subject: [PATCH] Add medium radiance reprojection --- src/Aquarium.Engine/Render/D3D12Renderer.cs | 44 ++++++++++++++++++- .../Render/Shaders/D3D12Post.hlsl | 32 +++++++++++--- .../Render/Shaders/D3D12Scene.hlsl | 10 ++++- state/evidence.jsonl | 1 + state/memory.json | 1 + state/scratch.md | 9 ++++ 6 files changed, 88 insertions(+), 9 deletions(-) diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 2a3aee2..3c2bf8b 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -97,12 +97,14 @@ public sealed class D3D12Renderer : IAquariumRenderer private D3D12RenderTarget sceneMetadataRenderTarget; private D3D12RenderTarget sceneControlRenderTarget; private D3D12RenderTarget sceneMediumPacketRenderTarget; + private D3D12RenderTarget sceneMediumColorRenderTarget; private D3D12RenderTarget sceneEventColorRenderTarget; private D3D12RenderTarget sceneEventMetadataRenderTarget; private readonly D3D12RenderTarget[] historyRenderTargets = new D3D12RenderTarget[2]; private readonly D3D12RenderTarget[] historyMetadataRenderTargets = new D3D12RenderTarget[2]; private readonly D3D12RenderTarget[] historyControlRenderTargets = new D3D12RenderTarget[2]; private readonly D3D12RenderTarget[] historyMediumPacketRenderTargets = new D3D12RenderTarget[2]; + private readonly D3D12RenderTarget[] historyMediumColorRenderTargets = new D3D12RenderTarget[2]; private readonly D3D12RenderTarget[] historyEventColorRenderTargets = new D3D12RenderTarget[2]; private readonly D3D12RenderTarget[] historyEventMetadataRenderTargets = new D3D12RenderTarget[2]; private readonly D3D12RenderTarget[] bloomRenderTargets = new D3D12RenderTarget[BloomLevelCount]; @@ -209,6 +211,7 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneMetadataRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-metadata-target", "Aquarium D3D12 Scene Metadata Target"); sceneControlRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-control-target", "Aquarium D3D12 Scene Control Target"); sceneMediumPacketRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-medium-packet-target", "Aquarium D3D12 Scene Medium Packet Target"); + sceneMediumColorRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-medium-color-target", "Aquarium D3D12 Scene Medium Color Target"); sceneEventColorRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-event-color-target", "Aquarium D3D12 Scene Event Color Target"); sceneEventMetadataRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-event-metadata-target", "Aquarium D3D12 Scene Event Metadata Target"); CreateHistoryRenderTargets(); @@ -363,6 +366,8 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneControlRenderTarget.CreateShaderResourceView(device, frameResources.SceneControlDescriptor); frameResources.SceneMediumPacketDescriptor = frameResources.TransientShaderDescriptors.Allocate(); sceneMediumPacketRenderTarget.CreateShaderResourceView(device, frameResources.SceneMediumPacketDescriptor); + frameResources.SceneMediumColorDescriptor = frameResources.TransientShaderDescriptors.Allocate(); + sceneMediumColorRenderTarget.CreateShaderResourceView(device, frameResources.SceneMediumColorDescriptor); frameResources.SceneEventColorDescriptor = frameResources.TransientShaderDescriptors.Allocate(); sceneEventColorRenderTarget.CreateShaderResourceView(device, frameResources.SceneEventColorDescriptor); frameResources.SceneEventMetadataDescriptor = frameResources.TransientShaderDescriptors.Allocate(); @@ -376,6 +381,8 @@ public sealed class D3D12Renderer : IAquariumRenderer historyControlRenderTargets[historyReadIndex].CreateShaderResourceView(device, frameResources.HistoryControlDescriptor); frameResources.HistoryMediumPacketDescriptor = frameResources.TransientShaderDescriptors.Allocate(); historyMediumPacketRenderTargets[historyReadIndex].CreateShaderResourceView(device, frameResources.HistoryMediumPacketDescriptor); + frameResources.HistoryMediumColorDescriptor = frameResources.TransientShaderDescriptors.Allocate(); + historyMediumColorRenderTargets[historyReadIndex].CreateShaderResourceView(device, frameResources.HistoryMediumColorDescriptor); frameResources.HistoryEventColorDescriptor = frameResources.TransientShaderDescriptors.Allocate(); historyEventColorRenderTargets[historyReadIndex].CreateShaderResourceView(device, frameResources.HistoryEventColorDescriptor); frameResources.HistoryEventMetadataDescriptor = frameResources.TransientShaderDescriptors.Allocate(); @@ -492,6 +499,7 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneMetadataRenderTarget.Dispose(); sceneControlRenderTarget.Dispose(); sceneMediumPacketRenderTarget.Dispose(); + sceneMediumColorRenderTarget.Dispose(); sceneEventColorRenderTarget.Dispose(); sceneEventMetadataRenderTarget.Dispose(); mediumTransportRenderTarget.Dispose(); @@ -659,6 +667,7 @@ public sealed class D3D12Renderer : IAquariumRenderer resourceRegistry.RemoveRenderTarget("scene-metadata-target"); resourceRegistry.RemoveRenderTarget("scene-control-target"); resourceRegistry.RemoveRenderTarget("scene-medium-packet-target"); + resourceRegistry.RemoveRenderTarget("scene-medium-color-target"); resourceRegistry.RemoveRenderTarget("scene-event-color-target"); resourceRegistry.RemoveRenderTarget("scene-event-metadata-target"); resourceRegistry.RemoveRenderTarget("medium-transport-target"); @@ -672,6 +681,7 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneMetadataRenderTarget.Dispose(); sceneControlRenderTarget.Dispose(); sceneMediumPacketRenderTarget.Dispose(); + sceneMediumColorRenderTarget.Dispose(); sceneEventColorRenderTarget.Dispose(); sceneEventMetadataRenderTarget.Dispose(); mediumTransportRenderTarget.Dispose(); @@ -705,6 +715,7 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneMetadataRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-metadata-target", "Aquarium D3D12 Scene Metadata Target"); sceneControlRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-control-target", "Aquarium D3D12 Scene Control Target"); sceneMediumPacketRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-medium-packet-target", "Aquarium D3D12 Scene Medium Packet Target"); + sceneMediumColorRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-medium-color-target", "Aquarium D3D12 Scene Medium Color Target"); sceneEventColorRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-event-color-target", "Aquarium D3D12 Scene Event Color Target"); sceneEventMetadataRenderTarget = CreateSceneAuxiliaryRenderTarget("scene-event-metadata-target", "Aquarium D3D12 Scene Event Metadata Target"); CreateHistoryRenderTargets(); @@ -765,6 +776,7 @@ public sealed class D3D12Renderer : IAquariumRenderer historyMetadataRenderTargets[index] = CreateSceneAuxiliaryRenderTarget($"history-metadata-{index}", $"Aquarium D3D12 History Metadata {index}"); historyControlRenderTargets[index] = CreateSceneAuxiliaryRenderTarget($"history-control-{index}", $"Aquarium D3D12 History Control {index}"); historyMediumPacketRenderTargets[index] = CreateSceneAuxiliaryRenderTarget($"history-medium-packet-{index}", $"Aquarium D3D12 History Medium Packet {index}"); + historyMediumColorRenderTargets[index] = CreateSceneAuxiliaryRenderTarget($"history-medium-color-{index}", $"Aquarium D3D12 History Medium Color {index}"); historyEventColorRenderTargets[index] = CreateSceneAuxiliaryRenderTarget($"history-event-color-{index}", $"Aquarium D3D12 History Event Color {index}"); historyEventMetadataRenderTargets[index] = CreateSceneAuxiliaryRenderTarget($"history-event-metadata-{index}", $"Aquarium D3D12 History Event Metadata {index}"); } @@ -778,6 +790,7 @@ public sealed class D3D12Renderer : IAquariumRenderer resourceRegistry.RemoveRenderTarget($"history-metadata-{index}"); resourceRegistry.RemoveRenderTarget($"history-control-{index}"); resourceRegistry.RemoveRenderTarget($"history-medium-packet-{index}"); + resourceRegistry.RemoveRenderTarget($"history-medium-color-{index}"); resourceRegistry.RemoveRenderTarget($"history-event-color-{index}"); resourceRegistry.RemoveRenderTarget($"history-event-metadata-{index}"); } @@ -791,6 +804,7 @@ public sealed class D3D12Renderer : IAquariumRenderer historyMetadataRenderTargets[index]?.Dispose(); historyControlRenderTargets[index]?.Dispose(); historyMediumPacketRenderTargets[index]?.Dispose(); + historyMediumColorRenderTargets[index]?.Dispose(); historyEventColorRenderTargets[index]?.Dispose(); historyEventMetadataRenderTargets[index]?.Dispose(); } @@ -879,12 +893,14 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneMetadataRenderTarget.Transition(context.CommandList, ResourceStates.RenderTarget); sceneControlRenderTarget.Transition(context.CommandList, ResourceStates.RenderTarget); sceneMediumPacketRenderTarget.Transition(context.CommandList, ResourceStates.RenderTarget); + sceneMediumColorRenderTarget.Transition(context.CommandList, ResourceStates.RenderTarget); sceneEventColorRenderTarget.Transition(context.CommandList, ResourceStates.RenderTarget); sceneEventMetadataRenderTarget.Transition(context.CommandList, ResourceStates.RenderTarget); context.CommandList.ClearRenderTargetView(sceneRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.CommandList.ClearRenderTargetView(sceneMetadataRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.CommandList.ClearRenderTargetView(sceneControlRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.CommandList.ClearRenderTargetView(sceneMediumPacketRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); + context.CommandList.ClearRenderTargetView(sceneMediumColorRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.CommandList.ClearRenderTargetView(sceneEventColorRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.CommandList.ClearRenderTargetView(sceneEventMetadataRenderTarget.RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.CommandList.SetDescriptorHeaps(frameResources.TransientShaderDescriptors.Heap); @@ -903,6 +919,7 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneMetadataRenderTarget.RenderTargetView.Cpu, sceneControlRenderTarget.RenderTargetView.Cpu, sceneMediumPacketRenderTarget.RenderTargetView.Cpu, + sceneMediumColorRenderTarget.RenderTargetView.Cpu, sceneEventColorRenderTarget.RenderTargetView.Cpu, sceneEventMetadataRenderTarget.RenderTargetView.Cpu, ], @@ -1014,24 +1031,28 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneMetadataRenderTarget.Transition(context.CommandList, ResourceStates.PixelShaderResource); sceneControlRenderTarget.Transition(context.CommandList, ResourceStates.PixelShaderResource); sceneMediumPacketRenderTarget.Transition(context.CommandList, ResourceStates.PixelShaderResource); + sceneMediumColorRenderTarget.Transition(context.CommandList, ResourceStates.PixelShaderResource); sceneEventColorRenderTarget.Transition(context.CommandList, ResourceStates.PixelShaderResource); sceneEventMetadataRenderTarget.Transition(context.CommandList, ResourceStates.PixelShaderResource); historyRenderTargets[historyReadIndex].Transition(context.CommandList, ResourceStates.PixelShaderResource); historyMetadataRenderTargets[historyReadIndex].Transition(context.CommandList, ResourceStates.PixelShaderResource); historyControlRenderTargets[historyReadIndex].Transition(context.CommandList, ResourceStates.PixelShaderResource); historyMediumPacketRenderTargets[historyReadIndex].Transition(context.CommandList, ResourceStates.PixelShaderResource); + historyMediumColorRenderTargets[historyReadIndex].Transition(context.CommandList, ResourceStates.PixelShaderResource); historyEventColorRenderTargets[historyReadIndex].Transition(context.CommandList, ResourceStates.PixelShaderResource); historyEventMetadataRenderTargets[historyReadIndex].Transition(context.CommandList, ResourceStates.PixelShaderResource); historyRenderTargets[historyWriteIndex].Transition(context.CommandList, ResourceStates.RenderTarget); historyMetadataRenderTargets[historyWriteIndex].Transition(context.CommandList, ResourceStates.RenderTarget); historyControlRenderTargets[historyWriteIndex].Transition(context.CommandList, ResourceStates.RenderTarget); historyMediumPacketRenderTargets[historyWriteIndex].Transition(context.CommandList, ResourceStates.RenderTarget); + historyMediumColorRenderTargets[historyWriteIndex].Transition(context.CommandList, ResourceStates.RenderTarget); historyEventColorRenderTargets[historyWriteIndex].Transition(context.CommandList, ResourceStates.RenderTarget); historyEventMetadataRenderTargets[historyWriteIndex].Transition(context.CommandList, ResourceStates.RenderTarget); context.CommandList.ClearRenderTargetView(historyRenderTargets[historyWriteIndex].RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.CommandList.ClearRenderTargetView(historyMetadataRenderTargets[historyWriteIndex].RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.CommandList.ClearRenderTargetView(historyControlRenderTargets[historyWriteIndex].RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.CommandList.ClearRenderTargetView(historyMediumPacketRenderTargets[historyWriteIndex].RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); + context.CommandList.ClearRenderTargetView(historyMediumColorRenderTargets[historyWriteIndex].RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.CommandList.ClearRenderTargetView(historyEventColorRenderTargets[historyWriteIndex].RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.CommandList.ClearRenderTargetView(historyEventMetadataRenderTargets[historyWriteIndex].RenderTargetView.Cpu, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); @@ -1055,6 +1076,8 @@ public sealed class D3D12Renderer : IAquariumRenderer context.CommandList.SetGraphicsRootDescriptorTable(16, frameResources.HistoryEventColorDescriptor.Gpu); context.CommandList.SetGraphicsRootDescriptorTable(17, frameResources.HistoryEventMetadataDescriptor.Gpu); context.CommandList.SetGraphicsRootDescriptorTable(18, frameResources.GridHeightDescriptor.Gpu); + context.CommandList.SetGraphicsRootDescriptorTable(19, frameResources.SceneMediumColorDescriptor.Gpu); + context.CommandList.SetGraphicsRootDescriptorTable(20, frameResources.HistoryMediumColorDescriptor.Gpu); context.CommandList.RSSetViewports(viewport); context.CommandList.RSSetScissorRects(scissorRect); @@ -1065,6 +1088,7 @@ public sealed class D3D12Renderer : IAquariumRenderer historyMetadataRenderTargets[historyWriteIndex].RenderTargetView.Cpu, historyControlRenderTargets[historyWriteIndex].RenderTargetView.Cpu, historyMediumPacketRenderTargets[historyWriteIndex].RenderTargetView.Cpu, + historyMediumColorRenderTargets[historyWriteIndex].RenderTargetView.Cpu, historyEventColorRenderTargets[historyWriteIndex].RenderTargetView.Cpu, historyEventMetadataRenderTargets[historyWriteIndex].RenderTargetView.Cpu, ], @@ -1909,6 +1933,18 @@ public sealed class D3D12Renderer : IAquariumRenderer 22, 0, D3D12.DescriptorRangeOffsetAppend); + var currentMediumColorRange = new DescriptorRange( + DescriptorRangeType.ShaderResourceView, + 1, + 23, + 0, + D3D12.DescriptorRangeOffsetAppend); + var historyMediumColorRange = new DescriptorRange( + DescriptorRangeType.ShaderResourceView, + 1, + 24, + 0, + D3D12.DescriptorRangeOffsetAppend); var rootParameters = new[] { new RootParameter(new RootDescriptorTable([constantBufferRange]), ShaderVisibility.All), @@ -1930,6 +1966,8 @@ public sealed class D3D12Renderer : IAquariumRenderer new RootParameter(new RootDescriptorTable([historyEventColorRange]), ShaderVisibility.Pixel), new RootParameter(new RootDescriptorTable([historyEventMetadataRange]), ShaderVisibility.Pixel), new RootParameter(new RootDescriptorTable([gridHeightMediumRange]), ShaderVisibility.Pixel), + new RootParameter(new RootDescriptorTable([currentMediumColorRange]), ShaderVisibility.Pixel), + new RootParameter(new RootDescriptorTable([historyMediumColorRange]), ShaderVisibility.Pixel), }; var staticSamplers = new[] { @@ -1959,7 +1997,7 @@ public sealed class D3D12Renderer : IAquariumRenderer private ID3D12PipelineState CreateScenePipelineState(string path) { - return CreateFullscreenPipelineState(path, "FullscreenTriangleVS", "D3D12ScenePS", [SceneHdrFormat, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat]); + return CreateFullscreenPipelineState(path, "FullscreenTriangleVS", "D3D12ScenePS", [SceneHdrFormat, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat]); } private ID3D12PipelineState CreateMediumVolumePipelineState(string path) @@ -2360,6 +2398,8 @@ public sealed class D3D12Renderer : IAquariumRenderer public D3D12DescriptorSlot SceneMediumPacketDescriptor { get; set; } + public D3D12DescriptorSlot SceneMediumColorDescriptor { get; set; } + public D3D12DescriptorSlot SceneEventColorDescriptor { get; set; } public D3D12DescriptorSlot SceneEventMetadataDescriptor { get; set; } @@ -2372,6 +2412,8 @@ public sealed class D3D12Renderer : IAquariumRenderer public D3D12DescriptorSlot HistoryMediumPacketDescriptor { get; set; } + public D3D12DescriptorSlot HistoryMediumColorDescriptor { get; set; } + public D3D12DescriptorSlot HistoryEventColorDescriptor { get; set; } public D3D12DescriptorSlot HistoryEventMetadataDescriptor { get; set; } diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl index 2880e49..c0ed229 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl @@ -38,6 +38,8 @@ Texture2D currentEventMetadataTexture : register(t19); Texture2D historyEventColorTexture : register(t20); Texture2D historyEventMetadataTexture : register(t21); Texture2D gridHeightTexture : register(t22); +Texture2D currentMediumColorTexture : register(t23); +Texture2D historyMediumColorTexture : register(t24); SamplerState sourceSampler : register(s0); struct FieldInstance @@ -65,8 +67,9 @@ struct ResolveOut float4 historyMetadata : SV_Target2; float4 historyControl : SV_Target3; float4 historyMediumPacket : SV_Target4; - float4 historyEventColor : SV_Target5; - float4 historyEventMetadata : SV_Target6; + float4 historyMediumColor : SV_Target5; + float4 historyEventColor : SV_Target6; + float4 historyEventMetadata : SV_Target7; }; static const float SUN_RADIUS = 1.12; @@ -255,6 +258,16 @@ float4 loadHistoryMediumPacket(float2 uv) return historyMediumPacketTexture.Load(int3(pixelFromUv(uv), 0)); } +float4 loadCurrentMediumColor(float2 uv) +{ + return currentMediumColorTexture.Load(int3(pixelFromUv(uv), 0)); +} + +float4 loadHistoryMediumColor(float2 uv) +{ + return historyMediumColorTexture.Load(int3(pixelFromUv(uv), 0)); +} + float4 loadCurrentEventColor(float2 uv) { return currentEventColorTexture.Load(int3(pixelFromUv(uv), 0)); @@ -593,6 +606,8 @@ ResolveOut D3D12ResolvePS(VertexOut input) float currentMediumTravel = currentMediumPacket.y; float currentMediumLaneOpacity = saturate(currentMediumPacket.z); float currentMediumDensity = saturate(currentMediumPacket.w); + float3 currentMediumRadiance = loadCurrentMediumColor(input.uv).rgb; + currentColor = max(currentColor - currentMediumRadiance, 0.0); float4 currentEventColor = loadCurrentEventColor(input.uv); float4 currentEventMetadata = loadCurrentEventMetadata(input.uv); float currentEventFieldId = currentEventMetadata.x; @@ -606,6 +621,7 @@ ResolveOut D3D12ResolvePS(VertexOut input) float3 historyColor = currentColor; float mediumHistoryWeight = 0.0; float mediumHistoryAge = 0.0; + float3 mediumHistoryColor = currentMediumRadiance; float eventHistoryWeight = 0.0; float eventHistoryAge = 0.0; float3 eventHistoryColor = currentEventRadiance; @@ -676,8 +692,10 @@ ResolveOut D3D12ResolvePS(VertexOut input) float opacityWeight = 1.0 - smoothstep(0.06, 0.42, abs(previousMediumOpacity - currentMediumLaneOpacity)); float densityWeight = 1.0 - smoothstep(0.06, 0.42, abs(previousMediumDensity - currentMediumDensity)); float validationWeight = travelWeight * opacityWeight * densityWeight; - mediumHistoryWeight = 0.0; + float mediumConfidence = smoothstep(0.0, 6.0, previousMediumAge); + mediumHistoryWeight = 0.74 * lerp(0.22, 1.0, mediumConfidence) * validationWeight * smoothstep(0.015, 0.35, currentMediumLaneOpacity); mediumHistoryAge = validationWeight > 0.01 ? min(previousMediumAge + 1.0, MAX_HISTORY_AGE) : 0.0; + mediumHistoryColor = loadHistoryMediumColor(previousMediumUv).rgb; } } @@ -707,18 +725,19 @@ ResolveOut D3D12ResolvePS(VertexOut input) } } - float combinedHistoryWeight = max(historyWeight, mediumHistoryWeight); + float combinedHistoryWeight = historyWeight; float combinedHistoryAge = max(max(historyAge, mediumHistoryAge), eventHistoryAge); float3 resolved = lerp(currentColor, historyColor, combinedHistoryWeight); + float3 resolvedMedium = lerp(currentMediumRadiance, mediumHistoryColor, mediumHistoryWeight); float3 resolvedEvent = lerp(currentEventRadiance, eventHistoryColor, eventHistoryWeight); - float3 finalColor = presentColor(resolved + resolvedEvent, input.uv); + float3 finalColor = presentColor(resolved + resolvedMedium + resolvedEvent, input.uv); if (renderDebugMode > 0.5 && renderDebugMode < 1.5) { finalColor = aces((rawCurrentColor + currentEventRadiance) * max(exposure, 0.001)); } else if (renderDebugMode >= 1.5 && renderDebugMode < 2.5) { - finalColor = aces((historyColor + eventHistoryColor) * max(exposure, 0.001)); + finalColor = aces((historyColor + mediumHistoryColor + eventHistoryColor) * max(exposure, 0.001)); } else if (renderDebugMode >= 2.5 && renderDebugMode < 3.5) { @@ -769,6 +788,7 @@ ResolveOut D3D12ResolvePS(VertexOut input) output.historyMetadata = currentMetadata; output.historyControl = float4(currentControl.xyz, combinedHistoryAge); output.historyMediumPacket = float4(mediumHistoryAge, currentMediumTravel, currentMediumLaneOpacity, currentMediumDensity); + output.historyMediumColor = float4(resolvedMedium, currentMediumLaneOpacity); output.historyEventColor = float4(resolvedEvent, currentEventCoverage); output.historyEventMetadata = float4(eventHistoryAge, currentEventTravel, currentEventCoverage, currentEventFieldId); return output; diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl index f19da9b..f1072c1 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl @@ -82,8 +82,9 @@ struct SceneOut float4 metadata : SV_Target1; float4 control : SV_Target2; float4 mediumPacket : SV_Target3; - float4 eventColor : SV_Target4; - float4 eventMetadata : SV_Target5; + float4 mediumColor : SV_Target4; + float4 eventColor : SV_Target5; + float4 eventMetadata : SV_Target6; }; struct SolidHit @@ -105,6 +106,7 @@ struct RayMarchResult float mediumOpacity; float densityMean; float mediumTravel; + float3 mediumColor; float eventTravel; float eventCoverage; float3 eventColor; @@ -644,6 +646,7 @@ RayMarchResult traverseRay(float2 uv, float2 screenUv, float3 origin, float3 dir result.mediumOpacity = 0.0; result.densityMean = 0.0; result.mediumTravel = farDistance + 1.0; + result.mediumColor = 0.0; result.eventTravel = farDistance + 1.0; result.eventCoverage = 0.0; result.eventColor = 0.0; @@ -706,6 +709,7 @@ RayMarchResult traverseRay(float2 uv, float2 screenUv, float3 origin, float3 dir result.mediumOpacity = mediumOpacity; result.densityMean = mediumDensityMean; result.mediumTravel = mediumTravel; + result.mediumColor = inScattering; } else { @@ -713,6 +717,7 @@ RayMarchResult traverseRay(float2 uv, float2 screenUv, float3 origin, float3 dir result.mediumOpacity = mediumOpacity; result.densityMean = mediumDensityMean; result.mediumTravel = mediumTravel; + result.mediumColor = inScattering; if (result.mediumOpacity > 0.015) { result.fieldId = FIELD_ID_MEDIUM; @@ -863,6 +868,7 @@ SceneOut D3D12ScenePS(VertexOut input) output.metadata = float4(result.fieldId, result.normal); output.control = float4(0.0, result.coverage, result.mediumOpacity, result.densityMean); output.mediumPacket = float4(FIELD_ID_MEDIUM, result.mediumTravel, result.mediumOpacity, result.densityMean); + output.mediumColor = float4(result.mediumColor, result.mediumOpacity); output.eventColor = float4(result.eventColor, result.eventCoverage); output.eventMetadata = float4(FIELD_ID_GRID, result.eventTravel, result.eventCoverage, 0.0); return output; diff --git a/state/evidence.jsonl b/state/evidence.jsonl index dd6a8e9..71d76e5 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -130,3 +130,4 @@ {"timestamp":"2026-05-10T08:15:20+01:00","topic":"emissive-only-fog-lighting","lesson":"D3D12 fog density no longer contains baked scattering color, and solid shading no longer uses a point-light radiance constant. Self is treated as a finite emissive sphere: medium in-scattering uses solid angle, Henyey-Greenstein phase, and cheap light-path transmittance; surfaces use emissive-sphere irradiance. Full noisy density inside the light-path propagation loop stalled shader pipeline compilation, so propagation uses a cheap extinction approximation while visible samples keep textured density.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"]} {"timestamp":"2026-05-10T08:22:10+01:00","topic":"field-emitter-light-injection","lesson":"The direct medium/surface lighting path now uses field-emitter data instead of hardcoded Self helpers. `FieldFlags.Emitter` plus `colorIntensity.rgb` inject radiance into D3D12 froxel medium samples and drive surface diffuse/specular lighting from the same table. Cursor is present as field 5 with zero emission until enabled; nebula/cloud fields can become emissive through data instead of bespoke shader branches.","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","state/memory.json","state/scratch.md"]} {"timestamp":"2026-05-10T08:27:10+01:00","topic":"medium-temporal-ghosting","lesson":"D3D12 medium color history caused strong fog ghosting after emissive injection: medium-classified pixels used the main surface-history lane and the medium lane also blended previous full-frame color. Final resolve now excludes medium pixels from surface history and keeps mediumHistoryWeight at zero. Medium packet age/travel/density may remain diagnostic, but fog color history needs a real light-volume/medium reprojection contract before it can affect final presentation.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","state/memory.json","state/scratch.md"]} +{"timestamp":"2026-05-10T08:38:10+01:00","topic":"medium-radiance-reprojection","lesson":"D3D12 medium radiance now has a separate temporal lane instead of borrowing final scene history. Scene writes `sceneMediumColor`; resolve subtracts current medium from scene color before surface history, reprojects medium packet travel to sample `historyMediumColor`, validates by medium travel/opacity/density, and composites resolved surface plus resolved medium plus events. Cold shader compilation may exceed the default 30s headless watchdog, but warm verification passes.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","state/memory.json","state/scratch.md"]} diff --git a/state/memory.json b/state/memory.json index 5ec6fd1..9b09489 100644 --- a/state/memory.json +++ b/state/memory.json @@ -54,6 +54,7 @@ "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 direct fog lighting injects radiance from `FieldFlags.Emitter` instances into froxel samples, using emitter `colorIntensity.rgb`, finite solid angle, phase, and cheap depth/radial light-path attenuation so shader compilation stays sane. 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. This is direct emitter propagation in froxels plus reprojection; full multi-bounce GI remains 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.", "For GPU SDF or newer volumetric storage, consult research/volumetrics/recent-volumetrics-and-gpu-sdf.md. The durable pattern is cascaded sparse bricks, compressed distance/occupancy, separate static/dynamic updates, toroidal partial updates, and learned fields only when runtime evaluation stays cheap and inspectable.", "For research decks with recordings, pull subtitles/transcripts before distilling. The Dreams transcript changed the lesson: preserve the evaluator/field that serves the image, not the elegant SDF/brick/froxel experiment that drifted away from the art direction.", diff --git a/state/scratch.md b/state/scratch.md index 4875f0d..1b48c52 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -103,6 +103,15 @@ reusing field identity/travel/control. silhouettes. Until there is a proper light-volume/medium-history reprojection contract, medium packets may track age/debug metadata but must not blend previous resolved color into final presentation. +- Medium propagation/reprojection lane: D3D12 now gives medium radiance its own + color lane. The scene pass emits `sceneMediumColor` alongside the medium + packet; resolve subtracts current medium radiance from the scene color so + surface history stores surface-only color, reprojects medium packet travel to + fetch `historyMediumColor`, validates by medium travel/opacity/density, and + composites resolved surface + resolved medium + resolved stochastic events. + This is direct froxel emitter propagation plus medium-color reprojection, not + full multi-bounce GI. A cold shader build for this wider scene/resolve shape + may exceed the 30s headless watchdog; warm/default verification passes. - Cursor locator SDF: `AquariumFrame` carries the mouse cursor projected onto the Grid XY plane. D3D12 bins a cursor primitive into the existing view-froxel solid table and traces it as an asymmetric teardrop lobe: local `z = -1`