d3d8: Release non implicit surfaces only if refcount is 0, with test.
[wine] / dlls / d3d8 / tests / device.c
1 /*
2  * Copyright (C) 2006 Vitaliy Margolen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define COBJMACROS
20 #include <d3d8.h>
21 #include <dxerr8.h>
22 #include "wine/test.h"
23
24 static IDirect3D8 *(WINAPI *pDirect3DCreate8)(UINT);
25
26 static int get_refcount(IUnknown *object)
27 {
28     IUnknown_AddRef( object );
29     return IUnknown_Release( object );
30 }
31
32 #define CHECK_CALL(r,c,d,rc) \
33     if (SUCCEEDED(r)) {\
34         int tmp1 = get_refcount( (IUnknown *)d ); \
35         int rc_new = rc; \
36         ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
37     } else {\
38         trace("%s failed: %s\n", c, DXGetErrorString8(r)); \
39     }
40
41 #define CHECK_RELEASE(obj,d,rc) \
42     if (obj) { \
43         int tmp1, rc_new = rc; \
44         IUnknown_Release( obj ); \
45         tmp1 = get_refcount( (IUnknown *)d ); \
46         ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
47     }
48
49 #define CHECK_REFCOUNT(obj,rc) \
50     { \
51         int rc_new = rc; \
52         int count = get_refcount( (IUnknown *)obj ); \
53         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
54     }
55
56 #define CHECK_RELEASE_REFCOUNT(obj,rc) \
57     { \
58         int rc_new = rc; \
59         int count = IUnknown_Release( (IUnknown *)obj ); \
60         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
61     }
62
63 #define CHECK_ADDREF_REFCOUNT(obj,rc) \
64     { \
65         int rc_new = rc; \
66         int count = IUnknown_AddRef( (IUnknown *)obj ); \
67         ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
68     }
69
70 #define CHECK_SURFACE_CONTAINER(obj,iid,expected) \
71     { \
72         void *container_ptr = (void *)0x1337c0d3; \
73         hr = IDirect3DSurface8_GetContainer(obj, &iid, &container_ptr); \
74         ok(SUCCEEDED(hr) && container_ptr == expected, "GetContainer returned: hr %#x, container_ptr %p. " \
75             "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, expected); \
76         if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr); \
77     }
78
79 static void check_mipmap_levels(
80     IDirect3DDevice8* device, 
81     int width, int height, int count) 
82 {
83
84     IDirect3DBaseTexture8* texture = NULL;
85     HRESULT hr = IDirect3DDevice8_CreateTexture( device, width, height, 0, 0, 
86         D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, (IDirect3DTexture8**) &texture );
87        
88     if (SUCCEEDED(hr)) {
89         DWORD levels = IDirect3DBaseTexture8_GetLevelCount(texture);
90         ok(levels == count, "Invalid level count. Expected %d got %u\n", count, levels);
91     } else 
92         trace("CreateTexture failed: %s\n", DXGetErrorString8(hr));
93
94     if (texture) IUnknown_Release( texture );
95 }
96
97 static void test_mipmap_levels(void)
98 {
99
100     HRESULT               hr;
101     HWND                  hwnd = NULL;
102
103     IDirect3D8            *pD3d = NULL;
104     IDirect3DDevice8      *pDevice = NULL;
105     D3DPRESENT_PARAMETERS d3dpp;
106     D3DDISPLAYMODE        d3ddm;
107  
108     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
109     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
110     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
111     ok(hwnd != NULL, "Failed to create window\n");
112     if (!pD3d || !hwnd) goto cleanup;
113
114     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
115     ZeroMemory( &d3dpp, sizeof(d3dpp) );
116     d3dpp.Windowed         = TRUE;
117     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
118     d3dpp.BackBufferFormat = d3ddm.Format;
119
120     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
121                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
122     ok(SUCCEEDED(hr), "Failed to create IDirect3D8Device (%s)\n", DXGetErrorString8(hr));
123     if (FAILED(hr)) goto cleanup;
124
125     check_mipmap_levels(pDevice, 32, 32, 6);
126     check_mipmap_levels(pDevice, 256, 1, 9);
127     check_mipmap_levels(pDevice, 1, 256, 9);
128     check_mipmap_levels(pDevice, 1, 1, 1);
129
130     cleanup:
131     if (pD3d)     IUnknown_Release( pD3d );
132     if (pDevice)  IUnknown_Release( pDevice );
133     DestroyWindow( hwnd );
134 }
135
136 static void test_swapchain(void)
137 {
138     HRESULT                      hr;
139     HWND                         hwnd               = NULL;
140     IDirect3D8                  *pD3d               = NULL;
141     IDirect3DDevice8            *pDevice            = NULL;
142     IDirect3DSwapChain8         *swapchain1         = NULL;
143     IDirect3DSwapChain8         *swapchain2         = NULL;
144     IDirect3DSwapChain8         *swapchain3         = NULL;
145     IDirect3DSurface8           *backbuffer         = NULL;
146     D3DPRESENT_PARAMETERS        d3dpp;
147     D3DDISPLAYMODE               d3ddm;
148
149     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
150     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
151     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
152     ok(hwnd != NULL, "Failed to create window\n");
153     if (!pD3d || !hwnd) goto cleanup;
154
155     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
156     ZeroMemory( &d3dpp, sizeof(d3dpp) );
157     d3dpp.Windowed         = TRUE;
158     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
159     d3dpp.BackBufferFormat = d3ddm.Format;
160     d3dpp.BackBufferCount  = 0;
161
162     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
163                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
164     ok(SUCCEEDED(hr), "Failed to create IDirect3D8Device (%s)\n", DXGetErrorString8(hr));
165     if (FAILED(hr)) goto cleanup;
166
167     /* Check if the back buffer count was modified */
168     ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
169
170     /* Create a bunch of swapchains */
171     d3dpp.BackBufferCount = 0;
172     hr = IDirect3DDevice8_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain1);
173     ok(SUCCEEDED(hr), "Failed to create a swapchain (%s)\n", DXGetErrorString8(hr));
174     ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
175
176     d3dpp.BackBufferCount  = 1;
177     hr = IDirect3DDevice8_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain2);
178     ok(SUCCEEDED(hr), "Failed to create a swapchain (%s)\n", DXGetErrorString8(hr));
179
180     d3dpp.BackBufferCount  = 2;
181     hr = IDirect3DDevice8_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain3);
182     ok(SUCCEEDED(hr), "Failed to create a swapchain (%s)\n", DXGetErrorString8(hr));
183     if(SUCCEEDED(hr)) {
184         /* Swapchain 3, created with backbuffercount 2 */
185         backbuffer = (void *) 0xdeadbeef;
186         hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 0, 0, &backbuffer);
187         ok(SUCCEEDED(hr), "Failed to get the 1st back buffer (%s)\n", DXGetErrorString8(hr));
188         ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
189         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
190
191         backbuffer = (void *) 0xdeadbeef;
192         hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 1, 0, &backbuffer);
193         ok(SUCCEEDED(hr), "Failed to get the 2nd back buffer (%s)\n", DXGetErrorString8(hr));
194         ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
195         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
196
197         backbuffer = (void *) 0xdeadbeef;
198         hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 2, 0, &backbuffer);
199         ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %s\n", DXGetErrorString8(hr));
200         ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
201         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
202
203         backbuffer = (void *) 0xdeadbeef;
204         hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 3, 0, &backbuffer);
205         ok(FAILED(hr), "Failed to get the back buffer (%s)\n", DXGetErrorString8(hr));
206         ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
207         if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
208     }
209
210     /* Check the back buffers of the swapchains */
211     /* Swapchain 1, created with backbuffercount 0 */
212     hr = IDirect3DSwapChain8_GetBackBuffer(swapchain1, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
213     ok(SUCCEEDED(hr), "Failed to get the back buffer (%s)\n", DXGetErrorString8(hr));
214     ok(backbuffer != NULL, "The back buffer is NULL (%s)\n", DXGetErrorString8(hr));
215     if(backbuffer) IDirect3DSurface8_Release(backbuffer);
216
217     backbuffer = (void *) 0xdeadbeef;
218     hr = IDirect3DSwapChain8_GetBackBuffer(swapchain1, 1, 0, &backbuffer);
219     ok(FAILED(hr), "Failed to get the back buffer (%s)\n", DXGetErrorString8(hr));
220     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
221     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
222
223     /* Swapchain 2 - created with backbuffercount 1 */
224     backbuffer = (void *) 0xdeadbeef;
225     hr = IDirect3DSwapChain8_GetBackBuffer(swapchain2, 0, 0, &backbuffer);
226     ok(SUCCEEDED(hr), "Failed to get the back buffer (%s)\n", DXGetErrorString8(hr));
227     ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
228     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
229
230     backbuffer = (void *) 0xdeadbeef;
231     hr = IDirect3DSwapChain8_GetBackBuffer(swapchain2, 1, 0, &backbuffer);
232     ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %s\n", DXGetErrorString8(hr));
233     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
234     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
235
236     backbuffer = (void *) 0xdeadbeef;
237     hr = IDirect3DSwapChain8_GetBackBuffer(swapchain2, 2, 0, &backbuffer);
238     ok(FAILED(hr), "Failed to get the back buffer (%s)\n", DXGetErrorString8(hr));
239     ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
240     if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
241
242     cleanup:
243     if(swapchain1) IDirect3DSwapChain8_Release(swapchain1);
244     if(swapchain2) IDirect3DSwapChain8_Release(swapchain2);
245     if(swapchain3) IDirect3DSwapChain8_Release(swapchain3);
246     if(pDevice) IDirect3DDevice8_Release(pDevice);
247     if(pD3d) IDirect3DDevice8_Release(pD3d);
248     DestroyWindow( hwnd );
249 }
250
251 static void test_refcount(void)
252 {
253     HRESULT                      hr;
254     HWND                         hwnd               = NULL;
255     IDirect3D8                  *pD3d               = NULL;
256     IDirect3DDevice8            *pDevice            = NULL;
257     IDirect3DVertexBuffer8      *pVertexBuffer      = NULL;
258     IDirect3DIndexBuffer8       *pIndexBuffer       = NULL;
259     DWORD                       dVertexShader       = -1;
260     DWORD                       dPixelShader        = -1;
261     IDirect3DCubeTexture8       *pCubeTexture       = NULL;
262     IDirect3DTexture8           *pTexture           = NULL;
263     IDirect3DVolumeTexture8     *pVolumeTexture     = NULL;
264     IDirect3DVolume8            *pVolumeLevel       = NULL;
265     IDirect3DSurface8           *pStencilSurface    = NULL;
266     IDirect3DSurface8           *pImageSurface      = NULL;
267     IDirect3DSurface8           *pRenderTarget      = NULL;
268     IDirect3DSurface8           *pRenderTarget2     = NULL;
269     IDirect3DSurface8           *pRenderTarget3     = NULL;
270     IDirect3DSurface8           *pTextureLevel      = NULL;
271     IDirect3DSurface8           *pBackBuffer        = NULL;
272     DWORD                       dStateBlock         = -1;
273     IDirect3DSwapChain8         *pSwapChain         = NULL;
274
275     D3DPRESENT_PARAMETERS        d3dpp;
276     D3DDISPLAYMODE               d3ddm;
277     int                          refcount = 0, tmp;
278
279     DWORD decl[] =
280     {
281         D3DVSD_STREAM(0),
282         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
283         D3DVSD_REG(D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR), /* D3DVSDE_DIFFUSE, Register v5 */
284         D3DVSD_END()
285     };
286     static DWORD simple_vs[] = {0xFFFE0101,             /* vs_1_1               */
287         0x00000009, 0xC0010000, 0x90E40000, 0xA0E40000, /* dp4 oPos.x, v0, c0   */
288         0x00000009, 0xC0020000, 0x90E40000, 0xA0E40001, /* dp4 oPos.y, v0, c1   */
289         0x00000009, 0xC0040000, 0x90E40000, 0xA0E40002, /* dp4 oPos.z, v0, c2   */
290         0x00000009, 0xC0080000, 0x90E40000, 0xA0E40003, /* dp4 oPos.w, v0, c3   */
291         0x0000FFFF};                                    /* END                  */
292     static DWORD simple_ps[] = {0xFFFF0101,                                     /* ps_1_1                       */
293         0x00000051, 0xA00F0001, 0x3F800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0  */
294         0x00000042, 0xB00F0000,                                                 /* tex t0                       */
295         0x00000008, 0x800F0000, 0xA0E40001, 0xA0E40000,                         /* dp3 r0, c1, c0               */
296         0x00000005, 0x800F0000, 0x90E40000, 0x80E40000,                         /* mul r0, v0, r0               */
297         0x00000005, 0x800F0000, 0xB0E40000, 0x80E40000,                         /* mul r0, t0, r0               */
298         0x0000FFFF};                                                            /* END                          */
299
300
301     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
302     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
303     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
304     ok(hwnd != NULL, "Failed to create window\n");
305     if (!pD3d || !hwnd) goto cleanup;
306
307     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
308     ZeroMemory( &d3dpp, sizeof(d3dpp) );
309     d3dpp.Windowed         = TRUE;
310     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
311     d3dpp.BackBufferFormat = d3ddm.Format;
312     d3dpp.EnableAutoDepthStencil = TRUE;
313     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
314
315     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
316                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
317     ok(SUCCEEDED(hr), "Failed to create IDirect3D8Device (%s)\n", DXGetErrorString8(hr));
318     if (FAILED(hr)) goto cleanup;
319
320     refcount = get_refcount( (IUnknown *)pDevice );
321     ok(refcount == 1, "Invalid device RefCount %d\n", refcount);
322
323     /**
324      * Check refcount of implicit surfaces. Findings:
325      *   - the container is the device
326      *   - they hold a refernce to the device
327      *   - they are created with a refcount of 0 (Get/Release returns orignial refcount)
328      *   - they are not freed if refcount reaches 0.
329      *   - the refcount is not forwarded to the container.
330      */
331     hr = IDirect3DDevice8_GetRenderTarget(pDevice, &pRenderTarget);
332     CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
333     if(pRenderTarget)
334     {
335         todo_wine CHECK_SURFACE_CONTAINER( pRenderTarget, IID_IDirect3DDevice8, pDevice);
336         CHECK_REFCOUNT( pRenderTarget, 1);
337
338         CHECK_ADDREF_REFCOUNT(pRenderTarget, 2);
339         CHECK_REFCOUNT(pDevice, refcount);
340         CHECK_RELEASE_REFCOUNT(pRenderTarget, 1);
341         CHECK_REFCOUNT(pDevice, refcount);
342
343         hr = IDirect3DDevice8_GetRenderTarget(pDevice, &pRenderTarget);
344         CHECK_CALL( hr, "GetRenderTarget", pDevice, refcount);
345         CHECK_REFCOUNT( pRenderTarget, 2);
346         CHECK_RELEASE_REFCOUNT( pRenderTarget, 1);
347         CHECK_RELEASE_REFCOUNT( pRenderTarget, 0);
348         CHECK_REFCOUNT( pDevice, --refcount);
349
350         /* The render target is released with the device, so AddRef with refcount=0 is fine here. */
351         CHECK_ADDREF_REFCOUNT(pRenderTarget, 1);
352         CHECK_REFCOUNT(pDevice, ++refcount);
353         CHECK_RELEASE_REFCOUNT(pRenderTarget, 0);
354         CHECK_REFCOUNT(pDevice, --refcount);
355     }
356
357     /* Render target and back buffer are identical. */
358     hr = IDirect3DDevice8_GetBackBuffer(pDevice, 0, 0, &pBackBuffer);
359     CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
360     if(pBackBuffer)
361     {
362         CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
363         ok(pRenderTarget == pBackBuffer, "RenderTarget=%p and BackBuffer=%p should be the same.\n",
364            pRenderTarget, pBackBuffer);
365         pBackBuffer = NULL;
366     }
367     CHECK_REFCOUNT( pDevice, --refcount);
368
369     hr = IDirect3DDevice8_GetDepthStencilSurface(pDevice, &pStencilSurface);
370     CHECK_CALL( hr, "GetDepthStencilSurface", pDevice, ++refcount);
371     if(pStencilSurface)
372     {
373         CHECK_SURFACE_CONTAINER( pStencilSurface, IID_IDirect3DDevice8, pDevice);
374         CHECK_REFCOUNT( pStencilSurface, 1);
375
376         CHECK_ADDREF_REFCOUNT(pStencilSurface, 2);
377         CHECK_REFCOUNT(pDevice, refcount);
378         CHECK_RELEASE_REFCOUNT(pStencilSurface, 1);
379         CHECK_REFCOUNT(pDevice, refcount);
380
381         CHECK_RELEASE_REFCOUNT( pStencilSurface, 0);
382         CHECK_REFCOUNT( pDevice, --refcount);
383
384         /* The stencil surface is released with the device, so AddRef with refcount=0 is fine here. */
385         CHECK_ADDREF_REFCOUNT(pStencilSurface, 1);
386         CHECK_REFCOUNT(pDevice, ++refcount);
387         CHECK_RELEASE_REFCOUNT(pStencilSurface, 0);
388         CHECK_REFCOUNT(pDevice, --refcount);
389         pStencilSurface = NULL;
390     }
391
392     /* Buffers */
393     hr = IDirect3DDevice8_CreateIndexBuffer( pDevice, 16, 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &pIndexBuffer );
394     CHECK_CALL( hr, "CreateIndexBuffer", pDevice, ++refcount );
395     if(pIndexBuffer)
396     {
397         tmp = get_refcount( (IUnknown *)pIndexBuffer );
398
399         hr = IDirect3DDevice8_SetIndices(pDevice, pIndexBuffer, 0);
400         CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
401         hr = IDirect3DDevice8_SetIndices(pDevice, NULL, 0);
402         CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
403     }
404
405     hr = IDirect3DDevice8_CreateVertexBuffer( pDevice, 16, 0, D3DFVF_XYZ, D3DPOOL_DEFAULT, &pVertexBuffer );
406     CHECK_CALL( hr, "CreateVertexBuffer", pDevice, ++refcount );
407     if(pVertexBuffer)
408     {
409         tmp = get_refcount( (IUnknown *)pVertexBuffer );
410
411         hr = IDirect3DDevice8_SetStreamSource(pDevice, 0, pVertexBuffer, 3 * sizeof(float));
412         CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
413         hr = IDirect3DDevice8_SetStreamSource(pDevice, 0, NULL, 0);
414         CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
415     }
416     /* Shaders */
417     hr = IDirect3DDevice8_CreateVertexShader( pDevice, decl, simple_vs, &dVertexShader, 0 );
418     CHECK_CALL( hr, "CreateVertexShader", pDevice, refcount );
419     hr = IDirect3DDevice8_CreatePixelShader( pDevice, simple_ps, &dPixelShader );
420     CHECK_CALL( hr, "CreatePixelShader", pDevice, refcount );
421     /* Textures */
422     hr = IDirect3DDevice8_CreateTexture( pDevice, 32, 32, 3, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pTexture );
423     CHECK_CALL( hr, "CreateTexture", pDevice, ++refcount );
424     if (pTexture)
425     {
426         tmp = get_refcount( (IUnknown *)pTexture );
427
428         /* SetTexture should not increase refcounts */
429         hr = IDirect3DDevice8_SetTexture(pDevice, 0, (IDirect3DBaseTexture8 *) pTexture);
430         CHECK_CALL( hr, "SetTexture", pTexture, tmp);
431         hr = IDirect3DDevice8_SetTexture(pDevice, 0, NULL);
432         CHECK_CALL( hr, "SetTexture", pTexture, tmp);
433
434         /* This should not increment device refcount */
435         hr = IDirect3DTexture8_GetSurfaceLevel( pTexture, 1, &pTextureLevel );
436         CHECK_CALL( hr, "GetSurfaceLevel", pDevice, refcount );
437         /* But should increment texture's refcount */
438         CHECK_REFCOUNT( pTexture, tmp+1 );
439         /* Because the texture and surface refcount are identical */
440         if (pTextureLevel)
441         {
442             CHECK_REFCOUNT        ( pTextureLevel, tmp+1 );
443             CHECK_ADDREF_REFCOUNT ( pTextureLevel, tmp+2 );
444             CHECK_REFCOUNT        ( pTexture     , tmp+2 );
445             CHECK_RELEASE_REFCOUNT( pTextureLevel, tmp+1 );
446             CHECK_REFCOUNT        ( pTexture     , tmp+1 );
447             CHECK_RELEASE_REFCOUNT( pTexture     , tmp   );
448             CHECK_REFCOUNT        ( pTextureLevel, tmp   );
449         }
450     }
451     hr = IDirect3DDevice8_CreateCubeTexture( pDevice, 32, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pCubeTexture );
452     CHECK_CALL( hr, "CreateCubeTexture", pDevice, ++refcount );
453     hr = IDirect3DDevice8_CreateVolumeTexture( pDevice, 32, 32, 2, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pVolumeTexture );
454     CHECK_CALL( hr, "CreateVolumeTexture", pDevice, ++refcount );
455     if (pVolumeTexture)
456     {
457         tmp = get_refcount( (IUnknown *)pVolumeTexture );
458
459         /* This should not increment device refcount */
460         hr = IDirect3DVolumeTexture8_GetVolumeLevel(pVolumeTexture, 0, &pVolumeLevel);
461         CHECK_CALL( hr, "GetVolumeLevel", pDevice, refcount );
462         /* But should increment volume texture's refcount */
463         CHECK_REFCOUNT( pVolumeTexture, tmp+1 );
464         /* Because the volume texture and volume refcount are identical */
465         if (pVolumeLevel)
466         {
467             CHECK_REFCOUNT        ( pVolumeLevel  , tmp+1 );
468             CHECK_ADDREF_REFCOUNT ( pVolumeLevel  , tmp+2 );
469             CHECK_REFCOUNT        ( pVolumeTexture, tmp+2 );
470             CHECK_RELEASE_REFCOUNT( pVolumeLevel  , tmp+1 );
471             CHECK_REFCOUNT        ( pVolumeTexture, tmp+1 );
472             CHECK_RELEASE_REFCOUNT( pVolumeTexture, tmp   );
473             CHECK_REFCOUNT        ( pVolumeLevel  , tmp   );
474         }
475     }
476     /* Surfaces */
477     hr = IDirect3DDevice8_CreateDepthStencilSurface( pDevice, 32, 32, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, &pStencilSurface );
478     CHECK_CALL( hr, "CreateDepthStencilSurface", pDevice, ++refcount );
479     CHECK_REFCOUNT( pStencilSurface, 1);
480     hr = IDirect3DDevice8_CreateImageSurface( pDevice, 32, 32, D3DFMT_X8R8G8B8, &pImageSurface );
481     CHECK_CALL( hr, "CreateImageSurface", pDevice, ++refcount );
482     CHECK_REFCOUNT( pImageSurface, 1);
483     hr = IDirect3DDevice8_CreateRenderTarget( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, TRUE, &pRenderTarget3 );
484     CHECK_CALL( hr, "CreateRenderTarget", pDevice, ++refcount );
485     CHECK_REFCOUNT( pRenderTarget3, 1);
486     /* Misc */
487     hr = IDirect3DDevice8_CreateStateBlock( pDevice, D3DSBT_ALL, &dStateBlock );
488     CHECK_CALL( hr, "CreateStateBlock", pDevice, refcount );
489     hr = IDirect3DDevice8_CreateAdditionalSwapChain( pDevice, &d3dpp, &pSwapChain );
490     CHECK_CALL( hr, "CreateAdditionalSwapChain", pDevice, ++refcount );
491     if(pSwapChain)
492     {
493         /* check implicit back buffer */
494         hr = IDirect3DSwapChain8_GetBackBuffer(pSwapChain, 0, 0, &pBackBuffer);
495         CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
496         CHECK_REFCOUNT( pSwapChain, 1);
497         if(pBackBuffer)
498         {
499             todo_wine CHECK_SURFACE_CONTAINER( pBackBuffer, IID_IDirect3DDevice8, pDevice);
500             CHECK_REFCOUNT( pBackBuffer, 1);
501             CHECK_RELEASE_REFCOUNT( pBackBuffer, 0);
502             CHECK_REFCOUNT( pDevice, --refcount);
503
504             /* The back buffer is released with the swapchain, so AddRef with refcount=0 is fine here. */
505             CHECK_ADDREF_REFCOUNT(pBackBuffer, 1);
506             CHECK_REFCOUNT(pDevice, ++refcount);
507             CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
508             CHECK_REFCOUNT(pDevice, --refcount);
509             pBackBuffer = NULL;
510         }
511         CHECK_REFCOUNT( pSwapChain, 1);
512     }
513
514     if(pVertexBuffer)
515     {
516         BYTE *data;
517         /* Vertex buffers can be locked multiple times */
518         hr = IDirect3DVertexBuffer8_Lock(pVertexBuffer, 0, 0, &data, 0);
519         ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Lock failed with %08x\n", hr);
520         hr = IDirect3DVertexBuffer8_Lock(pVertexBuffer, 0, 0, &data, 0);
521         ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Lock failed with %08x\n", hr);
522         hr = IDirect3DVertexBuffer8_Unlock(pVertexBuffer);
523         ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Unlock failed with %08x\n", hr);
524         hr = IDirect3DVertexBuffer8_Unlock(pVertexBuffer);
525         ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Unlock failed with %08x\n", hr);
526     }
527
528     /* The implicit render target is not freed if refcount reaches 0.
529      * Otherwise GetRenderTarget would re-allocate it and the pointer would change.*/
530     hr = IDirect3DDevice8_GetRenderTarget(pDevice, &pRenderTarget2);
531     CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
532     if(pRenderTarget2)
533     {
534         CHECK_RELEASE_REFCOUNT(pRenderTarget2, 0);
535         ok(pRenderTarget == pRenderTarget2, "RenderTarget=%p and RenderTarget2=%p should be the same.\n",
536            pRenderTarget, pRenderTarget2);
537         CHECK_REFCOUNT( pDevice, --refcount);
538         pRenderTarget2 = NULL;
539     }
540     pRenderTarget = NULL;
541
542 cleanup:
543     CHECK_RELEASE(pDevice,              pDevice, --refcount);
544
545     /* Buffers */
546     CHECK_RELEASE(pVertexBuffer,        pDevice, --refcount);
547     CHECK_RELEASE(pIndexBuffer,         pDevice, --refcount);
548     /* Shaders */
549     if (dVertexShader != -1)  IDirect3DDevice8_DeleteVertexShader( pDevice, dVertexShader );
550     if (dPixelShader != -1)   IDirect3DDevice8_DeletePixelShader( pDevice, dPixelShader );
551     /* Textures */
552     CHECK_RELEASE(pTexture,             pDevice, --refcount);
553     CHECK_RELEASE(pCubeTexture,         pDevice, --refcount);
554     CHECK_RELEASE(pVolumeTexture,       pDevice, --refcount);
555     /* Surfaces */
556     CHECK_RELEASE(pStencilSurface,      pDevice, --refcount);
557     CHECK_RELEASE(pImageSurface,        pDevice, --refcount);
558     CHECK_RELEASE(pRenderTarget3,       pDevice, --refcount);
559     /* Misc */
560     if (dStateBlock != -1)    IDirect3DDevice8_DeleteStateBlock( pDevice, dStateBlock );
561     /* This will destroy device - cannot check the refcount here */
562     if (pSwapChain)           CHECK_RELEASE_REFCOUNT( pSwapChain, 0);
563
564     if (pD3d)                 CHECK_RELEASE_REFCOUNT( pD3d, 0);
565
566     DestroyWindow( hwnd );
567 }
568
569 static void test_cursor(void)
570 {
571     HRESULT                      hr;
572     HWND                         hwnd               = NULL;
573     IDirect3D8                  *pD3d               = NULL;
574     IDirect3DDevice8            *pDevice            = NULL;
575     D3DPRESENT_PARAMETERS        d3dpp;
576     D3DDISPLAYMODE               d3ddm;
577     CURSORINFO                   info;
578     IDirect3DSurface8 *cursor = NULL;
579     HCURSOR cur;
580
581     memset(&info, 0, sizeof(info));
582     info.cbSize = sizeof(info);
583     hr = GetCursorInfo(&info);
584     cur = info.hCursor;
585
586     pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
587     ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
588     hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
589     ok(hwnd != NULL, "Failed to create window\n");
590     if (!pD3d || !hwnd) goto cleanup;
591
592     IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
593     ZeroMemory( &d3dpp, sizeof(d3dpp) );
594     d3dpp.Windowed         = TRUE;
595     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
596     d3dpp.BackBufferFormat = d3ddm.Format;
597
598     hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
599                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
600     ok(SUCCEEDED(hr), "Failed to create IDirect3D8Device (%s)\n", DXGetErrorString8(hr));
601     if (FAILED(hr)) goto cleanup;
602
603     IDirect3DDevice8_CreateImageSurface(pDevice, 32, 32, D3DFMT_A8R8G8B8, &cursor);
604     ok(cursor != NULL, "IDirect3DDevice8_CreateOffscreenPlainSurface failed with %08x\n", hr);
605
606     /* Initially hidden */
607     hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
608     ok(hr == FALSE, "IDirect3DDevice8_ShowCursor returned %08x\n", hr);
609
610     /* Not enabled without a surface*/
611     hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
612     ok(hr == FALSE, "IDirect3DDevice8_ShowCursor returned %08x\n", hr);
613
614     /* Fails */
615     hr = IDirect3DDevice8_SetCursorProperties(pDevice, 0, 0, NULL);
616     ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_SetCursorProperties returned %08x\n", hr);
617
618     hr = IDirect3DDevice8_SetCursorProperties(pDevice, 0, 0, cursor);
619     ok(hr == D3D_OK, "IDirect3DDevice8_SetCursorProperties returned %08x\n", hr);
620
621     IDirect3DSurface8_Release(cursor);
622
623     memset(&info, 0, sizeof(info));
624     info.cbSize = sizeof(info);
625     hr = GetCursorInfo(&info);
626     ok(hr != 0, "GetCursorInfo returned %08x\n", hr);
627     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
628     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
629
630     /* Still hidden */
631     hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
632     ok(hr == FALSE, "IDirect3DDevice8_ShowCursor returned %08x\n", hr);
633
634     /* Enabled now*/
635     hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
636     ok(hr == TRUE, "IDirect3DDevice8_ShowCursor returned %08x\n", hr);
637
638     /* GDI cursor unchanged */
639     memset(&info, 0, sizeof(info));
640     info.cbSize = sizeof(info);
641     hr = GetCursorInfo(&info);
642     ok(hr != 0, "GetCursorInfo returned %08x\n", hr);
643     ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
644     ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
645
646 cleanup:
647     if(pD3d) IDirect3D8_Release(pD3d);
648     if(pDevice) IDirect3D8_Release(pDevice);
649 }
650
651 START_TEST(device)
652 {
653     HMODULE d3d8_handle = LoadLibraryA( "d3d8.dll" );
654
655     pDirect3DCreate8 = (void *)GetProcAddress( d3d8_handle, "Direct3DCreate8" );
656     if (pDirect3DCreate8)
657     {
658         test_swapchain();
659         test_refcount();
660         test_mipmap_levels();
661         test_cursor();
662     }
663 }