mshtml: Properly report history update for location.replace call.
[wine] / dlls / d3d9 / tests / device.c
1 /*
2  * Copyright (C) 2006 Vitaliy Margolen
3  * Copyright (C) 2006 Chris Robinson
4  * Copyright (C) 2006-2007 Stefan Dösinger(For CodeWeavers)
5  * Copyright 2007 Henri Verbeet
6  * Copyright (C) 2008 Rico Schüller
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #define COBJMACROS
24 #include <windowsx.h>
25 #include <d3d9.h>
26 #include "wine/test.h"
27
28 static INT screen_width;
29 static INT screen_height;
30
31 static IDirect3D9 *(WINAPI *pDirect3DCreate9)(UINT);
32
33 static int get_refcount(IUnknown *object)
34 {
35     IUnknown_AddRef( object );
36     return IUnknown_Release( object );
37 }
38
39 /* try to make sure pending X events have been processed before continuing */
40 static void flush_events(void)
41 {
42     MSG msg;
43     int diff = 200;
44     int min_timeout = 100;
45     DWORD time = GetTickCount() + diff;
46
47     while (diff > 0)
48     {
49         if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
50         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
51         diff = time - GetTickCount();
52     }
53 }
54
55 static IDirect3DDevice9 *create_device(IDirect3D9 *d3d9, HWND device_window, HWND focus_window, BOOL windowed)
56 {
57     D3DPRESENT_PARAMETERS present_parameters = {0};
58     IDirect3DDevice9 *device;
59
60     present_parameters.Windowed = windowed;
61     present_parameters.hDeviceWindow = device_window;
62     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
63     present_parameters.BackBufferWidth = screen_width;
64     present_parameters.BackBufferHeight = screen_height;
65     present_parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
66     present_parameters.EnableAutoDepthStencil = TRUE;
67     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
68
69     if (SUCCEEDED(IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, focus_window,
70             D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device))) return device;
71
72     present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
73     if (SUCCEEDED(IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, focus_window,
74             D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device))) return device;
75
76     if (SUCCEEDED(IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, focus_window,
77             D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device))) return device;
78
79     return NULL;
80 }
81
82 static HRESULT reset_device(IDirect3DDevice9 *device, HWND device_window, BOOL windowed)
83 {
84     D3DPRESENT_PARAMETERS present_parameters = {0};
85
86     present_parameters.Windowed = windowed;
87     present_parameters.hDeviceWindow = device_window;
88     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
89     present_parameters.BackBufferWidth = screen_width;
90     present_parameters.BackBufferHeight = screen_height;
91     present_parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
92     present_parameters.EnableAutoDepthStencil = TRUE;
93     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
94
95     return IDirect3DDevice9_Reset(device, &present_parameters);
96 }
97
98 #define CHECK_CALL(r,c,d,rc) \
99     if (SUCCEEDED(r)) {\
100         int tmp1 = get_refcount( (IUnknown *)d ); \
101         int rc_new = rc; \
102         ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
103     } else {\
104         trace("%s failed: %08x\n", c, r); \
105     }
106
107 #define CHECK_RELEASE(obj,d,rc) \
108     if (obj) { \
109         int tmp1, rc_new = rc; \
110         IUnknown_Release( (IUnknown*)obj ); \
111         tmp1 = get_refcount( (IUnknown *)d ); \
112         ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
113     }
114
115 #define CHECK_REFCOUNT(obj,rc) \
116     { \
117         int rc_new = rc; \
118         int count = get_refcount( (IUnknown *)obj ); \
119         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
120     }
121
122 #define CHECK_RELEASE_REFCOUNT(obj,rc) \
123     { \
124         int rc_new = rc; \
125         int count = IUnknown_Release( (IUnknown *)obj ); \
126         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
127     }
128
129 #define CHECK_ADDREF_REFCOUNT(obj,rc) \
130     { \
131         int rc_new = rc; \
132         int count = IUnknown_AddRef( (IUnknown *)obj ); \
133         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
134     }
135
136 #define CHECK_SURFACE_CONTAINER(obj,iid,expected) \
137     { \
138         void *container_ptr = (void *)0x1337c0d3; \
139         hr = IDirect3DSurface9_GetContainer(obj, &iid, &container_ptr); \
140         ok(SUCCEEDED(hr) && container_ptr == expected, "GetContainer returned: hr %#x, container_ptr %p. " \
141             "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, expected); \
142         if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr); \
143     }
144
145 static void check_mipmap_levels(IDirect3DDevice9 *device, UINT width, UINT height, UINT count)
146 {
147     IDirect3DBaseTexture9* texture = NULL;
148     HRESULT hr = IDirect3DDevice9_CreateTexture( device, width, height, 0, 0,
149             D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, (IDirect3DTexture9**) &texture, NULL );
150
151     if (SUCCEEDED(hr)) {
152         DWORD levels = IDirect3DBaseTexture9_GetLevelCount(texture);
153         ok(levels == count, "Invalid level count. Expected %d got %u\n", count, levels);
154     } else
155         trace("CreateTexture failed: %08x\n", hr);
156
157     if (texture) IDirect3DBaseTexture9_Release( texture );
158 }
159
160 static void test_mipmap_levels(void)
161 {
162
163     HRESULT               hr;
164     HWND                  hwnd = NULL;
165
166     IDirect3D9            *pD3d = NULL;
167     IDirect3DDevice9      *pDevice = NULL;
168     D3DPRESENT_PARAMETERS d3dpp;
169     D3DDISPLAYMODE        d3ddm;
170
171     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
172     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
173     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
174     ok(hwnd != NULL, "Failed to create window\n");
175     if (!pD3d || !hwnd) goto cleanup;
176
177     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
178     ZeroMemory( &d3dpp, sizeof(d3dpp) );
179     d3dpp.Windowed         = TRUE;
180     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
181     d3dpp.BackBufferFormat = d3ddm.Format;
182
183     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, hwnd,
184                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
185     ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "Failed to create IDirect3D9Device (%08x)\n", hr);
186     if (FAILED(hr)) {
187         skip("failed to create a d3d device\n");
188         goto cleanup;
189     }
190
191     check_mipmap_levels(pDevice, 32, 32, 6);
192     check_mipmap_levels(pDevice, 256, 1, 9);
193     check_mipmap_levels(pDevice, 1, 256, 9);
194     check_mipmap_levels(pDevice, 1, 1, 1);
195
196 cleanup:
197     if (pDevice)
198     {
199         UINT refcount = IDirect3DDevice9_Release( pDevice );
200         ok(!refcount, "Device has %u references left.\n", refcount);
201     }
202     if (pD3d) IDirect3D9_Release( pD3d );
203     DestroyWindow( hwnd );
204 }
205
206 static void test_checkdevicemultisampletype(void)
207 {
208
209     HRESULT               hr;
210     HWND                  hwnd = NULL;
211
212     IDirect3D9            *pD3d = NULL;
213     IDirect3DDevice9      *pDevice = NULL;
214     D3DPRESENT_PARAMETERS d3dpp;
215     D3DDISPLAYMODE        d3ddm;
216     DWORD                 qualityLevels;
217
218     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
219     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
220     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
221     ok(hwnd != NULL, "Failed to create window\n");
222     if (!pD3d || !hwnd) goto cleanup;
223
224     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
225     ZeroMemory( &d3dpp, sizeof(d3dpp) );
226     d3dpp.Windowed         = TRUE;
227     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
228     d3dpp.BackBufferFormat = d3ddm.Format;
229
230     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, hwnd,
231                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
232     ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "Failed to create IDirect3D9Device (%08x)\n", hr);
233     if (FAILED(hr)) {
234         skip("failed to create a d3d device\n");
235         goto cleanup;
236     }
237
238     qualityLevels = 0;
239
240     hr = IDirect3D9_CheckDeviceMultiSampleType(pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, TRUE,
241     D3DMULTISAMPLE_NONE, &qualityLevels);
242     ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "CheckDeviceMultiSampleType failed with (%08x)\n", hr);
243     if(hr == D3DERR_NOTAVAILABLE)
244     {
245         skip("IDirect3D9_CheckDeviceMultiSampleType not available\n");
246         goto cleanup;
247     }
248     ok(qualityLevels == 1,"qualitylevel is not 1 but %d\n",qualityLevels);
249
250     hr = IDirect3D9_CheckDeviceMultiSampleType(pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, FALSE,
251     D3DMULTISAMPLE_NONE, &qualityLevels);
252     ok(SUCCEEDED(hr), "CheckDeviceMultiSampleType failed with (%08x)\n", hr);
253     ok(qualityLevels == 1,"qualitylevel is not 1 but %d\n",qualityLevels);
254
255 cleanup:
256     if (pDevice)
257     {
258         UINT refcount = IDirect3DDevice9_Release( pDevice );
259         ok(!refcount, "Device has %u references left.\n", refcount);
260     }
261     if (pD3d) IDirect3D9_Release( pD3d );
262     DestroyWindow( hwnd );
263 }
264
265 static void test_swapchain(void)
266 {
267     HRESULT                      hr;
268     HWND                         hwnd               = NULL;
269     IDirect3D9                  *pD3d               = NULL;
270     IDirect3DDevice9            *pDevice            = NULL;
271     IDirect3DSwapChain9         *swapchain0         = NULL;
272     IDirect3DSwapChain9         *swapchain1         = NULL;
273     IDirect3DSwapChain9         *swapchain2         = NULL;
274     IDirect3DSwapChain9         *swapchain3         = NULL;
275     IDirect3DSwapChain9         *swapchainX         = NULL;
276     IDirect3DSurface9           *backbuffer         = NULL;
277     D3DPRESENT_PARAMETERS        d3dpp;
278     D3DDISPLAYMODE               d3ddm;
279
280     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
281     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
282     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
283     ok(hwnd != NULL, "Failed to create window\n");
284     if (!pD3d || !hwnd) goto cleanup;
285
286     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
287     ZeroMemory( &d3dpp, sizeof(d3dpp) );
288     d3dpp.Windowed         = TRUE;
289     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
290     d3dpp.BackBufferFormat = d3ddm.Format;
291     d3dpp.BackBufferCount  = 0;
292
293     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
294                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
295     ok(hr == S_OK || hr == D3DERR_NOTAVAILABLE,
296        "Failed to create IDirect3D9Device (%08x)\n", hr);
297     if (FAILED(hr)) goto cleanup;
298
299     /* Check if the back buffer count was modified */
300     ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
301
302     /* Get the implicit swapchain */
303     hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &swapchain0);
304     ok(SUCCEEDED(hr), "Failed to get the implicit swapchain (%08x)\n", hr);
305     if(swapchain0) IDirect3DSwapChain9_Release(swapchain0);
306
307     /* Check if there is a back buffer */
308     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
309     ok(SUCCEEDED(hr), "Failed to get the back buffer (%08x)\n", hr);
310     ok(backbuffer != NULL, "The back buffer is NULL\n");
311     if(backbuffer) IDirect3DSurface9_Release(backbuffer);
312
313     /* Try to get a nonexistent swapchain */
314     hr = IDirect3DDevice9_GetSwapChain(pDevice, 1, &swapchainX);
315     ok(hr == D3DERR_INVALIDCALL, "GetSwapChain on an nonexistent swapchain returned (%08x)\n", hr);
316     ok(swapchainX == NULL, "Swapchain 1 is %p\n", swapchainX);
317     if(swapchainX) IDirect3DSwapChain9_Release(swapchainX);
318
319     /* Create a bunch of swapchains */
320     d3dpp.BackBufferCount = 0;
321     hr = IDirect3DDevice9_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain1);
322     ok(SUCCEEDED(hr), "Failed to create a swapchain (%08x)\n", hr);
323     ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
324
325     d3dpp.BackBufferCount  = 1;
326     hr = IDirect3DDevice9_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain2);
327     ok(SUCCEEDED(hr), "Failed to create a swapchain (%08x)\n", hr);
328
329     d3dpp.BackBufferCount  = 2;
330     hr = IDirect3DDevice9_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain3);
331     ok(SUCCEEDED(hr), "Failed to create a swapchain (%08x)\n", hr);
332     if(SUCCEEDED(hr)) {
333         /* Swapchain 3, created with backbuffercount 2 */
334         backbuffer = (void *) 0xdeadbeef;
335         hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 0, 0, &backbuffer);
336         ok(SUCCEEDED(hr), "Failed to get the 1st back buffer (%08x)\n", hr);
337         ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
338         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
339
340         backbuffer = (void *) 0xdeadbeef;
341         hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 1, 0, &backbuffer);
342         ok(SUCCEEDED(hr), "Failed to get the 2nd back buffer (%08x)\n", hr);
343         ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
344         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
345
346         backbuffer = (void *) 0xdeadbeef;
347         hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 2, 0, &backbuffer);
348         ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %08x\n", hr);
349         ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
350         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
351
352         backbuffer = (void *) 0xdeadbeef;
353         hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 3, 0, &backbuffer);
354         ok(FAILED(hr), "Failed to get the back buffer (%08x)\n", hr);
355         ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
356         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
357     }
358
359     /* Check the back buffers of the swapchains */
360     /* Swapchain 1, created with backbuffercount 0 */
361     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain1, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
362     ok(SUCCEEDED(hr), "Failed to get the back buffer (%08x)\n", hr);
363     ok(backbuffer != NULL, "The back buffer is NULL (%08x)\n", hr);
364     if(backbuffer) IDirect3DSurface9_Release(backbuffer);
365
366     backbuffer = (void *) 0xdeadbeef;
367     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain1, 1, 0, &backbuffer);
368     ok(FAILED(hr), "Failed to get the back buffer (%08x)\n", hr);
369     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
370     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
371
372     /* Swapchain 2 - created with backbuffercount 1 */
373     backbuffer = (void *) 0xdeadbeef;
374     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain2, 0, 0, &backbuffer);
375     ok(SUCCEEDED(hr), "Failed to get the back buffer (%08x)\n", hr);
376     ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
377     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
378
379     backbuffer = (void *) 0xdeadbeef;
380     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain2, 1, 0, &backbuffer);
381     ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %08x\n", hr);
382     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
383     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
384
385     backbuffer = (void *) 0xdeadbeef;
386     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain2, 2, 0, &backbuffer);
387     ok(FAILED(hr), "Failed to get the back buffer (%08x)\n", hr);
388     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
389     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
390
391     /* Try getSwapChain on a manually created swapchain
392      * it should fail, apparently GetSwapChain only returns implicit swapchains
393      */
394     swapchainX = (void *) 0xdeadbeef;
395     hr = IDirect3DDevice9_GetSwapChain(pDevice, 1, &swapchainX);
396     ok(hr == D3DERR_INVALIDCALL, "Failed to get the second swapchain (%08x)\n", hr);
397     ok(swapchainX == NULL, "The swapchain pointer is %p\n", swapchainX);
398     if(swapchainX && swapchainX != (void *) 0xdeadbeef ) IDirect3DSwapChain9_Release(swapchainX);
399
400 cleanup:
401     if(swapchain1) IDirect3DSwapChain9_Release(swapchain1);
402     if(swapchain2) IDirect3DSwapChain9_Release(swapchain2);
403     if(swapchain3) IDirect3DSwapChain9_Release(swapchain3);
404     if (pDevice)
405     {
406         UINT refcount = IDirect3DDevice9_Release(pDevice);
407         ok(!refcount, "Device has %u references left.\n", refcount);
408     }
409     if (pD3d) IDirect3D9_Release(pD3d);
410     DestroyWindow( hwnd );
411 }
412
413 /* Shared between two functions */
414 static const DWORD simple_vs[] = {0xFFFE0101,       /* vs_1_1               */
415     0x0000001F, 0x80000000, 0x900F0000,             /* dcl_position0 v0     */
416     0x00000009, 0xC0010000, 0x90E40000, 0xA0E40000, /* dp4 oPos.x, v0, c0   */
417     0x00000009, 0xC0020000, 0x90E40000, 0xA0E40001, /* dp4 oPos.y, v0, c1   */
418     0x00000009, 0xC0040000, 0x90E40000, 0xA0E40002, /* dp4 oPos.z, v0, c2   */
419     0x00000009, 0xC0080000, 0x90E40000, 0xA0E40003, /* dp4 oPos.w, v0, c3   */
420     0x0000FFFF};                                    /* END                  */
421
422 static void test_refcount(void)
423 {
424     HRESULT                      hr;
425     HWND                         hwnd               = NULL;
426     IDirect3D9                  *pD3d               = NULL;
427     IDirect3D9                  *pD3d2              = NULL;
428     IDirect3DDevice9            *pDevice            = NULL;
429     IDirect3DVertexBuffer9      *pVertexBuffer      = NULL;
430     IDirect3DIndexBuffer9       *pIndexBuffer       = NULL;
431     IDirect3DVertexDeclaration9 *pVertexDeclaration = NULL;
432     IDirect3DVertexShader9      *pVertexShader      = NULL;
433     IDirect3DPixelShader9       *pPixelShader       = NULL;
434     IDirect3DCubeTexture9       *pCubeTexture       = NULL;
435     IDirect3DTexture9           *pTexture           = NULL;
436     IDirect3DVolumeTexture9     *pVolumeTexture     = NULL;
437     IDirect3DVolume9            *pVolumeLevel       = NULL;
438     IDirect3DSurface9           *pStencilSurface    = NULL;
439     IDirect3DSurface9           *pOffscreenSurface  = NULL;
440     IDirect3DSurface9           *pRenderTarget      = NULL;
441     IDirect3DSurface9           *pRenderTarget2     = NULL;
442     IDirect3DSurface9           *pRenderTarget3     = NULL;
443     IDirect3DSurface9           *pTextureLevel      = NULL;
444     IDirect3DSurface9           *pBackBuffer        = NULL;
445     IDirect3DStateBlock9        *pStateBlock        = NULL;
446     IDirect3DStateBlock9        *pStateBlock1       = NULL;
447     IDirect3DSwapChain9         *pSwapChain         = NULL;
448     IDirect3DQuery9             *pQuery             = NULL;
449     D3DPRESENT_PARAMETERS        d3dpp;
450     D3DDISPLAYMODE               d3ddm;
451     int                          refcount = 0, tmp;
452
453     D3DVERTEXELEMENT9 decl[] =
454     {
455         D3DDECL_END()
456     };
457     static DWORD simple_ps[] = {0xFFFF0101,                                     /* ps_1_1                       */
458         0x00000051, 0xA00F0001, 0x3F800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0  */
459         0x00000042, 0xB00F0000,                                                 /* tex t0                       */
460         0x00000008, 0x800F0000, 0xA0E40001, 0xA0E40000,                         /* dp3 r0, c1, c0               */
461         0x00000005, 0x800F0000, 0x90E40000, 0x80E40000,                         /* mul r0, v0, r0               */
462         0x00000005, 0x800F0000, 0xB0E40000, 0x80E40000,                         /* mul r0, t0, r0               */
463         0x0000FFFF};                                                            /* END                          */
464
465
466     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
467     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
468     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
469     ok(hwnd != NULL, "Failed to create window\n");
470     if (!pD3d || !hwnd) goto cleanup;
471
472     CHECK_REFCOUNT( pD3d, 1 );
473
474     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
475     ZeroMemory( &d3dpp, sizeof(d3dpp) );
476     d3dpp.Windowed         = TRUE;
477     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
478     d3dpp.BackBufferFormat = d3ddm.Format;
479     d3dpp.EnableAutoDepthStencil = TRUE;
480     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
481
482     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
483                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
484     ok(hr == S_OK || hr == D3DERR_NOTAVAILABLE,
485        "Failed to create IDirect3D9Device (%08x)\n", hr);
486     if (FAILED(hr)) goto cleanup;
487
488     refcount = get_refcount( (IUnknown *)pDevice );
489     ok(refcount == 1, "Invalid device RefCount %d\n", refcount);
490
491     CHECK_REFCOUNT( pD3d, 2 );
492
493     hr = IDirect3DDevice9_GetDirect3D(pDevice, &pD3d2);
494     CHECK_CALL( hr, "GetDirect3D", pDevice, refcount );
495
496     ok(pD3d2 == pD3d, "Expected IDirect3D9 pointers to be equal\n");
497     CHECK_REFCOUNT( pD3d, 3 );
498     CHECK_RELEASE_REFCOUNT( pD3d, 2 );
499
500     /**
501      * Check refcount of implicit surfaces and implicit swapchain. Findings:
502      *   - the container is the device OR swapchain
503      *   - they hold a reference to the device
504      *   - they are created with a refcount of 0 (Get/Release returns original refcount)
505      *   - they are not freed if refcount reaches 0.
506      *   - the refcount is not forwarded to the container.
507      */
508     hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &pSwapChain);
509     CHECK_CALL( hr, "GetSwapChain", pDevice, ++refcount);
510     if (pSwapChain)
511     {
512         CHECK_REFCOUNT( pSwapChain, 1);
513
514         hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget);
515         CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
516         CHECK_REFCOUNT( pSwapChain, 1);
517         if(pRenderTarget)
518         {
519             CHECK_SURFACE_CONTAINER( pRenderTarget, IID_IDirect3DSwapChain9, pSwapChain);
520             CHECK_REFCOUNT( pRenderTarget, 1);
521
522             CHECK_ADDREF_REFCOUNT(pRenderTarget, 2);
523             CHECK_REFCOUNT(pDevice, refcount);
524             CHECK_RELEASE_REFCOUNT(pRenderTarget, 1);
525             CHECK_REFCOUNT(pDevice, refcount);
526
527             hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget);
528             CHECK_CALL( hr, "GetRenderTarget", pDevice, refcount);
529             CHECK_REFCOUNT( pRenderTarget, 2);
530             CHECK_RELEASE_REFCOUNT( pRenderTarget, 1);
531             CHECK_RELEASE_REFCOUNT( pRenderTarget, 0);
532             CHECK_REFCOUNT( pDevice, --refcount);
533
534             /* The render target is released with the device, so AddRef with refcount=0 is fine here. */
535             CHECK_ADDREF_REFCOUNT(pRenderTarget, 1);
536             CHECK_REFCOUNT(pDevice, ++refcount);
537             CHECK_RELEASE_REFCOUNT(pRenderTarget, 0);
538             CHECK_REFCOUNT(pDevice, --refcount);
539         }
540
541         /* Render target and back buffer are identical. */
542         hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, 0, &pBackBuffer);
543         CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
544         if(pBackBuffer)
545         {
546             CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
547             ok(pRenderTarget == pBackBuffer, "RenderTarget=%p and BackBuffer=%p should be the same.\n",
548             pRenderTarget, pBackBuffer);
549             pBackBuffer = NULL;
550         }
551         CHECK_REFCOUNT( pDevice, --refcount);
552
553         hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pStencilSurface);
554         CHECK_CALL( hr, "GetDepthStencilSurface", pDevice, ++refcount);
555         CHECK_REFCOUNT( pSwapChain, 1);
556         if(pStencilSurface)
557         {
558             CHECK_SURFACE_CONTAINER( pStencilSurface, IID_IDirect3DDevice9, pDevice);
559             CHECK_REFCOUNT( pStencilSurface, 1);
560
561             CHECK_ADDREF_REFCOUNT(pStencilSurface, 2);
562             CHECK_REFCOUNT(pDevice, refcount);
563             CHECK_RELEASE_REFCOUNT(pStencilSurface, 1);
564             CHECK_REFCOUNT(pDevice, refcount);
565
566             CHECK_RELEASE_REFCOUNT( pStencilSurface, 0);
567             CHECK_REFCOUNT( pDevice, --refcount);
568
569             /* The stencil surface is released with the device, so AddRef with refcount=0 is fine here. */
570             CHECK_ADDREF_REFCOUNT(pStencilSurface, 1);
571             CHECK_REFCOUNT(pDevice, ++refcount);
572             CHECK_RELEASE_REFCOUNT(pStencilSurface, 0);
573             CHECK_REFCOUNT(pDevice, --refcount);
574             pStencilSurface = NULL;
575         }
576
577         CHECK_RELEASE_REFCOUNT( pSwapChain, 0);
578         CHECK_REFCOUNT( pDevice, --refcount);
579
580         /* The implicit swapchwin is released with the device, so AddRef with refcount=0 is fine here. */
581         CHECK_ADDREF_REFCOUNT(pSwapChain, 1);
582         CHECK_REFCOUNT(pDevice, ++refcount);
583         CHECK_RELEASE_REFCOUNT(pSwapChain, 0);
584         CHECK_REFCOUNT(pDevice, --refcount);
585         pSwapChain = NULL;
586     }
587
588     /* Buffers */
589     hr = IDirect3DDevice9_CreateIndexBuffer( pDevice, 16, 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &pIndexBuffer, NULL );
590     CHECK_CALL( hr, "CreateIndexBuffer", pDevice, ++refcount );
591     if(pIndexBuffer)
592     {
593         tmp = get_refcount( (IUnknown *)pIndexBuffer );
594
595         hr = IDirect3DDevice9_SetIndices(pDevice, pIndexBuffer);
596         CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
597         hr = IDirect3DDevice9_SetIndices(pDevice, NULL);
598         CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
599     }
600
601     hr = IDirect3DDevice9_CreateVertexBuffer( pDevice, 16, 0, D3DFVF_XYZ, D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
602     CHECK_CALL( hr, "CreateVertexBuffer", pDevice, ++refcount );
603     if(pVertexBuffer)
604     {
605         IDirect3DVertexBuffer9 *pVBuf = (void*)~0;
606         UINT offset = ~0;
607         UINT stride = ~0;
608
609         tmp = get_refcount( (IUnknown *)pVertexBuffer );
610
611         hr = IDirect3DDevice9_SetStreamSource(pDevice, 0, pVertexBuffer, 0, 3 * sizeof(float));
612         CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
613         hr = IDirect3DDevice9_SetStreamSource(pDevice, 0, NULL, 0, 0);
614         CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
615
616         hr = IDirect3DDevice9_GetStreamSource(pDevice, 0, &pVBuf, &offset, &stride);
617         ok(SUCCEEDED(hr), "GetStreamSource did not succeed with NULL stream!\n");
618         ok(pVBuf==NULL, "pVBuf not NULL (%p)!\n", pVBuf);
619         ok(stride==3*sizeof(float), "stride not 3 floats (got %u)!\n", stride);
620         ok(offset==0, "offset not 0 (got %u)!\n", offset);
621     }
622     /* Shaders */
623     hr = IDirect3DDevice9_CreateVertexDeclaration( pDevice, decl, &pVertexDeclaration );
624     CHECK_CALL( hr, "CreateVertexDeclaration", pDevice, ++refcount );
625     hr = IDirect3DDevice9_CreateVertexShader( pDevice, simple_vs, &pVertexShader );
626     CHECK_CALL( hr, "CreateVertexShader", pDevice, ++refcount );
627     hr = IDirect3DDevice9_CreatePixelShader( pDevice, simple_ps, &pPixelShader );
628     CHECK_CALL( hr, "CreatePixelShader", pDevice, ++refcount );
629     /* Textures */
630     hr = IDirect3DDevice9_CreateTexture( pDevice, 32, 32, 3, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pTexture, NULL );
631     CHECK_CALL( hr, "CreateTexture", pDevice, ++refcount );
632     if (pTexture)
633     {
634         tmp = get_refcount( (IUnknown *)pTexture );
635
636         /* SetTexture should not increase refcounts */
637         hr = IDirect3DDevice9_SetTexture(pDevice, 0, (IDirect3DBaseTexture9 *) pTexture);
638         CHECK_CALL( hr, "SetTexture", pTexture, tmp);
639         hr = IDirect3DDevice9_SetTexture(pDevice, 0, NULL);
640         CHECK_CALL( hr, "SetTexture", pTexture, tmp);
641
642         /* This should not increment device refcount */
643         hr = IDirect3DTexture9_GetSurfaceLevel( pTexture, 1, &pTextureLevel );
644         CHECK_CALL( hr, "GetSurfaceLevel", pDevice, refcount );
645         /* But should increment texture's refcount */
646         CHECK_REFCOUNT( pTexture, tmp+1 );
647         /* Because the texture and surface refcount are identical */
648         if (pTextureLevel)
649         {
650             CHECK_REFCOUNT        ( pTextureLevel, tmp+1 );
651             CHECK_ADDREF_REFCOUNT ( pTextureLevel, tmp+2 );
652             CHECK_REFCOUNT        ( pTexture     , tmp+2 );
653             CHECK_RELEASE_REFCOUNT( pTextureLevel, tmp+1 );
654             CHECK_REFCOUNT        ( pTexture     , tmp+1 );
655             CHECK_RELEASE_REFCOUNT( pTexture     , tmp   );
656             CHECK_REFCOUNT        ( pTextureLevel, tmp   );
657         }
658     }
659     hr = IDirect3DDevice9_CreateCubeTexture( pDevice, 32, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pCubeTexture, NULL );
660     CHECK_CALL( hr, "CreateCubeTexture", pDevice, ++refcount );
661     hr = IDirect3DDevice9_CreateVolumeTexture( pDevice, 32, 32, 2, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pVolumeTexture, NULL );
662     CHECK_CALL( hr, "CreateVolumeTexture", pDevice, ++refcount );
663     if (pVolumeTexture)
664     {
665         tmp = get_refcount( (IUnknown *)pVolumeTexture );
666
667         /* This should not increment device refcount */
668         hr = IDirect3DVolumeTexture9_GetVolumeLevel(pVolumeTexture, 0, &pVolumeLevel);
669         CHECK_CALL( hr, "GetVolumeLevel", pDevice, refcount );
670         /* But should increment volume texture's refcount */
671         CHECK_REFCOUNT( pVolumeTexture, tmp+1 );
672         /* Because the volume texture and volume refcount are identical */
673         if (pVolumeLevel)
674         {
675             CHECK_REFCOUNT        ( pVolumeLevel  , tmp+1 );
676             CHECK_ADDREF_REFCOUNT ( pVolumeLevel  , tmp+2 );
677             CHECK_REFCOUNT        ( pVolumeTexture, tmp+2 );
678             CHECK_RELEASE_REFCOUNT( pVolumeLevel  , tmp+1 );
679             CHECK_REFCOUNT        ( pVolumeTexture, tmp+1 );
680             CHECK_RELEASE_REFCOUNT( pVolumeTexture, tmp   );
681             CHECK_REFCOUNT        ( pVolumeLevel  , tmp   );
682         }
683     }
684     /* Surfaces */
685     hr = IDirect3DDevice9_CreateDepthStencilSurface( pDevice, 32, 32, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, TRUE, &pStencilSurface, NULL );
686     CHECK_CALL( hr, "CreateDepthStencilSurface", pDevice, ++refcount );
687     CHECK_REFCOUNT( pStencilSurface, 1 );
688     hr = IDirect3DDevice9_CreateOffscreenPlainSurface( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pOffscreenSurface, NULL );
689     CHECK_CALL( hr, "CreateOffscreenPlainSurface", pDevice, ++refcount );
690     CHECK_REFCOUNT( pOffscreenSurface, 1 );
691     hr = IDirect3DDevice9_CreateRenderTarget( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &pRenderTarget3, NULL );
692     CHECK_CALL( hr, "CreateRenderTarget", pDevice, ++refcount );
693     CHECK_REFCOUNT( pRenderTarget3, 1 );
694     /* Misc */
695     hr = IDirect3DDevice9_CreateStateBlock( pDevice, D3DSBT_ALL, &pStateBlock );
696     CHECK_CALL( hr, "CreateStateBlock", pDevice, ++refcount );
697     hr = IDirect3DDevice9_CreateAdditionalSwapChain( pDevice, &d3dpp, &pSwapChain );
698     CHECK_CALL( hr, "CreateAdditionalSwapChain", pDevice, ++refcount );
699     if(pSwapChain)
700     {
701         /* check implicit back buffer */
702         hr = IDirect3DSwapChain9_GetBackBuffer(pSwapChain, 0, 0, &pBackBuffer);
703         CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
704         CHECK_REFCOUNT( pSwapChain, 1);
705         if(pBackBuffer)
706         {
707             CHECK_SURFACE_CONTAINER( pBackBuffer, IID_IDirect3DSwapChain9, pSwapChain);
708             CHECK_REFCOUNT( pBackBuffer, 1);
709             CHECK_RELEASE_REFCOUNT( pBackBuffer, 0);
710             CHECK_REFCOUNT( pDevice, --refcount);
711
712             /* The back buffer is released with the swapchain, so AddRef with refcount=0 is fine here. */
713             CHECK_ADDREF_REFCOUNT(pBackBuffer, 1);
714             CHECK_REFCOUNT(pDevice, ++refcount);
715             CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
716             CHECK_REFCOUNT(pDevice, --refcount);
717             pBackBuffer = NULL;
718         }
719         CHECK_REFCOUNT( pSwapChain, 1);
720     }
721     hr = IDirect3DDevice9_CreateQuery( pDevice, D3DQUERYTYPE_EVENT, &pQuery );
722     CHECK_CALL( hr, "CreateQuery", pDevice, ++refcount );
723
724     hr = IDirect3DDevice9_BeginStateBlock( pDevice );
725     CHECK_CALL( hr, "BeginStateBlock", pDevice, refcount );
726     hr = IDirect3DDevice9_EndStateBlock( pDevice, &pStateBlock1 );
727     CHECK_CALL( hr, "EndStateBlock", pDevice, ++refcount );
728
729     /* The implicit render target is not freed if refcount reaches 0.
730      * Otherwise GetRenderTarget would re-allocate it and the pointer would change.*/
731     hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget2);
732     CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
733     if(pRenderTarget2)
734     {
735         CHECK_RELEASE_REFCOUNT(pRenderTarget2, 0);
736         ok(pRenderTarget == pRenderTarget2, "RenderTarget=%p and RenderTarget2=%p should be the same.\n",
737            pRenderTarget, pRenderTarget2);
738         CHECK_REFCOUNT( pDevice, --refcount);
739         pRenderTarget2 = NULL;
740     }
741     pRenderTarget = NULL;
742
743 cleanup:
744     CHECK_RELEASE(pDevice,              pDevice, --refcount);
745
746     /* Buffers */
747     CHECK_RELEASE(pVertexBuffer,        pDevice, --refcount);
748     CHECK_RELEASE(pIndexBuffer,         pDevice, --refcount);
749     /* Shaders */
750     CHECK_RELEASE(pVertexDeclaration,   pDevice, --refcount);
751     CHECK_RELEASE(pVertexShader,        pDevice, --refcount);
752     CHECK_RELEASE(pPixelShader,         pDevice, --refcount);
753     /* Textures */
754     CHECK_RELEASE(pTextureLevel,        pDevice, --refcount);
755     CHECK_RELEASE(pCubeTexture,         pDevice, --refcount);
756     CHECK_RELEASE(pVolumeTexture,       pDevice, --refcount);
757     /* Surfaces */
758     CHECK_RELEASE(pStencilSurface,      pDevice, --refcount);
759     CHECK_RELEASE(pOffscreenSurface,    pDevice, --refcount);
760     CHECK_RELEASE(pRenderTarget3,       pDevice, --refcount);
761     /* Misc */
762     CHECK_RELEASE(pStateBlock,          pDevice, --refcount);
763     CHECK_RELEASE(pSwapChain,           pDevice, --refcount);
764     CHECK_RELEASE(pQuery,               pDevice, --refcount);
765     /* This will destroy device - cannot check the refcount here */
766     if (pStateBlock1)         CHECK_RELEASE_REFCOUNT( pStateBlock1, 0);
767
768     if (pD3d)                 CHECK_RELEASE_REFCOUNT( pD3d, 0);
769
770     DestroyWindow( hwnd );
771 }
772
773 static void test_cursor(void)
774 {
775     HRESULT                      hr;
776     HWND                         hwnd               = NULL;
777     IDirect3D9                  *pD3d               = NULL;
778     IDirect3DDevice9            *pDevice            = NULL;
779     D3DPRESENT_PARAMETERS        d3dpp;
780     D3DDISPLAYMODE               d3ddm;
781     CURSORINFO                   info;
782     IDirect3DSurface9 *cursor = NULL;
783     HCURSOR cur;
784
785     memset(&info, 0, sizeof(info));
786     info.cbSize = sizeof(info);
787     ok(GetCursorInfo(&info), "GetCursorInfo failed\n");
788     cur = info.hCursor;
789
790     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
791     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
792     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
793     ok(hwnd != NULL, "Failed to create window\n");
794     if (!pD3d || !hwnd) goto cleanup;
795
796     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
797     ZeroMemory( &d3dpp, sizeof(d3dpp) );
798     d3dpp.Windowed         = TRUE;
799     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
800     d3dpp.BackBufferFormat = d3ddm.Format;
801
802     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
803                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
804     ok(hr == S_OK || hr == D3DERR_NOTAVAILABLE,
805        "Failed to create IDirect3D9Device (%08x)\n", hr);
806     if (FAILED(hr)) goto cleanup;
807
808     IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 32, 32, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &cursor, 0);
809     ok(cursor != NULL, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %08x\n", hr);
810
811     /* Initially hidden */
812     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
813     ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
814
815     /* Not enabled without a surface*/
816     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
817     ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
818
819     /* Fails */
820     hr = IDirect3DDevice9_SetCursorProperties(pDevice, 0, 0, NULL);
821     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_SetCursorProperties returned %08x\n", hr);
822
823     hr = IDirect3DDevice9_SetCursorProperties(pDevice, 0, 0, cursor);
824     ok(hr == D3D_OK, "IDirect3DDevice9_SetCursorProperties returned %08x\n", hr);
825
826     IDirect3DSurface9_Release(cursor);
827
828     memset(&info, 0, sizeof(info));
829     info.cbSize = sizeof(info);
830     ok(GetCursorInfo(&info), "GetCursorInfo failed\n");
831     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
832     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
833
834     /* Still hidden */
835     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
836     ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
837
838     /* Enabled now*/
839     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
840     ok(hr == TRUE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
841
842     /* GDI cursor unchanged */
843     memset(&info, 0, sizeof(info));
844     info.cbSize = sizeof(info);
845     ok(GetCursorInfo(&info), "GetCursorInfo failed\n");
846     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
847     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
848
849 cleanup:
850     if (pDevice)
851     {
852         UINT refcount = IDirect3DDevice9_Release(pDevice);
853         ok(!refcount, "Device has %u references left.\n", refcount);
854     }
855     if (pD3d) IDirect3D9_Release(pD3d);
856     DestroyWindow( hwnd );
857 }
858
859 static void test_reset(void)
860 {
861     HRESULT                      hr;
862     HWND                         hwnd               = NULL;
863     RECT                         winrect;
864     IDirect3D9                  *pD3d               = NULL;
865     D3DPRESENT_PARAMETERS        d3dpp;
866     D3DDISPLAYMODE               d3ddm, d3ddm2;
867     D3DVIEWPORT9                 vp;
868     DWORD                        width, orig_width = GetSystemMetrics(SM_CXSCREEN);
869     DWORD                        height, orig_height = GetSystemMetrics(SM_CYSCREEN);
870     IDirect3DSwapChain9          *pSwapchain;
871     IDirect3DSurface9            *surface;
872     IDirect3DTexture9            *texture;
873     IDirect3DVertexShader9       *shader;
874     UINT                         i, adapter_mode_count;
875     D3DLOCKED_RECT               lockrect;
876     IDirect3DDevice9 *device1 = NULL;
877     IDirect3DDevice9 *device2 = NULL;
878     D3DCAPS9 caps;
879     struct
880     {
881         UINT w;
882         UINT h;
883     } *modes = NULL;
884     UINT mode_count = 0;
885
886     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
887     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
888     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
889     ok(hwnd != NULL, "Failed to create window\n");
890     if (!pD3d || !hwnd) goto cleanup;
891
892     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
893     adapter_mode_count = IDirect3D9_GetAdapterModeCount(pD3d, D3DADAPTER_DEFAULT, d3ddm.Format);
894     modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*modes) * adapter_mode_count);
895     for(i = 0; i < adapter_mode_count; ++i)
896     {
897         UINT j;
898         ZeroMemory( &d3ddm2, sizeof(d3ddm2) );
899         hr = IDirect3D9_EnumAdapterModes(pD3d, D3DADAPTER_DEFAULT, d3ddm.Format, i, &d3ddm2);
900         ok(hr == D3D_OK, "IDirect3D9_EnumAdapterModes returned %#x\n", hr);
901
902         for (j = 0; j < mode_count; ++j)
903         {
904             if (modes[j].w == d3ddm2.Width && modes[j].h == d3ddm2.Height)
905                 break;
906         }
907         if (j == mode_count)
908         {
909             modes[j].w = d3ddm2.Width;
910             modes[j].h = d3ddm2.Height;
911             ++mode_count;
912         }
913
914         /* We use them as invalid modes */
915         if((d3ddm2.Width == 801 && d3ddm2.Height == 600) ||
916            (d3ddm2.Width == 32 && d3ddm2.Height == 32)) {
917             skip("This system supports a screen resolution of %dx%d, not running mode tests\n",
918                  d3ddm2.Width, d3ddm2.Height);
919             goto cleanup;
920         }
921     }
922
923     if (mode_count < 2)
924     {
925         skip("Less than 2 modes supported, skipping mode tests\n");
926         goto cleanup;
927     }
928
929     i = 0;
930     if (modes[i].w == orig_width && modes[i].h == orig_height) ++i;
931
932     ZeroMemory( &d3dpp, sizeof(d3dpp) );
933     d3dpp.Windowed         = FALSE;
934     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
935     d3dpp.BackBufferWidth  = modes[i].w;
936     d3dpp.BackBufferHeight = modes[i].h;
937     d3dpp.BackBufferFormat = d3ddm.Format;
938     d3dpp.EnableAutoDepthStencil = TRUE;
939     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
940
941     hr = IDirect3D9_CreateDevice(pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
942             hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device1);
943     if (FAILED(hr))
944     {
945         skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
946         goto cleanup;
947     }
948     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
949     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after creation returned %#x\n", hr);
950
951     hr = IDirect3DDevice9_GetDeviceCaps(device1, &caps);
952     ok(SUCCEEDED(hr), "GetDeviceCaps failed, hr %#x.\n", hr);
953
954     width = GetSystemMetrics(SM_CXSCREEN);
955     height = GetSystemMetrics(SM_CYSCREEN);
956     ok(width == modes[i].w, "Screen width is %u, expected %u\n", width, modes[i].w);
957     ok(height == modes[i].h, "Screen height is %u, expected %u\n", height, modes[i].h);
958
959     hr = IDirect3DDevice9_GetViewport(device1, &vp);
960     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %08x\n", hr);
961     if(SUCCEEDED(hr))
962     {
963         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
964         ok(vp.Y == 0, "D3DVIEWPORT->Y = %d\n", vp.Y);
965         ok(vp.Width == modes[i].w, "D3DVIEWPORT->Width = %u, expected %u\n", vp.Width, modes[i].w);
966         ok(vp.Height == modes[i].h, "D3DVIEWPORT->Height = %u, expected %u\n", vp.Height, modes[i].h);
967         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %f\n", vp.MinZ);
968         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %f\n", vp.MaxZ);
969     }
970
971     i = 1;
972     vp.X = 10;
973     vp.Y = 20;
974     vp.MinZ = 2;
975     vp.MaxZ = 3;
976     hr = IDirect3DDevice9_SetViewport(device1, &vp);
977     ok(hr == D3D_OK, "IDirect3DDevice9_SetViewport failed with %08x\n", hr);
978
979     ZeroMemory( &d3dpp, sizeof(d3dpp) );
980     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
981     d3dpp.Windowed         = FALSE;
982     d3dpp.BackBufferWidth  = modes[i].w;
983     d3dpp.BackBufferHeight = modes[i].h;
984     d3dpp.BackBufferFormat = d3ddm.Format;
985     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
986     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
987     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
988     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
989
990     ZeroMemory(&vp, sizeof(vp));
991     hr = IDirect3DDevice9_GetViewport(device1, &vp);
992     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %08x\n", hr);
993     if(SUCCEEDED(hr))
994     {
995         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
996         ok(vp.Y == 0, "D3DVIEWPORT->Y = %d\n", vp.Y);
997         ok(vp.Width == modes[i].w, "D3DVIEWPORT->Width = %u, expected %u\n", vp.Width, modes[i].w);
998         ok(vp.Height == modes[i].h, "D3DVIEWPORT->Height = %u, expected %u\n", vp.Height, modes[i].h);
999         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %f\n", vp.MinZ);
1000         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %f\n", vp.MaxZ);
1001     }
1002
1003     width = GetSystemMetrics(SM_CXSCREEN);
1004     height = GetSystemMetrics(SM_CYSCREEN);
1005     ok(width == modes[i].w, "Screen width is %u, expected %u\n", width, modes[i].w);
1006     ok(height == modes[i].h, "Screen height is %u, expected %u\n", height, modes[i].h);
1007
1008     hr = IDirect3DDevice9_GetSwapChain(device1, 0, &pSwapchain);
1009     ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %08x\n", hr);
1010     if(SUCCEEDED(hr))
1011     {
1012         ZeroMemory(&d3dpp, sizeof(d3dpp));
1013         hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
1014         ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %08x\n", hr);
1015         if(SUCCEEDED(hr))
1016         {
1017             ok(d3dpp.BackBufferWidth == modes[i].w, "Back buffer width is %u, expected %u\n",
1018                     d3dpp.BackBufferWidth, modes[i].w);
1019             ok(d3dpp.BackBufferHeight == modes[i].h, "Back buffer height is %u, expected %u\n",
1020                     d3dpp.BackBufferHeight, modes[i].h);
1021         }
1022         IDirect3DSwapChain9_Release(pSwapchain);
1023     }
1024
1025     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1026     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1027     d3dpp.Windowed         = TRUE;
1028     d3dpp.BackBufferWidth  = 400;
1029     d3dpp.BackBufferHeight = 300;
1030     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1031     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1032     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1033     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1034
1035     width = GetSystemMetrics(SM_CXSCREEN);
1036     height = GetSystemMetrics(SM_CYSCREEN);
1037     ok(width == orig_width, "Screen width is %d\n", width);
1038     ok(height == orig_height, "Screen height is %d\n", height);
1039
1040     ZeroMemory(&vp, sizeof(vp));
1041     hr = IDirect3DDevice9_GetViewport(device1, &vp);
1042     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %08x\n", hr);
1043     if(SUCCEEDED(hr))
1044     {
1045         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
1046         ok(vp.Y == 0, "D3DVIEWPORT->Y = %d\n", vp.Y);
1047         ok(vp.Width == 400, "D3DVIEWPORT->Width = %d\n", vp.Width);
1048         ok(vp.Height == 300, "D3DVIEWPORT->Height = %d\n", vp.Height);
1049         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %f\n", vp.MinZ);
1050         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %f\n", vp.MaxZ);
1051     }
1052
1053     hr = IDirect3DDevice9_GetSwapChain(device1, 0, &pSwapchain);
1054     ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %08x\n", hr);
1055     if(SUCCEEDED(hr))
1056     {
1057         ZeroMemory(&d3dpp, sizeof(d3dpp));
1058         hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
1059         ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %08x\n", hr);
1060         if(SUCCEEDED(hr))
1061         {
1062             ok(d3dpp.BackBufferWidth == 400, "Back buffer width is %d\n", d3dpp.BackBufferWidth);
1063             ok(d3dpp.BackBufferHeight == 300, "Back buffer height is %d\n", d3dpp.BackBufferHeight);
1064         }
1065         IDirect3DSwapChain9_Release(pSwapchain);
1066     }
1067
1068     winrect.left = 0;
1069     winrect.top = 0;
1070     winrect.right = 200;
1071     winrect.bottom = 150;
1072     ok(AdjustWindowRect(&winrect, WS_OVERLAPPEDWINDOW, FALSE), "AdjustWindowRect failed\n");
1073     ok(SetWindowPos(hwnd, NULL, 0, 0,
1074                     winrect.right-winrect.left,
1075                     winrect.bottom-winrect.top,
1076                     SWP_NOMOVE|SWP_NOZORDER),
1077        "SetWindowPos failed\n");
1078
1079     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1080     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1081     d3dpp.Windowed         = TRUE;
1082     d3dpp.BackBufferWidth  = 0;
1083     d3dpp.BackBufferHeight = 0;
1084     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1085     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1086     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1087     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1088
1089     ZeroMemory(&vp, sizeof(vp));
1090     hr = IDirect3DDevice9_GetViewport(device1, &vp);
1091     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %08x\n", hr);
1092     if(SUCCEEDED(hr))
1093     {
1094         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
1095         ok(vp.Y == 0, "D3DVIEWPORT->Y = %d\n", vp.Y);
1096         todo_wine ok(vp.Width == 200, "D3DVIEWPORT->Width = %d\n", vp.Width);
1097         todo_wine ok(vp.Height == 150, "D3DVIEWPORT->Height = %d\n", vp.Height);
1098         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %f\n", vp.MinZ);
1099         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %f\n", vp.MaxZ);
1100     }
1101
1102     hr = IDirect3DDevice9_GetSwapChain(device1, 0, &pSwapchain);
1103     ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %08x\n", hr);
1104     if(SUCCEEDED(hr))
1105     {
1106         ZeroMemory(&d3dpp, sizeof(d3dpp));
1107         hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
1108         ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %08x\n", hr);
1109         if(SUCCEEDED(hr))
1110         {
1111             todo_wine ok(d3dpp.BackBufferWidth == 200, "Back buffer width is %d\n", d3dpp.BackBufferWidth);
1112             todo_wine ok(d3dpp.BackBufferHeight == 150, "Back buffer height is %d\n", d3dpp.BackBufferHeight);
1113         }
1114         IDirect3DSwapChain9_Release(pSwapchain);
1115     }
1116
1117     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1118     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1119     d3dpp.Windowed         = TRUE;
1120     d3dpp.BackBufferWidth  = 400;
1121     d3dpp.BackBufferHeight = 300;
1122
1123     /* _Reset fails if there is a resource in the default pool */
1124     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device1, 16, 16, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &surface, NULL);
1125     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
1126     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1127     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1128     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1129     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
1130     IDirect3DSurface9_Release(surface);
1131     /* Reset again to get the device out of the lost state */
1132     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1133     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1134     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1135     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1136
1137     if (caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP)
1138     {
1139         IDirect3DVolumeTexture9 *volume_texture;
1140
1141         hr = IDirect3DDevice9_CreateVolumeTexture(device1, 16, 16, 4, 1, 0,
1142                 D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &volume_texture, NULL);
1143         ok(SUCCEEDED(hr), "CreateVolumeTexture failed, hr %#x.\n", hr);
1144         hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1145         ok(hr == D3DERR_INVALIDCALL, "Reset returned %#x, expected %#x.\n", hr, D3DERR_INVALIDCALL);
1146         hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1147         ok(hr == D3DERR_DEVICENOTRESET, "TestCooperativeLevel returned %#x, expected %#x.\n",
1148                 hr, D3DERR_DEVICENOTRESET);
1149         IDirect3DVolumeTexture9_Release(volume_texture);
1150         hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1151         ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1152         hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1153         ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1154     }
1155     else
1156     {
1157         skip("Volume textures not supported.\n");
1158     }
1159
1160     /* Scratch, sysmem and managed pools are fine */
1161     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device1, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SCRATCH, &surface, NULL);
1162     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
1163     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1164     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1165     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1166     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1167     IDirect3DSurface9_Release(surface);
1168
1169     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device1, 16, 16,
1170             D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface, NULL);
1171     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
1172     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1173     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1174     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1175     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1176     IDirect3DSurface9_Release(surface);
1177
1178     /* The depth stencil should get reset to the auto depth stencil when present. */
1179     hr = IDirect3DDevice9_SetDepthStencilSurface(device1, NULL);
1180     ok(hr == D3D_OK, "SetDepthStencilSurface failed with 0x%08x\n", hr);
1181
1182     hr = IDirect3DDevice9_GetDepthStencilSurface(device1, &surface);
1183     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1184     ok(surface == NULL, "Depth stencil should be NULL\n");
1185
1186     d3dpp.EnableAutoDepthStencil = TRUE;
1187     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
1188     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1189     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with 0x%08x\n", hr);
1190
1191     hr = IDirect3DDevice9_GetDepthStencilSurface(device1, &surface);
1192     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1193     ok(surface != NULL, "Depth stencil should not be NULL\n");
1194     if (surface) IDirect3DSurface9_Release(surface);
1195
1196     d3dpp.EnableAutoDepthStencil = FALSE;
1197     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1198     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with 0x%08x\n", hr);
1199
1200     hr = IDirect3DDevice9_GetDepthStencilSurface(device1, &surface);
1201     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1202     ok(surface == NULL, "Depth stencil should be NULL\n");
1203
1204     /* Will a sysmem or scratch survive while locked */
1205     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device1, 16, 16,
1206             D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface, NULL);
1207     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
1208     hr = IDirect3DSurface9_LockRect(surface, &lockrect, NULL, D3DLOCK_DISCARD);
1209     ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned %08x\n", hr);
1210     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1211     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1212     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1213     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1214     IDirect3DSurface9_UnlockRect(surface);
1215     IDirect3DSurface9_Release(surface);
1216
1217     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device1, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SCRATCH, &surface, NULL);
1218     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
1219     hr = IDirect3DSurface9_LockRect(surface, &lockrect, NULL, D3DLOCK_DISCARD);
1220     ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned %08x\n", hr);
1221     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1222     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1223     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1224     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1225     IDirect3DSurface9_UnlockRect(surface);
1226     IDirect3DSurface9_Release(surface);
1227
1228     hr = IDirect3DDevice9_CreateTexture(device1, 16, 16, 0, 0, D3DFMT_R5G6B5, D3DPOOL_MANAGED, &texture, NULL);
1229     ok(hr == D3D_OK, "IDirect3DDevice9_CreateTexture returned %08x\n", hr);
1230     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1231     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1232     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1233     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1234     IDirect3DTexture9_Release(texture);
1235
1236     /* A reference held to an implicit surface causes failures as well */
1237     hr = IDirect3DDevice9_GetBackBuffer(device1, 0, 0, D3DBACKBUFFER_TYPE_MONO, &surface);
1238     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer returned %08x\n", hr);
1239     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1240     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1241     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1242     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
1243     IDirect3DSurface9_Release(surface);
1244     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1245     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1246     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1247     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1248
1249     /* Shaders are fine as well */
1250     hr = IDirect3DDevice9_CreateVertexShader(device1, simple_vs, &shader);
1251     ok(hr == D3D_OK, "IDirect3DDevice9_CreateVertexShader returned %08x\n", hr);
1252     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1253     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1254     IDirect3DVertexShader9_Release(shader);
1255
1256     /* Try setting invalid modes */
1257     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1258     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1259     d3dpp.Windowed         = FALSE;
1260     d3dpp.BackBufferWidth  = 32;
1261     d3dpp.BackBufferHeight = 32;
1262     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1263     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset to w=32, h=32, windowed=FALSE failed with %08x\n", hr);
1264     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1265     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
1266
1267     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1268     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1269     d3dpp.Windowed         = FALSE;
1270     d3dpp.BackBufferWidth  = 801;
1271     d3dpp.BackBufferHeight = 600;
1272     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1273     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset to w=801, h=600, windowed=FALSE failed with %08x\n", hr);
1274     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1275     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
1276
1277     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1278
1279     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1280     d3dpp.Windowed         = TRUE;
1281     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1282     d3dpp.BackBufferFormat = d3ddm.Format;
1283     d3dpp.EnableAutoDepthStencil = FALSE;
1284     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
1285
1286     hr = IDirect3D9_CreateDevice(pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
1287             hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device2);
1288     if (FAILED(hr))
1289     {
1290         skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
1291         goto cleanup;
1292     }
1293
1294     hr = IDirect3DDevice9_TestCooperativeLevel(device2);
1295     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after creation returned %#x\n", hr);
1296
1297     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1298     d3dpp.Windowed         = TRUE;
1299     d3dpp.BackBufferWidth  = 400;
1300     d3dpp.BackBufferHeight = 300;
1301     d3dpp.EnableAutoDepthStencil = TRUE;
1302     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
1303
1304     hr = IDirect3DDevice9_Reset(device2, &d3dpp);
1305     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with 0x%08x\n", hr);
1306
1307     if (FAILED(hr)) goto cleanup;
1308
1309     hr = IDirect3DDevice9_GetDepthStencilSurface(device2, &surface);
1310     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1311     ok(surface != NULL, "Depth stencil should not be NULL\n");
1312     if (surface) IDirect3DSurface9_Release(surface);
1313
1314 cleanup:
1315     HeapFree(GetProcessHeap(), 0, modes);
1316     if (device2)
1317     {
1318         UINT refcount = IDirect3DDevice9_Release(device2);
1319         ok(!refcount, "Device has %u references left.\n", refcount);
1320     }
1321     if (device1)
1322     {
1323         UINT refcount = IDirect3DDevice9_Release(device1);
1324         ok(!refcount, "Device has %u references left.\n", refcount);
1325     }
1326     if (pD3d) IDirect3D9_Release(pD3d);
1327     if (hwnd) DestroyWindow(hwnd);
1328 }
1329
1330 /* Test adapter display modes */
1331 static void test_display_modes(void)
1332 {
1333     D3DDISPLAYMODE dmode;
1334     IDirect3D9 *pD3d;
1335
1336     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1337     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1338     if(!pD3d) return;
1339
1340 #define TEST_FMT(x,r) do { \
1341     HRESULT res = IDirect3D9_EnumAdapterModes(pD3d, 0, (x), 0, &dmode); \
1342     ok(res==(r), "EnumAdapterModes("#x") did not return "#r" (got %08x)!\n", res); \
1343 } while(0)
1344
1345     TEST_FMT(D3DFMT_R8G8B8, D3DERR_INVALIDCALL);
1346     TEST_FMT(D3DFMT_A8R8G8B8, D3DERR_INVALIDCALL);
1347     TEST_FMT(D3DFMT_X8B8G8R8, D3DERR_INVALIDCALL);
1348     /* D3DFMT_R5G6B5 */
1349     TEST_FMT(D3DFMT_X1R5G5B5, D3DERR_INVALIDCALL);
1350     TEST_FMT(D3DFMT_A1R5G5B5, D3DERR_INVALIDCALL);
1351     TEST_FMT(D3DFMT_A4R4G4B4, D3DERR_INVALIDCALL);
1352     TEST_FMT(D3DFMT_R3G3B2, D3DERR_INVALIDCALL);
1353     TEST_FMT(D3DFMT_A8, D3DERR_INVALIDCALL);
1354     TEST_FMT(D3DFMT_A8R3G3B2, D3DERR_INVALIDCALL);
1355     TEST_FMT(D3DFMT_X4R4G4B4, D3DERR_INVALIDCALL);
1356     TEST_FMT(D3DFMT_A2B10G10R10, D3DERR_INVALIDCALL);
1357     TEST_FMT(D3DFMT_A8B8G8R8, D3DERR_INVALIDCALL);
1358     TEST_FMT(D3DFMT_X8B8G8R8, D3DERR_INVALIDCALL);
1359     TEST_FMT(D3DFMT_G16R16, D3DERR_INVALIDCALL);
1360     TEST_FMT(D3DFMT_A16B16G16R16, D3DERR_INVALIDCALL);
1361
1362     TEST_FMT(D3DFMT_A8P8, D3DERR_INVALIDCALL);
1363     TEST_FMT(D3DFMT_P8, D3DERR_INVALIDCALL);
1364
1365     TEST_FMT(D3DFMT_L8, D3DERR_INVALIDCALL);
1366     TEST_FMT(D3DFMT_A8L8, D3DERR_INVALIDCALL);
1367     TEST_FMT(D3DFMT_A4L4, D3DERR_INVALIDCALL);
1368
1369     TEST_FMT(D3DFMT_V8U8, D3DERR_INVALIDCALL);
1370     TEST_FMT(D3DFMT_L6V5U5, D3DERR_INVALIDCALL);
1371     TEST_FMT(D3DFMT_X8L8V8U8, D3DERR_INVALIDCALL);
1372     TEST_FMT(D3DFMT_Q8W8V8U8, D3DERR_INVALIDCALL);
1373     TEST_FMT(D3DFMT_V16U16, D3DERR_INVALIDCALL);
1374     TEST_FMT(D3DFMT_A2W10V10U10, D3DERR_INVALIDCALL);
1375
1376     TEST_FMT(D3DFMT_UYVY, D3DERR_INVALIDCALL);
1377     TEST_FMT(D3DFMT_YUY2, D3DERR_INVALIDCALL);
1378     TEST_FMT(D3DFMT_DXT1, D3DERR_INVALIDCALL);
1379     TEST_FMT(D3DFMT_DXT2, D3DERR_INVALIDCALL);
1380     TEST_FMT(D3DFMT_DXT3, D3DERR_INVALIDCALL);
1381     TEST_FMT(D3DFMT_DXT4, D3DERR_INVALIDCALL);
1382     TEST_FMT(D3DFMT_DXT5, D3DERR_INVALIDCALL);
1383     TEST_FMT(D3DFMT_MULTI2_ARGB8, D3DERR_INVALIDCALL);
1384     TEST_FMT(D3DFMT_G8R8_G8B8, D3DERR_INVALIDCALL);
1385     TEST_FMT(D3DFMT_R8G8_B8G8, D3DERR_INVALIDCALL);
1386
1387     TEST_FMT(D3DFMT_D16_LOCKABLE, D3DERR_INVALIDCALL);
1388     TEST_FMT(D3DFMT_D32, D3DERR_INVALIDCALL);
1389     TEST_FMT(D3DFMT_D15S1, D3DERR_INVALIDCALL);
1390     TEST_FMT(D3DFMT_D24S8, D3DERR_INVALIDCALL);
1391     TEST_FMT(D3DFMT_D24X8, D3DERR_INVALIDCALL);
1392     TEST_FMT(D3DFMT_D24X4S4, D3DERR_INVALIDCALL);
1393     TEST_FMT(D3DFMT_D16, D3DERR_INVALIDCALL);
1394     TEST_FMT(D3DFMT_L16, D3DERR_INVALIDCALL);
1395     TEST_FMT(D3DFMT_D32F_LOCKABLE, D3DERR_INVALIDCALL);
1396     TEST_FMT(D3DFMT_D24FS8, D3DERR_INVALIDCALL);
1397
1398     TEST_FMT(D3DFMT_VERTEXDATA, D3DERR_INVALIDCALL);
1399     TEST_FMT(D3DFMT_INDEX16, D3DERR_INVALIDCALL);
1400     TEST_FMT(D3DFMT_INDEX32, D3DERR_INVALIDCALL);
1401     TEST_FMT(D3DFMT_Q16W16V16U16, D3DERR_INVALIDCALL);
1402     /* Floating point formats */
1403     TEST_FMT(D3DFMT_R16F, D3DERR_INVALIDCALL);
1404     TEST_FMT(D3DFMT_G16R16F, D3DERR_INVALIDCALL);
1405     TEST_FMT(D3DFMT_A16B16G16R16F, D3DERR_INVALIDCALL);
1406
1407     /* IEEE formats */
1408     TEST_FMT(D3DFMT_R32F, D3DERR_INVALIDCALL);
1409     TEST_FMT(D3DFMT_G32R32F, D3DERR_INVALIDCALL);
1410     TEST_FMT(D3DFMT_A32B32G32R32F, D3DERR_INVALIDCALL);
1411
1412     TEST_FMT(D3DFMT_CxV8U8, D3DERR_INVALIDCALL);
1413
1414     TEST_FMT(0, D3DERR_INVALIDCALL);
1415
1416     IDirect3D9_Release(pD3d);
1417 }
1418
1419 static void test_scene(void)
1420 {
1421     HRESULT                      hr;
1422     HWND                         hwnd               = NULL;
1423     IDirect3D9                  *pD3d               = NULL;
1424     IDirect3DDevice9            *pDevice            = NULL;
1425     D3DPRESENT_PARAMETERS        d3dpp;
1426     D3DDISPLAYMODE               d3ddm;
1427     IDirect3DSurface9            *pSurface1 = NULL, *pSurface2 = NULL, *pSurface3 = NULL, *pRenderTarget = NULL;
1428     IDirect3DSurface9            *pBackBuffer = NULL, *pDepthStencil = NULL;
1429     RECT rect = {0, 0, 128, 128};
1430     D3DCAPS9                     caps;
1431
1432     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1433     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1434     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1435     ok(hwnd != NULL, "Failed to create window\n");
1436     if (!pD3d || !hwnd) goto cleanup;
1437
1438     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1439     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1440     d3dpp.Windowed         = TRUE;
1441     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1442     d3dpp.BackBufferWidth  = 800;
1443     d3dpp.BackBufferHeight = 600;
1444     d3dpp.BackBufferFormat = d3ddm.Format;
1445     d3dpp.EnableAutoDepthStencil = TRUE;
1446     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1447
1448     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1449                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1450     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1451     if(!pDevice)
1452     {
1453         skip("Failed to create a d3d device\n");
1454         goto cleanup;
1455     }
1456
1457     /* Get the caps, they will be needed to tell if an operation is supposed to be valid */
1458     memset(&caps, 0, sizeof(caps));
1459     hr = IDirect3DDevice9_GetDeviceCaps(pDevice, &caps);
1460     ok(hr == D3D_OK, "IDirect3DDevice9_GetCaps failed with %08x\n", hr);
1461     if(FAILED(hr)) goto cleanup;
1462
1463     /* Test an EndScene without BeginScene. Should return an error */
1464     hr = IDirect3DDevice9_EndScene(pDevice);
1465     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %08x\n", hr);
1466
1467     /* Test a normal BeginScene / EndScene pair, this should work */
1468     hr = IDirect3DDevice9_BeginScene(pDevice);
1469     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1470     if(SUCCEEDED(hr))
1471     {
1472         hr = IDirect3DDevice9_EndScene(pDevice);
1473         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1474     }
1475
1476     /* Test another EndScene without having begun a new scene. Should return an error */
1477     hr = IDirect3DDevice9_EndScene(pDevice);
1478     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %08x\n", hr);
1479
1480     /* Two nested BeginScene and EndScene calls */
1481     hr = IDirect3DDevice9_BeginScene(pDevice);
1482     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1483     hr = IDirect3DDevice9_BeginScene(pDevice);
1484     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_BeginScene returned %08x\n", hr);
1485     hr = IDirect3DDevice9_EndScene(pDevice);
1486     ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1487     hr = IDirect3DDevice9_EndScene(pDevice);
1488     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %08x\n", hr);
1489
1490     /* Create some surfaces to test stretchrect between the scenes */
1491     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface1, NULL);
1492     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %08x\n", hr);
1493     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface2, NULL);
1494     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %08x\n", hr);
1495     hr = IDirect3DDevice9_CreateDepthStencilSurface(pDevice, 800, 600, D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, FALSE, &pSurface3, NULL);
1496     ok(hr == D3D_OK, "IDirect3DDevice9_CreateDepthStencilSurface failed with %08x\n", hr);
1497     hr = IDirect3DDevice9_CreateRenderTarget(pDevice, 128, 128, d3ddm.Format, D3DMULTISAMPLE_NONE, 0, FALSE, &pRenderTarget, NULL);
1498     ok(hr == D3D_OK, "IDirect3DDevice9_CreateRenderTarget failed with %08x\n", hr);
1499
1500     hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
1501     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer failed with %08x\n", hr);
1502     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1503     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer failed with %08x\n", hr);
1504
1505     /* First make sure a simple StretchRect call works */
1506     if(pSurface1 && pSurface2) {
1507         hr = IDirect3DDevice9_StretchRect(pDevice, pSurface1, NULL, pSurface2, NULL, 0);
1508         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1509     }
1510     if(pBackBuffer && pRenderTarget) {
1511         hr = IDirect3DDevice9_StretchRect(pDevice, pBackBuffer, &rect, pRenderTarget, NULL, 0);
1512         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1513     }
1514     if(pDepthStencil && pSurface3) {
1515         HRESULT expected;
1516         if(0) /* Disabled for now because it crashes in wine */ {
1517             expected = caps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES ? D3D_OK : D3DERR_INVALIDCALL;
1518             hr = IDirect3DDevice9_StretchRect(pDevice, pDepthStencil, NULL, pSurface3, NULL, 0);
1519             ok( hr == expected, "IDirect3DDevice9_StretchRect returned %08x, expected %08x\n", hr, expected);
1520         }
1521     }
1522
1523     /* Now try it in a BeginScene - EndScene pair. Seems to be allowed in a beginScene - Endscene pair
1524      * with normal surfaces and render targets, but not depth stencil surfaces.
1525      */
1526     hr = IDirect3DDevice9_BeginScene(pDevice);
1527     ok( hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1528
1529     if(pSurface1 && pSurface2)
1530     {
1531         hr = IDirect3DDevice9_StretchRect(pDevice, pSurface1, NULL, pSurface2, NULL, 0);
1532         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1533     }
1534     if(pBackBuffer && pRenderTarget)
1535     {
1536         hr = IDirect3DDevice9_StretchRect(pDevice, pBackBuffer, &rect, pRenderTarget, NULL, 0);
1537         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1538     }
1539     if(pDepthStencil && pSurface3)
1540     {
1541         /* This is supposed to fail inside a BeginScene - EndScene pair. */
1542         hr = IDirect3DDevice9_StretchRect(pDevice, pDepthStencil, NULL, pSurface3, NULL, 0);
1543         ok( hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_StretchRect returned %08x, expected D3DERR_INVALIDCALL\n", hr);
1544     }
1545
1546     hr = IDirect3DDevice9_EndScene(pDevice);
1547     ok( hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1548
1549     /* Does a SetRenderTarget influence BeginScene / EndScene ?
1550      * Set a new render target, then see if it started a new scene. Flip the rt back and see if that maybe
1551      * ended the scene. Expected result is that the scene is not affected by SetRenderTarget
1552      */
1553     hr = IDirect3DDevice9_SetRenderTarget(pDevice, 0, pRenderTarget);
1554     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget failed with %08x\n", hr);
1555     hr = IDirect3DDevice9_BeginScene(pDevice);
1556     ok( hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1557     hr = IDirect3DDevice9_SetRenderTarget(pDevice, 0, pBackBuffer);
1558     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget failed with %08x\n", hr);
1559     hr = IDirect3DDevice9_EndScene(pDevice);
1560     ok( hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1561
1562 cleanup:
1563     if(pRenderTarget) IDirect3DSurface9_Release(pRenderTarget);
1564     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1565     if(pBackBuffer) IDirect3DSurface9_Release(pBackBuffer);
1566     if(pSurface1) IDirect3DSurface9_Release(pSurface1);
1567     if(pSurface2) IDirect3DSurface9_Release(pSurface2);
1568     if(pSurface3) IDirect3DSurface9_Release(pSurface3);
1569     if (pDevice)
1570     {
1571         UINT refcount = IDirect3DDevice9_Release(pDevice);
1572         ok(!refcount, "Device has %u references left.\n", refcount);
1573     }
1574     if (pD3d) IDirect3D9_Release(pD3d);
1575     if(hwnd) DestroyWindow(hwnd);
1576 }
1577
1578 static void test_limits(void)
1579 {
1580     HRESULT                      hr;
1581     HWND                         hwnd               = NULL;
1582     IDirect3D9                  *pD3d               = NULL;
1583     IDirect3DDevice9            *pDevice            = NULL;
1584     D3DPRESENT_PARAMETERS        d3dpp;
1585     D3DDISPLAYMODE               d3ddm;
1586     IDirect3DTexture9           *pTexture           = NULL;
1587     int i;
1588
1589     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1590     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1591     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1592     ok(hwnd != NULL, "Failed to create window\n");
1593     if (!pD3d || !hwnd) goto cleanup;
1594
1595     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1596     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1597     d3dpp.Windowed         = TRUE;
1598     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1599     d3dpp.BackBufferWidth  = 800;
1600     d3dpp.BackBufferHeight = 600;
1601     d3dpp.BackBufferFormat = d3ddm.Format;
1602     d3dpp.EnableAutoDepthStencil = TRUE;
1603     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1604
1605     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1606                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1607     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1608     if(!pDevice)
1609     {
1610         skip("Failed to create a d3d device\n");
1611         goto cleanup;
1612     }
1613
1614     hr = IDirect3DDevice9_CreateTexture(pDevice, 16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture, NULL);
1615     ok(hr == D3D_OK, "IDirect3DDevice9_CreateTexture failed with %08x\n", hr);
1616     if(!pTexture) goto cleanup;
1617
1618     /* There are 16 pixel samplers. We should be able to access all of them */
1619     for(i = 0; i < 16; i++) {
1620         hr = IDirect3DDevice9_SetTexture(pDevice, i, (IDirect3DBaseTexture9 *) pTexture);
1621         ok(hr == D3D_OK, "IDirect3DDevice9_SetTexture for sampler %d failed with %08x\n", i, hr);
1622         hr = IDirect3DDevice9_SetTexture(pDevice, i, NULL);
1623         ok(hr == D3D_OK, "IDirect3DDevice9_SetTexture for sampler %d failed with %08x\n", i, hr);
1624         hr = IDirect3DDevice9_SetSamplerState(pDevice, i, D3DSAMP_SRGBTEXTURE, TRUE);
1625         ok(hr == D3D_OK, "IDirect3DDevice9_SetSamplerState for sampler %d failed with %08x\n", i, hr);
1626     }
1627
1628     /* Now test all 8 textures stage states */
1629     for(i = 0; i < 8; i++) {
1630         hr = IDirect3DDevice9_SetTextureStageState(pDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
1631         ok(hr == D3D_OK, "IDirect3DDevice9_SetTextureStageState for texture %d failed with %08x\n", i, hr);
1632     }
1633
1634     /* Investigations show that accessing higher samplers / textures stage states does not return an error either. Writing
1635      * to too high samplers(approximately sampler 40) causes memory corruption in windows, so there is no bounds checking
1636      * but how do I test that?
1637      */
1638 cleanup:
1639     if(pTexture) IDirect3DTexture9_Release(pTexture);
1640     if (pDevice)
1641     {
1642         UINT refcount = IDirect3D9_Release(pDevice);
1643         ok(!refcount, "Device has %u references left.\n", refcount);
1644     }
1645     if (pD3d) IDirect3D9_Release(pD3d);
1646     if(hwnd) DestroyWindow(hwnd);
1647 }
1648
1649 static void test_depthstenciltest(void)
1650 {
1651     HRESULT                      hr;
1652     HWND                         hwnd               = NULL;
1653     IDirect3D9                  *pD3d               = NULL;
1654     IDirect3DDevice9            *pDevice            = NULL;
1655     D3DPRESENT_PARAMETERS        d3dpp;
1656     D3DDISPLAYMODE               d3ddm;
1657     IDirect3DSurface9           *pDepthStencil           = NULL;
1658     IDirect3DSurface9           *pDepthStencil2          = NULL;
1659     DWORD                        state;
1660
1661     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1662     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1663     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1664     ok(hwnd != NULL, "Failed to create window\n");
1665     if (!pD3d || !hwnd) goto cleanup;
1666
1667     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1668     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1669     d3dpp.Windowed         = TRUE;
1670     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1671     d3dpp.BackBufferWidth  = 800;
1672     d3dpp.BackBufferHeight = 600;
1673     d3dpp.BackBufferFormat = d3ddm.Format;
1674     d3dpp.EnableAutoDepthStencil = TRUE;
1675     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1676
1677     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1678                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1679     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1680     if(!pDevice)
1681     {
1682         skip("Failed to create a d3d device\n");
1683         goto cleanup;
1684     }
1685
1686     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1687     ok(hr == D3D_OK && pDepthStencil != NULL, "IDirect3DDevice9_GetDepthStencilSurface failed with %08x\n", hr);
1688
1689     /* Try to clear */
1690     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1691     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %08x\n", hr);
1692
1693     hr = IDirect3DDevice9_SetDepthStencilSurface(pDevice, NULL);
1694     ok(hr == D3D_OK, "IDirect3DDevice9_SetDepthStencilSurface failed with %08x\n", hr);
1695
1696     /* Check if the set buffer is returned on a get. WineD3D had a bug with that once, prevent it from coming back */
1697     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil2);
1698     ok(hr == D3DERR_NOTFOUND && pDepthStencil2 == NULL, "IDirect3DDevice9_GetDepthStencilSurface failed with %08x\n", hr);
1699     if(pDepthStencil2) IDirect3DSurface9_Release(pDepthStencil2);
1700
1701     /* This left the render states untouched! */
1702     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1703     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1704     ok(state == D3DZB_TRUE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1705     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZWRITEENABLE, &state);
1706     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1707     ok(state == TRUE, "D3DRS_ZWRITEENABLE is %s\n", state ? "TRUE" : "FALSE");
1708     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_STENCILENABLE, &state);
1709     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1710     ok(state == FALSE, "D3DRS_STENCILENABLE is %s\n", state ? "TRUE" : "FALSE");
1711     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_STENCILWRITEMASK, &state);
1712     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1713     ok(state == 0xffffffff, "D3DRS_STENCILWRITEMASK is 0x%08x\n", state);
1714
1715     /* This is supposed to fail now */
1716     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1717     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Clear failed with %08x\n", hr);
1718
1719     hr = IDirect3DDevice9_SetRenderState(pDevice, D3DRS_ZENABLE, D3DZB_FALSE);
1720     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState failed with %08x\n", hr);
1721
1722     hr = IDirect3DDevice9_SetDepthStencilSurface(pDevice, pDepthStencil);
1723     ok(hr == D3D_OK, "IDirect3DDevice9_SetDepthStencilSurface failed with %08x\n", hr);
1724
1725     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1726     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1727     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1728
1729     /* Now it works again */
1730     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1731     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %08x\n", hr);
1732
1733     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1734     if(pDevice) IDirect3D9_Release(pDevice);
1735
1736     /* Now see if autodepthstencil disable is honored. First, without a format set */
1737     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1738     d3dpp.Windowed         = TRUE;
1739     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1740     d3dpp.BackBufferWidth  = 800;
1741     d3dpp.BackBufferHeight = 600;
1742     d3dpp.BackBufferFormat = d3ddm.Format;
1743     d3dpp.EnableAutoDepthStencil = FALSE;
1744     d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
1745
1746     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1747                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1748     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1749     if(!pDevice)
1750     {
1751         skip("Failed to create a d3d device\n");
1752         goto cleanup;
1753     }
1754
1755     pDepthStencil = NULL;
1756     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1757     ok(hr == D3DERR_NOTFOUND && pDepthStencil == NULL, "IDirect3DDevice9_GetDepthStencilSurface returned %08x, surface = %p\n", hr, pDepthStencil);
1758     if(pDepthStencil) {
1759         IDirect3DSurface9_Release(pDepthStencil);
1760         pDepthStencil = NULL;
1761     }
1762
1763     /* Check the depth test state */
1764     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1765     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1766     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1767
1768     if(pDevice) IDirect3D9_Release(pDevice);
1769
1770     /* Next, try EnableAutoDepthStencil FALSE with a depth stencil format set */
1771     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1772     d3dpp.Windowed         = TRUE;
1773     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1774     d3dpp.BackBufferWidth  = 800;
1775     d3dpp.BackBufferHeight = 600;
1776     d3dpp.BackBufferFormat = d3ddm.Format;
1777     d3dpp.EnableAutoDepthStencil = FALSE;
1778     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1779
1780     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1781                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1782     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1783     if(!pDevice)
1784     {
1785         skip("Failed to create a d3d device\n");
1786         goto cleanup;
1787     }
1788
1789     pDepthStencil = NULL;
1790     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1791     ok(hr == D3DERR_NOTFOUND && pDepthStencil == NULL, "IDirect3DDevice9_GetDepthStencilSurface returned %08x, surface = %p\n", hr, pDepthStencil);
1792     if(pDepthStencil) {
1793         IDirect3DSurface9_Release(pDepthStencil);
1794         pDepthStencil = NULL;
1795     }
1796
1797     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1798     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1799     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1800
1801 cleanup:
1802     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1803     if (pDevice)
1804     {
1805         UINT refcount = IDirect3D9_Release(pDevice);
1806         ok(!refcount, "Device has %u references left.\n", refcount);
1807     }
1808     if (pD3d) IDirect3D9_Release(pD3d);
1809     if(hwnd) DestroyWindow(hwnd);
1810 }
1811
1812 static void test_get_rt(void)
1813 {
1814     IDirect3DSurface9 *backbuffer, *rt;
1815     IDirect3DDevice9 *device;
1816     IDirect3D9 *d3d9;
1817     D3DCAPS9 caps;
1818     HWND window;
1819     HRESULT hr;
1820     ULONG ref;
1821     UINT i;
1822
1823     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
1824     {
1825         skip("Failed to create IDirect3D9 object, skipping tests.\n");
1826         return;
1827     }
1828
1829     window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
1830             0, 0, 128, 128, 0, 0, 0, 0);
1831     device = create_device(d3d9, window, window, TRUE);
1832     if (!device)
1833     {
1834         skip("Failed to create a D3D device, skipping tests.\n");
1835         goto done;
1836     }
1837
1838     hr = IDirect3DDevice9_GetRenderTarget(device, 0, &backbuffer);
1839     ok(SUCCEEDED(hr), "Failed to get backbuffer, hr %#x.\n", hr);
1840     ok(!!backbuffer, "Got a NULL backbuffer.\n");
1841
1842     hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1843     ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
1844
1845     for (i = 1; i < caps.NumSimultaneousRTs; ++i)
1846     {
1847         rt = backbuffer;
1848         hr = IDirect3DDevice9_GetRenderTarget(device, i, &rt);
1849         ok(hr == D3DERR_NOTFOUND, "IDirect3DDevice9_GetRenderTarget returned %#x.\n", hr);
1850         ok(!rt, "Got rt %p.\n", rt);
1851     }
1852
1853     IDirect3DSurface9_Release(backbuffer);
1854
1855     ref = IDirect3DDevice9_Release(device);
1856     ok(!ref, "The device was not properly freed: refcount %u.\n", ref);
1857 done:
1858     IDirect3D9_Release(d3d9);
1859     DestroyWindow(window);
1860 }
1861
1862 /* Test what happens when IDirect3DDevice9_DrawIndexedPrimitive is called without a valid index buffer set. */
1863 static void test_draw_indexed(void)
1864 {
1865     static const struct {
1866         float position[3];
1867         DWORD color;
1868     } quad[] = {
1869         {{-1.0f, -1.0f, 0.0f}, 0xffff0000},
1870         {{-1.0f,  1.0f, 0.0f}, 0xffff0000},
1871         {{ 1.0f,  1.0f, 0.0f}, 0xffff0000},
1872         {{ 1.0f, -1.0f, 0.0f}, 0xffff0000},
1873     };
1874     WORD indices[] = {0, 1, 2, 3, 0, 2};
1875
1876     static const D3DVERTEXELEMENT9 decl_elements[] = {
1877         {0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1878         {0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,    D3DDECLUSAGE_COLOR, 0},
1879         D3DDECL_END()
1880     };
1881
1882     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
1883     IDirect3DVertexBuffer9 *vertex_buffer = NULL;
1884     IDirect3DIndexBuffer9 *index_buffer = NULL;
1885     D3DPRESENT_PARAMETERS present_parameters;
1886     IDirect3DDevice9 *device = NULL;
1887     IDirect3D9 *d3d9;
1888     HRESULT hr;
1889     HWND hwnd;
1890     void *ptr;
1891
1892     hwnd = CreateWindow("d3d9_test_wc", "d3d9_test",
1893             0, 0, 0, 10, 10, 0, 0, 0, 0);
1894     if (!hwnd)
1895     {
1896         skip("Failed to create window\n");
1897         return;
1898     }
1899
1900     d3d9 = pDirect3DCreate9(D3D_SDK_VERSION);
1901     if (!d3d9)
1902     {
1903         skip("Failed to create IDirect3D9 object\n");
1904         goto cleanup;
1905     }
1906
1907     ZeroMemory(&present_parameters, sizeof(present_parameters));
1908     present_parameters.Windowed = TRUE;
1909     present_parameters.hDeviceWindow = hwnd;
1910     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1911
1912     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
1913             NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device);
1914     if (FAILED(hr) || !device)
1915     {
1916         skip("Failed to create device\n");
1917         goto cleanup;
1918     }
1919
1920     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
1921     ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (0x%08x)\n", hr);
1922     hr = IDirect3DDevice9_SetVertexDeclaration(device, NULL);
1923     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
1924
1925     hr = IDirect3DDevice9_CreateVertexBuffer(device, sizeof(quad), 0, 0, D3DPOOL_DEFAULT, &vertex_buffer, NULL);
1926     ok(SUCCEEDED(hr), "CreateVertexBuffer failed (0x%08x)\n", hr);
1927     hr = IDirect3DVertexBuffer9_Lock(vertex_buffer, 0, 0, &ptr, D3DLOCK_DISCARD);
1928     ok(SUCCEEDED(hr), "Lock failed (0x%08x)\n", hr);
1929     memcpy(ptr, quad, sizeof(quad));
1930     hr = IDirect3DVertexBuffer9_Unlock(vertex_buffer);
1931     ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1932     hr = IDirect3DDevice9_SetStreamSource(device, 0, vertex_buffer, 0, sizeof(*quad));
1933     ok(SUCCEEDED(hr), "SetStreamSource failed (0x%08x)\n", hr);
1934
1935     hr = IDirect3DDevice9_CreateIndexBuffer(device, sizeof(indices), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &index_buffer, NULL);
1936     ok(SUCCEEDED(hr), "CreateIndexBuffer failed (0x%08x)\n", hr);
1937     hr = IDirect3DIndexBuffer9_Lock(index_buffer, 0, 0, &ptr, D3DLOCK_DISCARD);
1938     ok(SUCCEEDED(hr), "Lock failed (0x%08x)\n", hr);
1939     memcpy(ptr, indices, sizeof(indices));
1940     hr = IDirect3DIndexBuffer9_Unlock(index_buffer);
1941     ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1942     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1943     ok(SUCCEEDED(hr), "SetRenderState D3DRS_LIGHTING failed (0x%08x)\n", hr);
1944     hr = IDirect3DDevice9_BeginScene(device);
1945     ok(SUCCEEDED(hr), "BeginScene failed (0x%08x)\n", hr);
1946
1947     /* NULL index buffer. Should fail */
1948     hr = IDirect3DDevice9_SetIndices(device, NULL);
1949     ok(SUCCEEDED(hr), "SetIndices failed (0x%08x)\n", hr);
1950     hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1951             4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1952     ok(hr == D3DERR_INVALIDCALL, "DrawIndexedPrimitive returned 0x%08x, expected D3DERR_INVALIDCALL (0x%08x)\n",
1953             hr, D3DERR_INVALIDCALL);
1954
1955     /* Valid index buffer, NULL vertex declaration. Should fail */
1956     hr = IDirect3DDevice9_SetIndices(device, index_buffer);
1957     ok(SUCCEEDED(hr), "SetIndices failed (0x%08x)\n", hr);
1958     hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1959             4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1960     ok(hr == D3DERR_INVALIDCALL, "DrawIndexedPrimitive returned 0x%08x, expected D3DERR_INVALIDCALL (0x%08x)\n",
1961             hr, D3DERR_INVALIDCALL);
1962
1963     /* Valid index buffer and vertex declaration. Should succeed */
1964     hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
1965     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
1966     hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1967             4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1968     ok(SUCCEEDED(hr), "DrawIndexedPrimitive failed (0x%08x)\n", hr);
1969
1970     hr = IDirect3DDevice9_EndScene(device);
1971     ok(SUCCEEDED(hr), "EndScene failed (0x%08x)\n", hr);
1972
1973     hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
1974     ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1975
1976     IDirect3DVertexBuffer9_Release(vertex_buffer);
1977     IDirect3DIndexBuffer9_Release(index_buffer);
1978     IDirect3DVertexDeclaration9_Release(vertex_declaration);
1979
1980 cleanup:
1981     if (device)
1982     {
1983         UINT refcount = IDirect3DDevice9_Release(device);
1984         ok(!refcount, "Device has %u references left.\n", refcount);
1985     }
1986     if (d3d9) IDirect3D9_Release(d3d9);
1987     if (hwnd) DestroyWindow(hwnd);
1988 }
1989
1990 static void test_null_stream(void)
1991 {
1992     IDirect3DVertexBuffer9 *buffer = NULL;
1993     D3DPRESENT_PARAMETERS present_parameters;
1994     IDirect3DDevice9 *device = NULL;
1995     IDirect3D9 *d3d9;
1996     HWND hwnd;
1997     HRESULT hr;
1998     IDirect3DVertexShader9 *shader = NULL;
1999     IDirect3DVertexDeclaration9 *decl = NULL;
2000     DWORD shader_code[] = {
2001         0xfffe0101,                             /* vs_1_1           */
2002         0x0000001f, 0x80000000, 0x900f0000,     /* dcl_position v0  */
2003         0x00000001, 0xc00f0000, 0x90e40000,     /* mov oPos, v0     */
2004         0x0000ffff                              /* end              */
2005     };
2006     static const D3DVERTEXELEMENT9 decl_elements[] = {
2007         {0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
2008         {1, 0,  D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,    D3DDECLUSAGE_COLOR, 0},
2009         D3DDECL_END()
2010     };
2011
2012     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
2013     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
2014     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
2015     ok(hwnd != NULL, "Failed to create window\n");
2016     if (!d3d9 || !hwnd) goto cleanup;
2017
2018     ZeroMemory(&present_parameters, sizeof(present_parameters));
2019     present_parameters.Windowed = TRUE;
2020     present_parameters.hDeviceWindow = hwnd;
2021     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2022
2023     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
2024                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device );
2025     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
2026     if(!device)
2027     {
2028         skip("Failed to create a d3d device\n");
2029         goto cleanup;
2030     }
2031
2032     hr = IDirect3DDevice9_CreateVertexShader(device, shader_code, &shader);
2033     if(FAILED(hr)) {
2034         skip("No vertex shader support\n");
2035         goto cleanup;
2036     }
2037     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &decl);
2038     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexDeclaration failed (0x%08x)\n", hr);
2039     if (FAILED(hr)) {
2040         skip("Vertex declaration handling not possible.\n");
2041         goto cleanup;
2042     }
2043     hr = IDirect3DDevice9_CreateVertexBuffer(device, 12 * sizeof(float), 0, 0, D3DPOOL_MANAGED, &buffer, NULL);
2044     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexBuffer failed (0x%08x)\n", hr);
2045     if (FAILED(hr)) {
2046         skip("Vertex buffer handling not possible.\n");
2047         goto cleanup;
2048     }
2049
2050     hr = IDirect3DDevice9_SetStreamSource(device, 0, buffer, 0, sizeof(float) * 3);
2051     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetStreamSource failed (0x%08x)\n", hr);
2052     hr = IDirect3DDevice9_SetStreamSource(device, 1, NULL, 0, 0);
2053     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetStreamSource failed (0x%08x)\n", hr);
2054     hr = IDirect3DDevice9_SetVertexShader(device, shader);
2055     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetVertexShader failed (0x%08x)\n", hr);
2056     hr = IDirect3DDevice9_SetVertexDeclaration(device, decl);
2057     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetVertexDeclaration failed (0x%08x)\n", hr);
2058
2059     hr = IDirect3DDevice9_BeginScene(device);
2060     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed (0x%08x)\n", hr);
2061     if(SUCCEEDED(hr)) {
2062         hr = IDirect3DDevice9_DrawPrimitive(device, D3DPT_POINTLIST, 0, 1);
2063         ok(SUCCEEDED(hr), "IDirect3DDevice9_DrawPrimitive failed (0x%08x)\n", hr);
2064
2065         hr = IDirect3DDevice9_EndScene(device);
2066         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed (0x%08x)\n", hr);
2067     }
2068
2069     IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
2070     IDirect3DDevice9_SetVertexShader(device, NULL);
2071     IDirect3DDevice9_SetVertexDeclaration(device, NULL);
2072
2073 cleanup:
2074     if (buffer) IDirect3DVertexBuffer9_Release(buffer);
2075     if(decl) IDirect3DVertexDeclaration9_Release(decl);
2076     if(shader) IDirect3DVertexShader9_Release(shader);
2077     if (device)
2078     {
2079         UINT refcount = IDirect3DDevice9_Release(device);
2080         ok(!refcount, "Device has %u references left.\n", refcount);
2081     }
2082     if(d3d9) IDirect3D9_Release(d3d9);
2083 }
2084
2085 static void test_lights(void)
2086 {
2087     D3DPRESENT_PARAMETERS present_parameters;
2088     IDirect3DDevice9 *device = NULL;
2089     IDirect3D9 *d3d9;
2090     HWND hwnd;
2091     HRESULT hr;
2092     unsigned int i;
2093     BOOL enabled;
2094     D3DCAPS9 caps;
2095
2096     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
2097     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
2098     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
2099     ok(hwnd != NULL, "Failed to create window\n");
2100     if (!d3d9 || !hwnd) goto cleanup;
2101
2102     ZeroMemory(&present_parameters, sizeof(present_parameters));
2103     present_parameters.Windowed = TRUE;
2104     present_parameters.hDeviceWindow = hwnd;
2105     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2106
2107     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
2108                                   D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
2109     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
2110        "IDirect3D9_CreateDevice failed with %08x\n", hr);
2111     if(!device)
2112     {
2113         skip("Failed to create a d3d device\n");
2114         goto cleanup;
2115     }
2116
2117     memset(&caps, 0, sizeof(caps));
2118     hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
2119     ok(hr == D3D_OK, "IDirect3DDevice9_GetDeviceCaps failed with %08x\n", hr);
2120
2121     for(i = 1; i <= caps.MaxActiveLights; i++) {
2122         hr = IDirect3DDevice9_LightEnable(device, i, TRUE);
2123         ok(hr == D3D_OK, "Enabling light %u failed with %08x\n", i, hr);
2124         hr = IDirect3DDevice9_GetLightEnable(device, i, &enabled);
2125         ok(hr == D3D_OK, "GetLightEnable on light %u failed with %08x\n", i, hr);
2126         ok(enabled, "Light %d is %s\n", i, enabled ? "enabled" : "disabled");
2127     }
2128
2129     /* TODO: Test the rendering results in this situation */
2130     hr = IDirect3DDevice9_LightEnable(device, i + 1, TRUE);
2131     ok(hr == D3D_OK, "Enabling one light more than supported returned %08x\n", hr);
2132     hr = IDirect3DDevice9_GetLightEnable(device, i + 1, &enabled);
2133     ok(hr == D3D_OK, "GetLightEnable on light %u failed with %08x\n", i + 1, hr);
2134     ok(enabled, "Light %d is %s\n", i + 1, enabled ? "enabled" : "disabled");
2135     hr = IDirect3DDevice9_LightEnable(device, i + 1, FALSE);
2136     ok(hr == D3D_OK, "Disabling the additional returned %08x\n", hr);
2137
2138     for(i = 1; i <= caps.MaxActiveLights; i++) {
2139         hr = IDirect3DDevice9_LightEnable(device, i, FALSE);
2140         ok(hr == D3D_OK, "Disabling light %u failed with %08x\n", i, hr);
2141     }
2142
2143 cleanup:
2144     if (device)
2145     {
2146         UINT refcount = IDirect3DDevice9_Release(device);
2147         ok(!refcount, "Device has %u references left.\n", refcount);
2148     }
2149     if(d3d9) IDirect3D9_Release(d3d9);
2150 }
2151
2152 static void test_set_stream_source(void)
2153 {
2154     D3DPRESENT_PARAMETERS present_parameters;
2155     IDirect3DDevice9 *device = NULL;
2156     IDirect3D9 *d3d9;
2157     HWND hwnd;
2158     HRESULT hr;
2159     IDirect3DVertexBuffer9 *pVertexBuffer = NULL;
2160
2161     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
2162     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
2163     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
2164     ok(hwnd != NULL, "Failed to create window\n");
2165     if (!d3d9 || !hwnd) goto cleanup;
2166
2167     ZeroMemory(&present_parameters, sizeof(present_parameters));
2168     present_parameters.Windowed = TRUE;
2169     present_parameters.hDeviceWindow = hwnd;
2170     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2171
2172     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
2173                                   D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
2174     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
2175        "IDirect3D9_CreateDevice failed with %08x\n", hr);
2176     if(!device)
2177     {
2178         hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd,
2179                                       D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
2180         ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
2181            "IDirect3D9_CreateDevice failed with %08x\n", hr);
2182         if(!device)
2183         {
2184             skip("Failed to create a d3d device\n");
2185             goto cleanup;
2186         }
2187     }
2188
2189     hr = IDirect3DDevice9_CreateVertexBuffer( device, 512, 0, 0, D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
2190     ok(hr == D3D_OK, "Failed to create a vertex buffer, hr = %08x\n", hr);
2191     if (SUCCEEDED(hr)) {
2192         /* Some cards(Geforce 7400 at least) accept non-aligned offsets, others(radeon 9000 verified) reject it,
2193          * so accept both results. Wine currently rejects this to be able to optimize the vbo conversion, but writes
2194          * a WARN
2195          */
2196         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 0, 32);
2197         ok(hr == D3D_OK, "Failed to set the stream source, offset 0, hr = %08x\n", hr);
2198         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 1, 32);
2199         ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 1, hr = %08x\n", hr);
2200         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 2, 32);
2201         ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 2, hr = %08x\n", hr);
2202         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 3, 32);
2203         ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 3, hr = %08x\n", hr);
2204         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 4, 32);
2205         ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %08x\n", hr);
2206     }
2207     /* Try to set the NULL buffer with an offset and stride 0 */
2208     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
2209     ok(hr == D3D_OK, "Failed to set the stream source, offset 0, hr = %08x\n", hr);
2210     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 1, 0);
2211     ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 1, hr = %08x\n", hr);
2212     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 2, 0);
2213     ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 2, hr = %08x\n", hr);
2214     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 3, 0);
2215     ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 3, hr = %08x\n", hr);
2216     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 4, 0);
2217     ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %08x\n", hr);
2218
2219     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
2220     ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %08x\n", hr);
2221
2222 cleanup:
2223     if (pVertexBuffer) IDirect3DVertexBuffer9_Release(pVertexBuffer);
2224     if (device)
2225     {
2226         UINT refcount = IDirect3DDevice9_Release(device);
2227         ok(!refcount, "Device has %u references left.\n", refcount);
2228     }
2229     if(d3d9) IDirect3D9_Release(d3d9);
2230 }
2231
2232 struct formats {
2233     D3DFORMAT DisplayFormat;
2234     D3DFORMAT BackBufferFormat;
2235     BOOL shouldPass;
2236 };
2237
2238 static const struct formats r5g6b5_format_list[] =
2239 {
2240     { D3DFMT_R5G6B5, D3DFMT_R5G6B5, TRUE },
2241     { D3DFMT_R5G6B5, D3DFMT_X1R5G5B5, FALSE },
2242     { D3DFMT_R5G6B5, D3DFMT_A1R5G5B5, FALSE },
2243     { D3DFMT_R5G6B5, D3DFMT_X8R8G8B8, FALSE },
2244     { D3DFMT_R5G6B5, D3DFMT_A8R8G8B8, FALSE },
2245     { 0, 0, 0}
2246 };
2247
2248 static const struct formats x1r5g5b5_format_list[] =
2249 {
2250     { D3DFMT_X1R5G5B5, D3DFMT_R5G6B5, FALSE },
2251     { D3DFMT_X1R5G5B5, D3DFMT_X1R5G5B5, TRUE },
2252     { D3DFMT_X1R5G5B5, D3DFMT_A1R5G5B5, TRUE },
2253     { D3DFMT_X1R5G5B5, D3DFMT_X8R8G8B8, FALSE },
2254     { D3DFMT_X1R5G5B5, D3DFMT_A8R8G8B8, FALSE },
2255
2256     /* A1R5G5B5 should not be usable as a display format, it is backbuffer-only */
2257     { D3DFMT_A1R5G5B5, D3DFMT_R5G6B5, FALSE },
2258     { D3DFMT_A1R5G5B5, D3DFMT_X1R5G5B5, FALSE },
2259     { D3DFMT_A1R5G5B5, D3DFMT_A1R5G5B5, FALSE },
2260     { D3DFMT_A1R5G5B5, D3DFMT_X8R8G8B8, FALSE },
2261     { D3DFMT_A1R5G5B5, D3DFMT_A8R8G8B8, FALSE },
2262     { 0, 0, 0}
2263 };
2264
2265 static const struct formats x8r8g8b8_format_list[] =
2266 {
2267     { D3DFMT_X8R8G8B8, D3DFMT_R5G6B5, FALSE },
2268     { D3DFMT_X8R8G8B8, D3DFMT_X1R5G5B5, FALSE },
2269     { D3DFMT_X8R8G8B8, D3DFMT_A1R5G5B5, FALSE },
2270     { D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, TRUE },
2271     { D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, TRUE },
2272
2273     /* A1R8G8B8 should not be usable as a display format, it is backbuffer-only */
2274     { D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, FALSE },
2275     { D3DFMT_A8R8G8B8, D3DFMT_X1R5G5B5, FALSE },
2276     { D3DFMT_A8R8G8B8, D3DFMT_A1R5G5B5, FALSE },
2277     { D3DFMT_A8R8G8B8, D3DFMT_X8R8G8B8, FALSE },
2278     { D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, FALSE },
2279     { 0, 0, 0}
2280 };
2281
2282 static void test_display_formats(void)
2283 {
2284     /* Direct3D9 offers 4 display formats R5G6B5, X1R5G5B5, X8R8G8B8 and A2R10G10B10.
2285      * Next to these there are 6 different backbuffer formats. Only a fixed number of
2286      * combinations are possible in FULLSCREEN mode. In windowed mode more combinations are
2287      * allowed due to depth conversion and this is likely driver dependent.
2288      * This test checks which combinations are possible in fullscreen mode and this should not be driver dependent.
2289      * TODO: handle A2R10G10B10 but what hardware supports it? Parhelia? It is very rare. */
2290
2291     UINT Adapter = D3DADAPTER_DEFAULT;
2292     D3DDEVTYPE DeviceType = D3DDEVTYPE_HAL;
2293     int i, nmodes;
2294     HRESULT hr;
2295
2296     IDirect3D9 *d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
2297     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
2298     if(!d3d9) return;
2299
2300     nmodes = IDirect3D9_GetAdapterModeCount(d3d9, D3DADAPTER_DEFAULT, D3DFMT_R5G6B5);
2301     if(!nmodes) {
2302         skip("Display format R5G6B5 not supported, skipping\n");
2303     } else {
2304         trace("Testing display format R5G6B5\n");
2305         for(i=0; r5g6b5_format_list[i].DisplayFormat != 0; i++)
2306         {
2307             hr = IDirect3D9_CheckDeviceType(d3d9, Adapter, DeviceType, r5g6b5_format_list[i].DisplayFormat, r5g6b5_format_list[i].BackBufferFormat, FALSE);
2308
2309             if(r5g6b5_format_list[i].shouldPass)
2310                 ok(hr == D3D_OK ||
2311                    broken(hr == D3DERR_NOTAVAILABLE),
2312                    "format %d %d didn't pass with hr=%#08x\n", r5g6b5_format_list[i].DisplayFormat, r5g6b5_format_list[i].BackBufferFormat, hr);
2313             else
2314                 ok(hr != D3D_OK, "format %d %d didn't pass while it was expected to\n", r5g6b5_format_list[i].DisplayFormat, r5g6b5_format_list[i].BackBufferFormat);
2315         }
2316     }
2317
2318     nmodes = IDirect3D9_GetAdapterModeCount(d3d9, D3DADAPTER_DEFAULT, D3DFMT_X1R5G5B5);
2319     if(!nmodes) {
2320         skip("Display format X1R5G5B5 not supported, skipping\n");
2321     } else {
2322         trace("Testing display format X1R5G5B5\n");
2323         for(i=0; x1r5g5b5_format_list[i].DisplayFormat != 0; i++)
2324         {
2325             hr = IDirect3D9_CheckDeviceType(d3d9, Adapter, DeviceType, x1r5g5b5_format_list[i].DisplayFormat, x1r5g5b5_format_list[i].BackBufferFormat, FALSE);
2326
2327             if(x1r5g5b5_format_list[i].shouldPass)
2328                 ok(hr == D3D_OK, "format %d %d didn't pass with hr=%#08x\n", x1r5g5b5_format_list[i].DisplayFormat, x1r5g5b5_format_list[i].BackBufferFormat, hr);
2329             else
2330                 ok(hr != D3D_OK, "format %d %d didn't pass while it was expected to\n", x1r5g5b5_format_list[i].DisplayFormat, x1r5g5b5_format_list[i].BackBufferFormat);
2331         }
2332     }
2333
2334     nmodes = IDirect3D9_GetAdapterModeCount(d3d9, D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8);
2335     if(!nmodes) {
2336         skip("Display format X8R8G8B8 not supported, skipping\n");
2337     } else {
2338         trace("Testing display format X8R8G8B8\n");
2339         for(i=0; x8r8g8b8_format_list[i].DisplayFormat != 0; i++)
2340         {
2341             hr = IDirect3D9_CheckDeviceType(d3d9, Adapter, DeviceType, x8r8g8b8_format_list[i].DisplayFormat, x8r8g8b8_format_list[i].BackBufferFormat, FALSE);
2342
2343             if(x8r8g8b8_format_list[i].shouldPass)
2344                 ok(hr == D3D_OK ||
2345                    broken(hr == D3DERR_NOTAVAILABLE),
2346                    "format %d %d didn't pass with hr=%#08x\n", x8r8g8b8_format_list[i].DisplayFormat, x8r8g8b8_format_list[i].BackBufferFormat, hr);
2347             else
2348                 ok(hr != D3D_OK, "format %d %d didn't pass while it was expected to\n", x8r8g8b8_format_list[i].DisplayFormat, x8r8g8b8_format_list[i].BackBufferFormat);
2349         }
2350     }
2351
2352     if(d3d9) IDirect3D9_Release(d3d9);
2353 }
2354
2355 static void test_scissor_size(void)
2356 {
2357     IDirect3D9 *d3d9_ptr = 0;
2358     unsigned int i;
2359     static struct {
2360         int winx; int winy; int backx; int backy; BOOL window;
2361     } scts[] = { /* scissor tests */
2362         {800, 600, 640, 480, TRUE},
2363         {800, 600, 640, 480, FALSE},
2364         {640, 480, 800, 600, TRUE},
2365         {640, 480, 800, 600, FALSE},
2366     };
2367
2368     d3d9_ptr = pDirect3DCreate9(D3D_SDK_VERSION);
2369     ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
2370     if (!d3d9_ptr){
2371         skip("Failed to create IDirect3D9 object\n");
2372         return;
2373     }
2374
2375     for(i=0; i<sizeof(scts)/sizeof(scts[0]); i++) {
2376         IDirect3DDevice9 *device_ptr = 0;
2377         D3DPRESENT_PARAMETERS present_parameters;
2378         HRESULT hr;
2379         HWND hwnd = 0;
2380         RECT scissorrect;
2381
2382         hwnd = CreateWindow("d3d9_test_wc", "d3d9_test",
2383                         WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, scts[i].winx, scts[i].winy, 0, 0, 0, 0);
2384
2385         if (!scts[i].window)
2386         {
2387             scts[i].backx = screen_width;
2388             scts[i].backy = screen_height;
2389         }
2390
2391         ZeroMemory(&present_parameters, sizeof(present_parameters));
2392         present_parameters.Windowed = scts[i].window;
2393         present_parameters.hDeviceWindow = hwnd;
2394         present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2395         present_parameters.BackBufferWidth = scts[i].backx;
2396         present_parameters.BackBufferHeight = scts[i].backy;
2397         present_parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
2398         present_parameters.EnableAutoDepthStencil = TRUE;
2399         present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
2400
2401         hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
2402         if(FAILED(hr)) {
2403             present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
2404             hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
2405             if(FAILED(hr)) {
2406                 hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
2407             }
2408         }
2409         ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D_CreateDevice returned: %08x\n", hr);
2410
2411         if (!device_ptr)
2412         {
2413             DestroyWindow(hwnd);
2414             skip("Creating the device failed\n");
2415             goto err_out;
2416         }
2417
2418         /* Check for the default scissor rect size */
2419         hr = IDirect3DDevice9_GetScissorRect(device_ptr, &scissorrect);
2420         ok(hr == D3D_OK, "IDirect3DDevice9_GetScissorRect failed with: %08x\n", hr);
2421         ok(scissorrect.right == scts[i].backx && scissorrect.bottom == scts[i].backy && scissorrect.top == 0 && scissorrect.left == 0, "Scissorrect missmatch (%d, %d) should be (%d, %d)\n", scissorrect.right, scissorrect.bottom, scts[i].backx, scts[i].backy);
2422
2423         /* check the scissorrect values after a reset */
2424         present_parameters.BackBufferWidth = screen_width;
2425         present_parameters.BackBufferHeight = screen_height;
2426         hr = IDirect3DDevice9_Reset(device_ptr, &present_parameters);
2427         ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
2428         hr = IDirect3DDevice9_TestCooperativeLevel(device_ptr);
2429         ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
2430
2431         hr = IDirect3DDevice9_GetScissorRect(device_ptr, &scissorrect);
2432         ok(hr == D3D_OK, "IDirect3DDevice9_GetScissorRect failed with: %08x\n", hr);
2433         ok(scissorrect.right == screen_width && scissorrect.bottom == screen_height && scissorrect.top == 0 && scissorrect.left == 0, "Scissorrect missmatch (%d, %d) should be (%d, %d)\n", scissorrect.right, scissorrect.bottom, screen_width, screen_height);
2434
2435         if(device_ptr) {
2436             ULONG ref;
2437
2438             ref = IDirect3DDevice9_Release(device_ptr);
2439             DestroyWindow(hwnd);
2440             ok(ref == 0, "The device was not properly freed: refcount %u\n", ref);
2441         }
2442     }
2443
2444 err_out:
2445     if(d3d9_ptr) IDirect3D9_Release(d3d9_ptr);
2446     return;
2447 }
2448
2449 static void test_multi_device(void)
2450 {
2451     IDirect3DDevice9 *device1 = NULL, *device2 = NULL;
2452     D3DPRESENT_PARAMETERS present_parameters;
2453     HWND hwnd1 = NULL, hwnd2 = NULL;
2454     IDirect3D9 *d3d9;
2455     ULONG refcount;
2456     HRESULT hr;
2457
2458     d3d9 = pDirect3DCreate9(D3D_SDK_VERSION);
2459     ok(d3d9 != NULL, "Failed to create a d3d9 object.\n");
2460     if (!d3d9) goto fail;
2461
2462     hwnd1 = CreateWindow("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL);
2463     ok(hwnd1 != NULL, "Failed to create a window.\n");
2464     if (!hwnd1) goto fail;
2465
2466     memset(&present_parameters, 0, sizeof(present_parameters));
2467     present_parameters.Windowed = TRUE;
2468     present_parameters.hDeviceWindow = hwnd1;
2469     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2470
2471     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd1,
2472             D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device1);
2473     IDirect3D9_Release(d3d9);
2474     d3d9 = NULL;
2475     if (FAILED(hr)) {
2476         skip("Failed to create a device\n");
2477         goto fail;
2478     }
2479
2480     d3d9 = pDirect3DCreate9(D3D_SDK_VERSION);
2481     ok(d3d9 != NULL, "Failed to create a d3d9 object.\n");
2482     if (!d3d9) goto fail;
2483
2484     hwnd2 = CreateWindow("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL);
2485     ok(hwnd2 != NULL, "Failed to create a window.\n");
2486     if (!hwnd2) goto fail;
2487
2488     memset(&present_parameters, 0, sizeof(present_parameters));
2489     present_parameters.Windowed = TRUE;
2490     present_parameters.hDeviceWindow = hwnd2;
2491     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2492
2493     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd2,
2494             D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device2);
2495     ok(SUCCEEDED(hr), "Failed to create a device, hr %#x\n", hr);
2496     IDirect3D9_Release(d3d9);
2497     d3d9 = NULL;
2498     if (FAILED(hr)) goto fail;
2499
2500 fail:
2501     if (d3d9) IDirect3D9_Release(d3d9);
2502     if (device1)
2503     {
2504         refcount = IDirect3DDevice9_Release(device1);
2505         ok(!refcount, "Device has %u references left.\n", refcount);
2506     }
2507     if (device2)
2508     {
2509         refcount = IDirect3DDevice9_Release(device2);
2510         ok(!refcount, "Device has %u references left.\n", refcount);
2511     }
2512     if (hwnd1) DestroyWindow(hwnd1);
2513     if (hwnd2) DestroyWindow(hwnd2);
2514 }
2515
2516 static HWND filter_messages;
2517
2518 enum message_window
2519 {
2520     DEVICE_WINDOW,
2521     FOCUS_WINDOW,
2522 };
2523
2524 struct message
2525 {
2526     UINT message;
2527     enum message_window window;
2528 };
2529
2530 static const struct message *expect_messages;
2531 static HWND device_window, focus_window;
2532
2533 struct wndproc_thread_param
2534 {
2535     HWND dummy_window;
2536     HANDLE window_created;
2537     HANDLE test_finished;
2538     BOOL running_in_foreground;
2539 };
2540
2541 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2542 {
2543     if (filter_messages && filter_messages == hwnd)
2544     {
2545         if (message != WM_DISPLAYCHANGE && message != WM_IME_NOTIFY)
2546             todo_wine ok( 0, "Received unexpected message %#x for window %p.\n", message, hwnd);
2547     }
2548
2549     if (expect_messages)
2550     {
2551         HWND w;
2552
2553         switch (expect_messages->window)
2554         {
2555             case DEVICE_WINDOW:
2556                 w = device_window;
2557                 break;
2558
2559             case FOCUS_WINDOW:
2560                 w = focus_window;
2561                 break;
2562
2563             default:
2564                 w = NULL;
2565                 break;
2566         };
2567
2568         if (hwnd == w && expect_messages->message == message) ++expect_messages;
2569     }
2570
2571     return DefWindowProcA(hwnd, message, wparam, lparam);
2572 }
2573
2574 static DWORD WINAPI wndproc_thread(void *param)
2575 {
2576     struct wndproc_thread_param *p = param;
2577     DWORD res;
2578     BOOL ret;
2579
2580     p->dummy_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test",
2581             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2582     p->running_in_foreground = SetForegroundWindow(p->dummy_window);
2583
2584     ret = SetEvent(p->window_created);
2585     ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
2586
2587     for (;;)
2588     {
2589         MSG msg;
2590
2591         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
2592         res = WaitForSingleObject(p->test_finished, 100);
2593         if (res == WAIT_OBJECT_0) break;
2594         if (res != WAIT_TIMEOUT)
2595         {
2596             ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2597             break;
2598         }
2599     }
2600
2601     DestroyWindow(p->dummy_window);
2602
2603     return 0;
2604 }
2605
2606 static void test_wndproc(void)
2607 {
2608     struct wndproc_thread_param thread_params;
2609     IDirect3DDevice9 *device;
2610     WNDCLASSA wc = {0};
2611     IDirect3D9 *d3d9;
2612     HANDLE thread;
2613     LONG_PTR proc;
2614     ULONG ref;
2615     DWORD res, tid;
2616     HWND tmp;
2617
2618     static const struct message messages[] =
2619     {
2620         {WM_WINDOWPOSCHANGING,  FOCUS_WINDOW},
2621         {WM_ACTIVATE,           FOCUS_WINDOW},
2622         {WM_SETFOCUS,           FOCUS_WINDOW},
2623         {0,                     0},
2624     };
2625
2626     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
2627     {
2628         skip("Failed to create IDirect3D9 object, skipping tests.\n");
2629         return;
2630     }
2631
2632     wc.lpfnWndProc = test_proc;
2633     wc.lpszClassName = "d3d9_test_wndproc_wc";
2634     ok(RegisterClassA(&wc), "Failed to register window class.\n");
2635
2636     thread_params.window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
2637     ok(!!thread_params.window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
2638     thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
2639     ok(!!thread_params.test_finished, "CreateEvent failed, last error %#x.\n", GetLastError());
2640
2641     focus_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test",
2642             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2643     device_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test",
2644             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2645     thread = CreateThread(NULL, 0, wndproc_thread, &thread_params, 0, &tid);
2646     ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
2647
2648     res = WaitForSingleObject(thread_params.window_created, INFINITE);
2649     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2650
2651     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2652     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2653             (LONG_PTR)test_proc, proc);
2654     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2655     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2656             (LONG_PTR)test_proc, proc);
2657
2658     trace("device_window %p, focus_window %p, dummy_window %p.\n",
2659             device_window, focus_window, thread_params.dummy_window);
2660
2661     tmp = GetFocus();
2662     ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
2663     if (thread_params.running_in_foreground)
2664     {
2665         tmp = GetForegroundWindow();
2666         ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
2667                 thread_params.dummy_window, tmp);
2668     }
2669     else
2670         skip("Not running in foreground, skip foreground window test\n");
2671
2672     flush_events();
2673
2674     expect_messages = messages;
2675
2676     device = create_device(d3d9, device_window, focus_window, FALSE);
2677     if (!device)
2678     {
2679         skip("Failed to create a D3D device, skipping tests.\n");
2680         goto done;
2681     }
2682
2683     ok(!expect_messages->message, "Expected message %#x for window %#x, but didn't receive it.\n",
2684             expect_messages->message, expect_messages->window);
2685     expect_messages = NULL;
2686
2687     if (0) /* Disabled until we can make this work in a reliable way on Wine. */
2688     {
2689         tmp = GetFocus();
2690         ok(tmp == focus_window, "Expected focus %p, got %p.\n", focus_window, tmp);
2691         tmp = GetForegroundWindow();
2692         ok(tmp == focus_window, "Expected foreground window %p, got %p.\n", focus_window, tmp);
2693     }
2694     SetForegroundWindow(focus_window);
2695     flush_events();
2696
2697     filter_messages = focus_window;
2698
2699     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2700     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2701             (LONG_PTR)test_proc, proc);
2702
2703     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2704     ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2705             (LONG_PTR)test_proc, proc);
2706
2707     ref = IDirect3DDevice9_Release(device);
2708     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2709
2710     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2711     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2712             (LONG_PTR)test_proc, proc);
2713
2714     device = create_device(d3d9, focus_window, focus_window, FALSE);
2715     if (!device)
2716     {
2717         skip("Failed to create a D3D device, skipping tests.\n");
2718         goto done;
2719     }
2720
2721     ref = IDirect3DDevice9_Release(device);
2722     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2723
2724     device = create_device(d3d9, device_window, focus_window, FALSE);
2725     if (!device)
2726     {
2727         skip("Failed to create a D3D device, skipping tests.\n");
2728         goto done;
2729     }
2730
2731     proc = SetWindowLongPtrA(focus_window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2732     ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2733             (LONG_PTR)test_proc, proc);
2734
2735     ref = IDirect3DDevice9_Release(device);
2736     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2737
2738     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2739     ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2740             (LONG_PTR)DefWindowProcA, proc);
2741
2742 done:
2743     filter_messages = NULL;
2744     IDirect3D9_Release(d3d9);
2745
2746     SetEvent(thread_params.test_finished);
2747     WaitForSingleObject(thread, INFINITE);
2748     CloseHandle(thread_params.test_finished);
2749     CloseHandle(thread_params.window_created);
2750     CloseHandle(thread);
2751
2752     DestroyWindow(device_window);
2753     DestroyWindow(focus_window);
2754     UnregisterClassA("d3d9_test_wndproc_wc", GetModuleHandleA(NULL));
2755 }
2756
2757 static void test_wndproc_windowed(void)
2758 {
2759     struct wndproc_thread_param thread_params;
2760     IDirect3DDevice9 *device;
2761     WNDCLASSA wc = {0};
2762     IDirect3D9 *d3d9;
2763     HANDLE thread;
2764     LONG_PTR proc;
2765     HRESULT hr;
2766     ULONG ref;
2767     DWORD res, tid;
2768     HWND tmp;
2769
2770     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
2771     {
2772         skip("Failed to create IDirect3D9 object, skipping tests.\n");
2773         return;
2774     }
2775
2776     wc.lpfnWndProc = test_proc;
2777     wc.lpszClassName = "d3d9_test_wndproc_wc";
2778     ok(RegisterClassA(&wc), "Failed to register window class.\n");
2779
2780     thread_params.window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
2781     ok(!!thread_params.window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
2782     thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
2783     ok(!!thread_params.test_finished, "CreateEvent failed, last error %#x.\n", GetLastError());
2784
2785     focus_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test",
2786             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2787     device_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test",
2788             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2789     thread = CreateThread(NULL, 0, wndproc_thread, &thread_params, 0, &tid);
2790     ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
2791
2792     res = WaitForSingleObject(thread_params.window_created, INFINITE);
2793     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2794
2795     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2796     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2797             (LONG_PTR)test_proc, proc);
2798     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2799     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2800             (LONG_PTR)test_proc, proc);
2801
2802     trace("device_window %p, focus_window %p, dummy_window %p.\n",
2803             device_window, focus_window, thread_params.dummy_window);
2804
2805     tmp = GetFocus();
2806     ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
2807     if (thread_params.running_in_foreground)
2808     {
2809         tmp = GetForegroundWindow();
2810         ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
2811                 thread_params.dummy_window, tmp);
2812     }
2813     else
2814         skip("Not running in foreground, skip foreground window test\n");
2815
2816     filter_messages = focus_window;
2817
2818     device = create_device(d3d9, device_window, focus_window, TRUE);
2819     if (!device)
2820     {
2821         skip("Failed to create a D3D device, skipping tests.\n");
2822         goto done;
2823     }
2824
2825     tmp = GetFocus();
2826     ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
2827     tmp = GetForegroundWindow();
2828     ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
2829             thread_params.dummy_window, tmp);
2830
2831     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2832     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2833             (LONG_PTR)test_proc, proc);
2834
2835     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2836     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2837             (LONG_PTR)test_proc, proc);
2838
2839     filter_messages = NULL;
2840
2841     hr = reset_device(device, device_window, FALSE);
2842     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2843
2844     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2845     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2846             (LONG_PTR)test_proc, proc);
2847
2848     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2849     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2850             (LONG_PTR)test_proc, proc);
2851
2852     hr = reset_device(device, device_window, TRUE);
2853     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2854
2855     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2856     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2857             (LONG_PTR)test_proc, proc);
2858
2859     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2860     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2861             (LONG_PTR)test_proc, proc);
2862
2863     filter_messages = focus_window;
2864
2865     ref = IDirect3DDevice9_Release(device);
2866     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2867
2868     filter_messages = device_window;
2869
2870     device = create_device(d3d9, focus_window, focus_window, TRUE);
2871     if (!device)
2872     {
2873         skip("Failed to create a D3D device, skipping tests.\n");
2874         goto done;
2875     }
2876
2877     filter_messages = NULL;
2878
2879     hr = reset_device(device, focus_window, FALSE);
2880     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2881
2882     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2883     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2884             (LONG_PTR)test_proc, proc);
2885
2886     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2887     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2888             (LONG_PTR)test_proc, proc);
2889
2890     hr = reset_device(device, focus_window, TRUE);
2891     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2892
2893     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2894     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2895             (LONG_PTR)test_proc, proc);
2896
2897     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2898     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2899             (LONG_PTR)test_proc, proc);
2900
2901     filter_messages = device_window;
2902
2903     ref = IDirect3DDevice9_Release(device);
2904     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2905
2906     device = create_device(d3d9, device_window, focus_window, TRUE);
2907     if (!device)
2908     {
2909         skip("Failed to create a D3D device, skipping tests.\n");
2910         goto done;
2911     }
2912
2913     filter_messages = NULL;
2914
2915     hr = reset_device(device, device_window, FALSE);
2916     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2917
2918     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2919     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2920             (LONG_PTR)test_proc, proc);
2921
2922     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2923     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2924             (LONG_PTR)test_proc, proc);
2925
2926     hr = reset_device(device, device_window, TRUE);
2927     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2928
2929     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2930     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2931             (LONG_PTR)test_proc, proc);
2932
2933     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2934     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2935             (LONG_PTR)test_proc, proc);
2936
2937     filter_messages = device_window;
2938
2939     ref = IDirect3DDevice9_Release(device);
2940     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2941
2942 done:
2943     filter_messages = NULL;
2944     IDirect3D9_Release(d3d9);
2945
2946     SetEvent(thread_params.test_finished);
2947     WaitForSingleObject(thread, INFINITE);
2948     CloseHandle(thread_params.test_finished);
2949     CloseHandle(thread_params.window_created);
2950     CloseHandle(thread);
2951
2952     DestroyWindow(device_window);
2953     DestroyWindow(focus_window);
2954     UnregisterClassA("d3d9_test_wndproc_wc", GetModuleHandleA(NULL));
2955 }
2956
2957 static void test_reset_fullscreen(void)
2958 {
2959     WNDCLASSEX wc = {0};
2960     IDirect3DDevice9 *device = NULL;
2961     IDirect3D9 *d3d = NULL;
2962     ATOM atom;
2963     static const struct message messages[] =
2964     {
2965         {WM_ACTIVATEAPP,    FOCUS_WINDOW},
2966         {0,                     0},
2967     };
2968
2969     d3d = pDirect3DCreate9(D3D_SDK_VERSION);
2970     ok(d3d != NULL, "Failed to create an IDirect3D object.\n");
2971     expect_messages = messages;
2972
2973     wc.cbSize = sizeof(WNDCLASSEX);
2974     wc.lpfnWndProc = test_proc;
2975     wc.lpszClassName = "test_reset_fullscreen";
2976
2977     atom = RegisterClassEx(&wc);
2978     ok(atom, "Failed to register a new window class. GetLastError:%d\n", GetLastError());
2979
2980     device_window = focus_window = CreateWindowEx(0, wc.lpszClassName, "Test Reset Fullscreen", 0, 0, 0, screen_width, screen_height, NULL, NULL, NULL, NULL);
2981     ok(device_window != NULL, "Failed to create a window. GetLastError:%d\n", GetLastError());
2982
2983     /*
2984      * Create a device in windowed mode.
2985      * Since the device is windowed and we haven't called any methods that
2986      * could show the window (such as ShowWindow or SetWindowPos) yet,
2987      * WM_ACTIVATEAPP will not have been sent.
2988      */
2989     device = create_device(d3d, device_window, focus_window, TRUE);
2990     if (!device)
2991     {
2992         skip("Unable to create device.  Skipping test.\n");
2993         goto cleanup;
2994     }
2995
2996     /*
2997      * Switch to fullscreen mode.
2998      * This will force the window to be shown and will cause the WM_ACTIVATEAPP
2999      * message to be sent.
3000      */
3001     ok(SUCCEEDED(reset_device(device, device_window, FALSE)), "Failed to reset device.\n");
3002
3003     flush_events();
3004     ok(expect_messages->message == 0, "Expected to receive message %#x.\n", expect_messages->message);
3005     expect_messages = NULL;
3006
3007 cleanup:
3008     if (device) IDirect3DDevice9_Release(device);
3009     if (d3d) IDirect3D9_Release(d3d);
3010     DestroyWindow(device_window);
3011     device_window = focus_window = NULL;
3012     UnregisterClass(wc.lpszClassName, GetModuleHandle(NULL));
3013 }
3014
3015
3016 static inline void set_fpu_cw(WORD cw)
3017 {
3018 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3019 #define D3D9_TEST_SET_FPU_CW 1
3020     __asm__ volatile ("fnclex");
3021     __asm__ volatile ("fldcw %0" : : "m" (cw));
3022 #elif defined(__i386__) && defined(_MSC_VER)
3023 #define D3D9_TEST_SET_FPU_CW 1
3024     __asm fnclex;
3025     __asm fldcw cw;
3026 #endif
3027 }
3028
3029 static inline WORD get_fpu_cw(void)
3030 {
3031     WORD cw = 0;
3032 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3033 #define D3D9_TEST_GET_FPU_CW 1
3034     __asm__ volatile ("fnstcw %0" : "=m" (cw));
3035 #elif defined(__i386__) && defined(_MSC_VER)
3036 #define D3D9_TEST_GET_FPU_CW 1
3037     __asm fnstcw cw;
3038 #endif
3039     return cw;
3040 }
3041
3042 static void test_fpu_setup(void)
3043 {
3044 #if defined(D3D9_TEST_SET_FPU_CW) && defined(D3D9_TEST_GET_FPU_CW)
3045     D3DPRESENT_PARAMETERS present_parameters;
3046     IDirect3DDevice9 *device;
3047     HWND window = NULL;
3048     IDirect3D9 *d3d9;
3049     HRESULT hr;
3050     WORD cw;
3051
3052     d3d9 = pDirect3DCreate9(D3D_SDK_VERSION);
3053     ok(!!d3d9, "Failed to create a d3d9 object.\n");
3054     if (!d3d9) return;
3055
3056     window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
3057     ok(!!window, "Failed to create a window.\n");
3058     if (!window) goto done;
3059
3060     memset(&present_parameters, 0, sizeof(present_parameters));
3061     present_parameters.Windowed = TRUE;
3062     present_parameters.hDeviceWindow = window;
3063     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
3064
3065     set_fpu_cw(0xf60);
3066     cw = get_fpu_cw();
3067     ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
3068
3069     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
3070             D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device);
3071     if (FAILED(hr))
3072     {
3073         skip("Failed to create a device, hr %#x.\n", hr);
3074         set_fpu_cw(0x37f);
3075         goto done;
3076     }
3077
3078     cw = get_fpu_cw();
3079     ok(cw == 0x7f, "cw is %#x, expected 0x7f.\n", cw);
3080
3081     IDirect3DDevice9_Release(device);
3082
3083     cw = get_fpu_cw();
3084     ok(cw == 0x7f, "cw is %#x, expected 0x7f.\n", cw);
3085     set_fpu_cw(0xf60);
3086     cw = get_fpu_cw();
3087     ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
3088
3089     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
3090             D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &present_parameters, &device);
3091     ok(SUCCEEDED(hr), "CreateDevice failed, hr %#x.\n", hr);
3092
3093     cw = get_fpu_cw();
3094     ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
3095     set_fpu_cw(0x37f);
3096
3097     IDirect3DDevice9_Release(device);
3098
3099 done:
3100     if (window) DestroyWindow(window);
3101     if (d3d9) IDirect3D9_Release(d3d9);
3102 #endif
3103 }
3104
3105 static void test_window_style(void)
3106 {
3107     RECT focus_rect, fullscreen_rect, r;
3108     LONG device_style, device_exstyle;
3109     LONG focus_style, focus_exstyle;
3110     LONG style, expected_style;
3111     IDirect3DDevice9 *device;
3112     IDirect3D9 *d3d9;
3113     ULONG ref;
3114
3115
3116     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
3117     {
3118         skip("Failed to create IDirect3D9 object, skipping tests.\n");
3119         return;
3120     }
3121
3122     focus_window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3123             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3124     device_window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3125             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3126
3127     device_style = GetWindowLongA(device_window, GWL_STYLE);
3128     device_exstyle = GetWindowLongA(device_window, GWL_EXSTYLE);
3129     focus_style = GetWindowLongA(focus_window, GWL_STYLE);
3130     focus_exstyle = GetWindowLongA(focus_window, GWL_EXSTYLE);
3131
3132     SetRect(&fullscreen_rect, 0, 0, screen_width, screen_height);
3133     GetWindowRect(focus_window, &focus_rect);
3134
3135     device = create_device(d3d9, device_window, focus_window, FALSE);
3136     if (!device)
3137     {
3138         skip("Failed to create a D3D device, skipping tests.\n");
3139         goto done;
3140     }
3141
3142     style = GetWindowLongA(device_window, GWL_STYLE);
3143     expected_style = device_style | WS_VISIBLE;
3144     todo_wine ok(style == expected_style, "Expected device window style %#x, got %#x.\n",
3145             expected_style, style);
3146     style = GetWindowLongA(device_window, GWL_EXSTYLE);
3147     expected_style = device_exstyle | WS_EX_TOPMOST;
3148     todo_wine ok(style == expected_style, "Expected device window extended style %#x, got %#x.\n",
3149             expected_style, style);
3150
3151     style = GetWindowLongA(focus_window, GWL_STYLE);
3152     ok(style == focus_style, "Expected focus window style %#x, got %#x.\n",
3153             focus_style, style);
3154     style = GetWindowLongA(focus_window, GWL_EXSTYLE);
3155     ok(style == focus_exstyle, "Expected focus window extended style %#x, got %#x.\n",
3156             focus_exstyle, style);
3157
3158     GetWindowRect(device_window, &r);
3159     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3160             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
3161             r.left, r.top, r.right, r.bottom);
3162     GetClientRect(device_window, &r);
3163     todo_wine ok(!EqualRect(&r, &fullscreen_rect), "Client rect and window rect are equal.\n");
3164     GetWindowRect(focus_window, &r);
3165     ok(EqualRect(&r, &focus_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3166             focus_rect.left, focus_rect.top, focus_rect.right, focus_rect.bottom,
3167             r.left, r.top, r.right, r.bottom);
3168
3169     ref = IDirect3DDevice9_Release(device);
3170     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
3171
3172 done:
3173     IDirect3D9_Release(d3d9);
3174
3175     DestroyWindow(device_window);
3176     DestroyWindow(focus_window);
3177 }
3178
3179 static const POINT *expect_pos;
3180
3181 static LRESULT CALLBACK test_cursor_proc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
3182 {
3183     if (message == WM_MOUSEMOVE)
3184     {
3185         if (expect_pos && expect_pos->x && expect_pos->y)
3186         {
3187             POINT p = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)};
3188
3189             ClientToScreen(window, &p);
3190             if (expect_pos->x == p.x && expect_pos->y == p.y)
3191                 ++expect_pos;
3192         }
3193     }
3194
3195     return DefWindowProcA(window, message, wparam, lparam);
3196 }
3197
3198 static void test_cursor_pos(void)
3199 {
3200     IDirect3DSurface9 *cursor;
3201     IDirect3DDevice9 *device;
3202     WNDCLASSA wc = {0};
3203     IDirect3D9 *d3d9;
3204     UINT refcount;
3205     HWND window;
3206     HRESULT hr;
3207     BOOL ret;
3208
3209     /* Note that we don't check for movement we're not supposed to receive.
3210      * That's because it's hard to distinguish from the user accidentally
3211      * moving the mouse. */
3212     static const POINT points[] =
3213     {
3214         {50, 50},
3215         {75, 75},
3216         {100, 100},
3217         {125, 125},
3218         {150, 150},
3219         {125, 125},
3220         {150, 150},
3221         {150, 150},
3222         {0, 0},
3223     };
3224
3225     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
3226     {
3227         skip("Failed to create IDirect3D9 object, skipping cursor tests.\n");
3228         return;
3229     }
3230
3231     wc.lpfnWndProc = test_cursor_proc;
3232     wc.lpszClassName = "d3d9_test_cursor_wc";
3233     ok(RegisterClassA(&wc), "Failed to register window class.\n");
3234     window = CreateWindow("d3d9_test_cursor_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3235             0, 0, 320, 240, NULL, NULL, NULL, NULL);
3236     ShowWindow(window, SW_SHOW);
3237
3238     device = create_device(d3d9, window, window, TRUE);
3239     if (!device)
3240     {
3241         skip("Failed to create a D3D device, skipping tests.\n");
3242         goto done;
3243     }
3244
3245     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 32, 32,
3246             D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &cursor, NULL);
3247     ok(SUCCEEDED(hr), "Failed to create cursor surface, hr %#x.\n", hr);
3248     hr = IDirect3DDevice9_SetCursorProperties(device, 0, 0, cursor);
3249     ok(SUCCEEDED(hr), "Failed to set cursor properties, hr %#x.\n", hr);
3250     IDirect3DSurface9_Release(cursor);
3251     ret = IDirect3DDevice9_ShowCursor(device, TRUE);
3252     ok(!ret, "Failed to show cursor, hr %#x.\n", ret);
3253
3254     flush_events();
3255     expect_pos = points;
3256
3257     ret = SetCursorPos(50, 50);
3258     ok(ret, "Failed to set cursor position.\n");
3259     flush_events();
3260
3261     IDirect3DDevice9_SetCursorPosition(device, 75, 75, 0);
3262     flush_events();
3263     /* SetCursorPosition() eats duplicates. */
3264     IDirect3DDevice9_SetCursorPosition(device, 75, 75, 0);
3265     flush_events();
3266
3267     ret = SetCursorPos(100, 100);
3268     ok(ret, "Failed to set cursor position.\n");
3269     flush_events();
3270     /* Even if the position was set with SetCursorPos(). */
3271     IDirect3DDevice9_SetCursorPosition(device, 100, 100, 0);
3272     flush_events();
3273
3274     IDirect3DDevice9_SetCursorPosition(device, 125, 125, 0);
3275     flush_events();
3276     ret = SetCursorPos(150, 150);
3277     ok(ret, "Failed to set cursor position.\n");
3278     flush_events();
3279     IDirect3DDevice9_SetCursorPosition(device, 125, 125, 0);
3280     flush_events();
3281
3282     IDirect3DDevice9_SetCursorPosition(device, 150, 150, 0);
3283     flush_events();
3284     /* SetCursorPos() doesn't. */
3285     ret = SetCursorPos(150, 150);
3286     ok(ret, "Failed to set cursor position.\n");
3287     flush_events();
3288
3289     ok(!expect_pos->x && !expect_pos->y, "Didn't receive MOUSEMOVE %u (%d, %d).\n",
3290        (unsigned)(expect_pos - points), expect_pos->x, expect_pos->y);
3291
3292     refcount = IDirect3DDevice9_Release(device);
3293     ok(!refcount, "Device has %u references left.\n", refcount);
3294 done:
3295     DestroyWindow(window);
3296     UnregisterClassA("d3d9_test_cursor_wc", GetModuleHandleA(NULL));
3297     if (d3d9)
3298         IDirect3D9_Release(d3d9);
3299 }
3300
3301 static void test_mode_change(void)
3302 {
3303     RECT fullscreen_rect, focus_rect, r;
3304     IDirect3DSurface9 *backbuffer;
3305     IDirect3DDevice9 *device;
3306     D3DSURFACE_DESC desc;
3307     IDirect3D9 *d3d9;
3308     DEVMODEW devmode;
3309     UINT refcount;
3310     HRESULT hr;
3311     DWORD ret;
3312
3313     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
3314     {
3315         skip("Failed to create IDirect3D9 object, skipping mode change tests.\n");
3316         return;
3317     }
3318
3319     focus_window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3320             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3321     device_window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3322             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3323
3324     SetRect(&fullscreen_rect, 0, 0, screen_width, screen_height);
3325     GetWindowRect(focus_window, &focus_rect);
3326
3327     device = create_device(d3d9, device_window, focus_window, FALSE);
3328     if (!device)
3329     {
3330         skip("Failed to create a D3D device, skipping tests.\n");
3331         goto done;
3332     }
3333
3334     memset(&devmode, 0, sizeof(devmode));
3335     devmode.dmSize = sizeof(devmode);
3336     devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
3337     devmode.dmPelsWidth = 640;
3338     devmode.dmPelsHeight = 480;
3339
3340     ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
3341     ok(ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", ret);
3342
3343     memset(&devmode, 0, sizeof(devmode));
3344     devmode.dmSize = sizeof(devmode);
3345     ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3346     ok(ret, "Failed to get display mode.\n");
3347     ok(devmode.dmPelsWidth == 640, "Got unexpect width %u.\n", devmode.dmPelsWidth);
3348     ok(devmode.dmPelsHeight == 480, "Got unexpect height %u.\n", devmode.dmPelsHeight);
3349
3350     GetWindowRect(device_window, &r);
3351     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3352             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
3353             r.left, r.top, r.right, r.bottom);
3354     GetWindowRect(focus_window, &r);
3355     ok(EqualRect(&r, &focus_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3356             focus_rect.left, focus_rect.top, focus_rect.right, focus_rect.bottom,
3357             r.left, r.top, r.right, r.bottom);
3358
3359     hr = IDirect3DDevice9_GetBackBuffer(device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
3360     ok(SUCCEEDED(hr), "Failed to get backbuffer, hr %#x.\n", hr);
3361     hr = IDirect3DSurface9_GetDesc(backbuffer, &desc);
3362     ok(SUCCEEDED(hr), "Failed to get backbuffer desc, hr %#x.\n", hr);
3363     ok(desc.Width == screen_width, "Got unexpected backbuffer width %u.\n", desc.Width);
3364     ok(desc.Height == screen_height, "Got unexpected backbuffer height %u.\n", desc.Height);
3365     IDirect3DSurface9_Release(backbuffer);
3366
3367     refcount = IDirect3DDevice9_Release(device);
3368     ok(!refcount, "Device has %u references left.\n", refcount);
3369
3370     memset(&devmode, 0, sizeof(devmode));
3371     devmode.dmSize = sizeof(devmode);
3372     ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3373     ok(ret, "Failed to get display mode.\n");
3374     ok(devmode.dmPelsWidth == screen_width, "Got unexpect width %u.\n", devmode.dmPelsWidth);
3375     ok(devmode.dmPelsHeight == screen_height, "Got unexpect height %u.\n", devmode.dmPelsHeight);
3376
3377 done:
3378     DestroyWindow(device_window);
3379     DestroyWindow(focus_window);
3380     if (d3d9)
3381         IDirect3D9_Release(d3d9);
3382
3383     memset(&devmode, 0, sizeof(devmode));
3384     devmode.dmSize = sizeof(devmode);
3385     ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3386     ok(ret, "Failed to get display mode.\n");
3387     ok(devmode.dmPelsWidth == screen_width, "Got unexpect width %u.\n", devmode.dmPelsWidth);
3388     ok(devmode.dmPelsHeight == screen_height, "Got unexpect height %u.\n", devmode.dmPelsHeight);
3389 }
3390
3391 static void test_device_window_reset(void)
3392 {
3393     RECT fullscreen_rect, device_rect, r;
3394     IDirect3DDevice9 *device;
3395     WNDCLASSA wc = {0};
3396     IDirect3D9 *d3d9;
3397     LONG_PTR proc;
3398     HRESULT hr;
3399     ULONG ref;
3400
3401     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
3402     {
3403         skip("Failed to create IDirect3D9 object, skipping tests.\n");
3404         return;
3405     }
3406
3407     wc.lpfnWndProc = test_proc;
3408     wc.lpszClassName = "d3d9_test_wndproc_wc";
3409     ok(RegisterClassA(&wc), "Failed to register window class.\n");
3410
3411     focus_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3412             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3413     device_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3414             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3415
3416     SetRect(&fullscreen_rect, 0, 0, screen_width, screen_height);
3417     GetWindowRect(device_window, &device_rect);
3418
3419     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
3420     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3421             (LONG_PTR)test_proc, proc);
3422     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
3423     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3424             (LONG_PTR)test_proc, proc);
3425
3426     device = create_device(d3d9, NULL, focus_window, FALSE);
3427     if (!device)
3428     {
3429         skip("Failed to create a D3D device, skipping tests.\n");
3430         goto done;
3431     }
3432
3433     GetWindowRect(focus_window, &r);
3434     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3435             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
3436             r.left, r.top, r.right, r.bottom);
3437     GetWindowRect(device_window, &r);
3438     ok(EqualRect(&r, &device_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3439             device_rect.left, device_rect.top, device_rect.right, device_rect.bottom,
3440             r.left, r.top, r.right, r.bottom);
3441
3442     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
3443     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3444             (LONG_PTR)test_proc, proc);
3445     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
3446     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3447             (LONG_PTR)test_proc, proc);
3448
3449     hr = reset_device(device, device_window, FALSE);
3450     ok(SUCCEEDED(hr), "Failed to reset device.\n");
3451
3452     GetWindowRect(focus_window, &r);
3453     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3454             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
3455             r.left, r.top, r.right, r.bottom);
3456     GetWindowRect(device_window, &r);
3457     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3458             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
3459             r.left, r.top, r.right, r.bottom);
3460
3461     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
3462     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3463             (LONG_PTR)test_proc, proc);
3464     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
3465     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3466             (LONG_PTR)test_proc, proc);
3467
3468     ref = IDirect3DDevice9_Release(device);
3469     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
3470
3471 done:
3472     IDirect3D9_Release(d3d9);
3473     DestroyWindow(device_window);
3474     DestroyWindow(focus_window);
3475     UnregisterClassA("d3d9_test_wndproc_wc", GetModuleHandleA(NULL));
3476 }
3477
3478 static void test_reset_resources(void)
3479 {
3480     IDirect3DSurface9 *surface, *rt;
3481     IDirect3DTexture9 *texture;
3482     IDirect3DDevice9 *device;
3483     IDirect3D9 *d3d9;
3484     unsigned int i;
3485     D3DCAPS9 caps;
3486     HWND window;
3487     HRESULT hr;
3488     ULONG ref;
3489
3490     window = CreateWindowA("static", "d3d9_test", WS_OVERLAPPEDWINDOW,
3491             0, 0, 640, 480, 0, 0, 0, 0);
3492
3493     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
3494     {
3495         skip("Failed to create IDirect3D9 object, skipping tests.\n");
3496         DestroyWindow(window);
3497         return;
3498     }
3499
3500     if (!(device = create_device(d3d9, window, window, TRUE)))
3501     {
3502         skip("Failed to create a D3D device, skipping tests.\n");
3503         goto done;
3504     }
3505
3506     hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
3507     ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
3508
3509     hr = IDirect3DDevice9_CreateTexture(device, 128, 128, 1, D3DUSAGE_DEPTHSTENCIL,
3510             D3DFMT_D24S8, D3DPOOL_DEFAULT, &texture, NULL);
3511     ok(SUCCEEDED(hr), "Failed to create depth/stencil texture, hr %#x.\n", hr);
3512     hr = IDirect3DTexture9_GetSurfaceLevel(texture, 0, &surface);
3513     ok(SUCCEEDED(hr), "Failed to get surface, hr %#x.\n", hr);
3514     IDirect3DTexture9_Release(texture);
3515     hr = IDirect3DDevice9_SetDepthStencilSurface(device, surface);
3516     ok(SUCCEEDED(hr), "Failed to set depth/stencil surface, hr %#x.\n", hr);
3517     IDirect3DSurface9_Release(surface);
3518
3519     for (i = 0; i < caps.NumSimultaneousRTs; ++i)
3520     {
3521         hr = IDirect3DDevice9_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET,
3522                 D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture, NULL);
3523         ok(SUCCEEDED(hr), "Failed to create render target texture %u, hr %#x.\n", i, hr);
3524         hr = IDirect3DTexture9_GetSurfaceLevel(texture, 0, &surface);
3525         ok(SUCCEEDED(hr), "Failed to get surface %u, hr %#x.\n", i, hr);
3526         IDirect3DTexture9_Release(texture);
3527         hr = IDirect3DDevice9_SetRenderTarget(device, i, surface);
3528         ok(SUCCEEDED(hr), "Failed to set render target surface %u, hr %#x.\n", i, hr);
3529         IDirect3DSurface9_Release(surface);
3530     }
3531
3532     hr = reset_device(device, device_window, TRUE);
3533     ok(SUCCEEDED(hr), "Failed to reset device.\n");
3534
3535     hr = IDirect3DDevice9_GetBackBuffer(device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &rt);
3536     ok(SUCCEEDED(hr), "Failed to get back buffer, hr %#x.\n", hr);
3537     hr = IDirect3DDevice9_GetRenderTarget(device, 0, &surface);
3538     ok(SUCCEEDED(hr), "Failed to get render target surface, hr %#x.\n", hr);
3539     ok(surface == rt, "Got unexpected surface %p for render target.\n", surface);
3540     IDirect3DSurface9_Release(surface);
3541     IDirect3DSurface9_Release(rt);
3542
3543     for (i = 1; i < caps.NumSimultaneousRTs; ++i)
3544     {
3545         hr = IDirect3DDevice9_GetRenderTarget(device, i, &surface);
3546         ok(hr == D3DERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
3547     }
3548
3549     ref = IDirect3DDevice9_Release(device);
3550     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
3551
3552 done:
3553     IDirect3D9_Release(d3d9);
3554     DestroyWindow(window);
3555 }
3556
3557 START_TEST(device)
3558 {
3559     HMODULE d3d9_handle = LoadLibraryA( "d3d9.dll" );
3560     WNDCLASS wc = {0};
3561
3562     wc.lpfnWndProc = DefWindowProc;
3563     wc.lpszClassName = "d3d9_test_wc";
3564     RegisterClass(&wc);
3565
3566     if (!d3d9_handle)
3567     {
3568         skip("Could not load d3d9.dll\n");
3569         goto out;
3570     }
3571
3572     pDirect3DCreate9 = (void *)GetProcAddress( d3d9_handle, "Direct3DCreate9" );
3573     ok(pDirect3DCreate9 != NULL, "Failed to get address of Direct3DCreate9\n");
3574     if (pDirect3DCreate9)
3575     {
3576         IDirect3D9 *d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
3577         if(!d3d9)
3578         {
3579             skip("could not create D3D9 object\n");
3580             goto out;
3581         }
3582         IDirect3D9_Release(d3d9);
3583
3584         screen_width = GetSystemMetrics(SM_CXSCREEN);
3585         screen_height = GetSystemMetrics(SM_CYSCREEN);
3586
3587         test_fpu_setup();
3588         test_multi_device();
3589         test_display_formats();
3590         test_display_modes();
3591         test_swapchain();
3592         test_refcount();
3593         test_mipmap_levels();
3594         test_checkdevicemultisampletype();
3595         test_cursor();
3596         test_cursor_pos();
3597         test_reset_fullscreen();
3598         test_reset();
3599         test_scene();
3600         test_limits();
3601         test_depthstenciltest();
3602         test_get_rt();
3603         test_draw_indexed();
3604         test_null_stream();
3605         test_lights();
3606         test_set_stream_source();
3607         test_scissor_size();
3608         test_wndproc();
3609         test_wndproc_windowed();
3610         test_window_style();
3611         test_mode_change();
3612         test_device_window_reset();
3613         test_reset_resources();
3614     }
3615
3616 out:
3617     UnregisterClassA("d3d9_test_wc", GetModuleHandleA(NULL));
3618 }