mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 09:49:19 +02:00
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.
265 lines
11 KiB
C++
265 lines
11 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 <cstdint>
|
|
#include <string>
|
|
#include <algorithm>
|
|
#include <set>
|
|
#include <utility>
|
|
#include <vector>
|
|
#include "utilities/Globals_macros.h"
|
|
#include "global_include/interfaces/ITexture_macros.h"
|
|
#include "scene/logicformat.h"
|
|
|
|
module eu07.simulation.logiccook;
|
|
import eu07.scene.logiccontainer;
|
|
import eu07.simcore;
|
|
import eu07.simulation.simulation;
|
|
import eu07.simulation.simulationsounds;
|
|
import eu07.utilities.logs;
|
|
import eu07.utilities.globals;
|
|
import eu07.utilities.utilities;
|
|
|
|
namespace simulation {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// the flat record an event becomes. everything here is read off the event itself, so the
|
|
// container cannot drift from what the text path built
|
|
scene::logic::event_record
|
|
record_of( basic_event const &Event, scene::logic::event_kind const Kind, std::uint32_t const Sibling, std::uint32_t const Name ) {
|
|
|
|
scene::logic::event_record record {};
|
|
record.kind = static_cast<std::uint16_t>( Kind );
|
|
record.name = Name;
|
|
record.sibling = ( Sibling < simulation::Events.event_count() ? Sibling : scene::logic::no_index );
|
|
record.group = ( Event.group() == null_handle ? scene::logic::no_index : static_cast<std::uint32_t>( Event.group() ) );
|
|
record.delay = Event.m_delay;
|
|
record.delayrandom = Event.m_delayrandom;
|
|
record.delaydeparture = Event.m_delaydeparture;
|
|
record.ignored = ( Event.m_ignored ? 1 : 0 );
|
|
record.passive = ( Event.m_passive ? 1 : 0 );
|
|
return record;
|
|
}
|
|
|
|
|
|
// where an event of this kind looks for its targets, and where it looks next. this mirrors
|
|
// each subclass's init() rather than guessing: an event that resolves against the memory
|
|
// cells will never find its target among the tracks, and saying otherwise here would bind
|
|
// it to whatever happened to share the name
|
|
std::pair<scene::logic::node_table, scene::logic::node_table>
|
|
tables_for( scene::logic::event_kind const Kind ) {
|
|
|
|
using kind = scene::logic::event_kind;
|
|
using table = scene::logic::node_table;
|
|
|
|
switch( Kind ) {
|
|
case kind::updatevalues:
|
|
case kind::addvalues:
|
|
case kind::copyvalues:
|
|
case kind::getvalues:
|
|
case kind::putvalues:
|
|
case kind::whois:
|
|
case kind::logvalues:
|
|
case kind::multiple: return { table::memory, table::none };
|
|
case kind::texture:
|
|
case kind::animation:
|
|
case kind::lights: return { table::instance, table::none };
|
|
case kind::switchevent:
|
|
case kind::trackvel: return { table::path, table::none };
|
|
case kind::voltage: return { table::powergrid, table::none };
|
|
case kind::sound: return { table::sound, table::none };
|
|
// a visible event takes model instances and tracks alike, and looks in that order
|
|
case kind::visible: return { table::instance, table::path };
|
|
default: return { table::none, table::none };
|
|
}
|
|
}
|
|
|
|
std::uint32_t
|
|
index_in( scene::logic::node_table const Table, std::string const &Name ) {
|
|
|
|
using table = scene::logic::node_table;
|
|
switch( Table ) {
|
|
case table::memory: return simulation::Memory.find_id( Name );
|
|
case table::instance: return simulation::Instances.find_id( Name );
|
|
case table::path: return simulation::Paths.find_id( Name );
|
|
case table::sound: return simulation::Sounds.find_id( Name );
|
|
case table::powergrid: return simulation::Powergrid.find_id( Name );
|
|
default: return static_cast<std::uint32_t>( -1 );
|
|
}
|
|
}
|
|
|
|
// the entries an event's targets become, each one carrying the registry it was found in
|
|
std::vector<scene::logic::target_entry>
|
|
targets_of( basic_event const &Event, scene::logic::event_kind const Kind, scene::logic::container_writer &Writer ) {
|
|
|
|
auto const kind { Kind };
|
|
auto const [ first, second ] { tables_for( kind ) };
|
|
|
|
std::vector<scene::logic::target_entry> entries;
|
|
for( auto const &name : Event.target_names() ) {
|
|
|
|
scene::logic::target_entry entry {};
|
|
entry.name = Writer.intern( name );
|
|
entry.table = static_cast<std::uint16_t>( scene::logic::node_table::none );
|
|
entry.index = scene::logic::no_index;
|
|
|
|
for( auto const table : { first, second } ) {
|
|
if( table == scene::logic::node_table::none ) { continue; }
|
|
auto const index { index_in( table, name ) };
|
|
if( index != static_cast<std::uint32_t>( -1 ) ) {
|
|
entry.table = static_cast<std::uint16_t>( table );
|
|
entry.index = index;
|
|
break;
|
|
}
|
|
}
|
|
entries.emplace_back( entry );
|
|
}
|
|
return entries;
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
bool
|
|
cook_logic( std::string const &Sceneryfile ) {
|
|
|
|
scene::logic::container_writer writer;
|
|
auto const count { simulation::Events.event_count() };
|
|
std::uint32_t unknown { 0 };
|
|
std::set<std::string> unknownkinds;
|
|
|
|
for( std::uint32_t index = 0; index < count; ++index ) {
|
|
auto const *cooked { simulation::Events.event_at( index ) };
|
|
if( cooked == nullptr ) { continue; }
|
|
auto const name { writer.intern( cooked->name() ) };
|
|
auto const kind { scene::logic::kind_of( cooked->type_name() ) };
|
|
auto const record { record_of( *cooked, kind, simulation::Events.sibling_of( index ), name ) };
|
|
if( record.kind == static_cast<std::uint16_t>( scene::logic::event_kind::unknown ) ) {
|
|
++unknown;
|
|
if( unknownkinds.insert( cooked->type_name() ).second ) {
|
|
ErrorLog( "Logic container: no cooked kind for event type \"" + cooked->type_name() + "\" (first seen on \"" + cooked->name() + "\")" );
|
|
}
|
|
}
|
|
writer.add_event( record, targets_of( *cooked, kind, writer ) );
|
|
}
|
|
|
|
if( unknown > 0 ) {
|
|
// a kind the container has no value for would come back as something else entirely,
|
|
// so say so loudly rather than write a file that reads back wrong
|
|
ErrorLog( "Logic container: " + std::to_string( unknown ) + " events of a kind this build cannot cook" );
|
|
}
|
|
|
|
return writer.write( scene::logic::container_path( Sceneryfile ) );
|
|
}
|
|
|
|
bool
|
|
logic_stale( std::string const &Sceneryfile ) {
|
|
|
|
auto const path { scene::logic::container_path( Sceneryfile ) };
|
|
|
|
if( false == FileExists( path ) ) {
|
|
return true;
|
|
}
|
|
if( last_modified( scenery_file( Sceneryfile ) ) > last_modified( path ) ) {
|
|
WriteLog( "Logic container: \"" + path + "\" is older than the scenery, recooking" );
|
|
return true;
|
|
}
|
|
// the header says whether this build can read it at all, which covers a container
|
|
// cooked before a schema change
|
|
if( false == scene::logic::container_reader::readable( path ) ) {
|
|
WriteLog( "Logic container: \"" + path + "\" cannot be read by this build, recooking" );
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool
|
|
verify_logic( std::string const &Sceneryfile ) {
|
|
|
|
scene::logic::container_reader reader;
|
|
auto const path { scene::logic::container_path( Sceneryfile ) };
|
|
if( false == reader.open( path ) ) {
|
|
ErrorLog( "Logic container: cannot verify \"" + path + "\"" );
|
|
return false;
|
|
}
|
|
|
|
std::uint32_t mismatches { 0 };
|
|
auto const count { simulation::Events.event_count() };
|
|
if( reader.events() != count ) {
|
|
ErrorLog(
|
|
"Logic container: holds " + std::to_string( reader.events() )
|
|
+ " events, the scenery has " + std::to_string( count ) );
|
|
++mismatches;
|
|
}
|
|
|
|
auto const shared { std::min<std::size_t>( reader.events(), count ) };
|
|
for( std::size_t index = 0; index < shared; ++index ) {
|
|
auto const *loaded { simulation::Events.event_at( static_cast<std::uint32_t>( index ) ) };
|
|
if( loaded == nullptr ) { continue; }
|
|
auto const &record { reader.event( index ) };
|
|
|
|
auto const complain {
|
|
[ &mismatches, loaded ]( std::string const &What ) {
|
|
ErrorLog( "Logic container: event \"" + loaded->name() + "\" " + What );
|
|
++mismatches;
|
|
} };
|
|
|
|
if( reader.string( record.name ) != loaded->name() ) {
|
|
complain( "is called \"" + std::string( reader.string( record.name ) ) + "\" in the container" );
|
|
continue;
|
|
}
|
|
if( record.kind != static_cast<std::uint16_t>( scene::logic::kind_of( loaded->type_name() ) ) ) {
|
|
complain( "was cooked as a different kind" );
|
|
}
|
|
// delays are written as they are held, so they compare exactly. a difference here
|
|
// means the record lost precision, which is the whole thing worth catching
|
|
if( record.delay != loaded->m_delay ) {
|
|
complain( "has a different delay in the container" );
|
|
}
|
|
if( record.delayrandom != loaded->m_delayrandom ) {
|
|
complain( "has a different random delay in the container" );
|
|
}
|
|
|
|
auto const targets { loaded->target_names() };
|
|
if( record.targetcount != targets.size() ) {
|
|
complain(
|
|
"has " + std::to_string( record.targetcount ) + " targets in the container, "
|
|
+ std::to_string( targets.size() ) + " in the scenery" );
|
|
continue;
|
|
}
|
|
for( std::uint32_t target = 0; target < record.targetcount; ++target ) {
|
|
auto const &entry { reader.target( record.targetfirst + target ) };
|
|
if( reader.string( entry.name ) != targets[ target ] ) {
|
|
complain( "target " + std::to_string( target ) + " is \"" + std::string( reader.string( entry.name ) ) + "\", not \"" + targets[ target ] + "\"" );
|
|
continue;
|
|
}
|
|
// the index is only meaningful for the parse the container was cooked from, so
|
|
// the check that matters is whether it still lands on that same name
|
|
if( entry.index != scene::logic::no_index ) {
|
|
auto const table { static_cast<scene::logic::node_table>( entry.table ) };
|
|
if( index_in( table, targets[ target ] ) != entry.index ) {
|
|
complain( "target " + std::to_string( target ) + " \"" + targets[ target ] + "\" is at a different index now" );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if( mismatches == 0 ) {
|
|
WriteLog( "Logic container: \"" + path + "\" matches the loaded scenery, " + std::to_string( count ) + " events" );
|
|
return true;
|
|
}
|
|
ErrorLog( "Logic container: \"" + path + "\" disagrees with the loaded scenery in " + std::to_string( mismatches ) + " places" );
|
|
return false;
|
|
}
|
|
|
|
} // namespace simulation
|