winealsa: Map ALSA errors to AUDCLNT_E_*.
[wine] / dlls / ddraw / tests / ddraw4.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
19 #include "wine/test.h"
20 #include <limits.h>
21 #include "d3d.h"
22
23 struct vec3
24 {
25     float x, y, z;
26 };
27
28 struct vec4
29 {
30     float x, y, z, w;
31 };
32
33 static BOOL compare_float(float f, float g, unsigned int ulps)
34 {
35     int x = *(int *)&f;
36     int y = *(int *)&g;
37
38     if (x < 0)
39         x = INT_MIN - x;
40     if (y < 0)
41         y = INT_MIN - y;
42
43     if (abs(x - y) > ulps)
44         return FALSE;
45
46     return TRUE;
47 }
48
49 static BOOL compare_vec4(struct vec4 *vec, float x, float y, float z, float w, unsigned int ulps)
50 {
51     return compare_float(vec->x, x, ulps)
52             && compare_float(vec->y, y, ulps)
53             && compare_float(vec->z, z, ulps)
54             && compare_float(vec->w, w, ulps);
55 }
56
57 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
58 {
59     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
60     c1 >>= 8; c2 >>= 8;
61     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
62     c1 >>= 8; c2 >>= 8;
63     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
64     c1 >>= 8; c2 >>= 8;
65     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
66     return TRUE;
67 }
68
69 static D3DCOLOR get_surface_color(IDirectDrawSurface4 *surface, UINT x, UINT y)
70 {
71     RECT rect = {x, y, x + 1, y + 1};
72     DDSURFACEDESC2 surface_desc;
73     D3DCOLOR color;
74     HRESULT hr;
75
76     memset(&surface_desc, 0, sizeof(surface_desc));
77     surface_desc.dwSize = sizeof(surface_desc);
78
79     hr = IDirectDrawSurface4_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
80     ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
81     if (FAILED(hr))
82         return 0xdeadbeef;
83
84     color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
85
86     hr = IDirectDrawSurface4_Unlock(surface, &rect);
87     ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
88
89     return color;
90 }
91
92 static HRESULT CALLBACK enum_z_fmt(DDPIXELFORMAT *format, void *ctx)
93 {
94     DDPIXELFORMAT *z_fmt = ctx;
95
96     if (U1(*format).dwZBufferBitDepth > U1(*z_fmt).dwZBufferBitDepth)
97         *z_fmt = *format;
98
99     return DDENUMRET_OK;
100 }
101
102 static IDirectDraw4 *create_ddraw(void)
103 {
104     IDirectDraw4 *ddraw4;
105     IDirectDraw *ddraw1;
106     HRESULT hr;
107
108     if (FAILED(DirectDrawCreate(NULL, &ddraw1, NULL)))
109         return NULL;
110
111     hr = IDirectDraw_QueryInterface(ddraw1, &IID_IDirectDraw4, (void **)&ddraw4);
112     IDirectDraw_Release(ddraw1);
113     if (FAILED(hr))
114         return NULL;
115
116     return ddraw4;
117 }
118
119 static IDirect3DDevice3 *create_device(HWND window, DWORD coop_level)
120 {
121     IDirectDrawSurface4 *surface, *ds;
122     IDirect3DDevice3 *device = NULL;
123     DDSURFACEDESC2 surface_desc;
124     IDirectDraw4 *ddraw4;
125     DDPIXELFORMAT z_fmt;
126     IDirect3D3 *d3d3;
127     HRESULT hr;
128
129     if (!(ddraw4 = create_ddraw()))
130         return NULL;
131
132     hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, coop_level);
133     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
134
135     memset(&surface_desc, 0, sizeof(surface_desc));
136     surface_desc.dwSize = sizeof(surface_desc);
137     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
138     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
139     surface_desc.dwWidth = 640;
140     surface_desc.dwHeight = 480;
141
142     hr = IDirectDraw4_CreateSurface(ddraw4, &surface_desc, &surface, NULL);
143     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
144
145     if (coop_level & DDSCL_NORMAL)
146     {
147         IDirectDrawClipper *clipper;
148
149         hr = IDirectDraw4_CreateClipper(ddraw4, 0, &clipper, NULL);
150         ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
151         hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
152         ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
153         hr = IDirectDrawSurface4_SetClipper(surface, clipper);
154         ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
155         IDirectDrawClipper_Release(clipper);
156     }
157
158     hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirect3D3, (void **)&d3d3);
159     IDirectDraw4_Release(ddraw4);
160     if (FAILED(hr))
161     {
162         IDirectDrawSurface4_Release(surface);
163         return NULL;
164     }
165
166     memset(&z_fmt, 0, sizeof(z_fmt));
167     hr = IDirect3D3_EnumZBufferFormats(d3d3, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
168     if (FAILED(hr) || !z_fmt.dwSize)
169     {
170         IDirect3D3_Release(d3d3);
171         IDirectDrawSurface4_Release(surface);
172         return NULL;
173     }
174
175     memset(&surface_desc, 0, sizeof(surface_desc));
176     surface_desc.dwSize = sizeof(surface_desc);
177     surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
178     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
179     U4(surface_desc).ddpfPixelFormat = z_fmt;
180     surface_desc.dwWidth = 640;
181     surface_desc.dwHeight = 480;
182     hr = IDirectDraw4_CreateSurface(ddraw4, &surface_desc, &ds, NULL);
183     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
184     if (FAILED(hr))
185     {
186         IDirect3D3_Release(d3d3);
187         IDirectDrawSurface4_Release(surface);
188         return NULL;
189     }
190
191     hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
192     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
193     IDirectDrawSurface4_Release(ds);
194     if (FAILED(hr))
195     {
196         IDirect3D3_Release(d3d3);
197         IDirectDrawSurface4_Release(surface);
198         return NULL;
199     }
200
201     hr = IDirect3D3_CreateDevice(d3d3, &IID_IDirect3DHALDevice, surface, &device, NULL);
202     IDirect3D3_Release(d3d3);
203     IDirectDrawSurface4_Release(surface);
204     if (FAILED(hr))
205         return NULL;
206
207     return device;
208 }
209
210 static void test_process_vertices(void)
211 {
212     IDirect3DVertexBuffer *src_vb, *dst_vb;
213     IDirect3DViewport3 *viewport;
214     D3DVERTEXBUFFERDESC vb_desc;
215     IDirect3DDevice3 *device;
216     struct vec3 *src_data;
217     struct vec4 *dst_data;
218     IDirect3D3 *d3d3;
219     D3DVIEWPORT2 vp2;
220     D3DVIEWPORT vp1;
221     HWND window;
222     HRESULT hr;
223
224     static D3DMATRIX identity =
225     {
226         1.0f, 0.0f, 0.0f, 0.0f,
227         0.0f, 1.0f, 0.0f, 0.0f,
228         0.0f, 0.0f, 1.0f, 0.0f,
229         0.0f, 0.0f, 0.0f, 1.0f,
230     };
231     static D3DMATRIX projection =
232     {
233         1.0f, 0.0f, 0.0f, 0.0f,
234         0.0f, 1.0f, 0.0f, 0.0f,
235         0.0f, 0.0f, 1.0f, 0.0f,
236         6.0f, 7.0f, 8.0f, 1.0f,
237     };
238
239     window = CreateWindowA("static", "d3d7_test", WS_OVERLAPPEDWINDOW,
240             0, 0, 640, 480, 0, 0, 0, 0);
241     if (!(device = create_device(window, DDSCL_NORMAL)))
242     {
243         skip("Failed to create a 3D device, skipping test.\n");
244         DestroyWindow(window);
245         return;
246     }
247
248     hr = IDirect3DDevice3_GetDirect3D(device, &d3d3);
249     ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
250
251     memset(&vb_desc, 0, sizeof(vb_desc));
252     vb_desc.dwSize = sizeof(vb_desc);
253     vb_desc.dwFVF = D3DFVF_XYZ;
254     vb_desc.dwNumVertices = 3;
255     hr = IDirect3D3_CreateVertexBuffer(d3d3, &vb_desc, &src_vb, 0, NULL);
256     ok(SUCCEEDED(hr), "Failed to create source vertex buffer, hr %#x.\n", hr);
257
258     hr = IDirect3DVertexBuffer_Lock(src_vb, DDLOCK_WRITEONLY, (void **)&src_data, NULL);
259     ok(SUCCEEDED(hr), "Failed to lock source vertex buffer, hr %#x.\n", hr);
260     src_data[0].x = -1.0f;
261     src_data[0].y = -1.0f;
262     src_data[0].z = -1.0f;
263     src_data[1].x = 0.0f;
264     src_data[1].y = 0.0f;
265     src_data[1].z = 0.0f;
266     src_data[2].x = 1.0f;
267     src_data[2].y = 1.0f;
268     src_data[2].z = 1.0f;
269     hr = IDirect3DVertexBuffer_Unlock(src_vb);
270     ok(SUCCEEDED(hr), "Failed to unlock source vertex buffer, hr %#x.\n", hr);
271
272     memset(&vb_desc, 0, sizeof(vb_desc));
273     vb_desc.dwSize = sizeof(vb_desc);
274     vb_desc.dwFVF = D3DFVF_XYZRHW;
275     vb_desc.dwNumVertices = 3;
276     hr = IDirect3D3_CreateVertexBuffer(d3d3, &vb_desc, &dst_vb, 0, NULL);
277     ok(SUCCEEDED(hr), "Failed to create destination vertex buffer, hr %#x.\n", hr);
278
279     hr = IDirect3D3_CreateViewport(d3d3, &viewport, NULL);
280     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
281     hr = IDirect3DDevice3_AddViewport(device, viewport);
282     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
283     vp2.dwSize = sizeof(vp2);
284     vp2.dwX = 10;
285     vp2.dwY = 20;
286     vp2.dwWidth = 100;
287     vp2.dwHeight = 200;
288     vp2.dvClipX = 2.0f;
289     vp2.dvClipY = 3.0f;
290     vp2.dvClipWidth = 4.0f;
291     vp2.dvClipHeight = 5.0f;
292     vp2.dvMinZ = -2.0f;
293     vp2.dvMaxZ = 3.0f;
294     hr = IDirect3DViewport3_SetViewport2(viewport, &vp2);
295     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
296     hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
297     ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
298
299     hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &identity);
300     ok(SUCCEEDED(hr), "Failed to set world transformation, hr %#x.\n", hr);
301     hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &identity);
302     ok(SUCCEEDED(hr), "Failed to set view transformation, hr %#x.\n", hr);
303     hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &identity);
304     ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
305
306     hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
307     ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
308
309     hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
310     ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
311     ok(compare_vec4(&dst_data[0], -6.500e+1f, +1.800e+2f, +2.000e-1f, +1.000e+0f, 4096),
312             "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
313             dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
314     ok(compare_vec4(&dst_data[1], -4.000e+1f, +1.400e+2f, +4.000e-1f, +1.000e+0f, 4096),
315             "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
316             dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
317     ok(compare_vec4(&dst_data[2], -1.500e+1f, +1.000e+2f, +6.000e-1f, +1.000e+0f, 4096),
318             "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
319             dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
320     hr = IDirect3DVertexBuffer_Unlock(dst_vb);
321     ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
322
323     hr = IDirect3DDevice3_MultiplyTransform(device, D3DTRANSFORMSTATE_PROJECTION, &projection);
324     ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
325
326     hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
327     ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
328
329     hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
330     ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
331     ok(compare_vec4(&dst_data[0], +8.500e+1f, -1.000e+2f, +1.800e+0f, +1.000e+0f, 4096),
332             "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
333             dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
334     ok(compare_vec4(&dst_data[1], +1.100e+2f, -1.400e+2f, +2.000e+0f, +1.000e+0f, 4096),
335             "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
336             dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
337     ok(compare_vec4(&dst_data[2], +1.350e+2f, -1.800e+2f, +2.200e+0f, +1.000e+0f, 4096),
338             "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
339             dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
340     hr = IDirect3DVertexBuffer_Unlock(dst_vb);
341     ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
342
343     vp2.dwSize = sizeof(vp2);
344     vp2.dwX = 30;
345     vp2.dwY = 40;
346     vp2.dwWidth = 90;
347     vp2.dwHeight = 80;
348     vp2.dvClipX = 4.0f;
349     vp2.dvClipY = 6.0f;
350     vp2.dvClipWidth = 2.0f;
351     vp2.dvClipHeight = 4.0f;
352     vp2.dvMinZ = 3.0f;
353     vp2.dvMaxZ = -2.0f;
354     hr = IDirect3DViewport3_SetViewport2(viewport, &vp2);
355     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
356
357     hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
358     ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
359
360     hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
361     ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
362     ok(compare_vec4(&dst_data[0], +7.500e+1f, +4.000e+1f, -8.000e-1f, +1.000e+0f, 4096),
363             "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
364             dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
365     ok(compare_vec4(&dst_data[1], +1.200e+2f, +2.000e+1f, -1.000e+0f, +1.000e+0f, 4096),
366             "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
367             dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
368     ok(compare_vec4(&dst_data[2], +1.650e+2f, +0.000e+0f, -1.200e+0f, +1.000e+0f, 4096),
369             "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
370             dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
371     hr = IDirect3DVertexBuffer_Unlock(dst_vb);
372     ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
373
374     vp1.dwSize = sizeof(vp1);
375     vp1.dwX = 30;
376     vp1.dwY = 40;
377     vp1.dwWidth = 90;
378     vp1.dwHeight = 80;
379     vp1.dvScaleX = 7.0f;
380     vp1.dvScaleY = 2.0f;
381     vp1.dvMaxX = 6.0f;
382     vp1.dvMaxY = 10.0f;
383     vp1.dvMinZ = -2.0f;
384     vp1.dvMaxZ = 3.0f;
385     hr = IDirect3DViewport3_SetViewport(viewport, &vp1);
386     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
387
388     hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
389     ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
390
391     hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
392     ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
393     ok(compare_vec4(&dst_data[0], +1.100e+2f, +6.800e+1f, +7.000e+0f, +1.000e+0f, 4096),
394             "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
395             dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
396     ok(compare_vec4(&dst_data[1], +1.170e+2f, +6.600e+1f, +8.000e+0f, +1.000e+0f, 4096),
397             "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
398             dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
399     ok(compare_vec4(&dst_data[2], +1.240e+2f, +6.400e+1f, +9.000e+0f, +1.000e+0f, 4096),
400             "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
401             dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
402     hr = IDirect3DVertexBuffer_Unlock(dst_vb);
403     ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
404
405     hr = IDirect3DDevice3_DeleteViewport(device, viewport);
406     ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
407
408     IDirect3DVertexBuffer_Release(dst_vb);
409     IDirect3DVertexBuffer_Release(src_vb);
410     IDirect3DViewport3_Release(viewport);
411     IDirect3D3_Release(d3d3);
412     IDirect3DDevice3_Release(device);
413     DestroyWindow(window);
414 }
415
416 static void test_coop_level_create_device_window(void)
417 {
418     HWND focus_window, device_window;
419     IDirectDraw4 *ddraw;
420     HRESULT hr;
421
422     focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
423             0, 0, 640, 480, 0, 0, 0, 0);
424     if (!(ddraw = create_ddraw()))
425     {
426         skip("Failed to create a ddraw object, skipping test.\n");
427         DestroyWindow(focus_window);
428         return;
429     }
430
431     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
432     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
433     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
434     ok(!device_window, "Unexpected device window found.\n");
435     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
436     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
437     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
438     ok(!device_window, "Unexpected device window found.\n");
439     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
440     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
441     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
442     ok(!device_window, "Unexpected device window found.\n");
443     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
444     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
445     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
446     ok(!device_window, "Unexpected device window found.\n");
447     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
448     ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
449     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
450     ok(!device_window, "Unexpected device window found.\n");
451
452     /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
453     if (broken(hr == DDERR_INVALIDPARAMS))
454     {
455         win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
456         IDirectDraw4_Release(ddraw);
457         DestroyWindow(focus_window);
458         return;
459     }
460
461     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
462     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
463     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
464     ok(!device_window, "Unexpected device window found.\n");
465     hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
466     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
467     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
468     ok(!device_window, "Unexpected device window found.\n");
469
470     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
471     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
472     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
473     ok(!device_window, "Unexpected device window found.\n");
474     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
475             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
476     ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
477     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
478     ok(!!device_window, "Device window not found.\n");
479
480     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
481     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
482     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
483     ok(!device_window, "Unexpected device window found.\n");
484     hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
485             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
486     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
487     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
488     ok(!!device_window, "Device window not found.\n");
489
490     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
491     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
492     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
493     ok(!device_window, "Unexpected device window found.\n");
494     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
495     ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
496     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
497     ok(!device_window, "Unexpected device window found.\n");
498     hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
499     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
500     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
501     ok(!device_window, "Unexpected device window found.\n");
502     hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
503     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
504     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
505     ok(!!device_window, "Device window not found.\n");
506
507     IDirectDraw4_Release(ddraw);
508     DestroyWindow(focus_window);
509 }
510
511 static void test_clipper_blt(void)
512 {
513     IDirectDrawSurface4 *src_surface, *dst_surface;
514     RECT client_rect, src_rect, *rect;
515     IDirectDrawClipper *clipper;
516     DDSURFACEDESC2 surface_desc;
517     unsigned int i, j, x, y;
518     IDirectDraw4 *ddraw;
519     RGNDATA *rgn_data;
520     D3DCOLOR color;
521     HRGN r1, r2;
522     HWND window;
523     DDBLTFX fx;
524     HRESULT hr;
525     DWORD *ptr;
526     DWORD ret;
527
528     static const DWORD src_data[] =
529     {
530         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
531         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
532         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
533     };
534     static const D3DCOLOR expected1[] =
535     {
536         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
537         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
538         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
539         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
540     };
541     static const D3DCOLOR expected2[] =
542     {
543         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
544         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
545         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
546         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
547     };
548
549     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
550             10, 10, 640, 480, 0, 0, 0, 0);
551     ShowWindow(window, SW_SHOW);
552     if (!(ddraw = create_ddraw()))
553     {
554         skip("Failed to create a ddraw object, skipping test.\n");
555         DestroyWindow(window);
556         return;
557     }
558
559     ret = GetClientRect(window, &client_rect);
560     ok(ret, "Failed to get client rect.\n");
561     ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
562     ok(ret, "Failed to map client rect.\n");
563
564     hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
565     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
566
567     hr = IDirectDraw4_CreateClipper(ddraw, 0, &clipper, NULL);
568     ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
569     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
570     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
571     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
572     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
573     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
574     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
575     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
576     hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
577     ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
578     ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
579     ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
580     ok(rgn_data->rdh.nCount == 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
581     ok(rgn_data->rdh.nRgnSize == 16, "Got unexpected region size %u.\n", rgn_data->rdh.nRgnSize);
582     ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
583             "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
584             rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
585             rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
586             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
587     rect = (RECT *)&rgn_data->Buffer[0];
588     ok(EqualRect(rect, &client_rect),
589             "Got unexpected clip rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
590             rect->left, rect->top, rect->right, rect->bottom,
591             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
592     HeapFree(GetProcessHeap(), 0, rgn_data);
593
594     r1 = CreateRectRgn(0, 0, 320, 240);
595     ok(!!r1, "Failed to create region.\n");
596     r2 = CreateRectRgn(320, 240, 640, 480);
597     ok(!!r2, "Failed to create region.\n");
598     CombineRgn(r1, r1, r2, RGN_OR);
599     ret = GetRegionData(r1, 0, NULL);
600     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
601     ret = GetRegionData(r1, ret, rgn_data);
602     ok(!!ret, "Failed to get region data.\n");
603
604     DeleteObject(r2);
605     DeleteObject(r1);
606
607     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
608     ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
609     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
610     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
611     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
612     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
613
614     HeapFree(GetProcessHeap(), 0, rgn_data);
615
616     memset(&surface_desc, 0, sizeof(surface_desc));
617     surface_desc.dwSize = sizeof(surface_desc);
618     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
619     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
620     surface_desc.dwWidth = 640;
621     surface_desc.dwHeight = 480;
622     U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
623     U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
624     U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
625     U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
626     U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
627     U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
628
629     hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
630     ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
631     hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
632     ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
633
634     memset(&fx, 0, sizeof(fx));
635     fx.dwSize = sizeof(fx);
636     hr = IDirectDrawSurface4_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
637     ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
638     hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
639     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
640
641     hr = IDirectDrawSurface4_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
642     ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
643     ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
644     ptr = surface_desc.lpSurface;
645     memcpy(&ptr[   0], &src_data[ 0], 6 * sizeof(DWORD));
646     memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
647     memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
648     hr = IDirectDrawSurface4_Unlock(src_surface, NULL);
649     ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
650
651     hr = IDirectDrawSurface4_SetClipper(dst_surface, clipper);
652     ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
653
654     SetRect(&src_rect, 1, 1, 5, 2);
655     hr = IDirectDrawSurface4_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
656     ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
657     for (i = 0; i < 4; ++i)
658     {
659         for (j = 0; j < 4; ++j)
660         {
661             x = 80 * ((2 * j) + 1);
662             y = 60 * ((2 * i) + 1);
663             color = get_surface_color(dst_surface, x, y);
664             ok(compare_color(color, expected1[i * 4 + j], 1),
665                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
666         }
667     }
668
669     U5(fx).dwFillColor = 0xff0000ff;
670     hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
671     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
672     for (i = 0; i < 4; ++i)
673     {
674         for (j = 0; j < 4; ++j)
675         {
676             x = 80 * ((2 * j) + 1);
677             y = 60 * ((2 * i) + 1);
678             color = get_surface_color(dst_surface, x, y);
679             ok(compare_color(color, expected2[i * 4 + j], 1),
680                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
681         }
682     }
683
684     hr = IDirectDrawSurface4_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
685     ok(hr == DDERR_BLTFASTCANTCLIP, "Got unexpected hr %#x.\n", hr);
686
687     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
688     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
689     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
690     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
691     DestroyWindow(window);
692     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
693     ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
694     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
695     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
696     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
697     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
698     hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
699     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
700     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
701     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
702     hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
703     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
704
705     IDirectDrawSurface4_Release(dst_surface);
706     IDirectDrawSurface4_Release(src_surface);
707     IDirectDrawClipper_Release(clipper);
708     IDirectDraw4_Release(ddraw);
709 }
710
711 static void test_coop_level_d3d_state(void)
712 {
713     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
714     IDirectDrawSurface4 *rt, *surface;
715     IDirect3DViewport3 *viewport;
716     IDirect3DDevice3 *device;
717     IDirectDraw4 *ddraw;
718     D3DVIEWPORT2 vp;
719     IDirect3D3 *d3d;
720     D3DCOLOR color;
721     DWORD value;
722     HWND window;
723     HRESULT hr;
724
725     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
726             0, 0, 640, 480, 0, 0, 0, 0);
727     if (!(device = create_device(window, DDSCL_NORMAL)))
728     {
729         skip("Failed to create D3D device, skipping test.\n");
730         DestroyWindow(window);
731         return;
732     }
733
734     hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
735     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
736
737     hr = IDirect3D3_CreateViewport(d3d, &viewport, NULL);
738     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
739     hr = IDirect3DDevice3_AddViewport(device, viewport);
740     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
741     memset(&vp, 0, sizeof(vp));
742     vp.dwSize = sizeof(vp);
743     vp.dwX = 0;
744     vp.dwY = 0;
745     vp.dwWidth = 640;
746     vp.dwHeight = 480;
747     vp.dvClipX = -1.0f;
748     vp.dvClipY =  1.0f;
749     vp.dvClipWidth = 2.0f;
750     vp.dvClipHeight = 2.0f;
751     vp.dvMinZ = 0.0f;
752     vp.dvMaxZ = 1.0f;
753     hr = IDirect3DViewport3_SetViewport2(viewport, &vp);
754     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
755
756     hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
757     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
758     hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
759     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
760     ok(!!value, "Got unexpected z-enable state %#x.\n", value);
761     hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
762     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
763     ok(!value, "Got unexpected alpha blend enable state %#x.\n", value);
764     hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
765     ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
766     hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
767     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
768     color = get_surface_color(rt, 320, 240);
769     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
770
771     hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
772     ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
773     IDirect3D3_Release(d3d);
774     hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
775     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
776     hr = IDirectDrawSurface4_IsLost(rt);
777     ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
778     hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
779     ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
780     IDirectDraw4_Release(ddraw);
781
782     hr = IDirect3DDevice3_GetRenderTarget(device, &surface);
783     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
784     ok(surface == rt, "Got unexpected surface %p.\n", surface);
785     hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
786     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
787     ok(!!value, "Got unexpected z-enable state %#x.\n", value);
788     hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
789     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
790     ok(!!value, "Got unexpected alpha blend enable state %#x.\n", value);
791     hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 0.0f, 0);
792     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
793     color = get_surface_color(rt, 320, 240);
794     ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
795
796     hr = IDirect3DDevice3_DeleteViewport(device, viewport);
797     ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
798     IDirect3DViewport3_Release(viewport);
799     IDirectDrawSurface4_Release(surface);
800     IDirectDrawSurface4_Release(rt);
801     IDirect3DDevice3_Release(device);
802     DestroyWindow(window);
803 }
804
805 static void test_surface_interface_mismatch(void)
806 {
807     IDirectDraw4 *ddraw = NULL;
808     IDirect3D3 *d3d = NULL;
809     IDirectDrawSurface4 *surface = NULL, *ds;
810     IDirectDrawSurface3 *surface3 = NULL;
811     IDirect3DDevice3 *device = NULL;
812     IDirect3DViewport3 *viewport = NULL;
813     DDSURFACEDESC2 surface_desc;
814     DDPIXELFORMAT z_fmt;
815     HRESULT hr;
816     D3DCOLOR color;
817     HWND window;
818     D3DVIEWPORT2 vp;
819     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
820
821     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
822             0, 0, 640, 480, 0, 0, 0, 0);
823
824     if (!(ddraw = create_ddraw()))
825     {
826         skip("Failed to create a ddraw object, skipping test.\n");
827         goto cleanup;
828     }
829
830     hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
831     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
832
833     memset(&surface_desc, 0, sizeof(surface_desc));
834     surface_desc.dwSize = sizeof(surface_desc);
835     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
836     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
837     surface_desc.dwWidth = 640;
838     surface_desc.dwHeight = 480;
839
840     hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
841     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
842
843     hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
844     ok(SUCCEEDED(hr), "Failed to QI IDirectDrawSurface3, hr %#x.\n", hr);
845
846     hr = IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d);
847     if (FAILED(hr))
848     {
849         skip("Failed to get the IDirect3D7 interface, skipping test.\n");
850         goto cleanup;
851     }
852
853     memset(&z_fmt, 0, sizeof(z_fmt));
854     hr = IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
855     if (FAILED(hr) || !z_fmt.dwSize)
856     {
857         skip("No depth buffer formats available, skipping test.\n");
858         goto cleanup;
859     }
860
861     memset(&surface_desc, 0, sizeof(surface_desc));
862     surface_desc.dwSize = sizeof(surface_desc);
863     surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
864     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
865     U4(surface_desc).ddpfPixelFormat = z_fmt;
866     surface_desc.dwWidth = 640;
867     surface_desc.dwHeight = 480;
868     hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &ds, NULL);
869     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
870     if (FAILED(hr))
871         goto cleanup;
872
873     /* Using a different surface interface version still works */
874     hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
875     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
876     IDirectDrawSurface4_Release(ds);
877     if (FAILED(hr))
878         goto cleanup;
879
880     /* Here too */
881     hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, (IDirectDrawSurface4 *)surface3, &device, NULL);
882     ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
883     if (FAILED(hr))
884         goto cleanup;
885
886     hr = IDirect3D3_CreateViewport(d3d, &viewport, NULL);
887     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
888     hr = IDirect3DDevice3_AddViewport(device, viewport);
889     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
890     memset(&vp, 0, sizeof(vp));
891     vp.dwSize = sizeof(vp);
892     vp.dwX = 0;
893     vp.dwY = 0;
894     vp.dwWidth = 640;
895     vp.dwHeight = 480;
896     vp.dvClipX = -1.0f;
897     vp.dvClipY =  1.0f;
898     vp.dvClipWidth = 2.0f;
899     vp.dvClipHeight = 2.0f;
900     vp.dvMinZ = 0.0f;
901     vp.dvMaxZ = 1.0f;
902     hr = IDirect3DViewport3_SetViewport2(viewport, &vp);
903     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
904
905     hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
906     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
907     color = get_surface_color(surface, 320, 240);
908     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
909
910 cleanup:
911     if (viewport)
912     {
913         IDirect3DDevice2_DeleteViewport(device, viewport);
914         IDirect3DViewport2_Release(viewport);
915     }
916     if (surface3) IDirectDrawSurface3_Release(surface3);
917     if (surface) IDirectDrawSurface4_Release(surface);
918     if (device) IDirect3DDevice3_Release(device);
919     if (d3d) IDirect3D3_Release(d3d);
920     if (ddraw) IDirectDraw4_Release(ddraw);
921     DestroyWindow(window);
922 }
923
924 START_TEST(ddraw4)
925 {
926     test_process_vertices();
927     test_coop_level_create_device_window();
928     test_clipper_blt();
929     test_coop_level_d3d_state();
930     test_surface_interface_mismatch();
931 }