From d76633bdfccc01d0ad625ca846ef4050b3b64e75 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Tue, 13 Dec 2022 19:01:40 -0800 Subject: [PATCH] change sizeof calls to Marshal.SizeOf in interop situations --- src/Audio/AudioDevice.cs | 10 +++++----- src/Graphics/CommandBuffer.cs | 13 +++++++------ src/Graphics/Font/Packer.cs | 4 ++-- src/Graphics/RefreshStructs.cs | 2 +- src/Graphics/Resources/Buffer.cs | 3 ++- src/Graphics/State/ComputeShaderInfo.cs | 2 +- src/Graphics/State/GraphicsShaderInfo.cs | 2 +- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/Audio/AudioDevice.cs b/src/Audio/AudioDevice.cs index b297d736..5f00e198 100644 --- a/src/Audio/AudioDevice.cs +++ b/src/Audio/AudioDevice.cs @@ -118,13 +118,13 @@ namespace MoonWorks.Audio IntPtr chainPtr; chainPtr = Marshal.AllocHGlobal( - sizeof(FAudio.FAudioEffectChain) + Marshal.SizeOf() ); FAudio.FAudioEffectChain* reverbChain = (FAudio.FAudioEffectChain*) chainPtr; reverbChain->EffectCount = 1; reverbChain->pEffectDescriptors = Marshal.AllocHGlobal( - sizeof(FAudio.FAudioEffectDescriptor) + Marshal.SizeOf() ); FAudio.FAudioEffectDescriptor* reverbDescriptor = @@ -157,7 +157,7 @@ namespace MoonWorks.Audio // Defaults based on FAUDIOFX_I3DL2_PRESET_GENERIC IntPtr reverbParamsPtr = Marshal.AllocHGlobal( - sizeof(FAudio.FAudioFXReverbParameters) + Marshal.SizeOf() ); FAudio.FAudioFXReverbParameters* reverbParams = (FAudio.FAudioFXReverbParameters*) reverbParamsPtr; @@ -187,7 +187,7 @@ namespace MoonWorks.Audio ReverbVoice, 0, reverbParamsPtr, - (uint) sizeof(FAudio.FAudioFXReverbParameters), + (uint) Marshal.SizeOf(), 0 ); Marshal.FreeHGlobal(reverbParamsPtr); @@ -198,7 +198,7 @@ namespace MoonWorks.Audio { SendCount = 2, pSends = Marshal.AllocHGlobal( - 2 * sizeof(FAudio.FAudioSendDescriptor) + 2 * Marshal.SizeOf() ) }; FAudio.FAudioSendDescriptor* sendDesc = (FAudio.FAudioSendDescriptor*) ReverbSends.pSends; diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index c32139b9..84e794b7 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; using RefreshCS; namespace MoonWorks.Graphics @@ -1406,7 +1407,7 @@ namespace MoonWorks.Graphics Device.Handle, Handle, (IntPtr) uniformsPtr, - (uint) sizeof(T) + (uint) Marshal.SizeOf() ); } } @@ -1433,7 +1434,7 @@ namespace MoonWorks.Graphics Device.Handle, Handle, (IntPtr) uniformsPtr, - (uint) sizeof(T) + (uint) Marshal.SizeOf() ); } } @@ -1461,7 +1462,7 @@ namespace MoonWorks.Graphics Device.Handle, Handle, (IntPtr) uniformsPtr, - (uint) sizeof(T) + (uint) Marshal.SizeOf() ); } } @@ -1720,7 +1721,7 @@ namespace MoonWorks.Graphics AssertRenderPassInactive("Cannot copy during render pass!"); #endif - var elementSize = sizeof(T); + var elementSize = Marshal.SizeOf(); fixed (T* ptr = &data[startElement]) { @@ -1749,9 +1750,9 @@ namespace MoonWorks.Graphics Device.Handle, Handle, buffer.Handle, - (uint) sizeof(T) * bufferOffsetInElements, + (uint) Marshal.SizeOf() * bufferOffsetInElements, dataPtr, - (uint) sizeof(T) * numElements + (uint) Marshal.SizeOf() * numElements ); } diff --git a/src/Graphics/Font/Packer.cs b/src/Graphics/Font/Packer.cs index 3395a39b..198ea3b8 100644 --- a/src/Graphics/Font/Packer.cs +++ b/src/Graphics/Font/Packer.cs @@ -28,8 +28,8 @@ namespace MoonWorks.Graphics.Font { fixed (FontRange *pFontRanges = &fontRanges[0]) { - var nativeSize = fontRanges.Length * sizeof(Wellspring.FontRange); - void* fontRangeMemory = NativeMemory.Alloc((nuint) fontRanges.Length, (nuint) sizeof(Wellspring.FontRange)); + var nativeSize = fontRanges.Length * Marshal.SizeOf(); + void* fontRangeMemory = NativeMemory.Alloc((nuint) fontRanges.Length, (nuint) Marshal.SizeOf()); System.Buffer.MemoryCopy(pFontRanges, fontRangeMemory, nativeSize, nativeSize); var result = Wellspring.Wellspring_PackFontRanges(Handle, (IntPtr) fontRangeMemory, (uint) fontRanges.Length); diff --git a/src/Graphics/RefreshStructs.cs b/src/Graphics/RefreshStructs.cs index ad9e530d..af7f9b33 100644 --- a/src/Graphics/RefreshStructs.cs +++ b/src/Graphics/RefreshStructs.cs @@ -134,7 +134,7 @@ namespace MoonWorks.Graphics { Binding = 0, InputRate = VertexInputRate.Vertex, - Stride = (uint) sizeof(T) + Stride = (uint) Marshal.SizeOf() }; } } diff --git a/src/Graphics/Resources/Buffer.cs b/src/Graphics/Resources/Buffer.cs index 24047aae..63814a07 100644 --- a/src/Graphics/Resources/Buffer.cs +++ b/src/Graphics/Resources/Buffer.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; using RefreshCS; namespace MoonWorks.Graphics @@ -32,7 +33,7 @@ namespace MoonWorks.Graphics return new Buffer( device, usageFlags, - (uint) sizeof(T) * elementCount + (uint) Marshal.SizeOf() * elementCount ); } diff --git a/src/Graphics/State/ComputeShaderInfo.cs b/src/Graphics/State/ComputeShaderInfo.cs index ef831783..9294a1c8 100644 --- a/src/Graphics/State/ComputeShaderInfo.cs +++ b/src/Graphics/State/ComputeShaderInfo.cs @@ -24,7 +24,7 @@ namespace MoonWorks.Graphics { ShaderModule = shaderModule, EntryPointName = entryPointName, - UniformBufferSize = (uint) sizeof(T), + UniformBufferSize = (uint) Marshal.SizeOf(), BufferBindingCount = bufferBindingCount, ImageBindingCount = imageBindingCount }; diff --git a/src/Graphics/State/GraphicsShaderInfo.cs b/src/Graphics/State/GraphicsShaderInfo.cs index b0e84e5b..38b2bd33 100644 --- a/src/Graphics/State/GraphicsShaderInfo.cs +++ b/src/Graphics/State/GraphicsShaderInfo.cs @@ -22,7 +22,7 @@ namespace MoonWorks.Graphics { ShaderModule = shaderModule, EntryPointName = entryPointName, - UniformBufferSize = (uint) sizeof(T), + UniformBufferSize = (uint) Marshal.SizeOf(), SamplerBindingCount = samplerBindingCount }; }