Jump to content
  • Advertisement
Sign in to follow this  
Happy SDE

Different math types

This topic is 422 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

 

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites

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.)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

@[member="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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!