简体   繁体   中英

DX11 Mapping vertex buffer as WRL::ComPtr

I'm having trouble with mapping vertex buffer. When I do this like so:

ID3D11Buffer* pD3DSingleVertexBuffer;
...
pD3DImmediateContext->Map(pD3DSingleVertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &mappedSubresource);

Everything works, all frames works properly. However, when I do things like that:

Microsoft::WRL::ComPtr<ID3D11Buffer> pD3DSingleVertexBuffer;
...
pD3DImmediateContext->Map(pD3DSingleVertexBuffer.Get(), NULL, D3D11_MAP_WRITE_DISCARD, NULL, &mappedSubresource);

Nothing gets rendered, but application doesn't crash, nor there are any errors.

Do you know what am I doing wrong?

As you discovered, while the classic approach to creating single-element arrays often used in D3D is to just use the address-of operator on the single element, it's also commonly used to pass output paramaters to creation methods (eg CreateDevice(&device) ). Since this is the more common usage, especially in the context of the entire Win32 API surface, ComPtr is designed to safely provide output-parameter semantics, Release() ing the contained object prior to returning its address.

There are two ways around this problem. The first is to actually create an array of objects, eg ID3D11Buffer** ppBuffers[] = { pBuffer.Get() }; . The second is to utilize ComPtr::GetAddressOf which returns the address of the contained object without changing its refcount. In the latter case, be careful not to use this in cases where you really are using the container as an output parameter, otherwise you'll leak the previously held object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM