History

Merge branch 'master' of https://github.com/worklist/hifi

Philip Rosedale committed 12 years ago

Commit 46bd6772 parents aa2ee25 84a5d75184 files · +30499 −2550Browse files

Showing the changes to interface/src/VoxelSystem.cpp (1 of 184 files). Show all

Unified · Split

interface/src/VoxelSystem.cpp+24−41
@@ -5,16 +5,11 @@ · 4 unmodified lines
5// Copyright (c) 2012 High Fidelity, Inc. All rights reserved.5// Copyright (c) 2012 High Fidelity, Inc. All rights reserved.
6//6//
7 7
8#ifdef _WIN32
9#define _timeval_
10#define _USE_MATH_DEFINES
11#endif
12 8
13#include <cstring>9#include <cstring>
14#include <cmath>10#include <cmath>
15#include <iostream> // to load voxels from file11#include <iostream> // to load voxels from file
16#include <fstream> // to load voxels from file12#include <fstream> // to load voxels from file
17#include <pthread.h>
18 13
19#include <OctalCode.h>14#include <OctalCode.h>
20#include <PacketHeaders.h>15#include <PacketHeaders.h>
@@ -73,9 +68,6 @@ VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) · 52 unmodified lines
73 _tree = new VoxelTree();68 _tree = new VoxelTree();
74 69
75 _tree->getRoot()->setVoxelSystem(this);70 _tree->getRoot()->setVoxelSystem(this);
76 pthread_mutex_init(&_bufferWriteLock, NULL);
77 pthread_mutex_init(&_treeLock, NULL);
78 pthread_mutex_init(&_freeIndexLock, NULL);
79 71
80 VoxelTreeElement::addDeleteHook(this);72 VoxelTreeElement::addDeleteHook(this);
81 VoxelTreeElement::addUpdateHook(this);73 VoxelTreeElement::addUpdateHook(this);
@@ -187,10 +179,10 @@ glBufferIndex VoxelSystem::getNextBufferIndex() { · 105 unmodified lines
187 glBufferIndex output = GLBUFFER_INDEX_UNKNOWN;179 glBufferIndex output = GLBUFFER_INDEX_UNKNOWN;
188 // if there's a free index, use it...180 // if there's a free index, use it...
189 if (_freeIndexes.size() > 0) {181 if (_freeIndexes.size() > 0) {
190 pthread_mutex_lock(&_freeIndexLock);182 _freeIndexLock.lock();
191 output = _freeIndexes.back();183 output = _freeIndexes.back();
192 _freeIndexes.pop_back();184 _freeIndexes.pop_back();
193 pthread_mutex_unlock(&_freeIndexLock);185 _freeIndexLock.unlock();
194 } else {186 } else {
195 output = _voxelsInWriteArrays;187 output = _voxelsInWriteArrays;
196 _voxelsInWriteArrays++;188 _voxelsInWriteArrays++;
@@ -211,7 +203,7 @@ void VoxelSystem::freeBufferIndex(glBufferIndex index) { · 14 unmodified lines
211 203
212 // make sure the index isn't already in the free list..., this is a debugging measure only done if you've enabled audits204 // make sure the index isn't already in the free list..., this is a debugging measure only done if you've enabled audits
213 if (Menu::getInstance()->isOptionChecked(MenuOption::AutomaticallyAuditTree)) {205 if (Menu::getInstance()->isOptionChecked(MenuOption::AutomaticallyAuditTree)) {
214 for (long i = 0; i < _freeIndexes.size(); i++) {206 for (unsigned long i = 0; i < _freeIndexes.size(); i++) {
215 if (_freeIndexes[i] == index) {207 if (_freeIndexes[i] == index) {
216 printf("freeBufferIndex(glBufferIndex index)... index=%ld already in free list!", index);208 printf("freeBufferIndex(glBufferIndex index)... index=%ld already in free list!", index);
217 inList = true;209 inList = true;
@@ -221,9 +213,9 @@ void VoxelSystem::freeBufferIndex(glBufferIndex index) { · 3 unmodified lines
221 }213 }
222 if (!inList) {214 if (!inList) {
223 // make the index available for next node that needs to be drawn215 // make the index available for next node that needs to be drawn
224 pthread_mutex_lock(&_freeIndexLock);216 _freeIndexLock.lock();
225 _freeIndexes.push_back(index);217 _freeIndexes.push_back(index);
226 pthread_mutex_unlock(&_freeIndexLock);218 _freeIndexLock.unlock();
227 219
228 // make the VBO slot "invisible" in case this slot is not used220 // make the VBO slot "invisible" in case this slot is not used
229 const glm::vec3 startVertex(FLT_MAX, FLT_MAX, FLT_MAX);221 const glm::vec3 startVertex(FLT_MAX, FLT_MAX, FLT_MAX);
@@ -242,14 +234,14 @@ void VoxelSystem::clearFreeBufferIndexes() { · 12 unmodified lines
242 234
243 // clear out freeIndexes235 // clear out freeIndexes
244 {236 {
245 PerformanceWarning warn(showWarnings,"clearFreeBufferIndexes() : pthread_mutex_lock(&_freeIndexLock)");237 PerformanceWarning warn(showWarnings,"clearFreeBufferIndexes() : _freeIndexLock.lock()");
246 pthread_mutex_lock(&_freeIndexLock);238 _freeIndexLock.lock();
247 }239 }
248 {240 {
249 PerformanceWarning warn(showWarnings,"clearFreeBufferIndexes() : _freeIndexes.clear()");241 PerformanceWarning warn(showWarnings,"clearFreeBufferIndexes() : _freeIndexes.clear()");
250 _freeIndexes.clear();242 _freeIndexes.clear();
251 }243 }
252 pthread_mutex_unlock(&_freeIndexLock);244 _freeIndexLock.unlock();
253}245}
254 246
255VoxelSystem::~VoxelSystem() {247VoxelSystem::~VoxelSystem() {
@@ -258,9 +250,6 @@ VoxelSystem::~VoxelSystem() { · 2 unmodified lines
258 250
259 cleanupVoxelMemory();251 cleanupVoxelMemory();
260 delete _tree;252 delete _tree;
261 pthread_mutex_destroy(&_bufferWriteLock);
262 pthread_mutex_destroy(&_treeLock);
263 pthread_mutex_destroy(&_freeIndexLock);
264}253}
265 254
266void VoxelSystem::setMaxVoxels(int maxVoxels) {255void VoxelSystem::setMaxVoxels(int maxVoxels) {
@@ -344,7 +333,7 @@ void VoxelSystem::setVoxelsAsPoints(bool voxelsAsPoints) { · 77 unmodified lines
344 333
345void VoxelSystem::cleanupVoxelMemory() {334void VoxelSystem::cleanupVoxelMemory() {
346 if (_initialized) {335 if (_initialized) {
347 pthread_mutex_lock(&_bufferWriteLock);336 _bufferWriteLock.lock();
348 _initialized = false; // no longer initialized337 _initialized = false; // no longer initialized
349 if (_useVoxelShader) {338 if (_useVoxelShader) {
350 // these are used when in VoxelShader mode.339 // these are used when in VoxelShader mode.
@@ -382,7 +371,7 @@ void VoxelSystem::cleanupVoxelMemory() { · 31 unmodified lines
382 delete[] _writeVoxelDirtyArray;371 delete[] _writeVoxelDirtyArray;
383 delete[] _readVoxelDirtyArray;372 delete[] _readVoxelDirtyArray;
384 _writeVoxelDirtyArray = _readVoxelDirtyArray = NULL;373 _writeVoxelDirtyArray = _readVoxelDirtyArray = NULL;
385 pthread_mutex_unlock(&_bufferWriteLock);374 _bufferWriteLock.unlock();
386 }375 }
387}376}
388 377
@@ -391,7 +380,7 @@ void VoxelSystem::setupFaceIndices(GLuint& faceVBOID, GLubyte faceIdentityIndice · 2 unmodified lines
391 380
392 // populate the indicesArray381 // populate the indicesArray
393 // this will not change given new voxels, so we can set it all up now382 // this will not change given new voxels, so we can set it all up now
394 for (int n = 0; n < _maxVoxels; n++) {383 for (unsigned long n = 0; n < _maxVoxels; n++) {
395 // fill the indices array384 // fill the indices array
396 int voxelIndexOffset = n * INDICES_PER_FACE;385 int voxelIndexOffset = n * INDICES_PER_FACE;
397 GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;386 GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;
@@ -415,7 +404,7 @@ void VoxelSystem::setupFaceIndices(GLuint& faceVBOID, GLubyte faceIdentityIndice · 17 unmodified lines
415}404}
416 405
417void VoxelSystem::initVoxelMemory() {406void VoxelSystem::initVoxelMemory() {
418 pthread_mutex_lock(&_bufferWriteLock);407 _bufferWriteLock.lock();
419 408
420 _memoryUsageRAM = 0;409 _memoryUsageRAM = 0;
421 _memoryUsageVBO = 0; // our VBO allocations as we know them410 _memoryUsageVBO = 0; // our VBO allocations as we know them
@@ -430,7 +419,7 @@ void VoxelSystem::initVoxelMemory() { · 8 unmodified lines
430 419
431 // populate the indicesArray420 // populate the indicesArray
432 // this will not change given new voxels, so we can set it all up now421 // this will not change given new voxels, so we can set it all up now
433 for (int n = 0; n < _maxVoxels; n++) {422 for (unsigned long n = 0; n < _maxVoxels; n++) {
434 indicesArray[n] = n;423 indicesArray[n] = n;
435 }424 }
436 425
@@ -530,7 +519,7 @@ void VoxelSystem::initVoxelMemory() { · 93 unmodified lines
530 519
531 _initialized = true;520 _initialized = true;
532 521
533 pthread_mutex_unlock(&_bufferWriteLock);522 _bufferWriteLock.unlock();
534}523}
535 524
536void VoxelSystem::writeToSVOFile(const char* filename, VoxelTreeElement* element) const {525void VoxelSystem::writeToSVOFile(const char* filename, VoxelTreeElement* element) const {
@@ -684,7 +673,7 @@ void VoxelSystem::setupNewVoxelsForDrawing() { · 147 unmodified lines
684 }673 }
685 674
686 // lock on the buffer write lock so we can't modify the data when the GPU is reading it675 // lock on the buffer write lock so we can't modify the data when the GPU is reading it
687 pthread_mutex_lock(&_bufferWriteLock);676 _bufferWriteLock.lock();
688 677
689 if (_voxelsUpdated) {678 if (_voxelsUpdated) {
690 _voxelsDirty=true;679 _voxelsDirty=true;
@@ -693,7 +682,7 @@ void VoxelSystem::setupNewVoxelsForDrawing() { · 2 unmodified lines
693 // copy the newly written data to the arrays designated for reading, only does something if _voxelsDirty && _voxelsUpdated682 // copy the newly written data to the arrays designated for reading, only does something if _voxelsDirty && _voxelsUpdated
694 copyWrittenDataToReadArrays(didWriteFullVBO);683 copyWrittenDataToReadArrays(didWriteFullVBO);
695 684
696 pthread_mutex_unlock(&_bufferWriteLock);685 _bufferWriteLock.unlock();
697 686
698 uint64_t end = usecTimestampNow();687 uint64_t end = usecTimestampNow();
699 int elapsedmsec = (end - start) / 1000;688 int elapsedmsec = (end - start) / 1000;
@@ -724,8 +713,8 @@ void VoxelSystem::setupNewVoxelsForDrawingSingleNode(bool allowBailEarly) { · 24 unmodified lines
724 // lock on the buffer write lock so we can't modify the data when the GPU is reading it713 // lock on the buffer write lock so we can't modify the data when the GPU is reading it
725 {714 {
726 PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),715 PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
727 "setupNewVoxelsForDrawingSingleNode()... pthread_mutex_lock(&_bufferWriteLock);");716 "setupNewVoxelsForDrawingSingleNode()... _bufferWriteLock.lock();" );
728 pthread_mutex_lock(&_bufferWriteLock);717 _bufferWriteLock.lock();
729 }718 }
730 719
731 _voxelsDirty = true; // if we got this far, then we can assume some voxels are dirty720 _voxelsDirty = true; // if we got this far, then we can assume some voxels are dirty
@@ -736,7 +725,7 @@ void VoxelSystem::setupNewVoxelsForDrawingSingleNode(bool allowBailEarly) { · 4 unmodified lines
736 // after...725 // after...
737 _voxelsUpdated = 0;726 _voxelsUpdated = 0;
738 727
739 pthread_mutex_unlock(&_bufferWriteLock);728 _bufferWriteLock.unlock();
740 729
741 uint64_t end = usecTimestampNow();730 uint64_t end = usecTimestampNow();
742 int elapsedmsec = (end - start) / 1000;731 int elapsedmsec = (end - start) / 1000;
@@ -1603,13 +1592,13 @@ void VoxelSystem::falseColorizeBySource() { · 860 unmodified lines
1603 };1592 };
1604 1593
1605 // create a bunch of colors we'll use during colorization1594 // create a bunch of colors we'll use during colorization
1606 1595
1607 foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) {1596 foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) {
1608 if (node->getType() == NODE_TYPE_VOXEL_SERVER) {1597 if (node->getType() == NODE_TYPE_VOXEL_SERVER) {
1609 uint16_t nodeID = VoxelTreeElement::getSourceNodeUUIDKey(node->getUUID());1598 uint16_t nodeID = VoxelTreeElement::getSourceNodeUUIDKey(node->getUUID());
1610 int groupColor = voxelServerCount % NUMBER_OF_COLOR_GROUPS;1599 int groupColor = voxelServerCount % NUMBER_OF_COLOR_GROUPS;
1611 args.colors[nodeID] = groupColors[groupColor];1600 args.colors[nodeID] = groupColors[groupColor];
1612 1601
1613 if (groupColors[groupColor].red > 0) {1602 if (groupColors[groupColor].red > 0) {
1614 groupColors[groupColor].red = ((groupColors[groupColor].red - MIN_COLOR)/2) + MIN_COLOR;1603 groupColors[groupColor].red = ((groupColors[groupColor].red - MIN_COLOR)/2) + MIN_COLOR;
1615 }1604 }
@@ -1619,7 +1608,7 @@ void VoxelSystem::falseColorizeBySource() { · 3 unmodified lines
1619 if (groupColors[groupColor].blue > 0) {1608 if (groupColors[groupColor].blue > 0) {
1620 groupColors[groupColor].blue = ((groupColors[groupColor].blue - MIN_COLOR)/2) + MIN_COLOR;1609 groupColors[groupColor].blue = ((groupColors[groupColor].blue - MIN_COLOR)/2) + MIN_COLOR;
1621 }1610 }
1622 1611
1623 voxelServerCount++;1612 voxelServerCount++;
1624 }1613 }
1625 }1614 }
@@ -2397,12 +2386,6 @@ void VoxelSystem::createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, · 771 unmodified lines
2397 setupNewVoxelsForDrawing();2386 setupNewVoxelsForDrawing();
2398};2387};
2399 2388
2400void VoxelSystem::createSphere(float r,float xc, float yc, float zc, float s, bool solid,
2401 creationMode mode, bool destructive, bool debug) {
2402 _tree->createSphere(r, xc, yc, zc, s, solid, mode, destructive, debug);
2403 setupNewVoxelsForDrawing();
2404};
2405
2406void VoxelSystem::copySubTreeIntoNewTree(VoxelTreeElement* startNode, VoxelSystem* destination, bool rebaseToRoot) {2389void VoxelSystem::copySubTreeIntoNewTree(VoxelTreeElement* startNode, VoxelSystem* destination, bool rebaseToRoot) {
2407 _tree->copySubTreeIntoNewTree(startNode, destination->_tree, rebaseToRoot);2390 _tree->copySubTreeIntoNewTree(startNode, destination->_tree, rebaseToRoot);
2408 destination->setupNewVoxelsForDrawing();2391 destination->setupNewVoxelsForDrawing();
@@ -2765,13 +2748,13 @@ unsigned long VoxelSystem::getVoxelMemoryUsageGPU() { · 356 unmodified lines
2765}2748}
2766 2749
2767void VoxelSystem::lockTree() {2750void VoxelSystem::lockTree() {
2768 pthread_mutex_lock(&_treeLock);2751 _treeLock.lock();
2769 _treeIsBusy = true;2752 _treeIsBusy = true;
2770}2753}
2771 2754
2772void VoxelSystem::unlockTree() {2755void VoxelSystem::unlockTree() {
2773 _treeIsBusy = false;2756 _treeIsBusy = false;
2774 pthread_mutex_unlock(&_treeLock);2757 _treeLock.unlock();
2775}2758}
2776 2759
2777 2760