mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 11:47:06 +01:00
6c7ec65825
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.
185 lines
6.5 KiB
C++
185 lines
6.5 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/.
|
|
*/
|
|
// 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();
|
|
}
|