Add D3D12 medium temporal identity

This commit is contained in:
Meta Vens 2026-05-09 17:20:36 +01:00
commit 408eddc024
6 changed files with 57 additions and 10 deletions

View file

@ -48,7 +48,11 @@ has feature parity.
metadata, and control targets, with previous-camera reprojection and
field/travel/normal/color/coverage/medium validation for opaque current scene
hits. Debug mode `3` shows history age and mode `4` shows history weight.
Grid/transparent-specific temporal reconstruction is still pending.
Medium-only pixels use a density-weighted ray centroid as their temporal
anchor and a `FIELD_ID_MEDIUM` identity, with continuity weighted by medium
opacity and coverage rather than surface normals. Grid/transparent
contributions therefore get history through the medium path without becoming
fake alpha surfaces.
- Resize waits for the GPU, releases swapchain-dependent resources, rebuilds
static shader/RTV descriptor arenas, then recreates backbuffer views and
dependent render targets. Descriptor exhaustion on resize is no longer a

View file

@ -98,7 +98,10 @@ now writes ping-pong history color, metadata, and control targets and validates
opaque history with previous-camera reprojection plus field, travel, normal,
neighborhood color, coverage, and medium-continuity checks. Debug modes `3` and
`4` show history age and weight. Grid/transparent-specific reconstruction and
overlay/debug UI still need to move.
overlay/debug UI still need to move. Medium-only pixels already use a
density-weighted ray centroid and `FIELD_ID_MEDIUM`, so transparent Grid and fog
contribution can accumulate through the medium history path without inventing a
single alpha-surface hit.
The shader far distance is frame-derived, not a fixed constant. C# sends the
distance from the camera to the Grid origin plus the Grid radius, which matches

View file

@ -51,6 +51,7 @@ static const float SUN_RADIUS = 1.12;
static const int PLANET_COUNT = 5;
static const float FIELD_ID_GRID = 1.0;
static const float FIELD_ID_SELF = 2.0;
static const float FIELD_ID_MEDIUM = 3.0;
static const float FIELD_ID_PLANET_BASE = 10.0;
static const float MAX_HISTORY_AGE = 32.0;
@ -95,6 +96,11 @@ float3 debugFieldIdColor(float fieldId)
return float3(1.0, 0.92, 0.25);
}
if (abs(fieldId - FIELD_ID_MEDIUM) < 0.25)
{
return float3(0.28, 0.72, 1.0);
}
if (fieldId >= 10.0)
{
float phase = frac((fieldId - 10.0) * 0.37);
@ -309,6 +315,7 @@ ResolveOut D3D12ResolvePS(VertexOut input)
float currentReactive = saturate(currentControl.x);
float currentCoverage = saturate(currentControl.y);
float currentMediumOpacity = saturate(currentControl.z);
bool currentIsMedium = abs(currentFieldId - FIELD_ID_MEDIUM) < 0.25;
float historyWeight = 0.0;
float historyAge = 0.0;
@ -337,7 +344,11 @@ ResolveOut D3D12ResolvePS(VertexOut input)
float travelWeight = 1.0 - smoothstep(travelTolerance, travelTolerance * 4.0, travelDelta);
float fieldWeight = abs(previousFieldId - currentFieldId) < 0.001 ? 1.0 : 0.0;
float normalWeight = 0.0;
if (dot(previousNormal, previousNormal) > 0.01 && dot(currentNormal, currentNormal) > 0.01)
if (currentIsMedium)
{
normalWeight = 1.0;
}
else if (dot(previousNormal, previousNormal) > 0.01 && dot(currentNormal, currentNormal) > 0.01)
{
normalWeight = smoothstep(0.68, 0.96, dot(normalize(previousNormal), normalize(currentNormal)));
}
@ -345,9 +356,9 @@ ResolveOut D3D12ResolvePS(VertexOut input)
float3 neighborhoodMin;
float3 neighborhoodMax;
currentNeighborhood(input.uv, neighborhoodMin, neighborhoodMax);
float3 clampedHistory = clamp(previous.rgb, neighborhoodMin, neighborhoodMax);
float3 clampedHistory = currentIsMedium ? previous.rgb : clamp(previous.rgb, neighborhoodMin, neighborhoodMax);
float colorDelta = length(clampedHistory - currentColor);
float colorWeight = 1.0 - smoothstep(0.18, 1.2, colorDelta);
float colorWeight = currentIsMedium ? 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 coverageContinuityWeight = 1.0 - smoothstep(0.10, 0.50, abs(previousCoverage - currentCoverage));
@ -356,7 +367,9 @@ ResolveOut D3D12ResolvePS(VertexOut input)
float validationWeight = travelWeight * colorWeight * fieldWeight * normalWeight * reactiveWeight * coverageWeight * coverageContinuityWeight * mediumContinuityWeight;
historyColor = clampedHistory;
historyWeight = 0.82 * lerp(0.35, 1.0, historyConfidence) * validationWeight;
float maxHistoryWeight = currentIsMedium ? 0.88 : 0.82;
float freshHistoryScale = currentIsMedium ? 0.48 : 0.35;
historyWeight = maxHistoryWeight * lerp(freshHistoryScale, 1.0, historyConfidence) * validationWeight;
historyAge = validationWeight > 0.01 ? min(previousHistoryAge + 1.0, MAX_HISTORY_AGE) : 0.0;
}
}

