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