Commit de3d381cb64fbefebd63f80aecb651dbfc82c80a

Sam Lantinga 2014-08-17T14:44:53

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.

diff --git a/src/render/direct3d/SDL_render_d3d.c b/src/render/direct3d/SDL_render_d3d.c
index be88da3..d96522e 100644
--- a/src/render/direct3d/SDL_render_d3d.c
+++ b/src/render/direct3d/SDL_render_d3d.c
@@ -1831,9 +1831,10 @@ D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
     D3DLOCKED_RECT locked;
     HRESULT result;
 
-    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
-    if (FAILED(result)) {
-        return D3D_SetError("GetBackBuffer()", result);
+    if (data->currentRenderTarget) {
+        backBuffer = data->currentRenderTarget;
+    } else {
+        backBuffer = data->defaultRenderTarget;
     }
 
     result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
@@ -1874,7 +1875,6 @@ D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
     IDirect3DSurface9_UnlockRect(surface);
 
     IDirect3DSurface9_Release(surface);
-    IDirect3DSurface9_Release(backBuffer);
 
     return 0;
 }