18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 15:17:08 +01:00
Files
maszyna/rendering/common/skincache.cpp
T
maj00r 6c7ec65825 feat(render): the skinned parts of EU07M models moved by a compute shader in Vulkan
The skinning of stage 4 moved the vertices on the CPU and replaced the geometry of each instance. The Vulkan renderer moves
them in a compute shader now; GL 4.6 stays on the CPU.

- gfx::skin_cache: the walks only work out the palette of the joints; flush(), before the walks of the next frame, moves
  the vertices of the instances whose pose changed: through the new GfxRenderer->Skin_Geometry() where the renderer can,
  else on the CPU with Replace / Replace_Indexed as before. A chunk made or emptied makes the buffers of the bank anew from
  the vertices the CPU holds, so every instance skinned on the GPU is moved again then.
- Vulkan (rendering/vulkan14/vulkan14skinning.cpp, shaders/vk14/skin.comp): the bind pose with the weights of a skinned
  part goes to the GPU once, the palette in the ring of the frame; the kernel writes the moved vertices over those of the
  chunk of the instance in the buffer of its bank (gfx::basic_vertex), with barriers against the copies of the uploads and
  the draws of the frames before, and before the draws of the frame. The vertex buffers of the plain banks get the
  storage usage. The kernel is made the first time a part is skinned; without it the CPU moves them.
- eu07.ini: gfx.skinning.gpu (default yes); no moves them on the CPU in Vulkan too. Both give the same picture.

Checked in Vulkan: a tube of two joints turned by 60 degrees, GPU against CPU, with gfx.gpucull 0 and 1: no pixel
differs, and the log shows the kernel ran. Two instances, the second posed later: the first keeps its pose through the
rebuild of the bank, the same as on the CPU. A clip moving the tube every frame. GL 4.6 after the change of the cache:
both instances bent as before (time let run in the editor for the tests only). Unit tests 1200/1200. Not measured on a
large scene, not built with GCC here.
2026-11-29 01:17:01 +01:00

174 lines
4.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 "utilities/Globals.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.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};
// the instances not drawn for a while let go of their geometry; the geometry of those seen posed the first time is made, in the
// bind pose, to be moved below
auto remade{false};
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);
remade = true;
}
it = m_entries.erase(it);
continue;
}
if (cached.pending && (cached.handle == null_handle))
{
make(cached);
remade = true;
}
++it;
}
// a chunk made or emptied makes the buffers of the bank anew from the vertices the cpu holds: what the gpu wrote over them is gone
if (remade)
{
for (auto &entry : m_entries)
{
if (entry.second.gpu && entry.second.posed)
{
entry.second.pending = true;
}
}
}
for (auto &entry : m_entries)
{
auto &cached{entry.second};
if (cached.pending)
{
cached.pending = false;
move(cached);
}
}
}
void skin_cache::make(entry &Cached)
{
if (m_bank == null_handle)
{
m_bank = GfxRenderer->Create_Bank();
}
// the renderer takes the arrays over
auto vertices{Cached.source->rest};
auto indices{Cached.source->indices};
gfx::userdata_array userdata;
Cached.handle = indices.empty() ? GfxRenderer->Insert(vertices, userdata, m_bank, Cached.type) : GfxRenderer->Insert(indices, vertices, userdata, m_bank, Cached.type);
}
void skin_cache::move(entry &Cached)
{
Cached.gpu = Global.gfx_skinning_gpu && GfxRenderer->Skin_Geometry(Cached.handle, Cached.source, Cached.palette);
if (Cached.gpu)
{
return;
}
Cached.vertices.resize(Cached.source->rest.size());
skinning::skin_vertices(Cached.source->rest, Cached.source->weights, Cached.palette, Cached.vertices);
// the renderer takes the arrays over
auto vertices{Cached.vertices};
auto indices{Cached.source->indices};
gfx::userdata_array userdata;
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