Knowledgebase
Easy Fallback from DirectX to Software Rendering without code-behind
Posted by Andrew BT on 17 June 2015 07:40 PM

In the article Creating and Deploying Applications with the Direct3D10RenderSurface, we present how you can enable your applications to make use of the DirectX Renderer Plugin for accelerated performance in SciChart WPF Charts. 

If you haven't read about the benefits of the DirectX Renderer, you can do so at the article How Fast is SciChart's WPF Chart? DirectX vs. Software Comparison.

Falling back from DirectX to Software Rendering

If you deploy your application on multiple PCs, then it is possible that some will not support the DirectX renderer, which requires Windows Vista, 7, 8, 8.1 and DirectX10 or up. You will need to gracefully fall back to software rendering. We present a method in the article High Quality vs. High Speed vs. DirectX Renderer Plugins using the Direct3D10RenderSurface.InitializationFailed and Direct3D10RenderSurface.RenderingFailed events. This internally calls the Direct3D10CompatibilityHelper class which has this interface:

/// <summary>
/// A helper class which can be used to detect if the Direct3D10 enhancements to SciChart can run on the current hardware & operating system
/// </summary>
public static class Direct3D10CompatibilityHelper
{
    /// <summary>
    /// Supported operating systems include Windows Vista, Windows 7, Windows 8, 8.1 and up. Windows XP, 2000, 2003 are not supported. 
    /// </summary>
    public static bool IsSupportedOperatingSystem { get; ; }
 
    /// <summary>
    /// Determines if the DirectX10 Runtime Libraries can be found in the C:\Windows\System32 and C:\Windows\SysWow64 folders. If the libraries are not present, we recommend 
    /// installing the DirectX End User Runtime (June 2010) from https://www.microsoft.com/en-gb/download/details.aspx?id=8109. 
    /// This installs platform specific libraries (x86, x64) and SciChart dynamically chooses the correct DirectX version at runtime
    /// </summary>        
    public static bool HasDirectX10RuntimeInstalled { get; ; }
 
    /// <summary>
    /// Determines if the current GPU supports DirectX10 or later
    /// </summary>
    public static bool HasDirectX10CapableGpu { get; ; }
 
    /// <summary>
    /// Determines if the current hardware & software configuration fully supports DirectX10 or later
    /// </summary>
    public static bool SupportsDirectX10 { get { return IsSupportedOperatingSystem && HasDirectX10RuntimeInstalled && HasDirectX10CapableGpu; } }
}

InitializationFailed and RenderingFailed events require code behind

One of the drawbacks of the InitializationFailed and RenderingFailed events is that they require code-behind. You may not want to introduce this to your code. So we have included an easy attached property that allows you to enable the Direct3D10RenderSurface only when it can be enabled - and - with no code behind!

 

DirectXHelper.cs

/// <summary>
/// A helper class to enable the Direct3D10RenderSurface 
/// </summary>
public class DirectXHelper
{        
    public static readonly DependencyProperty TryApplyDirectXRendererProperty = DependencyProperty.RegisterAttached(
        "TryApplyDirectXRenderer", typeof (bool), typeof (DirectXHelper), new PropertyMetadata(default(bool), OnTryApplyDirectXRendererChanged));

    public static void SetTryApplyDirectXRenderer(SciChartSurface element, bool value)
    {
        element.SetValue(TryApplyDirectXRendererProperty, value);
    }

    public static bool GetTryApplyDirectXRenderer(SciChartSurface element)
    {
        return (bool)element.GetValue(TryApplyDirectXRendererProperty);
    }

    private static void OnTryApplyDirectXRendererChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var scs = d as SciChartSurface;
        if (scs != null && (bool)e.NewValue == true)
        {
            if (Direct3D10CompatibilityHelper.SupportsDirectX10)
            {
                var directXSurface = new Direct3D10RenderSurface();
                directXSurface.RenderingFailed += (s, arg) =>
                {
                    SetDefaultRenderSurface(scs);
                };
                directXSurface.InitializationFailed += (s, arg) =>
                {
                    SetDefaultRenderSurface(scs);
                };

                scs.RenderSurface = directXSurface;

                return;
            }
                
            SetDefaultRenderSurface(scs);
        }
    }

    private static void SetDefaultRenderSurface(SciChartSurface scs)
    {
        // Alternatively, fallback to HighSpeedRenderSurface, depending on your requirements
        scs.RenderSurface = new HighQualityRenderSurface();
    }
}

 

Usage in XAML

<SciChartSurface DirectXHelper.TryApplyDirectXRenderer="True">

... 
</SciChartSurface>

 

This will check if the Direct3D10RenderSurface can be applied, and apply it. If not, it will apply the HighQualityRenderSurface. Optionally, you can change the above code to fall back to the HighSpeedRenderSurface. 

 

 

 

(3 vote(s))
Helpful
Not helpful

Comments (0)

CONTACT US

Not sure where to start? Contact us, we are happy to help!


CONTACT US

SciChart Ltd, 16 Beaufort Court, Admirals Way, Docklands, London, E14 9XL. Email: Legal Company Number: 07430048, VAT Number: 101957725