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