RavEngine
Loading...
Searching...
No Matches
PxcNpCache.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 PXC_NP_CACHE_H
30#define PXC_NP_CACHE_H
31
32#include "foundation/PxMemory.h"
33#include "foundation/PxIntrinsics.h"
34#include "foundation/PxPool.h"
35#include "foundation/PxUtilities.h"
36
37#include "PxcNpCacheStreamPair.h"
38
39#include "GuContactMethodImpl.h"
40
41namespace physx
42{
43
44template <typename T>
45void PxcNpCacheWrite(PxcNpCacheStreamPair& streams,
46 Gu::Cache& cache,
47 const T& payload,
48 PxU32 bytes,
49 const PxU8* data)
50{
51 const PxU32 payloadSize = (sizeof(payload)+3)&~3;
52 cache.mCachedSize = PxTo16((payloadSize + 4 + bytes + 0xF)&~0xF);
53
54 PxU8* ls = streams.reserve(cache.mCachedSize);
55 cache.mCachedData = ls;
56 if(ls==NULL || (reinterpret_cast<PxU8*>(-1))==ls)
57 {
58 if(ls==NULL)
59 {
60 PX_WARN_ONCE(
61 "Reached limit set by PxSceneDesc::maxNbContactDataBlocks - ran out of buffer space for narrow phase. "
62 "Either accept dropped contacts or increase buffer size allocated for narrow phase by increasing PxSceneDesc::maxNbContactDataBlocks.");
63 return;
64 }
65 else
66 {
67 PX_WARN_ONCE(
68 "Attempting to allocate more than 16K of contact data for a single contact pair in narrowphase. "
69 "Either accept dropped contacts or simplify collision geometry.");
70 cache.mCachedData = NULL;
71 ls = NULL;
72 return;
73 }
74 }
75
76 *reinterpret_cast<T*>(ls) = payload;
77 *reinterpret_cast<PxU32*>(ls+payloadSize) = bytes;
78 if(data)
79 PxMemCopy(ls+payloadSize+sizeof(PxU32), data, bytes);
80}
81
82
83template <typename T>
84PxU8* PxcNpCacheWriteInitiate(PxcNpCacheStreamPair& streams, Gu::Cache& cache, const T& payload, PxU32 bytes)
85{
86 PX_UNUSED(payload);
87
88 const PxU32 payloadSize = (sizeof(payload)+3)&~3;
89 cache.mCachedSize = PxTo16((payloadSize + 4 + bytes + 0xF)&~0xF);
90
91 PxU8* ls = streams.reserve(cache.mCachedSize);
92 cache.mCachedData = ls;
93 if(NULL==ls || reinterpret_cast<PxU8*>(-1)==ls)
94 {
95 if(NULL==ls)
96 {
97 PX_WARN_ONCE(
98 "Reached limit set by PxSceneDesc::maxNbContactDataBlocks - ran out of buffer space for narrow phase. "
99 "Either accept dropped contacts or increase buffer size allocated for narrow phase by increasing PxSceneDesc::maxNbContactDataBlocks.");
100 }
101 else
102 {
103 PX_WARN_ONCE(
104 "Attempting to allocate more than 16K of contact data for a single contact pair in narrowphase. "
105 "Either accept dropped contacts or simplify collision geometry.");
106 cache.mCachedData = NULL;
107 ls = NULL;
108 }
109 }
110 return ls;
111}
112
113template <typename T>
114PX_FORCE_INLINE void PxcNpCacheWriteFinalize(PxU8* ls, const T& payload, PxU32 bytes, const PxU8* data)
115{
116 const PxU32 payloadSize = (sizeof(payload)+3)&~3;
117 *reinterpret_cast<T*>(ls) = payload;
118 *reinterpret_cast<PxU32*>(ls+payloadSize) = bytes;
119 if(data)
120 PxMemCopy(ls+payloadSize+sizeof(PxU32), data, bytes);
121}
122
123
124template <typename T>
125PX_FORCE_INLINE PxU8* PxcNpCacheRead(Gu::Cache& cache, T*& payload)
126{
127 PxU8* ls = cache.mCachedData;
128 payload = reinterpret_cast<T*>(ls);
129 const PxU32 payloadSize = (sizeof(T)+3)&~3;
130 return reinterpret_cast<PxU8*>(ls+payloadSize+sizeof(PxU32));
131}
132
133template <typename T>
134const PxU8* PxcNpCacheRead2(Gu::Cache& cache, T& payload, PxU32& bytes)
135{
136 const PxU8* ls = cache.mCachedData;
137 if(ls==NULL)
138 {
139 bytes = 0;
140 return NULL;
141 }
142
143 const PxU32 payloadSize = (sizeof(payload)+3)&~3;
144 payload = *reinterpret_cast<const T*>(ls);
145 bytes = *reinterpret_cast<const PxU32*>(ls+payloadSize);
146 PX_ASSERT(cache.mCachedSize == ((payloadSize + 4 + bytes+0xF)&~0xF));
147 return reinterpret_cast<const PxU8*>(ls+payloadSize+sizeof(PxU32));
148}
149
150}
151
152#endif
Definition format.h:4067
#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