| 1 | // |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions |
| 4 | // are met: |
| 5 | // * Redistributions of source code must retain the above copyright |
| 6 | // notice, this list of conditions and the following disclaimer. |
| 7 | // * Redistributions in binary form must reproduce the above copyright |
| 8 | // notice, this list of conditions and the following disclaimer in the |
| 9 | // documentation and/or other materials provided with the distribution. |
| 10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
| 11 | // contributors may be used to endorse or promote products derived |
| 12 | // from this software without specific prior written permission. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
| 15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | // |
| 26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
| 27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
| 28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
| 29 | |
| 30 | #ifndef PXC_NPCACHE_H |
| 31 | #define PXC_NPCACHE_H |
| 32 | |
| 33 | #include "foundation/PxMemory.h" |
| 34 | |
| 35 | #include "PsIntrinsics.h" |
| 36 | #include "PxcNpCacheStreamPair.h" |
| 37 | |
| 38 | #include "PsPool.h" |
| 39 | #include "PsFoundation.h" |
| 40 | #include "GuContactMethodImpl.h" |
| 41 | #include "PsUtilities.h" |
| 42 | |
| 43 | namespace physx |
| 44 | { |
| 45 | |
| 46 | template <typename T> |
| 47 | void PxcNpCacheWrite(PxcNpCacheStreamPair& streams, |
| 48 | Gu::Cache& cache, |
| 49 | const T& payload, |
| 50 | PxU32 bytes, |
| 51 | const PxU8* data) |
| 52 | { |
| 53 | const PxU32 payloadSize = (sizeof(payload)+3)&~3; |
| 54 | cache.mCachedSize = Ps::to16(value: (payloadSize + 4 + bytes + 0xF)&~0xF); |
| 55 | |
| 56 | PxU8* ls = streams.reserve(byteCount: cache.mCachedSize); |
| 57 | cache.mCachedData = ls; |
| 58 | if(ls==NULL || (reinterpret_cast<PxU8*>(-1))==ls) |
| 59 | { |
| 60 | if(ls==NULL) |
| 61 | { |
| 62 | PX_WARN_ONCE( |
| 63 | "Reached limit set by PxSceneDesc::maxNbContactDataBlocks - ran out of buffer space for narrow phase. " |
| 64 | "Either accept dropped contacts or increase buffer size allocated for narrow phase by increasing PxSceneDesc::maxNbContactDataBlocks." ); |
| 65 | return; |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | PX_WARN_ONCE( |
| 70 | "Attempting to allocate more than 16K of contact data for a single contact pair in narrowphase. " |
| 71 | "Either accept dropped contacts or simplify collision geometry." ); |
| 72 | cache.mCachedData = NULL; |
| 73 | ls = NULL; |
| 74 | return; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | *reinterpret_cast<T*>(ls) = payload; |
| 79 | *reinterpret_cast<PxU32*>(ls+payloadSize) = bytes; |
| 80 | if(data) |
| 81 | PxMemCopy(dest: ls+payloadSize+sizeof(PxU32), src: data, count: bytes); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | template <typename T> |
| 86 | PxU8* PxcNpCacheWriteInitiate(PxcNpCacheStreamPair& streams, Gu::Cache& cache, const T& payload, PxU32 bytes) |
| 87 | { |
| 88 | PX_UNUSED(payload); |
| 89 | |
| 90 | const PxU32 payloadSize = (sizeof(payload)+3)&~3; |
| 91 | cache.mCachedSize = Ps::to16(value: (payloadSize + 4 + bytes + 0xF)&~0xF); |
| 92 | |
| 93 | PxU8* ls = streams.reserve(byteCount: cache.mCachedSize); |
| 94 | cache.mCachedData = ls; |
| 95 | if(NULL==ls || reinterpret_cast<PxU8*>(-1)==ls) |
| 96 | { |
| 97 | if(NULL==ls) |
| 98 | { |
| 99 | PX_WARN_ONCE( |
| 100 | "Reached limit set by PxSceneDesc::maxNbContactDataBlocks - ran out of buffer space for narrow phase. " |
| 101 | "Either accept dropped contacts or increase buffer size allocated for narrow phase by increasing PxSceneDesc::maxNbContactDataBlocks." ); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | PX_WARN_ONCE( |
| 106 | "Attempting to allocate more than 16K of contact data for a single contact pair in narrowphase. " |
| 107 | "Either accept dropped contacts or simplify collision geometry." ); |
| 108 | cache.mCachedData = NULL; |
| 109 | ls = NULL; |
| 110 | } |
| 111 | } |
| 112 | return ls; |
| 113 | } |
| 114 | |
| 115 | template <typename T> |
| 116 | PX_FORCE_INLINE void PxcNpCacheWriteFinalize(PxU8* ls, const T& payload, PxU32 bytes, const PxU8* data) |
| 117 | { |
| 118 | const PxU32 payloadSize = (sizeof(payload)+3)&~3; |
| 119 | *reinterpret_cast<T*>(ls) = payload; |
| 120 | *reinterpret_cast<PxU32*>(ls+payloadSize) = bytes; |
| 121 | if(data) |
| 122 | PxMemCopy(dest: ls+payloadSize+sizeof(PxU32), src: data, count: bytes); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | template <typename T> |
| 127 | PX_FORCE_INLINE PxU8* PxcNpCacheRead(Gu::Cache& cache, T*& payload) |
| 128 | { |
| 129 | PxU8* ls = cache.mCachedData; |
| 130 | payload = reinterpret_cast<T*>(ls); |
| 131 | const PxU32 payloadSize = (sizeof(T)+3)&~3; |
| 132 | return reinterpret_cast<PxU8*>(ls+payloadSize+sizeof(PxU32)); |
| 133 | } |
| 134 | |
| 135 | template <typename T> |
| 136 | const PxU8* PxcNpCacheRead2(Gu::Cache& cache, T& payload, PxU32& bytes) |
| 137 | { |
| 138 | const PxU8* ls = cache.mCachedData; |
| 139 | if(ls==NULL) |
| 140 | { |
| 141 | bytes = 0; |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | const PxU32 payloadSize = (sizeof(payload)+3)&~3; |
| 146 | payload = *reinterpret_cast<const T*>(ls); |
| 147 | bytes = *reinterpret_cast<const PxU32*>(ls+payloadSize); |
| 148 | PX_ASSERT(cache.mCachedSize == ((payloadSize + 4 + bytes+0xF)&~0xF)); |
| 149 | return reinterpret_cast<const PxU8*>(ls+payloadSize+sizeof(PxU32)); |
| 150 | } |
| 151 | |
| 152 | } |
| 153 | |
| 154 | #endif // #ifndef PXC_NPCACHE_H |
| 155 | |