Compare commits

..

3 Commits

Author SHA1 Message Date
0e6bb132c8 wip: don't lock command mutex for each mass/spring in FillGraph() 2026-02-24 02:13:38 +01:00
550970f8a1 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
2026-02-24 02:05:09 +01:00
39c0b58f3f implement threaded physics (decoupled from rendering thread) - not yet integrated 2026-02-24 01:02:53 +01:00
9 changed files with 269 additions and 97 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.25)
project(MassSprings)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 26)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(raylib REQUIRED)

View File

@ -30,7 +30,7 @@ constexpr float ROT_SPEED = 1.0;
constexpr float CAMERA_SMOOTH_SPEED = 15.0;
// Physics Engine
constexpr float SIM_SPEED = 4.0; // How large each update should be
constexpr float SIM_SPEED = 0.2; // How large each update should be
constexpr float TIMESTEP = 1.0 / 90; // Do 90 physics updates per second
constexpr float MASS = 1.0; // Mass spring system
constexpr float SPRING_CONSTANT = 5.0; // Mass spring system

View File

@ -1,9 +1,15 @@
#ifndef __PHYSICS_HPP_
#define __PHYSICS_HPP_
#include <atomic>
#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"
@ -105,14 +111,13 @@ private:
#endif
public:
auto AddMass(float mass, bool fixed, const State &state) -> void;
auto AddMass(const State &state) -> void;
auto GetMass(const State &state) -> Mass &;
auto GetMass(const State &state) const -> const Mass &;
auto AddSpring(const State &massA, const State &massB, float spring_constant,
float dampening_constant, float rest_length) -> void;
auto AddSpring(const State &massA, const State &massB) -> void;
auto Clear() -> void;
@ -129,4 +134,69 @@ public:
#endif
};
class ThreadedPhysics {
struct AddMass {
State s;
};
struct AddSpring {
State a;
State b;
};
struct ClearGraph {};
using Command = std::variant<AddMass, AddSpring, ClearGraph>;
struct PhysicsState {
TracyLockable(std::mutex, command_mtx);
std::queue<Command> pending_commands;
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
std::unordered_map<std::pair<State, State>, int>
state_springs; // Read by renderer
std::atomic<bool> running{true};
};
private:
std::thread physics;
public:
PhysicsState state;
public:
ThreadedPhysics() : physics(PhysicsThread, std::ref(state)) {}
ThreadedPhysics(const ThreadedPhysics &copy) = delete;
ThreadedPhysics &operator=(const ThreadedPhysics &copy) = delete;
ThreadedPhysics(ThreadedPhysics &&move) = delete;
ThreadedPhysics &operator=(ThreadedPhysics &&move) = delete;
~ThreadedPhysics() {
state.running = false;
physics.join();
}
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;
auto NonLockingAddMassCmd(const State &_state) -> void;
auto NonLockingAddSpringCmd(const State &a, const State &b) -> void;
};
// https://en.cppreference.com/w/cpp/utility/variant/visit
template <class... Ts> struct overloads : Ts... {
using Ts::operator()...;
};
#endif

View File

@ -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 &current_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 &current_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 &current_state,
const std::unordered_set<State> &winning_states) -> void;

View File

@ -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 &copy) = delete;

View File

