diff --git a/docs/hlsl-renderer.md b/docs/hlsl-renderer.md index 213b147..2443c7b 100644 --- a/docs/hlsl-renderer.md +++ b/docs/hlsl-renderer.md @@ -44,15 +44,19 @@ The renderer now has a deferred Grid height target. Current frame order: -1. `GridHeightPS` renders body/gravity height into a 128x128 Grid-space +1. C# bins Self and planet bounds into a small Grid-space froxel table and + uploads fixed primitive id slots as a structured buffer. +2. `GridHeightPS` renders body/gravity height into a 128x128 Grid-space `R32G32B32A32_Float` render target. -2. `AquariumPS` raymarches solids and terrain against that height texture. -3. Terrain marching uses heightfield crossing and slope-aware steps instead of +3. `AquariumPS` raymarches solids and terrain against that height texture. +4. Body SDF checks read only the primitive ids binned into the current froxel. + Empty froxels skip body SDF work entirely. +5. Terrain marching uses heightfield crossing and slope-aware steps instead of pretending `z - height(xy)` is a true Euclidean SDF. Body objects still use SDF distance steps. -4. Terrain normals sample adjacent Grid texels in world space instead of +6. Terrain normals sample adjacent Grid texels in world space instead of re-evaluating analytic height per normal tap. -5. The Grid target is centered on the camera target; body anchors remain in +7. The Grid target is centered on the camera target; body anchors remain in world space. The target currently stores height in `.r` only. The next useful expansion is to diff --git a/src/Aquarium.Engine/Render/D3D11Renderer.cs b/src/Aquarium.Engine/Render/D3D11Renderer.cs index c45677f..f261f2d 100644 --- a/src/Aquarium.Engine/Render/D3D11Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D11Renderer.cs @@ -4,6 +4,8 @@ using Vortice.Direct3D11; using Vortice.DXGI; using Vortice.Mathematics; using CultMath; +using System.Numerics; +using System.Runtime.InteropServices; namespace Aquarium.Engine.Render; @@ -11,6 +13,15 @@ public sealed class D3D11Renderer : IDisposable { private const string ShaderRelativePath = "Render/Shaders/Aquarium.hlsl"; private const int GridHeightTextureSize = 128; + private const int FroxelCountX = 8; + private const int FroxelCountY = 8; + private const int FroxelCountZ = 4; + private const int FroxelSlotCount = 2; + private const int FroxelBufferElementCount = FroxelCountX * FroxelCountY * FroxelCountZ * FroxelSlotCount; + private const int PlanetCount = 5; + private const float SunRadius = 1.12f; + private const float FroxelMinZ = -2.0f; + private const float FroxelMaxZ = 6.0f; private readonly IDXGISwapChain swapChain; private readonly ID3D11Device device; @@ -24,6 +35,9 @@ public sealed class D3D11Renderer : IDisposable private readonly ID3D11PixelShader gridHeightPixelShader; private readonly ID3D11PixelShader pixelShader; private readonly ID3D11Buffer frameConstantBuffer; + private readonly ID3D11Buffer froxelPrimitiveBuffer; + private readonly ID3D11ShaderResourceView froxelPrimitiveShaderResourceView; + private readonly Int4[] froxelPrimitiveIds = new Int4[FroxelBufferElementCount]; private readonly int width; private readonly int height; @@ -92,6 +106,17 @@ public sealed class D3D11Renderer : IDisposable CpuAccessFlags.None, ResourceOptionFlags.None, 0); + froxelPrimitiveBuffer = device.CreateBuffer( + froxelPrimitiveIds, + BindFlags.ShaderResource, + ResourceUsage.Default, + CpuAccessFlags.None, + ResourceOptionFlags.BufferStructured, + 0, + (uint)Marshal.SizeOf()); + froxelPrimitiveShaderResourceView = device.CreateShaderResourceView( + froxelPrimitiveBuffer, + new ShaderResourceViewDescription(froxelPrimitiveBuffer, Format.Unknown, 0, FroxelBufferElementCount)); } public void Render(AquariumFrame frame) @@ -105,7 +130,9 @@ public sealed class D3D11Renderer : IDisposable (float2)frame.Grid.Center, new float2(0.0f, 0.0f)); + BuildFroxelPrimitiveTable(frame); context.UpdateSubresource(in constants, frameConstantBuffer); + context.UpdateSubresource(froxelPrimitiveIds, froxelPrimitiveBuffer); RenderGridHeight(); RenderAquarium(); swapChain.Present(1, PresentFlags.None); @@ -113,6 +140,8 @@ public sealed class D3D11Renderer : IDisposable public void Dispose() { + froxelPrimitiveShaderResourceView.Dispose(); + froxelPrimitiveBuffer.Dispose(); frameConstantBuffer.Dispose(); gridSampler.Dispose(); gridHeightShaderResourceView.Dispose(); @@ -149,6 +178,7 @@ public sealed class D3D11Renderer : IDisposable private void RenderGridHeight() { context.PSUnsetShaderResource(0); + context.PSUnsetShaderResource(1); 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)); @@ -171,10 +201,145 @@ public sealed class D3D11Renderer : IDisposable context.PSSetShader(pixelShader); context.PSSetConstantBuffer(0, frameConstantBuffer); context.PSSetShaderResource(0, gridHeightShaderResourceView); + context.PSSetShaderResource(1, froxelPrimitiveShaderResourceView); context.PSSetSampler(0, gridSampler); context.Draw(3, 0); } + private void BuildFroxelPrimitiveTable(AquariumFrame frame) + { + for (var i = 0; i < froxelPrimitiveIds.Length; i++) + { + froxelPrimitiveIds[i] = new Int4(-1, -1, -1, -1); + } + + AddPrimitiveToFroxels(frame, 0, new Vector3(0.0f, 0.0f, 2.2f), SunRadius * 1.22f); + + for (var i = 0; i < PlanetCount; i++) + { + var radius = PlanetRadius(i); + AddPrimitiveToFroxels(frame, i + 1, PlanetCenter(i, frame.TimeSeconds, radius), radius * 1.16f); + } + } + + private void AddPrimitiveToFroxels(AquariumFrame frame, int primitiveId, Vector3 center, float boundRadius) + { + var min = center - new Vector3(boundRadius); + var max = center + new Vector3(boundRadius); + var minCell = FroxelCellForPosition(frame, min); + var maxCell = FroxelCellForPosition(frame, max); + + for (var z = minCell.Z; z <= maxCell.Z; z++) + { + for (var y = minCell.Y; y <= maxCell.Y; y++) + { + for (var x = minCell.X; x <= maxCell.X; x++) + { + AddPrimitiveToFroxel(FroxelIndex(x, y, z), primitiveId); + } + } + } + } + + private void AddPrimitiveToFroxel(int froxelIndex, int primitiveId) + { + var baseElement = froxelIndex * FroxelSlotCount; + for (var slotGroup = 0; slotGroup < FroxelSlotCount; slotGroup++) + { + var elementIndex = baseElement + slotGroup; + var element = froxelPrimitiveIds[elementIndex]; + + if (element.X == primitiveId || element.Y == primitiveId || element.Z == primitiveId || element.W == primitiveId) + { + return; + } + + if (element.X == -1) + { + froxelPrimitiveIds[elementIndex] = element with { X = primitiveId }; + return; + } + + if (element.Y == -1) + { + froxelPrimitiveIds[elementIndex] = element with { Y = primitiveId }; + return; + } + + if (element.Z == -1) + { + froxelPrimitiveIds[elementIndex] = element with { Z = primitiveId }; + return; + } + + if (element.W == -1) + { + froxelPrimitiveIds[elementIndex] = element with { W = primitiveId }; + return; + } + } + } + + private static FroxelCell FroxelCellForPosition(AquariumFrame frame, Vector3 position) + { + var gridCenter = frame.Grid.Center; + var gridRadius = MathF.Max(frame.Grid.Radius, 0.001f); + var localX = ((position.X - gridCenter.X) / gridRadius) * 0.5f + 0.5f; + var localY = ((position.Y - gridCenter.Y) / gridRadius) * 0.5f + 0.5f; + var localZ = (position.Z - FroxelMinZ) / (FroxelMaxZ - FroxelMinZ); + + return new FroxelCell( + ClampCell(localX, FroxelCountX), + ClampCell(localY, FroxelCountY), + ClampCell(localZ, FroxelCountZ)); + } + + private static int ClampCell(float normalized, int count) + { + return Math.Clamp((int)MathF.Floor(normalized * count), 0, count - 1); + } + + private static int FroxelIndex(int x, int y, int z) + { + return x + y * FroxelCountX + z * FroxelCountX * FroxelCountY; + } + + private static float PlanetRadius(int index) + { + return Lerp(0.34f, 0.62f, Hash21(index, 19.7f)); + } + + private static Vector3 PlanetCenter(int index, float timeSeconds, float radius) + { + var f = (float)index; + var angle = f * 0.8975979f + timeSeconds * (0.08f + 0.011f * f); + var orbitRadius = 4.1f + f * 0.77f; + return new Vector3( + MathF.Cos(angle) * orbitRadius, + MathF.Sin(angle) * orbitRadius, + 1.15f + radius * 0.72f); + } + + private static float Hash21(float x, float y) + { + x = Frac(x * 123.34f); + y = Frac(y * 456.21f); + var d = x * (x + 45.32f) + y * (y + 45.32f); + x += d; + y += d; + return Frac(x * y); + } + + private static float Frac(float value) + { + return value - MathF.Floor(value); + } + + private static float Lerp(float a, float b, float t) + { + return a + (b - a) * t; + } + private static ReadOnlyMemory CompileShader(string path, string entryPoint, string profile) { var shaderFlags = ShaderFlags.EnableStrictness; @@ -193,4 +358,9 @@ public sealed class D3D11Renderer : IDisposable float CameraPad, float2 GridCenter, float2 Pad0); + + private readonly record struct FroxelCell(int X, int Y, int Z); + + [StructLayout(LayoutKind.Sequential)] + private readonly record struct Int4(int X, int Y, int Z, int W); } diff --git a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl index d859ec4..4a6d25f 100644 --- a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl @@ -10,6 +10,7 @@ cbuffer AquariumFrame : register(b0) }; Texture2D gridHeightTexture : register(t0); +StructuredBuffer froxelPrimitiveIds : register(t1); SamplerState gridSampler : register(s0); static const float PI = 3.14159265359; @@ -21,6 +22,12 @@ static const float GRID_WEATHER_WORLD_SCALE = 42.0; static const float GRID_LINE_WORLD_CELL = 2.0; static const int PLANET_COUNT = 5; 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; +static const int FROXEL_SLOT_COUNT = 2; +static const float FROXEL_MIN_Z = -2.0; +static const float FROXEL_MAX_Z = 6.0; struct VertexOut { @@ -275,26 +282,67 @@ float displacedSphereSdf(float3 p, float3 center, float radius, int index, bool return lerp(conservativeDistance, actualDistance, blend); } +float primitiveSdf(float3 p, int primitiveId, out int materialId) +{ + if (primitiveId == 0) + { + materialId = 2; + return displacedSphereSdf(p, SUN_POSITION, SUN_RADIUS, 17, true); + } + + int planetIndex = primitiveId - 1; + materialId = 3; + return displacedSphereSdf(p, planetCenter(planetIndex), planetRadius(planetIndex), planetIndex, false); +} + +int froxelIndexForPosition(float3 p) +{ + float2 xy = gridUv(p.xy); + float z = (p.z - FROXEL_MIN_Z) / (FROXEL_MAX_Z - FROXEL_MIN_Z); + + if (xy.x < 0.0 || xy.x >= 1.0 || xy.y < 0.0 || xy.y >= 1.0 || z < 0.0 || z >= 1.0) + { + return -1; + } + + int3 cell = int3( + min((int)(xy.x * FROXEL_COUNT_X), FROXEL_COUNT_X - 1), + min((int)(xy.y * FROXEL_COUNT_Y), FROXEL_COUNT_Y - 1), + min((int)(z * FROXEL_COUNT_Z), FROXEL_COUNT_Z - 1)); + + return cell.x + cell.y * FROXEL_COUNT_X + cell.z * FROXEL_COUNT_X * FROXEL_COUNT_Y; +} + float bodySdf(float3 p, out int materialId) { float distanceToScene = 100000.0; materialId = 0; - float sun = displacedSphereSdf(p, SUN_POSITION, SUN_RADIUS, 17, true); - if (sun < distanceToScene) + int froxelIndex = froxelIndexForPosition(p); + if (froxelIndex < 0) { - distanceToScene = sun; - materialId = 2; + return distanceToScene; } [unroll] - for (int i = 0; i < PLANET_COUNT; i++) + for (int slotGroup = 0; slotGroup < FROXEL_SLOT_COUNT; slotGroup++) { - float planet = displacedSphereSdf(p, planetCenter(i), planetRadius(i), i, false); - if (planet < distanceToScene) + int4 ids = froxelPrimitiveIds[froxelIndex * FROXEL_SLOT_COUNT + slotGroup]; + + [unroll] + for (int slot = 0; slot < 4; slot++) { - distanceToScene = planet; - materialId = 3; + int primitiveId = ids[slot]; + if (primitiveId >= 0) + { + int primitiveMaterialId; + float primitiveDistance = primitiveSdf(p, primitiveId, primitiveMaterialId); + if (primitiveDistance < distanceToScene) + { + distanceToScene = primitiveDistance; + materialId = primitiveMaterialId; + } + } } }