Can you make animations multithreaded? How to improve animation performance - wpf

I'm using the FluidKit Cube Animation in my WPF application.
The animation is smooth when the application window is not in full-screen, but when the application is running in full screen (maximised - borderless window with no controls), the animation is not smooth.
I noticed the WPF application is hammering a single thread and only utilising one of the four cores of the CPU, memory utilisatio is low.
I'm wondering if there is any way of making the window multithreaded or just the animations multithreaded so the application can utilise more of the available hardware?
Are there any other ways to improve animations?
Ben

I will be really excited if someone tells me a UI thread in WPF can leverage multi core CPUs....
Jokes apart, GPU and CPU are different concepts sadly. Will it be controversial if I say "The hardware acceleration mode for WPF apps utilizes GPU's own hyper threading model instead of multi core CPUs."?
Although there are ways to improve animations such as animating Adorners using PresentationSource, Transforming Drawings and Geometries than Shapes, CompositionTarget.Rendering event, BeginStoryBoard.HandoffBehavior="Compose" etc. You will find all of these discussed in details over the internet.
Interestingly there is an old (very useful post) about multi core systems and their impact on parallel .Net frameworks such as PLINQ and WPF 3.0 etc. http://social.msdn.microsoft.com/forums/en-US/wpf/thread/07562a10-17bd-44d3-975b-99ed02bd05de/
I hope this kicks off some very interesting brain storming from our very able StackOverflow experts....
Expectantly Waiting ... :-)

Try to avoid any effects and see how it works and totally avoid bitmap effects, because they force WPF to use software rendering which is not really good.
Suggest you to check out Performance Profiling Tools from Windows SDK. Good luck ;)

Related

How to keep a winform responsive while a heavy painting is underway

My program draws heavily on a winform. During the drawing, the winform is not responding. How to make it responding to my mouse? I want to use another thread to draw to the winform, but I am afraid I am going to meet the infamous cross-threads-access-conrtrol error.
Here is a great MSDN article that might help you: Give Your .NET-based Application a Fast and Responsive UI with Multiple Thread
When I was facing similar problems, it helped me a great deal to understand what to do.
Also, you might want to have a look at Parallel Programming in .NET Framework 4. This series also includes a strategy with calculations that are spread over multiple synchronized threads, all with using out-of-the-box .NET collections. This is not as complicatesd as it may sound. Just give it a try. :-)
Also, if you have the chance to use the upcoming .NET enhancements, it would be worth to try Asynchronous Programming with Async and Await
If the problem is GDI+ painting you can do all of your drawing on a separate bitmap in a separate thread (instead of drawing directly to screen) - and when you're done: copy the whole bitmap onto the form.
You can use BackgroundWorker class to keep your winform responsive. MSDN.
The problem is when you are performing UI stuff. You have to come back into the UI thread to do so. All I could recommend, is drawing a bit, letting the UI thread process any events, draw again a bit.
Thats probably the best way round, but it isnt easy to do ... Far less easier than just running non UI jobs in another thread ...

What type of application starts faster: Windows Forms or WPF?

As far as I know WPF applications should work faster than Windows Forms applications because WPF applications use DirectX for rendering instead of GDI. But what kind of application will start faster: WPF or Windows Forms? Will the start time be roughly the same or will one type of application be considerably faster than the other?
I am not targeting any specific operating system.
Just to point out, in my experience the WPF renderer is in fact quite a bit slower than Windows Forms, despite using DirectX. This is because lots of tesselation and layout calculations are done on the CPU side, which effectively cancels out the performance gain of using the GPU.
Just google around for "WPF Slow" or "WPF Performance" and you'll find horrified developers shocked to learn their time and effort spent converting WinForms to WPF because of spin about GPUs has been wasted, as the result is slower than the original application.
However ... I will say this. WPF is far superior to WindowsForms in terms of styling, graphical capabilities, developer productivity (databinding), animation, look and feel etc. If performance really is an issue, on certain areas you can optimise. For instance, did you know that you can get an API to write directly to bitmap in WPF (see the WriteableBitmap class) to do GDI-like operations in memory? This is (surprisingly) about 20x faster than the WPF primitives for some operations.
Similarly you can use virtualization to improve the performance of datagrids etc. Telerik have an example of their grid scrolling 1,000,000 rows at interactive framerates.
In conclusion, have a think about what you are doing and see similar examples on the web. Are you doing real-time scientific dataacquisition and visualization or just a standard line of business app? Comparing similar examples to see what the framework is capable of will help you in your decision.
Update 2013 Q3
As an update, in various projects I'm now seeing faster and faster performance from WPF by moving anything and everything off the UI thread. E.g. leave the UI thread just for UI, do as much work as possible using Task Parallel Library or RX. Make everything async and don't block the UI thread while waiting.
Eliminate binding errors as these contribute to WPF performance problems.
You can move animations (e.g. wait or loading animations) onto a different thread for glitch-free animation. You can use render-to-bitmap when doing animated transitions.
The long story short? It is possible to get great performance from WPF applications (as well as slick UI) but you need to put in a bit more work!
I think the start time will be about the same.. I have created a few WPF applications recently and in some cases I think the initial loadtime is a bit slower than win forms, but it is not much and WPF more than makes up for it when the application is up and running
From my experience, I think WPF is quite reactive at start compared to winforms. However, the question is tough, especially considering that WPF can contain the other and vice versa.
I'd follow the philosophy of this article : http://joshsmithonwpf.wordpress.com/2007/09/05/wpf-vs-windows-forms/
WPF and Winforms are just not the same tool, and neither of them is supposed to substitute for the other ;)
The question has a lot of duplicates on this forum, please check here for a link and discussion and all of them : WPF vs. Windows Forms

Creating high performance animations in WPF

