使用CreateSwapChainForComposition创建交换链时出现异常

问题描述 投票:1回答:1

我试图通过使用SharpDx在SwapChainPanel中渲染DirectX12,但创建一个SwapChain失败的原因不明。这是我的简化版本:

// select adapter based on some simple scoring mechanism
SelectedAdapter = SelectAdapter();

// create device
using (var defaultDevice = new Device(SelectedAdapter, FeatureLevel.Level_12_0))
    Device = defaultDevice.QueryInterface<SharpDX.Direct3D12.Device2>();

// describe swap chain
SwapChainDescription1 swapChainDescription = new SwapChainDescription1
{
    AlphaMode = AlphaMode.Ignore,
    BufferCount = 2,
    Format = Format.R8G8B8A8_UNorm,
    Height = (int)(MainSwapChainPanel.RenderSize.Height),
    Width = (int)(MainSwapChainPanel.RenderSize.Width),
    SampleDescription = new SampleDescription(1, 0),
    Scaling = Scaling.Stretch,
    Stereo = false,
    SwapEffect = SwapEffect.FlipSequential,
    Usage = Usage.RenderTargetOutput
};

// create swap chain
using (var factory2 = SelectedAdapter.GetParent<Factory2>())
{
    /*--> throws exception:*/
    SwapChain1 swapChain1 = new SwapChain1(factory2, Device, ref swapChainDescription);
    SwapChain = swapChain1.QueryInterface<SwapChain2>();
}

// tie created swap chain with swap chain panel
using (ISwapChainPanelNative nativeObject = ComObject.As<ISwapChainPanelNative>(MainSwapChainPanel))
    nativeObject.SwapChain = SwapChain;

适配器的选择按预期工作(我有2个适配器+软件适配器)。我可以创建一个设备,我可以看到任务管理器中的应用程序正在使用该选定的适配器。

交换链的创建主要基于此文档:https://docs.microsoft.com/en-us/windows/uwp/gaming/directx-and-xaml-interop#swapchainpanel-and-gaming

我得到了factory2(枚举了所有适配器和其他东西)。 SwapChain1的构造函数在内部使用factory2来创建交换链:https://github.com/sharpdx/SharpDX/blob/master/Source/SharpDX.DXGI/SwapChain1.cs#L64

我将此方法与其他几个示例和教程进行了比较,这是应该完成的方式,但是,无论我选择的格式还是适配器,我都会遇到此异常:

{SharpDX.SharpDXException:HRESULT:[0x887A0001],模块:[SharpDX.DXGI],ApiCode:[DXGI_ERROR_INVALID_CALL / InvalidCall],消息:应用程序进行了无效的调用。调用的参数或某个对象的状态是不正确的。启用D3D调试层以通过调试消息查看详细信息。

在SharpDX.DXGI.Factory2.CreateSwapChainForComposition(IUnknown deviceRef,SwapChainDescription1&descRef,输出restrictToOutputRef,SwapChain1 swapChainOut)的SharpDX.Result.CheckError()处于SharpDX.DXGI.SwapChain1..ctor(Factory2工厂,ComObject设备,SwapChainDescription1和描述,输出restrictToOutput) )在UI.MainPage.CreateSwapChain()}

DebugLayer不显示任何其他信息。

该应用程序本身是一个常规的Windows Universal Blank App(最低目标版本Creators Update 15063)。我知道我可以在我当前的硬件上运行Directx12(C ++ hello world工作得很好)。

有什么想法有什么不对?

c# xaml uwp sharpdx directx-12
1个回答
0
投票

这就是我的工作方式:

try
{
    using (var factory4 = new Factory4())
    {
        SwapChain1 swapChain1 = new SwapChain1(factory4, CommandQueue, ref swapChainDescription);
        SwapChain = swapChain1.QueryInterface<SwapChain2>();
    }
 }
 catch (Exception e)
 {
     Debug.WriteLine(e.Message);
     return;
 }

 using (ISwapChainPanelNative nativeObject = ComObject.As<ISwapChainPanelNative>(MainSwapChainPanel))
     nativeObject.SwapChain = SwapChain;

所以基本上我需要Factory4接口来创建临时SwapChain1,我可以从中查询SwapChain2,然后这个SwapChain2可以附加到SwapChainPanel。

此外,一个非常重要的事情需要注意的是,即使SwapChain1构造函数签名(和文档)https://github.com/sharpdx/SharpDX/blob/master/Source/SharpDX.DXGI/SwapChain1.cs#L51说第二个参数应该是设备 - 它不应该。您需要传递的是CommandQueue对象。我不知道为什么。

此外,SwapChain1的构造函数说它需要Factory2,但不,你必须通过Factory4!

© www.soinside.com 2019 - 2024. All rights reserved.