18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 19:57:06 +01:00
Files
maszyna/rendering/vulkan14/vulkan14geometry.h
T
maj00r 2edf9c410d feat(render): Vulkan keeps the compact vertex streams of EU07M models; E4D renamed EU07M (#537)
* feat(render): the Vulkan renderer keeps the vertex streams of E4D models as they are, 28 bytes a vertex

Stage 3 of the E4D model format. A model loaded from .e4d (modelformat e4d) reaches the Vulkan renderer as its compact
streams: positions (12 B), attributes (QTangent snorm16x4 + uv f32x2, 16 B), user data and indices, without the 48-byte
gfx::basic_vertex in between.

- gfx: gfx::compact_attributes, geometry_bank::create_compact() and GfxRenderer->Insert_Compact(). The default of
  Insert_Compact decodes to basic_vertex, so the OpenGL 4.6 and the null renderers get the models as before. The CPU
  readers of a chunk (pantograph, bounds, picking, the editor) get basic_vertex made from the streams the first time they
  read it; a chunk which is changed (replace, append) turns into basic_vertex first.
- Vulkan: a bank whose chunks are all compact keeps positions, user data, attributes and indices in buffers of their own;
  vertex_kind::scene_compact reads them (location 5 the quaternion as R16G16B16A16_SNORM). A program gets a COMPACT_VERTEX
  variant the first time a compact chunk is drawn with it, in the background with gfx.vulkan.asyncresources, as the
  MODEL_DRAWS one; the variant of the multi draws makes a compact one of its own. Only vertex.vert and cabprobe.vert
  decode the normal (compact_frame() in common.glsl); the shaders of shadows, prepass, picking and cube shadows read
  position and uv as before.
- Ray tracing: the BLAS of a compact chunk reads the positions with a stride of 12, and the reflections read the normal
  and uv from the attributes (RT_FLAG_COMPACT in the record). Not tried with ray tracing on.
- The loader hands the streams of the file to Insert_Compact as they are; the stars keep their colours in the normals
  of basic_vertex and go as before.

Measured on Vulkan (self-test, 250 models of the game, runs alternating with legacy): device memory 1.60-1.64 GiB
against 1.89 GiB, GPU time a frame 1.27-1.39 ms against 1.37-1.57 ms, the same frame time and load time, the picture
within 1/255 of the legacy one, no errors of Vulkan in the log. Unit tests 1189/1189; -e4d diff gives the same results
through the new decode. Not built with GCC here.

* refactor(model): the new model format is EU07M (.eu07m), not E4D

E4D read as the next version of E3D, which the format isn't meant to be tied to; EU07M goes with the world format .eu07.
The format isn't released, so nothing reads the old name.

- The extension .eu07m, the magic EU7M (the four bytes of the header), model/eu07m/ with eu07m_*.{h,cpp},
  namespace eu07m, docs/eu07m-format.md, tests/unit/test_eu07m.cpp.
- The command line -eu07m convert|dump|validate|diff, TModel3d::LoadFromEU07M, and the key modelformat eu07m|legacy
  of eu07.ini (Global.modelformat_eu07m).
- Nothing else changes: the sections, the records and the components are the same, so a converted file needs only its
  magic and its extension changed.

Unit tests 1189/1189.

* fix(render): EU07M review of #537: no whole vertices kept for the bounds, the compact variant checked by CI and given up when it fails

- 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.

* feat(model): the EU07M model files take the .r3a extension, the switch is modelformat r3a

The model files of the format are named .r3a in place of .eu07m, and eu07.ini selects them with modelformat r3a in place of
modelformat eu07m. The format keeps its name (EU07M), its magic and the -eu07m command line tool. Neither the extension nor
the key has been released, so nothing reads the old ones.

* feat(model): the EU07M model files take the .ng3d extension, the switch is modelformat ng3d

In place of .r3a (2fe1064f9): the model files are .ng3d and eu07.ini selects them with modelformat ng3d. The format keeps its name (EU07M), its magic and the -eu07m command line tool. Neither name has been released.
2026-12-01 18:11:17 +01:00

134 lines
6.0 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/.
*/
// the geometry banks of the vulkan renderer (stage D1 of the plan): the chunks of a bank in three device buffers of the bank
// (vertices, their user data, 32 bit indices), made the first time the bank is drawn after its chunks changed. a chunk replaced
// with the same size is copied in place, otherwise the buffers are made anew and the old ones go once the gpu is done with them.
// the data stays in memory as well (gfx::geometry_bank), the simulation reads it back. the primitives are the values of opengl
// the program gives them with; a line loop is drawn as a strip which goes back to its first vertex
#pragma once
#include "rendering/geometrybank.h"
#include "rendering/vulkan14/vulkan14common.h"
#include "rendering/vulkan14/vulkan14memory.h"
#include <chrono>
#include <memory>
#include <mutex>
#include <string_view>
namespace vk14
{
struct resources;
// where the data of a chunk is in the buffers of its bank
struct chunk_source
{
// basic_vertex, or for a bank of the compact layout the positions alone (12 bytes a vertex)
VkBuffer vertices{VK_NULL_HANDLE};
VkBuffer userdata{VK_NULL_HANDLE};
// the compact layout: the tangent quaternions and the texture coordinates (gfx::compact_attributes), drawn with the
// vertex_kind::scene_compact variant of the program; null for basic_vertex
VkBuffer attributes{VK_NULL_HANDLE};
bool compact{false};
VkBuffer indices{VK_NULL_HANDLE}; // null for a chunk without indices
std::uint32_t count{0}; // vertices, or indices
std::uint32_t first{0}; // first index, or vertex
std::int32_t basevertex{0};
VkPrimitiveTopology topology{VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST};
// for the ray tracing: the vertices of the chunk in the buffer of the bank, the device addresses of the buffers (0 without the ray
// tracing), and the number the data of the chunk on the gpu has: another one when the buffers were made anew or the chunk changed
std::uint32_t vertexfirst{0};
std::uint32_t vertexcount{0};
VkDeviceAddress vertex_address{0};
VkDeviceAddress attribute_address{0}; // the compact layout
VkDeviceAddress index_address{0};
std::uint64_t generation{0};
};
// the vulkan primitive of a primitive of opengl (GL_TRIANGLES ...)
VkPrimitiveTopology topology_of(unsigned int Type);
// a variant of a scene program for the compact layout didn't compile: the banks of EU07M models are made of basic_vertex
// from then on, those made already when they are drawn next, so the models stay in sight. told once in the log
void compact_layout_failed(std::string_view Program);
// false once compact_layout_failed() was called
bool compact_layout_usable();
class vulkan14_geometrybank : public gfx::geometry_bank
{
public:
explicit vulkan14_geometrybank(resources const &Resources) : m_resources(Resources) {}
~vulkan14_geometrybank() override;
// the chunk with its data on the gpu: uploaded now when it changed since the last time. false: nothing to draw.
// gfx::geometrybank_manager::indirect_source() calls it too (which marks the bank as used). any thread walking the passes (M2);
// what changes the chunks is made while none of them draws (gfx::walk_sync)
bool source(gfx::geometry_handle const &Geometry, chunk_source &Source);
// the bytes on the gpu, for the debug panel
VkDeviceSize gpu_bytes() const
{
return m_bytes;
}
private:
struct chunk_place
{
std::uint32_t vertexfirst{0};
std::uint32_t vertexcount{0}; // on the gpu: one more than the chunk for a line loop
std::uint32_t indexfirst{0};
std::uint32_t indexcount{0};
bool dirty{true};
std::uint64_t generation{0}; // of its data on the gpu, for the ray tracing (chunk_source::generation)
};
void create_(gfx::geometry_handle const &Geometry) override;
void replace_(gfx::geometry_handle const &Geometry) override;
std::size_t draw_(gfx::geometry_handle const &Geometry, gfx::stream_units const &Units, unsigned int const Streams) override
{
return 0;
}
bool indirect_source_(gfx::geometry_handle const &Geometry, gfx::indirect_source &Source) override;
void release_() override;
// the buffers made anew for all the chunks
bool build();
// the chunks changed in place copied to the buffers
void update();
// the vertices of a chunk as they go to the gpu
void gpu_vertices(geometry_chunk const &Chunk, std::vector<gfx::basic_vertex> &Vertices, std::vector<gfx::vertex_userdata> &Userdata) const;
// the same for a chunk of the compact layout
static void gpu_compact(geometry_chunk const &Chunk, gfx::position_array &Positions, gfx::compact_array &Attributes, std::vector<gfx::vertex_userdata> &Userdata);
// the user data of a chunk as it goes to the gpu, for its first Count vertices
static void gpu_userdata(geometry_chunk const &Chunk, std::size_t Count, std::vector<gfx::vertex_userdata> &Userdata);
// the vertex streams of the gpu made of the chunks, in place of the buffers made before
bool make_buffers(void const *Vertices, VkDeviceSize VertexBytes, std::vector<gfx::vertex_userdata> const &Userdata, gfx::compact_array const *Attributes, std::vector<gfx::basic_index> const &Indices);
resources const &m_resources;
// basic_vertex, or the positions of the compact layout
std::shared_ptr<buffer> m_vertices;
std::shared_ptr<buffer> m_userdata;
// the compact layout (every chunk of the bank made with create_compact()): the tangent quaternions and the texture coordinates
std::shared_ptr<buffer> m_attributes;
bool m_compact{false};
std::shared_ptr<buffer> m_indices;
std::vector<chunk_place> m_places;
bool m_layoutdirty{true}; // the buffers have to be made anew
bool m_chunksdirty{false}; // some chunks have to be copied
// the buffers couldn't be made (no memory for them), and when: made again after a while
bool m_failed{false};
std::chrono::steady_clock::time_point m_failedat{};
VkDeviceSize m_bytes{0};
// the buffers made or updated by one of the threads drawing from the bank at a time
std::mutex m_mutex;
};
} // namespace vk14