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/GLShaderObjectIO.h"
00013 #include "GL/TPProgramName.h"
00014
00015
00016 namespace gk {
00017
00018
00019 class GLShaderProgram : public GLResource
00020 {
00021 struct parameter
00022 {
00023 std::string name;
00024 int location;
00025 int index;
00026 bool is_integer;
00027
00028 parameter( )
00029 :
00030 name(),
00031 location(-1),
00032 index(-1),
00033 is_integer(false)
00034 {}
00035
00036 parameter( const char *_name, const int _location, const int _index, const bool _is_integer= false )
00037 :
00038 name(_name),
00039 location(_location),
00040 index(_index),
00041 is_integer(_is_integer)
00042 {}
00043
00044 ~parameter( ) {}
00045 };
00046
00047 private:
00048
00049 GLShaderProgram( const GLShaderProgram& );
00050 GLShaderProgram& operator=( const GLShaderProgram& );
00051
00052 protected:
00053 std::vector<GLShaderObject *> m_shaders;
00054 std::vector<parameter> m_feedbacks;
00055 std::vector<parameter> m_attributes;
00056 std::vector<parameter> m_samplers;
00057
00058 int m_attribute_count;
00059 int m_uniform_count;
00060 int m_sampler_count;
00061 int m_feedback_count;
00062
00063 bool m_is_linked;
00064 bool m_is_validated;
00065
00066 int make( );
00067
00068 public:
00069 static bool is_sampler( const GLenum type );
00070 static bool is_integer( const GLenum type );
00071
00072
00073 GLShaderProgram( )
00074 :
00075 GLResource(),
00076 m_shaders(),
00077 m_feedbacks(),
00078 m_is_linked(false),
00079 m_is_validated(false)
00080 {
00081 m_name= glCreateProgram();
00082 }
00083
00084
00085 virtual ~GLShaderProgram( ) {}
00086
00087
00088 int attachShader( GLShaderObject *shader );
00089
00090
00091
00092 int link( );
00093
00094 int validate( );
00095
00096
00097 int createGLResource( );
00098
00099
00100 int releaseGLResource( );
00101
00102
00103 int uniformCount( ) const
00104 {
00105 return m_uniform_count;
00106 }
00107
00108
00109 ProgramUniform uniform( const char *name ) const;
00110
00111
00112 int samplerCount( ) const
00113 {
00114 return m_sampler_count;
00115 }
00116
00117
00118 ProgramSampler sampler( const char *name ) const;
00119
00120
00121 int attributeCount( ) const
00122 {
00123 return m_attribute_count;
00124 }
00125
00126
00127 ProgramAttribute attribute( const char *name ) const;
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 ProgramDrawbuffer drawbuffer( const char *name ) const ;
00138
00139
00140 int feedbackCount( ) const
00141 {
00142 return m_feedback_count;
00143 }
00144
00145
00146 ProgramFeedback feedback( const char *name ) const;
00147 };
00148
00149
00150
00151
00152 class GL2ShaderProgram : public GLShaderProgram
00153 {
00154 public:
00155 GL2ShaderProgram( )
00156 :
00157 GLShaderProgram()
00158 {}
00159
00160 GL2ShaderProgram(
00161 const TextFile *vertex,
00162 const TextFile *fragment )
00163 :
00164 GLShaderProgram()
00165 {
00166 GLVertexShader *vertex_shader= GLManager<GLVertexShader>::manager().insert(
00167 new GLVertexShader(vertex) );
00168 GLFragmentShader *fragment_shader= GLManager<GLFragmentShader>::manager().insert(
00169 new GLFragmentShader(fragment) );
00170
00171 attachShader(vertex_shader);
00172 attachShader(fragment_shader);
00173 }
00174
00175 GL2ShaderProgram(
00176 const std::string& vertex_filename,
00177 const std::string& fragment_filename )
00178 :
00179 GLShaderProgram()
00180 {
00181 attachShader( GLVertexShaderIO::read(vertex_filename, "vertex") );
00182 attachShader( GLFragmentShaderIO::read(fragment_filename, "fragment") );
00183 }
00184
00185 ~GL2ShaderProgram( ) {}
00186 };
00187
00188
00189 #if defined GK_OPENGL3 || defined GK_OPENGL4
00190
00191
00192
00193
00194
00195
00196 class GL3ShaderProgram : public GLShaderProgram
00197 {
00198 public:
00199 GL3ShaderProgram( )
00200 :
00201 GLShaderProgram()
00202 {}
00203
00204 GL3ShaderProgram(
00205 const TextFile *vertex,
00206 const TextFile *geometry,
00207 const TextFile *fragment )
00208 :
00209 GLShaderProgram()
00210 {
00211 GLVertexShader *vertex_shader= GLManager<GLVertexShader>::manager().insert(
00212 new GLVertexShader(vertex) );
00213 GLGeometryShader *geometry_shader= GLManager<GLGeometryShader>::manager().insert(
00214 new GLGeometryShader(geometry) );
00215 GLFragmentShader *fragment_shader= GLManager<GLFragmentShader>::manager().insert(
00216 new GLFragmentShader(fragment) );
00217
00218 attachShader(vertex_shader);
00219 attachShader(geometry_shader);
00220 attachShader(fragment_shader);
00221 }
00222
00223 GL3ShaderProgram(
00224 const std::string& vertex_filename,
00225 const std::string& geometry_filename,
00226 const std::string& fragment_filename )
00227 :
00228 GLShaderProgram()
00229 {
00230 attachShader( GLVertexShaderIO::read(vertex_filename, "vertex") );
00231 attachShader( GLGeometryShaderIO::read(geometry_filename, "geometry") );
00232 attachShader( GLFragmentShaderIO::read(fragment_filename, "fragment") );
00233 }
00234
00235 ~GL3ShaderProgram( ) {}
00236 };
00237 #endif
00238
00239 #ifdef GK_OPENGL4
00240
00241
00242
00243
00244
00245
00246
00247
00248 class GL4ShaderProgram : public GLShaderProgram
00249 {
00250 public:
00251 GL4ShaderProgram( )
00252 :
00253 GLShaderProgram()
00254 {}
00255
00256 GL4ShaderProgram(
00257 const TextFile *vertex,
00258 const TextFile *control,
00259 const TextFile *evaluation,
00260 const TextFile *geometry,
00261 const TextFile *fragment )
00262 :
00263 GLShaderProgram()
00264 {
00265 GLVertexShader *vertex_shader= GLManager<GLVertexShader>::manager().insert(
00266 new GLVertexShader(vertex) );
00267 GLControlShader *control_shader= GLManager<GLControlShader >::manager().insert(
00268 new GLControlShader (control) );
00269 GLEvaluationShader *evaluation_shader= GLManager<GLEvaluationShader>::manager().insert(
00270 new GLEvaluationShader(evaluation) );
00271 GLGeometryShader *geometry_shader= GLManager<GLGeometryShader>::manager().insert(
00272 new GLGeometryShader(geometry) );
00273 GLFragmentShader *fragment_shader= GLManager<GLFragmentShader>::manager().insert(
00274 new GLFragmentShader(fragment) );
00275
00276 attachShader(vertex_shader);
00277 attachShader(control_shader);
00278 attachShader(evaluation_shader);
00279 attachShader(geometry_shader);
00280 attachShader(fragment_shader);
00281 }
00282
00283 GL4ShaderProgram(
00284 const std::string& vertex_filename,
00285 const std::string& control_filename,
00286 const std::string& evaluation_filename,
00287 const std::string& geometry_filename,
00288 const std::string& fragment_filename )
00289 :
00290 GLShaderProgram()
00291 {
00292 attachShader( GLVertexShaderIO::read(vertex_filename, "vertex") );
00293 attachShader( GLControlShaderIO::read(control_filename, "control") );
00294 attachShader( GLEvaluationShaderIO::read(evaluation_filename, "evaluation") );
00295 attachShader( GLGeometryShaderIO::read(geometry_filename, "geometry") );
00296 attachShader( GLFragmentShaderIO::read(fragment_filename, "fragment") );
00297 }
00298
00299 ~GL4ShaderProgram( ) {}
00300 };
00301 #endif
00302
00303
00304 }
00305
00306 #endif