Down Casting ID3D11Texture2D

你。 提交于 2019-12-11 11:37:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!