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