Sign in to follow this  
Such1

DirectX 11 Render-To-Texture not working

Recommended Posts

Hey, guys.I am trying to render to a texture, just for practice and future use.
But it seems that the OMSetRenderTargets is not working.
When I render the scene instead of drawing into the texture I am drawing to the backbuffer.

I create a Object CTexture11 to be the target, and save it on the first frame.

This is the part of CTexture11 the I use to create the target Texture.
[CODE]
void CTexture11::setAsTarget(ID3D11DeviceContext* deviceContext, ID3D11DepthStencilView* depthStencilView)
{
deviceContext->OMSetRenderTargets(1, &m_renderTargetView, depthStencilView);
}
bool CTexture11::create(ID3D11Device* device, int height, int width)
{
D3D11_TEXTURE2D_DESC textureDesc;
HRESULT result;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;

// Initialize the render target texture description.
ZeroMemory(&textureDesc, sizeof(textureDesc));
// Setup the render target texture description.
textureDesc.Width = width;
textureDesc.Height = height;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
textureDesc.SampleDesc.Count = 1;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
// Create the render target texture.
result = device->CreateTexture2D(&textureDesc, NULL, &m_renderTargetTexture);
if(FAILED(result))
{
return false;
}
// Setup the description of the render target view.
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;
// Create the render target view.
result = device->CreateRenderTargetView(m_renderTargetTexture, &renderTargetViewDesc, &m_renderTargetView);
if(FAILED(result))
{
return false;
}
// Setup the description of the shader resource view.
shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
// Create the shader resource view.
result = device->CreateShaderResourceView(m_renderTargetTexture, &shaderResourceViewDesc, &m_texture);
if(FAILED(result))
{
return false;
}
return true;
}
[/CODE]

m_camera1 is the target Texture in this case.
[CODE]
m_texture.load("Wood1.PNG", m_device);
m_camera1.create(m_device, m_height, m_width);
m_camera1.setAsTarget(m_deviceContext, m_zBuffer);
[/CODE]

My render function:
[CODE]
m_deviceContext->ClearRenderTargetView(m_backBuffer, m_backgroundColor);
m_deviceContext->ClearDepthStencilView(m_zBuffer, D3D11_CLEAR_DEPTH, 1.0f, 0);
m_deviceContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
m_camera1.setAsTarget(m_deviceContext, m_zBuffer);
m_camera1.clearRenderTarget(m_deviceContext);
m_deviceContext->ClearDepthStencilView(m_zBuffer, D3D11_CLEAR_DEPTH, 1.0f, 0);
m_texture.set(0, m_deviceContext);
renderScene();
static bool n = false;
if(!n)
{
m_camera1.save("Test.jpg", m_deviceContext);
n = true;
}
m_deviceContext->OMSetRenderTargets(1, &m_backBuffer, m_zBuffer);
m_deviceContext->ClearDepthStencilView(m_zBuffer, D3D11_CLEAR_DEPTH, 1.0f, 0);
m_camera1.set(0, m_deviceContext);
renderScene();
m_swapChain->Present(0, 0);
[/CODE]

I am sure that everything else is working because I can see the scene, but nothing is rendered to the texture.
And if I omit the second renderScene it looks like that instead of drawing into the texture I am drawing always into the m_backBuffer

Share this post


Link to post
Share on other sites
Ok, I fixed it, but I don't know why it did.
When I create the depthBuffer I changed the format to DXGI_FORMAT_D24_UNORM_S8_UINT and it worked.
Could anyone tell me why that cause my problem?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this