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