[DX11] Unbinding render target for use in next pass

Started by
7 comments, last by Aqua Costa 12 years, 10 months ago
I'm running a shader that writes to both the back buffer and a render target. I would then like to run a second shader that uses that render target as an input texture. This is working fine in debug mode, but does not work in release mode. I have a feeling that I am not correctly unbinding the render target after the first pass. In DX9 you would do the following, pDevice->SetRenderTarget( 1, NULL ), to unbind your render target. Is there a DX11 equivalent to this?

I am currently doing the following:

// Set the back buffer and the one render target
pDeviceContext->OMSetRenderTargets( 2, rTargets, pDepthStencilView );

// Draw geometry

// Set the back buffer as the only target ( the render target in the above step will be used as an input texture
pDeviceContext->OMSetRenderTargets( 1, &pBackBufferView, pDepthStencilView );

// Do Stuff...the input texture is blank in release mode...okay in debug mode.

Thanks for any advice!
Advertisement
In your case, you have to do this:


ID3D11RenderTargetView* rTargets[2] = { pBackBufferView, NULL };
pDeviceContext->OMSetRenderTargets( 2, rTargets, pDepthStencilView );


Otherwise the second render target will remain bound, since you're specifying that you only want to set the first slot and not the second.

In your case, you have to do this:


ID3D11RenderTargetView* rTargets[2] = { pBackBufferView, NULL };
pDeviceContext->OMSetRenderTargets( 2, rTargets, pDepthStencilView );


Otherwise the second render target will remain bound, since you're specifying that you only want to set the first slot and not the second.


Thank you, I didn't realize that was how it worked.

I'm still having the same issue even with that fix, but at least that is one less thing that is wrong. Are there any other things I need to keep in mind when it comes to using render targets as input textures?

Are there any other things I need to keep in mind when it comes to using render targets as input textures?


Make sure you create the render target with the D3D11_BIND_SHADER_RESOURCE flag :cool:

[quote name='myers80' timestamp='1307739573' post='4821859']
Are there any other things I need to keep in mind when it comes to using render targets as input textures?


Make sure you create the render target with the D3D11_BIND_SHADER_RESOURCE flag :cool:
[/quote]

Yep, I have them set with that flag and D3D11_BIND_RENDER_TARGET. So there is nothing I need to do after running the first shader and before starting the second shader except to call OMSetRenderTargets and make sure my input texture is not included on that list? Maybe I'm chasing up the wrong tree...why does release mode have to be so picky.
Well let's back up a bit here. What does "does not work" mean? Also, are you creating the device with the DEBUG flag? If you do that, you will get warning/error messages in your debug output about runtime issues.

Well let's back up a bit here. What does "does not work" mean? Also, are you creating the device with the DEBUG flag? If you do that, you will get warning/error messages in your debug output about runtime issues.


Yeah, I have the debug flag set and there are no warnings or errors. Basically what I'm trying to do is draw an object to the back buffer and my render target at the same time. I am then attempting to draw my render target to the screen on a textured quad ( so I can see what was drawn to the render target ).

I've been doing some more testing, and in release mode the textured quad does display the render target's back color. But it is only displaying the back color, none of the geometry that I drew to the render target is showing. Here is the basic outline of my code, I can give more detailed code if that would help,

// Clear the back buffer.
pDeviceContext->ClearRenderTargetView( pBackBufferView, clearColor );

// Clear the depth buffer.
pDeviceContext->ClearDepthStencilView( pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0 );

// Set the render targets ( Slot 0 contains the back buffer, slot 1 is my render target texture
pDeviceContext->OMSetRenderTargets( numRenderTargets, rTargets, pDepthStencilView );

// Clear the render targets ( in this case just one )
for( u32 i = 0; i < numRenderTargets; ++i )
pDeviceContext->ClearRenderTargetView( rTargets, clearColor );

// Load vertex and index buffers with geometry

// Draw the geometry using a simple shader that colors the objects one color
setShaderParameters( pDeviceContext, worldViewProj, color );
pDeviceContext->IASetInputLayout( pLayout );
pDeviceContext->VSSetShader( pVertexShader, NULL, 0);
pDeviceContext->PSSetShader( pPixelShader, NULL, 0);
pDeviceContext->DrawIndexed( indexCount, 0, 0 );

/////////////////////////////////////////////////////////////////////////////////////////////////////
// Pixel Shader Code
cbuffer PixelBuffer {
float4 color;
};

struct VS_OUTPUT {
float4 position : SV_POSITION;
};

struct PS_OUTPUT {
float4 color : SV_Target0;
float4 color2 : SV_Target1;
};

PS_OUTPUT ColorPixelShader( VS_OUTPUT input ) {
PS_OUTPUT output = (PS_OUTPUT)0;

output.color = color;
output.color2 = color

return output;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Back to C++

// Set the back buffer view in the first slot and the rest to NULL
pDeviceContext->OMSetRenderTargets( D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, rTargets, pDepthStencilView );

// Load vertex and index buffers with the geometry for a textured quad

// Draw a simple textured quad (in this case the texture is the render target from earlier )
pDeviceContext->PSSetShaderResources(0, 1, &texture);
pDeviceContext->IASetInputLayout( pLayout );
pDeviceContext->VSSetShader( pVertexShader, NULL, 0);
pDeviceContext->PSSetShader( pPixelShader, NULL, 0);
pDeviceContext->PSSetSamplers( 0, 1, &sampleState );
pDeviceContext->DrawIndexed( indexCount, 0, 0 );

pSwapChain->Present(1, 0);

// Release the shader resource
ID3D11ShaderResourceView *const pSRV[1] = {NULL};
pDirect3D11->pDeviceContext->PSSetShaderResources(0, 1, pSRV);
Spent all weekend reorganizing and cleaning up the code, and at some point I fixed the issue! No really sure exactly what was wrong but I'm thinking some uninitialized memory, which has bitten me in release mode more than once. Good lesson for myself, first clean up the code before going bug hunting ;)

Thanks MJP and TiagoCosta for the help.
You should use PIX (or PerfHUD if you have a NVIDIA GPU) when debugging your application... It allows you to see what each render target contains after/before each draw call and debug you shaders... Because you could either be incorrectly rendering to you render target, or incorrectly rendering the quad...

This topic is closed to new replies.

Advertisement