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