From 2742fef458c3626476193c9e2d1b9231e042e420 Mon Sep 17 00:00:00 2001 From: paradust7 <102263465+paradust7@users.noreply.github.com> Date: Sat, 21 May 2022 15:11:49 -0700 Subject: [PATCH] Fixes needed to use irrArray backed by std::vector (#12263) --- src/client/clientmap.cpp | 39 ++++++++++++++--------------- src/client/clientmap.h | 3 +++ src/client/mapblock_mesh.cpp | 21 ++++++++-------- src/client/mapblock_mesh.h | 17 ++++++++++--- src/irrlicht_changes/CGUITTFont.cpp | 14 ++++------- src/irrlicht_changes/CGUITTFont.h | 38 ++++++++++++++++++---------- 6 files changed, 77 insertions(+), 55 deletions(-) diff --git a/src/client/clientmap.cpp b/src/client/clientmap.cpp index 46cb115aa..98e3f40d3 100644 --- a/src/client/clientmap.cpp +++ b/src/client/clientmap.cpp @@ -449,15 +449,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) drawcall_count += draw_order.size(); for (auto &descriptor : draw_order) { - scene::IMeshBuffer *buf; - - if (descriptor.m_use_partial_buffer) { - descriptor.m_partial_buffer->beforeDraw(); - buf = descriptor.m_partial_buffer->getBuffer(); - } - else { - buf = descriptor.m_buffer; - } + scene::IMeshBuffer *buf = descriptor.getBuffer(); // Check and abort if the machine is swapping a lot if (draw.getTimerTime() > 2000) { @@ -501,7 +493,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) m.setTranslation(block_wpos - offset); driver->setTransform(video::ETS_WORLD, m); - driver->drawMeshBuffer(buf); + descriptor.draw(driver); vertex_count += buf->getIndexCount(); } @@ -812,15 +804,7 @@ void ClientMap::renderMapShadows(video::IVideoDriver *driver, drawcall_count += draw_order.size(); for (auto &descriptor : draw_order) { - scene::IMeshBuffer *buf; - - if (descriptor.m_use_partial_buffer) { - descriptor.m_partial_buffer->beforeDraw(); - buf = descriptor.m_partial_buffer->getBuffer(); - } - else { - buf = descriptor.m_buffer; - } + scene::IMeshBuffer *buf = descriptor.getBuffer(); // Check and abort if the machine is swapping a lot if (draw.getTimerTime() > 1000) { @@ -845,7 +829,7 @@ void ClientMap::renderMapShadows(video::IVideoDriver *driver, m.setTranslation(block_wpos - offset); driver->setTransform(video::ETS_WORLD, m); - driver->drawMeshBuffer(buf); + descriptor.draw(driver); vertex_count += buf->getIndexCount(); } @@ -966,3 +950,18 @@ void ClientMap::updateTransparentMeshBuffers() m_needs_update_transparent_meshes = false; } +scene::IMeshBuffer* ClientMap::DrawDescriptor::getBuffer() +{ + return m_use_partial_buffer ? m_partial_buffer->getBuffer() : m_buffer; +} + +void ClientMap::DrawDescriptor::draw(video::IVideoDriver* driver) +{ + if (m_use_partial_buffer) { + m_partial_buffer->beforeDraw(); + driver->drawMeshBuffer(m_partial_buffer->getBuffer()); + m_partial_buffer->afterDraw(); + } else { + driver->drawMeshBuffer(m_buffer); + } +} diff --git a/src/client/clientmap.h b/src/client/clientmap.h index 6d57f1911..823870c68 100644 --- a/src/client/clientmap.h +++ b/src/client/clientmap.h @@ -174,6 +174,9 @@ private: DrawDescriptor(v3s16 pos, const PartialMeshBuffer *buffer) : m_pos(pos), m_partial_buffer(buffer), m_reuse_material(false), m_use_partial_buffer(true) {} + + scene::IMeshBuffer* getBuffer(); + void draw(video::IVideoDriver* driver); }; Client *m_client; diff --git a/src/client/mapblock_mesh.cpp b/src/client/mapblock_mesh.cpp index 868573bf0..965dd5e29 100644 --- a/src/client/mapblock_mesh.cpp +++ b/src/client/mapblock_mesh.cpp @@ -1162,15 +1162,16 @@ void MapBlockBspTree::traverse(s32 node, v3f viewpoint, std::vector &output void PartialMeshBuffer::beforeDraw() const { // Patch the indexes in the mesh buffer before draw - - m_buffer->Indices.clear(); - if (!m_vertex_indexes.empty()) { - for (auto index : m_vertex_indexes) - m_buffer->Indices.push_back(index); - } + m_buffer->Indices = std::move(m_vertex_indexes); m_buffer->setDirty(scene::EBT_INDEX); } +void PartialMeshBuffer::afterDraw() const +{ + // Take the data back + m_vertex_indexes = std::move(m_buffer->Indices.steal()); +} + /* MapBlockMesh */ @@ -1514,7 +1515,7 @@ void MapBlockMesh::updateTransparentBuffers(v3f camera_pos, v3s16 block_pos) const auto &t = m_transparent_triangles[i]; if (current_buffer != t.buffer) { if (current_buffer) { - m_transparent_buffers.emplace_back(current_buffer, current_strain); + m_transparent_buffers.emplace_back(current_buffer, std::move(current_strain)); current_strain.clear(); } current_buffer = t.buffer; @@ -1525,7 +1526,7 @@ void MapBlockMesh::updateTransparentBuffers(v3f camera_pos, v3s16 block_pos) } if (!current_strain.empty()) - m_transparent_buffers.emplace_back(current_buffer, current_strain); + m_transparent_buffers.emplace_back(current_buffer, std::move(current_strain)); } void MapBlockMesh::consolidateTransparentBuffers() @@ -1539,7 +1540,7 @@ void MapBlockMesh::consolidateTransparentBuffers() for (const auto &t : m_transparent_triangles) { if (current_buffer != t.buffer) { if (current_buffer != nullptr) { - this->m_transparent_buffers.emplace_back(current_buffer, current_strain); + this->m_transparent_buffers.emplace_back(current_buffer, std::move(current_strain)); current_strain.clear(); } current_buffer = t.buffer; @@ -1550,7 +1551,7 @@ void MapBlockMesh::consolidateTransparentBuffers() } if (!current_strain.empty()) { - this->m_transparent_buffers.emplace_back(current_buffer, current_strain); + this->m_transparent_buffers.emplace_back(current_buffer, std::move(current_strain)); } } diff --git a/src/client/mapblock_mesh.h b/src/client/mapblock_mesh.h index 72d128038..169b3a8c1 100644 --- a/src/client/mapblock_mesh.h +++ b/src/client/mapblock_mesh.h @@ -140,20 +140,31 @@ private: s32 root = -1; // index of the root node }; +/* + * PartialMeshBuffer + * + * Attach alternate `Indices` to an existing mesh buffer, to make it possible to use different + * indices with the same vertex buffer. + * + * Irrlicht does not currently support this: `CMeshBuffer` ties together a single vertex buffer + * and a single index buffer. There's no way to share these between mesh buffers. + * + */ class PartialMeshBuffer { public: - PartialMeshBuffer(scene::SMeshBuffer *buffer, const std::vector &vertex_indexes) : - m_buffer(buffer), m_vertex_indexes(vertex_indexes) + PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector &&vertex_indexes) : + m_buffer(buffer), m_vertex_indexes(std::move(vertex_indexes)) {} scene::IMeshBuffer *getBuffer() const { return m_buffer; } const std::vector &getVertexIndexes() const { return m_vertex_indexes; } void beforeDraw() const; + void afterDraw() const; private: scene::SMeshBuffer *m_buffer; - std::vector m_vertex_indexes; + mutable std::vector m_vertex_indexes; }; /* diff --git a/src/irrlicht_changes/CGUITTFont.cpp b/src/irrlicht_changes/CGUITTFont.cpp index fe86adae6..0f2572ee0 100644 --- a/src/irrlicht_changes/CGUITTFont.cpp +++ b/src/irrlicht_changes/CGUITTFont.cpp @@ -292,9 +292,6 @@ shadow_offset(0), shadow_alpha(0), fallback(0) Driver->grab(); setInvisibleCharacters(L" "); - - // Glyphs aren't reference counted, so don't try to delete them when we free the array. - Glyphs.set_free_when_destroyed(false); } bool CGUITTFont::load(const io::path& filename, const u32 size, const bool antialias, const bool transparency) @@ -411,8 +408,7 @@ CGUITTFont::~CGUITTFont() { // Delete the glyphs and glyph pages. reset_images(); - CGUITTAssistDelete::Delete(Glyphs); - //Glyphs.clear(); + Glyphs.clear(); // We aren't using this face anymore. auto n = c_faces.find(filename); @@ -675,6 +671,8 @@ void CGUITTFont::draw(const EnrichedString &text, const core::rect& positio update_glyph_pages(); auto it = Render_Map.begin(); auto ie = Render_Map.end(); + core::array tmp_positions; + core::array tmp_source_rects; while (it != ie) { CGUITTGlyphPage* page = it->second; @@ -696,10 +694,8 @@ void CGUITTFont::draw(const EnrichedString &text, const core::rect& positio do ++i; while (i < page->render_positions.size() && page->render_colors[i] == colprev); - core::array tmp_positions; - core::array tmp_source_rects; - tmp_positions.set_pointer(&page->render_positions[ibegin], i - ibegin, false, false); // no copy - tmp_source_rects.set_pointer(&page->render_source_rects[ibegin], i - ibegin, false, false); + tmp_positions.set_data(&page->render_positions[ibegin], i - ibegin); + tmp_source_rects.set_data(&page->render_source_rects[ibegin], i - ibegin); --i; if (!use_transparency) diff --git a/src/irrlicht_changes/CGUITTFont.h b/src/irrlicht_changes/CGUITTFont.h index 9457e5b18..210222ed4 100644 --- a/src/irrlicht_changes/CGUITTFont.h +++ b/src/irrlicht_changes/CGUITTFont.h @@ -37,6 +37,7 @@ #include #include #include "util/enriched_string.h" +#include "util/basic_macros.h" #include FT_FREETYPE_H namespace irr @@ -46,23 +47,34 @@ namespace gui struct SGUITTFace; class CGUITTFont; - //! Class to assist in deleting glyphs. - class CGUITTAssistDelete - { - public: - template - static void Delete(core::array& a) - { - TAlloc allocator; - allocator.deallocate(a.pointer()); - } - }; - //! Structure representing a single TrueType glyph. struct SGUITTGlyph { //! Constructor. - SGUITTGlyph() : isLoaded(false), glyph_page(0), surface(0), parent(0) {} + SGUITTGlyph() : + isLoaded(false), + glyph_page(0), + source_rect(), + offset(), + advance(), + surface(0), + parent(0) + {} + + DISABLE_CLASS_COPY(SGUITTGlyph); + + //! This class would be trivially copyable except for the reference count on `surface`. + SGUITTGlyph(SGUITTGlyph &&other) : + isLoaded(other.isLoaded), + glyph_page(other.glyph_page), + source_rect(other.source_rect), + offset(other.offset), + advance(other.advance), + surface(other.surface), + parent(other.parent) + { + other.surface = 0; + } //! Destructor. ~SGUITTGlyph() { unload(); }