mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-20 04:35:55 +02:00
Stream the scenery: a baked terrain heightfield, a logic container, and prefabs
Today's work on the binary scene format plan (binary-scene-format-plan.md), from the first measurement to a terrain that bakes itself on first run and streams from a thread. The step-by-step history is kept on backup/scene-streaming-format-presquash-0916. Measuring first. -loadprofile times a scenery load by stage and by node kind and reports node counts and resident memory. On l204 terrain is 95% of the bytes and 99.8% of the nodes; on quarkmce2007 vehicles, not models, dominate what is left. The terrain turned out to be a heightfield in all but name, a ribbon covering 12% of its bounding box. The heightfield. scene/heightfieldformat.h is the .ehf layout: Morton-ordered tiles of 128 samples with one shared edge sample, a point-sampled mip chain, zstd per tile, a nodata sentinel, a material palette with a measured tiling per material, and a stamp of the rules that decided what counted as terrain. scene/heightfieldreader.h reads it for the tool, the renderer and the loader alike, and scene/terraincooker.h writes it, shared by the terraincook tool and the engine so a first-run bake is the file the tool would have made. The cooker rasterises every triangle where it is drawn (through scene::place_in_world, the placement the region applies too), keeps an upper surface laid within half a metre over another and the lower of two further apart, and then asks each triangle whether the heightfield shows it: laid, reaching a sample, keeping nearly all it claimed, and with the ground around it drawn at every level out to 2 km. Baking on first run. simulation/terrainbake bakes <scenario>.ehf the first time a scenery is opened, choosing the grid step from the first 200,000 vertices, and on later loads leaves out the triangles its node table says the heightfield shows. Everything else - trees, grass tufts, gantries, bridge decks - is drawn as written, so a bad bake can at worst draw something twice, and the heightfield sits 2 cm under the triangles so the original wins there. Nodes are keyed by file, line, include parameters and placement; an edited file changes its keys and those nodes are drawn again. A bake under older rules, or without its table, is thrown away and redone. A launcher copy named $scenario shares the scenario's files. -nobaketerrain shows the triangles as written. A scenery can still name hand-cooked fields, several at different steps. Drawing and streaming. rendering/terrainclipmap draws resident tiles at the mip level their distance earns, deriving the grid from gl_VertexID and texturing in world space with the scenery's own materials. rendering/terraintileloader reads and decodes tiles on a thread of its own; the renderer rescans what is in range only after moving a quarter of a tile, asks for what is missing nearest first, uploads at most 32 tiles a frame, drops arrivals the camera has moved past, and keeps drawing a tile at the level it has until the right one arrives. The terrain has its own range, terrainrange in the ini and a slider in the debug panel, 20 km by default and independent of basedrawrange; the far plane of an ordinary view is pushed out to it. The debug panel shows the range, tiles in view, on the gpu, wanted and queued, and how many sit at each level. Events. The event queue moved out of the events into a binary heap with a deterministic tie-break, and an event is handed its activator instead of storing it, so basic_event is a definition and nothing else. The logic container. scene/logicformat.h and scene/logiccontainer write a scenery's events, their targets and sibling chains as flat, deterministic arrays with a schema hash that refuses a mismatched build. simulation/logiccook cooks it from the loaded state whenever it is missing, stale or unreadable, reads it straight back to check it, and resolves each target to a position in the registry its event kind searches. -cooklogic forces a cook, -verifylogic only checks. Nothing reads it at load yet. Prefabs. scene/prefab describes a signal by the aspects it can show - lights, speeds, animation channels, a sound - instead of pasting an .inc per variant, and expands an instance to ordinary scenery text. Tried on a light signal and a shape signal. Screenshots without a person present: -shot x,y,z[,yaw[,pitch]], -shotsettle, -shotstay.
This commit is contained in:
@@ -17,6 +17,7 @@ module;
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include "global_include/interfaces/ITexture_macros.h"
|
||||
#include "utilities/Globals_macros.h"
|
||||
@@ -29,6 +30,7 @@ import eu07.utilities.classes;
|
||||
import eu07.glm;
|
||||
import eu07.utilities.globals;
|
||||
import eu07.simulation.simulation;
|
||||
import eu07.simulation.loadprofile;
|
||||
import eu07.simulation.simulationtime;
|
||||
import eu07.simulation.simulationsounds;
|
||||
import eu07.simulation.simulationenvironment;
|
||||
@@ -80,6 +82,7 @@ state_serializer::deserialize_begin( std::string const &Scenariofile ) {
|
||||
// TODO: check first for presence of serialized binary files
|
||||
// if this fails, fall back on the legacy text format
|
||||
state->scratchpad.name = Scenariofile;
|
||||
loadprofile::reset();
|
||||
if( true == Global.file_binary_terrain
|
||||
&& Scenariofile != "$.scn" ) {
|
||||
// compilation to binary file isn't supported for rainsted-created overrides
|
||||
@@ -99,6 +102,11 @@ state_serializer::deserialize_begin( std::string const &Scenariofile ) {
|
||||
}
|
||||
scene::Groups.create();
|
||||
|
||||
// before a token is read: pick up a heightfield baked on an earlier run, or arm the
|
||||
// bake when there is none. it has to happen here because the decision to skip terrain
|
||||
// triangles is taken per node while parsing
|
||||
terrainbake::begin( Scenariofile );
|
||||
|
||||
if( false == state->input.ok() ) {
|
||||
if( false == Global.editor_startup ) {
|
||||
throw invalid_scenery_exception();
|
||||
@@ -131,6 +139,7 @@ state_serializer::deserialize_begin( std::string const &Scenariofile ) {
|
||||
{ "endgroup", &state_serializer::deserialize_endgroup },
|
||||
{ "light", &state_serializer::deserialize_light },
|
||||
{ "node", &state_serializer::deserialize_node },
|
||||
{ "prefab", &state_serializer::deserialize_prefab },
|
||||
{ "origin", &state_serializer::deserialize_origin },
|
||||
{ "endorigin", &state_serializer::deserialize_endorigin },
|
||||
{ "scale", &state_serializer::deserialize_scale },
|
||||
@@ -177,11 +186,14 @@ state_serializer::deserialize_continue(std::shared_ptr<deserializer_state> state
|
||||
auto timenow = std::chrono::steady_clock::now();
|
||||
if( std::chrono::duration_cast<std::chrono::milliseconds>( timenow - timelast ).count() >= 200 ) {
|
||||
Application.set_progress( Input.getProgress(), Input.getFullProgress() );
|
||||
loadprofile::add_total( std::chrono::duration<double>( timenow - timelast ).count() );
|
||||
return true;
|
||||
}
|
||||
|
||||
token = Input.getToken<std::string>();
|
||||
}
|
||||
loadprofile::add_total(
|
||||
std::chrono::duration<double>( std::chrono::steady_clock::now() - timelast ).count() );
|
||||
|
||||
if( false == Scratchpad.initialized ) {
|
||||
// manually perform scenario initialization
|
||||
@@ -202,6 +214,22 @@ state_serializer::deserialize_continue(std::shared_ptr<deserializer_state> state
|
||||
Region->serialize( state->scenariofile );
|
||||
}
|
||||
|
||||
terrainbake::finish( state->scenariofile );
|
||||
|
||||
loadprofile::report( state->scenariofile );
|
||||
|
||||
if( true == Global.verify_logic ) {
|
||||
verify_logic( state->scenariofile );
|
||||
}
|
||||
else if( ( true == Global.cook_logic )
|
||||
|| ( true == logic_stale( state->scenariofile ) ) ) {
|
||||
// cooked from the loaded state rather than from the text, and immediately read back,
|
||||
// so a container that disagrees with what it was cooked from says so on the spot
|
||||
if( true == cook_logic( state->scenariofile ) ) {
|
||||
verify_logic( state->scenariofile );
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -457,6 +485,40 @@ state_serializer::deserialize_light( cParser &Input, scene::scratch_data &Scratc
|
||||
skip_until( Input, "endlight" );
|
||||
}
|
||||
|
||||
void
|
||||
state_serializer::deserialize_prefab( cParser &Input, scene::scratch_data &Scratchpad ) {
|
||||
|
||||
// prefab <definition> <name> <x> <y> <z> <yaw> [<texture>] endprefab
|
||||
scene::prefab_placement placement;
|
||||
Input.getTokens( 1, false );
|
||||
std::string definition; Input >> definition;
|
||||
Input.getTokens( 1, false );
|
||||
Input >> placement.name;
|
||||
Input.getTokens( 4 );
|
||||
Input
|
||||
>> placement.x
|
||||
>> placement.y
|
||||
>> placement.z
|
||||
>> placement.yaw;
|
||||
|
||||
auto token { Input.getToken<std::string>( false ) };
|
||||
if( ( false == token.empty() ) && ( token != "endprefab" ) ) {
|
||||
placement.texture = token;
|
||||
token = Input.getToken<std::string>( false );
|
||||
}
|
||||
if( token != "endprefab" ) {
|
||||
ErrorLog( "Bad scenario: prefab \"" + placement.name + "\" is missing its endprefab, found \"" + token + "\" instead" );
|
||||
}
|
||||
|
||||
auto const scenery { Prefabs.expand( definition, placement ) };
|
||||
if( true == scenery.empty() ) {
|
||||
ErrorLog( "Bad scenario: prefab \"" + placement.name + "\" of unknown type \"" + definition + "\" was skipped" );
|
||||
return;
|
||||
}
|
||||
// the expansion is ordinary scenery text, and goes in as an include would
|
||||
Input.injectString( scenery );
|
||||
}
|
||||
|
||||
void
|
||||
state_serializer::deserialize_node( cParser &Input, scene::scratch_data &Scratchpad ) {
|
||||
|
||||
@@ -471,6 +533,19 @@ state_serializer::deserialize_node( cParser &Input, scene::scratch_data &Scratch
|
||||
>> nodedata.name
|
||||
>> nodedata.type;
|
||||
if( nodedata.name == "none" ) { nodedata.name.clear(); }
|
||||
// the triangle family is timed in its own branch below; everything else is timed
|
||||
// against the kind of node it is, since "not a triangle" covers models, tracks,
|
||||
// traction and vehicles alike and they cost very different amounts
|
||||
std::optional<loadprofile::scoped_timer<loadprofile::stage>> profiledstage;
|
||||
std::optional<loadprofile::scoped_timer<loadprofile::kind>> profiledkindtime;
|
||||
if( true == loadprofile::enabled() ) {
|
||||
auto const profiledkind { loadprofile::kind_of( nodedata.type ) };
|
||||
loadprofile::add_node( profiledkind );
|
||||
if( profiledkind != loadprofile::kind::triangles ) {
|
||||
profiledstage.emplace( loadprofile::stage::other_nodes );
|
||||
profiledkindtime.emplace( profiledkind );
|
||||
}
|
||||
}
|
||||
// type-based deserialization. not elegant but it'll do
|
||||
if( nodedata.type == "dynamic" ) {
|
||||
|
||||
@@ -604,25 +679,71 @@ state_serializer::deserialize_node( cParser &Input, scene::scratch_data &Scratch
|
||||
|| nodedata.type == "triangle_strip"
|
||||
|| nodedata.type == "triangle_fan" ) {
|
||||
|
||||
auto const file { Input.Name() };
|
||||
auto const switchtrackbedfile {
|
||||
// crude way to detect fixed switch trackbed geometry
|
||||
true == Global.CreateSwitchTrackbeds
|
||||
&& file.size() >= 15
|
||||
&& file.starts_with( "scenery/zwr" )
|
||||
&& file.ends_with( ".inc" ) };
|
||||
|
||||
auto const offset {
|
||||
Scratchpad.location.offset.empty() ? glm::dvec3( 0.0 ) : Scratchpad.location.offset.top() };
|
||||
auto const worldspace {
|
||||
( offset == glm::dvec3( 0.0 ) ) && ( Scratchpad.location.rotation == glm::vec3( 0.0f ) ) };
|
||||
|
||||
// whether a heightfield stands in for this node, and whether a bake wants it
|
||||
auto const decision {
|
||||
( ( true == Scratchpad.binary.terrain ) || ( true == switchtrackbedfile ) )
|
||||
? terrainbake::node_decision{}
|
||||
: terrainbake::examine(
|
||||
file, Input.Line(), Input.Parameters(), nodedata.type,
|
||||
offset, Scratchpad.location.rotation, worldspace ) };
|
||||
|
||||
auto const skip {
|
||||
// all shapes will be loaded from the binary version of the file
|
||||
true == Scratchpad.binary.terrain
|
||||
// crude way to detect fixed switch trackbed geometry
|
||||
|| ( true == Global.CreateSwitchTrackbeds
|
||||
&& Input.Name().size() >= 15
|
||||
&& Input.Name().starts_with("scenery/zwr")
|
||||
&& Input.Name().ends_with(".inc") ) };
|
||||
( true == Scratchpad.binary.terrain )
|
||||
// old trackbed geometry the engine replaces
|
||||
|| ( true == switchtrackbedfile )
|
||||
|| ( true == decision.skip ) };
|
||||
|
||||
if( false == skip ) {
|
||||
|
||||
simulation::Region->insert(
|
||||
scene::shape_node().import(
|
||||
Input, nodedata ),
|
||||
Scratchpad,
|
||||
true );
|
||||
if( true == skip ) {
|
||||
skip_until( Input, "endtri" );
|
||||
}
|
||||
else {
|
||||
skip_until( Input, "endtri" );
|
||||
auto shape { [&]() {
|
||||
loadprofile::scoped_timer const importtime { loadprofile::stage::shape_import };
|
||||
return scene::shape_node().import( Input, nodedata ); }() };
|
||||
loadprofile::add_vertices( shape.data().vertices.size() );
|
||||
|
||||
if( true == decision.bake ) {
|
||||
// the bake sees the node exactly where it is drawn. placing is done on a copy:
|
||||
// the region applies the same placement to the shape itself when inserting it
|
||||
auto const &data { shape.data() };
|
||||
auto const material {
|
||||
data.material != null_handle
|
||||
? GfxRenderer->Material( data.material )->GetName()
|
||||
: "none" };
|
||||
// the old switch trackbeds are dropped from the scenery when generated ones
|
||||
// replace them, so they are no part of the ground either
|
||||
if( false == ( Global.CreateSwitchTrackbeds && scene::is_switch_trackbed( material ) ) ) {
|
||||
auto placed { data.vertices };
|
||||
scene::place_in_world( placed, Scratchpad );
|
||||
terrainbake::add( decision.key, placed, material );
|
||||
}
|
||||
}
|
||||
if( decision.plan.what == terrainbake::verdict::filter ) {
|
||||
auto const &drawn { decision.plan.drawn };
|
||||
shape.keep_triangles(
|
||||
[ &drawn ]( std::size_t const Triangle ) {
|
||||
return ( Triangle >= drawn.size() ) || drawn[ Triangle ]; } );
|
||||
}
|
||||
|
||||
loadprofile::scoped_timer const inserttime { loadprofile::stage::shape_insert };
|
||||
simulation::Region->insert(
|
||||
std::move( shape ),
|
||||
Scratchpad,
|
||||
true );
|
||||
}
|
||||
}
|
||||
else if( nodedata.type == "lines"
|
||||
@@ -827,7 +948,14 @@ state_serializer::deserialize_terrain(cParser &Input, scene::scratch_data &Scrat
|
||||
std::string line;
|
||||
Input.getTokens(1);
|
||||
Input >> line;
|
||||
if (Global.file_binary_terrain && line.ends_with(".sbt"))
|
||||
if (line.ends_with(".ehf"))
|
||||
{
|
||||
// a cooked heightfield. the renderer opens it and the scenery carries no terrain
|
||||
// geometry at all, which is the point: the text it replaces is most of the file
|
||||
Global.terrain_heightfields.push_back(line);
|
||||
WriteLog("Terrain heightfield: " + line);
|
||||
}
|
||||
else if (Global.file_binary_terrain && line.ends_with(".sbt"))
|
||||
{
|
||||
Scratchpad.binary.terrain = Region->is_scene(line);
|
||||
Global.file_binary_terrain_state = true;
|
||||
|
||||
Reference in New Issue
Block a user