/* 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 #include 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 g_generation{0}; // set when a variant of a program for the compact layout didn't compile (compact_layout_failed()) std::atomic 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(chunk.vertices.size()) + ((loop && chunk.indices.empty() && (false == chunk.vertices.empty())) ? 1u : 0u)}; auto const indexcount{static_cast(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 &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 &Vertices, std::vector &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 &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 vertices; gfx::position_array positions; gfx::compact_array attributes; std::vector userdata; std::vector 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(userdata.size()); place.indexfirst = static_cast(indices.size()); if (m_compact) { gpu_compact(chunk, positions, attributes, userdata); } else { gpu_vertices(chunk, vertices, userdata); } place.vertexcount = static_cast(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(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 const &Userdata, gfx::compact_array const *Attributes, std::vector 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, void const *Data, VkDeviceSize const Size, VkBufferUsageFlags const Usage, char const *Name) { Buffer = std::make_shared(); 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 vertices; std::vector userdata; std::vector indices; std::vector> 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 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(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