2722 lines · 105.0 KB · C++
RawDownload
        _writeVerticesArray = new GLfloat[vertexPointsPerVoxel * _maxVoxels];        _memoryUsageRAM += (sizeof(GLfloat) * vertexPointsPerVoxel * _maxVoxels);        _readVerticesArray = new GLfloat[vertexPointsPerVoxel * _maxVoxels];        _memoryUsageRAM += (sizeof(GLfloat) * vertexPointsPerVoxel * _maxVoxels);         _writeColorsArray = new GLubyte[vertexPointsPerVoxel * _maxVoxels];        _memoryUsageRAM += (sizeof(GLubyte) * vertexPointsPerVoxel * _maxVoxels);        _readColorsArray = new GLubyte[vertexPointsPerVoxel * _maxVoxels];        _memoryUsageRAM += (sizeof(GLubyte) * vertexPointsPerVoxel * _maxVoxels);          // create our simple fragment shader if we're the first system to init        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();        }    }     _initialized = true;     pthread_mutex_unlock(&_bufferWriteLock);} void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) {    _tree->loadVoxelsFile(fileName, wantColorRandomizer);    setupNewVoxelsForDrawing();} void VoxelSystem::writeToSVOFile(const char* filename, VoxelNode* node) const {    _tree->writeToSVOFile(filename, node);} 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;} long int VoxelSystem::getVoxelsCreated() {    return _tree->voxelsCreated;} float VoxelSystem::getVoxelsCreatedPerSecondAverage() {    return (1 / _tree->voxelsCreatedStats.getEventDeltaAverage());} long int VoxelSystem::getVoxelsColored() {    return _tree->voxelsColored;} float VoxelSystem::getVoxelsColoredPerSecondAverage() {    return (1 / _tree->voxelsColoredStats.getEventDeltaAverage());} long int VoxelSystem::getVoxelsBytesRead() {    return _tree->voxelsBytesRead;} float VoxelSystem::getVoxelsBytesReadPerSecondAverage() {    return _tree->voxelsBytesReadStats.getAverageSampleValuePerSecond();} int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) {     unsigned char command = *sourceBuffer;    int numBytesPacketHeader = numBytesForPacketHeader(sourceBuffer);    unsigned char* voxelData = sourceBuffer + numBytesPacketHeader;     switch(command) {        case PACKET_TYPE_VOXEL_DATA: {            PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),                                    "readBitstreamToTree()");            // ask the VoxelTree to read the bitstream into the tree            ReadBitstreamToTreeParams args(WANT_COLOR, WANT_EXISTS_BITS, NULL, getDataSourceUUID());            lockTree();            _tree->readBitstreamToTree(voxelData, numBytes - numBytesPacketHeader, args);            unlockTree();        }            break;        case PACKET_TYPE_VOXEL_DATA_MONOCHROME: {            PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),                                    "readBitstreamToTree()");            // ask the VoxelTree to read the MONOCHROME bitstream into the tree            ReadBitstreamToTreeParams args(NO_COLOR, WANT_EXISTS_BITS, NULL, getDataSourceUUID());            lockTree();            _tree->readBitstreamToTree(voxelData, numBytes - numBytesPacketHeader, args);            unlockTree();        }            break;        case PACKET_TYPE_Z_COMMAND:             // the Z command is a special command that allows the sender to send high level semantic            // requests, like erase all, or add sphere scene, different receivers may handle these            // messages differently            char* packetData = (char *)sourceBuffer;            char* command = &packetData[numBytesPacketHeader]; // start of the command            int commandLength = strlen(command); // commands are null terminated strings            int totalLength = 1+commandLength+1;             qDebug("got Z message len(%d)= %s\n", numBytes, command);             while (totalLength <= numBytes) {                if (0==strcmp(command,(char*)"erase all")) {                    qDebug("got Z message == erase all - NOT SUPPORTED ON INTERFACE\n");                }                if (0==strcmp(command,(char*)"add scene")) {                    qDebug("got Z message == add scene - NOT SUPPORTED ON INTERFACE\n");                }                totalLength += commandLength+1;            }        break;    }      if (!_useFastVoxelPipeline || _writeRenderFullVBO) {        setupNewVoxelsForDrawing();    } else {        checkForCulling();        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;     checkForCulling(); // check for out of view and deleted voxels...        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->rootNode);        _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    pthread_mutex_lock(&_bufferWriteLock);        if (_voxelsUpdated) {        _voxelsDirty=true;    }     // copy the newly written data to the arrays designated for reading, only does something if _voxelsDirty && _voxelsUpdated    copyWrittenDataToReadArrays(didWriteFullVBO);     pthread_mutex_unlock(&_bufferWriteLock);     uint64_t end = usecTimestampNow();    int elapsedmsec = (end - start) / 1000;    _setupNewVoxelsForDrawingLastFinished = end;    _setupNewVoxelsForDrawingLastElapsed = elapsedmsec;    _inSetupNewVoxelsForDrawing = false;} 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()... pthread_mutex_lock(&_bufferWriteLock);");        pthread_mutex_lock(&_bufferWriteLock);    }     _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;     pthread_mutex_unlock(&_bufferWriteLock);     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();    uint64_t sinceLastViewCulling = (start - _lastViewCulling) / 1000;        bool constantCulling = !Menu::getInstance()->isOptionChecked(MenuOption::DisableConstantCulling);        // If the view frustum is no longer changing, but has changed, since last time, then remove nodes that are out of view    if (constantCulling || (            (sinceLastViewCulling >= std::max((float) _lastViewCullingElapsed, VIEW_CULLING_RATE_IN_MILLISECONDS))            && !isViewChanging()        )    ) {        // When we call removeOutOfView() voxels, we don't actually remove the voxels from the VBOs, but we do remove        // them from tree, this makes our tree caclulations faster, but doesn't require us to fully rebuild the VBOs (which        // can be expensive).        if (!Menu::getInstance()->isOptionChecked(MenuOption::DisableHideOutOfView)) {             // 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;            }         } else if (Menu::getInstance()->isOptionChecked(MenuOption::RemoveOutOfView)) {            _lastViewCulling = start;            removeOutOfView();            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() << "\n";        }        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 \n";        }        _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(VoxelNode* node) {    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 = node->calculateShouldRender(_viewFrustum, voxelSizeScale, boundaryLevelAdjust);     node->setShouldRender(shouldRender);    // let children figure out their renderness    if (!node->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++) {            VoxelNode* childNode = node->getChildAtIndex(i);            if (childNode) {                bool wasShouldRender = childNode->getShouldRender();                voxelsUpdated += newTreeToArrays(childNode);                bool isShouldRender = childNode->getShouldRender();                if (wasShouldRender && !isShouldRender) {                    childrenGotHiddenCount++;                }            }        }        if (childrenGotHiddenCount > 0) {            node->setColorFromAverageOfChildren();        }    }    if (_writeRenderFullVBO) {        const bool DONT_REUSE_INDEX = false;        const bool FORCE_REDRAW = true;        voxelsUpdated += updateNodeInArrays(node, DONT_REUSE_INDEX, FORCE_REDRAW);    } else {        const bool REUSE_INDEX = true;        const bool DONT_FORCE_REDRAW = false;        voxelsUpdated += updateNodeInArrays(node, REUSE_INDEX, DONT_FORCE_REDRAW);    }    node->clearDirtyBit(); // clear the dirty bit, do this before we potentially delete things.        return voxelsUpdated;} // called as response to voxelDeleted() 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(VoxelNode* 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 is make the node invisible!        return 1; // updated!    }    return 0; // not-updated} int VoxelSystem::updateNodeInArrays(VoxelNode* node, bool reuseIndex, bool forceDraw) {    // If we've run out of room, then just bail...    if (_voxelsInWriteArrays >= _maxVoxels && (_freeIndexes.size() == 0)) {

Lines 501–1000 of 2722. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t