Reconstruct grid color in D3D12 resolve
This commit is contained in:
parent
a59a8c261b
commit
b39f62c46b
5 changed files with 131 additions and 5 deletions
|
|
@ -1037,6 +1037,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
context.CommandList.SetGraphicsRootSignature(fullscreenRootSignature);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(0, frameResources.FrameConstantsDescriptor.Gpu);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(1, frameResources.SceneDescriptor.Gpu);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(3, frameResources.GridHeightDescriptor.Gpu);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(4, frameResources.FieldInstanceDescriptor.Gpu);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(6, frameResources.BloomPresentationDescriptor.Gpu);
|
||||
context.CommandList.SetGraphicsRootDescriptorTable(7, frameResources.SceneMetadataDescriptor.Gpu);
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ cbuffer AquariumFrame : register(b0)
|
|||
};
|
||||
|
||||
Texture2D<float4> sourceTexture : register(t0);
|
||||
Texture2D<float4> gridHeightTexture : register(t1);
|
||||
Texture2D<float4> historyTexture : register(t4);
|
||||
Texture2D<float4> currentSceneMetadataTexture : register(t5);
|
||||
Texture2D<float4> historyMetadataTexture : register(t6);
|
||||
|
|
@ -69,6 +70,18 @@ static const float MAX_HISTORY_AGE = 32.0;
|
|||
static const int FIELD_INSTANCE_COUNT = 10;
|
||||
static const int FIELD_FLAG_CLOUD = 2;
|
||||
static const int MEDIUM_RAY_PREVIEW_STEPS = 48;
|
||||
static const float PI = 3.14159265359;
|
||||
static const float GRID_HEIGHT_TEXEL_COUNT = 128.0;
|
||||
static const float TERRAIN_ISOLINE_SPACING = 0.12;
|
||||
static const float GRID_LINE_WORLD_CELL = 2.0;
|
||||
static const float GRID_MAJOR_LINE_WORLD_CELL = GRID_LINE_WORLD_CELL * 5.0;
|
||||
static const float GRID_LINE_PIXEL_WIDTH = 0.46;
|
||||
static const float GRID_MAJOR_LINE_PIXEL_WIDTH = 0.82;
|
||||
static const float GRID_LINE_PIXEL_FADE = 0.95;
|
||||
static const float TERRAIN_ISOLINE_PIXEL_WIDTH = 0.54;
|
||||
static const float TERRAIN_FIELD_LINE_PIXEL_WIDTH = 0.38;
|
||||
static const float3 GRID_COLOR = float3(0.30, 0.90, 0.82);
|
||||
static const float GRID_ALPHA_SCALE = 0.24;
|
||||
|
||||
VertexOut FullscreenTriangleVS(uint vertexId : SV_VertexID)
|
||||
{
|
||||
|
|
@ -223,6 +236,96 @@ float4 loadHistoryControl(float2 uv)
|
|||
return historyControlTexture.Load(int3(pixelFromUv(uv), 0));
|
||||
}
|
||||
|
||||
float2 gridLocal(float2 p)
|
||||
{
|
||||
return (p - gridCenter) / max(gridRadius, 0.001);
|
||||
}
|
||||
|
||||
float2 gridUv(float2 p)
|
||||
{
|
||||
return gridLocal(p) * 0.5 + 0.5;
|
||||
}
|
||||
|
||||
float terrainHeight(float2 p)
|
||||
{
|
||||
return gridHeightTexture.SampleLevel(sourceSampler, saturate(gridUv(p)), 0.0).r;
|
||||
}
|
||||
|
||||
float2 terrainGradient(float2 p)
|
||||
{
|
||||
float2 uv = saturate(gridUv(p));
|
||||
float2 texel = 1.0 / GRID_HEIGHT_TEXEL_COUNT;
|
||||
float texelWorld = max((gridRadius * 2.0) / GRID_HEIGHT_TEXEL_COUNT, 0.001);
|
||||
|
||||
float hLeft = gridHeightTexture.SampleLevel(sourceSampler, uv - float2(texel.x, 0.0), 0.0).r;
|
||||
float hRight = gridHeightTexture.SampleLevel(sourceSampler, uv + float2(texel.x, 0.0), 0.0).r;
|
||||
float hDown = gridHeightTexture.SampleLevel(sourceSampler, uv - float2(0.0, texel.y), 0.0).r;
|
||||
float hUp = gridHeightTexture.SampleLevel(sourceSampler, uv + float2(0.0, texel.y), 0.0).r;
|
||||
|
||||
return float2(hRight - hLeft, hUp - hDown) / (texelWorld * 2.0);
|
||||
}
|
||||
|
||||
float periodicLineMask(float coordinate, float pixelWidth, float pixelFade)
|
||||
{
|
||||
float distanceToLine = min(frac(coordinate), 1.0 - frac(coordinate));
|
||||
float coordinatePerPixel = max(fwidth(coordinate), 0.00001);
|
||||
float distancePixels = distanceToLine / coordinatePerPixel;
|
||||
return 1.0 - smoothstep(pixelWidth, pixelWidth + pixelFade, distancePixels);
|
||||
}
|
||||
|
||||
float gridLine(float2 p)
|
||||
{
|
||||
float2 minorDomain = p / GRID_LINE_WORLD_CELL;
|
||||
float2 majorDomain = p / GRID_MAJOR_LINE_WORLD_CELL;
|
||||
|
||||
float minor = max(
|
||||
periodicLineMask(minorDomain.x, GRID_LINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE),
|
||||
periodicLineMask(minorDomain.y, GRID_LINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE));
|
||||
float major = max(
|
||||
periodicLineMask(majorDomain.x, GRID_MAJOR_LINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE),
|
||||
periodicLineMask(majorDomain.y, GRID_MAJOR_LINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE));
|
||||
|
||||
return saturate(minor * 0.58 + major);
|
||||
}
|
||||
|
||||
float isolineMask(float height)
|
||||
{
|
||||
float contourDomain = height / TERRAIN_ISOLINE_SPACING;
|
||||
float contour = periodicLineMask(contourDomain, TERRAIN_ISOLINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE);
|
||||
float contourDerivative = max(fwidth(contourDomain), 0.00001);
|
||||
float slopeFade = smoothstep(0.025, 0.25, contourDerivative);
|
||||
return contour * slopeFade;
|
||||
}
|
||||
|
||||
float fieldLineMask(float2 gradient)
|
||||
{
|
||||
float slope = length(gradient);
|
||||
if (slope < 0.0001)
|
||||
{
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
float angleDomain = (atan2(gradient.y, gradient.x) / PI + 1.0) * 6.0;
|
||||
float angleLine = periodicLineMask(angleDomain, TERRAIN_FIELD_LINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE);
|
||||
float slopeStrength = smoothstep(0.015, 0.16, slope);
|
||||
return angleLine * slopeStrength;
|
||||
}
|
||||
|
||||
float3 gridEventColor(float3 p, out float alpha)
|
||||
{
|
||||
float height = terrainHeight(p.xy);
|
||||
float2 gradient = terrainGradient(p.xy);
|
||||
float gridAmount = gridLine(p.xy);
|
||||
float contour = isolineMask(height);
|
||||
float fieldLine = fieldLineMask(gradient);
|
||||
float support = saturate(gridAmount * 0.58 + contour * 0.22 + fieldLine * 0.16);
|
||||
float3 color = GRID_COLOR * gridAmount * 0.90;
|
||||
color += float3(0.98, 1.0, 0.78) * contour * 0.16;
|
||||
color += float3(0.36, 0.92, 1.0) * fieldLine * 0.12;
|
||||
alpha = saturate(support * GRID_ALPHA_SCALE);
|
||||
return color;
|
||||
}
|
||||
|
||||
void currentNeighborhood(float2 uv, out float3 neighborhoodMin, out float3 neighborhoodMax)
|
||||
{
|
||||
float2 texel = 1.0 / resolution;
|
||||
|
|
@ -454,6 +557,7 @@ ResolveOut D3D12ResolvePS(VertexOut input)
|
|||
float2 pixel = screenUv * resolution;
|
||||
float4 current = sourceTexture.SampleLevel(sourceSampler, input.uv, 0.0);
|
||||
float currentTravel = current.a;
|
||||
float3 rawCurrentColor = current.rgb;
|
||||
float3 currentColor = current.rgb;
|
||||
float4 currentMetadata = loadCurrentMetadata(input.uv);
|
||||
float4 currentControl = loadCurrentControl(input.uv);
|
||||
|
|
@ -466,6 +570,15 @@ ResolveOut D3D12ResolvePS(VertexOut input)
|
|||
bool currentIsMedium = abs(currentFieldId - FIELD_ID_MEDIUM) < 0.25;
|
||||
bool currentIsDistributed = currentIsMedium || currentIsGrid;
|
||||
|
||||
if (currentIsGrid && currentTravel <= farDistance)
|
||||
{
|
||||
float3 currentRay = rayDirectionForPixel(pixel, jitterPixels, cameraPosition, gridCenter);
|
||||
float3 worldPosition = cameraPosition + currentRay * currentTravel;
|
||||
float gridAlpha;
|
||||
float3 gridColor = gridEventColor(worldPosition, gridAlpha);
|
||||
currentColor = gridColor * saturate(gridAlpha) + float3(0.001, 0.003, 0.004);
|
||||
}
|
||||
|
||||
float historyWeight = 0.0;
|
||||
float historyAge = 0.0;
|
||||
float3 historyColor = currentColor;
|
||||
|
|
@ -480,7 +593,7 @@ ResolveOut D3D12ResolvePS(VertexOut input)
|
|||
{
|
||||
float4 previousMetadata = loadHistoryMetadata(previousUv);
|
||||
float4 previousControl = loadHistoryControl(previousUv);
|
||||
float4 previous = historyTexture.SampleLevel(sourceSampler, previousUv, 0.0);
|
||||
float4 previous = currentIsGrid ? loadHistoryColor(previousUv) : historyTexture.SampleLevel(sourceSampler, previousUv, 0.0);
|
||||
float previousFieldId = previousMetadata.x;
|
||||
float previousTravel = previous.a;
|
||||
float3 previousNormal = previousMetadata.yzw;
|
||||
|
|
@ -507,13 +620,16 @@ ResolveOut D3D12ResolvePS(VertexOut input)
|
|||
currentNeighborhood(input.uv, neighborhoodMin, neighborhoodMax);
|
||||
float3 clampedHistory = currentIsDistributed ? previous.rgb : clamp(previous.rgb, neighborhoodMin, neighborhoodMax);
|
||||
float colorDelta = length(clampedHistory - currentColor);
|
||||
float colorWeight = currentIsDistributed ? 1.0 - smoothstep(0.35, 1.6, colorDelta) : 1.0 - smoothstep(0.18, 1.2, colorDelta);
|
||||
float colorWeight = currentIsGrid ? 1.0 : currentIsDistributed ? 1.0 - smoothstep(0.35, 1.6, colorDelta) : 1.0 - smoothstep(0.18, 1.2, colorDelta);
|
||||
float reactiveWeight = 1.0 - currentReactive;
|
||||
float coverageWeight = smoothstep(0.02, 0.55, currentCoverage);
|
||||
float coverageWeight = currentIsGrid ? 1.0 : smoothstep(0.02, 0.55, currentCoverage);
|
||||
float coverageContinuityWeight = 1.0 - smoothstep(0.10, 0.50, abs(previousCoverage - currentCoverage));
|
||||
float mediumContinuityWeight = 1.0 - smoothstep(0.04, 0.35, abs(previousMediumOpacity - currentMediumOpacity));
|
||||
float historyConfidence = smoothstep(0.0, 6.0, previousHistoryAge);
|
||||
float validationWeight = travelWeight * colorWeight * fieldWeight * normalWeight * reactiveWeight * coverageWeight * coverageContinuityWeight * mediumContinuityWeight;
|
||||
float surfaceValidationWeight = travelWeight * colorWeight * fieldWeight * normalWeight * reactiveWeight * coverageWeight * coverageContinuityWeight * mediumContinuityWeight;
|
||||
float gridSupportWeight = smoothstep(0.015, 0.18, currentCoverage);
|
||||
float gridValidationWeight = travelWeight * fieldWeight * normalWeight * coverageContinuityWeight * mediumContinuityWeight * gridSupportWeight;
|
||||
float validationWeight = currentIsGrid ? gridValidationWeight : surfaceValidationWeight;
|
||||
|
||||
historyColor = clampedHistory;
|
||||
float maxHistoryWeight = currentIsGrid ? 0.90 : currentIsMedium ? 0.88 : 0.82;
|
||||
|
|
@ -527,7 +643,7 @@ ResolveOut D3D12ResolvePS(VertexOut input)
|
|||
float3 finalColor = presentColor(resolved, input.uv);
|
||||
if (renderDebugMode > 0.5 && renderDebugMode < 1.5)
|
||||
{
|
||||
finalColor = aces(currentColor * max(exposure, 0.001));
|
||||
finalColor = aces(rawCurrentColor * max(exposure, 0.001));
|
||||
}
|
||||
else if (renderDebugMode >= 1.5 && renderDebugMode < 2.5)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -103,3 +103,4 @@
|
|||
{"timestamp":"2026-05-09T22:30:46.5567253+01:00","topic":"d3d12-grid-width-stochastic-reference","lesson":"The D3D11 reference Grid used periodic line domains with fwidth() and pixel-width constants for stable line width. The D3D12 world-space ddx/ddy footprint made width angle-dependent. D3D12 now uses the D3D11 line/contour/field-line width math and a screen-pixel-stable hash dither; the remaining quality gap is binding Aetheria blue-noise texture for proper stochastic transparency.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"]}
|
||||
{"timestamp":"2026-05-09T22:43:12.0000000+01:00","topic":"d3d12-grid-blue-noise-dither","lesson":"D3D12 Grid stochastic coverage now uses Aetheria's copied 512x512 R8 blue-noise texture instead of a hash. The texture is uploaded once to a default-heap R8_UNorm resource, exposed through a transient SRV at t15, and sampled point/wrap at s1 in screen space without a frame-wide animated seed so per-pixel coverage stays stable for TAA.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"]}
|
||||
{"timestamp":"2026-05-09T22:55:27.0000000+01:00","topic":"d3d12-grid-temporal-blue-noise","lesson":"Stable blue-noise dithering is too static for TAA to converge stochastic Grid coverage. The failure mode to avoid is global threshold flicker where all pixels toggle together. Use a per-frame low-discrepancy pixel offset into the blue-noise tile so each pixel changes threshold over time while the spatial mask remains decorrelated.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"]}
|
||||
{"timestamp":"2026-05-09T22:59:44.0000000+01:00","topic":"d3d12-grid-taa-color-contract","lesson":"Stable temporal control, history age/weight, and field identity do not prove Grid TAA is smoothing color. D3D12 was still feeding stochastic hit/miss radiance into resolve/history. The fix is the D3D11 contract: post resolve reconstructs analytic Grid color from current Grid travel/world position using the Grid height target, nearest-loads Grid history, and skips color-delta rejection for Grid pixels while raw debug keeps showing the stochastic stream.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","state/memory.json","state/scratch.md"]}
|
||||
|
|
|
|||
|
|
@ -71,6 +71,7 @@
|
|||
"TAA identity/control buffers must be point-loaded, not linearly sampled. Field ids are identities, not colors; interpolating them at silhouettes lets foreground solids contaminate background Grid history. Grid history color should use identity-aligned nearest sampling to avoid bright foreground bleed.",
|
||||
"Grid temporal coverage must mean current line/contour/field-line support, not broad overlay alpha. Broad low-alpha tint can render, but it must not keep history age alive across the Grid surface or stale stochastic hits billow into smoke.",
|
||||
"Grid TAA must not use the raw stochastic hit/miss scene color as the current radiance truth. The resolve should reconstruct analytic Grid overlay color at the current world hit and use premultiplied color as the current estimate, then use history for stability rather than reconstructing alpha from binary noise alone.",
|
||||
"D3D12 must follow the same Grid TAA color contract as the D3D11 reference: bind the Grid height target into the post resolve, reconstruct analytic Grid color from current travel/world position, nearest-load Grid history, and validate Grid history without color-delta rejection. Temporal control/identity can look perfect while final/history remain noisy if the color path still consumes stochastic hit/miss radiance.",
|
||||
"For the current Grid renderer, final Grid color should not blend previous color history. Use analytic current Grid color for the final pixel and write it into history; keep history age/weight/debug as diagnostics until a real transparent/volumetric composition contract needs color history again.",
|
||||
"With projection jitter disabled, final presentation should not blend temporal color history for opaque surfaces either. Keep the history/control buffers as diagnostics and future volumetric scaffolding, but mode 0 should present current-frame color until a real temporal contract earns final authority.",
|
||||
"Grid temporal control coverage is the line/support signal. Do not mark the whole low-support Grid surface reactive; that turns debug control mode red everywhere and reintroduces stale history-murder logic.",
|
||||
|
|
|
|||
|
|
@ -25,6 +25,13 @@ wrap sampled. It shifts the blue-noise tile by a low-discrepancy pixel offset
|
|||
each frame so TAA sees temporal coverage variation without all pixels toggling
|
||||
together.
|
||||
|
||||
Follow-up TAA diagnosis: D3D12 had stable Grid metadata/control but noisy final,
|
||||
raw, and history because the post resolve used stochastic hit/miss scene color
|
||||
as current radiance. It now matches the D3D11 reference more closely: the post
|
||||
pass binds the Grid height target, reconstructs analytic Grid color from current
|
||||
Grid travel/world position, nearest-loads Grid history, and skips color-delta
|
||||
history rejection for Grid pixels. Raw debug remains the stochastic stream.
|
||||
|
||||
## Hot Context
|
||||
|
||||
- Root: `E:\Projects\Aquarium-Engine`
|
||||
|
|
|
|||
Loading…
Reference in a new issue