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