if (!_perlinModulateProgram.isLinked()) { switchToResourcesParentIfRequired(); _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert"); _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/perlin_modulate.frag"); _perlinModulateProgram.link(); _perlinModulateProgram.bind(); _perlinModulateProgram.setUniformValue("permutationNormalTexture", 0); _perlinModulateProgram.release(); _shadowMapProgram.addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/shadow_map.frag"); _shadowMapProgram.link(); _shadowMapProgram.bind(); _shadowMapProgram.setUniformValue("shadowMap", 0); _shadowMapProgram.release(); } } _initialized = true; _bufferWriteLock.unlock();} void VoxelSystem::writeToSVOFile(const char* filename, VoxelTreeElement* element) const { _tree->writeToSVOFile(filename, element);} bool VoxelSystem::readFromSVOFile(const char* filename) { bool result = _tree->readFromSVOFile(filename); if (result) { setupNewVoxelsForDrawing(); } return result;} bool VoxelSystem::readFromSquareARGB32Pixels(const char *filename) { bool result = _tree->readFromSquareARGB32Pixels(filename); if (result) { setupNewVoxelsForDrawing(); } return result;} bool VoxelSystem::readFromSchematicFile(const char* filename) { bool result = _tree->readFromSchematicFile(filename); if (result) { setupNewVoxelsForDrawing(); } return result;} int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) { bool showTimingDetails = Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings); PerformanceWarning warn(showTimingDetails, "VoxelSystem::parseData()",showTimingDetails); unsigned char command = *sourceBuffer; int numBytesPacketHeader = numBytesForPacketHeader(sourceBuffer); switch(command) { case PACKET_TYPE_VOXEL_DATA: { PerformanceWarning warn(showTimingDetails, "VoxelSystem::parseData() PACKET_TYPE_VOXEL_DATA part...",showTimingDetails); unsigned char* dataAt = sourceBuffer + numBytesPacketHeader; VOXEL_PACKET_FLAGS flags = (*(VOXEL_PACKET_FLAGS*)(dataAt)); dataAt += sizeof(VOXEL_PACKET_FLAGS); VOXEL_PACKET_SEQUENCE sequence = (*(VOXEL_PACKET_SEQUENCE*)dataAt); dataAt += sizeof(VOXEL_PACKET_SEQUENCE); VOXEL_PACKET_SENT_TIME sentAt = (*(VOXEL_PACKET_SENT_TIME*)dataAt); dataAt += sizeof(VOXEL_PACKET_SENT_TIME); bool packetIsColored = oneAtBit(flags, PACKET_IS_COLOR_BIT); bool packetIsCompressed = oneAtBit(flags, PACKET_IS_COMPRESSED_BIT); VOXEL_PACKET_SENT_TIME arrivedAt = usecTimestampNow(); int flightTime = arrivedAt - sentAt; VOXEL_PACKET_INTERNAL_SECTION_SIZE sectionLength = 0; int dataBytes = numBytes - VOXEL_PACKET_HEADER_SIZE; int subsection = 1; while (dataBytes > 0) { if (packetIsCompressed) { if (dataBytes > sizeof(VOXEL_PACKET_INTERNAL_SECTION_SIZE)) { sectionLength = (*(VOXEL_PACKET_INTERNAL_SECTION_SIZE*)dataAt); dataAt += sizeof(VOXEL_PACKET_INTERNAL_SECTION_SIZE); dataBytes -= sizeof(VOXEL_PACKET_INTERNAL_SECTION_SIZE); } else { sectionLength = 0; dataBytes = 0; // stop looping something is wrong } } else { sectionLength = dataBytes; } if (sectionLength) { // ask the VoxelTree to read the bitstream into the tree ReadBitstreamToTreeParams args(packetIsColored ? WANT_COLOR : NO_COLOR, WANT_EXISTS_BITS, NULL, getDataSourceUUID()); lockTree(); VoxelPacketData packetData(packetIsCompressed); packetData.loadFinalizedContent(dataAt, sectionLength); if (Application::getInstance()->getLogger()->extraDebugging()) { qDebug("VoxelSystem::parseData() ... Got Packet Section" " color:%s compressed:%s sequence: %u flight:%d usec size:%d data:%d" " subsection:%d sectionLength:%d uncompressed:%d", debug::valueOf(packetIsColored), debug::valueOf(packetIsCompressed), sequence, flightTime, numBytes, dataBytes, subsection, sectionLength, packetData.getUncompressedSize()); } _tree->readBitstreamToTree(packetData.getUncompressedData(), packetData.getUncompressedSize(), args); unlockTree(); dataBytes -= sectionLength; dataAt += sectionLength; } } subsection++; } break; } if (!_useFastVoxelPipeline || _writeRenderFullVBO) { setupNewVoxelsForDrawing(); } else { setupNewVoxelsForDrawingSingleNode(DONT_BAIL_EARLY); } Application::getInstance()->getBandwidthMeter()->inputStream(BandwidthMeter::VOXELS).updateValue(numBytes); return numBytes;} void VoxelSystem::setupNewVoxelsForDrawing() { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "setupNewVoxelsForDrawing()"); if (!_initialized) { return; // bail early if we're not initialized } uint64_t start = usecTimestampNow(); uint64_t sinceLastTime = (start - _setupNewVoxelsForDrawingLastFinished) / 1000; bool iAmDebugging = false; // if you're debugging set this to true, so you won't get skipped for slow debugging if (!iAmDebugging && sinceLastTime <= std::max((float) _setupNewVoxelsForDrawingLastElapsed, SIXTY_FPS_IN_MILLISECONDS)) { return; // bail early, it hasn't been long enough since the last time we ran } _inSetupNewVoxelsForDrawing = true; bool didWriteFullVBO = _writeRenderFullVBO; if (_tree->isDirty()) { static char buffer[64] = { 0 }; if (Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings)) { sprintf(buffer, "newTreeToArrays() _writeRenderFullVBO=%s", debug::valueOf(_writeRenderFullVBO)); }; PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), buffer); _callsToTreesToArrays++; if (_writeRenderFullVBO) { clearFreeBufferIndexes(); } _voxelsUpdated = newTreeToArrays(_tree->getRoot()); _tree->clearDirtyBit(); // after we pull the trees into the array, we can consider the tree clean if (_writeRenderFullVBO) { _abandonedVBOSlots = 0; // reset the count of our abandoned slots, why is this here and not earlier???? } // since we called treeToArrays, we can assume that our VBO is in sync, and so partial updates to the VBOs are // ok again, until/unless we call removeOutOfView() _writeRenderFullVBO = false; } else { _voxelsUpdated = 0; } // lock on the buffer write lock so we can't modify the data when the GPU is reading it _bufferWriteLock.lock(); if (_voxelsUpdated) { _voxelsDirty=true; } // copy the newly written data to the arrays designated for reading, only does something if _voxelsDirty && _voxelsUpdated copyWrittenDataToReadArrays(didWriteFullVBO); _bufferWriteLock.unlock(); uint64_t end = usecTimestampNow(); int elapsedmsec = (end - start) / 1000; _setupNewVoxelsForDrawingLastFinished = end; _setupNewVoxelsForDrawingLastElapsed = elapsedmsec; _inSetupNewVoxelsForDrawing = false; bool extraDebugging = Application::getInstance()->getLogger()->extraDebugging(); if (extraDebugging) { qDebug("setupNewVoxelsForDrawing()... _voxelsUpdated=%lu...",_voxelsUpdated); _viewFrustum->printDebugDetails(); }} void VoxelSystem::setupNewVoxelsForDrawingSingleNode(bool allowBailEarly) { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "setupNewVoxelsForDrawingSingleNode() xxxxx"); uint64_t start = usecTimestampNow(); uint64_t sinceLastTime = (start - _setupNewVoxelsForDrawingLastFinished) / 1000; bool iAmDebugging = false; // if you're debugging set this to true, so you won't get skipped for slow debugging if (allowBailEarly && !iAmDebugging && sinceLastTime <= std::max((float) _setupNewVoxelsForDrawingLastElapsed, SIXTY_FPS_IN_MILLISECONDS)) { return; // bail early, it hasn't been long enough since the last time we ran } // lock on the buffer write lock so we can't modify the data when the GPU is reading it { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "setupNewVoxelsForDrawingSingleNode()... _bufferWriteLock.lock();" ); _bufferWriteLock.lock(); } _voxelsDirty = true; // if we got this far, then we can assume some voxels are dirty // copy the newly written data to the arrays designated for reading, only does something if _voxelsDirty && _voxelsUpdated copyWrittenDataToReadArrays(_writeRenderFullVBO); // after... _voxelsUpdated = 0; _bufferWriteLock.unlock(); uint64_t end = usecTimestampNow(); int elapsedmsec = (end - start) / 1000; _setupNewVoxelsForDrawingLastFinished = end; _setupNewVoxelsForDrawingLastElapsed = elapsedmsec;} void VoxelSystem::checkForCulling() { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "checkForCulling()"); uint64_t start = usecTimestampNow(); // track how long its been since we were last moving. If we have recently moved then only use delta frustums, if // it's been a long time since we last moved, then go ahead and do a full frustum cull. if (isViewChanging()) { _lastViewIsChanging = start; } uint64_t sinceLastMoving = (start - _lastViewIsChanging) / 1000; bool enoughTime = (sinceLastMoving >= std::max((float) _lastViewCullingElapsed, VIEW_CULLING_RATE_IN_MILLISECONDS)); // These has changed events will occur before we stop. So we need to remember this for when we finally have stopped // moving long enough to be enoughTime if (hasViewChanged()) { _hasRecentlyChanged = true; } // If we have recently changed, but it's been enough time since we last moved, then we will do a full frustum // hide/show culling pass bool forceFullFrustum = enoughTime && _hasRecentlyChanged; // in hide mode, we only track the full frustum culls, because we don't care about the partials. if (forceFullFrustum) { _lastViewCulling = start; _hasRecentlyChanged = false; } hideOutOfView(forceFullFrustum); if (forceFullFrustum) { uint64_t endViewCulling = usecTimestampNow(); _lastViewCullingElapsed = (endViewCulling - start) / 1000; } // Once we call cleanupRemovedVoxels() we do need to rebuild our VBOs (if anything was actually removed). So, // we should consider putting this someplace else... as this might be able to occur less frequently, and save us on // VBO reubuilding. Possibly we should do this only if our actual VBO usage crosses some lower boundary. cleanupRemovedVoxels(); uint64_t sinceLastAudit = (start - _lastAudit) / 1000; if (Menu::getInstance()->isOptionChecked(MenuOption::AutomaticallyAuditTree)) { if (sinceLastAudit >= std::max((float) _lastViewCullingElapsed, VIEW_CULLING_RATE_IN_MILLISECONDS)) { _lastAudit = start; collectStatsForTreesAndVBOs(); } }} void VoxelSystem::cleanupRemovedVoxels() { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "cleanupRemovedVoxels()"); // This handles cleanup of voxels that were culled as part of our regular out of view culling operation if (!_removedVoxels.isEmpty()) { if (Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings)) { qDebug() << "cleanupRemovedVoxels().. _removedVoxels=" << _removedVoxels.count(); } while (!_removedVoxels.isEmpty()) { delete _removedVoxels.extract(); } _writeRenderFullVBO = true; // if we remove voxels, we must update our full VBOs } // we also might have VBO slots that have been abandoned, if too many of our VBO slots // are abandonded we want to rerender our full VBOs const float TOO_MANY_ABANDONED_RATIO = 0.5f; if (!_writeRenderFullVBO && (_abandonedVBOSlots > (_voxelsInWriteArrays * TOO_MANY_ABANDONED_RATIO))) { if (Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings)) { qDebug() << "cleanupRemovedVoxels().. _abandonedVBOSlots [" << _abandonedVBOSlots << "] > TOO_MANY_ABANDONED_RATIO"; } _writeRenderFullVBO = true; }} void VoxelSystem::copyWrittenDataToReadArraysFullVBOs() { copyWrittenDataSegmentToReadArrays(0, _voxelsInWriteArrays - 1); _voxelsInReadArrays = _voxelsInWriteArrays; // clear our dirty flags memset(_writeVoxelDirtyArray, false, _voxelsInWriteArrays * sizeof(bool)); // let the reader know to get the full array _readRenderFullVBO = true;} void VoxelSystem::copyWrittenDataToReadArraysPartialVBOs() { glBufferIndex segmentStart = 0; bool inSegment = false; for (glBufferIndex i = 0; i < _voxelsInWriteArrays; i++) { bool thisVoxelDirty = _writeVoxelDirtyArray[i]; _readVoxelDirtyArray[i] |= thisVoxelDirty; _writeVoxelDirtyArray[i] = false; if (!inSegment) { if (thisVoxelDirty) { segmentStart = i; inSegment = true; } } else { if (!thisVoxelDirty) { // If we got here because because this voxel is NOT dirty, so the last dirty voxel was the one before // this one and so that's where the "segment" ends copyWrittenDataSegmentToReadArrays(segmentStart, i - 1); inSegment = false; } } } // if we got to the end of the array, and we're in an active dirty segment... if (inSegment) { copyWrittenDataSegmentToReadArrays(segmentStart, _voxelsInWriteArrays - 1); } // update our length _voxelsInReadArrays = _voxelsInWriteArrays;} void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, glBufferIndex segmentEnd) { int segmentLength = (segmentEnd - segmentStart) + 1; if (_useVoxelShader) { GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData); void* readDataAt = &_readVoxelShaderData[segmentStart]; void* writeDataAt = &_writeVoxelShaderData[segmentStart]; memcpy(readDataAt, writeDataAt, segmentSizeBytes); } else { // Depending on if we're using per vertex normals, we will need more or less vertex points per voxel int vertexPointsPerVoxel = GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL; GLintptr segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLfloat); GLsizeiptr segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLfloat); GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * vertexPointsPerVoxel); GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * vertexPointsPerVoxel); memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes); segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLubyte); segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLubyte); GLubyte* readColorsAt = _readColorsArray + (segmentStart * vertexPointsPerVoxel); GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * vertexPointsPerVoxel); memcpy(readColorsAt, writeColorsAt, segmentSizeBytes); }} void VoxelSystem::copyWrittenDataToReadArrays(bool fullVBOs) { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "copyWrittenDataToReadArrays()"); if (_voxelsDirty && _voxelsUpdated) { if (fullVBOs) { copyWrittenDataToReadArraysFullVBOs(); } else { copyWrittenDataToReadArraysPartialVBOs(); } }} int VoxelSystem::newTreeToArrays(VoxelTreeElement* voxel) { int voxelsUpdated = 0; bool shouldRender = false; // assume we don't need to render it // if it's colored, we might need to render it! float voxelSizeScale = Menu::getInstance()->getVoxelSizeScale();; int boundaryLevelAdjust = Menu::getInstance()->getBoundaryLevelAdjust(); shouldRender = voxel->calculateShouldRender(_viewFrustum, voxelSizeScale, boundaryLevelAdjust); voxel->setShouldRender(shouldRender); // let children figure out their renderness if (!voxel->isLeaf()) { // As we check our children, see if any of them went from shouldRender to NOT shouldRender // then we probably dropped LOD and if we don't have color, we want to average our children // for a new color. int childrenGotHiddenCount = 0; for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { VoxelTreeElement* childVoxel = voxel->getChildAtIndex(i); if (childVoxel) { bool wasShouldRender = childVoxel->getShouldRender(); voxelsUpdated += newTreeToArrays(childVoxel); bool isShouldRender = childVoxel->getShouldRender(); if (wasShouldRender && !isShouldRender) { childrenGotHiddenCount++; } } } if (childrenGotHiddenCount > 0) { voxel->calculateAverageFromChildren(); } } // update their geometry in the array. depending on our over all mode (fullVBO or not) we will reuse or not reuse the index if (_writeRenderFullVBO) { const bool DONT_REUSE_INDEX = false; const bool FORCE_REDRAW = true; voxelsUpdated += updateNodeInArrays(voxel, DONT_REUSE_INDEX, FORCE_REDRAW); } else { const bool REUSE_INDEX = true; const bool DONT_FORCE_REDRAW = false; voxelsUpdated += updateNodeInArrays(voxel, REUSE_INDEX, DONT_FORCE_REDRAW); } voxel->clearDirtyBit(); // clear the dirty bit, do this before we potentially delete things. return voxelsUpdated;} // called as response to elementDeleted() in fast pipeline case. The node// is being deleted, but it's state is such that it thinks it should render// and therefore we can't use the normal render calculations. This method// will forcibly remove it from the VBOs because we know better!!!int VoxelSystem::forceRemoveNodeFromArrays(VoxelTreeElement* node) { if (!_initialized) { return 0; } // if the node is not in the VBOs then we have nothing to do! if (node->isKnownBufferIndex()) { // If this node has not yet been written to the array, then add it to the end of the array. glBufferIndex nodeIndex = node->getBufferIndex(); node->setBufferIndex(GLBUFFER_INDEX_UNKNOWN); freeBufferIndex(nodeIndex); // NOTE: This will make the node invisible! return 1; // updated! } return 0; // not-updated} int VoxelSystem::updateNodeInArrays(VoxelTreeElement* node, bool reuseIndex, bool forceDraw) { // If we've run out of room, then just bail... if (_voxelsInWriteArrays >= _maxVoxels && (_freeIndexes.size() == 0)) { // We need to think about what else we can do in this case. This basically means that all of our available // VBO slots are used up, but we're trying to render more voxels. At this point, if this happens we'll just // not render these Voxels. We need to think about ways to keep the entire scene intact but maybe lower quality // possibly shifting down to lower LOD or something. This debug message is to help identify, if/when/how this // state actually occurs. if (Application::getInstance()->getLogger()->extraDebugging()) { qDebug("OH NO! updateNodeInArrays() BAILING (_voxelsInWriteArrays >= _maxVoxels)"); } return 0; } if (!_initialized) { return 0; } // If we've changed any attributes (our renderness, our color, etc), or we've been told to force a redraw // then update the Arrays... if (forceDraw || node->isDirty()) { // If we're should render, use our legit location and scale, if (node->getShouldRender()) { glm::vec3 startVertex = node->getCorner(); float voxelScale = node->getScale(); glBufferIndex nodeIndex = GLBUFFER_INDEX_UNKNOWN; if (reuseIndex && node->isKnownBufferIndex()) { nodeIndex = node->getBufferIndex(); } else { nodeIndex = getNextBufferIndex(); node->setBufferIndex(nodeIndex); node->setVoxelSystem(this); } // populate the array with points for the 8 vertices and RGB color for each added vertex updateArraysDetails(nodeIndex, startVertex, voxelScale, node->getColor()); return 1; // updated! } else { // If we shouldn't render, and we're in reuseIndex mode, then free our index, this only operates // on nodes with known index values, so it's safe to call for any node. if (reuseIndex) {Lines 501–1000 of 2779. Previous lines Load more
if (!_perlinModulateProgram.isLinked()) { switchToResourcesParentIfRequired(); _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert"); _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/perlin_modulate.frag"); _perlinModulateProgram.link(); _perlinModulateProgram.bind(); _perlinModulateProgram.setUniformValue("permutationNormalTexture", 0); _perlinModulateProgram.release(); _shadowMapProgram.addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/shadow_map.frag"); _shadowMapProgram.link(); _shadowMapProgram.bind(); _shadowMapProgram.setUniformValue("shadowMap", 0); _shadowMapProgram.release(); } } _initialized = true; _bufferWriteLock.unlock(); } void VoxelSystem::writeToSVOFile(const char* filename, VoxelTreeElement* element) const { _tree->writeToSVOFile(filename, element); } bool VoxelSystem::readFromSVOFile(const char* filename) { bool result = _tree->readFromSVOFile(filename); if (result) { setupNewVoxelsForDrawing(); } return result; } bool VoxelSystem::readFromSquareARGB32Pixels(const char *filename) { bool result = _tree->readFromSquareARGB32Pixels(filename); if (result) { setupNewVoxelsForDrawing(); } return result; } bool VoxelSystem::readFromSchematicFile(const char* filename) { bool result = _tree->readFromSchematicFile(filename); if (result) { setupNewVoxelsForDrawing(); } return result; } int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) { bool showTimingDetails = Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings); PerformanceWarning warn(showTimingDetails, "VoxelSystem::parseData()",showTimingDetails); unsigned char command = *sourceBuffer; int numBytesPacketHeader = numBytesForPacketHeader(sourceBuffer); switch(command) { case PACKET_TYPE_VOXEL_DATA: { PerformanceWarning warn(showTimingDetails, "VoxelSystem::parseData() PACKET_TYPE_VOXEL_DATA part...",showTimingDetails); unsigned char* dataAt = sourceBuffer + numBytesPacketHeader; VOXEL_PACKET_FLAGS flags = (*(VOXEL_PACKET_FLAGS*)(dataAt)); dataAt += sizeof(VOXEL_PACKET_FLAGS); VOXEL_PACKET_SEQUENCE sequence = (*(VOXEL_PACKET_SEQUENCE*)dataAt); dataAt += sizeof(VOXEL_PACKET_SEQUENCE); VOXEL_PACKET_SENT_TIME sentAt = (*(VOXEL_PACKET_SENT_TIME*)dataAt); dataAt += sizeof(VOXEL_PACKET_SENT_TIME); bool packetIsColored = oneAtBit(flags, PACKET_IS_COLOR_BIT); bool packetIsCompressed = oneAtBit(flags, PACKET_IS_COMPRESSED_BIT); VOXEL_PACKET_SENT_TIME arrivedAt = usecTimestampNow(); int flightTime = arrivedAt - sentAt; VOXEL_PACKET_INTERNAL_SECTION_SIZE sectionLength = 0; int dataBytes = numBytes - VOXEL_PACKET_HEADER_SIZE; int subsection = 1; while (dataBytes > 0) { if (packetIsCompressed) { if (dataBytes > sizeof(VOXEL_PACKET_INTERNAL_SECTION_SIZE)) { sectionLength = (*(VOXEL_PACKET_INTERNAL_SECTION_SIZE*)dataAt); dataAt += sizeof(VOXEL_PACKET_INTERNAL_SECTION_SIZE); dataBytes -= sizeof(VOXEL_PACKET_INTERNAL_SECTION_SIZE); } else { sectionLength = 0; dataBytes = 0; // stop looping something is wrong } } else { sectionLength = dataBytes; } if (sectionLength) { // ask the VoxelTree to read the bitstream into the tree ReadBitstreamToTreeParams args(packetIsColored ? WANT_COLOR : NO_COLOR, WANT_EXISTS_BITS, NULL, getDataSourceUUID()); lockTree(); VoxelPacketData packetData(packetIsCompressed); packetData.loadFinalizedContent(dataAt, sectionLength); if (Application::getInstance()->getLogger()->extraDebugging()) { qDebug("VoxelSystem::parseData() ... Got Packet Section" " color:%s compressed:%s sequence: %u flight:%d usec size:%d data:%d" " subsection:%d sectionLength:%d uncompressed:%d", debug::valueOf(packetIsColored), debug::valueOf(packetIsCompressed), sequence, flightTime, numBytes, dataBytes, subsection, sectionLength, packetData.getUncompressedSize()); } _tree->readBitstreamToTree(packetData.getUncompressedData(), packetData.getUncompressedSize(), args); unlockTree(); dataBytes -= sectionLength; dataAt += sectionLength; } } subsection++; } break; } if (!_useFastVoxelPipeline || _writeRenderFullVBO) { setupNewVoxelsForDrawing(); } else { setupNewVoxelsForDrawingSingleNode(DONT_BAIL_EARLY); } Application::getInstance()->getBandwidthMeter()->inputStream(BandwidthMeter::VOXELS).updateValue(numBytes); return numBytes; } void VoxelSystem::setupNewVoxelsForDrawing() { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "setupNewVoxelsForDrawing()"); if (!_initialized) { return; // bail early if we're not initialized } uint64_t start = usecTimestampNow(); uint64_t sinceLastTime = (start - _setupNewVoxelsForDrawingLastFinished) / 1000; bool iAmDebugging = false; // if you're debugging set this to true, so you won't get skipped for slow debugging if (!iAmDebugging && sinceLastTime <= std::max((float) _setupNewVoxelsForDrawingLastElapsed, SIXTY_FPS_IN_MILLISECONDS)) { return; // bail early, it hasn't been long enough since the last time we ran } _inSetupNewVoxelsForDrawing = true; bool didWriteFullVBO = _writeRenderFullVBO; if (_tree->isDirty()) { static char buffer[64] = { 0 }; if (Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings)) { sprintf(buffer, "newTreeToArrays() _writeRenderFullVBO=%s", debug::valueOf(_writeRenderFullVBO)); }; PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), buffer); _callsToTreesToArrays++; if (_writeRenderFullVBO) { clearFreeBufferIndexes(); } _voxelsUpdated = newTreeToArrays(_tree->getRoot()); _tree->clearDirtyBit(); // after we pull the trees into the array, we can consider the tree clean if (_writeRenderFullVBO) { _abandonedVBOSlots = 0; // reset the count of our abandoned slots, why is this here and not earlier???? } // since we called treeToArrays, we can assume that our VBO is in sync, and so partial updates to the VBOs are // ok again, until/unless we call removeOutOfView() _writeRenderFullVBO = false; } else { _voxelsUpdated = 0; } // lock on the buffer write lock so we can't modify the data when the GPU is reading it _bufferWriteLock.lock(); if (_voxelsUpdated) { _voxelsDirty=true; } // copy the newly written data to the arrays designated for reading, only does something if _voxelsDirty && _voxelsUpdated copyWrittenDataToReadArrays(didWriteFullVBO); _bufferWriteLock.unlock(); uint64_t end = usecTimestampNow(); int elapsedmsec = (end - start) / 1000; _setupNewVoxelsForDrawingLastFinished = end; _setupNewVoxelsForDrawingLastElapsed = elapsedmsec; _inSetupNewVoxelsForDrawing = false; bool extraDebugging = Application::getInstance()->getLogger()->extraDebugging(); if (extraDebugging) { qDebug("setupNewVoxelsForDrawing()... _voxelsUpdated=%lu...",_voxelsUpdated); _viewFrustum->printDebugDetails(); } } void VoxelSystem::setupNewVoxelsForDrawingSingleNode(bool allowBailEarly) { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "setupNewVoxelsForDrawingSingleNode() xxxxx"); uint64_t start = usecTimestampNow(); uint64_t sinceLastTime = (start - _setupNewVoxelsForDrawingLastFinished) / 1000; bool iAmDebugging = false; // if you're debugging set this to true, so you won't get skipped for slow debugging if (allowBailEarly && !iAmDebugging && sinceLastTime <= std::max((float) _setupNewVoxelsForDrawingLastElapsed, SIXTY_FPS_IN_MILLISECONDS)) { return; // bail early, it hasn't been long enough since the last time we ran } // lock on the buffer write lock so we can't modify the data when the GPU is reading it { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "setupNewVoxelsForDrawingSingleNode()... _bufferWriteLock.lock();" ); _bufferWriteLock.lock(); } _voxelsDirty = true; // if we got this far, then we can assume some voxels are dirty // copy the newly written data to the arrays designated for reading, only does something if _voxelsDirty && _voxelsUpdated copyWrittenDataToReadArrays(_writeRenderFullVBO); // after... _voxelsUpdated = 0; _bufferWriteLock.unlock(); uint64_t end = usecTimestampNow(); int elapsedmsec = (end - start) / 1000; _setupNewVoxelsForDrawingLastFinished = end; _setupNewVoxelsForDrawingLastElapsed = elapsedmsec; } void VoxelSystem::checkForCulling() { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "checkForCulling()"); uint64_t start = usecTimestampNow(); // track how long its been since we were last moving. If we have recently moved then only use delta frustums, if // it's been a long time since we last moved, then go ahead and do a full frustum cull. if (isViewChanging()) { _lastViewIsChanging = start; } uint64_t sinceLastMoving = (start - _lastViewIsChanging) / 1000; bool enoughTime = (sinceLastMoving >= std::max((float) _lastViewCullingElapsed, VIEW_CULLING_RATE_IN_MILLISECONDS)); // These has changed events will occur before we stop. So we need to remember this for when we finally have stopped // moving long enough to be enoughTime if (hasViewChanged()) { _hasRecentlyChanged = true; } // If we have recently changed, but it's been enough time since we last moved, then we will do a full frustum // hide/show culling pass bool forceFullFrustum = enoughTime && _hasRecentlyChanged; // in hide mode, we only track the full frustum culls, because we don't care about the partials. if (forceFullFrustum) { _lastViewCulling = start; _hasRecentlyChanged = false; } hideOutOfView(forceFullFrustum); if (forceFullFrustum) { uint64_t endViewCulling = usecTimestampNow(); _lastViewCullingElapsed = (endViewCulling - start) / 1000; } // Once we call cleanupRemovedVoxels() we do need to rebuild our VBOs (if anything was actually removed). So, // we should consider putting this someplace else... as this might be able to occur less frequently, and save us on // VBO reubuilding. Possibly we should do this only if our actual VBO usage crosses some lower boundary. cleanupRemovedVoxels(); uint64_t sinceLastAudit = (start - _lastAudit) / 1000; if (Menu::getInstance()->isOptionChecked(MenuOption::AutomaticallyAuditTree)) { if (sinceLastAudit >= std::max((float) _lastViewCullingElapsed, VIEW_CULLING_RATE_IN_MILLISECONDS)) { _lastAudit = start; collectStatsForTreesAndVBOs(); } } } void VoxelSystem::cleanupRemovedVoxels() { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "cleanupRemovedVoxels()"); // This handles cleanup of voxels that were culled as part of our regular out of view culling operation if (!_removedVoxels.isEmpty()) { if (Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings)) { qDebug() << "cleanupRemovedVoxels().. _removedVoxels=" << _removedVoxels.count(); } while (!_removedVoxels.isEmpty()) { delete _removedVoxels.extract(); } _writeRenderFullVBO = true; // if we remove voxels, we must update our full VBOs } // we also might have VBO slots that have been abandoned, if too many of our VBO slots // are abandonded we want to rerender our full VBOs const float TOO_MANY_ABANDONED_RATIO = 0.5f; if (!_writeRenderFullVBO && (_abandonedVBOSlots > (_voxelsInWriteArrays * TOO_MANY_ABANDONED_RATIO))) { if (Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings)) { qDebug() << "cleanupRemovedVoxels().. _abandonedVBOSlots [" << _abandonedVBOSlots << "] > TOO_MANY_ABANDONED_RATIO"; } _writeRenderFullVBO = true; } } void VoxelSystem::copyWrittenDataToReadArraysFullVBOs() { copyWrittenDataSegmentToReadArrays(0, _voxelsInWriteArrays - 1); _voxelsInReadArrays = _voxelsInWriteArrays; // clear our dirty flags memset(_writeVoxelDirtyArray, false, _voxelsInWriteArrays * sizeof(bool)); // let the reader know to get the full array _readRenderFullVBO = true; } void VoxelSystem::copyWrittenDataToReadArraysPartialVBOs() { glBufferIndex segmentStart = 0; bool inSegment = false; for (glBufferIndex i = 0; i < _voxelsInWriteArrays; i++) { bool thisVoxelDirty = _writeVoxelDirtyArray[i]; _readVoxelDirtyArray[i] |= thisVoxelDirty; _writeVoxelDirtyArray[i] = false; if (!inSegment) { if (thisVoxelDirty) { segmentStart = i; inSegment = true; } } else { if (!thisVoxelDirty) { // If we got here because because this voxel is NOT dirty, so the last dirty voxel was the one before // this one and so that's where the "segment" ends copyWrittenDataSegmentToReadArrays(segmentStart, i - 1); inSegment = false; } } } // if we got to the end of the array, and we're in an active dirty segment... if (inSegment) { copyWrittenDataSegmentToReadArrays(segmentStart, _voxelsInWriteArrays - 1); } // update our length _voxelsInReadArrays = _voxelsInWriteArrays; } void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, glBufferIndex segmentEnd) { int segmentLength = (segmentEnd - segmentStart) + 1; if (_useVoxelShader) { GLsizeiptr segmentSizeBytes = segmentLength * sizeof(VoxelShaderVBOData); void* readDataAt = &_readVoxelShaderData[segmentStart]; void* writeDataAt = &_writeVoxelShaderData[segmentStart]; memcpy(readDataAt, writeDataAt, segmentSizeBytes); } else { // Depending on if we're using per vertex normals, we will need more or less vertex points per voxel int vertexPointsPerVoxel = GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL; GLintptr segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLfloat); GLsizeiptr segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLfloat); GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * vertexPointsPerVoxel); GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * vertexPointsPerVoxel); memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes); segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLubyte); segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLubyte); GLubyte* readColorsAt = _readColorsArray + (segmentStart * vertexPointsPerVoxel); GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * vertexPointsPerVoxel); memcpy(readColorsAt, writeColorsAt, segmentSizeBytes); } } void VoxelSystem::copyWrittenDataToReadArrays(bool fullVBOs) { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "copyWrittenDataToReadArrays()"); if (_voxelsDirty && _voxelsUpdated) { if (fullVBOs) { copyWrittenDataToReadArraysFullVBOs(); } else { copyWrittenDataToReadArraysPartialVBOs(); } } } int VoxelSystem::newTreeToArrays(VoxelTreeElement* voxel) { int voxelsUpdated = 0; bool shouldRender = false; // assume we don't need to render it // if it's colored, we might need to render it! float voxelSizeScale = Menu::getInstance()->getVoxelSizeScale();; int boundaryLevelAdjust = Menu::getInstance()->getBoundaryLevelAdjust(); shouldRender = voxel->calculateShouldRender(_viewFrustum, voxelSizeScale, boundaryLevelAdjust); voxel->setShouldRender(shouldRender); // let children figure out their renderness if (!voxel->isLeaf()) { // As we check our children, see if any of them went from shouldRender to NOT shouldRender // then we probably dropped LOD and if we don't have color, we want to average our children // for a new color. int childrenGotHiddenCount = 0; for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { VoxelTreeElement* childVoxel = voxel->getChildAtIndex(i); if (childVoxel) { bool wasShouldRender = childVoxel->getShouldRender(); voxelsUpdated += newTreeToArrays(childVoxel); bool isShouldRender = childVoxel->getShouldRender(); if (wasShouldRender && !isShouldRender) { childrenGotHiddenCount++; } } } if (childrenGotHiddenCount > 0) { voxel->calculateAverageFromChildren(); } } // update their geometry in the array. depending on our over all mode (fullVBO or not) we will reuse or not reuse the index if (_writeRenderFullVBO) { const bool DONT_REUSE_INDEX = false; const bool FORCE_REDRAW = true; voxelsUpdated += updateNodeInArrays(voxel, DONT_REUSE_INDEX, FORCE_REDRAW); } else { const bool REUSE_INDEX = true; const bool DONT_FORCE_REDRAW = false; voxelsUpdated += updateNodeInArrays(voxel, REUSE_INDEX, DONT_FORCE_REDRAW); } voxel->clearDirtyBit(); // clear the dirty bit, do this before we potentially delete things. return voxelsUpdated; } // called as response to elementDeleted() in fast pipeline case. The node // is being deleted, but it's state is such that it thinks it should render // and therefore we can't use the normal render calculations. This method // will forcibly remove it from the VBOs because we know better!!! int VoxelSystem::forceRemoveNodeFromArrays(VoxelTreeElement* node) { if (!_initialized) { return 0; } // if the node is not in the VBOs then we have nothing to do! if (node->isKnownBufferIndex()) { // If this node has not yet been written to the array, then add it to the end of the array. glBufferIndex nodeIndex = node->getBufferIndex(); node->setBufferIndex(GLBUFFER_INDEX_UNKNOWN); freeBufferIndex(nodeIndex); // NOTE: This will make the node invisible! return 1; // updated! } return 0; // not-updated } int VoxelSystem::updateNodeInArrays(VoxelTreeElement* node, bool reuseIndex, bool forceDraw) { // If we've run out of room, then just bail... if (_voxelsInWriteArrays >= _maxVoxels && (_freeIndexes.size() == 0)) { // We need to think about what else we can do in this case. This basically means that all of our available // VBO slots are used up, but we're trying to render more voxels. At this point, if this happens we'll just // not render these Voxels. We need to think about ways to keep the entire scene intact but maybe lower quality // possibly shifting down to lower LOD or something. This debug message is to help identify, if/when/how this // state actually occurs. if (Application::getInstance()->getLogger()->extraDebugging()) { qDebug("OH NO! updateNodeInArrays() BAILING (_voxelsInWriteArrays >= _maxVoxels)"); } return 0; } if (!_initialized) { return 0; } // If we've changed any attributes (our renderness, our color, etc), or we've been told to force a redraw // then update the Arrays... if (forceDraw || node->isDirty()) { // If we're should render, use our legit location and scale, if (node->getShouldRender()) { glm::vec3 startVertex = node->getCorner(); float voxelScale = node->getScale(); glBufferIndex nodeIndex = GLBUFFER_INDEX_UNKNOWN; if (reuseIndex && node->isKnownBufferIndex()) { nodeIndex = node->getBufferIndex(); } else { nodeIndex = getNextBufferIndex(); node->setBufferIndex(nodeIndex); node->setVoxelSystem(this); } // populate the array with points for the 8 vertices and RGB color for each added vertex updateArraysDetails(nodeIndex, startVertex, voxelScale, node->getColor()); return 1; // updated! } else { // If we shouldn't render, and we're in reuseIndex mode, then free our index, this only operates // on nodes with known index values, so it's safe to call for any node. if (reuseIndex) {