#include "application.h" #include "rendering/render_api.h" // RendererAPI #include "settings_manager.h" #include "platform/opengl/opengl_device.h" #include "platform/vulkan/vulkan_device.h" #include #include #include #include #include #include #include // std::getenv (the DONUT_AUTOEXPORT dev hook) namespace Donut { Application* Application::s_instance = nullptr; // scroll is delivered through a GLFW callback; accumulate it here and drain it // once per frame in update_input (chaining ImGui's own scroll handler). static double g_scroll_accum = 0.0; static GLFWscrollfun g_prev_scroll = nullptr; static void donut_scroll_callback(GLFWwindow* w, double x, double y) { if (g_prev_scroll) g_prev_scroll(w, x, y); g_scroll_accum += y; } Application::Application(const std::string& name, int width, int height) { s_instance = this; // select the render API before the window is created: the window is built // differently for Vulkan (GLFW_NO_API) than for OpenGL. Logger::init(); SettingsManager::initialize(); { const auto& settings = SettingsManager::get_settings_const(); RendererAPI::set_api(settings.graphics.render_api == "Vulkan" ? RendererAPI::API::Vulkan : RendererAPI::API::OpenGL); } if (RendererAPI::get_api() == RendererAPI::API::Vulkan) RHI::vulkan_prepare_glfw(); // must precede glfwInit() inside the Window ctor m_window = create_scope(name, width, height); m_window->set_event_callback([this](Event& event) { on_event(event); }); on_init(); } Application::~Application() { on_shutdown(); s_instance = nullptr; } auto Application::on_init() -> void { int w = 0, h = 0; glfwGetFramebufferSize((GLFWwindow*)m_window->get_native_window(), &w, &h); if (RendererAPI::get_api() == RendererAPI::API::Vulkan) m_device = RHI::create_vulkan_device(); else m_device = RHI::create_opengl_device(); RHI::NativeWindow native{ m_window->get_native_window(), w, h }; if (!m_device->init(native)) { DONUT_ERROR("RHI device initialization failed"); return; } m_scene = create_scope(); m_render_path = create_scope(); m_render_path->init(*m_device, m_scene->hdri_path); m_ui = create_scope(); m_device->init_imgui(); // the UI layer owns its typography and its imgui.ini section; both go into // the device-created ImGui context before its first NewFrame reads the ini. m_ui->init(); g_prev_scroll = glfwSetScrollCallback((GLFWwindow*)m_window->get_native_window(), donut_scroll_callback); m_device->set_vsync(SettingsManager::get_v_sync_enabled()); // apply the persisted preference glfwGetWindowSize((GLFWwindow*)m_window->get_native_window(), &m_windowed_w, &m_windowed_h); m_start_time = glfwGetTime(); DONUT_INFO("Application ready on {} backend", m_device->device_name()); } auto Application::run() -> void { while (m_running) { double frame_start = glfwGetTime(); glfwPollEvents(); if (!m_minimized && m_device) render_frame(); // with vsync off, cap to the target FPS (vsync itself paces otherwise). if (!SettingsManager::get_v_sync_enabled()) { int fps = SettingsManager::get_target_fps(); if (fps > 0) { double budget = 1.0 / (double)fps; double elapsed = glfwGetTime() - frame_start; if (elapsed < budget) std::this_thread::sleep_for(std::chrono::duration(budget - elapsed)); } } } } auto Application::set_resolution(int w, int h) -> void { if (m_window && !is_fullscreen()) glfwSetWindowSize((GLFWwindow*)m_window->get_native_window(), w, h); } auto Application::set_fullscreen(bool on) -> void { if (!m_window) return; GLFWwindow* win = (GLFWwindow*)m_window->get_native_window(); if (on == is_fullscreen()) return; if (on) { glfwGetWindowPos(win, &m_windowed_x, &m_windowed_y); glfwGetWindowSize(win, &m_windowed_w, &m_windowed_h); GLFWmonitor* mon = glfwGetPrimaryMonitor(); const GLFWvidmode* mode = glfwGetVideoMode(mon); glfwSetWindowMonitor(win, mon, 0, 0, mode->width, mode->height, mode->refreshRate); } else { glfwSetWindowMonitor(win, nullptr, m_windowed_x, m_windowed_y, m_windowed_w, m_windowed_h, 0); } } auto Application::set_vsync(bool on) -> void { if (m_device) m_device->set_vsync(on); } auto Application::set_ui_scale(float scale) -> void { ImGui::GetIO().FontGlobalScale = scale; } auto Application::export_frame(const ExportConfig& cfg) -> int { return (m_render_path && m_scene) ? m_render_path->export_frame(*m_scene, cfg) : 0; } auto Application::is_fullscreen() const -> bool { return m_window && glfwGetWindowMonitor((GLFWwindow*)m_window->get_native_window()) != nullptr; } auto Application::get_window_size(int& w, int& h) const -> void { w = 0; h = 0; if (m_window) glfwGetWindowSize((GLFWwindow*)m_window->get_native_window(), &w, &h); } auto Application::render_frame() -> void { RHI::CommandList* cmd = m_device->begin_frame(glm::vec4(0.05f, 0.06f, 0.10f, 1.0f)); if (!cmd) return; // frame skipped (e.g. swapchain recreation) m_device->imgui_new_frame(); // keep the scene camera's projection current before the UI builds, so the // scene-view gizmo and the render agree on the same matrices this frame. int w = 0, h = 0; glfwGetFramebufferSize((GLFWwindow*)m_window->get_native_window(), &w, &h); m_scene->scene_camera.set_projection(45.0f, (float)w / (float)std::max(h, 1), 0.1f, 1000.0f); View view = m_ui->draw(UIContext{ *m_scene, m_device->device_name(), *this }); // dev hook: DONUT_AUTOEXPORT= fires one export from this exact // mid-frame spot (the Export button runs inside ui->draw, i.e. between // begin_frame and render) at that frame (30 if it's not a number), then // quits. lets the mid-frame export path be exercised headlessly — exit 0 // means no segfault. if (const char* autoexport = std::getenv("DONUT_AUTOEXPORT")) { static int frame = 0; const int at = std::atoi(autoexport) > 0 ? std::atoi(autoexport) : 30; if (++frame == at) { ExportConfig cfg; cfg.width = 1280; cfg.height = 720; int written = export_frame(cfg); DONUT_INFO("autoexport wrote {} file(s)", written); close(); } } update_input(view); m_render_path->sync_hdri(m_scene->hdri_path); float time = (float)(glfwGetTime() - m_start_time); m_render_path->render(*cmd, *m_scene, view, w, h, m_user_moving, time); m_device->end_frame(); } auto Application::update_input(View view) -> void { GLFWwindow* window = (GLFWwindow*)m_window->get_native_window(); bool over_ui = ImGui::GetCurrentContext() && ImGui::GetIO().WantCaptureMouse; double now = glfwGetTime(); float dt = m_last_frame_time > 0.0 ? (float)(now - m_last_frame_time) : 0.0f; m_last_frame_time = now; double mx = 0, my = 0; glfwGetCursorPos(window, &mx, &my); bool left_down = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS; // a None view has no live viewport, so there's no camera to drive. if (view == View::None) { m_left_was_down = left_down; // keep the drag-edge state coherent g_scroll_accum = 0.0; // no live view: nothing to scroll m_user_moving = false; return; } const bool scene_view = view == View::Scene; if (!scene_view && m_scene->sim_camera.get_camera_mode() == CameraMode::FPS) { Camera& cam = m_scene->sim_camera; if (left_down && !m_left_was_down) { m_fps_last_x = mx; m_fps_last_y = my; } if (left_down && !over_ui) cam.on_mouse_move(float(mx - m_fps_last_x), float(m_fps_last_y - my)); m_fps_last_x = mx; m_fps_last_y = my; m_left_was_down = left_down; bool over_kb = ImGui::GetCurrentContext() && ImGui::GetIO().WantCaptureKeyboard; bool moved = false; if (!over_kb && dt > 0.0f) { float mdt = dt * (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS ? 4.0f : 1.0f); if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { cam.move_forward(mdt); moved = true; } if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { cam.move_backward(mdt); moved = true; } if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { cam.move_left(mdt); moved = true; } if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { cam.move_right(mdt); moved = true; } if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) { cam.move_up(mdt); moved = true; } if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) { cam.move_down(mdt); moved = true; } } g_scroll_accum = 0.0; m_user_moving = moved || (left_down && !over_ui); } else { Camera& cam = scene_view ? m_scene->scene_camera : m_scene->sim_camera; if (left_down && !m_left_was_down && !over_ui) cam.process_orbital_mouse_button(GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS, 0); else if (!left_down && m_left_was_down) cam.process_orbital_mouse_button(GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE, 0); m_left_was_down = left_down; cam.process_orbital_mouse_move(mx, my); double scroll = g_scroll_accum; g_scroll_accum = 0.0; if (scroll != 0.0 && !over_ui) cam.process_orbital_scroll(0.0, scroll); m_user_moving = cam.is_dragging() || cam.is_panning(); } } auto Application::close() -> void { m_running = false; } auto Application::on_event(Event& event) -> void { EventDispatcher dispatcher(event); dispatcher.dispatch([this, &event](WindowCloseEvent& /*e*/) { m_running = false; event.handled = true; return true; }); dispatcher.dispatch([this, &event](WindowResizeEvent& e) { m_minimized = (e.get_width() == 0 || e.get_height() == 0); if (m_device) m_device->resize(e.get_width(), e.get_height()); event.handled = true; return true; }); } auto Application::on_shutdown() -> void { if (m_device) { m_device->wait_idle(); if (m_render_path) m_render_path->shutdown(); m_render_path.reset(); m_ui.reset(); m_scene.reset(); m_device->shutdown(); m_device.reset(); } SettingsManager::shutdown(); Logger::shutdown(); } }