Emit TubeField indirect draw packets

This commit is contained in:
Meta Vens 2026-05-26 20:58:42 +01:00
commit 7564e46732
4 changed files with 79 additions and 11 deletions

View file

@ -41,6 +41,9 @@ public sealed class D3D12Renderer : IAquariumRenderer
private const int MaxTubeFieldSegments = 65_536;
private const int MaxTubeFieldVertices = MaxTubeFieldSegments * 4;
private const int MaxTubeFieldIndices = MaxTubeFieldSegments * 6;
private const int MaxTubeFieldDrawBatches = 4_096;
private const int TubeFieldDrawArgumentUIntCount = 5;
private const int TubeFieldDrawArgumentBytes = TubeFieldDrawArgumentUIntCount * sizeof(uint);
private const float SurfaceTransparentMinZ = -1.85f;
private const float SurfaceTransparentMaxZ = 0.45f;
private const int BloomLevelCount = 3;
@ -92,6 +95,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
private const int RootTubeFieldVertices = 2;
private const int RootTubeFieldIndices = 3;
private const int RootTubeFieldStats = 4;
private const int RootTubeFieldDrawArguments = 5;
private const int RootTubeFieldRenderFrameConstants = 0;
private const int RootTubeFieldRenderConstants = 1;
private const int RootTubeFieldRenderSource = 2;
@ -139,6 +143,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
private readonly ID3D12RootSignature fractalReservoirRootSignature;
private readonly ID3D12RootSignature tubeFieldRootSignature;
private readonly ID3D12RootSignature tubeFieldRenderRootSignature;
private readonly ID3D12CommandSignature tubeFieldDrawCommandSignature;
private readonly D3D12BlueNoiseTexture blueNoiseTexture;
private D3D12FieldTexture2D? tubeFieldFallbackRampTexture;
private ID3D12PipelineState? heightFieldBasePipelineState;
@ -210,6 +215,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
private readonly D3D12StructuredBuffer tubeFieldVertexBuffer;
private readonly D3D12StructuredBuffer tubeFieldIndexBuffer;
private readonly D3D12StructuredBuffer tubeFieldStatsBuffer;
private readonly D3D12StructuredBuffer tubeFieldDrawArgumentBuffer;
private readonly List<D3D12TubeFieldDrawBatch> tubeFieldDrawBatches = [];
private readonly Dictionary<string, D3D12ExternalSensorTexture> externalSensorTextures = new(StringComparer.Ordinal);
private D3D12TrackedResource? programOutputTexture;
@ -247,6 +253,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
private int activeTubeFieldDispatchedSegments;
private int activeTubeFieldRequestedSegments;
private int activeTubeFieldTruncatedSegments;
private int activeTubeFieldIndirectDrawCount;
private int activeTubeFieldSkippedDrawBatches;
private Viewport viewport;
private RawRect scissorRect;
private int width;
@ -363,6 +371,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
tubeFieldVertexBuffer = new D3D12StructuredBuffer(device, MaxTubeFieldVertices, Marshal.SizeOf<D3D12TubeFieldVertex>(), "Aquarium D3D12 TubeField Vertex Buffer", allowUnorderedAccess: true);
tubeFieldIndexBuffer = new D3D12StructuredBuffer(device, MaxTubeFieldIndices, Marshal.SizeOf<uint>(), "Aquarium D3D12 TubeField Index Buffer", allowUnorderedAccess: true);
tubeFieldStatsBuffer = new D3D12StructuredBuffer(device, 4, Marshal.SizeOf<uint>(), "Aquarium D3D12 TubeField Stats Buffer", allowUnorderedAccess: true);
tubeFieldDrawArgumentBuffer = new D3D12StructuredBuffer(device, MaxTubeFieldDrawBatches * TubeFieldDrawArgumentUIntCount, Marshal.SizeOf<uint>(), "Aquarium D3D12 TubeField Indirect Draw Arguments", allowUnorderedAccess: true);
resourceRegistry.Add("sdf-light-buffer", sdfLightBuffer);
resourceRegistry.Add("sdf-object-buffer", sdfObjectBuffer);
resourceRegistry.Add("gpu-sensor-camera-buffer", gpuSensorCameraBuffer);
@ -373,6 +382,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
resourceRegistry.Add("tube-field-vertex-buffer", tubeFieldVertexBuffer);
resourceRegistry.Add("tube-field-index-buffer", tubeFieldIndexBuffer);
resourceRegistry.Add("tube-field-stats-buffer", tubeFieldStatsBuffer);
resourceRegistry.Add("tube-field-indirect-draw-arguments", tubeFieldDrawArgumentBuffer);
commandList = device.CreateCommandList<ID3D12GraphicsCommandList>(0, CommandListType.Direct, frames[frameIndex].CommandAllocator, null);
commandList.Name = "Aquarium D3D12 Graphics Command List";
commandList.Close();
@ -399,6 +409,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
tubeFieldRootSignature.Name = "Aquarium D3D12 TubeField Root Signature";
tubeFieldRenderRootSignature = CreateTubeFieldRenderRootSignature();
tubeFieldRenderRootSignature.Name = "Aquarium D3D12 TubeField Render Root Signature";
tubeFieldDrawCommandSignature = CreateTubeFieldDrawCommandSignature();
tubeFieldDrawCommandSignature.Name = "Aquarium D3D12 TubeField Draw Command Signature";
CaptureShaderWriteTimes();
StartPipelineBuild("initial");
viewport = new Viewport(0.0f, 0.0f, width, height);
@ -973,6 +985,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
fractalReservoirRootSignature.Dispose();
tubeFieldRootSignature.Dispose();
tubeFieldRenderRootSignature.Dispose();
tubeFieldDrawCommandSignature.Dispose();
blueNoiseTexture.Dispose();
tubeFieldFallbackRampTexture?.Dispose();
studioIrradianceTexture.Dispose();
@ -982,6 +995,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
bufferFieldTextureSplineProgramBuffer?.Dispose();
bufferFieldTextureSampleBuffer?.Dispose();
tubeFieldStatsBuffer.Dispose();
tubeFieldDrawArgumentBuffer.Dispose();
tubeFieldIndexBuffer.Dispose();
tubeFieldVertexBuffer.Dispose();
temporalGaussianBuffer.Dispose();
@ -1832,6 +1846,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
tubeFieldVertexBuffer.Transition(activeCommandList, ResourceStates.VertexAndConstantBuffer);
tubeFieldIndexBuffer.Transition(activeCommandList, ResourceStates.IndexBuffer);
tubeFieldDrawArgumentBuffer.Transition(activeCommandList, ResourceStates.IndirectArgument);
var vertexView = new VertexBufferView(
tubeFieldVertexBuffer.Resource.GPUVirtualAddress,
(uint)(MaxTubeFieldVertices * Marshal.SizeOf<D3D12TubeFieldVertex>()),
@ -1856,7 +1871,13 @@ public sealed class D3D12Renderer : IAquariumRenderer
activeCommandList.SetGraphicsRootConstantBufferView(RootTubeFieldRenderConstants, batch.ConstantsGpuVirtualAddress);
activeCommandList.SetGraphicsRootShaderResourceView(RootTubeFieldRenderSource, batch.Source.Resource.GPUVirtualAddress);
activeCommandList.SetGraphicsRootDescriptorTable(RootTubeFieldRenderRamp, rampDescriptor.Gpu);
activeCommandList.DrawIndexedInstanced((uint)batch.IndexCount, 1, (uint)batch.StartIndex, 0, 0);
activeCommandList.ExecuteIndirect(
tubeFieldDrawCommandSignature,
1,
tubeFieldDrawArgumentBuffer.Resource,
(ulong)batch.DrawArgumentOffsetBytes,
null,
0);
}
}
@ -2294,6 +2315,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
activeTubeFieldDispatchedSegments = 0;
activeTubeFieldRequestedSegments = 0;
activeTubeFieldTruncatedSegments = 0;
activeTubeFieldIndirectDrawCount = 0;
activeTubeFieldSkippedDrawBatches = 0;
tubeFieldDrawBatches.Clear();
if (tubeFieldComputePipelineState is null || activeFieldEvidenceFrame.TubeSplineLowerings.Count == 0)
{
@ -2309,6 +2332,12 @@ public sealed class D3D12Renderer : IAquariumRenderer
continue;
}
if (tubeFieldDrawBatches.Count >= MaxTubeFieldDrawBatches)
{
activeTubeFieldSkippedDrawBatches++;
continue;
}
var normalized = lowering.Normalized();
var requestedSegments = checked(Math.Max(0, normalized.Width - 1) * Math.Max(1, normalized.CatmullRomSubdivisions) * Math.Max(1, normalized.ColumnCount));
activeTubeFieldRequestedSegments += requestedSegments;
@ -2326,12 +2355,14 @@ public sealed class D3D12Renderer : IAquariumRenderer
}
activeTubeFieldTruncatedSegments += requestedSegments - dispatchSegments;
var startIndex = segmentBase * 6;
var constants = new D3D12TubeFieldConstants(
new Vector4(normalized.Width, normalized.Height, normalized.StrideBytes, normalized.FirstColumn),
new Vector4(normalized.ColumnCount, normalized.ColumnStride, normalized.RollingModulo, normalized.RollingOffset),
new Vector4(normalized.AmplitudePower, normalized.AmplitudeScale, normalized.NormalizeMin, normalized.NormalizeMax),
new Vector4(normalized.BaseRadius, normalized.RadiusScale, normalized.Alpha, normalized.Feather),
new Vector4(normalized.EmissionScale, normalized.CatmullRomSubdivisions, segmentBase, dispatchSegments),
new Vector4(tubeFieldDrawBatches.Count * TubeFieldDrawArgumentUIntCount, startIndex, 0.0f, 0.0f),
normalized.Origin,
0.0f,
normalized.AxisStep,
@ -2339,12 +2370,12 @@ public sealed class D3D12Renderer : IAquariumRenderer
normalized.ColumnStep,
0.0f);
var constantsUpload = frameResources.UploadRing.WriteConstant(constants);
var startIndex = segmentBase * 6;
sourceBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource);
tubeFieldVertexBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
tubeFieldIndexBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
tubeFieldStatsBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
tubeFieldDrawArgumentBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
activeCommandList.SetComputeRootSignature(tubeFieldRootSignature);
activeCommandList.SetPipelineState(tubeFieldComputePipelineState);
activeCommandList.SetComputeRootConstantBufferView(RootTubeFieldConstants, constantsUpload.GpuVirtualAddress);
@ -2352,19 +2383,21 @@ public sealed class D3D12Renderer : IAquariumRenderer
activeCommandList.SetComputeRootUnorderedAccessView(RootTubeFieldVertices, tubeFieldVertexBuffer.Resource.GPUVirtualAddress);
activeCommandList.SetComputeRootUnorderedAccessView(RootTubeFieldIndices, tubeFieldIndexBuffer.Resource.GPUVirtualAddress);
activeCommandList.SetComputeRootUnorderedAccessView(RootTubeFieldStats, tubeFieldStatsBuffer.Resource.GPUVirtualAddress);
activeCommandList.SetComputeRootUnorderedAccessView(RootTubeFieldDrawArguments, tubeFieldDrawArgumentBuffer.Resource.GPUVirtualAddress);
activeCommandList.Dispatch((uint)((dispatchSegments + 255) / 256), 1, 1);
activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(tubeFieldVertexBuffer.Resource));
activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(tubeFieldIndexBuffer.Resource));
activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(tubeFieldStatsBuffer.Resource));
activeCommandList.ResourceBarrier(ResourceBarrier.BarrierUnorderedAccessView(tubeFieldDrawArgumentBuffer.Resource));
segmentBase += dispatchSegments;
activeTubeFieldDispatchedSegments += dispatchSegments;
activeTubeFieldIndirectDrawCount++;
tubeFieldDrawBatches.Add(new D3D12TubeFieldDrawBatch(
sourceBuffer,
constantsUpload.GpuVirtualAddress,
normalized.RampResourceKey,
startIndex,
dispatchSegments * 6));
tubeFieldDrawBatches.Count * TubeFieldDrawArgumentBytes));
}
activeTubeFieldDrawIndexCount = activeTubeFieldDispatchedSegments * 6;
@ -3013,6 +3046,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
$"segments requested {activeTubeFieldRequestedSegments:N0}; " +
$"dispatched {activeTubeFieldDispatchedSegments:N0}; " +
$"truncated {activeTubeFieldTruncatedSegments:N0}; " +
$"indirect draws {activeTubeFieldIndirectDrawCount:N0}; " +
$"skipped draw batches {activeTubeFieldSkippedDrawBatches:N0}; " +
$"index draw count {activeTubeFieldDrawIndexCount:N0}; " +
$"budget {MaxTubeFieldSegments:N0}");
}
@ -3574,6 +3609,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(10, 0), ShaderVisibility.All),
new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(11, 0), ShaderVisibility.All),
new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(12, 0), ShaderVisibility.All),
new RootParameter(RootParameterType.UnorderedAccessView, new RootDescriptor(13, 0), ShaderVisibility.All),
};
var description = new RootSignatureDescription(
RootSignatureFlags.None,
@ -3582,6 +3618,23 @@ public sealed class D3D12Renderer : IAquariumRenderer
return device.CreateRootSignature(0, in description, RootSignatureVersion.Version1);
}
private ID3D12CommandSignature CreateTubeFieldDrawCommandSignature()
{
var arguments = new[]
{
new IndirectArgumentDescription
{
Type = IndirectArgumentType.DrawIndexed,
},
};
var description = new CommandSignatureDescription
{
ByteStride = TubeFieldDrawArgumentBytes,
IndirectArguments = arguments,
};
return device.CreateCommandSignature<ID3D12CommandSignature>(description, null);
}
private ID3D12RootSignature CreateTubeFieldRenderRootSignature()
{
var frameConstants = new DescriptorRange(
@ -4071,8 +4124,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
D3D12StructuredBuffer Source,
ulong ConstantsGpuVirtualAddress,
string RampResourceKey,
int StartIndex,
int IndexCount);
int DrawArgumentOffsetBytes);
[StructLayout(LayoutKind.Sequential)]
private readonly record struct D3D12TubeFieldConstants(
@ -4081,6 +4133,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
Vector4 Amplitude,
Vector4 Material,
Vector4 Dispatch,
Vector4 Draw,
Vector3 Origin,
float Padding0,
Vector3 AxisStep,

View file

@ -48,6 +48,7 @@ cbuffer TubeFieldConstants : register(b3)
float4 tubeAmplitude;
float4 tubeMaterial;
float4 tubeDispatch;
float4 tubeDraw;
float3 tubeOrigin;
float tubePadding0;
float3 tubeAxisStep;
@ -62,6 +63,7 @@ SamplerState TubeFieldRampSampler : register(s0);
RWStructuredBuffer<TubeFieldVertex> TubeFieldVertices : register(u10);
RWStructuredBuffer<uint> TubeFieldIndices : register(u11);
RWStructuredBuffer<uint> TubeFieldStats : register(u12);
RWStructuredBuffer<uint> TubeFieldDrawArguments : register(u13);
uint PositiveModulo(int value, uint modulo)
{
@ -167,6 +169,16 @@ void D3D12TubeFieldExpandCS(uint3 dispatchThreadId : SV_DispatchThreadID)
{
uint localSegment = dispatchThreadId.x;
uint dispatchSegments = (uint)round(tubeDispatch.w);
if (localSegment == 0u)
{
uint argumentBase = (uint)round(tubeDraw.x);
TubeFieldDrawArguments[argumentBase + 0u] = dispatchSegments * 6u;
TubeFieldDrawArguments[argumentBase + 1u] = 1u;
TubeFieldDrawArguments[argumentBase + 2u] = (uint)round(tubeDraw.y);
TubeFieldDrawArguments[argumentBase + 3u] = 0u;
TubeFieldDrawArguments[argumentBase + 4u] = 0u;
}
if (localSegment >= dispatchSegments)
{
return;

View file

@ -238,3 +238,4 @@
{"lesson":"D3D12 TubeField now has a first GPU-only rendering path: resolved resource-backed 2D rolling float buffers bind as read-only GPU memory, a compute shader expands endpoint-clamped Catmull-Rom tube envelopes into fixed-budget vertex/index UAVs, byte-address sample loads honor declared stride, the existing spline SDF pipeline draws opaque tube field geometry, and capacity logs report requested/dispatched/truncated segment counts. This proves shader-side expansion/binding without CPU tessellation; per-pixel source-row material sampling, ramp texture binding, and indirect draw remain the next cuts.","timestamp":"2026-05-26T20:11:00+01:00","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12TubeField.hlsl","src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs","state/map.yaml"],"source":"continued GPU-resident TubeField implementation","topic":"d3d12-tubefield-compute-expansion"}
{"lesson":"TubeField rendering now has a dedicated D3D12 render pipeline rather than reusing generic spline material. Compute expansion records one draw batch per lowering, and the TubeField pixel shader binds the matching source buffer/constants and resamples the rolling buffer per pixel by column+spline coordinate with the cheap 5-tap path. The initial material is opaque field geometry with fallback black-to-warm ramp, squared normalized emission, and fake Fresnel center-bright/edge-dark shaping. Real ramp texture binding, indirect draw packets, and non-buffer resolvers remain open. Headless dev reload, Aquarium.Engine.Tests, Mimir build, and both Mimir Fensalir field smokes passed.","timestamp":"2026-05-26T20:22:00+01:00","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12TubeField.hlsl","state/map.yaml"],"source":"continued GPU-resident TubeField material implementation","topic":"d3d12-tubefield-pixel-material"}
{"lesson":"Texture2D local assets are now first-class field resources. AquariumFieldResourceDeclaration carries SourceUri and has a LocalTexture2D factory; the field DSL accepts `texture2d id=... key=... path=...` and TubeField `ramp=...` resolves aliases into RampResourceKey. D3D12FieldResourceRegistry lazily uploads local Rgba8 assets into GPU-resident textures, TubeField rendering binds the ramp SRV plus linear clamp sampler, and unresolved ramps use a Fensalir-owned 0-1 fallback texture. This replaces raw ramp paths as live authority while keeping them as compatibility metadata. Fensalir build/tests/headless reload and Mimir build/field smokes passed.","timestamp":"2026-05-26T20:40:00+01:00","files":["src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs","src/Aquarium.Engine.Contracts/Render/AquariumFieldScriptCompiler.cs","src/Aquarium.Engine/Render/D3D12FieldTexture2D.cs","src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12TubeField.hlsl","tests/Aquarium.Engine.Tests/FieldScriptCompilerTests.cs","state/map.yaml"],"source":"user requested Texture2D local asset resource declaration and automatic DSL binding","topic":"field-texture2d-resource-binding"}
{"lesson":"TubeField draw dimensions are now emitted as GPU-resident indirect draw packets. The expansion compute shader writes one D3D12 DrawIndexedArguments packet per resolved TubeField batch into a UAV argument buffer; render binds the matching source/ramp material state and calls ExecuteIndirect instead of CPU-manufacturing DrawIndexedInstanced counts. CPU still schedules one indirect call per material/resource batch because each batch has distinct root bindings, but draw count/start index authority now lives in the GPU expansion output. Fensalir build/tests/headless reload and Mimir build/field smokes passed.","timestamp":"2026-05-26T20:59:00+01:00","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12TubeField.hlsl","state/map.yaml"],"source":"continued migration plan after Texture2D resolver cut","topic":"d3d12-tubefield-indirect-draw-packets"}

View file

@ -91,11 +91,13 @@ next_actions:
5-tap approximation. Texture2D local assets are now declared as first-class
field resources with source URI metadata, DSL aliases, lazy D3D12 upload,
SRV binding, and TubeField ramp lookup by resource key; missing/unresolved
ramps bind a Fensalir-owned linear fallback ramp. The first material is
opaque field geometry with squared normalized emission, fake Fresnel
center-bright/edge-dark shaping, and requested/dispatched/truncated segment
reporting. Remaining cuts are indirect draw packets plus mesh/surface-page/
volume resolvers.
ramps bind a Fensalir-owned linear fallback ramp. TubeField compute now
emits one GPU-resident D3D12 DrawIndexed indirect argument packet per
material/resource batch, and render binds the batch source/ramp state then
executes the packet with ExecuteIndirect. The first material is opaque field
geometry with squared normalized emission, fake Fresnel center-bright/
edge-dark shaping, and requested/dispatched/truncated/indirect-draw segment
reporting. Remaining cuts are mesh/surface-page/volume resolvers.
- Move any remaining client-only Epiphany state found here back to `E:\Projects\Aquarium-Engine`.
- Keep Mimir app/adapters out of this repo; Fensalir may expose generic sensor-fusion contracts only.
- Package contracts/host deliberately before replacing sibling source references in client repos.