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