Reputation: 225
I am having a bit of trouble understanding DirectX 11.1. I am interested in building a traditional Win32 desktop program with it, but all of the documentation is for metro apps. Is 11.1 only for metro apps? I would think not as support for 11.1 was added to Windows 7.
In the past I had no problem initializing D3D11. It was as simple as declaring pointers then calling functions in standard C++ fashion:
ID3D11Device *pDevice;
Now with 11.1 there is a whole host of new things like ComPtr:
ComPtr<ID3D11Device1> pDevice;
And one does not create D3D11.1 devices, they convert them from D3D11 using ComPtr's As() function, hence making ComPtr a mandatory requirement.
Assuming DX11.1 development is possible for desktop, is there a way to use the traditional initialization without this conversion nonsense?
If there is no way around it, how does one initialize D3D11.1 for the desktop?
Thank you.
Upvotes: 2
Views: 2896
Reputation: 34418
I think you do need to convert it from a DirectX11 object, yes, but you can do this without using the ComObj<>
helper wrapper using the normal COM QueryInterface
e.g.:
ID3D11Device* device = NULL;
D3D_FEATURE_LEVEL level;
ID3D11DeviceContext* context = NULL;
HRESULT hr = D3D11CreateDevice(NULL,
D3D_DRIVER_TYPE::D3D_DRIVER_TYPE_NULL,
NULL,
0,
NULL,
0,
D3D11_SDK_VERSION,
&device,
&level,
&context);
if (SUCCEEDED(hr))
{
ID3D11Device1* device1 = NULL;
// This is equivalent to the ComPtr<>.As<>()
hr = device->QueryInterface(IID_ID3D11Device1, (void**)&device1);
if (SUCCEEDED(hr) && (device1 != NULL))
{
// device1 now ready
// when finished with device1
// (this would be handled automatically by the ComPtr<> destructor)
device1->Release();
}
device->Release();
context->Release();
}
Alternatively you can use the helper wrapper but then take the pointer from it
ID3D11Device1* pDevice1 = device.As<ID3D11Device1>().Detach();
if (pDevice1 != NULL)
{
// use as before
// you'll then need to release manually since we detached the reference
pDevice1->Release();
}
but it may be easier just to use the helper wrappers in any case.
My answer was largely about the COM reference count handling and my code fragment was based on a sample from MSDN I think. However an anonymous user points out that it's not a great practical example of creating one of these objects:
Creating a D3D11.1 device requires a feature level array to be specified. See the MSDN page on D3D11CreateDevice. Also, D3D_DRIVER_TYPE_NULL is a reference device without rendering capability, and unlikely to be what the original poster wants.
He suggests you add feature level data and pass that into the D3D11CreateDevice
call along with
D3D_FEATURE_LEVEL pFeatureLevels[]={D3D_FEATURE_LEVEL_11_1};
UINT nFeatureLevels=ARRAY_SIZE(pFeatureLevels);
HRESULT hr = D3D11CreateDevice(NULL,
D3D_DRIVER_TYPE::D3D_DRIVER_TYPE_HARDWARE, /* was NONE in example I copied */
NULL,
0,
pFeatureLevels, /* was NULL */
nFeatureLevels, /* was 0 */
D3D11_SDK_VERSION,
etc. Hope that's useful - I don't know the API well enough to comment really.
Upvotes: 4