gKitGL
TPShaderProgram.h
00001 
00002 #ifndef _TP_SHADER_PROGRAM_H
00003 #define _TP_SHADER_PROGRAM_H
00004 
00005 #include <string>
00006 #include <vector>
00007 
00008 #include "GLManager.h"
00009 #include "GL/GLPlatform.h"
00010 #include "GLResource.h"
00011 #include "GL/GLShaderObject.h"
00012 #include "GL/TPProgramName.h"
00013 
00014 
00015 namespace gk {
00016 
00017 //! representation d'un shader program openGL.
00018 class GLShaderProgram : public GLResource
00019 {
00020     struct parameter
00021     {
00022         std::string name;
00023         int location;
00024         int index;
00025         int size;
00026         GLenum type;
00027         bool is_integer;
00028         
00029         parameter( )
00030             :
00031             name(),
00032             location(-1),
00033             index(-1),
00034             size(0),
00035             type(0),
00036             is_integer(false)
00037         {}
00038         
00039         parameter( const char *_name, const int _location, const int _index, const int _size, const GLenum _type, const bool _is_integer= false )
00040             :
00041             name(_name),
00042             location(_location),
00043             index(_index),
00044             size(_size),
00045             type(_type),
00046             is_integer(_is_integer)
00047         {}
00048         
00049         ~parameter( ) {}
00050     };
00051     
00052 private:
00053     // non copyable
00054     GLShaderProgram( const GLShaderProgram& );
00055     GLShaderProgram& operator=( const GLShaderProgram& );
00056     
00057 protected:
00058     GLShaderObject *m_shaders[GLShaderObject::SHADERTYPE_LAST];
00059     
00060     std::vector<parameter> m_feedbacks;
00061     std::vector<parameter> m_attributes;
00062     std::vector<parameter> m_uniforms;
00063     std::vector<parameter> m_samplers;
00064 
00065     int m_attribute_count;
00066     int m_uniform_count;
00067     int m_sampler_count;
00068     int m_feedback_count;
00069 
00070     bool m_is_linked;
00071     bool m_is_validated;
00072 
00073     int make( );
00074 
00075 public:
00076     static bool is_sampler( const GLenum type );
00077     static bool is_integer( const GLenum type );
00078     
00079     //! constructeur par defaut.
00080     GLShaderProgram( );
00081 
00082     //! desctructeur.
00083     virtual ~GLShaderProgram( ) {}
00084 
00085     //! ajoute un shader object au shader program.
00086     int attachShader( GLShaderObject *shader );
00087 
00088     //! renvoie un shader attache au shader program, cf gk::GLShaderObject::VERTEX, etc.
00089     GLShaderObject *shader( const unsigned int type );
00090     
00091     //! (re-)linke le shader program.
00092     int link( );
00093     //! valide la configuration du shader program.
00094     int validate( );
00095 
00096     //! construit le shader program. creation de l'objet openGL.
00097     int createGLResource( );
00098 
00099     //! detruit l'objet openGL.
00100     int releaseGLResource( );
00101 
00102     //! reinitialise l'etat du shader program.
00103     int clear( );
00104     
00105     //! renvoie le nombre d'uniforms.
00106     int uniformCount( ) const
00107     {
00108         return m_uniform_count;
00109     }
00110     
00111     const char *uniformName( const ProgramUniform& uniform ) const
00112     {
00113         return m_uniforms[uniform.index()].name.c_str();
00114     }
00115     
00116     //! renvoie l'identifiant d'un uniform du shader program.
00117     ProgramUniform uniform( const char *name ) const;
00118 
00119     //! renvoie le nombre de samplers.
00120     int samplerCount( ) const
00121     {
00122         return m_sampler_count;
00123     }
00124 
00125     const char *samplerName( const ProgramSampler& sampler ) const
00126     {
00127         return m_samplers[sampler.index()].name.c_str();
00128     }
00129     
00130     //! renvoie l'identifiant d'un sampler du shader program.
00131     ProgramSampler sampler( const char *name ) const;
00132     
00133     //! renvoie l'identifiant d'un uniform block du shader program.
00134     ProgramInterface interface( const char *name ) const;
00135     
00136     //! renvoie le nombre d'attributs.
00137     int attributeCount( ) const
00138     {
00139         return m_attribute_count;
00140     }
00141     
00142     //! renvoie l'identifiant d'un attribut du shader program.
00143     ProgramAttribute attribute( const char *name ) const;
00144 
00145     const char *attributeName( const ProgramAttribute& attribute ) const
00146     {
00147         return m_attributes[attribute.index()].name.c_str();
00148     }
00149     
00150     //~ //! renvoie le nombre de drawbuffer (varying out du fragment shader).
00151     //~ int drawbufferCount( ) const
00152     //~ {
00153         //~ //! \todo
00154         //~ return -1;
00155     //~ }
00156     
00157     //! renvoie le draw buffer d'un varying du fragment shader.
00158     ProgramDrawbuffer drawbuffer( const char *name ) const ;
00159     
00160     //! renvoie le nombre de feedbacks
00161     int feedbackCount( ) const
00162     {
00163         return m_feedback_count;
00164     }
00165     
00166     //! renvoie l'identifiant du buffer d'un varying utilise par le transform feedback.
00167     ProgramFeedback feedback( const char *name ) const;
00168 };
00169 
00170 }
00171 
00172 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerator Friends