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