Commit 445c22ba authored by Jakob Weiss's avatar Jakob Weiss
Browse files

Improvements to the CS support interface

* CSHelper ns contains methods to simplify boilerplate code
* some changes to the cgt::Texture interface
* Shader code now dumped to console when compilation fails (could still use some reworking, i.e. like line numbers)
* updated MedianFilter code
parent 2a6bc7a6
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2015, 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.
// 
// ================================================================================================

// (c) 2016 Jakob Weiss <jakob.weiss@tum.de>

#include "cshelper.h"

#include <sstream>

#include <cgt/texture.h> 
#include <cgt/imageunit.h>
#include <cgt/gltextureformattraits.h>

namespace campvis {
    namespace CSHelper {
        //const std::string loggerCat_ = "CAMPVis.core.tools.CSHelper";
        CAMPVIS_CORE_API std::string generateGLSLImageDefinition(const cgt::Texture & tex, const std::string& uniformName, const cgt::ImageUnit& imgUnit)
        {
            std::stringstream ss;
            auto fmtTraits = cgt::GLTextureFormatTraits::get(tex.getInternalFormat());
            ss << "layout(" << fmtTraits.glslFormatQualifier() << ", binding = " << imgUnit.getUnitNumber() << ") uniform "; // "layout( (r8|r16|rgba16|...) uniform"
            ss << (fmtTraits.isIntegerFormat() ? (fmtTraits.isSignedFormat() ? "iimage" : "uimage") : "image"); // "(i|u|_)image"
            ss << ( (tex.getType() == GL_TEXTURE_1D) ? 1 : ( (tex.getType() == GL_TEXTURE_2D) ? 2 : 3) ) << "D "; // "(1|2|3)D"
            ss << uniformName << ";" << std::endl;
            
            return ss.str();
        }
    }
}
 No newline at end of file

core/tools/cshelper.h

0 → 100644
+56 −0
Original line number Diff line number Diff line
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2015, 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.
// 
// ================================================================================================

// (c) 2016 Jakob Weiss <jakob.weiss@tum.de>

#ifndef CSHELPER_H__
#define CSHELPER_H__

#include <string>

#include "core/coreapi.h"

namespace cgt {
    class Texture;
    class ImageUnit;
}


namespace campvis {
    /**
     * CSHelper namespace
     * Collection of various helper classes and functions to simplify using Compute Shaders for various applications
     */
    namespace CSHelper {
        /// Loggin category for the LDEBUG macros
        //const std::string loggerCat_;

        /**
         *	Generates a glsl image definition that matches the texture type and layout
         */
        CAMPVIS_CORE_API std::string generateGLSLImageDefinition(const cgt::Texture& tex, const std::string& uniformName, const cgt::ImageUnit& imgUnit);
    }
}

#endif // CSHELPER_H__
+1 −1
Original line number Diff line number Diff line
@@ -176,7 +176,7 @@ namespace campvis {
        }

        // read back stuff
        GLenum readBackFormat = cgt::Texture::calcMatchingFormat(outputTex->getInternalFormat());
        GLenum readBackFormat = cgt::Texture::calcMatchingPixelFormat(outputTex->getInternalFormat());
        size_t channels = outputTex->getNumChannels();
        toReturn.resize(channels);
        glReadBuffer(GL_COLOR_ATTACHMENT0);
