I am developing an application using WPF. The app runs full screen, and I need it to resize nicely no matter the monitor resolution. The graphic designer has designed beautiful images for the UI buttons, backgrounds, etc. Using an Illustrator plug-in, all the images have been converted to xaml files. I've added all these images into the application, and they look great. I am also using a lot of Grid layouts so that the screen can resize while still maintain the layout. All of this is displaying as desired, nothing looks stretched when run at a different resolution. However, screen transitions and UI interaction are slow.
I am wondering, is this is due to the heavy use of graphics? Am I using too many Grid layouts? But, I need the Grids so that I can have resolution independence.
The application runs fine on my development machine, but is very noticeably slower on machine with lower performance capabilities. Yeah, this is to be expected, but not to the degree that I'm seeing. My employer insists the application runs smoothly on these lower performance machines.
I've done some profiling of the application and it seem that what takes the most time is the display stuff (though I'm not sure I fully understand how effectively to use a profiler).
If it is the WPF that is causing the slowdown, what can I do to improve this?
You can drill into which WPF activities are using up time using the Performance Profiling Tools for WPF. Assuming a heavy graphical load is causing the slowdown, this should give you some help as to what might need to be simplified (e.g. layouts) or removed (e.g. bitmap effects (these are a classic perf killer, though I don't want to prejudice your profiling!)).
If it is the WPF that is causing the slowdown
Probably not ;)
It is much more likely that it is your code that causes the slowdown. WPF is powerful, but you have to understand the core concepts to make it work well... You should have a look at this video from a PDC session, it provides lots of advice on how to make your WPF application faster
Convert your XAML Vector Images of buttons into Transparent PNG Images. Path and Shapes are very heavy to render, calculate and resize. Mostly after deployment, the images never change its better to have them as raster then vector, unless you want to perform smooth animation of changing shape, size or other attributes.
Grids are very costly layout managers as compared to Canvas, DockPanel. You can certainly think of replacing certain grids with DockPanel sometimes, but yes its not an easy fix it requires lot of brainstorming.
Avoid Panel with Single Child. Try to reduce Visual Hierarchy.
Use more of fixed size for buttons and such small elements, if you specify fixed sizes of children, it becomes easy for Panels to do layout processing.
In general, WPF is perfoming much worse in drawing performance than Windows Forms, and native GDI or DirectX.
Yes, WPF is powerful in the sense you might make some neat stuff that is not supported in GDI, but it is more sluggish.
If you are having much drawing to do, and you want to support it on slow hardware, then WPF is not a good choice.
WPF performance depends heavily on the quality of the video card in the machine more that the processor/memory. Bad video card = bad WPF performance.
Well, this is a long shot: when I installed VSTS 2010 (and it uses WPF) it was very slow on a Windows 2008 server with enough CPU/memory, and very fast in a more modest notebook. We managed to disable hardware acceleration and it became notably fast into that machine.
Maybe you do want to try this configuration, as it is very simple: Visual Studio 2010 Beta 2 editor performance fix running on a virtual machine
Related
If I'm looking to create a game that doesn't necessarily run full screen, but simply needs to feature 2D/3D graphics somewhere in a portion of the screen, what's my best approach?
Some specific questions could be:
What component would the rendered area use?
Are there any game libraries I could leverage for the rendered area?
What would be the most "pure" or "canonical" stack according to Microsoft to use here?
Omega --
Visual Studio 2010 and 2012 are Both WPF apps. WinRT is for Tablets / Mobile. WPF is certainly NOT outdated.
If I were you I wouldn't render everything out the way canvas forces you to, it might be a better approach to have the center item be a UI element named Frame, which is the base element for all UI related content in WPF.
In this way you would be able to leverage all of the possible types of controls in the Frame whether you decided that An ImageSourceType or Canvas is more applicable to a particular features of the game.
Depending on how you want to draw graphics, you could use (but are by no means limited to):
Canvas - which would be totally appropriate for slow moving games. This way you get the benefit of the various WPF layout routines and can define objects inside the scene in XAML/vectors as well.
WPF supports 3D graphics (using Direct3D on the backend) so you could probably set up an orthogonal projection matrix and treat it like a Direct3D context (with the WPF API). I don't have enough experience to know how slow this is compared to D3D, but it's certainly easier (built-in "scene graph" like support from the XAML architecture, for instance).
If you want to go whole-hog with Direct3D you could use SlimDX, which has a WPF shim that I've used in the past, as well as another third party control. There may be other libraries available as well.
Direct blitting to/from a Bitmap using WriteableBitmap (see WriteableBitmapEx for a third-party version with a much friendlier API) or similar.
There are probably a lot of other options too. My preference would be for using Canvas initially if it's a slow-paced game that doesn't need super-fast frame rates (the layout work does incur a fairly substantial overhead, but it's less work and may be easier to get looking exactly the way you want).
If you want absolute control and speed, use D3D through SlimDX, but this is a pretty hefty learning curve if you're new to it.
can anyone suggest any alternative to GDI?
The reason is that I have data that updates rapidly and GDI doesn't support that kind of render velocity... I've tried SharpDX & SlimDX, but none seems to work properly (can't force them to draw on form without errors)
Update 1: I'm developing in C#
Update 2: Data is binded to Visual Studio 2010 default Charts control. A lot of code has been written to add functionality & lags were discovered after large amount of data incoming. Reason is that I need compatibility with .NET Framework 4 & Windows XP and higher.
After spending a lot of time investigating, I've discovered that SlimDX and SharpDX have strict requirements to videocard, drivers, and problems running on Windows XP.
GDI is software only rendering, as well as GDI+ which handles alpha channel for transparency rendering. These two libraries are really limited and slow compared to a GPU based one.
My suggestion is that you should invest the time to implement a SharpDX Direct2D based drawing engine. I never heard about the issue you talked about with forms, I use SharpDX with Winforms and WPF and everything's going great!
If speed is the most important criteria, then you'll have to invest time into a GPU based rendering engine, GDI is way to slow and not adapted to real-time rendering.
Honestly I find it strange that GDI doesn't suffice in that context but there are a lot of alternatives: you could go for DirectDraw for complete power, GDI+ for more advanced manipulations and options, or even external softwares like GTK+.
I know WPF is more complex an flexible so could be thought to do more calculations. But since the rendering is done on the GPU, wouldn't it be faster than Winforms for the same application (functionally and visually)?
I mean when you are not running any games or heavy 3d rendering, the GPU isn't doing heavy work, right? Whereas the CPU is always busy.
Is this a valid assumption or is the GPU utilization of WPF a very minor operation in its pipeline?
EDIT: The application that I am interested is a 3d modeling and animation software, where you have 3d viewports to navigate and edit the scene, and objects inside the scene. But I want to use WPF because of its modern architecture, and it's from scratch.
EDIT2: Also for my purposes I will use DirectX hands down for the app itself because of the high end requirements of the software. As for people using lower end or computers without a dedicated GPU, that's OK since they aren't not in my primary customer area. Just like other high end 3d software for film and games, it will be understandable to require a powerful computer to fully benefit from the application.
Provided the machine has a GPU, you'll get better rendering performance in WPF.
We have a large desktop application that we wrote in WinForms, and are now porting to WPF. We've witnessed much better rendering performance, particularly when resizing windows or redrawing controls.
We've also found that WPF "controls" are more lightweight than WinForm controls. If I recall right, WPF controls do not necessarily require an operating system handle, and don't register for Windows window messages via WndProc, at least not independently.
For your case, since you're building a 3d modeling app, which kind of assumes some 3d hardware on the machine, you should absolutely use WPF over WinForms.
For the app scenario you describe, I would expect WPF to outperform WinForms for 3D work on a full featured GPU by a wide margin.
The difference between the application types is more than just the rendering the 3D vector pipeline. WPF's internal architecture is radically different than WinForms, specifically designed to overcome the caveats learned from years of prior experience with the Windows GDI and WinForms apps.
(WinForms is a relatively thin wrapper around Windows GDI and User model that was originally created in the late 1980's. The Windows User control model has evolved over the past 25 years, but the core architectural patterns are largely unchanged.)
For example, WPF always separates UI rendering from application logic. When the WPF window goes to draw something, the actual rendering happens on a background thread. The refreshed visuals are flipped to the display during the video retrace interval, so you don't get partial blits or "tearing" artifacts on screen.
WinForms does none of this. If you render to DirectX or OpenGL surfaces in a WinForms app, you have to do the work of flipping the video page and making sure it happens at the right time to avoid screen tearing artifacts.
Wpf's default controls are GPU aware and can be custom styled with glows and transparency and whatnot all GPU accelerated. WinForms controls do not benefit significantly from GPU features, since about the only things WinForms (Windows User controls) uses for rendering are 2D bitblit and rectangle fill. Glows, transparency, animations are all possible with WinForms, but you have to do all the work to implement them.
In WPF, UI slickness is mostly a matter of designing and styling to get WPF to do it for you. In WinForms, you have to push the pixels yourself.
This is a really tough question to answer.
A huge portion of WPF performance is your GPU. A good GPU works wonders for making WPF perform well. WPF can be very performant. If your requirements are to have a decent GPU, since it's a 3D modeling program, you'll probably find WPF performance to be as good or better than Windows Forms - though this really depends on what you're using.
That being said, it's usually difficult to compare - mostly because WPF allows you to add a lot of visual effects that people tend to never even attempt in Windows Forms. Many WPF applications "seem" faster even though they're actually slower in some instances because of extra visual clues.
That being said, if you have a LOT of controls, WPF can actually outperform Windows Forms by a fair amount. In Windows Forms, each control requires a separate window handle, and receives its own message sets. With a lot of controls, this can actually slow things down pretty dramatically.
The real question here should be is WPF perf. "good enough" for your application. If you're doing a 3D modeling application, chances are WPF will not be your bottleneck- it should be fine.
For 3D modeling, WPF is clearly a lot better choice than WinForms -- but it's still a long ways from ideal. Winforms has no (direct) support for 3D rendering at all, and WPF has some. For a program that's aimed primarily toward 3D rendering, however, you might be better off with something dedicated more specifically to 3D rendering tasks, such as OpenGL or Direct3D.
On their own, neither of those provides a huge advantage over WPF, but unless what you're doing is fairly specialized, I wouldn't use those directly though. I'd use something like OpenSceneGraph or Ogre3D, that can use either of those for its rendering, but provides a much higher-level interface and does more to manage rendering your scene.
Either or both of these probably could use WPF for the actual drawing -- but I'm pretty sure neither one does, at least currently. I don't know whether they ever will either -- they might, but I doubt it's a real priority, since they already support OpenGL and Direct3D. For their purposes WPF provides little advantage.
Seeing as WPF actually uses DirectX under the hood and the fact that you use XAML to specify DirectX objects, WPF is by far the easiest solution.
Example coding a camera in xaml:
<PerspectiveCamera x:Key="Camera"
Position="0, 0, 4"
LookDirection="0, 0, -4"
UpDirection="0, 1, 0"
FieldOfView="30"/>
I have developed a quite large application using MFC. Naturaly, I used GDI for drawing, CCmdTarget for event routing, and the document-view architecture.
It was a convenient development path.
Now, the client is interested in converting this application to .Net.
I would prefer (and they too) writing the new product in C#.
The application displays and interacts with thousands of graphic objects, so
I figured going with GDI+, although seems natuaral, can cause performance issues,
So I am thinking of using OpenGL, specifically - OpenTK - as the graphics library (it's 2D).
I know that OpenGL works differently that these Windows APIs, which rely on Invalidation of portion of the screen. OpenGL has a rendering loop which constantly draws to the screen.
My question is:
Is this an acceptable way to go, thinking of:
performance - will the users need special graphics cards (hardware?). It is graphics intensive, but it's not a high-end game
printing and print preview - are these things complex to achienve?
multiple selection and context menus
Is this library goes well inside windows forms?
I don't think so. Use WPF if you can or DirectX if you can't.
I know it might not be fair but if I'm programming on .NET (microsoft) on windows (microsoft) I'd rather use DirectX ... which is also from microsoft.
As a side note: don't reinvent the wheel. Recoding user controls in open-gl can be very time consuming, if you do make sure you have a good reason.
In my experience developing CAD-like software, the benefits of OpenGL and DirectX are fast depth testing, smooth rotation and panning, lighting and powerful texture capabilities. Obviously there are other benefits but, despite what most tutorials would lead you to believe, implementing a rendering system using either of these APIs is a significant undertaking and should not be taken lightly.
Specifically:
If it is a 2D app and you already have it implemented in GDI then switching to GDI+ will be much easier. Additionally, on modern hardware, 2D GDI or GDI+ can be about as fast as 2D OpenGL or DirectX. And ultimately, the end-user probably won't notice the difference, especially with double buffered support in GDI+.
You do not need (and probably don't want) a continuous rendering loop for your app. In OpenGL and DirectX you can explicitly invalidate the window when your scene changes.
If you go with OpenGL or DirectX you will need to consider putting your objects into display lists or vertex arrays (buffers) for fast drawing. This is not difficult but managing objects in this way adds complexity to the system and will most likely significantly change the architecture of your rendering system.
Printing in either OpenGL or DirectX can also be tedious. On the one hand you can render to a bitmap and print that out. However, for high quality images you may want vectorized images instead, which are difficult to produce with either of these rendering frameworks.
I would also stay away from writing GUIs in OpenGL or DirectX...unless you're really looking for a challenge ;~)
Finally, and this is just an annoyance from an install perspective, the Managed DirectX run-time library that must be installed on the user's machine is around 100 MB.
I have no experience with C#, but I have once built a layer system for a drawing program that used openGL for rendering.
To make this layer I asked openGL for the current framebuffer and converted it to an image to use as a texture under the current canvas. So I guess from there its pretty easy to go to printing and print preview.
Direct X and Open GL much faster than GDI+.
You can also use an TAO framework as an alternative to OpenTK.
Background: I am currently using custom controls within my C# project (basic controls just drawing a custom look and feel (using gdi+?)). The majoritiy of these controls have transparent segments for irregular shapes etc.
Problem: I am looking to overlay a semi-transparent (irregularly shaped) panel over a group of controls. Currently I Clip the covering panel to the region of the controls involved. This works fine and the results look good, however the process is so slow.
Question: Is there some manner I can speed up the rendering process by mixing in a little OpenGL/DirectX and getting the video card to perform all the necessary rendering rather than relying on the rather slow CPU?
AFAIK you can't really mix GDI+ and OpenGL/DX.
If you're getting slow performance and are absolutely sure that it's a bottleneck in GDI+ rather than in your code, than it could make sense to ditch GDI+ and replace it with DX/OGL. (You would have to write your own controls, though, which would be a major pain in the #$$)
Or, for a simpler approach, try WPF/Silverlight! It's customizable and skinnable by default and it's based on DX.
Look at WPF and Silverlight. you would sleep better at night.
Did you try turning on DoubleBuferring on your controls to see if that gives you a performance boost?
http://www.codeproject.com/KB/graphics/DoubleBuffering.aspx