UniformBuffer.hh 7.76 KB
Newer Older
1 2 3 4 5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
Robert Menzel's avatar
Robert Menzel committed
6 7 8 9

#ifndef ACGL_OPENGL_OBJECTS_UNIFORM_BUFFER_HH
#define ACGL_OPENGL_OBJECTS_UNIFORM_BUFFER_HH

Robert Menzel's avatar
Robert Menzel committed
10
/**
Robert Menzel's avatar
Robert Menzel committed
11 12 13 14 15
 * A uniform buffer is an OpenGL buffer object bound to a uniform buffer location.
 * It can be used:
   * provide the same set of uniforms to different ShaderPrograms without setting
     the values multiple times
   * set multiple uniform variables at once by mapping the buffer into the CPU
Robert Menzel's avatar
Robert Menzel committed
16 17
     memory, memset all values and unmap it (quickest way to change a lot of uniforms).
   * quick switching between multiple sets of uniforms (e.g. material properties).
Robert Menzel's avatar
Robert Menzel committed
18 19 20 21 22 23 24 25 26 27
 *
 * To be used, uniforms must be organized in a uniform block, this block has to be bound
 * to the same location the uniform buffer gets bound to (quite similar to textures).
 *
 * If only advantage one is requested, the individual offsets along with the uniform names
 * can be saved in a uniform buffer to set the uniforms by setUniform() as it would be done
 * for normal uniforms in a ShaderProgram.
 * Otherwise the exact memory layout must be known, which can be queried by OpenGL, but which
 * is also well defined in the case of std140-blocks (see OpenGL spec).
 *
Robert Menzel's avatar
Robert Menzel committed
28 29
 * In contrast to ShaderPrograms, nothing has to be activated before setUniform can be called
 * here.
Robert Menzel's avatar
Robert Menzel committed
30 31
 */

Robert Menzel's avatar
Robert Menzel committed
32 33 34 35 36 37 38
#include <ACGL/ACGL.hh>

#include <ACGL/Base/Macros.hh>
#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Tools.hh>
#include <ACGL/OpenGL/Objects/Buffer.hh>

Robert Menzel's avatar
Robert Menzel committed
39
#include <ACGL/OpenGL/Data/LocationMappings.hh>
Robert Menzel's avatar
Robert Menzel committed
40 41
#include <ACGL/Math/Math.hh>

Robert Menzel's avatar
Robert Menzel committed
42
#if (ACGL_OPENGL_VERSION >= 31)
Robert Menzel's avatar
Robert Menzel committed
43 44 45
namespace ACGL{
namespace OpenGL{

46 47
class ShaderProgram;

Robert Menzel's avatar
Robert Menzel committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61
class UniformBuffer : public Buffer
{
    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
public:
    UniformBuffer()
        : Buffer(GL_UNIFORM_BUFFER)
    {}

    UniformBuffer( SharedBufferObject _pBuffer )
         : Buffer(_pBuffer, GL_UNIFORM_BUFFER)
    {}

62
    //! inits the uniformbuffer to be used with the given shaderprogram, _uboName is the name of this buffer in the shader
63
    UniformBuffer( const ptr::shared_ptr<const ShaderProgram> &_shaderProgram, const std::string &_uboName );
64

Robert Menzel's avatar
Robert Menzel committed
65 66 67 68
    // ==================================================================================================== \/
    // ============================================================================================ GETTERS \/
    // ==================================================================================================== \/
public:
Robert Menzel's avatar
Robert Menzel committed
69 70 71 72 73 74 75
    /** returns the byte offset of a uniform within the buffer
     * Needed to upload the new value of the uniform.
     * Initially the UniformBuffer does not know any locations as it is just unstructured memory with no
     * intrinsic meaning, so the locations have to be queried from the matching ShaderProgram and provided
     * to the UniformBuffer. This is optional, the application can also know the layout and upload the data
     * without storing/querying the mappings to/from the buffer.
     */
Robert Menzel's avatar
Robert Menzel committed
76 77 78 79 80 81 82 83 84 85 86 87 88
    GLint getUniformOffset (const std::string& _nameInShader) const {
        if (!uniformNameToOffsetMap) return -1;
        return uniformNameToOffsetMap->getLocation(_nameInShader);
    }
    void setUniformOffsets (SharedLocationMappings _uniformNameToOffsetMap) { uniformNameToOffsetMap = _uniformNameToOffsetMap; }

