我试图使用SwapChainPanel DirectX12,使用SharpDx,但创建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://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运行得很好)。
有什么不对吗?
发布于 2018-09-10 20:24:49
我就是这样做的:
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!
https://stackoverflow.com/questions/52119683
复制相似问题