@ -1,3 +1,8 @@
#include <mutex>
#include <raylib.h>
#include <raymath.h>
#include <tracy/Tracy.hpp>
#include "config.hpp"
#include "input.hpp"
#include "physics.hpp"
@ -5,10 +10,6 @@
#include "state.hpp"
#include "tracy.hpp"
#include <raylib.h>
#include <raymath.h>
#include <tracy/Tracy.hpp>
// TODO: Klotski state file loading
// - File should contain a single state per line, multiple lines possible
// - If a file is loaded, the presets should be replaced with the states
@ -18,7 +19,7 @@
// TODO: Graph interaction
// - Click states to display them in the board
// - Find shortest path to any winning state and mark it in the graph
// - Also mark the next move along the path on the board
// - Also mark the next move along the path on the board
auto main(int argc, char *argv[]) -> int {
// if (argc < 2) {
@ -37,20 +38,27 @@ auto main(int argc, char *argv[]) -> int {
// Game setup
OrbitCamera3D camera;
Renderer renderer(camera);
MassSpringSystem mass_springs;
StateManager state(mass_springs);
ThreadedPhysics physics;
StateManager state(physics);
InputHandler input(state, renderer);
std::vector<Mass> masses; // Read from physics
std::unordered_map<State, int> state_masses; // Read from physics
std::vector<Spring> springs; // Read from physics
std::unordered_map<std::pair<State, State>, int>
state_springs; // Read from physics
// Game loop
double timestep_accumulator = 0.0;
// double timestep_accumulator = 0.0;
while (!WindowShouldClose()) {
timestep_accumulator += GetFrameTime();
// timestep_accumulator += GetFrameTime();
// Input update
state.previous_state = state.current_state;
input.HandleInput();
state.UpdateGraph(); // Add state added after user input
/*
// Physics update
if (timestep_accumulator > TIMESTEP) {
// Do not try to catch up if we're falling behind. Frametimes would get
@ -63,21 +71,43 @@ auto main(int argc, char *argv[]) -> int {
timestep_accumulator -= TIMESTEP;
}
*/
// Read positions from physics thread
// TODO: We're reading a HUGE amount of SHIT, because the state
// representations (std::string, ~100 Bytes) are copied ~3 times per
// spring: state_masses<State, int> and state_springs<<State, State>,
// int>
// TODO: Don't download each frame if nothing changed, check for update
// first
{
std::lock_guard<LockableBase(std::mutex)> lock(physics.state.pos_mtx);
masses = physics.state.masses;
state_masses = physics.state.state_masses;
springs = physics.state.springs;
state_springs = physics.state.state_springs;
}
// Update the camera after the physics, so target lock is smooth
camera.Update(mass_springs.GetMass(state.current_state).position);
if (state_masses.contains(state.current_state)) {
const Mass &current_mass =
masses.at(state_masses.at(state.current_state));
camera.Update(current_mass.position);
}
// Rendering
renderer.UpdateTextureSizes();
renderer.DrawMassSprings(mass_springs, state.current_state,
state.starting_state, state.winning_states,
state.visited_states);
if (masses.size() > 0 && state_masses.contains(state.current_state)) {
renderer.DrawMassSprings(masses, state_masses, springs, state_springs,
state.current_state, state.starting_state,
state.winning_states, state.visited_states);
}
renderer.DrawKlotski(state.current_state, input.hov_x, input.hov_y,
input.sel_x, input.sel_y, input.block_add_x,
input.block_add_y, state.CurrentWinCondition());
renderer.DrawMenu(mass_springs, state.current_preset, state.current_state,
state.winning_states);
renderer.DrawMenu(masses, springs, state.current_preset,
state.current_state, state.winning_states);
renderer.DrawTextures();
}

View File

@ -5,6 +5,7 @@
#include <algorithm>
#include <cfloat>
#include <cstddef>
#include <mutex>
#include <raylib.h>
#include <raymath.h>
#include <tracy/Tracy.hpp>
@ -69,8 +70,7 @@ auto Spring::CalculateSpringForce(Mass &_mass_a, Mass &_mass_b) const -> void {
_mass_b.force = Vector3Add(_mass_b.force, force_b);
}
auto MassSpringSystem::AddMass(float mass, bool fixed, const State &state)
-> void {
auto MassSpringSystem::AddMass(const State &state) -> void {
if (!state_masses.contains(state)) {
masses.emplace_back(Vector3Zero());
std::size_t idx = masses.size() - 1;
@ -86,9 +86,7 @@ auto MassSpringSystem::GetMass(const State &state) const -> const Mass & {
return masses.at(state_masses.at(state));
}
auto MassSpringSystem::AddSpring(const State &state_a, const State &state_b,
float spring_constant,
float dampening_constant, float rest_length)
auto MassSpringSystem::AddSpring(const State &state_a, const State &state_b)
-> void {
std::pair<State, State> key = std::make_pair(state_a, state_b);
if (!state_springs.contains(key)) {
@ -340,3 +338,76 @@ auto MassSpringSystem::InvalidateGrid() -> void {
last_springs_count = 0;
}
#endif
auto ThreadedPhysics::PhysicsThread(ThreadedPhysics::PhysicsState &state)
-> void {
MassSpringSystem mass_springs;
const auto visitor = overloads{
[&](const struct AddMass &am) { mass_springs.AddMass(am.s); },
[&](const struct AddSpring &as) { mass_springs.AddSpring(as.a, as.b); },
[&](const struct ClearGraph &cg) { mass_springs.Clear(); },
};
while (state.running) {
// Handle queued commands
{
std::lock_guard<LockableBase(std::mutex)> lock(state.command_mtx);
while (!state.pending_commands.empty()) {
Command &cmd = state.pending_commands.front();
cmd.visit(visitor);
state.pending_commands.pop();
}
}
if (mass_springs.masses.empty()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
// Physics update
mass_springs.ClearForces();
mass_springs.CalculateSpringForces();
mass_springs.CalculateRepulsionForces();
mass_springs.VerletUpdate(TIMESTEP * SIM_SPEED);
// Publish the positions for the renderer (copy)
{
std::lock_guard<LockableBase(std::mutex)> lock(state.pos_mtx);
state.masses = mass_springs.masses;
state.state_masses = mass_springs.state_masses;
state.springs = mass_springs.springs;
state.state_springs = mass_springs.state_springs;
}
}
}
auto ThreadedPhysics::AddMassCmd(const State &_state) -> void {
{
std::lock_guard<LockableBase(std::mutex)> lock(state.command_mtx);
state.pending_commands.push(AddMass{_state});
}
}
auto ThreadedPhysics::AddSpringCmd(const State &a, const State &b) -> void {
{
std::lock_guard<LockableBase(std::mutex)> lock(state.command_mtx);
state.pending_commands.push(AddSpring{a, b});
}
}
auto ThreadedPhysics::ClearCmd() -> void {
{
std::lock_guard<LockableBase(std::mutex)> lock(state.command_mtx);
state.pending_commands.push(ClearGraph{});
}
}
auto ThreadedPhysics::NonLockingAddMassCmd(const State &_state) -> void {
state.pending_commands.push(AddMass{_state});
}
auto ThreadedPhysics::NonLockingAddSpringCmd(const State &a, const State &b)
-> void {
state.pending_commands.push(AddSpring{a, b});
}

View File

@ -33,7 +33,7 @@ auto Renderer::UpdateTextureSizes() -> void {
menu_target = LoadRenderTexture(width * 2, MENU_HEIGHT);
}
auto Renderer::AllocateGraphInstancing(const MassSpringSystem &mass_springs)
auto Renderer::AllocateGraphInstancing(const std::vector<Mass> &masses)
-> void {
cube_instance = GenMeshCube(VERTEX_SIZE, VERTEX_SIZE, VERTEX_SIZE);
@ -48,38 +48,40 @@ auto Renderer::AllocateGraphInstancing(const MassSpringSystem &mass_springs)
vertex_mat.maps[MATERIAL_MAP_DIFFUSE].color = VERTEX_COLOR;
vertex_mat.shader = instancing_shader;
transforms = (Matrix *)MemAlloc(mass_springs.masses.size() * sizeof(Matrix));
transforms_size = mass_springs.masses.size();
transforms = (Matrix *)MemAlloc(masses.size() * sizeof(Matrix));
transforms_size = masses.size();
}
auto Renderer::ReallocateGraphInstancingIfNecessary(
const MassSpringSystem &mass_springs) -> void {
if (transforms_size != mass_springs.masses.size()) {
transforms = (Matrix *)MemRealloc(transforms, mass_springs.masses.size() *
sizeof(Matrix));
transforms_size = mass_springs.masses.size();
const std::vector<Mass> &masses) -> void {
if (transforms_size != masses.size()) {
transforms =
(Matrix *)MemRealloc(transforms, masses.size() * sizeof(Matrix));
transforms_size = masses.size();
}
}
auto Renderer::DrawMassSprings(const MassSpringSystem &mass_springs,
const State &current_state,
const State &starting_state,
const std::unordered_set<State> &winning_states,
const std::unordered_set<State> &visited_states)
-> void {
auto Renderer::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 &current_state, const State &starting_state,
const std::unordered_set<State> &winning_states,
const std::unordered_set<State> &visited_states) -> void {
ZoneScoped;
// Prepare cube instancing
{
ZoneNamedN(prepare_masses, "PrepareMasses", true);
if (mass_springs.masses.size() < DRAW_VERTICES_LIMIT) {
if (masses.size() < DRAW_VERTICES_LIMIT) {
if (transforms == nullptr) {
AllocateGraphInstancing(mass_springs);
AllocateGraphInstancing(masses);
}
ReallocateGraphInstancingIfNecessary(mass_springs);
ReallocateGraphInstancingIfNecessary(masses);
int i = 0;
for (const auto &mass : mass_springs.masses) {
for (const auto &mass : masses) {
transforms[i] =
MatrixTranslate(mass.position.x, mass.position.y, mass.position.z);
++i;
@ -96,10 +98,10 @@ auto Renderer::DrawMassSprings(const MassSpringSystem &mass_springs,
{
ZoneNamedN(draw_springs, "DrawSprings", true);
rlBegin(RL_LINES);
for (const auto &spring : mass_springs.springs) {
for (const auto &spring : springs) {
// We have to do a lookup of the actual mass object, which is slow :(
const Mass &a = mass_springs.masses.at(spring.mass_a);
const Mass &b = mass_springs.masses.at(spring.mass_b);
const Mass &a = masses.at(spring.mass_a);
const Mass &b = masses.at(spring.mass_b);
rlColor4ub(EDGE_COLOR.r, EDGE_COLOR.g, EDGE_COLOR.b, EDGE_COLOR.a);
rlVertex3f(a.position.x, a.position.y, a.position.z);
rlVertex3f(b.position.x, b.position.y, b.position.z);
@ -110,20 +112,19 @@ auto Renderer::DrawMassSprings(const MassSpringSystem &mass_springs,
// Draw masses (instanced)
{
ZoneNamedN(draw_masses, "DrawMasses", true);
if (mass_springs.masses.size() < DRAW_VERTICES_LIMIT) {
if (masses.size() < DRAW_VERTICES_LIMIT) {
// NOTE: I don't know if drawing all this inside a shader would make it
// much faster... The amount of data sent to the GPU would be
// reduced (just positions instead of matrices), but is this
// noticable for < 100000 cubes?
DrawMeshInstanced(cube_instance, vertex_mat, transforms,
mass_springs.masses.size());
DrawMeshInstanced(cube_instance, vertex_mat, transforms, masses.size());
}
}
// Mark winning states
if (mark_solutions || connect_solutions) {
for (const auto &state : winning_states) {
const Mass &winning_mass = mass_springs.GetMass(state);
const Mass &winning_mass = masses.at(state_masses.at(state));
if (mark_solutions) {
DrawCube(winning_mass.position, 2 * VERTEX_SIZE, 2 * VERTEX_SIZE,
2 * VERTEX_SIZE, BLUE);
@ -131,26 +132,26 @@ auto Renderer::DrawMassSprings(const MassSpringSystem &mass_springs,
if (connect_solutions) {
DrawLine3D(winning_mass.position,
mass_springs.GetMass(current_state).position, PURPLE);
masses.at(state_masses.at(current_state)).position, PURPLE);
}
}
}
// Mark visited states
for (const auto &state : visited_states) {
const Mass &visited_mass = mass_springs.GetMass(state);
const Mass &visited_mass = masses.at(state_masses.at(state));
DrawCube(visited_mass.position, VERTEX_SIZE * 1.5, VERTEX_SIZE * 1.5,
VERTEX_SIZE * 1.5, PURPLE);
}
// Mark starting state
const Mass &starting_mass = mass_springs.GetMass(starting_state);
const Mass &starting_mass = masses.at(state_masses.at(starting_state));
DrawCube(starting_mass.position, VERTEX_SIZE * 2, VERTEX_SIZE * 2,
VERTEX_SIZE * 2, ORANGE);
// Mark current state
const Mass &current_mass = mass_springs.GetMass(current_state);
const Mass &current_mass = masses.at(state_masses.at(current_state));
DrawCube(current_mass.position, VERTEX_SIZE * 2, VERTEX_SIZE * 2,
VERTEX_SIZE * 2, RED);
@ -250,8 +251,9 @@ auto Renderer::DrawKlotski(const State &state, int hov_x, int hov_y, int sel_x,
EndTextureMode();
}
auto Renderer::DrawMenu(const MassSpringSystem &mass_springs,
int current_preset, const State &current_state,
auto Renderer::DrawMenu(const std::vector<Mass> &masses,
const std::vector<Spring> &springs, int current_preset,
const State &current_state,
const std::unordered_set<State> &winning_states)
-> void {
ZoneScoped;
@ -277,8 +279,7 @@ auto Renderer::DrawMenu(const MassSpringSystem &mass_springs,
draw_btn(0, 0,
std::format("States: {}, Transitions: {}, Winning: {}",
mass_springs.masses.size(), mass_springs.springs.size(),
winning_states.size()),
masses.size(), springs.size(), winning_states.size()),
DARKGREEN);
draw_btn(
0, 1,

View File

@ -1,8 +1,8 @@
#include "state.hpp"
#include "config.hpp"
#include "presets.hpp"
#include "tracy.hpp"
#include <mutex>
#include <raymath.h>
auto StateManager::LoadPreset(int preset) -> void {
@ -37,31 +37,26 @@ auto StateManager::FillGraph() -> void {
std::pair<std::unordered_set<State>, std::vector<std::pair<State, State>>>
closure = current_state.Closure();
for (const auto &state : closure.first) {
mass_springs.AddMass(MASS, false, state);
{
std::lock_guard<LockableBase(std::mutex)> lock(physics.state.command_mtx);
for (const auto &state : closure.first) {
physics.NonLockingAddMassCmd(state);
}
for (const auto &[from, to] : closure.second) {
physics.NonLockingAddSpringCmd(from, to);
}
}
for (const auto &[from, to] : closure.second) {
mass_springs.AddSpring(from, to, SPRING_CONSTANT, DAMPENING_CONSTANT,
REST_LENGTH);
}
std::cout << "Inserted " << mass_springs.masses.size() << " masses and "
<< mass_springs.springs.size() << " springs." << std::endl;
// TODO: We have only dispatched the commands, the states won't be downloaded
// when calling this... Make FindWinningStates() another command?
FindWinningStates();
std::cout << "Consuming "
<< sizeof(decltype(*mass_springs.masses.begin())) *
mass_springs.masses.size()
<< " Bytes for masses." << std::endl;
std::cout << "Consuming "
<< sizeof(decltype(*mass_springs.springs.begin())) *
mass_springs.springs.size()
<< " Bytes for springs." << std::endl;
}
auto StateManager::UpdateGraph() -> void {
if (previous_state != current_state) {
mass_springs.AddMass(MASS, false, current_state);
mass_springs.AddSpring(current_state, previous_state, SPRING_CONSTANT,
DAMPENING_CONSTANT, REST_LENGTH);
physics.AddMassCmd(current_state);
physics.AddSpringCmd(current_state, previous_state);
if (win_conditions[current_preset](current_state)) {
winning_states.insert(current_state);
}
@ -72,8 +67,8 @@ auto StateManager::UpdateGraph() -> void {
auto StateManager::ClearGraph() -> void {
winning_states.clear();
visited_states.clear();
mass_springs.Clear();
mass_springs.AddMass(MASS, false, current_state);
physics.ClearCmd();
physics.AddMassCmd(current_state);
// The previous_state is no longer in the graph
previous_state = current_state;
@ -84,14 +79,16 @@ auto StateManager::ClearGraph() -> void {
auto StateManager::FindWinningStates() -> void {
winning_states.clear();
for (const auto &[state, mass] : mass_springs.state_masses) {
std::unordered_map<State, int> state_masses;
{
std::lock_guard<LockableBase(std::mutex)> lock(physics.state.pos_mtx);
state_masses = physics.state.state_masses;
}
for (const auto &[state, mass] : state_masses) {
if (win_conditions[current_preset](state)) {
winning_states.insert(state);
}
}
std::cout << "Found " << winning_states.size() << " winning states."
<< std::endl;
}
auto StateManager::CurrentGenerator() -> StateGenerator {