尝试启动并运行 DX11 项目。为渲染的基础知识启动并运行大部分所需的对象,但是当我调用 OMSetRenderTargets 或 IASetVertexBuffers 时,我为这些函数传递的对象在函数调用后自行为 null。
我所有的 ID3D11 对象都是使用 ComPtr 制作的,我真的不知道会发生这种情况。有什么想法吗?
我已经尝试将它们变成普通的旧指针,并且在将它们传递给所述函数时,它们不再停止自行清零。但我真的不想要那样,因为我正试图让它们都成为 ComPtr
最佳答案
您遇到的问题 ComPtr非常简单:您正在使用 operator&
,这与调用 ReleaseAndGetAddressOf
相同。
在典型的 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);
参见 this wiki有关 ComPtr 的一些一般使用建议。
关于c++ - 在调用某些设备上下文函数时,我的一些 D3D11 对象将自己作为 ComPtr 清空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57780075/