Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Software rendering mode - WPF

Tags:

I have a WPF user control for which I need to force rendering in RenderMode.SoftwareOnly. Since I am using .NET 3.5, I had to do something like this:

var hwndSource = PresentationSource.FromVisual(this) as HwndSource; if (hwndSource != null) {     hwndSource.CompositionTarget.RenderMode = RenderMode.SoftwareOnly;         } 

But this is not working on my application, The WPF program is crashing on few machines and turning off the hardware acceleration at the registry level seems to fix the issue.

The above code is written in the Loaded event of the window. If I am correct, Loaded event happens after the controls are rendered (MSDN). So does it make sense to have the above code in that event? If not, which event would be appropriate for it?

Also, will setting RenderMode on a visual affects its children? Or do I need to set this specifically for each child elements?

like image 828
Navaneeth K N Avatar asked Feb 09 '11 22:02

Navaneeth K N


People also ask

What is software rendering mode?

Software rendering is the process of generating an image from a model by means of computer software. In the context of computer graphics rendering, software rendering refers to a rendering process that is not dependent upon graphics hardware ASICs, such as a graphics card. The rendering takes place entirely in the CPU.

Is WPF GPU accelerated?

The WPF system defines three rendering tiers: Rendering Tier 0 No graphics hardware acceleration. All graphics features use software acceleration. The DirectX version level is less than version 9.0.


2 Answers

Here's what we did:

private void Window_Loaded(object sender, RoutedEventArgs e) {     if (ForceSoftwareRendering)     {         HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;         HwndTarget hwndTarget = hwndSource.CompositionTarget;         hwndTarget.RenderMode = RenderMode.SoftwareOnly;     } } 

It worked OK for us, EXCEPT... This needs to be done for every Window. In .NET 3.5 there was no way to make the setting take effect application-wide. And there are some windows that you won't have as much control over - for example, right-click "context" windows. We found that there was no good solution for .NET 3.5 except the registry setting.

Edited

Here's the logic we used to determine when to force software rendering. It was suggested by a Microsoft support engineer.

public bool ForceSoftwareRendering  {     get      {          int renderingTier = (System.Windows.Media.RenderCapability.Tier >> 16);         return renderingTier == 0;     } } 

In .NET 4 Microsoft added an application-wide setting that works perfectly for us. Its a much better option because you don't need to set it on every window. You just set it once and it applies to all windows.

System.Windows.Media.RenderOptions.ProcessRenderMode

Edited

The new .NET 4.0 property can be set at application startup like this:

public partial class App : Application {     protected override void OnStartup(StartupEventArgs e)     {         if (ForceSoftwareRendering)             RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;     } } 
like image 89
Matt Varblow Avatar answered Oct 01 '22 17:10

Matt Varblow


You can also disable hardware rendering for the whole process by putting the next line in the application startup handler:

RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly; 

It is also possible to switch during runtime

like image 41
Ariën Avatar answered Oct 01 '22 18:10

Ariën