d3d9/tests: Also test that render states get reset in test_reset().
[wine] / dlls / d3d9 / tests / device.c
1 /*
2  * Copyright (C) 2006 Vitaliy Margolen
3  * Copyright (C) 2006 Chris Robinson
4  * Copyright (C) 2006-2007 Stefan Dösinger(For CodeWeavers)
5  * Copyright 2007 Henri Verbeet
6  * Copyright (C) 2008 Rico Schüller
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #define COBJMACROS
24 #include <windowsx.h>
25 #include <d3d9.h>
26 #include "wine/test.h"
27
28 static INT screen_width;
29 static INT screen_height;
30
31 static IDirect3D9 *(WINAPI *pDirect3DCreate9)(UINT);
32
33 static int get_refcount(IUnknown *object)
34 {
35     IUnknown_AddRef( object );
36     return IUnknown_Release( object );
37 }
38
39 /* try to make sure pending X events have been processed before continuing */
40 static void flush_events(void)
41 {
42     MSG msg;
43     int diff = 200;
44     int min_timeout = 100;
45     DWORD time = GetTickCount() + diff;
46
47     while (diff > 0)
48     {
49         if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
50         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
51         diff = time - GetTickCount();
52     }
53 }
54
55 static IDirect3DDevice9 *create_device(IDirect3D9 *d3d9, HWND device_window, HWND focus_window, BOOL windowed)
56 {
57     D3DPRESENT_PARAMETERS present_parameters = {0};
58     IDirect3DDevice9 *device;
59
60     present_parameters.Windowed = windowed;
61     present_parameters.hDeviceWindow = device_window;
62     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
63     present_parameters.BackBufferWidth = screen_width;
64     present_parameters.BackBufferHeight = screen_height;
65     present_parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
66     present_parameters.EnableAutoDepthStencil = TRUE;
67     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
68
69     if (SUCCEEDED(IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, focus_window,
70             D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device))) return device;
71
72     present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
73     if (SUCCEEDED(IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, focus_window,
74             D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device))) return device;
75
76     if (SUCCEEDED(IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, focus_window,
77             D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device))) return device;
78
79     return NULL;
80 }
81
82 static HRESULT reset_device(IDirect3DDevice9 *device, HWND device_window, BOOL windowed)
83 {
84     D3DPRESENT_PARAMETERS present_parameters = {0};
85
86     present_parameters.Windowed = windowed;
87     present_parameters.hDeviceWindow = device_window;
88     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
89     present_parameters.BackBufferWidth = screen_width;
90     present_parameters.BackBufferHeight = screen_height;
91     present_parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
92     present_parameters.EnableAutoDepthStencil = TRUE;
93     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
94
95     return IDirect3DDevice9_Reset(device, &present_parameters);
96 }
97
98 #define CHECK_CALL(r,c,d,rc) \
99     if (SUCCEEDED(r)) {\
100         int tmp1 = get_refcount( (IUnknown *)d ); \
101         int rc_new = rc; \
102         ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
103     } else {\
104         trace("%s failed: %08x\n", c, r); \
105     }
106
107 #define CHECK_RELEASE(obj,d,rc) \
108     if (obj) { \
109         int tmp1, rc_new = rc; \
110         IUnknown_Release( (IUnknown*)obj ); \
111         tmp1 = get_refcount( (IUnknown *)d ); \
112         ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
113     }
114
115 #define CHECK_REFCOUNT(obj,rc) \
116     { \
117         int rc_new = rc; \
118         int count = get_refcount( (IUnknown *)obj ); \
119         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
120     }
121
122 #define CHECK_RELEASE_REFCOUNT(obj,rc) \
123     { \
124         int rc_new = rc; \
125         int count = IUnknown_Release( (IUnknown *)obj ); \
126         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
127     }
128
129 #define CHECK_ADDREF_REFCOUNT(obj,rc) \
130     { \
131         int rc_new = rc; \
132         int count = IUnknown_AddRef( (IUnknown *)obj ); \
133         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
134     }
135
136 #define CHECK_SURFACE_CONTAINER(obj,iid,expected) \
137     { \
138         void *container_ptr = (void *)0x1337c0d3; \
139         hr = IDirect3DSurface9_GetContainer(obj, &iid, &container_ptr); \
140         ok(SUCCEEDED(hr) && container_ptr == expected, "GetContainer returned: hr %#x, container_ptr %p. " \
141             "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, expected); \
142         if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr); \
143     }
144
145 static void check_mipmap_levels(IDirect3DDevice9 *device, UINT width, UINT height, UINT count)
146 {
147     IDirect3DBaseTexture9* texture = NULL;
148     HRESULT hr = IDirect3DDevice9_CreateTexture( device, width, height, 0, 0,
149             D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, (IDirect3DTexture9**) &texture, NULL );
150
151     if (SUCCEEDED(hr)) {
152         DWORD levels = IDirect3DBaseTexture9_GetLevelCount(texture);
153         ok(levels == count, "Invalid level count. Expected %d got %u\n", count, levels);
154     } else
155         trace("CreateTexture failed: %08x\n", hr);
156
157     if (texture) IDirect3DBaseTexture9_Release( texture );
158 }
159
160 static void test_mipmap_levels(void)
161 {
162
163     HRESULT               hr;
164     HWND                  hwnd = NULL;
165
166     IDirect3D9            *pD3d = NULL;
167     IDirect3DDevice9      *pDevice = NULL;
168     D3DPRESENT_PARAMETERS d3dpp;
169     D3DDISPLAYMODE        d3ddm;
170
171     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
172     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
173     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
174     ok(hwnd != NULL, "Failed to create window\n");
175     if (!pD3d || !hwnd) goto cleanup;
176
177     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
178     ZeroMemory( &d3dpp, sizeof(d3dpp) );
179     d3dpp.Windowed         = TRUE;
180     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
181     d3dpp.BackBufferFormat = d3ddm.Format;
182
183     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, hwnd,
184                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
185     ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "Failed to create IDirect3D9Device (%08x)\n", hr);
186     if (FAILED(hr)) {
187         skip("failed to create a d3d device\n");
188         goto cleanup;
189     }
190
191     check_mipmap_levels(pDevice, 32, 32, 6);
192     check_mipmap_levels(pDevice, 256, 1, 9);
193     check_mipmap_levels(pDevice, 1, 256, 9);
194     check_mipmap_levels(pDevice, 1, 1, 1);
195
196 cleanup:
197     if (pDevice)
198     {
199         UINT refcount = IDirect3DDevice9_Release( pDevice );
200         ok(!refcount, "Device has %u references left.\n", refcount);
201     }
202     if (pD3d) IDirect3D9_Release( pD3d );
203     DestroyWindow( hwnd );
204 }
205
206 static void test_checkdevicemultisampletype(void)
207 {
208
209     HRESULT               hr;
210     HWND                  hwnd = NULL;
211
212     IDirect3D9            *pD3d = NULL;
213     IDirect3DDevice9      *pDevice = NULL;
214     D3DPRESENT_PARAMETERS d3dpp;
215     D3DDISPLAYMODE        d3ddm;
216     DWORD                 qualityLevels;
217
218     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
219     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
220     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
221     ok(hwnd != NULL, "Failed to create window\n");
222     if (!pD3d || !hwnd) goto cleanup;
223
224     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
225     ZeroMemory( &d3dpp, sizeof(d3dpp) );
226     d3dpp.Windowed         = TRUE;
227     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
228     d3dpp.BackBufferFormat = d3ddm.Format;
229
230     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, hwnd,
231                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
232     ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "Failed to create IDirect3D9Device (%08x)\n", hr);
233     if (FAILED(hr)) {
234         skip("failed to create a d3d device\n");
235         goto cleanup;
236     }
237
238     qualityLevels = 0;
239
240     hr = IDirect3D9_CheckDeviceMultiSampleType(pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, TRUE,
241     D3DMULTISAMPLE_NONE, &qualityLevels);
242     ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "CheckDeviceMultiSampleType failed with (%08x)\n", hr);
243     if(hr == D3DERR_NOTAVAILABLE)
244     {
245         skip("IDirect3D9_CheckDeviceMultiSampleType not available\n");
246         goto cleanup;
247     }
248     ok(qualityLevels == 1,"qualitylevel is not 1 but %d\n",qualityLevels);
249
250     hr = IDirect3D9_CheckDeviceMultiSampleType(pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, FALSE,
251     D3DMULTISAMPLE_NONE, &qualityLevels);
252     ok(SUCCEEDED(hr), "CheckDeviceMultiSampleType failed with (%08x)\n", hr);
253     ok(qualityLevels == 1,"qualitylevel is not 1 but %d\n",qualityLevels);
254
255 cleanup:
256     if (pDevice)
257     {
258         UINT refcount = IDirect3DDevice9_Release( pDevice );
259         ok(!refcount, "Device has %u references left.\n", refcount);
260     }
261     if (pD3d) IDirect3D9_Release( pD3d );
262     DestroyWindow( hwnd );
263 }
264
265 static void test_swapchain(void)
266 {
267     HRESULT                      hr;
268     HWND                         hwnd               = NULL;
269     IDirect3D9                  *pD3d               = NULL;
270     IDirect3DDevice9            *pDevice            = NULL;
271     IDirect3DSwapChain9         *swapchain0         = NULL;
272     IDirect3DSwapChain9         *swapchain1         = NULL;
273     IDirect3DSwapChain9         *swapchain2         = NULL;
274     IDirect3DSwapChain9         *swapchain3         = NULL;
275     IDirect3DSwapChain9         *swapchainX         = NULL;
276     IDirect3DSurface9           *backbuffer         = NULL;
277     D3DPRESENT_PARAMETERS        d3dpp;
278     D3DDISPLAYMODE               d3ddm;
279
280     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
281     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
282     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
283     ok(hwnd != NULL, "Failed to create window\n");
284     if (!pD3d || !hwnd) goto cleanup;
285
286     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
287     ZeroMemory( &d3dpp, sizeof(d3dpp) );
288     d3dpp.Windowed         = TRUE;
289     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
290     d3dpp.BackBufferFormat = d3ddm.Format;
291     d3dpp.BackBufferCount  = 0;
292
293     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
294                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
295     ok(hr == S_OK || hr == D3DERR_NOTAVAILABLE,
296        "Failed to create IDirect3D9Device (%08x)\n", hr);
297     if (FAILED(hr)) goto cleanup;
298
299     /* Check if the back buffer count was modified */
300     ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
301
302     /* Get the implicit swapchain */
303     hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &swapchain0);
304     ok(SUCCEEDED(hr), "Failed to get the implicit swapchain (%08x)\n", hr);
305     if(swapchain0) IDirect3DSwapChain9_Release(swapchain0);
306
307     /* Check if there is a back buffer */
308     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
309     ok(SUCCEEDED(hr), "Failed to get the back buffer (%08x)\n", hr);
310     ok(backbuffer != NULL, "The back buffer is NULL\n");
311     if(backbuffer) IDirect3DSurface9_Release(backbuffer);
312
313     /* Try to get a nonexistent swapchain */
314     hr = IDirect3DDevice9_GetSwapChain(pDevice, 1, &swapchainX);
315     ok(hr == D3DERR_INVALIDCALL, "GetSwapChain on an nonexistent swapchain returned (%08x)\n", hr);
316     ok(swapchainX == NULL, "Swapchain 1 is %p\n", swapchainX);
317     if(swapchainX) IDirect3DSwapChain9_Release(swapchainX);
318
319     /* Create a bunch of swapchains */
320     d3dpp.BackBufferCount = 0;
321     hr = IDirect3DDevice9_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain1);
322     ok(SUCCEEDED(hr), "Failed to create a swapchain (%08x)\n", hr);
323     ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
324
325     d3dpp.BackBufferCount  = 1;
326     hr = IDirect3DDevice9_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain2);
327     ok(SUCCEEDED(hr), "Failed to create a swapchain (%08x)\n", hr);
328
329     d3dpp.BackBufferCount  = 2;
330     hr = IDirect3DDevice9_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain3);
331     ok(SUCCEEDED(hr), "Failed to create a swapchain (%08x)\n", hr);
332     if(SUCCEEDED(hr)) {
333         /* Swapchain 3, created with backbuffercount 2 */
334         backbuffer = (void *) 0xdeadbeef;
335         hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 0, 0, &backbuffer);
336         ok(SUCCEEDED(hr), "Failed to get the 1st back buffer (%08x)\n", hr);
337         ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
338         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
339
340         backbuffer = (void *) 0xdeadbeef;
341         hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 1, 0, &backbuffer);
342         ok(SUCCEEDED(hr), "Failed to get the 2nd back buffer (%08x)\n", hr);
343         ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
344         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
345
346         backbuffer = (void *) 0xdeadbeef;
347         hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 2, 0, &backbuffer);
348         ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %08x\n", hr);
349         ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
350         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
351
352         backbuffer = (void *) 0xdeadbeef;
353         hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 3, 0, &backbuffer);
354         ok(FAILED(hr), "Failed to get the back buffer (%08x)\n", hr);
355         ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
356         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
357     }
358
359     /* Check the back buffers of the swapchains */
360     /* Swapchain 1, created with backbuffercount 0 */
361     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain1, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
362     ok(SUCCEEDED(hr), "Failed to get the back buffer (%08x)\n", hr);
363     ok(backbuffer != NULL, "The back buffer is NULL (%08x)\n", hr);
364     if(backbuffer) IDirect3DSurface9_Release(backbuffer);
365
366     backbuffer = (void *) 0xdeadbeef;
367     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain1, 1, 0, &backbuffer);
368     ok(FAILED(hr), "Failed to get the back buffer (%08x)\n", hr);
369     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
370     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
371
372     /* Swapchain 2 - created with backbuffercount 1 */
373     backbuffer = (void *) 0xdeadbeef;
374     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain2, 0, 0, &backbuffer);
375     ok(SUCCEEDED(hr), "Failed to get the back buffer (%08x)\n", hr);
376     ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
377     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
378
379     backbuffer = (void *) 0xdeadbeef;
380     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain2, 1, 0, &backbuffer);
381     ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %08x\n", hr);
382     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
383     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
384
385     backbuffer = (void *) 0xdeadbeef;
386     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain2, 2, 0, &backbuffer);
387     ok(FAILED(hr), "Failed to get the back buffer (%08x)\n", hr);
388     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
389     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
390
391     /* Try getSwapChain on a manually created swapchain
392      * it should fail, apparently GetSwapChain only returns implicit swapchains
393      */
394     swapchainX = (void *) 0xdeadbeef;
395     hr = IDirect3DDevice9_GetSwapChain(pDevice, 1, &swapchainX);
396     ok(hr == D3DERR_INVALIDCALL, "Failed to get the second swapchain (%08x)\n", hr);
397     ok(swapchainX == NULL, "The swapchain pointer is %p\n", swapchainX);
398     if(swapchainX && swapchainX != (void *) 0xdeadbeef ) IDirect3DSwapChain9_Release(swapchainX);
399
400 cleanup:
401     if(swapchain1) IDirect3DSwapChain9_Release(swapchain1);
402     if(swapchain2) IDirect3DSwapChain9_Release(swapchain2);
403     if(swapchain3) IDirect3DSwapChain9_Release(swapchain3);
404     if (pDevice)
405     {
406         UINT refcount = IDirect3DDevice9_Release(pDevice);
407         ok(!refcount, "Device has %u references left.\n", refcount);
408     }
409     if (pD3d) IDirect3D9_Release(pD3d);
410     DestroyWindow( hwnd );
411 }
412
413 /* Shared between two functions */
414 static const DWORD simple_vs[] = {0xFFFE0101,       /* vs_1_1               */
415     0x0000001F, 0x80000000, 0x900F0000,             /* dcl_position0 v0     */
416     0x00000009, 0xC0010000, 0x90E40000, 0xA0E40000, /* dp4 oPos.x, v0, c0   */
417     0x00000009, 0xC0020000, 0x90E40000, 0xA0E40001, /* dp4 oPos.y, v0, c1   */
418     0x00000009, 0xC0040000, 0x90E40000, 0xA0E40002, /* dp4 oPos.z, v0, c2   */
419     0x00000009, 0xC0080000, 0x90E40000, 0xA0E40003, /* dp4 oPos.w, v0, c3   */
420     0x0000FFFF};                                    /* END                  */
421
422 static void test_refcount(void)
423 {
424     HRESULT                      hr;
425     HWND                         hwnd               = NULL;
426     IDirect3D9                  *pD3d               = NULL;
427     IDirect3D9                  *pD3d2              = NULL;
428     IDirect3DDevice9            *pDevice            = NULL;
429     IDirect3DVertexBuffer9      *pVertexBuffer      = NULL;
430     IDirect3DIndexBuffer9       *pIndexBuffer       = NULL;
431     IDirect3DVertexDeclaration9 *pVertexDeclaration = NULL;
432     IDirect3DVertexShader9      *pVertexShader      = NULL;
433     IDirect3DPixelShader9       *pPixelShader       = NULL;
434     IDirect3DCubeTexture9       *pCubeTexture       = NULL;
435     IDirect3DTexture9           *pTexture           = NULL;
436     IDirect3DVolumeTexture9     *pVolumeTexture     = NULL;
437     IDirect3DVolume9            *pVolumeLevel       = NULL;
438     IDirect3DSurface9           *pStencilSurface    = NULL;
439     IDirect3DSurface9           *pOffscreenSurface  = NULL;
440     IDirect3DSurface9           *pRenderTarget      = NULL;
441     IDirect3DSurface9           *pRenderTarget2     = NULL;
442     IDirect3DSurface9           *pRenderTarget3     = NULL;
443     IDirect3DSurface9           *pTextureLevel      = NULL;
444     IDirect3DSurface9           *pBackBuffer        = NULL;
445     IDirect3DStateBlock9        *pStateBlock        = NULL;
446     IDirect3DStateBlock9        *pStateBlock1       = NULL;
447     IDirect3DSwapChain9         *pSwapChain         = NULL;
448     IDirect3DQuery9             *pQuery             = NULL;
449     D3DPRESENT_PARAMETERS        d3dpp;
450     D3DDISPLAYMODE               d3ddm;
451     int                          refcount = 0, tmp;
452
453     D3DVERTEXELEMENT9 decl[] =
454     {
455         D3DDECL_END()
456     };
457     static DWORD simple_ps[] = {0xFFFF0101,                                     /* ps_1_1                       */
458         0x00000051, 0xA00F0001, 0x3F800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0  */
459         0x00000042, 0xB00F0000,                                                 /* tex t0                       */
460         0x00000008, 0x800F0000, 0xA0E40001, 0xA0E40000,                         /* dp3 r0, c1, c0               */
461         0x00000005, 0x800F0000, 0x90E40000, 0x80E40000,                         /* mul r0, v0, r0               */
462         0x00000005, 0x800F0000, 0xB0E40000, 0x80E40000,                         /* mul r0, t0, r0               */
463         0x0000FFFF};                                                            /* END                          */
464
465
466     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
467     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
468     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
469     ok(hwnd != NULL, "Failed to create window\n");
470     if (!pD3d || !hwnd) goto cleanup;
471
472     CHECK_REFCOUNT( pD3d, 1 );
473
474     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
475     ZeroMemory( &d3dpp, sizeof(d3dpp) );
476     d3dpp.Windowed         = TRUE;
477     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
478     d3dpp.BackBufferFormat = d3ddm.Format;
479     d3dpp.EnableAutoDepthStencil = TRUE;
480     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
481
482     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
483                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
484     ok(hr == S_OK || hr == D3DERR_NOTAVAILABLE,
485        "Failed to create IDirect3D9Device (%08x)\n", hr);
486     if (FAILED(hr)) goto cleanup;
487
488     refcount = get_refcount( (IUnknown *)pDevice );
489     ok(refcount == 1, "Invalid device RefCount %d\n", refcount);
490
491     CHECK_REFCOUNT( pD3d, 2 );
492
493     hr = IDirect3DDevice9_GetDirect3D(pDevice, &pD3d2);
494     CHECK_CALL( hr, "GetDirect3D", pDevice, refcount );
495
496     ok(pD3d2 == pD3d, "Expected IDirect3D9 pointers to be equal\n");
497     CHECK_REFCOUNT( pD3d, 3 );
498     CHECK_RELEASE_REFCOUNT( pD3d, 2 );
499
500     /**
501      * Check refcount of implicit surfaces and implicit swapchain. Findings:
502      *   - the container is the device OR swapchain
503      *   - they hold a reference to the device
504      *   - they are created with a refcount of 0 (Get/Release returns original refcount)
505      *   - they are not freed if refcount reaches 0.
506      *   - the refcount is not forwarded to the container.
507      */
508     hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &pSwapChain);
509     CHECK_CALL( hr, "GetSwapChain", pDevice, ++refcount);
510     if (pSwapChain)
511     {
512         CHECK_REFCOUNT( pSwapChain, 1);
513
514         hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget);
515         CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
516         CHECK_REFCOUNT( pSwapChain, 1);
517         if(pRenderTarget)
518         {
519             CHECK_SURFACE_CONTAINER( pRenderTarget, IID_IDirect3DSwapChain9, pSwapChain);
520             CHECK_REFCOUNT( pRenderTarget, 1);
521
522             CHECK_ADDREF_REFCOUNT(pRenderTarget, 2);
523             CHECK_REFCOUNT(pDevice, refcount);
524             CHECK_RELEASE_REFCOUNT(pRenderTarget, 1);
525             CHECK_REFCOUNT(pDevice, refcount);
526
527             hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget);
528             CHECK_CALL( hr, "GetRenderTarget", pDevice, refcount);
529             CHECK_REFCOUNT( pRenderTarget, 2);
530             CHECK_RELEASE_REFCOUNT( pRenderTarget, 1);
531             CHECK_RELEASE_REFCOUNT( pRenderTarget, 0);
532             CHECK_REFCOUNT( pDevice, --refcount);
533
534             /* The render target is released with the device, so AddRef with refcount=0 is fine here. */
535             CHECK_ADDREF_REFCOUNT(pRenderTarget, 1);
536             CHECK_REFCOUNT(pDevice, ++refcount);
537             CHECK_RELEASE_REFCOUNT(pRenderTarget, 0);
538             CHECK_REFCOUNT(pDevice, --refcount);
539         }
540
541         /* Render target and back buffer are identical. */
542         hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, 0, &pBackBuffer);
543         CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
544         if(pBackBuffer)
545         {
546             CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
547             ok(pRenderTarget == pBackBuffer, "RenderTarget=%p and BackBuffer=%p should be the same.\n",
548             pRenderTarget, pBackBuffer);
549             pBackBuffer = NULL;
550         }
551         CHECK_REFCOUNT( pDevice, --refcount);
552
553         hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pStencilSurface);
554         CHECK_CALL( hr, "GetDepthStencilSurface", pDevice, ++refcount);
555         CHECK_REFCOUNT( pSwapChain, 1);
556         if(pStencilSurface)
557         {
558             CHECK_SURFACE_CONTAINER( pStencilSurface, IID_IDirect3DDevice9, pDevice);
559             CHECK_REFCOUNT( pStencilSurface, 1);
560
561             CHECK_ADDREF_REFCOUNT(pStencilSurface, 2);
562             CHECK_REFCOUNT(pDevice, refcount);
563             CHECK_RELEASE_REFCOUNT(pStencilSurface, 1);
564             CHECK_REFCOUNT(pDevice, refcount);
565
566             CHECK_RELEASE_REFCOUNT( pStencilSurface, 0);
567             CHECK_REFCOUNT( pDevice, --refcount);
568
569             /* The stencil surface is released with the device, so AddRef with refcount=0 is fine here. */
570             CHECK_ADDREF_REFCOUNT(pStencilSurface, 1);
571             CHECK_REFCOUNT(pDevice, ++refcount);
572             CHECK_RELEASE_REFCOUNT(pStencilSurface, 0);
573             CHECK_REFCOUNT(pDevice, --refcount);
574             pStencilSurface = NULL;
575         }
576
577         CHECK_RELEASE_REFCOUNT( pSwapChain, 0);
578         CHECK_REFCOUNT( pDevice, --refcount);
579
580         /* The implicit swapchwin is released with the device, so AddRef with refcount=0 is fine here. */
581         CHECK_ADDREF_REFCOUNT(pSwapChain, 1);
582         CHECK_REFCOUNT(pDevice, ++refcount);
583         CHECK_RELEASE_REFCOUNT(pSwapChain, 0);
584         CHECK_REFCOUNT(pDevice, --refcount);
585         pSwapChain = NULL;
586     }
587
588     /* Buffers */
589     hr = IDirect3DDevice9_CreateIndexBuffer( pDevice, 16, 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &pIndexBuffer, NULL );
590     CHECK_CALL( hr, "CreateIndexBuffer", pDevice, ++refcount );
591     if(pIndexBuffer)
592     {
593         tmp = get_refcount( (IUnknown *)pIndexBuffer );
594
595         hr = IDirect3DDevice9_SetIndices(pDevice, pIndexBuffer);
596         CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
597         hr = IDirect3DDevice9_SetIndices(pDevice, NULL);
598         CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
599     }
600
601     hr = IDirect3DDevice9_CreateVertexBuffer( pDevice, 16, 0, D3DFVF_XYZ, D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
602     CHECK_CALL( hr, "CreateVertexBuffer", pDevice, ++refcount );
603     if(pVertexBuffer)
604     {
605         IDirect3DVertexBuffer9 *pVBuf = (void*)~0;
606         UINT offset = ~0;
607         UINT stride = ~0;
608
609         tmp = get_refcount( (IUnknown *)pVertexBuffer );
610
611         hr = IDirect3DDevice9_SetStreamSource(pDevice, 0, pVertexBuffer, 0, 3 * sizeof(float));
612         CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
613         hr = IDirect3DDevice9_SetStreamSource(pDevice, 0, NULL, 0, 0);
614         CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
615
616         hr = IDirect3DDevice9_GetStreamSource(pDevice, 0, &pVBuf, &offset, &stride);
617         ok(SUCCEEDED(hr), "GetStreamSource did not succeed with NULL stream!\n");
618         ok(pVBuf==NULL, "pVBuf not NULL (%p)!\n", pVBuf);
619         ok(stride==3*sizeof(float), "stride not 3 floats (got %u)!\n", stride);
620         ok(offset==0, "offset not 0 (got %u)!\n", offset);
621     }
622     /* Shaders */
623     hr = IDirect3DDevice9_CreateVertexDeclaration( pDevice, decl, &pVertexDeclaration );
624     CHECK_CALL( hr, "CreateVertexDeclaration", pDevice, ++refcount );
625     hr = IDirect3DDevice9_CreateVertexShader( pDevice, simple_vs, &pVertexShader );
626     CHECK_CALL( hr, "CreateVertexShader", pDevice, ++refcount );
627     hr = IDirect3DDevice9_CreatePixelShader( pDevice, simple_ps, &pPixelShader );
628     CHECK_CALL( hr, "CreatePixelShader", pDevice, ++refcount );
629     /* Textures */
630     hr = IDirect3DDevice9_CreateTexture( pDevice, 32, 32, 3, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pTexture, NULL );
631     CHECK_CALL( hr, "CreateTexture", pDevice, ++refcount );
632     if (pTexture)
633     {
634         tmp = get_refcount( (IUnknown *)pTexture );
635
636         /* SetTexture should not increase refcounts */
637         hr = IDirect3DDevice9_SetTexture(pDevice, 0, (IDirect3DBaseTexture9 *) pTexture);
638         CHECK_CALL( hr, "SetTexture", pTexture, tmp);
639         hr = IDirect3DDevice9_SetTexture(pDevice, 0, NULL);
640         CHECK_CALL( hr, "SetTexture", pTexture, tmp);
641
642         /* This should not increment device refcount */
643         hr = IDirect3DTexture9_GetSurfaceLevel( pTexture, 1, &pTextureLevel );
644         CHECK_CALL( hr, "GetSurfaceLevel", pDevice, refcount );
645         /* But should increment texture's refcount */
646         CHECK_REFCOUNT( pTexture, tmp+1 );
647         /* Because the texture and surface refcount are identical */
648         if (pTextureLevel)
649         {
650             CHECK_REFCOUNT        ( pTextureLevel, tmp+1 );
651             CHECK_ADDREF_REFCOUNT ( pTextureLevel, tmp+2 );
652             CHECK_REFCOUNT        ( pTexture     , tmp+2 );
653             CHECK_RELEASE_REFCOUNT( pTextureLevel, tmp+1 );
654             CHECK_REFCOUNT        ( pTexture     , tmp+1 );
655             CHECK_RELEASE_REFCOUNT( pTexture     , tmp   );
656             CHECK_REFCOUNT        ( pTextureLevel, tmp   );
657         }
658     }
659     hr = IDirect3DDevice9_CreateCubeTexture( pDevice, 32, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pCubeTexture, NULL );
660     CHECK_CALL( hr, "CreateCubeTexture", pDevice, ++refcount );
661     hr = IDirect3DDevice9_CreateVolumeTexture( pDevice, 32, 32, 2, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pVolumeTexture, NULL );
662     CHECK_CALL( hr, "CreateVolumeTexture", pDevice, ++refcount );
663     if (pVolumeTexture)
664     {
665         tmp = get_refcount( (IUnknown *)pVolumeTexture );
666
667         /* This should not increment device refcount */
668         hr = IDirect3DVolumeTexture9_GetVolumeLevel(pVolumeTexture, 0, &pVolumeLevel);
669         CHECK_CALL( hr, "GetVolumeLevel", pDevice, refcount );
670         /* But should increment volume texture's refcount */
671         CHECK_REFCOUNT( pVolumeTexture, tmp+1 );
672         /* Because the volume texture and volume refcount are identical */
673         if (pVolumeLevel)
674         {
675             CHECK_REFCOUNT        ( pVolumeLevel  , tmp+1 );
676             CHECK_ADDREF_REFCOUNT ( pVolumeLevel  , tmp+2 );
677             CHECK_REFCOUNT        ( pVolumeTexture, tmp+2 );
678             CHECK_RELEASE_REFCOUNT( pVolumeLevel  , tmp+1 );
679             CHECK_REFCOUNT        ( pVolumeTexture, tmp+1 );
680             CHECK_RELEASE_REFCOUNT( pVolumeTexture, tmp   );
681             CHECK_REFCOUNT        ( pVolumeLevel  , tmp   );
682         }
683     }
684     /* Surfaces */
685     hr = IDirect3DDevice9_CreateDepthStencilSurface( pDevice, 32, 32, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, TRUE, &pStencilSurface, NULL );
686     CHECK_CALL( hr, "CreateDepthStencilSurface", pDevice, ++refcount );
687     CHECK_REFCOUNT( pStencilSurface, 1 );
688     hr = IDirect3DDevice9_CreateOffscreenPlainSurface( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pOffscreenSurface, NULL );
689     CHECK_CALL( hr, "CreateOffscreenPlainSurface", pDevice, ++refcount );
690     CHECK_REFCOUNT( pOffscreenSurface, 1 );
691     hr = IDirect3DDevice9_CreateRenderTarget( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &pRenderTarget3, NULL );
692     CHECK_CALL( hr, "CreateRenderTarget", pDevice, ++refcount );
693     CHECK_REFCOUNT( pRenderTarget3, 1 );
694     /* Misc */
695     hr = IDirect3DDevice9_CreateStateBlock( pDevice, D3DSBT_ALL, &pStateBlock );
696     CHECK_CALL( hr, "CreateStateBlock", pDevice, ++refcount );
697     hr = IDirect3DDevice9_CreateAdditionalSwapChain( pDevice, &d3dpp, &pSwapChain );
698     CHECK_CALL( hr, "CreateAdditionalSwapChain", pDevice, ++refcount );
699     if(pSwapChain)
700     {
701         /* check implicit back buffer */
702         hr = IDirect3DSwapChain9_GetBackBuffer(pSwapChain, 0, 0, &pBackBuffer);
703         CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
704         CHECK_REFCOUNT( pSwapChain, 1);
705         if(pBackBuffer)
706         {
707             CHECK_SURFACE_CONTAINER( pBackBuffer, IID_IDirect3DSwapChain9, pSwapChain);
708             CHECK_REFCOUNT( pBackBuffer, 1);
709             CHECK_RELEASE_REFCOUNT( pBackBuffer, 0);
710             CHECK_REFCOUNT( pDevice, --refcount);
711
712             /* The back buffer is released with the swapchain, so AddRef with refcount=0 is fine here. */
713             CHECK_ADDREF_REFCOUNT(pBackBuffer, 1);
714             CHECK_REFCOUNT(pDevice, ++refcount);
715             CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
716             CHECK_REFCOUNT(pDevice, --refcount);
717             pBackBuffer = NULL;
718         }
719         CHECK_REFCOUNT( pSwapChain, 1);
720     }
721     hr = IDirect3DDevice9_CreateQuery( pDevice, D3DQUERYTYPE_EVENT, &pQuery );
722     CHECK_CALL( hr, "CreateQuery", pDevice, ++refcount );
723
724     hr = IDirect3DDevice9_BeginStateBlock( pDevice );
725     CHECK_CALL( hr, "BeginStateBlock", pDevice, refcount );
726     hr = IDirect3DDevice9_EndStateBlock( pDevice, &pStateBlock1 );
727     CHECK_CALL( hr, "EndStateBlock", pDevice, ++refcount );
728
729     /* The implicit render target is not freed if refcount reaches 0.
730      * Otherwise GetRenderTarget would re-allocate it and the pointer would change.*/
731     hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget2);
732     CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
733     if(pRenderTarget2)
734     {
735         CHECK_RELEASE_REFCOUNT(pRenderTarget2, 0);
736         ok(pRenderTarget == pRenderTarget2, "RenderTarget=%p and RenderTarget2=%p should be the same.\n",
737            pRenderTarget, pRenderTarget2);
738         CHECK_REFCOUNT( pDevice, --refcount);
739         pRenderTarget2 = NULL;
740     }
741     pRenderTarget = NULL;
742
743 cleanup:
744     CHECK_RELEASE(pDevice,              pDevice, --refcount);
745
746     /* Buffers */
747     CHECK_RELEASE(pVertexBuffer,        pDevice, --refcount);
748     CHECK_RELEASE(pIndexBuffer,         pDevice, --refcount);
749     /* Shaders */
750     CHECK_RELEASE(pVertexDeclaration,   pDevice, --refcount);
751     CHECK_RELEASE(pVertexShader,        pDevice, --refcount);
752     CHECK_RELEASE(pPixelShader,         pDevice, --refcount);
753     /* Textures */
754     CHECK_RELEASE(pTextureLevel,        pDevice, --refcount);
755     CHECK_RELEASE(pCubeTexture,         pDevice, --refcount);
756     CHECK_RELEASE(pVolumeTexture,       pDevice, --refcount);
757     /* Surfaces */
758     CHECK_RELEASE(pStencilSurface,      pDevice, --refcount);
759     CHECK_RELEASE(pOffscreenSurface,    pDevice, --refcount);
760     CHECK_RELEASE(pRenderTarget3,       pDevice, --refcount);
761     /* Misc */
762     CHECK_RELEASE(pStateBlock,          pDevice, --refcount);
763     CHECK_RELEASE(pSwapChain,           pDevice, --refcount);
764     CHECK_RELEASE(pQuery,               pDevice, --refcount);
765     /* This will destroy device - cannot check the refcount here */
766     if (pStateBlock1)         CHECK_RELEASE_REFCOUNT( pStateBlock1, 0);
767
768     if (pD3d)                 CHECK_RELEASE_REFCOUNT( pD3d, 0);
769
770     DestroyWindow( hwnd );
771 }
772
773 static void test_cursor(void)
774 {
775     HRESULT                      hr;
776     HWND                         hwnd               = NULL;
777     IDirect3D9                  *pD3d               = NULL;
778     IDirect3DDevice9            *pDevice            = NULL;
779     D3DPRESENT_PARAMETERS        d3dpp;
780     D3DDISPLAYMODE               d3ddm;
781     CURSORINFO                   info;
782     IDirect3DSurface9 *cursor = NULL;
783     HCURSOR cur;
784
785     memset(&info, 0, sizeof(info));
786     info.cbSize = sizeof(info);
787     ok(GetCursorInfo(&info), "GetCursorInfo failed\n");
788     cur = info.hCursor;
789
790     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
791     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
792     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
793     ok(hwnd != NULL, "Failed to create window\n");
794     if (!pD3d || !hwnd) goto cleanup;
795
796     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
797     ZeroMemory( &d3dpp, sizeof(d3dpp) );
798     d3dpp.Windowed         = TRUE;
799     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
800     d3dpp.BackBufferFormat = d3ddm.Format;
801
802     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
803                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
804     ok(hr == S_OK || hr == D3DERR_NOTAVAILABLE,
805        "Failed to create IDirect3D9Device (%08x)\n", hr);
806     if (FAILED(hr)) goto cleanup;
807
808     IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 32, 32, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &cursor, 0);
809     ok(cursor != NULL, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %08x\n", hr);
810
811     /* Initially hidden */
812     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
813     ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
814
815     /* Not enabled without a surface*/
816     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
817     ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
818
819     /* Fails */
820     hr = IDirect3DDevice9_SetCursorProperties(pDevice, 0, 0, NULL);
821     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_SetCursorProperties returned %08x\n", hr);
822
823     hr = IDirect3DDevice9_SetCursorProperties(pDevice, 0, 0, cursor);
824     ok(hr == D3D_OK, "IDirect3DDevice9_SetCursorProperties returned %08x\n", hr);
825
826     IDirect3DSurface9_Release(cursor);
827
828     memset(&info, 0, sizeof(info));
829     info.cbSize = sizeof(info);
830     ok(GetCursorInfo(&info), "GetCursorInfo failed\n");
831     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
832     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
833
834     /* Still hidden */
835     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
836     ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
837
838     /* Enabled now*/
839     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
840     ok(hr == TRUE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
841
842     /* GDI cursor unchanged */
843     memset(&info, 0, sizeof(info));
844     info.cbSize = sizeof(info);
845     ok(GetCursorInfo(&info), "GetCursorInfo failed\n");
846     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
847     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
848
849 cleanup:
850     if (pDevice)
851     {
852         UINT refcount = IDirect3DDevice9_Release(pDevice);
853         ok(!refcount, "Device has %u references left.\n", refcount);
854     }
855     if (pD3d) IDirect3D9_Release(pD3d);
856     DestroyWindow( hwnd );
857 }
858
859 static void test_reset(void)
860 {
861     HRESULT                      hr;
862     HWND                         hwnd               = NULL;
863     RECT                         winrect;
864     IDirect3D9                  *pD3d               = NULL;
865     D3DPRESENT_PARAMETERS        d3dpp;
866     D3DDISPLAYMODE               d3ddm, d3ddm2;
867     D3DVIEWPORT9                 vp;
868     DWORD                        width, orig_width = GetSystemMetrics(SM_CXSCREEN);
869     DWORD                        height, orig_height = GetSystemMetrics(SM_CYSCREEN);
870     IDirect3DSwapChain9          *pSwapchain;
871     IDirect3DSurface9            *surface;
872     IDirect3DTexture9            *texture;
873     IDirect3DVertexShader9       *shader;
874     UINT                         i, adapter_mode_count;
875     D3DLOCKED_RECT               lockrect;
876     IDirect3DDevice9 *device1 = NULL;
877     IDirect3DDevice9 *device2 = NULL;
878     D3DCAPS9 caps;
879     DWORD value;
880     struct
881     {
882         UINT w;
883         UINT h;
884     } *modes = NULL;
885     UINT mode_count = 0;
886
887     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
888     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
889     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
890     ok(hwnd != NULL, "Failed to create window\n");
891     if (!pD3d || !hwnd) goto cleanup;
892
893     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
894     adapter_mode_count = IDirect3D9_GetAdapterModeCount(pD3d, D3DADAPTER_DEFAULT, d3ddm.Format);
895     modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*modes) * adapter_mode_count);
896     for(i = 0; i < adapter_mode_count; ++i)
897     {
898         UINT j;
899         ZeroMemory( &d3ddm2, sizeof(d3ddm2) );
900         hr = IDirect3D9_EnumAdapterModes(pD3d, D3DADAPTER_DEFAULT, d3ddm.Format, i, &d3ddm2);
901         ok(hr == D3D_OK, "IDirect3D9_EnumAdapterModes returned %#x\n", hr);
902
903         for (j = 0; j < mode_count; ++j)
904         {
905             if (modes[j].w == d3ddm2.Width && modes[j].h == d3ddm2.Height)
906                 break;
907         }
908         if (j == mode_count)
909         {
910             modes[j].w = d3ddm2.Width;
911             modes[j].h = d3ddm2.Height;
912             ++mode_count;
913         }
914
915         /* We use them as invalid modes */
916         if((d3ddm2.Width == 801 && d3ddm2.Height == 600) ||
917            (d3ddm2.Width == 32 && d3ddm2.Height == 32)) {
918             skip("This system supports a screen resolution of %dx%d, not running mode tests\n",
919                  d3ddm2.Width, d3ddm2.Height);
920             goto cleanup;
921         }
922     }
923
924     if (mode_count < 2)
925     {
926         skip("Less than 2 modes supported, skipping mode tests\n");
927         goto cleanup;
928     }
929
930     i = 0;
931     if (modes[i].w == orig_width && modes[i].h == orig_height) ++i;
932
933     ZeroMemory( &d3dpp, sizeof(d3dpp) );
934     d3dpp.Windowed         = FALSE;
935     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
936     d3dpp.BackBufferWidth  = modes[i].w;
937     d3dpp.BackBufferHeight = modes[i].h;
938     d3dpp.BackBufferFormat = d3ddm.Format;
939     d3dpp.EnableAutoDepthStencil = TRUE;
940     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
941
942     hr = IDirect3D9_CreateDevice(pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
943             hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device1);
944     if (FAILED(hr))
945     {
946         skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
947         goto cleanup;
948     }
949     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
950     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after creation returned %#x\n", hr);
951
952     hr = IDirect3DDevice9_GetDeviceCaps(device1, &caps);
953     ok(SUCCEEDED(hr), "GetDeviceCaps failed, hr %#x.\n", hr);
954
955     width = GetSystemMetrics(SM_CXSCREEN);
956     height = GetSystemMetrics(SM_CYSCREEN);
957     ok(width == modes[i].w, "Screen width is %u, expected %u\n", width, modes[i].w);
958     ok(height == modes[i].h, "Screen height is %u, expected %u\n", height, modes[i].h);
959
960     hr = IDirect3DDevice9_GetViewport(device1, &vp);
961     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %08x\n", hr);
962     if(SUCCEEDED(hr))
963     {
964         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
965         ok(vp.Y == 0, "D3DVIEWPORT->Y = %d\n", vp.Y);
966         ok(vp.Width == modes[i].w, "D3DVIEWPORT->Width = %u, expected %u\n", vp.Width, modes[i].w);
967         ok(vp.Height == modes[i].h, "D3DVIEWPORT->Height = %u, expected %u\n", vp.Height, modes[i].h);
968         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %f\n", vp.MinZ);
969         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %f\n", vp.MaxZ);
970     }
971
972     i = 1;
973     vp.X = 10;
974     vp.Y = 20;
975     vp.MinZ = 2;
976     vp.MaxZ = 3;
977     hr = IDirect3DDevice9_SetViewport(device1, &vp);
978     ok(hr == D3D_OK, "IDirect3DDevice9_SetViewport failed with %08x\n", hr);
979
980     hr = IDirect3DDevice9_GetRenderState(device1, D3DRS_LIGHTING, &value);
981     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
982     ok(!!value, "Got unexpected value %#x for D3DRS_LIGHTING.\n", value);
983     hr = IDirect3DDevice9_SetRenderState(device1, D3DRS_LIGHTING, FALSE);
984     ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
985
986     ZeroMemory( &d3dpp, sizeof(d3dpp) );
987     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
988     d3dpp.Windowed         = FALSE;
989     d3dpp.BackBufferWidth  = modes[i].w;
990     d3dpp.BackBufferHeight = modes[i].h;
991     d3dpp.BackBufferFormat = d3ddm.Format;
992     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
993     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
994     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
995     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
996
997     hr = IDirect3DDevice9_GetRenderState(device1, D3DRS_LIGHTING, &value);
998     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
999     ok(!!value, "Got unexpected value %#x for D3DRS_LIGHTING.\n", value);
1000
1001     ZeroMemory(&vp, sizeof(vp));
1002     hr = IDirect3DDevice9_GetViewport(device1, &vp);
1003     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %08x\n", hr);
1004     if(SUCCEEDED(hr))
1005     {
1006         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
1007         ok(vp.Y == 0, "D3DVIEWPORT->Y = %d\n", vp.Y);
1008         ok(vp.Width == modes[i].w, "D3DVIEWPORT->Width = %u, expected %u\n", vp.Width, modes[i].w);
1009         ok(vp.Height == modes[i].h, "D3DVIEWPORT->Height = %u, expected %u\n", vp.Height, modes[i].h);
1010         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %f\n", vp.MinZ);
1011         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %f\n", vp.MaxZ);
1012     }
1013
1014     width = GetSystemMetrics(SM_CXSCREEN);
1015     height = GetSystemMetrics(SM_CYSCREEN);
1016     ok(width == modes[i].w, "Screen width is %u, expected %u\n", width, modes[i].w);
1017     ok(height == modes[i].h, "Screen height is %u, expected %u\n", height, modes[i].h);
1018
1019     hr = IDirect3DDevice9_GetSwapChain(device1, 0, &pSwapchain);
1020     ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %08x\n", hr);
1021     if(SUCCEEDED(hr))
1022     {
1023         ZeroMemory(&d3dpp, sizeof(d3dpp));
1024         hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
1025         ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %08x\n", hr);
1026         if(SUCCEEDED(hr))
1027         {
1028             ok(d3dpp.BackBufferWidth == modes[i].w, "Back buffer width is %u, expected %u\n",
1029                     d3dpp.BackBufferWidth, modes[i].w);
1030             ok(d3dpp.BackBufferHeight == modes[i].h, "Back buffer height is %u, expected %u\n",
1031                     d3dpp.BackBufferHeight, modes[i].h);
1032         }
1033         IDirect3DSwapChain9_Release(pSwapchain);
1034     }
1035
1036     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1037     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1038     d3dpp.Windowed         = TRUE;
1039     d3dpp.BackBufferWidth  = 400;
1040     d3dpp.BackBufferHeight = 300;
1041     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1042     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1043     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1044     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1045
1046     width = GetSystemMetrics(SM_CXSCREEN);
1047     height = GetSystemMetrics(SM_CYSCREEN);
1048     ok(width == orig_width, "Screen width is %d\n", width);
1049     ok(height == orig_height, "Screen height is %d\n", height);
1050
1051     ZeroMemory(&vp, sizeof(vp));
1052     hr = IDirect3DDevice9_GetViewport(device1, &vp);
1053     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %08x\n", hr);
1054     if(SUCCEEDED(hr))
1055     {
1056         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
1057         ok(vp.Y == 0, "D3DVIEWPORT->Y = %d\n", vp.Y);
1058         ok(vp.Width == 400, "D3DVIEWPORT->Width = %d\n", vp.Width);
1059         ok(vp.Height == 300, "D3DVIEWPORT->Height = %d\n", vp.Height);
1060         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %f\n", vp.MinZ);
1061         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %f\n", vp.MaxZ);
1062     }
1063
1064     hr = IDirect3DDevice9_GetSwapChain(device1, 0, &pSwapchain);
1065     ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %08x\n", hr);
1066     if(SUCCEEDED(hr))
1067     {
1068         ZeroMemory(&d3dpp, sizeof(d3dpp));
1069         hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
1070         ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %08x\n", hr);
1071         if(SUCCEEDED(hr))
1072         {
1073             ok(d3dpp.BackBufferWidth == 400, "Back buffer width is %d\n", d3dpp.BackBufferWidth);
1074             ok(d3dpp.BackBufferHeight == 300, "Back buffer height is %d\n", d3dpp.BackBufferHeight);
1075         }
1076         IDirect3DSwapChain9_Release(pSwapchain);
1077     }
1078
1079     winrect.left = 0;
1080     winrect.top = 0;
1081     winrect.right = 200;
1082     winrect.bottom = 150;
1083     ok(AdjustWindowRect(&winrect, WS_OVERLAPPEDWINDOW, FALSE), "AdjustWindowRect failed\n");
1084     ok(SetWindowPos(hwnd, NULL, 0, 0,
1085                     winrect.right-winrect.left,
1086                     winrect.bottom-winrect.top,
1087                     SWP_NOMOVE|SWP_NOZORDER),
1088        "SetWindowPos failed\n");
1089
1090     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1091     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1092     d3dpp.Windowed         = TRUE;
1093     d3dpp.BackBufferWidth  = 0;
1094     d3dpp.BackBufferHeight = 0;
1095     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1096     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1097     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1098     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1099
1100     ZeroMemory(&vp, sizeof(vp));
1101     hr = IDirect3DDevice9_GetViewport(device1, &vp);
1102     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %08x\n", hr);
1103     if(SUCCEEDED(hr))
1104     {
1105         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
1106         ok(vp.Y == 0, "D3DVIEWPORT->Y = %d\n", vp.Y);
1107         todo_wine ok(vp.Width == 200, "D3DVIEWPORT->Width = %d\n", vp.Width);
1108         todo_wine ok(vp.Height == 150, "D3DVIEWPORT->Height = %d\n", vp.Height);
1109         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %f\n", vp.MinZ);
1110         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %f\n", vp.MaxZ);
1111     }
1112
1113     hr = IDirect3DDevice9_GetSwapChain(device1, 0, &pSwapchain);
1114     ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %08x\n", hr);
1115     if(SUCCEEDED(hr))
1116     {
1117         ZeroMemory(&d3dpp, sizeof(d3dpp));
1118         hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
1119         ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %08x\n", hr);
1120         if(SUCCEEDED(hr))
1121         {
1122             todo_wine ok(d3dpp.BackBufferWidth == 200, "Back buffer width is %d\n", d3dpp.BackBufferWidth);
1123             todo_wine ok(d3dpp.BackBufferHeight == 150, "Back buffer height is %d\n", d3dpp.BackBufferHeight);
1124         }
1125         IDirect3DSwapChain9_Release(pSwapchain);
1126     }
1127
1128     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1129     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1130     d3dpp.Windowed         = TRUE;
1131     d3dpp.BackBufferWidth  = 400;
1132     d3dpp.BackBufferHeight = 300;
1133
1134     /* _Reset fails if there is a resource in the default pool */
1135     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device1, 16, 16, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &surface, NULL);
1136     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
1137     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1138     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1139     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1140     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
1141     IDirect3DSurface9_Release(surface);
1142     /* Reset again to get the device out of the lost state */
1143     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1144     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1145     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1146     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1147
1148     if (caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP)
1149     {
1150         IDirect3DVolumeTexture9 *volume_texture;
1151
1152         hr = IDirect3DDevice9_CreateVolumeTexture(device1, 16, 16, 4, 1, 0,
1153                 D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &volume_texture, NULL);
1154         ok(SUCCEEDED(hr), "CreateVolumeTexture failed, hr %#x.\n", hr);
1155         hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1156         ok(hr == D3DERR_INVALIDCALL, "Reset returned %#x, expected %#x.\n", hr, D3DERR_INVALIDCALL);
1157         hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1158         ok(hr == D3DERR_DEVICENOTRESET, "TestCooperativeLevel returned %#x, expected %#x.\n",
1159                 hr, D3DERR_DEVICENOTRESET);
1160         IDirect3DVolumeTexture9_Release(volume_texture);
1161         hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1162         ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1163         hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1164         ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1165     }
1166     else
1167     {
1168         skip("Volume textures not supported.\n");
1169     }
1170
1171     /* Scratch, sysmem and managed pools are fine */
1172     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device1, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SCRATCH, &surface, NULL);
1173     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
1174     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1175     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1176     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1177     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1178     IDirect3DSurface9_Release(surface);
1179
1180     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device1, 16, 16,
1181             D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface, NULL);
1182     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
1183     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1184     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1185     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1186     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1187     IDirect3DSurface9_Release(surface);
1188
1189     /* The depth stencil should get reset to the auto depth stencil when present. */
1190     hr = IDirect3DDevice9_SetDepthStencilSurface(device1, NULL);
1191     ok(hr == D3D_OK, "SetDepthStencilSurface failed with 0x%08x\n", hr);
1192
1193     hr = IDirect3DDevice9_GetDepthStencilSurface(device1, &surface);
1194     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1195     ok(surface == NULL, "Depth stencil should be NULL\n");
1196
1197     d3dpp.EnableAutoDepthStencil = TRUE;
1198     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
1199     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1200     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with 0x%08x\n", hr);
1201
1202     hr = IDirect3DDevice9_GetDepthStencilSurface(device1, &surface);
1203     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1204     ok(surface != NULL, "Depth stencil should not be NULL\n");
1205     if (surface) IDirect3DSurface9_Release(surface);
1206
1207     d3dpp.EnableAutoDepthStencil = FALSE;
1208     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1209     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with 0x%08x\n", hr);
1210
1211     hr = IDirect3DDevice9_GetDepthStencilSurface(device1, &surface);
1212     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1213     ok(surface == NULL, "Depth stencil should be NULL\n");
1214
1215     /* Will a sysmem or scratch survive while locked */
1216     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device1, 16, 16,
1217             D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface, NULL);
1218     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
1219     hr = IDirect3DSurface9_LockRect(surface, &lockrect, NULL, D3DLOCK_DISCARD);
1220     ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned %08x\n", hr);
1221     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1222     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1223     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1224     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1225     IDirect3DSurface9_UnlockRect(surface);
1226     IDirect3DSurface9_Release(surface);
1227
1228     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device1, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SCRATCH, &surface, NULL);
1229     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
1230     hr = IDirect3DSurface9_LockRect(surface, &lockrect, NULL, D3DLOCK_DISCARD);
1231     ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned %08x\n", hr);
1232     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1233     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1234     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1235     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1236     IDirect3DSurface9_UnlockRect(surface);
1237     IDirect3DSurface9_Release(surface);
1238
1239     hr = IDirect3DDevice9_CreateTexture(device1, 16, 16, 0, 0, D3DFMT_R5G6B5, D3DPOOL_MANAGED, &texture, NULL);
1240     ok(hr == D3D_OK, "IDirect3DDevice9_CreateTexture returned %08x\n", hr);
1241     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1242     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1243     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1244     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1245     IDirect3DTexture9_Release(texture);
1246
1247     /* A reference held to an implicit surface causes failures as well */
1248     hr = IDirect3DDevice9_GetBackBuffer(device1, 0, 0, D3DBACKBUFFER_TYPE_MONO, &surface);
1249     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer returned %08x\n", hr);
1250     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1251     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1252     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1253     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
1254     IDirect3DSurface9_Release(surface);
1255     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1256     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1257     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1258     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1259
1260     /* Shaders are fine as well */
1261     hr = IDirect3DDevice9_CreateVertexShader(device1, simple_vs, &shader);
1262     ok(hr == D3D_OK, "IDirect3DDevice9_CreateVertexShader returned %08x\n", hr);
1263     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1264     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1265     IDirect3DVertexShader9_Release(shader);
1266
1267     /* Try setting invalid modes */
1268     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1269     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1270     d3dpp.Windowed         = FALSE;
1271     d3dpp.BackBufferWidth  = 32;
1272     d3dpp.BackBufferHeight = 32;
1273     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1274     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset to w=32, h=32, windowed=FALSE failed with %08x\n", hr);
1275     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1276     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
1277
1278     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1279     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1280     d3dpp.Windowed         = FALSE;
1281     d3dpp.BackBufferWidth  = 801;
1282     d3dpp.BackBufferHeight = 600;
1283     hr = IDirect3DDevice9_Reset(device1, &d3dpp);
1284     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset to w=801, h=600, windowed=FALSE failed with %08x\n", hr);
1285     hr = IDirect3DDevice9_TestCooperativeLevel(device1);
1286     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
1287
1288     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1289
1290     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1291     d3dpp.Windowed         = TRUE;
1292     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1293     d3dpp.BackBufferFormat = d3ddm.Format;
1294     d3dpp.EnableAutoDepthStencil = FALSE;
1295     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
1296
1297     hr = IDirect3D9_CreateDevice(pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
1298             hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device2);
1299     if (FAILED(hr))
1300     {
1301         skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
1302         goto cleanup;
1303     }
1304
1305     hr = IDirect3DDevice9_TestCooperativeLevel(device2);
1306     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after creation returned %#x\n", hr);
1307
1308     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1309     d3dpp.Windowed         = TRUE;
1310     d3dpp.BackBufferWidth  = 400;
1311     d3dpp.BackBufferHeight = 300;
1312     d3dpp.EnableAutoDepthStencil = TRUE;
1313     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
1314
1315     hr = IDirect3DDevice9_Reset(device2, &d3dpp);
1316     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with 0x%08x\n", hr);
1317
1318     if (FAILED(hr)) goto cleanup;
1319
1320     hr = IDirect3DDevice9_GetDepthStencilSurface(device2, &surface);
1321     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1322     ok(surface != NULL, "Depth stencil should not be NULL\n");
1323     if (surface) IDirect3DSurface9_Release(surface);
1324
1325 cleanup:
1326     HeapFree(GetProcessHeap(), 0, modes);
1327     if (device2)
1328     {
1329         UINT refcount = IDirect3DDevice9_Release(device2);
1330         ok(!refcount, "Device has %u references left.\n", refcount);
1331     }
1332     if (device1)
1333     {
1334         UINT refcount = IDirect3DDevice9_Release(device1);
1335         ok(!refcount, "Device has %u references left.\n", refcount);
1336     }
1337     if (pD3d) IDirect3D9_Release(pD3d);
1338     if (hwnd) DestroyWindow(hwnd);
1339 }
1340
1341 /* Test adapter display modes */
1342 static void test_display_modes(void)
1343 {
1344     D3DDISPLAYMODE dmode;
1345     IDirect3D9 *pD3d;
1346
1347     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1348     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1349     if(!pD3d) return;
1350
1351 #define TEST_FMT(x,r) do { \
1352     HRESULT res = IDirect3D9_EnumAdapterModes(pD3d, 0, (x), 0, &dmode); \
1353     ok(res==(r), "EnumAdapterModes("#x") did not return "#r" (got %08x)!\n", res); \
1354 } while(0)
1355
1356     TEST_FMT(D3DFMT_R8G8B8, D3DERR_INVALIDCALL);
1357     TEST_FMT(D3DFMT_A8R8G8B8, D3DERR_INVALIDCALL);
1358     TEST_FMT(D3DFMT_X8B8G8R8, D3DERR_INVALIDCALL);
1359     /* D3DFMT_R5G6B5 */
1360     TEST_FMT(D3DFMT_X1R5G5B5, D3DERR_INVALIDCALL);
1361     TEST_FMT(D3DFMT_A1R5G5B5, D3DERR_INVALIDCALL);
1362     TEST_FMT(D3DFMT_A4R4G4B4, D3DERR_INVALIDCALL);
1363     TEST_FMT(D3DFMT_R3G3B2, D3DERR_INVALIDCALL);
1364     TEST_FMT(D3DFMT_A8, D3DERR_INVALIDCALL);
1365     TEST_FMT(D3DFMT_A8R3G3B2, D3DERR_INVALIDCALL);
1366     TEST_FMT(D3DFMT_X4R4G4B4, D3DERR_INVALIDCALL);
1367     TEST_FMT(D3DFMT_A2B10G10R10, D3DERR_INVALIDCALL);
1368     TEST_FMT(D3DFMT_A8B8G8R8, D3DERR_INVALIDCALL);
1369     TEST_FMT(D3DFMT_X8B8G8R8, D3DERR_INVALIDCALL);
1370     TEST_FMT(D3DFMT_G16R16, D3DERR_INVALIDCALL);
1371     TEST_FMT(D3DFMT_A16B16G16R16, D3DERR_INVALIDCALL);
1372
1373     TEST_FMT(D3DFMT_A8P8, D3DERR_INVALIDCALL);
1374     TEST_FMT(D3DFMT_P8, D3DERR_INVALIDCALL);
1375
1376     TEST_FMT(D3DFMT_L8, D3DERR_INVALIDCALL);
1377     TEST_FMT(D3DFMT_A8L8, D3DERR_INVALIDCALL);
1378     TEST_FMT(D3DFMT_A4L4, D3DERR_INVALIDCALL);
1379
1380     TEST_FMT(D3DFMT_V8U8, D3DERR_INVALIDCALL);
1381     TEST_FMT(D3DFMT_L6V5U5, D3DERR_INVALIDCALL);
1382     TEST_FMT(D3DFMT_X8L8V8U8, D3DERR_INVALIDCALL);
1383     TEST_FMT(D3DFMT_Q8W8V8U8, D3DERR_INVALIDCALL);
1384     TEST_FMT(D3DFMT_V16U16, D3DERR_INVALIDCALL);
1385     TEST_FMT(D3DFMT_A2W10V10U10, D3DERR_INVALIDCALL);
1386
1387     TEST_FMT(D3DFMT_UYVY, D3DERR_INVALIDCALL);
1388     TEST_FMT(D3DFMT_YUY2, D3DERR_INVALIDCALL);
1389     TEST_FMT(D3DFMT_DXT1, D3DERR_INVALIDCALL);
1390     TEST_FMT(D3DFMT_DXT2, D3DERR_INVALIDCALL);
1391     TEST_FMT(D3DFMT_DXT3, D3DERR_INVALIDCALL);
1392     TEST_FMT(D3DFMT_DXT4, D3DERR_INVALIDCALL);
1393     TEST_FMT(D3DFMT_DXT5, D3DERR_INVALIDCALL);
1394     TEST_FMT(D3DFMT_MULTI2_ARGB8, D3DERR_INVALIDCALL);
1395     TEST_FMT(D3DFMT_G8R8_G8B8, D3DERR_INVALIDCALL);
1396     TEST_FMT(D3DFMT_R8G8_B8G8, D3DERR_INVALIDCALL);
1397
1398     TEST_FMT(D3DFMT_D16_LOCKABLE, D3DERR_INVALIDCALL);
1399     TEST_FMT(D3DFMT_D32, D3DERR_INVALIDCALL);
1400     TEST_FMT(D3DFMT_D15S1, D3DERR_INVALIDCALL);
1401     TEST_FMT(D3DFMT_D24S8, D3DERR_INVALIDCALL);
1402     TEST_FMT(D3DFMT_D24X8, D3DERR_INVALIDCALL);
1403     TEST_FMT(D3DFMT_D24X4S4, D3DERR_INVALIDCALL);
1404     TEST_FMT(D3DFMT_D16, D3DERR_INVALIDCALL);
1405     TEST_FMT(D3DFMT_L16, D3DERR_INVALIDCALL);
1406     TEST_FMT(D3DFMT_D32F_LOCKABLE, D3DERR_INVALIDCALL);
1407     TEST_FMT(D3DFMT_D24FS8, D3DERR_INVALIDCALL);
1408
1409     TEST_FMT(D3DFMT_VERTEXDATA, D3DERR_INVALIDCALL);
1410     TEST_FMT(D3DFMT_INDEX16, D3DERR_INVALIDCALL);
1411     TEST_FMT(D3DFMT_INDEX32, D3DERR_INVALIDCALL);
1412     TEST_FMT(D3DFMT_Q16W16V16U16, D3DERR_INVALIDCALL);
1413     /* Floating point formats */
1414     TEST_FMT(D3DFMT_R16F, D3DERR_INVALIDCALL);
1415     TEST_FMT(D3DFMT_G16R16F, D3DERR_INVALIDCALL);
1416     TEST_FMT(D3DFMT_A16B16G16R16F, D3DERR_INVALIDCALL);
1417
1418     /* IEEE formats */
1419     TEST_FMT(D3DFMT_R32F, D3DERR_INVALIDCALL);
1420     TEST_FMT(D3DFMT_G32R32F, D3DERR_INVALIDCALL);
1421     TEST_FMT(D3DFMT_A32B32G32R32F, D3DERR_INVALIDCALL);
1422
1423     TEST_FMT(D3DFMT_CxV8U8, D3DERR_INVALIDCALL);
1424
1425     TEST_FMT(0, D3DERR_INVALIDCALL);
1426
1427     IDirect3D9_Release(pD3d);
1428 }
1429
1430 static void test_scene(void)
1431 {
1432     HRESULT                      hr;
1433     HWND                         hwnd               = NULL;
1434     IDirect3D9                  *pD3d               = NULL;
1435     IDirect3DDevice9            *pDevice            = NULL;
1436     D3DPRESENT_PARAMETERS        d3dpp;
1437     D3DDISPLAYMODE               d3ddm;
1438     IDirect3DSurface9            *pSurface1 = NULL, *pSurface2 = NULL, *pSurface3 = NULL, *pRenderTarget = NULL;
1439     IDirect3DSurface9            *pBackBuffer = NULL, *pDepthStencil = NULL;
1440     RECT rect = {0, 0, 128, 128};
1441     D3DCAPS9                     caps;
1442
1443     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1444     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1445     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1446     ok(hwnd != NULL, "Failed to create window\n");
1447     if (!pD3d || !hwnd) goto cleanup;
1448
1449     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1450     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1451     d3dpp.Windowed         = TRUE;
1452     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1453     d3dpp.BackBufferWidth  = 800;
1454     d3dpp.BackBufferHeight = 600;
1455     d3dpp.BackBufferFormat = d3ddm.Format;
1456     d3dpp.EnableAutoDepthStencil = TRUE;
1457     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1458
1459     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1460                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1461     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1462     if(!pDevice)
1463     {
1464         skip("Failed to create a d3d device\n");
1465         goto cleanup;
1466     }
1467
1468     /* Get the caps, they will be needed to tell if an operation is supposed to be valid */
1469     memset(&caps, 0, sizeof(caps));
1470     hr = IDirect3DDevice9_GetDeviceCaps(pDevice, &caps);
1471     ok(hr == D3D_OK, "IDirect3DDevice9_GetCaps failed with %08x\n", hr);
1472     if(FAILED(hr)) goto cleanup;
1473
1474     /* Test an EndScene without BeginScene. Should return an error */
1475     hr = IDirect3DDevice9_EndScene(pDevice);
1476     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %08x\n", hr);
1477
1478     /* Test a normal BeginScene / EndScene pair, this should work */
1479     hr = IDirect3DDevice9_BeginScene(pDevice);
1480     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1481     if(SUCCEEDED(hr))
1482     {
1483         hr = IDirect3DDevice9_EndScene(pDevice);
1484         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1485     }
1486
1487     /* Test another EndScene without having begun a new scene. Should return an error */
1488     hr = IDirect3DDevice9_EndScene(pDevice);
1489     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %08x\n", hr);
1490
1491     /* Two nested BeginScene and EndScene calls */
1492     hr = IDirect3DDevice9_BeginScene(pDevice);
1493     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1494     hr = IDirect3DDevice9_BeginScene(pDevice);
1495     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_BeginScene returned %08x\n", hr);
1496     hr = IDirect3DDevice9_EndScene(pDevice);
1497     ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1498     hr = IDirect3DDevice9_EndScene(pDevice);
1499     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %08x\n", hr);
1500
1501     /* Create some surfaces to test stretchrect between the scenes */
1502     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface1, NULL);
1503     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %08x\n", hr);
1504     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface2, NULL);
1505     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %08x\n", hr);
1506     hr = IDirect3DDevice9_CreateDepthStencilSurface(pDevice, 800, 600, D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, FALSE, &pSurface3, NULL);
1507     ok(hr == D3D_OK, "IDirect3DDevice9_CreateDepthStencilSurface failed with %08x\n", hr);
1508     hr = IDirect3DDevice9_CreateRenderTarget(pDevice, 128, 128, d3ddm.Format, D3DMULTISAMPLE_NONE, 0, FALSE, &pRenderTarget, NULL);
1509     ok(hr == D3D_OK, "IDirect3DDevice9_CreateRenderTarget failed with %08x\n", hr);
1510
1511     hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
1512     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer failed with %08x\n", hr);
1513     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1514     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer failed with %08x\n", hr);
1515
1516     /* First make sure a simple StretchRect call works */
1517     if(pSurface1 && pSurface2) {
1518         hr = IDirect3DDevice9_StretchRect(pDevice, pSurface1, NULL, pSurface2, NULL, 0);
1519         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1520     }
1521     if(pBackBuffer && pRenderTarget) {
1522         hr = IDirect3DDevice9_StretchRect(pDevice, pBackBuffer, &rect, pRenderTarget, NULL, 0);
1523         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1524     }
1525     if(pDepthStencil && pSurface3) {
1526         HRESULT expected;
1527         if(0) /* Disabled for now because it crashes in wine */ {
1528             expected = caps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES ? D3D_OK : D3DERR_INVALIDCALL;
1529             hr = IDirect3DDevice9_StretchRect(pDevice, pDepthStencil, NULL, pSurface3, NULL, 0);
1530             ok( hr == expected, "IDirect3DDevice9_StretchRect returned %08x, expected %08x\n", hr, expected);
1531         }
1532     }
1533
1534     /* Now try it in a BeginScene - EndScene pair. Seems to be allowed in a beginScene - Endscene pair
1535      * with normal surfaces and render targets, but not depth stencil surfaces.
1536      */
1537     hr = IDirect3DDevice9_BeginScene(pDevice);
1538     ok( hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1539
1540     if(pSurface1 && pSurface2)
1541     {
1542         hr = IDirect3DDevice9_StretchRect(pDevice, pSurface1, NULL, pSurface2, NULL, 0);
1543         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1544     }
1545     if(pBackBuffer && pRenderTarget)
1546     {
1547         hr = IDirect3DDevice9_StretchRect(pDevice, pBackBuffer, &rect, pRenderTarget, NULL, 0);
1548         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1549     }
1550     if(pDepthStencil && pSurface3)
1551     {
1552         /* This is supposed to fail inside a BeginScene - EndScene pair. */
1553         hr = IDirect3DDevice9_StretchRect(pDevice, pDepthStencil, NULL, pSurface3, NULL, 0);
1554         ok( hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_StretchRect returned %08x, expected D3DERR_INVALIDCALL\n", hr);
1555     }
1556
1557     hr = IDirect3DDevice9_EndScene(pDevice);
1558     ok( hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1559
1560     /* Does a SetRenderTarget influence BeginScene / EndScene ?
1561      * Set a new render target, then see if it started a new scene. Flip the rt back and see if that maybe
1562      * ended the scene. Expected result is that the scene is not affected by SetRenderTarget
1563      */
1564     hr = IDirect3DDevice9_SetRenderTarget(pDevice, 0, pRenderTarget);
1565     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget failed with %08x\n", hr);
1566     hr = IDirect3DDevice9_BeginScene(pDevice);
1567     ok( hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1568     hr = IDirect3DDevice9_SetRenderTarget(pDevice, 0, pBackBuffer);
1569     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget failed with %08x\n", hr);
1570     hr = IDirect3DDevice9_EndScene(pDevice);
1571     ok( hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1572
1573 cleanup:
1574     if(pRenderTarget) IDirect3DSurface9_Release(pRenderTarget);
1575     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1576     if(pBackBuffer) IDirect3DSurface9_Release(pBackBuffer);
1577     if(pSurface1) IDirect3DSurface9_Release(pSurface1);
1578     if(pSurface2) IDirect3DSurface9_Release(pSurface2);
1579     if(pSurface3) IDirect3DSurface9_Release(pSurface3);
1580     if (pDevice)
1581     {
1582         UINT refcount = IDirect3DDevice9_Release(pDevice);
1583         ok(!refcount, "Device has %u references left.\n", refcount);
1584     }
1585     if (pD3d) IDirect3D9_Release(pD3d);
1586     if(hwnd) DestroyWindow(hwnd);
1587 }
1588
1589 static void test_limits(void)
1590 {
1591     HRESULT                      hr;
1592     HWND                         hwnd               = NULL;
1593     IDirect3D9                  *pD3d               = NULL;
1594     IDirect3DDevice9            *pDevice            = NULL;
1595     D3DPRESENT_PARAMETERS        d3dpp;
1596     D3DDISPLAYMODE               d3ddm;
1597     IDirect3DTexture9           *pTexture           = NULL;
1598     int i;
1599
1600     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1601     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1602     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1603     ok(hwnd != NULL, "Failed to create window\n");
1604     if (!pD3d || !hwnd) goto cleanup;
1605
1606     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1607     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1608     d3dpp.Windowed         = TRUE;
1609     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1610     d3dpp.BackBufferWidth  = 800;
1611     d3dpp.BackBufferHeight = 600;
1612     d3dpp.BackBufferFormat = d3ddm.Format;
1613     d3dpp.EnableAutoDepthStencil = TRUE;
1614     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1615
1616     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1617                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1618     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1619     if(!pDevice)
1620     {
1621         skip("Failed to create a d3d device\n");
1622         goto cleanup;
1623     }
1624
1625     hr = IDirect3DDevice9_CreateTexture(pDevice, 16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture, NULL);
1626     ok(hr == D3D_OK, "IDirect3DDevice9_CreateTexture failed with %08x\n", hr);
1627     if(!pTexture) goto cleanup;
1628
1629     /* There are 16 pixel samplers. We should be able to access all of them */
1630     for(i = 0; i < 16; i++) {
1631         hr = IDirect3DDevice9_SetTexture(pDevice, i, (IDirect3DBaseTexture9 *) pTexture);
1632         ok(hr == D3D_OK, "IDirect3DDevice9_SetTexture for sampler %d failed with %08x\n", i, hr);
1633         hr = IDirect3DDevice9_SetTexture(pDevice, i, NULL);
1634         ok(hr == D3D_OK, "IDirect3DDevice9_SetTexture for sampler %d failed with %08x\n", i, hr);
1635         hr = IDirect3DDevice9_SetSamplerState(pDevice, i, D3DSAMP_SRGBTEXTURE, TRUE);
1636         ok(hr == D3D_OK, "IDirect3DDevice9_SetSamplerState for sampler %d failed with %08x\n", i, hr);
1637     }
1638
1639     /* Now test all 8 textures stage states */
1640     for(i = 0; i < 8; i++) {
1641         hr = IDirect3DDevice9_SetTextureStageState(pDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
1642         ok(hr == D3D_OK, "IDirect3DDevice9_SetTextureStageState for texture %d failed with %08x\n", i, hr);
1643     }
1644
1645     /* Investigations show that accessing higher samplers / textures stage states does not return an error either. Writing
1646      * to too high samplers(approximately sampler 40) causes memory corruption in windows, so there is no bounds checking
1647      * but how do I test that?
1648      */
1649 cleanup:
1650     if(pTexture) IDirect3DTexture9_Release(pTexture);
1651     if (pDevice)
1652     {
1653         UINT refcount = IDirect3D9_Release(pDevice);
1654         ok(!refcount, "Device has %u references left.\n", refcount);
1655     }
1656     if (pD3d) IDirect3D9_Release(pD3d);
1657     if(hwnd) DestroyWindow(hwnd);
1658 }
1659
1660 static void test_depthstenciltest(void)
1661 {
1662     HRESULT                      hr;
1663     HWND                         hwnd               = NULL;
1664     IDirect3D9                  *pD3d               = NULL;
1665     IDirect3DDevice9            *pDevice            = NULL;
1666     D3DPRESENT_PARAMETERS        d3dpp;
1667     D3DDISPLAYMODE               d3ddm;
1668     IDirect3DSurface9           *pDepthStencil           = NULL;
1669     IDirect3DSurface9           *pDepthStencil2          = NULL;
1670     DWORD                        state;
1671
1672     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1673     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1674     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1675     ok(hwnd != NULL, "Failed to create window\n");
1676     if (!pD3d || !hwnd) goto cleanup;
1677
1678     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1679     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1680     d3dpp.Windowed         = TRUE;
1681     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1682     d3dpp.BackBufferWidth  = 800;
1683     d3dpp.BackBufferHeight = 600;
1684     d3dpp.BackBufferFormat = d3ddm.Format;
1685     d3dpp.EnableAutoDepthStencil = TRUE;
1686     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1687
1688     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1689                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1690     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1691     if(!pDevice)
1692     {
1693         skip("Failed to create a d3d device\n");
1694         goto cleanup;
1695     }
1696
1697     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1698     ok(hr == D3D_OK && pDepthStencil != NULL, "IDirect3DDevice9_GetDepthStencilSurface failed with %08x\n", hr);
1699
1700     /* Try to clear */
1701     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1702     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %08x\n", hr);
1703
1704     hr = IDirect3DDevice9_SetDepthStencilSurface(pDevice, NULL);
1705     ok(hr == D3D_OK, "IDirect3DDevice9_SetDepthStencilSurface failed with %08x\n", hr);
1706
1707     /* Check if the set buffer is returned on a get. WineD3D had a bug with that once, prevent it from coming back */
1708     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil2);
1709     ok(hr == D3DERR_NOTFOUND && pDepthStencil2 == NULL, "IDirect3DDevice9_GetDepthStencilSurface failed with %08x\n", hr);
1710     if(pDepthStencil2) IDirect3DSurface9_Release(pDepthStencil2);
1711
1712     /* This left the render states untouched! */
1713     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1714     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1715     ok(state == D3DZB_TRUE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1716     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZWRITEENABLE, &state);
1717     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1718     ok(state == TRUE, "D3DRS_ZWRITEENABLE is %s\n", state ? "TRUE" : "FALSE");
1719     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_STENCILENABLE, &state);
1720     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1721     ok(state == FALSE, "D3DRS_STENCILENABLE is %s\n", state ? "TRUE" : "FALSE");
1722     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_STENCILWRITEMASK, &state);
1723     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1724     ok(state == 0xffffffff, "D3DRS_STENCILWRITEMASK is 0x%08x\n", state);
1725
1726     /* This is supposed to fail now */
1727     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1728     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Clear failed with %08x\n", hr);
1729
1730     hr = IDirect3DDevice9_SetRenderState(pDevice, D3DRS_ZENABLE, D3DZB_FALSE);
1731     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState failed with %08x\n", hr);
1732
1733     hr = IDirect3DDevice9_SetDepthStencilSurface(pDevice, pDepthStencil);
1734     ok(hr == D3D_OK, "IDirect3DDevice9_SetDepthStencilSurface failed with %08x\n", hr);
1735
1736     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1737     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1738     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1739
1740     /* Now it works again */
1741     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1742     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %08x\n", hr);
1743
1744     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1745     if(pDevice) IDirect3D9_Release(pDevice);
1746
1747     /* Now see if autodepthstencil disable is honored. First, without a format set */
1748     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1749     d3dpp.Windowed         = TRUE;
1750     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1751     d3dpp.BackBufferWidth  = 800;
1752     d3dpp.BackBufferHeight = 600;
1753     d3dpp.BackBufferFormat = d3ddm.Format;
1754     d3dpp.EnableAutoDepthStencil = FALSE;
1755     d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
1756
1757     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1758                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1759     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1760     if(!pDevice)
1761     {
1762         skip("Failed to create a d3d device\n");
1763         goto cleanup;
1764     }
1765
1766     pDepthStencil = NULL;
1767     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1768     ok(hr == D3DERR_NOTFOUND && pDepthStencil == NULL, "IDirect3DDevice9_GetDepthStencilSurface returned %08x, surface = %p\n", hr, pDepthStencil);
1769     if(pDepthStencil) {
1770         IDirect3DSurface9_Release(pDepthStencil);
1771         pDepthStencil = NULL;
1772     }
1773
1774     /* Check the depth test state */
1775     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1776     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1777     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1778
1779     if(pDevice) IDirect3D9_Release(pDevice);
1780
1781     /* Next, try EnableAutoDepthStencil FALSE with a depth stencil format set */
1782     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1783     d3dpp.Windowed         = TRUE;
1784     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1785     d3dpp.BackBufferWidth  = 800;
1786     d3dpp.BackBufferHeight = 600;
1787     d3dpp.BackBufferFormat = d3ddm.Format;
1788     d3dpp.EnableAutoDepthStencil = FALSE;
1789     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1790
1791     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1792                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1793     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1794     if(!pDevice)
1795     {
1796         skip("Failed to create a d3d device\n");
1797         goto cleanup;
1798     }
1799
1800     pDepthStencil = NULL;
1801     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1802     ok(hr == D3DERR_NOTFOUND && pDepthStencil == NULL, "IDirect3DDevice9_GetDepthStencilSurface returned %08x, surface = %p\n", hr, pDepthStencil);
1803     if(pDepthStencil) {
1804         IDirect3DSurface9_Release(pDepthStencil);
1805         pDepthStencil = NULL;
1806     }
1807
1808     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1809     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1810     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1811
1812 cleanup:
1813     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1814     if (pDevice)
1815     {
1816         UINT refcount = IDirect3D9_Release(pDevice);
1817         ok(!refcount, "Device has %u references left.\n", refcount);
1818     }
1819     if (pD3d) IDirect3D9_Release(pD3d);
1820     if(hwnd) DestroyWindow(hwnd);
1821 }
1822
1823 static void test_get_rt(void)
1824 {
1825     IDirect3DSurface9 *backbuffer, *rt;
1826     IDirect3DDevice9 *device;
1827     IDirect3D9 *d3d9;
1828     D3DCAPS9 caps;
1829     HWND window;
1830     HRESULT hr;
1831     ULONG ref;
1832     UINT i;
1833
1834     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
1835     {
1836         skip("Failed to create IDirect3D9 object, skipping tests.\n");
1837         return;
1838     }
1839
1840     window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
1841             0, 0, 128, 128, 0, 0, 0, 0);
1842     device = create_device(d3d9, window, window, TRUE);
1843     if (!device)
1844     {
1845         skip("Failed to create a D3D device, skipping tests.\n");
1846         goto done;
1847     }
1848
1849     hr = IDirect3DDevice9_GetRenderTarget(device, 0, &backbuffer);
1850     ok(SUCCEEDED(hr), "Failed to get backbuffer, hr %#x.\n", hr);
1851     ok(!!backbuffer, "Got a NULL backbuffer.\n");
1852
1853     hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1854     ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
1855
1856     for (i = 1; i < caps.NumSimultaneousRTs; ++i)
1857     {
1858         rt = backbuffer;
1859         hr = IDirect3DDevice9_GetRenderTarget(device, i, &rt);
1860         ok(hr == D3DERR_NOTFOUND, "IDirect3DDevice9_GetRenderTarget returned %#x.\n", hr);
1861         ok(!rt, "Got rt %p.\n", rt);
1862     }
1863
1864     IDirect3DSurface9_Release(backbuffer);
1865
1866     ref = IDirect3DDevice9_Release(device);
1867     ok(!ref, "The device was not properly freed: refcount %u.\n", ref);
1868 done:
1869     IDirect3D9_Release(d3d9);
1870     DestroyWindow(window);
1871 }
1872
1873 /* Test what happens when IDirect3DDevice9_DrawIndexedPrimitive is called without a valid index buffer set. */
1874 static void test_draw_indexed(void)
1875 {
1876     static const struct {
1877         float position[3];
1878         DWORD color;
1879     } quad[] = {
1880         {{-1.0f, -1.0f, 0.0f}, 0xffff0000},
1881         {{-1.0f,  1.0f, 0.0f}, 0xffff0000},
1882         {{ 1.0f,  1.0f, 0.0f}, 0xffff0000},
1883         {{ 1.0f, -1.0f, 0.0f}, 0xffff0000},
1884     };
1885     WORD indices[] = {0, 1, 2, 3, 0, 2};
1886
1887     static const D3DVERTEXELEMENT9 decl_elements[] = {
1888         {0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1889         {0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,    D3DDECLUSAGE_COLOR, 0},
1890         D3DDECL_END()
1891     };
1892
1893     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
1894     IDirect3DVertexBuffer9 *vertex_buffer = NULL;
1895     IDirect3DIndexBuffer9 *index_buffer = NULL;
1896     D3DPRESENT_PARAMETERS present_parameters;
1897     IDirect3DDevice9 *device = NULL;
1898     IDirect3D9 *d3d9;
1899     HRESULT hr;
1900     HWND hwnd;
1901     void *ptr;
1902
1903     hwnd = CreateWindow("d3d9_test_wc", "d3d9_test",
1904             0, 0, 0, 10, 10, 0, 0, 0, 0);
1905     if (!hwnd)
1906     {
1907         skip("Failed to create window\n");
1908         return;
1909     }
1910
1911     d3d9 = pDirect3DCreate9(D3D_SDK_VERSION);
1912     if (!d3d9)
1913     {
1914         skip("Failed to create IDirect3D9 object\n");
1915         goto cleanup;
1916     }
1917
1918     ZeroMemory(&present_parameters, sizeof(present_parameters));
1919     present_parameters.Windowed = TRUE;
1920     present_parameters.hDeviceWindow = hwnd;
1921     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1922
1923     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
1924             NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device);
1925     if (FAILED(hr) || !device)
1926     {
1927         skip("Failed to create device\n");
1928         goto cleanup;
1929     }
1930
1931     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
1932     ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (0x%08x)\n", hr);
1933     hr = IDirect3DDevice9_SetVertexDeclaration(device, NULL);
1934     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
1935
1936     hr = IDirect3DDevice9_CreateVertexBuffer(device, sizeof(quad), 0, 0, D3DPOOL_DEFAULT, &vertex_buffer, NULL);
1937     ok(SUCCEEDED(hr), "CreateVertexBuffer failed (0x%08x)\n", hr);
1938     hr = IDirect3DVertexBuffer9_Lock(vertex_buffer, 0, 0, &ptr, D3DLOCK_DISCARD);
1939     ok(SUCCEEDED(hr), "Lock failed (0x%08x)\n", hr);
1940     memcpy(ptr, quad, sizeof(quad));
1941     hr = IDirect3DVertexBuffer9_Unlock(vertex_buffer);
1942     ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1943     hr = IDirect3DDevice9_SetStreamSource(device, 0, vertex_buffer, 0, sizeof(*quad));
1944     ok(SUCCEEDED(hr), "SetStreamSource failed (0x%08x)\n", hr);
1945
1946     hr = IDirect3DDevice9_CreateIndexBuffer(device, sizeof(indices), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &index_buffer, NULL);
1947     ok(SUCCEEDED(hr), "CreateIndexBuffer failed (0x%08x)\n", hr);
1948     hr = IDirect3DIndexBuffer9_Lock(index_buffer, 0, 0, &ptr, D3DLOCK_DISCARD);
1949     ok(SUCCEEDED(hr), "Lock failed (0x%08x)\n", hr);
1950     memcpy(ptr, indices, sizeof(indices));
1951     hr = IDirect3DIndexBuffer9_Unlock(index_buffer);
1952     ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1953     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1954     ok(SUCCEEDED(hr), "SetRenderState D3DRS_LIGHTING failed (0x%08x)\n", hr);
1955     hr = IDirect3DDevice9_BeginScene(device);
1956     ok(SUCCEEDED(hr), "BeginScene failed (0x%08x)\n", hr);
1957
1958     /* NULL index buffer. Should fail */
1959     hr = IDirect3DDevice9_SetIndices(device, NULL);
1960     ok(SUCCEEDED(hr), "SetIndices failed (0x%08x)\n", hr);
1961     hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1962             4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1963     ok(hr == D3DERR_INVALIDCALL, "DrawIndexedPrimitive returned 0x%08x, expected D3DERR_INVALIDCALL (0x%08x)\n",
1964             hr, D3DERR_INVALIDCALL);
1965
1966     /* Valid index buffer, NULL vertex declaration. Should fail */
1967     hr = IDirect3DDevice9_SetIndices(device, index_buffer);
1968     ok(SUCCEEDED(hr), "SetIndices failed (0x%08x)\n", hr);
1969     hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1970             4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1971     ok(hr == D3DERR_INVALIDCALL, "DrawIndexedPrimitive returned 0x%08x, expected D3DERR_INVALIDCALL (0x%08x)\n",
1972             hr, D3DERR_INVALIDCALL);
1973
1974     /* Valid index buffer and vertex declaration. Should succeed */
1975     hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
1976     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
1977     hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1978             4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1979     ok(SUCCEEDED(hr), "DrawIndexedPrimitive failed (0x%08x)\n", hr);
1980
1981     hr = IDirect3DDevice9_EndScene(device);
1982     ok(SUCCEEDED(hr), "EndScene failed (0x%08x)\n", hr);
1983
1984     hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
1985     ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1986
1987     IDirect3DVertexBuffer9_Release(vertex_buffer);
1988     IDirect3DIndexBuffer9_Release(index_buffer);
1989     IDirect3DVertexDeclaration9_Release(vertex_declaration);
1990
1991 cleanup:
1992     if (device)
1993     {
1994         UINT refcount = IDirect3DDevice9_Release(device);
1995         ok(!refcount, "Device has %u references left.\n", refcount);
1996     }
1997     if (d3d9) IDirect3D9_Release(d3d9);
1998     if (hwnd) DestroyWindow(hwnd);
1999 }
2000
2001 static void test_null_stream(void)
2002 {
2003     IDirect3DVertexBuffer9 *buffer = NULL;
2004     D3DPRESENT_PARAMETERS present_parameters;
2005     IDirect3DDevice9 *device = NULL;
2006     IDirect3D9 *d3d9;
2007     HWND hwnd;
2008     HRESULT hr;
2009     IDirect3DVertexShader9 *shader = NULL;
2010     IDirect3DVertexDeclaration9 *decl = NULL;
2011     DWORD shader_code[] = {
2012         0xfffe0101,                             /* vs_1_1           */
2013         0x0000001f, 0x80000000, 0x900f0000,     /* dcl_position v0  */
2014         0x00000001, 0xc00f0000, 0x90e40000,     /* mov oPos, v0     */
2015         0x0000ffff                              /* end              */
2016     };
2017     static const D3DVERTEXELEMENT9 decl_elements[] = {
2018         {0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
2019         {1, 0,  D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,    D3DDECLUSAGE_COLOR, 0},
2020         D3DDECL_END()
2021     };
2022
2023     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
2024     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
2025     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
2026     ok(hwnd != NULL, "Failed to create window\n");
2027     if (!d3d9 || !hwnd) goto cleanup;
2028
2029     ZeroMemory(&present_parameters, sizeof(present_parameters));
2030     present_parameters.Windowed = TRUE;
2031     present_parameters.hDeviceWindow = hwnd;
2032     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2033
2034     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
2035                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device );
2036     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
2037     if(!device)
2038     {
2039         skip("Failed to create a d3d device\n");
2040         goto cleanup;
2041     }
2042
2043     hr = IDirect3DDevice9_CreateVertexShader(device, shader_code, &shader);
2044     if(FAILED(hr)) {
2045         skip("No vertex shader support\n");
2046         goto cleanup;
2047     }
2048     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &decl);
2049     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexDeclaration failed (0x%08x)\n", hr);
2050     if (FAILED(hr)) {
2051         skip("Vertex declaration handling not possible.\n");
2052         goto cleanup;
2053     }
2054     hr = IDirect3DDevice9_CreateVertexBuffer(device, 12 * sizeof(float), 0, 0, D3DPOOL_MANAGED, &buffer, NULL);
2055     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexBuffer failed (0x%08x)\n", hr);
2056     if (FAILED(hr)) {
2057         skip("Vertex buffer handling not possible.\n");
2058         goto cleanup;
2059     }
2060
2061     hr = IDirect3DDevice9_SetStreamSource(device, 0, buffer, 0, sizeof(float) * 3);
2062     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetStreamSource failed (0x%08x)\n", hr);
2063     hr = IDirect3DDevice9_SetStreamSource(device, 1, NULL, 0, 0);
2064     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetStreamSource failed (0x%08x)\n", hr);
2065     hr = IDirect3DDevice9_SetVertexShader(device, shader);
2066     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetVertexShader failed (0x%08x)\n", hr);
2067     hr = IDirect3DDevice9_SetVertexDeclaration(device, decl);
2068     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetVertexDeclaration failed (0x%08x)\n", hr);
2069
2070     hr = IDirect3DDevice9_BeginScene(device);
2071     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed (0x%08x)\n", hr);
2072     if(SUCCEEDED(hr)) {
2073         hr = IDirect3DDevice9_DrawPrimitive(device, D3DPT_POINTLIST, 0, 1);
2074         ok(SUCCEEDED(hr), "IDirect3DDevice9_DrawPrimitive failed (0x%08x)\n", hr);
2075
2076         hr = IDirect3DDevice9_EndScene(device);
2077         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed (0x%08x)\n", hr);
2078     }
2079
2080     IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
2081     IDirect3DDevice9_SetVertexShader(device, NULL);
2082     IDirect3DDevice9_SetVertexDeclaration(device, NULL);
2083
2084 cleanup:
2085     if (buffer) IDirect3DVertexBuffer9_Release(buffer);
2086     if(decl) IDirect3DVertexDeclaration9_Release(decl);
2087     if(shader) IDirect3DVertexShader9_Release(shader);
2088     if (device)
2089     {
2090         UINT refcount = IDirect3DDevice9_Release(device);
2091         ok(!refcount, "Device has %u references left.\n", refcount);
2092     }
2093     if(d3d9) IDirect3D9_Release(d3d9);
2094 }
2095
2096 static void test_lights(void)
2097 {
2098     D3DPRESENT_PARAMETERS present_parameters;
2099     IDirect3DDevice9 *device = NULL;
2100     IDirect3D9 *d3d9;
2101     HWND hwnd;
2102     HRESULT hr;
2103     unsigned int i;
2104     BOOL enabled;
2105     D3DCAPS9 caps;
2106
2107     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
2108     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
2109     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
2110     ok(hwnd != NULL, "Failed to create window\n");
2111     if (!d3d9 || !hwnd) goto cleanup;
2112
2113     ZeroMemory(&present_parameters, sizeof(present_parameters));
2114     present_parameters.Windowed = TRUE;
2115     present_parameters.hDeviceWindow = hwnd;
2116     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2117
2118     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
2119                                   D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
2120     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
2121        "IDirect3D9_CreateDevice failed with %08x\n", hr);
2122     if(!device)
2123     {
2124         skip("Failed to create a d3d device\n");
2125         goto cleanup;
2126     }
2127
2128     memset(&caps, 0, sizeof(caps));
2129     hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
2130     ok(hr == D3D_OK, "IDirect3DDevice9_GetDeviceCaps failed with %08x\n", hr);
2131
2132     for(i = 1; i <= caps.MaxActiveLights; i++) {
2133         hr = IDirect3DDevice9_LightEnable(device, i, TRUE);
2134         ok(hr == D3D_OK, "Enabling light %u failed with %08x\n", i, hr);
2135         hr = IDirect3DDevice9_GetLightEnable(device, i, &enabled);
2136         ok(hr == D3D_OK, "GetLightEnable on light %u failed with %08x\n", i, hr);
2137         ok(enabled, "Light %d is %s\n", i, enabled ? "enabled" : "disabled");
2138     }
2139
2140     /* TODO: Test the rendering results in this situation */
2141     hr = IDirect3DDevice9_LightEnable(device, i + 1, TRUE);
2142     ok(hr == D3D_OK, "Enabling one light more than supported returned %08x\n", hr);
2143     hr = IDirect3DDevice9_GetLightEnable(device, i + 1, &enabled);
2144     ok(hr == D3D_OK, "GetLightEnable on light %u failed with %08x\n", i + 1, hr);
2145     ok(enabled, "Light %d is %s\n", i + 1, enabled ? "enabled" : "disabled");
2146     hr = IDirect3DDevice9_LightEnable(device, i + 1, FALSE);
2147     ok(hr == D3D_OK, "Disabling the additional returned %08x\n", hr);
2148
2149     for(i = 1; i <= caps.MaxActiveLights; i++) {
2150         hr = IDirect3DDevice9_LightEnable(device, i, FALSE);
2151         ok(hr == D3D_OK, "Disabling light %u failed with %08x\n", i, hr);
2152     }
2153
2154 cleanup:
2155     if (device)
2156     {
2157         UINT refcount = IDirect3DDevice9_Release(device);
2158         ok(!refcount, "Device has %u references left.\n", refcount);
2159     }
2160     if(d3d9) IDirect3D9_Release(d3d9);
2161 }
2162
2163 static void test_set_stream_source(void)
2164 {
2165     D3DPRESENT_PARAMETERS present_parameters;
2166     IDirect3DDevice9 *device = NULL;
2167     IDirect3D9 *d3d9;
2168     HWND hwnd;
2169     HRESULT hr;
2170     IDirect3DVertexBuffer9 *pVertexBuffer = NULL;
2171
2172     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
2173     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
2174     hwnd = CreateWindow( "d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
2175     ok(hwnd != NULL, "Failed to create window\n");
2176     if (!d3d9 || !hwnd) goto cleanup;
2177
2178     ZeroMemory(&present_parameters, sizeof(present_parameters));
2179     present_parameters.Windowed = TRUE;
2180     present_parameters.hDeviceWindow = hwnd;
2181     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2182
2183     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
2184                                   D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
2185     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
2186        "IDirect3D9_CreateDevice failed with %08x\n", hr);
2187     if(!device)
2188     {
2189         hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd,
2190                                       D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
2191         ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
2192            "IDirect3D9_CreateDevice failed with %08x\n", hr);
2193         if(!device)
2194         {
2195             skip("Failed to create a d3d device\n");
2196             goto cleanup;
2197         }
2198     }
2199
2200     hr = IDirect3DDevice9_CreateVertexBuffer( device, 512, 0, 0, D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
2201     ok(hr == D3D_OK, "Failed to create a vertex buffer, hr = %08x\n", hr);
2202     if (SUCCEEDED(hr)) {
2203         /* Some cards(Geforce 7400 at least) accept non-aligned offsets, others(radeon 9000 verified) reject it,
2204          * so accept both results. Wine currently rejects this to be able to optimize the vbo conversion, but writes
2205          * a WARN
2206          */
2207         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 0, 32);
2208         ok(hr == D3D_OK, "Failed to set the stream source, offset 0, hr = %08x\n", hr);
2209         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 1, 32);
2210         ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 1, hr = %08x\n", hr);
2211         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 2, 32);
2212         ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 2, hr = %08x\n", hr);
2213         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 3, 32);
2214         ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 3, hr = %08x\n", hr);
2215         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 4, 32);
2216         ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %08x\n", hr);
2217     }
2218     /* Try to set the NULL buffer with an offset and stride 0 */
2219     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
2220     ok(hr == D3D_OK, "Failed to set the stream source, offset 0, hr = %08x\n", hr);
2221     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 1, 0);
2222     ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 1, hr = %08x\n", hr);
2223     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 2, 0);
2224     ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 2, hr = %08x\n", hr);
2225     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 3, 0);
2226     ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 3, hr = %08x\n", hr);
2227     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 4, 0);
2228     ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %08x\n", hr);
2229
2230     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
2231     ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %08x\n", hr);
2232
2233 cleanup:
2234     if (pVertexBuffer) IDirect3DVertexBuffer9_Release(pVertexBuffer);
2235     if (device)
2236     {
2237         UINT refcount = IDirect3DDevice9_Release(device);
2238         ok(!refcount, "Device has %u references left.\n", refcount);
2239     }
2240     if(d3d9) IDirect3D9_Release(d3d9);
2241 }
2242
2243 struct formats {
2244     D3DFORMAT DisplayFormat;
2245     D3DFORMAT BackBufferFormat;
2246     BOOL shouldPass;
2247 };
2248
2249 static const struct formats r5g6b5_format_list[] =
2250 {
2251     { D3DFMT_R5G6B5, D3DFMT_R5G6B5, TRUE },
2252     { D3DFMT_R5G6B5, D3DFMT_X1R5G5B5, FALSE },
2253     { D3DFMT_R5G6B5, D3DFMT_A1R5G5B5, FALSE },
2254     { D3DFMT_R5G6B5, D3DFMT_X8R8G8B8, FALSE },
2255     { D3DFMT_R5G6B5, D3DFMT_A8R8G8B8, FALSE },
2256     { 0, 0, 0}
2257 };
2258
2259 static const struct formats x1r5g5b5_format_list[] =
2260 {
2261     { D3DFMT_X1R5G5B5, D3DFMT_R5G6B5, FALSE },
2262     { D3DFMT_X1R5G5B5, D3DFMT_X1R5G5B5, TRUE },
2263     { D3DFMT_X1R5G5B5, D3DFMT_A1R5G5B5, TRUE },
2264     { D3DFMT_X1R5G5B5, D3DFMT_X8R8G8B8, FALSE },
2265     { D3DFMT_X1R5G5B5, D3DFMT_A8R8G8B8, FALSE },
2266
2267     /* A1R5G5B5 should not be usable as a display format, it is backbuffer-only */
2268     { D3DFMT_A1R5G5B5, D3DFMT_R5G6B5, FALSE },
2269     { D3DFMT_A1R5G5B5, D3DFMT_X1R5G5B5, FALSE },
2270     { D3DFMT_A1R5G5B5, D3DFMT_A1R5G5B5, FALSE },
2271     { D3DFMT_A1R5G5B5, D3DFMT_X8R8G8B8, FALSE },
2272     { D3DFMT_A1R5G5B5, D3DFMT_A8R8G8B8, FALSE },
2273     { 0, 0, 0}
2274 };
2275
2276 static const struct formats x8r8g8b8_format_list[] =
2277 {
2278     { D3DFMT_X8R8G8B8, D3DFMT_R5G6B5, FALSE },
2279     { D3DFMT_X8R8G8B8, D3DFMT_X1R5G5B5, FALSE },
2280     { D3DFMT_X8R8G8B8, D3DFMT_A1R5G5B5, FALSE },
2281     { D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, TRUE },
2282     { D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, TRUE },
2283
2284     /* A1R8G8B8 should not be usable as a display format, it is backbuffer-only */
2285     { D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, FALSE },
2286     { D3DFMT_A8R8G8B8, D3DFMT_X1R5G5B5, FALSE },
2287     { D3DFMT_A8R8G8B8, D3DFMT_A1R5G5B5, FALSE },
2288     { D3DFMT_A8R8G8B8, D3DFMT_X8R8G8B8, FALSE },
2289     { D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, FALSE },
2290     { 0, 0, 0}
2291 };
2292
2293 static void test_display_formats(void)
2294 {
2295     /* Direct3D9 offers 4 display formats R5G6B5, X1R5G5B5, X8R8G8B8 and A2R10G10B10.
2296      * Next to these there are 6 different backbuffer formats. Only a fixed number of
2297      * combinations are possible in FULLSCREEN mode. In windowed mode more combinations are
2298      * allowed due to depth conversion and this is likely driver dependent.
2299      * This test checks which combinations are possible in fullscreen mode and this should not be driver dependent.
2300      * TODO: handle A2R10G10B10 but what hardware supports it? Parhelia? It is very rare. */
2301
2302     UINT Adapter = D3DADAPTER_DEFAULT;
2303     D3DDEVTYPE DeviceType = D3DDEVTYPE_HAL;
2304     int i, nmodes;
2305     HRESULT hr;
2306
2307     IDirect3D9 *d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
2308     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
2309     if(!d3d9) return;
2310
2311     nmodes = IDirect3D9_GetAdapterModeCount(d3d9, D3DADAPTER_DEFAULT, D3DFMT_R5G6B5);
2312     if(!nmodes) {
2313         skip("Display format R5G6B5 not supported, skipping\n");
2314     } else {
2315         trace("Testing display format R5G6B5\n");
2316         for(i=0; r5g6b5_format_list[i].DisplayFormat != 0; i++)
2317         {
2318             hr = IDirect3D9_CheckDeviceType(d3d9, Adapter, DeviceType, r5g6b5_format_list[i].DisplayFormat, r5g6b5_format_list[i].BackBufferFormat, FALSE);
2319
2320             if(r5g6b5_format_list[i].shouldPass)
2321                 ok(hr == D3D_OK ||
2322                    broken(hr == D3DERR_NOTAVAILABLE),
2323                    "format %d %d didn't pass with hr=%#08x\n", r5g6b5_format_list[i].DisplayFormat, r5g6b5_format_list[i].BackBufferFormat, hr);
2324             else
2325                 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);
2326         }
2327     }
2328
2329     nmodes = IDirect3D9_GetAdapterModeCount(d3d9, D3DADAPTER_DEFAULT, D3DFMT_X1R5G5B5);
2330     if(!nmodes) {
2331         skip("Display format X1R5G5B5 not supported, skipping\n");
2332     } else {
2333         trace("Testing display format X1R5G5B5\n");
2334         for(i=0; x1r5g5b5_format_list[i].DisplayFormat != 0; i++)
2335         {
2336             hr = IDirect3D9_CheckDeviceType(d3d9, Adapter, DeviceType, x1r5g5b5_format_list[i].DisplayFormat, x1r5g5b5_format_list[i].BackBufferFormat, FALSE);
2337
2338             if(x1r5g5b5_format_list[i].shouldPass)
2339                 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);
2340             else
2341                 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);
2342         }
2343     }
2344
2345     nmodes = IDirect3D9_GetAdapterModeCount(d3d9, D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8);
2346     if(!nmodes) {
2347         skip("Display format X8R8G8B8 not supported, skipping\n");
2348     } else {
2349         trace("Testing display format X8R8G8B8\n");
2350         for(i=0; x8r8g8b8_format_list[i].DisplayFormat != 0; i++)
2351         {
2352             hr = IDirect3D9_CheckDeviceType(d3d9, Adapter, DeviceType, x8r8g8b8_format_list[i].DisplayFormat, x8r8g8b8_format_list[i].BackBufferFormat, FALSE);
2353
2354             if(x8r8g8b8_format_list[i].shouldPass)
2355                 ok(hr == D3D_OK ||
2356                    broken(hr == D3DERR_NOTAVAILABLE),
2357                    "format %d %d didn't pass with hr=%#08x\n", x8r8g8b8_format_list[i].DisplayFormat, x8r8g8b8_format_list[i].BackBufferFormat, hr);
2358             else
2359                 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);
2360         }
2361     }
2362
2363     if(d3d9) IDirect3D9_Release(d3d9);
2364 }
2365
2366 static void test_scissor_size(void)
2367 {
2368     IDirect3D9 *d3d9_ptr = 0;
2369     unsigned int i;
2370     static struct {
2371         int winx; int winy; int backx; int backy; BOOL window;
2372     } scts[] = { /* scissor tests */
2373         {800, 600, 640, 480, TRUE},
2374         {800, 600, 640, 480, FALSE},
2375         {640, 480, 800, 600, TRUE},
2376         {640, 480, 800, 600, FALSE},
2377     };
2378
2379     d3d9_ptr = pDirect3DCreate9(D3D_SDK_VERSION);
2380     ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
2381     if (!d3d9_ptr){
2382         skip("Failed to create IDirect3D9 object\n");
2383         return;
2384     }
2385
2386     for(i=0; i<sizeof(scts)/sizeof(scts[0]); i++) {
2387         IDirect3DDevice9 *device_ptr = 0;
2388         D3DPRESENT_PARAMETERS present_parameters;
2389         HRESULT hr;
2390         HWND hwnd = 0;
2391         RECT scissorrect;
2392
2393         hwnd = CreateWindow("d3d9_test_wc", "d3d9_test",
2394                         WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, scts[i].winx, scts[i].winy, 0, 0, 0, 0);
2395
2396         if (!scts[i].window)
2397         {
2398             scts[i].backx = screen_width;
2399             scts[i].backy = screen_height;
2400         }
2401
2402         ZeroMemory(&present_parameters, sizeof(present_parameters));
2403         present_parameters.Windowed = scts[i].window;
2404         present_parameters.hDeviceWindow = hwnd;
2405         present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2406         present_parameters.BackBufferWidth = scts[i].backx;
2407         present_parameters.BackBufferHeight = scts[i].backy;
2408         present_parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
2409         present_parameters.EnableAutoDepthStencil = TRUE;
2410         present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
2411
2412         hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
2413         if(FAILED(hr)) {
2414             present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
2415             hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
2416             if(FAILED(hr)) {
2417                 hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
2418             }
2419         }
2420         ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D_CreateDevice returned: %08x\n", hr);
2421
2422         if (!device_ptr)
2423         {
2424             DestroyWindow(hwnd);
2425             skip("Creating the device failed\n");
2426             goto err_out;
2427         }
2428
2429         /* Check for the default scissor rect size */
2430         hr = IDirect3DDevice9_GetScissorRect(device_ptr, &scissorrect);
2431         ok(hr == D3D_OK, "IDirect3DDevice9_GetScissorRect failed with: %08x\n", hr);
2432         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);
2433
2434         /* check the scissorrect values after a reset */
2435         present_parameters.BackBufferWidth = screen_width;
2436         present_parameters.BackBufferHeight = screen_height;
2437         hr = IDirect3DDevice9_Reset(device_ptr, &present_parameters);
2438         ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
2439         hr = IDirect3DDevice9_TestCooperativeLevel(device_ptr);
2440         ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
2441
2442         hr = IDirect3DDevice9_GetScissorRect(device_ptr, &scissorrect);
2443         ok(hr == D3D_OK, "IDirect3DDevice9_GetScissorRect failed with: %08x\n", hr);
2444         ok(scissorrect.right == screen_width && scissorrect.bottom == screen_height && scissorrect.top == 0 && scissorrect.left == 0, "Scissorrect missmatch (%d, %d) should be (%d, %d)\n", scissorrect.right, scissorrect.bottom, screen_width, screen_height);
2445
2446         if(device_ptr) {
2447             ULONG ref;
2448
2449             ref = IDirect3DDevice9_Release(device_ptr);
2450             DestroyWindow(hwnd);
2451             ok(ref == 0, "The device was not properly freed: refcount %u\n", ref);
2452         }
2453     }
2454
2455 err_out:
2456     if(d3d9_ptr) IDirect3D9_Release(d3d9_ptr);
2457     return;
2458 }
2459
2460 static void test_multi_device(void)
2461 {
2462     IDirect3DDevice9 *device1 = NULL, *device2 = NULL;
2463     D3DPRESENT_PARAMETERS present_parameters;
2464     HWND hwnd1 = NULL, hwnd2 = NULL;
2465     IDirect3D9 *d3d9;
2466     ULONG refcount;
2467     HRESULT hr;
2468
2469     d3d9 = pDirect3DCreate9(D3D_SDK_VERSION);
2470     ok(d3d9 != NULL, "Failed to create a d3d9 object.\n");
2471     if (!d3d9) goto fail;
2472
2473     hwnd1 = CreateWindow("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL);
2474     ok(hwnd1 != NULL, "Failed to create a window.\n");
2475     if (!hwnd1) goto fail;
2476
2477     memset(&present_parameters, 0, sizeof(present_parameters));
2478     present_parameters.Windowed = TRUE;
2479     present_parameters.hDeviceWindow = hwnd1;
2480     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2481
2482     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd1,
2483             D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device1);
2484     IDirect3D9_Release(d3d9);
2485     d3d9 = NULL;
2486     if (FAILED(hr)) {
2487         skip("Failed to create a device\n");
2488         goto fail;
2489     }
2490
2491     d3d9 = pDirect3DCreate9(D3D_SDK_VERSION);
2492     ok(d3d9 != NULL, "Failed to create a d3d9 object.\n");
2493     if (!d3d9) goto fail;
2494
2495     hwnd2 = CreateWindow("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL);
2496     ok(hwnd2 != NULL, "Failed to create a window.\n");
2497     if (!hwnd2) goto fail;
2498
2499     memset(&present_parameters, 0, sizeof(present_parameters));
2500     present_parameters.Windowed = TRUE;
2501     present_parameters.hDeviceWindow = hwnd2;
2502     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2503
2504     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd2,
2505             D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device2);
2506     ok(SUCCEEDED(hr), "Failed to create a device, hr %#x\n", hr);
2507     IDirect3D9_Release(d3d9);
2508     d3d9 = NULL;
2509     if (FAILED(hr)) goto fail;
2510
2511 fail:
2512     if (d3d9) IDirect3D9_Release(d3d9);
2513     if (device1)
2514     {
2515         refcount = IDirect3DDevice9_Release(device1);
2516         ok(!refcount, "Device has %u references left.\n", refcount);
2517     }
2518     if (device2)
2519     {
2520         refcount = IDirect3DDevice9_Release(device2);
2521         ok(!refcount, "Device has %u references left.\n", refcount);
2522     }
2523     if (hwnd1) DestroyWindow(hwnd1);
2524     if (hwnd2) DestroyWindow(hwnd2);
2525 }
2526
2527 static HWND filter_messages;
2528
2529 enum message_window
2530 {
2531     DEVICE_WINDOW,
2532     FOCUS_WINDOW,
2533 };
2534
2535 struct message
2536 {
2537     UINT message;
2538     enum message_window window;
2539 };
2540
2541 static const struct message *expect_messages;
2542 static HWND device_window, focus_window;
2543
2544 struct wndproc_thread_param
2545 {
2546     HWND dummy_window;
2547     HANDLE window_created;
2548     HANDLE test_finished;
2549     BOOL running_in_foreground;
2550 };
2551
2552 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2553 {
2554     if (filter_messages && filter_messages == hwnd)
2555     {
2556         if (message != WM_DISPLAYCHANGE && message != WM_IME_NOTIFY)
2557             todo_wine ok( 0, "Received unexpected message %#x for window %p.\n", message, hwnd);
2558     }
2559
2560     if (expect_messages)
2561     {
2562         HWND w;
2563
2564         switch (expect_messages->window)
2565         {
2566             case DEVICE_WINDOW:
2567                 w = device_window;
2568                 break;
2569
2570             case FOCUS_WINDOW:
2571                 w = focus_window;
2572                 break;
2573
2574             default:
2575                 w = NULL;
2576                 break;
2577         };
2578
2579         if (hwnd == w && expect_messages->message == message) ++expect_messages;
2580     }
2581
2582     return DefWindowProcA(hwnd, message, wparam, lparam);
2583 }
2584
2585 static DWORD WINAPI wndproc_thread(void *param)
2586 {
2587     struct wndproc_thread_param *p = param;
2588     DWORD res;
2589     BOOL ret;
2590
2591     p->dummy_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test",
2592             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2593     p->running_in_foreground = SetForegroundWindow(p->dummy_window);
2594
2595     ret = SetEvent(p->window_created);
2596     ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
2597
2598     for (;;)
2599     {
2600         MSG msg;
2601
2602         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
2603         res = WaitForSingleObject(p->test_finished, 100);
2604         if (res == WAIT_OBJECT_0) break;
2605         if (res != WAIT_TIMEOUT)
2606         {
2607             ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2608             break;
2609         }
2610     }
2611
2612     DestroyWindow(p->dummy_window);
2613
2614     return 0;
2615 }
2616
2617 static void test_wndproc(void)
2618 {
2619     struct wndproc_thread_param thread_params;
2620     IDirect3DDevice9 *device;
2621     WNDCLASSA wc = {0};
2622     IDirect3D9 *d3d9;
2623     HANDLE thread;
2624     LONG_PTR proc;
2625     ULONG ref;
2626     DWORD res, tid;
2627     HWND tmp;
2628
2629     static const struct message messages[] =
2630     {
2631         {WM_WINDOWPOSCHANGING,  FOCUS_WINDOW},
2632         {WM_ACTIVATE,           FOCUS_WINDOW},
2633         {WM_SETFOCUS,           FOCUS_WINDOW},
2634         {0,                     0},
2635     };
2636
2637     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
2638     {
2639         skip("Failed to create IDirect3D9 object, skipping tests.\n");
2640         return;
2641     }
2642
2643     wc.lpfnWndProc = test_proc;
2644     wc.lpszClassName = "d3d9_test_wndproc_wc";
2645     ok(RegisterClassA(&wc), "Failed to register window class.\n");
2646
2647     thread_params.window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
2648     ok(!!thread_params.window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
2649     thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
2650     ok(!!thread_params.test_finished, "CreateEvent failed, last error %#x.\n", GetLastError());
2651
2652     focus_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test",
2653             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2654     device_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test",
2655             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2656     thread = CreateThread(NULL, 0, wndproc_thread, &thread_params, 0, &tid);
2657     ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
2658
2659     res = WaitForSingleObject(thread_params.window_created, INFINITE);
2660     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2661
2662     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2663     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2664             (LONG_PTR)test_proc, proc);
2665     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2666     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2667             (LONG_PTR)test_proc, proc);
2668
2669     trace("device_window %p, focus_window %p, dummy_window %p.\n",
2670             device_window, focus_window, thread_params.dummy_window);
2671
2672     tmp = GetFocus();
2673     ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
2674     if (thread_params.running_in_foreground)
2675     {
2676         tmp = GetForegroundWindow();
2677         ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
2678                 thread_params.dummy_window, tmp);
2679     }
2680     else
2681         skip("Not running in foreground, skip foreground window test\n");
2682
2683     flush_events();
2684
2685     expect_messages = messages;
2686
2687     device = create_device(d3d9, device_window, focus_window, FALSE);
2688     if (!device)
2689     {
2690         skip("Failed to create a D3D device, skipping tests.\n");
2691         goto done;
2692     }
2693
2694     ok(!expect_messages->message, "Expected message %#x for window %#x, but didn't receive it.\n",
2695             expect_messages->message, expect_messages->window);
2696     expect_messages = NULL;
2697
2698     if (0) /* Disabled until we can make this work in a reliable way on Wine. */
2699     {
2700         tmp = GetFocus();
2701         ok(tmp == focus_window, "Expected focus %p, got %p.\n", focus_window, tmp);
2702         tmp = GetForegroundWindow();
2703         ok(tmp == focus_window, "Expected foreground window %p, got %p.\n", focus_window, tmp);
2704     }
2705     SetForegroundWindow(focus_window);
2706     flush_events();
2707
2708     filter_messages = focus_window;
2709
2710     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2711     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2712             (LONG_PTR)test_proc, proc);
2713
2714     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2715     ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2716             (LONG_PTR)test_proc, proc);
2717
2718     ref = IDirect3DDevice9_Release(device);
2719     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2720
2721     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2722     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2723             (LONG_PTR)test_proc, proc);
2724
2725     device = create_device(d3d9, focus_window, focus_window, FALSE);
2726     if (!device)
2727     {
2728         skip("Failed to create a D3D device, skipping tests.\n");
2729         goto done;
2730     }
2731
2732     ref = IDirect3DDevice9_Release(device);
2733     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2734
2735     device = create_device(d3d9, device_window, focus_window, FALSE);
2736     if (!device)
2737     {
2738         skip("Failed to create a D3D device, skipping tests.\n");
2739         goto done;
2740     }
2741
2742     proc = SetWindowLongPtrA(focus_window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2743     ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2744             (LONG_PTR)test_proc, proc);
2745
2746     ref = IDirect3DDevice9_Release(device);
2747     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2748
2749     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2750     ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2751             (LONG_PTR)DefWindowProcA, proc);
2752
2753 done:
2754     filter_messages = NULL;
2755     IDirect3D9_Release(d3d9);
2756
2757     SetEvent(thread_params.test_finished);
2758     WaitForSingleObject(thread, INFINITE);
2759     CloseHandle(thread_params.test_finished);
2760     CloseHandle(thread_params.window_created);
2761     CloseHandle(thread);
2762
2763     DestroyWindow(device_window);
2764     DestroyWindow(focus_window);
2765     UnregisterClassA("d3d9_test_wndproc_wc", GetModuleHandleA(NULL));
2766 }
2767
2768 static void test_wndproc_windowed(void)
2769 {
2770     struct wndproc_thread_param thread_params;
2771     IDirect3DDevice9 *device;
2772     WNDCLASSA wc = {0};
2773     IDirect3D9 *d3d9;
2774     HANDLE thread;
2775     LONG_PTR proc;
2776     HRESULT hr;
2777     ULONG ref;
2778     DWORD res, tid;
2779     HWND tmp;
2780
2781     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
2782     {
2783         skip("Failed to create IDirect3D9 object, skipping tests.\n");
2784         return;
2785     }
2786
2787     wc.lpfnWndProc = test_proc;
2788     wc.lpszClassName = "d3d9_test_wndproc_wc";
2789     ok(RegisterClassA(&wc), "Failed to register window class.\n");
2790
2791     thread_params.window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
2792     ok(!!thread_params.window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
2793     thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
2794     ok(!!thread_params.test_finished, "CreateEvent failed, last error %#x.\n", GetLastError());
2795
2796     focus_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test",
2797             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2798     device_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test",
2799             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2800     thread = CreateThread(NULL, 0, wndproc_thread, &thread_params, 0, &tid);
2801     ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
2802
2803     res = WaitForSingleObject(thread_params.window_created, INFINITE);
2804     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2805
2806     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2807     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2808             (LONG_PTR)test_proc, proc);
2809     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2810     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2811             (LONG_PTR)test_proc, proc);
2812
2813     trace("device_window %p, focus_window %p, dummy_window %p.\n",
2814             device_window, focus_window, thread_params.dummy_window);
2815
2816     tmp = GetFocus();
2817     ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
2818     if (thread_params.running_in_foreground)
2819     {
2820         tmp = GetForegroundWindow();
2821         ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
2822                 thread_params.dummy_window, tmp);
2823     }
2824     else
2825         skip("Not running in foreground, skip foreground window test\n");
2826
2827     filter_messages = focus_window;
2828
2829     device = create_device(d3d9, device_window, focus_window, TRUE);
2830     if (!device)
2831     {
2832         skip("Failed to create a D3D device, skipping tests.\n");
2833         goto done;
2834     }
2835
2836     tmp = GetFocus();
2837     ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
2838     tmp = GetForegroundWindow();
2839     ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
2840             thread_params.dummy_window, tmp);
2841
2842     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2843     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2844             (LONG_PTR)test_proc, proc);
2845
2846     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2847     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2848             (LONG_PTR)test_proc, proc);
2849
2850     filter_messages = NULL;
2851
2852     hr = reset_device(device, device_window, FALSE);
2853     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2854
2855     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2856     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2857             (LONG_PTR)test_proc, proc);
2858
2859     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2860     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2861             (LONG_PTR)test_proc, proc);
2862
2863     hr = reset_device(device, device_window, TRUE);
2864     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2865
2866     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2867     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2868             (LONG_PTR)test_proc, proc);
2869
2870     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2871     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2872             (LONG_PTR)test_proc, proc);
2873
2874     filter_messages = focus_window;
2875
2876     ref = IDirect3DDevice9_Release(device);
2877     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2878
2879     filter_messages = device_window;
2880
2881     device = create_device(d3d9, focus_window, focus_window, TRUE);
2882     if (!device)
2883     {
2884         skip("Failed to create a D3D device, skipping tests.\n");
2885         goto done;
2886     }
2887
2888     filter_messages = NULL;
2889
2890     hr = reset_device(device, focus_window, FALSE);
2891     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2892
2893     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2894     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2895             (LONG_PTR)test_proc, proc);
2896
2897     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2898     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2899             (LONG_PTR)test_proc, proc);
2900
2901     hr = reset_device(device, focus_window, TRUE);
2902     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2903
2904     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2905     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2906             (LONG_PTR)test_proc, proc);
2907
2908     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2909     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2910             (LONG_PTR)test_proc, proc);
2911
2912     filter_messages = device_window;
2913
2914     ref = IDirect3DDevice9_Release(device);
2915     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2916
2917     device = create_device(d3d9, device_window, focus_window, TRUE);
2918     if (!device)
2919     {
2920         skip("Failed to create a D3D device, skipping tests.\n");
2921         goto done;
2922     }
2923
2924     filter_messages = NULL;
2925
2926     hr = reset_device(device, device_window, FALSE);
2927     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2928
2929     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2930     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2931             (LONG_PTR)test_proc, proc);
2932
2933     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2934     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2935             (LONG_PTR)test_proc, proc);
2936
2937     hr = reset_device(device, device_window, TRUE);
2938     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2939
2940     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2941     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2942             (LONG_PTR)test_proc, proc);
2943
2944     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2945     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2946             (LONG_PTR)test_proc, proc);
2947
2948     filter_messages = device_window;
2949
2950     ref = IDirect3DDevice9_Release(device);
2951     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2952
2953 done:
2954     filter_messages = NULL;
2955     IDirect3D9_Release(d3d9);
2956
2957     SetEvent(thread_params.test_finished);
2958     WaitForSingleObject(thread, INFINITE);
2959     CloseHandle(thread_params.test_finished);
2960     CloseHandle(thread_params.window_created);
2961     CloseHandle(thread);
2962
2963     DestroyWindow(device_window);
2964     DestroyWindow(focus_window);
2965     UnregisterClassA("d3d9_test_wndproc_wc", GetModuleHandleA(NULL));
2966 }
2967
2968 static void test_reset_fullscreen(void)
2969 {
2970     WNDCLASSEX wc = {0};
2971     IDirect3DDevice9 *device = NULL;
2972     IDirect3D9 *d3d = NULL;
2973     ATOM atom;
2974     static const struct message messages[] =
2975     {
2976         {WM_ACTIVATEAPP,    FOCUS_WINDOW},
2977         {0,                     0},
2978     };
2979
2980     d3d = pDirect3DCreate9(D3D_SDK_VERSION);
2981     ok(d3d != NULL, "Failed to create an IDirect3D object.\n");
2982     expect_messages = messages;
2983
2984     wc.cbSize = sizeof(WNDCLASSEX);
2985     wc.lpfnWndProc = test_proc;
2986     wc.lpszClassName = "test_reset_fullscreen";
2987
2988     atom = RegisterClassEx(&wc);
2989     ok(atom, "Failed to register a new window class. GetLastError:%d\n", GetLastError());
2990
2991     device_window = focus_window = CreateWindowEx(0, wc.lpszClassName, "Test Reset Fullscreen", 0, 0, 0, screen_width, screen_height, NULL, NULL, NULL, NULL);
2992     ok(device_window != NULL, "Failed to create a window. GetLastError:%d\n", GetLastError());
2993
2994     /*
2995      * Create a device in windowed mode.
2996      * Since the device is windowed and we haven't called any methods that
2997      * could show the window (such as ShowWindow or SetWindowPos) yet,
2998      * WM_ACTIVATEAPP will not have been sent.
2999      */
3000     device = create_device(d3d, device_window, focus_window, TRUE);
3001     if (!device)
3002     {
3003         skip("Unable to create device.  Skipping test.\n");
3004         goto cleanup;
3005     }
3006
3007     /*
3008      * Switch to fullscreen mode.
3009      * This will force the window to be shown and will cause the WM_ACTIVATEAPP
3010      * message to be sent.
3011      */
3012     ok(SUCCEEDED(reset_device(device, device_window, FALSE)), "Failed to reset device.\n");
3013
3014     flush_events();
3015     ok(expect_messages->message == 0, "Expected to receive message %#x.\n", expect_messages->message);
3016     expect_messages = NULL;
3017
3018 cleanup:
3019     if (device) IDirect3DDevice9_Release(device);
3020     if (d3d) IDirect3D9_Release(d3d);
3021     DestroyWindow(device_window);
3022     device_window = focus_window = NULL;
3023     UnregisterClass(wc.lpszClassName, GetModuleHandle(NULL));
3024 }
3025
3026
3027 static inline void set_fpu_cw(WORD cw)
3028 {
3029 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3030 #define D3D9_TEST_SET_FPU_CW 1
3031     __asm__ volatile ("fnclex");
3032     __asm__ volatile ("fldcw %0" : : "m" (cw));
3033 #elif defined(__i386__) && defined(_MSC_VER)
3034 #define D3D9_TEST_SET_FPU_CW 1
3035     __asm fnclex;
3036     __asm fldcw cw;
3037 #endif
3038 }
3039
3040 static inline WORD get_fpu_cw(void)
3041 {
3042     WORD cw = 0;
3043 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3044 #define D3D9_TEST_GET_FPU_CW 1
3045     __asm__ volatile ("fnstcw %0" : "=m" (cw));
3046 #elif defined(__i386__) && defined(_MSC_VER)
3047 #define D3D9_TEST_GET_FPU_CW 1
3048     __asm fnstcw cw;
3049 #endif
3050     return cw;
3051 }
3052
3053 static void test_fpu_setup(void)
3054 {
3055 #if defined(D3D9_TEST_SET_FPU_CW) && defined(D3D9_TEST_GET_FPU_CW)
3056     D3DPRESENT_PARAMETERS present_parameters;
3057     IDirect3DDevice9 *device;
3058     HWND window = NULL;
3059     IDirect3D9 *d3d9;
3060     HRESULT hr;
3061     WORD cw;
3062
3063     d3d9 = pDirect3DCreate9(D3D_SDK_VERSION);
3064     ok(!!d3d9, "Failed to create a d3d9 object.\n");
3065     if (!d3d9) return;
3066
3067     window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
3068     ok(!!window, "Failed to create a window.\n");
3069     if (!window) goto done;
3070
3071     memset(&present_parameters, 0, sizeof(present_parameters));
3072     present_parameters.Windowed = TRUE;
3073     present_parameters.hDeviceWindow = window;
3074     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
3075
3076     set_fpu_cw(0xf60);
3077     cw = get_fpu_cw();
3078     ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
3079
3080     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
3081             D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device);
3082     if (FAILED(hr))
3083     {
3084         skip("Failed to create a device, hr %#x.\n", hr);
3085         set_fpu_cw(0x37f);
3086         goto done;
3087     }
3088
3089     cw = get_fpu_cw();
3090     ok(cw == 0x7f, "cw is %#x, expected 0x7f.\n", cw);
3091
3092     IDirect3DDevice9_Release(device);
3093
3094     cw = get_fpu_cw();
3095     ok(cw == 0x7f, "cw is %#x, expected 0x7f.\n", cw);
3096     set_fpu_cw(0xf60);
3097     cw = get_fpu_cw();
3098     ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
3099
3100     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
3101             D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &present_parameters, &device);
3102     ok(SUCCEEDED(hr), "CreateDevice failed, hr %#x.\n", hr);
3103
3104     cw = get_fpu_cw();
3105     ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
3106     set_fpu_cw(0x37f);
3107
3108     IDirect3DDevice9_Release(device);
3109
3110 done:
3111     if (window) DestroyWindow(window);
3112     if (d3d9) IDirect3D9_Release(d3d9);
3113 #endif
3114 }
3115
3116 static void test_window_style(void)
3117 {
3118     RECT focus_rect, fullscreen_rect, r;
3119     LONG device_style, device_exstyle;
3120     LONG focus_style, focus_exstyle;
3121     LONG style, expected_style;
3122     IDirect3DDevice9 *device;
3123     IDirect3D9 *d3d9;
3124     ULONG ref;
3125
3126
3127     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
3128     {
3129         skip("Failed to create IDirect3D9 object, skipping tests.\n");
3130         return;
3131     }
3132
3133     focus_window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3134             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3135     device_window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3136             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3137
3138     device_style = GetWindowLongA(device_window, GWL_STYLE);
3139     device_exstyle = GetWindowLongA(device_window, GWL_EXSTYLE);
3140     focus_style = GetWindowLongA(focus_window, GWL_STYLE);
3141     focus_exstyle = GetWindowLongA(focus_window, GWL_EXSTYLE);
3142
3143     SetRect(&fullscreen_rect, 0, 0, screen_width, screen_height);
3144     GetWindowRect(focus_window, &focus_rect);
3145
3146     device = create_device(d3d9, device_window, focus_window, FALSE);
3147     if (!device)
3148     {
3149         skip("Failed to create a D3D device, skipping tests.\n");
3150         goto done;
3151     }
3152
3153     style = GetWindowLongA(device_window, GWL_STYLE);
3154     expected_style = device_style | WS_VISIBLE;
3155     todo_wine ok(style == expected_style, "Expected device window style %#x, got %#x.\n",
3156             expected_style, style);
3157     style = GetWindowLongA(device_window, GWL_EXSTYLE);
3158     expected_style = device_exstyle | WS_EX_TOPMOST;
3159     todo_wine ok(style == expected_style, "Expected device window extended style %#x, got %#x.\n",
3160             expected_style, style);
3161
3162     style = GetWindowLongA(focus_window, GWL_STYLE);
3163     ok(style == focus_style, "Expected focus window style %#x, got %#x.\n",
3164             focus_style, style);
3165     style = GetWindowLongA(focus_window, GWL_EXSTYLE);
3166     ok(style == focus_exstyle, "Expected focus window extended style %#x, got %#x.\n",
3167             focus_exstyle, style);
3168
3169     GetWindowRect(device_window, &r);
3170     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3171             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
3172             r.left, r.top, r.right, r.bottom);
3173     GetClientRect(device_window, &r);
3174     todo_wine ok(!EqualRect(&r, &fullscreen_rect), "Client rect and window rect are equal.\n");
3175     GetWindowRect(focus_window, &r);
3176     ok(EqualRect(&r, &focus_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3177             focus_rect.left, focus_rect.top, focus_rect.right, focus_rect.bottom,
3178             r.left, r.top, r.right, r.bottom);
3179
3180     ref = IDirect3DDevice9_Release(device);
3181     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
3182
3183 done:
3184     IDirect3D9_Release(d3d9);
3185
3186     DestroyWindow(device_window);
3187     DestroyWindow(focus_window);
3188 }
3189
3190 static const POINT *expect_pos;
3191
3192 static LRESULT CALLBACK test_cursor_proc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
3193 {
3194     if (message == WM_MOUSEMOVE)
3195     {
3196         if (expect_pos && expect_pos->x && expect_pos->y)
3197         {
3198             POINT p = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)};
3199
3200             ClientToScreen(window, &p);
3201             if (expect_pos->x == p.x && expect_pos->y == p.y)
3202                 ++expect_pos;
3203         }
3204     }
3205
3206     return DefWindowProcA(window, message, wparam, lparam);
3207 }
3208
3209 static void test_cursor_pos(void)
3210 {
3211     IDirect3DSurface9 *cursor;
3212     IDirect3DDevice9 *device;
3213     WNDCLASSA wc = {0};
3214     IDirect3D9 *d3d9;
3215     UINT refcount;
3216     HWND window;
3217     HRESULT hr;
3218     BOOL ret;
3219
3220     /* Note that we don't check for movement we're not supposed to receive.
3221      * That's because it's hard to distinguish from the user accidentally
3222      * moving the mouse. */
3223     static const POINT points[] =
3224     {
3225         {50, 50},
3226         {75, 75},
3227         {100, 100},
3228         {125, 125},
3229         {150, 150},
3230         {125, 125},
3231         {150, 150},
3232         {150, 150},
3233         {0, 0},
3234     };
3235
3236     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
3237     {
3238         skip("Failed to create IDirect3D9 object, skipping cursor tests.\n");
3239         return;
3240     }
3241
3242     wc.lpfnWndProc = test_cursor_proc;
3243     wc.lpszClassName = "d3d9_test_cursor_wc";
3244     ok(RegisterClassA(&wc), "Failed to register window class.\n");
3245     window = CreateWindow("d3d9_test_cursor_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3246             0, 0, 320, 240, NULL, NULL, NULL, NULL);
3247     ShowWindow(window, SW_SHOW);
3248
3249     device = create_device(d3d9, window, window, TRUE);
3250     if (!device)
3251     {
3252         skip("Failed to create a D3D device, skipping tests.\n");
3253         goto done;
3254     }
3255
3256     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 32, 32,
3257             D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &cursor, NULL);
3258     ok(SUCCEEDED(hr), "Failed to create cursor surface, hr %#x.\n", hr);
3259     hr = IDirect3DDevice9_SetCursorProperties(device, 0, 0, cursor);
3260     ok(SUCCEEDED(hr), "Failed to set cursor properties, hr %#x.\n", hr);
3261     IDirect3DSurface9_Release(cursor);
3262     ret = IDirect3DDevice9_ShowCursor(device, TRUE);
3263     ok(!ret, "Failed to show cursor, hr %#x.\n", ret);
3264
3265     flush_events();
3266     expect_pos = points;
3267
3268     ret = SetCursorPos(50, 50);
3269     ok(ret, "Failed to set cursor position.\n");
3270     flush_events();
3271
3272     IDirect3DDevice9_SetCursorPosition(device, 75, 75, 0);
3273     flush_events();
3274     /* SetCursorPosition() eats duplicates. */
3275     IDirect3DDevice9_SetCursorPosition(device, 75, 75, 0);
3276     flush_events();
3277
3278     ret = SetCursorPos(100, 100);
3279     ok(ret, "Failed to set cursor position.\n");
3280     flush_events();
3281     /* Even if the position was set with SetCursorPos(). */
3282     IDirect3DDevice9_SetCursorPosition(device, 100, 100, 0);
3283     flush_events();
3284
3285     IDirect3DDevice9_SetCursorPosition(device, 125, 125, 0);
3286     flush_events();
3287     ret = SetCursorPos(150, 150);
3288     ok(ret, "Failed to set cursor position.\n");
3289     flush_events();
3290     IDirect3DDevice9_SetCursorPosition(device, 125, 125, 0);
3291     flush_events();
3292
3293     IDirect3DDevice9_SetCursorPosition(device, 150, 150, 0);
3294     flush_events();
3295     /* SetCursorPos() doesn't. */
3296     ret = SetCursorPos(150, 150);
3297     ok(ret, "Failed to set cursor position.\n");
3298     flush_events();
3299
3300     ok(!expect_pos->x && !expect_pos->y, "Didn't receive MOUSEMOVE %u (%d, %d).\n",
3301        (unsigned)(expect_pos - points), expect_pos->x, expect_pos->y);
3302
3303     refcount = IDirect3DDevice9_Release(device);
3304     ok(!refcount, "Device has %u references left.\n", refcount);
3305 done:
3306     DestroyWindow(window);
3307     UnregisterClassA("d3d9_test_cursor_wc", GetModuleHandleA(NULL));
3308     if (d3d9)
3309         IDirect3D9_Release(d3d9);
3310 }
3311
3312 static void test_mode_change(void)
3313 {
3314     RECT fullscreen_rect, focus_rect, r;
3315     IDirect3DSurface9 *backbuffer;
3316     IDirect3DDevice9 *device;
3317     D3DSURFACE_DESC desc;
3318     IDirect3D9 *d3d9;
3319     DEVMODEW devmode;
3320     UINT refcount;
3321     HRESULT hr;
3322     DWORD ret;
3323
3324     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
3325     {
3326         skip("Failed to create IDirect3D9 object, skipping mode change tests.\n");
3327         return;
3328     }
3329
3330     focus_window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3331             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3332     device_window = CreateWindowA("d3d9_test_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3333             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3334
3335     SetRect(&fullscreen_rect, 0, 0, screen_width, screen_height);
3336     GetWindowRect(focus_window, &focus_rect);
3337
3338     device = create_device(d3d9, device_window, focus_window, FALSE);
3339     if (!device)
3340     {
3341         skip("Failed to create a D3D device, skipping tests.\n");
3342         goto done;
3343     }
3344
3345     memset(&devmode, 0, sizeof(devmode));
3346     devmode.dmSize = sizeof(devmode);
3347     devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
3348     devmode.dmPelsWidth = 640;
3349     devmode.dmPelsHeight = 480;
3350
3351     ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
3352     ok(ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", ret);
3353
3354     memset(&devmode, 0, sizeof(devmode));
3355     devmode.dmSize = sizeof(devmode);
3356     ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3357     ok(ret, "Failed to get display mode.\n");
3358     ok(devmode.dmPelsWidth == 640, "Got unexpect width %u.\n", devmode.dmPelsWidth);
3359     ok(devmode.dmPelsHeight == 480, "Got unexpect height %u.\n", devmode.dmPelsHeight);
3360
3361     GetWindowRect(device_window, &r);
3362     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3363             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
3364             r.left, r.top, r.right, r.bottom);
3365     GetWindowRect(focus_window, &r);
3366     ok(EqualRect(&r, &focus_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3367             focus_rect.left, focus_rect.top, focus_rect.right, focus_rect.bottom,
3368             r.left, r.top, r.right, r.bottom);
3369
3370     hr = IDirect3DDevice9_GetBackBuffer(device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
3371     ok(SUCCEEDED(hr), "Failed to get backbuffer, hr %#x.\n", hr);
3372     hr = IDirect3DSurface9_GetDesc(backbuffer, &desc);
3373     ok(SUCCEEDED(hr), "Failed to get backbuffer desc, hr %#x.\n", hr);
3374     ok(desc.Width == screen_width, "Got unexpected backbuffer width %u.\n", desc.Width);
3375     ok(desc.Height == screen_height, "Got unexpected backbuffer height %u.\n", desc.Height);
3376     IDirect3DSurface9_Release(backbuffer);
3377
3378     refcount = IDirect3DDevice9_Release(device);
3379     ok(!refcount, "Device has %u references left.\n", refcount);
3380
3381     memset(&devmode, 0, sizeof(devmode));
3382     devmode.dmSize = sizeof(devmode);
3383     ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3384     ok(ret, "Failed to get display mode.\n");
3385     ok(devmode.dmPelsWidth == screen_width, "Got unexpect width %u.\n", devmode.dmPelsWidth);
3386     ok(devmode.dmPelsHeight == screen_height, "Got unexpect height %u.\n", devmode.dmPelsHeight);
3387
3388 done:
3389     DestroyWindow(device_window);
3390     DestroyWindow(focus_window);
3391     if (d3d9)
3392         IDirect3D9_Release(d3d9);
3393
3394     memset(&devmode, 0, sizeof(devmode));
3395     devmode.dmSize = sizeof(devmode);
3396     ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3397     ok(ret, "Failed to get display mode.\n");
3398     ok(devmode.dmPelsWidth == screen_width, "Got unexpect width %u.\n", devmode.dmPelsWidth);
3399     ok(devmode.dmPelsHeight == screen_height, "Got unexpect height %u.\n", devmode.dmPelsHeight);
3400 }
3401
3402 static void test_device_window_reset(void)
3403 {
3404     RECT fullscreen_rect, device_rect, r;
3405     IDirect3DDevice9 *device;
3406     WNDCLASSA wc = {0};
3407     IDirect3D9 *d3d9;
3408     LONG_PTR proc;
3409     HRESULT hr;
3410     ULONG ref;
3411
3412     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
3413     {
3414         skip("Failed to create IDirect3D9 object, skipping tests.\n");
3415         return;
3416     }
3417
3418     wc.lpfnWndProc = test_proc;
3419     wc.lpszClassName = "d3d9_test_wndproc_wc";
3420     ok(RegisterClassA(&wc), "Failed to register window class.\n");
3421
3422     focus_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3423             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3424     device_window = CreateWindowA("d3d9_test_wndproc_wc", "d3d9_test", WS_OVERLAPPEDWINDOW,
3425             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
3426
3427     SetRect(&fullscreen_rect, 0, 0, screen_width, screen_height);
3428     GetWindowRect(device_window, &device_rect);
3429
3430     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
3431     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3432             (LONG_PTR)test_proc, proc);
3433     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
3434     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3435             (LONG_PTR)test_proc, proc);
3436
3437     device = create_device(d3d9, NULL, focus_window, FALSE);
3438     if (!device)
3439     {
3440         skip("Failed to create a D3D device, skipping tests.\n");
3441         goto done;
3442     }
3443
3444     GetWindowRect(focus_window, &r);
3445     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3446             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
3447             r.left, r.top, r.right, r.bottom);
3448     GetWindowRect(device_window, &r);
3449     ok(EqualRect(&r, &device_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3450             device_rect.left, device_rect.top, device_rect.right, device_rect.bottom,
3451             r.left, r.top, r.right, r.bottom);
3452
3453     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
3454     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3455             (LONG_PTR)test_proc, proc);
3456     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
3457     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3458             (LONG_PTR)test_proc, proc);
3459
3460     hr = reset_device(device, device_window, FALSE);
3461     ok(SUCCEEDED(hr), "Failed to reset device.\n");
3462
3463     GetWindowRect(focus_window, &r);
3464     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3465             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
3466             r.left, r.top, r.right, r.bottom);
3467     GetWindowRect(device_window, &r);
3468     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3469             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
3470             r.left, r.top, r.right, r.bottom);
3471
3472     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
3473     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3474             (LONG_PTR)test_proc, proc);
3475     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
3476     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
3477             (LONG_PTR)test_proc, proc);
3478
3479     ref = IDirect3DDevice9_Release(device);
3480     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
3481
3482 done:
3483     IDirect3D9_Release(d3d9);
3484     DestroyWindow(device_window);
3485     DestroyWindow(focus_window);
3486     UnregisterClassA("d3d9_test_wndproc_wc", GetModuleHandleA(NULL));
3487 }
3488
3489 static void test_reset_resources(void)
3490 {
3491     IDirect3DSurface9 *surface, *rt;
3492     IDirect3DTexture9 *texture;
3493     IDirect3DDevice9 *device;
3494     IDirect3D9 *d3d9;
3495     unsigned int i;
3496     D3DCAPS9 caps;
3497     HWND window;
3498     HRESULT hr;
3499     ULONG ref;
3500
3501     window = CreateWindowA("static", "d3d9_test", WS_OVERLAPPEDWINDOW,
3502             0, 0, 640, 480, 0, 0, 0, 0);
3503
3504     if (!(d3d9 = pDirect3DCreate9(D3D_SDK_VERSION)))
3505     {
3506         skip("Failed to create IDirect3D9 object, skipping tests.\n");
3507         DestroyWindow(window);
3508         return;
3509     }
3510
3511     if (!(device = create_device(d3d9, window, window, TRUE)))
3512     {
3513         skip("Failed to create a D3D device, skipping tests.\n");
3514         goto done;
3515     }
3516
3517     hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
3518     ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
3519
3520     hr = IDirect3DDevice9_CreateTexture(device, 128, 128, 1, D3DUSAGE_DEPTHSTENCIL,
3521             D3DFMT_D24S8, D3DPOOL_DEFAULT, &texture, NULL);
3522     ok(SUCCEEDED(hr), "Failed to create depth/stencil texture, hr %#x.\n", hr);
3523     hr = IDirect3DTexture9_GetSurfaceLevel(texture, 0, &surface);
3524     ok(SUCCEEDED(hr), "Failed to get surface, hr %#x.\n", hr);
3525     IDirect3DTexture9_Release(texture);
3526     hr = IDirect3DDevice9_SetDepthStencilSurface(device, surface);
3527     ok(SUCCEEDED(hr), "Failed to set depth/stencil surface, hr %#x.\n", hr);
3528     IDirect3DSurface9_Release(surface);
3529
3530     for (i = 0; i < caps.NumSimultaneousRTs; ++i)
3531     {
3532         hr = IDirect3DDevice9_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET,
3533                 D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture, NULL);
3534         ok(SUCCEEDED(hr), "Failed to create render target texture %u, hr %#x.\n", i, hr);
3535         hr = IDirect3DTexture9_GetSurfaceLevel(texture, 0, &surface);
3536         ok(SUCCEEDED(hr), "Failed to get surface %u, hr %#x.\n", i, hr);
3537         IDirect3DTexture9_Release(texture);
3538         hr = IDirect3DDevice9_SetRenderTarget(device, i, surface);
3539         ok(SUCCEEDED(hr), "Failed to set render target surface %u, hr %#x.\n", i, hr);
3540         IDirect3DSurface9_Release(surface);
3541     }
3542
3543     hr = reset_device(device, device_window, TRUE);
3544     ok(SUCCEEDED(hr), "Failed to reset device.\n");
3545
3546     hr = IDirect3DDevice9_GetBackBuffer(device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &rt);
3547     ok(SUCCEEDED(hr), "Failed to get back buffer, hr %#x.\n", hr);
3548     hr = IDirect3DDevice9_GetRenderTarget(device, 0, &surface);
3549     ok(SUCCEEDED(hr), "Failed to get render target surface, hr %#x.\n", hr);
3550     ok(surface == rt, "Got unexpected surface %p for render target.\n", surface);
3551     IDirect3DSurface9_Release(surface);
3552     IDirect3DSurface9_Release(rt);
3553
3554     for (i = 1; i < caps.NumSimultaneousRTs; ++i)
3555     {
3556         hr = IDirect3DDevice9_GetRenderTarget(device, i, &surface);
3557         ok(hr == D3DERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
3558     }
3559
3560     ref = IDirect3DDevice9_Release(device);
3561     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
3562
3563 done:
3564     IDirect3D9_Release(d3d9);
3565     DestroyWindow(window);
3566 }
3567
3568 START_TEST(device)
3569 {
3570     HMODULE d3d9_handle = LoadLibraryA( "d3d9.dll" );
3571     WNDCLASS wc = {0};
3572
3573     wc.lpfnWndProc = DefWindowProc;
3574     wc.lpszClassName = "d3d9_test_wc";
3575     RegisterClass(&wc);
3576
3577     if (!d3d9_handle)
3578     {
3579         skip("Could not load d3d9.dll\n");
3580         goto out;
3581     }
3582
3583     pDirect3DCreate9 = (void *)GetProcAddress( d3d9_handle, "Direct3DCreate9" );
3584     ok(pDirect3DCreate9 != NULL, "Failed to get address of Direct3DCreate9\n");
3585     if (pDirect3DCreate9)
3586     {
3587         IDirect3D9 *d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
3588         if(!d3d9)
3589         {
3590             skip("could not create D3D9 object\n");
3591             goto out;
3592         }
3593         IDirect3D9_Release(d3d9);
3594
3595         screen_width = GetSystemMetrics(SM_CXSCREEN);
3596         screen_height = GetSystemMetrics(SM_CYSCREEN);
3597
3598         test_fpu_setup();
3599         test_multi_device();
3600         test_display_formats();
3601         test_display_modes();
3602         test_swapchain();
3603         test_refcount();
3604         test_mipmap_levels();
3605         test_checkdevicemultisampletype();
3606         test_cursor();
3607         test_cursor_pos();
3608         test_reset_fullscreen();
3609         test_reset();
3610         test_scene();
3611         test_limits();
3612         test_depthstenciltest();
3613         test_get_rt();
3614         test_draw_indexed();
3615         test_null_stream();
3616         test_lights();
3617         test_set_stream_source();
3618         test_scissor_size();
3619         test_wndproc();
3620         test_wndproc_windowed();
3621         test_window_style();
3622         test_mode_change();
3623         test_device_window_reset();
3624         test_reset_resources();
3625     }
3626
3627 out:
3628     UnregisterClassA("d3d9_test_wc", GetModuleHandleA(NULL));
3629 }