View file

@ -31,6 +31,7 @@ static const int PLANET_COUNT = 5;
static const float SUN_RADIUS = 1.12;
static const float FIELD_ID_GRID = 1.0;
static const float FIELD_ID_SELF = 2.0;
static const float FIELD_ID_MEDIUM = 3.0;
static const float FIELD_ID_PLANET_BASE = 10.0;
static const int MEDIUM_FROXEL_ATLAS_COLUMNS = 8;
static const int MEDIUM_FROXEL_ATLAS_ROWS = 4;
@ -176,16 +177,20 @@ float mediumSliceTravel(int sliceIndex)
return t * farDistance;
}
void integrateMedium(float2 uv, float maxTravel, out float densityMean, out float transmittance, out float3 inScattering)
void integrateMedium(float2 uv, float maxTravel, out float densityMean, out float transmittance, out float3 inScattering, out float representativeTravel)
{
densityMean = 0.0;
transmittance = 1.0;
inScattering = 0.0;
representativeTravel = maxTravel;
float densityTravelSum = 0.0;
float densitySum = 0.0;
[loop]
for (int sliceIndex = 0; sliceIndex < MEDIUM_FROXEL_SLICE_COUNT; sliceIndex++)
{
if (mediumSliceTravel(sliceIndex) > maxTravel)
float sliceTravel = mediumSliceTravel(sliceIndex);
if (sliceTravel > maxTravel)
{
break;
}
@ -194,11 +199,17 @@ void integrateMedium(float2 uv, float maxTravel, out float densityMean, out floa
float4 diagnostic = mediumVolumeTexture.SampleLevel(gridSampler, atlasUv, 0.0);
float4 transport = mediumTransportTexture.SampleLevel(gridSampler, atlasUv, 0.0);
densityMean += diagnostic.x;
densityTravelSum += diagnostic.x * sliceTravel;
densitySum += diagnostic.x;
inScattering += transmittance * transport.rgb;
transmittance *= saturate(transport.a);
}
densityMean = saturate(densityMean / (float)MEDIUM_FROXEL_SLICE_COUNT);
if (densitySum > 0.0001)
{
representativeTravel = densityTravelSum / densitySum;
}
}
float3 shadeBody(float3 p, float3 normal, int primitiveId)
@ -257,9 +268,18 @@ SceneOut D3D12ScenePS(VertexOut input)
float densityMean;
float transmittance;
float3 inScattering;
float mediumRepresentativeTravel;
float mediumTravel = travel <= farDistance ? travel : farDistance;
integrateMedium(input.uv, mediumTravel, densityMean, transmittance, inScattering);
integrateMedium(input.uv, mediumTravel, densityMean, transmittance, inScattering, mediumRepresentativeTravel);
color = color * transmittance + inScattering;
float mediumOpacity = saturate(1.0 - transmittance);
if (outputFieldId < 0.5 && mediumOpacity > 0.015)
{
outputFieldId = FIELD_ID_MEDIUM;
outputNormal = -rayDirection;
outputCoverage = saturate(max(mediumOpacity, densityMean * 4.0));
travel = min(mediumRepresentativeTravel, farDistance);
}
if (renderDebugMode >= 10.5 && renderDebugMode < 11.5)
{
@ -269,6 +289,6 @@ SceneOut D3D12ScenePS(VertexOut input)
SceneOut output;
output.colorTravel = float4(color, min(travel, farDistance + 1.0));
output.metadata = float4(outputFieldId, outputNormal);
output.control = float4(0.0, outputCoverage, saturate(1.0 - transmittance), densityMean);
output.control = float4(0.0, outputCoverage, mediumOpacity, densityMean);
return output;
}

View file

@ -81,3 +81,4 @@
{"area":"renderer/d3d12","lesson":"D3D12 Grid transparency now carries the richer Aetheria/Aquarium line vocabulary inside the transparent-surface froxel path: cartesian gridlines, height isolines, and gradient-angle field lines are evaluated from the Grid height target and integrated as binned transparent medium contribution, not as a revived alpha-blended surface path.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl","docs/d3d12-migration.md","docs/hlsl-renderer.md","state/scratch.md"],"verification":["dotnet build src\\Aquarium.Engine\\Aquarium.Engine.csproj",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=0 exited 0",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=11 exited 0"],"ts":"2026-05-09"}
{"area":"renderer/d3d12/temporal","lesson":"D3D12 now has the first temporal diagnostic spine: the scene pass writes color/travel, field-id/normal metadata, and temporal-control MRTs, and the present shader can inspect current control in debug mode 5 and field identity in debug mode 6. This is not TAA parity yet; history ping-pong, reprojection, validation, and age/weight outputs still need to be ported before temporal accumulation is real.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","docs/d3d12-migration.md","docs/hlsl-renderer.md","state/scratch.md"],"verification":["dotnet build src\\Aquarium.Engine\\Aquarium.Engine.csproj","dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=0 exited 0",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=5 exited 0",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=6 exited 0"],"ts":"2026-05-09"}
{"area":"renderer/d3d12/temporal","lesson":"D3D12 resolve now owns presentation and history output: it writes the swapchain plus ping-pong history color, metadata, and control targets. Opaque current scene hits reproject through previous camera/Grid state, remap orbiting planet hits to previous centers, and validate by field id, travel, normal, neighborhood color, coverage, and medium opacity before blending. Debug mode 3 shows history age and mode 4 history weight. Grid/transparent-specific temporal reconstruction remains pending.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","docs/d3d12-migration.md","docs/hlsl-renderer.md","state/scratch.md"],"verification":["dotnet build src\\Aquarium.Engine\\Aquarium.Engine.csproj","dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=0 exited 0",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=3 exited 0",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=4 exited 0",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=11 exited 0"],"ts":"2026-05-09"}
{"area":"renderer/d3d12/temporal","lesson":"Medium-only D3D12 pixels now get a volumetric temporal identity instead of being excluded from history: the scene pass stores FIELD_ID_MEDIUM, coverage from medium opacity/density, and travel from a density-weighted ray centroid. Resolve treats medium history as volumetric by skipping normal rejection, relaxing color rejection, and validating continuity through coverage and medium opacity. This stabilizes binned Grid/fog contribution without turning transparent surfaces back into alpha-depth surfaces.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","docs/d3d12-migration.md","docs/hlsl-renderer.md","state/scratch.md"],"verification":["dotnet build src\\Aquarium.Engine\\Aquarium.Engine.csproj","dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=0 exited 0",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=6 exited 0",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 20 with AQUARIUM_RENDERER=d3d12 and AQUARIUM_RENDER_DEBUG_MODE=3 exited 0"],"ts":"2026-05-09"}

View file

@ -323,3 +323,9 @@ can start porting real passes while keeping D3D11 as the visual reference.
neighborhood color, coverage, and medium opacity. Debug mode 3 shows history
age and mode 4 shows history weight. Grid/transparent-specific temporal
reconstruction remains pending.
- D3D12 medium temporal identity pass: medium-only pixels now store
`FIELD_ID_MEDIUM`, coverage from medium opacity/density, and travel from a
density-weighted ray centroid. Resolve treats this as volumetric history:
normal validation is skipped, color rejection is relaxed, and continuity is
driven by coverage plus medium opacity. This lets binned Grid/fog contribution
accumulate without reintroducing alpha surfaces.