RavEngine
Loading...
Searching...
No Matches
PxPvdFoundation.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 PX_PVD_FOUNDATION_H
28#define PX_PVD_FOUNDATION_H
29
30#include "foundation/PxVec3.h"
31#include "foundation/PxTransform.h"
32#include "foundation/PxBounds3.h"
33#include "foundation/PxHashSet.h"
34#include "foundation/PxHashMap.h"
35#include "foundation/PxArray.h"
36#include "foundation/PxString.h"
37#include "foundation/PxPool.h"
38#include "PxPvdObjectModelBaseTypes.h"
39
40namespace physx
41{
42namespace pvdsdk
43{
44
45extern PxAllocatorCallback* gPvdAllocatorCallback;
46
48{
49 void* allocate(size_t size, const char* typeName, const char* filename, int line)
50 {
51 return PxGetBroadcastAllocator()->allocate(size, typeName, filename, line);
52 }
53 void deallocate(void* ptr)
54 {
56 }
57};
58
60{
61 uint8_t* mBegin;
62 uint8_t* mEnd;
63 uint8_t* mCapacityEnd;
64 const char* mBufDataName;
65
66 public:
67 RawMemoryBuffer(const char* name) : mBegin(0), mEnd(0), mCapacityEnd(0),mBufDataName(name)
68 {
69 PX_UNUSED(mBufDataName);
70 }
72 {
73 PX_FREE(mBegin);
74 }
75 uint32_t size() const
76 {
77 return static_cast<uint32_t>(mEnd - mBegin);
78 }
79 uint32_t capacity() const
80 {
81 return static_cast<uint32_t>(mCapacityEnd - mBegin);
82 }
83 uint8_t* begin()
84 {
85 return mBegin;
86 }
87 uint8_t* end()
88 {
89 return mEnd;
90 }
91 const uint8_t* begin() const
92 {
93 return mBegin;
94 }
95 const uint8_t* end() const
96 {
97 return mEnd;
98 }
99 void clear()
100 {
101 mEnd = mBegin;
102 }
103 const char* cStr()
104 {
105 if(mEnd && (*mEnd != 0))
106 write(0);
107 return reinterpret_cast<const char*>(mBegin);
108 }
109 uint32_t write(uint8_t inValue)
110 {
111 *growBuf(1) = inValue;
112 return 1;
113 }
114
115 template <typename TDataType>
116 uint32_t write(const TDataType& inValue)
117 {
118 const uint8_t* __restrict readPtr = reinterpret_cast<const uint8_t*>(&inValue);
119 uint8_t* __restrict writePtr = growBuf(sizeof(TDataType));
120 for(uint32_t idx = 0; idx < sizeof(TDataType); ++idx)
121 writePtr[idx] = readPtr[idx];
122 return sizeof(TDataType);
123 }
124
125 template <typename TDataType>
126 uint32_t write(const TDataType* inValue, uint32_t inLength)
127 {
128 uint32_t writeSize = inLength * sizeof(TDataType);
129 if(inValue && inLength)
130 {
131 physx::intrinsics::memCopy(growBuf(writeSize), inValue, writeSize);
132 }
133 if(inLength && !inValue)
134 {
135 PX_ASSERT(false);
136 // You can't not write something, because that will cause
137 // the receiving end to crash.
138 for(uint32_t idx = 0; idx < writeSize; ++idx)
139 write(0);
140 }
141 return writeSize;
142 }
143
144 uint8_t* growBuf(uint32_t inAmount)
145 {
146 uint32_t offset = size();
147 uint32_t newSize = offset + inAmount;
148 reserve(newSize);
149 mEnd += inAmount;
150 return mBegin + offset;
151 }
152 void writeZeros(uint32_t inAmount)
153 {
154 uint32_t offset = size();
155 growBuf(inAmount);
156 physx::intrinsics::memZero(begin() + offset, inAmount);
157 }
158 void reserve(uint32_t newSize)
159 {
160 uint32_t currentSize = size();
161 if(newSize && newSize >= capacity())
162 {
163 uint32_t newDataSize = newSize > 4096 ? newSize + (newSize >> 2) : newSize*2;
164 uint8_t* newData = static_cast<uint8_t*>(PX_ALLOC(newDataSize, mBufDataName));
165 if(mBegin)
166 {
167 physx::intrinsics::memCopy(newData, mBegin, currentSize);
168 PX_FREE(mBegin);
169 }
170 mBegin = newData;
171 mEnd = mBegin + currentSize;
172 mCapacityEnd = mBegin + newDataSize;
173 }
174 }
175};
176
178{
179 ForwardingMemoryBuffer(const char* bufDataName) : RawMemoryBuffer(bufDataName)
180 {
181 }
182
183 ForwardingMemoryBuffer& operator<<(const char* inString)
184 {
185 if(inString && *inString)
186 {
187 uint32_t len = static_cast<uint32_t>(strlen(inString));
188 write(inString, len);
189 }
190 return *this;
191 }
192
193 template <typename TDataType>
194 inline ForwardingMemoryBuffer& toStream(const char* inFormat, const TDataType inData)
195 {
196 char buffer[128] = { 0 };
197 Pxsnprintf(buffer, 128, inFormat, inData);
198 *this << buffer;
199 return *this;
200 }
201
202 inline ForwardingMemoryBuffer& operator<<(bool inData)
203 {
204 *this << (inData ? "true" : "false");
205 return *this;
206 }
207 inline ForwardingMemoryBuffer& operator<<(int32_t inData)
208 {
209 return toStream("%d", inData);
210 }
211 inline ForwardingMemoryBuffer& operator<<(uint16_t inData)
212 {
213 return toStream("%u", uint32_t(inData));
214 }
215 inline ForwardingMemoryBuffer& operator<<(uint8_t inData)
216 {
217 return toStream("%u", uint32_t(inData));
218 }
219 inline ForwardingMemoryBuffer& operator<<(char inData)
220 {
221 return toStream("%c", inData);
222 }
223 inline ForwardingMemoryBuffer& operator<<(uint32_t inData)
224 {
225 return toStream("%u", inData);
226 }
227 inline ForwardingMemoryBuffer& operator<<(uint64_t inData)
228 {
229 return toStream("%I64u", inData);
230 }
231 inline ForwardingMemoryBuffer& operator<<(int64_t inData)
232 {
233 return toStream("%I64d", inData);
234 }
235 inline ForwardingMemoryBuffer& operator<<(const void* inData)
236 {
237 return *this << static_cast<uint64_t>(reinterpret_cast<size_t>(inData));
238 }
239 inline ForwardingMemoryBuffer& operator<<(float inData)
240 {
241 return toStream("%g", double(inData));
242 }
243 inline ForwardingMemoryBuffer& operator<<(double inData)
244 {
245 return toStream("%g", inData);
246 }
247 inline ForwardingMemoryBuffer& operator<<(const PxVec3& inData)
248 {
249 *this << inData[0];
250 *this << " ";
251 *this << inData[1];
252 *this << " ";
253 *this << inData[2];
254 return *this;
255 }
256
257 inline ForwardingMemoryBuffer& operator<<(const PxQuat& inData)
258 {
259 *this << inData.x;
260 *this << " ";
261 *this << inData.y;
262 *this << " ";
263 *this << inData.z;
264 *this << " ";
265 *this << inData.w;
266 return *this;
267 }
268
269 inline ForwardingMemoryBuffer& operator<<(const PxTransform& inData)
270 {
271 *this << inData.q;
272 *this << " ";
273 *this << inData.p;
274 return *this;
275 }
276
277 inline ForwardingMemoryBuffer& operator<<(const PxBounds3& inData)
278 {
279 *this << inData.minimum;
280 *this << " ";
281 *this << inData.maximum;
282 return *this;
283 }
284
285};
286
287template <typename TDataType>
288inline void* PvdAllocate(const char* typeName, const char* file, int line)
289{
290 PX_ASSERT(gPvdAllocatorCallback);
291 return gPvdAllocatorCallback->allocate(sizeof(TDataType), typeName, file, line);
292}
293
294template <typename TDataType>
295inline void PvdDeleteAndDeallocate(TDataType* inDType)
296{
297 PX_ASSERT(gPvdAllocatorCallback);
298 if(inDType)
299 {
300 inDType->~TDataType();
301 gPvdAllocatorCallback->deallocate(inDType);
302 }
303}
304}
305}
306
307#define PVD_NEW(dtype) new (PvdAllocate<dtype>(#dtype, __FILE__, __LINE__)) dtype
308#define PVD_DELETE(obj) PvdDeleteAndDeallocate(obj);
309//#define PVD_NEW(dtype) PX_NEW(dtype)
310//#define PVD_DELETE(obj) PX_DELETE(obj)
311#define PVD_FOREACH(varname, stop) for(uint32_t varname = 0; varname < stop; ++varname)
312#define PVD_POINTER_TO_U64(ptr) static_cast<uint64_t>(reinterpret_cast<size_t>(ptr))
313
314#endif
315
Abstract base class for an application defined memory allocator that can be used by the Nv library.
Definition PxAllocatorCallback.h:53
virtual void deallocate(void *ptr)=0
Frees memory previously allocated by allocate().
virtual void * allocate(size_t size, const char *typeName, const char *filename, int line)=0
Allocates size bytes of memory, which must be 16-byte aligned.
Class representing 3D range or axis aligned bounding box.
Definition PxBounds3.h:58
This is a quaternion class. For more information on quaternion mathematics consult a mathematics sour...
Definition PxQuat.h:50
float x
Definition PxQuat.h:395
class representing a rigid euclidean transform as a quaternion and a vector
Definition PxTransform.h:49
3 Element vector class.
Definition PxVec3.h:50
Definition PxPvdFoundation.h:48
Definition PxPvdFoundation.h:60
PX_C_EXPORT PX_FOUNDATION_API physx::PxAllocatorCallback *PX_CALL_CONV PxGetBroadcastAllocator()
Get the broadcasting allocator callback.
Definition FdFoundation.cpp:264
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition PxPvdFoundation.h:178