Fixed crash and lost pixel data when recovering from a lost device situation (e.g. alt-tab from fullscreen)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
diff --git a/src/render/direct3d/SDL_render_d3d.c b/src/render/direct3d/SDL_render_d3d.c
index 896af23..0c35c39 100644
--- a/src/render/direct3d/SDL_render_d3d.c
+++ b/src/render/direct3d/SDL_render_d3d.c
@@ -190,6 +190,7 @@ typedef struct
typedef struct
{
+ SDL_bool dirty;
IDirect3DTexture9 *texture;
IDirect3DTexture9 *staging;
} D3D_TextureRep;
@@ -652,7 +653,7 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
}
/* Store the default render target */
- IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget );
+ IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget);
data->currentRenderTarget = NULL;
/* Set up parameters for rendering */
@@ -818,6 +819,8 @@ D3D_CreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, DWORD us
{
HRESULT result;
+ texture->dirty = SDL_FALSE;
+
result = IDirect3DDevice9_CreateTexture(device, w, h, 1, usage,
PixelFormatToD3DFMT(format),
D3DPOOL_DEFAULT, &texture->texture, NULL);
@@ -837,30 +840,47 @@ D3D_CreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, DWORD us
}
static int
-D3D_RecreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, Uint32 format, int w, int h)
+D3D_BindTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, DWORD sampler)
{
HRESULT result;
- if (!texture->texture) {
- return 0;
- }
+ if (texture->dirty && texture->staging) {
+ if (!texture->texture) {
+ D3DSURFACE_DESC desc;
+ result = IDirect3DTexture9_GetLevelDesc(texture->staging, 0, &desc);
+ if (FAILED(result)) {
+ return D3D_SetError("GetLevelDesc", result);
+ }
- IDirect3DTexture9_Release(texture->texture);
- result = IDirect3DDevice9_CreateTexture(device, w, h, 1, 0,
- PixelFormatToD3DFMT(format),
- D3DPOOL_DEFAULT, &texture->texture, NULL);
- if (FAILED(result)) {
- return D3D_SetError("CreateTexture(D3DPOOL_DEFAULT)", result);
- }
+ result = IDirect3DDevice9_CreateTexture(device, desc.Width, desc.Height, 1, 0,
+ desc.Format, D3DPOOL_DEFAULT, &texture->texture, NULL);
+ if (FAILED(result)) {
+ return D3D_SetError("CreateTexture(D3DPOOL_DEFAULT)", result);
+ }
+ }
- result = IDirect3DTexture9_AddDirtyRect(texture->staging, NULL);
- if (FAILED(result)) {
- return D3D_SetError("AddDirtyRect()", result);
+ result = IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)texture->staging, (IDirect3DBaseTexture9 *)texture->texture);
+ if (FAILED(result)) {
+ return D3D_SetError("UpdateTexture()", result);
+ }
+ texture->dirty = SDL_FALSE;
}
- result = IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)texture->staging, (IDirect3DBaseTexture9 *)texture->texture);
+ result = IDirect3DDevice9_SetTexture(device, sampler, (IDirect3DBaseTexture9 *)texture->texture);
if (FAILED(result)) {
- return D3D_SetError("UpdateTexture()", result);
+ return D3D_SetError("SetTexture()", result);
+ }
+ return 0;
+}
+
+static int
+D3D_RecreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, Uint32 format, int w, int h)
+{
+ if (texture->texture) {
+ IDirect3DTexture9_Release(texture->texture);
+ texture->texture = NULL;
}
+ IDirect3DTexture9_AddDirtyRect(texture->staging, NULL);
+ texture->dirty = SDL_TRUE;
return 0;
}
@@ -901,8 +921,11 @@ D3D_UpdateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, Uint32 f
dst += locked.Pitch;
}
}
- IDirect3DTexture9_UnlockRect(texture->staging, 0);
- IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)texture->staging, (IDirect3DBaseTexture9 *)texture->texture);
+ result = IDirect3DTexture9_UnlockRect(texture->staging, 0);
+ if (FAILED(result)) {
+ return D3D_SetError("UnlockRect()", result);
+ }
+ texture->dirty = SDL_TRUE;
return 0;
}
@@ -1106,8 +1129,8 @@ D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
D3D_UpdateTexture(renderer, texture, rect, pixels, texturedata->pitch);
} else {
IDirect3DTexture9_UnlockRect(texturedata->texture.staging, 0);
- IDirect3DDevice9_UpdateTexture(data->device, (IDirect3DBaseTexture9 *)texturedata->texture.staging, (IDirect3DBaseTexture9 *)texturedata->texture.texture);
- }
+ texturedata->texture.dirty = SDL_TRUE;
+ }
}
static int
@@ -1580,11 +1603,8 @@ D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
D3D_UpdateTextureScaleMode(data, texturedata, 0);
- result =
- IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
- texturedata->texture.texture);
- if (FAILED(result)) {
- return D3D_SetError("SetTexture()", result);
+ if (D3D_BindTextureRep(data->device, &texturedata->texture, 0) < 0) {
+ return -1;
}
if (texturedata->yuv) {
@@ -1593,18 +1613,11 @@ D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
D3D_UpdateTextureScaleMode(data, texturedata, 1);
D3D_UpdateTextureScaleMode(data, texturedata, 2);
- result =
- IDirect3DDevice9_SetTexture(data->device, 1, (IDirect3DBaseTexture9 *)
- texturedata->utexture.texture);
- if (FAILED(result)) {
- return D3D_SetError("SetTexture()", result);
+ if (D3D_BindTextureRep(data->device, &texturedata->utexture, 1) < 0) {
+ return -1;
}
-
- result =
- IDirect3DDevice9_SetTexture(data->device, 2, (IDirect3DBaseTexture9 *)
- texturedata->vtexture.texture);
- if (FAILED(result)) {
- return D3D_SetError("SetTexture()", result);
+ if (D3D_BindTextureRep(data->device, &texturedata->vtexture, 2) < 0) {
+ return -1;
}
}
@@ -1718,16 +1731,13 @@ D3D_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
modelMatrix = MatrixMultiply(
MatrixRotationZ((float)(M_PI * (float) angle / 180.0f)),
MatrixTranslation(dstrect->x + center->x, dstrect->y + center->y, 0)
- );
+);
IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, (D3DMATRIX*)&modelMatrix);
D3D_UpdateTextureScaleMode(data, texturedata, 0);
- result =
- IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
- texturedata->texture.texture);
- if (FAILED(result)) {
- return D3D_SetError("SetTexture()", result);
+ if (D3D_BindTextureRep(data->device, &texturedata->texture, 0) < 0) {
+ return -1;
}
if (texturedata->yuv) {
@@ -1735,19 +1745,12 @@ D3D_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
D3D_UpdateTextureScaleMode(data, texturedata, 1);
D3D_UpdateTextureScaleMode(data, texturedata, 2);
-
- result =
- IDirect3DDevice9_SetTexture(data->device, 1, (IDirect3DBaseTexture9 *)
- texturedata->utexture.texture);
- if (FAILED(result)) {
- return D3D_SetError("SetTexture()", result);
+
+ if (D3D_BindTextureRep(data->device, &texturedata->utexture, 1) < 0) {
+ return -1;
}
-
- result =
- IDirect3DDevice9_SetTexture(data->device, 2, (IDirect3DBaseTexture9 *)
- texturedata->vtexture.texture);
- if (FAILED(result)) {
- return D3D_SetError("SetTexture()", result);
+ if (D3D_BindTextureRep(data->device, &texturedata->vtexture, 2) < 0) {
+ return -1;
}
}