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