VTK  9.1.0
OSPRayBackend.h
Go to the documentation of this file.
1#pragma once
2
3#include <ospray/ospray_util.h>
4
5#include "../Backend.h"
6
7#include <cstring>
8#include <sstream>
9#include <stdexcept>
10#include <stdlib.h>
11#include <vector>
12#include <iostream>
13
14namespace RTW
15{
16
17 OSPFrameBufferFormat convert(RTWFrameBufferFormat format)
18 {
19 switch (format)
20 {
21 case RTW_FB_RGBA8:
22 return OSP_FB_RGBA8;
23 case RTW_FB_SRGBA:
24 return OSP_FB_SRGBA;
25 case RTW_FB_RGBA32F:
26 return OSP_FB_RGBA32F;
27 default:
28 return OSP_FB_NONE;
29 }
30 }
31
33 {
34 switch (format)
35 {
37 return OSP_TEXTURE_RGBA8;
39 return OSP_TEXTURE_SRGBA;
43 return OSP_TEXTURE_RGB8;
45 return OSP_TEXTURE_SRGB;
47 return OSP_TEXTURE_RGB32F;
48 case RTW_TEXTURE_R8:
49 return OSP_TEXTURE_R8;
51 return OSP_TEXTURE_R32F;
52 case RTW_TEXTURE_L8:
53 return OSP_TEXTURE_L8;
54 case RTW_TEXTURE_RA8:
55 return OSP_TEXTURE_RA8;
56 case RTW_TEXTURE_LA8:
57 return OSP_TEXTURE_LA8;
59 default:
60 return OSP_TEXTURE_FORMAT_INVALID;
61 }
62 }
63
64
65
66
67 /*
68 * Simple pass-through backend for OSPRay.
69 */
70 class OSPRayBackend : public Backend
71 {
72 public:
73 RTWError Init() override
74 {
75 static bool once = false;
77 if (!once) {
78 ret = static_cast<RTWError>(ospInit(nullptr, nullptr));
79 OSPDevice device = ospGetCurrentDevice();
80 if (!device)
81 {
82 std::runtime_error("OSPRay device could not be fetched!");
83 }
84#if OSPRAY_VERSION_MINOR > 1
85 ospDeviceSetErrorCallback(device, [](void *, OSPError, const char *errorDetails) {
86 std::cerr << "OSPRay ERROR: " << errorDetails << std::endl;
87 }, nullptr);
88#else
89 ospDeviceSetErrorFunc(device, [](OSPError, const char *errorDetails) {
90 std::cerr << "OSPRay ERROR: " << errorDetails << std::endl;
91 });
92#endif
93 once = true;
94 }
95 return ret;
96 }
97
98 void Shutdown() override
99 {
100 //do nothing here. Since OSPRay 2
101 }
102
103 bool IsSupported(RTWFeature feature) const override
104 {
105 switch (feature)
106 {
108 return false;
110 return false;
112 return false;
113 case RTW_INSTANCING:
114 return true;
115 case RTW_DENOISER:
116 return false; // OpenImageDenoise is an external lib outside of the backend
118 return true;
119 }
120 return false;
121 }
122
123 RTWData NewCopyData1D(const void *source, RTWDataType dataType, size_t numElements) override
124 {
125 OSPData data = ospNewData1D(static_cast<OSPDataType>(dataType), numElements);
127 OSPData shared = ospNewSharedData1D(source, static_cast<OSPDataType>(dataType), numElements);
128 ospCommit(shared);
129 ospCopyData1D(shared, data, 0);
131 ospRelease(shared);
132 return reinterpret_cast<RTWData>(data);
133 }
134 RTWData NewCopyData2D(const void *source, RTWDataType dataType, size_t numElements, size_t numElements2) override
135 {
136 OSPData data = ospNewData2D(static_cast<OSPDataType>(dataType), numElements, numElements2);
138 OSPData shared = ospNewSharedData2D(source, static_cast<OSPDataType>(dataType), numElements, numElements2);
139 ospCommit(shared);
140 ospCopyData2D(shared, data, 0, 0);
142 ospRelease(shared);
143 return reinterpret_cast<RTWData>(data);
144 }
145 RTWData NewCopyData3D(const void *source, RTWDataType dataType, size_t numElements, size_t numElements2, size_t numElements3) override
146 {
147 OSPData data = ospNewData(static_cast<OSPDataType>(dataType), numElements, numElements2, numElements3);
149 OSPData shared = ospNewSharedData3D(source, static_cast<OSPDataType>(dataType), numElements, numElements2, numElements3);
150 ospCommit(shared);
151 ospCopyData(shared, data, 0, 0, 0);
153 ospRelease(shared);
154 return reinterpret_cast<RTWData>(data);
155 }
156
157 RTWData NewData(RTWDataType dataType, size_t numElements) override
158 {
159 return reinterpret_cast<RTWData>(ospNewData(static_cast<OSPDataType>(dataType), numElements));
160 }
161
162 RTWGeometry NewGeometry(const char *type) override
163 {
164 return reinterpret_cast<RTWGeometry>(ospNewGeometry(type));
165 }
166
168 {
169 return reinterpret_cast<RTWGroup>(ospNewGroup());
170 }
171
172 RTWData NewSharedData1D(const void* sharedData, RTWDataType type, uint32_t numItems1) override
173 {
174 return reinterpret_cast<RTWData>(ospNewSharedData1D(sharedData, (OSPDataType)((int)type), numItems1));
175 }
176
177 RTWData NewSharedData2D(const void* sharedData, RTWDataType type, uint32_t numItems1, uint32_t numItems2) override
178 {
179 return reinterpret_cast<RTWData>(ospNewSharedData2D(sharedData, (OSPDataType)((int)type), numItems1, numItems2));
180 }
181
182 RTWData NewSharedData3D(const void* sharedData, RTWDataType type, uint32_t numItems1, uint32_t numItems2,
183 uint32_t numItems3) override
184 {
185 return reinterpret_cast<RTWData>(ospNewSharedData3D(sharedData, (OSPDataType)((int)type),
186 numItems1, numItems2, numItems3));
187 }
188
189 RTWTexture NewTexture(const char* type) override
190 {
191 return reinterpret_cast<RTWTexture>(ospNewTexture(type));
192 }
193
194 RTWLight NewLight(const char *light_type) override
195 {
196 return reinterpret_cast<RTWLight>(ospNewLight(light_type));
197 }
198
199 RTWMaterial NewMaterial(const char *renderer_type, const char *material_type) override
200 {
201 return reinterpret_cast<RTWMaterial>(ospNewMaterial(renderer_type, material_type));
202 }
203
204 RTWVolume NewVolume(const char *type) override
205 {
206 return reinterpret_cast<RTWVolume>(ospNewVolume(type));
207 }
208
210 {
211 return reinterpret_cast<RTWTransferFunction>(ospNewTransferFunction(type));
212 }
213
214 RTWRenderer NewRenderer(const char *type) override
215 {
216 return reinterpret_cast<RTWRenderer>(ospNewRenderer(type));
217 }
218
219 RTWCamera NewCamera(const char *type) override
220 {
221 return reinterpret_cast<RTWCamera>(ospNewCamera(type));
222 }
223
225 {
226 return reinterpret_cast<RTWGeometricModel>(ospNewGeometricModel(reinterpret_cast<OSPGeometry>(geometry)));
227 }
228
230 {
231 return reinterpret_cast<RTWVolumetricModel>(ospNewVolumetricModel(reinterpret_cast<OSPVolume>(volume)));
232 }
233
235 {
236 return reinterpret_cast<RTWWorld>(ospNewWorld());
237 }
238
240 {
241 return reinterpret_cast<RTWInstance>(ospNewInstance(reinterpret_cast<OSPGroup>(geometry)));
242 }
243
244 RTWFrameBuffer NewFrameBuffer(const rtw::vec2i &size, const RTWFrameBufferFormat format, const uint32_t frameBufferChannels) override
245 {
246 return reinterpret_cast<RTWFrameBuffer>(ospNewFrameBuffer(size.x, size.y, convert(format), frameBufferChannels));
247 }
248
249 void Release(RTWObject object) override
250 {
251 ospRelease(reinterpret_cast<OSPObject>(object));
252 }
253
254 void SetString(RTWObject object, const char *id, const char *s) override
255 {
256 ospSetString(reinterpret_cast<OSPObject>(object), id, s);
257 }
258
259 void SetObject(RTWObject object, const char *id, RTWObject other) override
260 {
261 ospSetObject(reinterpret_cast<OSPObject>(object), id, reinterpret_cast<OSPObject>(other));
262 }
263
264 void SetObjectAsData(RTWObject target, const char *id, RTWDataType type, RTWObject obj) override{
265 ospSetObjectAsData(reinterpret_cast<OSPObject>(target), id, (OSPDataType)type,
266 reinterpret_cast<OSPObject>(obj));
267 }
268
269 void SetParam(RTWObject object, const char *id, RTWDataType dataType, const void* mem) override
270 {
271 ospSetParam(reinterpret_cast<OSPObject>(object), id, static_cast<OSPDataType>(dataType),
272 mem);
273 }
274
275 void SetInt(RTWObject object, const char *id, int32_t x) override
276 {
277 ospSetInt(reinterpret_cast<OSPObject>(object), id, x);
278 }
279
280 void SetBool(RTWObject object, const char *id, bool x) override
281 {
282 ospSetBool(reinterpret_cast<OSPObject>(object), id, x);
283 }
284
285 void SetFloat(RTWObject object, const char *id, float x) override
286 {
287 ospSetFloat(reinterpret_cast<OSPObject>(object), id, x);
288 }
289
290 void SetVec2f(RTWObject object, const char *id, float x, float y) override
291 {
292 ospSetVec2f(reinterpret_cast<OSPObject>(object), id, x, y);
293 }
294
295 void SetVec2i(RTWObject object, const char *id, int x, int y) override
296 {
297 ospSetVec2i(reinterpret_cast<OSPObject>(object), id, x, y);
298 }
299
300 void SetVec3i(RTWObject object, const char *id, int x, int y, int z) override
301 {
302 ospSetVec3i(reinterpret_cast<OSPObject>(object), id, x, y, z);
303 }
304
305 void SetVec3f(RTWObject object, const char *id, float x, float y, float z) override
306 {
307 ospSetVec3f(reinterpret_cast<OSPObject>(object), id, x, y, z);
308 }
309
310 void SetVec4f(RTWObject object, const char *id, float x, float y, float z, float w) override
311 {
312 ospSetVec4f(reinterpret_cast<OSPObject>(object), id, x, y, z, w);
313 }
314
315 void RemoveParam(RTWObject object, const char *id) override
316 {
317 ospRemoveParam(reinterpret_cast<OSPObject>(object), id);
318 }
319
320 void Commit(RTWObject object) override
321 {
322 ospCommit(reinterpret_cast<OSPObject>(object));
323 }
324
325 float RenderFrame(RTWFrameBuffer frameBuffer, RTWRenderer renderer, RTWCamera camera, RTWWorld world) override
326 {
327 return ospRenderFrameBlocking(reinterpret_cast<OSPFrameBuffer>(frameBuffer), reinterpret_cast<OSPRenderer>(renderer),
328 reinterpret_cast<OSPCamera>(camera), reinterpret_cast<OSPWorld>(world));
329 }
330
331 void FrameBufferClear(RTWFrameBuffer frameBuffer) override
332 {
333 ospResetAccumulation(reinterpret_cast<OSPFrameBuffer>(frameBuffer));
334 }
335
336 const void* MapFrameBuffer(RTWFrameBuffer frameBuffer, const RTWFrameBufferChannel channel) override
337 {
338 return ospMapFrameBuffer(reinterpret_cast<OSPFrameBuffer>(frameBuffer), static_cast<OSPFrameBufferChannel>(channel));
339 }
340
341 void UnmapFrameBuffer(const void *mapped, RTWFrameBuffer frameBuffer) override
342 {
343 ospUnmapFrameBuffer(mapped, reinterpret_cast<OSPFrameBuffer>(frameBuffer));
344 }
345
346 void SetDepthNormalizationGL(RTWFrameBuffer /*frameBuffer*/, float /*clipMin*/, float /*clipMax*/) override
347 {
348 // not supported
349 }
350
351 int GetColorTextureGL(RTWFrameBuffer /*frameBuffer*/) override
352 {
353 // not supported
354 return 0;
355 }
356
357 int GetDepthTextureGL(RTWFrameBuffer /*frameBuffer*/) override
358 {
359 // not supported
360 return 0;
361 }
362 };
363}
#define OSP_FB_SRGBA
Definition: RTWrapper.h:114
#define OSPDataType
Definition: RTWrapper.h:32
#define ospNewGeometry
Definition: RTWrapper.h:170
#define ospSetVec2f
Definition: RTWrapper.h:143
#define ospSetObjectAsData
Definition: RTWrapper.h:149
#define ospNewGroup
Definition: RTWrapper.h:163
#define ospSetString
Definition: RTWrapper.h:146
#define OSP_TEXTURE_SRGBA
Definition: RTWrapper.h:110
#define ospSetVec2i
Definition: RTWrapper.h:141
#define ospUnmapFrameBuffer
Definition: RTWrapper.h:181
#define ospNewTexture
Definition: RTWrapper.h:166
#define OSPWorld
Definition: RTWrapper.h:17
#define OSP_TEXTURE_SRGB
Definition: RTWrapper.h:109
#define ospNewGeometricModel
Definition: RTWrapper.h:172
#define OSPTextureFormat
Definition: RTWrapper.h:25
#define ospNewVolume
Definition: RTWrapper.h:169
#define ospNewVolumetricModel
Definition: RTWrapper.h:173
#define OSP_FB_RGBA8
Definition: RTWrapper.h:113
#define OSPData
Definition: RTWrapper.h:20
#define OSP_TEXTURE_R8
Definition: RTWrapper.h:104
#define ospSetVec4f
Definition: RTWrapper.h:145
#define ospNewWorld
Definition: RTWrapper.h:174
#define OSP_TEXTURE_RGBA8
Definition: RTWrapper.h:106
#define ospNewMaterial
Definition: RTWrapper.h:167
#define OSP_TEXTURE_RGB32F
Definition: RTWrapper.h:102
#define OSPFrameBuffer
Definition: RTWrapper.h:28
#define OSPRenderer
Definition: RTWrapper.h:16
#define OSPGeometry
Definition: RTWrapper.h:27
#define OSP_TEXTURE_RGB8
Definition: RTWrapper.h:105
#define ospSetVec3i
Definition: RTWrapper.h:142
#define ospSetObject
Definition: RTWrapper.h:148
#define ospNewSharedData2D
Definition: RTWrapper.h:161
#define OSP_TEXTURE_RGBA32F
Definition: RTWrapper.h:103
#define ospNewData
Definition: RTWrapper.h:156
#define ospNewFrameBuffer
Definition: RTWrapper.h:176
#define ospSetInt
Definition: RTWrapper.h:140
#define ospRemoveParam
Definition: RTWrapper.h:151
#define ospNewTransferFunction
Definition: RTWrapper.h:168
#define ospSetParam
Definition: RTWrapper.h:147
#define OSPCamera
Definition: RTWrapper.h:21
#define ospSetVec3f
Definition: RTWrapper.h:144
#define OSP_TEXTURE_R32F
Definition: RTWrapper.h:101
#define ospNewCamera
Definition: RTWrapper.h:164
#define ospNewSharedData3D
Definition: RTWrapper.h:162
#define ospSetFloat
Definition: RTWrapper.h:138
#define ospRelease
Definition: RTWrapper.h:154
#define OSPVolume
Definition: RTWrapper.h:29
#define ospNewSharedData1D
Definition: RTWrapper.h:160
#define ospCommit
Definition: RTWrapper.h:153
#define ospMapFrameBuffer
Definition: RTWrapper.h:180
#define ospNewLight
Definition: RTWrapper.h:165
#define OSP_TEXTURE_L8
Definition: RTWrapper.h:107
#define OSPGroup
Definition: RTWrapper.h:15
#define OSPObject
Definition: RTWrapper.h:14
#define ospSetBool
Definition: RTWrapper.h:139
#define ospNewInstance
Definition: RTWrapper.h:175
#define OSP_FB_RGBA32F
Definition: RTWrapper.h:112
#define OSP_TEXTURE_LA8
Definition: RTWrapper.h:108
#define ospNewRenderer
Definition: RTWrapper.h:177
struct RTWHandle * RTWData
Definition: Types.h:229
struct RTWHandle * RTWCamera
Definition: Types.h:223
RTWFrameBufferFormat
Definition: Types.h:18
@ RTW_FB_RGBA8
Definition: Types.h:20
@ RTW_FB_SRGBA
Definition: Types.h:21
@ RTW_FB_RGBA32F
Definition: Types.h:22
struct RTWHandle * RTWObject
Definition: Types.h:236
struct RTWHandle * RTWTexture
Definition: Types.h:235
RTWTextureFormat
Definition: Types.h:94
@ RTW_TEXTURE_L8
Definition: Types.h:103
@ RTW_TEXTURE_FORMAT_INVALID
Definition: Types.h:110
@ RTW_TEXTURE_R32F
Definition: Types.h:102
@ RTW_TEXTURE_RGBA32F
Definition: Types.h:97
@ RTW_TEXTURE_LA8
Definition: Types.h:105
@ RTW_TEXTURE_RA8
Definition: Types.h:104
@ RTW_TEXTURE_SRGB
Definition: Types.h:99
@ RTW_TEXTURE_RGB8
Definition: Types.h:98
@ RTW_TEXTURE_R8
Definition: Types.h:101
@ RTW_TEXTURE_RGBA8
Definition: Types.h:95
@ RTW_TEXTURE_RGB32F
Definition: Types.h:100
@ RTW_TEXTURE_SRGBA
Definition: Types.h:96
struct RTWHandle * RTWTransferFunction
Definition: Types.h:234
RTWFeature
Definition: Types.h:241
@ RTW_DEPTH_COMPOSITING
Definition: Types.h:247
@ RTW_INSTANCING
Definition: Types.h:245
@ RTW_DENOISER
Definition: Types.h:246
@ RTW_DEPTH_NORMALIZATION
Definition: Types.h:242
@ RTW_OPENGL_INTEROP
Definition: Types.h:243
@ RTW_ANIMATED_PARAMETERIZATION
Definition: Types.h:244
struct RTWHandle * RTWFrameBuffer
Definition: Types.h:220
struct RTWHandle * RTWGroup
Definition: Types.h:224
struct RTWHandle * RTWGeometry
Definition: Types.h:230
struct RTWHandle * RTWVolume
Definition: Types.h:233
RTWError
Definition: Types.h:7
@ RTW_NO_ERROR
Definition: Types.h:8
struct RTWHandle * RTWVolumetricModel
Definition: Types.h:227
RTWDataType
Definition: Types.h:120
struct RTWHandle * RTWMaterial
Definition: Types.h:231
struct RTWHandle * RTWRenderer
Definition: Types.h:222
struct RTWHandle * RTWLight
Definition: Types.h:232
struct RTWHandle * RTWInstance
Definition: Types.h:225
RTWFrameBufferChannel
Definition: Types.h:26
struct RTWHandle * RTWWorld
Definition: Types.h:228
struct RTWHandle * RTWGeometricModel
Definition: Types.h:226
const void * MapFrameBuffer(RTWFrameBuffer frameBuffer, const RTWFrameBufferChannel channel) override
float RenderFrame(RTWFrameBuffer frameBuffer, RTWRenderer renderer, RTWCamera camera, RTWWorld world) override
RTWTexture NewTexture(const char *type) override
RTWData NewSharedData1D(const void *sharedData, RTWDataType type, uint32_t numItems1) override
RTWInstance NewInstance(RTWGroup geometry) override
RTWData NewSharedData3D(const void *sharedData, RTWDataType type, uint32_t numItems1, uint32_t numItems2, uint32_t numItems3) override
RTWVolumetricModel NewVolumetricModel(RTWVolume volume) override
RTWRenderer NewRenderer(const char *type) override
RTWFrameBuffer NewFrameBuffer(const rtw::vec2i &size, const RTWFrameBufferFormat format, const uint32_t frameBufferChannels) override
RTWData NewCopyData2D(const void *source, RTWDataType dataType, size_t numElements, size_t numElements2) override
RTWGeometry NewGeometry(const char *type) override
void Commit(RTWObject object) override
RTWError Init() override
Definition: OSPRayBackend.h:73
int GetDepthTextureGL(RTWFrameBuffer) override
void SetObject(RTWObject object, const char *id, RTWObject other) override
void SetVec3i(RTWObject object, const char *id, int x, int y, int z) override
RTWData NewData(RTWDataType dataType, size_t numElements) override
void Release(RTWObject object) override
void UnmapFrameBuffer(const void *mapped, RTWFrameBuffer frameBuffer) override
RTWWorld NewWorld() override
void SetObjectAsData(RTWObject target, const char *id, RTWDataType type, RTWObject obj) override
RTWGeometricModel NewGeometricModel(RTWGeometry geometry) override
void SetParam(RTWObject object, const char *id, RTWDataType dataType, const void *mem) override
void SetVec2f(RTWObject object, const char *id, float x, float y) override
RTWVolume NewVolume(const char *type) override
RTWData NewCopyData1D(const void *source, RTWDataType dataType, size_t numElements) override
RTWMaterial NewMaterial(const char *renderer_type, const char *material_type) override
void FrameBufferClear(RTWFrameBuffer frameBuffer) override
void SetDepthNormalizationGL(RTWFrameBuffer, float, float) override
RTWData NewCopyData3D(const void *source, RTWDataType dataType, size_t numElements, size_t numElements2, size_t numElements3) override
void Shutdown() override
Definition: OSPRayBackend.h:98
RTWLight NewLight(const char *light_type) override
void SetBool(RTWObject object, const char *id, bool x) override
bool IsSupported(RTWFeature feature) const override
void SetString(RTWObject object, const char *id, const char *s) override
void SetVec4f(RTWObject object, const char *id, float x, float y, float z, float w) override
RTWGroup NewGroup() override
void SetVec3f(RTWObject object, const char *id, float x, float y, float z) override
void SetVec2i(RTWObject object, const char *id, int x, int y) override
RTWData NewSharedData2D(const void *sharedData, RTWDataType type, uint32_t numItems1, uint32_t numItems2) override
int GetColorTextureGL(RTWFrameBuffer) override
RTWCamera NewCamera(const char *type) override
void SetInt(RTWObject object, const char *id, int32_t x) override
void SetFloat(RTWObject object, const char *id, float x) override
RTWTransferFunction NewTransferFunction(const char *type) override
void RemoveParam(RTWObject object, const char *id) override
Definition: Backend.h:6
OSPFrameBufferFormat convert(RTWFrameBufferFormat format)
Definition: OSPRayBackend.h:17
@ type
Definition: vtkX3D.h:522
@ size
Definition: vtkX3D.h:259
@ data
Definition: vtkX3D.h:321
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)