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