mshtml: Added put_backgroundImage implementation.
[wine] / dlls / d3d8 / tests / visual.c
1 /*
2  * Copyright (C) 2005 Henri Verbeet
3  * Copyright (C) 2007 Stefan Dösinger(for CodeWeavers)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 /* See comment in dlls/d3d9/tests/visual.c for general guidelines */
21
22 #define COBJMACROS
23 #include <d3d8.h>
24 #include <dxerr8.h>
25 #include "wine/test.h"
26
27 static HMODULE d3d8_handle = 0;
28
29 static HWND create_window(void)
30 {
31     WNDCLASS wc = {0};
32     HWND ret;
33     wc.lpfnWndProc = &DefWindowProc;
34     wc.lpszClassName = "d3d8_test_wc";
35     RegisterClass(&wc);
36
37     ret = CreateWindow("d3d8_test_wc", "d3d8_test",
38                         WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
39     return ret;
40 }
41
42 static DWORD getPixelColor(IDirect3DDevice8 *device, UINT x, UINT y)
43 {
44     DWORD ret;
45     IDirect3DSurface8 *surf;
46     IDirect3DTexture8 *tex;
47     HRESULT hr;
48     D3DLOCKED_RECT lockedRect;
49     RECT rectToLock = {x, y, x+1, y+1};
50
51     hr = IDirect3DDevice8_CreateTexture(device, 640, 480, 1 /* Levels */, 0 /* usage */, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &tex);
52     if(FAILED(hr) || !tex )  /* This is not a test */
53     {
54         trace("Can't create an offscreen plain surface to read the render target data, hr=%#08x\n", hr);
55         return 0xdeadbeef;
56     }
57     hr = IDirect3DTexture8_GetSurfaceLevel(tex, 0, &surf);
58     if(FAILED(hr) || !tex )  /* This is not a test */
59     {
60         trace("Can't get surface from texture, hr=%#08x\n", hr);
61         ret = 0xdeadbeee;
62         goto out;
63     }
64
65     hr = IDirect3DDevice8_GetFrontBuffer(device, surf);
66     if(FAILED(hr))
67     {
68         trace("Can't read the front buffer data, hr=%#08x\n", hr);
69         ret = 0xdeadbeed;
70         goto out;
71     }
72
73     hr = IDirect3DSurface8_LockRect(surf, &lockedRect, &rectToLock, D3DLOCK_READONLY);
74     if(FAILED(hr))
75     {
76         trace("Can't lock the offscreen surface, hr=%#08x\n", hr);
77         ret = 0xdeadbeec;
78         goto out;
79     }
80     /* Remove the X channel for now. DirectX and OpenGL have different ideas how to treat it apparently, and it isn't
81      * really important for these tests
82      */
83     ret = ((DWORD *) lockedRect.pBits)[0] & 0x00ffffff;
84     hr = IDirect3DSurface8_UnlockRect(surf);
85     if(FAILED(hr))
86     {
87         trace("Can't unlock the offscreen surface, hr=%#08x\n", hr);
88     }
89
90 out:
91     if(surf) IDirect3DSurface8_Release(surf);
92     if(tex) IDirect3DTexture8_Release(tex);
93     return ret;
94 }
95
96 static IDirect3DDevice8 *init_d3d8(void)
97 {
98     IDirect3D8 * (__stdcall * d3d8_create)(UINT SDKVersion) = 0;
99     IDirect3D8 *d3d8_ptr = 0;
100     IDirect3DDevice8 *device_ptr = 0;
101     D3DPRESENT_PARAMETERS present_parameters;
102     HRESULT hr;
103
104     d3d8_create = (void *)GetProcAddress(d3d8_handle, "Direct3DCreate8");
105     ok(d3d8_create != NULL, "Failed to get address of Direct3DCreate8\n");
106     if (!d3d8_create) return NULL;
107
108     d3d8_ptr = d3d8_create(D3D_SDK_VERSION);
109     ok(d3d8_ptr != NULL, "Failed to create IDirect3D8 object\n");
110     if (!d3d8_ptr) return NULL;
111
112     ZeroMemory(&present_parameters, sizeof(present_parameters));
113     present_parameters.Windowed = FALSE;
114     present_parameters.hDeviceWindow = create_window();
115     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
116     present_parameters.BackBufferWidth = 640;
117     present_parameters.BackBufferHeight = 480;
118     present_parameters.BackBufferFormat = D3DFMT_X8R8G8B8;
119     present_parameters.EnableAutoDepthStencil = TRUE;
120     present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
121
122     hr = IDirect3D8_CreateDevice(d3d8_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
123     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "IDirect3D_CreateDevice returned: %#08x\n", hr);
124
125     return device_ptr;
126 }
127
128 struct vertex
129 {
130     float x, y, z;
131     DWORD diffuse;
132 };
133
134 struct nvertex
135 {
136     float x, y, z;
137     float nx, ny, nz;
138     DWORD diffuse;
139 };
140
141 static void lighting_test(IDirect3DDevice8 *device)
142 {
143     HRESULT hr;
144     DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
145     DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
146     DWORD color;
147
148     float mat[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
149                       0.0f, 1.0f, 0.0f, 0.0f,
150                       0.0f, 0.0f, 1.0f, 0.0f,
151                       0.0f, 0.0f, 0.0f, 1.0f };
152
153     struct vertex unlitquad[] =
154     {
155         {-1.0f, -1.0f,   0.1f,                          0xffff0000},
156         {-1.0f,  0.0f,   0.1f,                          0xffff0000},
157         { 0.0f,  0.0f,   0.1f,                          0xffff0000},
158         { 0.0f, -1.0f,   0.1f,                          0xffff0000},
159     };
160     struct vertex litquad[] =
161     {
162         {-1.0f,  0.0f,   0.1f,                          0xff00ff00},
163         {-1.0f,  1.0f,   0.1f,                          0xff00ff00},
164         { 0.0f,  1.0f,   0.1f,                          0xff00ff00},
165         { 0.0f,  0.0f,   0.1f,                          0xff00ff00},
166     };
167     struct nvertex unlitnquad[] =
168     {
169         { 0.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
170         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
171         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
172         { 1.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
173     };
174     struct nvertex litnquad[] =
175     {
176         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
177         { 0.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
178         { 1.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
179         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
180     };
181     WORD Indices[] = {0, 1, 2, 2, 3, 0};
182
183     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
184     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
185
186     /* Setup some states that may cause issues */
187     hr = IDirect3DDevice8_SetTransform(device, D3DTS_WORLDMATRIX(0), (D3DMATRIX *) mat);
188     ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %#08x\n", hr);
189     hr = IDirect3DDevice8_SetTransform(device, D3DTS_VIEW, (D3DMATRIX *)mat);
190     ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %#08x\n", hr);
191     hr = IDirect3DDevice8_SetTransform(device, D3DTS_PROJECTION, (D3DMATRIX *) mat);
192     ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %#08x\n", hr);
193     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CLIPPING, FALSE);
194     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
195     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, FALSE);
196     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
197     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
198     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
199     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_STENCILENABLE, FALSE);
200     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
201     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHATESTENABLE, FALSE);
202     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
203     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
204     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
205     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE);
206     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed with %#08x\n", hr);
207     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
208     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed with %#08x\n", hr);
209
210     hr = IDirect3DDevice8_SetVertexShader(device, fvf);
211     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
212
213     hr = IDirect3DDevice8_BeginScene(device);
214     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
215     if(hr == D3D_OK)
216     {
217         /* No lights are defined... That means, lit vertices should be entirely black */
218         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
219         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
220         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
221                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitquad, sizeof(unlitquad[0]));
222         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
223
224         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, TRUE);
225         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
226         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
227                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, litquad, sizeof(litquad[0]));
228         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
229
230         hr = IDirect3DDevice8_SetVertexShader(device, nfvf);
231         ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader failed with %#08x\n", hr);
232
233         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
234         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
235         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
236                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitnquad, sizeof(unlitnquad[0]));
237         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
238
239         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, TRUE);
240         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
241         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
242                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, litnquad, sizeof(litnquad[0]));
243         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
244
245         IDirect3DDevice8_EndScene(device);
246         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
247     }
248
249     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
250
251     color = getPixelColor(device, 160, 360); /* lower left quad - unlit without normals */
252     ok(color == 0x00ff0000, "Unlit quad without normals has color %08x\n", color);
253     color = getPixelColor(device, 160, 120); /* upper left quad - lit without normals */
254     ok(color == 0x00000000, "Lit quad without normals has color %08x\n", color);
255     color = getPixelColor(device, 480, 360); /* lower left quad - unlit width normals */
256     ok(color == 0x000000ff, "Unlit quad width normals has color %08x\n", color);
257     color = getPixelColor(device, 480, 120); /* upper left quad - lit width normals */
258     ok(color == 0x00000000, "Lit quad width normals has color %08x\n", color);
259
260     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
261     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
262 }
263
264 static void clear_test(IDirect3DDevice8 *device)
265 {
266     /* Tests the correctness of clearing parameters */
267     HRESULT hr;
268     D3DRECT rect[2];
269     D3DRECT rect_negneg;
270     DWORD color;
271
272     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
273     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
274
275     /* Positive x, negative y */
276     rect[0].x1 = 0;
277     rect[0].y1 = 480;
278     rect[0].x2 = 320;
279     rect[0].y2 = 240;
280
281     /* Positive x, positive y */
282     rect[1].x1 = 0;
283     rect[1].y1 = 0;
284     rect[1].x2 = 320;
285     rect[1].y2 = 240;
286     /* Clear 2 rectangles with one call. Shows that a positive value is returned, but the negative rectangle
287      * is ignored, the positive is still cleared afterwards
288      */
289     hr = IDirect3DDevice8_Clear(device, 2, rect, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
290     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
291
292     /* negative x, negative y */
293     rect_negneg.x1 = 640;
294     rect_negneg.x1 = 240;
295     rect_negneg.x2 = 320;
296     rect_negneg.y2 = 0;
297     hr = IDirect3DDevice8_Clear(device, 1, &rect_negneg, D3DCLEAR_TARGET, 0xff00ff00, 0.0, 0);
298     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
299
300     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
301
302     color = getPixelColor(device, 160, 360); /* lower left quad */
303     ok(color == 0x00ffffff, "Clear rectangle 3(pos, neg) has color %08x\n", color);
304     color = getPixelColor(device, 160, 120); /* upper left quad */
305     ok(color == 0x00ff0000, "Clear rectangle 1(pos, pos) has color %08x\n", color);
306     color = getPixelColor(device, 480, 360); /* lower right quad  */
307     ok(color == 0x00ffffff, "Clear rectangle 4(NULL) has color %08x\n", color);
308     color = getPixelColor(device, 480, 120); /* upper right quad */
309     ok(color == 0x00ffffff, "Clear rectangle 4(neg, neg) has color %08x\n", color);
310 }
311
312 struct sVertex {
313     float x, y, z;
314     DWORD diffuse;
315     DWORD specular;
316 };
317
318 struct sVertexT {
319     float x, y, z, rhw;
320     DWORD diffuse;
321     DWORD specular;
322 };
323
324 static void fog_test(IDirect3DDevice8 *device)
325 {
326     HRESULT hr;
327     DWORD color;
328     float start = 0.0, end = 1.0;
329
330     /* Gets full z based fog with linear fog, no fog with specular color */
331     struct sVertex unstransformed_1[] = {
332         {-1,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
333         {-1,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
334         { 0,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
335         { 0,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
336     };
337     /* Ok, I am too lazy to deal with transform matrices */
338     struct sVertex unstransformed_2[] = {
339         {-1,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
340         {-1,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
341         { 0,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
342         { 0,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
343     };
344     /* Untransformed ones. Give them a different diffuse color to make the test look
345      * nicer. It also makes making sure that they are drawn correctly easier.
346      */
347     struct sVertexT transformed_1[] = {
348         {320,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
349         {640,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
350         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
351         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
352     };
353     struct sVertexT transformed_2[] = {
354         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
355         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
356         {640,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
357         {320,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
358     };
359     WORD Indices[] = {0, 1, 2, 2, 3, 0};
360
361     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
362     ok(hr == D3D_OK, "IDirect3DDevice8_Clear returned %#08x\n", hr);
363
364     /* Setup initial states: No lighting, fog on, fog color */
365     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
366     ok(hr == D3D_OK, "Turning off lighting returned %#08x\n", hr);
367     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, TRUE);
368     ok(hr == D3D_OK, "Turning on fog calculations returned %#08x\n", hr);
369     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGCOLOR, 0xFF00FF00 /* A nice green */);
370     ok(hr == D3D_OK, "Turning on fog calculations returned %#08x\n", hr);
371
372     /* First test: Both table fog and vertex fog off */
373     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_NONE);
374     ok(hr == D3D_OK, "Turning off table fog returned %#08x\n", hr);
375     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
376     ok(hr == D3D_OK, "Turning off table fog returned %#08x\n", hr);
377
378     /* Start = 0, end = 1. Should be default, but set them */
379     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGSTART, *((DWORD *) &start));
380     ok(hr == D3D_OK, "Setting fog start returned %#08x\n", hr);
381     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGEND, *((DWORD *) &end));
382     ok(hr == D3D_OK, "Setting fog start returned %#08x\n", hr);
383
384     if(IDirect3DDevice8_BeginScene(device) == D3D_OK)
385     {
386         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
387         ok( hr == D3D_OK, "SetVertexShader returned %#08x\n", hr);
388         /* Untransformed, vertex fog = NONE, table fog = NONE: Read the fog weighting from the specular color */
389         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
390                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, unstransformed_1,
391                                                      sizeof(unstransformed_1[0]));
392         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
393
394         /* That makes it use the Z value */
395         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);
396         ok(hr == D3D_OK, "Turning off table fog returned %#08x\n", hr);
397         /* Untransformed, vertex fog != none (or table fog != none):
398          * Use the Z value as input into the equation
399          */
400         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
401                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, unstransformed_2,
402                                                      sizeof(unstransformed_1[0]));
403         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
404
405         /* transformed verts */
406         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
407         ok( hr == D3D_OK, "SetVertexShader returned %#08x\n", hr);
408         /* Transformed, vertex fog != NONE, pixel fog == NONE: Use specular color alpha component */
409         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
410                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_1,
411                                                      sizeof(transformed_1[0]));
412         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
413
414         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
415         ok( hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR returned %#08x\n", hr);
416         /* Transformed, table fog != none, vertex anything: Use Z value as input to the fog
417          * equation
418          */
419         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
420                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_2,
421                                                      sizeof(transformed_2[0]));
422
423         hr = IDirect3DDevice8_EndScene(device);
424         ok(hr == D3D_OK, "EndScene returned %#08x\n", hr);
425     }
426     else
427     {
428         ok(FALSE, "BeginScene failed\n");
429     }
430
431     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
432     color = getPixelColor(device, 160, 360);
433     ok(color == 0x00FF0000, "Untransformed vertex with no table or vertex fog has color %08x\n", color);
434     color = getPixelColor(device, 160, 120);
435     ok(color == 0x0000FF00, "Untransformed vertex with linear vertex fog has color %08x\n", color);
436     color = getPixelColor(device, 480, 120);
437     ok(color == 0x00FFFF00, "Transformed vertex with linear vertex fog has color %08x\n", color);
438     color = getPixelColor(device, 480, 360);
439     ok(color == 0x0000FF00, "Transformed vertex with linear table fog has color %08x\n", color);
440
441     /* Turn off the fog master switch to avoid confusing other tests */
442     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
443     ok(hr == D3D_OK, "Turning off fog calculations returned %#08x\n", hr);
444 }
445
446 static void present_test(IDirect3DDevice8 *device)
447 {
448     struct vertex quad[] =
449     {
450         {-1.0f, -1.0f,   0.9f,                          0xffff0000},
451         {-1.0f,  1.0f,   0.9f,                          0xffff0000},
452         { 1.0f, -1.0f,   0.1f,                          0xffff0000},
453         { 1.0f,  1.0f,   0.1f,                          0xffff0000},
454     };
455     HRESULT hr;
456     DWORD color;
457
458     /* Does the Present clear the depth stencil? Clear the depth buffer with some value != 0,
459     * then call Present. Then clear the color buffer to make sure it has some defined content
460     * after the Present with D3DSWAPEFFECT_DISCARD. After that draw a plane that is somewhere cut
461     * by the depth value.
462     */
463     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 0.75, 0);
464     ok(hr == D3D_OK, "IDirect3DDevice8_Clear returned %s\n", DXGetErrorString8(hr));
465     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
466     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.4, 0);
467
468     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
469     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
470     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_GREATER);
471     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
472     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
473     ok(hr == D3D_OK, "IDirect3DDevice8_SetFVF returned %s\n", DXGetErrorString8(hr));
474
475     hr = IDirect3DDevice8_BeginScene(device);
476     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %s\n", DXGetErrorString8(hr));
477     if(hr == D3D_OK)
478     {
479         /* No lights are defined... That means, lit vertices should be entirely black */
480         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2 /*PrimCount */, quad, sizeof(quad[0]));
481         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString8(hr));
482
483         hr = IDirect3DDevice8_EndScene(device);
484         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %s\n", DXGetErrorString8(hr));
485     }
486
487     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_FALSE);
488     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
489
490     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
491     ok(SUCCEEDED(hr), "Present failed (%#08x)\n", hr);
492     color = getPixelColor(device, 512, 240);
493     ok(color == 0x00ffffff, "Present failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
494     color = getPixelColor(device, 64, 240);
495     ok(color == 0x00ff0000, "Present failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
496 }
497
498 static void test_rcp_rsq(IDirect3DDevice8 *device)
499 {
500     HRESULT hr;
501     DWORD shader;
502     DWORD color;
503     unsigned char c1, c2, c3;
504     float constant[4] = {1.0, 1.0, 1.0, 2.0};
505
506     static const float quad[][3] = {
507         {-1.0f, -1.0f, 0.0f},
508         {-1.0f,  1.0f, 0.0f},
509         { 1.0f, -1.0f, 0.0f},
510         { 1.0f,  1.0f, 0.0f},
511     };
512
513     const DWORD rcp_test[] = {
514         0xfffe0101,                                         /* vs.1.1 */
515
516         0x0009fffe, 0x30303030, 0x30303030,                 /* Shaders have to have a minimal size. */
517         0x30303030, 0x30303030, 0x30303030,                 /* Add a filler comment. Usually D3DX8's*/
518         0x30303030, 0x30303030, 0x30303030,                 /* version comment makes the shader big */
519         0x00303030,                                         /* enough to make windows happy         */
520
521         0x00000001, 0xc00f0000, 0x90e40000,                 /* mov oPos, v0 */
522         0x00000006, 0xd00f0000, 0xa0e40000,                 /* rcp oD0, c0 */
523         0x0000ffff                                          /* END */
524     };
525
526     const DWORD rsq_test[] = {
527         0xfffe0101,                                         /* vs.1.1 */
528
529         0x0009fffe, 0x30303030, 0x30303030,                 /* Shaders have to have a minimal size. */
530         0x30303030, 0x30303030, 0x30303030,                 /* Add a filler comment. Usually D3DX8's*/
531         0x30303030, 0x30303030, 0x30303030,                 /* version comment makes the shader big */
532         0x00303030,                                         /* enough to make windows happy         */
533
534         0x00000001, 0xc00f0000, 0x90e40000,                 /* mov oPos, v0 */
535         0x00000007, 0xd00f0000, 0xa0e40000,                 /* rsq oD0, c0 */
536         0x0000ffff                                          /* END */
537     };
538
539     DWORD decl[] =
540     {
541         D3DVSD_STREAM(0),
542         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
543         D3DVSD_END()
544     };
545
546     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff800080, 0.0, 0);
547     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
548
549     hr = IDirect3DDevice8_CreateVertexShader(device, decl, rcp_test, &shader, 0);
550     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned with %#08x\n", hr);
551
552     IDirect3DDevice8_SetVertexShader(device, shader);
553     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
554     IDirect3DDevice8_SetVertexShaderConstant(device, 0, constant, 1);
555
556     hr = IDirect3DDevice8_BeginScene(device);
557     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
558     if(SUCCEEDED(hr))
559     {
560         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
561         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%#08x)\n", hr);
562         hr = IDirect3DDevice8_EndScene(device);
563         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
564     }
565
566     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
567     ok(SUCCEEDED(hr), "Present failed (%#08x)\n", hr);
568     color = getPixelColor(device, 320, 240);
569     c1 = (color & 0x00ff0000 )>> 16;
570     c2 = (color & 0x0000ff00 )>>  8;
571     c3 = (color & 0x000000ff )>>  0;
572     ok(c1 == c2 && c2 == c3, "Color components differ: c1 = %02x, c2 = %02x, c3 = %02x\n",
573        c1, c2, c3);
574     ok(c1 >= 0x7c && c1 <= 0x84, "Color component value is %02x\n", c1);
575
576     IDirect3DDevice8_SetVertexShader(device, 0);
577     IDirect3DDevice8_DeleteVertexShader(device, shader);
578
579     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff800080, 0.0, 0);
580     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
581
582     hr = IDirect3DDevice8_CreateVertexShader(device, decl, rsq_test, &shader, 0);
583     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned with %#08x\n", hr);
584
585     IDirect3DDevice8_SetVertexShader(device, shader);
586     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
587     IDirect3DDevice8_SetVertexShaderConstant(device, 0, constant, 1);
588
589     hr = IDirect3DDevice8_BeginScene(device);
590     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
591     if(SUCCEEDED(hr))
592     {
593         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
594         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%#08x)\n", hr);
595         hr = IDirect3DDevice8_EndScene(device);
596         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
597     }
598
599     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
600     ok(SUCCEEDED(hr), "Present failed (%#08x)\n", hr);
601     color = getPixelColor(device, 320, 240);
602     c1 = (color & 0x00ff0000 )>> 16;
603     c2 = (color & 0x0000ff00 )>>  8;
604     c3 = (color & 0x000000ff )>>  0;
605     ok(c1 == c2 && c2 == c3, "Color components differ: c1 = %02x, c2 = %02x, c3 = %02x\n",
606        c1, c2, c3);
607     ok(c1 >= 0xb0 && c1 <= 0xb8, "Color component value is %02x\n", c1);
608
609     IDirect3DDevice8_SetVertexShader(device, 0);
610     IDirect3DDevice8_DeleteVertexShader(device, shader);
611 }
612
613 static void offscreen_test(IDirect3DDevice8 *device)
614 {
615     HRESULT hr;
616     IDirect3DTexture8 *offscreenTexture = NULL;
617     IDirect3DSurface8 *backbuffer = NULL, *offscreen = NULL, *depthstencil = NULL;
618     DWORD color;
619
620     static const float quad[][5] = {
621         {-0.5f, -0.5f, 0.1f, 0.0f, 0.0f},
622         {-0.5f,  0.5f, 0.1f, 0.0f, 1.0f},
623         { 0.5f, -0.5f, 0.1f, 1.0f, 0.0f},
624         { 0.5f,  0.5f, 0.1f, 1.0f, 1.0f},
625     };
626
627     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &depthstencil);
628     ok(hr == D3D_OK, "IDirect3DDevice8_GetDepthStencilSurface failed, hr = %#08x\n", hr);
629
630     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
631     ok(hr == D3D_OK, "Clear failed, hr = %#08x\n", hr);
632
633     hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &offscreenTexture);
634     ok(hr == D3D_OK || D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
635     if(!offscreenTexture) {
636         trace("Failed to create an X8R8G8B8 offscreen texture, trying R5G6B5\n");
637         hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &offscreenTexture);
638         ok(hr == D3D_OK || D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
639         if(!offscreenTexture) {
640             skip("Cannot create an offscreen render target\n");
641             goto out;
642         }
643     }
644
645     hr = IDirect3DDevice8_GetBackBuffer(device, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
646     ok(hr == D3D_OK, "Can't get back buffer, hr = %#08x\n", hr);
647     if(!backbuffer) {
648         goto out;
649     }
650
651     hr = IDirect3DTexture8_GetSurfaceLevel(offscreenTexture, 0, &offscreen);
652     ok(hr == D3D_OK, "Can't get offscreen surface, hr = %#08x\n", hr);
653     if(!offscreen) {
654         goto out;
655     }
656
657     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
658     ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
659
660     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
661     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
662     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
663     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
664     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_NONE);
665     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (%#08x)\n", hr);
666     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_NONE);
667     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (%#08x)\n", hr);
668     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
669     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
670
671     if(IDirect3DDevice8_BeginScene(device) == D3D_OK) {
672         hr = IDirect3DDevice8_SetRenderTarget(device, offscreen, depthstencil);
673         ok(hr == D3D_OK, "SetRenderTarget failed, hr = %#08x\n", hr);
674         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
675         ok(hr == D3D_OK, "Clear failed, hr = %#08x\n", hr);
676
677         /* Draw without textures - Should result in a white quad */
678         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(quad[0]));
679         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
680
681         hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
682         ok(hr == D3D_OK, "SetRenderTarget failed, hr = %#08x\n", hr);
683         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) offscreenTexture);
684         ok(hr == D3D_OK, "SetTexture failed, %s\n", DXGetErrorString8(hr));
685
686         /* This time with the texture */
687         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(quad[0]));
688         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
689
690         IDirect3DDevice8_EndScene(device);
691     }
692
693     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
694
695     /* Center quad - should be white */
696     color = getPixelColor(device, 320, 240);
697     ok(color == 0x00ffffff, "Offscreen failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
698     /* Some quad in the cleared part of the texture */
699     color = getPixelColor(device, 170, 240);
700     ok(color == 0x00ff00ff, "Offscreen failed: Got color 0x%08x, expected 0x00ff00ff.\n", color);
701     /* Part of the originally cleared back buffer */
702     color = getPixelColor(device, 10, 10);
703     ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
704     if(0) {
705         /* Lower left corner of the screen, where back buffer offscreen rendering draws the offscreen texture.
706         * It should be red, but the offscreen texture may leave some junk there. Not tested yet. Depending on
707         * the offscreen rendering mode this test would succeed or fail
708         */
709         color = getPixelColor(device, 10, 470);
710         ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
711     }
712
713 out:
714     hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
715
716     /* restore things */
717     if(backbuffer) {
718         hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
719         IDirect3DSurface8_Release(backbuffer);
720     }
721     if(offscreenTexture) {
722         IDirect3DTexture8_Release(offscreenTexture);
723     }
724     if(offscreen) {
725         IDirect3DSurface8_Release(offscreen);
726     }
727     if(depthstencil) {
728         IDirect3DSurface8_Release(depthstencil);
729     }
730 }
731
732 static void alpha_test(IDirect3DDevice8 *device)
733 {
734     HRESULT hr;
735     IDirect3DTexture8 *offscreenTexture;
736     IDirect3DSurface8 *backbuffer = NULL, *offscreen = NULL, *depthstencil = NULL;
737     DWORD color, red, green, blue;
738
739     struct vertex quad1[] =
740     {
741         {-1.0f, -1.0f,   0.1f,                          0x4000ff00},
742         {-1.0f,  0.0f,   0.1f,                          0x4000ff00},
743         { 1.0f, -1.0f,   0.1f,                          0x4000ff00},
744         { 1.0f,  0.0f,   0.1f,                          0x4000ff00},
745     };
746     struct vertex quad2[] =
747     {
748         {-1.0f,  0.0f,   0.1f,                          0xc00000ff},
749         {-1.0f,  1.0f,   0.1f,                          0xc00000ff},
750         { 1.0f,  0.0f,   0.1f,                          0xc00000ff},
751         { 1.0f,  1.0f,   0.1f,                          0xc00000ff},
752     };
753     static const float composite_quad[][5] = {
754         { 0.0f, -1.0f, 0.1f, 0.0f, 1.0f},
755         { 0.0f,  1.0f, 0.1f, 0.0f, 0.0f},
756         { 1.0f, -1.0f, 0.1f, 1.0f, 1.0f},
757         { 1.0f,  1.0f, 0.1f, 1.0f, 0.0f},
758     };
759
760     /* Clear the render target with alpha = 0.5 */
761     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
762     ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
763
764     hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &offscreenTexture);
765     ok(hr == D3D_OK || D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
766
767     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &depthstencil);
768     ok(hr == D3D_OK, "IDirect3DDevice8_GetDepthStencilSurface failed, hr = %#08x\n", hr);
769
770     hr = IDirect3DDevice8_GetBackBuffer(device, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
771     ok(hr == D3D_OK, "Can't get back buffer, hr = %#08x\n", hr);
772     if(!backbuffer) {
773         goto out;
774     }
775     hr = IDirect3DTexture8_GetSurfaceLevel(offscreenTexture, 0, &offscreen);
776     ok(hr == D3D_OK, "Can't get offscreen surface, hr = %#08x\n", hr);
777     if(!offscreen) {
778         goto out;
779     }
780
781     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
782     ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
783
784     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
785     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
786     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
787     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
788     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_NONE);
789     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (%#08x)\n", hr);
790     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_NONE);
791     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (%#08x)\n", hr);
792     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
793     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
794
795     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
796     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
797     if(IDirect3DDevice8_BeginScene(device) == D3D_OK) {
798
799         /* Draw two quads, one with src alpha blending, one with dest alpha blending. The
800          * SRCALPHA / INVSRCALPHA blend doesn't give any surprises. Colors are blended based on
801          * the input alpha
802          *
803          * The DESTALPHA / INVDESTALPHA do not "work" on the regular buffer because there is no alpha.
804          * They give essentially ZERO and ONE blend factors
805          */
806         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
807         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
808         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
809         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
810         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(quad1[0]));
811         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
812
813         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_DESTALPHA);
814         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
815         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVDESTALPHA);
816         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
817         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(quad2[0]));
818         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
819
820         /* Switch to the offscreen buffer, and redo the testing. SRCALPHA and DESTALPHA. The offscreen buffer
821          * has a alpha channel on its own. Clear the offscreen buffer with alpha = 0.5 again, then draw the
822          * quads again. The SRCALPHA/INVSRCALPHA doesn't give any surprises, but the DESTALPHA/INVDESTALPHA
823          * blending works as supposed now - blend factor is 0.5 in both cases, not 0.75 as from the input
824          * vertices
825          */
826         hr = IDirect3DDevice8_SetRenderTarget(device, offscreen, 0);
827         ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
828         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
829         ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
830
831         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
832         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
833         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
834         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
835         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(quad1[0]));
836         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
837
838         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_DESTALPHA);
839         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
840         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVDESTALPHA);
841         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
842         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(quad2[0]));
843         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
844
845         hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
846         ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
847
848         /* Render the offscreen texture onto the frame buffer to be able to compare it regularly.
849          * Disable alpha blending for the final composition
850          */
851         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
852         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
853         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
854         ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
855
856         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) offscreenTexture);
857         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
858         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, composite_quad, sizeof(float) * 5);
859         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
860         hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
861         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
862
863         hr = IDirect3DDevice8_EndScene(device);
864         ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed, hr = %08x\n", hr);
865     }
866
867     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
868
869     color = getPixelColor(device, 160, 360);
870     red =   (color & 0x00ff0000) >> 16;
871     green = (color & 0x0000ff00) >>  8;
872     blue =  (color & 0x000000ff);
873     ok(red >= 0xbe && red <= 0xc0 && green >= 0x39 && green <= 0x41 && blue == 0x00,
874        "SRCALPHA on frame buffer returned color %08x, expected 0x00bf4000\n", color);
875
876     color = getPixelColor(device, 160, 120);
877     red =   (color & 0x00ff0000) >> 16;
878     green = (color & 0x0000ff00) >>  8;
879     blue =  (color & 0x000000ff);
880     ok(red == 0x00 && green == 0x00 && blue >= 0xfe && blue <= 0xff ,
881        "DSTALPHA on frame buffer returned color %08x, expected 0x00ff0000\n", color);
882
883     color = getPixelColor(device, 480, 360);
884     red =   (color & 0x00ff0000) >> 16;
885     green = (color & 0x0000ff00) >>  8;
886     blue =  (color & 0x000000ff);
887     ok(red >= 0xbe && red <= 0xc0 && green >= 0x39 && green <= 0x41 && blue == 0x00,
888        "SRCALPHA on texture returned color %08x, expected bar\n", color);
889
890     color = getPixelColor(device, 480, 120);
891     red =   (color & 0x00ff0000) >> 16;
892     green = (color & 0x0000ff00) >>  8;
893     blue =  (color & 0x000000ff);
894     ok(red >= 0x7e && red <= 0x81 && green == 0x00 && blue >= 0x7e && blue <= 0x81,
895        "DSTALPHA on texture returned color %08x, expected foo\n", color);
896
897     out:
898     /* restore things */
899     if(backbuffer) {
900         IDirect3DSurface8_Release(backbuffer);
901     }
902     if(offscreenTexture) {
903         IDirect3DTexture8_Release(offscreenTexture);
904     }
905     if(offscreen) {
906         IDirect3DSurface8_Release(offscreen);
907     }
908     if(depthstencil) {
909         IDirect3DSurface8_Release(depthstencil);
910     }
911 }
912
913 static void p8_texture_test(IDirect3DDevice8 *device)
914 {
915     IDirect3D8 *d3d = NULL;
916     HRESULT hr;
917     IDirect3DTexture8 *texture = NULL, *texture2 = NULL;
918     D3DLOCKED_RECT lr;
919     unsigned char *data;
920     DWORD color, red, green, blue;
921     PALETTEENTRY table[256];
922     D3DCAPS8 caps;
923     UINT i;
924     float quad[] = {
925        -1.0,       0,       0.1,     0.0,    0.0,
926        -1.0,       1.0,     0.1,     0.0,    1.0,
927         1.0,       0,       0.1,     1.0,    0.0,
928         1.0,       1.0,     0.1,     1.0,    1.0,
929     };
930     float quad2[] = {
931        -1.0,       -1.0,    0.1,     0.0,    0.0,
932        -1.0,       0,       0.1,     0.0,    1.0,
933         1.0,       -1.0,    0.1,     1.0,    0.0,
934         1.0,       0,       0.1,     1.0,    1.0,
935     };
936
937     IDirect3DDevice8_GetDirect3D(device, &d3d);
938
939     if(IDirect3D8_CheckDeviceFormat(d3d, 0, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, 0,
940        D3DRTYPE_TEXTURE, D3DFMT_P8) != D3D_OK) {
941            skip("D3DFMT_P8 textures not supported\n");
942            goto out;
943     }
944
945     hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_P8,
946                                         D3DPOOL_MANAGED, &texture2);
947     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed, hr = %08x\n", hr);
948     if(!texture2) {
949         skip("Failed to create D3DFMT_P8 texture\n");
950         goto out;
951     }
952
953     memset(&lr, 0, sizeof(lr));
954     hr = IDirect3DTexture8_LockRect(texture2, 0, &lr, NULL, 0);
955     ok(hr == D3D_OK, "IDirect3DTexture8_LockRect failed, hr = %08x\n", hr);
956     data = lr.pBits;
957     *data = 1;
958
959     hr = IDirect3DTexture8_UnlockRect(texture2, 0);
960     ok(hr == D3D_OK, "IDirect3DTexture8_UnlockRect failed, hr = %08x\n", hr);
961
962     hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_P8,
963                                         D3DPOOL_MANAGED, &texture);
964     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed, hr = %08x\n", hr);
965     if(!texture) {
966         skip("Failed to create D3DFMT_P8 texture\n");
967         goto out;
968     }
969
970     memset(&lr, 0, sizeof(lr));
971     hr = IDirect3DTexture8_LockRect(texture, 0, &lr, NULL, 0);
972     ok(hr == D3D_OK, "IDirect3DTexture8_LockRect failed, hr = %08x\n", hr);
973     data = lr.pBits;
974     *data = 1;
975
976     hr = IDirect3DTexture8_UnlockRect(texture, 0);
977     ok(hr == D3D_OK, "IDirect3DTexture8_UnlockRect failed, hr = %08x\n", hr);
978
979     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
980     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
981
982     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
983     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
984
985     /* The first part of the test should work both with and without D3DPTEXTURECAPS_ALPHAPALETTE;
986        alpha of every entry is set to 1.0, which MS says is required when there's no
987        D3DPTEXTURECAPS_ALPHAPALETTE capability */
988     for (i = 0; i < 256; i++) {
989         table[i].peRed = table[i].peGreen = table[i].peBlue = 0;
990         table[i].peFlags = 0xff;
991     }
992     table[1].peRed = 0xff;
993     hr = IDirect3DDevice8_SetPaletteEntries(device, 0, table);
994     ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
995
996     table[1].peRed = 0;
997     table[1].peBlue = 0xff;
998     hr = IDirect3DDevice8_SetPaletteEntries(device, 1, table);
999     ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1000
1001     hr = IDirect3DDevice8_BeginScene(device);
1002     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1003     if(SUCCEEDED(hr)) {
1004         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
1005         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1006         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
1007         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1008
1009         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
1010         ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
1011
1012         hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 0);
1013         ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1014
1015         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture2);
1016         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1017         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1018         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1019
1020         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture);
1021         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1022         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1023         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1024
1025         hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 1);
1026         ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1027         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, 5 * sizeof(float));
1028         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1029
1030         hr = IDirect3DDevice8_EndScene(device);
1031         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1032     }
1033
1034     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1035     ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1036
1037     color = getPixelColor(device, 32, 32);
1038     red   = (color & 0x00ff0000) >> 16;
1039     green = (color & 0x0000ff00) >>  8;
1040     blue  = (color & 0x000000ff) >>  0;
1041     ok(red == 0xff && blue == 0 && green == 0,
1042        "got color %08x, expected 0x00ff0000\n", color);
1043
1044     color = getPixelColor(device, 32, 320);
1045     red   = (color & 0x00ff0000) >> 16;
1046     green = (color & 0x0000ff00) >>  8;
1047     blue  = (color & 0x000000ff) >>  0;
1048     ok(red == 0 && blue == 0xff && green == 0,
1049     "got color %08x, expected 0x000000ff\n", color);
1050
1051     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
1052     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
1053
1054     hr = IDirect3DDevice8_BeginScene(device);
1055     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1056     if(SUCCEEDED(hr)) {
1057         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture2);
1058         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1059
1060         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1061         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1062
1063         hr = IDirect3DDevice8_EndScene(device);
1064         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1065     }
1066
1067     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1068     ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1069
1070     color = getPixelColor(device, 32, 32);
1071     red   = (color & 0x00ff0000) >> 16;
1072     green = (color & 0x0000ff00) >>  8;
1073     blue  = (color & 0x000000ff) >>  0;
1074     ok(red == 0 && blue == 0xff && green == 0,
1075     "got color %08x, expected 0x000000ff\n", color);
1076
1077     /* Test palettes with alpha */
1078     IDirect3DDevice8_GetDeviceCaps(device, &caps);
1079     if (!(caps.TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)) {
1080         skip("no D3DPTEXTURECAPS_ALPHAPALETTE capability, tests with alpha in palette will be skipped\n");
1081     } else {
1082         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
1083         ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
1084
1085         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
1086         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1087
1088         for (i = 0; i < 256; i++) {
1089             table[i].peRed = table[i].peGreen = table[i].peBlue = 0;
1090             table[i].peFlags = 0xff;
1091         }
1092         table[1].peRed = 0xff;
1093         table[1].peFlags = 0x80;
1094         hr = IDirect3DDevice8_SetPaletteEntries(device, 0, table);
1095         ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1096
1097         table[1].peRed = 0;
1098         table[1].peBlue = 0xff;
1099         table[1].peFlags = 0x80;
1100         hr = IDirect3DDevice8_SetPaletteEntries(device, 1, table);
1101         ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1102
1103         hr = IDirect3DDevice8_BeginScene(device);
1104         ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1105         if(SUCCEEDED(hr)) {
1106             hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
1107             ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1108             hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
1109             ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1110
1111             hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
1112             ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
1113
1114             hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 0);
1115             ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1116
1117             hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1118             ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1119
1120             hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 1);
1121             ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1122
1123             hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, 5 * sizeof(float));
1124             ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1125
1126             hr = IDirect3DDevice8_EndScene(device);
1127             ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1128         }
1129
1130         hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1131         ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1132
1133         color = getPixelColor(device, 32, 32);
1134         red   = (color & 0x00ff0000) >> 16;
1135         green = (color & 0x0000ff00) >>  8;
1136         blue  = (color & 0x000000ff) >>  0;
1137         ok(red >= 0x7e && red <= 0x81 && blue == 0 && green == 0,
1138         "got color %08x, expected 0x00800000 or near\n", color);
1139
1140         color = getPixelColor(device, 32, 320);
1141         red   = (color & 0x00ff0000) >> 16;
1142         green = (color & 0x0000ff00) >>  8;
1143         blue  = (color & 0x000000ff) >>  0;
1144         ok(red == 0 && blue >= 0x7e && blue <= 0x81 && green == 0,
1145         "got color %08x, expected 0x00000080 or near\n", color);
1146     }
1147
1148     hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
1149     ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1150     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
1151     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1152
1153 out:
1154     if(texture) IDirect3DTexture8_Release(texture);
1155     if(texture2) IDirect3DTexture8_Release(texture2);
1156     IDirect3D8_Release(d3d);
1157 }
1158
1159 START_TEST(visual)
1160 {
1161     IDirect3DDevice8 *device_ptr;
1162     HRESULT hr;
1163     DWORD color;
1164     D3DCAPS8 caps;
1165
1166     d3d8_handle = LoadLibraryA("d3d8.dll");
1167     if (!d3d8_handle)
1168     {
1169         skip("Could not load d3d8.dll\n");
1170         return;
1171     }
1172
1173     device_ptr = init_d3d8();
1174     if (!device_ptr)
1175     {
1176         skip("Could not initialize direct3d\n");
1177         return;
1178     }
1179
1180     IDirect3DDevice8_GetDeviceCaps(device_ptr, &caps);
1181
1182     /* Check for the reliability of the returned data */
1183     hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
1184     if(FAILED(hr))
1185     {
1186         trace("Clear failed, can't assure correctness of the test results, skipping\n");
1187         goto cleanup;
1188     }
1189     IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
1190
1191     color = getPixelColor(device_ptr, 1, 1);
1192     if(color !=0x00ff0000)
1193     {
1194         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
1195         goto cleanup;
1196     }
1197
1198     hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
1199     if(FAILED(hr))
1200     {
1201         trace("Clear failed, can't assure correctness of the test results, skipping\n");
1202         goto cleanup;
1203     }
1204     IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
1205
1206     color = getPixelColor(device_ptr, 639, 479);
1207     if(color != 0x0000ddee)
1208     {
1209         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
1210         goto cleanup;
1211     }
1212
1213     /* Now run the real test */
1214     lighting_test(device_ptr);
1215     clear_test(device_ptr);
1216     fog_test(device_ptr);
1217     present_test(device_ptr);
1218     offscreen_test(device_ptr);
1219     alpha_test(device_ptr);
1220
1221     if (caps.VertexShaderVersion >= D3DVS_VERSION(1, 1))
1222     {
1223         test_rcp_rsq(device_ptr);
1224     }
1225     else
1226     {
1227         skip("No vs.1.1 support\n");
1228     }
1229
1230     p8_texture_test(device_ptr);
1231
1232 cleanup:
1233     if(device_ptr) {
1234         D3DDEVICE_CREATION_PARAMETERS creation_parameters;
1235         IDirect3DDevice8_GetCreationParameters(device_ptr, &creation_parameters);
1236         IDirect3DDevice8_Release(device_ptr);
1237         DestroyWindow(creation_parameters.hFocusWindow);
1238     }
1239 }