shadermanager.h 23.1 KB
Newer Older
schultezub's avatar
schultezub committed
1 2
/**********************************************************************
 *                                                                    *
3 4 5 6
 * cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2014               *
 *     Chair for Computer Aided Medical Procedures                    *
 *     Technische Universitaet Muenchen, Germany.                     *
 *     <http://campar.in.tum.de/>                                     *
schultezub's avatar
schultezub committed
7
 *                                                                    *
8 9 10 11
 * 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>                                 *
schultezub's avatar
schultezub committed
12
 *                                                                    *
13
 * This file is part of the cgt library. This library is free         *
schultezub's avatar
schultezub committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * 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/>.                        *
 *                                                                    *
 **********************************************************************/

29 30
#ifndef CGT_SHADERMANAGER_H
#define CGT_SHADERMANAGER_H
schultezub's avatar
schultezub committed
31 32

#include <list>
33
#include <set>
schultezub's avatar
schultezub committed
34 35
#include <string>

36 37 38
#include "cgt/exception.h"
#include "cgt/manager.h"
#include "cgt/matrix.h"
39
#include "cgt/cgt_gl.h"
40 41 42
#include "cgt/types.h"
#include "cgt/singleton.h"
#include "cgt/vector.h"
schultezub's avatar
schultezub committed
43

44
namespace cgt {
schultezub's avatar
schultezub committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

/**
 * Type of a shader object, can be vertex, fragment or geometry shader
 *
 * #include statements are allowed.
 *
 * Geometry shaders can be controled using directives in shader source.
 * Accepted directives:
 * GL_GEOMETRY_INPUT_TYPE_EXT(GL_POINTS | GL_LINES | GL_LINES_ADJACENCY_EXT | GL_TRIANGLES | GL_TRIANGLES_ADJACENCY_EXT)
 * GL_GEOMETRY_OUTPUT_TYPE_EXT(GL_POINTS | GL_LINE_STRIP | GL_TRIANGLE_STRIP)
 * GL_GEOMETRY_VERTICES_OUT_EXT(<int>)
 * No newline or space allowed between each pair of brackets.
 *
 * Example geometry shader header:
 * #version 120
 * #extension GL_EXT_geometry_shader4 : enable
 * //GL_GEOMETRY_INPUT_TYPE_EXT(GL_LINES)
 * //GL_GEOMETRY_OUTPUT_TYPE_EXT(GL_LINE_STRIP)
 * //GL_GEOMETRY_VERTICES_OUT_EXT(42)
 * [...]
 */
66
class CGT_API ShaderObject {
schultezub's avatar
schultezub committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
public:
    friend class Shader;
    friend class ShaderPreprocessor;

    enum ShaderType {
        VERTEX_SHADER = GL_VERTEX_SHADER,
        FRAGMENT_SHADER = GL_FRAGMENT_SHADER,
        GEOMETRY_SHADER = GL_GEOMETRY_SHADER_EXT
    };

    // Helper for resolving line number when includes are used in shader files
    struct LineInfo {
        LineInfo(int n, std::string s, int sn)
            : lineNumber_(n), filename_(s), sourceLineNumber_(sn) {}

        int lineNumber_;          //< line number in preprocessed file
        std::string filename_;    //< filename of included file
        int sourceLineNumber_;    //< line number in included file (needed when it itself
                                  //< includes another file)
    };

    /**
     * Creates a shader object of the specified type
     */
    ShaderObject(const std::string& filename, ShaderType type = VERTEX_SHADER);

    /**
     * Deletes the shader and source
     */
    ~ShaderObject();

    /**
     * Loads the shader source from the specified file.
     *
     * @throw Exception if loading failed.
     */
    void loadSourceFromFile(const std::string& filename)
        throw (Exception);

    bool compileShader();

    bool isCompiled() const { return isCompiled_; }

    std::string getCompilerLog() const;

    bool rebuildFromFile();

    /**
     * Use h as header for shadersource (copies h)
     */
    void setHeader(const std::string& h);

119 120 121 122
    void setCustomGlslVersion(const std::string& version) {
        customGlslVersion_ = version;
    }

schultezub's avatar
schultezub committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    ShaderType getType() { return shaderType_; }

