18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 23:35:28 +01:00
Files
maszyna/rendering/vulkan14/vulkan14geometry.cpp
T
maj00r 89405a6eaf feat(render): the geometry of the scenery models and the data of the textures streamed, on by default
The streaming of the geometry of EU07M models (modelstreaming) and the streaming of the textures (gfx.textures.streaming) are now
both on by default; either is turned off in eu07.ini with "no", and turned off the game does what it did before.

Geometry (modelstreaming yes, with modelformat eu07m):
- A scenery model (node ... model) of an .eu07m file of at least modelstreaming.minsize KiB (256), without skins, stars and texts of
  E3D and with BNDS, has only its structure read at the load: one read of the beginning of the file (eu07m::read_file() with
  read_options::streams false, validate_scope::structure). Vehicles, cabs, loads and every other model load whole, as before; a
  streamed model asked for by anything else gets its geometry loaded and kept (TModelsManager::GetModel(..., Streamable)).
- Its geometry bank keeps its chunks empty (gfx::residency). A bank without its data draws nothing and is asked of the streamer
  (rendering/geometrystreamer.h): threads read and unpack the streams and the GREF blocks, nearest first, and the main thread hands
  them to the banks, 64 MB or 3 ms a frame. A bank unused for modelstreaming.evictafter s (30) lets go of its data in memory and on
  the GPU; above modelstreaming.budget MB (0, no limit) sooner. Whatever reads the vertices (picking, editor, sky occlusion) gets
  them loaded at once. The walks kept by the GPU culling are made anew when a bank got its data.
- The nodes, materials, components, clips and channels stay loaded: the walk goes through an instance without its geometry as
  through any other (RaAnimate(), RaPrepare()), only its draws are left out, so animations, lights and the events at their ends
  keep their timing. The radius of a streamed model and the bounds of its branches come from BNDS instead of the vertices.
- rendering/common/modelprefetch.h goes through the cells within the draw range and 500 m, 32 a frame, and asks for the geometry
  of the streamed models ahead of their drawing.

Textures (gfx.textures.streaming yes):
- The same pass has the data of the textures of the instances, shapes and tracks in reach read ahead by the texture workers in the
  Vulkan renderer (the OpenGL renderer has its own pass); the read-ahead data not drawn for 5 minutes or left behind is dropped.
- The loading screen waits, two minutes at most, for the geometry and the textures around the start and, in Vulkan, for their
  images made on the GPU (vulkan14_texture::request_background(false), made()).

Measured on Tomaszewo, Vulkan, editor self-test flight start to Kaletnik, four runs on an idle machine, alternated:
- peak committed memory 11.0 GB without streaming, 7.0 GB with textures, 6.2 GB with both; working set 9.0 / 4.8 / 4.2 GB
  (the copies of the texture data were 4.5 GB, the geometry banks 1.55 GB);
- scenario load 104-145 s without, 95-97 s with;
- frame time over the flight the same (11.8-12.5 ms), draws without their texture at the start the same as without streaming and
  fewer in the flight (380 against 636); the prefetch costs the main thread 0.1-0.2 ms a frame.

Also: the self-test commands "modelstream evict|wait|stats" and "memory <label>" (rendering/common/memoryreport.h, a breakdown of
the memory of the process), file_parts (utilities/wholefile.h) for reads of parts of a file, and the docs in docs/eu07m-format.md
("Streaming of the geometry"). The format doesn't change and no asset change is needed. Unit tests: test_eu07m_streaming.cpp.
2026-11-29 20:39:47 +01:00

390 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;
};
// 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;
}
// a streamed bank without its data draws nothing meanwhile; it's asked of the streamer
if (false == ready())
{
return false;
}
std::scoped_lock lock(m_buildmutex);
// 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