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