d3d8: Skip tests if d3d is not supported.
[wine] / dlls / d3d8 / tests / visual.c
1 /*
2  * Copyright (C) 2005 Henri Verbeet
3  * Copyright (C) 2007 Stefan Dösinger(for CodeWeavers)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 /* See comment in dlls/d3d9/tests/visual.c for general guidelines */
21
22 #define COBJMACROS
23 #include <d3d8.h>
24 #include <dxerr8.h>
25 #include "wine/test.h"
26
27 static HMODULE d3d8_handle = 0;
28
29 static HWND create_window(void)
30 {
31     WNDCLASS wc = {0};
32     HWND ret;
33     wc.lpfnWndProc = &DefWindowProc;
34     wc.lpszClassName = "d3d8_test_wc";
35     RegisterClass(&wc);
36
37     ret = CreateWindow("d3d8_test_wc", "d3d8_test",
38                         WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
39     return ret;
40 }
41
42 static DWORD getPixelColor(IDirect3DDevice8 *device, UINT x, UINT y)
43 {
44     DWORD ret;
45     IDirect3DSurface8 *surf;
46     IDirect3DTexture8 *tex;
47     HRESULT hr;
48     D3DLOCKED_RECT lockedRect;
49     RECT rectToLock = {x, y, x+1, y+1};
50
51     hr = IDirect3DDevice8_CreateTexture(device, 640, 480, 1 /* Levels */, 0 /* usage */, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &tex);
52     if(FAILED(hr) || !tex )  /* This is not a test */
53     {
54         trace("Can't create an offscreen plain surface to read the render target data, hr=%s\n", DXGetErrorString8(hr));
55         return 0xdeadbeef;
56     }
57     hr = IDirect3DTexture8_GetSurfaceLevel(tex, 0, &surf);
58     if(FAILED(hr) || !tex )  /* This is not a test */
59     {
60         trace("Can't get surface from texture, hr=%s\n", DXGetErrorString8(hr));
61         ret = 0xdeadbeee;
62         goto out;
63     }
64
65     hr = IDirect3DDevice8_GetFrontBuffer(device, surf);
66     if(FAILED(hr))
67     {
68         trace("Can't read the front buffer data, hr=%s\n", DXGetErrorString8(hr));
69         ret = 0xdeadbeed;
70         goto out;
71     }
72
73     hr = IDirect3DSurface8_LockRect(surf, &lockedRect, &rectToLock, D3DLOCK_READONLY);
74     if(FAILED(hr))
75     {
76         trace("Can't lock the offscreen surface, hr=%s\n", DXGetErrorString8(hr));
77         ret = 0xdeadbeec;
78         goto out;
79     }
80     /* Remove the X channel for now. DirectX and OpenGL have different ideas how to treat it apparently, and it isn't
81      * really important for these tests
82      */
83     ret = ((DWORD *) lockedRect.pBits)[0] & 0x00ffffff;
84     hr = IDirect3DSurface8_UnlockRect(surf);
85     if(FAILED(hr))
86     {
87         trace("Can't unlock the offscreen surface, hr=%s\n", DXGetErrorString8(hr));
88     }
89
90 out:
91     if(surf) IDirect3DSurface8_Release(surf);
92     if(tex) IDirect3DTexture8_Release(tex);
93     return ret;
94 }
95
96 static IDirect3DDevice8 *init_d3d8(void)
97 {
98     IDirect3D8 * (__stdcall * d3d8_create)(UINT SDKVersion) = 0;
99     IDirect3D8 *d3d8_ptr = 0;
100     IDirect3DDevice8 *device_ptr = 0;
101     D3DPRESENT_PARAMETERS present_parameters;
102     HRESULT hr;
103
104     d3d8_create = (void *)GetProcAddress(d3d8_handle, "Direct3DCreate8");
105     ok(d3d8_create != NULL, "Failed to get address of Direct3DCreate8\n");
106     if (!d3d8_create) return NULL;
107
108     d3d8_ptr = d3d8_create(D3D_SDK_VERSION);
109     ok(d3d8_ptr != NULL, "Failed to create IDirect3D8 object\n");
110     if (!d3d8_ptr) return NULL;
111
112     ZeroMemory(&present_parameters, sizeof(present_parameters));
113     present_parameters.Windowed = FALSE;
114     present_parameters.hDeviceWindow = create_window();
115     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
116     present_parameters.BackBufferWidth = 640;
117     present_parameters.BackBufferHeight = 480;
118     present_parameters.BackBufferFormat = D3DFMT_X8R8G8B8;
119     present_parameters.EnableAutoDepthStencil = TRUE;
120     present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
121
122     hr = IDirect3D8_CreateDevice(d3d8_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
123     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "IDirect3D_CreateDevice returned: %s\n", DXGetErrorString8(hr));
124
125     return device_ptr;
126 }
127
128 struct vertex
129 {
130     float x, y, z;
131     DWORD diffuse;
132 };
133
134 struct nvertex
135 {
136     float x, y, z;
137     float nx, ny, nz;
138     DWORD diffuse;
139 };
140
141 static void lighting_test(IDirect3DDevice8 *device)
142 {
143     HRESULT hr;
144     DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
145     DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
146     DWORD color;
147
148     float mat[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
149                       0.0f, 1.0f, 0.0f, 0.0f,
150                       0.0f, 0.0f, 1.0f, 0.0f,
151                       0.0f, 0.0f, 0.0f, 1.0f };
152
153     struct vertex unlitquad[] =
154     {
155         {-1.0f, -1.0f,   0.1f,                          0xffff0000},
156         {-1.0f,  0.0f,   0.1f,                          0xffff0000},
157         { 0.0f,  0.0f,   0.1f,                          0xffff0000},
158         { 0.0f, -1.0f,   0.1f,                          0xffff0000},
159     };
160     struct vertex litquad[] =
161     {
162         {-1.0f,  0.0f,   0.1f,                          0xff00ff00},
163         {-1.0f,  1.0f,   0.1f,                          0xff00ff00},
164         { 0.0f,  1.0f,   0.1f,                          0xff00ff00},
165         { 0.0f,  0.0f,   0.1f,                          0xff00ff00},
166     };
167     struct nvertex unlitnquad[] =
168     {
169         { 0.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
170         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
171         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
172         { 1.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
173     };
174     struct nvertex litnquad[] =
175     {
176         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
177         { 0.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
178         { 1.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
179         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
180     };
181     WORD Indices[] = {0, 1, 2, 2, 3, 0};
182
183     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
184     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %s\n", DXGetErrorString8(hr));
185
186     /* Setup some states that may cause issues */
187     hr = IDirect3DDevice8_SetTransform(device, D3DTS_WORLDMATRIX(0), (D3DMATRIX *) mat);
188     ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %s\n", DXGetErrorString8(hr));
189     hr = IDirect3DDevice8_SetTransform(device, D3DTS_VIEW, (D3DMATRIX *)mat);
190     ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %s\n", DXGetErrorString8(hr));
191     hr = IDirect3DDevice8_SetTransform(device, D3DTS_PROJECTION, (D3DMATRIX *) mat);
192     ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %s\n", DXGetErrorString8(hr));
193     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CLIPPING, FALSE);
194     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
195     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, FALSE);
196     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
197     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
198     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
199     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_STENCILENABLE, FALSE);
200     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
201     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHATESTENABLE, FALSE);
202     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
203     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
204     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
205     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE);
206     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed with %s\n", DXGetErrorString8(hr));
207     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
208     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed with %s\n", DXGetErrorString8(hr));
209
210     hr = IDirect3DDevice8_SetVertexShader(device, fvf);
211     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %s\n", DXGetErrorString8(hr));
212
213     hr = IDirect3DDevice8_BeginScene(device);
214     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %s\n", DXGetErrorString8(hr));
215     if(hr == D3D_OK)
216     {
217         /* No lights are defined... That means, lit vertices should be entirely black */
218         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
219         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
220         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
221                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitquad, sizeof(unlitquad[0]));
222         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString8(hr));
223
224         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, TRUE);
225         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
226         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
227                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, litquad, sizeof(litquad[0]));
228         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString8(hr));
229
230         hr = IDirect3DDevice8_SetVertexShader(device, nfvf);
231         ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader failed with %s\n", DXGetErrorString8(hr));
232
233         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
234         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
235         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
236                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitnquad, sizeof(unlitnquad[0]));
237         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString8(hr));
238
239         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, TRUE);
240         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
241         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
242                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, litnquad, sizeof(litnquad[0]));
243         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString8(hr));
244
245         IDirect3DDevice8_EndScene(device);
246         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %s\n", DXGetErrorString8(hr));
247     }
248
249     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
250
251     color = getPixelColor(device, 160, 360); /* lower left quad - unlit without normals */
252     ok(color == 0x00ff0000, "Unlit quad without normals has color %08x\n", color);
253     color = getPixelColor(device, 160, 120); /* upper left quad - lit without normals */
254     ok(color == 0x00000000, "Lit quad without normals has color %08x\n", color);
255     color = getPixelColor(device, 480, 360); /* lower left quad - unlit width normals */
256     ok(color == 0x000000ff, "Unlit quad width normals has color %08x\n", color);
257     color = getPixelColor(device, 480, 120); /* upper left quad - lit width normals */
258     ok(color == 0x00000000, "Lit quad width normals has color %08x\n", color);
259
260     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
261     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
262 }
263
264 static void clear_test(IDirect3DDevice8 *device)
265 {
266     /* Tests the correctness of clearing parameters */
267     HRESULT hr;
268     D3DRECT rect[2];
269     D3DRECT rect_negneg;
270     DWORD color;
271
272     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
273     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %s\n", DXGetErrorString8(hr));
274
275     /* Positive x, negative y */
276     rect[0].x1 = 0;
277     rect[0].y1 = 480;
278     rect[0].x2 = 320;
279     rect[0].y2 = 240;
280
281     /* Positive x, positive y */
282     rect[1].x1 = 0;
283     rect[1].y1 = 0;
284     rect[1].x2 = 320;
285     rect[1].y2 = 240;
286     /* Clear 2 rectangles with one call. Shows that a positive value is returned, but the negative rectangle
287      * is ignored, the positive is still cleared afterwards
288      */
289     hr = IDirect3DDevice8_Clear(device, 2, rect, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
290     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %s\n", DXGetErrorString8(hr));
291
292     /* negative x, negative y */
293     rect_negneg.x1 = 640;
294     rect_negneg.x1 = 240;
295     rect_negneg.x2 = 320;
296     rect_negneg.y2 = 0;
297     hr = IDirect3DDevice8_Clear(device, 1, &rect_negneg, D3DCLEAR_TARGET, 0xff00ff00, 0.0, 0);
298     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %s\n", DXGetErrorString8(hr));
299
300     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
301
302     color = getPixelColor(device, 160, 360); /* lower left quad */
303     ok(color == 0x00ffffff, "Clear rectangle 3(pos, neg) has color %08x\n", color);
304     color = getPixelColor(device, 160, 120); /* upper left quad */
305     ok(color == 0x00ff0000, "Clear rectangle 1(pos, pos) has color %08x\n", color);
306     color = getPixelColor(device, 480, 360); /* lower right quad  */
307     ok(color == 0x00ffffff, "Clear rectangle 4(NULL) has color %08x\n", color);
308     color = getPixelColor(device, 480, 120); /* upper right quad */
309     ok(color == 0x00ffffff, "Clear rectangle 4(neg, neg) has color %08x\n", color);
310 }
311
312 struct sVertex {
313     float x, y, z;
314     DWORD diffuse;
315     DWORD specular;
316 };
317
318 struct sVertexT {
319     float x, y, z, rhw;
320     DWORD diffuse;
321     DWORD specular;
322 };
323
324 static void fog_test(IDirect3DDevice8 *device)
325 {
326     HRESULT hr;
327     DWORD color;
328     float start = 0.0, end = 1.0;
329
330     /* Gets full z based fog with linear fog, no fog with specular color */
331     struct sVertex unstransformed_1[] = {
332         {-1,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
333         {-1,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
334         { 0,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
335         { 0,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
336     };
337     /* Ok, I am too lazy to deal with transform matrices */
338     struct sVertex unstransformed_2[] = {
339         {-1,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
340         {-1,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
341         { 0,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
342         { 0,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
343     };
344     /* Untransformed ones. Give them a different diffuse color to make the test look
345      * nicer. It also makes making sure that they are drawn correctly easier.
346      */
347     struct sVertexT transformed_1[] = {
348         {320,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
349         {640,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
350         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
351         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
352     };
353     struct sVertexT transformed_2[] = {
354         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
355         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
356         {640,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
357         {320,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
358     };
359     WORD Indices[] = {0, 1, 2, 2, 3, 0};
360
361     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
362     ok(hr == D3D_OK, "IDirect3DDevice8_Clear returned %s\n", DXGetErrorString8(hr));
363
364     /* Setup initial states: No lighting, fog on, fog color */
365     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
366     ok(hr == D3D_OK, "Turning off lighting returned %s\n", DXGetErrorString8(hr));
367     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, TRUE);
368     ok(hr == D3D_OK, "Turning on fog calculations returned %s\n", DXGetErrorString8(hr));
369     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGCOLOR, 0xFF00FF00 /* A nice green */);
370     ok(hr == D3D_OK, "Turning on fog calculations returned %s\n", DXGetErrorString8(hr));
371
372     /* First test: Both table fog and vertex fog off */
373     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_NONE);
374     ok(hr == D3D_OK, "Turning off table fog returned %s\n", DXGetErrorString8(hr));
375     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
376     ok(hr == D3D_OK, "Turning off table fog returned %s\n", DXGetErrorString8(hr));
377
378     /* Start = 0, end = 1. Should be default, but set them */
379     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGSTART, *((DWORD *) &start));
380     ok(hr == D3D_OK, "Setting fog start returned %s\n", DXGetErrorString8(hr));
381     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGEND, *((DWORD *) &end));
382     ok(hr == D3D_OK, "Setting fog start returned %s\n", DXGetErrorString8(hr));
383
384     if(IDirect3DDevice8_BeginScene(device) == D3D_OK)
385     {
386         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
387         ok( hr == D3D_OK, "SetVertexShader returned %s\n", DXGetErrorString8(hr));
388         /* Untransformed, vertex fog = NONE, table fog = NONE: Read the fog weighting from the specular color */
389         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
390                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, unstransformed_1,
391                                                      sizeof(unstransformed_1[0]));
392         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %s\n", DXGetErrorString8(hr));
393
394         /* That makes it use the Z value */
395         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);
396         ok(hr == D3D_OK, "Turning off table fog returned %s\n", DXGetErrorString8(hr));
397         /* Untransformed, vertex fog != none (or table fog != none):
398          * Use the Z value as input into the equation
399          */
400         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
401                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, unstransformed_2,
402                                                      sizeof(unstransformed_1[0]));
403         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %s\n", DXGetErrorString8(hr));
404
405         /* transformed verts */
406         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
407         ok( hr == D3D_OK, "SetVertexShader returned %s\n", DXGetErrorString8(hr));
408         /* Transformed, vertex fog != NONE, pixel fog == NONE: Use specular color alpha component */
409         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
410                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_1,
411                                                      sizeof(transformed_1[0]));
412         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %s\n", DXGetErrorString8(hr));
413
414         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
415         ok( hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR returned %s\n", DXGetErrorString8(hr));
416         /* Transformed, table fog != none, vertex anything: Use Z value as input to the fog
417          * equation
418          */
419         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
420                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_2,
421                                                      sizeof(transformed_2[0]));
422
423         hr = IDirect3DDevice8_EndScene(device);
424         ok(hr == D3D_OK, "EndScene returned %s\n", DXGetErrorString8(hr));
425     }
426     else
427     {
428         ok(FALSE, "BeginScene failed\n");
429     }
430
431     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
432     color = getPixelColor(device, 160, 360);
433     ok(color == 0x00FF0000, "Untransformed vertex with no table or vertex fog has color %08x\n", color);
434     color = getPixelColor(device, 160, 120);
435     ok(color == 0x0000FF00, "Untransformed vertex with linear vertex fog has color %08x\n", color);
436     color = getPixelColor(device, 480, 120);
437     ok(color == 0x00FFFF00, "Transformed vertex with linear vertex fog has color %08x\n", color);
438     color = getPixelColor(device, 480, 360);
439     ok(color == 0x0000FF00, "Transformed vertex with linear table fog has color %08x\n", color);
440
441     /* Turn off the fog master switch to avoid confusing other tests */
442     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
443     ok(hr == D3D_OK, "Turning off fog calculations returned %s\n", DXGetErrorString8(hr));
444 }
445
446 static void present_test(IDirect3DDevice8 *device)
447 {
448     struct vertex quad[] =
449     {
450         {-1.0f, -1.0f,   0.9f,                          0xffff0000},
451         {-1.0f,  1.0f,   0.9f,                          0xffff0000},
452         { 1.0f, -1.0f,   0.1f,                          0xffff0000},
453         { 1.0f,  1.0f,   0.1f,                          0xffff0000},
454     };
455     HRESULT hr;
456     DWORD color;
457
458     /* Does the Present clear the depth stencil? Clear the depth buffer with some value != 0,
459     * then call Present. Then clear the color buffer to make sure it has some defined content
460     * after the Present with D3DSWAPEFFECT_DISCARD. After that draw a plane that is somewhere cut
461     * by the depth value.
462     */
463     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 0.75, 0);
464     ok(hr == D3D_OK, "IDirect3DDevice8_Clear returned %s\n", DXGetErrorString8(hr));
465     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
466     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.4, 0);
467
468     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
469     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
470     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_GREATER);
471     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
472     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
473     ok(hr == D3D_OK, "IDirect3DDevice8_SetFVF returned %s\n", DXGetErrorString8(hr));
474
475     hr = IDirect3DDevice8_BeginScene(device);
476     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %s\n", DXGetErrorString8(hr));
477     if(hr == D3D_OK)
478     {
479         /* No lights are defined... That means, lit vertices should be entirely black */
480         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2 /*PrimCount */, quad, sizeof(quad[0]));
481         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString8(hr));
482
483         hr = IDirect3DDevice8_EndScene(device);
484         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %s\n", DXGetErrorString8(hr));
485     }
486
487     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_FALSE);
488     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
489
490     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
491     ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
492     color = getPixelColor(device, 512, 240);
493     ok(color == 0x00ffffff, "Present failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
494     color = getPixelColor(device, 64, 240);
495     ok(color == 0x00ff0000, "Present failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
496 }
497
498 static void test_rcp_rsq(IDirect3DDevice8 *device)
499 {
500     HRESULT hr;
501     DWORD shader;
502     DWORD color;
503     unsigned char c1, c2, c3;
504     float constant[4] = {1.0, 1.0, 1.0, 2.0};
505
506     static const float quad[][3] = {
507         {-1.0f, -1.0f, 0.0f},
508         {-1.0f,  1.0f, 0.0f},
509         { 1.0f, -1.0f, 0.0f},
510         { 1.0f,  1.0f, 0.0f},
511     };
512
513     const DWORD rcp_test[] = {
514         0xfffe0101,                                         /* vs.1.1 */
515
516         0x0009fffe, 0x30303030, 0x30303030,                 /* Shaders have to have a minimal size. */
517         0x30303030, 0x30303030, 0x30303030,                 /* Add a filler comment. Usually D3DX8's*/
518         0x30303030, 0x30303030, 0x30303030,                 /* version comment makes the shader big */
519         0x00303030,                                         /* enough to make windows happy         */
520
521         0x00000001, 0xc00f0000, 0x90e40000,                 /* mov oPos, v0 */
522         0x00000006, 0xd00f0000, 0xa0e40000,                 /* rcp oD0, c0 */
523         0x0000ffff                                          /* END */
524     };
525
526     const DWORD rsq_test[] = {
527         0xfffe0101,                                         /* vs.1.1 */
528
529         0x0009fffe, 0x30303030, 0x30303030,                 /* Shaders have to have a minimal size. */
530         0x30303030, 0x30303030, 0x30303030,                 /* Add a filler comment. Usually D3DX8's*/
531         0x30303030, 0x30303030, 0x30303030,                 /* version comment makes the shader big */
532         0x00303030,                                         /* enough to make windows happy         */
533
534         0x00000001, 0xc00f0000, 0x90e40000,                 /* mov oPos, v0 */
535         0x00000007, 0xd00f0000, 0xa0e40000,                 /* rsq oD0, c0 */
536         0x0000ffff                                          /* END */
537     };
538
539     DWORD decl[] =
540     {
541         D3DVSD_STREAM(0),
542         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
543         D3DVSD_END()
544     };
545
546     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff800080, 0.0, 0);
547     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %08x\n", hr);
548
549     hr = IDirect3DDevice8_CreateVertexShader(device, decl, rcp_test, &shader, 0);
550     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned with %08x\n", hr);
551
552     IDirect3DDevice8_SetVertexShader(device, shader);
553     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %08x\n", hr);
554     IDirect3DDevice8_SetVertexShaderConstant(device, 0, constant, 1);
555
556     hr = IDirect3DDevice8_BeginScene(device);
557     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene returned %08x\n", hr);
558     if(SUCCEEDED(hr))
559     {
560         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
561         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%08x)\n", hr);
562         hr = IDirect3DDevice8_EndScene(device);
563         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene returned %08x\n", hr);
564     }
565
566     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
567     ok(SUCCEEDED(hr), "Present failed (%08x)\n", hr);
568     color = getPixelColor(device, 320, 240);
569     c1 = (color & 0x00ff0000 )>> 16;
570     c2 = (color & 0x0000ff00 )>>  8;
571     c3 = (color & 0x000000ff )>>  0;
572     ok(c1 == c2 && c2 == c3, "Color components differ: c1 = %02x, c2 = %02x, c3 = %02x\n",
573        c1, c2, c3);
574     ok(c1 >= 0x7c && c1 <= 0x84, "Color component value is %02x\n", c1);
575
576     IDirect3DDevice8_SetVertexShader(device, 0);
577     IDirect3DDevice8_DeleteVertexShader(device, shader);
578
579     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff800080, 0.0, 0);
580     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %08x\n", hr);
581
582     hr = IDirect3DDevice8_CreateVertexShader(device, decl, rsq_test, &shader, 0);
583     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned with %08x\n", hr);
584
585     IDirect3DDevice8_SetVertexShader(device, shader);
586     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %08x\n", hr);
587     IDirect3DDevice8_SetVertexShaderConstant(device, 0, constant, 1);
588
589     hr = IDirect3DDevice8_BeginScene(device);
590     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene returned %08x\n", hr);
591     if(SUCCEEDED(hr))
592     {
593         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
594         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%08x)\n", hr);
595         hr = IDirect3DDevice8_EndScene(device);
596         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene returned %08x\n", hr);
597     }
598
599     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
600     ok(SUCCEEDED(hr), "Present failed (%08x)\n", hr);
601     color = getPixelColor(device, 320, 240);
602     c1 = (color & 0x00ff0000 )>> 16;
603     c2 = (color & 0x0000ff00 )>>  8;
604     c3 = (color & 0x000000ff )>>  0;
605     ok(c1 == c2 && c2 == c3, "Color components differ: c1 = %02x, c2 = %02x, c3 = %02x\n",
606        c1, c2, c3);
607     ok(c1 >= 0xb0 && c1 <= 0xb8, "Color component value is %02x\n", c1);
608
609     IDirect3DDevice8_SetVertexShader(device, 0);
610     IDirect3DDevice8_DeleteVertexShader(device, shader);
611 }
612
613 START_TEST(visual)
614 {
615     IDirect3DDevice8 *device_ptr;
616     HRESULT hr;
617     DWORD color;
618     D3DCAPS8 caps;
619
620     d3d8_handle = LoadLibraryA("d3d8.dll");
621     if (!d3d8_handle)
622     {
623         skip("Could not load d3d8.dll\n");
624         return;
625     }
626
627     device_ptr = init_d3d8();
628     if (!device_ptr)
629     {
630         skip("Could not initialize direct3d\n");
631         return;
632     }
633
634     IDirect3DDevice8_GetDeviceCaps(device_ptr, &caps);
635
636     /* Check for the reliability of the returned data */
637     hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
638     if(FAILED(hr))
639     {
640         trace("Clear failed, can't assure correctness of the test results, skipping\n");
641         goto cleanup;
642     }
643     IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
644
645     color = getPixelColor(device_ptr, 1, 1);
646     if(color !=0x00ff0000)
647     {
648         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
649         goto cleanup;
650     }
651
652     hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
653     if(FAILED(hr))
654     {
655         trace("Clear failed, can't assure correctness of the test results, skipping\n");
656         goto cleanup;
657     }
658     IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
659
660     color = getPixelColor(device_ptr, 639, 479);
661     if(color != 0x0000ddee)
662     {
663         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
664         goto cleanup;
665     }
666
667     /* Now run the real test */
668     lighting_test(device_ptr);
669     clear_test(device_ptr);
670     fog_test(device_ptr);
671     present_test(device_ptr);
672
673     if (caps.VertexShaderVersion >= D3DVS_VERSION(1, 1))
674     {
675         test_rcp_rsq(device_ptr);
676     }
677     else
678     {
679         skip("No vs.1.1 support\n");
680     }
681
682 cleanup:
683     if(device_ptr) IDirect3DDevice8_Release(device_ptr);
684 }