I'm in a situation that requires many animations with effects like transparency to be applied but when there are about 10 of them running, my application slows down to a grinding halt! :(
I also, tried implementing a particle like effect using a frame by frame manual animation using the CompositionTarget.Rendering event, which changed an Image's Source property at a given framerate. Again, this works fine for the first few instances of the particles on the screen, but when more and more get added I need it to stay performance wise, which is always a tough ask for any application
I was wondering if anyone has any experience using pixel shaders in WPF for animations, or perhaps custom writeablebitmap's for animations?
I basically need to palm off most of the animation processing to the GPU if possible...
Thanks for any help you can give!
Cheers,
Mark
If you want performance you should think about using XNA instead.
Then, you can add the XNA project into your WPF app

WPF Architecture and Direct3D graphics acceleration

After reading the wikipedia article on WPF architecture, I am a bit confused with the benefits that WPF will offer me. (wikipedia is not a good research reference, but i found it useful). I have some questions
1) WPF uses d3d surfaces to render. However, the scenegraph is rendered into the d3d surface by the media integrated layer, which runs on the CPU. Is this true ?
2) I just found out by asking a question here that bitmaps dont use native resources. Does this mean that if i use alot of images, the MIL will copy each when rendering, rather than storing the bitmaps on the video card as a texture ?
3) The article mentions that WPF uses the painters algorithm which is back to front. Thats painfully slow. Is there any rational why WPF omits using Z-buffering and rendering front to back ? I am guessing its because the simplest way to handle transparency, but it seems weak.
The reason i ask is that i am thinking it wont be wise for me to put hundreds of buttons on a screen even though my colleagues are saying its directx accelerated. I dont quite believe that whole directx accelerated bit about WPF. I used to work on video games and my memory of writing d3d and opengl code tells me to be cautious.
For questions #1 and #3 you might want to check out this section of the SDK that discusses the Visual class and how it's rendering instructions are exchanged between the higher level framework and the media integration layer (MIL). It also discusses why the painters algorithm is used.
For #2, no that is most definitely not the case. The bitmap data will be moved to the hardware and cached there.
I tested that, I wrote two programs that show 1,000 buttons on screen, one in WinForms and one in WPF, both worked just fine.
I then pushed that up to 10,000 buttons, at that point the WPF app took a few seconds to start but run just fine, the WinForms app didn't start.
Win32 itself (and WinForms) isn't built for applications with hundreds of controls (believe me I wrote such an app), at some point it just stops working, WPF on the other hand, keeps working even if it slows down a bit at some point.
So, if you do need to put a lot of controls on screen WPF is your best bet (unless you want to roll your own UI framework - and you think you can do better than the entire MS perf team).
Also, WPF has many advantages other than graphics acceleration: richer graphics, drawing model that is easier to work with, animations, 3d and my personal favorite - amazing data-binding.
This will let you develop richer UIs faster - and I think that will make a much bigger difference than the painting algorithm used.
BTW, if you need to put hundreds of buttons on the screen this is likely to be a bad user experience and you may want to reconsider your UI design,

How to speed up WPF programs?

I love programming with and for Windows Presentation Framework. Mostly I write browser-like apps using WPF and XAML.
But what really annoys me is the slowness of WPF. A simple page with only a few controls loads fast enough, but as soon as a page is a teeny weeny bit more complex, like containing a lot of data entry fields, one or two tab controls, and stuff, it gets painful.
Loading of such a page can take more than one second. Seconds, indeed, especially on not so fast computers (read: the customers computers) it can take ages.
Same with changing values on the page. Everything about the WPF UI is somehow sluggy.
This is so mean! They give me this beautiful framework, but make it so excruciatingly slow so I'll have to apologize to our customers all the time!
My Question:
How do you speed up WPF?
How do you profile bottlenecks?
How do you deal with the slowness?
Since this seems to be an universal problem with WPF, I'm looking for general advice, useful for many situations and problems.
Some other related questions:
What tools do you use for WPF development
Tools to develop WPF or Silverlight applications
How do you speed up WPF?
Often after using one of the following profiling tools it is obvious what is causing my bottlenecks.
If memory is the issue then I virtualize my data.
If render time is the issue then I virtualize the controls or simplify control templates where possible.
If processing time is the issue I try to improve my algorithm or move that work to a background thread and show a throbber in my ui while the work is going.
How do you profile bottlenecks?
.NET Memory Pofiler
dotTrace
Performance Profiling Tools for WPF
Snoop
Crack.NET
How do you deal with the slowness?
Profiling and counseling.
Install SP1... Loads of very cool performance increases for WPF!!!
Read more here
Here is a example of 2 enhanchements made in SP1: Deffered scrolling & UI Element recyceling!!!
I can not add comments, that's why I post a new answer to this: I've found this video from the pdc09 that gives some ideas about how to profile wpf apps and because it helped me lot, I want to share the link:
Advanced WPF Application Performance Tuning and Analysis
WPF is meant for computers with modern graphics cards. Do your clients have modern graphics cards capable of running Aero? If your clients have older graphics cards, WPF will fall back to software rendering which runs extremely slow in comparison to hardware accelerated graphics.
You also might want to profile your application to make sure that it is actually WPF that is the slow part. It's possible that there is something else that is actually the bottleneck.
avoiding animations also helps a lot sometimes. if you have to use animations, decrease the framerate, this will improve "Feeled" performance
Remove alpha transparency/ bitmap effects.
can you give more details?
I only noticed a slow performance when I use something like a listview or a grid that has some complexity. The solution is to simplify it.
Other than that I only noticed a slow performance when loading the app for the first time.
HTH
I find it helpful to side-step the XAML, and write the entire UI in C#. This lets me precisely control when the controls are created and loaded. It also helps me understand what XAML is doing "under the covers".

Resources