perf: 提高 GPU 优先级 (#1146)

This commit is contained in:
Xu 2025-05-17 12:30:01 +08:00 committed by GitHub
commit d502ca22a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -17,6 +17,7 @@
#include "OverlayDrawer.h"
#include "CursorManager.h"
#include "EffectsProfiler.h"
#include <d3dkmthk.h>
namespace Magpie {
@ -84,6 +85,17 @@ static void LogAdapter(IDXGIAdapter4* adapter) noexcept {
desc.VendorId, desc.DeviceId, StrHelper::UTF16ToUTF8(desc.Description)));
}
static void SetGpuPriority() noexcept {
// 来自 https://github.com/obsproject/obs-studio/blob/16cb051a57bb357fe866252c1360ce2c38e2deec/libobs-d3d11/d3d11-subsystem.cpp#L429
// 不使用 REALTIME 优先级,它会造成系统不稳定,而且可能会导致源窗口卡顿。
// OBS 还调用了 SetGPUThreadPriority但这个接口似乎无用。
NTSTATUS status = D3DKMTSetProcessSchedulingPriorityClass(
GetCurrentProcess(), D3DKMT_SCHEDULINGPRIORITYCLASS_HIGH);
if (status != STATUS_SUCCESS) {
Logger::Get().NTError("D3DKMTSetProcessSchedulingPriorityClass 失败", status);
}
}
ScalingError Renderer::Initialize() noexcept {
_backendThread = std::thread(std::bind(&Renderer::_BackendThreadProc, this));
@ -94,6 +106,9 @@ ScalingError Renderer::Initialize() noexcept {
LogAdapter(_frontendResources.GetGraphicsAdapter());
// 每次创建 D3D 设备后尝试提高 GPU 优先级OBS 也是这么做的
SetGpuPriority();
if (!_CreateSwapChain()) {
Logger::Get().Error("_CreateSwapChain 失败");
return ScalingError::ScalingFailedGeneral;