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