Port D3D12 binned Grid linework

This commit is contained in:
Meta Vens 2026-05-09 16:52:29 +01:00
commit 4520005a85
5 changed files with 79 additions and 19 deletions

View file

@ -9,16 +9,10 @@ has feature parity.
- `--renderer d3d11` is the default renderer and owns the live scene.
- `--renderer d3d12` creates a D3D12 device, command queue, flip-discard
swapchain, RTV heap, per-backbuffer command allocators, command list,
fence-backed present loop, shader-visible CBV/SRV/UAV descriptor arena,
per-frame upload buffers, renderer-owned Grid height target, offscreen smoke
target, fullscreen root signature, diagnostic UAV target, and smoke-test PSO.
- The D3D12 path currently draws a diagnostic fullscreen pass. It exists to
prove backend lifecycle, shader compilation, root signature creation,
descriptor-table binding, per-frame upload-ring constants, PSO creation,
render target binding, tracked resource state transitions,
render-target-to-swapchain copy, UAV binding from a transient descriptor,
named objects, command-list events, and draw submission before real scene
passes migrate.
fence-backed present loop, static and transient shader-visible descriptor
arenas, per-frame upload buffers, renderer-owned Grid height, medium, HDR
scene, and bloom render targets, a fullscreen root signature, named objects,
command-list events, and explicit tracked resource transitions.
- The first real migrated pass is the Grid height pass. D3D12 uploads Aquarium
frame constants plus a fixed body brush table, renders a base Grid field, then
draws one additive up-facing gravity quad per body into a 128x128
@ -39,7 +33,14 @@ has feature parity.
- Transparent Grid contribution is now represented by a transparent-surface
table plus froxel-binned surface ids. The medium pass discovers it through the
froxel bin, so future particles and billboard-like surfaces can use the same
class of integration instead of gaining special alpha handling.
class of integration instead of gaining special alpha handling. The binned
Grid surface now carries cartesian gridlines, height isolines, and
gradient-angle field lines through that medium path.
- D3D12 presentation is HDR-linear until the present pass. The scene renders to
`R16G16B16A16_Float`, a three-level bloom pyramid performs firefly-safe
downsample plus separable blur, and final presentation applies exposure,
bloom/veil, and ACES. Debug mode `7` shows bloom and `8` shows exposed
luminance.
- 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

@ -87,10 +87,12 @@ table into default-heap structured buffers every frame. The D3D12 medium pass
renders a packed frustum froxel atlas into diagnostic and transport render
targets from those field instances, and mode `11` displays the froxel density
target. D3D12 final mode now renders a first scene pass for Self, planets, and
medium transport. Grid line transparency contributes to the froxel medium as a
thin binned transparent-surface layer, not as an alpha-blended surface or
scene-depth terminator. The full temporal resolve, bloom pyramid, and full Grid
line shader still need to move.
medium transport. Grid transparency contributes to the froxel medium as a thin
binned transparent-surface layer, not as an alpha-blended surface or scene-depth
terminator. That binned Grid layer now carries cartesian gridlines, height
isolines, and gradient-angle field lines. D3D12 also owns an HDR scene target
and bloom/present pass. The full temporal resolve and overlay/debug UI still
need to move.
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