    void setSource(std::string source) {
        source_ = source;
        unparsedSource_ = source;
    }
    const std::string getSource() { return unparsedSource_; }

    /**
     * Set geometry shader input type. For this change to take effect call setDirectives() and
     * re-link already linked shaders. Currently only GL_POINTS, GL_LINES,
     * GL_LINES_ADJACENCY_EXT, GL_TRIANGLES or GL_TRIANGLES_ADJACENCY_EXT can be used.
     */
    void setInputType(GLint inputType) { inputType_ = inputType; }
    GLint getInputType() const { return inputType_; }

    /**
     * Set geometry shader output type. For this change to take effect call setDirectives() and
     * re-link already linked shaders. Currently only GL_POINTS, GL_LINE_STRIP or
     * GL_TRIANGLE_STRIP can be used.
     */
    void setOutputType(GLint outputType) { outputType_ = outputType; }
    GLint getOuputType() const { return outputType_; }

    /**
     * Set maximum number of primitives a geometry shader can create.
     * For this change to take effect call setDirectives()
     * re-link already linked shaders. Limited by GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT.
     */
    void setVerticesOut(GLint verticesOut) { verticesOut_ = verticesOut; }
    GLint getVerticesOut() const { return verticesOut_; }

protected:
    void uploadSource();

    std::string filename_;
    ShaderType shaderType_;

    GLuint id_;
    std::string source_;
    std::string unparsedSource_;
    std::string header_;
165
    std::string customGlslVersion_;     ///< Optional custom GLSL version, will override global GLSL version if set
schultezub's avatar
schultezub committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    bool isCompiled_;
    GLint inputType_;
    GLint outputType_;
    GLint verticesOut_;

    std::vector<LineInfo> lineTracker_; ///< keeps track of line numbers when includes are used
    static const std::string loggerCat_;
};

//------------------------------------------------------------------------------

/**
 * Represents an OpenGL shader program, consisting of linked ShaderObjects.
 *
 * @note Convenient loading of shaders from file is provided by ShaderManager.
 */
182
class CGT_API Shader {
schultezub's avatar
schultezub committed
183 184 185 186

    friend class ShaderManager;

public:
187 188 189 190 191 192 193 194 195 196
    /**
     * Guard to enable ignoring uniform location errors and ensuring to disable it again upon destruction.
     */
    struct IgnoreUniformLocationErrorGuard {
    public:
        /**
         * Creates a new IgnoreUniformLocationErrorGuard for the given shader.
         * Sets the shader's ignoreError_ to true.
         * \param   shader  Shader to enable ignoring uniform location errors.
         */
197
        IgnoreUniformLocationErrorGuard(cgt::Shader* shader)
198 199 200 201 202 203 204 205 206 207 208 209 210 211
            : _shader(shader)
            , _stateToRestore(shader->getIgnoreUniformLocationError())
        {
            _shader->setIgnoreUniformLocationError(true);
        };

        /**
         * Destructor, restores the shader's original ignoreError_ state.
         */
        ~IgnoreUniformLocationErrorGuard() {
            _shader->setIgnoreUniformLocationError(_stateToRestore);
        }

    private:
212
        cgt::Shader* _shader;   ///< Shader to modify
213 214 215
        bool _stateToRestore;   ///< Original state
    };

schultezub's avatar
schultezub committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    Shader();

    /**
     * Detaches all shader objects, deletes them an disposes all textures
     */
    ~Shader();

    /**
     * Attach shader object to Shader
     */
    void attachObject(ShaderObject* obj);
    void detachObject(ShaderObject* obj);
    void detachObjectsByType(ShaderObject::ShaderType type);

    /**
     * Link all shader objects to one shader.
     * Will re-link already linked shaders.
     * @return true for success
     */
    bool linkProgram();

    bool rebuild();
    bool rebuildFromFile();

    void setHeaders(const std::string& customHeader);

    void bindFragDataLocation(GLuint colorNumber, std::string name);

    GLint getID() {return id_;};
    /**
     * Activates the shader
     */
    void activate();

