The expiration time for new job artifacts in CI/CD pipelines is now 30 days (GitLab default). Previously generated artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

campvisapplication.cpp 12.2 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-2013, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// 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
17
// 
18
19
20
21
// 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.
22
23
24
// 
// ================================================================================================

schultezub's avatar
schultezub committed
25
#include "campvisapplication.h"
26
27
28

#include "tgt/assert.h"
#include "tgt/exception.h"
29
#include "tgt/glcanvas.h"
30
#include "tgt/gpucapabilities.h"
31
32
33
34
#include "tgt/shadermanager.h"
#include "tgt/qt/qtapplication.h"
#include "tgt/qt/qtthreadedcanvas.h"
#include "tgt/qt/qtcontextmanager.h"
schultezub's avatar
schultezub committed
35
#include "tbb/compat/thread"
36

schultezub's avatar
schultezub committed
37
#include "application/campvispainter.h"
38
#include "application/gui/mainwindow.h"
39
#include "core/tools/job.h"
40
#include "core/tools/opengljobprocessor.h"
41
#include "core/tools/simplejobprocessor.h"
42
#include "core/tools/stringutils.h"
43
#include "core/tools/quadrenderer.h"
44
#include "core/pipeline/abstractpipeline.h"
45
#include "core/pipeline/visualizationprocessor.h"
46
#include "modules/pipelinefactory.h"
47

