Shader.h 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: F. Larue.
 *
 *	See licence.txt for additional information.
 */


#ifndef __GPU__SHADER_H__
#define __GPU__SHADER_H__




#include "GPUdefs.h"
#include "InstantiatedObject.h"
#include "Texture2D.h"
#include <string>
#include <map>


/*
 *
 *  CLASS DECLARATION.
 *
 */

namespace GPU
{
    class VBO;
    class VBOAttribBuffer;


34
    class GenericShader
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    {
        /******************\
        | Internal type(s) |
        \******************/
    public:
        class PgObject : public InstantiatedObject
        {
            /********************\
            | Member variable(s) |
            \********************/
        private:
            GLhandleARB     m_Id;
            GLenum          m_PgType;

            /*****************************\
            | Constructor(s) / destructor |
            \*****************************/
        public:
            inline  PgObject() : InstantiatedObject(), m_Id(0)  {}
            virtual ~PgObject()                                 { Release(); }

            /********************\
            | Member function(s) |
            \********************/
        protected:
            bool        Allocate();
            bool        Unallocate();

        public:
            bool        CompileSrcFile( const std::string &filename,
                                        const GLenum pgType,
                                        std::string *logs = NULL );
            bool        CompileSrcString( const char *sourceString,
                                          const GLenum pgType,
                                          std::string *logs = NULL );
            GLhandleARB Id() const                                      { return m_Id; }
            GLenum      Type() const                                    { return m_PgType; }
        };

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
        /*******************\
        | Class function(s) |
        \*******************/
    public:
        static std::string      GetLogs( GLuint pgId );

        /*****************************\
        | Constructor(s) / destructor |
        \*****************************/
    public:
        inline              GenericShader() : m_Id(0)   {}
        virtual             ~GenericShader()            { Unallocate(); }

        /********************\
        | Member variable(s) |
        \********************/
    private:
        GLhandleARB         m_Id;

        /********************\
        | Member function(s) |
        \********************/
    public:
        inline bool			IsCreated() const           { return m_Id != 0; }
        inline GLhandleARB	Id() const                  { return m_Id; }

    protected:
        bool                Allocate();
        bool                Unallocate();
    };

    class Shader : public GenericShader
    {
        /******************\
        | Internal type(s) |
        \******************/
    public:
111 112 113 114 115 116 117 118 119 120 121 122 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
		class VertPg : public PgObject
        {
        public:
			inline bool CompileSrcFile( const std::string &filename,
                                        std::string *logs = NULL )      { return PgObject::CompileSrcFile( filename, GL_VERTEX_SHADER_ARB, logs ); }
            inline bool CompileSrcString( const char *sourceString,
                                          std::string *logs = NULL )    { return PgObject::CompileSrcString( sourceString, GL_VERTEX_SHADER_ARB, logs ); }
        };

		class FragPg : public PgObject
        {
        public:
			inline bool CompileSrcFile( const std::string &filename,
                                        std::string *logs = NULL )      { return PgObject::CompileSrcFile( filename, GL_FRAGMENT_SHADER_ARB, logs ); }
            inline bool CompileSrcString( const char *sourceString,
                                          std::string *logs = NULL )    { return PgObject::CompileSrcString( sourceString, GL_FRAGMENT_SHADER_ARB, logs ); }
        };

		class GeomPg : public PgObject
        {
        public:
			inline bool CompileSrcFile( const std::string &filename,
                                        std::string *logs = NULL )      { return PgObject::CompileSrcFile( filename, GL_GEOMETRY_SHADER_EXT, logs ); }
            inline bool CompileSrcString( const char *sourceString,
                                          std::string *logs = NULL )    { return PgObject::CompileSrcString( sourceString, GL_GEOMETRY_SHADER_EXT, logs ); }
        };

