Support offset field resource uploads

This commit is contained in:
Meta Vens 2026-05-27 12:01:21 +01:00
commit af6da11f07
6 changed files with 55 additions and 9 deletions

View file

@ -308,10 +308,13 @@ public sealed class AquariumFieldResourceUpload
public ulong Version { get; init; }
public int ElementOffset { get; init; }
public IReadOnlyList<float> Float32Data { get; init; } = [];
public bool IsValid =>
!string.IsNullOrWhiteSpace(ResourceKey) &&
ElementOffset >= 0 &&
Float32Data.Count > 0;
}
@ -703,9 +706,10 @@ public static class AquariumFieldEvidenceValidator
}
var capacity = Math.Max(1, resource.DepthOrCount > 0 ? resource.DepthOrCount : resource.Width);
if (upload.Float32Data.Count > capacity)
if (upload.ElementOffset >= capacity ||
upload.Float32Data.Count > capacity - upload.ElementOffset)
{
issues.Add(Error(upload.ResourceKey, $"Field resource upload has {upload.Float32Data.Count} Float32 values but resource capacity is {capacity}."));
issues.Add(Error(upload.ResourceKey, $"Field resource upload writes {upload.Float32Data.Count} Float32 values at offset {upload.ElementOffset}, exceeding resource capacity {capacity}."));
}
}
}

View file

@ -2477,11 +2477,11 @@ public sealed class D3D12Renderer : IAquariumRenderer
if (upload.Float32Data is float[] array)
{
buffer.UploadPartial<float>(activeCommandList, frameResources.UploadRing, array);
buffer.UploadPartial<float>(activeCommandList, frameResources.UploadRing, array, upload.ElementOffset);
}
else
{
buffer.UploadPartial<float>(activeCommandList, frameResources.UploadRing, upload.Float32Data.ToArray());
buffer.UploadPartial<float>(activeCommandList, frameResources.UploadRing, upload.Float32Data.ToArray(), upload.ElementOffset);
}
activeFieldResourceUploadCount++;

View file

@ -69,12 +69,18 @@ internal sealed class D3D12StructuredBuffer : IDisposable
Transition(commandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource);
}
public void UploadPartial<T>(ID3D12GraphicsCommandList commandList, D3D12UploadRing uploadRing, ReadOnlySpan<T> values)
public void UploadPartial<T>(
ID3D12GraphicsCommandList commandList,
D3D12UploadRing uploadRing,
ReadOnlySpan<T> values,
int destinationElementOffset = 0)
where T : unmanaged
{
if (values.Length > elementCount)
ArgumentOutOfRangeException.ThrowIfNegative(destinationElementOffset);
if (destinationElementOffset > elementCount ||
values.Length > elementCount - destinationElementOffset)
{
throw new ArgumentException($"Structured buffer holds {elementCount} elements but received {values.Length}.", nameof(values));
throw new ArgumentException($"Structured buffer holds {elementCount} elements but upload writes {values.Length} elements at offset {destinationElementOffset}.", nameof(values));
}
var actualStride = Unsafe.SizeOf<T>();
@ -87,7 +93,8 @@ internal sealed class D3D12StructuredBuffer : IDisposable
Transition(commandList, ResourceStates.CopyDest);
if (upload.DataBytes > 0)
{
commandList.CopyBufferRegion(Resource, 0, uploadRing.Resource, (ulong)upload.OffsetBytes, (ulong)upload.DataBytes);
var destinationOffsetBytes = checked(destinationElementOffset * strideBytes);
commandList.CopyBufferRegion(Resource, (ulong)destinationOffsetBytes, uploadRing.Resource, (ulong)upload.OffsetBytes, (ulong)upload.DataBytes);
}
Transition(commandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource);
}

View file

@ -252,3 +252,4 @@
{"lesson":"Field resource upload validation now checks upload shape, not only existence. Float32 uploads must target declared StructuredBuffer/CurvePointBuffer resources with Float32 format, 4-byte stride, and sufficient element capacity. Oversized or non-Float uploads are validation errors before renderer upload. Fensalir build, Aquarium.Engine.Tests (29 tests), and headless reload passed.","timestamp":"2026-05-26T23:39:00+01:00","files":["src/Aquarium.Engine.Contracts/Render/AquariumFieldEvidence.cs","tests/Aquarium.Engine.Tests/FieldEvidenceContractTests.cs"],"source":"tightened field resource upload contract","topic":"field-resource-upload-validation"}
{"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"}

View file

@ -136,7 +136,9 @@ next_actions:
Field evidence now carries explicit Float32 resource uploads separate from
resource declarations. D3D12 uploads those frame samples into resolved
structured buffers before TubeField compute reads them, so a Fensalir-owned
GPU resource can be live data rather than paperwork. Structured buffer
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
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

View file

@ -207,6 +207,7 @@ public sealed class FieldEvidenceContractTests
{
ResourceKey = frame.Resources[0].ResourceKey,
Version = frame.Resources[0].Version,
ElementOffset = 1,
Float32Data = [0.0f, 0.25f, 0.5f, 1.0f],
},
],
@ -224,6 +225,37 @@ public sealed class FieldEvidenceContractTests
Assert.False(report.HasErrors);
}
[Fact]
public void ValidatorRejectsOutOfRangeFieldResourceUploadOffsets()
{
var frame = BuildValidTubeEvidenceFrame();
frame = new AquariumFieldEvidenceFrame
{
Resources = [frame.Resources[0] with { DepthOrCount = 4, Width = 4, Format = "Float32", StrideBytes = 4 }],
ResourceUploads =
[
new AquariumFieldResourceUpload
{
ResourceKey = frame.Resources[0].ResourceKey,
ElementOffset = 3,
Float32Data = [0.0f, 0.5f],
},
],
Domains = frame.Domains,
Claims = frame.Claims,
Candidates = frame.Candidates,
BackendPackets = frame.BackendPackets,
TubeSplineLowerings = frame.TubeSplineLowerings,
AccumulationWindowSeconds = frame.AccumulationWindowSeconds,
PresentationDelaySeconds = frame.PresentationDelaySeconds,
};
var report = AquariumFieldEvidenceValidator.Validate(frame);
Assert.True(report.HasErrors);
Assert.Contains(report.Issues, issue => issue.Key == "mimir:resource:native-ring:asio-ch0");
}
[Fact]
public void ValidatorRejectsUnknownFieldResourceUploads()
{