vbscript: Added function parser implementation.
[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(hr == D3D_OK, "GetDesc failed, hr %#x.\n", hr);
1054     ok(surface_desc.Width == modes[i].w, "Back buffer width is %u, expected %u.\n",
1055             surface_desc.Width, modes[i].w);
1056     ok(surface_desc.Height == modes[i].h, "Back buffer height is %u, expected %u.\n",
1057             surface_desc.Height, modes[i].h);
1058     IDirect3DSurface8_Release(surface);
1059
1060     memset(&d3dpp, 0, sizeof(d3dpp));
1061     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1062     d3dpp.Windowed = TRUE;
1063     d3dpp.BackBufferWidth = 400;
1064     d3dpp.BackBufferHeight = 300;
1065     d3dpp.BackBufferFormat = d3ddm.Format;
1066     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1067     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1068     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1069     ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1070
1071     memset(&vp, 0, sizeof(vp));
1072     hr = IDirect3DDevice8_GetViewport(device1, &vp);
1073     ok(SUCCEEDED(hr), "GetViewport failed, hr %#x.\n", hr);
1074     if (SUCCEEDED(hr))
1075     {
1076         ok(vp.X == 0, "D3DVIEWPORT->X = %u, expected 0.\n", vp.X);
1077         ok(vp.Y == 0, "D3DVIEWPORT->Y = %u, expected 0.\n", vp.Y);
1078         ok(vp.Width == 400, "D3DVIEWPORT->Width = %u, expected 400.\n", vp.Width);
1079         ok(vp.Height == 300, "D3DVIEWPORT->Height = %u, expected 300.\n", vp.Height);
1080         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %.8e, expected 0.\n", vp.MinZ);
1081         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %.8e, expected 1.\n", vp.MaxZ);
1082     }
1083
1084     width = GetSystemMetrics(SM_CXSCREEN);
1085     height = GetSystemMetrics(SM_CYSCREEN);
1086     ok(width == orig_width, "Screen width is %u, expected %u.\n", width, orig_width);
1087     ok(height == orig_height, "Screen height is %u, expected %u.\n", height, orig_height);
1088
1089     hr = IDirect3DDevice8_GetRenderTarget(device1, &surface);
1090     ok(SUCCEEDED(hr), "GetRenderTarget failed, hr %#x.\n", hr);
1091     hr = IDirect3DSurface8_GetDesc(surface, &surface_desc);
1092     ok(hr == D3D_OK, "GetDesc failed, hr %#x.\n", hr);
1093     ok(surface_desc.Width == 400, "Back buffer width is %u, expected 400.\n",
1094             surface_desc.Width);
1095     ok(surface_desc.Height == 300, "Back buffer height is %u, expected 300.\n",
1096             surface_desc.Height);
1097     IDirect3DSurface8_Release(surface);
1098
1099     memset(&d3dpp, 0, sizeof(d3dpp));
1100     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1101     d3dpp.Windowed = TRUE;
1102     d3dpp.BackBufferWidth = 400;
1103     d3dpp.BackBufferHeight = 300;
1104     d3dpp.BackBufferFormat = d3ddm.Format;
1105
1106     /* Reset fails if there is a resource in the default pool. */
1107     hr = IDirect3DDevice8_CreateTexture(device1, 16, 16, 1, 0, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &texture);
1108     ok(SUCCEEDED(hr), "CreateTexture failed, hr %#x.\n", hr);
1109     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1110     ok(hr == D3DERR_DEVICELOST, "Reset returned %#x, expected %#x.\n", hr, D3DERR_DEVICELOST);
1111     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1112     ok(hr == D3DERR_DEVICENOTRESET, "TestCooperativeLevel returned %#x, expected %#x.\n", hr, D3DERR_DEVICENOTRESET);
1113     IDirect3DTexture8_Release(texture);
1114     /* Reset again to get the device out of the lost state. */
1115     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1116     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1117     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1118     ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1119
1120     if (caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP)
1121     {
1122         IDirect3DVolumeTexture8 *volume_texture;
1123
1124         hr = IDirect3DDevice8_CreateVolumeTexture(device1, 16, 16, 4, 1, 0,
1125                 D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &volume_texture);
1126         ok(SUCCEEDED(hr), "CreateVolumeTexture failed, hr %#x.\n", hr);
1127         hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1128         ok(hr == D3DERR_DEVICELOST, "Reset returned %#x, expected %#x.\n", hr, D3DERR_INVALIDCALL);
1129         hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1130         ok(hr == D3DERR_DEVICENOTRESET, "TestCooperativeLevel returned %#x, expected %#x.\n",
1131                 hr, D3DERR_DEVICENOTRESET);
1132         IDirect3DVolumeTexture8_Release(volume_texture);
1133         hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1134         ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1135         hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1136         ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1137     }
1138     else
1139     {
1140         skip("Volume textures not supported.\n");
1141     }
1142
1143     /* Scratch, sysmem and managed pool resources are fine. */
1144     hr = IDirect3DDevice8_CreateTexture(device1, 16, 16, 1, 0, D3DFMT_R5G6B5, D3DPOOL_SCRATCH, &texture);
1145     ok(SUCCEEDED(hr), "CreateTexture failed, hr %#x.\n", hr);
1146     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1147     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1148     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1149     ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1150     IDirect3DTexture8_Release(texture);
1151
1152     hr = IDirect3DDevice8_CreateTexture(device1, 16, 16, 1, 0, D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &texture);
1153     ok(SUCCEEDED(hr), "CreateTexture failed, hr %#x.\n", hr);
1154     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1155     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1156     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1157     ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1158     IDirect3DTexture8_Release(texture);
1159
1160     /* The depth stencil should get reset to the auto depth stencil when present. */
1161     hr = IDirect3DDevice8_SetRenderTarget(device1, NULL, NULL);
1162     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1163
1164     hr = IDirect3DDevice8_GetDepthStencilSurface(device1, &surface);
1165     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned %#x, expected %#x.\n", hr, D3DERR_NOTFOUND);
1166     ok(!surface, "Depth / stencil buffer should be NULL.\n");
1167
1168     d3dpp.EnableAutoDepthStencil = TRUE;
1169     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
1170     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1171     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1172
1173     hr = IDirect3DDevice8_GetDepthStencilSurface(device1, &surface);
1174     ok(SUCCEEDED(hr), "GetDepthStencilSurface failed, hr %#x.\n", hr);
1175     ok(!!surface, "Depth / stencil buffer should not be NULL.\n");
1176     if (surface) IDirect3DSurface8_Release(surface);
1177
1178     d3dpp.EnableAutoDepthStencil = FALSE;
1179     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1180     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1181
1182     hr = IDirect3DDevice8_GetDepthStencilSurface(device1, &surface);
1183     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned %#x, expected %#x.\n", hr, D3DERR_NOTFOUND);
1184     ok(!surface, "Depth / stencil buffer should be NULL.\n");
1185
1186     /* Will a sysmem or scratch resource survive while locked? */
1187     hr = IDirect3DDevice8_CreateTexture(device1, 16, 16, 1, 0, D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &texture);
1188     ok(SUCCEEDED(hr), "CreateTexture failed, hr %#x.\n", hr);
1189     hr = IDirect3DTexture8_LockRect(texture, 0, &lockrect, NULL, D3DLOCK_DISCARD);
1190     ok(SUCCEEDED(hr), "LockRect failed, hr %#x.\n", hr);
1191     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1192     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1193     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1194     ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1195     IDirect3DTexture8_UnlockRect(texture, 0);
1196     IDirect3DTexture8_Release(texture);
1197
1198     hr = IDirect3DDevice8_CreateTexture(device1, 16, 16, 1, 0, D3DFMT_R5G6B5, D3DPOOL_SCRATCH, &texture);
1199     ok(SUCCEEDED(hr), "CreateTexture failed, hr %#x.\n", hr);
1200     hr = IDirect3DTexture8_LockRect(texture, 0, &lockrect, NULL, D3DLOCK_DISCARD);
1201     ok(SUCCEEDED(hr), "LockRect failed, hr %#x.\n", hr);
1202     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1203     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1204     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1205     ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1206     IDirect3DTexture8_UnlockRect(texture, 0);
1207     IDirect3DTexture8_Release(texture);
1208
1209     hr = IDirect3DDevice8_CreateTexture(device1, 16, 16, 1, 0, D3DFMT_R5G6B5, D3DPOOL_MANAGED, &texture);
1210     ok(SUCCEEDED(hr), "CreateTexture failed, hr %#x.\n", hr);
1211     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1212     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1213     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1214     ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1215     IDirect3DTexture8_Release(texture);
1216
1217     /* A reference held to an implicit surface causes failures as well. */
1218     hr = IDirect3DDevice8_GetBackBuffer(device1, 0, D3DBACKBUFFER_TYPE_MONO, &surface);
1219     ok(SUCCEEDED(hr), "GetBackBuffer failed, hr %#x.\n", hr);
1220     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1221     ok(hr == D3DERR_DEVICELOST, "Reset returned %#x, expected %#x.\n", hr, D3DERR_DEVICELOST);
1222     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1223     ok(hr == D3DERR_DEVICENOTRESET, "TestCooperativeLevel returned %#x, expected %#x.\n", hr, D3DERR_DEVICENOTRESET);
1224     IDirect3DSurface8_Release(surface);
1225     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1226     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1227     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1228     ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1229
1230     /* Shaders are fine as well. */
1231     hr = IDirect3DDevice8_CreateVertexShader(device1, decl, simple_vs, &shader, 0);
1232     ok(SUCCEEDED(hr), "CreateVertexShader failed, hr %#x.\n", hr);
1233     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1234     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1235     hr = IDirect3DDevice8_DeleteVertexShader(device1, shader);
1236     ok(SUCCEEDED(hr), "DeleteVertexShader failed, hr %#x.\n", hr);
1237
1238     /* Try setting invalid modes. */
1239     memset(&d3dpp, 0, sizeof(d3dpp));
1240     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1241     d3dpp.Windowed = FALSE;
1242     d3dpp.BackBufferWidth = 32;
1243     d3dpp.BackBufferHeight = 32;
1244     d3dpp.BackBufferFormat = d3ddm.Format;
1245     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1246     ok(hr == D3DERR_INVALIDCALL, "Reset returned %#x, expected %#x.\n", hr, D3DERR_INVALIDCALL);
1247     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1248     ok(hr == D3DERR_DEVICENOTRESET, "TestCooperativeLevel returned %#x, expected %#x.\n", hr, D3DERR_DEVICENOTRESET);
1249
1250     memset(&d3dpp, 0, sizeof(d3dpp));
1251     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1252     d3dpp.Windowed = FALSE;
1253     d3dpp.BackBufferWidth = 801;
1254     d3dpp.BackBufferHeight = 600;
1255     d3dpp.BackBufferFormat = d3ddm.Format;
1256     hr = IDirect3DDevice8_Reset(device1, &d3dpp);
1257     ok(hr == D3DERR_INVALIDCALL, "Reset returned %#x, expected %#x.\n", hr, D3DERR_INVALIDCALL);
1258     hr = IDirect3DDevice8_TestCooperativeLevel(device1);
1259     ok(hr == D3DERR_DEVICENOTRESET, "TestCooperativeLevel returned %#x, expected %#x.\n", hr, D3DERR_DEVICENOTRESET);
1260
1261     hr = IDirect3D8_GetAdapterDisplayMode(d3d8, D3DADAPTER_DEFAULT, &d3ddm);
1262     ok(SUCCEEDED(hr), "GetAdapterDisplayMode failed, hr %#x.\n", hr);
1263
1264     memset(&d3dpp, 0, sizeof(d3dpp));
1265     d3dpp.Windowed = TRUE;
1266     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1267     d3dpp.BackBufferFormat = d3ddm.Format;
1268     d3dpp.EnableAutoDepthStencil = FALSE;
1269     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
1270
1271     hr = IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
1272             window, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device2);
1273     if (FAILED(hr))
1274     {
1275         skip("Failed to create device, hr %#x.\n", hr);
1276         goto cleanup;
1277     }
1278
1279     hr = IDirect3DDevice8_TestCooperativeLevel(device2);
1280     ok(SUCCEEDED(hr), "TestCooperativeLevel failed, hr %#x.\n", hr);
1281
1282     d3dpp.Windowed = TRUE;
1283     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1284     d3dpp.BackBufferWidth = 400;
1285     d3dpp.BackBufferHeight = 300;
1286     d3dpp.BackBufferFormat = d3ddm.Format;
1287     d3dpp.EnableAutoDepthStencil = TRUE;
1288     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
1289
1290     hr = IDirect3DDevice8_Reset(device2, &d3dpp);
1291     ok(SUCCEEDED(hr), "Reset failed, hr %#x.\n", hr);
1292     if (FAILED(hr))
1293         goto cleanup;
1294
1295     hr = IDirect3DDevice8_GetDepthStencilSurface(device2, &surface);
1296     ok(SUCCEEDED(hr), "GetDepthStencilSurface failed, hr %#x.\n", hr);
1297     ok(!!surface, "Depth / stencil buffer should not be NULL.\n");
1298     if (surface)
1299         IDirect3DSurface8_Release(surface);
1300
1301 cleanup:
1302     HeapFree(GetProcessHeap(), 0, modes);
1303     if (device2)
1304         IDirect3DDevice8_Release(device2);
1305     if (device1)
1306         IDirect3DDevice8_Release(device1);
1307     if (d3d8)
1308         IDirect3D8_Release(d3d8);
1309     if (window)
1310         DestroyWindow(window);
1311 }
1312
1313 static void test_scene(void)
1314 {
1315     HRESULT                      hr;
1316     HWND                         hwnd               = NULL;
1317     IDirect3D8                  *pD3d               = NULL;
1318     IDirect3DDevice8            *pDevice            = NULL;
1319     D3DPRESENT_PARAMETERS        d3dpp;
1320     D3DDISPLAYMODE               d3ddm;
1321
1322     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
1323     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
1324     hwnd = CreateWindow( "d3d8_test_wc", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1325     ok(hwnd != NULL, "Failed to create window\n");
1326     if (!pD3d || !hwnd) goto cleanup;
1327
1328     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1329     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1330     d3dpp.Windowed         = TRUE;
1331     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1332     d3dpp.BackBufferWidth  = 800;
1333     d3dpp.BackBufferHeight = 600;
1334     d3dpp.BackBufferFormat = d3ddm.Format;
1335
1336
1337     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1338                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1339     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL || broken(hr == D3DERR_NOTAVAILABLE), "IDirect3D8_CreateDevice failed with %#08x\n", hr);
1340     if(!pDevice)
1341     {
1342         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
1343         goto cleanup;
1344     }
1345
1346     /* Test an EndScene without BeginScene. Should return an error */
1347     hr = IDirect3DDevice8_EndScene(pDevice);
1348     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
1349
1350     /* Test a normal BeginScene / EndScene pair, this should work */
1351     hr = IDirect3DDevice8_BeginScene(pDevice);
1352     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
1353     if(SUCCEEDED(hr))
1354     {
1355         hr = IDirect3DDevice8_EndScene(pDevice);
1356         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
1357     }
1358
1359     /* Test another EndScene without having begun a new scene. Should return an error */
1360     hr = IDirect3DDevice8_EndScene(pDevice);
1361     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
1362
1363     /* Two nested BeginScene and EndScene calls */
1364     hr = IDirect3DDevice8_BeginScene(pDevice);
1365     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
1366     hr = IDirect3DDevice8_BeginScene(pDevice);
1367     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
1368     hr = IDirect3DDevice8_EndScene(pDevice);
1369     ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
1370     hr = IDirect3DDevice8_EndScene(pDevice);
1371     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
1372
1373     /* StretchRect does not exit in Direct3D8, so no equivalent to the d3d9 stretchrect tests */
1374
1375 cleanup:
1376     if (pDevice)
1377     {
1378         UINT refcount = IDirect3DDevice8_Release(pDevice);
1379         ok(!refcount, "Device has %u references left.\n", refcount);
1380     }
1381     if (pD3d) IDirect3D8_Release(pD3d);
1382     DestroyWindow(hwnd);
1383 }
1384
1385 static void test_shader(void)
1386 {
1387     HRESULT                      hr;
1388     HWND                         hwnd               = NULL;
1389     IDirect3D8                  *pD3d               = NULL;
1390     IDirect3DDevice8            *pDevice            = NULL;
1391     D3DPRESENT_PARAMETERS        d3dpp;
1392     D3DDISPLAYMODE               d3ddm;
1393     DWORD                        hPixelShader = 0, hVertexShader = 0;
1394     DWORD                        hPixelShader2 = 0, hVertexShader2 = 0;
1395     DWORD                        hTempHandle;
1396     D3DCAPS8                     caps;
1397     DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
1398     DWORD data_size;
1399     void *data;
1400
1401     static DWORD dwVertexDecl[] =
1402     {
1403         D3DVSD_STREAM(0),
1404         D3DVSD_REG(D3DVSDE_POSITION,  D3DVSDT_FLOAT3),
1405         D3DVSD_END()
1406     };
1407     DWORD decl_normal_float2[] =
1408     {
1409         D3DVSD_STREAM(0),
1410         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
1411         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_FLOAT2),  /* D3DVSDE_NORMAL,   Register v1 */
1412         D3DVSD_END()
1413     };
1414     DWORD decl_normal_float4[] =
1415     {
1416         D3DVSD_STREAM(0),
1417         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
1418         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_FLOAT4),  /* D3DVSDE_NORMAL,   Register v1 */
1419         D3DVSD_END()
1420     };
1421     DWORD decl_normal_d3dcolor[] =
1422     {
1423         D3DVSD_STREAM(0),
1424         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
1425         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_D3DCOLOR),/* D3DVSDE_NORMAL,   Register v1 */
1426         D3DVSD_END()
1427     };
1428     const DWORD vertex_decl_size = sizeof(dwVertexDecl);
1429     const DWORD simple_vs_size = sizeof(simple_vs);
1430     const DWORD simple_ps_size = sizeof(simple_ps);
1431
1432     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
1433     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
1434     hwnd = CreateWindow( "d3d8_test_wc", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1435     ok(hwnd != NULL, "Failed to create window\n");
1436     if (!pD3d || !hwnd) goto cleanup;
1437
1438     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1439     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1440     d3dpp.Windowed         = TRUE;
1441     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1442     d3dpp.BackBufferWidth  = 800;
1443     d3dpp.BackBufferHeight = 600;
1444     d3dpp.BackBufferFormat = d3ddm.Format;
1445
1446
1447     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1448                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1449     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL || broken(hr == D3DERR_NOTAVAILABLE), "IDirect3D8_CreateDevice failed with %#08x\n", hr);
1450     if(!pDevice)
1451     {
1452         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
1453         goto cleanup;
1454     }
1455     IDirect3DDevice8_GetDeviceCaps(pDevice, &caps);
1456
1457     /* Test setting and retrieving a FVF */
1458     hr = IDirect3DDevice8_SetVertexShader(pDevice, fvf);
1459     ok(SUCCEEDED(hr), "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1460     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1461     ok(SUCCEEDED(hr), "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1462     ok(hTempHandle == fvf, "Vertex shader %#08x is set, expected %#08x\n", hTempHandle, fvf);
1463
1464     /* First create a vertex shader */
1465     hr = IDirect3DDevice8_SetVertexShader(pDevice, 0);
1466     ok(SUCCEEDED(hr), "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1467     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, simple_vs, &hVertexShader, 0);
1468     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1469     /* Msdn says that the new vertex shader is set immediately. This is wrong, apparently */
1470     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1471     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1472     ok(hTempHandle == 0, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, 0);
1473     /* Assign the shader, then verify that GetVertexShader works */
1474     hr = IDirect3DDevice8_SetVertexShader(pDevice, hVertexShader);
1475     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1476     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1477     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1478     ok(hTempHandle == hVertexShader, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, hVertexShader);
1479     /* Verify that we can retrieve the declaration */
1480     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, NULL, &data_size);
1481     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderDeclaration returned %#08x\n", hr);
1482     ok(data_size == vertex_decl_size, "Got data_size %u, expected %u\n", data_size, vertex_decl_size);
1483     data = HeapAlloc(GetProcessHeap(), 0, vertex_decl_size);
1484     data_size = 1;
1485     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, data, &data_size);
1486     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetVertexShaderDeclaration returned (%#08x), "
1487             "expected D3DERR_INVALIDCALL\n", hr);
1488     ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
1489     data_size = vertex_decl_size;
1490     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, data, &data_size);
1491     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderDeclaration returned %#08x\n", hr);
1492     ok(data_size == vertex_decl_size, "Got data_size %u, expected %u\n", data_size, vertex_decl_size);
1493     ok(!memcmp(data, dwVertexDecl, vertex_decl_size), "data not equal to shader declaration\n");
1494     HeapFree(GetProcessHeap(), 0, data);
1495     /* Verify that we can retrieve the shader function */
1496     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, NULL, &data_size);
1497     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderFunction returned %#08x\n", hr);
1498     ok(data_size == simple_vs_size, "Got data_size %u, expected %u\n", data_size, simple_vs_size);
1499     data = HeapAlloc(GetProcessHeap(), 0, simple_vs_size);
1500     data_size = 1;
1501     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, data, &data_size);
1502     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetVertexShaderFunction returned (%#08x), "
1503             "expected D3DERR_INVALIDCALL\n", hr);
1504     ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
1505     data_size = simple_vs_size;
1506     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, data, &data_size);
1507     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderFunction returned %#08x\n", hr);
1508     ok(data_size == simple_vs_size, "Got data_size %u, expected %u\n", data_size, simple_vs_size);
1509     ok(!memcmp(data, simple_vs, simple_vs_size), "data not equal to shader function\n");
1510     HeapFree(GetProcessHeap(), 0, data);
1511     /* Delete the assigned shader. This is supposed to work */
1512     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1513     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1514     /* The shader should be unset now */
1515     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1516     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1517     ok(hTempHandle == 0, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, 0);
1518
1519     /* Test a broken declaration. 3DMark2001 tries to use normals with 2 components
1520      * First try the fixed function shader function, then a custom one
1521      */
1522     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float2, 0, &hVertexShader, 0);
1523     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1524     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1525     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float4, 0, &hVertexShader, 0);
1526     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1527     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1528     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_d3dcolor, 0, &hVertexShader, 0);
1529     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1530     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1531
1532     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float2, simple_vs, &hVertexShader, 0);
1533     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1534     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1535
1536     if (caps.PixelShaderVersion >= D3DPS_VERSION(1, 0))
1537     {
1538         /* The same with a pixel shader */
1539         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader);
1540         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1541         /* Msdn says that the new pixel shader is set immediately. This is wrong, apparently */
1542         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1543         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1544         ok(hTempHandle == 0, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, 0);
1545         /* Assign the shader, then verify that GetPixelShader works */
1546         hr = IDirect3DDevice8_SetPixelShader(pDevice, hPixelShader);
1547         ok(hr == D3D_OK, "IDirect3DDevice8_SetPixelShader returned %#08x\n", hr);
1548         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1549         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1550         ok(hTempHandle == hPixelShader, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, hPixelShader);
1551         /* Verify that we can retrieve the shader function */
1552         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, NULL, &data_size);
1553         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShaderFunction returned %#08x\n", hr);
1554         ok(data_size == simple_ps_size, "Got data_size %u, expected %u\n", data_size, simple_ps_size);
1555         data = HeapAlloc(GetProcessHeap(), 0, simple_ps_size);
1556         data_size = 1;
1557         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, data, &data_size);
1558         ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetPixelShaderFunction returned (%#08x), "
1559                 "expected D3DERR_INVALIDCALL\n", hr);
1560         ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
1561         data_size = simple_ps_size;
1562         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, data, &data_size);
1563         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShaderFunction returned %#08x\n", hr);
1564         ok(data_size == simple_ps_size, "Got data_size %u, expected %u\n", data_size, simple_ps_size);
1565         ok(!memcmp(data, simple_ps, simple_ps_size), "data not equal to shader function\n");
1566         HeapFree(GetProcessHeap(), 0, data);
1567         /* Delete the assigned shader. This is supposed to work */
1568         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1569         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1570         /* The shader should be unset now */
1571         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1572         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1573         ok(hTempHandle == 0, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, 0);
1574
1575         /* What happens if a non-bound shader is deleted? */
1576         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader);
1577         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1578         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader2);
1579         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1580
1581         hr = IDirect3DDevice8_SetPixelShader(pDevice, hPixelShader);
1582         ok(hr == D3D_OK, "IDirect3DDevice8_SetPixelShader returned %#08x\n", hr);
1583         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader2);
1584         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1585         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1586         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1587         ok(hTempHandle == hPixelShader, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, hPixelShader);
1588         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1589         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1590
1591         /* Check for double delete. */
1592         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader2);
1593         ok(hr == D3DERR_INVALIDCALL || broken(hr == D3D_OK), "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1594         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1595         ok(hr == D3DERR_INVALIDCALL || broken(hr == D3D_OK), "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1596     }
1597     else
1598     {
1599         skip("Pixel shaders not supported\n");
1600     }
1601
1602     /* What happens if a non-bound shader is deleted? */
1603     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, NULL, &hVertexShader, 0);
1604     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1605     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, NULL, &hVertexShader2, 0);
1606     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1607
1608     hr = IDirect3DDevice8_SetVertexShader(pDevice, hVertexShader);
1609     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1610     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader2);
1611     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1612     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1613     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1614     ok(hTempHandle == hVertexShader, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, hVertexShader);
1615     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1616     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1617
1618     /* Check for double delete. */
1619     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader2);
1620     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1621     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1622     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1623
1624 cleanup:
1625     if (pDevice)
1626     {
1627         UINT refcount = IDirect3DDevice8_Release(pDevice);
1628         ok(!refcount, "Device has %u references left.\n", refcount);
1629     }
1630     if (pD3d) IDirect3D8_Release(pD3d);
1631     DestroyWindow(hwnd);
1632 }
1633
1634 static void test_limits(void)
1635 {
1636     HRESULT                      hr;
1637     HWND                         hwnd               = NULL;
1638     IDirect3D8                  *pD3d               = NULL;
1639     IDirect3DDevice8            *pDevice            = NULL;
1640     D3DPRESENT_PARAMETERS        d3dpp;
1641     D3DDISPLAYMODE               d3ddm;
1642     IDirect3DTexture8           *pTexture           = NULL;
1643     int i;
1644
1645     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
1646     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
1647     hwnd = CreateWindow( "d3d8_test_wc", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1648     ok(hwnd != NULL, "Failed to create window\n");
1649     if (!pD3d || !hwnd) goto cleanup;
1650
1651     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1652     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1653     d3dpp.Windowed         = TRUE;
1654     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1655     d3dpp.BackBufferWidth  = 800;
1656     d3dpp.BackBufferHeight = 600;
1657     d3dpp.BackBufferFormat = d3ddm.Format;
1658     d3dpp.EnableAutoDepthStencil = TRUE;
1659     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1660
1661     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1662                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1663     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL || broken(hr == D3DERR_NOTAVAILABLE), "IDirect3D8_CreateDevice failed with %#08x\n", hr);
1664     if(!pDevice)
1665     {
1666         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
1667         goto cleanup;
1668     }
1669
1670     hr = IDirect3DDevice8_CreateTexture(pDevice, 16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture);
1671     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed with %#08x\n", hr);
1672     if(!pTexture) goto cleanup;
1673
1674     /* There are 8 texture stages. We should be able to access all of them */
1675     for(i = 0; i < 8; i++) {
1676         hr = IDirect3DDevice8_SetTexture(pDevice, i, (IDirect3DBaseTexture8 *) pTexture);
1677         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %#08x\n", i, hr);
1678         hr = IDirect3DDevice8_SetTexture(pDevice, i, NULL);
1679         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %#08x\n", i, hr);
1680         hr = IDirect3DDevice8_SetTextureStageState(pDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
1681         ok(hr == D3D_OK, "IDirect3DDevice8_SetTextureStageState for texture %d failed with %#08x\n", i, hr);
1682     }
1683
1684     /* Investigations show that accessing higher textures stage states does not return an error either. Writing
1685      * to too high texture stages(approximately texture 40) causes memory corruption in windows, so there is no
1686      * bounds checking but how do I test that?
1687      */
1688
1689 cleanup:
1690     if(pTexture) IDirect3DTexture8_Release(pTexture);
1691     if (pDevice)
1692     {
1693         UINT refcount = IDirect3DDevice8_Release(pDevice);
1694         ok(!refcount, "Device has %u references left.\n", refcount);
1695     }
1696     if (pD3d) IDirect3D8_Release(pD3d);
1697     DestroyWindow(hwnd);
1698 }
1699
1700 static void test_lights(void)
1701 {
1702     D3DPRESENT_PARAMETERS d3dpp;
1703     IDirect3DDevice8 *device = NULL;
1704     IDirect3D8 *d3d8;
1705     HWND hwnd;
1706     HRESULT hr;
1707     unsigned int i;
1708     BOOL enabled;
1709     D3DCAPS8 caps;
1710     D3DDISPLAYMODE               d3ddm;
1711
1712     d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1713     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1714     hwnd = CreateWindow( "d3d8_test_wc", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1715     ok(hwnd != NULL, "Failed to create window\n");
1716     if (!d3d8 || !hwnd) goto cleanup;
1717
1718     IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &d3ddm );
1719     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1720     d3dpp.Windowed         = TRUE;
1721     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1722     d3dpp.BackBufferWidth  = 800;
1723     d3dpp.BackBufferHeight = 600;
1724     d3dpp.BackBufferFormat = d3ddm.Format;
1725     d3dpp.EnableAutoDepthStencil = TRUE;
1726     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1727
1728     hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1729                                   D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &device );
1730     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1731        "IDirect3D8_CreateDevice failed with %08x\n", hr);
1732     if(!device)
1733     {
1734         skip("Failed to create a d3d device\n");
1735         goto cleanup;
1736     }
1737
1738     memset(&caps, 0, sizeof(caps));
1739     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1740     ok(hr == D3D_OK, "IDirect3DDevice8_GetDeviceCaps failed with %08x\n", hr);
1741
1742     for(i = 1; i <= caps.MaxActiveLights; i++) {
1743         hr = IDirect3DDevice8_LightEnable(device, i, TRUE);
1744         ok(hr == D3D_OK, "Enabling light %u failed with %08x\n", i, hr);
1745         hr = IDirect3DDevice8_GetLightEnable(device, i, &enabled);
1746         ok(hr == D3D_OK || broken(hr == D3DERR_INVALIDCALL),
1747             "GetLightEnable on light %u failed with %08x\n", i, hr);
1748         ok(enabled, "Light %d is %s\n", i, enabled ? "enabled" : "disabled");
1749     }
1750
1751     /* TODO: Test the rendering results in this situation */
1752     hr = IDirect3DDevice8_LightEnable(device, i + 1, TRUE);
1753     ok(hr == D3D_OK, "Enabling one light more than supported returned %08x\n", hr);
1754     hr = IDirect3DDevice8_GetLightEnable(device, i + 1, &enabled);
1755     ok(hr == D3D_OK, "GetLightEnable on light %u failed with %08x\n", i + 1, hr);
1756     ok(enabled, "Light %d is %s\n", i + 1, enabled ? "enabled" : "disabled");
1757     hr = IDirect3DDevice8_LightEnable(device, i + 1, FALSE);
1758     ok(hr == D3D_OK, "Disabling the additional returned %08x\n", hr);
1759
1760     for(i = 1; i <= caps.MaxActiveLights; i++) {
1761         hr = IDirect3DDevice8_LightEnable(device, i, FALSE);
1762         ok(hr == D3D_OK, "Disabling light %u failed with %08x\n", i, hr);
1763     }
1764
1765 cleanup:
1766     if (device)
1767     {
1768         UINT refcount = IDirect3DDevice8_Release(device);
1769         ok(!refcount, "Device has %u references left.\n", refcount);
1770     }
1771     if (d3d8) IDirect3D8_Release(d3d8);
1772     DestroyWindow(hwnd);
1773 }
1774
1775 static void test_render_zero_triangles(void)
1776 {
1777     D3DPRESENT_PARAMETERS d3dpp;
1778     IDirect3DDevice8 *device = NULL;
1779     IDirect3D8 *d3d8;
1780     HWND hwnd;
1781     HRESULT hr;
1782     D3DDISPLAYMODE               d3ddm;
1783
1784     struct nvertex
1785     {
1786         float x, y, z;
1787         float nx, ny, nz;
1788         DWORD diffuse;
1789     }  quad[] =
1790     {
1791         { 0.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1792         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1793         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1794         { 1.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1795     };
1796
1797     d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1798     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1799     hwnd = CreateWindow( "d3d8_test_wc", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1800     ok(hwnd != NULL, "Failed to create window\n");
1801     if (!d3d8 || !hwnd) goto cleanup;
1802
1803     IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &d3ddm );
1804     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1805     d3dpp.Windowed         = TRUE;
1806     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1807     d3dpp.BackBufferWidth  = 800;
1808     d3dpp.BackBufferHeight = 600;
1809     d3dpp.BackBufferFormat = d3ddm.Format;
1810     d3dpp.EnableAutoDepthStencil = TRUE;
1811     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1812
1813     hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1814                                   D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &device );
1815     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1816        "IDirect3D8_CreateDevice failed with %08x\n", hr);
1817     if(!device)
1818     {
1819         skip("Failed to create a d3d device\n");
1820         goto cleanup;
1821     }
1822
1823     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
1824     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1825
1826     hr = IDirect3DDevice8_BeginScene(device);
1827     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
1828     if(hr == D3D_OK)
1829     {
1830         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 0 /* NumVerts */,
1831                                                     0 /*PrimCount */, NULL, D3DFMT_INDEX16, quad, sizeof(quad[0]));
1832         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
1833
1834         IDirect3DDevice8_EndScene(device);
1835         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
1836     }
1837
1838     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1839
1840 cleanup:
1841     if (device)
1842     {
1843         UINT refcount = IDirect3DDevice8_Release(device);
1844         ok(!refcount, "Device has %u references left.\n", refcount);
1845     }
1846     if (d3d8) IDirect3D8_Release(d3d8);
1847     DestroyWindow(hwnd);
1848 }
1849
1850 static void test_depth_stencil_reset(void)
1851 {
1852     D3DPRESENT_PARAMETERS present_parameters;
1853     D3DDISPLAYMODE display_mode;
1854     IDirect3DSurface8 *surface, *orig_rt;
1855     IDirect3DDevice8 *device = NULL;
1856     IDirect3D8 *d3d8;
1857     UINT refcount;
1858     HRESULT hr;
1859     HWND hwnd;
1860
1861     d3d8 = pDirect3DCreate8(D3D_SDK_VERSION);
1862     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1863     hwnd = CreateWindow("d3d8_test_wc", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL);
1864     ok(hwnd != NULL, "Failed to create window\n");
1865     if (!d3d8 || !hwnd) goto cleanup;
1866
1867     IDirect3D8_GetAdapterDisplayMode(d3d8, D3DADAPTER_DEFAULT, &display_mode);
1868     memset(&present_parameters, 0, sizeof(present_parameters));
1869     present_parameters.Windowed               = TRUE;
1870     present_parameters.SwapEffect             = D3DSWAPEFFECT_DISCARD;
1871     present_parameters.BackBufferFormat       = display_mode.Format;
1872     present_parameters.EnableAutoDepthStencil = TRUE;
1873     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1874
1875     hr = IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1876             D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device);
1877     if(FAILED(hr))
1878     {
1879         skip("could not create device, IDirect3D8_CreateDevice returned %#x\n", hr);
1880         goto cleanup;
1881     }
1882
1883     hr = IDirect3DDevice8_GetRenderTarget(device, &orig_rt);
1884     ok(hr == D3D_OK, "GetRenderTarget failed with 0x%08x\n", hr);
1885
1886     hr = IDirect3DDevice8_TestCooperativeLevel(device);
1887     ok(SUCCEEDED(hr), "TestCooperativeLevel failed with %#x\n", hr);
1888
1889     hr = IDirect3DDevice8_SetRenderTarget(device, NULL, NULL);
1890     ok(hr == D3D_OK, "SetRenderTarget failed with 0x%08x\n", hr);
1891
1892     hr = IDirect3DDevice8_GetRenderTarget(device, &surface);
1893     ok(hr == D3D_OK, "GetRenderTarget failed with 0x%08x\n", hr);
1894     ok(surface == orig_rt, "Render target is %p, should be %p\n", surface, orig_rt);
1895     if (surface) IDirect3DSurface8_Release(surface);
1896     IDirect3DSurface8_Release(orig_rt);
1897
1898     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1899     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1900     ok(surface == NULL, "Depth stencil should be NULL\n");
1901
1902     present_parameters.EnableAutoDepthStencil = TRUE;
1903     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1904     hr = IDirect3DDevice8_Reset(device, &present_parameters);
1905     ok(hr == D3D_OK, "Reset failed with 0x%08x\n", hr);
1906
1907     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1908     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1909     ok(surface != NULL, "Depth stencil should not be NULL\n");
1910     if (surface) IDirect3DSurface8_Release(surface);
1911
1912     present_parameters.EnableAutoDepthStencil = FALSE;
1913     hr = IDirect3DDevice8_Reset(device, &present_parameters);
1914     ok(hr == D3D_OK, "Reset failed with 0x%08x\n", hr);
1915
1916     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1917     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1918     ok(surface == NULL, "Depth stencil should be NULL\n");
1919
1920     refcount = IDirect3DDevice8_Release(device);
1921     ok(!refcount, "Device has %u references left.\n", refcount);
1922     device = NULL;
1923
1924     IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &display_mode );
1925
1926     ZeroMemory( &present_parameters, sizeof(present_parameters) );
1927     present_parameters.Windowed         = TRUE;
1928     present_parameters.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1929     present_parameters.BackBufferFormat = display_mode.Format;
1930     present_parameters.EnableAutoDepthStencil = FALSE;
1931     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1932
1933     hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1934                     D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device );
1935
1936     if(FAILED(hr))
1937     {
1938         skip("could not create device, IDirect3D8_CreateDevice returned %#x\n", hr);
1939         goto cleanup;
1940     }
1941
1942     hr = IDirect3DDevice8_TestCooperativeLevel(device);
1943     ok(hr == D3D_OK, "IDirect3DDevice8_TestCooperativeLevel after creation returned %#x\n", hr);
1944
1945     present_parameters.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1946     present_parameters.Windowed         = TRUE;
1947     present_parameters.BackBufferWidth  = 400;
1948     present_parameters.BackBufferHeight = 300;
1949     present_parameters.EnableAutoDepthStencil = TRUE;
1950     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1951
1952     hr = IDirect3DDevice8_Reset(device, &present_parameters);
1953     ok(hr == D3D_OK, "IDirect3DDevice8_Reset failed with 0x%08x\n", hr);
1954
1955     if (FAILED(hr)) goto cleanup;
1956
1957     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1958     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1959     ok(surface != NULL, "Depth stencil should not be NULL\n");
1960     if (surface) IDirect3DSurface8_Release(surface);
1961
1962 cleanup:
1963     if (device)
1964     {
1965         refcount = IDirect3DDevice8_Release(device);
1966         ok(!refcount, "Device has %u references left.\n", refcount);
1967     }
1968     if (d3d8) IDirect3D8_Release(d3d8);
1969     DestroyWindow(hwnd);
1970 }
1971
1972 static HWND filter_messages;
1973
1974 enum message_window
1975 {
1976     DEVICE_WINDOW,
1977     FOCUS_WINDOW,
1978 };
1979
1980 struct message
1981 {
1982     UINT message;
1983     enum message_window window;
1984 };
1985
1986 static const struct message *expect_messages;
1987 static HWND device_window, focus_window;
1988
1989 struct wndproc_thread_param
1990 {
1991     HWND dummy_window;
1992     HANDLE window_created;
1993     HANDLE test_finished;
1994     BOOL running_in_foreground;
1995 };
1996
1997 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
1998 {
1999     if (filter_messages && filter_messages == hwnd)
2000     {
2001         if (message != WM_DISPLAYCHANGE && message != WM_IME_NOTIFY)
2002             todo_wine ok(0, "Received unexpected message %#x for window %p.\n", message, hwnd);
2003     }
2004
2005     if (expect_messages)
2006     {
2007         HWND w;
2008
2009         switch (expect_messages->window)
2010         {
2011             case DEVICE_WINDOW:
2012                 w = device_window;
2013                 break;
2014
2015             case FOCUS_WINDOW:
2016                 w = focus_window;
2017                 break;
2018
2019             default:
2020                 w = NULL;
2021                 break;
2022         };
2023
2024         if (hwnd == w && expect_messages->message == message) ++expect_messages;
2025     }
2026
2027     return DefWindowProcA(hwnd, message, wparam, lparam);
2028 }
2029
2030 static DWORD WINAPI wndproc_thread(void *param)
2031 {
2032     struct wndproc_thread_param *p = param;
2033     DWORD res;
2034     BOOL ret;
2035
2036     p->dummy_window = CreateWindowA("d3d8_test_wndproc_wc", "d3d8_test",
2037             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2038     p->running_in_foreground = SetForegroundWindow(p->dummy_window);
2039
2040     ret = SetEvent(p->window_created);
2041     ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
2042
2043     for (;;)
2044     {
2045         MSG msg;
2046
2047         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
2048         res = WaitForSingleObject(p->test_finished, 100);
2049         if (res == WAIT_OBJECT_0) break;
2050         if (res != WAIT_TIMEOUT)
2051         {
2052             ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2053             break;
2054         }
2055     }
2056
2057     DestroyWindow(p->dummy_window);
2058
2059     return 0;
2060 }
2061
2062 static void test_wndproc(void)
2063 {
2064     struct wndproc_thread_param thread_params;
2065     IDirect3DDevice8 *device;
2066     WNDCLASSA wc = {0};
2067     IDirect3D8 *d3d8;
2068     HANDLE thread;
2069     LONG_PTR proc;
2070     ULONG ref;
2071     DWORD res, tid;
2072     HWND tmp;
2073
2074     static const struct message messages[] =
2075     {
2076         {WM_WINDOWPOSCHANGING,  FOCUS_WINDOW},
2077         {WM_ACTIVATE,           FOCUS_WINDOW},
2078         {WM_SETFOCUS,           FOCUS_WINDOW},
2079         {WM_WINDOWPOSCHANGING,  DEVICE_WINDOW},
2080         {WM_MOVE,               DEVICE_WINDOW},
2081         {WM_SIZE,               DEVICE_WINDOW},
2082         {0,                     0},
2083     };
2084
2085     if (!(d3d8 = pDirect3DCreate8(D3D_SDK_VERSION)))
2086     {
2087         skip("Failed to create IDirect3D8 object, skipping tests.\n");
2088         return;
2089     }
2090
2091     wc.lpfnWndProc = test_proc;
2092     wc.lpszClassName = "d3d8_test_wndproc_wc";
2093     ok(RegisterClassA(&wc), "Failed to register window class.\n");
2094
2095     thread_params.window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
2096     ok(!!thread_params.window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
2097     thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
2098     ok(!!thread_params.test_finished, "CreateEvent failed, last error %#x.\n", GetLastError());
2099
2100     focus_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     device_window = CreateWindowA("d3d8_test_wndproc_wc", "d3d8_test",
2103             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2104     thread = CreateThread(NULL, 0, wndproc_thread, &thread_params, 0, &tid);
2105     ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
2106
2107     res = WaitForSingleObject(thread_params.window_created, INFINITE);
2108     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2109
2110     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2111     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2112             (LONG_PTR)test_proc, proc);
2113     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2114     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2115             (LONG_PTR)test_proc, proc);
2116
2117     trace("device_window %p, focus_window %p, dummy_window %p.\n",
2118             device_window, focus_window, thread_params.dummy_window);
2119
2120     tmp = GetFocus();
2121     ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
2122     if (thread_params.running_in_foreground)
2123     {
2124         tmp = GetForegroundWindow();
2125         ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
2126                 thread_params.dummy_window, tmp);
2127     }
2128     else
2129         skip("Not running in foreground, skip foreground window test\n");
2130
2131     flush_events();
2132
2133     expect_messages = messages;
2134
2135     device = create_device(d3d8, device_window, focus_window, FALSE);
2136     if (!device)
2137     {
2138         skip("Failed to create a D3D device, skipping tests.\n");
2139         goto done;
2140     }
2141
2142     ok(!expect_messages->message, "Expected message %#x for window %#x, but didn't receive it.\n",
2143             expect_messages->message, expect_messages->window);
2144     expect_messages = NULL;
2145
2146     if (0) /* Disabled until we can make this work in a reliable way on Wine. */
2147     {
2148         tmp = GetFocus();
2149         ok(tmp == focus_window, "Expected focus %p, got %p.\n", focus_window, tmp);
2150         tmp = GetForegroundWindow();
2151         ok(tmp == focus_window, "Expected foreground window %p, got %p.\n", focus_window, tmp);
2152     }
2153     SetForegroundWindow(focus_window);
2154     flush_events();
2155
2156     filter_messages = focus_window;
2157
2158     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2159     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2160             (LONG_PTR)test_proc, proc);
2161
2162     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2163     ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2164             (LONG_PTR)test_proc, proc);
2165
2166     ref = IDirect3DDevice8_Release(device);
2167     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2168
2169     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2170     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2171             (LONG_PTR)test_proc, proc);
2172
2173     device = create_device(d3d8, focus_window, focus_window, FALSE);
2174     if (!device)
2175     {
2176         skip("Failed to create a D3D device, skipping tests.\n");
2177         goto done;
2178     }
2179
2180     ref = IDirect3DDevice8_Release(device);
2181     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2182
2183     device = create_device(d3d8, device_window, focus_window, FALSE);
2184     if (!device)
2185     {
2186         skip("Failed to create a D3D device, skipping tests.\n");
2187         goto done;
2188     }
2189
2190     proc = SetWindowLongPtrA(focus_window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2191     ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2192             (LONG_PTR)test_proc, proc);
2193
2194     ref = IDirect3DDevice8_Release(device);
2195     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2196
2197     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2198     ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2199             (LONG_PTR)DefWindowProcA, proc);
2200
2201 done:
2202     filter_messages = NULL;
2203     IDirect3D8_Release(d3d8);
2204
2205     SetEvent(thread_params.test_finished);
2206     WaitForSingleObject(thread, INFINITE);
2207     CloseHandle(thread_params.test_finished);
2208     CloseHandle(thread_params.window_created);
2209     CloseHandle(thread);
2210
2211     DestroyWindow(device_window);
2212     DestroyWindow(focus_window);
2213     UnregisterClassA("d3d8_test_wndproc_wc", GetModuleHandleA(NULL));
2214 }
2215
2216 static void test_wndproc_windowed(void)
2217 {
2218     struct wndproc_thread_param thread_params;
2219     IDirect3DDevice8 *device;
2220     WNDCLASSA wc = {0};
2221     IDirect3D8 *d3d8;
2222     HANDLE thread;
2223     LONG_PTR proc;
2224     HRESULT hr;
2225     ULONG ref;
2226     DWORD res, tid;
2227     HWND tmp;
2228
2229     if (!(d3d8 = pDirect3DCreate8(D3D_SDK_VERSION)))
2230     {
2231         skip("Failed to create IDirect3D8 object, skipping tests.\n");
2232         return;
2233     }
2234
2235     wc.lpfnWndProc = test_proc;
2236     wc.lpszClassName = "d3d8_test_wndproc_wc";
2237     ok(RegisterClassA(&wc), "Failed to register window class.\n");
2238
2239     thread_params.window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
2240     ok(!!thread_params.window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
2241     thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
2242     ok(!!thread_params.test_finished, "CreateEvent failed, last error %#x.\n", GetLastError());
2243
2244     focus_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     device_window = CreateWindowA("d3d8_test_wndproc_wc", "d3d8_test",
2247             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2248     thread = CreateThread(NULL, 0, wndproc_thread, &thread_params, 0, &tid);
2249     ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
2250
2251     res = WaitForSingleObject(thread_params.window_created, INFINITE);
2252     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2253
2254     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2255     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2256             (LONG_PTR)test_proc, proc);
2257     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2258     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2259             (LONG_PTR)test_proc, proc);
2260
2261     trace("device_window %p, focus_window %p, dummy_window %p.\n",
2262             device_window, focus_window, thread_params.dummy_window);
2263
2264     tmp = GetFocus();
2265     ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
2266     if (thread_params.running_in_foreground)
2267     {
2268         tmp = GetForegroundWindow();
2269         ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
2270                 thread_params.dummy_window, tmp);
2271     }
2272     else
2273         skip("Not running in foreground, skip foreground window test\n");
2274
2275     filter_messages = focus_window;
2276
2277     device = create_device(d3d8, device_window, focus_window, TRUE);
2278     if (!device)
2279     {
2280         skip("Failed to create a D3D device, skipping tests.\n");
2281         goto done;
2282     }
2283
2284     tmp = GetFocus();
2285     ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
2286     tmp = GetForegroundWindow();
2287     ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
2288             thread_params.dummy_window, tmp);
2289
2290     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2291     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2292             (LONG_PTR)test_proc, proc);
2293
2294     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2295     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2296             (LONG_PTR)test_proc, proc);
2297
2298     filter_messages = NULL;
2299
2300     hr = reset_device(device, device_window, FALSE);
2301     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2302
2303     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2304     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2305             (LONG_PTR)test_proc, proc);
2306
2307     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2308     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2309             (LONG_PTR)test_proc, proc);
2310
2311     hr = reset_device(device, device_window, TRUE);
2312     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2313
2314     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2315     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2316             (LONG_PTR)test_proc, proc);
2317
2318     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2319     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2320             (LONG_PTR)test_proc, proc);
2321
2322     filter_messages = focus_window;
2323
2324     ref = IDirect3DDevice8_Release(device);
2325     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2326
2327     filter_messages = device_window;
2328
2329     device = create_device(d3d8, focus_window, focus_window, TRUE);
2330     if (!device)
2331     {
2332         skip("Failed to create a D3D device, skipping tests.\n");
2333         goto done;
2334     }
2335
2336     filter_messages = NULL;
2337
2338     hr = reset_device(device, focus_window, FALSE);
2339     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2340
2341     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2342     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2343             (LONG_PTR)test_proc, proc);
2344
2345     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2346     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2347             (LONG_PTR)test_proc, proc);
2348
2349     hr = reset_device(device, focus_window, TRUE);
2350     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2351
2352     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2353     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2354             (LONG_PTR)test_proc, proc);
2355
2356     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2357     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2358             (LONG_PTR)test_proc, proc);
2359
2360     filter_messages = device_window;
2361
2362     ref = IDirect3DDevice8_Release(device);
2363     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2364
2365     device = create_device(d3d8, device_window, focus_window, TRUE);
2366     if (!device)
2367     {
2368         skip("Failed to create a D3D device, skipping tests.\n");
2369         goto done;
2370     }
2371
2372     filter_messages = NULL;
2373
2374     hr = reset_device(device, device_window, FALSE);
2375     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2376
2377     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2378     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2379             (LONG_PTR)test_proc, proc);
2380
2381     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2382     ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2383             (LONG_PTR)test_proc, proc);
2384
2385     hr = reset_device(device, device_window, TRUE);
2386     ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
2387
2388     proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
2389     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2390             (LONG_PTR)test_proc, proc);
2391
2392     proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
2393     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2394             (LONG_PTR)test_proc, proc);
2395
2396     filter_messages = device_window;
2397
2398     ref = IDirect3DDevice8_Release(device);
2399     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2400
2401 done:
2402     filter_messages = NULL;
2403     IDirect3D8_Release(d3d8);
2404
2405     SetEvent(thread_params.test_finished);
2406     WaitForSingleObject(thread, INFINITE);
2407     CloseHandle(thread_params.test_finished);
2408     CloseHandle(thread_params.window_created);
2409     CloseHandle(thread);
2410
2411     DestroyWindow(device_window);
2412     DestroyWindow(focus_window);
2413     UnregisterClassA("d3d8_test_wndproc_wc", GetModuleHandleA(NULL));
2414 }
2415
2416 static inline void set_fpu_cw(WORD cw)
2417 {
2418 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2419     __asm__ volatile ("fnclex");
2420     __asm__ volatile ("fldcw %0" : : "m" (cw));
2421 #endif
2422 }
2423
2424 static inline WORD get_fpu_cw(void)
2425 {
2426     WORD cw = 0;
2427 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2428     __asm__ volatile ("fnstcw %0" : "=m" (cw));
2429 #endif
2430     return cw;
2431 }
2432
2433 static void test_fpu_setup(void)
2434 {
2435 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2436     D3DPRESENT_PARAMETERS present_parameters;
2437     IDirect3DDevice8 *device;
2438     D3DDISPLAYMODE d3ddm;
2439     HWND window = NULL;
2440     IDirect3D8 *d3d8;
2441     HRESULT hr;
2442     WORD cw;
2443
2444     d3d8 = pDirect3DCreate8(D3D_SDK_VERSION);
2445     ok(!!d3d8, "Failed to create a d3d8 object.\n");
2446     if (!d3d8) return;
2447
2448     window = CreateWindowA("d3d8_test_wc", "d3d8_test", WS_CAPTION, 0, 0, screen_width, screen_height, 0, 0, 0, 0);
2449     ok(!!window, "Failed to create a window.\n");
2450     if (!window) goto done;
2451
2452     hr = IDirect3D8_GetAdapterDisplayMode(d3d8, D3DADAPTER_DEFAULT, &d3ddm);
2453     ok(SUCCEEDED(hr), "GetAdapterDisplayMode failed, hr %#x.\n", hr);
2454
2455     memset(&present_parameters, 0, sizeof(present_parameters));
2456     present_parameters.Windowed = TRUE;
2457     present_parameters.hDeviceWindow = window;
2458     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2459     present_parameters.BackBufferFormat = d3ddm.Format;
2460
2461     set_fpu_cw(0xf60);
2462     cw = get_fpu_cw();
2463     ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
2464
2465     hr = IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
2466             D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device);
2467     if (FAILED(hr))
2468     {
2469         skip("Failed to create a device, hr %#x.\n", hr);
2470         set_fpu_cw(0x37f);
2471         goto done;
2472     }
2473
2474     cw = get_fpu_cw();
2475     ok(cw == 0x7f, "cw is %#x, expected 0x7f.\n", cw);
2476
2477     IDirect3DDevice8_Release(device);
2478
2479     cw = get_fpu_cw();
2480     ok(cw == 0x7f, "cw is %#x, expected 0x7f.\n", cw);
2481     set_fpu_cw(0xf60);
2482     cw = get_fpu_cw();
2483     ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
2484
2485     hr = IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
2486             D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &present_parameters, &device);
2487     ok(SUCCEEDED(hr), "CreateDevice failed, hr %#x.\n", hr);
2488
2489     cw = get_fpu_cw();
2490     ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
2491     set_fpu_cw(0x37f);
2492
2493     IDirect3DDevice8_Release(device);
2494
2495 done:
2496     if (window) DestroyWindow(window);
2497     if (d3d8) IDirect3D8_Release(d3d8);
2498 #endif
2499 }
2500
2501 static void test_ApplyStateBlock(void)
2502 {
2503     D3DPRESENT_PARAMETERS d3dpp;
2504     IDirect3DDevice8 *device = NULL;
2505     IDirect3D8 *d3d8;
2506     HWND hwnd;
2507     HRESULT hr;
2508     D3DDISPLAYMODE d3ddm;
2509     DWORD received, token;
2510
2511     d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
2512     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
2513     hwnd = CreateWindow( "d3d8_test_wc", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
2514     ok(hwnd != NULL, "Failed to create window\n");
2515     if (!d3d8 || !hwnd) goto cleanup;
2516
2517     IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &d3ddm );
2518     ZeroMemory( &d3dpp, sizeof(d3dpp) );
2519     d3dpp.Windowed         = TRUE;
2520     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
2521     d3dpp.BackBufferWidth  = 800;
2522     d3dpp.BackBufferHeight  = 600;
2523     d3dpp.BackBufferFormat = d3ddm.Format;
2524     d3dpp.EnableAutoDepthStencil = TRUE;
2525     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
2526
2527     hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
2528                                   D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &device );
2529     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
2530        "IDirect3D8_CreateDevice failed with %#x\n", hr);
2531     if(!device)
2532     {
2533         skip("Failed to create a d3d device\n");
2534         goto cleanup;
2535     }
2536
2537     IDirect3DDevice8_BeginStateBlock(device);
2538     IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, TRUE);
2539     IDirect3DDevice8_EndStateBlock(device, &token);
2540     ok(token, "Received zero stateblock handle.\n");
2541     IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, FALSE);
2542
2543     hr = IDirect3DDevice8_GetRenderState(device, D3DRS_ZENABLE, &received);
2544     ok(hr == D3D_OK, "Expected D3D_OK, received %#x.\n", hr);
2545     ok(!received, "Expected = FALSE, received TRUE.\n");
2546
2547     IDirect3DDevice8_ApplyStateBlock(device, 0);
2548     ok(hr == D3D_OK, "Expected D3D_OK, received %#x.\n", hr);
2549     hr = IDirect3DDevice8_GetRenderState(device, D3DRS_ZENABLE, &received);
2550     ok(hr == D3D_OK, "Expected D3D_OK, received %#x.\n", hr);
2551     ok(!received, "Expected FALSE, received TRUE.\n");
2552
2553     IDirect3DDevice8_ApplyStateBlock(device, token);
2554     ok(hr == D3D_OK, "Expected D3D_OK, received %#x.\n", hr);
2555     hr = IDirect3DDevice8_GetRenderState(device, D3DRS_ZENABLE, &received);
2556     ok(hr == D3D_OK, "Expected D3D_OK, received %#x.\n", hr);
2557     ok(received, "Expected TRUE, received FALSE.\n");
2558
2559     IDirect3DDevice8_DeleteStateBlock(device, token);
2560     IDirect3DDevice8_Release(device);
2561 cleanup:
2562     if (d3d8) IDirect3D8_Release(d3d8);
2563     DestroyWindow(hwnd);
2564 }
2565
2566 static void test_depth_stencil_size(void)
2567 {
2568     IDirect3DDevice8 *device;
2569     IDirect3DSurface8 *ds, *rt, *ds_bigger, *ds_bigger2;
2570     IDirect3DSurface8 *surf;
2571     IDirect3D8 *d3d8;
2572     HRESULT hr;
2573     HWND hwnd;
2574
2575     d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
2576     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
2577     hwnd = CreateWindow( "d3d8_test_wc", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
2578     ok(hwnd != NULL, "Failed to create window\n");
2579     if (!d3d8 || !hwnd) goto cleanup;
2580
2581     device = create_device(d3d8, hwnd, hwnd, TRUE);
2582     if (!device) goto cleanup;
2583
2584     hr = IDirect3DDevice8_CreateRenderTarget(device, 64, 64, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, FALSE, &rt);
2585     ok(SUCCEEDED(hr), "IDirect3DDevice8_CreateRenderTarget failed, hr %#x.\n", hr);
2586     hr = IDirect3DDevice8_CreateDepthStencilSurface(device, 32, 32, D3DFMT_D24X8, D3DMULTISAMPLE_NONE, &ds);
2587     ok(SUCCEEDED(hr), "IDirect3DDevice8_CreateDepthStencilSurface failed, hr %#x.\n", hr);
2588     hr = IDirect3DDevice8_CreateDepthStencilSurface(device, 128, 128, D3DFMT_D24X8, D3DMULTISAMPLE_NONE, &ds_bigger);
2589     ok(SUCCEEDED(hr), "IDirect3DDevice8_CreateDepthStencilSurface failed, hr %#x.\n", hr);
2590     hr = IDirect3DDevice8_CreateDepthStencilSurface(device, 128, 128, D3DFMT_D24X8, D3DMULTISAMPLE_NONE, &ds_bigger2);
2591     ok(SUCCEEDED(hr), "IDirect3DDevice8_CreateDepthStencilSurface failed, hr %#x.\n", hr);
2592
2593     hr = IDirect3DDevice8_SetRenderTarget(device, rt, ds);
2594     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_SetRenderTarget returned %#x, expected D3DERR_INVALIDCALL.\n", hr);
2595     hr = IDirect3DDevice8_SetRenderTarget(device, rt, ds_bigger);
2596     ok(SUCCEEDED(hr), "IDirect3DDevice8_SetRenderTarget failed, hr %#x.\n", hr);
2597
2598     /* try to set the small ds without changing the render target at the same time */
2599     hr = IDirect3DDevice8_SetRenderTarget(device, NULL, ds);
2600     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_SetRenderTarget returned %#x, expected D3DERR_INVALIDCALL.\n", hr);
2601     hr = IDirect3DDevice8_SetRenderTarget(device, NULL, ds_bigger2);
2602     ok(SUCCEEDED(hr), "IDirect3DDevice8_SetRenderTarget failed, hr %#x.\n", hr);
2603
2604     hr = IDirect3DDevice8_GetRenderTarget(device, &surf);
2605     ok(hr == D3D_OK, "IDirect3DDevice8_GetRenderTarget failed, hr %#x.\n", hr);
2606     ok(surf == rt, "The render target is %p, expected %p\n", surf, rt);
2607     IDirect3DSurface8_Release(surf);
2608     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surf);
2609     ok(hr == D3D_OK, "IDirect3DDevice8_GetDepthStencilSurface failed, hr %#x.\n", hr);
2610     ok(surf == ds_bigger2, "The depth stencil is %p, expected %p\n", surf, ds_bigger2);
2611     IDirect3DSurface8_Release(surf);
2612
2613     hr = IDirect3DDevice8_SetRenderTarget(device, NULL, NULL);
2614     ok(SUCCEEDED(hr), "IDirect3DDevice8_SetRenderTarget failed, hr %#x.\n", hr);
2615     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surf);
2616     ok(FAILED(hr), "IDirect3DDevice8_GetDepthStencilSurface should have failed, hr %#x.\n", hr);
2617     ok(surf == NULL, "The depth stencil is %p, expected NULL\n", surf);
2618     if (surf) IDirect3DSurface8_Release(surf);
2619
2620     IDirect3DSurface8_Release(rt);
2621     IDirect3DSurface8_Release(ds);
2622     IDirect3DSurface8_Release(ds_bigger);
2623     IDirect3DSurface8_Release(ds_bigger2);
2624
2625 cleanup:
2626     if (d3d8) IDirect3D8_Release(d3d8);
2627     DestroyWindow(hwnd);
2628 }
2629
2630 static void test_window_style(void)
2631 {
2632     RECT focus_rect, fullscreen_rect, r;
2633     LONG device_style, device_exstyle;
2634     LONG focus_style, focus_exstyle;
2635     LONG style, expected_style;
2636     IDirect3DDevice8 *device;
2637     IDirect3D8 *d3d8;
2638     ULONG ref;
2639
2640
2641     if (!(d3d8 = pDirect3DCreate8(D3D_SDK_VERSION)))
2642     {
2643         skip("Failed to create IDirect3D8 object, skipping tests.\n");
2644         return;
2645     }
2646
2647     focus_window = CreateWindowA("d3d8_test_wc", "d3d8_test", WS_OVERLAPPEDWINDOW,
2648             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
2649     device_window = CreateWindowA("d3d8_test_wc", "d3d8_test", WS_OVERLAPPEDWINDOW,
2650             0, 0, screen_width / 2, screen_height / 2, 0, 0, 0, 0);
2651
2652     device_style = GetWindowLongA(device_window, GWL_STYLE);
2653     device_exstyle = GetWindowLongA(device_window, GWL_EXSTYLE);
2654     focus_style = GetWindowLongA(focus_window, GWL_STYLE);
2655     focus_exstyle = GetWindowLongA(focus_window, GWL_EXSTYLE);
2656
2657     SetRect(&fullscreen_rect, 0, 0, screen_width, screen_height);
2658     GetWindowRect(focus_window, &focus_rect);
2659
2660     device = create_device(d3d8, device_window, focus_window, FALSE);
2661     if (!device)
2662     {
2663         skip("Failed to create a D3D device, skipping tests.\n");
2664         goto done;
2665     }
2666
2667     style = GetWindowLongA(device_window, GWL_STYLE);
2668     expected_style = device_style | WS_VISIBLE;
2669     todo_wine ok(style == expected_style, "Expected device window style %#x, got %#x.\n",
2670             expected_style, style);
2671     style = GetWindowLongA(device_window, GWL_EXSTYLE);
2672     expected_style = device_exstyle | WS_EX_TOPMOST;
2673     todo_wine ok(style == expected_style, "Expected device window extended style %#x, got %#x.\n",
2674             expected_style, style);
2675
2676     style = GetWindowLongA(focus_window, GWL_STYLE);
2677     ok(style == focus_style, "Expected focus window style %#x, got %#x.\n",
2678             focus_style, style);
2679     style = GetWindowLongA(focus_window, GWL_EXSTYLE);
2680     ok(style == focus_exstyle, "Expected focus window extended style %#x, got %#x.\n",
2681             focus_exstyle, style);
2682
2683     GetWindowRect(device_window, &r);
2684     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2685             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2686             r.left, r.top, r.right, r.bottom);
2687     GetClientRect(device_window, &r);
2688     todo_wine ok(!EqualRect(&r, &fullscreen_rect), "Client rect and window rect are equal.\n");
2689     GetWindowRect(focus_window, &r);
2690     ok(EqualRect(&r, &focus_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2691             focus_rect.left, focus_rect.top, focus_rect.right, focus_rect.bottom,
2692             r.left, r.top, r.right, r.bottom);
2693
2694     ref = IDirect3DDevice8_Release(device);
2695     ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
2696
2697 done:
2698     IDirect3D8_Release(d3d8);
2699
2700     DestroyWindow(device_window);
2701     DestroyWindow(focus_window);
2702 }
2703
2704 START_TEST(device)
2705 {
2706     HMODULE d3d8_handle = LoadLibraryA( "d3d8.dll" );
2707     WNDCLASS wc = {0};
2708     if (!d3d8_handle)
2709     {
2710         skip("Could not load d3d8.dll\n");
2711         return;
2712     }
2713
2714     wc.lpfnWndProc = DefWindowProc;
2715     wc.lpszClassName = "d3d8_test_wc";
2716     RegisterClass(&wc);
2717
2718     pDirect3DCreate8 = (void *)GetProcAddress( d3d8_handle, "Direct3DCreate8" );
2719     ok(pDirect3DCreate8 != NULL, "Failed to get address of Direct3DCreate8\n");
2720     if (pDirect3DCreate8)
2721     {
2722         IDirect3D8 *d3d8;
2723         d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
2724         if(!d3d8)
2725         {
2726             skip("could not create D3D8\n");
2727             return;
2728         }
2729         IDirect3D8_Release(d3d8);
2730
2731         screen_width = GetSystemMetrics(SM_CXSCREEN);
2732         screen_height = GetSystemMetrics(SM_CYSCREEN);
2733
2734         test_fpu_setup();
2735         test_display_modes();
2736         test_shader_versions();
2737         test_swapchain();
2738         test_refcount();
2739         test_mipmap_levels();
2740         test_cursor();
2741         test_states();
2742         test_reset();
2743         test_scene();
2744         test_shader();
2745         test_limits();
2746         test_lights();
2747         test_ApplyStateBlock();
2748         test_render_zero_triangles();
2749         test_depth_stencil_reset();
2750         test_wndproc();
2751         test_wndproc_windowed();
2752         test_depth_stencil_size();
2753         test_window_style();
2754     }
2755     UnregisterClassA("d3d8_test_wc", GetModuleHandleA(NULL));
2756 }