wip: integrating threaded decoupled physics
Current Issues: - HUGE memory leak - HUGE amount of needles copying - FillGraph() does thousands of lock_guards instead of one - Can no longer rely on new states appearing immediately - have to check each access - Physics run as fast as possible, no constant sim speed` - Irregular long freezes
This commit is contained in:
@ -7,6 +7,7 @@
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
#include <thread>
|
||||
#include <tracy/Tracy.hpp>
|
||||
#include <unordered_map>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
@ -146,10 +147,10 @@ class ThreadedPhysics {
|
||||
using Command = std::variant<AddMass, AddSpring, ClearGraph>;
|
||||
|
||||
struct PhysicsState {
|
||||
std::mutex command_mtx;
|
||||
TracyLockable(std::mutex, command_mtx);
|
||||
std::queue<Command> pending_commands;
|
||||
|
||||
std::mutex pos_mtx;
|
||||
TracyLockable(std::mutex, pos_mtx);
|
||||
std::vector<Mass> masses; // Read by renderer
|
||||
std::unordered_map<State, int> state_masses; // Read by renderer
|
||||
std::vector<Spring> springs; // Read by renderer
|
||||
@ -160,9 +161,11 @@ class ThreadedPhysics {
|
||||
};
|
||||
|
||||
private:
|
||||
PhysicsState state;
|
||||
std::thread physics;
|
||||
|
||||
public:
|
||||
PhysicsState state;
|
||||
|
||||
public:
|
||||
ThreadedPhysics() : physics(PhysicsThread, std::ref(state)) {}
|
||||
|
||||
@ -180,6 +183,11 @@ private:
|
||||
static auto PhysicsThread(PhysicsState &state) -> void;
|
||||
|
||||
public:
|
||||
auto AddMassCmd(const State &_state) -> void;
|
||||
|
||||
auto AddSpringCmd(const State &a, const State &b) -> void;
|
||||
|
||||
auto ClearCmd() -> void;
|
||||
};
|
||||
|
||||
// https://en.cppreference.com/w/cpp/utility/variant/visit
|
||||
|
||||
@ -61,25 +61,29 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
auto AllocateGraphInstancing(const MassSpringSystem &mass_springs) -> void;
|
||||
auto AllocateGraphInstancing(const std::vector<Mass> &masses) -> void;
|
||||
|
||||
auto
|
||||
ReallocateGraphInstancingIfNecessary(const MassSpringSystem &mass_springs)
|
||||
auto ReallocateGraphInstancingIfNecessary(const std::vector<Mass> &masses)
|
||||
-> void;
|
||||
|
||||
public:
|
||||
auto UpdateTextureSizes() -> void;
|
||||
|
||||
auto DrawMassSprings(const MassSpringSystem &mass_springs,
|
||||
const State ¤t_state, const State &starting_state,
|
||||
const std::unordered_set<State> &winning_states,
|
||||
const std::unordered_set<State> &visited_states) -> void;
|
||||
auto DrawMassSprings(
|
||||
const std::vector<Mass> &masses,
|
||||
const std::unordered_map<State, int> &state_masses,
|
||||
const std::vector<Spring> &springs,
|
||||
const std::unordered_map<std::pair<State, State>, int> state_springs,
|
||||
const State ¤t_state, const State &starting_state,
|
||||
const std::unordered_set<State> &winning_states,
|
||||
const std::unordered_set<State> &visited_states) -> void;
|
||||
|
||||
auto DrawKlotski(const State &state, int hov_x, int hov_y, int sel_x,
|
||||
int sel_y, int block_add_x, int block_add_y,
|
||||
const WinCondition win_condition) -> void;
|
||||
|
||||
auto DrawMenu(const MassSpringSystem &mass_springs, int current_preset,
|
||||
auto DrawMenu(const std::vector<Mass> &masses,
|
||||
const std::vector<Spring> &springs, int current_preset,
|
||||
const State ¤t_state,
|
||||
const std::unordered_set<State> &winning_states) -> void;
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
#ifndef __STATE_HPP_
|
||||
#define __STATE_HPP_
|
||||
|
||||
#include "config.hpp"
|
||||
#include "physics.hpp"
|
||||
#include "presets.hpp"
|
||||
#include "puzzle.hpp"
|
||||
@ -10,7 +9,7 @@
|
||||
|
||||
class StateManager {
|
||||
public:
|
||||
MassSpringSystem &mass_springs;
|
||||
ThreadedPhysics &physics;
|
||||
|
||||
int current_preset;
|
||||
State starting_state;
|
||||
@ -23,12 +22,12 @@ public:
|
||||
std::unordered_set<State> visited_states;
|
||||
|
||||
public:
|
||||
StateManager(MassSpringSystem &_mass_springs)
|
||||
: mass_springs(_mass_springs), current_preset(0),
|
||||
StateManager(ThreadedPhysics &_physics)
|
||||
: physics(_physics), current_preset(0),
|
||||
starting_state(generators[current_preset]()),
|
||||
current_state(starting_state), previous_state(starting_state),
|
||||
edited(false) {
|
||||
mass_springs.AddMass(MASS, false, current_state);
|
||||
physics.AddMassCmd(current_state);
|
||||
}
|
||||
|
||||
StateManager(const StateManager ©) = delete;
|
||||
|
||||
Reference in New Issue
Block a user