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