Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
PhysX.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_PHYSX_H_
34#define DVD_PHYSX_H_
35
36#ifndef DVD_PHYSICS_API_FOUND_
37#define DVD_PHYSICS_API_FOUND_
38#endif
39
40#include "PhysXActor.h"
41#include "PhysXSceneInterface.h"
43
44namespace physx
45{
46 class PxPvd;
47 class PxPvdTransport;
48}
49
50constexpr auto MAX_ACTOR_QUEUE = 30;
51
52namespace Divide {
53
54class PhysX;
55class PxDefaultAllocator final : public physx::PxAllocatorCallback
56{
57 void* allocate(const size_t size, const char*, const char*, int) noexcept override
58 {
59 return mi_new_aligned_nothrow(size, 16);
60 }
61
62 void deallocate(void* ptr) noexcept override
63 {
64 mi_free_aligned(ptr, s_alignment);
65 }
66
67private:
68 static constexpr size_t s_alignment = 16u;
69};
70
71class PhysicsAsset;
72class SceneGraphNode;
73class PhysX final : public PhysicsAPIWrapper {
74
75public:
76 explicit PhysX( PlatformContext& context );
77
78 [[nodiscard]] ErrorCode initPhysicsAPI(U8 targetFrameRate, F32 simSpeed) override;
79 [[nodiscard]] bool closePhysicsAPI() override;
80 void frameStarted(U64 deltaTimeGameUS ) override;
81 void frameEnded(U64 deltaTimeGameUS ) override;
82 void idle() override;
83
84 void updateTimeStep(U8 timeStepFactor, F32 simSpeed) override;
85
86 [[nodiscard]] bool initPhysicsScene(Scene& scene) override;
87 [[nodiscard]] bool destroyPhysicsScene(const Scene& scene) override;
88
89 [[nodiscard]] bool intersect(const Ray& intersectionRay, vec2<F32> range, vector<SGNRayResult>& intersectionsOut) const override;
90
91 [[nodiscard]] physx::PxPhysics* getSDK() const noexcept { return _gPhysicsSDK; }
92
93 [[nodiscard]] PhysicsAsset* createRigidActor(SceneGraphNode* node, RigidBodyComponent& parentComp) override;
94
95 [[nodiscard]] bool convertActor(PhysicsAsset* actor, PhysicsGroup newGroup) override;
96 void togglePvdConnection() const;
97 void createPvdConnection(const char* ip, physx::PxU32 port, physx::PxU32 timeout, bool useFullConnection);
98
99#if PX_SUPPORT_GPU_PHYSX
100 POINTER_R(physx::PxCudaContextManager, cudaContextManager, nullptr);
101#endif //PX_SUPPORT_GPU_PHYSX
102
103protected:
104 PhysXSceneInterface_uptr _targetScene;
105 physx::PxRigidActor* createActorForGroup(PhysicsGroup group, const physx::PxTransform& pose);
106private:
108 physx::PxPhysics* _gPhysicsSDK = nullptr;
109 physx::PxFoundation* _foundation = nullptr;
110 physx::PxMaterial* _defaultMaterial = nullptr;
111 physx::PxReal _timeStep = 0.0f;
112 physx::PxU8 _timeStepFactor = 0;
113 physx::PxReal _accumulator = 0.0f;
114 physx::PxPvd* _pvd = nullptr;
115 physx::PxPvdTransport* _transport = nullptr;
116
119};
120
121}; // namespace Divide
122
123#endif //DVD_PHYSX_H_
constexpr auto MAX_ACTOR_QUEUE
Definition: PhysX.h:50
#define POINTER_R(...)
Convenience method to add a class member with public read access but protected write access.
physx::PxU8 _timeStepFactor
Definition: PhysX.h:112
bool convertActor(PhysicsAsset *actor, PhysicsGroup newGroup) override
Definition: PhysX.cpp:599
physx::PxMaterial * _defaultMaterial
Definition: PhysX.h:110
physx::PxPvd * _pvd
Definition: PhysX.h:114
PhysXSceneInterface_uptr _targetScene
Definition: PhysX.h:104
physx::PxPhysics * _gPhysicsSDK
Definition: PhysX.h:108
ErrorCode initPhysicsAPI(U8 targetFrameRate, F32 simSpeed) override
Definition: PhysX.cpp:138
bool initPhysicsScene(Scene &scene) override
Definition: PhysX.cpp:332
void frameEnded(U64 deltaTimeGameUS) override
Update actors.
Definition: PhysX.cpp:314
physx::PxReal _timeStep
Definition: PhysX.h:111
PhysicsAsset * createRigidActor(SceneGraphNode *node, RigidBodyComponent &parentComp) override
Definition: PhysX.cpp:403
bool closePhysicsAPI() override
Definition: PhysX.cpp:213
physx::PxPhysics * getSDK() const noexcept
Definition: PhysX.h:91
physx::PxReal _accumulator
Definition: PhysX.h:113
physx::PxRigidActor * createActorForGroup(PhysicsGroup group, const physx::PxTransform &pose)
Definition: PhysX.cpp:379
static SharedMutex s_meshCacheLock
Definition: PhysX.h:117
bool destroyPhysicsScene(const Scene &scene) override
Definition: PhysX.cpp:362
void updateTimeStep(U8 timeStepFactor, F32 simSpeed) override
Definition: PhysX.cpp:286
F32 _simulationSpeed
Definition: PhysX.h:107
bool intersect(const Ray &intersectionRay, vec2< F32 > range, vector< SGNRayResult > &intersectionsOut) const override
Definition: PhysX.cpp:626
void frameStarted(U64 deltaTimeGameUS) override
Process results.
Definition: PhysX.cpp:295
void createPvdConnection(const char *ip, physx::PxU32 port, physx::PxU32 timeout, bool useFullConnection)
Definition: PhysX.cpp:254
void togglePvdConnection() const
Definition: PhysX.cpp:234
void idle() override
Definition: PhysX.cpp:323
physx::PxFoundation * _foundation
Definition: PhysX.h:109
static hashMap< U64, physx::PxTriangleMesh * > s_gMeshCache
Definition: PhysX.h:118
physx::PxPvdTransport * _transport
Definition: PhysX.h:115
PlatformContext & context() noexcept
void deallocate(void *ptr) noexcept override
Definition: PhysX.h:62
static constexpr size_t s_alignment
Definition: PhysX.h:68
void * allocate(const size_t size, const char *, const char *, int) noexcept override
Definition: PhysX.h:57
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
uint8_t U8
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
eastl::vector< Type > vector
Definition: Vector.h:42
hashAlg::unordered_map< K, V, HashFun, Predicate > hashMap
Definition: HashMap.h:55
uint64_t U64
Definition: PhysX.h:45