diff --git a/docs/hlsl-renderer.md b/docs/hlsl-renderer.md index dd08291..212750c 100644 --- a/docs/hlsl-renderer.md +++ b/docs/hlsl-renderer.md @@ -53,7 +53,8 @@ Current frame order: uploads fixed primitive id slots as a structured buffer. 2. `GridHeightPS` renders body/gravity height into a 128x128 Grid-space `R32G32B32A32_Float` render target. -3. `AquariumPS` raymarches solids and terrain against that height texture. +3. `AquariumScenePS` raymarches solids and the Grid against that height texture, + writing linear HDR color plus ray travel. 4. Body SDF checks read only the primitive ids binned into the current froxel. Empty froxels skip body SDF work entirely. 5. Displaced body SDFs report a noisy hit distance separately from a conservative @@ -124,7 +125,7 @@ Current prototype traits: ## Transparent Grid Overlay -The Grid now renders as a transparent schematic overlay. `AquariumPS` traces +The Grid now renders as a transparent schematic overlay. `AquariumScenePS` traces bodies separately from the Grid surface, shades solid bodies first, then intersects the Grid height field. The Grid only draws when its surface is closer than the nearest solid body, so it behaves like diegetic scene UI rather than a @@ -139,6 +140,29 @@ shader resource, following the `_DitheringTex` + `_FrameNumber * This keeps the current scene legible while leaving future volumetrics to provide the real spatial mass. +## Temporal Resolve + +The first clean-room TSR-inspired temporal pass is documented in +`docs/tsr-inspired-taa-spec.md`. + +Aquarium now renders the scene into an HDR/travel target first: + +```text +rgb = linear scene color +a = ray travel in world units +``` + +`AquariumResolvePS` then resolves that target against a ping-pong HDR history +texture. The resolver reconstructs the current world hit from the jittered camera +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. + ## Overlay Text Readable overlay text is handled by `DirectWriteOverlay`, using DirectWrite for diff --git a/docs/tsr-inspired-taa-spec.md b/docs/tsr-inspired-taa-spec.md new file mode 100644 index 0000000..a174426 --- /dev/null +++ b/docs/tsr-inspired-taa-spec.md @@ -0,0 +1,126 @@ +# TSR-Inspired TAA Spec + +Aquarium needs an owned temporal resolver, not a copied implementation. The +target is inspired by public TSR behavior: stable subpixel detail, aggressive +history validation, visible debug terms, and special handling for pixels whose +appearance is not fully described by motion vectors. + +## Inputs + +The resolver owns these signals: + +- current HDR scene color +- current ray travel +- previous HDR history color +- previous ray travel +- current camera/Grid constants +- previous camera/Grid constants +- current and previous projection jitter +- stochastic coverage from diegetic Grid surfaces + +Later gates add: + +- material/field id +- normal +- explicit velocity +- reactive mask +- transparency/composition mask +- volumetric accumulation terms +- resurrection history + +## Current Pass Contract + +The scene pass renders into an HDR texture: + +```text +rgb = linear scene color +a = ray travel in world units +``` + +The pass does not tonemap. Tonemapping belongs after temporal resolve. + +## Reprojection + +For the first implementation, motion is camera-derived: + +1. Reconstruct the current world hit from current camera, current jittered ray, + and current travel. +2. Project that world hit into the previous camera basis. +3. Remove previous jitter to find the previous history UV. +4. Sample previous history if the UV is valid. + +This is sufficient for camera motion and stochastic Grid coverage. It is not the +final answer for animated bodies, field animation, or volumetrics. + +## Rejection + +Reject or weaken history when: + +- previous UV is outside the frame +- travel differs too much +- current or previous travel is invalid +- current neighborhood color bounds do not contain the reprojected history + +The first pass uses a 3x3 neighborhood min/max clamp and travel-difference +weighting. Later passes add field/material ids, normals, disocclusion masks, and +reactive masks. + +## Resolve + +The resolver outputs both: + +- tonemapped final backbuffer color +- linear HDR history for the next frame + +Current blend target: + +```text +resolved = lerp(current, clamped_history, history_weight) +``` + +History weight starts conservative. Stochastic Grid coverage needs accumulation, +but ghosting is worse than shimmer while the renderer lacks full velocity, +material id, and reactive masks. The resolver should earn trust before taking +more history. + +## Non-Negotiables + +- Do not use Playdead-style TAA as the final model; it ghosts too easily for the + scene we are building. +- Do not vendor Unreal TSR code. Study behavior and build a clean-room resolver. +- Do not hide missing renderer signals with larger blend weights. +- Do not treat volumetrics as ordinary opaque surface history. +- Do not make the Grid a final-image HUD layer; it is diegetic scene UI and must + respect nearer solids. + +## Implementation Gates + +### Gate 1: Owned Temporal Pass + +- HDR scene target +- history ping-pong +- camera jitter +- current/previous camera reprojection +- travel rejection +- 3x3 color clamp +- final tonemap after resolve + +### Gate 2: Renderer Signals + +- material/field id buffer +- normal buffer +- explicit velocity for animated SDF bodies +- coverage/reactive masks for stochastic Grid and future particles + +### Gate 3: Volumetric Temporal Contract + +- local volume history separate from opaque/surface history +- accumulated extinction/transmittance history +- reactive reset for lighting and density changes + +### Gate 4: TSR Features Worth Stealing In Spirit + +- 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 d8e01a9..43b8f67 100644 --- a/src/Aquarium.Engine/Render/D3D11Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D11Renderer.cs @@ -38,6 +38,15 @@ public sealed class D3D11Renderer : IDisposable private readonly ID3D11ShaderResourceView gridHeightShaderResourceView; private readonly ID3D11Texture2D ditherTexture; private readonly ID3D11ShaderResourceView ditherShaderResourceView; + private readonly ID3D11Texture2D sceneTexture; + private readonly ID3D11RenderTargetView sceneRenderTargetView; + private readonly ID3D11ShaderResourceView sceneShaderResourceView; + private readonly ID3D11Texture2D historyTextureA; + private readonly ID3D11RenderTargetView historyRenderTargetViewA; + private readonly ID3D11ShaderResourceView historyShaderResourceViewA; + private readonly ID3D11Texture2D historyTextureB; + private readonly ID3D11RenderTargetView historyRenderTargetViewB; + private readonly ID3D11ShaderResourceView historyShaderResourceViewB; private readonly ID3D11SamplerState gridSampler; private readonly ID3D11SamplerState ditherSampler; private readonly ID3D11Buffer frameConstantBuffer; @@ -50,10 +59,16 @@ public sealed class D3D11Renderer : IDisposable private readonly Stopwatch shaderReloadClock = Stopwatch.StartNew(); private ID3D11VertexShader vertexShader; private ID3D11PixelShader gridHeightPixelShader; - private ID3D11PixelShader pixelShader; + private ID3D11PixelShader scenePixelShader; + private ID3D11PixelShader resolvePixelShader; private DateTime lastShaderWriteUtc; private TimeSpan lastShaderReloadCheck; private bool shaderReloadFailureReported; + private int frameIndex; + private Vector3 previousCameraPosition; + private Vector2 previousGridCenter; + private float previousGridRadius; + private float previousTimeSeconds; public D3D11Renderer(IntPtr windowHandle, int width, int height, string? shaderPath = null, Action? startupProgress = null) { @@ -101,12 +116,23 @@ public sealed class D3D11Renderer : IDisposable var initialShaders = CompileShaderSet(this.shaderPath); vertexShader = initialShaders.VertexShader; gridHeightPixelShader = initialShaders.GridHeightPixelShader; - pixelShader = initialShaders.AquariumPixelShader; + scenePixelShader = initialShaders.AquariumScenePixelShader; + resolvePixelShader = initialShaders.AquariumResolvePixelShader; lastShaderWriteUtc = File.GetLastWriteTimeUtc(this.shaderPath); startupProgress?.Invoke("Creating Grid render targets and buffers"); gridHeightTexture = CreateGridHeightTexture(); gridHeightRenderTargetView = device.CreateRenderTargetView(gridHeightTexture); gridHeightShaderResourceView = device.CreateShaderResourceView(gridHeightTexture); + startupProgress?.Invoke("Creating temporal render targets"); + sceneTexture = CreateHdrTexture(width, height); + sceneRenderTargetView = device.CreateRenderTargetView(sceneTexture); + sceneShaderResourceView = device.CreateShaderResourceView(sceneTexture); + historyTextureA = CreateHdrTexture(width, height); + historyRenderTargetViewA = device.CreateRenderTargetView(historyTextureA); + historyShaderResourceViewA = device.CreateShaderResourceView(historyTextureA); + historyTextureB = CreateHdrTexture(width, height); + historyRenderTargetViewB = device.CreateRenderTargetView(historyTextureB); + historyShaderResourceViewB = device.CreateShaderResourceView(historyTextureB); startupProgress?.Invoke("Loading Aetheria blue-noise dither texture"); ditherTexture = CreateDitherTexture(Path.Combine(AppContext.BaseDirectory, DitherTextureRelativePath)); ditherShaderResourceView = device.CreateShaderResourceView(ditherTexture); @@ -131,7 +157,7 @@ public sealed class D3D11Renderer : IDisposable MaxLOD = float.MaxValue, }); frameConstantBuffer = device.CreateBuffer( - 48, + 96, BindFlags.ConstantBuffer, ResourceUsage.Default, CpuAccessFlags.None, @@ -148,6 +174,9 @@ public sealed class D3D11Renderer : IDisposable froxelPrimitiveShaderResourceView = device.CreateShaderResourceView( froxelPrimitiveBuffer, new ShaderResourceViewDescription(froxelPrimitiveBuffer, Format.Unknown, 0, FroxelBufferElementCount)); + previousCameraPosition = Vector3.Zero; + previousGridCenter = Vector2.Zero; + previousGridRadius = 0.001f; } public void Render(AquariumFrame frame) @@ -156,24 +185,47 @@ public sealed class D3D11Renderer : IDisposable var gridOrigin = new Vector3(frame.Grid.Center.X, frame.Grid.Center.Y, 0.0f); var farDistance = Vector3.Distance(frame.CameraPosition, gridOrigin) + MathF.Max(frame.Grid.Radius, 0.001f); + var jitter = frameIndex == 0 ? Vector2.Zero : HaltonJitter(frameIndex); + var previousJitter = frameIndex <= 1 ? Vector2.Zero : HaltonJitter(frameIndex - 1); + if (frameIndex == 0) + { + previousCameraPosition = frame.CameraPosition; + previousGridCenter = frame.Grid.Center; + previousGridRadius = frame.Grid.Radius; + previousTimeSeconds = frame.TimeSeconds; + } + var constants = new FrameConstants( new float2(width, height), frame.TimeSeconds, frame.Grid.Radius, (float3)frame.CameraPosition, - 0.0f, - (float2)frame.Grid.Center, farDistance, - 0.0f); + (float2)frame.Grid.Center, + frameIndex, + previousTimeSeconds, + (float3)previousCameraPosition, + previousGridRadius, + (float2)previousGridCenter, + (float2)jitter, + (float2)previousJitter, + new float2(0.0f, 0.0f)); BuildFroxelPrimitiveTable(frame); context.UpdateSubresource(in constants, frameConstantBuffer); context.UpdateSubresource(froxelPrimitiveIds, froxelPrimitiveBuffer); RenderGridHeight(); - RenderAquarium(); + RenderScene(); + ResolveTemporal(); context.Flush(); overlay.Render(frame); swapChain.Present(1, PresentFlags.None); + + previousCameraPosition = frame.CameraPosition; + previousGridCenter = frame.Grid.Center; + previousGridRadius = frame.Grid.Radius; + previousTimeSeconds = frame.TimeSeconds; + frameIndex++; } public void Dispose() @@ -186,10 +238,20 @@ public sealed class D3D11Renderer : IDisposable gridSampler.Dispose(); ditherShaderResourceView.Dispose(); ditherTexture.Dispose(); + historyShaderResourceViewB.Dispose(); + historyRenderTargetViewB.Dispose(); + historyTextureB.Dispose(); + historyShaderResourceViewA.Dispose(); + historyRenderTargetViewA.Dispose(); + historyTextureA.Dispose(); + sceneShaderResourceView.Dispose(); + sceneRenderTargetView.Dispose(); + sceneTexture.Dispose(); gridHeightShaderResourceView.Dispose(); gridHeightRenderTargetView.Dispose(); gridHeightTexture.Dispose(); - pixelShader.Dispose(); + resolvePixelShader.Dispose(); + scenePixelShader.Dispose(); gridHeightPixelShader.Dispose(); vertexShader.Dispose(); renderTargetView.Dispose(); @@ -217,6 +279,25 @@ public sealed class D3D11Renderer : IDisposable return device.CreateTexture2D(description); } + private ID3D11Texture2D CreateHdrTexture(int textureWidth, int textureHeight) + { + var description = new Texture2DDescription + { + Width = (uint)textureWidth, + Height = (uint)textureHeight, + MipLevels = 1, + ArraySize = 1, + Format = Format.R32G32B32A32_Float, + SampleDescription = new SampleDescription(1, 0), + Usage = ResourceUsage.Default, + BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, + CPUAccessFlags = CpuAccessFlags.None, + MiscFlags = ResourceOptionFlags.None, + }; + + return device.CreateTexture2D(description); + } + private ID3D11Texture2D CreateDitherTexture(string path) { var pixels = File.ReadAllBytes(path); @@ -254,15 +335,17 @@ public sealed class D3D11Renderer : IDisposable context.Draw(3, 0); } - private void RenderAquarium() + private void RenderScene() { - context.OMSetRenderTargets(renderTargetView); + context.PSUnsetShaderResource(3); + context.PSUnsetShaderResource(4); + context.OMSetRenderTargets(sceneRenderTargetView); 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.ClearRenderTargetView(sceneRenderTargetView, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); context.IASetInputLayout(null); context.IASetPrimitiveTopology(PrimitiveTopology.TriangleList); context.VSSetShader(vertexShader); - context.PSSetShader(pixelShader); + context.PSSetShader(scenePixelShader); context.PSSetConstantBuffer(0, frameConstantBuffer); context.PSSetShaderResource(0, gridHeightShaderResourceView); context.PSSetShaderResource(1, froxelPrimitiveShaderResourceView); @@ -272,6 +355,27 @@ public sealed class D3D11Renderer : IDisposable context.Draw(3, 0); } + private void ResolveTemporal() + { + var historyReadView = frameIndex % 2 == 0 ? historyShaderResourceViewA : historyShaderResourceViewB; + var historyWriteView = frameIndex % 2 == 0 ? historyRenderTargetViewB : historyRenderTargetViewA; + + context.PSUnsetShaderResource(3); + context.PSUnsetShaderResource(4); + context.OMSetRenderTargets(new[] { renderTargetView, historyWriteView }); + 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); + context.IASetPrimitiveTopology(PrimitiveTopology.TriangleList); + context.VSSetShader(vertexShader); + context.PSSetShader(resolvePixelShader); + context.PSSetConstantBuffer(0, frameConstantBuffer); + context.PSSetShaderResource(3, sceneShaderResourceView); + context.PSSetShaderResource(4, historyReadView); + context.PSSetSampler(0, gridSampler); + context.Draw(3, 0); + } + private void TryHotReloadShaders() { var now = shaderReloadClock.Elapsed; @@ -308,13 +412,16 @@ public sealed class D3D11Renderer : IDisposable var replacement = CompileShaderSet(shaderPath); var oldVertexShader = vertexShader; var oldGridHeightPixelShader = gridHeightPixelShader; - var oldPixelShader = pixelShader; + var oldScenePixelShader = scenePixelShader; + var oldResolvePixelShader = resolvePixelShader; vertexShader = replacement.VertexShader; gridHeightPixelShader = replacement.GridHeightPixelShader; - pixelShader = replacement.AquariumPixelShader; + scenePixelShader = replacement.AquariumScenePixelShader; + resolvePixelShader = replacement.AquariumResolvePixelShader; - oldPixelShader.Dispose(); + oldResolvePixelShader.Dispose(); + oldScenePixelShader.Dispose(); oldGridHeightPixelShader.Dispose(); oldVertexShader.Dispose(); @@ -466,6 +573,25 @@ public sealed class D3D11Renderer : IDisposable return a + (b - a) * t; } + private static Vector2 HaltonJitter(int index) + { + return new Vector2(Halton(index & 1023, 2) - 0.5f, Halton(index & 1023, 3) - 0.5f); + } + + private static float Halton(int index, int radix) + { + var result = 0.0f; + var fraction = 1.0f / radix; + while (index > 0) + { + result += (index % radix) * fraction; + index /= radix; + fraction /= radix; + } + + return result; + } + private static ReadOnlyMemory CompileShader(string path, string entryPoint, string profile) { var shaderFlags = ShaderFlags.EnableStrictness; @@ -480,12 +606,14 @@ public sealed class D3D11Renderer : IDisposable { var vertexShaderBytecode = CompileShader(path, "FullscreenTriangleVS", "vs_5_0"); var gridHeightShaderBytecode = CompileShader(path, "GridHeightPS", "ps_5_0"); - var pixelShaderBytecode = CompileShader(path, "AquariumPS", "ps_5_0"); + var sceneShaderBytecode = CompileShader(path, "AquariumScenePS", "ps_5_0"); + var resolveShaderBytecode = CompileShader(path, "AquariumResolvePS", "ps_5_0"); return new ShaderSet( device.CreateVertexShader(vertexShaderBytecode.Span, null), device.CreatePixelShader(gridHeightShaderBytecode.Span, null), - device.CreatePixelShader(pixelShaderBytecode.Span, null)); + device.CreatePixelShader(sceneShaderBytecode.Span, null), + device.CreatePixelShader(resolveShaderBytecode.Span, null)); } private readonly record struct FrameConstants( @@ -493,10 +621,16 @@ public sealed class D3D11Renderer : IDisposable float TimeSeconds, float GridRadius, float3 CameraPosition, - float CameraPad, - float2 GridCenter, float FarDistance, - float Pad0); + float2 GridCenter, + float FrameIndex, + float PreviousTimeSeconds, + float3 PreviousCameraPosition, + float PreviousGridRadius, + float2 PreviousGridCenter, + float2 JitterPixels, + float2 PreviousJitterPixels, + float2 Pad0); private readonly record struct FroxelCell(int X, int Y, int Z); @@ -506,5 +640,6 @@ public sealed class D3D11Renderer : IDisposable private readonly record struct ShaderSet( ID3D11VertexShader VertexShader, ID3D11PixelShader GridHeightPixelShader, - ID3D11PixelShader AquariumPixelShader); + ID3D11PixelShader AquariumScenePixelShader, + ID3D11PixelShader AquariumResolvePixelShader); } diff --git a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl index 8701295..5ccf2cb 100644 --- a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl @@ -4,15 +4,23 @@ cbuffer AquariumFrame : register(b0) float timeSeconds; float gridRadius; float3 cameraPosition; - float _pad0; - float2 gridCenter; float farDistance; - float _pad1; + float2 gridCenter; + float frameIndex; + float previousTimeSeconds; + float3 previousCameraPosition; + float previousGridRadius; + float2 previousGridCenter; + float2 jitterPixels; + float2 previousJitterPixels; + float2 _pad0; }; Texture2D gridHeightTexture : register(t0); StructuredBuffer froxelPrimitiveIds : register(t1); Texture2D ditherTexture : register(t2); +Texture2D currentSceneTexture : register(t3); +Texture2D historyTexture : register(t4); SamplerState gridSampler : register(s0); SamplerState ditherSampler : register(s1); @@ -111,6 +119,39 @@ float stochasticTransparency(float2 screenUv, float alpha) return alpha - dither - 0.001 * (1.0 - ceil(alpha)); } +void cameraBasis(float3 camera, float2 center, out float3 forward, out float3 right, out float3 up) +{ + float3 target = float3(center, 0.0); + forward = normalize(target - camera); + right = normalize(cross(forward, float3(0.0, 0.0, 1.0))); + up = cross(right, forward); +} + +float3 rayDirectionForPixel(float2 pixel, float2 jitter, float3 camera, float2 center) +{ + float2 ndc = ((pixel + jitter) * 2.0 - resolution) / resolution.y; + float3 forward; + float3 right; + float3 up; + cameraBasis(camera, center, forward, right, up); + return normalize(forward * 1.6 + right * ndc.x + up * ndc.y); +} + +float2 projectWorldToPreviousHistoryUv(float3 worldPosition) +{ + float3 forward; + float3 right; + float3 up; + cameraBasis(previousCameraPosition, previousGridCenter, forward, right, up); + + float3 toPoint = worldPosition - previousCameraPosition; + float forwardDistance = max(dot(toPoint, forward), 0.0001); + float2 previousNdc = float2(dot(toPoint, right), dot(toPoint, up)) * (1.6 / forwardDistance); + float2 previousPixel = (previousNdc * resolution.y + resolution) * 0.5 - previousJitterPixels; + float2 previousScreenUv = previousPixel / resolution; + return float2(previousScreenUv.x, 1.0 - previousScreenUv.y); +} + float noised3(float3 x) { float3 p = floor(x); @@ -1172,17 +1213,11 @@ float3 aces(float3 color) return saturate((color * (a * color + b)) / (color * (c * color + d) + e)); } -float4 AquariumPS(VertexOut input) : SV_Target +float4 AquariumScenePS(VertexOut input) : SV_Target { float2 screenUv = float2(input.uv.x, 1.0 - input.uv.y); float2 pixel = screenUv * resolution; - float2 ndc = (pixel * 2.0 - resolution) / resolution.y; - - float3 target = float3(gridCenter, 0.0); - float3 forward = normalize(target - cameraPosition); - float3 right = normalize(cross(forward, float3(0.0, 0.0, 1.0))); - float3 up = cross(right, forward); - float3 rayDirection = normalize(forward * 1.6 + right * ndc.x + up * ndc.y); + float3 rayDirection = rayDirectionForPixel(pixel, jitterPixels, cameraPosition, gridCenter); float3 hitPosition; int materialId; @@ -1210,7 +1245,80 @@ float4 AquariumPS(VertexOut input) : SV_Target } color += float3(0.001, 0.003, 0.004); - return float4(aces(color), 1.0); + return float4(color, min(nearestSolidTravel, farDistance + 1.0)); +} + +float3 sampleCurrentScene(float2 uv) +{ + return currentSceneTexture.SampleLevel(gridSampler, uv, 0.0).rgb; +} + +void currentNeighborhood(float2 uv, out float3 neighborhoodMin, out float3 neighborhoodMax) +{ + float2 texel = 1.0 / resolution; + neighborhoodMin = 100000.0; + neighborhoodMax = -100000.0; + + [unroll] + for (int y = -1; y <= 1; y++) + { + [unroll] + for (int x = -1; x <= 1; x++) + { + float3 sampleColor = sampleCurrentScene(uv + float2(x, y) * texel); + neighborhoodMin = min(neighborhoodMin, sampleColor); + neighborhoodMax = max(neighborhoodMax, sampleColor); + } + } +} + +struct ResolveOut +{ + float4 finalColor : SV_Target0; + float4 historyColor : SV_Target1; +}; + +ResolveOut AquariumResolvePS(VertexOut input) +{ + float2 screenUv = float2(input.uv.x, 1.0 - input.uv.y); + float2 pixel = screenUv * resolution; + float4 current = currentSceneTexture.SampleLevel(gridSampler, input.uv, 0.0); + float currentTravel = current.a; + float3 currentColor = current.rgb; + + float historyWeight = 0.0; + float3 historyColor = currentColor; + if (frameIndex > 0.5 && currentTravel <= farDistance) + { + float3 currentRay = rayDirectionForPixel(pixel, jitterPixels, cameraPosition, gridCenter); + float3 worldPosition = cameraPosition + currentRay * currentTravel; + float2 previousUv = projectWorldToPreviousHistoryUv(worldPosition); + + if (all(previousUv >= 0.0) && all(previousUv <= 1.0)) + { + float4 previous = historyTexture.SampleLevel(gridSampler, previousUv, 0.0); + float previousTravel = previous.a; + float travelDelta = abs(previousTravel - currentTravel); + float travelTolerance = max(0.045, currentTravel * 0.018); + float travelWeight = 1.0 - smoothstep(travelTolerance, travelTolerance * 4.0, travelDelta); + + float3 neighborhoodMin; + float3 neighborhoodMax; + currentNeighborhood(input.uv, neighborhoodMin, neighborhoodMax); + float3 clampedHistory = clamp(previous.rgb, neighborhoodMin, neighborhoodMax); + float colorDelta = length(clampedHistory - currentColor); + float colorWeight = 1.0 - smoothstep(0.18, 1.2, colorDelta); + + historyColor = clampedHistory; + historyWeight = 0.82 * travelWeight * colorWeight; + } + } + + float3 resolved = lerp(currentColor, historyColor, historyWeight); + ResolveOut output; + output.finalColor = float4(aces(resolved), 1.0); + output.historyColor = float4(resolved, currentTravel); + return output; } float4 GridHeightPS(VertexOut input) : SV_Target diff --git a/state/evidence.jsonl b/state/evidence.jsonl index fc7d5b0..a29c48c 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -16,3 +16,4 @@ {"area":"renderer/composition","lesson":"Removed the analytic SDF cloud scaffold from visible scene composition and made the Grid a transparent schematic overlay. Bodies now trace as solid scene content; the Grid traces separately and blends lines/isolines/field lines/weather tint over the result. This preserves legibility while reserving real scene mass for the future explicit volumetric field renderer.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","docs/hlsl-renderer.md","docs/sdf-field-renderer-plan.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":"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"} diff --git a/state/map.yaml b/state/map.yaml index 88c80eb..ba47907 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -28,6 +28,7 @@ current_model: runtime_surface: docs/cult-runtime-surface.md native_spine: docs/vortice-spine.md sdf_field_renderer_plan: docs/sdf-field-renderer-plan.md + tsr_inspired_taa_spec: docs/tsr-inspired-taa-spec.md input: docs/input.md volumetrics_research: research/volumetrics/synthesis.md recent_volumetrics_and_gpu_sdf: research/volumetrics/recent-volumetrics-and-gpu-sdf.md @@ -58,6 +59,7 @@ renderer_state: - Restore HDR scene target, gentle pre-tonemap bloom, and ACES as explicit passes. - Add volumetrics as an explicit renderer-owned field pass, starting from Wronski/Hillaire-style frustum volume lighting with Self as the first emitter. - Evolve solids and clouds toward one SDF field registry with material and medium regimes. + - Extend TSR-inspired temporal resolve with material/field ids, normals, explicit animated-body velocity, reactive masks, and volumetric history. - Move Grid gradients into the Grid target for cheaper terrain normals. - Rebuild lighting as an Aquarium-owned field before adding richer fog. - Add diegetic labels and object-local menus after the renderer and text contracts are stable. @@ -70,5 +72,6 @@ next_actions: - Use research/volumetrics/recent-volumetrics-and-gpu-sdf.md before designing GPU SDF, cloud traversal, learned field storage, or radiance caching. - Check research/volumetrics/transcript-addendum.md before relying on slide decks as evidence; video transcripts can change the renderer lesson. - Follow docs/sdf-field-renderer-plan.md when adding SDF solids, SDF clouds, or hybrid field objects. + - Follow docs/tsr-inspired-taa-spec.md when changing jitter, history, stochastic coverage, or temporal rejection. - Promote runtime knobs into typed CultCache documents. - Port only proven synth concepts into C# when audio returns; keep external prototypes as references, not engine baggage. diff --git a/state/memory.json b/state/memory.json index 04448a3..37cc527 100644 --- a/state/memory.json +++ b/state/memory.json @@ -47,6 +47,7 @@ "SDF clouds/solids need waterfall architecture, not agile accretion. Define and build the field contract, broad phase, explicit local volume pass, composition contract, and acceleration gates before adding more cloud features.", "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.", "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 2a04701..3846a85 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -43,3 +43,7 @@ prototype integrated along the camera ray before surface composition. - Dev watcher fix: restart fingerprint now includes `src/Aquarium.Engine/Assets` so runtime content changes trigger apphost rebuild/restart instead of relying on shader or live assembly reload. +- Temporal pass: added clean-room TSR-inspired Gate 1. Scene renders into + 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.