VisRTXBackend.cxx 9.71 KB
Newer Older
Tim Biedert's avatar
Tim Biedert committed
1
2
#include "VisRTXBackend.h"

3
4
#include "vtkLogger.h"

Tim Biedert's avatar
Tim Biedert committed
5
6
7
8
#define VISRTX_DYNLOAD
#include <VisRTX.h>

#include <iomanip>
9
#include <iostream>
Tim Biedert's avatar
Tim Biedert committed
10

11
#include "Camera.h"
Tim Biedert's avatar
Tim Biedert committed
12
13
14
#include "Data.h"
#include "FrameBuffer.h"
#include "Geometry.h"
15
#include "GeometricModel.h"
Tim Biedert's avatar
Tim Biedert committed
16
17
#include "Light.h"
#include "Material.h"
18
19
20
#include "Instance.h"
#include "Group.h"
#include "World.h"
21
22
23
#include "Object.h"
#include "Renderer.h"
#include "Texture.h"
Tim Biedert's avatar
Tim Biedert committed
24
25
26

namespace RTW
{
27
    RTWError VisRTXBackend::Init()
Tim Biedert's avatar
Tim Biedert committed
28
29
30
31
32
    {
#ifdef VISRTX_DYNLOAD
        // Load library first
        if (!VisRTX_LoadLibrary())
        {
33
            vtkLogF(TRACE, "Failed to load VisRTX library");
Tim Biedert's avatar
Tim Biedert committed
34
35
36
37
38
39
            return RTW_UNKNOWN_ERROR;
        }
#endif

        VisRTX::Context* rtx = VisRTX_GetContext();

40
        if (!rtx || rtx->GetDeviceCount() <= 0)
41
        {
42
            vtkLogF(TRACE, "VisRTX Error: Unsupported device");
Tim Biedert's avatar
Tim Biedert committed
43
            return RTW_UNSUPPORTED_DEVICE;
44
        }
Tim Biedert's avatar
Tim Biedert committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

        //uint32_t n = rtx->GetDeviceCount();
        //for (uint32_t i = 0; i < n; ++i)
        //{
        //    std::string name = rtx->GetDeviceName(i);
        //    uint64_t totalMem = rtx->GetDeviceMemoryTotal(i);
        //    uint64_t availMem = rtx->GetDeviceMemoryAvailable(i);

        //    float totalGB = totalMem * 1e-9f;
        //    float availGB = availMem * 1e-9f;

        //    std::cout << std::fixed << std::setprecision(1) << "Device " << i << ": " << name << " (Total: " << totalGB << " GB, Available: " << availGB << " GB)" << std::endl;
        //}

        // Let VisRTX choose the default device(s)

        return RTW_NO_ERROR;
    }

    void VisRTXBackend::Shutdown()
    {
        // .. nothing to do here
    }

    bool VisRTXBackend::IsSupported(RTWFeature feature) const
    {
        switch (feature)
        {
        case RTW_DEPTH_NORMALIZATION:
            return true;
        case RTW_OPENGL_INTEROP:
            return true;
        case RTW_ANIMATED_PARAMETERIZATION:
            return true;
        case RTW_DENOISER:
            return true;
81
82
        default:
            break;
Tim Biedert's avatar
Tim Biedert committed
83
84
85
86
        }
        return false;
    }

87
    RTWData VisRTXBackend::NewData(RTWDataType dataType, size_t numElements)
Tim Biedert's avatar
Tim Biedert committed
88
    {
89
        return reinterpret_cast<RTWData>(new Data(nullptr, dataType, numElements));
Tim Biedert's avatar
Tim Biedert committed
90
91
92
93
94
95
96
97
98
99
100
101
    }

    RTWGeometry VisRTXBackend::NewGeometry(const char *type)
    {
        return reinterpret_cast<RTWGeometry>(new Geometry(type));
    }

    RTWTexture VisRTXBackend::NewTexture(const char* type)
    {
        return reinterpret_cast<RTWTexture>(new Texture(type));
    }

102
    RTWLight VisRTXBackend::NewLight(const char *light_type)
Tim Biedert's avatar
Tim Biedert committed
103
    {
104
        return reinterpret_cast<RTWLight>(new Light(light_type));
Tim Biedert's avatar
Tim Biedert committed
105
106
    }

107
    RTWMaterial VisRTXBackend::NewMaterial(const char *renderer_type, const char *light_type)
Tim Biedert's avatar
Tim Biedert committed
108
    {
109
        return reinterpret_cast<RTWMaterial>(new Material(light_type));
Tim Biedert's avatar
Tim Biedert committed
110
111
112
113
114
115
116
117
118
119
120
121
    }

    RTWRenderer VisRTXBackend::NewRenderer(const char *type)
    {
        return reinterpret_cast<RTWRenderer>(new Renderer(type));
    }

    RTWCamera VisRTXBackend::NewCamera(const char *type)
    {
        return reinterpret_cast<RTWCamera>(new Camera(type));
    }

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    RTWWorld VisRTXBackend::NewWorld()
    {
        return reinterpret_cast<RTWWorld>(new World());
    }

