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.
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.
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.
Common.WinRT
.SharpDX.dll
, SharpDX.D3DCompiler.dll
, SharpDX.Direct2D1.dll
, SharpDX.Direct3D11.dll
, and SharpDX.DXGI
within .ExternalBinDirectX11_2-Signed-winrt
.Common
project: D3DApplicationBase.cs
, DeviceManager.cs
, Mesh.cs
, RendererBase.cs
, and HLSLFileIncludeHandlers.hlsl
, and optionally, FpsRenderer.cs
and TextRenderer.cs
.NETFX_CORE
definition, as shown in the following snippet:#if NETFX_CORE ...Windows Store app code #else ...Windows Desktop code #endif
D3DApplicationWinRT
.// Implements support for swap chain description for // Windows Store apps public abstract class D3DApplicationWinRT : D3DApplicationBase { ... }
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();
}
}
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; }
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() { }
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.
3.144.15.43