ddraw/tests: Fix compilation on systems that don't support nameless unions.
[wine/testsucceed.git] / dlls / ddraw / tests / visual.c
blobc9a314738ce245e77aef714942ee041c50333070
1 /*
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 */
21 #include <assert.h>
22 #include "wine/test.h"
23 #include "ddraw.h"
24 #include "d3d.h"
26 HWND window;
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(void)
36 HRESULT hr;
37 HMODULE hmod = GetModuleHandleA("ddraw.dll");
38 WNDCLASS wc = {0};
39 DDSURFACEDESC2 ddsd;
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";
52 RegisterClass(&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);
59 if(FAILED(hr)) {
60 /* 24 bit is fine too */
61 hr = IDirectDraw7_SetDisplayMode(DirectDraw, 640, 480, 24, 0, 0);
64 ok(hr == DD_OK || hr == DDERR_UNSUPPORTED, "IDirectDraw7_SetDisplayMode failed with %08x\n", hr);
65 if(FAILED(hr)) {
66 /* use trace, the caller calls skip() */
67 trace("SetDisplayMode failed\n");
68 goto err;
71 hr = IDirectDraw7_QueryInterface(DirectDraw, &IID_IDirect3D7, (void**) &Direct3D);
72 if (hr == E_NOINTERFACE) goto err;
73 ok(hr==DD_OK, "QueryInterface returned: %08x\n", hr);
75 /* DirectDraw Flipping behavior doesn't seem that well-defined. The reference rasterizer behaves differently
76 * than hardware implementations. Request single buffering, that seems to work everywhere
78 memset(&ddsd, 0, sizeof(ddsd));
79 ddsd.dwSize = sizeof(ddsd);
80 ddsd.dwFlags = DDSD_CAPS;
81 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE;
82 ddsd.dwBackBufferCount = 1;
83 hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &Surface, NULL);
84 ok(hr==DD_OK, "CreateSurface returned: %08x\n", hr);
85 if(!Surface) goto err;
87 hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DTnLHalDevice, Surface, &Direct3DDevice);
88 if(FAILED(hr))
90 trace("Creating a TnLHal Device failed, trying HAL\n");
91 hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DHALDevice, Surface, &Direct3DDevice);
92 if(FAILED(hr))
94 trace("Creating a HAL device failed, trying Ref\n");
95 hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DRefDevice, Surface, &Direct3DDevice);
98 ok(hr == D3D_OK, "IDirect3D7_CreateDevice failed with %08x\n", hr);
99 if(!Direct3DDevice) goto err;
100 return TRUE;
102 err:
103 if(DirectDraw) IDirectDraw7_Release(DirectDraw);
104 if(Surface) IDirectDrawSurface7_Release(Surface);
105 if(Direct3D) IDirect3D7_Release(Direct3D);
106 if(Direct3DDevice) IDirect3DDevice7_Release(Direct3DDevice);
107 if(window) DestroyWindow(window);
108 return FALSE;
111 static void releaseObjects(void)
113 IDirect3DDevice7_Release(Direct3DDevice);
114 IDirect3D7_Release(Direct3D);
115 IDirectDrawSurface7_Release(Surface);
116 IDirectDraw7_Release(DirectDraw);
117 DestroyWindow(window);
120 static DWORD getPixelColor(IDirect3DDevice7 *device, UINT x, UINT y)
122 DWORD ret;
123 HRESULT hr;
124 DDSURFACEDESC2 ddsd;
125 RECT rectToLock = {x, y, x+1, y+1};
126 IDirectDrawSurface7 *surf = NULL;
128 /* Some implementations seem to dislike direct locking on the front buffer. Thus copy the front buffer
129 * to an offscreen surface and lock it instead of the front buffer
131 memset(&ddsd, 0, sizeof(ddsd));
132 ddsd.dwSize = sizeof(ddsd);
133 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
134 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
135 ddsd.dwWidth = 640;
136 ddsd.dwHeight = 480;
137 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
138 hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &surf, NULL);
139 ok(hr == DD_OK, "IDirectDraw7_CreateSurface failed with %08x\n", hr);
140 if(!surf)
142 trace("cannot create helper surface\n");
143 return 0xdeadbeef;
146 memset(&ddsd, 0, sizeof(ddsd));
147 ddsd.dwSize = sizeof(ddsd);
148 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
150 hr = IDirectDrawSurface_BltFast(surf, 0, 0, Surface, NULL, 0);
151 ok(hr == DD_OK, "IDirectDrawSurface7_BltFast returned %08x\n", hr);
152 if(FAILED(hr))
154 trace("Cannot blit\n");
155 ret = 0xdeadbee;
156 goto out;
159 hr = IDirectDrawSurface7_Lock(surf, &rectToLock, &ddsd, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
160 if(FAILED(hr))
162 trace("Can't lock the offscreen surface, hr=%08x\n", hr);
163 ret = 0xdeadbeec;
164 goto out;
167 /* Remove the X channel for now. DirectX and OpenGL have different ideas how to treat it apparently, and it isn't
168 * really important for these tests
170 ret = ((DWORD *) ddsd.lpSurface)[0] & 0x00ffffff;
171 hr = IDirectDrawSurface7_Unlock(surf, &rectToLock);
172 if(FAILED(hr))
174 trace("Can't unlock the offscreen surface, hr=%08x\n", hr);
177 out:
178 IDirectDrawSurface7_Release(surf);
179 return ret;
182 struct vertex
184 float x, y, z;
185 DWORD diffuse;
188 struct nvertex
190 float x, y, z;
191 float nx, ny, nz;
192 DWORD diffuse;
195 static void lighting_test(IDirect3DDevice7 *device)
197 HRESULT hr;
198 DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
199 DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
200 DWORD color;
202 float mat[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
203 0.0f, 1.0f, 0.0f, 0.0f,
204 0.0f, 0.0f, 1.0f, 0.0f,
205 0.0f, 0.0f, 0.0f, 1.0f };
207 struct vertex unlitquad[] =
209 {-1.0f, -1.0f, 0.1f, 0xffff0000},
210 {-1.0f, 0.0f, 0.1f, 0xffff0000},
211 { 0.0f, 0.0f, 0.1f, 0xffff0000},
212 { 0.0f, -1.0f, 0.1f, 0xffff0000},
214 struct vertex litquad[] =
216 {-1.0f, 0.0f, 0.1f, 0xff00ff00},
217 {-1.0f, 1.0f, 0.1f, 0xff00ff00},
218 { 0.0f, 1.0f, 0.1f, 0xff00ff00},
219 { 0.0f, 0.0f, 0.1f, 0xff00ff00},
221 struct nvertex unlitnquad[] =
223 { 0.0f, -1.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
224 { 0.0f, 0.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
225 { 1.0f, 0.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
226 { 1.0f, -1.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
228 struct nvertex litnquad[] =
230 { 0.0f, 0.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xffffff00},
231 { 0.0f, 1.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xffffff00},
232 { 1.0f, 1.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xffffff00},
233 { 1.0f, 0.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xffffff00},
235 WORD Indices[] = {0, 1, 2, 2, 3, 0};
237 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
238 ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
240 /* Setup some states that may cause issues */
241 hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_WORLD, (D3DMATRIX *) mat);
242 ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
243 hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_VIEW, (D3DMATRIX *)mat);
244 ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
245 hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, (D3DMATRIX *) mat);
246 ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
247 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
248 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
249 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
250 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
251 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
252 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
253 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_STENCILENABLE, FALSE);
254 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
255 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHATESTENABLE, FALSE);
256 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
257 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
258 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
259 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
260 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed with %08x\n", hr);
262 hr = IDirect3DDevice7_BeginScene(device);
263 ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
264 if(hr == D3D_OK)
266 /* No lights are defined... That means, lit vertices should be entirely black */
267 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
268 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
269 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, unlitquad, 4 /* NumVerts */,
270 Indices, 6 /* Indexcount */, 0 /* flags */);
271 ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
273 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
274 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
275 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, litquad, 4 /* NumVerts */,
276 Indices, 6 /* Indexcount */, 0 /* flags */);
277 ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
279 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
280 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
281 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, unlitnquad, 4 /* NumVerts */,
282 Indices, 6 /* Indexcount */, 0 /* flags */);
283 ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
285 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
286 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
287 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, litnquad, 4 /* NumVerts */,
288 Indices, 6 /* Indexcount */, 0 /* flags */);
289 ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
291 IDirect3DDevice7_EndScene(device);
292 ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
295 color = getPixelColor(device, 160, 360); /* lower left quad - unlit without normals */
296 ok(color == 0x00ff0000, "Unlit quad without normals has color %08x\n", color);
297 color = getPixelColor(device, 160, 120); /* upper left quad - lit without normals */
298 ok(color == 0x00000000, "Lit quad without normals has color %08x\n", color);
299 color = getPixelColor(device, 480, 360); /* lower left quad - unlit width normals */
300 ok(color == 0x000000ff, "Unlit quad width normals has color %08x\n", color);
301 color = getPixelColor(device, 480, 120); /* upper left quad - lit width normals */
302 ok(color == 0x00000000, "Lit quad width normals has color %08x\n", color);
304 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
305 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
308 static void clear_test(IDirect3DDevice7 *device)
310 /* Tests the correctness of clearing parameters */
311 HRESULT hr;
312 D3DRECT rect[2];
313 D3DRECT rect_negneg;
314 DWORD color;
316 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
317 ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
319 /* Positive x, negative y */
320 U1(rect[0]).x1 = 0;
321 U2(rect[0]).y1 = 480;
322 U3(rect[0]).x2 = 320;
323 U4(rect[0]).y2 = 240;
325 /* Positive x, positive y */
326 U1(rect[1]).x1 = 0;
327 U2(rect[1]).y1 = 0;
328 U3(rect[1]).x2 = 320;
329 U4(rect[1]).y2 = 240;
330 /* Clear 2 rectangles with one call. Shows that a positive value is returned, but the negative rectangle
331 * is ignored, the positive is still cleared afterwards
333 hr = IDirect3DDevice7_Clear(device, 2, rect, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
334 ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
336 /* negative x, negative y */
337 U1(rect_negneg).x1 = 640;
338 U2(rect_negneg).y1 = 240;
339 U3(rect_negneg).x2 = 320;
340 U4(rect_negneg).y2 = 0;
341 hr = IDirect3DDevice7_Clear(device, 1, &rect_negneg, D3DCLEAR_TARGET, 0xff00ff00, 0.0, 0);
342 ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
344 color = getPixelColor(device, 160, 360); /* lower left quad */
345 ok(color == 0x00ffffff, "Clear rectangle 3(pos, neg) has color %08x\n", color);
346 color = getPixelColor(device, 160, 120); /* upper left quad */
347 ok(color == 0x00ff0000, "Clear rectangle 1(pos, pos) has color %08x\n", color);
348 color = getPixelColor(device, 480, 360); /* lower right quad */
349 ok(color == 0x00ffffff, "Clear rectangle 4(NULL) has color %08x\n", color);
350 color = getPixelColor(device, 480, 120); /* upper right quad */
351 ok(color == 0x00ffffff, "Clear rectangle 4(neg, neg) has color %08x\n", color);
354 struct sVertex {
355 float x, y, z;
356 DWORD diffuse;
357 DWORD specular;
360 struct sVertexT {
361 float x, y, z, rhw;
362 DWORD diffuse;
363 DWORD specular;
366 static void fog_test(IDirect3DDevice7 *device)
368 HRESULT hr;
369 DWORD color;
370 float start = 0.0, end = 1.0;
371 D3DDEVICEDESC7 caps;
373 /* Gets full z based fog with linear fog, no fog with specular color */
374 struct sVertex unstransformed_1[] = {
375 {-1, -1, 0.1f, 0xFFFF0000, 0xFF000000 },
376 {-1, 0, 0.1f, 0xFFFF0000, 0xFF000000 },
377 { 0, 0, 0.1f, 0xFFFF0000, 0xFF000000 },
378 { 0, -1, 0.1f, 0xFFFF0000, 0xFF000000 },
380 /* Ok, I am too lazy to deal with transform matrices */
381 struct sVertex unstransformed_2[] = {
382 {-1, 0, 1.0f, 0xFFFF0000, 0xFF000000 },
383 {-1, 1, 1.0f, 0xFFFF0000, 0xFF000000 },
384 { 0, 1, 1.0f, 0xFFFF0000, 0xFF000000 },
385 { 0, 0, 1.0f, 0xFFFF0000, 0xFF000000 },
387 /* Untransformed ones. Give them a different diffuse color to make the test look
388 * nicer. It also makes making sure that they are drawn correctly easier.
390 struct sVertexT transformed_1[] = {
391 {320, 0, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
392 {640, 0, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
393 {640, 240, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
394 {320, 240, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
396 struct sVertexT transformed_2[] = {
397 {320, 240, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
398 {640, 240, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
399 {640, 480, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
400 {320, 480, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
402 WORD Indices[] = {0, 1, 2, 2, 3, 0};
404 memset(&caps, 0, sizeof(caps));
405 hr = IDirect3DDevice7_GetCaps(device, &caps);
406 ok(hr == D3D_OK, "IDirect3DDevice7_GetCaps returned %08x\n", hr);
407 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
408 ok(hr == D3D_OK, "IDirect3DDevice7_Clear returned %08x\n", hr);
410 /* Setup initial states: No lighting, fog on, fog color */
411 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
412 ok(hr == D3D_OK, "Turning off lighting returned %08x\n", hr);
413 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, TRUE);
414 ok(hr == D3D_OK, "Turning on fog calculations returned %08x\n", hr);
415 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGCOLOR, 0xFF00FF00 /* A nice green */);
416 ok(hr == D3D_OK, "Turning on fog calculations returned %08x\n", hr);
418 /* First test: Both table fog and vertex fog off */
419 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGTABLEMODE, D3DFOG_NONE);
420 ok(hr == D3D_OK, "Turning off table fog returned %08x\n", hr);
421 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGVERTEXMODE, D3DFOG_NONE);
422 ok(hr == D3D_OK, "Turning off table fog returned %08x\n", hr);
424 /* Start = 0, end = 1. Should be default, but set them */
425 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGSTART, *((DWORD *) &start));
426 ok(hr == D3D_OK, "Setting fog start returned %08x\n", hr);
427 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGEND, *((DWORD *) &end));
428 ok(hr == D3D_OK, "Setting fog start returned %08x\n", hr);
430 if(IDirect3DDevice7_BeginScene(device) == D3D_OK)
432 /* Untransformed, vertex fog = NONE, table fog = NONE: Read the fog weighting from the specular color */
433 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
434 unstransformed_1, 4, Indices, 6, 0);
435 ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
437 /* That makes it use the Z value */
438 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGVERTEXMODE, D3DFOG_LINEAR);
439 ok(hr == D3D_OK, "Turning off table fog returned %08x\n", hr);
440 /* Untransformed, vertex fog != none (or table fog != none):
441 * Use the Z value as input into the equation
443 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
444 unstransformed_2, 4, Indices, 6, 0);
445 ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
447 /* transformed verts */
448 ok( hr == D3D_OK, "SetFVF returned %08x\n", hr);
449 /* Transformed, vertex fog != NONE, pixel fog == NONE: Use specular color alpha component */
450 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
451 transformed_1, 4, Indices, 6, 0);
452 ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
454 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGTABLEMODE, D3DFOG_LINEAR);
455 ok( hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR returned %08x\n", hr);
456 /* Transformed, table fog != none, vertex anything: Use Z value as input to the fog
457 * equation
459 hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
460 transformed_2, 4, Indices, 6, 0);
461 ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
463 hr = IDirect3DDevice7_EndScene(device);
464 ok(hr == D3D_OK, "EndScene returned %08x\n", hr);
466 else
468 ok(FALSE, "BeginScene failed\n");
471 color = getPixelColor(device, 160, 360);
472 ok(color == 0x00FF0000, "Untransformed vertex with no table or vertex fog has color %08x\n", color);
473 color = getPixelColor(device, 160, 120);
474 ok(color == 0x0000FF00, "Untransformed vertex with linear vertex fog has color %08x\n", color);
475 color = getPixelColor(device, 480, 120);
476 ok(color == 0x00FFFF00, "Transformed vertex with linear vertex fog has color %08x\n", color);
477 if(caps.dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_FOGTABLE)
479 color = getPixelColor(device, 480, 360);
480 ok(color == 0x0000FF00, "Transformed vertex with linear table fog has color %08x\n", color);
482 else
484 /* Without fog table support the vertex fog is still applied, even though table fog is turned on.
485 * The settings above result in no fogging with vertex fog
487 color = getPixelColor(device, 480, 120);
488 ok(color == 0x00FFFF00, "Transformed vertex with linear vertex fog has color %08x\n", color);
489 trace("Info: Table fog not supported by this device\n");
492 /* Turn off the fog master switch to avoid confusing other tests */
493 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
494 ok(hr == D3D_OK, "Turning off fog calculations returned %08x\n", hr);
497 static void offscreen_test(IDirect3DDevice7 *device)
499 HRESULT hr;
500 IDirectDrawSurface7 *backbuffer = NULL, *offscreen = NULL;
501 DWORD color;
502 DDSURFACEDESC2 ddsd;
504 static const float quad[][5] = {
505 {-0.5f, -0.5f, 0.1f, 0.0f, 0.0f},
506 {-0.5f, 0.5f, 0.1f, 0.0f, 1.0f},
507 { 0.5f, -0.5f, 0.1f, 1.0f, 0.0f},
508 { 0.5f, 0.5f, 0.1f, 1.0f, 1.0f},
511 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
512 ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
514 memset(&ddsd, 0, sizeof(ddsd));
515 ddsd.dwSize = sizeof(ddsd);
516 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
517 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
518 ddsd.dwWidth = 128;
519 ddsd.dwHeight = 128;
520 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE;
521 hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &offscreen, NULL);
522 ok(hr == D3D_OK, "Creating the offscreen render target failed, hr = %08x\n", hr);
523 if(!offscreen) {
524 goto out;
527 hr = IDirect3DDevice7_GetRenderTarget(device, &backbuffer);
528 ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
529 if(!backbuffer) {
530 goto out;
533 hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
534 ok(hr == D3D_OK, "SetTextureStageState failed, hr = %08x\n", hr);
535 hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
536 ok(hr == D3D_OK, "SetTextureStageState failed, hr = %08x\n", hr);
537 hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DFILTER_NEAREST);
538 ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (0x%08x)\n", hr);
539 hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DFILTER_NEAREST);
540 ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (0x%08x)\n", hr);
541 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
542 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned hr = %08x\n", hr);
544 if(IDirect3DDevice7_BeginScene(device) == D3D_OK) {
545 hr = IDirect3DDevice7_SetRenderTarget(device, offscreen, 0);
546 ok(hr == D3D_OK, "SetRenderTarget failed, hr = %08x\n", hr);
547 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
548 ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
550 /* Draw without textures - Should resut in a white quad */
551 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEX1, quad, 4, 0);
552 ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
554 hr = IDirect3DDevice7_SetRenderTarget(device, backbuffer, 0);
555 ok(hr == D3D_OK, "SetRenderTarget failed, hr = %08x\n", hr);
556 hr = IDirect3DDevice7_SetTexture(device, 0, offscreen);
557 ok(hr == D3D_OK, "SetTexture failed, %08x\n", hr);
559 /* This time with the texture */
560 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEX1, quad, 4, 0);
561 ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
563 IDirect3DDevice7_EndScene(device);
566 /* Center quad - should be white */
567 color = getPixelColor(device, 320, 240);
568 ok(color == 0x00ffffff, "Offscreen failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
569 /* Some quad in the cleared part of the texture */
570 color = getPixelColor(device, 170, 240);
571 ok(color == 0x00ff00ff, "Offscreen failed: Got color 0x%08x, expected 0x00ff00ff.\n", color);
572 /* Part of the originally cleared back buffer */
573 color = getPixelColor(device, 10, 10);
574 ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
575 if(0) {
576 /* Lower left corner of the screen, where back buffer offscreen rendering draws the offscreen texture.
577 * It should be red, but the offscreen texture may leave some junk there. Not tested yet. Depending on
578 * the offscreen rendering mode this test would succeed or fail
580 color = getPixelColor(device, 10, 470);
581 ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
584 out:
585 hr = IDirect3DDevice7_SetTexture(device, 0, NULL);
587 /* restore things */
588 if(backbuffer) {
589 hr = IDirect3DDevice7_SetRenderTarget(device, backbuffer, 0);
590 IDirectDrawSurface7_Release(backbuffer);
592 if(offscreen) {
593 IDirectDrawSurface7_Release(offscreen);
597 static void alpha_test(IDirect3DDevice7 *device)
599 HRESULT hr;
600 IDirectDrawSurface7 *backbuffer = NULL, *offscreen = NULL;
601 DWORD color, red, green, blue;
602 DDSURFACEDESC2 ddsd;
604 struct vertex quad1[] =
606 {-1.0f, -1.0f, 0.1f, 0x4000ff00},
607 {-1.0f, 0.0f, 0.1f, 0x4000ff00},
608 { 1.0f, -1.0f, 0.1f, 0x4000ff00},
609 { 1.0f, 0.0f, 0.1f, 0x4000ff00},
611 struct vertex quad2[] =
613 {-1.0f, 0.0f, 0.1f, 0xc00000ff},
614 {-1.0f, 1.0f, 0.1f, 0xc00000ff},
615 { 1.0f, 0.0f, 0.1f, 0xc00000ff},
616 { 1.0f, 1.0f, 0.1f, 0xc00000ff},
618 static const float composite_quad[][5] = {
619 { 0.0f, -1.0f, 0.1f, 0.0f, 1.0f},
620 { 0.0f, 1.0f, 0.1f, 0.0f, 0.0f},
621 { 1.0f, -1.0f, 0.1f, 1.0f, 1.0f},
622 { 1.0f, 1.0f, 0.1f, 1.0f, 0.0f},
625 /* Clear the render target with alpha = 0.5 */
626 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
627 ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
629 memset(&ddsd, 0, sizeof(ddsd));
630 ddsd.dwSize = sizeof(ddsd);
631 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
632 ddsd.dwWidth = 128;
633 ddsd.dwHeight = 128;
634 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE;
635 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
636 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
637 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
638 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
639 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
640 U5(U4(ddsd).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
641 hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &offscreen, NULL);
642 ok(hr == D3D_OK, "Creating the offscreen render target failed, hr = %08x\n", hr);
643 if(!offscreen) {
644 goto out;
646 hr = IDirect3DDevice7_GetRenderTarget(device, &backbuffer);
647 ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
648 if(!backbuffer) {
649 goto out;
652 hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
653 ok(hr == D3D_OK, "SetTextureStageState failed, hr = %08x\n", hr);
654 hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
655 ok(hr == D3D_OK, "SetTextureStageState failed, hr = %08x\n", hr);
656 hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DFILTER_NEAREST);
657 ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (0x%08x)\n", hr);
658 hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DFILTER_NEAREST);
659 ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (0x%08x)\n", hr);
661 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
662 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
663 if(IDirect3DDevice7_BeginScene(device) == D3D_OK) {
665 /* Draw two quads, one with src alpha blending, one with dest alpha blending. The
666 * SRCALPHA / INVSRCALPHA blend doesn't give any surprises. Colors are blended based on
667 * the input alpha
669 * The DESTALPHA / INVDESTALPHA do not "work" on the regular buffer because there is no alpha.
670 * They give essentially ZERO and ONE blend factors
672 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
673 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
674 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
675 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
676 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE, quad1, 4, 0);
677 ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
679 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_DESTALPHA);
680 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
681 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVDESTALPHA);
682 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
683 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE, quad2, 4, 0);
684 ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
686 /* Switch to the offscreen buffer, and redo the testing. SRCALPHA and DESTALPHA. The offscreen buffer
687 * has a alpha channel on its own. Clear the offscreen buffer with alpha = 0.5 again, then draw the
688 * quads again. The SRCALPHA/INVSRCALPHA doesn't give any surprises, but the DESTALPHA/INVDESTALPHA
689 * blending works as supposed now - blend factor is 0.5 in both cases, not 0.75 as from the input
690 * vertices
692 hr = IDirect3DDevice7_SetRenderTarget(device, offscreen, 0);
693 ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
694 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
695 ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
697 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
698 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
699 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
700 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
701 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE, quad1, 4, 0);
702 ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
704 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_DESTALPHA);
705 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
706 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVDESTALPHA);
707 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
708 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE, quad2, 4, 0);
709 ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
711 hr = IDirect3DDevice7_SetRenderTarget(device, backbuffer, 0);
712 ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
714 /* Render the offscreen texture onto the frame buffer to be able to compare it regularly.
715 * Disable alpha blending for the final composition
717 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
718 ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
720 hr = IDirect3DDevice7_SetTexture(device, 0, offscreen);
721 ok(hr == D3D_OK, "IDirect3DDevice7_SetTexture failed, hr = %08x\n", hr);
722 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEX1, composite_quad, 4, 0);
723 ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
724 hr = IDirect3DDevice7_SetTexture(device, 0, NULL);
725 ok(hr == D3D_OK, "IDirect3DDevice7_SetTexture failed, hr = %08x\n", hr);
727 hr = IDirect3DDevice7_EndScene(device);
728 ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed, hr = %08x\n", hr);
731 color = getPixelColor(device, 160, 360);
732 red = (color & 0x00ff0000) >> 16;
733 green = (color & 0x0000ff00) >> 8;
734 blue = (color & 0x000000ff);
735 ok(red >= 0xbe && red <= 0xc0 && green >= 0x39 && green <= 0x41 && blue == 0x00,
736 "SRCALPHA on frame buffer returned color %08x, expected 0x00bf4000\n", color);
738 color = getPixelColor(device, 160, 120);
739 red = (color & 0x00ff0000) >> 16;
740 green = (color & 0x0000ff00) >> 8;
741 blue = (color & 0x000000ff);
742 ok(red == 0x00 && green == 0x00 && blue >= 0xfe && blue <= 0xff ,
743 "DSTALPHA on frame buffer returned color %08x, expected 0x00ff0000\n", color);
745 color = getPixelColor(device, 480, 360);
746 red = (color & 0x00ff0000) >> 16;
747 green = (color & 0x0000ff00) >> 8;
748 blue = (color & 0x000000ff);
749 ok(red >= 0xbe && red <= 0xc0 && green >= 0x39 && green <= 0x41 && blue == 0x00,
750 "SRCALPHA on texture returned color %08x, expected bar\n", color);
752 color = getPixelColor(device, 480, 120);
753 red = (color & 0x00ff0000) >> 16;
754 green = (color & 0x0000ff00) >> 8;
755 blue = (color & 0x000000ff);
756 ok(red >= 0x7e && red <= 0x81 && green == 0x00 && blue >= 0x7e && blue <= 0x81,
757 "DSTALPHA on texture returned color %08x, expected foo\n", color);
759 out:
760 if(offscreen) IDirectDrawSurface7_Release(offscreen);
761 if(backbuffer) IDirectDrawSurface7_Release(backbuffer);
764 START_TEST(visual)
766 HRESULT hr;
767 DWORD color;
768 if(!createObjects())
770 skip("Cannot initialize DirectDraw and Direct3D, skipping\n");
771 return;
774 /* Check for the reliability of the returned data */
775 hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
776 if(FAILED(hr))
778 trace("Clear failed, can't assure correctness of the test results, skipping\n");
779 goto cleanup;
782 color = getPixelColor(Direct3DDevice, 1, 1);
783 if(color !=0x00ff0000)
785 trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
786 goto cleanup;
789 hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
790 if(FAILED(hr))
792 trace("Clear failed, can't assure correctness of the test results, skipping\n");
793 goto cleanup;
796 color = getPixelColor(Direct3DDevice, 639, 479);
797 if(color != 0x0000ddee)
799 trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
800 goto cleanup;
803 /* Now run the tests */
804 lighting_test(Direct3DDevice);
805 clear_test(Direct3DDevice);
806 fog_test(Direct3DDevice);
807 offscreen_test(Direct3DDevice);
808 alpha_test(Direct3DDevice);
810 cleanup:
811 releaseObjects();