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