Creating A Render Target In DirectX11

This is the code to create a render target which can be bound for writing and reading in a texture.

//D3D Objects To Create Into
ID3D11Texture2D* _Texture2D = NULL;
ID3D11RenderTargetView*	_RenderTargetView = NULL;
ID3D11ShaderResourceView* _ShaderResourceView = NULL;

//D3D Device
ID3D11Device* Device = _directX->GetDevice();

D3D11_TEXTURE2D_DESC bufferDesc;
bufferDesc.ArraySize = 1;
bufferDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.Format = format;
bufferDesc.Height = height;
bufferDesc.MipLevels = 1;
bufferDesc.MiscFlags = 0;
bufferDesc.SampleDesc = sampleDesc;
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.Width = width;
HRESULT hr = Device->CreateTexture2D(&bufferDesc, 0, &_Texture2D);

//Creating a view of the texture to be used when binding it as a render target
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
renderTargetViewDesc.Format = format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;
hr = Device->CreateRenderTargetView(_Texture2D, 0, &_RenderTargetView);
if (hr != S_OK)
{
	return false;
}

//Creating a view of the texture to be used when binding it on a shader to sample
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
shaderResourceViewDesc.Format = format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
hr = Device->CreateShaderResourceView(_Texture2D, &shaderResourceViewDesc, &_ShaderResourceView);
if (hr != S_OK)
{
	return false;
}