diff --git a/docs/hlsl-renderer.md b/docs/hlsl-renderer.md index 2af2018..dd08291 100644 --- a/docs/hlsl-renderer.md +++ b/docs/hlsl-renderer.md @@ -126,9 +126,15 @@ Current prototype traits: The Grid now renders as a transparent schematic overlay. `AquariumPS` traces bodies separately from the Grid surface, shades solid bodies first, then -intersects the Grid height field and blends lines, isolines, field lines, and a -thin weather tint over the scene. The Grid no longer owns the opaque terrain hit -that stops scene composition. +intersects the Grid height field. The Grid only draws when its surface is closer +than the nearest solid body, so it behaves like diegetic scene UI rather than a +final-image HUD layer over planets. + +Grid coverage follows Aetheria's dithered particle transparency pattern: +screen-space stochastic alpha test instead of conventional alpha blending. This +uses Aetheria's `LDR_LLL1_0` blue-noise texture bound as a wrapping `R8_UNorm` +shader resource, following the `_DitheringTex` + `_FrameNumber * +1.61803398875` `ditherClip` path from `Dither Functions.cginc`. This keeps the current scene legible while leaving future volumetrics to provide the real spatial mass. diff --git a/src/Aquarium.Engine/Aquarium.Engine.csproj b/src/Aquarium.Engine/Aquarium.Engine.csproj index 3ee1085..0b1fb5a 100644 --- a/src/Aquarium.Engine/Aquarium.Engine.csproj +++ b/src/Aquarium.Engine/Aquarium.Engine.csproj @@ -24,6 +24,7 @@ + diff --git a/src/Aquarium.Engine/Assets/Textures/Aetheria-LDR_LLL1_0.png b/src/Aquarium.Engine/Assets/Textures/Aetheria-LDR_LLL1_0.png new file mode 100644 index 0000000..e7d9173 Binary files /dev/null and b/src/Aquarium.Engine/Assets/Textures/Aetheria-LDR_LLL1_0.png differ diff --git a/src/Aquarium.Engine/Assets/Textures/Aetheria-LDR_LLL1_0.r8 b/src/Aquarium.Engine/Assets/Textures/Aetheria-LDR_LLL1_0.r8 new file mode 100644 index 0000000..bcf3b09 Binary files /dev/null and b/src/Aquarium.Engine/Assets/Textures/Aetheria-LDR_LLL1_0.r8 differ diff --git a/src/Aquarium.Engine/Render/D3D11Renderer.cs b/src/Aquarium.Engine/Render/D3D11Renderer.cs index 531e915..d8e01a9 100644 --- a/src/Aquarium.Engine/Render/D3D11Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D11Renderer.cs @@ -13,9 +13,11 @@ namespace Aquarium.Engine.Render; public sealed class D3D11Renderer : IDisposable { private const string ShaderRelativePath = "Render/Shaders/Aquarium.hlsl"; + private const string DitherTextureRelativePath = "Assets/Textures/Aetheria-LDR_LLL1_0.r8"; private static readonly TimeSpan ShaderReloadPollInterval = TimeSpan.FromMilliseconds(250); private static readonly TimeSpan ShaderReloadWriteSettleTime = TimeSpan.FromMilliseconds(150); private const int GridHeightTextureSize = 128; + private const int DitherTextureSize = 512; private const int FroxelCountX = 8; private const int FroxelCountY = 8; private const int FroxelCountZ = 4; @@ -34,7 +36,10 @@ public sealed class D3D11Renderer : IDisposable private readonly ID3D11Texture2D gridHeightTexture; private readonly ID3D11RenderTargetView gridHeightRenderTargetView; private readonly ID3D11ShaderResourceView gridHeightShaderResourceView; + private readonly ID3D11Texture2D ditherTexture; + private readonly ID3D11ShaderResourceView ditherShaderResourceView; private readonly ID3D11SamplerState gridSampler; + private readonly ID3D11SamplerState ditherSampler; private readonly ID3D11Buffer frameConstantBuffer; private readonly ID3D11Buffer froxelPrimitiveBuffer; private readonly ID3D11ShaderResourceView froxelPrimitiveShaderResourceView; @@ -102,6 +107,9 @@ public sealed class D3D11Renderer : IDisposable gridHeightTexture = CreateGridHeightTexture(); gridHeightRenderTargetView = device.CreateRenderTargetView(gridHeightTexture); gridHeightShaderResourceView = device.CreateShaderResourceView(gridHeightTexture); + startupProgress?.Invoke("Loading Aetheria blue-noise dither texture"); + ditherTexture = CreateDitherTexture(Path.Combine(AppContext.BaseDirectory, DitherTextureRelativePath)); + ditherShaderResourceView = device.CreateShaderResourceView(ditherTexture); gridSampler = device.CreateSamplerState(new SamplerDescription { Filter = Filter.MinMagMipLinear, @@ -112,6 +120,16 @@ public sealed class D3D11Renderer : IDisposable MinLOD = 0.0f, MaxLOD = float.MaxValue, }); + ditherSampler = device.CreateSamplerState(new SamplerDescription + { + Filter = Filter.MinMagMipPoint, + AddressU = TextureAddressMode.Wrap, + AddressV = TextureAddressMode.Wrap, + AddressW = TextureAddressMode.Wrap, + ComparisonFunc = ComparisonFunction.Never, + MinLOD = 0.0f, + MaxLOD = float.MaxValue, + }); frameConstantBuffer = device.CreateBuffer( 48, BindFlags.ConstantBuffer, @@ -164,7 +182,10 @@ public sealed class D3D11Renderer : IDisposable froxelPrimitiveShaderResourceView.Dispose(); froxelPrimitiveBuffer.Dispose(); frameConstantBuffer.Dispose(); + ditherSampler.Dispose(); gridSampler.Dispose(); + ditherShaderResourceView.Dispose(); + ditherTexture.Dispose(); gridHeightShaderResourceView.Dispose(); gridHeightRenderTargetView.Dispose(); gridHeightTexture.Dispose(); @@ -196,10 +217,32 @@ public sealed class D3D11Renderer : IDisposable return device.CreateTexture2D(description); } + private ID3D11Texture2D CreateDitherTexture(string path) + { + var pixels = File.ReadAllBytes(path); + if (pixels.Length != DitherTextureSize * DitherTextureSize) + { + throw new InvalidOperationException($"Dither texture {path} must be {DitherTextureSize}x{DitherTextureSize} R8 data."); + } + + return device.CreateTexture2D( + pixels, + Format.R8_UNorm, + DitherTextureSize, + DitherTextureSize, + 1, + 1, + BindFlags.ShaderResource, + ResourceOptionFlags.None, + ResourceUsage.Default, + CpuAccessFlags.None); + } + private void RenderGridHeight() { context.PSUnsetShaderResource(0); context.PSUnsetShaderResource(1); + context.PSUnsetShaderResource(2); context.OMSetRenderTargets(gridHeightRenderTargetView); context.RSSetViewport(0.0f, 0.0f, GridHeightTextureSize, GridHeightTextureSize, 0.0f, 1.0f); context.ClearRenderTargetView(gridHeightRenderTargetView, new Color4(0.0f, 0.0f, 0.0f, 0.0f)); @@ -223,7 +266,9 @@ public sealed class D3D11Renderer : IDisposable context.PSSetConstantBuffer(0, frameConstantBuffer); context.PSSetShaderResource(0, gridHeightShaderResourceView); context.PSSetShaderResource(1, froxelPrimitiveShaderResourceView); + context.PSSetShaderResource(2, ditherShaderResourceView); context.PSSetSampler(0, gridSampler); + context.PSSetSampler(1, ditherSampler); context.Draw(3, 0); } diff --git a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl index ba1f90c..8701295 100644 --- a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl @@ -12,7 +12,9 @@ cbuffer AquariumFrame : register(b0) Texture2D gridHeightTexture : register(t0); StructuredBuffer froxelPrimitiveIds : register(t1); +Texture2D ditherTexture : register(t2); SamplerState gridSampler : register(s0); +SamplerState ditherSampler : register(s1); static const float PI = 3.14159265359; static const float SURFACE_EPSILON = 0.0015; @@ -101,6 +103,14 @@ float hash31(float3 p) return frac((p.x + p.y) * p.z) * 2.0 - 1.0; } +float stochasticTransparency(float2 screenUv, float alpha) +{ + float frameSeed = floor(timeSeconds * 60.0); + float2 ditherUv = screenUv * (resolution / 512.0); + float dither = frac(ditherTexture.SampleLevel(ditherSampler, ditherUv, 0.0).r + frameSeed * 1.61803398875); + return alpha - dither - 0.001 * (1.0 - ceil(alpha)); +} + float noised3(float3 x) { float3 p = floor(x); @@ -1178,20 +1188,25 @@ float4 AquariumPS(VertexOut input) : SV_Target int materialId; float travel; float3 color = 0.0; + float nearestSolidTravel = farDistance + 1.0; if (raymarchBodies(cameraPosition, rayDirection, hitPosition, materialId, travel)) { float3 normal = surfaceNormal(hitPosition, materialId); color = shade(hitPosition, normal, materialId, rayDirection); color *= exp(-travel * 0.012); + nearestSolidTravel = travel; } float3 gridHitPosition; float gridTravel; - if (traceGridSurface(cameraPosition, rayDirection, gridHitPosition, gridTravel)) + if (traceGridSurface(cameraPosition, rayDirection, gridHitPosition, gridTravel) && gridTravel < nearestSolidTravel) { float4 gridOverlay = shadeGridOverlay(gridHitPosition); - color = lerp(color, gridOverlay.rgb, gridOverlay.a); + if (stochasticTransparency(screenUv, gridOverlay.a) > 0.0) + { + color = gridOverlay.rgb; + } } color += float3(0.001, 0.003, 0.004); diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 617b756..818dc62 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -14,3 +14,4 @@ {"area":"renderer/sdf-fields","lesson":"Fainter extinction did not fix near-cloud silhouettes because the prototype was still structurally wrong: a global SDF shell march could spend the frame's cloud sample budget inside a nearer ellipsoid and fail to reach farther cloud intervals. The analytic prototype now intersects ray/ellipsoid intervals, jumps empty space to true entries, and samples density only inside container volumes.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","docs/hlsl-renderer.md","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"],"ts":"2026-05-08"} {"area":"renderer/sdf-fields","lesson":"The SDF field/cloud renderer should use a waterfall implementation shape, not a chain of shy compromised prototypes. Define the target machine first: field ABI, broad phase, explicit local volume pass, composition contract, and acceleration gates. The current fullscreen analytic cloud code is temporary scaffolding for inspection only, not a place to accumulate features.","files":["docs/sdf-field-renderer-plan.md","docs/hlsl-renderer.md","state/memory.json","state/scratch.md"],"ts":"2026-05-08"} {"area":"renderer/composition","lesson":"Removed the analytic SDF cloud scaffold from visible scene composition and made the Grid a transparent schematic overlay. Bodies now trace as solid scene content; the Grid traces separately and blends lines/isolines/field lines/weather tint over the result. This preserves legibility while reserving real scene mass for the future explicit volumetric field renderer.","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-08"} +{"area":"renderer/composition","lesson":"Diegetic Grid transparency over raymarched content should use Aetheria-style stochastic coverage, not alpha blending. Aquarium now ports the dither-clip path from Aetheria's Dither Functions and Dithered Particles shaders using the copied LDR_LLL1_0 blue-noise texture, and it only draws the Grid when the Grid hit is closer than the nearest solid body so planets are not overlaid.","files":["src/Aquarium.Engine/Assets/Textures/Aetheria-LDR_LLL1_0.png","src/Aquarium.Engine/Assets/Textures/Aetheria-LDR_LLL1_0.r8","src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","src/Aquarium.Engine/Aquarium.Engine.csproj","docs/hlsl-renderer.md","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"],"ts":"2026-05-08"} diff --git a/state/memory.json b/state/memory.json index b38cede..458e06e 100644 --- a/state/memory.json +++ b/state/memory.json @@ -46,6 +46,7 @@ "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.", "Until the real field renderer exists, do not composite the analytic SDF cloud scaffold into the Aquarium scene. The Grid should read as a transparent schematic overlay; volumetrics will own scene mass later.", + "For diegetic Grid transparency over raymarched content, use Aetheria-style stochastic coverage rather than alpha blending, and depth-test it against nearer solid bodies so the Grid does not draw over planets. Aquarium has copied Aetheria's LDR_LLL1_0 blue-noise texture and binds it for Grid dither coverage.", "Bruneton-style atmospheric scattering is the later sky/planet solution, not the first Grid fog pass. Use LUTs, explicit texture mappings, and unit-tested physical terms when Aquarium reaches ground-to-space atmosphere.", "GigaVoxels-style ray-guided sparse bricks are only for large sparse field/world storage. Do not turn early fog into a monolithic persistent volume cache." ], diff --git a/state/scratch.md b/state/scratch.md index 31d4692..5a4241d 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -34,3 +34,9 @@ prototype integrated along the camera ray before surface composition. - Current scene correction: analytic cloud scaffold is no longer composited. Bodies render as solids; the Grid traces separately and blends as a transparent schematic overlay so future volumetrics can own actual scene mass. +- Grid transparency correction: do not use alpha blending over raymarched scene + content. Use Aetheria-style stochastic coverage and only draw the Grid when it + is nearer than the nearest solid body, so it does not overlay planets. +- Pulled Aetheria's `Assets/Resources/LDR_LLL1_0.png` into Aquarium as source + PNG plus runtime `R8_UNorm` bytes. The renderer binds it as `t2` with wrapping + point sampling for stochastic Grid coverage.