/* 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 #include 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 source; // kept alive with its buffer std::shared_ptr data; }; std::unordered_map 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 pack(skinning::skinned_geometry const &Source) { std::vector 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 const &Source, std::span 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(); } 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(packed.size() * sizeof(skin_vertex))}; auto data{std::make_shared()}; 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(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(palettesize)); auto const count{static_cast(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(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(); }