wined3d: SetDepthStencilSurface is always called when AutoDepthStencil is enabled.
[wine] / dlls / d3d9 / tests / device.c
1 /*
2  * Copyright (C) 2006 Vitaliy Margolen
3  * Copyright (C) 2006 Chris Robinson
4  * Copyright (C) 2006-2007 Stefan Dösinger(For CodeWeavers)
5  * Copyright 2007 Henri Verbeet
6  * Copyright (C) 2008 Rico Schüller
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #define COBJMACROS
24 #include <d3d9.h>
25 #include "wine/test.h"
26
27 static IDirect3D9 *(WINAPI *pDirect3DCreate9)(UINT);
28
29 static int get_refcount(IUnknown *object)
30 {
31     IUnknown_AddRef( object );
32     return IUnknown_Release( object );
33 }
34
35 #define CHECK_CALL(r,c,d,rc) \
36     if (SUCCEEDED(r)) {\
37         int tmp1 = get_refcount( (IUnknown *)d ); \
38         int rc_new = rc; \
39         ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
40     } else {\
41         trace("%s failed: %08x\n", c, r); \
42     }
43
44 #define CHECK_RELEASE(obj,d,rc) \
45     if (obj) { \
46         int tmp1, rc_new = rc; \
47         IUnknown_Release( obj ); \
48         tmp1 = get_refcount( (IUnknown *)d ); \
49         ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
50     }
51
52 #define CHECK_REFCOUNT(obj,rc) \
53     { \
54         int rc_new = rc; \
55         int count = get_refcount( (IUnknown *)obj ); \
56         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
57     }
58
59 #define CHECK_RELEASE_REFCOUNT(obj,rc) \
60     { \
61         int rc_new = rc; \
62         int count = IUnknown_Release( (IUnknown *)obj ); \
63         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
64     }
65
66 #define CHECK_ADDREF_REFCOUNT(obj,rc) \
67     { \
68         int rc_new = rc; \
69         int count = IUnknown_AddRef( (IUnknown *)obj ); \
70         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
71     }
72
73 #define CHECK_SURFACE_CONTAINER(obj,iid,expected) \
74     { \
75         void *container_ptr = (void *)0x1337c0d3; \
76         hr = IDirect3DSurface9_GetContainer(obj, &iid, &container_ptr); \
77         ok(SUCCEEDED(hr) && container_ptr == expected, "GetContainer returned: hr %#x, container_ptr %p. " \
78             "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, expected); \
79         if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr); \
80     }
81
82 static void check_mipmap_levels(IDirect3DDevice9 *device, UINT width, UINT height, UINT count)
83 {
84     IDirect3DBaseTexture9* texture = NULL;
85     HRESULT hr = IDirect3DDevice9_CreateTexture( device, width, height, 0, 0, 
86         D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, (IDirect3DTexture9**) &texture, NULL );
87        
88     if (SUCCEEDED(hr)) {
89         DWORD levels = IDirect3DBaseTexture9_GetLevelCount(texture);
90         ok(levels == count, "Invalid level count. Expected %d got %u\n", count, levels);
91     } else 
92         trace("CreateTexture failed: %08x\n", hr);
93
94     if (texture) IUnknown_Release( texture );
95 }
96
97 static void test_mipmap_levels(void)
98 {
99
100     HRESULT               hr;
101     HWND                  hwnd = NULL;
102
103     IDirect3D9            *pD3d = NULL;
104     IDirect3DDevice9      *pDevice = NULL;
105     D3DPRESENT_PARAMETERS d3dpp;
106     D3DDISPLAYMODE        d3ddm;
107  
108     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
109     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
110     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
111     ok(hwnd != NULL, "Failed to create window\n");
112     if (!pD3d || !hwnd) goto cleanup;
113
114     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
115     ZeroMemory( &d3dpp, sizeof(d3dpp) );
116     d3dpp.Windowed         = TRUE;
117     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
118     d3dpp.BackBufferFormat = d3ddm.Format;
119
120     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, hwnd,
121                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
122     ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "Failed to create IDirect3D9Device (%08x)\n", hr);
123     if (FAILED(hr)) {
124         skip("failed to create a d3d device\n");
125         goto cleanup;
126     }
127
128     check_mipmap_levels(pDevice, 32, 32, 6);
129     check_mipmap_levels(pDevice, 256, 1, 9);
130     check_mipmap_levels(pDevice, 1, 256, 9);
131     check_mipmap_levels(pDevice, 1, 1, 1);
132
133     cleanup:
134     if (pD3d)     IUnknown_Release( pD3d );
135     if (pDevice)  IUnknown_Release( pDevice );
136     DestroyWindow( hwnd );
137 }
138
139 static void test_swapchain(void)
140 {
141     HRESULT                      hr;
142     HWND                         hwnd               = NULL;
143     IDirect3D9                  *pD3d               = NULL;
144     IDirect3DDevice9            *pDevice            = NULL;
145     IDirect3DSwapChain9         *swapchain0         = NULL;
146     IDirect3DSwapChain9         *swapchain1         = NULL;
147     IDirect3DSwapChain9         *swapchain2         = NULL;
148     IDirect3DSwapChain9         *swapchain3         = NULL;
149     IDirect3DSwapChain9         *swapchainX         = NULL;
150     IDirect3DSurface9           *backbuffer         = NULL;
151     D3DPRESENT_PARAMETERS        d3dpp;
152     D3DDISPLAYMODE               d3ddm;
153
154     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
155     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
156     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
157     ok(hwnd != NULL, "Failed to create window\n");
158     if (!pD3d || !hwnd) goto cleanup;
159
160     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
161     ZeroMemory( &d3dpp, sizeof(d3dpp) );
162     d3dpp.Windowed         = TRUE;
163     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
164     d3dpp.BackBufferFormat = d3ddm.Format;
165     d3dpp.BackBufferCount  = 0;
166
167     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
168                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
169     ok(hr == S_OK || hr == D3DERR_NOTAVAILABLE,
170        "Failed to create IDirect3D9Device (%08x)\n", hr);
171     if (FAILED(hr)) goto cleanup;
172
173     /* Check if the back buffer count was modified */
174     ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
175
176     /* Get the implicit swapchain */
177     hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &swapchain0);
178     ok(SUCCEEDED(hr), "Failed to get the impicit swapchain (%08x)\n", hr);
179     if(swapchain0) IDirect3DSwapChain9_Release(swapchain0);
180
181     /* Check if there is a back buffer */
182     hr = IDirect3DSwapChain9_GetBackBuffer(swapchain0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
183     ok(SUCCEEDED(hr), "Failed to get the back buffer (%08x)\n", hr);
184     ok(backbuffer != NULL, "The back buffer is NULL\n");
185     if(backbuffer) IDirect3DSurface9_Release(backbuffer);
186
187     /* Try to get a nonexistent swapchain */
188     hr = IDirect3DDevice9_GetSwapChain(pDevice, 1, &swapchainX);
189     ok(hr == D3DERR_INVALIDCALL, "GetSwapChain on an nonexistent swapchain returned (%08x)\n", hr);
190     ok(swapchainX == NULL, "Swapchain 1 is %p\n", swapchainX);
191     if(swapchainX) IDirect3DSwapChain9_Release(swapchainX);
192
193     /* Create a bunch of swapchains */
194     d3dpp.BackBufferCount = 0;
195     hr = IDirect3DDevice9_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 = IDirect3DDevice9_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain2);
201     ok(SUCCEEDED(hr), "Failed to create a swapchain (%08x)\n", hr);
202
203     d3dpp.BackBufferCount  = 2;
204     hr = IDirect3DDevice9_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 = IDirect3DSwapChain9_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) IDirect3DSurface9_Release(backbuffer);
213
214         backbuffer = (void *) 0xdeadbeef;
215         hr = IDirect3DSwapChain9_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) IDirect3DSurface9_Release(backbuffer);
219
220         backbuffer = (void *) 0xdeadbeef;
221         hr = IDirect3DSwapChain9_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) IDirect3DSurface9_Release(backbuffer);
225
226         backbuffer = (void *) 0xdeadbeef;
227         hr = IDirect3DSwapChain9_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) IDirect3DSurface9_Release(backbuffer);
231     }
232
233     /* Check the back buffers of the swapchains */
234     /* Swapchain 1, created with backbuffercount 0 */
235     hr = IDirect3DSwapChain9_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) IDirect3DSurface9_Release(backbuffer);
239
240     backbuffer = (void *) 0xdeadbeef;
241     hr = IDirect3DSwapChain9_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) IDirect3DSurface9_Release(backbuffer);
245
246     /* Swapchain 2 - created with backbuffercount 1 */
247     backbuffer = (void *) 0xdeadbeef;
248     hr = IDirect3DSwapChain9_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) IDirect3DSurface9_Release(backbuffer);
252
253     backbuffer = (void *) 0xdeadbeef;
254     hr = IDirect3DSwapChain9_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) IDirect3DSurface9_Release(backbuffer);
258
259     backbuffer = (void *) 0xdeadbeef;
260     hr = IDirect3DSwapChain9_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) IDirect3DSurface9_Release(backbuffer);
264
265     /* Try getSwapChain on a manually created swapchain
266      * it should fail, apparently GetSwapChain only returns implicit swapchains
267      */
268     swapchainX = (void *) 0xdeadbeef;
269     hr = IDirect3DDevice9_GetSwapChain(pDevice, 1, &swapchainX);
270     ok(hr == D3DERR_INVALIDCALL, "Failed to get the second swapchain (%08x)\n", hr);
271     ok(swapchainX == NULL, "The swapchain pointer is %p\n", swapchainX);
272     if(swapchainX && swapchainX != (void *) 0xdeadbeef ) IDirect3DSwapChain9_Release(swapchainX);
273
274     cleanup:
275     if(swapchain1) IDirect3DSwapChain9_Release(swapchain1);
276     if(swapchain2) IDirect3DSwapChain9_Release(swapchain2);
277     if(swapchain3) IDirect3DSwapChain9_Release(swapchain3);
278     if(pDevice) IDirect3DDevice9_Release(pDevice);
279     if(pD3d) IDirect3DDevice9_Release(pD3d);
280     DestroyWindow( hwnd );
281 }
282
283 /* Shared between two functions */
284 static const DWORD simple_vs[] = {0xFFFE0101,       /* vs_1_1               */
285     0x0000001F, 0x80000000, 0x900F0000,             /* dcl_position0 v0     */
286     0x00000009, 0xC0010000, 0x90E40000, 0xA0E40000, /* dp4 oPos.x, v0, c0   */
287     0x00000009, 0xC0020000, 0x90E40000, 0xA0E40001, /* dp4 oPos.y, v0, c1   */
288     0x00000009, 0xC0040000, 0x90E40000, 0xA0E40002, /* dp4 oPos.z, v0, c2   */
289     0x00000009, 0xC0080000, 0x90E40000, 0xA0E40003, /* dp4 oPos.w, v0, c3   */
290     0x0000FFFF};                                    /* END                  */
291
292 static void test_refcount(void)
293 {
294     HRESULT                      hr;
295     HWND                         hwnd               = NULL;
296     IDirect3D9                  *pD3d               = NULL;
297     IDirect3DDevice9            *pDevice            = NULL;
298     IDirect3DVertexBuffer9      *pVertexBuffer      = NULL;
299     IDirect3DIndexBuffer9       *pIndexBuffer       = NULL;
300     IDirect3DVertexDeclaration9 *pVertexDeclaration = NULL;
301     IDirect3DVertexShader9      *pVertexShader      = NULL;
302     IDirect3DPixelShader9       *pPixelShader       = NULL;
303     IDirect3DCubeTexture9       *pCubeTexture       = NULL;
304     IDirect3DTexture9           *pTexture           = NULL;
305     IDirect3DVolumeTexture9     *pVolumeTexture     = NULL;
306     IDirect3DVolume9            *pVolumeLevel       = NULL;
307     IDirect3DSurface9           *pStencilSurface    = NULL;
308     IDirect3DSurface9           *pOffscreenSurface  = NULL;
309     IDirect3DSurface9           *pRenderTarget      = NULL;
310     IDirect3DSurface9           *pRenderTarget2     = NULL;
311     IDirect3DSurface9           *pRenderTarget3     = NULL;
312     IDirect3DSurface9           *pTextureLevel      = NULL;
313     IDirect3DSurface9           *pBackBuffer        = NULL;
314     IDirect3DStateBlock9        *pStateBlock        = NULL;
315     IDirect3DStateBlock9        *pStateBlock1       = NULL;
316     IDirect3DSwapChain9         *pSwapChain         = NULL;
317     IDirect3DQuery9             *pQuery             = NULL;
318     D3DPRESENT_PARAMETERS        d3dpp;
319     D3DDISPLAYMODE               d3ddm;
320     int                          refcount = 0, tmp;
321
322     D3DVERTEXELEMENT9 decl[] =
323     {
324         D3DDECL_END()
325     };
326     static DWORD simple_ps[] = {0xFFFF0101,                                     /* ps_1_1                       */
327         0x00000051, 0xA00F0001, 0x3F800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0  */
328         0x00000042, 0xB00F0000,                                                 /* tex t0                       */
329         0x00000008, 0x800F0000, 0xA0E40001, 0xA0E40000,                         /* dp3 r0, c1, c0               */
330         0x00000005, 0x800F0000, 0x90E40000, 0x80E40000,                         /* mul r0, v0, r0               */
331         0x00000005, 0x800F0000, 0xB0E40000, 0x80E40000,                         /* mul r0, t0, r0               */
332         0x0000FFFF};                                                            /* END                          */
333
334
335     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
336     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
337     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
338     ok(hwnd != NULL, "Failed to create window\n");
339     if (!pD3d || !hwnd) goto cleanup;
340
341     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
342     ZeroMemory( &d3dpp, sizeof(d3dpp) );
343     d3dpp.Windowed         = TRUE;
344     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
345     d3dpp.BackBufferFormat = d3ddm.Format;
346     d3dpp.EnableAutoDepthStencil = TRUE;
347     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
348
349     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
350                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
351     ok(hr == S_OK || hr == D3DERR_NOTAVAILABLE,
352        "Failed to create IDirect3D9Device (%08x)\n", hr);
353     if (FAILED(hr)) goto cleanup;
354
355     refcount = get_refcount( (IUnknown *)pDevice );
356     ok(refcount == 1, "Invalid device RefCount %d\n", refcount);
357
358     /**
359      * Check refcount of implicit surfaces and implicit swapchain. Findings:
360      *   - the container is the device OR swapchain
361      *   - they hold a reference to the device
362      *   - they are created with a refcount of 0 (Get/Release returns original refcount)
363      *   - they are not freed if refcount reaches 0.
364      *   - the refcount is not forwarded to the container.
365      */
366     hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &pSwapChain);
367     CHECK_CALL( hr, "GetSwapChain", pDevice, ++refcount);
368     if (pSwapChain)
369     {
370         CHECK_REFCOUNT( pSwapChain, 1);
371
372         hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget);
373         CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
374         CHECK_REFCOUNT( pSwapChain, 1);
375         if(pRenderTarget)
376         {
377             CHECK_SURFACE_CONTAINER( pRenderTarget, IID_IDirect3DSwapChain9, pSwapChain);
378             CHECK_REFCOUNT( pRenderTarget, 1);
379
380             CHECK_ADDREF_REFCOUNT(pRenderTarget, 2);
381             CHECK_REFCOUNT(pDevice, refcount);
382             CHECK_RELEASE_REFCOUNT(pRenderTarget, 1);
383             CHECK_REFCOUNT(pDevice, refcount);
384
385             hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget);
386             CHECK_CALL( hr, "GetRenderTarget", pDevice, refcount);
387             CHECK_REFCOUNT( pRenderTarget, 2);
388             CHECK_RELEASE_REFCOUNT( pRenderTarget, 1);
389             CHECK_RELEASE_REFCOUNT( pRenderTarget, 0);
390             CHECK_REFCOUNT( pDevice, --refcount);
391
392             /* The render target is released with the device, so AddRef with refcount=0 is fine here. */
393             CHECK_ADDREF_REFCOUNT(pRenderTarget, 1);
394             CHECK_REFCOUNT(pDevice, ++refcount);
395             CHECK_RELEASE_REFCOUNT(pRenderTarget, 0);
396             CHECK_REFCOUNT(pDevice, --refcount);
397         }
398
399         /* Render target and back buffer are identical. */
400         hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, 0, &pBackBuffer);
401         CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
402         if(pBackBuffer)
403         {
404             CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
405             ok(pRenderTarget == pBackBuffer, "RenderTarget=%p and BackBuffer=%p should be the same.\n",
406             pRenderTarget, pBackBuffer);
407             pBackBuffer = NULL;
408         }
409         CHECK_REFCOUNT( pDevice, --refcount);
410
411         hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pStencilSurface);
412         CHECK_CALL( hr, "GetDepthStencilSurface", pDevice, ++refcount);
413         CHECK_REFCOUNT( pSwapChain, 1);
414         if(pStencilSurface)
415         {
416             CHECK_SURFACE_CONTAINER( pStencilSurface, IID_IDirect3DDevice9, pDevice);
417             CHECK_REFCOUNT( pStencilSurface, 1);
418
419             CHECK_ADDREF_REFCOUNT(pStencilSurface, 2);
420             CHECK_REFCOUNT(pDevice, refcount);
421             CHECK_RELEASE_REFCOUNT(pStencilSurface, 1);
422             CHECK_REFCOUNT(pDevice, refcount);
423
424             CHECK_RELEASE_REFCOUNT( pStencilSurface, 0);
425             CHECK_REFCOUNT( pDevice, --refcount);
426
427             /* The stencil surface is released with the device, so AddRef with refcount=0 is fine here. */
428             CHECK_ADDREF_REFCOUNT(pStencilSurface, 1);
429             CHECK_REFCOUNT(pDevice, ++refcount);
430             CHECK_RELEASE_REFCOUNT(pStencilSurface, 0);
431             CHECK_REFCOUNT(pDevice, --refcount);
432             pStencilSurface = NULL;
433         }
434
435         CHECK_RELEASE_REFCOUNT( pSwapChain, 0);
436         CHECK_REFCOUNT( pDevice, --refcount);
437
438         /* The implicit swapchwin is released with the device, so AddRef with refcount=0 is fine here. */
439         CHECK_ADDREF_REFCOUNT(pSwapChain, 1);
440         CHECK_REFCOUNT(pDevice, ++refcount);
441         CHECK_RELEASE_REFCOUNT(pSwapChain, 0);
442         CHECK_REFCOUNT(pDevice, --refcount);
443         pSwapChain = NULL;
444     }
445
446     /* Buffers */
447     hr = IDirect3DDevice9_CreateIndexBuffer( pDevice, 16, 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &pIndexBuffer, NULL );
448     CHECK_CALL( hr, "CreateIndexBuffer", pDevice, ++refcount );
449     if(pIndexBuffer)
450     {
451         tmp = get_refcount( (IUnknown *)pIndexBuffer );
452
453         hr = IDirect3DDevice9_SetIndices(pDevice, pIndexBuffer);
454         CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
455         hr = IDirect3DDevice9_SetIndices(pDevice, NULL);
456         CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
457     }
458
459     hr = IDirect3DDevice9_CreateVertexBuffer( pDevice, 16, 0, D3DFVF_XYZ, D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
460     CHECK_CALL( hr, "CreateVertexBuffer", pDevice, ++refcount );
461     if(pVertexBuffer)
462     {
463         IDirect3DVertexBuffer9 *pVBuf = (void*)~0;
464         UINT offset = ~0;
465         UINT stride = ~0;
466
467         tmp = get_refcount( (IUnknown *)pVertexBuffer );
468
469         hr = IDirect3DDevice9_SetStreamSource(pDevice, 0, pVertexBuffer, 0, 3 * sizeof(float));
470         CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
471         hr = IDirect3DDevice9_SetStreamSource(pDevice, 0, NULL, 0, 0);
472         CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
473
474         hr = IDirect3DDevice9_GetStreamSource(pDevice, 0, &pVBuf, &offset, &stride);
475         ok(SUCCEEDED(hr), "GetStreamSource did not succeed with NULL stream!\n");
476         ok(pVBuf==NULL, "pVBuf not NULL (%p)!\n", pVBuf);
477         ok(stride==3*sizeof(float), "stride not 3 floats (got %u)!\n", stride);
478         ok(offset==0, "offset not 0 (got %u)!\n", offset);
479     }
480     /* Shaders */
481     hr = IDirect3DDevice9_CreateVertexDeclaration( pDevice, decl, &pVertexDeclaration );
482     CHECK_CALL( hr, "CreateVertexDeclaration", pDevice, ++refcount );
483     hr = IDirect3DDevice9_CreateVertexShader( pDevice, simple_vs, &pVertexShader );
484     CHECK_CALL( hr, "CreateVertexShader", pDevice, ++refcount );
485     hr = IDirect3DDevice9_CreatePixelShader( pDevice, simple_ps, &pPixelShader );
486     CHECK_CALL( hr, "CreatePixelShader", pDevice, ++refcount );
487     /* Textures */
488     hr = IDirect3DDevice9_CreateTexture( pDevice, 32, 32, 3, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pTexture, NULL );
489     CHECK_CALL( hr, "CreateTexture", pDevice, ++refcount );
490     if (pTexture)
491     {
492         tmp = get_refcount( (IUnknown *)pTexture );
493
494         /* SetTexture should not increase refcounts */
495         hr = IDirect3DDevice9_SetTexture(pDevice, 0, (IDirect3DBaseTexture9 *) pTexture);
496         CHECK_CALL( hr, "SetTexture", pTexture, tmp);
497         hr = IDirect3DDevice9_SetTexture(pDevice, 0, NULL);
498         CHECK_CALL( hr, "SetTexture", pTexture, tmp);
499
500         /* This should not increment device refcount */
501         hr = IDirect3DTexture9_GetSurfaceLevel( pTexture, 1, &pTextureLevel );
502         CHECK_CALL( hr, "GetSurfaceLevel", pDevice, refcount );
503         /* But should increment texture's refcount */
504         CHECK_REFCOUNT( pTexture, tmp+1 );
505         /* Because the texture and surface refcount are identical */
506         if (pTextureLevel)
507         {
508             CHECK_REFCOUNT        ( pTextureLevel, tmp+1 );
509             CHECK_ADDREF_REFCOUNT ( pTextureLevel, tmp+2 );
510             CHECK_REFCOUNT        ( pTexture     , tmp+2 );
511             CHECK_RELEASE_REFCOUNT( pTextureLevel, tmp+1 );
512             CHECK_REFCOUNT        ( pTexture     , tmp+1 );
513             CHECK_RELEASE_REFCOUNT( pTexture     , tmp   );
514             CHECK_REFCOUNT        ( pTextureLevel, tmp   );
515         }
516     }
517     hr = IDirect3DDevice9_CreateCubeTexture( pDevice, 32, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pCubeTexture, NULL );
518     CHECK_CALL( hr, "CreateCubeTexture", pDevice, ++refcount );
519     hr = IDirect3DDevice9_CreateVolumeTexture( pDevice, 32, 32, 2, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pVolumeTexture, NULL );
520     CHECK_CALL( hr, "CreateVolumeTexture", pDevice, ++refcount );
521     if (pVolumeTexture)
522     {
523         tmp = get_refcount( (IUnknown *)pVolumeTexture );
524
525         /* This should not increment device refcount */
526         hr = IDirect3DVolumeTexture9_GetVolumeLevel(pVolumeTexture, 0, &pVolumeLevel);
527         CHECK_CALL( hr, "GetVolumeLevel", pDevice, refcount );
528         /* But should increment volume texture's refcount */
529         CHECK_REFCOUNT( pVolumeTexture, tmp+1 );
530         /* Because the volume texture and volume refcount are identical */
531         if (pVolumeLevel)
532         {
533             CHECK_REFCOUNT        ( pVolumeLevel  , tmp+1 );
534             CHECK_ADDREF_REFCOUNT ( pVolumeLevel  , tmp+2 );
535             CHECK_REFCOUNT        ( pVolumeTexture, tmp+2 );
536             CHECK_RELEASE_REFCOUNT( pVolumeLevel  , tmp+1 );
537             CHECK_REFCOUNT        ( pVolumeTexture, tmp+1 );
538             CHECK_RELEASE_REFCOUNT( pVolumeTexture, tmp   );
539             CHECK_REFCOUNT        ( pVolumeLevel  , tmp   );
540         }
541     }
542     /* Surfaces */
543     hr = IDirect3DDevice9_CreateDepthStencilSurface( pDevice, 32, 32, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, TRUE, &pStencilSurface, NULL );
544     CHECK_CALL( hr, "CreateDepthStencilSurface", pDevice, ++refcount );
545     CHECK_REFCOUNT( pStencilSurface, 1 );
546     hr = IDirect3DDevice9_CreateOffscreenPlainSurface( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pOffscreenSurface, NULL );
547     CHECK_CALL( hr, "CreateOffscreenPlainSurface", pDevice, ++refcount );
548     CHECK_REFCOUNT( pOffscreenSurface, 1 );
549     hr = IDirect3DDevice9_CreateRenderTarget( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &pRenderTarget3, NULL );
550     CHECK_CALL( hr, "CreateRenderTarget", pDevice, ++refcount );
551     CHECK_REFCOUNT( pRenderTarget3, 1 );
552     /* Misc */
553     hr = IDirect3DDevice9_CreateStateBlock( pDevice, D3DSBT_ALL, &pStateBlock );
554     CHECK_CALL( hr, "CreateStateBlock", pDevice, ++refcount );
555     hr = IDirect3DDevice9_CreateAdditionalSwapChain( pDevice, &d3dpp, &pSwapChain );
556     CHECK_CALL( hr, "CreateAdditionalSwapChain", pDevice, ++refcount );
557     if(pSwapChain)
558     {
559         /* check implicit back buffer */
560         hr = IDirect3DSwapChain9_GetBackBuffer(pSwapChain, 0, 0, &pBackBuffer);
561         CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
562         CHECK_REFCOUNT( pSwapChain, 1);
563         if(pBackBuffer)
564         {
565             CHECK_SURFACE_CONTAINER( pBackBuffer, IID_IDirect3DSwapChain9, pSwapChain);
566             CHECK_REFCOUNT( pBackBuffer, 1);
567             CHECK_RELEASE_REFCOUNT( pBackBuffer, 0);
568             CHECK_REFCOUNT( pDevice, --refcount);
569
570             /* The back buffer is released with the swapchain, so AddRef with refcount=0 is fine here. */
571             CHECK_ADDREF_REFCOUNT(pBackBuffer, 1);
572             CHECK_REFCOUNT(pDevice, ++refcount);
573             CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
574             CHECK_REFCOUNT(pDevice, --refcount);
575             pBackBuffer = NULL;
576         }
577         CHECK_REFCOUNT( pSwapChain, 1);
578     }
579     hr = IDirect3DDevice9_CreateQuery( pDevice, D3DQUERYTYPE_EVENT, &pQuery );
580     CHECK_CALL( hr, "CreateQuery", pDevice, ++refcount );
581
582     hr = IDirect3DDevice9_BeginStateBlock( pDevice );
583     CHECK_CALL( hr, "BeginStateBlock", pDevice, refcount );
584     hr = IDirect3DDevice9_EndStateBlock( pDevice, &pStateBlock1 );
585     CHECK_CALL( hr, "EndStateBlock", pDevice, ++refcount );
586
587     /* The implicit render target is not freed if refcount reaches 0.
588      * Otherwise GetRenderTarget would re-allocate it and the pointer would change.*/
589     hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget2);
590     CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
591     if(pRenderTarget2)
592     {
593         CHECK_RELEASE_REFCOUNT(pRenderTarget2, 0);
594         ok(pRenderTarget == pRenderTarget2, "RenderTarget=%p and RenderTarget2=%p should be the same.\n",
595            pRenderTarget, pRenderTarget2);
596         CHECK_REFCOUNT( pDevice, --refcount);
597         pRenderTarget2 = NULL;
598     }
599     pRenderTarget = NULL;
600
601 cleanup:
602     CHECK_RELEASE(pDevice,              pDevice, --refcount);
603
604     /* Buffers */
605     CHECK_RELEASE(pVertexBuffer,        pDevice, --refcount);
606     CHECK_RELEASE(pIndexBuffer,         pDevice, --refcount);
607     /* Shaders */
608     CHECK_RELEASE(pVertexDeclaration,   pDevice, --refcount);
609     CHECK_RELEASE(pVertexShader,        pDevice, --refcount);
610     CHECK_RELEASE(pPixelShader,         pDevice, --refcount);
611     /* Textures */
612     CHECK_RELEASE(pTextureLevel,        pDevice, --refcount);
613     CHECK_RELEASE(pCubeTexture,         pDevice, --refcount);
614     CHECK_RELEASE(pVolumeTexture,       pDevice, --refcount);
615     /* Surfaces */
616     CHECK_RELEASE(pStencilSurface,      pDevice, --refcount);
617     CHECK_RELEASE(pOffscreenSurface,    pDevice, --refcount);
618     CHECK_RELEASE(pRenderTarget3,       pDevice, --refcount);
619     /* Misc */
620     CHECK_RELEASE(pStateBlock,          pDevice, --refcount);
621     CHECK_RELEASE(pSwapChain,           pDevice, --refcount);
622     CHECK_RELEASE(pQuery,               pDevice, --refcount);
623     /* This will destroy device - cannot check the refcount here */
624     if (pStateBlock1)         CHECK_RELEASE_REFCOUNT( pStateBlock1, 0);
625
626     if (pD3d)                 CHECK_RELEASE_REFCOUNT( pD3d, 0);
627
628     DestroyWindow( hwnd );
629 }
630
631 static void test_cursor(void)
632 {
633     HRESULT                      hr;
634     HWND                         hwnd               = NULL;
635     IDirect3D9                  *pD3d               = NULL;
636     IDirect3DDevice9            *pDevice            = NULL;
637     D3DPRESENT_PARAMETERS        d3dpp;
638     D3DDISPLAYMODE               d3ddm;
639     CURSORINFO                   info;
640     IDirect3DSurface9 *cursor = NULL;
641     HCURSOR cur;
642
643     memset(&info, 0, sizeof(info));
644     info.cbSize = sizeof(info);
645     ok(GetCursorInfo(&info), "GetCursorInfo failed\n");
646     cur = info.hCursor;
647
648     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
649     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
650     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
651     ok(hwnd != NULL, "Failed to create window\n");
652     if (!pD3d || !hwnd) goto cleanup;
653
654     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
655     ZeroMemory( &d3dpp, sizeof(d3dpp) );
656     d3dpp.Windowed         = TRUE;
657     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
658     d3dpp.BackBufferFormat = d3ddm.Format;
659
660     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
661                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
662     ok(hr == S_OK || hr == D3DERR_NOTAVAILABLE,
663        "Failed to create IDirect3D9Device (%08x)\n", hr);
664     if (FAILED(hr)) goto cleanup;
665
666     IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 32, 32, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &cursor, 0);
667     ok(cursor != NULL, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %08x\n", hr);
668
669     /* Initially hidden */
670     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
671     ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
672
673     /* Not enabled without a surface*/
674     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
675     ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
676
677     /* Fails */
678     hr = IDirect3DDevice9_SetCursorProperties(pDevice, 0, 0, NULL);
679     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_SetCursorProperties returned %08x\n", hr);
680
681     hr = IDirect3DDevice9_SetCursorProperties(pDevice, 0, 0, cursor);
682     ok(hr == D3D_OK, "IDirect3DDevice9_SetCursorProperties returned %08x\n", hr);
683
684     IDirect3DSurface9_Release(cursor);
685
686     memset(&info, 0, sizeof(info));
687     info.cbSize = sizeof(info);
688     ok(GetCursorInfo(&info), "GetCursorInfo failed\n");
689     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
690     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
691
692     /* Still hidden */
693     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
694     ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
695
696     /* Enabled now*/
697     hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
698     ok(hr == TRUE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
699
700     /* GDI cursor unchanged */
701     memset(&info, 0, sizeof(info));
702     info.cbSize = sizeof(info);
703     ok(GetCursorInfo(&info), "GetCursorInfo failed\n");
704     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
705     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
706
707 cleanup:
708     if(pDevice) IDirect3D9_Release(pDevice);
709     if(pD3d) IDirect3D9_Release(pD3d);
710     DestroyWindow( hwnd );
711 }
712
713 static void test_reset(void)
714 {
715     HRESULT                      hr;
716     HWND                         hwnd               = NULL;
717     IDirect3D9                  *pD3d               = NULL;
718     IDirect3DDevice9            *pDevice            = NULL;
719     D3DPRESENT_PARAMETERS        d3dpp;
720     D3DDISPLAYMODE               d3ddm, d3ddm2;
721     D3DVIEWPORT9                 vp;
722     DWORD                        width, orig_width = GetSystemMetrics(SM_CXSCREEN);
723     DWORD                        height, orig_height = GetSystemMetrics(SM_CYSCREEN);
724     IDirect3DSwapChain9          *pSwapchain;
725     IDirect3DSurface9            *surface;
726     IDirect3DTexture9            *texture;
727     IDirect3DVertexShader9       *shader;
728     UINT                         i, adapter_mode_count;
729     D3DLOCKED_RECT               lockrect;
730     struct
731     {
732         UINT w;
733         UINT h;
734     } *modes = NULL;
735     UINT mode_count = 0;
736
737     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
738     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
739     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
740     ok(hwnd != NULL, "Failed to create window\n");
741     if (!pD3d || !hwnd) goto cleanup;
742
743     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
744     adapter_mode_count = IDirect3D9_GetAdapterModeCount(pD3d, D3DADAPTER_DEFAULT, d3ddm.Format);
745     modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*modes) * adapter_mode_count);
746     for(i = 0; i < adapter_mode_count; ++i)
747     {
748         int j;
749         ZeroMemory( &d3ddm2, sizeof(d3ddm2) );
750         hr = IDirect3D9_EnumAdapterModes(pD3d, D3DADAPTER_DEFAULT, d3ddm.Format, i, &d3ddm2);
751         ok(hr == D3D_OK, "IDirect3D9_EnumAdapterModes returned %#x\n", hr);
752
753         for (j = 0; j < mode_count; ++j)
754         {
755             if (modes[j].w == d3ddm2.Width && modes[j].h == d3ddm2.Height)
756                 break;
757         }
758         if (j == mode_count)
759         {
760             modes[j].w = d3ddm2.Width;
761             modes[j].h = d3ddm2.Height;
762             ++mode_count;
763         }
764
765         /* We use them as invalid modes */
766         if((d3ddm2.Width == 801 && d3ddm2.Height == 600) ||
767            (d3ddm2.Width == 32 && d3ddm2.Height == 32)) {
768             skip("This system supports a screen resolution of %dx%d, not running mode tests\n",
769                  d3ddm2.Width, d3ddm2.Height);
770             goto cleanup;
771         }
772     }
773
774     if (mode_count < 2)
775     {
776         skip("Less than 2 modes supported, skipping mode tests\n");
777         goto cleanup;
778     }
779
780     i = 0;
781     if (modes[i].w == orig_width && modes[i].h == orig_height) ++i;
782
783     ZeroMemory( &d3dpp, sizeof(d3dpp) );
784     d3dpp.Windowed         = FALSE;
785     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
786     d3dpp.BackBufferWidth  = modes[i].w;
787     d3dpp.BackBufferHeight = modes[i].h;
788     d3dpp.BackBufferFormat = d3ddm.Format;
789     d3dpp.EnableAutoDepthStencil = TRUE;
790     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
791
792     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
793                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
794
795     if(FAILED(hr))
796     {
797         skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
798         goto cleanup;
799     }
800     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
801     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after creation returned %#x\n", hr);
802
803     width = GetSystemMetrics(SM_CXSCREEN);
804     height = GetSystemMetrics(SM_CYSCREEN);
805     ok(width == modes[i].w, "Screen width is %u, expected %u\n", width, modes[i].w);
806     ok(height == modes[i].h, "Screen height is %u, expected %u\n", height, modes[i].h);
807
808     hr = IDirect3DDevice9_GetViewport(pDevice, &vp);
809     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %08x\n", hr);
810     if(SUCCEEDED(hr))
811     {
812         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
813         ok(vp.Y == 0, "D3DVIEWPORT->Y = %d\n", vp.Y);
814         ok(vp.Width == modes[i].w, "D3DVIEWPORT->Width = %u, expected %u\n", vp.Width, modes[i].w);
815         ok(vp.Height == modes[i].h, "D3DVIEWPORT->Height = %u, expected %u\n", vp.Height, modes[i].h);
816         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %f\n", vp.MinZ);
817         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %f\n", vp.MaxZ);
818     }
819
820     i = 1;
821     vp.X = 10;
822     vp.Y = 20;
823     vp.MinZ = 2;
824     vp.MaxZ = 3;
825     hr = IDirect3DDevice9_SetViewport(pDevice, &vp);
826     ok(hr == D3D_OK, "IDirect3DDevice9_SetViewport failed with %08x\n", hr);
827
828     ZeroMemory( &d3dpp, sizeof(d3dpp) );
829     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
830     d3dpp.Windowed         = FALSE;
831     d3dpp.BackBufferWidth  = modes[i].w;
832     d3dpp.BackBufferHeight = modes[i].h;
833     d3dpp.BackBufferFormat = d3ddm.Format;
834     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
835     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
836     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
837     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
838
839     ZeroMemory(&vp, sizeof(vp));
840     hr = IDirect3DDevice9_GetViewport(pDevice, &vp);
841     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %08x\n", hr);
842     if(SUCCEEDED(hr))
843     {
844         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
845         ok(vp.Y == 0, "D3DVIEWPORT->Y = %d\n", vp.Y);
846         ok(vp.Width == modes[i].w, "D3DVIEWPORT->Width = %u, expected %u\n", vp.Width, modes[i].w);
847         ok(vp.Height == modes[i].h, "D3DVIEWPORT->Height = %u, expected %u\n", vp.Height, modes[i].h);
848         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %f\n", vp.MinZ);
849         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %f\n", vp.MaxZ);
850     }
851
852     width = GetSystemMetrics(SM_CXSCREEN);
853     height = GetSystemMetrics(SM_CYSCREEN);
854     ok(width == modes[i].w, "Screen width is %u, expected %u\n", width, modes[i].w);
855     ok(height == modes[i].h, "Screen height is %u, expected %u\n", height, modes[i].h);
856
857     hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &pSwapchain);
858     ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %08x\n", hr);
859     if(SUCCEEDED(hr))
860     {
861         ZeroMemory(&d3dpp, sizeof(d3dpp));
862         hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
863         ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %08x\n", hr);
864         if(SUCCEEDED(hr))
865         {
866             ok(d3dpp.BackBufferWidth == modes[i].w, "Back buffer width is %u, expected %u\n",
867                     d3dpp.BackBufferWidth, modes[i].w);
868             ok(d3dpp.BackBufferHeight == modes[i].h, "Back buffer height is %u, expected %u\n",
869                     d3dpp.BackBufferHeight, modes[i].h);
870         }
871         IDirect3DSwapChain9_Release(pSwapchain);
872     }
873
874     ZeroMemory( &d3dpp, sizeof(d3dpp) );
875     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
876     d3dpp.Windowed         = TRUE;
877     d3dpp.BackBufferWidth  = 400;
878     d3dpp.BackBufferHeight = 300;
879     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
880     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
881     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
882     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
883
884     width = GetSystemMetrics(SM_CXSCREEN);
885     height = GetSystemMetrics(SM_CYSCREEN);
886     ok(width == orig_width, "Screen width is %d\n", width);
887     ok(height == orig_height, "Screen height is %d\n", height);
888
889     ZeroMemory(&vp, sizeof(vp));
890     hr = IDirect3DDevice9_GetViewport(pDevice, &vp);
891     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %08x\n", hr);
892     if(SUCCEEDED(hr))
893     {
894         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
895         ok(vp.Y == 0, "D3DVIEWPORT->Y = %d\n", vp.Y);
896         ok(vp.Width == 400, "D3DVIEWPORT->Width = %d\n", vp.Width);
897         ok(vp.Height == 300, "D3DVIEWPORT->Height = %d\n", vp.Height);
898         ok(vp.MinZ == 0, "D3DVIEWPORT->MinZ = %f\n", vp.MinZ);
899         ok(vp.MaxZ == 1, "D3DVIEWPORT->MaxZ = %f\n", vp.MaxZ);
900     }
901
902     hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &pSwapchain);
903     ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %08x\n", hr);
904     if(SUCCEEDED(hr))
905     {
906         ZeroMemory(&d3dpp, sizeof(d3dpp));
907         hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
908         ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %08x\n", hr);
909         if(SUCCEEDED(hr))
910         {
911             ok(d3dpp.BackBufferWidth == 400, "Back buffer width is %d\n", d3dpp.BackBufferWidth);
912             ok(d3dpp.BackBufferHeight == 300, "Back buffer height is %d\n", d3dpp.BackBufferHeight);
913         }
914         IDirect3DSwapChain9_Release(pSwapchain);
915     }
916
917     ZeroMemory( &d3dpp, sizeof(d3dpp) );
918     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
919     d3dpp.Windowed         = TRUE;
920     d3dpp.BackBufferWidth  = 400;
921     d3dpp.BackBufferHeight = 300;
922
923     /* _Reset fails if there is a resource in the default pool */
924     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 16, 16, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &surface, NULL);
925     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
926     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
927     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset failed with %08x\n", hr);
928     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
929     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
930     IDirect3DSurface9_Release(surface);
931     /* Reset again to get the device out of the lost state */
932     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
933     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
934     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
935     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
936
937     /* Scratch, sysmem and managed pools are fine */
938     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SCRATCH, &surface, NULL);
939     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
940     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
941     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
942     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
943     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
944     IDirect3DSurface9_Release(surface);
945
946     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface, NULL);
947     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
948     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
949     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
950     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
951     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
952     IDirect3DSurface9_Release(surface);
953
954     /* The depth stencil should get reset to the auto depth stencil when present. */
955     hr = IDirect3DDevice9_SetDepthStencilSurface(pDevice, NULL);
956     ok(hr == D3D_OK, "SetDepthStencilSurface failed with 0x%08x\n", hr);
957
958     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &surface);
959     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
960     ok(surface == NULL, "Depth stencil should be NULL\n");
961
962     d3dpp.EnableAutoDepthStencil = TRUE;
963     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
964     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
965     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with 0x%08x\n", hr);
966
967     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &surface);
968     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
969     ok(surface != NULL, "Depth stencil should not be NULL\n");
970     if (surface) IDirect3DSurface9_Release(surface);
971
972     d3dpp.EnableAutoDepthStencil = FALSE;
973     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
974     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with 0x%08x\n", hr);
975
976     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &surface);
977     ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
978     ok(surface == NULL, "Depth stencil should be NULL\n");
979
980     /* Will a sysmem or scratch survive while locked */
981     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface, NULL);
982     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
983     hr = IDirect3DSurface9_LockRect(surface, &lockrect, NULL, D3DLOCK_DISCARD);
984     ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned %08x\n", hr);
985     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
986     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
987     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
988     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
989     IDirect3DSurface9_UnlockRect(surface);
990     IDirect3DSurface9_Release(surface);
991
992     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SCRATCH, &surface, NULL);
993     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
994     hr = IDirect3DSurface9_LockRect(surface, &lockrect, NULL, D3DLOCK_DISCARD);
995     ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned %08x\n", hr);
996     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
997     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
998     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
999     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1000     IDirect3DSurface9_UnlockRect(surface);
1001     IDirect3DSurface9_Release(surface);
1002
1003     hr = IDirect3DDevice9_CreateTexture(pDevice, 16, 16, 0, 0, D3DFMT_R5G6B5, D3DPOOL_MANAGED, &texture, NULL);
1004     ok(hr == D3D_OK, "IDirect3DDevice9_CreateTexture returned %08x\n", hr);
1005     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
1006     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1007     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
1008     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1009     IDirect3DTexture9_Release(texture);
1010
1011     /* A reference held to an implicit surface causes failures as well */
1012     hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, D3DBACKBUFFER_TYPE_MONO, &surface);
1013     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer returned %08x\n", hr);
1014     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
1015     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1016     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
1017     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
1018     IDirect3DSurface9_Release(surface);
1019     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
1020     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1021     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
1022     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
1023
1024     /* Shaders are fine as well */
1025     hr = IDirect3DDevice9_CreateVertexShader(pDevice, simple_vs, &shader);
1026     ok(hr == D3D_OK, "IDirect3DDevice9_CreateVertexShader returned %08x\n", hr);
1027     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
1028     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
1029     IDirect3DVertexShader9_Release(shader);
1030
1031     /* Try setting invalid modes */
1032     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1033     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1034     d3dpp.Windowed         = FALSE;
1035     d3dpp.BackBufferWidth  = 32;
1036     d3dpp.BackBufferHeight = 32;
1037     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
1038     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset to w=32, h=32, windowed=FALSE failed with %08x\n", hr);
1039     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
1040     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
1041
1042     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1043     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1044     d3dpp.Windowed         = FALSE;
1045     d3dpp.BackBufferWidth  = 801;
1046     d3dpp.BackBufferHeight = 600;
1047     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
1048     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset to w=801, h=600, windowed=FALSE failed with %08x\n", hr);
1049     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
1050     ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
1051
1052     pDevice = NULL;
1053     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1054
1055     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1056     d3dpp.Windowed         = TRUE;
1057     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1058     d3dpp.BackBufferFormat = d3ddm.Format;
1059     d3dpp.EnableAutoDepthStencil = FALSE;
1060     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
1061
1062     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1063                     D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1064
1065     if(FAILED(hr))
1066     {
1067         skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
1068         goto cleanup;
1069     }
1070
1071     hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
1072     ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after creation returned %#x\n", hr);
1073
1074     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1075     d3dpp.Windowed         = TRUE;
1076     d3dpp.BackBufferWidth  = 400;
1077     d3dpp.BackBufferHeight = 300;
1078     d3dpp.EnableAutoDepthStencil = TRUE;
1079     d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
1080
1081     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
1082     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with 0x%08x\n", hr);
1083
1084     if (FAILED(hr)) goto cleanup;
1085
1086     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &surface);
1087     ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1088     ok(surface != NULL, "Depth stencil should not be NULL\n");
1089     if (surface) IDirect3DSurface9_Release(surface);
1090
1091 cleanup:
1092     HeapFree(GetProcessHeap(), 0, modes);
1093     if(pD3d) IDirect3D9_Release(pD3d);
1094     if(pDevice) IDirect3D9_Release(pDevice);
1095 }
1096
1097 /* Test adapter display modes */
1098 static void test_display_modes(void)
1099 {
1100     D3DDISPLAYMODE dmode;
1101     IDirect3D9 *pD3d;
1102
1103     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1104     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1105     if(!pD3d) return;
1106
1107 #define TEST_FMT(x,r) do { \
1108     HRESULT res = IDirect3D9_EnumAdapterModes(pD3d, 0, (x), 0, &dmode); \
1109     ok(res==(r), "EnumAdapterModes("#x") did not return "#r" (got %08x)!\n", res); \
1110 } while(0)
1111
1112     TEST_FMT(D3DFMT_R8G8B8, D3DERR_INVALIDCALL);
1113     TEST_FMT(D3DFMT_A8R8G8B8, D3DERR_INVALIDCALL);
1114     TEST_FMT(D3DFMT_X8B8G8R8, D3DERR_INVALIDCALL);
1115     /* D3DFMT_R5G6B5 */
1116     TEST_FMT(D3DFMT_X1R5G5B5, D3DERR_INVALIDCALL);
1117     TEST_FMT(D3DFMT_A1R5G5B5, D3DERR_INVALIDCALL);
1118     TEST_FMT(D3DFMT_A4R4G4B4, D3DERR_INVALIDCALL);
1119     TEST_FMT(D3DFMT_R3G3B2, D3DERR_INVALIDCALL);
1120     TEST_FMT(D3DFMT_A8, D3DERR_INVALIDCALL);
1121     TEST_FMT(D3DFMT_A8R3G3B2, D3DERR_INVALIDCALL);
1122     TEST_FMT(D3DFMT_X4R4G4B4, D3DERR_INVALIDCALL);
1123     TEST_FMT(D3DFMT_A2B10G10R10, D3DERR_INVALIDCALL);
1124     TEST_FMT(D3DFMT_A8B8G8R8, D3DERR_INVALIDCALL);
1125     TEST_FMT(D3DFMT_X8B8G8R8, D3DERR_INVALIDCALL);
1126     TEST_FMT(D3DFMT_G16R16, D3DERR_INVALIDCALL);
1127     TEST_FMT(D3DFMT_A2R10G10B10, D3DERR_INVALIDCALL);
1128     TEST_FMT(D3DFMT_A16B16G16R16, D3DERR_INVALIDCALL);
1129
1130     TEST_FMT(D3DFMT_A8P8, D3DERR_INVALIDCALL);
1131     TEST_FMT(D3DFMT_P8, D3DERR_INVALIDCALL);
1132
1133     TEST_FMT(D3DFMT_L8, D3DERR_INVALIDCALL);
1134     TEST_FMT(D3DFMT_A8L8, D3DERR_INVALIDCALL);
1135     TEST_FMT(D3DFMT_A4L4, D3DERR_INVALIDCALL);
1136
1137     TEST_FMT(D3DFMT_V8U8, D3DERR_INVALIDCALL);
1138     TEST_FMT(D3DFMT_L6V5U5, D3DERR_INVALIDCALL);
1139     TEST_FMT(D3DFMT_X8L8V8U8, D3DERR_INVALIDCALL);
1140     TEST_FMT(D3DFMT_Q8W8V8U8, D3DERR_INVALIDCALL);
1141     TEST_FMT(D3DFMT_V16U16, D3DERR_INVALIDCALL);
1142     TEST_FMT(D3DFMT_A2W10V10U10, D3DERR_INVALIDCALL);
1143
1144     TEST_FMT(D3DFMT_UYVY, D3DERR_INVALIDCALL);
1145     TEST_FMT(D3DFMT_YUY2, D3DERR_INVALIDCALL);
1146     TEST_FMT(D3DFMT_DXT1, D3DERR_INVALIDCALL);
1147     TEST_FMT(D3DFMT_DXT2, D3DERR_INVALIDCALL);
1148     TEST_FMT(D3DFMT_DXT3, D3DERR_INVALIDCALL);
1149     TEST_FMT(D3DFMT_DXT4, D3DERR_INVALIDCALL);
1150     TEST_FMT(D3DFMT_DXT5, D3DERR_INVALIDCALL);
1151     TEST_FMT(D3DFMT_MULTI2_ARGB8, D3DERR_INVALIDCALL);
1152     TEST_FMT(D3DFMT_G8R8_G8B8, D3DERR_INVALIDCALL);
1153     TEST_FMT(D3DFMT_R8G8_B8G8, D3DERR_INVALIDCALL);
1154
1155     TEST_FMT(D3DFMT_D16_LOCKABLE, D3DERR_INVALIDCALL);
1156     TEST_FMT(D3DFMT_D32, D3DERR_INVALIDCALL);
1157     TEST_FMT(D3DFMT_D15S1, D3DERR_INVALIDCALL);
1158     TEST_FMT(D3DFMT_D24S8, D3DERR_INVALIDCALL);
1159     TEST_FMT(D3DFMT_D24X8, D3DERR_INVALIDCALL);
1160     TEST_FMT(D3DFMT_D24X4S4, D3DERR_INVALIDCALL);
1161     TEST_FMT(D3DFMT_D16, D3DERR_INVALIDCALL);
1162     TEST_FMT(D3DFMT_L16, D3DERR_INVALIDCALL);
1163     TEST_FMT(D3DFMT_D32F_LOCKABLE, D3DERR_INVALIDCALL);
1164     TEST_FMT(D3DFMT_D24FS8, D3DERR_INVALIDCALL);
1165
1166     TEST_FMT(D3DFMT_VERTEXDATA, D3DERR_INVALIDCALL);
1167     TEST_FMT(D3DFMT_INDEX16, D3DERR_INVALIDCALL);
1168     TEST_FMT(D3DFMT_INDEX32, D3DERR_INVALIDCALL);
1169     TEST_FMT(D3DFMT_Q16W16V16U16, D3DERR_INVALIDCALL);
1170     /* Floating point formats */
1171     TEST_FMT(D3DFMT_R16F, D3DERR_INVALIDCALL);
1172     TEST_FMT(D3DFMT_G16R16F, D3DERR_INVALIDCALL);
1173     TEST_FMT(D3DFMT_A16B16G16R16F, D3DERR_INVALIDCALL);
1174
1175     /* IEEE formats */
1176     TEST_FMT(D3DFMT_R32F, D3DERR_INVALIDCALL);
1177     TEST_FMT(D3DFMT_G32R32F, D3DERR_INVALIDCALL);
1178     TEST_FMT(D3DFMT_A32B32G32R32F, D3DERR_INVALIDCALL);
1179
1180     TEST_FMT(D3DFMT_CxV8U8, D3DERR_INVALIDCALL);
1181
1182     TEST_FMT(0, D3DERR_INVALIDCALL);
1183
1184     IDirect3D9_Release(pD3d);
1185 }
1186
1187 static void test_scene(void)
1188 {
1189     HRESULT                      hr;
1190     HWND                         hwnd               = NULL;
1191     IDirect3D9                  *pD3d               = NULL;
1192     IDirect3DDevice9            *pDevice            = NULL;
1193     D3DPRESENT_PARAMETERS        d3dpp;
1194     D3DDISPLAYMODE               d3ddm;
1195     IDirect3DSurface9            *pSurface1 = NULL, *pSurface2 = NULL, *pSurface3 = NULL, *pRenderTarget = NULL;
1196     IDirect3DSurface9            *pBackBuffer = NULL, *pDepthStencil = NULL;
1197     RECT rect = {0, 0, 128, 128};
1198     D3DCAPS9                     caps;
1199
1200     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1201     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1202     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1203     ok(hwnd != NULL, "Failed to create window\n");
1204     if (!pD3d || !hwnd) goto cleanup;
1205
1206     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1207     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1208     d3dpp.Windowed         = TRUE;
1209     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1210     d3dpp.BackBufferWidth  = 800;
1211     d3dpp.BackBufferHeight = 600;
1212     d3dpp.BackBufferFormat = d3ddm.Format;
1213     d3dpp.EnableAutoDepthStencil = TRUE;
1214     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1215
1216     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1217                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1218     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1219     if(!pDevice)
1220     {
1221         skip("Failed to create a d3d device\n");
1222         goto cleanup;
1223     }
1224
1225     /* Get the caps, they will be needed to tell if an operation is supposed to be valid */
1226     memset(&caps, 0, sizeof(caps));
1227     hr = IDirect3DDevice9_GetDeviceCaps(pDevice, &caps);
1228     ok(hr == D3D_OK, "IDirect3DDevice9_GetCaps failed with %08x\n", hr);
1229     if(FAILED(hr)) goto cleanup;
1230
1231     /* Test an EndScene without beginscene. Should return an error */
1232     hr = IDirect3DDevice9_EndScene(pDevice);
1233     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %08x\n", hr);
1234
1235     /* Test a normal BeginScene / EndScene pair, this should work */
1236     hr = IDirect3DDevice9_BeginScene(pDevice);
1237     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1238     if(SUCCEEDED(hr))
1239     {
1240         hr = IDirect3DDevice9_EndScene(pDevice);
1241         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1242     }
1243
1244     /* Test another EndScene without having begun a new scene. Should return an error */
1245     hr = IDirect3DDevice9_EndScene(pDevice);
1246     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %08x\n", hr);
1247
1248     /* Two nested BeginScene and EndScene calls */
1249     hr = IDirect3DDevice9_BeginScene(pDevice);
1250     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1251     hr = IDirect3DDevice9_BeginScene(pDevice);
1252     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_BeginScene returned %08x\n", hr);
1253     hr = IDirect3DDevice9_EndScene(pDevice);
1254     ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1255     hr = IDirect3DDevice9_EndScene(pDevice);
1256     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %08x\n", hr);
1257
1258     /* Create some surfaces to test stretchrect between the scenes */
1259     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface1, NULL);
1260     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %08x\n", hr);
1261     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface2, NULL);
1262     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %08x\n", hr);
1263     hr = IDirect3DDevice9_CreateDepthStencilSurface(pDevice, 800, 600, D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, FALSE, &pSurface3, NULL);
1264     ok(hr == D3D_OK, "IDirect3DDevice9_CreateDepthStencilSurface failed with %08x\n", hr);
1265     hr = IDirect3DDevice9_CreateRenderTarget(pDevice, 128, 128, d3ddm.Format, D3DMULTISAMPLE_NONE, 0, FALSE, &pRenderTarget, NULL);
1266     ok(hr == D3D_OK, "IDirect3DDevice9_CreateRenderTarget failed with %08x\n", hr);
1267
1268     hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
1269     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer failed with %08x\n", hr);
1270     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1271     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer failed with %08x\n", hr);
1272
1273     /* First make sure a simple StretchRect call works */
1274     if(pSurface1 && pSurface2) {
1275         hr = IDirect3DDevice9_StretchRect(pDevice, pSurface1, NULL, pSurface2, NULL, 0);
1276         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1277     }
1278     if(pBackBuffer && pRenderTarget) {
1279         hr = IDirect3DDevice9_StretchRect(pDevice, pBackBuffer, &rect, pRenderTarget, NULL, 0);
1280         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1281     }
1282     if(pDepthStencil && pSurface3) {
1283         HRESULT expected;
1284         if(0) /* Disabled for now because it crashes in wine */ {
1285             expected = caps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES ? D3D_OK : D3DERR_INVALIDCALL;
1286             hr = IDirect3DDevice9_StretchRect(pDevice, pDepthStencil, NULL, pSurface3, NULL, 0);
1287             ok( hr == expected, "IDirect3DDevice9_StretchRect returned %08x, expected %08x\n", hr, expected);
1288         }
1289     }
1290
1291     /* Now try it in a BeginScene - EndScene pair. Seems to be allowed in a beginScene - Endscene pair
1292      * width normal surfaces, render targets and depth stencil surfaces.
1293      */
1294     hr = IDirect3DDevice9_BeginScene(pDevice);
1295     ok( hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1296
1297     if(pSurface1 && pSurface2)
1298     {
1299         hr = IDirect3DDevice9_StretchRect(pDevice, pSurface1, NULL, pSurface2, NULL, 0);
1300         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1301     }
1302     if(pBackBuffer && pRenderTarget)
1303     {
1304         hr = IDirect3DDevice9_StretchRect(pDevice, pBackBuffer, &rect, pRenderTarget, NULL, 0);
1305         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %08x\n", hr);
1306     }
1307     if(pDepthStencil && pSurface3)
1308     {
1309         /* This is supposed to fail inside a BeginScene - EndScene pair. */
1310         hr = IDirect3DDevice9_StretchRect(pDevice, pDepthStencil, NULL, pSurface3, NULL, 0);
1311         ok( hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_StretchRect returned %08x, expected D3DERR_INVALIDCALL\n", hr);
1312     }
1313
1314     hr = IDirect3DDevice9_EndScene(pDevice);
1315     ok( hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1316
1317     /* Does a SetRenderTarget influence BeginScene / EndScene ?
1318      * Set a new render target, then see if it started a new scene. Flip the rt back and see if that maybe
1319      * ended the scene. Expected result is that the scene is not affected by SetRenderTarget
1320      */
1321     hr = IDirect3DDevice9_SetRenderTarget(pDevice, 0, pRenderTarget);
1322     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget failed with %08x\n", hr);
1323     hr = IDirect3DDevice9_BeginScene(pDevice);
1324     ok( hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %08x\n", hr);
1325     hr = IDirect3DDevice9_SetRenderTarget(pDevice, 0, pBackBuffer);
1326     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget failed with %08x\n", hr);
1327     hr = IDirect3DDevice9_EndScene(pDevice);
1328     ok( hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %08x\n", hr);
1329
1330 cleanup:
1331     if(pRenderTarget) IDirect3DSurface9_Release(pRenderTarget);
1332     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1333     if(pBackBuffer) IDirect3DSurface9_Release(pBackBuffer);
1334     if(pSurface1) IDirect3DSurface9_Release(pSurface1);
1335     if(pSurface2) IDirect3DSurface9_Release(pSurface2);
1336     if(pSurface3) IDirect3DSurface9_Release(pSurface3);
1337     if(pD3d) IDirect3D9_Release(pD3d);
1338     if(pDevice) IDirect3D9_Release(pDevice);
1339     if(hwnd) DestroyWindow(hwnd);
1340 }
1341
1342 static void test_limits(void)
1343 {
1344     HRESULT                      hr;
1345     HWND                         hwnd               = NULL;
1346     IDirect3D9                  *pD3d               = NULL;
1347     IDirect3DDevice9            *pDevice            = NULL;
1348     D3DPRESENT_PARAMETERS        d3dpp;
1349     D3DDISPLAYMODE               d3ddm;
1350     IDirect3DTexture9           *pTexture           = NULL;
1351     int i;
1352
1353     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1354     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1355     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1356     ok(hwnd != NULL, "Failed to create window\n");
1357     if (!pD3d || !hwnd) goto cleanup;
1358
1359     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1360     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1361     d3dpp.Windowed         = TRUE;
1362     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1363     d3dpp.BackBufferWidth  = 800;
1364     d3dpp.BackBufferHeight = 600;
1365     d3dpp.BackBufferFormat = d3ddm.Format;
1366     d3dpp.EnableAutoDepthStencil = TRUE;
1367     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1368
1369     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1370                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1371     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1372     if(!pDevice)
1373     {
1374         skip("Failed to create a d3d device\n");
1375         goto cleanup;
1376     }
1377
1378     hr = IDirect3DDevice9_CreateTexture(pDevice, 16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture, NULL);
1379     ok(hr == D3D_OK, "IDirect3DDevice9_CreateTexture failed with %08x\n", hr);
1380     if(!pTexture) goto cleanup;
1381
1382     /* There are 16 pixel samplers. We should be able to access all of them */
1383     for(i = 0; i < 16; i++) {
1384         hr = IDirect3DDevice9_SetTexture(pDevice, i, (IDirect3DBaseTexture9 *) pTexture);
1385         ok(hr == D3D_OK, "IDirect3DDevice9_SetTexture for sampler %d failed with %08x\n", i, hr);
1386         hr = IDirect3DDevice9_SetTexture(pDevice, i, NULL);
1387         ok(hr == D3D_OK, "IDirect3DDevice9_SetTexture for sampler %d failed with %08x\n", i, hr);
1388         hr = IDirect3DDevice9_SetSamplerState(pDevice, i, D3DSAMP_SRGBTEXTURE, TRUE);
1389         ok(hr == D3D_OK, "IDirect3DDevice9_SetSamplerState for sampler %d failed with %08x\n", i, hr);
1390     }
1391
1392     /* Now test all 8 textures stage states */
1393     for(i = 0; i < 8; i++) {
1394         hr = IDirect3DDevice9_SetTextureStageState(pDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
1395         ok(hr == D3D_OK, "IDirect3DDevice9_SetTextureStageState for texture %d failed with %08x\n", i, hr);
1396     }
1397
1398     /* Investigations show that accessing higher samplers / textures stage states does not return an error either. Writing
1399      * to too high samplers(approximately sampler 40) causes memory corruption in windows, so there is no bounds checking
1400      * but how do I test that?
1401      */
1402 cleanup:
1403     if(pTexture) IDirect3DTexture9_Release(pTexture);
1404     if(pD3d) IDirect3D9_Release(pD3d);
1405     if(pDevice) IDirect3D9_Release(pDevice);
1406     if(hwnd) DestroyWindow(hwnd);
1407 }
1408
1409 static void test_depthstenciltest(void)
1410 {
1411     HRESULT                      hr;
1412     HWND                         hwnd               = NULL;
1413     IDirect3D9                  *pD3d               = NULL;
1414     IDirect3DDevice9            *pDevice            = NULL;
1415     D3DPRESENT_PARAMETERS        d3dpp;
1416     D3DDISPLAYMODE               d3ddm;
1417     IDirect3DSurface9           *pDepthStencil           = NULL;
1418     IDirect3DSurface9           *pDepthStencil2          = NULL;
1419     D3DZBUFFERTYPE               state;
1420
1421     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1422     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1423     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1424     ok(hwnd != NULL, "Failed to create window\n");
1425     if (!pD3d || !hwnd) goto cleanup;
1426
1427     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1428     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1429     d3dpp.Windowed         = TRUE;
1430     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1431     d3dpp.BackBufferWidth  = 800;
1432     d3dpp.BackBufferHeight = 600;
1433     d3dpp.BackBufferFormat = d3ddm.Format;
1434     d3dpp.EnableAutoDepthStencil = TRUE;
1435     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1436
1437     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1438                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1439     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1440     if(!pDevice)
1441     {
1442         skip("Failed to create a d3d device\n");
1443         goto cleanup;
1444     }
1445
1446     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1447     ok(hr == D3D_OK && pDepthStencil != NULL, "IDirect3DDevice9_GetDepthStencilSurface failed with %08x\n", hr);
1448
1449     /* Try to clear */
1450     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1451     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %08x\n", hr);
1452
1453     hr = IDirect3DDevice9_SetDepthStencilSurface(pDevice, NULL);
1454     ok(hr == D3D_OK, "IDirect3DDevice9_SetDepthStencilSurface failed with %08x\n", hr);
1455
1456     /* Check if the set buffer is returned on a get. WineD3D had a bug with that once, prevent it from coming back */
1457     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil2);
1458     ok(hr == D3DERR_NOTFOUND && pDepthStencil2 == NULL, "IDirect3DDevice9_GetDepthStencilSurface failed with %08x\n", hr);
1459     if(pDepthStencil2) IDirect3DSurface9_Release(pDepthStencil2);
1460
1461     /* This left the render states untouched! */
1462     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1463     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1464     ok(state == D3DZB_TRUE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1465     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZWRITEENABLE, &state);
1466     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1467     ok(state == TRUE, "D3DRS_ZWRITEENABLE is %s\n", state ? "TRUE" : "FALSE");
1468     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_STENCILENABLE, &state);
1469     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1470     ok(state == FALSE, "D3DRS_STENCILENABLE is %s\n", state ? "TRUE" : "FALSE");
1471     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_STENCILWRITEMASK, &state);
1472     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1473     ok(state == 0xffffffff, "D3DRS_STENCILWRITEMASK is 0x%08x\n", state);
1474
1475     /* This is supposed to fail now */
1476     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1477     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Clear failed with %08x\n", hr);
1478
1479     hr = IDirect3DDevice9_SetRenderState(pDevice, D3DRS_ZENABLE, D3DZB_FALSE);
1480     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState failed with %08x\n", hr);
1481
1482     hr = IDirect3DDevice9_SetDepthStencilSurface(pDevice, pDepthStencil);
1483     ok(hr == D3D_OK, "IDirect3DDevice9_SetDepthStencilSurface failed with %08x\n", hr);
1484
1485     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1486     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1487     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1488
1489     /* Now it works again */
1490     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1491     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %08x\n", hr);
1492
1493     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1494     if(pDevice) IDirect3D9_Release(pDevice);
1495
1496     /* Now see if autodepthstencil disable is honored. First, without a format set */
1497     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1498     d3dpp.Windowed         = TRUE;
1499     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1500     d3dpp.BackBufferWidth  = 800;
1501     d3dpp.BackBufferHeight = 600;
1502     d3dpp.BackBufferFormat = d3ddm.Format;
1503     d3dpp.EnableAutoDepthStencil = FALSE;
1504     d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
1505
1506     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1507                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1508     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1509     if(!pDevice)
1510     {
1511         skip("Failed to create a d3d device\n");
1512         goto cleanup;
1513     }
1514
1515     pDepthStencil = NULL;
1516     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1517     ok(hr == D3DERR_NOTFOUND && pDepthStencil == NULL, "IDirect3DDevice9_GetDepthStencilSurface returned %08x, surface = %p\n", hr, pDepthStencil);
1518     if(pDepthStencil) {
1519         IDirect3DSurface9_Release(pDepthStencil);
1520         pDepthStencil = NULL;
1521     }
1522
1523     /* Check the depth test state */
1524     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1525     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1526     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1527
1528     if(pDevice) IDirect3D9_Release(pDevice);
1529
1530     /* Next, try EnableAutoDepthStencil FALSE with a depth stencil format set */
1531     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1532     d3dpp.Windowed         = TRUE;
1533     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1534     d3dpp.BackBufferWidth  = 800;
1535     d3dpp.BackBufferHeight = 600;
1536     d3dpp.BackBufferFormat = d3ddm.Format;
1537     d3dpp.EnableAutoDepthStencil = FALSE;
1538     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1539
1540     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1541                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1542     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1543     if(!pDevice)
1544     {
1545         skip("Failed to create a d3d device\n");
1546         goto cleanup;
1547     }
1548
1549     pDepthStencil = NULL;
1550     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1551     ok(hr == D3DERR_NOTFOUND && pDepthStencil == NULL, "IDirect3DDevice9_GetDepthStencilSurface returned %08x, surface = %p\n", hr, pDepthStencil);
1552     if(pDepthStencil) {
1553         IDirect3DSurface9_Release(pDepthStencil);
1554         pDepthStencil = NULL;
1555     }
1556
1557     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1558     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %08x\n", hr);
1559     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1560
1561 cleanup:
1562     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1563     if(pD3d) IDirect3D9_Release(pD3d);
1564     if(pDevice) IDirect3D9_Release(pDevice);
1565     if(hwnd) DestroyWindow(hwnd);
1566 }
1567
1568 /* Test what happens when IDirect3DDevice9_DrawIndexedPrimitive is called without a valid index buffer set. */
1569 static void test_draw_indexed(void)
1570 {
1571     static const struct {
1572         float position[3];
1573         DWORD color;
1574     } quad[] = {
1575         {{-1.0f, -1.0f, 0.0f}, 0xffff0000},
1576         {{-1.0f,  1.0f, 0.0f}, 0xffff0000},
1577         {{ 1.0f,  1.0f, 0.0f}, 0xffff0000},
1578         {{ 1.0f, -1.0f, 0.0f}, 0xffff0000},
1579     };
1580     WORD indices[] = {0, 1, 2, 3, 0, 2};
1581
1582     static const D3DVERTEXELEMENT9 decl_elements[] = {
1583         {0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1584         {0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,    D3DDECLUSAGE_COLOR, 0},
1585         D3DDECL_END()
1586     };
1587
1588     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
1589     IDirect3DVertexBuffer9 *vertex_buffer = NULL;
1590     IDirect3DIndexBuffer9 *index_buffer = NULL;
1591     D3DPRESENT_PARAMETERS present_parameters;
1592     IDirect3DDevice9 *device = NULL;
1593     IDirect3D9 *d3d9;
1594     HRESULT hr;
1595     HWND hwnd;
1596     void *ptr;
1597
1598     hwnd = CreateWindow("static", "d3d9_test",
1599             0, 0, 0, 10, 10, 0, 0, 0, 0);
1600     if (!hwnd)
1601     {
1602         skip("Failed to create window\n");
1603         return;
1604     }
1605
1606     d3d9 = pDirect3DCreate9(D3D_SDK_VERSION);
1607     if (!d3d9)
1608     {
1609         skip("Failed to create IDirect3D9 object\n");
1610         goto cleanup;
1611     }
1612
1613     ZeroMemory(&present_parameters, sizeof(present_parameters));
1614     present_parameters.Windowed = TRUE;
1615     present_parameters.hDeviceWindow = hwnd;
1616     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1617
1618     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
1619             NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device);
1620     if (FAILED(hr) || !device)
1621     {
1622         skip("Failed to create device\n");
1623         goto cleanup;
1624     }
1625
1626     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
1627     ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (0x%08x)\n", hr);
1628     hr = IDirect3DDevice9_SetVertexDeclaration(device, NULL);
1629     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
1630
1631     hr = IDirect3DDevice9_CreateVertexBuffer(device, sizeof(quad), 0, 0, D3DPOOL_DEFAULT, &vertex_buffer, NULL);
1632     ok(SUCCEEDED(hr), "CreateVertexBuffer failed (0x%08x)\n", hr);
1633     hr = IDirect3DVertexBuffer9_Lock(vertex_buffer, 0, 0, &ptr, D3DLOCK_DISCARD);
1634     ok(SUCCEEDED(hr), "Lock failed (0x%08x)\n", hr);
1635     memcpy(ptr, quad, sizeof(quad));
1636     hr = IDirect3DVertexBuffer9_Unlock(vertex_buffer);
1637     ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1638     hr = IDirect3DDevice9_SetStreamSource(device, 0, vertex_buffer, 0, sizeof(*quad));
1639     ok(SUCCEEDED(hr), "SetStreamSource failed (0x%08x)\n", hr);
1640
1641     hr = IDirect3DDevice9_CreateIndexBuffer(device, sizeof(indices), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &index_buffer, NULL);
1642     ok(SUCCEEDED(hr), "CreateIndexBuffer failed (0x%08x)\n", hr);
1643     hr = IDirect3DIndexBuffer9_Lock(index_buffer, 0, 0, &ptr, D3DLOCK_DISCARD);
1644     ok(SUCCEEDED(hr), "Lock failed (0x%08x)\n", hr);
1645     memcpy(ptr, indices, sizeof(indices));
1646     hr = IDirect3DIndexBuffer9_Unlock(index_buffer);
1647     ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1648     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1649     ok(SUCCEEDED(hr), "SetRenderState D3DRS_LIGHTING failed (0x%08x)\n", hr);
1650     hr = IDirect3DDevice9_BeginScene(device);
1651     ok(SUCCEEDED(hr), "BeginScene failed (0x%08x)\n", hr);
1652
1653     /* NULL index buffer. Should fail */
1654     hr = IDirect3DDevice9_SetIndices(device, NULL);
1655     ok(SUCCEEDED(hr), "SetIndices failed (0x%08x)\n", hr);
1656     hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1657             4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1658     ok(hr == D3DERR_INVALIDCALL, "DrawIndexedPrimitive returned 0x%08x, expected D3DERR_INVALIDCALL (0x%08x)\n",
1659             hr, D3DERR_INVALIDCALL);
1660
1661     /* Valid index buffer, NULL vertex declaration. Should fail */
1662     hr = IDirect3DDevice9_SetIndices(device, index_buffer);
1663     ok(SUCCEEDED(hr), "SetIndices failed (0x%08x)\n", hr);
1664     hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1665             4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1666     ok(hr == D3DERR_INVALIDCALL, "DrawIndexedPrimitive returned 0x%08x, expected D3DERR_INVALIDCALL (0x%08x)\n",
1667             hr, D3DERR_INVALIDCALL);
1668
1669     /* Valid index buffer and vertex declaration. Should succeed */
1670     hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
1671     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
1672     hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1673             4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1674     ok(SUCCEEDED(hr), "DrawIndexedPrimitive failed (0x%08x)\n", hr);
1675
1676     hr = IDirect3DDevice9_EndScene(device);
1677     ok(SUCCEEDED(hr), "EndScene failed (0x%08x)\n", hr);
1678
1679     hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
1680     ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1681
1682     IDirect3DVertexBuffer9_Release(vertex_buffer);
1683     IDirect3DIndexBuffer9_Release(index_buffer);
1684     IDirect3DVertexDeclaration9_Release(vertex_declaration);
1685
1686 cleanup:
1687     if (d3d9) IDirect3D9_Release(d3d9);
1688     if (device) IDirect3DDevice9_Release(device);
1689     if (hwnd) DestroyWindow(hwnd);
1690 }
1691
1692 static void test_null_stream(void)
1693 {
1694     IDirect3DVertexBuffer9 *buffer = NULL;
1695     D3DPRESENT_PARAMETERS present_parameters;
1696     IDirect3DDevice9 *device = NULL;
1697     IDirect3D9 *d3d9;
1698     HWND hwnd;
1699     HRESULT hr;
1700     IDirect3DVertexShader9 *shader = NULL;
1701     IDirect3DVertexDeclaration9 *decl = NULL;
1702     DWORD shader_code[] = {
1703         0xfffe0101,                             /* vs_1_1           */
1704         0x0000001f, 0x80000000, 0x900f0000,     /* dcl_position v0  */
1705         0x00000001, 0xc00f0000, 0x90e40000,     /* mov oPos, v0     */
1706         0x0000ffff                              /* end              */
1707     };
1708     static const D3DVERTEXELEMENT9 decl_elements[] = {
1709         {0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1710         {1, 0,  D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,    D3DDECLUSAGE_COLOR, 0},
1711         D3DDECL_END()
1712     };
1713
1714     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1715     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1716     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1717     ok(hwnd != NULL, "Failed to create window\n");
1718     if (!d3d9 || !hwnd) goto cleanup;
1719
1720     ZeroMemory(&present_parameters, sizeof(present_parameters));
1721     present_parameters.Windowed = TRUE;
1722     present_parameters.hDeviceWindow = hwnd;
1723     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1724
1725     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1726                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device );
1727     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1728     if(!device)
1729     {
1730         skip("Failed to create a d3d device\n");
1731         goto cleanup;
1732     }
1733
1734     hr = IDirect3DDevice9_CreateVertexShader(device, shader_code, &shader);
1735     if(FAILED(hr)) {
1736         skip("No vertex shader support\n");
1737         goto cleanup;
1738     }
1739     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &decl);
1740     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexDeclaration failed (0x%08x)\n", hr);
1741     if (FAILED(hr)) {
1742         skip("Vertex declaration handling not possible.\n");
1743         goto cleanup;
1744     }
1745     hr = IDirect3DDevice9_CreateVertexBuffer(device, 12 * sizeof(float), 0, 0, D3DPOOL_MANAGED, &buffer, NULL);
1746     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexBuffer failed (0x%08x)\n", hr);
1747     if (FAILED(hr)) {
1748         skip("Vertex buffer handling not possible.\n");
1749         goto cleanup;
1750     }
1751
1752     hr = IDirect3DDevice9_SetStreamSource(device, 0, buffer, 0, sizeof(float) * 3);
1753     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetStreamSource failed (0x%08x)\n", hr);
1754     hr = IDirect3DDevice9_SetStreamSource(device, 1, NULL, 0, 0);
1755     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetStreamSource failed (0x%08x)\n", hr);
1756     hr = IDirect3DDevice9_SetVertexShader(device, shader);
1757     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetVertexShader failed (0x%08x)\n", hr);
1758     hr = IDirect3DDevice9_SetVertexDeclaration(device, decl);
1759     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetVertexDeclaration failed (0x%08x)\n", hr);
1760
1761     hr = IDirect3DDevice9_BeginScene(device);
1762     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed (0x%08x)\n", hr);
1763     if(SUCCEEDED(hr)) {
1764         hr = IDirect3DDevice9_DrawPrimitive(device, D3DPT_POINTLIST, 0, 1);
1765         ok(SUCCEEDED(hr), "IDirect3DDevice9_DrawPrimitive failed (0x%08x)\n", hr);
1766
1767         hr = IDirect3DDevice9_EndScene(device);
1768         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed (0x%08x)\n", hr);
1769     }
1770
1771     IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
1772     IDirect3DDevice9_SetVertexShader(device, NULL);
1773     IDirect3DDevice9_SetVertexDeclaration(device, NULL);
1774
1775 cleanup:
1776     if(decl) IDirect3DVertexDeclaration9_Release(decl);
1777     if(shader) IDirect3DVertexShader9_Release(shader);
1778     if(device) IDirect3DDevice9_Release(device);
1779     if(d3d9) IDirect3D9_Release(d3d9);
1780 }
1781
1782 static inline const char *debug_d3dpool(D3DPOOL pool) {
1783     switch(pool) {
1784         case D3DPOOL_DEFAULT: return "D3DPOOL_DEFAULT";
1785         case D3DPOOL_SYSTEMMEM: return "D3DPOOL_SYSTEMMEM";
1786         case D3DPOOL_SCRATCH: return "D3DPOOL_SCRATCH";
1787         case D3DPOOL_MANAGED: return "D3DPOOL_MANAGED";
1788         default:
1789         return "unknown pool";
1790     }
1791 }
1792
1793 static void test_vertex_buffer_alignment(void)
1794 {
1795     IDirect3DVertexBuffer9 *buffer = NULL;
1796     D3DPRESENT_PARAMETERS present_parameters;
1797     IDirect3DDevice9 *device = NULL;
1798     IDirect3D9 *d3d9;
1799     HWND hwnd;
1800     HRESULT hr;
1801     D3DPOOL pools[] = {D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEM, D3DPOOL_SCRATCH, D3DPOOL_MANAGED};
1802     DWORD sizes[] = {1, 4, 16, 17, 32, 33, 64, 65, 1024, 1025, 1048576, 1048577};
1803     unsigned int i, j;
1804     void *data;
1805
1806     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1807     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1808     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1809     ok(hwnd != NULL, "Failed to create window\n");
1810     if (!d3d9 || !hwnd) goto cleanup;
1811
1812     ZeroMemory(&present_parameters, sizeof(present_parameters));
1813     present_parameters.Windowed = TRUE;
1814     present_parameters.hDeviceWindow = hwnd;
1815     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1816
1817     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1818                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device );
1819     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1820     if(!device)
1821     {
1822         skip("Failed to create a d3d device\n");
1823         goto cleanup;
1824     }
1825
1826     for(i = 0; i < (sizeof(sizes) / sizeof(sizes[0])); i++) {
1827         for(j = 0; j < (sizeof(pools) / sizeof(pools[0])); j++) {
1828             hr = IDirect3DDevice9_CreateVertexBuffer(device, sizes[i], 0, 0, pools[j], &buffer, NULL);
1829             if(pools[j] == D3DPOOL_SCRATCH) {
1830                 ok(hr == D3DERR_INVALIDCALL, "Creating a D3DPOOL_SCRATCH buffer returned (0x%08x)\n", hr);
1831             } else {
1832                 ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexBuffer failed (0x%08x). Pool = %s, size %d\n", hr,
1833                    debug_d3dpool(pools[j]), sizes[i]);
1834             }
1835             if(FAILED(hr)) continue;
1836
1837             hr = IDirect3DVertexBuffer9_Lock(buffer, 0, 0, &data, 0);
1838             ok(SUCCEEDED(hr), "IDirect3DVertexBuffer9_Lock failed (0x%08x)\n", hr);
1839             ok(((DWORD_PTR) data & 31) == 0, "Vertex buffer start address is not 32 byte aligned(size: %d, pool: %s, data: %p)\n",
1840                sizes[i], debug_d3dpool(pools[j]), data);
1841             hr = IDirect3DVertexBuffer9_Unlock(buffer);
1842             ok(SUCCEEDED(hr), "IDirect3DVertexBuffer9_Unlock failed (0x%08x)\n", hr);
1843
1844             if(buffer) IDirect3DVertexBuffer9_Release(buffer);
1845         }
1846     }
1847
1848     cleanup:
1849     if(d3d9) IDirect3D9_Release(d3d9);
1850 }
1851
1852 static void test_lights(void)
1853 {
1854     D3DPRESENT_PARAMETERS present_parameters;
1855     IDirect3DDevice9 *device = NULL;
1856     IDirect3D9 *d3d9;
1857     HWND hwnd;
1858     HRESULT hr;
1859     unsigned int i;
1860     BOOL enabled;
1861     D3DCAPS9 caps;
1862
1863     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1864     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1865     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1866     ok(hwnd != NULL, "Failed to create window\n");
1867     if (!d3d9 || !hwnd) goto cleanup;
1868
1869     ZeroMemory(&present_parameters, sizeof(present_parameters));
1870     present_parameters.Windowed = TRUE;
1871     present_parameters.hDeviceWindow = hwnd;
1872     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1873
1874     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1875                                   D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &present_parameters, &device );
1876     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1877        "IDirect3D9_CreateDevice failed with %08x\n", hr);
1878     if(!device)
1879     {
1880         skip("Failed to create a d3d device\n");
1881         goto cleanup;
1882     }
1883
1884     memset(&caps, 0, sizeof(caps));
1885     hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1886     ok(hr == D3D_OK, "IDirect3DDevice9_GetDeviceCaps failed with %08x\n", hr);
1887
1888     for(i = 1; i <= caps.MaxActiveLights; i++) {
1889         hr = IDirect3DDevice9_LightEnable(device, i, TRUE);
1890         ok(hr == D3D_OK, "Enabling light %u failed with %08x\n", i, hr);
1891         hr = IDirect3DDevice9_GetLightEnable(device, i, &enabled);
1892         ok(hr == D3D_OK, "GetLightEnable on light %u failed with %08x\n", i, hr);
1893         ok(enabled, "Light %d is %s\n", i, enabled ? "enabled" : "disabled");
1894     }
1895
1896     /* TODO: Test the rendering results in this situation */
1897     hr = IDirect3DDevice9_LightEnable(device, i + 1, TRUE);
1898     ok(hr == D3D_OK, "Enabling one light more than supported returned %08x\n", hr);
1899     hr = IDirect3DDevice9_GetLightEnable(device, i + 1, &enabled);
1900     ok(hr == D3D_OK, "GetLightEnable on light %u failed with %08x\n", i + 1, hr);
1901     ok(enabled, "Light %d is %s\n", i + 1, enabled ? "enabled" : "disabled");
1902     hr = IDirect3DDevice9_LightEnable(device, i + 1, FALSE);
1903     ok(hr == D3D_OK, "Disabling the additional returned %08x\n", hr);
1904
1905     for(i = 1; i <= caps.MaxActiveLights; i++) {
1906         hr = IDirect3DDevice9_LightEnable(device, i, FALSE);
1907         ok(hr == D3D_OK, "Disabling light %u failed with %08x\n", i, hr);
1908     }
1909
1910     cleanup:
1911     if(device) IDirect3DDevice9_Release(device);
1912     if(d3d9) IDirect3D9_Release(d3d9);
1913 }
1914
1915 static void test_set_stream_source(void)
1916 {
1917     D3DPRESENT_PARAMETERS present_parameters;
1918     IDirect3DDevice9 *device = NULL;
1919     IDirect3D9 *d3d9;
1920     HWND hwnd;
1921     HRESULT hr;
1922     IDirect3DVertexBuffer9 *pVertexBuffer = NULL;
1923
1924     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1925     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1926     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1927     ok(hwnd != NULL, "Failed to create window\n");
1928     if (!d3d9 || !hwnd) goto cleanup;
1929
1930     ZeroMemory(&present_parameters, sizeof(present_parameters));
1931     present_parameters.Windowed = TRUE;
1932     present_parameters.hDeviceWindow = hwnd;
1933     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1934
1935     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1936                                   D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
1937     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1938        "IDirect3D9_CreateDevice failed with %08x\n", hr);
1939     if(!device)
1940     {
1941         hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd,
1942                                       D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
1943         ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %08x\n", hr);
1944         if(!device)
1945         {
1946             skip("Failed to create a d3d device\n");
1947             goto cleanup;
1948         }
1949     }
1950
1951     hr = IDirect3DDevice9_CreateVertexBuffer( device, 512, 0, 0, D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
1952     ok(hr == D3D_OK, "Failed to create a vertex buffer, hr = %08x\n", hr);
1953     if (SUCCEEDED(hr)) {
1954         /* Some cards(Geforce 7400 at least) accept non-aligned offsets, others(radeon 9000 verified) reject it,
1955          * so accept both results. Wine currently rejects this to be able to optimize the vbo conversion, but writes
1956          * a WARN
1957          */
1958         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 0, 32);
1959         ok(hr == D3D_OK, "Failed to set the stream source, offset 0, hr = %08x\n", hr);
1960         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 1, 32);
1961         ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 1, hr = %08x\n", hr);
1962         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 2, 32);
1963         ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 2, hr = %08x\n", hr);
1964         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 3, 32);
1965         ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 3, hr = %08x\n", hr);
1966         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 4, 32);
1967         ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %08x\n", hr);
1968     }
1969     /* Try to set the NULL buffer with an offset and stride 0 */
1970     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
1971     ok(hr == D3D_OK, "Failed to set the stream source, offset 0, hr = %08x\n", hr);
1972     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 1, 0);
1973     ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 1, hr = %08x\n", hr);
1974     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 2, 0);
1975     ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 2, hr = %08x\n", hr);
1976     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 3, 0);
1977     ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 3, hr = %08x\n", hr);
1978     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 4, 0);
1979     ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %08x\n", hr);
1980
1981     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
1982     ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %08x\n", hr);
1983
1984     if(pVertexBuffer) IDirect3DDevice9_Release(pVertexBuffer);
1985 cleanup:
1986     if(device) IDirect3DDevice9_Release(device);
1987     if(d3d9) IDirect3D9_Release(d3d9);
1988 }
1989
1990 struct formats {
1991     D3DFORMAT DisplayFormat;
1992     D3DFORMAT BackBufferFormat;
1993     BOOL shouldPass;
1994 };
1995
1996 struct formats r5g6b5_format_list[] =
1997 {
1998     { D3DFMT_R5G6B5, D3DFMT_R5G6B5, TRUE },
1999     { D3DFMT_R5G6B5, D3DFMT_X1R5G5B5, FALSE },
2000     { D3DFMT_R5G6B5, D3DFMT_A1R5G5B5, FALSE },
2001     { D3DFMT_R5G6B5, D3DFMT_X8R8G8B8, FALSE },
2002     { D3DFMT_R5G6B5, D3DFMT_A8R8G8B8, FALSE },
2003     { 0, 0, 0}
2004 };
2005
2006 struct formats x1r5g5b5_format_list[] =
2007 {
2008     { D3DFMT_X1R5G5B5, D3DFMT_R5G6B5, FALSE },
2009     { D3DFMT_X1R5G5B5, D3DFMT_X1R5G5B5, TRUE },
2010     { D3DFMT_X1R5G5B5, D3DFMT_A1R5G5B5, TRUE },
2011     { D3DFMT_X1R5G5B5, D3DFMT_X8R8G8B8, FALSE },
2012     { D3DFMT_X1R5G5B5, D3DFMT_A8R8G8B8, FALSE },
2013
2014     /* A1R5G5B5 should not be usable as a display format, it is backbuffer-only */
2015     { D3DFMT_A1R5G5B5, D3DFMT_R5G6B5, FALSE },
2016     { D3DFMT_A1R5G5B5, D3DFMT_X1R5G5B5, FALSE },
2017     { D3DFMT_A1R5G5B5, D3DFMT_A1R5G5B5, FALSE },
2018     { D3DFMT_A1R5G5B5, D3DFMT_X8R8G8B8, FALSE },
2019     { D3DFMT_A1R5G5B5, D3DFMT_A8R8G8B8, FALSE },
2020     { 0, 0, 0}
2021 };
2022
2023 struct formats x8r8g8b8_format_list[] =
2024 {
2025     { D3DFMT_X8R8G8B8, D3DFMT_R5G6B5, FALSE },
2026     { D3DFMT_X8R8G8B8, D3DFMT_X1R5G5B5, FALSE },
2027     { D3DFMT_X8R8G8B8, D3DFMT_A1R5G5B5, FALSE },
2028     { D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, TRUE },
2029     { D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, TRUE },
2030
2031     /* A1R8G8B8 should not be usable as a display format, it is backbuffer-only */
2032     { D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, FALSE },
2033     { D3DFMT_A8R8G8B8, D3DFMT_X1R5G5B5, FALSE },
2034     { D3DFMT_A8R8G8B8, D3DFMT_A1R5G5B5, FALSE },
2035     { D3DFMT_A8R8G8B8, D3DFMT_X8R8G8B8, FALSE },
2036     { D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8, FALSE },
2037     { 0, 0, 0}
2038 };
2039
2040 static void test_display_formats(void)
2041 {
2042     /* Direct3D9 offers 4 display formats R5G6B5, X1R5G5B5, X8R8G8B8 and A2R10G10B10.
2043      * Next to these there are 6 different backbuffer formats. Only a fixed number of
2044      * combinations are possible in FULLSCREEN mode. In windowed mode more combinations are
2045      * allowed due to depth conversion and this is likely driver dependent.
2046      * This test checks which combinations are possible in fullscreen mode and this should not be driver dependent.
2047      * TODO: handle A2R10G10B10 but what hardware supports it? Parhelia? It is very rare. */
2048
2049     UINT Adapter = D3DADAPTER_DEFAULT;
2050     D3DDEVTYPE DeviceType = D3DDEVTYPE_HAL;
2051     int i, nmodes;
2052     HRESULT hr;
2053
2054     IDirect3D9 *d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
2055     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
2056     if(!d3d9) return;
2057
2058     nmodes = IDirect3D9_GetAdapterModeCount(d3d9, D3DADAPTER_DEFAULT, D3DFMT_R5G6B5);
2059     if(!nmodes) {
2060         skip("Display format R5G6B5 not supported, skipping\n");
2061     } else {
2062         trace("Testing display format R5G6B5\n");
2063         for(i=0; r5g6b5_format_list[i].DisplayFormat != 0; i++)
2064         {
2065             hr = IDirect3D9_CheckDeviceType(d3d9, Adapter, DeviceType, r5g6b5_format_list[i].DisplayFormat, r5g6b5_format_list[i].BackBufferFormat, FALSE);
2066
2067             if(r5g6b5_format_list[i].shouldPass)
2068                 ok(hr == D3D_OK ||
2069                    broken(hr == D3DERR_NOTAVAILABLE),
2070                    "format %d %d didn't pass with hr=%#08x\n", r5g6b5_format_list[i].DisplayFormat, r5g6b5_format_list[i].BackBufferFormat, hr);
2071             else
2072                 ok(hr != D3D_OK, "format %d %d didn't pass while it was expected to\n", r5g6b5_format_list[i].DisplayFormat, r5g6b5_format_list[i].BackBufferFormat);
2073         }
2074     }
2075
2076     nmodes = IDirect3D9_GetAdapterModeCount(d3d9, D3DADAPTER_DEFAULT, D3DFMT_X1R5G5B5);
2077     if(!nmodes) {
2078         skip("Display format X1R5G5B5 not supported, skipping\n");
2079     } else {
2080         trace("Testing display format X1R5G5B5\n");
2081         for(i=0; x1r5g5b5_format_list[i].DisplayFormat != 0; i++)
2082         {
2083             hr = IDirect3D9_CheckDeviceType(d3d9, Adapter, DeviceType, x1r5g5b5_format_list[i].DisplayFormat, x1r5g5b5_format_list[i].BackBufferFormat, FALSE);
2084
2085             if(x1r5g5b5_format_list[i].shouldPass)
2086                 ok(hr == D3D_OK, "format %d %d didn't pass with hr=%#08x\n", x1r5g5b5_format_list[i].DisplayFormat, x1r5g5b5_format_list[i].BackBufferFormat, hr);
2087             else
2088                 ok(hr != D3D_OK, "format %d %d didn't pass while it was expected to\n", x1r5g5b5_format_list[i].DisplayFormat, x1r5g5b5_format_list[i].BackBufferFormat);
2089         }
2090     }
2091
2092     nmodes = IDirect3D9_GetAdapterModeCount(d3d9, D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8);
2093     if(!nmodes) {
2094         skip("Display format X8R8G8B8 not supported, skipping\n");
2095     } else {
2096         trace("Testing display format X8R8G8B8\n");
2097         for(i=0; x8r8g8b8_format_list[i].DisplayFormat != 0; i++)
2098         {
2099             hr = IDirect3D9_CheckDeviceType(d3d9, Adapter, DeviceType, x8r8g8b8_format_list[i].DisplayFormat, x8r8g8b8_format_list[i].BackBufferFormat, FALSE);
2100
2101             if(x8r8g8b8_format_list[i].shouldPass)
2102                 ok(hr == D3D_OK ||
2103                    broken(hr == D3DERR_NOTAVAILABLE),
2104                    "format %d %d didn't pass with hr=%#08x\n", x8r8g8b8_format_list[i].DisplayFormat, x8r8g8b8_format_list[i].BackBufferFormat, hr);
2105             else
2106                 ok(hr != D3D_OK, "format %d %d didn't pass while it was expected to\n", x8r8g8b8_format_list[i].DisplayFormat, x8r8g8b8_format_list[i].BackBufferFormat);
2107         }
2108     }
2109
2110     if(d3d9) IDirect3D9_Release(d3d9);
2111 }
2112
2113 static void test_scissor_size(void)
2114 {
2115     IDirect3D9 *d3d9_ptr = 0;
2116     unsigned int i;
2117     static const struct {
2118         int winx; int winy; int backx; int backy; BOOL window;
2119     } scts[] = { /* scissor tests */
2120         {800, 600, 640, 480, TRUE},
2121         {800, 600, 640, 480, FALSE},
2122         {640, 480, 800, 600, TRUE},
2123         {640, 480, 800, 600, FALSE},
2124     };
2125
2126     d3d9_ptr = pDirect3DCreate9(D3D_SDK_VERSION);
2127     ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
2128     if (!d3d9_ptr){
2129         skip("Failed to create IDirect3D9 object\n");
2130         return;
2131     }
2132
2133     for(i=0; i<sizeof(scts)/sizeof(scts[0]); i++) {
2134         IDirect3DDevice9 *device_ptr = 0;
2135         D3DPRESENT_PARAMETERS present_parameters;
2136         HRESULT hr;
2137         WNDCLASS wc = {0};
2138         HWND hwnd = 0;
2139         RECT scissorrect;
2140
2141         wc.lpfnWndProc = DefWindowProc;
2142         wc.lpszClassName = "d3d9_test_wc";
2143         RegisterClass(&wc);
2144
2145         hwnd = CreateWindow("d3d9_test_wc", "d3d9_test",
2146                         WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, scts[i].winx, scts[i].winy, 0, 0, 0, 0);
2147
2148         ZeroMemory(&present_parameters, sizeof(present_parameters));
2149         present_parameters.Windowed = scts[i].window;
2150         present_parameters.hDeviceWindow = hwnd;
2151         present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
2152         present_parameters.BackBufferWidth = scts[i].backx;
2153         present_parameters.BackBufferHeight = scts[i].backy;
2154         present_parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
2155         present_parameters.EnableAutoDepthStencil = TRUE;
2156         present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
2157
2158         hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
2159         if(FAILED(hr)) {
2160             present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
2161             hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
2162             if(FAILED(hr)) {
2163                 hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
2164             }
2165         }
2166         ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D_CreateDevice returned: %08x\n", hr);
2167
2168         if (!device_ptr)
2169         {
2170             DestroyWindow(hwnd);
2171             skip("Creating the device failed\n");
2172             goto err_out;
2173         }
2174
2175         /* Check for the default scissor rect size */
2176         hr = IDirect3DDevice9_GetScissorRect(device_ptr, &scissorrect);
2177         ok(hr == D3D_OK, "IDirect3DDevice9_GetScissorRect failed with: %08x\n", hr);
2178         ok(scissorrect.right == scts[i].backx && scissorrect.bottom == scts[i].backy && scissorrect.top == 0 && scissorrect.left == 0, "Scissorrect missmatch (%d, %d) should be (%d, %d)\n", scissorrect.right, scissorrect.bottom, scts[i].backx, scts[i].backy);
2179
2180         /* check the scissorrect values after a reset */
2181         present_parameters.BackBufferWidth = 800;
2182         present_parameters.BackBufferHeight = 600;
2183         hr = IDirect3DDevice9_Reset(device_ptr, &present_parameters);
2184         ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %08x\n", hr);
2185         hr = IDirect3DDevice9_TestCooperativeLevel(device_ptr);
2186         ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
2187
2188         hr = IDirect3DDevice9_GetScissorRect(device_ptr, &scissorrect);
2189         ok(hr == D3D_OK, "IDirect3DDevice9_GetScissorRect failed with: %08x\n", hr);
2190         ok(scissorrect.right == 800 && scissorrect.bottom == 600 && scissorrect.top == 0 && scissorrect.left == 0, "Scissorrect missmatch (%d, %d) should be (%d, %d)\n", scissorrect.right, scissorrect.bottom, 800, 600);
2191
2192         if(device_ptr) {
2193             ULONG ref;
2194
2195             ref = IDirect3DDevice9_Release(device_ptr);
2196             DestroyWindow(hwnd);
2197             ok(ref == 0, "The device was not properly freed: refcount %u\n", ref);
2198         }
2199     }
2200
2201 err_out:
2202     if(d3d9_ptr) IDirect3D9_Release(d3d9_ptr);
2203     return;
2204 }
2205
2206
2207 START_TEST(device)
2208 {
2209     HMODULE d3d9_handle = LoadLibraryA( "d3d9.dll" );
2210     if (!d3d9_handle)
2211     {
2212         skip("Could not load d3d9.dll\n");
2213         return;
2214     }
2215
2216     pDirect3DCreate9 = (void *)GetProcAddress( d3d9_handle, "Direct3DCreate9" );
2217     ok(pDirect3DCreate9 != NULL, "Failed to get address of Direct3DCreate9\n");
2218     if (pDirect3DCreate9)
2219     {
2220         IDirect3D9 *d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
2221         if(!d3d9)
2222         {
2223             skip("could not create D3D9 object\n");
2224             return;
2225         }
2226         IDirect3D9_Release(d3d9);
2227
2228         test_display_formats();
2229         test_display_modes();
2230         test_swapchain();
2231         test_refcount();
2232         test_mipmap_levels();
2233         test_cursor();
2234         test_reset();
2235         test_scene();
2236         test_limits();
2237         test_depthstenciltest();
2238         test_draw_indexed();
2239         test_null_stream();
2240         test_vertex_buffer_alignment();
2241         test_lights();
2242         test_set_stream_source();
2243         test_scissor_size();
2244     }
2245 }