Commit 49c53fd280b74f287b040a8bc62422f3ce75b008

Sam Lantinga 2014-05-31T11:37:12

Use D3D9Ex when available This hopefully works around crashes in Intel D3D9 support in Windows 8.1.

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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
diff --git a/src/render/direct3d/SDL_render_d3d.c b/src/render/direct3d/SDL_render_d3d.c
index 74dce2b..896af23 100644
--- a/src/render/direct3d/SDL_render_d3d.c
+++ b/src/render/direct3d/SDL_render_d3d.c
@@ -124,6 +124,7 @@ static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
 static void D3D_WindowEvent(SDL_Renderer * renderer,
                             const SDL_WindowEvent *event);
 static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
+static int D3D_RecreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
 static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
                              const SDL_Rect * rect, const void *pixels,
                              int pitch);
@@ -190,12 +191,18 @@ typedef struct
 typedef struct
 {
     IDirect3DTexture9 *texture;
+    IDirect3DTexture9 *staging;
+} D3D_TextureRep;
+
+typedef struct
+{
+    D3D_TextureRep texture;
     D3DTEXTUREFILTERTYPE scaleMode;
 
     /* YV12 texture support */
     SDL_bool yuv;
-    IDirect3DTexture9 *utexture;
-    IDirect3DTexture9 *vtexture;
+    D3D_TextureRep utexture;
+    D3D_TextureRep vtexture;
     Uint8 *pixels;
     int pitch;
     SDL_Rect locked_rect;
@@ -408,6 +415,8 @@ D3D_Reset(SDL_Renderer * renderer)
     for (texture = renderer->textures; texture; texture = texture->next) {
         if (texture->access == SDL_TEXTUREACCESS_TARGET) {
             D3D_DestroyTexture(renderer, texture);
+        } else {
+            D3D_RecreateTexture(renderer, texture);
         }
     }
 
@@ -805,74 +814,58 @@ GetScaleQuality(void)
 }
 
 static int
-D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
+D3D_CreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, DWORD usage, Uint32 format, int w, int h)
 {
-    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
-    D3D_TextureData *data;
-    D3DPOOL pool;
-    DWORD usage;
     HRESULT result;
 
-    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
-    if (!data) {
-        return SDL_OutOfMemory();
+    result = IDirect3DDevice9_CreateTexture(device, w, h, 1, usage,
+        PixelFormatToD3DFMT(format),
+        D3DPOOL_DEFAULT, &texture->texture, NULL);
+    if (FAILED(result)) {
+        return D3D_SetError("CreateTexture(D3DPOOL_DEFAULT)", result);
     }
-    data->scaleMode = GetScaleQuality();
 
-    texture->driverdata = data;
+    if (usage != D3DUSAGE_RENDERTARGET) {
+        result = IDirect3DDevice9_CreateTexture(device, w, h, 1, usage,
+            PixelFormatToD3DFMT(format),
+            D3DPOOL_SYSTEMMEM, &texture->staging, NULL);
+        if (FAILED(result)) {
+            return D3D_SetError("CreateTexture(D3DPOOL_SYSTEMMEM)", result);
+        }
+    }
+    return 0;
+}
 
-#ifdef USE_DYNAMIC_TEXTURE
-    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
-        pool = D3DPOOL_DEFAULT;
-        usage = D3DUSAGE_DYNAMIC;
-    } else
-#endif
-    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
-        /* D3DPOOL_MANAGED does not work with D3DUSAGE_RENDERTARGET */
-        pool = D3DPOOL_DEFAULT;
-        usage = D3DUSAGE_RENDERTARGET;
-    } else {
-        pool = D3DPOOL_MANAGED;
-        usage = 0;
+static int
+D3D_RecreateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, Uint32 format, int w, int h)
+{
+    HRESULT result;
+
+    if (!texture->texture) {
+        return 0;
     }
 
-    result =
-        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
-                                       texture->h, 1, usage,
-                                       PixelFormatToD3DFMT(texture->format),
-                                       pool, &data->texture, NULL);
+    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()", result);
+        return D3D_SetError("CreateTexture(D3DPOOL_DEFAULT)", result);
     }
 
