1 // Copyright 2009-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #ifdef _WIN32
7 // ----------- windows only -----------
8 typedef unsigned long long id_t;
9 #ifndef _USE_MATH_DEFINES
10 #define _USE_MATH_DEFINES
11 #endif
12 #include <math.h>
13 #include <cmath>
14 #ifdef _M_X64
15 typedef long long ssize_t;
16 #else
17 typedef int ssize_t;
18 #endif
19 #else
20 // ----------- NOT windows -----------
21 #include "unistd.h"
22 #endif
23 
24 #include "rkcommon/math/AffineSpace.h"
25 #include "rkcommon/memory/RefCount.h"
26 #include "rkcommon/memory/malloc.h"
27 
28 // ospray
29 #include "ospray/ospray.h"
30 #include "ospray/ospray_cpp/ext/rkcommon.h"
31 #include "ospray/version.h"
32 
33 // std
34 #include <cstdint> // for int64_t etc
35 #include <map>
36 #include <sstream>
37 
38 // NOTE(jda) - This one file is shared between the core OSPRay library and the
39 //             ispc module...thus we have to define 2 different EXPORTS macros
40 //             here. This needs to be split between the libraries!
41 #ifdef _WIN32
42 #ifdef ospray_EXPORTS
43 #define OSPRAY_INTERFACE __declspec(dllexport)
44 #else
45 #define OSPRAY_INTERFACE __declspec(dllimport)
46 #endif
47 #define OSPRAY_DLLEXPORT __declspec(dllexport)
48 #else
49 #define OSPRAY_INTERFACE
50 #define OSPRAY_DLLEXPORT
51 #endif
52 #define OSPRAY_CORE_INTERFACE OSPRAY_INTERFACE
53 
54 #ifdef _WIN32
55 #ifdef ospray_module_ispc_EXPORTS
56 #define OSPRAY_MODULE_ISPC_INTERFACE __declspec(dllexport)
57 #else
58 #define OSPRAY_MODULE_ISPC_INTERFACE __declspec(dllimport)
59 #endif
60 #define OSPRAY_MODULE_ISPC_DLLEXPORT __declspec(dllexport)
61 #else
62 #define OSPRAY_MODULE_ISPC_INTERFACE
63 #define OSPRAY_MODULE_ISPC_DLLEXPORT
64 #endif
65 #define OSPRAY_SDK_INTERFACE OSPRAY_MODULE_ISPC_INTERFACE
66 
67 //! main namespace for all things ospray (for internal code)
68 namespace ospray {
69 
70 using namespace rkcommon;
71 using namespace rkcommon::math;
72 using namespace rkcommon::memory;
73 
74 /*! basic types */
75 using int64 = std::int64_t;
76 using uint64 = std::uint64_t;
77 
78 using int32 = std::int32_t;
79 using uint32 = std::uint32_t;
80 
81 using int16 = std::int16_t;
82 using uint16 = std::uint16_t;
83 
84 using int8 = std::int8_t;
85 using uint8 = std::uint8_t;
86 
87 using index_t = std::int64_t;
88 
89 template <typename T>
90 using FactoryFcn = T *(*)();
91 
92 template <typename T>
93 using FactoryMap = std::map<std::string, FactoryFcn<T>>;
94 
95 template <typename B, typename T>
allocate_object()96 inline B *allocate_object()
97 {
98   return new T;
99 }
100 
101 // Argument parsing functions
102 std::string getArgString(const std::string &s);
103 int getArgInt(const std::string &s);
104 
105 void initFromCommandLine(int *ac = nullptr, const char ***av = nullptr);
106 
107 extern "C" {
108 /*! 64-bit malloc. allows for alloc'ing memory larger than 4GB */
109 OSPRAY_CORE_INTERFACE void *malloc64(size_t size);
110 /*! 64-bit malloc. allows for alloc'ing memory larger than 4GB */
111 OSPRAY_CORE_INTERFACE void free64(void *ptr);
112 /*! Thread Local Storage functions */
113 OSPRAY_CORE_INTERFACE void *pushTLS(size_t size);
114 OSPRAY_CORE_INTERFACE void *reallocTLS(void *ptr, size_t size);
115 OSPRAY_CORE_INTERFACE void popTLS(void *ptr);
116 }
117 
118 OSPRAY_CORE_INTERFACE OSPDataType typeOf(const char *string);
typeOf(const std::string & s)119 inline OSPDataType typeOf(const std::string &s)
120 {
121   return typeOf(s.c_str());
122 }
123 
124 OSPRAY_CORE_INTERFACE std::string stringFor(OSPDataType);
125 OSPRAY_CORE_INTERFACE std::string stringFor(OSPTextureFormat);
126 
isObjectType(OSPDataType type)127 inline bool isObjectType(OSPDataType type)
128 {
129   return type & OSP_OBJECT;
130 }
131 
132 OSPRAY_CORE_INTERFACE size_t sizeOf(OSPDataType);
133 OSPRAY_CORE_INTERFACE size_t sizeOf(OSPTextureFormat);
134 OSPRAY_CORE_INTERFACE size_t sizeOf(OSPFrameBufferFormat);
135 
136 OSPRAY_CORE_INTERFACE OSPError loadLocalModule(const std::string &name);
137 
moduleVersionCheck(int16_t versionMajor,int16_t versionMinor)138 inline OSPError moduleVersionCheck(int16_t versionMajor, int16_t versionMinor)
139 {
140   if ((OSPRAY_VERSION_MAJOR == versionMajor)
141       && (OSPRAY_VERSION_MINOR == versionMinor)) {
142     return OSP_NO_ERROR;
143   } else
144     return OSP_VERSION_MISMATCH;
145 }
146 
147 /*! little helper class that prints out a warning string upon the
148   first time it is encountered.
149 
150   Usage:
151 
152   if (someThisBadHappens) {
153      static WarnOnce warning("something bad happened, at least once!");
154      ...
155   }
156 */
157 struct OSPRAY_CORE_INTERFACE WarnOnce
158 {
159   WarnOnce(
160       const std::string &message, uint32_t postAtLogLevel = OSP_LOG_WARNING);
161 
162  private:
163   const std::string s;
164 };
165 
166 OSPRAY_CORE_INTERFACE uint32_t logLevel();
167 
168 extern "C" OSPRAY_CORE_INTERFACE void postStatusMsg(
169     uint32_t msgId, uint32_t postAtLogLevel);
170 
171 OSPRAY_CORE_INTERFACE void postStatusMsg(
172     const std::stringstream &msg, uint32_t postAtLogLevel = OSP_LOG_DEBUG);
173 
174 OSPRAY_CORE_INTERFACE void postStatusMsg(
175     const std::string &msg, uint32_t postAtLogLevel = OSP_LOG_DEBUG);
176 
177 // use postStatusMsg to output any information, which will use OSPRay's
178 // internal infrastructure, optionally at a given loglevel
179 // a newline is added implicitly
180 //////////////////////////////////////////////////////////////////////////////
181 
182 struct StatusMsgStream : public std::stringstream
183 {
184   StatusMsgStream(uint32_t postAtLogLevel = OSP_LOG_DEBUG);
185   StatusMsgStream(const StatusMsgStream &copy) = delete;
186   StatusMsgStream(StatusMsgStream &&move);
187   StatusMsgStream &operator=(const StatusMsgStream &copy) = delete;
188   StatusMsgStream &operator=(StatusMsgStream &&move) = default;
189   ~StatusMsgStream() override;
190 
191  private:
192   uint32_t logLevel{OSP_LOG_DEBUG};
193 };
194 
StatusMsgStream(uint32_t postAtLogLevel)195 inline StatusMsgStream::StatusMsgStream(uint32_t postAtLogLevel)
196     : logLevel(postAtLogLevel)
197 {}
198 
~StatusMsgStream()199 inline StatusMsgStream::~StatusMsgStream()
200 {
201   auto msg = str();
202   if (!msg.empty())
203     postStatusMsg(msg, logLevel);
204 }
205 
StatusMsgStream(StatusMsgStream && move)206 inline StatusMsgStream::StatusMsgStream(StatusMsgStream &&move)
207 {
208   this->str(move.str());
209 }
210 
211 OSPRAY_CORE_INTERFACE StatusMsgStream postStatusMsg(
212     uint32_t postAtLogLevel = OSP_LOG_DEBUG);
213 
214 /////////////////////////////////////////////////////////////////////////////
215 
216 OSPRAY_CORE_INTERFACE void handleError(OSPError e, const std::string &message);
217 
218 OSPRAY_CORE_INTERFACE void postTraceMsg(const std::string &message);
219 
220 //! log status message at loglevel x
221 #define ospLog(x) StatusMsgStream(x) << "(" << x << "): "
222 //! log status message at loglevel x with function name
223 #define ospLogF(x) StatusMsgStream(x) << __FUNCTION__ << ": "
224 //! log status message at loglevel x with function, file, and line number
225 #define ospLogL(x)                                                             \
226   StatusMsgStream(x) << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__     \
227                      << "): "
228 
229 // RTTI hash ID lookup helper functions ///////////////////////////////////
230 
231 OSPRAY_CORE_INTERFACE size_t translatedHash(size_t v);
232 
233 template <typename T>
typeIdOf()234 inline size_t typeIdOf()
235 {
236   return translatedHash(typeid(T).hash_code());
237 }
238 
239 template <typename T>
typeIdOf(T * v)240 inline size_t typeIdOf(T *v)
241 {
242   return translatedHash(typeid(*v).hash_code());
243 }
244 
245 template <typename T>
typeIdOf(const T & v)246 inline size_t typeIdOf(const T &v)
247 {
248   return translatedHash(typeid(v).hash_code());
249 }
250 
251 template <typename T>
typeIdOf(const std::unique_ptr<T> & v)252 inline size_t typeIdOf(const std::unique_ptr<T> &v)
253 {
254   return translatedHash(typeid(*v).hash_code());
255 }
256 
257 template <typename T>
typeIdOf(const std::shared_ptr<T> & v)258 inline size_t typeIdOf(const std::shared_ptr<T> &v)
259 {
260   return translatedHash(typeid(*v).hash_code());
261 }
262 
263 // RTTI string name lookup helper functions ///////////////////////////////
264 
265 template <typename T>
typeString()266 inline std::string typeString()
267 {
268   return typeid(T).name();
269 }
270 
271 template <typename T>
typeString(T * v)272 inline std::string typeString(T *v)
273 {
274   return typeid(*v).name();
275 }
276 
277 template <typename T>
typeString(const T & v)278 inline std::string typeString(const T &v)
279 {
280   return typeid(v).name();
281 }
282 
283 template <typename T>
typeString(const std::unique_ptr<T> & v)284 inline std::string typeString(const std::unique_ptr<T> &v)
285 {
286   return typeid(*v).name();
287 }
288 
289 template <typename T>
typeString(const std::shared_ptr<T> & v)290 inline std::string typeString(const std::shared_ptr<T> &v)
291 {
292   return typeid(*v).name();
293 }
294 
295 } // namespace ospray
296