17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 15:39:20 +02:00
Files
maszyna/scene/logiccontainer.cppm
maj00r 2660d14565 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.
2026-09-16 23:18:09 +02:00

85 lines
3.0 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 <map>
#include <string>
#include <string_view>
#include <vector>
#include "scene/logicformat.h"
export module eu07.scene.logiccontainer;
export {
// Writing and reading the LOGIC container. The layout it produces is described entirely by
// logicformat.h; this is the part that knows how to get bytes in and out of it.
//
// The writer is deterministic: the same scenery cooked twice produces the same file, byte for
// byte. Strings are interned in first-seen order rather than by a hash, records are written in
// the order the manager holds them, and the padding between sections is zeroed. That is worth
// the small cost, because it is what makes a cooked file comparable - a diff that is empty
// proves a refactor changed nothing.
namespace scene::logic {
class container_writer {
public:
// interns a string, returning its index. the same text always gets the same index
std::uint32_t intern( std::string const &Text );
// adds one event and its targets, already resolved to the registry that holds them
void add_event( event_record Record, std::vector<target_entry> const &Targets );
// writes the container. returns false and logs on any failure
bool write( std::string const &Path ) const;
std::size_t events() const { return m_events.size(); }
private:
std::vector<std::string> m_strings;
std::map<std::string, std::uint32_t> m_interned;
std::vector<event_record> m_events;
std::vector<target_entry> m_targets;
};
class container_reader {
public:
// reads the whole container into memory. refuses anything whose magic, version, schema
// hash, endianness or size disagrees with what this build expects
bool open( std::string const &Path );
// whether this build can read the container, judged from its header and size alone
static bool readable( std::string const &Path );
void close();
bool ready() const { return m_ready; }
std::size_t events() const { return m_events.size(); }
event_record const & event( std::size_t const Index ) const { return m_events[ Index ]; }
std::size_t targets() const { return m_targets.size(); }
target_entry const & target( std::size_t const Index ) const { return m_targets[ Index ]; }
// a view into the string blob, valid while the reader is open
std::string_view string( std::uint32_t const Index ) const;
private:
bool m_ready { false };
std::vector<char> m_strings;
std::vector<string_entry> m_stringtable;
std::vector<event_record> m_events;
std::vector<target_entry> m_targets;
};
// the container's name for a scenery file: alongside it, with the extension replaced
std::string container_path( std::string const &Sceneryfile );
// maps the word an event writes as its type onto the cooked kind
event_kind kind_of( std::string const &Type );
} // namespace scene::logic
}