+0 −1
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ SET(CGT_SOURCES
	frustum.cpp
	glcanvas.cpp
	glcontextmanager.cpp
	gltextureformattraits.cpp
	gpucapabilities.cpp
	gpucapabilitieswindows.cpp
	imageunit.cpp

ext/cgt/gltextureformattraits.cpp

deleted100644 → 0
+0 −138
Original line number Diff line number Diff line
/**********************************************************************
*                                                                    *
* cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2015               *
*     Chair for Computer Aided Medical Procedures                    *
*     Technische Universitaet Muenchen, Germany.                     *
*     <http://campar.in.tum.de/>                                     *
*                                                                    *
* forked from tgt - Tiny Graphics Toolbox, Copyright (C) 2006-2011   *
*     Visualization and Computer Graphics Group, Department of       *
*     Computer Science, University of Muenster, Germany.             *
*     <http://viscg.uni-muenster.de>                                 *
*                                                                    *
* This file is part of the cgt library. This library is free         *
* software; you can redistribute it and/or modify it under the terms *
* of the GNU Lesser General Public License version 2.1 as published  *
* by the Free Software Foundation.                                   *
*                                                                    *
* This library is distributed in the hope that it will be useful,    *
* but WITHOUT ANY WARRANTY; without even the implied warranty of     *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
* GNU Lesser General Public License for more details.                *
*                                                                    *
* You should have received a copy of the GNU Lesser General Public   *
* License in the file "LICENSE.txt" along with this library.         *
* If not, see <http://www.gnu.org/licenses/>.                        *
*                                                                    *
**********************************************************************/

#include "gltextureformattraits.h"

namespace cgt {

//#define defFormat(INTERNAL_FORMAT, BPP, CHANNELS, PIXEL_FORMAT, PIXEL_DATA_TYPE, GLSL_FORMAT_QUALIFIER, GLSL_IMAGE_TYPE) \
//template <> \
//class GLTextureFormatTraits<INTERNAL_FORMAT> { \
//    const std::string internalFormatName() { return #INTERNAL_FORMAT; }; \
//    const int bpp() { return BPP; };\
//    const int channels() { return CHANNELS; };\
//    const GLint internalFormat() { return INTERNAL_FORMAT; };\
//    const GLint pixelFormat() { return PIXEL_FORMAT; };\
//    const GLint pixelDataType() { return PIXEL_DATA_TYPE; };\
//    const std::string glslFormatQualifier() { return GLSL_FORMAT_QUALIFIER; };\
//    const std::string glslImageType() { return GLSL_IMAGE_TYPE; };\
//};

#define defFormat(INTERNAL_FORMAT, BPP, CHANNELS, PIXEL_FORMAT, PIXEL_DATA_TYPE, GLSL_FORMAT_QUALIFIER) \
    case INTERNAL_FORMAT: return GLTextureFormatTraits(INTERNAL_FORMAT, #INTERNAL_FORMAT, BPP, CHANNELS, PIXEL_FORMAT, PIXEL_DATA_TYPE, GLSL_FORMAT_QUALIFIER);


GLTextureFormatTraits GLTextureFormatTraits::get(GLint internalFormat) 
{
    switch(internalFormat) {
        defFormat(GL_DEPTH_COMPONENT         ,   1,   1, GL_DEPTH_COMPONENT, GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_R8                      ,   1,   1, GL_RED            , GL_UNSIGNED_BYTE , "r8");
        defFormat(GL_R8_SNORM                ,   1,   1, GL_RED            , GL_BYTE          , "r8_snorm");
        defFormat(GL_R8I                     ,   1,   1, GL_RED_INTEGER    , GL_BYTE          , "r8i");
        defFormat(GL_R8UI                    ,   1,   1, GL_RED_INTEGER    , GL_UNSIGNED_BYTE , "r8ui");
        defFormat(GL_R3_G3_B2                ,   1,   3, GL_RGB            , GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_RGBA2                   ,   1,   4, GL_RGB            , GL_UNSIGNED_BYTE , "**UNKNOWN**");
        //                                   , bpp, chn, pixel_format      , pixel_data_type, "**UNKNOWN**");
        defFormat(GL_DEPTH_COMPONENT16       ,   2,   1, GL_DEPTH_COMPONENT, GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_R16                     ,   2,   1, GL_RED            , GL_UNSIGNED_SHORT, "r16");
        defFormat(GL_R16_SNORM               ,   2,   1, GL_RED            , GL_SHORT         , "r16_snorm");
        defFormat(GL_R16F                    ,   2,   1, GL_RED            , GL_FLOAT         , "r16f");
        defFormat(GL_R16I                    ,   2,   1, GL_RED_INTEGER    , GL_SHORT         , "r16i");
        defFormat(GL_R16UI                   ,   2,   1, GL_RED_INTEGER    , GL_UNSIGNED_SHORT, "r16ui");
        defFormat(GL_DEPTH_STENCIL           ,   2,   1, GL_RG             , GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_RG8                     ,   2,   2, GL_RG             , GL_UNSIGNED_BYTE , "rg8");
        defFormat(GL_RG8_SNORM               ,   2,   2, GL_RG             , GL_BYTE          , "rg8_snorm");
        defFormat(GL_RG8I                    ,   2,   2, GL_RG_INTEGER     , GL_BYTE          , "rg8i");
        defFormat(GL_RG8UI                   ,   2,   2, GL_RG_INTEGER     , GL_UNSIGNED_BYTE , "rg8ui");
        defFormat(GL_RGB4                    ,   2,   3, GL_RGB            , GL_UNSIGNED_BYTE , "**UNKNOWN**");
        defFormat(GL_RGB5                    ,   2,   3, GL_RGB            , GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_RGBA4                   ,   2,   4, GL_RGBA           , GL_UNSIGNED_BYTE , "**UNKNOWN**");
        defFormat(GL_RGB5_A1                 ,   2,   4, GL_RGBA           , GL_FLOAT         , "**UNKNOWN**");
        //                                   , bpp, chn, pixel_format      , pixel_data_type, "**UNKNOWN**");
        defFormat(GL_DEPTH_COMPONENT24       ,   3,   1, GL_DEPTH_COMPONENT, GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_RGB8                    ,   3,   3, GL_RGB            , GL_UNSIGNED_BYTE , "**UNKNOWN**");
        defFormat(GL_RGB8_SNORM              ,   3,   3, GL_RGB            , GL_BYTE          , "**UNKNOWN**");
        defFormat(GL_SRGB8                   ,   3,   3, GL_RGB            , GL_UNSIGNED_BYTE , "**UNKNOWN**");
        defFormat(GL_RGB8I                   ,   3,   3, GL_RGB_INTEGER    , GL_BYTE          , "**UNKNOWN**");
        defFormat(GL_RGB8UI                  ,   3,   3, GL_RGB_INTEGER    , GL_UNSIGNED_BYTE , "**UNKNOWN**");
        //                                   , bpp, chn, pixel_format      , pixel_data_type, "**UNKNOWN**");
        defFormat(GL_DEPTH_COMPONENT32       ,   4,   1, GL_DEPTH_COMPONENT, GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_DEPTH_COMPONENT32F      ,   4,   1, GL_DEPTH_COMPONENT, GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_R32F                    ,   4,   1, GL_RED            , GL_FLOAT         , "r32f");
        defFormat(GL_R32I                    ,   4,   1, GL_RED_INTEGER    , GL_INT           , "r32i");
        defFormat(GL_R32UI                   ,   4,   1, GL_RED_INTEGER    , GL_UNSIGNED_INT  , "r32ui");
        defFormat(GL_RG16                    ,   4,   2, GL_RG             , GL_UNSIGNED_SHORT, "rg16");
        defFormat(GL_RG16_SNORM              ,   4,   2, GL_RG             , GL_SHORT         , "rg16_snorm");
        defFormat(GL_RG16F                   ,   4,   2, GL_RG             , GL_FLOAT         , "rg16f");
        defFormat(GL_RG16I                   ,   4,   2, GL_RG_INTEGER     , GL_SHORT         , "rg16i");
        defFormat(GL_RG16UI                  ,   4,   2, GL_RG_INTEGER     , GL_UNSIGNED_SHORT, "rg16ui");
        defFormat(GL_RGB10                   ,   4,   3, GL_RGB            , GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_R11F_G11F_B10F          ,   4,   3, GL_RGB            , GL_FLOAT         , "r11f_g11f_b10f");
        defFormat(GL_RGB9_E5                 ,   4,   3, GL_RGB            , GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_RGBA8                   ,   4,   4, GL_RGBA           , GL_UNSIGNED_BYTE , "rgba8");
        defFormat(GL_RGBA8_SNORM             ,   4,   4, GL_RGBA           , GL_BYTE          , "rgba8_snorm");
        defFormat(GL_RGB10_A2                ,   4,   4, GL_RGBA           , GL_FLOAT         , "rgb10_a2");
        defFormat(GL_RGB10_A2UI              ,   4,   4, GL_RGBA_INTEGER   , GL_FLOAT         , "rgb10_a2ui");
        defFormat(GL_SRGB8_ALPHA8            ,   4,   4, GL_RGBA           , GL_UNSIGNED_BYTE , "**UNKNOWN**");
        defFormat(GL_RGBA8I                  ,   4,   4, GL_RGBA_INTEGER   , GL_BYTE          , "rgba8i");
        defFormat(GL_RGBA8UI                 ,   4,   4, GL_RGBA_INTEGER   , GL_UNSIGNED_BYTE , "rgba8ui");
        //                                   , bpp, chn, pixel_format      , pixel_data_type, "**UNKNOWN**");
        defFormat(GL_RGB12                   ,   5,   3, GL_RGB            , GL_FLOAT         , "**UNKNOWN**");
        //                                   , bpp, chn, pixel_format      , pixel_data_type, "**UNKNOWN**");
        defFormat(GL_RGB16                   ,   6,   3, GL_RGB            , GL_UNSIGNED_SHORT, "glslfmptq");
        defFormat(GL_RGB16_SNORM             ,   6,   3, GL_RGB            , GL_SHORT         , "**UNKNOWN**");
        defFormat(GL_RGB16F                  ,   6,   3, GL_RGB            , GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_RGB16I                  ,   6,   3, GL_RGB_INTEGER    , GL_SHORT         , "**UNKNOWN**");
        defFormat(GL_RGB16UI                 ,   6,   3, GL_RGB_INTEGER    , GL_UNSIGNED_SHORT, "**UNKNOWN**");
        defFormat(GL_RGBA12                  ,   6,   4, GL_RGBA           , GL_FLOAT         , "**UNKNOWN**");
        //                                   , bpp, chn, pixel_format      , pixel_data_type, "**UNKNOWN**");
        defFormat(GL_RG32F                   ,   8,   2, GL_RG             , GL_FLOAT         , "rg32f");
        defFormat(GL_RG32I                   ,   8,   2, GL_RG_INTEGER     , GL_INT           , "rg32i");
        defFormat(GL_RG32UI                  ,   8,   2, GL_RG_INTEGER     , GL_UNSIGNED_INT  , "rg32ui");
        defFormat(GL_RGBA16                  ,   8,   4, GL_RGBA           , GL_FLOAT         , "rgba16");
        defFormat(GL_RGBA16_SNORM            ,   8,   4, GL_RGBA           , GL_SHORT         , "rgba16_snorm");
        defFormat(GL_RGBA16F                 ,   8,   4, GL_RGBA           , GL_FLOAT         , "rgba16f");
        defFormat(GL_RGBA16I                 ,   8,   4, GL_RGBA_INTEGER   , GL_SHORT         , "rgba16i");
        defFormat(GL_RGBA16UI                ,   8,   4, GL_RGBA_INTEGER   , GL_UNSIGNED_SHORT, "rgba16ui");
        //                                   , bpp, chn, pixel_format      , pixel_data_type, "**UNKNOWN**");
        defFormat(GL_RGB32F                  ,  12,   3, GL_RGB            , GL_FLOAT         , "**UNKNOWN**");
        defFormat(GL_RGB32I                  ,  12,   3, GL_RGB_INTEGER    , GL_INT           , "**UNKNOWN**");
        defFormat(GL_RGB32UI                 ,  12,   3, GL_RGB_INTEGER    , GL_UNSIGNED_INT  , "**UNKNOWN**");
        //                                   , bpp, chn, pixel_format      , pixel_data_type, "**UNKNOWN**");
        defFormat(GL_RGBA32F                 ,  16,   4, GL_RGBA           , GL_FLOAT         , "rgba32f");
        defFormat(GL_RGBA32I                 ,  16,   4, GL_RGBA_INTEGER   , GL_INT           , "rgba32i");
        defFormat(GL_RGBA32UI                ,  16,   4, GL_RGBA_INTEGER   , GL_UNSIGNED_INT  , "rgba32ui");
    
    default: // for unknown/invalid texture format specifiers
        // Log a warning here??
        return GLTextureFormatTraits(internalFormat);
    }
}


}; // namspace cgt
 No newline at end of file
Loading