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
|
// 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.
#pragma once
#include "NvBlastExtSerialization.h"
#include <cstring>
#define ExtSerializerBoilerplate(_name, _description, _objectTypeID, _encodingID) \
virtual const char* getName() const override { return _name; } \
virtual const char* getDescription() const override { return _description; } \
virtual uint32_t getObjectTypeID() const override { return _objectTypeID; } \
virtual uint32_t getEncodingID() const override { return _encodingID; }
#define ExtSerializerReadOnly(_name) \
virtual bool isReadOnly() const override { return true; } \
virtual uint64_t serializeIntoBuffer \
( \
void*& buffer, \
ExtSerialization::BufferProvider& bufferProvider, \
const void* object, \
uint64_t offset = 0 \
) override \
{ \
NVBLAST_LOG_WARNING(#_name "::serializeIntoBuffer: ExtPxAsset_RAW serializer is read-only."); \
NV_UNUSED(buffer); \
NV_UNUSED(bufferProvider); \
NV_UNUSED(object); \
NV_UNUSED(offset); \
return 0; \
}
#define ExtSerializerDefaultFactoryAndRelease(_classname) \
static ExtSerializer* create() \
{ \
return NVBLAST_NEW(_classname) (); \
} \
virtual void release() override \
{ \
NVBLAST_DELETE(this, _classname); \
}
namespace Nv
{
namespace Blast
{
/**
Serializer internal interface
*/
class ExtSerializer
{
public:
virtual ~ExtSerializer() {}
/**
return the name of this serializer.
*/
virtual const char* getName() const = 0;
/**
return a description of this serializer.
*/
virtual const char* getDescription() const = 0;
/**
return an identifier for the type of object handled.
*/
virtual uint32_t getObjectTypeID() const = 0;
/**
return an identifier for serialization format.
*/
virtual uint32_t getEncodingID() const = 0;
/**
Whether or not this serializer supports writing. Legacy formats, for example, may not.
\return true iff this serialization does not support writing.
*/
virtual bool isReadOnly() const { return false; }
/**
Deserialize from a buffer into a newly allocated object.
\param[in] buffer Pointer to the buffer to read.
\param[in] size Size of the buffer to read.
\return object pointer; returns null if failed to deserialize.
*/
virtual void* deserializeFromBuffer(const void* buffer, uint64_t size) = 0;
/**
Serialize into a buffer. Allocates the buffer internally using the ExtSerialization::BufferProvider callack interface.
\param[out] buffer Pointer to the buffer created.
\param[in] bufferProvider The buffer provider callback interface to use.
\param[in] object Object pointer.
\return the number of bytes serialized into the buffer (zero if unsuccessful).
*/
virtual uint64_t serializeIntoBuffer(void*& buffer, ExtSerialization::BufferProvider& bufferProvider, const void* object, uint64_t offset = 0) = 0;
/**
Release the serializer and free associated memory.
*/
virtual void release() = 0;
};
/**
Internal serialization manager interface
*/
class ExtSerializationInternal : public ExtSerialization
{
public:
/**
Internal interfaces to register and unregister a serializer, used by modules to automatically
register all of their serializers with a serialization manager.
*/
virtual bool registerSerializer(ExtSerializer& serializer) = 0;
virtual bool unregisterSerializer(ExtSerializer& serializer) = 0;
/**
Find a registered serializer for the given object type and encoding.
\param[in] objectTypeID ID for the requested object type.
\param[in] encodingID ID for the requested encoding (see EncodingID).
\return a registered serializer if found, NULL otherwise.
*/
virtual ExtSerializer* findSerializer(uint32_t objectTypeID, uint32_t encodingID) = 0;
//// Enums ////
enum { HeaderSize = 128 };
};
template<typename Factory, size_t N>
size_t ExtSerializationLoadSet(Nv::Blast::ExtSerializationInternal& serialization, Factory(&factories)[N])
{
size_t count = 0;
for (auto f : factories)
{
Nv::Blast::ExtSerializer* serializer = f();
if (serializer != nullptr)
{
if (serialization.registerSerializer(*serializer))
{
++count;
}
else
{
NVBLAST_LOG_ERROR("Nv::Blast::ExtSerializationLoadSet: failed to register serailizer:");
NVBLAST_LOG_ERROR(serializer->getName());
serializer->release();
}
}
else
{
NVBLAST_LOG_ERROR("Nv::Blast::ExtSerializationLoadSet: failed to create serailizer.");
}
}
return count;
}
class ExtIStream
{
public:
enum Flags
{
LittleEndian = (1 << 0),
Fail = (1 << 1)
};
ExtIStream(const void* buffer, size_t size) : m_buf(reinterpret_cast<const char*>(buffer)), m_flags(0)
{
m_cur = m_buf;
m_end = m_buf + size;
const uint16_t x = LittleEndian;
m_flags = *reinterpret_cast<const char*>(&x);
}
bool advance(ptrdiff_t diff)
{
m_cur += diff;
if (m_cur < m_buf)
{
m_cur = m_buf;
m_flags |= Fail;
return false;
}
else
if (m_cur > m_end)
{
m_cur = m_end;
m_flags |= Fail;
return false;
}
return true;
}
const void* view()
{
return m_cur;
}
bool read(void* buffer, size_t size)
{
if (!canRead(size)) return false;
std::memcpy(buffer, m_cur, size);
m_cur += size;
return true;
}
size_t tellg() const { return m_cur - m_buf; }
size_t left() const { return m_end - m_cur; }
bool eof() const { return m_cur >= m_end; }
bool fail() const { return (m_flags & Fail) != 0; }
private:
const char* m_buf;
const char* m_cur;
const char* m_end;
uint32_t m_flags;
bool isLittleEndian() const { return (m_flags & LittleEndian) != 0; }
bool canRead(size_t size) const { return m_cur + size <= m_end; }
template<typename T>
friend ExtIStream& operator >> (ExtIStream& s, T& x);
};
template<typename T>
NV_INLINE ExtIStream& operator >> (ExtIStream& s, T& x)
{
if (s.canRead(sizeof(T)))
{
if (s.isLittleEndian())
{
x = *reinterpret_cast<const T*>(s.m_cur);
s.m_cur += sizeof(T);
}
else
{
char* b = reinterpret_cast<char*>(&x) + sizeof(T);
for (size_t n = sizeof(T); n--;) *--b = *s.m_cur++;
}
}
else
{
s.m_flags |= ExtIStream::Fail;
}
return s;
}
} // namespace Blast
} // namespace Nv
|