18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 08:17:09 +01:00
Files
maszyna/rendering/common/skincache.cpp
T
maj00r d324c53233 feat(model): skinned parts of EU07M models move with their joints, skinned on the CPU
Stage 4 of the EU07M model format. A primitive with a skin (SKIN, SKJT, weights in VSKN or VSKW) was drawn in its bind
pose. Now it follows the joints of its skin, in both renderers.

- The joints are nodes of the model, so they're posed as before: animation events, the .mmd, SetRotate / SetTranslate
  from the vehicle code. The matrix of a joint is world(joint) * inverse bind(joint), world being the transform of the
  joint from the root of the model with the animations of the instance drawn (TSubModel::posed_transform()); the
  vertices lie in the bind pose in the space of the model, the transform of the node of the mesh left out, as in glTF.
- The loader keeps the bind pose and the weights of a skinned primitive (skinning::skinned_geometry on the submodel) and
  the skins of the model (TModel3d::skins()).
- The first pass of a frame to draw a skinned submodel of an instance works out the palette and, when it changed, moves
  the vertices on the CPU: positions by the blended matrix, normals and tangents by its rotation (model/skinning.cpp,
  gfx::skin_cache in rendering/common/skincache.cpp). The geometry of the instance is made or replaced before the passes
  of the next frame (flush() in the frame setup of both renderers), as nothing the passes draw may change while they draw
  (gfx::walk_sync): the vertices follow the joints a frame late. In the bind pose the shared geometry of the model is
  drawn; an instance not drawn for 600 frames lets go of its own.
- Fix: a node with range_max_sq 0, which the spec gives as no limit, was never drawn (a model made anew rather than
  converted from E3D); it keeps the default range of a submodel now.
- The editor self-test gets "animate <node> <submodel> <x> <y> <z>", which turns a submodel of an instance as an
  animation event does, since the editor doesn't run the events.

Not in this commit: the skinning in a compute shader, bounds of the posed submodel, motion vectors and ray tracing of the
moved vertices.

Checked: a tube of two joints in two instances, one turned by 60 degrees at its middle, bends smoothly while the other
stays straight, in Vulkan and GL 4.6, with gfx.gpucull 0 and 1 (time let run in the editor for the test only). Unit
tests 1194/1194, 5 new ones for the weights, the bind pose, the blending and the file. Not built with GCC here.
2026-11-28 23:52:35 +01:00

135 lines
3.4 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 "rendering/common/skincache.h"
#include "model/Model3d.h"
#include "rendering/renderer.h"
#include <functional>
namespace gfx
{
namespace
{
// frames an instance keeps its skinned geometry without being drawn
constexpr unsigned int kept_frames{600};
} // namespace
skin_cache &skin_cache::instance()
{
static skin_cache cache;
return cache;
}
std::size_t skin_cache::key_hash::operator()(key const &Key) const noexcept
{
return std::hash<std::uintptr_t>{}(Key.owner) ^ (std::hash<void const *>{}(Key.submodel) * 31u);
}
geometry_handle skin_cache::geometry(TSubModel &Submodel, TModel3d const &Model, unsigned int const Frame)
{
auto const &source{Submodel.m_skin};
auto const &skins{Model.skins()};
if ((source == nullptr) || (source->skin < 0) || (static_cast<std::size_t>(source->skin) >= skins.size()))
{
return Submodel.m_geometry.handle;
}
std::scoped_lock lock{m_mutex};
auto &cached{m_entries[key{TSubModel::iInstance, &Submodel}]};
if (cached.source == nullptr)
{
cached.source = source;
cached.type = Submodel.eType;
}
if (cached.frame != Frame)
{
// the first pass of the frame to draw the instance works out its pose; the joints are set up for the instance drawn
cached.frame = Frame;
auto const &skin{skins[source->skin]};
std::vector<glm::mat4> joints;
joints.reserve(skin.joints.size());
for (auto *joint : skin.joints)
{
joints.emplace_back(joint != nullptr ? joint->posed_transform() : glm::mat4{1.f});
}
auto palette{skinning::palette(Submodel.posed_transform(), joints, skin.inverse_bind)};
cached.posed = (false == skinning::rest_pose(palette));
if (cached.posed && (palette != cached.palette))
{
cached.vertices.resize(source->rest.size());
skinning::skin_vertices(source->rest, source->weights, palette, cached.vertices);
cached.pending = true;
}
cached.palette = std::move(palette);
}
if ((false == cached.posed) || (cached.handle == null_handle))
{
return Submodel.m_geometry.handle;
}
return cached.handle;
}
void skin_cache::flush(unsigned int const Frame)
{
std::scoped_lock lock{m_mutex};
for (auto it = m_entries.begin(); it != m_entries.end();)
{
auto &cached{it->second};
if (Frame - cached.frame > kept_frames)
{
if (cached.handle != null_handle)
{
GfxRenderer->Erase(cached.handle);
}
it = m_entries.erase(it);
continue;
}
++it;
if (false == cached.pending)
{
continue;
}
cached.pending = false;
// the renderer takes the arrays over
auto vertices{cached.vertices};
auto indices{cached.source->indices};
gfx::userdata_array userdata;
if (cached.handle == null_handle)
{
if (m_bank == null_handle)
{
m_bank = GfxRenderer->Create_Bank();
}
cached.handle = indices.empty() ? GfxRenderer->Insert(vertices, userdata, m_bank, cached.type) : GfxRenderer->Insert(indices, vertices, userdata, m_bank, cached.type);
}
else if (indices.empty())
{
GfxRenderer->Replace(vertices, userdata, cached.handle, cached.type);
}
else
{
GfxRenderer->Replace_Indexed(indices, vertices, userdata, cached.handle);
}
}
}
std::size_t skin_cache::size() const
{
std::scoped_lock lock{m_mutex};
return m_entries.size();
}
} // namespace gfx