abstractpipeline.cpp 11.9 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
5
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
8
9
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

30
#include "abstractpipeline.h"
schultezub's avatar
schultezub committed
31

schultezub's avatar
schultezub committed
32
#include "tgt/exception.h"
33
34
35
36
37
#include "tgt/glcanvas.h"
#include "tgt/glcontext.h"
#include "tgt/tgt_gl.h"

#include "core/pipeline/visualizationprocessor.h"
38
#include "core/pipeline/abstractprocessor.h"
39
#include "core/tools/job.h"
40
#include "core/tools/opengljobprocessor.h"
41
#include "core/tools/simplejobprocessor.h"
42

43
44
#include <ctime>

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 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;
    }
}

66

schultezub's avatar
schultezub committed
67
68
namespace campvis {
    const std::string AbstractPipeline::loggerCat_ = "CAMPVis.core.datastructures.AbstractPipeline";
69

70
    AbstractPipeline::AbstractPipeline(DataContainer* dc) 
71
        : HasPropertyCollection()
72
73
        , tgt::EventHandler()
        , tgt::EventListener()
74
        , _data(dc)
75
76
77
78
        , _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
79
    {
80
81
        tgtAssert(_data != 0, "Pointer to the DataContainer for this pipeline must not be 0!");

82
        _enabled = true;
83
84
85

        addProperty(&_renderTargetID);
        addProperty(&_canvasSize);
schultezub's avatar
   
schultezub committed
86
87
88
89
90
    }

    AbstractPipeline::~AbstractPipeline() {
    }

91
    void AbstractPipeline::init() {
92
        _renderTargetID.s_changed.connect<AbstractPipeline>(this, &AbstractPipeline::onPropertyChanged);
93
        _data->s_dataAdded.connect(this, &AbstractPipeline::onDataContainerDataAdded);
94

schultezub's avatar
schultezub committed
95
96
        initAllProperties();

97
        // initialize all processors:
schultezub's avatar
schultezub committed
98
99
100
101
102
103
104
105
        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());
            }
        }
106
    }
schultezub's avatar
schultezub committed
107

108
    void AbstractPipeline::deinit() {
schultezub's avatar
schultezub committed
109
110
        deinitAllProperties();

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

122
        _data->s_dataAdded.disconnect(this);
123
124
        _renderTargetID.s_changed.disconnect(this);

125
        // clear DataContainer
126
        _data->clear();
127
128
    }

schultezub's avatar
schultezub committed
129
    void AbstractPipeline::onPropertyChanged(const AbstractProperty* prop) {
130
131
132
133
134
135
136
137
138
139
140
141
142
        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;
            }
        }
        else {
            HasPropertyCollection::onPropertyChanged(prop);
        }
schultezub's avatar
schultezub committed
143
144
145
    }

    const DataContainer& AbstractPipeline::getDataContainer() const {
146
        return *_data;
schultezub's avatar
schultezub committed
147
148
    }

149
    DataContainer& AbstractPipeline::getDataContainer() {
150
        return *_data;
151
152
    }

153
    void AbstractPipeline::executeProcessor(AbstractProcessor* processor, bool unlockInExtraThred) {
154
155
        tgtAssert(processor != 0, "Processor must not be 0.");

156
157
158
        if (processor->getEnabled() && !processor->isLocked()) {
            // update properties if they're invalid
            if (processor->hasInvalidProperties()) {
159
                processor->updateProperties(*_data);
160
161
162
163
#if CAMPVIS_DEBUG
                if (processor->hasInvalidProperties())
                    LDEBUG("Processor " << processor->getName() << " still has INVALID_PROPERTIES level. Did you forget to validate the processor in updateProperties()?");
#endif
164
165
            }

166
167
168
169
170
171
            // execute processor if needed
            if (processor->hasInvalidResult()) {
                processor->lockProcessor();
                clock_t startTime = clock();

                try {
172
                    processor->process(*_data);
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
                }
                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();
                    LDEBUG("Executed processor " << processor->getName() << " duration: " << (endTime - startTime));
                }

                // Unlocking processors might be expensive, since a long chain of invalidations might be started
                // -> do this in another thread...
                if (unlockInExtraThred)
                    SimpleJobProc.enqueueJob(makeJob(processor, &AbstractProcessor::unlockProcessor));
                else
                    processor->unlockProcessor();
192
            }
193
        }
194
195
    }

196
197
198
199
    const std::vector<AbstractProcessor*>& AbstractPipeline::getProcessors() const {
        return _processors;
    }

200
201
202
203
204
205
206
207
    bool AbstractPipeline::getEnabled() const {
        return _enabled;
    }

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

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    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();
    }

238
    void AbstractPipeline::addProcessor(AbstractProcessor* processor) {
schultezub's avatar
schultezub committed
239
        tgtAssert(processor != 0, "Processor must not be 0.")
240
241
        _processors.push_back(processor);
    }
242

243
244
245
246
247
248
249
250
251
252
    void AbstractPipeline::lockAllProcessors() {
        for (std::vector<AbstractProcessor*>::iterator it = _processors.begin(); it != _processors.end(); ++it)
            (*it)->lockProcessor();
    }

    void AbstractPipeline::unlockAllProcessors() {
        for (std::vector<AbstractProcessor*>::iterator it = _processors.begin(); it != _processors.end(); ++it)
            (*it)->unlockProcessor();
    }

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    void AbstractPipeline::lockGLContextAndExecuteProcessor(AbstractProcessor* processor) {
        tgtAssert(_canvas != 0, "Set a valid canvas before calling this method!");
        GLJobProc.enqueueJob(
            _canvas, 
            makeJobOnHeap<AbstractPipeline, AbstractProcessor*, bool>(this, &AbstractPipeline::executeProcessor, processor, true),
            OpenGLJobProcessor::SerialJob);
    }

    void AbstractPipeline::executeProcessorAndCheckOpenGLState(AbstractProcessor* processor) {
        AbstractPipeline::executeProcessor(processor, true);

#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
    }

    void AbstractPipeline::onDataContainerDataAdded(const std::string& name, const DataHandle& dh) {
        if (name == _renderTargetID.getValue()) {
            s_renderTargetChanged();
        }
    }


292

293
}