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