shdocvw: Constify some variables.
[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 || 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;
69     }
70
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);
74
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
77      */
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;
86
87     hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DTnLHalDevice, Surface, &Direct3DDevice);
88     if(FAILED(hr))
89     {
90         trace("Creating a TnLHal Device failed, trying HAL\n");
91         hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DHALDevice, Surface, &Direct3DDevice);
92         if(FAILED(hr))
93         {
94             trace("Creating a HAL device failed, trying Ref\n");
95             hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DRefDevice, Surface, &Direct3DDevice);
96         }
97     }
98     ok(hr == D3D_OK, "IDirect3D7_CreateDevice failed with %08x\n", hr);
99     if(!Direct3DDevice) goto err;
100     return TRUE;
101
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;
109 }
110
111 static void releaseObjects(void)
112 {
113     IDirect3DDevice7_Release(Direct3DDevice);
114     IDirect3D7_Release(Direct3D);
115     IDirectDrawSurface7_Release(Surface);
116     IDirectDraw7_Release(DirectDraw);
117     DestroyWindow(window);
118 }
119
120 static DWORD getPixelColor(IDirect3DDevice7 *device, UINT x, UINT y)
121 {
122     DWORD ret;
123     HRESULT hr;
124     DDSURFACEDESC2 ddsd;
125     RECT rectToLock = {x, y, x+1, y+1};
126     IDirectDrawSurface7 *surf = NULL;
127
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
130      */
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)
141     {
142         trace("cannot create helper surface\n");
143         return 0xdeadbeef;
144     }
145
146     memset(&ddsd, 0, sizeof(ddsd));
147     ddsd.dwSize = sizeof(ddsd);
148     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
149
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))
153     {
154         trace("Cannot blit\n");
155         ret = 0xdeadbee;
156         goto out;
157     }
158
159     hr = IDirectDrawSurface7_Lock(surf, &rectToLock, &ddsd, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
160     if(FAILED(hr))
161     {
162         trace("Can't lock the offscreen surface, hr=%08x\n", hr);
163         ret = 0xdeadbeec;
164         goto out;
165     }
166
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
169      */
170     ret = ((DWORD *) ddsd.lpSurface)[0] & 0x00ffffff;
171     hr = IDirectDrawSurface7_Unlock(surf, &rectToLock);
172     if(FAILED(hr))
173     {
174         trace("Can't unlock the offscreen surface, hr=%08x\n", hr);
175     }
176
177 out:
178     IDirectDrawSurface7_Release(surf);
179     return ret;
180 }
181
182 struct vertex
183 {
184     float x, y, z;
185     DWORD diffuse;
186 };
187
188 struct nvertex
189 {
190     float x, y, z;
191     float nx, ny, nz;
192     DWORD diffuse;
193 };
194
195 static void lighting_test(IDirect3DDevice7 *device)
196 {
197     HRESULT hr;
198     DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
199     DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
200     DWORD color;
201
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 };
206
207     struct vertex unlitquad[] =
208     {
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},
213     };
214     struct vertex litquad[] =
215     {
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},
220     };
221     struct nvertex unlitnquad[] =
222     {
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},
227     };
228     struct nvertex litnquad[] =
229     {
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},
234     };
235     WORD Indices[] = {0, 1, 2, 2, 3, 0};
236
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);
239
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);
261
262     hr = IDirect3DDevice7_BeginScene(device);
263     ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
264     if(hr == D3D_OK)
265     {
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);
272
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);
278
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);
284
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);
290
291         IDirect3DDevice7_EndScene(device);
292         ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
293     }
294
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);
303
304     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
305     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
306 }
307
308 static void clear_test(IDirect3DDevice7 *device)
309 {
310     /* Tests the correctness of clearing parameters */
311     HRESULT hr;
312     D3DRECT rect[2];
313     D3DRECT rect_negneg;
314     DWORD color;
315
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);
318
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;
324
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
332      */
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);
335
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);
343
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);
352 }
353
354 struct sVertex {
355     float x, y, z;
356     DWORD diffuse;
357     DWORD specular;
358 };
359
360 struct sVertexT {
361     float x, y, z, rhw;
362     DWORD diffuse;
363     DWORD specular;
364 };
365
366 static void fog_test(IDirect3DDevice7 *device)
367 {
368     HRESULT hr;
369     DWORD color;
370     float start = 0.0, end = 1.0;
371     D3DDEVICEDESC7 caps;
372
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  },
379     };
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  },
386     };
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.
389      */
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  },
395     };
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  },
401     };
402     WORD Indices[] = {0, 1, 2, 2, 3, 0};
403
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);
409
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);
417
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);
423
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);
429
430     if(IDirect3DDevice7_BeginScene(device) == D3D_OK)
431     {
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);
436
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
442          */
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);
446
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);
453
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
458          */
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);
462
463         hr = IDirect3DDevice7_EndScene(device);
464         ok(hr == D3D_OK, "EndScene returned %08x\n", hr);
465     }
466     else
467     {
468         ok(FALSE, "BeginScene failed\n");
469     }
470
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)
478     {
479         color = getPixelColor(device, 480, 360);
480         ok(color == 0x0000FF00, "Transformed vertex with linear table fog has color %08x\n", color);
481     }
482     else
483     {
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
486          */
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");
490     }
491
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);
495 }
496
497 static void offscreen_test(IDirect3DDevice7 *device)
498 {
499     HRESULT hr;
500     IDirectDrawSurface7 *backbuffer = NULL, *offscreen = NULL;
501     DWORD color;
502     DDSURFACEDESC2 ddsd;
503
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},
509     };
510
511     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
512     ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
513
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;
525     }
526
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;
531     }
532
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);
543
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);
549
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);
553
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);
558
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);
562
563         IDirect3DDevice7_EndScene(device);
564     }
565
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
579          */
580         color = getPixelColor(device, 10, 470);
581         ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
582     }
583
584 out:
585     hr = IDirect3DDevice7_SetTexture(device, 0, NULL);
586
587     /* restore things */
588     if(backbuffer) {
589         hr = IDirect3DDevice7_SetRenderTarget(device, backbuffer, 0);
590         IDirectDrawSurface7_Release(backbuffer);
591     }
592     if(offscreen) {
593         IDirectDrawSurface7_Release(offscreen);
594     }
595 }
596
597 START_TEST(visual)
598 {
599     HRESULT hr;
600     DWORD color;
601     if(!createObjects())
602     {
603         skip("Cannot initialize DirectDraw and Direct3D, skipping\n");
604         return;
605     }
606
607     /* Check for the reliability of the returned data */
608     hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
609     if(FAILED(hr))
610     {
611         trace("Clear failed, can't assure correctness of the test results, skipping\n");
612         goto cleanup;
613     }
614
615     color = getPixelColor(Direct3DDevice, 1, 1);
616     if(color !=0x00ff0000)
617     {
618         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
619         goto cleanup;
620     }
621
622     hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
623     if(FAILED(hr))
624     {
625         trace("Clear failed, can't assure correctness of the test results, skipping\n");
626         goto cleanup;
627     }
628
629     color = getPixelColor(Direct3DDevice, 639, 479);
630     if(color != 0x0000ddee)
631     {
632         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
633         goto cleanup;
634     }
635
636     /* Now run the tests */
637     lighting_test(Direct3DDevice);
638     clear_test(Direct3DDevice);
639     fog_test(Direct3DDevice);
640     offscreen_test(Direct3DDevice);
641
642 cleanup:
643     releaseObjects();
644 }