简体   繁体   English

DX11将顶点缓冲区映射为WRL :: ComPtr

[英]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) ). 正如您所发现的那样,尽管创建D3D中经常使用的单元素数组的经典方法是仅对单个元素使用address-of运算符,但它也通常用于将输出参数传递给创建方法(例如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. 由于这是更常见的用法,尤其是在整个Win32 API表面的上下文中,因此ComPtr旨在安全地提供输出参数语义,在返回其地址之前,先对所包含的对象进行Release()处理。

There are two ways around this problem. 有两种方法可以解决此问题。 The first is to actually create an array of objects, eg ID3D11Buffer** ppBuffers[] = { pBuffer.Get() }; 首先是实际创建对象数组,例如ID3D11Buffer** ppBuffers[] = { pBuffer.Get() }; . The second is to utilize ComPtr::GetAddressOf which returns the address of the contained object without changing its refcount. 第二种是利用ComPtr::GetAddressOf ,它返回包含对象的地址,而不更改其引用计数。 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. 在后一种情况下,请注意不要在确实使用容器作为输出参数的情况下使用此方法,否则会泄漏先前保存的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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