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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
|
//
// 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/PxErrorCallback.h"
#include "SnConvX.h"
#include "serialization/SnSerialUtils.h"
#include "PsAlloca.h"
#include "CmUtils.h"
#include "PxDefaultStreams.h"
#include <assert.h>
using namespace physx;
using namespace physx::Sn;
using namespace Cm;
void Sn::ConvX::resetConvexFlags()
{
mConvexFlags.clear();
}
void Sn::ConvX::_enumerateFields(const MetaClass* mc, ExtraDataEntry2* entries, int& nb, int baseOffset, MetaDataType type) const
{
PxU32 nbFields = mc->mFields.size();
int offsetCheck = baseOffset;
for(PxU32 j=0;j<nbFields;j++)
{
const PxMetaDataEntry& entry = mc->mFields[j];
if(entry.mFlags & PxMetaDataFlag::eCLASS || entry.mFlags & PxMetaDataFlag::eEXTRA_DATA)
continue;
assert(offsetCheck == baseOffset + entry.mOffset);
int currentOffset = baseOffset + entry.mOffset;
//for(int c=0;c<entry.mCount;c++)
{
if(entry.mFlags & PxMetaDataFlag::eUNION)
{
entries[nb].entry = entry;
entries[nb].offset = currentOffset;
entries[nb].cb = 0;
nb++;
}
else if(entry.mFlags & PxMetaDataFlag::ePTR) // This also takes care of the vtable pointer
{
entries[nb].entry = entry;
entries[nb].offset = currentOffset;
entries[nb].cb = &Sn::ConvX::convertPtr;
nb++;
}
else
{
MetaClass* fieldType = getMetaClass(entry.mType, type);
assert(fieldType);
if(fieldType->mCallback)
{
entries[nb].entry = entry;
entries[nb].offset = currentOffset;
entries[nb].cb = fieldType->mCallback;
nb++;
}
else
{
for(int c=0;c<entry.mCount;c++)
{
_enumerateFields(fieldType, entries, nb, currentOffset, type);
currentOffset += entry.mSize/entry.mCount;
}
}
}
}
offsetCheck += entry.mSize;
}
}
void Sn::ConvX::_enumerateExtraData(const char* address, const MetaClass* mc, ExtraDataEntry* entries,
int& nb, int offset, MetaDataType type) const
{
PxU32 nbFields = mc->mFields.size();
for(PxU32 j=0;j<nbFields;j++)
{
const PxMetaDataEntry& entry = mc->mFields[j];
if(entry.mFlags & PxMetaDataFlag::eCLASS /*|| entry.mFlags & PxMetaDataFlag::ePTR*/ || entry.mFlags & PxMetaDataFlag::eTYPEDEF)
continue;
const char* entryType = entry.mType;
//
// Insanely Twisted Shadow GeometryUnion
//
// Special code is needed as long as there are no meta data tags to describe our unions properly. The way it is done here is
// not future-proof at all. There should be a tag to describe where the union type can be found and the number of bytes
// this type id needs. Then a mapping needs to get added from each union type id to the proper meta class name.
//
if (entry.mFlags & PxMetaDataFlag::eUNION)
{
if (!mc->mClassName || strcmp(mc->mClassName, "Gu::GeometryUnion")!=0)
continue;
else
{
// ### hardcoded bit here, will only work when union type is the first int of the struct
const int* tmp = reinterpret_cast<const int*>(address + offset);
const int unionType = *tmp;
ConvX* tmpConv = const_cast<ConvX*>(this); // ... don't ask
const char* typeName = tmpConv->getTypeName(entry.mType, unionType);
assert(typeName);
bool isTriMesh = (strcmp(typeName, "PxTriangleMeshGeometryLL") == 0);
bool isHeightField = (strcmp(typeName, "PxHeightFieldGeometryLL") == 0);
if (!isTriMesh && !isHeightField)
{
continue;
}
else
{
entryType = typeName;
}
}
}
// MetaClass* extraDataType = getMetaClass(entry.mType, type);
// if(!extraDataType)
// continue;
if(entry.mFlags & PxMetaDataFlag::eEXTRA_DATA)
{
entries[nb].entry = entry;
entries[nb].offset = offset+entry.mOffset;
nb++;
}
else
{
if(entry.mFlags & PxMetaDataFlag::ePTR)
continue;
MetaClass* extraDataType = getMetaClass(entryType, type);
if(!extraDataType)
continue;
if(!extraDataType->mCallback)
_enumerateExtraData(address, extraDataType, entries, nb, offset+entry.mOffset, type);
}
}
}
PxU64 Sn::ConvX::read64(const void*& buffer)
{
const PxU64* buf64 = reinterpret_cast<const PxU64*>(buffer);
buffer = reinterpret_cast<const void*>(size_t(buffer) + sizeof(PxU64));
PxU64 value = *buf64;
output(value);
return value;
}
int Sn::ConvX::read32(const void*& buffer)
{
const int* buf32 = reinterpret_cast<const int*>(buffer);
buffer = reinterpret_cast<const void*>(size_t(buffer) + sizeof(int));
int value = *buf32;
output(value);
return value;
}
short Sn::ConvX::read16(const void*& buffer)
{
const short* buf16 = reinterpret_cast<const short*>(buffer);
buffer = reinterpret_cast<const void*>(size_t(buffer) + sizeof(short));
short value = *buf16;
output(value);
return value;
}
#if PX_CHECKED
extern const char* gVTable;
static bool compareEntries(const ExtraDataEntry2& e0, const ExtraDataEntry2& e1)
{
if(e0.entry.isVTablePtr() && e1.entry.isVTablePtr())
return true;
if((e0.entry.mFlags & PxMetaDataFlag::eUNION) && (e1.entry.mFlags & PxMetaDataFlag::eUNION))
{
if(e0.entry.mType && e1.entry.mType)
{
// We can't compare the ptrs since they index different string tables
if(strcmp(e0.entry.mType, e1.entry.mType)==0)
return true;
}
return false;
}
if(e0.entry.mName && e1.entry.mName)
{
// We can't compare the ptrs since they index different string tables
if(strcmp(e0.entry.mName, e1.entry.mName)==0)
return true;
}
return false;
}
#endif
// TODO: optimize this
bool Sn::ConvX::convertClass(const char* buffer, const MetaClass* mc, int offset)
{
// ---- big convex surgery ----
bool convexSurgery = false;
bool foundNbVerts = false;
bool removeBigData = false;
// force reference
(void)foundNbVerts;
displayMessage(PxErrorCode::eDEBUG_INFO, "%s\n", mc->mClassName);
displayMessage(PxErrorCode::eDEBUG_INFO, "+++++++++++++++++++++++++++++++++++++++++++++\n");
if(strcmp(mc->mClassName, "ConvexMesh")==0)
{
convexSurgery = true;
}
// ---- big convex surgery ----
int nbSrcEntries = 0;
PX_ALLOCA(srcEntries, ExtraDataEntry2, 256); // ### painful ctors here
int nbDstEntries = 0;
PX_ALLOCA(dstEntries, ExtraDataEntry2, 256); // ### painful ctors here
// Find corresponding meta-class for target platform
const MetaClass* target_mc = getMetaClass(mc->mClassName, META_DATA_DST);
assert(target_mc);
if(mc->mCallback)
{
srcEntries[0].cb = mc->mCallback;
srcEntries[0].offset = offset;
srcEntries[0].entry.mType = mc->mClassName;
srcEntries[0].entry.mName = mc->mClassName;
srcEntries[0].entry.mOffset = offset;
srcEntries[0].entry.mSize = mc->mSize;
srcEntries[0].entry.mCount = 1;
srcEntries[0].entry.mFlags = 0;
nbSrcEntries = 1;
assert(target_mc->mCallback);
dstEntries[0].cb = target_mc->mCallback;
dstEntries[0].offset = offset;
dstEntries[0].entry.mType = target_mc->mClassName;
dstEntries[0].entry.mName = target_mc->mClassName;
dstEntries[0].entry.mOffset = offset;
dstEntries[0].entry.mSize = target_mc->mSize;
dstEntries[0].entry.mCount = 1;
dstEntries[0].entry.mFlags = 0;
nbDstEntries = 1;
}
else
{
nbSrcEntries = 0;
_enumerateFields(mc, srcEntries, nbSrcEntries, 0, META_DATA_SRC);
assert(nbSrcEntries<256);
nbDstEntries = 0;
_enumerateFields(target_mc, dstEntries, nbDstEntries, 0, META_DATA_DST);
assert(nbDstEntries<256);
// nb = mc->mNbEntries;
// assert(nb>=0);
// memcpy(entries, mc->mEntries, nb*sizeof(ExtraDataEntry2));
}
int srcOffsetCheck = 0;
int dstOffsetCheck = 0;
int j = 0;
// Track cases where the vtable pointer location is different for different platforms.
// The variables indicate whether a platform has a vtable pointer entry that has not been converted yet
// and they will remember the index of the corrssponding entry. This works because there can only
// be one open vtable pointer entry at a time.
int srcOpenVTablePtrEntry = -1;
int dstOpenVTablePtrEntry = -1;
//if the src and dst platform place the vtable pointers at different locations some fiddling with the iteration count can be necessary.
int addVTablePtrShiftIteration = 0;
const int maxNb = nbSrcEntries > nbDstEntries ? nbSrcEntries : nbDstEntries;
for(int i=0; i < (maxNb + addVTablePtrShiftIteration); i++)
{
if (i < nbSrcEntries)
{
displayMessage(PxErrorCode::eDEBUG_INFO, "\t0x%p\t%02x\t%d\t%d\t%s", buffer + srcOffsetCheck,
static_cast<unsigned char>(buffer[srcOffsetCheck]), srcOffsetCheck, srcEntries[i].entry.mOffset, srcEntries[i].entry.mName);
for (int byteCount = 1; byteCount < srcEntries[i].entry.mSize; ++byteCount)
displayMessage(PxErrorCode::eDEBUG_INFO, "\t0x%p\t%02x\t%d\t%d\t.", buffer + srcOffsetCheck + byteCount,
static_cast<unsigned char>(buffer[srcOffsetCheck + byteCount]), srcOffsetCheck + byteCount, srcEntries[i].entry.mOffset + byteCount);
}
bool handlePadding = true;
bool skipLoop = false;
while(handlePadding)
{
const int pad0 = i<nbSrcEntries ? srcEntries[i].entry.mFlags & PxMetaDataFlag::ePADDING : 0;
const int pad1 = j<nbDstEntries ? dstEntries[j].entry.mFlags & PxMetaDataFlag::ePADDING : 0;
if(pad0 || pad1)
{
if(pad0)
{
#if PX_CHECKED
if (mMarkedPadding && (strcmp(srcEntries[i].entry.mType, "paddingByte")==0))
if(!checkPaddingBytes(buffer + srcOffsetCheck, srcEntries[i].entry.mSize))
{
if(i>0)
{
displayMessage(PxErrorCode::eDEBUG_WARNING,
"PxBinaryConverter warning: Bytes after %s::%s don't look like padding bytes. Likely mismatch between binary data and metadata.\n",
mc->mClassName, srcEntries[i-1].entry.mName );
}
else
displayMessage(PxErrorCode::eDEBUG_WARNING,
"PxBinaryConverter warning: Bytes after %s don't look like padding bytes. Likely mismatch between binary data and metadata.\n",
mc->mClassName);
}
#endif
if(pad1)
{
// Both have padding
// ### check sizes, output bytes
if(srcEntries[i].entry.mSize==dstEntries[j].entry.mSize)
{
// I guess we can just go on with the normal code here
handlePadding = false;
}
else
{
// Output padding
assert(srcEntries[i].cb);
assert(srcEntries[i].offset == srcOffsetCheck);
const int padSize = dstEntries[j].entry.mSize;
char* paddingBytes = reinterpret_cast<char*>(PX_ALLOC(sizeof(char)*padSize, "paddingByte"));
memset(paddingBytes, 0, size_t(padSize));
assert(dstEntries[j].cb);
(this->*dstEntries[j].cb)(paddingBytes, dstEntries[j].entry, dstEntries[j].entry);
assert(dstOffsetCheck==dstEntries[j].offset);
dstOffsetCheck += padSize;
PX_FREE(paddingBytes);
// srcEntries[i].cb(buffer+srcOffsetCheck, srcEntries[i].entry, dstEntries[j].entry);
// assert(dstOffsetCheck==dstEntries[j].offset);
// dstOffsetCheck += dstEntries[j].entry.mSize;
srcOffsetCheck += srcEntries[i].entry.mSize;
// Skip dest padding field
j++;
// continue; // ### BUG, doesn't go back to the "for"
skipLoop = true;
handlePadding = false;
}
}
else
{
// Src has padding, dst has not => skip conversion
// Don't increase j
skipLoop = true;
handlePadding = false;
srcOffsetCheck += srcEntries[i].entry.mSize;
}
}
else
{
if(pad1)
{
// Dst has padding, src has not
// Output padding
const int padSize = dstEntries[j].entry.mSize;
char* paddingBytes = reinterpret_cast<char*>(PX_ALLOC(sizeof(char)*padSize, "paddingByte"));
memset(paddingBytes, 0, size_t(padSize));
assert(dstEntries[j].cb);
(this->*dstEntries[j].cb)(paddingBytes, dstEntries[j].entry, dstEntries[j].entry);
assert(dstOffsetCheck==dstEntries[j].offset);
dstOffsetCheck += padSize;
PX_FREE(paddingBytes);
// Skip dest padding field, keep same src field
j++;
}
else
{
assert(0);
}
}
}
else handlePadding = false;
}
if(skipLoop)
continue;
int modSrcOffsetCheck = srcOffsetCheck;
const ExtraDataEntry2* srcEntryPtr = &srcEntries[i];
const ExtraDataEntry2* dstEntryPtr = &dstEntries[j];
bool isSrcVTablePtr = (i < nbSrcEntries) ? srcEntryPtr->entry.isVTablePtr() : false;
if (isSrcVTablePtr && (dstOpenVTablePtrEntry != -1))
{
// vtable ptr position mismatch:
// this check is necessary to align src and dst index again when the
// dst vtable pointer has been written already and the src vtable ptr
// element is reached.
//
// i
// src: | a | b | vt-ptr | c | ...
// dst: | vt-ptr | a | b | c | ...
// j
//
// it needs special treatment because the following case fails otherwise
// i
// src: | a | b | vt-ptr | c | vt-ptr | ...
// dst: | vt-ptr | a | b | vt-ptr | c | ...
// j
//
// This entry has been written already -> advance to next src entry
//
srcOffsetCheck += srcEntryPtr->entry.mSize;
i++;
isSrcVTablePtr = (i < nbSrcEntries) ? srcEntryPtr->entry.isVTablePtr() : false;
PX_ASSERT(dstOpenVTablePtrEntry < nbDstEntries);
PX_ASSERT(dstEntries[dstOpenVTablePtrEntry].entry.isVTablePtr());
dstOpenVTablePtrEntry = -1;
PX_ASSERT(addVTablePtrShiftIteration == 0);
}
bool isDstVTablePtr = (j < nbDstEntries) ? dstEntryPtr->entry.isVTablePtr() : false;
if (isDstVTablePtr && (srcOpenVTablePtrEntry != -1))
{
// i
// src: | vt-ptr | a | b | c | ...
// dst: | a | b | vt-ptr | c | ...
// j
i--; // next iteration the current element should get processed
isSrcVTablePtr = true;
PX_ASSERT(srcOpenVTablePtrEntry < nbSrcEntries);
srcEntryPtr = &srcEntries[srcOpenVTablePtrEntry];
PX_ASSERT(srcEntryPtr->entry.isVTablePtr());
modSrcOffsetCheck = srcEntryPtr->offset;
srcOffsetCheck -= srcEntryPtr->entry.mSize; // to make sure total change is 0 after this iteration
srcOpenVTablePtrEntry = -1;
PX_ASSERT(addVTablePtrShiftIteration == 1);
addVTablePtrShiftIteration = 0;
}
if(i==nbSrcEntries && j==nbDstEntries)
{
PX_ASSERT((srcOpenVTablePtrEntry == -1) && (dstOpenVTablePtrEntry == -1));
break;
}
if (isSrcVTablePtr || isDstVTablePtr)
{
if (!isSrcVTablePtr)
{
// i
// src: | a | b | vt-ptr | c | ...
// dst: | vt-ptr | a | b | c | ...
// j
PX_ASSERT(dstOpenVTablePtrEntry == -1); // the other case should be detected and treated earlier
PX_ASSERT(srcOpenVTablePtrEntry == -1);
PX_ASSERT(addVTablePtrShiftIteration == 0);
int k;
for(k=i+1; k < nbSrcEntries; k++)
{
if (srcEntries[k].entry.isVTablePtr())
break;
}
PX_ASSERT(k < nbSrcEntries);
srcEntryPtr = &srcEntries[k];
modSrcOffsetCheck = srcEntryPtr->offset;
srcOffsetCheck -= srcEntryPtr->entry.mSize; // to make sure total change is 0 after this iteration
dstOpenVTablePtrEntry = j;
i--; // to make sure the original entry gets processed in the next iteration
}
else if (!isDstVTablePtr)
{
// i ---> i
// src: | vt-ptr | a | b | c | ...
// dst: | a | b | vt-ptr | c | ...
// j
PX_ASSERT(srcOpenVTablePtrEntry == -1); // the other case should be detected and treated earlier
PX_ASSERT(dstOpenVTablePtrEntry == -1);
PX_ASSERT(addVTablePtrShiftIteration == 0);
srcOffsetCheck += srcEntryPtr->entry.mSize;
modSrcOffsetCheck = srcOffsetCheck;
srcOpenVTablePtrEntry = i;
i++;
srcEntryPtr = &srcEntries[i];
addVTablePtrShiftIteration = 1; // additional iteration might be needed to process vtable pointer at the end of a class
PX_ASSERT((i < nbSrcEntries) && ((srcEntryPtr->entry.mFlags & PxMetaDataFlag::ePADDING) == 0));
// if the second check fails, this whole section might have to be done before the padding bytes get processed. Not sure
// what other consequences that might have though.
}
}
#if PX_CHECKED
else
{
if(!compareEntries(*srcEntryPtr, *dstEntryPtr))
{
displayMessage(PxErrorCode::eINVALID_PARAMETER, "\rConvX::convertClass: %s, src meta data and dst meta data don't match!", mc->mClassName);
return false;
}
}
#endif
const ExtraDataEntry2& srcEntry = *srcEntryPtr;
const ExtraDataEntry2& dstEntry = *dstEntryPtr;
if(srcEntry.entry.mFlags & PxMetaDataFlag::eUNION)
{
// ### hardcoded bit here, will only work when union type is the first int of the struct
const int* tmp = reinterpret_cast<const int*>(buffer + modSrcOffsetCheck);
const int unionType = *tmp;
const char* typeName = getTypeName(srcEntry.entry.mType, unionType);
assert(typeName);
MetaClass* unionMC = getMetaClass(typeName, META_DATA_SRC);
assert(unionMC);
convertClass(buffer + modSrcOffsetCheck, unionMC, 0); // ### recurse
dstOffsetCheck += dstEntry.entry.mSize;
MetaClass* targetUnionMC = getMetaClass(typeName, META_DATA_DST);
assert(targetUnionMC);
const int delta = dstEntry.entry.mSize - targetUnionMC->mSize;
char* deltaBytes = reinterpret_cast<char*>(PX_ALLOC(sizeof(char)*delta, "deltaBytes"));
memset(deltaBytes, 0, size_t(delta));
output(deltaBytes, delta); // Skip unused bytes at the end of the union
PX_FREE(deltaBytes);
srcOffsetCheck += srcEntry.entry.mSize; // do not use modSrcOffsetCheck here!
}
else
{
assert(srcEntry.cb);
assert(srcEntry.offset == modSrcOffsetCheck);
// ---- big convex surgery ----
if(convexSurgery)
{
if(strcmp(srcEntry.entry.mName, "mNbHullVertices")==0)
{
assert(srcEntry.entry.mSize==1);
const int nbVerts = int(*(buffer+modSrcOffsetCheck));
assert(!foundNbVerts);
foundNbVerts = true;
const int gaussMapLimit = getBinaryMetaData(META_DATA_DST)->getGaussMapLimit();
if(nbVerts > gaussMapLimit)
{
// We need a gauss map and we have one => keep it
}
else
{
// We don't need a gauss map and we have one => remove it
removeBigData = true;
}
}
else
{
if(removeBigData)
{
const bool isBigConvexData = strcmp(srcEntry.entry.mType, "BigConvexData")==0 ||
strcmp(srcEntry.entry.mType, "BigConvexRawData")==0;
if(isBigConvexData)
{
assert(foundNbVerts);
setNullPtr(true);
}
}
}
}
// ---- big convex surgery ----
(this->*srcEntry.cb)(buffer+modSrcOffsetCheck, srcEntry.entry, dstEntry.entry);
assert(dstOffsetCheck==dstEntry.offset);
dstOffsetCheck += dstEntry.entry.mSize;
srcOffsetCheck += srcEntry.entry.mSize; // do not use modSrcOffsetCheck here!
// ---- big convex surgery ----
if(convexSurgery && removeBigData)
setNullPtr(false);
// ---- big convex surgery ----
}
j++;
}
displayMessage(PxErrorCode::eDEBUG_INFO, "---------------------------------------------\n");
while(j<nbDstEntries)
{
assert(dstEntries[j].entry.mFlags & PxMetaDataFlag::ePADDING);
if(dstEntries[j].entry.mFlags & PxMetaDataFlag::ePADDING)
{
dstOffsetCheck += dstEntries[j].entry.mSize;
}
j++;
}
assert(j==nbDstEntries);
assert(dstOffsetCheck==target_mc->mSize);
assert(srcOffsetCheck==mc->mSize);
// ---- big convex surgery ----
if(convexSurgery)
mConvexFlags.pushBack(removeBigData);
// ---- big convex surgery ----
return true;
}
// Handles data defined with PX_DEF_BIN_METADATA_EXTRA_ARRAY
const char* Sn::ConvX::convertExtraData_Array(const char* Address, const char* lastAddress, const char* objectAddress,
const ExtraDataEntry& ed)
{
(void)lastAddress;
MetaClass* mc = getMetaClass(ed.entry.mType, META_DATA_SRC);
assert(mc);
// PT: safe to cast to int here since we're reading a count.
const int count = int(peek(ed.entry.mSize, objectAddress + ed.offset, ed.entry.mFlags));
// if(ed.entry.mCount) // Reused as align value
if(ed.entry.mAlignment)
{
Address = alignStream(Address, ed.entry.mAlignment);
// Address = alignStream(Address, ed.entry.mCount);
assert(Address<=lastAddress);
}
for(int c=0;c<count;c++)
{
convertClass(Address, mc, 0);
Address += mc->mSize;
assert(Address<=lastAddress);
}
return Address;
}
const char* Sn::ConvX::convertExtraData_Ptr(const char* Address, const char* lastAddress, const PxMetaDataEntry& entry, int count,
int ptrSize_Src, int ptrSize_Dst)
{
(void)lastAddress;
PxMetaDataEntry tmpSrc = entry;
tmpSrc.mCount = count;
tmpSrc.mSize = count * ptrSize_Src;
PxMetaDataEntry tmpDst = entry;
tmpDst.mCount = count;
tmpDst.mSize = count * ptrSize_Dst;
displayMessage(PxErrorCode::eDEBUG_INFO, "extra data ptrs\n");
displayMessage(PxErrorCode::eDEBUG_INFO, "+++++++++++++++++++++++++++++++++++++++++++++\n");
displayMessage(PxErrorCode::eDEBUG_INFO, "\t0x%p\t%02x\t\t\t%s", Address, static_cast<unsigned char>(Address[0]), entry.mName);
for (int byteCount = 1; byteCount < ptrSize_Src*count; ++byteCount)
displayMessage(PxErrorCode::eDEBUG_INFO, "\t0x%p\t%02x\t\t\t.", Address + byteCount, static_cast<unsigned char>(Address[byteCount]));
convertPtr(Address, tmpSrc, tmpDst);
Address += count * ptrSize_Src;
assert(Address<=lastAddress);
return Address;
}
static bool decodeControl(PxU64 control, const ExtraDataEntry& ed, PxU64 controlMask = 0)
{
if(ed.entry.mFlags & PxMetaDataFlag::eCONTROL_FLIP)
{
if(controlMask)
{
return (control & controlMask) ? false : true;
}
else
{
return control==0;
}
}
else
{
if(controlMask)
{
return (control & controlMask) ? true : false;
}
else
{
return control!=0;
}
}
}
// ### currently hardcoded, should change
int Sn::ConvX::getConcreteType(const char* buffer)
{
MetaClass* mc = getMetaClass("PxBase", META_DATA_SRC);
assert(mc);
PxMetaDataEntry entry;
if(mc->getFieldByType("PxType", entry))
{
// PT: safe to cast to int here since we're reading our own PxType
return int(peek(entry.mSize, buffer + entry.mOffset));
}
assert(0);
return 0xffffffff;
}
struct Item : public shdfnd::UserAllocated
{
MetaClass* mc;
const char* address;
};
bool Sn::ConvX::convertCollection(const void* buffer, int fileSize, int nbObjects)
{
const char* lastAddress = reinterpret_cast<const char*>(buffer) + fileSize;
const char* Address = alignStream(reinterpret_cast<const char*>(buffer));
const int ptrSize_Src = mSrcPtrSize;
const int ptrSize_Dst = mDstPtrSize;
Item* objects = PX_NEW(Item)[PxU32(nbObjects)];
for(PxU32 i=0;i<PxU32(nbObjects);i++)
{
const float percents = float(i)/float(nbObjects);
displayMessage(PxErrorCode::eDEBUG_INFO, "Object conversion: %d%%", int(percents*100.0f));
Address = alignStream(Address);
assert(Address<=lastAddress);
PxConcreteType::Enum classType = PxConcreteType::Enum(getConcreteType(Address));
MetaClass* metaClass = getMetaClass(classType, META_DATA_SRC);
if(!metaClass)
{
PX_DELETE_ARRAY(objects);
return false;
}
objects[i].mc = metaClass;
objects[i].address = Address;
if(!convertClass(Address, metaClass, 0))
{
PX_DELETE_ARRAY(objects);
return false;
}
Address += metaClass->mSize;
assert(Address<=lastAddress);
}
// Fields / extra data
if(1)
{
// ---- big convex surgery ----
unsigned int nbConvexes = 0;
// ---- big convex surgery ----
//const char* StartAddress2 = Address;
//int startDstSize2 = getCurrentOutputSize();
for(int i=0;i<nbObjects;i++)
{
//const char* StartAddress = Address;
//int startDstSize = getCurrentOutputSize();
const float percents = float(i)/float(nbObjects);
displayMessage(PxErrorCode::eDEBUG_INFO, "Extra data conversion: %d%%", int(percents*100.0f));
MetaClass* mc0 = objects[i].mc;
const char* objectAddress = objects[i].address;
// printf("%d: %s\n", i, mc->mClassName);
// if(strcmp(mc->mClassName, "TriangleMesh")==0)
// if(strcmp(mc->mClassName, "NpRigidDynamic")==0)
if(strcmp(mc0->mClassName, "HybridModel")==0)
{
int stop=1;
(void)(stop);
}
// ### we actually need to collect all extra data for this class, including data from embedded members.
PX_ALLOCA(entries, ExtraDataEntry, 256);
int nbEntries = 0;
_enumerateExtraData(objectAddress, mc0, entries, nbEntries, 0, META_DATA_SRC);
assert(nbEntries<256);
Address = alignStream(Address);
assert(Address<=lastAddress);
for(int j=0;j<nbEntries;j++)
{
const ExtraDataEntry& ed = entries[j];
assert(ed.entry.mFlags & PxMetaDataFlag::eEXTRA_DATA);
if(ed.entry.mFlags & PxMetaDataFlag::eEXTRA_ITEM)
{
// ---- big convex surgery ----
if(1)
{
const bool isBigConvexData = strcmp(ed.entry.mType, "BigConvexData")==0;
if(isBigConvexData)
{
assert(nbConvexes<mConvexFlags.size());
if(mConvexFlags[nbConvexes++])
setNoOutput(true);
}
}
// ---- big convex surgery ----
MetaClass* extraDataType = getMetaClass(ed.entry.mType, META_DATA_SRC);
assert(extraDataType);
//sschirm: we used to have ed.entry.mOffset here, but that made cloth deserialization fail.
const char* controlAddress = objectAddress + ed.offset;
const PxU64 controlValue = peek(ed.entry.mOffsetSize, controlAddress);
if(controlValue)
{
if(ed.entry.mAlignment)
{
Address = alignStream(Address, ed.entry.mAlignment);
assert(Address<=lastAddress);
}
const char* classAddress = Address;
convertClass(Address, extraDataType, 0);
Address += extraDataType->mSize;
assert(Address<=lastAddress);
// Enumerate extra data for this optional class, and convert it too.
// This assumes the extra data for the optional class is always appended to the class itself,
// which is something we'll need to enforce in the SDK. So far this is only to handle optional
// inline arrays.
// ### this should probably be recursive eventually
PX_ALLOCA(entries2, ExtraDataEntry, 256);
int nbEntries2 = 0;
_enumerateExtraData(objectAddress, extraDataType, entries2, nbEntries2, 0, META_DATA_SRC);
assert(nbEntries2<256);
for(int k=0;k<nbEntries2;k++)
{
const ExtraDataEntry& ed2 = entries2[k];
assert(ed2.entry.mFlags & PxMetaDataFlag::eEXTRA_DATA);
if(ed2.entry.mFlags & PxMetaDataFlag::eEXTRA_ITEMS)
{
const int controlOffset = ed2.entry.mOffset;
const int controlSize = ed2.entry.mSize;
const int countOffset = ed2.entry.mCount;
const int countSize = ed2.entry.mOffsetSize;
const PxU64 controlValue2 = peek(controlSize, classAddress + controlOffset);
PxU64 controlMask = 0;
if(ed2.entry.mFlags & PxMetaDataFlag::eCONTROL_MASK)
{
controlMask = PxU64(ed2.entry.mFlags & (PxMetaDataFlag::eCONTROL_MASK_RANGE << 16));
controlMask = controlMask >> 16;
}
if(decodeControl(controlValue2, ed2, controlMask))
{
// PT: safe to cast to int here since we're reading a count
int count = int(peek(countSize, classAddress + countOffset, ed2.entry.mFlags));
if(ed2.entry.mAlignment)
{
assert(0); // Never tested
Address = alignStream(Address, ed2.entry.mAlignment);
assert(Address<=lastAddress);
}
if(ed2.entry.mFlags & PxMetaDataFlag::ePTR)
{
assert(0); // Never tested
}
else
{
MetaClass* mc = getMetaClass(ed2.entry.mType, META_DATA_SRC);
assert(mc);
while(count--)
{
convertClass(Address, mc, 0);
Address += mc->mSize;
assert(Address<=lastAddress);
}
}
}
}
else
{
if( (ed2.entry.mFlags & PxMetaDataFlag::eALIGNMENT) && ed2.entry.mAlignment)
{
Address = alignStream(Address, ed2.entry.mAlignment);
assert(Address<=lastAddress);
}
else
{
// We assume it's an normal array, e.g. the ones from "big convexes"
assert(!(ed2.entry.mFlags & PxMetaDataFlag::eEXTRA_ITEM));
Address = convertExtraData_Array(Address, lastAddress, classAddress, ed2);
}
}
}
}
else
{
int stop = 0;
(void)(stop);
}
// ---- big convex surgery ----
setNoOutput(false);
// ---- big convex surgery ----
}
else if(ed.entry.mFlags & PxMetaDataFlag::eEXTRA_ITEMS)
{
// PX_DEF_BIN_METADATA_EXTRA_ITEMS
int reloc = ed.offset - ed.entry.mOffset; // ### because the enum code only fixed the "controlOffset"!
const int controlOffset = ed.entry.mOffset;
const int controlSize = ed.entry.mSize;
const int countOffset = ed.entry.mCount;
const int countSize = ed.entry.mOffsetSize;
// const int controlValue2 = peek(controlSize, objectAddress + controlOffset);
const PxU64 controlValue2 = peek(controlSize, objectAddress + controlOffset + reloc);
PxU64 controlMask = 0;
if(ed.entry.mFlags & PxMetaDataFlag::eCONTROL_MASK)
{
controlMask = PxU64(ed.entry.mFlags & (PxMetaDataFlag::eCONTROL_MASK_RANGE << 16));
controlMask = controlMask >> 16;
}
if(decodeControl(controlValue2, ed, controlMask))
{
// PT: safe to cast to int here since we're reading a count
// int count = peek(countSize, objectAddress + countOffset); // ###
int count = int(peek(countSize, objectAddress + countOffset + reloc, ed.entry.mFlags)); // ###
if(ed.entry.mAlignment)
{
Address = alignStream(Address, ed.entry.mAlignment);
assert(Address<=lastAddress);
}
if(ed.entry.mFlags & PxMetaDataFlag::ePTR)
{
Address = convertExtraData_Ptr(Address, lastAddress, ed.entry, count, ptrSize_Src, ptrSize_Dst);
}
else
{
MetaClass* mc = getMetaClass(ed.entry.mType, META_DATA_SRC);
assert(mc);
while(count--)
{
convertClass(Address, mc, 0);
Address += mc->mSize;
assert(Address<=lastAddress);
}
}
}
}
else if(ed.entry.mFlags & PxMetaDataFlag::eALIGNMENT)
{
if(ed.entry.mAlignment)
{
displayMessage(PxErrorCode::eDEBUG_INFO, " align to %d bytes\n", ed.entry.mAlignment);
displayMessage(PxErrorCode::eDEBUG_INFO, "---------------------------------------------\n");
Address = alignStream(Address, ed.entry.mAlignment);
assert(Address<=lastAddress);
}
}
else if(ed.entry.mFlags & PxMetaDataFlag::eEXTRA_NAME)
{
if(ed.entry.mAlignment)
{
Address = alignStream(Address, ed.entry.mAlignment);
assert(Address<=lastAddress);
}
//get string count
MetaClass* mc = getMetaClass("PxU32", META_DATA_SRC);
assert(mc);
//safe to cast to int here since we're reading a count.
const int count = int(peek(mc->mSize, Address, 0));
displayMessage(PxErrorCode::eDEBUG_INFO, " convert %d bytes string\n", count);
convertClass(Address, mc, 0);
Address += mc->mSize;
mc = getMetaClass(ed.entry.mType, META_DATA_SRC);
assert(mc);
for(int c=0;c<count;c++)
{
convertClass(Address, mc, 0);
Address += mc->mSize;
assert(Address<=lastAddress);
}
}
else
{
Address = convertExtraData_Array(Address, lastAddress, objectAddress, ed);
}
}
}
PX_DELETE_ARRAY(objects);
assert(nbConvexes==mConvexFlags.size());
}
assert(Address==lastAddress);
return true;
}
bool Sn::ConvX::convert(const void* buffer, int fileSize)
{
// Test initial alignment
if(size_t(buffer) & (ALIGN_DEFAULT-1))
{
assert(0);
return false;
}
const int header = read32(buffer); fileSize -= 4; (void)header;
if (header != PX_MAKE_FOURCC('S','E','B','D'))
{
displayMessage(physx::PxErrorCode::eINVALID_PARAMETER,
"PxBinaryConverter: Buffer contains data with bad header indicating invalid serialized data.");
return false;
}
const int version = read32(buffer); fileSize -= 4; (void)version;
const int binaryVersion = read32(buffer); fileSize -= 4;
if (!checkCompatibility(PxU32(version), PxU32(binaryVersion)))
{
char buf[512];
getCompatibilityVersionsStr(buf, 512);
displayMessage(physx::PxErrorCode::eINVALID_PARAMETER,
"PxBinaryConverter: Buffer contains data version (%x-%d) is incompatible with this PhysX sdk.\n These versions would be compatible: %s",
version, binaryVersion, buf);
return false;
}
const int buildNumber = read32(buffer); fileSize -= 4; (void)buildNumber;
//read src platform tag and write dst platform tag according dst meta data
const int srcPlatformTag = *reinterpret_cast<const int*>(buffer);
buffer = reinterpret_cast<const void*>(size_t(buffer) + 4);
fileSize -= 4;
const int dstPlatformTag = mMetaData_Dst->getPlatformTag();
output(dstPlatformTag);
if (srcPlatformTag != mMetaData_Src->getPlatformTag())
{
displayMessage(physx::PxErrorCode::eINVALID_PARAMETER,
"PxBinaryConverter: Mismatch of platform tags of binary data and metadata:\n Binary Data: %s\n MetaData: %s\n",
getBinaryPlatformName(PxU32(srcPlatformTag)),
getBinaryPlatformName(PxU32(mMetaData_Src->getPlatformTag())));
return false;
}
//read whether input data has marked padding, and set it for the output data (since 0xcd is written into pads on conversion)
const int srcMarkedPadding = *reinterpret_cast<const int*>(buffer);
buffer = reinterpret_cast<const void*>(size_t(buffer) + 4);
fileSize -= 4;
mMarkedPadding = srcMarkedPadding != 0;
const int dstMarkedPadding = 1;
output(dstMarkedPadding);
int nbObjectsInCollection;
buffer = convertReferenceTables(buffer, fileSize, nbObjectsInCollection);
if(!buffer)
return false;
bool ret = convertCollection(buffer, fileSize, nbObjectsInCollection);
mMarkedPadding = false;
return ret;
}
// PT: code below added to support 64bit-to-32bit conversions
void Sn::ConvX::exportIntAsPtr(int value)
{
const int ptrSize_Src = mSrcPtrSize;
const int ptrSize_Dst = mDstPtrSize;
PxMetaDataEntry entry;
const char* address = NULL;
const PxU32 value32 = PxU32(value);
const PxU64 value64 = PxU64(value)&0xffffffff;
if(ptrSize_Src==4)
{
address = reinterpret_cast<const char*>(&value32);
}
else if(ptrSize_Src==8)
{
address = reinterpret_cast<const char*>(&value64);
}
else assert(0);
convertExtraData_Ptr(address, address + ptrSize_Src, entry, 1, ptrSize_Src, ptrSize_Dst);
}
void Sn::ConvX::exportInt(int value)
{
output(value);
}
void Sn::ConvX::exportInt64(PxU64 value)
{
output(value);
}
PointerRemap::PointerRemap()
{
}
PointerRemap::~PointerRemap()
{
}
bool PointerRemap::checkRefIsNotUsed(PxU32 ref) const
{
const PxU32 size = mData.size();
for(PxU32 i=0;i<size;i++)
{
if(mData[i].id==ref)
return false;
}
return true;
}
void PointerRemap::setObjectRef(PxU64 object64, PxU32 ref)
{
const PxU32 size = mData.size();
for(PxU32 i=0;i<size;i++)
{
if(mData[i].object==object64)
{
mData[i].id = ref;
return;
}
}
InternalData data;
data.object = object64;
data.id = ref;
mData.pushBack(data);
}
bool PointerRemap::getObjectRef(PxU64 object64, PxU32& ref) const
{
const PxU32 size = mData.size();
for(PxU32 i=0;i<size;i++)
{
if(mData[i].object==object64)
{
ref = mData[i].id;
return true;
}
}
return false;
}
/**
Converting the PxBase object offsets in the manifest table is fairly complicated now.
It would be good to have an easy callback mechanism for custom things like this.
*/
const void* Sn::ConvX::convertManifestTable(const void* buffer, int& fileSize)
{
PxU32 padding = getPadding(size_t(buffer), ALIGN_DEFAULT);
buffer = alignStream(reinterpret_cast<const char*>(buffer));
fileSize -= padding;
int nb = read32(buffer);
fileSize -= 4;
MetaClass* mc_src = getMetaClass("Sn::ManifestEntry", META_DATA_SRC);
assert(mc_src);
MetaClass* mc_dst = getMetaClass("Sn::ManifestEntry", META_DATA_DST);
assert(mc_dst);
bool mdOk;
PxMetaDataEntry srcTypeField;
mdOk = mc_src->getFieldByName("type", srcTypeField);
PX_UNUSED(mdOk);
PX_ASSERT(mdOk);
PxMetaDataEntry dstOffsetField;
mdOk = mc_dst->getFieldByName("offset", dstOffsetField);
PX_ASSERT(mdOk);
const char* address = reinterpret_cast<const char*>(buffer);
PxU32 headerOffset = 0;
for(int i=0;i<nb;i++)
{
PxConcreteType::Enum classType = PxConcreteType::Enum(peek(srcTypeField.mSize, address + srcTypeField.mOffset));
//convert ManifestEntry but output to tmpStream
PxDefaultMemoryOutputStream tmpStream;
{
//backup output state
PxOutputStream* outStream = mOutStream;
PxU32 outputSize = PxU32(mOutputSize);
mOutStream = &tmpStream;
mOutputSize = 0;
convertClass(address, mc_src, 0);
PX_ASSERT(tmpStream.getSize() == PxU32(mc_dst->mSize));
//restore output state
mOutStream = outStream;
mOutputSize = int(outputSize);
}
//output patched offset
PX_ASSERT(dstOffsetField.mOffset == 0); //assuming offset is the first data
output(int(headerOffset));
//output rest of ManifestEntry
PxU32 restSize = PxU32(mc_dst->mSize - dstOffsetField.mSize);
mOutStream->write(tmpStream.getData() + dstOffsetField.mSize, restSize);
mOutputSize += restSize;
//increment source stream
address += mc_src->mSize;
fileSize -= mc_src->mSize;
assert(fileSize>=0);
//update headerOffset using the type and dst meta data of the type
MetaClass* mc_classType_dst = getMetaClass(classType, META_DATA_DST);
if(!mc_classType_dst)
return NULL;
headerOffset += getPadding(size_t(mc_classType_dst->mSize), PX_SERIAL_ALIGN) + mc_classType_dst->mSize;
}
output(int(headerOffset)); //endoffset
buffer = address + 4;
fileSize -= 4;
return buffer;
}
const void* Sn::ConvX::convertImportReferences(const void* buffer, int& fileSize)
{
PxU32 padding = getPadding(size_t(buffer), ALIGN_DEFAULT);
buffer = alignStream(reinterpret_cast<const char*>(buffer));
fileSize -= padding;
int nb = read32(buffer);
fileSize -= 4;
if(!nb)
return buffer;
MetaClass* mc = getMetaClass("Sn::ImportReference", META_DATA_SRC);
assert(mc);
const char* address = reinterpret_cast<const char*>(buffer);
for(int i=0;i<nb;i++)
{
convertClass(address, mc, 0);
address += mc->mSize;
fileSize -= mc->mSize;
assert(fileSize>=0);
}
return address;
}
const void* Sn::ConvX::convertExportReferences(const void* buffer, int& fileSize)
{
PxU32 padding = getPadding(size_t(buffer), ALIGN_DEFAULT);
buffer = alignStream(reinterpret_cast<const char*>(buffer));
fileSize -= padding;
int nb = read32(buffer);
fileSize -= 4;
if(!nb)
return buffer;
MetaClass* mc = getMetaClass("Sn::ExportReference", META_DATA_SRC);
assert(mc);
const char* address = reinterpret_cast<const char*>(buffer);
for(int i=0;i<nb;i++)
{
convertClass(address, mc, 0);
address += mc->mSize;
fileSize -= mc->mSize;
assert(fileSize>=0);
}
return address;
}
const void* Sn::ConvX::convertInternalReferences(const void* buffer, int& fileSize)
{
PxU32 padding = getPadding(size_t(buffer), ALIGN_DEFAULT);
buffer = alignStream(reinterpret_cast<const char*>(buffer));
fileSize -= padding;
//pointer references
int nbPtrReferences = read32(buffer);
fileSize -= 4;
if(nbPtrReferences)
{
const char* address = reinterpret_cast<const char*>(buffer);
MetaClass* mc = getMetaClass("Sn::InternalReferencePtr", META_DATA_SRC);
assert(mc);
for(int i=0;i<nbPtrReferences;i++)
{
convertClass(address, mc, 0);
address += mc->mSize;
fileSize -= mc->mSize;
assert(fileSize>=0);
}
buffer = address;
}
//index references
int nbIdxReferences = read32(buffer);
fileSize -= 4;
if (nbIdxReferences)
{
const char* address = reinterpret_cast<const char*>(buffer);
MetaClass* mc = getMetaClass("Sn::InternalReferenceIdx", META_DATA_SRC);
assert(mc);
for(int i=0;i<nbIdxReferences;i++)
{
convertClass(address, mc, 0);
address += mc->mSize;
fileSize -= mc->mSize;
assert(fileSize>=0);
}
buffer = address;
}
return buffer;
}
const void* Sn::ConvX::convertReferenceTables(const void* buffer, int& fileSize, int& nbObjectsInCollection)
{
// PT: the map should not be used while creating it, so use one indirection
mActiveRemap = NULL;
mRemap.mData.clear();
mPointerRemapCounter = 0;
PxU32 padding = getPadding(size_t(buffer), ALIGN_DEFAULT);
buffer = alignStream(reinterpret_cast<const char*>(buffer));
fileSize -= padding;
nbObjectsInCollection = read32(buffer);
if (nbObjectsInCollection == 0)
displayMessage(PxErrorCode::eDEBUG_INFO, "\n\nConverting empty collection!\n\n");
fileSize -= 4;
buffer = convertManifestTable(buffer, fileSize);
if(!buffer)
return NULL;
buffer = convertImportReferences(buffer, fileSize);
buffer = convertExportReferences(buffer, fileSize);
buffer = convertInternalReferences(buffer, fileSize);
// PT: the map can now be used
mActiveRemap = &mRemap;
return buffer;
}
bool Sn::ConvX::checkPaddingBytes(const char* buffer, int byteCount)
{
const unsigned char* src = reinterpret_cast<const unsigned char*>(buffer);
int i = 0;
while ((i < byteCount) && (src[i] == 0xcd))
i++;
return (i == byteCount);
}
|