1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
48 #include "OSGShaderProgram.h"
49 #include "OSGGLFuncProtos.h"
51 #include <boost/bind.hpp>
55 // Documentation for this class is emitted in the
56 // OSGShaderProgramBase.cpp file.
57 // To modify it, please change the .fcd file (OSGShaderProgram.fcd) and
58 // regenerate the base file.
60 /***************************************************************************\
62 \***************************************************************************/
64 UInt32
ShaderProgram::_extSHL
= Window::invalidExtensionID
;
65 UInt32
ShaderProgram::_extCG
= Window::invalidExtensionID
;
66 UInt32
ShaderProgram::_extGeoShader4
= Window::invalidExtensionID
;
67 UInt32
ShaderProgram::_extGPUShader4
= Window::invalidExtensionID
;
68 UInt32
ShaderProgram::_extTransformFeedback2
= Window::invalidExtensionID
;
69 UInt32
ShaderProgram::_extUniformBufferObject
= Window::invalidExtensionID
;
70 UInt32
ShaderProgram::_extProgramInterfaceQuery
= Window::invalidExtensionID
;
71 UInt32
ShaderProgram::_extShaderStorageBufferObject
= Window::invalidExtensionID
;
73 UInt32
ShaderProgram::FuncIdCreateShader
= Window::invalidFunctionID
;
74 UInt32
ShaderProgram::FuncIdDeleteShader
= Window::invalidFunctionID
;
75 UInt32
ShaderProgram::FuncIdShaderSource
= Window::invalidFunctionID
;
76 UInt32
ShaderProgram::FuncIdCompileShader
= Window::invalidFunctionID
;
77 UInt32
ShaderProgram::FuncIdAttachShader
= Window::invalidFunctionID
;
78 UInt32
ShaderProgram::FuncIdGetShaderiv
= Window::invalidFunctionID
;
79 UInt32
ShaderProgram::FuncIdGetShaderInfoLog
= Window::invalidFunctionID
;
80 UInt32
ShaderProgram::FuncIdGetActiveAttrib
= Window::invalidFunctionID
;
82 UInt32
ShaderProgram::FuncIdCreateProgram
= Window::invalidFunctionID
;
83 UInt32
ShaderProgram::FuncIdDeleteProgram
= Window::invalidFunctionID
;
84 UInt32
ShaderProgram::FuncIdLinkProgram
= Window::invalidFunctionID
;
85 UInt32
ShaderProgram::FuncIdGetProgramiv
= Window::invalidFunctionID
;
86 UInt32
ShaderProgram::FuncIdGetProgramInfoLog
= Window::invalidFunctionID
;
87 UInt32
ShaderProgram::FuncIdUseProgram
= Window::invalidFunctionID
;
89 UInt32
ShaderProgram::FuncIdGetUniformLocation
= Window::invalidFunctionID
;
91 UInt32
ShaderProgram::FuncIdUniform1i
= Window::invalidFunctionID
;
92 UInt32
ShaderProgram::FuncIdUniform2i
= Window::invalidFunctionID
;
93 UInt32
ShaderProgram::FuncIdUniform3i
= Window::invalidFunctionID
;
94 UInt32
ShaderProgram::FuncIdUniform4i
= Window::invalidFunctionID
;
96 UInt32
ShaderProgram::FuncIdUniform1ui
= Window::invalidFunctionID
;
97 UInt32
ShaderProgram::FuncIdUniform2ui
= Window::invalidFunctionID
;
98 UInt32
ShaderProgram::FuncIdUniform3ui
= Window::invalidFunctionID
;
99 UInt32
ShaderProgram::FuncIdUniform4ui
= Window::invalidFunctionID
;
101 UInt32
ShaderProgram::FuncIdUniform1f
= Window::invalidFunctionID
;
102 UInt32
ShaderProgram::FuncIdUniform2f
= Window::invalidFunctionID
;
103 UInt32
ShaderProgram::FuncIdUniform3f
= Window::invalidFunctionID
;
104 UInt32
ShaderProgram::FuncIdUniform4f
= Window::invalidFunctionID
;
107 UInt32
ShaderProgram::FuncIdUniform1iv
= Window::invalidFunctionID
;
108 UInt32
ShaderProgram::FuncIdUniform2iv
= Window::invalidFunctionID
;
109 UInt32
ShaderProgram::FuncIdUniform3iv
= Window::invalidFunctionID
;
110 UInt32
ShaderProgram::FuncIdUniform4iv
= Window::invalidFunctionID
;
112 UInt32
ShaderProgram::FuncIdUniform1uiv
= Window::invalidFunctionID
;
113 UInt32
ShaderProgram::FuncIdUniform2uiv
= Window::invalidFunctionID
;
114 UInt32
ShaderProgram::FuncIdUniform3uiv
= Window::invalidFunctionID
;
115 UInt32
ShaderProgram::FuncIdUniform4uiv
= Window::invalidFunctionID
;
117 UInt32
ShaderProgram::FuncIdUniform1fv
= Window::invalidFunctionID
;
118 UInt32
ShaderProgram::FuncIdUniform2fv
= Window::invalidFunctionID
;
119 UInt32
ShaderProgram::FuncIdUniform3fv
= Window::invalidFunctionID
;
120 UInt32
ShaderProgram::FuncIdUniform4fv
= Window::invalidFunctionID
;
122 UInt32
ShaderProgram::FuncIdUniformMatrix2fv
= Window::invalidFunctionID
;
123 UInt32
ShaderProgram::FuncIdUniformMatrix3fv
= Window::invalidFunctionID
;
124 UInt32
ShaderProgram::FuncIdUniformMatrix4fv
= Window::invalidFunctionID
;
126 UInt32
ShaderProgram::FuncIdGetUniformiv
= Window::invalidFunctionID
;
127 UInt32
ShaderProgram::FuncIdGetUniformfv
= Window::invalidFunctionID
;
129 UInt32
ShaderProgram::FuncIdProgramParameteri
= Window::invalidFunctionID
;
130 UInt32
ShaderProgram::FuncIdBindAttribLocation
= Window::invalidFunctionID
;
132 UInt32
ShaderProgram::FuncIdBindBufferBase
= Window::invalidFunctionID
;
134 UInt32
ShaderProgram::FuncIdTransformFeedbackVaryings
= Window::invalidFunctionID
;
136 UInt32
ShaderProgram::FuncIdBeginTransformFeedback
= Window::invalidFunctionID
;
137 UInt32
ShaderProgram::FuncIdEndTransformFeedback
= Window::invalidFunctionID
;
139 UInt32
ShaderProgram::FuncIdPauseTransformFeedback
= Window::invalidFunctionID
;
140 UInt32
ShaderProgram::FuncIdResumeTransformFeedback
= Window::invalidFunctionID
;
142 UInt32
ShaderProgram::FuncIdGetUniformBlockIndex
= Window::invalidFunctionID
;
143 UInt32
ShaderProgram::FuncIdUniformBlockBinding
= Window::invalidFunctionID
;
145 UInt32
ShaderProgram::FuncIdGetProgramResourceIndex
= Window::invalidFunctionID
;
146 UInt32
ShaderProgram::FuncIdGetProgramResourceiv
= Window::invalidFunctionID
;
147 UInt32
ShaderProgram::FuncIdShaderStorageBlockBinding
= Window::invalidFunctionID
;
149 const Char8
*ShaderProgram::NextBufferToken
= "gl_NextBuffer";
151 ShaderProgram::ProgramIdPool
*ShaderProgram::_pProgIdPool
= NULL
;
154 void SimpleReusePool
<Int32
,
155 ShaderProgram::ProgramIdPoolTag
,
156 SingleLockPolicy
>::initializeValue(void)
161 /***************************************************************************\
163 \***************************************************************************/
165 void ShaderProgram::initMethod(InitPhase ePhase
)
167 Inherited::initMethod(ePhase
);
169 if(ePhase
== TypeObject::SystemPost
)
172 Window::registerExtension("GL_ARB_shading_language_100");
175 Window::registerExtension("GL_EXT_Cg_shader");
178 Window::registerExtension("GL_EXT_geometry_shader4");
181 Window::registerExtension("GL_EXT_gpu_shader4");
183 _extTransformFeedback2
=
184 Window::registerExtension("GL_EXT_transform_feedback2");
186 _extUniformBufferObject
=
187 Window::registerExtension("GL_ARB_uniform_buffer_object");
189 _extProgramInterfaceQuery
=
190 Window::registerExtension("GL_ARB_program_interface_query");
192 _extShaderStorageBufferObject
=
193 Window::registerExtension("GL_ARB_shader_storage_buffer_object");
197 Window::registerFunction
198 (OSG_DLSYM_UNDERSCORE
"glCreateShader",
202 Window::registerFunction (
203 OSG_DLSYM_UNDERSCORE
"glDeleteShader",
207 Window::registerFunction (
208 OSG_DLSYM_UNDERSCORE
"glShaderSource",
211 FuncIdCompileShader
=
212 Window::registerFunction (
213 OSG_DLSYM_UNDERSCORE
"glCompileShader",
217 Window::registerFunction (
218 OSG_DLSYM_UNDERSCORE
"glAttachShader",
222 Window::registerFunction (
223 OSG_DLSYM_UNDERSCORE
"glGetShaderiv",
226 FuncIdGetShaderInfoLog
=
227 Window::registerFunction (
228 OSG_DLSYM_UNDERSCORE
"glGetShaderInfoLog",
231 FuncIdGetActiveAttrib
=
232 Window::registerFunction (
233 OSG_DLSYM_UNDERSCORE
"glGetActiveAttrib",
237 FuncIdCreateProgram
=
238 Window::registerFunction (
239 OSG_DLSYM_UNDERSCORE
"glCreateProgram",
242 FuncIdDeleteProgram
=
243 Window::registerFunction (
244 OSG_DLSYM_UNDERSCORE
"glDeleteProgram",
248 Window::registerFunction (
249 OSG_DLSYM_UNDERSCORE
"glLinkProgram",
253 Window::registerFunction (
254 OSG_DLSYM_UNDERSCORE
"glGetProgramiv",
257 FuncIdGetProgramInfoLog
=
258 Window::registerFunction (
259 OSG_DLSYM_UNDERSCORE
"glGetProgramInfoLog",
263 Window::registerFunction (
264 OSG_DLSYM_UNDERSCORE
"glUseProgram",
268 FuncIdGetUniformLocation
=
269 Window::registerFunction (
270 OSG_DLSYM_UNDERSCORE
"glGetUniformLocationARB",
273 // int{,2,3,4} uniforms
275 Window::registerFunction (
276 OSG_DLSYM_UNDERSCORE
"glUniform1iARB",
280 Window::registerFunction (
281 OSG_DLSYM_UNDERSCORE
"glUniform2iARB",
285 Window::registerFunction (
286 OSG_DLSYM_UNDERSCORE
"glUniform3iARB",
290 Window::registerFunction (
291 OSG_DLSYM_UNDERSCORE
"glUniform4iARB",
294 // uint{,2,3,4} uniforms
296 Window::registerFunction (
297 OSG_DLSYM_UNDERSCORE
"glUniform1uiEXT",
301 Window::registerFunction (
302 OSG_DLSYM_UNDERSCORE
"glUniform2uiEXT",
306 Window::registerFunction (
307 OSG_DLSYM_UNDERSCORE
"glUniform3uiEXT",
311 Window::registerFunction (
312 OSG_DLSYM_UNDERSCORE
"glUniform4uiEXT",
315 // float, vec{2,3,4} uniforms
317 Window::registerFunction (
318 OSG_DLSYM_UNDERSCORE
"glUniform1fARB",
322 Window::registerFunction (
323 OSG_DLSYM_UNDERSCORE
"glUniform2fARB",
327 Window::registerFunction (
328 OSG_DLSYM_UNDERSCORE
"glUniform3fARB",
332 Window::registerFunction (
333 OSG_DLSYM_UNDERSCORE
"glUniform4fARB",
336 // int{,2,3,4} uniform arrays
338 Window::registerFunction (
339 OSG_DLSYM_UNDERSCORE
"glUniform1ivARB",
343 Window::registerFunction (
344 OSG_DLSYM_UNDERSCORE
"glUniform2ivARB",
347 Window::registerFunction (
348 OSG_DLSYM_UNDERSCORE
"glUniform3ivARB",
351 Window::registerFunction (
352 OSG_DLSYM_UNDERSCORE
"glUniform4ivARB",
355 // uint{,2,3,4} uniform arrays
357 Window::registerFunction (
358 OSG_DLSYM_UNDERSCORE
"glUniform1uivEXT",
362 Window::registerFunction (
363 OSG_DLSYM_UNDERSCORE
"glUniform2uivEXT",
366 Window::registerFunction (
367 OSG_DLSYM_UNDERSCORE
"glUniform3uivEXT",
370 Window::registerFunction (
371 OSG_DLSYM_UNDERSCORE
"glUniform4uivEXT",
374 // float, vec{2,3,4} uniform arrays
376 Window::registerFunction (
377 OSG_DLSYM_UNDERSCORE
"glUniform1fvARB",
381 Window::registerFunction (
382 OSG_DLSYM_UNDERSCORE
"glUniform2fvARB",
386 Window::registerFunction (
387 OSG_DLSYM_UNDERSCORE
"glUniform3fvARB",
391 Window::registerFunction (
392 OSG_DLSYM_UNDERSCORE
"glUniform4fvARB",
396 FuncIdUniformMatrix2fv
=
397 Window::registerFunction (
398 OSG_DLSYM_UNDERSCORE
"glUniformMatrix2fvARB",
401 FuncIdUniformMatrix3fv
=
402 Window::registerFunction (
403 OSG_DLSYM_UNDERSCORE
"glUniformMatrix3fvARB",
406 FuncIdUniformMatrix4fv
=
407 Window::registerFunction (
408 OSG_DLSYM_UNDERSCORE
"glUniformMatrix4fvARB",
412 Window::registerFunction (
413 OSG_DLSYM_UNDERSCORE
"glGetUniformivARB",
417 Window::registerFunction (
418 OSG_DLSYM_UNDERSCORE
"glGetUniformfvARB",
421 FuncIdProgramParameteri
=
422 Window::registerFunction (
423 OSG_DLSYM_UNDERSCORE
"glProgramParameteriEXT",
426 FuncIdBindAttribLocation
=
427 Window::registerFunction (
428 OSG_DLSYM_UNDERSCORE
"glBindAttribLocationARB",
431 FuncIdBindBufferBase
=
432 Window::registerFunction (
433 OSG_DLSYM_UNDERSCORE
"glBindBufferBase",
434 _extTransformFeedback2
);
436 FuncIdTransformFeedbackVaryings
=
437 Window::registerFunction (
438 OSG_DLSYM_UNDERSCORE
"glTransformFeedbackVaryings",
439 _extTransformFeedback2
);
441 FuncIdBeginTransformFeedback
=
442 Window::registerFunction (
443 OSG_DLSYM_UNDERSCORE
"glBeginTransformFeedback",
444 _extTransformFeedback2
);
446 FuncIdEndTransformFeedback
=
447 Window::registerFunction (
448 OSG_DLSYM_UNDERSCORE
"glEndTransformFeedback",
449 _extTransformFeedback2
);
451 FuncIdPauseTransformFeedback
=
452 Window::registerFunction (
453 OSG_DLSYM_UNDERSCORE
"glPauseTransformFeedback",
454 _extTransformFeedback2
);
456 FuncIdResumeTransformFeedback
=
457 Window::registerFunction (
458 OSG_DLSYM_UNDERSCORE
"glResumeTransformFeedback",
459 _extTransformFeedback2
);
461 FuncIdGetUniformBlockIndex
=
462 Window::registerFunction (
463 OSG_DLSYM_UNDERSCORE
"glGetUniformBlockIndex",
464 _extUniformBufferObject
);
466 FuncIdUniformBlockBinding
=
467 Window::registerFunction (
468 OSG_DLSYM_UNDERSCORE
"glUniformBlockBinding",
469 _extUniformBufferObject
);
471 FuncIdGetProgramResourceIndex
=
472 Window::registerFunction (
473 OSG_DLSYM_UNDERSCORE
"glGetProgramResourceIndex",
474 _extProgramInterfaceQuery
);
476 FuncIdGetProgramResourceiv
=
477 Window::registerFunction (
478 OSG_DLSYM_UNDERSCORE
"glGetProgramResourceiv",
479 _extProgramInterfaceQuery
);
481 FuncIdShaderStorageBlockBinding
=
482 Window::registerFunction (
483 OSG_DLSYM_UNDERSCORE
"glShaderStorageBlockBinding",
484 _extShaderStorageBufferObject
);
489 /***************************************************************************\
491 \***************************************************************************/
493 /*-------------------------------------------------------------------------*\
495 \*-------------------------------------------------------------------------*/
497 /*----------------------- constructors & destructors ----------------------*/
499 ShaderProgram::ShaderProgram(void) :
505 ShaderProgram::ShaderProgram(const ShaderProgram
&source
) :
511 ShaderProgram::~ShaderProgram(void)
515 void ShaderProgram::onCreate(const ShaderProgram
*source
)
517 Inherited::onCreate(source
);
519 // ignore prototypes.
520 if(GlobalSystemState
== Startup
)
522 if(_pProgIdPool
== NULL
)
524 _pProgIdPool
= new ProgramIdPool
;
531 Window::registerGLObject(
532 boost::bind(&ShaderProgram::handleGL
,
533 ShaderProgramMTUncountedPtr(this),
535 &ShaderProgram::handleDestroyGL
));
537 _uiProgId
= _pProgIdPool
->create();
540 void ShaderProgram::onCreateAspect(const ShaderProgram
*createAspect
,
541 const ShaderProgram
*source
)
543 Inherited::onCreateAspect(createAspect
, source
);
545 _uiProgId
= createAspect
->_uiProgId
;
548 void ShaderProgram::onDestroy(UInt32 uiId
)
550 if(GlobalSystemState
== OSG::Running
)
552 _pProgIdPool
->release(_uiProgId
);
555 Window::destroyGLObject(getGLId(), 1);
557 else if(GlobalSystemState
== OSG::Shutdown
)
564 Inherited::onDestroy(uiId
);
567 void ShaderProgram::onDestroyAspect(UInt32 uiContainerId
,
570 Inherited::onDestroyAspect(uiContainerId
, uiAspect
);
573 void ShaderProgram::resolveLinks(void)
575 MFDestroyedFunctorsType::const_iterator dfIt
=
576 _mfDestroyedFunctors
.begin();
577 MFDestroyedFunctorsType::const_iterator dfEnd
=
578 _mfDestroyedFunctors
.end();
580 for(; dfIt
!= dfEnd
; ++dfIt
)
582 if(dfIt
->_func
.empty() == false)
583 (dfIt
->_func
)(this, 0x0000, ChangedOrigin::External
);
586 Inherited::resolveLinks();
589 UInt32
ShaderProgram::handleGL(DrawEnv
*pEnv
,
591 Window::GLObjectStatusE mode
,
594 Window
*pWin
= pEnv
->getWindow();
596 if(pWin
->hasExtOrVersion(_extSHL
, 0x0200, 0x0200) == false)
598 FWARNING(("OpenGL Shading Language is not supported, couldn't find "
599 "extension 'GL_ARB_shading_language_100'!\n"));
601 pWin
->setGLObjectId(id
, 0);
606 if(mode
== Window::initialize
||
607 mode
== Window::reinitialize
||
608 mode
== Window::needrefresh
)
610 if( mode
!= Window::needrefresh
&&
611 _sfProgram
.getValue().empty() == false )
613 GLuint uiShader
= GLuint(pWin
->getGLObjectId(getGLId()));
616 if(mode
== Window::initialize
&& uiShader
!= 0)
618 FWARNING(("ShaderProgram::handleGL: "
619 "Initialize with non-zero GL object Id.\n"));
626 // if(mode == Window::initialize)
628 OSGGETGLFUNCBYID_GL3_ES(glCreateShader
,
633 GLenum shaderType
= _sfShaderType
.getValue();
635 if(_sfCgFrontEnd
.getValue() == true)
637 if(pWin
->hasExtension(_extCG
))
641 case GL_VERTEX_SHADER
:
642 shaderType
= GL_CG_VERTEX_SHADER_EXT
;
644 case GL_FRAGMENT_SHADER
:
645 shaderType
= GL_CG_FRAGMENT_SHADER_EXT
;
651 FWARNING(("EXT_Cg_shader extension not supported, "
652 "using GLSL front end!\n"));
656 uiShader
= osgGlCreateShader(shaderType
);
662 const Char8
*source
= _sfProgram
.getValue().c_str();
664 OSGGETGLFUNCBYID_GL3_ES(glShaderSource
,
669 OSGGETGLFUNCBYID_GL3_ES(glCompileShader
,
674 OSGGETGLFUNCBYID_GL3_ES(glGetShaderiv
,
679 if(_sfDefines
.getValue().empty() == true)
681 osgGlShaderSource(uiShader
,
683 static_cast<const char **>(&source
),
688 const Char8
*defines
= _sfDefines
.getValue().c_str();
690 const char *shaderSources
[2] =
696 osgGlShaderSource(uiShader
,
702 osgGlCompileShader(uiShader
);
706 osgGlGetShaderiv( uiShader
,
714 osgGlGetShaderiv( uiShader
,
720 Char8
*szDebug
= new Char8
[iDebugLength
];
722 OSGGETGLFUNCBYID_GL3_ES(glGetShaderInfoLog
,
723 osgGlGetShaderInfoLog
,
724 FuncIdGetShaderInfoLog
,
727 osgGlGetShaderInfoLog( uiShader
,
732 FFATAL(( "Couldn't compile shader program (0x%x)!\n%s\n",
733 _sfShaderType
.getValue(),
739 // log source that failed to compile
740 FINFO(("Shader source was:\n"));
742 if(_sfDefines
.getValue().empty() == false)
744 FPINFO(("%s\n", _sfDefines
.getValue().c_str()));
747 FPINFO(("%s\n", source
));
749 OSGGETGLFUNCBYID_GL3_ES(glDeleteShader
,
754 osgGlDeleteShader(uiShader
);
759 pWin
->setGLObjectId(getGLId(), uiShader
);
762 //updateProgramParameters(win);
763 //updateParameters(win,
764 //*getMFParameters(),
766 //true /*mode != Window::needrefresh*/);
772 void ShaderProgram::handleDestroyGL(DrawEnv
*pEnv
,
774 Window::GLObjectStatusE mode
)
776 Window
*pWin
= pEnv
->getWindow();
778 if(!pWin
->hasExtOrVersion(_extSHL
, 0x0200, 0x0200))
780 FWARNING(("OpenGL Shading Language is not supported, couldn't find "
781 "extension 'GL_ARB_shading_language_100'!\n"));
783 pWin
->setGLObjectId(id
, 0);
788 if(mode
== Window::destroy
)
790 GLuint uiShader
= GLuint(pWin
->getGLObjectId(id
));
794 OSGGETGLFUNCBYID_GL3_ES(glDeleteShader
,
799 osgGlDeleteShader(uiShader
);
801 pWin
->setGLObjectId(id
, 0);
804 else if(mode
== Window::finaldestroy
)
806 ;//SWARNING << "Last program user destroyed" << std::endl;
811 /*----------------------------- class specific ----------------------------*/
813 void ShaderProgram::changed(ConstFieldMaskArg whichField
,
817 Inherited::changed(whichField
, origin
, details
);
819 if(0x0000 != (whichField
& VariablesFieldMask
))
821 if(details
== 0x0001)
823 MFParentFieldContainerPtr::const_iterator parentsIt
=
824 this->_mfParents
.begin();
826 MFParentFieldContainerPtr::const_iterator parentsEnd
=
827 this->_mfParents
.end();
829 while(parentsIt
!= parentsEnd
)
831 (*parentsIt
)->changed(
832 TypeTraits
<BitVector
>::One
<< parentsIt
.getParentFieldPos(),
833 ChangedOrigin::Child
,
841 if(0x0000 != (whichField
& ProgramFieldMask
))
843 MFParentFieldContainerPtr::const_iterator parentsIt
=
844 this->_mfParents
.begin();
846 MFParentFieldContainerPtr::const_iterator parentsEnd
=
847 this->_mfParents
.end();
849 while(parentsIt
!= parentsEnd
)
851 (*parentsIt
)->changed(
852 TypeTraits
<BitVector
>::One
<< parentsIt
.getParentFieldPos(),
853 ChangedOrigin::Child
,
860 Window::reinitializeGLObject(this->getGLId());
864 ShaderProgramTransitPtr
ShaderProgram::createVertexShader(
865 bool bCreateDefAttribMap
)
867 ShaderProgramTransitPtr returnValue
= ShaderProgram::create();
869 returnValue
->setShaderType(GL_VERTEX_SHADER
);
871 if(bCreateDefAttribMap
== true)
872 returnValue
->createDefaulAttribMapping();
877 ShaderProgramTransitPtr
ShaderProgram::createGeometryShader(void)
879 ShaderProgramTransitPtr returnValue
= ShaderProgram::create();
881 returnValue
->setShaderType(GL_GEOMETRY_SHADER_EXT
);
886 ShaderProgramTransitPtr
ShaderProgram::createFragmentShader(void)
888 ShaderProgramTransitPtr returnValue
= ShaderProgram::create();
890 returnValue
->setShaderType(GL_FRAGMENT_SHADER
);
895 void ShaderProgram::accumulateFeedback(
898 std::vector
<const Char8
*> &vTFVaryings
,
899 UInt32
&uiVaryingBufferIndex
)
901 if(((_sfShaderType
.getValue() == GL_VERTEX_SHADER
) ||
902 (_sfShaderType
.getValue() == GL_GEOMETRY_SHADER_EXT
) ) == false)
907 if(vTFVaryings
.size() != 0)
909 vTFVaryings
.push_back(NextBufferToken
);
912 MFFeedbackVaryingsType::const_iterator vIt
=
913 this->getMFFeedbackVaryings()->begin();
914 MFFeedbackVaryingsType::const_iterator vEnd
=
915 this->getMFFeedbackVaryings()->end ();
917 for(; vIt
!= vEnd
; ++vIt
)
919 vTFVaryings
.push_back((*vIt
).c_str());
923 void ShaderProgram::addParent(FieldContainer
* const pParent
,
924 UInt16 uiParentFieldId
)
926 editMField(ParentsFieldMask
, _mfParents
);
928 OSG_ASSERT(pParent
!= NULL
);
930 _mfParents
.push_back(pParent
, uiParentFieldId
);
933 void ShaderProgram::subParent(FieldContainer
* const pParent
)
935 Int32 iParentIdx
= _mfParents
.findIndex(pParent
);
939 editMField(ParentsFieldMask
, _mfParents
);
941 _mfParents
.erase(iParentIdx
);
945 void ShaderProgram::dump( UInt32
,
946 const BitVector
) const
948 SLOG
<< "Dump ShaderProgram NI" << std::endl
;
951 bool ShaderProgram::readProgram(const Char8
*file
)
953 return readProgram(editSFProgram()->getValue(), file
);
956 bool ShaderProgram::subUniformVariable(const Char8
*name
)
958 if(_sfVariables
.getValue() != NULL
)
961 return _sfVariables
.getValue()->subUniformVariable(
963 editMFVariableLocations(),
964 editMFProceduralVariableLocations());
966 return _sfVariables
.getValue()->subUniformVariable(
976 void ShaderProgram::clearUniformVariables(void)
978 if(_sfVariables
.getValue() != NULL
)
980 _sfVariables
.getValue()->clearUniformVariables();
985 bool ShaderProgram::subUniformBlock(const Char8
*name
)
987 if(_sfVariables
.getValue() != NULL
)
990 return _sfVariables
.getValue()->subUniformBlock(
992 editMFBlockLocations(),
993 editMFProceduralVariableLocations());
995 return _sfVariables
.getValue()->subUniformBlock(
1005 bool ShaderProgram::subShaderStorageBlock(const Char8
*name
)
1007 if(_sfVariables
.getValue() != NULL
)
1010 return _sfVariables
.getValue()->subShaderStorageBlock(
1012 editMFBlockLocations(),
1013 editMFProceduralVariableLocations());
1015 return _sfVariables
.getValue()->subShaderStorageBlock(
1025 bool ShaderProgram::addProceduralVariable(const Char8
*name
,
1026 ProcVarFunctor pFunc
,
1027 UInt32 uiDependency
)
1029 if(_sfVariables
.getValue() == NULL
)
1031 ShaderProgramVariablesUnrecPtr pParam
=
1032 ShaderProgramVariables::create();
1034 setVariables(pParam
);
1038 return _sfVariables
.getValue()->addProceduralVariable(
1042 editMFProceduralVariableLocations());
1044 return _sfVariables
.getValue()->addProceduralVariable(
1052 bool ShaderProgram::addNodeProceduralVariable(
1054 ProcVarNodeFunctor pFunc
,
1055 UInt32 uiDependency
)
1057 if(_sfVariables
.getValue() == NULL
)
1059 ShaderProgramVariablesUnrecPtr pParam
=
1060 ShaderProgramVariables::create();
1062 setVariables(pParam
);
1066 return _sfVariables
.getValue()->addNodeProceduralVariable(
1070 editMFProceduralVariableLocations());
1072 return _sfVariables
.getValue()->addNodeProceduralVariable(
1087 ParamEqual(GLenum ref
) :
1092 bool operator() (const ShaderParameter
&lhs
)
1094 return lhs
.first
== _ref
;
1098 void ShaderProgram::setProgramParameter(GLenum name
, UInt32 value
)
1100 editMField(ParameterFieldMask
, _mfParameter
);
1102 MFShaderParameter::iterator pIt
=
1103 std::find_if(_mfParameter
.begin(),
1104 _mfParameter
.end (),
1107 if(pIt
!= _mfParameter
.end())
1109 pIt
->second
= value
;
1113 ShaderParameter tmpParam
;
1115 tmpParam
.first
= name
;
1116 tmpParam
.second
= value
;
1118 _mfParameter
.push_back(tmpParam
);
1122 void ShaderProgram::subProgramParameter(GLenum name
)
1124 MFShaderParameter::iterator pIt
=
1125 std::find_if(_mfParameter
.begin(),
1126 _mfParameter
.end (),
1129 if(pIt
!= _mfParameter
.end())
1131 editMField(ParameterFieldMask
, _mfParameter
);
1133 _mfParameter
.erase(pIt
);
1141 AttribEqual(UInt16 ref
) :
1146 bool operator() (const ShaderAttribute
&lhs
)
1148 return lhs
.first
== _ref
;
1152 void ShaderProgram::setProgramAttribute(UInt16 uiIndex
, std::string szName
)
1154 editMField(AttributesFieldMask
, _mfAttributes
);
1156 MFShaderAttribute::iterator aIt
=
1157 std::find_if(_mfAttributes
.begin(),
1158 _mfAttributes
.end (),
1159 AttribEqual (uiIndex
));
1162 if(aIt
!= _mfAttributes
.end())
1164 aIt
->second
= szName
;
1168 ShaderAttribute tmpAttr
;
1170 tmpAttr
.first
= uiIndex
;
1171 tmpAttr
.second
= szName
;
1173 _mfAttributes
.push_back(tmpAttr
);
1177 void ShaderProgram::subProgramAttribute(UInt16 uiIndex
)
1179 MFAttributesType::iterator aIt
=
1180 std::find_if(_mfAttributes
.begin(),
1181 _mfAttributes
.end (),
1182 AttribEqual (uiIndex
));
1184 if(aIt
!= _mfAttributes
.end())
1186 editMField(AttributesFieldMask
, _mfAttributes
);
1188 _mfAttributes
.erase(aIt
);
1192 void ShaderProgram::createDefaulAttribMapping(void)
1194 if(_sfShaderType
.getValue() == GL_VERTEX_SHADER
)
1196 this->setProgramAttribute(ShaderConstants::Attribute0Index
,
1198 this->setProgramAttribute(ShaderConstants::Attribute2Index
,
1201 this->setProgramAttribute(ShaderConstants::Attribute3Index
,
1203 this->setProgramAttribute(ShaderConstants::Attribute4Index
,
1204 "osg_SecondaryColor" );
1206 this->setProgramAttribute(ShaderConstants::Attribute8Index
,
1207 "osg_MultiTexCoord0" );
1208 this->setProgramAttribute(ShaderConstants::Attribute9Index
,
1209 "osg_MultiTexCoord1" );
1210 this->setProgramAttribute(ShaderConstants::Attribute10Index
,
1211 "osg_MultiTexCoord2" );
1212 this->setProgramAttribute(ShaderConstants::Attribute11Index
,
1213 "osg_MultiTexCoord3" );
1214 this->setProgramAttribute(ShaderConstants::Attribute12Index
,
1215 "osg_MultiTexCoord4" );
1216 this->setProgramAttribute(ShaderConstants::Attribute13Index
,
1217 "osg_MultiTexCoord5" );
1218 this->setProgramAttribute(ShaderConstants::Attribute14Index
,
1219 "osg_MultiTexCoord6" );
1220 this->setProgramAttribute(ShaderConstants::Attribute15Index
,
1221 "osg_MultiTexCoord7" );
1225 FWARNING(("attrib map can only be created for a vertex shader"));
1230 bool ShaderProgram::addOSGVariable(const Char8
*name
)
1232 if(_sfVariables
.getValue() == NULL
)
1234 ShaderProgramVariablesUnrecPtr pVar
=
1235 ShaderProgramVariables::create();
1241 return _sfVariables
.getValue()->addOSGVariable(
1243 editMFProceduralVariableLocations());
1245 return _sfVariables
.getValue()->addOSGVariable(name
,
1250 bool ShaderProgram::updateProceduralVariable(const Char8
*name
,
1251 ProcVarFunctor pFunc
,
1252 UInt32 uiDependency
)
1254 if(_sfVariables
.getValue() == NULL
)
1256 ShaderProgramVariablesUnrecPtr pParam
=
1257 ShaderProgramVariables::create();
1259 setVariables(pParam
);
1262 return _sfVariables
.getValue()->updateProceduralVariable(name
,
1267 bool ShaderProgram::updateNodeProceduralVariable(
1269 ProcVarNodeFunctor pFunc
,
1270 UInt32 uiDependency
)
1272 if(_sfVariables
.getValue() == NULL
)
1274 ShaderProgramVariablesUnrecPtr pParam
=
1275 ShaderProgramVariables::create();
1277 setVariables(pParam
);
1280 return _sfVariables
.getValue()->updateNodeProceduralVariable(name
,
1285 bool ShaderProgram::readProgram( std::string
&szTarget
,
1286 const Char8
*szFilename
)
1288 std::ifstream
s(szFilename
);
1292 return readProgram(szTarget
, s
);
1296 FWARNING(("ShaderChunk::readProgram: couldn't open '%s' "
1297 "for reading!\n", szFilename
));
1304 bool ShaderProgram::readProgram(std::string
&szTarget
,
1305 std::istream
&iStream
)
1315 FWARNING(("SHLChunk::readProgram: stream is not good!\n"));
1322 iStream
.read(buf
, BUFSIZE
);
1324 szTarget
.append(buf
, iStream
.gcount());
1326 while(!iStream
.eof());