18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 02:42:46 +01:00

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.
This commit is contained in:
maj00r
2026-11-29 18:12:50 +01:00
parent 20f2156ce2
commit 3b9d8b2fa3
10 changed files with 109 additions and 11 deletions
+11
View File
@@ -226,6 +226,17 @@ bool check_vk14(std::string const &Name)
if (!check_text(gfx::preprocess_shader(gfx::shader_api::vulkan14, Name, "#define NO_MOTION_OUTPUT 1\n", {}, false, vk14_dir), "-V --target-env " + vulkan_env))
suc = false;
}
// the compact vertex layout of EU07M models (vk14::vertex_kind::scene_compact), alone and with the multi draws, as the
// renderer puts the definitions before the sources (compact_kind and multidraw_kind of vulkan14indirect.cpp)
if (source.text.find("COMPACT_VERTEX") != std::string::npos)
{
std::cout << "done, compact vertices..." << std::flush;
for (std::string_view const defines : {"#define COMPACT_VERTEX\n", "#define MODEL_DRAWS\n#define COMPACT_VERTEX\n"})
{
if (!check_text(gfx::preprocess_shader(gfx::shader_api::vulkan14, Name, std::string{defines}, {}, false, vk14_dir), "-V --target-env " + vulkan_env))
suc = false;
}
}
// the ray traced reflections (gfx.raytracing): the renderer puts the extensions and REFLECTION_RAYS before the sources
// (vk14::binding_preamble()), the shaders which shade with light_common.glsl trace the rays
if (source.text.find("REFLECTION_RAYS_ACTIVE") != std::string::npos)
+23 -9
View File
@@ -1486,9 +1486,10 @@ void TSubModel::create_geometry(std::size_t &Indexoffset, std::size_t &Vertexoff
// since we're comparing squared radii, we need to square it back for correct results
m_boundingradius *= m_boundingradius;
auto const submodeloffset{offset(std::numeric_limits<float>::max())};
for (auto const &vertex : GfxRenderer->Vertices(m_geometry.handle))
// the positions alone: the vertices of an EU07M model on Vulkan would be made and kept whole for it
for (auto const &position : GfxRenderer->Positions(m_geometry.handle))
{
squaredradius = glm::length2(submodeloffset + vertex.position);
squaredradius = glm::length2(submodeloffset + position);
if (squaredradius > m_boundingradius)
{
m_boundingradius = squaredradius;
@@ -1612,9 +1613,9 @@ float TSubModel::MaxY(float4x4 const &m)
if (m_geometry.handle != null_handle)
{
for (auto const &vertex : GfxRenderer->Vertices(m_geometry.handle))
for (auto const &position : GfxRenderer->Positions(m_geometry.handle))
{
maxy = std::max(maxy, m[0][1] * vertex.position.x + m[1][1] * vertex.position.y + m[2][1] * vertex.position.z + m[3][1]);
maxy = std::max(maxy, m[0][1] * position.x + m[1][1] * position.y + m[2][1] * position.z + m[3][1]);
}
}
else if (false == Vertices.empty())
@@ -1756,17 +1757,30 @@ glm::vec3 TSubModel::offset(float const Geometrytestoffsetthreshold) const
// offset of zero generally means the submodel has optimized identity matrix
// for such cases we resort to an estimate from submodel geometry
// TODO: do proper bounding area calculation for submodel when loading mesh and grab the centre point from it here
auto const &vertices{(m_geometry.handle != null_handle ? GfxRenderer->Vertices(m_geometry.handle) : Vertices)};
if (false == vertices.empty())
// the positions alone: the vertices of an EU07M model on Vulkan would be made and kept whole for them
gfx::position_array positions;
if (m_geometry.handle != null_handle)
{
positions = GfxRenderer->Positions(m_geometry.handle);
}
else
{
positions.reserve(Vertices.size());
for (auto const &vertex : Vertices)
{
positions.push_back(vertex.position);
}
}
if (false == positions.empty())
{
// transformation matrix for the submodel can still contain rotation and/or scaling,
// so we pass the vertex positions through it rather than just grab them directly
offset = glm::vec3();
auto const vertexfactor{1.f / vertices.size()};
auto const vertexfactor{1.f / positions.size()};
auto const transformationmatrix{glm::make_mat4(parentmatrix.readArray())};
for (auto const &vertex : vertices)
for (auto const &position : positions)
{
offset += glm::vec3{transformationmatrix * glm::vec4{vertex.position, 1}} * vertexfactor;
offset += glm::vec3{transformationmatrix * glm::vec4{position, 1}} * vertexfactor;
}
}
}
+24
View File
@@ -496,6 +496,24 @@ geometry_bank::indices( gfx::geometry_handle const &Geometry ) const {
return geometry_bank::chunk( Geometry ).indices;
}
// the positions of the vertices of specified chunk: of a chunk of the compact layout its stream as it is, without making the
// basic_vertex of it which vertices() keeps for good
position_array
geometry_bank::positions( gfx::geometry_handle const &Geometry ) const {
auto const &chunk { geometry_bank::chunk( Geometry ) };
std::scoped_lock lock { m_decodemutex };
if( chunk.compact() ) {
return chunk.positions;
}
position_array positions;
positions.reserve( chunk.vertices.size() );
for( auto const &vertex : chunk.vertices ) {
positions.push_back( vertex.position );
}
return positions;
}
// provides direct access to vertex data of specfied chunk
vertex_array const &
geometry_bank::vertices( gfx::geometry_handle const &Geometry ) const {
@@ -751,6 +769,12 @@ geometrybank_manager::indices( gfx::geometry_handle const &Geometry ) const {
}
// provides direct access to vertex data of specfied chunk
gfx::position_array
geometrybank_manager::positions( gfx::geometry_handle const &Geometry ) const {
return bank( Geometry ).first->positions( Geometry );
}
gfx::vertex_array const &
geometrybank_manager::vertices( gfx::geometry_handle const &Geometry ) const {
+4
View File
@@ -181,6 +181,8 @@ public:
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
@@ -324,6 +326,8 @@ public:
++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
+9
View File
@@ -123,6 +123,15 @@ public:
virtual auto Indices( gfx::geometry_handle const &Geometry ) const->gfx::index_array const & = 0;
// provides direct access to vertex data of specfied chunk
virtual auto Vertices( gfx::geometry_handle const &Geometry ) const ->gfx::vertex_array const & = 0;
// the positions of the vertices of specified chunk, for what needs no more of them (the bounds of a model as it loads): a renderer
// keeping the compact streams of an EU07M model gives them without making and keeping the basic_vertex Vertices() would
virtual auto Positions( gfx::geometry_handle const &Geometry ) const -> gfx::position_array {
auto const &vertices { Vertices( Geometry ) };
gfx::position_array positions;
positions.reserve( vertices.size() );
for( auto const &vertex : vertices ) {
positions.push_back( vertex.position ); }
return positions; }
// provides direct access to vertex user data of specfied chunk
virtual auto UserData( gfx::geometry_handle const &Geometry ) const ->gfx::userdata_array const & = 0;
// kind of geometry of specified chunk, -1 when the renderer doesn't tell
+20 -2
View File
@@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/.
#include "rendering/vulkan14/vulkan14texture.h"
#include <atomic>
#include <format>
namespace vk14
{
@@ -25,8 +26,24 @@ 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)
@@ -150,7 +167,7 @@ bool vulkan14_geometrybank::build()
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 = (false == m_chunks.empty()) && std::ranges::all_of(m_chunks, [](geometry_chunk const &Chunk) { return Chunk.compact(); });
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)
@@ -297,7 +314,8 @@ bool vulkan14_geometrybank::source(gfx::geometry_handle const &Geometry, chunk_s
return false;
}
std::lock_guard<std::mutex> lock(m_mutex);
if (m_layoutdirty)
// 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();
}
+7
View File
@@ -22,6 +22,7 @@ http://mozilla.org/MPL/2.0/.
#include <chrono>
#include <memory>
#include <mutex>
#include <string_view>
namespace vk14
{
@@ -56,6 +57,12 @@ struct chunk_source
// 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:
+5
View File
@@ -320,6 +320,11 @@ std::shared_ptr<vk14::scene_program> make_variant(vk14::shader_library &Shaders,
catch (gfx::shader_exception const &Exception)
{
vk14::log_error(Exception.what());
if (Kind.compact)
{
// without the variant the chunks of the compact layout would be left out for good: their banks go back to basic_vertex
vk14::compact_layout_failed(Name);
}
}
return nullptr;
}
+1
View File
@@ -88,6 +88,7 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk
void Erase(gfx::geometry_handle const &Geometry) override;
gfx::index_array const &Indices(gfx::geometry_handle const &Geometry) const override;
gfx::vertex_array const &Vertices(gfx::geometry_handle const &Geometry) const override;
gfx::position_array Positions(gfx::geometry_handle const &Geometry) const override;
gfx::userdata_array const &UserData(gfx::geometry_handle const &Geometry) const override;
int Primitive(gfx::geometry_handle const &Geometry) const override;
// the sleepers and timbers are drawn by the walk of the scene (gfx::scene_walk), as in the opengl renderer
+5
View File
@@ -1352,6 +1352,11 @@ gfx::vertex_array const &vulkan14_renderer::Vertices(gfx::geometry_handle const
return m_geometry.vertices(Geometry);
}
gfx::position_array vulkan14_renderer::Positions(gfx::geometry_handle const &Geometry) const
{
return m_geometry.positions(Geometry);
}
gfx::userdata_array const &vulkan14_renderer::UserData(gfx::geometry_handle const &Geometry) const
{
return m_geometry.userdata(Geometry);