main_impl.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "main_impl.h"
  2. #include "device/orb_camera_ui.h"
  3. #include "core/image_utility.hpp"
  4. #include "module/image_streamer.h"
  5. #include "module/image_viewer.h"
  6. #include "module/viewport_downloader.hpp"
  7. #include "object_names.h"
  8. #include <boost/asio/io_context.hpp>
  9. #include <boost/asio/post.hpp>
  10. #include <boost/asio/steady_timer.hpp>
  11. #include <glad/gl.h>
  12. #include <GLFW/glfw3.h>
  13. #include <imgui.h>
  14. #include <imgui_impl_glfw.h>
  15. #include <imgui_impl_opengl3.h>
  16. // make glad happy
  17. #include "core/imgui_utility.hpp"
  18. using boost::asio::io_context;
  19. using boost::asio::post;
  20. using boost::asio::steady_timer;
  21. using boost::system::error_code;
  22. CUcontext cuda_ctx = nullptr;
  23. GLFWwindow *window = nullptr;
  24. smart_cuda_stream *default_cuda_stream = nullptr;
  25. io_context *main_ctx;
  26. object_manager *main_ob;
  27. std::unique_ptr<steady_timer> ui_timer;
  28. std::chrono::milliseconds ui_interval;
  29. // modules
  30. std::unique_ptr<orb_camera_ui> orb_cam;
  31. std::unique_ptr<image_viewer> bg_viewer; // background viewer
  32. std::unique_ptr<viewport_downloader> out_downloader;
  33. std::unique_ptr<image_streamer> out_streamer; // output streamer
  34. void init_cuda() {
  35. cuInit(0);
  36. auto cuda_dev = CUdevice();
  37. CUDA_API_CHECK(cuDeviceGet(&cuda_dev, 0)); // TODO: select device
  38. CUDA_API_CHECK(cuCtxCreate(&cuda_ctx, CU_CTX_SCHED_AUTO, cuda_dev));
  39. default_cuda_stream = new smart_cuda_stream();
  40. std::atexit([] { // elegant cleanup
  41. cuCtxDestroy(cuda_ctx);
  42. });
  43. }
  44. void init_window() {
  45. // set GLFW error handler
  46. glfwSetErrorCallback([](int error, const char *desc) {
  47. SPDLOG_ERROR("GLFW error: code = {}, description = {}", error, desc);
  48. });
  49. // create main window
  50. auto ret = glfwInit();
  51. assert(ret == GLFW_TRUE);
  52. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  53. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
  54. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  55. window = glfwCreateWindow(800, 600, "DepthGuide V1.-1", nullptr, nullptr); // TODO: select width and height
  56. assert(window != nullptr);
  57. glfwMakeContextCurrent(window);
  58. glfwSwapInterval(0);
  59. // load opengl functions
  60. auto version = gladLoadGL(glfwGetProcAddress);
  61. assert(version > 0);
  62. SPDLOG_INFO("Loaded OpenGL {}.{}", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
  63. // enable color blending
  64. glEnable(GL_BLEND);
  65. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  66. #ifndef NDEBUG
  67. // log opengl error
  68. glEnable(GL_DEBUG_OUTPUT);
  69. glDebugMessageCallback([](GLenum source, GLenum type, GLuint id, GLenum severity,
  70. GLsizei length, const GLchar *message, const void *user_data) {
  71. if (type == GL_DEBUG_TYPE_ERROR) {
  72. SPDLOG_ERROR("OpenGL error: type = {}, severity = {}, message = {}", type, severity, message);
  73. assert(false);
  74. }
  75. }, nullptr);
  76. #endif
  77. // setup imgui context
  78. IMGUI_CHECKVERSION();
  79. ImGui::CreateContext();
  80. auto io = ImGui::GetIO();
  81. io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
  82. ImGui::StyleColorsDark();
  83. ImGui_ImplGlfw_InitForOpenGL(window, true);
  84. ImGui_ImplOpenGL3_Init();
  85. // elegant cleanup
  86. std::atexit([] {
  87. ImGui_ImplOpenGL3_Shutdown();
  88. ImGui_ImplGlfw_Shutdown();
  89. ImGui::DestroyContext();
  90. glfwDestroyWindow(window);
  91. glfwTerminate();
  92. });
  93. }
  94. void init_om() {
  95. main_ctx = new io_context();
  96. main_ob = new object_manager({.ctx = main_ctx});
  97. OBJ_SAVE(img_color, image_u8c3());
  98. OBJ_SAVE(img_depth, image_f32c1());
  99. OBJ_SAVE(img_bg, image_u8c3());
  100. OBJ_SAVE(img_out, image_u8c4());
  101. OBJ_SIG(img_color)->connect(INT_MIN, [=](obj_name_type _) {
  102. OBJ_SAVE(img_bg, OBJ_QUERY(image_u8c3, img_color));
  103. });
  104. }
  105. void init_modules() {
  106. auto orb_cam_conf = orb_camera_ui::create_config{
  107. .cf_name = img_color, .df_name = img_depth,
  108. .stream = default_cuda_stream,
  109. };
  110. orb_cam = std::make_unique<orb_camera_ui>(orb_cam_conf);
  111. auto bg_viewer_conf = image_viewer::create_config{
  112. .mode = VIEW_COLOR_DEPTH, .flip_y = true,
  113. .stream = default_cuda_stream,
  114. };
  115. auto &bg_extra_conf = bg_viewer_conf.extra.color_depth;
  116. bg_extra_conf.c_name = img_color;
  117. bg_extra_conf.d_name = img_depth;
  118. bg_viewer = std::make_unique<image_viewer>(bg_viewer_conf);
  119. auto out_down_conf = viewport_downloader::create_config{
  120. .type = PIX_RGBA, .stream = default_cuda_stream
  121. };
  122. out_downloader = std::make_unique<viewport_downloader>(out_down_conf);
  123. auto out_streamer_conf = image_streamer::create_config{
  124. .img_name = img_out, .cuda_ctx = &cuda_ctx, .stream = default_cuda_stream
  125. };
  126. out_streamer = std::make_unique<image_streamer>(out_streamer_conf);
  127. }
  128. void ui_timer_func(error_code ec) {
  129. if (ec == boost::asio::error::operation_aborted) return;
  130. assert(ec == error_code());
  131. show_ui();
  132. ui_timer->expires_after(ui_interval);
  133. ui_timer->async_wait(ui_timer_func);
  134. }
  135. void init_all() {
  136. init_cuda();
  137. init_window();
  138. init_om();
  139. init_modules();
  140. ui_interval = std::chrono::milliseconds(33); // TODO: select refresh rate
  141. ui_timer = std::make_unique<steady_timer>(*main_ctx, ui_interval);
  142. ui_timer->async_wait(ui_timer_func);
  143. }
  144. void show_ui() {
  145. glfwPollEvents();
  146. ImGui_ImplOpenGL3_NewFrame();
  147. ImGui_ImplGlfw_NewFrame();
  148. ImGui::NewFrame();
  149. if (glfwWindowShouldClose(window)) {
  150. ui_timer->cancel();
  151. main_ctx->stop();
  152. return;
  153. }
  154. if (ImGui::Begin("Depth Guide Control")) {
  155. ImGui::PushItemWidth(200);
  156. if (ImGui::CollapsingHeader("Camera")) {
  157. auto id_guard = imgui_id_guard("camera");
  158. orb_cam->show();
  159. }
  160. if (ImGui::CollapsingHeader("Streamer")) {
  161. auto id_guard = imgui_id_guard("streamer");
  162. out_streamer->show();
  163. }
  164. if (ImGui::CollapsingHeader("Debug")) {
  165. if (ImGui::TreeNode("Background")) {
  166. bg_viewer->show();
  167. ImGui::TreePop();
  168. }
  169. if (ImGui::TreeNode("Memory Pool")) {
  170. if (ImGui::Button("Purge")) {
  171. post(*main_ctx, [] { global_mp.purge(); });
  172. }
  173. ImGui::TreePop();
  174. }
  175. }
  176. ImGui::PopItemWidth();
  177. }
  178. ImGui::End();
  179. ImGui::Render();
  180. cv::Size frame_size;
  181. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  182. glfwGetFramebufferSize(window, &frame_size.width, &frame_size.height);
  183. glViewport(0, 0, frame_size.width, frame_size.height);
  184. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  185. bg_viewer->render();
  186. // TODO: for test
  187. auto bg_img = out_downloader->download_rgba();
  188. OBJ_SAVE(img_out, bg_img);
  189. ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  190. glfwSwapBuffers(window);
  191. }
  192. void cleanup() {
  193. ui_timer = nullptr;
  194. orb_cam = nullptr;
  195. bg_viewer = nullptr;
  196. out_downloader = nullptr;
  197. out_streamer = nullptr;
  198. delete main_ob;
  199. delete main_ctx;
  200. }