【发布时间】:2021-04-07 09:36:40
【问题描述】:
我是图形渲染的新手,我正在尝试使用 D2D 和 D3D11 编写一个 win32 绘图应用程序。我使用两个重叠的屏幕外 D2D 位图来保留画布的内容,顶层位图是透明的。
每当接收到鼠标消息时,从最后一点到当前点的一条线将被渲染到顶级位图。然后我将按 Z-Order 将两个位图绘制到交换链的后台缓冲区,然后调用 Present(0, 0)。
您可能会注意到,在我的设计中,当前调用是事件驱动的。如果每 1 毫秒接收一次鼠标消息,那么在 1 秒内我将在顶层位图上渲染 1000 条折线(这很好),然后 1000 次合成两个位图,1000 次调用 Present(这是真的很糟糕,因为我只需要以 60 fps 的速度呈现)。冗余的组合调用和present调用占用了GPU最多的资源,最终Present(0, 0)阻塞了UI线程,鼠标消息上报的频率大幅降低。
int OnMouseMove(int x, int y)
{
// ...
// update top-level bitmap
DrawLineTo(topLevelBitmap, x, y);
// get back buffer
CComPtr<IDXGISurface> dxgiBackBuffer;
HRESULT hr = _dxgiSwapChain->GetBuffer(0, IID_PPV_ARGS(&dxgiBackBuffer));
// Draw two bitmaps to the back buffer
ClearBackBuffer(dxgiBackBuffer);
DrawBimap(dxgiBackBuffer, backgroundBitmap);
DrawBimap(dxgiBackBuffer, topLevelBitmap);
// Present
DXGI_PRESENT_PARAMETERS parameters = { 0 };
_dxgiSwapChain->Present1(0, 0, ¶meters);
// ...
}
我试图找到一个可用于触发当前调用的回调,例如 macOS/iOS 上的CVDisplayLink/CADisplayLink,或者可以生成可靠回调的更高优先级的计时器,但失败了。 (WM_TIMER 的优先级相当低,所以我什至没有尝试)
另一个想法是创建一个新线程并在 while 循环中调用 present 并在每个 present 被调用后休眠 16 毫秒。但是,我不确定这是否是标准方式,并且我也担心线程安全。
// in UI thread
int OnMouseMove(int x, int y)
{
// update top-level bitmap
DrawLineTo(topLevelBitmap, x, y);
}
// in new thread
while(1)
{
// get back buffer
CComPtr<IDXGISurface> dxgiBackBuffer;
HRESULT hr = _dxgiSwapChain->GetBuffer(0, IID_PPV_ARGS(&dxgiBackBuffer));
// Draw two bitmaps to the back buffer
ClearBackBuffer(dxgiBackBuffer);
DrawBimap(dxgiBackBuffer, backgroundBitmap);
DrawBimap(dxgiBackBuffer, topLevelBitmap);
// Present
DXGI_PRESENT_PARAMETERS parameters = { 0 };
_dxgiSwapChain->Present1(0, 0, ¶meters);
sleep(16);
}
所以我的问题是分离屏幕外渲染(绘制线)和屏幕渲染(绘制位图和呈现)的正确方法是什么?
【问题讨论】:
-
我的建议是使用 DirectComposition docs.microsoft.com/en-us/windows/win32/directcomp/… 或更好的 Windows.UI.Composition(这有点像 DComp 3.0,适用于桌面应用程序)。这样您只需“组合”您的场景(支持多线程、D2D、DWrite 等),您不必担心交换链、vsync 等。这是开创性的示例gist.github.com/kennykerr/…
标签: graphics directx directx-11 direct2d dxgi