Preparing the swap chain for a Windows Store app

In this recipe, we will look at the code necessary to create a new class inheriting from the D3DApplicationBase class from Chapter 2, Rendering with Direct3D, that will prepare a swap chain description for use in a Windows Store app for Windows 8.1. We will also prepare a Windows Store compatible version of the Common library we have used throughout this book.

Getting ready

To target Windows 8.1, we need to use Visual Studio 2013.

Before commencing, we will need to have the SharpDX 11.2 WinRT binaries at hand. At the time of writing this book, this requires using the latest development package (2.5.1) found on the SharpDX webpage http://sharpdx.org/news/. For the remainder of the chapter, we will assume that these can be located upon navigating to .ExternalBinDirectX11_2-Signed-winrt under the solution location.

Tip

There exists a SharpDX NuGet package; however, at the time of writing this book, the package has not yet been updated for Windows 8.1 and Direct3D 11.2.

How to do it…

We'll begin by creating a new class library and reusing a majority of the Common project used throughout the book so far, then we will create a new class D3DApplicationWinRT inheriting from D3DApplicationBase to be used as a starting point for our Windows Store app's render targets.

  1. Within Visual Studio, create a new Class Library (Windows Store apps) called Common.WinRT.
    How to do it…

    New Project dialog to create a class library project for Windows Store apps

  2. Add references to the following SharpDX assemblies: SharpDX.dll, SharpDX.D3DCompiler.dll, SharpDX.Direct2D1.dll, SharpDX.Direct3D11.dll, and SharpDX.DXGI within .ExternalBinDirectX11_2-Signed-winrt.
  3. Right-click on the new project; navigate to Add | Existing item...; and select the following files from the existing Common project: D3DApplicationBase.cs, DeviceManager.cs, Mesh.cs, RendererBase.cs, and HLSLFileIncludeHandlers.hlsl, and optionally, FpsRenderer.cs and TextRenderer.cs.
  4. Instead of duplicating the files, we can choose to Add As Link within the file selection dialog, as shown in the following screenshot:
    How to do it…

    Files can be added as a link instead of a copy

  5. Any platform-specific code can be wrapped with a check for the NETFX_CORE definition, as shown in the following snippet:
    #if NETFX_CORE
        ...Windows Store app code
    #else
        ...Windows Desktop code
    #endif
  6. Add a new C# abstract class called D3DApplicationWinRT.
    // Implements support for swap chain description for 
    // Windows Store apps
    public abstract class D3DApplicationWinRT
        : D3DApplicationBase
    {
    ...
    }
  7. In order to reduce the chances of our app being terminated to reclaim system resources, we will use the new SharpDX.DXGI.Device3.Trim function whenever our app is suspended (native equivalent is IDXGIDevice3::Trim). The following code shows how this is done:
    public D3DApplicationWinRT()
        : base()
    {
        // Register application suspending event
        Windows.ApplicationModel.Core
            .CoreApplication.Suspending += OnSuspending;
    }
    // When suspending hint that resources may be reclaimed
    private void OnSuspending(Object sender, Windows.ApplicationModel.SuspendingEventArgs e)
    {
        // Retrieve the DXGI Device3 interface from our 
        // existing Direct3D device.
        using (SharpDX.DXGI.Device3 dxgiDevice = DeviceManager
    .Direct3DDevice.QueryInterface<SharpDX.DXGI.Device3>())
        {
            dxgiDevice.Trim();
        }
    }
  8. The existing D3DApplicationBase.CreateSwapChainDescription function is not compatible with Windows Store apps. Therefore, we will override this and create a SwapChainDescription1 instance that is compatible with Windows Store apps. The following code shows the changes necessary:
    protected override SharpDX.DXGI.SwapChainDescription1 CreateSwapChainDescription()
    {
        var desc = new SharpDX.DXGI.SwapChainDescription1()
        {
            Width = Width,
            Height = Height,
            Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
            Stereo = false,
            SampleDescription.Count = 1,
            SampleDescription.Quality = 0,
            Usage = SharpDX.DXGI.Usage.BackBuffer | 
                SharpDX.DXGI.Usage.RenderTargetOutput,
            Scaling = SharpDX.DXGI.Scaling.Stretch,
            BufferCount = 2,
            SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential,
            Flags = SharpDX.DXGI.SwapChainFlags.None
        };
        return desc;
    }
  9. We will not be implementing the Direct3D render loop within a Run method for our Windows Store apps—this is because we will use the existing composition events where appropriate. Therefore, we will create a new abstract method Render and provide a default empty implementation of Run.
    public abstract void Render();
    
    [Obsolete("Use the Render method for WinRT", true)]
    public override void Run()
    { }

How it works…

As of Windows 8.1 and DirectX Graphics Infrastructure (DXGI) 1.3, all Direct3D devices created by our Windows Store apps should call SharpDX.DXGI.Device3.Trim when suspending to reduce the memory consumed by the app and graphics driver. This reduces the chance that our app will be terminated to reclaim resources while it is suspended—although our application should consider destroying other resources as well. When resuming, drivers that support trimming will recreate the resources as required.

We have used Windows.ApplicationModel.Core.CoreApplication rather than Windows.UI.Xaml.Application for the Suspending event, so that we can use the class for both an XAML-based Direct3D app as well as one that implements its own Windows.ApplicationModel.Core.IFrameworkView in order to render to CoreWindow directly.

Windows Store apps only support a flip presentation model and therefore require that the swap chain is created using a SharpDX.DXGI.SwapEffect.FlipSequential swap effect; this in turn requires between two and 16 buffers specified in the SwapChainDescription1.BufferCount property. When using a flip model, it is also necessary to specify the SwapChainDescription1.SampleDescription property with Count=1 and Quality=0, as multisample anti-aliasing (MSAA) is not supported on the swap chain buffers themselves. A flip presentation model avoids unnecessarily copying the swap-chain buffer and increases the performance.

Note

By removing Windows 8.1 specific calls (such as the SharpDX.DXGI.Device3.Trim method), it is also possible to implement this recipe using Direct3D 11.1 for Windows Store apps that target Windows 8.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.15.43