From 088e7c4b6f9bae7812876bd3456f469f268faf42 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Mon, 7 Aug 2023 10:12:46 -0700 Subject: [PATCH] add debug check for zero length buffer copy --- src/Graphics/CommandBuffer.cs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index a6647918..431770d5 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -1718,10 +1718,6 @@ namespace MoonWorks.Graphics uint bufferOffsetInBytes = 0 ) where T : unmanaged { -#if DEBUG - AssertRenderPassInactive("Cannot copy during render pass!"); -#endif - SetBufferData( buffer, data, @@ -1770,6 +1766,7 @@ namespace MoonWorks.Graphics { #if DEBUG AssertRenderPassInactive("Cannot copy during render pass!"); + AssertNonEmptyCopy(dataLengthInBytes); #endif Refresh.Refresh_SetBufferData( @@ -1799,13 +1796,15 @@ namespace MoonWorks.Graphics uint numElements ) where T : unmanaged { + var elementSize = Marshal.SizeOf(); + var dataLengthInBytes = (uint) (elementSize * numElements); + var dataOffsetInBytes = (int) startElement * elementSize; + #if DEBUG AssertRenderPassInactive("Cannot copy during render pass!"); + AssertNonEmptyCopy(dataLengthInBytes); #endif - var elementSize = Marshal.SizeOf(); - var dataOffsetInBytes = (int) startElement * elementSize; - fixed (T* ptr = data) { Refresh.Refresh_SetBufferData( @@ -1814,7 +1813,7 @@ namespace MoonWorks.Graphics buffer.Handle, bufferOffsetInBytes, (IntPtr) ptr + dataOffsetInBytes, - (uint) (numElements * elementSize) + dataLengthInBytes ); } } @@ -1844,18 +1843,23 @@ namespace MoonWorks.Graphics IntPtr dataPtr, uint bufferOffsetInElements, uint numElements - ) where T : unmanaged { + ) where T : unmanaged + { + var elementSize = Marshal.SizeOf(); + var dataLengthInBytes = (uint) (elementSize * numElements); + #if DEBUG AssertRenderPassInactive("Cannot copy during render pass!"); + AssertNonEmptyCopy((uint) (elementSize * numElements)); #endif Refresh.Refresh_SetBufferData( Device.Handle, Handle, buffer.Handle, - (uint) Marshal.SizeOf() * bufferOffsetInElements, + (uint) elementSize * bufferOffsetInElements, dataPtr, - (uint) Marshal.SizeOf() * numElements + dataLengthInBytes ); } @@ -2197,6 +2201,14 @@ namespace MoonWorks.Graphics throw new System.ArgumentException("The bound Texture's UsageFlags must include TextureUsageFlags.Sampler!"); } } + + private void AssertNonEmptyCopy(uint dataLengthInBytes) + { + if (dataLengthInBytes == 0) + { + throw new System.InvalidOperationException("SetBufferData must have a length greater than 0 bytes!"); + } + } #endif } }