18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 12:57:06 +01:00
Files
maszyna/rendering/common/skincache.cpp
T
maj00r 115c13b274 fix(model): EU07M review of #539: a model made anew keeps its transforms, normals of scaled joints, the rest pose far from the origin
- A node of a model made anew (legacy_flags 0) with a transform gets 0x8000, as a converted one has it: the walk and
  posed_transform() left such nodes out, so a skin was drawn off its place in the bind pose (the tube: the upper half 2 m
  off) and jumped when an animation set 0x4000 on the joint.
- Normals are skinned by the cofactors of the blended rotation (the direction of its inverse transpose), with the sign of
  its determinant, not by the rotation: a joint scaled unevenly tilted the normals of its part.
- rest_pose() takes the translation within the tolerance times the distance from the origin of the mesh and the joints
  (palette_scale()): the float chain rounds in proportion to it, and a model a few kilometres out counted as posed in the
  bind pose, with a geometry copy and a skinning pass of its own.

Tests: the rest pose far from the origin, the normals of a stretched and of a mirroring joint.
2026-11-29 18:16:19 +01:00

136 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 const mesh{Submodel.posed_transform()};
auto palette{skinning::palette(mesh, joints, skin.inverse_bind)};
cached.posed = (false == skinning::rest_pose(palette, 1e-5f, skinning::palette_scale(mesh, joints)));
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