    static void activate(GLint id);

    static void deactivate();

    static GLint getCurrentProgram();


    /**
     * Returns whether the Shader is currently activated
     */
    bool isActivated();

    bool isLinked() { return isLinked_; }

    std::string getLinkerLog() const;

266 267 268 269
    // Subroutines
    bool selectSubroutine(ShaderObject::ShaderType type, const std::string& subroutineName);


schultezub's avatar
schultezub committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    //
    // Uniform stuff
    //

    /**
     * Returns uniform location, or -1 on failure
     */
    GLint getUniformLocation(const std::string& name);

    void setIgnoreUniformLocationError(bool ignoreError);
    bool getIgnoreUniformLocationError();

    // Floats
    bool setUniform(const std::string& name, GLfloat value);
    bool setUniform(const std::string& name, GLfloat v1, GLfloat v2);
    bool setUniform(const std::string& name, GLfloat v1, GLfloat v2, GLfloat v3);
    bool setUniform(const std::string& name, GLfloat v1, GLfloat v2, GLfloat v3, GLfloat v4);
    bool setUniform(const std::string& name, GLfloat* v, int count);

    // Integers
    bool setUniform(const std::string& name, GLint value);
    bool setUniform(const std::string& name, GLint v1, GLint v2);
    bool setUniform(const std::string& name, GLint v1, GLint v2, GLint v3);
    bool setUniform(const std::string& name, GLint v1, GLint v2, GLint v3, GLint v4);
    bool setUniform(const std::string& name, GLint* v, int count);

296 297 298 299 300 301 302
    // Unsigned Integers
    bool setUniform(const std::string& name, GLuint value);
    bool setUniform(const std::string& name, GLuint v1, GLuint v2);
    bool setUniform(const std::string& name, GLuint v1, GLuint v2, GLuint v3);
    bool setUniform(const std::string& name, GLuint v1, GLuint v2, GLuint v3, GLuint v4);
    bool setUniform(const std::string& name, GLuint* v, int count);

schultezub's avatar
schultezub committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    // Booleans
    bool setUniform(const std::string& name, bool value);
    bool setUniform(const std::string& name, bool v1, bool v2);
    bool setUniform(const std::string& name, bool v1, bool v2, bool v3);
    bool setUniform(const std::string& name, bool v1, bool v2, bool v3, bool v4);
    bool setUniform(const std::string& name, GLboolean* v, int count);

    // Vectors
    bool setUniform(const std::string& name, const Vector2f& value);
    bool setUniform(const std::string& name, Vector2f* vectors, GLsizei count = 1);
    bool setUniform(const std::string& name, const Vector3f& value);
    bool setUniform(const std::string& name, Vector3f* vectors, GLsizei count = 1);
    bool setUniform(const std::string& name, const Vector4f& value);
    bool setUniform(const std::string& name, Vector4f* vectors, GLsizei count = 1);
    bool setUniform(const std::string& name, const ivec2& value);
    bool setUniform(const std::string& name, ivec2* vectors, GLsizei count = 1);
    bool setUniform(const std::string& name, const ivec3& value);
    bool setUniform(const std::string& name, ivec3* vectors, GLsizei count = 1);
    bool setUniform(const std::string& name, const ivec4& value);
    bool setUniform(const std::string& name, ivec4* vectors, GLsizei count = 1);

    // Note: Matrix is transposed by OpenGL
    bool setUniform(const std::string& name, const Matrix2f& value, bool transpose = false);
    bool setUniform(const std::string& name, const Matrix3f& value, bool transpose = false);
    bool setUniform(const std::string& name, const Matrix4f& value, bool transpose = false);

    // No location lookup
    //
    // Floats
    static void setUniform(GLint l, GLfloat value);
    static void setUniform(GLint l, GLfloat v1, GLfloat v2);
    static void setUniform(GLint l, GLfloat v1, GLfloat v2, GLfloat v3);
    static void setUniform(GLint l, GLfloat v1, GLfloat v2, GLfloat v3, GLfloat v4);