    RTWInstance VisRTXBackend::NewInstance(RTWGroup group)
    {
        return reinterpret_cast<RTWInstance>(new Instance(reinterpret_cast<Group *>(group)));
    }

    RTWGroup VisRTXBackend::NewGroup()
    {
        return reinterpret_cast<RTWGroup>(new Group());
    }

    RTWGeometricModel VisRTXBackend::NewGeometricModel(RTWGeometry geometry)
Tim Biedert's avatar
Tim Biedert committed
138
    {
139
        return reinterpret_cast<RTWGeometricModel>(new GeometricModel(reinterpret_cast<Geometry *>(geometry)));
Tim Biedert's avatar
Tim Biedert committed
140
141
    }

142

Tim Biedert's avatar
Tim Biedert committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    RTWFrameBuffer VisRTXBackend::NewFrameBuffer(const rtw::vec2i &size, const RTWFrameBufferFormat format, const uint32_t frameBufferChannels)
    {
        return reinterpret_cast<RTWFrameBuffer>(new FrameBuffer(size, format, frameBufferChannels));
    }

    void VisRTXBackend::Release(RTWObject object)
    {
        if (!object)
            return;

        reinterpret_cast<Object*>(object)->Release();
    }

    void VisRTXBackend::SetString(RTWObject object, const char *id, const char *s)
    {
        if (!object)
            return;

        reinterpret_cast<Object*>(object)->SetString(id, s);
    }

164
    void VisRTXBackend::SetBool(RTWObject object, const char *id, bool b)
Tim Biedert's avatar
Tim Biedert committed
165
    {
166
        if(!object)
Tim Biedert's avatar
Tim Biedert committed
167
168
            return;

169
        reinterpret_cast<Object*>(object)->SetBool(id, b);
Tim Biedert's avatar
Tim Biedert committed
170
171
    }

172
    void VisRTXBackend::SetObject(RTWObject object, const char *id, RTWObject other)
173
    {
Tim Biedert's avatar
Tim Biedert committed
174
175
176
        if (!object)
            return;

177
        reinterpret_cast<Object*>(object)->SetObject(id, reinterpret_cast<Object*>(other));
Tim Biedert's avatar
Tim Biedert committed
178
179
    }

180
    void VisRTXBackend::SetObjectAsData(RTWObject target, const char *id, RTWDataType type, RTWObject obj)
Tim Biedert's avatar
Tim Biedert committed
181
    {
182
        if(!target)
Tim Biedert's avatar
Tim Biedert committed
183
184
            return;

185
        reinterpret_cast<Object*>(target)->SetObject(id, reinterpret_cast<Object*>(obj));
Tim Biedert's avatar
Tim Biedert committed
186
187
    }

188
    void VisRTXBackend::SetInt(RTWObject object, const char *id, int32_t x)
Tim Biedert's avatar
Tim Biedert committed
189
190
191
192
    {
        if (!object)
            return;

193
        reinterpret_cast<Object*>(object)->SetInt(id, x);
Tim Biedert's avatar
Tim Biedert committed
194
195
    }

196
    void VisRTXBackend::SetFloat(RTWObject object, const char *id, float x)
Tim Biedert's avatar
Tim Biedert committed
197
198
199
200
    {
        if (!object)
            return;

201
        reinterpret_cast<Object*>(object)->SetFloat(id, x);
Tim Biedert's avatar
Tim Biedert committed
202
203
    }

204
    void VisRTXBackend::SetVec2f(RTWObject object, const char *id, float x, float y)
Tim Biedert's avatar
Tim Biedert committed
205
206
207
208
    {
        if (!object)
            return;

209
        reinterpret_cast<Object*>(object)->SetVec2f(id, x, y);
Tim Biedert's avatar
Tim Biedert committed
210
211
    }

212
    void VisRTXBackend::SetVec2i(RTWObject object, const char *id, int x, int y)
Tim Biedert's avatar
Tim Biedert committed
213
214
215
216
    {
      if (!object)
        return;

217
      reinterpret_cast<Object*>(object)->SetVec2i(id, x, y);
Tim Biedert's avatar
Tim Biedert committed
218
219
    }

220
    void VisRTXBackend::SetVec3i(RTWObject object, const char *id, int x, int y, int z)
Tim Biedert's avatar
Tim Biedert committed
221
222
223
224
    {
        if (!object)
            return;

225
        reinterpret_cast<Object*>(object)->SetVec3i(id, x, y, z);
Tim Biedert's avatar
Tim Biedert committed
226
227
    }

228
    void VisRTXBackend::SetVec3f(RTWObject object, const char *id, float x, float y, float z)
Tim Biedert's avatar
Tim Biedert committed
229
230
231
232
    {
        if (!object)
            return;

233
        reinterpret_cast<Object*>(object)->SetVec3f(id, x, y, z);
Tim Biedert's avatar
Tim Biedert committed
234
235
    }

236
    void VisRTXBackend::SetVec4f(RTWObject object, const char *id, float x, float y, float z, float w)
Tim Biedert's avatar
Tim Biedert committed
237
238
239
240
    {
        if (!object)
            return;

241
        reinterpret_cast<Object*>(object)->SetVec4f(id, x, y, z, w);
Tim Biedert's avatar
Tim Biedert committed
242
243
    }

244
245
246
247
248
249
250
251
    void VisRTXBackend::RemoveParam(RTWObject object, const char *id)
    {
        if (object)
        {
            reinterpret_cast<Object*>(object)->RemoveParam(id);
        }
    }

252
253
254
255
256
257
258
259
260
    RTWData VisRTXBackend::NewSharedData1D(const void *source, RTWDataType type, uint32_t numElements)
    {
        return reinterpret_cast<RTWData>(new Data(source, type, numElements, true));
    }

