d3d9/tests: Fix the Visual C++ double to float conversion warnings.
[wine] / dlls / d3d9 / tests / visual.c
1 /*
2  * Copyright 2005, 2007 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 /* This test framework allows limited testing of rendering results. Things are rendered, shown on
21  * the framebuffer, read back from there and compared to expected colors.
22  *
23  * However, neither d3d nor opengl is guaranteed to be pixel exact, and thus the capability of this test
24  * is rather limited. As a general guideline for adding tests, do not rely on corner pixels. Draw a big enough
25  * area which shows specific behavior(like a quad on the whole screen), and try to get resulting colos with
26  * all bits set or unset in all channels(like pure red, green, blue, white, black). Hopefully everything that
27  * causes visible results in games can be tested in a way that does not depend on pixel exactness
28  */
29
30 #define COBJMACROS
31 #include <d3d9.h>
32 #include <dxerr9.h>
33 #include "wine/test.h"
34
35 static HMODULE d3d9_handle = 0;
36
37 static HWND create_window(void)
38 {
39     WNDCLASS wc = {0};
40     HWND ret;
41     wc.lpfnWndProc = &DefWindowProc;
42     wc.lpszClassName = "d3d9_test_wc";
43     RegisterClass(&wc);
44
45     ret = CreateWindow("d3d9_test_wc", "d3d9_test",
46                         WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
47     return ret;
48 }
49
50 static DWORD getPixelColor(IDirect3DDevice9 *device, UINT x, UINT y)
51 {
52     DWORD ret;
53     IDirect3DSurface9 *surf;
54     HRESULT hr;
55     D3DLOCKED_RECT lockedRect;
56     RECT rectToLock = {x, y, x+1, y+1};
57
58     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 640, 480, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surf, NULL);
59     if(FAILED(hr) || !surf )  /* This is not a test */
60     {
61         trace("Can't create an offscreen plain surface to read the render target data, hr=%s\n", DXGetErrorString9(hr));
62         return 0xdeadbeef;
63     }
64
65     hr = IDirect3DDevice9_GetFrontBufferData(device, 0, surf);
66     if(FAILED(hr))
67     {
68         trace("Can't read the front buffer data, hr=%s\n", DXGetErrorString9(hr));
69         ret = 0xdeadbeed;
70         goto out;
71     }
72
73     hr = IDirect3DSurface9_LockRect(surf, &lockedRect, &rectToLock, D3DLOCK_READONLY);
74     if(FAILED(hr))
75     {
76         trace("Can't lock the offscreen surface, hr=%s\n", DXGetErrorString9(hr));
77         ret = 0xdeadbeec;
78         goto out;
79     }
80
81     /* Remove the X channel for now. DirectX and OpenGL have different ideas how to treat it apparently, and it isn't
82      * really important for these tests
83      */
84     ret = ((DWORD *) lockedRect.pBits)[0] & 0x00ffffff;
85     hr = IDirect3DSurface9_UnlockRect(surf);
86     if(FAILED(hr))
87     {
88         trace("Can't unlock the offscreen surface, hr=%s\n", DXGetErrorString9(hr));
89     }
90
91 out:
92     if(surf) IDirect3DSurface9_Release(surf);
93     return ret;
94 }
95
96 static IDirect3DDevice9 *init_d3d9(void)
97 {
98     IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
99     IDirect3D9 *d3d9_ptr = 0;
100     IDirect3DDevice9 *device_ptr = 0;
101     D3DPRESENT_PARAMETERS present_parameters;
102     HRESULT hr;
103
104     d3d9_create = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
105     ok(d3d9_create != NULL, "Failed to get address of Direct3DCreate9\n");
106     if (!d3d9_create) return NULL;
107
108     d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
109     ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
110     if (!d3d9_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
120     hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
121     ok(hr == D3D_OK, "IDirect3D_CreateDevice returned: %s\n", DXGetErrorString9(hr));
122
123     return device_ptr;
124 }
125
126 struct vertex
127 {
128     float x, y, z;
129     DWORD diffuse;
130 };
131
132 struct nvertex
133 {
134     float x, y, z;
135     float nx, ny, nz;
136     DWORD diffuse;
137 };
138
139 static void lighting_test(IDirect3DDevice9 *device)
140 {
141     HRESULT hr;
142     DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
143     DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
144     DWORD color;
145
146     float mat[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
147                       0.0f, 1.0f, 0.0f, 0.0f,
148                       0.0f, 0.0f, 1.0f, 0.0f,
149                       0.0f, 0.0f, 0.0f, 1.0f };
150
151     struct vertex unlitquad[] =
152     {
153         {-1.0f, -1.0f,   0.1f,                          0xffff0000},
154         {-1.0f,  0.0f,   0.1f,                          0xffff0000},
155         { 0.0f,  0.0f,   0.1f,                          0xffff0000},
156         { 0.0f, -1.0f,   0.1f,                          0xffff0000},
157     };
158     struct vertex litquad[] =
159     {
160         {-1.0f,  0.0f,   0.1f,                          0xff00ff00},
161         {-1.0f,  1.0f,   0.1f,                          0xff00ff00},
162         { 0.0f,  1.0f,   0.1f,                          0xff00ff00},
163         { 0.0f,  0.0f,   0.1f,                          0xff00ff00},
164     };
165     struct nvertex unlitnquad[] =
166     {
167         { 0.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
168         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
169         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
170         { 1.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
171     };
172     struct nvertex litnquad[] =
173     {
174         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
175         { 0.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
176         { 1.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
177         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
178     };
179     WORD Indices[] = {0, 1, 2, 2, 3, 0};
180
181     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
182     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
183
184     /* Setup some states that may cause issues */
185     hr = IDirect3DDevice9_SetTransform(device, D3DTS_WORLDMATRIX(0), (D3DMATRIX *) mat);
186     ok(hr == D3D_OK, "IDirect3DDevice9_SetTransform returned %s\n", DXGetErrorString9(hr));
187     hr = IDirect3DDevice9_SetTransform(device, D3DTS_VIEW, (D3DMATRIX *)mat);
188     ok(hr == D3D_OK, "IDirect3DDevice9_SetTransform returned %s\n", DXGetErrorString9(hr));
189     hr = IDirect3DDevice9_SetTransform(device, D3DTS_PROJECTION, (D3DMATRIX *) mat);
190     ok(hr == D3D_OK, "IDirect3DDevice9_SetTransform returned %s\n", DXGetErrorString9(hr));
191     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_CLIPPING, FALSE);
192     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
193     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ZENABLE, FALSE);
194     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
195     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
196     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
197     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_STENCILENABLE, FALSE);
198     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
199     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHATESTENABLE, FALSE);
200     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
201     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
202     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
203     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_SCISSORTESTENABLE, FALSE);
204     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
205     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE);
206     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState failed with %s\n", DXGetErrorString9(hr));
207     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
208     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState failed with %s\n", DXGetErrorString9(hr));
209
210     hr = IDirect3DDevice9_SetFVF(device, fvf);
211     ok(hr == D3D_OK, "IDirect3DDevice9_SetFVF returned %s\n", DXGetErrorString9(hr));
212
213     hr = IDirect3DDevice9_BeginScene(device);
214     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
215     if(hr == D3D_OK)
216     {
217         /* No lights are defined... That means, lit vertices should be entirely black */
218         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
219         ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
220         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
221                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitquad, sizeof(unlitquad[0]));
222         ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
223
224         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, TRUE);
225         ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
226         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
227                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, litquad, sizeof(litquad[0]));
228         ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
229
230         hr = IDirect3DDevice9_SetFVF(device, nfvf);
231         ok(hr == D3D_OK, "IDirect3DDevice9_SetFVF failed with %s\n", DXGetErrorString9(hr));
232
233         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
234         ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
235         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
236                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitnquad, sizeof(unlitnquad[0]));
237         ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
238
239         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, TRUE);
240         ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
241         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
242                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, litnquad, sizeof(litnquad[0]));
243         ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
244
245         IDirect3DDevice9_EndScene(device);
246         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
247     }
248
249     IDirect3DDevice9_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 = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
261     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
262 }
263
264 static void clear_test(IDirect3DDevice9 *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 = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
273     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(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 = IDirect3DDevice9_Clear(device, 2, rect, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
290     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(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 = IDirect3DDevice9_Clear(device, 1, &rect_negneg, D3DCLEAR_TARGET, 0xff00ff00, 0.0, 0);
298     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
299
300     IDirect3DDevice9_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 typedef struct {
313     float in[4];
314     DWORD out;
315 } test_data_t;
316
317 /*
318  *  c7      rounded     ARGB
319  * -2.4     -2          0x00ffff00
320  * -1.6     -2          0x00ffff00
321  * -0.4      0          0x0000ffff
322  *  0.4      0          0x0000ffff
323  *  1.6      2          0x00ff00ff
324  *  2.4      2          0x00ff00ff
325  */
326 static void test_mova(IDirect3DDevice9 *device)
327 {
328     static const DWORD mova_test[] = {
329         0xfffe0200,                                                             /* vs_2_0                       */
330         0x0200001f, 0x80000000, 0x900f0000,                                     /* dcl_position v0              */
331         0x05000051, 0xa00f0000, 0x3f800000, 0x00000000, 0x00000000, 0x3f800000, /* def c0, 1.0, 0.0, 0.0, 1.0   */
332         0x05000051, 0xa00f0001, 0x3f800000, 0x3f800000, 0x00000000, 0x3f800000, /* def c1, 1.0, 1.0, 0.0, 1.0   */
333         0x05000051, 0xa00f0002, 0x00000000, 0x3f800000, 0x00000000, 0x3f800000, /* def c2, 0.0, 1.0, 0.0, 1.0   */
334         0x05000051, 0xa00f0003, 0x00000000, 0x3f800000, 0x3f800000, 0x3f800000, /* def c3, 0.0, 1.0, 1.0, 1.0   */
335         0x05000051, 0xa00f0004, 0x00000000, 0x00000000, 0x3f800000, 0x3f800000, /* def c4, 0.0, 0.0, 1.0, 1.0   */
336         0x05000051, 0xa00f0005, 0x3f800000, 0x00000000, 0x3f800000, 0x3f800000, /* def c5, 1.0, 0.0, 1.0, 1.0   */
337         0x05000051, 0xa00f0006, 0x3f800000, 0x3f800000, 0x3f800000, 0x3f800000, /* def c6, 1.0, 1.0, 1.0, 1.0   */
338         0x0200002e, 0xb0010000, 0xa0000007,                                     /* mova a0.x, c7.x              */
339         0x03000001, 0xd00f0000, 0xa0e42003, 0xb0000000,                         /* mov oD0, c[a0.x + 3]         */
340         0x02000001, 0xc00f0000, 0x90e40000,                                     /* mov oPos, v0                 */
341         0x0000ffff                                                              /* END                          */
342     };
343
344     static const test_data_t test_data[] = {
345         {{-2.4f, 0.0f, 0.0f, 0.0f}, 0x00ffff00},
346         {{-1.6f, 0.0f, 0.0f, 0.0f}, 0x00ffff00},
347         {{-0.4f, 0.0f, 0.0f, 0.0f}, 0x0000ffff},
348         {{ 0.4f, 0.0f, 0.0f, 0.0f}, 0x0000ffff},
349         {{ 1.6f, 0.0f, 0.0f, 0.0f}, 0x00ff00ff},
350         {{ 2.4f, 0.0f, 0.0f, 0.0f}, 0x00ff00ff}
351     };
352
353     static const float quad[][3] = {
354         {-1.0f, -1.0f, 0.0f},
355         {-1.0f,  1.0f, 0.0f},
356         { 1.0f, -1.0f, 0.0f},
357         { 1.0f,  1.0f, 0.0f},
358     };
359
360     static const D3DVERTEXELEMENT9 decl_elements[] = {
361         {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
362         D3DDECL_END()
363     };
364
365     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
366     IDirect3DVertexShader9 *mova_shader = NULL;
367     HRESULT hr;
368     int i;
369
370     hr = IDirect3DDevice9_CreateVertexShader(device, mova_test, &mova_shader);
371     ok(SUCCEEDED(hr), "CreateVertexShader failed (%08x)\n", hr);
372     hr = IDirect3DDevice9_SetVertexShader(device, mova_shader);
373     ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
374
375     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
376     ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (%08x)\n", hr);
377     hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
378     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (%08x)\n", hr);
379
380     for (i = 0; i < (sizeof(test_data) / sizeof(test_data_t)); ++i)
381     {
382         DWORD color;
383
384         hr = IDirect3DDevice9_SetVertexShaderConstantF(device, 7, test_data[i].in, 1);
385         ok(SUCCEEDED(hr), "SetVertexShaderConstantF failed (%08x)\n", hr);
386
387         hr = IDirect3DDevice9_BeginScene(device);
388         ok(SUCCEEDED(hr), "BeginScene failed (%08x)\n", hr);
389
390         hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
391         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%08x)\n", hr);
392
393         hr = IDirect3DDevice9_EndScene(device);
394         ok(SUCCEEDED(hr), "EndScene failed (%08x)\n", hr);
395
396         hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
397         ok(SUCCEEDED(hr), "Present failed (%08x)\n", hr);
398
399         color = getPixelColor(device, 320, 240);
400         ok(color == test_data[i].out, "Expected color %08x, got %08x (for input %f)\n", test_data[i].out, color, test_data[i].in[0]);
401
402         hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0, 0.0f, 0);
403         ok(SUCCEEDED(hr), "Clear failed (%08x)\n", hr);
404     }
405
406     hr = IDirect3DDevice9_SetVertexShader(device, NULL);
407     ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
408
409     IDirect3DVertexDeclaration9_Release(vertex_declaration);
410     IDirect3DVertexShader9_Release(mova_shader);
411 }
412
413 struct sVertex {
414     float x, y, z;
415     DWORD diffuse;
416     DWORD specular;
417 };
418
419 struct sVertexT {
420     float x, y, z, rhw;
421     DWORD diffuse;
422     DWORD specular;
423 };
424
425 static void fog_test(IDirect3DDevice9 *device)
426 {
427     HRESULT hr;
428     DWORD color;
429     float start = 0.0f, end = 1.0f;
430
431     /* Gets full z based fog with linear fog, no fog with specular color */
432     struct sVertex unstransformed_1[] = {
433         {-1,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
434         {-1,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
435         { 0,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
436         { 0,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
437     };
438     /* Ok, I am too lazy to deal with transform matrices */
439     struct sVertex unstransformed_2[] = {
440         {-1,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
441         {-1,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
442         { 0,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
443         { 0,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
444     };
445     /* Untransformed ones. Give them a different diffuse color to make the test look
446      * nicer. It also makes making sure that they are drawn correctly easier.
447      */
448     struct sVertexT transformed_1[] = {
449         {320,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
450         {640,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
451         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
452         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
453     };
454     struct sVertexT transformed_2[] = {
455         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
456         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
457         {640,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
458         {320,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
459     };
460     WORD Indices[] = {0, 1, 2, 2, 3, 0};
461
462     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
463     ok(hr == D3D_OK, "IDirect3DDevice9_Clear returned %s\n", DXGetErrorString9(hr));
464
465     /* Setup initial states: No lighting, fog on, fog color */
466     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
467     ok(hr == D3D_OK, "Turning off lighting returned %s\n", DXGetErrorString9(hr));
468     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, TRUE);
469     ok(hr == D3D_OK, "Turning on fog calculations returned %s\n", DXGetErrorString9(hr));
470     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGCOLOR, 0xFF00FF00 /* A nice green */);
471     ok(hr == D3D_OK, "Turning on fog calculations returned %s\n", DXGetErrorString9(hr));
472
473     /* First test: Both table fog and vertex fog off */
474     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_NONE);
475     ok(hr == D3D_OK, "Turning off table fog returned %s\n", DXGetErrorString9(hr));
476     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
477     ok(hr == D3D_OK, "Turning off table fog returned %s\n", DXGetErrorString9(hr));
478
479     /* Start = 0, end = 1. Should be default, but set them */
480     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGSTART, *((DWORD *) &start));
481     ok(hr == D3D_OK, "Setting fog start returned %s\n", DXGetErrorString9(hr));
482     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGEND, *((DWORD *) &end));
483     ok(hr == D3D_OK, "Setting fog start returned %s\n", DXGetErrorString9(hr));
484
485     if(IDirect3DDevice9_BeginScene(device) == D3D_OK)
486     {
487         hr = IDirect3DDevice9_SetFVF(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
488         ok( hr == D3D_OK, "SetFVF returned %s\n", DXGetErrorString9(hr));
489         /* Untransformed, vertex fog = NONE, table fog = NONE: Read the fog weighting from the specular color */
490         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
491                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, unstransformed_1,
492                                                      sizeof(unstransformed_1[0]));
493         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %s\n", DXGetErrorString9(hr));
494
495         /* That makes it use the Z value */
496         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);
497         ok(hr == D3D_OK, "Turning off table fog returned %s\n", DXGetErrorString9(hr));
498         /* Untransformed, vertex fog != none (or table fog != none):
499          * Use the Z value as input into the equation
500          */
501         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
502                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, unstransformed_2,
503                                                      sizeof(unstransformed_1[0]));
504         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %s\n", DXGetErrorString9(hr));
505
506         /* transformed verts */
507         hr = IDirect3DDevice9_SetFVF(device, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
508         ok( hr == D3D_OK, "SetFVF returned %s\n", DXGetErrorString9(hr));
509         /* Transformed, vertex fog != NONE, pixel fog == NONE: Use specular color alpha component */
510         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
511                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_1,
512                                                      sizeof(transformed_1[0]));
513         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %s\n", DXGetErrorString9(hr));
514
515         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
516         ok( hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR returned %s\n", DXGetErrorString9(hr));
517         /* Transformed, table fog != none, vertex anything: Use Z value as input to the fog
518          * equation
519          */
520         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
521                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_2,
522                                                      sizeof(transformed_2[0]));
523
524         hr = IDirect3DDevice9_EndScene(device);
525         ok(hr == D3D_OK, "EndScene returned %s\n", DXGetErrorString9(hr));
526     }
527     else
528     {
529         ok(FALSE, "BeginScene failed\n");
530     }
531
532     IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
533     color = getPixelColor(device, 160, 360);
534     ok(color == 0x00FF0000, "Untransformed vertex with no table or vertex fog has color %08x\n", color);
535     color = getPixelColor(device, 160, 120);
536     ok(color == 0x0000FF00, "Untransformed vertex with linear vertex fog has color %08x\n", color);
537     color = getPixelColor(device, 480, 120);
538     ok(color == 0x00FFFF00, "Transformed vertex with linear vertex fog has color %08x\n", color);
539     color = getPixelColor(device, 480, 360);
540     ok(color == 0x0000FF00, "Transformed vertex with linear table fog has color %08x\n", color);
541
542     /* Turn off the fog master switch to avoid confusing other tests */
543     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
544     ok(hr == D3D_OK, "Turning off fog calculations returned %s\n", DXGetErrorString9(hr));
545 }
546
547 /* This test verifies the behaviour of cube maps wrt. texture wrapping.
548  * D3D cube map wrapping always behaves like GL_CLAMP_TO_EDGE,
549  * regardless of the actual addressing mode set. */
550 static void test_cube_wrap(IDirect3DDevice9 *device)
551 {
552     static const float quad[][6] = {
553         {-1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f},
554         {-1.0f,  1.0f, 0.0f, 1.0f, 1.0f, 1.0f},
555         { 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f},
556         { 1.0f,  1.0f, 0.0f, 1.0f, 1.0f, 1.0f},
557     };
558
559     static const D3DVERTEXELEMENT9 decl_elements[] = {
560         {0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
561         {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
562         D3DDECL_END()
563     };
564
565     static const struct {
566         D3DTEXTUREADDRESS mode;
567         const char *name;
568     } address_modes[] = {
569         {D3DTADDRESS_WRAP, "D3DTADDRESS_WRAP"},
570         {D3DTADDRESS_MIRROR, "D3DTADDRESS_MIRROR"},
571         {D3DTADDRESS_CLAMP, "D3DTADDRESS_CLAMP"},
572         {D3DTADDRESS_BORDER, "D3DTADDRESS_BORDER"},
573         {D3DTADDRESS_MIRRORONCE, "D3DTADDRESS_MIRRORONCE"},
574     };
575
576     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
577     IDirect3DCubeTexture9 *texture = NULL;
578     IDirect3DSurface9 *surface = NULL;
579     D3DLOCKED_RECT locked_rect;
580     HRESULT hr;
581     INT x, y, face;
582
583     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
584     ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (0x%08x)\n", hr);
585     hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
586     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
587
588     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 128, 128,
589             D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surface, NULL);
590     ok(SUCCEEDED(hr), "CreateOffscreenPlainSurface failed (0x%08x)\n", hr);
591
592     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, D3DLOCK_DISCARD);
593     ok(SUCCEEDED(hr), "LockRect failed (0x%08x)\n", hr);
594
595     for (y = 0; y < 128; ++y)
596     {
597         DWORD *ptr = (DWORD *)(((BYTE *)locked_rect.pBits) + (y * locked_rect.Pitch));
598         for (x = 0; x < 64; ++x)
599         {
600             *ptr++ = 0xffff0000;
601         }
602         for (x = 64; x < 128; ++x)
603         {
604             *ptr++ = 0xff0000ff;
605         }
606     }
607
608     hr = IDirect3DSurface9_UnlockRect(surface);
609     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
610
611     hr = IDirect3DDevice9_CreateCubeTexture(device, 128, 1, 0, D3DFMT_A8R8G8B8,
612             D3DPOOL_DEFAULT, &texture, NULL);
613     ok(SUCCEEDED(hr), "CreateCubeTexture failed (0x%08x)\n", hr);
614
615     /* Create cube faces */
616     for (face = 0; face < 6; ++face)
617     {
618         IDirect3DSurface9 *face_surface = NULL;
619
620         hr= IDirect3DCubeTexture9_GetCubeMapSurface(texture, face, 0, &face_surface);
621         ok(SUCCEEDED(hr), "GetCubeMapSurface failed (0x%08x)\n", hr);
622
623         hr = IDirect3DDevice9_UpdateSurface(device, surface, NULL, face_surface, NULL);
624         ok(SUCCEEDED(hr), "UpdateSurface failed (0x%08x)\n", hr);
625
626         IDirect3DSurface9_Release(face_surface);
627     }
628
629     hr = IDirect3DDevice9_SetTexture(device, 0, (IDirect3DBaseTexture9 *)texture);
630     ok(SUCCEEDED(hr), "SetTexture failed (0x%08x)\n", hr);
631
632     hr = IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
633     ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_MINFILTER failed (0x%08x)\n", hr);
634     hr = IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
635     ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_MAGFILTER failed (0x%08x)\n", hr);
636     hr = IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_BORDERCOLOR, 0xff00ff00);
637     ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_BORDERCOLOR failed (0x%08x)\n", hr);
638
639     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
640     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
641
642     for (x = 0; x < (sizeof(address_modes) / sizeof(*address_modes)); ++x)
643     {
644         DWORD color;
645
646         hr = IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_ADDRESSU, address_modes[x].mode);
647         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_ADDRESSU (%s) failed (0x%08x)\n", address_modes[x].name, hr);
648         hr = IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_ADDRESSV, address_modes[x].mode);
649         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_ADDRESSV (%s) failed (0x%08x)\n", address_modes[x].name, hr);
650
651         hr = IDirect3DDevice9_BeginScene(device);
652         ok(SUCCEEDED(hr), "BeginScene failed (0x%08x)\n", hr);
653
654         hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], sizeof(quad[0]));
655         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (0x%08x)\n", hr);
656
657         hr = IDirect3DDevice9_EndScene(device);
658         ok(SUCCEEDED(hr), "EndScene failed (0x%08x)\n", hr);
659
660         hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
661         ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
662
663         /* Due to the nature of this test, we sample essentially at the edge
664          * between two faces. Because of this it's undefined from which face
665          * the driver will sample. Furtunately that's not important for this
666          * test, since all we care about is that it doesn't sample from the
667          * other side of the surface or from the border. */
668         color = getPixelColor(device, 320, 240);
669         ok(color == 0x00ff0000 || color == 0x000000ff,
670                 "Got color 0x%08x for addressing mode %s, expected 0x00ff0000 or 0x000000ff.\n",
671                 color, address_modes[x].name);
672
673         hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0, 0.0f, 0);
674         ok(SUCCEEDED(hr), "Clear failed (0x%08x)\n", hr);
675     }
676
677     hr = IDirect3DDevice9_SetTexture(device, 0, NULL);
678     ok(SUCCEEDED(hr), "SetTexture failed (0x%08x)\n", hr);
679
680     IDirect3DVertexDeclaration9_Release(vertex_declaration);
681     IDirect3DCubeTexture9_Release(texture);
682     IDirect3DSurface9_Release(surface);
683 }
684
685 /* This test tests fog in combination with shaders.
686  * What's tested: linear fog (vertex and table) with pixel shader
687  *                linear table fog with non foggy vertex shader
688  *                vertex fog with foggy vertex shader
689  * What's not tested: non linear fog with shader
690  *                    table fog with foggy vertex shader
691  */
692 static void fog_with_shader_test(IDirect3DDevice9 *device)
693 {
694     HRESULT hr;
695     DWORD color;
696     union {
697         float f;
698         DWORD i;
699     } start, end;
700     unsigned int i, j;
701
702     /* basic vertex shader without fog computation ("non foggy") */
703     static const DWORD vertex_shader_code1[] = {
704         0xfffe0101,                                                             /* vs_1_1                       */
705         0x0000001f, 0x80000000, 0x900f0000,                                     /* dcl_position v0              */
706         0x0000001f, 0x8000000a, 0x900f0001,                                     /* dcl_color0 v1                */
707         0x00000001, 0xc00f0000, 0x90e40000,                                     /* mov oPos, v0                 */
708         0x00000001, 0xd00f0000, 0x90e40001,                                     /* mov oD0, v1                  */
709         0x0000ffff
710     };
711     /* basic vertex shader with reversed fog computation ("foggy") */
712     static const DWORD vertex_shader_code2[] = {
713         0xfffe0101,                                                             /* vs_1_1                        */
714         0x0000001f, 0x80000000, 0x900f0000,                                     /* dcl_position v0               */
715         0x0000001f, 0x8000000a, 0x900f0001,                                     /* dcl_color0 v1                 */
716         0x00000051, 0xa00f0000, 0xbfa00000, 0x00000000, 0xbf666666, 0x00000000, /* def c0, -1.25, 0.0, -0.9, 0.0 */
717         0x00000001, 0xc00f0000, 0x90e40000,                                     /* mov oPos, v0                  */
718         0x00000001, 0xd00f0000, 0x90e40001,                                     /* mov oD0, v1                   */
719         0x00000002, 0x800f0000, 0x90aa0000, 0xa0aa0000,                         /* add r0, v0.z, c0.z            */
720         0x00000005, 0xc00f0001, 0x80000000, 0xa0000000,                         /* mul oFog, r0.x, c0.x          */
721         0x0000ffff
722     };
723     /* basic pixel shader */
724     static const DWORD pixel_shader_code[] = {
725         0xffff0101,                                                             /* ps_1_1     */
726         0x00000001, 0x800f0000, 0x90e40000,                                     /* mov r0, vo */
727         0x0000ffff
728     };
729
730     static struct vertex quad[] = {
731         {-1.0f, -1.0f,  0.0f,          0xFFFF0000  },
732         {-1.0f,  1.0f,  0.0f,          0xFFFF0000  },
733         { 1.0f, -1.0f,  0.0f,          0xFFFF0000  },
734         { 1.0f,  1.0f,  0.0f,          0xFFFF0000  },
735     };
736
737     static const D3DVERTEXELEMENT9 decl_elements[] = {
738         {0,  0, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
739         {0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,    D3DDECLUSAGE_COLOR, 0},
740         D3DDECL_END()
741     };
742
743     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
744     IDirect3DVertexShader9      *vertex_shader[3]   = {NULL, NULL, NULL};
745     IDirect3DPixelShader9       *pixel_shader[2]    = {NULL, NULL};
746
747     /* This reference data was collected on a nVidia GeForce 7600GS driver version 84.19 DirectX version 9.0c on Windows XP */
748     static const struct test_data_t {
749         int vshader;
750         int pshader;
751         D3DFOGMODE vfog;
752         D3DFOGMODE tfog;
753         unsigned int color[11];
754     } test_data[] = {
755         /* only pixel shader: */
756         {0, 1, 0, 3,
757         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
758          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
759         {0, 1, 1, 3,
760         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
761          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
762         {0, 1, 2, 3,
763         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
764          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
765         {0, 1, 3, 0,
766         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
767          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
768         {0, 1, 3, 3,
769         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
770          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
771
772         /* vertex shader */
773         {1, 0, 0, 0,
774         {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00,
775          0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00}},
776         {1, 0, 0, 3,
777         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
778          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
779         {1, 0, 1, 3,
780         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
781          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
782         {1, 0, 2, 3,
783         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
784          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
785         {1, 0, 3, 3,
786         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
787          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
788
789         /* vertex shader and pixel shader */
790         {1, 1, 0, 3,
791         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
792          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
793         {1, 1, 1, 3,
794         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
795          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
796         {1, 1, 2, 3,
797         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
798          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
799         {1, 1, 3, 3,
800         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
801          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
802
803         /* foggy vertex shader */
804         {2, 0, 0, 0,
805         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
806          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
807         {2, 0, 1, 0,
808         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
809          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
810         {2, 0, 2, 0,
811         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
812          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
813         {2, 0, 3, 0,
814         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
815          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
816
817         /* foggy vertex shader and pixel shader */
818         {2, 1, 0, 0,
819         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
820          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
821         {2, 1, 1, 0,
822         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
823          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
824         {2, 1, 2, 0,
825         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
826          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
827         {2, 1, 3, 0,
828         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
829          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
830
831     };
832
833     /* NOTE: changing these values will not affect the tests with foggy vertex shader, as the values are hardcoded in the shader*/
834     start.f=0.9f;
835     end.f=0.1f;
836
837     hr = IDirect3DDevice9_CreateVertexShader(device, vertex_shader_code1, &vertex_shader[1]);
838     ok(SUCCEEDED(hr), "CreateVertexShader failed (%08x)\n", hr);
839     hr = IDirect3DDevice9_CreateVertexShader(device, vertex_shader_code2, &vertex_shader[2]);
840     ok(SUCCEEDED(hr), "CreateVertexShader failed (%08x)\n", hr);
841     hr = IDirect3DDevice9_CreatePixelShader(device, pixel_shader_code, &pixel_shader[1]);
842     ok(SUCCEEDED(hr), "CreatePixelShader failed (%08x)\n", hr);
843     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
844     ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (%08x)\n", hr);
845
846     /* Setup initial states: No lighting, fog on, fog color */
847     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
848     ok(hr == D3D_OK, "Turning off lighting failed (%08x)\n", hr);
849     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, TRUE);
850     ok(hr == D3D_OK, "Turning on fog calculations failed (%08x)\n", hr);
851     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGCOLOR, 0xFF00FF00 /* A nice green */);
852     ok(hr == D3D_OK, "Setting fog color failed (%08x)\n", hr);
853     hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
854     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (%08x)\n", hr);
855
856     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_NONE);
857     ok(hr == D3D_OK, "Turning off table fog failed (%08x)\n", hr);
858     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
859     ok(hr == D3D_OK, "Turning off vertex fog failed (%08x)\n", hr);
860
861     /* Use fogtart = 0.1 and end = 0.9 to test behavior outside the fog transition phase, too*/
862     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGSTART, start.i);
863     ok(hr == D3D_OK, "Setting fog start failed (%08x)\n", hr);
864     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGEND, end.i);
865     ok(hr == D3D_OK, "Setting fog end failed (%08x)\n", hr);
866
867     for (i = 0; i < 22; i++)
868     {
869         hr = IDirect3DDevice9_SetVertexShader(device, vertex_shader[test_data[i].vshader]);
870         ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
871         hr = IDirect3DDevice9_SetPixelShader(device, pixel_shader[test_data[i].pshader]);
872         ok(SUCCEEDED(hr), "SetPixelShader failed (%08x)\n", hr);
873         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGVERTEXMODE, test_data[i].vfog);
874         ok( hr == D3D_OK, "Setting fog vertex mode to D3DFOG_LINEAR failed (%08x)\n", hr);
875         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGTABLEMODE, test_data[i].tfog);
876         ok( hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR failed (%08x)\n", hr);
877
878         for(j=0; j < 11; j++)
879         {
880             /* Don't use the whole zrange to prevent rounding errors */
881             quad[0].z = 0.001f + (float)j / 10.02f;
882             quad[1].z = 0.001f + (float)j / 10.02f;
883             quad[2].z = 0.001f + (float)j / 10.02f;
884             quad[3].z = 0.001f + (float)j / 10.02f;
885
886             hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
887             ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed (%08x)\n", hr);
888
889             hr = IDirect3DDevice9_BeginScene(device);
890             ok( hr == D3D_OK, "BeginScene returned failed (%08x)\n", hr);
891
892             hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], sizeof(quad[0]));
893             ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%08x)\n", hr);
894
895             hr = IDirect3DDevice9_EndScene(device);
896             ok(hr == D3D_OK, "EndScene failed (%08x)\n", hr);
897
898             IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
899
900             /* As the red and green component are the result of blending use 5% tolerance on the expected value */
901             color = getPixelColor(device, 128, 240);
902             ok((unsigned char)(color) == ((unsigned char)test_data[i].color[j])
903                     && abs( ((unsigned char)(color>>8)) - (unsigned char)(test_data[i].color[j]>>8) ) < 13
904                     && abs( ((unsigned char)(color>>16)) - (unsigned char)(test_data[i].color[j]>>16) ) < 13,
905                     "fog ps%i vs%i fvm%i ftm%i %d: got color %08x, expected %08x +-5%%\n", test_data[i].vshader, test_data[i].pshader, test_data[i].vfog, test_data[i].tfog, j, color, test_data[i].color[j]);
906         }
907     }
908
909     /* reset states */
910     hr = IDirect3DDevice9_SetVertexShader(device, NULL);
911     ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
912     hr = IDirect3DDevice9_SetPixelShader(device, NULL);
913     ok(SUCCEEDED(hr), "SetPixelShader failed (%08x)\n", hr);
914     hr = IDirect3DDevice9_SetVertexDeclaration(device, NULL);
915     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (%08x)\n", hr);
916     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
917     ok(hr == D3D_OK, "Turning off fog calculations failed (%08x)\n", hr);
918
919     IDirect3DVertexShader9_Release(vertex_shader[1]);
920     IDirect3DVertexShader9_Release(vertex_shader[2]);
921     IDirect3DPixelShader9_Release(pixel_shader[1]);
922     IDirect3DVertexDeclaration9_Release(vertex_declaration);
923 }
924
925 /* test the behavior of the texbem instruction
926  * with normal 2D and projective 2D textures
927  */
928 static void texbem_test(IDirect3DDevice9 *device)
929 {
930     HRESULT hr;
931     DWORD color;
932     unsigned int i, x, y;
933
934     static const DWORD pixel_shader_code[] = {
935         0xffff0101,                         /* ps_1_1*/
936         0x00000042, 0xb00f0000,             /* tex t0*/
937         0x00000043, 0xb00f0001, 0xb0e40000, /* texbem t1, t0*/
938         0x00000001, 0x800f0000, 0xb0e40001, /* mov r0, t1*/
939         0x0000ffff
940     };
941
942     static const float quad[][7] = {
943         {-128.0f/640.0f, -128.0f/480.0f, 0.1f, 0.0f, 0.0f, 0.0f, 0.0f},
944         {-128.0f/640.0f,  128.0f/480.0f, 0.1f, 0.0f, 1.0f, 0.0f, 1.0f},
945         { 128.0f/640.0f, -128.0f/480.0f, 0.1f, 1.0f, 0.0f, 1.0f, 0.0f},
946         { 128.0f/640.0f,  128.0f/480.0f, 0.1f, 1.0f, 1.0f, 1.0f, 1.0f},
947     };
948     static const float quad_proj[][9] = {
949         {-128.0f/640.0f, -128.0f/480.0f, 0.1f, 0.0f, 0.0f,   0.0f,   0.0f, 0.0f, 128.0f},
950         {-128.0f/640.0f,  128.0f/480.0f, 0.1f, 0.0f, 1.0f,   0.0f, 128.0f, 0.0f, 128.0f},
951         { 128.0f/640.0f, -128.0f/480.0f, 0.1f, 1.0f, 0.0f, 128.0f,   0.0f, 0.0f, 128.0f},
952         { 128.0f/640.0f,  128.0f/480.0f, 0.1f, 1.0f, 1.0f, 128.0f, 128.0f, 0.0f, 128.0f},
953     };
954
955     static const D3DVERTEXELEMENT9 decl_elements[][4] = { {
956         {0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
957         {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
958         {0, 20, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
959         D3DDECL_END()
960     },{
961         {0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
962         {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
963         {0, 20, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
964         D3DDECL_END()
965     } };
966
967     /* use assymetric matrix to test loading */
968     float bumpenvmat[4] = {0.0,0.5,-0.5,0.0};
969
970     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
971     IDirect3DPixelShader9       *pixel_shader       = NULL;
972     IDirect3DTexture9           *texture[2]         = {NULL, NULL};
973     D3DLOCKED_RECT locked_rect;
974
975     /* Generate the textures */
976     for(i=0; i<2; i++)
977     {
978         hr = IDirect3DDevice9_CreateTexture(device, 128, 128, 1, 0, i?D3DFMT_A8R8G8B8:D3DFMT_V8U8,
979                 D3DPOOL_MANAGED, &texture[i], NULL);
980         ok(SUCCEEDED(hr), "CreateTexture failed (0x%08x)\n", hr);
981
982         hr = IDirect3DTexture9_LockRect(texture[i], 0, &locked_rect, NULL, D3DLOCK_DISCARD);
983         ok(SUCCEEDED(hr), "LockRect failed (0x%08x)\n", hr);
984         for (y = 0; y < 128; ++y)
985         {
986             if(i)
987             { /* Set up black texture with 2x2 texel white spot in the middle */
988                 DWORD *ptr = (DWORD *)(((BYTE *)locked_rect.pBits) + (y * locked_rect.Pitch));
989                 for (x = 0; x < 128; ++x)
990                 {
991                     if(y>62 && y<66 && x>62 && x<66)
992                         *ptr++ = 0xffffffff;
993                     else
994                         *ptr++ = 0xff000000;
995                 }
996             }
997             else
998             { /* Set up a displacement map which points away from the center parallel to the closest axis.
999               * (if multiplied with bumpenvmat)
1000               */
1001                 WORD *ptr = (WORD *)(((BYTE *)locked_rect.pBits) + (y * locked_rect.Pitch));
1002                 for (x = 0; x < 128; ++x)
1003                 {
1004                     if(abs(x-64)>abs(y-64))
1005                     {
1006                         if(x < 64)
1007                             *ptr++ = 0xc000;
1008                         else
1009                             *ptr++ = 0x4000;
1010                     }
1011                     else
1012                     {
1013                         if(y < 64)
1014                             *ptr++ = 0x0040;
1015                         else
1016                             *ptr++ = 0x00c0;
1017                     }
1018                 }
1019             }
1020         }
1021         hr = IDirect3DTexture9_UnlockRect(texture[i], 0);
1022         ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
1023
1024         hr = IDirect3DDevice9_SetTexture(device, i, (IDirect3DBaseTexture9 *)texture[i]);
1025         ok(SUCCEEDED(hr), "SetTexture failed (0x%08x)\n", hr);
1026
1027         /* Disable texture filtering */
1028         hr = IDirect3DDevice9_SetSamplerState(device, i, D3DSAMP_MINFILTER, D3DTEXF_POINT);
1029         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_MINFILTER failed (0x%08x)\n", hr);
1030         hr = IDirect3DDevice9_SetSamplerState(device, i, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
1031         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_MAGFILTER failed (0x%08x)\n", hr);
1032
1033         hr = IDirect3DDevice9_SetSamplerState(device, i, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
1034         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_ADDRESSU failed (0x%08x)\n", hr);
1035         hr = IDirect3DDevice9_SetSamplerState(device, i, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
1036         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_ADDRESSV failed (0x%08x)\n", hr);
1037     }
1038
1039     IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_BUMPENVMAT00, *(LPDWORD)&bumpenvmat[0]);
1040     IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_BUMPENVMAT01, *(LPDWORD)&bumpenvmat[1]);
1041     IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_BUMPENVMAT10, *(LPDWORD)&bumpenvmat[2]);
1042     hr = IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_BUMPENVMAT11, *(LPDWORD)&bumpenvmat[3]);
1043     ok(SUCCEEDED(hr), "SetTextureStageState failed (%08x)\n", hr);
1044
1045     hr = IDirect3DDevice9_SetVertexShader(device, NULL);
1046     ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
1047
1048     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
1049     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed (%08x)\n", hr);
1050
1051     for(i=0; i<2; i++)
1052     {
1053         if(i)
1054         {
1055             hr = IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4|D3DTTFF_PROJECTED);
1056             ok(SUCCEEDED(hr), "SetTextureStageState D3DTSS_TEXTURETRANSFORMFLAGS failed (0x%08x)\n", hr);
1057         }
1058
1059         hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements[i], &vertex_declaration);
1060         ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (0x%08x)\n", hr);
1061         hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
1062         ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
1063
1064         hr = IDirect3DDevice9_CreatePixelShader(device, pixel_shader_code, &pixel_shader);
1065         ok(SUCCEEDED(hr), "CreatePixelShader failed (%08x)\n", hr);
1066         hr = IDirect3DDevice9_SetPixelShader(device, pixel_shader);
1067         ok(SUCCEEDED(hr), "SetPixelShader failed (%08x)\n", hr);
1068
1069         hr = IDirect3DDevice9_BeginScene(device);
1070         ok(SUCCEEDED(hr), "BeginScene failed (0x%08x)\n", hr);
1071
1072         if(!i)
1073             hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], sizeof(quad[0]));
1074         else
1075             hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad_proj[0], sizeof(quad_proj[0]));
1076         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (0x%08x)\n", hr);
1077
1078         hr = IDirect3DDevice9_EndScene(device);
1079         ok(SUCCEEDED(hr), "EndScene failed (0x%08x)\n", hr);
1080
1081         hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
1082         ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1083
1084         color = getPixelColor(device, 320-32, 240);
1085         ok(color == 0x00ffffff, "texbem failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
1086         color = getPixelColor(device, 320+32, 240);
1087         ok(color == 0x00ffffff, "texbem failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
1088         color = getPixelColor(device, 320, 240-32);
1089         ok(color == 0x00ffffff, "texbem failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
1090         color = getPixelColor(device, 320, 240+32);
1091         ok(color == 0x00ffffff, "texbem failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
1092
1093         hr = IDirect3DDevice9_SetPixelShader(device, NULL);
1094         ok(SUCCEEDED(hr), "SetPixelShader failed (%08x)\n", hr);
1095         IDirect3DPixelShader9_Release(pixel_shader);
1096
1097         hr = IDirect3DDevice9_SetVertexDeclaration(device, NULL);
1098         ok(SUCCEEDED(hr), "SetVertexDeclaration failed (%08x)\n", hr);
1099         IDirect3DVertexDeclaration9_Release(vertex_declaration);
1100     }
1101
1102     /* clean up */
1103     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0, 0.0f, 0);
1104     ok(SUCCEEDED(hr), "Clear failed (0x%08x)\n", hr);
1105
1106     hr = IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
1107     ok(SUCCEEDED(hr), "SetTextureStageState D3DTSS_TEXTURETRANSFORMFLAGS failed (0x%08x)\n", hr);
1108
1109     for(i=0; i<2; i++)
1110     {
1111         hr = IDirect3DDevice9_SetTexture(device, i, NULL);
1112         ok(SUCCEEDED(hr), "SetTexture failed (0x%08x)\n", hr);
1113         IDirect3DCubeTexture9_Release(texture[i]);
1114     }
1115 }
1116
1117 START_TEST(visual)
1118 {
1119     IDirect3DDevice9 *device_ptr;
1120     D3DCAPS9 caps;
1121     HRESULT hr;
1122     DWORD color;
1123
1124     d3d9_handle = LoadLibraryA("d3d9.dll");
1125     if (!d3d9_handle)
1126     {
1127         skip("Could not load d3d9.dll\n");
1128         return;
1129     }
1130
1131     device_ptr = init_d3d9();
1132     if (!device_ptr) return;
1133
1134     IDirect3DDevice9_GetDeviceCaps(device_ptr, &caps);
1135
1136     /* Check for the reliability of the returned data */
1137     hr = IDirect3DDevice9_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
1138     if(FAILED(hr))
1139     {
1140         trace("Clear failed, can't assure correctness of the test results, skipping\n");
1141         goto cleanup;
1142     }
1143     IDirect3DDevice9_Present(device_ptr, NULL, NULL, NULL, NULL);
1144
1145     color = getPixelColor(device_ptr, 1, 1);
1146     if(color !=0x00ff0000)
1147     {
1148         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
1149         goto cleanup;
1150     }
1151
1152     hr = IDirect3DDevice9_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
1153     if(FAILED(hr))
1154     {
1155         trace("Clear failed, can't assure correctness of the test results, skipping\n");
1156         goto cleanup;
1157     }
1158     IDirect3DDevice9_Present(device_ptr, NULL, NULL, NULL, NULL);
1159
1160     color = getPixelColor(device_ptr, 639, 479);
1161     if(color != 0x0000ddee)
1162     {
1163         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
1164         goto cleanup;
1165     }
1166
1167     /* Now execute the real tests */
1168     lighting_test(device_ptr);
1169     clear_test(device_ptr);
1170     fog_test(device_ptr);
1171     test_cube_wrap(device_ptr);
1172
1173     if (caps.VertexShaderVersion >= D3DVS_VERSION(2, 0))
1174     {
1175         test_mova(device_ptr);
1176     }
1177     else skip("No vs_2_0 support\n");
1178
1179     if (caps.VertexShaderVersion >= D3DVS_VERSION(1, 1) && caps.PixelShaderVersion >= D3DVS_VERSION(1, 1))
1180     {
1181         fog_with_shader_test(device_ptr);
1182     }
1183     else skip("No vs_1_1 and ps_1_1 support\n");
1184
1185     if (caps.PixelShaderVersion >= D3DVS_VERSION(1, 1))
1186     {
1187         texbem_test(device_ptr);
1188     }
1189     else skip("No ps_1_1 support\n");
1190
1191 cleanup:
1192     if(device_ptr) IDirect3DDevice9_Release(device_ptr);
1193 }