1 #include "VisRTXBackend.h"
2 
3 #include "vtkLogger.h"
4 
5 #define VISRTX_DYNLOAD
6 #include <VisRTX.h>
7 
8 #include <iomanip>
9 #include <iostream>
10 
11 #include "Camera.h"
12 #include "Data.h"
13 #include "FrameBuffer.h"
14 #include "Geometry.h"
15 #include "GeometricModel.h"
16 #include "Light.h"
17 #include "Material.h"
18 #include "Instance.h"
19 #include "Group.h"
20 #include "World.h"
21 #include "Object.h"
22 #include "Renderer.h"
23 #include "Texture.h"
24 
25 namespace RTW
26 {
Init()27     RTWError VisRTXBackend::Init()
28     {
29 #ifdef VISRTX_DYNLOAD
30         // Load library first
31         if (!VisRTX_LoadLibrary())
32         {
33             vtkLogF(TRACE, "Failed to load VisRTX library");
34             return RTW_UNKNOWN_ERROR;
35         }
36 #endif
37 
38         VisRTX::Context* rtx = VisRTX_GetContext();
39 
40         if (!rtx || rtx->GetDeviceCount() <= 0)
41         {
42             vtkLogF(TRACE, "VisRTX Error: Unsupported device");
43             return RTW_UNSUPPORTED_DEVICE;
44         }
45 
46         //uint32_t n = rtx->GetDeviceCount();
47         //for (uint32_t i = 0; i < n; ++i)
48         //{
49         //    std::string name = rtx->GetDeviceName(i);
50         //    uint64_t totalMem = rtx->GetDeviceMemoryTotal(i);
51         //    uint64_t availMem = rtx->GetDeviceMemoryAvailable(i);
52 
53         //    float totalGB = totalMem * 1e-9f;
54         //    float availGB = availMem * 1e-9f;
55 
56         //    std::cout << std::fixed << std::setprecision(1) << "Device " << i << ": " << name << " (Total: " << totalGB << " GB, Available: " << availGB << " GB)" << std::endl;
57         //}
58 
59         // Let VisRTX choose the default device(s)
60 
61         return RTW_NO_ERROR;
62     }
63 
Shutdown()64     void VisRTXBackend::Shutdown()
65     {
66         // .. nothing to do here
67     }
68 
IsSupported(RTWFeature feature) const69     bool VisRTXBackend::IsSupported(RTWFeature feature) const
70     {
71         switch (feature)
72         {
73         case RTW_DEPTH_NORMALIZATION:
74             return true;
75         case RTW_OPENGL_INTEROP:
76             return true;
77         case RTW_ANIMATED_PARAMETERIZATION:
78             return true;
79         case RTW_DENOISER:
80             return true;
81         default:
82             break;
83         }
84         return false;
85     }
86 
NewData(RTWDataType dataType,size_t numElements)87     RTWData VisRTXBackend::NewData(RTWDataType dataType, size_t numElements)
88     {
89         return reinterpret_cast<RTWData>(new Data(nullptr, dataType, numElements));
90     }
91 
NewGeometry(const char * type)92     RTWGeometry VisRTXBackend::NewGeometry(const char *type)
93     {
94         return reinterpret_cast<RTWGeometry>(new Geometry(type));
95     }
96 
NewTexture(const char * type)97     RTWTexture VisRTXBackend::NewTexture(const char* type)
98     {
99         return reinterpret_cast<RTWTexture>(new Texture(type));
100     }
101 
NewLight(const char * light_type)102     RTWLight VisRTXBackend::NewLight(const char *light_type)
103     {
104         return reinterpret_cast<RTWLight>(new Light(light_type));
105     }
106 
NewMaterial(const char * renderer_type,const char * light_type)107     RTWMaterial VisRTXBackend::NewMaterial(const char *renderer_type, const char *light_type)
108     {
109         return reinterpret_cast<RTWMaterial>(new Material(light_type));
110     }
111 
NewRenderer(const char * type)112     RTWRenderer VisRTXBackend::NewRenderer(const char *type)
113     {
114         return reinterpret_cast<RTWRenderer>(new Renderer(type));
115     }
116 
NewCamera(const char * type)117     RTWCamera VisRTXBackend::NewCamera(const char *type)
118     {
119         return reinterpret_cast<RTWCamera>(new Camera(type));
120     }
121 
NewWorld()122     RTWWorld VisRTXBackend::NewWorld()
123     {
124         return reinterpret_cast<RTWWorld>(new World());
125     }
126 
NewInstance(RTWGroup group)127     RTWInstance VisRTXBackend::NewInstance(RTWGroup group)
128     {
129         return reinterpret_cast<RTWInstance>(new Instance(reinterpret_cast<Group *>(group)));
130     }
131 
NewGroup()132     RTWGroup VisRTXBackend::NewGroup()
133     {
134         return reinterpret_cast<RTWGroup>(new Group());
135     }
136 
NewGeometricModel(RTWGeometry geometry)137     RTWGeometricModel VisRTXBackend::NewGeometricModel(RTWGeometry geometry)
138     {
139         return reinterpret_cast<RTWGeometricModel>(new GeometricModel(reinterpret_cast<Geometry *>(geometry)));
140     }
141 
142 
NewFrameBuffer(const rtw::vec2i & size,const RTWFrameBufferFormat format,const uint32_t frameBufferChannels)143     RTWFrameBuffer VisRTXBackend::NewFrameBuffer(const rtw::vec2i &size, const RTWFrameBufferFormat format, const uint32_t frameBufferChannels)
144     {
145         return reinterpret_cast<RTWFrameBuffer>(new FrameBuffer(size, format, frameBufferChannels));
146     }
147 
Release(RTWObject object)148     void VisRTXBackend::Release(RTWObject object)
149     {
150         if (!object)
151             return;
152 
153         reinterpret_cast<Object*>(object)->Release();
154     }
155 
SetString(RTWObject object,const char * id,const char * s)156     void VisRTXBackend::SetString(RTWObject object, const char *id, const char *s)
157     {
158         if (!object)
159             return;
160 
161         reinterpret_cast<Object*>(object)->SetString(id, s);
162     }
163 
SetBool(RTWObject object,const char * id,bool b)164     void VisRTXBackend::SetBool(RTWObject object, const char *id, bool b)
165     {
166         if(!object)
167             return;
168 
169         reinterpret_cast<Object*>(object)->SetBool(id, b);
170     }
171 
SetObject(RTWObject object,const char * id,RTWObject other)172     void VisRTXBackend::SetObject(RTWObject object, const char *id, RTWObject other)
173     {
174         if (!object)
175             return;
176 
177         reinterpret_cast<Object*>(object)->SetObject(id, reinterpret_cast<Object*>(other));
178     }
179 
SetObjectAsData(RTWObject target,const char * id,RTWDataType type,RTWObject obj)180     void VisRTXBackend::SetObjectAsData(RTWObject target, const char *id, RTWDataType type, RTWObject obj)
181     {
182         if(!target)
183             return;
184 
185         reinterpret_cast<Object*>(target)->SetObject(id, reinterpret_cast<Object*>(obj));
186     }
187 
SetInt(RTWObject object,const char * id,int32_t x)188     void VisRTXBackend::SetInt(RTWObject object, const char *id, int32_t x)
189     {
190         if (!object)
191             return;
192 
193         reinterpret_cast<Object*>(object)->SetInt(id, x);
194     }
195 
SetFloat(RTWObject object,const char * id,float x)196     void VisRTXBackend::SetFloat(RTWObject object, const char *id, float x)
197     {
198         if (!object)
199             return;
200 
201         reinterpret_cast<Object*>(object)->SetFloat(id, x);
202     }
203 
SetVec2f(RTWObject object,const char * id,float x,float y)204     void VisRTXBackend::SetVec2f(RTWObject object, const char *id, float x, float y)
205     {
206         if (!object)
207             return;
208 
209         reinterpret_cast<Object*>(object)->SetVec2f(id, x, y);
210     }
211 
SetVec2i(RTWObject object,const char * id,int x,int y)212     void VisRTXBackend::SetVec2i(RTWObject object, const char *id, int x, int y)
213     {
214       if (!object)
215         return;
216 
217       reinterpret_cast<Object*>(object)->SetVec2i(id, x, y);
218     }
219 
SetVec3i(RTWObject object,const char * id,int x,int y,int z)220     void VisRTXBackend::SetVec3i(RTWObject object, const char *id, int x, int y, int z)
221     {
222         if (!object)
223             return;
224 
225         reinterpret_cast<Object*>(object)->SetVec3i(id, x, y, z);
226     }
227 
SetVec3f(RTWObject object,const char * id,float x,float y,float z)228     void VisRTXBackend::SetVec3f(RTWObject object, const char *id, float x, float y, float z)
229     {
230         if (!object)
231             return;
232 
233         reinterpret_cast<Object*>(object)->SetVec3f(id, x, y, z);
234     }
235 
SetVec4f(RTWObject object,const char * id,float x,float y,float z,float w)236     void VisRTXBackend::SetVec4f(RTWObject object, const char *id, float x, float y, float z, float w)
237     {
238         if (!object)
239             return;
240 
241         reinterpret_cast<Object*>(object)->SetVec4f(id, x, y, z, w);
242     }
243 
RemoveParam(RTWObject object,const char * id)244     void VisRTXBackend::RemoveParam(RTWObject object, const char *id)
245     {
246         if (object)
247         {
248             reinterpret_cast<Object*>(object)->RemoveParam(id);
249         }
250     }
251 
NewSharedData1D(const void * source,RTWDataType type,uint32_t numElements)252     RTWData VisRTXBackend::NewSharedData1D(const void *source, RTWDataType type, uint32_t numElements)
253     {
254         return reinterpret_cast<RTWData>(new Data(source, type, numElements, true));
255     }
256 
NewSharedData2D(const void * source,RTWDataType type,uint32_t numElements1,uint32_t numElements2)257     RTWData VisRTXBackend::NewSharedData2D(const void *source, RTWDataType type, uint32_t numElements1, uint32_t numElements2)
258     {
259         return reinterpret_cast<RTWData>(new Data(source, type, numElements1, numElements2, true));
260     }
261 
NewSharedData3D(const void * source,RTWDataType type,uint32_t numElements1,uint32_t numElements2,uint32_t numElements3)262     RTWData VisRTXBackend::NewSharedData3D(const void *source, RTWDataType type, uint32_t numElements1, uint32_t numElements2, uint32_t numElements3)
263     {
264         return reinterpret_cast<RTWData>(new Data(source, type, numElements1, numElements2, numElements3, true));
265     }
266 
NewCopyData1D(const void * source,RTWDataType type,size_t numElements)267     RTWData VisRTXBackend::NewCopyData1D(const void *source, RTWDataType type, size_t numElements)
268     {
269         return reinterpret_cast<RTWData>(new Data(source, type, numElements, false));
270     }
271 
NewCopyData2D(const void * source,RTWDataType type,size_t numElements1,size_t numElements2)272     RTWData VisRTXBackend::NewCopyData2D(const void *source, RTWDataType type, size_t numElements1, size_t numElements2)
273     {
274         return reinterpret_cast<RTWData>(new Data(source, type, numElements1, numElements2, false));
275     }
276 
NewCopyData3D(const void * source,RTWDataType type,size_t numElements1,size_t numElements2,size_t numElements3)277     RTWData VisRTXBackend::NewCopyData3D(const void *source, RTWDataType type, size_t numElements1, size_t numElements2, size_t numElements3)
278     {
279         return reinterpret_cast<RTWData>(new Data(source, type, numElements1, numElements2, numElements3, false));
280     }
281 
282 
Commit(RTWObject object)283     void VisRTXBackend::Commit(RTWObject object)
284     {
285         if (!object)
286             return;
287 
288         reinterpret_cast<Object*>(object)->Commit();
289     }
290 
RenderFrame(RTWFrameBuffer frameBuffer,RTWRenderer renderer,RTWCamera camera,RTWWorld world)291     float VisRTXBackend::RenderFrame(RTWFrameBuffer frameBuffer, RTWRenderer renderer, RTWCamera camera, RTWWorld world)
292     {
293         if (!renderer)
294             return 0.0f;
295 
296         return reinterpret_cast<Renderer*>(renderer)->RenderFrame(
297                 reinterpret_cast<FrameBuffer*>(frameBuffer),
298                 reinterpret_cast<Camera*>(camera),
299                 reinterpret_cast<World*>(world));
300     }
301 
FrameBufferClear(RTWFrameBuffer frameBuffer)302     void VisRTXBackend::FrameBufferClear(RTWFrameBuffer frameBuffer)
303     {
304         if (!frameBuffer)
305             return;
306 
307         reinterpret_cast<FrameBuffer*>(frameBuffer)->Clear();
308     }
309 
MapFrameBuffer(RTWFrameBuffer frameBuffer,const RTWFrameBufferChannel channel)310     const void* VisRTXBackend::MapFrameBuffer(RTWFrameBuffer frameBuffer, const RTWFrameBufferChannel channel)
311     {
312         if (!frameBuffer)
313             return nullptr;
314 
315         return reinterpret_cast<FrameBuffer*>(frameBuffer)->Map(channel);
316     }
317 
UnmapFrameBuffer(const void * mapped,RTWFrameBuffer frameBuffer)318     void VisRTXBackend::UnmapFrameBuffer(const void *mapped, RTWFrameBuffer frameBuffer)
319     {
320         if (!frameBuffer)
321             return;
322 
323         reinterpret_cast<FrameBuffer*>(frameBuffer)->Unmap(mapped);
324     }
325 
SetDepthNormalizationGL(RTWFrameBuffer frameBuffer,float clipMin,float clipMax)326     void VisRTXBackend::SetDepthNormalizationGL(RTWFrameBuffer frameBuffer, float clipMin, float clipMax)
327     {
328         if (!frameBuffer)
329             return;
330 
331         reinterpret_cast<FrameBuffer*>(frameBuffer)->SetDepthNormalizationGL(clipMin, clipMax);
332     }
333 
GetColorTextureGL(RTWFrameBuffer frameBuffer)334     int VisRTXBackend::GetColorTextureGL(RTWFrameBuffer frameBuffer)
335     {
336         if (!frameBuffer)
337             return 0;
338 
339         return reinterpret_cast<FrameBuffer*>(frameBuffer)->GetColorTextureGL();
340     }
341 
GetDepthTextureGL(RTWFrameBuffer frameBuffer)342     int VisRTXBackend::GetDepthTextureGL(RTWFrameBuffer frameBuffer)
343     {
344         if (!frameBuffer)
345             return 0;
346 
347         return reinterpret_cast<FrameBuffer*>(frameBuffer)->GetDepthTextureGL();
348     }
349 }
350