Skip to content

Commit 05e5771

Browse files
committed
- dkphysics: jolt shape cache, fix physics obj destroy
1 parent 2abb544 commit 05e5771

7 files changed

Lines changed: 187 additions & 9 deletions

File tree

shared/dkphysics/DkJoltPCH.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
#define JPH_INSBYTE_NO_VEHICLES
1313
#define JPH_INSBYTE_NO_SOFTBODY
1414

15-
#include <Jolt/Jolt.h>
15+
#include <Jolt/Jolt.h>
16+
#include "DkJoltConvert.h"
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//////////////////////////////////////////////////////////////////////////////////
2+
// Copyright (C) Inspiration Byte
3+
// 2009-2020
4+
//////////////////////////////////////////////////////////////////////////////////
5+
// Description: Physics model cache for bullet physics
6+
// Generates real shapes for Bullet Collision
7+
///////////////////////////////////////////////////////////////////////////////////
8+
9+
#include "core/core_common.h"
10+
#include "core/ConVar.h"
11+
12+
#include "DkJoltPCH.h"
13+
#include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
14+
#include <Jolt/Physics/Collision/Shape/MeshShape.h>
15+
16+
#include "DkJoltShapeCache.h"
17+
#include "egf/physmodel.h"
18+
19+
static Threading::CEqMutex s_shapeCacheMutex;
20+
21+
DECLARE_CVAR(ph_studioShapeMargin, "0.05", "Studio model shape marginal", CV_CHEAT); // same as JPH::cDefaultConvexRadius
22+
23+
// makes and caches shape. IsConvex defines that it was convex or not (also for internal use)
24+
static JPH::Ref<JPH::Shape> jphGenerateCollisionShape(ArrayCRef<Vector3D> vertices, ArrayCRef<int> indices, EPhysShapeType type)
25+
{
26+
const float margin = ph_studioShapeMargin.GetFloat();
27+
28+
switch(type)
29+
{
30+
case PHYSSHAPE_TYPE_CONCAVE:
31+
case PHYSSHAPE_TYPE_MOVABLECONCAVE:
32+
{
33+
JPH::VertexList jphVertList;
34+
JPH::IndexedTriangleList jphTriList;
35+
jphVertList.reserve(vertices.numElem());
36+
37+
for (int i = 0; i < vertices.numElem(); ++i)
38+
jphVertList.push_back(Convert::ToFloat3(vertices[i]));
39+
40+
jphTriList.reserve(indices.numElem() / 3);
41+
for (int i = 0; i < indices.numElem(); i += 3)
42+
{
43+
JPH::IndexedTriangle jphTri(indices[i], indices[i + 1], indices[i + 2]);
44+
jphTriList.push_back(jphTri);
45+
}
46+
47+
JPH::MeshShapeSettings jphMeshSettings(jphVertList, jphTriList);
48+
auto jphResult = jphMeshSettings.Create();
49+
return jphResult.Get();
50+
}
51+
case PHYSSHAPE_TYPE_CONVEX:
52+
{
53+
JPH::Array<JPH::Vec3> jphVertList;
54+
jphVertList.reserve(vertices.numElem());
55+
for (int i = 0; i < vertices.numElem(); ++i)
56+
jphVertList.push_back(Convert::ToVec3(vertices[i]));
57+
58+
JPH::ConvexHullShapeSettings jphHullSettings(jphVertList, ph_studioShapeMargin.GetFloat());
59+
auto jphResult = jphHullSettings.Create();
60+
return jphResult.Get();
61+
}
62+
}
63+
64+
MsgError("InternalGenerateShape: Shape type %d is invalid!\n", type);
65+
66+
return nullptr;
67+
}
68+
69+
bool CDkJoltStudioShapeCache::IsInitialized() const
70+
{
71+
return true;
72+
}
73+
74+
// checks the shape is initialized for the cache
75+
bool CDkJoltStudioShapeCache::IsShapeCachePresent( StudioPhyShapeData& shapeInfo )
76+
{
77+
using namespace Threading;
78+
CScopedMutex m(s_shapeCacheMutex);
79+
if (arrayFindIndex(m_collisionShapes, reinterpret_cast<JPH::Shape*>(shapeInfo.cacheRef)) != -1)
80+
return true;
81+
82+
return false;
83+
}
84+
85+
// initializes whole studio shape model with all objects
86+
void CDkJoltStudioShapeCache::InitStudioCache(StudioPhysData& studioData)
87+
{
88+
using namespace Threading;
89+
90+
for (StudioPhyShapeData& shapeData : studioData.shapes)
91+
{
92+
const physgeominfo_t& shapeInfo = shapeData.desc;
93+
94+
JPH::Ref<JPH::Shape> shape = jphGenerateCollisionShape(
95+
studioData.vertices,
96+
ArrayCRef(studioData.indices.ptr() + shapeInfo.startIndices, shapeInfo.numIndices),
97+
static_cast<EPhysShapeType>(shapeInfo.type));
98+
99+
{
100+
CScopedMutex m(s_shapeCacheMutex);
101+
m_collisionShapes.append(shape);
102+
shapeData.cacheRef = shape;
103+
}
104+
}
105+
106+
for (StudioPhyObjData& obj : studioData.objects)
107+
{
108+
for (int i = 0; i < obj.desc.numShapes; ++i)
109+
obj.shapeCacheRefs[i] = studioData.shapes[obj.desc.shapeIndex[i]].cacheRef;
110+
}
111+
}
112+
113+
void CDkJoltStudioShapeCache::DestroyStudioCache(StudioPhysData& studioData)
114+
{
115+
using namespace Threading;
116+
117+
for(StudioPhyShapeData& shapeData : studioData.shapes)
118+
{
119+
CScopedMutex m(s_shapeCacheMutex);
120+
const int shapeIdx = arrayFindIndex(m_collisionShapes, reinterpret_cast<JPH::Shape*>(shapeData.cacheRef));
121+
if (shapeIdx == -1)
122+
continue;
123+
124+
m_collisionShapes.fastRemoveIndex(shapeIdx);
125+
}
126+
}
127+
128+
// does all shape cleanup
129+
void CDkJoltStudioShapeCache::Cleanup_Invalidate()
130+
{
131+
using namespace Threading;
132+
133+
CScopedMutex m(s_shapeCacheMutex);
134+
m_collisionShapes.clear(true);
135+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//////////////////////////////////////////////////////////////////////////////////
2+
// Copyright (C) Inspiration Byte
3+
// 2009-2020
4+
//////////////////////////////////////////////////////////////////////////////////
5+
// Description: Physics model cache for bullet physics
6+
// Generates real shapes for Bullet Collision
7+
///////////////////////////////////////////////////////////////////////////////////
8+
9+
#pragma once
10+
#include "physics/IStudioShapeCache.h"
11+
12+
class CDkJoltStudioShapeCache : public IStudioShapeCache
13+
{
14+
public:
15+
CDkJoltStudioShapeCache() = default;
16+
17+
bool IsInitialized() const;
18+
19+
// checks the shape is initialized for the cache
20+
bool IsShapeCachePresent( StudioPhyShapeData& shapeInfo );
21+
22+
// initializes whole studio shape model with all objects
23+
void InitStudioCache( StudioPhysData& studioData );
24+
void DestroyStudioCache( StudioPhysData& studioData );
25+
26+
// does all shape cleanup
27+
void Cleanup_Invalidate();
28+
29+
protected:
30+
31+
// cached shapes
32+
Array<JPH::ShapeRefC> m_collisionShapes{ PP_SL };
33+
};

shared/dkphysics/DkPhysicsInit.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "core/IDkCore.h"
1111
#include "core/ConVar.h"
1212
#include "DkPhysicsWorld.h"
13+
#include "DkJoltShapeCache.h"
1314

1415
DECLARE_CVAR(ph_jobThreads, "2", "Physics job threads", CV_ARCHIVE);
1516
DECLARE_CVAR(ph_tempMemAllocMB, "10", "Temp allocator memory", CV_CHEAT);
@@ -18,6 +19,7 @@ DECLARE_CVAR(ph_tempMemAllocMB, "10", "Temp allocator memory", CV_CHEAT);
1819

1920
static JPH::JobSystemThreadPool* s_jphJobThreadPool = nullptr;
2021
static JPH::TempAllocatorImpl* s_jphTempAlloc = nullptr;
22+
static CDkJoltStudioShapeCache s_joltShapeCache;
2123

2224
static bool jphAssertFailedImpl(const char *inExpression, const char *inMessage, const char *inFile, uint inLine)
2325
{
@@ -69,6 +71,7 @@ JPH::TempAllocator* GetJoltTempAlloc() { return s_jphTempAlloc; }
6971
void dkPhysicsLibInit()
7072
{
7173
GetIDkCoreImpl()->RegisterInterface(&s_dkPhysics);
74+
GetIDkCoreImpl()->RegisterInterface(&s_joltShapeCache);
7275

7376
using namespace JPH;
7477
#ifdef JPH_ENABLE_ASSERTS
@@ -104,4 +107,5 @@ void dkPhysicsLibShutdown()
104107
UnregisterTypes();
105108

106109
GetIDkCoreImpl()->UnregisterInterface<DkPhysics>();
110+
GetIDkCoreImpl()->UnregisterInterface<IStudioShapeCache>();
107111
}

shared/dkphysics/DkPhysicsJoint.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "DkJoltPCH.h"
1111
#include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
1212

13-
#include "DkJoltConvert.h"
1413
#include "DkPhysicsObject.h"
1514
#include "DkPhysicsJoint.h"
1615

shared/dkphysics/DkPhysicsObject.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "DkJoltPCH.h"
1111
#include <Jolt/Physics/PhysicsSystem.h>
1212

13-
#include "DkJoltConvert.h"
1413
#include "DkPhysicsObject.h"
1514

1615
#include "render/IDebugOverlay.h"
@@ -203,7 +202,6 @@ const char* DkPhysicsObject::GetName() const
203202

204203
void DkPhysicsObject::SetCollisionResponseEnabled(bool bEnabled)
205204
{
206-
ASSERT_FAIL("Unimplemented");
207205
}
208206

209207
// returns object static state

shared/dkphysics/DkPhysicsWorld.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <Jolt/Physics/Body/BodyCreationSettings.h>
3030
#include <Jolt/Physics/Body/BodyActivationListener.h>
3131

32-
#include "DkJoltConvert.h"
3332
#include "DkPhysicsInit.h"
3433
#include "DkPhysicsWorld.h"
3534
#include "DkPhysicsObject.h"
@@ -575,9 +574,19 @@ void DkPhysics::CastShape(const Vector3D &tracestart, const Vector3D &traceend,
575574

576575
void DkPhysics::DestroyPhysicsObject(IPhysicsObject *pObject)
577576
{
578-
Threading::CScopedMutex m(m_Mutex);
579-
if(m_objects.fastRemove(static_cast<DkPhysicsObject*>(pObject)))
580-
delete pObject;
577+
DkPhysicsObject* physObj = static_cast<DkPhysicsObject*>(pObject);
578+
{
579+
Threading::CScopedMutex m(m_Mutex);
580+
if (!m_objects.fastRemove(static_cast<DkPhysicsObject*>(pObject)))
581+
return;
582+
}
583+
584+
JPH::BodyID jphBodyId = physObj->m_jphObjId;
585+
delete physObj;
586+
587+
JPH::BodyInterface& jphBodyIface = m_jphPhysSys->GetBodyInterface();
588+
jphBodyIface.RemoveBody(jphBodyId);
589+
jphBodyIface.DestroyBody(jphBodyId);
581590
}
582591

583592
void DkPhysics::DestroyPhysicsJoint(IPhysicsJoint *pJoint)
@@ -804,7 +813,6 @@ IPhysicsObject* DkPhysics::CreateStudioObject(const StudioPhysData& data, int nO
804813
DkPhysicsObject* physObj = PPNew DkPhysicsObject(*m_jphPhysSys, jphBodyId);
805814
physObj->m_physMaterial = material;
806815
physObj->SetFriction(material->friction);
807-
physObj->SetDamping(material->dampening, material->dampening);
808816
jphBodyIface.SetUserData(jphBodyId, reinterpret_cast<JPH::uint64>(physObj));
809817

810818
{

0 commit comments

Comments
 (0)