    // Integers
    static void setUniform(GLint l, GLint value);
    static void setUniform(GLint l, GLint v1, GLint v2);
    static void setUniform(GLint l, GLint v1, GLint v2, GLint v3);
    static void setUniform(GLint l, GLint v1, GLint v2, GLint v3, GLint v4);

    // Vectors
    static void setUniform(GLint l, const Vector2f& value);
    static void setUniform(GLint l, const Vector3f& value);
    static void setUniform(GLint l, const Vector4f& value);
    static void setUniform(GLint l, const ivec2& value);
    static void setUniform(GLint l, const ivec3& value);
    static void setUniform(GLint l, const ivec4& value);
    static void setUniform(GLint l, const Matrix2f& value, bool transpose = false);
    static void setUniform(GLint l, const Matrix3f& value, bool transpose = false);
    static void setUniform(GLint l, const Matrix4f& value, bool transpose = false);

    // Attributes
    //
    // 1 component
    static void setAttribute(GLint index, GLfloat v1);
    static void setAttribute(GLint index, GLshort v1);
    static void setAttribute(GLint index, GLdouble v1);

    // 2 components
    static void setAttribute(GLint index, GLfloat v1, GLfloat v2);
    static void setAttribute(GLint index, GLshort v1, GLshort v2);
    static void setAttribute(GLint index, GLdouble v1, GLdouble v2);

    // 3 components
    static void setAttribute(GLint index, GLfloat v1, GLfloat v2, GLfloat v3);
    static void setAttribute(GLint index, GLshort v1, GLshort v2, GLshort v3);
    static void setAttribute(GLint index, GLdouble v1, GLdouble v2, GLdouble v3);

    // 4 components
    static void setAttribute(GLint index, GLfloat v1, GLfloat v2, GLfloat v3, GLfloat v4);
    static void setAttribute(GLint index, GLshort v1, GLshort v2, GLshort v3, GLshort v4);
    static void setAttribute(GLint index, GLdouble v1, GLdouble v2, GLdouble v3, GLdouble v4);

    // For vectors
    static void setAttribute(GLint index, const Vector2f& v);
    static void setAttribute(GLint index, const Vector3f& v);
    static void setAttribute(GLint index, const Vector4f& v);

    static void setAttribute(GLint index, const Vector2d& v);
    static void setAttribute(GLint index, const Vector3d& v);
    static void setAttribute(GLint index, const Vector4d& v);

    static void setAttribute(GLint index, const Vector2<GLshort>& v);
    static void setAttribute(GLint index, const Vector3<GLshort>& v);
    static void setAttribute(GLint index, const Vector4<GLshort>& v);

    static void setAttribute(GLint index, const Vector4<GLint>& v);
    static void setAttribute(GLint index, const Vector4<GLbyte>& v);
    static void setAttribute(GLint index, const Vector4<GLubyte>& v);
    static void setAttribute(GLint index, const Vector4<GLushort>& v);
    static void setAttribute(GLint index, const Vector4<GLuint>& v);

    // Attribute locations
396 397 398
    // ATTENTION: This method was deliberately deactivated, since it was not working as expected
    //            with AMD GPUs.
    // void setAttributeLocation(GLuint index, const std::string& name);
schultezub's avatar
schultezub committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    GLint getAttributeLocation(const std::string& name);

    // Normalized attributes
    static void setNormalizedAttribute(GLint index, GLubyte v1, GLubyte v2, GLubyte v3, GLubyte v4);

    static void setNormalizedAttribute(GLint index, const Vector4<GLbyte>& v);
    static void setNormalizedAttribute(GLint index, const Vector4<GLshort>& v);
    static void setNormalizedAttribute(GLint index, const Vector4<GLint>& v);

    // Unsigned version
    static void setNormalizedAttribute(GLint index, const Vector4<GLubyte>& v);
    static void setNormalizedAttribute(GLint index, const Vector4<GLushort>& v);
    static void setNormalizedAttribute(GLint index, const Vector4<GLuint>& v);

protected:
    /**
     * Load filename.vert and filename.frag (vertex and fragment shader) and link shader.
     *
     * @param customHeader Header to be put in front of the shader source.
     *
     * @throw Exception if loading failed
     */
    void load(const std::string& filename, const std::string& customHeader = "")
        throw (Exception);

