[MFC] [Direct3D] D3D11CreateDeviceAndSwapChain返回E_INVALIDARG [英] [MFC] [Direct3D] D3D11CreateDeviceAndSwapChain returning E_INVALIDARG
问题描述
我正在尝试编写一个库,为我的应用程序提供Direct3D支持.它们正在扩展DLL中实现,我期望可以从MFC应用程序中调用这些对象/函数.
I'm trying to write a library that provides my Applications with Direct3D support. They're being implemented in an extension DLL and I anticipate the objects/functions being called from MFC applications.
int D3DEngine::initialize(HWND w, HINSTANCE i)
{
window = w;
instance = i;
GetClientRect(window, &Location);
unsigned int width = Location.right - Location.left,
height = Location.bottom-Location.top;
D3D_DRIVER_TYPE dTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE//, D3D_DRIVER_TYPE_WARP
};
int tTypes = ARRAYSIZE(dTypes);
D3D_FEATURE_LEVEL dLevels[] =
{
D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0
};
int tLevels = ARRAYSIZE(dLevels);
DXGI_SWAP_CHAIN_DESC swapChainDescription;
ZeroMemory(&swapChainDescription, sizeof(DXGI_SWAP_CHAIN_DESC));
// Initialize the swap cahin
swapChainDescription.BufferCount = 1;
swapChainDescription.BufferDesc.Width = Location.right - Location.left;
swapChainDescription.BufferDesc.Height = Location.bottom - Location.top;
swapChainDescription.BufferDesc.Format = DXGI_FORMAT_R32G32B32A32_SINT;
swapChainDescription.BufferDesc.RefreshRate.Numerator = 30;
swapChainDescription.BufferDesc.RefreshRate.Denominator = 1;
swapChainDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDescription.OutputWindow = w;
swapChainDescription.Windowed = true;
swapChainDescription.SampleDesc.Count = 1;
swapChainDescription.SampleDesc.Quality = 0;
unsigned int flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if _DEBUG
flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
HRESULT results;
for (int c = 0; c < tTypes; c++)
{
results = D3D11CreateDeviceAndSwapChain(0, dTypes[c], 0,
flags, dLevels, tLevels, D3D11_SDK_VERSION,
&swapChainDescription, &swapper, &graphicsDevice, &version, &contextDevice);
if(results == E_INVALIDARG)
results = D3D11CreateDeviceAndSwapChain(0, dTypes[c], 0,
flags, &dLevels[1], tLevels-1, D3D11_SDK_VERSION,
&swapChainDescription, &swapper, &graphicsDevice, &version, &contextDevice);
if (SUCCEEDED(results))
{
driver = dTypes[c];
goto exitLoop;
}
//WCHAR* errorDesc =
}
exitLoop:
if (FAILED(results))
{
return NO_DEVICE;
}
ID3D11Texture2D* texture;
results = swapper->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&texture);
if (FAILED(results))
{
return NO_SWAP_CHAIN;
}
results = graphicsDevice->CreateRenderTargetView(texture, 0, &renderTarget);
if (texture)
texture->Release();
if (FAILED(results))
{
return NO_RENDER_TARGET;
}
contextDevice->OMSetRenderTargets(1, &renderTarget, 0);
// Create Swap Chain
isInit = true;
return NO_ERROR;
}
两次调用函数的想法来自这篇文章:
The idea of calling the function twice came from this article:
https://msdn.microsoft.com/zh-cn/library/windows/desktop/ff476879(v = vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ff476879(v=vs.85).aspx
文档中提供的关于E_INVALIDARG可能来自何处的唯一其他情况是,当第一个参数是与HARDWARE设备组合在一起的非NULL值时.但是,这不是我在这里所做的.
The only other circumstance the documentation provides regarding where the E_INVALIDARG might come from is when the first parameter is a non-NULL value combined with a HARDWARE device. However, that is not what I'm doing here.
是否有办法弄清楚为什么我会收到此错误?
Is there a way to figure out why I'm getting this error?
Visual Studio社区2017,
Visual Studio Community 2017,
Windows 10 Home
Windows 10 Home
我确实看到场景与Direct2D(可能已初始化其渲染目标)以及媒体基础一起使用.
I do see the scene being used in conjunction with Direct2D (the render target of which might have already been initialized), and possibly with the media foundation.
推荐答案
这篇关于[MFC] [Direct3D] D3D11CreateDeviceAndSwapChain返回E_INVALIDARG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!