changed: gcc8 base update
[opensg.git] / Source / System / State / Base / OSGStateOverride.cpp
blob429fd7cb0b3b7ca548eb19da5a1bc29c479037ce
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 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 #include <cstdlib>
40 #include <cstdio>
42 #include "OSGConfig.h"
44 #include "OSGBaseFunctions.h"
45 #include "OSGFieldContainer.h"
46 #include "OSGStateOverride.h"
47 #include "OSGShaderProgramChunk.h"
48 #include "OSGShaderProgramVariableChunk.h"
49 #include "OSGSimpleSHLChunk.h"
51 OSG_USING_NAMESPACE
53 /*! \class OSG::StateOverride
56 /*-------------------------------------------------------------------------*/
57 /* Constructors */
59 StateOverride::StateOverride(void) :
60 _vChunks ( ),
61 _pShader (NULL),
62 _pShaderVar (NULL),
63 _vProgChunks ( ),
64 _vProgIds ( ),
65 _vProgVarChunks( ),
66 _vProgVarIds ( ),
67 _uiSortKey ( 0),
68 _uiKeyGen ( 0),
69 _uiKeyMask ( 0)
73 /*-------------------------------------------------------------------------*/
74 /* Destructor */
76 StateOverride::~StateOverride(void)
80 void StateOverride::rebuildSortKey(void)
82 OSG_ASSERT(false);
85 void StateOverride::addOverride(UInt32 uiSlot, ShaderProgramChunk *pChunk)
87 if(pChunk == NULL)
89 return;
92 ShaderProgramChunk::MFVertexShaderType::const_iterator sIt =
93 pChunk->getMFVertexShader()->begin();
95 ShaderProgramChunk::MFVertexShaderType::const_iterator sEnd =
96 pChunk->getMFVertexShader()->end();
99 for(; sIt != sEnd; ++sIt)
101 UInt16 uiProgId = (*sIt)->getProgramId();
103 IdStoreIt iIt = std::lower_bound(_vProgIds.begin(),
104 _vProgIds.end (),
105 uiProgId );
107 if(iIt == _vProgIds.end())
109 _vProgIds.push_back(uiProgId);
111 else if(*iIt != (*sIt)->getProgramId())
113 _vProgIds.insert(iIt, uiProgId);
118 sIt = pChunk->getMFGeometryShader()->begin();
119 sEnd = pChunk->getMFGeometryShader()->end ();
121 for(; sIt != sEnd; ++sIt)
123 UInt16 uiProgId = (*sIt)->getProgramId();
125 IdStoreIt iIt = std::lower_bound(_vProgIds.begin(),
126 _vProgIds.end (),
127 uiProgId );
129 if(iIt == _vProgIds.end())
131 _vProgIds.push_back(uiProgId);
133 else if(*iIt != (*sIt)->getProgramId())
135 _vProgIds.insert(iIt, uiProgId);
139 sIt = pChunk->getMFFragmentShader()->begin();
140 sEnd = pChunk->getMFFragmentShader()->end ();
142 for(; sIt != sEnd; ++sIt)
144 UInt16 uiProgId = (*sIt)->getProgramId();
146 IdStoreIt iIt = std::lower_bound(_vProgIds.begin(),
147 _vProgIds.end (),
148 uiProgId );
150 if(iIt == _vProgIds.end())
152 _vProgIds.push_back(uiProgId);
154 else if(*iIt != (*sIt)->getProgramId())
156 _vProgIds.insert(iIt, uiProgId);
160 _vProgChunks.push_back(pChunk);
162 _pShader = NULL;
165 void StateOverride::addOverride(UInt32 uiSlot,
166 ShaderProgramVariableChunk *pChunk)
168 if(pChunk == NULL)
170 return;
173 UInt16 uiVarId = pChunk->getVariableId();
175 IdStoreIt iIt = std::lower_bound(_vProgVarIds.begin(),
176 _vProgVarIds.end (),
177 uiVarId );
179 if(iIt == _vProgVarIds.end())
181 _vProgVarIds.push_back(uiVarId);
183 else if(*iIt != uiVarId)
185 _vProgVarIds.insert(iIt, uiVarId);
188 _vProgVarChunks.push_back(pChunk);
190 _pShaderVar = NULL;
193 void StateOverride::addOverride(UInt32 uiSlot,
194 SimpleSHLChunk *pChunk)
196 insertOverride(uiSlot, pChunk);
199 void StateOverride::insertOverride(UInt32 uiSlot,
200 StateChunk *pChunk)
202 ChunkElement newElem(uiSlot, pChunk);
204 ChunkStoreIt cIt = std::lower_bound(_vChunks.begin(),
205 _vChunks.end (),
206 newElem );
208 if(cIt == _vChunks.end())
210 _vChunks.insert(cIt, newElem);
212 else
214 if(cIt->first == uiSlot)
216 cIt->second = pChunk;
218 else
220 _vChunks.insert(cIt, newElem);
224 if(pChunk == NULL)
226 return;
229 UInt32 uiKey1 = _uiKeyGen & Key1Mask;
230 UInt32 uiKey2 = (_uiKeyGen & Key2Mask) >> 10;
231 UInt32 uiKey3 = (_uiKeyGen & Key3Mask) >> 20;
233 if(uiKey1 != InvalidKey && uiKey1 == uiSlot)
235 uiKey1 =
236 (pChunk->getIgnore() == false) ? pChunk->getChunkId() : 0;
238 _uiKeyMask &= ~Key1Mask;
240 else
242 uiKey1 = 0;
245 if(uiKey2 != InvalidKey && uiKey2 == uiSlot)
247 uiKey2 =
248 (pChunk->getIgnore() == false) ? pChunk->getChunkId() : 0;
250 _uiKeyMask &= ~Key2Mask;
252 else
254 uiKey2 = 0;
257 if(uiKey3 != InvalidKey && uiKey3 == uiSlot)
259 uiKey3 =
260 (pChunk->getIgnore() == false) ? pChunk->getChunkId() : 0;
262 _uiKeyMask &= ~Key3Mask;
264 else
266 uiKey3 = 0;
269 _uiSortKey =
270 (uiKey1 & Key1Mask) |
271 ((uiKey2 & Key1Mask) << 10) |
272 ((uiKey3 & Key1Mask) << 20);
275 void StateOverride::addOverride(UInt32 uiSlot, StateChunk *pChunk)
277 ShaderProgramChunk *pSPChunk = dynamic_cast<ShaderProgramChunk *>(pChunk);
279 if(pSPChunk == NULL)
281 ShaderProgramVariableChunk *pSPVChunk =
282 dynamic_cast<ShaderProgramVariableChunk *>(pChunk);
284 if(pSPVChunk == NULL)
286 insertOverride(uiSlot, pChunk);
288 else
290 this->addOverride(uiSlot, pSPVChunk);
293 else
295 this->addOverride(uiSlot, pSPChunk);
299 void StateOverride::subOverride(UInt32 uiSlot, ShaderProgramChunk *pChunk)
301 if(pChunk == NULL)
303 return;
306 ShaderProgramChunk::MFVertexShaderType::const_iterator sIt =
307 pChunk->getMFVertexShader()->begin();
309 ShaderProgramChunk::MFVertexShaderType::const_iterator sEnd =
310 pChunk->getMFVertexShader()->end();
313 for(; sIt != sEnd; ++sIt)
315 UInt16 uiProgId = (*sIt)->getProgramId();
317 std::pair<IdStoreIt, IdStoreIt> pairIt
318 = std::equal_range(_vProgIds.begin(),
319 _vProgIds.end (),
320 uiProgId );
322 if(pairIt.first != pairIt.second)
324 _vProgIds.erase(pairIt.first);
328 sIt = pChunk->getMFGeometryShader()->begin();
329 sEnd = pChunk->getMFGeometryShader()->end ();
331 for(; sIt != sEnd; ++sIt)
333 UInt16 uiProgId = (*sIt)->getProgramId();
335 std::pair<IdStoreIt, IdStoreIt> pairIt
336 = std::equal_range(_vProgIds.begin(),
337 _vProgIds.end (),
338 uiProgId );
340 if(pairIt.first != pairIt.second)
342 _vProgIds.erase(pairIt.first);
346 sIt = pChunk->getMFFragmentShader()->begin();
347 sEnd = pChunk->getMFFragmentShader()->end ();
349 for(; sIt != sEnd; ++sIt)
351 UInt16 uiProgId = (*sIt)->getProgramId();
353 std::pair<IdStoreIt, IdStoreIt> pairIt
354 = std::equal_range(_vProgIds.begin(),
355 _vProgIds.end (),
356 uiProgId );
358 if(pairIt.first != pairIt.second)
360 _vProgIds.erase(pairIt.first);
364 ProgramChunkStore::iterator sIt2
365 = std::find(_vProgChunks.begin(), _vProgChunks.end(), pChunk);
367 if(sIt2 != _vProgChunks.end())
368 _vProgChunks.erase(sIt2);
371 void StateOverride::subOverride(UInt32 uiSlot,
372 ShaderProgramVariableChunk *pChunk)
374 if(pChunk == NULL)
376 return;
379 UInt16 uiVarId = pChunk->getVariableId();
381 std::pair<IdStoreIt, IdStoreIt> pairIt
382 = std::equal_range(_vProgVarIds.begin(),
383 _vProgVarIds.end (),
384 uiVarId );
386 if(pairIt.first != pairIt.second)
388 _vProgVarIds.erase(pairIt.first);
391 ProgramVarChunkStore::iterator sIt2
392 = std::find(_vProgVarChunks.begin(), _vProgVarChunks.end(), pChunk);
394 if(sIt2 != _vProgVarChunks.end())
395 _vProgVarChunks.erase(sIt2);
398 void StateOverride::subOverride(UInt32 uiSlot,
399 SimpleSHLChunk *pChunk)
401 eraseOverride(uiSlot, pChunk);
404 void StateOverride::eraseOverride(UInt32 uiSlot,
405 StateChunk *pChunk)
407 ChunkElement newElem(uiSlot, pChunk);
409 std::pair<ChunkStoreIt, ChunkStoreIt> pairIt
410 = std::equal_range(_vChunks.begin(),
411 _vChunks.end (),
412 newElem );
414 for(; pairIt.first != pairIt.second; ++pairIt.first)
416 if(pairIt.first->first == uiSlot)
418 _vChunks.erase(pairIt.first);
419 break;
424 void StateOverride::subOverride(UInt32 uiSlot, StateChunk *pChunk)
426 ShaderProgramChunk *pSPChunk = dynamic_cast<ShaderProgramChunk *>(pChunk);
428 if(pSPChunk == NULL)
430 ShaderProgramVariableChunk *pSPVChunk =
431 dynamic_cast<ShaderProgramVariableChunk *>(pChunk);
433 if(pSPVChunk == NULL)
435 eraseOverride(uiSlot, pChunk);
437 else
439 this->subOverride(uiSlot, pSPVChunk);
442 else
444 this->subOverride(uiSlot, pSPChunk);