schultezub's avatar
schultezub committed
48
namespace campvis {
49

50
    const std::string CampVisApplication::loggerCat_ = "CAMPVis.application.CampVisApplication";
51

52
    CampVisApplication::CampVisApplication(int& argc, char** argv) 
53
54
        : QApplication(argc, argv)
        , _localContext(0)
55
        , _mainWindow(0)
56
57
58
59
        , _initialized(false)
        , _argc(argc)
        , _argv(argv)
    {
60
61
        // Make Xlib and GLX thread safe under X11
        QApplication::setAttribute(Qt::AA_X11InitThreads);
62

63
64
        _mainWindow = new MainWindow(this);
        tgt::QtContextManager::init();
65
66

        OpenGLJobProcessor::init();
67
        SimpleJobProcessor::init();
68
69
    }

70
71
    CampVisApplication::~CampVisApplication() {
        tgtAssert(_initialized == false, "Destructing initialized CampVisApplication, deinitialize first!");
72
73

        // delete everything in the right order:
74
        for (std::vector< std::pair<AbstractPipeline*, CampVisPainter*> >::iterator it = _visualizations.begin(); it != _visualizations.end(); ++it) {
75
76
77
78
79
            delete it->second;
        }
        for (std::vector<AbstractPipeline*>::iterator it = _pipelines.begin(); it != _pipelines.end(); ++it) {
            delete *it;
        }
80
81
82
        for (std::vector<DataContainer*>::iterator it = _dataContainers.begin(); it != _dataContainers.end(); ++it) {
            delete *it;
        }
83
84
    }

85
86
    void CampVisApplication::init() {
        tgtAssert(_initialized == false, "Tried to initialize CampVisApplication twice.");
87

88
89
90
        // parse argument list and create pipelines
        QStringList pipelinesToAdd = this->arguments();
        for (int i = 1; i < pipelinesToAdd.size(); ++i) {
91
92
            DataContainer* dc = createAndAddDataContainer("DataContainer #" + StringUtils::toString(_dataContainers.size() + 1));
            AbstractPipeline* p = PipelineFactory::getRef().createPipeline(pipelinesToAdd[i].toStdString(), dc);
93
94
95
96
            if (p != 0)
                addPipeline(pipelinesToAdd[i].toStdString(), p);
        }

97
98
99
100
101
102
        // Init TGT
        tgt::InitFeature::Features featureset = tgt::InitFeature::ALL;
        tgt::init(featureset);
        LogMgr.getConsoleLog()->addCat("", true);

        // create a local OpenGL context and init GL
103
        _localContext = tgt::GlContextManager::getRef().createContext("AppContext", "", tgt::ivec2(16, 16));
104
        tgtAssert(_localContext != 0, "Could not create local OpenGL context");
105

106
        tgt::GLContextScopedLock lock(_localContext);
107

108
        tgt::initGL(featureset);
109
        ShdrMgr.setDefaultGlslVersion("330");
110
111
        LGL_ERROR;

112
        QuadRenderer::init();
113
        
114
115
        if (_argc > 0) {
            // ugly hack
schultezub's avatar
schultezub committed
116
            std::string basePath(_argv[0]);
117
118
119
120
121
            basePath = tgt::FileSystem::parentDir(basePath);
            ShdrMgr.addPath(basePath);
            ShdrMgr.addPath(basePath + "/core/glsl");

            basePath = tgt::FileSystem::parentDir(tgt::FileSystem::parentDir(basePath));
schultezub's avatar
schultezub committed
122
123
124
            ShdrMgr.addPath(basePath);
            ShdrMgr.addPath(basePath + "/core/glsl");

125
126
127
128
129
130
#ifdef CAMPVIS_SOURCE_DIR
            {
                std::string sourcePath = CAMPVIS_SOURCE_DIR;
                ShdrMgr.addPath(sourcePath);
                ShdrMgr.addPath(sourcePath + "/core/glsl");
            }
schultezub's avatar
schultezub committed
131
#endif
132
133
        }

134
135
        _mainWindow->init();

136
137
138
139
140
141
142
143
144
145
        // ensure matching OpenGL specs
        LINFO("Using Graphics Hardware " << GpuCaps.getVendorAsString() << " " << GpuCaps.getGlRendererString() << " on " << GpuCaps.getOSVersionString());
        LINFO("Supported OpenGL " << GpuCaps.getGlVersion() << ", GLSL " << GpuCaps.getShaderVersion());
        if (GpuCaps.getGlVersion() < tgt::GpuCapabilities::GlVersion::TGT_GL_VERSION_3_3) {
            LERROR("Your system does not support OpenGL 3.3, which is mandatory. CAMPVis will probably not work as intended.");
        }
        if (GpuCaps.getShaderVersion() < tgt::GpuCapabilities::GlVersion::SHADER_VERSION_330) {
            LERROR("Your system does not support GLSL Shader Version 3.30, which is mandatory. CAMPVis will probably not work as intended.");
        }

146
147
148
149
150
151
        // init pipeline first
        for (std::vector<AbstractPipeline*>::iterator it = _pipelines.begin(); it != _pipelines.end(); ++it) {
            (*it)->init();
        }

        // Now init painters:
152
        for (std::vector< std::pair<AbstractPipeline*, CampVisPainter*> >::iterator it = _visualizations.begin(); it != _visualizations.end(); ++it) {
153
154
155
            it->second->init();
        }

156
        GLJobProc.start();
157
        GLJobProc.registerContext(_localContext);
158
159
160
        _initialized = true;
    }

161
162
    void CampVisApplication::deinit() {
        tgtAssert(_initialized, "Tried to deinitialize uninitialized CampVisApplication.");
163

164
165
        GLJobProc.stop();

166
167
        {
            // Deinit everything OpenGL related using the local context.
168
            tgt::GLContextScopedLock lock(_localContext);
169

170
171
172
173
174
175
            // Deinit pipeline first
            for (std::vector<AbstractPipeline*>::iterator it = _pipelines.begin(); it != _pipelines.end(); ++it) {
                (*it)->deinit();
            }

            // Now deinit painters:
176
            for (std::vector< std::pair<AbstractPipeline*, CampVisPainter*> >::iterator it = _visualizations.begin(); it != _visualizations.end(); ++it) {
177
178
179
                it->second->deinit();
            }

180
            _mainWindow->deinit();
181
182
            QuadRenderer::deinit();

183
184
            // deinit OpenGL and tgt
            tgt::deinitGL();
185
186
        }

187
        SimpleJobProcessor::deinit();
188
        OpenGLJobProcessor::deinit();
189
        PipelineFactory::deinit();
190

191
192
193
        tgt::QtContextManager::deinit();
        tgt::deinit();

194
        // MainWindow dtor needs a valid CampVisApplication, so we need to call it here instead of during destruction.
schultezub's avatar
schultezub committed
195
196
        delete _mainWindow;

197
198
199
        _initialized = false;
    }

200
201
    int CampVisApplication::run() {
        tgtAssert(_initialized, "Tried to run uninitialized CampVisApplication.");
202
203

        // disconnect OpenGL context from this thread so that the other threads can acquire an OpenGL context.
204
        tgt::GlContextManager::getRef().releaseCurrentContext();
205

206
207
        _mainWindow->show();

208
209
210
211
212
213
        // Start QApplication
        int toReturn = QApplication::exec();

        return toReturn;
    }

214
    void CampVisApplication::addPipeline(const std::string& name, AbstractPipeline* pipeline) {
215
216
        tgtAssert(pipeline != 0, "Pipeline must not be 0.");

217
218
219
220
        // if CAMPVis is already fully initialized, we need to temporarily shut down its
        // OpenGL job processor, since we need to create a new context.
        if (_initialized) {
            GLJobProc.pause();
221
            {
222
223
224
                tgt::QtThreadedCanvas* canvas = dynamic_cast<tgt::QtThreadedCanvas*>(tgt::GlContextManager::getRef().createContext(name, "CAMPVis", tgt::ivec2(512, 512)));
                tgtAssert(canvas != 0, "Dynamic cast failed. This should not be the case, since we initialized the GlContextManager singleton with a QtContextManager.");
                tgt::GLContextScopedLock lock(canvas);
225
226
227
                addPipelineImpl(canvas, name, pipeline);
            }
            GLJobProc.resume();
228
        }
229
        else {
230
231
            tgt::QtThreadedCanvas* canvas = dynamic_cast<tgt::QtThreadedCanvas*>(tgt::GlContextManager::getRef().createContext(name, "CAMPVis", tgt::ivec2(512, 512)));
            tgtAssert(canvas != 0, "Dynamic cast failed. This should not be the case, since we initialized the GlContextManager singleton with a QtContextManager.");
232
233
234
235
236
            addPipelineImpl(canvas, name, pipeline);
        }

        s_PipelinesChanged();
    }
237

238
    void CampVisApplication::addPipelineImpl(tgt::QtThreadedCanvas* canvas, const std::string& name, AbstractPipeline* pipeline) {
239
        // create canvas and painter for the pipeline and connect all together
240
        
241
        GLJobProc.registerContext(canvas);
242
243
        canvas->init();

244
        CampVisPainter* painter = new CampVisPainter(canvas, pipeline);
245
        canvas->setPainter(painter, false);
246
        pipeline->setCanvas(canvas);
247

248
249
250
        _visualizations.push_back(std::make_pair(pipeline, painter));
        _pipelines.push_back(pipeline);

251
        if (_initialized) {
252
            LGL_ERROR;
253
            pipeline->init();
254
            LGL_ERROR;
255
            painter->init();
256
            LGL_ERROR;
257
258
        }

259
        tgt::GlContextManager::getRef().releaseCurrentContext();
260
261
262
263
264
265
266
        _mainWindow->addVisualizationPipelineWidget(name, canvas);

        // enable pipeline and invalidate all processors
        pipeline->setEnabled(true);
        for (std::vector<AbstractProcessor*>::const_iterator it = pipeline->getProcessors().begin(); it != pipeline->getProcessors().end(); ++it) {
            (*it)->invalidate(AbstractProcessor::INVALID_RESULT);
        }
267
268
    }

269
270
271
272
273
274
    void CampVisApplication::registerDockWidget(Qt::DockWidgetArea area, QDockWidget* dock) {
        tgtAssert(dock != 0, "Dock widget must not be 0.");

        _mainWindow->addDockWidget(area, dock);
    }

275
276
277
278
279
    DataContainer* CampVisApplication::createAndAddDataContainer(const std::string& name) {
        DataContainer* dc = new DataContainer(name);
        _dataContainers.push_back(dc);
        s_DataContainersChanged();
        return dc;
280
281
    }

282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    void CampVisApplication::rebuildAllShadersFromFiles() {
        // rebuilding all shaders has to be done from OpenGL context, use the local one.
        GLJobProc.enqueueJob(_localContext, makeJobOnHeap(this, &CampVisApplication::triggerShaderRebuild), OpenGLJobProcessor::SerialJob);
    }

    void CampVisApplication::triggerShaderRebuild() {

        if (! ShdrMgr.rebuildAllShadersFromFile()) {
            LERROR("Could not rebuild all shaders from file.");
            return;
        }

        for (std::vector<AbstractPipeline*>::iterator it = _pipelines.begin(); it != _pipelines.end(); ++it) {
            for (std::vector<AbstractProcessor*>::const_iterator pit = (*it)->getProcessors().begin(); pit != (*it)->getProcessors().end(); ++pit) {
                if (VisualizationProcessor* tester = dynamic_cast<VisualizationProcessor*>(*pit)) {
                	tester->invalidate(AbstractProcessor::INVALID_RESULT);
                }
            }
        }
    }

303

304
}