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