From c9809d8d827c68ff956adfb9d9aaee4396ebc217 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 1 Feb 2020 15:59:38 -0400 Subject: [PATCH] Clean up some Mesh code --- src/modules/graphics/Buffer.h | 20 ++++---------------- src/modules/graphics/Graphics.cpp | 2 +- src/modules/graphics/Mesh.cpp | 22 +++++----------------- src/modules/graphics/Mesh.h | 2 +- 4 files changed, 11 insertions(+), 35 deletions(-) diff --git a/src/modules/graphics/Buffer.h b/src/modules/graphics/Buffer.h index 12fc2659a..75e999dda 100644 --- a/src/modules/graphics/Buffer.h +++ b/src/modules/graphics/Buffer.h @@ -158,23 +158,11 @@ public: { public: - Mapper(Buffer &buffer) - : buf(buffer) - { - elems = buf.map(); - } + Mapper(Buffer &buffer) : buffer(buffer) { data = buffer.map(); } + ~Mapper() { buffer.unmap(); } - ~Mapper() - { - buf.unmap(); - } - - void *get() { return elems; } - - private: - - Buffer &buf; - void *elems; + Buffer &buffer; + void *data; }; // Mapper diff --git a/src/modules/graphics/Graphics.cpp b/src/modules/graphics/Graphics.cpp index d7e754aee..4369b2bdf 100644 --- a/src/modules/graphics/Graphics.cpp +++ b/src/modules/graphics/Graphics.cpp @@ -176,7 +176,7 @@ void Graphics::createQuadIndexBuffer() quadIndexBuffer = newIndexBuffer(INDEX_UINT16, nullptr, size, BUFFERUSAGE_STATIC, 0); Buffer::Mapper map(*quadIndexBuffer); - fillIndices(TRIANGLEINDEX_QUADS, 0, LOVE_UINT16_MAX, (uint16 *) map.get()); + fillIndices(TRIANGLEINDEX_QUADS, 0, LOVE_UINT16_MAX, (uint16 *) map.data); } Quad *Graphics::newQuad(Quad::Viewport v, double sw, double sh) diff --git a/src/modules/graphics/Mesh.cpp b/src/modules/graphics/Mesh.cpp index ef186137e..4396842d1 100644 --- a/src/modules/graphics/Mesh.cpp +++ b/src/modules/graphics/Mesh.cpp @@ -118,12 +118,6 @@ Mesh::Mesh(graphics::Graphics *gfx, const std::vector & Mesh::~Mesh() { delete vertexScratchBuffer; - - for (const auto &attrib : attachedAttributes) - { - if (attrib.second.buffer != nullptr && attrib.second.buffer != vertexBuffer.get()) - attrib.second.buffer->release(); - } } void Mesh::setupAttachedAttributes() @@ -282,28 +276,22 @@ void Mesh::attachAttribute(const std::string &name, Buffer *buffer, const std::s throw love::Exception("A maximum of %d attributes can be attached at once.", VertexAttributes::MAX); newattrib.buffer = buffer; - newattrib.enabled = oldattrib.buffer ? oldattrib.enabled : true; + newattrib.enabled = oldattrib.buffer.get() ? oldattrib.enabled : true; newattrib.index = buffer->getDataMemberIndex(attachname); newattrib.step = step; if (newattrib.index < 0) throw love::Exception("The specified vertex buffer does not have a vertex attribute named '%s'", attachname.c_str()); - newattrib.buffer->retain(); - attachedAttributes[name] = newattrib; - - if (oldattrib.buffer) - oldattrib.buffer->release(); } bool Mesh::detachAttribute(const std::string &name) { auto it = attachedAttributes.find(name); - if (it != attachedAttributes.end() && it->second.buffer != vertexBuffer.get()) + if (it != attachedAttributes.end()) { - it->second.buffer->release(); attachedAttributes.erase(it); if (getAttributeIndex(name) != -1) @@ -340,7 +328,7 @@ void Mesh::flush() template static void copyToIndexBuffer(const std::vector &indices, Buffer::Mapper &buffermap, size_t maxval) { - T *elems = (T *) buffermap.get(); + T *elems = (T *) buffermap.data; for (size_t i = 0; i < indices.size(); i++) { @@ -405,7 +393,7 @@ void Mesh::setVertexMap(IndexDataType datatype, const void *data, size_t datasiz return; Buffer::Mapper ibomap(*indexBuffer); - memcpy(ibomap.get(), data, datasize); + memcpy(ibomap.data, data, datasize); useIndexBuffer = true; indexDataType = datatype; @@ -541,7 +529,7 @@ void Mesh::drawInstanced(Graphics *gfx, const Matrix4 &m, int instancecount) if (!attrib.second.enabled) continue; - Buffer *buffer = attrib.second.buffer; + Buffer *buffer = attrib.second.buffer.get(); int attributeindex = -1; // If the attribute is one of the LOVE-defined ones, use the constant diff --git a/src/modules/graphics/Mesh.h b/src/modules/graphics/Mesh.h index 160dcf66e..7e099f3dc 100644 --- a/src/modules/graphics/Mesh.h +++ b/src/modules/graphics/Mesh.h @@ -174,7 +174,7 @@ private: struct AttachedAttribute { - Buffer *buffer; + StrongRef buffer; int index; AttributeStep step; bool enabled;