changed: gcc8 base update
[opensg.git] / Source / System / State / Shader / Chunks / OSGShaderExecutableVarChunk.cpp
blob04befc5aa9ac9cf86170d452c87e8a37f7db6a18
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 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGShaderExecutableVarChunk.h"
50 #include "OSGShaderVariables.h"
51 #include "OSGShaderProgramVariableChunk.h"
53 #include "OSGGLFuncProtos.h"
55 OSG_BEGIN_NAMESPACE
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 /***************************************************************************\
63 * Class variables *
64 \***************************************************************************/
66 StateChunkClass ShaderExecutableVarChunk::_class("ShaderExeVariable", 1, 8);
68 volatile UInt16 ShaderExecutableVarChunk::_uiChunkCounter = 1;
70 /***************************************************************************\
71 * Class methods *
72 \***************************************************************************/
74 void ShaderExecutableVarChunk::initMethod(InitPhase ePhase)
76 Inherited::initMethod(ePhase);
78 if(ePhase == TypeObject::SystemPost)
84 /***************************************************************************\
85 * Instance methods *
86 \***************************************************************************/
88 /*-------------------------------------------------------------------------*\
89 - private -
90 \*-------------------------------------------------------------------------*/
92 /*----------------------- constructors & destructors ----------------------*/
94 ShaderExecutableVarChunk::ShaderExecutableVarChunk(void) :
95 Inherited ( ),
96 #ifdef OSG_MULTISHADER_VARCHUNK
97 _mShaderVarLocs( ),
98 #endif
99 _uiChunkId (0)
103 ShaderExecutableVarChunk::ShaderExecutableVarChunk(
104 const ShaderExecutableVarChunk &source) :
106 Inherited (source),
107 #ifdef OSG_MULTISHADER_VARCHUNK
108 _mShaderVarLocs( ),
109 #endif
110 _uiChunkId ( 0)
114 ShaderExecutableVarChunk::~ShaderExecutableVarChunk(void)
118 void ShaderExecutableVarChunk::onCreate(const ShaderExecutableVarChunk *source)
120 Inherited::onCreate(source);
122 if(GlobalSystemState == Startup)
123 return;
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
139 return &_class;
142 UInt16 ShaderExecutableVarChunk::getChunkId(void)
144 return _uiChunkId;
147 void ShaderExecutableVarChunk::merge(ShaderProgramVariableChunk *pVar)
149 if(pVar == NULL)
150 return;
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(),
163 NULL);
166 editMField(ProgramVarChunksFieldMask, _mfProgramVarChunks);
168 _mfProgramVarChunks.push_back(pVar);
170 pVar->addParent(this, ProgramVarChunksFieldId);
173 /*----------------------------- class specific ----------------------------*/
175 void ShaderExecutableVarChunk::changed(ConstFieldMaskArg whichField,
176 UInt32 origin,
177 BitVector details)
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,
202 StateChunk *pOther,
203 UInt32 )
205 ShaderExecutableVarChunk *pOld =
206 dynamic_cast<ShaderExecutableVarChunk *>(pOther);
208 if(pOld == NULL)
210 FWARNING(( "ShaderExecutableVar::changeFrom: caught non-SHLChunk!\n"));
211 return;
214 // SHLChunk didn't change so do nothing.
215 #ifdef OSG_MULTISHADER_VARCHUNK
216 if(pOld == this && pEnv->getActiveShader() == this->getActiveShader())
217 #else
218 if(pOld == this)
219 #endif
221 return;
224 updateVariables(pEnv);
227 void ShaderExecutableVarChunk::deactivate(DrawEnv *, UInt32)
231 void ShaderExecutableVarChunk::updateVariables(DrawEnv *pEnv)
233 UInt32 uiProgram = pEnv->getActiveShader();
235 if(uiProgram == 0)
236 return;
238 #ifdef OSG_MULTISHADER_VARCHUNK
239 this->setActiveShader(uiProgram);
240 #endif
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)
253 return;
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,
265 MFInt32()));
267 mIt->second.resize(pVarLocations->size(), -1);
270 pVarLocations = &(mIt->second);
271 #endif
273 OSG_ASSERT(pMFVars->size() == pVarLocations->size());
275 MFInt32::iterator mLocIt = pVarLocations->begin();
277 ShaderProgramVariables::MFVariablesType::const_iterator mVarIt =
278 pMFVars->begin();
279 ShaderProgramVariables::MFVariablesType::const_iterator mVarEnd =
280 pMFVars->end ();
282 bool warnUnknown = ShaderVariable::WarnUnknown;
284 for(; mVarIt != mVarEnd; ++mVarIt, ++mLocIt)
286 ShaderVariable *pVar = *mVarIt;
288 if(pVar == NULL)
289 continue;
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);
304 setVariables(pVar);
306 else
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(),
320 NULL);
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(),
332 mIt->second.end (),
333 -1 );
335 mIt->second.resize(uiVarLocSize, -1);
337 #endif
340 OSG_END_NAMESPACE