aboutsummaryrefslogtreecommitdiff
path: root/sdk/extensions/serialization/source/NvBlastExtPxSerializerRAW.cpp
blob: 064f34a524b7c5aa1404f490a57aeec4e2b89255 (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
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2018 NVIDIA Corporation. All rights reserved.


#include "NvBlastExtSerialization.h"
#include "NvBlastExtTkSerializerRAW.h"
#include "NvBlastExtPxAsset.h"
#include "NvBlastTkAsset.h"
#include "physics/NvBlastExtPxAssetImpl.h"
#include "NvBlastIndexFns.h"
#include "NvBlastAssert.h"
#include "NvBlastExtSerializationInternal.h"

#include "PxPhysics.h"
#include "PsMemoryBuffer.h"
#include "PxIO.h"


namespace Nv
{
namespace Blast
{

// Legacy IDs
struct ExtPxSerializationLegacyID
{
	enum Enum
	{
		Asset = NVBLAST_FOURCC('B', 'P', 'X', 'A'),	//!< ExtPxAsset identifier token, used in serialization
	};
};


// Legacy object format versions
struct ExtPxSerializationLegacyAssetVersion
{
	enum Enum
	{
		/** Initial version */
		Initial,

		//	New formats must come before Count.  They should be given descriptive names with more information in comments.

		/** The number of serialized formats. */
		Count,

		/** The current version.  This should always be Count-1 */
		Current = Count - 1
	};
};


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//											Helpers/Wrappers
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class FileBufToPxInputStream final : public PxInputStream
{
public:
	FileBufToPxInputStream(PxFileBuf& filebuf) : m_filebuf(filebuf) {}

	virtual uint32_t read(void* dest, uint32_t count)
	{
		return m_filebuf.read(dest, count);
	}

private:
	FileBufToPxInputStream& operator=(const FileBufToPxInputStream&);

	PxFileBuf& m_filebuf;
};


class FileBufToPxOutputStream final : public PxOutputStream
{
public:
	FileBufToPxOutputStream(PxFileBuf& filebuf) : m_filebuf(filebuf) {}

	virtual uint32_t write(const void* src, uint32_t count) override
	{
		return m_filebuf.write(src, count);
	}

private:
	FileBufToPxOutputStream& operator=(const FileBufToPxOutputStream&);

	PxFileBuf& m_filebuf;
};


ExtPxAsset* deserializeExtPxAsset(ExtIStream& stream, TkFramework& framework, physx::PxPhysics& physics)
{
	// Read header
	struct LegacyAssetDataHeader
	{
		LegacyAssetDataHeader() : dataType(0), version(0) {}
		uint32_t dataType;
		uint32_t version;
	};
	LegacyAssetDataHeader header;
	stream >> header.dataType;
	stream >> header.version;
	NVBLAST_CHECK_ERROR(header.dataType == ExtPxSerializationLegacyID::Asset, "deserializeExtPxAsset: wrong data type in filebuf stream.", return nullptr);
	NVBLAST_CHECK_ERROR(header.version == ExtPxSerializationLegacyAssetVersion::Current, "deserializeExtPxAsset: wrong data version in filebuf stream.", return nullptr);

	// Read initial TkAsset
	TkAsset* tkAsset = deserializeTkAsset(stream, framework);
	NVBLAST_CHECK_ERROR(tkAsset != nullptr, "ExtPxAsset::deserialize: failed to deserialize TkAsset.", return nullptr);

	// Create ExtPxAsset
	ExtPxAssetImpl* asset = reinterpret_cast<ExtPxAssetImpl*>(ExtPxAsset::create(tkAsset));

	// Fill arrays
	auto& chunks = asset->getChunksArray();
	chunks.resize(tkAsset->getChunkCount());
	const uint32_t chunkCount = chunks.size();
	for (uint32_t i = 0; i < chunkCount; ++i)
	{
		ExtPxChunk& chunk = chunks[i];
		stream >> chunk.firstSubchunkIndex;
		stream >> chunk.subchunkCount;
		uint32_t val;
		stream >> val;
		chunk.isStatic = 0 != val;
	}

	auto& subchunks = asset->getSubchunksArray();
	uint32_t subchunkCount;
	stream >> subchunkCount;
	subchunks.resize(subchunkCount);
	for (uint32_t i = 0; i < subchunkCount; ++i)
	{
		ExtPxSubchunk& subchunk = subchunks[i];

		// Subchunk transform
		stream >> subchunk.transform.q.x >> subchunk.transform.q.y >> subchunk.transform.q.z >> subchunk.transform.q.w;
		stream >> subchunk.transform.p.x >> subchunk.transform.p.y >> subchunk.transform.p.z;

		// Subchunk scale
		stream >> subchunk.geometry.scale.scale.x >> subchunk.geometry.scale.scale.y >> subchunk.geometry.scale.scale.z;
		stream >> subchunk.geometry.scale.rotation.x >> subchunk.geometry.scale.rotation.y >> subchunk.geometry.scale.rotation.z >> subchunk.geometry.scale.rotation.w;

		uint32_t convexReuseIndex;
		stream >> convexReuseIndex;
		if (isInvalidIndex(convexReuseIndex))
		{
			physx::PsMemoryBuffer memBuf(stream.view(), stream.left());
			FileBufToPxInputStream inputStream(memBuf);
			subchunk.geometry.convexMesh = physics.createConvexMesh(inputStream);
			stream.advance(memBuf.tellRead());
		}
		else
		{
			NVBLAST_ASSERT_WITH_MESSAGE(convexReuseIndex < i, "ExtPxAsset::deserialize: wrong convexReuseIndex.");
			subchunk.geometry.convexMesh = subchunks[convexReuseIndex].geometry.convexMesh;
		}
		if (!subchunk.geometry.convexMesh)
		{
			NVBLAST_LOG_ERROR("ExtPxAsset::deserialize: failed to deserialize convex mesh.");
			return nullptr;
		}
	}

	// checking if it's the end, so it will be binary compatible with asset before m_defaultActorDesc was added
	if (!stream.eof())
	{
		auto& defaultActorDesc = asset->getDefaultActorDesc();

		stream >> defaultActorDesc.uniformInitialBondHealth;
		stream >> defaultActorDesc.uniformInitialLowerSupportChunkHealth;

		auto& bondHealths = asset->getBondHealthsArray();
		uint32_t bondHealthCount;
		stream >> bondHealthCount;
		bondHealths.resize(bondHealthCount);
		for (uint32_t i = 0; i < bondHealths.size(); ++i)
		{
			stream >> bondHealths[i];
		}
		defaultActorDesc.initialBondHealths = bondHealthCount ? bondHealths.begin() : nullptr;

		auto& supportChunkHealths = asset->getSupportChunkHealthsArray();
		uint32_t supportChunkHealthCount;
		stream >> supportChunkHealthCount;
		supportChunkHealths.resize(supportChunkHealthCount);
		for (uint32_t i = 0; i < supportChunkHealths.size(); ++i)
		{
			stream >> supportChunkHealths[i];
		}
		defaultActorDesc.initialSupportChunkHealths = supportChunkHealthCount ? supportChunkHealths.begin() : nullptr;
	}

	return asset;
}

}	// namespace Blast
}	// namespace Nv