Fixed bug 2685 - SDL_RenderReadPixels() doesn't work with offscreen targets
authorSam Lantinga <slouken@libsdl.org>
Sun, 17 Aug 2014 14:44:53 -0700
changeset 90857cafa57c23ec
parent 9084 a8cbb653aea4
child 9086 c5e33f9a0d03
Fixed bug 2685 - SDL_RenderReadPixels() doesn't work with offscreen targets

Andreas Falkenhahn

SDL_RenderReadPixels() doesn't seem to work when trying to read pixels from a texture that has been created using SDL_TEXTUREACCESS_TARGET and has been selected as the render target using SDL_SetRenderTarget().

I am attaching a small program that demonstrates the issue. I get the following result here:

READ PIXEL RETURN: 0 --- COLOR CHECK: ff000000

But it should be:

READ PIXEL RETURN: 0 --- COLOR CHECK: ffff0000

Tested with SDL 2.0.3 on Windows 7.
src/render/direct3d/SDL_render_d3d.c
     1.1 --- a/src/render/direct3d/SDL_render_d3d.c	Sun Aug 17 14:34:41 2014 -0700
     1.2 +++ b/src/render/direct3d/SDL_render_d3d.c	Sun Aug 17 14:44:53 2014 -0700
     1.3 @@ -1831,9 +1831,10 @@
     1.4      D3DLOCKED_RECT locked;
     1.5      HRESULT result;
     1.6  
     1.7 -    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
     1.8 -    if (FAILED(result)) {
     1.9 -        return D3D_SetError("GetBackBuffer()", result);
    1.10 +    if (data->currentRenderTarget) {
    1.11 +        backBuffer = data->currentRenderTarget;
    1.12 +    } else {
    1.13 +        backBuffer = data->defaultRenderTarget;
    1.14      }
    1.15  
    1.16      result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
    1.17 @@ -1874,7 +1875,6 @@
    1.18      IDirect3DSurface9_UnlockRect(surface);
    1.19  
    1.20      IDirect3DSurface9_Release(surface);
    1.21 -    IDirect3DSurface9_Release(backBuffer);
    1.22  
    1.23      return 0;
    1.24  }