        static VertPg VertPgFromFile( const std::string &filename,
                                      std::string *logs = NULL )        { VertPg pg; return (pg.CompileSrcFile(filename,logs)? pg : VertPg()); }
        static VertPg VertPgFromString( const char *sourceString,
                                        std::string *logs = NULL )      { VertPg pg; return (pg.CompileSrcString(sourceString,logs)? pg : VertPg()); }

        static FragPg FragPgFromFile( const std::string &filename,
                                      std::string *logs = NULL )        { FragPg pg; return (pg.CompileSrcFile(filename,logs)? pg : FragPg()); }
        static FragPg FragPgFromString( const char *sourceString,
                                        std::string *logs = NULL )      { FragPg pg; return (pg.CompileSrcString(sourceString,logs)? pg : FragPg()); }

        static GeomPg GeomPgFromFile( const std::string &filename,
                                      std::string *logs = NULL )        { GeomPg pg; return (pg.CompileSrcFile(filename,logs)? pg : GeomPg()); }
        static GeomPg GeomPgFromString( const char *sourceString,
                                        std::string *logs = NULL )      { GeomPg pg; return (pg.CompileSrcString(sourceString,logs)? pg : GeomPg()); }

153 154 155 156 157 158 159
        struct Uniform
        {
            GLint       location;
            GLint       size;
            GLenum      type;
        };

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    private:
        typedef std::map<std::string,Uniform>       UniformMap;
	    typedef std::map<std::string,GLint>         AttribMap;
        typedef std::map<GLint,Texture*>            SamplerBindingMap;
        typedef std::map<GLint,VBOAttribBuffer*>    AttribBindingMap;

        /********************\
        | Member variable(s) |
        \********************/
    private:
        VertPg              m_VertPg;
        FragPg              m_FragPg;
        GeomPg              m_GeomPg;

        UniformMap          m_Uniforms;
175
        UniformMap          m_Samplers;
176 177 178 179 180 181 182
        AttribMap           m_Attributes;
        SamplerBindingMap   m_SamplerBinding;

        /*****************************\
        | Constructor(s) / destructor |
        \*****************************/
    public:
183 184
        inline  Shader() : GenericShader()  {}
        inline  ~Shader()                   { Release(); }
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

        /********************\
        | Member function(s) |
        \********************/
    protected:
        void					RecoverActiveUniforms();
        void					RecoverActiveAttributes();

    public:
        bool                    Link( std::string *logs = NULL );

        Shader&                 Attach( const PgObject &pg );
        inline bool             AttachAndLink( const PgObject &pg,
                                               std::string *logs = NULL )           { Attach(pg); return Link(logs); }
        Shader&                 DetachVertPg();
        Shader&                 DetachFragPg();
        Shader&                 DetachGeomPg();

        void					Release();

        inline const VertPg&    VertProgram() const                                 { return m_VertPg; }
        inline const FragPg&    FragProgram() const                                 { return m_FragPg; }
        inline const GeomPg&    GeomProgram() const                                 { return m_GeomPg; }
        inline GeomPg&          GeomProgram()                                       { return m_GeomPg; }

210
        inline void				Bind()                                              { gpuAssert( glUseProgramObjectARB( Id() ) ); BindSamplers(); }
211
        inline void				Unbind()                                            { UnbindSamplers(); gpuAssert( glUseProgramObjectARB( 0 ) ); }
212 213 214

        void					SetUniform( const std::string& name,
								            const void *value );
215 216 217 218
        void					SetUniform( const std::string& name, GLint    value );
        void					SetUniform( const std::string& name, GLfloat  value );
        void					SetUniform( const std::string& name, GLdouble value );

219
        inline void				SetSampler( const std::string& name,
220 221 222 223 224
								            const GLint texUnit )                   { SetSamplers( name, &texUnit ); }
        void					SetSamplers( const std::string& name,
                                             const GLint* texUnit );
        void					SetSamplers( const std::string& name,
                                             const GLint firstTexUnit );
225 226 227 228 229 230 231 232 233 234 235 236 237 238
        void					SetAttrib( const std::string& name,
                                           VBOAttribBuffer *attrib );

