Comments (9)
I see, i applied the patch to luabridge so we should allow overaligned userdata values
from luabridge3.
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.
Are MyQuat
and MyVector
also aligned ?
from luabridge3.
Are
MyQuat
andMyVector
also aligned ?
MyQuat
is also 16 aligned. MyVector
is just a normal struct
.
from luabridge3.
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.
I will try to make a compilable code for you but no promises to be smaller 🤣
from luabridge3.
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.
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.
Seems right. I think the best thing to do is:
.addStaticProperty("Identity", []() -> MyQuat { return MyQuat:Identity; })
from luabridge3.
Related Issues (20)
- Compiler error use of deleted function luabridge::TypeResult<T>::TypeResult(...) HOT 6
- Enhancement Request: Custom error function for `lua_pcall`. HOT 11
- Error C3861 (using with Luau) HOT 3
- Can I cast types inside Lua? HOT 4
- Duplicate symbols on Win32 with default __stdcall HOT 9
- Debug names of C functions for Luau HOT 8
- Extensible class bug with inheritance
- Add support for addStaticIndexMetamethod and addStaticNewIndexMetamethod
- Using LuaBridge3 in a precompiled header on MSVC leads to an internal compiler error HOT 2
- Use of std::shared_ptr on classes without `std::enable_shared_from_this` HOT 1
- Stateful lambdas registered as functions loose state HOT 2
- Compile error with Cfunc property getter/setter HOT 2
- std::vector to 0-based Lua table HOT 5
- MSC Warning C4146 HOT 2
- How to expose C++ data (a vector, in this case) to Lua without making a copy? HOT 2
- Does LuaBridge3 Support Android for SDL2-based Games? HOT 2
- Passing a Lua Table (LuaRef) as an argument to C++ function HOT 1
- How to get the name of a Lua variable? HOT 2
- Comparison Issue Between `uint64_t` in C++ and Numeric Constant in Lua HOT 8
- NAI run lua code in string from cpp
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from luabridge3.