From a64bbacf2d28ff7d96e3193531fb9355b14b00fe Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 9 May 2026 02:48:43 +0100 Subject: [PATCH] Anchor medium density diagnostics --- docs/hlsl-renderer.md | 14 ++-- docs/sdf-field-renderer-plan.md | 9 ++- .../Render/Shaders/Aquarium.hlsl | 74 +++++++++++++++---- state/evidence.jsonl | 1 + state/memory.json | 2 +- state/scratch.md | 5 ++ 6 files changed, 79 insertions(+), 26 deletions(-) diff --git a/docs/hlsl-renderer.md b/docs/hlsl-renderer.md index 7c5480a..06f2fb4 100644 --- a/docs/hlsl-renderer.md +++ b/docs/hlsl-renderer.md @@ -227,12 +227,14 @@ startup mode can still be set with `--render-debug` or Fog work has started behind diagnostics rather than final-frame haze. The host packs stable field instances into a structured buffer shared with HLSL. A -half-resolution medium pass writes registered density, transmittance, and source -lighting into diagnostic and transport textures before the scene pass. Modes -`9`, `10`, and `11` show medium density, transmittance, and source respectively. -Final composition uses `surface * transmittance + in-scattering`, gated by the -persisted Medium Composite control. The default is `0`, so the pass can be -inspected without changing the presented frame until the medium is tuned. +half-resolution medium pass writes a 4x4 frustum slice atlas into diagnostic and +transport textures before the scene pass. Each atlas texel reconstructs a world +position and samples registered medium density there; resolve integrates those +stored cells front-to-back for density, transmittance, and source diagnostics. +Modes `9`, `10`, and `11` show medium density, transmittance, and source +respectively. Final composition uses `surface * transmittance + in-scattering`, +gated by the persisted Medium Composite control. The default is `0`, so the pass +can be inspected without changing the presented frame until the medium is tuned. The running window also has a Direct2D debug panel toggled with `F2`. It follows the CultLib code-first composition style rather than an immediate-mode toolkit: diff --git a/docs/sdf-field-renderer-plan.md b/docs/sdf-field-renderer-plan.md index fd25f1e..4d21f73 100644 --- a/docs/sdf-field-renderer-plan.md +++ b/docs/sdf-field-renderer-plan.md @@ -100,10 +100,11 @@ except to replace them. Current status: the renderer now packs a first fixed-size field instance buffer for Self, planets, and local cloud/medium ellipsoids. HLSL consumes that buffer -in a half-resolution medium pass that writes density, transmittance, and source -diagnostics plus transport. Final composition uses the transport texture behind -a persisted intensity gate that defaults to zero. This is the contract foothold, -not the final volume renderer. +in a half-resolution 4x4 frustum slice atlas pass that writes density, +transmittance, and source diagnostics plus transport. Density noise is evaluated +in field-local/world coordinates, not animated screen space. Final composition +uses the transport texture behind a persisted intensity gate that defaults to +zero. This is the contract foothold, not the final volume renderer. ### Gate 2: Broad Phase diff --git a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl index 0cec27c..07c6a4c 100644 --- a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl @@ -67,6 +67,8 @@ static const float TERRAIN_FIELD_LINE_PIXEL_WIDTH = 0.38; static const int CLOUD_FIELD_COUNT = 4; static const int FIELD_INSTANCE_COUNT = 10; static const int FIELD_FLAG_CLOUD = 2; +static const int MEDIUM_SLICE_GRID = 4; +static const int MEDIUM_SLICE_COUNT = MEDIUM_SLICE_GRID * MEDIUM_SLICE_GRID; static const float CLOUD_MIN_STEP = 0.085; static const float CLOUD_MAX_STEP = 0.46; static const float CLOUD_EMPTY_STEP_SCALE = 0.42; @@ -415,7 +417,7 @@ float registeredMediumDensity(float3 p, out float3 scattering) float3 local = p - field.centerRadius.xyz; local.xy = rotate2(local.xy, -field.radiusAngle.w); local /= max(field.radiusAngle.xyz, 0.001); - float erosion = saturate(0.62 + fbm3(local * 3.4 + float3(0.0, 0.0, timeSeconds * 0.018)) * 0.38); + float erosion = saturate(0.62 + fbm3(local * 3.4) * 0.38); float core = 1.0 - smoothstep(0.80, 1.05, length(local)); float fieldDensity = shell * core * erosion * field.mediumTerms.w; density += fieldDensity; @@ -469,6 +471,33 @@ float4 sampleMediumTransport(float2 uv) return mediumTransportTexture.SampleLevel(gridSampler, uv, 0.0); } +float2 mediumAtlasUv(float2 uv, int sliceIndex) +{ + int tileX = sliceIndex % MEDIUM_SLICE_GRID; + int tileY = sliceIndex / MEDIUM_SLICE_GRID; + return (float2(tileX, tileY) + uv) / (float)MEDIUM_SLICE_GRID; +} + +void integrateMediumAtlas(float2 uv, out float densityMean, out float transmittance, out float3 inScattering) +{ + densityMean = 0.0; + transmittance = 1.0; + inScattering = 0.0; + + [unroll] + for (int sliceIndex = 0; sliceIndex < MEDIUM_SLICE_COUNT; sliceIndex++) + { + float2 atlasUv = mediumAtlasUv(uv, sliceIndex); + float4 diagnostic = mediumVolumeTexture.SampleLevel(gridSampler, atlasUv, 0.0); + float4 transport = mediumTransportTexture.SampleLevel(gridSampler, atlasUv, 0.0); + densityMean += diagnostic.x; + inScattering += transmittance * transport.rgb; + transmittance *= saturate(transport.a); + } + + densityMean = saturate(densityMean / (float)MEDIUM_SLICE_COUNT); +} + void cloudFieldInfo(int index, out float3 center, out float3 radius, out float3 tint, out float densityScale) { float slowTime = timeSeconds * 0.035; @@ -1459,19 +1488,28 @@ struct MediumVolumeOut MediumVolumeOut MediumVolumePS(VertexOut input) { - float2 screenUv = float2(input.uv.x, 1.0 - input.uv.y); + float2 atlasCoord = saturate(input.uv) * (float)MEDIUM_SLICE_GRID; + int2 tile = clamp((int2)floor(atlasCoord), int2(0, 0), int2(MEDIUM_SLICE_GRID - 1, MEDIUM_SLICE_GRID - 1)); + int sliceIndex = tile.x + tile.y * MEDIUM_SLICE_GRID; + float2 localUv = frac(atlasCoord); + float2 screenUv = float2(localUv.x, 1.0 - localUv.y); float2 pixel = screenUv * resolution; float3 rayDirection = rayDirectionForPixel(pixel, jitterPixels, cameraPosition, gridCenter); - float densityIntegral; - float transmittance; - float3 inScattering; - integrateRegisteredMedium(cameraPosition, rayDirection, farDistance, densityIntegral, transmittance, inScattering); + float sliceLength = farDistance / (float)MEDIUM_SLICE_COUNT; + float travel = ((float)sliceIndex + 0.5) * sliceLength; + float3 p = cameraPosition + rayDirection * travel; + float3 scattering; + float density = registeredMediumDensity(p, scattering); + float extinction = density * 0.16; + float transmittance = exp(-extinction * sliceLength); + float3 inScattering = density > 0.001 + ? scattering * (1.0 - transmittance) / max(extinction, 0.001) + : 0.0; - float densityDebug = saturate(densityIntegral * 0.18); float sourceDebug = saturate(dot(inScattering, float3(0.2126, 0.7152, 0.0722)) * 0.65); MediumVolumeOut output; - output.diagnostic = float4(densityDebug, saturate(transmittance), sourceDebug, 1.0); + output.diagnostic = float4(saturate(density), saturate(transmittance), sourceDebug, 1.0); output.transport = float4(inScattering, saturate(transmittance)); return output; } @@ -1526,9 +1564,12 @@ SceneOut AquariumScenePS(VertexOut input) } } - float4 mediumVolume = sampleMediumVolume(input.uv); - outputMediumOpacity = saturate(1.0 - mediumVolume.y); - outputMediumDensity = saturate(mediumVolume.x); + float mediumDensityMean; + float mediumTransmittance; + float3 mediumInScattering; + integrateMediumAtlas(input.uv, mediumDensityMean, mediumTransmittance, mediumInScattering); + outputMediumOpacity = saturate(1.0 - mediumTransmittance); + outputMediumDensity = mediumDensityMean; color += float3(0.001, 0.003, 0.004); @@ -1725,10 +1766,13 @@ ResolveOut AquariumResolvePS(VertexOut input) } } - float4 mediumTransport = sampleMediumTransport(input.uv); + float mediumDensityMean; + float atlasMediumTransmittance; + float3 atlasMediumInScattering; + integrateMediumAtlas(input.uv, mediumDensityMean, atlasMediumTransmittance, atlasMediumInScattering); float mediumBlend = saturate(mediumCompositeIntensity); - float mediumTransmittance = lerp(1.0, saturate(mediumTransport.a), mediumBlend); - float3 resolved = currentColor * mediumTransmittance + mediumTransport.rgb * mediumBlend; + float blendedMediumTransmittance = lerp(1.0, atlasMediumTransmittance, mediumBlend); + float3 resolved = currentColor * blendedMediumTransmittance + atlasMediumInScattering * mediumBlend; float3 exposedColor = exposeSceneColor(resolved); float3 bloomColor = @@ -1781,7 +1825,7 @@ ResolveOut AquariumResolvePS(VertexOut input) } else if (renderDebugMode >= 10.5 && renderDebugMode < 11.5) { - float source = saturate(sampleMediumVolume(input.uv).z); + float source = saturate(dot(atlasMediumInScattering, float3(0.2126, 0.7152, 0.0722)) * 0.65); finalColor = lerp(float3(0.015, 0.015, 0.028), float3(0.95, 0.74, 0.36), source); } diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 99deb71..b71d630 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -49,3 +49,4 @@ {"area":"renderer/volumetrics","lesson":"The first fog-land implementation should establish the field contract and diagnostics before compositing fog. Aquarium now sends a fixed field instance structured buffer for Self, planets, and local medium ellipsoids to HLSL; modes 9 and 10 expose registered medium density and transmittance. Final mode 0 remains unchanged until the explicit low-res volume pass and compositor are built.","files":["src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","src/Aquarium.Engine.Contracts/GraphicsSettings.cs","src/Aquarium.Engine.Contracts/Input/InputState.cs","src/Aquarium.Engine/AquariumHost.cs","src/Aquarium.Engine/Platform/Win32Window.cs","src/Aquarium.Engine/Render/DirectWriteOverlay.cs","docs/sdf-field-renderer-plan.md","docs/hlsl-renderer.md","state/map.yaml","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4","direct headless startup with --render-debug 10 compiled shaders and loaded persisted debug mode 10"],"ts":"2026-05-09"} {"area":"renderer/volumetrics","lesson":"Registered medium diagnostics now come from a separate half-resolution medium pass instead of being integrated inside the scene shader. The pass samples the field instance buffer, writes density/transmittance/source into a medium texture, and scene/resolve stages sample that texture for debug/control. Final composition remains unchanged until the compositor is explicit.","files":["src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","src/Aquarium.Engine.Contracts/GraphicsSettings.cs","docs/sdf-field-renderer-plan.md","docs/hlsl-renderer.md","state/map.yaml","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4","direct headless startup with --render-debug 11 compiled shaders and loaded persisted debug mode 11"],"ts":"2026-05-09"} {"area":"renderer/volumetrics","lesson":"Medium composition is now explicit and gated: the half-resolution medium pass writes diagnostic and transport textures, and resolve can composite surface * transmittance + in-scattering through a persisted Medium Composite setting. The default remains zero so final presentation does not change until the medium is intentionally enabled and tuned.","files":["src/Aquarium.Engine.Contracts/GraphicsSettings.cs","src/Aquarium.Engine.Live/State/AquariumGraphicsSettingsState.cs","src/Aquarium.Engine.Live/State/AquariumCultStateStore.cs","src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","docs/cult-runtime-surface.md","docs/hlsl-renderer.md","docs/sdf-field-renderer-plan.md","state/map.yaml","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4","direct headless startup in final mode with default Medium Composite 0"],"ts":"2026-05-09"} +{"area":"renderer/volumetrics","lesson":"The first medium density view was too screen-ray dependent: it stored a completed half-res ray integral, so debug texture detail read as view-space soup. The pass now stores a 4x4 frustum slice atlas where each texel reconstructs a world point and evaluates field-local density; resolve integrates those stored cells front-to-back. Erosion noise is stationary in field-local space.","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-09"} diff --git a/state/memory.json b/state/memory.json index 008f86c..db41cb2 100644 --- a/state/memory.json +++ b/state/memory.json @@ -44,7 +44,7 @@ "For research decks with recordings, pull subtitles/transcripts before distilling. The Dreams transcript changed the lesson: preserve the evaluator/field that serves the image, not the elegant SDF/brick/froxel experiment that drifted away from the art direction.", "Sea of Thieves clouds are the stylized counterexample: authored opaque cloud geometry filtered through low-res blurred color/depth compositing can beat dense raymarching when clouds are symbolic world objects or gameplay signage. Consider tool-time SDF baking for per-vertex cloud lighting, not runtime volume traversal by reflex.", "The SDF field renderer plan is docs/sdf-field-renderer-plan.md: solids and clouds should share one field grammar, with distance, density, material, medium, bounds, and debug views.", - "Fog-land started with a renderer/HLSL field instance ABI, not final-frame haze. Aquarium now packs Self, planets, and local medium ellipsoids into a structured buffer; a half-resolution medium pass emits density, transmittance, source diagnostics, and transport. Final composition uses surface * transmittance + in-scattering behind a persisted Medium Composite control that defaults to zero.", + "Fog-land started with a renderer/HLSL field instance ABI, not final-frame haze. Aquarium now packs Self, planets, and local medium ellipsoids into a structured buffer; a half-resolution 4x4 frustum slice atlas samples field-local/world density, then emits density, transmittance, source diagnostics, and transport. Final composition uses surface * transmittance + in-scattering behind a persisted Medium Composite control that defaults to zero.", "SDF cloud distance should shape and accelerate the medium, not create a positive-distance absorbing shell. Prototype ellipsoid edges occluded farther clouds until density was changed to eroded interior occupancy with lower extinction.", "A bounded cloud prototype should march analytic ray/volume intervals, not global SDF shells. The first cloud artifact persisted until rays jumped between true ellipsoid entry/exit intervals; otherwise near fields can consume the sample budget and masquerade as occluders.", "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.", diff --git a/state/scratch.md b/state/scratch.md index d17d579..b019d28 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -143,6 +143,11 @@ prototype integrated along the camera ray before surface composition. transport targets. Resolve can composite `surface * transmittance + in-scattering`, controlled by persisted `MediumCompositeIntensity`, which defaults to zero so final remains unchanged until deliberately enabled/tuned. +- Medium density correction: the medium pass now stores a 4x4 frustum slice + atlas instead of a finished screen-space ray integral. Each slice texel + reconstructs a world point and evaluates field-local density there; resolve + integrates the stored cells front-to-back. Removed the time term from erosion + noise so the texture itself is stationary under inspection. - Debug UI control grammar now includes named option rows. Render Debug uses an option/dropdown selector instead of a numeric slider so the expanding debug mode list remains legible.