18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 18:47:06 +01:00
Files
maszyna/rendering/geometrybank.h
T
maj00r 3b9d8b2fa3 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.
2026-11-29 18:12:50 +01:00

380 lines
22 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/.
*/
#pragma once
#include "model/ResourceManager.h"
#include "utilities/memorystats.h"
#include <array>
#include <mutex>
struct world_vertex;
namespace gfx {
struct basic_vertex {
glm::vec3 position; // 3d space
glm::vec3 normal; // 3d space
glm::vec2 texture; // uv space
glm::vec4 tangent; // xyz - tangent, w - handedness
basic_vertex() = default;
basic_vertex( glm::vec3 Position, glm::vec3 Normal, glm::vec2 Texture ) :
position( Position ), normal( Normal ), texture( Texture )
{}
static basic_vertex convert(world_vertex const &world, glm::dvec3 const &origin);
world_vertex to_world(glm::dvec3 const &origin = glm::dvec3(0.)) const;
void serialize( std::ostream&, bool const Tangent = false ) const;
void deserialize( std::istream&, bool const Tangent = false );
void serialize_packed( std::ostream&, bool const Tangent = false ) const;
void deserialize_packed( std::istream&, bool const Tangent = false );
};
struct vertex_userdata{
glm::vec4 data; // user data (for color or additional uv channels, not subject to post-processing)
void serialize( std::ostream& ) const;
void deserialize( std::istream& );
void serialize_packed( std::ostream& ) const;
void deserialize_packed( std::istream& );
};
// data streams carried in a vertex
enum stream {
none = 0x0,
position = 0x1,
normal = 0x2,
color = 0x4, // currently normal and colour streams are stored in the same slot, and mutually exclusive
texture = 0x8
};
unsigned int const basic_streams { stream::position | stream::normal | stream::texture };
unsigned int const color_streams { stream::position | stream::color | stream::texture };
struct stream_units {
std::vector<GLint> texture { GL_TEXTURE0 }; // unit associated with main texture data stream. TODO: allow multiple units per stream
};
using basic_index = std::uint32_t;
// where a chunk lies in the buffers of its bank, for an indirect draw command made on the cpu: count of indices (or of
// vertices), first index (or vertex), base vertex, whether the draw is indexed, and the primitive type
struct indirect_source {
std::uint32_t count { 0 };
std::uint32_t first { 0 };
std::int32_t basevertex { 0 };
bool indexed { false };
unsigned int type { 0 };
};
using vertex_array = std::vector<basic_vertex>;
using userdata_array = std::vector<vertex_userdata>;
using index_array = std::vector<basic_index>;
// the attributes of a vertex in the compact layout of the EU07M models: the tangent frame as a snorm16 quaternion (its w is negative
// where the bitangent is flipped), then the texture coordinates. with the position in a stream of its own a vertex takes 28 bytes
// against the 48 of basic_vertex, and the depth and shadow passes read 12 of them
struct compact_attributes {
std::array<std::int16_t, 4> qtangent { 0, 0, 0, 32767 };
glm::vec2 texture { 0.f, 0.f };
};
static_assert( sizeof( compact_attributes ) == 16 );
using position_array = std::vector<glm::vec3>;
using compact_array = std::vector<compact_attributes>;
// the vertices the compact streams stand for (the normal and the tangent from the quaternion)
auto decode_compact( position_array const &Positions, compact_array const &Attributes ) -> vertex_array;
void calculate_tangents( vertex_array &vertices, index_array const &indices, int const type );
void calculate_indices( index_array &Indices, vertex_array &Vertices, userdata_array &Userdata, float tolerancescale = 1.0f );
// generic geometry bank class, allows storage, update and drawing of geometry chunks
struct geometry_handle {
// constructors
geometry_handle() :
bank( 0 ), chunk( 0 )
{}
geometry_handle( std::uint32_t Bank, std::uint32_t Chunk ) :
bank( Bank ), chunk( Chunk )
{}
// methods
inline
operator std::uint64_t() const {
/*
return bank << 14 | chunk; }
*/
return ( std::uint64_t { bank } << 32 | chunk );
}
// members
/*
std::uint32_t
bank : 18, // 250k banks
chunk : 14; // 16k chunks per bank
*/
std::uint32_t bank;
std::uint32_t chunk;
};
class geometry_bank {
public:
// types:
// constructors:
// destructor:
virtual
~geometry_bank() {}
// methods:
// creates a new geometry chunk of specified type from supplied data. returns: handle to the chunk or NULL
auto create( gfx::vertex_array &Vertices, gfx::userdata_array& Userdata, unsigned int const Type ) -> gfx::geometry_handle;
// creates a new indexed geometry chunk of specified type from supplied data. returns: handle to the chunk or NULL
auto create( gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array& Userdata, unsigned int const Type ) -> gfx::geometry_handle;
// creates a new chunk of the compact layout: positions and compact attributes, with indices or without. its vertices() are
// made from them when something reads them first. returns: handle to the chunk or NULL
auto create_compact( gfx::index_array &Indices, gfx::position_array &Positions, gfx::compact_array &Attributes, gfx::userdata_array &Userdata, unsigned int const Type ) -> gfx::geometry_handle;
// replaces vertex data of specified chunk with the supplied data, starting from specified offset
auto replace( gfx::vertex_array &Vertices, gfx::userdata_array& Userdata, gfx::geometry_handle const &Geometry, std::size_t const Offset = 0 ) -> bool;
// replaces the index and the vertex data of specified indexed chunk with the supplied data of the same counts. returns: false if the counts differ
auto replace( gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry ) -> bool;
// adds supplied vertex data at the end of specified chunk
auto append( gfx::vertex_array &Vertices, gfx::userdata_array& Userdata, gfx::geometry_handle const &Geometry ) -> bool;
void erase( gfx::geometry_handle const &Geometry );
// draws geometry stored in specified chunk
auto draw( gfx::geometry_handle const &Geometry, gfx::stream_units const &Units, unsigned int const Streams = basic_streams ) -> std::size_t;
// draws geometry stored in specified chunk N times via glDrawElementsInstanced*, as instances BaseInstance and onwards
auto draw_instanced( gfx::geometry_handle const &Geometry, gfx::stream_units const &Units, std::size_t const InstanceCount, std::uint32_t const BaseInstance, unsigned int const Streams = basic_streams ) -> std::size_t;
// draws geometry stored in supplied sequence of chunks of the bank, in their order. the bank is free to combine them into fewer draw calls
auto draw_batch( gfx::geometry_handle const *First, gfx::geometry_handle const *Last, gfx::stream_units const &Units, unsigned int const Streams = basic_streams ) -> std::size_t;
// makes the data of specified chunk ready for drawing and tells where it lies. returns: false if there's nothing to draw
auto indirect_source( gfx::geometry_handle const &Geometry, gfx::indirect_source &Source ) -> bool;
// draws geometry stored in specified chunk with the command at Offset (bytes) in the bound draw indirect buffer,
// a glDrawElementsIndirect one for an indexed chunk, glDrawArraysIndirect otherwise
void draw_indirect( gfx::geometry_handle const &Geometry, std::intptr_t const Offset, unsigned int const Streams = basic_streams );
// draws chunks of the bank with Count commands from Offset (bytes) in the bound draw indirect buffer, Stride bytes apart,
// with a single glMultiDrawElementsIndirect (or glMultiDrawArraysIndirect). the chunks have to be of the type of the
// specified one and indexed alike. returns: false if the bank can't, nothing is drawn then
auto draw_indirect_multi( gfx::geometry_handle const &Geometry, std::intptr_t const Offset, std::uint32_t const Count, std::uint32_t const Stride ) -> bool;
// the buffers the bank keeps its geometry in, when they're shared with other banks: chunks of banks of the same group can go
// in one multi draw (draw_indirect_multi()). 0 when the bank has buffers of its own. valid once the chunks were made ready
// for drawing (indirect_source())
auto draw_group() const -> std::uint32_t;
// draws geometry stored in supplied list of chunks
template <typename Iterator_>
auto draw( Iterator_ First, Iterator_ Last, gfx::stream_units const &Units, unsigned int const Streams = basic_streams ) ->std::size_t {
std::size_t count { 0 };
while( First != Last ) {
count += draw( *First, Units, Streams ); ++First; }
return count; }
// frees subclass-specific resources associated with the bank, typically called when the bank wasn't in use for a period of time
void release();
// provides direct access to index data of specfied chunk
auto indices( gfx::geometry_handle const &Geometry ) const -> gfx::index_array const &;
// the positions of the vertices of specified chunk, without the basic_vertex of a chunk of the compact layout being made
auto positions( gfx::geometry_handle const &Geometry ) const -> gfx::position_array;
// provides direct access to vertex data of specfied chunk
auto vertices( gfx::geometry_handle const &Geometry ) const -> gfx::vertex_array const &;
// provides direct access to vertex user data of specfied chunk
auto userdata( gfx::geometry_handle const &Geometry ) const -> gfx::userdata_array const &;
// primitive type of specified chunk, 0 if there's no such chunk
auto type( gfx::geometry_handle const &Geometry ) const -> unsigned int;
// what the chunks keep in the process (a copy of what went to the gpu), added to the counts: chunks and vertex bytes, index bytes,
// user data bytes
void count_memory( memory_count &Vertices, memory_count &Indices, memory_count &Userdata ) const;
// kind of geometry of specified chunk, GL_TRIANGLES, GL_TRIANGLE_STRIP...; no_primitive for a handle to no chunk
auto primitive( gfx::geometry_handle const &Geometry ) const -> unsigned int;
static constexpr unsigned int no_primitive { ~0u };
protected:
// types:
struct geometry_chunk {
unsigned int type; // kind of geometry used by the chunk
// geometry data. for a chunk of the compact layout it's made from the compact streams when it's read first (vertices())
mutable gfx::vertex_array vertices;
gfx::index_array indices; // index data
gfx::userdata_array userdata;
// the compact layout (create_compact()): the positions and the attributes, empty for a chunk of basic_vertex
gfx::position_array positions;
gfx::compact_array attributes;
auto compact() const -> bool {
return false == positions.empty(); }
// NOTE: constructor doesn't copy provided geometry data, but moves it
geometry_chunk( gfx::vertex_array &Vertices, gfx::userdata_array& Userdata, unsigned int Type ) :
type( Type )
{
vertices.swap( Vertices );
userdata.swap( Userdata );
}
// NOTE: constructor doesn't copy provided geometry data, but moves it
geometry_chunk( gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array& Userdata, unsigned int Type ) :
type( Type )
{
vertices.swap( Vertices );
userdata.swap( Userdata );
indices.swap( Indices );
}
};
using geometrychunk_sequence = std::vector<geometry_chunk>;
// methods
inline
auto chunk( gfx::geometry_handle const Geometry ) -> geometry_chunk & {
return m_chunks[ Geometry.chunk - 1 ]; }
inline
auto chunk( gfx::geometry_handle const Geometry ) const -> geometry_chunk const & {
return m_chunks[ Geometry.chunk - 1 ]; }
// a chunk of the compact layout turned into one of basic_vertex, before it's changed (replace(), append(), erase())
void make_plain( geometry_chunk &Chunk ) const;
// members:
geometrychunk_sequence m_chunks;
// the vertices of the compact chunks made when they're first read, by one of the threads reading them at a time
mutable std::mutex m_decodemutex;
private:
// methods:
// create() subclass details
virtual void create_( gfx::geometry_handle const &Geometry ) = 0;
// replace() subclass details
virtual void replace_( gfx::geometry_handle const &Geometry ) = 0;
// draw() subclass details
virtual auto draw_( gfx::geometry_handle const &Geometry, gfx::stream_units const &Units, unsigned int const Streams ) -> std::size_t = 0;
// draw_instanced() subclass details. Default implementation falls back to N regular draws.
virtual auto draw_instanced_( gfx::geometry_handle const &Geometry, gfx::stream_units const &Units, std::size_t const InstanceCount, std::uint32_t const BaseInstance, unsigned int const Streams ) -> std::size_t {
std::size_t count { 0 };
for( std::size_t i = 0; i < InstanceCount; ++i ) { count += draw_( Geometry, Units, Streams ); }
return count; }
// draw_batch() subclass details. Default implementation falls back to regular draw of each chunk.
virtual auto draw_batch_( gfx::geometry_handle const *First, gfx::geometry_handle const *Last, gfx::stream_units const &Units, unsigned int const Streams ) -> std::size_t {
std::size_t count { 0 };
for( ; First != Last; ++First ) { count += draw_( *First, Units, Streams ); }
return count; }
// indirect_source() and draw_indirect() subclass details. by default the bank can't draw indirectly
virtual auto indirect_source_( gfx::geometry_handle const & /*Geometry*/, gfx::indirect_source & /*Source*/ ) -> bool { return false; }
virtual void draw_indirect_( gfx::geometry_handle const & /*Geometry*/, std::intptr_t const /*Offset*/, unsigned int const /*Streams*/ ) {}
virtual auto draw_indirect_multi_( gfx::geometry_handle const & /*Geometry*/, std::intptr_t const /*Offset*/, std::uint32_t const /*Count*/, std::uint32_t const /*Stride*/ ) -> bool { return false; }
// draw_group() subclass details. by default the bank keeps its geometry to itself
virtual auto draw_group_() const -> std::uint32_t { return 0; }
// resource release subclass details
virtual void release_() = 0;
};
// geometry bank manager, holds collection of geometry banks
using geometrybank_handle = geometry_handle;
class geometrybank_manager {
public:
// constructors
geometrybank_manager() = default;
// methods:
// performs a resource sweep
void update();
// registers a new geometry bank. returns: handle to the bank
auto register_bank(std::unique_ptr<geometry_bank> bank) -> gfx::geometrybank_handle;
// creates a new geometry chunk of specified type from supplied data, in specified bank. returns: handle to the chunk or NULL
auto create_chunk( gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometrybank_handle const &Geometry, int const Type ) -> gfx::geometry_handle;
// creates a new indexed geometry chunk of specified type from supplied data, in specified bank. returns: handle to the chunk or NULL
auto create_chunk( gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometrybank_handle const &Geometry, unsigned int const Type ) -> gfx::geometry_handle;
// creates a new chunk of the compact layout in specified bank (geometry_bank::create_compact()). returns: handle to the chunk or NULL
auto create_chunk_compact( gfx::index_array &Indices, gfx::position_array &Positions, gfx::compact_array &Attributes, gfx::userdata_array &Userdata, gfx::geometrybank_handle const &Geometry, unsigned int const Type ) -> gfx::geometry_handle;
// replaces data of specified chunk with the supplied vertex data, starting from specified offset
auto replace( gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, std::size_t const Offset = 0 ) -> bool;
// replaces the index and the vertex data of specified indexed chunk with the supplied data of the same counts. returns: false if the counts differ
auto replace( gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry ) -> bool;
// adds supplied vertex data at the end of specified chunk
auto append( gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry ) -> bool;
void erase( gfx::geometry_handle const &Geometry );
// draws geometry stored in specified chunk
void draw( gfx::geometry_handle const &Geometry, unsigned int const Streams = basic_streams );
// draws geometry stored in specified chunk InstanceCount times via GPU instancing.
// The shader reads per-instance modelview matrices from instance_ssbo[gl_BaseInstance + gl_InstanceID].
void draw_instanced( gfx::geometry_handle const &Geometry, std::size_t const InstanceCount, std::uint32_t const BaseInstance, unsigned int const Streams = basic_streams );
// draws geometry stored in supplied sequence of chunks, in their order. neighbouring chunks of the same bank are passed to it together
void draw_batch( gfx::geometry_handle const *First, gfx::geometry_handle const *Last, unsigned int const Streams = basic_streams );
// makes the data of specified chunk ready for an indirect draw and tells where it lies. returns: false if there's nothing to draw
auto indirect_source( gfx::geometry_handle const &Geometry, gfx::indirect_source &Source ) -> bool;
// draws geometry stored in specified chunk with the command at Offset (bytes) in the bound draw indirect buffer
void draw_indirect( gfx::geometry_handle const &Geometry, std::intptr_t const Offset, unsigned int const Streams = basic_streams );
// draws chunks of the bank of the specified one, of its type and indexed alike, with Count commands from Offset in the bound
// draw indirect buffer, Stride bytes apart, in one call. returns: false if the bank can't, nothing is drawn then
auto draw_indirect_multi( gfx::geometry_handle const &Geometry, std::intptr_t const Offset, std::uint32_t const Count, std::uint32_t const Stride ) -> bool;
// what the chunks of a multi draw (draw_indirect_multi()) have to share, besides their type and whether they're indexed: the
// buffers of the bank of the specified chunk. the same for chunks of banks which share their buffers, the bank otherwise.
// valid once the chunk was made ready for drawing (indirect_source())
auto draw_group( gfx::geometry_handle const &Geometry ) const -> std::uint32_t;
// marks the bank of the chunk as used, as the draws do (the vulkan renderer marks the banks its walkers drew from, M2)
void mark_used( gfx::geometry_handle const &Geometry );
template <typename Iterator_>
void draw( Iterator_ First, Iterator_ Last, unsigned int const Streams = basic_streams ) {
while( First != Last ) {
draw( *First, Streams );
++First; } }
// provides direct access to index data of specfied chunk
auto indices( gfx::geometry_handle const &Geometry ) const -> gfx::index_array const &;
// the positions of the vertices of specified chunk (geometry_bank::positions())
auto positions( gfx::geometry_handle const &Geometry ) const -> gfx::position_array;
// provides direct access to vertex data of specfied chunk
auto vertices( gfx::geometry_handle const &Geometry ) const -> gfx::vertex_array const &;
// what the banks keep in the process, for the debug panel: banks, chunks with their vertex bytes, index bytes and user data bytes
struct memory_data {
std::uint64_t banks { 0 };
memory_count vertices;
memory_count indices;
memory_count userdata;
};
auto memory() const -> memory_data;
// provides direct access to vertex data of specfied chunk
auto userdata( gfx::geometry_handle const &Geometry ) const -> gfx::userdata_array const &;
// primitive type of specified chunk, 0 if there's no such chunk
auto type( gfx::geometry_handle const &Geometry ) const -> unsigned int;
// kind of geometry of specified chunk
auto primitive( gfx::geometry_handle const &Geometry ) const -> unsigned int;
// sets target texture unit for the texture data stream
auto units() -> gfx::stream_units & { return m_units; }
// provides access to primitives count
auto primitives_count() const -> std::size_t const & { return m_primitivecount; }
auto primitives_count() -> std::size_t & { return m_primitivecount; }
private:
// types:
using geometrybanktimepoint_pair = std::pair< std::shared_ptr<geometry_bank>, resource_timestamp >;
using geometrybanktimepointpair_sequence = std::deque< geometrybanktimepoint_pair >;
// members:
geometrybanktimepointpair_sequence m_geometrybanks;
garbage_collector<geometrybanktimepointpair_sequence> m_garbagecollector { m_geometrybanks, 60, 120, "geometry buffer" };
gfx::stream_units m_units;
// methods
inline
auto valid( gfx::geometry_handle const &Geometry ) const -> bool {
return ( ( Geometry.bank != 0 )
&& ( Geometry.bank <= m_geometrybanks.size() ) ); }
inline
auto bank( gfx::geometry_handle const Geometry ) -> geometrybanktimepointpair_sequence::value_type & {
return m_geometrybanks[ Geometry.bank - 1 ]; }
inline
auto bank( gfx::geometry_handle const Geometry ) const -> geometrybanktimepointpair_sequence::value_type const & {
return m_geometrybanks[ Geometry.bank - 1 ]; }
// members:
std::size_t m_primitivecount { 0 }; // number of drawn primitives
};
} // namespace gfx