Compiling SharpDX fork as AnyCPU

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
jens_n
Posts: 4
Joined: Tue Feb 28, 2012 6:57 am

Compiling SharpDX fork as AnyCPU

Post 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?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Compiling SharpDX fork as AnyCPU

Post 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.)
jens_n
Posts: 4
Joined: Tue Feb 28, 2012 6:57 am

Re: Compiling SharpDX fork as AnyCPU

Post 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.)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Compiling SharpDX fork as AnyCPU

Post by Norbo »

Yup. The unmodified version uses 24 bytes (x86). The modified version uses 28 bytes on x86 and 32 bytes on x64.
Post Reply