rpcrt4: Try a lot harder to resuse existing connections by comparing inside the RpcQu...
[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     present_parameters.EnableAutoDepthStencil = TRUE;
120     present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
121
122     hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
123     ok(hr == D3D_OK, "IDirect3D_CreateDevice returned: %s\n", DXGetErrorString9(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(IDirect3DDevice9 *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 = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
184     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
185
186     /* Setup some states that may cause issues */
187     hr = IDirect3DDevice9_SetTransform(device, D3DTS_WORLDMATRIX(0), (D3DMATRIX *) mat);
188     ok(hr == D3D_OK, "IDirect3DDevice9_SetTransform returned %s\n", DXGetErrorString9(hr));
189     hr = IDirect3DDevice9_SetTransform(device, D3DTS_VIEW, (D3DMATRIX *)mat);
190     ok(hr == D3D_OK, "IDirect3DDevice9_SetTransform returned %s\n", DXGetErrorString9(hr));
191     hr = IDirect3DDevice9_SetTransform(device, D3DTS_PROJECTION, (D3DMATRIX *) mat);
192     ok(hr == D3D_OK, "IDirect3DDevice9_SetTransform returned %s\n", DXGetErrorString9(hr));
193     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_CLIPPING, FALSE);
194     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
195     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ZENABLE, FALSE);
196     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
197     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
198     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
199     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_STENCILENABLE, FALSE);
200     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
201     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHATESTENABLE, FALSE);
202     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
203     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
204     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
205     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_SCISSORTESTENABLE, FALSE);
206     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
207     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE);
208     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState failed with %s\n", DXGetErrorString9(hr));
209     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
210     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState failed with %s\n", DXGetErrorString9(hr));
211
212     hr = IDirect3DDevice9_SetFVF(device, fvf);
213     ok(hr == D3D_OK, "IDirect3DDevice9_SetFVF returned %s\n", DXGetErrorString9(hr));
214
215     hr = IDirect3DDevice9_BeginScene(device);
216     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
217     if(hr == D3D_OK)
218     {
219         /* No lights are defined... That means, lit vertices should be entirely black */
220         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
221         ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
222         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
223                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitquad, sizeof(unlitquad[0]));
224         ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
225
226         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, TRUE);
227         ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
228         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
229                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, litquad, sizeof(litquad[0]));
230         ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
231
232         hr = IDirect3DDevice9_SetFVF(device, nfvf);
233         ok(hr == D3D_OK, "IDirect3DDevice9_SetFVF failed with %s\n", DXGetErrorString9(hr));
234
235         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
236         ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
237         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
238                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitnquad, sizeof(unlitnquad[0]));
239         ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
240
241         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, TRUE);
242         ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
243         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
244                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, litnquad, sizeof(litnquad[0]));
245         ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
246
247         IDirect3DDevice9_EndScene(device);
248         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
249     }
250
251     IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
252
253     color = getPixelColor(device, 160, 360); /* lower left quad - unlit without normals */
254     ok(color == 0x00ff0000, "Unlit quad without normals has color %08x\n", color);
255     color = getPixelColor(device, 160, 120); /* upper left quad - lit without normals */
256     ok(color == 0x00000000, "Lit quad without normals has color %08x\n", color);
257     color = getPixelColor(device, 480, 360); /* lower left quad - unlit width normals */
258     ok(color == 0x000000ff, "Unlit quad width normals has color %08x\n", color);
259     color = getPixelColor(device, 480, 120); /* upper left quad - lit width normals */
260     ok(color == 0x00000000, "Lit quad width normals has color %08x\n", color);
261
262     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
263     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
264 }
265
266 static void clear_test(IDirect3DDevice9 *device)
267 {
268     /* Tests the correctness of clearing parameters */
269     HRESULT hr;
270     D3DRECT rect[2];
271     D3DRECT rect_negneg;
272     DWORD color;
273
274     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
275     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
276
277     /* Positive x, negative y */
278     rect[0].x1 = 0;
279     rect[0].y1 = 480;
280     rect[0].x2 = 320;
281     rect[0].y2 = 240;
282
283     /* Positive x, positive y */
284     rect[1].x1 = 0;
285     rect[1].y1 = 0;
286     rect[1].x2 = 320;
287     rect[1].y2 = 240;
288     /* Clear 2 rectangles with one call. Shows that a positive value is returned, but the negative rectangle
289      * is ignored, the positive is still cleared afterwards
290      */
291     hr = IDirect3DDevice9_Clear(device, 2, rect, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
292     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
293
294     /* negative x, negative y */
295     rect_negneg.x1 = 640;
296     rect_negneg.x1 = 240;
297     rect_negneg.x2 = 320;
298     rect_negneg.y2 = 0;
299     hr = IDirect3DDevice9_Clear(device, 1, &rect_negneg, D3DCLEAR_TARGET, 0xff00ff00, 0.0, 0);
300     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
301
302     IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
303
304     color = getPixelColor(device, 160, 360); /* lower left quad */
305     ok(color == 0x00ffffff, "Clear rectangle 3(pos, neg) has color %08x\n", color);
306     color = getPixelColor(device, 160, 120); /* upper left quad */
307     ok(color == 0x00ff0000, "Clear rectangle 1(pos, pos) has color %08x\n", color);
308     color = getPixelColor(device, 480, 360); /* lower right quad  */
309     ok(color == 0x00ffffff, "Clear rectangle 4(NULL) has color %08x\n", color);
310     color = getPixelColor(device, 480, 120); /* upper right quad */
311     ok(color == 0x00ffffff, "Clear rectangle 4(neg, neg) has color %08x\n", color);
312 }
313
314 typedef struct {
315     float in[4];
316     DWORD out;
317 } test_data_t;
318
319 /*
320  *  c7      rounded     ARGB
321  * -2.4     -2          0x00ffff00
322  * -1.6     -2          0x00ffff00
323  * -0.4      0          0x0000ffff
324  *  0.4      0          0x0000ffff
325  *  1.6      2          0x00ff00ff
326  *  2.4      2          0x00ff00ff
327  */
328 static void test_mova(IDirect3DDevice9 *device)
329 {
330     static const DWORD mova_test[] = {
331         0xfffe0200,                                                             /* vs_2_0                       */
332         0x0200001f, 0x80000000, 0x900f0000,                                     /* dcl_position v0              */
333         0x05000051, 0xa00f0000, 0x3f800000, 0x00000000, 0x00000000, 0x3f800000, /* def c0, 1.0, 0.0, 0.0, 1.0   */
334         0x05000051, 0xa00f0001, 0x3f800000, 0x3f800000, 0x00000000, 0x3f800000, /* def c1, 1.0, 1.0, 0.0, 1.0   */
335         0x05000051, 0xa00f0002, 0x00000000, 0x3f800000, 0x00000000, 0x3f800000, /* def c2, 0.0, 1.0, 0.0, 1.0   */
336         0x05000051, 0xa00f0003, 0x00000000, 0x3f800000, 0x3f800000, 0x3f800000, /* def c3, 0.0, 1.0, 1.0, 1.0   */
337         0x05000051, 0xa00f0004, 0x00000000, 0x00000000, 0x3f800000, 0x3f800000, /* def c4, 0.0, 0.0, 1.0, 1.0   */
338         0x05000051, 0xa00f0005, 0x3f800000, 0x00000000, 0x3f800000, 0x3f800000, /* def c5, 1.0, 0.0, 1.0, 1.0   */
339         0x05000051, 0xa00f0006, 0x3f800000, 0x3f800000, 0x3f800000, 0x3f800000, /* def c6, 1.0, 1.0, 1.0, 1.0   */
340         0x0200002e, 0xb0010000, 0xa0000007,                                     /* mova a0.x, c7.x              */
341         0x03000001, 0xd00f0000, 0xa0e42003, 0xb0000000,                         /* mov oD0, c[a0.x + 3]         */
342         0x02000001, 0xc00f0000, 0x90e40000,                                     /* mov oPos, v0                 */
343         0x0000ffff                                                              /* END                          */
344     };
345
346     static const test_data_t test_data[] = {
347         {{-2.4f, 0.0f, 0.0f, 0.0f}, 0x00ffff00},
348         {{-1.6f, 0.0f, 0.0f, 0.0f}, 0x00ffff00},
349         {{-0.4f, 0.0f, 0.0f, 0.0f}, 0x0000ffff},
350         {{ 0.4f, 0.0f, 0.0f, 0.0f}, 0x0000ffff},
351         {{ 1.6f, 0.0f, 0.0f, 0.0f}, 0x00ff00ff},
352         {{ 2.4f, 0.0f, 0.0f, 0.0f}, 0x00ff00ff}
353     };
354
355     static const float quad[][3] = {
356         {-1.0f, -1.0f, 0.0f},
357         {-1.0f,  1.0f, 0.0f},
358         { 1.0f, -1.0f, 0.0f},
359         { 1.0f,  1.0f, 0.0f},
360     };
361
362     static const D3DVERTEXELEMENT9 decl_elements[] = {
363         {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
364         D3DDECL_END()
365     };
366
367     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
368     IDirect3DVertexShader9 *mova_shader = NULL;
369     HRESULT hr;
370     int i;
371
372     hr = IDirect3DDevice9_CreateVertexShader(device, mova_test, &mova_shader);
373     ok(SUCCEEDED(hr), "CreateVertexShader failed (%08x)\n", hr);
374     hr = IDirect3DDevice9_SetVertexShader(device, mova_shader);
375     ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
376
377     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
378     ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (%08x)\n", hr);
379     hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
380     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (%08x)\n", hr);
381
382     for (i = 0; i < (sizeof(test_data) / sizeof(test_data_t)); ++i)
383     {
384         DWORD color;
385
386         hr = IDirect3DDevice9_SetVertexShaderConstantF(device, 7, test_data[i].in, 1);
387         ok(SUCCEEDED(hr), "SetVertexShaderConstantF failed (%08x)\n", hr);
388
389         hr = IDirect3DDevice9_BeginScene(device);
390         ok(SUCCEEDED(hr), "BeginScene failed (%08x)\n", hr);
391
392         hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
393         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%08x)\n", hr);
394
395         hr = IDirect3DDevice9_EndScene(device);
396         ok(SUCCEEDED(hr), "EndScene failed (%08x)\n", hr);
397
398         hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
399         ok(SUCCEEDED(hr), "Present failed (%08x)\n", hr);
400
401         color = getPixelColor(device, 320, 240);
402         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]);
403
404         hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0, 0.0f, 0);
405         ok(SUCCEEDED(hr), "Clear failed (%08x)\n", hr);
406     }
407
408     hr = IDirect3DDevice9_SetVertexShader(device, NULL);
409     ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
410
411     IDirect3DVertexDeclaration9_Release(vertex_declaration);
412     IDirect3DVertexShader9_Release(mova_shader);
413 }
414
415 struct sVertex {
416     float x, y, z;
417     DWORD diffuse;
418     DWORD specular;
419 };
420
421 struct sVertexT {
422     float x, y, z, rhw;
423     DWORD diffuse;
424     DWORD specular;
425 };
426
427 static void fog_test(IDirect3DDevice9 *device)
428 {
429     HRESULT hr;
430     DWORD color;
431     float start = 0.0f, end = 1.0f;
432
433     /* Gets full z based fog with linear fog, no fog with specular color */
434     struct sVertex unstransformed_1[] = {
435         {-1,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
436         {-1,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
437         { 0,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
438         { 0,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
439     };
440     /* Ok, I am too lazy to deal with transform matrices */
441     struct sVertex unstransformed_2[] = {
442         {-1,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
443         {-1,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
444         { 0,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
445         { 0,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
446     };
447     /* Untransformed ones. Give them a different diffuse color to make the test look
448      * nicer. It also makes making sure that they are drawn correctly easier.
449      */
450     struct sVertexT transformed_1[] = {
451         {320,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
452         {640,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
453         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
454         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
455     };
456     struct sVertexT transformed_2[] = {
457         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
458         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
459         {640,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
460         {320,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
461     };
462     WORD Indices[] = {0, 1, 2, 2, 3, 0};
463
464     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
465     ok(hr == D3D_OK, "IDirect3DDevice9_Clear returned %s\n", DXGetErrorString9(hr));
466
467     /* Setup initial states: No lighting, fog on, fog color */
468     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
469     ok(hr == D3D_OK, "Turning off lighting returned %s\n", DXGetErrorString9(hr));
470     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, TRUE);
471     ok(hr == D3D_OK, "Turning on fog calculations returned %s\n", DXGetErrorString9(hr));
472     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGCOLOR, 0xFF00FF00 /* A nice green */);
473     ok(hr == D3D_OK, "Turning on fog calculations returned %s\n", DXGetErrorString9(hr));
474
475     /* First test: Both table fog and vertex fog off */
476     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_NONE);
477     ok(hr == D3D_OK, "Turning off table fog returned %s\n", DXGetErrorString9(hr));
478     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
479     ok(hr == D3D_OK, "Turning off table fog returned %s\n", DXGetErrorString9(hr));
480
481     /* Start = 0, end = 1. Should be default, but set them */
482     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGSTART, *((DWORD *) &start));
483     ok(hr == D3D_OK, "Setting fog start returned %s\n", DXGetErrorString9(hr));
484     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGEND, *((DWORD *) &end));
485     ok(hr == D3D_OK, "Setting fog start returned %s\n", DXGetErrorString9(hr));
486
487     if(IDirect3DDevice9_BeginScene(device) == D3D_OK)
488     {
489         hr = IDirect3DDevice9_SetFVF(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
490         ok( hr == D3D_OK, "SetFVF returned %s\n", DXGetErrorString9(hr));
491         /* Untransformed, vertex fog = NONE, table fog = NONE: Read the fog weighting from the specular color */
492         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
493                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, unstransformed_1,
494                                                      sizeof(unstransformed_1[0]));
495         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %s\n", DXGetErrorString9(hr));
496
497         /* That makes it use the Z value */
498         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);
499         ok(hr == D3D_OK, "Turning off table fog returned %s\n", DXGetErrorString9(hr));
500         /* Untransformed, vertex fog != none (or table fog != none):
501          * Use the Z value as input into the equation
502          */
503         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
504                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, unstransformed_2,
505                                                      sizeof(unstransformed_1[0]));
506         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %s\n", DXGetErrorString9(hr));
507
508         /* transformed verts */
509         hr = IDirect3DDevice9_SetFVF(device, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
510         ok( hr == D3D_OK, "SetFVF returned %s\n", DXGetErrorString9(hr));
511         /* Transformed, vertex fog != NONE, pixel fog == NONE: Use specular color alpha component */
512         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
513                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_1,
514                                                      sizeof(transformed_1[0]));
515         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %s\n", DXGetErrorString9(hr));
516
517         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
518         ok( hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR returned %s\n", DXGetErrorString9(hr));
519         /* Transformed, table fog != none, vertex anything: Use Z value as input to the fog
520          * equation
521          */
522         hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
523                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_2,
524                                                      sizeof(transformed_2[0]));
525
526         hr = IDirect3DDevice9_EndScene(device);
527         ok(hr == D3D_OK, "EndScene returned %s\n", DXGetErrorString9(hr));
528     }
529     else
530     {
531         ok(FALSE, "BeginScene failed\n");
532     }
533
534     IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
535     color = getPixelColor(device, 160, 360);
536     ok(color == 0x00FF0000, "Untransformed vertex with no table or vertex fog has color %08x\n", color);
537     color = getPixelColor(device, 160, 120);
538     ok(color == 0x0000FF00, "Untransformed vertex with linear vertex fog has color %08x\n", color);
539     color = getPixelColor(device, 480, 120);
540     ok(color == 0x00FFFF00, "Transformed vertex with linear vertex fog has color %08x\n", color);
541     color = getPixelColor(device, 480, 360);
542     ok(color == 0x0000FF00, "Transformed vertex with linear table fog has color %08x\n", color);
543
544     /* Turn off the fog master switch to avoid confusing other tests */
545     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
546     ok(hr == D3D_OK, "Turning off fog calculations returned %s\n", DXGetErrorString9(hr));
547 }
548
549 /* This test verifies the behaviour of cube maps wrt. texture wrapping.
550  * D3D cube map wrapping always behaves like GL_CLAMP_TO_EDGE,
551  * regardless of the actual addressing mode set. */
552 static void test_cube_wrap(IDirect3DDevice9 *device)
553 {
554     static const float quad[][6] = {
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         { 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f},
558         { 1.0f,  1.0f, 0.0f, 1.0f, 1.0f, 1.0f},
559     };
560
561     static const D3DVERTEXELEMENT9 decl_elements[] = {
562         {0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
563         {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
564         D3DDECL_END()
565     };
566
567     static const struct {
568         D3DTEXTUREADDRESS mode;
569         const char *name;
570     } address_modes[] = {
571         {D3DTADDRESS_WRAP, "D3DTADDRESS_WRAP"},
572         {D3DTADDRESS_MIRROR, "D3DTADDRESS_MIRROR"},
573         {D3DTADDRESS_CLAMP, "D3DTADDRESS_CLAMP"},
574         {D3DTADDRESS_BORDER, "D3DTADDRESS_BORDER"},
575         {D3DTADDRESS_MIRRORONCE, "D3DTADDRESS_MIRRORONCE"},
576     };
577
578     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
579     IDirect3DCubeTexture9 *texture = NULL;
580     IDirect3DSurface9 *surface = NULL;
581     D3DLOCKED_RECT locked_rect;
582     HRESULT hr;
583     INT x, y, face;
584
585     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
586     ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (0x%08x)\n", hr);
587     hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
588     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
589
590     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 128, 128,
591             D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surface, NULL);
592     ok(SUCCEEDED(hr), "CreateOffscreenPlainSurface failed (0x%08x)\n", hr);
593
594     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, D3DLOCK_DISCARD);
595     ok(SUCCEEDED(hr), "LockRect failed (0x%08x)\n", hr);
596
597     for (y = 0; y < 128; ++y)
598     {
599         DWORD *ptr = (DWORD *)(((BYTE *)locked_rect.pBits) + (y * locked_rect.Pitch));
600         for (x = 0; x < 64; ++x)
601         {
602             *ptr++ = 0xffff0000;
603         }
604         for (x = 64; x < 128; ++x)
605         {
606             *ptr++ = 0xff0000ff;
607         }
608     }
609
610     hr = IDirect3DSurface9_UnlockRect(surface);
611     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
612
613     hr = IDirect3DDevice9_CreateCubeTexture(device, 128, 1, 0, D3DFMT_A8R8G8B8,
614             D3DPOOL_DEFAULT, &texture, NULL);
615     ok(SUCCEEDED(hr), "CreateCubeTexture failed (0x%08x)\n", hr);
616
617     /* Create cube faces */
618     for (face = 0; face < 6; ++face)
619     {
620         IDirect3DSurface9 *face_surface = NULL;
621
622         hr= IDirect3DCubeTexture9_GetCubeMapSurface(texture, face, 0, &face_surface);
623         ok(SUCCEEDED(hr), "GetCubeMapSurface failed (0x%08x)\n", hr);
624
625         hr = IDirect3DDevice9_UpdateSurface(device, surface, NULL, face_surface, NULL);
626         ok(SUCCEEDED(hr), "UpdateSurface failed (0x%08x)\n", hr);
627
628         IDirect3DSurface9_Release(face_surface);
629     }
630
631     hr = IDirect3DDevice9_SetTexture(device, 0, (IDirect3DBaseTexture9 *)texture);
632     ok(SUCCEEDED(hr), "SetTexture failed (0x%08x)\n", hr);
633
634     hr = IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
635     ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_MINFILTER failed (0x%08x)\n", hr);
636     hr = IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
637     ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_MAGFILTER failed (0x%08x)\n", hr);
638     hr = IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_BORDERCOLOR, 0xff00ff00);
639     ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_BORDERCOLOR failed (0x%08x)\n", hr);
640
641     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
642     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
643
644     for (x = 0; x < (sizeof(address_modes) / sizeof(*address_modes)); ++x)
645     {
646         DWORD color;
647
648         hr = IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_ADDRESSU, address_modes[x].mode);
649         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_ADDRESSU (%s) failed (0x%08x)\n", address_modes[x].name, hr);
650         hr = IDirect3DDevice9_SetSamplerState(device, 0, D3DSAMP_ADDRESSV, address_modes[x].mode);
651         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_ADDRESSV (%s) failed (0x%08x)\n", address_modes[x].name, hr);
652
653         hr = IDirect3DDevice9_BeginScene(device);
654         ok(SUCCEEDED(hr), "BeginScene failed (0x%08x)\n", hr);
655
656         hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], sizeof(quad[0]));
657         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (0x%08x)\n", hr);
658
659         hr = IDirect3DDevice9_EndScene(device);
660         ok(SUCCEEDED(hr), "EndScene failed (0x%08x)\n", hr);
661
662         hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
663         ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
664
665         /* Due to the nature of this test, we sample essentially at the edge
666          * between two faces. Because of this it's undefined from which face
667          * the driver will sample. Furtunately that's not important for this
668          * test, since all we care about is that it doesn't sample from the
669          * other side of the surface or from the border. */
670         color = getPixelColor(device, 320, 240);
671         ok(color == 0x00ff0000 || color == 0x000000ff,
672                 "Got color 0x%08x for addressing mode %s, expected 0x00ff0000 or 0x000000ff.\n",
673                 color, address_modes[x].name);
674
675         hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0, 0.0f, 0);
676         ok(SUCCEEDED(hr), "Clear failed (0x%08x)\n", hr);
677     }
678
679     hr = IDirect3DDevice9_SetTexture(device, 0, NULL);
680     ok(SUCCEEDED(hr), "SetTexture failed (0x%08x)\n", hr);
681
682     IDirect3DVertexDeclaration9_Release(vertex_declaration);
683     IDirect3DCubeTexture9_Release(texture);
684     IDirect3DSurface9_Release(surface);
685 }
686
687 /* This test tests fog in combination with shaders.
688  * What's tested: linear fog (vertex and table) with pixel shader
689  *                linear table fog with non foggy vertex shader
690  *                vertex fog with foggy vertex shader
691  * What's not tested: non linear fog with shader
692  *                    table fog with foggy vertex shader
693  */
694 static void fog_with_shader_test(IDirect3DDevice9 *device)
695 {
696     HRESULT hr;
697     DWORD color;
698     union {
699         float f;
700         DWORD i;
701     } start, end;
702     unsigned int i, j;
703
704     /* basic vertex shader without fog computation ("non foggy") */
705     static const DWORD vertex_shader_code1[] = {
706         0xfffe0101,                                                             /* vs_1_1                       */
707         0x0000001f, 0x80000000, 0x900f0000,                                     /* dcl_position v0              */
708         0x0000001f, 0x8000000a, 0x900f0001,                                     /* dcl_color0 v1                */
709         0x00000001, 0xc00f0000, 0x90e40000,                                     /* mov oPos, v0                 */
710         0x00000001, 0xd00f0000, 0x90e40001,                                     /* mov oD0, v1                  */
711         0x0000ffff
712     };
713     /* basic vertex shader with reversed fog computation ("foggy") */
714     static const DWORD vertex_shader_code2[] = {
715         0xfffe0101,                                                             /* vs_1_1                        */
716         0x0000001f, 0x80000000, 0x900f0000,                                     /* dcl_position v0               */
717         0x0000001f, 0x8000000a, 0x900f0001,                                     /* dcl_color0 v1                 */
718         0x00000051, 0xa00f0000, 0xbfa00000, 0x00000000, 0xbf666666, 0x00000000, /* def c0, -1.25, 0.0, -0.9, 0.0 */
719         0x00000001, 0xc00f0000, 0x90e40000,                                     /* mov oPos, v0                  */
720         0x00000001, 0xd00f0000, 0x90e40001,                                     /* mov oD0, v1                   */
721         0x00000002, 0x800f0000, 0x90aa0000, 0xa0aa0000,                         /* add r0, v0.z, c0.z            */
722         0x00000005, 0xc00f0001, 0x80000000, 0xa0000000,                         /* mul oFog, r0.x, c0.x          */
723         0x0000ffff
724     };
725     /* basic pixel shader */
726     static const DWORD pixel_shader_code[] = {
727         0xffff0101,                                                             /* ps_1_1     */
728         0x00000001, 0x800f0000, 0x90e40000,                                     /* mov r0, vo */
729         0x0000ffff
730     };
731
732     static struct vertex quad[] = {
733         {-1.0f, -1.0f,  0.0f,          0xFFFF0000  },
734         {-1.0f,  1.0f,  0.0f,          0xFFFF0000  },
735         { 1.0f, -1.0f,  0.0f,          0xFFFF0000  },
736         { 1.0f,  1.0f,  0.0f,          0xFFFF0000  },
737     };
738
739     static const D3DVERTEXELEMENT9 decl_elements[] = {
740         {0,  0, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
741         {0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT,    D3DDECLUSAGE_COLOR, 0},
742         D3DDECL_END()
743     };
744
745     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
746     IDirect3DVertexShader9      *vertex_shader[3]   = {NULL, NULL, NULL};
747     IDirect3DPixelShader9       *pixel_shader[2]    = {NULL, NULL};
748
749     /* This reference data was collected on a nVidia GeForce 7600GS driver version 84.19 DirectX version 9.0c on Windows XP */
750     static const struct test_data_t {
751         int vshader;
752         int pshader;
753         D3DFOGMODE vfog;
754         D3DFOGMODE tfog;
755         unsigned int color[11];
756     } test_data[] = {
757         /* only pixel shader: */
758         {0, 1, 0, 3,
759         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
760          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
761         {0, 1, 1, 3,
762         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
763          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
764         {0, 1, 2, 3,
765         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
766          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
767         {0, 1, 3, 0,
768         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
769          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
770         {0, 1, 3, 3,
771         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
772          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
773
774         /* vertex shader */
775         {1, 0, 0, 0,
776         {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00,
777          0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00}},
778         {1, 0, 0, 3,
779         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
780          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
781         {1, 0, 1, 3,
782         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
783          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
784         {1, 0, 2, 3,
785         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
786          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
787         {1, 0, 3, 3,
788         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
789          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
790
791         /* vertex shader and pixel shader */
792         {1, 1, 0, 3,
793         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
794          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
795         {1, 1, 1, 3,
796         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
797          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
798         {1, 1, 2, 3,
799         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
800          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
801         {1, 1, 3, 3,
802         {0x0000ff00, 0x0000ff00, 0x0020df00, 0x0040bf00, 0x005fa000, 0x007f8000,
803          0x009f6000, 0x00bf4000, 0x00df2000, 0x00ff0000, 0x00ff0000}},
804
805         /* foggy vertex shader */
806         {2, 0, 0, 0,
807         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
808          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
809         {2, 0, 1, 0,
810         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
811          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
812         {2, 0, 2, 0,
813         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
814          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
815         {2, 0, 3, 0,
816         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
817          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
818
819         /* foggy vertex shader and pixel shader */
820         {2, 1, 0, 0,
821         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
822          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
823         {2, 1, 1, 0,
824         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
825          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
826         {2, 1, 2, 0,
827         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
828          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
829         {2, 1, 3, 0,
830         {0x00ff0000, 0x00fe0100, 0x00de2100, 0x00bf4000, 0x009f6000, 0x007f8000,
831          0x005fa000, 0x003fc000, 0x001fe000, 0x0000ff00, 0x0000ff00}},
832
833     };
834
835     /* NOTE: changing these values will not affect the tests with foggy vertex shader, as the values are hardcoded in the shader*/
836     start.f=0.9f;
837     end.f=0.1f;
838
839     hr = IDirect3DDevice9_CreateVertexShader(device, vertex_shader_code1, &vertex_shader[1]);
840     ok(SUCCEEDED(hr), "CreateVertexShader failed (%08x)\n", hr);
841     hr = IDirect3DDevice9_CreateVertexShader(device, vertex_shader_code2, &vertex_shader[2]);
842     ok(SUCCEEDED(hr), "CreateVertexShader failed (%08x)\n", hr);
843     hr = IDirect3DDevice9_CreatePixelShader(device, pixel_shader_code, &pixel_shader[1]);
844     ok(SUCCEEDED(hr), "CreatePixelShader failed (%08x)\n", hr);
845     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
846     ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (%08x)\n", hr);
847
848     /* Setup initial states: No lighting, fog on, fog color */
849     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
850     ok(hr == D3D_OK, "Turning off lighting failed (%08x)\n", hr);
851     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, TRUE);
852     ok(hr == D3D_OK, "Turning on fog calculations failed (%08x)\n", hr);
853     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGCOLOR, 0xFF00FF00 /* A nice green */);
854     ok(hr == D3D_OK, "Setting fog color failed (%08x)\n", hr);
855     hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
856     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (%08x)\n", hr);
857
858     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_NONE);
859     ok(hr == D3D_OK, "Turning off table fog failed (%08x)\n", hr);
860     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
861     ok(hr == D3D_OK, "Turning off vertex fog failed (%08x)\n", hr);
862
863     /* Use fogtart = 0.1 and end = 0.9 to test behavior outside the fog transition phase, too*/
864     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGSTART, start.i);
865     ok(hr == D3D_OK, "Setting fog start failed (%08x)\n", hr);
866     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGEND, end.i);
867     ok(hr == D3D_OK, "Setting fog end failed (%08x)\n", hr);
868
869     for (i = 0; i < 22; i++)
870     {
871         hr = IDirect3DDevice9_SetVertexShader(device, vertex_shader[test_data[i].vshader]);
872         ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
873         hr = IDirect3DDevice9_SetPixelShader(device, pixel_shader[test_data[i].pshader]);
874         ok(SUCCEEDED(hr), "SetPixelShader failed (%08x)\n", hr);
875         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGVERTEXMODE, test_data[i].vfog);
876         ok( hr == D3D_OK, "Setting fog vertex mode to D3DFOG_LINEAR failed (%08x)\n", hr);
877         hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGTABLEMODE, test_data[i].tfog);
878         ok( hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR failed (%08x)\n", hr);
879
880         for(j=0; j < 11; j++)
881         {
882             /* Don't use the whole zrange to prevent rounding errors */
883             quad[0].z = 0.001f + (float)j / 10.02f;
884             quad[1].z = 0.001f + (float)j / 10.02f;
885             quad[2].z = 0.001f + (float)j / 10.02f;
886             quad[3].z = 0.001f + (float)j / 10.02f;
887
888             hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
889             ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed (%08x)\n", hr);
890
891             hr = IDirect3DDevice9_BeginScene(device);
892             ok( hr == D3D_OK, "BeginScene returned failed (%08x)\n", hr);
893
894             hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], sizeof(quad[0]));
895             ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%08x)\n", hr);
896
897             hr = IDirect3DDevice9_EndScene(device);
898             ok(hr == D3D_OK, "EndScene failed (%08x)\n", hr);
899
900             IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
901
902             /* As the red and green component are the result of blending use 5% tolerance on the expected value */
903             color = getPixelColor(device, 128, 240);
904             ok((unsigned char)(color) == ((unsigned char)test_data[i].color[j])
905                     && abs( ((unsigned char)(color>>8)) - (unsigned char)(test_data[i].color[j]>>8) ) < 13
906                     && abs( ((unsigned char)(color>>16)) - (unsigned char)(test_data[i].color[j]>>16) ) < 13,
907                     "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]);
908         }
909     }
910
911     /* reset states */
912     hr = IDirect3DDevice9_SetVertexShader(device, NULL);
913     ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
914     hr = IDirect3DDevice9_SetPixelShader(device, NULL);
915     ok(SUCCEEDED(hr), "SetPixelShader failed (%08x)\n", hr);
916     hr = IDirect3DDevice9_SetVertexDeclaration(device, NULL);
917     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (%08x)\n", hr);
918     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
919     ok(hr == D3D_OK, "Turning off fog calculations failed (%08x)\n", hr);
920
921     IDirect3DVertexShader9_Release(vertex_shader[1]);
922     IDirect3DVertexShader9_Release(vertex_shader[2]);
923     IDirect3DPixelShader9_Release(pixel_shader[1]);
924     IDirect3DVertexDeclaration9_Release(vertex_declaration);
925 }
926
927 /* test the behavior of the texbem instruction
928  * with normal 2D and projective 2D textures
929  */
930 static void texbem_test(IDirect3DDevice9 *device)
931 {
932     HRESULT hr;
933     DWORD color;
934     unsigned int i, x, y;
935
936     static const DWORD pixel_shader_code[] = {
937         0xffff0101,                         /* ps_1_1*/
938         0x00000042, 0xb00f0000,             /* tex t0*/
939         0x00000043, 0xb00f0001, 0xb0e40000, /* texbem t1, t0*/
940         0x00000001, 0x800f0000, 0xb0e40001, /* mov r0, t1*/
941         0x0000ffff
942     };
943
944     static const float quad[][7] = {
945         {-128.0f/640.0f, -128.0f/480.0f, 0.1f, 0.0f, 0.0f, 0.0f, 0.0f},
946         {-128.0f/640.0f,  128.0f/480.0f, 0.1f, 0.0f, 1.0f, 0.0f, 1.0f},
947         { 128.0f/640.0f, -128.0f/480.0f, 0.1f, 1.0f, 0.0f, 1.0f, 0.0f},
948         { 128.0f/640.0f,  128.0f/480.0f, 0.1f, 1.0f, 1.0f, 1.0f, 1.0f},
949     };
950     static const float quad_proj[][9] = {
951         {-128.0f/640.0f, -128.0f/480.0f, 0.1f, 0.0f, 0.0f,   0.0f,   0.0f, 0.0f, 128.0f},
952         {-128.0f/640.0f,  128.0f/480.0f, 0.1f, 0.0f, 1.0f,   0.0f, 128.0f, 0.0f, 128.0f},
953         { 128.0f/640.0f, -128.0f/480.0f, 0.1f, 1.0f, 0.0f, 128.0f,   0.0f, 0.0f, 128.0f},
954         { 128.0f/640.0f,  128.0f/480.0f, 0.1f, 1.0f, 1.0f, 128.0f, 128.0f, 0.0f, 128.0f},
955     };
956
957     static const D3DVERTEXELEMENT9 decl_elements[][4] = { {
958         {0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
959         {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
960         {0, 20, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
961         D3DDECL_END()
962     },{
963         {0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
964         {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
965         {0, 20, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
966         D3DDECL_END()
967     } };
968
969     /* use assymetric matrix to test loading */
970     float bumpenvmat[4] = {0.0,0.5,-0.5,0.0};
971
972     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
973     IDirect3DPixelShader9       *pixel_shader       = NULL;
974     IDirect3DTexture9           *texture[2]         = {NULL, NULL};
975     D3DLOCKED_RECT locked_rect;
976
977     /* Generate the textures */
978     for(i=0; i<2; i++)
979     {
980         hr = IDirect3DDevice9_CreateTexture(device, 128, 128, 1, 0, i?D3DFMT_A8R8G8B8:D3DFMT_V8U8,
981                 D3DPOOL_MANAGED, &texture[i], NULL);
982         ok(SUCCEEDED(hr), "CreateTexture failed (0x%08x)\n", hr);
983
984         hr = IDirect3DTexture9_LockRect(texture[i], 0, &locked_rect, NULL, D3DLOCK_DISCARD);
985         ok(SUCCEEDED(hr), "LockRect failed (0x%08x)\n", hr);
986         for (y = 0; y < 128; ++y)
987         {
988             if(i)
989             { /* Set up black texture with 2x2 texel white spot in the middle */
990                 DWORD *ptr = (DWORD *)(((BYTE *)locked_rect.pBits) + (y * locked_rect.Pitch));
991                 for (x = 0; x < 128; ++x)
992                 {
993                     if(y>62 && y<66 && x>62 && x<66)
994                         *ptr++ = 0xffffffff;
995                     else
996                         *ptr++ = 0xff000000;
997                 }
998             }
999             else
1000             { /* Set up a displacement map which points away from the center parallel to the closest axis.
1001               * (if multiplied with bumpenvmat)
1002               */
1003                 WORD *ptr = (WORD *)(((BYTE *)locked_rect.pBits) + (y * locked_rect.Pitch));
1004                 for (x = 0; x < 128; ++x)
1005                 {
1006                     if(abs(x-64)>abs(y-64))
1007                     {
1008                         if(x < 64)
1009                             *ptr++ = 0xc000;
1010                         else
1011                             *ptr++ = 0x4000;
1012                     }
1013                     else
1014                     {
1015                         if(y < 64)
1016                             *ptr++ = 0x0040;
1017                         else
1018                             *ptr++ = 0x00c0;
1019                     }
1020                 }
1021             }
1022         }
1023         hr = IDirect3DTexture9_UnlockRect(texture[i], 0);
1024         ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
1025
1026         hr = IDirect3DDevice9_SetTexture(device, i, (IDirect3DBaseTexture9 *)texture[i]);
1027         ok(SUCCEEDED(hr), "SetTexture failed (0x%08x)\n", hr);
1028
1029         /* Disable texture filtering */
1030         hr = IDirect3DDevice9_SetSamplerState(device, i, D3DSAMP_MINFILTER, D3DTEXF_POINT);
1031         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_MINFILTER failed (0x%08x)\n", hr);
1032         hr = IDirect3DDevice9_SetSamplerState(device, i, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
1033         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_MAGFILTER failed (0x%08x)\n", hr);
1034
1035         hr = IDirect3DDevice9_SetSamplerState(device, i, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
1036         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_ADDRESSU failed (0x%08x)\n", hr);
1037         hr = IDirect3DDevice9_SetSamplerState(device, i, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
1038         ok(SUCCEEDED(hr), "SetSamplerState D3DSAMP_ADDRESSV failed (0x%08x)\n", hr);
1039     }
1040
1041     IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_BUMPENVMAT00, *(LPDWORD)&bumpenvmat[0]);
1042     IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_BUMPENVMAT01, *(LPDWORD)&bumpenvmat[1]);
1043     IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_BUMPENVMAT10, *(LPDWORD)&bumpenvmat[2]);
1044     hr = IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_BUMPENVMAT11, *(LPDWORD)&bumpenvmat[3]);
1045     ok(SUCCEEDED(hr), "SetTextureStageState failed (%08x)\n", hr);
1046
1047     hr = IDirect3DDevice9_SetVertexShader(device, NULL);
1048     ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
1049
1050     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
1051     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed (%08x)\n", hr);
1052
1053     for(i=0; i<2; i++)
1054     {
1055         if(i)
1056         {
1057             hr = IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4|D3DTTFF_PROJECTED);
1058             ok(SUCCEEDED(hr), "SetTextureStageState D3DTSS_TEXTURETRANSFORMFLAGS failed (0x%08x)\n", hr);
1059         }
1060
1061         hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements[i], &vertex_declaration);
1062         ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (0x%08x)\n", hr);
1063         hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
1064         ok(SUCCEEDED(hr), "SetVertexDeclaration failed (0x%08x)\n", hr);
1065
1066         hr = IDirect3DDevice9_CreatePixelShader(device, pixel_shader_code, &pixel_shader);
1067         ok(SUCCEEDED(hr), "CreatePixelShader failed (%08x)\n", hr);
1068         hr = IDirect3DDevice9_SetPixelShader(device, pixel_shader);
1069         ok(SUCCEEDED(hr), "SetPixelShader failed (%08x)\n", hr);
1070
1071         hr = IDirect3DDevice9_BeginScene(device);
1072         ok(SUCCEEDED(hr), "BeginScene failed (0x%08x)\n", hr);
1073
1074         if(!i)
1075             hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], sizeof(quad[0]));
1076         else
1077             hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad_proj[0], sizeof(quad_proj[0]));
1078         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (0x%08x)\n", hr);
1079
1080         hr = IDirect3DDevice9_EndScene(device);
1081         ok(SUCCEEDED(hr), "EndScene failed (0x%08x)\n", hr);
1082
1083         hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
1084         ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1085
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+32, 240);
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         color = getPixelColor(device, 320, 240+32);
1093         ok(color == 0x00ffffff, "texbem failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
1094
1095         hr = IDirect3DDevice9_SetPixelShader(device, NULL);
1096         ok(SUCCEEDED(hr), "SetPixelShader failed (%08x)\n", hr);
1097         IDirect3DPixelShader9_Release(pixel_shader);
1098
1099         hr = IDirect3DDevice9_SetVertexDeclaration(device, NULL);
1100         ok(SUCCEEDED(hr), "SetVertexDeclaration failed (%08x)\n", hr);
1101         IDirect3DVertexDeclaration9_Release(vertex_declaration);
1102     }
1103
1104     /* clean up */
1105     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0, 0.0f, 0);
1106     ok(SUCCEEDED(hr), "Clear failed (0x%08x)\n", hr);
1107
1108     hr = IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
1109     ok(SUCCEEDED(hr), "SetTextureStageState D3DTSS_TEXTURETRANSFORMFLAGS failed (0x%08x)\n", hr);
1110
1111     for(i=0; i<2; i++)
1112     {
1113         hr = IDirect3DDevice9_SetTexture(device, i, NULL);
1114         ok(SUCCEEDED(hr), "SetTexture failed (0x%08x)\n", hr);
1115         IDirect3DCubeTexture9_Release(texture[i]);
1116     }
1117 }
1118
1119 static void present_test(IDirect3DDevice9 *device)
1120 {
1121     struct vertex quad[] =
1122     {
1123         {-1.0f, -1.0f,   0.9f,                          0xffff0000},
1124         {-1.0f,  1.0f,   0.9f,                          0xffff0000},
1125         { 1.0f, -1.0f,   0.1f,                          0xffff0000},
1126         { 1.0f,  1.0f,   0.1f,                          0xffff0000},
1127     };
1128     HRESULT hr;
1129     DWORD color;
1130
1131     /* Does the Present clear the depth stencil? Clear the depth buffer with some value != 0,
1132      * then call Present. Then clear the color buffer to make sure it has some defined content
1133      * after the Present with D3DSWAPEFFECT_DISCARD. After that draw a plane that is somewhere cut
1134      * by the depth value.
1135      */
1136     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 0.75, 0);
1137     ok(hr == D3D_OK, "IDirect3DDevice9_Clear returned %s\n", DXGetErrorString9(hr));
1138     hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
1139     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.4, 0);
1140
1141     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
1142     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
1143     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_GREATER);
1144     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
1145     hr = IDirect3DDevice9_SetFVF(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
1146     ok(hr == D3D_OK, "IDirect3DDevice9_SetFVF returned %s\n", DXGetErrorString9(hr));
1147
1148     hr = IDirect3DDevice9_BeginScene(device);
1149     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
1150     if(hr == D3D_OK)
1151     {
1152         /* No lights are defined... That means, lit vertices should be entirely black */
1153         hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2 /*PrimCount */, quad, sizeof(quad[0]));
1154         ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
1155
1156         hr = IDirect3DDevice9_EndScene(device);
1157         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
1158     }
1159
1160     hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ZENABLE, D3DZB_FALSE);
1161     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
1162
1163     hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
1164     ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1165     color = getPixelColor(device, 512, 240);
1166     ok(color == 0x00ffffff, "Present failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
1167     color = getPixelColor(device, 64, 240);
1168     ok(color == 0x00ff0000, "Present failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
1169 }
1170
1171 START_TEST(visual)
1172 {
1173     IDirect3DDevice9 *device_ptr;
1174     D3DCAPS9 caps;
1175     HRESULT hr;
1176     DWORD color;
1177
1178     d3d9_handle = LoadLibraryA("d3d9.dll");
1179     if (!d3d9_handle)
1180     {
1181         skip("Could not load d3d9.dll\n");
1182         return;
1183     }
1184
1185     device_ptr = init_d3d9();
1186     if (!device_ptr) return;
1187
1188     IDirect3DDevice9_GetDeviceCaps(device_ptr, &caps);
1189
1190     /* Check for the reliability of the returned data */
1191     hr = IDirect3DDevice9_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
1192     if(FAILED(hr))
1193     {
1194         trace("Clear failed, can't assure correctness of the test results, skipping\n");
1195         goto cleanup;
1196     }
1197     IDirect3DDevice9_Present(device_ptr, NULL, NULL, NULL, NULL);
1198
1199     color = getPixelColor(device_ptr, 1, 1);
1200     if(color !=0x00ff0000)
1201     {
1202         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
1203         goto cleanup;
1204     }
1205
1206     hr = IDirect3DDevice9_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
1207     if(FAILED(hr))
1208     {
1209         trace("Clear failed, can't assure correctness of the test results, skipping\n");
1210         goto cleanup;
1211     }
1212     IDirect3DDevice9_Present(device_ptr, NULL, NULL, NULL, NULL);
1213
1214     color = getPixelColor(device_ptr, 639, 479);
1215     if(color != 0x0000ddee)
1216     {
1217         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
1218         goto cleanup;
1219     }
1220
1221     /* Now execute the real tests */
1222     lighting_test(device_ptr);
1223     clear_test(device_ptr);
1224     fog_test(device_ptr);
1225     test_cube_wrap(device_ptr);
1226     present_test(device_ptr);
1227
1228     if (caps.VertexShaderVersion >= D3DVS_VERSION(2, 0))
1229     {
1230         test_mova(device_ptr);
1231     }
1232     else skip("No vs_2_0 support\n");
1233
1234     if (caps.VertexShaderVersion >= D3DVS_VERSION(1, 1) && caps.PixelShaderVersion >= D3DVS_VERSION(1, 1))
1235     {
1236         fog_with_shader_test(device_ptr);
1237     }
1238     else skip("No vs_1_1 and ps_1_1 support\n");
1239
1240     if (caps.PixelShaderVersion >= D3DVS_VERSION(1, 1))
1241     {
1242         texbem_test(device_ptr);
1243     }
1244     else skip("No ps_1_1 support\n");
1245
1246 cleanup:
1247     if(device_ptr) IDirect3DDevice9_Release(device_ptr);
1248 }