From d097fa798211cad4773041bc7617a7f72331e1ae Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 16 Nov 2016 14:10:50 -0800 Subject: [PATCH 1/3] 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; From a14ff22df35e2d7b3568324224c4d37abb01fe4c Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 16 Nov 2016 15:10:43 -0800 Subject: [PATCH 2/3] update render-land bounds when polyvox moves --- .../src/RenderablePolyVoxEntityItem.cpp | 29 ++++++++++++++----- .../src/RenderablePolyVoxEntityItem.h | 3 ++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp index 162f1a80b6..e8b0378899 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp @@ -1323,14 +1323,14 @@ void RenderablePolyVoxEntityItem::setCollisionPoints(ShapeInfo::PointCollection // include the registrationPoint in the shape key, because the offset is already // included in the points and the shapeManager wont know that the shape has changed. withWriteLock([&] { - QString shapeKey = QString(_voxelData.toBase64()) + "," + - QString::number(_registrationPoint.x) + "," + - QString::number(_registrationPoint.y) + "," + - QString::number(_registrationPoint.z); - _shapeInfo.setParams(SHAPE_TYPE_COMPOUND, collisionModelDimensions, shapeKey); - _shapeInfo.setPointCollection(pointCollection); - _meshDirty = false; - }); + QString shapeKey = QString(_voxelData.toBase64()) + "," + + QString::number(_registrationPoint.x) + "," + + QString::number(_registrationPoint.y) + "," + + QString::number(_registrationPoint.z); + _shapeInfo.setParams(SHAPE_TYPE_COMPOUND, collisionModelDimensions, shapeKey); + _shapeInfo.setPointCollection(pointCollection); + _meshDirty = false; + }); } void RenderablePolyVoxEntityItem::setXNNeighborID(const EntityItemID& xNNeighborID) { @@ -1439,3 +1439,16 @@ void RenderablePolyVoxEntityItem::bonkNeighbors() { currentZNNeighbor->setVolDataDirty(); } } + + +void RenderablePolyVoxEntityItem::locationChanged(bool tellPhysics) { + EntityItem::locationChanged(tellPhysics); + if (!render::Item::isValidID(_myItem)) { + return; + } + render::ScenePointer scene = AbstractViewStateInterface::instance()->getMain3DScene(); + render::PendingChanges pendingChanges; + pendingChanges.updateItem(_myItem, [](PolyVoxPayload& payload) {}); + + scene->enqueuePendingChanges(pendingChanges); +} diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h index f84637ec95..1b6ea34bda 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h @@ -141,6 +141,9 @@ public: // Transparent polyvox didn't seem to be working so disable for now bool isTransparent() override { return false; } +protected: + virtual void locationChanged(bool tellPhysics = true) override; + private: // The PolyVoxEntityItem class has _voxelData which contains dimensions and compressed voxel data. The dimensions // may not match _voxelVolumeSize. From 697369dece77a4bb6b6227628cb34ff10ec00711 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 16 Nov 2016 16:11:17 -0800 Subject: [PATCH 3/3] avoid crash on startup --- libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp index e8b0378899..7defe347ca 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp @@ -1443,7 +1443,7 @@ void RenderablePolyVoxEntityItem::bonkNeighbors() { void RenderablePolyVoxEntityItem::locationChanged(bool tellPhysics) { EntityItem::locationChanged(tellPhysics); - if (!render::Item::isValidID(_myItem)) { + if (!_pipeline || !render::Item::isValidID(_myItem)) { return; } render::ScenePointer scene = AbstractViewStateInterface::instance()->getMain3DScene();