From d097fa798211cad4773041bc7617a7f72331e1ae Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 16 Nov 2016 14:10:50 -0800 Subject: [PATCH] fix polyvox memory leak --- .../src/RenderablePolyVoxEntityItem.cpp | 12 ++++++------ libraries/gpu/src/gpu/Buffer.cpp | 12 ++++++++++-- libraries/gpu/src/gpu/Buffer.h | 2 ++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp index d1dd5cce8e..162f1a80b6 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp @@ -1139,8 +1139,8 @@ void RenderablePolyVoxEntityItem::getMesh() { auto indexBuffer = std::make_shared(vecIndices.size() * sizeof(uint32_t), (gpu::Byte*)vecIndices.data()); auto indexBufferPtr = gpu::BufferPointer(indexBuffer); - auto indexBufferView = new gpu::BufferView(indexBufferPtr, gpu::Element(gpu::SCALAR, gpu::UINT32, gpu::RAW)); - mesh->setIndexBuffer(*indexBufferView); + gpu::BufferView indexBufferView(indexBufferPtr, gpu::Element(gpu::SCALAR, gpu::UINT32, gpu::RAW)); + mesh->setIndexBuffer(indexBufferView); const std::vector& vecVertices = polyVoxMesh.getVertices(); auto vertexBuffer = std::make_shared(vecVertices.size() * sizeof(PolyVox::PositionMaterialNormal), @@ -1150,10 +1150,10 @@ void RenderablePolyVoxEntityItem::getMesh() { if (vertexBufferPtr->getSize() > sizeof(float) * 3) { vertexBufferSize = vertexBufferPtr->getSize() - sizeof(float) * 3; } - auto vertexBufferView = new gpu::BufferView(vertexBufferPtr, 0, vertexBufferSize, - sizeof(PolyVox::PositionMaterialNormal), - gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::RAW)); - mesh->setVertexBuffer(*vertexBufferView); + gpu::BufferView vertexBufferView(vertexBufferPtr, 0, vertexBufferSize, + sizeof(PolyVox::PositionMaterialNormal), + gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::RAW)); + mesh->setVertexBuffer(vertexBufferView); mesh->addAttribute(gpu::Stream::NORMAL, gpu::BufferView(vertexBufferPtr, sizeof(float) * 3, diff --git a/libraries/gpu/src/gpu/Buffer.cpp b/libraries/gpu/src/gpu/Buffer.cpp index ff425d1f6b..54533b3263 100644 --- a/libraries/gpu/src/gpu/Buffer.cpp +++ b/libraries/gpu/src/gpu/Buffer.cpp @@ -25,6 +25,14 @@ void Buffer::updateBufferCPUMemoryUsage(Size prevObjectSize, Size newObjectSize) } } +void Buffer::incrementBufferCPUCount() { + _bufferCPUCount++; +} + +void Buffer::decrementBufferCPUCount() { + _bufferCPUCount--; +} + uint32_t Buffer::getBufferCPUCount() { return _bufferCPUCount.load(); } @@ -43,7 +51,7 @@ Buffer::Size Buffer::getBufferGPUMemoryUsage() { Buffer::Buffer(Size pageSize) : _renderPages(pageSize), _pages(pageSize) { - _bufferCPUCount++; + Buffer::incrementBufferCPUCount(); } Buffer::Buffer(Size size, const Byte* bytes, Size pageSize) : Buffer(pageSize) { @@ -61,7 +69,7 @@ Buffer& Buffer::operator=(const Buffer& buf) { } Buffer::~Buffer() { - _bufferCPUCount--; + Buffer::decrementBufferCPUCount(); Buffer::updateBufferCPUMemoryUsage(_sysmem.getSize(), 0); } diff --git a/libraries/gpu/src/gpu/Buffer.h b/libraries/gpu/src/gpu/Buffer.h index 981424de2f..9e85868a01 100644 --- a/libraries/gpu/src/gpu/Buffer.h +++ b/libraries/gpu/src/gpu/Buffer.h @@ -27,6 +27,8 @@ class Buffer : public Resource { static std::atomic _bufferCPUCount; static std::atomic _bufferCPUMemoryUsage; static void updateBufferCPUMemoryUsage(Size prevObjectSize, Size newObjectSize); + static void incrementBufferCPUCount(); + static void decrementBufferCPUCount(); public: using Flag = PageManager::Flag;