17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 22:39:19 +02:00

Cook the ground into a quadtree of mesh tiles, and keep the mesh the scenery drew

The ground in a scenery is already an irregular mesh, so the cook keeps it: it cuts the
triangles along the tile grid, welds what coincides, and builds each coarser level by
collapsing edges. Nothing is resampled, nothing is interpolated, and a hole the scenery left
stays a hole. What was here before threw the triangles away, kept their vertices and built a
fresh Delaunay triangulation over them, which filled every hole, merged the two heights of
every retaining wall into one, and - because it never erased the super-triangle - emitted
indices three too large together with triangles reaching out to the artificial corners.

Tiles form a quadtree, as Cesium Quantized Mesh intends. A distant tile is a large one, so the
horizon costs tens of tiles rather than the thousands a fixed grid needed, and the level to
draw follows from the tile's own geometric error against what the screen can resolve - which is
what the format carries that field for. Every vertex on a tile's border survives into all of
its coarser levels, and so does every edge between two of them, so two neighbours drawn at
different levels share their border exactly: there is nothing to stitch and nothing to morph.

The file layout is the format's, with two deliberate departures, both written down in
quantizedmeshformat.h: the geodetic header fields carry scenery metres, there being no
geographic reference to convert from, and the material a vertex belongs to rides in an
extension of ours. Texture coordinates are not stored at all - ground tiles in world space, so
the coordinate follows from the position and from how many metres one repeat of the material
covers, measured off the scenery once and kept in the terrain's table.

Tiles live in one archive beside the scenery, an entry each, compressed on its own. Baltyk
cooks to 546 kB against 2.2 MB of heightfields, quark to 361 kB against 9.6 MB, tomaszewo to
375 kB against 9.9 MB.

Drawing is one call a frame: every resident tile lives in the same vertex and index buffer,
with its vertices measured from the middle of the terrain so that the whole map fits in floats,
and a frame is one glMultiDrawElementsBaseVertex over the tiles the walk chose and the frustum
kept. On baltyk that settles at 291 tiles resident, 16 MB of mesh, 85 tiles and 6k triangles
drawn.

What counts as ground is what the scenery says, not what its triangles look like. A node under
origin, rotate or scale is an object standing on the ground; a material with an alpha channel is
a tree, a bush, a fence or a catenary mast. Everything else is laid whichever way it faces, so
the side of an embankment or a cutting goes in with the rest instead of leaving a hole along
every bank. Whatever is left out is recorded per node, and the scenery goes on drawing it.

A cook is a cache: it is used again only when the archive, the note of which nodes went into it
and the manifest of what the scenery was made from all agree. A manifest listing a file the
scenery names but does not have used to fail that test for ever, since an absent file never
matches - quark has such an include, and nothing beside it could ever be reused.

tools/terraincook cooks the same way outside the engine, and meshtest beside it checks what the
cook promises: a tile reads back as written, neighbouring tiles agree on their border to the
millimetre, a hole is not filled in, a wall is laid exactly once, and a material's tiling comes
out of the source. seamcheck.py goes: it tested the morphing constants of a heightfield nothing
reads any more.
This commit is contained in:
maj00r
2026-09-20 08:04:46 +02:00
parent 975e62df95
commit b877bc0772
36 changed files with 3356 additions and 3614 deletions

View File

