0
\$\begingroup\$

I want to render the surface base on the the last surface. So I try to copy FrontBuffer to BackBuffer, but it does not work:

//Get the BackBuffer(it will be FrontBuffer after SwapChain swapped the BackBuffer and FrontBuffer)
ID3D11Texture2D* backBufferTexture1 = NULL;
swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferTexture1);
D3D11_MAPPED_SUBRESOURCE mapResource1;
HRESULT result1 = m_pID3DContext->Map(backBufferTexture1, 0, D3D11_MAP_READ, 0, &mapResource1);
assert(result1 == S_OK);

swapChain->Present(1,0); //swap the BackBuffer and FrontBuffer

//Get the BackBuffer
ID3D11Texture2D* backBufferTexture2 = NULL;
swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferTexture2);
D3D11_MAPPED_SUBRESOURCE mapResource2;
HRESULT result2 = m_pID3DContext->Map(backBufferTexture2, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapResource2);
assert(result2 == S_OK);

//Copy the FrontBuffer to the BackBuffer
memcpy(mapResource2.pData, mapResource1.pData, sizeof(mapResource1.pData));
m_pID3DContext->Unmap(backBufferTexture1, 0);
m_pID3DContext->Unmap(backBufferTexture2, 0);

The result1 and result2 both return E_INVALIDARG One or more arguments are invalid. How could I get this to work?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You can't Map a swapchain buffer as they are not necessarily created in CPU-readable memory. You could in theory copy it to a CPU staging resource, but the performance would be terrible.

A better option is to create another render target that you do your drawing on to, then you copy that texture to the backbuffer for Present. Then you can render your next scene on the same render target. The performance here is probably not as good as just rendering the whole scene again, but it really depends on what exactly you are doing.

\$\endgroup\$
0
\$\begingroup\$

I finally found the most easy way to do this work. What I do is just change the SwapChain's SwapEffect to DXGI_SWAP_EFFECT_SEQUENTIAL and don't Clear RenderTargetView before draw to keep the backbuffer as same as last time.

\$\endgroup\$
1
  • \$\begingroup\$ I may be misremembering but I think what happens to a render target when it's not cleared is GPU and driver specific. \$\endgroup\$ Commented Jul 30, 2016 at 11:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .