DXGI_ERROR_INVALID_CALL 为核心 window 创建交换链时
DXGI_ERROR_INVALID_CALL when creating swap chain for core window
我正在通过 DirectXTutorials.com 上的 this tutorial 学习 DX11。我已经创建了设备及其上下文,现在需要创建交换链。
但是,当我调用 CreateSwapChainForCoreWindow(...)
时,它会将交换链保留为 nullptr 和 returns 0x887a0001
。 DirectX 错误查找为代码吐出以下内容:
Name: DXGI_ERROR_INVALID_CALL
Description: The application has made an erroneous API call that it had enough information to avoid.
This error is intended to denote that the application should be altered to avoid the error.
Use of the debug version of the DXGI.DLL will provide run-time debug output with further information.
Severity code: Failed
我应该如何更改代码以避免此错误?我尝试按照其他答案更改交换链描述的格式或效果,但无济于事。
这是有问题的代码,这是最后一次失败的调用:
void Game::Initialize()
{
// Define temporary pointers to a device and a device context
ComPtr<ID3D11Device> dev11;
ComPtr<ID3D11DeviceContext> devcon11;
// Create the device and device context objects
D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
0,
nullptr,
0,
D3D11_SDK_VERSION,
&dev11,
nullptr,
&devcon11
);
// Convert the pointers from the DirectX 11 version to the DirectX 11.1 versions
dev11.As(&mDevice);
devcon11.As(&mDeviceContext);
// Obtain the DXGI factory
// [1] Convert our ID3D11Device1 into an IDXGIDevice1
ComPtr<IDXGIDevice1> dxgiDevice;
mDevice.As(&dxgiDevice);
// [2] Use the IDXGDevice1 interface to get access to the adapter
ComPtr<IDXGIAdapter> dxgiAdapter;
dxgiDevice->GetAdapter(&dxgiAdapter);
// [3] Use the IDXGIAdapter interface to get access to the factory
ComPtr<IDXGIFactory2> dxgiFactory;
dxgiAdapter->GetParent(__uuidof(IDXGIFactory2), &dxgiFactory);
// Set up the swap chain description
DXGI_SWAP_CHAIN_DESC1 scd = { 0 };
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // How the swap chain should be used
scd.BufferCount = 2; // A front and back buffer
scd.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // The most common swap chain format
scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // The recommended flip mode
scd.SampleDesc.Count = 1; // Disable anti-aliasing
HRESULT result = dxgiFactory->CreateSwapChainForCoreWindow(
mDevice.Get(), // Address of the device
reinterpret_cast<IUnknown*>(CoreWindow::GetForCurrentThread()), // Address of the window
&scd, // Address of the swap chain description
nullptr, // Monitor selection stuff - leave null
&mSwapChain // Address of the new swap chain
);
}
mDevice
、mDeviceContext
、mSwapChain
都是class.
的成员变量
我直接从 SDL 和 SFML 转向 DirectX,所以这是我一直以来的最低水平。如果有足够的信息来避免有问题的调用,那么我应该调用什么?我看到一些问题,人们在打电话 CreateDeviceAndSwapChain
,这是首选方法吗?
编辑:
澄清一下,项目类型是 DirectX11 App (Universal Windows)
,如果这有什么不同的话。
这是问题行:
reinterpret_cast<IUnknown*>(CoreWindow::GetForCurrentThread()), // Address of the window
查看失败点的堆栈跟踪表明我正在从 App::Initialize
调用 Game::Initialize
。因此,GetForCurrentThread()
返回 null,因为 window 还不存在! (App::SetWindow(CoreWindow^ window)
是后面调用的,以后才能得到window。)
这是一个非常简单的错误。
我只是在同一个调用中遇到了同样的错误,但在我的情况下,原因是我传入了 D3D 设备而不是 D3D 命令队列。这对于 D3D11 和 D3D12 是不同的。参见:
Parameters
pDevice
For Direct3D 11, and earlier versions of Direct3D, this is a pointer to the Direct3D device for the swap chain. For Direct3D 12 this is a pointer to a direct command queue (refer to ID3D12CommandQueue). This parameter cannot be NULL.
我正在通过 DirectXTutorials.com 上的 this tutorial 学习 DX11。我已经创建了设备及其上下文,现在需要创建交换链。
但是,当我调用 CreateSwapChainForCoreWindow(...)
时,它会将交换链保留为 nullptr 和 returns 0x887a0001
。 DirectX 错误查找为代码吐出以下内容:
Name: DXGI_ERROR_INVALID_CALL
Description: The application has made an erroneous API call that it had enough information to avoid.
This error is intended to denote that the application should be altered to avoid the error.
Use of the debug version of the DXGI.DLL will provide run-time debug output with further information.
Severity code: Failed
我应该如何更改代码以避免此错误?我尝试按照其他答案更改交换链描述的格式或效果,但无济于事。
这是有问题的代码,这是最后一次失败的调用:
void Game::Initialize()
{
// Define temporary pointers to a device and a device context
ComPtr<ID3D11Device> dev11;
ComPtr<ID3D11DeviceContext> devcon11;
// Create the device and device context objects
D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
0,
nullptr,
0,
D3D11_SDK_VERSION,
&dev11,
nullptr,
&devcon11
);
// Convert the pointers from the DirectX 11 version to the DirectX 11.1 versions
dev11.As(&mDevice);
devcon11.As(&mDeviceContext);
// Obtain the DXGI factory
// [1] Convert our ID3D11Device1 into an IDXGIDevice1
ComPtr<IDXGIDevice1> dxgiDevice;
mDevice.As(&dxgiDevice);
// [2] Use the IDXGDevice1 interface to get access to the adapter
ComPtr<IDXGIAdapter> dxgiAdapter;
dxgiDevice->GetAdapter(&dxgiAdapter);
// [3] Use the IDXGIAdapter interface to get access to the factory
ComPtr<IDXGIFactory2> dxgiFactory;
dxgiAdapter->GetParent(__uuidof(IDXGIFactory2), &dxgiFactory);
// Set up the swap chain description
DXGI_SWAP_CHAIN_DESC1 scd = { 0 };
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // How the swap chain should be used
scd.BufferCount = 2; // A front and back buffer
scd.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // The most common swap chain format
scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // The recommended flip mode
scd.SampleDesc.Count = 1; // Disable anti-aliasing
HRESULT result = dxgiFactory->CreateSwapChainForCoreWindow(
mDevice.Get(), // Address of the device
reinterpret_cast<IUnknown*>(CoreWindow::GetForCurrentThread()), // Address of the window
&scd, // Address of the swap chain description
nullptr, // Monitor selection stuff - leave null
&mSwapChain // Address of the new swap chain
);
}
mDevice
、mDeviceContext
、mSwapChain
都是class.
我直接从 SDL 和 SFML 转向 DirectX,所以这是我一直以来的最低水平。如果有足够的信息来避免有问题的调用,那么我应该调用什么?我看到一些问题,人们在打电话 CreateDeviceAndSwapChain
,这是首选方法吗?
编辑:
澄清一下,项目类型是 DirectX11 App (Universal Windows)
,如果这有什么不同的话。
这是问题行:
reinterpret_cast<IUnknown*>(CoreWindow::GetForCurrentThread()), // Address of the window
查看失败点的堆栈跟踪表明我正在从 App::Initialize
调用 Game::Initialize
。因此,GetForCurrentThread()
返回 null,因为 window 还不存在! (App::SetWindow(CoreWindow^ window)
是后面调用的,以后才能得到window。)
这是一个非常简单的错误。
我只是在同一个调用中遇到了同样的错误,但在我的情况下,原因是我传入了 D3D 设备而不是 D3D 命令队列。这对于 D3D11 和 D3D12 是不同的。参见:
Parameters
pDevice
For Direct3D 11, and earlier versions of Direct3D, this is a pointer to the Direct3D device for the swap chain. For Direct3D 12 this is a pointer to a direct command queue (refer to ID3D12CommandQueue). This parameter cannot be NULL.