SilkVulkanTutorial icon indicating copy to clipboard operation
SilkVulkanTutorial copied to clipboard

Small aligment suggestion

Open Arugin opened this issue 2 years ago • 1 comments

https://github.com/stymee/SilkVulkanTutorial/blob/7d089b0f4f947bfcd152b657b8db9a27ca7fceba/Source/Chapter09PushConstants/FirstApp.cs#L30C17-L30C17

Here you can do something like this

[StructLayout(LayoutKind.Explicit)]
public struct SimplePushConstantData {
    [FieldOffset(0)] public Vector2 Offset;
    [FieldOffset(16)] public Vector3 Color;

    public static uint SizeOf() => (uint)Unsafe.SizeOf<SimplePushConstantData>();
}

Pack property of LayoutKind.Sequential is for type aligment itself, but each field must align with fields of its own size (1, 2, 4, 8, etc., bytes) or the alignment of the type, whichever is smaller (source https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.structlayoutattribute.pack?view=net-7.0)

Arugin avatar Sep 15 '23 15:09 Arugin

@Arugin you can also do

[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct SimplePushConstantData 
{
    fixed byte padding[16];
    public Vector4 Color;
}

clibequilibrium avatar Oct 11 '23 18:10 clibequilibrium