1 #ifndef __GPU__SHADER_H__
2 #define __GPU__SHADER_H__
3 
4 
5 
6 
7 #include "commonDefs.h"
8 #include "InstantiatedObject.h"
9 #include <string>
10 #include <map>
11 
12 
13 /*
14  *
15  *  CLASS DECLARATION.
16  *
17  */
18 
19 namespace GPU
20 {
21     class Shader
22     {
23         /******************\
24         | Internal type(s) |
25         \******************/
26     public:
27         struct Uniform
28         {
29             GLint       location;
30             GLint       size;
31             GLenum      type;
32         };
33 
34         class PgObject : public InstantiatedObject
35         {
36             /********************\
37             | Member variable(s) |
38             \********************/
39         private:
40             GLhandleARB     m_Id;
41             GLenum          m_PgType;
42 
43             /*****************************\
44             | Constructor(s) / destructor |
45             \*****************************/
46         public:
PgObject()47             inline  PgObject() : InstantiatedObject(), m_Id(0)  {}
~PgObject()48             virtual ~PgObject()                                 { Release(); }
49 
50             /********************\
51             | Member function(s) |
52             \********************/
53         protected:
54             bool        Allocate();
55             bool        Unallocate();
56 
57         public:
58             bool        CompileSrcFile( const std::string &filename,
59                                         const GLenum pgType,
60                                         std::string *logs = NULL );
61             bool        CompileSrcString( const char *sourceString,
62                                           const GLenum pgType,
63                                           std::string *logs = NULL );
Id()64             GLhandleARB Id() const                                      { return m_Id; }
Type()65             GLenum      Type() const                                    { return m_PgType; }
66         };
67 
68 		class VertPg : public PgObject
69         {
70         public:
71 			inline bool CompileSrcFile( const std::string &filename,
72                                         std::string *logs = NULL )      { return PgObject::CompileSrcFile( filename, GL_VERTEX_SHADER_ARB, logs ); }
73             inline bool CompileSrcString( const char *sourceString,
74                                           std::string *logs = NULL )    { return PgObject::CompileSrcString( sourceString, GL_VERTEX_SHADER_ARB, logs ); }
75         };
76 
77 		class FragPg : public PgObject
78         {
79         public:
80 			inline bool CompileSrcFile( const std::string &filename,
81                                         std::string *logs = NULL )      { return PgObject::CompileSrcFile( filename, GL_FRAGMENT_SHADER_ARB, logs ); }
82             inline bool CompileSrcString( const char *sourceString,
83                                           std::string *logs = NULL )    { return PgObject::CompileSrcString( sourceString, GL_FRAGMENT_SHADER_ARB, logs ); }
84         };
85 
86 		class GeomPg : public PgObject
87         {
88         public:
89 			inline bool CompileSrcFile( const std::string &filename,
90                                         std::string *logs = NULL )      { return PgObject::CompileSrcFile( filename, GL_GEOMETRY_SHADER_EXT, logs ); }
91             inline bool CompileSrcString( const char *sourceString,
92                                           std::string *logs = NULL )    { return PgObject::CompileSrcString( sourceString, GL_GEOMETRY_SHADER_EXT, logs ); }
SetInputPrimitives(int value)93             inline void SetInputPrimitives( int value )                 { if( IsInstantiated() ) glProgramParameteriARB( Id(), GL_GEOMETRY_INPUT_TYPE_EXT , value ); }
SetOutputPrimitives(int value)94             inline void SetOutputPrimitives( int value )                { if( IsInstantiated() ) glProgramParameteriARB( Id(), GL_GEOMETRY_OUTPUT_TYPE_EXT, value ); }
95         };
96 
97     private:
98         typedef std::map<std::string,Uniform> UniformMap;
99 	    typedef std::map<std::string,GLint>   SamplerMap;
100 	    typedef std::map<std::string,GLint>   AttribMap;
101 
102         /********************\
103         | Member variable(s) |
104         \********************/
105     private:
106         GLhandleARB m_Id;
107 
108         VertPg      m_VertPg;
109         FragPg      m_FragPg;
110         GeomPg      m_GeomPg;
111 
112         UniformMap  m_Uniforms;
113         SamplerMap  m_Samplers;
114         AttribMap   m_Attributes;
115 
116         /*****************************\
117         | Constructor(s) / destructor |
118         \*****************************/
119     public:
Shader()120         inline  Shader() : m_Id(0)                          {}
~Shader()121         inline  ~Shader()                                   { Release(); }
122 
123         /*******************\
124         | Class function(s) |
125         \*******************/
126     public:
127         static std::string      GetLogs( GLuint pgId );
128 
129         /********************\
130         | Member function(s) |
131         \********************/
132     protected:
133         void					RecoverActiveUniforms();
134         void					RecoverActiveAttributes();
135 
136     public:
137         bool                    Link( std::string *logs = NULL );
138 
139         Shader&                 Attach( const PgObject &pg );
140         inline bool             AttachAndLink( const PgObject &pg,
141                                                std::string *logs = NULL )           { Attach(pg); return Link(logs); }
142         Shader&                 DetachVertPg();
143         Shader&                 DetachFragPg();
144         Shader&                 DetachGeomPg();
145 
146         void					Release();
IsCreated()147         inline bool				IsCreated() const                                   { return m_Id != 0; }
Id()148         inline GLhandleARB		Id() const                                          { return m_Id; }
149 
VertProgram()150         inline const VertPg&    VertProgram() const                                 { return m_VertPg; }
FragProgram()151         inline const FragPg&    FragProgram() const                                 { return m_FragPg; }
GeomProgram()152         inline const GeomPg&    GeomProgram() const                                 { return m_GeomPg; }
153 
Bind()154         inline void				Bind() const                                        { glUseProgramObjectARB( m_Id ); }
Unbind()155         inline void				Unbind() const                                      { glUseProgramObjectARB( 0 ); }
156 
157         void					SetUniform( const std::string& name,
158 								            const void *value );
159         void					SetSampler( const std::string& name,
160 								            const GLint texUnit );
161 
UniformLoc(const std::string & name)162         inline const Uniform*   UniformLoc( const std::string& name ) const
163         {
164             UniformMap::const_iterator u = m_Uniforms.find( name );
165             return (u!=m_Uniforms.end())? &u->second : NULL;
166         }
SamplerLoc(const std::string & name)167         inline GLint            SamplerLoc( const std::string& name ) const
168         {
169             SamplerMap::const_iterator s = m_Samplers.find( name );
170             return (s!=m_Samplers.end())? s->second : -1;
171         }
AttribLoc(const std::string & name)172         inline GLint            AttribLoc( const std::string& name ) const
173         {
174             AttribMap::const_iterator a = m_Attributes.find( name );
175             return (a!=m_Attributes.end())? a->second : -1;
176         }
177     };
178 };
179 
180 
181 
182 
183 #endif //__GPU__SHADER_H__
184