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