From d0ddf2256f00d8c6e62df0533db39351b51aedfe Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Wed, 4 Oct 2023 12:10:33 +0200 Subject: [PATCH 1/6] move chunkmeshdata into its own file And let it be managed by chunkmesher instead of chunkmanager --- include/chunkmeshdata.hpp | 36 +++++++++++++++++++++ include/chunkmesher.hpp | 9 +++--- include/renderer.hpp | 4 ++- src/chunkmanager.cpp | 4 --- src/chunkmesher.cpp | 67 ++++++++++----------------------------- src/main.cpp | 1 + src/renderer.cpp | 6 ++-- 7 files changed, 64 insertions(+), 63 deletions(-) create mode 100644 include/chunkmeshdata.hpp diff --git a/include/chunkmeshdata.hpp b/include/chunkmeshdata.hpp new file mode 100644 index 0000000..74fd74d --- /dev/null +++ b/include/chunkmeshdata.hpp @@ -0,0 +1,36 @@ +#ifndef CHUNK_MESH_DATA_H +#define CHUNK_MESH_DATA_H + +#include +#include "chunk.hpp" + +enum class ChunkMeshDataType{ + MESH_UPDATE +}; + +typedef struct ChunkMeshData{ + chunk_index_t index; + glm::vec3 position; + int num_vertices = 0; + + std::vector vertices; + std::vector extents; + std::vector texinfo; + + ChunkMeshDataType message_type; + + void clear(){ + vertices.clear(); + texinfo.clear(); + extents.clear(); + index = 0; + position = glm::vec3(0); + num_vertices = 0; + } + +}ChunkMeshData; +typedef oneapi::tbb::concurrent_queue ChunkMeshDataQueue; + + +#endif + diff --git a/include/chunkmesher.hpp b/include/chunkmesher.hpp index ff8a79a..cbfe4f9 100644 --- a/include/chunkmesher.hpp +++ b/include/chunkmesher.hpp @@ -5,10 +5,12 @@ #include #include +#include #include #include #include "chunk.hpp" +#include "chunkmeshdata.hpp" #include "globals.hpp" #include "shader.hpp" @@ -21,13 +23,12 @@ namespace chunkmesher{ std::vector extents; std::vector texinfo; }; - oneapi::tbb::concurrent_queue& getMeshDataQueue(); + ChunkMeshDataQueue& getMeshDataQueue(); + void init(); void mesh(Chunk::Chunk* chunk); - void sendtogpu(MeshData* mesh_data); - void quad(MeshData* mesh_data, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, - glm::vec3 bottomRight, glm::vec3 normal, Block block, int dim, bool backFace); } #endif + diff --git a/include/renderer.hpp b/include/renderer.hpp index d772c77..58907f7 100644 --- a/include/renderer.hpp +++ b/include/renderer.hpp @@ -6,6 +6,7 @@ #include "chunk.hpp" #include "chunkmesher.hpp" +#include "chunkmeshdata.hpp" #include "shader.hpp" namespace renderer{ @@ -21,8 +22,9 @@ namespace renderer{ Shader* getRenderShader(); RenderSet& getChunksToRender(); - oneapi::tbb::concurrent_queue& getMeshDataQueue(); + ChunkMeshDataQueue& getMeshDataQueue(); }; #endif + diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index 1eb021d..e2e1505 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -53,10 +53,6 @@ namespace chunkmanager index++; } - // Also init mesh data queue - for(int i = 0; i < 10; i++) - chunkmesher::getMeshDataQueue().push(new chunkmesher::MeshData()); - should_run = true; update_thread = std::thread(update); gen_thread = std::thread(generate); diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index d87a6e2..770aeae 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -11,15 +11,23 @@ #include "spacefilling.hpp" #include "utils.hpp" +#define CHUNK_MESH_DATA_QUANTITY 100 + namespace chunkmesher{ -oneapi::tbb::concurrent_queue MeshDataQueue; +ChunkMeshDataQueue MeshDataQueue; -oneapi::tbb::concurrent_queue& getMeshDataQueue(){ return MeshDataQueue; } +ChunkMeshDataQueue& getMeshDataQueue(){ return MeshDataQueue; } + +void init() +{ + for(int i = 0; i < CHUNK_MESH_DATA_QUANTITY; i++) + MeshDataQueue.push(new ChunkMeshData{}); +} void mesh(Chunk::Chunk* chunk) { - MeshData* mesh_data; + ChunkMeshData* mesh_data; if(!MeshDataQueue.try_pop(mesh_data)) return; /* @@ -38,11 +46,10 @@ void mesh(Chunk::Chunk* chunk) */ // Cleanup previous data - mesh_data->numVertices = 0; - mesh_data->chunk = chunk; - mesh_data->vertices.clear(); - mesh_data->extents.clear(); - mesh_data->texinfo.clear(); + mesh_data->clear(); + mesh_data->message_type = ChunkMeshDataType::MESH_UPDATE; + mesh_data->index = chunk->getIndex(); + mesh_data->position = chunk->getPosition(); // Abort if chunk is empty if(chunk->getState(Chunk::CHUNK_STATE_EMPTY)){ @@ -191,7 +198,7 @@ void mesh(Chunk::Chunk* chunk) mesh_data->texinfo.push_back(backFace ? 0.0 : 1.0); mesh_data->texinfo.push_back((int)(mask[n]) - 2); - mesh_data->numVertices++; + mesh_data->num_vertices++; } for (l = 0; l < h; ++l) @@ -223,46 +230,4 @@ void mesh(Chunk::Chunk* chunk) chunk->setState(Chunk::CHUNK_STATE_MESHED, true); renderer::getMeshDataQueue().push(mesh_data); } - -void sendtogpu(MeshData* mesh_data) -{ - if (mesh_data->numVertices > 0) - { - if(mesh_data->chunk->VAO == 0) mesh_data->chunk->createBuffers(); - - // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). - glBindVertexArray(mesh_data->chunk->VAO); - - // position attribute - glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->VBO); - glBufferData(GL_ARRAY_BUFFER, mesh_data->vertices.size() * sizeof(GLfloat), &(mesh_data->vertices[0]), GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); - glEnableVertexAttribArray(0); - - // normal attribute - glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->extentsBuffer); - glBufferData(GL_ARRAY_BUFFER, mesh_data->extents.size() * sizeof(GLfloat), &(mesh_data->extents[0]), GL_STATIC_DRAW); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)(0)); - glEnableVertexAttribArray(1); - - // texcoords attribute - glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->texinfoBuffer); - glBufferData(GL_ARRAY_BUFFER, mesh_data->texinfo.size() * sizeof(GLfloat), &(mesh_data->texinfo[0]), GL_STATIC_DRAW); - glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0); - - glBindVertexArray(0); - - // save the number of indices of the mesh, it is needed later for drawing - mesh_data->chunk->numVertices = (GLuint)(mesh_data->numVertices); - - // once data has been sent to the GPU, it can be cleared from system RAM - mesh_data->vertices.clear(); - mesh_data->extents.clear(); - mesh_data->texinfo.clear(); - } - - // mark the chunk mesh has loaded on GPU - mesh_data->chunk->setState(Chunk::CHUNK_STATE_MESH_LOADED, true); -} }; diff --git a/src/main.cpp b/src/main.cpp index 6aafb51..7bc51f9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -69,6 +69,7 @@ int main() SpaceFilling::initLUT(); controls::init(); chunkmanager::init(); + chunkmesher::init(); debug::window::init(window); renderer::init(window); diff --git a/src/renderer.cpp b/src/renderer.cpp index 0dcd3d2..ddd2b45 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -14,14 +14,14 @@ namespace renderer{ RenderSet chunks_torender; oneapi::tbb::concurrent_vector render_todelete; - oneapi::tbb::concurrent_queue MeshDataQueue; + ChunkMeshDataQueue MeshDataQueue; Shader* theShader, *quadShader; GLuint chunkTexture; Shader* getRenderShader() { return theShader; } RenderSet& getChunksToRender(){ return chunks_torender; } - oneapi::tbb::concurrent_queue& getMeshDataQueue(){ return MeshDataQueue; } + ChunkMeshDataQueue& getMeshDataQueue(){ return MeshDataQueue; } GLuint renderTexFrameBuffer, renderTex, renderTexDepthBuffer, quadVAO, quadVBO; int screenWidth, screenHeight; @@ -138,7 +138,7 @@ namespace renderer{ theShader->use(); theShader->setVec3("viewPos", cameraPos); - chunkmesher::MeshData* m; + ChunkMeshData* m; while(MeshDataQueue.try_pop(m)){ //chunkmesher::sendtogpu(m); chunkmesher::getMeshDataQueue().push(m); From 1822911845e144aea13c9da199d6212f5ebc9701 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Wed, 4 Oct 2023 12:12:29 +0200 Subject: [PATCH 2/6] chunkmesher: use goto for error handling --- src/chunkmesher.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index 770aeae..0a747fe 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -51,28 +51,23 @@ void mesh(Chunk::Chunk* chunk) mesh_data->index = chunk->getIndex(); mesh_data->position = chunk->getPosition(); - // Abort if chunk is empty - if(chunk->getState(Chunk::CHUNK_STATE_EMPTY)){ - chunk->setState(Chunk::CHUNK_STATE_MESHED, true); - renderer::getMeshDataQueue().push(mesh_data); - return; - } // convert tree to array since it is easier to work with it int length{0}; - std::unique_ptr blocks = chunk->getBlocksArray(&length); - if(length == 0) { - chunk->setState(Chunk::CHUNK_STATE_MESHED, true); - renderer::getMeshDataQueue().push(mesh_data); - return; - } - + std::unique_ptr blocks; + int k, l, u, v, w, h, n, j, i; int x[]{0, 0, 0}; int q[]{0, 0, 0}; int du[]{0, 0, 0}; int dv[]{0, 0, 0}; + // Abort if chunk is empty + if(chunk->getState(Chunk::CHUNK_STATE_EMPTY)) goto end; + + blocks = chunk->getBlocksArray(&length); + if(length == 0) goto end; + std::array mask; for (bool backFace = true, b = false; b != backFace; backFace = backFace && b, b = !b) { @@ -227,6 +222,7 @@ void mesh(Chunk::Chunk* chunk) } } +end: chunk->setState(Chunk::CHUNK_STATE_MESHED, true); renderer::getMeshDataQueue().push(mesh_data); } From 7786d4f04d4e3a23ea15683d2f9701dac5b9f0d6 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Wed, 4 Oct 2023 12:54:16 +0200 Subject: [PATCH 3/6] renderer: use ChunkMeshData from rendering thread This system decouples the Secondary threads from the Render thread. Once a chunk is meshed, only the mesh data is sent to the rendering thread, which does not use any direct reference to the chunk itself --- include/renderer.hpp | 32 +++++++- src/chunkmanager.cpp | 1 - src/chunkmesher.cpp | 1 - src/debugwindow.cpp | 11 +-- src/renderer.cpp | 172 ++++++++++++++++++++++++------------------- 5 files changed, 130 insertions(+), 87 deletions(-) diff --git a/include/renderer.hpp b/include/renderer.hpp index 58907f7..2b560c9 100644 --- a/include/renderer.hpp +++ b/include/renderer.hpp @@ -10,9 +10,38 @@ #include "shader.hpp" namespace renderer{ - typedef oneapi::tbb::concurrent_unordered_set RenderSet; + typedef struct RenderInfo { + chunk_index_t index; + int num_vertices; + glm::vec3 position; + bool buffers_allocated=false; + + GLuint VAO, VBO, extentsBuffer, texinfoBuffer; + + void allocateBuffers(){ + // Allocate buffers + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + glGenBuffers(1, &extentsBuffer); + glGenBuffers(1, &texinfoBuffer); + + buffers_allocated=true; + } + + void deallocateBuffers(){ + // Allocate buffers + glDeleteBuffers(1, &VBO); + glDeleteBuffers(1, &extentsBuffer); + glDeleteBuffers(1, &texinfoBuffer); + glDeleteVertexArrays(1, &VAO); + + buffers_allocated=false; + } + } RenderInfo; + void init(GLFWwindow* window); + void send_chunk_to_gpu(ChunkMeshData* mesh_data, RenderInfo* render_info); void render(); void resize_framebuffer(int width, int height); void framebuffer_size_callback(GLFWwindow *window, int width, int height); @@ -21,7 +50,6 @@ namespace renderer{ void saveScreenshot(bool forceFullHD=false); Shader* getRenderShader(); - RenderSet& getChunksToRender(); ChunkMeshDataQueue& getMeshDataQueue(); }; diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index e2e1505..b54c3d4 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -81,7 +81,6 @@ namespace chunkmanager if(chunks_to_mesh_queue.try_pop(entry)){ Chunk::Chunk* chunk = entry.first; chunkmesher::mesh(chunk); - renderer::getChunksToRender().insert(chunk); chunk->setState(Chunk::CHUNK_STATE_IN_MESHING_QUEUE, false); } } diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index 0a747fe..42cb65f 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -51,7 +51,6 @@ void mesh(Chunk::Chunk* chunk) mesh_data->index = chunk->getIndex(); mesh_data->position = chunk->getPosition(); - // convert tree to array since it is easier to work with it int length{0}; std::unique_ptr blocks; diff --git a/src/debugwindow.cpp b/src/debugwindow.cpp index f6e7977..256dd2e 100644 --- a/src/debugwindow.cpp +++ b/src/debugwindow.cpp @@ -95,18 +95,15 @@ namespace debug{ } if(ImGui::CollapsingHeader("Mesh")){ - ImGui::Text("Total chunks updated: %d", + ImGui::Text("Total chunk meshed: %d", std::any_cast(parameters.at("render_chunks_total"))); + ImGui::Text("Of which renderable (not empty): %d", + std::any_cast(parameters.at("render_chunks_renderable"))); ImGui::Text("Chunks rendered: %d", std::any_cast(parameters.at("render_chunks_rendered"))); ImGui::Text("Frustum culled: %d", std::any_cast(parameters.at("render_chunks_culled"))); - ImGui::Text("Chunks out of view: %d", - std::any_cast(parameters.at("render_chunks_oof"))); - if(parameters.find("render_chunks_deleted") != parameters.end()) - ImGui::Text("Chunks deleted: %d", - std::any_cast(parameters.at("render_chunks_deleted"))); - ImGui::Text("Vertices in the scene: %d", + ImGui::Text("Total vertices in the scene: %d", std::any_cast(parameters.at("render_chunks_vertices"))); ImGui::Checkbox("Wireframe", std::any_cast(parameters.at("wireframe_return"))); diff --git a/src/renderer.cpp b/src/renderer.cpp index ddd2b45..ed20533 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,7 +1,8 @@ #include "renderer.hpp" -#include -#include +#include +#include +#include #include "chunkmanager.hpp" #include "chunkmesher.hpp" @@ -12,15 +13,15 @@ #include "stb_image_write.h" namespace renderer{ - RenderSet chunks_torender; - oneapi::tbb::concurrent_vector render_todelete; + typedef oneapi::tbb::concurrent_hash_map RenderTable; + + RenderTable ChunksToRender; ChunkMeshDataQueue MeshDataQueue; Shader* theShader, *quadShader; GLuint chunkTexture; Shader* getRenderShader() { return theShader; } - RenderSet& getChunksToRender(){ return chunks_torender; } ChunkMeshDataQueue& getMeshDataQueue(){ return MeshDataQueue; } GLuint renderTexFrameBuffer, renderTex, renderTexDepthBuffer, quadVAO, quadVBO; @@ -140,97 +141,82 @@ namespace renderer{ ChunkMeshData* m; while(MeshDataQueue.try_pop(m)){ - //chunkmesher::sendtogpu(m); + RenderTable::accessor a; + RenderInfo* render_info; + + if(ChunksToRender.find(a, m->index)){ + render_info = a->second; + render_info->position = m->position; + render_info->num_vertices = m->num_vertices; + }else{ + render_info = new RenderInfo(); + render_info->index = m->index; + render_info->position = m->position; + render_info->num_vertices = m->num_vertices; + + ChunksToRender.emplace(a, std::make_pair(render_info->index, render_info)); + } + + send_chunk_to_gpu(m, render_info); chunkmesher::getMeshDataQueue().push(m); } - /* - for(auto& c : chunks_torender){ - float dist = glm::distance(c->getPosition(), cameraChunkPos); - if(dist <= static_cast(RENDER_DISTANCE)){ - if(!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) continue; + // TODO: implement removal of chunks from rendering - if(c->numVertices > 0){ - // Increase total vertex count - vertices += c->numVertices; + /* Render the chunks */ + // parallel_for cannot be used since all the rendering needs to happen in a single thread + for(RenderTable::iterator i = ChunksToRender.begin(); i != ChunksToRender.end(); i++){ + RenderInfo* render_info = i->second; - // reset out-of-vision and unload flags - c->setState(Chunk::CHUNK_STATE_OUTOFVISION, false); - c->setState(Chunk::CHUNK_STATE_UNLOADED, false); + if(render_info->num_vertices > 0) + { + total++; - // Perform frustum culling and eventually render - glm::vec3 chunk = c->getPosition(); - glm::vec4 chunkW = glm::vec4(chunk.x*static_cast(CHUNK_SIZE), chunk.y*static_cast(CHUNK_SIZE), chunk.z*static_cast(CHUNK_SIZE),1.0); - glm::mat4 model = glm::translate(glm::mat4(1.0), ((float)CHUNK_SIZE) * chunk); + // Increase total vertex count + vertices += render_info->num_vertices; - // Check if all the corners of the chunk are outside any of the planes - // TODO (?) implement frustum culling as per (Inigo Quilez)[https://iquilezles.org/articles/frustumcorrect/], and check each - // plane against each corner of the chunk - bool out=false; - int a{0}; - for(int p = 0; p < 6; p++){ - a = 0; - for(int i = 0; i < 8; i++) a += glm::dot(frustumPlanes[p], glm::vec4(chunkW.x + ((float)(i & 1))*CHUNK_SIZE, chunkW.y - + ((float)((i & 2) >> 1))*CHUNK_SIZE, chunkW.z + ((float)((i & 4) >> 2))*CHUNK_SIZE, 1.0)) < 0.0; + // Perform frustum culling and eventually render + glm::vec3 chunk = render_info->position; + glm::vec4 chunkW = glm::vec4(chunk.x*static_cast(CHUNK_SIZE), chunk.y*static_cast(CHUNK_SIZE), chunk.z*static_cast(CHUNK_SIZE),1.0); + glm::mat4 model = glm::translate(glm::mat4(1.0), ((float)CHUNK_SIZE) * chunk); - if(a==8){ - out=true; - break; - } - } + // Check if all the corners of the chunk are outside any of the planes + // TODO (?) implement frustum culling as per (Inigo Quilez)[https://iquilezles.org/articles/frustumcorrect/], and check each + // plane against each corner of the chunk + bool out=false; + int a{0}; + for(int p = 0; p < 6; p++){ + a = 0; + for(int i = 0; i < 8; i++) a += glm::dot(frustumPlanes[p], glm::vec4(chunkW.x + ((float)(i & 1))*CHUNK_SIZE, chunkW.y + + ((float)((i & 2) >> 1))*CHUNK_SIZE, chunkW.z + ((float)((i & 4) >> 2))*CHUNK_SIZE, 1.0)) < 0.0; - if (!out) - { - theShader->setMat4("model", model); - theShader->setMat4("view", theCamera.getView()); - theShader->setMat4("projection", theCamera.getProjection()); - - glBindVertexArray(c->VAO); - glDrawArrays(GL_POINTS, 0, c->numVertices); - glBindVertexArray(0); - - toGpu++; + if(a==8){ + out=true; + break; } } - }else{ - // When the chunk is outside render distance - if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){ - oof++; - if(glfwGetTime() - c->unload_timer > UNLOAD_TIMEOUT){ - // If chunk was already out and enough time has passed - // Mark the chunk to be unloaded - // And mark is to be removed from the render set - render_todelete.push_back(c); - } - } else{ - // Mark has out of vision and annotate when it started - c->setState(Chunk::CHUNK_STATE_OUTOFVISION, true); - c->setState(Chunk::CHUNK_STATE_UNLOADED, false); - c->unload_timer = glfwGetTime(); + if (!out) + { + theShader->setMat4("model", model); + theShader->setMat4("view", theCamera.getView()); + theShader->setMat4("projection", theCamera.getProjection()); + + glBindVertexArray(render_info->VAO); + glDrawArrays(GL_POINTS, 0, render_info->num_vertices); + glBindVertexArray(0); + + toGpu++; } - } } - */ - total = chunks_torender.size(); - debug::window::set_parameter("render_chunks_total", total); + debug::window::set_parameter("render_chunks_total", (int)(ChunksToRender.size())); debug::window::set_parameter("render_chunks_rendered", toGpu); + debug::window::set_parameter("render_chunks_renderable", total); debug::window::set_parameter("render_chunks_culled", total-toGpu); - debug::window::set_parameter("render_chunks_oof", oof); - debug::window::set_parameter("render_chunks_deleted", (int) (render_todelete.size())); debug::window::set_parameter("render_chunks_vertices", vertices); - /*for(auto& c : render_todelete){ - // we can get away with unsafe erase as access to the container is only done by this - // thread - c->deleteBuffers(); - chunks_torender.unsafe_erase(c); - chunkmanager::getDeleteVector().push(c); - } - render_todelete.clear();*/ - /* DISPLAY TEXTURE ON A QUAD THAT FILLS THE SCREEN */ // Now to render the quad, with the texture on top // Switch to the default frame buffer @@ -252,6 +238,40 @@ namespace renderer{ debug::window::render(); } + void send_chunk_to_gpu(ChunkMeshData* mesh_data, RenderInfo* render_info) + { + if (render_info->num_vertices > 0) + { + if(!render_info->buffers_allocated) render_info->allocateBuffers(); + + // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). + glBindVertexArray(render_info->VAO); + + // TODO: change GL_STATIC_DRAW to the one that means "few redraws and further in between" + + // position attribute + glBindBuffer(GL_ARRAY_BUFFER, render_info->VBO); + glBufferData(GL_ARRAY_BUFFER, mesh_data->vertices.size() * sizeof(GLfloat), &(mesh_data->vertices[0]), GL_STATIC_DRAW); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); + glEnableVertexAttribArray(0); + + // normal attribute + glBindBuffer(GL_ARRAY_BUFFER, render_info->extentsBuffer); + glBufferData(GL_ARRAY_BUFFER, mesh_data->extents.size() * sizeof(GLfloat), &(mesh_data->extents[0]), GL_STATIC_DRAW); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)(0)); + glEnableVertexAttribArray(1); + + // texcoords attribute + glBindBuffer(GL_ARRAY_BUFFER, render_info->texinfoBuffer); + glBufferData(GL_ARRAY_BUFFER, mesh_data->texinfo.size() * sizeof(GLfloat), &(mesh_data->texinfo[0]), GL_STATIC_DRAW); + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0); + + glBindVertexArray(0); + } + } + + void framebuffer_size_callback(GLFWwindow *window, int width, int height){ resize_framebuffer(width, height); } From 7c82a71dd099386d89345c3e100188284f9d4c5e Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Wed, 4 Oct 2023 12:55:16 +0200 Subject: [PATCH 4/6] chunk: vram buffers are managed by renderer now --- include/chunk.hpp | 4 ---- src/chunk.cpp | 15 --------------- 2 files changed, 19 deletions(-) diff --git a/include/chunk.hpp b/include/chunk.hpp index 19a9dc6..2ae8daf 100644 --- a/include/chunk.hpp +++ b/include/chunk.hpp @@ -52,9 +52,6 @@ namespace Chunk ~Chunk(); public: - void createBuffers(); - void deleteBuffers(); - glm::vec3 getPosition() { return this->position; } void setState(chunk_state_t nstate, bool value); bool getState(chunk_state_t n) { return (this->state & n) == n; } @@ -72,7 +69,6 @@ namespace Chunk std::unique_ptr getBlocksArray(int* len) { return (this->blocks.toArray(len)); } public: - GLuint VAO{0}, VBO{0}, extentsBuffer{0}, texinfoBuffer{0}, numVertices{0}; std::atomic unload_timer{0}; chunk_index_t getIndex(){ return this->index; } diff --git a/src/chunk.cpp b/src/chunk.cpp index 6eebecd..b85443d 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -36,21 +36,6 @@ namespace Chunk { } - void Chunk::createBuffers(){ - glGenVertexArrays(1, &(this->VAO)); - glGenBuffers(1, &(this->VBO)); - glGenBuffers(1, &(this->extentsBuffer)); - glGenBuffers(1, &(this->texinfoBuffer)); - - } - - void Chunk::deleteBuffers(){ - glDeleteBuffers(1, &(this->VBO)); - glDeleteBuffers(1, &(this->extentsBuffer)); - glDeleteBuffers(1, &(this->texinfoBuffer)); - glDeleteVertexArrays(1, &(this->VAO)); - } - Block Chunk::getBlock(int x, int y, int z) { if(x < 0 || y < 0 || z < 0 || x > CHUNK_SIZE -1 || y > CHUNK_SIZE -1 || z > CHUNK_SIZE-1 || From ba95db467862ff45c4b2c4ca78f73506d4a021f3 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Wed, 4 Oct 2023 13:02:10 +0200 Subject: [PATCH 5/6] renderer: also delete meshes of old chunks --- include/renderer.hpp | 3 +++ src/chunkmanager.cpp | 1 + src/renderer.cpp | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/renderer.hpp b/include/renderer.hpp index 2b560c9..c0cdcba 100644 --- a/include/renderer.hpp +++ b/include/renderer.hpp @@ -39,6 +39,7 @@ namespace renderer{ } } RenderInfo; + typedef oneapi::tbb::concurrent_queue IndexQueue; void init(GLFWwindow* window); void send_chunk_to_gpu(ChunkMeshData* mesh_data, RenderInfo* render_info); @@ -51,6 +52,8 @@ namespace renderer{ Shader* getRenderShader(); ChunkMeshDataQueue& getMeshDataQueue(); + IndexQueue& getDeleteIndexQueue(); + }; diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index b54c3d4..1addbce 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -112,6 +112,7 @@ namespace chunkmanager // Using the key doesn't work if(chunks.erase(a)){ nUnloaded++; + renderer::getDeleteIndexQueue().push(index); delete c; } else { c->setState(Chunk::CHUNK_STATE_IN_DELETING_QUEUE, false); diff --git a/src/renderer.cpp b/src/renderer.cpp index ed20533..5daa398 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -17,12 +17,14 @@ namespace renderer{ RenderTable ChunksToRender; ChunkMeshDataQueue MeshDataQueue; + IndexQueue MeshDataToDelete; Shader* theShader, *quadShader; GLuint chunkTexture; Shader* getRenderShader() { return theShader; } ChunkMeshDataQueue& getMeshDataQueue(){ return MeshDataQueue; } + IndexQueue& getDeleteIndexQueue(){ return MeshDataToDelete; } GLuint renderTexFrameBuffer, renderTex, renderTexDepthBuffer, quadVAO, quadVBO; int screenWidth, screenHeight; @@ -139,6 +141,7 @@ namespace renderer{ theShader->use(); theShader->setVec3("viewPos", cameraPos); + /* Process incoming mesh data */ ChunkMeshData* m; while(MeshDataQueue.try_pop(m)){ RenderTable::accessor a; @@ -161,7 +164,18 @@ namespace renderer{ chunkmesher::getMeshDataQueue().push(m); } - // TODO: implement removal of chunks from rendering + /* Process chunks to be removed */ + chunk_index_t queue_index; + while(MeshDataToDelete.try_pop(queue_index)){ + RenderTable::accessor a; + + if(ChunksToRender.find(a, queue_index)){ + RenderInfo* render_info = a->second; + render_info->deallocateBuffers(); + delete render_info; + ChunksToRender.erase(a); + } + } /* Render the chunks */ // parallel_for cannot be used since all the rendering needs to happen in a single thread From 78b65894b77fcbf71d70fd153deb9ef7ed89c698 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Wed, 4 Oct 2023 13:26:11 +0200 Subject: [PATCH 6/6] chunkmesher: compiler flag to not show borders at world limit --- src/chunkmesher.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index 42cb65f..2d200ad 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -12,6 +12,7 @@ #include "utils.hpp" #define CHUNK_MESH_DATA_QUANTITY 100 +#define CHUNK_MESH_WORLD_LIMIT_BORDERS 0 namespace chunkmesher{ @@ -129,9 +130,15 @@ void mesh(Chunk::Chunk* chunk) // The else case provides face culling for adjacent solid faces // Checking for NULLBLK avoids creating empty faces if nearby chunk was not // yet generated +#if CHUNK_MESH_WORLD_LIMIT_BORDERS == 1 mask[n++] = b1 == b2 ? Block::NULLBLK : backFace ? b1 == Block::NULLBLK || b1 == Block::AIR ? b2 : Block::NULLBLK : b2 == Block::NULLBLK || b2 == Block::AIR ? b1 : Block::NULLBLK; +#else + mask[n++] = b1 == b2 ? Block::NULLBLK + : backFace ? b1 == Block::AIR ? b2 : Block::NULLBLK + : b2 == Block::AIR ? b1 : Block::NULLBLK; +#endif } }