d3d9: Reject IDirect3DDevice9::Reset with active default pool resources.
[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 refernce to the device
360      *   - they are created with a refcount of 0 (Get/Release returns orignial 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;
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
728     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
729     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
730     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
731     ok(hwnd != NULL, "Failed to create window\n");
732     if (!pD3d || !hwnd) goto cleanup;
733
734     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
735     ZeroMemory( &d3dpp, sizeof(d3dpp) );
736     d3dpp.Windowed         = FALSE;
737     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
738     d3dpp.BackBufferWidth  = 800;
739     d3dpp.BackBufferHeight  = 600;
740     d3dpp.BackBufferFormat = d3ddm.Format;
741
742     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
743                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
744
745     if(FAILED(hr))
746     {
747         skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
748         goto cleanup;
749     }
750
751     width = GetSystemMetrics(SM_CXSCREEN);
752     height = GetSystemMetrics(SM_CYSCREEN);
753     ok(width == 800, "Screen width is %d\n", width);
754     ok(height == 600, "Screen height is %d\n", height);
755
756     hr = IDirect3DDevice9_GetViewport(pDevice, &vp);
757     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %s\n", DXGetErrorString9(hr));
758     if(SUCCEEDED(hr))
759     {
760         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
761         ok(vp.Y == 0, "D3DVIEWPORT->X = %d\n", vp.Y);
762         ok(vp.Width == 800, "D3DVIEWPORT->X = %d\n", vp.Width);
763         ok(vp.Height == 600, "D3DVIEWPORT->X = %d\n", vp.Height);
764         ok(vp.MinZ == 0, "D3DVIEWPORT->X = %d\n", vp.Height);
765         ok(vp.MaxZ == 1, "D3DVIEWPORT->X = %d\n", vp.Height);
766     }
767     vp.X = 10;
768     vp.X = 20;
769     vp.MinZ = 2;
770     vp.MaxZ = 3;
771     hr = IDirect3DDevice9_SetViewport(pDevice, &vp);
772     ok(hr == D3D_OK, "IDirect3DDevice9_SetViewport failed with %s\n", DXGetErrorString9(hr));
773
774     ZeroMemory( &d3dpp, sizeof(d3dpp) );
775     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
776     d3dpp.Windowed         = FALSE;
777     d3dpp.BackBufferWidth  = 640;
778     d3dpp.BackBufferHeight  = 480;
779     d3dpp.BackBufferFormat = d3ddm.Format;
780     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
781     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
782
783     ZeroMemory(&vp, sizeof(vp));
784     hr = IDirect3DDevice9_GetViewport(pDevice, &vp);
785     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %s\n", DXGetErrorString9(hr));
786     if(SUCCEEDED(hr))
787     {
788         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
789         ok(vp.Y == 0, "D3DVIEWPORT->X = %d\n", vp.Y);
790         ok(vp.Width == 640, "D3DVIEWPORT->X = %d\n", vp.Width);
791         ok(vp.Height == 480, "D3DVIEWPORT->X = %d\n", vp.Height);
792         ok(vp.MinZ == 0, "D3DVIEWPORT->X = %d\n", vp.Height);
793         ok(vp.MaxZ == 1, "D3DVIEWPORT->X = %d\n", vp.Height);
794     }
795
796     width = GetSystemMetrics(SM_CXSCREEN);
797     height = GetSystemMetrics(SM_CYSCREEN);
798     ok(width == 640, "Screen width is %d\n", width);
799     ok(height == 480, "Screen height is %d\n", height);
800
801     hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &pSwapchain);
802     ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %s\n", DXGetErrorString9(hr));
803     if(SUCCEEDED(hr))
804     {
805         ZeroMemory(&d3dpp, sizeof(d3dpp));
806         hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
807         ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %s\n", DXGetErrorString9(hr));
808         if(SUCCEEDED(hr))
809         {
810             ok(d3dpp.BackBufferWidth == 640, "Back buffer width is %d\n", d3dpp.BackBufferWidth);
811             ok(d3dpp.BackBufferHeight == 480, "Back buffer height is %d\n", d3dpp.BackBufferHeight);
812         }
813         IDirect3DSwapChain9_Release(pSwapchain);
814     }
815
816     ZeroMemory( &d3dpp, sizeof(d3dpp) );
817     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
818     d3dpp.Windowed         = TRUE;
819     d3dpp.BackBufferWidth  = 400;
820     d3dpp.BackBufferHeight  = 300;
821     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
822     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
823
824     width = GetSystemMetrics(SM_CXSCREEN);
825     height = GetSystemMetrics(SM_CYSCREEN);
826     ok(width == orig_width, "Screen width is %d\n", width);
827     ok(height == orig_height, "Screen height is %d\n", height);
828
829     ZeroMemory(&vp, sizeof(vp));
830     hr = IDirect3DDevice9_GetViewport(pDevice, &vp);
831     ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %s\n", DXGetErrorString9(hr));
832     if(SUCCEEDED(hr))
833     {
834         ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
835         ok(vp.Y == 0, "D3DVIEWPORT->X = %d\n", vp.Y);
836         ok(vp.Width == 400, "D3DVIEWPORT->X = %d\n", vp.Width);
837         ok(vp.Height == 300, "D3DVIEWPORT->X = %d\n", vp.Height);
838         ok(vp.MinZ == 0, "D3DVIEWPORT->X = %d\n", vp.Height);
839         ok(vp.MaxZ == 1, "D3DVIEWPORT->X = %d\n", vp.Height);
840     }
841
842     hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &pSwapchain);
843     ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %s\n", DXGetErrorString9(hr));
844     if(SUCCEEDED(hr))
845     {
846         ZeroMemory(&d3dpp, sizeof(d3dpp));
847         hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
848         ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %s\n", DXGetErrorString9(hr));
849         if(SUCCEEDED(hr))
850         {
851             ok(d3dpp.BackBufferWidth == 400, "Back buffer width is %d\n", d3dpp.BackBufferWidth);
852             ok(d3dpp.BackBufferHeight == 300, "Back buffer height is %d\n", d3dpp.BackBufferHeight);
853         }
854         IDirect3DSwapChain9_Release(pSwapchain);
855     }
856
857     ZeroMemory( &d3dpp, sizeof(d3dpp) );
858     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
859     d3dpp.Windowed         = TRUE;
860     d3dpp.BackBufferWidth  = 400;
861     d3dpp.BackBufferHeight  = 300;
862
863     /* _Reset fails if there is a resource in the default pool */
864     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 16, 16, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &surface, NULL);
865     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %s\n", DXGetErrorString9(hr));
866     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
867     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
868     IDirect3DSurface9_Release(surface);
869     /* Reset again to get the device out of the lost state */
870     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
871     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
872
873     /* Scratch, sysmem and managed pools are fine */
874     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SCRATCH, &surface, NULL);
875     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %s\n", DXGetErrorString9(hr));
876     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
877     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
878     IDirect3DSurface9_Release(surface);
879     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface, NULL);
880     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %s\n", DXGetErrorString9(hr));
881     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
882     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
883     IDirect3DSurface9_Release(surface);
884     hr = IDirect3DDevice9_CreateTexture(pDevice, 16, 16, 0, 0, D3DFMT_R5G6B5, D3DPOOL_MANAGED, &texture, NULL);
885     ok(hr == D3D_OK, "IDirect3DDevice9_CreateTexture returned %s\n", DXGetErrorString9(hr));
886     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
887     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
888     IDirect3DTexture9_Release(texture);
889
890     /* A reference held to an implicit surface causes failures as well */
891     hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, D3DBACKBUFFER_TYPE_MONO, &surface);
892     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer returned %s\n", DXGetErrorString9(hr));
893     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
894     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
895     IDirect3DSurface9_Release(surface);
896     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
897     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
898
899     /* Shaders are fine as well */
900     hr = IDirect3DDevice9_CreateVertexShader(pDevice, simple_vs, &shader);
901     ok(hr == D3D_OK, "IDirect3DDevice9_CreateVertexShader returned %s\n", DXGetErrorString9(hr));
902     hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
903     ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
904     IDirect3DVertexShader9_Release(shader);
905
906 cleanup:
907     if(pD3d) IDirect3D9_Release(pD3d);
908     if(pDevice) IDirect3D9_Release(pDevice);
909 }
910
911 /* Test adapter display modes */
912 static void test_display_modes(void)
913 {
914     D3DDISPLAYMODE dmode;
915     IDirect3D9 *pD3d;
916
917     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
918     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
919     if(!pD3d) return;
920
921 #define TEST_FMT(x,r) do { \
922     HRESULT res = IDirect3D9_EnumAdapterModes(pD3d, 0, (x), 0, &dmode); \
923     ok(res==(r), "EnumAdapterModes("#x") did not return "#r" (got %s)!\n", DXGetErrorString9(res)); \
924 } while(0)
925
926     TEST_FMT(D3DFMT_R8G8B8, D3DERR_INVALIDCALL);
927     TEST_FMT(D3DFMT_A8R8G8B8, D3DERR_INVALIDCALL);
928     TEST_FMT(D3DFMT_X8B8G8R8, D3DERR_INVALIDCALL);
929     /* D3DFMT_R5G6B5 */
930     TEST_FMT(D3DFMT_X1R5G5B5, D3DERR_INVALIDCALL);
931     TEST_FMT(D3DFMT_A1R5G5B5, D3DERR_INVALIDCALL);
932     TEST_FMT(D3DFMT_A4R4G4B4, D3DERR_INVALIDCALL);
933     TEST_FMT(D3DFMT_R3G3B2, D3DERR_INVALIDCALL);
934     TEST_FMT(D3DFMT_A8, D3DERR_INVALIDCALL);
935     TEST_FMT(D3DFMT_A8R3G3B2, D3DERR_INVALIDCALL);
936     TEST_FMT(D3DFMT_X4R4G4B4, D3DERR_INVALIDCALL);
937     TEST_FMT(D3DFMT_A2B10G10R10, D3DERR_INVALIDCALL);
938     TEST_FMT(D3DFMT_A8B8G8R8, D3DERR_INVALIDCALL);
939     TEST_FMT(D3DFMT_X8B8G8R8, D3DERR_INVALIDCALL);
940     TEST_FMT(D3DFMT_G16R16, D3DERR_INVALIDCALL);
941     TEST_FMT(D3DFMT_A2R10G10B10, D3DERR_INVALIDCALL);
942     TEST_FMT(D3DFMT_A16B16G16R16, D3DERR_INVALIDCALL);
943
944     TEST_FMT(D3DFMT_A8P8, D3DERR_INVALIDCALL);
945     TEST_FMT(D3DFMT_P8, D3DERR_INVALIDCALL);
946
947     TEST_FMT(D3DFMT_L8, D3DERR_INVALIDCALL);
948     TEST_FMT(D3DFMT_A8L8, D3DERR_INVALIDCALL);
949     TEST_FMT(D3DFMT_A4L4, D3DERR_INVALIDCALL);
950
951     TEST_FMT(D3DFMT_V8U8, D3DERR_INVALIDCALL);
952     TEST_FMT(D3DFMT_L6V5U5, D3DERR_INVALIDCALL);
953     TEST_FMT(D3DFMT_X8L8V8U8, D3DERR_INVALIDCALL);
954     TEST_FMT(D3DFMT_Q8W8V8U8, D3DERR_INVALIDCALL);
955     TEST_FMT(D3DFMT_V16U16, D3DERR_INVALIDCALL);
956     TEST_FMT(D3DFMT_A2W10V10U10, D3DERR_INVALIDCALL);
957
958     TEST_FMT(D3DFMT_UYVY, D3DERR_INVALIDCALL);
959     TEST_FMT(D3DFMT_YUY2, D3DERR_INVALIDCALL);
960     TEST_FMT(D3DFMT_DXT1, D3DERR_INVALIDCALL);
961     TEST_FMT(D3DFMT_DXT2, D3DERR_INVALIDCALL);
962     TEST_FMT(D3DFMT_DXT3, D3DERR_INVALIDCALL);
963     TEST_FMT(D3DFMT_DXT4, D3DERR_INVALIDCALL);
964     TEST_FMT(D3DFMT_DXT5, D3DERR_INVALIDCALL);
965     TEST_FMT(D3DFMT_MULTI2_ARGB8, D3DERR_INVALIDCALL);
966     TEST_FMT(D3DFMT_G8R8_G8B8, D3DERR_INVALIDCALL);
967     TEST_FMT(D3DFMT_R8G8_B8G8, D3DERR_INVALIDCALL);
968
969     TEST_FMT(D3DFMT_D16_LOCKABLE, D3DERR_INVALIDCALL);
970     TEST_FMT(D3DFMT_D32, D3DERR_INVALIDCALL);
971     TEST_FMT(D3DFMT_D15S1, D3DERR_INVALIDCALL);
972     TEST_FMT(D3DFMT_D24S8, D3DERR_INVALIDCALL);
973     TEST_FMT(D3DFMT_D24X8, D3DERR_INVALIDCALL);
974     TEST_FMT(D3DFMT_D24X4S4, D3DERR_INVALIDCALL);
975     TEST_FMT(D3DFMT_D16, D3DERR_INVALIDCALL);
976     TEST_FMT(D3DFMT_L16, D3DERR_INVALIDCALL);
977     TEST_FMT(D3DFMT_D32F_LOCKABLE, D3DERR_INVALIDCALL);
978     TEST_FMT(D3DFMT_D24FS8, D3DERR_INVALIDCALL);
979
980     TEST_FMT(D3DFMT_VERTEXDATA, D3DERR_INVALIDCALL);
981     TEST_FMT(D3DFMT_INDEX16, D3DERR_INVALIDCALL);
982     TEST_FMT(D3DFMT_INDEX32, D3DERR_INVALIDCALL);
983     TEST_FMT(D3DFMT_Q16W16V16U16, D3DERR_INVALIDCALL);
984     /* Floating point formats */
985     TEST_FMT(D3DFMT_R16F, D3DERR_INVALIDCALL);
986     TEST_FMT(D3DFMT_G16R16F, D3DERR_INVALIDCALL);
987     TEST_FMT(D3DFMT_A16B16G16R16F, D3DERR_INVALIDCALL);
988
989     /* IEEE formats */
990     TEST_FMT(D3DFMT_R32F, D3DERR_INVALIDCALL);
991     TEST_FMT(D3DFMT_G32R32F, D3DERR_INVALIDCALL);
992     TEST_FMT(D3DFMT_A32B32G32R32F, D3DERR_INVALIDCALL);
993
994     TEST_FMT(D3DFMT_CxV8U8, D3DERR_INVALIDCALL);
995
996     TEST_FMT(0, D3DERR_INVALIDCALL);
997
998     IDirect3D9_Release(pD3d);
999 }
1000
1001 static void test_scene(void)
1002 {
1003     HRESULT                      hr;
1004     HWND                         hwnd               = NULL;
1005     IDirect3D9                  *pD3d               = NULL;
1006     IDirect3DDevice9            *pDevice            = NULL;
1007     D3DPRESENT_PARAMETERS        d3dpp;
1008     D3DDISPLAYMODE               d3ddm;
1009     IDirect3DSurface9            *pSurface1 = NULL, *pSurface2 = NULL, *pSurface3 = NULL, *pRenderTarget = NULL;
1010     IDirect3DSurface9            *pBackBuffer = NULL, *pDepthStencil = NULL;
1011     RECT rect = {0, 0, 128, 128};
1012     D3DCAPS9                     caps;
1013
1014     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1015     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1016     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1017     ok(hwnd != NULL, "Failed to create window\n");
1018     if (!pD3d || !hwnd) goto cleanup;
1019
1020     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1021     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1022     d3dpp.Windowed         = TRUE;
1023     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1024     d3dpp.BackBufferWidth  = 800;
1025     d3dpp.BackBufferHeight  = 600;
1026     d3dpp.BackBufferFormat = d3ddm.Format;
1027     d3dpp.EnableAutoDepthStencil = TRUE;
1028     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1029
1030     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1031                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1032     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1033     if(!pDevice)
1034     {
1035         skip("Failed to create a d3d device\n");
1036         goto cleanup;
1037     }
1038
1039     /* Get the caps, they will be needed to tell if an operation is supposed to be valid */
1040     memset(&caps, 0, sizeof(caps));
1041     hr = IDirect3DDevice9_GetDeviceCaps(pDevice, &caps);
1042     ok(hr == D3D_OK, "IDirect3DDevice9_GetCaps failed with %s\n", DXGetErrorString9(hr));
1043     if(FAILED(hr)) goto cleanup;
1044
1045     /* Test an EndScene without beginscene. Should return an error */
1046     hr = IDirect3DDevice9_EndScene(pDevice);
1047     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %s\n", DXGetErrorString9(hr));
1048
1049     /* Test a normal BeginScene / EndScene pair, this should work */
1050     hr = IDirect3DDevice9_BeginScene(pDevice);
1051     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
1052     if(SUCCEEDED(hr))
1053     {
1054         hr = IDirect3DDevice9_EndScene(pDevice);
1055         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
1056     }
1057
1058     /* Test another EndScene without having begun a new scene. Should return an error */
1059     hr = IDirect3DDevice9_EndScene(pDevice);
1060     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %s\n", DXGetErrorString9(hr));
1061
1062     /* Two nested BeginScene and EndScene calls */
1063     hr = IDirect3DDevice9_BeginScene(pDevice);
1064     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
1065     hr = IDirect3DDevice9_BeginScene(pDevice);
1066     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_BeginScene returned %s\n", DXGetErrorString9(hr));
1067     hr = IDirect3DDevice9_EndScene(pDevice);
1068     ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
1069     hr = IDirect3DDevice9_EndScene(pDevice);
1070     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %s\n", DXGetErrorString9(hr));
1071
1072     /* Create some surfaces to test stretchrect between the scenes */
1073     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface1, NULL);
1074     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %s\n", DXGetErrorString9(hr));
1075     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface2, NULL);
1076     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %s\n", DXGetErrorString9(hr));
1077     hr = IDirect3DDevice9_CreateDepthStencilSurface(pDevice, 800, 600, D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, FALSE, &pSurface3, NULL);
1078     ok(hr == D3D_OK, "IDirect3DDevice9_CreateDepthStencilSurface failed with %s\n", DXGetErrorString9(hr));
1079     hr = IDirect3DDevice9_CreateRenderTarget(pDevice, 128, 128, d3ddm.Format, D3DMULTISAMPLE_NONE, 0, FALSE, &pRenderTarget, NULL);
1080     ok(hr == D3D_OK, "IDirect3DDevice9_CreateRenderTarget failed with %s\n", DXGetErrorString9(hr));
1081
1082     hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
1083     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer failed with %s\n", DXGetErrorString9(hr));
1084     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1085     ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer failed with %s\n", DXGetErrorString9(hr));
1086
1087     /* First make sure a simple StretchRect call works */
1088     if(pSurface1 && pSurface2) {
1089         hr = IDirect3DDevice9_StretchRect(pDevice, pSurface1, NULL, pSurface2, NULL, 0);
1090         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %s\n", DXGetErrorString9(hr));
1091     }
1092     if(pBackBuffer && pRenderTarget) {
1093         hr = IDirect3DDevice9_StretchRect(pDevice, pBackBuffer, &rect, pRenderTarget, NULL, 0);
1094         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %s\n", DXGetErrorString9(hr));
1095     }
1096     if(pDepthStencil && pSurface3) {
1097         HRESULT expected;
1098         if(0) /* Disabled for now because it crashes in wine */ {
1099             expected = caps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES ? D3D_OK : D3DERR_INVALIDCALL;
1100             hr = IDirect3DDevice9_StretchRect(pDevice, pDepthStencil, NULL, pSurface3, NULL, 0);
1101             ok( hr == expected, "IDirect3DDevice9_StretchRect returned %s, expected %s\n", DXGetErrorString9(hr), DXGetErrorString9(expected));
1102         }
1103     }
1104
1105     /* Now try it in a BeginScene - EndScene pair. Seems to be allowed in a beginScene - Endscene pair
1106      * width normal surfaces, render targets and depth stencil surfaces.
1107      */
1108     hr = IDirect3DDevice9_BeginScene(pDevice);
1109     ok( hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
1110
1111     if(pSurface1 && pSurface2)
1112     {
1113         hr = IDirect3DDevice9_StretchRect(pDevice, pSurface1, NULL, pSurface2, NULL, 0);
1114         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %s\n", DXGetErrorString9(hr));
1115     }
1116     if(pBackBuffer && pRenderTarget)
1117     {
1118         hr = IDirect3DDevice9_StretchRect(pDevice, pBackBuffer, &rect, pRenderTarget, NULL, 0);
1119         ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %s\n", DXGetErrorString9(hr));
1120     }
1121     if(pDepthStencil && pSurface3)
1122     {
1123         /* This is supposed to fail inside a BeginScene - EndScene pair. */
1124         hr = IDirect3DDevice9_StretchRect(pDevice, pDepthStencil, NULL, pSurface3, NULL, 0);
1125         ok( hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_StretchRect returned %s, expected D3DERR_INVALIDCALL\n", DXGetErrorString9(hr));
1126     }
1127
1128     hr = IDirect3DDevice9_EndScene(pDevice);
1129     ok( hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
1130
1131     /* Does a SetRenderTarget influence BeginScene / EndScene ?
1132      * Set a new render target, then see if it started a new scene. Flip the rt back and see if that maybe
1133      * ended the scene. Expected result is that the scene is not affected by SetRenderTarget
1134      */
1135     hr = IDirect3DDevice9_SetRenderTarget(pDevice, 0, pRenderTarget);
1136     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget failed with %s\n", DXGetErrorString9(hr));
1137     hr = IDirect3DDevice9_BeginScene(pDevice);
1138     ok( hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
1139     hr = IDirect3DDevice9_SetRenderTarget(pDevice, 0, pBackBuffer);
1140     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget failed with %s\n", DXGetErrorString9(hr));
1141     hr = IDirect3DDevice9_EndScene(pDevice);
1142     ok( hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
1143
1144 cleanup:
1145     if(pRenderTarget) IDirect3DSurface9_Release(pRenderTarget);
1146     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1147     if(pBackBuffer) IDirect3DSurface9_Release(pBackBuffer);
1148     if(pSurface1) IDirect3DSurface9_Release(pSurface1);
1149     if(pSurface2) IDirect3DSurface9_Release(pSurface2);
1150     if(pSurface3) IDirect3DSurface9_Release(pSurface3);
1151     if(pD3d) IDirect3D9_Release(pD3d);
1152     if(pDevice) IDirect3D9_Release(pDevice);
1153     if(hwnd) DestroyWindow(hwnd);
1154 }
1155
1156 static void test_limits(void)
1157 {
1158     HRESULT                      hr;
1159     HWND                         hwnd               = NULL;
1160     IDirect3D9                  *pD3d               = NULL;
1161     IDirect3DDevice9            *pDevice            = NULL;
1162     D3DPRESENT_PARAMETERS        d3dpp;
1163     D3DDISPLAYMODE               d3ddm;
1164     IDirect3DTexture9           *pTexture           = NULL;
1165     int i;
1166
1167     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1168     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1169     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1170     ok(hwnd != NULL, "Failed to create window\n");
1171     if (!pD3d || !hwnd) goto cleanup;
1172
1173     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1174     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1175     d3dpp.Windowed         = TRUE;
1176     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1177     d3dpp.BackBufferWidth  = 800;
1178     d3dpp.BackBufferHeight  = 600;
1179     d3dpp.BackBufferFormat = d3ddm.Format;
1180     d3dpp.EnableAutoDepthStencil = TRUE;
1181     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1182
1183     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1184                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1185     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1186     if(!pDevice)
1187     {
1188         skip("Failed to create a d3d device\n");
1189         goto cleanup;
1190     }
1191
1192     hr = IDirect3DDevice9_CreateTexture(pDevice, 16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture, NULL);
1193     ok(hr == D3D_OK, "IDirect3DDevice9_CreateTexture failed with %s\n", DXGetErrorString9(hr));
1194     if(!pTexture) goto cleanup;
1195
1196     /* There are 16 pixel samplers. We should be able to access all of them */
1197     for(i = 0; i < 16; i++) {
1198         hr = IDirect3DDevice9_SetTexture(pDevice, i, (IDirect3DBaseTexture9 *) pTexture);
1199         ok(hr == D3D_OK, "IDirect3DDevice9_SetTexture for sampler %d failed with %s\n", i, DXGetErrorString9(hr));
1200         hr = IDirect3DDevice9_SetTexture(pDevice, i, NULL);
1201         ok(hr == D3D_OK, "IDirect3DDevice9_SetTexture for sampler %d failed with %s\n", i, DXGetErrorString9(hr));
1202         hr = IDirect3DDevice9_SetSamplerState(pDevice, i, D3DSAMP_SRGBTEXTURE, TRUE);
1203         ok(hr == D3D_OK, "IDirect3DDevice9_SetSamplerState for sampler %d failed with %s\n", i, DXGetErrorString9(hr));
1204     }
1205
1206     /* Now test all 8 textures stage states */
1207     for(i = 0; i < 8; i++) {
1208         hr = IDirect3DDevice9_SetTextureStageState(pDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
1209         ok(hr == D3D_OK, "IDirect3DDevice9_SetTextureStageState for texture %d failed with %s\n", i, DXGetErrorString9(hr));
1210     }
1211
1212     /* Investigations show that accessing higher samplers / textures stage states does not return an error either. Writing
1213      * to too high samplers(approximately sampler 40) causes memory corruption in windows, so there is no bounds checking
1214      * but how do I test that?
1215      */
1216 cleanup:
1217     if(pTexture) IDirect3DTexture9_Release(pTexture);
1218     if(pD3d) IDirect3D9_Release(pD3d);
1219     if(pDevice) IDirect3D9_Release(pDevice);
1220     if(hwnd) DestroyWindow(hwnd);
1221 }
1222
1223 static void test_depthstenciltest(void)
1224 {
1225     HRESULT                      hr;
1226     HWND                         hwnd               = NULL;
1227     IDirect3D9                  *pD3d               = NULL;
1228     IDirect3DDevice9            *pDevice            = NULL;
1229     D3DPRESENT_PARAMETERS        d3dpp;
1230     D3DDISPLAYMODE               d3ddm;
1231     IDirect3DSurface9           *pDepthStencil           = NULL;
1232     IDirect3DSurface9           *pDepthStencil2          = NULL;
1233     D3DZBUFFERTYPE               state;
1234
1235     pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1236     ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1237     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1238     ok(hwnd != NULL, "Failed to create window\n");
1239     if (!pD3d || !hwnd) goto cleanup;
1240
1241     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1242     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1243     d3dpp.Windowed         = TRUE;
1244     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1245     d3dpp.BackBufferWidth  = 800;
1246     d3dpp.BackBufferHeight  = 600;
1247     d3dpp.BackBufferFormat = d3ddm.Format;
1248     d3dpp.EnableAutoDepthStencil = TRUE;
1249     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1250
1251     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1252                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1253     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1254     if(!pDevice)
1255     {
1256         skip("Failed to create a d3d device\n");
1257         goto cleanup;
1258     }
1259
1260     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1261     ok(hr == D3D_OK && pDepthStencil != NULL, "IDirect3DDevice9_GetDepthStencilSurface failed with %s\n", DXGetErrorString9(hr));
1262
1263     /* Try to clear */
1264     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1265     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
1266
1267     hr = IDirect3DDevice9_SetDepthStencilSurface(pDevice, NULL);
1268     ok(hr == D3D_OK, "IDirect3DDevice9_SetDepthStencilSurface failed with %s\n", DXGetErrorString9(hr));
1269
1270     /* Check if the set buffer is returned on a get. WineD3D had a bug with that once, prevent it from coming back */
1271     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil2);
1272     ok(hr == D3DERR_NOTFOUND && pDepthStencil2 == NULL, "IDirect3DDevice9_GetDepthStencilSurface failed with %s\n", DXGetErrorString9(hr));
1273     if(pDepthStencil2) IDirect3DSurface9_Release(pDepthStencil2);
1274
1275     /* This left the render states untouched! */
1276     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1277     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1278     ok(state == D3DZB_TRUE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1279     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZWRITEENABLE, &state);
1280     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1281     ok(state == TRUE, "D3DRS_ZWRITEENABLE is %s\n", state ? "TRUE" : "FALSE");
1282     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_STENCILENABLE, &state);
1283     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1284     ok(state == FALSE, "D3DRS_STENCILENABLE is %s\n", state ? "TRUE" : "FALSE");
1285     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_STENCILWRITEMASK, &state);
1286     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1287     ok(state == 0xffffffff, "D3DRS_STENCILWRITEMASK is 0x%08x\n", state);
1288
1289     /* This is supposed to fail now */
1290     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1291     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
1292
1293     hr = IDirect3DDevice9_SetRenderState(pDevice, D3DRS_ZENABLE, D3DZB_FALSE);
1294     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState failed with %s\n", DXGetErrorString9(hr));
1295
1296     hr = IDirect3DDevice9_SetDepthStencilSurface(pDevice, pDepthStencil);
1297     ok(hr == D3D_OK, "IDirect3DDevice9_SetDepthStencilSurface failed with %s\n", DXGetErrorString9(hr));
1298
1299     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1300     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1301     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1302
1303     /* Now it works again */
1304     hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1305     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
1306
1307     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1308     if(pDevice) IDirect3D9_Release(pDevice);
1309
1310     /* Now see if autodepthstencil disable is honored. First, without a format set */
1311     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1312     d3dpp.Windowed         = TRUE;
1313     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1314     d3dpp.BackBufferWidth  = 800;
1315     d3dpp.BackBufferHeight  = 600;
1316     d3dpp.BackBufferFormat = d3ddm.Format;
1317     d3dpp.EnableAutoDepthStencil = FALSE;
1318     d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
1319
1320     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1321                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1322     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1323     if(!pDevice)
1324     {
1325         skip("Failed to create a d3d device\n");
1326         goto cleanup;
1327     }
1328
1329     pDepthStencil = NULL;
1330     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1331     ok(hr == D3DERR_NOTFOUND && pDepthStencil == NULL, "IDirect3DDevice9_GetDepthStencilSurface returned %s, surface = %p\n", DXGetErrorString9(hr), pDepthStencil);
1332     if(pDepthStencil) {
1333         IDirect3DSurface9_Release(pDepthStencil);
1334         pDepthStencil = NULL;
1335     }
1336
1337     /* Check the depth test state */
1338     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1339     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1340     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1341
1342     if(pDevice) IDirect3D9_Release(pDevice);
1343
1344     /* Next, try EnableAutoDepthStencil FALSE with a depth stencil format set */
1345     ZeroMemory( &d3dpp, sizeof(d3dpp) );
1346     d3dpp.Windowed         = TRUE;
1347     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
1348     d3dpp.BackBufferWidth  = 800;
1349     d3dpp.BackBufferHeight  = 600;
1350     d3dpp.BackBufferFormat = d3ddm.Format;
1351     d3dpp.EnableAutoDepthStencil = FALSE;
1352     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1353
1354     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1355                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1356     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1357     if(!pDevice)
1358     {
1359         skip("Failed to create a d3d device\n");
1360         goto cleanup;
1361     }
1362
1363     pDepthStencil = NULL;
1364     hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1365     ok(hr == D3DERR_NOTFOUND && pDepthStencil == NULL, "IDirect3DDevice9_GetDepthStencilSurface returned %s, surface = %p\n", DXGetErrorString9(hr), pDepthStencil);
1366     if(pDepthStencil) {
1367         IDirect3DSurface9_Release(pDepthStencil);
1368         pDepthStencil = NULL;
1369     }
1370
1371     hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1372     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1373     ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1374
1375 cleanup:
1376     if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1377     if(pD3d) IDirect3D9_Release(pD3d);
1378     if(pDevice) IDirect3D9_Release(pDevice);
1379     if(hwnd) DestroyWindow(hwnd);
1380 }
1381
1382 /* Test what happens when IDirect3DDevice9_DrawIndexedPrimitive is called without a valid index buffer set. */
1383 static void test_draw_indexed(void)
1384 {
1385     static const struct {
1386         float position[3];
1387         DWORD color;
1388     } quad[] = {
1389         {{-1.0f, -1.0f, 0.0f}, 0xffff0000},
1390         {{-1.0f,  1.0f, 0.0f}, 0xffff0000},
1391         {{ 1.0f,  1.0f, 0.0f}, 0xffff0000},
1392         {{ 1.0f, -1.0f, 0.0f}, 0xffff0000},
1393     };
1394     WORD indices[] = {0, 1, 2, 3, 0, 2};
1395
1396     static const D3DVERTEXELEMENT9 decl_elements[] = {
1397         {0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1398         {0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,    D3DDECLUSAGE_COLOR, 0},
1399         D3DDECL_END()
1400     };
1401
1402     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
1403     IDirect3DVertexBuffer9 *vertex_buffer = NULL;
1404     IDirect3DIndexBuffer9 *index_buffer = NULL;
1405     D3DPRESENT_PARAMETERS present_parameters;
1406     IDirect3DDevice9 *device = NULL;
1407     IDirect3D9 *d3d9;
1408     HRESULT hr;
1409     HWND hwnd;
1410     void *ptr;
1411
1412     hwnd = CreateWindow("static", "d3d9_test",
1413             0, 0, 0, 10, 10, 0, 0, 0, 0);
1414     if (!hwnd)
1415     {
1416         skip("Failed to create window\n");
1417         return;
1418     }
1419
1420     d3d9 = pDirect3DCreate9(D3D_SDK_VERSION);
1421     if (!d3d9)
1422     {
1423         skip("Failed to create IDirect3D9 object\n");
1424         goto cleanup;
1425     }
1426
1427     ZeroMemory(&present_parameters, sizeof(present_parameters));
1428     present_parameters.Windowed = TRUE;
1429     present_parameters.hDeviceWindow = hwnd;
1430     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1431
1432     hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
1433             NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device);
1434     if (FAILED(hr) || !device)
1435     {
1436         skip("Failed to create device\n");
1437         goto cleanup;
1438     }
1439
1440     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
1441     ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (0x%08x)\n", hr);
1442     hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
1443     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
1444
1445     hr = IDirect3DDevice9_CreateVertexBuffer(device, sizeof(quad), 0, 0, D3DPOOL_DEFAULT, &vertex_buffer, NULL);
1446     ok(SUCCEEDED(hr), "CreateVertexBuffer failed (0x%08x)\n", hr);
1447     hr = IDirect3DVertexBuffer9_Lock(vertex_buffer, 0, 0, &ptr, D3DLOCK_DISCARD);
1448     ok(SUCCEEDED(hr), "Lock failed (0x%08x)\n", hr);
1449     memcpy(ptr, quad, sizeof(quad));
1450     hr = IDirect3DVertexBuffer9_Unlock(vertex_buffer);
1451     ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1452     hr = IDirect3DDevice9_SetStreamSource(device, 0, vertex_buffer, 0, sizeof(*quad));
1453     ok(SUCCEEDED(hr), "SetStreamSource failed (0x%08x)\n", hr);
1454
1455     hr = IDirect3DDevice9_CreateIndexBuffer(device, sizeof(indices), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &index_buffer, NULL);
1456     ok(SUCCEEDED(hr), "CreateIndexBuffer failed (0x%08x)\n", hr);
1457     hr = IDirect3DIndexBuffer9_Lock(index_buffer, 0, 0, &ptr, D3DLOCK_DISCARD);
1458     ok(SUCCEEDED(hr), "Lock failed (0x%08x)\n", hr);
1459     memcpy(ptr, indices, sizeof(indices));
1460     hr = IDirect3DIndexBuffer9_Unlock(index_buffer);
1461     ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1462     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1463     ok(SUCCEEDED(hr), "SetRenderState D3DRS_LIGHTING failed (0x%08x)\n", hr);
1464     hr = IDirect3DDevice9_BeginScene(device);
1465     ok(SUCCEEDED(hr), "BeginScene failed (0x%08x)\n", hr);
1466
1467     /* NULL index buffer. Should fail */
1468     hr = IDirect3DDevice9_SetIndices(device, NULL);
1469     ok(SUCCEEDED(hr), "SetIndices failed (0x%08x)\n", hr);
1470     hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1471             4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1472     ok(hr == D3DERR_INVALIDCALL, "DrawIndexedPrimitive returned 0x%08x, expected D3DERR_INVALIDCALL (0x%08x)\n",
1473             hr, D3DERR_INVALIDCALL);
1474
1475     /* Valid index buffer. Should succeed */
1476     hr = IDirect3DDevice9_SetIndices(device, index_buffer);
1477     ok(SUCCEEDED(hr), "SetIndices failed (0x%08x)\n", hr);
1478     hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1479             4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1480     ok(SUCCEEDED(hr), "DrawIndexedPrimitive failed (0x%08x)\n", hr);
1481
1482     hr = IDirect3DDevice9_EndScene(device);
1483     ok(SUCCEEDED(hr), "EndScene failed (0x%08x)\n", hr);
1484
1485     hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
1486     ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1487
1488     IDirect3DVertexBuffer9_Release(vertex_buffer);
1489     IDirect3DIndexBuffer9_Release(index_buffer);
1490     IDirect3DVertexDeclaration9_Release(vertex_declaration);
1491
1492 cleanup:
1493     if (d3d9) IDirect3D9_Release(d3d9);
1494     if (device) IDirect3DDevice9_Release(device);
1495     if (hwnd) DestroyWindow(hwnd);
1496 }
1497
1498 static void test_null_stream(void)
1499 {
1500     IDirect3DVertexBuffer9 *buffer = NULL;
1501     D3DPRESENT_PARAMETERS present_parameters;
1502     IDirect3DDevice9 *device = NULL;
1503     IDirect3D9 *d3d9;
1504     HWND hwnd;
1505     HRESULT hr;
1506     IDirect3DVertexShader9 *shader = NULL;
1507     IDirect3DVertexDeclaration9 *decl;
1508     DWORD shader_code[] = {
1509         0xfffe0101,                             /* vs_1_1           */
1510         0x0000001f, 0x80000000, 0x900f0000,     /* dcl_position v0  */
1511         0x00000001, 0xc00f0000, 0x90e40000,     /* mov oPos, v0     */
1512         0x0000ffff                              /* end              */
1513     };
1514     static const D3DVERTEXELEMENT9 decl_elements[] = {
1515         {0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1516         {1, 0,  D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,    D3DDECLUSAGE_COLOR, 0},
1517         D3DDECL_END()
1518     };
1519
1520     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1521     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1522     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1523     ok(hwnd != NULL, "Failed to create window\n");
1524     if (!d3d9 || !hwnd) goto cleanup;
1525
1526     ZeroMemory(&present_parameters, sizeof(present_parameters));
1527     present_parameters.Windowed = TRUE;
1528     present_parameters.hDeviceWindow = hwnd;
1529     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1530
1531     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1532                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device );
1533     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1534     if(!device)
1535     {
1536         skip("Failed to create a d3d device\n");
1537         goto cleanup;
1538     }
1539
1540     hr = IDirect3DDevice9_CreateVertexShader(device, shader_code, &shader);
1541     if(FAILED(hr)) {
1542         skip("No vertex shader support\n");
1543         goto cleanup;
1544     }
1545     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &decl);
1546     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexDeclaration failed (0x%08x)\n", hr);
1547     hr = IDirect3DDevice9_CreateVertexBuffer(device, 12 * sizeof(float), 0, 0, D3DPOOL_MANAGED, &buffer, NULL);
1548     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexBuffer failed (0x%08x)\n", hr);
1549
1550     hr = IDirect3DDevice9_SetStreamSource(device, 0, buffer, 0, sizeof(float) * 3);
1551     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetStreamSource failed (0x%08x)\n", hr);
1552     hr = IDirect3DDevice9_SetStreamSource(device, 1, NULL, 0, 0);
1553     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetStreamSource failed (0x%08x)\n", hr);
1554     hr = IDirect3DDevice9_SetVertexShader(device, shader);
1555     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetVertexShader failed (0x%08x)\n", hr);
1556     hr = IDirect3DDevice9_SetVertexDeclaration(device, decl);
1557     ok(SUCCEEDED(hr), "IDirect3DDevice9_SetVertexDeclaration failed (0x%08x)\n", hr);
1558
1559     hr = IDirect3DDevice9_BeginScene(device);
1560     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed (0x%08x)\n", hr);
1561     if(SUCCEEDED(hr)) {
1562         hr = IDirect3DDevice9_DrawPrimitive(device, D3DPT_POINTLIST, 0, 1);
1563         ok(SUCCEEDED(hr), "IDirect3DDevice9_DrawPrimitive failed (0x%08x)\n", hr);
1564
1565         hr = IDirect3DDevice9_EndScene(device);
1566         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed (0x%08x)\n", hr);
1567     }
1568
1569     IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
1570     IDirect3DDevice9_SetVertexShader(device, NULL);
1571     IDirect3DDevice9_SetVertexDeclaration(device, NULL);
1572
1573     cleanup:
1574     if(decl) IDirect3DVertexDeclaration9_Release(decl);
1575     if(shader) IDirect3DVertexShader9_Release(shader);
1576     if(device) IDirect3DDevice9_Release(device);
1577     if(d3d9) IDirect3D9_Release(d3d9);
1578 }
1579
1580 static inline const char *debug_d3dpool(D3DPOOL pool) {
1581     switch(pool) {
1582         case D3DPOOL_DEFAULT: return "D3DPOOL_DEFAULT";
1583         case D3DPOOL_SYSTEMMEM: return "D3DPOOL_SYSTEMMEM";
1584         case D3DPOOL_SCRATCH: return "D3DPOOL_SCRATCH";
1585         case D3DPOOL_MANAGED: return "D3DPOOL_MANAGED";
1586         default:
1587         return "unknown pool";
1588     }
1589 }
1590
1591 static void test_vertex_buffer_alignment(void)
1592 {
1593     IDirect3DVertexBuffer9 *buffer = NULL;
1594     D3DPRESENT_PARAMETERS present_parameters;
1595     IDirect3DDevice9 *device = NULL;
1596     IDirect3D9 *d3d9;
1597     HWND hwnd;
1598     HRESULT hr;
1599     D3DPOOL pools[] = {D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEM, D3DPOOL_SCRATCH, D3DPOOL_MANAGED};
1600     DWORD sizes[] = {1, 4, 16, 17, 32, 33, 64, 65, 1024, 1025, 1048576, 1048577};
1601     unsigned int i, j;
1602     void *data;
1603
1604     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1605     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1606     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1607     ok(hwnd != NULL, "Failed to create window\n");
1608     if (!d3d9 || !hwnd) goto cleanup;
1609
1610     ZeroMemory(&present_parameters, sizeof(present_parameters));
1611     present_parameters.Windowed = TRUE;
1612     present_parameters.hDeviceWindow = hwnd;
1613     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1614
1615     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1616                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device );
1617     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1618     if(!device)
1619     {
1620         skip("Failed to create a d3d device\n");
1621         goto cleanup;
1622     }
1623
1624     for(i = 0; i < (sizeof(sizes) / sizeof(sizes[0])); i++) {
1625         for(j = 0; j < (sizeof(pools) / sizeof(pools[0])); j++) {
1626             hr = IDirect3DDevice9_CreateVertexBuffer(device, sizes[i], 0, 0, pools[j], &buffer, NULL);
1627             if(pools[j] == D3DPOOL_SCRATCH) {
1628                 ok(hr == D3DERR_INVALIDCALL, "Creating a D3DPOOL_SCRATCH buffer returned (0x%08x)\n", hr);
1629             } else {
1630                 ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexBuffer failed (0x%08x). Pool = %s, size %d\n", hr,
1631                    debug_d3dpool(pools[j]), sizes[i]);
1632             }
1633             if(FAILED(hr)) continue;
1634
1635             hr = IDirect3DVertexBuffer9_Lock(buffer, 0, 0, (void **) &data, 0);
1636             ok(SUCCEEDED(hr), "IDirect3DVertexBuffer9_Lock failed (0x%08x)\n", hr);
1637             ok(((DWORD_PTR) data & 31) == 0, "Vertex buffer start address is not 32 byte aligned(size: %d, pool: %s, data: %p)\n",
1638                sizes[i], debug_d3dpool(pools[j]), data);
1639             hr = IDirect3DVertexBuffer9_Unlock(buffer);
1640             ok(SUCCEEDED(hr), "IDirect3DVertexBuffer9_Unlock failed (0x%08x)\n", hr);
1641
1642             if(buffer) IDirect3DVertexBuffer9_Release(buffer);
1643         }
1644     }
1645
1646     cleanup:
1647     if(d3d9) IDirect3D9_Release(d3d9);
1648 }
1649
1650 static void test_lights(void)
1651 {
1652     D3DPRESENT_PARAMETERS present_parameters;
1653     IDirect3DDevice9 *device = NULL;
1654     IDirect3D9 *d3d9;
1655     HWND hwnd;
1656     HRESULT hr;
1657     unsigned int i;
1658     BOOL enabled;
1659     D3DCAPS9 caps;
1660
1661     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1662     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1663     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1664     ok(hwnd != NULL, "Failed to create window\n");
1665     if (!d3d9 || !hwnd) goto cleanup;
1666
1667     ZeroMemory(&present_parameters, sizeof(present_parameters));
1668     present_parameters.Windowed = TRUE;
1669     present_parameters.hDeviceWindow = hwnd;
1670     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1671
1672     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1673                                   D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &present_parameters, &device );
1674     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1675     if(!device)
1676     {
1677         skip("Failed to create a d3d device\n");
1678         goto cleanup;
1679     }
1680
1681     memset(&caps, 0, sizeof(caps));
1682     hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1683     ok(hr == D3D_OK, "IDirect3DDevice9_GetDeviceCaps failed with %s\n", DXGetErrorString9(hr));
1684
1685     for(i = 1; i <= caps.MaxActiveLights; i++) {
1686         hr = IDirect3DDevice9_LightEnable(device, i, TRUE);
1687         ok(hr == D3D_OK, "Enabling light %u failed with %s\n", i, DXGetErrorString9(hr));
1688         hr = IDirect3DDevice9_GetLightEnable(device, i, &enabled);
1689         ok(hr == D3D_OK, "GetLightEnable on light %u failed with %s\n", i, DXGetErrorString9(hr));
1690         ok(enabled, "Light %d is %s\n", i, enabled ? "enabled" : "disabled");
1691     }
1692
1693     /* TODO: Test the rendering results in this situation */
1694     hr = IDirect3DDevice9_LightEnable(device, i + 1, TRUE);
1695     ok(hr == D3D_OK, "Enabling one light more than supported returned %s\n", DXGetErrorString9(hr));
1696     hr = IDirect3DDevice9_GetLightEnable(device, i + 1, &enabled);
1697     ok(hr == D3D_OK, "GetLightEnable on light %u failed with %s\n", i + 1, DXGetErrorString9(hr));
1698     ok(enabled, "Light %d is %s\n", i + 1, enabled ? "enabled" : "disabled");
1699     hr = IDirect3DDevice9_LightEnable(device, i + 1, FALSE);
1700     ok(hr == D3D_OK, "Disabling the additional returned %s\n", DXGetErrorString9(hr));
1701
1702     for(i = 1; i <= caps.MaxActiveLights; i++) {
1703         hr = IDirect3DDevice9_LightEnable(device, i, FALSE);
1704         ok(hr == D3D_OK, "Disabling light %u failed with %s\n", i, DXGetErrorString9(hr));
1705     }
1706
1707     cleanup:
1708     if(device) IDirect3DDevice9_Release(device);
1709     if(d3d9) IDirect3D9_Release(d3d9);
1710 }
1711
1712 static void test_set_stream_source(void)
1713 {
1714     D3DPRESENT_PARAMETERS present_parameters;
1715     IDirect3DDevice9 *device = NULL;
1716     IDirect3D9 *d3d9;
1717     HWND hwnd;
1718     HRESULT hr;
1719     IDirect3DVertexBuffer9 *pVertexBuffer = NULL;
1720
1721     d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1722     ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1723     hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1724     ok(hwnd != NULL, "Failed to create window\n");
1725     if (!d3d9 || !hwnd) goto cleanup;
1726
1727     ZeroMemory(&present_parameters, sizeof(present_parameters));
1728     present_parameters.Windowed = TRUE;
1729     present_parameters.hDeviceWindow = hwnd;
1730     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1731
1732     hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1733                                   D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
1734     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1735     if(!device)
1736     {
1737         hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd,
1738                                       D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
1739         ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1740         if(!device)
1741         {
1742             skip("Failed to create a d3d device\n");
1743             goto cleanup;
1744         }
1745     }
1746
1747     hr = IDirect3DDevice9_CreateVertexBuffer( device, 512, 0, 0, D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
1748     ok(hr == D3D_OK, "Failed to create a vertex buffer, hr = %s\n", DXGetErrorString9(hr));
1749     if (SUCCEEDED(hr)) {
1750         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 0, 32);
1751         ok(hr == D3D_OK, "Failed to set the stream source, offset 0, hr = %s\n",
1752            DXGetErrorString9(hr));
1753         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 1, 32);
1754         ok(hr == D3DERR_INVALIDCALL, "Unexpected result when setting the stream source, offset 1, hr = %s\n",
1755            DXGetErrorString9(hr));
1756         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 2, 32);
1757         ok(hr == D3DERR_INVALIDCALL, "Unexpected result when setting the stream source, offset 2, hr = %s\n",
1758            DXGetErrorString9(hr));
1759         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 3, 32);
1760         ok(hr == D3DERR_INVALIDCALL, "Unexpected result when setting the stream source, offset 3, hr = %s\n",
1761            DXGetErrorString9(hr));
1762         hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 4, 32);
1763         ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %s\n",
1764           DXGetErrorString9(hr));
1765     }
1766     /* Try to set the NULL buffer with an offset and stride 0 */
1767     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
1768     ok(hr == D3D_OK, "Failed to set the stream source, offset 0, hr = %s\n",
1769        DXGetErrorString9(hr));
1770     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 1, 0);
1771     ok(hr == D3DERR_INVALIDCALL, "Unexpected result when setting the stream source, offset 1, hr = %s\n",
1772        DXGetErrorString9(hr));
1773     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 2, 0);
1774     ok(hr == D3DERR_INVALIDCALL, "Unexpected result when setting the stream source, offset 2, hr = %s\n",
1775        DXGetErrorString9(hr));
1776     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 3, 0);
1777     ok(hr == D3DERR_INVALIDCALL, "Unexpected result when setting the stream source, offset 3, hr = %s\n",
1778        DXGetErrorString9(hr));
1779     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 4, 0);
1780     ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %s\n",
1781        DXGetErrorString9(hr));
1782
1783     hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
1784     ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %s\n", DXGetErrorString9(hr));
1785
1786     if(pVertexBuffer) IDirect3DDevice9_Release(pVertexBuffer);
1787 cleanup:
1788     if(device) IDirect3DDevice9_Release(device);
1789     if(d3d9) IDirect3D9_Release(d3d9);
1790 }
1791
1792 START_TEST(device)
1793 {
1794     HMODULE d3d9_handle = LoadLibraryA( "d3d9.dll" );
1795     if (!d3d9_handle)
1796     {
1797         skip("Could not load d3d9.dll\n");
1798         return;
1799     }
1800
1801     pDirect3DCreate9 = (void *)GetProcAddress( d3d9_handle, "Direct3DCreate9" );
1802     ok(pDirect3DCreate9 != NULL, "Failed to get address of Direct3DCreate9\n");
1803     if (pDirect3DCreate9)
1804     {
1805         test_display_modes();
1806         test_swapchain();
1807         test_refcount();
1808         test_mipmap_levels();
1809         test_cursor();
1810         test_reset();
1811         test_scene();
1812         test_limits();
1813         test_depthstenciltest();
1814         test_draw_indexed();
1815         test_null_stream();
1816         test_vertex_buffer_alignment();
1817         test_lights();
1818         test_set_stream_source();
1819     }
1820 }