添加windows构建适配,禁用ImGui多视口功能
This commit is contained in:
+314
-320
@@ -1,320 +1,314 @@
|
|||||||
//
|
//
|
||||||
// Created by atdunbg on 2026/9/20.
|
// Created by atdunbg on 2026/9/20.
|
||||||
//
|
//
|
||||||
// SandboxApp:第一个基于 Application 框架的程序。
|
// SandboxApp:第一个基于 Application 框架的程序。
|
||||||
//
|
//
|
||||||
// main 的职责边界:
|
// main 的职责边界:
|
||||||
// - 定义 Application 派生类并运行它
|
// - 定义 Application 派生类并运行它
|
||||||
// - "程序逻辑":场景搭建(实体/组件)、逐帧更新、事件响应、
|
// - "程序逻辑":场景搭建(实体/组件)、逐帧更新、事件响应、
|
||||||
// 异步环境加载
|
// 异步环境加载
|
||||||
// - UI 全部委托 App/editor/ 的 docking Editor(本文件不再出现
|
// - UI 全部委托 App/editor/ 的 docking Editor(本文件不再出现
|
||||||
// 任何 ImGui 控件)
|
// 任何 ImGui 控件)
|
||||||
//
|
//
|
||||||
// 窗口、渲染器、事件泵、帧计时、场景容器全部由框架管理。
|
// 窗口、渲染器、事件泵、帧计时、场景容器全部由框架管理。
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <Engine/Core/Application.h>
|
#include <Engine/Core/Application.h>
|
||||||
#include <Engine/Core/Time.h>
|
#include <Engine/Core/Time.h>
|
||||||
#include <Engine/Core/Window.h> // SetInputCapture(Application.h 只前置声明)
|
#include <Engine/Core/Window.h> // SetInputCapture(Application.h 只前置声明)
|
||||||
#include <Engine/Core/Log.h>
|
#include <Engine/Core/Log.h>
|
||||||
#include <Engine/Core/Stats.h>
|
#include <Engine/Core/Stats.h>
|
||||||
#include <Engine/Core/AsyncTask.h>
|
#include <Engine/Core/AsyncTask.h>
|
||||||
#include <Engine/ECS/Scene.h>
|
#include <Engine/ECS/Scene.h>
|
||||||
#include <Engine/ECS/EntityHandle.h>
|
#include <Engine/ECS/EntityHandle.h>
|
||||||
#include <Engine/ECS/Components.h>
|
#include <Engine/ECS/Components.h>
|
||||||
#include <Engine/Assets/AssetManager.h>
|
#include <Engine/Assets/AssetManager.h>
|
||||||
#include <Engine/Renderer/Renderer.h>
|
#include <Engine/Renderer/Renderer.h>
|
||||||
#include <Engine/Renderer/Model.h>
|
#include <Engine/Renderer/Model.h>
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include "Editor.h"
|
#include "Editor.h"
|
||||||
#include "EditorContext.h" // kEnvPaths / kDefaultEnv(与 StatsPanel 共用)
|
#include "EditorContext.h" // kEnvPaths / kDefaultEnv(与 StatsPanel 共用)
|
||||||
|
|
||||||
using namespace Engine;
|
using namespace Engine;
|
||||||
using namespace Engine::ECS;
|
using namespace Engine::ECS;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
constexpr const char* kTemplePath = "assets/models/SunTemple/SunTemple.fbx";
|
constexpr const char* kTemplePath = "assets/models/SunTemple/SunTemple.fbx";
|
||||||
|
|
||||||
class SandboxApp final : public Application
|
class SandboxApp final : public Application
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SandboxApp()
|
SandboxApp()
|
||||||
: Application("HelloVulkan", 800, 600)
|
: Application("HelloVulkan", 1920, 1080)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
~SandboxApp() override
|
~SandboxApp() override
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// ---- 生命周期 ----
|
// ---- 生命周期 ----
|
||||||
|
|
||||||
void OnInit() override
|
void OnInit() override
|
||||||
{
|
{
|
||||||
Scene& scene = GetScene();
|
Scene& scene = GetScene();
|
||||||
Renderer& renderer = GetRenderer();
|
Renderer& renderer = GetRenderer();
|
||||||
renderer.SetDisplayMode(kDisplayViewport);
|
renderer.SetDisplayMode(kDisplayViewport);
|
||||||
|
|
||||||
|
|
||||||
// ImGui 叠加层(框架只做管线桥接;UI 内容 = docking Editor)
|
// ImGui 叠加层(框架只做管线桥接;UI 内容 = docking Editor)
|
||||||
EnableImGui();
|
EnableImGui();
|
||||||
|
|
||||||
// Editor 注入:引擎门面 + 沙盒能力回调(环境异步切换等)
|
// Editor 注入:引擎门面 + 沙盒能力回调(环境异步切换等)
|
||||||
InitEditor();
|
InitEditor();
|
||||||
|
|
||||||
// 默认环境(IBL 四件套):经资产层加载(compute 预积分,毫秒级),
|
// 默认环境(IBL 四件套):经资产层加载(compute 预积分,毫秒级),
|
||||||
// 其余环境在 Stats 面板里按需 lazy 加载后切换
|
// 其余环境在 Stats 面板里按需 lazy 加载后切换
|
||||||
const auto env = GetAssets().LoadEnvironment(kEnvPaths[0]);
|
const auto env = GetAssets().LoadEnvironment(kEnvPaths[0]);
|
||||||
if (env.Valid()) {
|
if (env.Valid()) {
|
||||||
GetRenderer().SetEnvironment(GetAssets().GetEnvironment(env));
|
GetRenderer().SetEnvironment(GetAssets().GetEnvironment(env));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
const auto suntemple = GetAssets().LoadModel(kTemplePath);
|
const auto suntemple = GetAssets().LoadModel(kTemplePath);
|
||||||
auto entityhandle = scene.CreateEntity("SunTemple");
|
auto entityhandle = scene.CreateEntity("SunTemple");
|
||||||
entityhandle.AddComponent<MeshRendererComponent>(MeshRendererComponent{suntemple});
|
entityhandle.AddComponent<MeshRendererComponent>(MeshRendererComponent{suntemple});
|
||||||
*/
|
*/
|
||||||
|
|
||||||
scene.CreateEntity("test entity");
|
scene.CreateEntity("test entity");
|
||||||
scene.CreateEntity("test entity");
|
scene.CreateEntity("test entity");
|
||||||
scene.CreateEntity("test entity");
|
scene.CreateEntity("test entity");
|
||||||
|
|
||||||
const auto gun = GetAssets().LoadModel("assets/models/m1911/M1911.fbx");
|
const auto gun = GetAssets().LoadModel("assets/models/m1911/M1911.fbx");
|
||||||
if (gun.Valid()) {
|
if (gun.Valid()) {
|
||||||
m_Gun = scene.CreateEntity("M1911");
|
m_Gun = scene.CreateEntity("M1911");
|
||||||
m_Gun.AddComponent<MeshRendererComponent>(MeshRendererComponent{gun});
|
m_Gun.AddComponent<MeshRendererComponent>(MeshRendererComponent{gun});
|
||||||
m_Gun.AddComponent<AnimationComponent>();
|
m_Gun.AddComponent<AnimationComponent>();
|
||||||
auto& gt = m_Gun.GetComponent<TransformComponent>();
|
auto& gt = m_Gun.GetComponent<TransformComponent>();
|
||||||
gt.SetPosition(glm::vec3(0.0f, 1.2f, -6.0f));
|
gt.SetPosition(glm::vec3(0.0f, 1.2f, -6.0f));
|
||||||
// 物理验证:Dynamic 刚体(默认 Box halfExtents 0.5),
|
// 物理验证:Dynamic 刚体(默认 Box halfExtents 0.5),
|
||||||
// 出生位置悬空 → 应下落至地面静止(Transform 由物理写回)
|
// 出生位置悬空 → 应下落至地面静止(Transform 由物理写回)
|
||||||
m_Gun.AddComponent<RigidBodyComponent>();
|
m_Gun.AddComponent<RigidBodyComponent>();
|
||||||
} else {
|
} else {
|
||||||
ENGINE_WARN("m1911 model not loaded, animation test skipped");
|
ENGINE_WARN("m1911 model not loaded, animation test skipped");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 地面:程序化纯色平面(半边长 15,中心 y=-0.5)+ 静态盒刚体
|
// 地面:程序化纯色平面(半边长 15,中心 y=-0.5)+ 静态盒刚体
|
||||||
const auto ground = GetAssets().CreatePlane(15.0f, 140, 140, 145);
|
const auto ground = GetAssets().CreatePlane(15.0f, 140, 140, 145);
|
||||||
m_Ground = scene.CreateEntity("Ground");
|
m_Ground = scene.CreateEntity("Ground");
|
||||||
m_Ground.GetComponent<TransformComponent>()
|
m_Ground.GetComponent<TransformComponent>()
|
||||||
.SetPosition(glm::vec3(0.0f, -0.5f, 0.0f)); // dirty → 帧循环统一 Compose
|
.SetPosition(glm::vec3(0.0f, -0.5f, 0.0f)); // dirty → 帧循环统一 Compose
|
||||||
m_Ground.AddComponent<MeshRendererComponent>(MeshRendererComponent{ground});
|
m_Ground.AddComponent<MeshRendererComponent>(MeshRendererComponent{ground});
|
||||||
auto& groundRb = m_Ground.AddComponent<RigidBodyComponent>();
|
auto& groundRb = m_Ground.AddComponent<RigidBodyComponent>();
|
||||||
groundRb.type = RigidBodyComponent::BodyType::Static;
|
groundRb.type = RigidBodyComponent::BodyType::Static;
|
||||||
groundRb.boxHalfExtents = glm::vec3(15.0f, 0.1f, 15.0f); // 平面顶点烘焙 ±15,不能用默认 0.5
|
groundRb.boxHalfExtents = glm::vec3(15.0f, 0.1f, 15.0f); // 平面顶点烘焙 ±15,不能用默认 0.5
|
||||||
|
|
||||||
/*
|
/*
|
||||||
const auto gun = GetAssets().LoadModel("assets/models/m1911/M1911.fbx");
|
const auto gun = GetAssets().LoadModel("assets/models/m1911/M1911.fbx");
|
||||||
if (gun.Valid()) {
|
if (gun.Valid()) {
|
||||||
m_Gun = scene.CreateEntity("M1911");
|
m_Gun = scene.CreateEntity("M1911");
|
||||||
m_Gun.AddComponent<MeshRendererComponent>(MeshRendererComponent{gun});
|
m_Gun.AddComponent<MeshRendererComponent>(MeshRendererComponent{gun});
|
||||||
m_Gun.AddComponent<AnimationComponent>();
|
m_Gun.AddComponent<AnimationComponent>();
|
||||||
auto& gt = m_Gun.GetComponent<TransformComponent>();
|
auto& gt = m_Gun.GetComponent<TransformComponent>();
|
||||||
gt.SetPosition(glm::vec3(0.0f, 1.2f, -6.0f));
|
gt.SetPosition(glm::vec3(0.0f, 1.2f, -6.0f));
|
||||||
} else {
|
} else {
|
||||||
ENGINE_WARN("m1911 model not loaded, animation test skipped");
|
ENGINE_WARN("m1911 model not loaded, animation test skipped");
|
||||||
}
|
}
|
||||||
|
|
||||||
int countSize = 0;
|
int countSize = 0;
|
||||||
if (countSize) {
|
if (countSize) {
|
||||||
// 模型资产(PBR 验证场景:backpack 为旧式贴图资产,
|
// 模型资产(PBR 验证场景:backpack 为旧式贴图资产,
|
||||||
// 退化为电介质因子路径——albedo+normal,metallic=0/roughness=0.5)
|
// 退化为电介质因子路径——albedo+normal,metallic=0/roughness=0.5)
|
||||||
const auto model = GetAssets().LoadModel("assets/models/backpack/backpack.obj");
|
const auto model = GetAssets().LoadModel("assets/models/backpack/backpack.obj");
|
||||||
// const auto model = GetAssets().LoadModel("assets/models/pocket_watch_4k.gltf/pocket_watch_4k.gltf");
|
// const auto model = GetAssets().LoadModel("assets/models/pocket_watch_4k.gltf/pocket_watch_4k.gltf");
|
||||||
|
|
||||||
for (int i = 0; i < countSize; i ++)
|
for (int i = 0; i < countSize; i ++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < countSize; j ++)
|
for (int j = 0; j < countSize; j ++)
|
||||||
{
|
{
|
||||||
auto m = scene.CreateEntity();
|
auto m = scene.CreateEntity();
|
||||||
m.AddComponent<MeshRendererComponent>(MeshRendererComponent{model});
|
m.AddComponent<MeshRendererComponent>(MeshRendererComponent{model});
|
||||||
m.GetComponent<TransformComponent>().SetPosition(glm::vec3{(i - countSize * 0.5) * 2, 0, (j - countSize * 0.5) * 2});
|
m.GetComponent<TransformComponent>().SetPosition(glm::vec3{(i - countSize * 0.5) * 2, 0, (j - countSize * 0.5) * 2});
|
||||||
m_Spinners.push_back(m);
|
m_Spinners.push_back(m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 地面:程序化纯色平面(半边长 15,中心 y=-2)
|
// 地面:程序化纯色平面(半边长 15,中心 y=-2)
|
||||||
const auto ground = GetAssets().CreatePlane(15.0f, 140, 140, 145);
|
const auto ground = GetAssets().CreatePlane(15.0f, 140, 140, 145);
|
||||||
m_Ground = scene.CreateEntity("Ground");
|
m_Ground = scene.CreateEntity("Ground");
|
||||||
m_Ground.GetComponent<TransformComponent>()
|
m_Ground.GetComponent<TransformComponent>()
|
||||||
.SetPosition(glm::vec3(0.0f, -0.5f, 0.0f)); // dirty → 帧循环统一 Compose
|
.SetPosition(glm::vec3(0.0f, -0.5f, 0.0f)); // dirty → 帧循环统一 Compose
|
||||||
m_Ground.AddComponent<MeshRendererComponent>(MeshRendererComponent{ground});
|
m_Ground.AddComponent<MeshRendererComponent>(MeshRendererComponent{ground});
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// 相机实体(primary = 当前渲染视角;位姿存实体 Transform)
|
// 相机实体(primary = 当前渲染视角;位姿存实体 Transform)
|
||||||
m_Camera = scene.CreateEntity("MainCamera");
|
m_Camera = scene.CreateEntity("MainCamera");
|
||||||
auto& camera = m_Camera.AddComponent<CameraComponent>();
|
auto& camera = m_Camera.AddComponent<CameraComponent>();
|
||||||
camera.primary = true;
|
camera.primary = true;
|
||||||
m_Camera.GetComponent<TransformComponent>()
|
m_Camera.GetComponent<TransformComponent>()
|
||||||
.SetPosition(glm::vec3(0.0f, 12.0f, 22.0f));
|
.SetPosition(glm::vec3(0.0f, 12.0f, 22.0f));
|
||||||
m_Camera.GetComponent<TransformComponent>()
|
m_Camera.GetComponent<TransformComponent>()
|
||||||
.SetRotation(glm::vec3(-25.0f, 0.0f, 0.0f));
|
.SetRotation(glm::vec3(-25.0f, 0.0f, 0.0f));
|
||||||
|
|
||||||
// 灯光实体(第一个方向光 = 阴影光源)
|
// 灯光实体(第一个方向光 = 阴影光源)
|
||||||
m_KeyLight = scene.CreateEntity("KeyLight");
|
m_KeyLight = scene.CreateEntity("KeyLight");
|
||||||
m_KeyLight.AddComponent<LightComponent>(
|
m_KeyLight.AddComponent<LightComponent>(
|
||||||
LightComponent{
|
LightComponent{
|
||||||
.type = LightComponent::Type::Directional,
|
.type = LightComponent::Type::Directional,
|
||||||
.direction = glm::vec3(-0.5f, -0.6f, -0.35f),
|
.direction = glm::vec3(-0.5f, -0.6f, -0.35f),
|
||||||
.color = glm::vec4(1.0f, 0.95f, 0.9f, 5.0f),
|
.color = glm::vec4(1.0f, 0.95f, 0.9f, 5.0f),
|
||||||
.castShadow = true,
|
.castShadow = true,
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
scene.CreateEntity("FillLight")
|
scene.CreateEntity("FillLight")
|
||||||
.AddComponent<LightComponent>(
|
.AddComponent<LightComponent>(
|
||||||
LightComponent{
|
LightComponent{
|
||||||
.type = LightComponent::Type::Directional,
|
.type = LightComponent::Type::Directional,
|
||||||
.direction = glm::vec3(0.5f, -0.5f, 0.3f),
|
.direction = glm::vec3(0.5f, -0.5f, 0.3f),
|
||||||
.color = glm::vec4(0.3f, 0.5f, 1.0f, 1.5f)
|
.color = glm::vec4(0.3f, 0.5f, 1.0f, 1.5f)
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// 点光源(位置 = 实体 Transform,出生即有;无阴影,距离衰减)
|
// 点光源(位置 = 实体 Transform,出生即有;无阴影,距离衰减)
|
||||||
auto fire = scene.CreateEntity("FirePitLight");
|
auto fire = scene.CreateEntity("FirePitLight");
|
||||||
fire.GetComponent<TransformComponent>().SetPosition(glm::vec3(2.0f, 1.5f, 1.0f));
|
fire.GetComponent<TransformComponent>().SetPosition(glm::vec3(2.0f, 1.5f, 1.0f));
|
||||||
fire.AddComponent<LightComponent>(
|
fire.AddComponent<LightComponent>(
|
||||||
LightComponent{
|
LightComponent{
|
||||||
.type = LightComponent::Type::Point,
|
.type = LightComponent::Type::Point,
|
||||||
.color = glm::vec4(1.0f, 0.6f, 0.3f, 1.0f), // 暖橙
|
.color = glm::vec4(1.0f, 0.6f, 0.3f, 1.0f), // 暖橙
|
||||||
.radius = 8.0f,
|
.radius = 8.0f,
|
||||||
});
|
});
|
||||||
|
|
||||||
auto cool = scene.CreateEntity("CoolFillLight");
|
auto cool = scene.CreateEntity("CoolFillLight");
|
||||||
cool.GetComponent<TransformComponent>().SetPosition(glm::vec3(-2.5f, 2.0f, -2.0f));
|
cool.GetComponent<TransformComponent>().SetPosition(glm::vec3(-2.5f, 2.0f, -2.0f));
|
||||||
cool.AddComponent<LightComponent>(
|
cool.AddComponent<LightComponent>(
|
||||||
LightComponent{
|
LightComponent{
|
||||||
.type = LightComponent::Type::Point,
|
.type = LightComponent::Type::Point,
|
||||||
.color = glm::vec4(0.3f, 0.5f, 1.0f, 1.2f), // 冷蓝
|
.color = glm::vec4(0.3f, 0.5f, 1.0f, 1.2f), // 冷蓝
|
||||||
.radius = 10.0f,
|
.radius = 10.0f,
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnUpdate(const FrameTime& frameTime) override
|
void OnUpdate(const FrameTime& frameTime) override
|
||||||
{
|
{
|
||||||
// 相机(WASD/Space/Shift 移动;位姿读写实体 Transform)。
|
// 相机(WASD/Space/Shift 移动;位姿读写实体 Transform)。
|
||||||
// 编辑器相机走真实轨:暂停(timeScale=0)时仍可飞行观察
|
// 编辑器相机走真实轨:暂停(timeScale=0)时仍可飞行观察
|
||||||
m_Camera.GetComponent<CameraComponent>()
|
m_Camera.GetComponent<CameraComponent>()
|
||||||
.camera.Update(GetWindow(),
|
.camera.Update(GetWindow(),
|
||||||
m_Camera.GetComponent<TransformComponent>(),
|
m_Camera.GetComponent<TransformComponent>(),
|
||||||
frameTime.UnscaledDt());
|
frameTime.UnscaledDt());
|
||||||
|
|
||||||
// 旋转动画:编辑 Transform 分量后 Compose 重算 world。
|
// 旋转动画:编辑 Transform 分量后 Compose 重算 world。
|
||||||
// 动画时钟 = Time::Elapsed()(scaled 累计,暂停自动冻结——
|
// 动画时钟 = Time::Elapsed()(scaled 累计,暂停自动冻结——
|
||||||
// 帧历史验证:暂停后 Temporal Diff 视图 2 帧内整屏变黑)
|
// 帧历史验证:暂停后 Temporal Diff 视图 2 帧内整屏变黑)
|
||||||
for (size_t i = 0; i < m_Spinners.size(); ++i)
|
for (size_t i = 0; i < m_Spinners.size(); ++i)
|
||||||
{
|
{
|
||||||
auto& t = m_Spinners[i].GetComponent<TransformComponent>();
|
auto& t = m_Spinners[i].GetComponent<TransformComponent>();
|
||||||
t.SetRotation(glm::vec3(
|
t.SetRotation(glm::vec3(
|
||||||
0.0f, static_cast<f32>(Time::Elapsed()) * 60.0f +
|
0.0f, static_cast<f32>(Time::Elapsed()) * 60.0f +
|
||||||
static_cast<f32>(i) * 40.0f, 0.0f));
|
static_cast<f32>(i) * 40.0f, 0.0f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- UI(全部委托 docking Editor;本文件零 ImGui 控件)----
|
// ---- UI(全部委托 docking Editor;本文件零 ImGui 控件)----
|
||||||
|
|
||||||
void OnImGuiRender() override
|
void OnImGuiRender() override
|
||||||
{
|
{
|
||||||
m_Editor->Draw();
|
m_Editor->Draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- 事件 ----
|
// ---- 事件 ----
|
||||||
|
|
||||||
void OnWindowClose(const Event& event) override
|
void OnWindowClose(const Event& event) override
|
||||||
{
|
{
|
||||||
// 退出确认走 Editor 的模态框(ImGui 必须可见才有 UI)
|
// 退出确认走 Editor 的模态框(ImGui 必须可见才有 UI)
|
||||||
SetImGuiEnabled(true);
|
SetImGuiEnabled(true);
|
||||||
m_Editor->RequestQuit();
|
m_Editor->RequestQuit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnKeyPress(const Event& event) override
|
void OnKeyPress(const Event& event) override
|
||||||
{
|
{
|
||||||
if (event.key == GLFW_KEY_GRAVE_ACCENT)
|
if (event.key == GLFW_KEY_GRAVE_ACCENT)
|
||||||
{
|
{
|
||||||
SetImGuiEnabled(!GetImGuiEnabled());
|
SetImGuiEnabled(!GetImGuiEnabled());
|
||||||
}
|
}
|
||||||
if (event.key == GLFW_KEY_F1)
|
if (event.key == GLFW_KEY_F1)
|
||||||
{
|
{
|
||||||
STATS.PrintComparison();
|
STATS.PrintComparison();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void OnWindowResize(const Event& event) override
|
void OnWindowResize(const Event& event) override
|
||||||
{
|
{
|
||||||
ENGINE_INFO("App: window resized to {}x{}", event.width, event.height);
|
ENGINE_INFO("App: window resized to {}x{}", event.width, event.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Editor 注入:门面指针 + 反向能力回调(面板 → 沙盒)
|
// Editor 注入:门面指针 + 反向能力回调(面板 → 沙盒)
|
||||||
void InitEditor()
|
void InitEditor()
|
||||||
{
|
{
|
||||||
EditorContext ctx;
|
EditorContext ctx;
|
||||||
ctx.renderer = &GetRenderer();
|
ctx.renderer = &GetRenderer();
|
||||||
ctx.scene = &GetScene();
|
ctx.scene = &GetScene();
|
||||||
ctx.assets = &GetAssets();
|
ctx.assets = &GetAssets();
|
||||||
ctx.app = this;
|
ctx.app = this;
|
||||||
ctx.getEnvIndex = [this] { return m_EnvIndex; };
|
ctx.getEnvIndex = [this] { return m_EnvIndex; };
|
||||||
ctx.setEnvIndex = [this](int index) { m_EnvIndex = index; };
|
ctx.setEnvIndex = [this](int index) { m_EnvIndex = index; };
|
||||||
// 继承原沙盒默认:初始暂停(StatsPanel 的 Pause Simulation 开关恢复)
|
// 继承原沙盒默认:初始暂停(StatsPanel 的 Pause Simulation 开关恢复)
|
||||||
Time::SetTimeScale(0.0f);
|
Time::SetTimeScale(0.0f);
|
||||||
m_Editor->Init(ctx);
|
m_Editor->Init(ctx);
|
||||||
|
|
||||||
// 输入归属:覆盖 EnableImGui 的全局 WantCapture* 谓词
|
// 输入归属:覆盖 EnableImGui 的全局 WantCapture* 谓词
|
||||||
//(全屏 dock host 让它恒真,输入全死)。改为 Editor 按帧
|
//(全屏 dock host 让它恒真,输入全死)。改为 Editor 按帧
|
||||||
// 判定——Viewport hover/focus 拥有输入(Hazel/UE 语义);
|
// 判定——Viewport hover/focus 拥有输入(Hazel/UE 语义);
|
||||||
// UI 隐藏(反引号)时 Draw 不再执行,直接全放行
|
// UI 隐藏(反引号)时 Draw 不再执行,直接全放行
|
||||||
GetWindow().SetInputCapture(
|
GetWindow().SetInputCapture(
|
||||||
[this] { return GetImGuiEnabled() && m_Editor->BlockingKeyboard(); },
|
[this] { return GetImGuiEnabled() && m_Editor->BlockingKeyboard(); },
|
||||||
[this] { return GetImGuiEnabled() && m_Editor->BlockingMouse(); });
|
[this] { return GetImGuiEnabled() && m_Editor->BlockingMouse(); });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Editor> m_Editor = std::make_unique<Editor>();
|
std::unique_ptr<Editor> m_Editor = std::make_unique<Editor>();
|
||||||
|
|
||||||
EntityHandle m_Camera{}; // Scene::CreateEntity 之前为空
|
EntityHandle m_Camera{}; // Scene::CreateEntity 之前为空
|
||||||
EntityHandle m_KeyLight{};
|
EntityHandle m_KeyLight{};
|
||||||
EntityHandle m_Ground{};
|
EntityHandle m_Ground{};
|
||||||
EntityHandle m_Gun{}; // 骨骼动画验证实体(M1911)
|
EntityHandle m_Gun{}; // 骨骼动画验证实体(M1911)
|
||||||
std::vector<EntityHandle> m_Spinners;
|
std::vector<EntityHandle> m_Spinners;
|
||||||
int m_EnvIndex = kDefaultEnv;
|
int m_EnvIndex = kDefaultEnv;
|
||||||
|
};
|
||||||
// ---- 异步模型加载(SunTemple)----
|
}
|
||||||
AsyncTask<ParsedModel> m_TempleTask;
|
|
||||||
f32 m_TempleScale = 1.0f; // cm 尺度资产 → 米(解析后应用)
|
int main()
|
||||||
};
|
{
|
||||||
}
|
Logger::Initialize();
|
||||||
|
|
||||||
int main()
|
{
|
||||||
{
|
SandboxApp app;
|
||||||
Logger::Initialize();
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
SandboxApp app;
|
app.Run();
|
||||||
|
}
|
||||||
try
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
app.Run();
|
ENGINE_FATAL("Exception: {}", e.what());
|
||||||
}
|
Logger::Shutdown();
|
||||||
catch (const std::exception& e)
|
return -1;
|
||||||
{
|
}
|
||||||
ENGINE_FATAL("Exception: {}", e.what());
|
} // app 析构:成员(Device/Renderer/Window…)全部销毁完毕
|
||||||
Logger::Shutdown();
|
|
||||||
return -1;
|
Logger::Shutdown();
|
||||||
}
|
return 0;
|
||||||
} // app 析构:成员(Device/Renderer/Window…)全部销毁完毕
|
}
|
||||||
|
|
||||||
// 日志必须最后关:Device 析构期间 Vulkan validation 回调仍在
|
|
||||||
// 打日志(销毁资源时报 VUID),logger 先亡 = 悬空调用 SIGSEGV
|
|
||||||
Logger::Shutdown();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|||||||
+77
-69
@@ -1,69 +1,77 @@
|
|||||||
cmake_minimum_required(VERSION 3.20)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
project(HelloVulkan CXX)
|
project(HelloVulkan CXX)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
set(CMAKE_BINARY_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
|
set(CMAKE_BINARY_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
|
||||||
|
|
||||||
# 第三方库
|
# 第三方库
|
||||||
add_subdirectory(Lib/glfw)
|
add_subdirectory(Lib/glfw)
|
||||||
add_subdirectory(Lib/glm)
|
add_subdirectory(Lib/glm)
|
||||||
add_subdirectory(Lib/spdlog)
|
add_subdirectory(Lib/spdlog)
|
||||||
add_subdirectory(Lib/stb)
|
add_subdirectory(Lib/stb)
|
||||||
# meshoptimizer(submodule,LOD 网格简化;demo/gltfpack 默认关闭,install 不需要)
|
# meshoptimizer(submodule,LOD 网格简化;demo/gltfpack 默认关闭,install 不需要)
|
||||||
set(MESHOPT_INSTALL OFF CACHE BOOL "" FORCE)
|
set(MESHOPT_INSTALL OFF CACHE BOOL "" FORCE)
|
||||||
add_subdirectory(Lib/meshoptimizer)
|
add_subdirectory(Lib/meshoptimizer)
|
||||||
|
|
||||||
# ---- Jolt Physics(submodule v5.6,刚体/软体/角色/载具模拟)----
|
# ---- Jolt Physics(submodule v5.6,刚体/软体/角色/载具模拟)----
|
||||||
# 顶层 CMake 在 Build/ 子目录;只编 jolt 静态库,示例/测试目标默认全关
|
if(MSVC)
|
||||||
set(JPH_BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
|
set(USE_STATIC_MSVC_RUNTIME_LIBRARY OFF CACHE BOOL "" FORCE)
|
||||||
set(INTERPROCEDURAL_OPTIMIZATION OFF CACHE BOOL "" FORCE) # LTO 拖慢开发迭代
|
endif ()
|
||||||
set(OVERRIDE_CXX_FLAGS OFF CACHE BOOL "" FORCE) # 不动全局优化参数
|
# 顶层 CMake 在 Build/ 子目录;只编 jolt 静态库,示例/测试目标默认全关
|
||||||
set(FLOATING_POINT_EXCEPTIONS_ENABLED OFF CACHE BOOL "" FORCE) # NaN 进物理不炸,仅报警
|
set(JPH_BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
|
||||||
# JPH_USE_CPU_COMPUTE 默认 ON,需要 DXC 把 Shaders/*.hlsl 编成 .spv(本机无
|
set(INTERPROCEDURAL_OPTIMIZATION OFF CACHE BOOL "" FORCE) # LTO 拖慢开发迭代
|
||||||
# 工具链);我们不使用 Jolt 的 GPU compute 路径,连同后端 shader 一并关掉
|
set(OVERRIDE_CXX_FLAGS OFF CACHE BOOL "" FORCE) # 不动全局优化参数
|
||||||
set(JPH_USE_CPU_COMPUTE OFF CACHE BOOL "" FORCE)
|
set(FLOATING_POINT_EXCEPTIONS_ENABLED OFF CACHE BOOL "" FORCE) # NaN 进物理不炸,仅报警
|
||||||
set(JPH_USE_DX12 OFF CACHE BOOL "" FORCE)
|
# JPH_USE_CPU_COMPUTE 默认 ON,需要 DXC 把 Shaders/*.hlsl 编成 .spv(本机无
|
||||||
set(JPH_USE_VK OFF CACHE BOOL "" FORCE)
|
# 工具链);我们不使用 Jolt 的 GPU compute 路径,连同后端 shader 一并关掉
|
||||||
set(JPH_USE_MTL OFF CACHE BOOL "" FORCE)
|
set(JPH_USE_CPU_COMPUTE OFF CACHE BOOL "" FORCE)
|
||||||
add_subdirectory(Lib/JoltPhysics/Build)
|
set(JPH_USE_DX12 OFF CACHE BOOL "" FORCE)
|
||||||
|
set(JPH_USE_VK OFF CACHE BOOL "" FORCE)
|
||||||
# ---- Assimp(vendored submodule,模型导入;只编译需要的 importer) ----
|
set(JPH_USE_MTL OFF CACHE BOOL "" FORCE)
|
||||||
set(BUILD_SHARED_LIBS OFF) # 静态链接
|
add_subdirectory(Lib/JoltPhysics/Build)
|
||||||
set(ASSIMP_BUILD_TESTS OFF CACHE BOOL "" FORCE)
|
|
||||||
set(ASSIMP_BUILD_ASSIMP_TOOLS OFF CACHE BOOL "" FORCE) # 不编 assimp_cmd 等工具
|
# ---- Assimp(vendored submodule,模型导入;只编译需要的 importer) ----
|
||||||
set(ASSIMP_INSTALL OFF CACHE BOOL "" FORCE)
|
set(BUILD_SHARED_LIBS OFF) # 静态链接
|
||||||
set(ASSIMP_WARNINGS_AS_ERRORS OFF CACHE BOOL "" FORCE) # assimp 源码警告多
|
set(ASSIMP_BUILD_TESTS OFF CACHE BOOL "" FORCE)
|
||||||
set(ASSIMP_INJECT_DEBUG_POSTFIX OFF CACHE BOOL "" FORCE)
|
set(ASSIMP_BUILD_ASSIMP_TOOLS OFF CACHE BOOL "" FORCE) # 不编 assimp_cmd 等工具
|
||||||
set(ASSIMP_NO_EXPORT ON CACHE BOOL "" FORCE) # 彻底移除导出代码
|
set(ASSIMP_INSTALL OFF CACHE BOOL "" FORCE)
|
||||||
set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT OFF CACHE BOOL "" FORCE) # 全关,按需开
|
set(ASSIMP_WARNINGS_AS_ERRORS OFF CACHE BOOL "" FORCE) # assimp 源码警告多
|
||||||
set(ASSIMP_BUILD_DRACO OFF CACHE BOOL "" FORCE)
|
set(ASSIMP_INJECT_DEBUG_POSTFIX OFF CACHE BOOL "" FORCE)
|
||||||
# 用 assimp 自带 contrib zlib(而非系统 zlib)。原因:系统 zlib 会使 assimp 走
|
set(ASSIMP_NO_EXPORT ON CACHE BOOL "" FORCE) # 彻底移除导出代码
|
||||||
# pkg-config 找系统 minizip,而 Arch 的 minizip 头文件在 /usr/include/minizip/
|
set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT OFF CACHE BOOL "" FORCE) # 全关,按需开
|
||||||
# 子目录,与 assimp 的 #include <unzip.h> 不符 → ZipArchiveIOSystem 编译失败。
|
set(ASSIMP_BUILD_DRACO OFF CACHE BOOL "" FORCE)
|
||||||
# 自带 zlib 后 ASSIMP_BUILD_MINIZIP=1,跳过 pkg-config,直接用 contrib/unzip。
|
# 用 assimp 自带 contrib zlib(而非系统 zlib)。原因:系统 zlib 会使 assimp 走
|
||||||
set(ASSIMP_BUILD_ZLIB ON CACHE BOOL "" FORCE)
|
# pkg-config 找系统 minizip,而 Arch 的 minizip 头文件在 /usr/include/minizip/
|
||||||
# 清除首次 configure 残留的 UNZIP_FOUND 缓存(INTERNAL=1 会持续生效,
|
# 子目录,与 assimp 的 #include <unzip.h> 不符 → ZipArchiveIOSystem 编译失败。
|
||||||
# 使 assimp 误判系统 minizip 可用而不把 contrib/unzip 加进 include 路径)。
|
# 自带 zlib 后 ASSIMP_BUILD_MINIZIP=1,跳过 pkg-config,直接用 contrib/unzip。
|
||||||
set(UNZIP_FOUND OFF CACHE INTERNAL "" FORCE)
|
set(ASSIMP_BUILD_ZLIB ON CACHE BOOL "" FORCE)
|
||||||
# 需要的导入格式(按需增减,每关一个省编译时间):
|
# 清除首次 configure 残留的 UNZIP_FOUND 缓存(INTERNAL=1 会持续生效,
|
||||||
set(ASSIMP_BUILD_OBJ_IMPORTER ON CACHE BOOL "" FORCE)
|
# 使 assimp 误判系统 minizip 可用而不把 contrib/unzip 加进 include 路径)。
|
||||||
set(ASSIMP_BUILD_GLTF_IMPORTER ON CACHE BOOL "" FORCE) # glTF1+2 同一选项
|
set(UNZIP_FOUND OFF CACHE INTERNAL "" FORCE)
|
||||||
set(ASSIMP_BUILD_FBX_IMPORTER ON CACHE BOOL "" FORCE)
|
# 需要的导入格式(按需增减,每关一个省编译时间):
|
||||||
set(ASSIMP_BUILD_STL_IMPORTER ON CACHE BOOL "" FORCE)
|
set(ASSIMP_BUILD_OBJ_IMPORTER ON CACHE BOOL "" FORCE)
|
||||||
set(ASSIMP_BUILD_PLY_IMPORTER ON CACHE BOOL "" FORCE)
|
set(ASSIMP_BUILD_GLTF_IMPORTER ON CACHE BOOL "" FORCE) # glTF1+2 同一选项
|
||||||
add_subdirectory(Lib/assimp)
|
set(ASSIMP_BUILD_FBX_IMPORTER ON CACHE BOOL "" FORCE)
|
||||||
|
set(ASSIMP_BUILD_STL_IMPORTER ON CACHE BOOL "" FORCE)
|
||||||
|
set(ASSIMP_BUILD_PLY_IMPORTER ON CACHE BOOL "" FORCE)
|
||||||
|
add_subdirectory(Lib/assimp)
|
||||||
# Vulkan
|
|
||||||
find_package(Vulkan REQUIRED)
|
|
||||||
|
|
||||||
# Engine 和 App
|
# Vulkan
|
||||||
add_subdirectory(Engine)
|
find_package(Vulkan REQUIRED)
|
||||||
add_subdirectory(App)
|
|
||||||
|
|
||||||
file(GLOB ASSETS "assets")
|
if(MINGW)
|
||||||
file(COPY ${ASSETS} DESTINATION ${CMAKE_BINARY_DIR})
|
set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -Wl,-Bdynamic -lucrt")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Engine 和 App
|
||||||
|
add_subdirectory(Engine)
|
||||||
|
add_subdirectory(App)
|
||||||
|
|
||||||
|
file(GLOB ASSETS "assets")
|
||||||
|
file(COPY ${ASSETS} DESTINATION ${CMAKE_BINARY_DIR})
|
||||||
|
|||||||
+69
-60
@@ -1,60 +1,69 @@
|
|||||||
# ============ 第三方库目标(Engine 的依赖) ============
|
# ============ 第三方库目标(Engine 的依赖) ============
|
||||||
|
|
||||||
# ImGui(docking 分支,vendored submodule)
|
# ImGui(docking 分支,vendored submodule)
|
||||||
set(IMGUI_DIR ${CMAKE_SOURCE_DIR}/Lib/imgui)
|
set(IMGUI_DIR ${CMAKE_SOURCE_DIR}/Lib/imgui)
|
||||||
add_library(imgui STATIC
|
add_library(imgui STATIC
|
||||||
${IMGUI_DIR}/imgui.cpp
|
${IMGUI_DIR}/imgui.cpp
|
||||||
${IMGUI_DIR}/imgui_draw.cpp
|
${IMGUI_DIR}/imgui_draw.cpp
|
||||||
${IMGUI_DIR}/imgui_tables.cpp
|
${IMGUI_DIR}/imgui_tables.cpp
|
||||||
${IMGUI_DIR}/imgui_widgets.cpp
|
${IMGUI_DIR}/imgui_widgets.cpp
|
||||||
# ${IMGUI_DIR}/imgui_demo.cpp # ShowDemoWindow 的实现(调试期保留)
|
# ${IMGUI_DIR}/imgui_demo.cpp # ShowDemoWindow 的实现(调试期保留)
|
||||||
${IMGUI_DIR}/backends/imgui_impl_glfw.cpp
|
${IMGUI_DIR}/backends/imgui_impl_glfw.cpp
|
||||||
${IMGUI_DIR}/backends/imgui_impl_vulkan.cpp
|
${IMGUI_DIR}/backends/imgui_impl_vulkan.cpp
|
||||||
)
|
)
|
||||||
target_include_directories(imgui PUBLIC
|
target_include_directories(imgui PUBLIC
|
||||||
${IMGUI_DIR}
|
${IMGUI_DIR}
|
||||||
${IMGUI_DIR}/backends)
|
${IMGUI_DIR}/backends)
|
||||||
# 两个 backend 头文件分别依赖 GLFW / Vulkan 头
|
# 两个 backend 头文件分别依赖 GLFW / Vulkan 头
|
||||||
target_link_libraries(imgui PUBLIC glfw Vulkan::Vulkan)
|
target_link_libraries(imgui PUBLIC glfw Vulkan::Vulkan)
|
||||||
|
|
||||||
# Tracy(CPU/GPU 帧分析)
|
# Tracy(CPU/GPU 帧分析)
|
||||||
add_library(tracy STATIC ${CMAKE_SOURCE_DIR}/Lib/tracy/public/TracyClient.cpp)
|
add_library(tracy STATIC ${CMAKE_SOURCE_DIR}/Lib/tracy/public/TracyClient.cpp)
|
||||||
target_include_directories(tracy PUBLIC ${CMAKE_SOURCE_DIR}/Lib/tracy/public)
|
target_include_directories(tracy PUBLIC ${CMAKE_SOURCE_DIR}/Lib/tracy/public)
|
||||||
target_compile_definitions(tracy PUBLIC
|
target_compile_definitions(tracy PUBLIC
|
||||||
TRACY_ENABLE
|
TRACY_ENABLE
|
||||||
# 按需剖析:GPU context 宣告(GpuNewContext)与连接绑定——GUI 永远晚于
|
# 按需剖析:GPU context 宣告(GpuNewContext)与连接绑定——GUI 永远晚于
|
||||||
# 程序启动连接,没有这个宏宣告消息被丢弃,GPU 时间线永远空白
|
# 程序启动连接,没有这个宏宣告消息被丢弃,GPU 时间线永远空白
|
||||||
TRACY_ON_DEMAND)
|
TRACY_ON_DEMAND)
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
target_link_libraries(tracy PUBLIC Threads::Threads)
|
if(WIN32)
|
||||||
|
target_link_libraries(tracy PUBLIC
|
||||||
# ============ Engine ============
|
Threads::Threads
|
||||||
|
ws2_32
|
||||||
file(GLOB_RECURSE ENGINE_SOURCES CONFIGURE_DEPENDS src/*.cpp)
|
dbghelp
|
||||||
add_library(Engine STATIC ${ENGINE_SOURCES})
|
secur32)
|
||||||
|
else()
|
||||||
# PhysicsDebugDraw 派生 Jolt::DebugRendererSimple:Jolt 以 -fno-rtti 编译
|
target_link_libraries(tracy PUBLIC Threads::Threads)
|
||||||
#(有 key function 的基类 typeinfo 符号不存在),该 TU 必须同步关闭 RTTI,
|
endif()
|
||||||
# 否则派生类 typeinfo 引用悬空符号(链接期 undefined reference)
|
|
||||||
set_source_files_properties(src/Physics/PhysicsDebugDraw.cpp PROPERTIES
|
|
||||||
COMPILE_OPTIONS "-fno-rtti")
|
# ============ Engine ============
|
||||||
|
|
||||||
target_include_directories(Engine PUBLIC
|
file(GLOB_RECURSE ENGINE_SOURCES CONFIGURE_DEPENDS src/*.cpp)
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
add_library(Engine STATIC ${ENGINE_SOURCES})
|
||||||
# header-only 库:纯 include 路径
|
|
||||||
${CMAKE_SOURCE_DIR}/Lib/entt/src
|
# PhysicsDebugDraw 派生 Jolt::DebugRendererSimple:Jolt 以 -fno-rtti 编译
|
||||||
${CMAKE_SOURCE_DIR}/Lib/VulkanMemoryAllocator/include)
|
#(有 key function 的基类 typeinfo 符号不存在),该 TU 必须同步关闭 RTTI,
|
||||||
|
# 否则派生类 typeinfo 引用悬空符号(链接期 undefined reference)
|
||||||
target_link_libraries(Engine PUBLIC
|
set_source_files_properties(src/Physics/PhysicsDebugDraw.cpp PROPERTIES
|
||||||
glfw
|
COMPILE_OPTIONS "-fno-rtti")
|
||||||
glm::glm
|
|
||||||
Vulkan::Vulkan
|
target_include_directories(Engine PUBLIC
|
||||||
spdlog::spdlog
|
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
stb
|
# header-only 库:纯 include 路径
|
||||||
assimp::assimp
|
${CMAKE_SOURCE_DIR}/Lib/entt/src
|
||||||
meshoptimizer
|
${CMAKE_SOURCE_DIR}/Lib/VulkanMemoryAllocator/include)
|
||||||
Jolt::Jolt
|
|
||||||
imgui
|
target_link_libraries(Engine PUBLIC
|
||||||
tracy)
|
glfw
|
||||||
|
glm::glm
|
||||||
target_compile_definitions(Engine PRIVATE VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1)
|
Vulkan::Vulkan
|
||||||
|
spdlog::spdlog
|
||||||
|
stb
|
||||||
|
assimp::assimp
|
||||||
|
meshoptimizer
|
||||||
|
Jolt::Jolt
|
||||||
|
imgui
|
||||||
|
tracy)
|
||||||
|
|
||||||
|
target_compile_definitions(Engine PRIVATE VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1)
|
||||||
|
|||||||
+154
-154
@@ -1,154 +1,154 @@
|
|||||||
//
|
//
|
||||||
// Created by atdunbg on 2026/9/22.
|
// Created by atdunbg on 2026/9/22.
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <Engine/ImGui/ImGuiLayer.h>
|
#include <Engine/ImGui/ImGuiLayer.h>
|
||||||
#include <Engine/Core/Window.h>
|
#include <Engine/Core/Window.h>
|
||||||
#include <Engine/Core/Log.h>
|
#include <Engine/Core/Log.h>
|
||||||
#include <Engine/Vulkan/Device.h>
|
#include <Engine/Vulkan/Device.h>
|
||||||
#include <Engine/Renderer/Renderer.h>
|
#include <Engine/Renderer/Renderer.h>
|
||||||
|
|
||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
#include <backends/imgui_impl_glfw.h>
|
#include <backends/imgui_impl_glfw.h>
|
||||||
#include <backends/imgui_impl_vulkan.h>
|
#include <backends/imgui_impl_vulkan.h>
|
||||||
|
|
||||||
namespace Engine {
|
namespace Engine {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
void CheckVkResult(VkResult err)
|
void CheckVkResult(VkResult err)
|
||||||
{
|
{
|
||||||
if (err != VK_SUCCESS) {
|
if (err != VK_SUCCESS) {
|
||||||
ENGINE_ERROR("ImGui Vulkan backend error: VkResult={}", static_cast<int>(err));
|
ENGINE_ERROR("ImGui Vulkan backend error: VkResult={}", static_cast<int>(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGuiLayer::ImGuiLayer(Window& window, Renderer& renderer)
|
ImGuiLayer::ImGuiLayer(Window& window, Renderer& renderer)
|
||||||
: m_Device(renderer.GetDevice())
|
: m_Device(renderer.GetDevice())
|
||||||
{
|
{
|
||||||
Device& device = renderer.GetDevice();
|
Device& device = renderer.GetDevice();
|
||||||
|
|
||||||
// 1. 描述符池(ImGui 自用:字体贴图 + 用户贴图)
|
// 1. 描述符池(ImGui 自用:字体贴图 + 用户贴图)
|
||||||
std::array<vk::DescriptorPoolSize, 11> poolSizes = {{
|
std::array<vk::DescriptorPoolSize, 11> poolSizes = {{
|
||||||
{ vk::DescriptorType::eSampler, 1000 },
|
{ vk::DescriptorType::eSampler, 1000 },
|
||||||
{ vk::DescriptorType::eCombinedImageSampler, 1000 },
|
{ vk::DescriptorType::eCombinedImageSampler, 1000 },
|
||||||
{ vk::DescriptorType::eSampledImage, 1000 },
|
{ vk::DescriptorType::eSampledImage, 1000 },
|
||||||
{ vk::DescriptorType::eStorageImage, 1000 },
|
{ vk::DescriptorType::eStorageImage, 1000 },
|
||||||
{ vk::DescriptorType::eUniformTexelBuffer, 1000 },
|
{ vk::DescriptorType::eUniformTexelBuffer, 1000 },
|
||||||
{ vk::DescriptorType::eStorageTexelBuffer, 1000 },
|
{ vk::DescriptorType::eStorageTexelBuffer, 1000 },
|
||||||
{ vk::DescriptorType::eUniformBuffer, 1000 },
|
{ vk::DescriptorType::eUniformBuffer, 1000 },
|
||||||
{ vk::DescriptorType::eStorageBuffer, 1000 },
|
{ vk::DescriptorType::eStorageBuffer, 1000 },
|
||||||
{ vk::DescriptorType::eUniformBufferDynamic, 1000 },
|
{ vk::DescriptorType::eUniformBufferDynamic, 1000 },
|
||||||
{ vk::DescriptorType::eStorageBufferDynamic, 1000 },
|
{ vk::DescriptorType::eStorageBufferDynamic, 1000 },
|
||||||
{ vk::DescriptorType::eInputAttachment, 1000 },
|
{ vk::DescriptorType::eInputAttachment, 1000 },
|
||||||
}};
|
}};
|
||||||
vk::DescriptorPoolCreateInfo poolInfo;
|
vk::DescriptorPoolCreateInfo poolInfo;
|
||||||
poolInfo.setFlags(vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet)
|
poolInfo.setFlags(vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet)
|
||||||
.setMaxSets(1000)
|
.setMaxSets(1000)
|
||||||
.setPoolSizes(poolSizes);
|
.setPoolSizes(poolSizes);
|
||||||
m_Pool = device.GetDevice().createDescriptorPool(poolInfo);
|
m_Pool = device.GetDevice().createDescriptorPool(poolInfo);
|
||||||
|
|
||||||
// 2. ImGui 上下文 + 平台/渲染后端
|
// 2. ImGui 上下文 + 平台/渲染后端
|
||||||
IMGUI_CHECKVERSION();
|
IMGUI_CHECKVERSION();
|
||||||
ImGui::CreateContext();
|
ImGui::CreateContext();
|
||||||
ImGui::StyleColorsDark(); // 深色主题(ImGui 默认是灰白 Classic)
|
ImGui::StyleColorsDark(); // 深色主题(ImGui 默认是灰白 Classic)
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||||
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // docking 分支
|
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // docking 分支
|
||||||
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // 多视口
|
// io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // 多视口
|
||||||
|
|
||||||
ImGui_ImplGlfw_InitForVulkan(window.GetHandle(), true);
|
ImGui_ImplGlfw_InitForVulkan(window.GetHandle(), true);
|
||||||
|
|
||||||
ImGui_ImplVulkan_InitInfo info = {};
|
ImGui_ImplVulkan_InitInfo info = {};
|
||||||
info.ApiVersion = VK_API_VERSION_1_4;
|
info.ApiVersion = VK_API_VERSION_1_4;
|
||||||
info.Instance = device.GetInstance();
|
info.Instance = device.GetInstance();
|
||||||
info.PhysicalDevice = device.GetPhysicalDevice();
|
info.PhysicalDevice = device.GetPhysicalDevice();
|
||||||
info.Device = device.GetDevice();
|
info.Device = device.GetDevice();
|
||||||
info.QueueFamily = device.GetGraphicsFamily();
|
info.QueueFamily = device.GetGraphicsFamily();
|
||||||
info.Queue = device.GetGraphicsQueue();
|
info.Queue = device.GetGraphicsQueue();
|
||||||
info.DescriptorPool = m_Pool;
|
info.DescriptorPool = m_Pool;
|
||||||
info.MinImageCount = 2; // kMaxFramesInFlight
|
info.MinImageCount = 2; // kMaxFramesInFlight
|
||||||
info.ImageCount = renderer.GetSwapchainImageCount();
|
info.ImageCount = renderer.GetSwapchainImageCount();
|
||||||
info.CheckVkResultFn = CheckVkResult;
|
info.CheckVkResultFn = CheckVkResult;
|
||||||
|
|
||||||
// 与引擎共存的关键:不用 RenderPass,走 dynamic rendering,
|
// 与引擎共存的关键:不用 RenderPass,走 dynamic rendering,
|
||||||
// 颜色格式 = swapchain 格式(引擎无 vk::Pipeline,全动态状态)
|
// 颜色格式 = swapchain 格式(引擎无 vk::Pipeline,全动态状态)
|
||||||
info.UseDynamicRendering = true;
|
info.UseDynamicRendering = true;
|
||||||
const vk::Format colorFormat = renderer.GetSwapchainFormat();
|
const vk::Format colorFormat = renderer.GetSwapchainFormat();
|
||||||
|
|
||||||
VkPipelineRenderingCreateInfoKHR pri{};
|
VkPipelineRenderingCreateInfoKHR pri{};
|
||||||
pri.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR;
|
pri.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR;
|
||||||
pri.colorAttachmentCount = 1;
|
pri.colorAttachmentCount = 1;
|
||||||
pri.pColorAttachmentFormats =
|
pri.pColorAttachmentFormats =
|
||||||
reinterpret_cast<const VkFormat*>(&colorFormat);
|
reinterpret_cast<const VkFormat*>(&colorFormat);
|
||||||
info.PipelineInfoMain.PipelineRenderingCreateInfo = pri;
|
info.PipelineInfoMain.PipelineRenderingCreateInfo = pri;
|
||||||
// 多视口子窗口同格式
|
// 多视口子窗口同格式
|
||||||
info.PipelineInfoForViewports.PipelineRenderingCreateInfo = pri;
|
info.PipelineInfoForViewports.PipelineRenderingCreateInfo = pri;
|
||||||
|
|
||||||
ImGui_ImplVulkan_Init(&info);
|
ImGui_ImplVulkan_Init(&info);
|
||||||
|
|
||||||
ENGINE_INFO("ImGuiLayer initialized (docking + viewports, dynamic rendering)");
|
ENGINE_INFO("ImGuiLayer initialized (docking + viewports, dynamic rendering)");
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGuiLayer::~ImGuiLayer()
|
ImGuiLayer::~ImGuiLayer()
|
||||||
{
|
{
|
||||||
// 后端销毁自己的管线/字体资源前先等 GPU 空闲(层先于 Renderer 析构)
|
// 后端销毁自己的管线/字体资源前先等 GPU 空闲(层先于 Renderer 析构)
|
||||||
m_Device.GetDevice().waitIdle();
|
m_Device.GetDevice().waitIdle();
|
||||||
|
|
||||||
// 场景纹理描述符先归还(后端 shutdown 前必须完成)
|
// 场景纹理描述符先归还(后端 shutdown 前必须完成)
|
||||||
for (u32 slot = 0; slot < kMaxFramesInFlight; ++slot) {
|
for (u32 slot = 0; slot < kMaxFramesInFlight; ++slot) {
|
||||||
UpdateSceneTexture(slot, vk::ImageView{});
|
UpdateSceneTexture(slot, vk::ImageView{});
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui_ImplVulkan_Shutdown();
|
ImGui_ImplVulkan_Shutdown();
|
||||||
ImGui_ImplGlfw_Shutdown();
|
ImGui_ImplGlfw_Shutdown();
|
||||||
ImGui::DestroyContext();
|
ImGui::DestroyContext();
|
||||||
|
|
||||||
m_Device.GetDevice().destroyDescriptorPool(m_Pool);
|
m_Device.GetDevice().destroyDescriptorPool(m_Pool);
|
||||||
|
|
||||||
ENGINE_INFO("ImGuiLayer shut down");
|
ENGINE_INFO("ImGuiLayer shut down");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImGuiLayer::UpdateSceneTexture(u32 slot, vk::ImageView view)
|
void ImGuiLayer::UpdateSceneTexture(u32 slot, vk::ImageView view)
|
||||||
{
|
{
|
||||||
if (slot >= kMaxFramesInFlight) return;
|
if (slot >= kMaxFramesInFlight) return;
|
||||||
|
|
||||||
// 旧描述符先归还(池带 eFreeDescriptorSet;resize 重建时 GPU 已
|
// 旧描述符先归还(池带 eFreeDescriptorSet;resize 重建时 GPU 已
|
||||||
// waitIdle,无在飞引用)
|
// waitIdle,无在飞引用)
|
||||||
if (m_SceneTextures[slot]) {
|
if (m_SceneTextures[slot]) {
|
||||||
ImGui_ImplVulkan_RemoveTexture(
|
ImGui_ImplVulkan_RemoveTexture(
|
||||||
(VkDescriptorSet)(intptr_t)m_SceneTextures[slot]);
|
(VkDescriptorSet)(intptr_t)m_SceneTextures[slot]);
|
||||||
m_SceneTextures[slot] = ImTextureID_Invalid;
|
m_SceneTextures[slot] = ImTextureID_Invalid;
|
||||||
}
|
}
|
||||||
if (view) {
|
if (view) {
|
||||||
// 后端自有采样器(linear):只需提供 view 与采样期 layout——
|
// 后端自有采样器(linear):只需提供 view 与采样期 layout——
|
||||||
// RG 已保证 ImGuiPass 执行期处于 eShaderReadOnlyOptimal
|
// RG 已保证 ImGuiPass 执行期处于 eShaderReadOnlyOptimal
|
||||||
m_SceneTextures[slot] = (ImTextureID)(intptr_t)
|
m_SceneTextures[slot] = (ImTextureID)(intptr_t)
|
||||||
ImGui_ImplVulkan_AddTexture(
|
ImGui_ImplVulkan_AddTexture(
|
||||||
(VkImageView)view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
(VkImageView)view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImGuiLayer::NewFrame()
|
void ImGuiLayer::NewFrame()
|
||||||
{
|
{
|
||||||
ImGui_ImplVulkan_NewFrame();
|
ImGui_ImplVulkan_NewFrame();
|
||||||
ImGui_ImplGlfw_NewFrame();
|
ImGui_ImplGlfw_NewFrame();
|
||||||
ImGui::NewFrame();
|
ImGui::NewFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImGuiLayer::BuildDrawData()
|
void ImGuiLayer::BuildDrawData()
|
||||||
{
|
{
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImGuiLayer::RenderDrawData(vk::CommandBuffer cmd)
|
void ImGuiLayer::RenderDrawData(vk::CommandBuffer cmd)
|
||||||
{
|
{
|
||||||
if (!m_Enabled) return;
|
if (!m_Enabled) return;
|
||||||
|
|
||||||
ImDrawData* drawData = ImGui::GetDrawData();
|
ImDrawData* drawData = ImGui::GetDrawData();
|
||||||
if (drawData && drawData->CmdListsCount > 0) {
|
if (drawData && drawData->CmdListsCount > 0) {
|
||||||
ImGui_ImplVulkan_RenderDrawData(drawData, cmd);
|
ImGui_ImplVulkan_RenderDrawData(drawData, cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Engine
|
} // namespace Engine
|
||||||
|
|||||||
Reference in New Issue
Block a user