    // ==================================================================================================== \/
    // ============================================================================================ SETTERS \/
    // ==================================================================================================== \/
public:
    //! reserve a number of bytes on the GPU for uniforms
    inline void reserveMemory(GLsizeiptr _size){ setData( _size, NULL, GL_STREAM_DRAW ); }

Robert Menzel's avatar
Robert Menzel committed
89
    //! uniform setters for scalar types: can only work if the offset-uniformname mapping was set before via setUniformOffsets()
Robert Menzel's avatar
Robert Menzel committed
90 91 92 93 94 95
    inline void setUniform (const std::string &_nameInShader, GLfloat   _v) { setUniformScalar<GLfloat>  (_nameInShader, _v); }
    inline void setUniform (const std::string &_nameInShader, GLint     _v) { setUniformScalar<GLint>    (_nameInShader, _v); }
    inline void setUniform (const std::string &_nameInShader, GLuint    _v) { setUniformScalar<GLuint>   (_nameInShader, _v); }
    inline void setUniform (const std::string &_nameInShader, GLboolean _v) { setUniformScalar<GLboolean>(_nameInShader, _v); }
    inline void setUniform (const std::string &_nameInShader, GLdouble  _v) { setUniformScalar<GLdouble> (_nameInShader, _v); }

96 97 98 99 100 101 102 103
    //! asuming std140 layout, add padding:
    void setUniform(const std::string &_nameInShader, glm::mat2x2 _v) { setUniform(_nameInShader, glm::mat2x4(_v)); }
    void setUniform(const std::string &_nameInShader, glm::mat3x2 _v) { setUniform(_nameInShader, glm::mat3x4(_v)); }
    void setUniform(const std::string &_nameInShader, glm::mat4x2 _v) { setUniform(_nameInShader, glm::mat4x4(_v)); }
    void setUniform(const std::string &_nameInShader, glm::mat2x3 _v) { setUniform(_nameInShader, glm::mat2x4(_v)); }
    void setUniform(const std::string &_nameInShader, glm::mat3x3 _v) { setUniform(_nameInShader, glm::mat3x4(_v)); }
    void setUniform(const std::string &_nameInShader, glm::mat4x3 _v) { setUniform(_nameInShader, glm::mat4x4(_v)); }

Robert Menzel's avatar
Robert Menzel committed
104
    //! uniform setters for glm types: can only work if the offset-uniformname mapping was set before via setUniformOffsets()
Robert Menzel's avatar
Robert Menzel committed
105 106 107
    template <typename T>
    void setUniform (const std::string &_nameInShader, T _v) {
        GLint offset = getUniformOffset( _nameInShader );
108 109 110 111 112
        if (offset == -1) {
            // hack for MacOS bug:
            offset = getUniformOffset( mBlockName+"."+_nameInShader  );
            //ACGL::Utils::debug() << "testing " + mBlockName+"."+_nameInShader << std::endl;
        }
Robert Menzel's avatar
Robert Menzel committed
113 114 115 116 117 118 119 120 121 122 123
        if (offset == -1) {
            ACGL::Utils::error() << "UniformBuffer does not know uniform " << _nameInShader << std::endl;
            return;
        }
        setSubData( offset, sizeof(T), glm::value_ptr(_v) );
    }

    // =================================================================================================== \/
    // ============================================================================================ FIELDS \/
    // =================================================================================================== \/
private:
Robert Menzel's avatar
Robert Menzel committed
124
    // template for scalar types: private as the setUniform() functions above map to this
Robert Menzel's avatar
Robert Menzel committed
125 126 127
    template <typename T>
    void setUniformScalar (const std::string &_nameInShader, T _v) {
        GLint offset = getUniformOffset( _nameInShader );
128 129 130 131 132
        if (offset == -1) {
            // hack for MacOS bug:
            offset = getUniformOffset( mBlockName+"."+_nameInShader  );
            //ACGL::Utils::debug() << "testing " + mBlockName+"."+_nameInShader << std::endl;
        }
Robert Menzel's avatar
Robert Menzel committed
133 134 135 136 137 138 139 140
        if (offset == -1) {
            ACGL::Utils::error() << "UniformBuffer does not know uniform " << _nameInShader << std::endl;
            return;
        }
        setSubData( offset, sizeof(T), &_v );
    }

    SharedLocationMappings uniformNameToOffsetMap;
141 142 143

public: // for mac bug
    std::string mBlockName;
Robert Menzel's avatar
Robert Menzel committed
144 145 146 147 148 149 150
};

ACGL_SMARTPOINTER_TYPEDEFS(UniformBuffer)

} // OpenGL
} // ACGL

Robert Menzel's avatar
Robert Menzel committed
151 152
#endif // OpenGL >= 3.1

Robert Menzel's avatar
Robert Menzel committed
153
#endif // ACGL_OPENGL_OBJECTS_UNIFORM_BUFFER_HH