18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 19:57:06 +01:00
Files
maszyna/model/MdlMngr.cpp
T
maj00r 2edf9c410d feat(render): Vulkan keeps the compact vertex streams of EU07M models; E4D renamed EU07M (#537)
* feat(render): the Vulkan renderer keeps the vertex streams of E4D models as they are, 28 bytes a vertex

Stage 3 of the E4D model format. A model loaded from .e4d (modelformat e4d) reaches the Vulkan renderer as its compact
streams: positions (12 B), attributes (QTangent snorm16x4 + uv f32x2, 16 B), user data and indices, without the 48-byte
gfx::basic_vertex in between.

- gfx: gfx::compact_attributes, geometry_bank::create_compact() and GfxRenderer->Insert_Compact(). The default of
  Insert_Compact decodes to basic_vertex, so the OpenGL 4.6 and the null renderers get the models as before. The CPU
  readers of a chunk (pantograph, bounds, picking, the editor) get basic_vertex made from the streams the first time they
  read it; a chunk which is changed (replace, append) turns into basic_vertex first.
- Vulkan: a bank whose chunks are all compact keeps positions, user data, attributes and indices in buffers of their own;
  vertex_kind::scene_compact reads them (location 5 the quaternion as R16G16B16A16_SNORM). A program gets a COMPACT_VERTEX
  variant the first time a compact chunk is drawn with it, in the background with gfx.vulkan.asyncresources, as the
  MODEL_DRAWS one; the variant of the multi draws makes a compact one of its own. Only vertex.vert and cabprobe.vert
  decode the normal (compact_frame() in common.glsl); the shaders of shadows, prepass, picking and cube shadows read
  position and uv as before.
- Ray tracing: the BLAS of a compact chunk reads the positions with a stride of 12, and the reflections read the normal
  and uv from the attributes (RT_FLAG_COMPACT in the record). Not tried with ray tracing on.
- The loader hands the streams of the file to Insert_Compact as they are; the stars keep their colours in the normals
  of basic_vertex and go as before.

Measured on Vulkan (self-test, 250 models of the game, runs alternating with legacy): device memory 1.60-1.64 GiB
against 1.89 GiB, GPU time a frame 1.27-1.39 ms against 1.37-1.57 ms, the same frame time and load time, the picture
within 1/255 of the legacy one, no errors of Vulkan in the log. Unit tests 1189/1189; -e4d diff gives the same results
through the new decode. Not built with GCC here.

* refactor(model): the new model format is EU07M (.eu07m), not E4D

E4D read as the next version of E3D, which the format isn't meant to be tied to; EU07M goes with the world format .eu07.
The format isn't released, so nothing reads the old name.

- The extension .eu07m, the magic EU7M (the four bytes of the header), model/eu07m/ with eu07m_*.{h,cpp},
  namespace eu07m, docs/eu07m-format.md, tests/unit/test_eu07m.cpp.
- The command line -eu07m convert|dump|validate|diff, TModel3d::LoadFromEU07M, and the key modelformat eu07m|legacy
  of eu07.ini (Global.modelformat_eu07m).
- Nothing else changes: the sections, the records and the components are the same, so a converted file needs only its
  magic and its extension changed.

Unit tests 1189/1189.

* fix(render): EU07M review of #537: no whole vertices kept for the bounds, the compact variant checked by CI and given up when it fails

- The bounding radius, offset() and MaxY() read the positions of a chunk alone (Positions(), geometry_bank::positions()):
  on Vulkan the compact streams are no longer decoded into basic_vertex and kept for the life of the bank as every model
  loads, so the host memory of EU07M models stays below that of E3D. What still needs whole vertices (the editor, the
  picking, the export) decodes them on first use as before.
- A COMPACT_VERTEX variant which doesn't compile is told once in the log and the banks of EU07M models go back to
  basic_vertex (compact_layout_failed()), those built already when they are drawn next, instead of the models being left
  out for the session.
- The shader validator compiles the vk14 shaders which have COMPACT_VERTEX with it, alone and with MODEL_DRAWS, as the
  renderer makes the variants.

* feat(model): the EU07M model files take the .r3a extension, the switch is modelformat r3a

The model files of the format are named .r3a in place of .eu07m, and eu07.ini selects them with modelformat r3a in place of
modelformat eu07m. The format keeps its name (EU07M), its magic and the -eu07m command line tool. Neither the extension nor
the key has been released, so nothing reads the old ones.

* feat(model): the EU07M model files take the .ng3d extension, the switch is modelformat ng3d

In place of .r3a (2fe1064f9): the model files are .ng3d and eu07.ini selects them with modelformat ng3d. The format keeps its name (EU07M), its magic and the -eu07m command line tool. Neither name has been released.
2026-12-01 18:11:17 +01:00

187 lines
6.8 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
*/
#include "stdafx.h"
#include "model/MdlMngr.h"
#include "model/Model3d.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/utilities.h"
// wczytanie modelu do kontenerka
TModel3d *
TMdlContainer::LoadModel(std::string const &Name, bool const Dynamic) {
Model = std::make_shared<TModel3d>();
if( true == Model->LoadFromFile( Name, Dynamic ) ) {
m_name = Name;
return Model.get();
}
else {
m_name.clear();
Model = nullptr;
return nullptr;
}
};
TModelsManager::modelcontainer_sequence TModelsManager::m_models { 1, TMdlContainer{} };
TModelsManager::stringmodelcontainerindex_map TModelsManager::m_modelsmap;
// what the loaded models keep in memory, for the debug panel
TModelsManager::memory_data
TModelsManager::memory() {
memory_data result;
std::vector<TSubModel const *> pending;
for( auto const &container : m_models ) {
if( container.Model == nullptr ) { continue; }
++result.models;
pending.assign( 1, container.Model->Root );
while( false == pending.empty() ) {
auto const *submodel { pending.back() };
pending.pop_back();
if( submodel == nullptr ) { continue; }
++result.submodels.count;
result.submodels.bytes += memory_of( submodel->Vertices ) + memory_of( submodel->Userdata ) + memory_of( submodel->Indices );
pending.push_back( submodel->Child );
pending.push_back( submodel->Next );
}
}
return result;
}
// wczytanie modelu do tablicy
TModel3d *
TModelsManager::LoadModel(std::string const &Name, std::string const &virtualName, bool dynamic) {
m_models.emplace_back();
auto model = m_models.back().LoadModel( Name, dynamic );
if( model != nullptr ) {
m_modelsmap.try_emplace( virtualName, m_models.size() - 1 );
}
else {
m_models.pop_back();
m_modelsmap.try_emplace( virtualName, null_handle );
}
return model;
}
TModel3d *
TModelsManager::GetModel(std::string const &Name, bool const Dynamic, bool const Logerrors, int uid )
{ // model może być we wpisie "node...model" albo "node...dynamic", a także być dodatkowym w dynamic
// (kabina, wnętrze, ładunek)
// dla "node...dynamic" mamy podaną ścieżkę w "\dynamic\" i musi być co najmniej 1 poziom, zwkle
// są 2
// dla "node...model" może być typowy model statyczny ze ścieżką, domyślnie w "\scenery\" albo
// "\models"
// albo może być model z "\dynamic\", jeśli potrzebujemy wstawić auto czy wagon nieruchomo
// - ze ścieżki z której jest wywołany, np. dir="scenery\bud\" albo dir="dynamic\pkp\st44_v1\"
// plus name="model.t3d"
// - z domyślnej ścieżki dla modeli, np. "scenery\" albo "models\" plus name="bud\dombale.t3d"
// (dir="")
// - konkretnie podanej ścieżki np. name="\scenery\bud\dombale.t3d" (dir="")
// wywołania:
// - konwersja wszystkiego do E3D, podawana dokładna ścieżka, tekstury tam, gdzie plik
// - wczytanie kabiny, dokładna ścieżka, tekstury z katalogu modelu
// - wczytanie ładunku, ścieżka dokładna, tekstury z katalogu modelu
// - wczytanie modelu, ścieżka dokładna, tekstury z katalogu modelu
// - wczytanie przedsionków, ścieżka dokładna, tekstury z katalogu modelu
// - wczytanie uproszczonego wnętrza, ścieżka dokładna, tekstury z katalogu modelu
// - niebo animowane, ścieżka brana ze wpisu, tekstury nieokreślone
// - wczytanie modelu animowanego - Init() - sprawdzić
std::string const buftp { Global.asCurrentTexturePath }; // zapamiętanie aktualnej ścieżki do tekstur,
std::string filename { Name };
if( false == Dynamic
&& contains(Name, '/') ) {
// pobieranie tekstur z katalogu, w którym jest model
// when loading vehicles the path is set by the calling routine, so we can skip it here
Global.asCurrentTexturePath += Name;
Global.asCurrentTexturePath.erase( Global.asCurrentTexturePath.rfind( '/' ) + 1 );
}
erase_extension( filename );
filename = ToLower( filename );
std::string postfix;
if (uid != 0)
postfix = "^^" + std::to_string(uid);
// see if we have it in the databank
auto [banklookupfound, banklookupmodel]{ find_in_databank( filename + postfix ) };
TModel3d *model { banklookupmodel };
if( true == banklookupfound ) {
Global.asCurrentTexturePath = buftp;
return model;
}
// first load attempt, check if it's on disk
if( std::string disklookup { find_on_disk( filename ) }; false == disklookup.empty() ) {
model = LoadModel( disklookup, disklookup + postfix, Dynamic ); // model nie znaleziony, to wczytać
}
else {
// there's nothing matching in the databank nor on the disk, report failure...
if( Logerrors ) {
ErrorLog( "Bad file: failed to locate 3d model file \"" + filename + "\"", logtype::file );
}
// ...and link it with the error model slot
m_modelsmap.try_emplace( filename + postfix, null_handle );
}
Global.asCurrentTexturePath = buftp; // odtworzenie ścieżki do tekstur
return model; // NULL jeśli błąd
};
std::pair<bool, TModel3d *>
TModelsManager::find_in_databank( std::string const &Name ) {
std::vector<std::string> filenames {
Name,
paths::models + Name };
for( auto const &filename : filenames ) {
auto const lookup { m_modelsmap.find( filename ) };
if( lookup != m_modelsmap.end() ) {
return { true, m_models[ lookup->second ].Model.get() };
}
}
return { false, nullptr };
}
// checks whether specified file exists. returns name of the located file, or empty string.
// the file is found in any case, as the scenery made on Windows names it, but the name is given back as asked for: it's the key of
// the bank, and TModel3d::LoadFromFile() opens the file as it's named on the disk
std::string
TModelsManager::find_on_disk( std::string const &Name ) {
std::vector<std::string> extensions { { ".e3d" }, { ".t3d" } };
if( Global.modelformat_eu07m ) {
// a model may come as an .ng3d alone
extensions.emplace_back( ".ng3d" );
}
for( auto const &extension : extensions ) {
if( false == FileCased( Name + extension ).empty() ) {
return Name;
}
if( false == FileCased( paths::models + Name + extension ).empty() ) {
return paths::models + Name;
}
}
return {};
}
//---------------------------------------------------------------------------