remove byte pointer interface from TransferBuffer

what_if_no_video_threads
cosmonaut 2024-02-23 10:56:03 -08:00
parent ad97aed60f
commit ecfcb666a8
2 changed files with 41 additions and 46 deletions

View File

@ -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<byte>(data, (int) dataSize);
TransferBuffer.SetData(dataSpan, SetDataOptions.Discard);
var commandBuffer = Device.AcquireCommandBuffer();

View File

@ -48,29 +48,6 @@ namespace MoonWorks.Graphics
Size = sizeInBytes;
}
/// <summary>
/// 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.
/// </summary>
public unsafe void SetData(
byte* dataPtr,
in BufferCopy copyParams,
SetDataOptions setDataOption
) {
Refresh.Refresh_SetData(
Device.Handle,
(nint) dataPtr,
Handle,
copyParams.ToRefresh(),
(Refresh.SetDataOptions) setDataOption
);
}
/// <summary>
/// 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<T>();
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);
}
/// <summary>
/// Immediately copies data from the TransferBuffer into a data pointer.
/// </summary>
public unsafe void GetData(
byte* dataPtr,
in BufferCopy copyParams
) {
Refresh.Refresh_GetData(
Device.Handle,
Handle,
(nint) dataPtr,
copyParams.ToRefresh()
);
}
/// <summary>
/// Immediately copies data from the TransferBuffer into a Span.
/// </summary>
@ -146,13 +119,34 @@ namespace MoonWorks.Graphics
var elementSize = Marshal.SizeOf<T>();
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
}
}