我的一些 D3D11 对象在调用某些设备上下文函数时将自身作为 ComPtrs 为空



正在尝试启动并运行 DX11 项目。为渲染的基础知识启动并运行了大多数所需的对象,但是当我调用 OMSetRenderTargets 或 IASetVertexBuffers 时,我为这些函数传递的对象在函数调用后本身为空。

我所有的 ID3D11 对象都是使用 ComPtrs 制作的,我真的不知道会发生这种情况。有什么想法吗?

我尝试将它们转换为普通的旧指针,当将它们传递给所述函数时,它们不再停止清空。但我真的不想这样,因为我试图让它们都成为ComPtrs

您使用ComPtr遇到的问题非常简单:您正在使用与调用ReleaseAndGetAddressOf相同的operator&

在典型的 COM 用法中,operator&仅在创建新实例时使用,因此映射有意义:

// wicFactory can be either a IWICFactory* or a Microsoft::WRL::ComPtr<IWICFactory>
// and the following code will work.
HRESULT hr = CoCreateInstance(
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_PPV_ARG(&wicFactory));

但是,在 Direct3D 中,有许多函数将指针到 COM 指针作为不是对象工厂的输入参数,例如您在问题中列出的那些。在这种情况下,您应该使用Get方法或GetAddressOf可能带有临时变量:

context->OMSetRenderTargets(1, renderTargetView.GetAddressOf(),
depthStencilView.Get());

-或-

auto rt = renderTargetView.Get();
context->OMSetRenderTargets(1, &rt, depthStencilView.Get());

在需要 Comptr 中的 COM 指针数组的情况下:

ID3D11SamplerState* samplers[] = { sampler1.Get(), sampler2.Get() };
context->PSSetSamplers(0, _countof(samplers), samplers);

请参阅此维基以获取 Comptr 的一些一般使用建议。

最新更新