Assorted spelling fixes.
[wine/multimedia.git] / dlls / d3d9 / tests / device.c
blob0d2f53b807a1943548c250e851b00d1a2644ff6a
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
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.
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.
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
22 #define COBJMACROS
23 #include <d3d9.h>
24 #include <dxerr9.h>
25 #include "wine/test.h"
27 static IDirect3D9 *(WINAPI *pDirect3DCreate9)(UINT);
29 static int get_refcount(IUnknown *object)
31 IUnknown_AddRef( object );
32 return IUnknown_Release( object );
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)); \
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); \
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); \
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); \
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); \
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); \
82 static void check_mipmap_levels(
83 IDirect3DDevice9* device,
84 int width, int height, int count)
87 IDirect3DBaseTexture9* texture = NULL;
88 HRESULT hr = IDirect3DDevice9_CreateTexture( device, width, height, 0, 0,
89 D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, (IDirect3DTexture9**) &texture, NULL );
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));
97 if (texture) IUnknown_Release( texture );
100 static void test_mipmap_levels(void)
103 HRESULT hr;
104 HWND hwnd = NULL;
106 IDirect3D9 *pD3d = NULL;
107 IDirect3DDevice9 *pDevice = NULL;
108 D3DPRESENT_PARAMETERS d3dpp;
109 D3DDISPLAYMODE d3ddm;
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;
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;
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;
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);
133 cleanup:
134 if (pD3d) IUnknown_Release( pD3d );
135 if (pDevice) IUnknown_Release( pDevice );
136 DestroyWindow( hwnd );
139 static void test_swapchain(void)
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;
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;
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;
167 hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
168 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
169 ok(SUCCEEDED(hr), "Failed to create IDirect3D9Device (%s)\n", DXGetErrorString9(hr));
170 if (FAILED(hr)) goto cleanup;
172 /* Check if the back buffer count was modified */
173 ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
175 /* Get the implicit swapchain */
176 hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &swapchain0);
177 ok(SUCCEEDED(hr), "Failed to get the impicit swapchain (%s)\n", DXGetErrorString9(hr));
178 if(swapchain0) IDirect3DSwapChain9_Release(swapchain0);
180 /* Check if there is a back buffer */
181 hr = IDirect3DSwapChain9_GetBackBuffer(swapchain0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
182 ok(SUCCEEDED(hr), "Failed to get the back buffer (%s)\n", DXGetErrorString9(hr));
183 ok(backbuffer != NULL, "The back buffer is NULL\n");
184 if(backbuffer) IDirect3DSurface9_Release(backbuffer);
186 /* Try to get a nonexistent swapchain */
187 hr = IDirect3DDevice9_GetSwapChain(pDevice, 1, &swapchainX);
188 ok(hr == D3DERR_INVALIDCALL, "GetSwapChain on an nonexistent swapchain returned (%s)\n", DXGetErrorString9(hr));
189 ok(swapchainX == NULL, "Swapchain 1 is %p\n", swapchainX);
190 if(swapchainX) IDirect3DSwapChain9_Release(swapchainX);
192 /* Create a bunch of swapchains */
193 d3dpp.BackBufferCount = 0;
194 hr = IDirect3DDevice9_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain1);
195 ok(SUCCEEDED(hr), "Failed to create a swapchain (%s)\n", DXGetErrorString9(hr));
196 ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
198 d3dpp.BackBufferCount = 1;
199 hr = IDirect3DDevice9_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain2);
200 ok(SUCCEEDED(hr), "Failed to create a swapchain (%s)\n", DXGetErrorString9(hr));
202 d3dpp.BackBufferCount = 2;
203 hr = IDirect3DDevice9_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain3);
204 ok(SUCCEEDED(hr), "Failed to create a swapchain (%s)\n", DXGetErrorString9(hr));
205 if(SUCCEEDED(hr)) {
206 /* Swapchain 3, created with backbuffercount 2 */
207 backbuffer = (void *) 0xdeadbeef;
208 hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 0, 0, &backbuffer);
209 ok(SUCCEEDED(hr), "Failed to get the 1st back buffer (%s)\n", DXGetErrorString9(hr));
210 ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
211 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
213 backbuffer = (void *) 0xdeadbeef;
214 hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 1, 0, &backbuffer);
215 ok(SUCCEEDED(hr), "Failed to get the 2nd back buffer (%s)\n", DXGetErrorString9(hr));
216 ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
217 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
219 backbuffer = (void *) 0xdeadbeef;
220 hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 2, 0, &backbuffer);
221 ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %s\n", DXGetErrorString9(hr));
222 ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
223 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
225 backbuffer = (void *) 0xdeadbeef;
226 hr = IDirect3DSwapChain9_GetBackBuffer(swapchain3, 3, 0, &backbuffer);
227 ok(FAILED(hr), "Failed to get the back buffer (%s)\n", DXGetErrorString9(hr));
228 ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
229 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
232 /* Check the back buffers of the swapchains */
233 /* Swapchain 1, created with backbuffercount 0 */
234 hr = IDirect3DSwapChain9_GetBackBuffer(swapchain1, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
235 ok(SUCCEEDED(hr), "Failed to get the back buffer (%s)\n", DXGetErrorString9(hr));
236 ok(backbuffer != NULL, "The back buffer is NULL (%s)\n", DXGetErrorString9(hr));
237 if(backbuffer) IDirect3DSurface9_Release(backbuffer);
239 backbuffer = (void *) 0xdeadbeef;
240 hr = IDirect3DSwapChain9_GetBackBuffer(swapchain1, 1, 0, &backbuffer);
241 ok(FAILED(hr), "Failed to get the back buffer (%s)\n", DXGetErrorString9(hr));
242 ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
243 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
245 /* Swapchain 2 - created with backbuffercount 1 */
246 backbuffer = (void *) 0xdeadbeef;
247 hr = IDirect3DSwapChain9_GetBackBuffer(swapchain2, 0, 0, &backbuffer);
248 ok(SUCCEEDED(hr), "Failed to get the back buffer (%s)\n", DXGetErrorString9(hr));
249 ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
250 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
252 backbuffer = (void *) 0xdeadbeef;
253 hr = IDirect3DSwapChain9_GetBackBuffer(swapchain2, 1, 0, &backbuffer);
254 ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %s\n", DXGetErrorString9(hr));
255 ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
256 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
258 backbuffer = (void *) 0xdeadbeef;
259 hr = IDirect3DSwapChain9_GetBackBuffer(swapchain2, 2, 0, &backbuffer);
260 ok(FAILED(hr), "Failed to get the back buffer (%s)\n", DXGetErrorString9(hr));
261 ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
262 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface9_Release(backbuffer);
264 /* Try getSwapChain on a manually created swapchain
265 * it should fail, apparently GetSwapChain only returns implicit swapchains
267 swapchainX = (void *) 0xdeadbeef;
268 hr = IDirect3DDevice9_GetSwapChain(pDevice, 1, &swapchainX);
269 ok(hr == D3DERR_INVALIDCALL, "Failed to get the second swapchain (%s)\n", DXGetErrorString9(hr));
270 ok(swapchainX == NULL, "The swapchain pointer is %p\n", swapchainX);
271 if(swapchainX && swapchainX != (void *) 0xdeadbeef ) IDirect3DSwapChain9_Release(swapchainX);
273 cleanup:
274 if(swapchain1) IDirect3DSwapChain9_Release(swapchain1);
275 if(swapchain2) IDirect3DSwapChain9_Release(swapchain2);
276 if(swapchain3) IDirect3DSwapChain9_Release(swapchain3);
277 if(pDevice) IDirect3DDevice9_Release(pDevice);
278 if(pD3d) IDirect3DDevice9_Release(pD3d);
279 DestroyWindow( hwnd );
282 /* Shared between two functions */
283 static const DWORD simple_vs[] = {0xFFFE0101, /* vs_1_1 */
284 0x0000001F, 0x80000000, 0x900F0000, /* dcl_position0 v0 */
285 0x00000009, 0xC0010000, 0x90E40000, 0xA0E40000, /* dp4 oPos.x, v0, c0 */
286 0x00000009, 0xC0020000, 0x90E40000, 0xA0E40001, /* dp4 oPos.y, v0, c1 */
287 0x00000009, 0xC0040000, 0x90E40000, 0xA0E40002, /* dp4 oPos.z, v0, c2 */
288 0x00000009, 0xC0080000, 0x90E40000, 0xA0E40003, /* dp4 oPos.w, v0, c3 */
289 0x0000FFFF}; /* END */
291 static void test_refcount(void)
293 HRESULT hr;
294 HWND hwnd = NULL;
295 IDirect3D9 *pD3d = NULL;
296 IDirect3DDevice9 *pDevice = NULL;
297 IDirect3DVertexBuffer9 *pVertexBuffer = NULL;
298 IDirect3DIndexBuffer9 *pIndexBuffer = NULL;
299 IDirect3DVertexDeclaration9 *pVertexDeclaration = NULL;
300 IDirect3DVertexShader9 *pVertexShader = NULL;
301 IDirect3DPixelShader9 *pPixelShader = NULL;
302 IDirect3DCubeTexture9 *pCubeTexture = NULL;
303 IDirect3DTexture9 *pTexture = NULL;
304 IDirect3DVolumeTexture9 *pVolumeTexture = NULL;
305 IDirect3DVolume9 *pVolumeLevel = NULL;
306 IDirect3DSurface9 *pStencilSurface = NULL;
307 IDirect3DSurface9 *pOffscreenSurface = NULL;
308 IDirect3DSurface9 *pRenderTarget = NULL;
309 IDirect3DSurface9 *pRenderTarget2 = NULL;
310 IDirect3DSurface9 *pRenderTarget3 = NULL;
311 IDirect3DSurface9 *pTextureLevel = NULL;
312 IDirect3DSurface9 *pBackBuffer = NULL;
313 IDirect3DStateBlock9 *pStateBlock = NULL;
314 IDirect3DStateBlock9 *pStateBlock1 = NULL;
315 IDirect3DSwapChain9 *pSwapChain = NULL;
316 IDirect3DQuery9 *pQuery = NULL;
317 D3DPRESENT_PARAMETERS d3dpp;
318 D3DDISPLAYMODE d3ddm;
319 int refcount = 0, tmp;
321 D3DVERTEXELEMENT9 decl[] =
323 D3DDECL_END()
325 static DWORD simple_ps[] = {0xFFFF0101, /* ps_1_1 */
326 0x00000051, 0xA00F0001, 0x3F800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0 */
327 0x00000042, 0xB00F0000, /* tex t0 */
328 0x00000008, 0x800F0000, 0xA0E40001, 0xA0E40000, /* dp3 r0, c1, c0 */
329 0x00000005, 0x800F0000, 0x90E40000, 0x80E40000, /* mul r0, v0, r0 */
330 0x00000005, 0x800F0000, 0xB0E40000, 0x80E40000, /* mul r0, t0, r0 */
331 0x0000FFFF}; /* END */
334 pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
335 ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
336 hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
337 ok(hwnd != NULL, "Failed to create window\n");
338 if (!pD3d || !hwnd) goto cleanup;
340 IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
341 ZeroMemory( &d3dpp, sizeof(d3dpp) );
342 d3dpp.Windowed = TRUE;
343 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
344 d3dpp.BackBufferFormat = d3ddm.Format;
345 d3dpp.EnableAutoDepthStencil = TRUE;
346 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
348 hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
349 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
350 ok(SUCCEEDED(hr), "Failed to create IDirect3D9Device (%s)\n", DXGetErrorString9(hr));
351 if (FAILED(hr)) goto cleanup;
353 refcount = get_refcount( (IUnknown *)pDevice );
354 ok(refcount == 1, "Invalid device RefCount %d\n", refcount);
357 * Check refcount of implicit surfaces and implicit swapchain. Findings:
358 * - the container is the device OR swapchain
359 * - they hold a refernce to the device
360 * - they are created with a refcount of 0 (Get/Release returns orignial refcount)
361 * - they are not freed if refcount reaches 0.
362 * - the refcount is not forwarded to the container.
364 hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &pSwapChain);
365 CHECK_CALL( hr, "GetSwapChain", pDevice, ++refcount);
366 if (pSwapChain)
368 CHECK_REFCOUNT( pSwapChain, 1);
370 hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget);
371 CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
372 CHECK_REFCOUNT( pSwapChain, 1);
373 if(pRenderTarget)
375 CHECK_SURFACE_CONTAINER( pRenderTarget, IID_IDirect3DSwapChain9, pSwapChain);
376 CHECK_REFCOUNT( pRenderTarget, 1);
378 CHECK_ADDREF_REFCOUNT(pRenderTarget, 2);
379 CHECK_REFCOUNT(pDevice, refcount);
380 CHECK_RELEASE_REFCOUNT(pRenderTarget, 1);
381 CHECK_REFCOUNT(pDevice, refcount);
383 hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget);
384 CHECK_CALL( hr, "GetRenderTarget", pDevice, refcount);
385 CHECK_REFCOUNT( pRenderTarget, 2);
386 CHECK_RELEASE_REFCOUNT( pRenderTarget, 1);
387 CHECK_RELEASE_REFCOUNT( pRenderTarget, 0);
388 CHECK_REFCOUNT( pDevice, --refcount);
390 /* The render target is released with the device, so AddRef with refcount=0 is fine here. */
391 CHECK_ADDREF_REFCOUNT(pRenderTarget, 1);
392 CHECK_REFCOUNT(pDevice, ++refcount);
393 CHECK_RELEASE_REFCOUNT(pRenderTarget, 0);
394 CHECK_REFCOUNT(pDevice, --refcount);
397 /* Render target and back buffer are identical. */
398 hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, 0, &pBackBuffer);
399 CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
400 if(pBackBuffer)
402 CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
403 ok(pRenderTarget == pBackBuffer, "RenderTarget=%p and BackBuffer=%p should be the same.\n",
404 pRenderTarget, pBackBuffer);
405 pBackBuffer = NULL;
407 CHECK_REFCOUNT( pDevice, --refcount);
409 hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pStencilSurface);
410 CHECK_CALL( hr, "GetDepthStencilSurface", pDevice, ++refcount);
411 CHECK_REFCOUNT( pSwapChain, 1);
412 if(pStencilSurface)
414 CHECK_SURFACE_CONTAINER( pStencilSurface, IID_IDirect3DDevice9, pDevice);
415 CHECK_REFCOUNT( pStencilSurface, 1);
417 CHECK_ADDREF_REFCOUNT(pStencilSurface, 2);
418 CHECK_REFCOUNT(pDevice, refcount);
419 CHECK_RELEASE_REFCOUNT(pStencilSurface, 1);
420 CHECK_REFCOUNT(pDevice, refcount);
422 CHECK_RELEASE_REFCOUNT( pStencilSurface, 0);
423 CHECK_REFCOUNT( pDevice, --refcount);
425 /* The stencil surface is released with the device, so AddRef with refcount=0 is fine here. */
426 CHECK_ADDREF_REFCOUNT(pStencilSurface, 1);
427 CHECK_REFCOUNT(pDevice, ++refcount);
428 CHECK_RELEASE_REFCOUNT(pStencilSurface, 0);
429 CHECK_REFCOUNT(pDevice, --refcount);
430 pStencilSurface = NULL;
433 CHECK_RELEASE_REFCOUNT( pSwapChain, 0);
434 CHECK_REFCOUNT( pDevice, --refcount);
436 /* The implicit swapchwin is released with the device, so AddRef with refcount=0 is fine here. */
437 CHECK_ADDREF_REFCOUNT(pSwapChain, 1);
438 CHECK_REFCOUNT(pDevice, ++refcount);
439 CHECK_RELEASE_REFCOUNT(pSwapChain, 0);
440 CHECK_REFCOUNT(pDevice, --refcount);
441 pSwapChain = NULL;
444 /* Buffers */
445 hr = IDirect3DDevice9_CreateIndexBuffer( pDevice, 16, 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &pIndexBuffer, NULL );
446 CHECK_CALL( hr, "CreateIndexBuffer", pDevice, ++refcount );
447 if(pIndexBuffer)
449 tmp = get_refcount( (IUnknown *)pIndexBuffer );
451 hr = IDirect3DDevice9_SetIndices(pDevice, pIndexBuffer);
452 CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
453 hr = IDirect3DDevice9_SetIndices(pDevice, NULL);
454 CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
457 hr = IDirect3DDevice9_CreateVertexBuffer( pDevice, 16, 0, D3DFVF_XYZ, D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
458 CHECK_CALL( hr, "CreateVertexBuffer", pDevice, ++refcount );
459 if(pVertexBuffer)
461 IDirect3DVertexBuffer9 *pVBuf = (void*)~0;
462 UINT offset = ~0;
463 UINT stride = ~0;
465 tmp = get_refcount( (IUnknown *)pVertexBuffer );
467 hr = IDirect3DDevice9_SetStreamSource(pDevice, 0, pVertexBuffer, 0, 3 * sizeof(float));
468 CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
469 hr = IDirect3DDevice9_SetStreamSource(pDevice, 0, NULL, 0, 0);
470 CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
472 hr = IDirect3DDevice9_GetStreamSource(pDevice, 0, &pVBuf, &offset, &stride);
473 ok(SUCCEEDED(hr), "GetStreamSource did not succeed with NULL stream!\n");
474 ok(pVBuf==NULL, "pVBuf not NULL (%p)!\n", pVBuf);
475 ok(stride==3*sizeof(float), "stride not 3 floats (got %u)!\n", stride);
476 ok(offset==0, "offset not 0 (got %u)!\n", offset);
478 /* Shaders */
479 hr = IDirect3DDevice9_CreateVertexDeclaration( pDevice, decl, &pVertexDeclaration );
480 CHECK_CALL( hr, "CreateVertexDeclaration", pDevice, ++refcount );
481 hr = IDirect3DDevice9_CreateVertexShader( pDevice, simple_vs, &pVertexShader );
482 CHECK_CALL( hr, "CreateVertexShader", pDevice, ++refcount );
483 hr = IDirect3DDevice9_CreatePixelShader( pDevice, simple_ps, &pPixelShader );
484 CHECK_CALL( hr, "CreatePixelShader", pDevice, ++refcount );
485 /* Textures */
486 hr = IDirect3DDevice9_CreateTexture( pDevice, 32, 32, 3, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pTexture, NULL );
487 CHECK_CALL( hr, "CreateTexture", pDevice, ++refcount );
488 if (pTexture)
490 tmp = get_refcount( (IUnknown *)pTexture );
492 /* SetTexture should not increase refcounts */
493 hr = IDirect3DDevice9_SetTexture(pDevice, 0, (IDirect3DBaseTexture9 *) pTexture);
494 CHECK_CALL( hr, "SetTexture", pTexture, tmp);
495 hr = IDirect3DDevice9_SetTexture(pDevice, 0, NULL);
496 CHECK_CALL( hr, "SetTexture", pTexture, tmp);
498 /* This should not increment device refcount */
499 hr = IDirect3DTexture9_GetSurfaceLevel( pTexture, 1, &pTextureLevel );
500 CHECK_CALL( hr, "GetSurfaceLevel", pDevice, refcount );
501 /* But should increment texture's refcount */
502 CHECK_REFCOUNT( pTexture, tmp+1 );
503 /* Because the texture and surface refcount are identical */
504 if (pTextureLevel)
506 CHECK_REFCOUNT ( pTextureLevel, tmp+1 );
507 CHECK_ADDREF_REFCOUNT ( pTextureLevel, tmp+2 );
508 CHECK_REFCOUNT ( pTexture , tmp+2 );
509 CHECK_RELEASE_REFCOUNT( pTextureLevel, tmp+1 );
510 CHECK_REFCOUNT ( pTexture , tmp+1 );
511 CHECK_RELEASE_REFCOUNT( pTexture , tmp );
512 CHECK_REFCOUNT ( pTextureLevel, tmp );
515 hr = IDirect3DDevice9_CreateCubeTexture( pDevice, 32, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pCubeTexture, NULL );
516 CHECK_CALL( hr, "CreateCubeTexture", pDevice, ++refcount );
517 hr = IDirect3DDevice9_CreateVolumeTexture( pDevice, 32, 32, 2, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pVolumeTexture, NULL );
518 CHECK_CALL( hr, "CreateVolumeTexture", pDevice, ++refcount );
519 if (pVolumeTexture)
521 tmp = get_refcount( (IUnknown *)pVolumeTexture );
523 /* This should not increment device refcount */
524 hr = IDirect3DVolumeTexture9_GetVolumeLevel(pVolumeTexture, 0, &pVolumeLevel);
525 CHECK_CALL( hr, "GetVolumeLevel", pDevice, refcount );
526 /* But should increment volume texture's refcount */
527 CHECK_REFCOUNT( pVolumeTexture, tmp+1 );
528 /* Because the volume texture and volume refcount are identical */
529 if (pVolumeLevel)
531 CHECK_REFCOUNT ( pVolumeLevel , tmp+1 );
532 CHECK_ADDREF_REFCOUNT ( pVolumeLevel , tmp+2 );
533 CHECK_REFCOUNT ( pVolumeTexture, tmp+2 );
534 CHECK_RELEASE_REFCOUNT( pVolumeLevel , tmp+1 );
535 CHECK_REFCOUNT ( pVolumeTexture, tmp+1 );
536 CHECK_RELEASE_REFCOUNT( pVolumeTexture, tmp );
537 CHECK_REFCOUNT ( pVolumeLevel , tmp );
540 /* Surfaces */
541 hr = IDirect3DDevice9_CreateDepthStencilSurface( pDevice, 32, 32, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, TRUE, &pStencilSurface, NULL );
542 CHECK_CALL( hr, "CreateDepthStencilSurface", pDevice, ++refcount );
543 CHECK_REFCOUNT( pStencilSurface, 1 );
544 hr = IDirect3DDevice9_CreateOffscreenPlainSurface( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pOffscreenSurface, NULL );
545 CHECK_CALL( hr, "CreateOffscreenPlainSurface", pDevice, ++refcount );
546 CHECK_REFCOUNT( pOffscreenSurface, 1 );
547 hr = IDirect3DDevice9_CreateRenderTarget( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &pRenderTarget3, NULL );
548 CHECK_CALL( hr, "CreateRenderTarget", pDevice, ++refcount );
549 CHECK_REFCOUNT( pRenderTarget3, 1 );
550 /* Misc */
551 hr = IDirect3DDevice9_CreateStateBlock( pDevice, D3DSBT_ALL, &pStateBlock );
552 CHECK_CALL( hr, "CreateStateBlock", pDevice, ++refcount );
553 hr = IDirect3DDevice9_CreateAdditionalSwapChain( pDevice, &d3dpp, &pSwapChain );
554 CHECK_CALL( hr, "CreateAdditionalSwapChain", pDevice, ++refcount );
555 if(pSwapChain)
557 /* check implicit back buffer */
558 hr = IDirect3DSwapChain9_GetBackBuffer(pSwapChain, 0, 0, &pBackBuffer);
559 CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
560 CHECK_REFCOUNT( pSwapChain, 1);
561 if(pBackBuffer)
563 CHECK_SURFACE_CONTAINER( pBackBuffer, IID_IDirect3DSwapChain9, pSwapChain);
564 CHECK_REFCOUNT( pBackBuffer, 1);
565 CHECK_RELEASE_REFCOUNT( pBackBuffer, 0);
566 CHECK_REFCOUNT( pDevice, --refcount);
568 /* The back buffer is released with the swapchain, so AddRef with refcount=0 is fine here. */
569 CHECK_ADDREF_REFCOUNT(pBackBuffer, 1);
570 CHECK_REFCOUNT(pDevice, ++refcount);
571 CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
572 CHECK_REFCOUNT(pDevice, --refcount);
573 pBackBuffer = NULL;
575 CHECK_REFCOUNT( pSwapChain, 1);
577 hr = IDirect3DDevice9_CreateQuery( pDevice, D3DQUERYTYPE_EVENT, &pQuery );
578 CHECK_CALL( hr, "CreateQuery", pDevice, ++refcount );
580 hr = IDirect3DDevice9_BeginStateBlock( pDevice );
581 CHECK_CALL( hr, "BeginStateBlock", pDevice, refcount );
582 hr = IDirect3DDevice9_EndStateBlock( pDevice, &pStateBlock1 );
583 CHECK_CALL( hr, "EndStateBlock", pDevice, ++refcount );
585 /* The implicit render target is not freed if refcount reaches 0.
586 * Otherwise GetRenderTarget would re-allocate it and the pointer would change.*/
587 hr = IDirect3DDevice9_GetRenderTarget(pDevice, 0, &pRenderTarget2);
588 CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
589 if(pRenderTarget2)
591 CHECK_RELEASE_REFCOUNT(pRenderTarget2, 0);
592 ok(pRenderTarget == pRenderTarget2, "RenderTarget=%p and RenderTarget2=%p should be the same.\n",
593 pRenderTarget, pRenderTarget2);
594 CHECK_REFCOUNT( pDevice, --refcount);
595 pRenderTarget2 = NULL;
597 pRenderTarget = NULL;
599 cleanup:
600 CHECK_RELEASE(pDevice, pDevice, --refcount);
602 /* Buffers */
603 CHECK_RELEASE(pVertexBuffer, pDevice, --refcount);
604 CHECK_RELEASE(pIndexBuffer, pDevice, --refcount);
605 /* Shaders */
606 CHECK_RELEASE(pVertexDeclaration, pDevice, --refcount);
607 CHECK_RELEASE(pVertexShader, pDevice, --refcount);
608 CHECK_RELEASE(pPixelShader, pDevice, --refcount);
609 /* Textures */
610 CHECK_RELEASE(pTextureLevel, pDevice, --refcount);
611 CHECK_RELEASE(pCubeTexture, pDevice, --refcount);
612 CHECK_RELEASE(pVolumeTexture, pDevice, --refcount);
613 /* Surfaces */
614 CHECK_RELEASE(pStencilSurface, pDevice, --refcount);
615 CHECK_RELEASE(pOffscreenSurface, pDevice, --refcount);
616 CHECK_RELEASE(pRenderTarget3, pDevice, --refcount);
617 /* Misc */
618 CHECK_RELEASE(pStateBlock, pDevice, --refcount);
619 CHECK_RELEASE(pSwapChain, pDevice, --refcount);
620 CHECK_RELEASE(pQuery, pDevice, --refcount);
621 /* This will destroy device - cannot check the refcount here */
622 if (pStateBlock1) CHECK_RELEASE_REFCOUNT( pStateBlock1, 0);
624 if (pD3d) CHECK_RELEASE_REFCOUNT( pD3d, 0);
626 DestroyWindow( hwnd );
629 static void test_cursor(void)
631 HRESULT hr;
632 HWND hwnd = NULL;
633 IDirect3D9 *pD3d = NULL;
634 IDirect3DDevice9 *pDevice = NULL;
635 D3DPRESENT_PARAMETERS d3dpp;
636 D3DDISPLAYMODE d3ddm;
637 CURSORINFO info;
638 IDirect3DSurface9 *cursor = NULL;
639 HCURSOR cur;
641 memset(&info, 0, sizeof(info));
642 info.cbSize = sizeof(info);
643 hr = GetCursorInfo(&info);
644 cur = info.hCursor;
646 pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
647 ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
648 hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
649 ok(hwnd != NULL, "Failed to create window\n");
650 if (!pD3d || !hwnd) goto cleanup;
652 IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
653 ZeroMemory( &d3dpp, sizeof(d3dpp) );
654 d3dpp.Windowed = TRUE;
655 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
656 d3dpp.BackBufferFormat = d3ddm.Format;
658 hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
659 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
660 ok(SUCCEEDED(hr), "Failed to create IDirect3D9Device (%s)\n", DXGetErrorString9(hr));
661 if (FAILED(hr)) goto cleanup;
663 IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 32, 32, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &cursor, 0);
664 ok(cursor != NULL, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %08x\n", hr);
666 /* Initially hidden */
667 hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
668 ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
670 /* Not enabled without a surface*/
671 hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
672 ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
674 /* Fails */
675 hr = IDirect3DDevice9_SetCursorProperties(pDevice, 0, 0, NULL);
676 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_SetCursorProperties returned %08x\n", hr);
678 hr = IDirect3DDevice9_SetCursorProperties(pDevice, 0, 0, cursor);
679 ok(hr == D3D_OK, "IDirect3DDevice9_SetCursorProperties returned %08x\n", hr);
681 IDirect3DSurface9_Release(cursor);
683 memset(&info, 0, sizeof(info));
684 info.cbSize = sizeof(info);
685 hr = GetCursorInfo(&info);
686 ok(hr != 0, "GetCursorInfo returned %08x\n", hr);
687 ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
688 ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
690 /* Still hidden */
691 hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
692 ok(hr == FALSE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
694 /* Enabled now*/
695 hr = IDirect3DDevice9_ShowCursor(pDevice, TRUE);
696 ok(hr == TRUE, "IDirect3DDevice9_ShowCursor returned %08x\n", hr);
698 /* GDI cursor unchanged */
699 memset(&info, 0, sizeof(info));
700 info.cbSize = sizeof(info);
701 hr = GetCursorInfo(&info);
702 ok(hr != 0, "GetCursorInfo returned %08x\n", hr);
703 ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
704 ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
706 cleanup:
707 if(pDevice) IDirect3D9_Release(pDevice);
708 if(pD3d) IDirect3D9_Release(pD3d);
709 DestroyWindow( hwnd );
712 static void test_reset(void)
714 HRESULT hr;
715 HWND hwnd = NULL;
716 IDirect3D9 *pD3d = NULL;
717 IDirect3DDevice9 *pDevice = NULL;
718 D3DPRESENT_PARAMETERS d3dpp;
719 D3DDISPLAYMODE d3ddm, d3ddm2;
720 D3DVIEWPORT9 vp;
721 DWORD width, orig_width = GetSystemMetrics(SM_CXSCREEN);
722 DWORD height, orig_height = GetSystemMetrics(SM_CYSCREEN);
723 IDirect3DSwapChain9 *pSwapchain;
724 IDirect3DSurface9 *surface;
725 IDirect3DTexture9 *texture;
726 IDirect3DVertexShader9 *shader;
727 BOOL support_800x600 = FALSE;
728 UINT i;
730 pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
731 ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
732 hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
733 ok(hwnd != NULL, "Failed to create window\n");
734 if (!pD3d || !hwnd) goto cleanup;
736 IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
737 ZeroMemory( &d3dpp, sizeof(d3dpp) );
738 d3dpp.Windowed = FALSE;
739 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
740 d3dpp.BackBufferWidth = 800;
741 d3dpp.BackBufferHeight = 600;
742 d3dpp.BackBufferFormat = d3ddm.Format;
744 for(i = 0; i < IDirect3D9_GetAdapterModeCount(pD3d, D3DADAPTER_DEFAULT, d3ddm.Format); i++) {
745 ZeroMemory( &d3ddm2, sizeof(d3ddm2) );
746 hr = IDirect3D9_EnumAdapterModes(pD3d, D3DADAPTER_DEFAULT, d3ddm.Format, i, &d3ddm2);
747 ok(hr == D3D_OK, "IDirect3D9Impl_EnumAdapterModes returned %#x\n", hr);
749 if(d3ddm2.Width == 800 && d3ddm2.Height == 600) {
750 support_800x600 = TRUE;
752 /* We use them as invalid modes */
753 if((d3ddm2.Width == 801 && d3ddm2.Height == 600) ||
754 (d3ddm2.Width == 32 && d3ddm2.Height == 32)) {
755 skip("This system supports a screen resolution of %dx%d, not running mode tests\n",
756 d3ddm2.Width, d3ddm2.Height);
757 goto cleanup;
760 if(!support_800x600) {
761 skip("Mode 800x600 not supported, skipping mode tests\n");
762 goto cleanup;
765 hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
766 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
768 if(FAILED(hr))
770 skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
771 goto cleanup;
773 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
774 ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after creation returned %#x\n", hr);
776 width = GetSystemMetrics(SM_CXSCREEN);
777 height = GetSystemMetrics(SM_CYSCREEN);
778 ok(width == 800, "Screen width is %d\n", width);
779 ok(height == 600, "Screen height is %d\n", height);
781 hr = IDirect3DDevice9_GetViewport(pDevice, &vp);
782 ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %s\n", DXGetErrorString9(hr));
783 if(SUCCEEDED(hr))
785 ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
786 ok(vp.Y == 0, "D3DVIEWPORT->X = %d\n", vp.Y);
787 ok(vp.Width == 800, "D3DVIEWPORT->X = %d\n", vp.Width);
788 ok(vp.Height == 600, "D3DVIEWPORT->X = %d\n", vp.Height);
789 ok(vp.MinZ == 0, "D3DVIEWPORT->X = %d\n", vp.Height);
790 ok(vp.MaxZ == 1, "D3DVIEWPORT->X = %d\n", vp.Height);
792 vp.X = 10;
793 vp.X = 20;
794 vp.MinZ = 2;
795 vp.MaxZ = 3;
796 hr = IDirect3DDevice9_SetViewport(pDevice, &vp);
797 ok(hr == D3D_OK, "IDirect3DDevice9_SetViewport failed with %s\n", DXGetErrorString9(hr));
799 ZeroMemory( &d3dpp, sizeof(d3dpp) );
800 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
801 d3dpp.Windowed = FALSE;
802 d3dpp.BackBufferWidth = 640;
803 d3dpp.BackBufferHeight = 480;
804 d3dpp.BackBufferFormat = d3ddm.Format;
805 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
806 ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
807 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
808 ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
810 ZeroMemory(&vp, sizeof(vp));
811 hr = IDirect3DDevice9_GetViewport(pDevice, &vp);
812 ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %s\n", DXGetErrorString9(hr));
813 if(SUCCEEDED(hr))
815 ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
816 ok(vp.Y == 0, "D3DVIEWPORT->X = %d\n", vp.Y);
817 ok(vp.Width == 640, "D3DVIEWPORT->X = %d\n", vp.Width);
818 ok(vp.Height == 480, "D3DVIEWPORT->X = %d\n", vp.Height);
819 ok(vp.MinZ == 0, "D3DVIEWPORT->X = %d\n", vp.Height);
820 ok(vp.MaxZ == 1, "D3DVIEWPORT->X = %d\n", vp.Height);
823 width = GetSystemMetrics(SM_CXSCREEN);
824 height = GetSystemMetrics(SM_CYSCREEN);
825 ok(width == 640, "Screen width is %d\n", width);
826 ok(height == 480, "Screen height is %d\n", height);
828 hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &pSwapchain);
829 ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %s\n", DXGetErrorString9(hr));
830 if(SUCCEEDED(hr))
832 ZeroMemory(&d3dpp, sizeof(d3dpp));
833 hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
834 ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %s\n", DXGetErrorString9(hr));
835 if(SUCCEEDED(hr))
837 ok(d3dpp.BackBufferWidth == 640, "Back buffer width is %d\n", d3dpp.BackBufferWidth);
838 ok(d3dpp.BackBufferHeight == 480, "Back buffer height is %d\n", d3dpp.BackBufferHeight);
840 IDirect3DSwapChain9_Release(pSwapchain);
843 ZeroMemory( &d3dpp, sizeof(d3dpp) );
844 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
845 d3dpp.Windowed = TRUE;
846 d3dpp.BackBufferWidth = 400;
847 d3dpp.BackBufferHeight = 300;
848 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
849 ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
850 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
851 ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
853 width = GetSystemMetrics(SM_CXSCREEN);
854 height = GetSystemMetrics(SM_CYSCREEN);
855 ok(width == orig_width, "Screen width is %d\n", width);
856 ok(height == orig_height, "Screen height is %d\n", height);
858 ZeroMemory(&vp, sizeof(vp));
859 hr = IDirect3DDevice9_GetViewport(pDevice, &vp);
860 ok(hr == D3D_OK, "IDirect3DDevice9_GetViewport failed with %s\n", DXGetErrorString9(hr));
861 if(SUCCEEDED(hr))
863 ok(vp.X == 0, "D3DVIEWPORT->X = %d\n", vp.X);
864 ok(vp.Y == 0, "D3DVIEWPORT->X = %d\n", vp.Y);
865 ok(vp.Width == 400, "D3DVIEWPORT->X = %d\n", vp.Width);
866 ok(vp.Height == 300, "D3DVIEWPORT->X = %d\n", vp.Height);
867 ok(vp.MinZ == 0, "D3DVIEWPORT->X = %d\n", vp.Height);
868 ok(vp.MaxZ == 1, "D3DVIEWPORT->X = %d\n", vp.Height);
871 hr = IDirect3DDevice9_GetSwapChain(pDevice, 0, &pSwapchain);
872 ok(hr == D3D_OK, "IDirect3DDevice9_GetSwapChain returned %s\n", DXGetErrorString9(hr));
873 if(SUCCEEDED(hr))
875 ZeroMemory(&d3dpp, sizeof(d3dpp));
876 hr = IDirect3DSwapChain9_GetPresentParameters(pSwapchain, &d3dpp);
877 ok(hr == D3D_OK, "IDirect3DSwapChain9_GetPresentParameters returned %s\n", DXGetErrorString9(hr));
878 if(SUCCEEDED(hr))
880 ok(d3dpp.BackBufferWidth == 400, "Back buffer width is %d\n", d3dpp.BackBufferWidth);
881 ok(d3dpp.BackBufferHeight == 300, "Back buffer height is %d\n", d3dpp.BackBufferHeight);
883 IDirect3DSwapChain9_Release(pSwapchain);
886 ZeroMemory( &d3dpp, sizeof(d3dpp) );
887 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
888 d3dpp.Windowed = TRUE;
889 d3dpp.BackBufferWidth = 400;
890 d3dpp.BackBufferHeight = 300;
892 /* _Reset fails if there is a resource in the default pool */
893 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 16, 16, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &surface, NULL);
894 ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %s\n", DXGetErrorString9(hr));
895 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
896 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
897 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
898 ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
899 IDirect3DSurface9_Release(surface);
900 /* Reset again to get the device out of the lost state */
901 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
902 ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
903 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
904 ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
906 /* Scratch, sysmem and managed pools are fine */
907 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SCRATCH, &surface, NULL);
908 ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %s\n", DXGetErrorString9(hr));
909 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
910 ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
911 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
912 ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
913 IDirect3DSurface9_Release(surface);
915 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 16, 16, D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface, NULL);
916 ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %s\n", DXGetErrorString9(hr));
917 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
918 ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
919 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
920 ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
921 IDirect3DSurface9_Release(surface);
923 hr = IDirect3DDevice9_CreateTexture(pDevice, 16, 16, 0, 0, D3DFMT_R5G6B5, D3DPOOL_MANAGED, &texture, NULL);
924 ok(hr == D3D_OK, "IDirect3DDevice9_CreateTexture returned %s\n", DXGetErrorString9(hr));
925 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
926 ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
927 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
928 ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
929 IDirect3DTexture9_Release(texture);
931 /* A reference held to an implicit surface causes failures as well */
932 hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, D3DBACKBUFFER_TYPE_MONO, &surface);
933 ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer returned %s\n", DXGetErrorString9(hr));
934 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
935 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
936 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
937 ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
938 IDirect3DSurface9_Release(surface);
939 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
940 ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
941 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
942 ok(hr == D3D_OK, "IDirect3DDevice9_TestCooperativeLevel after a successful reset returned %#x\n", hr);
944 /* Shaders are fine as well */
945 hr = IDirect3DDevice9_CreateVertexShader(pDevice, simple_vs, &shader);
946 ok(hr == D3D_OK, "IDirect3DDevice9_CreateVertexShader returned %s\n", DXGetErrorString9(hr));
947 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
948 ok(hr == D3D_OK, "IDirect3DDevice9_Reset failed with %s\n", DXGetErrorString9(hr));
949 IDirect3DVertexShader9_Release(shader);
951 /* Try setting invalid modes */
952 ZeroMemory( &d3dpp, sizeof(d3dpp) );
953 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
954 d3dpp.Windowed = FALSE;
955 d3dpp.BackBufferWidth = 32;
956 d3dpp.BackBufferHeight = 32;
957 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
958 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset to w=32, h=32, windowed=FALSE failed with %s\n", DXGetErrorString9(hr));
959 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
960 ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
962 ZeroMemory( &d3dpp, sizeof(d3dpp) );
963 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
964 d3dpp.Windowed = FALSE;
965 d3dpp.BackBufferWidth = 801;
966 d3dpp.BackBufferHeight = 600;
967 hr = IDirect3DDevice9_Reset(pDevice, &d3dpp);
968 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Reset to w=801, h=600, windowed=FALSE failed with %s\n", DXGetErrorString9(hr));
969 hr = IDirect3DDevice9_TestCooperativeLevel(pDevice);
970 ok(hr == D3DERR_DEVICENOTRESET, "IDirect3DDevice9_TestCooperativeLevel after a failed reset returned %#x\n", hr);
972 cleanup:
973 if(pD3d) IDirect3D9_Release(pD3d);
974 if(pDevice) IDirect3D9_Release(pDevice);
977 /* Test adapter display modes */
978 static void test_display_modes(void)
980 D3DDISPLAYMODE dmode;
981 IDirect3D9 *pD3d;
983 pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
984 ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
985 if(!pD3d) return;
987 #define TEST_FMT(x,r) do { \
988 HRESULT res = IDirect3D9_EnumAdapterModes(pD3d, 0, (x), 0, &dmode); \
989 ok(res==(r), "EnumAdapterModes("#x") did not return "#r" (got %s)!\n", DXGetErrorString9(res)); \
990 } while(0)
992 TEST_FMT(D3DFMT_R8G8B8, D3DERR_INVALIDCALL);
993 TEST_FMT(D3DFMT_A8R8G8B8, D3DERR_INVALIDCALL);
994 TEST_FMT(D3DFMT_X8B8G8R8, D3DERR_INVALIDCALL);
995 /* D3DFMT_R5G6B5 */
996 TEST_FMT(D3DFMT_X1R5G5B5, D3DERR_INVALIDCALL);
997 TEST_FMT(D3DFMT_A1R5G5B5, D3DERR_INVALIDCALL);
998 TEST_FMT(D3DFMT_A4R4G4B4, D3DERR_INVALIDCALL);
999 TEST_FMT(D3DFMT_R3G3B2, D3DERR_INVALIDCALL);
1000 TEST_FMT(D3DFMT_A8, D3DERR_INVALIDCALL);
1001 TEST_FMT(D3DFMT_A8R3G3B2, D3DERR_INVALIDCALL);
1002 TEST_FMT(D3DFMT_X4R4G4B4, D3DERR_INVALIDCALL);
1003 TEST_FMT(D3DFMT_A2B10G10R10, D3DERR_INVALIDCALL);
1004 TEST_FMT(D3DFMT_A8B8G8R8, D3DERR_INVALIDCALL);
1005 TEST_FMT(D3DFMT_X8B8G8R8, D3DERR_INVALIDCALL);
1006 TEST_FMT(D3DFMT_G16R16, D3DERR_INVALIDCALL);
1007 TEST_FMT(D3DFMT_A2R10G10B10, D3DERR_INVALIDCALL);
1008 TEST_FMT(D3DFMT_A16B16G16R16, D3DERR_INVALIDCALL);
1010 TEST_FMT(D3DFMT_A8P8, D3DERR_INVALIDCALL);
1011 TEST_FMT(D3DFMT_P8, D3DERR_INVALIDCALL);
1013 TEST_FMT(D3DFMT_L8, D3DERR_INVALIDCALL);
1014 TEST_FMT(D3DFMT_A8L8, D3DERR_INVALIDCALL);
1015 TEST_FMT(D3DFMT_A4L4, D3DERR_INVALIDCALL);
1017 TEST_FMT(D3DFMT_V8U8, D3DERR_INVALIDCALL);
1018 TEST_FMT(D3DFMT_L6V5U5, D3DERR_INVALIDCALL);
1019 TEST_FMT(D3DFMT_X8L8V8U8, D3DERR_INVALIDCALL);
1020 TEST_FMT(D3DFMT_Q8W8V8U8, D3DERR_INVALIDCALL);
1021 TEST_FMT(D3DFMT_V16U16, D3DERR_INVALIDCALL);
1022 TEST_FMT(D3DFMT_A2W10V10U10, D3DERR_INVALIDCALL);
1024 TEST_FMT(D3DFMT_UYVY, D3DERR_INVALIDCALL);
1025 TEST_FMT(D3DFMT_YUY2, D3DERR_INVALIDCALL);
1026 TEST_FMT(D3DFMT_DXT1, D3DERR_INVALIDCALL);
1027 TEST_FMT(D3DFMT_DXT2, D3DERR_INVALIDCALL);
1028 TEST_FMT(D3DFMT_DXT3, D3DERR_INVALIDCALL);
1029 TEST_FMT(D3DFMT_DXT4, D3DERR_INVALIDCALL);
1030 TEST_FMT(D3DFMT_DXT5, D3DERR_INVALIDCALL);
1031 TEST_FMT(D3DFMT_MULTI2_ARGB8, D3DERR_INVALIDCALL);
1032 TEST_FMT(D3DFMT_G8R8_G8B8, D3DERR_INVALIDCALL);
1033 TEST_FMT(D3DFMT_R8G8_B8G8, D3DERR_INVALIDCALL);
1035 TEST_FMT(D3DFMT_D16_LOCKABLE, D3DERR_INVALIDCALL);
1036 TEST_FMT(D3DFMT_D32, D3DERR_INVALIDCALL);
1037 TEST_FMT(D3DFMT_D15S1, D3DERR_INVALIDCALL);
1038 TEST_FMT(D3DFMT_D24S8, D3DERR_INVALIDCALL);
1039 TEST_FMT(D3DFMT_D24X8, D3DERR_INVALIDCALL);
1040 TEST_FMT(D3DFMT_D24X4S4, D3DERR_INVALIDCALL);
1041 TEST_FMT(D3DFMT_D16, D3DERR_INVALIDCALL);
1042 TEST_FMT(D3DFMT_L16, D3DERR_INVALIDCALL);
1043 TEST_FMT(D3DFMT_D32F_LOCKABLE, D3DERR_INVALIDCALL);
1044 TEST_FMT(D3DFMT_D24FS8, D3DERR_INVALIDCALL);
1046 TEST_FMT(D3DFMT_VERTEXDATA, D3DERR_INVALIDCALL);
1047 TEST_FMT(D3DFMT_INDEX16, D3DERR_INVALIDCALL);
1048 TEST_FMT(D3DFMT_INDEX32, D3DERR_INVALIDCALL);
1049 TEST_FMT(D3DFMT_Q16W16V16U16, D3DERR_INVALIDCALL);
1050 /* Floating point formats */
1051 TEST_FMT(D3DFMT_R16F, D3DERR_INVALIDCALL);
1052 TEST_FMT(D3DFMT_G16R16F, D3DERR_INVALIDCALL);
1053 TEST_FMT(D3DFMT_A16B16G16R16F, D3DERR_INVALIDCALL);
1055 /* IEEE formats */
1056 TEST_FMT(D3DFMT_R32F, D3DERR_INVALIDCALL);
1057 TEST_FMT(D3DFMT_G32R32F, D3DERR_INVALIDCALL);
1058 TEST_FMT(D3DFMT_A32B32G32R32F, D3DERR_INVALIDCALL);
1060 TEST_FMT(D3DFMT_CxV8U8, D3DERR_INVALIDCALL);
1062 TEST_FMT(0, D3DERR_INVALIDCALL);
1064 IDirect3D9_Release(pD3d);
1067 static void test_scene(void)
1069 HRESULT hr;
1070 HWND hwnd = NULL;
1071 IDirect3D9 *pD3d = NULL;
1072 IDirect3DDevice9 *pDevice = NULL;
1073 D3DPRESENT_PARAMETERS d3dpp;
1074 D3DDISPLAYMODE d3ddm;
1075 IDirect3DSurface9 *pSurface1 = NULL, *pSurface2 = NULL, *pSurface3 = NULL, *pRenderTarget = NULL;
1076 IDirect3DSurface9 *pBackBuffer = NULL, *pDepthStencil = NULL;
1077 RECT rect = {0, 0, 128, 128};
1078 D3DCAPS9 caps;
1080 pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1081 ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1082 hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1083 ok(hwnd != NULL, "Failed to create window\n");
1084 if (!pD3d || !hwnd) goto cleanup;
1086 IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1087 ZeroMemory( &d3dpp, sizeof(d3dpp) );
1088 d3dpp.Windowed = TRUE;
1089 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1090 d3dpp.BackBufferWidth = 800;
1091 d3dpp.BackBufferHeight = 600;
1092 d3dpp.BackBufferFormat = d3ddm.Format;
1093 d3dpp.EnableAutoDepthStencil = TRUE;
1094 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1096 hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1097 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1098 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1099 if(!pDevice)
1101 skip("Failed to create a d3d device\n");
1102 goto cleanup;
1105 /* Get the caps, they will be needed to tell if an operation is supposed to be valid */
1106 memset(&caps, 0, sizeof(caps));
1107 hr = IDirect3DDevice9_GetDeviceCaps(pDevice, &caps);
1108 ok(hr == D3D_OK, "IDirect3DDevice9_GetCaps failed with %s\n", DXGetErrorString9(hr));
1109 if(FAILED(hr)) goto cleanup;
1111 /* Test an EndScene without beginscene. Should return an error */
1112 hr = IDirect3DDevice9_EndScene(pDevice);
1113 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %s\n", DXGetErrorString9(hr));
1115 /* Test a normal BeginScene / EndScene pair, this should work */
1116 hr = IDirect3DDevice9_BeginScene(pDevice);
1117 ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
1118 if(SUCCEEDED(hr))
1120 hr = IDirect3DDevice9_EndScene(pDevice);
1121 ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
1124 /* Test another EndScene without having begun a new scene. Should return an error */
1125 hr = IDirect3DDevice9_EndScene(pDevice);
1126 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %s\n", DXGetErrorString9(hr));
1128 /* Two nested BeginScene and EndScene calls */
1129 hr = IDirect3DDevice9_BeginScene(pDevice);
1130 ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
1131 hr = IDirect3DDevice9_BeginScene(pDevice);
1132 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_BeginScene returned %s\n", DXGetErrorString9(hr));
1133 hr = IDirect3DDevice9_EndScene(pDevice);
1134 ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
1135 hr = IDirect3DDevice9_EndScene(pDevice);
1136 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_EndScene returned %s\n", DXGetErrorString9(hr));
1138 /* Create some surfaces to test stretchrect between the scenes */
1139 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface1, NULL);
1140 ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %s\n", DXGetErrorString9(hr));
1141 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(pDevice, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface2, NULL);
1142 ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface failed with %s\n", DXGetErrorString9(hr));
1143 hr = IDirect3DDevice9_CreateDepthStencilSurface(pDevice, 800, 600, D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, FALSE, &pSurface3, NULL);
1144 ok(hr == D3D_OK, "IDirect3DDevice9_CreateDepthStencilSurface failed with %s\n", DXGetErrorString9(hr));
1145 hr = IDirect3DDevice9_CreateRenderTarget(pDevice, 128, 128, d3ddm.Format, D3DMULTISAMPLE_NONE, 0, FALSE, &pRenderTarget, NULL);
1146 ok(hr == D3D_OK, "IDirect3DDevice9_CreateRenderTarget failed with %s\n", DXGetErrorString9(hr));
1148 hr = IDirect3DDevice9_GetBackBuffer(pDevice, 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
1149 ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer failed with %s\n", DXGetErrorString9(hr));
1150 hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1151 ok(hr == D3D_OK, "IDirect3DDevice9_GetBackBuffer failed with %s\n", DXGetErrorString9(hr));
1153 /* First make sure a simple StretchRect call works */
1154 if(pSurface1 && pSurface2) {
1155 hr = IDirect3DDevice9_StretchRect(pDevice, pSurface1, NULL, pSurface2, NULL, 0);
1156 ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %s\n", DXGetErrorString9(hr));
1158 if(pBackBuffer && pRenderTarget) {
1159 hr = IDirect3DDevice9_StretchRect(pDevice, pBackBuffer, &rect, pRenderTarget, NULL, 0);
1160 ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %s\n", DXGetErrorString9(hr));
1162 if(pDepthStencil && pSurface3) {
1163 HRESULT expected;
1164 if(0) /* Disabled for now because it crashes in wine */ {
1165 expected = caps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES ? D3D_OK : D3DERR_INVALIDCALL;
1166 hr = IDirect3DDevice9_StretchRect(pDevice, pDepthStencil, NULL, pSurface3, NULL, 0);
1167 ok( hr == expected, "IDirect3DDevice9_StretchRect returned %s, expected %s\n", DXGetErrorString9(hr), DXGetErrorString9(expected));
1171 /* Now try it in a BeginScene - EndScene pair. Seems to be allowed in a beginScene - Endscene pair
1172 * width normal surfaces, render targets and depth stencil surfaces.
1174 hr = IDirect3DDevice9_BeginScene(pDevice);
1175 ok( hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
1177 if(pSurface1 && pSurface2)
1179 hr = IDirect3DDevice9_StretchRect(pDevice, pSurface1, NULL, pSurface2, NULL, 0);
1180 ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %s\n", DXGetErrorString9(hr));
1182 if(pBackBuffer && pRenderTarget)
1184 hr = IDirect3DDevice9_StretchRect(pDevice, pBackBuffer, &rect, pRenderTarget, NULL, 0);
1185 ok( hr == D3D_OK, "IDirect3DDevice9_StretchRect failed with %s\n", DXGetErrorString9(hr));
1187 if(pDepthStencil && pSurface3)
1189 /* This is supposed to fail inside a BeginScene - EndScene pair. */
1190 hr = IDirect3DDevice9_StretchRect(pDevice, pDepthStencil, NULL, pSurface3, NULL, 0);
1191 ok( hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_StretchRect returned %s, expected D3DERR_INVALIDCALL\n", DXGetErrorString9(hr));
1194 hr = IDirect3DDevice9_EndScene(pDevice);
1195 ok( hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
1197 /* Does a SetRenderTarget influence BeginScene / EndScene ?
1198 * Set a new render target, then see if it started a new scene. Flip the rt back and see if that maybe
1199 * ended the scene. Expected result is that the scene is not affected by SetRenderTarget
1201 hr = IDirect3DDevice9_SetRenderTarget(pDevice, 0, pRenderTarget);
1202 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget failed with %s\n", DXGetErrorString9(hr));
1203 hr = IDirect3DDevice9_BeginScene(pDevice);
1204 ok( hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
1205 hr = IDirect3DDevice9_SetRenderTarget(pDevice, 0, pBackBuffer);
1206 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget failed with %s\n", DXGetErrorString9(hr));
1207 hr = IDirect3DDevice9_EndScene(pDevice);
1208 ok( hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
1210 cleanup:
1211 if(pRenderTarget) IDirect3DSurface9_Release(pRenderTarget);
1212 if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1213 if(pBackBuffer) IDirect3DSurface9_Release(pBackBuffer);
1214 if(pSurface1) IDirect3DSurface9_Release(pSurface1);
1215 if(pSurface2) IDirect3DSurface9_Release(pSurface2);
1216 if(pSurface3) IDirect3DSurface9_Release(pSurface3);
1217 if(pD3d) IDirect3D9_Release(pD3d);
1218 if(pDevice) IDirect3D9_Release(pDevice);
1219 if(hwnd) DestroyWindow(hwnd);
1222 static void test_limits(void)
1224 HRESULT hr;
1225 HWND hwnd = NULL;
1226 IDirect3D9 *pD3d = NULL;
1227 IDirect3DDevice9 *pDevice = NULL;
1228 D3DPRESENT_PARAMETERS d3dpp;
1229 D3DDISPLAYMODE d3ddm;
1230 IDirect3DTexture9 *pTexture = NULL;
1231 int i;
1233 pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1234 ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1235 hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1236 ok(hwnd != NULL, "Failed to create window\n");
1237 if (!pD3d || !hwnd) goto cleanup;
1239 IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1240 ZeroMemory( &d3dpp, sizeof(d3dpp) );
1241 d3dpp.Windowed = TRUE;
1242 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1243 d3dpp.BackBufferWidth = 800;
1244 d3dpp.BackBufferHeight = 600;
1245 d3dpp.BackBufferFormat = d3ddm.Format;
1246 d3dpp.EnableAutoDepthStencil = TRUE;
1247 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1249 hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1250 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1251 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1252 if(!pDevice)
1254 skip("Failed to create a d3d device\n");
1255 goto cleanup;
1258 hr = IDirect3DDevice9_CreateTexture(pDevice, 16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture, NULL);
1259 ok(hr == D3D_OK, "IDirect3DDevice9_CreateTexture failed with %s\n", DXGetErrorString9(hr));
1260 if(!pTexture) goto cleanup;
1262 /* There are 16 pixel samplers. We should be able to access all of them */
1263 for(i = 0; i < 16; i++) {
1264 hr = IDirect3DDevice9_SetTexture(pDevice, i, (IDirect3DBaseTexture9 *) pTexture);
1265 ok(hr == D3D_OK, "IDirect3DDevice9_SetTexture for sampler %d failed with %s\n", i, DXGetErrorString9(hr));
1266 hr = IDirect3DDevice9_SetTexture(pDevice, i, NULL);
1267 ok(hr == D3D_OK, "IDirect3DDevice9_SetTexture for sampler %d failed with %s\n", i, DXGetErrorString9(hr));
1268 hr = IDirect3DDevice9_SetSamplerState(pDevice, i, D3DSAMP_SRGBTEXTURE, TRUE);
1269 ok(hr == D3D_OK, "IDirect3DDevice9_SetSamplerState for sampler %d failed with %s\n", i, DXGetErrorString9(hr));
1272 /* Now test all 8 textures stage states */
1273 for(i = 0; i < 8; i++) {
1274 hr = IDirect3DDevice9_SetTextureStageState(pDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
1275 ok(hr == D3D_OK, "IDirect3DDevice9_SetTextureStageState for texture %d failed with %s\n", i, DXGetErrorString9(hr));
1278 /* Investigations show that accessing higher samplers / textures stage states does not return an error either. Writing
1279 * to too high samplers(approximately sampler 40) causes memory corruption in windows, so there is no bounds checking
1280 * but how do I test that?
1282 cleanup:
1283 if(pTexture) IDirect3DTexture9_Release(pTexture);
1284 if(pD3d) IDirect3D9_Release(pD3d);
1285 if(pDevice) IDirect3D9_Release(pDevice);
1286 if(hwnd) DestroyWindow(hwnd);
1289 static void test_depthstenciltest(void)
1291 HRESULT hr;
1292 HWND hwnd = NULL;
1293 IDirect3D9 *pD3d = NULL;
1294 IDirect3DDevice9 *pDevice = NULL;
1295 D3DPRESENT_PARAMETERS d3dpp;
1296 D3DDISPLAYMODE d3ddm;
1297 IDirect3DSurface9 *pDepthStencil = NULL;
1298 IDirect3DSurface9 *pDepthStencil2 = NULL;
1299 D3DZBUFFERTYPE state;
1301 pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
1302 ok(pD3d != NULL, "Failed to create IDirect3D9 object\n");
1303 hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1304 ok(hwnd != NULL, "Failed to create window\n");
1305 if (!pD3d || !hwnd) goto cleanup;
1307 IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1308 ZeroMemory( &d3dpp, sizeof(d3dpp) );
1309 d3dpp.Windowed = TRUE;
1310 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1311 d3dpp.BackBufferWidth = 800;
1312 d3dpp.BackBufferHeight = 600;
1313 d3dpp.BackBufferFormat = d3ddm.Format;
1314 d3dpp.EnableAutoDepthStencil = TRUE;
1315 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1317 hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1318 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1319 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1320 if(!pDevice)
1322 skip("Failed to create a d3d device\n");
1323 goto cleanup;
1326 hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1327 ok(hr == D3D_OK && pDepthStencil != NULL, "IDirect3DDevice9_GetDepthStencilSurface failed with %s\n", DXGetErrorString9(hr));
1329 /* Try to clear */
1330 hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1331 ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
1333 hr = IDirect3DDevice9_SetDepthStencilSurface(pDevice, NULL);
1334 ok(hr == D3D_OK, "IDirect3DDevice9_SetDepthStencilSurface failed with %s\n", DXGetErrorString9(hr));
1336 /* Check if the set buffer is returned on a get. WineD3D had a bug with that once, prevent it from coming back */
1337 hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil2);
1338 ok(hr == D3DERR_NOTFOUND && pDepthStencil2 == NULL, "IDirect3DDevice9_GetDepthStencilSurface failed with %s\n", DXGetErrorString9(hr));
1339 if(pDepthStencil2) IDirect3DSurface9_Release(pDepthStencil2);
1341 /* This left the render states untouched! */
1342 hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1343 ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1344 ok(state == D3DZB_TRUE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1345 hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZWRITEENABLE, &state);
1346 ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1347 ok(state == TRUE, "D3DRS_ZWRITEENABLE is %s\n", state ? "TRUE" : "FALSE");
1348 hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_STENCILENABLE, &state);
1349 ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1350 ok(state == FALSE, "D3DRS_STENCILENABLE is %s\n", state ? "TRUE" : "FALSE");
1351 hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_STENCILWRITEMASK, &state);
1352 ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1353 ok(state == 0xffffffff, "D3DRS_STENCILWRITEMASK is 0x%08x\n", state);
1355 /* This is supposed to fail now */
1356 hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1357 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
1359 hr = IDirect3DDevice9_SetRenderState(pDevice, D3DRS_ZENABLE, D3DZB_FALSE);
1360 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState failed with %s\n", DXGetErrorString9(hr));
1362 hr = IDirect3DDevice9_SetDepthStencilSurface(pDevice, pDepthStencil);
1363 ok(hr == D3D_OK, "IDirect3DDevice9_SetDepthStencilSurface failed with %s\n", DXGetErrorString9(hr));
1365 hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1366 ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1367 ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1369 /* Now it works again */
1370 hr = IDirect3DDevice9_Clear(pDevice, 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0, 0);
1371 ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
1373 if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1374 if(pDevice) IDirect3D9_Release(pDevice);
1376 /* Now see if autodepthstencil disable is honored. First, without a format set */
1377 ZeroMemory( &d3dpp, sizeof(d3dpp) );
1378 d3dpp.Windowed = TRUE;
1379 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1380 d3dpp.BackBufferWidth = 800;
1381 d3dpp.BackBufferHeight = 600;
1382 d3dpp.BackBufferFormat = d3ddm.Format;
1383 d3dpp.EnableAutoDepthStencil = FALSE;
1384 d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
1386 hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1387 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1388 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1389 if(!pDevice)
1391 skip("Failed to create a d3d device\n");
1392 goto cleanup;
1395 pDepthStencil = NULL;
1396 hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1397 ok(hr == D3DERR_NOTFOUND && pDepthStencil == NULL, "IDirect3DDevice9_GetDepthStencilSurface returned %s, surface = %p\n", DXGetErrorString9(hr), pDepthStencil);
1398 if(pDepthStencil) {
1399 IDirect3DSurface9_Release(pDepthStencil);
1400 pDepthStencil = NULL;
1403 /* Check the depth test state */
1404 hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1405 ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1406 ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1408 if(pDevice) IDirect3D9_Release(pDevice);
1410 /* Next, try EnableAutoDepthStencil FALSE with a depth stencil format set */
1411 ZeroMemory( &d3dpp, sizeof(d3dpp) );
1412 d3dpp.Windowed = TRUE;
1413 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1414 d3dpp.BackBufferWidth = 800;
1415 d3dpp.BackBufferHeight = 600;
1416 d3dpp.BackBufferFormat = d3ddm.Format;
1417 d3dpp.EnableAutoDepthStencil = FALSE;
1418 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1420 hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1421 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1422 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1423 if(!pDevice)
1425 skip("Failed to create a d3d device\n");
1426 goto cleanup;
1429 pDepthStencil = NULL;
1430 hr = IDirect3DDevice9_GetDepthStencilSurface(pDevice, &pDepthStencil);
1431 ok(hr == D3DERR_NOTFOUND && pDepthStencil == NULL, "IDirect3DDevice9_GetDepthStencilSurface returned %s, surface = %p\n", DXGetErrorString9(hr), pDepthStencil);
1432 if(pDepthStencil) {
1433 IDirect3DSurface9_Release(pDepthStencil);
1434 pDepthStencil = NULL;
1437 hr = IDirect3DDevice9_GetRenderState(pDevice, D3DRS_ZENABLE, &state);
1438 ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderState failed with %s\n", DXGetErrorString9(hr));
1439 ok(state == D3DZB_FALSE, "D3DRS_ZENABLE is %s\n", state == D3DZB_FALSE ? "D3DZB_FALSE" : (state == D3DZB_TRUE ? "D3DZB_TRUE" : "D3DZB_USEW"));
1441 cleanup:
1442 if(pDepthStencil) IDirect3DSurface9_Release(pDepthStencil);
1443 if(pD3d) IDirect3D9_Release(pD3d);
1444 if(pDevice) IDirect3D9_Release(pDevice);
1445 if(hwnd) DestroyWindow(hwnd);
1448 /* Test what happens when IDirect3DDevice9_DrawIndexedPrimitive is called without a valid index buffer set. */
1449 static void test_draw_indexed(void)
1451 static const struct {
1452 float position[3];
1453 DWORD color;
1454 } quad[] = {
1455 {{-1.0f, -1.0f, 0.0f}, 0xffff0000},
1456 {{-1.0f, 1.0f, 0.0f}, 0xffff0000},
1457 {{ 1.0f, 1.0f, 0.0f}, 0xffff0000},
1458 {{ 1.0f, -1.0f, 0.0f}, 0xffff0000},
1460 WORD indices[] = {0, 1, 2, 3, 0, 2};
1462 static const D3DVERTEXELEMENT9 decl_elements[] = {
1463 {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1464 {0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
1465 D3DDECL_END()
1468 IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
1469 IDirect3DVertexBuffer9 *vertex_buffer = NULL;
1470 IDirect3DIndexBuffer9 *index_buffer = NULL;
1471 D3DPRESENT_PARAMETERS present_parameters;
1472 IDirect3DDevice9 *device = NULL;
1473 IDirect3D9 *d3d9;
1474 HRESULT hr;
1475 HWND hwnd;
1476 void *ptr;
1478 hwnd = CreateWindow("static", "d3d9_test",
1479 0, 0, 0, 10, 10, 0, 0, 0, 0);
1480 if (!hwnd)
1482 skip("Failed to create window\n");
1483 return;
1486 d3d9 = pDirect3DCreate9(D3D_SDK_VERSION);
1487 if (!d3d9)
1489 skip("Failed to create IDirect3D9 object\n");
1490 goto cleanup;
1493 ZeroMemory(&present_parameters, sizeof(present_parameters));
1494 present_parameters.Windowed = TRUE;
1495 present_parameters.hDeviceWindow = hwnd;
1496 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1498 hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
1499 NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device);
1500 if (FAILED(hr) || !device)
1502 skip("Failed to create device\n");
1503 goto cleanup;
1506 hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
1507 ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (0x%08x)\n", hr);
1508 hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
1509 ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
1511 hr = IDirect3DDevice9_CreateVertexBuffer(device, sizeof(quad), 0, 0, D3DPOOL_DEFAULT, &vertex_buffer, NULL);
1512 ok(SUCCEEDED(hr), "CreateVertexBuffer failed (0x%08x)\n", hr);
1513 hr = IDirect3DVertexBuffer9_Lock(vertex_buffer, 0, 0, &ptr, D3DLOCK_DISCARD);
1514 ok(SUCCEEDED(hr), "Lock failed (0x%08x)\n", hr);
1515 memcpy(ptr, quad, sizeof(quad));
1516 hr = IDirect3DVertexBuffer9_Unlock(vertex_buffer);
1517 ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1518 hr = IDirect3DDevice9_SetStreamSource(device, 0, vertex_buffer, 0, sizeof(*quad));
1519 ok(SUCCEEDED(hr), "SetStreamSource failed (0x%08x)\n", hr);
1521 hr = IDirect3DDevice9_CreateIndexBuffer(device, sizeof(indices), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &index_buffer, NULL);
1522 ok(SUCCEEDED(hr), "CreateIndexBuffer failed (0x%08x)\n", hr);
1523 hr = IDirect3DIndexBuffer9_Lock(index_buffer, 0, 0, &ptr, D3DLOCK_DISCARD);
1524 ok(SUCCEEDED(hr), "Lock failed (0x%08x)\n", hr);
1525 memcpy(ptr, indices, sizeof(indices));
1526 hr = IDirect3DIndexBuffer9_Unlock(index_buffer);
1527 ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1528 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1529 ok(SUCCEEDED(hr), "SetRenderState D3DRS_LIGHTING failed (0x%08x)\n", hr);
1530 hr = IDirect3DDevice9_BeginScene(device);
1531 ok(SUCCEEDED(hr), "BeginScene failed (0x%08x)\n", hr);
1533 /* NULL index buffer. Should fail */
1534 hr = IDirect3DDevice9_SetIndices(device, NULL);
1535 ok(SUCCEEDED(hr), "SetIndices failed (0x%08x)\n", hr);
1536 hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1537 4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1538 ok(hr == D3DERR_INVALIDCALL, "DrawIndexedPrimitive returned 0x%08x, expected D3DERR_INVALIDCALL (0x%08x)\n",
1539 hr, D3DERR_INVALIDCALL);
1541 /* Valid index buffer. Should succeed */
1542 hr = IDirect3DDevice9_SetIndices(device, index_buffer);
1543 ok(SUCCEEDED(hr), "SetIndices failed (0x%08x)\n", hr);
1544 hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0 /* BaseVertexIndex */, 0 /* MinIndex */,
1545 4 /* NumVerts */, 0 /* StartIndex */, 2 /*PrimCount */);
1546 ok(SUCCEEDED(hr), "DrawIndexedPrimitive failed (0x%08x)\n", hr);
1548 hr = IDirect3DDevice9_EndScene(device);
1549 ok(SUCCEEDED(hr), "EndScene failed (0x%08x)\n", hr);
1551 hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
1552 ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1554 IDirect3DVertexBuffer9_Release(vertex_buffer);
1555 IDirect3DIndexBuffer9_Release(index_buffer);
1556 IDirect3DVertexDeclaration9_Release(vertex_declaration);
1558 cleanup:
1559 if (d3d9) IDirect3D9_Release(d3d9);
1560 if (device) IDirect3DDevice9_Release(device);
1561 if (hwnd) DestroyWindow(hwnd);
1564 static void test_null_stream(void)
1566 IDirect3DVertexBuffer9 *buffer = NULL;
1567 D3DPRESENT_PARAMETERS present_parameters;
1568 IDirect3DDevice9 *device = NULL;
1569 IDirect3D9 *d3d9;
1570 HWND hwnd;
1571 HRESULT hr;
1572 IDirect3DVertexShader9 *shader = NULL;
1573 IDirect3DVertexDeclaration9 *decl = NULL;
1574 DWORD shader_code[] = {
1575 0xfffe0101, /* vs_1_1 */
1576 0x0000001f, 0x80000000, 0x900f0000, /* dcl_position v0 */
1577 0x00000001, 0xc00f0000, 0x90e40000, /* mov oPos, v0 */
1578 0x0000ffff /* end */
1580 static const D3DVERTEXELEMENT9 decl_elements[] = {
1581 {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1582 {1, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
1583 D3DDECL_END()
1586 d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1587 ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1588 hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1589 ok(hwnd != NULL, "Failed to create window\n");
1590 if (!d3d9 || !hwnd) goto cleanup;
1592 ZeroMemory(&present_parameters, sizeof(present_parameters));
1593 present_parameters.Windowed = TRUE;
1594 present_parameters.hDeviceWindow = hwnd;
1595 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1597 hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1598 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device );
1599 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1600 if(!device)
1602 skip("Failed to create a d3d device\n");
1603 goto cleanup;
1606 hr = IDirect3DDevice9_CreateVertexShader(device, shader_code, &shader);
1607 if(FAILED(hr)) {
1608 skip("No vertex shader support\n");
1609 goto cleanup;
1611 hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &decl);
1612 ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexDeclaration failed (0x%08x)\n", hr);
1613 if (!SUCCEEDED(hr)) {
1614 skip("Vertex declaration handling not possible.\n");
1615 goto cleanup;
1617 hr = IDirect3DDevice9_CreateVertexBuffer(device, 12 * sizeof(float), 0, 0, D3DPOOL_MANAGED, &buffer, NULL);
1618 ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexBuffer failed (0x%08x)\n", hr);
1619 if (!SUCCEEDED(hr)) {
1620 skip("Vertex buffer handling not possible.\n");
1621 goto cleanup;
1624 hr = IDirect3DDevice9_SetStreamSource(device, 0, buffer, 0, sizeof(float) * 3);
1625 ok(SUCCEEDED(hr), "IDirect3DDevice9_SetStreamSource failed (0x%08x)\n", hr);
1626 hr = IDirect3DDevice9_SetStreamSource(device, 1, NULL, 0, 0);
1627 ok(SUCCEEDED(hr), "IDirect3DDevice9_SetStreamSource failed (0x%08x)\n", hr);
1628 hr = IDirect3DDevice9_SetVertexShader(device, shader);
1629 ok(SUCCEEDED(hr), "IDirect3DDevice9_SetVertexShader failed (0x%08x)\n", hr);
1630 hr = IDirect3DDevice9_SetVertexDeclaration(device, decl);
1631 ok(SUCCEEDED(hr), "IDirect3DDevice9_SetVertexDeclaration failed (0x%08x)\n", hr);
1633 hr = IDirect3DDevice9_BeginScene(device);
1634 ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed (0x%08x)\n", hr);
1635 if(SUCCEEDED(hr)) {
1636 hr = IDirect3DDevice9_DrawPrimitive(device, D3DPT_POINTLIST, 0, 1);
1637 ok(SUCCEEDED(hr), "IDirect3DDevice9_DrawPrimitive failed (0x%08x)\n", hr);
1639 hr = IDirect3DDevice9_EndScene(device);
1640 ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed (0x%08x)\n", hr);
1643 IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
1644 IDirect3DDevice9_SetVertexShader(device, NULL);
1645 IDirect3DDevice9_SetVertexDeclaration(device, NULL);
1647 cleanup:
1648 if(decl) IDirect3DVertexDeclaration9_Release(decl);
1649 if(shader) IDirect3DVertexShader9_Release(shader);
1650 if(device) IDirect3DDevice9_Release(device);
1651 if(d3d9) IDirect3D9_Release(d3d9);
1654 static inline const char *debug_d3dpool(D3DPOOL pool) {
1655 switch(pool) {
1656 case D3DPOOL_DEFAULT: return "D3DPOOL_DEFAULT";
1657 case D3DPOOL_SYSTEMMEM: return "D3DPOOL_SYSTEMMEM";
1658 case D3DPOOL_SCRATCH: return "D3DPOOL_SCRATCH";
1659 case D3DPOOL_MANAGED: return "D3DPOOL_MANAGED";
1660 default:
1661 return "unknown pool";
1665 static void test_vertex_buffer_alignment(void)
1667 IDirect3DVertexBuffer9 *buffer = NULL;
1668 D3DPRESENT_PARAMETERS present_parameters;
1669 IDirect3DDevice9 *device = NULL;
1670 IDirect3D9 *d3d9;
1671 HWND hwnd;
1672 HRESULT hr;
1673 D3DPOOL pools[] = {D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEM, D3DPOOL_SCRATCH, D3DPOOL_MANAGED};
1674 DWORD sizes[] = {1, 4, 16, 17, 32, 33, 64, 65, 1024, 1025, 1048576, 1048577};
1675 unsigned int i, j;
1676 void *data;
1678 d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1679 ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1680 hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1681 ok(hwnd != NULL, "Failed to create window\n");
1682 if (!d3d9 || !hwnd) goto cleanup;
1684 ZeroMemory(&present_parameters, sizeof(present_parameters));
1685 present_parameters.Windowed = TRUE;
1686 present_parameters.hDeviceWindow = hwnd;
1687 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1689 hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1690 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device );
1691 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1692 if(!device)
1694 skip("Failed to create a d3d device\n");
1695 goto cleanup;
1698 for(i = 0; i < (sizeof(sizes) / sizeof(sizes[0])); i++) {
1699 for(j = 0; j < (sizeof(pools) / sizeof(pools[0])); j++) {
1700 hr = IDirect3DDevice9_CreateVertexBuffer(device, sizes[i], 0, 0, pools[j], &buffer, NULL);
1701 if(pools[j] == D3DPOOL_SCRATCH) {
1702 ok(hr == D3DERR_INVALIDCALL, "Creating a D3DPOOL_SCRATCH buffer returned (0x%08x)\n", hr);
1703 } else {
1704 ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexBuffer failed (0x%08x). Pool = %s, size %d\n", hr,
1705 debug_d3dpool(pools[j]), sizes[i]);
1707 if(FAILED(hr)) continue;
1709 hr = IDirect3DVertexBuffer9_Lock(buffer, 0, 0, (void **) &data, 0);
1710 ok(SUCCEEDED(hr), "IDirect3DVertexBuffer9_Lock failed (0x%08x)\n", hr);
1711 ok(((DWORD_PTR) data & 31) == 0, "Vertex buffer start address is not 32 byte aligned(size: %d, pool: %s, data: %p)\n",
1712 sizes[i], debug_d3dpool(pools[j]), data);
1713 hr = IDirect3DVertexBuffer9_Unlock(buffer);
1714 ok(SUCCEEDED(hr), "IDirect3DVertexBuffer9_Unlock failed (0x%08x)\n", hr);
1716 if(buffer) IDirect3DVertexBuffer9_Release(buffer);
1720 cleanup:
1721 if(d3d9) IDirect3D9_Release(d3d9);
1724 static void test_lights(void)
1726 D3DPRESENT_PARAMETERS present_parameters;
1727 IDirect3DDevice9 *device = NULL;
1728 IDirect3D9 *d3d9;
1729 HWND hwnd;
1730 HRESULT hr;
1731 unsigned int i;
1732 BOOL enabled;
1733 D3DCAPS9 caps;
1735 d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1736 ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1737 hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1738 ok(hwnd != NULL, "Failed to create window\n");
1739 if (!d3d9 || !hwnd) goto cleanup;
1741 ZeroMemory(&present_parameters, sizeof(present_parameters));
1742 present_parameters.Windowed = TRUE;
1743 present_parameters.hDeviceWindow = hwnd;
1744 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1746 hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1747 D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &present_parameters, &device );
1748 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1749 if(!device)
1751 skip("Failed to create a d3d device\n");
1752 goto cleanup;
1755 memset(&caps, 0, sizeof(caps));
1756 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1757 ok(hr == D3D_OK, "IDirect3DDevice9_GetDeviceCaps failed with %s\n", DXGetErrorString9(hr));
1759 for(i = 1; i <= caps.MaxActiveLights; i++) {
1760 hr = IDirect3DDevice9_LightEnable(device, i, TRUE);
1761 ok(hr == D3D_OK, "Enabling light %u failed with %s\n", i, DXGetErrorString9(hr));
1762 hr = IDirect3DDevice9_GetLightEnable(device, i, &enabled);
1763 ok(hr == D3D_OK, "GetLightEnable on light %u failed with %s\n", i, DXGetErrorString9(hr));
1764 ok(enabled, "Light %d is %s\n", i, enabled ? "enabled" : "disabled");
1767 /* TODO: Test the rendering results in this situation */
1768 hr = IDirect3DDevice9_LightEnable(device, i + 1, TRUE);
1769 ok(hr == D3D_OK, "Enabling one light more than supported returned %s\n", DXGetErrorString9(hr));
1770 hr = IDirect3DDevice9_GetLightEnable(device, i + 1, &enabled);
1771 ok(hr == D3D_OK, "GetLightEnable on light %u failed with %s\n", i + 1, DXGetErrorString9(hr));
1772 ok(enabled, "Light %d is %s\n", i + 1, enabled ? "enabled" : "disabled");
1773 hr = IDirect3DDevice9_LightEnable(device, i + 1, FALSE);
1774 ok(hr == D3D_OK, "Disabling the additional returned %s\n", DXGetErrorString9(hr));
1776 for(i = 1; i <= caps.MaxActiveLights; i++) {
1777 hr = IDirect3DDevice9_LightEnable(device, i, FALSE);
1778 ok(hr == D3D_OK, "Disabling light %u failed with %s\n", i, DXGetErrorString9(hr));
1781 cleanup:
1782 if(device) IDirect3DDevice9_Release(device);
1783 if(d3d9) IDirect3D9_Release(d3d9);
1786 static void test_set_stream_source(void)
1788 D3DPRESENT_PARAMETERS present_parameters;
1789 IDirect3DDevice9 *device = NULL;
1790 IDirect3D9 *d3d9;
1791 HWND hwnd;
1792 HRESULT hr;
1793 IDirect3DVertexBuffer9 *pVertexBuffer = NULL;
1795 d3d9 = pDirect3DCreate9( D3D_SDK_VERSION );
1796 ok(d3d9 != NULL, "Failed to create IDirect3D9 object\n");
1797 hwnd = CreateWindow( "static", "d3d9_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1798 ok(hwnd != NULL, "Failed to create window\n");
1799 if (!d3d9 || !hwnd) goto cleanup;
1801 ZeroMemory(&present_parameters, sizeof(present_parameters));
1802 present_parameters.Windowed = TRUE;
1803 present_parameters.hDeviceWindow = hwnd;
1804 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1806 hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1807 D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
1808 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1809 if(!device)
1811 hr = IDirect3D9_CreateDevice( d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd,
1812 D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device );
1813 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE, "IDirect3D9_CreateDevice failed with %s\n", DXGetErrorString9(hr));
1814 if(!device)
1816 skip("Failed to create a d3d device\n");
1817 goto cleanup;
1821 hr = IDirect3DDevice9_CreateVertexBuffer( device, 512, 0, 0, D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
1822 ok(hr == D3D_OK, "Failed to create a vertex buffer, hr = %s\n", DXGetErrorString9(hr));
1823 if (SUCCEEDED(hr)) {
1824 /* Some cards(Geforce 7400 at least) accept non-aligned offsets, others(radeon 9000 verified) reject it,
1825 * so accept both results. Wine currently rejects this to be able to optimize the vbo conversion, but writes
1826 * a WARN
1828 hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 0, 32);
1829 ok(hr == D3D_OK, "Failed to set the stream source, offset 0, hr = %s\n",
1830 DXGetErrorString9(hr));
1831 hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 1, 32);
1832 ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 1, hr = %s\n",
1833 DXGetErrorString9(hr));
1834 hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 2, 32);
1835 ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 2, hr = %s\n",
1836 DXGetErrorString9(hr));
1837 hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 3, 32);
1838 ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 3, hr = %s\n",
1839 DXGetErrorString9(hr));
1840 hr = IDirect3DDevice9_SetStreamSource(device, 0, pVertexBuffer, 4, 32);
1841 ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %s\n",
1842 DXGetErrorString9(hr));
1844 /* Try to set the NULL buffer with an offset and stride 0 */
1845 hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
1846 ok(hr == D3D_OK, "Failed to set the stream source, offset 0, hr = %s\n",
1847 DXGetErrorString9(hr));
1848 hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 1, 0);
1849 ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 1, hr = %s\n",
1850 DXGetErrorString9(hr));
1851 hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 2, 0);
1852 ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 2, hr = %s\n",
1853 DXGetErrorString9(hr));
1854 hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 3, 0);
1855 ok(hr == D3DERR_INVALIDCALL || hr == D3D_OK, "Unexpected result when setting the stream source, offset 3, hr = %s\n",
1856 DXGetErrorString9(hr));
1857 hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 4, 0);
1858 ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %s\n",
1859 DXGetErrorString9(hr));
1861 hr = IDirect3DDevice9_SetStreamSource(device, 0, NULL, 0, 0);
1862 ok(hr == D3D_OK, "Failed to set the stream source, offset 4, hr = %s\n", DXGetErrorString9(hr));
1864 if(pVertexBuffer) IDirect3DDevice9_Release(pVertexBuffer);
1865 cleanup:
1866 if(device) IDirect3DDevice9_Release(device);
1867 if(d3d9) IDirect3D9_Release(d3d9);
1870 START_TEST(device)
1872 HMODULE d3d9_handle = LoadLibraryA( "d3d9.dll" );
1873 if (!d3d9_handle)
1875 skip("Could not load d3d9.dll\n");
1876 return;
1879 pDirect3DCreate9 = (void *)GetProcAddress( d3d9_handle, "Direct3DCreate9" );
1880 ok(pDirect3DCreate9 != NULL, "Failed to get address of Direct3DCreate9\n");
1881 if (pDirect3DCreate9)
1883 test_display_modes();
1884 test_swapchain();
1885 test_refcount();
1886 test_mipmap_levels();
1887 test_cursor();
1888 test_reset();
1889 test_reset();
1890 test_scene();
1891 test_limits();
1892 test_depthstenciltest();
1893 test_draw_indexed();
1894 test_null_stream();
1895 test_vertex_buffer_alignment();
1896 test_lights();
1897 test_set_stream_source();