summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-08-26 20:43:03 +0200
committerStanislaw Halik <sthalik@misaki.pl>2024-09-20 16:08:45 +0200
commit1efe1e05181a9131cbcf114122513fc91e65014f (patch)
tree36ded0d6dc87bcf8b55e7c2800ebb2be177222a1
parenta0b26eea80f4703c632a60efcc4115e826e36506 (diff)
src/chunk: don't allocate empty ground/wall/scenery meshes
-rw-r--r--draw/ground.cpp2
-rw-r--r--draw/wall.cpp3
-rw-r--r--src/chunk-render.cpp6
-rw-r--r--src/chunk-scenery.cpp19
-rw-r--r--src/chunk-walls.cpp3
5 files changed, 26 insertions, 7 deletions
diff --git a/draw/ground.cpp b/draw/ground.cpp
index 515729b1..b049f96d 100644
--- a/draw/ground.cpp
+++ b/draw/ground.cpp
@@ -13,6 +13,8 @@ void ground_mesh::draw(tile_shader& shader, chunk& c)
{
constexpr int quad_index_count = 6;
const auto [mesh_, ids, size] = c.ensure_ground_mesh();
+ if (size == 0)
+ return;
struct {
ground_atlas* atlas = nullptr; size_t pos = 0; } last;
GL::MeshView mesh{mesh_};
diff --git a/draw/wall.cpp b/draw/wall.cpp
index 1c9c80cc..cbe90b23 100644
--- a/draw/wall.cpp
+++ b/draw/wall.cpp
@@ -16,6 +16,9 @@ wall_mesh::wall_mesh() = default;
void wall_mesh::draw(tile_shader& shader, chunk& c)
{
const auto [mesh_, ids, size] = c.ensure_wall_mesh();
+ if (size == 0)
+ return;
+
struct { wall_atlas* atlas = nullptr; size_t pos = 0; } last;
GL::MeshView mesh{mesh_};
[[maybe_unused]] size_t draw_count = 0;
diff --git a/src/chunk-render.cpp b/src/chunk-render.cpp
index 0cc88af2..0c5d5bc6 100644
--- a/src/chunk-render.cpp
+++ b/src/chunk-render.cpp
@@ -52,6 +52,12 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple
if (_ground->atlases[i])
_ground->indexes[count++] = uint8_t(i);
+ if (count == 0)
+ {
+ ground_mesh = GL::Mesh{NoCreate};
+ return { ground_mesh, {}, 0 };
+ }
+
std::sort(_ground->indexes.begin(), _ground->indexes.begin() + count,
[this](uint8_t a, uint8_t b) {
return _ground->atlases[a].get() < _ground->atlases[b].get();
diff --git a/src/chunk-scenery.cpp b/src/chunk-scenery.cpp
index 2e491d4d..4bf55312 100644
--- a/src/chunk-scenery.cpp
+++ b/src/chunk-scenery.cpp
@@ -177,13 +177,18 @@ auto chunk::ensure_scenery_mesh(scenery_scratch_buffers buffers) noexcept -> sce
i++;
}
- GL::Mesh mesh{GL::MeshPrimitive::Triangles};
- auto vert_view = ArrayView<const std::array<vertex, 4>>{scenery_vertexes, count};
- auto index_view = ArrayView<const std::array<UnsignedShort, 6>>{scenery_indexes, count};
- mesh.addVertexBuffer(GL::Buffer{vert_view}, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{}, tile_shader::Depth{})
- .setIndexBuffer(GL::Buffer{index_view}, 0, GL::MeshIndexType::UnsignedShort)
- .setCount(int32_t(6 * count));
- scenery_mesh = move(mesh);
+ if (count == 0)
+ scenery_mesh = GL::Mesh{NoCreate};
+ else
+ {
+ GL::Mesh mesh{GL::MeshPrimitive::Triangles};
+ auto vert_view = ArrayView<const std::array<vertex, 4>>{scenery_vertexes, count};
+ auto index_view = ArrayView<const std::array<UnsignedShort, 6>>{scenery_indexes, count};
+ mesh.addVertexBuffer(GL::Buffer{vert_view}, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{}, tile_shader::Depth{})
+ .setIndexBuffer(GL::Buffer{index_view}, 0, GL::MeshIndexType::UnsignedShort)
+ .setCount(int32_t(6 * count));
+ scenery_mesh = move(mesh);
+ }
}
const auto size = _objects.size();
diff --git a/src/chunk-walls.cpp b/src/chunk-walls.cpp
index 9971bcde..efff51b8 100644
--- a/src/chunk-walls.cpp
+++ b/src/chunk-walls.cpp
@@ -323,6 +323,9 @@ GL::Mesh chunk::make_wall_mesh()
}
}
+ if (N == 0)
+ return GL::Mesh{NoCreate};
+
ranges::sort(ranges::zip_view(vertexes.prefix(N),
ArrayView{_walls->mesh_indexes.data(), N}),
[&A = _walls->atlases](const auto& a, const auto& b) {