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

Improve performance of certain physics queries when using Jolt Physics #101071

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
126 changes: 126 additions & 0 deletions modules/jolt_physics/misc/jolt_inline_vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**************************************************************************/
/* jolt_inline_vector.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef JOLT_INLINE_VECTOR_H
#define JOLT_INLINE_VECTOR_H

#include "core/error/error_macros.h"

#include "Jolt/Jolt.h"

template <typename T, int TInlineCapacity>
class JoltInlineVector {
alignas(T) uint8_t inline_buffer[sizeof(T) * TInlineCapacity];
JPH::Array<T> dynamic_buffer;
T *data = (T *)inline_buffer;
int count = 0;
int capacity = TInlineCapacity;

void _switch_to_dynamic(int p_new_capacity) {
dynamic_buffer.reserve(p_new_capacity);

if constexpr (std::is_trivially_copyable_v<T>) {
memcpy(dynamic_buffer.data(), data, sizeof(T) * count);
} else {
for (int i = 0; i < count; ++i) {
new (dynamic_buffer.data() + i) T(std::move(data[i]));
data[i].~T();
}
}

data = dynamic_buffer.data();
capacity = dynamic_buffer.capacity();
}

public:
JoltInlineVector() = default;

JoltInlineVector(const JoltInlineVector &p_other) = delete;
JoltInlineVector(JoltInlineVector &&p_other) = delete;

JoltInlineVector &operator=(const JoltInlineVector &p_other) = delete;
JoltInlineVector &operator=(JoltInlineVector &&p_other) = delete;

~JoltInlineVector() { clear(); }

int size() const { return count; }

void resize(int p_size) {
ERR_FAIL_COND(p_size < 0);

if (p_size > capacity) {
_switch_to_dynamic(p_size);
}

if (p_size > count) {
for (int i = count; i < p_size; ++i) {
new (data + i) T();
}
} else if (p_size < count) {
for (int i = p_size; i < count; ++i) {
data[i].~T();
}
}

count = p_size;
}

void clear() { resize(0); }

void insert(int p_index, T p_val) {
ERR_FAIL_INDEX(p_index, count + 1);

if (count == capacity) {
_switch_to_dynamic(capacity + 1);
}

for (int i = count; i > p_index; i--) {
new (data + i) T(std::move(data[i - 1]));
data[i - 1].~T();
}

new (data + p_index) T(std::move(p_val));
count++;
}

void push_back(T p_val) { insert(count, std::move(p_val)); }

const T &operator[](int p_index) const {
CRASH_BAD_INDEX(p_index, count);
return data[p_index];
}

T &operator[](int p_index) {
CRASH_BAD_INDEX(p_index, count);
return data[p_index];
}
};

#endif // JOLT_INLINE_VECTOR_H
23 changes: 11 additions & 12 deletions modules/jolt_physics/spaces/jolt_query_collectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@
#define JOLT_QUERY_COLLECTORS_H

#include "../jolt_project_settings.h"
#include "../misc/jolt_inline_vector.h"
#include "jolt_space_3d.h"

#include "core/templates/local_vector.h"

#include "Jolt/Jolt.h"

#include "Jolt/Physics/Collision/InternalEdgeRemovingCollector.h"
Expand All @@ -47,7 +46,7 @@ class JoltQueryCollectorAll final : public TBase {
typedef typename TBase::ResultType Hit;

private:
JPH::Array<Hit> hits;
JoltInlineVector<Hit, TDefaultCapacity> hits;

public:
bool had_hit() const {
Expand Down Expand Up @@ -111,7 +110,7 @@ class JoltQueryCollectorAnyMulti final : public TBase {
typedef typename TBase::ResultType Hit;

private:
JPH::Array<Hit> hits;
JoltInlineVector<Hit, TDefaultCapacity> hits;
int max_hits = 0;

public:
Expand Down Expand Up @@ -140,11 +139,11 @@ class JoltQueryCollectorAnyMulti final : public TBase {
}

virtual void AddHit(const Hit &p_hit) override {
if ((int)hits.size() < max_hits) {
if (hits.size() < max_hits) {
hits.push_back(p_hit);
}

if ((int)hits.size() == max_hits) {
if (hits.size() == max_hits) {
TBase::ForceEarlyOut();
}
}
Expand Down Expand Up @@ -191,7 +190,7 @@ class JoltQueryCollectorClosestMulti final : public TBase {
typedef typename TBase::ResultType Hit;

private:
JPH::Array<Hit> hits;
JoltInlineVector<Hit, TDefaultCapacity + 1> hits;
int max_hits = 0;

public:
Expand Down Expand Up @@ -220,16 +219,16 @@ class JoltQueryCollectorClosestMulti final : public TBase {
}

virtual void AddHit(const Hit &p_hit) override {
typename JPH::Array<Hit>::const_iterator E = hits.cbegin();
for (; E != hits.cend(); ++E) {
if (p_hit.GetEarlyOutFraction() < E->GetEarlyOutFraction()) {
int i = 0;
for (; i < hits.size(); ++i) {
if (p_hit.GetEarlyOutFraction() < hits[i].GetEarlyOutFraction()) {
break;
}
}

hits.insert(E, p_hit);
hits.insert(i, p_hit);

if ((int)hits.size() > max_hits) {
if (hits.size() > max_hits) {
hits.resize(max_hits);
}
}
Expand Down