Jump to content

View more

Image of the Day

Adding some finishing touches...
Follow us for more
#screenshotsaturday #indiedev... by #MakeGoodGames https://t.co/Otbwywbm3a
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Different math types

4: Adsense
  • You cannot reply to this topic
8 replies to this topic

#1 Happy SDE   Members   

2182
Like
0Likes
Like

Posted 19 May 2017 - 07:20 AM

Hi Forum!

I’ve started to play with NV PhysX, and found a little bit inconvenient working with different math types, that solve the same thing.

Right now, I have:
1. Own float3
2. physx::PxExtendedVec3
3. DirectX::XMFLOAT3

I am thinking on using own float3 that will have constructor and cast operators for ANY other type and on API boundary cast to particular API type.

Are there any better options for dealing with such zoo of types?

Thanks in advance!

 



#2 Shaarigan   Members   

958
Like
3Likes
Like

Posted 19 May 2017 - 08:08 AM

My simplest implementation is a union (removed any cast/constructor/operator for simplicity)

typedef union Vector3
{
   public:
      struct
      {
         float X;
         float Y;
         float Z;
      };
      float Value[3];
}vec3

I would handle any other type consisting of 3 floats like this and provide a typecast when that type does not already provide a cast from array. In my opinion this is the most simple way to traverse between different APIs



#3 Kylotan   Moderators   

9233
Like
2Likes
Like

Posted 19 May 2017 - 08:19 AM

I normally just see the explicit conversion functions. Since they only have to be used on API boundaries it's not as awkward as it might initially seem. And often there are only 1 or 2 'real' representations anyway*, so if your conversions are inlined or templated, they sometimes disappear entirely at compile time.

 

(* e.g. A Vector3 is almost always either 3 contiguous floats, or a __m128 type with 32 bits/the 'w' component disregarded.)



#4 Happy SDE   Members   

2182
Like
1Likes
Like

Posted 19 May 2017 - 08:31 AM

Thank you all!

Using union is a pretty good solution! :)

struct float3
{
    union
    {
        struct
        {
            float x, y, z;
        };

        float             arr[3];
        DirectX::XMFLOAT3 xmf3;
        physx::PxVec3     pv3;
    };

    float3(float x, float y, float z) : x{ x }, y{ y }, z{ z } {}
    float3(__m128 xyz){ DirectX::XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(this), xyz); }
    
    operator const physx::PxVec3&() const { return pv3; }
};

Edited by Happy SDE, 19 May 2017 - 08:32 AM.


#5 Kylotan   Moderators   

9233
Like
6Likes
Like

Posted 19 May 2017 - 08:41 AM

*
POPULAR

I wouldn't recommend including the 3rd party types in the union unless you are very sure about their alignment and endianness.



#6 Alberth   Members   

9192
Like
4Likes
Like

Posted 19 May 2017 - 08:41 AM

Officially it is not supported to read data from a different union field than you wrote it, afaik.

You may want to throw in a few compile-time checks that offsets and sizes of all the fields match with each other.



#7 Happy SDE   Members   

2182
Like
2Likes
Like

Posted 19 May 2017 - 09:08 AM

Thanks for advise!

So, in addition to unit tests, here are some static asserts:

TEST(float3, FieldTests)
{
    const float3 f3{ 1,2,3 };
    
    EXPECT_EQ(f3.x, 1);
    EXPECT_EQ(f3.y, 2);
    EXPECT_EQ(f3.z, 3);

    EXPECT_EQ(f3.arr[0], 1);
    EXPECT_EQ(f3.arr[1], 2);
    EXPECT_EQ(f3.arr[2], 3);

    EXPECT_EQ(f3.xmf3.x, 1);
    EXPECT_EQ(f3.xmf3.y, 2);
    EXPECT_EQ(f3.xmf3.z, 3);

    EXPECT_EQ(f3.pv3.x, 1);
    EXPECT_EQ(f3.pv3.y, 2);
    EXPECT_EQ(f3.pv3.z, 3);
}

static_assert(sizeof(float3) == 12, "Some union type is not plain 3-float");

static_assert(offsetof(float3, x) == offsetof(float3, xmf3.x), "Invalid alignment");
static_assert(offsetof(float3, y) == offsetof(float3, xmf3.y), "Invalid alignment");
static_assert(offsetof(float3, z) == offsetof(float3, xmf3.z), "Invalid alignment");

static_assert(offsetof(float3, x) == offsetof(float3, pv3.x), "Invalid alignment");
static_assert(offsetof(float3, y) == offsetof(float3, pv3.y), "Invalid alignment");
static_assert(offsetof(float3, z) == offsetof(float3, pv3.z), "Invalid alignment");
I wouldn't recommend including the 3rd party types in the union unless you are very sure about their alignment and endianness.

Kylotan, I am not sure why I should care about endianness.

The only time I heard about them is on interview.

If I target only PC (Win x64 only), and probably in a future, XBO/PS4, or next gen+1, why should I care about endianness?


Edited by Happy SDE, 19 May 2017 - 09:27 AM.


#8 Kylotan   Moderators   

9233
Like
2Likes
Like

Posted 19 May 2017 - 09:12 AM

I don't really mean endianness in terms of processor endianness, but in terms of how the values are ordered in memory. Sometimes the 'x' component of a vector uses the most significant bits of the structure, and sometimes it uses the least significant. If you start using unions and reinterpret_cast then you miss this subtlety and can break the values during conversion.



#9 Hodgman   Moderators   

50872
Like
2Likes
Like

Posted 20 May 2017 - 05:46 AM

@Happy SDE, you also want:

static_assert( alignof(physx::PxVec3) == alignof(float), "uh oh" );
static_assert( alignof(DirectX::XMFLOAT3) == alignof(float), "uh oh" );

However, this is still ill advised from a physical architecture point of view (the way that your code is physically laid out in files).

You generally want to contain dependencies as much as possible, keeping the number of dependencies for each individual file as low as absolutely possible. What you're doing here is the opposite of that. You're forcing every bit of code that wants to operate on a float3 type to by physically dependent on DirectXMath library and the PhysX library. The vast majority of your game code should be completely unaware of the existence of these two libraries, which makes this a bad physical architecture.

On small projects, bad physical architecture is tolerable. On larger projects though, its the thing that determines whether you game gets two-minute compile times or two-hour compile times :lol: and also has massive impacts on long-term maintainability.

Officially it is not supported to read data from a different union field than you wrote it, afaik
Yep the language spec forbids this kind of code, as it violates the aliasing rules... however, every compiler that I'm aware of actually has valid implementation defined behaviour for this kind of code, and they actually encourage this as the recommended way of getting around the aliasing rules when you have to (besides using memcpy, which is the language spec's official loophole).

Edited by Hodgman, 20 May 2017 - 05:50 AM.