    RTWData VisRTXBackend::NewSharedData2D(const void *source, RTWDataType type, uint32_t numElements1, uint32_t numElements2)
    {
        return reinterpret_cast<RTWData>(new Data(source, type, numElements1, numElements2, true));
    }
261

262
263
264
265
    RTWData VisRTXBackend::NewSharedData3D(const void *source, RTWDataType type, uint32_t numElements1, uint32_t numElements2, uint32_t numElements3)
    {
        return reinterpret_cast<RTWData>(new Data(source, type, numElements1, numElements2, numElements3, true));
    }
266

267
268
269
270
    RTWData VisRTXBackend::NewCopyData1D(const void *source, RTWDataType type, size_t numElements)
    {
        return reinterpret_cast<RTWData>(new Data(source, type, numElements, false));
    }
271

272
273
274
275
276
    RTWData VisRTXBackend::NewCopyData2D(const void *source, RTWDataType type, size_t numElements1, size_t numElements2)
    {
        return reinterpret_cast<RTWData>(new Data(source, type, numElements1, numElements2, false));
    }

277
278
279
280
281
    RTWData VisRTXBackend::NewCopyData3D(const void *source, RTWDataType type, size_t numElements1, size_t numElements2, size_t numElements3)
    {
        return reinterpret_cast<RTWData>(new Data(source, type, numElements1, numElements2, numElements3, false));
    }

282

Tim Biedert's avatar
Tim Biedert committed
283
284
285
286
287
288
289
290
    void VisRTXBackend::Commit(RTWObject object)
    {
        if (!object)
            return;

        reinterpret_cast<Object*>(object)->Commit();
    }

291
    float VisRTXBackend::RenderFrame(RTWFrameBuffer frameBuffer, RTWRenderer renderer, RTWCamera camera, RTWWorld world)
Tim Biedert's avatar
Tim Biedert committed
292
293
294
295
    {
        if (!renderer)
            return 0.0f;

296
        return reinterpret_cast<Renderer*>(renderer)->RenderFrame(
297
298
                reinterpret_cast<FrameBuffer*>(frameBuffer),
                reinterpret_cast<Camera*>(camera),
299
                reinterpret_cast<World*>(world));
Tim Biedert's avatar
Tim Biedert committed
300
301
    }

302
    void VisRTXBackend::FrameBufferClear(RTWFrameBuffer frameBuffer)
Tim Biedert's avatar
Tim Biedert committed
303
304
305
306
    {
        if (!frameBuffer)
            return;

307
        reinterpret_cast<FrameBuffer*>(frameBuffer)->Clear();
Tim Biedert's avatar
Tim Biedert committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    }

    const void* VisRTXBackend::MapFrameBuffer(RTWFrameBuffer frameBuffer, const RTWFrameBufferChannel channel)
    {
        if (!frameBuffer)
            return nullptr;

        return reinterpret_cast<FrameBuffer*>(frameBuffer)->Map(channel);
    }

    void VisRTXBackend::UnmapFrameBuffer(const void *mapped, RTWFrameBuffer frameBuffer)
    {
        if (!frameBuffer)
            return;

        reinterpret_cast<FrameBuffer*>(frameBuffer)->Unmap(mapped);
    }

    void VisRTXBackend::SetDepthNormalizationGL(RTWFrameBuffer frameBuffer, float clipMin, float clipMax)
    {
        if (!frameBuffer)
            return;

        reinterpret_cast<FrameBuffer*>(frameBuffer)->SetDepthNormalizationGL(clipMin, clipMax);
    }

    int VisRTXBackend::GetColorTextureGL(RTWFrameBuffer frameBuffer)
    {
        if (!frameBuffer)
            return 0;

        return reinterpret_cast<FrameBuffer*>(frameBuffer)->GetColorTextureGL();
    }

    int VisRTXBackend::GetDepthTextureGL(RTWFrameBuffer frameBuffer)
    {
        if (!frameBuffer)
            return 0;

347
        return reinterpret_cast<FrameBuffer*>(frameBuffer)->GetDepthTextureGL();
Tim Biedert's avatar
Tim Biedert committed
348
349
    }
}