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