diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-12-08 08:13:37 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-12-08 08:29:12 +0100 |
commit | ab31f0ed81301c23d6847f3ca1513d4b90275de7 (patch) | |
tree | 735d63bd525eccabc43ebc75e804469e898950af /src | |
parent | b26ddfcee60de0951f0feaf4ae4e12551853ee21 (diff) |
draw, main, src/chunk: batch scenery writes
Diffstat (limited to 'src')
-rw-r--r-- | src/chunk-render.cpp | 53 | ||||
-rw-r--r-- | src/chunk.cpp | 1 | ||||
-rw-r--r-- | src/chunk.hpp | 20 |
3 files changed, 65 insertions, 9 deletions
diff --git a/src/chunk-render.cpp b/src/chunk-render.cpp index cfd10307..f7b0678a 100644 --- a/src/chunk-render.cpp +++ b/src/chunk-render.cpp @@ -9,11 +9,11 @@ namespace floormat { template<std::size_t N = 1> -static auto make_index_array(std::size_t offset, std::size_t max) +static auto make_index_array(std::size_t max) { std::array<std::array<UnsignedShort, 6>, 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); + array[i] = tile_atlas::indices(i); return array; } @@ -53,7 +53,7 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple v[j] = { quad[j], texcoords[j], depth }; } - const auto indexes = make_index_array(0, count); + const auto indexes = make_index_array(count); const auto vertex_view = ArrayView{vertexes.data(), count}; const auto vert_index_view = ArrayView{indexes.data(), count}; @@ -97,7 +97,7 @@ auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple v[j] = { quad[j], texcoords[j], depth, }; } - auto indexes = make_index_array<2>(0, count); + auto indexes = make_index_array<2>(count); const auto vertex_view = ArrayView{vertexes.data(), count}; const auto vert_index_view = ArrayView{indexes.data(), count}; @@ -109,4 +109,49 @@ auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple return { wall_mesh, wall_indexes, count }; } +auto chunk::ensure_scenery_mesh() noexcept -> scenery_mesh_tuple +{ + if (!_scenery_modified) + return { scenery_mesh, scenery_indexes, std::size_t(scenery_mesh.count()/6) }; + _scenery_modified = false; + + std::size_t count = 0; + for (std::size_t i = 0; i < TILE_COUNT; i++) + if (const auto& atlas = _scenery_atlases[i]; atlas && atlas->info().fps == 0) + scenery_indexes[count++] = std::uint8_t(i); + +#if 0 + std::sort(scenery_indexes.begin(), scenery_indexes.begin() + count, + [this](std::uint8_t a, std::uint8_t b) { + return _scenery_atlases[a] < _scenery_atlases[b]; + }); +#endif + std::array<std::array<vertex, 4>, TILE_COUNT> vertexes; + for (std::size_t k = 0; k < count; k++) + { + const std::uint8_t i = scenery_indexes[k]; + const local_coords pos{i}; + const auto& atlas = _scenery_atlases[i]; + const auto& fr = _scenery_variants[i]; + const auto quad = atlas->frame_quad(Vector3(pos.x, pos.y, 0) * TILE_SIZE, fr.r, fr.frame); + const auto& group = atlas->group(fr.r); + const auto texcoords = atlas->texcoords_for_frame(fr.r, fr.frame, !group.mirror_from.isEmpty()); + const float depth = tile_shader::depth_value(pos, tile_shader::scenery_depth_offset); + 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(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{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)); + scenery_mesh = Utility::move(mesh); + return { scenery_mesh, scenery_indexes, count }; +} + } // namespace floormat diff --git a/src/chunk.cpp b/src/chunk.cpp index cb9760e1..94506f1a 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -16,6 +16,7 @@ bool chunk::empty(bool force) const noexcept tile_atlas* chunk::ground_atlas_at(std::size_t i) const noexcept { return _ground_atlases[i].get(); } tile_atlas* chunk::wall_atlas_at(std::size_t i) const noexcept { return _wall_atlases[i].get(); } +anim_atlas* chunk::scenery_atlas_at(std::size_t i) const noexcept { return _scenery_atlases[i].get(); } tile_ref chunk::operator[](std::size_t idx) noexcept { return { *this, std::uint8_t(idx) }; } tile_proto chunk::operator[](std::size_t idx) const noexcept { return tile_proto(tile_ref { *const_cast<chunk*>(this), std::uint8_t(idx) }); } diff --git a/src/chunk.hpp b/src/chunk.hpp index 2cf7278e..d0df8e5d 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -66,13 +66,19 @@ struct chunk final void mark_modified() noexcept; struct ground_mesh_tuple final { - GL::Mesh& mesh; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) - const ArrayView<const std::uint8_t> ids; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) + GL::Mesh& mesh; + const ArrayView<const std::uint8_t> ids; const std::size_t size; }; struct wall_mesh_tuple final { - GL::Mesh& mesh; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) - const ArrayView<const std::uint16_t> ids; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) + GL::Mesh& mesh; + const ArrayView<const std::uint16_t> ids; + const std::size_t size; + }; + + struct scenery_mesh_tuple final { + GL::Mesh& mesh; + const ArrayView<const std::uint8_t> ids; const std::size_t size; }; @@ -82,6 +88,9 @@ struct chunk final wall_mesh_tuple ensure_wall_mesh() noexcept; tile_atlas* wall_atlas_at(std::size_t i) const noexcept; + scenery_mesh_tuple ensure_scenery_mesh() noexcept; + anim_atlas* scenery_atlas_at(std::size_t i) const noexcept; + void ensure_passability() noexcept; #ifdef FLOORMAT_64 @@ -106,6 +115,7 @@ private: std::array<std::uint16_t, TILE_COUNT*2> wall_indexes = {}; std::array<variant_t, TILE_COUNT*2> _wall_variants = {}; std::array<std::shared_ptr<anim_atlas>, TILE_COUNT> _scenery_atlases; + std::array<std::uint8_t, TILE_COUNT> scenery_indexes = {}; std::array<scenery, TILE_COUNT> _scenery_variants = {}; template<bool> struct lqt_ops; @@ -113,7 +123,7 @@ private: std::unique_ptr<lqt> _lqt_move, _lqt_shoot, _lqt_view; std::vector<loose_quadtree::BoundingBox<std::int16_t>> _bboxes; - GL::Mesh ground_mesh{NoCreate}, wall_mesh{NoCreate}; + GL::Mesh ground_mesh{NoCreate}, wall_mesh{NoCreate}, scenery_mesh{NoCreate}; mutable bool _maybe_empty : 1 = true, _ground_modified : 1 = true, _walls_modified : 1 = true, |