I am working on learning DirectX 12 from some examples but I am having trouble understanding what does the ComPtr.As() method does.
ComPtr<ID3D12Device> device;
ComPtr<ID3D12Device> device2;
// Do Stuff with Device
device.As(&device2); // What does this do?
Where did you find this example code? It looks fishy. This is kind of a nonsense use of
As
. It's equally silly if you expand it to the underlyingQueryInterface
:In fact, your code would be better written as:
Typically you'd use
As
to obtain a new interface from an existing interface. For example with Direct3D 11, you create the device as a Direct3D 11.0 interface and then have toQueryInterface
the 11.1, 11.2, 11.3, and/or 11.4 versions to use them:Another common use with Direct3D 11 is dealing with the debug interfaces which may not be present:
It's also important to not ignore the HRESULT return value that comes back from
As
. You should useSUCCEEDED
orFAILED
macro, or a fast-fail like ThrowIfFailed.See ComPtr.