-
Notifications
You must be signed in to change notification settings - Fork 25
/
unity.cu
353 lines (276 loc) · 9.36 KB
/
unity.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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
347
348
349
350
351
352
#include "neural-graphics-primitives/unity.h"
#include "Unity/IUnityInterface.h"
#include "Unity/IUnityGraphics.h"
#ifdef _WIN32
# include <GL/gl3w.h>
#else
# include <GL/glew.h>
#endif
#include <GLFW/glfw3.h>
#include "gl/GL.h"
#include "gl/GLU.h"
#include <neural-graphics-primitives/adam_optimizer.h>
#include <neural-graphics-primitives/camera_path.h>
#include <neural-graphics-primitives/common.h>
#include <neural-graphics-primitives/discrete_distribution.h>
#include <neural-graphics-primitives/nerf.h>
#include <neural-graphics-primitives/nerf_loader.h>
#include <neural-graphics-primitives/render_buffer.h>
#include <neural-graphics-primitives/sdf.h>
#include <neural-graphics-primitives/shared_queue.h>
#include <neural-graphics-primitives/trainable_buffer.cuh>
#include <neural-graphics-primitives/testbed.h>
#include <neural-graphics-primitives/common_device.cuh>
#include <neural-graphics-primitives/common.h>
#include <neural-graphics-primitives/render_buffer.h>
#include <neural-graphics-primitives/tinyexr_wrapper.h>
#include <tiny-cuda-nn/gpu_memory.h>
#include <filesystem/path.h>
#include <cuda_gl_interop.h>
#include <tiny-cuda-nn/multi_stream.h>
#include <tiny-cuda-nn/random.h>
#include <json/json.hpp>
#include <filesystem/path.h>
#include <thread>
#include "gl/GL.h"
#include "gl/GLU.h"
#include <memory>
using Texture = std::shared_ptr<ngp::GLTexture>;
using RenderBuffer = std::shared_ptr<ngp::CudaRenderBuffer>;
struct TextureData {
TextureData(const Texture& tex, const RenderBuffer& buf, int width, int heigth)
: surface_texture(tex), render_buffer(buf), width(width), height(height) {
}
~TextureData(){
surface_texture.reset();
render_buffer.reset();
};
Texture surface_texture;
RenderBuffer render_buffer;
int width;
int height;
};
static std::shared_ptr<ngp::Testbed> testbed = nullptr;
static std::unordered_map<GLuint, std::shared_ptr<TextureData>> textures;
// flags
bool graphics_initialized = false;
bool use_dlss = false;
static int _width;
static int _height;
static GLuint leftHandle;
static GLuint rightHandle;
float zero_matrix[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
float* view_matrix_left;
float* view_matrix_right;
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API unity_nerf_set_initialize_values(const char* scene, const char* snapshot, bool dlss, int width, int height){
use_dlss = dlss;
_width = width;
_height = height;
view_matrix_left = zero_matrix;
view_matrix_right = zero_matrix;
testbed = std::make_shared<ngp::Testbed>(
ngp::ETestbedMode::Nerf,
scene
);
if (snapshot) {
testbed->load_snapshot(
snapshot
);
}
tlog::info() << "instant ngp testbed created" ;
};
extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API unity_nerf_get_graphics_init_state(){
return graphics_initialized;
}
// this needs to happen in the render thread
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API unity_nerf_initialize_graphics() {
if(graphics_initialized){
tlog::info() << "graphics already initialized" ;
return;
}
if (!glfwInit()) {
std::cout << "Could not initialize glfw" << std::endl;
}
if (!gl3wInit()) {
std::cout << "Could not initialize gl3w" << std::endl;
}
#ifdef NGP_VULKAN
if (use_dlss) {
try {
ngp::vulkan_and_ngx_init();
}
catch (std::runtime_error exception) {
use_dlss= false;
std::cout << "Could not initialize vulkan" << std::endl;
}
}
#else
use_dlss = false;
#endif
graphics_initialized = true;
tlog::info() << "graphics initialized" ;
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API unity_nerf_deinit_ngx_vulkan(){
#ifdef NGP_VULKAN
if (use_dlss) {
ngp::vulkan_and_ngx_destroy();
use_dlss = false;
}
#endif
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API unity_nerf_deinitialize() {
textures.clear();
// #ifdef NGP_VULKAN
// if (use_dlss) {
// ngp::vulkan_and_ngx_destroy();
// use_dlss = false;
// }
// #endif
testbed.reset();
glfwTerminate();
leftHandle = 0;
rightHandle = 0;
view_matrix_left = NULL;
view_matrix_right = NULL;
graphics_initialized = false;
use_dlss = false;
tlog::info() << "instant ngp testbed deinitialized" ;
}
static GLuint UNITY_INTERFACE_API unity_nerf_create_texture(int width, int height) {
if (!testbed){
tlog::info() << "testbed not found!!" ;
return 0;
}
auto texture = std::make_shared<ngp::GLTexture>();
auto buffer = std::make_shared<ngp::CudaRenderBuffer>(texture);
Eigen::Vector2i render_res { width, height };
#if defined(NGP_VULKAN)
if (use_dlss) {
buffer->enable_dlss({ width, height });
// buffer->resize({ width, height });
Eigen::Vector2i texture_res { width, height };
render_res = buffer->in_resolution();
if (render_res.isZero()) {
render_res = texture_res / 16;
} else {
render_res = render_res.cwiseMin(texture_res);
}
if (buffer->dlss()) {
render_res = buffer->dlss()->clamp_resolution(render_res);
}
}
else{ buffer->disable_dlss();}
#endif
buffer->resize(render_res);
GLuint handle = texture->texture();
textures[handle] = std::make_shared<TextureData>(
texture,
buffer,
width,
height
);
tlog::info() << "GLTexture handle" << handle ;
return handle;
}
void UNITY_INTERFACE_API unity_nerf_update_texture() {
if (!testbed){
tlog::error() << "testbed not found" ;
return;
}
auto left = textures.find(leftHandle);
auto right = textures.find(rightHandle);
if (left == std::end(textures)) {
tlog::error() << "left texture handle not found" ;
return;
}
if (right == std::end(textures)) {
tlog::error() << "left texture handle not found" ;
return;
}
Eigen::Matrix<float, 3, 4> camera_left {view_matrix_left};
Eigen::Matrix<float, 3, 4> camera_right {view_matrix_right};
RenderBuffer render_buffer_left = left->second->render_buffer;
RenderBuffer render_buffer_right = right->second->render_buffer;
render_buffer_left->reset_accumulation();
render_buffer_right->reset_accumulation();
testbed->render_frame(camera_left,//testbed->m_camera,
camera_left,//testbed->m_camera,
Eigen::Vector4f::Zero(),
*render_buffer_left.get(),
true);
testbed->render_frame(camera_right,//testbed->m_camera,
camera_right,//testbed->m_camera,
Eigen::Vector4f::Zero(),
*render_buffer_right.get(),
true);
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API unity_nerf_destroy_texture(GLuint handle) {
if (!testbed)
return;
auto found = textures.find(handle);
if (found == std::end(textures)) {
return;
}
found->second->render_buffer->reset_accumulation();
found->second->render_buffer.reset();
found->second->surface_texture.reset();
found->second.reset();
tlog::info() << "GLTexture and render buffer destroyed" ;
}
// utility functions
static void UNITY_INTERFACE_API unity_nerf_reset_camera(){
if (!testbed)
return;
testbed->reset_camera();
}
extern "C" void UNITY_INTERFACE_API unity_nerf_update_aabb_crop(float* min_vec, float* max_vec){
if (!testbed)
return;
Eigen::Vector3f min_aabb {min_vec};
Eigen::Vector3f max_aabb {max_vec};
testbed->m_render_aabb = ngp::BoundingBox(min_aabb, max_aabb);
}
const int INIT_EVENT = 0x0001;
const int DRAW_EVENT = 0x0002;
const int DEINIT_EVENT = 0x0003;
const int CREATE_TEX = 0x0004;
const int DEINIT_VULKAN = 0x0005;
static void UNITY_INTERFACE_API unity_nerf_run_on_render_thread(int eventID)
{
switch (eventID)
{
case INIT_EVENT:
unity_nerf_initialize_graphics();
break;
case CREATE_TEX:
leftHandle = unity_nerf_create_texture(_width, _height);
rightHandle = unity_nerf_create_texture(_width, _height);
break;
case DRAW_EVENT:
unity_nerf_update_texture();
break;
case DEINIT_EVENT:
unity_nerf_destroy_texture(leftHandle);
unity_nerf_destroy_texture(rightHandle);
unity_nerf_deinitialize();
break;
case DEINIT_VULKAN:
unity_nerf_deinit_ngx_vulkan();
break;
}
}
// --------------------------------------------------------------------------
// GetRenderEventFunc, an example function we export which is used to get a rendering event callback function.
extern "C" UnityRenderingEvent UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetRenderEventFunc(){
return unity_nerf_run_on_render_thread;
}
extern "C" GLuint UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API unity_nerf_get_left_handle(){
return leftHandle;
}
extern "C" GLuint UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API unity_nerf_get_right_handle(){
return rightHandle;
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API unity_nerf_update_stereo_view_matrix(float* left, float* right){
view_matrix_left = left;
view_matrix_right = right;
}