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