wip: implement a smaller data model to reduce copying from physics to main thread

This commit is contained in:
2026-02-24 17:05:24 +01:00
parent 1347abad34
commit c4222c783c
13 changed files with 312 additions and 470 deletions

View File

@ -3,22 +3,17 @@
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <mutex>
#include <queue>
#include <raylib.h>
#include <raymath.h>
#include <thread>
#include <tracy/Tracy.hpp>
#include <unordered_map>
#include <variant>
#include <vector>
#include "config.hpp"
#include "puzzle.hpp"
#ifdef BARNES_HUT
#include "octree.hpp"
#endif
#ifndef WEB
#include <BS_thread_pool.hpp>
@ -48,29 +43,19 @@ public:
class Spring {
public:
int mass_a;
int mass_b;
std::size_t a;
std::size_t b;
public:
Spring(int _mass_a, int _mass_b) : mass_a(_mass_a), mass_b(_mass_b) {}
Spring(std::size_t _a, std::size_t _b) : a(_a), b(_b) {}
public:
auto CalculateSpringForce(Mass &_mass_a, Mass &_mass_b) const -> void;
auto CalculateSpringForce(Mass &_a, Mass &_b) const -> void;
};
class MassSpringSystem {
private:
#ifdef BARNES_HUT
// Barnes-Hut
Octree octree;
#else
// Uniform grid
std::vector<int> mass_indices;
std::vector<int64_t> cell_ids;
int last_build;
int last_masses_count;
int last_springs_count;
#endif
#ifndef WEB
BS::thread_pool<BS::tp::none> threads;
@ -79,19 +64,12 @@ private:
public:
// This is the main ownership of all the states/masses/springs.
std::vector<Mass> masses;
std::unordered_map<State, int> state_masses;
std::vector<Spring> springs;
std::unordered_map<std::pair<State, State>, int> state_springs;
public:
MassSpringSystem() : threads(std::thread::hardware_concurrency() - 1) {
#ifndef BARNES_HUT
last_build = REPULSION_GRID_REFRESH;
std::cout << "Using uniform grid repulsion force calculation." << std::endl;
#else
std::cout << "Using Barnes-Hut + octree repulsion force calculation."
<< std::endl;
#endif
#ifndef WEB
std::cout << "Thread-Pool: " << threads.get_thread_count() << " threads."
@ -105,20 +83,12 @@ public:
MassSpringSystem &operator=(MassSpringSystem &&move) = delete;
private:
#ifdef BARNES_HUT
auto BuildOctree() -> void;
#else
auto BuildUniformGrid() -> void;
#endif
public:
auto AddMass(const State &state) -> void;
auto AddMass() -> void;
auto GetMass(const State &state) -> Mass &;
auto GetMass(const State &state) const -> const Mass &;
auto AddSpring(const State &massA, const State &massB) -> void;
auto AddSpring(int a, int b) -> void;
auto Clear() -> void;
@ -129,19 +99,13 @@ public:
auto CalculateRepulsionForces() -> void;
auto VerletUpdate(float delta_time) -> void;
#ifndef BARNES_HUT
auto InvalidateGrid() -> void;
#endif
};
class ThreadedPhysics {
struct AddMass {
State s;
};
struct AddMass {};
struct AddSpring {
State a;
State b;
std::size_t a;
std::size_t b;
};
struct ClearGraph {};
@ -156,11 +120,9 @@ class ThreadedPhysics {
std::condition_variable_any data_consumed_cnd;
bool data_ready = false;
bool data_consumed = true;
std::vector<Mass> masses; // Read by renderer
std::unordered_map<State, int> state_masses; // Read by renderer
std::vector<Spring> springs; // Read by renderer
std::unordered_map<std::pair<State, State>, int>
state_springs; // Read by renderer
std::vector<Vector3> masses; // Read by renderer
std::vector<std::pair<std::size_t, std::size_t>>
springs; // Read by renderer
std::atomic<bool> running{true};
};
@ -181,6 +143,8 @@ public:
~ThreadedPhysics() {
state.running = false;
state.data_ready_cnd.notify_all();
state.data_consumed_cnd.notify_all();
physics.join();
}
@ -188,15 +152,15 @@ private:
static auto PhysicsThread(PhysicsState &state) -> void;
public:
auto AddMassCmd(const State &_state) -> void;
auto AddMassCmd() -> void;
auto AddSpringCmd(const State &a, const State &b) -> void;
auto AddSpringCmd(std::size_t a, std::size_t b) -> void;
auto ClearCmd() -> void;
auto AddMassSpringsCmd(const std::unordered_set<State> &masses,
const std::vector<std::pair<State, State>> &springs)
-> void;
auto AddMassSpringsCmd(
std::size_t num_masses,
const std::vector<std::pair<std::size_t, std::size_t>> &springs) -> void;
};
// https://en.cppreference.com/w/cpp/utility/variant/visit