From 1ba168fcbce192c3a6e4aec3bddb895d13d050cf Mon Sep 17 00:00:00 2001 From: Nazarii Date: Sun, 29 Sep 2024 19:36:56 +0300 Subject: [PATCH] Use local vector for skeleton --- drivers/gles3/storage/mesh_storage.cpp | 6 +- drivers/gles3/storage/mesh_storage.h | 2 +- scene/2d/skeleton_2d.cpp | 44 +++++----- scene/2d/skeleton_2d.h | 2 +- scene/3d/skeleton_3d.cpp | 82 +++++++++---------- scene/3d/skeleton_3d.h | 2 +- .../renderer_rd/storage_rd/mesh_storage.cpp | 7 +- .../renderer_rd/storage_rd/mesh_storage.h | 2 +- 8 files changed, 73 insertions(+), 74 deletions(-) diff --git a/drivers/gles3/storage/mesh_storage.cpp b/drivers/gles3/storage/mesh_storage.cpp index 50585546590..73d95d75baf 100644 --- a/drivers/gles3/storage/mesh_storage.cpp +++ b/drivers/gles3/storage/mesh_storage.cpp @@ -2201,7 +2201,7 @@ void MeshStorage::skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_ glBindTexture(GL_TEXTURE_2D, 0); GLES3::Utilities::get_singleton()->texture_allocated_data(skeleton->transforms_texture, skeleton->data.size() * sizeof(float), "Skeleton transforms texture"); - memset(skeleton->data.ptrw(), 0, skeleton->data.size() * sizeof(float)); + memset(skeleton->data.ptr(), 0, skeleton->data.size() * sizeof(float)); _skeleton_make_dirty(skeleton); } @@ -2232,7 +2232,7 @@ void MeshStorage::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const ERR_FAIL_INDEX(p_bone, skeleton->size); ERR_FAIL_COND(skeleton->use_2d); - float *dataptr = skeleton->data.ptrw() + p_bone * 12; + float *dataptr = skeleton->data.ptr() + p_bone * 12; dataptr[0] = p_transform.basis.rows[0][0]; dataptr[1] = p_transform.basis.rows[0][1]; @@ -2284,7 +2284,7 @@ void MeshStorage::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, con ERR_FAIL_INDEX(p_bone, skeleton->size); ERR_FAIL_COND(!skeleton->use_2d); - float *dataptr = skeleton->data.ptrw() + p_bone * 8; + float *dataptr = skeleton->data.ptr() + p_bone * 8; dataptr[0] = p_transform.columns[0][0]; dataptr[1] = p_transform.columns[1][0]; diff --git a/drivers/gles3/storage/mesh_storage.h b/drivers/gles3/storage/mesh_storage.h index 31858cd372c..8615b89a30d 100644 --- a/drivers/gles3/storage/mesh_storage.h +++ b/drivers/gles3/storage/mesh_storage.h @@ -214,7 +214,7 @@ struct Skeleton { bool use_2d = false; int size = 0; int height = 0; - Vector data; + LocalVector data; bool dirty = false; Skeleton *dirty_list = nullptr; diff --git a/scene/2d/skeleton_2d.cpp b/scene/2d/skeleton_2d.cpp index 90bfb4c84c8..39cfccf983f 100644 --- a/scene/2d/skeleton_2d.cpp +++ b/scene/2d/skeleton_2d.cpp @@ -159,7 +159,7 @@ void Bone2D::_notification(int p_what) { case NOTIFICATION_EXIT_TREE: { if (skeleton) { - for (int i = 0; i < skeleton->bones.size(); i++) { + for (uint32_t i = 0; i < skeleton->bones.size(); i++) { if (skeleton->bones[i].bone == this) { skeleton->bones.remove_at(i); break; @@ -555,17 +555,17 @@ void Skeleton2D::_update_bone_setup() { bones.sort(); //sorting so that they are always in the same order/index - for (int i = 0; i < bones.size(); i++) { - bones.write[i].rest_inverse = bones[i].bone->get_skeleton_rest().affine_inverse(); //bind pose - bones.write[i].bone->skeleton_index = i; + for (uint32_t i = 0; i < bones.size(); i++) { + bones[i].rest_inverse = bones[i].bone->get_skeleton_rest().affine_inverse(); //bind pose + bones[i].bone->skeleton_index = i; Bone2D *parent_bone = Object::cast_to(bones[i].bone->get_parent()); if (parent_bone) { - bones.write[i].parent_index = parent_bone->skeleton_index; + bones[i].parent_index = parent_bone->skeleton_index; } else { - bones.write[i].parent_index = -1; + bones[i].parent_index = -1; } - bones.write[i].local_pose_override = bones[i].bone->get_skeleton_rest(); + bones[i].local_pose_override = bones[i].bone->get_skeleton_rest(); } transform_dirty = true; @@ -594,16 +594,16 @@ void Skeleton2D::_update_transform() { transform_dirty = false; - for (int i = 0; i < bones.size(); i++) { - ERR_CONTINUE(bones[i].parent_index >= i); + for (uint32_t i = 0; i < bones.size(); i++) { + ERR_CONTINUE(bones[i].parent_index >= (int)i); if (bones[i].parent_index >= 0) { - bones.write[i].accum_transform = bones[bones[i].parent_index].accum_transform * bones[i].bone->get_transform(); + bones[i].accum_transform = bones[bones[i].parent_index].accum_transform * bones[i].bone->get_transform(); } else { - bones.write[i].accum_transform = bones[i].bone->get_transform(); + bones[i].accum_transform = bones[i].bone->get_transform(); } } - for (int i = 0; i < bones.size(); i++) { + for (uint32_t i = 0; i < bones.size(); i++) { Transform2D final_xform = bones[i].accum_transform * bones[i].rest_inverse; RS::get_singleton()->skeleton_bone_set_transform_2d(skeleton, i, final_xform); } @@ -621,7 +621,7 @@ int Skeleton2D::get_bone_count() const { Bone2D *Skeleton2D::get_bone(int p_idx) { ERR_FAIL_COND_V(!is_inside_tree(), nullptr); - ERR_FAIL_INDEX_V(p_idx, bones.size(), nullptr); + ERR_FAIL_INDEX_V(p_idx, (int)bones.size(), nullptr); return bones[p_idx].bone; } @@ -733,14 +733,14 @@ RID Skeleton2D::get_skeleton() const { } void Skeleton2D::set_bone_local_pose_override(int p_bone_idx, Transform2D p_override, real_t p_amount, bool p_persistent) { - ERR_FAIL_INDEX_MSG(p_bone_idx, bones.size(), "Bone index is out of range!"); - bones.write[p_bone_idx].local_pose_override = p_override; - bones.write[p_bone_idx].local_pose_override_amount = p_amount; - bones.write[p_bone_idx].local_pose_override_persistent = p_persistent; + ERR_FAIL_INDEX_MSG(p_bone_idx, (int)bones.size(), "Bone index is out of range!"); + bones[p_bone_idx].local_pose_override = p_override; + bones[p_bone_idx].local_pose_override_amount = p_amount; + bones[p_bone_idx].local_pose_override_persistent = p_persistent; } Transform2D Skeleton2D::get_bone_local_pose_override(int p_bone_idx) { - ERR_FAIL_INDEX_V_MSG(p_bone_idx, bones.size(), Transform2D(), "Bone index is out of range!"); + ERR_FAIL_INDEX_V_MSG(p_bone_idx, (int)bones.size(), Transform2D(), "Bone index is out of range!"); return bones[p_bone_idx].local_pose_override; } @@ -771,7 +771,7 @@ void Skeleton2D::execute_modifications(real_t p_delta, int p_execution_mode) { } // Do not cache the transform changes caused by the modifications! - for (int i = 0; i < bones.size(); i++) { + for (uint32_t i = 0; i < bones.size(); i++) { bones[i].bone->copy_transform_to_cache = false; } @@ -783,7 +783,7 @@ void Skeleton2D::execute_modifications(real_t p_delta, int p_execution_mode) { // Only apply the local pose override on _process. Otherwise, just calculate the local_pose_override and reset the transform. if (p_execution_mode == SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_process) { - for (int i = 0; i < bones.size(); i++) { + for (uint32_t i = 0; i < bones.size(); i++) { if (bones[i].local_pose_override_amount > 0) { bones[i].bone->set_meta("_local_pose_override_enabled_", true); @@ -793,7 +793,7 @@ void Skeleton2D::execute_modifications(real_t p_delta, int p_execution_mode) { bones[i].bone->propagate_call("force_update_transform"); if (bones[i].local_pose_override_persistent) { - bones.write[i].local_pose_override_amount = 0.0; + bones[i].local_pose_override_amount = 0.0; } } else { // TODO: see if there is a way to undo the override without having to resort to setting every bone's transform. @@ -804,7 +804,7 @@ void Skeleton2D::execute_modifications(real_t p_delta, int p_execution_mode) { } // Cache any future transform changes - for (int i = 0; i < bones.size(); i++) { + for (uint32_t i = 0; i < bones.size(); i++) { bones[i].bone->copy_transform_to_cache = true; } diff --git a/scene/2d/skeleton_2d.h b/scene/2d/skeleton_2d.h index 033bdff41d6..16bd6fd34bc 100644 --- a/scene/2d/skeleton_2d.h +++ b/scene/2d/skeleton_2d.h @@ -123,7 +123,7 @@ class Skeleton2D : public Node2D { bool local_pose_override_persistent = false; }; - Vector bones; + LocalVector bones; bool bone_setup_dirty = true; void _make_bone_setup_dirty(); diff --git a/scene/3d/skeleton_3d.cpp b/scene/3d/skeleton_3d.cpp index db9c4db30d0..f778fccf095 100644 --- a/scene/3d/skeleton_3d.cpp +++ b/scene/3d/skeleton_3d.cpp @@ -81,7 +81,7 @@ bool Skeleton3D::_set(const StringName &p_path, const Variant &p_value) { return false; } - int which = path.get_slicec('/', 1).to_int(); + uint32_t which = path.get_slicec('/', 1).to_int(); String what = path.get_slicec('/', 2); if (which == bones.size() && what == "name") { @@ -89,7 +89,7 @@ bool Skeleton3D::_set(const StringName &p_path, const Variant &p_value) { return true; } - ERR_FAIL_INDEX_V(which, bones.size(), false); + ERR_FAIL_UNSIGNED_INDEX_V(which, bones.size(), false); if (what == "parent") { set_bone_parent(which, p_value); @@ -153,10 +153,10 @@ bool Skeleton3D::_get(const StringName &p_path, Variant &r_ret) const { return false; } - int which = path.get_slicec('/', 1).to_int(); + uint32_t which = path.get_slicec('/', 1).to_int(); String what = path.get_slicec('/', 2); - ERR_FAIL_INDEX_V(which, bones.size(), false); + ERR_FAIL_UNSIGNED_INDEX_V(which, bones.size(), false); if (what == "name") { r_ret = get_bone_name(which); @@ -182,7 +182,7 @@ bool Skeleton3D::_get(const StringName &p_path, Variant &r_ret) const { } void Skeleton3D::_get_property_list(List *p_list) const { - for (int i = 0; i < bones.size(); i++) { + for (uint32_t i = 0; i < bones.size(); i++) { const String prep = vformat("%s/%d/", PNAME("bones"), i); p_list->push_back(PropertyInfo(Variant::STRING, prep + PNAME("name"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR)); p_list->push_back(PropertyInfo(Variant::INT, prep + PNAME("parent"), PROPERTY_HINT_RANGE, "-1," + itos(bones.size() - 1) + ",1", PROPERTY_USAGE_NO_EDITOR)); @@ -241,7 +241,7 @@ void Skeleton3D::_update_process_order() { return; } - Bone *bonesptr = bones.ptrw(); + Bone *bonesptr = bones.ptr(); int len = bones.size(); parentless_bones.clear(); @@ -283,7 +283,7 @@ void Skeleton3D::_update_process_order() { void Skeleton3D::_update_bone_names() const { String names; - for (int i = 0; i < bones.size(); i++) { + for (uint32_t i = 0; i < bones.size(); i++) { if (i > 0) { names += ","; } @@ -331,14 +331,14 @@ void Skeleton3D::_notification(int p_what) { updating = true; - Bone *bonesptr = bones.ptrw(); + Bone *bonesptr = bones.ptr(); int len = bones.size(); // Process modifiers. _find_modifiers(); if (!modifiers.is_empty()) { // Store unmodified bone poses. - for (int i = 0; i < bones.size(); i++) { + for (uint32_t i = 0; i < bones.size(); i++) { bones_backup[i].save(bones[i]); } _process_modifiers(); @@ -412,8 +412,8 @@ void Skeleton3D::_notification(int p_what) { if (!modifiers.is_empty()) { // Restore unmodified bone poses. - for (int i = 0; i < bones.size(); i++) { - bones_backup[i].restore(bones.write[i]); + for (uint32_t i = 0; i < bones.size(); i++) { + bones_backup[i].restore(bones[i]); } } @@ -534,7 +534,7 @@ void Skeleton3D::set_bone_name(int p_bone, const String &p_name) { } name_to_bone_index.erase(bones[p_bone].name); - bones.write[p_bone].name = p_name; + bones[p_bone].name = p_name; name_to_bone_index.insert(p_name, p_bone); version++; @@ -582,13 +582,13 @@ void Skeleton3D::set_bone_meta(int p_bone, const StringName &p_key, const Varian ERR_FAIL_INDEX(p_bone, bone_size); if (p_value.get_type() == Variant::NIL) { - if (bones.write[p_bone].metadata.has(p_key)) { - bones.write[p_bone].metadata.erase(p_key); + if (bones[p_bone].metadata.has(p_key)) { + bones[p_bone].metadata.erase(p_key); } return; } - bones.write[p_bone].metadata.insert(p_key, p_value, false); + bones[p_bone].metadata.insert(p_key, p_value, false); } bool Skeleton3D::is_bone_parent_of(int p_bone, int p_parent_bone_id) const { @@ -615,7 +615,7 @@ void Skeleton3D::set_bone_parent(int p_bone, int p_parent) { ERR_FAIL_COND(p_parent != -1 && (p_parent < 0)); ERR_FAIL_COND(p_bone == p_parent); - bones.write[p_bone].parent = p_parent; + bones[p_bone].parent = p_parent; process_order_dirty = true; rest_dirty = true; _make_dirty(); @@ -629,11 +629,11 @@ void Skeleton3D::unparent_bone_and_rest(int p_bone) { int parent = bones[p_bone].parent; while (parent >= 0) { - bones.write[p_bone].rest = bones[parent].rest * bones[p_bone].rest; + bones[p_bone].rest = bones[parent].rest * bones[p_bone].rest; parent = bones[parent].parent; } - bones.write[p_bone].parent = -1; + bones[p_bone].parent = -1; process_order_dirty = true; rest_dirty = true; @@ -669,7 +669,7 @@ void Skeleton3D::set_bone_rest(int p_bone, const Transform3D &p_rest) { const int bone_size = bones.size(); ERR_FAIL_INDEX(p_bone, bone_size); - bones.write[p_bone].rest = p_rest; + bones[p_bone].rest = p_rest; rest_dirty = true; _make_dirty(); } @@ -692,7 +692,7 @@ void Skeleton3D::set_bone_enabled(int p_bone, bool p_enabled) { const int bone_size = bones.size(); ERR_FAIL_INDEX(p_bone, bone_size); - bones.write[p_bone].enabled = p_enabled; + bones[p_bone].enabled = p_enabled; emit_signal(SceneStringName(bone_enabled_changed), p_bone); _make_dirty(); } @@ -727,10 +727,10 @@ void Skeleton3D::set_bone_pose(int p_bone, const Transform3D &p_pose) { const int bone_size = bones.size(); ERR_FAIL_INDEX(p_bone, bone_size); - bones.write[p_bone].pose_position = p_pose.origin; - bones.write[p_bone].pose_rotation = p_pose.basis.get_rotation_quaternion(); - bones.write[p_bone].pose_scale = p_pose.basis.get_scale(); - bones.write[p_bone].pose_cache_dirty = true; + bones[p_bone].pose_position = p_pose.origin; + bones[p_bone].pose_rotation = p_pose.basis.get_rotation_quaternion(); + bones[p_bone].pose_scale = p_pose.basis.get_scale(); + bones[p_bone].pose_cache_dirty = true; if (is_inside_tree()) { _make_dirty(); } @@ -740,8 +740,8 @@ void Skeleton3D::set_bone_pose_position(int p_bone, const Vector3 &p_position) { const int bone_size = bones.size(); ERR_FAIL_INDEX(p_bone, bone_size); - bones.write[p_bone].pose_position = p_position; - bones.write[p_bone].pose_cache_dirty = true; + bones[p_bone].pose_position = p_position; + bones[p_bone].pose_cache_dirty = true; if (is_inside_tree()) { _make_dirty(); } @@ -750,8 +750,8 @@ void Skeleton3D::set_bone_pose_rotation(int p_bone, const Quaternion &p_rotation const int bone_size = bones.size(); ERR_FAIL_INDEX(p_bone, bone_size); - bones.write[p_bone].pose_rotation = p_rotation; - bones.write[p_bone].pose_cache_dirty = true; + bones[p_bone].pose_rotation = p_rotation; + bones[p_bone].pose_cache_dirty = true; if (is_inside_tree()) { _make_dirty(); } @@ -760,8 +760,8 @@ void Skeleton3D::set_bone_pose_scale(int p_bone, const Vector3 &p_scale) { const int bone_size = bones.size(); ERR_FAIL_INDEX(p_bone, bone_size); - bones.write[p_bone].pose_scale = p_scale; - bones.write[p_bone].pose_cache_dirty = true; + bones[p_bone].pose_scale = p_scale; + bones[p_bone].pose_cache_dirty = true; if (is_inside_tree()) { _make_dirty(); } @@ -794,7 +794,7 @@ void Skeleton3D::reset_bone_pose(int p_bone) { } void Skeleton3D::reset_bone_poses() { - for (int i = 0; i < bones.size(); i++) { + for (uint32_t i = 0; i < bones.size(); i++) { reset_bone_pose(i); } } @@ -802,7 +802,7 @@ void Skeleton3D::reset_bone_poses() { Transform3D Skeleton3D::get_bone_pose(int p_bone) const { const int bone_size = bones.size(); ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D()); - const_cast(this)->bones.write[p_bone].update_pose_cache(); + const_cast(this)->bones[p_bone].update_pose_cache(); return bones[p_bone].pose_cache; } @@ -853,7 +853,7 @@ Ref Skeleton3D::create_skin_from_rest_transforms() { // Pose changed, rebuild cache of inverses. const Bone *bonesptr = bones.ptr(); - int len = bones.size(); + uint32_t len = bones.size(); // Calculate global rests and invert them. LocalVector bones_to_process; @@ -877,7 +877,7 @@ Ref Skeleton3D::create_skin_from_rest_transforms() { } } - for (int i = 0; i < len; i++) { + for (uint32_t i = 0; i < len; i++) { // The inverse is what is actually required. skin->set_bind_bone(i, i); skin->set_bind_pose(i, skin->get_bind_pose(i).affine_inverse()); @@ -937,7 +937,7 @@ void Skeleton3D::force_update_bone_children_transforms(int p_bone_idx) { const int bone_size = bones.size(); ERR_FAIL_INDEX(p_bone_idx, bone_size); - Bone *bonesptr = bones.ptrw(); + Bone *bonesptr = bones.ptr(); thread_local LocalVector bones_to_process; bones_to_process.clear(); bones_to_process.push_back(p_bone_idx); @@ -1171,9 +1171,9 @@ void Skeleton3D::_bind_methods() { #ifndef DISABLE_DEPRECATED void Skeleton3D::clear_bones_global_pose_override() { - for (int i = 0; i < bones.size(); i += 1) { - bones.write[i].global_pose_override_amount = 0; - bones.write[i].global_pose_override_reset = true; + for (uint32_t i = 0; i < bones.size(); i += 1) { + bones[i].global_pose_override_amount = 0; + bones[i].global_pose_override_reset = true; } _make_dirty(); } @@ -1181,9 +1181,9 @@ void Skeleton3D::clear_bones_global_pose_override() { void Skeleton3D::set_bone_global_pose_override(int p_bone, const Transform3D &p_pose, real_t p_amount, bool p_persistent) { const int bone_size = bones.size(); ERR_FAIL_INDEX(p_bone, bone_size); - bones.write[p_bone].global_pose_override_amount = p_amount; - bones.write[p_bone].global_pose_override = p_pose; - bones.write[p_bone].global_pose_override_reset = !p_persistent; + bones[p_bone].global_pose_override_amount = p_amount; + bones[p_bone].global_pose_override = p_pose; + bones[p_bone].global_pose_override_reset = !p_persistent; _make_dirty(); } diff --git a/scene/3d/skeleton_3d.h b/scene/3d/skeleton_3d.h index 07bdeccf2f7..7213dc580cb 100644 --- a/scene/3d/skeleton_3d.h +++ b/scene/3d/skeleton_3d.h @@ -153,7 +153,7 @@ private: HashSet skin_bindings; void _skin_changed(); - Vector bones; + LocalVector bones; bool process_order_dirty = false; Vector parentless_bones; diff --git a/servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp b/servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp index 9ae39691dc2..a0514e9c956 100644 --- a/servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp +++ b/servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp @@ -2166,7 +2166,7 @@ void MeshStorage::skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_ if (skeleton->size) { skeleton->data.resize(skeleton->size * (skeleton->use_2d ? 8 : 12)); skeleton->buffer = RD::get_singleton()->storage_buffer_create(skeleton->data.size() * sizeof(float)); - memset(skeleton->data.ptrw(), 0, skeleton->data.size() * sizeof(float)); + memset(skeleton->data.ptr(), 0, skeleton->data.size() * sizeof(float)); _skeleton_make_dirty(skeleton); @@ -2200,7 +2200,7 @@ void MeshStorage::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const ERR_FAIL_INDEX(p_bone, skeleton->size); ERR_FAIL_COND(skeleton->use_2d); - float *dataptr = skeleton->data.ptrw() + p_bone * 12; + float *dataptr = skeleton->data.ptr() + p_bone * 12; dataptr[0] = p_transform.basis.rows[0][0]; dataptr[1] = p_transform.basis.rows[0][1]; @@ -2251,8 +2251,7 @@ void MeshStorage::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, con ERR_FAIL_NULL(skeleton); ERR_FAIL_INDEX(p_bone, skeleton->size); ERR_FAIL_COND(!skeleton->use_2d); - - float *dataptr = skeleton->data.ptrw() + p_bone * 8; + float *dataptr = skeleton->data.ptr() + p_bone * 8; dataptr[0] = p_transform.columns[0][0]; dataptr[1] = p_transform.columns[1][0]; diff --git a/servers/rendering/renderer_rd/storage_rd/mesh_storage.h b/servers/rendering/renderer_rd/storage_rd/mesh_storage.h index f811314fb66..cad6a312e77 100644 --- a/servers/rendering/renderer_rd/storage_rd/mesh_storage.h +++ b/servers/rendering/renderer_rd/storage_rd/mesh_storage.h @@ -311,7 +311,7 @@ private: struct Skeleton { bool use_2d = false; int size = 0; - Vector data; + LocalVector data; RID buffer; bool dirty = false;