22 #ifndef hifi_OctreePacketData_h
23 #define hifi_OctreePacketData_h
31 #include <SharedUtil.h>
32 #include <ShapeInfo.h>
34 #include <udt/PacketHeaders.h>
36 #include "EntityShape.h"
37 #include "MaterialMappingMode.h"
38 #include "BillboardMode.h"
39 #include "RenderLayer.h"
40 #include "PrimitiveMode.h"
41 #include "WebInputMode.h"
42 #include "PulseMode.h"
43 #include "GizmoType.h"
44 #include "TextEffect.h"
45 #include "TextAlignment.h"
46 #include "TextVerticalAlignment.h"
47 #include "MirrorMode.h"
48 #include "TonemappingCurve.h"
49 #include "AmbientOcclusionTechnique.h"
50 #include "FadeTiming.h"
53 #include "OctreeConstants.h"
54 #include "OctreeElement.h"
56 using AtomicUIntStat = std::atomic<uintmax_t>;
58 typedef unsigned char OCTREE_PACKET_FLAGS;
59 typedef uint16_t OCTREE_PACKET_SEQUENCE;
60 const uint16_t MAX_OCTREE_PACKET_SEQUENCE = 65535;
61 typedef quint64 OCTREE_PACKET_SENT_TIME;
62 typedef uint16_t OCTREE_PACKET_INTERNAL_SECTION_SIZE;
63 const int MAX_OCTREE_PACKET_SIZE = udt::MAX_PACKET_SIZE;
65 const unsigned int OCTREE_PACKET_EXTRA_HEADERS_SIZE =
sizeof(OCTREE_PACKET_FLAGS)
66 +
sizeof(OCTREE_PACKET_SEQUENCE) +
sizeof(OCTREE_PACKET_SENT_TIME);
68 const unsigned int MAX_OCTREE_PACKET_DATA_SIZE =
69 udt::MAX_PACKET_SIZE - (NLPacket::MAX_PACKET_HEADER_SIZE + OCTREE_PACKET_EXTRA_HEADERS_SIZE);
70 const unsigned int MAX_OCTREE_UNCOMRESSED_PACKET_SIZE = MAX_OCTREE_PACKET_DATA_SIZE;
72 const unsigned int MINIMUM_ATTEMPT_MORE_PACKING =
sizeof(OCTREE_PACKET_INTERNAL_SECTION_SIZE) + 40;
73 const unsigned int COMPRESS_PADDING = 15;
74 const int REASONABLE_NUMBER_OF_PACKING_ATTEMPTS = 5;
76 const int PACKET_IS_COLOR_BIT = 0;
77 const int PACKET_IS_COMPRESSED_BIT = 1;
81 LevelDetails(
int startIndex,
int bytesOfOctalCodes,
int bytesOfBitmasks,
int bytesOfColor,
int bytesReservedAtStart) :
82 _startIndex(startIndex),
83 _bytesOfOctalCodes(bytesOfOctalCodes),
84 _bytesOfBitmasks(bytesOfBitmasks),
85 _bytesOfColor(bytesOfColor),
86 _bytesReservedAtStart(bytesReservedAtStart) {
93 int _bytesOfOctalCodes;
96 int _bytesReservedAtStart;
102 OctreePacketData(
bool enableCompression =
false,
int maxFinalizedSize = MAX_OCTREE_PACKET_DATA_SIZE);
106 void changeSettings(
bool enableCompression =
false,
unsigned int targetSize = MAX_OCTREE_PACKET_DATA_SIZE);
119 void discardSubTree();
153 bool updatePriorBytes(
int offset,
const unsigned char* replacementBytes,
int length);
156 bool appendColor(colorPart red, colorPart green, colorPart blue);
267 int getBytesAvailable() {
return _bytesAvailable; }
273 static quint64 getCompressContentTime() {
return _compressContentTime; }
279 static int unpackDataFromBytes(
const unsigned char* dataBytes,
float& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
280 static int unpackDataFromBytes(
const unsigned char* dataBytes,
bool& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
281 static int unpackDataFromBytes(
const unsigned char* dataBytes, quint64& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
282 static int unpackDataFromBytes(
const unsigned char* dataBytes, uint32_t& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
283 static int unpackDataFromBytes(
const unsigned char* dataBytes, uint16_t& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
284 static int unpackDataFromBytes(
const unsigned char* dataBytes, uint8_t& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
285 static int unpackDataFromBytes(
const unsigned char* dataBytes, glm::quat& result) {
int bytes = unpackOrientationQuatFromBytes(dataBytes, result);
return bytes; }
286 static int unpackDataFromBytes(
const unsigned char* dataBytes, EntityShape& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
287 static int unpackDataFromBytes(
const unsigned char* dataBytes, ShapeType& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
288 static int unpackDataFromBytes(
const unsigned char* dataBytes, MaterialMappingMode& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
289 static int unpackDataFromBytes(
const unsigned char* dataBytes, BillboardMode& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
290 static int unpackDataFromBytes(
const unsigned char* dataBytes, RenderLayer& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
291 static int unpackDataFromBytes(
const unsigned char* dataBytes, PrimitiveMode& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
292 static int unpackDataFromBytes(
const unsigned char* dataBytes, WebInputMode& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
293 static int unpackDataFromBytes(
const unsigned char* dataBytes, PulseMode& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
294 static int unpackDataFromBytes(
const unsigned char* dataBytes, GizmoType& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
295 static int unpackDataFromBytes(
const unsigned char* dataBytes, TextEffect& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
296 static int unpackDataFromBytes(
const unsigned char* dataBytes, TextAlignment& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
297 static int unpackDataFromBytes(
const unsigned char* dataBytes, TextVerticalAlignment& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
298 static int unpackDataFromBytes(
const unsigned char* dataBytes, MirrorMode& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
299 static int unpackDataFromBytes(
const unsigned char* dataBytes, TonemappingCurve& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
300 static int unpackDataFromBytes(
const unsigned char* dataBytes, AmbientOcclusionTechnique& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
301 static int unpackDataFromBytes(
const unsigned char* dataBytes, FadeTiming& result) { memcpy(&result, dataBytes,
sizeof(result));
return sizeof(result); }
307 static int unpackDataFromBytes(
const unsigned char* dataBytes, QVector<glm::vec3>& result);
308 static int unpackDataFromBytes(
const unsigned char* dataBytes, QVector<glm::quat>& result);
320 bool append(
const unsigned char* data,
int length);
323 bool append(
unsigned char byte);
325 unsigned int _targetSize;
326 bool _enableCompression;
328 QByteArray _uncompressedByteArray;
329 unsigned char* _uncompressed {
nullptr };
334 int _subTreeBytesReserved;
336 bool compressContent();
338 QByteArray _compressedByteArray;
339 unsigned char* _compressed {
nullptr };
340 int _compressedBytes;
341 int _bytesInUseLastCheck;
345 int _bytesOfOctalCodes;
346 int _bytesOfBitMasks;
349 int _bytesOfPositions;
352 int _bytesOfOctalCodesCurrentSubTree;
356 static AtomicUIntStat _compressContentTime;
357 static AtomicUIntStat _compressContentCalls;
359 static AtomicUIntStat _totalBytesOfOctalCodes;
360 static AtomicUIntStat _totalBytesOfBitMasks;
361 static AtomicUIntStat _totalBytesOfColor;
362 static AtomicUIntStat _totalBytesOfValues;
363 static AtomicUIntStat _totalBytesOfPositions;
364 static AtomicUIntStat _totalBytesOfRawData;
An opaque key used when starting, ending, and discarding encoding/packing levels of OctreePacketData.
Definition: OctreePacketData.h:80
Handles packing of the data portion of PacketType_OCTREE_DATA messages.
Definition: OctreePacketData.h:100
void debugContent()
displays contents for debugging
Definition: OctreePacketData.cpp:708
LevelDetails startLevel()
starts a level marker. returns an opaque key which can be used to discard the level
Definition: OctreePacketData.cpp:241
void changeSettings(bool enableCompression=false, unsigned int targetSize=MAX_OCTREE_PACKET_DATA_SIZE)
change compression and target size settings
Definition: OctreePacketData.cpp:39
bool reserveBytes(int numberOfBytes)
Definition: OctreePacketData.cpp:113
int getUncompressedSize()
the size of the packet in uncompressed form
Definition: OctreePacketData.h:247
static quint64 getTotalBytesOfOctalCodes()
total calls to compress content
Definition: OctreePacketData.h:275
void setUncompressedSize(int newSize)
update the size of the packet in uncompressed form
Definition: OctreePacketData.h:250
int getReservedBytes()
the number of bytes in the packet currently reserved
Definition: OctreePacketData.h:265
bool isCompressed() const
returns whether or not zlib compression enabled on finalization
Definition: OctreePacketData.h:259
bool appendValue(const nodeColor &color)
appends a color to the end of the stream, may fail if new data stream is too long to fit in packet
Definition: OctreePacketData.cpp:302
unsigned int getTargetSize() const
returns the target uncompressed size
Definition: OctreePacketData.h:262
bool startSubTree(const unsigned char *octcode=NULL)
Definition: OctreePacketData.cpp:166
void reset()
reset completely, all data is discarded
Definition: OctreePacketData.cpp:55
bool appendColor(colorPart red, colorPart green, colorPart blue)
appends a color to the end of the stream, may fail if new data stream is too long to fit in packet
Definition: OctreePacketData.cpp:306
void discardLevel(LevelDetails key)
discards all content back to a previous marker key
Definition: OctreePacketData.cpp:246
static quint64 getCompressContentCalls()
total time spent compressing content
Definition: OctreePacketData.h:274
const unsigned char * getUncompressedData(int byteOffset=0)
get pointer to the uncompressed stream buffer at the byteOffset
Definition: OctreePacketData.h:244
void loadFinalizedContent(const unsigned char *data, int length)
load finalized content to allow access to decoded content for parsing
Definition: OctreePacketData.cpp:673
int getFinalizedSize()
get size of the finalized data (it may be compressed or rewritten into optimal form)
Definition: OctreePacketData.cpp:205
int getUncompressedByteOffset(int offsetFromEnd=0) const
Definition: OctreePacketData.h:236
static int unpackDataFromBytes(const unsigned char *dataBytes, float &result)
total bytes of color
Definition: OctreePacketData.h:279
bool hasContent() const
has some content been written to the packet
Definition: OctreePacketData.h:253
bool releaseReservedBitMask()
releases previously reserved space in the stream.
Definition: OctreePacketData.cpp:125
bool appendRawData(const unsigned char *data, int length)
appends raw bytes, might fail if byte would cause packet to be too large
Definition: OctreePacketData.cpp:626
bool appendPosition(const glm::vec3 &value)
appends a position to the end of the stream, may fail if new data stream is too long to fit in packet
Definition: OctreePacketData.cpp:615
static quint64 getTotalBytesOfColor()
total bytes of bitmasks
Definition: OctreePacketData.h:277
bool releaseReservedBytes(int numberOfBytes)
releases previously reserved space in the stream.
Definition: OctreePacketData.cpp:129
bool updatePriorBytes(int offset, const unsigned char *replacementBytes, int length)
Definition: OctreePacketData.cpp:152
bool endLevel(LevelDetails key)
Definition: OctreePacketData.cpp:280
bool updatePriorBitMask(int offset, unsigned char bitmask)
Definition: OctreePacketData.cpp:142
bool reserveBitMask()
reserves space in the stream for a future bitmask, may fail if new data stream is too long to fit in ...
Definition: OctreePacketData.cpp:109
bool appendBitMask(unsigned char bitmask)
appends a bitmask to the end of the stream, may fail if new data stream is too long to fit in packet
Definition: OctreePacketData.cpp:293
const unsigned char * getFinalizedData()
get access to the finalized data (it may be compressed or rewritten into optimal form)
Definition: OctreePacketData.cpp:191
static quint64 getTotalBytesOfBitMasks()
total bytes for octal codes
Definition: OctreePacketData.h:276