From 9a6a923c68afc4e9b6f47164c7ae9b2ed67bcf77 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 7 Dec 2022 17:50:50 +0100 Subject: src, draw: don't overallocate ground & wall vbo's --- src/chunk-render.cpp | 111 +++++++++++++++++++++++++++------------------------ src/chunk.hpp | 6 ++- 2 files changed, 62 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/chunk-render.cpp b/src/chunk-render.cpp index 37440af9..cfd10307 100644 --- a/src/chunk-render.cpp +++ b/src/chunk-render.cpp @@ -1,16 +1,18 @@ #include "chunk.hpp" #include "tile-atlas.hpp" +#include "anim-atlas.hpp" #include "shaders/tile.hpp" #include -#include #include +#include namespace floormat { -static auto make_index_array(std::size_t offset) +template +static auto make_index_array(std::size_t offset, std::size_t max) { - std::array, TILE_COUNT> array; // NOLINT(cppcoreguidelines-pro-type-member-init) - for (std::size_t i = 0; i < TILE_COUNT; i++) + std::array, N*TILE_COUNT> array; // NOLINT(cppcoreguidelines-pro-type-member-init) + for (std::size_t i = 0; i < max; i++) array[i] = tile_atlas::indices(i + offset); return array; } @@ -24,84 +26,87 @@ struct vertex { auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple { if (!_ground_modified) - return { ground_mesh, ground_indexes }; + return { ground_mesh, ground_indexes, std::size_t(ground_mesh.count()/6) }; _ground_modified = false; + std::size_t count = 0; for (std::size_t i = 0; i < TILE_COUNT; i++) - ground_indexes[i] = std::uint8_t(i); - std::sort(ground_indexes.begin(), ground_indexes.end(), [this](std::uint8_t a, std::uint8_t b) { - return _ground_atlases[a].get() < _ground_atlases[b].get(); - }); + if (_ground_atlases[i]) + ground_indexes[count++] = std::uint8_t(i); + + std::sort(ground_indexes.begin(), ground_indexes.begin() + count, + [this](std::uint8_t a, std::uint8_t b) { + return _ground_atlases[a] < _ground_atlases[b]; + }); std::array, TILE_COUNT> vertexes; - for (std::size_t k = 0; k < TILE_COUNT; k++) + for (std::size_t k = 0; k < count; k++) { const std::uint8_t i = ground_indexes[k]; - if (auto atlas = _ground_atlases[i]; !atlas) - vertexes[k] = {}; - else - { - const local_coords pos{i}; - const auto quad = atlas->floor_quad(Vector3(pos.x, pos.y, 0) * TILE_SIZE, TILE_SIZE2); - const auto texcoords = atlas->texcoords_for_id(_ground_variants[i]); - const float depth = tile_shader::depth_value(pos); - auto& v = vertexes[k]; - for (std::size_t j = 0; j < 4; j++) - v[j] = { quad[j], texcoords[j], depth }; - } + const auto& atlas = _ground_atlases[i]; + const local_coords pos{i}; + const auto quad = atlas->floor_quad(Vector3(pos.x, pos.y, 0) * TILE_SIZE, TILE_SIZE2); + const auto texcoords = atlas->texcoords_for_id(_ground_variants[i]); + const float depth = tile_shader::depth_value(pos); + auto& v = vertexes[k]; + for (std::size_t j = 0; j < 4; j++) + v[j] = { quad[j], texcoords[j], depth }; } - const auto indexes = make_index_array(0); + + const auto indexes = make_index_array(0, count); + const auto vertex_view = ArrayView{vertexes.data(), count}; + const auto vert_index_view = ArrayView{indexes.data(), count}; GL::Mesh mesh{GL::MeshPrimitive::Triangles}; - mesh.addVertexBuffer(GL::Buffer{vertexes}, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{}, tile_shader::Depth{}) - .setIndexBuffer(GL::Buffer{indexes}, 0, GL::MeshIndexType::UnsignedShort) - .setCount(6 * TILE_COUNT); + mesh.addVertexBuffer(GL::Buffer{vertex_view}, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{}, tile_shader::Depth{}) + .setIndexBuffer(GL::Buffer{vert_index_view}, 0, GL::MeshIndexType::UnsignedShort) + .setCount(std::int32_t(6 * count)); ground_mesh = Utility::move(mesh); - return { ground_mesh, ground_indexes }; + return { ground_mesh, ground_indexes, count }; } auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple { if (!_walls_modified) - return { wall_mesh, wall_indexes }; + return { wall_mesh, wall_indexes, std::size_t(wall_mesh.count()/6) }; _walls_modified = false; + std::size_t count = 0; for (std::size_t i = 0; i < TILE_COUNT*2; i++) - wall_indexes[i] = std::uint16_t(i); + if (_wall_atlases[i]) + wall_indexes[count++] = std::uint16_t(i); - std::sort(wall_indexes.begin(), wall_indexes.end(), [this](std::uint16_t a, std::uint16_t b) { - return _wall_atlases[a] < _wall_atlases[b]; - }); + std::sort(wall_indexes.begin(), wall_indexes.begin() + count, + [this](std::uint16_t a, std::uint16_t b) { + return _wall_atlases[a] < _wall_atlases[b]; + }); std::array, TILE_COUNT*2> vertexes; - for (std::size_t k = 0; k < TILE_COUNT*2; k++) + for (std::size_t k = 0; k < count; k++) { const std::uint16_t i = wall_indexes[k]; - if (const auto& atlas = _wall_atlases[i]; !atlas) - vertexes[k] = {}; - else - { - const auto& variant = _wall_variants[i]; - const local_coords pos{i / 2u}; - const auto center = Vector3(pos.x, pos.y, 0) * TILE_SIZE; - const auto quad = i & 1 ? atlas->wall_quad_W(center, TILE_SIZE) : atlas->wall_quad_N(center, TILE_SIZE); - const float depth = tile_shader::depth_value(pos); - const auto texcoords = atlas->texcoords_for_id(variant); - auto& v = vertexes[k]; - for (std::size_t j = 0; j < 4; j++) - v[j] = { quad[j], texcoords[j], depth, }; - } + const auto& atlas = _wall_atlases[i]; + const auto& variant = _wall_variants[i]; + const local_coords pos{i / 2u}; + const auto center = Vector3(pos.x, pos.y, 0) * TILE_SIZE; + const auto quad = i & 1 ? atlas->wall_quad_W(center, TILE_SIZE) : atlas->wall_quad_N(center, TILE_SIZE); + const float depth = tile_shader::depth_value(pos); + const auto texcoords = atlas->texcoords_for_id(variant); + auto& v = vertexes[k]; + for (std::size_t j = 0; j < 4; j++) + v[j] = { quad[j], texcoords[j], depth, }; } - using index_t = std::array, TILE_COUNT>; - const index_t indexes[2] = { make_index_array(0), make_index_array(TILE_COUNT) }; + auto indexes = make_index_array<2>(0, count); + const auto vertex_view = ArrayView{vertexes.data(), count}; + const auto vert_index_view = ArrayView{indexes.data(), count}; GL::Mesh mesh{GL::MeshPrimitive::Triangles}; - mesh.addVertexBuffer(GL::Buffer{vertexes}, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{}, tile_shader::Depth{}) - .setIndexBuffer(GL::Buffer{indexes}, 0, GL::MeshIndexType::UnsignedShort) - .setCount(6 * TILE_COUNT); + mesh.addVertexBuffer(GL::Buffer{vertex_view}, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{}, tile_shader::Depth{}) + .setIndexBuffer(GL::Buffer{vert_index_view}, 0, GL::MeshIndexType::UnsignedShort) + .setCount(std::int32_t(6 * count)); wall_mesh = Utility::move(mesh); - return { wall_mesh, wall_indexes }; + return { wall_mesh, wall_indexes, count }; } } // namespace floormat diff --git a/src/chunk.hpp b/src/chunk.hpp index 3a6ea3df..c061d070 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -63,11 +63,13 @@ struct chunk final struct ground_mesh_tuple final { GL::Mesh& mesh; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) - const std::array& ids; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) + const ArrayView ids; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) + const std::size_t size; }; struct wall_mesh_tuple final { GL::Mesh& mesh; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) - const std::array& ids; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) + const ArrayView ids; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) + const std::size_t size; }; ground_mesh_tuple ensure_ground_mesh() noexcept; -- cgit v1.2.3