@@ -9,28 +9,27 @@ http://mozilla.org/MPL/2.0/.
module;
#include <algorithm>
#include <cmath>
#include <array>
#include <cmath>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "glad/glad.h"
#include "scene/heightfieldformat.h"
#include "scene/heightfieldreader.h"
#include "scene/quantizedmeshreader.h"
module eu07.rendering.terrainclipmap;
import eu07.glm;
import eu07.utilities.logs;
import eu07.model.texture;
terrain_clipmap::~terrain_clipmap() {
close();
}
@@ -39,19 +38,34 @@ terrain_clipmap::open( std::string const &Path ) {
close();
if( ( false == m_reader.open( Path ) )
|| ( false == m_loader.open( Path ) ) ) {
ErrorLog( "Terrain: cannot open heightfield \"" + Path + "\"" );
if( false == m_reader.open( Path ) ) {
ErrorLog( "Terrain: cannot read cooked terrain \"" + Path + "\"" );
return false;
}
m_table = m_reader.table();
m_levels = std::min<std::uint32_t>( m_table.levels, terrain_maxlevels );
m_finesttile = static_cast<float>( m_table.tilesize( m_levels - 1 ) );
// every vertex of every tile is measured from the middle of the terrain, so that the whole of
// it fits in floats: at ten kilometres a float still holds a millimetre
m_origin = glm::dvec3 {
m_table.west + m_table.side * 0.5,
( m_table.lowest + m_table.highest ) * 0.5,
m_table.north + m_table.side * 0.5 };
if( false == m_loader.open( Path, m_origin.x, m_origin.y, m_origin.z ) ) {
ErrorLog( "Terrain: cannot open \"" + Path + "\" for reading" );
close();
return false;
}
if( false == m_arena.create() ) {
ErrorLog( "Terrain: cannot allocate the terrain buffers" );
close();
return false;
}
m_path = Path;
build_indices();
try {
gl::shader vertex( "terrainclipmap.vert" );
gl::shader fragment( "terrainclipmap.frag" );
gl::shader vertex( "terrainmesh.vert" );
gl::shader fragment( "terrainmesh.frag" );
m_shader.emplace( std::vector<std::reference_wrapper<gl::shader const>>( { vertex, fragment } ) );
}
catch( gl::shader_exception const &error ) {
@@ -59,48 +73,44 @@ terrain_clipmap::open( std::string const &Path ) {
close();
return false;
}
if( false == m_ground.create( m_reader.materials(), m_reader.material_scales() ) ) {
std::vector<std::string> names;
std::vector<float> repeats;
names.reserve( m_table.materials.size() );
repeats.reserve( m_table.materials.size() );
for( auto const &material : m_table.materials ) {
names.push_back( material.name );
repeats.push_back( material.repeat );
}
if( false == m_ground.create( names, repeats ) ) {
ErrorLog( "Terrain: ground textures failed" );
close();
return false;
}
for( std::uint32_t level = 0; level < m_reader.levels(); ++level ) {
m_pools.emplace_back( std::make_unique<terrain_tile_pool>( m_reader.samples_per_side( level ) ) );
}
m_instances.resize( m_reader.levels() );
m_instancebuffer.emplace();
::glGenTextures( 1, &m_instancetexture );
opengl_texture::reset_unit_cache();
m_vao.emplace();
resolve_uniforms();
m_path = Path;
m_ready = true;
WriteLog(
"Terrain: " + Path + ", " + std::to_string( m_reader.tiles().size() ) + " tiles, "
+ std::to_string( m_reader.levels() ) + " levels, grid "
+ std::to_string( m_reader.gridstep() ) + " m, tile " + std::to_string( m_reader.tilesize() ) + " m" );
"Terrain: " + Path + ", " + std::to_string( m_levels ) + " levels, finest tile "
+ std::to_string( static_cast<int>( m_finesttile ) ) + " m over "
+ std::to_string( static_cast<int>( m_table.side ) ) + " m, "
+ std::to_string( m_reader.present().size() ) + " tiles, "
+ std::to_string( m_table.materials.size() ) + " ground materials" );
return true;
}
// uniform locations do not change once the program is linked, so they are looked up here
// rather than per draw
// uniform locations do not change once the program is linked, so they are looked up here rather
// than per draw
void
terrain_clipmap::resolve_uniforms() {
auto const program { static_cast<GLuint>( *m_shader ) };
m_uniforms.samplestep = ::glGetUniformLocation( program, "samplestep" );
m_uniforms.side = ::glGetUniformLocation( program, "side" );
m_uniforms.morph = ::glGetUniformLocation( program, "morph" );
m_uniforms.heightbias = ::glGetUniformLocation( program, "heightbias" );
m_uniforms.heightscale = ::glGetUniformLocation( program, "heightscale" );
m_uniforms.nodata = ::glGetUniformLocation( program, "nodata" );
m_uniforms.heights = ::glGetUniformLocation( program, "heights" );
m_uniforms.materials = ::glGetUniformLocation( program, "materials" );
m_uniforms.instances = ::glGetUniformLocation( program, "instances" );
m_uniforms.origin = ::glGetUniformLocation( program, "terrainorigin" );
m_uniforms.world = ::glGetUniformLocation( program, "terrainworld" );
m_uniforms.ground = ::glGetUniformLocation( program, "ground" );
m_uniforms.materialtable = ::glGetUniformLocation( program, "materialtable" );
m_uniforms.materials = ::glGetUniformLocation( program, "materialtable" );
}
void
@@ -108,367 +118,368 @@ terrain_clipmap::close() {
// the loader first, so nothing arrives for tiles that are being torn down
m_loader.close();
m_tiles.clear();
m_chosen.clear();
m_wishes.clear();
m_wanted.clear();
m_arrived.clear();
m_inrange.clear();
m_wantedlevel.clear();
m_scanrange = -1.f;
m_tiles.clear();
m_pools.clear();
m_indices.clear();
m_indexcounts.clear();
m_instances.clear();
m_instancebuffer.reset();
if( m_instancetexture != 0 ) { ::glDeleteTextures( 1, &m_instancetexture ); m_instancetexture = 0; }
m_asked.clear();
m_readiness.clear();
m_arena.destroy();
m_ground.destroy();
m_vao.reset();
m_shader.reset();
m_reader.close();
m_table = {};
m_stats = {};
m_walked = false;
m_walkrange = -1.f;
m_ready = false;
m_path.clear();
}
// index buffers, one per mip level. the grid they describe is the same shape at every
// level, only smaller, and the vertices themselves never exist in memory: the shader
// derives a sample's row and column from gl_VertexID
void
terrain_clipmap::build_indices() {
float
terrain_clipmap::leveltile( std::uint32_t const Level ) const {
m_indices.resize( m_reader.levels() );
m_indexcounts.resize( m_reader.levels() );
std::vector<std::uint32_t> indices;
for( std::uint32_t level = 0; level < m_reader.levels(); ++level ) {
auto const side { m_reader.samples_per_side( level ) };
indices.clear();
indices.reserve( static_cast<std::size_t>( side - 1 ) * ( side - 1 ) * 6 );
for( std::uint32_t row = 0; row + 1 < side; ++row ) {
for( std::uint32_t column = 0; column + 1 < side; ++column ) {
auto const topleft { row * side + column };
auto const topright { topleft + 1 };
auto const bottomleft { topleft + side };
auto const bottomright { bottomleft + 1 };
indices.push_back( topleft ); indices.push_back( bottomleft ); indices.push_back( topright );
indices.push_back( topright ); indices.push_back( bottomleft ); indices.push_back( bottomright );
}
}
m_indexcounts[ level ] = static_cast<std::uint32_t>( indices.size() );
m_indices[ level ] = std::make_unique<gl::buffer>();
m_indices[ level ]->allocate(
gl::buffer::ELEMENT_ARRAY_BUFFER, indices.size() * sizeof( std::uint32_t ), GL_STATIC_DRAW );
m_indices[ level ]->upload(
gl::buffer::ELEMENT_ARRAY_BUFFER, indices.data(), 0, indices.size() * sizeof( std::uint32_t ) );
}
return static_cast<float>( m_table.tilesize( Level ) );
}
float
terrain_clipmap::levelerror( std::uint32_t const Level ) const {
std::uint32_t
terrain_clipmap::level_for( double const Distance ) const {
if( Distance <= m_finest ) { return 0; }
auto const steps { static_cast<std::uint32_t>( std::log2( Distance / m_finest ) ) + 1 };
return std::min( steps, m_reader.levels() - 1 );
}
void
terrain_clipmap::upload( terrain_tile_loader::payload const &Tile ) {
auto const tilekey { key( Tile.tile.x, Tile.tile.z ) };
auto const existing { m_tiles.find( tilekey ) };
if( existing != m_tiles.end() ) {
// a level change moves the tile to another pool
retire( existing->second );
m_tiles.erase( existing );
}
auto &pool { *m_pools[ Tile.tile.level ] };
resident_tile tile;
tile.x = Tile.tile.x;
tile.z = Tile.tile.z;
tile.level = Tile.tile.level;
tile.slot = pool.acquire();
tile.lastused = m_frame;
pool.upload( tile.slot, Tile.heights.data(), Tile.materials.data() );
if( auto const *description { m_reader.find( Tile.tile.x, Tile.tile.z ) } ) {
tile.minheight = description->minheight;
tile.maxheight = description->maxheight;
}
++m_stats.uploaded;
m_tiles.emplace( tilekey, tile );
}
void
terrain_clipmap::retire( resident_tile const &Tile ) {
m_pools[ Tile.level ]->release( Tile.slot );
return m_table.error( Level );
}
void
terrain_clipmap::detail( double const Pixelsperunit, double const Pixels ) {
m_finestwanted = terrain_finest_range( m_reader.gridstep(), m_reader.tilesize(), Pixelsperunit, Pixels );
m_pixelsperunit = Pixelsperunit;
m_detail = std::max( 0.1, Pixels );
}
void
terrain_clipmap::scan( glm::dvec3 const &Viewpoint ) {
terrain_clipmap::square( quantizedmesh::tile_address const &Tile, double &West, double &North, double &Side ) const {
m_scanpoint = Viewpoint;
m_scanrange = m_range;
m_finest = ( m_finestwanted > 0.0 ? m_finestwanted : terrain_finest_tiles * m_reader.tilesize() );
m_inrange.clear();
m_wantedlevel.clear();
Side = m_table.tilesize( Tile.level );
West = m_table.west + Tile.x * Side;
North = m_table.north + Tile.z * Side;
}
auto const tilesize { static_cast<double>( m_reader.tilesize() ) };
auto const reach { std::max( 1, static_cast<std::int32_t>( m_range / tilesize ) + 1 ) };
auto const centrex { m_reader.tile_x( Viewpoint.x ) };
auto const centrez { m_reader.tile_z( Viewpoint.z ) };
std::vector<std::pair<double, terrain_tile_loader::request>> wishes;
// the distance to the nearest point of the tile's square, not to its centre: a coarse tile is
// kilometres across, and the one the camera stands on has its centre far outside any draw range
double
terrain_clipmap::distance_to( quantizedmesh::tile_address const &Tile, glm::dvec3 const &Viewpoint ) const {
for( std::int32_t z = centrez - reach; z <= centrez + reach; ++z ) {
for( std::int32_t x = centrex - reach; x <= centrex + reach; ++x ) {
double west { 0.0 }, north { 0.0 }, side { 0.0 };
square( Tile, west, north, side );
auto const nearestx { std::clamp( Viewpoint.x, west, west + side ) };
auto const nearestz { std::clamp( Viewpoint.z, north, north + side ) };
return glm::length( glm::dvec2 { nearestx - Viewpoint.x, nearestz - Viewpoint.z } );
}
if( false == m_reader.contains( x, z ) ) { continue; }
bool
terrain_clipmap::present( quantizedmesh::tile_address const &Tile ) const {
// distance to the nearest point of the tile, not to its centre. a regional
// field has tiles kilometres across, and a tile the camera is standing on
// can have its centre far outside the draw range
auto const west { x * tilesize };
auto const north { z * tilesize };
auto const nearestx { std::clamp( Viewpoint.x, west, west + tilesize ) };
auto const nearestz { std::clamp( Viewpoint.z, north, north + tilesize ) };
auto const distance {
glm::length( glm::dvec2 { nearestx - Viewpoint.x, nearestz - Viewpoint.z } ) };
if( distance > m_range ) { continue; }
return m_reader.present().contains( quantizedmesh::tile_key( Tile ) );
}
auto const tilekey { key( x, z ) };
auto const level { level_for( distance ) };
m_inrange.push_back( { tilekey, level } );
m_wantedlevel.emplace( tilekey, level );
// Whether the ground a tile stands for is close enough to the real thing. The tile's error, at its
// distance, covers so many pixels; while that is fewer than the detail asks for there is nothing to
// be had by splitting it. The tile the camera stands on is always split, whatever its error, so that
// what is underfoot is the ground as the scenery drew it.
bool
terrain_clipmap::good_enough( quantizedmesh::tile_address const &Tile, double const Distance ) const {
auto const resident { m_tiles.find( tilekey ) };
if( ( resident == m_tiles.end() ) || ( resident->second.level != level ) ) {
wishes.push_back( { distance, { x, z, level } } );
}
}
if( Tile.level + 1 >= m_levels ) { return true; }
if( Distance <= 0.0 ) { return false; }
return terrain_level_enough( m_table.error( Tile.level ), Distance, m_pixelsperunit, m_detail );
}
// Walks down to the levels the screen wants and asks the loader for whatever of them is not on the
// card, deepest tile of the wanted cut included - not only the tiles one level down. Asking one level
// at a time is what a first attempt at this did, and it stops dead: a tile whose children are all
// resident asks for nothing, while what it is waiting for is their children.
//
// Returns whether the cut under this tile, and the tile itself where the cut ends at it, is there. A
// tile whose subtree is not ready yet is asked for as well, so that something coarse covers the
// ground rather than a hole while the rest is read.
bool
terrain_clipmap::need( quantizedmesh::tile_address const &Tile, glm::dvec3 const &Viewpoint ) {
if( false == present( Tile ) ) { return true; } // no ground here to draw
auto const distance { distance_to( Tile, Viewpoint ) };
if( distance > m_range ) { return true; } // nothing wanted this far out
auto const key { quantizedmesh::tile_key( Tile ) };
auto const resident { m_tiles.contains( key ) };
if( true == good_enough( Tile, distance ) ) {
if( false == resident ) { ask_for( Tile, distance ); }
m_readiness.emplace( key, resident );
return resident;
}
auto ready { true };
for( std::int32_t stepz = 0; stepz < 2; ++stepz ) {
for( std::int32_t stepx = 0; stepx < 2; ++stepx ) {
// every child is asked, so the whole cut is queued at once rather than a level a frame
ready = need( { Tile.level + 1, Tile.x * 2 + stepx, Tile.z * 2 + stepz }, Viewpoint ) && ready;
}
}
if( ( false == ready ) && ( false == resident ) ) { ask_for( Tile, distance ); }
m_readiness.emplace( key, ready );
return ready;
}
void
terrain_clipmap::choose( quantizedmesh::tile_address const &Tile, glm::dvec3 const &Viewpoint ) {
if( false == present( Tile ) ) { return; }
auto const distance { distance_to( Tile, Viewpoint ) };
if( distance > m_range ) { return; }
auto const key { quantizedmesh::tile_key( Tile ) };
auto const ready { m_readiness.find( key ) };
if( ( ready != m_readiness.end() ) && ( true == ready->second )
&& ( false == good_enough( Tile, distance ) ) ) {
// the whole cut under this tile is on the card: draw that instead
for( std::int32_t stepz = 0; stepz < 2; ++stepz ) {
for( std::int32_t stepx = 0; stepx < 2; ++stepx ) {
choose( { Tile.level + 1, Tile.x * 2 + stepx, Tile.z * 2 + stepz }, Viewpoint );
}
}
return;
}
// either this level is good enough, or what is below it is still being read: this tile covers
// the ground meanwhile, and nothing under it is drawn, so nothing is drawn twice
if( true == m_tiles.contains( key ) ) { m_chosen.push_back( key ); }
}
void
terrain_clipmap::ask_for( quantizedmesh::tile_address const &Tile, double const Distance ) {
if( false == m_asked.insert( quantizedmesh::tile_key( Tile ) ).second ) { return; }
m_wishes.emplace_back( Distance, Tile );
}
void
terrain_clipmap::walk( glm::dvec3 const &Viewpoint ) {
m_walkpoint = Viewpoint;
m_walkrange = m_range;
m_walkdetail = m_detail;
m_walked = true;
m_chosen.clear();
m_wishes.clear();
m_asked.clear();
m_readiness.clear();
need( { 0, 0, 0 }, Viewpoint );
choose( { 0, 0, 0 }, Viewpoint );
// nearest first, so the ground under the camera is never waiting behind the horizon
std::sort( wishes.begin(), wishes.end(),
std::sort( m_wishes.begin(), m_wishes.end(),
[]( auto const &Left, auto const &Right ) { return Left.first < Right.first; } );
m_wanted.clear();
for( auto const &wish : wishes ) { m_wanted.push_back( wish.second ); }
m_wanted.reserve( m_wishes.size() );
for( auto const &wish : m_wishes ) { m_wanted.push_back( wish.second ); }
m_loader.want( m_wanted );
m_stats.chosen = m_chosen.size();
m_stats.wanted = m_wanted.size();
}
void
terrain_clipmap::put_in( terrain_tile_loader::payload const &Tile ) {
auto const key { quantizedmesh::tile_key( Tile.data.tile ) };
auto const existing { m_tiles.find( key ) };
if( existing != m_tiles.end() ) {
// already there; nothing about a tile changes between reads
++m_stats.dropped;
return;
}
auto piece { m_arena.put( Tile.data ) };
if( false == piece.held() ) { return; }
resident_tile tile;
tile.piece = piece;
tile.centre = glm::dvec3 { Tile.data.centrex, Tile.data.centrey, Tile.data.centrez };
tile.radius = static_cast<float>( Tile.data.radius );
tile.level = Tile.data.tile.level;
tile.lastused = m_frame;
m_tiles.emplace( key, tile );
++m_stats.uploaded;
}
// What is held is what the walk has been choosing or asking for. Once the buffers are fuller than
// the budget, whatever has gone longest without either is given back - never something the walk
// wants now, because a coarse tile it is drawing is what stands in for its children while they are
// read.
void
terrain_clipmap::retire_unused() {
if( m_arena.bytes() <= m_budget ) { return; }
std::vector<std::pair<std::uint64_t, std::int64_t>> aged;
aged.reserve( m_tiles.size() );
for( auto const &[ key, tile ] : m_tiles ) {
if( tile.lastused != m_frame ) { aged.emplace_back( tile.lastused, key ); }
}
if( true == aged.empty() ) { return; }
std::sort( aged.begin(), aged.end() );
// a fifth of what is held, so that this does not run every frame
auto const letgo { std::max<std::size_t>( 1, m_tiles.size() / 5 ) };
for( std::size_t index = 0; ( index < letgo ) && ( index < aged.size() ); ++index ) {
auto const found { m_tiles.find( aged[ index ].second ) };
if( found == m_tiles.end() ) { continue; }
m_arena.take( found->second.piece );
m_tiles.erase( found );
}
}
void
terrain_clipmap::update( glm::dvec3 const &Viewpoint ) {
if( false == m_ready ) { return; }
++m_frame;
auto const moved {
glm::length( glm::dvec2 { Viewpoint.x - m_scanpoint.x, Viewpoint.z - m_scanpoint.z } ) };
// a zoom or a resized window changes the levels; a change under a percent is not worth
// redoing the scan for
auto const detailchanged {
( m_finestwanted > 0.0 ) && ( std::abs( m_finestwanted - m_finest ) > 0.01 * m_finest ) };
if( ( m_range != m_scanrange )
|| ( true == detailchanged )
|| ( moved > m_reader.tilesize() * terrain_rescan_tiles ) ) {
scan( Viewpoint );
}
// the tiles in range are touched every frame, which is what render() draws and what
// eviction spares
m_stats.inview = 0;
m_stats.wanted = 0;
m_stats.perlevel.fill( 0 );
for( auto const &tile : m_inrange ) {
auto const resident { m_tiles.find( tile.key ) };
if( resident == m_tiles.end() ) {
++m_stats.wanted;
continue;
}
// still wanted, at whatever level it has until the right one arrives
resident->second.lastused = m_frame;
++m_stats.inview;
++m_stats.perlevel[ std::min<std::size_t>( resident->second.level, m_stats.perlevel.size() - 1 ) ];
if( resident->second.level != tile.level ) { ++m_stats.wanted; }
}
m_arrived.clear();
m_loader.collect( m_arrived, m_uploadsperframe );
m_loader.collect( m_arrived, m_putsperframe );
for( auto const &tile : m_arrived ) {
if( false == tile.valid ) { continue; }
// a tile read for where the camera was is put up only if nothing of it is on the gpu
// yet - some level is better than a hole. otherwise it is dropped, and the level
// a tile read for where the camera was is still put in: it covers ground, and what is
// wanted now is already queued
auto const tilekey { key( tile.tile.x, tile.tile.z ) };
auto const wanted { m_wantedlevel.find( tilekey ) };
auto const stillwanted { ( wanted != m_wantedlevel.end() ) && ( wanted->second == tile.tile.level ) };
if( stillwanted || ( false == m_tiles.contains( tilekey ) ) ) {
upload( tile );
// put up this frame, so drawn this frame
m_tiles.find( tilekey )->second.lastused = m_frame;
}
else {
++m_stats.dropped;
}
put_in( tile );
}
if( ( m_tiles.size() > m_budget ) && ( m_tiles.size() > m_stats.inview ) ) {
// over budget, and not everything is in view: drop whatever has gone longest without
// being asked for
std::vector<std::pair<std::uint64_t, std::int64_t>> aged;
for( auto const & [ tilekey, tile ] : m_tiles ) {
if( tile.lastused != m_frame ) { aged.emplace_back( tile.lastused, tilekey ); }
}
auto const excess { std::min( aged.size(), m_tiles.size() - m_budget ) };
std::partial_sort( aged.begin(), aged.begin() + excess, aged.end() );
for( std::size_t index = 0; index < excess; ++index ) {
auto const victim { m_tiles.find( aged[ index ].second ) };
retire( victim->second );
m_tiles.erase( victim );
}
auto const moved {
glm::length( glm::dvec2 { Viewpoint.x - m_walkpoint.x, Viewpoint.z - m_walkpoint.z } ) };
auto const detailchanged { std::abs( m_detail - m_walkdetail ) > 0.01 * m_walkdetail };
if( ( false == m_walked )
|| ( m_range != m_walkrange )
|| ( true == detailchanged )
|| ( moved > m_finesttile * terrain_rescan_tiles )
|| ( false == m_arrived.empty() ) ) {
// a tile that has just arrived lets the walk go deeper where it was drawing a coarse one
walk( Viewpoint );
}
// what the walk settled on is wanted now, and is what eviction spares
m_stats.perlevel.fill( 0 );
for( auto const key : m_chosen ) {
auto const found { m_tiles.find( key ) };
if( found == m_tiles.end() ) { continue; }
found->second.lastused = m_frame;
++m_stats.perlevel[ std::min<std::size_t>( found->second.level, m_stats.perlevel.size() - 1 ) ];
}
retire_unused();
m_stats.resident = m_tiles.size();
m_stats.texturebytes = 0;
for( auto const &pool : m_pools ) { m_stats.texturebytes += pool->bytes(); }
m_stats.gpubytes = m_arena.bytes();
report_residency();
}
// residency settles within a second of the camera stopping and then barely moves, so a
// line is written when it shifts by a noticeable amount rather than every frame. that
// makes the warm figure - what the terrain actually costs - readable from the log
// residency settles within a second of the camera stopping and then barely moves, so a line is
// written when it shifts by a noticeable amount rather than every frame. that makes the warm figure
// - what the terrain actually costs - readable from the log
void
terrain_clipmap::report_residency() {
auto const difference {
m_stats.resident > m_reportedresidency ?
m_stats.resident - m_reportedresidency :
m_reportedresidency - m_stats.resident };
if( ( m_reported == true ) && ( difference < 16 ) ) { return; }
auto const apart {
[]( std::size_t const Left, std::size_t const Right ) {
return Left > Right ? Left - Right : Right - Left; } };
auto const moved {
( apart( m_stats.resident, m_reportedresidency ) >= 8 )
|| ( apart( m_stats.drawn, m_reporteddrawn ) >= 4 ) };
// at most once every couple of seconds, and only when the picture has actually changed: what
// this is for is the warm figure, what the terrain settles at, not a line per frame
if( ( m_reported == true ) && ( ( false == moved ) || ( m_frame < m_reportedframe + 120 ) ) ) { return; }
m_reported = true;
m_reportedresidency = m_stats.resident;
m_reporteddrawn = m_stats.drawn;
m_reportedframe = m_frame;
WriteLog(
"Terrain: " + std::to_string( m_stats.resident ) + " tiles resident, "
+ std::to_string( m_stats.texturebytes / 1048576 ) + " MB of samples, "
+ std::to_string( m_stats.uploaded ) + " uploads so far, "
+ std::to_string( m_stats.drawn ) + " of " + std::to_string( m_stats.inview ) + " in range drawn, finest level out to "
+ std::to_string( static_cast<int>( m_finest ) ) + " m" );
+ std::to_string( m_stats.gpubytes / 1048576 ) + " MB of mesh, "
+ std::to_string( m_stats.uploaded ) + " put in so far, "
+ std::to_string( m_stats.drawn ) + " of " + std::to_string( m_stats.chosen ) + " chosen drawn, "
+ std::to_string( m_stats.triangles / 1000 ) + "k triangles, levels "
+ [ this ]() {
std::string counts;
for( std::uint32_t level = 0; level < m_levels; ++level ) {
counts += ( level > 0 ? "/" : "" ) + std::to_string( m_stats.perlevel[ level ] );
}
return counts; }() );
}
void
terrain_clipmap::render( glm::dvec3 const &Viewpoint, visibility const &Visible ) {
if( ( false == m_ready ) || ( true == m_tiles.empty() ) ) { return; }
if( ( false == m_ready ) || ( true == m_chosen.empty() ) ) { return; }
// textures that finished loading since the last frame go into the ground array first
// the ground textures the engine has finished loading since the last frame
m_ground.update();
// what each level draws: the tiles touched by the last update, and seen. a tile's bounding
// sphere holds the whole tile, morphing included, since morphing only moves vertices
// towards samples of the same tile
auto const tilesize { static_cast<double>( m_reader.tilesize() ) };
for( auto &instances : m_instances ) { instances.clear(); }
for( auto const & [ tilekey, tile ] : m_tiles ) {
if( tile.lastused != m_frame ) { continue; }
auto const halfheight { 0.5 * ( tile.maxheight - tile.minheight ) };
glm::dvec3 const centre {
( tile.x + 0.5 ) * tilesize, 0.5 * ( tile.minheight + tile.maxheight ), ( tile.z + 0.5 ) * tilesize };
auto const radius {
static_cast<float>( std::sqrt( 0.5 * tilesize * tilesize + halfheight * halfheight ) ) };
if( false == Visible( centre, radius ) ) { continue; }
// relative to the viewpoint, so that the shader never sees a coordinate large enough
// for single precision to matter
m_instances[ tile.level ].insert(
m_instances[ tile.level ].end(),
{ static_cast<float>( tile.x * tilesize - Viewpoint.x ),
static_cast<float>( -Viewpoint.y ),
static_cast<float>( tile.z * tilesize - Viewpoint.z ),
static_cast<float>( tile.slot ),
static_cast<float>( tile.x * tilesize ),
static_cast<float>( tile.z * tilesize ),
0.f, 0.f } );
m_counts.clear();
m_offsets.clear();
m_bases.clear();
std::size_t triangles { 0 };
for( auto const key : m_chosen ) {
auto const found { m_tiles.find( key ) };
if( found == m_tiles.end() ) { continue; }
auto const &tile { found->second };
if( false == Visible( tile.centre, tile.radius ) ) { continue; }
m_counts.push_back( static_cast<std::int32_t>( tile.piece.indices ) );
m_offsets.push_back(
reinterpret_cast<void const *>(
static_cast<std::uintptr_t>( tile.piece.firstindex ) * sizeof( std::uint32_t ) ) );
m_bases.push_back( static_cast<std::int32_t>( tile.piece.firstvertex ) );
triangles += tile.piece.indices / 3;
}
m_stats.drawn = m_counts.size();
m_stats.triangles = triangles;
if( true == m_counts.empty() ) { return; }
m_shader->bind();
m_vao->bind();
// the depth buffer is reversed - nearer is greater - so pushing back is a negative offset
::glEnable( GL_POLYGON_OFFSET_FILL );
::glPolygonOffset( -1.f - 2.f * static_cast<float>( m_depthrank ), -2.f - 4.f * static_cast<float>( m_depthrank ) );
::glUniform1f( m_uniforms.heightbias, m_reader.height( 0 ) );
::glUniform1f( m_uniforms.heightscale, m_reader.height( 1 ) - m_reader.height( 0 ) );
::glUniform1ui( m_uniforms.nodata, heightfield::nodata );
::glUniform1i( m_uniforms.heights, 0 );
glm::vec3 const origin {
static_cast<float>( m_origin.x - Viewpoint.x ),
static_cast<float>( m_origin.y - Viewpoint.y ),
static_cast<float>( m_origin.z - Viewpoint.z ) };
::glUniform3fv( m_uniforms.origin, 1, &origin.x );
glm::vec2 const world { static_cast<float>( m_origin.x ), static_cast<float>( m_origin.z ) };
::glUniform2fv( m_uniforms.world, 1, &world.x );
::glUniform1i( m_uniforms.ground, 0 );
::glUniform1i( m_uniforms.materials, 1 );
::glUniform1i( m_uniforms.ground, 2 );
::glUniform1i( m_uniforms.materialtable, 3 );
::glUniform1i( m_uniforms.instances, 4 );
::glActiveTexture( GL_TEXTURE2 );
::glActiveTexture( GL_TEXTURE0 );
::glBindTexture( GL_TEXTURE_2D_ARRAY, m_ground.textures() );
::glActiveTexture( GL_TEXTURE3 );
::glActiveTexture( GL_TEXTURE1 );
::glBindTexture( GL_TEXTURE_2D, m_ground.table() );
std::size_t drawn { 0 };
auto const gridstep { m_reader.gridstep() };
for( std::uint32_t level = 0; level < m_instances.size(); ++level ) {
// the ground loses the depth test to whatever lies on it, and a coarser terrain to a finer one
// describing the same ground. the offset is negative because depth runs the other way here
::glEnable( GL_POLYGON_OFFSET_FILL );
::glPolygonOffset(
-1.f - 2.f * static_cast<float>( m_depthrank ),
-2.f - 4.f * static_cast<float>( m_depthrank ) );
auto const &instances { m_instances[ level ] };
if( true == instances.empty() ) { continue; }
auto const count { instances.size() / 8 };
drawn += count;
m_instancebuffer->allocate(
gl::buffer::TEXTURE_BUFFER, static_cast<GLsizeiptr>( instances.size() * sizeof( float ) ), GL_STREAM_DRAW );
m_instancebuffer->upload(
gl::buffer::TEXTURE_BUFFER, instances.data(), 0, static_cast<GLsizeiptr>( instances.size() * sizeof( float ) ) );
::glActiveTexture( GL_TEXTURE4 );
::glBindTexture( GL_TEXTURE_BUFFER, m_instancetexture );
::glTexBuffer( GL_TEXTURE_BUFFER, GL_RGBA32F, static_cast<GLuint>( *m_instancebuffer ) );
auto const &pool { *m_pools[ level ] };
::glActiveTexture( GL_TEXTURE0 );
::glBindTexture( GL_TEXTURE_2D_ARRAY, pool.heights() );
::glActiveTexture( GL_TEXTURE1 );
::glBindTexture( GL_TEXTURE_2D_ARRAY, pool.materials() );
::glUniform1f( m_uniforms.samplestep, gridstep * static_cast<float>( 1u << level ) );
::glUniform1i( m_uniforms.side, static_cast<GLint>( pool.side() ) );
// the band over which this level turns into the next. the coarsest level has no next
// one, so its band lies beyond anything that is ever drawn
if( level + 1 < m_reader.levels() ) {
::glUniform2f(
m_uniforms.morph,
static_cast<float>( terrain_morph_begin( level, m_finest, tilesize ) ),
static_cast<float>( terrain_morph_end( level, m_finest, tilesize ) ) );
}
else {
::glUniform2f( m_uniforms.morph, 1e30f, 2e30f );
}
m_indices[ level ]->bind( gl::buffer::ELEMENT_ARRAY_BUFFER );
::glDrawElementsInstanced(
GL_TRIANGLES, static_cast<GLsizei>( m_indexcounts[ level ] ), GL_UNSIGNED_INT, nullptr,
static_cast<GLsizei>( count ) );
}
m_stats.drawn = drawn;
::glBindVertexArray( m_arena.vertexarray() );
::glMultiDrawElementsBaseVertex(
GL_TRIANGLES, m_counts.data(), GL_UNSIGNED_INT,
const_cast<void const **>( m_offsets.data() ),
static_cast<GLsizei>( m_counts.size() ), m_bases.data() );
::glBindVertexArray( 0 );
::glDisable( GL_POLYGON_OFFSET_FILL );
gl::buffer::unbind( gl::buffer::TEXTURE_BUFFER );
::glActiveTexture( GL_TEXTURE1 );
::glBindTexture( GL_TEXTURE_2D, 0 );
::glActiveTexture( GL_TEXTURE0 );
::glBindTexture( GL_TEXTURE_2D_ARRAY, 0 );
opengl_texture::reset_unit_cache();
m_vao->unbind();
gl::program::bind( 0 );
}