d3d8/tests: Skip tests if we can't create the D3D8 object.
[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     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, "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 data_size;
873     void *data;
874
875     static DWORD dwVertexDecl[] =
876     {
877         D3DVSD_STREAM(0),
878         D3DVSD_REG(D3DVSDE_POSITION,  D3DVSDT_FLOAT3),
879         D3DVSD_END()
880     };
881     DWORD decl_normal_float2[] =
882     {
883         D3DVSD_STREAM(0),
884         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
885         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_FLOAT2),  /* D3DVSDE_NORMAL,   Register v1 */
886         D3DVSD_END()
887     };
888     DWORD decl_normal_float4[] =
889     {
890         D3DVSD_STREAM(0),
891         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
892         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_FLOAT4),  /* D3DVSDE_NORMAL,   Register v1 */
893         D3DVSD_END()
894     };
895     DWORD decl_normal_d3dcolor[] =
896     {
897         D3DVSD_STREAM(0),
898         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
899         D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_D3DCOLOR),/* D3DVSDE_NORMAL,   Register v1 */
900         D3DVSD_END()
901     };
902     const DWORD vertex_decl_size = sizeof(dwVertexDecl);
903     const DWORD simple_vs_size = sizeof(simple_vs);
904     const DWORD simple_ps_size = sizeof(simple_ps);
905
906     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
907     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
908     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
909     ok(hwnd != NULL, "Failed to create window\n");
910     if (!pD3d || !hwnd) goto cleanup;
911
912     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
913     ZeroMemory( &d3dpp, sizeof(d3dpp) );
914     d3dpp.Windowed         = TRUE;
915     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
916     d3dpp.BackBufferWidth  = 800;
917     d3dpp.BackBufferHeight  = 600;
918     d3dpp.BackBufferFormat = d3ddm.Format;
919
920
921     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
922                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
923     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "IDirect3D8_CreateDevice failed with %#08x\n", hr);
924     if(!pDevice)
925     {
926         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
927         goto cleanup;
928     }
929     IDirect3DDevice8_GetDeviceCaps(pDevice, &caps);
930
931     /* First create a vertex shader */
932     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, simple_vs, &hVertexShader, 0);
933     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
934     /* Msdn says that the new vertex shader is set immediately. This is wrong, apparently */
935     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
936     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
937     ok(hTempHandle == 0, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, 0);
938     /* Assign the shader, then verify that GetVertexShader works */
939     hr = IDirect3DDevice8_SetVertexShader(pDevice, hVertexShader);
940     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
941     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
942     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
943     ok(hTempHandle == hVertexShader, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, hVertexShader);
944     /* Verify that we can retrieve the declaration */
945     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, NULL, &data_size);
946     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderDeclaration returned %#08x\n", hr);
947     ok(data_size == vertex_decl_size, "Got data_size %u, expected %u\n", data_size, vertex_decl_size);
948     data = HeapAlloc(GetProcessHeap(), 0, vertex_decl_size);
949     data_size = 1;
950     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, data, &data_size);
951     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetVertexShaderDeclaration returned (%#08x), "
952             "expected D3DERR_INVALIDCALL\n", hr);
953     ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
954     data_size = vertex_decl_size;
955     hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, data, &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     ok(!memcmp(data, dwVertexDecl, vertex_decl_size), "data not equal to shader declaration\n");
959     HeapFree(GetProcessHeap(), 0, data);
960     /* Verify that we can retrieve the shader function */
961     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, NULL, &data_size);
962     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderFunction returned %#08x\n", hr);
963     ok(data_size == simple_vs_size, "Got data_size %u, expected %u\n", data_size, simple_vs_size);
964     data = HeapAlloc(GetProcessHeap(), 0, simple_vs_size);
965     data_size = 1;
966     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, data, &data_size);
967     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetVertexShaderFunction returned (%#08x), "
968             "expected D3DERR_INVALIDCALL\n", hr);
969     ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
970     data_size = simple_vs_size;
971     hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, data, &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     ok(!memcmp(data, simple_vs, simple_vs_size), "data not equal to shader function\n");
975     HeapFree(GetProcessHeap(), 0, data);
976     /* Delete the assigned shader. This is supposed to work */
977     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
978     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
979     /* The shader should be unset now */
980     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
981     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
982     ok(hTempHandle == 0, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, 0);
983
984     /* Test a broken declaration. 3DMark2001 tries to use normals with 2 components
985      * First try the fixed function shader function, then a custom one
986      */
987     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float2, 0, &hVertexShader, 0);
988     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
989     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
990     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float4, 0, &hVertexShader, 0);
991     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
992     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
993     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_d3dcolor, 0, &hVertexShader, 0);
994     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
995     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
996
997     hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float2, simple_vs, &hVertexShader, 0);
998     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
999     if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1000
1001     if (caps.PixelShaderVersion >= D3DPS_VERSION(1, 0))
1002     {
1003         /* The same with a pixel shader */
1004         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader);
1005         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1006         /* Msdn says that the new pixel shader is set immediately. This is wrong, apparently */
1007         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1008         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1009         ok(hTempHandle == 0, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, 0);
1010         /* Assign the shader, then verify that GetPixelShader works */
1011         hr = IDirect3DDevice8_SetPixelShader(pDevice, hPixelShader);
1012         ok(hr == D3D_OK, "IDirect3DDevice8_SetPixelShader returned %#08x\n", hr);
1013         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1014         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1015         ok(hTempHandle == hPixelShader, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, hPixelShader);
1016         /* Verify that we can retrieve the shader function */
1017         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, NULL, &data_size);
1018         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShaderFunction returned %#08x\n", hr);
1019         ok(data_size == simple_ps_size, "Got data_size %u, expected %u\n", data_size, simple_ps_size);
1020         data = HeapAlloc(GetProcessHeap(), 0, simple_ps_size);
1021         data_size = 1;
1022         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, data, &data_size);
1023         ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetPixelShaderFunction returned (%#08x), "
1024                 "expected D3DERR_INVALIDCALL\n", hr);
1025         ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
1026         data_size = simple_ps_size;
1027         hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, data, &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         ok(!memcmp(data, simple_ps, simple_ps_size), "data not equal to shader function\n");
1031         HeapFree(GetProcessHeap(), 0, data);
1032         /* Delete the assigned shader. This is supposed to work */
1033         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1034         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1035         /* The shader should be unset now */
1036         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1037         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1038         ok(hTempHandle == 0, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, 0);
1039
1040         /* What happens if a non-bound shader is deleted? */
1041         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader);
1042         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1043         hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader2);
1044         ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1045
1046         hr = IDirect3DDevice8_SetPixelShader(pDevice, hPixelShader);
1047         ok(hr == D3D_OK, "IDirect3DDevice8_SetPixelShader returned %#08x\n", hr);
1048         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader2);
1049         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1050         hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1051         ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1052         ok(hTempHandle == hPixelShader, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, hPixelShader);
1053         hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1054         ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1055     }
1056     else
1057     {
1058         skip("Pixel shaders not supported\n");
1059     }
1060
1061     /* What happens if a non-bound shader is deleted? */
1062     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, NULL, &hVertexShader, 0);
1063     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1064     hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, NULL, &hVertexShader2, 0);
1065     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1066
1067     hr = IDirect3DDevice8_SetVertexShader(pDevice, hVertexShader);
1068     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1069     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader2);
1070     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1071     hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1072     ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1073     ok(hTempHandle == hVertexShader, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, hVertexShader);
1074     hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1075     ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1076
1077 cleanup:
1078     if(pD3d) IDirect3D8_Release(pD3d);
1079     if(pDevice) IDirect3D8_Release(pDevice);
1080     if(hwnd) DestroyWindow(hwnd);
1081 }
1082
1083 static void test_limits(void)
1084 {
1085     HRESULT                      hr;
1086     HWND                         hwnd               = NULL;
1087     IDirect3D8                  *pD3d               = NULL;
1088     IDirect3DDevice8            *pDevice            = NULL;
1089     D3DPRESENT_PARAMETERS        d3dpp;
1090     D3DDISPLAYMODE               d3ddm;
1091     IDirect3DTexture8           *pTexture           = NULL;
1092     int i;
1093
1094     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
1095     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
1096     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1097     ok(hwnd != NULL, "Failed to create window\n");
1098     if (!pD3d || !hwnd) goto cleanup;
1099
1100     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1101     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1102     d3dpp.Windowed         = TRUE;
1103     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1104     d3dpp.BackBufferWidth  = 800;
1105     d3dpp.BackBufferHeight  = 600;
1106     d3dpp.BackBufferFormat = d3ddm.Format;
1107     d3dpp.EnableAutoDepthStencil = TRUE;
1108     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1109
1110     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1111                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1112     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "IDirect3D8_CreateDevice failed with %#08x\n", hr);
1113     if(!pDevice)
1114     {
1115         skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
1116         goto cleanup;
1117     }
1118
1119     hr = IDirect3DDevice8_CreateTexture(pDevice, 16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture);
1120     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed with %#08x\n", hr);
1121     if(!pTexture) goto cleanup;
1122
1123     /* There are 8 texture stages. We should be able to access all of them */
1124     for(i = 0; i < 8; i++) {
1125         hr = IDirect3DDevice8_SetTexture(pDevice, i, (IDirect3DBaseTexture8 *) pTexture);
1126         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %#08x\n", i, hr);
1127         hr = IDirect3DDevice8_SetTexture(pDevice, i, NULL);
1128         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %#08x\n", i, hr);
1129         hr = IDirect3DDevice8_SetTextureStageState(pDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
1130         ok(hr == D3D_OK, "IDirect3DDevice8_SetTextureStageState for texture %d failed with %#08x\n", i, hr);
1131     }
1132
1133     /* Investigations show that accessing higher textures stage states does not return an error either. Writing
1134      * to too high texture stages(approximately texture 40) causes memory corruption in windows, so there is no
1135      * bounds checking but how do I test that?
1136      */
1137
1138 cleanup:
1139     if(pTexture) IDirect3DTexture8_Release(pTexture);
1140     if(pD3d) IDirect3D8_Release(pD3d);
1141     if(pDevice) IDirect3D8_Release(pDevice);
1142     if(hwnd) DestroyWindow(hwnd);
1143 }
1144
1145 static void test_lights(void)
1146 {
1147     D3DPRESENT_PARAMETERS d3dpp;
1148     IDirect3DDevice8 *device = NULL;
1149     IDirect3D8 *d3d8;
1150     HWND hwnd;
1151     HRESULT hr;
1152     unsigned int i;
1153     BOOL enabled;
1154     D3DCAPS8 caps;
1155     D3DDISPLAYMODE               d3ddm;
1156
1157     d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1158     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1159     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1160     ok(hwnd != NULL, "Failed to create window\n");
1161     if (!d3d8 || !hwnd) goto cleanup;
1162
1163     IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &d3ddm );
1164     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1165     d3dpp.Windowed         = TRUE;
1166     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1167     d3dpp.BackBufferWidth  = 800;
1168     d3dpp.BackBufferHeight  = 600;
1169     d3dpp.BackBufferFormat = d3ddm.Format;
1170     d3dpp.EnableAutoDepthStencil = TRUE;
1171     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1172
1173     hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1174                                   D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &device );
1175     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1176        "IDirect3D8_CreateDevice failed with %08x\n", hr);
1177     if(!device)
1178     {
1179         skip("Failed to create a d3d device\n");
1180         goto cleanup;
1181     }
1182
1183     memset(&caps, 0, sizeof(caps));
1184     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1185     ok(hr == D3D_OK, "IDirect3DDevice8_GetDeviceCaps failed with %08x\n", hr);
1186
1187     for(i = 1; i <= caps.MaxActiveLights; i++) {
1188         hr = IDirect3DDevice8_LightEnable(device, i, TRUE);
1189         ok(hr == D3D_OK, "Enabling light %u failed with %08x\n", i, hr);
1190         hr = IDirect3DDevice8_GetLightEnable(device, i, &enabled);
1191         ok(hr == D3D_OK || broken(hr == D3DERR_INVALIDCALL),
1192             "GetLightEnable on light %u failed with %08x\n", i, hr);
1193         ok(enabled, "Light %d is %s\n", i, enabled ? "enabled" : "disabled");
1194     }
1195
1196     /* TODO: Test the rendering results in this situation */
1197     hr = IDirect3DDevice8_LightEnable(device, i + 1, TRUE);
1198     ok(hr == D3D_OK, "Enabling one light more than supported returned %08x\n", hr);
1199     hr = IDirect3DDevice8_GetLightEnable(device, i + 1, &enabled);
1200     ok(hr == D3D_OK, "GetLightEnable on light %u failed with %08x\n", i + 1, hr);
1201     ok(enabled, "Light %d is %s\n", i + 1, enabled ? "enabled" : "disabled");
1202     hr = IDirect3DDevice8_LightEnable(device, i + 1, FALSE);
1203     ok(hr == D3D_OK, "Disabling the additional returned %08x\n", hr);
1204
1205     for(i = 1; i <= caps.MaxActiveLights; i++) {
1206         hr = IDirect3DDevice8_LightEnable(device, i, FALSE);
1207         ok(hr == D3D_OK, "Disabling light %u failed with %08x\n", i, hr);
1208     }
1209
1210     cleanup:
1211     if(device) IDirect3DDevice8_Release(device);
1212     if(d3d8) IDirect3D8_Release(d3d8);
1213 }
1214
1215 static void test_render_zero_triangles(void)
1216 {
1217     D3DPRESENT_PARAMETERS d3dpp;
1218     IDirect3DDevice8 *device = NULL;
1219     IDirect3D8 *d3d8;
1220     HWND hwnd;
1221     HRESULT hr;
1222     D3DDISPLAYMODE               d3ddm;
1223
1224     struct nvertex
1225     {
1226         float x, y, z;
1227         float nx, ny, nz;
1228         DWORD diffuse;
1229     }  quad[] =
1230     {
1231         { 0.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1232         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1233         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1234         { 1.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
1235     };
1236
1237     d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1238     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1239     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1240     ok(hwnd != NULL, "Failed to create window\n");
1241     if (!d3d8 || !hwnd) goto cleanup;
1242
1243     IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &d3ddm );
1244     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1245     d3dpp.Windowed         = TRUE;
1246     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1247     d3dpp.BackBufferWidth  = 800;
1248     d3dpp.BackBufferHeight  = 600;
1249     d3dpp.BackBufferFormat = d3ddm.Format;
1250     d3dpp.EnableAutoDepthStencil = TRUE;
1251     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1252
1253     hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1254                                   D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &device );
1255     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1256        "IDirect3D8_CreateDevice failed with %08x\n", hr);
1257     if(!device)
1258     {
1259         skip("Failed to create a d3d device\n");
1260         goto cleanup;
1261     }
1262
1263     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
1264     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1265
1266     hr = IDirect3DDevice8_BeginScene(device);
1267     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
1268     if(hr == D3D_OK)
1269     {
1270         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 0 /* NumVerts */,
1271                                                     0 /*PrimCount */, NULL, D3DFMT_INDEX16, quad, sizeof(quad[0]));
1272         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
1273
1274         IDirect3DDevice8_EndScene(device);
1275         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
1276     }
1277
1278     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1279
1280     cleanup:
1281     if(device) IDirect3DDevice8_Release(device);
1282     if(d3d8) IDirect3D8_Release(d3d8);
1283 }
1284
1285 static void test_depth_stencil_reset(void)
1286 {
1287     D3DPRESENT_PARAMETERS present_parameters;
1288     D3DDISPLAYMODE display_mode;
1289     IDirect3DSurface8 *surface;
1290     IDirect3DDevice8 *device = NULL;
1291     IDirect3D8 *d3d8;
1292     HRESULT hr;
1293     HWND hwnd;
1294
1295     d3d8 = pDirect3DCreate8(D3D_SDK_VERSION);
1296     ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1297     hwnd = CreateWindow("static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL);
1298     ok(hwnd != NULL, "Failed to create window\n");
1299     if (!d3d8 || !hwnd) goto cleanup;
1300
1301     IDirect3D8_GetAdapterDisplayMode(d3d8, D3DADAPTER_DEFAULT, &display_mode);
1302     memset(&present_parameters, 0, sizeof(present_parameters));
1303     present_parameters.Windowed               = TRUE;
1304     present_parameters.SwapEffect             = D3DSWAPEFFECT_DISCARD;
1305     present_parameters.BackBufferFormat       = display_mode.Format;
1306     present_parameters.EnableAutoDepthStencil = TRUE;
1307     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1308
1309     hr = IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1310             D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device);
1311     if(FAILED(hr))
1312     {
1313         skip("could not create device, IDirect3D8_CreateDevice returned %#x\n", hr);
1314         goto cleanup;
1315     }
1316
1317     hr = IDirect3DDevice8_TestCooperativeLevel(device);
1318     ok(SUCCEEDED(hr), "TestCooperativeLevel failed with %#x\n", hr);
1319
1320     hr = IDirect3DDevice8_SetRenderTarget(device, NULL, NULL);
1321     ok(hr == D3D_OK, "SetRenderTarget failed with 0x%08x\n", hr);
1322
1323     hr = IDirect3DDevice8_GetRenderTarget(device, &surface);
1324     ok(hr == D3D_OK, "GetRenderTarget failed with 0x%08x\n", hr);
1325     ok(surface != NULL, "Render target should not be NULL\n");
1326     if (surface) IDirect3DSurface8_Release(surface);
1327
1328     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1329     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1330     ok(surface == NULL, "Depth stencil should be NULL\n");
1331
1332     present_parameters.EnableAutoDepthStencil = TRUE;
1333     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1334     hr = IDirect3DDevice8_Reset(device, &present_parameters);
1335     ok(hr == D3D_OK, "Reset failed with 0x%08x\n", hr);
1336
1337     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1338     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1339     ok(surface != NULL, "Depth stencil should not be NULL\n");
1340     if (surface) IDirect3DSurface8_Release(surface);
1341
1342     present_parameters.EnableAutoDepthStencil = FALSE;
1343     hr = IDirect3DDevice8_Reset(device, &present_parameters);
1344     ok(hr == D3D_OK, "Reset failed with 0x%08x\n", hr);
1345
1346     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1347     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1348     ok(surface == NULL, "Depth stencil should be NULL\n");
1349
1350 cleanup:
1351     if(d3d8) IDirect3D8_Release(d3d8);
1352     if(device) IDirect3D8_Release(device);
1353 }
1354
1355 START_TEST(device)
1356 {
1357     HMODULE d3d8_handle = LoadLibraryA( "d3d8.dll" );
1358     if (!d3d8_handle)
1359     {
1360         skip("Could not load d3d8.dll\n");
1361         return;
1362     }
1363
1364     pDirect3DCreate8 = (void *)GetProcAddress( d3d8_handle, "Direct3DCreate8" );
1365     ok(pDirect3DCreate8 != NULL, "Failed to get address of Direct3DCreate8\n");
1366     if (pDirect3DCreate8)
1367     {
1368         IDirect3D8 *d3d8;
1369         d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1370         if(!d3d8)
1371         {
1372             skip("could not create D3D8\n");
1373             return;
1374         }
1375         IDirect3D8_Release(d3d8);
1376
1377         test_display_modes();
1378         test_shader_versions();
1379         test_swapchain();
1380         test_refcount();
1381         test_mipmap_levels();
1382         test_cursor();
1383         test_states();
1384         test_scene();
1385         test_shader();
1386         test_limits();
1387         test_lights();
1388         test_render_zero_triangles();
1389         test_depth_stencil_reset();
1390     }
1391 }