mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 18:47:06 +01:00
d930b6519d
The variants of a vehicle repeat the same parts byte for byte, and each EU07M file carried them all again. PR #521 showed the problem on E3D; EU07M solves it in the format. - Block: the geometry of one primitive, its vertices in every vertex stream of the file and its indices, with a 16-byte header; its id is 128 bits, two FNV-1a 64 of its bytes. Only a primitive whose vertex and index ranges are its own can be shared. - Store: geometry.eu07mpack in the folder of the models, the blocks each once (zstd when it saves an eighth) and an index sorted by their id (model/eu07m/eu07m_store.{h,cpp}). - GREF, a new required section: the primitives kept in the store. The writer leaves their ranges out of the streams of the file; the reader takes their blocks from the store (read_options::blocks), checks the id and the counts of each and puts them back in place, so the model in memory has its whole streams and nothing past the reader knows the difference. A model with references and no store, or with a block which isn't the one referred to, is refused with the reason. The validator checks the references (primitive, counts, ranges of its own). - The game opens the store of a folder once and keeps it (eu07m::family_store()); dump and validate read it too. - Tools: "-eu07m pack <folder> [minimum bytes]" moves every block of at least 4 KiB (by default) which occurs more than once in the models of the folder into its store, made anew from all of them, and writes the models again referring to it, each read back and compared with what it was before it's replaced; "-eu07m unpack <folder>" writes them whole again. Measured on copies of the converted models of the game: cd/181_v2 60.8 MB -> 4.0 MB (7%), pkp/11xa_v2/main 69.3 MB -> 5.8 MB (8%), pkp/elf_v1 109.6 MB -> 39.0 MB (36%), pkp/en57_v1 104.1 MB -> 39.3 MB (38%), 2-4 s a folder. All 223 packed files pass -eu07m validate with their store. Four ELF parts drawn in the game (Vulkan) from the packed and from the whole files, with the auto exposure off and the same seed: no pixel differs by more than 1. Unit tests 1208/1208, 4 new ones for a round trip through a store, a model without its store, a wrong block, a primitive sharing its vertices and a folder packed, packed again and unpacked. The renderer still makes the geometry of each model on its own; sharing one range of the buffers between the models of a family is the next step. No asset change: the game data has no .eu07m files, and the release doesn't read them.
140 lines
7.2 KiB
C++
140 lines
7.2 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/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "model/eu07m/eu07m_dump.h"
|
|
|
|
#include <array>
|
|
#include <format>
|
|
#include <string_view>
|
|
|
|
namespace eu07m {
|
|
|
|
std::string fourcc_name(std::uint32_t const Id) {
|
|
std::string text(4, ' ');
|
|
for (std::size_t idx = 0; idx < 4; ++idx) {
|
|
auto const character{static_cast<char>((Id >> (idx * 8)) & 0xFF)};
|
|
text[idx] = (character >= 32 && character < 127) ? character : '?';
|
|
}
|
|
return text;
|
|
}
|
|
|
|
namespace {
|
|
|
|
std::string_view topology_name(std::uint32_t const Flags) {
|
|
switch (static_cast<topology>(Flags & primitive_flags::topology_mask)) {
|
|
case topology::points: return "points";
|
|
case topology::lines: return "lines";
|
|
case topology::line_loop: return "line loop";
|
|
case topology::line_strip: return "line strip";
|
|
case topology::triangles: return "triangles";
|
|
case topology::triangle_strip: return "triangle strip";
|
|
case topology::triangle_fan: return "triangle fan";
|
|
default: return "?";
|
|
}
|
|
}
|
|
|
|
std::string quoted(model const &Model, std::uint32_t const Id) {
|
|
return Id == no_index ? std::string{"-"} : std::format("\"{}\"", Model.string(Id));
|
|
}
|
|
|
|
void dump_counts(model const &Model, std::string &Output) {
|
|
Output += std::format("flags: {:#x}, source hash: {:#018x}\n", Model.flags, Model.source_hash);
|
|
Output += std::format("strings: {}, nodes: {} (matrices {}), meshes: {}, primitives: {}, materials: {}\n", Model.strings.size(), Model.nodes.size(), Model.transforms.size(),
|
|
Model.meshes.size(), Model.primitives.size(), Model.materials.size());
|
|
Output += std::format("vertices: {}, indices: {} x u16 + {} x u32, weights: {} + {} wide, userdata: {}, bindings: {}\n", Model.vertex_count(), Model.indices16.size(),
|
|
Model.indices32.size(), Model.weights.size(), Model.weights_wide.size(), Model.vertex_userdata.size(), Model.bindings.size());
|
|
Output += std::format("skins: {} ({} joints), vertex groups: {}, components: {} ({} bytes), bounds: {}, name index: {}\n", Model.skins.size(), Model.skin_joints.size(),
|
|
Model.vertex_groups.size(), Model.components.size(), Model.component_data.size(), Model.bounds.size(), Model.name_index.size());
|
|
if (false == Model.geometry_references.empty())
|
|
Output += std::format("geometry in the store of the family: {} primitives\n", Model.geometry_references.size());
|
|
for (auto const §ion : Model.unknown)
|
|
Output += std::format("unknown section {}: {} bytes\n", fourcc_name(section.id), section.data.size());
|
|
}
|
|
|
|
void dump_primitives(model const &Model, mesh_record const &Mesh, std::string_view const Indent, std::string &Output) {
|
|
for (auto idx = Mesh.primitive_first; idx < Mesh.primitive_first + Mesh.primitive_count && idx < Model.primitives.size(); ++idx) {
|
|
auto const &primitive{Model.primitives[idx]};
|
|
auto const material{primitive.material < Model.materials.size() ? Model.materials[primitive.material] : material_record{}};
|
|
auto const materialname{material.replaceable != 0 ? std::format("skin {}", material.replaceable) : quoted(Model, material.name)};
|
|
Output += std::format("{} {} {}{}, vertices {}+{}, indices {}+{}, material {}, flags {:#x}\n", Indent, topology_name(primitive.flags),
|
|
(primitive.flags & primitive_flags::indexed) != 0 ? "indexed" : "plain", (primitive.flags & primitive_flags::index32) != 0 ? " u32" : "", primitive.base_vertex,
|
|
primitive.vertex_count, primitive.first_index, primitive.index_count, materialname, primitive.flags);
|
|
}
|
|
}
|
|
|
|
void dump_components(model const &Model, std::uint32_t const First, std::uint32_t const Count, std::string_view const Indent, std::string &Output) {
|
|
for (auto idx = First; idx < First + Count && idx < Model.components.size(); ++idx) {
|
|
auto const &component{Model.components[idx]};
|
|
Output += std::format("{} [{}] {} v{}, {} bytes\n", Indent, idx, fourcc_name(component.type), component.version, component.data_size);
|
|
}
|
|
}
|
|
|
|
void dump_nodes(model const &Model, std::string &Output) {
|
|
std::vector<int> depth(Model.nodes.size(), 0);
|
|
for (std::uint32_t idx = 0; idx < Model.nodes.size(); ++idx) {
|
|
auto const &node{Model.nodes[idx]};
|
|
if (node.parent >= 0 && node.parent < static_cast<std::int64_t>(idx))
|
|
depth[idx] = depth[node.parent] + 1;
|
|
std::string const indent(static_cast<std::size_t>(depth[idx]) * 2, ' ');
|
|
Output += std::format("{}{} {} t({:.4f} {:.4f} {:.4f}) r({:.4f} {:.4f} {:.4f} {:.4f}) s({:.4f} {:.4f} {:.4f}){}{} range {:.1f}-{:.1f} legacy {:#x}\n", indent, idx,
|
|
quoted(Model, node.name), node.translation[0], node.translation[1], node.translation[2], node.rotation[0], node.rotation[1], node.rotation[2], node.rotation[3],
|
|
node.scale[0], node.scale[1], node.scale[2], (node.flags & node_flags::matrix) != 0 ? " matrix" : "", (node.flags & node_flags::animated) != 0 ? " animated" : "",
|
|
std::sqrt(node.range_min_sq), std::sqrt(node.range_max_sq), node.legacy_flags);
|
|
if (node.mesh >= 0 && node.mesh < static_cast<std::int64_t>(Model.meshes.size()))
|
|
dump_primitives(Model, Model.meshes[node.mesh], indent, Output);
|
|
dump_components(Model, node.component_first, node.component_count, indent, Output);
|
|
}
|
|
}
|
|
|
|
void dump_model_components(model const &Model, std::string &Output) {
|
|
for (std::uint32_t idx = 0; idx < Model.components.size(); ++idx) {
|
|
auto const &component{Model.components[idx]};
|
|
if (component.kind == target_kind::model)
|
|
Output += std::format("model component [{}] {} v{}, {} bytes\n", idx, fourcc_name(component.type), component.version, component.data_size);
|
|
}
|
|
}
|
|
|
|
void dump_clips(model const &Model, std::string &Output) {
|
|
constexpr std::array<std::string_view, 3> paths{"translation", "rotation", "scale"};
|
|
constexpr std::array<std::string_view, 3> interpolations{"step", "linear", "cubic"};
|
|
for (std::uint32_t idx = 0; idx < Model.clips.size(); ++idx) {
|
|
auto const &clip{Model.clips[idx]};
|
|
Output += std::format("clip {} {} {:.3f} s{}{}\n", idx, quoted(Model, clip.name), clip.duration, (clip.flags & clip_flags::loop) != 0 ? " loop" : "",
|
|
(clip.flags & clip_flags::autoplay) != 0 ? " autoplay" : "");
|
|
for (auto track = clip.track_first; track < clip.track_first + clip.track_count && track < Model.clip_tracks.size(); ++track) {
|
|
auto const &record{Model.clip_tracks[track]};
|
|
auto const path{static_cast<std::size_t>(record.path)};
|
|
auto const interpolation{static_cast<std::size_t>(record.interpolation)};
|
|
auto const node{record.node < Model.nodes.size() ? quoted(Model, Model.nodes[record.node].name) : std::string{"?"}};
|
|
Output += std::format(" node {} {} {} {}, {} keys\n", record.node, node, path < paths.size() ? paths[path] : "?", interpolation < interpolations.size() ? interpolations[interpolation] : "?",
|
|
record.key_count);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::string dump(model const &Model) {
|
|
std::string output;
|
|
dump_counts(Model, output);
|
|
dump_nodes(Model, output);
|
|
dump_model_components(Model, output);
|
|
dump_clips(Model, output);
|
|
if (false == Model.bounds.empty()) {
|
|
auto const &whole{Model.bounds.back()};
|
|
output += std::format("bounds: ({:.3f} {:.3f} {:.3f}) - ({:.3f} {:.3f} {:.3f}), radius {:.3f}\n", whole.min[0], whole.min[1], whole.min[2], whole.max[0], whole.max[1], whole.max[2],
|
|
whole.sphere[3]);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
} // namespace eu07m
|