首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用CreateSwapChainForComposition创建交换链时出现异常

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

Stack Overflow用户
提问于 2018-08-31 16:41:19
回答 1查看 1.1K关注 0票数 1

我试图使用SwapChainPanel DirectX12,使用SharpDx,但创建SwapChain失败的原因不明。以下是我所拥有的一个简化版本:

代码语言:javascript
运行
复制
// 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://learn.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,Output restrictToOutputRef,SwapChain1 swapChainOut,at SharpDX.DXGI.SwapChain1..ctor(Factory2工厂,ComObject设备,SwapChainDescription1& description,Output restrictToOutput) at UI.MainPage.CreateSwapChain()}

DebugLayer没有显示任何其他信息。

该应用程序本身是一个普通的应用程序(min目标版本创建者更新15063)。我知道我可以在我当前的硬件上运行Directx12 (C++ hello world运行得很好)。

有什么不对吗?

EN

回答 1

Stack Overflow用户

发布于 2018-09-10 20:24:49

我就是这样做的:

代码语言:javascript
运行
复制
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!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52119683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档