mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 12:57:06 +01:00
3b9d8b2fa3
- The bounding radius, offset() and MaxY() read the positions of a chunk alone (Positions(), geometry_bank::positions()): on Vulkan the compact streams are no longer decoded into basic_vertex and kept for the life of the bank as every model loads, so the host memory of EU07M models stays below that of E3D. What still needs whole vertices (the editor, the picking, the export) decodes them on first use as before. - A COMPACT_VERTEX variant which doesn't compile is told once in the log and the banks of EU07M models go back to basic_vertex (compact_layout_failed()), those built already when they are drawn next, instead of the models being left out for the session. - The shader validator compiles the vk14 shaders which have COMPACT_VERTEX with it, alone and with MODEL_DRAWS, as the renderer makes the variants.
379 lines
13 KiB
C++
379 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>
|
|
#include <format>
|
|
|
|
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};
|
|
|
|
// set when a variant of a program for the compact layout didn't compile (compact_layout_failed())
|
|
std::atomic<bool> g_compactfailed{false};
|
|
|
|
} // namespace
|
|
|
|
void compact_layout_failed(std::string_view const Program)
|
|
{
|
|
if (false == g_compactfailed.exchange(true))
|
|
{
|
|
log_error(std::format("the compact vertex variant of \"{}\" didn't compile; EU07M models are drawn from basic_vertex from now on", Program));
|
|
}
|
|
}
|
|
|
|
bool compact_layout_usable()
|
|
{
|
|
return false == g_compactfailed.load();
|
|
}
|
|
|
|
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 = compact_layout_usable() && (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;
|
|
};
|
|
auto ok{make(m_vertices, Vertices, VertexBytes, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, 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);
|
|
// a bank of the compact layout whose programs can't be had is made anew of basic_vertex
|
|
if (m_layoutdirty || (m_compact && (false == compact_layout_usable())))
|
|
{
|
|
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
|