问题描述
我将SharpDX.WPF项目用于WPF功能,与SharpDX附带的工具包(具有相同的问题!)相比,这似乎是一个易于理解的低开销库.
I am using the SharpDX.WPF project for the WPF abilities, it seems like an easy to understand low-overhead library, compared to the Toolkit that comes with SharpDX (which has the same issue!)
首先:我使用以下命令为最新的SharpDX修复了SharpDX.WPF项目: https://stackoverflow.com/a/19791534/442833
First: I fixed the SharpDX.WPF project for the latest SharpDX using the following: https://stackoverflow.com/a/19791534/442833
然后,我对DXElement.cs进行了如下修改:此处:
Then I made the following hacky adjustment to DXElement.cs, a solution that was also done here:
private Query queryForCompletion;
public void Render()
{
if (Renderer == null || IsInDesignMode)
return;
var test = Renderer as D3D11;
if (queryForCompletion == null)
{
queryForCompletion = new Query(test.Device,
new QueryDescription {Type = QueryType.Event, Flags = QueryFlags.None});
}
Renderer.Render(GetDrawEventArgs());
Surface.Lock();
test.Device.ImmediateContext.End(queryForCompletion);
// wait until drawing completes
Bool completed;
var counter = 0;
while (!(test.Device.ImmediateContext.GetData(queryForCompletion, out completed)
&& completed))
{
Console.WriteLine("Yielding..." + ++counter);
Thread.Yield();
}
//Surface.Invalidate();
Surface.AddDirtyRect(new Int32Rect(0, 0, Surface.PixelWidth, Surface.PixelHeight));
Surface.Unlock();
}
然后我以立方体模式渲染8000个立方体...
Then I render 8000 cubes in a cube pattern...
经常被打印到控制台,但是闪烁仍然存在.我假设WPF足以在渲染完成之前使用其他线程显示图像,但是不确定...当我将WPF支持的Toolkit变体与SharpDX一起使用时,也会发生同样的问题.
gets printed to the console quite often, but the flickering is still there.I am assuming that WPF is nice enough to show the image using a different thread before the rendering is done, not sure though...This same issue also happens when I use the Toolkit variant of WPF support with SharpDX.
用于说明问题的图像:
- Bad
- Better
- Almost
- Intended
注意:它会随机在这些旧图像之间随机切换.我还使用了真正的旧硬件,使得闪烁现象更加明显(GeForce Quadro FX 1700)
Note: It randomly switches between these old images, randomly. I am also using really old hardware which makes the flickering much more appearant (GeForce Quadro FX 1700)
A制作了一个repo,其中包含与我用来解决此问题的源代码完全相同的源代码: https://github.com/ManIkWeet/FlickeringIssue/
A made a repo which contains the exact same source-code as I am using to get this issue:https://github.com/ManIkWeet/FlickeringIssue/
推荐答案
我认为您没有正确锁定.据我了解,您应该在整个渲染过程中锁定MSDN文档,而不仅仅是在渲染过程的最后锁定:
I think you are not locking properly. As far as I understand the MSDN documentation you are supposed to lock during the entire rendering not just at the end of it:
您在网上找到的有关D3DImage/SharpDX的信息有些令人困惑,因为SharpDX家伙并不真正喜欢D3DImage的实现方式(不能怪他们),因此有一些说法称这是"bug"在Microsoft方面,实际上它只是对API的不当使用.
The information you find on the net about D3DImage/SharpDX is somewhat confusing because the SharpDX guys don't really like the way D3DImage is implemented (can't blame them), so there are statements about this being a "bug" on Microsofts side when its actually just improper usage of the API.
是的,渲染期间的锁定存在性能问题,但是如果不将WPF移植到DirectX11并实现 SwapChainPanel . (WPF本身仍在DirectX9上运行)
Yes, locking during rendering has performance issues, but it is probably not possible to fix them without porting WPF to DirectX11 and implementing something like a SwapChainPanel which is available in UWP apps. (WPF itself still runs on DirectX9)
如果锁定是您的性能问题,我曾经(但从未测试过)的一个想法是,您可以渲染到屏幕外的表面,并减少锁定时间,以将该表面复制到D3DImage上.不知道这是否有助于提高性能,但可以尝试一下.
If the locking is a performance issue for you, one idea I had (but never tested) is that you could render to an offscreen surface and reduce the lock duration to copying that surface over to the D3DImage. No idea if that would help performance wise but its something to try.
这篇关于D3DImage和SharpDX在缓慢的硬件上闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!