    /**
     * Load vertex shader \p vertFilename, geometry shader \p geomFilename,
     * fragment shader \p fragFilename.
     *
     * @param customHeader header to be put in front of the shader source
     *
     * @throw Exception if loading failed
     */
    void loadSeparate(const std::string& vertFilename, const std::string& geomFilename,
433
        const std::string& fragFilename, const std::string& customHeader = "", const std::string& customGlslVersion = "")
schultezub's avatar
schultezub committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
        throw (Exception);

    typedef std::list<ShaderObject*> ShaderObjects;
    ShaderObjects objects_;

    GLuint id_;
    bool isLinked_;
    bool ignoreError_;

    static const std::string loggerCat_;
};

//------------------------------------------------------------------------------

class ShaderManager;
#ifdef DLL_TEMPLATE_INST
450
template class CGT_API Singleton<ShaderManager>;
schultezub's avatar
schultezub committed
451 452
#endif
#ifdef DLL_TEMPLATE_INST
453
template class CGT_API ResourceManager<Shader>;
schultezub's avatar
schultezub committed
454 455 456 457 458 459 460
#endif

/**
 * Loads shaders from the file system, managing a shader search path.
 *
 * @see ResourceManager
 */
461
class CGT_API ShaderManager : public ResourceManager<Shader>, public Singleton<ShaderManager> {
schultezub's avatar
schultezub committed
462 463 464 465 466
public:

    ShaderManager();

    /**
467 468 469 470 471 472 473 474 475
     * Load given shaders from file and link them.
     * Empty file names will be ignored (not loaded and linked). You have to pass the complete 
     * file names, inclusive file extensions (".vert", ".geom", frag").
     * 
     * \param   vertFilename        Vertex shader file name
     * \param   fragFilename        Fragment shader file name
     * \param   customHeader        Custom header to add to all shaders (may be empty)
     * \throw   Exception if loading failed.
     * \return  The newly created shader.
schultezub's avatar
schultezub committed
476
     */
477 478
    Shader* load(const std::string& vertFilename, const std::string& fragFilename,
                 const std::string& customHeader)
schultezub's avatar
schultezub committed
479 480 481
                 throw (Exception);

    /**
482 483 484 485 486 487 488 489 490 491
     * Load given shaders from file and link them.
     * Empty file names will be ignored (not loaded and linked). You have to pass the complete 
     * file names, inclusive file extensions (".vert", ".geom", frag").
     * 
     * \param   vertFilename        Vertex shader file name
     * \param   geomFilename        Geometry shader file name (leave empty to disable geometry shader)
     * \param   fragFilename        Fragment shader file name
     * \param   customHeader        Custom header to add to all shaders (may be empty)
     * \throw   Exception if loading failed.
     * \return  The newly created shader.
schultezub's avatar
schultezub committed
492
     */
493 494 495
    Shader* load(const std::string& vertFilename, const std::string& geomFilename, const std::string& fragFilename,
                 const std::string& customHeader)
                 throw(Exception);
schultezub's avatar
schultezub committed
496 497

    /**
498 499 500 501 502 503 504 505 506 507 508
     * Load given shaders from file and link them.
     * Empty file names will be ignored (not loaded and linked). You have to pass the complete 
     * file names, inclusive file extensions (".vert", ".geom", frag").
     * 
     * \param   vertFilename        Vertex shader file name
     * \param   geomFilename        Geometry shader file name (leave empty to disable geometry shader)
     * \param   fragFilename        Fragment shader file name
     * \param   customHeader        Custom header to add to all shaders (may be empty)
     * \param   customGlslVersion   Custom GLSL version for all shaders (leave empty to use default GLSL version from ShaderManager).
     * \throw   Exception if loading failed.
     * \return  The newly created shader.
schultezub's avatar
schultezub committed
509
     */
510 511 512
    Shader* loadWithCustomGlslVersion(const std::string& vertFilename, const std::string& geomFilename, const std::string& fragFilename, 
                                      const std::string& customHeader, const std::string& customGlslVersion)
                                      throw(Exception);
schultezub's avatar
schultezub committed
513 514 515

