From 2a32bcc4ef319934a6ab9b2e1fbf199e4ccded5c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 9 May 2026 23:44:47 +0100 Subject: [PATCH] Split medium temporal packet from surface identity --- src/Aquarium.Engine/Render/D3D12Renderer.cs | 48 ++++++++++++++- .../Render/Shaders/D3D12Post.hlsl | 61 +++++++++++++++++-- .../Render/Shaders/D3D12Scene.hlsl | 19 ++++-- state/evidence.jsonl | 1 + state/memory.json | 2 +- state/scratch.md | 7 +++ 6 files changed, 123 insertions(+), 15 deletions(-) diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 9272af2..3e1af37 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -96,9 +96,11 @@ public sealed class D3D12Renderer : IAquariumRenderer private D3D12RenderTarget sceneRenderTarget; private D3D12RenderTarget sceneMetadataRenderTarget; private D3D12RenderTarget sceneControlRenderTarget; + private D3D12RenderTarget sceneMediumPacketRenderTarget; 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[] bloomRenderTargets = new D3D12RenderTarget[BloomLevelCount]; private readonly D3D12RenderTarget[] bloomScratchTargets = new D3D12RenderTarget[BloomLevelCount]; private D3D12StructuredBuffer froxelPrimitiveBuffer; @@ -198,6 +200,7 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneRenderTarget = CreateSceneRenderTarget(); 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"); CreateHistoryRenderTargets(); CreateBloomRenderTargets(); froxelPrimitiveBuffer = CreateViewFroxelPrimitiveBuffer(); @@ -349,6 +352,8 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneMetadataRenderTarget.CreateShaderResourceView(device, frameResources.SceneMetadataDescriptor); frameResources.SceneControlDescriptor = frameResources.TransientShaderDescriptors.Allocate(); sceneControlRenderTarget.CreateShaderResourceView(device, frameResources.SceneControlDescriptor); + frameResources.SceneMediumPacketDescriptor = frameResources.TransientShaderDescriptors.Allocate(); + sceneMediumPacketRenderTarget.CreateShaderResourceView(device, frameResources.SceneMediumPacketDescriptor); var historyReadIndex = temporalFrameIndex & 1; frameResources.HistoryDescriptor = frameResources.TransientShaderDescriptors.Allocate(); historyRenderTargets[historyReadIndex].CreateShaderResourceView(device, frameResources.HistoryDescriptor); @@ -356,6 +361,8 @@ public sealed class D3D12Renderer : IAquariumRenderer historyMetadataRenderTargets[historyReadIndex].CreateShaderResourceView(device, frameResources.HistoryMetadataDescriptor); frameResources.HistoryControlDescriptor = frameResources.TransientShaderDescriptors.Allocate(); historyControlRenderTargets[historyReadIndex].CreateShaderResourceView(device, frameResources.HistoryControlDescriptor); + frameResources.HistoryMediumPacketDescriptor = frameResources.TransientShaderDescriptors.Allocate(); + historyMediumPacketRenderTargets[historyReadIndex].CreateShaderResourceView(device, frameResources.HistoryMediumPacketDescriptor); for (var level = 0; level < BloomLevelCount; level++) { frameResources.BloomDescriptors[level] = frameResources.TransientShaderDescriptors.Allocate(); @@ -468,6 +475,7 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneRenderTarget.Dispose(); sceneMetadataRenderTarget.Dispose(); sceneControlRenderTarget.Dispose(); + sceneMediumPacketRenderTarget.Dispose(); mediumTransportRenderTarget.Dispose(); mediumVolumeRenderTarget.Dispose(); gridHeightRenderTarget.Dispose(); @@ -632,6 +640,7 @@ public sealed class D3D12Renderer : IAquariumRenderer resourceRegistry.RemoveRenderTarget("scene-hdr-target"); resourceRegistry.RemoveRenderTarget("scene-metadata-target"); resourceRegistry.RemoveRenderTarget("scene-control-target"); + resourceRegistry.RemoveRenderTarget("scene-medium-packet-target"); resourceRegistry.RemoveRenderTarget("medium-transport-target"); resourceRegistry.RemoveRenderTarget("medium-event-target"); resourceRegistry.RemoveRenderTarget("medium-volume-target"); @@ -642,6 +651,7 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneRenderTarget.Dispose(); sceneMetadataRenderTarget.Dispose(); sceneControlRenderTarget.Dispose(); + sceneMediumPacketRenderTarget.Dispose(); mediumTransportRenderTarget.Dispose(); mediumVolumeRenderTarget.Dispose(); gridHeightRenderTarget.Dispose(); @@ -672,6 +682,7 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneRenderTarget = CreateSceneRenderTarget(); 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"); CreateHistoryRenderTargets(); CreateBloomRenderTargets(); viewport = new Viewport(0.0f, 0.0f, width, height); @@ -729,6 +740,7 @@ public sealed class D3D12Renderer : IAquariumRenderer historyRenderTargets[index] = CreateSceneAuxiliaryRenderTarget($"history-{index}", $"Aquarium D3D12 History {index}"); 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}"); } } @@ -739,6 +751,7 @@ public sealed class D3D12Renderer : IAquariumRenderer resourceRegistry.RemoveRenderTarget($"history-{index}"); resourceRegistry.RemoveRenderTarget($"history-metadata-{index}"); resourceRegistry.RemoveRenderTarget($"history-control-{index}"); + resourceRegistry.RemoveRenderTarget($"history-medium-packet-{index}"); } } @@ -749,6 +762,7 @@ public sealed class D3D12Renderer : IAquariumRenderer historyRenderTargets[index]?.Dispose(); historyMetadataRenderTargets[index]?.Dispose(); historyControlRenderTargets[index]?.Dispose(); + historyMediumPacketRenderTargets[index]?.Dispose(); } } @@ -878,9 +892,11 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneRenderTarget.Transition(context.CommandList, ResourceStates.RenderTarget); sceneMetadataRenderTarget.Transition(context.CommandList, ResourceStates.RenderTarget); sceneControlRenderTarget.Transition(context.CommandList, ResourceStates.RenderTarget); + sceneMediumPacketRenderTarget.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.SetDescriptorHeaps(frameResources.TransientShaderDescriptors.Heap); context.CommandList.SetPipelineState(scenePipelineState!); context.CommandList.SetGraphicsRootSignature(fullscreenRootSignature); @@ -897,6 +913,7 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneRenderTarget.RenderTargetView.Cpu, sceneMetadataRenderTarget.RenderTargetView.Cpu, sceneControlRenderTarget.RenderTargetView.Cpu, + sceneMediumPacketRenderTarget.RenderTargetView.Cpu, ], null); context.CommandList.IASetPrimitiveTopology(PrimitiveTopology.TriangleList); @@ -1005,15 +1022,19 @@ public sealed class D3D12Renderer : IAquariumRenderer sceneRenderTarget.Transition(context.CommandList, ResourceStates.PixelShaderResource); sceneMetadataRenderTarget.Transition(context.CommandList, ResourceStates.PixelShaderResource); sceneControlRenderTarget.Transition(context.CommandList, ResourceStates.PixelShaderResource); + sceneMediumPacketRenderTarget.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); 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); 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.BackBuffer.Transition(context.CommandList, ResourceStates.RenderTarget); context.CommandList.SetDescriptorHeaps(frameResources.TransientShaderDescriptors.Heap); @@ -1028,6 +1049,8 @@ public sealed class D3D12Renderer : IAquariumRenderer context.CommandList.SetGraphicsRootDescriptorTable(9, frameResources.HistoryDescriptor.Gpu); context.CommandList.SetGraphicsRootDescriptorTable(10, frameResources.HistoryMetadataDescriptor.Gpu); context.CommandList.SetGraphicsRootDescriptorTable(11, frameResources.HistoryControlDescriptor.Gpu); + context.CommandList.SetGraphicsRootDescriptorTable(13, frameResources.SceneMediumPacketDescriptor.Gpu); + context.CommandList.SetGraphicsRootDescriptorTable(14, frameResources.HistoryMediumPacketDescriptor.Gpu); context.CommandList.RSSetViewports(viewport); context.CommandList.RSSetScissorRects(scissorRect); @@ -1037,6 +1060,7 @@ public sealed class D3D12Renderer : IAquariumRenderer historyRenderTargets[historyWriteIndex].RenderTargetView.Cpu, historyMetadataRenderTargets[historyWriteIndex].RenderTargetView.Cpu, historyControlRenderTargets[historyWriteIndex].RenderTargetView.Cpu, + historyMediumPacketRenderTargets[historyWriteIndex].RenderTargetView.Cpu, ], null); context.CommandList.IASetPrimitiveTopology(PrimitiveTopology.TriangleList); @@ -1656,7 +1680,7 @@ public sealed class D3D12Renderer : IAquariumRenderer { if (!PipelinesReady && !pipelineBuildInProgressReported) { - Console.WriteLine("D3D12 shader pipeline build still running; presenting loading frame."); + Console.WriteLine("D3D12 shader pipeline build still running; waiting behind startup splash."); pipelineBuildInProgressReported = true; } @@ -1866,6 +1890,18 @@ public sealed class D3D12Renderer : IAquariumRenderer 8, 0, D3D12.DescriptorRangeOffsetAppend); + var currentMediumPacketRange = new DescriptorRange( + DescriptorRangeType.ShaderResourceView, + 1, + 16, + 0, + D3D12.DescriptorRangeOffsetAppend); + var historyMediumPacketRange = new DescriptorRange( + DescriptorRangeType.ShaderResourceView, + 1, + 17, + 0, + D3D12.DescriptorRangeOffsetAppend); var ditherRange = new DescriptorRange( DescriptorRangeType.ShaderResourceView, 1, @@ -1887,6 +1923,8 @@ public sealed class D3D12Renderer : IAquariumRenderer new RootParameter(new RootDescriptorTable([historyMetadataRange]), ShaderVisibility.Pixel), new RootParameter(new RootDescriptorTable([historyControlRange]), ShaderVisibility.Pixel), new RootParameter(new RootDescriptorTable([ditherRange]), ShaderVisibility.Pixel), + new RootParameter(new RootDescriptorTable([currentMediumPacketRange]), ShaderVisibility.Pixel), + new RootParameter(new RootDescriptorTable([historyMediumPacketRange]), ShaderVisibility.Pixel), }; var staticSamplers = new[] { @@ -1930,7 +1968,7 @@ public sealed class D3D12Renderer : IAquariumRenderer private ID3D12PipelineState CreateScenePipelineState(string path) { - return CreateFullscreenPipelineState(path, "FullscreenTriangleVS", "D3D12ScenePS", [SceneHdrFormat, SceneHdrFormat, SceneHdrFormat]); + return CreateFullscreenPipelineState(path, "FullscreenTriangleVS", "D3D12ScenePS", [SceneHdrFormat, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat]); } private ID3D12PipelineState CreateMediumVolumePipelineState(string path) @@ -1973,7 +2011,7 @@ public sealed class D3D12Renderer : IAquariumRenderer path, "FullscreenTriangleVS", "D3D12ResolvePS", - [Format.B8G8R8A8_UNorm, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat]); + [Format.B8G8R8A8_UNorm, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat, SceneHdrFormat]); } private ID3D12PipelineState CreateGridHeightBasePipelineState(string path) @@ -2326,12 +2364,16 @@ public sealed class D3D12Renderer : IAquariumRenderer public D3D12DescriptorSlot SceneControlDescriptor { get; set; } + public D3D12DescriptorSlot SceneMediumPacketDescriptor { get; set; } + public D3D12DescriptorSlot HistoryDescriptor { get; set; } public D3D12DescriptorSlot HistoryMetadataDescriptor { get; set; } public D3D12DescriptorSlot HistoryControlDescriptor { get; set; } + public D3D12DescriptorSlot HistoryMediumPacketDescriptor { get; set; } + public D3D12DescriptorSlot[] BloomDescriptors { get; } = new D3D12DescriptorSlot[BloomLevelCount]; public D3D12DescriptorSlot[] BloomScratchDescriptors { get; } = new D3D12DescriptorSlot[BloomLevelCount]; diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl index 0dfe52e..9821f12 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl @@ -31,6 +31,8 @@ Texture2D historyControlTexture : register(t8); Texture2D bloomTexture0 : register(t9); Texture2D bloomTexture1 : register(t10); Texture2D bloomTexture2 : register(t11); +Texture2D currentMediumPacketTexture : register(t16); +Texture2D historyMediumPacketTexture : register(t17); SamplerState sourceSampler : register(s0); struct FieldInstance @@ -57,6 +59,7 @@ struct ResolveOut float4 historyColor : SV_Target1; float4 historyMetadata : SV_Target2; float4 historyControl : SV_Target3; + float4 historyMediumPacket : SV_Target4; }; static const float SUN_RADIUS = 1.12; @@ -221,6 +224,16 @@ float4 loadHistoryControl(float2 uv) return historyControlTexture.Load(int3(pixelFromUv(uv), 0)); } +float4 loadCurrentMediumPacket(float2 uv) +{ + return currentMediumPacketTexture.Load(int3(pixelFromUv(uv), 0)); +} + +float4 loadHistoryMediumPacket(float2 uv) +{ + return historyMediumPacketTexture.Load(int3(pixelFromUv(uv), 0)); +} + void currentNeighborhood(float2 uv, out float3 neighborhoodMin, out float3 neighborhoodMax) { float2 texel = 1.0 / resolution; @@ -461,12 +474,18 @@ ResolveOut D3D12ResolvePS(VertexOut input) float currentReactive = saturate(currentControl.x); float currentCoverage = saturate(currentControl.y); float currentMediumOpacity = saturate(currentControl.z); + float4 currentMediumPacket = loadCurrentMediumPacket(input.uv); + float currentMediumTravel = currentMediumPacket.y; + float currentMediumLaneOpacity = saturate(currentMediumPacket.z); + float currentMediumDensity = saturate(currentMediumPacket.w); bool currentIsMedium = abs(currentFieldId - FIELD_ID_MEDIUM) < 0.25; bool currentIsDistributed = currentIsMedium; float historyWeight = 0.0; float historyAge = 0.0; float3 historyColor = currentColor; + float mediumHistoryWeight = 0.0; + float mediumHistoryAge = 0.0; if (frameIndex > 0.5 && currentTravel <= farDistance && currentFieldId > 0.5) { float3 currentRay = rayDirectionForPixel(pixel, jitterPixels, cameraPosition, gridCenter); @@ -521,7 +540,36 @@ ResolveOut D3D12ResolvePS(VertexOut input) } } - float3 resolved = lerp(currentColor, historyColor, historyWeight); + if (frameIndex > 0.5 && currentMediumLaneOpacity > 0.015 && currentMediumTravel <= farDistance) + { + float3 currentRay = rayDirectionForPixel(pixel, jitterPixels, cameraPosition, gridCenter); + float3 mediumWorldPosition = cameraPosition + currentRay * currentMediumTravel; + float2 previousMediumUv = projectWorldToPreviousHistoryUv(mediumWorldPosition); + if (all(previousMediumUv >= 0.0) && all(previousMediumUv <= 1.0)) + { + float4 previousMediumPacket = loadHistoryMediumPacket(previousMediumUv); + float previousMediumTravel = previousMediumPacket.y; + float previousMediumOpacity = saturate(previousMediumPacket.z); + float previousMediumDensity = saturate(previousMediumPacket.w); + float previousMediumAge = max(previousMediumPacket.x, 0.0); + float expectedPreviousMediumTravel = distance(previousCameraPosition, mediumWorldPosition); + float travelDelta = abs(previousMediumTravel - expectedPreviousMediumTravel); + float travelTolerance = max(0.08, expectedPreviousMediumTravel * 0.035); + float travelWeight = 1.0 - smoothstep(travelTolerance, travelTolerance * 5.0, travelDelta); + 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 mediumConfidence = smoothstep(0.0, 6.0, previousMediumAge); + float validationWeight = travelWeight * opacityWeight * densityWeight; + mediumHistoryWeight = 0.88 * lerp(0.48, 1.0, mediumConfidence) * validationWeight * smoothstep(0.015, 0.35, currentMediumLaneOpacity); + mediumHistoryAge = validationWeight > 0.01 ? min(previousMediumAge + 1.0, MAX_HISTORY_AGE) : 0.0; + float4 previous = historyTexture.SampleLevel(sourceSampler, previousMediumUv, 0.0); + historyColor = lerp(historyColor, previous.rgb, saturate(mediumHistoryWeight / max(max(historyWeight, mediumHistoryWeight), 0.0001))); + } + } + + float combinedHistoryWeight = max(historyWeight, mediumHistoryWeight); + float combinedHistoryAge = max(historyAge, mediumHistoryAge); + float3 resolved = lerp(currentColor, historyColor, combinedHistoryWeight); float3 finalColor = presentColor(resolved, input.uv); if (renderDebugMode > 0.5 && renderDebugMode < 1.5) { @@ -533,19 +581,19 @@ ResolveOut D3D12ResolvePS(VertexOut input) } else if (renderDebugMode >= 2.5 && renderDebugMode < 3.5) { - finalColor = (historyAge / MAX_HISTORY_AGE).xxx; + finalColor = (combinedHistoryAge / MAX_HISTORY_AGE).xxx; } else if (renderDebugMode >= 3.5 && renderDebugMode < 4.5) { - finalColor = historyWeight.xxx; + finalColor = combinedHistoryWeight.xxx; } else if (renderDebugMode >= 4.5 && renderDebugMode < 5.5) { - finalColor = saturate(currentControl.xyz); + finalColor = saturate(float3(currentReactive, max(currentCoverage, currentMediumLaneOpacity), currentMediumOpacity)); } else if (renderDebugMode >= 5.5 && renderDebugMode < 6.5) { - finalColor = debugFieldIdColor(currentFieldId); + finalColor = currentMediumLaneOpacity > 0.015 ? debugFieldIdColor(FIELD_ID_MEDIUM) : debugFieldIdColor(currentFieldId); } else if (renderDebugMode >= 6.5 && renderDebugMode < 7.5) { @@ -578,6 +626,7 @@ ResolveOut D3D12ResolvePS(VertexOut input) output.finalColor = float4(finalColor, 1.0); output.historyColor = float4(resolved, currentTravel); output.historyMetadata = currentMetadata; - output.historyControl = float4(currentControl.xyz, historyAge); + output.historyControl = float4(currentControl.xyz, combinedHistoryAge); + output.historyMediumPacket = float4(mediumHistoryAge, currentMediumTravel, currentMediumLaneOpacity, currentMediumDensity); return output; } diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl index 8a4c066..ad9e0ca 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl @@ -65,6 +65,7 @@ struct SceneOut float4 colorTravel : SV_Target0; float4 metadata : SV_Target1; float4 control : SV_Target2; + float4 mediumPacket : SV_Target3; }; struct SolidHit @@ -85,6 +86,7 @@ struct RayMarchResult float coverage; float mediumOpacity; float densityMean; + float mediumTravel; }; VertexOut FullscreenTriangleVS(uint vertexId : SV_VertexID) @@ -508,6 +510,7 @@ RayMarchResult traverseRay(float2 uv, float2 screenUv, float3 origin, float3 dir result.coverage = 0.0; result.mediumOpacity = 0.0; result.densityMean = 0.0; + result.mediumTravel = farDistance + 1.0; float transmittance = 1.0; float3 inScattering = 0.0; @@ -554,6 +557,9 @@ RayMarchResult traverseRay(float2 uv, float2 screenUv, float3 origin, float3 dir } integrateMediumRange(uv, 0.0, stopTravel, transmittance, inScattering, densityAccumulation, densityTravelSum, densitySum); + float mediumOpacity = saturate(1.0 - transmittance); + float mediumDensityMean = saturate(densityAccumulation / (float)MEDIUM_FROXEL_SLICE_COUNT); + float mediumTravel = densitySum > 0.0001 ? min(densityTravelSum / densitySum, stopTravel) : farDistance + 1.0; bool drawGrid = gridHit && stochasticTransparency(screenUv, gridAlpha) > 0.0; @@ -566,20 +572,22 @@ RayMarchResult traverseRay(float2 uv, float2 screenUv, float3 origin, float3 dir result.fieldId = nearestSolid.fieldId; result.normal = nearestSolid.normal; result.coverage = 1.0; - result.mediumOpacity = saturate(1.0 - transmittance); - result.densityMean = saturate(densityAccumulation / (float)MEDIUM_FROXEL_SLICE_COUNT); + result.mediumOpacity = mediumOpacity; + result.densityMean = mediumDensityMean; + result.mediumTravel = mediumTravel; } else { result.color = result.color * transmittance + inScattering; - result.mediumOpacity = saturate(1.0 - transmittance); - result.densityMean = saturate(densityAccumulation / (float)MEDIUM_FROXEL_SLICE_COUNT); + result.mediumOpacity = mediumOpacity; + result.densityMean = mediumDensityMean; + result.mediumTravel = mediumTravel; if (result.mediumOpacity > 0.015) { result.fieldId = FIELD_ID_MEDIUM; result.normal = -direction; result.coverage = saturate(max(result.mediumOpacity, result.densityMean * 4.0)); - result.travel = densitySum > 0.0001 ? min(densityTravelSum / densitySum, farDistance) : farDistance; + result.travel = mediumTravel; } } @@ -623,5 +631,6 @@ SceneOut D3D12ScenePS(VertexOut input) output.colorTravel = float4(result.color, min(result.travel, farDistance + 1.0)); 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); return output; } diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 9f7646c..4139d60 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -108,3 +108,4 @@ {"timestamp":"2026-05-09T23:16:34.0000000+01:00","topic":"startup-splash-ready-gate","lesson":"Do not present D3D12 loading frames before the initial shader pipelines are ready. Presenting a black/loading swapchain frame takes visible ownership away from the Win32 splash, and DirectWrite loading text can persist into later frames because it wrote into a live backbuffer. While the async pipeline task runs, the renderer should avoid touching the swapchain; the host can keep repainting the GDI splash until HasPresentedReadyFrame becomes true.","files":["src/Aquarium.Engine/AquariumHost.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","state/evidence.jsonl"]} {"timestamp":"2026-05-09T23:24:18.0000000+01:00","topic":"startup-splash-flicker-followup","lesson":"Current D3D12 startup splash behavior works but flickers slightly while the host repaints the Win32/GDI splash during async shader pipeline compilation. Treat that as a polish debt, not a reason to reintroduce swapchain loading frames. Future fix should either repaint only on invalidation/throttled intervals or use a proper pre-renderer splash state/window before D3D12 begins presenting.","files":["state/memory.json","state/scratch.md","state/evidence.jsonl"]} {"timestamp":"2026-05-09T23:29:52.0000000+01:00","topic":"d3d12-grid-volumetric-ordering","lesson":"A transparent Grid overlay must not become the visible scene packet. D3D12 was writing FIELD_ID_GRID, Grid travel, normal, coverage, and medium-to-Grid opacity even when fog/solid content behind it was the real scene. That made temporal debug views and volumetric history classify or clip fog by the Grid. The Grid is now a non-terminating stochastic color overlay added after solid/medium composition, while field identity/travel/control stay owned by the actual opaque or medium result. Future Grid stabilization needs a separate transparent-overlay temporal lane, not reuse of the scene packet.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","src/Aquarium.Engine/Render/D3D12Renderer.cs","state/memory.json","state/scratch.md"]} +{"timestamp":"2026-05-09T23:45:11.0000000+01:00","topic":"d3d12-medium-temporal-lane","lesson":"Surface identity and medium identity cannot share one temporal metadata packet. A medium in front of a planet should have its own opacity, centroid travel, density, history age, and history weight even though the planet remains the hard ray terminator. D3D12 now emits a separate medium packet MRT and ping-pongs medium history beside surface history. Debug field identity can show medium where opacity exists; Grid/particles should follow with a separate stochastic-event packet rather than stealing surface or medium identity.","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 d85e97f..1049068 100644 --- a/state/memory.json +++ b/state/memory.json @@ -19,7 +19,7 @@ "D3D12 previous-frame camera/Grid/time state must be renderer-level temporal state, not per-swapchain-frame-resource state. The swapchain image index is an allocator/reuse index, not frame history.", "D3D12 Grid height must be brush-rendered, not a fullscreen per-texel loop over bodies. Use a top-down Grid-space orthographic brush pass: base field draw, then one additive up-facing quad per body into a scalar R16_Float height target. Height is one value; do not spend extra channels unless the pass contract actually gains more data. Do not use R32G32B32A32_Float for additive brush composition.", "D3D12 field data should live in default-heap structured buffers, updated from the per-frame upload ring by CopyBufferRegion, then bound through transient SRV descriptors owned by the consuming pass. Do not make shaders read dynamic field tables straight from upload memory. D3D11 is reference-only during migration; do not pay DRY tax to preserve it.", - "Do not put the D3D12 Grid through transparent froxel candidate/event traversal. The one-pass transparent binning experiment made the Grid angle-dependent and brittle. The live Grid contract is direct scene tracing as a non-terminating stochastic color overlay before the nearest solid. Grid must not write visible scene identity/travel/control because volumetrics and temporal debug views belong to the actual opaque/medium packet. A future transparent-overlay temporal lane can stabilize Grid without stealing the scene packet. Git holds the old experiment; runtime code should stay lean.", + "Do not put the D3D12 Grid through transparent froxel candidate/event traversal. The one-pass transparent binning experiment made the Grid angle-dependent and brittle. The live Grid contract is direct scene tracing as a non-terminating stochastic color overlay before the nearest solid. Grid must not write the surface packet. Medium now has a separate temporal packet even when a solid is behind it; Grid needs the same kind of separate stochastic-event packet rather than stealing surface or medium identity. Git holds the old experiment; runtime code should stay lean.", "D3D12 Grid line width normalization should match the D3D11 reference: use periodic line domains with `fwidth()` and pixel-width constants, not world-space `ddx/ddy(p.xy)` footprint guesses. D3D12 binds Aetheria's 512x512 R8 blue-noise texture at `t15` with point/wrap sampling at `s1` for stochastic coverage. Animate it by shifting the blue-noise tile per frame with a low-discrepancy pixel offset, not by giving all pixels the same threshold.", "D3D12 solid scene candidate discovery belongs in the view-froxel volume. At 720p the active tube is 160x90x32; primitive ids are indexed by low-res screen cell and depth slice, then scene rays test exact solid intersections inside that slice interval. The CPU-filled view-froxel buffers are bootstrap only; the durable path should become GPU/raster binning so per-frame upload bandwidth does not become the next stupid tax.", "D3D12 pixel shader UAV registers share the output namespace. If a shader writes SV_Target0, bind the first manual UAV at u1, not u0.", diff --git a/state/scratch.md b/state/scratch.md index 0c2a098..657be2f 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -37,6 +37,13 @@ This was later superseded because using Grid as the visible scene packet broke volumetric ordering. The Grid is currently a direct color overlay only; future work should add a separate transparent-overlay temporal lane rather than reusing field identity/travel/control. +- Medium temporal split: D3D12 now emits a separate `sceneMediumPacket` MRT and + ping-pongs `historyMediumPacket` alongside surface color/metadata/control. + The medium packet stores medium identity, density-weighted travel, opacity, + and density even when a solid surface is behind it. Debug field identity can + show medium where opacity exists instead of pretending the planet behind it + is the whole truth. This is the pattern Grid/particles need next: a separate + stochastic-event packet, not reuse of the surface packet. ## Hot Context