Page 1 of 1

Compiling SharpDX fork as AnyCPU

Posted: Sat Nov 10, 2012 5:26 am
by jens_n
Hi! I'm trying to recompile Bepu (the SharpDX-Fork) as AnyCPU. Compiling and loading the library in an x86 process works perfectly fine, but when loading the library into a x64 process the following exception is thrown:
Could not load type 'EntityStateChange' from assembly 'BEPUphysics, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 20 that is incorrectly aligned or overlapped by a non-object field.
Looking at the code i'm not sure if I can fix the problem:

Code: Select all

        //TODO: Not a particularly elegant buffering mechanism.  Make a better in-order buffering scheme.
        //There are platform requirements on layout that cause issues with the WINDOWS explicit version.
        //TODO: There's probably a better way to handle it on the XBOX/WP7 than the "give up" approach taken below.
#if WINDOWS
        [StructLayout(LayoutKind.Explicit)]
        internal struct EntityStateChange
        {
            [FieldOffset(0)]
            internal Quaternion orientationQuaternion;
            [FieldOffset(0)]
            internal Vector3 vector;
            [FieldOffset(16)]
            internal TargetField targetField;
            [FieldOffset(20)]
            internal Entity target;
        }
#else
        internal struct EntityStateChange
        {
            internal Quaternion orientationQuaternion;
            internal Vector3 vector;
            internal TargetField targetField;
            internal Entity target;
        }
#endif
Switching to the non-windows version of the struct solves the problem, but I wonder if there is a better approach?

Re: Compiling SharpDX fork as AnyCPU

Posted: Sat Nov 10, 2012 5:51 am
by Norbo
64 bit likes 8 byte alignment for reference types in structs; changing the Entity's FieldOffset to 24 (or another valid alignment) should fix the problem.

(This bug was already fixed in the development version, so it will be fixed in the SharpDX fork the next time the development fork gets promoted.)

Re: Compiling SharpDX fork as AnyCPU

Posted: Sat Nov 10, 2012 12:39 pm
by jens_n
Ooh, "incorrectly aligned" :idea:
It works now on AnyCPU, thank you!
Does changing the FieldOffset to 24 cause the struct to use 4 more bytes than actually required? (I'm not really caring about those 4 bytes, just wondering.)

Re: Compiling SharpDX fork as AnyCPU

Posted: Sat Nov 10, 2012 5:42 pm
by Norbo
Yup. The unmodified version uses 24 bytes (x86). The modified version uses 28 bytes on x86 and 32 bytes on x64.