2 * Copyright (C) 2007 Stefan Dösinger(for CodeWeavers)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 /* See comment in dlls/d3d9/tests/visual.c for general guidelines */
22 #include "wine/test.h"
27 IDirectDraw7 *DirectDraw = NULL;
28 IDirectDrawSurface7 *Surface;
29 IDirect3D7 *Direct3D = NULL;
30 IDirect3DDevice7 *Direct3DDevice = NULL;
32 static HRESULT (WINAPI *pDirectDrawCreateEx)(LPGUID,LPVOID*,REFIID,LPUNKNOWN);
34 static BOOL createObjects()
37 HMODULE hmod = GetModuleHandleA("ddraw.dll");
42 if(!hmod) return FALSE;
43 pDirectDrawCreateEx = (void*)GetProcAddress(hmod, "DirectDrawCreateEx");
44 if(!pDirectDrawCreateEx) return FALSE;
46 hr = pDirectDrawCreateEx(NULL, (void **) &DirectDraw, &IID_IDirectDraw7, NULL);
47 ok(hr==DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", hr);
48 if(!DirectDraw) goto err;
50 wc.lpfnWndProc = &DefWindowProc;
51 wc.lpszClassName = "d3d7_test_wc";
53 window = CreateWindow("d3d7_test_wc", "d3d7_test", WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
55 hr = IDirectDraw7_SetCooperativeLevel(DirectDraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
56 ok(hr == DD_OK, "IDirectDraw7_SetCooperativeLevel failed with %08x\n", hr);
57 if(FAILED(hr)) goto err;
58 hr = IDirectDraw7_SetDisplayMode(DirectDraw, 640, 480, 32, 0, 0);
60 /* 24 bit is fine too */
61 hr = IDirectDraw7_SetDisplayMode(DirectDraw, 640, 480, 24, 0, 0);
64 ok(hr == DD_OK, "IDirectDraw7_SetDisplayMode failed with %08x\n", hr);
65 if(FAILED(hr)) goto err;
67 hr = IDirectDraw7_QueryInterface(DirectDraw, &IID_IDirect3D7, (void**) &Direct3D);
68 if (hr == E_NOINTERFACE) goto err;
69 ok(hr==DD_OK, "QueryInterface returned: %08x\n", hr);
71 /* DirectDraw Flipping behavior doesn't seem that well-defined. The reference rasterizer behaves differently
72 * than hardware implementations. Request single buffering, that seems to work everywhere
74 memset(&ddsd, 0, sizeof(ddsd));
75 ddsd.dwSize = sizeof(ddsd);
76 ddsd.dwFlags = DDSD_CAPS;
77 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE;
78 ddsd.dwBackBufferCount = 1;
79 hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &Surface, NULL);
80 ok(hr==DD_OK, "CreateSurface returned: %08x\n", hr);
81 if(!Surface) goto err;
83 hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DTnLHalDevice, Surface, &Direct3DDevice);
86 trace("Creating a TnLHal Device failed, trying HAL\n");
87 hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DHALDevice, Surface, &Direct3DDevice);
90 trace("Creating a HAL device failed, trying Ref\n");
91 hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DRefDevice, Surface, &Direct3DDevice);
94 ok(hr == D3D_OK, "IDirect3D7_CreateDevice failed with %08x\n", hr);
95 if(!Direct3DDevice) goto err;
99 if(DirectDraw) IDirectDraw7_Release(DirectDraw);
100 if(Surface) IDirectDrawSurface7_Release(Surface);
101 if(Direct3D) IDirect3D7_Release(Direct3D);
102 if(Direct3DDevice) IDirect3DDevice7_Release(Direct3DDevice);
103 if(window) DestroyWindow(window);
107 static void releaseObjects()
109 IDirect3DDevice7_Release(Direct3DDevice);
110 IDirect3D7_Release(Direct3D);
111 IDirectDrawSurface7_Release(Surface);
112 IDirectDraw7_Release(DirectDraw);
113 DestroyWindow(window);
116 static DWORD getPixelColor(IDirect3DDevice7 *device, UINT x, UINT y)
121 RECT rectToLock = {x, y, x+1, y+1};
122 IDirectDrawSurface7 *surf = NULL;
124 /* Some implementations seem to dislike direct locking on the front buffer. Thus copy the front buffer
125 * to an offscreen surface and lock it instead of the front buffer
127 memset(&ddsd, 0, sizeof(ddsd));
128 ddsd.dwSize = sizeof(ddsd);
129 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
130 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
133 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
134 hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &surf, NULL);
135 ok(hr == DD_OK, "IDirectDraw7_CreateSurface failed with %08x\n", hr);
138 trace("cannot create helper surface\n");
142 memset(&ddsd, 0, sizeof(ddsd));
143 ddsd.dwSize = sizeof(ddsd);
144 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
146 hr = IDirectDrawSurface_BltFast(surf, 0, 0, Surface, NULL, 0);
147 ok(hr == DD_OK, "IDirectDrawSurface7_BltFast returned %08x\n", hr);
150 trace("Cannot blit\n");
155 hr = IDirectDrawSurface7_Lock(surf, &rectToLock, &ddsd, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
158 trace("Can't lock the offscreen surface, hr=%08x\n", hr);
163 /* Remove the X channel for now. DirectX and OpenGL have different ideas how to treat it apparently, and it isn't
164 * really important for these tests
166 ret = ((DWORD *) ddsd.lpSurface)[0] & 0x00ffffff;
167 hr = IDirectDrawSurface7_Unlock(surf, &rectToLock);
170 trace("Can't unlock the offscreen surface, hr=%08x\n", hr);
174 IDirectDrawSurface7_Release(surf);
191 static void lighting_test(IDirect3DDevice7 *device)
194 DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
195 DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
198 float mat[16] = { 1.0, 0.0, 0.0, 0.0,
201 0.0, 0.0, 0.0, 1.0 };
203 struct vertex unlitquad[] =
205 {-1.0, -1.0, 0.1, 0xffff0000},
206 {-1.0, 0.0, 0.1, 0xffff0000},
207 { 0.0, 0.0, 0.1, 0xffff0000},
208 { 0.0, -1.0, 0.1, 0xffff0000},
210 struct vertex litquad[] =
212 {-1.0, 0.0, 0.1, 0xff00ff00},
213 {-1.0, 1.0, 0.1, 0xff00ff00},
214 { 0.0, 1.0, 0.1, 0xff00ff00},
215 { 0.0, 0.0, 0.1, 0xff00ff00},
217 struct nvertex unlitnquad[] =
219 { 0.0, -1.0, 0.1, 1.0, 1.0, 1.0, 0xff0000ff},
220 { 0.0, 0.0, 0.1, 1.0, 1.0, 1.0, 0xff0000ff},
221 { 1.0, 0.0, 0.1, 1.0, 1.0, 1.0, 0xff0000ff},
222 { 1.0, -1.0, 0.1, 1.0, 1.0, 1.0, 0xff0000ff},
224 struct nvertex litnquad[] =
226 { 0.0, 0.0, 0.1, 1.0, 1.0, 1.0, 0xffffff00},
227 { 0.0, 1.0, 0.1, 1.0, 1.0, 1.0, 0xffffff00},
228 { 1.0, 1.0, 0.1, 1.0, 1.0, 1.0, 0xffffff00},
229 { 1.0, 0.0, 0.1, 1.0, 1.0, 1.0, 0xffffff00},
231 WORD Indices[] = {0, 1, 2, 2, 3, 0};
233 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
234 ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
236 /* Setup some states that may cause issues */
237 hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_WORLD, (D3DMATRIX *) mat);
238 ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
239 hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_VIEW, (D3DMATRIX *)mat);
240 ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
241 hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, (D3DMATRIX *) mat);
242 ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
243 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
244 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
245 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
246 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
247 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
248 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
249 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_STENCILENABLE, FALSE);
250 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
251 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHATESTENABLE, FALSE);
252 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
253 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
254 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
255 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
256 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed with %08x\n", hr);
258 hr = IDirect3DDevice7_BeginScene(device);
259 ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
262 /* No lights are defined... That means, lit vertices should be entirely black */
263 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
264 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
265 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, unlitquad, 4 /* NumVerts */,
266 Indices, 6 /* Indexcount */, 0 /* flags */);
267 ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
269 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
270 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
271 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, litquad, 4 /* NumVerts */,
272 Indices, 6 /* Indexcount */, 0 /* flags */);
273 ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
275 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
276 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
277 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, unlitnquad, 4 /* NumVerts */,
278 Indices, 6 /* Indexcount */, 0 /* flags */);
279 ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
281 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
282 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
283 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, litnquad, 4 /* NumVerts */,
284 Indices, 6 /* Indexcount */, 0 /* flags */);
285 ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
287 IDirect3DDevice7_EndScene(device);
288 ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
291 color = getPixelColor(device, 160, 360); /* lower left quad - unlit without normals */
292 ok(color == 0x00ff0000, "Unlit quad without normals has color %08x\n", color);
293 color = getPixelColor(device, 160, 120); /* upper left quad - lit without normals */
294 ok(color == 0x00000000, "Lit quad without normals has color %08x\n", color);
295 color = getPixelColor(device, 480, 360); /* lower left quad - unlit width normals */
296 ok(color == 0x000000ff, "Unlit quad width normals has color %08x\n", color);
297 color = getPixelColor(device, 480, 120); /* upper left quad - lit width normals */
298 ok(color == 0x00000000, "Lit quad width normals has color %08x\n", color);
300 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
301 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
310 skip("Cannot initialize DirectDraw and Direct3D, skipping\n");
314 /* Check for the reliability of the returned data */
315 hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
318 trace("Clear failed, can't assure correctness of the test results, skipping\n");
322 color = getPixelColor(Direct3DDevice, 1, 1);
323 if(color !=0x00ff0000)
325 trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
329 hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
332 trace("Clear failed, can't assure correctness of the test results, skipping\n");
336 color = getPixelColor(Direct3DDevice, 639, 479);
337 if(color != 0x0000ddee)
339 trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
343 /* Now run the tests */
344 lighting_test(Direct3DDevice);