1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#ifndef PX_PHYSICS_SCB_SHAPE
#define PX_PHYSICS_SCB_SHAPE
#include "NpMaterial.h"
#include "NpPhysics.h"
#include "ScbNpDeps.h"
#include "ScShapeCore.h"
#include "ScRigidCore.h"
#include "PsUtilities.h"
// PX_SERIALIZATION
#include "PxSerialFramework.h"
//~PX_SERIALIZATION
#include "ScbDefs.h"
namespace physx
{
#if PX_SUPPORT_PVD
#define UPDATE_PVD_MATERIALS() \
if(getControlState() == ControlState::eIN_SCENE) \
getScbScene()->getScenePvdClient().updateMaterials(this);
#else
#define UPDATE_PVD_MATERIALS() {}
#endif
namespace Scb
{
class RigidObject;
struct ShapeBuffer
{
template <PxU32 I, PxU32 dummy> struct Fns {}; // TODO: make the base class traits visible
typedef Sc::ShapeCore Core;
typedef ShapeBuffer Buf;
ShapeBuffer() : materialBufferIndex(0), materialCount(0) {}
// PT: I think we start with "2" (instead of 0) because the two first bits are reserved
// below, for geometry & materials.
SCB_REGULAR_ATTRIBUTE_ALIGNED(2, PxTransform, Shape2Actor, 16)
SCB_REGULAR_ATTRIBUTE(3, PxFilterData, SimulationFilterData)
SCB_REGULAR_ATTRIBUTE(4, PxReal, ContactOffset)
SCB_REGULAR_ATTRIBUTE(5, PxReal, RestOffset)
SCB_REGULAR_ATTRIBUTE(6, PxShapeFlags, Flags)
Gu::GeometryUnion geometry;
union
{
PxU16 materialIndex; // for single material shapes
PxU32 materialBufferIndex; // for multi material shapes
};
PxU16 materialCount;
enum
{
BF_Geometry = 1<<0,
BF_Material = 1<<1
};
};
class Shape : public Base
{
//= ATTENTION! =====================================================================================
// Changing the data layout of this class breaks the binary serialization format. See comments for
// PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
// function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
// accordingly.
//==================================================================================================
typedef Sc::ShapeCore Core;
typedef ShapeBuffer Buf;
public:
// PX_SERIALIZATION
Shape(const PxEMPTY) : Base(PxEmpty), mShape(PxEmpty) {}
static void getBinaryMetaData(PxOutputStream& stream);
//~PX_SERIALIZATION
PX_INLINE Shape(const PxGeometry& geometry,
PxShapeFlags shapeFlags,
const PxU16* materialIndices,
PxU16 materialCount,
bool isExclusive);
PX_INLINE PxGeometryType::Enum getGeometryType() const;
PX_INLINE const PxGeometry& getGeometry() const;
PX_INLINE const Gu::GeometryUnion&getGeometryUnion() const;
PX_INLINE Scb::ShapeBuffer* setGeometry(const PxGeometry& geom);
PX_INLINE PxU16 getNbMaterials() const;
PX_INLINE PxMaterial* getMaterial(PxU32 index) const;
PX_INLINE PxU32 getMaterials(PxMaterial** buffer, PxU32 bufferSize, PxU32 startIndex=0) const;
PX_INLINE bool setMaterials(PxMaterial*const* materials, PxU16 materialCount);
PX_INLINE const PxTransform& getShape2Actor() const { return read<Buf::BF_Shape2Actor>(); }
PX_INLINE void setShape2Actor(const PxTransform& v) { write<Buf::BF_Shape2Actor>(v); }
PX_INLINE PxFilterData getSimulationFilterData() const { return read<Buf::BF_SimulationFilterData>(); }
PX_INLINE void setSimulationFilterData(const PxFilterData& v) { write<Buf::BF_SimulationFilterData>(v); }
PX_INLINE PxReal getContactOffset() const { return read<Buf::BF_ContactOffset>(); }
PX_INLINE void setContactOffset(PxReal v) { write<Buf::BF_ContactOffset>(v); }
PX_INLINE PxReal getRestOffset() const { return read<Buf::BF_RestOffset>(); }
PX_INLINE void setRestOffset(PxReal v) { write<Buf::BF_RestOffset>(v); }
PX_INLINE PxShapeFlags getFlags() const { return read<Buf::BF_Flags>(); }
PX_INLINE void setFlags(PxShapeFlags v) { write<Buf::BF_Flags>(v); }
//---------------------------------------------------------------------------------
// Data synchronization
//---------------------------------------------------------------------------------
void syncState();
//---------------------------------------------------------------------------------
// Miscellaneous
//---------------------------------------------------------------------------------
PX_FORCE_INLINE const PxU16* getScMaterialIndices() const { return mShape.getMaterialIndices(); } // Only use if you know what you're doing!
PX_FORCE_INLINE Sc::ShapeCore& getScShape() { return mShape; } // Only use if you know what you're doing!
PX_FORCE_INLINE const Sc::ShapeCore& getScShape() const { return mShape; }
PX_FORCE_INLINE bool isExclusive() const { return getScbType() == ScbType::eSHAPE_EXCLUSIVE; }
PX_FORCE_INLINE void setControlStateIfExclusive(Scene* s, ControlState::Enum cs); // for exclusive shapes
template<bool sync> PX_FORCE_INLINE void checkUpdateOnRemove(Scene* s);
static size_t getScOffset() { return reinterpret_cast<size_t>(&reinterpret_cast<Shape*>(0)->mShape); }
private:
bool setMaterialsHelper(PxMaterial* const* materials, PxU16 materialCount);
Sc::ShapeCore mShape;
PX_FORCE_INLINE const Scb::ShapeBuffer* getBufferedData() const { return reinterpret_cast<const Scb::ShapeBuffer*>(getStream()); }
PX_FORCE_INLINE Scb::ShapeBuffer* getBufferedData() { return reinterpret_cast<Scb::ShapeBuffer*>(getStream()); }
PX_FORCE_INLINE const PxU16* getMaterialBuffer(const Scb::Scene& scene, const Scb::ShapeBuffer& sb) const
{
if(sb.materialCount == 1)
return &sb.materialIndex;
else
return scene.getShapeMaterialBuffer(sb.materialBufferIndex);
}
//---------------------------------------------------------------------------------
// Infrastructure for regular attributes
//---------------------------------------------------------------------------------
struct Access: public BufferedAccess<Buf, Core, Shape>
{
template<typename Fns>
static PX_FORCE_INLINE void write(Shape& base, Core& core, typename Fns::Arg v)
{
if(!base.isBuffering())
{
PxShapeFlags oldShapeFlags = core.getFlags();
Fns::setCore(core, v);
// shared shapes return NULL. But shared shapes aren't mutable when attached to an actor, so no notification needed.
Sc::RigidCore* rigidCore = NpShapeGetScRigidObjectFromScbSLOW(base);
if(rigidCore && base.getControlState() != ControlState::eINSERT_PENDING)
rigidCore->onShapeChange(core, Sc::ShapeChangeNotifyFlags(Fns::flag), oldShapeFlags);
#if PX_SUPPORT_PVD
Scb::Scene* scene = base.getScbSceneForAPI(); // shared shapes also return zero here
if(scene && !base.insertPending())
scene->getScenePvdClient().updatePvdProperties(&base);
#endif
}
else
{
Fns::setBuffered(*reinterpret_cast<Buf*>(base.getStream()), v);
base.markUpdated(Fns::flag);
}
}
};
template<PxU32 f> PX_FORCE_INLINE typename Buf::Fns<f,0>::Arg read() const { return Access::read<Buf::Fns<f,0> >(*this, mShape); }
template<PxU32 f> PX_FORCE_INLINE void write(typename Buf::Fns<f,0>::Arg v) { Access::write<Buf::Fns<f,0> >(*this, mShape, v); }
template<PxU32 f> PX_FORCE_INLINE void flush(const Buf& buf) { Access::flush<Buf::Fns<f,0> >(*this, mShape, buf); }
};
PX_INLINE Shape::Shape(const PxGeometry& geometry,
PxShapeFlags shapeFlags,
const PxU16* materialIndices,
PxU16 materialCount,
bool isExclusive) :
mShape(geometry, shapeFlags, materialIndices, materialCount)
{
// paranoia: the notify flags in Sc have to match up
PX_COMPILE_TIME_ASSERT(PxU32(ShapeBuffer::BF_Geometry) == PxU32(Sc::ShapeChangeNotifyFlag::eGEOMETRY));
PX_COMPILE_TIME_ASSERT(PxU32(ShapeBuffer::BF_Material) == PxU32(Sc::ShapeChangeNotifyFlag::eMATERIAL));
PX_COMPILE_TIME_ASSERT(PxU32(ShapeBuffer::BF_Shape2Actor) == PxU32(Sc::ShapeChangeNotifyFlag::eSHAPE2BODY));
PX_COMPILE_TIME_ASSERT(PxU32(ShapeBuffer::BF_SimulationFilterData) == PxU32(Sc::ShapeChangeNotifyFlag::eFILTERDATA));
PX_COMPILE_TIME_ASSERT(PxU32(ShapeBuffer::BF_ContactOffset) == PxU32(Sc::ShapeChangeNotifyFlag::eCONTACTOFFSET));
PX_COMPILE_TIME_ASSERT(PxU32(ShapeBuffer::BF_RestOffset) == PxU32(Sc::ShapeChangeNotifyFlag::eRESTOFFSET));
PX_COMPILE_TIME_ASSERT(PxU32(ShapeBuffer::BF_Flags) == PxU32(Sc::ShapeChangeNotifyFlag::eFLAGS));
if(isExclusive)
setScbType(ScbType::eSHAPE_EXCLUSIVE);
else
setScbType(ScbType::eSHAPE_SHARED);
}
PX_INLINE PxGeometryType::Enum Shape::getGeometryType() const
{
return mShape.getGeometryType();
}
PX_INLINE const PxGeometry& Shape::getGeometry() const
{
if(isBuffered(Buf::BF_Geometry))
return getBufferedData()->geometry.getGeometry();
else
return mShape.getGeometry();
}
PX_INLINE const Gu::GeometryUnion& Shape::getGeometryUnion() const
{
if(isBuffered(Buf::BF_Geometry))
return getBufferedData()->geometry;
else
return mShape.getGeometryUnion();
}
PX_INLINE Scb::ShapeBuffer* Shape::setGeometry(const PxGeometry& geom)
{
Scb::ShapeBuffer* shapeBuffer = NULL;
if (!isBuffering())
{
Scb::Scene* sc = getScbScene();
if(sc)
sc->getScScene().unregisterShapeFromNphase(mShape);
mShape.setGeometry(geom);
if(sc)
sc->getScScene().registerShapeInNphase(mShape);
Sc::RigidCore* rigidCore = NpShapeGetScRigidObjectFromScbSLOW(*this);
if(rigidCore)
rigidCore->onShapeChange(mShape, Sc::ShapeChangeNotifyFlag::eGEOMETRY, PxShapeFlags());
#if PX_SUPPORT_PVD
Scb::Scene* scbScene = getScbSceneForAPI();
if(scbScene)
scbScene->getScenePvdClient().releaseAndRecreateGeometry(this);
#endif
}
else
{
markUpdated(Buf::BF_Geometry);
shapeBuffer = getBufferedData();
shapeBuffer->geometry.set(geom);
}
return shapeBuffer;
}
PX_INLINE PxU16 Shape::getNbMaterials() const
{
if(isBuffered(Buf::BF_Material))
return getBufferedData()->materialCount;
else
return mShape.getNbMaterialIndices();
}
PX_INLINE PxMaterial* Shape::getMaterial(PxU32 index) const
{
PX_ASSERT(index < getNbMaterials());
NpMaterialManager& matManager = NpPhysics::getInstance().getMaterialManager();
if(isBuffered(Buf::BF_Material))
{
const PxU16* materialIndices = getMaterialBuffer(*getScbScene(), *getBufferedData());
return matManager.getMaterial(materialIndices[index]);
}
else
{
PxU16 matTableIndex = mShape.getMaterialIndices()[index];
return matManager.getMaterial(matTableIndex);
}
}
PX_INLINE PxU32 Shape::getMaterials(PxMaterial** buffer, PxU32 bufferSize, PxU32 startIndex) const
{
const PxU16* materialIndices;
PxU32 matCount;
NpMaterialManager& matManager = NpPhysics::getInstance().getMaterialManager();
if(isBuffered(Buf::BF_Material))
{
// IMPORTANT:
// As long as the material pointers get copied to a user buffer, this works fine.
// Never give direct access to the internal material buffer because in the
// double buffered case the pointer changes on resize.
const Scb::ShapeBuffer* PX_RESTRICT bufferedData = getBufferedData();
materialIndices = getMaterialBuffer(*getScbScene(), *bufferedData);
matCount = bufferedData->materialCount;
}
else
{
materialIndices = mShape.getMaterialIndices();
matCount = mShape.getNbMaterialIndices();
}
// PT: this is copied from Cm::getArrayOfPointers(). We cannot use the Cm function here
// because of the extra indirection needed to access the materials.
PxU32 size = matCount;
const PxU32 remainder = PxU32(PxMax<PxI32>(PxI32(size - startIndex), 0));
const PxU32 writeCount = PxMin(remainder, bufferSize);
materialIndices += startIndex;
for(PxU32 i=0;i<writeCount;i++)
buffer[i] = matManager.getMaterial(materialIndices[i]);
return writeCount;
}
PX_INLINE bool Shape::setMaterials(PxMaterial* const* materials, PxU16 materialCount)
{
if(!isBuffering())
{
bool ret = setMaterialsHelper(materials, materialCount);
UPDATE_PVD_MATERIALS()
return ret;
}
else
{
Scb::ShapeBuffer* PX_RESTRICT bufferedData = getBufferedData();
PxU16* materialIndices;
if(materialCount == 1)
materialIndices = &bufferedData->materialIndex;
else
{
PxU32 bufferIdx;
materialIndices = getScbScene()->allocShapeMaterialBuffer(materialCount, bufferIdx);
bufferedData->materialBufferIndex = bufferIdx;
}
bufferedData->materialCount = materialCount;
NpMaterial::getMaterialIndices(materials, materialIndices, materialCount);
markUpdated(Buf::BF_Material);
return true;
}
}
PX_FORCE_INLINE void Shape::setControlStateIfExclusive(Scene* s, ControlState::Enum cs)
{
if(isExclusive())
{
setControlState(cs);
setScbScene(s);
}
}
template<bool sync>
PX_FORCE_INLINE void Shape::checkUpdateOnRemove(Scene* s)
{
// special code to cover the case where a shape has a pending update and gets released. The following operations have to be done
// before the ref-counter of the shape gets decremented because that could cause the shape to be deleted in which case it must not
// be in the pending update list any longer.
if(getControlFlags() & Scb::ControlFlag::eIS_UPDATED)
{
if(sync)
syncState();
s->removeShapeFromPendingUpdateList(*this);
resetControlFlag(ControlFlag::eIS_UPDATED);
}
}
} // namespace Scb
}
#endif
|