From 5b868ae857b1b425a4f529c663b3dad55fcbf59a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 14 Jul 2026 19:03:32 +0300 Subject: [PATCH] fix graphics settings & reduce extra GL calls --- src/graphics/core/Batch2D.cpp | 2 +- src/graphics/core/Batch3D.cpp | 4 ++-- src/graphics/core/LineBatch.cpp | 2 +- src/graphics/core/Mesh.hpp | 11 ++++++++--- src/graphics/core/Mesh.inl | 15 ++++++++------- src/graphics/render/MainBatch.cpp | 2 +- src/graphics/render/WorldRenderer.hpp | 2 +- 7 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/graphics/core/Batch2D.cpp b/src/graphics/core/Batch2D.cpp index 7aa856f3f..33265b49f 100644 --- a/src/graphics/core/Batch2D.cpp +++ b/src/graphics/core/Batch2D.cpp @@ -372,7 +372,7 @@ void Batch2D::sprite( void Batch2D::flush() { if (index == 0) return; - mesh->reload(buffer.get(), index); + mesh->reload(buffer.get(), index, true); mesh->draw(gl::to_glenum(primitive)); index = 0; } diff --git a/src/graphics/core/Batch3D.cpp b/src/graphics/core/Batch3D.cpp index ce9f176c7..3aa996706 100644 --- a/src/graphics/core/Batch3D.cpp +++ b/src/graphics/core/Batch3D.cpp @@ -292,13 +292,13 @@ void Batch3D::point(const glm::vec3& coord, const glm::vec4& tint) { } void Batch3D::flush() { - mesh->reload(buffer.get(), index); + mesh->reload(buffer.get(), index, true); mesh->draw(); index = 0; } void Batch3D::flushPoints() { - mesh->reload(buffer.get(), index); + mesh->reload(buffer.get(), index, true); mesh->draw(GL_POINTS); index = 0; } diff --git a/src/graphics/core/LineBatch.cpp b/src/graphics/core/LineBatch.cpp index 9e33398ac..514f550d6 100644 --- a/src/graphics/core/LineBatch.cpp +++ b/src/graphics/core/LineBatch.cpp @@ -57,7 +57,7 @@ void LineBatch::box(float x, float y, float z, float w, float h, float d, void LineBatch::flush(){ if (index == 0) return; - mesh->reload(buffer.get(), index); + mesh->reload(buffer.get(), index, true); mesh->draw(GL_LINES); index = 0; } diff --git a/src/graphics/core/Mesh.hpp b/src/graphics/core/Mesh.hpp index 48c029696..2ff15cc2a 100644 --- a/src/graphics/core/Mesh.hpp +++ b/src/graphics/core/Mesh.hpp @@ -47,12 +47,15 @@ public: void reload( const VertexStructure* vertexBuffer, size_t vertexCount, - const std::vector& indices + const std::vector& indices, + bool streaming ); - void reload(const VertexStructure* vertexBuffer, size_t vertexCount) { + void reload( + const VertexStructure* vertexBuffer, size_t vertexCount, bool streaming + ) { static const std::vector indices {}; - reload(vertexBuffer, vertexCount, indices); + reload(vertexBuffer, vertexCount, indices, streaming); } /// @brief Draw mesh with specified primitives type @@ -61,6 +64,8 @@ public: /// @brief Draw mesh as triangles void draw() const; +private: + }; #include "graphics/core/Mesh.inl" diff --git a/src/graphics/core/Mesh.inl b/src/graphics/core/Mesh.inl index 913da2bce..4c6424dd1 100644 --- a/src/graphics/core/Mesh.inl +++ b/src/graphics/core/Mesh.inl @@ -46,9 +46,9 @@ Mesh::Mesh( glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); - reload(vertexBuffer, vertices, std::move(indices)); - glBindVertexArray(vao); + reload(vertexBuffer, vertices, std::move(indices), false); + // attributes int offset = 0; for (int i = 0; attrs[i].count; i++) { @@ -82,20 +82,22 @@ template void Mesh::reload( const VertexStructure* vertexBuffer, size_t vertexCount, - const std::vector& indices + const std::vector& indices, + bool streaming ) { this->vertexCount = vertexCount; - glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); if (vertexBuffer != nullptr && vertexCount != 0) { glBufferData( GL_ARRAY_BUFFER, vertexCount * sizeof(VertexStructure), vertexBuffer, - GL_STREAM_DRAW + streaming ? GL_STREAM_DRAW : GL_STATIC_DRAW ); } else { - glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STREAM_DRAW); + glBufferData( + GL_ARRAY_BUFFER, 0, {}, streaming ? GL_STREAM_DRAW : GL_STATIC_DRAW + ); } for (int i = 0; i < ibos.size(); i++) { @@ -116,7 +118,6 @@ void Mesh::reload( GL_STATIC_DRAW ); } - glBindVertexArray(0); } template diff --git a/src/graphics/render/MainBatch.cpp b/src/graphics/render/MainBatch.cpp index 1a501ca15..91364e510 100644 --- a/src/graphics/render/MainBatch.cpp +++ b/src/graphics/render/MainBatch.cpp @@ -46,7 +46,7 @@ void MainBatch::flush() { texture = blank.get(); } texture->bind(); - mesh->reload(buffer.get(), index); + mesh->reload(buffer.get(), index, true); mesh->draw(); index = 0; } diff --git a/src/graphics/render/WorldRenderer.hpp b/src/graphics/render/WorldRenderer.hpp index 49add0351..b3b10f88c 100644 --- a/src/graphics/render/WorldRenderer.hpp +++ b/src/graphics/render/WorldRenderer.hpp @@ -83,7 +83,7 @@ private: bool debug = false; bool lightsDebug = false; bool gbufferPipeline = false; - bool dirtySettings = false; + bool dirtySettings = true; /// @brief Render block selection lines void renderBlockSelection();