diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-07-22 18:56:19 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-07-22 21:17:41 +0200 |
commit | 125cb3c0feb94e51b830957fb44ccc709b6afa61 (patch) | |
tree | 9dfc68af6c2c010a31ff9d3418ccab2f221da448 /src/chunk-render.cpp | |
parent | 391e513ab8100b4ef0ab783354640fcff2be6c36 (diff) |
src/chunk: move big arrays to separate heap allocation
Diffstat (limited to 'src/chunk-render.cpp')
-rw-r--r-- | src/chunk-render.cpp | 55 |
1 files changed, 37 insertions, 18 deletions
diff --git a/src/chunk-render.cpp b/src/chunk-render.cpp index 38d35030..6dff9a40 100644 --- a/src/chunk-render.cpp +++ b/src/chunk-render.cpp @@ -16,30 +16,45 @@ static auto make_index_array(size_t max) return array; } +void chunk::ensure_alloc_ground() +{ + if (!_ground) [[unlikely]] + _ground = Pointer<ground_stuff>{InPlaceInit}; +} + +void chunk::ensure_alloc_walls() +{ + if (!_walls) [[unlikely]] + _walls = Pointer<wall_stuff>{InPlaceInit}; +} + auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple { + if (!_ground) + return { ground_mesh, {}, 0 }; + if (!_ground_modified) - return { ground_mesh, ground_indexes, size_t(ground_mesh.count()/6) }; + return { ground_mesh, _ground->ground_indexes, size_t(ground_mesh.count()/6) }; _ground_modified = false; size_t count = 0; for (auto i = 0uz; i < TILE_COUNT; i++) - if (_ground_atlases[i]) - ground_indexes[count++] = uint8_t(i); + if (_ground->_ground_atlases[i]) + _ground->ground_indexes[count++] = uint8_t(i); - std::sort(ground_indexes.begin(), ground_indexes.begin() + count, + std::sort(_ground->ground_indexes.begin(), _ground->ground_indexes.begin() + count, [this](uint8_t a, uint8_t b) { - return _ground_atlases[a] < _ground_atlases[b]; + return _ground->_ground_atlases[a] < _ground->_ground_atlases[b]; }); std::array<std::array<vertex, 4>, TILE_COUNT> vertexes; for (auto k = 0uz; k < count; k++) { - const uint8_t i = ground_indexes[k]; - const auto& atlas = _ground_atlases[i]; + const uint8_t i = _ground->ground_indexes[k]; + const auto& atlas = _ground->_ground_atlases[i]; const local_coords pos{i}; const auto quad = atlas->floor_quad(Vector3(pos) * TILE_SIZE, TILE_SIZE2); - const auto texcoords = atlas->texcoords_for_id(_ground_variants[i]); + const auto texcoords = atlas->texcoords_for_id(_ground->_ground_variants[i]); const float depth = tile_shader::depth_value(pos, tile_shader::ground_depth_offset); auto& v = vertexes[k]; for (auto j = 0uz; j < 4; j++) @@ -55,19 +70,20 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple .setIndexBuffer(GL::Buffer{vert_index_view}, 0, GL::MeshIndexType::UnsignedShort) .setCount(int32_t(6 * count)); ground_mesh = Utility::move(mesh); - return { ground_mesh, ground_indexes, count }; + return { ground_mesh, _ground->ground_indexes, count }; } fm_noinline GL::Mesh chunk::make_wall_mesh(size_t count) { + fm_debug_assert(_walls); //std::array<std::array<vertex, 4>, TILE_COUNT*2> vertexes; vertex vertexes[TILE_COUNT*2][4]; for (auto k = 0uz; k < count; k++) { - const uint16_t i = wall_indexes[k]; - const auto& atlas = _wall_atlases[i]; - const auto& variant = _wall_variants[i]; + const uint16_t i = _walls->wall_indexes[k]; + const auto& atlas = _walls->_wall_atlases[i]; + const auto& variant = _walls->_wall_variants[i]; const local_coords pos{i / 2u}; const auto center = Vector3(pos) * TILE_SIZE; const auto quad = i & 1 ? atlas->wall_quad_W(center, TILE_SIZE) : atlas->wall_quad_N(center, TILE_SIZE); @@ -91,22 +107,25 @@ GL::Mesh chunk::make_wall_mesh(size_t count) auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple { + if (!_walls) + return {wall_mesh, {}, 0}; + if (!_walls_modified) - return { wall_mesh, wall_indexes, size_t(wall_mesh.count()/6) }; + return { wall_mesh, _walls->wall_indexes, size_t(wall_mesh.count()/6) }; _walls_modified = false; size_t count = 0; for (auto i = 0uz; i < TILE_COUNT*2; i++) - if (_wall_atlases[i]) - wall_indexes[count++] = uint16_t(i); + if (_walls->_wall_atlases[i]) + _walls->wall_indexes[count++] = uint16_t(i); - std::sort(wall_indexes.data(), wall_indexes.data() + (ptrdiff_t)count, + std::sort(_walls->wall_indexes.data(), _walls->wall_indexes.data() + (ptrdiff_t)count, [this](uint16_t a, uint16_t b) { - return _wall_atlases[a] < _wall_atlases[b]; + return _walls->_wall_atlases[a] < _walls->_wall_atlases[b]; }); wall_mesh = make_wall_mesh(count); - return { wall_mesh, wall_indexes, count }; + return { wall_mesh, _walls->wall_indexes, count }; } } // namespace floormat |