From f8f0da189f290f649569ab5b73d3e1b9dfdb8cb4 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 15 Aug 2014 17:13:59 -0700 Subject: [PATCH] more work on more entities in a cell --- .../src/octree/OctreeSendThread.cpp | 44 ++++++++++++++++--- libraries/entities/src/EntityTreeElement.cpp | 6 +++ libraries/octree/src/Octree.cpp | 17 ++++++- libraries/octree/src/OctreePacketData.cpp | 32 +++++++++++++- libraries/octree/src/OctreePacketData.h | 10 +++-- 5 files changed, 97 insertions(+), 12 deletions(-) diff --git a/assignment-client/src/octree/OctreeSendThread.cpp b/assignment-client/src/octree/OctreeSendThread.cpp index b08296d5c3..f83514bb1f 100644 --- a/assignment-client/src/octree/OctreeSendThread.cpp +++ b/assignment-client/src/octree/OctreeSendThread.cpp @@ -411,6 +411,15 @@ int OctreeSendThread::packetDistributor(OctreeQueryNode* nodeData, bool viewFrus bool lastNodeDidntFit = false; // assume each node fits if (!nodeData->elementBag.isEmpty()) { OctreeElement* subTree = nodeData->elementBag.extract(); + +qDebug() << "==============================================================="; +if (subTree == _myServer->getOctree()->getRoot()) { + qDebug() << "OctreeSendThread::packetDistributor() subTree=ROOT"; +} else { + qDebug() << "==============================================================="; + qDebug() << "OctreeSendThread::packetDistributor() subTree=" << subTree; +} + /* TODO: Looking for a way to prevent locking and encoding a tree that is not // going to result in any packets being sent... @@ -474,12 +483,27 @@ qDebug() << " bytesWritten=" << bytesWritten; // the packet and send it completedScene = nodeData->elementBag.isEmpty(); + +qDebug() << "+++++++++++++++ lastNodeDidntFit logic +++++++++++++++"; +qDebug() << "OctreeSendThread::packetDistributor()... line:" << __LINE__; +qDebug() << " _packetData.getTargetSize()=" << _packetData.getTargetSize(); +qDebug() << " MAX_OCTREE_PACKET_DATA_SIZE=" << MAX_OCTREE_PACKET_DATA_SIZE; +qDebug() << " _packetData.hasContent()=" << _packetData.hasContent(); +qDebug() << " bytesWritten=" << bytesWritten; +if (params.stopReason == EncodeBitstreamParams::DIDNT_FIT) { + qDebug() << " params.stopReason == EncodeBitstreamParams::DIDNT_FIT"; +} else { + qDebug() << " params.stopReason == ???"; + params.displayStopReason(); +} + // if we're trying to fill a full size packet, then we use this logic to determine if we have a DIDNT_FIT case. if (_packetData.getTargetSize() == MAX_OCTREE_PACKET_DATA_SIZE) { if (_packetData.hasContent() && bytesWritten == 0 && params.stopReason == EncodeBitstreamParams::DIDNT_FIT) { lastNodeDidntFit = true; qDebug() << "OctreeSendThread::packetDistributor()... line:" << __LINE__; +qDebug() << " -- trying to fill a full size packet --"; qDebug() << " lastNodeDidntFit=true"; } } else { @@ -490,6 +514,7 @@ qDebug() << " lastNodeDidntFit=true"; if (bytesWritten == 0 && params.stopReason == EncodeBitstreamParams::DIDNT_FIT) { lastNodeDidntFit = true; qDebug() << "OctreeSendThread::packetDistributor()... line:" << __LINE__; +qDebug() << " -- in compressed mode and we are trying to pack more --"; qDebug() << " lastNodeDidntFit=true"; } } @@ -500,6 +525,10 @@ qDebug() << " lastNodeDidntFit=true"; // If the bag was empty then we didn't even attempt to encode, and so we know the bytesWritten were 0 bytesWritten = 0; somethingToSend = false; // this will cause us to drop out of the loop... + +qDebug() << "OctreeSendThread::packetDistributor()... line:" << __LINE__; +qDebug() << " -- If the bag was empty then we didn't even attempt to encode, and so we know the bytesWritten were 0 --"; + } // If the last node didn't fit, but we're in compressed mode, then we actually want to see if we can fit a @@ -517,7 +546,7 @@ qDebug() << " lastNodeDidntFit=" << lastNodeDidntFit; if (_packetData.hasContent()) { -qDebug() << " _packetData.hasContent()=" << _packetData.hasContent(); +//qDebug() << " _packetData.hasContent()=" << _packetData.hasContent(); quint64 compressAndWriteStart = usecTimestampNow(); @@ -527,20 +556,21 @@ qDebug() << " _packetData.hasContent()=" << _packetData.hasContent(); unsigned int writtenSize = _packetData.getFinalizedSize() + (nodeData->getCurrentPacketIsCompressed() ? sizeof(OCTREE_PACKET_INTERNAL_SECTION_SIZE) : 0); +/* qDebug() << " _packetData.getUncompressedSize()=" << _packetData.getUncompressedSize(); qDebug() << " _packetData.getFinalizedSize()=" << _packetData.getFinalizedSize(); qDebug() << " writtenSize=" << writtenSize; qDebug() << " nodeData->getAvailable()=" << nodeData->getAvailable(); - +*/ if (writtenSize > nodeData->getAvailable()) { -qDebug() << "OctreeSendThread::packetDistributor()... line:" << __LINE__; -qDebug() << " calling handlePacketSend()..."; +//qDebug() << "OctreeSendThread::packetDistributor()... line:" << __LINE__; +//qDebug() << " calling handlePacketSend()..."; packetsSentThisInterval += handlePacketSend(nodeData, trueBytesSent, truePacketsSent); } -qDebug() << "OctreeSendThread::packetDistributor()... line:" << __LINE__; -qDebug() << " _packetData.getFinalizedSize()=" << _packetData.getFinalizedSize(); -qDebug() << " called nodeData->writeToPacket(... _packetData.getFinalizedSize()... )"; +//qDebug() << "OctreeSendThread::packetDistributor()... line:" << __LINE__; +//qDebug() << " _packetData.getFinalizedSize()=" << _packetData.getFinalizedSize(); +//qDebug() << " called nodeData->writeToPacket(... _packetData.getFinalizedSize()... )"; nodeData->writeToPacket(_packetData.getFinalizedData(), _packetData.getFinalizedSize()); extraPackingAttempts = 0; diff --git a/libraries/entities/src/EntityTreeElement.cpp b/libraries/entities/src/EntityTreeElement.cpp index 1bedb8b0e7..042a6c06f0 100644 --- a/libraries/entities/src/EntityTreeElement.cpp +++ b/libraries/entities/src/EntityTreeElement.cpp @@ -215,6 +215,12 @@ OctreeElement::AppendState EntityTreeElement::appendElementData(OctreePacketData qDebug() << " something went wrong... discardLevel().... appendElementState = OctreeElement::NONE;"; qDebug() << " successUpdateEntityCount=" << successUpdateEntityCount; qDebug() << " noEntitiesFit=" << noEntitiesFit; + if (extraEncodeData) { + qDebug() << " do we still have extra data?? " << extraEncodeData->contains(this); + } else { + qDebug() << " what happened to extraEncodeData??"; + } + } else { packetData->endLevel(elementLevel); diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index 9da6cb63f7..1cfd54b360 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -1015,6 +1015,8 @@ int Octree::encodeTreeBitstream(OctreeElement* element, if (params.includeColor && childBytesWritten == 2) { childBytesWritten = 0; //params.stopReason = EncodeBitstreamParams::UNKNOWN; // possibly should be DIDNT_FIT... + +qDebug() << "STOP REASON.... if (params.includeColor && childBytesWritten == 2)...."; } // if we wrote child bytes, then return our result of all bytes written @@ -1024,6 +1026,7 @@ int Octree::encodeTreeBitstream(OctreeElement* element, // otherwise... if we didn't write any child bytes, then pretend like we also didn't write our octal code bytesWritten = 0; //params.stopReason = EncodeBitstreamParams::DIDNT_FIT; +qDebug() << "STOP REASON.... childBytesWritten == 0??????"; } if (bytesWritten == 0) { @@ -1203,7 +1206,17 @@ int Octree::encodeTreeBitstreamRecursion(OctreeElement* element, // Make our local buffer large enough to handle writing at this level in case we need to. LevelDetails thisLevelKey = packetData->startLevel(); + bool continueThisLevel = packetData->reserveBytes(sizeof(childrenDataBits) + + sizeof(childrenExistInPacketBits) + + sizeof(childrenExistInTreeBits)); + // If we can't reserve our minimum bytes then we can discard this level and return as if none of this level fits + if (!continueThisLevel) { + packetData->discardLevel(thisLevelKey); + params.stopReason = EncodeBitstreamParams::DIDNT_FIT; + return bytesAtThisLevel; + } + int inViewCount = 0; int inViewNotLeafCount = 0; int inViewWithColorCount = 0; @@ -1386,7 +1399,7 @@ int Octree::encodeTreeBitstreamRecursion(OctreeElement* element, // NOTE: the childrenDataBits is really more generically the childDataBits and it indicates // that there is an array of child element data included in this packet. We wil write this bit mask // but we may come back later and update the bits that are actually included - bool continueThisLevel = true; + packetData->releaseReservedBytes(sizeof(childrenDataBits)); continueThisLevel = packetData->appendBitMask(childrenDataBits); // we know the last thing we wrote to the packet was our childrenDataBits. Let's remember where that was! @@ -1501,6 +1514,7 @@ qDebug() << " packetData->getUncompressedSize()=" << packetData->getUncompres // if the caller wants to include childExistsBits, then include them even if not in view, put them before the // childrenExistInPacketBits, so that the lower code can properly repair the packet exists bits if (continueThisLevel && params.includeExistsBits) { + packetData->releaseReservedBytes(sizeof(childrenExistInTreeBits)); continueThisLevel = packetData->appendBitMask(childrenExistInTreeBits); qDebug() << " packetData->appendBitMask() line:" << __LINE__; qDebug() << " continueThisLevel=" << continueThisLevel; @@ -1514,6 +1528,7 @@ qDebug() << " continueThisLevel=" << continueThisLevel; // write the child exist bits if (continueThisLevel) { + packetData->releaseReservedBytes(sizeof(childrenExistInPacketBits)); continueThisLevel = packetData->appendBitMask(childrenExistInPacketBits); qDebug() << " packetData->appendBitMask() line:" << __LINE__; qDebug() << " continueThisLevel=" << continueThisLevel; diff --git a/libraries/octree/src/OctreePacketData.cpp b/libraries/octree/src/OctreePacketData.cpp index 1d41030b2f..cf9187d2af 100644 --- a/libraries/octree/src/OctreePacketData.cpp +++ b/libraries/octree/src/OctreePacketData.cpp @@ -35,6 +35,7 @@ void OctreePacketData::changeSettings(bool enableCompression, unsigned int targe void OctreePacketData::reset() { _bytesInUse = 0; _bytesAvailable = _targetSize; + _bytesReserved = 0; _subTreeAt = 0; _compressedBytes = 0; _bytesInUseLastCheck = 0; @@ -80,6 +81,13 @@ bool OctreePacketData::reserveBitMask() { bool OctreePacketData::reserveBytes(int numberOfBytes) { bool success = false; + + if (_bytesAvailable >= numberOfBytes) { + _bytesReserved += numberOfBytes; + _bytesAvailable -= numberOfBytes; + success = true; + } + return success; } @@ -89,6 +97,13 @@ bool OctreePacketData::releaseReservedBitMask() { bool OctreePacketData::releaseReservedBytes(int numberOfBytes) { bool success = false; + + if (_bytesReserved >= numberOfBytes) { + _bytesReserved -= numberOfBytes; + _bytesAvailable += numberOfBytes; + success = true; + } + return success; } @@ -129,6 +144,7 @@ bool OctreePacketData::startSubTree(const unsigned char* octcode) { } if (success) { _subTreeAt = possibleStartAt; + _subTreeBytesReserved = _bytesReserved; } if (success) { _bytesOfOctalCodes += length; @@ -182,10 +198,13 @@ void OctreePacketData::discardSubTree() { int reduceBytesOfOctalCodes = _bytesOfOctalCodes - _bytesOfOctalCodesCurrentSubTree; _bytesOfOctalCodes = _bytesOfOctalCodesCurrentSubTree; _totalBytesOfOctalCodes -= reduceBytesOfOctalCodes; + + // if we discard the subtree then reset reserved bytes to the value when we started the subtree + _bytesReserved = _subTreeBytesReserved; } LevelDetails OctreePacketData::startLevel() { - LevelDetails key(_bytesInUse, _bytesOfOctalCodes, _bytesOfBitMasks, _bytesOfColor); + LevelDetails key(_bytesInUse, _bytesOfOctalCodes, _bytesOfBitMasks, _bytesOfColor, _bytesReserved); return key; } @@ -213,6 +232,9 @@ void OctreePacketData::discardLevel(LevelDetails key) { _bytesInUse -= bytesInLevel; _bytesAvailable += bytesInLevel; _dirty = true; + + // reserved bytes are reset to the value when the level started + _bytesReserved = key._bytesReservedAtStart; if (_debug) { qDebug("discardLevel() AFTER _dirty=%s bytesInLevel=%d _compressedBytes=%d _bytesInUse=%d", @@ -222,6 +244,14 @@ void OctreePacketData::discardLevel(LevelDetails key) { bool OctreePacketData::endLevel(LevelDetails key) { bool success = true; + + // reserved bytes should be the same value as when the level started + if (_bytesReserved != key._bytesReservedAtStart) { + qDebug() << "WARNING: endLevel() called but some reserved bytes not used."; + qDebug() << " current bytesReserved:" << _bytesReserved; + qDebug() << " start level bytesReserved:" << key._bytesReservedAtStart; + } + return success; } diff --git a/libraries/octree/src/OctreePacketData.h b/libraries/octree/src/OctreePacketData.h index abe9310eae..4f7deb0f54 100644 --- a/libraries/octree/src/OctreePacketData.h +++ b/libraries/octree/src/OctreePacketData.h @@ -53,11 +53,12 @@ const int PACKET_IS_COMPRESSED_BIT = 1; /// An opaque key used when starting, ending, and discarding encoding/packing levels of OctreePacketData class LevelDetails { - LevelDetails(int startIndex, int bytesOfOctalCodes, int bytesOfBitmasks, int bytesOfColor) : + LevelDetails(int startIndex, int bytesOfOctalCodes, int bytesOfBitmasks, int bytesOfColor, int bytesReservedAtStart) : _startIndex(startIndex), _bytesOfOctalCodes(bytesOfOctalCodes), _bytesOfBitmasks(bytesOfBitmasks), - _bytesOfColor(bytesOfColor) { + _bytesOfColor(bytesOfColor), + _bytesReservedAtStart(bytesReservedAtStart) { } friend class OctreePacketData; @@ -67,6 +68,7 @@ private: int _bytesOfOctalCodes; int _bytesOfBitmasks; int _bytesOfColor; + int _bytesReservedAtStart; }; /// Handles packing of the data portion of PacketType_OCTREE_DATA messages. @@ -116,7 +118,7 @@ public: bool reserveBytes(int numberOfBytes); /// releases previously reserved space in the stream. - bool releaseReserveBitMask(); + bool releaseReservedBitMask(); /// releases previously reserved space in the stream. bool releaseReservedBytes(int numberOfBytes); @@ -226,6 +228,8 @@ private: int _bytesInUse; int _bytesAvailable; int _subTreeAt; + int _bytesReserved; + int _subTreeBytesReserved; // the number of reserved bytes at start of a subtree bool compressContent();