Since ID3D11Texture2D
is a COM interface you should use QueryInterface
to get other interfaces the object might support. Something like this:
HRESULT hr = resource->QueryInterface(IID_ID3D11Texture2D, (void **) &tex2d);
if (FAILED(hr)) {
// handle failure here.
}
Note this still can still fail if the object pointed to by resource
doesn't implement the ID3D11Texture2D
interface, that is, it's not a 2D texture resource.
Strictly speaking you should also be using QueryInterface
to "up cast" ID3D11Texture2D
interfaces to ID3D11Resource
interfaces. COM doesn't require that if an object implements a derived interface that it also implement the base interface. However up casting should work with any Direct3D COM interface.