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);
}