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