wintrust: Don't assume dwStateAction is set in WINTRUST_DATA.
[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         skip("GetCursorInfo is not available\n");
627         return;
628     }
629
630     memset(&info, 0, sizeof(info));
631     info.cbSize = sizeof(info);
632     hr = pGetCursorInfo(&info);
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     hr = pGetCursorInfo(&info);
678     ok(hr != 0, "GetCursorInfo returned %#08x\n", hr);
679     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
680     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
681
682     /* Still hidden */
683     hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
684     ok(hr == FALSE, "IDirect3DDevice8_ShowCursor returned %#08x\n", hr);
685
686     /* Enabled now*/
687     hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
688     ok(hr == TRUE, "IDirect3DDevice8_ShowCursor returned %#08x\n", hr);
689
690     /* GDI cursor unchanged */
691     memset(&info, 0, sizeof(info));
692     info.cbSize = sizeof(info);
693     hr = pGetCursorInfo(&info);
694     ok(hr != 0, "GetCursorInfo returned %#08x\n", hr);
695     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
696     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
697
698 cleanup:
699     if(pD3d) IDirect3D8_Release(pD3d);
700     if(pDevice) IDirect3D8_Release(pDevice);
701 }
702
703 static void test_states(void)
704 {
705     HRESULT                      hr;
706     HWND                         hwnd               = NULL;
707     IDirect3D8                  *pD3d               = NULL;
708     IDirect3DDevice8            *pDevice            = NULL;
709     D3DPRESENT_PARAMETERS        d3dpp;
710     D3DDISPLAYMODE               d3ddm;
711
712     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
713     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
714     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
715     ok(hwnd != NULL, "Failed to create window\n");
716     if (!pD3d || !hwnd) goto cleanup;
717
718     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
719     ZeroMemory( &d3dpp, sizeof(d3dpp) );
720     d3dpp.Windowed         = TRUE;
721     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
722     d3dpp.BackBufferWidth  = 640;
723     d3dpp.BackBufferHeight  = 480;
724     d3dpp.BackBufferFormat = d3ddm.Format;
725
726     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
727                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
728     if(FAILED(hr))
729     {
730         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
731         goto cleanup;
732     }
733
734     hr = IDirect3DDevice8_SetRenderState(pDevice, D3DRS_ZVISIBLE, TRUE);
735     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState(D3DRS_ZVISIBLE, TRUE) returned %#08x\n", hr);
736     hr = IDirect3DDevice8_SetRenderState(pDevice, D3DRS_ZVISIBLE, FALSE);
737     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState(D3DRS_ZVISIBLE, FALSE) returned %#08x\n", hr);
738
739 cleanup:
740     if(pD3d) IDirect3D8_Release(pD3d);
741     if(pDevice) IDirect3D8_Release(pDevice);
742 }
743
744 static void test_shader_versions(void)
745 {
746     HRESULT                      hr;
747     IDirect3D8                  *pD3d               = NULL;
748     D3DCAPS8                     d3dcaps;
749
750     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
751     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
752     if (pD3d != NULL) {
753         hr = IDirect3D8_GetDeviceCaps(pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dcaps);
754         ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "Failed to get D3D8 caps (%#08x)\n", hr);
755         if (SUCCEEDED(hr)) {
756             ok(d3dcaps.VertexShaderVersion <= D3DVS_VERSION(1,1), "Unexpected VertexShaderVersion (%#x > %#x)\n", d3dcaps.VertexShaderVersion, D3DVS_VERSION(1,1));
757             ok(d3dcaps.PixelShaderVersion <= D3DPS_VERSION(1,4), "Unexpected PixelShaderVersion (%#x > %#x)\n", d3dcaps.PixelShaderVersion, D3DPS_VERSION(1,4));
758         } else {
759             skip("No Direct3D support\n");
760         }
761         IDirect3D8_Release(pD3d);
762     }
763 }
764
765
766 /* Test adapter display modes */
767 static void test_display_modes(void)
768 {
769     UINT max_modes, i;
770     D3DDISPLAYMODE dmode;
771     HRESULT res;
772     IDirect3D8 *pD3d;
773
774     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
775     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
776     if(!pD3d) return;
777
778     max_modes = IDirect3D8_GetAdapterModeCount(pD3d, D3DADAPTER_DEFAULT);
779     ok(max_modes > 0, "GetAdapterModeCount(D3DADAPTER_DEFAULT) returned 0!\n");
780
781     for(i=0; i<max_modes;i++) {
782         res = IDirect3D8_EnumAdapterModes(pD3d, D3DADAPTER_DEFAULT, i, &dmode);
783         ok(res==D3D_OK, "EnumAdapterModes returned %#08x for mode %u!\n", res, i);
784         if(res != D3D_OK)
785             continue;
786
787         ok(dmode.Format==D3DFMT_X8R8G8B8 || dmode.Format==D3DFMT_R5G6B5,
788            "Unexpected display mode returned for mode %u: %#x\n", i , dmode.Format);
789     }
790
791     IDirect3D8_Release(pD3d);
792 }
793
794 static void test_scene(void)
795 {
796     HRESULT                      hr;
797     HWND                         hwnd               = NULL;
798     IDirect3D8                  *pD3d               = NULL;
799     IDirect3DDevice8            *pDevice            = NULL;
800     D3DPRESENT_PARAMETERS        d3dpp;
801     D3DDISPLAYMODE               d3ddm;
802
803     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
804     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
805     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
806     ok(hwnd != NULL, "Failed to create window\n");
807     if (!pD3d || !hwnd) goto cleanup;
808
809     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
810     ZeroMemory( &d3dpp, sizeof(d3dpp) );
811     d3dpp.Windowed         = TRUE;
812     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
813     d3dpp.BackBufferWidth  = 800;
814     d3dpp.BackBufferHeight  = 600;
815     d3dpp.BackBufferFormat = d3ddm.Format;
816
817
818     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
819                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
820     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "IDirect3D8_CreateDevice failed with %#08x\n", hr);
821     if(!pDevice)
822     {
823         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
824         goto cleanup;
825     }
826
827     /* Test an EndScene without beginscene. Should return an error */
828     hr = IDirect3DDevice8_EndScene(pDevice);
829     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
830
831     /* Test a normal BeginScene / EndScene pair, this should work */
832     hr = IDirect3DDevice8_BeginScene(pDevice);
833     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
834     if(SUCCEEDED(hr))
835     {
836         hr = IDirect3DDevice8_EndScene(pDevice);
837         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
838     }
839
840     /* Test another EndScene without having begun a new scene. Should return an error */
841     hr = IDirect3DDevice8_EndScene(pDevice);
842     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
843
844     /* Two nested BeginScene and EndScene calls */
845     hr = IDirect3DDevice8_BeginScene(pDevice);
846     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
847     hr = IDirect3DDevice8_BeginScene(pDevice);
848     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
849     hr = IDirect3DDevice8_EndScene(pDevice);
850     ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
851     hr = IDirect3DDevice8_EndScene(pDevice);
852     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
853
854     /* StretchRect does not exit in Direct3D8, so no equivalent to the d3d9 stretchrect tests */
855
856 cleanup:
857     if(pD3d) IDirect3D8_Release(pD3d);
858     if(pDevice) IDirect3D8_Release(pDevice);
859     if(hwnd) DestroyWindow(hwnd);
860 }
861
862 static void test_shader(void)
863 {
864     HRESULT                      hr;
865     HWND                         hwnd               = NULL;
866     IDirect3D8                  *pD3d               = NULL;
867     IDirect3DDevice8            *pDevice            = NULL;
868     D3DPRESENT_PARAMETERS        d3dpp;
869     D3DDISPLAYMODE               d3ddm;
870     DWORD                        hPixelShader = 0, hVertexShader = 0;
871     DWORD                        hPixelShader2 = 0, hVertexShader2 = 0;
872     DWORD                        hTempHandle;
873     D3DCAPS8                     caps;
874     DWORD data_size;
875     void *data;
876
877     static DWORD dwVertexDecl[] =
878     {
879         D3DVSD_STREAM(0),
880         D3DVSD_REG(D3DVSDE_POSITION,  D3DVSDT_FLOAT3),
881         D3DVSD_END()
882     };
883     DWORD decl_normal_float2[] =
884     {
885         D3DVSD_STREAM(0),
886         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
887         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_FLOAT2),  /* D3DVSDE_NORMAL,   Register v1 */
888         D3DVSD_END()
889     };
890     DWORD decl_normal_float4[] =
891     {
892         D3DVSD_STREAM(0),
893         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
894         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_FLOAT4),  /* D3DVSDE_NORMAL,   Register v1 */
895         D3DVSD_END()
896     };
897     DWORD decl_normal_d3dcolor[] =
898     {
899         D3DVSD_STREAM(0),
900         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
901         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_D3DCOLOR),/* D3DVSDE_NORMAL,   Register v1 */
902         D3DVSD_END()
903     };
904     const DWORD vertex_decl_size = sizeof(dwVertexDecl);
905     const DWORD simple_vs_size = sizeof(simple_vs);
906     const DWORD simple_ps_size = sizeof(simple_ps);
907
908     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
909     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
910     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
911     ok(hwnd != NULL, "Failed to create window\n");
912     if (!pD3d || !hwnd) goto cleanup;
913
914     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
915     ZeroMemory( &d3dpp, sizeof(d3dpp) );
916     d3dpp.Windowed         = TRUE;
917     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
918     d3dpp.BackBufferWidth  = 800;
919     d3dpp.BackBufferHeight  = 600;
920     d3dpp.BackBufferFormat = d3ddm.Format;
921
922
923     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
924                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
925     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "IDirect3D8_CreateDevice failed with %#08x\n", hr);
926     if(!pDevice)
927     {
928         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
929         goto cleanup;
930     }
931     IDirect3DDevice8_GetDeviceCaps(pDevice, &caps);
932
933     /* First create a vertex shader */
934     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, simple_vs, &hVertexShader, 0);
935     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
936     /* Msdn says that the new vertex shader is set immediately. This is wrong, apparently */
937     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
938     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
939     ok(hTempHandle == 0, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, 0);
940     /* Assign the shader, then verify that GetVertexShader works */
941     hr = IDirect3DDevice8_SetVertexShader(pDevice, hVertexShader);
942     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
943     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
944     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
945     ok(hTempHandle == hVertexShader, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, hVertexShader);
946     /* Verify that we can retrieve the declaration */
947     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, NULL, &data_size);
948     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderDeclaration returned %#08x\n", hr);
949     ok(data_size == vertex_decl_size, "Got data_size %u, expected %u\n", data_size, vertex_decl_size);
950     data = HeapAlloc(GetProcessHeap(), 0, vertex_decl_size);
951     data_size = 1;
952     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, data, &data_size);
953     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetVertexShaderDeclaration returned (%#08x), "
954             "expected D3DERR_INVALIDCALL\n", hr);
955     ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
956     data_size = vertex_decl_size;
957     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, data, &data_size);
958     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderDeclaration returned %#08x\n", hr);
959     ok(data_size == vertex_decl_size, "Got data_size %u, expected %u\n", data_size, vertex_decl_size);
960     ok(!memcmp(data, dwVertexDecl, vertex_decl_size), "data not equal to shader declaration\n");
961     HeapFree(GetProcessHeap(), 0, data);
962     /* Verify that we can retrieve the shader function */
963     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, NULL, &data_size);
964     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderFunction returned %#08x\n", hr);
965     ok(data_size == simple_vs_size, "Got data_size %u, expected %u\n", data_size, simple_vs_size);
966     data = HeapAlloc(GetProcessHeap(), 0, simple_vs_size);
967     data_size = 1;
968     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, data, &data_size);
969     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetVertexShaderFunction returned (%#08x), "
970             "expected D3DERR_INVALIDCALL\n", hr);
971     ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
972     data_size = simple_vs_size;
973     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, data, &data_size);
974     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderFunction returned %#08x\n", hr);
975     ok(data_size == simple_vs_size, "Got data_size %u, expected %u\n", data_size, simple_vs_size);
976     ok(!memcmp(data, simple_vs, simple_vs_size), "data not equal to shader function\n");
977     HeapFree(GetProcessHeap(), 0, data);
978     /* Delete the assigned shader. This is supposed to work */
979     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
980     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
981     /* The shader should be unset now */
982     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
983     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
984     ok(hTempHandle == 0, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, 0);
985
986     /* Test a broken declaration. 3DMark2001 tries to use normals with 2 components
987      * First try the fixed function shader function, then a custom one
988      */
989     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float2, 0, &hVertexShader, 0);
990     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
991     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
992     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float4, 0, &hVertexShader, 0);
993     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
994     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
995     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_d3dcolor, 0, &hVertexShader, 0);
996     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
997     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
998
999     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float2, simple_vs, &hVertexShader, 0);
1000     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1001     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1002
1003     if (caps.PixelShaderVersion >= D3DPS_VERSION(1, 0))
1004     {
1005         /* The same with a pixel shader */
1006         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader);
1007         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1008         /* Msdn says that the new pixel shader is set immediately. This is wrong, apparently */
1009         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1010         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1011         ok(hTempHandle == 0, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, 0);
1012         /* Assign the shader, then verify that GetPixelShader works */
1013         hr = IDirect3DDevice8_SetPixelShader(pDevice, hPixelShader);
1014         ok(hr == D3D_OK, "IDirect3DDevice8_SetPixelShader returned %#08x\n", hr);
1015         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1016         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1017         ok(hTempHandle == hPixelShader, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, hPixelShader);
1018         /* Verify that we can retrieve the shader function */
1019         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, NULL, &data_size);
1020         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShaderFunction returned %#08x\n", hr);
1021         ok(data_size == simple_ps_size, "Got data_size %u, expected %u\n", data_size, simple_ps_size);
1022         data = HeapAlloc(GetProcessHeap(), 0, simple_ps_size);
1023         data_size = 1;
1024         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, data, &data_size);
1025         ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetPixelShaderFunction returned (%#08x), "
1026                 "expected D3DERR_INVALIDCALL\n", hr);
1027         ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
1028         data_size = simple_ps_size;
1029         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, data, &data_size);
1030         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShaderFunction returned %#08x\n", hr);
1031         ok(data_size == simple_ps_size, "Got data_size %u, expected %u\n", data_size, simple_ps_size);
1032         ok(!memcmp(data, simple_ps, simple_ps_size), "data not equal to shader function\n");
1033         HeapFree(GetProcessHeap(), 0, data);
1034         /* Delete the assigned shader. This is supposed to work */
1035         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1036         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1037         /* The shader should be unset now */
1038         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1039         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1040         ok(hTempHandle == 0, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, 0);
1041
1042         /* What happens if a non-bound shader is deleted? */
1043         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader);
1044         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1045         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader2);
1046         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1047
1048         hr = IDirect3DDevice8_SetPixelShader(pDevice, hPixelShader);
1049         ok(hr == D3D_OK, "IDirect3DDevice8_SetPixelShader returned %#08x\n", hr);
1050         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader2);
1051         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1052         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1053         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1054         ok(hTempHandle == hPixelShader, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, hPixelShader);
1055         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1056         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1057     }
1058     else
1059     {
1060         skip("Pixel shaders not supported\n");
1061     }
1062
1063     /* What happens if a non-bound shader is deleted? */
1064     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, NULL, &hVertexShader, 0);
1065     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1066     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, NULL, &hVertexShader2, 0);
1067     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1068
1069     hr = IDirect3DDevice8_SetVertexShader(pDevice, hVertexShader);
1070     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1071     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader2);
1072     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1073     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1074     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1075     ok(hTempHandle == hVertexShader, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, hVertexShader);
1076     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1077     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1078
1079 cleanup:
1080     if(pD3d) IDirect3D8_Release(pD3d);
1081     if(pDevice) IDirect3D8_Release(pDevice);
1082     if(hwnd) DestroyWindow(hwnd);
1083 }
1084
1085 static void test_limits(void)
1086 {
1087     HRESULT                      hr;
1088     HWND                         hwnd               = NULL;
1089     IDirect3D8                  *pD3d               = NULL;
1090     IDirect3DDevice8            *pDevice            = NULL;
1091     D3DPRESENT_PARAMETERS        d3dpp;
1092     D3DDISPLAYMODE               d3ddm;
1093     IDirect3DTexture8           *pTexture           = NULL;
1094     int i;
1095
1096     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
1097     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
1098     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1099     ok(hwnd != NULL, "Failed to create window\n");
1100     if (!pD3d || !hwnd) goto cleanup;
1101
1102     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1103     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1104     d3dpp.Windowed         = TRUE;
1105     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1106     d3dpp.BackBufferWidth  = 800;
1107     d3dpp.BackBufferHeight  = 600;
1108     d3dpp.BackBufferFormat = d3ddm.Format;
1109     d3dpp.EnableAutoDepthStencil = TRUE;
1110     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1111
1112     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1113                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1114     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "IDirect3D8_CreateDevice failed with %#08x\n", hr);
1115     if(!pDevice)
1116     {
1117         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
1118         goto cleanup;
1119     }
1120
1121     hr = IDirect3DDevice8_CreateTexture(pDevice, 16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture);
1122     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed with %#08x\n", hr);
1123     if(!pTexture) goto cleanup;
1124
1125     /* There are 8 texture stages. We should be able to access all of them */
1126     for(i = 0; i < 8; i++) {
1127         hr = IDirect3DDevice8_SetTexture(pDevice, i, (IDirect3DBaseTexture8 *) pTexture);
1128         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %#08x\n", i, hr);
1129         hr = IDirect3DDevice8_SetTexture(pDevice, i, NULL);
1130         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %#08x\n", i, hr);
1131         hr = IDirect3DDevice8_SetTextureStageState(pDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
1132         ok(hr == D3D_OK, "IDirect3DDevice8_SetTextureStageState for texture %d failed with %#08x\n", i, hr);
1133     }
1134
1135     /* Investigations show that accessing higher textures stage states does not return an error either. Writing
1136      * to too high texture stages(approximately texture 40) causes memory corruption in windows, so there is no
1137      * bounds checking but how do I test that?
1138      */
1139
1140 cleanup:
1141     if(pTexture) IDirect3DTexture8_Release(pTexture);
1142     if(pD3d) IDirect3D8_Release(pD3d);
1143     if(pDevice) IDirect3D8_Release(pDevice);
1144     if(hwnd) DestroyWindow(hwnd);
1145 }
1146
1147 static void test_lights(void)
1148 {
1149     D3DPRESENT_PARAMETERS d3dpp;
1150     IDirect3DDevice8 *device = NULL;
1151     IDirect3D8 *d3d8;
1152     HWND hwnd;
1153     HRESULT hr;
1154     unsigned int i;
1155     BOOL enabled;
1156     D3DCAPS8 caps;
1157     D3DDISPLAYMODE               d3ddm;
1158
1159     d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1160     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1161     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1162     ok(hwnd != NULL, "Failed to create window\n");
1163     if (!d3d8 || !hwnd) goto cleanup;
1164
1165     IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &d3ddm );
1166     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1167     d3dpp.Windowed         = TRUE;
1168     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1169     d3dpp.BackBufferWidth  = 800;
1170     d3dpp.BackBufferHeight  = 600;
1171     d3dpp.BackBufferFormat = d3ddm.Format;
1172     d3dpp.EnableAutoDepthStencil = TRUE;
1173     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1174
1175     hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1176                                   D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &device );
1177     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1178        "IDirect3D8_CreateDevice failed with %08x\n", hr);
1179     if(!device)
1180     {
1181         skip("Failed to create a d3d device\n");
1182         goto cleanup;
1183     }
1184
1185     memset(&caps, 0, sizeof(caps));
1186     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1187     ok(hr == D3D_OK, "IDirect3DDevice8_GetDeviceCaps failed with %08x\n", hr);
1188
1189     for(i = 1; i <= caps.MaxActiveLights; i++) {
1190         hr = IDirect3DDevice8_LightEnable(device, i, TRUE);
1191         ok(hr == D3D_OK, "Enabling light %u failed with %08x\n", i, hr);
1192         hr = IDirect3DDevice8_GetLightEnable(device, i, &enabled);
1193         ok(hr == D3D_OK, "GetLightEnable on light %u failed with %08x\n", i, hr);
1194         ok(enabled, "Light %d is %s\n", i, enabled ? "enabled" : "disabled");
1195     }
1196
1197     /* TODO: Test the rendering results in this situation */
1198     hr = IDirect3DDevice8_LightEnable(device, i + 1, TRUE);
1199     ok(hr == D3D_OK, "Enabling one light more than supported returned %08x\n", hr);
1200     hr = IDirect3DDevice8_GetLightEnable(device, i + 1, &enabled);
1201     ok(hr == D3D_OK, "GetLightEnable on light %u failed with %08x\n", i + 1, hr);
1202     ok(enabled, "Light %d is %s\n", i + 1, enabled ? "enabled" : "disabled");
1203     hr = IDirect3DDevice8_LightEnable(device, i + 1, FALSE);
1204     ok(hr == D3D_OK, "Disabling the additional returned %08x\n", hr);
1205
1206     for(i = 1; i <= caps.MaxActiveLights; i++) {
1207         hr = IDirect3DDevice8_LightEnable(device, i, FALSE);
1208         ok(hr == D3D_OK, "Disabling light %u failed with %08x\n", i, hr);
1209     }
1210
1211     cleanup:
1212     if(device) IDirect3DDevice8_Release(device);
1213     if(d3d8) IDirect3D8_Release(d3d8);
1214 }
1215
1216 static void test_render_zero_triangles(void)
1217 {
1218     D3DPRESENT_PARAMETERS d3dpp;
1219     IDirect3DDevice8 *device = NULL;
1220     IDirect3D8 *d3d8;
1221     HWND hwnd;
1222     HRESULT hr;
1223     D3DDISPLAYMODE               d3ddm;
1224
1225     struct nvertex
1226     {
1227         float x, y, z;
1228         float nx, ny, nz;
1229         DWORD diffuse;
1230     }  quad[] =
1231     {
1232         { 0.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1233         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1234         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1235         { 1.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1236     };
1237
1238     d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1239     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1240     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1241     ok(hwnd != NULL, "Failed to create window\n");
1242     if (!d3d8 || !hwnd) goto cleanup;
1243
1244     IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &d3ddm );
1245     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1246     d3dpp.Windowed         = TRUE;
1247     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1248     d3dpp.BackBufferWidth  = 800;
1249     d3dpp.BackBufferHeight  = 600;
1250     d3dpp.BackBufferFormat = d3ddm.Format;
1251     d3dpp.EnableAutoDepthStencil = TRUE;
1252     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1253
1254     hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1255                                   D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &device );
1256     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1257        "IDirect3D8_CreateDevice failed with %08x\n", hr);
1258     if(!device)
1259     {
1260         skip("Failed to create a d3d device\n");
1261         goto cleanup;
1262     }
1263
1264     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
1265     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1266
1267     hr = IDirect3DDevice8_BeginScene(device);
1268     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
1269     if(hr == D3D_OK)
1270     {
1271         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 0 /* NumVerts */,
1272                                                     0 /*PrimCount */, NULL, D3DFMT_INDEX16, quad, sizeof(quad[0]));
1273         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
1274
1275         IDirect3DDevice8_EndScene(device);
1276         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
1277     }
1278
1279     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1280
1281     cleanup:
1282     if(device) IDirect3DDevice8_Release(device);
1283     if(d3d8) IDirect3D8_Release(d3d8);
1284 }
1285
1286 static void test_depth_stencil_reset(void)
1287 {
1288     D3DPRESENT_PARAMETERS present_parameters;
1289     D3DDISPLAYMODE display_mode;
1290     IDirect3DSurface8 *surface;
1291     IDirect3DDevice8 *device;
1292     IDirect3D8 *d3d8;
1293     HRESULT hr;
1294     HWND hwnd;
1295
1296     d3d8 = pDirect3DCreate8(D3D_SDK_VERSION);
1297     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1298     hwnd = CreateWindow("static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL);
1299     ok(hwnd != NULL, "Failed to create window\n");
1300     if (!d3d8 || !hwnd) goto cleanup;
1301
1302     IDirect3D8_GetAdapterDisplayMode(d3d8, D3DADAPTER_DEFAULT, &display_mode);
1303     memset(&present_parameters, 0, sizeof(present_parameters));
1304     present_parameters.Windowed               = TRUE;
1305     present_parameters.SwapEffect             = D3DSWAPEFFECT_DISCARD;
1306     present_parameters.BackBufferFormat       = display_mode.Format;
1307     present_parameters.EnableAutoDepthStencil = TRUE;
1308     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1309
1310     hr = IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1311             D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device);
1312     if(FAILED(hr))
1313     {
1314         skip("could not create device, IDirect3D8_CreateDevice returned %#x\n", hr);
1315         goto cleanup;
1316     }
1317
1318     hr = IDirect3DDevice8_TestCooperativeLevel(device);
1319     ok(SUCCEEDED(hr), "TestCooperativeLevel failed with %#x\n", hr);
1320
1321     hr = IDirect3DDevice8_SetRenderTarget(device, NULL, NULL);
1322     ok(hr == D3D_OK, "SetRenderTarget failed with 0x%08x\n", hr);
1323
1324     hr = IDirect3DDevice8_GetRenderTarget(device, &surface);
1325     ok(hr == D3D_OK, "GetRenderTarget failed with 0x%08x\n", hr);
1326     ok(surface != NULL, "Render target should not be NULL\n");
1327     if (surface) IDirect3DSurface8_Release(surface);
1328
1329     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1330     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1331     ok(surface == NULL, "Depth stencil should be NULL\n");
1332
1333     present_parameters.EnableAutoDepthStencil = TRUE;
1334     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1335     hr = IDirect3DDevice8_Reset(device, &present_parameters);
1336     ok(hr == D3D_OK, "Reset failed with 0x%08x\n", hr);
1337
1338     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1339     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1340     ok(surface != NULL, "Depth stencil should not be NULL\n");
1341     if (surface) IDirect3DSurface8_Release(surface);
1342
1343     present_parameters.EnableAutoDepthStencil = FALSE;
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 == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1349     ok(surface == NULL, "Depth stencil should be NULL\n");
1350
1351 cleanup:
1352     if(d3d8) IDirect3D8_Release(d3d8);
1353     if(device) IDirect3D8_Release(device);
1354 }
1355
1356 START_TEST(device)
1357 {
1358     HMODULE d3d8_handle = LoadLibraryA( "d3d8.dll" );
1359     if (!d3d8_handle)
1360     {
1361         skip("Could not load d3d8.dll\n");
1362         return;
1363     }
1364
1365     pDirect3DCreate8 = (void *)GetProcAddress( d3d8_handle, "Direct3DCreate8" );
1366     ok(pDirect3DCreate8 != NULL, "Failed to get address of Direct3DCreate8\n");
1367     if (pDirect3DCreate8)
1368     {
1369         test_display_modes();
1370         test_shader_versions();
1371         test_swapchain();
1372         test_refcount();
1373         test_mipmap_levels();
1374         test_cursor();
1375         test_states();
1376         test_scene();
1377         test_shader();
1378         test_limits();
1379         test_lights();
1380         test_render_zero_triangles();
1381         test_depth_stencil_reset();
1382     }
1383 }