sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.69k stars 641 forks source link

SharpDX Samples - Why is there a performance problem when using WinForms and worse in WPF? #956

Open datoml opened 6 years ago

datoml commented 6 years ago

Hello,

Currently I am using a WriteableBitmap to output images from a camera and I need at least 60FPS or more for it. So I wanted to use pure hardware rendering for this task because the WriteableBitmap doesn't perform that well when maximizing the window.

I used the Direct2D example and modified the Render methode to iterate over 5 Full-HD pictures.

When using the Direct2D1DemoApp as Base I am able to achieve 200-250 FPS.

But when I change the Base class to Direct2D1WinFormDemoApp the FPS are stuck at 60FPS.

I also created an example with WPF and used it as an ImageSource but the performance is even worse. A small window is able to achieve 60FPS aswell but when I maximze the window the FPS drops to 30FPS.

Can someone help me why this is happening? Is WinForms locked to 60FPS or is it bound to the refresh rate of my monitor or something? Why is WPF performing so bad? Should I use a WinForms Host inside WPF to achieve at least 60FPS?

Thank you in advance.

Gavin-Williams commented 6 years ago

Sounds like it's running at vsync rate. I don't know the particulars of that demo code, but you may be able to unlock it. Though I don't see a reason to even worry about it. You aren't typically rendering video at >60fps. I would just forget it and get on with the job.

As for the 30 fps, that is also suspiciously like it's missing it's 60fps frame target and dropping down to the next available vsync rate - 30fps. So yeah, your hunch is probably right. I'm not sure why it wouldn't be meeting it's 60fps target though.

Edit : I can't load those samples in my VS (updated). I'll have look into that before I can even think about helping. Ok, I got the MediaEngineAppVideo sample running - but unfortunately I can't run diagnostics on it because the following code throws an exception.

private static DXDevice CreateDeviceForVideo(out DXGIDeviceManager manager) { //Device need bgra and video support var device = new DXDevice(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.VideoSupport);

datoml commented 6 years ago

@Gavin-Williams Thank you for your feedback. The reason I need more then 60 FPS is that it's an acoustic camera and its sending high speed images. Video images and acoustic images and I want to display them with the FPS the camera is sending.

datoml commented 6 years ago

Ok. The following was the problem with the WinForms.

HwndRenderTargetProperties properties = new HwndRenderTargetProperties();
            properties.Hwnd = DisplayHandle;
            properties.PixelSize = new SharpDX.Size2(demoConfiguration.Width, demoConfiguration.Height);
            properties.PresentOptions = PresentOptions.Immediately;

You have to set the PresentOptions to Immediately to break out of the 60FPS cap. The problem now is how can I do it with WPF...

xoofx commented 6 years ago

The problem now is how can I do it with WPF...

Don't expect anything good FPS wise when using WPF. Hosting HWND into a WPF partially solve this, but WPF is intrinsically not designed to handle this correctly.

datoml commented 6 years ago

@xoofx I found an example that us a HWND inside WPF. Maybe I am lucky with that implementation. Currently I am looking into the RenderLoop how I can create my own inside the WPF control.

Mitra-M commented 6 years ago

@datoml, Can you share your WPF code?

datoml commented 6 years ago

@Mitra-M I used this control to render D3DImages inside WPF. Hope it helps you a bit :).

Mitra-M commented 6 years ago

@datoml , Thank you. I'll try that.

datoml commented 6 years ago

@Mitra-M If you have some further question I'll try to help as good as I can :).

Mitra-M commented 6 years ago

@datoml , I've tried it now, But has Airspace problem. I would prefer to use HelixToolkit.

Thanks anyway.