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