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 "OSGShaderExecutableVarChunk.h"
50 #include "OSGShaderVariables.h"
51 #include "OSGShaderProgramVariableChunk.h"
53 #include "OSGGLFuncProtos.h"
57 // Documentation for this class is emitted in the
58 // OSGShaderExecutableVarChunkBase.cpp file.
59 // To modify it, please change the .fcd file (OSGShaderExecutableVarChunk.fcd)
60 // and regenerate the base file.
62 /***************************************************************************\
64 \***************************************************************************/
66 StateChunkClass
ShaderExecutableVarChunk::_class("ShaderExeVariable", 1, 8);
68 volatile UInt16
ShaderExecutableVarChunk::_uiChunkCounter
= 1;
70 /***************************************************************************\
72 \***************************************************************************/
74 void ShaderExecutableVarChunk::initMethod(InitPhase ePhase
)
76 Inherited::initMethod(ePhase
);
78 if(ePhase
== TypeObject::SystemPost
)
84 /***************************************************************************\
86 \***************************************************************************/
88 /*-------------------------------------------------------------------------*\
90 \*-------------------------------------------------------------------------*/
92 /*----------------------- constructors & destructors ----------------------*/
94 ShaderExecutableVarChunk::ShaderExecutableVarChunk(void) :
96 #ifdef OSG_MULTISHADER_VARCHUNK
103 ShaderExecutableVarChunk::ShaderExecutableVarChunk(
104 const ShaderExecutableVarChunk
&source
) :
107 #ifdef OSG_MULTISHADER_VARCHUNK
114 ShaderExecutableVarChunk::~ShaderExecutableVarChunk(void)
118 void ShaderExecutableVarChunk::onCreate(const ShaderExecutableVarChunk
*source
)
120 Inherited::onCreate(source
);
122 if(GlobalSystemState
== Startup
)
125 _uiChunkId
= _uiChunkCounter
++;
128 void ShaderExecutableVarChunk::onCreateAspect(
129 const ShaderExecutableVarChunk
*createAspect
,
130 const ShaderExecutableVarChunk
*source
)
132 Inherited::onCreateAspect(createAspect
, source
);
134 _uiChunkId
= createAspect
->_uiChunkId
;
137 const StateChunkClass
*ShaderExecutableVarChunk::getClass(void) const
142 UInt16
ShaderExecutableVarChunk::getChunkId(void)
147 void ShaderExecutableVarChunk::merge(ShaderProgramVariableChunk
*pVar
)
152 if(_sfVariables
.getValue() == NULL
)
154 ShaderProgramVariablesUnrecPtr pVarNew
=
155 ShaderProgramVariables::createDependent(
156 this->getFieldFlags()->_bNamespaceMask
);
158 setVariables(pVarNew
);
161 _sfVariables
.getValue()->merge(pVar
->getVariables(),
162 this->editMFVariableLocations(),
166 editMField(ProgramVarChunksFieldMask
, _mfProgramVarChunks
);
168 _mfProgramVarChunks
.push_back(pVar
);
170 pVar
->addParent(this, ProgramVarChunksFieldId
);
173 /*----------------------------- class specific ----------------------------*/
175 void ShaderExecutableVarChunk::changed(ConstFieldMaskArg whichField
,
179 if(0x0000 != (whichField
& ProgramVarChunksFieldMask
))
181 if(origin
== ChangedOrigin::Child
)
183 this->remergeVariables();
187 Inherited::changed(whichField
, origin
, details
);
190 void ShaderExecutableVarChunk::dump( UInt32
,
191 const BitVector
) const
193 SLOG
<< "Dump ShaderExecutableVarChunk NI" << std::endl
;
196 void ShaderExecutableVarChunk::activate(DrawEnv
*pEnv
, UInt32
)
198 updateVariables(pEnv
);
201 void ShaderExecutableVarChunk::changeFrom(DrawEnv
*pEnv
,
205 ShaderExecutableVarChunk
*pOld
=
206 dynamic_cast<ShaderExecutableVarChunk
*>(pOther
);
210 FWARNING(( "ShaderExecutableVar::changeFrom: caught non-SHLChunk!\n"));
214 // SHLChunk didn't change so do nothing.
215 #ifdef OSG_MULTISHADER_VARCHUNK
216 if(pOld
== this && pEnv
->getActiveShader() == this->getActiveShader())
224 updateVariables(pEnv
);
227 void ShaderExecutableVarChunk::deactivate(DrawEnv
*, UInt32
)
231 void ShaderExecutableVarChunk::updateVariables(DrawEnv
*pEnv
)
233 UInt32 uiProgram
= pEnv
->getActiveShader();
238 #ifdef OSG_MULTISHADER_VARCHUNK
239 this->setActiveShader(uiProgram
);
242 pEnv
->incNumShaderParamChanges();
244 const ShaderProgramVariables::MFVariablesType
*pMFVars
= NULL
;
246 if(_sfVariables
.getValue() != NULL
)
248 pMFVars
= _sfVariables
.getValue()->getMFVariables();
251 if(pMFVars
== NULL
|| pMFVars
->size() == 0)
256 MFInt32
*pVarLocations
= this->editMFVariableLocations();
258 #ifdef OSG_MULTISHADER_VARCHUNK
259 ShaderVarLocMapIt mIt
= _mShaderVarLocs
.lower_bound(uiProgram
);
261 if(mIt
== _mShaderVarLocs
.end() || mIt
->first
!= uiProgram
)
263 mIt
= _mShaderVarLocs
.insert(mIt
,
264 ShaderVarLocMapValueT(uiProgram
,
267 mIt
->second
.resize(pVarLocations
->size(), -1);
270 pVarLocations
= &(mIt
->second
);
273 OSG_ASSERT(pMFVars
->size() == pVarLocations
->size());
275 MFInt32::iterator mLocIt
= pVarLocations
->begin();
277 ShaderProgramVariables::MFVariablesType::const_iterator mVarIt
=
279 ShaderProgramVariables::MFVariablesType::const_iterator mVarEnd
=
282 bool warnUnknown
= ShaderVariable::WarnUnknown
;
284 for(; mVarIt
!= mVarEnd
; ++mVarIt
, ++mLocIt
)
286 ShaderVariable
*pVar
= *mVarIt
;
291 osgUniformShaderVariableSwitch(pEnv
, pVar
,
292 *mLocIt
, uiProgram
, warnUnknown
);
296 void ShaderExecutableVarChunk::remergeVariables(void)
298 if(_sfVariables
.getValue() == NULL
)
300 ShaderProgramVariablesUnrecPtr pVar
=
301 ShaderProgramVariables::createDependent(
302 this->getFieldFlags()->_bNamespaceMask
);
308 _sfVariables
.getValue()->clearVariables();
310 this->editMFVariableLocations()->clear();
313 MFProgramVarChunksType::const_iterator vIt
= _mfProgramVarChunks
.begin();
314 MFProgramVarChunksType::const_iterator vEnd
= _mfProgramVarChunks
.end ();
316 for(; vIt
!= vEnd
; ++vIt
)
318 _sfVariables
.getValue()->merge((*vIt
)->getVariables(),
319 this->editMFVariableLocations(),
323 #ifdef OSG_MULTISHADER_VARCHUNK
324 ShaderVarLocMapIt mIt
= _mShaderVarLocs
.begin();
325 ShaderVarLocMapConstIt mEnd
= _mShaderVarLocs
.end ();
327 SizeT uiVarLocSize
= this->getMFVariableLocations()->size();
329 for(; mIt
!= mEnd
; ++mIt
)
331 std::fill( mIt
->second
.begin(),
335 mIt
->second
.resize(uiVarLocSize
, -1);