    bool rebuildAllShadersFromFile();

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    /**
     * Sets the global header that will be added to all shaders to \a header.
     * \param   header  The new global header that will be added to all shaders.
     */
    void setGlobalHeader(const std::string& header) {
        globalHeader_ = header;
    }

    /**
     * Gets the global header that will be added to all shaders.
     * \return  globalHeader_
     */
    const std::string& getGlobalHeader() const {
        return globalHeader_;
    }

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    /**
     * Sets the default GLSL version string, will be added to the '#version' pragma 
     * at the beginning of each shader.
     * \param   version     Default GLSL version string.
     */
    void setDefaultGlslVersion(const std::string& version) {
        defaultGlslVersion_ = version;
    }

    /**
     * Gets the default GLSL version string, will be added to the '#version' pragma 
     * at the beginning of each shader.
     */
    const std::string& getDefaultGlslVersion() const {
        return defaultGlslVersion_;
    }

schultezub's avatar
schultezub committed
549
protected:
550
    std::string defaultGlslVersion_;    ///< Default GLSL version string, will be added to the '#version' pragma at the beginning of each shader
551 552
    std::string globalHeader_;      ///< Global header that will be added to all shaders.

schultezub's avatar
schultezub committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    static const std::string loggerCat_;
};

/**
 * Parses #include statements and geometry shader settings
 */
class ShaderPreprocessor {
public:
    enum Mode {
        MODE_NONE      = 0,
        MODE_INCLUDE   = 1,
        MODE_GEOMETRY  = 2
    };

    ShaderPreprocessor(ShaderObject* obj, Mode mode = Mode(MODE_INCLUDE | MODE_GEOMETRY));

    // Returns the parsed result
    std::string getResult() const;

    GLint getGeomShaderInputType() const;
    GLint getGeomShaderOutputType() const;
    GLint getGeomShaderVerticesOut() const;

protected:
    void parse();
    void parsePart(const std::string& input, const std::string& name = "");

    void outputComment(const std::string& comment, const std::string& type = "INFO");

    /**
     *    Scan for geometry shader directives in shader source.
     *
     *    Accepted directives:
     *    GL_GEOMETRY_INPUT_TYPE_EXT(GL_POINTS | GL_LINES | GL_LINES_ADJACENCY_EXT | GL_TRIANGLES | GL_TRIANGLES_ADJACENCY_EXT)
     *    GL_GEOMETRY_OUTPUT_TYPE_EXT(GL_POINTS | GL_LINE_STRIP | GL_TRIANGLE_STRIP)
     *    GL_GEOMETRY_VERTICES_OUT_EXT(<int>)
     *    No newline or space allowed between each pair of brackets.
     *
     *    Example geometry shader header:
     *    #version 120
     *    #extension GL_EXT_geometry_shader4 : enable
     *
     *    //GL_GEOMETRY_INPUT_TYPE_EXT(GL_LINES)
     *    //GL_GEOMETRY_OUTPUT_TYPE_EXT(GL_LINE_STRIP)
     *    //GL_GEOMETRY_VERTICES_OUT_EXT(42)
     *    [...]
     */
    bool scanGeomShaderDirectives();

    std::string getGeomShaderDirective(const std::string& d);


    ShaderObject* shd_;
    std::vector<ShaderObject::LineInfo>& lineTracker_; ///< keeps track of line numbers when includes are used
607
    std::set<std::string> includedFilenames_;           ///< list of all filenames that have already been included (to avoid double inclusion)
schultezub's avatar
schultezub committed
608 609 610 611 612 613 614 615 616 617
    int activeLine_;
    std::ostringstream result_;

    GLint inputType_;
    GLint outputType_;
    GLint verticesOut_;

    static const std::string loggerCat_;
};

618
} // namespace cgt
schultezub's avatar
schultezub committed
619

620
#define ShdrMgr cgt::Singleton<cgt::ShaderManager>::getRef()
schultezub's avatar
schultezub committed
621

622
#endif //CGT_SHADERMANAGER_H