RavEngine
Loading...
Searching...
No Matches
ScContactReportBuffer.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// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef SC_CONTACT_REPORT_BUFFER_H
30#define SC_CONTACT_REPORT_BUFFER_H
31
32#include "foundation/Px.h"
33#include "common/PxProfileZone.h"
34
35namespace physx
36{
37 namespace Sc
38 {
40 {
41 public:
42 PX_FORCE_INLINE ContactReportBuffer(PxU32 initialSize, bool noResizeAllowed)
43 : mBuffer(NULL)
44 ,mCurrentBufferIndex(0)
45 ,mCurrentBufferSize(initialSize)
46 ,mDefaultBufferSize(initialSize)
47 ,mLastBufferIndex(0)
48 ,mAllocationLocked(noResizeAllowed)
49 {
50 mBuffer = allocateBuffer(initialSize);
51 PX_ASSERT(mBuffer);
52 }
53
55 {
56 PX_FREE(mBuffer);
57 }
58
59 PX_FORCE_INLINE void reset();
60 PX_FORCE_INLINE void flush();
61
62 PX_FORCE_INLINE PxU8* allocateNotThreadSafe(PxU32 size, PxU32& index, PxU32 alignment= 16);
63 PX_FORCE_INLINE PxU8* reallocateNotThreadSafe(PxU32 size, PxU32& index, PxU32 alignment= 16, PxU32 lastIndex = 0xFFFFFFFF);
64 PX_FORCE_INLINE PxU8* getData(const PxU32& index) const { return mBuffer+index; }
65
66 PX_FORCE_INLINE PxU32 getDefaultBufferSize() const {return mDefaultBufferSize;}
67
68 private:
69 PX_FORCE_INLINE PxU8* allocateBuffer(PxU32 size);
70
71 private:
72 PxU8* mBuffer;
73 PxU32 mCurrentBufferIndex;
74 PxU32 mCurrentBufferSize;
75 PxU32 mDefaultBufferSize;
76 PxU32 mLastBufferIndex;
77 bool mAllocationLocked;
78 };
79
80 } // namespace Sc
81
83
84 PX_FORCE_INLINE void Sc::ContactReportBuffer::reset()
85 {
86 mCurrentBufferIndex = 0;
87 mLastBufferIndex = 0xFFFFFFFF;
88 }
89
91
92 void Sc::ContactReportBuffer::flush()
93 {
94 mCurrentBufferIndex = 0;
95 mLastBufferIndex = 0xFFFFFFFF;
96
97 if(mCurrentBufferSize != mDefaultBufferSize)
98 {
99 PX_FREE(mBuffer);
100
101 mBuffer = allocateBuffer(mDefaultBufferSize);
102 PX_ASSERT(mBuffer);
103
104 mCurrentBufferSize = mDefaultBufferSize;
105 }
106 }
107
109
110 PxU8* Sc::ContactReportBuffer::allocateNotThreadSafe(PxU32 size, PxU32& index ,PxU32 alignment/* =16 */)
111 {
112 PX_ASSERT(PxIsPowerOfTwo(alignment));
113
114 // padding for alignment
115 PxU32 pad = ((mCurrentBufferIndex+alignment-1)&~(alignment-1)) - mCurrentBufferIndex;
116
117 index = mCurrentBufferIndex + pad;
118
119 if (index + size > mCurrentBufferSize)
120 {
121 PX_PROFILE_ZONE("ContactReportBuffer::Resize", 0);
122 if(mAllocationLocked)
123 return NULL;
124
125 PxU32 oldBufferSize = mCurrentBufferSize;
126 while(index + size > mCurrentBufferSize)
127 {
128 mCurrentBufferSize *= 2;
129 }
130
131 PxU8* tempBuffer = allocateBuffer(mCurrentBufferSize);
132
133 PxMemCopy(tempBuffer,mBuffer,oldBufferSize);
134
135 PX_FREE(mBuffer);
136
137 mBuffer = tempBuffer;
138 }
139
140
141 PxU8* ptr = mBuffer + index;
142 mLastBufferIndex = index;
143 PX_ASSERT((size_t(ptr)&(alignment-1)) == 0);
144 mCurrentBufferIndex += size + pad;
145 return ptr;
146 }
147
149
150 PxU8* Sc::ContactReportBuffer::reallocateNotThreadSafe(PxU32 size, PxU32& index ,PxU32 alignment/* =16 */, PxU32 lastIndex)
151 {
152 if(lastIndex != mLastBufferIndex)
153 {
154 return allocateNotThreadSafe(size,index,alignment);
155 }
156 else
157 {
158 mCurrentBufferIndex = mLastBufferIndex;
159 return allocateNotThreadSafe(size,index,alignment);
160 }
161 }
162
164
165 PX_FORCE_INLINE PxU8* Sc::ContactReportBuffer::allocateBuffer(PxU32 size)
166 {
167 return (static_cast<PxU8*>(PX_ALLOC(size, "ContactReportBuffer")));
168 }
169
170} // namespace physx
171
172#endif
173
Definition ScContactReportBuffer.h:40
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_FORCE_INLINE void * PxMemCopy(void *dest, const void *src, PxU32 count)
Copies the bytes of one memory block to another. The memory blocks must not overlap.
Definition PxMemory.h:83