mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 10:59:20 +02:00
Each resident tile had a height texture and a material texture of its own, uploaded with glTexImage2D, and was drawn once per material it held with every other material's fragments discarded, all of them whether in view or not. Thousands of draws a frame. Now the tiles of a level live in one pair of texture arrays (terrain_tile_pool), a layer per tile. A pool starts at sixteen layers and doubles when full, copying what it holds on the gpu, so a level takes only the memory its tiles need; uploads go through a pixel buffer orphaned for each tile. The ground textures of every material are gathered into one sRGB array with mipmaps (terrain_ground) - drawn into it layer by layer by a small shader once the engine has each texture ready, since the scenery's textures are compressed and cannot be blitted - with a table of placeholder colours and measured tiling beside it. Each frame the tiles in range are tested against the frustum by bounding sphere and gathered per level into a buffer texture of instance data, and each level is drawn in one instanced call. The fragment shader reads the four samples around it, drops the cell when any of them carries no ground - exactly the rule the bake already assumed - and blends the ground textures of their materials by distance, so material boundaries follow the samples smoothly instead of stepping along triangle edges. State the copy pass changes is captured and put back, and the texture unit cache is reset after raw binds, so the rest of the renderer finds things as it left them. On baltyk: all 30 materials land in the array, 663 of the 1120 tiles in range are drawn after culling, and a frame is at most six terrain draws. The residency line in the log now says how many tiles in range were drawn.
63 lines
2.1 KiB
C++
63 lines
2.1 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
module;
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
export module eu07.rendering.terraintilepool;
|
|
import eu07.gl.buffer;
|
|
|
|
export {
|
|
|
|
// GPU storage for the terrain tiles of one mip level: a texture array of heights and one of
|
|
// material indices, a layer per tile. Tiles of a level all have the same side, so they share
|
|
// the arrays, and the whole level draws from them in a single call.
|
|
//
|
|
// Slots are handed out and taken back as tiles come and go. The arrays start small and double
|
|
// when full, copying what they hold on the gpu, so a level only ever takes the memory its
|
|
// tiles need. Uploads go through a pixel buffer that is orphaned for each tile, which lets the
|
|
// driver take the data without stalling on the previous transfer.
|
|
class terrain_tile_pool {
|
|
|
|
public:
|
|
explicit terrain_tile_pool( std::uint32_t const Side );
|
|
~terrain_tile_pool();
|
|
terrain_tile_pool( terrain_tile_pool const & ) = delete;
|
|
terrain_tile_pool &operator=( terrain_tile_pool const & ) = delete;
|
|
|
|
// a free layer, growing the arrays when there is none
|
|
std::uint32_t acquire();
|
|
void release( std::uint32_t const Slot );
|
|
// writes one tile's samples into its layer. both arrays hold Side * Side values
|
|
void upload( std::uint32_t const Slot, std::uint16_t const *Heights, std::uint8_t const *Materials );
|
|
|
|
std::uint32_t heights() const { return m_heights; }
|
|
std::uint32_t materials() const { return m_materials; }
|
|
std::uint32_t side() const { return m_side; }
|
|
// gpu memory the arrays occupy
|
|
std::size_t bytes() const { return static_cast<std::size_t>( m_side ) * m_side * 3u * m_capacity; }
|
|
|
|
private:
|
|
void grow();
|
|
|
|
static constexpr std::uint32_t initialcapacity { 16 };
|
|
|
|
std::uint32_t m_side { 0 };
|
|
std::uint32_t m_capacity { 0 };
|
|
std::uint32_t m_heights { 0 };
|
|
std::uint32_t m_materials { 0 };
|
|
std::optional<gl::buffer> m_unpack;
|
|
std::vector<std::uint32_t> m_free;
|
|
};
|
|
|
|
}
|