RavEngine
Loading...
Searching...
No Matches
CmSerialize.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 CM_SERIALIZE_H
30#define CM_SERIALIZE_H
31
32#include "foundation/PxSimpleTypes.h"
33#include "foundation/PxIO.h"
34#include "common/PxPhysXCommonConfig.h"
35#include "foundation/PxUtilities.h"
36
37namespace physx
38{
39 PX_INLINE void flip(PxU16& v)
40 {
41 PxU8* b = reinterpret_cast<PxU8*>(&v);
42 PxU8 temp = b[0];
43 b[0] = b[1];
44 b[1] = temp;
45 }
46
47 PX_INLINE void flip(PxI16& v)
48 {
49 PxI8* b = reinterpret_cast<PxI8*>(&v);
50 PxI8 temp = b[0];
51 b[0] = b[1];
52 b[1] = temp;
53 }
54
55 PX_INLINE void flip(PxU32& v)
56 {
57 PxU8* b = reinterpret_cast<PxU8*>(&v);
58
59 PxU8 temp = b[0];
60 b[0] = b[3];
61 b[3] = temp;
62 temp = b[1];
63 b[1] = b[2];
64 b[2] = temp;
65 }
66
67 // MS: It is important to modify the value directly and not use a temporary variable or a return
68 // value. The reason for this is that a flipped float might have a bit pattern which indicates
69 // an invalid float. If such a float is assigned to another float, the bit pattern
70 // can change again (maybe to map invalid floats to a common invalid pattern?).
71 // When reading the float and flipping again, the changed bit pattern will result in a different
72 // float than the original one.
73 PX_INLINE void flip(PxF32& v)
74 {
75 PxU8* b = reinterpret_cast<PxU8*>(&v);
76
77 PxU8 temp = b[0];
78 b[0] = b[3];
79 b[3] = temp;
80 temp = b[1];
81 b[1] = b[2];
82 b[2] = temp;
83 }
84
85 PX_INLINE void writeChunk(PxI8 a, PxI8 b, PxI8 c, PxI8 d, PxOutputStream& stream)
86 {
87 stream.write(&a, sizeof(PxI8));
88 stream.write(&b, sizeof(PxI8));
89 stream.write(&c, sizeof(PxI8));
90 stream.write(&d, sizeof(PxI8));
91 }
92
93 void readChunk(PxI8& a, PxI8& b, PxI8& c, PxI8& d, PxInputStream& stream);
94
95 PxU16 readWord(bool mismatch, PxInputStream& stream);
96 PxU32 readDword(bool mismatch, PxInputStream& stream);
97 PxF32 readFloat(bool mismatch, PxInputStream& stream);
98
99 void writeWord(PxU16 value, bool mismatch, PxOutputStream& stream);
100 void writeDword(PxU32 value, bool mismatch, PxOutputStream& stream);
101 void writeFloat(PxF32 value, bool mismatch, PxOutputStream& stream);
102
103 bool readFloatBuffer(PxF32* dest, PxU32 nbFloats, bool mismatch, PxInputStream& stream);
104 void writeFloatBuffer(const PxF32* src, PxU32 nb, bool mismatch, PxOutputStream& stream);
105 void writeWordBuffer(const PxU16* src, PxU32 nb, bool mismatch, PxOutputStream& stream);
106 void readWordBuffer(PxU16* dest, PxU32 nb, bool mismatch, PxInputStream& stream);
107 void writeWordBuffer(const PxI16* src, PxU32 nb, bool mismatch, PxOutputStream& stream);
108 void readWordBuffer(PxI16* dest, PxU32 nb, bool mismatch, PxInputStream& stream);
109 void writeByteBuffer(const PxU8* src, PxU32 nb, PxOutputStream& stream);
110 void readByteBuffer(PxU8* dest, PxU32 nb, PxInputStream& stream);
111
112 bool writeHeader(PxI8 a, PxI8 b, PxI8 c, PxI8 d, PxU32 version, bool mismatch, PxOutputStream& stream);
113 bool readHeader(PxI8 a, PxI8 b, PxI8 c, PxI8 d, PxU32& version, bool& mismatch, PxInputStream& stream);
114
115 PX_INLINE bool readIntBuffer(PxU32* dest, PxU32 nbInts, bool mismatch, PxInputStream& stream)
116 {
117 return readFloatBuffer(reinterpret_cast<PxF32*>(dest), nbInts, mismatch, stream);
118 }
119
120 PX_INLINE void writeIntBuffer(const PxU32* src, PxU32 nb, bool mismatch, PxOutputStream& stream)
121 {
122 writeFloatBuffer(reinterpret_cast<const PxF32*>(src), nb, mismatch, stream);
123 }
124
125 PX_INLINE bool ReadDwordBuffer(PxU32* dest, PxU32 nb, bool mismatch, PxInputStream& stream)
126 {
127 return readFloatBuffer(reinterpret_cast<float*>(dest), nb, mismatch, stream);
128 }
129
130 PX_INLINE void WriteDwordBuffer(const PxU32* src, PxU32 nb, bool mismatch, PxOutputStream& stream)
131 {
132 writeFloatBuffer(reinterpret_cast<const float*>(src), nb, mismatch, stream);
133 }
134
135 PxU32 computeMaxIndex(const PxU32* indices, PxU32 nbIndices);
136 PxU16 computeMaxIndex(const PxU16* indices, PxU32 nbIndices);
137 void storeIndices(PxU32 maxIndex, PxU32 nbIndices, const PxU32* indices, PxOutputStream& stream, bool platformMismatch);
138 void readIndices(PxU32 maxIndex, PxU32 nbIndices, PxU32* indices, PxInputStream& stream, bool platformMismatch);
139
140 // PT: see PX-1163
141 PX_FORCE_INLINE bool readBigEndianVersionNumber(PxInputStream& stream, bool mismatch_, PxU32& fileVersion, bool& mismatch)
142 {
143 // PT: allright this is going to be subtle:
144 // - in version 1 the data was always saved in big-endian format
145 // - *including the version number*!
146 // - so we cannot just read the version "as usual" using the passed mismatch param
147
148 // PT: mismatch value for version 1
149 mismatch = (PxLittleEndian() == 1);
150
151 const PxU32 rawFileVersion = readDword(false, stream);
152 if(rawFileVersion==1)
153 {
154 // PT: this is a version-1 file with no flip
155 fileVersion = 1;
156 PX_ASSERT(!mismatch);
157 }
158 else
159 {
160 PxU32 fileVersionFlipped = rawFileVersion;
161 flip(fileVersionFlipped);
162 if(fileVersionFlipped==1)
163 {
164 // PT: this is a version-1 file with flip
165 fileVersion = 1;
166 PX_ASSERT(mismatch);
167 }
168 else
169 {
170 // PT: this is at least version 2 so we can process it "as usual"
171 mismatch = mismatch_;
172 fileVersion = mismatch_ ? fileVersionFlipped : rawFileVersion;
173 }
174 }
175
176 PX_ASSERT(fileVersion<=3);
177 if(fileVersion>3)
178 return false;
179 return true;
180 }
181
182// PT: TODO: copied from IceSerialize.h, still needs to be refactored/cleaned up.
183namespace Cm
184{
185 bool WriteHeader(PxU8 a, PxU8 b, PxU8 c, PxU8 d, PxU32 version, bool mismatch, PxOutputStream& stream);
186 bool ReadHeader(PxU8 a_, PxU8 b_, PxU8 c_, PxU8 d_, PxU32& version, bool& mismatch, PxInputStream& stream);
187
188 void StoreIndices(PxU32 maxIndex, PxU32 nbIndices, const PxU32* indices, PxOutputStream& stream, bool platformMismatch);
189 void ReadIndices(PxU32 maxIndex, PxU32 nbIndices, PxU32* indices, PxInputStream& stream, bool platformMismatch);
190
191 void StoreIndices(PxU16 maxIndex, PxU32 nbIndices, const PxU16* indices, PxOutputStream& stream, bool platformMismatch);
192 void ReadIndices(PxU16 maxIndex, PxU32 nbIndices, PxU16* indices, PxInputStream& stream, bool platformMismatch);
193}
194
195}
196
197#endif
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39