I would like to create a render target view from a texture, for use in multiple target rendering. I am currently able to create a render target view for the back buffer - all of that works nicely. Further, I am able to create the texture. However, when I try to build the view from it, I get an error.
First, here's the code:
D3D11_TEXTURE2D_DESC textureDesc;
ZeroMemory(&textureDesc, sizeof(textureDesc));
textureDesc.ArraySize = 1;
textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
textureDesc.Format = DXGI_FORMAT_R32_FLOAT;
textureDesc.Height = m_renderTargetSize.Height;
textureDesc.Width = m_renderTargetSize.Width;
textureDesc.MipLevels = 1;
textureDesc.MiscFlags = 0;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
ComPtr<ID3D11Texture2D> texture;
DX::ThrowIfFailed(
m_d3dDevice->CreateTexture2D(
&textureDesc,
nullptr,
&texture
)
);
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDescription;
ZeroMemory(&renderTargetViewDescription, sizeof(renderTargetViewDescription));
renderTargetViewDescription.Format = textureDesc.Format;
DX::ThrowIfFailed(
m_d3dDevice->CreateRenderTargetView(
texture,
&renderTargetViewDescription,
&m_renderTargetView[1]
)
);
I am getting the following error from the compiler on the line with the call to CreateRenderTargetView
:
Error: no suitable conversion function from "Microsoft::WRL::ComPtr" to "ID3D11Resource *" exists.
According to MSDN, ID3D11Texture2D inherits from ID3D11Resource. Do I have to somehow upcast first?
I am working in DirectX 11, and compiling with vc110.
It seems that WRL's ComPtr doesn't support implicit conversion to T* (unlike ATL's CComPtr), so you need to use the Get method:
DX::ThrowIfFailed(
m_d3dDevice->CreateRenderTargetView(
texture.Get(),
&renderTargetViewDescription,
&m_renderTargetView[1]
)
);