18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 19:57:06 +01:00

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.
This commit is contained in:
maj00r
2026-12-01 18:11:17 +01:00
committed by GitHub
parent 161a7762c9
commit 2edf9c410d
37 changed files with 924 additions and 423 deletions
+37 -17
View File
@@ -1486,9 +1486,10 @@ void TSubModel::create_geometry(std::size_t &Indexoffset, std::size_t &Vertexoff
// since we're comparing squared radii, we need to square it back for correct results
m_boundingradius *= m_boundingradius;
auto const submodeloffset{offset(std::numeric_limits<float>::max())};
for (auto const &vertex : GfxRenderer->Vertices(m_geometry.handle))
// the positions alone: the vertices of an EU07M model on Vulkan would be made and kept whole for it
for (auto const &position : GfxRenderer->Positions(m_geometry.handle))
{
squaredradius = glm::length2(submodeloffset + vertex.position);
squaredradius = glm::length2(submodeloffset + position);
if (squaredradius > m_boundingradius)
{
m_boundingradius = squaredradius;
@@ -1612,9 +1613,9 @@ float TSubModel::MaxY(float4x4 const &m)
if (m_geometry.handle != null_handle)
{
for (auto const &vertex : GfxRenderer->Vertices(m_geometry.handle))
for (auto const &position : GfxRenderer->Positions(m_geometry.handle))
{
maxy = std::max(maxy, m[0][1] * vertex.position.x + m[1][1] * vertex.position.y + m[2][1] * vertex.position.z + m[3][1]);
maxy = std::max(maxy, m[0][1] * position.x + m[1][1] * position.y + m[2][1] * position.z + m[3][1]);
}
}
else if (false == Vertices.empty())
@@ -1756,17 +1757,30 @@ glm::vec3 TSubModel::offset(float const Geometrytestoffsetthreshold) const
// offset of zero generally means the submodel has optimized identity matrix
// for such cases we resort to an estimate from submodel geometry
// TODO: do proper bounding area calculation for submodel when loading mesh and grab the centre point from it here
auto const &vertices{(m_geometry.handle != null_handle ? GfxRenderer->Vertices(m_geometry.handle) : Vertices)};
if (false == vertices.empty())
// the positions alone: the vertices of an EU07M model on Vulkan would be made and kept whole for them
gfx::position_array positions;
if (m_geometry.handle != null_handle)
{
positions = GfxRenderer->Positions(m_geometry.handle);
}
else
{
positions.reserve(Vertices.size());
for (auto const &vertex : Vertices)
{
positions.push_back(vertex.position);
}
}
if (false == positions.empty())
{
// transformation matrix for the submodel can still contain rotation and/or scaling,
// so we pass the vertex positions through it rather than just grab them directly
offset = glm::vec3();
auto const vertexfactor{1.f / vertices.size()};
auto const vertexfactor{1.f / positions.size()};
auto const transformationmatrix{glm::make_mat4(parentmatrix.readArray())};
for (auto const &vertex : vertices)
for (auto const &position : positions)
{
offset += glm::vec3{transformationmatrix * glm::vec4{vertex.position, 1}} * vertexfactor;
offset += glm::vec3{transformationmatrix * glm::vec4{position, 1}} * vertexfactor;
}
}
}
@@ -1776,12 +1790,12 @@ glm::vec3 TSubModel::offset(float const Geometrytestoffsetthreshold) const
namespace
{
// modelformat e4d: an .e4d older than the .e3d / .t3d it was made from isn't used, the model was changed after it was
// modelformat ng3d: an .ng3d older than the .e3d / .t3d it was made from isn't used, the model was changed after it was
// converted (the hash of the source in its header would need the source read whole to be compared)
bool e4d_up_to_date(std::string const &E4d, std::initializer_list<std::string> const Sources)
bool eu07m_up_to_date(std::string const &Eu07m, std::initializer_list<std::string> const Sources)
{
std::error_code error;
auto const made{std::filesystem::last_write_time(E4d, error)};
auto const made{std::filesystem::last_write_time(Eu07m, error)};
if (error)
return false;
for (auto const &source : Sources)
@@ -1790,7 +1804,7 @@ bool e4d_up_to_date(std::string const &E4d, std::initializer_list<std::string> c
continue;
if (auto const changed{std::filesystem::last_write_time(source, error)}; (false == bool(error)) && (changed > made))
{
WriteLog("Model \"" + source + "\" is newer than its \"" + E4d + "\", which isn't used", logtype::model);
WriteLog("Model \"" + source + "\" is newer than its \"" + Eu07m + "\", which isn't used", logtype::model);
return false;
}
}
@@ -1898,10 +1912,10 @@ bool TModel3d::LoadFromFile(std::string const &FileName, bool dynamic)
Init();
assign_lod_ranges(Root, name);
}
else if (auto const e4dfile{Global.modelformat_e4d ? FileCased(name + ".e4d") : std::string{}};
(false == e4dfile.empty()) && e4d_up_to_date(e4dfile, {binaryfile, textfile}) && LoadFromE4D(e4dfile, dynamic))
else if (auto const eu07mfile{Global.modelformat_eu07m ? FileCased(name + ".ng3d") : std::string{}};
(false == eu07mfile.empty()) && eu07m_up_to_date(eu07mfile, {binaryfile, textfile}) && LoadFromEU07M(eu07mfile, dynamic))
{
// modelformat e4d: the .e4d made from the model takes its place; one which doesn't load leaves the .e3d / .t3d to go on
// modelformat ng3d: the .ng3d made from the model takes its place; one which doesn't load leaves the .e3d / .t3d to go on
asBinary = "";
Init();
assign_lod_ranges(Root, name);
@@ -2675,7 +2689,7 @@ void TModel3d::deserialize(std::istream &s, size_t size, bool dynamic)
finish_binary(dynamic, hastangents);
}
// the submodels read from a file (E3D or E4D) tied together, their materials looked up and their geometry handed to the renderer
// the submodels read from a file (E3D or EU07M) tied together, their materials looked up and their geometry handed to the renderer
void TModel3d::finish_binary(bool const dynamic, bool const hastangents)
{
for (size_t i = 0; (int)i < iSubModelsCount; ++i)
@@ -2703,6 +2717,12 @@ void TModel3d::finish_binary(bool const dynamic, bool const hastangents)
break;
}
}
if (false == Root[i].CompactPositions.empty())
{
// a submodel of an EU07M model: its compact streams as they are, the renderer keeps them or makes basic_vertex of them
Root[i].m_geometry.handle = GfxRenderer->Insert_Compact(Root[i].Indices, Root[i].CompactPositions, Root[i].CompactAttributes, Root[i].Userdata, m_geometrybank, type);
continue;
}
if (false == hastangents)
{
gfx::calculate_tangents(Root[i].Vertices, Root[i].Indices, type);