change sizeof calls to Marshal.SizeOf in interop situations

cosmonaut 2022-12-13 19:01:40 -08:00 committed by Caleb Cornett
parent debb76f62a
commit b9f2d50730
7 changed files with 46 additions and 17 deletions

View File

@ -118,13 +118,13 @@ namespace MoonWorks.Audio
IntPtr chainPtr;
chainPtr = Marshal.AllocHGlobal(
sizeof(FAudio.FAudioEffectChain)
Marshal.SizeOf<FAudio.FAudioEffectChain>()
);
FAudio.FAudioEffectChain* reverbChain = (FAudio.FAudioEffectChain*) chainPtr;
reverbChain->EffectCount = 1;
reverbChain->pEffectDescriptors = Marshal.AllocHGlobal(
sizeof(FAudio.FAudioEffectDescriptor)
Marshal.SizeOf<FAudio.FAudioEffectDescriptor>()
);
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>()
);
FAudio.FAudioFXReverbParameters* reverbParams = (FAudio.FAudioFXReverbParameters*) reverbParamsPtr;
@ -187,7 +187,7 @@ namespace MoonWorks.Audio
ReverbVoice,
0,
reverbParamsPtr,
(uint) sizeof(FAudio.FAudioFXReverbParameters),
(uint) Marshal.SizeOf<FAudio.FAudioFXReverbParameters>(),
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>()
)
};
FAudio.FAudioSendDescriptor* sendDesc = (FAudio.FAudioSendDescriptor*) ReverbSends.pSends;

View File

@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;
using RefreshCS;
namespace MoonWorks.Graphics
@ -846,6 +847,10 @@ namespace MoonWorks.Graphics
in BufferBinding bufferBinding,
uint firstBinding = 0
) {
#if DEBUG
AssertGraphicsPipelineBound();
#endif
var bufferPtrs = stackalloc IntPtr[1];
var offsets = stackalloc ulong[1];
@ -873,6 +878,10 @@ namespace MoonWorks.Graphics
in BufferBinding bufferBindingTwo,
uint firstBinding = 0
) {
#if DEBUG
AssertGraphicsPipelineBound();
#endif
var bufferPtrs = stackalloc IntPtr[2];
var offsets = stackalloc ulong[2];
@ -905,6 +914,10 @@ namespace MoonWorks.Graphics
in BufferBinding bufferBindingThree,
uint firstBinding = 0
) {
#if DEBUG
AssertGraphicsPipelineBound();
#endif
var bufferPtrs = stackalloc IntPtr[3];
var offsets = stackalloc ulong[3];
@ -941,6 +954,10 @@ namespace MoonWorks.Graphics
in BufferBinding bufferBindingFour,
uint firstBinding = 0
) {
#if DEBUG
AssertGraphicsPipelineBound();
#endif
var bufferPtrs = stackalloc IntPtr[4];
var offsets = stackalloc ulong[4];
@ -973,6 +990,10 @@ namespace MoonWorks.Graphics
in Span<BufferBinding> bufferBindings,
uint firstBinding = 0
) {
#if DEBUG
AssertGraphicsPipelineBound();
#endif
var bufferPtrs = stackalloc IntPtr[bufferBindings.Length];
var offsets = stackalloc ulong[bufferBindings.Length];
@ -1406,7 +1427,7 @@ namespace MoonWorks.Graphics
Device.Handle,
Handle,
(IntPtr) uniformsPtr,
(uint) sizeof(T)
(uint) Marshal.SizeOf<T>()
);
}
}
@ -1433,7 +1454,7 @@ namespace MoonWorks.Graphics
Device.Handle,
Handle,
(IntPtr) uniformsPtr,
(uint) sizeof(T)
(uint) Marshal.SizeOf<T>()
);
}
}
@ -1461,7 +1482,7 @@ namespace MoonWorks.Graphics
Device.Handle,
Handle,
(IntPtr) uniformsPtr,
(uint) sizeof(T)
(uint) Marshal.SizeOf<T>()
);
}
}
@ -1622,6 +1643,13 @@ namespace MoonWorks.Graphics
public Texture AcquireSwapchainTexture(
Window window
) {
#if DEBUG
if (!window.Claimed)
{
throw new System.InvalidOperationException("Cannot acquire swapchain texture, window has not been claimed!");
}
#endif
var texturePtr = Refresh.Refresh_AcquireSwapchainTexture(
Device.Handle,
Handle,
@ -1720,7 +1748,7 @@ namespace MoonWorks.Graphics
AssertRenderPassInactive("Cannot copy during render pass!");
#endif
var elementSize = sizeof(T);
var elementSize = Marshal.SizeOf<T>();
fixed (T* ptr = &data[startElement])
{
@ -1749,9 +1777,9 @@ namespace MoonWorks.Graphics
Device.Handle,
Handle,
buffer.Handle,
(uint) sizeof(T) * bufferOffsetInElements,
(uint) Marshal.SizeOf<T>() * bufferOffsetInElements,
dataPtr,
(uint) sizeof(T) * numElements
(uint) Marshal.SizeOf<T>() * numElements
);
}

View File

@ -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<Wellspring.FontRange>();
void* fontRangeMemory = NativeMemory.Alloc((nuint) fontRanges.Length, (nuint) Marshal.SizeOf<Wellspring.FontRange>());
System.Buffer.MemoryCopy(pFontRanges, fontRangeMemory, nativeSize, nativeSize);
var result = Wellspring.Wellspring_PackFontRanges(Handle, (IntPtr) fontRangeMemory, (uint) fontRanges.Length);

View File

@ -134,7 +134,7 @@ namespace MoonWorks.Graphics
{
Binding = 0,
InputRate = VertexInputRate.Vertex,
Stride = (uint) sizeof(T)
Stride = (uint) Marshal.SizeOf<T>()
};
}
}

View File

@ -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<T>() * elementCount
);
}

View File

@ -24,7 +24,7 @@ namespace MoonWorks.Graphics
{
ShaderModule = shaderModule,
EntryPointName = entryPointName,
UniformBufferSize = (uint) sizeof(T),
UniformBufferSize = (uint) Marshal.SizeOf<T>(),
BufferBindingCount = bufferBindingCount,
ImageBindingCount = imageBindingCount
};

View File

@ -22,7 +22,7 @@ namespace MoonWorks.Graphics
{
ShaderModule = shaderModule,
EntryPointName = entryPointName,
UniformBufferSize = (uint) sizeof(T),
UniformBufferSize = (uint) Marshal.SizeOf<T>(),
SamplerBindingCount = samplerBindingCount
};
}