quartz: Use proper alloc/free functions for COM objects.
[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.0, 0.0, 0.0, 0.0,
147                       0.0, 1.0, 0.0, 0.0,
148                       0.0, 0.0, 1.0, 0.0,
149                       0.0, 0.0, 0.0, 1.0 };
150
151     struct vertex unlitquad[] =
152     {
153         {-1.0,  -1.0,    0.1,                           0xffff0000},
154         {-1.0,   0.0,    0.1,                           0xffff0000},
155         { 0.0,   0.0,    0.1,                           0xffff0000},
156         { 0.0,  -1.0,    0.1,                           0xffff0000},
157     };
158     struct vertex litquad[] =
159     {
160         {-1.0,   0.0,    0.1,                           0xff00ff00},
161         {-1.0,   1.0,    0.1,                           0xff00ff00},
162         { 0.0,   1.0,    0.1,                           0xff00ff00},
163         { 0.0,   0.0,    0.1,                           0xff00ff00},
164     };
165     struct nvertex unlitnquad[] =
166     {
167         { 0.0,  -1.0,    0.1,   1.0,    1.0,    1.0,    0xff0000ff},
168         { 0.0,   0.0,    0.1,   1.0,    1.0,    1.0,    0xff0000ff},
169         { 1.0,   0.0,    0.1,   1.0,    1.0,    1.0,    0xff0000ff},
170         { 1.0,  -1.0,    0.1,   1.0,    1.0,    1.0,    0xff0000ff},
171     };
172     struct nvertex litnquad[] =
173     {
174         { 0.0,   0.0,    0.1,   1.0,    1.0,    1.0,    0xffffff00},
175         { 0.0,   1.0,    0.1,   1.0,    1.0,    1.0,    0xffffff00},
176         { 1.0,   1.0,    0.1,   1.0,    1.0,    1.0,    0xffffff00},
177         { 1.0,   0.0,    0.1,   1.0,    1.0,    1.0,    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, D3DCOLORWRITEENABLED_RED | D3DCOLORWRITEENABLED_GREEN | D3DCOLORWRITEENABLED_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     /* Hack for a bug in d3d9: SetFVF creates a converted vertex declaration, with a circular refcount.
264      * This prevents the screen resolution from being restored correctly on device release. Unset the vdecl
265      */
266     IDirect3DDevice9_SetVertexDeclaration(device, NULL);
267 }
268
269 static void clear_test(IDirect3DDevice9 *device)
270 {
271     /* Tests the correctness of clearing parameters */
272     HRESULT hr;
273     D3DRECT rect[2];
274     D3DRECT rect_negneg;
275     DWORD color;
276
277     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
278     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
279
280     /* Positive x, negative y */
281     rect[0].x1 = 0;
282     rect[0].y1 = 480;
283     rect[0].x2 = 320;
284     rect[0].y2 = 240;
285
286     /* Positive x, positive y */
287     rect[1].x1 = 0;
288     rect[1].y1 = 0;
289     rect[1].x2 = 320;
290     rect[1].y2 = 240;
291     /* Clear 2 rectangles with one call. Shows that a positive value is returned, but the negative rectangle
292      * is ignored, the positive is still cleared afterwards
293      */
294     hr = IDirect3DDevice9_Clear(device, 2, rect, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
295     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
296
297     /* negative x, negative y */
298     rect_negneg.x1 = 640;
299     rect_negneg.x1 = 240;
300     rect_negneg.x2 = 320;
301     rect_negneg.y2 = 0;
302     hr = IDirect3DDevice9_Clear(device, 1, &rect_negneg, D3DCLEAR_TARGET, 0xff00ff00, 0.0, 0);
303     ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
304
305     IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
306
307     color = getPixelColor(device, 160, 360); /* lower left quad */
308     ok(color == 0x00ffffff, "Clear rectangle 3(pos, neg) has color %08x\n", color);
309     color = getPixelColor(device, 160, 120); /* upper left quad */
310     ok(color == 0x00ff0000, "Clear rectangle 1(pos, pos) has color %08x\n", color);
311     color = getPixelColor(device, 480, 360); /* lower right quad  */
312     ok(color == 0x00ffffff, "Clear rectangle 4(NULL) has color %08x\n", color);
313     color = getPixelColor(device, 480, 120); /* upper right quad */
314     ok(color == 0x00ffffff, "Clear rectangle 4(neg, neg) has color %08x\n", color);
315 }
316
317 typedef struct {
318     float in[4];
319     DWORD out;
320 } test_data_t;
321
322 /*
323  *  c7      rounded     ARGB
324  * -2.4     -2          0x00ffff00
325  * -1.6     -2          0x00ffff00
326  * -0.4      0          0x0000ffff
327  *  0.4      0          0x0000ffff
328  *  1.6      2          0x00ff00ff
329  *  2.4      2          0x00ff00ff
330  */
331 static void test_mova(IDirect3DDevice9 *device)
332 {
333     static const DWORD mova_test[] = {
334         0xfffe0200,                                                             /* vs_2_0                       */
335         0x0200001f, 0x80000000, 0x900f0000,                                     /* dcl_position v0              */
336         0x05000051, 0xa00f0000, 0x3f800000, 0x00000000, 0x00000000, 0x3f800000, /* def c0, 1.0, 0.0, 0.0, 1.0   */
337         0x05000051, 0xa00f0001, 0x3f800000, 0x3f800000, 0x00000000, 0x3f800000, /* def c1, 1.0, 1.0, 0.0, 1.0   */
338         0x05000051, 0xa00f0002, 0x00000000, 0x3f800000, 0x00000000, 0x3f800000, /* def c2, 0.0, 1.0, 0.0, 1.0   */
339         0x05000051, 0xa00f0003, 0x00000000, 0x3f800000, 0x3f800000, 0x3f800000, /* def c3, 0.0, 1.0, 1.0, 1.0   */
340         0x05000051, 0xa00f0004, 0x00000000, 0x00000000, 0x3f800000, 0x3f800000, /* def c4, 0.0, 0.0, 1.0, 1.0   */
341         0x05000051, 0xa00f0005, 0x3f800000, 0x00000000, 0x3f800000, 0x3f800000, /* def c5, 1.0, 0.0, 1.0, 1.0   */
342         0x05000051, 0xa00f0006, 0x3f800000, 0x3f800000, 0x3f800000, 0x3f800000, /* def c6, 1.0, 1.0, 1.0, 1.0   */
343         0x0200002e, 0xb0010000, 0xa0000007,                                     /* mova a0.x, c7.x              */
344         0x03000001, 0xd00f0000, 0xa0e42003, 0xb0000000,                         /* mov oD0, c[a0.x + 3]         */
345         0x02000001, 0xc00f0000, 0x90e40000,                                     /* mov oPos, v0                 */
346         0x0000ffff                                                              /* END                          */
347     };
348
349     static const test_data_t test_data[] = {
350         {{-2.4f, 0.0f, 0.0f, 0.0f}, 0x00ffff00},
351         {{-1.6f, 0.0f, 0.0f, 0.0f}, 0x00ffff00},
352         {{-0.4f, 0.0f, 0.0f, 0.0f}, 0x0000ffff},
353         {{ 0.4f, 0.0f, 0.0f, 0.0f}, 0x0000ffff},
354         {{ 1.6f, 0.0f, 0.0f, 0.0f}, 0x00ff00ff},
355         {{ 2.4f, 0.0f, 0.0f, 0.0f}, 0x00ff00ff}
356     };
357
358     static const float quad[][3] = {
359         {-1.0f, -1.0f, 0.0f},
360         {-1.0f,  1.0f, 0.0f},
361         { 1.0f, -1.0f, 0.0f},
362         { 1.0f,  1.0f, 0.0f},
363     };
364
365     static const D3DVERTEXELEMENT9 decl_elements[] = {
366         {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
367         D3DDECL_END()
368     };
369
370     IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
371     IDirect3DVertexShader9 *mova_shader = NULL;
372     HRESULT hr;
373     int i;
374
375     hr = IDirect3DDevice9_CreateVertexShader(device, mova_test, &mova_shader);
376     ok(SUCCEEDED(hr), "CreateVertexShader failed (%08x)\n", hr);
377     hr = IDirect3DDevice9_SetVertexShader(device, mova_shader);
378     ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
379
380     hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
381     ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (%08x)\n", hr);
382     hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
383     ok(SUCCEEDED(hr), "SetVertexDeclaration failed (%08x)\n", hr);
384
385     for (i = 0; i < (sizeof(test_data) / sizeof(test_data_t)); ++i)
386     {
387         DWORD color;
388
389         hr = IDirect3DDevice9_SetVertexShaderConstantF(device, 7, test_data[i].in, 1);
390         ok(SUCCEEDED(hr), "SetVertexShaderConstantF failed (%08x)\n", hr);
391
392         hr = IDirect3DDevice9_BeginScene(device);
393         ok(SUCCEEDED(hr), "BeginScene failed (%08x)\n", hr);
394
395         hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
396         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%08x)\n", hr);
397
398         hr = IDirect3DDevice9_EndScene(device);
399         ok(SUCCEEDED(hr), "EndScene failed (%08x)\n", hr);
400
401         hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
402         ok(SUCCEEDED(hr), "Present failed (%08x)\n", hr);
403
404         color = getPixelColor(device, 320, 240);
405         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]);
406
407         hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0, 0.0f, 0);
408         ok(SUCCEEDED(hr), "Clear failed (%08x)\n", hr);
409     }
410
411     IDirect3DVertexDeclaration9_Release(vertex_declaration);
412     IDirect3DVertexShader9_Release(mova_shader);
413 }
414
415 START_TEST(visual)
416 {
417     IDirect3DDevice9 *device_ptr;
418     D3DCAPS9 caps;
419     HRESULT hr;
420     DWORD color;
421
422     d3d9_handle = LoadLibraryA("d3d9.dll");
423     if (!d3d9_handle)
424     {
425         trace("Could not load d3d9.dll, skipping tests\n");
426         return;
427     }
428
429     device_ptr = init_d3d9();
430     if (!device_ptr) return;
431
432     IDirect3DDevice9_GetDeviceCaps(device_ptr, &caps);
433
434     /* Check for the reliability of the returned data */
435     hr = IDirect3DDevice9_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
436     if(FAILED(hr))
437     {
438         trace("Clear failed, can't assure correctness of the test results, skipping\n");
439         goto cleanup;
440     }
441     IDirect3DDevice9_Present(device_ptr, NULL, NULL, NULL, NULL);
442
443     color = getPixelColor(device_ptr, 1, 1);
444     if(color !=0x00ff0000)
445     {
446         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
447         goto cleanup;
448     }
449
450     hr = IDirect3DDevice9_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
451     if(FAILED(hr))
452     {
453         trace("Clear failed, can't assure correctness of the test results, skipping\n");
454         goto cleanup;
455     }
456     IDirect3DDevice9_Present(device_ptr, NULL, NULL, NULL, NULL);
457
458     color = getPixelColor(device_ptr, 639, 479);
459     if(color != 0x0000ddee)
460     {
461         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
462         goto cleanup;
463     }
464
465     /* Now execute the real tests */
466     lighting_test(device_ptr);
467     clear_test(device_ptr);
468
469     if (caps.VertexShaderVersion >= D3DVS_VERSION(2, 0))
470     {
471         test_mova(device_ptr);
472     }
473     else skip("No vs_2_0 support\n");
474
475 cleanup:
476     if(device_ptr) IDirect3DDevice9_Release(device_ptr);
477 }