mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 02:42:46 +01:00
89405a6eaf
The streaming of the geometry of EU07M models (modelstreaming) and the streaming of the textures (gfx.textures.streaming) are now
both on by default; either is turned off in eu07.ini with "no", and turned off the game does what it did before.
Geometry (modelstreaming yes, with modelformat eu07m):
- A scenery model (node ... model) of an .eu07m file of at least modelstreaming.minsize KiB (256), without skins, stars and texts of
E3D and with BNDS, has only its structure read at the load: one read of the beginning of the file (eu07m::read_file() with
read_options::streams false, validate_scope::structure). Vehicles, cabs, loads and every other model load whole, as before; a
streamed model asked for by anything else gets its geometry loaded and kept (TModelsManager::GetModel(..., Streamable)).
- Its geometry bank keeps its chunks empty (gfx::residency). A bank without its data draws nothing and is asked of the streamer
(rendering/geometrystreamer.h): threads read and unpack the streams and the GREF blocks, nearest first, and the main thread hands
them to the banks, 64 MB or 3 ms a frame. A bank unused for modelstreaming.evictafter s (30) lets go of its data in memory and on
the GPU; above modelstreaming.budget MB (0, no limit) sooner. Whatever reads the vertices (picking, editor, sky occlusion) gets
them loaded at once. The walks kept by the GPU culling are made anew when a bank got its data.
- The nodes, materials, components, clips and channels stay loaded: the walk goes through an instance without its geometry as
through any other (RaAnimate(), RaPrepare()), only its draws are left out, so animations, lights and the events at their ends
keep their timing. The radius of a streamed model and the bounds of its branches come from BNDS instead of the vertices.
- rendering/common/modelprefetch.h goes through the cells within the draw range and 500 m, 32 a frame, and asks for the geometry
of the streamed models ahead of their drawing.
Textures (gfx.textures.streaming yes):
- The same pass has the data of the textures of the instances, shapes and tracks in reach read ahead by the texture workers in the
Vulkan renderer (the OpenGL renderer has its own pass); the read-ahead data not drawn for 5 minutes or left behind is dropped.
- The loading screen waits, two minutes at most, for the geometry and the textures around the start and, in Vulkan, for their
images made on the GPU (vulkan14_texture::request_background(false), made()).
Measured on Tomaszewo, Vulkan, editor self-test flight start to Kaletnik, four runs on an idle machine, alternated:
- peak committed memory 11.0 GB without streaming, 7.0 GB with textures, 6.2 GB with both; working set 9.0 / 4.8 / 4.2 GB
(the copies of the texture data were 4.5 GB, the geometry banks 1.55 GB);
- scenario load 104-145 s without, 95-97 s with;
- frame time over the flight the same (11.8-12.5 ms), draws without their texture at the start the same as without streaming and
fewer in the flight (380 against 636); the prefetch costs the main thread 0.1-0.2 ms a frame.
Also: the self-test commands "modelstream evict|wait|stats" and "memory <label>" (rendering/common/memoryreport.h, a breakdown of
the memory of the process), file_parts (utilities/wholefile.h) for reads of parts of a file, and the docs in docs/eu07m-format.md
("Streaming of the geometry"). The format doesn't change and no asset change is needed. Unit tests: test_eu07m_streaming.cpp.
307 lines
14 KiB
C++
307 lines
14 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/.
|
|
*/
|
|
/*
|
|
MaSzyna EU07 locomotive simulator
|
|
Copyright (C) 2001-2004 Marcin Wozniak and others
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "utilities/Classes.h"
|
|
#include "utilities/Float3d.h"
|
|
#include "model/Model3d.h"
|
|
#include "vehicle/DynObj.h"
|
|
#include "scene/scenenode.h"
|
|
|
|
#include <optional>
|
|
|
|
const int iMaxNumLights = 8;
|
|
float const DefaultDarkThresholdLevel { 0.325f };
|
|
|
|
// typy stanu świateł
|
|
enum TLightState
|
|
{
|
|
ls_Off = 0, // zgaszone
|
|
ls_On = 1, // zapalone
|
|
ls_Blink = 2, // migające
|
|
ls_Dark = 3, // Ra: zapalajce się automatycznie, gdy zrobi się ciemno
|
|
ls_Home = 4, // like ls_dark but off late at night
|
|
ls_winter = 5 // turned on when its winter
|
|
};
|
|
|
|
class TAnimVocaloidFrame
|
|
{ // ramka animacji typu Vocaloid Motion Data z programu MikuMikuDance
|
|
public:
|
|
char cBone[15]; // nazwa kości, może być po japońsku
|
|
int iFrame; // numer ramki
|
|
float3 f3Vector; // przemieszczenie
|
|
float4 qAngle; // kwaternion obrotu
|
|
char cBezier[64]; // krzywe Béziera do interpolacji dla x,y,z i obrotu
|
|
};
|
|
|
|
class basic_event;
|
|
|
|
class TAnimContainer : public std::enable_shared_from_this<TAnimContainer>
|
|
{ // opakowanie submodelu, określające animację egzemplarza - obsługiwane jako lista
|
|
friend TAnimModel;
|
|
|
|
private:
|
|
glm::vec3 vRotateAngles; // dla obrotów Eulera
|
|
glm::vec3 vDesiredAngles;
|
|
double fRotateSpeed;
|
|
glm::dvec3 vTranslation;
|
|
glm::dvec3 vTranslateTo;
|
|
double fTranslateSpeed; // może tu dać wektor?
|
|
float4 qCurrent; // aktualny interpolowany
|
|
float4 qStart; // pozycja początkowa (0 dla interpolacji)
|
|
float4 qDesired; // pozycja końcowa (1 dla interpolacji)
|
|
float fAngleCurrent; // parametr interpolacyjny: 0=start, 1=docelowy
|
|
float fAngleSpeed; // zmiana parametru interpolacji w sekundach
|
|
public:
|
|
TSubModel *pSubModel;
|
|
|
|
private:
|
|
std::shared_ptr<float4x4> mAnim; // macierz do animacji kwaternionowych
|
|
// dla kinematyki odwróconej używane są kwaterniony
|
|
float fLength; // długość kości dla IK
|
|
int iAnim; // animacja: +1-obrót Eulera, +2-przesuw, +4-obrót kwaternionem, +8-IK
|
|
//+0x80000000: animacja z eventem, wykonywana poza wyświetlaniem
|
|
//+0x100: pierwszy stopień IK - obrócić w stronę pierwszego potomnego (dziecka)
|
|
//+0x200: drugi stopień IK - dostosować do pozycji potomnego potomnego (wnuka)
|
|
basic_event *evDone; // ewent wykonywany po zakończeniu animacji, np. zapór, obrotnicy
|
|
public:
|
|
// wyświetlania
|
|
TAnimContainer();
|
|
bool Init(TSubModel *pNewSubModel);
|
|
inline
|
|
std::string NameGet() {
|
|
return pSubModel ? pSubModel->pName : ""; };
|
|
void SetRotateAnim( glm::vec3 vNewRotateAngles, double fNewRotateSpeed);
|
|
void SetTranslateAnim( glm::dvec3 vNewTranslate, double fNewSpeed);
|
|
// the rotation and the shift set brought to their ends at once, for the editor which shows them with the time stopped
|
|
void Finish();
|
|
void AnimSetVMD(double fNewSpeed);
|
|
void PrepareModel();
|
|
void UpdateModel();
|
|
void UpdateModelIK();
|
|
bool InMovement(); // czy w trakcie animacji?
|
|
inline
|
|
double AngleGet() {
|
|
return vRotateAngles.z; }; // jednak ostatnia, T3D ma inny układ
|
|
inline
|
|
glm::dvec3 TransGet() {
|
|
return glm::dvec3(-vTranslation.x, vTranslation.z, vTranslation.y); }; // zmiana, bo T3D ma inny układ
|
|
inline
|
|
void WillBeAnimated() {
|
|
if (pSubModel)
|
|
pSubModel->WillBeAnimated(); };
|
|
void EventAssign(basic_event *ev);
|
|
inline
|
|
basic_event * Event() {
|
|
return evDone; };
|
|
};
|
|
|
|
// opakowanie modelu, określające stan egzemplarza
|
|
class TAnimModel : public scene::basic_node {
|
|
|
|
friend opengl46_renderer;
|
|
friend itemproperties_panel;
|
|
|
|
public:
|
|
// constructors
|
|
explicit TAnimModel( scene::node_data const &Nodedata );
|
|
// methods
|
|
static void AnimUpdate( double dt );
|
|
bool Init(std::string const &asName, std::string const &asReplacableTexture);
|
|
bool Load(cParser *parser, bool ter = false);
|
|
// sets up the instance from the values of a definition which was already taken apart, the way Load(cParser *) does it
|
|
// from the text. Name, Texture: as that method gets them ready, lower case model name and forward slashes in both.
|
|
// Twin: instance set up for the same model and texture, to take them after instead of locating them again; can be nullptr.
|
|
// Angles, Scale: content of the optional blocks of the definition, nullptr for a block which isn't there
|
|
void Load( std::string const &Name, std::string const &Texture, TAnimModel const *Twin, glm::vec3 const *Angles, glm::vec3 const *Scale, bool const Transition );
|
|
std::shared_ptr<TAnimContainer> AddContainer(std::string const &Name);
|
|
std::shared_ptr<TAnimContainer> GetContainer(std::string const &Name = "");
|
|
void LightSet( int const n, float const v );
|
|
void SkinSet( int const Index, material_handle const Material );
|
|
std::optional<std::tuple<float, float, std::optional<glm::vec3> > > LightGet( int const n );
|
|
int TerrainCount();
|
|
TSubModel * TerrainSquare(int n);
|
|
int Flags();
|
|
void on_season_update();
|
|
inline
|
|
material_data const *
|
|
Material() const {
|
|
return &m_materialdata; }
|
|
inline
|
|
TModel3d *
|
|
Model() const {
|
|
return pModel; }
|
|
// whether the data of the textures of the instance was read ahead (gfx.textures.streaming, rendering/common/modelprefetch.h)
|
|
bool
|
|
textures_prefetched() const {
|
|
return m_texturesprefetched; }
|
|
void
|
|
textures_prefetched( bool const Value ) {
|
|
m_texturesprefetched = Value; }
|
|
inline
|
|
void
|
|
Angles( glm::vec3 const &Angles ) {
|
|
vAngle = Angles; }
|
|
inline
|
|
glm::vec3
|
|
Angles() const {
|
|
return vAngle; }
|
|
// per-axis scale, applied between rotation and the submodel-local transform chain.
|
|
// (1,1,1) = unchanged. Set by the `scale`/`endscale` scenario directives or the
|
|
// optional `scale <factor>` / `scale <x> <y> <z>` token inside a `node model` block.
|
|
// Per-axis values let you stretch a model along a single dimension; uniform input
|
|
// (single float) broadcasts to all three axes.
|
|
inline
|
|
void
|
|
Scale( glm::vec3 const &Factor ) {
|
|
m_scale = glm::vec3(
|
|
Factor.x > 0.0f ? Factor.x : 1.0f,
|
|
Factor.y > 0.0f ? Factor.y : 1.0f,
|
|
Factor.z > 0.0f ? Factor.z : 1.0f ); }
|
|
inline
|
|
void
|
|
Scale( float const Factor ) {
|
|
Scale( glm::vec3( Factor ) ); }
|
|
inline
|
|
glm::vec3 const &
|
|
Scale() const {
|
|
return m_scale; }
|
|
// members
|
|
std::list<std::shared_ptr<TAnimContainer>> m_animlist;
|
|
|
|
// lista animacji z eventem, które muszą być przeliczane również bez wyświetlania
|
|
static std::list<std::weak_ptr<TAnimContainer>> acAnimList;
|
|
|
|
public:
|
|
// methods
|
|
void RaPrepare(); // ustawienie animacji egzemplarza na wzorcu
|
|
// RaPrepare() for the instanced draws of a model with lights (gfx.instancedlights): both parts of every light shown, the
|
|
// shaders hide what each instance doesn't show by instance_light_bits()
|
|
void RaPrepareInstanced();
|
|
// the light state of the instance for its matrix in the instanced draws: the bits 0x40000000 when the model has numbered
|
|
// lights, bits 0-7 the lit parts of the slots shown, bits 8-15 their dark parts, as RaPrepare() shows them. 0 without lights
|
|
std::uint32_t instance_light_bits() const;
|
|
bool has_lights() const {
|
|
return iNumLights > 0; }
|
|
// rotation and scale of the instance as a matrix: rotate y * rotate x * rotate z * scale
|
|
glm::mat4 const &rotation_scale();
|
|
void RaAnimate( unsigned int const Framestamp ); // przeliczenie animacji egzemplarza
|
|
// starts the animation clip of the model (EU07M) with the name from its beginning, at Speed times its pace; a clip already playing
|
|
// starts over. false when the model has no clip of the name
|
|
bool play_clip( std::string_view Name, float Speed = 1.f );
|
|
// stops the clips playing: each is held at the moment it was stopped, so the nodes stay where they were put, till a clip
|
|
// started on them or an animation event of the scenery moves them
|
|
void stop_clips();
|
|
// sets the channel of the model (EU07M) with the name to Value, kept within its range; the joints and the clips it's bound to
|
|
// follow the next time the instance is drawn. false when the model has no channel of the name
|
|
bool set_channel( std::string_view Name, float Value );
|
|
|
|
// radius() subclass details, calculates node's bounding radius
|
|
float radius_() const override;
|
|
// serialize() subclass details, sends content of the subclass to provided stream
|
|
void serialize_( std::ostream &Output ) const;
|
|
// deserialize() subclass details, restores content of the subclass from provided stream
|
|
void deserialize_( std::istream &Input );
|
|
// export() subclass details, sends basic content of the class in legacy (text) format to provided stream
|
|
void export_as_text_( std::ostream &Output ) const;
|
|
// checks whether provided token is a legacy (text) format keyword
|
|
bool is_keyword( std::string const &Token ) const;
|
|
|
|
// members
|
|
std::shared_ptr<TAnimContainer> pRoot; // pojemniki sterujące, tylko dla aniomowanych submodeli
|
|
TModel3d *pModel { nullptr };
|
|
glm::vec3 vAngle; // bazowe obroty egzemplarza względem osi
|
|
glm::vec3 m_scale { 1.0f, 1.0f, 1.0f }; // per-axis scale (see Scale() accessors above)
|
|
// rotation and scale part of the instance transform, cached for the instanced render path.
|
|
// rebuilt by rotation_scale() whenever the angles or the scale differ from the ones it was built from
|
|
glm::mat4 m_rotationscale { 1.0f };
|
|
glm::vec3 m_rotationscaleangles { 0.0f, 0.0f, 0.0f };
|
|
glm::vec3 m_rotationscalescale { 1.0f, 1.0f, 1.0f };
|
|
bool m_rotationscalevalid { false };
|
|
bool m_texturesprefetched { false }; // the renderer had the data of its textures read ahead (gfx.textures.streaming)
|
|
material_data m_materialdata;
|
|
|
|
std::string asText; // tekst dla wyświetlacza znakowego
|
|
std::string m_skintoken; // replacable skin the way the definition of the node gave it, kept for the export
|
|
// TODO: wrap into a light state struct, remove fixed element count
|
|
int iNumLights { 0 };
|
|
std::array<TSubModel *, iMaxNumLights> LightsOn {}; // Ra: te wskaźniki powinny być w ramach TModel3d
|
|
std::array<TSubModel *, iMaxNumLights> LightsOff {};
|
|
TSubModel *sm_winter_variant {}; // submodel zimowego wariantu
|
|
TSubModel *sm_spring_variant {}; // submodel wiosennego wariantu
|
|
TSubModel *sm_summer_variant {}; // submodel letniego wariantu
|
|
TSubModel *sm_autumn_variant {}; // submodel jesiennego wariantu
|
|
std::array<float, iMaxNumLights> lsLights {}; // ls_Off
|
|
std::array<glm::vec3, iMaxNumLights> m_lightcolors; // -1 in constructor
|
|
std::array<float, iMaxNumLights> m_lighttimers {};
|
|
std::array<float, iMaxNumLights> m_lightopacities; // {1} in constructor
|
|
float fOnTime { 1.f / 2 };// { 60.f / 45.f / 2 };
|
|
float fOffTime { 1.f / 2 };// { 60.f / 45.f / 2 }; // były stałymi, teraz mogą być zmienne dla każdego egzemplarza
|
|
// float fTransitionTime { fOnTime * 0.9f }; // time
|
|
bool m_transition { true }; // smooth transition between light states
|
|
bool m_lightsanimated { false }; // the lights were brought to their state at least once
|
|
unsigned int m_framestamp { 0 }; // id of last rendered gfx frame
|
|
// the animation clips of the model playing on the instance: the clip, the simulation time it started at and its pace
|
|
struct clip_playing {
|
|
std::size_t clip { 0 };
|
|
double start { 0.0 };
|
|
float speed { 1.f };
|
|
std::optional<double> held; // stopped (stop_clips()): the time into the clip it stays at
|
|
};
|
|
std::vector<clip_playing> m_clips;
|
|
bool m_autoplaystarted { false }; // the clips of the model to play on their own were started
|
|
// the transforms of the nodes moved by the clips playing, set on the submodels for the instance drawn (RaPrepare())
|
|
void apply_clips();
|
|
// the values of the channels of the model on the instance, their initial values till one is set
|
|
std::vector<float> m_channels;
|
|
// the joints and the clips bound to the channels set on the submodels for the instance drawn (RaPrepare())
|
|
void apply_channels();
|
|
|
|
// true if this instance is eligible for the batched/instanced render path:
|
|
// no lights, no submodel animations, no replacable skins, no seasonal variants.
|
|
// computed once at end of Load() so the renderer can simply test the flag.
|
|
bool m_instanceable { false };
|
|
// true if the walk of the submodel tree gives the same submodel-local transforms whatever modelview it starts from
|
|
// (no billboards turned to the camera): an instance drawn on its own can then be culled on the gpu like the instanced
|
|
// ones, its tree walked from the origin of the instance, which the culling places in view
|
|
bool m_cameraindependent { false };
|
|
// helper: evaluates current state and updates m_instanceable accordingly.
|
|
void update_instanceable_flag();
|
|
// whether the lit and the dark part of the light are shown now, as RaPrepare() sets them
|
|
void light_parts_shown( int Light, bool &Lit, bool &Dark ) const;
|
|
// helper: assigns the model and the replacable skin to the instance, and binds light and variant submodels of the model
|
|
void assign_model( std::string &name, std::string const &texture, bool const ter );
|
|
|
|
// diagnostic counters (process-wide). Updated inside update_instanceable_flag()
|
|
// so the renderer can surface load-time classification stats in the debug overlay.
|
|
static int s_instanceable_total; // count of TAnimModel*'s classified instanceable
|
|
// bumped whenever an instance loses its instanceable status after it was put in a scene cell (an event adds
|
|
// an animation container, or changes a skin); the cells rebuild their lists of instances drawn one by one then
|
|
static unsigned int s_instanceable_revoked;
|
|
static int s_classified_total; // count of TAnimModel*'s that ran through the classifier
|
|
static int s_rejected_no_pmodel;
|
|
static int s_rejected_lights;
|
|
static int s_rejected_animlist;
|
|
static int s_rejected_animated_submodel;
|
|
};
|
|
|
|
|
|
|
|
class instance_table : public basic_table<TAnimModel> {
|
|
|
|
};
|
|
|
|
//---------------------------------------------------------------------------
|