changed: gcc8 base update
[opensg.git] / Source / System / State / Base / OSGMultiPropertySSBOChunk.cpp
blob630f94e48fec2c0fc3cd889585026dfae8ca00c6
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2013 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, carsten_neumann@gmx.net *
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 <boost/bind.hpp>
48 #include "OSGConfig.h"
50 #include "OSGGL.h"
51 #include "OSGGLU.h"
52 #include "OSGGLEXT.h"
53 #include "OSGWindow.h"
55 #include "OSGGLFuncProtos.h"
56 #include "OSGBaseFunctions.h"
58 #include "OSGDrawEnv.h"
59 #include "OSGNode.h"
61 #include "OSGMultiPropertySSBOChunk.h"
63 OSG_BEGIN_NAMESPACE
65 // Documentation for this class is emitted in the
66 // OSGMultiPropertySSBOChunkBase.cpp file.
67 // To modify it, please change the .fcd file (OSGMultiPropertySSBOChunk.fcd) and
68 // regenerate the base file.
70 /***************************************************************************\
71 * Class variables *
72 \***************************************************************************/
74 const UInt8 MultiPropertySSBOChunk::_numTypes =
75 MultiPropertySSBOChunk::MAT4_T + 1;
77 typedef OSG::Window Win;
79 /***************************************************************************\
80 * Class methods *
81 \***************************************************************************/
83 void MultiPropertySSBOChunk::initMethod(InitPhase ePhase)
85 Inherited::initMethod(ePhase);
87 if(ePhase == TypeObject::SystemPost)
93 /***************************************************************************\
94 * Instance methods *
95 \***************************************************************************/
97 /*-------------------------------------------------------------------------*\
98 - private -
99 \*-------------------------------------------------------------------------*/
101 /*----------------------- constructors & destructors ----------------------*/
103 MultiPropertySSBOChunk::MultiPropertySSBOChunk(void) :
104 Inherited(),
105 _bUpdateBuffer(false)
109 MultiPropertySSBOChunk::MultiPropertySSBOChunk(const MultiPropertySSBOChunk &source) :
110 Inherited(source),
111 _bUpdateBuffer(false)
115 MultiPropertySSBOChunk::~MultiPropertySSBOChunk(void)
119 /*----------------------------- class specific ----------------------------*/
121 void MultiPropertySSBOChunk::changed(ConstFieldMaskArg whichField,
122 UInt32 origin,
123 BitVector details)
125 if((whichField & (TypesFieldMask |
126 CardinalityFieldMask |
127 CardinalitiesFieldMask |
128 NumPropertiesFieldMask |
129 FloatValuesFieldMask |
130 DoubleValuesFieldMask |
131 IntValuesFieldMask |
132 UIntValuesFieldMask |
133 BoolValuesFieldMask |
134 Vec3ValuesFieldMask |
135 Vec4ValuesFieldMask |
136 MatrixValuesFieldMask)) != 0)
138 _bUpdateBuffer = true;
141 Inherited::changed(whichField, origin, details);
144 /*----------------------------- onCreate --------------------------------*/
146 void MultiPropertySSBOChunk::onCreate(const MultiPropertySSBOChunk *source)
148 Inherited::onCreate(source);
150 if(GlobalSystemState == Startup)
151 return;
154 void MultiPropertySSBOChunk::onCreateAspect(
155 const MultiPropertySSBOChunk *createAspect,
156 const MultiPropertySSBOChunk *source )
158 Inherited::onCreateAspect(createAspect, source);
161 void MultiPropertySSBOChunk::onDestroy(UInt32 uiContainerId)
163 Inherited::onDestroy(uiContainerId);
166 /*------------------------------ Output -------------------------------------*/
167 void MultiPropertySSBOChunk::dump( UInt32 ,
168 const BitVector ) const
170 SLOG << "Dump MultiPropertySSBOChunk NI" << std::endl;
173 /*------------------------------ buffer -------------------------------------*/
175 std::size_t MultiPropertySSBOChunk::calc_property_buffer_size() const
177 std::size_t ao = 0; // aligned offset
178 std::size_t bo = 0; // base offset
180 OSG_ASSERT(check_invariant());
182 std::size_t sz = _mfTypes.size();
184 for (std::size_t i = 0; i < sz; ++i)
186 UInt32 card = _mfCardinality[i];
187 UInt8 e = _mfTypes[i];
189 while (card-- > 0)
191 switch (e)
193 case FLOAT_T:
194 ao = alignOffset( 4, bo); bo = ao + sizeof(Real32);
195 break;
196 case DOUBLE_T:
197 ao = alignOffset( 8, bo); bo = ao + sizeof(Real64);
198 break;
199 case INT_T:
200 ao = alignOffset( 4, bo); bo = ao + sizeof(Int32);
201 break;
202 case UINT_T:
203 ao = alignOffset( 4, bo); bo = ao + sizeof(UInt32);
204 break;
205 case BOOL_T:
206 ao = alignOffset( 4, bo); bo = ao + sizeof(Int32);
207 break;
208 case VEC3_T:
209 ao = alignOffset(16, bo); bo = ao + sizeof(Vec3f);
210 break;
211 case VEC4_T:
212 ao = alignOffset(16, bo); bo = ao + sizeof(Vec4f);
213 break;
214 case MAT4_T:
215 ao = alignOffset(16, bo); bo = ao + sizeof(Vec4f);
216 ao = alignOffset(16, bo); bo = ao + sizeof(Vec4f);
217 ao = alignOffset(16, bo); bo = ao + sizeof(Vec4f);
218 ao = alignOffset(16, bo); bo = ao + sizeof(Vec4f);
219 ao = alignOffset(16, bo); bo = ao;
220 break;
225 ao = alignOffset( 16, bo); bo = ao; // padding
227 ao *= getNumProperties(); bo = ao; // array
228 ao = alignOffset( 16, bo); bo = ao; // padding
230 return ao;
233 std::vector<UInt8> MultiPropertySSBOChunk::create_property_buffer(DrawEnv* pEnv) const
235 std::size_t size = calc_property_buffer_size();
237 std::vector<UInt8> buffer(size);
239 UInt32 num_properties = getNumProperties();
241 std::size_t ao = 0; // aligned offset
242 std::size_t bo = 0; // base offset
244 for (UInt32 idx = 0; idx < num_properties; ++idx)
246 std::size_t sz = _mfTypes.size();
248 UInt32 card_float = 0;
249 UInt32 card_double = 0;
250 UInt32 card_int = 0;
251 UInt32 card_uint = 0;
252 UInt32 card_bool = 0;
253 UInt32 card_vec3 = 0;
254 UInt32 card_vec4 = 0;
255 UInt32 card_mat4 = 0;
257 for (std::size_t i = 0; i < sz; ++i)
259 UInt32 card = _mfCardinality[i];
260 UInt8 e = _mfTypes[i];
261 UInt32 j = 0;
263 while (card-- > 0)
265 switch (e)
267 case FLOAT_T:
268 j = idx * _mfCardinalities[FLOAT_T] + card_float;
269 ao = alignOffset( 4, bo); *(reinterpret_cast<Real32*>(&buffer[0] + ao)) = getFloatValues(j); bo = ao + sizeof(Real32);
270 card_float++;
271 break;
273 case DOUBLE_T:
274 j = idx * _mfCardinalities[DOUBLE_T] + card_double;
275 ao = alignOffset( 8, bo); *(reinterpret_cast<Real64*>(&buffer[0] + ao)) = getDoubleValues(j); bo = ao + sizeof(Real64);
276 card_double++;
277 break;
279 case INT_T:
280 j = idx * _mfCardinalities[INT_T] + card_int;
281 ao = alignOffset( 4, bo); *(reinterpret_cast<Int32*>(&buffer[0] + ao)) = getIntValues(j); bo = ao + sizeof(Int32);
282 card_int++;
283 break;
285 case UINT_T:
286 j = idx * _mfCardinalities[UINT_T] + card_uint;
287 ao = alignOffset( 4, bo); *(reinterpret_cast<UInt32*>(&buffer[0] + ao)) = getUIntValues(j); bo = ao + sizeof(UInt32);
288 card_uint++;
289 break;
291 case BOOL_T:
292 j = idx * _mfCardinalities[BOOL_T] + card_bool;
293 ao = alignOffset( 4, bo); *(reinterpret_cast<bool*>(&buffer[0] + ao)) = getBoolValues(j); bo = ao + sizeof(Int32);
294 card_bool++;
295 break;
297 case VEC3_T:
298 j = idx * _mfCardinalities[VEC3_T] + card_vec3;
299 ao = alignOffset(16, bo); memcpy(&buffer[0] + ao, &getVec3Values(j)[0], sizeof(Vec3f)); bo = ao + sizeof(Vec3f);
300 card_vec3++;
301 break;
303 case VEC4_T:
304 j = idx * _mfCardinalities[VEC4_T] + card_vec4;
305 ao = alignOffset(16, bo); memcpy(&buffer[0] + ao, &getVec4Values(j)[0], sizeof(Vec4f)); bo = ao + sizeof(Vec4f);
306 card_vec4++;
307 break;
309 case MAT4_T:
310 j = idx * _mfCardinalities[MAT4_T] + card_mat4;
311 ao = alignOffset(16, bo); memcpy(&buffer[0] + ao, &getMatrixValues(j)[0][0], sizeof(Vec4f)); bo = ao + sizeof(Vec4f);
312 ao = alignOffset(16, bo); memcpy(&buffer[0] + ao, &getMatrixValues(j)[1][0], sizeof(Vec4f)); bo = ao + sizeof(Vec4f);
313 ao = alignOffset(16, bo); memcpy(&buffer[0] + ao, &getMatrixValues(j)[2][0], sizeof(Vec4f)); bo = ao + sizeof(Vec4f);
314 ao = alignOffset(16, bo); memcpy(&buffer[0] + ao, &getMatrixValues(j)[3][0], sizeof(Vec4f)); bo = ao + sizeof(Vec4f);
315 ao = alignOffset(16, bo); bo = ao;
316 card_mat4++;
317 break;
322 ao = alignOffset( 16, bo); bo = ao; // padding
324 OSG_ASSERT(card_float == _mfCardinalities[FLOAT_T]);
325 OSG_ASSERT(card_double == _mfCardinalities[DOUBLE_T]);
326 OSG_ASSERT(card_int == _mfCardinalities[INT_T]);
327 OSG_ASSERT(card_uint == _mfCardinalities[UINT_T]);
328 OSG_ASSERT(card_bool == _mfCardinalities[BOOL_T]);
329 OSG_ASSERT(card_vec3 == _mfCardinalities[VEC3_T]);
330 OSG_ASSERT(card_vec4 == _mfCardinalities[VEC4_T]);
331 OSG_ASSERT(card_mat4 == _mfCardinalities[MAT4_T]);
334 return buffer;
337 void MultiPropertySSBOChunk::create_property_state(DrawEnv* pEnv)
339 std::vector<UInt8> buffer = create_property_buffer(pEnv);
340 editMFBuffer()->setValues(buffer);
343 void MultiPropertySSBOChunk::update_property_state(DrawEnv* pEnv)
345 std::vector<UInt8> buffer = create_property_buffer(pEnv);
346 editMFBuffer()->setValues(buffer);
348 _bUpdateBuffer = false;
351 /*------------------------------ activate -----------------------------------*/
353 void MultiPropertySSBOChunk::activate(DrawEnv *pEnv, UInt32 idx)
355 if (_bUpdateBuffer)
356 update_property_state(pEnv);
358 Inherited::activate(pEnv, idx);
361 /*------------------------------ deactivate ---------------------------------*/
363 void MultiPropertySSBOChunk::deactivate(DrawEnv *pEnv, UInt32 idx)
365 Inherited::deactivate(pEnv, idx);
368 /*------------------------------ changeFrom ---------------------------------*/
370 void MultiPropertySSBOChunk::changeFrom(DrawEnv *pEnv,
371 StateChunk *old,
372 UInt32 idx )
374 Inherited::changeFrom(pEnv, old, idx);
377 /*------------------------------ interface ----------------------------------*/
379 UInt32 MultiPropertySSBOChunk::addProperty()
381 if (!check_invariant())
382 clearProperties();
384 OSG_ASSERT(check_invariant());
386 editMField(FloatValuesFieldMask, _mfFloatValues);
387 editMField(DoubleValuesFieldMask, _mfDoubleValues);
388 editMField(IntValuesFieldMask, _mfIntValues);
389 editMField(UIntValuesFieldMask, _mfUIntValues);
390 editMField(BoolValuesFieldMask, _mfBoolValues);
391 editMField(Vec3ValuesFieldMask, _mfVec3Values);
392 editMField(Vec4ValuesFieldMask, _mfVec4Values);
393 editMField(MatrixValuesFieldMask, _mfMatrixValues);
395 UInt32 card;
397 card = _mfCardinalities[FLOAT_T];
398 while (card-- > 0)
399 _mfFloatValues.push_back(0.f);
401 card = _mfCardinalities[DOUBLE_T];
402 while (card-- > 0)
403 _mfDoubleValues.push_back(0.0);
405 card = _mfCardinalities[INT_T];
406 while (card-- > 0)
407 _mfIntValues.push_back(0);
409 card = _mfCardinalities[UINT_T];
410 while (card-- > 0)
411 _mfUIntValues.push_back(0);
413 card = _mfCardinalities[BOOL_T];
414 while (card-- > 0)
415 _mfBoolValues.push_back(false);
417 card = _mfCardinalities[VEC3_T];
418 while (card-- > 0)
419 _mfVec3Values.push_back(Vec3f());
421 card = _mfCardinalities[VEC4_T];
422 while (card-- > 0)
423 _mfVec4Values.push_back(Vec4f());
425 card = _mfCardinalities[MAT4_T];
426 while (card-- > 0)
427 _mfMatrixValues.push_back(Matrix());
429 UInt32 n = getNumProperties();
430 setNumProperties(++n);
432 return UInt32(getNumProperties() - 1);
435 void MultiPropertySSBOChunk::removeProperty(const UInt32 idx)
437 OSG_ASSERT(check_invariant());
439 if (idx >= getNumProperties())
440 return;
442 editMField(FloatValuesFieldMask, _mfFloatValues);
443 editMField(DoubleValuesFieldMask, _mfDoubleValues);
444 editMField(IntValuesFieldMask, _mfIntValues);
445 editMField(UIntValuesFieldMask, _mfUIntValues);
446 editMField(BoolValuesFieldMask, _mfBoolValues);
447 editMField(Vec3ValuesFieldMask, _mfVec3Values);
448 editMField(Vec4ValuesFieldMask, _mfVec4Values);
449 editMField(MatrixValuesFieldMask, _mfMatrixValues);
451 UInt32 card, i;
453 card = _mfCardinalities[FLOAT_T];
454 i = getNumProperties() * card;
455 while (card-- > 0)
456 _mfFloatValues.erase(i);
458 card = _mfCardinalities[DOUBLE_T];
459 i = getNumProperties() * card;
460 while (card-- > 0)
461 _mfDoubleValues.erase(i);
463 card = _mfCardinalities[INT_T];
464 i = getNumProperties() * card;
465 while (card-- > 0)
466 _mfIntValues.erase(i);
468 card = _mfCardinalities[UINT_T];
469 i = getNumProperties() * card;
470 while (card-- > 0)
471 _mfUIntValues.erase(i);
473 card = _mfCardinalities[BOOL_T];
474 i = getNumProperties() * card;
475 while (card-- > 0)
476 _mfBoolValues.erase(i);
478 card = _mfCardinalities[VEC3_T];
479 i = getNumProperties() * card;
480 while (card-- > 0)
481 _mfVec3Values.erase(i);
483 card = _mfCardinalities[VEC4_T];
484 i = getNumProperties() * card;
485 while (card-- > 0)
486 _mfVec4Values.erase(i);
488 card = _mfCardinalities[MAT4_T];
489 i = getNumProperties() * card;
490 while (card-- > 0)
491 _mfMatrixValues.erase(i);
493 UInt32 n = getNumProperties();
494 setNumProperties(--n);
496 OSG_ASSERT(check_invariant());
499 void MultiPropertySSBOChunk::clearProperties()
501 setNumProperties(0);
503 editMField(FloatValuesFieldMask, _mfFloatValues);
504 editMField(DoubleValuesFieldMask, _mfDoubleValues);
505 editMField(IntValuesFieldMask, _mfIntValues);
506 editMField(UIntValuesFieldMask, _mfUIntValues);
507 editMField(BoolValuesFieldMask, _mfBoolValues);
508 editMField(Vec3ValuesFieldMask, _mfVec3Values);
509 editMField(Vec4ValuesFieldMask, _mfVec4Values);
510 editMField(MatrixValuesFieldMask, _mfMatrixValues);
512 _mfFloatValues.clear();
513 _mfDoubleValues.clear();
514 _mfIntValues.clear();
515 _mfUIntValues.clear();
516 _mfBoolValues.clear();
517 _mfVec3Values.clear();
518 _mfVec4Values.clear();
519 _mfMatrixValues.clear();
522 void MultiPropertySSBOChunk::clearMembers()
524 clearProperties();
526 editMField(CardinalitiesFieldMask, _mfCardinalities);
527 editMField(TypesFieldMask, _mfTypes);
528 editMField(CardinalityFieldMask, _mfCardinality);
530 _mfCardinalities.clear();
531 _mfTypes.clear();
532 _mfCardinality.clear();
535 UInt32 MultiPropertySSBOChunk::addMember(Types type, UInt32 cardinality)
537 OSG_ASSERT(cardinality > 0);
539 if (getNumProperties() > 0)
540 clearProperties();
542 editMField(CardinalitiesFieldMask, _mfCardinalities);
544 if (_mfCardinalities.empty())
546 _mfCardinalities.resize(_numTypes);
549 UInt32 tag = _mfCardinalities[type] + 1;
551 _mfCardinalities[type] += cardinality;
553 editMField(TypesFieldMask, _mfTypes);
554 editMField(CardinalityFieldMask, _mfCardinality);
556 _mfTypes.push_back(type);
557 _mfCardinality.push_back(cardinality);
559 return tag;
562 OSG_END_NAMESPACE