18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 12:57:06 +01:00
Files
maszyna/rendering/vulkan14/vulkan14geometry.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

367 lines
13 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/vulkan14/vulkan14geometry.h"
#include "rendering/vulkan14/vulkan14texture.h"
#include <atomic>
namespace vk14
{
namespace
{
// what opengl reads for the user data of a vertex without any (the attribute isn't given)
gfx::vertex_userdata const no_userdata{glm::vec4{0.f, 0.f, 0.f, 1.f}};
// the numbers the data of the banks get, unique across the banks (a bank made where another one was gets a new one)
std::atomic<std::uint64_t> g_generation{0};
} // namespace
VkPrimitiveTopology topology_of(unsigned int const Type)
{
switch (Type)
{
case GL_POINTS:
return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
case GL_LINES:
return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
case GL_LINE_STRIP:
case GL_LINE_LOOP:
return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
case GL_TRIANGLE_STRIP:
return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
case GL_TRIANGLE_FAN:
return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
case GL_TRIANGLES:
default:
return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
}
}
vulkan14_geometrybank::~vulkan14_geometrybank()
{
release_();
}
void vulkan14_geometrybank::create_(gfx::geometry_handle const &Geometry)
{
// a new chunk: the buffers are made anew with it
m_layoutdirty = true;
}
void vulkan14_geometrybank::replace_(gfx::geometry_handle const &Geometry)
{
// a chunk of the compact layout is changed as basic_vertex (geometry_bank::make_plain()): the bank isn't of the compact layout
// any more, its buffers are made anew
if (m_layoutdirty || m_compact)
{
m_layoutdirty = true;
return;
}
auto const index{Geometry.chunk - 1};
if (index >= m_places.size())
{
m_layoutdirty = true;
return;
}
auto const &chunk{gfx::geometry_bank::chunk(Geometry)};
auto &place{m_places[index]};
auto const loop{chunk.type == GL_LINE_LOOP};
auto const vertexcount{static_cast<std::uint32_t>(chunk.vertices.size()) + ((loop && chunk.indices.empty() && (false == chunk.vertices.empty())) ? 1u : 0u)};
auto const indexcount{static_cast<std::uint32_t>(chunk.indices.size()) + ((loop && (false == chunk.indices.empty())) ? 1u : 0u)};
if ((vertexcount != place.vertexcount) || (indexcount != place.indexcount))
{
// it doesn't fit in its place any more
m_layoutdirty = true;
return;
}
place.dirty = true;
m_chunksdirty = true;
}
void vulkan14_geometrybank::release_()
{
auto *deletions{m_resources.deletions};
if ((deletions != nullptr) && (m_resources.uploads != nullptr) && (m_vertices || m_userdata || m_attributes || m_indices))
{
deletions->push(m_resources.uploads->current_value(),
[vertices = std::move(m_vertices), userdata = std::move(m_userdata), attributes = std::move(m_attributes), indices = std::move(m_indices)]() {});
}
m_vertices.reset();
m_userdata.reset();
m_attributes.reset();
m_indices.reset();
m_places.clear();
m_bytes = 0;
m_layoutdirty = true;
m_chunksdirty = false;
}
void vulkan14_geometrybank::gpu_userdata(geometry_chunk const &Chunk, std::size_t const Count, std::vector<gfx::vertex_userdata> &Userdata)
{
auto const target{Userdata.size() + Count};
Userdata.insert(Userdata.end(), Chunk.userdata.begin(), Chunk.userdata.begin() + std::min(Count, Chunk.userdata.size()));
Userdata.resize(target, no_userdata);
}
void vulkan14_geometrybank::gpu_vertices(geometry_chunk const &Chunk, std::vector<gfx::basic_vertex> &Vertices, std::vector<gfx::vertex_userdata> &Userdata) const
{
Vertices.insert(Vertices.end(), Chunk.vertices.begin(), Chunk.vertices.end());
gpu_userdata(Chunk, Chunk.vertices.size(), Userdata);
if ((Chunk.type == GL_LINE_LOOP) && Chunk.indices.empty() && (false == Chunk.vertices.empty()))
{
// back to the first vertex
Vertices.push_back(Chunk.vertices.front());
Userdata.push_back(Chunk.userdata.empty() ? no_userdata : Chunk.userdata.front());
}
}
void vulkan14_geometrybank::gpu_compact(geometry_chunk const &Chunk, gfx::position_array &Positions, gfx::compact_array &Attributes, std::vector<gfx::vertex_userdata> &Userdata)
{
Positions.insert(Positions.end(), Chunk.positions.begin(), Chunk.positions.end());
Attributes.insert(Attributes.end(), Chunk.attributes.begin(), Chunk.attributes.end());
gpu_userdata(Chunk, Chunk.positions.size(), Userdata);
if ((Chunk.type == GL_LINE_LOOP) && Chunk.indices.empty())
{
// back to the first vertex
Positions.push_back(Chunk.positions.front());
Attributes.push_back(Chunk.attributes.front());
Userdata.push_back(Chunk.userdata.empty() ? no_userdata : Chunk.userdata.front());
}
}
bool vulkan14_geometrybank::build()
{
// there was no memory for the buffers a moment ago: tried again after a while, not by every draw of every chunk of the bank
if (m_failed && (std::chrono::steady_clock::now() - m_failedat < std::chrono::seconds{1}))
{
return false;
}
release_();
// the compact layout when every chunk of the bank is of it (an EU07M model); otherwise its chunks go as basic_vertex
m_compact = (false == m_chunks.empty()) && std::ranges::all_of(m_chunks, [](geometry_chunk const &Chunk) { return Chunk.compact(); });
if (false == m_compact)
{
for (auto &chunk : m_chunks)
{
make_plain(chunk);
}
}
std::vector<gfx::basic_vertex> vertices;
gfx::position_array positions;
gfx::compact_array attributes;
std::vector<gfx::vertex_userdata> userdata;
std::vector<gfx::basic_index> indices;
m_places.resize(m_chunks.size());
for (std::size_t i = 0; i < m_chunks.size(); ++i)
{
auto const &chunk{m_chunks[i]};
auto &place{m_places[i]};
place.vertexfirst = static_cast<std::uint32_t>(userdata.size());
place.indexfirst = static_cast<std::uint32_t>(indices.size());
if (m_compact)
{
gpu_compact(chunk, positions, attributes, userdata);
}
else
{
gpu_vertices(chunk, vertices, userdata);
}
place.vertexcount = static_cast<std::uint32_t>(userdata.size()) - place.vertexfirst;
indices.insert(indices.end(), chunk.indices.begin(), chunk.indices.end());
if ((chunk.type == GL_LINE_LOOP) && (false == chunk.indices.empty()))
{
indices.push_back(chunk.indices.front());
}
place.indexcount = static_cast<std::uint32_t>(indices.size()) - place.indexfirst;
place.dirty = false;
place.generation = ++g_generation;
}
m_layoutdirty = false;
m_chunksdirty = false;
if (userdata.empty())
{
return false;
}
auto const made{m_compact ? make_buffers(positions.data(), positions.size() * sizeof(glm::vec3), userdata, &attributes, indices)
: make_buffers(vertices.data(), vertices.size() * sizeof(gfx::basic_vertex), userdata, nullptr, indices)};
if (false == made)
{
if (false == m_failed)
{
log_error("no memory for the geometry of a bank of " + std::to_string(userdata.size()) + " vertices, trying again every second");
}
release_();
// tried again when it's drawn after a while
m_layoutdirty = true;
m_failed = true;
m_failedat = std::chrono::steady_clock::now();
return false;
}
m_failed = false;
return true;
}
bool vulkan14_geometrybank::make_buffers(void const *Vertices, VkDeviceSize const VertexBytes, std::vector<gfx::vertex_userdata> const &Userdata, gfx::compact_array const *Attributes,
std::vector<gfx::basic_index> const &Indices)
{
auto &memory{*m_resources.memory};
auto &uploads{*m_resources.uploads};
// with the ray tracing the vertices and the indices are built into acceleration structures, and the shading reads them
auto const raytracing{m_resources.raytracing ? (VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR) : 0};
auto const make = [this, &memory, &uploads, raytracing](std::shared_ptr<buffer> &Buffer, void const *Data, VkDeviceSize const Size, VkBufferUsageFlags const Usage, char const *Name) {
Buffer = std::make_shared<buffer>();
if ((false == Buffer->create(memory, Size, Usage | VK_BUFFER_USAGE_TRANSFER_DST_BIT | raytracing, memory_kind::device, Name)) || (false == uploads.upload_buffer(Buffer, Data, Size)))
{
Buffer.reset();
return false;
}
m_bytes += Size;
return true;
};
// 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))
{
ok = make(m_attributes, Attributes->data(), Attributes->size() * sizeof(gfx::compact_attributes), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, "geometry attributes");
}
if (ok && (false == Indices.empty()))
{
ok = make(m_indices, Indices.data(), Indices.size() * sizeof(gfx::basic_index), VK_BUFFER_USAGE_INDEX_BUFFER_BIT, "geometry indices");
}
return ok;
}
void vulkan14_geometrybank::update()
{
m_chunksdirty = false;
// a bank of the compact layout is made anew when a chunk changes (replace_())
if ((false == bool(m_vertices)) || (false == bool(m_userdata)) || m_compact)
{
return;
}
std::vector<gfx::basic_vertex> vertices;
std::vector<gfx::vertex_userdata> userdata;
std::vector<gfx::basic_index> indices;
std::vector<std::pair<VkDeviceSize, VkDeviceSize>> vertexparts, userdataparts, indexparts;
for (std::size_t i = 0; i < m_places.size(); ++i)
{
auto &place{m_places[i]};
if (false == place.dirty)
{
continue;
}
place.dirty = false;
place.generation = ++g_generation;
auto const &chunk{m_chunks[i]};
gpu_vertices(chunk, vertices, userdata);
vertexparts.emplace_back(place.vertexfirst * sizeof(gfx::basic_vertex), place.vertexcount * sizeof(gfx::basic_vertex));
userdataparts.emplace_back(place.vertexfirst * sizeof(gfx::vertex_userdata), place.vertexcount * sizeof(gfx::vertex_userdata));
if ((place.indexcount > 0) && m_indices)
{
indices.insert(indices.end(), chunk.indices.begin(), chunk.indices.end());
if (chunk.type == GL_LINE_LOOP)
{
indices.push_back(chunk.indices.front());
}
indexparts.emplace_back(place.indexfirst * sizeof(gfx::basic_index), place.indexcount * sizeof(gfx::basic_index));
}
}
auto &uploads{*m_resources.uploads};
if (false == vertices.empty())
{
uploads.upload_buffer(m_vertices, vertices.data(), vertices.size() * sizeof(gfx::basic_vertex), vertexparts);
uploads.upload_buffer(m_userdata, userdata.data(), userdata.size() * sizeof(gfx::vertex_userdata), userdataparts);
}
if (false == indices.empty())
{
uploads.upload_buffer(m_indices, indices.data(), indices.size() * sizeof(gfx::basic_index), indexparts);
}
}
bool vulkan14_geometrybank::source(gfx::geometry_handle const &Geometry, chunk_source &Source)
{
if ((Geometry.chunk == 0) || (Geometry.chunk > m_chunks.size()))
{
return false;
}
std::lock_guard<std::mutex> lock(m_mutex);
if (m_layoutdirty)
{
build();
}
else if (m_chunksdirty)
{
update();
}
auto const index{Geometry.chunk - 1};
if ((false == bool(m_vertices)) || (index >= m_places.size()))
{
return false;
}
auto const &place{m_places[index]};
Source.vertices = m_vertices->handle();
Source.userdata = m_userdata->handle();
Source.compact = m_compact && m_attributes;
Source.attributes = Source.compact ? m_attributes->handle() : VK_NULL_HANDLE;
Source.topology = topology_of(m_chunks[index].type);
Source.vertexfirst = place.vertexfirst;
Source.vertexcount = place.vertexcount;
Source.generation = place.generation;
if (m_resources.raytracing)
{
Source.vertex_address = m_vertices->address();
Source.attribute_address = (Source.compact ? m_attributes->address() : 0);
Source.index_address = (m_indices ? m_indices->address() : 0);
}
if (place.indexcount > 0)
{
Source.indices = m_indices->handle();
Source.count = place.indexcount;
Source.first = place.indexfirst;
Source.basevertex = static_cast<std::int32_t>(place.vertexfirst);
}
else
{
Source.indices = VK_NULL_HANDLE;
Source.count = place.vertexcount;
Source.first = place.vertexfirst;
Source.basevertex = 0;
}
return Source.count > 0;
}
bool vulkan14_geometrybank::indirect_source_(gfx::geometry_handle const &Geometry, gfx::indirect_source &Source)
{
chunk_source chunk;
if (false == source(Geometry, chunk))
{
return false;
}
Source.count = chunk.count;
Source.first = chunk.first;
Source.basevertex = chunk.basevertex;
Source.indexed = (chunk.indices != VK_NULL_HANDLE);
Source.type = m_chunks[Geometry.chunk - 1].type;
return true;
}
} // namespace vk14