mshtml: Added IHTMLWindow2::focus implementation.
[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 0x%08x, expected 0x00ff0000.\n", color);
280     color = getPixelColor(device, 160, 120); /* Upper left quad - lit without normals */
281     ok(color == 0x00000000, "Lit quad without normals has color 0x%08x, expected 0x00000000.\n", color);
282     color = getPixelColor(device, 480, 360); /* Lower right quad - unlit with normals */
283     ok(color == 0x000000ff, "Unlit quad with normals has color 0x%08x, expected 0x000000ff.\n", color);
284     color = getPixelColor(device, 480, 120); /* Upper right quad - lit with normals */
285     ok(color == 0x00000000, "Lit quad with normals has color 0x%08x, expected 0x00000000.\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     float constant[4] = {1.0, 1.0, 1.0, 2.0};
538
539     static const float quad[][3] = {
540         {-1.0f, -1.0f, 0.0f},
541         {-1.0f,  1.0f, 0.0f},
542         { 1.0f, -1.0f, 0.0f},
543         { 1.0f,  1.0f, 0.0f},
544     };
545
546     const DWORD rcp_test[] = {
547         0xfffe0101,                                         /* vs.1.1 */
548
549         0x0009fffe, 0x30303030, 0x30303030,                 /* Shaders have to have a minimal size. */
550         0x30303030, 0x30303030, 0x30303030,                 /* Add a filler comment. Usually D3DX8's*/
551         0x30303030, 0x30303030, 0x30303030,                 /* version comment makes the shader big */
552         0x00303030,                                         /* enough to make windows happy         */
553
554         0x00000001, 0xc00f0000, 0x90e40000,                 /* mov oPos, v0 */
555         0x00000006, 0xd00f0000, 0xa0e40000,                 /* rcp oD0, c0 */
556         0x0000ffff                                          /* END */
557     };
558
559     const DWORD rsq_test[] = {
560         0xfffe0101,                                         /* vs.1.1 */
561
562         0x0009fffe, 0x30303030, 0x30303030,                 /* Shaders have to have a minimal size. */
563         0x30303030, 0x30303030, 0x30303030,                 /* Add a filler comment. Usually D3DX8's*/
564         0x30303030, 0x30303030, 0x30303030,                 /* version comment makes the shader big */
565         0x00303030,                                         /* enough to make windows happy         */
566
567         0x00000001, 0xc00f0000, 0x90e40000,                 /* mov oPos, v0 */
568         0x00000007, 0xd00f0000, 0xa0e40000,                 /* rsq oD0, c0 */
569         0x0000ffff                                          /* END */
570     };
571
572     DWORD decl[] =
573     {
574         D3DVSD_STREAM(0),
575         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
576         D3DVSD_END()
577     };
578
579     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff336699, 0.0f, 0);
580     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
581
582     hr = IDirect3DDevice8_CreateVertexShader(device, decl, rcp_test, &shader, 0);
583     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned with %#08x\n", hr);
584
585     IDirect3DDevice8_SetVertexShader(device, shader);
586     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
587     IDirect3DDevice8_SetVertexShaderConstant(device, 0, constant, 1);
588
589     hr = IDirect3DDevice8_BeginScene(device);
590     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
591     if(SUCCEEDED(hr))
592     {
593         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
594         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%#08x)\n", hr);
595         hr = IDirect3DDevice8_EndScene(device);
596         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
597     }
598
599     color = getPixelColor(device, 320, 240);
600     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0x80, 0x80, 0x80), 4),
601             "RCP test returned color 0x%08x, expected 0x00808080.\n", color);
602
603     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
604     ok(SUCCEEDED(hr), "Present failed (%#08x)\n", hr);
605
606     IDirect3DDevice8_SetVertexShader(device, 0);
607     IDirect3DDevice8_DeleteVertexShader(device, shader);
608
609     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff996633, 0.0f, 0);
610     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
611
612     hr = IDirect3DDevice8_CreateVertexShader(device, decl, rsq_test, &shader, 0);
613     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned with %#08x\n", hr);
614
615     IDirect3DDevice8_SetVertexShader(device, shader);
616     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
617     IDirect3DDevice8_SetVertexShaderConstant(device, 0, constant, 1);
618
619     hr = IDirect3DDevice8_BeginScene(device);
620     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
621     if(SUCCEEDED(hr))
622     {
623         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
624         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%#08x)\n", hr);
625         hr = IDirect3DDevice8_EndScene(device);
626         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
627     }
628
629     color = getPixelColor(device, 320, 240);
630     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0xb4, 0xb4, 0xb4), 4),
631             "RSQ test returned color 0x%08x, expected 0x00b4b4b4.\n", color);
632
633     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
634     ok(SUCCEEDED(hr), "Present failed (%#08x)\n", hr);
635
636     IDirect3DDevice8_SetVertexShader(device, 0);
637     IDirect3DDevice8_DeleteVertexShader(device, shader);
638 }
639
640 static void offscreen_test(IDirect3DDevice8 *device)
641 {
642     HRESULT hr;
643     IDirect3DTexture8 *offscreenTexture = NULL;
644     IDirect3DSurface8 *backbuffer = NULL, *offscreen = NULL, *depthstencil = NULL;
645     DWORD color;
646
647     static const float quad[][5] = {
648         {-0.5f, -0.5f, 0.1f, 0.0f, 0.0f},
649         {-0.5f,  0.5f, 0.1f, 0.0f, 1.0f},
650         { 0.5f, -0.5f, 0.1f, 1.0f, 0.0f},
651         { 0.5f,  0.5f, 0.1f, 1.0f, 1.0f},
652     };
653
654     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &depthstencil);
655     ok(hr == D3D_OK, "IDirect3DDevice8_GetDepthStencilSurface failed, hr = %#08x\n", hr);
656
657     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
658     ok(hr == D3D_OK, "Clear failed, hr = %#08x\n", hr);
659
660     hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &offscreenTexture);
661     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
662     if(!offscreenTexture) {
663         trace("Failed to create an X8R8G8B8 offscreen texture, trying R5G6B5\n");
664         hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &offscreenTexture);
665         ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
666         if(!offscreenTexture) {
667             skip("Cannot create an offscreen render target\n");
668             goto out;
669         }
670     }
671
672     hr = IDirect3DDevice8_GetBackBuffer(device, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
673     ok(hr == D3D_OK, "Can't get back buffer, hr = %#08x\n", hr);
674     if(!backbuffer) {
675         goto out;
676     }
677
678     hr = IDirect3DTexture8_GetSurfaceLevel(offscreenTexture, 0, &offscreen);
679     ok(hr == D3D_OK, "Can't get offscreen surface, hr = %#08x\n", hr);
680     if(!offscreen) {
681         goto out;
682     }
683
684     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
685     ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
686
687     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
688     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
689     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
690     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
691     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_NONE);
692     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (%#08x)\n", hr);
693     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_NONE);
694     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (%#08x)\n", hr);
695     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
696     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %08x\n", hr);
697
698     if(IDirect3DDevice8_BeginScene(device) == D3D_OK) {
699         hr = IDirect3DDevice8_SetRenderTarget(device, offscreen, depthstencil);
700         ok(hr == D3D_OK, "SetRenderTarget failed, hr = %#08x\n", hr);
701         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
702         ok(hr == D3D_OK, "Clear failed, hr = %#08x\n", hr);
703
704         /* Draw without textures - Should result in a white quad */
705         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(quad[0]));
706         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
707
708         hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
709         ok(hr == D3D_OK, "SetRenderTarget failed, hr = %#08x\n", hr);
710         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) offscreenTexture);
711         ok(hr == D3D_OK, "SetTexture failed, %08x\n", hr);
712
713         /* This time with the texture */
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         IDirect3DDevice8_EndScene(device);
718     }
719
720     /* Center quad - should be white */
721     color = getPixelColor(device, 320, 240);
722     ok(color == 0x00ffffff, "Offscreen failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
723     /* Some quad in the cleared part of the texture */
724     color = getPixelColor(device, 170, 240);
725     ok(color == 0x00ff00ff, "Offscreen failed: Got color 0x%08x, expected 0x00ff00ff.\n", color);
726     /* Part of the originally cleared back buffer */
727     color = getPixelColor(device, 10, 10);
728     ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
729     if(0) {
730         /* Lower left corner of the screen, where back buffer offscreen rendering draws the offscreen texture.
731         * It should be red, but the offscreen texture may leave some junk there. Not tested yet. Depending on
732         * the offscreen rendering mode this test would succeed or fail
733         */
734         color = getPixelColor(device, 10, 470);
735         ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
736     }
737
738     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
739
740 out:
741     hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
742     ok(SUCCEEDED(hr), "IDirect3DDevice8_SetTexture returned %#x.\n", hr);
743
744     /* restore things */
745     if(backbuffer) {
746         hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
747         ok(SUCCEEDED(hr), "IDirect3DDevice8_SetRenderTarget returned %#x.\n", hr);
748         IDirect3DSurface8_Release(backbuffer);
749     }
750     if(offscreenTexture) {
751         IDirect3DTexture8_Release(offscreenTexture);
752     }
753     if(offscreen) {
754         IDirect3DSurface8_Release(offscreen);
755     }
756     if(depthstencil) {
757         IDirect3DSurface8_Release(depthstencil);
758     }
759 }
760
761 static void alpha_test(IDirect3DDevice8 *device)
762 {
763     HRESULT hr;
764     IDirect3DTexture8 *offscreenTexture;
765     IDirect3DSurface8 *backbuffer = NULL, *offscreen = NULL, *depthstencil = NULL;
766     DWORD color;
767
768     struct vertex quad1[] =
769     {
770         {-1.0f, -1.0f,   0.1f,                          0x4000ff00},
771         {-1.0f,  0.0f,   0.1f,                          0x4000ff00},
772         { 1.0f, -1.0f,   0.1f,                          0x4000ff00},
773         { 1.0f,  0.0f,   0.1f,                          0x4000ff00},
774     };
775     struct vertex quad2[] =
776     {
777         {-1.0f,  0.0f,   0.1f,                          0xc00000ff},
778         {-1.0f,  1.0f,   0.1f,                          0xc00000ff},
779         { 1.0f,  0.0f,   0.1f,                          0xc00000ff},
780         { 1.0f,  1.0f,   0.1f,                          0xc00000ff},
781     };
782     static const float composite_quad[][5] = {
783         { 0.0f, -1.0f, 0.1f, 0.0f, 1.0f},
784         { 0.0f,  1.0f, 0.1f, 0.0f, 0.0f},
785         { 1.0f, -1.0f, 0.1f, 1.0f, 1.0f},
786         { 1.0f,  1.0f, 0.1f, 1.0f, 0.0f},
787     };
788
789     /* Clear the render target with alpha = 0.5 */
790     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
791     ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
792
793     hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &offscreenTexture);
794     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
795
796     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &depthstencil);
797     ok(hr == D3D_OK, "IDirect3DDevice8_GetDepthStencilSurface failed, hr = %#08x\n", hr);
798
799     hr = IDirect3DDevice8_GetBackBuffer(device, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
800     ok(hr == D3D_OK, "Can't get back buffer, hr = %#08x\n", hr);
801     if(!backbuffer) {
802         goto out;
803     }
804     hr = IDirect3DTexture8_GetSurfaceLevel(offscreenTexture, 0, &offscreen);
805     ok(hr == D3D_OK, "Can't get offscreen surface, hr = %#08x\n", hr);
806     if(!offscreen) {
807         goto out;
808     }
809
810     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
811     ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
812
813     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
814     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
815     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
816     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
817     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_NONE);
818     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (%#08x)\n", hr);
819     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_NONE);
820     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (%#08x)\n", hr);
821     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
822     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %08x\n", hr);
823
824     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
825     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
826     if(IDirect3DDevice8_BeginScene(device) == D3D_OK) {
827
828         /* Draw two quads, one with src alpha blending, one with dest alpha blending. */
829         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
830         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
831         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
832         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
833         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(quad1[0]));
834         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
835
836         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_DESTALPHA);
837         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
838         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVDESTALPHA);
839         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
840         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(quad2[0]));
841         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
842
843         /* Switch to the offscreen buffer, and redo the testing. The offscreen render target
844          * doesn't have an alpha channel. DESTALPHA and INVDESTALPHA "don't work" on render
845          * targets without alpha channel, they give essentially ZERO and ONE blend factors. */
846         hr = IDirect3DDevice8_SetRenderTarget(device, offscreen, 0);
847         ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
848         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
849         ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
850
851         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
852         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
853         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
854         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
855         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(quad1[0]));
856         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
857
858         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_DESTALPHA);
859         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
860         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVDESTALPHA);
861         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
862         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(quad2[0]));
863         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
864
865         hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
866         ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
867
868         /* Render the offscreen texture onto the frame buffer to be able to compare it regularly.
869          * Disable alpha blending for the final composition
870          */
871         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
872         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
873         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
874         ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
875
876         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) offscreenTexture);
877         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
878         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, composite_quad, sizeof(float) * 5);
879         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
880         hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
881         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
882
883         hr = IDirect3DDevice8_EndScene(device);
884         ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed, hr = %08x\n", hr);
885     }
886
887     color = getPixelColor(device, 160, 360);
888     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0xbf, 0x40, 0x00), 1),
889        "SRCALPHA on frame buffer returned color %08x, expected 0x00bf4000\n", color);
890
891     color = getPixelColor(device, 160, 120);
892     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0x7f, 0x00, 0x80), 2),
893        "DSTALPHA on frame buffer returned color %08x, expected 0x007f0080\n", color);
894
895     color = getPixelColor(device, 480, 360);
896     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0xbf, 0x40, 0x00), 1),
897        "SRCALPHA on texture returned color %08x, expected 0x00bf4000\n", color);
898
899     color = getPixelColor(device, 480, 120);
900     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0x00, 0x00, 0xff), 1),
901        "DSTALPHA on texture returned color %08x, expected 0x000000ff\n", color);
902
903     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
904
905     out:
906     /* restore things */
907     if(backbuffer) {
908         IDirect3DSurface8_Release(backbuffer);
909     }
910     if(offscreenTexture) {
911         IDirect3DTexture8_Release(offscreenTexture);
912     }
913     if(offscreen) {
914         IDirect3DSurface8_Release(offscreen);
915     }
916     if(depthstencil) {
917         IDirect3DSurface8_Release(depthstencil);
918     }
919 }
920
921 static void p8_texture_test(IDirect3DDevice8 *device)
922 {
923     IDirect3D8 *d3d = NULL;
924     HRESULT hr;
925     IDirect3DTexture8 *texture = NULL, *texture2 = NULL;
926     D3DLOCKED_RECT lr;
927     unsigned char *data;
928     DWORD color, red, green, blue;
929     PALETTEENTRY table[256];
930     D3DCAPS8 caps;
931     UINT i;
932     float quad[] = {
933        -1.0,       0,       0.1,     0.0,    0.0,
934        -1.0,       1.0,     0.1,     0.0,    1.0,
935         1.0,       0,       0.1,     1.0,    0.0,
936         1.0,       1.0,     0.1,     1.0,    1.0,
937     };
938     float quad2[] = {
939        -1.0,       -1.0,    0.1,     0.0,    0.0,
940        -1.0,       0,       0.1,     0.0,    1.0,
941         1.0,       -1.0,    0.1,     1.0,    0.0,
942         1.0,       0,       0.1,     1.0,    1.0,
943     };
944
945     IDirect3DDevice8_GetDirect3D(device, &d3d);
946
947     if(IDirect3D8_CheckDeviceFormat(d3d, 0, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, 0,
948        D3DRTYPE_TEXTURE, D3DFMT_P8) != D3D_OK) {
949            skip("D3DFMT_P8 textures not supported\n");
950            goto out;
951     }
952
953     hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_P8,
954                                         D3DPOOL_MANAGED, &texture2);
955     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed, hr = %08x\n", hr);
956     if(!texture2) {
957         skip("Failed to create D3DFMT_P8 texture\n");
958         goto out;
959     }
960
961     memset(&lr, 0, sizeof(lr));
962     hr = IDirect3DTexture8_LockRect(texture2, 0, &lr, NULL, 0);
963     ok(hr == D3D_OK, "IDirect3DTexture8_LockRect failed, hr = %08x\n", hr);
964     data = lr.pBits;
965     *data = 1;
966
967     hr = IDirect3DTexture8_UnlockRect(texture2, 0);
968     ok(hr == D3D_OK, "IDirect3DTexture8_UnlockRect failed, hr = %08x\n", hr);
969
970     hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_P8,
971                                         D3DPOOL_MANAGED, &texture);
972     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed, hr = %08x\n", hr);
973     if(!texture) {
974         skip("Failed to create D3DFMT_P8 texture\n");
975         goto out;
976     }
977
978     memset(&lr, 0, sizeof(lr));
979     hr = IDirect3DTexture8_LockRect(texture, 0, &lr, NULL, 0);
980     ok(hr == D3D_OK, "IDirect3DTexture8_LockRect failed, hr = %08x\n", hr);
981     data = lr.pBits;
982     *data = 1;
983
984     hr = IDirect3DTexture8_UnlockRect(texture, 0);
985     ok(hr == D3D_OK, "IDirect3DTexture8_UnlockRect failed, hr = %08x\n", hr);
986
987     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
988     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
989
990     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
991     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
992
993     /* The first part of the test should work both with and without D3DPTEXTURECAPS_ALPHAPALETTE;
994        alpha of every entry is set to 1.0, which MS says is required when there's no
995        D3DPTEXTURECAPS_ALPHAPALETTE capability */
996     for (i = 0; i < 256; i++) {
997         table[i].peRed = table[i].peGreen = table[i].peBlue = 0;
998         table[i].peFlags = 0xff;
999     }
1000     table[1].peRed = 0xff;
1001     hr = IDirect3DDevice8_SetPaletteEntries(device, 0, table);
1002     ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1003
1004     table[1].peRed = 0;
1005     table[1].peBlue = 0xff;
1006     hr = IDirect3DDevice8_SetPaletteEntries(device, 1, table);
1007     ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1008
1009     hr = IDirect3DDevice8_BeginScene(device);
1010     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1011     if(SUCCEEDED(hr)) {
1012         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
1013         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1014         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
1015         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1016
1017         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
1018         ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
1019
1020         hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 0);
1021         ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1022
1023         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture2);
1024         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1025         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1026         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1027
1028         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture);
1029         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1030         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1031         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1032
1033         hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 1);
1034         ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1035         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, 5 * sizeof(float));
1036         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1037
1038         hr = IDirect3DDevice8_EndScene(device);
1039         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1040     }
1041
1042     color = getPixelColor(device, 32, 32);
1043     red   = (color & 0x00ff0000) >> 16;
1044     green = (color & 0x0000ff00) >>  8;
1045     blue  = (color & 0x000000ff) >>  0;
1046     ok(red == 0xff && blue == 0 && green == 0,
1047        "got color %08x, expected 0x00ff0000\n", color);
1048
1049     color = getPixelColor(device, 32, 320);
1050     red   = (color & 0x00ff0000) >> 16;
1051     green = (color & 0x0000ff00) >>  8;
1052     blue  = (color & 0x000000ff) >>  0;
1053     ok(red == 0 && blue == 0xff && green == 0,
1054     "got color %08x, expected 0x000000ff\n", color);
1055
1056     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1057     ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1058
1059     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
1060     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
1061
1062     hr = IDirect3DDevice8_BeginScene(device);
1063     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1064     if(SUCCEEDED(hr)) {
1065         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture2);
1066         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1067
1068         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1069         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1070
1071         hr = IDirect3DDevice8_EndScene(device);
1072         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1073     }
1074
1075
1076     color = getPixelColor(device, 32, 32);
1077     red   = (color & 0x00ff0000) >> 16;
1078     green = (color & 0x0000ff00) >>  8;
1079     blue  = (color & 0x000000ff) >>  0;
1080     ok(red == 0 && blue == 0xff && green == 0,
1081     "got color %08x, expected 0x000000ff\n", color);
1082
1083     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1084     ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1085
1086     /* Test palettes with alpha */
1087     IDirect3DDevice8_GetDeviceCaps(device, &caps);
1088     if (!(caps.TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)) {
1089         skip("no D3DPTEXTURECAPS_ALPHAPALETTE capability, tests with alpha in palette will be skipped\n");
1090     } else {
1091         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
1092         ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
1093
1094         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
1095         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1096
1097         for (i = 0; i < 256; i++) {
1098             table[i].peRed = table[i].peGreen = table[i].peBlue = 0;
1099             table[i].peFlags = 0xff;
1100         }
1101         table[1].peRed = 0xff;
1102         table[1].peFlags = 0x80;
1103         hr = IDirect3DDevice8_SetPaletteEntries(device, 0, table);
1104         ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1105
1106         table[1].peRed = 0;
1107         table[1].peBlue = 0xff;
1108         table[1].peFlags = 0x80;
1109         hr = IDirect3DDevice8_SetPaletteEntries(device, 1, table);
1110         ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1111
1112         hr = IDirect3DDevice8_BeginScene(device);
1113         ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1114         if(SUCCEEDED(hr)) {
1115             hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
1116             ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1117             hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
1118             ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1119
1120             hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
1121             ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
1122
1123             hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 0);
1124             ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1125
1126             hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1127             ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1128
1129             hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 1);
1130             ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1131
1132             hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, 5 * sizeof(float));
1133             ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1134
1135             hr = IDirect3DDevice8_EndScene(device);
1136             ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1137         }
1138
1139         color = getPixelColor(device, 32, 32);
1140         red   = (color & 0x00ff0000) >> 16;
1141         green = (color & 0x0000ff00) >>  8;
1142         blue  = (color & 0x000000ff) >>  0;
1143         ok(red >= 0x7e && red <= 0x81 && blue == 0 && green == 0,
1144         "got color %08x, expected 0x00800000 or near\n", color);
1145
1146         color = getPixelColor(device, 32, 320);
1147         red   = (color & 0x00ff0000) >> 16;
1148         green = (color & 0x0000ff00) >>  8;
1149         blue  = (color & 0x000000ff) >>  0;
1150         ok(red == 0 && blue >= 0x7e && blue <= 0x81 && green == 0,
1151         "got color %08x, expected 0x00000080 or near\n", color);
1152
1153         hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1154         ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1155     }
1156
1157     hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
1158     ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1159     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
1160     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1161
1162 out:
1163     if(texture) IDirect3DTexture8_Release(texture);
1164     if(texture2) IDirect3DTexture8_Release(texture2);
1165     IDirect3D8_Release(d3d);
1166 }
1167
1168 static void texop_test(IDirect3DDevice8 *device)
1169 {
1170     IDirect3DTexture8 *texture = NULL;
1171     D3DLOCKED_RECT locked_rect;
1172     D3DCOLOR color;
1173     D3DCAPS8 caps;
1174     HRESULT hr;
1175     unsigned int i;
1176
1177     static const struct {
1178         float x, y, z;
1179         D3DCOLOR diffuse;
1180         float s, t;
1181     } quad[] = {
1182         {-1.0f, -1.0f, 0.1f, D3DCOLOR_ARGB(0x55, 0xff, 0x00, 0x00), -1.0f, -1.0f},
1183         {-1.0f,  1.0f, 0.1f, D3DCOLOR_ARGB(0x55, 0xff, 0x00, 0x00), -1.0f,  1.0f},
1184         { 1.0f, -1.0f, 0.1f, D3DCOLOR_ARGB(0x55, 0xff, 0x00, 0x00),  1.0f, -1.0f},
1185         { 1.0f,  1.0f, 0.1f, D3DCOLOR_ARGB(0x55, 0xff, 0x00, 0x00),  1.0f,  1.0f}
1186     };
1187
1188     static const struct {
1189         D3DTEXTUREOP op;
1190         const char *name;
1191         DWORD caps_flag;
1192         D3DCOLOR result;
1193     } test_data[] = {
1194         {D3DTOP_SELECTARG1,                "SELECTARG1",                D3DTEXOPCAPS_SELECTARG1,                D3DCOLOR_ARGB(0x00, 0x00, 0xff, 0x00)},
1195         {D3DTOP_SELECTARG2,                "SELECTARG2",                D3DTEXOPCAPS_SELECTARG2,                D3DCOLOR_ARGB(0x00, 0x33, 0x33, 0x33)},
1196         {D3DTOP_MODULATE,                  "MODULATE",                  D3DTEXOPCAPS_MODULATE,                  D3DCOLOR_ARGB(0x00, 0x00, 0x33, 0x00)},
1197         {D3DTOP_MODULATE2X,                "MODULATE2X",                D3DTEXOPCAPS_MODULATE2X,                D3DCOLOR_ARGB(0x00, 0x00, 0x66, 0x00)},
1198         {D3DTOP_MODULATE4X,                "MODULATE4X",                D3DTEXOPCAPS_MODULATE4X,                D3DCOLOR_ARGB(0x00, 0x00, 0xcc, 0x00)},
1199         {D3DTOP_ADD,                       "ADD",                       D3DTEXOPCAPS_ADD,                       D3DCOLOR_ARGB(0x00, 0x33, 0xff, 0x33)},
1200
1201         {D3DTOP_ADDSIGNED,                 "ADDSIGNED",                 D3DTEXOPCAPS_ADDSIGNED,                 D3DCOLOR_ARGB(0x00, 0x00, 0xb2, 0x00)},
1202         {D3DTOP_ADDSIGNED2X,               "ADDSIGNED2X",               D3DTEXOPCAPS_ADDSIGNED2X,               D3DCOLOR_ARGB(0x00, 0x00, 0xff, 0x00)},
1203
1204         {D3DTOP_SUBTRACT,                  "SUBTRACT",                  D3DTEXOPCAPS_SUBTRACT,                  D3DCOLOR_ARGB(0x00, 0x00, 0xcc, 0x00)},
1205         {D3DTOP_ADDSMOOTH,                 "ADDSMOOTH",                 D3DTEXOPCAPS_ADDSMOOTH,                 D3DCOLOR_ARGB(0x00, 0x33, 0xff, 0x33)},
1206         {D3DTOP_BLENDDIFFUSEALPHA,         "BLENDDIFFUSEALPHA",         D3DTEXOPCAPS_BLENDDIFFUSEALPHA,         D3DCOLOR_ARGB(0x00, 0x22, 0x77, 0x22)},
1207         {D3DTOP_BLENDTEXTUREALPHA,         "BLENDTEXTUREALPHA",         D3DTEXOPCAPS_BLENDTEXTUREALPHA,         D3DCOLOR_ARGB(0x00, 0x14, 0xad, 0x14)},
1208         {D3DTOP_BLENDFACTORALPHA,          "BLENDFACTORALPHA",          D3DTEXOPCAPS_BLENDFACTORALPHA,          D3DCOLOR_ARGB(0x00, 0x07, 0xe4, 0x07)},
1209         {D3DTOP_BLENDTEXTUREALPHAPM,       "BLENDTEXTUREALPHAPM",       D3DTEXOPCAPS_BLENDTEXTUREALPHAPM,       D3DCOLOR_ARGB(0x00, 0x14, 0xff, 0x14)},
1210         {D3DTOP_BLENDCURRENTALPHA,         "BLENDCURRENTALPHA",         D3DTEXOPCAPS_BLENDCURRENTALPHA,         D3DCOLOR_ARGB(0x00, 0x22, 0x77, 0x22)},
1211         {D3DTOP_MODULATEALPHA_ADDCOLOR,    "MODULATEALPHA_ADDCOLOR",    D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR,    D3DCOLOR_ARGB(0x00, 0x1f, 0xff, 0x1f)},
1212         {D3DTOP_MODULATECOLOR_ADDALPHA,    "MODULATECOLOR_ADDALPHA",    D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA,    D3DCOLOR_ARGB(0x00, 0x99, 0xcc, 0x99)},
1213         {D3DTOP_MODULATEINVALPHA_ADDCOLOR, "MODULATEINVALPHA_ADDCOLOR", D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR, D3DCOLOR_ARGB(0x00, 0x14, 0xff, 0x14)},
1214         {D3DTOP_MODULATEINVCOLOR_ADDALPHA, "MODULATEINVCOLOR_ADDALPHA", D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA, D3DCOLOR_ARGB(0x00, 0xcc, 0x99, 0xcc)},
1215         /* BUMPENVMAP & BUMPENVMAPLUMINANCE have their own tests */
1216         {D3DTOP_DOTPRODUCT3,               "DOTPRODUCT2",               D3DTEXOPCAPS_DOTPRODUCT3,               D3DCOLOR_ARGB(0x00, 0x99, 0x99, 0x99)},
1217         {D3DTOP_MULTIPLYADD,               "MULTIPLYADD",               D3DTEXOPCAPS_MULTIPLYADD,               D3DCOLOR_ARGB(0x00, 0xff, 0x33, 0x00)},
1218         {D3DTOP_LERP,                      "LERP",                      D3DTEXOPCAPS_LERP,                      D3DCOLOR_ARGB(0x00, 0x00, 0x33, 0x33)},
1219     };
1220
1221     memset(&caps, 0, sizeof(caps));
1222     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1223     ok(SUCCEEDED(hr), "GetDeviceCaps failed with 0x%08x\n", hr);
1224
1225     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX0);
1226     ok(SUCCEEDED(hr), "SetVertexShader failed with 0x%08x\n", hr);
1227
1228     hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture);
1229     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateTexture failed with 0x%08x\n", hr);
1230     hr = IDirect3DTexture8_LockRect(texture, 0, &locked_rect, NULL, 0);
1231     ok(SUCCEEDED(hr), "LockRect failed with 0x%08x\n", hr);
1232     *((DWORD *)locked_rect.pBits) = D3DCOLOR_ARGB(0x99, 0x00, 0xff, 0x00);
1233     hr = IDirect3DTexture8_UnlockRect(texture, 0);
1234     ok(SUCCEEDED(hr), "LockRect failed with 0x%08x\n", hr);
1235     hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *)texture);
1236     ok(SUCCEEDED(hr), "SetTexture failed with 0x%08x\n", hr);
1237
1238     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG0, D3DTA_DIFFUSE);
1239     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x\n", hr);
1240     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
1241     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x\n", hr);
1242     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
1243     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x\n", hr);
1244
1245     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
1246     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x\n", hr);
1247
1248     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1249     ok(SUCCEEDED(hr), "SetRenderState failed with 0x%08x\n", hr);
1250     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_TEXTUREFACTOR, 0xdd333333);
1251     ok(SUCCEEDED(hr), "SetRenderState failed with 0x%08x\n", hr);
1252     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA);
1253     ok(SUCCEEDED(hr), "SetRenderState failed with 0x%08x\n", hr);
1254
1255     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
1256     ok(SUCCEEDED(hr), "IDirect3DDevice9_Clear failed with 0x%08x\n", hr);
1257
1258     for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
1259     {
1260         if (!(caps.TextureOpCaps & test_data[i].caps_flag))
1261         {
1262             skip("tex operation %s not supported\n", test_data[i].name);
1263             continue;
1264         }
1265
1266         hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLOROP, test_data[i].op);
1267         ok(SUCCEEDED(hr), "SetTextureStageState (%s) failed with 0x%08x\n", test_data[i].name, hr);
1268
1269         hr = IDirect3DDevice8_BeginScene(device);
1270         ok(SUCCEEDED(hr), "BeginScene failed with 0x%08x\n", hr);
1271
1272         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
1273         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed with 0x%08x\n", hr);
1274
1275         hr = IDirect3DDevice8_EndScene(device);
1276         ok(SUCCEEDED(hr), "EndScene failed with 0x%08x\n", hr);
1277
1278         color = getPixelColor(device, 320, 240);
1279         ok(color_match(color, test_data[i].result, 3), "Operation %s returned color 0x%08x, expected 0x%08x\n",
1280                 test_data[i].name, color, test_data[i].result);
1281
1282         hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1283         ok(SUCCEEDED(hr), "Present failed with 0x%08x\n", hr);
1284
1285         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
1286         ok(SUCCEEDED(hr), "IDirect3DDevice9_Clear failed with 0x%08x\n", hr);
1287     }
1288
1289     hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
1290     ok(SUCCEEDED(hr), "SetTexture failed with 0x%08x\n", hr);
1291     if (texture) IDirect3DTexture8_Release(texture);
1292 }
1293
1294 /* This test tests depth clamping / clipping behaviour:
1295  *   - When D3DRS_CLIPPING is disabled depth values are *clamped* to the
1296  *   minimum/maximum z value.
1297  *   - The viewport's MinZ/MaxZ is irrelevant for this.
1298  *   - When D3DRS_CLIPPING is enabled depth values are clipped.
1299  *   - Pretransformed vertices behave the same as regular vertices.
1300  */
1301 static void depth_clamp_test(IDirect3DDevice8 *device)
1302 {
1303     const struct tvertex quad1[] =
1304     {
1305         {    0,    0,  5.0f, 1.0, 0xff002b7f},
1306         {  640,    0,  5.0f, 1.0, 0xff002b7f},
1307         {    0,  480,  5.0f, 1.0, 0xff002b7f},
1308         {  640,  480,  5.0f, 1.0, 0xff002b7f},
1309     };
1310     const struct tvertex quad2[] =
1311     {
1312         {    0,  300, 10.0f, 1.0, 0xfff9e814},
1313         {  640,  300, 10.0f, 1.0, 0xfff9e814},
1314         {    0,  360, 10.0f, 1.0, 0xfff9e814},
1315         {  640,  360, 10.0f, 1.0, 0xfff9e814},
1316     };
1317     const struct vertex quad3[] =
1318     {
1319         {-0.65, 0.55,  5.0f,      0xffffffff},
1320         {-0.35, 0.55,  5.0f,      0xffffffff},
1321         {-0.65, 0.15,  5.0f,      0xffffffff},
1322         {-0.35, 0.15,  5.0f,      0xffffffff},
1323     };
1324     const struct vertex quad4[] =
1325     {
1326         {-0.87, 0.83, 10.0f,      0xffffffff},
1327         {-0.65, 0.83, 10.0f,      0xffffffff},
1328         {-0.87, 0.55, 10.0f,      0xffffffff},
1329         {-0.65, 0.55, 10.0f,      0xffffffff},
1330     };
1331     const struct vertex quad5[] =
1332     {
1333         { -0.5,  0.5, 10.0f,      0xff14f914},
1334         {  0.5,  0.5, 10.0f,      0xff14f914},
1335         { -0.5, -0.5, 10.0f,      0xff14f914},
1336         {  0.5, -0.5, 10.0f,      0xff14f914},
1337     };
1338     const struct tvertex quad6[] =
1339     {
1340         {    0,  120, 10.0f, 1.0, 0xfff91414},
1341         {  640,  120, 10.0f, 1.0, 0xfff91414},
1342         {    0,  180, 10.0f, 1.0, 0xfff91414},
1343         {  640,  180, 10.0f, 1.0, 0xfff91414},
1344     };
1345
1346     D3DVIEWPORT8 vp;
1347     D3DCOLOR color;
1348     HRESULT hr;
1349
1350     vp.X = 0;
1351     vp.Y = 0;
1352     vp.Width = 640;
1353     vp.Height = 480;
1354     vp.MinZ = 0.0;
1355     vp.MaxZ = 7.5;
1356
1357     hr = IDirect3DDevice8_SetViewport(device, &vp);
1358     ok(SUCCEEDED(hr), "SetViewport failed, hr %#x.\n", hr);
1359
1360     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0, 0);
1361     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1362
1363     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CLIPPING, FALSE);
1364     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1365     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1366     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1367     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, TRUE);
1368     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1369     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
1370     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1371
1372     hr = IDirect3DDevice8_BeginScene(device);
1373     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1374
1375     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1376     ok(SUCCEEDED(hr), "SetVertexSahder failed, hr %#x.\n", hr);
1377
1378     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(*quad1));
1379     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1380     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(*quad2));
1381     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1382
1383     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
1384     ok(SUCCEEDED(hr), "SetVertexShader failed, hr %#x.\n", hr);
1385
1386     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad3, sizeof(*quad3));
1387     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1388     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad4, sizeof(*quad4));
1389     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1390
1391     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CLIPPING, TRUE);
1392     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1393
1394     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad5, sizeof(*quad5));
1395     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1396
1397     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1398     ok(SUCCEEDED(hr), "SetVertexShader failed, hr %#x.\n", hr);
1399
1400     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad6, sizeof(*quad6));
1401     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1402
1403     hr = IDirect3DDevice8_EndScene(device);
1404     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1405
1406     color = getPixelColor(device, 75, 75);
1407     ok(color_match(color, 0x00ffffff, 1), "color 0x%08x.\n", color);
1408     color = getPixelColor(device, 150, 150);
1409     ok(color_match(color, 0x00ffffff, 1), "color 0x%08x.\n", color);
1410     color = getPixelColor(device, 320, 240);
1411     ok(color_match(color, 0x00002b7f, 1), "color 0x%08x.\n", color);
1412     color = getPixelColor(device, 320, 330);
1413     ok(color_match(color, 0x00f9e814, 1), "color 0x%08x.\n", color);
1414     color = getPixelColor(device, 320, 330);
1415     ok(color_match(color, 0x00f9e814, 1), "color 0x%08x.\n", color);
1416
1417     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1418     ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1419
1420     vp.MinZ = 0.0;
1421     vp.MaxZ = 1.0;
1422     hr = IDirect3DDevice8_SetViewport(device, &vp);
1423     ok(SUCCEEDED(hr), "SetViewport failed, hr %#x.\n", hr);
1424 }
1425
1426 static void depth_buffer_test(IDirect3DDevice8 *device)
1427 {
1428     static const struct vertex quad1[] =
1429     {
1430         { -1.0,  1.0, 0.33f, 0xff00ff00},
1431         {  1.0,  1.0, 0.33f, 0xff00ff00},
1432         { -1.0, -1.0, 0.33f, 0xff00ff00},
1433         {  1.0, -1.0, 0.33f, 0xff00ff00},
1434     };
1435     static const struct vertex quad2[] =
1436     {
1437         { -1.0,  1.0, 0.50f, 0xffff00ff},
1438         {  1.0,  1.0, 0.50f, 0xffff00ff},
1439         { -1.0, -1.0, 0.50f, 0xffff00ff},
1440         {  1.0, -1.0, 0.50f, 0xffff00ff},
1441     };
1442     static const struct vertex quad3[] =
1443     {
1444         { -1.0,  1.0, 0.66f, 0xffff0000},
1445         {  1.0,  1.0, 0.66f, 0xffff0000},
1446         { -1.0, -1.0, 0.66f, 0xffff0000},
1447         {  1.0, -1.0, 0.66f, 0xffff0000},
1448     };
1449     static const DWORD expected_colors[4][4] =
1450     {
1451         {0x000000ff, 0x000000ff, 0x0000ff00, 0x00ff0000},
1452         {0x000000ff, 0x000000ff, 0x0000ff00, 0x00ff0000},
1453         {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x00ff0000},
1454         {0x00ff0000, 0x00ff0000, 0x00ff0000, 0x00ff0000},
1455     };
1456
1457     IDirect3DSurface8 *backbuffer, *rt1, *rt2, *rt3;
1458     IDirect3DSurface8 *depth_stencil;
1459     unsigned int i, j;
1460     D3DVIEWPORT8 vp;
1461     D3DCOLOR color;
1462     HRESULT hr;
1463
1464     vp.X = 0;
1465     vp.Y = 0;
1466     vp.Width = 640;
1467     vp.Height = 480;
1468     vp.MinZ = 0.0;
1469     vp.MaxZ = 1.0;
1470
1471     hr = IDirect3DDevice8_SetViewport(device, &vp);
1472     ok(SUCCEEDED(hr), "SetViewport failed, hr %#x.\n", hr);
1473
1474     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1475     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1476     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
1477     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1478     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, TRUE);
1479     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1480     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
1481     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1482     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
1483     ok(SUCCEEDED(hr), "SetVertexShader failed, hr %#x.\n", hr);
1484
1485     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &depth_stencil);
1486     ok(SUCCEEDED(hr), "GetDepthStencilSurface failed, hr %#x.\n", hr);
1487     hr = IDirect3DDevice8_GetRenderTarget(device, &backbuffer);
1488     ok(SUCCEEDED(hr), "GetRenderTarget failed, hr %#x.\n", hr);
1489     hr = IDirect3DDevice8_CreateRenderTarget(device, 320, 240, D3DFMT_A8R8G8B8,
1490             D3DMULTISAMPLE_NONE, FALSE, &rt1);
1491     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
1492     hr = IDirect3DDevice8_CreateRenderTarget(device, 480, 360, D3DFMT_A8R8G8B8,
1493             D3DMULTISAMPLE_NONE, FALSE, &rt2);
1494     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
1495     hr = IDirect3DDevice8_CreateRenderTarget(device, 640, 480, D3DFMT_A8R8G8B8,
1496             D3DMULTISAMPLE_NONE, FALSE, &rt3);
1497     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
1498
1499     hr = IDirect3DDevice8_SetRenderTarget(device, rt3, depth_stencil);
1500     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1501     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 0.0f, 0);
1502     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1503
1504     hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depth_stencil);
1505     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1506     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 1.0f, 0);
1507     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1508
1509     hr = IDirect3DDevice8_SetRenderTarget(device, rt1, depth_stencil);
1510     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1511     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 0.0f, 0);
1512     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1513
1514     hr = IDirect3DDevice8_SetRenderTarget(device, rt2, depth_stencil);
1515     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1516     hr = IDirect3DDevice8_BeginScene(device);
1517     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1518     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(*quad2));
1519     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1520     hr = IDirect3DDevice8_EndScene(device);
1521     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1522
1523     hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depth_stencil);
1524     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1525     IDirect3DSurface8_Release(depth_stencil);
1526     IDirect3DSurface8_Release(backbuffer);
1527     IDirect3DSurface8_Release(rt3);
1528     IDirect3DSurface8_Release(rt2);
1529     IDirect3DSurface8_Release(rt1);
1530
1531     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, FALSE);
1532     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1533
1534     hr = IDirect3DDevice8_BeginScene(device);
1535     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1536     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(*quad1));
1537     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1538     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad3, sizeof(*quad3));
1539     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1540     hr = IDirect3DDevice8_EndScene(device);
1541     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1542
1543     for (i = 0; i < 4; ++i)
1544     {
1545         for (j = 0; j < 4; ++j)
1546         {
1547             unsigned int x = 80 * ((2 * j) + 1);
1548             unsigned int y = 60 * ((2 * i) + 1);
1549             color = getPixelColor(device, x, y);
1550             ok(color_match(color, expected_colors[i][j], 0),
1551                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected_colors[i][j], x, y, color);
1552         }
1553     }
1554
1555     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1556     ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1557 }
1558
1559 static void intz_test(IDirect3DDevice8 *device)
1560 {
1561     static const DWORD ps_code[] =
1562     {
1563         0xffff0101,                                                             /* ps_1_1                       */
1564         0x00000051, 0xa00f0000, 0x3f800000, 0x00000000, 0x00000000, 0x00000000, /* def c0, 1.0, 0.0, 0.0, 0.0   */
1565         0x00000051, 0xa00f0001, 0x00000000, 0x3f800000, 0x00000000, 0x00000000, /* def c1, 0.0, 1.0, 0.0, 0.0   */
1566         0x00000051, 0xa00f0002, 0x00000000, 0x00000000, 0x3f800000, 0x00000000, /* def c2, 0.0, 0.0, 1.0, 0.0   */
1567         0x00000042, 0xb00f0000,                                                 /* tex t0                       */
1568         0x00000042, 0xb00f0001,                                                 /* tex t1                       */
1569         0x00000008, 0xb0070001, 0xa0e40000, 0xb0e40001,                         /* dp3 t1.xyz, c0, t1           */
1570         0x00000005, 0x80070000, 0xa0e40001, 0xb0e40001,                         /* mul r0.xyz, c1, t1           */
1571         0x00000004, 0x80070000, 0xa0e40000, 0xb0e40000, 0x80e40000,             /* mad r0.xyz, c0, t0, r0       */
1572         0x40000001, 0x80080000, 0xa0aa0002,                                     /* +mov r0.w, c2.z              */
1573         0x0000ffff,                                                             /* end                          */
1574     };
1575     struct
1576     {
1577         float x, y, z;
1578         float s0, t0, p0;
1579         float s1, t1, p1, q1;
1580     }
1581     quad[] =
1582     {
1583         { -1.0f,  1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f},
1584         {  1.0f,  1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f},
1585         { -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
1586         {  1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
1587     };
1588     struct
1589     {
1590         UINT x, y;
1591         D3DCOLOR color;
1592     }
1593     expected_colors[] =
1594     {
1595         {400,  60, D3DCOLOR_ARGB(0x00, 0x9f, 0xff, 0x00)},
1596         {560, 180, D3DCOLOR_ARGB(0x00, 0xdf, 0x55, 0x00)},
1597         {560, 300, D3DCOLOR_ARGB(0x00, 0xdf, 0x66, 0x00)},
1598         {400, 420, D3DCOLOR_ARGB(0x00, 0x9f, 0xb6, 0x00)},
1599         {240, 420, D3DCOLOR_ARGB(0x00, 0x60, 0x6d, 0x00)},
1600         { 80, 300, D3DCOLOR_ARGB(0x00, 0x20, 0x33, 0x00)},
1601         { 80, 180, D3DCOLOR_ARGB(0x00, 0x20, 0x55, 0x00)},
1602         {240,  60, D3DCOLOR_ARGB(0x00, 0x60, 0xff, 0x00)},
1603     };
1604
1605     IDirect3DSurface8 *original_ds, *original_rt, *rt;
1606     IDirect3DTexture8 *texture;
1607     IDirect3DSurface8 *ds;
1608     IDirect3D8 *d3d8;
1609     D3DCAPS8 caps;
1610     HRESULT hr;
1611     DWORD ps;
1612     UINT i;
1613
1614     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1615     ok(SUCCEEDED(hr), "GetDeviceCaps failed, hr %#x.\n", hr);
1616     if (caps.PixelShaderVersion < D3DPS_VERSION(1, 1))
1617     {
1618         skip("No pixel shader 1.1 support, skipping INTZ test.\n");
1619         return;
1620     }
1621
1622     hr = IDirect3DDevice8_GetDirect3D(device, &d3d8);
1623     ok(SUCCEEDED(hr), "GetDirect3D failed, hr %#x.\n", hr);
1624
1625     hr = IDirect3D8_CheckDeviceFormat(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8,
1626             D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, MAKEFOURCC('I','N','T','Z'));
1627     if (FAILED(hr))
1628     {
1629         skip("No INTZ support, skipping INTZ test.\n");
1630         return;
1631     }
1632
1633     IDirect3D8_Release(d3d8);
1634
1635     hr = IDirect3DDevice8_GetRenderTarget(device, &original_rt);
1636     ok(SUCCEEDED(hr), "GetRenderTarget failed, hr %#x.\n", hr);
1637     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &original_ds);
1638     ok(SUCCEEDED(hr), "GetDepthStencilSurface failed, hr %#x.\n", hr);
1639
1640     hr = IDirect3DDevice8_CreateTexture(device, 1024, 1024, 1,
1641             D3DUSAGE_DEPTHSTENCIL, MAKEFOURCC('I','N','T','Z'), D3DPOOL_DEFAULT, &texture);
1642     ok(SUCCEEDED(hr), "CreateTexture failed, hr %#x.\n", hr);
1643     hr = IDirect3DDevice8_CreateRenderTarget(device, 1024, 1024, D3DFMT_A8R8G8B8,
1644             D3DMULTISAMPLE_NONE, FALSE, &rt);
1645     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
1646     hr = IDirect3DDevice8_CreatePixelShader(device, ps_code, &ps);
1647     ok(SUCCEEDED(hr), "CreatePixelShader failed, hr %#x.\n", hr);
1648
1649     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX2
1650             | D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEXCOORDSIZE4(1));
1651     ok(SUCCEEDED(hr), "SetVertexShader failed, hr %#x.\n", hr);
1652     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
1653     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1654     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_ALWAYS);
1655     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1656     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, TRUE);
1657     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1658     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1659     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1660
1661     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_POINT);
1662     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1663     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MIPFILTER, D3DTEXF_POINT);
1664     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1665     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_POINT);
1666     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1667     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3);
1668     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1669
1670     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
1671     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1672     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
1673     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1674     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MAGFILTER, D3DTEXF_POINT);
1675     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1676     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MINFILTER, D3DTEXF_POINT);
1677     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1678     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MIPFILTER, D3DTEXF_POINT);
1679     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1680     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_TEXTURETRANSFORMFLAGS,
1681             D3DTTFF_COUNT4 | D3DTTFF_PROJECTED);
1682     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1683
1684     hr = IDirect3DTexture8_GetSurfaceLevel(texture, 0, &ds);
1685     ok(SUCCEEDED(hr), "GetSurfaceLevel failed, hr %#x.\n", hr);
1686     hr = IDirect3DDevice8_SetRenderTarget(device, rt, ds);
1687     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1688     IDirect3DDevice8_SetPixelShader(device, 0);
1689     ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
1690
1691     /* Setup the depth/stencil surface. */
1692     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_ZBUFFER, 0, 0.0f, 0);
1693     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1694
1695     hr = IDirect3DDevice8_BeginScene(device);
1696     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1697     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
1698     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1699     hr = IDirect3DDevice8_EndScene(device);
1700     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1701
1702     hr = IDirect3DDevice8_SetRenderTarget(device, original_rt, NULL);
1703     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1704     IDirect3DSurface8_Release(ds);
1705     hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *)texture);
1706     ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
1707     hr = IDirect3DDevice8_SetTexture(device, 1, (IDirect3DBaseTexture8 *)texture);
1708     ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
1709     hr = IDirect3DDevice8_SetPixelShader(device, ps);
1710     ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
1711
1712     /* Read the depth values back. */
1713     hr = IDirect3DDevice8_BeginScene(device);
1714     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1715     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
1716     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1717     hr = IDirect3DDevice8_EndScene(device);
1718     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1719
1720     for (i = 0; i < sizeof(expected_colors) / sizeof(*expected_colors); ++i)
1721     {
1722         D3DCOLOR color = getPixelColor(device, expected_colors[i].x, expected_colors[i].y);
1723         ok(color_match(color, expected_colors[i].color, 1),
1724                 "Expected color 0x%08x at (%u, %u), got 0x%08x.\n",
1725                 expected_colors[i].color, expected_colors[i].x, expected_colors[i].y, color);
1726     }
1727
1728     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1729     ok(SUCCEEDED(hr), "Present failed, hr %#x.\n", hr);
1730
1731     hr = IDirect3DDevice8_SetRenderTarget(device, original_rt, original_ds);
1732     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1733     IDirect3DSurface8_Release(original_ds);
1734     hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
1735     ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
1736     hr = IDirect3DDevice8_SetTexture(device, 1, NULL);
1737     ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
1738     IDirect3DTexture8_Release(texture);
1739     hr = IDirect3DDevice8_SetPixelShader(device, 0);
1740     ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
1741     hr = IDirect3DDevice8_DeletePixelShader(device, ps);
1742     ok(SUCCEEDED(hr), "DeletePixelShader failed, hr %#x.\n", hr);
1743
1744     IDirect3DSurface8_Release(original_rt);
1745     IDirect3DSurface8_Release(rt);
1746 }
1747
1748 static void shadow_test(IDirect3DDevice8 *device)
1749 {
1750     static const DWORD ps_code[] =
1751     {
1752         0xffff0101,                                                             /* ps_1_1                       */
1753         0x00000051, 0xa00f0000, 0x3f800000, 0x00000000, 0x00000000, 0x00000000, /* def c0, 1.0, 0.0, 0.0, 0.0   */
1754         0x00000051, 0xa00f0001, 0x00000000, 0x3f800000, 0x00000000, 0x00000000, /* def c1, 0.0, 1.0, 0.0, 0.0   */
1755         0x00000051, 0xa00f0002, 0x00000000, 0x00000000, 0x3f800000, 0x00000000, /* def c2, 0.0, 0.0, 1.0, 0.0   */
1756         0x00000042, 0xb00f0000,                                                 /* tex t0                       */
1757         0x00000042, 0xb00f0001,                                                 /* tex t1                       */
1758         0x00000008, 0xb0070001, 0xa0e40000, 0xb0e40001,                         /* dp3 t1.xyz, c0, t1           */
1759         0x00000005, 0x80070000, 0xa0e40001, 0xb0e40001,                         /* mul r0.xyz, c1, t1           */
1760         0x00000004, 0x80070000, 0xa0e40000, 0xb0e40000, 0x80e40000,             /* mad r0.xyz, c0, t0, r0       */
1761         0x40000001, 0x80080000, 0xa0aa0002,                                     /* +mov r0.w, c2.z              */
1762         0x0000ffff,                                                             /* end                          */
1763     };
1764     struct
1765     {
1766         D3DFORMAT format;
1767         const char *name;
1768     }
1769     formats[] =
1770     {
1771         {D3DFMT_D16_LOCKABLE,   "D3DFMT_D16_LOCKABLE"},
1772         {D3DFMT_D32,            "D3DFMT_D32"},
1773         {D3DFMT_D15S1,          "D3DFMT_D15S1"},
1774         {D3DFMT_D24S8,          "D3DFMT_D24S8"},
1775         {D3DFMT_D24X8,          "D3DFMT_D24X8"},
1776         {D3DFMT_D24X4S4,        "D3DFMT_D24X4S4"},
1777         {D3DFMT_D16,            "D3DFMT_D16"},
1778     };
1779     struct
1780     {
1781         float x, y, z;
1782         float s0, t0, p0;
1783         float s1, t1, p1, q1;
1784     }
1785     quad[] =
1786     {
1787         { -1.0f,  1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f},
1788         {  1.0f,  1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f},
1789         { -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
1790         {  1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
1791     };
1792     struct
1793     {
1794         UINT x, y;
1795         D3DCOLOR color;
1796     }
1797     expected_colors[] =
1798     {
1799         {400,  60, D3DCOLOR_ARGB(0x00, 0x00, 0x00, 0x00)},
1800         {560, 180, D3DCOLOR_ARGB(0x00, 0xff, 0x00, 0x00)},
1801         {560, 300, D3DCOLOR_ARGB(0x00, 0xff, 0x00, 0x00)},
1802         {400, 420, D3DCOLOR_ARGB(0x00, 0xff, 0xff, 0x00)},
1803         {240, 420, D3DCOLOR_ARGB(0x00, 0xff, 0xff, 0x00)},
1804         { 80, 300, D3DCOLOR_ARGB(0x00, 0x00, 0x00, 0x00)},
1805         { 80, 180, D3DCOLOR_ARGB(0x00, 0x00, 0x00, 0x00)},
1806         {240,  60, D3DCOLOR_ARGB(0x00, 0x00, 0x00, 0x00)},
1807     };
1808
1809     IDirect3DSurface8 *original_ds, *original_rt, *rt;
1810     IDirect3D8 *d3d8;
1811     D3DCAPS8 caps;
1812     HRESULT hr;
1813     DWORD ps;
1814     UINT i;
1815
1816     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1817     ok(SUCCEEDED(hr), "GetDeviceCaps failed, hr %#x.\n", hr);
1818     if (caps.PixelShaderVersion < D3DPS_VERSION(1, 1))
1819     {
1820         skip("No pixel shader 1.1 support, skipping shadow test.\n");
1821         return;
1822     }
1823
1824     hr = IDirect3DDevice8_GetDirect3D(device, &d3d8);
1825     ok(SUCCEEDED(hr), "GetDirect3D failed, hr %#x.\n", hr);
1826     hr = IDirect3DDevice8_GetRenderTarget(device, &original_rt);
1827     ok(SUCCEEDED(hr), "GetRenderTarget failed, hr %#x.\n", hr);
1828     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &original_ds);
1829     ok(SUCCEEDED(hr), "GetDepthStencilSurface failed, hr %#x.\n", hr);
1830
1831     hr = IDirect3DDevice8_CreateRenderTarget(device, 1024, 1024, D3DFMT_A8R8G8B8,
1832             D3DMULTISAMPLE_NONE, FALSE, &rt);
1833     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
1834     hr = IDirect3DDevice8_CreatePixelShader(device, ps_code, &ps);
1835     ok(SUCCEEDED(hr), "CreatePixelShader failed, hr %#x.\n", hr);
1836
1837     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX2
1838             | D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEXCOORDSIZE4(1));
1839     ok(SUCCEEDED(hr), "SetVertexShader failed, hr %#x.\n", hr);
1840     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
1841     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1842     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_ALWAYS);
1843     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1844     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, TRUE);
1845     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1846     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1847     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1848
1849     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_POINT);
1850     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1851     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MIPFILTER, D3DTEXF_POINT);
1852     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1853     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_POINT);
1854     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1855     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3);
1856     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1857
1858     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
1859     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1860     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
1861     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1862     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MAGFILTER, D3DTEXF_POINT);
1863     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1864     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MINFILTER, D3DTEXF_POINT);
1865     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1866     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MIPFILTER, D3DTEXF_POINT);
1867     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1868     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_TEXTURETRANSFORMFLAGS,
1869             D3DTTFF_COUNT4 | D3DTTFF_PROJECTED);
1870     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1871
1872     for (i = 0; i < sizeof(formats) / sizeof(*formats); ++i)
1873     {
1874         D3DFORMAT format = formats[i].format;
1875         IDirect3DTexture8 *texture;
1876         IDirect3DSurface8 *ds;
1877         unsigned int j;
1878
1879         hr = IDirect3D8_CheckDeviceFormat(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8,
1880                 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, format);
1881         if (FAILED(hr)) continue;
1882
1883         hr = IDirect3DDevice8_CreateTexture(device, 1024, 1024, 1,
1884                 D3DUSAGE_DEPTHSTENCIL, format, D3DPOOL_DEFAULT, &texture);
1885         ok(SUCCEEDED(hr), "CreateTexture failed, hr %#x.\n", hr);
1886
1887         hr = IDirect3DTexture8_GetSurfaceLevel(texture, 0, &ds);
1888         ok(SUCCEEDED(hr), "GetSurfaceLevel failed, hr %#x.\n", hr);
1889
1890         hr = IDirect3DDevice8_SetRenderTarget(device, rt, ds);
1891         ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1892
1893         IDirect3DDevice8_SetPixelShader(device, 0);
1894         ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
1895
1896         /* Setup the depth/stencil surface. */
1897         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_ZBUFFER, 0, 0.0f, 0);
1898         ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1899
1900         hr = IDirect3DDevice8_BeginScene(device);
1901         ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1902         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
1903         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1904         hr = IDirect3DDevice8_EndScene(device);
1905         ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1906
1907         hr = IDirect3DDevice8_SetRenderTarget(device, original_rt, NULL);
1908         ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1909         IDirect3DSurface8_Release(ds);
1910
1911         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *)texture);
1912         ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
1913         hr = IDirect3DDevice8_SetTexture(device, 1, (IDirect3DBaseTexture8 *)texture);
1914         ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
1915
1916         hr = IDirect3DDevice8_SetPixelShader(device, ps);
1917         ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
1918
1919         /* Do the actual shadow mapping. */
1920         hr = IDirect3DDevice8_BeginScene(device);
1921         ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1922         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
1923         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1924         hr = IDirect3DDevice8_EndScene(device);
1925         ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1926
1927         hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
1928         ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
1929         hr = IDirect3DDevice8_SetTexture(device, 1, NULL);
1930         ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
1931         IDirect3DTexture8_Release(texture);
1932
1933         for (j = 0; j < sizeof(expected_colors) / sizeof(*expected_colors); ++j)
1934         {
1935             D3DCOLOR color = getPixelColor(device, expected_colors[j].x, expected_colors[j].y);
1936             ok(color_match(color, expected_colors[j].color, 0),
1937                     "Expected color 0x%08x at (%u, %u) for format %s, got 0x%08x.\n",
1938                     expected_colors[j].color, expected_colors[j].x, expected_colors[j].y,
1939                     formats[i].name, color);
1940         }
1941
1942         hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1943         ok(SUCCEEDED(hr), "Present failed, hr %#x.\n", hr);
1944     }
1945
1946     hr = IDirect3DDevice8_SetPixelShader(device, 0);
1947     ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
1948     hr = IDirect3DDevice8_DeletePixelShader(device, ps);
1949     ok(SUCCEEDED(hr), "DeletePixelShader failed, hr %#x.\n", hr);
1950
1951     hr = IDirect3DDevice8_SetRenderTarget(device, original_rt, original_ds);
1952     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1953     IDirect3DSurface8_Release(original_ds);
1954
1955     IDirect3DSurface8_Release(original_rt);
1956     IDirect3DSurface8_Release(rt);
1957
1958     IDirect3D8_Release(d3d8);
1959 }
1960
1961 START_TEST(visual)
1962 {
1963     IDirect3DDevice8 *device_ptr;
1964     HRESULT hr;
1965     DWORD color;
1966     D3DCAPS8 caps;
1967
1968     d3d8_handle = LoadLibraryA("d3d8.dll");
1969     if (!d3d8_handle)
1970     {
1971         win_skip("Could not load d3d8.dll\n");
1972         return;
1973     }
1974
1975     device_ptr = init_d3d8();
1976     if (!device_ptr)
1977     {
1978         win_skip("Could not initialize direct3d\n");
1979         return;
1980     }
1981
1982     IDirect3DDevice8_GetDeviceCaps(device_ptr, &caps);
1983
1984     /* Check for the reliability of the returned data */
1985     hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
1986     if(FAILED(hr))
1987     {
1988         skip("Clear failed, can't assure correctness of the test results\n");
1989         goto cleanup;
1990     }
1991
1992     color = getPixelColor(device_ptr, 1, 1);
1993     if(color !=0x00ff0000)
1994     {
1995         skip("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests\n", color);
1996         goto cleanup;
1997     }
1998     IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
1999
2000     hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
2001     if(FAILED(hr))
2002     {
2003         skip("Clear failed, can't assure correctness of the test results\n");
2004         goto cleanup;
2005     }
2006
2007     color = getPixelColor(device_ptr, 639, 479);
2008     if(color != 0x0000ddee)
2009     {
2010         skip("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests\n", color);
2011         goto cleanup;
2012     }
2013     IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
2014
2015     /* Now run the real test */
2016     depth_clamp_test(device_ptr);
2017     lighting_test(device_ptr);
2018     clear_test(device_ptr);
2019     fog_test(device_ptr);
2020     present_test(device_ptr);
2021     offscreen_test(device_ptr);
2022     alpha_test(device_ptr);
2023
2024     if (caps.VertexShaderVersion >= D3DVS_VERSION(1, 1))
2025     {
2026         test_rcp_rsq(device_ptr);
2027     }
2028     else
2029     {
2030         skip("No vs.1.1 support\n");
2031     }
2032
2033     p8_texture_test(device_ptr);
2034     texop_test(device_ptr);
2035     depth_buffer_test(device_ptr);
2036     intz_test(device_ptr);
2037     shadow_test(device_ptr);
2038
2039 cleanup:
2040     if(device_ptr) {
2041         D3DDEVICE_CREATION_PARAMETERS creation_parameters;
2042         ULONG refcount;
2043
2044         IDirect3DDevice8_GetCreationParameters(device_ptr, &creation_parameters);
2045         DestroyWindow(creation_parameters.hFocusWindow);
2046         refcount = IDirect3DDevice8_Release(device_ptr);
2047         ok(!refcount, "Device has %u references left\n", refcount);
2048     }
2049 }