Commit bb1f62f5 authored by Christian Schulte zu Berge's avatar Christian Schulte zu Berge
Browse files

Cleaned up CAMPVis (de-)initialization code:

Introducing free campvis::init() and campvis::deinit() functions that take care of initializing all cgt and campvis-core stuff properly and tearing everything down in the correct order again.
parent e734212b
......@@ -39,6 +39,7 @@
#include "application/gui/mainwindow.h"
#include "application/gui/mdi/mdidockablewindow.h"
#include "core/init.h"
#include "core/tools/simplejobprocessor.h"
#include "core/tools/stringutils.h"
#include "core/tools/quadrenderer.h"
......@@ -74,103 +75,62 @@ namespace campvis {
// Make Xlib and GLX thread safe under X11
QApplication::setAttribute(Qt::AA_X11InitThreads);
sigslot::signal_manager::init();
sigslot::signal_manager::getRef().start();
cgt::GlContextManager::init();
OpenGLJobProcessor::init();
SimpleJobProcessor::init();
QtJobProcessor::init();
}
CampVisApplication::~CampVisApplication() {
cgtAssert(_initialized == false, "Destructing initialized CampVisApplication, deinitialize first!");
sigslot::signal_manager::getRef().stop();
sigslot::signal_manager::deinit();
}
void CampVisApplication::init() {
cgtAssert(_initialized == false, "Tried to initialize CampVisApplication twice.");
// Init CGT
cgt::InitFeature::Features featureset = cgt::InitFeature::ALL;
cgt::init(featureset);
LogMgr.getConsoleLog()->addCat("", true);
_mainWindow = new MainWindow(this);
// create a local OpenGL context and init GL
_localContext = new QtThreadedCanvas("", cgt::ivec2(16, 16));
cgt::GlContextManager::getRef().registerContextAndInitGlew(_localContext, "Local Context");
cgt::initGL(featureset);
ShdrMgr.setDefaultGlslVersion("330");
LGL_ERROR;
QuadRenderer::init();
std::vector<std::string> searchPaths;
if (_argc > 0) {
// ugly hack
std::string basePath(_argv[0]);
basePath = cgt::FileSystem::parentDir(basePath);
ShdrMgr.addPath(basePath);
ShdrMgr.addPath(basePath + "/core/glsl");
basePath = cgt::FileSystem::parentDir(cgt::FileSystem::parentDir(basePath));
ShdrMgr.addPath(basePath);
ShdrMgr.addPath(basePath + "/core/glsl");
searchPaths.push_back(cgt::FileSystem::parentDir(basePath));
searchPaths.push_back(cgt::FileSystem::parentDir(cgt::FileSystem::parentDir(basePath)));
#ifdef CAMPVIS_SOURCE_DIR
{
std::string sourcePath = CAMPVIS_SOURCE_DIR;
ShdrMgr.addPath(sourcePath);
ShdrMgr.addPath(sourcePath + "/core/glsl");
}
searchPaths.push_back(CAMPVIS_SOURCE_DIR);
#endif
}
_mainWindow->init();
// 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() < cgt::GpuCapabilities::GlVersion::CGT_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() < cgt::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.");
}
_localContext = new QtThreadedCanvas("", cgt::ivec2(16, 16));
campvis::init(_localContext, searchPaths);
// load textureData from file
cgt::TextureReaderTga trt;
_errorTexture = trt.loadTexture(ShdrMgr.completePath("application/data/no_input.tga"), cgt::Texture::LINEAR);
_mainWindow = new MainWindow(this);
cgt::GLContextScopedLock lock(_localContext);
{
_mainWindow->init();
// load textureData from file
cgt::TextureReaderTga trt;
_errorTexture = trt.loadTexture(ShdrMgr.completePath("application/data/no_input.tga"), cgt::Texture::LINEAR);
#ifdef CAMPVIS_HAS_SCRIPTING
// create and store Lua VM for this very pipeline
_luaVmState = new LuaVmState();
_luaVmState->redirectLuaPrint();
// Let Lua know where CAMPVis modules are located
if (! _luaVmState->execString("package.cpath = '" CAMPVIS_LUA_MODS_PATH "'"))
LERROR("Error setting up Lua VM.");
if (! _luaVmState->execString("package.path = package.path .. ';" CAMPVIS_LUA_SCRIPTS_PATH "'"))
LERROR("Error setting up Lua VM.");
// Load CAMPVis' core Lua module to have SWIG glue for AutoEvaluationPipeline available
if (! _luaVmState->execString("require(\"campvis\")"))
LERROR("Error setting up Lua VM.");
if (! _luaVmState->execString("require(\"cgt\")"))
LERROR("Error setting up Lua VM.");
if (! _luaVmState->execString("pipelines = {}"))
LERROR("Error setting up Lua VM.");
if (! _luaVmState->execString("inspect = require 'inspect'"))
LERROR("Error setting up Lua VM.");
// create and store Lua VM for this very pipeline
_luaVmState = new LuaVmState();
_luaVmState->redirectLuaPrint();
// Let Lua know where CAMPVis modules are located
if (! _luaVmState->execString("package.cpath = '" CAMPVIS_LUA_MODS_PATH "'"))
LERROR("Error setting up Lua VM.");
if (! _luaVmState->execString("package.path = package.path .. ';" CAMPVIS_LUA_SCRIPTS_PATH "'"))
LERROR("Error setting up Lua VM.");
// Load CAMPVis' core Lua module to have SWIG glue for AutoEvaluationPipeline available
if (! _luaVmState->execString("require(\"campvis\")"))
LERROR("Error setting up Lua VM.");
if (! _luaVmState->execString("require(\"cgt\")"))
LERROR("Error setting up Lua VM.");
if (! _luaVmState->execString("pipelines = {}"))
LERROR("Error setting up Lua VM.");
if (! _luaVmState->execString("inspect = require 'inspect'"))
LERROR("Error setting up Lua VM.");
#endif
GLCtxtMgr.releaseContext(_localContext, false);
}
// parse argument list and create pipelines
QStringList pipelinesToAdd = this->arguments();
......@@ -207,10 +167,7 @@ namespace campvis {
addPipeline(pipelinesToAdd[i].toStdString(), p);
}
}
GLJobProc.setContext(_localContext);
GLJobProc.start();
_initialized = true;
}
......@@ -237,7 +194,6 @@ namespace campvis {
}
_mainWindow->deinit();
QuadRenderer::deinit();
}
// now delete everything in the right order:
......@@ -248,21 +204,8 @@ namespace campvis {
delete *it;
}
{
// Deinit everything OpenGL using the local context.
cgt::GLContextScopedLock lock(_localContext);
cgt::deinitGL();
}
GLJobProc.stop();
OpenGLJobProcessor::deinit();
SimpleJobProcessor::deinit();
cgt::GlContextManager::deinit();
cgt::deinit();
campvis::deinit();
PropertyWidgetFactory::deinit();
ImageRepresentationConverter::deinit();
PipelineFactory::deinit();
_initialized = false;
......@@ -319,8 +262,6 @@ namespace campvis {
pipeline->init();
LGL_ERROR;
canvas->getPainter()->init();
LGL_ERROR;
// enable pipeline and invalidate all processors
pipeline->setEnabled(true);
......
......@@ -6,6 +6,7 @@ MESSAGE(STATUS "Configuring CAMPVis Core")
# glob sources from core directories
FILE(GLOB CampvisCoreSources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
*.cpp
classification/*.cpp
datastructures/*.cpp
eventhandlers/*.cpp
......
// ================================================================================================
//
// This file is part of the CAMPVis Software Framework.
//
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
// Christian Schulte zu Berge <christian.szb@in.tum.de>
// Chair for Computer Aided Medical Procedures
// Technische Universitaet Muenchen
// Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
//
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
//
// 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
//
// 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.
//
// ================================================================================================
#include "init.h"
#include "sigslot/sigslot.h"
#include "cgt/init.h"
#include "cgt/glcontextmanager.h"
#include "cgt/logmanager.h"
#include "cgt/opengljobprocessor.h"
#include "cgt/shadermanager.h"
#include "cgt/qt/qtthreadedcanvas.h"
#include "core/datastructures/imagerepresentationconverter.h"
#include "core/tools/quadrenderer.h"
namespace campvis {
void init(cgt::GLCanvas* backgroundGlContext, const std::vector<std::string>& searchPaths) {
// start sigslot signal manager
sigslot::signal_manager::init();
sigslot::signal_manager::getRef().start();
// Init CGT
cgt::init(cgt::InitFeature::ALL, cgt::Debug);
cgt::initGL(backgroundGlContext, cgt::InitFeature::ALL);
// ensure matching OpenGL specs
LINFOC("CAMPVis.core.init", "Using Graphics Hardware " << GpuCaps.getVendorAsString() << " " << GpuCaps.getGlRendererString() << " on " << GpuCaps.getOSVersionString());
LINFOC("CAMPVis.core.init", "Supported OpenGL " << GpuCaps.getGlVersion() << ", GLSL " << GpuCaps.getShaderVersion());
if (GpuCaps.getGlVersion() < cgt::GpuCapabilities::GlVersion::CGT_GL_VERSION_3_3) {
LERRORC("CAMPVis.core.init", "Your system does not support OpenGL 3.3, which is mandatory. CAMPVis will probably not work as intended.");
}
if (GpuCaps.getShaderVersion() < cgt::GpuCapabilities::GlVersion::SHADER_VERSION_330) {
LERRORC("CAMPVis.core.init", "Your system does not support GLSL Shader Version 3.30, which is mandatory. CAMPVis will probably not work as intended.");
}
ShdrMgr.setDefaultGlslVersion("330");
for (auto it = searchPaths.cbegin(); it != searchPaths.cend(); ++it) {
ShdrMgr.addPath(*it);
ShdrMgr.addPath(*it + "/core/glsl");
}
QuadRenderer::init();
LGL_ERROR;
GLCtxtMgr.releaseContext(backgroundGlContext, false);
GLJobProc.setContext(backgroundGlContext);
GLJobProc.start();
}
void deinit() {
{
// Deinit everything OpenGL related using the background context.
cgt::GLContextScopedLock lock(GLJobProc.getContext());
QuadRenderer::deinit();
}
cgt::deinitGL();
cgt::deinit();
ImageRepresentationConverter::deinit();
//PipelineFactory::deinit();
}
}
// ================================================================================================
//
// This file is part of the CAMPVis Software Framework.
//
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
// Christian Schulte zu Berge <christian.szb@in.tum.de>
// Chair for Computer Aided Medical Procedures
// Technische Universitaet Muenchen
// Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
//
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
//
// 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
//
// 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.
//
// ================================================================================================
#ifndef CAMPVIS_H__
#define CAMPVIS_H__
#include "cgt/singleton.h"
#include "core/coreapi.h"
#include <string>
#include <vector>
namespace cgt {
class GLCanvas;
}
namespace campvis {
/**
* Initializes the CAMPVis core stuff.
* This includes all cgt, sigslot, and campvis singletons. Make sure to call this function
* before using any other CAMPVis methods/classes.
* \param backgroundGlContext OpenGL context to use by CAMPVis for background OpenGL tasks. Must not be 0.
* \param searchPaths Seach paths to use for shaders.
*/
CAMPVIS_CORE_API void init(cgt::GLCanvas* backgroundGlContext, const std::vector<std::string>& searchPaths = std::vector<std::string>());
/**
*
*/
CAMPVIS_CORE_API void deinit();
}
#endif // CAMPVIS_H__
\ No newline at end of file
......@@ -27,20 +27,22 @@
**********************************************************************/
#include "cgt/init.h"
#include "cgt/cgt_gl.h"
#include "cgt/assert.h"
#include "cgt/singleton.h"
#include "cgt/glcanvas.h"
#include "cgt/glcontextmanager.h"
#include "cgt/gpucapabilities.h"
#ifdef _MSC_VER
#include "cgt/gpucapabilitieswindows.h"
#endif
#include "cgt/opengljobprocessor.h"
#include "cgt/shadermanager.h"
#include "cgt/event/eventhandler.h"
#include "cgt/singleton.h"
#include "cgt/texturereadertga.h"
#include "cgt/event/eventhandler.h"
#ifdef CGT_HAS_DEVIL
#include <IL/il.h>
#include <IL/ilu.h>
......@@ -86,7 +88,7 @@ void deinit() {
LogManager::deinit();
}
void initGL(InitFeature::Features featureset) {
void initGL(GLCanvas* backgroundGlContext, InitFeature::Features featureset) {
if (featureset & InitFeature::SHADER_MANAGER) {
featureset = (InitFeature::Features) (featureset | InitFeature::GPU_PROPERTIES | InitFeature::FILE_SYSTEM);
}
......@@ -94,15 +96,10 @@ void initGL(InitFeature::Features featureset) {
featureset = (InitFeature::Features) (featureset | InitFeature::GPU_PROPERTIES | InitFeature::FILE_SYSTEM);
}
GLenum err = glewInit();
if (err != GLEW_OK) {
// Problem: glewInit failed, something is seriously wrong.
cgtAssert(false, "glewInit failed");
std::cerr << "glewInit failed, error: " << glewGetErrorString(err) << std::endl;
exit(EXIT_FAILURE);
}
LINFOC("cgt.init", "GLEW version: " << glewGetString(GLEW_VERSION));
// init and register background context
GlContextManager::init();
OpenGLJobProcessor::init();
GlContextManager::getRef().registerContextAndInitGlew(backgroundGlContext, "CGT Background Context");
if (featureset & InitFeature::GPU_PROPERTIES )
GpuCapabilities::init();
......@@ -115,14 +112,24 @@ void initGL(InitFeature::Features featureset) {
}
void deinitGL() {
if (GpuCapabilities::isInited())
GpuCapabilities::deinit();
GLCanvas* backgroundGlContext = GLJobProc.getContext();
{
// Deinit everything OpenGL related using the local context.
GLContextScopedLock lock(backgroundGlContext);
if (GpuCapabilities::isInited())
GpuCapabilities::deinit();
#ifdef _MSC_VER
if (GpuCapabilitiesWindows::isInited())
GpuCapabilitiesWindows::deinit();
if (GpuCapabilitiesWindows::isInited())
GpuCapabilitiesWindows::deinit();
#endif
if (ShaderManager::isInited())
ShaderManager::deinit();
if (ShaderManager::isInited())
ShaderManager::deinit();
}
GLJobProc.stop();
OpenGLJobProcessor::deinit();
GlContextManager::deinit();
}
} // namespace
......@@ -33,6 +33,7 @@
#include "cgt/logmanager.h"
namespace cgt {
class GLCanvas;
class CGT_API InitFeature {
public:
......@@ -59,7 +60,7 @@ public:
CGT_API void init(InitFeature::Features featureset = InitFeature::ALL, LogLevel logLevel = Info);
/// init GLEW and OpenGL-dependent singletons of cgt.
/// to be called when OpenGL context already exists.
CGT_API void initGL(InitFeature::Features featureset = InitFeature::ALL);
CGT_API void initGL(GLCanvas* backgroundGlContext, InitFeature::Features featureset = InitFeature::ALL);
/// deinit the singletons of cgt
CGT_API void deinit();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment