mirror of
https://github.com/luau-lang/luau.git
synced 2025-05-04 10:33:46 +01:00
Extend vector constants to four components
This commit is contained in:
parent
5ea64be340
commit
996db08210
8 changed files with 41 additions and 18 deletions
|
@ -54,7 +54,7 @@ public:
|
||||||
int32_t addConstantNil();
|
int32_t addConstantNil();
|
||||||
int32_t addConstantBoolean(bool value);
|
int32_t addConstantBoolean(bool value);
|
||||||
int32_t addConstantNumber(double value);
|
int32_t addConstantNumber(double value);
|
||||||
int32_t addConstantVector(float x, float y, float z);
|
int32_t addConstantVector(float x, float y, float z, float w);
|
||||||
int32_t addConstantString(StringRef value);
|
int32_t addConstantString(StringRef value);
|
||||||
int32_t addImport(uint32_t iid);
|
int32_t addImport(uint32_t iid);
|
||||||
int32_t addConstantTable(const TableShape& shape);
|
int32_t addConstantTable(const TableShape& shape);
|
||||||
|
@ -159,7 +159,7 @@ private:
|
||||||
{
|
{
|
||||||
bool valueBoolean;
|
bool valueBoolean;
|
||||||
double valueNumber;
|
double valueNumber;
|
||||||
float valueVector[3];
|
float valueVector[4];
|
||||||
unsigned int valueString; // index into string table
|
unsigned int valueString; // index into string table
|
||||||
uint32_t valueImport; // 10-10-10-2 encoded import id
|
uint32_t valueImport; // 10-10-10-2 encoded import id
|
||||||
uint32_t valueTable; // index into tableShapes[]
|
uint32_t valueTable; // index into tableShapes[]
|
||||||
|
@ -171,9 +171,9 @@ private:
|
||||||
{
|
{
|
||||||
Constant::Type type;
|
Constant::Type type;
|
||||||
// Note: this stores value* from Constant; when type is Type_Number, this stores the same bits as double does but in uint64_t.
|
// Note: this stores value* from Constant; when type is Type_Number, this stores the same bits as double does but in uint64_t.
|
||||||
// for Type_Vector, x and y are stored in value and z is stored in extra.
|
// For Type_Vector, x and y are stored in 'value' and z and w are stored in 'extra'.
|
||||||
uint64_t value;
|
uint64_t value;
|
||||||
uint32_t extra = 0;
|
uint64_t extra = 0;
|
||||||
|
|
||||||
bool operator==(const ConstantKey& key) const
|
bool operator==(const ConstantKey& key) const
|
||||||
{
|
{
|
||||||
|
|
|
@ -34,12 +34,13 @@ static Constant cnum(double v)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Constant cvector(double x, double y, double z)
|
static Constant cvector(double x, double y, double z, double w)
|
||||||
{
|
{
|
||||||
Constant res = {Constant::Type_Vector};
|
Constant res = {Constant::Type_Vector};
|
||||||
res.valueVector[0] = (float)x;
|
res.valueVector[0] = (float)x;
|
||||||
res.valueVector[1] = (float)y;
|
res.valueVector[1] = (float)y;
|
||||||
res.valueVector[2] = (float)z;
|
res.valueVector[2] = (float)z;
|
||||||
|
res.valueVector[3] = (float)w;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -472,11 +473,16 @@ Constant foldBuiltin(int bfid, const Constant* args, size_t count)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LBF_VECTOR:
|
case LBF_VECTOR:
|
||||||
if (FFlag::LuauVectorLiterals && count == 3 &&
|
if (FFlag::LuauVectorLiterals && count >= 3 &&
|
||||||
args[0].type == Constant::Type_Number &&
|
args[0].type == Constant::Type_Number &&
|
||||||
args[1].type == Constant::Type_Number &&
|
args[1].type == Constant::Type_Number &&
|
||||||
args[2].type == Constant::Type_Number)
|
args[2].type == Constant::Type_Number)
|
||||||
return cvector(args[0].valueNumber, args[1].valueNumber, args[2].valueNumber);
|
{
|
||||||
|
if (count == 3)
|
||||||
|
return cvector(args[0].valueNumber, args[1].valueNumber, args[2].valueNumber, 0.0);
|
||||||
|
else if (count == 4 && args[3].type == Constant::Type_Number)
|
||||||
|
return cvector(args[0].valueNumber, args[1].valueNumber, args[2].valueNumber, args[3].valueNumber);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -154,17 +154,18 @@ size_t BytecodeBuilder::ConstantKeyHash::operator()(const ConstantKey& key) cons
|
||||||
{
|
{
|
||||||
if (key.type == Constant::Type_Vector)
|
if (key.type == Constant::Type_Vector)
|
||||||
{
|
{
|
||||||
uint32_t i[3];
|
uint32_t i[4];
|
||||||
static_assert(sizeof(key.value) + sizeof(key.extra) == sizeof(i), "Expecting vector to have three 32-bit components");
|
static_assert(sizeof(key.value) + sizeof(key.extra) == sizeof(i), "Expecting vector to have four 32-bit components");
|
||||||
memcpy(i, &key.value, sizeof(i));
|
memcpy(i, &key.value, sizeof(i));
|
||||||
|
|
||||||
// scramble bits to make sure that integer coordinates have entropy in lower bits
|
// scramble bits to make sure that integer coordinates have entropy in lower bits
|
||||||
i[0] ^= i[0] >> 17;
|
i[0] ^= i[0] >> 17;
|
||||||
i[1] ^= i[1] >> 17;
|
i[1] ^= i[1] >> 17;
|
||||||
i[2] ^= i[2] >> 17;
|
i[2] ^= i[2] >> 17;
|
||||||
|
i[3] ^= i[3] >> 17;
|
||||||
|
|
||||||
// Optimized Spatial Hashing for Collision Detection of Deformable Objects
|
// Optimized Spatial Hashing for Collision Detection of Deformable Objects
|
||||||
uint32_t h = (i[0] * 73856093) ^ (i[1] * 19349663) ^ (i[2] * 83492791);
|
uint32_t h = (i[0] * 73856093) ^ (i[1] * 19349663) ^ (i[2] * 83492791) ^ (i[3] * 39916801);
|
||||||
|
|
||||||
return size_t(h);
|
return size_t(h);
|
||||||
}
|
}
|
||||||
|
@ -355,18 +356,20 @@ int32_t BytecodeBuilder::addConstantNumber(double value)
|
||||||
return addConstant(k, c);
|
return addConstant(k, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t BytecodeBuilder::addConstantVector(float x, float y, float z)
|
int32_t BytecodeBuilder::addConstantVector(float x, float y, float z, float w)
|
||||||
{
|
{
|
||||||
Constant c = {Constant::Type_Vector};
|
Constant c = {Constant::Type_Vector};
|
||||||
c.valueVector[0] = x;
|
c.valueVector[0] = x;
|
||||||
c.valueVector[1] = y;
|
c.valueVector[1] = y;
|
||||||
c.valueVector[2] = z;
|
c.valueVector[2] = z;
|
||||||
|
c.valueVector[3] = w;
|
||||||
|
|
||||||
ConstantKey k = {Constant::Type_Vector};
|
ConstantKey k = {Constant::Type_Vector};
|
||||||
static_assert(sizeof(k.value) == sizeof(x) + sizeof(y) && sizeof(k.extra) == sizeof(z), "Expecting vector to have three 32-bit components");
|
static_assert(sizeof(k.value) == sizeof(x) + sizeof(y) && sizeof(k.extra) == sizeof(z) + sizeof(w), "Expecting vector to have four 32-bit components");
|
||||||
memcpy(&k.value, &x, sizeof(x));
|
memcpy(&k.value, &x, sizeof(x));
|
||||||
memcpy((char*)&k.value + sizeof(x), &y, sizeof(y));
|
memcpy((char*)&k.value + sizeof(x), &y, sizeof(y));
|
||||||
memcpy(&k.extra, &z, sizeof(z));
|
memcpy(&k.extra, &z, sizeof(z));
|
||||||
|
memcpy((char*)&k.extra + sizeof(z), &w, sizeof(w));
|
||||||
|
|
||||||
return addConstant(k, c);
|
return addConstant(k, c);
|
||||||
}
|
}
|
||||||
|
@ -693,6 +696,7 @@ void BytecodeBuilder::writeFunction(std::string& ss, uint32_t id, uint8_t flags)
|
||||||
writeFloat(ss, c.valueVector[0]);
|
writeFloat(ss, c.valueVector[0]);
|
||||||
writeFloat(ss, c.valueVector[1]);
|
writeFloat(ss, c.valueVector[1]);
|
||||||
writeFloat(ss, c.valueVector[2]);
|
writeFloat(ss, c.valueVector[2]);
|
||||||
|
writeFloat(ss, c.valueVector[3]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Constant::Type_String:
|
case Constant::Type_String:
|
||||||
|
@ -1684,7 +1688,11 @@ void BytecodeBuilder::dumpConstant(std::string& result, int k) const
|
||||||
formatAppend(result, "%.17g", data.valueNumber);
|
formatAppend(result, "%.17g", data.valueNumber);
|
||||||
break;
|
break;
|
||||||
case Constant::Type_Vector:
|
case Constant::Type_Vector:
|
||||||
|
// 3-vectors is the most common configuration, so truncate to three components if possible
|
||||||
|
if (data.valueVector[3] == 0.0)
|
||||||
formatAppend(result, "%.9g, %.9g, %.9g", data.valueVector[0], data.valueVector[1], data.valueVector[2]);
|
formatAppend(result, "%.9g, %.9g, %.9g", data.valueVector[0], data.valueVector[1], data.valueVector[2]);
|
||||||
|
else
|
||||||
|
formatAppend(result, "%.9g, %.9g, %.9g, %.9g", data.valueVector[0], data.valueVector[1], data.valueVector[2], data.valueVector[3]);
|
||||||
break;
|
break;
|
||||||
case Constant::Type_String:
|
case Constant::Type_String:
|
||||||
{
|
{
|
||||||
|
|
|
@ -1238,7 +1238,7 @@ struct Compiler
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Constant::Type_Vector:
|
case Constant::Type_Vector:
|
||||||
cid = bytecode.addConstantVector(c->valueVector[0], c->valueVector[1], c->valueVector[2]);
|
cid = bytecode.addConstantVector(c->valueVector[0], c->valueVector[1], c->valueVector[2], c->valueVector[3]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Constant::Type_String:
|
case Constant::Type_String:
|
||||||
|
@ -2069,7 +2069,7 @@ struct Compiler
|
||||||
|
|
||||||
case Constant::Type_Vector:
|
case Constant::Type_Vector:
|
||||||
{
|
{
|
||||||
int32_t cid = bytecode.addConstantVector(cv->valueVector[0], cv->valueVector[1], cv->valueVector[2]);
|
int32_t cid = bytecode.addConstantVector(cv->valueVector[0], cv->valueVector[1], cv->valueVector[2], cv->valueVector[3]);
|
||||||
emitLoadK(target, cid);
|
emitLoadK(target, cid);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -30,7 +30,8 @@ static bool constantsEqual(const Constant& la, const Constant& ra)
|
||||||
return ra.type == Constant::Type_Vector &&
|
return ra.type == Constant::Type_Vector &&
|
||||||
la.valueVector[0] == ra.valueVector[0] &&
|
la.valueVector[0] == ra.valueVector[0] &&
|
||||||
la.valueVector[1] == ra.valueVector[1] &&
|
la.valueVector[1] == ra.valueVector[1] &&
|
||||||
la.valueVector[2] == ra.valueVector[2];
|
la.valueVector[2] == ra.valueVector[2] &&
|
||||||
|
la.valueVector[3] == ra.valueVector[3];
|
||||||
|
|
||||||
case Constant::Type_String:
|
case Constant::Type_String:
|
||||||
return ra.type == Constant::Type_String && la.stringLength == ra.stringLength && memcmp(la.valueString, ra.valueString, la.stringLength) == 0;
|
return ra.type == Constant::Type_String && la.stringLength == ra.stringLength && memcmp(la.valueString, ra.valueString, la.stringLength) == 0;
|
||||||
|
|
|
@ -27,7 +27,7 @@ struct Constant
|
||||||
{
|
{
|
||||||
bool valueBoolean;
|
bool valueBoolean;
|
||||||
double valueNumber;
|
double valueNumber;
|
||||||
float valueVector[3];
|
float valueVector[4];
|
||||||
const char* valueString = nullptr; // length stored in stringLength
|
const char* valueString = nullptr; // length stored in stringLength
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -292,7 +292,8 @@ int luau_load(lua_State* L, const char* chunkname, const char* data, size_t size
|
||||||
float x = read<float>(data, size, offset);
|
float x = read<float>(data, size, offset);
|
||||||
float y = read<float>(data, size, offset);
|
float y = read<float>(data, size, offset);
|
||||||
float z = read<float>(data, size, offset);
|
float z = read<float>(data, size, offset);
|
||||||
setvvalue(&p->k[j], x, y, z, 0.0f);
|
float w = read<float>(data, size, offset);
|
||||||
|
setvvalue(&p->k[j], x, y, z, w);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4494,6 +4494,13 @@ GETIMPORT R0 1 [print]
|
||||||
LOADK R1 K2 [1, 2, 3]
|
LOADK R1 K2 [1, 2, 3]
|
||||||
CALL R0 1 0
|
CALL R0 1 0
|
||||||
RETURN R0 0
|
RETURN R0 0
|
||||||
|
)");
|
||||||
|
|
||||||
|
CHECK_EQ("\n" + compileFunction("print(Vector3.new(1, 2, 3, 4))", 0, 2, /*enableVectors*/ true), R"(
|
||||||
|
GETIMPORT R0 1 [print]
|
||||||
|
LOADK R1 K2 [1, 2, 3, 4]
|
||||||
|
CALL R0 1 0
|
||||||
|
RETURN R0 0
|
||||||
)");
|
)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue