ddraw/tests: Fix the Visual C++ double to float conversion warnings.
[wine] / dlls / ddraw / tests / visual.c
1 /*
2  * Copyright (C) 2007 Stefan Dösinger(for CodeWeavers)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 /* See comment in dlls/d3d9/tests/visual.c for general guidelines */
20
21 #include <assert.h>
22 #include "wine/test.h"
23 #include "ddraw.h"
24 #include "d3d.h"
25
26 HWND window;
27 IDirectDraw7        *DirectDraw = NULL;
28 IDirectDrawSurface7 *Surface;
29 IDirect3D7          *Direct3D = NULL;
30 IDirect3DDevice7    *Direct3DDevice = NULL;
31
32 static HRESULT (WINAPI *pDirectDrawCreateEx)(LPGUID,LPVOID*,REFIID,LPUNKNOWN);
33
34 static BOOL createObjects(void)
35 {
36     HRESULT hr;
37     HMODULE hmod = GetModuleHandleA("ddraw.dll");
38     WNDCLASS wc = {0};
39     DDSURFACEDESC2 ddsd;
40
41
42     if(!hmod) return FALSE;
43     pDirectDrawCreateEx = (void*)GetProcAddress(hmod, "DirectDrawCreateEx");
44     if(!pDirectDrawCreateEx) return FALSE;
45
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;
49
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);
54
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);
62
63     }
64     ok(hr == DD_OK, "IDirectDraw7_SetDisplayMode failed with %08x\n", hr);
65     if(FAILED(hr)) goto err;
66
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);
70
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
73      */
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;
82
83     hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DTnLHalDevice, Surface, &Direct3DDevice);
84     if(FAILED(hr))
85     {
86         trace("Creating a TnLHal Device failed, trying HAL\n");
87         hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DHALDevice, Surface, &Direct3DDevice);
88         if(FAILED(hr))
89         {
90             trace("Creating a HAL device failed, trying Ref\n");
91             hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DRefDevice, Surface, &Direct3DDevice);
92         }
93     }
94     ok(hr == D3D_OK, "IDirect3D7_CreateDevice failed with %08x\n", hr);
95     if(!Direct3DDevice) goto err;
96     return TRUE;
97
98     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);
104     return FALSE;
105 }
106
107 static void releaseObjects(void)
108 {
109     IDirect3DDevice7_Release(Direct3DDevice);
110     IDirect3D7_Release(Direct3D);
111     IDirectDrawSurface7_Release(Surface);
112     IDirectDraw7_Release(DirectDraw);
113     DestroyWindow(window);
114 }
115
116 static DWORD getPixelColor(IDirect3DDevice7 *device, UINT x, UINT y)
117 {
118     DWORD ret;
119     HRESULT hr;
120     DDSURFACEDESC2 ddsd;
121     RECT rectToLock = {x, y, x+1, y+1};
122     IDirectDrawSurface7 *surf = NULL;
123
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
126      */
127     memset(&ddsd, 0, sizeof(ddsd));
128     ddsd.dwSize = sizeof(ddsd);
129     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
130     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
131     ddsd.dwWidth = 640;
132     ddsd.dwHeight = 480;
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);
136     if(!surf)
137     {
138         trace("cannot create helper surface\n");
139         return 0xdeadbeef;
140     }
141
142     memset(&ddsd, 0, sizeof(ddsd));
143     ddsd.dwSize = sizeof(ddsd);
144     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
145
146     hr = IDirectDrawSurface_BltFast(surf, 0, 0, Surface, NULL, 0);
147     ok(hr == DD_OK, "IDirectDrawSurface7_BltFast returned %08x\n", hr);
148     if(FAILED(hr))
149     {
150         trace("Cannot blit\n");
151         ret = 0xdeadbee;
152         goto out;
153     }
154
155     hr = IDirectDrawSurface7_Lock(surf, &rectToLock, &ddsd, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
156     if(FAILED(hr))
157     {
158         trace("Can't lock the offscreen surface, hr=%08x\n", hr);
159         ret = 0xdeadbeec;
160         goto out;
161     }
162
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
165      */
166     ret = ((DWORD *) ddsd.lpSurface)[0] & 0x00ffffff;
167     hr = IDirectDrawSurface7_Unlock(surf, &rectToLock);
168     if(FAILED(hr))
169     {
170         trace("Can't unlock the offscreen surface, hr=%08x\n", hr);
171     }
172
173 out:
174     IDirectDrawSurface7_Release(surf);
175     return ret;
176 }
177
178 struct vertex
179 {
180     float x, y, z;
181     DWORD diffuse;
182 };
183
184 struct nvertex
185 {
186     float x, y, z;
187     float nx, ny, nz;
188     DWORD diffuse;
189 };
190
191 static void lighting_test(IDirect3DDevice7 *device)
192 {
193     HRESULT hr;
194     DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
195     DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
196     DWORD color;
197
198     float mat[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
199                       0.0f, 1.0f, 0.0f, 0.0f,
200                       0.0f, 0.0f, 1.0f, 0.0f,
201                       0.0f, 0.0f, 0.0f, 1.0f };
202
203     struct vertex unlitquad[] =
204     {
205         {-1.0f, -1.0f,   0.1f,                          0xffff0000},
206         {-1.0f,  0.0f,   0.1f,                          0xffff0000},
207         { 0.0f,  0.0f,   0.1f,                          0xffff0000},
208         { 0.0f, -1.0f,   0.1f,                          0xffff0000},
209     };
210     struct vertex litquad[] =
211     {
212         {-1.0f,  0.0f,   0.1f,                          0xff00ff00},
213         {-1.0f,  1.0f,   0.1f,                          0xff00ff00},
214         { 0.0f,  1.0f,   0.1f,                          0xff00ff00},
215         { 0.0f,  0.0f,   0.1f,                          0xff00ff00},
216     };
217     struct nvertex unlitnquad[] =
218     {
219         { 0.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
220         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
221         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
222         { 1.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
223     };
224     struct nvertex litnquad[] =
225     {
226         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
227         { 0.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
228         { 1.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
229         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
230     };
231     WORD Indices[] = {0, 1, 2, 2, 3, 0};
232
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);
235
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);
257
258     hr = IDirect3DDevice7_BeginScene(device);
259     ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
260     if(hr == D3D_OK)
261     {
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);
268
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);
274
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);
280
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);
286
287         IDirect3DDevice7_EndScene(device);
288         ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
289     }
290
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);
299
300     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
301     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
302 }
303
304 static void clear_test(IDirect3DDevice7 *device)
305 {
306     /* Tests the correctness of clearing parameters */
307     HRESULT hr;
308     D3DRECT rect[2];
309     D3DRECT rect_negneg;
310     DWORD color;
311
312     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
313     ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
314
315     /* Positive x, negative y */
316     U1(rect[0]).x1 = 0;
317     U2(rect[0]).y1 = 480;
318     U3(rect[0]).x2 = 320;
319     U4(rect[0]).y2 = 240;
320
321     /* Positive x, positive y */
322     U1(rect[1]).x1 = 0;
323     U2(rect[1]).y1 = 0;
324     U3(rect[1]).x2 = 320;
325     U4(rect[1]).y2 = 240;
326     /* Clear 2 rectangles with one call. Shows that a positive value is returned, but the negative rectangle
327      * is ignored, the positive is still cleared afterwards
328      */
329     hr = IDirect3DDevice7_Clear(device, 2, rect, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
330     ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
331
332     /* negative x, negative y */
333     U1(rect_negneg).x1 = 640;
334     U2(rect_negneg).y1 = 240;
335     U3(rect_negneg).x2 = 320;
336     U4(rect_negneg).y2 = 0;
337     hr = IDirect3DDevice7_Clear(device, 1, &rect_negneg, D3DCLEAR_TARGET, 0xff00ff00, 0.0, 0);
338     ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
339
340     color = getPixelColor(device, 160, 360); /* lower left quad */
341     ok(color == 0x00ffffff, "Clear rectangle 3(pos, neg) has color %08x\n", color);
342     color = getPixelColor(device, 160, 120); /* upper left quad */
343     ok(color == 0x00ff0000, "Clear rectangle 1(pos, pos) has color %08x\n", color);
344     color = getPixelColor(device, 480, 360); /* lower right quad  */
345     ok(color == 0x00ffffff, "Clear rectangle 4(NULL) has color %08x\n", color);
346     color = getPixelColor(device, 480, 120); /* upper right quad */
347     ok(color == 0x00ffffff, "Clear rectangle 4(neg, neg) has color %08x\n", color);
348 }
349
350 struct sVertex {
351     float x, y, z;
352     DWORD diffuse;
353     DWORD specular;
354 };
355
356 struct sVertexT {
357     float x, y, z, rhw;
358     DWORD diffuse;
359     DWORD specular;
360 };
361
362 static void fog_test(IDirect3DDevice7 *device)
363 {
364     HRESULT hr;
365     DWORD color;
366     float start = 0.0, end = 1.0;
367
368     /* Gets full z based fog with linear fog, no fog with specular color */
369     struct sVertex unstransformed_1[] = {
370         {-1,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
371         {-1,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
372         { 0,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
373         { 0,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
374     };
375     /* Ok, I am too lazy to deal with transform matrices */
376     struct sVertex unstransformed_2[] = {
377         {-1,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
378         {-1,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
379         { 0,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
380         { 0,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
381     };
382     /* Untransformed ones. Give them a different diffuse color to make the test look
383      * nicer. It also makes making sure that they are drawn correctly easier.
384      */
385     struct sVertexT transformed_1[] = {
386         {320,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
387         {640,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
388         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
389         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
390     };
391     struct sVertexT transformed_2[] = {
392         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
393         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
394         {640,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
395         {320,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
396     };
397     WORD Indices[] = {0, 1, 2, 2, 3, 0};
398
399     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
400     ok(hr == D3D_OK, "IDirect3DDevice7_Clear returned %08x\n", hr);
401
402     /* Setup initial states: No lighting, fog on, fog color */
403     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
404     ok(hr == D3D_OK, "Turning off lighting returned %08x\n", hr);
405     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, TRUE);
406     ok(hr == D3D_OK, "Turning on fog calculations returned %08x\n", hr);
407     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGCOLOR, 0xFF00FF00 /* A nice green */);
408     ok(hr == D3D_OK, "Turning on fog calculations returned %08x\n", hr);
409
410     /* First test: Both table fog and vertex fog off */
411     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGTABLEMODE, D3DFOG_NONE);
412     ok(hr == D3D_OK, "Turning off table fog returned %08x\n", hr);
413     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGVERTEXMODE, D3DFOG_NONE);
414     ok(hr == D3D_OK, "Turning off table fog returned %08x\n", hr);
415
416     /* Start = 0, end = 1. Should be default, but set them */
417     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGSTART, *((DWORD *) &start));
418     ok(hr == D3D_OK, "Setting fog start returned %08x\n", hr);
419     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGEND, *((DWORD *) &end));
420     ok(hr == D3D_OK, "Setting fog start returned %08x\n", hr);
421
422     if(IDirect3DDevice7_BeginScene(device) == D3D_OK)
423     {
424         /* Untransformed, vertex fog = NONE, table fog = NONE: Read the fog weighting from the specular color */
425         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
426                                                    unstransformed_1, 4, Indices, 6, 0);
427         ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
428
429         /* That makes it use the Z value */
430         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGVERTEXMODE, D3DFOG_LINEAR);
431         ok(hr == D3D_OK, "Turning off table fog returned %08x\n", hr);
432         /* Untransformed, vertex fog != none (or table fog != none):
433          * Use the Z value as input into the equation
434          */
435         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
436                                                    unstransformed_2, 4, Indices, 6, 0);
437         ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
438
439         /* transformed verts */
440         ok( hr == D3D_OK, "SetFVF returned %08x\n", hr);
441         /* Transformed, vertex fog != NONE, pixel fog == NONE: Use specular color alpha component */
442         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
443                                                    transformed_1, 4, Indices, 6, 0);
444         ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
445
446         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGTABLEMODE, D3DFOG_LINEAR);
447         ok( hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR returned %08x\n", hr);
448         /* Transformed, table fog != none, vertex anything: Use Z value as input to the fog
449          * equation
450          */
451         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
452                                                    transformed_2, 4, Indices, 6, 0);
453         ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
454
455         hr = IDirect3DDevice7_EndScene(device);
456         ok(hr == D3D_OK, "EndScene returned %08x\n", hr);
457     }
458     else
459     {
460         ok(FALSE, "BeginScene failed\n");
461     }
462
463     color = getPixelColor(device, 160, 360);
464     ok(color == 0x00FF0000, "Untransformed vertex with no table or vertex fog has color %08x\n", color);
465     color = getPixelColor(device, 160, 120);
466     ok(color == 0x0000FF00, "Untransformed vertex with linear vertex fog has color %08x\n", color);
467     color = getPixelColor(device, 480, 120);
468     ok(color == 0x00FFFF00, "Transformed vertex with linear vertex fog has color %08x\n", color);
469     color = getPixelColor(device, 480, 360);
470     ok(color == 0x0000FF00, "Transformed vertex with linear table fog has color %08x\n", color);
471
472     /* Turn off the fog master switch to avoid confusing other tests */
473     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
474     ok(hr == D3D_OK, "Turning off fog calculations returned %08x\n", hr);
475 }
476
477 START_TEST(visual)
478 {
479     HRESULT hr;
480     DWORD color;
481     if(!createObjects())
482     {
483         skip("Cannot initialize DirectDraw and Direct3D, skipping\n");
484         return;
485     }
486
487     /* Check for the reliability of the returned data */
488     hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
489     if(FAILED(hr))
490     {
491         trace("Clear failed, can't assure correctness of the test results, skipping\n");
492         goto cleanup;
493     }
494
495     color = getPixelColor(Direct3DDevice, 1, 1);
496     if(color !=0x00ff0000)
497     {
498         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
499         goto cleanup;
500     }
501
502     hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
503     if(FAILED(hr))
504     {
505         trace("Clear failed, can't assure correctness of the test results, skipping\n");
506         goto cleanup;
507     }
508
509     color = getPixelColor(Direct3DDevice, 639, 479);
510     if(color != 0x0000ddee)
511     {
512         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
513         goto cleanup;
514     }
515
516     /* Now run the tests */
517     lighting_test(Direct3DDevice);
518     clear_test(Direct3DDevice);
519     fog_test(Direct3DDevice);
520
521 cleanup:
522     releaseObjects();
523 }