d3d8/tests: Add window proc tests for switching between fullscreen and windowed.
[wine/multimedia.git] / dlls / d3d8 / tests / device.c
blob663dc917121af7e58bffe1f6dc373e89fc8d8442
1 /*
2 * Copyright (C) 2006 Vitaliy Margolen
3 * Copyright (C) 2006 Chris Robinson
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #define COBJMACROS
21 #include <initguid.h>
22 #include <d3d8.h>
23 #include "wine/test.h"
25 static IDirect3D8 *(WINAPI *pDirect3DCreate8)(UINT);
27 static BOOL (WINAPI *pGetCursorInfo)(PCURSORINFO);
29 static const DWORD simple_vs[] = {0xFFFE0101, /* vs_1_1 */
30 0x00000009, 0xC0010000, 0x90E40000, 0xA0E40000, /* dp4 oPos.x, v0, c0 */
31 0x00000009, 0xC0020000, 0x90E40000, 0xA0E40001, /* dp4 oPos.y, v0, c1 */
32 0x00000009, 0xC0040000, 0x90E40000, 0xA0E40002, /* dp4 oPos.z, v0, c2 */
33 0x00000009, 0xC0080000, 0x90E40000, 0xA0E40003, /* dp4 oPos.w, v0, c3 */
34 0x0000FFFF}; /* END */
35 static const DWORD simple_ps[] = {0xFFFF0101, /* ps_1_1 */
36 0x00000051, 0xA00F0001, 0x3F800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0 */
37 0x00000042, 0xB00F0000, /* tex t0 */
38 0x00000008, 0x800F0000, 0xA0E40001, 0xA0E40000, /* dp3 r0, c1, c0 */
39 0x00000005, 0x800F0000, 0x90E40000, 0x80E40000, /* mul r0, v0, r0 */
40 0x00000005, 0x800F0000, 0xB0E40000, 0x80E40000, /* mul r0, t0, r0 */
41 0x0000FFFF}; /* END */
43 static int get_refcount(IUnknown *object)
45 IUnknown_AddRef( object );
46 return IUnknown_Release( object );
49 /* try to make sure pending X events have been processed before continuing */
50 static void flush_events(void)
52 MSG msg;
53 int diff = 200;
54 int min_timeout = 100;
55 DWORD time = GetTickCount() + diff;
57 while (diff > 0)
59 if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
60 while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
61 diff = time - GetTickCount();
65 static IDirect3DDevice8 *create_device(IDirect3D8 *d3d8, HWND device_window, HWND focus_window, BOOL windowed)
67 D3DPRESENT_PARAMETERS present_parameters = {0};
68 IDirect3DDevice8 *device;
70 present_parameters.Windowed = windowed;
71 present_parameters.hDeviceWindow = device_window;
72 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
73 present_parameters.BackBufferWidth = 640;
74 present_parameters.BackBufferHeight = 480;
75 present_parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
76 present_parameters.EnableAutoDepthStencil = TRUE;
77 present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
79 if (SUCCEEDED(IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, focus_window,
80 D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device))) return device;
82 present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
83 if (SUCCEEDED(IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, focus_window,
84 D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device))) return device;
86 if (SUCCEEDED(IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, focus_window,
87 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device))) return device;
89 return NULL;
92 static HRESULT reset_device(IDirect3DDevice8 *device, HWND device_window, BOOL windowed)
94 D3DPRESENT_PARAMETERS present_parameters = {0};
96 present_parameters.Windowed = windowed;
97 present_parameters.hDeviceWindow = device_window;
98 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
99 present_parameters.BackBufferWidth = 640;
100 present_parameters.BackBufferHeight = 480;
101 present_parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
102 present_parameters.EnableAutoDepthStencil = TRUE;
103 present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
105 return IDirect3DDevice8_Reset(device, &present_parameters);
108 #define CHECK_CALL(r,c,d,rc) \
109 if (SUCCEEDED(r)) {\
110 int tmp1 = get_refcount( (IUnknown *)d ); \
111 int rc_new = rc; \
112 ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
113 } else {\
114 trace("%s failed: %#08x\n", c, r); \
117 #define CHECK_RELEASE(obj,d,rc) \
118 if (obj) { \
119 int tmp1, rc_new = rc; \
120 IUnknown_Release( obj ); \
121 tmp1 = get_refcount( (IUnknown *)d ); \
122 ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
125 #define CHECK_REFCOUNT(obj,rc) \
127 int rc_new = rc; \
128 int count = get_refcount( (IUnknown *)obj ); \
129 ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
132 #define CHECK_RELEASE_REFCOUNT(obj,rc) \
134 int rc_new = rc; \
135 int count = IUnknown_Release( (IUnknown *)obj ); \
136 ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
139 #define CHECK_ADDREF_REFCOUNT(obj,rc) \
141 int rc_new = rc; \
142 int count = IUnknown_AddRef( (IUnknown *)obj ); \
143 ok(count == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, count); \
146 #define CHECK_SURFACE_CONTAINER(obj,iid,expected) \
148 void *container_ptr = (void *)0x1337c0d3; \
149 hr = IDirect3DSurface8_GetContainer(obj, &iid, &container_ptr); \
150 ok(SUCCEEDED(hr) && container_ptr == expected, "GetContainer returned: hr %#08x, container_ptr %p. " \
151 "Expected hr %#08x, container_ptr %p\n", hr, container_ptr, S_OK, expected); \
152 if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr); \
155 static void check_mipmap_levels(IDirect3DDevice8 *device, UINT width, UINT height, UINT count)
157 IDirect3DBaseTexture8* texture = NULL;
158 HRESULT hr = IDirect3DDevice8_CreateTexture( device, width, height, 0, 0,
159 D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, (IDirect3DTexture8**) &texture );
161 if (SUCCEEDED(hr)) {
162 DWORD levels = IDirect3DBaseTexture8_GetLevelCount(texture);
163 ok(levels == count, "Invalid level count. Expected %d got %u\n", count, levels);
164 } else
165 trace("CreateTexture failed: %#08x\n", hr);
167 if (texture) IUnknown_Release( texture );
170 static void test_mipmap_levels(void)
173 HRESULT hr;
174 HWND hwnd = NULL;
176 IDirect3D8 *pD3d = NULL;
177 IDirect3DDevice8 *pDevice = NULL;
178 D3DPRESENT_PARAMETERS d3dpp;
179 D3DDISPLAYMODE d3ddm;
181 pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
182 ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
183 hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
184 ok(hwnd != NULL, "Failed to create window\n");
185 if (!pD3d || !hwnd) goto cleanup;
187 IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
188 ZeroMemory( &d3dpp, sizeof(d3dpp) );
189 d3dpp.Windowed = TRUE;
190 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
191 d3dpp.BackBufferFormat = d3ddm.Format;
193 hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
194 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
195 if(FAILED(hr))
197 skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
198 goto cleanup;
201 check_mipmap_levels(pDevice, 32, 32, 6);
202 check_mipmap_levels(pDevice, 256, 1, 9);
203 check_mipmap_levels(pDevice, 1, 256, 9);
204 check_mipmap_levels(pDevice, 1, 1, 1);
206 cleanup:
207 if (pDevice)
209 UINT refcount = IUnknown_Release( pDevice );
210 ok(!refcount, "Device has %u references left.\n", refcount);
212 if (pD3d) IUnknown_Release( pD3d );
213 DestroyWindow( hwnd );
216 static void test_swapchain(void)
218 HRESULT hr;
219 HWND hwnd = NULL;
220 IDirect3D8 *pD3d = NULL;
221 IDirect3DDevice8 *pDevice = NULL;
222 IDirect3DSwapChain8 *swapchain1 = NULL;
223 IDirect3DSwapChain8 *swapchain2 = NULL;
224 IDirect3DSwapChain8 *swapchain3 = NULL;
225 IDirect3DSurface8 *backbuffer = NULL;
226 D3DPRESENT_PARAMETERS d3dpp;
227 D3DDISPLAYMODE d3ddm;
229 pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
230 ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
231 hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
232 ok(hwnd != NULL, "Failed to create window\n");
233 if (!pD3d || !hwnd) goto cleanup;
235 IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
236 ZeroMemory( &d3dpp, sizeof(d3dpp) );
237 d3dpp.Windowed = TRUE;
238 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
239 d3dpp.BackBufferFormat = d3ddm.Format;
240 d3dpp.BackBufferCount = 0;
242 hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
243 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
244 if(FAILED(hr))
246 skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
247 goto cleanup;
250 /* Check if the back buffer count was modified */
251 ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
253 /* Create a bunch of swapchains */
254 d3dpp.BackBufferCount = 0;
255 hr = IDirect3DDevice8_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain1);
256 ok(SUCCEEDED(hr), "Failed to create a swapchain (%#08x)\n", hr);
257 ok(d3dpp.BackBufferCount == 1, "The back buffer count in the presentparams struct is %d\n", d3dpp.BackBufferCount);
259 d3dpp.BackBufferCount = 1;
260 hr = IDirect3DDevice8_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain2);
261 ok(SUCCEEDED(hr), "Failed to create a swapchain (%#08x)\n", hr);
263 d3dpp.BackBufferCount = 2;
264 hr = IDirect3DDevice8_CreateAdditionalSwapChain(pDevice, &d3dpp, &swapchain3);
265 ok(SUCCEEDED(hr), "Failed to create a swapchain (%#08x)\n", hr);
266 if(SUCCEEDED(hr)) {
267 /* Swapchain 3, created with backbuffercount 2 */
268 backbuffer = (void *) 0xdeadbeef;
269 hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 0, 0, &backbuffer);
270 ok(SUCCEEDED(hr), "Failed to get the 1st back buffer (%#08x)\n", hr);
271 ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
272 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
274 backbuffer = (void *) 0xdeadbeef;
275 hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 1, 0, &backbuffer);
276 ok(SUCCEEDED(hr), "Failed to get the 2nd back buffer (%#08x)\n", hr);
277 ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
278 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
280 backbuffer = (void *) 0xdeadbeef;
281 hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 2, 0, &backbuffer);
282 ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %#08x\n", hr);
283 ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
284 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
286 backbuffer = (void *) 0xdeadbeef;
287 hr = IDirect3DSwapChain8_GetBackBuffer(swapchain3, 3, 0, &backbuffer);
288 ok(FAILED(hr), "Failed to get the back buffer (%#08x)\n", hr);
289 ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
290 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
293 /* Check the back buffers of the swapchains */
294 /* Swapchain 1, created with backbuffercount 0 */
295 hr = IDirect3DSwapChain8_GetBackBuffer(swapchain1, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
296 ok(SUCCEEDED(hr), "Failed to get the back buffer (%#08x)\n", hr);
297 ok(backbuffer != NULL, "The back buffer is NULL (%#08x)\n", hr);
298 if(backbuffer) IDirect3DSurface8_Release(backbuffer);
300 backbuffer = (void *) 0xdeadbeef;
301 hr = IDirect3DSwapChain8_GetBackBuffer(swapchain1, 1, 0, &backbuffer);
302 ok(FAILED(hr), "Failed to get the back buffer (%#08x)\n", hr);
303 ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
304 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
306 /* Swapchain 2 - created with backbuffercount 1 */
307 backbuffer = (void *) 0xdeadbeef;
308 hr = IDirect3DSwapChain8_GetBackBuffer(swapchain2, 0, 0, &backbuffer);
309 ok(SUCCEEDED(hr), "Failed to get the back buffer (%#08x)\n", hr);
310 ok(backbuffer != NULL && backbuffer != (void *) 0xdeadbeef, "The back buffer is %p\n", backbuffer);
311 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
313 backbuffer = (void *) 0xdeadbeef;
314 hr = IDirect3DSwapChain8_GetBackBuffer(swapchain2, 1, 0, &backbuffer);
315 ok(hr == D3DERR_INVALIDCALL, "GetBackBuffer returned %#08x\n", hr);
316 ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
317 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
319 backbuffer = (void *) 0xdeadbeef;
320 hr = IDirect3DSwapChain8_GetBackBuffer(swapchain2, 2, 0, &backbuffer);
321 ok(FAILED(hr), "Failed to get the back buffer (%#08x)\n", hr);
322 ok(backbuffer == (void *) 0xdeadbeef, "The back buffer pointer was modified (%p)\n", backbuffer);
323 if(backbuffer && backbuffer != (void *) 0xdeadbeef) IDirect3DSurface8_Release(backbuffer);
325 cleanup:
326 if(swapchain1) IDirect3DSwapChain8_Release(swapchain1);
327 if(swapchain2) IDirect3DSwapChain8_Release(swapchain2);
328 if(swapchain3) IDirect3DSwapChain8_Release(swapchain3);
329 if (pDevice)
331 UINT refcount = IDirect3DDevice8_Release(pDevice);
332 ok(!refcount, "Device has %u references left.\n", refcount);
334 if (pD3d) IDirect3D8_Release(pD3d);
335 DestroyWindow( hwnd );
338 static void test_refcount(void)
340 HRESULT hr;
341 HWND hwnd = NULL;
342 IDirect3D8 *pD3d = NULL;
343 IDirect3DDevice8 *pDevice = NULL;
344 IDirect3DVertexBuffer8 *pVertexBuffer = NULL;
345 IDirect3DIndexBuffer8 *pIndexBuffer = NULL;
346 DWORD dVertexShader = -1;
347 DWORD dPixelShader = -1;
348 IDirect3DCubeTexture8 *pCubeTexture = NULL;
349 IDirect3DTexture8 *pTexture = NULL;
350 IDirect3DVolumeTexture8 *pVolumeTexture = NULL;
351 IDirect3DVolume8 *pVolumeLevel = NULL;
352 IDirect3DSurface8 *pStencilSurface = NULL;
353 IDirect3DSurface8 *pImageSurface = NULL;
354 IDirect3DSurface8 *pRenderTarget = NULL;
355 IDirect3DSurface8 *pRenderTarget2 = NULL;
356 IDirect3DSurface8 *pRenderTarget3 = NULL;
357 IDirect3DSurface8 *pTextureLevel = NULL;
358 IDirect3DSurface8 *pBackBuffer = NULL;
359 DWORD dStateBlock = -1;
360 IDirect3DSwapChain8 *pSwapChain = NULL;
361 D3DCAPS8 caps;
363 D3DPRESENT_PARAMETERS d3dpp;
364 D3DDISPLAYMODE d3ddm;
365 int refcount = 0, tmp;
367 DWORD decl[] =
369 D3DVSD_STREAM(0),
370 D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3), /* D3DVSDE_POSITION, Register v0 */
371 D3DVSD_REG(D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR), /* D3DVSDE_DIFFUSE, Register v5 */
372 D3DVSD_END()
375 pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
376 ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
377 hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
378 ok(hwnd != NULL, "Failed to create window\n");
379 if (!pD3d || !hwnd) goto cleanup;
381 IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
382 ZeroMemory( &d3dpp, sizeof(d3dpp) );
383 d3dpp.Windowed = TRUE;
384 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
385 d3dpp.BackBufferFormat = d3ddm.Format;
386 d3dpp.EnableAutoDepthStencil = TRUE;
387 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
389 hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
390 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
391 if(FAILED(hr))
393 skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
394 goto cleanup;
396 IDirect3DDevice8_GetDeviceCaps(pDevice, &caps);
398 refcount = get_refcount( (IUnknown *)pDevice );
399 ok(refcount == 1, "Invalid device RefCount %d\n", refcount);
402 * Check refcount of implicit surfaces. Findings:
403 * - the container is the device
404 * - they hold a reference to the device
405 * - they are created with a refcount of 0 (Get/Release returns original refcount)
406 * - they are not freed if refcount reaches 0.
407 * - the refcount is not forwarded to the container.
409 hr = IDirect3DDevice8_GetRenderTarget(pDevice, &pRenderTarget);
410 CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
411 if(pRenderTarget)
413 CHECK_SURFACE_CONTAINER( pRenderTarget, IID_IDirect3DDevice8, pDevice);
414 CHECK_REFCOUNT( pRenderTarget, 1);
416 CHECK_ADDREF_REFCOUNT(pRenderTarget, 2);
417 CHECK_REFCOUNT(pDevice, refcount);
418 CHECK_RELEASE_REFCOUNT(pRenderTarget, 1);
419 CHECK_REFCOUNT(pDevice, refcount);
421 hr = IDirect3DDevice8_GetRenderTarget(pDevice, &pRenderTarget);
422 CHECK_CALL( hr, "GetRenderTarget", pDevice, refcount);
423 CHECK_REFCOUNT( pRenderTarget, 2);
424 CHECK_RELEASE_REFCOUNT( pRenderTarget, 1);
425 CHECK_RELEASE_REFCOUNT( pRenderTarget, 0);
426 CHECK_REFCOUNT( pDevice, --refcount);
428 /* The render target is released with the device, so AddRef with refcount=0 is fine here. */
429 CHECK_ADDREF_REFCOUNT(pRenderTarget, 1);
430 CHECK_REFCOUNT(pDevice, ++refcount);
431 CHECK_RELEASE_REFCOUNT(pRenderTarget, 0);
432 CHECK_REFCOUNT(pDevice, --refcount);
435 /* Render target and back buffer are identical. */
436 hr = IDirect3DDevice8_GetBackBuffer(pDevice, 0, 0, &pBackBuffer);
437 CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
438 if(pBackBuffer)
440 CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
441 ok(pRenderTarget == pBackBuffer, "RenderTarget=%p and BackBuffer=%p should be the same.\n",
442 pRenderTarget, pBackBuffer);
443 pBackBuffer = NULL;
445 CHECK_REFCOUNT( pDevice, --refcount);
447 hr = IDirect3DDevice8_GetDepthStencilSurface(pDevice, &pStencilSurface);
448 CHECK_CALL( hr, "GetDepthStencilSurface", pDevice, ++refcount);
449 if(pStencilSurface)
451 CHECK_SURFACE_CONTAINER( pStencilSurface, IID_IDirect3DDevice8, pDevice);
452 CHECK_REFCOUNT( pStencilSurface, 1);
454 CHECK_ADDREF_REFCOUNT(pStencilSurface, 2);
455 CHECK_REFCOUNT(pDevice, refcount);
456 CHECK_RELEASE_REFCOUNT(pStencilSurface, 1);
457 CHECK_REFCOUNT(pDevice, refcount);
459 CHECK_RELEASE_REFCOUNT( pStencilSurface, 0);
460 CHECK_REFCOUNT( pDevice, --refcount);
462 /* The stencil surface is released with the device, so AddRef with refcount=0 is fine here. */
463 CHECK_ADDREF_REFCOUNT(pStencilSurface, 1);
464 CHECK_REFCOUNT(pDevice, ++refcount);
465 CHECK_RELEASE_REFCOUNT(pStencilSurface, 0);
466 CHECK_REFCOUNT(pDevice, --refcount);
467 pStencilSurface = NULL;
470 /* Buffers */
471 hr = IDirect3DDevice8_CreateIndexBuffer( pDevice, 16, 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &pIndexBuffer );
472 CHECK_CALL( hr, "CreateIndexBuffer", pDevice, ++refcount );
473 if(pIndexBuffer)
475 tmp = get_refcount( (IUnknown *)pIndexBuffer );
477 hr = IDirect3DDevice8_SetIndices(pDevice, pIndexBuffer, 0);
478 CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
479 hr = IDirect3DDevice8_SetIndices(pDevice, NULL, 0);
480 CHECK_CALL( hr, "SetIndices", pIndexBuffer, tmp);
483 hr = IDirect3DDevice8_CreateVertexBuffer( pDevice, 16, 0, D3DFVF_XYZ, D3DPOOL_DEFAULT, &pVertexBuffer );
484 CHECK_CALL( hr, "CreateVertexBuffer", pDevice, ++refcount );
485 if(pVertexBuffer)
487 IDirect3DVertexBuffer8 *pVBuf = (void*)~0;
488 UINT stride = ~0;
490 tmp = get_refcount( (IUnknown *)pVertexBuffer );
492 hr = IDirect3DDevice8_SetStreamSource(pDevice, 0, pVertexBuffer, 3 * sizeof(float));
493 CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
494 hr = IDirect3DDevice8_SetStreamSource(pDevice, 0, NULL, 0);
495 CHECK_CALL( hr, "SetStreamSource", pVertexBuffer, tmp);
497 hr = IDirect3DDevice8_GetStreamSource(pDevice, 0, &pVBuf, &stride);
498 ok(SUCCEEDED(hr), "GetStreamSource did not succeed with NULL stream!\n");
499 ok(pVBuf==NULL, "pVBuf not NULL (%p)!\n", pVBuf);
500 ok(stride==3*sizeof(float), "stride not 3 floats (got %u)!\n", stride);
503 /* Shaders */
504 hr = IDirect3DDevice8_CreateVertexShader( pDevice, decl, simple_vs, &dVertexShader, 0 );
505 CHECK_CALL( hr, "CreateVertexShader", pDevice, refcount );
506 if (caps.PixelShaderVersion >= D3DPS_VERSION(1, 0))
508 hr = IDirect3DDevice8_CreatePixelShader( pDevice, simple_ps, &dPixelShader );
509 CHECK_CALL( hr, "CreatePixelShader", pDevice, refcount );
511 /* Textures */
512 hr = IDirect3DDevice8_CreateTexture( pDevice, 32, 32, 3, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pTexture );
513 CHECK_CALL( hr, "CreateTexture", pDevice, ++refcount );
514 if (pTexture)
516 tmp = get_refcount( (IUnknown *)pTexture );
518 /* SetTexture should not increase refcounts */
519 hr = IDirect3DDevice8_SetTexture(pDevice, 0, (IDirect3DBaseTexture8 *) pTexture);
520 CHECK_CALL( hr, "SetTexture", pTexture, tmp);
521 hr = IDirect3DDevice8_SetTexture(pDevice, 0, NULL);
522 CHECK_CALL( hr, "SetTexture", pTexture, tmp);
524 /* This should not increment device refcount */
525 hr = IDirect3DTexture8_GetSurfaceLevel( pTexture, 1, &pTextureLevel );
526 CHECK_CALL( hr, "GetSurfaceLevel", pDevice, refcount );
527 /* But should increment texture's refcount */
528 CHECK_REFCOUNT( pTexture, tmp+1 );
529 /* Because the texture and surface refcount are identical */
530 if (pTextureLevel)
532 CHECK_REFCOUNT ( pTextureLevel, tmp+1 );
533 CHECK_ADDREF_REFCOUNT ( pTextureLevel, tmp+2 );
534 CHECK_REFCOUNT ( pTexture , tmp+2 );
535 CHECK_RELEASE_REFCOUNT( pTextureLevel, tmp+1 );
536 CHECK_REFCOUNT ( pTexture , tmp+1 );
537 CHECK_RELEASE_REFCOUNT( pTexture , tmp );
538 CHECK_REFCOUNT ( pTextureLevel, tmp );
541 if(caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP)
543 hr = IDirect3DDevice8_CreateCubeTexture( pDevice, 32, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pCubeTexture );
544 CHECK_CALL( hr, "CreateCubeTexture", pDevice, ++refcount );
546 else
548 skip("Cube textures not supported\n");
550 if(caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP)
552 hr = IDirect3DDevice8_CreateVolumeTexture( pDevice, 32, 32, 2, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pVolumeTexture );
553 CHECK_CALL( hr, "CreateVolumeTexture", pDevice, ++refcount );
555 else
557 skip("Volume textures not supported\n");
560 if (pVolumeTexture)
562 tmp = get_refcount( (IUnknown *)pVolumeTexture );
564 /* This should not increment device refcount */
565 hr = IDirect3DVolumeTexture8_GetVolumeLevel(pVolumeTexture, 0, &pVolumeLevel);
566 CHECK_CALL( hr, "GetVolumeLevel", pDevice, refcount );
567 /* But should increment volume texture's refcount */
568 CHECK_REFCOUNT( pVolumeTexture, tmp+1 );
569 /* Because the volume texture and volume refcount are identical */
570 if (pVolumeLevel)
572 CHECK_REFCOUNT ( pVolumeLevel , tmp+1 );
573 CHECK_ADDREF_REFCOUNT ( pVolumeLevel , tmp+2 );
574 CHECK_REFCOUNT ( pVolumeTexture, tmp+2 );
575 CHECK_RELEASE_REFCOUNT( pVolumeLevel , tmp+1 );
576 CHECK_REFCOUNT ( pVolumeTexture, tmp+1 );
577 CHECK_RELEASE_REFCOUNT( pVolumeTexture, tmp );
578 CHECK_REFCOUNT ( pVolumeLevel , tmp );
581 /* Surfaces */
582 hr = IDirect3DDevice8_CreateDepthStencilSurface( pDevice, 32, 32, D3DFMT_D16, D3DMULTISAMPLE_NONE, &pStencilSurface );
583 CHECK_CALL( hr, "CreateDepthStencilSurface", pDevice, ++refcount );
584 CHECK_REFCOUNT( pStencilSurface, 1);
585 hr = IDirect3DDevice8_CreateImageSurface( pDevice, 32, 32, D3DFMT_X8R8G8B8, &pImageSurface );
586 CHECK_CALL( hr, "CreateImageSurface", pDevice, ++refcount );
587 CHECK_REFCOUNT( pImageSurface, 1);
588 hr = IDirect3DDevice8_CreateRenderTarget( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, TRUE, &pRenderTarget3 );
589 CHECK_CALL( hr, "CreateRenderTarget", pDevice, ++refcount );
590 CHECK_REFCOUNT( pRenderTarget3, 1);
591 /* Misc */
592 hr = IDirect3DDevice8_CreateStateBlock( pDevice, D3DSBT_ALL, &dStateBlock );
593 CHECK_CALL( hr, "CreateStateBlock", pDevice, refcount );
594 hr = IDirect3DDevice8_CreateAdditionalSwapChain( pDevice, &d3dpp, &pSwapChain );
595 CHECK_CALL( hr, "CreateAdditionalSwapChain", pDevice, ++refcount );
596 if(pSwapChain)
598 /* check implicit back buffer */
599 hr = IDirect3DSwapChain8_GetBackBuffer(pSwapChain, 0, 0, &pBackBuffer);
600 CHECK_CALL( hr, "GetBackBuffer", pDevice, ++refcount);
601 CHECK_REFCOUNT( pSwapChain, 1);
602 if(pBackBuffer)
604 CHECK_SURFACE_CONTAINER( pBackBuffer, IID_IDirect3DDevice8, pDevice);
605 CHECK_REFCOUNT( pBackBuffer, 1);
606 CHECK_RELEASE_REFCOUNT( pBackBuffer, 0);
607 CHECK_REFCOUNT( pDevice, --refcount);
609 /* The back buffer is released with the swapchain, so AddRef with refcount=0 is fine here. */
610 CHECK_ADDREF_REFCOUNT(pBackBuffer, 1);
611 CHECK_REFCOUNT(pDevice, ++refcount);
612 CHECK_RELEASE_REFCOUNT(pBackBuffer, 0);
613 CHECK_REFCOUNT(pDevice, --refcount);
614 pBackBuffer = NULL;
616 CHECK_REFCOUNT( pSwapChain, 1);
619 if(pVertexBuffer)
621 BYTE *data;
622 /* Vertex buffers can be locked multiple times */
623 hr = IDirect3DVertexBuffer8_Lock(pVertexBuffer, 0, 0, &data, 0);
624 ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Lock failed with %#08x\n", hr);
625 hr = IDirect3DVertexBuffer8_Lock(pVertexBuffer, 0, 0, &data, 0);
626 ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Lock failed with %#08x\n", hr);
627 hr = IDirect3DVertexBuffer8_Unlock(pVertexBuffer);
628 ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Unlock failed with %#08x\n", hr);
629 hr = IDirect3DVertexBuffer8_Unlock(pVertexBuffer);
630 ok(hr == D3D_OK, "IDirect3DVertexBuffer8::Unlock failed with %#08x\n", hr);
633 /* The implicit render target is not freed if refcount reaches 0.
634 * Otherwise GetRenderTarget would re-allocate it and the pointer would change.*/
635 hr = IDirect3DDevice8_GetRenderTarget(pDevice, &pRenderTarget2);
636 CHECK_CALL( hr, "GetRenderTarget", pDevice, ++refcount);
637 if(pRenderTarget2)
639 CHECK_RELEASE_REFCOUNT(pRenderTarget2, 0);
640 ok(pRenderTarget == pRenderTarget2, "RenderTarget=%p and RenderTarget2=%p should be the same.\n",
641 pRenderTarget, pRenderTarget2);
642 CHECK_REFCOUNT( pDevice, --refcount);
643 pRenderTarget2 = NULL;
645 pRenderTarget = NULL;
647 cleanup:
648 CHECK_RELEASE(pDevice, pDevice, --refcount);
650 /* Buffers */
651 CHECK_RELEASE(pVertexBuffer, pDevice, --refcount);
652 CHECK_RELEASE(pIndexBuffer, pDevice, --refcount);
653 /* Shaders */
654 if (dVertexShader != ~0U) IDirect3DDevice8_DeleteVertexShader( pDevice, dVertexShader );
655 if (dPixelShader != ~0U) IDirect3DDevice8_DeletePixelShader( pDevice, dPixelShader );
656 /* Textures */
657 CHECK_RELEASE(pTexture, pDevice, --refcount);
658 CHECK_RELEASE(pCubeTexture, pDevice, --refcount);
659 CHECK_RELEASE(pVolumeTexture, pDevice, --refcount);
660 /* Surfaces */
661 CHECK_RELEASE(pStencilSurface, pDevice, --refcount);
662 CHECK_RELEASE(pImageSurface, pDevice, --refcount);
663 CHECK_RELEASE(pRenderTarget3, pDevice, --refcount);
664 /* Misc */
665 if (dStateBlock != ~0U) IDirect3DDevice8_DeleteStateBlock( pDevice, dStateBlock );
666 /* This will destroy device - cannot check the refcount here */
667 if (pSwapChain) CHECK_RELEASE_REFCOUNT( pSwapChain, 0);
669 if (pD3d) CHECK_RELEASE_REFCOUNT( pD3d, 0);
671 DestroyWindow( hwnd );
674 static void test_cursor(void)
676 HRESULT hr;
677 HWND hwnd = NULL;
678 IDirect3D8 *pD3d = NULL;
679 IDirect3DDevice8 *pDevice = NULL;
680 D3DPRESENT_PARAMETERS d3dpp;
681 D3DDISPLAYMODE d3ddm;
682 CURSORINFO info;
683 IDirect3DSurface8 *cursor = NULL;
684 HCURSOR cur;
685 HMODULE user32_handle = GetModuleHandleA("user32.dll");
687 pGetCursorInfo = (void *)GetProcAddress(user32_handle, "GetCursorInfo");
688 if (!pGetCursorInfo)
690 win_skip("GetCursorInfo is not available\n");
691 return;
694 memset(&info, 0, sizeof(info));
695 info.cbSize = sizeof(info);
696 ok(pGetCursorInfo(&info), "GetCursorInfo failed\n");
697 cur = info.hCursor;
699 pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
700 ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
701 hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
702 ok(hwnd != NULL, "Failed to create window\n");
703 if (!pD3d || !hwnd) goto cleanup;
705 IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
706 ZeroMemory( &d3dpp, sizeof(d3dpp) );
707 d3dpp.Windowed = TRUE;
708 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
709 d3dpp.BackBufferFormat = d3ddm.Format;
711 hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
712 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
713 if(FAILED(hr))
715 skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
716 goto cleanup;
719 IDirect3DDevice8_CreateImageSurface(pDevice, 32, 32, D3DFMT_A8R8G8B8, &cursor);
720 ok(cursor != NULL, "IDirect3DDevice8_CreateOffscreenPlainSurface failed with %#08x\n", hr);
722 /* Initially hidden */
723 hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
724 ok(hr == FALSE, "IDirect3DDevice8_ShowCursor returned %#08x\n", hr);
726 /* Not enabled without a surface*/
727 hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
728 ok(hr == FALSE, "IDirect3DDevice8_ShowCursor returned %#08x\n", hr);
730 /* Fails */
731 hr = IDirect3DDevice8_SetCursorProperties(pDevice, 0, 0, NULL);
732 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_SetCursorProperties returned %#08x\n", hr);
734 hr = IDirect3DDevice8_SetCursorProperties(pDevice, 0, 0, cursor);
735 ok(hr == D3D_OK, "IDirect3DDevice8_SetCursorProperties returned %#08x\n", hr);
737 IDirect3DSurface8_Release(cursor);
739 memset(&info, 0, sizeof(info));
740 info.cbSize = sizeof(info);
741 ok(pGetCursorInfo(&info), "GetCursorInfo failed\n");
742 ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
743 ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
745 /* Still hidden */
746 hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
747 ok(hr == FALSE, "IDirect3DDevice8_ShowCursor returned %#08x\n", hr);
749 /* Enabled now*/
750 hr = IDirect3DDevice8_ShowCursor(pDevice, TRUE);
751 ok(hr == TRUE, "IDirect3DDevice8_ShowCursor returned %#08x\n", hr);
753 /* GDI cursor unchanged */
754 memset(&info, 0, sizeof(info));
755 info.cbSize = sizeof(info);
756 ok(pGetCursorInfo(&info), "GetCursorInfo failed\n");
757 ok(info.flags & CURSOR_SHOWING, "The gdi cursor is hidden (%08x)\n", info.flags);
758 ok(info.hCursor == cur, "The cursor handle is %p\n", info.hCursor); /* unchanged */
760 cleanup:
761 if (pDevice)
763 UINT refcount = IDirect3DDevice8_Release(pDevice);
764 ok(!refcount, "Device has %u references left.\n", refcount);
766 if (pD3d) IDirect3D8_Release(pD3d);
769 static void test_states(void)
771 HRESULT hr;
772 HWND hwnd = NULL;
773 IDirect3D8 *pD3d = NULL;
774 IDirect3DDevice8 *pDevice = NULL;
775 D3DPRESENT_PARAMETERS d3dpp;
776 D3DDISPLAYMODE d3ddm;
778 pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
779 ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
780 hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
781 ok(hwnd != NULL, "Failed to create window\n");
782 if (!pD3d || !hwnd) goto cleanup;
784 IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
785 ZeroMemory( &d3dpp, sizeof(d3dpp) );
786 d3dpp.Windowed = TRUE;
787 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
788 d3dpp.BackBufferWidth = 640;
789 d3dpp.BackBufferHeight = 480;
790 d3dpp.BackBufferFormat = d3ddm.Format;
792 hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
793 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
794 if(FAILED(hr))
796 skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
797 goto cleanup;
800 hr = IDirect3DDevice8_SetRenderState(pDevice, D3DRS_ZVISIBLE, TRUE);
801 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState(D3DRS_ZVISIBLE, TRUE) returned %#08x\n", hr);
802 hr = IDirect3DDevice8_SetRenderState(pDevice, D3DRS_ZVISIBLE, FALSE);
803 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState(D3DRS_ZVISIBLE, FALSE) returned %#08x\n", hr);
805 cleanup:
806 if (pDevice)
808 UINT refcount = IDirect3DDevice8_Release(pDevice);
809 ok(!refcount, "Device has %u references left.\n", refcount);
811 if (pD3d) IDirect3D8_Release(pD3d);
814 static void test_shader_versions(void)
816 HRESULT hr;
817 IDirect3D8 *pD3d = NULL;
818 D3DCAPS8 d3dcaps;
820 pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
821 ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
822 if (pD3d != NULL) {
823 hr = IDirect3D8_GetDeviceCaps(pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dcaps);
824 ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "Failed to get D3D8 caps (%#08x)\n", hr);
825 if (SUCCEEDED(hr)) {
826 ok(d3dcaps.VertexShaderVersion <= D3DVS_VERSION(1,1), "Unexpected VertexShaderVersion (%#x > %#x)\n", d3dcaps.VertexShaderVersion, D3DVS_VERSION(1,1));
827 ok(d3dcaps.PixelShaderVersion <= D3DPS_VERSION(1,4), "Unexpected PixelShaderVersion (%#x > %#x)\n", d3dcaps.PixelShaderVersion, D3DPS_VERSION(1,4));
828 } else {
829 skip("No Direct3D support\n");
831 IDirect3D8_Release(pD3d);
836 /* Test adapter display modes */
837 static void test_display_modes(void)
839 UINT max_modes, i;
840 D3DDISPLAYMODE dmode;
841 HRESULT res;
842 IDirect3D8 *pD3d;
844 pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
845 ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
846 if(!pD3d) return;
848 max_modes = IDirect3D8_GetAdapterModeCount(pD3d, D3DADAPTER_DEFAULT);
849 ok(max_modes > 0 ||
850 broken(max_modes == 0), /* VMware */
851 "GetAdapterModeCount(D3DADAPTER_DEFAULT) returned 0!\n");
853 for(i=0; i<max_modes;i++) {
854 res = IDirect3D8_EnumAdapterModes(pD3d, D3DADAPTER_DEFAULT, i, &dmode);
855 ok(res==D3D_OK, "EnumAdapterModes returned %#08x for mode %u!\n", res, i);
856 if(res != D3D_OK)
857 continue;
859 ok(dmode.Format==D3DFMT_X8R8G8B8 || dmode.Format==D3DFMT_R5G6B5,
860 "Unexpected display mode returned for mode %u: %#x\n", i , dmode.Format);
863 IDirect3D8_Release(pD3d);
866 static void test_scene(void)
868 HRESULT hr;
869 HWND hwnd = NULL;
870 IDirect3D8 *pD3d = NULL;
871 IDirect3DDevice8 *pDevice = NULL;
872 D3DPRESENT_PARAMETERS d3dpp;
873 D3DDISPLAYMODE d3ddm;
875 pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
876 ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
877 hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
878 ok(hwnd != NULL, "Failed to create window\n");
879 if (!pD3d || !hwnd) goto cleanup;
881 IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
882 ZeroMemory( &d3dpp, sizeof(d3dpp) );
883 d3dpp.Windowed = TRUE;
884 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
885 d3dpp.BackBufferWidth = 800;
886 d3dpp.BackBufferHeight = 600;
887 d3dpp.BackBufferFormat = d3ddm.Format;
890 hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
891 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
892 ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL || broken(hr == D3DERR_NOTAVAILABLE), "IDirect3D8_CreateDevice failed with %#08x\n", hr);
893 if(!pDevice)
895 skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
896 goto cleanup;
899 /* Test an EndScene without beginscene. Should return an error */
900 hr = IDirect3DDevice8_EndScene(pDevice);
901 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
903 /* Test a normal BeginScene / EndScene pair, this should work */
904 hr = IDirect3DDevice8_BeginScene(pDevice);
905 ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
906 if(SUCCEEDED(hr))
908 hr = IDirect3DDevice8_EndScene(pDevice);
909 ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
912 /* Test another EndScene without having begun a new scene. Should return an error */
913 hr = IDirect3DDevice8_EndScene(pDevice);
914 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
916 /* Two nested BeginScene and EndScene calls */
917 hr = IDirect3DDevice8_BeginScene(pDevice);
918 ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
919 hr = IDirect3DDevice8_BeginScene(pDevice);
920 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
921 hr = IDirect3DDevice8_EndScene(pDevice);
922 ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
923 hr = IDirect3DDevice8_EndScene(pDevice);
924 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
926 /* StretchRect does not exit in Direct3D8, so no equivalent to the d3d9 stretchrect tests */
928 cleanup:
929 if (pDevice)
931 UINT refcount = IDirect3DDevice8_Release(pDevice);
932 ok(!refcount, "Device has %u references left.\n", refcount);
934 if (pD3d) IDirect3D8_Release(pD3d);
935 if(hwnd) DestroyWindow(hwnd);
938 static void test_shader(void)
940 HRESULT hr;
941 HWND hwnd = NULL;
942 IDirect3D8 *pD3d = NULL;
943 IDirect3DDevice8 *pDevice = NULL;
944 D3DPRESENT_PARAMETERS d3dpp;
945 D3DDISPLAYMODE d3ddm;
946 DWORD hPixelShader = 0, hVertexShader = 0;
947 DWORD hPixelShader2 = 0, hVertexShader2 = 0;
948 DWORD hTempHandle;
949 D3DCAPS8 caps;
950 DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
951 DWORD data_size;
952 void *data;
954 static DWORD dwVertexDecl[] =
956 D3DVSD_STREAM(0),
957 D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),
958 D3DVSD_END()
960 DWORD decl_normal_float2[] =
962 D3DVSD_STREAM(0),
963 D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3), /* D3DVSDE_POSITION, Register v0 */
964 D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_FLOAT2), /* D3DVSDE_NORMAL, Register v1 */
965 D3DVSD_END()
967 DWORD decl_normal_float4[] =
969 D3DVSD_STREAM(0),
970 D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3), /* D3DVSDE_POSITION, Register v0 */
971 D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_FLOAT4), /* D3DVSDE_NORMAL, Register v1 */
972 D3DVSD_END()
974 DWORD decl_normal_d3dcolor[] =
976 D3DVSD_STREAM(0),
977 D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3), /* D3DVSDE_POSITION, Register v0 */
978 D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_D3DCOLOR),/* D3DVSDE_NORMAL, Register v1 */
979 D3DVSD_END()
981 const DWORD vertex_decl_size = sizeof(dwVertexDecl);
982 const DWORD simple_vs_size = sizeof(simple_vs);
983 const DWORD simple_ps_size = sizeof(simple_ps);
985 pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
986 ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
987 hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
988 ok(hwnd != NULL, "Failed to create window\n");
989 if (!pD3d || !hwnd) goto cleanup;
991 IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
992 ZeroMemory( &d3dpp, sizeof(d3dpp) );
993 d3dpp.Windowed = TRUE;
994 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
995 d3dpp.BackBufferWidth = 800;
996 d3dpp.BackBufferHeight = 600;
997 d3dpp.BackBufferFormat = d3ddm.Format;
1000 hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1001 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1002 ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL || broken(hr == D3DERR_NOTAVAILABLE), "IDirect3D8_CreateDevice failed with %#08x\n", hr);
1003 if(!pDevice)
1005 skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
1006 goto cleanup;
1008 IDirect3DDevice8_GetDeviceCaps(pDevice, &caps);
1010 /* Test setting and retrieving a FVF */
1011 hr = IDirect3DDevice8_SetVertexShader(pDevice, fvf);
1012 ok(SUCCEEDED(hr), "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1013 hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1014 ok(SUCCEEDED(hr), "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1015 ok(hTempHandle == fvf, "Vertex shader %#08x is set, expected %#08x\n", hTempHandle, fvf);
1017 /* First create a vertex shader */
1018 hr = IDirect3DDevice8_SetVertexShader(pDevice, 0);
1019 ok(SUCCEEDED(hr), "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1020 hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, simple_vs, &hVertexShader, 0);
1021 ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1022 /* Msdn says that the new vertex shader is set immediately. This is wrong, apparently */
1023 hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1024 ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1025 ok(hTempHandle == 0, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, 0);
1026 /* Assign the shader, then verify that GetVertexShader works */
1027 hr = IDirect3DDevice8_SetVertexShader(pDevice, hVertexShader);
1028 ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1029 hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1030 ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1031 ok(hTempHandle == hVertexShader, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, hVertexShader);
1032 /* Verify that we can retrieve the declaration */
1033 hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, NULL, &data_size);
1034 ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderDeclaration returned %#08x\n", hr);
1035 ok(data_size == vertex_decl_size, "Got data_size %u, expected %u\n", data_size, vertex_decl_size);
1036 data = HeapAlloc(GetProcessHeap(), 0, vertex_decl_size);
1037 data_size = 1;
1038 hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, data, &data_size);
1039 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetVertexShaderDeclaration returned (%#08x), "
1040 "expected D3DERR_INVALIDCALL\n", hr);
1041 ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
1042 data_size = vertex_decl_size;
1043 hr = IDirect3DDevice8_GetVertexShaderDeclaration(pDevice, hVertexShader, data, &data_size);
1044 ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderDeclaration returned %#08x\n", hr);
1045 ok(data_size == vertex_decl_size, "Got data_size %u, expected %u\n", data_size, vertex_decl_size);
1046 ok(!memcmp(data, dwVertexDecl, vertex_decl_size), "data not equal to shader declaration\n");
1047 HeapFree(GetProcessHeap(), 0, data);
1048 /* Verify that we can retrieve the shader function */
1049 hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, NULL, &data_size);
1050 ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderFunction returned %#08x\n", hr);
1051 ok(data_size == simple_vs_size, "Got data_size %u, expected %u\n", data_size, simple_vs_size);
1052 data = HeapAlloc(GetProcessHeap(), 0, simple_vs_size);
1053 data_size = 1;
1054 hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, data, &data_size);
1055 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetVertexShaderFunction returned (%#08x), "
1056 "expected D3DERR_INVALIDCALL\n", hr);
1057 ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
1058 data_size = simple_vs_size;
1059 hr = IDirect3DDevice8_GetVertexShaderFunction(pDevice, hVertexShader, data, &data_size);
1060 ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShaderFunction returned %#08x\n", hr);
1061 ok(data_size == simple_vs_size, "Got data_size %u, expected %u\n", data_size, simple_vs_size);
1062 ok(!memcmp(data, simple_vs, simple_vs_size), "data not equal to shader function\n");
1063 HeapFree(GetProcessHeap(), 0, data);
1064 /* Delete the assigned shader. This is supposed to work */
1065 hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1066 ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1067 /* The shader should be unset now */
1068 hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1069 ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1070 ok(hTempHandle == 0, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, 0);
1072 /* Test a broken declaration. 3DMark2001 tries to use normals with 2 components
1073 * First try the fixed function shader function, then a custom one
1075 hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float2, 0, &hVertexShader, 0);
1076 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1077 if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1078 hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float4, 0, &hVertexShader, 0);
1079 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1080 if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1081 hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_d3dcolor, 0, &hVertexShader, 0);
1082 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1083 if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1085 hr = IDirect3DDevice8_CreateVertexShader(pDevice, decl_normal_float2, simple_vs, &hVertexShader, 0);
1086 ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1087 if(SUCCEEDED(hr)) IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1089 if (caps.PixelShaderVersion >= D3DPS_VERSION(1, 0))
1091 /* The same with a pixel shader */
1092 hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader);
1093 ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1094 /* Msdn says that the new pixel shader is set immediately. This is wrong, apparently */
1095 hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1096 ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1097 ok(hTempHandle == 0, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, 0);
1098 /* Assign the shader, then verify that GetPixelShader works */
1099 hr = IDirect3DDevice8_SetPixelShader(pDevice, hPixelShader);
1100 ok(hr == D3D_OK, "IDirect3DDevice8_SetPixelShader returned %#08x\n", hr);
1101 hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1102 ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1103 ok(hTempHandle == hPixelShader, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, hPixelShader);
1104 /* Verify that we can retrieve the shader function */
1105 hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, NULL, &data_size);
1106 ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShaderFunction returned %#08x\n", hr);
1107 ok(data_size == simple_ps_size, "Got data_size %u, expected %u\n", data_size, simple_ps_size);
1108 data = HeapAlloc(GetProcessHeap(), 0, simple_ps_size);
1109 data_size = 1;
1110 hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, data, &data_size);
1111 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_GetPixelShaderFunction returned (%#08x), "
1112 "expected D3DERR_INVALIDCALL\n", hr);
1113 ok(data_size == 1, "Got data_size %u, expected 1\n", data_size);
1114 data_size = simple_ps_size;
1115 hr = IDirect3DDevice8_GetPixelShaderFunction(pDevice, hPixelShader, data, &data_size);
1116 ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShaderFunction returned %#08x\n", hr);
1117 ok(data_size == simple_ps_size, "Got data_size %u, expected %u\n", data_size, simple_ps_size);
1118 ok(!memcmp(data, simple_ps, simple_ps_size), "data not equal to shader function\n");
1119 HeapFree(GetProcessHeap(), 0, data);
1120 /* Delete the assigned shader. This is supposed to work */
1121 hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1122 ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1123 /* The shader should be unset now */
1124 hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1125 ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1126 ok(hTempHandle == 0, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, 0);
1128 /* What happens if a non-bound shader is deleted? */
1129 hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader);
1130 ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1131 hr = IDirect3DDevice8_CreatePixelShader(pDevice, simple_ps, &hPixelShader2);
1132 ok(hr == D3D_OK, "IDirect3DDevice8_CreatePixelShader returned %#08x\n", hr);
1134 hr = IDirect3DDevice8_SetPixelShader(pDevice, hPixelShader);
1135 ok(hr == D3D_OK, "IDirect3DDevice8_SetPixelShader returned %#08x\n", hr);
1136 hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader2);
1137 ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1138 hr = IDirect3DDevice8_GetPixelShader(pDevice, &hTempHandle);
1139 ok(hr == D3D_OK, "IDirect3DDevice8_GetPixelShader returned %#08x\n", hr);
1140 ok(hTempHandle == hPixelShader, "Pixel Shader %d is set, expected shader %d\n", hTempHandle, hPixelShader);
1141 hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1142 ok(hr == D3D_OK, "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1144 /* Check for double delete. */
1145 hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader2);
1146 ok(hr == D3DERR_INVALIDCALL || broken(hr == D3D_OK), "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1147 hr = IDirect3DDevice8_DeletePixelShader(pDevice, hPixelShader);
1148 ok(hr == D3DERR_INVALIDCALL || broken(hr == D3D_OK), "IDirect3DDevice8_DeletePixelShader returned %#08x\n", hr);
1150 else
1152 skip("Pixel shaders not supported\n");
1155 /* What happens if a non-bound shader is deleted? */
1156 hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, NULL, &hVertexShader, 0);
1157 ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1158 hr = IDirect3DDevice8_CreateVertexShader(pDevice, dwVertexDecl, NULL, &hVertexShader2, 0);
1159 ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned %#08x\n", hr);
1161 hr = IDirect3DDevice8_SetVertexShader(pDevice, hVertexShader);
1162 ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1163 hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader2);
1164 ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1165 hr = IDirect3DDevice8_GetVertexShader(pDevice, &hTempHandle);
1166 ok(hr == D3D_OK, "IDirect3DDevice8_GetVertexShader returned %#08x\n", hr);
1167 ok(hTempHandle == hVertexShader, "Vertex Shader %d is set, expected shader %d\n", hTempHandle, hVertexShader);
1168 hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1169 ok(hr == D3D_OK, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1171 /* Check for double delete. */
1172 hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader2);
1173 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1174 hr = IDirect3DDevice8_DeleteVertexShader(pDevice, hVertexShader);
1175 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice8_DeleteVertexShader returned %#08x\n", hr);
1177 cleanup:
1178 if (pDevice)
1180 UINT refcount = IDirect3DDevice8_Release(pDevice);
1181 ok(!refcount, "Device has %u references left.\n", refcount);
1183 if (pD3d) IDirect3D8_Release(pD3d);
1184 if(hwnd) DestroyWindow(hwnd);
1187 static void test_limits(void)
1189 HRESULT hr;
1190 HWND hwnd = NULL;
1191 IDirect3D8 *pD3d = NULL;
1192 IDirect3DDevice8 *pDevice = NULL;
1193 D3DPRESENT_PARAMETERS d3dpp;
1194 D3DDISPLAYMODE d3ddm;
1195 IDirect3DTexture8 *pTexture = NULL;
1196 int i;
1198 pD3d = pDirect3DCreate8( D3D_SDK_VERSION );
1199 ok(pD3d != NULL, "Failed to create IDirect3D8 object\n");
1200 hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1201 ok(hwnd != NULL, "Failed to create window\n");
1202 if (!pD3d || !hwnd) goto cleanup;
1204 IDirect3D8_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
1205 ZeroMemory( &d3dpp, sizeof(d3dpp) );
1206 d3dpp.Windowed = TRUE;
1207 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1208 d3dpp.BackBufferWidth = 800;
1209 d3dpp.BackBufferHeight = 600;
1210 d3dpp.BackBufferFormat = d3ddm.Format;
1211 d3dpp.EnableAutoDepthStencil = TRUE;
1212 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1214 hr = IDirect3D8_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1215 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
1216 ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL || broken(hr == D3DERR_NOTAVAILABLE), "IDirect3D8_CreateDevice failed with %#08x\n", hr);
1217 if(!pDevice)
1219 skip("could not create device, IDirect3D8_CreateDevice returned %#08x\n", hr);
1220 goto cleanup;
1223 hr = IDirect3DDevice8_CreateTexture(pDevice, 16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture);
1224 ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed with %#08x\n", hr);
1225 if(!pTexture) goto cleanup;
1227 /* There are 8 texture stages. We should be able to access all of them */
1228 for(i = 0; i < 8; i++) {
1229 hr = IDirect3DDevice8_SetTexture(pDevice, i, (IDirect3DBaseTexture8 *) pTexture);
1230 ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %#08x\n", i, hr);
1231 hr = IDirect3DDevice8_SetTexture(pDevice, i, NULL);
1232 ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %#08x\n", i, hr);
1233 hr = IDirect3DDevice8_SetTextureStageState(pDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
1234 ok(hr == D3D_OK, "IDirect3DDevice8_SetTextureStageState for texture %d failed with %#08x\n", i, hr);
1237 /* Investigations show that accessing higher textures stage states does not return an error either. Writing
1238 * to too high texture stages(approximately texture 40) causes memory corruption in windows, so there is no
1239 * bounds checking but how do I test that?
1242 cleanup:
1243 if(pTexture) IDirect3DTexture8_Release(pTexture);
1244 if (pDevice)
1246 UINT refcount = IDirect3DDevice8_Release(pDevice);
1247 ok(!refcount, "Device has %u references left.\n", refcount);
1249 if (pD3d) IDirect3D8_Release(pD3d);
1250 if(hwnd) DestroyWindow(hwnd);
1253 static void test_lights(void)
1255 D3DPRESENT_PARAMETERS d3dpp;
1256 IDirect3DDevice8 *device = NULL;
1257 IDirect3D8 *d3d8;
1258 HWND hwnd;
1259 HRESULT hr;
1260 unsigned int i;
1261 BOOL enabled;
1262 D3DCAPS8 caps;
1263 D3DDISPLAYMODE d3ddm;
1265 d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1266 ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1267 hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1268 ok(hwnd != NULL, "Failed to create window\n");
1269 if (!d3d8 || !hwnd) goto cleanup;
1271 IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &d3ddm );
1272 ZeroMemory( &d3dpp, sizeof(d3dpp) );
1273 d3dpp.Windowed = TRUE;
1274 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1275 d3dpp.BackBufferWidth = 800;
1276 d3dpp.BackBufferHeight = 600;
1277 d3dpp.BackBufferFormat = d3ddm.Format;
1278 d3dpp.EnableAutoDepthStencil = TRUE;
1279 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1281 hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1282 D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &device );
1283 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1284 "IDirect3D8_CreateDevice failed with %08x\n", hr);
1285 if(!device)
1287 skip("Failed to create a d3d device\n");
1288 goto cleanup;
1291 memset(&caps, 0, sizeof(caps));
1292 hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1293 ok(hr == D3D_OK, "IDirect3DDevice8_GetDeviceCaps failed with %08x\n", hr);
1295 for(i = 1; i <= caps.MaxActiveLights; i++) {
1296 hr = IDirect3DDevice8_LightEnable(device, i, TRUE);
1297 ok(hr == D3D_OK, "Enabling light %u failed with %08x\n", i, hr);
1298 hr = IDirect3DDevice8_GetLightEnable(device, i, &enabled);
1299 ok(hr == D3D_OK || broken(hr == D3DERR_INVALIDCALL),
1300 "GetLightEnable on light %u failed with %08x\n", i, hr);
1301 ok(enabled, "Light %d is %s\n", i, enabled ? "enabled" : "disabled");
1304 /* TODO: Test the rendering results in this situation */
1305 hr = IDirect3DDevice8_LightEnable(device, i + 1, TRUE);
1306 ok(hr == D3D_OK ||
1307 broken(hr == D3DERR_INVALIDCALL), /* Some Win9x and WinME */
1308 "Enabling one light more than supported returned %08x\n", hr);
1309 hr = IDirect3DDevice8_GetLightEnable(device, i + 1, &enabled);
1310 ok(hr == D3D_OK ||
1311 broken(hr == D3DERR_INVALIDCALL), /* Some Win9x and WinME */
1312 "GetLightEnable on light %u failed with %08x\n", i + 1, hr);
1313 ok(enabled, "Light %d is %s\n", i + 1, enabled ? "enabled" : "disabled");
1314 hr = IDirect3DDevice8_LightEnable(device, i + 1, FALSE);
1315 ok(hr == D3D_OK, "Disabling the additional returned %08x\n", hr);
1317 for(i = 1; i <= caps.MaxActiveLights; i++) {
1318 hr = IDirect3DDevice8_LightEnable(device, i, FALSE);
1319 ok(hr == D3D_OK, "Disabling light %u failed with %08x\n", i, hr);
1322 cleanup:
1323 if (device)
1325 UINT refcount = IDirect3DDevice8_Release(device);
1326 ok(!refcount, "Device has %u references left.\n", refcount);
1328 if (d3d8) IDirect3D8_Release(d3d8);
1331 static void test_render_zero_triangles(void)
1333 D3DPRESENT_PARAMETERS d3dpp;
1334 IDirect3DDevice8 *device = NULL;
1335 IDirect3D8 *d3d8;
1336 HWND hwnd;
1337 HRESULT hr;
1338 D3DDISPLAYMODE d3ddm;
1340 struct nvertex
1342 float x, y, z;
1343 float nx, ny, nz;
1344 DWORD diffuse;
1345 } quad[] =
1347 { 0.0f, -1.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
1348 { 0.0f, 0.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
1349 { 1.0f, 0.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
1350 { 1.0f, -1.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
1353 d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
1354 ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1355 hwnd = CreateWindow( "static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1356 ok(hwnd != NULL, "Failed to create window\n");
1357 if (!d3d8 || !hwnd) goto cleanup;
1359 IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &d3ddm );
1360 ZeroMemory( &d3dpp, sizeof(d3dpp) );
1361 d3dpp.Windowed = TRUE;
1362 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1363 d3dpp.BackBufferWidth = 800;
1364 d3dpp.BackBufferHeight = 600;
1365 d3dpp.BackBufferFormat = d3ddm.Format;
1366 d3dpp.EnableAutoDepthStencil = TRUE;
1367 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
1369 hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL /* no NULLREF here */, hwnd,
1370 D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &device );
1371 ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE || hr == D3DERR_INVALIDCALL,
1372 "IDirect3D8_CreateDevice failed with %08x\n", hr);
1373 if(!device)
1375 skip("Failed to create a d3d device\n");
1376 goto cleanup;
1379 hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
1380 ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
1382 hr = IDirect3DDevice8_BeginScene(device);
1383 ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
1384 if(hr == D3D_OK)
1386 hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 0 /* NumVerts */,
1387 0 /*PrimCount */, NULL, D3DFMT_INDEX16, quad, sizeof(quad[0]));
1388 ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
1390 IDirect3DDevice8_EndScene(device);
1391 ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
1394 IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1396 cleanup:
1397 if (device)
1399 UINT refcount = IDirect3DDevice8_Release(device);
1400 ok(!refcount, "Device has %u references left.\n", refcount);
1402 if (d3d8) IDirect3D8_Release(d3d8);
1405 static void test_depth_stencil_reset(void)
1407 D3DPRESENT_PARAMETERS present_parameters;
1408 D3DDISPLAYMODE display_mode;
1409 IDirect3DSurface8 *surface;
1410 IDirect3DDevice8 *device = NULL;
1411 IDirect3D8 *d3d8;
1412 UINT refcount;
1413 HRESULT hr;
1414 HWND hwnd;
1416 d3d8 = pDirect3DCreate8(D3D_SDK_VERSION);
1417 ok(d3d8 != NULL, "Failed to create IDirect3D8 object\n");
1418 hwnd = CreateWindow("static", "d3d8_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL);
1419 ok(hwnd != NULL, "Failed to create window\n");
1420 if (!d3d8 || !hwnd) goto cleanup;
1422 IDirect3D8_GetAdapterDisplayMode(d3d8, D3DADAPTER_DEFAULT, &display_mode);
1423 memset(&present_parameters, 0, sizeof(present_parameters));
1424 present_parameters.Windowed = TRUE;
1425 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1426 present_parameters.BackBufferFormat = display_mode.Format;
1427 present_parameters.EnableAutoDepthStencil = TRUE;
1428 present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1430 hr = IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1431 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device);
1432 if(FAILED(hr))
1434 skip("could not create device, IDirect3D8_CreateDevice returned %#x\n", hr);
1435 goto cleanup;
1438 hr = IDirect3DDevice8_TestCooperativeLevel(device);
1439 ok(SUCCEEDED(hr), "TestCooperativeLevel failed with %#x\n", hr);
1441 hr = IDirect3DDevice8_SetRenderTarget(device, NULL, NULL);
1442 ok(hr == D3D_OK, "SetRenderTarget failed with 0x%08x\n", hr);
1444 hr = IDirect3DDevice8_GetRenderTarget(device, &surface);
1445 ok(hr == D3D_OK, "GetRenderTarget failed with 0x%08x\n", hr);
1446 ok(surface != NULL, "Render target should not be NULL\n");
1447 if (surface) IDirect3DSurface8_Release(surface);
1449 hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1450 ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1451 ok(surface == NULL, "Depth stencil should be NULL\n");
1453 present_parameters.EnableAutoDepthStencil = TRUE;
1454 present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1455 hr = IDirect3DDevice8_Reset(device, &present_parameters);
1456 ok(hr == D3D_OK, "Reset failed with 0x%08x\n", hr);
1458 hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1459 ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1460 ok(surface != NULL, "Depth stencil should not be NULL\n");
1461 if (surface) IDirect3DSurface8_Release(surface);
1463 present_parameters.EnableAutoDepthStencil = FALSE;
1464 hr = IDirect3DDevice8_Reset(device, &present_parameters);
1465 ok(hr == D3D_OK, "Reset failed with 0x%08x\n", hr);
1467 hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1468 ok(hr == D3DERR_NOTFOUND, "GetDepthStencilSurface returned 0x%08x, expected D3DERR_NOTFOUND\n", hr);
1469 ok(surface == NULL, "Depth stencil should be NULL\n");
1471 refcount = IDirect3DDevice8_Release(device);
1472 ok(!refcount, "Device has %u references left.\n", refcount);
1473 device = NULL;
1475 IDirect3D8_GetAdapterDisplayMode( d3d8, D3DADAPTER_DEFAULT, &display_mode );
1477 ZeroMemory( &present_parameters, sizeof(present_parameters) );
1478 present_parameters.Windowed = TRUE;
1479 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1480 present_parameters.BackBufferFormat = display_mode.Format;
1481 present_parameters.EnableAutoDepthStencil = FALSE;
1482 present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1484 hr = IDirect3D8_CreateDevice( d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
1485 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device );
1487 if(FAILED(hr))
1489 skip("could not create device, IDirect3D8_CreateDevice returned %#x\n", hr);
1490 goto cleanup;
1493 hr = IDirect3DDevice8_TestCooperativeLevel(device);
1494 ok(hr == D3D_OK, "IDirect3DDevice8_TestCooperativeLevel after creation returned %#x\n", hr);
1496 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1497 present_parameters.Windowed = TRUE;
1498 present_parameters.BackBufferWidth = 400;
1499 present_parameters.BackBufferHeight = 300;
1500 present_parameters.EnableAutoDepthStencil = TRUE;
1501 present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
1503 hr = IDirect3DDevice8_Reset(device, &present_parameters);
1504 ok(hr == D3D_OK, "IDirect3DDevice8_Reset failed with 0x%08x\n", hr);
1506 if (FAILED(hr)) goto cleanup;
1508 hr = IDirect3DDevice8_GetDepthStencilSurface(device, &surface);
1509 ok(hr == D3D_OK, "GetDepthStencilSurface failed with 0x%08x\n", hr);
1510 ok(surface != NULL, "Depth stencil should not be NULL\n");
1511 if (surface) IDirect3DSurface8_Release(surface);
1513 cleanup:
1514 if (device)
1516 refcount = IDirect3DDevice8_Release(device);
1517 ok(!refcount, "Device has %u references left.\n", refcount);
1519 if (d3d8) IDirect3D8_Release(d3d8);
1522 static HWND filter_messages;
1523 static struct
1525 HWND window;
1526 UINT message;
1527 } expect_message;
1529 struct wndproc_thread_param
1531 HWND dummy_window;
1532 HANDLE window_created;
1533 HANDLE test_finished;
1536 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
1538 if (filter_messages && filter_messages == hwnd)
1540 if (message != WM_DISPLAYCHANGE && message != WM_IME_NOTIFY)
1541 todo_wine ok(0, "Received unexpected message %#x for window %p.\n", message, hwnd);
1544 if (expect_message.window == hwnd && expect_message.message == message) expect_message.message = 0;
1546 return DefWindowProcA(hwnd, message, wparam, lparam);
1549 static DWORD WINAPI wndproc_thread(void *param)
1551 struct wndproc_thread_param *p = param;
1552 DWORD res;
1553 BOOL ret;
1555 p->dummy_window = CreateWindowA("d3d8_test_wndproc_wc", "d3d8_test",
1556 WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, 640, 480, 0, 0, 0, 0);
1558 ret = SetEvent(p->window_created);
1559 ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
1561 for (;;)
1563 MSG msg;
1565 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
1566 res = WaitForSingleObject(p->test_finished, 100);
1567 if (res == WAIT_OBJECT_0) break;
1568 if (res != WAIT_TIMEOUT)
1570 ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
1571 break;
1575 DestroyWindow(p->dummy_window);
1577 return 0;
1580 static void test_wndproc(void)
1582 struct wndproc_thread_param thread_params;
1583 HWND device_window, focus_window, tmp;
1584 IDirect3DDevice8 *device;
1585 WNDCLASSA wc = {0};
1586 IDirect3D8 *d3d8;
1587 HANDLE thread;
1588 LONG_PTR proc;
1589 ULONG ref;
1590 DWORD res, tid;
1592 if (!(d3d8 = pDirect3DCreate8(D3D_SDK_VERSION)))
1594 skip("Failed to create IDirect3D8 object, skipping tests.\n");
1595 return;
1598 wc.lpfnWndProc = test_proc;
1599 wc.lpszClassName = "d3d8_test_wndproc_wc";
1600 ok(RegisterClassA(&wc), "Failed to register window class.\n");
1602 thread_params.window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
1603 ok(!!thread_params.window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
1604 thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
1605 ok(!!thread_params.test_finished, "CreateEvent failed, last error %#x.\n", GetLastError());
1607 focus_window = CreateWindowA("d3d8_test_wndproc_wc", "d3d8_test",
1608 WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
1609 device_window = CreateWindowA("d3d8_test_wndproc_wc", "d3d8_test",
1610 WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
1611 thread = CreateThread(NULL, 0, wndproc_thread, &thread_params, 0, &tid);
1612 ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
1614 res = WaitForSingleObject(thread_params.window_created, INFINITE);
1615 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
1617 proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
1618 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1619 (LONG_PTR)test_proc, proc);
1620 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1621 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1622 (LONG_PTR)test_proc, proc);
1624 trace("device_window %p, focus_window %p, dummy_window %p.\n",
1625 device_window, focus_window, thread_params.dummy_window);
1627 tmp = GetFocus();
1628 ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
1629 tmp = GetForegroundWindow();
1630 ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
1631 thread_params.dummy_window, tmp);
1633 expect_message.window = focus_window;
1634 expect_message.message = WM_SETFOCUS;
1636 flush_events();
1638 device = create_device(d3d8, device_window, focus_window, FALSE);
1639 if (!device)
1641 skip("Failed to create a D3D device, skipping tests.\n");
1642 goto done;
1645 ok(!expect_message.message, "Expected message %#x for window %p, but didn't receive it.\n",
1646 expect_message.message, expect_message.window);
1647 if (0) /* Disabled until we can make this work in a reliable way on Wine. */
1649 tmp = GetFocus();
1650 ok(tmp == focus_window, "Expected focus %p, got %p.\n", focus_window, tmp);
1651 tmp = GetForegroundWindow();
1652 ok(tmp == focus_window, "Expected foreground window %p, got %p.\n", focus_window, tmp);
1654 SetForegroundWindow(focus_window);
1655 flush_events();
1657 filter_messages = focus_window;
1659 proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
1660 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1661 (LONG_PTR)test_proc, proc);
1663 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1664 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
1665 (LONG_PTR)test_proc, proc);
1667 ref = IDirect3DDevice8_Release(device);
1668 ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
1670 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1671 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1672 (LONG_PTR)test_proc, proc);
1674 device = create_device(d3d8, focus_window, focus_window, FALSE);
1675 if (!device)
1677 skip("Failed to create a D3D device, skipping tests.\n");
1678 goto done;
1681 ref = IDirect3DDevice8_Release(device);
1682 ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
1684 device = create_device(d3d8, device_window, focus_window, FALSE);
1685 if (!device)
1687 skip("Failed to create a D3D device, skipping tests.\n");
1688 goto done;
1691 proc = SetWindowLongPtrA(focus_window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
1692 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
1693 (LONG_PTR)test_proc, proc);
1695 ref = IDirect3DDevice8_Release(device);
1696 ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
1698 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1699 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
1700 (LONG_PTR)DefWindowProcA, proc);
1702 done:
1703 filter_messages = NULL;
1704 IDirect3D8_Release(d3d8);
1706 SetEvent(thread_params.test_finished);
1707 WaitForSingleObject(thread, INFINITE);
1708 CloseHandle(thread_params.test_finished);
1709 CloseHandle(thread_params.window_created);
1710 CloseHandle(thread);
1712 DestroyWindow(device_window);
1713 DestroyWindow(focus_window);
1714 UnregisterClassA("d3d8_test_wndproc_wc", GetModuleHandleA(NULL));
1717 static void test_wndproc_windowed(void)
1719 struct wndproc_thread_param thread_params;
1720 HWND device_window, focus_window, tmp;
1721 IDirect3DDevice8 *device;
1722 WNDCLASSA wc = {0};
1723 IDirect3D8 *d3d8;
1724 HANDLE thread;
1725 LONG_PTR proc;
1726 HRESULT hr;
1727 ULONG ref;
1728 DWORD res, tid;
1730 if (!(d3d8 = pDirect3DCreate8(D3D_SDK_VERSION)))
1732 skip("Failed to create IDirect3D8 object, skipping tests.\n");
1733 return;
1736 wc.lpfnWndProc = test_proc;
1737 wc.lpszClassName = "d3d8_test_wndproc_wc";
1738 ok(RegisterClassA(&wc), "Failed to register window class.\n");
1740 thread_params.window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
1741 ok(!!thread_params.window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
1742 thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
1743 ok(!!thread_params.test_finished, "CreateEvent failed, last error %#x.\n", GetLastError());
1745 focus_window = CreateWindowA("d3d8_test_wndproc_wc", "d3d8_test",
1746 WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, 640, 480, 0, 0, 0, 0);
1747 device_window = CreateWindowA("d3d8_test_wndproc_wc", "d3d8_test",
1748 WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION, 0, 0, 640, 480, 0, 0, 0, 0);
1749 thread = CreateThread(NULL, 0, wndproc_thread, &thread_params, 0, &tid);
1750 ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
1752 res = WaitForSingleObject(thread_params.window_created, INFINITE);
1753 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
1755 proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
1756 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1757 (LONG_PTR)test_proc, proc);
1758 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1759 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1760 (LONG_PTR)test_proc, proc);
1762 trace("device_window %p, focus_window %p, dummy_window %p.\n",
1763 device_window, focus_window, thread_params.dummy_window);
1765 tmp = GetFocus();
1766 ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
1767 tmp = GetForegroundWindow();
1768 ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
1769 thread_params.dummy_window, tmp);
1771 filter_messages = focus_window;
1773 device = create_device(d3d8, device_window, focus_window, TRUE);
1774 if (!device)
1776 skip("Failed to create a D3D device, skipping tests.\n");
1777 goto done;
1780 tmp = GetFocus();
1781 ok(tmp == device_window, "Expected focus %p, got %p.\n", device_window, tmp);
1782 tmp = GetForegroundWindow();
1783 ok(tmp == thread_params.dummy_window, "Expected foreground window %p, got %p.\n",
1784 thread_params.dummy_window, tmp);
1786 proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
1787 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1788 (LONG_PTR)test_proc, proc);
1790 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1791 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1792 (LONG_PTR)test_proc, proc);
1794 filter_messages = NULL;
1796 hr = reset_device(device, device_window, FALSE);
1797 ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
1799 proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
1800 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1801 (LONG_PTR)test_proc, proc);
1803 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1804 ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1805 (LONG_PTR)test_proc, proc);
1807 hr = reset_device(device, device_window, TRUE);
1808 ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
1810 proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
1811 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1812 (LONG_PTR)test_proc, proc);
1814 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1815 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1816 (LONG_PTR)test_proc, proc);
1818 filter_messages = focus_window;
1820 ref = IDirect3DDevice8_Release(device);
1821 ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
1823 filter_messages = device_window;
1825 device = create_device(d3d8, focus_window, focus_window, TRUE);
1826 if (!device)
1828 skip("Failed to create a D3D device, skipping tests.\n");
1829 goto done;
1832 filter_messages = NULL;
1834 hr = reset_device(device, focus_window, FALSE);
1835 ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
1837 proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
1838 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1839 (LONG_PTR)test_proc, proc);
1841 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1842 ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1843 (LONG_PTR)test_proc, proc);
1845 hr = reset_device(device, focus_window, TRUE);
1846 ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
1848 proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
1849 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1850 (LONG_PTR)test_proc, proc);
1852 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1853 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1854 (LONG_PTR)test_proc, proc);
1856 filter_messages = device_window;
1858 ref = IDirect3DDevice8_Release(device);
1859 ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
1861 device = create_device(d3d8, device_window, focus_window, TRUE);
1862 if (!device)
1864 skip("Failed to create a D3D device, skipping tests.\n");
1865 goto done;
1868 filter_messages = NULL;
1870 hr = reset_device(device, device_window, FALSE);
1871 ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
1873 proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
1874 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1875 (LONG_PTR)test_proc, proc);
1877 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1878 ok(proc != (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1879 (LONG_PTR)test_proc, proc);
1881 hr = reset_device(device, device_window, TRUE);
1882 ok(SUCCEEDED(hr), "Failed to reset device, hr %#x.\n", hr);
1884 proc = GetWindowLongPtrA(device_window, GWLP_WNDPROC);
1885 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1886 (LONG_PTR)test_proc, proc);
1888 proc = GetWindowLongPtrA(focus_window, GWLP_WNDPROC);
1889 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1890 (LONG_PTR)test_proc, proc);
1892 filter_messages = device_window;
1894 ref = IDirect3DDevice8_Release(device);
1895 ok(ref == 0, "The device was not properly freed: refcount %u.\n", ref);
1897 done:
1898 filter_messages = NULL;
1899 IDirect3D8_Release(d3d8);
1901 SetEvent(thread_params.test_finished);
1902 WaitForSingleObject(thread, INFINITE);
1903 CloseHandle(thread_params.test_finished);
1904 CloseHandle(thread_params.window_created);
1905 CloseHandle(thread);
1907 DestroyWindow(device_window);
1908 DestroyWindow(focus_window);
1909 UnregisterClassA("d3d8_test_wndproc_wc", GetModuleHandleA(NULL));
1912 static inline void set_fpu_cw(WORD cw)
1914 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1915 __asm__ volatile ("fnclex");
1916 __asm__ volatile ("fldcw %0" : : "m" (cw));
1917 #endif
1920 static inline WORD get_fpu_cw(void)
1922 WORD cw = 0;
1923 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1924 __asm__ volatile ("fnstcw %0" : "=m" (cw));
1925 #endif
1926 return cw;
1929 static void test_fpu_setup(void)
1931 D3DPRESENT_PARAMETERS present_parameters;
1932 IDirect3DDevice8 *device;
1933 D3DDISPLAYMODE d3ddm;
1934 HWND window = NULL;
1935 IDirect3D8 *d3d8;
1936 HRESULT hr;
1937 WORD cw;
1939 d3d8 = pDirect3DCreate8(D3D_SDK_VERSION);
1940 ok(!!d3d8, "Failed to create a d3d8 object.\n");
1941 if (!d3d8) return;
1943 window = CreateWindowA("static", "d3d8_test", WS_CAPTION, 0, 0, 640, 480, 0, 0, 0, 0);
1944 ok(!!window, "Failed to create a window.\n");
1945 if (!window) goto done;
1947 hr = IDirect3D8_GetAdapterDisplayMode(d3d8, D3DADAPTER_DEFAULT, &d3ddm);
1948 ok(SUCCEEDED(hr), "GetAdapterDisplayMode failed, hr %#x.\n", hr);
1950 memset(&present_parameters, 0, sizeof(present_parameters));
1951 present_parameters.Windowed = TRUE;
1952 present_parameters.hDeviceWindow = window;
1953 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1954 present_parameters.BackBufferFormat = d3ddm.Format;
1956 set_fpu_cw(0xf60);
1957 cw = get_fpu_cw();
1958 ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
1960 hr = IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
1961 D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device);
1962 if (FAILED(hr))
1964 skip("Failed to create a device, hr %#x.\n", hr);
1965 set_fpu_cw(0x37f);
1966 goto done;
1969 cw = get_fpu_cw();
1970 ok(cw == 0x7f, "cw is %#x, expected 0x7f.\n", cw);
1972 IDirect3DDevice8_Release(device);
1974 cw = get_fpu_cw();
1975 ok(cw == 0x7f, "cw is %#x, expected 0x7f.\n", cw);
1976 set_fpu_cw(0xf60);
1977 cw = get_fpu_cw();
1978 ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
1980 hr = IDirect3D8_CreateDevice(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window,
1981 D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &present_parameters, &device);
1982 ok(SUCCEEDED(hr), "CreateDevice failed, hr %#x.\n", hr);
1984 cw = get_fpu_cw();
1985 ok(cw == 0xf60, "cw is %#x, expected 0xf60.\n", cw);
1986 set_fpu_cw(0x37f);
1988 IDirect3DDevice8_Release(device);
1990 done:
1991 if (window) DestroyWindow(window);
1992 if (d3d8) IDirect3D8_Release(d3d8);
1995 START_TEST(device)
1997 HMODULE d3d8_handle = LoadLibraryA( "d3d8.dll" );
1998 if (!d3d8_handle)
2000 skip("Could not load d3d8.dll\n");
2001 return;
2004 pDirect3DCreate8 = (void *)GetProcAddress( d3d8_handle, "Direct3DCreate8" );
2005 ok(pDirect3DCreate8 != NULL, "Failed to get address of Direct3DCreate8\n");
2006 if (pDirect3DCreate8)
2008 IDirect3D8 *d3d8;
2009 d3d8 = pDirect3DCreate8( D3D_SDK_VERSION );
2010 if(!d3d8)
2012 skip("could not create D3D8\n");
2013 return;
2015 IDirect3D8_Release(d3d8);
2017 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2018 test_fpu_setup();
2019 #endif
2020 test_display_modes();
2021 test_shader_versions();
2022 test_swapchain();
2023 test_refcount();
2024 test_mipmap_levels();
2025 test_cursor();
2026 test_states();
2027 test_scene();
2028 test_shader();
2029 test_limits();
2030 test_lights();
2031 test_render_zero_triangles();
2032 test_depth_stencil_reset();
2033 test_wndproc();
2034 test_wndproc_windowed();