VTK  9.4.20241118
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 ospDeviceSetErrorCallback(device, [](void *, OSPError, const char *errorDetails) {
88 std::cerr << "OSPRay ERROR: " << errorDetails << std::endl;
89 }, nullptr);
90 once = true;
91 }
92 return ret;
93 }
94
95 void Shutdown() override
96 {
97 //do nothing here. Since OSPRay 2
98 }
99
100 bool IsSupported(RTWFeature feature) const override
101 {
102 switch (feature)
103 {
105 return false;
107 return false;
109 return false;
110 case RTW_INSTANCING:
111 return true;
112 case RTW_DENOISER:
113 return false; // OpenImageDenoise is an external lib outside of the backend
115 return true;
116 }
117 return false;
118 }
119
120 RTWData NewCopyData1D(const void *source, RTWDataType dataType, size_t numElements) override
121 {
122 OSPData data = ospNewData1D(static_cast<OSPDataType>(dataType), numElements);
123 ospCommit(data);
124 OSPData shared = ospNewSharedData1D(source, static_cast<OSPDataType>(dataType), numElements);
125 ospCommit(shared);
126 ospCopyData1D(shared, data, 0);
127 ospCommit(data);
128 ospRelease(shared);
129 return reinterpret_cast<RTWData>(data);
130 }
131 RTWData NewCopyData2D(const void *source, RTWDataType dataType, size_t numElements, size_t numElements2) override
132 {
133 OSPData data = ospNewData2D(static_cast<OSPDataType>(dataType), numElements, numElements2);
134 ospCommit(data);
135 OSPData shared = ospNewSharedData2D(source, static_cast<OSPDataType>(dataType), numElements, numElements2);
136 ospCommit(shared);
137 ospCopyData2D(shared, data, 0, 0);
138 ospCommit(data);
139 ospRelease(shared);
140 return reinterpret_cast<RTWData>(data);
141 }
142 RTWData NewCopyData3D(const void *source, RTWDataType dataType, size_t numElements, size_t numElements2, size_t numElements3) override
143 {
144 OSPData data = ospNewData(static_cast<OSPDataType>(dataType), numElements, numElements2, numElements3);
145 ospCommit(data);
146 OSPData shared = ospNewSharedData3D(source, static_cast<OSPDataType>(dataType), numElements, numElements2, numElements3);
147 ospCommit(shared);
148 ospCopyData(shared, data, 0, 0, 0);
149 ospCommit(data);
150 ospRelease(shared);
151 return reinterpret_cast<RTWData>(data);
152 }
153
154 RTWData NewData(RTWDataType dataType, size_t numElements) override
155 {
156 return reinterpret_cast<RTWData>(ospNewData(static_cast<OSPDataType>(dataType), numElements));
157 }
158
159 RTWGeometry NewGeometry(const char *type) override
160 {
161 return reinterpret_cast<RTWGeometry>(ospNewGeometry(type));
162 }
163
165 {
166 return reinterpret_cast<RTWGroup>(ospNewGroup());
167 }
168
169 RTWData NewSharedData1D(const void* sharedData, RTWDataType type, uint32_t numItems1) override
170 {
171 return reinterpret_cast<RTWData>(ospNewSharedData1D(sharedData, (OSPDataType)((int)type), numItems1));
172 }
173
174 RTWData NewSharedData2D(const void* sharedData, RTWDataType type, uint32_t numItems1, uint32_t numItems2) override
175 {
176 return reinterpret_cast<RTWData>(ospNewSharedData2D(sharedData, (OSPDataType)((int)type), numItems1, numItems2));
177 }
178
179 RTWData NewSharedData3D(const void* sharedData, RTWDataType type, uint32_t numItems1, uint32_t numItems2,
180 uint32_t numItems3) override
181 {
182 return reinterpret_cast<RTWData>(ospNewSharedData3D(sharedData, (OSPDataType)((int)type),
183 numItems1, numItems2, numItems3));
184 }
185
186 RTWTexture NewTexture(const char* type) override
187 {
188 return reinterpret_cast<RTWTexture>(ospNewTexture(type));
189 }
190
191 RTWLight NewLight(const char *light_type) override
192 {
193 return reinterpret_cast<RTWLight>(ospNewLight(light_type));
194 }
195
196 RTWMaterial NewMaterial(const char *material_type) override
197 {
198#if OSPRAY_VERSION_MAJOR < 3
199 return reinterpret_cast<RTWMaterial>(ospNewMaterial(nullptr, material_type));
200#else
201 return reinterpret_cast<RTWMaterial>(ospNewMaterial(material_type));
202#endif
203 }
204
205 RTWVolume NewVolume(const char *type) override
206 {
207 return reinterpret_cast<RTWVolume>(ospNewVolume(type));
208 }
209
210 RTWTransferFunction NewTransferFunction(const char *type) override
211 {
212 return reinterpret_cast<RTWTransferFunction>(ospNewTransferFunction(type));
213 }
214
215 RTWRenderer NewRenderer(const char *type) override
216 {
217 return reinterpret_cast<RTWRenderer>(ospNewRenderer(type));
218 }
219
220 RTWCamera NewCamera(const char *type) override
221 {
222 return reinterpret_cast<RTWCamera>(ospNewCamera(type));
223 }
224
226 {
227 return reinterpret_cast<RTWGeometricModel>(ospNewGeometricModel(reinterpret_cast<OSPGeometry>(geometry)));
228 }
229
231 {
232 return reinterpret_cast<RTWVolumetricModel>(ospNewVolumetricModel(reinterpret_cast<OSPVolume>(volume)));
233 }
234
236 {
237 return reinterpret_cast<RTWWorld>(ospNewWorld());
238 }
239
241 {
242 return reinterpret_cast<RTWInstance>(ospNewInstance(reinterpret_cast<OSPGroup>(geometry)));
243 }
244
245 RTWFrameBuffer NewFrameBuffer(const rtw::vec2i &size, const RTWFrameBufferFormat format, const uint32_t frameBufferChannels) override
246 {
247 return reinterpret_cast<RTWFrameBuffer>(ospNewFrameBuffer(size.x, size.y, convert(format), frameBufferChannels));
248 }
249
250 void Release(RTWObject object) override
251 {
252 ospRelease(reinterpret_cast<OSPObject>(object));
253 }
254
255 void SetString(RTWObject object, const char *id, const char *s) override
256 {
257 ospSetString(reinterpret_cast<OSPObject>(object), id, s);
258 }
259
260 void SetObject(RTWObject object, const char *id, RTWObject other) override
261 {
262 ospSetObject(reinterpret_cast<OSPObject>(object), id, reinterpret_cast<OSPObject>(other));
263 }
264
265 void SetObjectAsData(RTWObject target, const char *id, RTWDataType type, RTWObject obj) override{
266 ospSetObjectAsData(reinterpret_cast<OSPObject>(target), id, (OSPDataType)type,
267 reinterpret_cast<OSPObject>(obj));
268 }
269
270 void SetParam(RTWObject object, const char *id, RTWDataType dataType, const void* mem) override
271 {
272 ospSetParam(reinterpret_cast<OSPObject>(object), id, static_cast<OSPDataType>(dataType),
273 mem);
274 }
275
276 void SetInt(RTWObject object, const char *id, int32_t x) override
277 {
278 ospSetInt(reinterpret_cast<OSPObject>(object), id, x);
279 }
280
281 void SetUInt(RTWObject object, const char *id, uint32_t x) override
282 {
283#if OSPRAY_VERSION_MAJOR < 3
284 ospSetInt(reinterpret_cast<OSPObject>(object), id, static_cast<int>(x));
285#else
286 ospSetUInt(reinterpret_cast<OSPObject>(object), id, x);
287#endif
288 }
289
290 void SetBool(RTWObject object, const char *id, bool x) override
291 {
292 ospSetBool(reinterpret_cast<OSPObject>(object), id, x);
293 }
294
295 void SetFloat(RTWObject object, const char *id, float x) override
296 {
297 ospSetFloat(reinterpret_cast<OSPObject>(object), id, x);
298 }
299
300 void SetLinear2f(RTWObject object, const char *id, float x, float y, float z, float w) override
301 {
302#if OSPRAY_VERSION_MAJOR < 3
303 ospSetVec4f(reinterpret_cast<OSPObject>(object), id, x, y, z, w);
304#else
305 ospSetLinear2f(reinterpret_cast<OSPObject>(object), id, x, y, z, w);
306#endif
307 }
308
309 void SetBox1f(RTWObject object, const char *id, float x, float y) override
310 {
311#if OSPRAY_VERSION_MAJOR < 3
312 ospSetVec2f(reinterpret_cast<OSPObject>(object), id, x, y);
313#else
314 ospSetBox1f(reinterpret_cast<OSPObject>(object), id, x, y);
315#endif
316 }
317
318 void SetVec2f(RTWObject object, const char *id, float x, float y) override
319 {
320 ospSetVec2f(reinterpret_cast<OSPObject>(object), id, x, y);
321 }
322
323 void SetVec2i(RTWObject object, const char *id, int x, int y) override
324 {
325 ospSetVec2i(reinterpret_cast<OSPObject>(object), id, x, y);
326 }
327
328 void SetVec3i(RTWObject object, const char *id, int x, int y, int z) override
329 {
330 ospSetVec3i(reinterpret_cast<OSPObject>(object), id, x, y, z);
331 }
332
333 void SetVec3f(RTWObject object, const char *id, float x, float y, float z) override
334 {
335 ospSetVec3f(reinterpret_cast<OSPObject>(object), id, x, y, z);
336 }
337
338 void SetVec4f(RTWObject object, const char *id, float x, float y, float z, float w) override
339 {
340 ospSetVec4f(reinterpret_cast<OSPObject>(object), id, x, y, z, w);
341 }
342
343 void RemoveParam(RTWObject object, const char *id) override
344 {
345 ospRemoveParam(reinterpret_cast<OSPObject>(object), id);
346 }
347
348 void Commit(RTWObject object) override
349 {
350 ospCommit(reinterpret_cast<OSPObject>(object));
351 }
352
353 float RenderFrame(RTWFrameBuffer frameBuffer, RTWRenderer renderer, RTWCamera camera, RTWWorld world) override
354 {
355 return ospRenderFrameBlocking(reinterpret_cast<OSPFrameBuffer>(frameBuffer), reinterpret_cast<OSPRenderer>(renderer),
356 reinterpret_cast<OSPCamera>(camera), reinterpret_cast<OSPWorld>(world));
357 }
358
359 void FrameBufferClear(RTWFrameBuffer frameBuffer) override
360 {
361 ospResetAccumulation(reinterpret_cast<OSPFrameBuffer>(frameBuffer));
362 }
363
364 const void* MapFrameBuffer(RTWFrameBuffer frameBuffer, const RTWFrameBufferChannel channel) override
365 {
366 return ospMapFrameBuffer(reinterpret_cast<OSPFrameBuffer>(frameBuffer), static_cast<OSPFrameBufferChannel>(channel));
367 }
368
369 void UnmapFrameBuffer(const void *mapped, RTWFrameBuffer frameBuffer) override
370 {
371 ospUnmapFrameBuffer(mapped, reinterpret_cast<OSPFrameBuffer>(frameBuffer));
372 }
373
374 void SetDepthNormalizationGL(RTWFrameBuffer /*frameBuffer*/, float /*clipMin*/, float /*clipMax*/) override
375 {
376 // not supported
377 }
378
379 int GetColorTextureGL(RTWFrameBuffer /*frameBuffer*/) override
380 {
381 // not supported
382 return 0;
383 }
384
385 int GetDepthTextureGL(RTWFrameBuffer /*frameBuffer*/) override
386 {
387 // not supported
388 return 0;
389 }
390 };
391VTK_ABI_NAMESPACE_END
392}
#define OSP_FB_SRGBA
Definition RTWrapper.h:117
#define OSPDataType
Definition RTWrapper.h:35
#define ospNewGeometry
Definition RTWrapper.h:176
#define ospSetVec2f
Definition RTWrapper.h:149
#define ospSetObjectAsData
Definition RTWrapper.h:155
#define ospNewGroup
Definition RTWrapper.h:169
#define ospSetString
Definition RTWrapper.h:152
#define OSP_TEXTURE_SRGBA
Definition RTWrapper.h:113
#define ospSetBox1f
Definition RTWrapper.h:143
#define ospSetVec2i
Definition RTWrapper.h:147
#define ospUnmapFrameBuffer
Definition RTWrapper.h:187
#define ospNewTexture
Definition RTWrapper.h:172
#define OSPWorld
Definition RTWrapper.h:20
#define OSP_TEXTURE_SRGB
Definition RTWrapper.h:112
#define ospNewGeometricModel
Definition RTWrapper.h:178
#define OSPTextureFormat
Definition RTWrapper.h:28
#define ospNewVolume
Definition RTWrapper.h:175
#define ospNewVolumetricModel
Definition RTWrapper.h:179
#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:151
#define ospNewWorld
Definition RTWrapper.h:180
#define OSP_TEXTURE_RGBA8
Definition RTWrapper.h:109
#define ospNewMaterial
Definition RTWrapper.h:173
#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:148
#define ospSetUInt
Definition RTWrapper.h:146
#define ospSetObject
Definition RTWrapper.h:154
#define ospNewSharedData2D
Definition RTWrapper.h:167
#define OSP_TEXTURE_RGBA32F
Definition RTWrapper.h:106
#define ospNewData
Definition RTWrapper.h:162
#define ospNewFrameBuffer
Definition RTWrapper.h:182
#define ospSetInt
Definition RTWrapper.h:144
#define ospRemoveParam
Definition RTWrapper.h:157
#define ospNewTransferFunction
Definition RTWrapper.h:174
#define ospSetParam
Definition RTWrapper.h:153
#define OSPCamera
Definition RTWrapper.h:24
#define ospSetLinear2f
Definition RTWrapper.h:145
#define ospSetVec3f
Definition RTWrapper.h:150
#define OSP_TEXTURE_R32F
Definition RTWrapper.h:104
#define ospNewCamera
Definition RTWrapper.h:170
#define ospNewSharedData3D
Definition RTWrapper.h:168
#define ospSetFloat
Definition RTWrapper.h:141
#define ospRelease
Definition RTWrapper.h:160
#define OSPVolume
Definition RTWrapper.h:32
#define ospNewSharedData1D
Definition RTWrapper.h:166
#define ospCommit
Definition RTWrapper.h:159
#define ospMapFrameBuffer
Definition RTWrapper.h:186
#define ospNewLight
Definition RTWrapper.h:171
#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:181
#define OSP_FB_RGBA32F
Definition RTWrapper.h:115
#define OSP_TEXTURE_LA8
Definition RTWrapper.h:111
#define ospNewRenderer
Definition RTWrapper.h:183
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
void SetUInt(RTWObject object, const char *id, uint32_t x) override
void SetLinear2f(RTWObject object, const char *id, float x, float y, float z, float w) 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
RTWMaterial NewMaterial(const char *material_type) 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
void SetBox1f(RTWObject object, const char *id, float x, float y) 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
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 *)