@ -48,6 +48,8 @@ SamplerState gridSampler : register(s0);
static const int FIELD_INSTANCE_COUNT = 10;
static const int FIELD_FLAG_CLOUD = 2;
static const float PI = 3.14159265359;
static const float GRID_HEIGHT_TEXEL_COUNT = 128.0;
static const int FROXEL_COUNT_X = 8;
static const int FROXEL_COUNT_Y = 8;
static const int FROXEL_COUNT_Z = 4;
@ -57,6 +59,7 @@ static const float FROXEL_MAX_Z = 6.0;
static const int MEDIUM_FROXEL_ATLAS_COLUMNS = 8;
static const int MEDIUM_FROXEL_ATLAS_ROWS = 4;
static const int MEDIUM_FROXEL_SLICE_COUNT = MEDIUM_FROXEL_ATLAS_COLUMNS * MEDIUM_FROXEL_ATLAS_ROWS;
static const float TERRAIN_ISOLINE_SPACING = 0.12;
struct VertexOut
{
@ -225,12 +228,52 @@ float terrainHeight(float2 p)
return gridHeightTexture.SampleLevel(gridSampler, uv, 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(gridSampler, uv - float2(texel.x, 0.0), 0.0).r;
float hRight = gridHeightTexture.SampleLevel(gridSampler, uv + float2(texel.x, 0.0), 0.0).r;
float hDown = gridHeightTexture.SampleLevel(gridSampler, uv - float2(0.0, texel.y), 0.0).r;
float hUp = gridHeightTexture.SampleLevel(gridSampler, uv + float2(0.0, texel.y), 0.0).r;
return float2(hRight - hLeft, hUp - hDown) / (texelWorld * 2.0);
}
float lineDistance(float2 p, float cell)
{
float2 centered = abs(frac(p / cell + 0.5) - 0.5) * cell;
return min(centered.x, centered.y);
}
float lineMaskFromDistance(float distanceValue, float width, float fade)
{
return 1.0 - smoothstep(width, max(width + fade, width + 0.0001), distanceValue);
}
float isolineMask(float height, float width)
{
float wrapped = abs(frac(height / TERRAIN_ISOLINE_SPACING + 0.5) - 0.5) * TERRAIN_ISOLINE_SPACING;
return lineMaskFromDistance(wrapped, width, width * 1.8);
}
float fieldLineMask(float2 gradient, float width)
{
float slope = length(gradient);
if (slope < 0.0001)
{
return 0.0;
}
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 slopeStrength = smoothstep(0.015, 0.16, slope);
return angleLine * slopeStrength;
}
int clampCell(float normalized, int count)
{
return clamp((int)floor(normalized * (float)count), 0, count - 1);
@ -263,11 +306,18 @@ float transparentSurfaceDensity(float3 p, TransparentSurface surface, out float3
}
float height = terrainHeight(p.xy);
float2 gradient = terrainGradient(p.xy);
float sheet = 1.0 - smoothstep(0.018, max(surface.kindDensity.w, 0.02), abs(p.z - height));
float minor = 1.0 - smoothstep(0.016, max(surface.lineParams.z, 0.016), lineDistance(p.xy, surface.lineParams.x));
float major = 1.0 - smoothstep(0.030, max(surface.lineParams.w, 0.030), lineDistance(p.xy, surface.lineParams.y));
float density = sheet * radiusMask * saturate(minor * 0.42 + major * 0.92);
scattering = surface.colorScatter.rgb * density * surface.colorScatter.w;
float minor = lineMaskFromDistance(lineDistance(p.xy, surface.lineParams.x), 0.016, max(surface.lineParams.z, 0.016));
float major = lineMaskFromDistance(lineDistance(p.xy, surface.lineParams.y), 0.030, max(surface.lineParams.w, 0.030));
float contour = isolineMask(height, 0.012) * smoothstep(0.025, 0.25, length(gradient));
float fieldLine = fieldLineMask(gradient, 0.038);
float lineSupport = saturate(minor * 0.38 + major * 0.92 + contour * 0.28 + fieldLine * 0.18);
float density = sheet * radiusMask * lineSupport;
float3 gridColor = surface.colorScatter.rgb * (minor * 0.30 + major * 0.74);
gridColor += float3(0.98, 1.0, 0.78) * contour * 0.16;
gridColor += float3(0.36, 0.92, 1.0) * fieldLine * 0.12;
scattering = gridColor * density * surface.colorScatter.w;
return density * surface.kindDensity.y;
}

View file

@ -78,3 +78,4 @@
{"area":"renderer/d3d12","lesson":"D3D12 final mode now renders a first real scene pass instead of smoke: Self, planets, and medium transport are composed from D3D12 resources. Grid line transparency is injected into the medium froxel atlas as a thin participating layer and is not used as a scene-depth terminator; transparent surfaces should enter the same binned froxel/field integration path as volumetrics rather than alpha-blending as a separate class.","files":["src/Aquarium.Engine/Render/D3D12RenderTarget.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","docs/d3d12-migration.md","docs/d3d12-best-practices-audit.md","docs/hlsl-renderer.md","state/map.yaml","state/memory.json","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=11 exited 0"],"ts":"2026-05-09"}
{"area":"renderer/d3d12","lesson":"Transparent Grid contribution is now routed through an explicit transparent-surface table and froxel-binned transparent surface id buffer. The medium pass resolves transparent surface density/scattering by looking up the current froxel, so Grid/particles/billboards can share the same integration class instead of becoming alpha-blended scene-depth exceptions. The Medium Composite knob gates registered cloud media, while binned transparent surfaces still contribute to transport.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","docs/d3d12-migration.md","docs/d3d12-best-practices-audit.md","docs/hlsl-renderer.md","state/map.yaml","state/memory.json","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=11 exited 0"],"ts":"2026-05-09"}
{"area":"renderer/d3d12","lesson":"D3D12 presentation now mirrors the HDR doctrine: render scene-linear into an R16G16B16A16_Float scene target, build a three-level half/quarter/eighth bloom pyramid with firefly-safe downsample and separable blur scratch targets, then apply exposure, bloom/veil composite, and ACES only in the present pass. Debug mode 7 shows bloom contribution, mode 8 exposed luminance, and mode 11 remains a direct medium-density bypass.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","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=7 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","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"}

View file

@ -305,3 +305,9 @@ can start porting real passes while keeping D3D11 as the visual reference.
exposure, bloom/veil composite, and ACES presentation. Debug mode 7 shows
bloom contribution; mode 8 shows exposed luminance; mode 11 still bypasses
post to inspect the medium density atlas.
- D3D12 binned Grid linework pass: the transparent surface medium path now
evaluates Grid height gradients and contributes cartesian gridlines, height
isolines, and gradient-angle field lines from the same froxel-binned
transparent surface entry. Keep future particles/billboards in this shared
transparent-surface integration class; do not resurrect alpha surfaces for
them.