Fixed bug 5015 - SDL_RenderReadPixels on DirectX 11.1 backend seems to be broken
authorSam Lantinga
Sun, 05 Apr 2020 08:58:47 -0700
changeset 13689c3055b205671
parent 13687 4897f7f06e52
child 13690 699e489c7bb6
Fixed bug 5015 - SDL_RenderReadPixels on DirectX 11.1 backend seems to be broken

Konrad

It appears that I cannot use SDL_RenderReadPixels on a bound framebuffer (SDL_Texture set as render target) as it simply results in gibberish data. However, drawing that framebuffer into the default target (window surface) does render it correctly. Other backends (OpenGL, software, Direct3D) do work fine.

It looks to me like D3D11_RenderReadPixels just gets the general backbuffer and not the current render target and its backbuffer.

Here is the patch which actually fetches the current render target and its underlying ID3D11Resource which is ID3D11Texture2D.
src/render/direct3d11/SDL_render_d3d11.c
     1.1 --- a/src/render/direct3d11/SDL_render_d3d11.c	Sun Apr 05 08:46:59 2020 -0700
     1.2 +++ b/src/render/direct3d11/SDL_render_d3d11.c	Sun Apr 05 08:58:47 2020 -0700
     1.3 @@ -2325,6 +2325,7 @@
     1.4                         Uint32 format, void * pixels, int pitch)
     1.5  {
     1.6      D3D11_RenderData * data = (D3D11_RenderData *) renderer->driverdata;
     1.7 +    ID3D11RenderTargetView *renderTargetView = NULL;
     1.8      ID3D11Texture2D *backBuffer = NULL;
     1.9      ID3D11Texture2D *stagingTexture = NULL;
    1.10      HRESULT result;
    1.11 @@ -2334,14 +2335,15 @@
    1.12      D3D11_BOX srcBox;
    1.13      D3D11_MAPPED_SUBRESOURCE textureMemory;
    1.14  
    1.15 -    /* Retrieve a pointer to the back buffer: */
    1.16 -    result = IDXGISwapChain_GetBuffer(data->swapChain,
    1.17 -        0,
    1.18 -        &SDL_IID_ID3D11Texture2D,
    1.19 -        (void **)&backBuffer
    1.20 -        );
    1.21 -    if (FAILED(result)) {
    1.22 -        WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain1::GetBuffer [get back buffer]"), result);
    1.23 +    ID3D11DeviceContext_OMGetRenderTargets(data->d3dContext, 1, &renderTargetView, NULL);
    1.24 +    if (renderTargetView == NULL) {
    1.25 +        SDL_SetError("%s, ID3D11DeviceContext::OMGetRenderTargets failed", __FUNCTION__);
    1.26 +        goto done;
    1.27 +    }
    1.28 +
    1.29 +    ID3D11View_GetResource(renderTargetView, (ID3D11Resource**)&backBuffer);
    1.30 +    if (backBuffer == NULL) {
    1.31 +        SDL_SetError("%s, ID3D11View::GetResource failed", __FUNCTION__);
    1.32          goto done;
    1.33      }
    1.34