Match D3D11 grid line widths

This commit is contained in:
Meta Vens 2026-05-09 22:31:31 +01:00
commit 2019b9e3b2
4 changed files with 45 additions and 24 deletions

View file

@ -42,8 +42,13 @@ static const int VIEW_FROXEL_PRIMITIVE_SLOT_COUNT = 2;
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_MINOR_CELL = 2.0;
static const float GRID_MAJOR_CELL = 10.0;
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;
@ -98,8 +103,7 @@ float hash21(float2 p)
float stochasticTransparency(float2 screenUv, float alpha)
{
float frameSeed = frac(frameIndex * 0.754877666);
float dither = frac(hash21(screenUv * resolution + frameIndex.xx * float2(17.0, 43.0)) + frameSeed);
float dither = hash21(floor(screenUv * resolution));
return alpha - dither - 0.001 * (1.0 - ceil(alpha));
}
@ -172,24 +176,39 @@ float3 terrainNormal(float3 p)
return normalize(float3(-gradient.x, -gradient.y, 1.0));
}
float lineDistance(float2 p, float cell)
float periodicLineMask(float coordinate, float pixelWidth, float pixelFade)
{
float2 centered = abs(frac(p / cell + 0.5) - 0.5) * cell;
return min(centered.x, centered.y);
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 lineMaskFromDistance(float distanceValue, float width, float fade)
float gridLine(float2 p)
{
return 1.0 - smoothstep(width, max(width + fade, width + 0.0001), distanceValue);
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 width)
float isolineMask(float height)
{
float wrapped = abs(frac(height / TERRAIN_ISOLINE_SPACING + 0.5) - 0.5) * TERRAIN_ISOLINE_SPACING;
return lineMaskFromDistance(wrapped, width, width * 1.8);
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 width)
float fieldLineMask(float2 gradient)
{
float slope = length(gradient);
if (slope < 0.0001)
@ -198,8 +217,7 @@ float fieldLineMask(float2 gradient, float width)
}
float angleDomain = (atan2(gradient.y, gradient.x) / PI + 1.0) * 6.0;
float wrapped = abs(frac(angleDomain + 0.5) - 0.5);
float angleLine = lineMaskFromDistance(wrapped, width, width * 1.6);
float angleLine = periodicLineMask(angleDomain, TERRAIN_FIELD_LINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE);
float slopeStrength = smoothstep(0.015, 0.16, slope);
return angleLine * slopeStrength;
}
@ -350,14 +368,11 @@ float3 gridEventColor(float3 p, out float alpha)
{
float height = terrainHeight(p.xy);
float2 gradient = terrainGradient(p.xy);
float2 footprint = max(abs(ddx(p.xy)), abs(ddy(p.xy)));
float pixelWidth = max(max(footprint.x, footprint.y), 0.006);
float minor = lineMaskFromDistance(lineDistance(p.xy, GRID_MINOR_CELL), pixelWidth * 0.56, pixelWidth * 1.35);
float major = lineMaskFromDistance(lineDistance(p.xy, GRID_MAJOR_CELL), pixelWidth * 0.88, pixelWidth * 1.60);
float contour = isolineMask(height, max(pixelWidth * 0.52, 0.010)) * smoothstep(0.025, 0.25, length(gradient));
float fieldLine = fieldLineMask(gradient, max(pixelWidth * 0.90, 0.016));
float support = saturate(minor * 0.38 + major * 0.92 + contour * 0.28 + fieldLine * 0.18);
float3 color = GRID_COLOR * (minor * 0.30 + major * 0.74);
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);

View file

