add flag to toggle tracy (disabled for now)

This commit is contained in:
2026-02-24 20:39:48 +01:00
parent d88b66c058
commit 349d614611
20 changed files with 153 additions and 24 deletions

View File

@ -1,11 +1,11 @@
#ifndef __CAMERA_HPP_
#define __CAMERA_HPP_
#include "config.hpp"
#include <raylib.h>
#include <raymath.h>
#include "config.hpp"
class OrbitCamera3D {
friend class Renderer;

View File

@ -4,6 +4,7 @@
#include <raylib.h>
// #define WEB // Disables multithreading
// #define TRACY // Enable tracy profiling support
// Window
constexpr int INITIAL_WIDTH = 800;

View File

@ -1,6 +1,7 @@
#ifndef __INPUT_HPP_
#define __INPUT_HPP_
#include "config.hpp"
#include "state.hpp"
class InputHandler {

View File

@ -1,6 +1,8 @@
#ifndef __OCTREE_HPP_
#define __OCTREE_HPP_
#include "config.hpp"
#include <raylib.h>
#include <raymath.h>
#include <vector>

View File

@ -1,6 +1,9 @@
#ifndef __PHYSICS_HPP_
#define __PHYSICS_HPP_
#include "config.hpp"
#include "octree.hpp"
#include <atomic>
#include <condition_variable>
#include <cstddef>
@ -9,17 +12,18 @@
#include <raylib.h>
#include <raymath.h>
#include <thread>
#include <tracy/Tracy.hpp>
#include <variant>
#include <vector>
#include "octree.hpp"
#ifndef WEB
#define BS_THREAD_POOL_NATIVE_EXTENSIONS
#include <BS_thread_pool.hpp>
#endif
#ifdef TRACY
#include <tracy/Tracy.hpp>
#endif
class Mass {
public:
Vector3 position;
@ -116,10 +120,18 @@ class ThreadedPhysics {
using Command = std::variant<AddMass, AddSpring, ClearGraph>;
struct PhysicsState {
#ifdef TRACY
TracyLockable(std::mutex, command_mtx);
#else
std::mutex command_mtx;
#endif
std::queue<Command> pending_commands;
#ifdef TRACY
TracyLockable(std::mutex, data_mtx);
#else
std::mutex data_mtx;
#endif
std::condition_variable_any data_ready_cnd;
std::condition_variable_any data_consumed_cnd;
unsigned int ups = 0;

View File

@ -1,11 +1,12 @@
#ifndef __PRESETS_HPP_
#define __PRESETS_HPP_
#include "config.hpp"
#include "puzzle.hpp"
#include <functional>
#include <vector>
#include "puzzle.hpp"
using StateGenerator = std::function<State(void)>;
inline auto state_simple_1r() -> State {

View File

@ -1,6 +1,8 @@
#ifndef __PUZZLE_HPP_
#define __PUZZLE_HPP_
#include "config.hpp"
#include <array>
#include <cstddef>
#include <format>

View File

@ -1,14 +1,14 @@
#ifndef __RENDERER_HPP_
#define __RENDERER_HPP_
#include <raylib.h>
#include <raymath.h>
#include "camera.hpp"
#include "config.hpp"
#include "input.hpp"
#include "state.hpp"
#include <raylib.h>
#include <raymath.h>
class Renderer {
private:
const StateManager &state;

View File

@ -1,6 +1,7 @@
#ifndef __STATE_HPP_
#define __STATE_HPP_
#include "config.hpp"
#include "physics.hpp"
#include "presets.hpp"
#include "puzzle.hpp"

View File

@ -1,6 +1,10 @@
#ifndef __TRACY_HPP_
#define __TRACY_HPP_
#include "config.hpp"
#ifdef TRACY
#include <cstddef>
void *operator new(std::size_t count);
@ -8,3 +12,5 @@ void operator delete(void *ptr) noexcept;
void operator delete(void *ptr, std::size_t count) noexcept;
#endif
#endif

View File

@ -1,6 +1,8 @@
#ifndef __UTIL_HPP_
#define __UTIL_HPP_
#include "config.hpp"
#include <iostream>
#include <raylib.h>
#include <raymath.h>

View File

@ -1,10 +1,14 @@
#include "camera.hpp"
#include "config.hpp"
#include "tracy.hpp"
#include <raylib.h>
#include <raymath.h>
#ifdef TRACY
#include "tracy.hpp"
#include <tracy/Tracy.hpp>
#endif
auto OrbitCamera3D::HandleCameraInput() -> Vector2 {
Vector2 mouse = GetMousePosition();
if (mouse.x >= GetScreenWidth() / 2.0 && mouse.y >= MENU_HEIGHT) {

View File

@ -1,10 +1,14 @@
#include "input.hpp"
#include "config.hpp"
#include "tracy.hpp"
#include <algorithm>
#include <raylib.h>
#ifdef TRACY
#include "tracy.hpp"
#include <tracy/Tracy.hpp>
#endif
auto InputHandler::HandleMouseHover() -> void {
const int board_width = GetScreenWidth() / 2.0 - 2 * BOARD_PADDING;
const int board_height = GetScreenHeight() - MENU_HEIGHT - 2 * BOARD_PADDING;

View File

@ -1,14 +1,17 @@
#include <mutex>
#include <raylib.h>
#include <raymath.h>
#include <tracy/Tracy.hpp>
#include "config.hpp"
#include "input.hpp"
#include "physics.hpp"
#include "renderer.hpp"
#include "state.hpp"
#ifdef TRACY
#include "tracy.hpp"
#include <tracy/Tracy.hpp>
#endif
// TODO: Klotski state file loading
// - File should contain a single state per line, multiple lines possible
@ -20,8 +23,6 @@
// - 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
// TODO: Do I have a huge memory leak or is the memory just not reclaimed from
// the C++ runtime?
auto main(int argc, char *argv[]) -> int {
// if (argc < 2) {
@ -50,7 +51,9 @@ auto main(int argc, char *argv[]) -> int {
// Game loop
while (!WindowShouldClose()) {
#ifdef TRACY
FrameMarkStart("MainThread");
#endif
// Input update
state.previous_state = state.current_state;
@ -58,9 +61,15 @@ auto main(int argc, char *argv[]) -> int {
state.UpdateGraph(); // Add state added after user input
// Read positions from physics thread
#ifdef TRACY
FrameMarkStart("MainThreadConsumeLock");
#endif
{
#ifdef TRACY
std::unique_lock<LockableBase(std::mutex)> lock(physics.state.data_mtx);
#else
std::unique_lock<std::mutex> lock(physics.state.data_mtx);
#endif
ups = physics.state.ups;
@ -77,7 +86,9 @@ auto main(int argc, char *argv[]) -> int {
physics.state.data_consumed_cnd.notify_all();
}
}
#ifdef TRACY
FrameMarkEnd("MainThreadConsumeLock");
#endif
// Update the camera after the physics, so target lock is smooth
std::size_t current_index = state.CurrentMassIndex();
@ -92,7 +103,10 @@ auto main(int argc, char *argv[]) -> int {
renderer.DrawKlotski();
renderer.DrawMenu(masses, springs);
renderer.DrawTextures(ups);
#ifdef TRACY
FrameMark;
FrameMarkEnd("MainThread");
#endif
}
CloseWindow();

View File

@ -1,11 +1,15 @@
#include "octree.hpp"
#include "config.hpp"
#include "tracy.hpp"
#include "util.hpp"
#include <iostream>
#include <raymath.h>
#ifdef TRACY
#include "tracy.hpp"
#include <tracy/Tracy.hpp>
#endif
auto OctreeNode::ChildCount() const -> int {
int child_count = 0;
for (int child : children) {

View File

@ -1,20 +1,21 @@
#include "physics.hpp"
#include "config.hpp"
#include "tracy.hpp"
#include <BS_thread_pool.hpp>
#include <algorithm>
#include <cfloat>
#include <chrono>
#include <cstddef>
#include <mutex>
#include <ratio>
#include <raylib.h>
#include <raymath.h>
#include <tracy/Tracy.hpp>
#include <utility>
#include <vector>
#ifdef TRACY
#include "tracy.hpp"
#include <tracy/Tracy.hpp>
#endif
auto Mass::ClearForce() -> void { force = Vector3Zero(); }
auto Mass::CalculateVelocity(const float delta_time) -> void {
@ -94,7 +95,9 @@ auto MassSpringSystem::Clear() -> void {
}
auto MassSpringSystem::ClearForces() -> void {
#ifdef TRACY
ZoneScoped;
#endif
for (auto &mass : masses) {
mass.ClearForce();
@ -102,7 +105,9 @@ auto MassSpringSystem::ClearForces() -> void {
}
auto MassSpringSystem::CalculateSpringForces() -> void {
#ifdef TRACY
ZoneScoped;
#endif
for (const auto spring : springs) {
Mass &a = masses.at(spring.a);
@ -116,7 +121,9 @@ auto MassSpringSystem::SetThreadName(std::size_t idx) -> void {
}
auto MassSpringSystem::BuildOctree() -> void {
#ifdef TRACY
ZoneScoped;
#endif
octree.nodes.clear();
octree.nodes.reserve(masses.size() * 2);
@ -151,7 +158,9 @@ auto MassSpringSystem::BuildOctree() -> void {
}
auto MassSpringSystem::CalculateRepulsionForces() -> void {
#ifdef TRACY
ZoneScoped;
#endif
BuildOctree();
@ -173,7 +182,9 @@ auto MassSpringSystem::CalculateRepulsionForces() -> void {
}
auto MassSpringSystem::VerletUpdate(float delta_time) -> void {
#ifdef TRACY
ZoneScoped;
#endif
for (auto &mass : masses) {
mass.VerletUpdate(delta_time);
@ -198,7 +209,9 @@ auto ThreadedPhysics::PhysicsThread(ThreadedPhysics::PhysicsState &state)
unsigned int updates = 0;
while (state.running.load()) {
#ifdef TRACY
FrameMarkStart("PhysicsThread");
#endif
// Time tracking
std::chrono::time_point now = std::chrono::high_resolution_clock::now();
@ -209,7 +222,11 @@ auto ThreadedPhysics::PhysicsThread(ThreadedPhysics::PhysicsState &state)
// Handle queued commands
{
#ifdef TRACY
std::lock_guard<LockableBase(std::mutex)> lock(state.command_mtx);
#else
std::lock_guard<std::mutex> lock(state.command_mtx);
#endif
while (!state.pending_commands.empty()) {
Command &cmd = state.pending_commands.front();
cmd.visit(visitor);
@ -234,9 +251,15 @@ auto ThreadedPhysics::PhysicsThread(ThreadedPhysics::PhysicsState &state)
}
// Publish the positions for the renderer (copy)
#ifdef TRACY
FrameMarkStart("PhysicsThreadProduceLock");
#endif
{
#ifdef TRACY
std::unique_lock<LockableBase(std::mutex)> lock(state.data_mtx);
#else
std::unique_lock<std::mutex> lock(state.data_mtx);
#endif
state.data_consumed_cnd.wait(
lock, [&] { return state.data_consumed || !state.running.load(); });
if (!state.running.load()) {
@ -268,29 +291,43 @@ auto ThreadedPhysics::PhysicsThread(ThreadedPhysics::PhysicsState &state)
}
// Notify the rendering thread that new data is available
state.data_ready_cnd.notify_all();
#ifdef TRACY
FrameMarkEnd("PhysicsThreadProduceLock");
FrameMarkEnd("PhysicsThread");
#endif
}
}
auto ThreadedPhysics::AddMassCmd() -> void {
{
#ifdef TRACY
std::lock_guard<LockableBase(std::mutex)> lock(state.command_mtx);
#else
std::lock_guard<std::mutex> lock(state.command_mtx);
#endif
state.pending_commands.push(AddMass{});
}
}
auto ThreadedPhysics::AddSpringCmd(std::size_t a, std::size_t b) -> void {
{
#ifdef TRACY
std::lock_guard<LockableBase(std::mutex)> lock(state.command_mtx);
#else
std::lock_guard<std::mutex> lock(state.command_mtx);
#endif
state.pending_commands.push(AddSpring{a, b});
}
}
auto ThreadedPhysics::ClearCmd() -> void {
{
#ifdef TRACY
std::lock_guard<LockableBase(std::mutex)> lock(state.command_mtx);
#else
std::lock_guard<std::mutex> lock(state.command_mtx);
#endif
state.pending_commands.push(ClearGraph{});
}
}
@ -299,7 +336,11 @@ auto ThreadedPhysics::AddMassSpringsCmd(
std::size_t num_masses,
const std::vector<std::pair<std::size_t, std::size_t>> &springs) -> void {
{
#ifdef TRACY
std::lock_guard<LockableBase(std::mutex)> lock(state.command_mtx);
#else
std::lock_guard<std::mutex> lock(state.command_mtx);
#endif
for (std::size_t i = 0; i < num_masses; ++i) {
state.pending_commands.push(AddMass{});
}

View File

@ -1,8 +1,13 @@
#include "puzzle.hpp"
#include "tracy.hpp"
#include "config.hpp"
#include <unordered_set>
#ifdef TRACY
#include "tracy.hpp"
#include <tracy/Tracy.hpp>
#endif
auto Block::Hash() const -> int {
std::string s = std::format("{},{},{},{}", x, y, width, height);
return std::hash<std::string>{}(s);
@ -267,6 +272,10 @@ auto State::GetNextStates() const -> std::vector<State> {
auto State::Closure() const
-> std::pair<std::vector<State>,
std::vector<std::pair<std::size_t, std::size_t>>> {
#ifdef TRACY
ZoneScoped;
#endif
std::vector<State> states;
std::vector<std::pair<std::size_t, std::size_t>> links;

View File

@ -1,14 +1,17 @@
#include "renderer.hpp"
#include "config.hpp"
#include "puzzle.hpp"
#include "tracy.hpp"
#include <algorithm>
#include <format>
#include <raylib.h>
#include <raymath.h>
#include <rlgl.h>
#ifdef TRACY
#include "tracy.hpp"
#include <tracy/Tracy.hpp>
#endif
#ifdef BATCHING
#include <cstring>
@ -59,11 +62,15 @@ auto Renderer::ReallocateGraphInstancingIfNecessary(std::size_t size) -> void {
auto Renderer::DrawMassSprings(
const std::vector<Vector3> &masses,
const std::vector<std::pair<std::size_t, std::size_t>> &springs) -> void {
#ifdef TRACY
ZoneScoped;
#endif
// Prepare cube instancing
{
#ifdef TRACY
ZoneNamedN(prepare_masses, "PrepareMasses", true);
#endif
if (masses.size() < DRAW_VERTICES_LIMIT) {
if (transforms == nullptr) {
AllocateGraphInstancing(masses.size());
@ -85,7 +92,9 @@ auto Renderer::DrawMassSprings(
// Draw springs (batched)
{
#ifdef TRACY
ZoneNamedN(draw_springs, "DrawSprings", true);
#endif
rlBegin(RL_LINES);
for (const auto &[from, to] : springs) {
if (masses.size() > from && masses.size() > to) {
@ -101,7 +110,9 @@ auto Renderer::DrawMassSprings(
// Draw masses (instanced)
{
#ifdef TRACY
ZoneNamedN(draw_masses, "DrawMasses", true);
#endif
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
@ -171,7 +182,9 @@ auto Renderer::DrawMassSprings(
}
auto Renderer::DrawKlotski() -> void {
#ifdef TRACY
ZoneScoped;
#endif
BeginTextureMode(klotski_target);
ClearBackground(RAYWHITE);
@ -262,7 +275,9 @@ auto Renderer::DrawKlotski() -> void {
auto Renderer::DrawMenu(
const std::vector<Vector3> &masses,
const std::vector<std::pair<std::size_t, std::size_t>> &springs) -> void {
#ifdef TRACY
ZoneScoped;
#endif
BeginTextureMode(menu_target);
ClearBackground(RAYWHITE);

View File

@ -1,9 +1,14 @@
#include "state.hpp"
#include "config.hpp"
#include "presets.hpp"
#include "tracy.hpp"
#include <raymath.h>
#ifdef TRACY
#include "tracy.hpp"
#include <tracy/Tracy.hpp>
#endif
auto StateManager::LoadPreset(int preset) -> void {
current_preset = preset;
current_state = CurrentGenerator()();

View File

@ -1,5 +1,8 @@
#include "tracy.hpp"
#include "config.hpp"
#ifdef TRACY
#include "tracy.hpp"
#include <tracy/Tracy.hpp>
void *operator new(std::size_t count) {
@ -15,3 +18,5 @@ void operator delete(void *ptr, std::size_t count) noexcept {
TracyFreeS(ptr, 20);
free(ptr);
}
#endif