2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

abstractpipeline.cpp 9.89 KB
Newer Older
1

2
3
// ================================================================================================
// 
schultezub's avatar
schultezub committed
4
// This file is part of the CAMPVis Software Framework.
5
// 
6
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
8
//      Chair for Computer Aided Medical Procedures
9
10
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
11
// 
schultezub's avatar
schultezub committed
12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
13
// 
14
15
16
17
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
18
// 
19
20
21
22
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
23
24
25
// 
// ================================================================================================

26
#include "abstractpipeline.h"
schultezub's avatar
schultezub committed
27

schultezub's avatar
schultezub committed
28
#include "tgt/exception.h"
29
30
31
32
#include "tgt/glcanvas.h"
#include "tgt/tgt_gl.h"

#include "core/pipeline/visualizationprocessor.h"
33
#include "core/pipeline/abstractprocessor.h"
34
#include "core/tools/job.h"
35
#include "core/tools/opengljobprocessor.h"
36
#include "core/tools/simplejobprocessor.h"
37

38
39
#include <ctime>

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Anonymous OpenGL helper functions
namespace {
    GLboolean getGlBool(GLenum param) {
        GLboolean toReturn;
        glGetBooleanv(param, &toReturn);
        return toReturn;
    };

    GLint getGlInt(GLenum param) {
        GLint toReturn;
        glGetIntegerv(param, &toReturn);
        return toReturn;
    };

    GLfloat getGlFloat(GLenum param) {
        GLfloat toReturn;
        glGetFloatv(param, &toReturn);
        return toReturn;
    }
}

61

schultezub's avatar
schultezub committed
62
63
namespace campvis {
    const std::string AbstractPipeline::loggerCat_ = "CAMPVis.core.datastructures.AbstractPipeline";
64

65
    AbstractPipeline::AbstractPipeline(DataContainer* dc) 
66
        : HasPropertyCollection()
67
68
        , tgt::EventHandler()
        , tgt::EventListener()
69
        , _data(dc)
70
71
72
73
        , _canvas(0)
        , _canvasSize("CanvasSize", "Canvas Size", tgt::ivec2(128, 128), tgt::ivec2(1, 1), tgt::ivec2(4096, 4096))
        , _ignoreCanvasSizeUpdate(false)
        , _renderTargetID("renderTargetID", "Render Target ID", "AbstractPipeline.renderTarget", DataNameProperty::READ)
schultezub's avatar
   
schultezub committed
74
    {
75
76
        tgtAssert(_data != 0, "Pointer to the DataContainer for this pipeline must not be 0!");

77
        _enabled = false;
78

79
80
        addProperty(_renderTargetID);
        addProperty(_canvasSize);
schultezub's avatar
   
schultezub committed
81
82
83
84
85
    }

    AbstractPipeline::~AbstractPipeline() {
    }

86
    void AbstractPipeline::init() {
87
        _renderTargetID.s_changed.connect<AbstractPipeline>(this, &AbstractPipeline::onPropertyChanged);
88
        _data->s_dataAdded.connect(this, &AbstractPipeline::onDataContainerDataAdded);
89

schultezub's avatar
schultezub committed
90
91
        initAllProperties();

92
        // initialize all processors:
schultezub's avatar
schultezub committed
93
94
95
96
97
98
99
100
        for (std::vector<AbstractProcessor*>::iterator it = _processors.begin(); it != _processors.end(); ++it) {
            try {
                (*it)->init();
            }
            catch (tgt::Exception& e) {
                LERROR("Caught Exception during initialization of processor: " << e.what());
            }
        }
101
    }
schultezub's avatar
schultezub committed
102

103
    void AbstractPipeline::deinit() {
schultezub's avatar
schultezub committed
104
105
        deinitAllProperties();

106
107
108
        // deinitialize all processors:
        for (std::vector<AbstractProcessor*>::iterator it = _processors.begin(); it != _processors.end(); ++it) {
            try {
schultezub's avatar
schultezub committed
109
                (*it)->s_invalidated.disconnect(this);
110
111
112
113
114
115
                (*it)->deinit();
            }
            catch (tgt::Exception& e) {
                LERROR("Caught Exception during deinitialization of processor: " << e.what());
            }
        }
116

117
        _data->s_dataAdded.disconnect(this);
118
119
        _renderTargetID.s_changed.disconnect(this);

120
        // clear DataContainer
121
        _data->clear();
122
123
    }

schultezub's avatar
schultezub committed
124
    void AbstractPipeline::onPropertyChanged(const AbstractProperty* prop) {
125
126
127
128
129
130
131
132
133
134
        if (prop == &_renderTargetID) {
            s_renderTargetChanged();
        }
        else if (prop == &_canvasSize && _canvas != 0 && !_ignoreCanvasSizeUpdate) {
            if (_canvasSize.getValue() != _canvas->getSize()) {
                _ignoreCanvasSizeUpdate = true;
                _canvas->setSize(_canvasSize.getValue());
                _ignoreCanvasSizeUpdate = false;
            }
        }
schultezub's avatar
schultezub committed
135
136
137
    }

