Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PackedVector4Array Variant type #85474

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/core_constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ void register_global_constants() {
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_PACKED_VECTOR2_ARRAY", Variant::PACKED_VECTOR2_ARRAY);
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_PACKED_VECTOR3_ARRAY", Variant::PACKED_VECTOR3_ARRAY);
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_PACKED_COLOR_ARRAY", Variant::PACKED_COLOR_ARRAY);
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_PACKED_VECTOR4_ARRAY", Variant::PACKED_VECTOR4_ARRAY);
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_MAX", Variant::VARIANT_MAX);

//comparison
Expand Down
2 changes: 2 additions & 0 deletions core/extension/extension_api_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
{ Variant::PACKED_VECTOR2_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
{ Variant::PACKED_VECTOR3_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
{ Variant::PACKED_COLOR_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
{ Variant::PACKED_VECTOR4_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
{ Variant::VARIANT_MAX, sizeof(uint64_t) + sizeof(float) * 4, sizeof(uint64_t) + sizeof(float) * 4, sizeof(uint64_t) + sizeof(double) * 4, sizeof(uint64_t) + sizeof(double) * 4 },
};

Expand Down Expand Up @@ -230,6 +231,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
static_assert(type_size_array[Variant::PACKED_VECTOR2_ARRAY][sizeof(void *)] == sizeof(PackedVector2Array), "Size of PackedVector2Array mismatch");
static_assert(type_size_array[Variant::PACKED_VECTOR3_ARRAY][sizeof(void *)] == sizeof(PackedVector3Array), "Size of PackedVector3Array mismatch");
static_assert(type_size_array[Variant::PACKED_COLOR_ARRAY][sizeof(void *)] == sizeof(PackedColorArray), "Size of PackedColorArray mismatch");
static_assert(type_size_array[Variant::PACKED_VECTOR4_ARRAY][sizeof(void *)] == sizeof(PackedVector4Array), "Size of PackedVector4Array mismatch");
static_assert(type_size_array[Variant::VARIANT_MAX][sizeof(void *)] == sizeof(Variant), "Size of Variant mismatch");

Array core_type_sizes;
Expand Down
22 changes: 22 additions & 0 deletions core/extension/gdextension_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@ static GDExtensionVariantFromTypeConstructorFunc gdextension_get_variant_from_ty
return VariantTypeConstructor<PackedVector2Array>::variant_from_type;
case GDEXTENSION_VARIANT_TYPE_PACKED_VECTOR3_ARRAY:
return VariantTypeConstructor<PackedVector3Array>::variant_from_type;
case GDEXTENSION_VARIANT_TYPE_PACKED_VECTOR4_ARRAY:
return VariantTypeConstructor<PackedVector4Array>::variant_from_type;
case GDEXTENSION_VARIANT_TYPE_PACKED_COLOR_ARRAY:
return VariantTypeConstructor<PackedColorArray>::variant_from_type;
case GDEXTENSION_VARIANT_TYPE_NIL:
Expand Down Expand Up @@ -678,6 +680,8 @@ static GDExtensionTypeFromVariantConstructorFunc gdextension_get_variant_to_type
return VariantTypeConstructor<PackedVector2Array>::type_from_variant;
case GDEXTENSION_VARIANT_TYPE_PACKED_VECTOR3_ARRAY:
return VariantTypeConstructor<PackedVector3Array>::type_from_variant;
case GDEXTENSION_VARIANT_TYPE_PACKED_VECTOR4_ARRAY:
return VariantTypeConstructor<PackedVector4Array>::type_from_variant;
case GDEXTENSION_VARIANT_TYPE_PACKED_COLOR_ARRAY:
return VariantTypeConstructor<PackedColorArray>::type_from_variant;
case GDEXTENSION_VARIANT_TYPE_NIL:
Expand Down Expand Up @@ -1116,6 +1120,22 @@ static GDExtensionTypePtr gdextension_packed_vector3_array_operator_index_const(
return (GDExtensionTypePtr)&self->ptr()[p_index];
}

static GDExtensionTypePtr gdextension_packed_vector4_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
PackedVector4Array *self = (PackedVector4Array *)p_self;
if (unlikely(p_index < 0 || p_index >= self->size())) {
return nullptr;
}
return (GDExtensionTypePtr)&self->ptrw()[p_index];
}

static GDExtensionTypePtr gdextension_packed_vector4_array_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionInt p_index) {
const PackedVector4Array *self = (const PackedVector4Array *)p_self;
if (unlikely(p_index < 0 || p_index >= self->size())) {
return nullptr;
}
return (GDExtensionTypePtr)&self->ptr()[p_index];
}

static GDExtensionVariantPtr gdextension_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
Array *self = (Array *)p_self;
if (unlikely(p_index < 0 || p_index >= self->size())) {
Expand Down Expand Up @@ -1620,6 +1640,8 @@ void gdextension_setup_interface() {
REGISTER_INTERFACE_FUNC(packed_vector2_array_operator_index_const);
REGISTER_INTERFACE_FUNC(packed_vector3_array_operator_index);
REGISTER_INTERFACE_FUNC(packed_vector3_array_operator_index_const);
REGISTER_INTERFACE_FUNC(packed_vector4_array_operator_index);
REGISTER_INTERFACE_FUNC(packed_vector4_array_operator_index_const);
REGISTER_INTERFACE_FUNC(array_operator_index);
REGISTER_INTERFACE_FUNC(array_operator_index_const);
REGISTER_INTERFACE_FUNC(array_ref);
Expand Down
79 changes: 53 additions & 26 deletions core/extension/gdextension_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ typedef enum {
GDEXTENSION_VARIANT_TYPE_PACKED_VECTOR2_ARRAY,
GDEXTENSION_VARIANT_TYPE_PACKED_VECTOR3_ARRAY,
GDEXTENSION_VARIANT_TYPE_PACKED_COLOR_ARRAY,
GDEXTENSION_VARIANT_TYPE_PACKED_VECTOR4_ARRAY,

GDEXTENSION_VARIANT_TYPE_VARIANT_MAX
} GDExtensionVariantType;
Expand Down Expand Up @@ -1963,32 +1964,6 @@ typedef uint8_t *(*GDExtensionInterfacePackedByteArrayOperatorIndex)(GDExtension
*/
typedef const uint8_t *(*GDExtensionInterfacePackedByteArrayOperatorIndexConst)(GDExtensionConstTypePtr p_self, GDExtensionInt p_index);

/**
* @name packed_color_array_operator_index
* @since 4.1
*
* Gets a pointer to a color in a PackedColorArray.
*
* @param p_self A pointer to a PackedColorArray object.
* @param p_index The index of the Color to get.
*
* @return A pointer to the requested Color.
*/
typedef GDExtensionTypePtr (*GDExtensionInterfacePackedColorArrayOperatorIndex)(GDExtensionTypePtr p_self, GDExtensionInt p_index);

/**
* @name packed_color_array_operator_index_const
* @since 4.1
*
* Gets a const pointer to a color in a PackedColorArray.
*
* @param p_self A const pointer to a const PackedColorArray object.
* @param p_index The index of the Color to get.
*
* @return A const pointer to the requested Color.
*/
typedef GDExtensionTypePtr (*GDExtensionInterfacePackedColorArrayOperatorIndexConst)(GDExtensionConstTypePtr p_self, GDExtensionInt p_index);

/**
* @name packed_float32_array_operator_index
* @since 4.1
Expand Down Expand Up @@ -2171,6 +2146,58 @@ typedef GDExtensionTypePtr (*GDExtensionInterfacePackedVector3ArrayOperatorIndex
*/
typedef GDExtensionTypePtr (*GDExtensionInterfacePackedVector3ArrayOperatorIndexConst)(GDExtensionConstTypePtr p_self, GDExtensionInt p_index);

/**
* @name packed_vector4_array_operator_index
* @since 4.3
*
* Gets a pointer to a Vector4 in a PackedVector4Array.
*
* @param p_self A pointer to a PackedVector4Array object.
* @param p_index The index of the Vector4 to get.
*
* @return A pointer to the requested Vector4.
*/
typedef GDExtensionTypePtr (*GDExtensionInterfacePackedVector4ArrayOperatorIndex)(GDExtensionTypePtr p_self, GDExtensionInt p_index);

/**
* @name packed_vector4_array_operator_index_const
* @since 4.3
*
* Gets a const pointer to a Vector4 in a PackedVector4Array.
*
* @param p_self A const pointer to a PackedVector4Array object.
* @param p_index The index of the Vector4 to get.
*
* @return A const pointer to the requested Vector4.
*/
typedef GDExtensionTypePtr (*GDExtensionInterfacePackedVector4ArrayOperatorIndexConst)(GDExtensionConstTypePtr p_self, GDExtensionInt p_index);

/**
* @name packed_color_array_operator_index
* @since 4.1
*
* Gets a pointer to a color in a PackedColorArray.
*
* @param p_self A pointer to a PackedColorArray object.
* @param p_index The index of the Color to get.
*
* @return A pointer to the requested Color.
*/
typedef GDExtensionTypePtr (*GDExtensionInterfacePackedColorArrayOperatorIndex)(GDExtensionTypePtr p_self, GDExtensionInt p_index);

/**
* @name packed_color_array_operator_index_const
* @since 4.1
*
* Gets a const pointer to a color in a PackedColorArray.
*
* @param p_self A const pointer to a PackedColorArray object.
* @param p_index The index of the Color to get.
*
* @return A const pointer to the requested Color.
*/
typedef GDExtensionTypePtr (*GDExtensionInterfacePackedColorArrayOperatorIndexConst)(GDExtensionConstTypePtr p_self, GDExtensionInt p_index);

/**
* @name array_operator_index
* @since 4.1
Expand Down
94 changes: 94 additions & 0 deletions core/io/marshalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,73 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int

r_variant = carray;

} break;

case Variant::PACKED_VECTOR4_ARRAY: {
ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
int32_t count = decode_uint32(buf);
buf += 4;
len -= 4;

Vector<Vector4> varray;

if (header & HEADER_DATA_FLAG_64) {
ERR_FAIL_MUL_OF(count, sizeof(double) * 4, ERR_INVALID_DATA);
ERR_FAIL_COND_V(count < 0 || count * sizeof(double) * 4 > (size_t)len, ERR_INVALID_DATA);

if (r_len) {
(*r_len) += 4; // Size of count number.
}

if (count) {
varray.resize(count);
Vector4 *w = varray.ptrw();

for (int32_t i = 0; i < count; i++) {
w[i].x = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 0);
w[i].y = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 1);
w[i].z = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 2);
w[i].w = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 3);
}

int adv = sizeof(double) * 4 * count;

if (r_len) {
(*r_len) += adv;
}
len -= adv;
buf += adv;
}
} else {
ERR_FAIL_MUL_OF(count, sizeof(float) * 4, ERR_INVALID_DATA);
ERR_FAIL_COND_V(count < 0 || count * sizeof(float) * 4 > (size_t)len, ERR_INVALID_DATA);

if (r_len) {
(*r_len) += 4; // Size of count number.
}

if (count) {
varray.resize(count);
Vector4 *w = varray.ptrw();

for (int32_t i = 0; i < count; i++) {
w[i].x = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 0);
w[i].y = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 1);
w[i].z = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 2);
w[i].w = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 3);
}

int adv = sizeof(float) * 4 * count;

if (r_len) {
(*r_len) += adv;
}
len -= adv;
buf += adv;
}
}
r_variant = varray;

} break;
default: {
ERR_FAIL_V(ERR_BUG);
Expand Down Expand Up @@ -1263,6 +1330,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
case Variant::VECTOR4:
case Variant::PACKED_VECTOR2_ARRAY:
case Variant::PACKED_VECTOR3_ARRAY:
case Variant::PACKED_VECTOR4_ARRAY:
case Variant::TRANSFORM2D:
case Variant::TRANSFORM3D:
case Variant::PROJECTION:
Expand Down Expand Up @@ -1946,6 +2014,32 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo

r_len += 4 * 4 * len;

} break;
case Variant::PACKED_VECTOR4_ARRAY: {
Vector<Vector4> data = p_variant;
int len = data.size();

if (buf) {
encode_uint32(len, buf);
buf += 4;
}

r_len += 4;

if (buf) {
for (int i = 0; i < len; i++) {
Vector4 v = data.get(i);

encode_real(v.x, &buf[0]);
encode_real(v.y, &buf[sizeof(real_t)]);
encode_real(v.z, &buf[sizeof(real_t) * 2]);
encode_real(v.w, &buf[sizeof(real_t) * 3]);
buf += sizeof(real_t) * 4;
}
}

r_len += sizeof(real_t) * 4 * len;

} break;
default: {
ERR_FAIL_V(ERR_BUG);
Expand Down
1 change: 1 addition & 0 deletions core/io/packed_data_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpd
case Variant::PACKED_VECTOR2_ARRAY:
case Variant::PACKED_VECTOR3_ARRAY:
case Variant::PACKED_COLOR_ARRAY:
case Variant::PACKED_VECTOR4_ARRAY:
case Variant::STRING_NAME:
case Variant::NODE_PATH: {
uint32_t pos = tmpdata.size();
Expand Down
3 changes: 2 additions & 1 deletion core/io/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ Ref<Resource> Resource::duplicate(bool p_subresources) const {
case Variant::Type::PACKED_FLOAT64_ARRAY:
case Variant::Type::PACKED_STRING_ARRAY:
case Variant::Type::PACKED_VECTOR2_ARRAY:
case Variant::Type::PACKED_VECTOR3_ARRAY: {
case Variant::Type::PACKED_VECTOR3_ARRAY:
case Variant::Type::PACKED_VECTOR4_ARRAY: {
r->set(E.name, p.duplicate(p_subresources));
} break;

Expand Down
Loading
Loading