feat: 帧率控制——手动限帧 + VSync 开关
- Application 帧率控制 API:SetLimitFps/SetTargetFps/SetVSync。 限帧目标最低 24fps(防止过低刷新导致交互卡死),无上限; vsync 开启时 present 自带节奏,手动限帧不生效 - 限帧策略:sleep 到剩 ~2ms 后自旋,精度高且不烧 CPU - Swapchain 加 VSync 开关:ChoosePresentMode 按开关选 Mailbox(低延迟垂直同步)/ Immediate(不限速可撕裂), FIFO 兜底;切换经 SetVSync → 重建 swapchain(复用 HandleResize) - Scene Debug 面板新增 Frame 区块(vsync/limit/目标滑条) - Run 循环新增 CPU 归属遥测(update/ui/render/total,每 120 帧)
This commit is contained in:
@@ -54,6 +54,16 @@ namespace Engine {
|
||||
// 主渲染(ImGui pass 把 draw data 叠加到场景上)。
|
||||
void EnableImGui();
|
||||
|
||||
// ---- 帧率控制 ----
|
||||
// vsync 开启时 present 自带节奏,手动限帧不生效;
|
||||
// 限帧目标最低 24fps(防止过低刷新导致交互卡死),无上限
|
||||
void SetVSync(bool vsync);
|
||||
bool GetVSync() const { return m_VSync; }
|
||||
void SetLimitFps(bool on) { m_LimitFps = on; }
|
||||
bool GetLimitFps() const { return m_LimitFps; }
|
||||
void SetTargetFps(f32 fps) { m_TargetFps = std::max(24.0f, fps); }
|
||||
f32 GetTargetFps() const { return m_TargetFps; }
|
||||
|
||||
// ---- 供派生类访问的模块 ----
|
||||
Window& GetWindow() const { return *m_Window; }
|
||||
Renderer& GetRenderer() const { return *m_Renderer; }
|
||||
@@ -108,6 +118,11 @@ namespace Engine {
|
||||
f32 m_DeltaTime = 0.0f;
|
||||
u64 m_FrameCount = 0;
|
||||
|
||||
// 帧率控制
|
||||
bool m_VSync = true; // 垂直同步(present 自带节奏)
|
||||
bool m_LimitFps = false; // 手动限帧开关(vsync 开启时不生效)
|
||||
f32 m_TargetFps = 60.0f; // 限帧目标(≥24)
|
||||
|
||||
std::chrono::steady_clock::time_point m_LastFrameTime;
|
||||
};
|
||||
|
||||
|
||||
@@ -174,6 +174,10 @@ namespace Engine {
|
||||
const std::vector<std::string>& GetPassNames() const { return m_PassNames; }
|
||||
const std::vector<f32>& GetPassGpuMs() const { return m_PassGpuMs; }
|
||||
|
||||
// 垂直同步开关(Mailbox ↔ Immediate):切换会重建 swapchain,
|
||||
// 只应在帧 UI 构建段调用(Render 之前)
|
||||
void SetVSync(bool vsync);
|
||||
|
||||
private:
|
||||
|
||||
vk::Extent2D m_Extent{};
|
||||
|
||||
@@ -28,6 +28,11 @@ namespace Engine
|
||||
vk::SwapchainKHR Get() const { return m_Swapchain; }
|
||||
vk::Format GetFormat() const { return m_Format; }
|
||||
vk::Extent2D GetExtent() const { return m_Extent; }
|
||||
|
||||
// 垂直同步开关:影响 present mode(Mailbox ↔ Immediate),
|
||||
// 切换后需 Recreate 才生效
|
||||
void SetVSync(bool vsync) { m_VSync = vsync; }
|
||||
bool GetVSync() const { return m_VSync; }
|
||||
uint32_t GetImageCount() const { return static_cast<u32>(m_Images.size()); }
|
||||
|
||||
const std::vector<vk::Image>& GetImages() const { return m_Images; }
|
||||
@@ -56,6 +61,7 @@ namespace Engine
|
||||
|
||||
vk::Format m_Format;
|
||||
vk::Extent2D m_Extent;
|
||||
bool m_VSync = true;
|
||||
};
|
||||
} // namespace Engine
|
||||
|
||||
|
||||
Reference in New Issue
Block a user