18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 21:07:06 +01:00
Files
maszyna/rendering/vulkan14/vulkan14geometry.h
T
maj00r 5185d69dc9 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.
2026-11-28 21:52:47 +01:00

127 lines
5.6 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>
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);
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