【问题标题】:Exception when creating Swap Chain with CreateSwapChainForComposition使用 CreateSwapChainForComposition 创建交换链时出现异常
【发布时间】:2018-08-31 16:41:19
【问题描述】:

我正在尝试使用 SharpDxSwapChainPanel 中渲染 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.Result.CheckError() 在 SharpDX.DXGI.Factory2.CreateSwapChainForComposition(IUnknown deviceRef、SwapChainDescription1& descRef、输出限制输出引用、 SwapChain1 swapChainOut) 在 SharpDX.DXGI.SwapChain1..ctor(Factory2 工厂、ComObject 设备、SwapChainDescription1& 描述、输出 在 UI.MainPage.CreateSwapChain()}

中的 restrictToOutput)

DebugLayer 不显示任何附加信息。

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

有什么想法吗?

【问题讨论】:

  • 上次我在创建 SwapChain 时遇到问题,我必须先创建一个新的 DeviceContext。

标签: c# xaml uwp sharpdx directx-12


【解决方案1】:

这就是我的工作方式:

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!

【讨论】:

  • CommandQueues 替换了 direct12 中的设备,以便部分有意义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 2018-11-01
  • 1970-01-01
相关资源
最近更新 更多