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