Replace medium atlas with packed froxel volume
This commit is contained in:
parent
1918d78754
commit
5c368a2457
6 changed files with 69 additions and 41 deletions
|
|
@ -227,15 +227,18 @@ 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 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 transport diagnostics. Mode `9` is a separate
|
||||
world-anchored density column over the Grid plane, while modes `10` and `11`
|
||||
show transport transmittance and debug-boosted 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.
|
||||
packed froxel medium pass writes 32 frustum slices into diagnostic and transport
|
||||
textures before the scene pass. At 1280x720 the first volume is 160x90x32,
|
||||
packed as an 8x4 D3D11 atlas. Each froxel reconstructs a world position and
|
||||
samples registered medium density there; resolve integrates those cells
|
||||
front-to-back for composition and diagnostics. Mode `9` previews direct
|
||||
registered-medium density at the selected ray step, mode `10` previews direct
|
||||
ray transmittance to that step, and mode `11` shows integrated froxel density.
|
||||
Direct ray debug remains the correctness reference while the froxel pass earns
|
||||
final-frame authority. 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:
|
||||
|
|
|
|||
|
|
@ -100,11 +100,12 @@ 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 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.
|
||||
in a packed froxel medium pass that writes density, transmittance, and
|
||||
in-scattering transport. Density noise is evaluated in field-local/world
|
||||
coordinates, not animated screen space. Final composition uses the froxel
|
||||
transport behind a persisted intensity gate that defaults to zero, while direct
|
||||
ray debug modes remain the correctness reference. This is the first real volume
|
||||
pass, not the final sparse/cascaded volume renderer.
|
||||
|
||||
### Gate 2: Broad Phase
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,10 @@ public sealed class D3D11Renderer : IDisposable
|
|||
private const int GridHeightTextureSize = 128;
|
||||
private const int DitherTextureSize = 512;
|
||||
private const int BloomLevelCount = 3;
|
||||
private const int MediumVolumeDownscale = 4;
|
||||
private const int MediumFroxelDownscale = 8;
|
||||
private const int MediumFroxelSliceCount = 32;
|
||||
private const int MediumFroxelAtlasColumns = 8;
|
||||
private const int MediumFroxelAtlasRows = 4;
|
||||
private const int FroxelCountX = 8;
|
||||
private const int FroxelCountY = 8;
|
||||
private const int FroxelCountZ = 4;
|
||||
|
|
@ -43,7 +46,7 @@ public sealed class D3D11Renderer : IDisposable
|
|||
new(8, "Exposed Luminance"),
|
||||
new(9, "Medium Ray Density"),
|
||||
new(10, "Medium Ray Transmittance"),
|
||||
new(11, "Medium Atlas Density"),
|
||||
new(11, "Froxel Density"),
|
||||
];
|
||||
|
||||
private const float SunRadius = 1.12f;
|
||||
|
|
@ -111,6 +114,8 @@ public sealed class D3D11Renderer : IDisposable
|
|||
private readonly FieldInstanceGpu[] fieldInstances = new FieldInstanceGpu[FieldInstanceCount];
|
||||
private readonly int width;
|
||||
private readonly int height;
|
||||
private readonly int mediumFroxelWidth;
|
||||
private readonly int mediumFroxelHeight;
|
||||
private readonly int mediumVolumeWidth;
|
||||
private readonly int mediumVolumeHeight;
|
||||
private readonly string shaderPath;
|
||||
|
|
@ -137,8 +142,10 @@ public sealed class D3D11Renderer : IDisposable
|
|||
{
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
mediumVolumeWidth = Math.Max(1, width / MediumVolumeDownscale);
|
||||
mediumVolumeHeight = Math.Max(1, height / MediumVolumeDownscale);
|
||||
mediumFroxelWidth = Math.Max(1, width / MediumFroxelDownscale);
|
||||
mediumFroxelHeight = Math.Max(1, height / MediumFroxelDownscale);
|
||||
mediumVolumeWidth = mediumFroxelWidth * MediumFroxelAtlasColumns;
|
||||
mediumVolumeHeight = mediumFroxelHeight * MediumFroxelAtlasRows;
|
||||
ApplyGraphicsSettings(graphicsSettings ?? GraphicsSettings.Default);
|
||||
this.shaderPath = shaderPath ?? Path.Combine(AppContext.BaseDirectory, ShaderRelativePath);
|
||||
ReportStartupProgress(startupProgress, "Creating D3D11 device and swapchain");
|
||||
|
|
|
|||
|
|
@ -68,8 +68,9 @@ 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 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 CLOUD_MIN_STEP = 0.085;
|
||||
static const float CLOUD_MAX_STEP = 0.46;
|
||||
static const float CLOUD_EMPTY_STEP_SCALE = 0.42;
|
||||
|
|
@ -511,20 +512,31 @@ float4 sampleMediumTransport(float2 uv)
|
|||
|
||||
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;
|
||||
int tileX = sliceIndex % MEDIUM_FROXEL_ATLAS_COLUMNS;
|
||||
int tileY = sliceIndex / MEDIUM_FROXEL_ATLAS_COLUMNS;
|
||||
return (float2(tileX, tileY) + uv) / float2(MEDIUM_FROXEL_ATLAS_COLUMNS, MEDIUM_FROXEL_ATLAS_ROWS);
|
||||
}
|
||||
|
||||
void integrateMediumAtlas(float2 uv, out float densityMean, out float transmittance, out float3 inScattering)
|
||||
float mediumSliceTravel(int sliceIndex)
|
||||
{
|
||||
float t = ((float)sliceIndex + 0.5) / (float)MEDIUM_FROXEL_SLICE_COUNT;
|
||||
return t * farDistance;
|
||||
}
|
||||
|
||||
void integrateMediumFroxelAtlas(float2 uv, float maxTravel, 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++)
|
||||
[loop]
|
||||
for (int sliceIndex = 0; sliceIndex < MEDIUM_FROXEL_SLICE_COUNT; sliceIndex++)
|
||||
{
|
||||
if (mediumSliceTravel(sliceIndex) > maxTravel)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
float2 atlasUv = mediumAtlasUv(uv, sliceIndex);
|
||||
float4 diagnostic = mediumVolumeTexture.SampleLevel(gridSampler, atlasUv, 0.0);
|
||||
float4 transport = mediumTransportTexture.SampleLevel(gridSampler, atlasUv, 0.0);
|
||||
|
|
@ -533,7 +545,7 @@ void integrateMediumAtlas(float2 uv, out float densityMean, out float transmitta
|
|||
transmittance *= saturate(transport.a);
|
||||
}
|
||||
|
||||
densityMean = saturate(densityMean / (float)MEDIUM_SLICE_COUNT);
|
||||
densityMean = saturate(densityMean / (float)MEDIUM_FROXEL_SLICE_COUNT);
|
||||
}
|
||||
|
||||
void cloudFieldInfo(int index, out float3 center, out float3 radius, out float3 tint, out float densityScale)
|
||||
|
|
@ -1526,16 +1538,16 @@ struct MediumVolumeOut
|
|||
|
||||
MediumVolumeOut MediumVolumePS(VertexOut input)
|
||||
{
|
||||
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 atlasCoord = saturate(input.uv) * float2(MEDIUM_FROXEL_ATLAS_COLUMNS, MEDIUM_FROXEL_ATLAS_ROWS);
|
||||
int2 tile = clamp((int2)floor(atlasCoord), int2(0, 0), int2(MEDIUM_FROXEL_ATLAS_COLUMNS - 1, MEDIUM_FROXEL_ATLAS_ROWS - 1));
|
||||
int sliceIndex = tile.x + tile.y * MEDIUM_FROXEL_ATLAS_COLUMNS;
|
||||
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 sliceLength = farDistance / (float)MEDIUM_SLICE_COUNT;
|
||||
float travel = ((float)sliceIndex + 0.5) * sliceLength;
|
||||
float sliceLength = farDistance / (float)MEDIUM_FROXEL_SLICE_COUNT;
|
||||
float travel = mediumSliceTravel(sliceIndex);
|
||||
float3 p = cameraPosition + rayDirection * travel;
|
||||
float3 scattering;
|
||||
float density = registeredMediumDensity(p, scattering);
|
||||
|
|
@ -1605,7 +1617,7 @@ SceneOut AquariumScenePS(VertexOut input)
|
|||
float mediumDensityMean;
|
||||
float mediumTransmittance;
|
||||
float3 mediumInScattering;
|
||||
integrateMediumAtlas(input.uv, mediumDensityMean, mediumTransmittance, mediumInScattering);
|
||||
integrateMediumFroxelAtlas(input.uv, farDistance, mediumDensityMean, mediumTransmittance, mediumInScattering);
|
||||
outputMediumOpacity = saturate(1.0 - mediumTransmittance);
|
||||
outputMediumDensity = mediumDensityMean;
|
||||
|
||||
|
|
@ -1805,18 +1817,17 @@ ResolveOut AquariumResolvePS(VertexOut input)
|
|||
}
|
||||
|
||||
float mediumBlend = saturate(mediumCompositeIntensity);
|
||||
float directMediumTransmittance = 1.0;
|
||||
float3 directMediumInScattering = 0.0;
|
||||
float froxelMediumDensity = 0.0;
|
||||
float froxelMediumTransmittance = 1.0;
|
||||
float3 froxelMediumInScattering = 0.0;
|
||||
if (mediumBlend > 0.0001)
|
||||
{
|
||||
float directMediumDensity;
|
||||
float mediumTravel = currentTravel <= farDistance ? currentTravel : farDistance;
|
||||
float3 mediumRay = rayDirectionForPixel(pixel, jitterPixels, cameraPosition, gridCenter);
|
||||
integrateRegisteredMedium(cameraPosition, mediumRay, mediumTravel, directMediumDensity, directMediumTransmittance, directMediumInScattering);
|
||||
integrateMediumFroxelAtlas(input.uv, mediumTravel, froxelMediumDensity, froxelMediumTransmittance, froxelMediumInScattering);
|
||||
}
|
||||
|
||||
float blendedMediumTransmittance = lerp(1.0, directMediumTransmittance, mediumBlend);
|
||||
float3 resolved = currentColor * blendedMediumTransmittance + directMediumInScattering * mediumBlend;
|
||||
float blendedMediumTransmittance = lerp(1.0, froxelMediumTransmittance, mediumBlend);
|
||||
float3 resolved = currentColor * blendedMediumTransmittance + froxelMediumInScattering * mediumBlend;
|
||||
|
||||
float3 exposedColor = exposeSceneColor(resolved);
|
||||
float3 bloomColor =
|
||||
|
|
@ -1880,7 +1891,7 @@ ResolveOut AquariumResolvePS(VertexOut input)
|
|||
float mediumDensityMean;
|
||||
float atlasMediumTransmittance;
|
||||
float3 atlasMediumInScattering;
|
||||
integrateMediumAtlas(input.uv, mediumDensityMean, atlasMediumTransmittance, atlasMediumInScattering);
|
||||
integrateMediumFroxelAtlas(input.uv, farDistance, mediumDensityMean, atlasMediumTransmittance, atlasMediumInScattering);
|
||||
finalColor = lerp(float3(0.006, 0.016, 0.026), float3(0.32, 0.86, 1.0), saturate(mediumDensityMean * 6.0));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -57,3 +57,4 @@
|
|||
{"area":"debug-ui/volumetrics","lesson":"Ray-based debug views need a visible step selector instead of more opaque modes. DebugUi controls now support conditional visibility; the persisted MediumDebugStep slider appears only for medium ray preview modes and drives shader inspection of the selected raymarch sample or cumulative transmittance to that sample.","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/Ui/DebugUi.cs","src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 60"],"ts":"2026-05-09"}
|
||||
{"area":"renderer/volumetrics","lesson":"If the medium atlas density view shows fields but ray density/transmittance are empty at every step, check pass bindings before tuning density. The resolve shader also calls registeredMediumDensity for ray preview modes, so it must bind fieldInstances at t12 just like the medium and scene passes.","files":["src/Aquarium.Engine/Render/D3D11Renderer.cs","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 60"],"ts":"2026-05-09"}
|
||||
{"area":"renderer/volumetrics","lesson":"Do not compose the final frame from the diagnostic 4x4 medium atlas. If atlas density shows frustum-slice blobs while direct ray density is continuous, final composition should use direct registered-medium ray integration until a real froxel volume replaces the diagnostic atlas. Keep atlas density as an artifact-revealing debug view only.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 60"],"ts":"2026-05-09"}
|
||||
{"area":"renderer/volumetrics","lesson":"The medium atlas trial has been replaced by a first real packed froxel volume: independent froxel dimensions, 32 depth slices, 8x4 D3D11 atlas packing, per-froxel density/transmittance/in-scattering, and resolve-side front-to-back composition. Direct ray density/transmittance debug remains the correctness reference; froxel density is the volume inspection view.","files":["src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","docs/hlsl-renderer.md","docs/sdf-field-renderer-plan.md","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 60"],"ts":"2026-05-09"}
|
||||
|
|
|
|||
|
|
@ -196,3 +196,8 @@ prototype integrated along the camera ray before surface composition.
|
|||
artifact; composite now uses the same direct registered-medium ray integration
|
||||
as ray debug, clamped to the visible surface travel. Atlas density remains a
|
||||
diagnostic view only.
|
||||
- First real froxel volume pass: the failed 4x4 MAD path has been replaced by a
|
||||
packed 160x90x32-at-720p frustum froxel volume stored as an 8x4 D3D11 atlas.
|
||||
The medium pass writes per-froxel density/transmittance/in-scattering, resolve
|
||||
integrates the froxel transport for final composite, and direct ray debug
|
||||
remains the reference for checking shape and continuity.
|
||||
|
|
|
|||
Loading…
Reference in a new issue