Sign in to follow this  
myers80

DX11 [DX11] Unbinding render target for use in next pass

Recommended Posts

myers80    194
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!

Share this post


Link to post
Share on other sites
MJP    19786
In your case, you have to do this:

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

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.

Share this post


Link to post
Share on other sites
myers80    194
[quote name='MJP' timestamp='1307739040' post='4821853']
In your case, you have to do this:

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

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.
[/quote]

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?

Share this post


Link to post
Share on other sites
Aqua Costa    3692
[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?
[/quote]

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

Share this post


Link to post
Share on other sites
myers80    194
[quote name='TiagoCosta' timestamp='1307740356' post='4821862']
[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?
[/quote]

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.

Share this post


Link to post
Share on other sites
MJP    19786
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.

Share this post


Link to post
Share on other sites
myers80    194
[quote name='MJP' timestamp='1307748766' post='4821913']
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.
[/quote]

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[i], 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);

Share this post


Link to post
Share on other sites
myers80    194
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.

Share this post


Link to post
Share on other sites
Aqua Costa    3692
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...

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  

  • Similar Content

    • By gomidas
      I am trying to add normal map to my project I have an example of a cube: 
      I have normal in my shader I think. Then I set shader resource view for texture (NOT BUMP)
                  device.ImmediateContext.PixelShader.SetShaderResource(0, textureView);             device.ImmediateContext.Draw(VerticesCount,0); What should I do to set my normal map or how it is done in dx11 generally example c++?
    • By fighting_falcon93
      Imagine that we have a vertex structure that looks like this:
      struct Vertex { XMFLOAT3 position; XMFLOAT4 color; }; The vertex shader looks like this:
      cbuffer MatrixBuffer { matrix world; matrix view; matrix projection; }; struct VertexInput { float4 position : POSITION; float4 color : COLOR; }; struct PixelInput { float4 position : SV_POSITION; float4 color : COLOR; }; PixelInput main(VertexInput input) { PixelInput output; input.position.w = 1.0f; output.position = mul(input.position, world); output.position = mul(output.position, view); output.position = mul(output.position, projection); output.color = input.color; return output; } And the pixel shader looks like this:
      struct PixelInput { float4 position : SV_POSITION; float4 color : COLOR; }; float4 main(PixelInput input) : SV_TARGET { return input.color; } Now let's create a quad consisting of 2 triangles and the vertices A, B, C and D:
      // Vertex A. vertices[0].position = XMFLOAT3(-1.0f, 1.0f, 0.0f); vertices[0].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // Vertex B. vertices[1].position = XMFLOAT3( 1.0f, 1.0f, 0.0f); vertices[1].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // Vertex C. vertices[2].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); vertices[2].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // Vertex D. vertices[3].position = XMFLOAT3( 1.0f, -1.0f, 0.0f); vertices[3].color = XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f); // 1st triangle. indices[0] = 0; // Vertex A. indices[1] = 3; // Vertex D. indices[2] = 2; // Vertex C. // 2nd triangle. indices[3] = 0; // Vertex A. indices[4] = 1; // Vertex B. indices[5] = 3; // Vertex D. This will result in a grey quad as shown in the image below. I've outlined the edges in red color to better illustrate the triangles:

      Now imagine that we’d want our quad to have a different color in vertex A:
      // Vertex A. vertices[0].position = XMFLOAT3(-1.0f, 1.0f, 0.0f); vertices[0].color = XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f);
      That works as expected since there’s now an interpolation between the black color in vertex A and the grey color in vertices B, C and D. Let’s revert the previus changes and instead change the color of vertex C:
      // Vertex C. vertices[2].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); vertices[2].color = XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f);
      As you can see, the interpolation is only done half of the way across the first triangle and not across the entire quad. This is because there's no edge between vertex C and vertex B.
      Which brings us to my question:
      I want the interpolation to go across the entire quad and not only across the triangle. So regardless of which vertex we decide to change the color of, the color interpolation should always go across the entire quad. Is there any efficient way of achieving this without adding more vertices and triangles?
      An illustration of what I'm trying to achieve is shown in the image below:

       
      Background
      This is just a very brief explanation of the problems background in case that would make it easier for you to understand the problems roots and maybe help you with finding a better solution to the problem.
      I'm trying to texture a terrain mesh in DirectX11. It's working, but I'm a bit unsatisfied with the result. When changing the terrain texture of a single vertex, the interpolation with the other vertices results in a hexagon shape instead of a squared shape:

      As the red arrows illustrate, I'd like the texture to be interpolated all the way into the corners of the quads.
    • By -Tau-
      Hello, I'm close to releasing my first game to Steam however, my game keeps failing the review process because it keeps crashing. The problem is that the game doesn't crash on my computer, on my laptop, on our family computer, on fathers laptop and i also gave 3 beta keys to people i know and they said the game hasn't crashed.
      Steam reports that the game doesn't crash on startup but few frames after a level has been started.
      What could cause something like this? I have no way of debugging this as the game works fine on every computer i have.
       
      Game is written in C++, using DirectX 11 and DXUT framework.
    • By haiiry
      I'm trying to get, basically, screenshot (each 1 second, without saving) of Direct3D11 application. Code works fine on my PC(Intel CPU, Radeon GPU) but crashes after few iterations on 2 others (Intel CPU + Intel integrated GPU, Intel CPU + Nvidia GPU).
      void extractBitmap(void* texture) { if (texture) { ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)texture; ID3D11Texture2D* pNewTexture = NULL; D3D11_TEXTURE2D_DESC desc; d3dtex->GetDesc(&desc); desc.BindFlags = 0; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; desc.Usage = D3D11_USAGE_STAGING; desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; HRESULT hRes = D3D11Device->CreateTexture2D(&desc, NULL, &pNewTexture); if (FAILED(hRes)) { printCon(std::string("CreateTexture2D FAILED:" + format_error(hRes)).c_str()); if (hRes == DXGI_ERROR_DEVICE_REMOVED) printCon(std::string("DXGI_ERROR_DEVICE_REMOVED -- " + format_error(D3D11Device->GetDeviceRemovedReason())).c_str()); } else { if (pNewTexture) { D3D11DeviceContext->CopyResource(pNewTexture, d3dtex); // Wokring with texture pNewTexture->Release(); } } } return; } D3D11SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast< void** >(&pBackBuffer)); extractBitmap(pBackBuffer); pBackBuffer->Release(); Crash log:
      CreateTexture2D FAILED:887a0005 DXGI_ERROR_DEVICE_REMOVED -- 887a0020 Once I comment out 
      D3D11DeviceContext->CopyResource(pNewTexture, d3dtex); 
      code works fine on all 3 PC's.
    • By Fluffy10
      Hi i'm new to this forum and was wondering if there are any good places to start learning directX 11. I bought Frank D Luna's book but it's really outdated and the projects won't even compile. I was excited to start learning from this book because it gives detailed explanations on the functions being used as well as the mathematics. Are there any tutorials / courses /books that are up to date which goes over the 3D math and functions in a detailed manner? Or where does anyone here learn directX 11? I've followed some tutorials from this website http://www.directxtutorial.com/LessonList.aspx?listid=11 which did a nice job but it doesn't explain what's happening with the math so I feel like I'm not actually learning, and it only goes up until color blending. Rasteriks tutorials doesn't go over the functions much at all or the math involved either. I'd really appreciate it if anyone can point me in the right direction, I feel really lost. Thank you
  • Popular Now