1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2013 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, carsten_neumann@gmx.net *
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 <boost/bind.hpp>
48 #include "OSGConfig.h"
53 #include "OSGWindow.h"
55 #include "OSGGLFuncProtos.h"
56 #include "OSGBaseFunctions.h"
58 #include "OSGDrawEnv.h"
61 #include "OSGMultiPropertySSBOChunk.h"
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 /***************************************************************************\
72 \***************************************************************************/
74 const UInt8
MultiPropertySSBOChunk::_numTypes
=
75 MultiPropertySSBOChunk::MAT4_T
+ 1;
77 typedef OSG::Window Win
;
79 /***************************************************************************\
81 \***************************************************************************/
83 void MultiPropertySSBOChunk::initMethod(InitPhase ePhase
)
85 Inherited::initMethod(ePhase
);
87 if(ePhase
== TypeObject::SystemPost
)
93 /***************************************************************************\
95 \***************************************************************************/
97 /*-------------------------------------------------------------------------*\
99 \*-------------------------------------------------------------------------*/
101 /*----------------------- constructors & destructors ----------------------*/
103 MultiPropertySSBOChunk::MultiPropertySSBOChunk(void) :
105 _bUpdateBuffer(false)
109 MultiPropertySSBOChunk::MultiPropertySSBOChunk(const MultiPropertySSBOChunk
&source
) :
111 _bUpdateBuffer(false)
115 MultiPropertySSBOChunk::~MultiPropertySSBOChunk(void)
119 /*----------------------------- class specific ----------------------------*/
121 void MultiPropertySSBOChunk::changed(ConstFieldMaskArg whichField
,
125 if((whichField
& (TypesFieldMask
|
126 CardinalityFieldMask
|
127 CardinalitiesFieldMask
|
128 NumPropertiesFieldMask
|
129 FloatValuesFieldMask
|
130 DoubleValuesFieldMask
|
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
)
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
];
194 ao
= alignOffset( 4, bo
); bo
= ao
+ sizeof(Real32
);
197 ao
= alignOffset( 8, bo
); bo
= ao
+ sizeof(Real64
);
200 ao
= alignOffset( 4, bo
); bo
= ao
+ sizeof(Int32
);
203 ao
= alignOffset( 4, bo
); bo
= ao
+ sizeof(UInt32
);
206 ao
= alignOffset( 4, bo
); bo
= ao
+ sizeof(Int32
);
209 ao
= alignOffset(16, bo
); bo
= ao
+ sizeof(Vec3f
);
212 ao
= alignOffset(16, bo
); bo
= ao
+ sizeof(Vec4f
);
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
;
225 ao
= alignOffset( 16, bo
); bo
= ao
; // padding
227 ao
*= getNumProperties(); bo
= ao
; // array
228 ao
= alignOffset( 16, bo
); bo
= ao
; // padding
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;
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
];
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
;
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
]);
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
)
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
,
374 Inherited::changeFrom(pEnv
, old
, idx
);
377 /*------------------------------ interface ----------------------------------*/
379 UInt32
MultiPropertySSBOChunk::addProperty()
381 if (!check_invariant())
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
);
397 card
= _mfCardinalities
[FLOAT_T
];
399 _mfFloatValues
.push_back(0.f
);
401 card
= _mfCardinalities
[DOUBLE_T
];
403 _mfDoubleValues
.push_back(0.0);
405 card
= _mfCardinalities
[INT_T
];
407 _mfIntValues
.push_back(0);
409 card
= _mfCardinalities
[UINT_T
];
411 _mfUIntValues
.push_back(0);
413 card
= _mfCardinalities
[BOOL_T
];
415 _mfBoolValues
.push_back(false);
417 card
= _mfCardinalities
[VEC3_T
];
419 _mfVec3Values
.push_back(Vec3f());
421 card
= _mfCardinalities
[VEC4_T
];
423 _mfVec4Values
.push_back(Vec4f());
425 card
= _mfCardinalities
[MAT4_T
];
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())
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
);
453 card
= _mfCardinalities
[FLOAT_T
];
454 i
= getNumProperties() * card
;
456 _mfFloatValues
.erase(i
);
458 card
= _mfCardinalities
[DOUBLE_T
];
459 i
= getNumProperties() * card
;
461 _mfDoubleValues
.erase(i
);
463 card
= _mfCardinalities
[INT_T
];
464 i
= getNumProperties() * card
;
466 _mfIntValues
.erase(i
);
468 card
= _mfCardinalities
[UINT_T
];
469 i
= getNumProperties() * card
;
471 _mfUIntValues
.erase(i
);
473 card
= _mfCardinalities
[BOOL_T
];
474 i
= getNumProperties() * card
;
476 _mfBoolValues
.erase(i
);
478 card
= _mfCardinalities
[VEC3_T
];
479 i
= getNumProperties() * card
;
481 _mfVec3Values
.erase(i
);
483 card
= _mfCardinalities
[VEC4_T
];
484 i
= getNumProperties() * card
;
486 _mfVec4Values
.erase(i
);
488 card
= _mfCardinalities
[MAT4_T
];
489 i
= getNumProperties() * card
;
491 _mfMatrixValues
.erase(i
);
493 UInt32 n
= getNumProperties();
494 setNumProperties(--n
);
496 OSG_ASSERT(check_invariant());
499 void MultiPropertySSBOChunk::clearProperties()
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()
526 editMField(CardinalitiesFieldMask
, _mfCardinalities
);
527 editMField(TypesFieldMask
, _mfTypes
);
528 editMField(CardinalityFieldMask
, _mfCardinality
);
530 _mfCardinalities
.clear();
532 _mfCardinality
.clear();
535 UInt32
MultiPropertySSBOChunk::addMember(Types type
, UInt32 cardinality
)
537 OSG_ASSERT(cardinality
> 0);
539 if (getNumProperties() > 0)
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
);