NvBlastExtSerializationCAPN.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 "capnp/serialize.h"
32 #include "NvBlastExtInputStream.h"
33 #include "NvBlastExtOutputStream.h"
34 #include "NvBlastArray.h"
36 
37 
38 namespace Nv
39 {
40 namespace Blast
41 {
42 
43 template<typename TObject, typename TSerializationReader, typename TSerializationBuilder>
45 {
46 public:
47  static TObject* deserializeFromBuffer(const unsigned char* input, uint64_t size);
48  static TObject* deserializeFromStream(std::istream& inputStream);
49 
50  static uint64_t serializationBufferSize(const TObject* object);
51 
52  static bool serializeIntoBuffer(const TObject* object, unsigned char* buffer, uint64_t maxSize, uint64_t& usedSize);
53  static bool serializeIntoBuffer(const TObject *object, unsigned char*& buffer, uint64_t& size, ExtSerialization::BufferProvider* bufferProvider = nullptr, uint64_t offset = 0);
54  static bool serializeIntoStream(const TObject* object, std::ostream& outputStream);
55 
56 private:
57  // Specialized
58  static bool serializeIntoBuilder(TSerializationBuilder& objectBuilder, const TObject* object);
59  static bool serializeIntoMessage(capnp::MallocMessageBuilder& message, const TObject* object);
60  static TObject* deserializeFromStreamReader(capnp::InputStreamMessageReader& message);
61 };
62 
63 
64 template<typename TObject, typename TSerializationReader, typename TSerializationBuilder>
66 {
67  kj::ArrayPtr<const unsigned char> source(input, size);
68 
69  kj::ArrayInputStream inputStream(source);
70 
71  Nv::Blast::Array<uint64_t>::type scratch(static_cast<uint32_t>(size));
72  kj::ArrayPtr<capnp::word> scratchArray((capnp::word*) scratch.begin(), size);
73 
74  capnp::InputStreamMessageReader message(inputStream, capnp::ReaderOptions(), scratchArray);
75 
76  return deserializeFromStreamReader(message);
77 }
78 
79 
80 template<typename TObject, typename TSerializationReader, typename TSerializationBuilder>
82 {
83  ExtInputStream readStream(inputStream);
84 
85  capnp::InputStreamMessageReader message(readStream);
86 
87  return deserializeFromStreamReader(message);
88 }
89 
90 
91 template<typename TObject, typename TSerializationReader, typename TSerializationBuilder>
93 {
94  capnp::MallocMessageBuilder message;
95 
96  bool result = serializeIntoMessage(message, object);
97 
98  if (result == false)
99  {
100  return 0;
101  }
102 
103  return computeSerializedSizeInWords(message) * sizeof(uint64_t);
104 }
105 
106 
107 template<typename TObject, typename TSerializationReader, typename TSerializationBuilder>
108 bool ExtSerializationCAPN<TObject, TSerializationReader, TSerializationBuilder>::serializeIntoBuffer(const TObject* object, unsigned char* buffer, uint64_t maxSize, uint64_t& usedSize)
109 {
110  capnp::MallocMessageBuilder message;
111 
112  bool result = serializeIntoMessage(message, object);
113 
114  if (result == false)
115  {
116  usedSize = 0;
117  return false;
118  }
119 
120  uint64_t messageSize = computeSerializedSizeInWords(message) * sizeof(uint64_t);
121 
122  if (maxSize < messageSize)
123  {
124  NVBLAST_LOG_ERROR("When attempting to serialize into an existing buffer, the provided buffer was too small.");
125  usedSize = 0;
126  return false;
127  }
128 
129  kj::ArrayPtr<unsigned char> outputBuffer(buffer, maxSize);
130  kj::ArrayOutputStream outputStream(outputBuffer);
131 
132  capnp::writeMessage(outputStream, message);
133 
134  usedSize = messageSize;
135  return true;
136 }
137 
138 
139 template<typename TObject, typename TSerializationReader, typename TSerializationBuilder>
140 bool ExtSerializationCAPN<TObject, TSerializationReader, TSerializationBuilder>::serializeIntoBuffer(const TObject *object, unsigned char*& buffer, uint64_t& size, ExtSerialization::BufferProvider* bufferProvider, uint64_t offset)
141 {
142  capnp::MallocMessageBuilder message;
143 
144  bool result = serializeIntoMessage(message, object);
145 
146  if (result == false)
147  {
148  buffer = nullptr;
149  size = 0;
150  return false;
151  }
152 
153  const uint64_t blockSize = computeSerializedSizeInWords(message) * sizeof(uint64_t);
154 
155  size = blockSize + offset;
156 
157  buffer = static_cast<unsigned char *>(bufferProvider != nullptr ? bufferProvider->requestBuffer(size) : NVBLAST_ALLOC(size));
158 
159  kj::ArrayPtr<unsigned char> outputBuffer(buffer + offset, blockSize);
160  kj::ArrayOutputStream outputStream(outputBuffer);
161 
162  capnp::writeMessage(outputStream, message);
163 
164  return true;
165 }
166 
167 
168 template<typename TObject, typename TSerializationReader, typename TSerializationBuilder>
170 {
171  capnp::MallocMessageBuilder message;
172 
173  bool result = serializeIntoMessage(message, object);
174 
175  if (result == false)
176  {
177  return false;
178  }
179 
180  ExtOutputStream blastOutputStream(outputStream);
181 
182  writeMessage(blastOutputStream, message);
183 
184  return true;
185 }
186 
187 } // namespace Blast
188 } // namespace Nv
#define NVBLAST_ALLOC(_size)
Definition: NvBlastGlobals.h:220
Definition: NvBlastExtSerializationCAPN.h:44
physx::shdfnd::Array< T, Allocator > type
Definition: NvBlastArray.h:48
static bool serializeIntoBuffer(const TObject *object, unsigned char *buffer, uint64_t maxSize, uint64_t &usedSize)
Definition: NvBlastExtSerializationCAPN.h:108
static TObject * deserializeFromStream(std::istream &inputStream)
Definition: NvBlastExtSerializationCAPN.h:81
virtual void * requestBuffer(size_t size)=0
static uint64_t serializationBufferSize(const TObject *object)
Definition: NvBlastExtSerializationCAPN.h:92
Definition: NvBlastExtOutputStream.h:39
#define NVBLAST_LOG_ERROR(_msg)
Definition: NvBlastGlobals.h:246
Definition: NvBlastExtSerialization.h:56
static TObject * deserializeFromBuffer(const unsigned char *input, uint64_t size)
Definition: NvBlastExtSerializationCAPN.h:65
Definition: NvBlastExtInputStream.h:39
static bool serializeIntoStream(const TObject *object, std::ostream &outputStream)
Definition: NvBlastExtSerializationCAPN.h:169
Definition: NvBlastArray.h:37