feat: ImGui 叠加层接入 RenderGraph(docking 分支,dynamic rendering)

框架与 UI 的职责边界:
- ImGuiLayer(Engine/ImGui):只做管线桥接——上下文初始化、
  NewFrame/BuildDrawData、draw data 录入 cmd;框架自身永不绘制
  任何 ImGui 控件,UI 内容全部由派生类在 OnRenderOverlay() 定义
- 每帧顺序:NewFrame → OnRenderOverlay(声明控件)→ BuildDrawData →
  Renderer.Render(RG: Forward → ImGui(loadOp=Load 叠加) → Present)
- ImGui pass 常驻 RG,未启用叠加层时空转,无图结构开销
- 后端共存:VK_EXT_shader_object 引擎无 vk::Pipeline,ImGui 用
  UseDynamicRendering + PipelineInfoMain.PipelineRenderingCreateInfo
  (swapchain 格式),不需要 RenderPass
- 析构序:ImGuiLayer 在 Application 成员序中后声明(先于 Renderer
  析构),析构内 waitIdle 保证后端资源安全销毁

App 示例(证明职责边界):Scene Debug 面板实时拖动阴影光方向,
` 键反折叠加层
This commit is contained in:
2026-09-22 17:09:55 +08:00
parent bc63f76fc1
commit 07792c34d7
10 changed files with 338 additions and 11 deletions
+40 -4
View File
@@ -16,6 +16,8 @@
#include <Engine/ECS/Components.h>
#include <Engine/Assets/AssetManager.h>
#include <imgui.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
@@ -39,6 +41,9 @@ protected:
{
Scene& scene = GetScene();
// ImGui 叠加层(框架只做管线桥接,UI 内容全部在这里定义)
EnableImGui();
// 模型资产(AssetManager 负责加载/缓存/生命周期,返回句柄)
auto backpack = GetAssets().LoadModel("assets/models/backpack/backpack.obj");
@@ -72,10 +77,10 @@ protected:
}
// 灯光实体(第一个灯 = 阴影光源)
scene.CreateEntity("KeyLight")
.AddComponent<LightComponent>(
LightComponent{glm::vec3(-0.5f, -0.6f, -0.35f),
glm::vec4(1.0f, 0.95f, 0.9f, 1.0f)});
m_KeyLight = scene.CreateEntity("KeyLight");
m_KeyLight.AddComponent<LightComponent>(
LightComponent{glm::vec3(-0.5f, -0.6f, -0.35f),
glm::vec4(1.0f, 0.95f, 0.9f, 1.0f)});
scene.CreateEntity("FillLight")
.AddComponent<LightComponent>(
@@ -103,8 +108,37 @@ protected:
// ---- 事件 ----
// ---- 叠加层 UI(唯一允许出现 ImGui 控件的地方)----
void OnRenderOverlay() override
{
if (!m_OverlayVisible) return;
ImGui::Begin("Scene Debug");
// 阴影光源方向:拖动滑条实时看影子变化
if (auto* light = m_KeyLight.TryGetComponent<LightComponent>()) {
ImGui::Text("Key Light (shadow caster)");
ImGui::SliderFloat3("direction",
&light->direction.x, -1.0f, 1.0f);
if (ImGui::Button("reset")) {
light->direction = glm::vec3(-0.5f, -0.6f, -0.35f);
}
}
ImGui::Separator();
ImGui::Text("fps: %.1f frame: %llu",
1.0f / GetDeltaTime(),
static_cast<unsigned long long>(GetFrameCount()));
ImGui::End();
}
void OnKeyPress(const Event& event) override
{
if (event.key == GLFW_KEY_GRAVE_ACCENT) {
m_OverlayVisible = !m_OverlayVisible; // ` 反折叠加层
}
if (event.key == GLFW_KEY_F1) {
STATS.PrintComparison();
}
@@ -121,7 +155,9 @@ protected:
private:
EntityHandle m_Camera{}; // Scene::CreateEntity 之前为空
EntityHandle m_Ground{};
EntityHandle m_KeyLight{};
std::vector<EntityHandle> m_Spinners;
bool m_OverlayVisible = true;
f32 m_Time = 0.0f;
};