From b5b358245f349a0c3ac1fbfd95bfbfd6948589cd Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 27 May 2026 12:22:15 +0100 Subject: [PATCH] Gate TubeField dispatch by valid resource slots --- .../Render/D3D12FieldResourceRegistry.cs | 67 +++++++++++++++++++ src/Aquarium.Engine/Render/D3D12Renderer.cs | 14 +++- state/evidence.jsonl | 1 + state/map.yaml | 6 +- 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs b/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs index d473033..e2037a3 100644 --- a/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs +++ b/src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs @@ -189,6 +189,60 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable return false; } + public void MarkStructuredBufferUpload(string resourceKey, int elementOffset, int elementCount) + { + if (!structuredBuffers.TryGetValue(resourceKey, out var slot) || + elementOffset < 0 || + elementCount <= 0) + { + return; + } + + var firstFullColumn = (elementOffset + slot.Width - 1) / slot.Width; + var endFullColumn = (elementOffset + elementCount) / slot.Width; + if (endFullColumn <= firstFullColumn) + { + return; + } + + var validColumns = slot.ValidColumns; + for (var column = Math.Max(0, firstFullColumn); column < Math.Min(validColumns.Length, endFullColumn); column++) + { + validColumns[column] = true; + } + } + + public int CountContiguousValidColumns(AquariumFieldTubeSplineLowering lowering) + { + if (!structuredBuffers.TryGetValue(lowering.ResourceKey, out var slot)) + { + return 0; + } + + var normalized = lowering.Normalized(); + var validCount = 0; + for (var logicalColumn = 0; logicalColumn < normalized.ColumnCount; logicalColumn++) + { + var physicalColumn = normalized.FirstColumn + logicalColumn * normalized.ColumnStride; + if (normalized.RollingModulo > 0) + { + physicalColumn = PositiveModulo(physicalColumn + normalized.RollingOffset, normalized.RollingModulo); + } + + physicalColumn = Math.Min(physicalColumn, slot.ValidColumns.Length - 1); + if (physicalColumn < 0 || + physicalColumn >= slot.ValidColumns.Length || + !slot.ValidColumns[physicalColumn]) + { + break; + } + + validCount++; + } + + return validCount; + } + public bool TryGetTexture2D(string resourceKey, out D3D12FieldTexture2D texture) { if (textures.TryGetValue(resourceKey, out var slot)) @@ -384,9 +438,12 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable var allowUnorderedAccess = declaration.Access == AquariumFieldShaderAccess.UnorderedAccess; var hasExisting = structuredBuffers.TryGetValue(declaration.ResourceKey, out var existing); + var width = Math.Max(1, declaration.Width); + var columnCount = Math.Max(1, (elementCount + width - 1) / width); if (hasExisting && existing is not null && existing.ElementCount == elementCount && existing.StrideBytes == strideBytes && + existing.Width == width && existing.AllowUnorderedAccess == allowUnorderedAccess) { structuredBuffers[declaration.ResourceKey] = existing with { Version = declaration.Version }; @@ -418,6 +475,8 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable structuredBuffers[declaration.ResourceKey] = new StructuredBufferSlot( buffer, elementCount, + width, + new bool[columnCount], strideBytes, allowUnorderedAccess, declaration.Version); @@ -625,6 +684,12 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable private static string RegistryKey(string resourceKey) => RegistryPrefix + resourceKey; + private static int PositiveModulo(int value, int modulo) + { + var remainder = value % modulo; + return remainder < 0 ? remainder + modulo : remainder; + } + private static D3D12StructuredBuffer? OpenSharedStructuredBuffer( ID3D12Device device, AquariumFieldResourceDeclaration declaration, @@ -659,6 +724,8 @@ internal sealed class D3D12FieldResourceRegistry : IDisposable private sealed record StructuredBufferSlot( D3D12StructuredBuffer Buffer, int ElementCount, + int Width, + bool[] ValidColumns, int StrideBytes, bool AllowUnorderedAccess, ulong Version); diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 43b3171..5ba2a80 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -256,6 +256,7 @@ public sealed class D3D12Renderer : IAquariumRenderer private int activeTubeFieldIndirectDrawCount; private int activeTubeFieldSkippedDrawBatches; private int activeTubeFieldUnplannedLowerings; + private int activeTubeFieldInvalidColumns; private int activeFieldResourceUploadCount; private int activeFieldResourceUploadSkippedCount; private Viewport viewport; @@ -553,7 +554,7 @@ public sealed class D3D12Renderer : IAquariumRenderer $"candidates {activeFieldEvidenceFrame.Candidates.Count} / packets {activeFieldEvidenceFrame.BackendPackets.Count} / " + $"planned {activeFieldLoweringPlan.Packets.Count} / deferred {activeFieldLoweringPlan.DeferredRequests.Count} / errors {errorCount}" + (activeFieldEvidenceFrame.TubeSplineLowerings.Count > 0 - ? $" / tube requested {activeTubeFieldRequestedSegments} dispatched {activeTubeFieldDispatchedSegments} truncated {activeTubeFieldTruncatedSegments} uploads {activeFieldResourceUploadCount} skipped {activeFieldResourceUploadSkippedCount} unplanned {activeTubeFieldUnplannedLowerings}" + ? $" / tube requested {activeTubeFieldRequestedSegments} dispatched {activeTubeFieldDispatchedSegments} truncated {activeTubeFieldTruncatedSegments} invalid {activeTubeFieldInvalidColumns} uploads {activeFieldResourceUploadCount} skipped {activeFieldResourceUploadSkippedCount} unplanned {activeTubeFieldUnplannedLowerings}" : string.Empty); } @@ -2344,6 +2345,7 @@ public sealed class D3D12Renderer : IAquariumRenderer activeTubeFieldIndirectDrawCount = 0; activeTubeFieldSkippedDrawBatches = 0; activeTubeFieldUnplannedLowerings = 0; + activeTubeFieldInvalidColumns = 0; activeFieldResourceUploadCount = 0; activeFieldResourceUploadSkippedCount = 0; tubeFieldDrawBatches.Clear(); @@ -2383,6 +2385,14 @@ public sealed class D3D12Renderer : IAquariumRenderer } var normalized = lowering.Normalized(); + var validColumnCount = fieldResourceRegistry.CountContiguousValidColumns(normalized); + activeTubeFieldInvalidColumns += Math.Max(0, normalized.ColumnCount - validColumnCount); + if (validColumnCount <= 0) + { + continue; + } + + normalized = normalized with { ColumnCount = validColumnCount }; var requestedSegments = checked(Math.Max(0, normalized.Width - 1) * Math.Max(1, normalized.CatmullRomSubdivisions) * Math.Max(1, normalized.ColumnCount)); activeTubeFieldRequestedSegments += requestedSegments; var remainingSegments = MaxTubeFieldSegments - segmentBase; @@ -2485,6 +2495,7 @@ public sealed class D3D12Renderer : IAquariumRenderer } activeFieldResourceUploadCount++; + fieldResourceRegistry.MarkStructuredBufferUpload(upload.ResourceKey, upload.ElementOffset, upload.Float32Data.Count); } } @@ -3137,6 +3148,7 @@ public sealed class D3D12Renderer : IAquariumRenderer $"indirect draws {activeTubeFieldIndirectDrawCount:N0}; " + $"skipped draw batches {activeTubeFieldSkippedDrawBatches:N0}; " + $"unplanned lowerings {activeTubeFieldUnplannedLowerings:N0}; " + + $"invalid columns {activeTubeFieldInvalidColumns:N0}; " + $"resource uploads {activeFieldResourceUploadCount:N0}; " + $"skipped uploads {activeFieldResourceUploadSkippedCount:N0}; " + $"index draw count {activeTubeFieldDrawIndexCount:N0}; " + diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 7ddce88..a828d5d 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -253,3 +253,4 @@ {"lesson":"Structured buffer declaration version is content freshness, not shape authority. D3D12FieldResourceRegistry now reuses an existing structured buffer when element count, stride, and access are unchanged, updating the recorded version instead of reallocating GPU memory every frame. This fixed the rolling Mimir spectrum path, where increasing evidence versions and history updates were able to churn the D3D12 resource and trigger DXGI device removal during a longer headless capture. Fensalir build, Aquarium.Engine.Tests, Mimir build/field smokes, and long Mimir headless capture passed after the fix.","timestamp":"2026-05-27T11:37:00+01:00","files":["src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs","state/map.yaml"],"source":"rolling Mimir spectrum TubeField verification","topic":"structured-buffer-version-reuse"} {"lesson":"Fensalir's debug field-evidence summary now exposes TubeField requested, dispatched, truncated, resource-upload, skipped-upload, and unplanned-lowering counters. Mimir's source-lane and subdivision budgets can be tuned from live renderer feedback instead of inferred from planner counts alone. Fensalir build and Aquarium.Engine.Tests passed.","timestamp":"2026-05-27T11:52:00+01:00","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","state/map.yaml"],"source":"Mimir fixed source-lane budget follow-up","topic":"tubefield-budget-debug-summary"} {"lesson":"Field resource Float32 uploads now carry an explicit element offset. Validation rejects writes whose offset plus length exceeds declared resource capacity, and D3D12StructuredBuffer copies partial uploads into that destination offset. This lets clients update sampled ring-buffer slots without uploading the entire fixed-capacity GPU resource every frame. Fensalir build, Aquarium.Engine.Tests, Mimir build/smokes, and Mimir headless render passed.","timestamp":"2026-05-27T12:00:00+01:00","files":["src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs","src/Aquarium.Engine/Render/D3D12StructuredBuffer.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","tests/Aquarium.Engine.Tests/FieldEvidenceContractTests.cs","state/map.yaml"],"source":"Mimir spectrum ring upload copy reduction","topic":"field-resource-upload-offsets"} +{"lesson":"Fensalir now owns rolling structured-buffer column validity for TubeField resources. Successful Float32 resource uploads mark fully covered columns valid beside the resolved structured buffer, new allocations start invalid, and TubeField dispatch clamps each lowering to the contiguous valid logical history range before compute expansion. Mimir can upload only the newest spectrum ring slot while Fensalir prevents stale or never-uploaded older slots from becoming shader-sampled geometry. Fensalir build/tests, Mimir build/smokes, and headless Mimir render passed.","timestamp":"2026-05-27T12:20:00+01:00","files":["src/Aquarium.Engine/Render/D3D12FieldResourceRegistry.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","state/map.yaml"],"source":"option 4 rolling resource validity decision","topic":"tubefield-rolling-slot-validity"} diff --git a/state/map.yaml b/state/map.yaml index ad937e0..80aac2f 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -138,7 +138,11 @@ next_actions: structured buffers before TubeField compute reads them, so a Fensalir-owned GPU resource can be live data rather than paperwork. Resource uploads carry an explicit element offset, validation rejects out-of-range writes, and - D3D12 copies partial uploads into the declared buffer offset. Structured buffer + D3D12 copies partial uploads into the declared buffer offset. Fensalir now + tracks uploaded structured-buffer columns beside the resolved resource and + clamps TubeField dispatch to the contiguous valid logical history range, so + stale or never-uploaded rolling slots cannot reach the TubeField shader as + geometry after allocation, reset, or partial update. Structured buffer declaration version is content freshness, not buffer-shape authority: D3D12FieldResourceRegistry now reuses an existing structured buffer when element count, stride, and access are unchanged, updating the recorded