        void                    UniformNames( std::vector<std::string> &names ) const;
        void                    SamplerNames( std::vector<std::string> &names ) const;
        void                    AttribNames( std::vector<std::string> &names ) const;

        inline const Uniform*   UniformLoc( const std::string& name ) const
        {
            UniformMap::const_iterator u = m_Uniforms.find( name );
            return (u!=m_Uniforms.end())? &u->second : NULL;
        }
        inline GLint            SamplerLoc( const std::string& name ) const
        {
239 240
            UniformMap::const_iterator s = m_Samplers.find( name );
            return (s!=m_Samplers.end())? s->second.location : -1;
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 266 267 268 269 270 271 272 273 274 275
        }
        inline GLint            AttribLoc( const std::string& name ) const
        {
            AttribMap::const_iterator a = m_Attributes.find( name );
            return (a!=m_Attributes.end())? a->second : -1;
        }

        inline void             SetSamplerBinding( const std::string& name, Texture &sampler )      { SetSamplerBinding( name, &sampler ); }
        inline void             SetSamplerBinding( const std::string& name, Texture *sampler )
        {
            GLint loc = SamplerLoc( name );
            if( loc >= 0 )
                m_SamplerBinding[loc] = sampler;
        }

        inline void             RemoveSamplerBinding( const std::string& name )
        {
            auto sb = m_SamplerBinding.find( SamplerLoc(name) );
            if( sb != m_SamplerBinding.end() )
                m_SamplerBinding.erase( sb );
        }

        inline void             RemoveAllSamplerBindings()
        {
            m_SamplerBinding.clear();
        }

    private:
        inline void             BindSamplers()
        {
            GLint texUnit = 0;

            for( auto sb=m_SamplerBinding.begin(); sb!=m_SamplerBinding.end(); ++sb )
            {
                sb->second->Bind( texUnit );
276
                gpuAssert( glUniform1iARB( sb->first, texUnit++ ) );
277 278 279 280 281 282 283 284 285 286 287
            }
        }

        inline void             UnbindSamplers()
        {
            for( auto sb=m_SamplerBinding.begin(); sb!=m_SamplerBinding.end(); ++sb )
                sb->second->Unbind();
        }
    };


Frédéric Larue's avatar
Frédéric Larue committed
288 289 290 291 292 293 294 295 296 297
    bool        CreateShaderFromSources( GPU::Shader &shader,
                                         const char *vpgSrc,
                                         const char *gpgSrc,
                                         const char *fpgSrc,
                                         std::string *logs = NULL );

    inline bool CreateShaderFromSources( GPU::Shader &shader,
                                         const char *vpgSrc,
                                         const char *fpgSrc,
                                         std::string *logs = NULL ) { return CreateShaderFromSources( shader, vpgSrc, NULL, fpgSrc, logs ); }
298 299 300 301

    inline bool CreateShaderFromSources( GPU::Shader &shader,
                                         const std::string &vpgSrc,
                                         const std::string &fpgSrc,
Frédéric Larue's avatar
Frédéric Larue committed
302 303
                                         std::string *logs = NULL ) { return CreateShaderFromSources( shader, vpgSrc.c_str(), fpgSrc.c_str(), logs ); }

304 305 306 307
    inline bool CreateShaderFromSources( GPU::Shader &shader,
                                         const std::string &vpgSrc,
                                         const std::string &gpgSrc,
                                         const std::string &fpgSrc,
Frédéric Larue's avatar
Frédéric Larue committed
308
                                         std::string *logs = NULL ) { return CreateShaderFromSources( shader, vpgSrc.c_str(), gpgSrc.c_str(), fpgSrc.c_str(), logs ); }
309 310 311 312 313 314
};




#endif //__GPU__SHADER_H__