RavEngine
Loading...
Searching...
No Matches
NpHairSystem.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26
27#ifndef NP_HAIR_SYSTEM_H
28#define NP_HAIR_SYSTEM_H
29
30#include "foundation/PxPreprocessor.h"
31#if PX_ENABLE_FEATURES_UNDER_CONSTRUCTION
32#include "PxHairSystem.h"
33#include "ScHairSystemCore.h"
34#include "NpActorTemplate.h"
35
36namespace physx
37{
38 class NpScene;
39 class NpShape;
40
41 class NpHairSystem : public NpActorTemplate<PxHairSystem>
42 {
43 public:
44 template <class T>
45 class MemoryWithAlloc
46 {
47 PX_NOCOPY(MemoryWithAlloc)
48 public:
49 MemoryWithAlloc() : mData(NULL), mSize(0), mAlloc(NULL) {}
50
51 ~MemoryWithAlloc()
52 {
53 deallocate();
54 }
55
56 void allocate(uint32_t size)
57 {
58 if (size == mSize)
59 return;
60
61 deallocate();
62 if (size > 0)
63 {
64 mData = reinterpret_cast<T*>(mAlloc->allocate(sizeof(T) * size, 0, __FILE__, __LINE__));
65 if (mData != NULL)
66 mSize = size;
67 }
68 else
69 {
70 mData = NULL;
71 mSize = 0;
72 }
73 }
74
75 void setAllocatorCallback(physx::PxVirtualAllocatorCallback* cb)
76 {
77 if (mSize > 0)
78 {
79 deallocate();
80 }
81 mAlloc = cb;
82 }
83
84 void reset()
85 {
86 if (mSize > 0)
87 {
88 deallocate();
89 }
90 }
91
92 void swap(MemoryWithAlloc<T>& other)
93 {
94 physx::PxSwap(mData, other.mData);
95 physx::PxSwap(mSize, other.mSize);
96 physx::PxSwap(mAlloc, other.mAlloc);
97 }
98
99 uint32_t size() const
100 {
101 return mSize;
102 }
103
104 const T& operator[](uint32_t i) const
105 {
106 PX_ASSERT(i < mSize);
107 return mData[i];
108 }
109
110 T& operator[](uint32_t i)
111 {
112 PX_ASSERT(i < mSize);
113 return mData[i];
114 }
115
116 const T* begin() const
117 {
118 return mData;
119 }
120
121 T* begin()
122 {
123 return mData;
124 }
125
126 private:
127 void deallocate()
128 {
129 if (mSize > 0)
130 {
131 mAlloc->deallocate(mData);
132 mSize = 0;
133 }
134 }
135
136 private:
137 T* mData;
138 uint32_t mSize;
140 };
141
142 public:
143 NpHairSystem(PxCudaContextManager& cudaContextManager);
144 NpHairSystem(PxBaseFlags baseFlags, PxCudaContextManager& cudaContextManager);
145
146 virtual ~NpHairSystem() PX_OVERRIDE;
147
148 // external API
149 virtual void release() PX_OVERRIDE;
150
151 virtual PxActorType::Enum getType() const PX_OVERRIDE { return PxActorType::eHAIRSYSTEM; }
152
153 virtual PxBounds3 getWorldBounds(float inflation = 1.01f) const PX_OVERRIDE;
154
155 virtual void setHairSystemFlag(PxHairSystemFlag::Enum flag, bool val) PX_OVERRIDE;
156 virtual void setHairSystemFlags(PxHairSystemFlags flags) PX_OVERRIDE { mCore.setFlags(flags); }
157 virtual PxHairSystemFlags getHairSystemFlags() const PX_OVERRIDE { return mCore.getFlags(); }
158
159 virtual void setReadRequestFlag(PxHairSystemData::Enum flag, bool val) PX_OVERRIDE;
160 virtual void setReadRequestFlags(PxHairSystemDataFlags flags) PX_OVERRIDE;
161 virtual PxHairSystemDataFlags getReadRequestFlags() const PX_OVERRIDE;
162
163 virtual void setBendingRestAngles(const PxReal* bendingRestAngles, PxReal bendingCompliance) PX_OVERRIDE;
164
165 virtual void setTwistingCompliance(PxReal twistingCompliance) PX_OVERRIDE;
166 virtual void getTwistingRestPositions(PxReal* buffer) PX_OVERRIDE;
167
168 virtual PxCudaContextManager* getCudaContextManager() const PX_OVERRIDE { return mCudaContextManager; }
169
170 virtual void setWakeCounter(PxReal wakeCounterValue) PX_OVERRIDE;
171 virtual PxReal getWakeCounter() const PX_OVERRIDE;
172 virtual bool isSleeping() const PX_OVERRIDE;
173
174 virtual void setRestPositions(PxVec4* restPos, bool isGpuPtr, const PxTransform& transf, const PxRigidBody* actor) PX_OVERRIDE;
175 virtual PxVec4* getRestPositionsGpu(PxTransform* transf) PX_OVERRIDE;
176
177 virtual void addRigidAttachment(const PxRigidActor* actor) PX_OVERRIDE;
178 virtual void removeRigidAttachment(const PxRigidActor* actor) PX_OVERRIDE;
179 virtual void setRigidAttachments(PxParticleRigidAttachment* attachments, PxU32 numAttachments, bool isGpuPtr) PX_OVERRIDE;
180 virtual PxParticleRigidAttachment* getRigidAttachmentsGpu(PxU32* numAttachments = NULL) PX_OVERRIDE;
181
182 virtual void initFromDesc(const PxHairSystemDesc& desc) PX_OVERRIDE;
183
184 virtual void setTopology(PxVec4* vertexPositionsInvMass, PxVec4* vertexVelocities,
185 const PxU32* strandPastEndIndices, PxReal segmentLength, PxReal segmentRadius, PxU32 numVertices,
186 PxU32 numStrands, const PxBounds3& bounds) PX_OVERRIDE;
187
188 virtual void setLevelOfDetailGradations(const PxReal* proportionOfStrands, const PxReal* proportionOfVertices, PxU32 numLevels) PX_OVERRIDE;
189 virtual void setLevelOfDetail(PxU32 level) PX_OVERRIDE;
190 virtual PxU32 getLevelOfDetail(PxU32* numLevels) const PX_OVERRIDE;
191
192 virtual void setPositionsInvMass(PxVec4* vertexPositionsInvMass, const PxBounds3& bounds) PX_OVERRIDE;
193 virtual PxVec4* getPositionInvMass() PX_OVERRIDE { return mCore.getShapeCore().getLLCore().mPositionInvMass; }
194 virtual const PxVec4* getPositionInvMass() const PX_OVERRIDE{ return mCore.getShapeCore().getLLCore().mPositionInvMass; }
195 virtual const PxVec4* getPositionInvMassGpuSim() const PX_OVERRIDE{ return mCore.getShapeCore().getLLCore().mPositionInvMassGpuSim; }
196
197 virtual void setVelocities(PxVec4* vertexVelocities) PX_OVERRIDE;
198 virtual PxVec4* getVelocities() PX_OVERRIDE { return mCore.getShapeCore().getLLCore().mVelocity; }
199 virtual const PxVec4* getVelocities() const PX_OVERRIDE { return mCore.getShapeCore().getLLCore().mVelocity; }
200
201 virtual PxU32 getNumVertices() const PX_OVERRIDE { return mCore.getShapeCore().getLLCore().mNumVertices; }
202 virtual PxU32 getNumStrands() const PX_OVERRIDE { return mCore.getShapeCore().getLLCore().mNumStrands; }
203 virtual const PxU32* getStrandPastEndIndices() const PX_OVERRIDE { return mCore.getShapeCore().getLLCore().mStrandPastEndIndices; }
204 virtual const PxU32* getStrandPastEndIndicesGpuSim() const PX_OVERRIDE { return mCore.getShapeCore().getLLCore().mStrandPastEndIndicesGpuSim; }
205
206 virtual void setSolverIterationCounts(PxU32 iters) PX_OVERRIDE;
207 virtual PxU32 getSolverIterationCounts() const PX_OVERRIDE;
208 virtual void setWind(const PxVec3& wind) PX_OVERRIDE;
209 virtual PxVec3 getWind() const PX_OVERRIDE;
210 virtual void setSegmentRadius(PxReal radius) PX_OVERRIDE;
211 virtual void getSegmentDimensions(PxReal& length, PxReal& radius) const PX_OVERRIDE;
212 virtual void setFrictionParameters(PxReal interHairVelDamping, PxReal frictionCoeff) PX_OVERRIDE;
213 virtual void getFrictionParameters(PxReal& interHairVelDamping, PxReal& frictionCoeff) const PX_OVERRIDE;
214 virtual void setShapeCompliance(PxReal startCompliance, PxReal strandRatio) PX_OVERRIDE;
215 virtual void getShapeCompliance(PxReal& startCompliance, PxReal& strandRatio) const PX_OVERRIDE;
216 virtual void setInterHairRepulsion(PxReal repulsion) PX_OVERRIDE;
217 virtual PxReal getInterHairRepulsion() const PX_OVERRIDE;
218 virtual void setSelfCollisionRelaxation(PxReal relaxation) PX_OVERRIDE;
219 virtual PxReal getSelfCollisionRelaxation() const PX_OVERRIDE;
220 virtual void setStretchingRelaxation(PxReal relaxation) PX_OVERRIDE;
221 virtual PxReal getStretchingRelaxation() const PX_OVERRIDE;
222 virtual void setContactOffset(PxReal contactOffset) PX_OVERRIDE;
223 virtual PxReal getContactOffset() const PX_OVERRIDE;
224 virtual void setHairContactOffset(PxReal hairContactOffset) PX_OVERRIDE;
225 virtual PxReal getHairContactOffset() const PX_OVERRIDE;
226 virtual void setShapeMatchingParameters(PxReal compliance, PxReal linearStretching, PxU16 numVerticesPerGroup, PxU16 numVerticesOverlap) PX_OVERRIDE;
227 virtual PxReal getShapeMatchingCompliance() const PX_OVERRIDE;
228
229#if PX_ENABLE_DEBUG_VISUALIZATION
230 void visualize(PxRenderOutput& out, NpScene& npScene) const;
231#endif
232
233 PX_FORCE_INLINE const Sc::HairSystemCore& getCore() const { return mCore; }
234 PX_FORCE_INLINE Sc::HairSystemCore& getCore() { return mCore; }
235 static PX_FORCE_INLINE size_t getCoreOffset() { return PX_OFFSET_OF_RT(NpHairSystem, mCore); }
236
237 // Debug name
238 void setName(const char*);
239 const char* getName() const;
240
241 private:
242 void setLlGridSize(const PxBounds3& bounds);
243 void createAllocator();
244 void releaseAllocator();
245
246 private:
247 Sc::HairSystemCore mCore;
248 PxHairSystemGeometry mGeometry;
249 PxCudaContextManager* mCudaContextManager;
250
251 PxsMemoryManager* mMemoryManager;
252 PxVirtualAllocatorCallback* mHostMemoryAllocator;
253 PxVirtualAllocatorCallback* mDeviceMemoryAllocator;
254
255 // internal buffers populated when hair system initialized from desc instead of user-provided buffers
256 MemoryWithAlloc<PxVec4> mPosInvMassInternal;
257 MemoryWithAlloc<PxVec4> mVelInternal;
258 MemoryWithAlloc<PxU32> mStrandPastEndIndicesInternal;
259
260 // internal buffers in case user gives us CPU-buffers
261 MemoryWithAlloc<PxParticleRigidAttachment> mParticleRigidAttachmentsInternal;
262 MemoryWithAlloc<PxVec4> mRestPositionsInternal;
263
264 MemoryWithAlloc<PxTransform> mRestPositionTransformPinnedBuf;
265
266 PxArray<PxReal> mLodProportionOfStrands;
267 PxArray<PxReal> mLodProportionOfVertices;
268
269 const PxRigidActor* mRestPositionActor;
270 };
271}
272
273#endif
274
275#endif
Definition PxAllocator.h:140
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_OVERRIDE
Definition PxPreprocessor.h:375
uint32 uint32_t
Definition fwd.hpp:131
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39