diff --git a/CMakeLists.txt b/CMakeLists.txt index 4079e3ab15..ff62ae9684 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,7 @@ set(DEPENDENT_LIBS ${DEPENDENT_LIBS} ${SGCT_LIBRARIES}) # GLM set(GLM_ROOT_DIR "${GHOUL_ROOT_DIR}/ext/glm") find_package(GLM REQUIRED) +add_definitions(-DGLM_SWIZZLE) include_directories(${GLM_INCLUDE_DIRS}) # GLEW diff --git a/include/openspace/rendering/renderablevolumecl.h b/include/openspace/rendering/renderablevolumecl.h index cb7ab38790..43d5ad71a0 100644 --- a/include/openspace/rendering/renderablevolumecl.h +++ b/include/openspace/rendering/renderablevolumecl.h @@ -1,105 +1,106 @@ -/***************************************************************************************** - * * - * OpenSpace * - * * - * Copyright (c) 2014 * - * * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this * - * software and associated documentation files (the "Software"), to deal in the Software * - * without restriction, including without limitation the rights to use, copy, modify, * - * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * - * permit persons to whom the Software is furnished to do so, subject to the following * - * conditions: * - * * - * The above copyright notice and this permission notice shall be included in all copies * - * or substantial portions of the Software. * - * * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * - ****************************************************************************************/ - -#ifndef __RENDERABLEVOLUMECL_H__ -#define __RENDERABLEVOLUMECL_H__ - -// open space includes -#include - -// ghoul includes -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define SGCT_WINDOWS_INCLUDE -#include - -namespace sgct_utils { - class SGCTBox; -} - -//#include -//#include -#ifdef __APPLE__ - #include -#else - #include -#endif - -namespace openspace { - -class RenderableVolumeCL: public RenderableVolume { -public: - - // constructors & destructor - RenderableVolumeCL(const ghoul::Dictionary& dictionary); - ~RenderableVolumeCL(); - - bool initialize(); - bool deinitialize(); - - virtual void render(const Camera *camera, const psc& thisPosition); - virtual void update(); - -private: - - void safeKernelCompilation(); - - std::string _filename; - ghoul::RawVolumeReader::ReadHints _hints; - float _stepSize; - ghoul::opengl::FramebufferObject* _fbo; - ghoul::opengl::Texture* _backTexture; - ghoul::opengl::Texture* _frontTexture; - ghoul::opengl::Texture* _volume; - ghoul::opengl::Texture* _output; - ghoul::opengl::ProgramObject *_fboProgram; - ghoul::opengl::ProgramObject *_quadProgram; - sgct_utils::SGCTBox* _boundingBox; - GLuint _screenQuad; - - ghoul::opencl::CLContext _context; - ghoul::opencl::CLCommandQueue _commands; - ghoul::opencl::CLProgram _program; - ghoul::opencl::CLKernel _kernel; - ghoul::opencl::CLWorkSize* _ws; - cl_mem _clBackTexture, _clFrontTexture, _clVolume, _clOutput; - - ghoul::filesystem::File* _kernelSourceFile; - bool _kernelUpdateOnSave; - std::mutex* _kernelMutex; - -}; - -} // namespace openspace - -#endif \ No newline at end of file +// This is still in the repository to be cannibalized for a possible rewrite (ab) +///***************************************************************************************** +// * * +// * OpenSpace * +// * * +// * Copyright (c) 2014 * +// * * +// * Permission is hereby granted, free of charge, to any person obtaining a copy of this * +// * software and associated documentation files (the "Software"), to deal in the Software * +// * without restriction, including without limitation the rights to use, copy, modify, * +// * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +// * permit persons to whom the Software is furnished to do so, subject to the following * +// * conditions: * +// * * +// * The above copyright notice and this permission notice shall be included in all copies * +// * or substantial portions of the Software. * +// * * +// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +// * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +// * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +// * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +// * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +// * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +// ****************************************************************************************/ +// +//#ifndef __RENDERABLEVOLUMECL_H__ +//#define __RENDERABLEVOLUMECL_H__ +// +//// open space includes +//#include +// +//// ghoul includes +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +// +//#define SGCT_WINDOWS_INCLUDE +//#include +// +//namespace sgct_utils { +// class SGCTBox; +//} +// +////#include +////#include +//#ifdef __APPLE__ +// #include +//#else +// #include +//#endif +// +//namespace openspace { +// +//class RenderableVolumeCL: public RenderableVolume { +//public: +// +// // constructors & destructor +// RenderableVolumeCL(const ghoul::Dictionary& dictionary); +// ~RenderableVolumeCL(); +// +// bool initialize(); +// bool deinitialize(); +// +// virtual void render(const Camera *camera, const psc& thisPosition); +// virtual void update(); +// +//private: +// +// void safeKernelCompilation(); +// +// std::string _filename; +// ghoul::RawVolumeReader::ReadHints _hints; +// float _stepSize; +// ghoul::opengl::FramebufferObject* _fbo; +// ghoul::opengl::Texture* _backTexture; +// ghoul::opengl::Texture* _frontTexture; +// ghoul::opengl::Texture* _volume; +// ghoul::opengl::Texture* _output; +// ghoul::opengl::ProgramObject *_fboProgram; +// ghoul::opengl::ProgramObject *_quadProgram; +// sgct_utils::SGCTBox* _boundingBox; +// GLuint _screenQuad; +// +// ghoul::opencl::CLContext _context; +// ghoul::opencl::CLCommandQueue _commands; +// ghoul::opencl::CLProgram _program; +// ghoul::opencl::CLKernel _kernel; +// ghoul::opencl::CLWorkSize* _ws; +// cl_mem _clBackTexture, _clFrontTexture, _clVolume, _clOutput; +// +// ghoul::filesystem::File* _kernelSourceFile; +// bool _kernelUpdateOnSave; +// std::mutex* _kernelMutex; +// +//}; +// +//} // namespace openspace +// +//#endif \ No newline at end of file diff --git a/include/openspace/rendering/renderablevolumeexpert.h b/include/openspace/rendering/renderablevolumeexpert.h index 5d445a8abb..90f50ccbbd 100644 --- a/include/openspace/rendering/renderablevolumeexpert.h +++ b/include/openspace/rendering/renderablevolumeexpert.h @@ -1,118 +1,119 @@ -/***************************************************************************************** - * * - * OpenSpace * - * * - * Copyright (c) 2014 * - * * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this * - * software and associated documentation files (the "Software"), to deal in the Software * - * without restriction, including without limitation the rights to use, copy, modify, * - * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * - * permit persons to whom the Software is furnished to do so, subject to the following * - * conditions: * - * * - * The above copyright notice and this permission notice shall be included in all copies * - * or substantial portions of the Software. * - * * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * - ****************************************************************************************/ - -#ifndef __RENDERABLEVOLUMEEXPERT_H__ -#define __RENDERABLEVOLUMEEXPERT_H__ - -// open space includes -#include -#include - -// ghoul includes -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef __APPLE__ - #include -#else - #include -#endif - -namespace ghoul { - namespace opencl { - class CLWorkSize; - } -} - -namespace openspace { - -class RenderableVolumeExpert: public RenderableVolume { -public: - - // constructors & destructor - RenderableVolumeExpert(const ghoul::Dictionary& dictionary); - ~RenderableVolumeExpert(); - - bool initialize(); - bool deinitialize(); - - virtual void render(const Camera *camera, const psc& thisPosition); - virtual void update(); - -private: - - // private methods - void safeKernelCompilation(); - void safeUpdateTexture(const ghoul::filesystem::File& file); - - // Volumes - std::vector _volumePaths; - std::vector _volumeHints; - - // Textures - ghoul::opengl::Texture* _output; - std::vector _volumes; - std::vector _transferFunctions; - std::vector _transferFunctionsFiles; - - // opencl texture memory pointers - cl_mem _clBackTexture; - cl_mem _clFrontTexture; - cl_mem _clOutput; - std::vector _clVolumes; - std::vector _clTransferFunctions; - - // opencl program - ghoul::opencl::CLContext _context; - ghoul::opencl::CLCommandQueue _commands; - ghoul::opencl::CLProgram _program; - ghoul::opencl::CLKernel _kernel; - ghoul::opencl::CLWorkSize* _ws; - ghoul::filesystem::File* _kernelSourceFile; - std::vector > _kernelOptions; - std::vector _kernelIncludes; - std::vector > _kernelDefinitions; - bool _programUpdateOnSave; - - // mutexes to prevent inconsistencies - std::mutex* _kernelLock; - std::mutex* _textureLock; - - ghoul::opengl::ProgramObject *_quadProgram; - GLuint _screenQuad; - - VolumeRaycasterBox* _colorBoxRenderer; - glm::vec3 _boxScaling; - -}; - -} // namespace openspace - -#endif \ No newline at end of file +// This is still in the repository to be cannibalized for a possible rewrite (ab) +///***************************************************************************************** +// * * +// * OpenSpace * +// * * +// * Copyright (c) 2014 * +// * * +// * Permission is hereby granted, free of charge, to any person obtaining a copy of this * +// * software and associated documentation files (the "Software"), to deal in the Software * +// * without restriction, including without limitation the rights to use, copy, modify, * +// * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +// * permit persons to whom the Software is furnished to do so, subject to the following * +// * conditions: * +// * * +// * The above copyright notice and this permission notice shall be included in all copies * +// * or substantial portions of the Software. * +// * * +// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +// * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +// * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +// * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +// * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +// * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +// ****************************************************************************************/ +// +//#ifndef __RENDERABLEVOLUMEEXPERT_H__ +//#define __RENDERABLEVOLUMEEXPERT_H__ +// +//// open space includes +//#include +//#include +// +//// ghoul includes +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +// +//#ifdef __APPLE__ +// #include +//#else +// #include +//#endif +// +//namespace ghoul { +// namespace opencl { +// class CLWorkSize; +// } +//} +// +//namespace openspace { +// +//class RenderableVolumeExpert: public RenderableVolume { +//public: +// +// // constructors & destructor +// RenderableVolumeExpert(const ghoul::Dictionary& dictionary); +// ~RenderableVolumeExpert(); +// +// bool initialize(); +// bool deinitialize(); +// +// virtual void render(const Camera *camera, const psc& thisPosition); +// virtual void update(); +// +//private: +// +// // private methods +// void safeKernelCompilation(); +// void safeUpdateTexture(const ghoul::filesystem::File& file); +// +// // Volumes +// std::vector _volumePaths; +// std::vector _volumeHints; +// +// // Textures +// ghoul::opengl::Texture* _output; +// std::vector _volumes; +// std::vector _transferFunctions; +// std::vector _transferFunctionsFiles; +// +// // opencl texture memory pointers +// cl_mem _clBackTexture; +// cl_mem _clFrontTexture; +// cl_mem _clOutput; +// std::vector _clVolumes; +// std::vector _clTransferFunctions; +// +// // opencl program +// ghoul::opencl::CLContext _context; +// ghoul::opencl::CLCommandQueue _commands; +// ghoul::opencl::CLProgram _program; +// ghoul::opencl::CLKernel _kernel; +// ghoul::opencl::CLWorkSize* _ws; +// ghoul::filesystem::File* _kernelSourceFile; +// std::vector > _kernelOptions; +// std::vector _kernelIncludes; +// std::vector > _kernelDefinitions; +// bool _programUpdateOnSave; +// +// // mutexes to prevent inconsistencies +// std::mutex* _kernelLock; +// std::mutex* _textureLock; +// +// ghoul::opengl::ProgramObject *_quadProgram; +// GLuint _screenQuad; +// +// VolumeRaycasterBox* _colorBoxRenderer; +// glm::vec3 _boxScaling; +// +//}; +// +//} // namespace openspace +// +//#endif \ No newline at end of file diff --git a/include/openspace/rendering/renderablevolumegl.h b/include/openspace/rendering/renderablevolumegl.h index dce4c1942b..4706078bfd 100644 --- a/include/openspace/rendering/renderablevolumegl.h +++ b/include/openspace/rendering/renderablevolumegl.h @@ -25,7 +25,6 @@ #ifndef __RENDERABLEVOLUMEGL_H__ #define __RENDERABLEVOLUMEGL_H__ -// open space includes #include // ghoul includes @@ -39,7 +38,6 @@ namespace openspace { class RenderableVolumeGL: public RenderableVolume { public: - // constructors & destructor RenderableVolumeGL(const ghoul::Dictionary& dictionary); ~RenderableVolumeGL(); diff --git a/include/openspace/util/constants.h b/include/openspace/util/constants.h index 6b16882f15..51ca026cc7 100644 --- a/include/openspace/util/constants.h +++ b/include/openspace/util/constants.h @@ -61,6 +61,16 @@ namespace renderableplanet { const std::string keyGeometry = "Geometry"; } // namespace renderableplanet +namespace renderablevolumegl { + const std::string keyVolume = "Volume"; + const std::string keyHints = "Hints"; + const std::string keyTransferFunction = "TransferFunction"; + const std::string keySampler = "Sampler"; + const std::string keyBoxScaling = "BoxScaling"; + const std::string keyVolumeName = "VolumeName"; + const std::string keyTransferFunctionName = "TransferFunctionName"; +} // namespace renderablevolumegl + namespace planetgeometry { const std::string keyType = "Type"; } // namespace planetgeometry diff --git a/src/rendering/planets/renderableplanet.cpp b/src/rendering/planets/renderableplanet.cpp index 4f0700a763..266963bbe0 100644 --- a/src/rendering/planets/renderableplanet.cpp +++ b/src/rendering/planets/renderableplanet.cpp @@ -52,7 +52,8 @@ RenderablePlanet::RenderablePlanet(const ghoul::Dictionary& dictionary) assert(success); std::string path; - dictionary.getValue(constants::scenegraph::keyPathModule, path); + success = dictionary.getValue(constants::scenegraph::keyPathModule, path); + assert(success); ghoul::Dictionary geometryDictionary; success = dictionary.getValueSafe( @@ -67,10 +68,9 @@ RenderablePlanet::RenderablePlanet(const ghoul::Dictionary& dictionary) // TODO: textures need to be replaced by a good system similar to the geometry as soon // as the requirements are fixed (ab) std::string texturePath = ""; - if (dictionary.hasKey("Textures.Color")) { - dictionary.getValue("Textures.Color", texturePath); + success = dictionary.getValueSafe("Textures.Color", texturePath); + if (success) _colorTexturePath = path + "/" + texturePath; - } addPropertySubOwner(_geometry); diff --git a/src/rendering/renderablevolumecl.cpp b/src/rendering/renderablevolumecl.cpp index b87c395705..380ea1f805 100644 --- a/src/rendering/renderablevolumecl.cpp +++ b/src/rendering/renderablevolumecl.cpp @@ -1,401 +1,402 @@ -/***************************************************************************************** - * * - * OpenSpace * - * * - * Copyright (c) 2014 * - * * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this * - * software and associated documentation files (the "Software"), to deal in the Software * - * without restriction, including without limitation the rights to use, copy, modify, * - * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * - * permit persons to whom the Software is furnished to do so, subject to the following * - * conditions: * - * * - * The above copyright notice and this permission notice shall be included in all copies * - * or substantial portions of the Software. * - * * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * - ****************************************************************************************/ - -// open space includes -#include - -#include - -#include -#include - -#include - -#include - -namespace { - std::string _loggerCat = "RenderableVolumeCL"; -} - -namespace openspace { - -RenderableVolumeCL::RenderableVolumeCL(const ghoul::Dictionary& dictionary): - RenderableVolume(dictionary), - _backTexture(nullptr), _frontTexture(nullptr), _output(nullptr), - _clBackTexture(0), _clFrontTexture(0), _clOutput(0), - _kernelSourceFile(nullptr) { - - _kernelMutex = new std::mutex; - - _filename = ""; - if(dictionary.hasKey("Volume")) { - if(dictionary.getValue("Volume", _filename)) { - _filename = findPath(_filename); - } - } - ghoul::Dictionary hintsDictionary; - if(dictionary.hasKey("Hints")) - dictionary.getValue("Hints", hintsDictionary); - _hints = readHints(hintsDictionary); - - /* - if(dictionary.hasKey("TransferFunctions")) { - ghoul::Dictionary transferFunctions; - if(dictionary.getValue("TransferFunctions", transferFunctions)) { - auto keys = transferFunctions.keys(); - for(auto key: keys) { - std::string transferFunctionPath = ""; - if(transferFunctions.getValue(key, transferFunctionPath)) { - transferFunctionPath = findPath(transferFunctionPath); - if(transferFunctionPath != "") { - ghoul::filesystem::File* tmp = new ghoul::filesystem::File(transferFunctionPath, false); - ghoul::opengl::Texture* tmpTexture = ghoul::opengl::loadTexture(tmp->path()); - - _transferFunctions.push_back(tmpTexture); - _transferFunctionsFiles.push_back(tmp); - } - } - } - } - } - */ - - if(dictionary.hasKey("UpdateOnSave")) { - dictionary.getValue("UpdateOnSave", _kernelUpdateOnSave); - } - - /* - if(dictionary.hasKey("KernelOptions")) { - using namespace ghoul::opencl; - ghoul::Dictionary kernelOptions; - if(dictionary.getValue("KernelOptions", kernelOptions)) { - auto keys = kernelOptions.keys(); - for(auto key: keys) { - bool value = false; - if(kernelOptions.getValue(key, value)) { - if(key == "DenormsAreZero") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::DenormsAreZero, value)); - } else if(key == "FastRelaxedMath") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::FastRelaxedMath, value)); - } else if(key == "FiniteMathOnly") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::FiniteMathOnly, value)); - } else if(key == "KernelArgInfo") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::KernelArgInfo, value)); - } else if(key == "MadEnable") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::MadEnable, value)); - } else if(key == "NoSignedZero") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::NoSignedZero, value)); - } else if(key == "OptDisable") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::OptDisable, value)); - } else if(key == "SinglePrecisionConstant") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::SinglePrecisionConstant, value)); - } else if(key == "StrictAliasing") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::StrictAliasing, value)); - } else if(key == "UnsafeMathOptimizations") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::UnsafeMathOptimizations, value)); - } - } - } - } - } - */ - - std::string kernelPath = ""; - if (dictionary.hasKey("Kernel")) { - if(dictionary.getValue("Kernel", kernelPath)) { - kernelPath = findPath(kernelPath); - } - } - if (kernelPath != "") { - _kernelSourceFile = new ghoul::filesystem::File(kernelPath, false); - } - -} - -RenderableVolumeCL::~RenderableVolumeCL() { - deinitialize(); - delete _kernelMutex; -} - -bool RenderableVolumeCL::initialize() { - assert(_filename != ""); - // ------ VOLUME READING ---------------- - ghoul::RawVolumeReader rawReader(_hints); - _volume = rawReader.read(_filename); - glm::size3_t d = _volume->dimensions(); - - // ------ SETUP GEOMETRY ---------------- - const GLfloat size = 1.0f; - const GLfloat vertex_texcoord_data[] = { // square of two triangles (sigh) - // x y z s t - -size, -size, 0.0f, 0.0f, 0.0f, - size, size, 0.0f, 1.0f, 1.0f, - -size, size, 0.0f, 0.0f, 1.0f, - -size, -size, 0.0f, 0.0f, 0.0f, - size, -size, 0.0f, 1.0f, 0.0f, - size, size, 0.0f, 1.0f, 1.0f - }; - - GLuint vertexPositionBuffer; - glGenVertexArrays(1, &_screenQuad); // generate array - glBindVertexArray(_screenQuad); // bind array - glGenBuffers(1, &vertexPositionBuffer); // generate buffer - glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBuffer); // bind buffer - glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_texcoord_data), vertex_texcoord_data, GL_STATIC_DRAW); - - // Vertex positions - GLuint vertexLocation = 2; - glEnableVertexAttribArray(vertexLocation); - glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), reinterpret_cast(0)); - - // Texture coordinates - GLuint texcoordLocation = 0; - glEnableVertexAttribArray(texcoordLocation); - glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void*)(3*sizeof(GLfloat))); - - glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind buffer - glBindVertexArray(0); //unbind array - - _boundingBox = new sgct_utils::SGCTBox(1.0f, sgct_utils::SGCTBox::Regular); - - // ------ SETUP SHADERS ----------------- - // TODO error control or better design pattern - OsEng.ref().configurationManager().getValue("RaycastProgram", _fboProgram); - OsEng.ref().configurationManager().getValue("Quad", _quadProgram); - - // ------ SETUP FBO --------------------- - _fbo = new ghoul::opengl::FramebufferObject(); - _fbo->activate(); - - size_t x = sgct::Engine::instance()->getActiveXResolution(); - size_t y = sgct::Engine::instance()->getActiveYResolution(); - _backTexture = new ghoul::opengl::Texture(glm::size3_t(x,y,1)); - _frontTexture = new ghoul::opengl::Texture(glm::size3_t(x,y,1)); - _output = new ghoul::opengl::Texture(glm::size3_t(x,y,1)); - _backTexture->uploadTexture(); - _frontTexture->uploadTexture(); - _output->uploadTexture(); - _fbo->attachTexture(_backTexture, GL_COLOR_ATTACHMENT0); - _fbo->attachTexture(_frontTexture, GL_COLOR_ATTACHMENT1); - - _fbo->deactivate(); - - _context = OsEng.clContext(); - _commands = _context.createCommandQueue(); - - _clBackTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *_backTexture); - _clFrontTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *_frontTexture); - _clVolume = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *_volume); - _clOutput = _context.createTextureFromGLTexture(CL_MEM_WRITE_ONLY, *_output); - - auto privateCallback = [this](const ghoul::filesystem::File& file) { - safeKernelCompilation(); - }; - - _kernelSourceFile = new ghoul::filesystem::File(_kernelSourceFile->path(), false); - if(_kernelUpdateOnSave) - _kernelSourceFile->setCallback(privateCallback); - - safeKernelCompilation(); - - size_t local_x = 32; - size_t local_y = 32; - while (local_x > 1) { - if(x % local_x == 0) - break; - local_x /= 2; - } - while (local_y > 1) { - if(y % local_y == 0) - break; - local_y /= 2; - } - _ws = new ghoul::opencl::CLWorkSize ({x,y}, {local_x,local_y}); - - return true; - -} - -bool RenderableVolumeCL::deinitialize() { - - delete _ws; - _ws = nullptr; - - return true; -} - -void RenderableVolumeCL::render(const Camera *camera, const psc &thisPosition) { - - if( ! _kernel.isValidKernel()) - return; - - float speed = 50.0f; - float time = sgct::Engine::getTime(); - glm::mat4 transform = camera->viewProjectionMatrix(); - - double factor = pow(10.0,thisPosition[3]); - transform = glm::translate(transform, glm::vec3(thisPosition[0]*factor, thisPosition[1]*factor, thisPosition[2]*factor)); - transform = glm::rotate(transform, time*speed, glm::vec3(0.0f, 1.0f, 0.0f)); - - - if(_kernel.isValidKernel()) { - _stepSize = 0.01f; - - // ------ DRAW TO FBO ------------------- - GLuint sgctFBO = ghoul::opengl::FramebufferObject::getActiveObject(); // Save SGCTs main FBO - _fbo->activate(); - _fboProgram->activate(); - _fboProgram->setUniform("modelViewProjection", transform); - - // Draw backface - glDrawBuffer(GL_COLOR_ATTACHMENT0); - glClearColor(0.2f, 0.2f, 0.2f, 0); - glClear(GL_COLOR_BUFFER_BIT); - glEnable(GL_CULL_FACE); - glCullFace(GL_FRONT); - _boundingBox->draw(); - glDisable(GL_CULL_FACE); - - // Draw frontface - glDrawBuffer(GL_COLOR_ATTACHMENT1); - glClear(GL_COLOR_BUFFER_BIT); - glClearColor(0.2f, 0.2f, 0.2f, 0); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); - _boundingBox->draw(); - glDisable(GL_CULL_FACE); - - _fboProgram->deactivate(); - _fbo->deactivate(); - - // ------ DRAW TO SCREEN ---------------- - glBindFramebuffer(GL_FRAMEBUFFER, sgctFBO); // Re-bind SGCTs main FBO - - - - glFinish(); - _commands.enqueueKernelBlocking(_kernel, *_ws); - _commands.finish(); - _quadProgram->activate(); - glActiveTexture(GL_TEXTURE0); - _output->bind(); - glClearColor(0.0f, 0.0f, 0.0f, 0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glBindVertexArray(_screenQuad); - glDrawArrays(GL_TRIANGLES, 0, 6); - glBindVertexArray(0); - - _quadProgram->deactivate(); - - } - - - -} - -void RenderableVolumeCL::update() { - -} - -void RenderableVolumeCL::safeKernelCompilation() { - std::string _loggerCat = "RenderableVolumeCL::safeKernelCompilation"; - if(_context.isValidContext()) { - - ghoul::opencl::CLProgram tmpProgram = _context.createProgram(_kernelSourceFile->path()); - tmpProgram.setOption(ghoul::opencl::CLProgram::Option::OptDisable, true); - tmpProgram.setOption(ghoul::opencl::CLProgram::Option::KernelArgInfo, true); - if(tmpProgram.build()) { - ghoul::opencl::CLKernel tmpKernel = tmpProgram.createKernel("volumeraycaster"); - if(tmpKernel.isValidKernel()) { - tmpKernel.setArgument(0, &_clFrontTexture); - tmpKernel.setArgument(1, &_clBackTexture); - tmpKernel.setArgument(2, &_clVolume); - tmpKernel.setArgument(3, &_clOutput); - - // do the actual assignment behind locked doors - _kernelMutex->lock(); - _program = tmpProgram; - _kernel = tmpKernel; - _kernelMutex->unlock(); - - LDEBUG("Done updating kernel"); - } else { - LWARNING("Kernel is not valid"); - } - } else { - LWARNING("Could not build CLProgram"); - } - } else { - LWARNING("No valid CLContext"); - } -} - /* -void RenderableVolumeCL::safeUpdateTexture(const ghoul::filesystem::File& file) { - int fileID = 0; - for (fileID = 0; fileID < _transferFunctionsFiles.size(); ++fileID) { - if (_transferFunctionsFiles.at(fileID) == &file) { - //LDEBUG("Transferfunction found at id " << fileID); - break; - } - } - if(fileID == _transferFunctionsFiles.size()) - return; - - LDEBUG("Updating transferfunction"); - // create the new texture - ghoul::opengl::Texture* newTexture = ghoul::opengl::loadTexture(file.path()); - - if(newTexture) { - - // upload the new texture and create a cl memory - newTexture->uploadTexture(); - cl_mem clNewTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *newTexture); - - if(clNewTexture == 0) { - delete newTexture; - return; - } - - // everything is ok, critical point to replace current texture pointers - _textureLock->lock(); - - // deallocate current texture - delete _transferFunctions.at(fileID); - clReleaseMemObject(_clTransferFunctions.at(fileID)); - - // set the new texture - _transferFunctions.at(fileID) = newTexture; - _clTransferFunctions.at(fileID) = clNewTexture; - - // update kernel - // __kernel arguments(front, back, output, [_volumes], .. fileID)) - _kernel.setArgument(3 + _volumes.size() + fileID, &clNewTexture); - - // end of critical section - _textureLock->unlock(); - } -} -*/ - -} // namespace openspace \ No newline at end of file +// This is still in the repository to be cannibalized for a possible rewrite (ab) +///***************************************************************************************** +// * * +// * OpenSpace * +// * * +// * Copyright (c) 2014 * +// * * +// * Permission is hereby granted, free of charge, to any person obtaining a copy of this * +// * software and associated documentation files (the "Software"), to deal in the Software * +// * without restriction, including without limitation the rights to use, copy, modify, * +// * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +// * permit persons to whom the Software is furnished to do so, subject to the following * +// * conditions: * +// * * +// * The above copyright notice and this permission notice shall be included in all copies * +// * or substantial portions of the Software. * +// * * +// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +// * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +// * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +// * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +// * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +// * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +// ****************************************************************************************/ +// +//// open space includes +//#include +// +//#include +// +//#include +//#include +// +//#include +// +//#include +// +//namespace { +// std::string _loggerCat = "RenderableVolumeCL"; +//} +// +//namespace openspace { +// +//RenderableVolumeCL::RenderableVolumeCL(const ghoul::Dictionary& dictionary): +// RenderableVolume(dictionary), +// _backTexture(nullptr), _frontTexture(nullptr), _output(nullptr), +// _clBackTexture(0), _clFrontTexture(0), _clOutput(0), +// _kernelSourceFile(nullptr) { +// +// _kernelMutex = new std::mutex; +// +// _filename = ""; +// if(dictionary.hasKey("Volume")) { +// if(dictionary.getValue("Volume", _filename)) { +// _filename = findPath(_filename); +// } +// } +// ghoul::Dictionary hintsDictionary; +// if(dictionary.hasKey("Hints")) +// dictionary.getValue("Hints", hintsDictionary); +// _hints = readHints(hintsDictionary); +// +// /* +// if(dictionary.hasKey("TransferFunctions")) { +// ghoul::Dictionary transferFunctions; +// if(dictionary.getValue("TransferFunctions", transferFunctions)) { +// auto keys = transferFunctions.keys(); +// for(auto key: keys) { +// std::string transferFunctionPath = ""; +// if(transferFunctions.getValue(key, transferFunctionPath)) { +// transferFunctionPath = findPath(transferFunctionPath); +// if(transferFunctionPath != "") { +// ghoul::filesystem::File* tmp = new ghoul::filesystem::File(transferFunctionPath, false); +// ghoul::opengl::Texture* tmpTexture = ghoul::opengl::loadTexture(tmp->path()); +// +// _transferFunctions.push_back(tmpTexture); +// _transferFunctionsFiles.push_back(tmp); +// } +// } +// } +// } +// } +// */ +// +// if(dictionary.hasKey("UpdateOnSave")) { +// dictionary.getValue("UpdateOnSave", _kernelUpdateOnSave); +// } +// +// /* +// if(dictionary.hasKey("KernelOptions")) { +// using namespace ghoul::opencl; +// ghoul::Dictionary kernelOptions; +// if(dictionary.getValue("KernelOptions", kernelOptions)) { +// auto keys = kernelOptions.keys(); +// for(auto key: keys) { +// bool value = false; +// if(kernelOptions.getValue(key, value)) { +// if(key == "DenormsAreZero") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::DenormsAreZero, value)); +// } else if(key == "FastRelaxedMath") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::FastRelaxedMath, value)); +// } else if(key == "FiniteMathOnly") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::FiniteMathOnly, value)); +// } else if(key == "KernelArgInfo") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::KernelArgInfo, value)); +// } else if(key == "MadEnable") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::MadEnable, value)); +// } else if(key == "NoSignedZero") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::NoSignedZero, value)); +// } else if(key == "OptDisable") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::OptDisable, value)); +// } else if(key == "SinglePrecisionConstant") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::SinglePrecisionConstant, value)); +// } else if(key == "StrictAliasing") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::StrictAliasing, value)); +// } else if(key == "UnsafeMathOptimizations") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::UnsafeMathOptimizations, value)); +// } +// } +// } +// } +// } +// */ +// +// std::string kernelPath = ""; +// if (dictionary.hasKey("Kernel")) { +// if(dictionary.getValue("Kernel", kernelPath)) { +// kernelPath = findPath(kernelPath); +// } +// } +// if (kernelPath != "") { +// _kernelSourceFile = new ghoul::filesystem::File(kernelPath, false); +// } +// +//} +// +//RenderableVolumeCL::~RenderableVolumeCL() { +// deinitialize(); +// delete _kernelMutex; +//} +// +//bool RenderableVolumeCL::initialize() { +// assert(_filename != ""); +// // ------ VOLUME READING ---------------- +// ghoul::RawVolumeReader rawReader(_hints); +// _volume = rawReader.read(_filename); +// glm::size3_t d = _volume->dimensions(); +// +// // ------ SETUP GEOMETRY ---------------- +// const GLfloat size = 1.0f; +// const GLfloat vertex_texcoord_data[] = { // square of two triangles (sigh) +// // x y z s t +// -size, -size, 0.0f, 0.0f, 0.0f, +// size, size, 0.0f, 1.0f, 1.0f, +// -size, size, 0.0f, 0.0f, 1.0f, +// -size, -size, 0.0f, 0.0f, 0.0f, +// size, -size, 0.0f, 1.0f, 0.0f, +// size, size, 0.0f, 1.0f, 1.0f +// }; +// +// GLuint vertexPositionBuffer; +// glGenVertexArrays(1, &_screenQuad); // generate array +// glBindVertexArray(_screenQuad); // bind array +// glGenBuffers(1, &vertexPositionBuffer); // generate buffer +// glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBuffer); // bind buffer +// glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_texcoord_data), vertex_texcoord_data, GL_STATIC_DRAW); +// +// // Vertex positions +// GLuint vertexLocation = 2; +// glEnableVertexAttribArray(vertexLocation); +// glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), reinterpret_cast(0)); +// +// // Texture coordinates +// GLuint texcoordLocation = 0; +// glEnableVertexAttribArray(texcoordLocation); +// glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void*)(3*sizeof(GLfloat))); +// +// glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind buffer +// glBindVertexArray(0); //unbind array +// +// _boundingBox = new sgct_utils::SGCTBox(1.0f, sgct_utils::SGCTBox::Regular); +// +// // ------ SETUP SHADERS ----------------- +// // TODO error control or better design pattern +// OsEng.ref().configurationManager().getValue("RaycastProgram", _fboProgram); +// OsEng.ref().configurationManager().getValue("Quad", _quadProgram); +// +// // ------ SETUP FBO --------------------- +// _fbo = new ghoul::opengl::FramebufferObject(); +// _fbo->activate(); +// +// size_t x = sgct::Engine::instance()->getActiveXResolution(); +// size_t y = sgct::Engine::instance()->getActiveYResolution(); +// _backTexture = new ghoul::opengl::Texture(glm::size3_t(x,y,1)); +// _frontTexture = new ghoul::opengl::Texture(glm::size3_t(x,y,1)); +// _output = new ghoul::opengl::Texture(glm::size3_t(x,y,1)); +// _backTexture->uploadTexture(); +// _frontTexture->uploadTexture(); +// _output->uploadTexture(); +// _fbo->attachTexture(_backTexture, GL_COLOR_ATTACHMENT0); +// _fbo->attachTexture(_frontTexture, GL_COLOR_ATTACHMENT1); +// +// _fbo->deactivate(); +// +// _context = OsEng.clContext(); +// _commands = _context.createCommandQueue(); +// +// _clBackTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *_backTexture); +// _clFrontTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *_frontTexture); +// _clVolume = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *_volume); +// _clOutput = _context.createTextureFromGLTexture(CL_MEM_WRITE_ONLY, *_output); +// +// auto privateCallback = [this](const ghoul::filesystem::File& file) { +// safeKernelCompilation(); +// }; +// +// _kernelSourceFile = new ghoul::filesystem::File(_kernelSourceFile->path(), false); +// if(_kernelUpdateOnSave) +// _kernelSourceFile->setCallback(privateCallback); +// +// safeKernelCompilation(); +// +// size_t local_x = 32; +// size_t local_y = 32; +// while (local_x > 1) { +// if(x % local_x == 0) +// break; +// local_x /= 2; +// } +// while (local_y > 1) { +// if(y % local_y == 0) +// break; +// local_y /= 2; +// } +// _ws = new ghoul::opencl::CLWorkSize ({x,y}, {local_x,local_y}); +// +// return true; +// +//} +// +//bool RenderableVolumeCL::deinitialize() { +// +// delete _ws; +// _ws = nullptr; +// +// return true; +//} +// +//void RenderableVolumeCL::render(const Camera *camera, const psc &thisPosition) { +// +// if( ! _kernel.isValidKernel()) +// return; +// +// float speed = 50.0f; +// float time = sgct::Engine::getTime(); +// glm::mat4 transform = camera->viewProjectionMatrix(); +// +// double factor = pow(10.0,thisPosition[3]); +// transform = glm::translate(transform, glm::vec3(thisPosition[0]*factor, thisPosition[1]*factor, thisPosition[2]*factor)); +// transform = glm::rotate(transform, time*speed, glm::vec3(0.0f, 1.0f, 0.0f)); +// +// +// if(_kernel.isValidKernel()) { +// _stepSize = 0.01f; +// +// // ------ DRAW TO FBO ------------------- +// GLuint sgctFBO = ghoul::opengl::FramebufferObject::getActiveObject(); // Save SGCTs main FBO +// _fbo->activate(); +// _fboProgram->activate(); +// _fboProgram->setUniform("modelViewProjection", transform); +// +// // Draw backface +// glDrawBuffer(GL_COLOR_ATTACHMENT0); +// glClearColor(0.2f, 0.2f, 0.2f, 0); +// glClear(GL_COLOR_BUFFER_BIT); +// glEnable(GL_CULL_FACE); +// glCullFace(GL_FRONT); +// _boundingBox->draw(); +// glDisable(GL_CULL_FACE); +// +// // Draw frontface +// glDrawBuffer(GL_COLOR_ATTACHMENT1); +// glClear(GL_COLOR_BUFFER_BIT); +// glClearColor(0.2f, 0.2f, 0.2f, 0); +// glEnable(GL_CULL_FACE); +// glCullFace(GL_BACK); +// _boundingBox->draw(); +// glDisable(GL_CULL_FACE); +// +// _fboProgram->deactivate(); +// _fbo->deactivate(); +// +// // ------ DRAW TO SCREEN ---------------- +// glBindFramebuffer(GL_FRAMEBUFFER, sgctFBO); // Re-bind SGCTs main FBO +// +// +// +// glFinish(); +// _commands.enqueueKernelBlocking(_kernel, *_ws); +// _commands.finish(); +// _quadProgram->activate(); +// glActiveTexture(GL_TEXTURE0); +// _output->bind(); +// glClearColor(0.0f, 0.0f, 0.0f, 0); +// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +// glBindVertexArray(_screenQuad); +// glDrawArrays(GL_TRIANGLES, 0, 6); +// glBindVertexArray(0); +// +// _quadProgram->deactivate(); +// +// } +// +// +// +//} +// +//void RenderableVolumeCL::update() { +// +//} +// +//void RenderableVolumeCL::safeKernelCompilation() { +// std::string _loggerCat = "RenderableVolumeCL::safeKernelCompilation"; +// if(_context.isValidContext()) { +// +// ghoul::opencl::CLProgram tmpProgram = _context.createProgram(_kernelSourceFile->path()); +// tmpProgram.setOption(ghoul::opencl::CLProgram::Option::OptDisable, true); +// tmpProgram.setOption(ghoul::opencl::CLProgram::Option::KernelArgInfo, true); +// if(tmpProgram.build()) { +// ghoul::opencl::CLKernel tmpKernel = tmpProgram.createKernel("volumeraycaster"); +// if(tmpKernel.isValidKernel()) { +// tmpKernel.setArgument(0, &_clFrontTexture); +// tmpKernel.setArgument(1, &_clBackTexture); +// tmpKernel.setArgument(2, &_clVolume); +// tmpKernel.setArgument(3, &_clOutput); +// +// // do the actual assignment behind locked doors +// _kernelMutex->lock(); +// _program = tmpProgram; +// _kernel = tmpKernel; +// _kernelMutex->unlock(); +// +// LDEBUG("Done updating kernel"); +// } else { +// LWARNING("Kernel is not valid"); +// } +// } else { +// LWARNING("Could not build CLProgram"); +// } +// } else { +// LWARNING("No valid CLContext"); +// } +//} +// /* +//void RenderableVolumeCL::safeUpdateTexture(const ghoul::filesystem::File& file) { +// int fileID = 0; +// for (fileID = 0; fileID < _transferFunctionsFiles.size(); ++fileID) { +// if (_transferFunctionsFiles.at(fileID) == &file) { +// //LDEBUG("Transferfunction found at id " << fileID); +// break; +// } +// } +// if(fileID == _transferFunctionsFiles.size()) +// return; +// +// LDEBUG("Updating transferfunction"); +// // create the new texture +// ghoul::opengl::Texture* newTexture = ghoul::opengl::loadTexture(file.path()); +// +// if(newTexture) { +// +// // upload the new texture and create a cl memory +// newTexture->uploadTexture(); +// cl_mem clNewTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *newTexture); +// +// if(clNewTexture == 0) { +// delete newTexture; +// return; +// } +// +// // everything is ok, critical point to replace current texture pointers +// _textureLock->lock(); +// +// // deallocate current texture +// delete _transferFunctions.at(fileID); +// clReleaseMemObject(_clTransferFunctions.at(fileID)); +// +// // set the new texture +// _transferFunctions.at(fileID) = newTexture; +// _clTransferFunctions.at(fileID) = clNewTexture; +// +// // update kernel +// // __kernel arguments(front, back, output, [_volumes], .. fileID)) +// _kernel.setArgument(3 + _volumes.size() + fileID, &clNewTexture); +// +// // end of critical section +// _textureLock->unlock(); +// } +//} +//*/ +// +//} // namespace openspace \ No newline at end of file diff --git a/src/rendering/renderablevolumeexpert.cpp b/src/rendering/renderablevolumeexpert.cpp index d1d33c9e51..49b22aab65 100644 --- a/src/rendering/renderablevolumeexpert.cpp +++ b/src/rendering/renderablevolumeexpert.cpp @@ -1,533 +1,534 @@ -/***************************************************************************************** - * * - * OpenSpace * - * * - * Copyright (c) 2014 * - * * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this * - * software and associated documentation files (the "Software"), to deal in the Software * - * without restriction, including without limitation the rights to use, copy, modify, * - * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * - * permit persons to whom the Software is furnished to do so, subject to the following * - * conditions: * - * * - * The above copyright notice and this permission notice shall be included in all copies * - * or substantial portions of the Software. * - * * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * - ****************************************************************************************/ - -// open space includes -#include - -#include - -#include -#include -#include -#include - -#include - -namespace { - std::string _loggerCat = "RenderableVolumeExpert"; - - size_t countKernelArguments(ghoul::opencl::CLKernel& kernel) { - - using ghoul::opencl::CLKernel; - CLKernel::AddressQualifier adq; - CLKernel::AccessQualifier acq; - CLKernel::TypeQualifier atq; - - size_t arguments = 0; - do { - adq = kernel.argumentAddressQualifier(arguments); - acq = kernel.argumentAccessQualifier(arguments); - atq = kernel.argumentTypeQualifier(arguments); - ++arguments; - } while (adq != CLKernel::AddressQualifier::Error && - acq != CLKernel::AccessQualifier::Error && - atq != CLKernel::TypeQualifier::Error); - - return arguments - 1; - } -} - -namespace openspace { - -RenderableVolumeExpert::RenderableVolumeExpert(const ghoul::Dictionary& dictionary): - RenderableVolume(dictionary), - _output(nullptr), - _clBackTexture(0), _clFrontTexture(0), _clOutput(0), - _kernelSourceFile(nullptr), _programUpdateOnSave(false), _colorBoxRenderer(nullptr), - _boxScaling(1.0,1.0,1.0) { - - _kernelLock = new std::mutex; - _textureLock = new std::mutex; - - if(dictionary.hasKey("Volumes")) { - ghoul::Dictionary volumes; - if(dictionary.getValue("Volumes", volumes)) { - auto keys = volumes.keys(); - for(auto key: keys) { - ghoul::Dictionary volume; - if(volumes.getValue(key, volume)) { - if (volume.hasKey("File")) { - std::string file = ""; - if (volume.getValue("File", file)) { - file = findPath(file); - if (file != "") { - - // parse hints - ghoul::Dictionary hintsDictionary; - if(volume.hasKey("Hints")) - volume.getValue("Hints", hintsDictionary); - - _volumePaths.push_back(file); - _volumeHints.push_back(hintsDictionary); - } - } - } - } - } - } - } - - if(dictionary.hasKey("TransferFunctions")) { - ghoul::Dictionary transferFunctions; - if(dictionary.getValue("TransferFunctions", transferFunctions)) { - auto keys = transferFunctions.keys(); - for(auto key: keys) { - std::string transferFunctionPath = ""; - if(transferFunctions.getValue(key, transferFunctionPath)) { - transferFunctionPath = findPath(transferFunctionPath); - ghoul::opengl::Texture* tmpTexture = loadTransferFunction(transferFunctionPath); - if(tmpTexture) { - ghoul::filesystem::File* tmp = new ghoul::filesystem::File(transferFunctionPath, false); - _transferFunctions.push_back(tmpTexture); - _transferFunctionsFiles.push_back(tmp); - } - } - } - } - } - - std::string kernelPath = ""; - if (dictionary.hasKey("Kernel")) { - ghoul::Dictionary kernelDictionary; - if(dictionary.getValue("Kernel", kernelDictionary)) { - if(kernelDictionary.getValue("Source", kernelPath)) { - kernelPath = findPath(kernelPath); - - } - if(kernelDictionary.hasKey("UpdateOnSave")) { - kernelDictionary.getValue("UpdateOnSave", _programUpdateOnSave); - } - - if(kernelDictionary.hasKey("Options")) { - using namespace ghoul::opencl; - ghoul::Dictionary kernelOptions; - if(kernelDictionary.getValue("Options", kernelOptions)) { - auto keys = kernelOptions.keys(); - for(auto key: keys) { - bool value = false; - if(kernelOptions.getValue(key, value)) { - if(key == "DenormsAreZero") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::DenormsAreZero, value)); - } else if(key == "FastRelaxedMath") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::FastRelaxedMath, value)); - } else if(key == "FiniteMathOnly") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::FiniteMathOnly, value)); - } else if(key == "KernelArgInfo") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::KernelArgInfo, value)); - } else if(key == "MadEnable") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::MadEnable, value)); - } else if(key == "NoSignedZero") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::NoSignedZero, value)); - } else if(key == "OptDisable") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::OptDisable, value)); - } else if(key == "SinglePrecisionConstant") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::SinglePrecisionConstant, value)); - } else if(key == "StrictAliasing") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::StrictAliasing, value)); - } else if(key == "UnsafeMathOptimizations") { - _kernelOptions.push_back(std::make_pair(CLProgram::Option::UnsafeMathOptimizations, value)); - } - } - } - } - } - - ghoul::Dictionary includeDictionary; - if (kernelDictionary.hasKey("Includes") && kernelDictionary.getValue("Includes", includeDictionary)) { - auto keys = includeDictionary.keys(); - for(auto key: keys) { - std::string includePath; - if(includeDictionary.getValue(key, includePath)) { - if(FileSys.directoryExists(includePath)) { - _kernelIncludes.push_back(absPath(includePath)); - } - } - } - } - - ghoul::Dictionary defineDictionary; - if (kernelDictionary.hasKey("Definitions") && kernelDictionary.getValue("Definitions", defineDictionary)) { - auto keys = defineDictionary.keys(); - for(auto key: keys) { - std::string defintion; - if(defineDictionary.getValue(key, defintion)) { - _kernelDefinitions.push_back(std::make_pair(key, defintion)); - } - } - } - } - } - if (kernelPath != "") { - _kernelSourceFile = new ghoul::filesystem::File(kernelPath, false); - } - - _colorBoxRenderer = new VolumeRaycasterBox(); - double tempValue; - if(dictionary.hasKey("BoxScaling.1") && dictionary.getValue("BoxScaling.1", tempValue)) { - if(tempValue > 0.0) { - _boxScaling[0] = tempValue; - } - } - if(dictionary.hasKey("BoxScaling.2") && dictionary.getValue("BoxScaling.2", tempValue)) { - if(tempValue > 0.0) { - _boxScaling[1] = tempValue; - } - } - if(dictionary.hasKey("BoxScaling.3") && dictionary.getValue("BoxScaling.3", tempValue)) { - if(tempValue > 0.0) { - _boxScaling[2] = tempValue; - } - } - - setBoundingSphere(PowerScaledScalar::CreatePSS(glm::length(_boxScaling))); -} - -RenderableVolumeExpert::~RenderableVolumeExpert() { - deinitialize(); - delete _textureLock; - delete _kernelLock; -} - -bool RenderableVolumeExpert::initialize() { - if(_kernelSourceFile == nullptr) { - LERROR("Could not find the kernel file!"); - return false; - } - - _context = OsEng.clContext(); - - auto textureCallback = [this](const ghoul::filesystem::File& file) { - safeUpdateTexture(file); - }; - auto kernelCallback = [this](const ghoul::filesystem::File& file) { - safeKernelCompilation(); - }; - - for(auto texture: _transferFunctions) { - texture->uploadTexture(); - cl_mem transferMem = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *texture); - _clTransferFunctions.push_back(transferMem); - } - if(_programUpdateOnSave) { - _kernelSourceFile->setCallback(kernelCallback); - for(auto texture: _transferFunctionsFiles) { - texture->setCallback(textureCallback); - } - } - - for (int i = 0; i < _volumePaths.size(); ++i) { - ghoul::opengl::Texture* volume = loadVolume(_volumePaths.at(i), _volumeHints.at(i)); - if(volume) { - volume->uploadTexture(); - - LDEBUG("Creating CL texture from GL texture with path '" << _volumePaths.at(i) << "'"); - cl_mem volumeTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *volume); - - _volumes.push_back(volume); - _clVolumes.push_back(volumeTexture); - } else { - LERROR("Invalid volume"); - } - } - - // ------ SETUP GEOMETRY ---------------- - const GLfloat size = 1.0f; - const GLfloat vertex_texcoord_data[] = { // square of two triangles (sigh) - // x y z s t - -size, -size, 0.0f, 0.0f, 0.0f, - size, size, 0.0f, 1.0f, 1.0f, - -size, size, 0.0f, 0.0f, 1.0f, - -size, -size, 0.0f, 0.0f, 0.0f, - size, -size, 0.0f, 1.0f, 0.0f, - size, size, 0.0f, 1.0f, 1.0f - }; - - GLuint vertexPositionBuffer; - glGenVertexArrays(1, &_screenQuad); // generate array - glBindVertexArray(_screenQuad); // bind array - glGenBuffers(1, &vertexPositionBuffer); // generate buffer - glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBuffer); // bind buffer - glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_texcoord_data), vertex_texcoord_data, GL_STATIC_DRAW); - - // Vertex positions - GLuint vertexLocation = 2; - glEnableVertexAttribArray(vertexLocation); - glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), reinterpret_cast(0)); - - // Texture coordinates - GLuint texcoordLocation = 0; - glEnableVertexAttribArray(texcoordLocation); - glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void*)(3*sizeof(GLfloat))); - - glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind buffer - glBindVertexArray(0); //unbind array - - if( ! OsEng.ref().configurationManager().getValue("Quad", _quadProgram)) { - LERROR("Could not find 'Quad'"); - return false; - } - - _colorBoxRenderer->initialize(); - glm::size2_t dimensions = _colorBoxRenderer->dimensions(); - ghoul::opengl::Texture* backTexture = _colorBoxRenderer->backFace(); - ghoul::opengl::Texture* frontTexture = _colorBoxRenderer->frontFace(); - _output = new ghoul::opengl::Texture(glm::size3_t(dimensions[0],dimensions[1],1)); - _output->uploadTexture(); - - _context = OsEng.clContext(); - _commands = _context.createCommandQueue(); - - _clBackTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *backTexture); - _clFrontTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *frontTexture); - _clOutput = _context.createTextureFromGLTexture(CL_MEM_WRITE_ONLY, *_output); - - // Compile kernels - safeKernelCompilation(); - - // create work group - size_t local_x = 32; - size_t local_y = 32; - while (local_x > 1) { - if(dimensions[0] % local_x == 0) - break; - local_x /= 2; - } - while (local_y > 1) { - if(dimensions[1] % local_y == 0) - break; - local_y /= 2; - } - _ws = new ghoul::opencl::CLWorkSize({dimensions[0],dimensions[1]}, {local_x,local_y}); - - return true; -} - -bool RenderableVolumeExpert::deinitialize() { - - - return true; -} - -void RenderableVolumeExpert::render(const Camera *camera, const psc &thisPosition) { - if( ! _kernel.isValidKernel()) - return; - - glm::mat4 transform = camera->viewProjectionMatrix(); - glm::mat4 camTransform = camera->viewRotationMatrix(); - psc relative = thisPosition-camera->position(); - - transform = transform*camTransform; - transform = glm::translate(transform, relative.vec3()); - transform = glm::scale(transform, _boxScaling); - - _colorBoxRenderer->render(transform); - - _textureLock->lock(); - _kernelLock->lock(); - - // tell opengl to finish everything before opencl takes ownerhip (uses) the textures - glFinish(); - - // Aquire GL objects - _commands.enqueueAcquireGLObjects(_clBackTexture); - _commands.enqueueAcquireGLObjects(_clFrontTexture); - _commands.enqueueAcquireGLObjects(_clOutput); - _commands.enqueueAcquireGLObjects(_clVolumes); - _commands.enqueueAcquireGLObjects(_clTransferFunctions); - - _commands.enqueueKernelBlocking(_kernel, *_ws); - _commands.finish(); - - // Release GL objects - _commands.enqueueReleaseGLObjects(_clBackTexture); - _commands.enqueueReleaseGLObjects(_clFrontTexture); - _commands.enqueueReleaseGLObjects(_clOutput); - _commands.enqueueReleaseGLObjects(_clVolumes); - _commands.enqueueReleaseGLObjects(_clTransferFunctions); - - _quadProgram->activate(); - glActiveTexture(GL_TEXTURE0); - _output->bind(); - - // enable blending - glEnable(GL_BLEND); - glDisable(GL_DEPTH_TEST); - glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); - - glBindVertexArray(_screenQuad); - glDrawArrays(GL_TRIANGLES, 0, 6); - glBindVertexArray(0); - - _kernelLock->unlock(); - _textureLock->unlock(); - - // disable blending - glDisable(GL_BLEND); - glEnable(GL_DEPTH_TEST); - glBlendFunc(GL_ONE, GL_ZERO); - - _quadProgram->deactivate(); -} - -void RenderableVolumeExpert::update() { - -} - -void RenderableVolumeExpert::safeKernelCompilation() { - if(_context.isValidContext()) { - - ghoul::opencl::CLProgram tmpProgram = _context.createProgram(_kernelSourceFile->path()); - for(auto option: _kernelOptions) { - tmpProgram.setOption(option.first, option.second); - } - - for(auto defintion: _kernelDefinitions) { - tmpProgram.addDefinition(defintion.first, defintion.second); - } - - // add the include directories - tmpProgram.addIncludeDirectory(_kernelIncludes); - - if(tmpProgram.build()) { - ghoul::opencl::CLKernel tmpKernel = tmpProgram.createKernel("volumeraycaster"); - if(tmpKernel.isValidKernel()) { - - auto begin = _kernelOptions.begin(); - auto end = _kernelOptions.end(); - auto f = std::find(begin, end, std::make_pair(ghoul::opencl::CLProgram::Option::KernelArgInfo, true)); - - int maxarguments = 1024; - bool argumentError = false; - if (f != end) { - LDEBUG("Checking argument types"); - - using ghoul::opencl::CLKernel; - maxarguments = countKernelArguments(tmpKernel); - - for (int i = 3; i maxarguments) { - LWARNING("More arguments set than kernel accepts."); - } - - // do the actual assignment behind locked doors - _kernelLock->lock(); - _program = tmpProgram; - _kernel = tmpKernel; - _kernelLock->unlock(); - LDEBUG("Done updating kernel"); - } - } - } -} - -void RenderableVolumeExpert::safeUpdateTexture(const ghoul::filesystem::File& file) { - int fileID = 0; - for (fileID = 0; fileID < _transferFunctionsFiles.size(); ++fileID) { - if (_transferFunctionsFiles.at(fileID) == &file) { - //LDEBUG("Transferfunction found at id " << fileID); - break; - } - } - if(fileID == _transferFunctionsFiles.size()) - return; - - LDEBUG("Updating transferfunction"); - - // create the new texture - ghoul::opengl::Texture* newTexture = loadTransferFunction(file.path()); - - if(newTexture) { - - // upload the new texture and create a cl memory - newTexture->uploadTexture(); - cl_mem clNewTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *newTexture); - - // check if opencl memory is unsuccessfull - if(clNewTexture == 0) { - delete newTexture; - return; - } - - // everything seems ok, critical point to replace current texture pointers - _textureLock->lock(); - - // deallocate current texture - clReleaseMemObject(_clTransferFunctions.at(fileID)); - delete _transferFunctions.at(fileID); - - // set the new texture - _transferFunctions.at(fileID) = newTexture; - _clTransferFunctions.at(fileID) = clNewTexture; - - // update kernel - // __kernel arguments(front, back, output, [_volumes], .. fileID)) - _kernel.setArgument(3 + _volumes.size() + fileID, &clNewTexture); - - LDEBUG("Transferfunction successfully updated"); - - // end of critical section - _textureLock->unlock(); - } -} - -} // namespace openspace +// This is still in the repository to be cannibalized for a possible rewrite (ab) +///***************************************************************************************** +// * * +// * OpenSpace * +// * * +// * Copyright (c) 2014 * +// * * +// * Permission is hereby granted, free of charge, to any person obtaining a copy of this * +// * software and associated documentation files (the "Software"), to deal in the Software * +// * without restriction, including without limitation the rights to use, copy, modify, * +// * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +// * permit persons to whom the Software is furnished to do so, subject to the following * +// * conditions: * +// * * +// * The above copyright notice and this permission notice shall be included in all copies * +// * or substantial portions of the Software. * +// * * +// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +// * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +// * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +// * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +// * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +// * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +// ****************************************************************************************/ +// +//// open space includes +//#include +// +//#include +// +//#include +//#include +//#include +//#include +// +//#include +// +//namespace { +// std::string _loggerCat = "RenderableVolumeExpert"; +// +// size_t countKernelArguments(ghoul::opencl::CLKernel& kernel) { +// +// using ghoul::opencl::CLKernel; +// CLKernel::AddressQualifier adq; +// CLKernel::AccessQualifier acq; +// CLKernel::TypeQualifier atq; +// +// size_t arguments = 0; +// do { +// adq = kernel.argumentAddressQualifier(arguments); +// acq = kernel.argumentAccessQualifier(arguments); +// atq = kernel.argumentTypeQualifier(arguments); +// ++arguments; +// } while (adq != CLKernel::AddressQualifier::Error && +// acq != CLKernel::AccessQualifier::Error && +// atq != CLKernel::TypeQualifier::Error); +// +// return arguments - 1; +// } +//} +// +//namespace openspace { +// +//RenderableVolumeExpert::RenderableVolumeExpert(const ghoul::Dictionary& dictionary): +// RenderableVolume(dictionary), +// _output(nullptr), +// _clBackTexture(0), _clFrontTexture(0), _clOutput(0), +// _kernelSourceFile(nullptr), _programUpdateOnSave(false), _colorBoxRenderer(nullptr), +// _boxScaling(1.0,1.0,1.0) { +// +// _kernelLock = new std::mutex; +// _textureLock = new std::mutex; +// +// if(dictionary.hasKey("Volumes")) { +// ghoul::Dictionary volumes; +// if(dictionary.getValue("Volumes", volumes)) { +// auto keys = volumes.keys(); +// for(auto key: keys) { +// ghoul::Dictionary volume; +// if(volumes.getValue(key, volume)) { +// if (volume.hasKey("File")) { +// std::string file = ""; +// if (volume.getValue("File", file)) { +// file = findPath(file); +// if (file != "") { +// +// // parse hints +// ghoul::Dictionary hintsDictionary; +// if(volume.hasKey("Hints")) +// volume.getValue("Hints", hintsDictionary); +// +// _volumePaths.push_back(file); +// _volumeHints.push_back(hintsDictionary); +// } +// } +// } +// } +// } +// } +// } +// +// if(dictionary.hasKey("TransferFunctions")) { +// ghoul::Dictionary transferFunctions; +// if(dictionary.getValue("TransferFunctions", transferFunctions)) { +// auto keys = transferFunctions.keys(); +// for(auto key: keys) { +// std::string transferFunctionPath = ""; +// if(transferFunctions.getValue(key, transferFunctionPath)) { +// transferFunctionPath = findPath(transferFunctionPath); +// ghoul::opengl::Texture* tmpTexture = loadTransferFunction(transferFunctionPath); +// if(tmpTexture) { +// ghoul::filesystem::File* tmp = new ghoul::filesystem::File(transferFunctionPath, false); +// _transferFunctions.push_back(tmpTexture); +// _transferFunctionsFiles.push_back(tmp); +// } +// } +// } +// } +// } +// +// std::string kernelPath = ""; +// if (dictionary.hasKey("Kernel")) { +// ghoul::Dictionary kernelDictionary; +// if(dictionary.getValue("Kernel", kernelDictionary)) { +// if(kernelDictionary.getValue("Source", kernelPath)) { +// kernelPath = findPath(kernelPath); +// +// } +// if(kernelDictionary.hasKey("UpdateOnSave")) { +// kernelDictionary.getValue("UpdateOnSave", _programUpdateOnSave); +// } +// +// if(kernelDictionary.hasKey("Options")) { +// using namespace ghoul::opencl; +// ghoul::Dictionary kernelOptions; +// if(kernelDictionary.getValue("Options", kernelOptions)) { +// auto keys = kernelOptions.keys(); +// for(auto key: keys) { +// bool value = false; +// if(kernelOptions.getValue(key, value)) { +// if(key == "DenormsAreZero") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::DenormsAreZero, value)); +// } else if(key == "FastRelaxedMath") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::FastRelaxedMath, value)); +// } else if(key == "FiniteMathOnly") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::FiniteMathOnly, value)); +// } else if(key == "KernelArgInfo") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::KernelArgInfo, value)); +// } else if(key == "MadEnable") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::MadEnable, value)); +// } else if(key == "NoSignedZero") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::NoSignedZero, value)); +// } else if(key == "OptDisable") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::OptDisable, value)); +// } else if(key == "SinglePrecisionConstant") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::SinglePrecisionConstant, value)); +// } else if(key == "StrictAliasing") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::StrictAliasing, value)); +// } else if(key == "UnsafeMathOptimizations") { +// _kernelOptions.push_back(std::make_pair(CLProgram::Option::UnsafeMathOptimizations, value)); +// } +// } +// } +// } +// } +// +// ghoul::Dictionary includeDictionary; +// if (kernelDictionary.hasKey("Includes") && kernelDictionary.getValue("Includes", includeDictionary)) { +// auto keys = includeDictionary.keys(); +// for(auto key: keys) { +// std::string includePath; +// if(includeDictionary.getValue(key, includePath)) { +// if(FileSys.directoryExists(includePath)) { +// _kernelIncludes.push_back(absPath(includePath)); +// } +// } +// } +// } +// +// ghoul::Dictionary defineDictionary; +// if (kernelDictionary.hasKey("Definitions") && kernelDictionary.getValue("Definitions", defineDictionary)) { +// auto keys = defineDictionary.keys(); +// for(auto key: keys) { +// std::string defintion; +// if(defineDictionary.getValue(key, defintion)) { +// _kernelDefinitions.push_back(std::make_pair(key, defintion)); +// } +// } +// } +// } +// } +// if (kernelPath != "") { +// _kernelSourceFile = new ghoul::filesystem::File(kernelPath, false); +// } +// +// _colorBoxRenderer = new VolumeRaycasterBox(); +// double tempValue; +// if(dictionary.hasKey("BoxScaling.1") && dictionary.getValue("BoxScaling.1", tempValue)) { +// if(tempValue > 0.0) { +// _boxScaling[0] = tempValue; +// } +// } +// if(dictionary.hasKey("BoxScaling.2") && dictionary.getValue("BoxScaling.2", tempValue)) { +// if(tempValue > 0.0) { +// _boxScaling[1] = tempValue; +// } +// } +// if(dictionary.hasKey("BoxScaling.3") && dictionary.getValue("BoxScaling.3", tempValue)) { +// if(tempValue > 0.0) { +// _boxScaling[2] = tempValue; +// } +// } +// +// setBoundingSphere(PowerScaledScalar::CreatePSS(glm::length(_boxScaling))); +//} +// +//RenderableVolumeExpert::~RenderableVolumeExpert() { +// deinitialize(); +// delete _textureLock; +// delete _kernelLock; +//} +// +//bool RenderableVolumeExpert::initialize() { +// if(_kernelSourceFile == nullptr) { +// LERROR("Could not find the kernel file!"); +// return false; +// } +// +// _context = OsEng.clContext(); +// +// auto textureCallback = [this](const ghoul::filesystem::File& file) { +// safeUpdateTexture(file); +// }; +// auto kernelCallback = [this](const ghoul::filesystem::File& file) { +// safeKernelCompilation(); +// }; +// +// for(auto texture: _transferFunctions) { +// texture->uploadTexture(); +// cl_mem transferMem = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *texture); +// _clTransferFunctions.push_back(transferMem); +// } +// if(_programUpdateOnSave) { +// _kernelSourceFile->setCallback(kernelCallback); +// for(auto texture: _transferFunctionsFiles) { +// texture->setCallback(textureCallback); +// } +// } +// +// for (int i = 0; i < _volumePaths.size(); ++i) { +// ghoul::opengl::Texture* volume = loadVolume(_volumePaths.at(i), _volumeHints.at(i)); +// if(volume) { +// volume->uploadTexture(); +// +// LDEBUG("Creating CL texture from GL texture with path '" << _volumePaths.at(i) << "'"); +// cl_mem volumeTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *volume); +// +// _volumes.push_back(volume); +// _clVolumes.push_back(volumeTexture); +// } else { +// LERROR("Invalid volume"); +// } +// } +// +// // ------ SETUP GEOMETRY ---------------- +// const GLfloat size = 1.0f; +// const GLfloat vertex_texcoord_data[] = { // square of two triangles (sigh) +// // x y z s t +// -size, -size, 0.0f, 0.0f, 0.0f, +// size, size, 0.0f, 1.0f, 1.0f, +// -size, size, 0.0f, 0.0f, 1.0f, +// -size, -size, 0.0f, 0.0f, 0.0f, +// size, -size, 0.0f, 1.0f, 0.0f, +// size, size, 0.0f, 1.0f, 1.0f +// }; +// +// GLuint vertexPositionBuffer; +// glGenVertexArrays(1, &_screenQuad); // generate array +// glBindVertexArray(_screenQuad); // bind array +// glGenBuffers(1, &vertexPositionBuffer); // generate buffer +// glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBuffer); // bind buffer +// glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_texcoord_data), vertex_texcoord_data, GL_STATIC_DRAW); +// +// // Vertex positions +// GLuint vertexLocation = 2; +// glEnableVertexAttribArray(vertexLocation); +// glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), reinterpret_cast(0)); +// +// // Texture coordinates +// GLuint texcoordLocation = 0; +// glEnableVertexAttribArray(texcoordLocation); +// glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void*)(3*sizeof(GLfloat))); +// +// glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind buffer +// glBindVertexArray(0); //unbind array +// +// if( ! OsEng.ref().configurationManager().getValue("Quad", _quadProgram)) { +// LERROR("Could not find 'Quad'"); +// return false; +// } +// +// _colorBoxRenderer->initialize(); +// glm::size2_t dimensions = _colorBoxRenderer->dimensions(); +// ghoul::opengl::Texture* backTexture = _colorBoxRenderer->backFace(); +// ghoul::opengl::Texture* frontTexture = _colorBoxRenderer->frontFace(); +// _output = new ghoul::opengl::Texture(glm::size3_t(dimensions[0],dimensions[1],1)); +// _output->uploadTexture(); +// +// _context = OsEng.clContext(); +// _commands = _context.createCommandQueue(); +// +// _clBackTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *backTexture); +// _clFrontTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *frontTexture); +// _clOutput = _context.createTextureFromGLTexture(CL_MEM_WRITE_ONLY, *_output); +// +// // Compile kernels +// safeKernelCompilation(); +// +// // create work group +// size_t local_x = 32; +// size_t local_y = 32; +// while (local_x > 1) { +// if(dimensions[0] % local_x == 0) +// break; +// local_x /= 2; +// } +// while (local_y > 1) { +// if(dimensions[1] % local_y == 0) +// break; +// local_y /= 2; +// } +// _ws = new ghoul::opencl::CLWorkSize({dimensions[0],dimensions[1]}, {local_x,local_y}); +// +// return true; +//} +// +//bool RenderableVolumeExpert::deinitialize() { +// +// +// return true; +//} +// +//void RenderableVolumeExpert::render(const Camera *camera, const psc &thisPosition) { +// if( ! _kernel.isValidKernel()) +// return; +// +// glm::mat4 transform = camera->viewProjectionMatrix(); +// glm::mat4 camTransform = camera->viewRotationMatrix(); +// psc relative = thisPosition-camera->position(); +// +// transform = transform*camTransform; +// transform = glm::translate(transform, relative.vec3()); +// transform = glm::scale(transform, _boxScaling); +// +// _colorBoxRenderer->render(transform); +// +// _textureLock->lock(); +// _kernelLock->lock(); +// +// // tell opengl to finish everything before opencl takes ownerhip (uses) the textures +// glFinish(); +// +// // Aquire GL objects +// _commands.enqueueAcquireGLObjects(_clBackTexture); +// _commands.enqueueAcquireGLObjects(_clFrontTexture); +// _commands.enqueueAcquireGLObjects(_clOutput); +// _commands.enqueueAcquireGLObjects(_clVolumes); +// _commands.enqueueAcquireGLObjects(_clTransferFunctions); +// +// _commands.enqueueKernelBlocking(_kernel, *_ws); +// _commands.finish(); +// +// // Release GL objects +// _commands.enqueueReleaseGLObjects(_clBackTexture); +// _commands.enqueueReleaseGLObjects(_clFrontTexture); +// _commands.enqueueReleaseGLObjects(_clOutput); +// _commands.enqueueReleaseGLObjects(_clVolumes); +// _commands.enqueueReleaseGLObjects(_clTransferFunctions); +// +// _quadProgram->activate(); +// glActiveTexture(GL_TEXTURE0); +// _output->bind(); +// +// // enable blending +// glEnable(GL_BLEND); +// glDisable(GL_DEPTH_TEST); +// glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); +// +// glBindVertexArray(_screenQuad); +// glDrawArrays(GL_TRIANGLES, 0, 6); +// glBindVertexArray(0); +// +// _kernelLock->unlock(); +// _textureLock->unlock(); +// +// // disable blending +// glDisable(GL_BLEND); +// glEnable(GL_DEPTH_TEST); +// glBlendFunc(GL_ONE, GL_ZERO); +// +// _quadProgram->deactivate(); +//} +// +//void RenderableVolumeExpert::update() { +// +//} +// +//void RenderableVolumeExpert::safeKernelCompilation() { +// if(_context.isValidContext()) { +// +// ghoul::opencl::CLProgram tmpProgram = _context.createProgram(_kernelSourceFile->path()); +// for(auto option: _kernelOptions) { +// tmpProgram.setOption(option.first, option.second); +// } +// +// for(auto defintion: _kernelDefinitions) { +// tmpProgram.addDefinition(defintion.first, defintion.second); +// } +// +// // add the include directories +// tmpProgram.addIncludeDirectory(_kernelIncludes); +// +// if(tmpProgram.build()) { +// ghoul::opencl::CLKernel tmpKernel = tmpProgram.createKernel("volumeraycaster"); +// if(tmpKernel.isValidKernel()) { +// +// auto begin = _kernelOptions.begin(); +// auto end = _kernelOptions.end(); +// auto f = std::find(begin, end, std::make_pair(ghoul::opencl::CLProgram::Option::KernelArgInfo, true)); +// +// int maxarguments = 1024; +// bool argumentError = false; +// if (f != end) { +// LDEBUG("Checking argument types"); +// +// using ghoul::opencl::CLKernel; +// maxarguments = countKernelArguments(tmpKernel); +// +// for (int i = 3; i maxarguments) { +// LWARNING("More arguments set than kernel accepts."); +// } +// +// // do the actual assignment behind locked doors +// _kernelLock->lock(); +// _program = tmpProgram; +// _kernel = tmpKernel; +// _kernelLock->unlock(); +// LDEBUG("Done updating kernel"); +// } +// } +// } +//} +// +//void RenderableVolumeExpert::safeUpdateTexture(const ghoul::filesystem::File& file) { +// int fileID = 0; +// for (fileID = 0; fileID < _transferFunctionsFiles.size(); ++fileID) { +// if (_transferFunctionsFiles.at(fileID) == &file) { +// //LDEBUG("Transferfunction found at id " << fileID); +// break; +// } +// } +// if(fileID == _transferFunctionsFiles.size()) +// return; +// +// LDEBUG("Updating transferfunction"); +// +// // create the new texture +// ghoul::opengl::Texture* newTexture = loadTransferFunction(file.path()); +// +// if(newTexture) { +// +// // upload the new texture and create a cl memory +// newTexture->uploadTexture(); +// cl_mem clNewTexture = _context.createTextureFromGLTexture(CL_MEM_READ_ONLY, *newTexture); +// +// // check if opencl memory is unsuccessfull +// if(clNewTexture == 0) { +// delete newTexture; +// return; +// } +// +// // everything seems ok, critical point to replace current texture pointers +// _textureLock->lock(); +// +// // deallocate current texture +// clReleaseMemObject(_clTransferFunctions.at(fileID)); +// delete _transferFunctions.at(fileID); +// +// // set the new texture +// _transferFunctions.at(fileID) = newTexture; +// _clTransferFunctions.at(fileID) = clNewTexture; +// +// // update kernel +// // __kernel arguments(front, back, output, [_volumes], .. fileID)) +// _kernel.setArgument(3 + _volumes.size() + fileID, &clNewTexture); +// +// LDEBUG("Transferfunction successfully updated"); +// +// // end of critical section +// _textureLock->unlock(); +// } +//} +// +//} // namespace openspace diff --git a/src/rendering/renderablevolumegl.cpp b/src/rendering/renderablevolumegl.cpp index ce6480c8e8..232b443b18 100644 --- a/src/rendering/renderablevolumegl.cpp +++ b/src/rendering/renderablevolumegl.cpp @@ -22,7 +22,6 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -// open space includes #include #include #include @@ -35,78 +34,84 @@ #include #include +#include namespace { - std::string _loggerCat = "RenderableVolumeGL"; + const std::string _loggerCat = "RenderableVolumeGL"; } namespace openspace { -RenderableVolumeGL::RenderableVolumeGL(const ghoul::Dictionary& dictionary): - RenderableVolume(dictionary), _boxScaling(1.0, 1.0, 1.0), - _updateTransferfunction(false), _id(-1) { +RenderableVolumeGL::RenderableVolumeGL(const ghoul::Dictionary& dictionary) + : RenderableVolume(dictionary) + , _transferFunctionName("") + , _volumeName("") + , _boxScaling(1.0, 1.0, 1.0) + , _w(0.f) + , _updateTransferfunction(false) + , _id(-1) +{ + std::string name; + bool success = dictionary.getValue(constants::scenegraphnode::keyName, name); + assert(success); _filename = ""; - if(dictionary.hasKey("Volume")) { - if(dictionary.getValue("Volume", _filename)) { - _filename = findPath(_filename); - } - } + success = dictionary.getValueSafe(constants::renderablevolumegl::keyVolume, + _filename); + if (!success) { + LERROR("Node '" << name << "' did not contain a valid '" << + constants::renderablevolumegl::keyVolume << "'"); + return; + } + _filename = findPath(_filename); - LDEBUG("filename: " << _filename); - - if(dictionary.hasKey("Hints")) - dictionary.getValue("Hints", _hintsDictionary); + LDEBUG("Volume Filename: " << _filename); + + dictionary.getValueSafe(constants::renderablevolumegl::keyHints, _hintsDictionary); _transferFunction = nullptr; _transferFunctionFile = nullptr; _transferFunctionPath = ""; - if (dictionary.hasKey("TransferFunction")) { - std::string transferFunctionPath = ""; - if(dictionary.getValue("TransferFunction", transferFunctionPath)) { - _transferFunctionPath = findPath(transferFunctionPath); - } - } - _samplerFilename = ""; - if (dictionary.hasKey("Sampler")) { - if(dictionary.getValue("Sampler", _samplerFilename)) { - _samplerFilename = findPath(_samplerFilename); - } - } - if( _transferFunctionPath == "") { - LERROR("No transferFunction!"); - } else { - _transferFunctionFile = new ghoul::filesystem::File(_transferFunctionPath, true); - } - if( _samplerFilename == "") { - LERROR("No samplerfile!"); - } + success = dictionary.getValueSafe( + constants::renderablevolumegl::keyTransferFunction, _transferFunctionPath); + if (!success) { + LERROR("Node '" << name << "' did not contain a valid '" << + constants::renderablevolumegl::keyTransferFunction << "'"); + return; + } + _transferFunctionPath = findPath(_transferFunctionPath); + _transferFunctionFile = new ghoul::filesystem::File(_transferFunctionPath, true); + + _samplerFilename = ""; + success = dictionary.getValueSafe(constants::renderablevolumegl::keySampler, + _samplerFilename); + if (!success) { + LERROR("Node '" << name << "' did not contain a valid '" << + constants::renderablevolumegl::keySampler << "'"); + return; + } + _samplerFilename = findPath(_samplerFilename); - double tempValue; - if(dictionary.hasKey("BoxScaling.1") && dictionary.getValue("BoxScaling.1", tempValue)) { - if(tempValue > 0.0) - _boxScaling[0] = tempValue; - } - if(dictionary.hasKey("BoxScaling.2") && dictionary.getValue("BoxScaling.2", tempValue)) { - if(tempValue > 0.0) - _boxScaling[1] = tempValue; - } - if(dictionary.hasKey("BoxScaling.3") && dictionary.getValue("BoxScaling.3", tempValue)) { - if(tempValue > 0.0) - _boxScaling[2] = tempValue; - } - if(dictionary.hasKey("BoxScaling.4") && dictionary.getValue("BoxScaling.4", tempValue)) { - _w = tempValue; - } - else - _w = 0.0; + glm::vec4 scalingVec4(_boxScaling, _w); + success = dictionary.getValueSafe(constants::renderablevolumegl::keyBoxScaling, + scalingVec4); + if (success) { + _boxScaling = scalingVec4.xyz; + _w = scalingVec4.w; + } + else { + success = dictionary.getValueSafe(constants::renderablevolumegl::keyBoxScaling, + _boxScaling); + if (!success) { + LERROR("Node '" << name << "' did not contain a valid '" << + constants::renderablevolumegl::keyBoxScaling << "'"); + return; + } + } - _volumeName = ""; - if (dictionary.hasKey("VolumeName")) - dictionary.getValue("VolumeName", _volumeName); - _transferFunctionName = ""; - if (dictionary.hasKey("TransferFunctionName")) - dictionary.getValue("TransferFunctionName", _transferFunctionName); + dictionary.getValueSafe(constants::renderablevolumegl::keyVolumeName, _volumeName); + dictionary.getValueSafe(constants::renderablevolumegl::keyTransferFunctionName, + _transferFunctionName); setBoundingSphere(PowerScaledScalar::CreatePSS(glm::length(_boxScaling)*pow(10,_w))); } diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index b42fd198a2..6e64d380c2 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -318,9 +318,9 @@ void ScriptEngine::addLibraryFunctions(const LuaLibrary& library, bool replace) { for (LuaLibrary::Function p : library.functions) { if (!replace) { - ghoul::lua::logStack(_state); + //ghoul::lua::logStack(_state); lua_getfield(_state, -1, p.name.c_str()); - ghoul::lua::logStack(_state); + //ghoul::lua::logStack(_state); const bool isNil = lua_isnil(_state, -1); if (!isNil) { LERROR("Function name '" << p.name << "' was already assigned"); diff --git a/src/util/factorymanager.cpp b/src/util/factorymanager.cpp index 4a6e71ff59..90b5f7eb47 100644 --- a/src/util/factorymanager.cpp +++ b/src/util/factorymanager.cpp @@ -56,13 +56,8 @@ void FactoryManager::initialize() _manager->addFactory(new ghoul::TemplateFactory); _manager->factory()->registerClass( "RenderablePlanet"); - _manager->factory()->registerClass( - "RenderableVolumeCL"); _manager->factory()->registerClass( "RenderableVolumeGL"); - _manager->factory()->registerClass( - "RenderableVolumeExpert"); - _manager->factory()->registerClass("RenderableFlare"); _manager->factory()->registerClass("RenderableFieldlines"); // Add Ephimerides