user32/tests: Fix the listbox delete test on NT4.
[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 #define CHECK_CALL(r,c,d,rc) \
50     if (SUCCEEDED(r)) {\
51         int tmp1 = get_refcount( (IUnknown *)d ); \
52         int rc_new = rc; \
53         ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
54     } else {\
55         trace("%s failed: %#08x\n", c, r); \
56     }
57
58 #define CHECK_RELEASE(obj,d,rc) \
59     if (obj) { \
60         int tmp1, rc_new = rc; \
61         IUnknown_Release( obj ); \
62         tmp1 = get_refcount( (IUnknown *)d ); \
63         ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
64     }
65
66 #define CHECK_REFCOUNT(obj,rc) \
67     { \
68         int rc_new = rc; \
69         int count = get_refcount( (IUnknown *)obj ); \
70         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
71     }
72
73 #define CHECK_RELEASE_REFCOUNT(obj,rc) \
74     { \
75         int rc_new = rc; \
76         int count = IUnknown_Release( (IUnknown *)obj ); \
77         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
78     }
79
80 #define CHECK_ADDREF_REFCOUNT(obj,rc) \
81     { \
82         int rc_new = rc; \
83         int count = IUnknown_AddRef( (IUnknown *)obj ); \
84         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
85     }
86
87 #define CHECK_SURFACE_CONTAINER(obj,iid,expected) \
88     { \
89         void *container_ptr = (void *)0x1337c0d3; \
90         hr = IDirect3DSurface8_GetContainer(obj, &iid, &container_ptr); \
91         ok(SUCCEEDED(hr) && container_ptr == expected, "GetContainer returned: hr %#08x, container_ptr %p. " \
92             "Expected hr %#08x, container_ptr %p\n", hr, container_ptr, S_OK, expected); \
93         if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr); \
94     }
95
96 static void check_mipmap_levels(IDirect3DDevice8 *device, UINT width, UINT height, UINT count)
97 {
98     IDirect3DBaseTexture8* texture = NULL;
99     HRESULT hr = IDirect3DDevice8_CreateTexture( device, width, height, 0, 0, 
100         D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, (IDirect3DTexture8**) &texture );
101        
102     if (SUCCEEDED(hr)) {
103         DWORD levels = IDirect3DBaseTexture8_GetLevelCount(texture);
104         ok(levels == count, "Invalid level count. Expected %d got %u\n", count, levels);
105     } else 
106         trace("CreateTexture failed: %#08x\n", hr);
107
108     if (texture) IUnknown_Release( texture );
109 }
110
111 static void test_mipmap_levels(void)
112 {
113
114     HRESULT               hr;
115     HWND                  hwnd = NULL;
116
117     IDirect3D8            *pD3d = NULL;
118     IDirect3DDevice8      *pDevice = NULL;
119     D3DPRESENT_PARAMETERS d3dpp;
120     D3DDISPLAYMODE        d3ddm;
121  
122     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
123     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
124     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
125     ok(hwnd != NULL, "Failed to create window\n");
126     if (!pD3d || !hwnd) goto cleanup;
127
128     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
129     ZeroMemory( &d3dpp, sizeof(d3dpp) );
130     d3dpp.Windowed         = TRUE;
131     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
132     d3dpp.BackBufferFormat = d3ddm.Format;
133
134     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
135                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
136     if(FAILED(hr))
137     {
138         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
139         goto cleanup;
140     }
141
142     check_mipmap_levels(pDevice, 32, 32, 6);
143     check_mipmap_levels(pDevice, 256, 1, 9);
144     check_mipmap_levels(pDevice, 1, 256, 9);
145     check_mipmap_levels(pDevice, 1, 1, 1);
146
147     cleanup:
148     if (pD3d)     IUnknown_Release( pD3d );
149     if (pDevice)  IUnknown_Release( pDevice );
150     DestroyWindow( hwnd );
151 }
152
153 static void test_swapchain(void)
154 {
155     HRESULT                      hr;
156     HWND                         hwnd               = NULL;
157     IDirect3D8                  *pD3d               = NULL;
158     IDirect3DDevice8            *pDevice            = NULL;
159     IDirect3DSwapChain8         *swapchain1         = NULL;
160     IDirect3DSwapChain8         *swapchain2         = NULL;
161     IDirect3DSwapChain8         *swapchain3         = NULL;
162     IDirect3DSurface8           *backbuffer         = NULL;
163     D3DPRESENT_PARAMETERS        d3dpp;
164     D3DDISPLAYMODE               d3ddm;
165
166     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
167     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
168     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
169     ok(hwnd != NULL, "Failed to create window\n");
170     if (!pD3d || !hwnd) goto cleanup;
171
172     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
173     ZeroMemory( &d3dpp, sizeof(d3dpp) );
174     d3dpp.Windowed         = TRUE;
175     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
176     d3dpp.BackBufferFormat = d3ddm.Format;
177     d3dpp.BackBufferCount  = 0;
178
179     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
180                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
181     if(FAILED(hr))
182     {
183         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
184         goto cleanup;
185     }
186
187     /* Check if the back buffer count was modified */
188     ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
189
190     /* Create a bunch of swapchains */
191     d3dpp.BackBufferCount = 0;
192     hr = IDirect3DDevice8_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain1);
193     ok(SUCCEEDED(hr), "Failed to create a swapchain (%#08x)\n", hr);
194     ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
195
196     d3dpp.BackBufferCount  = 1;
197     hr = IDirect3DDevice8_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain2);
198     ok(SUCCEEDED(hr), "Failed to create a swapchain (%#08x)\n", hr);
199
200     d3dpp.BackBufferCount  = 2;
201     hr = IDirect3DDevice8_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain3);
202     ok(SUCCEEDED(hr), "Failed to create a swapchain (%#08x)\n", hr);
203     if(SUCCEEDED(hr)) {
204         /* Swapchain 3, created with backbuffercount 2 */
205         backbuffer = (void *) 0xdeadbeef;
206         hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 0, 0, &backbuffer);
207         ok(SUCCEEDED(hr), "Failed to get the 1st back buffer (%#08x)\n", hr);
208         ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
209         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
210
211         backbuffer = (void *) 0xdeadbeef;
212         hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 1, 0, &backbuffer);
213         ok(SUCCEEDED(hr), "Failed to get the 2nd back buffer (%#08x)\n", hr);
214         ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
215         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
216
217         backbuffer = (void *) 0xdeadbeef;
218         hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 2, 0, &backbuffer);
219         ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %#08x\n", hr);
220         ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
221         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
222
223         backbuffer = (void *) 0xdeadbeef;
224         hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 3, 0, &backbuffer);
225         ok(FAILED(hr), "Failed to get the back buffer (%#08x)\n", hr);
226         ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
227         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
228     }
229
230     /* Check the back buffers of the swapchains */
231     /* Swapchain 1, created with backbuffercount 0 */
232     hr = IDirect3DSwapChain8_GetBackBuffer(swapchain1, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
233     ok(SUCCEEDED(hr), "Failed to get the back buffer (%#08x)\n", hr);
234     ok(backbuffer != NULL, "The back buffer is NULL (%#08x)\n", hr);
235     if(backbuffer) IDirect3DSurface8_Release(backbuffer);
236
237     backbuffer = (void *) 0xdeadbeef;
238     hr = IDirect3DSwapChain8_GetBackBuffer(swapchain1, 1, 0, &backbuffer);
239     ok(FAILED(hr), "Failed to get the back buffer (%#08x)\n", hr);
240     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
241     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
242
243     /* Swapchain 2 - created with backbuffercount 1 */
244     backbuffer = (void *) 0xdeadbeef;
245     hr = IDirect3DSwapChain8_GetBackBuffer(swapchain2, 0, 0, &backbuffer);
246     ok(SUCCEEDED(hr), "Failed to get the back buffer (%#08x)\n", hr);
247     ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
248     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
249
250     backbuffer = (void *) 0xdeadbeef;
251     hr = IDirect3DSwapChain8_GetBackBuffer(swapchain2, 1, 0, &backbuffer);
252     ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %#08x\n", hr);
253     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
254     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
255
256     backbuffer = (void *) 0xdeadbeef;
257     hr = IDirect3DSwapChain8_GetBackBuffer(swapchain2, 2, 0, &backbuffer);
258     ok(FAILED(hr), "Failed to get the back buffer (%#08x)\n", hr);
259     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
260     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
261
262     cleanup:
263     if(swapchain1) IDirect3DSwapChain8_Release(swapchain1);
264     if(swapchain2) IDirect3DSwapChain8_Release(swapchain2);
265     if(swapchain3) IDirect3DSwapChain8_Release(swapchain3);
266     if(pDevice) IDirect3DDevice8_Release(pDevice);
267     if(pD3d) IDirect3DDevice8_Release(pD3d);
268     DestroyWindow( hwnd );
269 }
270
271 static void test_refcount(void)
272 {
273     HRESULT                      hr;
274     HWND                         hwnd               = NULL;
275     IDirect3D8                  *pD3d               = NULL;
276     IDirect3DDevice8            *pDevice            = NULL;
277     IDirect3DVertexBuffer8      *pVertexBuffer      = NULL;
278     IDirect3DIndexBuffer8       *pIndexBuffer       = NULL;
279     DWORD                       dVertexShader       = -1;
280     DWORD                       dPixelShader        = -1;
281     IDirect3DCubeTexture8       *pCubeTexture       = NULL;
282     IDirect3DTexture8           *pTexture           = NULL;
283     IDirect3DVolumeTexture8     *pVolumeTexture     = NULL;
284     IDirect3DVolume8            *pVolumeLevel       = NULL;
285     IDirect3DSurface8           *pStencilSurface    = NULL;
286     IDirect3DSurface8           *pImageSurface      = NULL;
287     IDirect3DSurface8           *pRenderTarget      = NULL;
288     IDirect3DSurface8           *pRenderTarget2     = NULL;
289     IDirect3DSurface8           *pRenderTarget3     = NULL;
290     IDirect3DSurface8           *pTextureLevel      = NULL;
291     IDirect3DSurface8           *pBackBuffer        = NULL;
292     DWORD                       dStateBlock         = -1;
293     IDirect3DSwapChain8         *pSwapChain         = NULL;
294     D3DCAPS8                    caps;
295
296     D3DPRESENT_PARAMETERS        d3dpp;
297     D3DDISPLAYMODE               d3ddm;
298     int                          refcount = 0, tmp;
299
300     DWORD decl[] =
301     {
302         D3DVSD_STREAM(0),
303         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
304         D3DVSD_REG(D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR), /* D3DVSDE_DIFFUSE, Register v5 */
305         D3DVSD_END()
306     };
307
308     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
309     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
310     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
311     ok(hwnd != NULL, "Failed to create window\n");
312     if (!pD3d || !hwnd) goto cleanup;
313
314     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
315     ZeroMemory( &d3dpp, sizeof(d3dpp) );
316     d3dpp.Windowed         = TRUE;
317     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
318     d3dpp.BackBufferFormat = d3ddm.Format;
319     d3dpp.EnableAutoDepthStencil = TRUE;
320     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
321
322     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
323                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
324     if(FAILED(hr))
325     {
326         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
327         goto cleanup;
328     }
329     IDirect3DDevice8_GetDeviceCaps(pDevice, &caps);
330
331     refcount = get_refcount( (IUnknown *)pDevice );
332     ok(refcount == 1, "Invalid device RefCount %d\n", refcount);
333
334     /**
335      * Check refcount of implicit surfaces. Findings:
336      *   - the container is the device
337      *   - they hold a reference to the device
338      *   - they are created with a refcount of 0 (Get/Release returns original refcount)
339      *   - they are not freed if refcount reaches 0.
340      *   - the refcount is not forwarded to the container.
341      */
342     hr = IDirect3DDevice8_GetRenderTarget(pDevice, &pRenderTarget);
343     CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
344     if(pRenderTarget)
345     {
346         CHECK_SURFACE_CONTAINER( pRenderTarget, IID_IDirect3DDevice8, pDevice);
347         CHECK_REFCOUNT( pRenderTarget, 1);
348
349         CHECK_ADDREF_REFCOUNT(pRenderTarget, 2);
350         CHECK_REFCOUNT(pDevice, refcount);
351         CHECK_RELEASE_REFCOUNT(pRenderTarget, 1);
352         CHECK_REFCOUNT(pDevice, refcount);
353
354         hr = IDirect3DDevice8_GetRenderTarget(pDevice, &pRenderTarget);
355         CHECK_CALL( hr, "GetRenderTarget", pDevice, refcount);
356         CHECK_REFCOUNT( pRenderTarget, 2);
357         CHECK_RELEASE_REFCOUNT( pRenderTarget, 1);
358         CHECK_RELEASE_REFCOUNT( pRenderTarget, 0);
359         CHECK_REFCOUNT( pDevice, --refcount);
360
361         /* The render target is released with the device, so AddRef with refcount=0 is fine here. */
362         CHECK_ADDREF_REFCOUNT(pRenderTarget, 1);
363         CHECK_REFCOUNT(pDevice, ++refcount);
364         CHECK_RELEASE_REFCOUNT(pRenderTarget, 0);
365         CHECK_REFCOUNT(pDevice, --refcount);
366     }
367
368     /* Render target and back buffer are identical. */
369     hr = IDirect3DDevice8_GetBackBuffer(pDevice, 0, 0, &pBackBuffer);
370     CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
371     if(pBackBuffer)
372     {
373         CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
374         ok(pRenderTarget == pBackBuffer, "RenderTarget=%p and BackBuffer=%p should be the same.\n",
375            pRenderTarget, pBackBuffer);
376         pBackBuffer = NULL;
377     }
378     CHECK_REFCOUNT( pDevice, --refcount);
379
380     hr = IDirect3DDevice8_GetDepthStencilSurface(pDevice, &pStencilSurface);
381     CHECK_CALL( hr, "GetDepthStencilSurface", pDevice, ++refcount);
382     if(pStencilSurface)
383     {
384         CHECK_SURFACE_CONTAINER( pStencilSurface, IID_IDirect3DDevice8, pDevice);
385         CHECK_REFCOUNT( pStencilSurface, 1);
386
387         CHECK_ADDREF_REFCOUNT(pStencilSurface, 2);
388         CHECK_REFCOUNT(pDevice, refcount);
389         CHECK_RELEASE_REFCOUNT(pStencilSurface, 1);
390         CHECK_REFCOUNT(pDevice, refcount);
391
392         CHECK_RELEASE_REFCOUNT( pStencilSurface, 0);
393         CHECK_REFCOUNT( pDevice, --refcount);
394
395         /* The stencil surface is released with the device, so AddRef with refcount=0 is fine here. */
396         CHECK_ADDREF_REFCOUNT(pStencilSurface, 1);
397         CHECK_REFCOUNT(pDevice, ++refcount);
398         CHECK_RELEASE_REFCOUNT(pStencilSurface, 0);
399         CHECK_REFCOUNT(pDevice, --refcount);
400         pStencilSurface = NULL;
401     }
402
403     /* Buffers */
404     hr = IDirect3DDevice8_CreateIndexBuffer( pDevice, 16, 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &pIndexBuffer );
405     CHECK_CALL( hr, "CreateIndexBuffer", pDevice, ++refcount );
406     if(pIndexBuffer)
407     {
408         tmp = get_refcount( (IUnknown *)pIndexBuffer );
409
410         hr = IDirect3DDevice8_SetIndices(pDevice, pIndexBuffer, 0);
411         CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
412         hr = IDirect3DDevice8_SetIndices(pDevice, NULL, 0);
413         CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
414     }
415
416     hr = IDirect3DDevice8_CreateVertexBuffer( pDevice, 16, 0, D3DFVF_XYZ, D3DPOOL_DEFAULT, &pVertexBuffer );
417     CHECK_CALL( hr, "CreateVertexBuffer", pDevice, ++refcount );
418     if(pVertexBuffer)
419     {
420         IDirect3DVertexBuffer8 *pVBuf = (void*)~0;
421         UINT stride = ~0;
422
423         tmp = get_refcount( (IUnknown *)pVertexBuffer );
424
425         hr = IDirect3DDevice8_SetStreamSource(pDevice, 0, pVertexBuffer, 3 * sizeof(float));
426         CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
427         hr = IDirect3DDevice8_SetStreamSource(pDevice, 0, NULL, 0);
428         CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
429
430         hr = IDirect3DDevice8_GetStreamSource(pDevice, 0, &pVBuf, &stride);
431         ok(SUCCEEDED(hr), "GetStreamSource did not succeed with NULL stream!\n");
432         ok(pVBuf==NULL, "pVBuf not NULL (%p)!\n", pVBuf);
433         ok(stride==3*sizeof(float), "stride not 3 floats (got %u)!\n", stride);
434     }
435
436     /* Shaders */
437     hr = IDirect3DDevice8_CreateVertexShader( pDevice, decl, simple_vs, &dVertexShader, 0 );
438     CHECK_CALL( hr, "CreateVertexShader", pDevice, refcount );
439     if (caps.PixelShaderVersion >= D3DPS_VERSION(1, 0))
440     {
441         hr = IDirect3DDevice8_CreatePixelShader( pDevice, simple_ps, &dPixelShader );
442         CHECK_CALL( hr, "CreatePixelShader", pDevice, refcount );
443     }
444     /* Textures */
445     hr = IDirect3DDevice8_CreateTexture( pDevice, 32, 32, 3, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pTexture );
446     CHECK_CALL( hr, "CreateTexture", pDevice, ++refcount );
447     if (pTexture)
448     {
449         tmp = get_refcount( (IUnknown *)pTexture );
450
451         /* SetTexture should not increase refcounts */
452         hr = IDirect3DDevice8_SetTexture(pDevice, 0, (IDirect3DBaseTexture8 *) pTexture);
453         CHECK_CALL( hr, "SetTexture", pTexture, tmp);
454         hr = IDirect3DDevice8_SetTexture(pDevice, 0, NULL);
455         CHECK_CALL( hr, "SetTexture", pTexture, tmp);
456
457         /* This should not increment device refcount */
458         hr = IDirect3DTexture8_GetSurfaceLevel( pTexture, 1, &pTextureLevel );
459         CHECK_CALL( hr, "GetSurfaceLevel", pDevice, refcount );
460         /* But should increment texture's refcount */
461         CHECK_REFCOUNT( pTexture, tmp+1 );
462         /* Because the texture and surface refcount are identical */
463         if (pTextureLevel)
464         {
465             CHECK_REFCOUNT        ( pTextureLevel, tmp+1 );
466             CHECK_ADDREF_REFCOUNT ( pTextureLevel, tmp+2 );
467             CHECK_REFCOUNT        ( pTexture     , tmp+2 );
468             CHECK_RELEASE_REFCOUNT( pTextureLevel, tmp+1 );
469             CHECK_REFCOUNT        ( pTexture     , tmp+1 );
470             CHECK_RELEASE_REFCOUNT( pTexture     , tmp   );
471             CHECK_REFCOUNT        ( pTextureLevel, tmp   );
472         }
473     }
474     if(caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP)
475     {
476         hr = IDirect3DDevice8_CreateCubeTexture( pDevice, 32, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pCubeTexture );
477         CHECK_CALL( hr, "CreateCubeTexture", pDevice, ++refcount );
478     }
479     else
480     {
481         skip("Cube textures not supported\n");
482     }
483     if(caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP)
484     {
485         hr = IDirect3DDevice8_CreateVolumeTexture( pDevice, 32, 32, 2, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pVolumeTexture );
486         CHECK_CALL( hr, "CreateVolumeTexture", pDevice, ++refcount );
487     }
488     else
489     {
490         skip("Volume textures not supported\n");
491     }
492
493     if (pVolumeTexture)
494     {
495         tmp = get_refcount( (IUnknown *)pVolumeTexture );
496
497         /* This should not increment device refcount */
498         hr = IDirect3DVolumeTexture8_GetVolumeLevel(pVolumeTexture, 0, &pVolumeLevel);
499         CHECK_CALL( hr, "GetVolumeLevel", pDevice, refcount );
500         /* But should increment volume texture's refcount */
501         CHECK_REFCOUNT( pVolumeTexture, tmp+1 );
502         /* Because the volume texture and volume refcount are identical */
503         if (pVolumeLevel)
504         {
505             CHECK_REFCOUNT        ( pVolumeLevel  , tmp+1 );
506             CHECK_ADDREF_REFCOUNT ( pVolumeLevel  , tmp+2 );
507             CHECK_REFCOUNT        ( pVolumeTexture, tmp+2 );
508             CHECK_RELEASE_REFCOUNT( pVolumeLevel  , tmp+1 );
509             CHECK_REFCOUNT        ( pVolumeTexture, tmp+1 );
510             CHECK_RELEASE_REFCOUNT( pVolumeTexture, tmp   );
511             CHECK_REFCOUNT        ( pVolumeLevel  , tmp   );
512         }
513     }
514     /* Surfaces */
515     hr = IDirect3DDevice8_CreateDepthStencilSurface( pDevice, 32, 32, D3DFMT_D16, D3DMULTISAMPLE_NONE, &pStencilSurface );
516     CHECK_CALL( hr, "CreateDepthStencilSurface", pDevice, ++refcount );
517     CHECK_REFCOUNT( pStencilSurface, 1);
518     hr = IDirect3DDevice8_CreateImageSurface( pDevice, 32, 32, D3DFMT_X8R8G8B8, &pImageSurface );
519     CHECK_CALL( hr, "CreateImageSurface", pDevice, ++refcount );
520     CHECK_REFCOUNT( pImageSurface, 1);
521     hr = IDirect3DDevice8_CreateRenderTarget( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, TRUE, &pRenderTarget3 );
522     CHECK_CALL( hr, "CreateRenderTarget", pDevice, ++refcount );
523     CHECK_REFCOUNT( pRenderTarget3, 1);
524     /* Misc */
525     hr = IDirect3DDevice8_CreateStateBlock( pDevice, D3DSBT_ALL, &dStateBlock );
526     CHECK_CALL( hr, "CreateStateBlock", pDevice, refcount );
527     hr = IDirect3DDevice8_CreateAdditionalSwapChain( pDevice, &d3dpp, &pSwapChain );
528     CHECK_CALL( hr, "CreateAdditionalSwapChain", pDevice, ++refcount );
529     if(pSwapChain)
530     {
531         /* check implicit back buffer */
532         hr = IDirect3DSwapChain8_GetBackBuffer(pSwapChain, 0, 0, &pBackBuffer);
533         CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
534         CHECK_REFCOUNT( pSwapChain, 1);
535         if(pBackBuffer)
536         {
537             CHECK_SURFACE_CONTAINER( pBackBuffer, IID_IDirect3DDevice8, pDevice);
538             CHECK_REFCOUNT( pBackBuffer, 1);
539             CHECK_RELEASE_REFCOUNT( pBackBuffer, 0);
540             CHECK_REFCOUNT( pDevice, --refcount);
541
542             /* The back buffer is released with the swapchain, so AddRef with refcount=0 is fine here. */
543             CHECK_ADDREF_REFCOUNT(pBackBuffer, 1);
544             CHECK_REFCOUNT(pDevice, ++refcount);
545             CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
546             CHECK_REFCOUNT(pDevice, --refcount);
547             pBackBuffer = NULL;
548         }
549         CHECK_REFCOUNT( pSwapChain, 1);
550     }
551
552     if(pVertexBuffer)
553     {
554         BYTE *data;
555         /* Vertex buffers can be locked multiple times */
556         hr = IDirect3DVertexBuffer8_Lock(pVertexBuffer, 0, 0, &data, 0);
557         ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Lock failed with %#08x\n", hr);
558         hr = IDirect3DVertexBuffer8_Lock(pVertexBuffer, 0, 0, &data, 0);
559         ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Lock failed with %#08x\n", hr);
560         hr = IDirect3DVertexBuffer8_Unlock(pVertexBuffer);
561         ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Unlock failed with %#08x\n", hr);
562         hr = IDirect3DVertexBuffer8_Unlock(pVertexBuffer);
563         ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Unlock failed with %#08x\n", hr);
564     }
565
566     /* The implicit render target is not freed if refcount reaches 0.
567      * Otherwise GetRenderTarget would re-allocate it and the pointer would change.*/
568     hr = IDirect3DDevice8_GetRenderTarget(pDevice, &pRenderTarget2);
569     CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
570     if(pRenderTarget2)
571     {
572         CHECK_RELEASE_REFCOUNT(pRenderTarget2, 0);
573         ok(pRenderTarget == pRenderTarget2, "RenderTarget=%p and RenderTarget2=%p should be the same.\n",
574            pRenderTarget, pRenderTarget2);
575         CHECK_REFCOUNT( pDevice, --refcount);
576         pRenderTarget2 = NULL;
577     }
578     pRenderTarget = NULL;
579
580 cleanup:
581     CHECK_RELEASE(pDevice,              pDevice, --refcount);
582
583     /* Buffers */
584     CHECK_RELEASE(pVertexBuffer,        pDevice, --refcount);
585     CHECK_RELEASE(pIndexBuffer,         pDevice, --refcount);
586     /* Shaders */
587     if (dVertexShader != ~0U) IDirect3DDevice8_DeleteVertexShader( pDevice, dVertexShader );
588     if (dPixelShader != ~0U) IDirect3DDevice8_DeletePixelShader( pDevice, dPixelShader );
589     /* Textures */
590     CHECK_RELEASE(pTexture,             pDevice, --refcount);
591     CHECK_RELEASE(pCubeTexture,         pDevice, --refcount);
592     CHECK_RELEASE(pVolumeTexture,       pDevice, --refcount);
593     /* Surfaces */
594     CHECK_RELEASE(pStencilSurface,      pDevice, --refcount);
595     CHECK_RELEASE(pImageSurface,        pDevice, --refcount);
596     CHECK_RELEASE(pRenderTarget3,       pDevice, --refcount);
597     /* Misc */
598     if (dStateBlock != ~0U) IDirect3DDevice8_DeleteStateBlock( pDevice, dStateBlock );
599     /* This will destroy device - cannot check the refcount here */
600     if (pSwapChain)           CHECK_RELEASE_REFCOUNT( pSwapChain, 0);
601
602     if (pD3d)                 CHECK_RELEASE_REFCOUNT( pD3d, 0);
603
604     DestroyWindow( hwnd );
605 }
606
607 static void test_cursor(void)
608 {
609     HRESULT                      hr;
610     HWND                         hwnd               = NULL;
611     IDirect3D8                  *pD3d               = NULL;
612     IDirect3DDevice8            *pDevice            = NULL;
613     D3DPRESENT_PARAMETERS        d3dpp;
614     D3DDISPLAYMODE               d3ddm;
615     CURSORINFO                   info;
616     IDirect3DSurface8 *cursor = NULL;
617     HCURSOR cur;
618     HMODULE user32_handle = GetModuleHandleA("user32.dll");
619
620     pGetCursorInfo = (void *)GetProcAddress(user32_handle, "GetCursorInfo");
621     if (!pGetCursorInfo)
622     {
623         win_skip("GetCursorInfo is not available\n");
624         return;
625     }
626
627     memset(&info, 0, sizeof(info));
628     info.cbSize = sizeof(info);
629     ok(pGetCursorInfo(&info), "GetCursorInfo failed\n");
630     cur = info.hCursor;
631
632     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
633     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
634     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
635     ok(hwnd != NULL, "Failed to create window\n");
636     if (!pD3d || !hwnd) goto cleanup;
637
638     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
639     ZeroMemory( &d3dpp, sizeof(d3dpp) );
640     d3dpp.Windowed         = TRUE;
641     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
642     d3dpp.BackBufferFormat = d3ddm.Format;
643
644     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
645                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
646     if(FAILED(hr))
647     {
648         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
649         goto cleanup;
650     }
651
652     IDirect3DDevice8_CreateImageSurface(pDevice, 32, 32, D3DFMT_A8R8G8B8, &cursor);
653     ok(cursor != NULL, "IDirect3DDevice8_CreateOffscreenPlainSurface failed with %#08x\n", hr);
654
655     /* Initially hidden */
656     hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
657     ok(hr == FALSE, "IDirect3DDevice8_ShowCursor returned %#08x\n", hr);
658
659     /* Not enabled without a surface*/
660     hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
661     ok(hr == FALSE, "IDirect3DDevice8_ShowCursor returned %#08x\n", hr);
662
663     /* Fails */
664     hr = IDirect3DDevice8_SetCursorProperties(pDevice, 0, 0, NULL);
665     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_SetCursorProperties returned %#08x\n", hr);
666
667     hr = IDirect3DDevice8_SetCursorProperties(pDevice, 0, 0, cursor);
668     ok(hr == D3D_OK, "IDirect3DDevice8_SetCursorProperties returned %#08x\n", hr);
669
670     IDirect3DSurface8_Release(cursor);
671
672     memset(&info, 0, sizeof(info));
673     info.cbSize = sizeof(info);
674     ok(pGetCursorInfo(&info), "GetCursorInfo failed\n");
675     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
676     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
677
678     /* Still hidden */
679     hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
680     ok(hr == FALSE, "IDirect3DDevice8_ShowCursor returned %#08x\n", hr);
681
682     /* Enabled now*/
683     hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
684     ok(hr == TRUE, "IDirect3DDevice8_ShowCursor returned %#08x\n", hr);
685
686     /* GDI cursor unchanged */
687     memset(&info, 0, sizeof(info));
688     info.cbSize = sizeof(info);
689     ok(pGetCursorInfo(&info), "GetCursorInfo failed\n");
690     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
691     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
692
693 cleanup:
694     if(pD3d) IDirect3D8_Release(pD3d);
695     if(pDevice) IDirect3D8_Release(pDevice);
696 }
697
698 static void test_states(void)
699 {
700     HRESULT                      hr;
701     HWND                         hwnd               = NULL;
702     IDirect3D8                  *pD3d               = NULL;
703     IDirect3DDevice8            *pDevice            = NULL;
704     D3DPRESENT_PARAMETERS        d3dpp;
705     D3DDISPLAYMODE               d3ddm;
706
707     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
708     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
709     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
710     ok(hwnd != NULL, "Failed to create window\n");
711     if (!pD3d || !hwnd) goto cleanup;
712
713     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
714     ZeroMemory( &d3dpp, sizeof(d3dpp) );
715     d3dpp.Windowed         = TRUE;
716     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
717     d3dpp.BackBufferWidth  = 640;
718     d3dpp.BackBufferHeight = 480;
719     d3dpp.BackBufferFormat = d3ddm.Format;
720
721     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
722                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
723     if(FAILED(hr))
724     {
725         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
726         goto cleanup;
727     }
728
729     hr = IDirect3DDevice8_SetRenderState(pDevice, D3DRS_ZVISIBLE, TRUE);
730     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState(D3DRS_ZVISIBLE, TRUE) returned %#08x\n", hr);
731     hr = IDirect3DDevice8_SetRenderState(pDevice, D3DRS_ZVISIBLE, FALSE);
732     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState(D3DRS_ZVISIBLE, FALSE) returned %#08x\n", hr);
733
734 cleanup:
735     if(pD3d) IDirect3D8_Release(pD3d);
736     if(pDevice) IDirect3D8_Release(pDevice);
737 }
738
739 static void test_shader_versions(void)
740 {
741     HRESULT                      hr;
742     IDirect3D8                  *pD3d               = NULL;
743     D3DCAPS8                     d3dcaps;
744
745     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
746     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
747     if (pD3d != NULL) {
748         hr = IDirect3D8_GetDeviceCaps(pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dcaps);
749         ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "Failed to get D3D8 caps (%#08x)\n", hr);
750         if (SUCCEEDED(hr)) {
751             ok(d3dcaps.VertexShaderVersion <= D3DVS_VERSION(1,1), "Unexpected VertexShaderVersion (%#x > %#x)\n", d3dcaps.VertexShaderVersion, D3DVS_VERSION(1,1));
752             ok(d3dcaps.PixelShaderVersion <= D3DPS_VERSION(1,4), "Unexpected PixelShaderVersion (%#x > %#x)\n", d3dcaps.PixelShaderVersion, D3DPS_VERSION(1,4));
753         } else {
754             skip("No Direct3D support\n");
755         }
756         IDirect3D8_Release(pD3d);
757     }
758 }
759
760
761 /* Test adapter display modes */
762 static void test_display_modes(void)
763 {
764     UINT max_modes, i;
765     D3DDISPLAYMODE dmode;
766     HRESULT res;
767     IDirect3D8 *pD3d;
768
769     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
770     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
771     if(!pD3d) return;
772
773     max_modes = IDirect3D8_GetAdapterModeCount(pD3d, D3DADAPTER_DEFAULT);
774     ok(max_modes > 0 ||
775        broken(max_modes == 0), /* VMware */
776        "GetAdapterModeCount(D3DADAPTER_DEFAULT) returned 0!\n");
777
778     for(i=0; i<max_modes;i++) {
779         res = IDirect3D8_EnumAdapterModes(pD3d, D3DADAPTER_DEFAULT, i, &dmode);
780         ok(res==D3D_OK, "EnumAdapterModes returned %#08x for mode %u!\n", res, i);
781         if(res != D3D_OK)
782             continue;
783
784         ok(dmode.Format==D3DFMT_X8R8G8B8 || dmode.Format==D3DFMT_R5G6B5,
785            "Unexpected display mode returned for mode %u: %#x\n", i , dmode.Format);
786     }
787
788     IDirect3D8_Release(pD3d);
789 }
790
791 static void test_scene(void)
792 {
793     HRESULT                      hr;
794     HWND                         hwnd               = NULL;
795     IDirect3D8                  *pD3d               = NULL;
796     IDirect3DDevice8            *pDevice            = NULL;
797     D3DPRESENT_PARAMETERS        d3dpp;
798     D3DDISPLAYMODE               d3ddm;
799
800     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
801     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
802     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
803     ok(hwnd != NULL, "Failed to create window\n");
804     if (!pD3d || !hwnd) goto cleanup;
805
806     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
807     ZeroMemory( &d3dpp, sizeof(d3dpp) );
808     d3dpp.Windowed         = TRUE;
809     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
810     d3dpp.BackBufferWidth  = 800;
811     d3dpp.BackBufferHeight = 600;
812     d3dpp.BackBufferFormat = d3ddm.Format;
813
814
815     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
816                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
817     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL || broken(hr == D3DERR_NOTAVAILABLE), "IDirect3D8_CreateDevice failed with %#08x\n", hr);
818     if(!pDevice)
819     {
820         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
821         goto cleanup;
822     }
823
824     /* Test an EndScene without beginscene. Should return an error */
825     hr = IDirect3DDevice8_EndScene(pDevice);
826     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
827
828     /* Test a normal BeginScene / EndScene pair, this should work */
829     hr = IDirect3DDevice8_BeginScene(pDevice);
830     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
831     if(SUCCEEDED(hr))
832     {
833         hr = IDirect3DDevice8_EndScene(pDevice);
834         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
835     }
836
837     /* Test another EndScene without having begun a new scene. Should return an error */
838     hr = IDirect3DDevice8_EndScene(pDevice);
839     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
840
841     /* Two nested BeginScene and EndScene calls */
842     hr = IDirect3DDevice8_BeginScene(pDevice);
843     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
844     hr = IDirect3DDevice8_BeginScene(pDevice);
845     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
846     hr = IDirect3DDevice8_EndScene(pDevice);
847     ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
848     hr = IDirect3DDevice8_EndScene(pDevice);
849     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
850
851     /* StretchRect does not exit in Direct3D8, so no equivalent to the d3d9 stretchrect tests */
852
853 cleanup:
854     if(pD3d) IDirect3D8_Release(pD3d);
855     if(pDevice) IDirect3D8_Release(pDevice);
856     if(hwnd) DestroyWindow(hwnd);
857 }
858
859 static void test_shader(void)
860 {
861     HRESULT                      hr;
862     HWND                         hwnd               = NULL;
863     IDirect3D8                  *pD3d               = NULL;
864     IDirect3DDevice8            *pDevice            = NULL;
865     D3DPRESENT_PARAMETERS        d3dpp;
866     D3DDISPLAYMODE               d3ddm;
867     DWORD                        hPixelShader = 0, hVertexShader = 0;
868     DWORD                        hPixelShader2 = 0, hVertexShader2 = 0;
869     DWORD                        hTempHandle;
870     D3DCAPS8                     caps;
871     DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
872     DWORD data_size;
873     void *data;
874
875     static DWORD dwVertexDecl[] =
876     {
877         D3DVSD_STREAM(0),
878         D3DVSD_REG(D3DVSDE_POSITION,  D3DVSDT_FLOAT3),
879         D3DVSD_END()
880     };
881     DWORD decl_normal_float2[] =
882     {
883         D3DVSD_STREAM(0),
884         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
885         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_FLOAT2),  /* D3DVSDE_NORMAL,   Register v1 */
886         D3DVSD_END()
887     };
888     DWORD decl_normal_float4[] =
889     {
890         D3DVSD_STREAM(0),
891         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
892         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_FLOAT4),  /* D3DVSDE_NORMAL,   Register v1 */
893         D3DVSD_END()
894     };
895     DWORD decl_normal_d3dcolor[] =
896     {
897         D3DVSD_STREAM(0),
898         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
899         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_D3DCOLOR),/* D3DVSDE_NORMAL,   Register v1 */
900         D3DVSD_END()
901     };
902     const DWORD vertex_decl_size = sizeof(dwVertexDecl);
903     const DWORD simple_vs_size = sizeof(simple_vs);
904     const DWORD simple_ps_size = sizeof(simple_ps);
905
906     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
907     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
908     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
909     ok(hwnd != NULL, "Failed to create window\n");
910     if (!pD3d || !hwnd) goto cleanup;
911
912     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
913     ZeroMemory( &d3dpp, sizeof(d3dpp) );
914     d3dpp.Windowed         = TRUE;
915     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
916     d3dpp.BackBufferWidth  = 800;
917     d3dpp.BackBufferHeight = 600;
918     d3dpp.BackBufferFormat = d3ddm.Format;
919
920
921     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
922                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
923     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL || broken(hr == D3DERR_NOTAVAILABLE), "IDirect3D8_CreateDevice failed with %#08x\n", hr);
924     if(!pDevice)
925     {
926         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
927         goto cleanup;
928     }
929     IDirect3DDevice8_GetDeviceCaps(pDevice, &caps);
930
931     /* Test setting and retrieving a FVF */
932     hr = IDirect3DDevice8_SetVertexShader(pDevice, fvf);
933     ok(SUCCEEDED(hr), "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
934     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
935     ok(SUCCEEDED(hr), "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
936     ok(hTempHandle == fvf, "Vertex shader %#08x is set, expected %#08x\n", hTempHandle, fvf);
937
938     /* First create a vertex shader */
939     hr = IDirect3DDevice8_SetVertexShader(pDevice, 0);
940     ok(SUCCEEDED(hr), "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
941     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, simple_vs, &hVertexShader, 0);
942     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
943     /* Msdn says that the new vertex shader is set immediately. This is wrong, apparently */
944     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
945     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
946     ok(hTempHandle == 0, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, 0);
947     /* Assign the shader, then verify that GetVertexShader works */
948     hr = IDirect3DDevice8_SetVertexShader(pDevice, hVertexShader);
949     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
950     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
951     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
952     ok(hTempHandle == hVertexShader, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, hVertexShader);
953     /* Verify that we can retrieve the declaration */
954     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, NULL, &data_size);
955     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderDeclaration returned %#08x\n", hr);
956     ok(data_size == vertex_decl_size, "Got data_size %u, expected %u\n", data_size, vertex_decl_size);
957     data = HeapAlloc(GetProcessHeap(), 0, vertex_decl_size);
958     data_size = 1;
959     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, data, &data_size);
960     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetVertexShaderDeclaration returned (%#08x), "
961             "expected D3DERR_INVALIDCALL\n", hr);
962     ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
963     data_size = vertex_decl_size;
964     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, data, &data_size);
965     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderDeclaration returned %#08x\n", hr);
966     ok(data_size == vertex_decl_size, "Got data_size %u, expected %u\n", data_size, vertex_decl_size);
967     ok(!memcmp(data, dwVertexDecl, vertex_decl_size), "data not equal to shader declaration\n");
968     HeapFree(GetProcessHeap(), 0, data);
969     /* Verify that we can retrieve the shader function */
970     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, NULL, &data_size);
971     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderFunction returned %#08x\n", hr);
972     ok(data_size == simple_vs_size, "Got data_size %u, expected %u\n", data_size, simple_vs_size);
973     data = HeapAlloc(GetProcessHeap(), 0, simple_vs_size);
974     data_size = 1;
975     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, data, &data_size);
976     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetVertexShaderFunction returned (%#08x), "
977             "expected D3DERR_INVALIDCALL\n", hr);
978     ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
979     data_size = simple_vs_size;
980     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, data, &data_size);
981     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderFunction returned %#08x\n", hr);
982     ok(data_size == simple_vs_size, "Got data_size %u, expected %u\n", data_size, simple_vs_size);
983     ok(!memcmp(data, simple_vs, simple_vs_size), "data not equal to shader function\n");
984     HeapFree(GetProcessHeap(), 0, data);
985     /* Delete the assigned shader. This is supposed to work */
986     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
987     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
988     /* The shader should be unset now */
989     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
990     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
991     ok(hTempHandle == 0, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, 0);
992
993     /* Test a broken declaration. 3DMark2001 tries to use normals with 2 components
994      * First try the fixed function shader function, then a custom one
995      */
996     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float2, 0, &hVertexShader, 0);
997     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
998     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
999     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float4, 0, &hVertexShader, 0);
1000     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1001     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1002     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_d3dcolor, 0, &hVertexShader, 0);
1003     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1004     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1005
1006     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float2, simple_vs, &hVertexShader, 0);
1007     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1008     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1009
1010     if (caps.PixelShaderVersion >= D3DPS_VERSION(1, 0))
1011     {
1012         /* The same with a pixel shader */
1013         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader);
1014         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1015         /* Msdn says that the new pixel shader is set immediately. This is wrong, apparently */
1016         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1017         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1018         ok(hTempHandle == 0, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, 0);
1019         /* Assign the shader, then verify that GetPixelShader works */
1020         hr = IDirect3DDevice8_SetPixelShader(pDevice, hPixelShader);
1021         ok(hr == D3D_OK, "IDirect3DDevice8_SetPixelShader returned %#08x\n", hr);
1022         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1023         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1024         ok(hTempHandle == hPixelShader, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, hPixelShader);
1025         /* Verify that we can retrieve the shader function */
1026         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, NULL, &data_size);
1027         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShaderFunction returned %#08x\n", hr);
1028         ok(data_size == simple_ps_size, "Got data_size %u, expected %u\n", data_size, simple_ps_size);
1029         data = HeapAlloc(GetProcessHeap(), 0, simple_ps_size);
1030         data_size = 1;
1031         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, data, &data_size);
1032         ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetPixelShaderFunction returned (%#08x), "
1033                 "expected D3DERR_INVALIDCALL\n", hr);
1034         ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
1035         data_size = simple_ps_size;
1036         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, data, &data_size);
1037         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShaderFunction returned %#08x\n", hr);
1038         ok(data_size == simple_ps_size, "Got data_size %u, expected %u\n", data_size, simple_ps_size);
1039         ok(!memcmp(data, simple_ps, simple_ps_size), "data not equal to shader function\n");
1040         HeapFree(GetProcessHeap(), 0, data);
1041         /* Delete the assigned shader. This is supposed to work */
1042         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1043         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1044         /* The shader should be unset now */
1045         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1046         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1047         ok(hTempHandle == 0, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, 0);
1048
1049         /* What happens if a non-bound shader is deleted? */
1050         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader);
1051         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1052         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader2);
1053         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1054
1055         hr = IDirect3DDevice8_SetPixelShader(pDevice, hPixelShader);
1056         ok(hr == D3D_OK, "IDirect3DDevice8_SetPixelShader returned %#08x\n", hr);
1057         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader2);
1058         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1059         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1060         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1061         ok(hTempHandle == hPixelShader, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, hPixelShader);
1062         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1063         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1064     }
1065     else
1066     {
1067         skip("Pixel shaders not supported\n");
1068     }
1069
1070     /* What happens if a non-bound shader is deleted? */
1071     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, NULL, &hVertexShader, 0);
1072     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1073     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, NULL, &hVertexShader2, 0);
1074     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1075
1076     hr = IDirect3DDevice8_SetVertexShader(pDevice, hVertexShader);
1077     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1078     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader2);
1079     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1080     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1081     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1082     ok(hTempHandle == hVertexShader, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, hVertexShader);
1083     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1084     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1085
1086 cleanup:
1087     if(pD3d) IDirect3D8_Release(pD3d);
1088     if(pDevice) IDirect3D8_Release(pDevice);
1089     if(hwnd) DestroyWindow(hwnd);
1090 }
1091
1092 static void test_limits(void)
1093 {
1094     HRESULT                      hr;
1095     HWND                         hwnd               = NULL;
1096     IDirect3D8                  *pD3d               = NULL;
1097     IDirect3DDevice8            *pDevice            = NULL;
1098     D3DPRESENT_PARAMETERS        d3dpp;
1099     D3DDISPLAYMODE               d3ddm;
1100     IDirect3DTexture8           *pTexture           = NULL;
1101     int i;
1102
1103     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
1104     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
1105     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1106     ok(hwnd != NULL, "Failed to create window\n");
1107     if (!pD3d || !hwnd) goto cleanup;
1108
1109     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1110     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1111     d3dpp.Windowed         = TRUE;
1112     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1113     d3dpp.BackBufferWidth  = 800;
1114     d3dpp.BackBufferHeight = 600;
1115     d3dpp.BackBufferFormat = d3ddm.Format;
1116     d3dpp.EnableAutoDepthStencil = TRUE;
1117     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1118
1119     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1120                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1121     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL || broken(hr == D3DERR_NOTAVAILABLE), "IDirect3D8_CreateDevice failed with %#08x\n", hr);
1122     if(!pDevice)
1123     {
1124         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
1125         goto cleanup;
1126     }
1127
1128     hr = IDirect3DDevice8_CreateTexture(pDevice, 16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture);
1129     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed with %#08x\n", hr);
1130     if(!pTexture) goto cleanup;
1131
1132     /* There are 8 texture stages. We should be able to access all of them */
1133     for(i = 0; i < 8; i++) {
1134         hr = IDirect3DDevice8_SetTexture(pDevice, i, (IDirect3DBaseTexture8 *) pTexture);
1135         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %#08x\n", i, hr);
1136         hr = IDirect3DDevice8_SetTexture(pDevice, i, NULL);
1137         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %#08x\n", i, hr);
1138         hr = IDirect3DDevice8_SetTextureStageState(pDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
1139         ok(hr == D3D_OK, "IDirect3DDevice8_SetTextureStageState for texture %d failed with %#08x\n", i, hr);
1140     }
1141
1142     /* Investigations show that accessing higher textures stage states does not return an error either. Writing
1143      * to too high texture stages(approximately texture 40) causes memory corruption in windows, so there is no
1144      * bounds checking but how do I test that?
1145      */
1146
1147 cleanup:
1148     if(pTexture) IDirect3DTexture8_Release(pTexture);
1149     if(pD3d) IDirect3D8_Release(pD3d);
1150     if(pDevice) IDirect3D8_Release(pDevice);
1151     if(hwnd) DestroyWindow(hwnd);
1152 }
1153
1154 static void test_lights(void)
1155 {
1156     D3DPRESENT_PARAMETERS d3dpp;
1157     IDirect3DDevice8 *device = NULL;
1158     IDirect3D8 *d3d8;
1159     HWND hwnd;
1160     HRESULT hr;
1161     unsigned int i;
1162     BOOL enabled;
1163     D3DCAPS8 caps;
1164     D3DDISPLAYMODE               d3ddm;
1165
1166     d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1167     ok(d3d8 != 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 (!d3d8 || !hwnd) goto cleanup;
1171
1172     IDirect3D8_GetAdapterDisplayMode( d3d8, 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( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1183                                   D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &device );
1184     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1185        "IDirect3D8_CreateDevice failed with %08x\n", hr);
1186     if(!device)
1187     {
1188         skip("Failed to create a d3d device\n");
1189         goto cleanup;
1190     }
1191
1192     memset(&caps, 0, sizeof(caps));
1193     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1194     ok(hr == D3D_OK, "IDirect3DDevice8_GetDeviceCaps failed with %08x\n", hr);
1195
1196     for(i = 1; i <= caps.MaxActiveLights; i++) {
1197         hr = IDirect3DDevice8_LightEnable(device, i, TRUE);
1198         ok(hr == D3D_OK, "Enabling light %u failed with %08x\n", i, hr);
1199         hr = IDirect3DDevice8_GetLightEnable(device, i, &enabled);
1200         ok(hr == D3D_OK || broken(hr == D3DERR_INVALIDCALL),
1201             "GetLightEnable on light %u failed with %08x\n", i, hr);
1202         ok(enabled, "Light %d is %s\n", i, enabled ? "enabled" : "disabled");
1203     }
1204
1205     /* TODO: Test the rendering results in this situation */
1206     hr = IDirect3DDevice8_LightEnable(device, i + 1, TRUE);
1207     ok(hr == D3D_OK ||
1208        broken(hr == D3DERR_INVALIDCALL), /* Some Win9x and WinME */
1209        "Enabling one light more than supported returned %08x\n", hr);
1210     hr = IDirect3DDevice8_GetLightEnable(device, i + 1, &enabled);
1211     ok(hr == D3D_OK, "GetLightEnable on light %u failed with %08x\n", i + 1, hr);
1212     ok(enabled, "Light %d is %s\n", i + 1, enabled ? "enabled" : "disabled");
1213     hr = IDirect3DDevice8_LightEnable(device, i + 1, FALSE);
1214     ok(hr == D3D_OK, "Disabling the additional returned %08x\n", hr);
1215
1216     for(i = 1; i <= caps.MaxActiveLights; i++) {
1217         hr = IDirect3DDevice8_LightEnable(device, i, FALSE);
1218         ok(hr == D3D_OK, "Disabling light %u failed with %08x\n", i, hr);
1219     }
1220
1221     cleanup:
1222     if(device) IDirect3DDevice8_Release(device);
1223     if(d3d8) IDirect3D8_Release(d3d8);
1224 }
1225
1226 static void test_render_zero_triangles(void)
1227 {
1228     D3DPRESENT_PARAMETERS d3dpp;
1229     IDirect3DDevice8 *device = NULL;
1230     IDirect3D8 *d3d8;
1231     HWND hwnd;
1232     HRESULT hr;
1233     D3DDISPLAYMODE               d3ddm;
1234
1235     struct nvertex
1236     {
1237         float x, y, z;
1238         float nx, ny, nz;
1239         DWORD diffuse;
1240     }  quad[] =
1241     {
1242         { 0.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1243         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1244         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1245         { 1.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1246     };
1247
1248     d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1249     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1250     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1251     ok(hwnd != NULL, "Failed to create window\n");
1252     if (!d3d8 || !hwnd) goto cleanup;
1253
1254     IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &d3ddm );
1255     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1256     d3dpp.Windowed         = TRUE;
1257     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1258     d3dpp.BackBufferWidth  = 800;
1259     d3dpp.BackBufferHeight = 600;
1260     d3dpp.BackBufferFormat = d3ddm.Format;
1261     d3dpp.EnableAutoDepthStencil = TRUE;
1262     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1263
1264     hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1265                                   D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &device );
1266     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1267        "IDirect3D8_CreateDevice failed with %08x\n", hr);
1268     if(!device)
1269     {
1270         skip("Failed to create a d3d device\n");
1271         goto cleanup;
1272     }
1273
1274     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
1275     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1276
1277     hr = IDirect3DDevice8_BeginScene(device);
1278     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
1279     if(hr == D3D_OK)
1280     {
1281         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 0 /* NumVerts */,
1282                                                     0 /*PrimCount */, NULL, D3DFMT_INDEX16, quad, sizeof(quad[0]));
1283         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
1284
1285         IDirect3DDevice8_EndScene(device);
1286         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
1287     }
1288
1289     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1290
1291     cleanup:
1292     if(device) IDirect3DDevice8_Release(device);
1293     if(d3d8) IDirect3D8_Release(d3d8);
1294 }
1295
1296 static void test_depth_stencil_reset(void)
1297 {
1298     D3DPRESENT_PARAMETERS present_parameters;
1299     D3DDISPLAYMODE display_mode;
1300     IDirect3DSurface8 *surface;
1301     IDirect3DDevice8 *device = NULL;
1302     IDirect3D8 *d3d8;
1303     HRESULT hr;
1304     HWND hwnd;
1305
1306     d3d8 = pDirect3DCreate8(D3D_SDK_VERSION);
1307     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1308     hwnd = CreateWindow("static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL);
1309     ok(hwnd != NULL, "Failed to create window\n");
1310     if (!d3d8 || !hwnd) goto cleanup;
1311
1312     IDirect3D8_GetAdapterDisplayMode(d3d8, D3DADAPTER_DEFAULT, &display_mode);
1313     memset(&present_parameters, 0, sizeof(present_parameters));
1314     present_parameters.Windowed               = TRUE;
1315     present_parameters.SwapEffect             = D3DSWAPEFFECT_DISCARD;
1316     present_parameters.BackBufferFormat       = display_mode.Format;
1317     present_parameters.EnableAutoDepthStencil = TRUE;
1318     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1319
1320     hr = IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1321             D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device);
1322     if(FAILED(hr))
1323     {
1324         skip("could not create device, IDirect3D8_CreateDevice returned %#x\n", hr);
1325         goto cleanup;
1326     }
1327
1328     hr = IDirect3DDevice8_TestCooperativeLevel(device);
1329     ok(SUCCEEDED(hr), "TestCooperativeLevel failed with %#x\n", hr);
1330
1331     hr = IDirect3DDevice8_SetRenderTarget(device, NULL, NULL);
1332     ok(hr == D3D_OK, "SetRenderTarget failed with 0x%08x\n", hr);
1333
1334     hr = IDirect3DDevice8_GetRenderTarget(device, &surface);
1335     ok(hr == D3D_OK, "GetRenderTarget failed with 0x%08x\n", hr);
1336     ok(surface != NULL, "Render target should not be NULL\n");
1337     if (surface) IDirect3DSurface8_Release(surface);
1338
1339     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1340     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1341     ok(surface == NULL, "Depth stencil should be NULL\n");
1342
1343     present_parameters.EnableAutoDepthStencil = TRUE;
1344     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1345     hr = IDirect3DDevice8_Reset(device, &present_parameters);
1346     ok(hr == D3D_OK, "Reset failed with 0x%08x\n", hr);
1347
1348     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1349     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1350     ok(surface != NULL, "Depth stencil should not be NULL\n");
1351     if (surface) IDirect3DSurface8_Release(surface);
1352
1353     present_parameters.EnableAutoDepthStencil = FALSE;
1354     hr = IDirect3DDevice8_Reset(device, &present_parameters);
1355     ok(hr == D3D_OK, "Reset failed with 0x%08x\n", hr);
1356
1357     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1358     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1359     ok(surface == NULL, "Depth stencil should be NULL\n");
1360
1361     device = NULL;
1362     IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &display_mode );
1363
1364     ZeroMemory( &present_parameters, sizeof(present_parameters) );
1365     present_parameters.Windowed         = TRUE;
1366     present_parameters.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1367     present_parameters.BackBufferFormat = display_mode.Format;
1368     present_parameters.EnableAutoDepthStencil = FALSE;
1369     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1370
1371     hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1372                     D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device );
1373
1374     if(FAILED(hr))
1375     {
1376         skip("could not create device, IDirect3D8_CreateDevice returned %#x\n", hr);
1377         goto cleanup;
1378     }
1379
1380     hr = IDirect3DDevice8_TestCooperativeLevel(device);
1381     ok(hr == D3D_OK, "IDirect3DDevice8_TestCooperativeLevel after creation returned %#x\n", hr);
1382
1383     present_parameters.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1384     present_parameters.Windowed         = TRUE;
1385     present_parameters.BackBufferWidth  = 400;
1386     present_parameters.BackBufferHeight = 300;
1387     present_parameters.EnableAutoDepthStencil = TRUE;
1388     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1389
1390     hr = IDirect3DDevice8_Reset(device, &present_parameters);
1391     ok(hr == D3D_OK, "IDirect3DDevice8_Reset failed with 0x%08x\n", hr);
1392
1393     if (FAILED(hr)) goto cleanup;
1394
1395     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1396     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1397     ok(surface != NULL, "Depth stencil should not be NULL\n");
1398     if (surface) IDirect3DSurface8_Release(surface);
1399
1400 cleanup:
1401     if(d3d8) IDirect3D8_Release(d3d8);
1402     if(device) IDirect3D8_Release(device);
1403 }
1404
1405 START_TEST(device)
1406 {
1407     HMODULE d3d8_handle = LoadLibraryA( "d3d8.dll" );
1408     if (!d3d8_handle)
1409     {
1410         skip("Could not load d3d8.dll\n");
1411         return;
1412     }
1413
1414     pDirect3DCreate8 = (void *)GetProcAddress( d3d8_handle, "Direct3DCreate8" );
1415     ok(pDirect3DCreate8 != NULL, "Failed to get address of Direct3DCreate8\n");
1416     if (pDirect3DCreate8)
1417     {
1418         IDirect3D8 *d3d8;
1419         d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1420         if(!d3d8)
1421         {
1422             skip("could not create D3D8\n");
1423             return;
1424         }
1425         IDirect3D8_Release(d3d8);
1426
1427         test_display_modes();
1428         test_shader_versions();
1429         test_swapchain();
1430         test_refcount();
1431         test_mipmap_levels();
1432         test_cursor();
1433         test_states();
1434         test_scene();
1435         test_shader();
1436         test_limits();
1437         test_lights();
1438         test_render_zero_triangles();
1439         test_depth_stencil_reset();
1440     }
1441 }