SharpShade
SharpShade

Reputation: 2183

DirectX - Access denied when creating swap chain for CoreWindow

I'm currently trying to create a swapchain for a CoreWindow using the latest SharpDX as DirectX wrapper and UWP as project base framework.

The documentation on that is so sparse it's unbelievable. Nonetheless I could find a snippet which looked promising. Inititally I always got an E_INVALIDCALL error message. Now it's "only" E_ACCESSDENIED.

So far I've done this to set up the chain:

var description = new SwapChainDescription1
{
    BufferCount = 2,
    Flags = SwapChainFlags.None,
    SampleDescription = new SampleDescription(1, 0),
    SwapEffect = SwapEffect.FlipSequential,
    Usage = Usage.RenderTargetOutput,
    Width = 0,
    Height = 0,
    Scaling = Scaling.None,
    Format = Format.B8G8R8A8_UNorm,
    Stereo = false
};

CoreWindow window = CoreWindow.GetForCurrentThread();
if (window == null)
{
    Logging.Error("Could not retrieve core window for swap chain.");
    throw new Exception("Invalid core window.");
}

using (var device = _device.QueryInterface<SharpDX.DXGI.Device2>())
{
    device.MaximumFrameLatency = 1;
    using (Adapter adapter = device.Adapter)
    {
        using (ComObject coreWindow = new ComObject(window))
        {
            using (Factory2 factory = adapter.GetParent<Factory2>())
                _swapChain = new SwapChain1(factory, _device, coreWindow, ref description);
        }
    }
}

The constructor of SwapChain1 throws the SharpDX exception:

SharpDX.Result.CheckError() SharpDX.DXGI.Factory2.CreateSwapChainForCoreWindow(ComObject deviceRef, ComObject windowRef, SwapChainDescription1& descRef, Output restrictToOutputRef, SwapChain1 swapChainOut) SharpDX.DXGI.SwapChain1..ctor(Factory2 factory, ComObject device, ComObject coreWindow, SwapChainDescription1& description, Output restrictToOutput) RobInspect.Visualizer.Rendering.RenderingPanel.InitializeSizeDependentResources() RobInspect.Visualizer.Rendering.RenderingPanel.InitializeDevice()

"HRESULT: [0x80070005], Module: [General], ApiCode: [E_ACCESSDENIED/General access denied error], Message: Access is denied. "

Can anyone explain me why? "Access denied" is quite a broad statement and I'm not that experienced with DirectX's internals.

Further information: The code is executing on the main (UI) thread. So I guess I can exclude that the CoreWindow reference is inaccessible. Since this is first-time initialisation I also exclude the possibility of DirectX objects not being freed properly before creating the swap chain.

EDIT: That's the code for creating the device. Whereas the flags are set to DeviceCreationFlags.BgraSuuport and DeviceCreationFlags.Debug. The levels are set to FeatureLevel.Level_11_1 down to FeatureLevel.Level_9_1.

using (var device = new Device(DriverType.Hardware, flags, levels))
{
    _device = device.QueryInterface<Device1>();
    _context = _device.ImmediateContext1;
}

Upvotes: 2

Views: 2071

Answers (1)

SharpShade
SharpShade

Reputation: 2183

The solution to this problem is that the terms WinRT Core and WinRT XAML are rather misleading. Since UWP is based on CoreWindow and both support and use them it's not clear where to use what.

DirectX exposes two methods for WinRT and one for Desktop. One being Factory2.CreateSwapChainForCoreWindow(...) and one Factory2.CreateSwapChainForComposition(...). The difference is that one takes the CoreWindow as parameter and one does not. And here's the trap I fell into.

Core stands for the design-scheme with which one only uses IFrameworkView and IFrameworkViewSource (see here for an example with SharpDX) whereas XAML stands for the traditional scheme where you have the Windows.UI.Xaml.Application class.

When using the Core-model you have to call the ...ForCoreWindow(...) method in order to create a swap chain. While using the XAML based approach you need a composition swap chain. I for myself already tried that, but failed because I forgot to enable (tip: do this if not already done) native debugging so the DirectX Debug Layer actually showed me essential information which could have saved me hours if not days of trial and error.

The issue here is that both composition and CoreWindow swap chains require special settings in the SwapChainDescription1. I'll leave you with the MSDN documentation. Moreover if native debugging and the debug layer is enabled, DirectX will tell you exactly what setting is invalid.

Upvotes: 3

Related Questions