]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Fixes needed to use irrArray backed by std::vector (#12263)
authorparadust7 <102263465+paradust7@users.noreply.github.com>
Sat, 21 May 2022 22:11:49 +0000 (15:11 -0700)
committerGitHub <noreply@github.com>
Sat, 21 May 2022 22:11:49 +0000 (00:11 +0200)
src/client/clientmap.cpp
src/client/clientmap.h
src/client/mapblock_mesh.cpp
src/client/mapblock_mesh.h
src/irrlicht_changes/CGUITTFont.cpp
src/irrlicht_changes/CGUITTFont.h

index 46cb115aae398ed25b0cda8486d6392268de6063..98e3f40d3acb56fbe5485c6073e3e9b15ee05c69 100644 (file)
@@ -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);
+       }
+}
index 6d57f1911571c1471db23e775ce641df4357e83c..823870c6860d699a3623a0b45c1f7d4de0851a67 100644 (file)
@@ -174,6 +174,9 @@ class ClientMap : public Map, public scene::ISceneNode
                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;
index 868573bf07c225ef44bf7170fc42f47bd26f34bf..965dd5e2954af2f9adf0162dde57240a20ef9d8e 100644 (file)
@@ -1162,15 +1162,16 @@ void MapBlockBspTree::traverse(s32 node, v3f viewpoint, std::vector<s32> &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));
        }
 }
 
index 72d12803825701a4b613a80453999950b6d4c12d..169b3a8c12ec2328babf63f177b70602fdbc129b 100644 (file)
@@ -140,20 +140,31 @@ class MapBlockBspTree
        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<u16> &vertex_indexes) :
-                       m_buffer(buffer), m_vertex_indexes(vertex_indexes)
+       PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector<u16> &&vertex_indexes) :
+                       m_buffer(buffer), m_vertex_indexes(std::move(vertex_indexes))
        {}
 
        scene::IMeshBuffer *getBuffer() const { return m_buffer; }
        const std::vector<u16> &getVertexIndexes() const { return m_vertex_indexes; }
 
        void beforeDraw() const;
+       void afterDraw() const;
 private:
        scene::SMeshBuffer *m_buffer;
-       std::vector<u16> m_vertex_indexes;
+       mutable std::vector<u16> m_vertex_indexes;
 };
 
 /*
index fe86adae67fa4929beb838ad813ae3dfdd6c2b63..0f2572ee016923bb2424a3ab5048eb4bb7980dc8 100644 (file)
@@ -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<s32>& positio
        update_glyph_pages();
        auto it = Render_Map.begin();
        auto ie = Render_Map.end();
+       core::array<core::vector2di> tmp_positions;
+       core::array<core::recti> tmp_source_rects;
        while (it != ie)
        {
                CGUITTGlyphPage* page = it->second;
@@ -696,10 +694,8 @@ void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& positio
                        do
                                ++i;
                        while (i < page->render_positions.size() && page->render_colors[i] == colprev);
-                       core::array<core::vector2di> tmp_positions;
-                       core::array<core::recti> 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)
index 9457e5b182fa5ed27789970c533700a05578403e..210222ed4d21a036885ba4017e8e623d5d3d57ef 100644 (file)
@@ -37,6 +37,7 @@
 #include <map>
 #include <irrUString.h>
 #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 <class T, typename TAlloc>
-                       static void Delete(core::array<T, TAlloc>& 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(); }