-    if (texture->format == SDL_PIXELFORMAT_YV12 ||
-        texture->format == SDL_PIXELFORMAT_IYUV) {
-        data->yuv = SDL_TRUE;
-
-        result =
-            IDirect3DDevice9_CreateTexture(renderdata->device, texture->w / 2,
-                                           texture->h / 2, 1, usage,
-                                           PixelFormatToD3DFMT(texture->format),
-                                           pool, &data->utexture, NULL);
-        if (FAILED(result)) {
-            return D3D_SetError("CreateTexture()", result);
-        }
-
-        result =
-            IDirect3DDevice9_CreateTexture(renderdata->device, texture->w / 2,
-                                           texture->h / 2, 1, usage,
-                                           PixelFormatToD3DFMT(texture->format),
-                                           pool, &data->vtexture, NULL);
-        if (FAILED(result)) {
-            return D3D_SetError("CreateTexture()", 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);
     }
-
     return 0;
 }
 
 static int
-D3D_UpdateTextureInternal(IDirect3DTexture9 *texture, Uint32 format, SDL_bool full_texture, int x, int y, int w, int h, const void *pixels, int pitch)
+D3D_UpdateTextureRep(IDirect3DDevice9 *device, D3D_TextureRep *texture, Uint32 format, int x, int y, int w, int h, const void *pixels, int pitch)
 {
     RECT d3drect;
     D3DLOCKED_RECT locked;
@@ -881,16 +874,11 @@ D3D_UpdateTextureInternal(IDirect3DTexture9 *texture, Uint32 format, SDL_bool fu
     int row, length;
     HRESULT result;
 
-    if (full_texture) {
-        result = IDirect3DTexture9_LockRect(texture, 0, &locked, NULL, D3DLOCK_DISCARD);
-    } else {
-        d3drect.left = x;
-        d3drect.right = x + w;
-        d3drect.top = y;
-        d3drect.bottom = y + h;
-        result = IDirect3DTexture9_LockRect(texture, 0, &locked, &d3drect, 0);
-    }
-
+    d3drect.left = x;
+    d3drect.right = x + w;
+    d3drect.top = y;
+    d3drect.bottom = y + h;
+    result = IDirect3DTexture9_LockRect(texture->staging, 0, &locked, &d3drect, 0);
     if (FAILED(result)) {
         return D3D_SetError("LockRect()", result);
     }
@@ -913,46 +901,114 @@ D3D_UpdateTextureInternal(IDirect3DTexture9 *texture, Uint32 format, SDL_bool fu
             dst += locked.Pitch;
         }
     }
-    IDirect3DTexture9_UnlockRect(texture, 0);
+    IDirect3DTexture9_UnlockRect(texture->staging, 0);
+    IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)texture->staging, (IDirect3DBaseTexture9 *)texture->texture);
 
     return 0;
 }
 
+static void
+D3D_DestroyTextureRep(D3D_TextureRep *texture)
+{
+    if (texture->texture) {
+        IDirect3DTexture9_Release(texture->texture);
+        texture->texture = NULL;
+    }
+    if (texture->staging) {
+        IDirect3DTexture9_Release(texture->staging);
+        texture->staging = NULL;
+    }
+}
+
 static int
