From 57d749f09bae7df4673b577d786a5a4f7ea2ac95 Mon Sep 17 00:00:00 2001 From: 0x01FE <0x01fe@0x01fe.net> Date: Wed, 19 Nov 2025 22:04:05 -0600 Subject: [PATCH] fixed FPS issues (turns out you shouldn't look at 32768 * 16 voxels every frame --- src/Chunk.h | 16 ++++++++++++---- src/Mesh.cpp | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Chunk.h b/src/Chunk.h index 5948e76..17ca20e 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -59,25 +59,33 @@ class Chunk { // Mutators inline void set(int x, int y, int z, VoxelKind v) { data[index(x, y, z)] = v; + empty_checked = false; // Invalidate cache } inline void set(glm::ivec3 pos, VoxelKind v) { // std::cout << "Setting Pos (" << pos.x << ", " << pos.y << ", " << pos.z << ")" << std::endl; data[index(pos.x, pos.y, pos.z)] = v; + empty_checked = false; // Invalidate cache } // Other inline bool isEmpty() const { - return std::all_of(data.begin(), data.end(), [](VoxelKind k){ - return k == VoxelKind::Air; - }); + if (!empty_checked) { + is_empty = std::all_of(data.begin(), data.end(), [](VoxelKind k){ + return k == VoxelKind::Air; + }); + empty_checked = true; + } + return is_empty; } private: static constexpr int SIZE = CHUNK_SIZE; static constexpr int TOTAL = SIZE * SIZE * SIZE; - std::array data; + std::array data; std::unique_ptr mesh; // Nullable mesh + mutable bool is_empty = true; // Cache empty state + mutable bool empty_checked = false; // Track if we've checked static inline constexpr int index(int x, int y, int z) noexcept { return x + (y * SIZE) + (z * SIZE * SIZE); diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 625bc5b..f0b772a 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -43,7 +43,7 @@ void Mesh::init() { void Mesh::uploadData(const std::vector &vboData, const std::vector &eboData) { m_indexCount = eboData.size(); - // std::cout << "Uploading mesh: " << vboData.size() << " floats, " << eboData.size() << " indices" << std::endl; + std::cout << "Uploading mesh: " << vboData.size() << " floats, " << eboData.size() << " indices" << std::endl; // Bind VAO first, then upload buffer data glBindVertexArray(m_VAO);