campvisapplication.cpp 10.7 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.
// 
// ================================================================================================

schultezub's avatar
schultezub committed
30
#include "campvisapplication.h"
31
32
33
34

#include "tgt/assert.h"
#include "tgt/exception.h"
#include "tgt/glcontext.h"
35
#include "tgt/gpucapabilities.h"
36
37
38
39
#include "tgt/shadermanager.h"
#include "tgt/qt/qtapplication.h"
#include "tgt/qt/qtthreadedcanvas.h"
#include "tgt/qt/qtcontextmanager.h"
schultezub's avatar
schultezub committed
40
#include "tbb/compat/thread"
41

schultezub's avatar
schultezub committed
42
#ifdef HAS_KISSCL
43
#include "kisscl/clruntime.h"
schultezub's avatar
schultezub committed
44
#endif
45

schultezub's avatar
schultezub committed
46
#include "application/campvispainter.h"
47
#include "application/gui/mainwindow.h"
48
#include "core/tools/opengljobprocessor.h"
49
#include "core/tools/simplejobprocessor.h"
50
#include "core/tools/stringutils.h"
51
#include "core/tools/quadrenderer.h"
52
#include "core/pipeline/abstractpipeline.h"
53
#include "modules/pipelinefactory.h"
54

schultezub's avatar
schultezub committed
55
namespace campvis {
56

57
    const std::string CampVisApplication::loggerCat_ = "CAMPVis.application.CampVisApplication";
58

59
    CampVisApplication::CampVisApplication(int& argc, char** argv, bool useOpenCL) 
60
61
        : QApplication(argc, argv)
        , _localContext(0)
62
        , _mainWindow(0)
63
        , _initialized(false)
64
        , _useOpenCL(useOpenCL)
65
66
67
        , _argc(argc)
        , _argv(argv)
    {
68
69
        // Make Xlib and GLX thread safe under X11
        QApplication::setAttribute(Qt::AA_X11InitThreads);
70

71
72
        _mainWindow = new MainWindow(this);
        tgt::QtContextManager::init();
73
74

        OpenGLJobProcessor::init();
75
        SimpleJobProcessor::init();
76
77
    }

78
79
    CampVisApplication::~CampVisApplication() {
        tgtAssert(_initialized == false, "Destructing initialized CampVisApplication, deinitialize first!");
80
81

        // delete everything in the right order:
82
        for (std::vector< std::pair<AbstractPipeline*, CampVisPainter*> >::iterator it = _visualizations.begin(); it != _visualizations.end(); ++it) {
83
84
85
86
87
            delete it->second;
        }
        for (std::vector<AbstractPipeline*>::iterator it = _pipelines.begin(); it != _pipelines.end(); ++it) {
            delete *it;
        }
88
89
90
        for (std::vector<DataContainer*>::iterator it = _dataContainers.begin(); it != _dataContainers.end(); ++it) {
            delete *it;
        }
91
92
    }

93
94
    void CampVisApplication::init() {
        tgtAssert(_initialized == false, "Tried to initialize CampVisApplication twice.");
95

96
97
98
        // parse argument list and create pipelines
        QStringList pipelinesToAdd = this->arguments();
        for (int i = 1; i < pipelinesToAdd.size(); ++i) {
99
100
            DataContainer* dc = createAndAddDataContainer("DataContainer #" + StringUtils::toString(_dataContainers.size() + 1));
            AbstractPipeline* p = PipelineFactory::getRef().createPipeline(pipelinesToAdd[i].toStdString(), dc);
101
102
103
104
            if (p != 0)
                addPipeline(pipelinesToAdd[i].toStdString(), p);
        }

105
106
107
108
109
110
111
112
        // Init TGT
        tgt::InitFeature::Features featureset = tgt::InitFeature::ALL;
        tgt::init(featureset);
        LogMgr.getConsoleLog()->addCat("", true);

        // create a local OpenGL context and init GL
        _localContext = CtxtMgr.createContext("AppContext", "", tgt::ivec2(16, 16));
        tgtAssert(_localContext != 0, "Could not create local OpenGL context");
113
114
115

        tgt::GLContextScopedLock lock(_localContext->getContext());

116
        tgt::initGL(featureset);
117
        ShdrMgr.setGlobalHeader("#version 330\n");
118
119
        LGL_ERROR;

120
        // ensure matching OpenGL specs
121
        if (GpuCaps.getGlVersion() < tgt::GpuCapabilities::GlVersion::TGT_GL_VERSION_3_3) {
122
            LERROR("Your system does not support OpenGL 3.3, which is mandatory. CAMPVis will probably not work as intended.");
123
        }
124
        if (GpuCaps.getShaderVersion() < tgt::GpuCapabilities::GlVersion::SHADER_VERSION_330) {
125
126
127
128
            LERROR("Your system does not support GLSL Shader Version 3.30, which is mandatory. CAMPVis will probably not work as intended.");
        }
        if (!GpuCaps.isNpotSupported() && !GpuCaps.areTextureRectanglesSupported()) {
            LERROR("Neither non-power-of-two textures nor texture rectangles seem to be supported. CAMPVis will probably not work as intended.");
129
130
        }

131
132
        QuadRenderer::init();

schultezub's avatar
schultezub committed
133
#ifdef HAS_KISSCL
134
135
136
        if (_useOpenCL) {
            kisscl::CLRuntime::init();
        }
schultezub's avatar
schultezub committed
137
#endif
138

139
140
        if (_argc > 0) {
            // ugly hack
schultezub's avatar
schultezub committed
141
            std::string basePath(_argv[0]);
142
143
144
145
146
            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
147
148
149
            ShdrMgr.addPath(basePath);
            ShdrMgr.addPath(basePath + "/core/glsl");

150
151
152
153
154
155
156
157
#ifdef CAMPVIS_SOURCE_DIR
            {
                std::string sourcePath = CAMPVIS_SOURCE_DIR;
                ShdrMgr.addPath(sourcePath);
                ShdrMgr.addPath(sourcePath + "/core/glsl");
            }
#endif

schultezub's avatar
schultezub committed
158
#ifdef HAS_KISSCL
schultezub's avatar
schultezub committed
159
160
161
162
            if (_useOpenCL) {
                CLRtm.addPath(basePath);
                CLRtm.addPath(basePath + "/core/cl");
            }
163
164
165
166
167
168
169
170
171

#ifdef CAMPVIS_SOURCE_DIR
            {
                std::string sourcePath = CAMPVIS_SOURCE_DIR;
                CLRtm.addPath(sourcePath);
                CLRtm.addPath(sourcePath + "/core/glsl");
            }
#endif

schultezub's avatar
schultezub committed
172
#endif
173
174
        }

175
176
        _mainWindow->init();

177
178
179
180
181
182
        // init pipeline first
        for (std::vector<AbstractPipeline*>::iterator it = _pipelines.begin(); it != _pipelines.end(); ++it) {
            (*it)->init();
        }

        // Now init painters:
183
        for (std::vector< std::pair<AbstractPipeline*, CampVisPainter*> >::iterator it = _visualizations.begin(); it != _visualizations.end(); ++it) {
184
185
186
            it->second->init();
        }

187
        GLJobProc.start();
188
189
190
        _initialized = true;
    }

191
192
    void CampVisApplication::deinit() {
        tgtAssert(_initialized, "Tried to deinitialize uninitialized CampVisApplication.");
193

194
195
        GLJobProc.stop();

196
197
198
        {
            // Deinit everything OpenGL related using the local context.
            tgt::GLContextScopedLock lock(_localContext->getContext());
199

200
201
202
203
204
205
            // Deinit pipeline first
            for (std::vector<AbstractPipeline*>::iterator it = _pipelines.begin(); it != _pipelines.end(); ++it) {
                (*it)->deinit();
            }

            // Now deinit painters:
206
            for (std::vector< std::pair<AbstractPipeline*, CampVisPainter*> >::iterator it = _visualizations.begin(); it != _visualizations.end(); ++it) {
207
208
209
                it->second->deinit();
            }

210
211
            _mainWindow->deinit();

212
213
            QuadRenderer::deinit();

schultezub's avatar
schultezub committed
214
#ifdef HAS_KISSCL
215
216
217
            if (_useOpenCL) {
                kisscl::CLRuntime::deinit();
            }
schultezub's avatar
schultezub committed
218
#endif
219
220
221
222


            // deinit OpenGL and tgt
            tgt::deinitGL();
223
224
        }

225
        SimpleJobProcessor::deinit();
226
227
        OpenGLJobProcessor::deinit();

228
229
230
        tgt::QtContextManager::deinit();
        tgt::deinit();

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

234
235
236
        _initialized = false;
    }

237
238
    int CampVisApplication::run() {
        tgtAssert(_initialized, "Tried to run uninitialized CampVisApplication.");
239
240
241
242

        // disconnect OpenGL context from this thread so that the other threads can acquire an OpenGL context.
        CtxtMgr.releaseCurrentContext();

243
244
        _mainWindow->show();

245
246
247
248
249
250
        // Start QApplication
        int toReturn = QApplication::exec();

        return toReturn;
    }

251
    void CampVisApplication::addPipeline(const std::string& name, AbstractPipeline* pipeline) {
252
        tgtAssert(_initialized == false, "Adding pipelines after initialization is currently not supported.");
253
254
        tgtAssert(pipeline != 0, "Pipeline must not be 0.");

255
        // create canvas and painter for the pipeline and connect all together
schultezub's avatar
schultezub committed
256
        tgt::QtThreadedCanvas* canvas = CtxtMgr.createContext(name, "CAMPVis", tgt::ivec2(512, 512));
257
        GLJobProc.registerContext(canvas);
258
        _mainWindow->addVisualizationPipelineWidget(name, canvas);
259
260
        canvas->init();

261
        CampVisPainter* painter = new CampVisPainter(canvas, pipeline);
262
        canvas->setPainter(painter, false);
263
        pipeline->setCanvas(canvas);
264

265
266
        _visualizations.push_back(std::make_pair(pipeline, painter));
        _pipelines.push_back(pipeline);
267
        CtxtMgr.releaseCurrentContext();
268
269

        s_PipelinesChanged();
270
271
    }

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

        _mainWindow->addDockWidget(area, dock);
    }

278
279
280
281
282
    DataContainer* CampVisApplication::createAndAddDataContainer(const std::string& name) {
        DataContainer* dc = new DataContainer(name);
        _dataContainers.push_back(dc);
        s_DataContainersChanged();
        return dc;
283
284
285
    }


286
}