-D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
-                  const SDL_Rect * rect, const void *pixels, int pitch)
+D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
 {
-    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
-    SDL_bool full_texture = SDL_FALSE;
+    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
+    D3D_TextureData *texturedata;
+    DWORD usage;
 
-#ifdef USE_DYNAMIC_TEXTURE
-    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
-        rect->x == 0 && rect->y == 0 &&
-        rect->w == texture->w && rect->h == texture->h) {
-        full_texture = SDL_TRUE;
+    texturedata = (D3D_TextureData *) SDL_calloc(1, sizeof(*texturedata));
+    if (!texturedata) {
+        return SDL_OutOfMemory();
     }
-#endif
+    texturedata->scaleMode = GetScaleQuality();
 
-    if (!data) {
+    texture->driverdata = texturedata;
+
+    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
+        usage = D3DUSAGE_RENDERTARGET;
+    } else {
+        usage = 0;
+    }
+
+    if (D3D_CreateTextureRep(data->device, &texturedata->texture, usage, texture->format, texture->w, texture->h) < 0) {
+        return -1;
+    }
+
+    if (texture->format == SDL_PIXELFORMAT_YV12 ||
+        texture->format == SDL_PIXELFORMAT_IYUV) {
+        texturedata->yuv = SDL_TRUE;
+
+        if (D3D_CreateTextureRep(data->device, &texturedata->utexture, usage, texture->format, texture->w / 2, texture->h / 2) < 0) {
+            return -1;
+        }
+
+        if (D3D_CreateTextureRep(data->device, &texturedata->vtexture, usage, texture->format, texture->w / 2, texture->h / 2) < 0) {
+            return -1;
+        }
+    }
+    return 0;
+}
+
+static int
+D3D_RecreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
+{
+    D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
+    D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
+
+    if (D3D_RecreateTextureRep(data->device, &texturedata->texture, texture->format, texture->w, texture->h) < 0) {
+        return -1;
+    }
+
+    if (texturedata->yuv) {
+        if (D3D_RecreateTextureRep(data->device, &texturedata->utexture, texture->format, texture->w / 2, texture->h / 2) < 0) {
+            return -1;
+        }
+
+        if (D3D_RecreateTextureRep(data->device, &texturedata->vtexture, texture->format, texture->w / 2, texture->h / 2) < 0) {
+            return -1;
+        }
+    }
+    return 0;
+}
+
+static int
+D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
+                  const SDL_Rect * rect, const void *pixels, int pitch)
+{
+    D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
+    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
+
+    if (!texturedata) {
         SDL_SetError("Texture is not currently available");
         return -1;
     }
 
-    if (D3D_UpdateTextureInternal(data->texture, texture->format, full_texture, rect->x, rect->y, rect->w, rect->h, pixels, pitch) < 0) {
+    if (D3D_UpdateTextureRep(data->device, &texturedata->texture, texture->format, rect->x, rect->y, rect->w, rect->h, pixels, pitch) < 0) {
         return -1;
     }
 
-    if (data->yuv) {
+    if (texturedata->yuv) {
         /* Skip to the correct offset into the next texture */
         pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
 
-        if (D3D_UpdateTextureInternal(texture->format == SDL_PIXELFORMAT_YV12 ? data->vtexture : data->utexture, texture->format, full_texture, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, pixels, pitch / 2) < 0) {
+        if (D3D_UpdateTextureRep(data->device, texture->format == SDL_PIXELFORMAT_YV12 ? &texturedata->vtexture : &texturedata->utexture, texture->format, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, pixels, pitch / 2) < 0) {
             return -1;
         }
 
         /* Skip to the correct offset into the next texture */
         pixels = (const void*)((const Uint8*)pixels + (rect->h * pitch)/4);
-        if (D3D_UpdateTextureInternal(texture->format == SDL_PIXELFORMAT_YV12 ? data->utexture : data->vtexture, texture->format, full_texture, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, pixels, pitch / 2) < 0) {
+        if (D3D_UpdateTextureRep(data->device, texture->format == SDL_PIXELFORMAT_YV12 ? &texturedata->utexture : &texturedata->vtexture, texture->format, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, pixels, pitch / 2) < 0) {
             return -1;
         }
     }
@@ -966,29 +1022,21 @@ D3D_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
                      const Uint8 *Uplane, int Upitch,
                      const Uint8 *Vplane, int Vpitch)
 {
-    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
-    SDL_bool full_texture = SDL_FALSE;
-
-#ifdef USE_DYNAMIC_TEXTURE
-    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
-        rect->x == 0 && rect->y == 0 &&
-        rect->w == texture->w && rect->h == texture->h) {
-            full_texture = SDL_TRUE;
-    }
-#endif
+    D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
+    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
 
-    if (!data) {
+    if (!texturedata) {
         SDL_SetError("Texture is not currently available");
         return -1;
     }
 
-    if (D3D_UpdateTextureInternal(data->texture, texture->format, full_texture, rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch) < 0) {
+    if (D3D_UpdateTextureRep(data->device, &texturedata->texture, texture->format, rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch) < 0) {
         return -1;
     }
-    if (D3D_UpdateTextureInternal(data->utexture, texture->format, full_texture, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Uplane, Upitch) < 0) {
+    if (D3D_UpdateTextureRep(data->device, &texturedata->utexture, texture->format, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Uplane, Upitch) < 0) {
         return -1;
     }
-    if (D3D_UpdateTextureInternal(data->vtexture, texture->format, full_texture, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Vplane, Vpitch) < 0) {
+    if (D3D_UpdateTextureRep(data->device, &texturedata->vtexture, texture->format, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Vplane, Vpitch) < 0) {
         return -1;
     }
     return 0;
@@ -998,37 +1046,39 @@ static int
 D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
                 const SDL_Rect * rect, void **pixels, int *pitch)
 {
-    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
-    RECT d3drect;
-    D3DLOCKED_RECT locked;
-    HRESULT result;
+    D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
 
-    if (!data) {
+    if (!texturedata) {
         SDL_SetError("Texture is not currently available");
         return -1;
     }
 
-    if (data->yuv) {
+    texturedata->locked_rect = *rect;
+
+    if (texturedata->yuv) {
         /* It's more efficient to upload directly... */
-        if (!data->pixels) {
-            data->pitch = texture->w;
-            data->pixels = (Uint8 *)SDL_malloc((texture->h * data->pitch * 3) / 2);
-            if (!data->pixels) {
+        if (!texturedata->pixels) {
+            texturedata->pitch = texture->w;
+            texturedata->pixels = (Uint8 *)SDL_malloc((texture->h * texturedata->pitch * 3) / 2);
+            if (!texturedata->pixels) {
                 return SDL_OutOfMemory();
             }
         }
-        data->locked_rect = *rect;
         *pixels =
-            (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
+            (void *) ((Uint8 *) texturedata->pixels + rect->y * texturedata->pitch +
                       rect->x * SDL_BYTESPERPIXEL(texture->format));
-        *pitch = data->pitch;
+        *pitch = texturedata->pitch;
     } else {
+        RECT d3drect;
+        D3DLOCKED_RECT locked;
+        HRESULT result;
+
         d3drect.left = rect->x;
         d3drect.right = rect->x + rect->w;
         d3drect.top = rect->y;
         d3drect.bottom = rect->y + rect->h;
 
-        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
+        result = IDirect3DTexture9_LockRect(texturedata->texture.staging, 0, &locked, &d3drect, 0);
         if (FAILED(result)) {
             return D3D_SetError("LockRect()", result);
         }
@@ -1041,20 +1091,22 @@ D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
 static void
 D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
 {
-    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
+    D3D_RenderData *data = (D3D_RenderData *)renderer->driverdata;
+    D3D_TextureData *texturedata = (D3D_TextureData *)texture->driverdata;
 
-    if (!data) {
+    if (!texturedata) {
         return;
     }
 
-    if (data->yuv) {
-        const SDL_Rect *rect = &data->locked_rect;
+    if (texturedata->yuv) {
+        const SDL_Rect *rect = &texturedata->locked_rect;
         void *pixels =
-            (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
+            (void *) ((Uint8 *) texturedata->pixels + rect->y * texturedata->pitch +
                       rect->x * SDL_BYTESPERPIXEL(texture->format));
-        D3D_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
+        D3D_UpdateTexture(renderer, texture, rect, pixels, texturedata->pitch);
     } else {
-        IDirect3DTexture9_UnlockRect(data->texture, 0);
+        IDirect3DTexture9_UnlockRect(texturedata->texture.staging, 0);
+        IDirect3DDevice9_UpdateTexture(data->device, (IDirect3DBaseTexture9 *)texturedata->texture.staging, (IDirect3DBaseTexture9 *)texturedata->texture.texture);
     }
 }
 
@@ -1082,7 +1134,7 @@ D3D_SetRenderTargetInternal(SDL_Renderer * renderer, SDL_Texture * texture)
         return -1;
     }
 
-    result = IDirect3DTexture9_GetSurfaceLevel(texturedata->texture, 0, &data->currentRenderTarget);
+    result = IDirect3DTexture9_GetSurfaceLevel(texturedata->texture.texture, 0, &data->currentRenderTarget);
     if(FAILED(result)) {
         return D3D_SetError("GetSurfaceLevel()", result);
     }
@@ -1530,7 +1582,7 @@ D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
 
     result =
         IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
-                                    texturedata->texture);
+                                    texturedata->texture.texture);
     if (FAILED(result)) {
         return D3D_SetError("SetTexture()", result);
     }
@@ -1543,14 +1595,14 @@ D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
 
         result =
             IDirect3DDevice9_SetTexture(data->device, 1, (IDirect3DBaseTexture9 *)
-                                        texturedata->utexture);
+                                        texturedata->utexture.texture);
         if (FAILED(result)) {
             return D3D_SetError("SetTexture()", result);
         }
 
         result =
             IDirect3DDevice9_SetTexture(data->device, 2, (IDirect3DBaseTexture9 *)
-                                        texturedata->vtexture);
+                                        texturedata->vtexture.texture);
         if (FAILED(result)) {
             return D3D_SetError("SetTexture()", result);
         }
@@ -1673,7 +1725,7 @@ D3D_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
 
     result =
         IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
-                                    texturedata->texture);
+                                    texturedata->texture.texture);
     if (FAILED(result)) {
         return D3D_SetError("SetTexture()", result);
     }
@@ -1686,14 +1738,14 @@ D3D_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
 
         result =
             IDirect3DDevice9_SetTexture(data->device, 1, (IDirect3DBaseTexture9 *)
-                                        texturedata->utexture);
+                                        texturedata->utexture.texture);
         if (FAILED(result)) {
             return D3D_SetError("SetTexture()", result);
         }
 
         result =
             IDirect3DDevice9_SetTexture(data->device, 2, (IDirect3DBaseTexture9 *)
-                                        texturedata->vtexture);
+                                        texturedata->vtexture.texture);
         if (FAILED(result)) {
             return D3D_SetError("SetTexture()", result);
         }
@@ -1816,15 +1868,9 @@ D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
     if (!data) {
         return;
     }
-    if (data->texture) {
-        IDirect3DTexture9_Release(data->texture);
-    }
-    if (data->utexture) {
-        IDirect3DTexture9_Release(data->utexture);
-    }
-    if (data->vtexture) {
-        IDirect3DTexture9_Release(data->vtexture);
-    }
+    D3D_DestroyTextureRep(&data->texture);
+    D3D_DestroyTextureRep(&data->utexture);
+    D3D_DestroyTextureRep(&data->vtexture);
     SDL_free(data->pixels);
     SDL_free(data);
     texture->driverdata = NULL;
diff --git a/src/video/windows/SDL_windowsvideo.c b/src/video/windows/SDL_windowsvideo.c
index 9ab1f4c..13738b9 100644
--- a/src/video/windows/SDL_windowsvideo.c
+++ b/src/video/windows/SDL_windowsvideo.c
@@ -184,25 +184,38 @@ D3D_LoadDLL( void **pD3DDLL, IDirect3D9 **pDirect3D9Interface )
 {
     *pD3DDLL = SDL_LoadObject("D3D9.DLL");
     if (*pD3DDLL) {
-        IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
-
-        D3DCreate =
-            (IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(*pD3DDLL,
-            "Direct3DCreate9");
-        if (D3DCreate) {
-            *pDirect3D9Interface = D3DCreate(D3D_SDK_VERSION);
+        typedef IDirect3D9 *(WINAPI *Direct3DCreate9_t) (UINT SDKVersion);
+        typedef HRESULT *(WINAPI *Direct3DCreate9Ex_t)(UINT SDKVersion, IDirect3D9Ex **ppD3D);
+        Direct3DCreate9_t Direct3DCreate9Func;
+        Direct3DCreate9Ex_t Direct3DCreate9ExFunc;
+
+        Direct3DCreate9ExFunc = (Direct3DCreate9Ex_t)SDL_LoadFunction(*pD3DDLL, "Direct3DCreate9Ex");
+        if (Direct3DCreate9ExFunc) {
+            IDirect3D9Ex *pDirect3D9ExInterface;
+            HRESULT hr = Direct3DCreate9ExFunc(D3D_SDK_VERSION, &pDirect3D9ExInterface);
+            if (SUCCEEDED(hr)) {
+                const GUID IDirect3D9_GUID = { 0x81bdcbca, 0x64d4, 0x426d, { 0xae, 0x8d, 0xad, 0x1, 0x47, 0xf4, 0x27, 0x5c } };
+                hr = IDirect3D9Ex_QueryInterface(pDirect3D9ExInterface, &IDirect3D9_GUID, (LPVOID)pDirect3D9Interface);
+                IDirect3D9Ex_Release(pDirect3D9ExInterface);
+                if (SUCCEEDED(hr)) {
+                    return SDL_TRUE;
+                }
+            }
         }
-        if (!*pDirect3D9Interface) {
-            SDL_UnloadObject(*pD3DDLL);
-            *pD3DDLL = NULL;
-            return SDL_FALSE;
+
+        Direct3DCreate9Func = (Direct3DCreate9_t)SDL_LoadFunction(*pD3DDLL, "Direct3DCreate9");
+        if (Direct3DCreate9Func) {
+            *pDirect3D9Interface = Direct3DCreate9Func(D3D_SDK_VERSION);
+            if (*pDirect3D9Interface) {
+                return SDL_TRUE;
+            }
         }
 
-        return SDL_TRUE;
-    } else {
-        *pDirect3D9Interface = NULL;
-        return SDL_FALSE;
+        SDL_UnloadObject(*pD3DDLL);
+        *pD3DDLL = NULL;
     }
+    *pDirect3D9Interface = NULL;
+    return SDL_FALSE;
 }