fix: 修复叠加层不会立刻显示的问题

This commit is contained in:
刘旭 2023-07-09 19:57:38 +08:00
commit fd06ecc6a9
5 changed files with 31 additions and 15 deletions

View file

@ -59,6 +59,12 @@ void OverlayDrawer::Draw() noexcept {
return;
}
if (_isFirstFrame) {
// ImGui 的第一帧不会显示,我们连续渲染两帧
_isFirstFrame = false;
Draw();
}
_imguiImpl.BeginFrame();
ImGui::ShowDemoWindow();

View file

@ -66,6 +66,8 @@ private:
bool _isUIVisiable = false;
bool _isSrcMainWnd = false;
bool _isFirstFrame = true;
};
}

View file

@ -183,18 +183,7 @@ bool Renderer::_CreateSwapChain() noexcept {
return true;
}
void Renderer::Render(HCURSOR hCursor, POINT cursorPos) noexcept {
// 有新帧或光标改变则渲染新的帧
if (_lastAccessMutexKey == _sharedTextureMutexKey) {
if (_lastAccessMutexKey == 0 || (hCursor == _lastCursorHandle && cursorPos == _lastCursorPos)) {
// 第一帧尚未完成或光标没有移动
return;
}
}
_lastCursorHandle = hCursor;
_lastCursorPos = cursorPos;
void Renderer::_FrontendRender() noexcept {
WaitForSingleObjectEx(_frameLatencyWaitableObject.get(), 1000, TRUE);
ID3D11DeviceContext4* d3dDC = _frontendResources.GetD3DDC();
@ -228,7 +217,7 @@ void Renderer::Render(HCURSOR hCursor, POINT cursorPos) noexcept {
_backBuffer.get(),
0,
_destRect.left - scalingWndRect.left,
_destRect.top- scalingWndRect.top,
_destRect.top - scalingWndRect.top,
0,
_frontendSharedTexture.get(),
0,
@ -247,7 +236,7 @@ void Renderer::Render(HCURSOR hCursor, POINT cursorPos) noexcept {
}
// 绘制光标
_cursorDrawer.Draw(hCursor, cursorPos);
_cursorDrawer.Draw(_lastCursorHandle, _lastCursorPos);
// 两个垂直同步之间允许渲染数帧SyncInterval = 0 只呈现最新的一帧,旧帧被丢弃
_swapChain->Present(0, 0);
@ -256,6 +245,21 @@ void Renderer::Render(HCURSOR hCursor, POINT cursorPos) noexcept {
d3dDC->DiscardView(backBufferRtv);
}
void Renderer::Render(HCURSOR hCursor, POINT cursorPos) noexcept {
// 有新帧或光标改变则渲染新的帧
if (_lastAccessMutexKey == _sharedTextureMutexKey) {
if (_lastAccessMutexKey == 0 || (hCursor == _lastCursorHandle && cursorPos == _lastCursorPos)) {
// 第一帧尚未完成或光标没有移动
return;
}
}
_lastCursorHandle = hCursor;
_lastCursorPos = cursorPos;
_FrontendRender();
}
void Renderer::ToggleOverlay() noexcept {
if (!_overlayDrawer) {
_overlayDrawer = std::make_unique<OverlayDrawer>();
@ -271,6 +275,9 @@ void Renderer::ToggleOverlay() noexcept {
} else {
_overlayDrawer->SetUIVisibility(true);
}
// 立即渲染一帧
_FrontendRender();
}
bool Renderer::IsOverlayVisible() noexcept {

View file

@ -37,6 +37,8 @@ public:
private:
bool _CreateSwapChain() noexcept;
void _FrontendRender() noexcept;
void _BackendThreadProc() noexcept;
ID3D11Texture2D* _InitBackend() noexcept;

View file

@ -13,4 +13,3 @@ visual_studio
fmt:header_only=True
spdlog:header_only=True
spdlog:no_exceptions=True