diff --git a/docs/hlsl-renderer.md b/docs/hlsl-renderer.md index 212750c..22b1baa 100644 --- a/docs/hlsl-renderer.md +++ b/docs/hlsl-renderer.md @@ -158,10 +158,15 @@ ray and current travel, projects that point into the previous camera basis, samples history, clamps it to the current 3x3 neighborhood, rejects by travel delta, and only then tonemaps to the backbuffer. -This is Gate 1 of the TAA plan: camera reprojection, jitter, travel rejection, -neighborhood clamping, and history ping-pong. It deliberately does not pretend to -solve animated SDF velocity, material/field id rejection, volumetric history, or -history resurrection yet. +This started as Gate 1 of the TAA plan: camera reprojection, jitter, travel +rejection, neighborhood clamping, and history ping-pong. Gate 2A now adds a +scene metadata target and matching history metadata, storing material id and +surface normal so history is rejected across material/normal discontinuities. +The Grid also records its own travel and normal when it is the nearest diegetic +surface, even if the current stochastic coverage sample does not draw color. + +It deliberately does not pretend to solve explicit animated SDF velocity, +reactive masks, volumetric history, or history resurrection yet. ## Overlay Text diff --git a/docs/tsr-inspired-taa-spec.md b/docs/tsr-inspired-taa-spec.md index a174426..108e00b 100644 --- a/docs/tsr-inspired-taa-spec.md +++ b/docs/tsr-inspired-taa-spec.md @@ -112,6 +112,23 @@ more history. - explicit velocity for animated SDF bodies - coverage/reactive masks for stochastic Grid and future particles +Current Gate 2A implementation: + +- scene metadata target stores material id plus surface normal +- history metadata ping-pongs alongside color/travel history +- material id mismatch rejects history +- normal mismatch weakens/rejects history +- Grid stochastic surfaces write Grid travel/normal even when the current + dither sample does not draw color, so the temporal resolver accumulates the + diegetic surface rather than reprojecting from the solid behind it + +Still missing: + +- explicit animated-body velocity +- separate field ids once the SDF field registry exists +- reactive/coverage masks +- richer disocclusion classification + ### Gate 3: Volumetric Temporal Contract - local volume history separate from opaque/surface history @@ -123,4 +140,3 @@ more history. - flicker analysis for high-frequency linework and stochastic coverage - history resurrection for reappearing stable details - debug visualizations for rejection, clamp, velocity, travel, and history age - diff --git a/src/Aquarium.Engine/Render/D3D11Renderer.cs b/src/Aquarium.Engine/Render/D3D11Renderer.cs index 43b8f67..2124698 100644 --- a/src/Aquarium.Engine/Render/D3D11Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D11Renderer.cs @@ -41,12 +41,21 @@ public sealed class D3D11Renderer : IDisposable private readonly ID3D11Texture2D sceneTexture; private readonly ID3D11RenderTargetView sceneRenderTargetView; private readonly ID3D11ShaderResourceView sceneShaderResourceView; + private readonly ID3D11Texture2D sceneMetadataTexture; + private readonly ID3D11RenderTargetView sceneMetadataRenderTargetView; + private readonly ID3D11ShaderResourceView sceneMetadataShaderResourceView; private readonly ID3D11Texture2D historyTextureA; private readonly ID3D11RenderTargetView historyRenderTargetViewA; private readonly ID3D11ShaderResourceView historyShaderResourceViewA; + private readonly ID3D11Texture2D historyMetadataTextureA; + private readonly ID3D11RenderTargetView historyMetadataRenderTargetViewA; + private readonly ID3D11ShaderResourceView historyMetadataShaderResourceViewA; private readonly ID3D11Texture2D historyTextureB; private readonly ID3D11RenderTargetView historyRenderTargetViewB; private readonly ID3D11ShaderResourceView historyShaderResourceViewB; + private readonly ID3D11Texture2D historyMetadataTextureB; + private readonly ID3D11RenderTargetView historyMetadataRenderTargetViewB; + private readonly ID3D11ShaderResourceView historyMetadataShaderResourceViewB; private readonly ID3D11SamplerState gridSampler; private readonly ID3D11SamplerState ditherSampler; private readonly ID3D11Buffer frameConstantBuffer; @@ -127,12 +136,21 @@ public sealed class D3D11Renderer : IDisposable sceneTexture = CreateHdrTexture(width, height); sceneRenderTargetView = device.CreateRenderTargetView(sceneTexture); sceneShaderResourceView = device.CreateShaderResourceView(sceneTexture); + sceneMetadataTexture = CreateHdrTexture(width, height); + sceneMetadataRenderTargetView = device.CreateRenderTargetView(sceneMetadataTexture); + sceneMetadataShaderResourceView = device.CreateShaderResourceView(sceneMetadataTexture); historyTextureA = CreateHdrTexture(width, height); historyRenderTargetViewA = device.CreateRenderTargetView(historyTextureA); historyShaderResourceViewA = device.CreateShaderResourceView(historyTextureA); + historyMetadataTextureA = CreateHdrTexture(width, height); + historyMetadataRenderTargetViewA = device.CreateRenderTargetView(historyMetadataTextureA); + historyMetadataShaderResourceViewA = device.CreateShaderResourceView(historyMetadataTextureA); historyTextureB = CreateHdrTexture(width, height); historyRenderTargetViewB = device.CreateRenderTargetView(historyTextureB); historyShaderResourceViewB = device.CreateShaderResourceView(historyTextureB); + historyMetadataTextureB = CreateHdrTexture(width, height); + historyMetadataRenderTargetViewB = device.CreateRenderTargetView(historyMetadataTextureB); + historyMetadataShaderResourceViewB = device.CreateShaderResourceView(historyMetadataTextureB); startupProgress?.Invoke("Loading Aetheria blue-noise dither texture"); ditherTexture = CreateDitherTexture(Path.Combine(AppContext.BaseDirectory, DitherTextureRelativePath)); ditherShaderResourceView = device.CreateShaderResourceView(ditherTexture); @@ -241,12 +259,21 @@ public sealed class D3D11Renderer : IDisposable historyShaderResourceViewB.Dispose(); historyRenderTargetViewB.Dispose(); historyTextureB.Dispose(); + historyMetadataShaderResourceViewB.Dispose(); + historyMetadataRenderTargetViewB.Dispose(); + historyMetadataTextureB.Dispose(); historyShaderResourceViewA.Dispose(); historyRenderTargetViewA.Dispose(); historyTextureA.Dispose(); + historyMetadataShaderResourceViewA.Dispose(); + historyMetadataRenderTargetViewA.Dispose(); + historyMetadataTextureA.Dispose(); sceneShaderResourceView.Dispose(); sceneRenderTargetView.Dispose(); sceneTexture.Dispose(); + sceneMetadataShaderResourceView.Dispose(); + sceneMetadataRenderTargetView.Dispose(); + sceneMetadataTexture.Dispose(); gridHeightShaderResourceView.Dispose(); gridHeightRenderTargetView.Dispose(); gridHeightTexture.Dispose(); @@ -324,6 +351,10 @@ public sealed class D3D11Renderer : IDisposable context.PSUnsetShaderResource(0); context.PSUnsetShaderResource(1); context.PSUnsetShaderResource(2); + context.PSUnsetShaderResource(3); + context.PSUnsetShaderResource(4); + context.PSUnsetShaderResource(5); + context.PSUnsetShaderResource(6); context.OMSetRenderTargets(gridHeightRenderTargetView); context.RSSetViewport(0.0f, 0.0f, GridHeightTextureSize, GridHeightTextureSize, 0.0f, 1.0f); context.ClearRenderTargetView(gridHeightRenderTargetView, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); @@ -339,9 +370,12 @@ public sealed class D3D11Renderer : IDisposable { context.PSUnsetShaderResource(3); context.PSUnsetShaderResource(4); - context.OMSetRenderTargets(sceneRenderTargetView); + context.PSUnsetShaderResource(5); + context.PSUnsetShaderResource(6); + context.OMSetRenderTargets(new[] { sceneRenderTargetView, sceneMetadataRenderTargetView }); context.RSSetViewport(0.0f, 0.0f, width, height, 0.0f, 1.0f); context.ClearRenderTargetView(sceneRenderTargetView, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); + context.ClearRenderTargetView(sceneMetadataRenderTargetView, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); context.IASetInputLayout(null); context.IASetPrimitiveTopology(PrimitiveTopology.TriangleList); context.VSSetShader(vertexShader); @@ -359,10 +393,14 @@ public sealed class D3D11Renderer : IDisposable { var historyReadView = frameIndex % 2 == 0 ? historyShaderResourceViewA : historyShaderResourceViewB; var historyWriteView = frameIndex % 2 == 0 ? historyRenderTargetViewB : historyRenderTargetViewA; + var historyMetadataReadView = frameIndex % 2 == 0 ? historyMetadataShaderResourceViewA : historyMetadataShaderResourceViewB; + var historyMetadataWriteView = frameIndex % 2 == 0 ? historyMetadataRenderTargetViewB : historyMetadataRenderTargetViewA; context.PSUnsetShaderResource(3); context.PSUnsetShaderResource(4); - context.OMSetRenderTargets(new[] { renderTargetView, historyWriteView }); + context.PSUnsetShaderResource(5); + context.PSUnsetShaderResource(6); + context.OMSetRenderTargets(new[] { renderTargetView, historyWriteView, historyMetadataWriteView }); context.RSSetViewport(0.0f, 0.0f, width, height, 0.0f, 1.0f); context.ClearRenderTargetView(renderTargetView, new Color4(0.0f, 0.0f, 0.0f, 1.0f)); context.IASetInputLayout(null); @@ -372,6 +410,8 @@ public sealed class D3D11Renderer : IDisposable context.PSSetConstantBuffer(0, frameConstantBuffer); context.PSSetShaderResource(3, sceneShaderResourceView); context.PSSetShaderResource(4, historyReadView); + context.PSSetShaderResource(5, sceneMetadataShaderResourceView); + context.PSSetShaderResource(6, historyMetadataReadView); context.PSSetSampler(0, gridSampler); context.Draw(3, 0); } diff --git a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl index 5ccf2cb..f6c8229 100644 --- a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl @@ -21,6 +21,8 @@ StructuredBuffer froxelPrimitiveIds : register(t1); Texture2D ditherTexture : register(t2); Texture2D currentSceneTexture : register(t3); Texture2D historyTexture : register(t4); +Texture2D currentSceneMetadataTexture : register(t5); +Texture2D historyMetadataTexture : register(t6); SamplerState gridSampler : register(s0); SamplerState ditherSampler : register(s1); @@ -1213,7 +1215,13 @@ float3 aces(float3 color) return saturate((color * (a * color + b)) / (color * (c * color + d) + e)); } -float4 AquariumScenePS(VertexOut input) : SV_Target +struct SceneOut +{ + float4 colorTravel : SV_Target0; + float4 metadata : SV_Target1; +}; + +SceneOut AquariumScenePS(VertexOut input) { float2 screenUv = float2(input.uv.x, 1.0 - input.uv.y); float2 pixel = screenUv * resolution; @@ -1224,6 +1232,9 @@ float4 AquariumScenePS(VertexOut input) : SV_Target float travel; float3 color = 0.0; float nearestSolidTravel = farDistance + 1.0; + float outputTravel = farDistance + 1.0; + float outputMaterialId = 0.0; + float3 outputNormal = 0.0; if (raymarchBodies(cameraPosition, rayDirection, hitPosition, materialId, travel)) { @@ -1231,6 +1242,9 @@ float4 AquariumScenePS(VertexOut input) : SV_Target color = shade(hitPosition, normal, materialId, rayDirection); color *= exp(-travel * 0.012); nearestSolidTravel = travel; + outputTravel = travel; + outputMaterialId = materialId; + outputNormal = normal; } float3 gridHitPosition; @@ -1238,6 +1252,9 @@ float4 AquariumScenePS(VertexOut input) : SV_Target if (traceGridSurface(cameraPosition, rayDirection, gridHitPosition, gridTravel) && gridTravel < nearestSolidTravel) { float4 gridOverlay = shadeGridOverlay(gridHitPosition); + outputTravel = gridTravel; + outputMaterialId = 1.0; + outputNormal = terrainNormal(gridHitPosition); if (stochasticTransparency(screenUv, gridOverlay.a) > 0.0) { color = gridOverlay.rgb; @@ -1245,7 +1262,11 @@ float4 AquariumScenePS(VertexOut input) : SV_Target } color += float3(0.001, 0.003, 0.004); - return float4(color, min(nearestSolidTravel, farDistance + 1.0)); + + SceneOut output; + output.colorTravel = float4(color, min(outputTravel, farDistance + 1.0)); + output.metadata = float4(outputMaterialId, outputNormal); + return output; } float3 sampleCurrentScene(float2 uv) @@ -1276,6 +1297,7 @@ struct ResolveOut { float4 finalColor : SV_Target0; float4 historyColor : SV_Target1; + float4 historyMetadata : SV_Target2; }; ResolveOut AquariumResolvePS(VertexOut input) @@ -1285,10 +1307,13 @@ ResolveOut AquariumResolvePS(VertexOut input) float4 current = currentSceneTexture.SampleLevel(gridSampler, input.uv, 0.0); float currentTravel = current.a; float3 currentColor = current.rgb; + float4 currentMetadata = currentSceneMetadataTexture.SampleLevel(gridSampler, input.uv, 0.0); + float currentMaterialId = currentMetadata.x; + float3 currentNormal = currentMetadata.yzw; float historyWeight = 0.0; float3 historyColor = currentColor; - if (frameIndex > 0.5 && currentTravel <= farDistance) + if (frameIndex > 0.5 && currentTravel <= farDistance && currentMaterialId > 0.5) { float3 currentRay = rayDirectionForPixel(pixel, jitterPixels, cameraPosition, gridCenter); float3 worldPosition = cameraPosition + currentRay * currentTravel; @@ -1297,10 +1322,19 @@ ResolveOut AquariumResolvePS(VertexOut input) if (all(previousUv >= 0.0) && all(previousUv <= 1.0)) { float4 previous = historyTexture.SampleLevel(gridSampler, previousUv, 0.0); + float4 previousMetadata = historyMetadataTexture.SampleLevel(gridSampler, previousUv, 0.0); float previousTravel = previous.a; + float previousMaterialId = previousMetadata.x; + float3 previousNormal = previousMetadata.yzw; float travelDelta = abs(previousTravel - currentTravel); float travelTolerance = max(0.045, currentTravel * 0.018); float travelWeight = 1.0 - smoothstep(travelTolerance, travelTolerance * 4.0, travelDelta); + float materialWeight = abs(previousMaterialId - currentMaterialId) < 0.25 ? 1.0 : 0.0; + float normalWeight = 0.0; + if (dot(previousNormal, previousNormal) > 0.01 && dot(currentNormal, currentNormal) > 0.01) + { + normalWeight = smoothstep(0.68, 0.96, dot(normalize(previousNormal), normalize(currentNormal))); + } float3 neighborhoodMin; float3 neighborhoodMax; @@ -1310,7 +1344,7 @@ ResolveOut AquariumResolvePS(VertexOut input) float colorWeight = 1.0 - smoothstep(0.18, 1.2, colorDelta); historyColor = clampedHistory; - historyWeight = 0.82 * travelWeight * colorWeight; + historyWeight = 0.82 * travelWeight * colorWeight * materialWeight * normalWeight; } } @@ -1318,6 +1352,7 @@ ResolveOut AquariumResolvePS(VertexOut input) ResolveOut output; output.finalColor = float4(aces(resolved), 1.0); output.historyColor = float4(resolved, currentTravel); + output.historyMetadata = currentMetadata; return output; } diff --git a/state/evidence.jsonl b/state/evidence.jsonl index a29c48c..e094a90 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -17,3 +17,4 @@ {"area":"renderer/composition","lesson":"Diegetic Grid transparency over raymarched content should use Aetheria-style stochastic coverage, not alpha blending. Aquarium now ports the dither-clip path from Aetheria's Dither Functions and Dithered Particles shaders using the copied LDR_LLL1_0 blue-noise texture, and it only draws the Grid when the Grid hit is closer than the nearest solid body so planets are not overlaid.","files":["src/Aquarium.Engine/Assets/Textures/Aetheria-LDR_LLL1_0.png","src/Aquarium.Engine/Assets/Textures/Aetheria-LDR_LLL1_0.r8","src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","src/Aquarium.Engine/Aquarium.Engine.csproj","docs/hlsl-renderer.md","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"],"ts":"2026-05-08"} {"area":"dev-loop/reload","lesson":"dev-watch must treat runtime content as restart-owned input. Shader hot reload can swap HLSL, and live reload can swap Aquarium.Engine.Live, but new files under src/Aquarium.Engine/Assets require rebuilding the apphost slot and restarting the process so the renderer loads them.","files":["scripts/dev-watch.ps1","README.md","state/memory.json","state/scratch.md"],"verification":[".\\scripts\\dev-watch.ps1 -NoInitialLaunch -Once -IntervalMilliseconds 10 -DebounceMilliseconds 10","asset fingerprint check includes Aetheria-LDR_LLL1_0.png and .r8","dotnet build Aquarium.Engine.slnx"],"ts":"2026-05-08"} {"area":"renderer/taa","lesson":"Added a clean-room TSR-inspired Gate 1 temporal resolver instead of Playdead-style TAA. Aquarium now renders linear HDR scene color plus ray travel, applies Halton jitter, reprojects current world hits into the previous camera basis, clamps history to the current 3x3 neighborhood, rejects by travel delta, writes ping-pong history, and tonemaps after resolve.","files":["docs/tsr-inspired-taa-spec.md","docs/hlsl-renderer.md","src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","state/map.yaml","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"],"ts":"2026-05-08"} +{"area":"renderer/taa","lesson":"TAA Gate 2A adds renderer metadata to history validation. Scene and history now carry material id plus surface normal; resolve rejects material mismatches and normal discontinuities. Grid stochastic surfaces write Grid travel/normal even on dither-miss pixels, preventing temporal accumulation from reprojecting against the body or background behind the diegetic Grid surface.","files":["docs/tsr-inspired-taa-spec.md","docs/hlsl-renderer.md","src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"],"ts":"2026-05-08"} diff --git a/state/memory.json b/state/memory.json index 37cc527..ac4b342 100644 --- a/state/memory.json +++ b/state/memory.json @@ -48,6 +48,7 @@ "Until the real field renderer exists, do not composite the analytic SDF cloud scaffold into the Aquarium scene. The Grid should read as a transparent schematic overlay; volumetrics will own scene mass later.", "For diegetic Grid transparency over raymarched content, use Aetheria-style stochastic coverage rather than alpha blending, and depth-test it against nearer solid bodies so the Grid does not draw over planets. Aquarium has copied Aetheria's LDR_LLL1_0 blue-noise texture and binds it for Grid dither coverage.", "TSR-inspired TAA is specified in docs/tsr-inspired-taa-spec.md. Gate 1 is an owned clean-room temporal resolve: HDR/travel scene target, Halton jitter, camera reprojection, 3x3 neighborhood clamp, travel rejection, ping-pong history, and tonemap after resolve.", + "TAA Gate 2A adds scene/history metadata with material id and surface normal rejection. Grid stochastic surfaces must record Grid travel/normal even on dither-miss pixels so history accumulates the diegetic Grid surface instead of the solid behind it.", "Bruneton-style atmospheric scattering is the later sky/planet solution, not the first Grid fog pass. Use LUTs, explicit texture mappings, and unit-tested physical terms when Aquarium reaches ground-to-space atmosphere.", "GigaVoxels-style ray-guided sparse bricks are only for large sparse field/world storage. Do not turn early fog into a monolithic persistent volume cache." ], diff --git a/state/scratch.md b/state/scratch.md index 3846a85..b2c6d50 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -47,3 +47,6 @@ prototype integrated along the camera ray before surface composition. HDR/travel texture, resolve pass reprojects by current/previous camera basis, clamps history to current 3x3 neighborhood, rejects by travel delta, writes ping-pong history, and tonemaps after resolve. +- Temporal Gate 2A: added scene/history metadata storing material id and normal. + Resolve now rejects material mismatches and normal discontinuities. Grid + stochastic surfaces record Grid travel/normal even when dither misses.