Skip to content

Commit 44b2dca

Browse files
authored
Unshackle renderer; move DearImGui into engine; merge editor with context (#42)
* Merge editor into context; use Gui interface for Renderer - Move `DearImGui` to engine (internal), have `Engine` create a `GuiDearImGui` instance for the main window. * Restrict Dear ImGui to a single instance * Avoid passing incomplete `Renderer` instance to `Gui`. A class instance technically isn't constructed until the constructor returns: `Renderer` passes `*this` to `Gui` to initialize it. Duplicate `DearImGui::CreateInfo` as `Gui::InitInfo` instead. * Remove (now) unecessary API, make DearImGui constructor private `ConstructTag` approach yields a cleaner implementation: can pass such a dummy object to `std::make_unique` directly instead of having to construct `DearImGui` instance manually, but it muddies the API. Having a private constructor makes it clear to readers that an instance of this class cannot be constructed externally. * Integrate DearImGui into engine - Move `Glfw::Wsi` into its own header. (Avoid including vulkan in glfw.hpp). * Move Window ownership to Context - Add docs for Engine and Context. * Cleanup, docs * Lock shared mutex before using device queue - Add `Engine::Validation` to enable users to override default logic. This will be useful with RenderDoc, which refuses to load an app that even attempts to check if validation layers are available, unless layers are available in PATH. - Remove `.at()` indexing in `Scene`: harder to figure out where the problem is when debugging: stack unwinds all the way to try/catch in main. - Add default camera to `Scene` on its construction.
1 parent 492e8bd commit 44b2dca

File tree

33 files changed

+404
-309
lines changed

33 files changed

+404
-309
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ target_sources(${PROJECT_NAME} PRIVATE
5656

5757
target_link_libraries(${PROJECT_NAME} PRIVATE
5858
facade::context
59-
facade::editor
6059
facade::compile-options
6160
)
6261

lib/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ add_subdirectory(glfw)
1818
add_subdirectory(render)
1919
add_subdirectory(scene)
2020
add_subdirectory(engine)
21-
add_subdirectory(editor)
2221
add_subdirectory(context)

lib/context/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,15 @@ endif()
3535
target_sources(${PROJECT_NAME} PRIVATE
3636
include/${target_prefix}/context/context.hpp
3737

38+
include/${target_prefix}/context/editor/common.hpp
39+
include/${target_prefix}/context/editor/inspector.hpp
40+
include/${target_prefix}/context/editor/log.hpp
41+
include/${target_prefix}/context/editor/scene_tree.hpp
42+
3843
src/context.cpp
44+
45+
src/editor/common.cpp
46+
src/editor/inspector.cpp
47+
src/editor/log.cpp
48+
src/editor/scene_tree.cpp
3949
)

lib/context/include/facade/context/context.hpp

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,68 @@
44
#include <facade/util/time.hpp>
55

66
namespace facade {
7+
struct ContextCreateInfo {
8+
glm::uvec2 extent{1280, 800};
9+
char const* title{"facade"};
10+
std::uint8_t msaa_samples{2};
11+
bool auto_show{false};
12+
Engine::Validation validation{Engine::Validation::eDefault};
13+
};
14+
15+
///
16+
/// \brief Owns and operates a Window, Engine and Scene
17+
///
718
class Context {
19+
// order is important here: window and engine must be initialized before / destroyed after all other data members
20+
UniqueWin m_window;
21+
Engine m_engine;
22+
823
public:
9-
Context(Engine::CreateInfo const& create_info = {});
24+
using CreateInfo = ContextCreateInfo;
25+
///
26+
/// \brief Construct a Context instance
27+
///
28+
/// Throws if an instance already exists
29+
///
30+
Context(CreateInfo const& create_info = {}) noexcept(false);
1031

32+
///
33+
/// \brief Register a shader for the renderer to look up during draws
34+
///
1135
void add_shader(Shader shader);
1236

37+
///
38+
/// \brief Show the window
39+
///
1340
void show(bool reset_dt);
41+
///
42+
/// \brief Hide the window
43+
///
44+
void hide();
1445

15-
bool running() const { return engine.running(); }
46+
///
47+
/// \brief Check if window has been requested to be closed
48+
///
49+
bool running() const { return !glfwWindowShouldClose(m_window.get()); }
50+
///
51+
/// \brief Submit previous frame (if applicable), and begin a new frame
52+
/// \returns time elapsed since the previous call to next_frame()
53+
///
1654
float next_frame();
1755

18-
void request_stop() { engine.request_stop(); }
56+
///
57+
/// \brief Request window to be closed
58+
///
59+
void request_stop();
60+
61+
Glfw::Window const& window() const { return m_window; }
62+
Engine const& engine() const { return m_engine; }
63+
Glfw::State const& state() const { return m_window.get().state(); }
64+
Input const& input() const { return m_window.get().state().input; }
1965

20-
Engine engine;
66+
///
67+
/// \brief The scene to render every frame
68+
///
2169
Scene scene;
2270

2371
private:

lib/editor/include/facade/editor/common.hpp renamed to lib/context/include/facade/context/editor/common.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ class MainMenu : public MenuBar {
8585
~MainMenu();
8686
};
8787

88+
///
89+
/// \brief RAII Dear ImGui Popup
90+
///
91+
class Popup : public Openable {
92+
public:
93+
explicit Popup(char const* id, int flags = {});
94+
~Popup();
95+
96+
static void close_current();
97+
};
98+
8899
///
89100
/// \brief RAII Dear ImGui StyleVar stack
90101
///

lib/editor/include/facade/editor/inspector.hpp renamed to lib/context/include/facade/context/editor/inspector.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include <facade/editor/common.hpp>
2+
#include <facade/context/editor/common.hpp>
33
#include <facade/scene/material.hpp>
44
#include <facade/scene/node.hpp>
55
#include <limits>

lib/editor/include/facade/editor/log.hpp renamed to lib/context/include/facade/context/editor/log.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include <facade/editor/common.hpp>
2+
#include <facade/context/editor/common.hpp>
33
#include <facade/util/enum_array.hpp>
44
#include <facade/util/logger.hpp>
55
#include <vector>

lib/editor/include/facade/editor/scene_tree.hpp renamed to lib/context/include/facade/context/editor/scene_tree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include <facade/editor/common.hpp>
2+
#include <facade/context/editor/common.hpp>
33
#include <facade/scene/id.hpp>
44
#include <facade/util/fixed_string.hpp>
55

lib/context/src/context.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
#include <facade/context/context.hpp>
2+
#include <facade/render/renderer.hpp>
23

34
namespace facade {
4-
Context::Context(Engine::CreateInfo const& create_info) : engine(create_info), scene(engine.gfx()) {}
5+
namespace {
6+
UniqueWin make_window(glm::ivec2 extent, char const* title) {
7+
auto ret = Glfw::Window::make();
8+
glfwSetWindowTitle(ret.get(), title);
9+
glfwSetWindowSize(ret.get(), extent.x, extent.y);
10+
return ret;
11+
}
12+
} // namespace
13+
14+
Context::Context(CreateInfo const& create_info) noexcept(false)
15+
: m_window(make_window(create_info.extent, create_info.title)), m_engine(m_window, create_info.validation, create_info.msaa_samples),
16+
scene(m_engine.gfx()) {
17+
if (create_info.auto_show) { show(true); }
18+
}
519

6-
void Context::add_shader(Shader shader) { engine.add_shader(std::move(shader)); }
20+
void Context::add_shader(Shader shader) { m_engine.renderer().add_shader(std::move(shader)); }
721

822
void Context::show(bool reset_dt) {
9-
engine.show_window();
23+
glfwShowWindow(m_window.get());
1024
if (reset_dt) { m_dt = {}; }
1125
}
1226

27+
void Context::hide() { glfwHideWindow(m_window.get()); }
28+
29+
void Context::request_stop() { glfwSetWindowShouldClose(m_window.get(), GLFW_TRUE); }
30+
1331
float Context::next_frame() {
1432
if (m_ready_to_render) {
15-
if (m_cb) { scene.render(engine.renderer(), m_cb); }
16-
engine.submit();
33+
if (m_cb) { scene.render(m_engine.renderer(), m_cb); }
34+
m_engine.submit();
1735
}
18-
if (!engine.next_frame(m_cb)) { m_cb = vk::CommandBuffer{}; }
36+
m_window.get().glfw->poll_events();
37+
if (!m_engine.next_frame(m_cb)) { m_cb = vk::CommandBuffer{}; }
1938
m_ready_to_render = true;
2039
return m_dt();
2140
}

lib/editor/src/common.cpp renamed to lib/context/src/editor/common.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <imgui.h>
2-
#include <facade/editor/common.hpp>
2+
#include <facade/context/editor/common.hpp>
33
#include <cassert>
44

55
namespace facade::editor {
@@ -37,6 +37,14 @@ MainMenu::~MainMenu() {
3737
if (m_open) { ImGui::EndMainMenuBar(); }
3838
}
3939

40+
Popup::Popup(char const* id, int flags) : Openable(ImGui::BeginPopup(id, flags)) {}
41+
42+
Popup::~Popup() {
43+
if (m_open) { ImGui::EndPopup(); }
44+
}
45+
46+
void Popup::close_current() { ImGui::CloseCurrentPopup(); }
47+
4048
Menu::Menu(NotClosed<MenuBar>, char const* label, bool enabled) : Openable(ImGui::BeginMenu(label, enabled)) {}
4149

4250
Menu::~Menu() {

0 commit comments

Comments
 (0)