Direct2D中的多线程 [英] Multithreading in Direct2D

查看:1199
本文介绍了Direct2D中的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的D2D游戏引擎(它必须能够在窗口中显示和移动图像,至少),一切顺利,直到我决定切换到多线程版本的那一刻。我阅读了 MSDN文章,并且建议从多个线程使用一个多线程工厂。但文章声称它会更有效的几个单线程工厂(虽然本文描述服务器端渲染场景,原则是一样的我的情况,我错了吗?)。当我试图使用单线程单工厂方法,所有的图像显示和移动,但有可怕的闪烁。在我的WM_PAINT处理程序中,我试图做这样的事情:

I'm trying to create a simple D2D game engine (it must be able to display and move images in a window, at least), and everything went right until the moment when I decided to switch to the multithreaded version. I read this MSDN article, and it recommends using one multithreaded factory from several threads. But this article claims it would be more effective to have several single-threaded factories (though the article describes server-side rendering scenario, the principle is the same for my case, am I wrong?). When I tried to use one-thread-one-factory approach, all the images are displayed and moved, but there's terrible flickering. In my WM_PAINT handler I'm trying to do something like this:

for (CSingleThreadEngine *pElSingleThreadEngine : m_SingleThreadEngines) //each CSingleThreadEngine instance has its own D2D factory and an image collection
    pElSingleThreadEngine->Draw();

和pElSingleThreadEngine-> Draw()绘制如下:

and pElSingleThreadEngine->Draw() does drawing like this:

m_pRenderTarget->BeginDraw();
m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
for (CGameImage *pImage : m_GameImages)
{
    if (FAILED(pImage->Draw()))
        throw runtime_error("An object cannot be drawn");
}
m_pRenderTarget->EndDraw();

我认为错误的事情是,只有一个窗口有几个ID2D1HwndRenderTarget实例,线程在一个单独的窗口,它的工作原理很好。但是我想在一个窗口只绘制,我不能避免使用多个ID2D1HwndRenderTarget实例为此目的。所以我的问题是:

I think the wrong thing here is having several ID2D1HwndRenderTarget instances for just one window because if I make drawing each thread in a separate window, it works just fine. But I want to draw in one window only, and I can't avoid using multiple ID2D1HwndRenderTarget instances for this purpose. So my questions are:


  1. 创建多线程Direct2D应用程序的最佳做法是什么?

  2. 如果我使用的方法是正确的,我做错了什么,如何解决?

任何帮助非常感谢。

推荐答案

我看不到一个原因,为单个窗口使用几个HWND渲染目标。你尝试为每个线程创建offscrean位图,并将它们绘制到单个hwnd渲染目标?

I can't see a reason why you use several HWND render targets for a single window. Have you tried creating offscrean bitmaps for each thread and draw those to a single hwnd render target instead?

这篇关于Direct2D中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