Skip to content

Commit

Permalink
[#182] : Various memory usage fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ange-yaghi committed Sep 3, 2019
1 parent 3a8206e commit a2c0ca4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion demos/sdl/the_gadget.mr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ private import "mantaray.mr"
private import "utilities.mr"

auto node settings {
output samples: 31700;
output samples: 100;
output resolution_x: 1900;
output resolution_y: 1268;
output output_dir: "../../workspace/render/bitmap/";
Expand Down
2 changes: 1 addition & 1 deletion include/kd_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace manta {
};

KDBoundEdge() {

/* void */
}

KDBoundEdge(math::real t, int primitiveIndex, bool start) : t(t), primitiveIndex(primitiveIndex) {
Expand Down
21 changes: 14 additions & 7 deletions src/kd_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void manta::KDTree::destroy() {
}

if (m_nodes != nullptr) {
StandardAllocator::Global()->aligned_free(m_nodes, m_nodeCapacity);
StandardAllocator::Global()->free(m_nodes, m_nodeCapacity);
m_nodes = nullptr;
m_nodeCapacity = 0;
m_nodeCount = 0;
Expand Down Expand Up @@ -288,7 +288,7 @@ void manta::KDTree::_analyze(int currentNode, AABB *nodeBounds, const std::vecto
createNode();

// Debug only
m_nodeBounds.push_back(*nodeBounds);
//m_nodeBounds.push_back(*nodeBounds);

int primitiveCount = (int)faces.size();
if (primitiveCount <= workspace->maxPrimitives || depth == 0) {
Expand Down Expand Up @@ -400,14 +400,14 @@ int manta::KDTree::createNode() {
int newSize = 1;
if (m_nodeCapacity > 0) newSize = m_nodeCapacity * 2;

KDTreeNode *newNodes = StandardAllocator::Global()->allocate<KDTreeNode>(newSize, 16);
KDTreeNode *newNodes = StandardAllocator::Global()->allocate<KDTreeNode>(newSize);
if (m_nodeCount > 0) {
// Copy old nodes
memcpy((void *)newNodes, (void *)m_nodes, sizeof(KDTreeNode) * m_nodeCount);
}

if (m_nodeCapacity > 0) {
StandardAllocator::Global()->aligned_free(m_nodes, m_nodeCapacity);
StandardAllocator::Global()->free(m_nodes, m_nodeCapacity);
}

m_nodes = newNodes;
Expand All @@ -433,6 +433,8 @@ int manta::KDTree::createNodeVolume() {
StandardAllocator::Global()->aligned_free(m_nodeVolumes, m_volumeCapacity);
}

std::cout << "Growing volumes from " << m_volumeCapacity << " to " << newSize << " (total size: " << sizeof(KDBoundingVolume) * newSize << ")" << std::endl;

m_nodeVolumes = newVolumes;
m_volumeCapacity = newSize;
}
Expand All @@ -446,16 +448,19 @@ void manta::KDTree::initLeaf(int node, const std::vector<int> &faces, const AABB
int newNodeVolume = createNodeVolume();

constexpr bool ENABLE_FILTERING = true;
std::vector<bool> filtered;

bool *filtered = (primitiveCount > 0)
? new bool[primitiveCount]
: nullptr;

// Filter faces
if (primitiveCount > 0) {
for (int i = 0; i < primitiveCount; i++) {
if (ENABLE_FILTERING && !m_mesh->checkFaceAABB(faces[i], bounds)) {
filtered.push_back(true);
filtered[i] = true;
}
else {
filtered.push_back(false);
filtered[i] = false;
filteredPrimitiveCount++;
}
}
Expand Down Expand Up @@ -483,6 +488,8 @@ void manta::KDTree::initLeaf(int node, const std::vector<int> &faces, const AABB
}
}
}

if (filtered != nullptr) delete[] filtered;
}

void manta::KDTree::writeToObjFile(const char *fname) const {
Expand Down

0 comments on commit a2c0ca4

Please sign in to comment.