aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/NvParameterized/include/BinaryHelper.h
blob: 4d67d0cca8eb1dc0d44ef8b80b96d43f059f0aef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.

#ifndef PX_SERIALIZE_BINARY_HELPER_H
#define PX_SERIALIZE_BINARY_HELPER_H

// WARNING: before doing any changes to this file
// check comments at the head of BinSerializer.cpp

#include "PxAssert.h"

#include <PsArray.h>

#include "nvparameterized/NvParameterized.h"
#include "nvparameterized/NvParameterizedTraits.h"

namespace NvParameterized
{

template<typename T> static PX_INLINE T NvMax3(T x, T y, T z)
{
	return physx::PxMax<T>(x, physx::PxMax<T>(y, z));
}

#ifndef offsetof
#	define offsetof(StructType, field) reinterpret_cast<size_t>(&((StructType *)0)->field)
#endif

// Alignment calculator
template<typename T> class GetAlignment {
	struct TestStruct {
		char _;
		T x;
	};

public:
	static const size_t value = offsetof(struct TestStruct, x);
};

// Maps C type to NvParameterized::DataType
template<typename T> struct GetDataType {
	static const DataType value = NvParameterized::TYPE_UNDEFINED;
};

// Currently we only need to distinguish PxVec2 from other 64-bit stuff
// (like uint64_t, int64_t, double, pointer) in SwapBytes

template<> struct GetDataType <physx::PxVec2> {
	static const DataType value = NvParameterized::TYPE_VEC2;
};

//Copied from NvApexStream
PX_INLINE static bool IsBigEndian()
{
	uint32_t i = 1;
	return 0 == *(char *)&i;
}

PX_INLINE static void SwapBytes(char *data, uint32_t size, NvParameterized::DataType type)
{
	// XDK compiler does not like switch here
	if( 1 == size )
	{
		// Do nothing
	}
	else if( 2 == size )
	{
		char one_byte;
		one_byte = data[0]; data[0] = data[1]; data[1] = one_byte;
	}
	else if( 4 == size )
	{
		char one_byte;
		one_byte = data[0]; data[0] = data[3]; data[3] = one_byte;
		one_byte = data[1]; data[1] = data[2]; data[2] = one_byte;
	}
	else if( 8 == size )
	{
		//Handling of PxVec2 agregate is different from 64-bit atomic types
		if( TYPE_VEC2 == type )
		{
			//PxVec2 => swap each field separately
			SwapBytes(data + 0, 4, TYPE_F32);
			SwapBytes(data + 4, 4, TYPE_F32);
		}
		else
		{
			char one_byte;
			one_byte = data[0]; data[0] = data[7]; data[7] = one_byte;
			one_byte = data[1]; data[1] = data[6]; data[6] = one_byte;
			one_byte = data[2]; data[2] = data[5]; data[5] = one_byte;
			one_byte = data[3]; data[3] = data[4]; data[4] = one_byte;
		}
	}
	else
	{
		//Generic algorithm for containers of float

		const size_t elemSize = sizeof(float); //We assume that float sizes match on both platforms
		PX_ASSERT( elemSize >= GetAlignment<float>::value ); //If alignment is non-trivial below algorithm will not work
		PX_ASSERT( size > elemSize );

		//Just swap all PxReals
		for(size_t i = 0; i < size; i += elemSize)
			SwapBytes(data + i, elemSize, TYPE_F32);
	}
}

//Convert value to platform-independent format (network byte order)
template<typename T> PX_INLINE static T Canonize(T x)
{
	if( !IsBigEndian() )
		SwapBytes((char *)&x, sizeof(T), GetDataType<T>::value);

	return x;
}

//Convert value to native format (from network byte order)
template<typename T> PX_INLINE static T Decanonize(T x)
{
	return Canonize(x);
}

//Read platform-independent value from stream and convert it to native format
template<typename T> static PX_INLINE T readAndConvert(const char *&p)
{
	T val;
	memcpy((char *)&val, p, sizeof(T));
	val = Decanonize(val);
	p += sizeof(T);

	return val;
}

//Byte array used for data serialization
//TODO: replace this with Array?
class StringBuf
{
	Traits *mTraits;
	char *mData;
	uint32_t mSize, mCapacity;

	PX_INLINE void internalAppend(const char *data, uint32_t size, bool doCopy = true)
	{
		if( 0 == size )
			return;

		if( mCapacity < mSize + size )
			reserve(physx::PxMax(mSize + size, 3 * mCapacity / 2));

		if( doCopy )
			memcpy(mData + mSize, data, size);
		else
			memset(mData + mSize, 0, size); //We want padding bytes filled with 0

		mSize += size;
	}

public:

	PX_INLINE StringBuf(Traits *traits)
		: mTraits(traits), mData(0), mSize(0), mCapacity(0)
	{}

	PX_INLINE StringBuf(const StringBuf &s)
		: mTraits(s.mTraits), mSize(s.mSize), mCapacity(s.mSize)
	{
		mData = (char *)mTraits->alloc(mSize);
		memcpy(mData, s.mData, mSize);
	}

	PX_INLINE ~StringBuf() { mTraits->free((void *)mData); }

	PX_INLINE void reserve(uint32_t newCapacity)
	{
		if( mCapacity >= newCapacity )
			return;

		char *newData = (char *)mTraits->alloc(newCapacity);
		PX_ASSERT(newData);

		if( mData )
		{
			memcpy(newData, mData, mSize);
			mTraits->free(mData);
		}

		mData = newData;
		mCapacity = newCapacity;
	}

	PX_INLINE char *getBuffer()
	{
		char *data = mData;

		mSize = mCapacity = 0;
		mData = 0;

		return data;
	}

	PX_INLINE uint32_t size() const { return mSize; }

	template< typename T > PX_INLINE void append(T x)
	{
		internalAppend((char *)&x, sizeof(T));
	}

	template< typename T > PX_INLINE void append(T *x)
	{
		PX_UNUSED(x);
		PX_ASSERT(0 && "Unable to append pointer");
	}

	PX_INLINE void appendBytes(const char *data, uint32_t size) { internalAppend(data, size); }

	PX_INLINE void skipBytes(uint32_t size) { internalAppend(0, size, false); }

	PX_INLINE char &operator [](uint32_t i)
	{
		PX_ASSERT( i < mSize );
		return mData[i];
	}

	PX_INLINE operator char *() { return mData; }

	PX_INLINE operator const char *() const { return mData; }
};

//Dictionary of strings used for binary serialization
class Dictionary
{
	struct Entry
	{
		const char *s;
		uint32_t offset;
	};

	physx::shdfnd::Array<Entry, Traits::Allocator> entries; //TODO: use hash map after DE402 is fixed

public:
	Dictionary(Traits *traits_): entries(Traits::Allocator(traits_)) {}

	uint32_t put(const char *s);

	void setOffset(const char *s, uint32_t off);

	PX_INLINE void setOffset(uint32_t i, uint32_t off) { setOffset(get(i), off); }

	uint32_t getOffset(const char *s) const;

	uint32_t getOffset(uint32_t i) const { return getOffset(get(i)); }

	const char *get(uint32_t i) const { return entries[i].s; }

	PX_INLINE uint32_t size() const { return entries.size(); }

	void serialize(StringBuf &res) const;
};

//Binary file pretty-printer (mimics xxd)
void dumpBytes(const char *data, uint32_t nbytes);

}

#endif