mshtml: Populate dynamic properties table in get_dynamic_data.
[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_D24S8;
144
145     hr = IDirect3D8_CreateDevice(d3d8_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
146             present_parameters.hDeviceWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
147     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL || broken(hr == D3DERR_NOTAVAILABLE), "IDirect3D_CreateDevice returned: %#08x\n", hr);
148
149     return device_ptr;
150 }
151
152 struct vertex
153 {
154     float x, y, z;
155     DWORD diffuse;
156 };
157
158 struct tvertex
159 {
160     float x, y, z, w;
161     DWORD diffuse;
162 };
163
164 struct nvertex
165 {
166     float x, y, z;
167     float nx, ny, nz;
168     DWORD diffuse;
169 };
170
171 static void lighting_test(IDirect3DDevice8 *device)
172 {
173     HRESULT hr;
174     DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
175     DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
176     DWORD color;
177
178     float mat[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
179                       0.0f, 1.0f, 0.0f, 0.0f,
180                       0.0f, 0.0f, 1.0f, 0.0f,
181                       0.0f, 0.0f, 0.0f, 1.0f };
182
183     struct vertex unlitquad[] =
184     {
185         {-1.0f, -1.0f,   0.1f,                          0xffff0000},
186         {-1.0f,  0.0f,   0.1f,                          0xffff0000},
187         { 0.0f,  0.0f,   0.1f,                          0xffff0000},
188         { 0.0f, -1.0f,   0.1f,                          0xffff0000},
189     };
190     struct vertex litquad[] =
191     {
192         {-1.0f,  0.0f,   0.1f,                          0xff00ff00},
193         {-1.0f,  1.0f,   0.1f,                          0xff00ff00},
194         { 0.0f,  1.0f,   0.1f,                          0xff00ff00},
195         { 0.0f,  0.0f,   0.1f,                          0xff00ff00},
196     };
197     struct nvertex unlitnquad[] =
198     {
199         { 0.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
200         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
201         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
202         { 1.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
203     };
204     struct nvertex litnquad[] =
205     {
206         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
207         { 0.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
208         { 1.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
209         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
210     };
211     WORD Indices[] = {0, 1, 2, 2, 3, 0};
212
213     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
214     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
215
216     /* Setup some states that may cause issues */
217     hr = IDirect3DDevice8_SetTransform(device, D3DTS_WORLDMATRIX(0), (D3DMATRIX *) mat);
218     ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %#08x\n", hr);
219     hr = IDirect3DDevice8_SetTransform(device, D3DTS_VIEW, (D3DMATRIX *)mat);
220     ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %#08x\n", hr);
221     hr = IDirect3DDevice8_SetTransform(device, D3DTS_PROJECTION, (D3DMATRIX *) mat);
222     ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %#08x\n", hr);
223     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CLIPPING, FALSE);
224     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
225     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, FALSE);
226     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
227     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
228     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
229     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_STENCILENABLE, FALSE);
230     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
231     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHATESTENABLE, FALSE);
232     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
233     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
234     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
235     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE);
236     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed with %#08x\n", hr);
237     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
238     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed with %#08x\n", hr);
239
240     hr = IDirect3DDevice8_SetVertexShader(device, fvf);
241     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
242
243     hr = IDirect3DDevice8_BeginScene(device);
244     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
245     if(hr == D3D_OK)
246     {
247         /* No lights are defined... That means, lit vertices should be entirely black */
248         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
249         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
250         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
251                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitquad, sizeof(unlitquad[0]));
252         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
253
254         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, TRUE);
255         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
256         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
257                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, litquad, sizeof(litquad[0]));
258         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
259
260         hr = IDirect3DDevice8_SetVertexShader(device, nfvf);
261         ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader failed with %#08x\n", hr);
262
263         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
264         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
265         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
266                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitnquad, sizeof(unlitnquad[0]));
267         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
268
269         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, TRUE);
270         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
271         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
272                                                     2 /*PrimCount */, Indices, D3DFMT_INDEX16, litnquad, sizeof(litnquad[0]));
273         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
274
275         IDirect3DDevice8_EndScene(device);
276         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
277     }
278
279     color = getPixelColor(device, 160, 360); /* Lower left quad - unlit without normals */
280     ok(color == 0x00ff0000, "Unlit quad without normals has color 0x%08x, expected 0x00ff0000.\n", color);
281     color = getPixelColor(device, 160, 120); /* Upper left quad - lit without normals */
282     ok(color == 0x00000000, "Lit quad without normals has color 0x%08x, expected 0x00000000.\n", color);
283     color = getPixelColor(device, 480, 360); /* Lower right quad - unlit with normals */
284     ok(color == 0x000000ff, "Unlit quad with normals has color 0x%08x, expected 0x000000ff.\n", color);
285     color = getPixelColor(device, 480, 120); /* Upper right quad - lit with normals */
286     ok(color == 0x00000000, "Lit quad with normals has color 0x%08x, expected 0x00000000.\n", color);
287
288     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
289
290     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
291     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
292 }
293
294 static void clear_test(IDirect3DDevice8 *device)
295 {
296     /* Tests the correctness of clearing parameters */
297     HRESULT hr;
298     D3DRECT rect[2];
299     D3DRECT rect_negneg;
300     DWORD color;
301
302     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
303     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
304
305     /* Positive x, negative y */
306     rect[0].x1 = 0;
307     rect[0].y1 = 480;
308     rect[0].x2 = 320;
309     rect[0].y2 = 240;
310
311     /* Positive x, positive y */
312     rect[1].x1 = 0;
313     rect[1].y1 = 0;
314     rect[1].x2 = 320;
315     rect[1].y2 = 240;
316     /* Clear 2 rectangles with one call. Shows that a positive value is returned, but the negative rectangle
317      * is ignored, the positive is still cleared afterwards
318      */
319     hr = IDirect3DDevice8_Clear(device, 2, rect, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
320     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
321
322     /* negative x, negative y */
323     rect_negneg.x1 = 640;
324     rect_negneg.y1 = 240;
325     rect_negneg.x2 = 320;
326     rect_negneg.y2 = 0;
327     hr = IDirect3DDevice8_Clear(device, 1, &rect_negneg, D3DCLEAR_TARGET, 0xff00ff00, 0.0, 0);
328     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
329
330     color = getPixelColor(device, 160, 360); /* lower left quad */
331     ok(color == 0x00ffffff, "Clear rectangle 3(pos, neg) has color %08x\n", color);
332     color = getPixelColor(device, 160, 120); /* upper left quad */
333     ok(color == 0x00ff0000, "Clear rectangle 1(pos, pos) has color %08x\n", color);
334     color = getPixelColor(device, 480, 360); /* lower right quad  */
335     ok(color == 0x00ffffff, "Clear rectangle 4(NULL) has color %08x\n", color);
336     color = getPixelColor(device, 480, 120); /* upper right quad */
337     ok(color == 0x00ffffff, "Clear rectangle 4(neg, neg) has color %08x\n", color);
338
339     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
340 }
341
342 struct sVertex {
343     float x, y, z;
344     DWORD diffuse;
345     DWORD specular;
346 };
347
348 struct sVertexT {
349     float x, y, z, rhw;
350     DWORD diffuse;
351     DWORD specular;
352 };
353
354 static void fog_test(IDirect3DDevice8 *device)
355 {
356     HRESULT hr;
357     DWORD color;
358     float start = 0.0, end = 1.0;
359
360     /* Gets full z based fog with linear fog, no fog with specular color */
361     struct sVertex untransformed_1[] = {
362         {-1,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
363         {-1,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
364         { 0,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
365         { 0,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
366     };
367     /* Ok, I am too lazy to deal with transform matrices */
368     struct sVertex untransformed_2[] = {
369         {-1,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
370         {-1,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
371         { 0,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
372         { 0,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
373     };
374     /* Untransformed ones. Give them a different diffuse color to make the test look
375      * nicer. It also makes making sure that they are drawn correctly easier.
376      */
377     struct sVertexT transformed_1[] = {
378         {320,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
379         {640,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
380         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
381         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
382     };
383     struct sVertexT transformed_2[] = {
384         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
385         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
386         {640,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
387         {320,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
388     };
389     WORD Indices[] = {0, 1, 2, 2, 3, 0};
390
391     D3DCAPS8 caps;
392     float ident_mat[16] =
393     {
394         1.0f, 0.0f, 0.0f, 0.0f,
395         0.0f, 1.0f, 0.0f, 0.0f,
396         0.0f, 0.0f, 1.0f, 0.0f,
397         0.0f, 0.0f, 0.0f, 1.0f
398     };
399     float world_mat1[16] =
400     {
401         1.0f, 0.0f,  0.0f, 0.0f,
402         0.0f, 1.0f,  0.0f, 0.0f,
403         0.0f, 0.0f,  1.0f, 0.0f,
404         0.0f, 0.0f, -0.5f, 1.0f
405     };
406     float world_mat2[16] =
407     {
408         1.0f, 0.0f, 0.0f, 0.0f,
409         0.0f, 1.0f, 0.0f, 0.0f,
410         0.0f, 0.0f, 1.0f, 0.0f,
411         0.0f, 0.0f, 1.0f, 1.0f
412     };
413     float proj_mat[16] =
414     {
415         1.0f, 0.0f,  0.0f, 0.0f,
416         0.0f, 1.0f,  0.0f, 0.0f,
417         0.0f, 0.0f,  1.0f, 0.0f,
418         0.0f, 0.0f, -1.0f, 1.0f
419     };
420
421     struct sVertex far_quad1[] =
422     {
423         {-1.0f, -1.0f, 0.5f, 0xffff0000, 0xff000000},
424         {-1.0f,  0.0f, 0.5f, 0xffff0000, 0xff000000},
425         { 0.0f,  0.0f, 0.5f, 0xffff0000, 0xff000000},
426         { 0.0f, -1.0f, 0.5f, 0xffff0000, 0xff000000},
427     };
428     struct sVertex far_quad2[] =
429     {
430         {-1.0f, 0.0f, 1.5f, 0xffff0000, 0xff000000},
431         {-1.0f, 1.0f, 1.5f, 0xffff0000, 0xff000000},
432         { 0.0f, 1.0f, 1.5f, 0xffff0000, 0xff000000},
433         { 0.0f, 0.0f, 1.5f, 0xffff0000, 0xff000000},
434     };
435
436     memset(&caps, 0, sizeof(caps));
437     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
438     ok(hr == D3D_OK, "IDirect3DDevice8_GetDeviceCaps returned %08x\n", hr);
439
440     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
441     ok(hr == D3D_OK, "IDirect3DDevice8_Clear returned %#08x\n", hr);
442
443     /* Setup initial states: No lighting, fog on, fog color */
444     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
445     ok(hr == D3D_OK, "Turning off lighting returned %#08x\n", hr);
446     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, TRUE);
447     ok(hr == D3D_OK, "Turning on fog calculations returned %#08x\n", hr);
448     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGCOLOR, 0xFF00FF00 /* A nice green */);
449     ok(hr == D3D_OK, "Setting fog color returned %#08x\n", hr);
450
451     /* First test: Both table fog and vertex fog off */
452     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_NONE);
453     ok(hr == D3D_OK, "Turning off table fog returned %#08x\n", hr);
454     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
455     ok(hr == D3D_OK, "Turning off vertex fog returned %#08x\n", hr);
456
457     /* Start = 0, end = 1. Should be default, but set them */
458     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGSTART, *((DWORD *) &start));
459     ok(hr == D3D_OK, "Setting fog start returned %#08x\n", hr);
460     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGEND, *((DWORD *) &end));
461     ok(hr == D3D_OK, "Setting fog start returned %#08x\n", hr);
462
463     if(IDirect3DDevice8_BeginScene(device) == D3D_OK)
464     {
465         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
466         ok( hr == D3D_OK, "SetVertexShader returned %#08x\n", hr);
467         /* Untransformed, vertex fog = NONE, table fog = NONE: Read the fog weighting from the specular color */
468         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
469                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, untransformed_1,
470                                                      sizeof(untransformed_1[0]));
471         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
472
473         /* That makes it use the Z value */
474         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);
475         ok(hr == D3D_OK, "Setting fog vertex mode to D3DFOG_LINEAR returned %#08x\n", hr);
476         /* Untransformed, vertex fog != none (or table fog != none):
477          * Use the Z value as input into the equation
478          */
479         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
480                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, untransformed_2,
481                                                      sizeof(untransformed_2[0]));
482         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
483
484         /* transformed verts */
485         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
486         ok( hr == D3D_OK, "SetVertexShader returned %#08x\n", hr);
487         /* Transformed, vertex fog != NONE, pixel fog == NONE: Use specular color alpha component */
488         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
489                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_1,
490                                                      sizeof(transformed_1[0]));
491         ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
492
493         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
494         ok( hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR returned %#08x\n", hr);
495         /* Transformed, table fog != none, vertex anything: Use Z value as input to the fog
496          * equation
497          */
498         hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
499                                                      2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_2,
500                                                      sizeof(transformed_2[0]));
501         ok(SUCCEEDED(hr), "IDirect3DDevice8_DrawIndexedPrimitiveUP returned %#x.\n", hr);
502
503         hr = IDirect3DDevice8_EndScene(device);
504         ok(hr == D3D_OK, "EndScene returned %#08x\n", hr);
505     }
506     else
507     {
508         ok(FALSE, "BeginScene failed\n");
509     }
510
511     color = getPixelColor(device, 160, 360);
512     ok(color == 0x00FF0000, "Untransformed vertex with no table or vertex fog has color %08x\n", color);
513     color = getPixelColor(device, 160, 120);
514     ok(color == 0x0000FF00, "Untransformed vertex with linear vertex fog has color %08x\n", color);
515     color = getPixelColor(device, 480, 120);
516     ok(color == 0x00FFFF00, "Transformed vertex with linear vertex fog has color %08x\n", color);
517     color = getPixelColor(device, 480, 360);
518     ok(color == 0x0000FF00, "Transformed vertex with linear table fog has color %08x\n", color);
519
520     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
521
522     if (caps.RasterCaps & D3DPRASTERCAPS_FOGTABLE)
523     {
524         /* A simple fog + non-identity world matrix test */
525         hr = IDirect3DDevice8_SetTransform(device, D3DTS_WORLDMATRIX(0), (D3DMATRIX *) world_mat1);
526         ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %#08x\n", hr);
527
528         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
529         ok(hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR returned %#08x\n", hr);
530         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
531         ok(hr == D3D_OK, "Turning off vertex fog returned %#08x\n", hr);
532
533         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
534         ok(hr == D3D_OK, "IDirect3DDevice8_Clear returned %#08x\n", hr);
535
536         if (IDirect3DDevice8_BeginScene(device) == D3D_OK)
537         {
538             hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
539             ok(hr == D3D_OK, "SetVertexShader returned %#08x\n", hr);
540
541             hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0, 4,
542                     2, Indices, D3DFMT_INDEX16, far_quad1, sizeof(far_quad1[0]));
543             ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
544
545             hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0, 4,
546                     2, Indices, D3DFMT_INDEX16, far_quad2, sizeof(far_quad2[0]));
547             ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
548
549             hr = IDirect3DDevice8_EndScene(device);
550             ok(hr == D3D_OK, "EndScene returned %#08x\n", hr);
551         }
552         else
553         {
554             ok(FALSE, "BeginScene failed\n");
555         }
556
557         color = getPixelColor(device, 160, 360);
558         ok(color_match(color, 0x00ff0000, 4), "Unfogged quad has color %08x\n", color);
559         color = getPixelColor(device, 160, 120);
560         ok(color == 0x0000ff00, "Fogged out quad has color %08x\n", color);
561
562         IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
563
564         /* Test fog behavior with an orthogonal (but not identity) projection matrix */
565         hr = IDirect3DDevice8_SetTransform(device, D3DTS_WORLDMATRIX(0), (D3DMATRIX *) world_mat2);
566         ok(hr == D3D_OK, "SetTransform returned %#08x\n", hr);
567         hr = IDirect3DDevice8_SetTransform(device, D3DTS_PROJECTION, (D3DMATRIX *) proj_mat);
568         ok(hr == D3D_OK, "SetTransform returned %#08x\n", hr);
569
570         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
571         ok(hr == D3D_OK, "Clear returned %#08x\n", hr);
572
573         if (IDirect3DDevice8_BeginScene(device) == D3D_OK)
574         {
575             hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
576             ok(hr == D3D_OK, "SetVertexShader returned %#08x\n", hr);
577
578             hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0, 4,
579                     2, Indices, D3DFMT_INDEX16, untransformed_1, sizeof(untransformed_1[0]));
580             ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
581
582             hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0, 4,
583                     2, Indices, D3DFMT_INDEX16, untransformed_2, sizeof(untransformed_2[0]));
584             ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
585
586             hr = IDirect3DDevice8_EndScene(device);
587             ok(hr == D3D_OK, "EndScene returned %#08x\n", hr);
588         }
589         else
590         {
591             ok(FALSE, "BeginScene failed\n");
592         }
593
594         color = getPixelColor(device, 160, 360);
595         todo_wine ok(color_match(color, 0x00e51900, 4), "Partially fogged quad has color %08x\n", color);
596         color = getPixelColor(device, 160, 120);
597         ok(color == 0x0000ff00, "Fogged out quad has color %08x\n", color);
598
599         IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
600
601         hr = IDirect3DDevice8_SetTransform(device, D3DTS_WORLDMATRIX(0), (D3DMATRIX *) ident_mat);
602         ok(hr == D3D_OK, "SetTransform returned %#08x\n", hr);
603         hr = IDirect3DDevice8_SetTransform(device, D3DTS_PROJECTION, (D3DMATRIX *) ident_mat);
604         ok(hr == D3D_OK, "SetTransform returned %#08x\n", hr);
605     }
606     else
607     {
608         skip("D3DPRASTERCAPS_FOGTABLE not supported, skipping some fog tests\n");
609     }
610
611     /* Turn off the fog master switch to avoid confusing other tests */
612     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
613     ok(hr == D3D_OK, "Turning off fog calculations returned %#08x\n", hr);
614 }
615
616 static void present_test(IDirect3DDevice8 *device)
617 {
618     struct vertex quad[] =
619     {
620         {-1.0f, -1.0f,   0.9f,                          0xffff0000},
621         {-1.0f,  1.0f,   0.9f,                          0xffff0000},
622         { 1.0f, -1.0f,   0.1f,                          0xffff0000},
623         { 1.0f,  1.0f,   0.1f,                          0xffff0000},
624     };
625     HRESULT hr;
626     DWORD color;
627
628     /* Does the Present clear the depth stencil? Clear the depth buffer with some value != 0,
629     * then call Present. Then clear the color buffer to make sure it has some defined content
630     * after the Present with D3DSWAPEFFECT_DISCARD. After that draw a plane that is somewhere cut
631     * by the depth value.
632     */
633     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 0.75, 0);
634     ok(hr == D3D_OK, "IDirect3DDevice8_Clear returned %08x\n", hr);
635     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
636     ok(SUCCEEDED(hr), "IDirect3DDevice8_Present returned %#x.\n", hr);
637     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.4f, 0);
638     ok(SUCCEEDED(hr), "IDirect3DDevice8_Clear returned %#x.\n", hr);
639
640     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
641     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %08x\n", hr);
642     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_GREATER);
643     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %08x\n", hr);
644     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
645     ok(hr == D3D_OK, "IDirect3DDevice8_SetFVF returned %08x\n", hr);
646
647     hr = IDirect3DDevice8_BeginScene(device);
648     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %08x\n", hr);
649     if(hr == D3D_OK)
650     {
651         /* No lights are defined... That means, lit vertices should be entirely black */
652         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2 /*PrimCount */, quad, sizeof(quad[0]));
653         ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %08x\n", hr);
654
655         hr = IDirect3DDevice8_EndScene(device);
656         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %08x\n", hr);
657     }
658
659     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_FALSE);
660     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %08x\n", hr);
661
662     color = getPixelColor(device, 512, 240);
663     ok(color == 0x00ffffff, "Present failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
664     color = getPixelColor(device, 64, 240);
665     ok(color == 0x00ff0000, "Present failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
666
667     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
668     ok(SUCCEEDED(hr), "Present failed (%#08x)\n", hr);
669 }
670
671 static void test_rcp_rsq(IDirect3DDevice8 *device)
672 {
673     HRESULT hr;
674     DWORD shader;
675     DWORD color;
676     float constant[4] = {1.0, 1.0, 1.0, 2.0};
677
678     static const float quad[][3] = {
679         {-1.0f, -1.0f, 0.0f},
680         {-1.0f,  1.0f, 0.0f},
681         { 1.0f, -1.0f, 0.0f},
682         { 1.0f,  1.0f, 0.0f},
683     };
684
685     const DWORD rcp_test[] = {
686         0xfffe0101,                                         /* vs.1.1 */
687
688         0x0009fffe, 0x30303030, 0x30303030,                 /* Shaders have to have a minimal size. */
689         0x30303030, 0x30303030, 0x30303030,                 /* Add a filler comment. Usually D3DX8's*/
690         0x30303030, 0x30303030, 0x30303030,                 /* version comment makes the shader big */
691         0x00303030,                                         /* enough to make windows happy         */
692
693         0x00000001, 0xc00f0000, 0x90e40000,                 /* mov oPos, v0 */
694         0x00000006, 0xd00f0000, 0xa0e40000,                 /* rcp oD0, c0 */
695         0x0000ffff                                          /* END */
696     };
697
698     const DWORD rsq_test[] = {
699         0xfffe0101,                                         /* vs.1.1 */
700
701         0x0009fffe, 0x30303030, 0x30303030,                 /* Shaders have to have a minimal size. */
702         0x30303030, 0x30303030, 0x30303030,                 /* Add a filler comment. Usually D3DX8's*/
703         0x30303030, 0x30303030, 0x30303030,                 /* version comment makes the shader big */
704         0x00303030,                                         /* enough to make windows happy         */
705
706         0x00000001, 0xc00f0000, 0x90e40000,                 /* mov oPos, v0 */
707         0x00000007, 0xd00f0000, 0xa0e40000,                 /* rsq oD0, c0 */
708         0x0000ffff                                          /* END */
709     };
710
711     DWORD decl[] =
712     {
713         D3DVSD_STREAM(0),
714         D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),  /* D3DVSDE_POSITION, Register v0 */
715         D3DVSD_END()
716     };
717
718     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff336699, 0.0f, 0);
719     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
720
721     hr = IDirect3DDevice8_CreateVertexShader(device, decl, rcp_test, &shader, 0);
722     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned with %#08x\n", hr);
723
724     IDirect3DDevice8_SetVertexShader(device, shader);
725     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
726     IDirect3DDevice8_SetVertexShaderConstant(device, 0, constant, 1);
727
728     hr = IDirect3DDevice8_BeginScene(device);
729     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
730     if(SUCCEEDED(hr))
731     {
732         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
733         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%#08x)\n", hr);
734         hr = IDirect3DDevice8_EndScene(device);
735         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
736     }
737
738     color = getPixelColor(device, 320, 240);
739     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0x80, 0x80, 0x80), 4),
740             "RCP test returned color 0x%08x, expected 0x00808080.\n", color);
741
742     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
743     ok(SUCCEEDED(hr), "Present failed (%#08x)\n", hr);
744
745     IDirect3DDevice8_SetVertexShader(device, 0);
746     IDirect3DDevice8_DeleteVertexShader(device, shader);
747
748     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff996633, 0.0f, 0);
749     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
750
751     hr = IDirect3DDevice8_CreateVertexShader(device, decl, rsq_test, &shader, 0);
752     ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned with %#08x\n", hr);
753
754     IDirect3DDevice8_SetVertexShader(device, shader);
755     ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
756     IDirect3DDevice8_SetVertexShaderConstant(device, 0, constant, 1);
757
758     hr = IDirect3DDevice8_BeginScene(device);
759     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
760     if(SUCCEEDED(hr))
761     {
762         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
763         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%#08x)\n", hr);
764         hr = IDirect3DDevice8_EndScene(device);
765         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
766     }
767
768     color = getPixelColor(device, 320, 240);
769     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0xb4, 0xb4, 0xb4), 4),
770             "RSQ test returned color 0x%08x, expected 0x00b4b4b4.\n", color);
771
772     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
773     ok(SUCCEEDED(hr), "Present failed (%#08x)\n", hr);
774
775     IDirect3DDevice8_SetVertexShader(device, 0);
776     IDirect3DDevice8_DeleteVertexShader(device, shader);
777 }
778
779 static void offscreen_test(IDirect3DDevice8 *device)
780 {
781     HRESULT hr;
782     IDirect3DTexture8 *offscreenTexture = NULL;
783     IDirect3DSurface8 *backbuffer = NULL, *offscreen = NULL, *depthstencil = NULL;
784     DWORD color;
785
786     static const float quad[][5] = {
787         {-0.5f, -0.5f, 0.1f, 0.0f, 0.0f},
788         {-0.5f,  0.5f, 0.1f, 0.0f, 1.0f},
789         { 0.5f, -0.5f, 0.1f, 1.0f, 0.0f},
790         { 0.5f,  0.5f, 0.1f, 1.0f, 1.0f},
791     };
792
793     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &depthstencil);
794     ok(hr == D3D_OK, "IDirect3DDevice8_GetDepthStencilSurface failed, hr = %#08x\n", hr);
795
796     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
797     ok(hr == D3D_OK, "Clear failed, hr = %#08x\n", hr);
798
799     hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &offscreenTexture);
800     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
801     if(!offscreenTexture) {
802         trace("Failed to create an X8R8G8B8 offscreen texture, trying R5G6B5\n");
803         hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &offscreenTexture);
804         ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
805         if(!offscreenTexture) {
806             skip("Cannot create an offscreen render target\n");
807             goto out;
808         }
809     }
810
811     hr = IDirect3DDevice8_GetBackBuffer(device, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
812     ok(hr == D3D_OK, "Can't get back buffer, hr = %#08x\n", hr);
813     if(!backbuffer) {
814         goto out;
815     }
816
817     hr = IDirect3DTexture8_GetSurfaceLevel(offscreenTexture, 0, &offscreen);
818     ok(hr == D3D_OK, "Can't get offscreen surface, hr = %#08x\n", hr);
819     if(!offscreen) {
820         goto out;
821     }
822
823     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
824     ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
825
826     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
827     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
828     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
829     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
830     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_NONE);
831     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (%#08x)\n", hr);
832     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_NONE);
833     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (%#08x)\n", hr);
834     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
835     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %08x\n", hr);
836
837     if(IDirect3DDevice8_BeginScene(device) == D3D_OK) {
838         hr = IDirect3DDevice8_SetRenderTarget(device, offscreen, depthstencil);
839         ok(hr == D3D_OK, "SetRenderTarget failed, hr = %#08x\n", hr);
840         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
841         ok(hr == D3D_OK, "Clear failed, hr = %#08x\n", hr);
842
843         /* Draw without textures - Should result in a white quad */
844         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(quad[0]));
845         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
846
847         hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
848         ok(hr == D3D_OK, "SetRenderTarget failed, hr = %#08x\n", hr);
849         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) offscreenTexture);
850         ok(hr == D3D_OK, "SetTexture failed, %08x\n", hr);
851
852         /* This time with the texture */
853         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(quad[0]));
854         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
855
856         IDirect3DDevice8_EndScene(device);
857     }
858
859     /* Center quad - should be white */
860     color = getPixelColor(device, 320, 240);
861     ok(color == 0x00ffffff, "Offscreen failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
862     /* Some quad in the cleared part of the texture */
863     color = getPixelColor(device, 170, 240);
864     ok(color == 0x00ff00ff, "Offscreen failed: Got color 0x%08x, expected 0x00ff00ff.\n", color);
865     /* Part of the originally cleared back buffer */
866     color = getPixelColor(device, 10, 10);
867     ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
868     if(0) {
869         /* Lower left corner of the screen, where back buffer offscreen rendering draws the offscreen texture.
870         * It should be red, but the offscreen texture may leave some junk there. Not tested yet. Depending on
871         * the offscreen rendering mode this test would succeed or fail
872         */
873         color = getPixelColor(device, 10, 470);
874         ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
875     }
876
877     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
878
879 out:
880     hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
881     ok(SUCCEEDED(hr), "IDirect3DDevice8_SetTexture returned %#x.\n", hr);
882
883     /* restore things */
884     if(backbuffer) {
885         hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
886         ok(SUCCEEDED(hr), "IDirect3DDevice8_SetRenderTarget returned %#x.\n", hr);
887         IDirect3DSurface8_Release(backbuffer);
888     }
889     if(offscreenTexture) {
890         IDirect3DTexture8_Release(offscreenTexture);
891     }
892     if(offscreen) {
893         IDirect3DSurface8_Release(offscreen);
894     }
895     if(depthstencil) {
896         IDirect3DSurface8_Release(depthstencil);
897     }
898 }
899
900 static void alpha_test(IDirect3DDevice8 *device)
901 {
902     HRESULT hr;
903     IDirect3DTexture8 *offscreenTexture;
904     IDirect3DSurface8 *backbuffer = NULL, *offscreen = NULL, *depthstencil = NULL;
905     DWORD color;
906
907     struct vertex quad1[] =
908     {
909         {-1.0f, -1.0f,   0.1f,                          0x4000ff00},
910         {-1.0f,  0.0f,   0.1f,                          0x4000ff00},
911         { 1.0f, -1.0f,   0.1f,                          0x4000ff00},
912         { 1.0f,  0.0f,   0.1f,                          0x4000ff00},
913     };
914     struct vertex quad2[] =
915     {
916         {-1.0f,  0.0f,   0.1f,                          0xc00000ff},
917         {-1.0f,  1.0f,   0.1f,                          0xc00000ff},
918         { 1.0f,  0.0f,   0.1f,                          0xc00000ff},
919         { 1.0f,  1.0f,   0.1f,                          0xc00000ff},
920     };
921     static const float composite_quad[][5] = {
922         { 0.0f, -1.0f, 0.1f, 0.0f, 1.0f},
923         { 0.0f,  1.0f, 0.1f, 0.0f, 0.0f},
924         { 1.0f, -1.0f, 0.1f, 1.0f, 1.0f},
925         { 1.0f,  1.0f, 0.1f, 1.0f, 0.0f},
926     };
927
928     /* Clear the render target with alpha = 0.5 */
929     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
930     ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
931
932     hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &offscreenTexture);
933     ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
934
935     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &depthstencil);
936     ok(hr == D3D_OK, "IDirect3DDevice8_GetDepthStencilSurface failed, hr = %#08x\n", hr);
937
938     hr = IDirect3DDevice8_GetBackBuffer(device, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
939     ok(hr == D3D_OK, "Can't get back buffer, hr = %#08x\n", hr);
940     if(!backbuffer) {
941         goto out;
942     }
943     hr = IDirect3DTexture8_GetSurfaceLevel(offscreenTexture, 0, &offscreen);
944     ok(hr == D3D_OK, "Can't get offscreen surface, hr = %#08x\n", hr);
945     if(!offscreen) {
946         goto out;
947     }
948
949     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
950     ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
951
952     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
953     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
954     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
955     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
956     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_NONE);
957     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (%#08x)\n", hr);
958     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_NONE);
959     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (%#08x)\n", hr);
960     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
961     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %08x\n", hr);
962
963     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
964     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
965     if(IDirect3DDevice8_BeginScene(device) == D3D_OK) {
966
967         /* Draw two quads, one with src alpha blending, one with dest alpha blending. */
968         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
969         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
970         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
971         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
972         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(quad1[0]));
973         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
974
975         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_DESTALPHA);
976         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
977         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVDESTALPHA);
978         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
979         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(quad2[0]));
980         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
981
982         /* Switch to the offscreen buffer, and redo the testing. The offscreen render target
983          * doesn't have an alpha channel. DESTALPHA and INVDESTALPHA "don't work" on render
984          * targets without alpha channel, they give essentially ZERO and ONE blend factors. */
985         hr = IDirect3DDevice8_SetRenderTarget(device, offscreen, 0);
986         ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
987         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
988         ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
989
990         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
991         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
992         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
993         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
994         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(quad1[0]));
995         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
996
997         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_DESTALPHA);
998         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
999         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVDESTALPHA);
1000         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1001         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(quad2[0]));
1002         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
1003
1004         hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
1005         ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
1006
1007         /* Render the offscreen texture onto the frame buffer to be able to compare it regularly.
1008          * Disable alpha blending for the final composition
1009          */
1010         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
1011         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1012         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
1013         ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
1014
1015         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) offscreenTexture);
1016         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1017         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, composite_quad, sizeof(float) * 5);
1018         ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
1019         hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
1020         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1021
1022         hr = IDirect3DDevice8_EndScene(device);
1023         ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed, hr = %08x\n", hr);
1024     }
1025
1026     color = getPixelColor(device, 160, 360);
1027     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0xbf, 0x40, 0x00), 1),
1028        "SRCALPHA on frame buffer returned color %08x, expected 0x00bf4000\n", color);
1029
1030     color = getPixelColor(device, 160, 120);
1031     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0x7f, 0x00, 0x80), 2),
1032        "DSTALPHA on frame buffer returned color %08x, expected 0x007f0080\n", color);
1033
1034     color = getPixelColor(device, 480, 360);
1035     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0xbf, 0x40, 0x00), 1),
1036        "SRCALPHA on texture returned color %08x, expected 0x00bf4000\n", color);
1037
1038     color = getPixelColor(device, 480, 120);
1039     ok(color_match(color, D3DCOLOR_ARGB(0x00, 0x00, 0x00, 0xff), 1),
1040        "DSTALPHA on texture returned color %08x, expected 0x000000ff\n", color);
1041
1042     IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1043
1044     out:
1045     /* restore things */
1046     if(backbuffer) {
1047         IDirect3DSurface8_Release(backbuffer);
1048     }
1049     if(offscreenTexture) {
1050         IDirect3DTexture8_Release(offscreenTexture);
1051     }
1052     if(offscreen) {
1053         IDirect3DSurface8_Release(offscreen);
1054     }
1055     if(depthstencil) {
1056         IDirect3DSurface8_Release(depthstencil);
1057     }
1058 }
1059
1060 static void p8_texture_test(IDirect3DDevice8 *device)
1061 {
1062     IDirect3D8 *d3d = NULL;
1063     HRESULT hr;
1064     IDirect3DTexture8 *texture = NULL, *texture2 = NULL;
1065     D3DLOCKED_RECT lr;
1066     unsigned char *data;
1067     DWORD color, red, green, blue;
1068     PALETTEENTRY table[256];
1069     D3DCAPS8 caps;
1070     UINT i;
1071     float quad[] = {
1072        -1.0f,      0.0f,    0.1f,    0.0f,   0.0f,
1073        -1.0f,      1.0f,    0.1f,    0.0f,   1.0f,
1074         1.0f,      0.0f,    0.1f,    1.0f,   0.0f,
1075         1.0f,      1.0f,    0.1f,    1.0f,   1.0f,
1076     };
1077     float quad2[] = {
1078        -1.0f,      -1.0f,   0.1f,    0.0f,   0.0f,
1079        -1.0f,      0.0f,    0.1f,    0.0f,   1.0f,
1080         1.0f,      -1.0f,   0.1f,    1.0f,   0.0f,
1081         1.0f,      0.0f,    0.1f,    1.0f,   1.0f,
1082     };
1083
1084     IDirect3DDevice8_GetDirect3D(device, &d3d);
1085
1086     if(IDirect3D8_CheckDeviceFormat(d3d, 0, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, 0,
1087        D3DRTYPE_TEXTURE, D3DFMT_P8) != D3D_OK) {
1088            skip("D3DFMT_P8 textures not supported\n");
1089            goto out;
1090     }
1091
1092     hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_P8,
1093                                         D3DPOOL_MANAGED, &texture2);
1094     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed, hr = %08x\n", hr);
1095     if(!texture2) {
1096         skip("Failed to create D3DFMT_P8 texture\n");
1097         goto out;
1098     }
1099
1100     memset(&lr, 0, sizeof(lr));
1101     hr = IDirect3DTexture8_LockRect(texture2, 0, &lr, NULL, 0);
1102     ok(hr == D3D_OK, "IDirect3DTexture8_LockRect failed, hr = %08x\n", hr);
1103     data = lr.pBits;
1104     *data = 1;
1105
1106     hr = IDirect3DTexture8_UnlockRect(texture2, 0);
1107     ok(hr == D3D_OK, "IDirect3DTexture8_UnlockRect failed, hr = %08x\n", hr);
1108
1109     hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_P8,
1110                                         D3DPOOL_MANAGED, &texture);
1111     ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed, hr = %08x\n", hr);
1112     if(!texture) {
1113         skip("Failed to create D3DFMT_P8 texture\n");
1114         goto out;
1115     }
1116
1117     memset(&lr, 0, sizeof(lr));
1118     hr = IDirect3DTexture8_LockRect(texture, 0, &lr, NULL, 0);
1119     ok(hr == D3D_OK, "IDirect3DTexture8_LockRect failed, hr = %08x\n", hr);
1120     data = lr.pBits;
1121     *data = 1;
1122
1123     hr = IDirect3DTexture8_UnlockRect(texture, 0);
1124     ok(hr == D3D_OK, "IDirect3DTexture8_UnlockRect failed, hr = %08x\n", hr);
1125
1126     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
1127     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
1128
1129     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
1130     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1131
1132     /* The first part of the test should work both with and without D3DPTEXTURECAPS_ALPHAPALETTE;
1133        alpha of every entry is set to 1.0, which MS says is required when there's no
1134        D3DPTEXTURECAPS_ALPHAPALETTE capability */
1135     for (i = 0; i < 256; i++) {
1136         table[i].peRed = table[i].peGreen = table[i].peBlue = 0;
1137         table[i].peFlags = 0xff;
1138     }
1139     table[1].peRed = 0xff;
1140     hr = IDirect3DDevice8_SetPaletteEntries(device, 0, table);
1141     ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1142
1143     table[1].peRed = 0;
1144     table[1].peBlue = 0xff;
1145     hr = IDirect3DDevice8_SetPaletteEntries(device, 1, table);
1146     ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1147
1148     hr = IDirect3DDevice8_BeginScene(device);
1149     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1150     if(SUCCEEDED(hr)) {
1151         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
1152         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1153         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
1154         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1155
1156         hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
1157         ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
1158
1159         hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 0);
1160         ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1161
1162         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture2);
1163         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1164         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1165         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1166
1167         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture);
1168         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1169         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1170         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1171
1172         hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 1);
1173         ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1174         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, 5 * sizeof(float));
1175         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1176
1177         hr = IDirect3DDevice8_EndScene(device);
1178         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1179     }
1180
1181     color = getPixelColor(device, 32, 32);
1182     red   = (color & 0x00ff0000) >> 16;
1183     green = (color & 0x0000ff00) >>  8;
1184     blue  = (color & 0x000000ff) >>  0;
1185     ok(red == 0xff && blue == 0 && green == 0,
1186        "got color %08x, expected 0x00ff0000\n", color);
1187
1188     color = getPixelColor(device, 32, 320);
1189     red   = (color & 0x00ff0000) >> 16;
1190     green = (color & 0x0000ff00) >>  8;
1191     blue  = (color & 0x000000ff) >>  0;
1192     ok(red == 0 && blue == 0xff && green == 0,
1193     "got color %08x, expected 0x000000ff\n", color);
1194
1195     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1196     ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1197
1198     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
1199     ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
1200
1201     hr = IDirect3DDevice8_BeginScene(device);
1202     ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1203     if(SUCCEEDED(hr)) {
1204         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture2);
1205         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1206
1207         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1208         ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1209
1210         hr = IDirect3DDevice8_EndScene(device);
1211         ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1212     }
1213
1214
1215     color = getPixelColor(device, 32, 32);
1216     red   = (color & 0x00ff0000) >> 16;
1217     green = (color & 0x0000ff00) >>  8;
1218     blue  = (color & 0x000000ff) >>  0;
1219     ok(red == 0 && blue == 0xff && green == 0,
1220     "got color %08x, expected 0x000000ff\n", color);
1221
1222     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1223     ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1224
1225     /* Test palettes with alpha */
1226     IDirect3DDevice8_GetDeviceCaps(device, &caps);
1227     if (!(caps.TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)) {
1228         skip("no D3DPTEXTURECAPS_ALPHAPALETTE capability, tests with alpha in palette will be skipped\n");
1229     } else {
1230         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
1231         ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
1232
1233         hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
1234         ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1235
1236         for (i = 0; i < 256; i++) {
1237             table[i].peRed = table[i].peGreen = table[i].peBlue = 0;
1238             table[i].peFlags = 0xff;
1239         }
1240         table[1].peRed = 0xff;
1241         table[1].peFlags = 0x80;
1242         hr = IDirect3DDevice8_SetPaletteEntries(device, 0, table);
1243         ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1244
1245         table[1].peRed = 0;
1246         table[1].peBlue = 0xff;
1247         table[1].peFlags = 0x80;
1248         hr = IDirect3DDevice8_SetPaletteEntries(device, 1, table);
1249         ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1250
1251         hr = IDirect3DDevice8_BeginScene(device);
1252         ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1253         if(SUCCEEDED(hr)) {
1254             hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
1255             ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1256             hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
1257             ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1258
1259             hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
1260             ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
1261
1262             hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 0);
1263             ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1264
1265             hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1266             ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1267
1268             hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 1);
1269             ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1270
1271             hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, 5 * sizeof(float));
1272             ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1273
1274             hr = IDirect3DDevice8_EndScene(device);
1275             ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1276         }
1277
1278         color = getPixelColor(device, 32, 32);
1279         red   = (color & 0x00ff0000) >> 16;
1280         green = (color & 0x0000ff00) >>  8;
1281         blue  = (color & 0x000000ff) >>  0;
1282         ok(red >= 0x7e && red <= 0x81 && blue == 0 && green == 0,
1283         "got color %08x, expected 0x00800000 or near\n", color);
1284
1285         color = getPixelColor(device, 32, 320);
1286         red   = (color & 0x00ff0000) >> 16;
1287         green = (color & 0x0000ff00) >>  8;
1288         blue  = (color & 0x000000ff) >>  0;
1289         ok(red == 0 && blue >= 0x7e && blue <= 0x81 && green == 0,
1290         "got color %08x, expected 0x00000080 or near\n", color);
1291
1292         hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1293         ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1294     }
1295
1296     hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
1297     ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1298     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
1299     ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1300
1301 out:
1302     if(texture) IDirect3DTexture8_Release(texture);
1303     if(texture2) IDirect3DTexture8_Release(texture2);
1304     IDirect3D8_Release(d3d);
1305 }
1306
1307 static void texop_test(IDirect3DDevice8 *device)
1308 {
1309     IDirect3DTexture8 *texture = NULL;
1310     D3DLOCKED_RECT locked_rect;
1311     D3DCOLOR color;
1312     D3DCAPS8 caps;
1313     HRESULT hr;
1314     unsigned int i;
1315
1316     static const struct {
1317         float x, y, z;
1318         D3DCOLOR diffuse;
1319         float s, t;
1320     } quad[] = {
1321         {-1.0f, -1.0f, 0.1f, D3DCOLOR_ARGB(0x55, 0xff, 0x00, 0x00), -1.0f, -1.0f},
1322         {-1.0f,  1.0f, 0.1f, D3DCOLOR_ARGB(0x55, 0xff, 0x00, 0x00), -1.0f,  1.0f},
1323         { 1.0f, -1.0f, 0.1f, D3DCOLOR_ARGB(0x55, 0xff, 0x00, 0x00),  1.0f, -1.0f},
1324         { 1.0f,  1.0f, 0.1f, D3DCOLOR_ARGB(0x55, 0xff, 0x00, 0x00),  1.0f,  1.0f}
1325     };
1326
1327     static const struct {
1328         D3DTEXTUREOP op;
1329         const char *name;
1330         DWORD caps_flag;
1331         D3DCOLOR result;
1332     } test_data[] = {
1333         {D3DTOP_SELECTARG1,                "SELECTARG1",                D3DTEXOPCAPS_SELECTARG1,                D3DCOLOR_ARGB(0x00, 0x00, 0xff, 0x00)},
1334         {D3DTOP_SELECTARG2,                "SELECTARG2",                D3DTEXOPCAPS_SELECTARG2,                D3DCOLOR_ARGB(0x00, 0x33, 0x33, 0x33)},
1335         {D3DTOP_MODULATE,                  "MODULATE",                  D3DTEXOPCAPS_MODULATE,                  D3DCOLOR_ARGB(0x00, 0x00, 0x33, 0x00)},
1336         {D3DTOP_MODULATE2X,                "MODULATE2X",                D3DTEXOPCAPS_MODULATE2X,                D3DCOLOR_ARGB(0x00, 0x00, 0x66, 0x00)},
1337         {D3DTOP_MODULATE4X,                "MODULATE4X",                D3DTEXOPCAPS_MODULATE4X,                D3DCOLOR_ARGB(0x00, 0x00, 0xcc, 0x00)},
1338         {D3DTOP_ADD,                       "ADD",                       D3DTEXOPCAPS_ADD,                       D3DCOLOR_ARGB(0x00, 0x33, 0xff, 0x33)},
1339
1340         {D3DTOP_ADDSIGNED,                 "ADDSIGNED",                 D3DTEXOPCAPS_ADDSIGNED,                 D3DCOLOR_ARGB(0x00, 0x00, 0xb2, 0x00)},
1341         {D3DTOP_ADDSIGNED2X,               "ADDSIGNED2X",               D3DTEXOPCAPS_ADDSIGNED2X,               D3DCOLOR_ARGB(0x00, 0x00, 0xff, 0x00)},
1342
1343         {D3DTOP_SUBTRACT,                  "SUBTRACT",                  D3DTEXOPCAPS_SUBTRACT,                  D3DCOLOR_ARGB(0x00, 0x00, 0xcc, 0x00)},
1344         {D3DTOP_ADDSMOOTH,                 "ADDSMOOTH",                 D3DTEXOPCAPS_ADDSMOOTH,                 D3DCOLOR_ARGB(0x00, 0x33, 0xff, 0x33)},
1345         {D3DTOP_BLENDDIFFUSEALPHA,         "BLENDDIFFUSEALPHA",         D3DTEXOPCAPS_BLENDDIFFUSEALPHA,         D3DCOLOR_ARGB(0x00, 0x22, 0x77, 0x22)},
1346         {D3DTOP_BLENDTEXTUREALPHA,         "BLENDTEXTUREALPHA",         D3DTEXOPCAPS_BLENDTEXTUREALPHA,         D3DCOLOR_ARGB(0x00, 0x14, 0xad, 0x14)},
1347         {D3DTOP_BLENDFACTORALPHA,          "BLENDFACTORALPHA",          D3DTEXOPCAPS_BLENDFACTORALPHA,          D3DCOLOR_ARGB(0x00, 0x07, 0xe4, 0x07)},
1348         {D3DTOP_BLENDTEXTUREALPHAPM,       "BLENDTEXTUREALPHAPM",       D3DTEXOPCAPS_BLENDTEXTUREALPHAPM,       D3DCOLOR_ARGB(0x00, 0x14, 0xff, 0x14)},
1349         {D3DTOP_BLENDCURRENTALPHA,         "BLENDCURRENTALPHA",         D3DTEXOPCAPS_BLENDCURRENTALPHA,         D3DCOLOR_ARGB(0x00, 0x22, 0x77, 0x22)},
1350         {D3DTOP_MODULATEALPHA_ADDCOLOR,    "MODULATEALPHA_ADDCOLOR",    D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR,    D3DCOLOR_ARGB(0x00, 0x1f, 0xff, 0x1f)},
1351         {D3DTOP_MODULATECOLOR_ADDALPHA,    "MODULATECOLOR_ADDALPHA",    D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA,    D3DCOLOR_ARGB(0x00, 0x99, 0xcc, 0x99)},
1352         {D3DTOP_MODULATEINVALPHA_ADDCOLOR, "MODULATEINVALPHA_ADDCOLOR", D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR, D3DCOLOR_ARGB(0x00, 0x14, 0xff, 0x14)},
1353         {D3DTOP_MODULATEINVCOLOR_ADDALPHA, "MODULATEINVCOLOR_ADDALPHA", D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA, D3DCOLOR_ARGB(0x00, 0xcc, 0x99, 0xcc)},
1354         /* BUMPENVMAP & BUMPENVMAPLUMINANCE have their own tests */
1355         {D3DTOP_DOTPRODUCT3,               "DOTPRODUCT2",               D3DTEXOPCAPS_DOTPRODUCT3,               D3DCOLOR_ARGB(0x00, 0x99, 0x99, 0x99)},
1356         {D3DTOP_MULTIPLYADD,               "MULTIPLYADD",               D3DTEXOPCAPS_MULTIPLYADD,               D3DCOLOR_ARGB(0x00, 0xff, 0x33, 0x00)},
1357         {D3DTOP_LERP,                      "LERP",                      D3DTEXOPCAPS_LERP,                      D3DCOLOR_ARGB(0x00, 0x00, 0x33, 0x33)},
1358     };
1359
1360     memset(&caps, 0, sizeof(caps));
1361     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1362     ok(SUCCEEDED(hr), "GetDeviceCaps failed with 0x%08x\n", hr);
1363
1364     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX0);
1365     ok(SUCCEEDED(hr), "SetVertexShader failed with 0x%08x\n", hr);
1366
1367     hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture);
1368     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateTexture failed with 0x%08x\n", hr);
1369     hr = IDirect3DTexture8_LockRect(texture, 0, &locked_rect, NULL, 0);
1370     ok(SUCCEEDED(hr), "LockRect failed with 0x%08x\n", hr);
1371     *((DWORD *)locked_rect.pBits) = D3DCOLOR_ARGB(0x99, 0x00, 0xff, 0x00);
1372     hr = IDirect3DTexture8_UnlockRect(texture, 0);
1373     ok(SUCCEEDED(hr), "LockRect failed with 0x%08x\n", hr);
1374     hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *)texture);
1375     ok(SUCCEEDED(hr), "SetTexture failed with 0x%08x\n", hr);
1376
1377     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG0, D3DTA_DIFFUSE);
1378     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x\n", hr);
1379     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
1380     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x\n", hr);
1381     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
1382     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x\n", hr);
1383
1384     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
1385     ok(SUCCEEDED(hr), "SetTextureStageState failed with 0x%08x\n", hr);
1386
1387     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1388     ok(SUCCEEDED(hr), "SetRenderState failed with 0x%08x\n", hr);
1389     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_TEXTUREFACTOR, 0xdd333333);
1390     ok(SUCCEEDED(hr), "SetRenderState failed with 0x%08x\n", hr);
1391     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA);
1392     ok(SUCCEEDED(hr), "SetRenderState failed with 0x%08x\n", hr);
1393
1394     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
1395     ok(SUCCEEDED(hr), "IDirect3DDevice9_Clear failed with 0x%08x\n", hr);
1396
1397     for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
1398     {
1399         if (!(caps.TextureOpCaps & test_data[i].caps_flag))
1400         {
1401             skip("tex operation %s not supported\n", test_data[i].name);
1402             continue;
1403         }
1404
1405         hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLOROP, test_data[i].op);
1406         ok(SUCCEEDED(hr), "SetTextureStageState (%s) failed with 0x%08x\n", test_data[i].name, hr);
1407
1408         hr = IDirect3DDevice8_BeginScene(device);
1409         ok(SUCCEEDED(hr), "BeginScene failed with 0x%08x\n", hr);
1410
1411         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
1412         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed with 0x%08x\n", hr);
1413
1414         hr = IDirect3DDevice8_EndScene(device);
1415         ok(SUCCEEDED(hr), "EndScene failed with 0x%08x\n", hr);
1416
1417         color = getPixelColor(device, 320, 240);
1418         ok(color_match(color, test_data[i].result, 3), "Operation %s returned color 0x%08x, expected 0x%08x\n",
1419                 test_data[i].name, color, test_data[i].result);
1420
1421         hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1422         ok(SUCCEEDED(hr), "Present failed with 0x%08x\n", hr);
1423
1424         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
1425         ok(SUCCEEDED(hr), "IDirect3DDevice9_Clear failed with 0x%08x\n", hr);
1426     }
1427
1428     hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
1429     ok(SUCCEEDED(hr), "SetTexture failed with 0x%08x\n", hr);
1430     if (texture) IDirect3DTexture8_Release(texture);
1431 }
1432
1433 /* This test tests depth clamping / clipping behaviour:
1434  *   - With software vertex processing, depth values are clamped to the
1435  *     minimum / maximum z value when D3DRS_CLIPPING is disabled, and clipped
1436  *     when D3DRS_CLIPPING is enabled. Pretransformed vertices behave the
1437  *     same as regular vertices here.
1438  *   - With hardware vertex processing, D3DRS_CLIPPING seems to be ignored.
1439  *     Normal vertices are always clipped. Pretransformed vertices are
1440  *     clipped when D3DPMISCCAPS_CLIPTLVERTS is set, clamped when it isn't.
1441  *   - The viewport's MinZ/MaxZ is irrelevant for this.
1442  */
1443 static void depth_clamp_test(IDirect3DDevice8 *device)
1444 {
1445     const struct tvertex quad1[] =
1446     {
1447         {  0.0f,   0.0f,  5.0f, 1.0f, 0xff002b7f},
1448         {640.0f,   0.0f,  5.0f, 1.0f, 0xff002b7f},
1449         {  0.0f, 480.0f,  5.0f, 1.0f, 0xff002b7f},
1450         {640.0f, 480.0f,  5.0f, 1.0f, 0xff002b7f},
1451     };
1452     const struct tvertex quad2[] =
1453     {
1454         {  0.0f, 300.0f, 10.0f, 1.0f, 0xfff9e814},
1455         {640.0f, 300.0f, 10.0f, 1.0f, 0xfff9e814},
1456         {  0.0f, 360.0f, 10.0f, 1.0f, 0xfff9e814},
1457         {640.0f, 360.0f, 10.0f, 1.0f, 0xfff9e814},
1458     };
1459     const struct tvertex quad3[] =
1460     {
1461         {112.0f, 108.0f,  5.0f, 1.0f, 0xffffffff},
1462         {208.0f, 108.0f,  5.0f, 1.0f, 0xffffffff},
1463         {112.0f, 204.0f,  5.0f, 1.0f, 0xffffffff},
1464         {208.0f, 204.0f,  5.0f, 1.0f, 0xffffffff},
1465     };
1466     const struct tvertex quad4[] =
1467     {
1468         { 42.0f,  41.0f, 10.0f, 1.0f, 0xffffffff},
1469         {112.0f,  41.0f, 10.0f, 1.0f, 0xffffffff},
1470         { 42.0f, 108.0f, 10.0f, 1.0f, 0xffffffff},
1471         {112.0f, 108.0f, 10.0f, 1.0f, 0xffffffff},
1472     };
1473     const struct vertex quad5[] =
1474     {
1475         { -0.5f,   0.5f, 10.0f,       0xff14f914},
1476         {  0.5f,   0.5f, 10.0f,       0xff14f914},
1477         { -0.5f,  -0.5f, 10.0f,       0xff14f914},
1478         {  0.5f,  -0.5f, 10.0f,       0xff14f914},
1479     };
1480     const struct vertex quad6[] =
1481     {
1482         { -1.0f,   0.5f, 10.0f,       0xfff91414},
1483         {  1.0f,   0.5f, 10.0f,       0xfff91414},
1484         { -1.0f,  0.25f, 10.0f,       0xfff91414},
1485         {  1.0f,  0.25f, 10.0f,       0xfff91414},
1486     };
1487
1488     D3DVIEWPORT8 vp;
1489     D3DCOLOR color;
1490     D3DCAPS8 caps;
1491     HRESULT hr;
1492
1493     vp.X = 0;
1494     vp.Y = 0;
1495     vp.Width = 640;
1496     vp.Height = 480;
1497     vp.MinZ = 0.0;
1498     vp.MaxZ = 7.5;
1499
1500     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1501     ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
1502
1503     hr = IDirect3DDevice8_SetViewport(device, &vp);
1504     ok(SUCCEEDED(hr), "SetViewport failed, hr %#x.\n", hr);
1505
1506     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff00ff00, 1.0, 0);
1507     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1508
1509     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CLIPPING, FALSE);
1510     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1511     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1512     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1513     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, TRUE);
1514     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1515     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
1516     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1517
1518     hr = IDirect3DDevice8_BeginScene(device);
1519     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1520
1521     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1522     ok(SUCCEEDED(hr), "SetVertexSahder failed, hr %#x.\n", hr);
1523
1524     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(*quad1));
1525     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1526     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(*quad2));
1527     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1528
1529     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CLIPPING, TRUE);
1530     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1531
1532     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad3, sizeof(*quad3));
1533     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1534     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad4, sizeof(*quad4));
1535     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1536
1537     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CLIPPING, FALSE);
1538     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1539     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
1540     ok(SUCCEEDED(hr), "SetVertexShader failed, hr %#x.\n", hr);
1541
1542     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad5, sizeof(*quad5));
1543     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1544
1545     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CLIPPING, TRUE);
1546     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1547
1548     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad6, sizeof(*quad6));
1549     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1550
1551     hr = IDirect3DDevice8_EndScene(device);
1552     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1553
1554     if (caps.PrimitiveMiscCaps & D3DPMISCCAPS_CLIPTLVERTS)
1555     {
1556         color = getPixelColor(device, 75, 75);
1557         ok(color_match(color, 0x0000ff00, 1), "color 0x%08x.\n", color);
1558         color = getPixelColor(device, 150, 150);
1559         ok(color_match(color, 0x0000ff00, 1), "color 0x%08x.\n", color);
1560         color = getPixelColor(device, 320, 240);
1561         ok(color_match(color, 0x0000ff00, 1), "color 0x%08x.\n", color);
1562         color = getPixelColor(device, 320, 330);
1563         ok(color_match(color, 0x0000ff00, 1), "color 0x%08x.\n", color);
1564         color = getPixelColor(device, 320, 330);
1565         ok(color_match(color, 0x0000ff00, 1), "color 0x%08x.\n", color);
1566     }
1567     else
1568     {
1569         color = getPixelColor(device, 75, 75);
1570         ok(color_match(color, 0x00ffffff, 1), "color 0x%08x.\n", color);
1571         color = getPixelColor(device, 150, 150);
1572         ok(color_match(color, 0x00ffffff, 1), "color 0x%08x.\n", color);
1573         color = getPixelColor(device, 320, 240);
1574         ok(color_match(color, 0x00002b7f, 1), "color 0x%08x.\n", color);
1575         color = getPixelColor(device, 320, 330);
1576         ok(color_match(color, 0x00f9e814, 1), "color 0x%08x.\n", color);
1577         color = getPixelColor(device, 320, 330);
1578         ok(color_match(color, 0x00f9e814, 1), "color 0x%08x.\n", color);
1579     }
1580
1581     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1582     ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1583
1584     vp.MinZ = 0.0;
1585     vp.MaxZ = 1.0;
1586     hr = IDirect3DDevice8_SetViewport(device, &vp);
1587     ok(SUCCEEDED(hr), "SetViewport failed, hr %#x.\n", hr);
1588 }
1589
1590 static void depth_buffer_test(IDirect3DDevice8 *device)
1591 {
1592     static const struct vertex quad1[] =
1593     {
1594         { -1.0,  1.0, 0.33f, 0xff00ff00},
1595         {  1.0,  1.0, 0.33f, 0xff00ff00},
1596         { -1.0, -1.0, 0.33f, 0xff00ff00},
1597         {  1.0, -1.0, 0.33f, 0xff00ff00},
1598     };
1599     static const struct vertex quad2[] =
1600     {
1601         { -1.0,  1.0, 0.50f, 0xffff00ff},
1602         {  1.0,  1.0, 0.50f, 0xffff00ff},
1603         { -1.0, -1.0, 0.50f, 0xffff00ff},
1604         {  1.0, -1.0, 0.50f, 0xffff00ff},
1605     };
1606     static const struct vertex quad3[] =
1607     {
1608         { -1.0,  1.0, 0.66f, 0xffff0000},
1609         {  1.0,  1.0, 0.66f, 0xffff0000},
1610         { -1.0, -1.0, 0.66f, 0xffff0000},
1611         {  1.0, -1.0, 0.66f, 0xffff0000},
1612     };
1613     static const DWORD expected_colors[4][4] =
1614     {
1615         {0x000000ff, 0x000000ff, 0x0000ff00, 0x00ff0000},
1616         {0x000000ff, 0x000000ff, 0x0000ff00, 0x00ff0000},
1617         {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x00ff0000},
1618         {0x00ff0000, 0x00ff0000, 0x00ff0000, 0x00ff0000},
1619     };
1620
1621     IDirect3DSurface8 *backbuffer, *rt1, *rt2, *rt3;
1622     IDirect3DSurface8 *depth_stencil;
1623     unsigned int i, j;
1624     D3DVIEWPORT8 vp;
1625     D3DCOLOR color;
1626     HRESULT hr;
1627
1628     vp.X = 0;
1629     vp.Y = 0;
1630     vp.Width = 640;
1631     vp.Height = 480;
1632     vp.MinZ = 0.0;
1633     vp.MaxZ = 1.0;
1634
1635     hr = IDirect3DDevice8_SetViewport(device, &vp);
1636     ok(SUCCEEDED(hr), "SetViewport failed, hr %#x.\n", hr);
1637
1638     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1639     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1640     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
1641     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1642     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, TRUE);
1643     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1644     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
1645     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1646     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
1647     ok(SUCCEEDED(hr), "SetVertexShader failed, hr %#x.\n", hr);
1648
1649     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &depth_stencil);
1650     ok(SUCCEEDED(hr), "GetDepthStencilSurface failed, hr %#x.\n", hr);
1651     hr = IDirect3DDevice8_GetRenderTarget(device, &backbuffer);
1652     ok(SUCCEEDED(hr), "GetRenderTarget failed, hr %#x.\n", hr);
1653     hr = IDirect3DDevice8_CreateRenderTarget(device, 320, 240, D3DFMT_A8R8G8B8,
1654             D3DMULTISAMPLE_NONE, FALSE, &rt1);
1655     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
1656     hr = IDirect3DDevice8_CreateRenderTarget(device, 480, 360, D3DFMT_A8R8G8B8,
1657             D3DMULTISAMPLE_NONE, FALSE, &rt2);
1658     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
1659     hr = IDirect3DDevice8_CreateRenderTarget(device, 640, 480, D3DFMT_A8R8G8B8,
1660             D3DMULTISAMPLE_NONE, FALSE, &rt3);
1661     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
1662
1663     hr = IDirect3DDevice8_SetRenderTarget(device, rt3, depth_stencil);
1664     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1665     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 0.0f, 0);
1666     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1667
1668     hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depth_stencil);
1669     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1670     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 1.0f, 0);
1671     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1672
1673     hr = IDirect3DDevice8_SetRenderTarget(device, rt1, depth_stencil);
1674     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1675     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 0.0f, 0);
1676     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1677
1678     hr = IDirect3DDevice8_SetRenderTarget(device, rt2, depth_stencil);
1679     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1680     hr = IDirect3DDevice8_BeginScene(device);
1681     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1682     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(*quad2));
1683     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1684     hr = IDirect3DDevice8_EndScene(device);
1685     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1686
1687     hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depth_stencil);
1688     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1689     IDirect3DSurface8_Release(depth_stencil);
1690     IDirect3DSurface8_Release(backbuffer);
1691     IDirect3DSurface8_Release(rt3);
1692     IDirect3DSurface8_Release(rt2);
1693     IDirect3DSurface8_Release(rt1);
1694
1695     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, FALSE);
1696     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1697
1698     hr = IDirect3DDevice8_BeginScene(device);
1699     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1700     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(*quad1));
1701     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1702     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad3, sizeof(*quad3));
1703     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1704     hr = IDirect3DDevice8_EndScene(device);
1705     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1706
1707     for (i = 0; i < 4; ++i)
1708     {
1709         for (j = 0; j < 4; ++j)
1710         {
1711             unsigned int x = 80 * ((2 * j) + 1);
1712             unsigned int y = 60 * ((2 * i) + 1);
1713             color = getPixelColor(device, x, y);
1714             ok(color_match(color, expected_colors[i][j], 0),
1715                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected_colors[i][j], x, y, color);
1716         }
1717     }
1718
1719     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1720     ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1721 }
1722
1723 /* Test that partial depth copies work the way they're supposed to. The clear
1724  * on rt2 only needs a partial copy of the onscreen depth/stencil buffer, and
1725  * the following draw should only copy back the part that was modified. */
1726 static void depth_buffer2_test(IDirect3DDevice8 *device)
1727 {
1728     static const struct vertex quad[] =
1729     {
1730         { -1.0,  1.0, 0.66f, 0xffff0000},
1731         {  1.0,  1.0, 0.66f, 0xffff0000},
1732         { -1.0, -1.0, 0.66f, 0xffff0000},
1733         {  1.0, -1.0, 0.66f, 0xffff0000},
1734     };
1735
1736     IDirect3DSurface8 *backbuffer, *rt1, *rt2;
1737     IDirect3DSurface8 *depth_stencil;
1738     unsigned int i, j;
1739     D3DVIEWPORT8 vp;
1740     D3DCOLOR color;
1741     HRESULT hr;
1742
1743     vp.X = 0;
1744     vp.Y = 0;
1745     vp.Width = 640;
1746     vp.Height = 480;
1747     vp.MinZ = 0.0;
1748     vp.MaxZ = 1.0;
1749
1750     hr = IDirect3DDevice8_SetViewport(device, &vp);
1751     ok(SUCCEEDED(hr), "SetViewport failed, hr %#x.\n", hr);
1752
1753     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1754     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1755     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
1756     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1757     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, TRUE);
1758     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1759     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
1760     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1761     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
1762     ok(SUCCEEDED(hr), "SetVertexShader failed, hr %#x.\n", hr);
1763
1764     hr = IDirect3DDevice8_CreateRenderTarget(device, 640, 480, D3DFMT_A8R8G8B8,
1765             D3DMULTISAMPLE_NONE, FALSE, &rt1);
1766     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
1767     hr = IDirect3DDevice8_CreateRenderTarget(device, 480, 360, D3DFMT_A8R8G8B8,
1768             D3DMULTISAMPLE_NONE, FALSE, &rt2);
1769     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
1770     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &depth_stencil);
1771     ok(SUCCEEDED(hr), "GetDepthStencilSurface failed, hr %#x.\n", hr);
1772     hr = IDirect3DDevice8_GetRenderTarget(device, &backbuffer);
1773     ok(SUCCEEDED(hr), "GetRenderTarget failed, hr %#x.\n", hr);
1774
1775     hr = IDirect3DDevice8_SetRenderTarget(device, rt1, depth_stencil);
1776     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1777     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 1.0f, 0);
1778     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1779
1780     hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depth_stencil);
1781     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1782     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff00ff00, 0.5f, 0);
1783     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1784
1785     hr = IDirect3DDevice8_SetRenderTarget(device, rt2, depth_stencil);
1786     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1787     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 0.0f, 0);
1788     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1789
1790     hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depth_stencil);
1791     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1792     IDirect3DSurface8_Release(depth_stencil);
1793     IDirect3DSurface8_Release(backbuffer);
1794     IDirect3DSurface8_Release(rt2);
1795     IDirect3DSurface8_Release(rt1);
1796
1797     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, FALSE);
1798     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1799
1800     hr = IDirect3DDevice8_BeginScene(device);
1801     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1802     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
1803     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1804     hr = IDirect3DDevice8_EndScene(device);
1805     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1806
1807     for (i = 0; i < 4; ++i)
1808     {
1809         for (j = 0; j < 4; ++j)
1810         {
1811             unsigned int x = 80 * ((2 * j) + 1);
1812             unsigned int y = 60 * ((2 * i) + 1);
1813             color = getPixelColor(device, x, y);
1814             ok(color_match(color, D3DCOLOR_ARGB(0x00, 0x00, 0xff, 0x00), 0),
1815                     "Expected color 0x0000ff00 %u,%u, got 0x%08x.\n", x, y, color);
1816         }
1817     }
1818
1819     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1820     ok(SUCCEEDED(hr), "Present failed (0x%08x)\n", hr);
1821 }
1822
1823 static void intz_test(IDirect3DDevice8 *device)
1824 {
1825     static const DWORD ps_code[] =
1826     {
1827         0xffff0101,                                                             /* ps_1_1                       */
1828         0x00000051, 0xa00f0000, 0x3f800000, 0x00000000, 0x00000000, 0x00000000, /* def c0, 1.0, 0.0, 0.0, 0.0   */
1829         0x00000051, 0xa00f0001, 0x00000000, 0x3f800000, 0x00000000, 0x00000000, /* def c1, 0.0, 1.0, 0.0, 0.0   */
1830         0x00000051, 0xa00f0002, 0x00000000, 0x00000000, 0x3f800000, 0x00000000, /* def c2, 0.0, 0.0, 1.0, 0.0   */
1831         0x00000042, 0xb00f0000,                                                 /* tex t0                       */
1832         0x00000042, 0xb00f0001,                                                 /* tex t1                       */
1833         0x00000008, 0xb0070001, 0xa0e40000, 0xb0e40001,                         /* dp3 t1.xyz, c0, t1           */
1834         0x00000005, 0x80070000, 0xa0e40001, 0xb0e40001,                         /* mul r0.xyz, c1, t1           */
1835         0x00000004, 0x80070000, 0xa0e40000, 0xb0e40000, 0x80e40000,             /* mad r0.xyz, c0, t0, r0       */
1836         0x40000001, 0x80080000, 0xa0aa0002,                                     /* +mov r0.w, c2.z              */
1837         0x0000ffff,                                                             /* end                          */
1838     };
1839     struct
1840     {
1841         float x, y, z;
1842         float s0, t0, p0;
1843         float s1, t1, p1, q1;
1844     }
1845     quad[] =
1846     {
1847         { -1.0f,  1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.5f},
1848         {  1.0f,  1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.5f},
1849         { -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f},
1850         {  1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f},
1851     };
1852     struct
1853     {
1854         UINT x, y;
1855         D3DCOLOR color;
1856     }
1857     expected_colors[] =
1858     {
1859         { 80, 100, D3DCOLOR_ARGB(0x00, 0x20, 0x40, 0x00)},
1860         {240, 100, D3DCOLOR_ARGB(0x00, 0x60, 0xbf, 0x00)},
1861         {400, 100, D3DCOLOR_ARGB(0x00, 0x9f, 0x40, 0x00)},
1862         {560, 100, D3DCOLOR_ARGB(0x00, 0xdf, 0xbf, 0x00)},
1863         { 80, 450, D3DCOLOR_ARGB(0x00, 0x20, 0x40, 0x00)},
1864         {240, 450, D3DCOLOR_ARGB(0x00, 0x60, 0xbf, 0x00)},
1865         {400, 450, D3DCOLOR_ARGB(0x00, 0x9f, 0x40, 0x00)},
1866         {560, 450, D3DCOLOR_ARGB(0x00, 0xdf, 0xbf, 0x00)},
1867     };
1868
1869     IDirect3DSurface8 *original_ds, *original_rt, *rt;
1870     IDirect3DTexture8 *texture;
1871     IDirect3DSurface8 *ds;
1872     IDirect3D8 *d3d8;
1873     D3DCAPS8 caps;
1874     HRESULT hr;
1875     DWORD ps;
1876     UINT i;
1877
1878     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
1879     ok(SUCCEEDED(hr), "GetDeviceCaps failed, hr %#x.\n", hr);
1880     if (caps.PixelShaderVersion < D3DPS_VERSION(1, 1))
1881     {
1882         skip("No pixel shader 1.1 support, skipping INTZ test.\n");
1883         return;
1884     }
1885
1886     hr = IDirect3DDevice8_GetDirect3D(device, &d3d8);
1887     ok(SUCCEEDED(hr), "GetDirect3D failed, hr %#x.\n", hr);
1888
1889     hr = IDirect3D8_CheckDeviceFormat(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8,
1890             D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, MAKEFOURCC('I','N','T','Z'));
1891     if (FAILED(hr))
1892     {
1893         skip("No INTZ support, skipping INTZ test.\n");
1894         return;
1895     }
1896
1897     IDirect3D8_Release(d3d8);
1898
1899     hr = IDirect3DDevice8_GetRenderTarget(device, &original_rt);
1900     ok(SUCCEEDED(hr), "GetRenderTarget failed, hr %#x.\n", hr);
1901     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &original_ds);
1902     ok(SUCCEEDED(hr), "GetDepthStencilSurface failed, hr %#x.\n", hr);
1903
1904     hr = IDirect3DDevice8_CreateTexture(device, 1024, 1024, 1,
1905             D3DUSAGE_DEPTHSTENCIL, MAKEFOURCC('I','N','T','Z'), D3DPOOL_DEFAULT, &texture);
1906     ok(SUCCEEDED(hr), "CreateTexture failed, hr %#x.\n", hr);
1907     hr = IDirect3DDevice8_CreateRenderTarget(device, 1024, 1024, D3DFMT_A8R8G8B8,
1908             D3DMULTISAMPLE_NONE, FALSE, &rt);
1909     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
1910     hr = IDirect3DDevice8_CreatePixelShader(device, ps_code, &ps);
1911     ok(SUCCEEDED(hr), "CreatePixelShader failed, hr %#x.\n", hr);
1912
1913     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX2
1914             | D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEXCOORDSIZE4(1));
1915     ok(SUCCEEDED(hr), "SetVertexShader failed, hr %#x.\n", hr);
1916     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
1917     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1918     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_ALWAYS);
1919     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1920     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, TRUE);
1921     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1922     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
1923     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
1924
1925     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_POINT);
1926     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1927     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MIPFILTER, D3DTEXF_POINT);
1928     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1929     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_POINT);
1930     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1931     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3);
1932     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1933
1934     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
1935     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1936     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
1937     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1938     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MAGFILTER, D3DTEXF_POINT);
1939     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1940     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MINFILTER, D3DTEXF_POINT);
1941     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1942     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MIPFILTER, D3DTEXF_POINT);
1943     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1944     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_TEXTURETRANSFORMFLAGS,
1945             D3DTTFF_COUNT4 | D3DTTFF_PROJECTED);
1946     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
1947
1948     hr = IDirect3DTexture8_GetSurfaceLevel(texture, 0, &ds);
1949     ok(SUCCEEDED(hr), "GetSurfaceLevel failed, hr %#x.\n", hr);
1950     hr = IDirect3DDevice8_SetRenderTarget(device, rt, ds);
1951     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1952     IDirect3DDevice8_SetPixelShader(device, 0);
1953     ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
1954
1955     /* Setup the depth/stencil surface. */
1956     hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_ZBUFFER, 0, 0.0f, 0);
1957     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
1958
1959     hr = IDirect3DDevice8_BeginScene(device);
1960     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1961     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
1962     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1963     hr = IDirect3DDevice8_EndScene(device);
1964     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1965
1966     hr = IDirect3DDevice8_SetRenderTarget(device, original_rt, NULL);
1967     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1968     IDirect3DSurface8_Release(ds);
1969     hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *)texture);
1970     ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
1971     hr = IDirect3DDevice8_SetTexture(device, 1, (IDirect3DBaseTexture8 *)texture);
1972     ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
1973     hr = IDirect3DDevice8_SetPixelShader(device, ps);
1974     ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
1975
1976     /* Read the depth values back. */
1977     hr = IDirect3DDevice8_BeginScene(device);
1978     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
1979     hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
1980     ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
1981     hr = IDirect3DDevice8_EndScene(device);
1982     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
1983
1984     for (i = 0; i < sizeof(expected_colors) / sizeof(*expected_colors); ++i)
1985     {
1986         D3DCOLOR color = getPixelColor(device, expected_colors[i].x, expected_colors[i].y);
1987         ok(color_match(color, expected_colors[i].color, 1),
1988                 "Expected color 0x%08x at (%u, %u), got 0x%08x.\n",
1989                 expected_colors[i].color, expected_colors[i].x, expected_colors[i].y, color);
1990     }
1991
1992     hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1993     ok(SUCCEEDED(hr), "Present failed, hr %#x.\n", hr);
1994
1995     hr = IDirect3DDevice8_SetRenderTarget(device, original_rt, original_ds);
1996     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
1997     IDirect3DSurface8_Release(original_ds);
1998     hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
1999     ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
2000     hr = IDirect3DDevice8_SetTexture(device, 1, NULL);
2001     ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
2002     IDirect3DTexture8_Release(texture);
2003     hr = IDirect3DDevice8_SetPixelShader(device, 0);
2004     ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
2005     hr = IDirect3DDevice8_DeletePixelShader(device, ps);
2006     ok(SUCCEEDED(hr), "DeletePixelShader failed, hr %#x.\n", hr);
2007
2008     IDirect3DSurface8_Release(original_rt);
2009     IDirect3DSurface8_Release(rt);
2010 }
2011
2012 static void shadow_test(IDirect3DDevice8 *device)
2013 {
2014     static const DWORD ps_code[] =
2015     {
2016         0xffff0101,                                                             /* ps_1_1                       */
2017         0x00000051, 0xa00f0000, 0x3f800000, 0x00000000, 0x00000000, 0x00000000, /* def c0, 1.0, 0.0, 0.0, 0.0   */
2018         0x00000051, 0xa00f0001, 0x00000000, 0x3f800000, 0x00000000, 0x00000000, /* def c1, 0.0, 1.0, 0.0, 0.0   */
2019         0x00000051, 0xa00f0002, 0x00000000, 0x00000000, 0x3f800000, 0x00000000, /* def c2, 0.0, 0.0, 1.0, 0.0   */
2020         0x00000042, 0xb00f0000,                                                 /* tex t0                       */
2021         0x00000042, 0xb00f0001,                                                 /* tex t1                       */
2022         0x00000008, 0xb0070001, 0xa0e40000, 0xb0e40001,                         /* dp3 t1.xyz, c0, t1           */
2023         0x00000005, 0x80070000, 0xa0e40001, 0xb0e40001,                         /* mul r0.xyz, c1, t1           */
2024         0x00000004, 0x80070000, 0xa0e40000, 0xb0e40000, 0x80e40000,             /* mad r0.xyz, c0, t0, r0       */
2025         0x40000001, 0x80080000, 0xa0aa0002,                                     /* +mov r0.w, c2.z              */
2026         0x0000ffff,                                                             /* end                          */
2027     };
2028     struct
2029     {
2030         D3DFORMAT format;
2031         const char *name;
2032     }
2033     formats[] =
2034     {
2035         {D3DFMT_D16_LOCKABLE,   "D3DFMT_D16_LOCKABLE"},
2036         {D3DFMT_D32,            "D3DFMT_D32"},
2037         {D3DFMT_D15S1,          "D3DFMT_D15S1"},
2038         {D3DFMT_D24S8,          "D3DFMT_D24S8"},
2039         {D3DFMT_D24X8,          "D3DFMT_D24X8"},
2040         {D3DFMT_D24X4S4,        "D3DFMT_D24X4S4"},
2041         {D3DFMT_D16,            "D3DFMT_D16"},
2042     };
2043     struct
2044     {
2045         float x, y, z;
2046         float s0, t0, p0;
2047         float s1, t1, p1, q1;
2048     }
2049     quad[] =
2050     {
2051         { -1.0f,  1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f},
2052         {  1.0f,  1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f},
2053         { -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
2054         {  1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
2055     };
2056     struct
2057     {
2058         UINT x, y;
2059         D3DCOLOR color;
2060     }
2061     expected_colors[] =
2062     {
2063         {400,  60, D3DCOLOR_ARGB(0x00, 0x00, 0x00, 0x00)},
2064         {560, 180, D3DCOLOR_ARGB(0x00, 0xff, 0x00, 0x00)},
2065         {560, 300, D3DCOLOR_ARGB(0x00, 0xff, 0x00, 0x00)},
2066         {400, 420, D3DCOLOR_ARGB(0x00, 0xff, 0xff, 0x00)},
2067         {240, 420, D3DCOLOR_ARGB(0x00, 0xff, 0xff, 0x00)},
2068         { 80, 300, D3DCOLOR_ARGB(0x00, 0x00, 0x00, 0x00)},
2069         { 80, 180, D3DCOLOR_ARGB(0x00, 0x00, 0x00, 0x00)},
2070         {240,  60, D3DCOLOR_ARGB(0x00, 0x00, 0x00, 0x00)},
2071     };
2072
2073     IDirect3DSurface8 *original_ds, *original_rt, *rt;
2074     IDirect3D8 *d3d8;
2075     D3DCAPS8 caps;
2076     HRESULT hr;
2077     DWORD ps;
2078     UINT i;
2079
2080     hr = IDirect3DDevice8_GetDeviceCaps(device, &caps);
2081     ok(SUCCEEDED(hr), "GetDeviceCaps failed, hr %#x.\n", hr);
2082     if (caps.PixelShaderVersion < D3DPS_VERSION(1, 1))
2083     {
2084         skip("No pixel shader 1.1 support, skipping shadow test.\n");
2085         return;
2086     }
2087
2088     hr = IDirect3DDevice8_GetDirect3D(device, &d3d8);
2089     ok(SUCCEEDED(hr), "GetDirect3D failed, hr %#x.\n", hr);
2090     hr = IDirect3DDevice8_GetRenderTarget(device, &original_rt);
2091     ok(SUCCEEDED(hr), "GetRenderTarget failed, hr %#x.\n", hr);
2092     hr = IDirect3DDevice8_GetDepthStencilSurface(device, &original_ds);
2093     ok(SUCCEEDED(hr), "GetDepthStencilSurface failed, hr %#x.\n", hr);
2094
2095     hr = IDirect3DDevice8_CreateRenderTarget(device, 1024, 1024, D3DFMT_A8R8G8B8,
2096             D3DMULTISAMPLE_NONE, FALSE, &rt);
2097     ok(SUCCEEDED(hr), "CreateRenderTarget failed, hr %#x.\n", hr);
2098     hr = IDirect3DDevice8_CreatePixelShader(device, ps_code, &ps);
2099     ok(SUCCEEDED(hr), "CreatePixelShader failed, hr %#x.\n", hr);
2100
2101     hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX2
2102             | D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEXCOORDSIZE4(1));
2103     ok(SUCCEEDED(hr), "SetVertexShader failed, hr %#x.\n", hr);
2104     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
2105     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
2106     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_ALWAYS);
2107     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
2108     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZWRITEENABLE, TRUE);
2109     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
2110     hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
2111     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
2112
2113     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_POINT);
2114     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
2115     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MIPFILTER, D3DTEXF_POINT);
2116     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
2117     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_POINT);
2118     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
2119     hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3);
2120     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
2121
2122     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
2123     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
2124     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
2125     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
2126     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MAGFILTER, D3DTEXF_POINT);
2127     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
2128     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MINFILTER, D3DTEXF_POINT);
2129     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
2130     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_MIPFILTER, D3DTEXF_POINT);
2131     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
2132     hr = IDirect3DDevice8_SetTextureStageState(device, 1, D3DTSS_TEXTURETRANSFORMFLAGS,
2133             D3DTTFF_COUNT4 | D3DTTFF_PROJECTED);
2134     ok(SUCCEEDED(hr), "SetTextureStageState failed, hr %#x.\n", hr);
2135
2136     for (i = 0; i < sizeof(formats) / sizeof(*formats); ++i)
2137     {
2138         D3DFORMAT format = formats[i].format;
2139         IDirect3DTexture8 *texture;
2140         IDirect3DSurface8 *ds;
2141         unsigned int j;
2142
2143         hr = IDirect3D8_CheckDeviceFormat(d3d8, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8,
2144                 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, format);
2145         if (FAILED(hr)) continue;
2146
2147         hr = IDirect3DDevice8_CreateTexture(device, 1024, 1024, 1,
2148                 D3DUSAGE_DEPTHSTENCIL, format, D3DPOOL_DEFAULT, &texture);
2149         ok(SUCCEEDED(hr), "CreateTexture failed, hr %#x.\n", hr);
2150
2151         hr = IDirect3DTexture8_GetSurfaceLevel(texture, 0, &ds);
2152         ok(SUCCEEDED(hr), "GetSurfaceLevel failed, hr %#x.\n", hr);
2153
2154         hr = IDirect3DDevice8_SetRenderTarget(device, rt, ds);
2155         ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
2156
2157         IDirect3DDevice8_SetPixelShader(device, 0);
2158         ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
2159
2160         /* Setup the depth/stencil surface. */
2161         hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_ZBUFFER, 0, 0.0f, 0);
2162         ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
2163
2164         hr = IDirect3DDevice8_BeginScene(device);
2165         ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
2166         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
2167         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
2168         hr = IDirect3DDevice8_EndScene(device);
2169         ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
2170
2171         hr = IDirect3DDevice8_SetRenderTarget(device, original_rt, NULL);
2172         ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
2173         IDirect3DSurface8_Release(ds);
2174
2175         hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *)texture);
2176         ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
2177         hr = IDirect3DDevice8_SetTexture(device, 1, (IDirect3DBaseTexture8 *)texture);
2178         ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
2179
2180         hr = IDirect3DDevice8_SetPixelShader(device, ps);
2181         ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
2182
2183         /* Do the actual shadow mapping. */
2184         hr = IDirect3DDevice8_BeginScene(device);
2185         ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
2186         hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(*quad));
2187         ok(SUCCEEDED(hr), "DrawPrimitiveUP failed, hr %#x.\n", hr);
2188         hr = IDirect3DDevice8_EndScene(device);
2189         ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
2190
2191         hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
2192         ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
2193         hr = IDirect3DDevice8_SetTexture(device, 1, NULL);
2194         ok(SUCCEEDED(hr), "SetTexture failed, hr %#x.\n", hr);
2195         IDirect3DTexture8_Release(texture);
2196
2197         for (j = 0; j < sizeof(expected_colors) / sizeof(*expected_colors); ++j)
2198         {
2199             D3DCOLOR color = getPixelColor(device, expected_colors[j].x, expected_colors[j].y);
2200             ok(color_match(color, expected_colors[j].color, 0),
2201                     "Expected color 0x%08x at (%u, %u) for format %s, got 0x%08x.\n",
2202                     expected_colors[j].color, expected_colors[j].x, expected_colors[j].y,
2203                     formats[i].name, color);
2204         }
2205
2206         hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
2207         ok(SUCCEEDED(hr), "Present failed, hr %#x.\n", hr);
2208     }
2209
2210     hr = IDirect3DDevice8_SetPixelShader(device, 0);
2211     ok(SUCCEEDED(hr), "SetPixelShader failed, hr %#x.\n", hr);
2212     hr = IDirect3DDevice8_DeletePixelShader(device, ps);
2213     ok(SUCCEEDED(hr), "DeletePixelShader failed, hr %#x.\n", hr);
2214
2215     hr = IDirect3DDevice8_SetRenderTarget(device, original_rt, original_ds);
2216     ok(SUCCEEDED(hr), "SetRenderTarget failed, hr %#x.\n", hr);
2217     IDirect3DSurface8_Release(original_ds);
2218
2219     IDirect3DSurface8_Release(original_rt);
2220     IDirect3DSurface8_Release(rt);
2221
2222     IDirect3D8_Release(d3d8);
2223 }
2224
2225 START_TEST(visual)
2226 {
2227     IDirect3DDevice8 *device_ptr;
2228     HRESULT hr;
2229     DWORD color;
2230     D3DCAPS8 caps;
2231
2232     d3d8_handle = LoadLibraryA("d3d8.dll");
2233     if (!d3d8_handle)
2234     {
2235         win_skip("Could not load d3d8.dll\n");
2236         return;
2237     }
2238
2239     device_ptr = init_d3d8();
2240     if (!device_ptr)
2241     {
2242         win_skip("Could not initialize direct3d\n");
2243         return;
2244     }
2245
2246     IDirect3DDevice8_GetDeviceCaps(device_ptr, &caps);
2247
2248     /* Check for the reliability of the returned data */
2249     hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
2250     if(FAILED(hr))
2251     {
2252         skip("Clear failed, can't assure correctness of the test results\n");
2253         goto cleanup;
2254     }
2255
2256     color = getPixelColor(device_ptr, 1, 1);
2257     if(color !=0x00ff0000)
2258     {
2259         skip("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests\n", color);
2260         goto cleanup;
2261     }
2262     IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
2263
2264     hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
2265     if(FAILED(hr))
2266     {
2267         skip("Clear failed, can't assure correctness of the test results\n");
2268         goto cleanup;
2269     }
2270
2271     color = getPixelColor(device_ptr, 639, 479);
2272     if(color != 0x0000ddee)
2273     {
2274         skip("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests\n", color);
2275         goto cleanup;
2276     }
2277     IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
2278
2279     /* Now run the real test */
2280     depth_clamp_test(device_ptr);
2281     lighting_test(device_ptr);
2282     clear_test(device_ptr);
2283     fog_test(device_ptr);
2284     present_test(device_ptr);
2285     offscreen_test(device_ptr);
2286     alpha_test(device_ptr);
2287
2288     if (caps.VertexShaderVersion >= D3DVS_VERSION(1, 1))
2289     {
2290         test_rcp_rsq(device_ptr);
2291     }
2292     else
2293     {
2294         skip("No vs.1.1 support\n");
2295     }
2296
2297     p8_texture_test(device_ptr);
2298     texop_test(device_ptr);
2299     depth_buffer_test(device_ptr);
2300     depth_buffer2_test(device_ptr);
2301     intz_test(device_ptr);
2302     shadow_test(device_ptr);
2303
2304 cleanup:
2305     if(device_ptr) {
2306         D3DDEVICE_CREATION_PARAMETERS creation_parameters;
2307         ULONG refcount;
2308
2309         IDirect3DDevice8_GetCreationParameters(device_ptr, &creation_parameters);
2310         DestroyWindow(creation_parameters.hFocusWindow);
2311         refcount = IDirect3DDevice8_Release(device_ptr);
2312         ok(!refcount, "Device has %u references left\n", refcount);
2313     }
2314 }