    const DataContainer& AbstractPipeline::getDataContainer() const {
138
        return *_data;
schultezub's avatar
schultezub committed
139
140
    }

141
    DataContainer& AbstractPipeline::getDataContainer() {
142
        return *_data;
143
144
    }

145
    void AbstractPipeline::executeProcessor(AbstractProcessor* processor) {
146
147
        tgtAssert(processor != 0, "Processor must not be 0.");

148
        // execute processor if needed
149
        if (processor->getEnabled() && !processor->isLocked()) {
150
            if (! processor->isValid()) {
151
152
153
                clock_t startTime = clock();

                try {
154
                    processor->process(*_data);
155
156
157
158
159
160
161
162
163
164
                }
                catch (std::exception& e) {
                    LERROR("Caught unhandled exception while executing processor " << processor->getName() << ": " << e.what());
                }
                catch (...) {
                    LERROR("Caught unhandled exception while executing processor " << processor->getName() << ": unknown exception");
                }

                if (processor->getClockExecutionTime()) {
                    clock_t endTime = clock();
165
                    LINFO("Executed processor " << processor->getName() << " duration: " << (endTime - startTime));
166
                }
167
            }
168
        }
169
170
    }

171
172
173
174
    const std::vector<AbstractProcessor*>& AbstractPipeline::getProcessors() const {
        return _processors;
    }

175
176
177
178
179
180
181
182
    bool AbstractPipeline::getEnabled() const {
        return _enabled;
    }

    void AbstractPipeline::setEnabled(bool enabled) {
        _enabled = enabled;
    }

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    void AbstractPipeline::onEvent(tgt::Event* e) {
        // copy and paste from tgt::EventHandler::onEvent() but without deleting e
        for (size_t i = 0 ; i < listeners_.size() ; ++i) {
            // check if current listener listens to the eventType of e
            if(listeners_[i]->getEventTypes() & e->getEventType() ){
                listeners_[i]->onEvent(e);
                if (e->isAccepted())
                    break;
            }
        }
    }

    void AbstractPipeline::setCanvas(tgt::GLCanvas* canvas) {
        _canvas = canvas;
    }

    void AbstractPipeline::setRenderTargetSize(const tgt::ivec2& size) {
        if (_canvasSize.getValue() != size && !_ignoreCanvasSizeUpdate) {
            _canvasSize.setValue(size);
        }
    }

    const tgt::ivec2& AbstractPipeline::getRenderTargetSize() const {
        return _canvasSize.getValue();
    }

    const std::string& AbstractPipeline::getRenderTargetID() const {
        return _renderTargetID.getValue();
    }

213
    void AbstractPipeline::addProcessor(AbstractProcessor* processor) {
schultezub's avatar
schultezub committed
214
        tgtAssert(processor != 0, "Processor must not be 0.")
215
216
        _processors.push_back(processor);
    }
217

218
219
220
221
    void AbstractPipeline::lockGLContextAndExecuteProcessor(AbstractProcessor* processor) {
        tgtAssert(_canvas != 0, "Set a valid canvas before calling this method!");
        GLJobProc.enqueueJob(
            _canvas, 
222
            makeJobOnHeap<AbstractPipeline, AbstractProcessor*>(this, &AbstractPipeline::executeProcessor, processor),
223
224
225
226
            OpenGLJobProcessor::SerialJob);
    }

    void AbstractPipeline::executeProcessorAndCheckOpenGLState(AbstractProcessor* processor) {
227
        AbstractPipeline::executeProcessor(processor);
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

#ifdef CAMPVIS_DEBUG
        tgtAssert(getGlBool(GL_DEPTH_TEST) == false, "Invalid OpenGL state after processor execution, GL_DEPTH_TEST != false.");
        tgtAssert(getGlBool(GL_SCISSOR_TEST) == false, "Invalid OpenGL state after processor execution, GL_SCISSOR_TEST != false.");

        tgtAssert(getGlInt(GL_CULL_FACE_MODE) == GL_BACK, "Invalid OpenGL state after processor execution, GL_CULL_FACE_MODE != GL_BACk.");
        tgtAssert(getGlInt(GL_DEPTH_FUNC) == GL_LESS, "Invalid OpenGL state after processor execution, GL_DEPTH_FUNC != GL_LESS.");

        tgtAssert(getGlFloat(GL_DEPTH_CLEAR_VALUE) == 1.f, "Invalid OpenGL state after processor execution, GL_DEPTH_CLEAR_VALUE != 1.f.");

        tgtAssert(getGlFloat(GL_RED_SCALE) == 1.f, "Invalid OpenGL state after processor execution, GL_RED_SCALE != 1.f.");
        tgtAssert(getGlFloat(GL_GREEN_SCALE) == 1.f, "Invalid OpenGL state after processor execution, GL_GREEN_SCALE != 1.f.");
        tgtAssert(getGlFloat(GL_BLUE_SCALE) == 1.f, "Invalid OpenGL state after processor execution, GL_BLUE_SCALE != 1.f.");
        tgtAssert(getGlFloat(GL_ALPHA_SCALE) == 1.f, "Invalid OpenGL state after processor execution, GL_ALPHA_SCALE != 1.f.");

        tgtAssert(getGlFloat(GL_RED_BIAS) == 0.f, "Invalid OpenGL state after processor execution, GL_RED_BIAS != 0.f.");
        tgtAssert(getGlFloat(GL_GREEN_BIAS) == 0.f, "Invalid OpenGL state after processor execution, GL_GREEN_BIAS != 0.f.");
        tgtAssert(getGlFloat(GL_BLUE_BIAS) == 0.f, "Invalid OpenGL state after processor execution, GL_BLUE_BIAS != 0.f.");
        tgtAssert(getGlFloat(GL_ALPHA_BIAS) == 0.f, "Invalid OpenGL state after processor execution, GL_ALPHA_BIAS != 0.f.");
#endif
    }

250
    void AbstractPipeline::onDataContainerDataAdded(const std::string& name, DataHandle dh) {
251
252
253
254
255
256
        if (name == _renderTargetID.getValue()) {
            s_renderTargetChanged();
        }
    }


257

258
}