fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / Base / OSGShaderProgram.h
blob6228e388b35b31953591e3a1bfd9a5eda5341a30
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #ifndef _OSGSHADERPROGRAM_H_
40 #define _OSGSHADERPROGRAM_H_
41 #ifdef __sgi
42 #pragma once
43 #endif
45 #include "OSGShaderProgramBase.h"
46 #include "OSGShaderProgramVariables.h"
47 #include "OSGWindow.h"
49 OSG_BEGIN_NAMESPACE
51 /*! \brief ShaderProgram class. See \ref
52 PageSystemShaderProgram for a description.
53 \ingroup GrpSystemShaderBase
54 \ingroup GrpLibOSGSystem
55 \includebasedoc
58 class OSG_SYSTEM_DLLMAPPING ShaderProgram : public ShaderProgramBase
60 protected:
62 /*========================== PUBLIC =================================*/
64 public:
66 typedef ShaderProgramBase Inherited;
67 typedef ShaderProgram Self;
69 typedef ShaderVariableFunctor::ProcVarFunctor ProcVarFunctor;
70 typedef ShaderVariableFunctor::ProcVarNodeFunctor ProcVarNodeFunctor;
72 static const UInt32 KeepProgActive = 0x0001;
74 static const UInt32 ProgActive = 0x0001;
76 struct ProgramIdPoolTag;
78 /*---------------------------------------------------------------------*/
79 /*! \name Sync */
80 /*! \{ */
82 static UInt32 getExtIdSHL (void);
83 static UInt32 getExtIdCgShader (void);
84 static UInt32 getExtIdGeometryShader4 (void);
85 static UInt32 getExtIdGPUShader4 (void);
86 static UInt32 getExtIdTransformFeedback2 (void);
87 static UInt32 getExtIdUniformBufferObject (void);
88 static UInt32 getExtIdProgramInterfaceQuery (void);
89 static UInt32 getExtIdShaderStorageBufferObject (void);
91 static UInt32 getFuncIdCreateShader (void);
92 static UInt32 getFuncIdDeleteShader (void);
93 static UInt32 getFuncIdShaderSource (void);
94 static UInt32 getFuncIdCompileShader (void);
95 static UInt32 getFuncIdAttachShader (void);
96 static UInt32 getFuncIdGetShaderiv (void);
97 static UInt32 getFuncIdGetShaderInfoLog (void);
98 static UInt32 getFuncIdGetActiveAttrib (void);
100 static UInt32 getFuncIdCreateProgram (void);
101 static UInt32 getFuncIdDeleteProgram (void);
102 static UInt32 getFuncIdLinkProgram (void);
103 static UInt32 getFuncIdGetProgramiv (void);
104 static UInt32 getFuncIdGetProgramInfoLog (void);
105 static UInt32 getFuncIdUseProgram (void);
107 static UInt32 getFuncIdGetUniformLocation (void);
109 static UInt32 getFuncIdUniform1i (void);
110 static UInt32 getFuncIdUniform2i (void);
111 static UInt32 getFuncIdUniform3i (void);
112 static UInt32 getFuncIdUniform4i (void);
114 static UInt32 getFuncIdUniform1ui (void);
115 static UInt32 getFuncIdUniform2ui (void);
116 static UInt32 getFuncIdUniform3ui (void);
117 static UInt32 getFuncIdUniform4ui (void);
119 static UInt32 getFuncIdUniform1f (void);
120 static UInt32 getFuncIdUniform2f (void);
121 static UInt32 getFuncIdUniform3f (void);
122 static UInt32 getFuncIdUniform4f (void);
124 static UInt32 getFuncIdUniform1iv (void);
125 static UInt32 getFuncIdUniform2iv (void);
126 static UInt32 getFuncIdUniform3iv (void);
127 static UInt32 getFuncIdUniform4iv (void);
129 static UInt32 getFuncIdUniform1uiv (void);
130 static UInt32 getFuncIdUniform2uiv (void);
131 static UInt32 getFuncIdUniform3uiv (void);
132 static UInt32 getFuncIdUniform4uiv (void);
134 static UInt32 getFuncIdUniform1fv (void);
135 static UInt32 getFuncIdUniform2fv (void);
136 static UInt32 getFuncIdUniform3fv (void);
137 static UInt32 getFuncIdUniform4fv (void);
139 static UInt32 getFuncIdUniformMatrix2fv (void);
140 static UInt32 getFuncIdUniformMatrix3fv (void);
141 static UInt32 getFuncIdUniformMatrix4fv (void);
143 static UInt32 getFuncIdGetUniformiv (void);
144 static UInt32 getFuncIdGetUniformfv (void);
146 static UInt32 getFuncIdProgramParameteri (void);
147 static UInt32 getFuncIdBindAttribLocation (void);
149 static UInt32 getFuncIdBindBufferBase (void);
150 static UInt32 getFuncIdTransformFeedbackVaryings(void);
152 static UInt32 getFuncIdBeginTransformFeedback (void);
153 static UInt32 getFuncIdEndTransformFeedback (void);
155 static UInt32 getFuncIdPauseTransformFeedback (void);
156 static UInt32 getFuncIdResumeTransformFeedback (void);
158 static UInt32 getFuncIdGetUniformBlockIndex (void);
159 static UInt32 getFuncIdUniformBlockBinding (void);
161 static UInt32 getFuncIdGetProgramResourceIndex (void);
162 static UInt32 getFuncIdGetProgramResourceiv (void);
163 static UInt32 getFuncIdShaderStorageBlockBinding(void);
165 /*! \} */
166 /*---------------------------------------------------------------------*/
167 /*! \name Sync */
168 /*! \{ */
170 virtual void changed(ConstFieldMaskArg whichField,
171 UInt32 origin,
172 BitVector details );
174 /*! \} */
175 /*---------------------------------------------------------------------*/
176 /*! \name Output */
177 /*! \{ */
179 void validate(DrawEnv *pEnv);
181 /*! \} */
182 /*---------------------------------------------------------------------*/
183 /*! \name Output */
184 /*! \{ */
186 static ShaderProgramTransitPtr createVertexShader (
187 bool bCreateDefAttribMap = false);
188 static ShaderProgramTransitPtr createGeometryShader(void );
189 static ShaderProgramTransitPtr createFragmentShader(void );
191 /*! \} */
192 /*---------------------------------------------------------------------*/
193 /*! \name Output */
194 /*! \{ */
196 bool readProgram(const Char8 *file);
198 /*! \} */
199 /*---------------------------------------------------------------------*/
200 /*! \name Output */
201 /*! \{ */
203 template<class ValueT>
204 bool addUniformVariable (const Char8 *name, const ValueT &value);
206 template<class ValueT>
207 bool updateUniformVariable(const Char8 *name, const ValueT &value);
209 template<class ValueT>
210 bool getUniformVariable (const Char8 *name, ValueT &value);
212 bool subUniformVariable (const Char8 *name );
214 void clearUniformVariables( void );
216 bool addOSGVariable (const Char8 *name );
218 /*! \} */
220 /*---------------------------------------------------------------------*/
221 /*! \name Output */
222 /*! \{ */
224 bool addProceduralVariable (const Char8 *name,
225 ProcVarFunctor pFunc,
226 UInt32 uiDependency =
227 ShaderProcVariable::SHDObject);
229 bool updateProceduralVariable (const Char8 *name,
230 ProcVarFunctor pFunc,
231 UInt32 uiDependency =
232 ShaderProcVariable::SHDObject);
234 bool addNodeProceduralVariable (const Char8 *name,
235 ProcVarNodeFunctor pFunc,
236 UInt32 uiDependency =
237 ShaderProcVariable::SHDObject);
239 bool updateNodeProceduralVariable(const Char8 *name,
240 ProcVarNodeFunctor pFunc,
241 UInt32 uiDependency =
242 ShaderProcVariable::SHDObject);
244 /*! \} */
245 /*---------------------------------------------------------------------*/
246 /*! \name Output */
247 /*! \{ */
248 bool addUniformBlock (const Char8 *name, UInt32 value);
249 bool updateUniformBlock (const Char8 *name, UInt32 value);
250 bool getUniformBlock (const Char8 *name, UInt32& value);
251 bool subUniformBlock (const Char8 *name );
253 /*! \} */
254 /*---------------------------------------------------------------------*/
255 /*! \name Output */
256 /*! \{ */
257 bool addShaderStorageBlock (const Char8 *name, UInt32 value);
258 bool updateShaderStorageBlock (const Char8 *name, UInt32 value);
259 bool getShaderStorageBlock (const Char8 *name, UInt32& value);
260 bool subShaderStorageBlock (const Char8 *name );
262 /*! \} */
263 /*---------------------------------------------------------------------*/
264 /*! \name Output */
265 /*! \{ */
267 void setProgramParameter(GLenum name, UInt32 value);
268 void subProgramParameter(GLenum name );
270 /*! \} */
271 /*---------------------------------------------------------------------*/
272 /*! \name Output */
273 /*! \{ */
275 void setProgramAttribute (UInt16 uiIndex, std::string szName);
276 void subProgramAttribute (UInt16 uiIndex );
278 void createDefaulAttribMapping(void );
280 /*! \} */
281 /*---------------------------------------------------------------------*/
282 /*! \name Output */
283 /*! \{ */
285 bool hasParameter (void);
286 bool hasAttributes(void);
287 UInt16 getProgramId (void);
289 /*! \} */
290 /*---------------------------------------------------------------------*/
291 /*! \name Output */
292 /*! \{ */
294 void accumulateFeedback(DrawEnv *pEnv,
295 UInt32 uiProgram,
296 std::vector<const Char8 *> &vTFVaryings,
297 UInt32 &uiVaryingBufferIndex);
299 /*! \} */
300 /*---------------------------------------------------------------------*/
301 /*! \name Output */
302 /*! \{ */
304 void addParent(FieldContainer * const pParent,
305 UInt16 uiParentFieldId);
307 void subParent(FieldContainer * const pParent );
309 /*! \} */
310 /*---------------------------------------------------------------------*/
311 /*! \name Output */
312 /*! \{ */
314 virtual void dump( UInt32 uiIndent = 0,
315 const BitVector bvFlags = 0) const;
317 /*! \} */
318 /*---------------------------------------------------------------------*/
319 /*! \name Output */
320 /*! \{ */
322 void addDestroyedFunctor (ChangedFunctor func,
323 std::string createSymbol);
325 template<class FunctorT>
326 void subDestroyedFunctor (FunctorT func );
328 template<class FunctorT>
329 bool hasDestroyedFunctor (FunctorT func );
331 void clearDestroyedFunctors (void );
333 UInt32 getNumDestroyedFunctors(void ) const;
335 /*! \} */
336 /*========================= PROTECTED ===============================*/
338 protected:
340 static UInt32 _extSHL;
341 static UInt32 _extCG;
342 static UInt32 _extGeoShader4;
343 static UInt32 _extGPUShader4;
344 static UInt32 _extTransformFeedback2;
345 static UInt32 _extUniformBufferObject;
346 static UInt32 _extProgramInterfaceQuery;
347 static UInt32 _extShaderStorageBufferObject;
349 static UInt32 FuncIdCreateShader;
350 static UInt32 FuncIdDeleteShader;
351 static UInt32 FuncIdShaderSource;
352 static UInt32 FuncIdCompileShader;
353 static UInt32 FuncIdAttachShader;
354 static UInt32 FuncIdGetShaderiv;
355 static UInt32 FuncIdGetShaderInfoLog;
356 static UInt32 FuncIdGetActiveAttrib;
358 static UInt32 FuncIdCreateProgram;
359 static UInt32 FuncIdDeleteProgram;
360 static UInt32 FuncIdLinkProgram;
361 static UInt32 FuncIdGetProgramiv;
362 static UInt32 FuncIdGetProgramInfoLog;
363 static UInt32 FuncIdUseProgram;
365 static UInt32 FuncIdGetUniformLocation;
367 static UInt32 FuncIdUniform1i;
368 static UInt32 FuncIdUniform2i;
369 static UInt32 FuncIdUniform3i;
370 static UInt32 FuncIdUniform4i;
372 static UInt32 FuncIdUniform1ui;
373 static UInt32 FuncIdUniform2ui;
374 static UInt32 FuncIdUniform3ui;
375 static UInt32 FuncIdUniform4ui;
377 static UInt32 FuncIdUniform1f;
378 static UInt32 FuncIdUniform2f;
379 static UInt32 FuncIdUniform3f;
380 static UInt32 FuncIdUniform4f;
382 static UInt32 FuncIdUniform1iv;
383 static UInt32 FuncIdUniform2iv;
384 static UInt32 FuncIdUniform3iv;
385 static UInt32 FuncIdUniform4iv;
387 static UInt32 FuncIdUniform1uiv;
388 static UInt32 FuncIdUniform2uiv;
389 static UInt32 FuncIdUniform3uiv;
390 static UInt32 FuncIdUniform4uiv;
392 static UInt32 FuncIdUniform1fv;
393 static UInt32 FuncIdUniform2fv;
394 static UInt32 FuncIdUniform3fv;
395 static UInt32 FuncIdUniform4fv;
397 static UInt32 FuncIdUniformMatrix2fv;
398 static UInt32 FuncIdUniformMatrix3fv;
399 static UInt32 FuncIdUniformMatrix4fv;
401 static UInt32 FuncIdGetUniformiv;
402 static UInt32 FuncIdGetUniformfv;
404 static UInt32 FuncIdProgramParameteri;
405 static UInt32 FuncIdBindAttribLocation;
407 static UInt32 FuncIdBindBufferBase;
408 static UInt32 FuncIdTransformFeedbackVaryings;
409 static UInt32 FuncIdBeginTransformFeedback;
410 static UInt32 FuncIdEndTransformFeedback;
411 static UInt32 FuncIdPauseTransformFeedback;
412 static UInt32 FuncIdResumeTransformFeedback;
414 static UInt32 FuncIdGetUniformBlockIndex;
415 static UInt32 FuncIdUniformBlockBinding;
417 static UInt32 FuncIdGetProgramResourceIndex;
418 static UInt32 FuncIdGetProgramResourceiv;
419 static UInt32 FuncIdShaderStorageBlockBinding;
421 static const Char8 *NextBufferToken;
423 /*---------------------------------------------------------------------*/
425 typedef SimpleReusePool<Int32,
426 ProgramIdPoolTag,
427 SingleLockPolicy> ProgramIdPool;
430 static ProgramIdPool *_pProgIdPool;
432 UInt16 _uiProgId;
434 /*---------------------------------------------------------------------*/
436 // Variables should all be in ShaderProgramBase.
438 /*---------------------------------------------------------------------*/
439 /*! \name Constructors */
440 /*! \{ */
442 ShaderProgram(void);
443 ShaderProgram(const ShaderProgram &source);
445 /*! \} */
446 /*---------------------------------------------------------------------*/
447 /*! \name Destructors */
448 /*! \{ */
450 virtual ~ShaderProgram(void);
452 /*! \} */
453 /*---------------------------------------------------------------------*/
454 /*! \name Init */
455 /*! \{ */
457 static void initMethod(InitPhase ePhase);
459 /*! \} */
460 /*---------------------------------------------------------------------*/
461 /*! \name Init */
462 /*! \{ */
464 bool readProgram( std::string &szTarget,
465 const Char8 *szFilename);
467 bool readProgram( std::string &szTarget,
468 std::istream &iStream);
470 /*! \} */
471 /*---------------------------------------------------------------------*/
472 /*! \name Init */
473 /*! \{ */
475 void onCreate (const ShaderProgram *source = NULL);
476 void onCreateAspect (const ShaderProgram *createAspect,
477 const ShaderProgram *source = NULL);
478 virtual void onDestroy ( UInt32 uiId );
480 virtual void onDestroyAspect( UInt32 uiContainerId,
481 UInt32 uiAspect );
483 virtual void resolveLinks ( void );
485 /*! \} */
486 /*---------------------------------------------------------------------*/
487 /*! \name Init */
488 /*! \{ */
490 UInt32 handleGL (DrawEnv *pEnv,
491 UInt32 id,
492 Window::GLObjectStatusE mode,
493 UInt64 uiOptions);
494 static void handleDestroyGL(DrawEnv *pEnv,
495 UInt32 id,
496 Window::GLObjectStatusE mode );
498 /*! \} */
499 /*========================== PRIVATE ================================*/
501 private:
503 friend class FieldContainer;
504 friend class ShaderProgramBase;
506 // prohibit default functions (move to 'public' if you need one)
507 void operator =(const ShaderProgram &source);
510 typedef ShaderProgram *ShaderProgramP;
512 OSG_END_NAMESPACE
514 #include "OSGShaderProgramBase.inl"
515 #include "OSGShaderProgram.inl"
517 #endif /* _OSGSHADERPROGRAM_H_ */