ddraw/tests: Add tests for z-clipping with D3DRENDERSTATE_ZENABLE disabled.
[wine] / dlls / ddraw / tests / ddraw1.c
1 /*
2  * Copyright 2011 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 "d3d.h"
22
23 struct create_window_thread_param
24 {
25     HWND window;
26     HANDLE window_created;
27     HANDLE destroy_window;
28     HANDLE thread;
29 };
30
31 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
32 {
33     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
34     c1 >>= 8; c2 >>= 8;
35     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
36     c1 >>= 8; c2 >>= 8;
37     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
38     c1 >>= 8; c2 >>= 8;
39     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
40     return TRUE;
41 }
42
43 static DWORD WINAPI create_window_thread_proc(void *param)
44 {
45     struct create_window_thread_param *p = param;
46     DWORD res;
47     BOOL ret;
48
49     p->window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
50             0, 0, 640, 480, 0, 0, 0, 0);
51     ret = SetEvent(p->window_created);
52     ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
53
54     for (;;)
55     {
56         MSG msg;
57
58         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
59             DispatchMessage(&msg);
60         res = WaitForSingleObject(p->destroy_window, 100);
61         if (res == WAIT_OBJECT_0)
62             break;
63         if (res != WAIT_TIMEOUT)
64         {
65             ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
66             break;
67         }
68     }
69
70     DestroyWindow(p->window);
71
72     return 0;
73 }
74
75 static void create_window_thread(struct create_window_thread_param *p)
76 {
77     DWORD res, tid;
78
79     p->window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
80     ok(!!p->window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
81     p->destroy_window = CreateEvent(NULL, FALSE, FALSE, NULL);
82     ok(!!p->destroy_window, "CreateEvent failed, last error %#x.\n", GetLastError());
83     p->thread = CreateThread(NULL, 0, create_window_thread_proc, p, 0, &tid);
84     ok(!!p->thread, "Failed to create thread, last error %#x.\n", GetLastError());
85     res = WaitForSingleObject(p->window_created, INFINITE);
86     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
87 }
88
89 static void destroy_window_thread(struct create_window_thread_param *p)
90 {
91     SetEvent(p->destroy_window);
92     WaitForSingleObject(p->thread, INFINITE);
93     CloseHandle(p->destroy_window);
94     CloseHandle(p->window_created);
95     CloseHandle(p->thread);
96 }
97
98 static D3DCOLOR get_surface_color(IDirectDrawSurface *surface, UINT x, UINT y)
99 {
100     RECT rect = {x, y, x + 1, y + 1};
101     DDSURFACEDESC surface_desc;
102     D3DCOLOR color;
103     HRESULT hr;
104
105     memset(&surface_desc, 0, sizeof(surface_desc));
106     surface_desc.dwSize = sizeof(surface_desc);
107
108     hr = IDirectDrawSurface_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
109     ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
110     if (FAILED(hr))
111         return 0xdeadbeef;
112
113     color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
114
115     hr = IDirectDrawSurface_Unlock(surface, NULL);
116     ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
117
118     return color;
119 }
120
121 static void emit_process_vertices(void **ptr, DWORD vertex_count)
122 {
123     D3DINSTRUCTION *inst = *ptr;
124     D3DPROCESSVERTICES *pv = (D3DPROCESSVERTICES *)(inst + 1);
125
126     inst->bOpcode = D3DOP_PROCESSVERTICES;
127     inst->bSize = sizeof(*pv);
128     inst->wCount = 1;
129
130     pv->dwFlags = D3DPROCESSVERTICES_COPY;
131     pv->wStart = 0;
132     pv->wDest = 0;
133     pv->dwCount = vertex_count;
134     pv->dwReserved = 0;
135
136     *ptr = pv + 1;
137 }
138
139 static void emit_set_rs(void **ptr, D3DRENDERSTATETYPE state, DWORD value)
140 {
141     D3DINSTRUCTION *inst = *ptr;
142     D3DSTATE *rs = (D3DSTATE *)(inst + 1);
143
144     inst->bOpcode = D3DOP_STATERENDER;
145     inst->bSize = sizeof(*rs);
146     inst->wCount = 1;
147
148     U1(*rs).drstRenderStateType = state;
149     U2(*rs).dwArg[0] = value;
150
151     *ptr = rs + 1;
152 }
153
154 static void emit_tquad(void **ptr, WORD base_idx)
155 {
156     D3DINSTRUCTION *inst = *ptr;
157     D3DTRIANGLE *tri = (D3DTRIANGLE *)(inst + 1);
158
159     inst->bOpcode = D3DOP_TRIANGLE;
160     inst->bSize = sizeof(*tri);
161     inst->wCount = 2;
162
163     tri->v1 = base_idx;
164     tri->v2 = base_idx + 1;
165     tri->v3 = base_idx + 2;
166     tri->wFlags = D3DTRIFLAG_START;
167     ++tri;
168
169     tri->v1 = base_idx + 2;
170     tri->v2 = base_idx + 1;
171     tri->v3 = base_idx + 3;
172     tri->wFlags = D3DTRIFLAG_ODD;
173     ++tri;
174
175     *ptr = tri;
176 }
177
178 static void emit_end(void **ptr)
179 {
180     D3DINSTRUCTION *inst = *ptr;
181
182     inst->bOpcode = D3DOP_EXIT;
183     inst->bSize = 0;
184     inst->wCount = 0;
185
186     *ptr = inst + 1;
187 }
188
189 static HRESULT CALLBACK enum_z_fmt(GUID *guid, char *description, char *name,
190         D3DDEVICEDESC *hal_desc, D3DDEVICEDESC *hel_desc, void *ctx)
191 {
192     DWORD *z_depth = ctx;
193
194     if (!IsEqualGUID(&IID_IDirect3DHALDevice, guid))
195         return D3DENUMRET_OK;
196
197     if (hal_desc->dwDeviceZBufferBitDepth & DDBD_32)
198         *z_depth = 32;
199     else if (hal_desc->dwDeviceZBufferBitDepth & DDBD_24)
200         *z_depth = 24;
201     else if (hal_desc->dwDeviceZBufferBitDepth & DDBD_16)
202         *z_depth = 16;
203
204     return DDENUMRET_OK;
205 }
206
207 static IDirectDraw *create_ddraw(void)
208 {
209     IDirectDraw *ddraw;
210
211     if (FAILED(DirectDrawCreate(NULL, &ddraw, NULL)))
212         return NULL;
213
214     return ddraw;
215 }
216
217 static IDirect3DDevice *create_device(IDirectDraw *ddraw, HWND window, DWORD coop_level)
218 {
219     IDirectDrawSurface *surface, *ds;
220     IDirect3DDevice *device = NULL;
221     DDSURFACEDESC surface_desc;
222     DWORD z_depth = 0;
223     IDirect3D *d3d;
224     HRESULT hr;
225
226     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, coop_level);
227     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
228
229     memset(&surface_desc, 0, sizeof(surface_desc));
230     surface_desc.dwSize = sizeof(surface_desc);
231     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
232     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
233     surface_desc.dwWidth = 640;
234     surface_desc.dwHeight = 480;
235
236     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
237     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
238
239     if (coop_level & DDSCL_NORMAL)
240     {
241         IDirectDrawClipper *clipper;
242
243         hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
244         ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
245         hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
246         ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
247         hr = IDirectDrawSurface_SetClipper(surface, clipper);
248         ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
249         IDirectDrawClipper_Release(clipper);
250     }
251
252     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
253     if (FAILED(hr))
254     {
255         IDirectDrawSurface_Release(surface);
256         return NULL;
257     }
258
259     hr = IDirect3D_EnumDevices(d3d, enum_z_fmt, &z_depth);
260     ok(SUCCEEDED(hr), "Failed to enumerate z-formats, hr %#x.\n", hr);
261     IDirect3D_Release(d3d);
262     if (FAILED(hr) || !z_depth)
263     {
264         IDirectDrawSurface_Release(surface);
265         return NULL;
266     }
267
268     memset(&surface_desc, 0, sizeof(surface_desc));
269     surface_desc.dwSize = sizeof(surface_desc);
270     surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
271     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
272     U2(surface_desc).dwZBufferBitDepth = z_depth;
273     surface_desc.dwWidth = 640;
274     surface_desc.dwHeight = 480;
275     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
276     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
277     if (FAILED(hr))
278     {
279         IDirectDrawSurface_Release(surface);
280         return NULL;
281     }
282
283     hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
284     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
285     IDirectDrawSurface_Release(ds);
286     if (FAILED(hr))
287     {
288         IDirectDrawSurface_Release(surface);
289         return NULL;
290     }
291
292     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DHALDevice, (void **)&device);
293     IDirectDrawSurface_Release(surface);
294     if (FAILED(hr))
295         return NULL;
296
297     return device;
298 }
299
300 static HRESULT CALLBACK restore_callback(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
301 {
302     HRESULT hr = IDirectDrawSurface_Restore(surface);
303     ok(SUCCEEDED(hr), "Failed to restore surface, hr %#x.\n", hr);
304     IDirectDrawSurface_Release(surface);
305
306     return DDENUMRET_OK;
307 }
308
309 static HRESULT restore_surfaces(IDirectDraw *ddraw)
310 {
311     return IDirectDraw_EnumSurfaces(ddraw, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
312             NULL, NULL, restore_callback);
313 }
314
315 static void test_coop_level_create_device_window(void)
316 {
317     HWND focus_window, device_window;
318     IDirectDraw *ddraw;
319     HRESULT hr;
320
321     focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
322             0, 0, 640, 480, 0, 0, 0, 0);
323     if (!(ddraw = create_ddraw()))
324     {
325         skip("Failed to create a ddraw object, skipping test.\n");
326         DestroyWindow(focus_window);
327         return;
328     }
329
330     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
331     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
332     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
333     ok(!device_window, "Unexpected device window found.\n");
334     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
335     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
336     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
337     ok(!device_window, "Unexpected device window found.\n");
338     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
339     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
340     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
341     ok(!device_window, "Unexpected device window found.\n");
342     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
343     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
344     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
345     ok(!device_window, "Unexpected device window found.\n");
346     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
347     ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
348     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
349     ok(!device_window, "Unexpected device window found.\n");
350
351     /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
352     if (broken(hr == DDERR_INVALIDPARAMS))
353     {
354         win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
355         IDirectDraw_Release(ddraw);
356         DestroyWindow(focus_window);
357         return;
358     }
359
360     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
361     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
362     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
363     ok(!device_window, "Unexpected device window found.\n");
364     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
365     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
366     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
367     ok(!device_window, "Unexpected device window found.\n");
368
369     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
370     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
371     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
372     ok(!device_window, "Unexpected device window found.\n");
373     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
374             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
375     ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
376     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
377     ok(!!device_window, "Device window not found.\n");
378
379     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
380     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
381     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
382     ok(!device_window, "Unexpected device window found.\n");
383     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
384             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
385     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
386     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
387     ok(!!device_window, "Device window not found.\n");
388
389     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
390     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
391     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
392     ok(!device_window, "Unexpected device window found.\n");
393     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
394     ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
395     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
396     ok(!device_window, "Unexpected device window found.\n");
397     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
398     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
399     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
400     ok(!device_window, "Unexpected device window found.\n");
401     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
402     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
403     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
404     ok(!!device_window, "Device window not found.\n");
405
406     IDirectDraw_Release(ddraw);
407     DestroyWindow(focus_window);
408 }
409
410 static void test_clipper_blt(void)
411 {
412     IDirectDrawSurface *src_surface, *dst_surface;
413     RECT client_rect, src_rect, *rect;
414     IDirectDrawClipper *clipper;
415     DDSURFACEDESC surface_desc;
416     unsigned int i, j, x, y;
417     IDirectDraw *ddraw;
418     RGNDATA *rgn_data;
419     D3DCOLOR color;
420     HRGN r1, r2;
421     HWND window;
422     DDBLTFX fx;
423     HRESULT hr;
424     DWORD *ptr;
425     DWORD ret;
426
427     static const DWORD src_data[] =
428     {
429         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
430         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
431         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
432     };
433     static const D3DCOLOR expected1[] =
434     {
435         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
436         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
437         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
438         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
439     };
440     static const D3DCOLOR expected2[] =
441     {
442         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
443         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
444         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
445         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
446     };
447
448     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
449             10, 10, 640, 480, 0, 0, 0, 0);
450     ShowWindow(window, SW_SHOW);
451     if (!(ddraw = create_ddraw()))
452     {
453         skip("Failed to create a ddraw object, skipping test.\n");
454         DestroyWindow(window);
455         return;
456     }
457
458     ret = GetClientRect(window, &client_rect);
459     ok(ret, "Failed to get client rect.\n");
460     ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
461     ok(ret, "Failed to map client rect.\n");
462
463     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
464     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
465
466     hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
467     ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
468     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
469     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
470     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
471     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
472     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
473     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
474     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
475     hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
476     ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
477     ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
478     ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
479     ok(rgn_data->rdh.nCount == 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
480     ok(rgn_data->rdh.nRgnSize == 16 || broken(rgn_data->rdh.nRgnSize == 168 /* NT4 */),
481             "Got unexpected region size %u.\n", rgn_data->rdh.nRgnSize);
482     ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
483             "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
484             rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
485             rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
486             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
487     rect = (RECT *)&rgn_data->Buffer[0];
488     ok(EqualRect(rect, &client_rect),
489             "Got unexpected clip rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
490             rect->left, rect->top, rect->right, rect->bottom,
491             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
492     HeapFree(GetProcessHeap(), 0, rgn_data);
493
494     r1 = CreateRectRgn(0, 0, 320, 240);
495     ok(!!r1, "Failed to create region.\n");
496     r2 = CreateRectRgn(320, 240, 640, 480);
497     ok(!!r2, "Failed to create region.\n");
498     CombineRgn(r1, r1, r2, RGN_OR);
499     ret = GetRegionData(r1, 0, NULL);
500     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
501     ret = GetRegionData(r1, ret, rgn_data);
502     ok(!!ret, "Failed to get region data.\n");
503
504     DeleteObject(r2);
505     DeleteObject(r1);
506
507     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
508     ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
509     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
510     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
511     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
512     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
513
514     HeapFree(GetProcessHeap(), 0, rgn_data);
515
516     memset(&surface_desc, 0, sizeof(surface_desc));
517     surface_desc.dwSize = sizeof(surface_desc);
518     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
519     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
520     surface_desc.dwWidth = 640;
521     surface_desc.dwHeight = 480;
522     surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
523     surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
524     U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
525     U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
526     U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
527     U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
528
529     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
530     ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
531     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
532     ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
533
534     memset(&fx, 0, sizeof(fx));
535     fx.dwSize = sizeof(fx);
536     hr = IDirectDrawSurface_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
537     ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
538     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
539     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
540
541     hr = IDirectDrawSurface_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
542     ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
543     ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
544     ptr = surface_desc.lpSurface;
545     memcpy(&ptr[   0], &src_data[ 0], 6 * sizeof(DWORD));
546     memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
547     memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
548     hr = IDirectDrawSurface_Unlock(src_surface, NULL);
549     ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
550
551     hr = IDirectDrawSurface_SetClipper(dst_surface, clipper);
552     ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
553
554     SetRect(&src_rect, 1, 1, 5, 2);
555     hr = IDirectDrawSurface_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
556     ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
557     for (i = 0; i < 4; ++i)
558     {
559         for (j = 0; j < 4; ++j)
560         {
561             x = 80 * ((2 * j) + 1);
562             y = 60 * ((2 * i) + 1);
563             color = get_surface_color(dst_surface, x, y);
564             ok(compare_color(color, expected1[i * 4 + j], 1),
565                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
566         }
567     }
568
569     U5(fx).dwFillColor = 0xff0000ff;
570     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
571     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
572     for (i = 0; i < 4; ++i)
573     {
574         for (j = 0; j < 4; ++j)
575         {
576             x = 80 * ((2 * j) + 1);
577             y = 60 * ((2 * i) + 1);
578             color = get_surface_color(dst_surface, x, y);
579             ok(compare_color(color, expected2[i * 4 + j], 1),
580                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
581         }
582     }
583
584     hr = IDirectDrawSurface_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
585     ok(hr == DDERR_BLTFASTCANTCLIP || broken(hr == E_NOTIMPL /* NT4 */), "Got unexpected hr %#x.\n", hr);
586
587     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
588     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
589     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
590     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
591     DestroyWindow(window);
592     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
593     ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
594     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
595     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
596     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
597     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
598     hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
599     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
600     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
601     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
602     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
603     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
604
605     IDirectDrawSurface_Release(dst_surface);
606     IDirectDrawSurface_Release(src_surface);
607     IDirectDrawClipper_Release(clipper);
608     IDirectDraw_Release(ddraw);
609 }
610
611 static void test_coop_level_d3d_state(void)
612 {
613     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
614     D3DMATERIALHANDLE background_handle;
615     IDirectDrawSurface *rt, *surface;
616     IDirect3DMaterial *background;
617     IDirect3DViewport *viewport;
618     IDirect3DDevice *device;
619     D3DMATERIAL material;
620     IDirectDraw *ddraw;
621     D3DVIEWPORT vp;
622     IDirect3D *d3d;
623     D3DCOLOR color;
624     HWND window;
625     HRESULT hr;
626
627     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
628             0, 0, 640, 480, 0, 0, 0, 0);
629     if (!(ddraw = create_ddraw()))
630     {
631         skip("Failed to create ddraw object, skipping test.\n");
632         DestroyWindow(window);
633         return;
634     }
635     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
636     {
637         skip("Failed to create D3D device, skipping test.\n");
638         IDirectDraw_Release(ddraw);
639         DestroyWindow(window);
640         return;
641     }
642
643     hr = IDirect3DDevice_GetDirect3D(device, &d3d);
644     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
645     hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
646     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
647     hr = IDirect3D_CreateMaterial(d3d, &background, NULL);
648     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
649     IDirect3D_Release(d3d);
650
651     hr = IDirect3DDevice_AddViewport(device, viewport);
652     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
653     memset(&vp, 0, sizeof(vp));
654     vp.dwSize = sizeof(vp);
655     vp.dwX = 0;
656     vp.dwY = 0;
657     vp.dwWidth = 640;
658     vp.dwHeight = 480;
659     vp.dvScaleX = 320.0f;
660     vp.dvScaleY = 240.0f;
661     vp.dvMaxX = 1.0f;
662     vp.dvMaxY = 1.0f;
663     vp.dvMinZ = 0.0f;
664     vp.dvMaxZ = 1.0f;
665     hr = IDirect3DViewport_SetViewport(viewport, &vp);
666     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
667
668     memset(&material, 0, sizeof(material));
669     material.dwSize = sizeof(material);
670     U1(U(material).diffuse).r = 1.0f;
671     U2(U(material).diffuse).g = 0.0f;
672     U3(U(material).diffuse).b = 0.0f;
673     U4(U(material).diffuse).a = 1.0f;
674     hr = IDirect3DMaterial_SetMaterial(background, &material);
675     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
676     hr = IDirect3DMaterial_GetHandle(background, device, &background_handle);
677     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
678     hr = IDirect3DViewport_SetBackground(viewport, background_handle);
679     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
680
681     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
682     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
683     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
684     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
685     color = get_surface_color(rt, 320, 240);
686     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
687
688     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
689     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
690     hr = IDirectDrawSurface_IsLost(rt);
691     ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
692     hr = restore_surfaces(ddraw);
693     ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
694
695     memset(&material, 0, sizeof(material));
696     material.dwSize = sizeof(material);
697     U1(U(material).diffuse).r = 0.0f;
698     U2(U(material).diffuse).g = 1.0f;
699     U3(U(material).diffuse).b = 0.0f;
700     U4(U(material).diffuse).a = 1.0f;
701     hr = IDirect3DMaterial_SetMaterial(background, &material);
702     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
703
704     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&surface);
705     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
706     ok(surface == rt, "Got unexpected surface %p.\n", surface);
707     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
708     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
709     color = get_surface_color(rt, 320, 240);
710     ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
711
712     hr = IDirect3DDevice_DeleteViewport(device, viewport);
713     ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
714     IDirect3DMaterial_Release(background);
715     IDirect3DViewport_Release(viewport);
716     IDirectDrawSurface_Release(surface);
717     IDirectDrawSurface_Release(rt);
718     IDirect3DDevice_Release(device);
719     IDirectDraw_Release(ddraw);
720     DestroyWindow(window);
721 }
722
723 static void test_surface_interface_mismatch(void)
724 {
725     IDirectDraw *ddraw = NULL;
726     IDirect3D *d3d = NULL;
727     IDirectDrawSurface *surface = NULL, *ds;
728     IDirectDrawSurface3 *surface3 = NULL;
729     IDirect3DDevice *device = NULL;
730     IDirect3DViewport *viewport = NULL;
731     IDirect3DMaterial *background = NULL;
732     DDSURFACEDESC surface_desc;
733     DWORD z_depth = 0;
734     ULONG refcount;
735     HRESULT hr;
736     D3DCOLOR color;
737     HWND window;
738     D3DVIEWPORT vp;
739     D3DMATERIAL material;
740     D3DMATERIALHANDLE background_handle;
741     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
742
743     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
744             0, 0, 640, 480, 0, 0, 0, 0);
745
746     if (!(ddraw = create_ddraw()))
747     {
748         skip("Failed to create a ddraw object, skipping test.\n");
749         goto cleanup;
750     }
751
752     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
753     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
754
755     memset(&surface_desc, 0, sizeof(surface_desc));
756     surface_desc.dwSize = sizeof(surface_desc);
757     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
758     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
759     surface_desc.dwWidth = 640;
760     surface_desc.dwHeight = 480;
761
762     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
763     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
764
765     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
766     if (FAILED(hr))
767     {
768         skip("Failed to get the IDirectDrawSurface3 interface, skipping test.\n");
769         goto cleanup;
770     }
771
772     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
773     if (FAILED(hr))
774     {
775         skip("Failed to get the IDirect3D interface, skipping test.\n");
776         goto cleanup;
777     }
778
779     hr = IDirect3D_EnumDevices(d3d, enum_z_fmt, &z_depth);
780     if (FAILED(hr) || !z_depth)
781     {
782         skip("No depth buffer formats available, skipping test.\n");
783         goto cleanup;
784     }
785
786     memset(&surface_desc, 0, sizeof(surface_desc));
787     surface_desc.dwSize = sizeof(surface_desc);
788     surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
789     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
790     U2(surface_desc).dwZBufferBitDepth = z_depth;
791     surface_desc.dwWidth = 640;
792     surface_desc.dwHeight = 480;
793     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
794     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
795     if (FAILED(hr))
796         goto cleanup;
797
798     /* Using a different surface interface version still works */
799     hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
800     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
801     refcount = IDirectDrawSurface_Release(ds);
802     ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
803     if (FAILED(hr))
804         goto cleanup;
805
806     /* Here too */
807     hr = IDirectDrawSurface3_QueryInterface(surface3, &IID_IDirect3DHALDevice, (void **)&device);
808     ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
809     if (FAILED(hr))
810         goto cleanup;
811
812     hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
813     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
814     hr = IDirect3D_CreateMaterial(d3d, &background, NULL);
815     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
816
817     hr = IDirect3DDevice_AddViewport(device, viewport);
818     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
819     memset(&vp, 0, sizeof(vp));
820     vp.dwSize = sizeof(vp);
821     vp.dwX = 0;
822     vp.dwY = 0;
823     vp.dwWidth = 640;
824     vp.dwHeight = 480;
825     vp.dvScaleX = 320.0f;
826     vp.dvScaleY = 240.0f;
827     vp.dvMaxX = 1.0f;
828     vp.dvMaxY = 1.0f;
829     vp.dvMinZ = 0.0f;
830     vp.dvMaxZ = 1.0f;
831     hr = IDirect3DViewport_SetViewport(viewport, &vp);
832     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
833
834     memset(&material, 0, sizeof(material));
835     material.dwSize = sizeof(material);
836     U1(U(material).diffuse).r = 1.0f;
837     U2(U(material).diffuse).g = 0.0f;
838     U3(U(material).diffuse).b = 0.0f;
839     U4(U(material).diffuse).a = 1.0f;
840     hr = IDirect3DMaterial_SetMaterial(background, &material);
841     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
842     hr = IDirect3DMaterial_GetHandle(background, device, &background_handle);
843     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
844     hr = IDirect3DViewport_SetBackground(viewport, background_handle);
845     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
846
847     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
848     ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
849     color = get_surface_color(surface, 320, 240);
850     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
851
852 cleanup:
853     if (viewport)
854     {
855         IDirect3DDevice_DeleteViewport(device, viewport);
856         IDirect3DViewport_Release(viewport);
857     }
858     if (background) IDirect3DMaterial_Release(background);
859     if (surface3) IDirectDrawSurface3_Release(surface3);
860     if (surface) IDirectDrawSurface_Release(surface);
861     if (device) IDirect3DDevice_Release(device);
862     if (d3d) IDirect3D_Release(d3d);
863     if (ddraw) IDirectDraw_Release(ddraw);
864     DestroyWindow(window);
865 }
866
867 static void test_coop_level_threaded(void)
868 {
869     struct create_window_thread_param p;
870     IDirectDraw *ddraw;
871     HRESULT hr;
872
873     if (!(ddraw = create_ddraw()))
874     {
875         skip("Failed to create a ddraw object, skipping test.\n");
876         return;
877     }
878     create_window_thread(&p);
879
880     hr = IDirectDraw_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
881     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
882
883     IDirectDraw_Release(ddraw);
884     destroy_window_thread(&p);
885 }
886
887 static LONG get_refcount(IUnknown *test_iface)
888 {
889     IUnknown_AddRef(test_iface);
890     return IUnknown_Release(test_iface);
891 }
892
893 static void test_viewport_interfaces(void)
894 {
895     IDirectDraw *ddraw;
896     IDirect3D *d3d;
897     HRESULT hr;
898     LONG ref;
899     IDirect3DViewport *viewport;
900     IDirect3DViewport2 *viewport2;
901     IDirect3DViewport3 *viewport3;
902     IDirectDrawGammaControl *gamma;
903     IUnknown *unknown;
904
905     if (!(ddraw = create_ddraw()))
906     {
907         skip("Failed to create ddraw object, skipping test.\n");
908         return;
909     }
910     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
911     ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get d3d interface, hr %#x.\n", hr);
912     if (FAILED(hr))
913     {
914         skip("Direct3D not available, skipping tests\n");
915         IDirectDraw_Release(ddraw);
916         return;
917     }
918     ref = get_refcount((IUnknown *)d3d);
919     ok(ref == 2, "IDirect3D refcount is %d\n", ref);
920
921     hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
922     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
923     ref = get_refcount((IUnknown *)viewport);
924     ok(ref == 1, "Initial IDirect3DViewport refcount is %d\n", ref);
925     ref = get_refcount((IUnknown *)d3d);
926     ok(ref == 2, "IDirect3D refcount is %d\n", ref);
927
928     /* E_FAIL return values are returned by Winetestbot Windows NT machines. While not supporting
929      * newer interfaces is legitimate for old ddraw versions, E_FAIL violates Microsoft's rules
930      * for QueryInterface, hence the broken() */
931     gamma = (IDirectDrawGammaControl *)0xdeadbeef;
932     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirectDrawGammaControl, (void **)&gamma);
933     ok(hr == E_NOINTERFACE || broken(hr == E_FAIL), "Got unexpected hr %#x.\n", hr);
934     ok(gamma == NULL, "Interface not set to NULL by failed QI call: %p\n", gamma);
935     if (SUCCEEDED(hr)) IDirectDrawGammaControl_Release(gamma);
936     /* NULL iid: Segfaults */
937
938     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirect3DViewport2, (void **)&viewport2);
939     ok(SUCCEEDED(hr) || broken(hr == E_FAIL), "Failed to QI IDirect3DViewport2, hr %#x.\n", hr);
940     if (viewport2)
941     {
942         ref = get_refcount((IUnknown *)viewport);
943         ok(ref == 2, "IDirect3DViewport refcount is %d\n", ref);
944         ref = get_refcount((IUnknown *)viewport2);
945         ok(ref == 2, "IDirect3DViewport2 refcount is %d\n", ref);
946         IDirect3DViewport2_Release(viewport2);
947         viewport2 = NULL;
948     }
949
950     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirect3DViewport3, (void **)&viewport3);
951     ok(SUCCEEDED(hr) || broken(hr == E_FAIL), "Failed to QI IDirect3DViewport3, hr %#x.\n", hr);
952     if (viewport3)
953     {
954         ref = get_refcount((IUnknown *)viewport);
955         ok(ref == 2, "IDirect3DViewport refcount is %d\n", ref);
956         ref = get_refcount((IUnknown *)viewport3);
957         ok(ref == 2, "IDirect3DViewport3 refcount is %d\n", ref);
958         IDirect3DViewport3_Release(viewport3);
959     }
960
961     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IUnknown, (void **)&unknown);
962     ok(SUCCEEDED(hr), "Failed to QI IUnknown, hr %#x.\n", hr);
963     if (unknown)
964     {
965         ref = get_refcount((IUnknown *)viewport);
966         ok(ref == 2, "IDirect3DViewport refcount is %d\n", ref);
967         ref = get_refcount(unknown);
968         ok(ref == 2, "IUnknown refcount is %d\n", ref);
969         IUnknown_Release(unknown);
970     }
971
972     IDirect3DViewport_Release(viewport);
973     IDirect3D_Release(d3d);
974     IDirectDraw_Release(ddraw);
975 }
976
977 static void test_zenable(void)
978 {
979     static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
980     static D3DTLVERTEX tquad[] =
981     {
982         {{  0.0f}, {480.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
983         {{  0.0f}, {  0.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
984         {{640.0f}, {480.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
985         {{640.0f}, {  0.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
986     };
987     IDirect3DExecuteBuffer *execute_buffer;
988     D3DMATERIALHANDLE background_handle;
989     D3DEXECUTEBUFFERDESC exec_desc;
990     IDirect3DMaterial *background;
991     IDirect3DViewport *viewport;
992     D3DEXECUTEDATA exec_data;
993     IDirect3DDevice *device;
994     IDirectDrawSurface *rt;
995     D3DMATERIAL material;
996     IDirectDraw *ddraw;
997     UINT inst_length;
998     D3DVIEWPORT vp;
999     IDirect3D *d3d;
1000     D3DCOLOR color;
1001     HWND window;
1002     HRESULT hr;
1003     UINT x, y;
1004     UINT i, j;
1005     void *ptr;
1006
1007     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1008             0, 0, 640, 480, 0, 0, 0, 0);
1009     if (!(ddraw = create_ddraw()))
1010     {
1011         skip("Failed to create ddraw object, skipping test.\n");
1012         DestroyWindow(window);
1013         return;
1014     }
1015     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1016     {
1017         skip("Failed to create D3D device, skipping test.\n");
1018         IDirectDraw_Release(ddraw);
1019         DestroyWindow(window);
1020         return;
1021     }
1022
1023     hr = IDirect3DDevice_GetDirect3D(device, &d3d);
1024     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1025     hr = IDirect3D_CreateMaterial(d3d, &background, NULL);
1026     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
1027     hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
1028     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1029
1030     hr = IDirect3DDevice_AddViewport(device, viewport);
1031     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
1032     memset(&vp, 0, sizeof(vp));
1033     vp.dwSize = sizeof(vp);
1034     vp.dwX = 0;
1035     vp.dwY = 0;
1036     vp.dwWidth = 640;
1037     vp.dwHeight = 480;
1038     vp.dvScaleX = 320.0f;
1039     vp.dvScaleY = 240.0f;
1040     vp.dvMaxX = 1.0f;
1041     vp.dvMaxY = 1.0f;
1042     vp.dvMinZ = 0.0f;
1043     vp.dvMaxZ = 1.0f;
1044     hr = IDirect3DViewport_SetViewport(viewport, &vp);
1045     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
1046
1047     memset(&material, 0, sizeof(material));
1048     material.dwSize = sizeof(material);
1049     U1(U(material).diffuse).r = 1.0f;
1050     U2(U(material).diffuse).g = 0.0f;
1051     U3(U(material).diffuse).b = 0.0f;
1052     U4(U(material).diffuse).a = 1.0f;
1053     hr = IDirect3DMaterial_SetMaterial(background, &material);
1054     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
1055     hr = IDirect3DMaterial_GetHandle(background, device, &background_handle);
1056     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
1057     hr = IDirect3DViewport_SetBackground(viewport, background_handle);
1058     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
1059
1060     memset(&exec_desc, 0, sizeof(exec_desc));
1061     exec_desc.dwSize = sizeof(exec_desc);
1062     exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1063     exec_desc.dwBufferSize = 1024;
1064     exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1065
1066     hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1067     ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1068     hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1069     ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1070     memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1071     ptr = ((BYTE *)exec_desc.lpData) + sizeof(tquad);
1072     emit_process_vertices(&ptr, 4);
1073     emit_set_rs(&ptr, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
1074     emit_tquad(&ptr, 0);
1075     emit_end(&ptr);
1076     inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
1077     inst_length -= sizeof(tquad);
1078     hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1079     ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1080
1081     memset(&exec_data, 0, sizeof(exec_data));
1082     exec_data.dwSize = sizeof(exec_data);
1083     exec_data.dwVertexCount = 4;
1084     exec_data.dwInstructionOffset = sizeof(tquad);
1085     exec_data.dwInstructionLength = inst_length;
1086     hr = IDirect3DExecuteBuffer_SetExecuteData(execute_buffer, &exec_data);
1087     ok(SUCCEEDED(hr), "Failed to set execute data, hr %#x.\n", hr);
1088
1089     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1090     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1091     hr = IDirect3DDevice_BeginScene(device);
1092     ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1093     hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1094     ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1095     hr = IDirect3DDevice_EndScene(device);
1096     ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1097
1098     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1099     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1100     for (i = 0; i < 4; ++i)
1101     {
1102         for (j = 0; j < 4; ++j)
1103         {
1104             x = 80 * ((2 * j) + 1);
1105             y = 60 * ((2 * i) + 1);
1106             color = get_surface_color(rt, x, y);
1107             ok(compare_color(color, 0x0000ff00, 1),
1108                     "Expected color 0x0000ff00 at %u, %u, got 0x%08x.\n", x, y, color);
1109         }
1110     }
1111     IDirectDrawSurface_Release(rt);
1112
1113     IDirect3DExecuteBuffer_Release(execute_buffer);
1114     IDirect3DMaterial_Release(background);
1115     IDirect3DViewport_Release(viewport);
1116     IDirect3D_Release(d3d);
1117     IDirect3DDevice_Release(device);
1118     IDirectDraw_Release(ddraw);
1119     DestroyWindow(window);
1120 }
1121
1122 START_TEST(ddraw1)
1123 {
1124     test_coop_level_create_device_window();
1125     test_clipper_blt();
1126     test_coop_level_d3d_state();
1127     test_surface_interface_mismatch();
1128     test_coop_level_threaded();
1129     test_viewport_interfaces();
1130     test_zenable();
1131 }