DirectX RenderTargetTexture绑定到RTV和SRV

遵循Rastertek的第22个教程。 我和其他一些在互联网上有一个错误。 尽管遵循了这封信的教程。 但是,这个错误信息的答案已经有点弥漫了。 所以为了将来的参考,如果有人创建这个线程。 让我们一劳永逸地解决这个问题!

debugging时,我们得到这个错误消息:

ID3D11DeviceContext :: OMSetRenderTargets:资源被设置为OM RenderTarget插槽0仍然绑定在input上!

ID3D11DeviceContext :: OMSetRenderTargets [AndUnorderedAccessViews]:强制PS着色器资源插槽0为NULL。

所以,这里是一些代码;

HRESULT Application::RenderToTexture() { HRESULT hr = S_OK; //Set render target to render to the texture inside mRenderTexture //Inside this func we see this: _deviceContext->OMSetRenderTargets(1, &this->mRenderTargetView, _depthStencilView); this->mRenderTexture.SetRenderTarget(this->mDirectX.GetDeviceContext(), this->mDirectX.GetDepthStencilView()); //clear it this->mRenderTexture.ClearRenderTarget(this->mDirectX.GetDeviceContext(), this->mDirectX.GetDepthStencilView(), 0.0f, 0.0f, 1.0f, 1.0f); // Render the scene now and it will draw to the render to texture instead of the back buffer. hr = RenderScene(); //Using the render target if(FAILED(hr)) return hr; // Reset the render target back to the original back buffer and not the render to texture anymore. this->mDirectX.SetBackBufferRenderTarget(); return hr; } 

我们需要看看RenderTextureClass(mRenderTexture)。 它关注于这些成员variables。

 class RenderTextureClass { private: ID3D11Texture2D* mRenderTargetTexture; //This is the root of the issue ID3D11RenderTargetView* mRenderTargetView; ID3D11ShaderResourceView* mShaderResourceView; 

如果我们要了解别人的答案。 他们说,RenderTargetView和ShaderResourceView被同时使用,然后无法正常工作。

初始化函数;

 HRESULT RenderTextureClass::Initialize(ID3D11Device* _device, XMFLOAT2 _textureSize) { HRESULT hr = S_OK; //Setup RenderTargetTexture D3D11_TEXTURE2D_DESC textureDesc; ZeroMemory(&textureDesc, sizeof(textureDesc)); textureDesc.Width = _textureSize.x; textureDesc.Height = _textureSize.y; textureDesc.MipLevels = 1; textureDesc.ArraySize = 1; textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; textureDesc.SampleDesc.Count = 1; textureDesc.SampleDesc.Quality = 0; textureDesc.Usage = D3D11_USAGE_DEFAULT; textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; textureDesc.CPUAccessFlags = 0; textureDesc.MiscFlags = 0; hr = _device->CreateTexture2D(&textureDesc, NULL, &this->mRenderTargetTexture); if(FAILED(hr)) return hr; //Setup RenderTargetView D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc; renderTargetViewDesc.Format = textureDesc.Format; renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; renderTargetViewDesc.Texture2D.MipSlice = 0; hr = _device->CreateRenderTargetView(this->mRenderTargetTexture, &renderTargetViewDesc, &this->mRenderTargetView); if(FAILED(hr)) return hr; //Setup Shader resourceview D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc; shaderResourceViewDesc.Format = textureDesc.Format; shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; shaderResourceViewDesc.Texture2D.MostDetailedMip = 0; shaderResourceViewDesc.Texture2D.MipLevels = 1; hr = _device->CreateShaderResourceView(this->mRenderTargetTexture, &shaderResourceViewDesc, &this->mShaderResourceView); if(FAILED(hr)) return hr; return hr; } 

所以这个问题。 我们如何以最合理的方式纠正这一点,而不会破坏一切?

您不能同时具有与input和输出相同的纹理。 解决scheme是在应用新的绑定之前明确地清除一个绑定。

在DirectX工具包教程进行后期处理 :

 m_d3dContext->OMSetRenderTargets(1, m_rt1RT.GetAddressOf(), nullptr); // Draw using m_sceneSRV m_d3dContext->OMSetRenderTargets(1, m_rt2RT.GetAddressOf(), nullptr); // Draw using m_rt1SRV ID3D11ShaderResourceView* null[] = { nullptr, nullptr }; m_d3dContext->PSSetShaderResources(0, 2, null); m_d3dContext->OMSetRenderTargets(1, m_rt1RT.GetAddressOf(), nullptr); // Draw using m_rt2SRV m_d3dContext->OMSetRenderTargets(1, m_renderTargetView.GetAddressOf(), nullptr); // Draw using m_rt1SRV m_d3dContext->PSSetShaderResources(0, 2, null); 

避免相同的警告。