Upload raw frames into leased textures
This commit is contained in:
parent
cbde900146
commit
3e03ebdaec
8 changed files with 160 additions and 5 deletions
|
|
@ -25,4 +25,6 @@ internal sealed class AquariumNullFieldResourceBroker : IAquariumFieldResourceBr
|
|||
public AquariumFieldResourceLease LeaseTexture2D(AquariumTexture2DLeaseRequest request) => AquariumFieldResourceLease.Invalid;
|
||||
|
||||
public bool CommitLeaseVersion(string resourceKey, ulong version, ulong producerFenceValue) => false;
|
||||
|
||||
public bool UploadTexture2D(AquariumTexture2DUpload upload) => false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -525,7 +525,11 @@ public readonly record struct AquariumFieldResourceDeclaration(
|
|||
"R8Unorm" or "R8_UNorm" or "R8_UNORM" => 1,
|
||||
"R16Float" or "R16_Float" or "R16_FLOAT" => 2,
|
||||
"R32Float" or "R32_Float" or "R32_FLOAT" or "Float32" => 4,
|
||||
"Rg8" or "Rg8Unorm" or "R8G8_UNorm" or "R8G8_UNORM" or "Yuy2" or "YUY2" or "LeapStereoIr" or "LeapPackedMap" => 2,
|
||||
"Bayer8" or "Gray8" or "R8" => 1,
|
||||
"Bgra8" or "Bgra8Unorm" or "B8G8R8A8_UNorm" or "B8G8R8A8_UNORM" or "Rgba8Unorm" or "R8G8B8A8_UNorm" or "R8G8B8A8_UNORM" => 4,
|
||||
"Rgba16Float" or "R16G16B16A16_Float" or "R16G16B16A16_FLOAT" => 8,
|
||||
"Nv12" or "NV12" => 1,
|
||||
_ => 4,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ public interface IAquariumFieldResourceBroker
|
|||
AquariumFieldResourceLease LeaseTexture2D(AquariumTexture2DLeaseRequest request);
|
||||
|
||||
bool CommitLeaseVersion(string resourceKey, ulong version, ulong producerFenceValue);
|
||||
|
||||
bool UploadTexture2D(AquariumTexture2DUpload upload);
|
||||
}
|
||||
|
||||
public readonly record struct AquariumTexture2DLeaseRequest(
|
||||
|
|
@ -41,3 +43,21 @@ public readonly record struct AquariumFieldResourceLease(
|
|||
0,
|
||||
false);
|
||||
}
|
||||
|
||||
public readonly record struct AquariumTexture2DUpload(
|
||||
string ResourceKey,
|
||||
int Width,
|
||||
int Height,
|
||||
string Format,
|
||||
int SourceStrideBytes,
|
||||
ReadOnlyMemory<byte> Data,
|
||||
ulong Version = 0)
|
||||
{
|
||||
public bool IsValid =>
|
||||
!string.IsNullOrWhiteSpace(ResourceKey) &&
|
||||
Width > 0 &&
|
||||
Height > 0 &&
|
||||
SourceStrideBytes > 0 &&
|
||||
!string.IsNullOrWhiteSpace(Format) &&
|
||||
Data.Length >= checked(SourceStrideBytes * Height);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,12 +11,12 @@ internal static class D3D12FieldTextureFormat
|
|||
"Gray8" or "R8" or "R8Unorm" or "R8_UNorm" or "R8_UNORM" => Format.R8_UNorm,
|
||||
"R16Float" or "R16_Float" or "R16_FLOAT" => Format.R16_Float,
|
||||
"R32Float" or "R32_Float" or "R32_FLOAT" or "Float32" => Format.R32_Float,
|
||||
"Rg8" or "Rg8Unorm" or "R8G8_UNorm" or "R8G8_UNORM" => Format.R8G8_UNorm,
|
||||
"Rg8" or "Rg8Unorm" or "R8G8_UNorm" or "R8G8_UNORM" or "LeapStereoIr" or "LeapPackedMap" => Format.R8G8_UNorm,
|
||||
"Bgra8" or "Bgra8Unorm" or "B8G8R8A8_UNorm" or "B8G8R8A8_UNORM" => Format.B8G8R8A8_UNorm,
|
||||
"Rgba8Unorm" or "R8G8B8A8_UNorm" or "R8G8B8A8_UNORM" => Format.R8G8B8A8_UNorm,
|
||||
"Rgba16Float" or "R16G16B16A16_Float" or "R16G16B16A16_FLOAT" => Format.R16G16B16A16_Float,
|
||||
"Yuy2" or "YUY2" => Format.YUY2,
|
||||
"Nv12" or "NV12" => Format.NV12,
|
||||
"LeapStereoIr" or "LeapPackedMap" => Format.B8G8R8A8_UNorm,
|
||||
_ => Format.Unknown,
|
||||
};
|
||||
return format != Format.Unknown;
|
||||
|
|
|
|||
|
|
@ -198,6 +198,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private readonly D3D12RenderTarget[] bloomRenderTargets = new D3D12RenderTarget[BloomLevelCount];
|
||||
private readonly D3D12RenderTarget[] bloomScratchTargets = new D3D12RenderTarget[BloomLevelCount];
|
||||
private readonly Dictionary<string, SharedTextureLeaseSlot> sharedTextureLeases = new(StringComparer.Ordinal);
|
||||
private readonly object sharedTextureUploadLock = new();
|
||||
private readonly D3D12StructuredBuffer sdfLightBuffer;
|
||||
private readonly D3D12StructuredBuffer sdfObjectBuffer;
|
||||
private readonly D3D12StructuredBuffer gpuSensorCameraBuffer;
|
||||
|
|
@ -730,6 +731,24 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
return true;
|
||||
}
|
||||
|
||||
public bool UploadTexture2D(AquariumTexture2DUpload upload)
|
||||
{
|
||||
if (!upload.IsValid ||
|
||||
!sharedTextureLeases.TryGetValue(upload.ResourceKey, out var slot) ||
|
||||
slot.Width != upload.Width ||
|
||||
slot.Height != upload.Height ||
|
||||
!D3D12FieldTextureFormat.TryFormat(upload.Format, out var format) ||
|
||||
slot.Format != format)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
lock (sharedTextureUploadLock)
|
||||
{
|
||||
return UploadTexture2DLocked(upload, slot);
|
||||
}
|
||||
}
|
||||
|
||||
public void Render(AquariumFrame frame, int width, int height)
|
||||
{
|
||||
var frameCpuStart = Stopwatch.GetTimestamp();
|
||||
|
|
@ -1176,6 +1195,110 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
true);
|
||||
}
|
||||
|
||||
private bool UploadTexture2DLocked(AquariumTexture2DUpload upload, SharedTextureLeaseSlot slot)
|
||||
{
|
||||
ID3D12CommandAllocator? uploadAllocator = null;
|
||||
ID3D12GraphicsCommandList? uploadList = null;
|
||||
ID3D12Resource? uploadResource = null;
|
||||
try
|
||||
{
|
||||
if (string.Equals(upload.Format.Trim(), "NV12", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var rowBytes = RowBytes(upload.Format, upload.Width);
|
||||
if (rowBytes <= 0 || upload.SourceStrideBytes < rowBytes)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var rowPitch = AlignTo(rowBytes, D3D12.TextureDataPitchAlignment);
|
||||
var uploadBytes = checked(rowPitch * upload.Height);
|
||||
uploadResource = device.CreateCommittedResource(
|
||||
HeapType.Upload,
|
||||
ResourceDescription.Buffer((ulong)uploadBytes),
|
||||
ResourceStates.GenericRead,
|
||||
null);
|
||||
uploadResource.Name = $"Aquarium D3D12 Texture2D Lease Upload {upload.ResourceKey}";
|
||||
|
||||
unsafe
|
||||
{
|
||||
var mapped = uploadResource.Map<byte>(0);
|
||||
try
|
||||
{
|
||||
var sourceBytes = upload.Data.Span;
|
||||
for (var row = 0; row < upload.Height; row++)
|
||||
{
|
||||
sourceBytes.Slice(row * upload.SourceStrideBytes, rowBytes)
|
||||
.CopyTo(new Span<byte>(mapped + (row * rowPitch), rowBytes));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
uploadResource.Unmap(0, null);
|
||||
}
|
||||
}
|
||||
|
||||
uploadAllocator = device.CreateCommandAllocator(CommandListType.Direct);
|
||||
uploadList = device.CreateCommandList<ID3D12GraphicsCommandList>(0, CommandListType.Direct, uploadAllocator, null);
|
||||
var source = new TextureCopyLocation(
|
||||
uploadResource,
|
||||
new PlacedSubresourceFootPrint
|
||||
{
|
||||
Offset = 0,
|
||||
Footprint = new SubresourceFootPrint(slot.Format, (uint)upload.Width, (uint)upload.Height, 1, (uint)rowPitch),
|
||||
});
|
||||
var destination = new TextureCopyLocation(slot.Resource, 0);
|
||||
uploadList.ResourceBarrier(ResourceBarrier.BarrierTransition(
|
||||
slot.Resource,
|
||||
ResourceStates.PixelShaderResource,
|
||||
ResourceStates.CopyDest));
|
||||
uploadList.CopyTextureRegion(destination, 0, 0, 0, source, null);
|
||||
uploadList.ResourceBarrier(ResourceBarrier.BarrierTransition(
|
||||
slot.Resource,
|
||||
ResourceStates.CopyDest,
|
||||
ResourceStates.PixelShaderResource));
|
||||
uploadList.Close();
|
||||
commandQueue.ExecuteCommandList(uploadList);
|
||||
var signalValue = ++fenceValue;
|
||||
commandQueue.Signal(fence, signalValue).CheckError();
|
||||
if (fence.CompletedValue < signalValue)
|
||||
{
|
||||
fence.SetEventOnCompletion(signalValue, fenceEvent.SafeWaitHandle.DangerousGetHandle()).CheckError();
|
||||
fenceEvent.WaitOne();
|
||||
}
|
||||
|
||||
slot.CommittedProducerFenceValue = 0;
|
||||
slot.WaitedProducerFenceValue = 0;
|
||||
return true;
|
||||
}
|
||||
catch (SharpGenException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
uploadList?.Dispose();
|
||||
uploadAllocator?.Dispose();
|
||||
uploadResource?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private static int RowBytes(string format, int width)
|
||||
{
|
||||
var stride = AquariumFieldResourceDeclaration.FormatStrideBytes(format);
|
||||
return format.Trim().ToUpperInvariant() switch
|
||||
{
|
||||
"NV12" => width,
|
||||
_ => checked(Math.Max(1, width) * Math.Max(1, stride)),
|
||||
};
|
||||
}
|
||||
|
||||
private void WaitForCommittedFieldResourceLeases(AquariumFieldEvidenceFrame fieldEvidenceFrame)
|
||||
{
|
||||
if (!fieldEvidenceFrame.HasInput || sharedTextureLeases.Count == 0)
|
||||
|
|
|
|||
Loading…
Reference in a new issue