Git Product home page Git Product logo

Comments (9)

kunitoki avatar kunitoki commented on September 26, 2024 2

I see, i applied the patch to luabridge so we should allow overaligned userdata values

from luabridge3.

Mellnik avatar Mellnik commented on September 26, 2024

After your alignment fix there is still a problem when an aligned class has members that are also aligned.

Consider this kind of pseudo code below. Initializing the MyTransform with the constructor in Lua will store and print invalid values in tostring. You might need to run it multiple times to see the problem!

Could you please look into this again. Thank you.

typedef __m128d SSEType;

struct alignas(16) OtherClass
{
public:
	SSEType XY;
	SSEType ZW;
};

void __VectorStoreFloat3(const OtherClass& Vec, double* Dst)
{
	_mm_storeu_pd(Dst, Vec.XY);
	_mm_store_sd(Dst + 2, Vec.ZW);
}

void __VectorStoreAligned(const OtherClass& Vec, double* Dst)
{
	_mm_store_pd(Dst, Vec.XY);
	_mm_store_pd(Dst + 2, Vec.ZW);
}

OtherClass __MakeVectorRegisterDouble(double X, double Y, double Z, double W)
{
	OtherClass Result;
	Result.XY = _mm_setr_pd(X, Y);
	Result.ZW = _mm_setr_pd(Z, W);
	return Result;
}

OtherClass __VectorLoadAligned(const double* Ptr)
{
	OtherClass Result;
	Result.XY = _mm_load_pd((const double*)(Ptr));
	Result.ZW = _mm_load_pd((const double*)(Ptr + 2));
	return Result;
}

OtherClass __VectorLoadAligned(const MyQuat* Ptr)
{
	return __VectorLoadAligned((const double*)(Ptr));
}

template<typename T>
struct alignas(16) MyTransform
{
public:
	OtherClass Rotation;
	OtherClass Translation;
	OtherClass Scale3D;

	MyTransform(const MyQuat& InRotation, const MyVector& InTranslation, const MyVector& InScale3D)
	{
		Rotation = __VectorLoadAligned(&InRotation);
		Translation = __MakeVectorRegisterDouble(InTranslation.X, InTranslation.Y, InTranslation.Z, 0.0f);
		Scale3D = __MakeVectorRegisterDouble(InScale3D.X, InScale3D.Y, InScale3D.Z, 0.0f);
	}

	MyRotation Rotator() const
	{
		MyQuat OutRotation;
		__VectorStoreAligned(Rotation, (double*)&OutRotation);
		return OutRotation.Rotator();
	}

	MyVector GetTranslation() const
	{
		MyVector OutTranslation;
		__VectorStoreFloat3(Translation, (double*)&OutTranslation);
		return OutTranslation;
	}

	MyVector GetScale3D() const
	{
		MyVector OutScale3D;
		__VectorStoreFloat3(Scale3D, (double*)&OutScale3D);
		return OutScale3D;
	}

	MyString ToString() const
	{
		const MyRotation R(Rotator());
		const MyVector TT(GetTranslation());
		const MyVector S(GetScale3D());

		return MyString::fmt("%f,%f,%f|%f,%f,%f|%f,%f,%f", TT.X, TT.Y, TT.Z, R.Pitch, R.Yaw, R.Roll, S.X, S.Y, S.Z);
	}
};

using FMyTransform = MyTransform<double>;

		.beginClass<FMyTransform>("Trans")
			.addConstructor<void(const MyQuat&, const MyVector&, const MyVector&)>()
			.addFunction("__tostring", &FMyTransform::ToString)
		.endClass()

from luabridge3.

kunitoki avatar kunitoki commented on September 26, 2024

Are MyQuat and MyVector also aligned ?

from luabridge3.

Mellnik avatar Mellnik commented on September 26, 2024

Are MyQuat and MyVector also aligned ?

MyQuat is also 16 aligned. MyVector is just a normal struct.

from luabridge3.

kunitoki avatar kunitoki commented on September 26, 2024

Are you able to reduce the example to something smaller ? It's very difficult for me to recreate a unit test for it, there's lot going on there, conversions back and forth from aligned / unaligned with lot of reinterpret casting.

from luabridge3.

Mellnik avatar Mellnik commented on September 26, 2024

I will try to make a compilable code for you but no promises to be smaller 🤣

from luabridge3.

Mellnik avatar Mellnik commented on September 26, 2024

Okay I have narrowed it down to addStaticProperty.
I am binding a non-const static value there of type MyQuat with alignas(16).

.addStaticProperty("Identity", &MyLuaConstants::Identity, false)

Now when I do this in Lua:

MyQ = MyQuat.Identity
MyQ.X = 1337
print(MyQuat.Identity) -- X got overridden with the value above

The static variable actually gets overridden!
I remember I had the same problem with vanilla LB but didn't track it down further.

Did I do something wrong or why is this?

Edit:

class MyLuaConstants
{
public:
	static MyQuat Identity;
};

MyQuat MyLuaConstants::Identity(MyQuat(0, 0, 0, 1));

Edit2:
It's not related to alignas. I misinterpreted addStaticProperty. I thought it just makes a copy for constants.
This made my test scripts fail.

Gotta need a function that binds a static const class variable and makes it read only in Lua.

from luabridge3.

kunitoki avatar kunitoki commented on September 26, 2024

I think that luabridge is not able to prevent subobject assignments to const properties.

So if you register a static property and make it non writable, you cannot set the static property, but if you access a property of the property, that could not know it shouldn't allow modification because the parent object is assigned to a non writable property.

What happens if you make Identity const or register it with std::as_const ?

from luabridge3.

Mellnik avatar Mellnik commented on September 26, 2024

Seems right. I think the best thing to do is:

.addStaticProperty("Identity", []() -> MyQuat { return MyQuat:Identity; })

from luabridge3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.