From 6ae671c25f58c08902a95baa2736a3ccbdb3f205 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 8 May 2026 22:59:44 +0100 Subject: [PATCH] Add temporal field ids and body reprojection --- docs/hlsl-renderer.md | 8 ++- docs/tsr-inspired-taa-spec.md | 15 ++++- .../Render/Shaders/Aquarium.hlsl | 61 ++++++++++++++----- state/evidence.jsonl | 1 + state/memory.json | 3 +- state/scratch.md | 3 + 6 files changed, 69 insertions(+), 22 deletions(-) diff --git a/docs/hlsl-renderer.md b/docs/hlsl-renderer.md index 22b1baa..5807cc0 100644 --- a/docs/hlsl-renderer.md +++ b/docs/hlsl-renderer.md @@ -160,12 +160,14 @@ delta, and only then tonemaps to the backbuffer. 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. +scene metadata target and matching history metadata, storing field id and +surface normal so history is rejected across field/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. +Gate 2B adds stable field ids for Self/Grid/each planet and maps orbiting planet +hits back to their previous center before camera reprojection. -It deliberately does not pretend to solve explicit animated SDF velocity, +It deliberately does not pretend to solve general non-rigid field 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 108e00b..863e18d 100644 --- a/docs/tsr-inspired-taa-spec.md +++ b/docs/tsr-inspired-taa-spec.md @@ -114,20 +114,29 @@ more history. Current Gate 2A implementation: -- scene metadata target stores material id plus surface normal +- scene metadata target stores stable field id plus surface normal - history metadata ping-pongs alongside color/travel history -- material id mismatch rejects history +- field 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 +Current Gate 2B implementation: + +- Self, Grid, and each orbiting planet have distinct field ids +- orbiting planet hits map their current world hit back to the planet's previous + center before camera reprojection +- this gives analytic SDF bodies object motion without a full velocity buffer + yet + Still missing: -- explicit animated-body velocity - separate field ids once the SDF field registry exists - reactive/coverage masks - richer disocclusion classification +- a general velocity buffer for non-rigid fields, deformation, and future + volumetrics ### Gate 3: Volumetric Temporal Contract diff --git a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl index f6c8229..a3f8389 100644 --- a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl @@ -45,6 +45,9 @@ static const float CLOUD_MAX_STEP = 0.46; static const float CLOUD_EMPTY_STEP_SCALE = 0.42; static const int PLANET_COUNT = 5; static const int PRIMITIVE_COUNT = PLANET_COUNT + 1; +static const float FIELD_ID_GRID = 1.0; +static const float FIELD_ID_SELF = 2.0; +static const float FIELD_ID_PLANET_BASE = 10.0; static const float GRID_HEIGHT_TEXEL_COUNT = 128.0; static const int FROXEL_COUNT_X = 8; static const int FROXEL_COUNT_Y = 8; @@ -113,6 +116,8 @@ float hash31(float3 p) return frac((p.x + p.y) * p.z) * 2.0 - 1.0; } +float3 planetCenterAt(int index, float sampleTime); + float stochasticTransparency(float2 screenUv, float alpha) { float frameSeed = floor(timeSeconds * 60.0); @@ -154,6 +159,19 @@ float2 projectWorldToPreviousHistoryUv(float3 worldPosition) return float2(previousScreenUv.x, 1.0 - previousScreenUv.y); } +float3 temporalPreviousWorldPosition(float3 currentWorldPosition, float fieldId) +{ + if (fieldId >= FIELD_ID_PLANET_BASE && fieldId < FIELD_ID_PLANET_BASE + PLANET_COUNT) + { + int planetIndex = (int)(fieldId - FIELD_ID_PLANET_BASE + 0.5); + float3 currentCenter = planetCenterAt(planetIndex, timeSeconds); + float3 previousCenter = planetCenterAt(planetIndex, previousTimeSeconds); + return currentWorldPosition - currentCenter + previousCenter; + } + + return currentWorldPosition; +} + float noised3(float3 x) { float3 p = floor(x); @@ -260,15 +278,20 @@ float planetRadius(int index) return lerp(0.34, 0.62, hash21(float2(index, 19.7))); } -float3 planetCenter(int index) +float3 planetCenterAt(int index, float sampleTime) { float f = (float)index; - float angle = f * 0.8975979 + timeSeconds * (0.08 + 0.011 * f); + float angle = f * 0.8975979 + sampleTime * (0.08 + 0.011 * f); float radius = 4.1 + f * 0.77; float2 xy = float2(cos(angle), sin(angle)) * radius; return float3(xy, 1.15 + planetRadius(index) * 0.72); } +float3 planetCenter(int index) +{ + return planetCenterAt(index, timeSeconds); +} + float2 gridUv(float2 p) { return gridLocal(p) * 0.5 + 0.5; @@ -795,12 +818,14 @@ float traceNearestBodyInterval( out float entryTravel, out float hitTravel, out float exitTravel, - out int materialId) + out int materialId, + out int hitPrimitiveId) { entryTravel = 100000.0; hitTravel = 100000.0; exitTravel = currentTravel; materialId = 0; + hitPrimitiveId = -1; float hit = 0.0; float nearestEntry = 100000.0; float nearestExit = 100000.0; @@ -833,6 +858,7 @@ float traceNearestBodyInterval( hitTravel = primitiveHitTravel; exitTravel = primitiveExitTravel; materialId = primitiveMaterialId; + hitPrimitiveId = primitiveId; hit = 1.0; } } @@ -1028,6 +1054,7 @@ bool raymarch(float3 rayOrigin, float3 rayDirection, out float3 hitPosition, out float bodyHitTravel; float bodyExitTravel; int bodyMaterialId; + int bodyPrimitiveId; float bodyHit = traceNearestBodyInterval( rayOrigin, rayDirection, @@ -1035,7 +1062,8 @@ bool raymarch(float3 rayOrigin, float3 rayDirection, out float3 hitPosition, out bodyEntryTravel, bodyHitTravel, bodyExitTravel, - bodyMaterialId); + bodyMaterialId, + bodyPrimitiveId); if (bodyHit > 0.5) { @@ -1083,7 +1111,7 @@ bool raymarch(float3 rayOrigin, float3 rayDirection, out float3 hitPosition, out return false; } -bool raymarchBodies(float3 rayOrigin, float3 rayDirection, out float3 hitPosition, out int materialId, out float travel) +bool raymarchBodies(float3 rayOrigin, float3 rayDirection, out float3 hitPosition, out int materialId, out int primitiveId, out float travel) { float entryTravel; float exitTravel; @@ -1094,7 +1122,8 @@ bool raymarchBodies(float3 rayOrigin, float3 rayDirection, out float3 hitPositio entryTravel, travel, exitTravel, - materialId); + materialId, + primitiveId); hitPosition = rayOrigin + rayDirection * travel; return bodyHit > 0.5 && travel <= farDistance; @@ -1229,6 +1258,7 @@ SceneOut AquariumScenePS(VertexOut input) float3 hitPosition; int materialId; + int primitiveId; float travel; float3 color = 0.0; float nearestSolidTravel = farDistance + 1.0; @@ -1236,14 +1266,14 @@ SceneOut AquariumScenePS(VertexOut input) float outputMaterialId = 0.0; float3 outputNormal = 0.0; - if (raymarchBodies(cameraPosition, rayDirection, hitPosition, materialId, travel)) + if (raymarchBodies(cameraPosition, rayDirection, hitPosition, materialId, primitiveId, travel)) { float3 normal = surfaceNormal(hitPosition, materialId); color = shade(hitPosition, normal, materialId, rayDirection); color *= exp(-travel * 0.012); nearestSolidTravel = travel; outputTravel = travel; - outputMaterialId = materialId; + outputMaterialId = primitiveId == 0 ? FIELD_ID_SELF : FIELD_ID_PLANET_BASE + (float)(primitiveId - 1); outputNormal = normal; } @@ -1253,7 +1283,7 @@ SceneOut AquariumScenePS(VertexOut input) { float4 gridOverlay = shadeGridOverlay(gridHitPosition); outputTravel = gridTravel; - outputMaterialId = 1.0; + outputMaterialId = FIELD_ID_GRID; outputNormal = terrainNormal(gridHitPosition); if (stochasticTransparency(screenUv, gridOverlay.a) > 0.0) { @@ -1308,28 +1338,29 @@ ResolveOut AquariumResolvePS(VertexOut input) float currentTravel = current.a; float3 currentColor = current.rgb; float4 currentMetadata = currentSceneMetadataTexture.SampleLevel(gridSampler, input.uv, 0.0); - float currentMaterialId = currentMetadata.x; + float currentFieldId = currentMetadata.x; float3 currentNormal = currentMetadata.yzw; float historyWeight = 0.0; float3 historyColor = currentColor; - if (frameIndex > 0.5 && currentTravel <= farDistance && currentMaterialId > 0.5) + if (frameIndex > 0.5 && currentTravel <= farDistance && currentFieldId > 0.5) { float3 currentRay = rayDirectionForPixel(pixel, jitterPixels, cameraPosition, gridCenter); float3 worldPosition = cameraPosition + currentRay * currentTravel; - float2 previousUv = projectWorldToPreviousHistoryUv(worldPosition); + float3 previousWorldPosition = temporalPreviousWorldPosition(worldPosition, currentFieldId); + float2 previousUv = projectWorldToPreviousHistoryUv(previousWorldPosition); 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; + float previousFieldId = 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 fieldWeight = abs(previousFieldId - currentFieldId) < 0.25 ? 1.0 : 0.0; float normalWeight = 0.0; if (dot(previousNormal, previousNormal) > 0.01 && dot(currentNormal, currentNormal) > 0.01) { @@ -1344,7 +1375,7 @@ ResolveOut AquariumResolvePS(VertexOut input) float colorWeight = 1.0 - smoothstep(0.18, 1.2, colorDelta); historyColor = clampedHistory; - historyWeight = 0.82 * travelWeight * colorWeight * materialWeight * normalWeight; + historyWeight = 0.82 * travelWeight * colorWeight * fieldWeight * normalWeight; } } diff --git a/state/evidence.jsonl b/state/evidence.jsonl index e094a90..bf31539 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -18,3 +18,4 @@ {"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"} +{"area":"renderer/taa","lesson":"TAA Gate 2B replaces coarse material identity with stable field ids and adds analytic object-motion reprojection for orbiting SDF bodies. Planet hits now move from current planet center to previous planet center before history projection, so history follows rigid body motion instead of relying only on camera reprojection.","files":["docs/tsr-inspired-taa-spec.md","docs/hlsl-renderer.md","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 ac4b342..33b1955 100644 --- a/state/memory.json +++ b/state/memory.json @@ -48,7 +48,8 @@ "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.", + "TAA Gate 2A adds scene/history metadata with field identity 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.", + "TAA Gate 2B uses stable field ids and analytic object-motion reprojection for orbiting planets: current planet hit positions are offset from current center to previous center before projecting into history.", "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 b2c6d50..a3a085f 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -50,3 +50,6 @@ prototype integrated along the camera ray before surface composition. - 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. +- Temporal Gate 2B: field id replaces coarse material id for TAA identity. Each + planet has a stable field id and analytic previous-center reprojection so + orbiting body history follows the body instead of camera-only motion.