问题
According to the documentation http://msdn.microsoft.com/en-us/library/windows/desktop/ff476635(v=vs.85).aspx
ID3D11Texture2D inherits from the ID3D11Resource.
I tried the following but it gives a std:non-rtti exception.
ID3D11Texture2D *tex2d = dynamic_cast<ID3D11Texture2D*>(resource);
回答1:
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.
回答2:
Its unneccessary to Use Queryinterface if you know what type of Resource it is.
ID3D11Texture2D* ptx = NULL;
resource->GetResource(reinterpret_cast<ID3D11Resource**>(&ptx));
Though if the rviews contains a varity of types Query i needed.
来源:https://stackoverflow.com/questions/25493929/down-casting-id3d11texture2d