gdiplus: Implement GdipGetEmHeight.
[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;
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     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0xbf, 0x40, 0x00), 1),
874        "SRCALPHA on frame buffer returned color %08x, expected 0x00bf4000\n", color);
875
876     color = getPixelColor(device, 160, 120);
877     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0x7f, 0x00, 0x80), 2),
878        "DSTALPHA on frame buffer returned color %08x, expected 0x007f0080\n", color);
879
880     color = getPixelColor(device, 480, 360);
881     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0xbf, 0x40, 0x00), 1),
882        "SRCALPHA on texture returned color %08x, expected 0x00bf4000\n", color);
883
884     color = getPixelColor(device, 480, 120);
885     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0x00, 0x00, 0xff), 1),
886        "DSTALPHA on texture returned color %08x, expected 0x000000ff\n", color);
887
888     out:
889     /* restore things */
890     if(backbuffer) {
891         IDirect3DSurface8_Release(backbuffer);
892     }
893     if(offscreenTexture) {
894         IDirect3DTexture8_Release(offscreenTexture);
895     }
896     if(offscreen) {
897         IDirect3DSurface8_Release(offscreen);
898     }
899     if(depthstencil) {
900         IDirect3DSurface8_Release(depthstencil);
901     }
902 }
903
904 static void p8_texture_test(IDirect3DDevice8 *device)
905 {
906     IDirect3D8 *d3d = NULL;
907     HRESULT hr;
908     IDirect3DTexture8 *texture = NULL, *texture2 = NULL;
909     D3DLOCKED_RECT lr;
910     unsigned char *data;
911     DWORD color, red, green, blue;
912     PALETTEENTRY table[256];
913     D3DCAPS8 caps;
914     UINT i;
915     float quad[] = {
916        -1.0,       0,       0.1,     0.0,    0.0,
917        -1.0,       1.0,     0.1,     0.0,    1.0,
918         1.0,       0,       0.1,     1.0,    0.0,
919         1.0,       1.0,     0.1,     1.0,    1.0,
920     };
921     float quad2[] = {
922        -1.0,       -1.0,    0.1,     0.0,    0.0,
923        -1.0,       0,       0.1,     0.0,    1.0,
924         1.0,       -1.0,    0.1,     1.0,    0.0,
925         1.0,       0,       0.1,     1.0,    1.0,
926     };
927
928     IDirect3DDevice8_GetDirect3D(device, &d3d);
929
930     if(IDirect3D8_CheckDeviceFormat(d3d, 0, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, 0,
931        D3DRTYPE_TEXTURE, D3DFMT_P8) != D3D_OK) {
932            skip("D3DFMT_P8 textures not supported\n");
933            goto out;
934     }
935
936     hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_P8,
937                                         D3DPOOL_MANAGED, &texture2);
938     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed, hr = %08x\n", hr);
939     if(!texture2) {
940         skip("Failed to create D3DFMT_P8 texture\n");
941         goto out;
942     }
943
944     memset(&lr, 0, sizeof(lr));
945     hr = IDirect3DTexture8_LockRect(texture2, 0, &lr, NULL, 0);
946     ok(hr == D3D_OK, "IDirect3DTexture8_LockRect failed, hr = %08x\n", hr);
947     data = lr.pBits;
948     *data = 1;
949
950     hr = IDirect3DTexture8_UnlockRect(texture2, 0);
951     ok(hr == D3D_OK, "IDirect3DTexture8_UnlockRect failed, hr = %08x\n", hr);
952
953     hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_P8,
954                                         D3DPOOL_MANAGED, &texture);
955     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed, hr = %08x\n", hr);
956     if(!texture) {
957         skip("Failed to create D3DFMT_P8 texture\n");
958         goto out;
959     }
960
961     memset(&lr, 0, sizeof(lr));
962     hr = IDirect3DTexture8_LockRect(texture, 0, &lr, NULL, 0);
963     ok(hr == D3D_OK, "IDirect3DTexture8_LockRect failed, hr = %08x\n", hr);
964     data = lr.pBits;
965     *data = 1;
966
967     hr = IDirect3DTexture8_UnlockRect(texture, 0);
968     ok(hr == D3D_OK, "IDirect3DTexture8_UnlockRect failed, hr = %08x\n", hr);
969
970     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
971     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
972
973     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
974     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
975
976     /* The first part of the test should work both with and without D3DPTEXTURECAPS_ALPHAPALETTE;
977        alpha of every entry is set to 1.0, which MS says is required when there's no
978        D3DPTEXTURECAPS_ALPHAPALETTE capability */
979     for (i = 0; i < 256; i++) {
980         table[i].peRed = table[i].peGreen = table[i].peBlue = 0;
981         table[i].peFlags = 0xff;
982     }
983     table[1].peRed = 0xff;
984     hr = IDirect3DDevice8_SetPaletteEntries(device, 0, table);
985     ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
986
987     table[1].peRed = 0;
988     table[1].peBlue = 0xff;
989     hr = IDirect3DDevice8_SetPaletteEntries(device, 1, table);
990     ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
991
992     hr = IDirect3DDevice8_BeginScene(device);
993     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
994     if(SUCCEEDED(hr)) {
995         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
996         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
997         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
998         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
999
1000         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
1001         ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
1002
1003         hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 0);
1004         ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1005
1006         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture2);
1007         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1008         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1009         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1010
1011         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture);
1012         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1013         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1014         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1015
1016         hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 1);
1017         ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1018         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, 5 * sizeof(float));
1019         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1020
1021         hr = IDirect3DDevice8_EndScene(device);
1022         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1023     }
1024
1025     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1026     ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1027
1028     color = getPixelColor(device, 32, 32);
1029     red   = (color & 0x00ff0000) >> 16;
1030     green = (color & 0x0000ff00) >>  8;
1031     blue  = (color & 0x000000ff) >>  0;
1032     ok(red == 0xff && blue == 0 && green == 0,
1033        "got color %08x, expected 0x00ff0000\n", color);
1034
1035     color = getPixelColor(device, 32, 320);
1036     red   = (color & 0x00ff0000) >> 16;
1037     green = (color & 0x0000ff00) >>  8;
1038     blue  = (color & 0x000000ff) >>  0;
1039     ok(red == 0 && blue == 0xff && green == 0,
1040     "got color %08x, expected 0x000000ff\n", color);
1041
1042     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
1043     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
1044
1045     hr = IDirect3DDevice8_BeginScene(device);
1046     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1047     if(SUCCEEDED(hr)) {
1048         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture2);
1049         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1050
1051         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1052         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1053
1054         hr = IDirect3DDevice8_EndScene(device);
1055         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1056     }
1057
1058     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1059     ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1060
1061     color = getPixelColor(device, 32, 32);
1062     red   = (color & 0x00ff0000) >> 16;
1063     green = (color & 0x0000ff00) >>  8;
1064     blue  = (color & 0x000000ff) >>  0;
1065     ok(red == 0 && blue == 0xff && green == 0,
1066     "got color %08x, expected 0x000000ff\n", color);
1067
1068     /* Test palettes with alpha */
1069     IDirect3DDevice8_GetDeviceCaps(device, &caps);
1070     if (!(caps.TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)) {
1071         skip("no D3DPTEXTURECAPS_ALPHAPALETTE capability, tests with alpha in palette will be skipped\n");
1072     } else {
1073         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
1074         ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
1075
1076         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
1077         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1078
1079         for (i = 0; i < 256; i++) {
1080             table[i].peRed = table[i].peGreen = table[i].peBlue = 0;
1081             table[i].peFlags = 0xff;
1082         }
1083         table[1].peRed = 0xff;
1084         table[1].peFlags = 0x80;
1085         hr = IDirect3DDevice8_SetPaletteEntries(device, 0, table);
1086         ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1087
1088         table[1].peRed = 0;
1089         table[1].peBlue = 0xff;
1090         table[1].peFlags = 0x80;
1091         hr = IDirect3DDevice8_SetPaletteEntries(device, 1, table);
1092         ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1093
1094         hr = IDirect3DDevice8_BeginScene(device);
1095         ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1096         if(SUCCEEDED(hr)) {
1097             hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
1098             ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1099             hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
1100             ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1101
1102             hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
1103             ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
1104
1105             hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 0);
1106             ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1107
1108             hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1109             ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1110
1111             hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 1);
1112             ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1113
1114             hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, 5 * sizeof(float));
1115             ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1116
1117             hr = IDirect3DDevice8_EndScene(device);
1118             ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1119         }
1120
1121         hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1122         ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1123
1124         color = getPixelColor(device, 32, 32);
1125         red   = (color & 0x00ff0000) >> 16;
1126         green = (color & 0x0000ff00) >>  8;
1127         blue  = (color & 0x000000ff) >>  0;
1128         ok(red >= 0x7e && red <= 0x81 && blue == 0 && green == 0,
1129         "got color %08x, expected 0x00800000 or near\n", color);
1130
1131         color = getPixelColor(device, 32, 320);
1132         red   = (color & 0x00ff0000) >> 16;
1133         green = (color & 0x0000ff00) >>  8;
1134         blue  = (color & 0x000000ff) >>  0;
1135         ok(red == 0 && blue >= 0x7e && blue <= 0x81 && green == 0,
1136         "got color %08x, expected 0x00000080 or near\n", color);
1137     }
1138
1139     hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
1140     ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1141     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
1142     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1143
1144 out:
1145     if(texture) IDirect3DTexture8_Release(texture);
1146     if(texture2) IDirect3DTexture8_Release(texture2);
1147     IDirect3D8_Release(d3d);
1148 }
1149
1150 static void texop_test(IDirect3DDevice8 *device)
1151 {
1152     IDirect3DTexture8 *texture = NULL;
1153     D3DLOCKED_RECT locked_rect;
1154     D3DCOLOR color;
1155     D3DCAPS8 caps;
1156     HRESULT hr;
1157     int i;
1158
1159     static const struct {
1160         float x, y, z;
1161         D3DCOLOR diffuse;
1162         float s, t;
1163     } quad[] = {
1164         {-1.0f, -1.0f, 0.1f, D3DCOLOR_ARGB(0x55, 0xff, 0x00, 0x00), -1.0f, -1.0f},
1165         {-1.0f,  1.0f, 0.1f, D3DCOLOR_ARGB(0x55, 0xff, 0x00, 0x00), -1.0f,  1.0f},
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     };
1169
1170     static const struct {
1171         D3DTEXTUREOP op;
1172         const char *name;
1173         DWORD caps_flag;
1174         D3DCOLOR result;
1175     } test_data[] = {
1176         {D3DTOP_SELECTARG1,                "SELECTARG1",                D3DTEXOPCAPS_SELECTARG1,                D3DCOLOR_ARGB(0x00, 0x00, 0xff, 0x00)},
1177         {D3DTOP_SELECTARG2,                "SELECTARG2",                D3DTEXOPCAPS_SELECTARG2,                D3DCOLOR_ARGB(0x00, 0x33, 0x33, 0x33)},
1178         {D3DTOP_MODULATE,                  "MODULATE",                  D3DTEXOPCAPS_MODULATE,                  D3DCOLOR_ARGB(0x00, 0x00, 0x33, 0x00)},
1179         {D3DTOP_MODULATE2X,                "MODULATE2X",                D3DTEXOPCAPS_MODULATE2X,                D3DCOLOR_ARGB(0x00, 0x00, 0x66, 0x00)},
1180         {D3DTOP_MODULATE4X,                "MODULATE4X",                D3DTEXOPCAPS_MODULATE4X,                D3DCOLOR_ARGB(0x00, 0x00, 0xcc, 0x00)},
1181         {D3DTOP_ADD,                       "ADD",                       D3DTEXOPCAPS_ADD,                       D3DCOLOR_ARGB(0x00, 0x33, 0xff, 0x33)},
1182         {D3DTOP_ADDSIGNED,                 "ADDSIGNED",                 D3DTEXOPCAPS_ADDSIGNED,                 D3DCOLOR_ARGB(0x00, 0x00, 0xb2, 0x00)},
1183         {D3DTOP_ADDSIGNED2X,               "ADDSIGNED2X",               D3DTEXOPCAPS_ADDSIGNED2X,               D3DCOLOR_ARGB(0x00, 0x00, 0xff, 0x00)},
1184         {D3DTOP_SUBTRACT,                  "SUBTRACT",                  D3DTEXOPCAPS_SUBTRACT,                  D3DCOLOR_ARGB(0x00, 0x00, 0xcc, 0x00)},
1185         {D3DTOP_ADDSMOOTH,                 "ADDSMOOTH",                 D3DTEXOPCAPS_ADDSMOOTH,                 D3DCOLOR_ARGB(0x00, 0x33, 0xff, 0x33)},
1186         {D3DTOP_BLENDDIFFUSEALPHA,         "BLENDDIFFUSEALPHA",         D3DTEXOPCAPS_BLENDDIFFUSEALPHA,         D3DCOLOR_ARGB(0x00, 0x22, 0x77, 0x22)},
1187         {D3DTOP_BLENDTEXTUREALPHA,         "BLENDTEXTUREALPHA",         D3DTEXOPCAPS_BLENDTEXTUREALPHA,         D3DCOLOR_ARGB(0x00, 0x14, 0xad, 0x14)},
1188         {D3DTOP_BLENDFACTORALPHA,          "BLENDFACTORALPHA",          D3DTEXOPCAPS_BLENDFACTORALPHA,          D3DCOLOR_ARGB(0x00, 0x07, 0xe4, 0x07)},
1189         {D3DTOP_BLENDTEXTUREALPHAPM,       "BLENDTEXTUREALPHAPM",       D3DTEXOPCAPS_BLENDTEXTUREALPHAPM,       D3DCOLOR_ARGB(0x00, 0x14, 0xff, 0x14)},
1190         {D3DTOP_BLENDCURRENTALPHA,         "BLENDCURRENTALPHA",         D3DTEXOPCAPS_BLENDCURRENTALPHA,         D3DCOLOR_ARGB(0x00, 0x22, 0x77, 0x22)},
1191         {D3DTOP_MODULATEALPHA_ADDCOLOR,    "MODULATEALPHA_ADDCOLOR",    D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR,    D3DCOLOR_ARGB(0x00, 0x1f, 0xff, 0x1f)},
1192         {D3DTOP_MODULATECOLOR_ADDALPHA,    "MODULATECOLOR_ADDALPHA",    D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA,    D3DCOLOR_ARGB(0x00, 0x99, 0xcc, 0x99)},
1193         {D3DTOP_MODULATEINVALPHA_ADDCOLOR, "MODULATEINVALPHA_ADDCOLOR", D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR, D3DCOLOR_ARGB(0x00, 0x14, 0xff, 0x14)},
1194         {D3DTOP_MODULATEINVCOLOR_ADDALPHA, "MODULATEINVCOLOR_ADDALPHA", D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA, D3DCOLOR_ARGB(0x00, 0xcc, 0x99, 0xcc)},
1195         /* BUMPENVMAP & BUMPENVMAPLUMINANCE have their own tests */
1196         {D3DTOP_DOTPRODUCT3,               "DOTPRODUCT2",               D3DTEXOPCAPS_DOTPRODUCT3,               D3DCOLOR_ARGB(0x00, 0x99, 0x99, 0x99)},
1197         {D3DTOP_MULTIPLYADD,               "MULTIPLYADD",               D3DTEXOPCAPS_MULTIPLYADD,               D3DCOLOR_ARGB(0x00, 0xff, 0x33, 0x00)},
1198         {D3DTOP_LERP,                      "LERP",                      D3DTEXOPCAPS_LERP,                      D3DCOLOR_ARGB(0x00, 0x00, 0x33, 0x33)},
1199     };
1200
1201     memset(&caps, 0, sizeof(caps));
1202     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1203     ok(SUCCEEDED(hr), "GetDeviceCaps failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1204
1205     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX0);
1206     ok(SUCCEEDED(hr), "SetVertexShader failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1207
1208     hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture);
1209     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateTexture failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1210     hr = IDirect3DTexture8_LockRect(texture, 0, &locked_rect, NULL, 0);
1211     ok(SUCCEEDED(hr), "LockRect failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1212     *((DWORD *)locked_rect.pBits) = D3DCOLOR_ARGB(0x99, 0x00, 0xff, 0x00);
1213     hr = IDirect3DTexture8_UnlockRect(texture, 0);
1214     ok(SUCCEEDED(hr), "LockRect failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1215     hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *)texture);
1216     ok(SUCCEEDED(hr), "SetTexture failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1217
1218     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG0, D3DTA_DIFFUSE);
1219     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1220     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
1221     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1222     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
1223     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1224
1225     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
1226     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1227
1228     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1229     ok(SUCCEEDED(hr), "SetRenderState failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1230     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_TEXTUREFACTOR, 0xdd333333);
1231     ok(SUCCEEDED(hr), "SetRenderState failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1232     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA);
1233     ok(SUCCEEDED(hr), "SetRenderState failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1234
1235     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
1236     ok(SUCCEEDED(hr), "IDirect3DDevice9_Clear failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1237
1238     for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
1239     {
1240         if (!(caps.TextureOpCaps & test_data[i].caps_flag))
1241         {
1242             skip("tex operation %s not supported\n", test_data[i].name);
1243             continue;
1244         }
1245
1246         hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLOROP, test_data[i].op);
1247         ok(SUCCEEDED(hr), "SetTextureStageState (%s) failed with 0x%08x (%s)\n",
1248                 test_data[i].name, hr, DXGetErrorString8(hr));
1249
1250         hr = IDirect3DDevice8_BeginScene(device);
1251         ok(SUCCEEDED(hr), "BeginScene failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1252
1253         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
1254         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1255
1256         hr = IDirect3DDevice8_EndScene(device);
1257         ok(SUCCEEDED(hr), "EndScene failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1258
1259         hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1260         ok(SUCCEEDED(hr), "Present failed with 0x%08x (%s)\n", hr, DXGetErrorString8(hr));
1261
1262         color = getPixelColor(device, 320, 240);
1263         ok(color_match(color, test_data[i].result, 3), "Operation %s returned color 0x%08x, expected 0x%08x\n",
1264                 test_data[i].name, color, test_data[i].result);
1265     }
1266
1267     if (texture) IDirect3DTexture8_Release(texture);
1268 }
1269
1270 START_TEST(visual)
1271 {
1272     IDirect3DDevice8 *device_ptr;
1273     HRESULT hr;
1274     DWORD color;
1275     D3DCAPS8 caps;
1276
1277     d3d8_handle = LoadLibraryA("d3d8.dll");
1278     if (!d3d8_handle)
1279     {
1280         skip("Could not load d3d8.dll\n");
1281         return;
1282     }
1283
1284     device_ptr = init_d3d8();
1285     if (!device_ptr)
1286     {
1287         skip("Could not initialize direct3d\n");
1288         return;
1289     }
1290
1291     IDirect3DDevice8_GetDeviceCaps(device_ptr, &caps);
1292
1293     /* Check for the reliability of the returned data */
1294     hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
1295     if(FAILED(hr))
1296     {
1297         trace("Clear failed, can't assure correctness of the test results, skipping\n");
1298         goto cleanup;
1299     }
1300     IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
1301
1302     color = getPixelColor(device_ptr, 1, 1);
1303     if(color !=0x00ff0000)
1304     {
1305         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
1306         goto cleanup;
1307     }
1308
1309     hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
1310     if(FAILED(hr))
1311     {
1312         trace("Clear failed, can't assure correctness of the test results, skipping\n");
1313         goto cleanup;
1314     }
1315     IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
1316
1317     color = getPixelColor(device_ptr, 639, 479);
1318     if(color != 0x0000ddee)
1319     {
1320         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
1321         goto cleanup;
1322     }
1323
1324     /* Now run the real test */
1325     lighting_test(device_ptr);
1326     clear_test(device_ptr);
1327     fog_test(device_ptr);
1328     present_test(device_ptr);
1329     offscreen_test(device_ptr);
1330     alpha_test(device_ptr);
1331
1332     if (caps.VertexShaderVersion >= D3DVS_VERSION(1, 1))
1333     {
1334         test_rcp_rsq(device_ptr);
1335     }
1336     else
1337     {
1338         skip("No vs.1.1 support\n");
1339     }
1340
1341     p8_texture_test(device_ptr);
1342     texop_test(device_ptr);
1343
1344 cleanup:
1345     if(device_ptr) {
1346         D3DDEVICE_CREATION_PARAMETERS creation_parameters;
1347         IDirect3DDevice8_GetCreationParameters(device_ptr, &creation_parameters);
1348         IDirect3DDevice8_Release(device_ptr);
1349         DestroyWindow(creation_parameters.hFocusWindow);
1350     }
1351 }