Main Page Class List Class Members

NvBlastExtSerializationInternal.h
Go to the documentation of this file.
1 // This code contains NVIDIA Confidential Information and is disclosed to you
2 // under a form of NVIDIA software license agreement provided separately to you.
3 //
4 // Notice
5 // NVIDIA Corporation and its licensors retain all intellectual property and
6 // proprietary rights in and to this software and related documentation and
7 // any modifications thereto. Any use, reproduction, disclosure, or
8 // distribution of this software and related documentation without an express
9 // license agreement from NVIDIA Corporation is strictly prohibited.
10 //
11 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
12 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
13 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
14 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
15 //
16 // Information and code furnished is believed to be accurate and reliable.
17 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
18 // information or for any infringement of patents or other rights of third parties that may
19 // result from its use. No license is granted by implication or otherwise under any patent
20 // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
21 // This code supersedes and replaces all information previously supplied.
22 // NVIDIA Corporation products are not authorized for use as critical
23 // components in life support devices or systems without express written approval of
24 // NVIDIA Corporation.
25 //
26 // Copyright (c) 2020 NVIDIA Corporation. All rights reserved.
27
28
29 #pragma once
30
31 #include "NvBlastExtSerialization.h"
32
33 #include <cstring>
34
35 #define ExtSerializerBoilerplate(_name, _description, _objectTypeID, _encodingID) \
36 virtual const char* getName() const override { return _name; } \
37 virtual const char* getDescription() const override { return _description; } \
38 virtual uint32_t getObjectTypeID() const override { return _objectTypeID; } \
39 virtual uint32_t getEncodingID() const override { return _encodingID; }
40
41
42 #define ExtSerializerReadOnly(_name) \
43 virtual bool isReadOnly() const override { return true; } \
44 virtual uint64_t serializeIntoBuffer \
45 ( \
46 void*& buffer, \
47 ExtSerialization::BufferProvider& bufferProvider, \
48 const void* object, \
49 uint64_t offset = 0 \
50 ) override \
51 { \
52 NVBLAST_LOG_WARNING(#_name "::serializeIntoBuffer: ExtPxAsset_RAW serializer is read-only."); \
53 NV_UNUSED(buffer); \
54 NV_UNUSED(bufferProvider); \
55 NV_UNUSED(object); \
56 NV_UNUSED(offset); \
57 return 0; \
58 }
59
60
61 #define ExtSerializerDefaultFactoryAndRelease(_classname) \
62 static ExtSerializer* create() \
63 { \
64 return NVBLAST_NEW(_classname) (); \
65 } \
66 virtual void release() override \
67 { \
68 NVBLAST_DELETE(this, _classname); \
69 }
70
71
72 namespace Nv
73 {
74 namespace Blast
75 {
76
80 class ExtSerializer
81 {
82 public:
83 virtual ~ExtSerializer() {}
84
88 virtual const char* getName() const = 0;
89
93 virtual const char* getDescription() const = 0;
94
98 virtual uint32_t getObjectTypeID() const = 0;
99
103 virtual uint32_t getEncodingID() const = 0;
104
110 virtual bool isReadOnly() const { return false; }
111
119 virtual void* deserializeFromBuffer(const void* buffer, uint64_t size) = 0;
120
130 virtual uint64_t serializeIntoBuffer(void*& buffer, ExtSerialization::BufferProvider& bufferProvider, const void* object, uint64_t offset = 0) = 0;
131
135 virtual void release() = 0;
136 };
137
138
142 class ExtSerializationInternal : public ExtSerialization
143 {
144 public:
149 virtual bool registerSerializer(ExtSerializer& serializer) = 0;
150 virtual bool unregisterSerializer(ExtSerializer& serializer) = 0;
151
160 virtual ExtSerializer* findSerializer(uint32_t objectTypeID, uint32_t encodingID) = 0;
161
163 enum { HeaderSize = 128 };
164 };
165
166
167 template<typename Factory, size_t N>
168 size_t ExtSerializationLoadSet(Nv::Blast::ExtSerializationInternal& serialization, Factory(&factories)[N])
169 {
170 size_t count = 0;
171
172 for (auto f : factories)
173 {
174 Nv::Blast::ExtSerializer* serializer = f();
175 if (serializer != nullptr)
176 {
177 if (serialization.registerSerializer(*serializer))
178 {
179 ++count;
180 }
181 else
182 {
183 NVBLAST_LOG_ERROR("Nv::Blast::ExtSerializationLoadSet: failed to register serailizer:");
184 NVBLAST_LOG_ERROR(serializer->getName());
185 serializer->release();
186 }
187 }
188 else
189 {
190 NVBLAST_LOG_ERROR("Nv::Blast::ExtSerializationLoadSet: failed to create serailizer.");
191 }
192 }
193
194 return count;
195 }
196
197
198 class ExtIStream
199 {
200 public:
201 enum Flags
202 {
203 LittleEndian = (1 << 0),
204 Fail = (1 << 1)
205 };
206
207 ExtIStream(const void* buffer, size_t size) : m_buf(reinterpret_cast<const char*>(buffer)), m_flags(0)
208 {
209 m_cur = m_buf;
210 m_end = m_buf + size;
211 const uint16_t x = LittleEndian;
212 m_flags = *reinterpret_cast<const char*>(&x);
213 }
214
215 bool advance(ptrdiff_t diff)
216 {
217 m_cur += diff;
218 if (m_cur < m_buf)
219 {
220 m_cur = m_buf;
221 m_flags |= Fail;
222 return false;
223 }
224 else
225 if (m_cur > m_end)
226 {
227 m_cur = m_end;
228 m_flags |= Fail;
229 return false;
230 }
231 return true;
232 }
233
234 const void* view()
235 {
236 return m_cur;
237 }
238
239 bool read(void* buffer, size_t size)
240 {
241 if (!canRead(size)) return false;
242 std::memcpy(buffer, m_cur, size);
243 m_cur += size;
244 return true;
245 }
246
247 size_t tellg() const { return m_cur - m_buf; }
248 size_t left() const { return m_end - m_cur; }
249
250 bool eof() const { return m_cur >= m_end; }
251 bool fail() const { return (m_flags & Fail) != 0; }
252
253 private:
254 const char* m_buf;
255 const char* m_cur;
256 const char* m_end;
257 uint32_t m_flags;
258
259 bool isLittleEndian() const { return (m_flags & LittleEndian) != 0; }
260
261 bool canRead(size_t size) const { return m_cur + size <= m_end; }
262
263 template<typename T>
264 friend ExtIStream& operator >> (ExtIStream& s, T& x);
265 };
266
267 template<typename T>
268 NV_INLINE ExtIStream& operator >> (ExtIStream& s, T& x)
269 {
270 if (s.canRead(sizeof(T)))
271 {
272 if (s.isLittleEndian())
273 {
274 x = *reinterpret_cast<const T*>(s.m_cur);
275 s.m_cur += sizeof(T);
276 }
277 else
278 {
279 char* b = reinterpret_cast<char*>(&x) + sizeof(T);
280 for (size_t n = sizeof(T); n--;) *--b = *s.m_cur++;
281 }
282 }
283 else
284 {
285 s.m_flags |= ExtIStream::Fail;
286 }
287 return s;
288 }
289
290 } // namespace Blast
291 } // namespace Nv
Nv::Blast::ExtIStream::fail
bool fail() const
Definition: NvBlastExtSerializationInternal.h:251
Nv::Blast::ExtIStream::Flags
Flags
Definition: NvBlastExtSerializationInternal.h:201
Nv::Blast::ExtSerializer::getDescription
virtual const char * getDescription() const =0
x
SIMD_FORCE_INLINE const btScalar & x() const
Return the x value.
Definition: btVector3.h:275
Nv::Blast::ExtSerializer::deserializeFromBuffer
virtual void * deserializeFromBuffer(const void *buffer, uint64_t size)=0
Nv::Blast::ExtIStream
Definition: NvBlastExtSerializationInternal.h:198
Nv::Blast::ExtIStream::view
const void * view()
Definition: NvBlastExtSerializationInternal.h:234
Nv::Blast::ExtSerializer::getObjectTypeID
virtual uint32_t getObjectTypeID() const =0
Nv::Blast::ExtSerializer::~ExtSerializer
virtual ~ExtSerializer()
Definition: NvBlastExtSerializationInternal.h:83
Nv::Blast::ExtIStream::Fail
Definition: NvBlastExtSerializationInternal.h:204
Nv::Blast::ExtSerializer::getName
virtual const char * getName() const =0
Nv::Blast::ExtIStream::eof
bool eof() const
Definition: NvBlastExtSerializationInternal.h:250
Nv::Blast::ExtSerializationInternal
Definition: NvBlastExtSerializationInternal.h:142
Nv::Blast::ExtSerializer::isReadOnly
virtual bool isReadOnly() const
Definition: NvBlastExtSerializationInternal.h:110
Nv::Blast::ExtIStream::left
size_t left() const
Definition: NvBlastExtSerializationInternal.h:248
NV_INLINE
#define NV_INLINE
Definition: NvPreprocessor.h:350
Nv::Blast::ExtSerializer::release
virtual void release()=0
Nv::Blast::ExtSerialization
Definition: NvBlastExtSerialization.h:42
Nv::Blast::ExtIStream::advance
bool advance(ptrdiff_t diff)
Definition: NvBlastExtSerializationInternal.h:215
NVBLAST_LOG_ERROR
#define NVBLAST_LOG_ERROR(_msg)
Definition: NvBlastGlobals.h:246
Nv::Blast::ExtSerialization::BufferProvider
Definition: NvBlastExtSerialization.h:56
Nv::Blast::ExtSerializer
Definition: NvBlastExtSerializationInternal.h:80
Nv::Blast::ExtSerializationLoadSet
size_t ExtSerializationLoadSet(Nv::Blast::ExtSerializationInternal &serialization, Factory(&factories)[N])
Definition: NvBlastExtSerializationInternal.h:168
Nv::Blast::ExtIStream::tellg
size_t tellg() const
Definition: NvBlastExtSerializationInternal.h:247
Nv::Blast::operator>>
NV_INLINE ExtIStream & operator>>(ExtIStream &s, T &x)
Definition: NvBlastExtSerializationInternal.h:268
Nv::Blast::ExtSerializer::getEncodingID
virtual uint32_t getEncodingID() const =0
Nv::Blast::ExtIStream::read
bool read(void *buffer, size_t size)
Definition: NvBlastExtSerializationInternal.h:239
Nv::Blast::ExtSerializer::serializeIntoBuffer
virtual uint64_t serializeIntoBuffer(void *&buffer, ExtSerialization::BufferProvider &bufferProvider, const void *object, uint64_t offset=0)=0
Nv
Definition: NvBlastArray.h:37
Nv::Blast::ExtSerializationInternal::registerSerializer
virtual bool registerSerializer(ExtSerializer &serializer)=0
NvBlastExtSerialization.h
Nv::Blast::ExtIStream::ExtIStream
ExtIStream(const void *buffer, size_t size)
Definition: NvBlastExtSerializationInternal.h:207