aboutsummaryrefslogtreecommitdiff
path: root/PhysX_3.4/Source/PhysXExtensions/src/serialization/Binary/SnConvX_Output.cpp
blob: b602526ad6d274a29f249ce31ff0e9d62d43d33a (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//
// 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.

#include "foundation/PxIO.h"
#include "foundation/PxErrorCallback.h"
#include "SnConvX.h"

#if PX_VC
#pragma warning(disable:4389)	// signed/unsigned mismatch
#endif

using namespace physx;

void Sn::ConvX::setNullPtr(bool flag)
{
	mNullPtr = flag;
}

void Sn::ConvX::setNoOutput(bool flag)
{
	mNoOutput = flag;
}

bool Sn::ConvX::initOutput(PxOutputStream& targetStream)
{
	mOutStream = &targetStream;

	mOutputSize = 0;
	mNullPtr = false;
	mNoOutput = false;

	const MetaData* srcMetaData = getBinaryMetaData(META_DATA_SRC);
	PX_ASSERT(srcMetaData);
	const MetaData* dstMetaData = getBinaryMetaData(META_DATA_DST);
	PX_ASSERT(dstMetaData);

	mSrcPtrSize = srcMetaData->getPtrSize();
	mDstPtrSize = dstMetaData->getPtrSize();

	PX_ASSERT(!srcMetaData->getFlip());
	mMustFlip = dstMetaData->getFlip();
	return true;
}

void Sn::ConvX::closeOutput()
{
	mOutStream = NULL;
}

int Sn::ConvX::getCurrentOutputSize()
{
	return mOutputSize;
}

void Sn::ConvX::output(short value)
{
	if(mNoOutput)
		return;

	if(mMustFlip)
		flip(value);

	PX_ASSERT(mOutStream);
	const size_t size = mOutStream->write(&value, 2);
	PX_ASSERT(size==2);
	mOutputSize += int(size);
}

void Sn::ConvX::output(int value)
{
	if(mNoOutput)
		return;

	if(mMustFlip)
		flip(value);

	PX_ASSERT(mOutStream);
	const size_t size = mOutStream->write(&value, 4);
	PX_ASSERT(size==4);
	mOutputSize += int(size);
}

//ntohll is a macro on apple yosemite
static PxU64 ntohll_internal(const PxU64 value)
{
	union
	{
		PxU64 ull;
		PxU8  c[8];
	} x;

	x.ull = value;

	PxU8 c = 0;
	c = x.c[0]; x.c[0] = x.c[7]; x.c[7] = c;
	c = x.c[1]; x.c[1] = x.c[6]; x.c[6] = c;
	c = x.c[2]; x.c[2] = x.c[5]; x.c[5] = c;
	c = x.c[3]; x.c[3] = x.c[4]; x.c[4] = c;

	return x.ull;
}

void Sn::ConvX::output(PxU64 value)
{
	if(mNoOutput)
		return;

	if(mMustFlip)
//		flip(value);
		value = ntohll_internal(value);

	PX_ASSERT(mOutStream);
	const size_t size = mOutStream->write(&value, 8);
	PX_ASSERT(size==8);
	mOutputSize += int(size);
}

void Sn::ConvX::output(const char* buffer, int nbBytes)
{
	if(mNoOutput)
		return;

	if(!nbBytes)
		return;

	PX_ASSERT(mOutStream);
	const PxU32 size = mOutStream->write(buffer, PxU32(nbBytes));
	PX_ASSERT(size== PxU32(nbBytes));
	mOutputSize += int(size);
}

void Sn::ConvX::convert8(const char* src, const PxMetaDataEntry& entry, const PxMetaDataEntry& dstEntry)
{
	(void)dstEntry;
	if(mNoOutput)
		return;

	PX_ASSERT(entry.mSize==1*entry.mCount);
	PX_ASSERT(mOutStream);
	PX_ASSERT(entry.mSize==dstEntry.mSize);

	const PxU32 size = mOutStream->write(src, PxU32(entry.mSize));
	PX_ASSERT(size== PxU32(entry.mSize));
	mOutputSize += int(size);
}

// This is called to convert auto-generated "padding bytes" (or so we think).
// We use a special converter to check the input bytes and issue warnings when it doesn't look like padding
void Sn::ConvX::convertPad8(const char* src, const PxMetaDataEntry& entry, const PxMetaDataEntry& dstEntry)
{
	(void)dstEntry;
	(void)src;
	if(mNoOutput)
		return;

	PX_ASSERT(entry.mSize);
	PX_ASSERT(entry.mSize==1*entry.mCount);
	PX_ASSERT(mOutStream);
	PX_ASSERT(entry.mSize==dstEntry.mSize);

	// PT: we don't output the source data on purpose, to catch missing meta-data
	// sschirm: changed that to 0xcd, so we can mark the output as "having marked pads" 
	const unsigned char b = 0xcd;
	for(int i=0;i<entry.mSize;i++)
	{
		const size_t size = mOutStream->write(&b, 1);
		(void)size;
	}
	mOutputSize += entry.mSize;
}

void Sn::ConvX::convert16(const char* src, const PxMetaDataEntry& entry, const PxMetaDataEntry& dstEntry)
{
	(void)dstEntry;
	if(mNoOutput)
		return;

	PX_ASSERT(entry.mSize==int(sizeof(short)*entry.mCount));
	PX_ASSERT(mOutStream);
	PX_ASSERT(entry.mSize==dstEntry.mSize);

	const short* data = reinterpret_cast<const short*>(src);
	for(int i=0;i<entry.mCount;i++)
	{
		short value = *data++;
		if(mMustFlip)
			flip(value);

		const size_t size = mOutStream->write(&value, sizeof(short));
		PX_ASSERT(size==sizeof(short));
		mOutputSize += int(size);
	}
}

void Sn::ConvX::convert32(const char* src, const PxMetaDataEntry& entry, const PxMetaDataEntry& dstEntry)
{
	(void)dstEntry;
	if(mNoOutput)
		return;

	PX_ASSERT(entry.mSize==int(sizeof(int)*entry.mCount));
	PX_ASSERT(mOutStream);
	PX_ASSERT(entry.mSize==dstEntry.mSize);

	const int* data = reinterpret_cast<const int*>(src);
	for(int i=0;i<entry.mCount;i++)
	{
		int value = *data++;
		if(mMustFlip)
			flip(value);

		const size_t size = mOutStream->write(&value, sizeof(int));
		PX_ASSERT(size==sizeof(int));
		mOutputSize += int(size);
	}
}

void Sn::ConvX::convert64(const char* src, const PxMetaDataEntry& entry, const PxMetaDataEntry& dstEntry)
{
	(void)dstEntry;
	if(mNoOutput)
		return;

	PX_ASSERT(entry.mSize==int(sizeof(PxU64)*entry.mCount));
	PX_ASSERT(mOutStream);
	PX_ASSERT(entry.mSize==dstEntry.mSize);

	const PxU64* data = reinterpret_cast<const PxU64*>(src);
	for(int i=0;i<entry.mCount;i++)
	{
		PxU64 value = *data++;
		if(mMustFlip)
			value = ntohll_internal(value);

		const size_t size = mOutStream->write(&value, sizeof(PxU64));
		PX_ASSERT(size==sizeof(PxU64));
		mOutputSize += int(size);
	}
}

void Sn::ConvX::convertFloat(const char* src, const PxMetaDataEntry& entry, const PxMetaDataEntry& dstEntry)
{
	(void)dstEntry;
	if(mNoOutput)
		return;

	PX_ASSERT(entry.mSize==int(sizeof(float)*entry.mCount));
	PX_ASSERT(mOutStream);
	PX_ASSERT(entry.mSize==dstEntry.mSize);

	const float* data = reinterpret_cast<const float*>(src);
	for(int i=0;i<entry.mCount;i++)
	{
		float value = *data++;
		if(mMustFlip)
			flip(value);

		const size_t size = mOutStream->write(&value, sizeof(float));
		PX_ASSERT(size==sizeof(float));
		mOutputSize += int(size);
	}
}

void Sn::ConvX::convertPtr(const char* src, const PxMetaDataEntry& entry, const PxMetaDataEntry& dstEntry)
{
	(void)dstEntry;
	if(mNoOutput)
		return;

	PX_ASSERT(entry.mSize==mSrcPtrSize*entry.mCount);
	PX_ASSERT(mOutStream);

	char buffer[16];
	for(int i=0;i<entry.mCount;i++)
	{
		PxU64 testValue=0;
		// Src pointer can be 4 or 8 bytes so we can't use "void*" here
		if(mSrcPtrSize==4)
		{
			PX_ASSERT(sizeof(PxU32)==4);
			const PxU32* data = reinterpret_cast<const PxU32*>(src);
			PxU32 value = *data++;
			src = reinterpret_cast<const char*>(data);

			if(mActiveRemap)
			{
				PxU32 ref;
				if(mActiveRemap->getObjectRef(value, ref))
				{
					value = ref;
				}
				else if(value)
				{
	//				value = 0;
					//We use the pointer of mName for its length
					// PT: on serialization mName is transformed to an index by the name manager, so we should not modify its value.
					if(!entry.mName || strcmp(entry.mName, "mName"))
						value=0x12345678;
				}
			}
			else
			{
				//we should only get here during convertReferenceTables to build up the pointer map
				PxU32 ref;
				if (mRemap.getObjectRef(value, ref))
				{
					value = ref;
				}
				else if(value)
				{
					const PxU32 remappedRef = 0x80000000 | (mPointerRemapCounter++ +1);
					mRemap.setObjectRef(value, remappedRef);
					value = remappedRef;
				}
			}

			if(mMustFlip)
				flip(value);

			if(mNullPtr)
				value = 0;

			*reinterpret_cast<PxU32*>(buffer) = value;
		}
		else
		{
			PX_ASSERT(mSrcPtrSize==8);
			PX_ASSERT(sizeof(PxU64)==8);
			const PxU64* data = reinterpret_cast<const PxU64*>(src);
			PxU64 value = *data++;
			src = reinterpret_cast<const char*>(data);

			if(mActiveRemap)
			{
				PxU32 ref;
				if(mActiveRemap->getObjectRef(value, ref))
				{
					value = ref;
				}
				else if(value)
				{
	//				value = 0;
					//We use the pointer of mName for its length
					// PT: on serialization mName is transformed to an index by the name manager, so we should not modify its value.
					if(!entry.mName || strcmp(entry.mName, "mName"))
						value=0x12345678;
				}
			}
			else
			{
				//we should only get here during convertReferenceTables to build up the pointer map
				PxU32 ref;
				if (mRemap.getObjectRef(value, ref))
				{
					value = ref;
				}
				else if(value)
				{
					const PxU32 remappedRef = 0x80000000 | (mPointerRemapCounter++ +1);
					mRemap.setObjectRef(value, remappedRef);
					value = remappedRef;
				}
			}

//			PX_ASSERT(!mMustFlip);
//			if(mMustFlip)
//				flip(value);

			if(mNullPtr)
				value = 0;

			testValue = value;

			*reinterpret_cast<PxU64*>(buffer) = value;
		}

		if(mSrcPtrSize==mDstPtrSize)
		{
			const size_t size = mOutStream->write(buffer, PxU32(mSrcPtrSize));
			PX_ASSERT(size==PxU32(mSrcPtrSize));
			mOutputSize += int(size);
		}
		else
		{
			if(mDstPtrSize>mSrcPtrSize)
			{
				// 32bit to 64bit
				PX_ASSERT(mDstPtrSize==8);
				PX_ASSERT(mSrcPtrSize==4);

				// We need to output the lower 32bits first for PC. Might be different on a 64bit console....

				// Output src ptr for the lower 32bits
				const size_t size = mOutStream->write(buffer, PxU32(mSrcPtrSize));
				PX_ASSERT(size==PxU32(mSrcPtrSize));
				mOutputSize += int(size);

				// Output zeros for the higher 32bits
				const int zero = 0;
				const size_t size0 = mOutStream->write(&zero, 4);
				PX_ASSERT(size0==4);
				mOutputSize += int(size0);
			}
			else
			{
				// 64bit to 32bit
				PX_ASSERT(mSrcPtrSize==8);
				PX_ASSERT(mDstPtrSize==4);

				// Not sure how we can safely convert 64bit ptrs to 32bit... just drop the high 32 bits?!?

				PxU32 ptr32 = *reinterpret_cast<PxU32*>(buffer);
				(void)ptr32;
				PxU32 ptr32b = PxU32(testValue);
				(void)ptr32b;

				if(mMustFlip)
					flip(ptr32b);

				// Output src ptr for the lower 32bits
				const size_t size = mOutStream->write(&ptr32b, 4);
				PX_ASSERT(size==4);
				mOutputSize += int(size);
			}
		}
	}
}