d3drm: Implement IDirect3DRMMesh_AddGroup.
[wine] / dlls / ddraw / tests / ddraw4.c
1 /*
2  * Copyright 2011-2012 Henri Verbeet for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 #define COBJMACROS
19
20 #include "wine/test.h"
21 #include <limits.h>
22 #include "d3d.h"
23
24 struct vec2
25 {
26     float x, y;
27 };
28
29 struct vec3
30 {
31     float x, y, z;
32 };
33
34 struct vec4
35 {
36     float x, y, z, w;
37 };
38
39 struct create_window_thread_param
40 {
41     HWND window;
42     HANDLE window_created;
43     HANDLE destroy_window;
44     HANDLE thread;
45 };
46
47 static BOOL compare_float(float f, float g, unsigned int ulps)
48 {
49     int x = *(int *)&f;
50     int y = *(int *)&g;
51
52     if (x < 0)
53         x = INT_MIN - x;
54     if (y < 0)
55         y = INT_MIN - y;
56
57     if (abs(x - y) > ulps)
58         return FALSE;
59
60     return TRUE;
61 }
62
63 static BOOL compare_vec4(struct vec4 *vec, float x, float y, float z, float w, unsigned int ulps)
64 {
65     return compare_float(vec->x, x, ulps)
66             && compare_float(vec->y, y, ulps)
67             && compare_float(vec->z, z, ulps)
68             && compare_float(vec->w, w, ulps);
69 }
70
71 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
72 {
73     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
74     c1 >>= 8; c2 >>= 8;
75     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
76     c1 >>= 8; c2 >>= 8;
77     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
78     c1 >>= 8; c2 >>= 8;
79     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
80     return TRUE;
81 }
82
83 static DWORD WINAPI create_window_thread_proc(void *param)
84 {
85     struct create_window_thread_param *p = param;
86     DWORD res;
87     BOOL ret;
88
89     p->window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
90             0, 0, 640, 480, 0, 0, 0, 0);
91     ret = SetEvent(p->window_created);
92     ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
93
94     for (;;)
95     {
96         MSG msg;
97
98         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
99             DispatchMessage(&msg);
100         res = WaitForSingleObject(p->destroy_window, 100);
101         if (res == WAIT_OBJECT_0)
102             break;
103         if (res != WAIT_TIMEOUT)
104         {
105             ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
106             break;
107         }
108     }
109
110     DestroyWindow(p->window);
111
112     return 0;
113 }
114
115 static void create_window_thread(struct create_window_thread_param *p)
116 {
117     DWORD res, tid;
118
119     p->window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
120     ok(!!p->window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
121     p->destroy_window = CreateEvent(NULL, FALSE, FALSE, NULL);
122     ok(!!p->destroy_window, "CreateEvent failed, last error %#x.\n", GetLastError());
123     p->thread = CreateThread(NULL, 0, create_window_thread_proc, p, 0, &tid);
124     ok(!!p->thread, "Failed to create thread, last error %#x.\n", GetLastError());
125     res = WaitForSingleObject(p->window_created, INFINITE);
126     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
127 }
128
129 static void destroy_window_thread(struct create_window_thread_param *p)
130 {
131     SetEvent(p->destroy_window);
132     WaitForSingleObject(p->thread, INFINITE);
133     CloseHandle(p->destroy_window);
134     CloseHandle(p->window_created);
135     CloseHandle(p->thread);
136 }
137
138 static IDirectDrawSurface4 *get_depth_stencil(IDirect3DDevice3 *device)
139 {
140     IDirectDrawSurface4 *rt, *ret;
141     DDSCAPS2 caps = {DDSCAPS_ZBUFFER, 0, 0, 0};
142     HRESULT hr;
143
144     hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
145     ok(SUCCEEDED(hr), "Failed to get the render target, hr %#x.\n", hr);
146     hr = IDirectDrawSurface4_GetAttachedSurface(rt, &caps, &ret);
147     ok(SUCCEEDED(hr) || hr == DDERR_NOTFOUND, "Failed to get the z buffer, hr %#x.\n", hr);
148     IDirectDrawSurface4_Release(rt);
149     return ret;
150 }
151
152 static D3DCOLOR get_surface_color(IDirectDrawSurface4 *surface, UINT x, UINT y)
153 {
154     RECT rect = {x, y, x + 1, y + 1};
155     DDSURFACEDESC2 surface_desc;
156     D3DCOLOR color;
157     HRESULT hr;
158
159     memset(&surface_desc, 0, sizeof(surface_desc));
160     surface_desc.dwSize = sizeof(surface_desc);
161
162     hr = IDirectDrawSurface4_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
163     ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
164     if (FAILED(hr))
165         return 0xdeadbeef;
166
167     color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
168
169     hr = IDirectDrawSurface4_Unlock(surface, &rect);
170     ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
171
172     return color;
173 }
174
175 static HRESULT CALLBACK enum_z_fmt(DDPIXELFORMAT *format, void *ctx)
176 {
177     DDPIXELFORMAT *z_fmt = ctx;
178
179     if (U1(*format).dwZBufferBitDepth > U1(*z_fmt).dwZBufferBitDepth)
180         *z_fmt = *format;
181
182     return DDENUMRET_OK;
183 }
184
185 static IDirectDraw4 *create_ddraw(void)
186 {
187     IDirectDraw4 *ddraw4;
188     IDirectDraw *ddraw1;
189     HRESULT hr;
190
191     if (FAILED(DirectDrawCreate(NULL, &ddraw1, NULL)))
192         return NULL;
193
194     hr = IDirectDraw_QueryInterface(ddraw1, &IID_IDirectDraw4, (void **)&ddraw4);
195     IDirectDraw_Release(ddraw1);
196     if (FAILED(hr))
197         return NULL;
198
199     return ddraw4;
200 }
201
202 static IDirect3DDevice3 *create_device(HWND window, DWORD coop_level)
203 {
204     IDirectDrawSurface4 *surface, *ds;
205     IDirect3DDevice3 *device = NULL;
206     DDSURFACEDESC2 surface_desc;
207     IDirectDraw4 *ddraw4;
208     DDPIXELFORMAT z_fmt;
209     IDirect3D3 *d3d3;
210     HRESULT hr;
211
212     if (!(ddraw4 = create_ddraw()))
213         return NULL;
214
215     hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, coop_level);
216     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
217
218     memset(&surface_desc, 0, sizeof(surface_desc));
219     surface_desc.dwSize = sizeof(surface_desc);
220     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
221     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
222     surface_desc.dwWidth = 640;
223     surface_desc.dwHeight = 480;
224
225     hr = IDirectDraw4_CreateSurface(ddraw4, &surface_desc, &surface, NULL);
226     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
227
228     if (coop_level & DDSCL_NORMAL)
229     {
230         IDirectDrawClipper *clipper;
231
232         hr = IDirectDraw4_CreateClipper(ddraw4, 0, &clipper, NULL);
233         ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
234         hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
235         ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
236         hr = IDirectDrawSurface4_SetClipper(surface, clipper);
237         ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
238         IDirectDrawClipper_Release(clipper);
239     }
240
241     hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirect3D3, (void **)&d3d3);
242     IDirectDraw4_Release(ddraw4);
243     if (FAILED(hr))
244     {
245         IDirectDrawSurface4_Release(surface);
246         return NULL;
247     }
248
249     memset(&z_fmt, 0, sizeof(z_fmt));
250     hr = IDirect3D3_EnumZBufferFormats(d3d3, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
251     if (FAILED(hr) || !z_fmt.dwSize)
252     {
253         IDirect3D3_Release(d3d3);
254         IDirectDrawSurface4_Release(surface);
255         return NULL;
256     }
257
258     memset(&surface_desc, 0, sizeof(surface_desc));
259     surface_desc.dwSize = sizeof(surface_desc);
260     surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
261     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
262     U4(surface_desc).ddpfPixelFormat = z_fmt;
263     surface_desc.dwWidth = 640;
264     surface_desc.dwHeight = 480;
265     hr = IDirectDraw4_CreateSurface(ddraw4, &surface_desc, &ds, NULL);
266     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
267     if (FAILED(hr))
268     {
269         IDirect3D3_Release(d3d3);
270         IDirectDrawSurface4_Release(surface);
271         return NULL;
272     }
273
274     hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
275     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
276     IDirectDrawSurface4_Release(ds);
277     if (FAILED(hr))
278     {
279         IDirect3D3_Release(d3d3);
280         IDirectDrawSurface4_Release(surface);
281         return NULL;
282     }
283
284     hr = IDirect3D3_CreateDevice(d3d3, &IID_IDirect3DHALDevice, surface, &device, NULL);
285     IDirect3D3_Release(d3d3);
286     IDirectDrawSurface4_Release(surface);
287     if (FAILED(hr))
288         return NULL;
289
290     return device;
291 }
292
293 static IDirect3DViewport3 *create_viewport(IDirect3DDevice3 *device, UINT x, UINT y, UINT w, UINT h)
294 {
295     IDirect3DViewport3 *viewport;
296     D3DVIEWPORT2 vp;
297     IDirect3D3 *d3d;
298     HRESULT hr;
299
300     hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
301     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
302     hr = IDirect3D3_CreateViewport(d3d, &viewport, NULL);
303     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
304     hr = IDirect3DDevice3_AddViewport(device, viewport);
305     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
306     memset(&vp, 0, sizeof(vp));
307     vp.dwSize = sizeof(vp);
308     vp.dwX = x;
309     vp.dwY = y;
310     vp.dwWidth = w;
311     vp.dwHeight = h;
312     vp.dvClipX = -1.0f;
313     vp.dvClipY =  1.0f;
314     vp.dvClipWidth = 2.0f;
315     vp.dvClipHeight = 2.0f;
316     vp.dvMinZ = 0.0f;
317     vp.dvMaxZ = 1.0f;
318     hr = IDirect3DViewport3_SetViewport2(viewport, &vp);
319     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
320     IDirect3D3_Release(d3d);
321
322     return viewport;
323 }
324
325 static void destroy_viewport(IDirect3DDevice3 *device, IDirect3DViewport3 *viewport)
326 {
327     HRESULT hr;
328
329     hr = IDirect3DDevice3_DeleteViewport(device, viewport);
330     ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
331     IDirect3DViewport3_Release(viewport);
332 }
333
334 static void test_process_vertices(void)
335 {
336     IDirect3DVertexBuffer *src_vb, *dst_vb;
337     IDirect3DViewport3 *viewport;
338     D3DVERTEXBUFFERDESC vb_desc;
339     IDirect3DDevice3 *device;
340     struct vec3 *src_data;
341     struct vec4 *dst_data;
342     IDirect3D3 *d3d3;
343     D3DVIEWPORT2 vp2;
344     D3DVIEWPORT vp1;
345     HWND window;
346     HRESULT hr;
347
348     static D3DMATRIX identity =
349     {
350         1.0f, 0.0f, 0.0f, 0.0f,
351         0.0f, 1.0f, 0.0f, 0.0f,
352         0.0f, 0.0f, 1.0f, 0.0f,
353         0.0f, 0.0f, 0.0f, 1.0f,
354     };
355     static D3DMATRIX projection =
356     {
357         1.0f, 0.0f, 0.0f, 0.0f,
358         0.0f, 1.0f, 0.0f, 0.0f,
359         0.0f, 0.0f, 1.0f, 0.0f,
360         6.0f, 7.0f, 8.0f, 1.0f,
361     };
362
363     window = CreateWindowA("static", "d3d7_test", WS_OVERLAPPEDWINDOW,
364             0, 0, 640, 480, 0, 0, 0, 0);
365     if (!(device = create_device(window, DDSCL_NORMAL)))
366     {
367         skip("Failed to create a 3D device, skipping test.\n");
368         DestroyWindow(window);
369         return;
370     }
371
372     hr = IDirect3DDevice3_GetDirect3D(device, &d3d3);
373     ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
374
375     memset(&vb_desc, 0, sizeof(vb_desc));
376     vb_desc.dwSize = sizeof(vb_desc);
377     vb_desc.dwFVF = D3DFVF_XYZ;
378     vb_desc.dwNumVertices = 3;
379     hr = IDirect3D3_CreateVertexBuffer(d3d3, &vb_desc, &src_vb, 0, NULL);
380     ok(SUCCEEDED(hr), "Failed to create source vertex buffer, hr %#x.\n", hr);
381
382     hr = IDirect3DVertexBuffer_Lock(src_vb, DDLOCK_WRITEONLY, (void **)&src_data, NULL);
383     ok(SUCCEEDED(hr), "Failed to lock source vertex buffer, hr %#x.\n", hr);
384     src_data[0].x = -1.0f;
385     src_data[0].y = -1.0f;
386     src_data[0].z = -1.0f;
387     src_data[1].x = 0.0f;
388     src_data[1].y = 0.0f;
389     src_data[1].z = 0.0f;
390     src_data[2].x = 1.0f;
391     src_data[2].y = 1.0f;
392     src_data[2].z = 1.0f;
393     hr = IDirect3DVertexBuffer_Unlock(src_vb);
394     ok(SUCCEEDED(hr), "Failed to unlock source vertex buffer, hr %#x.\n", hr);
395
396     memset(&vb_desc, 0, sizeof(vb_desc));
397     vb_desc.dwSize = sizeof(vb_desc);
398     vb_desc.dwFVF = D3DFVF_XYZRHW;
399     vb_desc.dwNumVertices = 3;
400     hr = IDirect3D3_CreateVertexBuffer(d3d3, &vb_desc, &dst_vb, 0, NULL);
401     ok(SUCCEEDED(hr), "Failed to create destination vertex buffer, hr %#x.\n", hr);
402
403     hr = IDirect3D3_CreateViewport(d3d3, &viewport, NULL);
404     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
405     hr = IDirect3DDevice3_AddViewport(device, viewport);
406     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
407     vp2.dwSize = sizeof(vp2);
408     vp2.dwX = 10;
409     vp2.dwY = 20;
410     vp2.dwWidth = 100;
411     vp2.dwHeight = 200;
412     vp2.dvClipX = 2.0f;
413     vp2.dvClipY = 3.0f;
414     vp2.dvClipWidth = 4.0f;
415     vp2.dvClipHeight = 5.0f;
416     vp2.dvMinZ = -2.0f;
417     vp2.dvMaxZ = 3.0f;
418     hr = IDirect3DViewport3_SetViewport2(viewport, &vp2);
419     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
420     hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
421     ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
422
423     hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &identity);
424     ok(SUCCEEDED(hr), "Failed to set world transformation, hr %#x.\n", hr);
425     hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &identity);
426     ok(SUCCEEDED(hr), "Failed to set view transformation, hr %#x.\n", hr);
427     hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &identity);
428     ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
429
430     hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
431     ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
432
433     hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
434     ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
435     ok(compare_vec4(&dst_data[0], -6.500e+1f, +1.800e+2f, +2.000e-1f, +1.000e+0f, 4096),
436             "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
437             dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
438     ok(compare_vec4(&dst_data[1], -4.000e+1f, +1.400e+2f, +4.000e-1f, +1.000e+0f, 4096),
439             "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
440             dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
441     ok(compare_vec4(&dst_data[2], -1.500e+1f, +1.000e+2f, +6.000e-1f, +1.000e+0f, 4096),
442             "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
443             dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
444     hr = IDirect3DVertexBuffer_Unlock(dst_vb);
445     ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
446
447     hr = IDirect3DDevice3_MultiplyTransform(device, D3DTRANSFORMSTATE_PROJECTION, &projection);
448     ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
449
450     hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
451     ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
452
453     hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
454     ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
455     ok(compare_vec4(&dst_data[0], +8.500e+1f, -1.000e+2f, +1.800e+0f, +1.000e+0f, 4096),
456             "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
457             dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
458     ok(compare_vec4(&dst_data[1], +1.100e+2f, -1.400e+2f, +2.000e+0f, +1.000e+0f, 4096),
459             "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
460             dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
461     ok(compare_vec4(&dst_data[2], +1.350e+2f, -1.800e+2f, +2.200e+0f, +1.000e+0f, 4096),
462             "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
463             dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
464     hr = IDirect3DVertexBuffer_Unlock(dst_vb);
465     ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
466
467     vp2.dwSize = sizeof(vp2);
468     vp2.dwX = 30;
469     vp2.dwY = 40;
470     vp2.dwWidth = 90;
471     vp2.dwHeight = 80;
472     vp2.dvClipX = 4.0f;
473     vp2.dvClipY = 6.0f;
474     vp2.dvClipWidth = 2.0f;
475     vp2.dvClipHeight = 4.0f;
476     vp2.dvMinZ = 3.0f;
477     vp2.dvMaxZ = -2.0f;
478     hr = IDirect3DViewport3_SetViewport2(viewport, &vp2);
479     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
480
481     hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
482     ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
483
484     hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
485     ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
486     ok(compare_vec4(&dst_data[0], +7.500e+1f, +4.000e+1f, -8.000e-1f, +1.000e+0f, 4096),
487             "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
488             dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
489     ok(compare_vec4(&dst_data[1], +1.200e+2f, +2.000e+1f, -1.000e+0f, +1.000e+0f, 4096),
490             "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
491             dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
492     ok(compare_vec4(&dst_data[2], +1.650e+2f, +0.000e+0f, -1.200e+0f, +1.000e+0f, 4096),
493             "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
494             dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
495     hr = IDirect3DVertexBuffer_Unlock(dst_vb);
496     ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
497
498     vp1.dwSize = sizeof(vp1);
499     vp1.dwX = 30;
500     vp1.dwY = 40;
501     vp1.dwWidth = 90;
502     vp1.dwHeight = 80;
503     vp1.dvScaleX = 7.0f;
504     vp1.dvScaleY = 2.0f;
505     vp1.dvMaxX = 6.0f;
506     vp1.dvMaxY = 10.0f;
507     vp1.dvMinZ = -2.0f;
508     vp1.dvMaxZ = 3.0f;
509     hr = IDirect3DViewport3_SetViewport(viewport, &vp1);
510     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
511
512     hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
513     ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
514
515     hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
516     ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
517     ok(compare_vec4(&dst_data[0], +1.100e+2f, +6.800e+1f, +7.000e+0f, +1.000e+0f, 4096),
518             "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
519             dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
520     ok(compare_vec4(&dst_data[1], +1.170e+2f, +6.600e+1f, +8.000e+0f, +1.000e+0f, 4096),
521             "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
522             dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
523     ok(compare_vec4(&dst_data[2], +1.240e+2f, +6.400e+1f, +9.000e+0f, +1.000e+0f, 4096),
524             "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
525             dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
526     hr = IDirect3DVertexBuffer_Unlock(dst_vb);
527     ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
528
529     hr = IDirect3DDevice3_DeleteViewport(device, viewport);
530     ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
531
532     IDirect3DVertexBuffer_Release(dst_vb);
533     IDirect3DVertexBuffer_Release(src_vb);
534     IDirect3DViewport3_Release(viewport);
535     IDirect3D3_Release(d3d3);
536     IDirect3DDevice3_Release(device);
537     DestroyWindow(window);
538 }
539
540 static void test_coop_level_create_device_window(void)
541 {
542     HWND focus_window, device_window;
543     IDirectDraw4 *ddraw;
544     HRESULT hr;
545
546     focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
547             0, 0, 640, 480, 0, 0, 0, 0);
548     if (!(ddraw = create_ddraw()))
549     {
550         skip("Failed to create a ddraw object, skipping test.\n");
551         DestroyWindow(focus_window);
552         return;
553     }
554
555     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
556     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
557     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
558     ok(!device_window, "Unexpected device window found.\n");
559     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
560     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
561     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
562     ok(!device_window, "Unexpected device window found.\n");
563     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
564     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
565     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
566     ok(!device_window, "Unexpected device window found.\n");
567     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
568     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
569     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
570     ok(!device_window, "Unexpected device window found.\n");
571     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
572     ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
573     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
574     ok(!device_window, "Unexpected device window found.\n");
575
576     /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
577     if (broken(hr == DDERR_INVALIDPARAMS))
578     {
579         win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
580         IDirectDraw4_Release(ddraw);
581         DestroyWindow(focus_window);
582         return;
583     }
584
585     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
586     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
587     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
588     ok(!device_window, "Unexpected device window found.\n");
589     hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
590     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
591     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
592     ok(!device_window, "Unexpected device window found.\n");
593
594     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
595     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
596     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
597     ok(!device_window, "Unexpected device window found.\n");
598     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
599             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
600     ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
601     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
602     ok(!!device_window, "Device window not found.\n");
603
604     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
605     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
606     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
607     ok(!device_window, "Unexpected device window found.\n");
608     hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
609             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
610     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
611     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
612     ok(!!device_window, "Device window not found.\n");
613
614     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
615     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
616     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
617     ok(!device_window, "Unexpected device window found.\n");
618     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
619     ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
620     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
621     ok(!device_window, "Unexpected device window found.\n");
622     hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
623     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
624     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
625     ok(!device_window, "Unexpected device window found.\n");
626     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
627     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
628     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
629     ok(!!device_window, "Device window not found.\n");
630
631     IDirectDraw4_Release(ddraw);
632     DestroyWindow(focus_window);
633 }
634
635 static void test_clipper_blt(void)
636 {
637     IDirectDrawSurface4 *src_surface, *dst_surface;
638     RECT client_rect, src_rect, *rect;
639     IDirectDrawClipper *clipper;
640     DDSURFACEDESC2 surface_desc;
641     unsigned int i, j, x, y;
642     IDirectDraw4 *ddraw;
643     RGNDATA *rgn_data;
644     D3DCOLOR color;
645     HRGN r1, r2;
646     HWND window;
647     DDBLTFX fx;
648     HRESULT hr;
649     DWORD *ptr;
650     DWORD ret;
651
652     static const DWORD src_data[] =
653     {
654         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
655         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
656         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
657     };
658     static const D3DCOLOR expected1[] =
659     {
660         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
661         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
662         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
663         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
664     };
665     static const D3DCOLOR expected2[] =
666     {
667         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
668         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
669         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
670         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
671     };
672
673     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
674             10, 10, 640, 480, 0, 0, 0, 0);
675     ShowWindow(window, SW_SHOW);
676     if (!(ddraw = create_ddraw()))
677     {
678         skip("Failed to create a ddraw object, skipping test.\n");
679         DestroyWindow(window);
680         return;
681     }
682
683     ret = GetClientRect(window, &client_rect);
684     ok(ret, "Failed to get client rect.\n");
685     ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
686     ok(ret, "Failed to map client rect.\n");
687
688     hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
689     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
690
691     hr = IDirectDraw4_CreateClipper(ddraw, 0, &clipper, NULL);
692     ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
693     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
694     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
695     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
696     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
697     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
698     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
699     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
700     hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
701     ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
702     ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
703     ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
704     ok(rgn_data->rdh.nCount == 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
705     ok(rgn_data->rdh.nRgnSize == 16, "Got unexpected region size %u.\n", rgn_data->rdh.nRgnSize);
706     ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
707             "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
708             rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
709             rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
710             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
711     rect = (RECT *)&rgn_data->Buffer[0];
712     ok(EqualRect(rect, &client_rect),
713             "Got unexpected clip rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
714             rect->left, rect->top, rect->right, rect->bottom,
715             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
716     HeapFree(GetProcessHeap(), 0, rgn_data);
717
718     r1 = CreateRectRgn(0, 0, 320, 240);
719     ok(!!r1, "Failed to create region.\n");
720     r2 = CreateRectRgn(320, 240, 640, 480);
721     ok(!!r2, "Failed to create region.\n");
722     CombineRgn(r1, r1, r2, RGN_OR);
723     ret = GetRegionData(r1, 0, NULL);
724     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
725     ret = GetRegionData(r1, ret, rgn_data);
726     ok(!!ret, "Failed to get region data.\n");
727
728     DeleteObject(r2);
729     DeleteObject(r1);
730
731     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
732     ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
733     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
734     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
735     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
736     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
737
738     HeapFree(GetProcessHeap(), 0, rgn_data);
739
740     memset(&surface_desc, 0, sizeof(surface_desc));
741     surface_desc.dwSize = sizeof(surface_desc);
742     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
743     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
744     surface_desc.dwWidth = 640;
745     surface_desc.dwHeight = 480;
746     U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
747     U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
748     U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
749     U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
750     U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
751     U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
752
753     hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
754     ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
755     hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
756     ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
757
758     memset(&fx, 0, sizeof(fx));
759     fx.dwSize = sizeof(fx);
760     hr = IDirectDrawSurface4_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
761     ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
762     hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
763     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
764
765     hr = IDirectDrawSurface4_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
766     ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
767     ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
768     ptr = surface_desc.lpSurface;
769     memcpy(&ptr[   0], &src_data[ 0], 6 * sizeof(DWORD));
770     memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
771     memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
772     hr = IDirectDrawSurface4_Unlock(src_surface, NULL);
773     ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
774
775     hr = IDirectDrawSurface4_SetClipper(dst_surface, clipper);
776     ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
777
778     SetRect(&src_rect, 1, 1, 5, 2);
779     hr = IDirectDrawSurface4_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
780     ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
781     for (i = 0; i < 4; ++i)
782     {
783         for (j = 0; j < 4; ++j)
784         {
785             x = 80 * ((2 * j) + 1);
786             y = 60 * ((2 * i) + 1);
787             color = get_surface_color(dst_surface, x, y);
788             ok(compare_color(color, expected1[i * 4 + j], 1),
789                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
790         }
791     }
792
793     U5(fx).dwFillColor = 0xff0000ff;
794     hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
795     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
796     for (i = 0; i < 4; ++i)
797     {
798         for (j = 0; j < 4; ++j)
799         {
800             x = 80 * ((2 * j) + 1);
801             y = 60 * ((2 * i) + 1);
802             color = get_surface_color(dst_surface, x, y);
803             ok(compare_color(color, expected2[i * 4 + j], 1),
804                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
805         }
806     }
807
808     hr = IDirectDrawSurface4_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
809     ok(hr == DDERR_BLTFASTCANTCLIP, "Got unexpected hr %#x.\n", hr);
810
811     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
812     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
813     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
814     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
815     DestroyWindow(window);
816     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
817     ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
818     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
819     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
820     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
821     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
822     hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
823     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
824     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
825     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
826     hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
827     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
828
829     IDirectDrawSurface4_Release(dst_surface);
830     IDirectDrawSurface4_Release(src_surface);
831     IDirectDrawClipper_Release(clipper);
832     IDirectDraw4_Release(ddraw);
833 }
834
835 static void test_coop_level_d3d_state(void)
836 {
837     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
838     IDirectDrawSurface4 *rt, *surface;
839     IDirect3DViewport3 *viewport;
840     IDirect3DDevice3 *device;
841     IDirectDraw4 *ddraw;
842     IDirect3D3 *d3d;
843     D3DCOLOR color;
844     DWORD value;
845     HWND window;
846     HRESULT hr;
847
848     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
849             0, 0, 640, 480, 0, 0, 0, 0);
850     if (!(device = create_device(window, DDSCL_NORMAL)))
851     {
852         skip("Failed to create D3D device, skipping test.\n");
853         DestroyWindow(window);
854         return;
855     }
856
857     viewport = create_viewport(device, 0, 0, 640, 480);
858
859     hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
860     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
861     hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
862     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
863     ok(!!value, "Got unexpected z-enable state %#x.\n", value);
864     hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
865     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
866     ok(!value, "Got unexpected alpha blend enable state %#x.\n", value);
867     hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
868     ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
869     hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
870     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
871     color = get_surface_color(rt, 320, 240);
872     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
873
874     hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
875     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
876     hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
877     ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
878     IDirect3D3_Release(d3d);
879     hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
880     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
881     hr = IDirectDrawSurface4_IsLost(rt);
882     ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
883     hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
884     ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
885     IDirectDraw4_Release(ddraw);
886
887     hr = IDirect3DDevice3_GetRenderTarget(device, &surface);
888     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
889     ok(surface == rt, "Got unexpected surface %p.\n", surface);
890     hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
891     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
892     ok(!!value, "Got unexpected z-enable state %#x.\n", value);
893     hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
894     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
895     ok(!!value, "Got unexpected alpha blend enable state %#x.\n", value);
896     hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 0.0f, 0);
897     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
898     color = get_surface_color(rt, 320, 240);
899     ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
900
901     destroy_viewport(device, viewport);
902     IDirectDrawSurface4_Release(surface);
903     IDirectDrawSurface4_Release(rt);
904     IDirect3DDevice3_Release(device);
905     DestroyWindow(window);
906 }
907
908 static void test_surface_interface_mismatch(void)
909 {
910     IDirectDraw4 *ddraw = NULL;
911     IDirect3D3 *d3d = NULL;
912     IDirectDrawSurface4 *surface = NULL, *ds;
913     IDirectDrawSurface3 *surface3 = NULL;
914     IDirect3DDevice3 *device = NULL;
915     IDirect3DViewport3 *viewport = NULL;
916     DDSURFACEDESC2 surface_desc;
917     DDPIXELFORMAT z_fmt;
918     ULONG refcount;
919     HRESULT hr;
920     D3DCOLOR color;
921     HWND window;
922     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
923
924     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
925             0, 0, 640, 480, 0, 0, 0, 0);
926
927     if (!(ddraw = create_ddraw()))
928     {
929         skip("Failed to create a ddraw object, skipping test.\n");
930         goto cleanup;
931     }
932
933     hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
934     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
935
936     memset(&surface_desc, 0, sizeof(surface_desc));
937     surface_desc.dwSize = sizeof(surface_desc);
938     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
939     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
940     surface_desc.dwWidth = 640;
941     surface_desc.dwHeight = 480;
942
943     hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
944     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
945
946     hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
947     ok(SUCCEEDED(hr), "Failed to QI IDirectDrawSurface3, hr %#x.\n", hr);
948
949     hr = IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d);
950     if (FAILED(hr))
951     {
952         skip("Failed to get the IDirect3D7 interface, skipping test.\n");
953         goto cleanup;
954     }
955
956     memset(&z_fmt, 0, sizeof(z_fmt));
957     hr = IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
958     if (FAILED(hr) || !z_fmt.dwSize)
959     {
960         skip("No depth buffer formats available, skipping test.\n");
961         goto cleanup;
962     }
963
964     memset(&surface_desc, 0, sizeof(surface_desc));
965     surface_desc.dwSize = sizeof(surface_desc);
966     surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
967     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
968     U4(surface_desc).ddpfPixelFormat = z_fmt;
969     surface_desc.dwWidth = 640;
970     surface_desc.dwHeight = 480;
971     hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &ds, NULL);
972     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
973     if (FAILED(hr))
974         goto cleanup;
975
976     /* Using a different surface interface version still works */
977     hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
978     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
979     refcount = IDirectDrawSurface4_Release(ds);
980     ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
981     if (FAILED(hr))
982         goto cleanup;
983
984     /* Here too */
985     hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, (IDirectDrawSurface4 *)surface3, &device, NULL);
986     ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
987     if (FAILED(hr))
988         goto cleanup;
989
990     viewport = create_viewport(device, 0, 0, 640, 480);
991
992     hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
993     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
994     color = get_surface_color(surface, 320, 240);
995     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
996
997 cleanup:
998     if (viewport)
999         destroy_viewport(device, viewport);
1000     if (surface3) IDirectDrawSurface3_Release(surface3);
1001     if (surface) IDirectDrawSurface4_Release(surface);
1002     if (device) IDirect3DDevice3_Release(device);
1003     if (d3d) IDirect3D3_Release(d3d);
1004     if (ddraw) IDirectDraw4_Release(ddraw);
1005     DestroyWindow(window);
1006 }
1007
1008 static void test_coop_level_threaded(void)
1009 {
1010     struct create_window_thread_param p;
1011     IDirectDraw4 *ddraw;
1012     HRESULT hr;
1013
1014     if (!(ddraw = create_ddraw()))
1015     {
1016         skip("Failed to create a ddraw object, skipping test.\n");
1017         return;
1018     }
1019     create_window_thread(&p);
1020
1021     hr = IDirectDraw4_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1022     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1023
1024     IDirectDraw4_Release(ddraw);
1025     destroy_window_thread(&p);
1026 }
1027
1028 static void test_depth_blit(void)
1029 {
1030     static struct
1031     {
1032         float x, y, z;
1033         DWORD color;
1034     }
1035     quad1[] =
1036     {
1037         { -1.0,  1.0, 0.50f, 0xff00ff00},
1038         {  1.0,  1.0, 0.50f, 0xff00ff00},
1039         { -1.0, -1.0, 0.50f, 0xff00ff00},
1040         {  1.0, -1.0, 0.50f, 0xff00ff00},
1041     };
1042     static const D3DCOLOR expected_colors[4][4] =
1043     {
1044         {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1045         {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1046         {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1047         {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1048     };
1049     DDSURFACEDESC2 ddsd_new, ddsd_existing;
1050
1051     IDirect3DDevice3 *device;
1052     IDirectDrawSurface4 *ds1, *ds2, *ds3, *rt;
1053     IDirect3DViewport3 *viewport;
1054     RECT src_rect, dst_rect;
1055     unsigned int i, j;
1056     D3DCOLOR color;
1057     HRESULT hr;
1058     IDirect3D3 *d3d;
1059     IDirectDraw4 *ddraw;
1060     DDBLTFX fx;
1061     HWND window;
1062     D3DRECT d3drect;
1063
1064     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1065             0, 0, 640, 480, 0, 0, 0, 0);
1066     if (!(device = create_device(window, DDSCL_NORMAL)))
1067     {
1068         skip("Failed to create D3D device, skipping test.\n");
1069         DestroyWindow(window);
1070         return;
1071     }
1072
1073     hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1074     ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
1075     hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1076     ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
1077     IDirect3D3_Release(d3d);
1078
1079     ds1 = get_depth_stencil(device);
1080
1081     memset(&ddsd_new, 0, sizeof(ddsd_new));
1082     ddsd_new.dwSize = sizeof(ddsd_new);
1083     memset(&ddsd_existing, 0, sizeof(ddsd_existing));
1084     ddsd_existing.dwSize = sizeof(ddsd_existing);
1085     hr = IDirectDrawSurface4_GetSurfaceDesc(ds1, &ddsd_existing);
1086     ddsd_new.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
1087     ddsd_new.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
1088     ddsd_new.dwWidth = ddsd_existing.dwWidth;
1089     ddsd_new.dwHeight = ddsd_existing.dwHeight;
1090     U4(ddsd_new).ddpfPixelFormat = U4(ddsd_existing).ddpfPixelFormat;
1091     hr = IDirectDraw4_CreateSurface(ddraw, &ddsd_new, &ds2, NULL);
1092     ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
1093     hr = IDirectDraw4_CreateSurface(ddraw, &ddsd_new, &ds3, NULL);
1094     ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
1095     IDirectDraw4_Release(ddraw);
1096
1097     viewport = create_viewport(device, 0, 0, ddsd_existing.dwWidth, ddsd_existing.dwHeight);
1098     hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1099     ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
1100
1101     hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
1102     ok(SUCCEEDED(hr), "Failed to enable z testing, hr %#x.\n", hr);
1103     hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL);
1104     ok(SUCCEEDED(hr), "Failed to set the z function, hr %#x.\n", hr);
1105     hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
1106     ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
1107
1108     U1(d3drect).x1 = U2(d3drect).y1 = 0;
1109     U3(d3drect).x2 = ddsd_existing.dwWidth; U4(d3drect).y2 = ddsd_existing.dwHeight;
1110     hr = IDirect3DViewport3_Clear2(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER, 0, 0.0f, 0);
1111     ok(SUCCEEDED(hr), "Failed to clear the z buffer, hr %#x.\n", hr);
1112
1113     /* Partial blit. */
1114     SetRect(&src_rect, 0, 0, 320, 240);
1115     SetRect(&dst_rect, 0, 0, 320, 240);
1116     hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1117     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1118     /* Different locations. */
1119     SetRect(&src_rect, 0, 0, 320, 240);
1120     SetRect(&dst_rect, 320, 240, 640, 480);
1121     hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1122     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1123     /* Streched. */
1124     SetRect(&src_rect, 0, 0, 320, 240);
1125     SetRect(&dst_rect, 0, 0, 640, 480);
1126     hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1127     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1128     /* Flipped. */
1129     SetRect(&src_rect, 0, 480, 640, 0);
1130     SetRect(&dst_rect, 0, 0, 640, 480);
1131     hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1132     ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1133     SetRect(&src_rect, 0, 0, 640, 480);
1134     SetRect(&dst_rect, 0, 480, 640, 0);
1135     hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1136     ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1137     /* Full, explicit. */
1138     SetRect(&src_rect, 0, 0, 640, 480);
1139     SetRect(&dst_rect, 0, 0, 640, 480);
1140     hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1141     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1142     /* Depth -> color blit: Succeeds on Win7 + Radeon HD 5700, fails on WinXP + Radeon X1600 */
1143
1144     /* Depth blit inside a BeginScene / EndScene pair */
1145     hr = IDirect3DDevice3_BeginScene(device);
1146     ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1147     /* From the current depth stencil */
1148     hr = IDirectDrawSurface4_Blt(ds2, NULL, ds1, NULL, DDBLT_WAIT, NULL);
1149     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1150     /* To the current depth stencil */
1151     hr = IDirectDrawSurface4_Blt(ds1, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1152     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1153     /* Between unbound surfaces */
1154     hr = IDirectDrawSurface4_Blt(ds3, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1155     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1156     hr = IDirect3DDevice3_EndScene(device);
1157     ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1158
1159     /* Avoid changing the depth stencil, it doesn't work properly on Windows.
1160      * Instead use DDBLT_DEPTHFILL to clear the depth stencil. Unfortunately
1161      * drivers disagree on the meaning of dwFillDepth. Only 0 seems to produce
1162      * a reliable result(z = 0.0) */
1163     memset(&fx, 0, sizeof(fx));
1164     fx.dwSize = sizeof(fx);
1165     hr = IDirectDrawSurface4_Blt(ds2, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
1166     ok(SUCCEEDED(hr), "Failed to clear the source z buffer, hr %#x.\n", hr);
1167
1168     hr = IDirect3DViewport3_Clear2(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET, 0xffff0000, 1.0f, 0);
1169     ok(SUCCEEDED(hr), "Failed to clear the color and z buffers, hr %#x.\n", hr);
1170     SetRect(&dst_rect, 0, 0, 320, 240);
1171     hr = IDirectDrawSurface4_Blt(ds1, &dst_rect, ds2, NULL, DDBLT_WAIT, NULL);
1172     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1173     IDirectDrawSurface4_Release(ds3);
1174     IDirectDrawSurface4_Release(ds2);
1175     IDirectDrawSurface4_Release(ds1);
1176
1177     hr = IDirect3DDevice3_BeginScene(device);
1178     ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1179     hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE,
1180             quad1, 4, 0);
1181     ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1182     hr = IDirect3DDevice3_EndScene(device);
1183     ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1184
1185     hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1186     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1187     for (i = 0; i < 4; ++i)
1188     {
1189         for (j = 0; j < 4; ++j)
1190         {
1191             unsigned int x = 80 * ((2 * j) + 1);
1192             unsigned int y = 60 * ((2 * i) + 1);
1193             color = get_surface_color(rt, x, y);
1194             ok(compare_color(color, expected_colors[i][j], 1),
1195                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected_colors[i][j], x, y, color);
1196         }
1197     }
1198     IDirectDrawSurface4_Release(rt);
1199
1200     destroy_viewport(device, viewport);
1201     IDirect3DDevice3_Release(device);
1202     DestroyWindow(window);
1203 }
1204
1205 static void test_texture_load_ckey(void)
1206 {
1207     IDirectDraw4 *ddraw;
1208     IDirectDrawSurface4 *src;
1209     IDirectDrawSurface4 *dst;
1210     IDirect3DTexture2 *src_tex;
1211     IDirect3DTexture2 *dst_tex;
1212     DDSURFACEDESC2 ddsd;
1213     HRESULT hr;
1214     DDCOLORKEY ckey;
1215
1216     if (!(ddraw = create_ddraw()))
1217     {
1218         skip("Failed to create ddraw object, skipping test.\n");
1219         return;
1220     }
1221     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
1222     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1223
1224     memset(&ddsd, 0, sizeof(ddsd));
1225     ddsd.dwSize = sizeof(ddsd);
1226     ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
1227     ddsd.dwHeight = 128;
1228     ddsd.dwWidth = 128;
1229     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
1230     hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &src, NULL);
1231     ok(SUCCEEDED(hr), "Failed to create source texture, hr %#x.\n", hr);
1232     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1233     hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &dst, NULL);
1234     ok(SUCCEEDED(hr), "Failed to create destination texture, hr %#x.\n", hr);
1235
1236     hr = IDirectDrawSurface4_QueryInterface(src, &IID_IDirect3DTexture2, (void **)&src_tex);
1237     ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get Direct3DTexture2 interface, hr %#x.\n", hr);
1238     if (FAILED(hr))
1239     {
1240         /* 64 bit ddraw does not support d3d */
1241         skip("Could not get Direct3DTexture2 interface, skipping texture::Load color keying tests.\n");
1242         IDirectDrawSurface4_Release(dst);
1243         IDirectDrawSurface4_Release(src);
1244         IDirectDraw4_Release(ddraw);
1245         return;
1246     }
1247     hr = IDirectDrawSurface4_QueryInterface(dst, &IID_IDirect3DTexture2, (void **)&dst_tex);
1248     ok(SUCCEEDED(hr), "Failed to get Direct3DTexture2 interface, hr %#x.\n", hr);
1249
1250     /* No surface has a color key */
1251     hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1252     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1253     ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0xdeadbeef;
1254     hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1255     ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1256     ok(ckey.dwColorSpaceLowValue == 0xdeadbeef, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1257     ok(ckey.dwColorSpaceHighValue == 0xdeadbeef, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1258
1259     /* Source surface has a color key */
1260     ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x0000ff00;
1261     hr = IDirectDrawSurface4_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
1262     ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1263     hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1264     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1265     hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1266     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1267     ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1268     ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1269
1270     /* Both surfaces have a color key: Dest ckey is overwritten */
1271     ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x000000ff;
1272     hr = IDirectDrawSurface4_SetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1273     ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1274     hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1275     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1276     hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1277     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1278     ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1279     ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1280
1281     /* Only the destination has a color key: It is not deleted */
1282     hr = IDirectDrawSurface4_SetColorKey(src, DDCKEY_SRCBLT, NULL);
1283     ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1284     hr = IDirectDrawSurface4_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
1285     ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1286     hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1287     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1288     hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1289     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1290     ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1291     ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1292
1293     IDirect3DTexture2_Release(dst_tex);
1294     IDirect3DTexture2_Release(src_tex);
1295     IDirectDrawSurface4_Release(dst);
1296     IDirectDrawSurface4_Release(src);
1297     IDirectDraw4_Release(ddraw);
1298 }
1299
1300 static ULONG get_refcount(IUnknown *test_iface)
1301 {
1302     IUnknown_AddRef(test_iface);
1303     return IUnknown_Release(test_iface);
1304 }
1305
1306 static void test_viewport_interfaces(void)
1307 {
1308     IDirectDraw4 *ddraw;
1309     IDirect3D3 *d3d;
1310     HRESULT hr, old_d3d_ref;
1311     ULONG ref;
1312     IDirect3DViewport *viewport;
1313     IDirect3DViewport2 *viewport2;
1314     IDirect3DViewport3 *viewport3;
1315     IDirectDrawGammaControl *gamma;
1316     IUnknown *unknown;
1317
1318     if (!(ddraw = create_ddraw()))
1319     {
1320         skip("Failed to create ddraw object, skipping test.\n");
1321         return;
1322     }
1323     hr = IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d);
1324     ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get d3d interface, hr %#x.\n", hr);
1325     if (FAILED(hr))
1326     {
1327         skip("Direct3D not available, skipping tests\n");
1328         IDirectDraw4_Release(ddraw);
1329         return;
1330     }
1331     old_d3d_ref = get_refcount((IUnknown *)d3d);
1332
1333     hr = IDirect3D3_CreateViewport(d3d, &viewport3, NULL);
1334     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1335     ref = get_refcount((IUnknown *)viewport3);
1336     ok(ref == 1, "Initial IDirect3DViewport3 refcount is %u\n", ref);
1337     ref = get_refcount((IUnknown *)d3d);
1338     ok(ref == old_d3d_ref, "IDirect3D3 refcount is %u\n", ref);
1339
1340     gamma = (IDirectDrawGammaControl *)0xdeadbeef;
1341     hr = IDirect3DViewport2_QueryInterface(viewport3, &IID_IDirectDrawGammaControl, (void **)&gamma);
1342     ok(hr == E_NOINTERFACE, "Got unexpected hr %#x.\n", hr);
1343     ok(gamma == NULL, "Interface not set to NULL by failed QI call: %p\n", gamma);
1344     if (SUCCEEDED(hr)) IDirectDrawGammaControl_Release(gamma);
1345     /* NULL iid: Segfaults */
1346
1347     hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IDirect3DViewport, (void **)&viewport);
1348     ok(SUCCEEDED(hr), "Failed to QI IDirect3DViewport, hr %#x.\n", hr);
1349     if (viewport)
1350     {
1351         ref = get_refcount((IUnknown *)viewport);
1352         ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1353         ref = get_refcount((IUnknown *)viewport3);
1354         ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1355         IDirect3DViewport_Release(viewport);
1356         viewport = NULL;
1357     }
1358
1359     hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IDirect3DViewport3, (void **)&viewport2);
1360     ok(SUCCEEDED(hr), "Failed to QI IDirect3DViewport3, hr %#x.\n", hr);
1361     if (viewport2)
1362     {
1363         ref = get_refcount((IUnknown *)viewport2);
1364         ok(ref == 2, "IDirect3DViewport2 refcount is %u\n", ref);
1365         ref = get_refcount((IUnknown *)viewport3);
1366         ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1367         IDirect3DViewport3_Release(viewport2);
1368     }
1369
1370     hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IUnknown, (void **)&unknown);
1371     ok(SUCCEEDED(hr), "Failed to QI IUnknown, hr %#x.\n", hr);
1372     if (unknown)
1373     {
1374         ref = get_refcount((IUnknown *)viewport3);
1375         ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1376         ref = get_refcount(unknown);
1377         ok(ref == 2, "IUnknown refcount is %u\n", ref);
1378         IUnknown_Release(unknown);
1379     }
1380
1381     IDirect3DViewport3_Release(viewport3);
1382     IDirect3D3_Release(d3d);
1383     IDirectDraw4_Release(ddraw);
1384 }
1385
1386 static void test_zenable(void)
1387 {
1388     static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1389     static struct
1390     {
1391         struct vec4 position;
1392         D3DCOLOR diffuse;
1393     }
1394     tquad[] =
1395     {
1396         {{  0.0f, 480.0f, -0.5f, 1.0f}, 0xff00ff00},
1397         {{  0.0f,   0.0f, -0.5f, 1.0f}, 0xff00ff00},
1398         {{640.0f, 480.0f,  1.5f, 1.0f}, 0xff00ff00},
1399         {{640.0f,   0.0f,  1.5f, 1.0f}, 0xff00ff00},
1400     };
1401     IDirect3DViewport3 *viewport;
1402     IDirect3DDevice3 *device;
1403     IDirectDrawSurface4 *rt;
1404     D3DCOLOR color;
1405     HWND window;
1406     HRESULT hr;
1407     UINT x, y;
1408     UINT i, j;
1409
1410     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1411             0, 0, 640, 480, 0, 0, 0, 0);
1412     if (!(device = create_device(window, DDSCL_NORMAL)))
1413     {
1414         skip("Failed to create D3D device, skipping test.\n");
1415         DestroyWindow(window);
1416         return;
1417     }
1418
1419     viewport = create_viewport(device, 0, 0, 640, 480);
1420     hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1421     ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1422
1423     hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
1424     ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
1425
1426     hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff0000, 0.0f, 0);
1427     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1428     hr = IDirect3DDevice3_BeginScene(device);
1429     ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1430     hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, tquad, 4, 0);
1431     ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1432     hr = IDirect3DDevice3_EndScene(device);
1433     ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1434
1435     hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1436     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1437     for (i = 0; i < 4; ++i)
1438     {
1439         for (j = 0; j < 4; ++j)
1440         {
1441             x = 80 * ((2 * j) + 1);
1442             y = 60 * ((2 * i) + 1);
1443             color = get_surface_color(rt, x, y);
1444             ok(compare_color(color, 0x0000ff00, 1),
1445                     "Expected color 0x0000ff00 at %u, %u, got 0x%08x.\n", x, y, color);
1446         }
1447     }
1448     IDirectDrawSurface4_Release(rt);
1449
1450     destroy_viewport(device, viewport);
1451     IDirect3DDevice3_Release(device);
1452     DestroyWindow(window);
1453 }
1454
1455 static void test_ck_rgba(void)
1456 {
1457     static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1458     static struct
1459     {
1460         struct vec4 position;
1461         struct vec2 texcoord;
1462     }
1463     tquad[] =
1464     {
1465         {{  0.0f, 480.0f, 0.25f, 1.0f}, {0.0f, 0.0f}},
1466         {{  0.0f,   0.0f, 0.25f, 1.0f}, {0.0f, 1.0f}},
1467         {{640.0f, 480.0f, 0.25f, 1.0f}, {1.0f, 0.0f}},
1468         {{640.0f,   0.0f, 0.25f, 1.0f}, {1.0f, 1.0f}},
1469         {{  0.0f, 480.0f, 0.75f, 1.0f}, {0.0f, 0.0f}},
1470         {{  0.0f,   0.0f, 0.75f, 1.0f}, {0.0f, 1.0f}},
1471         {{640.0f, 480.0f, 0.75f, 1.0f}, {1.0f, 0.0f}},
1472         {{640.0f,   0.0f, 0.75f, 1.0f}, {1.0f, 1.0f}},
1473     };
1474     static const struct
1475     {
1476         D3DCOLOR fill_color;
1477         BOOL color_key;
1478         BOOL blend;
1479         D3DCOLOR result1;
1480         D3DCOLOR result2;
1481     }
1482     tests[] =
1483     {
1484         {0xff00ff00, TRUE,  TRUE,  0x00ff0000, 0x000000ff},
1485         {0xff00ff00, TRUE,  FALSE, 0x00ff0000, 0x000000ff},
1486         {0xff00ff00, FALSE, TRUE,  0x0000ff00, 0x0000ff00},
1487         {0xff00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00},
1488         {0x7f00ff00, TRUE,  TRUE,  0x00807f00, 0x00807f00},
1489         {0x7f00ff00, TRUE,  FALSE, 0x0000ff00, 0x0000ff00},
1490         {0x7f00ff00, FALSE, TRUE,  0x00807f00, 0x00807f00},
1491         {0x7f00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00},
1492     };
1493
1494     IDirectDrawSurface4 *surface;
1495     IDirect3DViewport3 *viewport;
1496     DDSURFACEDESC2 surface_desc;
1497     IDirect3DTexture2 *texture;
1498     IDirect3DDevice3 *device;
1499     IDirectDrawSurface4 *rt;
1500     IDirectDraw4 *ddraw;
1501     IDirect3D3 *d3d;
1502     D3DCOLOR color;
1503     HWND window;
1504     DDBLTFX fx;
1505     HRESULT hr;
1506     UINT i;
1507
1508     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1509             0, 0, 640, 480, 0, 0, 0, 0);
1510     if (!(device = create_device(window, DDSCL_NORMAL)))
1511     {
1512         skip("Failed to create D3D device, skipping test.\n");
1513         DestroyWindow(window);
1514         return;
1515     }
1516
1517     viewport = create_viewport(device, 0, 0, 640, 480);
1518     hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1519     ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1520
1521     hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1522     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1523     hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1524     ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1525     IDirect3D3_Release(d3d);
1526
1527     memset(&surface_desc, 0, sizeof(surface_desc));
1528     surface_desc.dwSize = sizeof(surface_desc);
1529     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1530     surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1531     surface_desc.dwWidth = 256;
1532     surface_desc.dwHeight = 256;
1533     U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
1534     U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1535     U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
1536     U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1537     U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1538     U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
1539     U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
1540     surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0xff00ff00;
1541     surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0xff00ff00;
1542     hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1543     ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
1544     hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
1545     ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1546
1547     hr = IDirect3DDevice3_SetTexture(device, 0, texture);
1548     ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
1549     hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
1550     ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1551     hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
1552     ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1553
1554     hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1555     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1556
1557     for (i = 0; i < sizeof(tests) / sizeof(*tests); ++i)
1558     {
1559         hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, tests[i].color_key);
1560         ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
1561         hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, tests[i].blend);
1562         ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1563
1564         memset(&fx, 0, sizeof(fx));
1565         fx.dwSize = sizeof(fx);
1566         U5(fx).dwFillColor = tests[i].fill_color;
1567         hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1568         ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1569
1570         hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect,
1571                 D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff0000, 1.0f, 0);
1572         ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1573         hr = IDirect3DDevice3_BeginScene(device);
1574         ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1575         hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1576         ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1577         hr = IDirect3DDevice3_EndScene(device);
1578         ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1579
1580         color = get_surface_color(rt, 320, 240);
1581         if (i == 2)
1582             todo_wine ok(compare_color(color, tests[i].result1, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1583                     tests[i].result1, i, color);
1584         else
1585             ok(compare_color(color, tests[i].result1, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1586                     tests[i].result1, i, color);
1587
1588         U5(fx).dwFillColor = 0xff0000ff;
1589         hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1590         ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1591
1592         hr = IDirect3DDevice3_BeginScene(device);
1593         ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1594         hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[4], 4, 0);
1595         ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1596         hr = IDirect3DDevice3_EndScene(device);
1597         ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1598
1599         /* This tests that fragments that are masked out by the color key are
1600          * discarded, instead of just fully transparent. */
1601         color = get_surface_color(rt, 320, 240);
1602         if (i == 2)
1603             todo_wine ok(compare_color(color, tests[i].result2, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1604                     tests[i].result2, i, color);
1605         else
1606             ok(compare_color(color, tests[i].result2, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1607                     tests[i].result2, i, color);
1608     }
1609
1610     IDirectDrawSurface4_Release(rt);
1611     IDirect3DTexture2_Release(texture);
1612     IDirectDrawSurface4_Release(surface);
1613     destroy_viewport(device, viewport);
1614     IDirectDraw4_Release(ddraw);
1615     IDirect3DDevice3_Release(device);
1616     DestroyWindow(window);
1617 }
1618
1619 struct qi_test
1620 {
1621     REFIID iid;
1622     REFIID refcount_iid;
1623     HRESULT hr;
1624 };
1625
1626 static void test_qi(const char *test_name, IUnknown *base_iface,
1627         REFIID refcount_iid, const struct qi_test *tests, UINT entry_count)
1628 {
1629     ULONG refcount, expected_refcount;
1630     IUnknown *iface1, *iface2;
1631     HRESULT hr;
1632     UINT i, j;
1633
1634     for (i = 0; i < entry_count; ++i)
1635     {
1636         hr = IUnknown_QueryInterface(base_iface, tests[i].iid, (void **)&iface1);
1637         ok(hr == tests[i].hr, "Got hr %#x for test \"%s\" %u.\n", hr, test_name, i);
1638         if (SUCCEEDED(hr))
1639         {
1640             for (j = 0; j < entry_count; ++j)
1641             {
1642                 hr = IUnknown_QueryInterface(iface1, tests[j].iid, (void **)&iface2);
1643                 ok(hr == tests[j].hr, "Got hr %#x for test \"%s\" %u, %u.\n", hr, test_name, i, j);
1644                 if (SUCCEEDED(hr))
1645                 {
1646                     expected_refcount = 0;
1647                     if (IsEqualGUID(refcount_iid, tests[j].refcount_iid))
1648                         ++expected_refcount;
1649                     if (IsEqualGUID(tests[i].refcount_iid, tests[j].refcount_iid))
1650                         ++expected_refcount;
1651                     refcount = IUnknown_Release(iface2);
1652                     ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, %u, expected %u.\n",
1653                             refcount, test_name, i, j, expected_refcount);
1654                 }
1655             }
1656
1657             expected_refcount = 0;
1658             if (IsEqualGUID(refcount_iid, tests[i].refcount_iid))
1659                 ++expected_refcount;
1660             refcount = IUnknown_Release(iface1);
1661             ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, expected %u.\n",
1662                     refcount, test_name, i, expected_refcount);
1663         }
1664     }
1665 }
1666
1667 static void test_surface_qi(void)
1668 {
1669     static const struct qi_test tests[] =
1670     {
1671         {&IID_IDirect3DTexture2,        &IID_IDirectDrawSurface4,       S_OK         },
1672         {&IID_IDirect3DTexture,         &IID_IDirectDrawSurface4,       S_OK         },
1673         {&IID_IDirectDrawGammaControl,  &IID_IDirectDrawGammaControl,   S_OK         },
1674         {&IID_IDirectDrawColorControl,  NULL,                           E_NOINTERFACE},
1675         {&IID_IDirectDrawSurface7,      &IID_IDirectDrawSurface7,       S_OK         },
1676         {&IID_IDirectDrawSurface4,      &IID_IDirectDrawSurface4,       S_OK         },
1677         {&IID_IDirectDrawSurface3,      &IID_IDirectDrawSurface3,       S_OK         },
1678         {&IID_IDirectDrawSurface2,      &IID_IDirectDrawSurface2,       S_OK         },
1679         {&IID_IDirectDrawSurface,       &IID_IDirectDrawSurface,        S_OK         },
1680         {&IID_IDirect3DDevice7,         NULL,                           E_INVALIDARG },
1681         {&IID_IDirect3DDevice3,         NULL,                           E_INVALIDARG },
1682         {&IID_IDirect3DDevice2,         NULL,                           E_INVALIDARG },
1683         {&IID_IDirect3DDevice,          NULL,                           E_INVALIDARG },
1684         {&IID_IDirect3D7,               NULL,                           E_INVALIDARG },
1685         {&IID_IDirect3D3,               NULL,                           E_INVALIDARG },
1686         {&IID_IDirect3D2,               NULL,                           E_INVALIDARG },
1687         {&IID_IDirect3D,                NULL,                           E_INVALIDARG },
1688         {&IID_IDirectDraw7,             NULL,                           E_INVALIDARG },
1689         {&IID_IDirectDraw4,             NULL,                           E_INVALIDARG },
1690         {&IID_IDirectDraw3,             NULL,                           E_INVALIDARG },
1691         {&IID_IDirectDraw2,             NULL,                           E_INVALIDARG },
1692         {&IID_IDirectDraw,              NULL,                           E_INVALIDARG },
1693         {&IID_IDirect3DLight,           NULL,                           E_INVALIDARG },
1694         {&IID_IDirect3DMaterial,        NULL,                           E_INVALIDARG },
1695         {&IID_IDirect3DMaterial2,       NULL,                           E_INVALIDARG },
1696         {&IID_IDirect3DMaterial3,       NULL,                           E_INVALIDARG },
1697         {&IID_IDirect3DExecuteBuffer,   NULL,                           E_INVALIDARG },
1698         {&IID_IDirect3DViewport,        NULL,                           E_INVALIDARG },
1699         {&IID_IDirect3DViewport2,       NULL,                           E_INVALIDARG },
1700         {&IID_IDirect3DViewport3,       NULL,                           E_INVALIDARG },
1701         {&IID_IDirect3DVertexBuffer,    NULL,                           E_INVALIDARG },
1702         {&IID_IDirect3DVertexBuffer7,   NULL,                           E_INVALIDARG },
1703         {&IID_IDirectDrawPalette,       NULL,                           E_INVALIDARG },
1704         {&IID_IDirectDrawClipper,       NULL,                           E_INVALIDARG },
1705         {&IID_IUnknown,                 &IID_IDirectDrawSurface,        S_OK         },
1706     };
1707
1708     IDirectDrawSurface4 *surface;
1709     DDSURFACEDESC2 surface_desc;
1710     IDirect3DDevice3 *device;
1711     IDirectDraw4 *ddraw;
1712     HWND window;
1713     HRESULT hr;
1714
1715     if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
1716     {
1717         win_skip("DirectDrawCreateEx not available, skipping test.\n");
1718         return;
1719     }
1720
1721     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1722             0, 0, 640, 480, 0, 0, 0, 0);
1723     /* Try to create a D3D device to see if the ddraw implementation supports
1724      * D3D. 64-bit ddraw in particular doesn't seem to support D3D, and
1725      * doesn't support e.g. the IDirect3DTexture interfaces. */
1726     if (!(device = create_device(window, DDSCL_NORMAL)))
1727     {
1728         skip("Failed to create D3D device, skipping test.\n");
1729         DestroyWindow(window);
1730         return;
1731     }
1732     IDirect3DDevice_Release(device);
1733     if (!(ddraw = create_ddraw()))
1734     {
1735         skip("Failed to create a ddraw object, skipping test.\n");
1736         DestroyWindow(window);
1737         return;
1738     }
1739     hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
1740     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1741
1742     memset(&surface_desc, 0, sizeof(surface_desc));
1743     surface_desc.dwSize = sizeof(surface_desc);
1744     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1745     surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1746     surface_desc.dwWidth = 512;
1747     surface_desc.dwHeight = 512;
1748     hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1749     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1750
1751     test_qi("surface_qi", (IUnknown *)surface, &IID_IDirectDrawSurface4, tests, sizeof(tests) / sizeof(*tests));
1752
1753     IDirectDrawSurface4_Release(surface);
1754     IDirectDraw4_Release(ddraw);
1755     DestroyWindow(window);
1756 }
1757
1758 static void test_device_qi(void)
1759 {
1760     static const struct qi_test tests[] =
1761     {
1762         {&IID_IDirect3DTexture2,        NULL,                           E_NOINTERFACE},
1763         {&IID_IDirect3DTexture,         NULL,                           E_NOINTERFACE},
1764         {&IID_IDirectDrawGammaControl,  NULL,                           E_NOINTERFACE},
1765         {&IID_IDirectDrawColorControl,  NULL,                           E_NOINTERFACE},
1766         {&IID_IDirectDrawSurface7,      NULL,                           E_NOINTERFACE},
1767         {&IID_IDirectDrawSurface4,      NULL,                           E_NOINTERFACE},
1768         {&IID_IDirectDrawSurface3,      NULL,                           E_NOINTERFACE},
1769         {&IID_IDirectDrawSurface2,      NULL,                           E_NOINTERFACE},
1770         {&IID_IDirectDrawSurface,       NULL,                           E_NOINTERFACE},
1771         {&IID_IDirect3DDevice7,         NULL,                           E_NOINTERFACE},
1772         {&IID_IDirect3DDevice3,         &IID_IDirect3DDevice3,          S_OK         },
1773         {&IID_IDirect3DDevice2,         &IID_IDirect3DDevice3,          S_OK         },
1774         {&IID_IDirect3DDevice,          &IID_IDirect3DDevice3,          S_OK         },
1775         {&IID_IDirect3DRampDevice,      NULL,                           E_NOINTERFACE},
1776         {&IID_IDirect3DRGBDevice,       NULL,                           E_NOINTERFACE},
1777         {&IID_IDirect3DHALDevice,       NULL,                           E_NOINTERFACE},
1778         {&IID_IDirect3DMMXDevice,       NULL,                           E_NOINTERFACE},
1779         {&IID_IDirect3DRefDevice,       NULL,                           E_NOINTERFACE},
1780         {&IID_IDirect3DTnLHalDevice,    NULL,                           E_NOINTERFACE},
1781         {&IID_IDirect3DNullDevice,      NULL,                           E_NOINTERFACE},
1782         {&IID_IDirect3D7,               NULL,                           E_NOINTERFACE},
1783         {&IID_IDirect3D3,               NULL,                           E_NOINTERFACE},
1784         {&IID_IDirect3D2,               NULL,                           E_NOINTERFACE},
1785         {&IID_IDirect3D,                NULL,                           E_NOINTERFACE},
1786         {&IID_IDirectDraw7,             NULL,                           E_NOINTERFACE},
1787         {&IID_IDirectDraw4,             NULL,                           E_NOINTERFACE},
1788         {&IID_IDirectDraw3,             NULL,                           E_NOINTERFACE},
1789         {&IID_IDirectDraw2,             NULL,                           E_NOINTERFACE},
1790         {&IID_IDirectDraw,              NULL,                           E_NOINTERFACE},
1791         {&IID_IDirect3DLight,           NULL,                           E_NOINTERFACE},
1792         {&IID_IDirect3DMaterial,        NULL,                           E_NOINTERFACE},
1793         {&IID_IDirect3DMaterial2,       NULL,                           E_NOINTERFACE},
1794         {&IID_IDirect3DMaterial3,       NULL,                           E_NOINTERFACE},
1795         {&IID_IDirect3DExecuteBuffer,   NULL,                           E_NOINTERFACE},
1796         {&IID_IDirect3DViewport,        NULL,                           E_NOINTERFACE},
1797         {&IID_IDirect3DViewport2,       NULL,                           E_NOINTERFACE},
1798         {&IID_IDirect3DViewport3,       NULL,                           E_NOINTERFACE},
1799         {&IID_IDirect3DVertexBuffer,    NULL,                           E_NOINTERFACE},
1800         {&IID_IDirect3DVertexBuffer7,   NULL,                           E_NOINTERFACE},
1801         {&IID_IDirectDrawPalette,       NULL,                           E_NOINTERFACE},
1802         {&IID_IDirectDrawClipper,       NULL,                           E_NOINTERFACE},
1803         {&IID_IUnknown,                 &IID_IDirect3DDevice3,          S_OK         },
1804     };
1805
1806     IDirect3DDevice3 *device;
1807     HWND window;
1808
1809     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1810             0, 0, 640, 480, 0, 0, 0, 0);
1811     if (!(device = create_device(window, DDSCL_NORMAL)))
1812     {
1813         skip("Failed to create D3D device, skipping test.\n");
1814         DestroyWindow(window);
1815         return;
1816     }
1817
1818     test_qi("device_qi", (IUnknown *)device, &IID_IDirect3DDevice3, tests, sizeof(tests) / sizeof(*tests));
1819
1820     IDirect3DDevice3_Release(device);
1821     DestroyWindow(window);
1822 }
1823
1824 START_TEST(ddraw4)
1825 {
1826     test_process_vertices();
1827     test_coop_level_create_device_window();
1828     test_clipper_blt();
1829     test_coop_level_d3d_state();
1830     test_surface_interface_mismatch();
1831     test_coop_level_threaded();
1832     test_depth_blit();
1833     test_texture_load_ckey();
1834     test_viewport_interfaces();
1835     test_zenable();
1836     test_ck_rgba();
1837     test_surface_qi();
1838     test_device_qi();
1839 }