UniformBuffer.hh 6.22 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1 2 3 4 5 6 7 8
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2012, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

#ifndef ACGL_OPENGL_OBJECTS_UNIFORM_BUFFER_HH
#define ACGL_OPENGL_OBJECTS_UNIFORM_BUFFER_HH

Robert Menzel's avatar
Robert Menzel committed
9
/**
Robert Menzel's avatar
Robert Menzel committed
10 11 12 13 14
 * 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
15 16
     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
17 18 19 20 21 22 23 24 25 26
 *
 * 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
27 28
 * In contrast to ShaderPrograms, nothing has to be activated before setUniform can be called
 * here.
Robert Menzel's avatar
Robert Menzel committed
29 30
 */

Robert Menzel's avatar
Robert Menzel committed
31 32 33 34 35 36 37 38 39 40
#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>

#include <ACGL/OpenGL/Objects/LocationMappings.hh>
#include <ACGL/Math/Math.hh>

Robert Menzel's avatar
Robert Menzel committed
41
#if (ACGL_OPENGL_VERSION >= 31)
Robert Menzel's avatar
Robert Menzel committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
namespace ACGL{
namespace OpenGL{

class UniformBuffer : public Buffer
{
    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
public:
    UniformBuffer()
        : Buffer(GL_UNIFORM_BUFFER)
    {}

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

    // ==================================================================================================== \/
    // ============================================================================================ GETTERS \/
    // ==================================================================================================== \/
public:
Robert Menzel's avatar
Robert Menzel committed
63 64 65 66 67 68 69
    /** 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
70 71 72 73 74 75 76 77 78 79 80 81 82
    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
83
    //! 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
84 85 86 87 88 89
    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); }

Robert Menzel's avatar
Robert Menzel committed
90
    //! 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
91 92 93 94 95 96 97 98 99 100 101 102 103 104
    template <typename T>
    void setUniform (const std::string &_nameInShader, T _v) {
        GLint offset = getUniformOffset( _nameInShader );
        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
105
    // template for scalar types: private as the setUniform() functions above map to this
Robert Menzel's avatar
Robert Menzel committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    template <typename T>
    void setUniformScalar (const std::string &_nameInShader, T _v) {
        GLint offset = getUniformOffset( _nameInShader );
        if (offset == -1) {
            ACGL::Utils::error() << "UniformBuffer does not know uniform " << _nameInShader << std::endl;
            return;
        }
        setSubData( offset, sizeof(T), &_v );
    }

    SharedLocationMappings uniformNameToOffsetMap;
};

ACGL_SMARTPOINTER_TYPEDEFS(UniformBuffer)

} // OpenGL
} // ACGL

Robert Menzel's avatar
Robert Menzel committed
124 125
#endif // OpenGL >= 3.1

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