mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 20:05:23 +01:00
feat(render): the skinned parts of EU07M models moved by a compute shader in Vulkan (#541)
* 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. * fix(render): EU07M review of #541: the skinning kernel moves normals as the cpu does skin.comp moves the normals by the cofactors of the blended rotation with the sign of its determinant, as skinning::skin_vertices() now does (#539), so a joint scaled unevenly lights its part the same on both paths. * fix(render): the skinning kernel turns the handedness of the tangent frame for a mirroring joint, as the cpu does
This commit is contained in:
@@ -601,6 +601,7 @@ if (WITH_VULKAN)
|
||||
"rendering/vulkan14/vulkan14shadows.cpp"
|
||||
"rendering/vulkan14/vulkan14lamps.cpp"
|
||||
"rendering/vulkan14/vulkan14cabprobe.cpp"
|
||||
"rendering/vulkan14/vulkan14skinning.cpp"
|
||||
"rendering/vulkan14/vulkan14skyocclusion.cpp"
|
||||
"rendering/vulkan14/vulkan14terrain.cpp"
|
||||
"rendering/vulkan14/vulkan14grass.cpp"
|
||||
|
||||
+13
-7
@@ -282,15 +282,21 @@ Prymityw ze skórą (`skin` ≥ 0, wagi w `VSKN` albo `VSKW`) jest rysowany prze
|
||||
animacjami egzemplarza. Odwrotny bind w `SKJT` odwraca więc świat jointu w pozie bindu.
|
||||
- **Pozy.** Jointy to zwykłe węzły modelu, więc ustawia się je jak dotąd: eventy `animation`, animacje z `.mmd` i kod
|
||||
pojazdów (`GetFromName`, `SetRotate`, `SetTranslate`). Osobne API nie jest potrzebne.
|
||||
- **Wierzchołki.** Przejście renderera, które pierwsze w klatce rysuje skinowany submodel egzemplarza, liczy na CPU
|
||||
paletę jointów i, jeśli zmieniła się od poprzedniej klatki, przesuwa wierzchołki (`gfx::skin_cache`,
|
||||
`rendering/common/skincache.cpp`, matematyka w `model/skinning.cpp`). Pozycje idą przez mieszaną macierz, a normalne i
|
||||
tangensy przez jej obrót. Geometria egzemplarza jest robiona i podmieniana przed przejściami następnej klatki, bo w
|
||||
trakcie przejść nic, co rysują, się nie zmienia (`gfx::walk_sync`). Wierzchołki nadążają więc za jointami z
|
||||
opóźnieniem jednej klatki, a w pierwszej klatce egzemplarz jest w pozie bindu.
|
||||
- **Paleta.** Przejście renderera, które pierwsze w klatce rysuje skinowany submodel egzemplarza, liczy na CPU paletę
|
||||
jointów (`gfx::skin_cache`, `rendering/common/skincache.cpp`, matematyka w `model/skinning.cpp`).
|
||||
- **Wierzchołki.** Jeśli paleta zmieniła się od poprzedniej klatki, wierzchołki są przesuwane przed przejściami
|
||||
następnej klatki, bo w trakcie przejść nic, co rysują, się nie zmienia (`gfx::walk_sync`). Pozycje idą przez mieszaną
|
||||
macierz, a normalne i tangensy przez jej obrót.
|
||||
- Renderer Vulkan robi to compute shaderem (`shaders/vk14/skin.comp`, `rendering/vulkan14/vulkan14skinning.cpp`):
|
||||
poza bindu z wagami trafia na GPU raz na skinowaną część, paleta idzie w buforze klatki, a kernel zapisuje
|
||||
wierzchołki prosto w bufor banku, w miejsce geometrii egzemplarza. Gdy bank robi swoje bufory od nowa (nowa
|
||||
geometria egzemplarza albo oddana), wszystkie egzemplarze skinowane na GPU są przesuwane od nowa.
|
||||
- Renderer OpenGL 4.6, a także Vulkan z `gfx.skinning.gpu 0` (domyślnie 1) albo bez kernela, przesuwa wierzchołki na
|
||||
CPU i podmienia geometrię egzemplarza. Oba sposoby dają ten sam obraz.
|
||||
- Wierzchołki nadążają więc za jointami z opóźnieniem jednej klatki, a w pierwszej klatce egzemplarz jest w pozie bindu.
|
||||
- Dopóki jointy są w pozie bindu, rysowana jest geometria modelu, wspólna dla wszystkich egzemplarzy. Egzemplarz
|
||||
niewidziany przez 600 klatek oddaje swoją geometrię.
|
||||
- **Ograniczenia tej wersji:** skinning na CPU (compute shader to następny krok), granice submodelu z pozy bindu,
|
||||
- **Ograniczenia tej wersji:** granice submodelu z pozy bindu,
|
||||
wektory ruchu i ray tracing bez ruchu jointów.
|
||||
- Węzeł z `range_max_sq` 0 nie ma limitu odległości; wcześniej taki węzeł (model zrobiony od nowa, nie przekonwertowany
|
||||
z E3D) nie był w ogóle rysowany.
|
||||
|
||||
@@ -13,6 +13,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#include "model/Model3d.h"
|
||||
#include "rendering/renderer.h"
|
||||
#include "utilities/Globals.h"
|
||||
|
||||
#include <functional>
|
||||
|
||||
@@ -69,8 +70,6 @@ geometry_handle skin_cache::geometry(TSubModel &Submodel, TModel3d const &Model,
|
||||
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);
|
||||
@@ -85,6 +84,9 @@ geometry_handle skin_cache::geometry(TSubModel &Submodel, TModel3d const &Model,
|
||||
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};
|
||||
@@ -93,39 +95,76 @@ void skin_cache::flush(unsigned int const Frame)
|
||||
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;
|
||||
if (false == cached.pending)
|
||||
}
|
||||
// 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)
|
||||
{
|
||||
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)
|
||||
if (entry.second.gpu && entry.second.posed)
|
||||
{
|
||||
m_bank = GfxRenderer->Create_Bank();
|
||||
entry.second.pending = true;
|
||||
}
|
||||
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())
|
||||
}
|
||||
for (auto &entry : m_entries)
|
||||
{
|
||||
auto &cached{entry.second};
|
||||
if (cached.pending)
|
||||
{
|
||||
GfxRenderer->Replace(vertices, userdata, cached.handle, cached.type);
|
||||
}
|
||||
else
|
||||
{
|
||||
GfxRenderer->Replace_Indexed(indices, vertices, userdata, cached.handle);
|
||||
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};
|
||||
|
||||
@@ -25,9 +25,10 @@ namespace gfx
|
||||
{
|
||||
|
||||
// the skinned submodels of the instances drawn (EU07M skins), moved by the pose of the joints of each instance. the walks of the passes
|
||||
// work out the vertices once a frame for each instance and submodel, on the CPU, when the pose changed; the geometry is made and
|
||||
// replaced by flush(), before the walks of the next frame, as nothing the walks draw may change while they draw (gfx::walk_sync). so
|
||||
// the vertices drawn follow the joints a frame late, and the first frame an instance is seen it's drawn in the bind pose
|
||||
// work out the palette of the joints once a frame for each instance and submodel; flush(), before the walks of the next frame, moves
|
||||
// the vertices of those whose pose changed: in a compute shader where the renderer can (Skin_Geometry(), gfx.skinning.gpu), else on
|
||||
// the CPU, replacing their geometry, as nothing the walks draw may change while they draw (gfx::walk_sync). so the vertices drawn
|
||||
// follow the joints a frame late, and the first frame an instance is seen it's drawn in the bind pose
|
||||
class skin_cache
|
||||
{
|
||||
public:
|
||||
@@ -60,10 +61,16 @@ class skin_cache
|
||||
std::vector<glm::mat4> palette;
|
||||
gfx::vertex_array vertices;
|
||||
bool posed{false}; // the joints aren't in the bind pose
|
||||
bool pending{false}; // the vertices are to go to the renderer
|
||||
bool pending{false}; // the vertices are to be moved
|
||||
bool gpu{false}; // moved by the renderer on the gpu the last time
|
||||
geometry_handle handle{};
|
||||
};
|
||||
|
||||
// the geometry of the instance, in the bind pose
|
||||
void make(entry &Cached);
|
||||
// the vertices of the instance moved by its palette, on the gpu or the cpu
|
||||
void move(entry &Cached);
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
std::unordered_map<key, entry, key_hash> m_entries;
|
||||
geometrybank_handle m_bank{};
|
||||
|
||||
@@ -12,6 +12,8 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <utility>
|
||||
|
||||
#include "rendering/geometrybank.h"
|
||||
@@ -21,6 +23,11 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
struct lighting_data;
|
||||
|
||||
namespace skinning
|
||||
{
|
||||
struct skinned_geometry;
|
||||
}
|
||||
|
||||
namespace gfx
|
||||
{
|
||||
class shader_program;
|
||||
@@ -115,6 +122,10 @@ public:
|
||||
virtual auto Replace_Indexed( gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry ) -> bool { return false; }
|
||||
// adds supplied vertex data at the end of specified chunk
|
||||
virtual auto Append(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type) -> bool = 0;
|
||||
// moves the vertices of the chunk Geometry, made from the bind pose of Source, by Palette (skinning::palette()) on the gpu, with the
|
||||
// commands of the frame being made. returns: false when the renderer doesn't (the caller moves them on the cpu)
|
||||
virtual auto Skin_Geometry( gfx::geometry_handle const & /*Geometry*/, std::shared_ptr<skinning::skinned_geometry const> const & /*Source*/, std::span<glm::mat4 const> /*Palette*/ ) -> bool {
|
||||
return false; }
|
||||
// empties specified chunk, its handle stays valid to be filled again
|
||||
virtual void Erase( gfx::geometry_handle const & /*Geometry*/ ) {
|
||||
// a renderer which keeps no chunks of its own has none to empty
|
||||
|
||||
@@ -247,7 +247,13 @@ bool vulkan14_geometrybank::make_buffers(void const *Vertices, VkDeviceSize cons
|
||||
m_bytes += Size;
|
||||
return true;
|
||||
};
|
||||
auto ok{make(m_vertices, Vertices, VertexBytes, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, Attributes != nullptr ? "geometry positions" : "geometry vertices") &&
|
||||
// the vertices of the plain layout may be written by the skinning in a compute shader (vulkan14skinning.cpp)
|
||||
VkBufferUsageFlags vertexusage{VK_BUFFER_USAGE_VERTEX_BUFFER_BIT};
|
||||
if (Attributes == nullptr)
|
||||
{
|
||||
vertexusage |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
||||
}
|
||||
auto ok{make(m_vertices, Vertices, VertexBytes, vertexusage, Attributes != nullptr ? "geometry positions" : "geometry vertices") &&
|
||||
make(m_userdata, Userdata.data(), Userdata.size() * sizeof(gfx::vertex_userdata), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, "geometry user data")};
|
||||
if (ok && (Attributes != nullptr))
|
||||
{
|
||||
|
||||
@@ -56,6 +56,7 @@ class instance_cull;
|
||||
struct image_use;
|
||||
struct ray_draw;
|
||||
struct chunk_source;
|
||||
struct skin_state;
|
||||
class command_stream;
|
||||
} // namespace vk14
|
||||
|
||||
@@ -86,6 +87,7 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk
|
||||
bool Replace_Indexed(gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry) override;
|
||||
bool Append(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type) override;
|
||||
void Erase(gfx::geometry_handle const &Geometry) override;
|
||||
bool Skin_Geometry(gfx::geometry_handle const &Geometry, std::shared_ptr<skinning::skinned_geometry const> const &Source, std::span<glm::mat4 const> Palette) override;
|
||||
gfx::index_array const &Indices(gfx::geometry_handle const &Geometry) const override;
|
||||
gfx::vertex_array const &Vertices(gfx::geometry_handle const &Geometry) const override;
|
||||
gfx::position_array Positions(gfx::geometry_handle const &Geometry) const override;
|
||||
@@ -517,6 +519,10 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk
|
||||
// occupied vehicle whose cab is drawn (nullptr: none), the drawing of the probe when it needs it in this frame
|
||||
bool init_cab_probe(std::string &Reason);
|
||||
void destroy_cab_probe();
|
||||
// the skinning of the skinned parts of EU07M models in a compute shader (vulkan14skinning.cpp): the kernel and the bind poses with
|
||||
// their weights on the gpu, made the first time a part is skinned
|
||||
std::shared_ptr<vk14::skin_state> m_skinning;
|
||||
void destroy_skinning();
|
||||
TDynamicObject *cab_probe_vehicle() const;
|
||||
void render_cab_probe();
|
||||
// the particles of the gpu: what the colour pass under way draws them with, and the depth of the scene drawn so far copied for
|
||||
|
||||
@@ -949,6 +949,7 @@ void vulkan14_renderer::destroy_scene()
|
||||
destroy_weather();
|
||||
destroy_ao();
|
||||
destroy_cab_probe();
|
||||
destroy_skinning();
|
||||
scene.skyocclusion.destroy();
|
||||
scene.skyocclusion_heights_empty.reset();
|
||||
scene.skyocclusion_empty.reset();
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
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/.
|
||||
*/
|
||||
// the skinned parts of EU07M models moved in a compute shader (shaders/vk14/skin.comp): gfx::skin_cache hands each instance whose joints
|
||||
// moved to Skin_Geometry() before the walks of the frame, with the palette of its joints, and the kernel writes the moved vertices over
|
||||
// those of the chunk of the instance in the buffer of its bank, with the commands of the frame. the bind pose and the weights of a
|
||||
// skinned part go to the gpu once, the first time one of its instances is skinned
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "rendering/vulkan14/vulkan14renderer.h"
|
||||
#include "rendering/vulkan14/vulkan14context.h"
|
||||
#include "rendering/vulkan14/vulkan14commands.h"
|
||||
#include "rendering/vulkan14/vulkan14compute.h"
|
||||
#include "rendering/vulkan14/vulkan14geometry.h"
|
||||
#include "model/skinning.h"
|
||||
#include "utilities/Globals.h"
|
||||
#include "utilities/Logs.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace vk14
|
||||
{
|
||||
|
||||
struct skin_state
|
||||
{
|
||||
compute_kernel kernel;
|
||||
bool tried{false}; // the kernel was made, or failed to be
|
||||
bool reported{false}; // the first part skinned on the gpu was logged
|
||||
struct source_data
|
||||
{
|
||||
std::shared_ptr<skinning::skinned_geometry const> source; // kept alive with its buffer
|
||||
std::shared_ptr<buffer> data;
|
||||
};
|
||||
std::unordered_map<skinning::skinned_geometry const *, source_data> sources;
|
||||
};
|
||||
|
||||
} // namespace vk14
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// a vertex of the bind pose with its weights, as skin.comp reads it (std430)
|
||||
struct skin_vertex
|
||||
{
|
||||
glm::vec4 position_u;
|
||||
glm::vec4 normal_v;
|
||||
glm::vec4 tangent;
|
||||
glm::uvec4 joints;
|
||||
glm::vec4 weights;
|
||||
};
|
||||
static_assert(sizeof(skin_vertex) == 80);
|
||||
|
||||
struct skin_constants
|
||||
{
|
||||
glm::uvec4 counts; // vertices, the first vertex of the chunk in the bank, joints
|
||||
};
|
||||
|
||||
constexpr std::uint32_t skin_group_size{64}; // local_size_x of skin.comp
|
||||
|
||||
std::vector<skin_vertex> pack(skinning::skinned_geometry const &Source)
|
||||
{
|
||||
std::vector<skin_vertex> result;
|
||||
result.reserve(Source.rest.size());
|
||||
for (std::size_t idx = 0; idx < Source.rest.size(); ++idx)
|
||||
{
|
||||
auto const &rest{Source.rest[idx]};
|
||||
skin_vertex vertex;
|
||||
vertex.position_u = glm::vec4{rest.position, rest.texture.x};
|
||||
vertex.normal_v = glm::vec4{rest.normal, rest.texture.y};
|
||||
vertex.tangent = rest.tangent;
|
||||
if (idx < Source.weights.size())
|
||||
{
|
||||
auto const &weights{Source.weights[idx]};
|
||||
vertex.joints = glm::uvec4{weights.joints[0], weights.joints[1], weights.joints[2], weights.joints[3]};
|
||||
vertex.weights = glm::vec4{weights.weights[0], weights.weights[1], weights.weights[2], weights.weights[3]};
|
||||
}
|
||||
else
|
||||
{
|
||||
// as skinning::skin_vertices(): a vertex without weights stays in the bind pose
|
||||
vertex.joints = glm::uvec4{0xFFFFFFFFu};
|
||||
vertex.weights = glm::vec4{1.f, 0.f, 0.f, 0.f};
|
||||
}
|
||||
result.push_back(vertex);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool vulkan14_renderer::Skin_Geometry(gfx::geometry_handle const &Geometry, std::shared_ptr<skinning::skinned_geometry const> const &Source, std::span<glm::mat4 const> const Palette)
|
||||
{
|
||||
if ((false == Global.gfx_skinning_gpu) || (m_context == nullptr) || (m_commands == nullptr) || (Source == nullptr) || Source->rest.empty() || Palette.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto &ctx{*m_context};
|
||||
if (m_skinning == nullptr)
|
||||
{
|
||||
m_skinning = std::make_shared<vk14::skin_state>();
|
||||
}
|
||||
auto &state{*m_skinning};
|
||||
if (false == state.tried)
|
||||
{
|
||||
state.tried = true;
|
||||
try
|
||||
{
|
||||
if (false == state.kernel.create(ctx.device.handle(), ctx.pipelines, ctx.shaders.get("skin.comp"), ctx.scene.pass_layout, {}, "skinning"))
|
||||
{
|
||||
ErrorLog("vulkan: no compute pipeline of the skinning, the skinned parts are moved on the cpu");
|
||||
}
|
||||
}
|
||||
catch (gfx::shader_exception const &e)
|
||||
{
|
||||
ErrorLog(std::string("vulkan: invalid shader of the skinning, the skinned parts are moved on the cpu: ") + e.what());
|
||||
}
|
||||
}
|
||||
if (false == bool(state.kernel))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
vk14::chunk_source chunk;
|
||||
if ((false == chunk_of(Geometry, chunk)) || chunk.compact || (chunk.vertexcount < Source->rest.size()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto &source{state.sources[Source.get()]};
|
||||
if (source.data == nullptr)
|
||||
{
|
||||
auto const packed{pack(*Source)};
|
||||
auto const size{static_cast<VkDeviceSize>(packed.size() * sizeof(skin_vertex))};
|
||||
auto data{std::make_shared<vk14::buffer>()};
|
||||
if ((false == data->create(ctx.memory, size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, vk14::memory_kind::device, "skinning bind pose")) ||
|
||||
(false == ctx.uploads.upload_buffer(data, packed.data(), size)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
source.source = Source;
|
||||
source.data = std::move(data);
|
||||
}
|
||||
auto const palettesize{static_cast<VkDeviceSize>(Palette.size() * sizeof(glm::mat4))};
|
||||
auto const slice{ctx.ring.allocate(palettesize, ctx.device.caps().limits.minStorageBufferOffsetAlignment)};
|
||||
if (false == bool(slice))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
std::memcpy(slice.data, Palette.data(), static_cast<std::size_t>(palettesize));
|
||||
auto const count{static_cast<std::uint32_t>(Source->rest.size())};
|
||||
// the vertices are written over what the uploads copied there and what the draws of the frames sent before read
|
||||
vk14::memory_barrier(m_commands, VK_PIPELINE_STAGE_2_TRANSFER_BIT | VK_PIPELINE_STAGE_2_VERTEX_ATTRIBUTE_INPUT_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT | VK_ACCESS_2_VERTEX_ATTRIBUTE_READ_BIT,
|
||||
VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_READ_BIT | VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT);
|
||||
skin_constants const constants{glm::uvec4{count, chunk.vertexfirst, static_cast<std::uint32_t>(Palette.size()), 0u}};
|
||||
state.kernel.dispatch(m_commands,
|
||||
{vk14::buffer_descriptor(source.data->handle(), 0, VK_WHOLE_SIZE), vk14::buffer_descriptor(slice.buffer, slice.offset, palettesize),
|
||||
vk14::buffer_descriptor(chunk.vertices, 0, VK_WHOLE_SIZE)},
|
||||
constants, (count + skin_group_size - 1) / skin_group_size);
|
||||
// read by the draws of the frame
|
||||
vk14::memory_barrier(m_commands, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT, VK_PIPELINE_STAGE_2_VERTEX_ATTRIBUTE_INPUT_BIT,
|
||||
VK_ACCESS_2_VERTEX_ATTRIBUTE_READ_BIT);
|
||||
if (false == state.reported)
|
||||
{
|
||||
state.reported = true;
|
||||
WriteLog("vulkan: the skinned parts of the models are moved by a compute shader (gfx.skinning.gpu)");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void vulkan14_renderer::destroy_skinning()
|
||||
{
|
||||
if (m_skinning == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (m_context != nullptr)
|
||||
{
|
||||
m_skinning->kernel.destroy(m_context->device.handle());
|
||||
}
|
||||
m_skinning.reset();
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
#version 460 core
|
||||
#define STAGE_COMPUTE 1
|
||||
#define LAYOUT_COMPUTE 1
|
||||
#include "common.glsl"
|
||||
// the skinned parts of EU07M models (gfx::skin_cache, vulkan14skinning.cpp): the vertices of the bind pose moved by the joints of their
|
||||
// skin, written over the vertices of the chunk of the instance in the buffer of its bank (gfx::basic_vertex, 12 floats). the
|
||||
// positions go by the matrix blended by the weights, the normals and the tangents by its rotation, renormalised; a joint out of the
|
||||
// palette counts as the identity, as in skinning::skin_vertices()
|
||||
|
||||
layout(local_size_x = 64) in;
|
||||
|
||||
// the bind pose and the weights of a vertex: the uv in the w of the position and of the normal, the handedness in the w of the tangent
|
||||
struct skin_vertex
|
||||
{
|
||||
vec4 position_u;
|
||||
vec4 normal_v;
|
||||
vec4 tangent;
|
||||
uvec4 joints;
|
||||
vec4 weights;
|
||||
};
|
||||
|
||||
layout(std430, set = COMPUTE_SET, binding = 0) readonly buffer skin_source_ssbo
|
||||
{
|
||||
skin_vertex skin_source[];
|
||||
};
|
||||
|
||||
layout(std430, set = COMPUTE_SET, binding = 1) readonly buffer skin_palette_ssbo
|
||||
{
|
||||
mat4 skin_palette[];
|
||||
};
|
||||
|
||||
layout(std430, set = COMPUTE_SET, binding = 2) writeonly buffer skin_output_ssbo
|
||||
{
|
||||
float skin_output[];
|
||||
};
|
||||
|
||||
// x the vertices, y the first vertex of the chunk in the buffer of the bank, z the joints of the palette
|
||||
layout(push_constant) uniform kernel_constants
|
||||
{
|
||||
uvec4 skin_counts;
|
||||
};
|
||||
|
||||
vec3 skin_direction(mat3 rotation, vec3 direction)
|
||||
{
|
||||
vec3 moved = rotation * direction;
|
||||
float len = length(moved);
|
||||
return len > 0.0 ? moved / len : direction;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
uint index = gl_GlobalInvocationID.x;
|
||||
if (index >= skin_counts.x)
|
||||
return;
|
||||
skin_vertex source = skin_source[index];
|
||||
mat4 blended = mat4(0.0);
|
||||
for (int k = 0; k < 4; ++k)
|
||||
{
|
||||
float weight = source.weights[k];
|
||||
if (weight <= 0.0)
|
||||
continue;
|
||||
uint joint = source.joints[k];
|
||||
blended += weight * (joint < skin_counts.z ? skin_palette[joint] : mat4(1.0));
|
||||
}
|
||||
vec3 position = (blended * vec4(source.position_u.xyz, 1.0)).xyz;
|
||||
mat3 rotation = mat3(blended);
|
||||
// the normals by the cofactors of the rotation (the direction of its inverse transpose), with the sign of its determinant,
|
||||
// as skinning::skin_vertices() moves them on the cpu; a mirroring rotation turns the handedness of the tangent frame as well
|
||||
mat3 cofactors = mat3(cross(rotation[1], rotation[2]), cross(rotation[2], rotation[0]), cross(rotation[0], rotation[1]));
|
||||
float handedness = source.tangent.w;
|
||||
if (dot(rotation[0], cofactors[0]) < 0.0)
|
||||
{
|
||||
cofactors = -cofactors;
|
||||
handedness = -handedness;
|
||||
}
|
||||
vec3 normal = skin_direction(cofactors, source.normal_v.xyz);
|
||||
vec3 tangent = skin_direction(rotation, source.tangent.xyz);
|
||||
uint target = (skin_counts.y + index) * 12u;
|
||||
skin_output[target + 0u] = position.x;
|
||||
skin_output[target + 1u] = position.y;
|
||||
skin_output[target + 2u] = position.z;
|
||||
skin_output[target + 3u] = normal.x;
|
||||
skin_output[target + 4u] = normal.y;
|
||||
skin_output[target + 5u] = normal.z;
|
||||
skin_output[target + 6u] = source.position_u.w;
|
||||
skin_output[target + 7u] = source.normal_v.w;
|
||||
skin_output[target + 8u] = tangent.x;
|
||||
skin_output[target + 9u] = tangent.y;
|
||||
skin_output[target + 10u] = tangent.z;
|
||||
skin_output[target + 11u] = handedness;
|
||||
}
|
||||
@@ -1932,6 +1932,11 @@ global_settings::ConfigParse_gfx( cParser &Parser, std::string_view const Token
|
||||
Parser.getTokens(1);
|
||||
Parser >> gfx_instancedlights;
|
||||
}
|
||||
else if (Token == "gfx.skinning.gpu")
|
||||
{
|
||||
Parser.getTokens(1);
|
||||
Parser >> gfx_skinning_gpu;
|
||||
}
|
||||
else if (Token == "gfx.gpucull.bucketrange")
|
||||
{
|
||||
Parser.getTokens(1);
|
||||
@@ -2794,6 +2799,7 @@ global_settings::export_as_text( std::ostream &Output ) const {
|
||||
export_as_text( Output, "gfx.gpucull.sleepers", gfx_gpucull_sleepers );
|
||||
export_as_text( Output, "gfx.gpucull.models", gfx_gpucull_models );
|
||||
export_as_text( Output, "gfx.instancedlights", gfx_instancedlights );
|
||||
export_as_text( Output, "gfx.skinning.gpu", gfx_skinning_gpu );
|
||||
export_as_text( Output, "gfx.gpucull.bucketrange", gfx_gpucull_bucketrange );
|
||||
export_as_text( Output, "gfx.gpucull.walkcache", gfx_gpucull_walkcache );
|
||||
export_as_text( Output, "gfx.geometry.shared", gfx_geometry_shared );
|
||||
|
||||
@@ -459,6 +459,8 @@ struct global_settings {
|
||||
// state of the lights of each instance goes with its matrix, and the shaders hide the parts it doesn't show. no: each one
|
||||
// drawn on its own, as before
|
||||
bool gfx_instancedlights = false;
|
||||
// the skinned parts of EU07M models moved by a compute shader where the renderer can (the vulkan one), else on the cpu
|
||||
bool gfx_skinning_gpu = true;
|
||||
// the buckets of instances of a scene cell lying wholly beyond their range (the largest range of their instances and of
|
||||
// the submodels of their model) are left out of a pass before the culling, the gpu one included, which would leave out
|
||||
// each of their instances anyway
|
||||
|
||||
Reference in New Issue
Block a user