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