@ -100,3 +100,4 @@
{"timestamp":"2026-05-09T22:05:11.0000000+01:00","topic":"d3d12-view-froxel-y-contract","lesson":"The repeated horizontal strip was caused by CPU view-froxel binning using raw top-left pixel Y while the D3D12 scene shader flips input UV Y before ray generation. CPU candidate producers must mirror the shader screen-ray pixel convention exactly. Spheres now use projected screen bounds plus travel slice ranges instead of brute-force per-froxel testing, reducing bootstrap CPU cost.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","state/memory.json","state/scratch.md"]}
{"timestamp":"2026-05-09T22:01:24.4341856+01:00","topic":"d3d12-grid-depth-residency","lesson":"Depth-sparse CPU binning for the Grid is the wrong bootstrap contract. At grazing angles the Grid is a global transparent height sheet and guessed crossing slice ranges produce sliced bands. Bin the Grid by low-res screen tube and write it to all depth slices for tubes that may see the Grid radius; the shader exact height-sheet intersection remains the authority for whether an event exists inside each interval.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","state/memory.json","state/scratch.md"]}
{"timestamp":"2026-05-09T22:18:03.0000000+01:00","topic":"d3d12-grid-stochastic-retreat","lesson":"The D3D12 transparent froxel candidate experiment made the Grid angle-dependent, spiky, and brittle. The Grid should follow the proven D3D11 contract: trace it directly as a stochastic surface before the nearest solid, write Grid identity/travel/normal/coverage for TAA, and do not leave dead transparent-surface binning code in the renderer. Git is the archive.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","state/memory.json","state/scratch.md"]}
{"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"]}

View file

@ -20,6 +20,7 @@
"D3D12 Grid height must be brush-rendered, not a fullscreen per-texel loop over bodies. Use a top-down Grid-space orthographic brush pass: base field draw, then one additive up-facing quad per body into a scalar R16_Float height target. Height is one value; do not spend extra channels unless the pass contract actually gains more data. Do not use R32G32B32A32_Float for additive brush composition.",
"D3D12 field data should live in default-heap structured buffers, updated from the per-frame upload ring by CopyBufferRegion, then bound through transient SRV descriptors owned by the consuming pass. Do not make shaders read dynamic field tables straight from upload memory. D3D11 is reference-only during migration; do not pay DRY tax to preserve it.",
"Do not put the D3D12 Grid through transparent froxel candidate/event traversal. The one-pass transparent binning experiment made the Grid angle-dependent and brittle. The live Grid contract is direct scene tracing as a stochastic surface before the nearest solid, writing Grid identity/travel/normal/coverage for TAA accumulation. Git holds the old experiment; runtime code should stay lean.",
"D3D12 Grid line width normalization should match the D3D11 reference: use periodic line domains with `fwidth()` and pixel-width constants, not world-space `ddx/ddy(p.xy)` footprint guesses. Hash dither is only a temporary D3D12 fallback; bind Aetheria's blue-noise texture for the proper stochastic mask.",
"D3D12 solid scene candidate discovery belongs in the view-froxel volume. At 720p the active tube is 160x90x32; primitive ids are indexed by low-res screen cell and depth slice, then scene rays test exact solid intersections inside that slice interval. The CPU-filled view-froxel buffers are bootstrap only; the durable path should become GPU/raster binning so per-frame upload bandwidth does not become the next stupid tax.",
"D3D12 pixel shader UAV registers share the output namespace. If a shader writes SV_Target0, bind the first manual UAV at u1, not u0.",
"D3D12 shader routing derives its editable source directory from --shader-source/AQUARIUM_SHADER_SOURCE, then watches D3D12Grid/Medium/Scene/Post/Smoke HLSL files directly. Shader/PSO builds run on a background task; startup presents a lightweight loading frame until the first pipeline set is ready, and hot reload keeps the previous pipeline set if compilation fails.",

View file

@ -17,7 +17,11 @@ identity/travel/normal/coverage for temporal support, and relies on TAA to
accumulate coverage noise. Solid spheres still use view-froxel projected bounds
for empty-space skipping; transparent surface candidate buffers, descriptors,
root parameters, shader structs, and event traversal were cut rather than left
as dead scaffolding.
as dead scaffolding. D3D12 Grid line width now mirrors the D3D11 reference:
periodic domains use `fwidth()` and pixel-width constants instead of world-space
`ddx/ddy` footprint guesses. The temporary D3D12 stochastic mask is screen-pixel
stable; next polish pass should bind the Aetheria blue-noise texture like D3D11
instead of keeping a hash dither.
## Hot Context