c++directxdirectx-11direct3ddirect3d11

When updating a buffer bound to a Direct3D swapchain using CopyResource(), does the corresponding main render target view need to be set?


I have two textures both of which are assigned to separate RTVs:

I am rendering my scene to texture_extra, the contents of which I am modifying in some way (e.g. change some texels' values). I then proceed with CopyResource(texture_rgb, texture_extra) to copy the contents in my empty texture_rgb. The swapchain then presents the result.

Is the copying of a texture's content into another one's depending on whether OMSetRenderTargets(...) was called for the target RTV before the copying was triggered or not?

Pseudo code (render frame function):

// Switch to main RTV
device_context->OMSetRenderTargets(1, &rtv, dsv);
// Clear buffers
device_context->ClearRenderTargetView(rtv, color);
device_context->ClearDepthStencilView(dsv, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

// Switch to extra RTV
device_context->OMSetRenderTargets(1, &rtv_extra, dsv); // DSV is the depth stencil view used by all RTVs
// Clear buffers
device_context->ClearRenderTargetView(rtv_extra, color);
device_context->ClearDepthStencilView(dsv, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
// Set constant buffer and shaders
...
// Draw
device_context->DrawIndexed(36, 0, 0);
// Process rendered scene further
...

// Copy result to main <--------------- DO I NEED TO SWITCH TO MAIN RTV HERE?
device_context->CopyResource(texture_rgb, texture_extra);

// Switch back to main RTV
device_context->OMSetRenderTargets(1, &rtv, dsv);
// Present
swapchain->Present(1, 0);

Solution

  • CopyResource is not dependent on where a resource is assigned, so the resource does not need to be assigned to rtv in order to perform the copy.

    Also if you don't draw on the swapchain but only blit using copy resource, the call to :

    device_context->CopyResource(texture_rgb, texture_extra);
    
    // This is not necessary unless you still want to draw something on the swapchain
    device_context->OMSetRenderTargets(1, &rtv, dsv);
    // Present
    swapchain->Present(1, 0);