From ecfcb666a8b61759e21b9f55bd5aeed5cc7e3163 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 23 Feb 2024 10:56:03 -0800 Subject: [PATCH] remove byte pointer interface from TransferBuffer --- src/Graphics/ResourceInitializer.cs | 3 +- src/Graphics/Resources/CpuBuffer.cs | 84 ++++++++++++++--------------- 2 files changed, 41 insertions(+), 46 deletions(-) diff --git a/src/Graphics/ResourceInitializer.cs b/src/Graphics/ResourceInitializer.cs index 027ddbd..e13e34f 100644 --- a/src/Graphics/ResourceInitializer.cs +++ b/src/Graphics/ResourceInitializer.cs @@ -99,7 +99,8 @@ namespace MoonWorks.Graphics TransferBuffer = new TransferBuffer(Device, dataSize); } - TransferBuffer.SetData(data, new BufferCopy(0, 0, dataSize), SetDataOptions.Discard); + var dataSpan = new Span(data, (int) dataSize); + TransferBuffer.SetData(dataSpan, SetDataOptions.Discard); var commandBuffer = Device.AcquireCommandBuffer(); diff --git a/src/Graphics/Resources/CpuBuffer.cs b/src/Graphics/Resources/CpuBuffer.cs index b8f75ab..b93200d 100644 --- a/src/Graphics/Resources/CpuBuffer.cs +++ b/src/Graphics/Resources/CpuBuffer.cs @@ -48,29 +48,6 @@ namespace MoonWorks.Graphics Size = sizeInBytes; } - /// - /// Immediately copies data from a data pointer to the TransferBuffer. - /// - /// If setDataOption is DISCARD and this TransferBuffer was used in an Upload command, - /// that command will still use the correct data at the cost of increased memory usage. - /// - /// If setDataOption is OVERWRITE and this TransferBuffer was used in an Upload command, - /// this could cause a data race. - /// - public unsafe void SetData( - byte* dataPtr, - in BufferCopy copyParams, - SetDataOptions setDataOption - ) { - Refresh.Refresh_SetData( - Device.Handle, - (nint) dataPtr, - Handle, - copyParams.ToRefresh(), - (Refresh.SetDataOptions) setDataOption - ); - } - /// /// Immediately copies data from a Span to the TransferBuffer. /// Returns the length of the copy in bytes. @@ -90,12 +67,23 @@ namespace MoonWorks.Graphics var elementSize = Marshal.SizeOf(); var dataLengthInBytes = (uint) (elementSize * data.Length); +#if DEBUG + AssertBufferBoundsCheck(Size, bufferOffsetInBytes, dataLengthInBytes); +#endif + fixed (T* dataPtr = data) { - SetData( - (byte*) dataPtr, - new BufferCopy(0, bufferOffsetInBytes, dataLengthInBytes), - setDataOption + Refresh.Refresh_SetData( + Device.Handle, + (nint) dataPtr, + Handle, + new Refresh.BufferCopy + { + srcOffset = 0, + dstOffset = bufferOffsetInBytes, + size = dataLengthInBytes + }, + (Refresh.SetDataOptions) setDataOption ); } @@ -120,21 +108,6 @@ namespace MoonWorks.Graphics return SetData(data, 0, setDataOption); } - /// - /// Immediately copies data from the TransferBuffer into a data pointer. - /// - public unsafe void GetData( - byte* dataPtr, - in BufferCopy copyParams - ) { - Refresh.Refresh_GetData( - Device.Handle, - Handle, - (nint) dataPtr, - copyParams.ToRefresh() - ); - } - /// /// Immediately copies data from the TransferBuffer into a Span. /// @@ -146,13 +119,34 @@ namespace MoonWorks.Graphics var elementSize = Marshal.SizeOf(); var dataLengthInBytes = (uint) (elementSize * data.Length); +#if DEBUG + AssertBufferBoundsCheck(Size, bufferOffsetInBytes, dataLengthInBytes); +#endif + fixed (T* dataPtr = data) { - GetData( - (byte*) dataPtr, - new BufferCopy(bufferOffsetInBytes, 0, dataLengthInBytes) + Refresh.Refresh_GetData( + Device.Handle, + Handle, + (nint) dataPtr, + new Refresh.BufferCopy + { + srcOffset = bufferOffsetInBytes, + dstOffset = 0, + size = dataLengthInBytes + } ); } } + +#if DEBUG + private void AssertBufferBoundsCheck(uint bufferLengthInBytes, uint offsetInBytes, uint copyLengthInBytes) + { + if (copyLengthInBytes > bufferLengthInBytes + offsetInBytes) + { + throw new InvalidOperationException($"SetData overflow! Transfer buffer length {bufferLengthInBytes}, offset {offsetInBytes}, copy length {copyLengthInBytes}"); + } + } +#endif } }