tests: Remove unneeded assert.h includes.
[wine] / dlls / ddraw / tests / visual.c
1 /*
2  * Copyright (C) 2007 Stefan Dösinger(for CodeWeavers)
3  * Copyright (C) 2008 Alexander Dorofeyev
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 #include "wine/test.h"
23 #include "ddraw.h"
24 #include "d3d.h"
25
26 static HWND window;
27 static IDirectDraw7        *DirectDraw;
28 static IDirectDrawSurface7 *Surface;
29 static IDirectDrawSurface7 *depth_buffer;
30 static IDirect3D7          *Direct3D;
31 static IDirect3DDevice7    *Direct3DDevice;
32
33 static IDirectDraw *DirectDraw1;
34 static IDirectDrawSurface *Surface1;
35 static IDirect3D *Direct3D1;
36 static IDirect3DDevice *Direct3DDevice1;
37 static IDirect3DExecuteBuffer *ExecuteBuffer;
38 static IDirect3DViewport *Viewport;
39
40 static BOOL refdevice = FALSE;
41
42 static HRESULT (WINAPI *pDirectDrawCreateEx)(LPGUID,LPVOID*,REFIID,LPUNKNOWN);
43
44 static BOOL color_match(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
45 {
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     c1 >>= 8; c2 >>= 8;
52     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
53     return TRUE;
54 }
55
56 static HRESULT WINAPI enum_z_fmt(DDPIXELFORMAT *fmt, void *ctx)
57 {
58     DDPIXELFORMAT *zfmt = ctx;
59
60     if(U1(*fmt).dwZBufferBitDepth > U1(*zfmt).dwZBufferBitDepth)
61     {
62         *zfmt = *fmt;
63     }
64     return DDENUMRET_OK;
65 }
66
67 static BOOL createObjects(void)
68 {
69     HRESULT hr;
70     HMODULE hmod = GetModuleHandleA("ddraw.dll");
71     WNDCLASS wc = {0};
72     DDSURFACEDESC2 ddsd;
73     DDPIXELFORMAT zfmt;
74
75     if(!hmod) return FALSE;
76     pDirectDrawCreateEx = (void*)GetProcAddress(hmod, "DirectDrawCreateEx");
77     if(!pDirectDrawCreateEx) return FALSE;
78
79     hr = pDirectDrawCreateEx(NULL, (void **) &DirectDraw, &IID_IDirectDraw7, NULL);
80     ok(hr==DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", hr);
81     if(!DirectDraw) goto err;
82
83     wc.lpfnWndProc = DefWindowProc;
84     wc.lpszClassName = "d3d7_test_wc";
85     RegisterClass(&wc);
86     window = CreateWindow("d3d7_test_wc", "d3d7_test", WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
87
88     hr = IDirectDraw7_SetCooperativeLevel(DirectDraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
89     ok(hr == DD_OK, "IDirectDraw7_SetCooperativeLevel failed with %08x\n", hr);
90     if(FAILED(hr)) goto err;
91     hr = IDirectDraw7_SetDisplayMode(DirectDraw, 640, 480, 32, 0, 0);
92     if(FAILED(hr)) {
93         /* 24 bit is fine too */
94         hr = IDirectDraw7_SetDisplayMode(DirectDraw, 640, 480, 24, 0, 0);
95
96     }
97     ok(hr == DD_OK || hr == DDERR_UNSUPPORTED, "IDirectDraw7_SetDisplayMode failed with %08x\n", hr);
98     if(FAILED(hr)) {
99         /* use trace, the caller calls skip() */
100         trace("SetDisplayMode failed\n");
101         goto err;
102     }
103
104     hr = IDirectDraw7_QueryInterface(DirectDraw, &IID_IDirect3D7, (void**) &Direct3D);
105     if (hr == E_NOINTERFACE) goto err;
106     ok(hr==DD_OK, "QueryInterface returned: %08x\n", hr);
107
108     /* DirectDraw Flipping behavior doesn't seem that well-defined. The reference rasterizer behaves differently
109      * than hardware implementations. Request single buffering, that seems to work everywhere
110      */
111     memset(&ddsd, 0, sizeof(ddsd));
112     ddsd.dwSize = sizeof(ddsd);
113     ddsd.dwFlags = DDSD_CAPS;
114     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE;
115     ddsd.dwBackBufferCount = 1;
116     hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &Surface, NULL);
117     if(FAILED(hr)) goto err;
118
119     memset(&zfmt, 0, sizeof(zfmt));
120     hr = IDirect3D7_EnumZBufferFormats(Direct3D, &IID_IDirect3DTnLHalDevice, enum_z_fmt, &zfmt);
121     if (FAILED(hr)) goto err;
122     if (zfmt.dwSize == 0) goto err;
123
124     memset(&ddsd, 0, sizeof(ddsd));
125     ddsd.dwSize = sizeof(ddsd);
126     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
127     ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
128     U4(ddsd).ddpfPixelFormat = zfmt;
129     ddsd.dwWidth = 640;
130     ddsd.dwHeight = 480;
131     hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &depth_buffer, NULL);
132     ok(SUCCEEDED(hr), "CreateSurface failed, hr %#x.\n", hr);
133     if (FAILED(hr)) goto err;
134
135     hr = IDirectDrawSurface_AddAttachedSurface(Surface, depth_buffer);
136     ok(SUCCEEDED(hr), "AddAttachedSurface failed, hr %#x.\n", hr);
137     if (FAILED(hr)) goto err;
138
139     hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DTnLHalDevice, Surface, &Direct3DDevice);
140     if (FAILED(hr) || !Direct3DDevice) goto err;
141     return TRUE;
142
143     err:
144     if(DirectDraw) IDirectDraw7_Release(DirectDraw);
145     if (depth_buffer) IDirectDrawSurface7_Release(depth_buffer);
146     if(Surface) IDirectDrawSurface7_Release(Surface);
147     if(Direct3D) IDirect3D7_Release(Direct3D);
148     if(Direct3DDevice) IDirect3DDevice7_Release(Direct3DDevice);
149     if(window) DestroyWindow(window);
150     return FALSE;
151 }
152
153 static void releaseObjects(void)
154 {
155     IDirect3DDevice7_Release(Direct3DDevice);
156     IDirect3D7_Release(Direct3D);
157     IDirectDrawSurface7_Release(depth_buffer);
158     IDirectDrawSurface7_Release(Surface);
159     IDirectDraw7_Release(DirectDraw);
160     DestroyWindow(window);
161 }
162
163 static DWORD getPixelColor(IDirect3DDevice7 *device, UINT x, UINT y)
164 {
165     DWORD ret;
166     HRESULT hr;
167     DDSURFACEDESC2 ddsd;
168     RECT rectToLock = {x, y, x+1, y+1};
169     IDirectDrawSurface7 *surf = NULL;
170
171     /* Some implementations seem to dislike direct locking on the front buffer. Thus copy the front buffer
172      * to an offscreen surface and lock it instead of the front buffer
173      */
174     memset(&ddsd, 0, sizeof(ddsd));
175     ddsd.dwSize = sizeof(ddsd);
176     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
177     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
178     ddsd.dwWidth = 640;
179     ddsd.dwHeight = 480;
180     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
181     hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &surf, NULL);
182     ok(hr == DD_OK, "IDirectDraw7_CreateSurface failed with %08x\n", hr);
183     if(!surf)
184     {
185         trace("cannot create helper surface\n");
186         return 0xdeadbeef;
187     }
188
189     memset(&ddsd, 0, sizeof(ddsd));
190     ddsd.dwSize = sizeof(ddsd);
191     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
192
193     hr = IDirectDrawSurface_BltFast(surf, 0, 0, Surface, NULL, 0);
194     ok(hr == DD_OK, "IDirectDrawSurface7_BltFast returned %08x\n", hr);
195     if(FAILED(hr))
196     {
197         trace("Cannot blit\n");
198         ret = 0xdeadbee;
199         goto out;
200     }
201
202     hr = IDirectDrawSurface7_Lock(surf, &rectToLock, &ddsd, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
203     if(FAILED(hr))
204     {
205         trace("Can't lock the offscreen surface, hr=%08x\n", hr);
206         ret = 0xdeadbeec;
207         goto out;
208     }
209
210     /* Remove the X channel for now. DirectX and OpenGL have different ideas how to treat it apparently, and it isn't
211      * really important for these tests
212      */
213     ret = ((DWORD *) ddsd.lpSurface)[0] & 0x00ffffff;
214     hr = IDirectDrawSurface7_Unlock(surf, NULL);
215     if(FAILED(hr))
216     {
217         trace("Can't unlock the offscreen surface, hr=%08x\n", hr);
218     }
219
220 out:
221     IDirectDrawSurface7_Release(surf);
222     return ret;
223 }
224
225 static void set_viewport_size(IDirect3DDevice7 *device)
226 {
227     D3DVIEWPORT7 vp = {0};
228     DDSURFACEDESC2 ddsd;
229     HRESULT hr;
230     IDirectDrawSurface7 *target;
231
232     hr = IDirect3DDevice7_GetRenderTarget(device, &target);
233     ok(hr == D3D_OK, "IDirect3DDevice7_GetRenderTarget returned %08x\n", hr);
234
235     memset(&ddsd, 0, sizeof(ddsd));
236     ddsd.dwSize = sizeof(ddsd);
237     IDirectDrawSurface7_GetSurfaceDesc(target, &ddsd);
238     ok(hr == D3D_OK, "IDirectDrawSurface7_GetSurfaceDesc returned %08x\n", hr);
239     IDirectDrawSurface7_Release(target);
240
241     vp.dwWidth = ddsd.dwWidth;
242     vp.dwHeight = ddsd.dwHeight;
243     hr = IDirect3DDevice7_SetViewport(device, &vp);
244     ok(hr == D3D_OK, "IDirect3DDevice7_SetViewport returned %08x\n", hr);
245     return;
246 }
247
248 struct vertex
249 {
250     float x, y, z;
251     DWORD diffuse;
252 };
253
254 struct tvertex
255 {
256     float x, y, z, w;
257     DWORD diffuse;
258 };
259
260 struct nvertex
261 {
262     float x, y, z;
263     float nx, ny, nz;
264     DWORD diffuse;
265 };
266
267 static void lighting_test(IDirect3DDevice7 *device)
268 {
269     HRESULT hr;
270     DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
271     DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
272     DWORD color;
273
274     float mat[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
275                       0.0f, 1.0f, 0.0f, 0.0f,
276                       0.0f, 0.0f, 1.0f, 0.0f,
277                       0.0f, 0.0f, 0.0f, 1.0f };
278
279     struct vertex unlitquad[] =
280     {
281         {-1.0f, -1.0f,   0.1f,                          0xffff0000},
282         {-1.0f,  0.0f,   0.1f,                          0xffff0000},
283         { 0.0f,  0.0f,   0.1f,                          0xffff0000},
284         { 0.0f, -1.0f,   0.1f,                          0xffff0000},
285     };
286     struct vertex litquad[] =
287     {
288         {-1.0f,  0.0f,   0.1f,                          0xff00ff00},
289         {-1.0f,  1.0f,   0.1f,                          0xff00ff00},
290         { 0.0f,  1.0f,   0.1f,                          0xff00ff00},
291         { 0.0f,  0.0f,   0.1f,                          0xff00ff00},
292     };
293     struct nvertex unlitnquad[] =
294     {
295         { 0.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
296         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
297         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
298         { 1.0f, -1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xff0000ff},
299     };
300     struct nvertex litnquad[] =
301     {
302         { 0.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
303         { 0.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
304         { 1.0f,  1.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
305         { 1.0f,  0.0f,   0.1f,  1.0f,   1.0f,   1.0f,   0xffffff00},
306     };
307     WORD Indices[] = {0, 1, 2, 2, 3, 0};
308
309     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
310     ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
311
312     /* Setup some states that may cause issues */
313     hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_WORLD, (D3DMATRIX *) mat);
314     ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
315     hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_VIEW, (D3DMATRIX *)mat);
316     ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
317     hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, (D3DMATRIX *) mat);
318     ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
319     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
320     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
321     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
322     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
323     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
324     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
325     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_STENCILENABLE, FALSE);
326     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
327     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHATESTENABLE, FALSE);
328     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
329     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
330     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
331     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
332     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed with %08x\n", hr);
333
334     hr = IDirect3DDevice7_BeginScene(device);
335     ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
336     if(hr == D3D_OK)
337     {
338         /* No lights are defined... That means, lit vertices should be entirely black */
339         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
340         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
341         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, unlitquad, 4 /* NumVerts */,
342                                                     Indices, 6 /* Indexcount */, 0 /* flags */);
343         ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
344
345         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
346         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
347         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, litquad, 4 /* NumVerts */,
348                                                     Indices, 6 /* Indexcount */, 0 /* flags */);
349         ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
350
351         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
352         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
353         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, unlitnquad, 4 /* NumVerts */,
354                                                     Indices, 6 /* Indexcount */, 0 /* flags */);
355         ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
356
357         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
358         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
359         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, litnquad, 4 /* NumVerts */,
360                                                     Indices, 6 /* Indexcount */, 0 /* flags */);
361         ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
362
363         IDirect3DDevice7_EndScene(device);
364         ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
365     }
366
367     color = getPixelColor(device, 160, 360); /* Lower left quad - unlit without normals */
368     ok(color == 0x00ff0000, "Unlit quad without normals has color 0x%08x, expected 0x00ff0000.\n", color);
369     color = getPixelColor(device, 160, 120); /* Upper left quad - lit without normals */
370     ok(color == 0x00000000, "Lit quad without normals has color 0x%08x, expected 0x00000000.\n", color);
371     color = getPixelColor(device, 480, 360); /* Lower right quad - unlit with normals */
372     ok(color == 0x000000ff, "Unlit quad with normals has color 0x%08x, expected 0x000000ff.\n", color);
373     color = getPixelColor(device, 480, 120); /* Upper right quad - lit with normals */
374     ok(color == 0x00000000, "Lit quad with normals has color 0x%08x, expected 0x00000000.\n", color);
375
376     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
377     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
378 }
379
380 static void clear_test(IDirect3DDevice7 *device)
381 {
382     /* Tests the correctness of clearing parameters */
383     HRESULT hr;
384     D3DRECT rect[2];
385     D3DRECT rect_negneg;
386     DWORD color;
387
388     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
389     ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
390
391     /* Positive x, negative y */
392     U1(rect[0]).x1 = 0;
393     U2(rect[0]).y1 = 480;
394     U3(rect[0]).x2 = 320;
395     U4(rect[0]).y2 = 240;
396
397     /* Positive x, positive y */
398     U1(rect[1]).x1 = 0;
399     U2(rect[1]).y1 = 0;
400     U3(rect[1]).x2 = 320;
401     U4(rect[1]).y2 = 240;
402     /* Clear 2 rectangles with one call. Shows that a positive value is returned, but the negative rectangle
403      * is ignored, the positive is still cleared afterwards
404      */
405     hr = IDirect3DDevice7_Clear(device, 2, rect, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
406     ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
407
408     /* negative x, negative y */
409     U1(rect_negneg).x1 = 640;
410     U2(rect_negneg).y1 = 240;
411     U3(rect_negneg).x2 = 320;
412     U4(rect_negneg).y2 = 0;
413     hr = IDirect3DDevice7_Clear(device, 1, &rect_negneg, D3DCLEAR_TARGET, 0xff00ff00, 0.0, 0);
414     ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
415
416     color = getPixelColor(device, 160, 360); /* lower left quad */
417     ok(color == 0x00ffffff, "Clear rectangle 3(pos, neg) has color %08x\n", color);
418     color = getPixelColor(device, 160, 120); /* upper left quad */
419     ok(color == 0x00ff0000, "Clear rectangle 1(pos, pos) has color %08x\n", color);
420     color = getPixelColor(device, 480, 360); /* lower right quad  */
421     ok(color == 0x00ffffff, "Clear rectangle 4(NULL) has color %08x\n", color);
422     color = getPixelColor(device, 480, 120); /* upper right quad */
423     ok(color == 0x00ffffff, "Clear rectangle 4(neg, neg) has color %08x\n", color);
424 }
425
426 struct sVertex {
427     float x, y, z;
428     DWORD diffuse;
429     DWORD specular;
430 };
431
432 struct sVertexT {
433     float x, y, z, rhw;
434     DWORD diffuse;
435     DWORD specular;
436 };
437
438 static void fog_test(IDirect3DDevice7 *device)
439 {
440     HRESULT hr;
441     DWORD color;
442     float start = 0.0, end = 1.0;
443     D3DDEVICEDESC7 caps;
444
445     /* Gets full z based fog with linear fog, no fog with specular color */
446     struct sVertex untransformed_1[] = {
447         {-1,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
448         {-1,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
449         { 0,     0,   0.1f,         0xFFFF0000,     0xFF000000  },
450         { 0,    -1,   0.1f,         0xFFFF0000,     0xFF000000  },
451     };
452     /* Ok, I am too lazy to deal with transform matrices */
453     struct sVertex untransformed_2[] = {
454         {-1,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
455         {-1,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
456         { 0,     1,   1.0f,         0xFFFF0000,     0xFF000000  },
457         { 0,     0,   1.0f,         0xFFFF0000,     0xFF000000  },
458     };
459     /* Untransformed ones. Give them a different diffuse color to make the test look
460      * nicer. It also makes making sure that they are drawn correctly easier.
461      */
462     struct sVertexT transformed_1[] = {
463         {320,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
464         {640,    0,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
465         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
466         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
467     };
468     struct sVertexT transformed_2[] = {
469         {320,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
470         {640,  240,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
471         {640,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
472         {320,  480,   1.0f, 1.0f,   0xFFFFFF00,     0xFF000000  },
473     };
474     WORD Indices[] = {0, 1, 2, 2, 3, 0};
475
476     float ident_mat[16] =
477     {
478         1.0f, 0.0f, 0.0f, 0.0f,
479         0.0f, 1.0f, 0.0f, 0.0f,
480         0.0f, 0.0f, 1.0f, 0.0f,
481         0.0f, 0.0f, 0.0f, 1.0f
482     };
483     float world_mat1[16] =
484     {
485         1.0f, 0.0f,  0.0f, 0.0f,
486         0.0f, 1.0f,  0.0f, 0.0f,
487         0.0f, 0.0f,  1.0f, 0.0f,
488         0.0f, 0.0f, -0.5f, 1.0f
489     };
490     float world_mat2[16] =
491     {
492         1.0f, 0.0f, 0.0f, 0.0f,
493         0.0f, 1.0f, 0.0f, 0.0f,
494         0.0f, 0.0f, 1.0f, 0.0f,
495         0.0f, 0.0f, 1.0f, 1.0f
496     };
497     float proj_mat[16] =
498     {
499         1.0f, 0.0f,  0.0f, 0.0f,
500         0.0f, 1.0f,  0.0f, 0.0f,
501         0.0f, 0.0f,  1.0f, 0.0f,
502         0.0f, 0.0f, -1.0f, 1.0f
503     };
504
505     struct sVertex far_quad1[] =
506     {
507         {-1.0f, -1.0f, 0.5f, 0xffff0000, 0xff000000},
508         {-1.0f,  0.0f, 0.5f, 0xffff0000, 0xff000000},
509         { 0.0f,  0.0f, 0.5f, 0xffff0000, 0xff000000},
510         { 0.0f, -1.0f, 0.5f, 0xffff0000, 0xff000000},
511     };
512     struct sVertex far_quad2[] =
513     {
514         {-1.0f, 0.0f, 1.5f, 0xffff0000, 0xff000000},
515         {-1.0f, 1.0f, 1.5f, 0xffff0000, 0xff000000},
516         { 0.0f, 1.0f, 1.5f, 0xffff0000, 0xff000000},
517         { 0.0f, 0.0f, 1.5f, 0xffff0000, 0xff000000},
518     };
519
520     memset(&caps, 0, sizeof(caps));
521     hr = IDirect3DDevice7_GetCaps(device, &caps);
522     ok(hr == D3D_OK, "IDirect3DDevice7_GetCaps returned %08x\n", hr);
523     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
524     ok(hr == D3D_OK, "IDirect3DDevice7_Clear returned %08x\n", hr);
525
526     /* Setup initial states: No lighting, fog on, fog color */
527     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
528     ok(hr == D3D_OK, "Turning off lighting returned %08x\n", hr);
529     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, TRUE);
530     ok(hr == D3D_OK, "Turning on fog calculations returned %08x\n", hr);
531     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGCOLOR, 0xFF00FF00 /* A nice green */);
532     ok(hr == D3D_OK, "Setting fog color returned %08x\n", hr);
533
534     /* First test: Both table fog and vertex fog off */
535     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGTABLEMODE, D3DFOG_NONE);
536     ok(hr == D3D_OK, "Turning off table fog returned %08x\n", hr);
537     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGVERTEXMODE, D3DFOG_NONE);
538     ok(hr == D3D_OK, "Turning off vertex fog returned %08x\n", hr);
539
540     /* Start = 0, end = 1. Should be default, but set them */
541     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGSTART, *((DWORD *) &start));
542     ok(hr == D3D_OK, "Setting fog start returned %08x\n", hr);
543     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGEND, *((DWORD *) &end));
544     ok(hr == D3D_OK, "Setting fog end returned %08x\n", hr);
545
546     if(IDirect3DDevice7_BeginScene(device) == D3D_OK)
547     {
548         /* Untransformed, vertex fog = NONE, table fog = NONE: Read the fog weighting from the specular color */
549         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
550                                                    untransformed_1, 4, Indices, 6, 0);
551         ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
552
553         /* That makes it use the Z value */
554         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGVERTEXMODE, D3DFOG_LINEAR);
555         ok(hr == D3D_OK, "Setting fog vertex mode to D3DFOG_LINEAR returned %08x\n", hr);
556         /* Untransformed, vertex fog != none (or table fog != none):
557          * Use the Z value as input into the equation
558          */
559         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
560                                                    untransformed_2, 4, Indices, 6, 0);
561         ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
562
563         /* transformed verts */
564         ok( hr == D3D_OK, "SetFVF returned %08x\n", hr);
565         /* Transformed, vertex fog != NONE, pixel fog == NONE: Use specular color alpha component */
566         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
567                                                    transformed_1, 4, Indices, 6, 0);
568         ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
569
570         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGTABLEMODE, D3DFOG_LINEAR);
571         ok( hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR returned %08x\n", hr);
572         /* Transformed, table fog != none, vertex anything: Use Z value as input to the fog
573          * equation
574          */
575         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR,
576                                                    transformed_2, 4, Indices, 6, 0);
577         ok(hr == D3D_OK, "DrawIndexedPrimitive returned %08x\n", hr);
578
579         hr = IDirect3DDevice7_EndScene(device);
580         ok(hr == D3D_OK, "EndScene returned %08x\n", hr);
581     }
582     else
583     {
584         ok(FALSE, "BeginScene failed\n");
585     }
586
587     color = getPixelColor(device, 160, 360);
588     ok(color_match(color, 0x00FF0000, 1), "Untransformed vertex with no table or vertex fog has color %08x\n", color);
589     color = getPixelColor(device, 160, 120);
590     ok(color_match(color, 0x0000FF00, 1), "Untransformed vertex with linear vertex fog has color %08x\n", color);
591     color = getPixelColor(device, 480, 120);
592     ok(color_match(color, 0x00FFFF00, 1), "Transformed vertex with linear vertex fog has color %08x\n", color);
593     if(caps.dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_FOGTABLE)
594     {
595         color = getPixelColor(device, 480, 360);
596         ok(color_match(color, 0x0000FF00, 1), "Transformed vertex with linear table fog has color %08x\n", color);
597     }
598     else
599     {
600         /* Without fog table support the vertex fog is still applied, even though table fog is turned on.
601          * The settings above result in no fogging with vertex fog
602          */
603         color = getPixelColor(device, 480, 120);
604         ok(color == 0x00FFFF00, "Transformed vertex with linear vertex fog has color %08x\n", color);
605         trace("Info: Table fog not supported by this device\n");
606     }
607
608     if (caps.dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_FOGTABLE)
609     {
610         /* A simple fog + non-identity world matrix test */
611         hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_WORLD, (D3DMATRIX *)world_mat1);
612         ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %#08x\n", hr);
613
614         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGTABLEMODE, D3DFOG_LINEAR);
615         ok(hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR returned %#08x\n", hr);
616         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGVERTEXMODE, D3DFOG_NONE);
617         ok(hr == D3D_OK, "Turning off vertex fog returned %#08x\n", hr);
618
619         hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
620         ok(hr == D3D_OK, "IDirect3DDevice7_Clear returned %#08x\n", hr);
621
622         if (IDirect3DDevice7_BeginScene(device) == D3D_OK)
623         {
624             hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST,
625                     D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR, far_quad1, 4, Indices, 6, 0);
626             ok(hr == D3D_OK, "DrawIndexedPrimitive returned %#08x\n", hr);
627
628             hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST,
629                     D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR, far_quad2, 4, Indices, 6, 0);
630             ok(hr == D3D_OK, "DrawIndexedPrimitive returned %#08x\n", hr);
631
632             hr = IDirect3DDevice7_EndScene(device);
633             ok(hr == D3D_OK, "EndScene returned %#08x\n", hr);
634         }
635         else
636         {
637             ok(FALSE, "BeginScene failed\n");
638         }
639
640         color = getPixelColor(device, 160, 360);
641         ok(color_match(color, 0x00ff0000, 4), "Unfogged quad has color %08x\n", color);
642         color = getPixelColor(device, 160, 120);
643         ok(color_match(color, 0x0000ff00, 1), "Fogged out quad has color %08x\n", color);
644
645         /* Test fog behavior with an orthogonal (but not identity) projection matrix */
646         hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_WORLD, (D3DMATRIX *)world_mat2);
647         ok(hr == D3D_OK, "SetTransform returned %#08x\n", hr);
648         hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, (D3DMATRIX *)proj_mat);
649         ok(hr == D3D_OK, "SetTransform returned %#08x\n", hr);
650
651         hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
652         ok(hr == D3D_OK, "Clear returned %#08x\n", hr);
653
654         if (IDirect3DDevice7_BeginScene(device) == D3D_OK)
655         {
656             hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST,
657                     D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR, untransformed_1, 4, Indices, 6, 0);
658             ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
659
660             hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST,
661                     D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR, untransformed_2, 4, Indices, 6, 0);
662             ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
663
664             hr = IDirect3DDevice7_EndScene(device);
665             ok(hr == D3D_OK, "EndScene returned %#08x\n", hr);
666         }
667         else
668         {
669             ok(FALSE, "BeginScene failed\n");
670         }
671
672         color = getPixelColor(device, 160, 360);
673         todo_wine ok(color_match(color, 0x00e51900, 4), "Partially fogged quad has color %08x\n", color);
674         color = getPixelColor(device, 160, 120);
675         ok(color_match(color, 0x0000ff00, 1), "Fogged out quad has color %08x\n", color);
676
677         hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_WORLD, (D3DMATRIX *)ident_mat);
678         ok(hr == D3D_OK, "SetTransform returned %#08x\n", hr);
679         hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, (D3DMATRIX *)ident_mat);
680         ok(hr == D3D_OK, "SetTransform returned %#08x\n", hr);
681     }
682     else
683     {
684         skip("D3DPRASTERCAPS_FOGTABLE not supported, skipping some fog tests\n");
685     }
686
687     /* Turn off the fog master switch to avoid confusing other tests */
688     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
689     ok(hr == D3D_OK, "Turning off fog calculations returned %08x\n", hr);
690 }
691
692 static void blt_test(IDirect3DDevice7 *device)
693 {
694     IDirectDrawSurface7 *backbuffer = NULL, *offscreen = NULL;
695     DDSURFACEDESC2 ddsd;
696     HRESULT hr;
697
698     memset(&ddsd, 0, sizeof(ddsd));
699     ddsd.dwSize = sizeof(ddsd);
700     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
701     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
702     ddsd.dwWidth = 640;
703     ddsd.dwHeight = 480;
704     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE;
705     hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &offscreen, NULL);
706     ok(hr == D3D_OK, "Creating the offscreen render target failed, hr = %08x\n", hr);
707
708     /* Offscreen blits with the same source as destination */
709     if(SUCCEEDED(hr))
710     {
711         RECT src_rect, dst_rect;
712
713         /* Blit the whole surface to itself */
714         hr = IDirectDrawSurface_Blt(offscreen, NULL, offscreen, NULL, 0, NULL);
715         ok(hr == DD_OK, "IDirectDrawSurface7_Blt returned %08x\n", hr);
716
717         /* Overlapped blit */
718         dst_rect.left = 0; dst_rect.right = 480;
719         dst_rect.top = 0; dst_rect.bottom = 480;
720         src_rect.left = 160; src_rect.right = 640;
721         src_rect.top = 0; src_rect.bottom = 480;
722         hr = IDirectDrawSurface_Blt(offscreen, &dst_rect, offscreen, &src_rect, 0, NULL);
723         ok(hr == DD_OK, "IDirectDrawSurface7_Blt returned %08x\n", hr);
724
725         /* Overlapped blit, flip-y through source rectangle (not allowed) */
726         dst_rect.left = 0; dst_rect.right = 480;
727         dst_rect.top = 0; dst_rect.bottom = 480;
728         src_rect.left = 160; src_rect.right = 640;
729         src_rect.top = 480; src_rect.bottom = 0;
730         hr = IDirectDrawSurface_Blt(offscreen, &dst_rect, offscreen, &src_rect, 0, NULL);
731         ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface7_Blt returned %08x\n", hr);
732
733         /* Overlapped blit, with shrinking in x */
734         dst_rect.left = 0; dst_rect.right = 480;
735         dst_rect.top = 0; dst_rect.bottom = 480;
736         src_rect.left = 160; src_rect.right = 480;
737         src_rect.top = 0; src_rect.bottom = 480;
738         hr = IDirectDrawSurface_Blt(offscreen, &dst_rect, offscreen, &src_rect, 0, NULL);
739         ok(hr == DD_OK, "IDirectDrawSurface7_Blt returned %08x\n", hr);
740     }
741
742     hr = IDirect3DDevice7_GetRenderTarget(device, &backbuffer);
743     ok(hr == D3D_OK, "Unable to obtain a surface pointer to the backbuffer, hr = %08x\n", hr);
744
745     /* backbuffer ==> texture blits */
746     if(SUCCEEDED(hr) && offscreen)
747     {
748         RECT src_rect, dst_rect;
749
750         /* backbuffer ==> texture, src_rect=NULL, dst_rect=NULL, no scaling */
751         hr = IDirectDrawSurface_Blt(offscreen, NULL, backbuffer, NULL, 0, NULL);
752         ok(hr == DD_OK, "fullscreen Blt from backbuffer => texture failed with hr = %08x\n", hr);
753
754         /* backbuffer ==> texture, full surface blits, no scaling */
755         dst_rect.left = 0; dst_rect.right = 640;
756         dst_rect.top = 0; dst_rect.bottom = 480;
757         src_rect.left = 0; src_rect.right = 640;
758         src_rect.top = 0; src_rect.bottom = 480;
759         hr = IDirectDrawSurface_Blt(offscreen, &dst_rect, backbuffer, &src_rect, 0, NULL);
760         ok(hr == DD_OK, "fullscreen Blt from backbuffer => texture failed with hr = %08x\n", hr);
761
762         /* backbuffer ==> texture, flip in y-direction through source rectangle, no scaling (allowed) */
763         dst_rect.left = 0; dst_rect.right = 640;
764         dst_rect.top = 480; dst_rect.top = 0;
765         src_rect.left = 0; src_rect.right = 640;
766         src_rect.top = 0; src_rect.bottom = 480;
767         hr = IDirectDrawSurface_Blt(offscreen, &dst_rect, backbuffer, &src_rect, 0, NULL);
768         ok(hr == DD_OK, "backbuffer => texture flip-y src_rect failed with hr = %08x\n", hr);
769
770         /* backbuffer ==> texture, flip in x-direction through source rectangle, no scaling (not allowed) */
771         dst_rect.left = 640; dst_rect.right = 0;
772         dst_rect.top = 0; dst_rect.top = 480;
773         src_rect.left = 0; src_rect.right = 640;
774         src_rect.top = 0; src_rect.bottom = 480;
775         hr = IDirectDrawSurface_Blt(offscreen, &dst_rect, backbuffer, &src_rect, 0, NULL);
776         ok(hr == DDERR_INVALIDRECT, "backbuffer => texture flip-x src_rect failed with hr = %08x\n", hr);
777
778         /* backbuffer ==> texture, flip in y-direction through destination rectangle (not allowed) */
779         dst_rect.left = 0; dst_rect.right = 640;
780         dst_rect.top = 0; dst_rect.top = 480;
781         src_rect.left = 0; src_rect.right = 640;
782         src_rect.top = 480; src_rect.bottom = 0;
783         hr = IDirectDrawSurface_Blt(offscreen, &dst_rect, backbuffer, &src_rect, 0, NULL);
784         ok(hr == DDERR_INVALIDRECT, "backbuffer => texture flip-y dst_rect failed with hr = %08x\n", hr);
785
786         /* backbuffer ==> texture, flip in x-direction through destination rectangle, no scaling (not allowed) */
787         dst_rect.left = 0; dst_rect.right = 640;
788         dst_rect.top = 0; dst_rect.top = 480;
789         src_rect.left = 640; src_rect.right = 0;
790         src_rect.top = 0; src_rect.bottom = 480;
791         hr = IDirectDrawSurface_Blt(offscreen, &dst_rect, backbuffer, &src_rect, 0, NULL);
792         ok(hr == DDERR_INVALIDRECT, "backbuffer => texture flip-x dst_rect failed with hr = %08x\n", hr);
793     }
794
795     if(offscreen) IDirectDrawSurface7_Release(offscreen);
796     if(backbuffer) IDirectDrawSurface7_Release(backbuffer);
797 }
798
799 static void offscreen_test(IDirect3DDevice7 *device)
800 {
801     HRESULT hr;
802     IDirectDrawSurface7 *backbuffer = NULL, *offscreen = NULL;
803     DWORD color;
804     DDSURFACEDESC2 ddsd;
805
806     static float quad[][5] = {
807         {-0.5f, -0.5f, 0.1f, 0.0f, 0.0f},
808         {-0.5f,  0.5f, 0.1f, 0.0f, 1.0f},
809         { 0.5f, -0.5f, 0.1f, 1.0f, 0.0f},
810         { 0.5f,  0.5f, 0.1f, 1.0f, 1.0f},
811     };
812
813     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
814     ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
815
816     memset(&ddsd, 0, sizeof(ddsd));
817     ddsd.dwSize = sizeof(ddsd);
818     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
819     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
820     ddsd.dwWidth = 128;
821     ddsd.dwHeight = 128;
822     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE;
823     hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &offscreen, NULL);
824     ok(hr == D3D_OK, "Creating the offscreen render target failed, hr = %08x\n", hr);
825     if(!offscreen) {
826         goto out;
827     }
828
829     hr = IDirect3DDevice7_GetRenderTarget(device, &backbuffer);
830     ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
831     if(!backbuffer) {
832         goto out;
833     }
834
835     hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
836     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %08x\n", hr);
837     hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
838     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %08x\n", hr);
839     hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DFILTER_NEAREST);
840     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (0x%08x)\n", hr);
841     hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DFILTER_NEAREST);
842     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (0x%08x)\n", hr);
843     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
844     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned hr = %08x\n", hr);
845
846     if (refdevice) {
847         win_skip("Tests would crash on W2K with a refdevice\n");
848         goto out;
849     }
850
851     if(IDirect3DDevice7_BeginScene(device) == D3D_OK) {
852         hr = IDirect3DDevice7_SetRenderTarget(device, offscreen, 0);
853         ok(hr == D3D_OK, "SetRenderTarget failed, hr = %08x\n", hr);
854         set_viewport_size(device);
855         hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
856         ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
857
858         /* Draw without textures - Should result in a white quad */
859         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEX1, quad, 4, 0);
860         ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
861
862         hr = IDirect3DDevice7_SetRenderTarget(device, backbuffer, 0);
863         ok(hr == D3D_OK, "SetRenderTarget failed, hr = %08x\n", hr);
864         set_viewport_size(device);
865
866         hr = IDirect3DDevice7_SetTexture(device, 0, offscreen);
867         ok(hr == D3D_OK, "SetTexture failed, %08x\n", hr);
868
869         /* This time with the texture */
870         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEX1, quad, 4, 0);
871         ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
872
873         IDirect3DDevice7_EndScene(device);
874     }
875
876     /* Center quad - should be white */
877     color = getPixelColor(device, 320, 240);
878     ok(color == 0x00ffffff, "Offscreen failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
879     /* Some quad in the cleared part of the texture */
880     color = getPixelColor(device, 170, 240);
881     ok(color == 0x00ff00ff, "Offscreen failed: Got color 0x%08x, expected 0x00ff00ff.\n", color);
882     /* Part of the originally cleared back buffer */
883     color = getPixelColor(device, 10, 10);
884     ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
885     if(0) {
886         /* Lower left corner of the screen, where back buffer offscreen rendering draws the offscreen texture.
887          * It should be red, but the offscreen texture may leave some junk there. Not tested yet. Depending on
888          * the offscreen rendering mode this test would succeed or fail
889          */
890         color = getPixelColor(device, 10, 470);
891         ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
892     }
893
894 out:
895     hr = IDirect3DDevice7_SetTexture(device, 0, NULL);
896     ok(SUCCEEDED(hr), "IDirect3DDevice7_SetTexture returned %#x.\n", hr);
897
898     /* restore things */
899     if(backbuffer) {
900         hr = IDirect3DDevice7_SetRenderTarget(device, backbuffer, 0);
901         ok(SUCCEEDED(hr), "IDirect3DDevice7_SetRenderTarget returned %#x.\n", hr);
902         IDirectDrawSurface7_Release(backbuffer);
903     }
904     if(offscreen) {
905         IDirectDrawSurface7_Release(offscreen);
906     }
907 }
908
909 static void alpha_test(IDirect3DDevice7 *device)
910 {
911     HRESULT hr;
912     IDirectDrawSurface7 *backbuffer = NULL, *offscreen = NULL;
913     DWORD color, red, green, blue;
914     DDSURFACEDESC2 ddsd;
915
916     struct vertex quad1[] =
917     {
918         {-1.0f, -1.0f,   0.1f,                          0x4000ff00},
919         {-1.0f,  0.0f,   0.1f,                          0x4000ff00},
920         { 1.0f, -1.0f,   0.1f,                          0x4000ff00},
921         { 1.0f,  0.0f,   0.1f,                          0x4000ff00},
922     };
923     struct vertex quad2[] =
924     {
925         {-1.0f,  0.0f,   0.1f,                          0xc00000ff},
926         {-1.0f,  1.0f,   0.1f,                          0xc00000ff},
927         { 1.0f,  0.0f,   0.1f,                          0xc00000ff},
928         { 1.0f,  1.0f,   0.1f,                          0xc00000ff},
929     };
930     static float composite_quad[][5] = {
931         { 0.0f, -1.0f, 0.1f, 0.0f, 1.0f},
932         { 0.0f,  1.0f, 0.1f, 0.0f, 0.0f},
933         { 1.0f, -1.0f, 0.1f, 1.0f, 1.0f},
934         { 1.0f,  1.0f, 0.1f, 1.0f, 0.0f},
935     };
936
937     /* Clear the render target with alpha = 0.5 */
938     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
939     ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
940
941     memset(&ddsd, 0, sizeof(ddsd));
942     ddsd.dwSize = sizeof(ddsd);
943     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
944     ddsd.dwWidth = 128;
945     ddsd.dwHeight = 128;
946     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE;
947     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
948     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount      = 32;
949     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask         = 0x00ff0000;
950     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask         = 0x0000ff00;
951     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask         = 0x000000ff;
952     U5(U4(ddsd).ddpfPixelFormat).dwRGBAlphaBitMask  = 0xff000000;
953     hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &offscreen, NULL);
954     ok(hr == D3D_OK, "Creating the offscreen render target failed, hr = %08x\n", hr);
955     if(!offscreen) {
956         goto out;
957     }
958     hr = IDirect3DDevice7_GetRenderTarget(device, &backbuffer);
959     ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
960     if(!backbuffer) {
961         goto out;
962     }
963
964     hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
965     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %08x\n", hr);
966     hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
967     ok(hr == D3D_OK, "SetTextureStageState failed, hr = %08x\n", hr);
968     hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DFILTER_NEAREST);
969     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (0x%08x)\n", hr);
970     hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DFILTER_NEAREST);
971     ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (0x%08x)\n", hr);
972
973     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
974     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
975
976     if (refdevice) {
977         win_skip("Tests would crash on W2K with a refdevice\n");
978         goto out;
979     }
980
981     if(IDirect3DDevice7_BeginScene(device) == D3D_OK) {
982
983         /* Draw two quads, one with src alpha blending, one with dest alpha blending. The
984          * SRCALPHA / INVSRCALPHA blend doesn't give any surprises. Colors are blended based on
985          * the input alpha
986          *
987          * The DESTALPHA / INVDESTALPHA do not "work" on the regular buffer because there is no alpha.
988          * They give essentially ZERO and ONE blend factors
989          */
990         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
991         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
992         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
993         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
994         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE, quad1, 4, 0);
995         ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
996
997         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_DESTALPHA);
998         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
999         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVDESTALPHA);
1000         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
1001         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE, quad2, 4, 0);
1002         ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
1003
1004         /* Switch to the offscreen buffer, and redo the testing. SRCALPHA and DESTALPHA. The offscreen buffer
1005          * has a alpha channel on its own. Clear the offscreen buffer with alpha = 0.5 again, then draw the
1006          * quads again. The SRCALPHA/INVSRCALPHA doesn't give any surprises, but the DESTALPHA/INVDESTALPHA
1007          * blending works as supposed now - blend factor is 0.5 in both cases, not 0.75 as from the input
1008          * vertices
1009          */
1010         hr = IDirect3DDevice7_SetRenderTarget(device, offscreen, 0);
1011         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderTarget failed, hr = %08x\n", hr);
1012         set_viewport_size(device);
1013         hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
1014         ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
1015
1016         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
1017         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
1018         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
1019         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
1020         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE, quad1, 4, 0);
1021         ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
1022
1023         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_DESTALPHA);
1024         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
1025         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVDESTALPHA);
1026         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
1027         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE, quad2, 4, 0);
1028         ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
1029
1030         hr = IDirect3DDevice7_SetRenderTarget(device, backbuffer, 0);
1031         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderTarget failed, hr = %08x\n", hr);
1032         set_viewport_size(device);
1033
1034         /* Render the offscreen texture onto the frame buffer to be able to compare it regularly.
1035          * Disable alpha blending for the final composition
1036          */
1037         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
1038         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed, hr = %08x\n", hr);
1039
1040         hr = IDirect3DDevice7_SetTexture(device, 0, offscreen);
1041         ok(hr == D3D_OK, "IDirect3DDevice7_SetTexture failed, hr = %08x\n", hr);
1042         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEX1, composite_quad, 4, 0);
1043         ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
1044         hr = IDirect3DDevice7_SetTexture(device, 0, NULL);
1045         ok(hr == D3D_OK, "IDirect3DDevice7_SetTexture failed, hr = %08x\n", hr);
1046
1047         hr = IDirect3DDevice7_EndScene(device);
1048         ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed, hr = %08x\n", hr);
1049     }
1050
1051     color = getPixelColor(device, 160, 360);
1052     red =   (color & 0x00ff0000) >> 16;
1053     green = (color & 0x0000ff00) >>  8;
1054     blue =  (color & 0x000000ff);
1055     ok(red >= 0xbe && red <= 0xc0 && green >= 0x39 && green <= 0x41 && blue == 0x00,
1056        "SRCALPHA on frame buffer returned color 0x%08x, expected 0x00bf4000\n", color);
1057
1058     color = getPixelColor(device, 160, 120);
1059     red =   (color & 0x00ff0000) >> 16;
1060     green = (color & 0x0000ff00) >>  8;
1061     blue =  (color & 0x000000ff);
1062     ok(red == 0x00 && green == 0x00 && blue >= 0xfe && blue <= 0xff ,
1063        "DSTALPHA on frame buffer returned color 0x%08x, expected 0x000000ff\n", color);
1064
1065     color = getPixelColor(device, 480, 360);
1066     red =   (color & 0x00ff0000) >> 16;
1067     green = (color & 0x0000ff00) >>  8;
1068     blue =  (color & 0x000000ff);
1069     ok(red >= 0xbe && red <= 0xc0 && green >= 0x39 && green <= 0x41 && blue == 0x00,
1070        "SRCALPHA on texture returned color 0x%08x, expected 0x00bf4000\n", color);
1071
1072     color = getPixelColor(device, 480, 120);
1073     red =   (color & 0x00ff0000) >> 16;
1074     green = (color & 0x0000ff00) >>  8;
1075     blue =  (color & 0x000000ff);
1076     ok(red >= 0x7e && red <= 0x81 && green == 0x00 && blue >= 0x7e && blue <= 0x81,
1077        "DSTALPHA on texture returned color 0x%08x, expected 0x00800080\n", color);
1078
1079     out:
1080     if(offscreen) IDirectDrawSurface7_Release(offscreen);
1081     if(backbuffer) IDirectDrawSurface7_Release(backbuffer);
1082 }
1083
1084 static void rhw_zero_test(IDirect3DDevice7 *device)
1085 {
1086 /* Test if it will render a quad correctly when vertex rhw = 0 */
1087     HRESULT hr;
1088     DWORD color;
1089
1090     struct {
1091         float x, y, z;
1092         float rhw;
1093         DWORD diffuse;
1094         } quad1[] =
1095     {
1096         {0, 100, 0, 0, 0xffffffff},
1097         {0, 0, 0, 0, 0xffffffff},
1098         {100, 100, 0, 0, 0xffffffff},
1099         {100, 0, 0, 0, 0xffffffff},
1100     };
1101
1102     /* Clear to black */
1103     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0, 0.0, 0);
1104     ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
1105
1106     hr = IDirect3DDevice7_BeginScene(device);
1107     ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
1108
1109     if (SUCCEEDED(hr)) {
1110         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, quad1, 4, 0);
1111         ok(hr == D3D_OK, "DrawPrimitive failed, hr = %08x\n", hr);
1112
1113         hr = IDirect3DDevice7_EndScene(device);
1114         ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed, hr = %08x\n", hr);
1115     }
1116
1117     color = getPixelColor(device, 5, 5);
1118     ok(color == 0xffffff ||
1119        broken(color == 0), /* VMware */
1120        "Got color %08x, expected 00ffffff\n", color);
1121
1122     color = getPixelColor(device, 105, 105);
1123     ok(color == 0, "Got color %08x, expected 00000000\n", color);
1124 }
1125
1126 static BOOL D3D1_createObjects(void)
1127 {
1128     WNDCLASS wc = {0};
1129     HRESULT hr;
1130     DDSURFACEDESC ddsd;
1131     D3DEXECUTEBUFFERDESC exdesc;
1132     D3DVIEWPORT vp_data;
1133
1134     /* An IDirect3DDevice cannot be queryInterfaced from an IDirect3DDevice7 on windows */
1135     hr = DirectDrawCreate(NULL, &DirectDraw1, NULL);
1136
1137     ok(hr==DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreate returned: %x\n", hr);
1138     if (FAILED(hr)) {
1139         return FALSE;
1140     }
1141
1142     wc.lpfnWndProc = DefWindowProc;
1143     wc.lpszClassName = "texturemapblend_test_wc";
1144     RegisterClass(&wc);
1145     window = CreateWindow("texturemapblend_test_wc", "texturemapblend_test", WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
1146
1147     hr = IDirectDraw_SetCooperativeLevel(DirectDraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1148     ok(hr==DD_OK, "SetCooperativeLevel returned: %x\n", hr);
1149     if(FAILED(hr)) {
1150         return FALSE;
1151     }
1152
1153     hr = IDirectDraw_SetDisplayMode(DirectDraw1, 640, 480, 32);
1154     if(FAILED(hr)) {
1155         /* 24 bit is fine too */
1156         hr = IDirectDraw_SetDisplayMode(DirectDraw1, 640, 480, 24);
1157     }
1158     ok(hr==DD_OK || hr == DDERR_UNSUPPORTED, "SetDisplayMode returned: %x\n", hr);
1159     if (FAILED(hr)) {
1160         return FALSE;
1161     }
1162
1163     hr = IDirectDraw_QueryInterface(DirectDraw1, &IID_IDirect3D, (void**) &Direct3D1);
1164     ok(hr==DD_OK, "QueryInterface returned: %x\n", hr);
1165     if (FAILED(hr)) {
1166         return FALSE;
1167     }
1168
1169     memset(&ddsd, 0, sizeof(ddsd));
1170     ddsd.dwSize = sizeof(ddsd);
1171     ddsd.dwFlags = DDSD_CAPS;
1172     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE;
1173     hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &Surface1, NULL);
1174     ok(hr==DD_OK, "CreateSurface returned: %x\n", hr);
1175     if (FAILED(hr)) {
1176         return FALSE;
1177     }
1178
1179     hr = IDirectDrawSurface_QueryInterface(Surface1, &IID_IDirect3DHALDevice, (void **) &Direct3DDevice1);
1180     if(FAILED(hr)) {
1181         trace("Creating a HAL device failed, trying Ref\n");
1182         hr = IDirectDrawSurface_QueryInterface(Surface1, &IID_IDirect3DRefDevice, (void **) &Direct3DDevice1);
1183     }
1184     ok(hr==D3D_OK, "Creating 3D device returned: %x\n", hr);
1185     if(FAILED(hr)) {
1186         return FALSE;
1187     }
1188
1189     hr = IDirect3D_CreateViewport(Direct3D1, &Viewport, NULL);
1190     ok(hr == D3D_OK, "IDirect3D_CreateViewport failed: %08x\n", hr);
1191     if (FAILED(hr)) {
1192         return FALSE;
1193     }
1194
1195     hr = IDirect3DViewport_Initialize(Viewport, Direct3D1);
1196     ok(hr == D3D_OK || hr == DDERR_ALREADYINITIALIZED, "IDirect3DViewport_Initialize returned %08x\n", hr);
1197     hr = IDirect3DDevice_AddViewport(Direct3DDevice1, Viewport);
1198     ok(hr == D3D_OK, "IDirect3DDevice_AddViewport returned %08x\n", hr);
1199     vp_data.dwSize = sizeof(vp_data);
1200     vp_data.dwX = 0;
1201     vp_data.dwY = 0;
1202     vp_data.dwWidth = 640;
1203     vp_data.dwHeight = 480;
1204     vp_data.dvScaleX = 1;
1205     vp_data.dvScaleY = 1;
1206     vp_data.dvMaxX = 640;
1207     vp_data.dvMaxY = 480;
1208     vp_data.dvMinZ = 0;
1209     vp_data.dvMaxZ = 1;
1210     hr = IDirect3DViewport_SetViewport(Viewport, &vp_data);
1211     ok(hr == D3D_OK, "IDirect3DViewport_SetViewport returned %08x\n", hr);
1212
1213     memset(&exdesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
1214     exdesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
1215     exdesc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1216     exdesc.dwBufferSize = 512;
1217     exdesc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1218     hr = IDirect3DDevice_CreateExecuteBuffer(Direct3DDevice1, &exdesc, &ExecuteBuffer, NULL);
1219     ok(hr == D3D_OK, "IDirect3DDevice_CreateExecuteBuffer failed with %08x\n", hr);
1220     if (FAILED(hr)) {
1221         return FALSE;
1222     }
1223
1224     return TRUE;
1225 }
1226
1227 static void D3D1_releaseObjects(void)
1228 {
1229     if(ExecuteBuffer) IDirect3DExecuteBuffer_Release(ExecuteBuffer);
1230     if(Surface1) IDirectDrawSurface_Release(Surface1);
1231     if(Viewport) IDirect3DViewport_Release(Viewport);
1232     if(Direct3DDevice1) IDirect3DDevice_Release(Direct3DDevice1);
1233     if(Direct3D1) IDirect3D_Release(Direct3D1);
1234     if(DirectDraw1) IDirectDraw_Release(DirectDraw1);
1235     if(window) DestroyWindow(window);
1236 }
1237
1238 static DWORD D3D1_getPixelColor(IDirectDraw *DirectDraw1, IDirectDrawSurface *Surface, UINT x, UINT y)
1239 {
1240     DWORD ret;
1241     HRESULT hr;
1242     DDSURFACEDESC ddsd;
1243     RECT rectToLock = {x, y, x+1, y+1};
1244     IDirectDrawSurface *surf = NULL;
1245
1246     /* Some implementations seem to dislike direct locking on the front buffer. Thus copy the front buffer
1247      * to an offscreen surface and lock it instead of the front buffer
1248      */
1249     memset(&ddsd, 0, sizeof(ddsd));
1250     ddsd.dwSize = sizeof(ddsd);
1251     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
1252     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
1253     ddsd.dwWidth = 640;
1254     ddsd.dwHeight = 480;
1255     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
1256     hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &surf, NULL);
1257     ok(hr == DD_OK, "IDirectDraw_CreateSurface failed with %08x\n", hr);
1258     if(!surf)
1259     {
1260         trace("cannot create helper surface\n");
1261         return 0xdeadbeef;
1262     }
1263
1264     memset(&ddsd, 0, sizeof(ddsd));
1265     ddsd.dwSize = sizeof(ddsd);
1266     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
1267
1268     hr = IDirectDrawSurface_BltFast(surf, 0, 0, Surface, NULL, 0);
1269     ok(hr == DD_OK, "IDirectDrawSurface_BltFast returned %08x\n", hr);
1270     if(FAILED(hr))
1271     {
1272         trace("Cannot blit\n");
1273         ret = 0xdeadbee;
1274         goto out;
1275     }
1276
1277     hr = IDirectDrawSurface_Lock(surf, &rectToLock, &ddsd, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
1278     if(FAILED(hr))
1279     {
1280         trace("Can't lock the offscreen surface, hr=%08x\n", hr);
1281         ret = 0xdeadbeec;
1282         goto out;
1283     }
1284
1285     /* Remove the X channel for now. DirectX and OpenGL have different ideas how to treat it apparently, and it isn't
1286      * really important for these tests
1287      */
1288     ret = ((DWORD *) ddsd.lpSurface)[0] & 0x00ffffff;
1289     hr = IDirectDrawSurface_Unlock(surf, NULL);
1290     if(FAILED(hr))
1291     {
1292         trace("Can't unlock the offscreen surface, hr=%08x\n", hr);
1293     }
1294
1295 out:
1296     IDirectDrawSurface_Release(surf);
1297     return ret;
1298 }
1299
1300 #define EXEBUF_START_RENDER_STATES(count, ptr) do {\
1301                          ((D3DINSTRUCTION*)(ptr))->bOpcode = D3DOP_STATERENDER;\
1302                          ((D3DINSTRUCTION*)(ptr))->bSize = sizeof(D3DSTATE);\
1303                          ((D3DINSTRUCTION*)(ptr))->wCount = count;\
1304                          ptr = ((D3DINSTRUCTION*)(ptr))+1; } while (0)
1305
1306 #define EXEBUF_PUT_RENDER_STATE(state, value, ptr) do {\
1307                          U1(*((D3DSTATE*)(ptr))).drstRenderStateType = state; \
1308                          U2(*((D3DSTATE*)(ptr))).dwArg[0] = value; \
1309                          ptr = ((D3DSTATE*)(ptr))+1; } while (0)
1310
1311 #define EXEBUF_PUT_PROCESSVERTICES(nvertices, ptr) do {\
1312                          ((D3DINSTRUCTION*)(ptr))->bOpcode = D3DOP_PROCESSVERTICES;\
1313                          ((D3DINSTRUCTION*)(ptr))->bSize = sizeof(D3DPROCESSVERTICES);\
1314                          ((D3DINSTRUCTION*)(ptr))->wCount = 1;\
1315                          ptr = ((D3DINSTRUCTION*)(ptr))+1;\
1316                          ((D3DPROCESSVERTICES*)(ptr))->dwFlags = D3DPROCESSVERTICES_COPY;\
1317                          ((D3DPROCESSVERTICES*)(ptr))->wStart = 0;\
1318                          ((D3DPROCESSVERTICES*)(ptr))->wDest = 0;\
1319                          ((D3DPROCESSVERTICES*)(ptr))->dwCount = nvertices;\
1320                          ((D3DPROCESSVERTICES*)(ptr))->dwReserved = 0;\
1321                          ptr = ((D3DPROCESSVERTICES*)(ptr))+1; } while (0)
1322
1323 #define EXEBUF_END(ptr) do {\
1324                          ((D3DINSTRUCTION*)(ptr))->bOpcode = D3DOP_EXIT;\
1325                          ((D3DINSTRUCTION*)(ptr))->bSize = 0;\
1326                          ((D3DINSTRUCTION*)(ptr))->wCount = 0;\
1327                          ptr = ((D3DINSTRUCTION*)(ptr))+1; } while (0)
1328
1329 #define EXEBUF_PUT_QUAD(base_idx, ptr) do {\
1330                          ((D3DINSTRUCTION*)(ptr))->bOpcode = D3DOP_TRIANGLE;\
1331                          ((D3DINSTRUCTION*)(ptr))->bSize = sizeof(D3DTRIANGLE);\
1332                          ((D3DINSTRUCTION*)(ptr))->wCount = 2;\
1333                          ptr = ((D3DINSTRUCTION*)(ptr))+1;\
1334                          U1(*((D3DTRIANGLE*)(ptr))).v1 = base_idx;\
1335                          U2(*((D3DTRIANGLE*)(ptr))).v2 = (base_idx) + 1; \
1336                          U3(*((D3DTRIANGLE*)(ptr))).v3 = (base_idx) + 3; \
1337                          ((D3DTRIANGLE*)(ptr))->wFlags = 0;\
1338                          ptr = ((D3DTRIANGLE*)ptr)+1;\
1339                          U1(*((D3DTRIANGLE*)(ptr))).v1 = (base_idx) + 1; \
1340                          U2(*((D3DTRIANGLE*)(ptr))).v2 = (base_idx) + 2; \
1341                          U3(*((D3DTRIANGLE*)(ptr))).v3 = (base_idx) + 3; \
1342                          ((D3DTRIANGLE*)(ptr))->wFlags = 0;\
1343                          ptr = ((D3DTRIANGLE*)(ptr))+1;\
1344                         } while (0)
1345
1346 static HRESULT CALLBACK TextureFormatEnumCallback(LPDDSURFACEDESC lpDDSD, LPVOID lpContext)
1347 {
1348     if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
1349         *(BOOL*)lpContext = TRUE;
1350     }
1351
1352     return DDENUMRET_OK;
1353 }
1354
1355 static void D3D1_TextureMapBlendTest(void)
1356 {
1357     HRESULT hr;
1358     DDSURFACEDESC ddsd;
1359     D3DEXECUTEBUFFERDESC exdesc;
1360     D3DEXECUTEDATA exdata;
1361     DDBLTFX ddbltfx;
1362     RECT rect = { 0, 0, 64, 128 };
1363     DWORD color, red, blue, green;
1364     void *exe_buffer_ptr;
1365     DWORD exe_length;
1366     D3DTEXTUREHANDLE htex;
1367     DDCOLORKEY clrKey;
1368     IDirectDrawSurface *TexSurface = NULL;
1369     IDirect3DTexture *Texture = NULL;
1370     IDirectDrawPalette *Palette = NULL;
1371     PALETTEENTRY table1[256];
1372     BOOL p8_textures_supported = FALSE;
1373
1374     struct {
1375         float x, y, z;
1376         float rhw;
1377         DWORD diffuse;
1378         DWORD specular;
1379         float tu, tv;
1380         } test1_quads[] =
1381     {
1382           {0.0f,   0.0f,     0.0f, 1.0f, 0xffffffff, 0, 0.0f, 0.0f},
1383           {640.0f, 0.0f,     0.0f, 1.0f, 0xffffffff, 0, 1.0f, 0.0f},
1384           {640.0f, 240.0f,   0.0f, 1.0f, 0xffffffff, 0, 1.0f, 1.0f},
1385           {0.0f,   240.0f,   0.0f, 1.0f, 0xffffffff, 0, 0.0f, 1.0f},
1386           {0.0f,   240.0f,   0.0f, 1.0f, 0x80ffffff, 0, 0.0f, 0.0f},
1387           {640.0f, 240.0f,   0.0f, 1.0f, 0x80ffffff, 0, 1.0f, 0.0f},
1388           {640.0f, 480.0f,   0.0f, 1.0f, 0x80ffffff, 0, 1.0f, 1.0f},
1389           {0.0f,   480.0f,   0.0f, 1.0f, 0x80ffffff, 0, 0.0f, 1.0f}
1390     },  test2_quads[] =
1391           {
1392           {0.0f,   0.0f,     0.0f, 1.0f, 0x00ff0080, 0, 0.0f, 0.0f},
1393           {640.0f, 0.0f,     0.0f, 1.0f, 0x00ff0080, 0, 1.0f, 0.0f},
1394           {640.0f, 240.0f,   0.0f, 1.0f, 0x00ff0080, 0, 1.0f, 1.0f},
1395           {0.0f,   240.0f,   0.0f, 1.0f, 0x00ff0080, 0, 0.0f, 1.0f},
1396           {0.0f,   240.0f,   0.0f, 1.0f, 0x008000ff, 0, 0.0f, 0.0f},
1397           {640.0f, 240.0f,   0.0f, 1.0f, 0x008000ff, 0, 1.0f, 0.0f},
1398           {640.0f, 480.0f,   0.0f, 1.0f, 0x008000ff, 0, 1.0f, 1.0f},
1399           {0.0f,   480.0f,   0.0f, 1.0f, 0x008000ff, 0, 0.0f, 1.0f}
1400     };
1401
1402     /* 1) Test alpha with DDPF_ALPHAPIXELS texture - should be taken from texture alpha channel*/
1403     memset (&ddsd, 0, sizeof (ddsd));
1404     ddsd.dwSize = sizeof (ddsd);
1405     ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
1406     ddsd.dwHeight = 128;
1407     ddsd.dwWidth = 128;
1408     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1409     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
1410     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1411     U1(ddsd.ddpfPixelFormat).dwRGBBitCount      = 32;
1412     U2(ddsd.ddpfPixelFormat).dwRBitMask         = 0x00ff0000;
1413     U3(ddsd.ddpfPixelFormat).dwGBitMask         = 0x0000ff00;
1414     U4(ddsd.ddpfPixelFormat).dwBBitMask         = 0x000000ff;
1415     U5(ddsd.ddpfPixelFormat).dwRGBAlphaBitMask  = 0xff000000;
1416     hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &TexSurface, NULL);
1417     ok(hr==D3D_OK, "CreateSurface returned: %x\n", hr);
1418     if (FAILED(hr)) {
1419         skip("IDirectDraw_CreateSurface failed; skipping further tests\n");
1420         goto out;
1421     }
1422
1423     hr = IDirectDrawSurface_QueryInterface(TexSurface, &IID_IDirect3DTexture,
1424                 (void *)&Texture);
1425     ok(hr==D3D_OK, "IDirectDrawSurface_QueryInterface returned: %x\n", hr);
1426     if (FAILED(hr)) {
1427         skip("Can't get IDirect3DTexture interface; skipping further tests\n");
1428         goto out;
1429     }
1430
1431     memset(&ddbltfx, 0, sizeof(ddbltfx));
1432     ddbltfx.dwSize = sizeof(ddbltfx);
1433     U5(ddbltfx).dwFillColor = 0;
1434     hr = IDirectDrawSurface_Blt(Surface1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1435     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1436
1437     U5(ddbltfx).dwFillColor = 0xff0000ff;
1438     hr = IDirectDrawSurface_Blt(TexSurface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1439     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1440     U5(ddbltfx).dwFillColor = 0x800000ff;
1441     hr = IDirectDrawSurface_Blt(TexSurface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1442     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1443
1444     memset(&exdesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
1445     exdesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
1446     hr = IDirect3DExecuteBuffer_Lock(ExecuteBuffer, &exdesc);
1447     ok(hr == D3D_OK, "IDirect3DExecuteBuffer_Lock failed with %08x\n", hr);
1448     if (FAILED(hr)) {
1449         skip("IDirect3DExecuteBuffer_Lock failed; skipping further tests\n");
1450         goto out;
1451     }
1452
1453     memcpy(exdesc.lpData, test1_quads, sizeof(test1_quads));
1454
1455     exe_buffer_ptr = sizeof(test1_quads) + (char*)exdesc.lpData;
1456
1457     EXEBUF_PUT_PROCESSVERTICES(8, exe_buffer_ptr);
1458
1459     EXEBUF_START_RENDER_STATES(12, exe_buffer_ptr);
1460     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_CULLMODE,         D3DCULL_NONE,              exe_buffer_ptr);
1461     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_ZENABLE,          FALSE,                     exe_buffer_ptr);
1462     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_FOGENABLE,        FALSE,                     exe_buffer_ptr);
1463     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_SPECULARENABLE,   FALSE,                     exe_buffer_ptr);
1464     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_TEXTUREMAG,       D3DFILTER_NEAREST,         exe_buffer_ptr);
1465     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_TEXTUREMIN,       D3DFILTER_NEAREST,         exe_buffer_ptr);
1466     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_FILLMODE  ,       D3DFILL_SOLID,             exe_buffer_ptr);
1467     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_SRCBLEND,         D3DBLEND_SRCALPHA,         exe_buffer_ptr);
1468     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_DESTBLEND,        D3DBLEND_INVSRCALPHA,      exe_buffer_ptr);
1469     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_ALPHABLENDENABLE, TRUE,                      exe_buffer_ptr);
1470     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_TEXTUREMAPBLEND,  D3DTBLEND_MODULATE,        exe_buffer_ptr);
1471     hr = IDirect3DTexture_GetHandle(Texture, Direct3DDevice1, &htex);
1472     ok(hr == D3D_OK, "IDirect3DTexture_GetHandle failed with %08x\n", hr);
1473     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_TEXTUREHANDLE,  htex, exe_buffer_ptr);
1474
1475     EXEBUF_PUT_QUAD(0, exe_buffer_ptr);
1476     EXEBUF_PUT_QUAD(4, exe_buffer_ptr);
1477
1478     EXEBUF_END(exe_buffer_ptr);
1479
1480     exe_length = ((char*)exe_buffer_ptr - (char*)exdesc.lpData) - sizeof(test1_quads);
1481
1482     hr = IDirect3DExecuteBuffer_Unlock(ExecuteBuffer);
1483     if (FAILED(hr)) {
1484         trace("IDirect3DExecuteBuffer_Unlock failed with %08x\n", hr);
1485     }
1486
1487     memset(&exdata, 0, sizeof(exdata));
1488     exdata.dwSize = sizeof(exdata);
1489     exdata.dwVertexCount = 8;
1490     exdata.dwInstructionOffset = sizeof(test1_quads);
1491     exdata.dwInstructionLength = exe_length;
1492     hr = IDirect3DExecuteBuffer_SetExecuteData(ExecuteBuffer, &exdata);
1493     ok(hr == D3D_OK, "IDirect3DExecuteBuffer_SetExecuteData failed with %08x\n", hr);
1494
1495     hr = IDirect3DDevice_BeginScene(Direct3DDevice1);
1496     ok(hr == D3D_OK, "IDirect3DDevice3_BeginScene failed with %08x\n", hr);
1497
1498     if (SUCCEEDED(hr)) {
1499         hr = IDirect3DDevice_Execute(Direct3DDevice1, ExecuteBuffer, Viewport, D3DEXECUTE_UNCLIPPED);
1500         ok(hr == D3D_OK, "IDirect3DDevice_Execute failed, hr = %08x\n", hr);
1501         hr = IDirect3DDevice_EndScene(Direct3DDevice1);
1502         ok(hr == D3D_OK, "IDirect3DDevice3_EndScene failed, hr = %08x\n", hr);
1503     }
1504
1505     color = D3D1_getPixelColor(DirectDraw1, Surface1, 5, 5);
1506     red =   (color & 0x00ff0000) >> 16;
1507     green = (color & 0x0000ff00) >>  8;
1508     blue =  (color & 0x000000ff);
1509     ok(red == 0 &&  green == 0 && blue >= 0x7e && blue <= 0x82, "Got color %08x, expected 00000080 or near\n", color);
1510
1511     color = D3D1_getPixelColor(DirectDraw1, Surface1, 400, 5);
1512     red =   (color & 0x00ff0000) >> 16;
1513     green = (color & 0x0000ff00) >>  8;
1514     blue =  (color & 0x000000ff);
1515     ok(red == 0 &&  green == 0 && blue == 0xff, "Got color %08x, expected 000000ff or near\n", color);
1516
1517     color = D3D1_getPixelColor(DirectDraw1, Surface1, 5, 245);
1518     red =   (color & 0x00ff0000) >> 16;
1519     green = (color & 0x0000ff00) >>  8;
1520     blue =  (color & 0x000000ff);
1521     ok(red == 0 &&  green == 0 && blue >= 0x7e && blue <= 0x82, "Got color %08x, expected 00000080 or near\n", color);
1522
1523     color = D3D1_getPixelColor(DirectDraw1, Surface1, 400, 245);
1524     red =   (color & 0x00ff0000) >> 16;
1525     green = (color & 0x0000ff00) >>  8;
1526     blue =  (color & 0x000000ff);
1527     ok(red == 0 &&  green == 0 && blue == 0xff, "Got color %08x, expected 000000ff or near\n", color);
1528
1529     /* 2) Test alpha with texture that has no alpha channel - alpha should be taken from diffuse color */
1530     if(Texture) IDirect3DTexture_Release(Texture);
1531     Texture = NULL;
1532     if(TexSurface) IDirectDrawSurface_Release(TexSurface);
1533     TexSurface = NULL;
1534
1535     memset (&ddsd, 0, sizeof (ddsd));
1536     ddsd.dwSize = sizeof (ddsd);
1537     ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
1538     ddsd.dwHeight = 128;
1539     ddsd.dwWidth = 128;
1540     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1541     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
1542     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
1543     U1(ddsd.ddpfPixelFormat).dwRGBBitCount      = 32;
1544     U2(ddsd.ddpfPixelFormat).dwRBitMask         = 0x00ff0000;
1545     U3(ddsd.ddpfPixelFormat).dwGBitMask         = 0x0000ff00;
1546     U4(ddsd.ddpfPixelFormat).dwBBitMask         = 0x000000ff;
1547
1548     hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &TexSurface, NULL);
1549     ok(hr==D3D_OK, "CreateSurface returned: %x\n", hr);
1550     if (FAILED(hr)) {
1551         skip("IDirectDraw_CreateSurface failed; skipping further tests\n");
1552         goto out;
1553     }
1554
1555     hr = IDirectDrawSurface_QueryInterface(TexSurface, &IID_IDirect3DTexture,
1556                 (void *)&Texture);
1557     ok(hr==D3D_OK, "IDirectDrawSurface_QueryInterface returned: %x\n", hr);
1558     if (FAILED(hr)) {
1559         skip("Can't get IDirect3DTexture interface; skipping further tests\n");
1560         goto out;
1561     }
1562
1563     memset(&ddbltfx, 0, sizeof(ddbltfx));
1564     ddbltfx.dwSize = sizeof(ddbltfx);
1565     U5(ddbltfx).dwFillColor = 0;
1566     hr = IDirectDrawSurface_Blt(Surface1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1567     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1568
1569     U5(ddbltfx).dwFillColor = 0xff0000ff;
1570     hr = IDirectDrawSurface_Blt(TexSurface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1571     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1572     U5(ddbltfx).dwFillColor = 0x800000ff;
1573     hr = IDirectDrawSurface_Blt(TexSurface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1574     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1575
1576     memset(&exdesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
1577     exdesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
1578     hr = IDirect3DExecuteBuffer_Lock(ExecuteBuffer, &exdesc);
1579     ok(hr == D3D_OK, "IDirect3DExecuteBuffer_Lock failed with %08x\n", hr);
1580     if (FAILED(hr)) {
1581         skip("IDirect3DExecuteBuffer_Lock failed; skipping further tests\n");
1582         goto out;
1583     }
1584
1585     memcpy(exdesc.lpData, test1_quads, sizeof(test1_quads));
1586
1587     exe_buffer_ptr = sizeof(test1_quads) + (char*)exdesc.lpData;
1588
1589     EXEBUF_PUT_PROCESSVERTICES(8, exe_buffer_ptr);
1590
1591     EXEBUF_START_RENDER_STATES(1, exe_buffer_ptr);
1592     hr = IDirect3DTexture_GetHandle(Texture, Direct3DDevice1, &htex);
1593     ok(hr == D3D_OK, "IDirect3DTexture_GetHandle failed with %08x\n", hr);
1594     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_TEXTUREHANDLE,  htex, exe_buffer_ptr);
1595
1596     EXEBUF_PUT_QUAD(0, exe_buffer_ptr);
1597     EXEBUF_PUT_QUAD(4, exe_buffer_ptr);
1598
1599     EXEBUF_END(exe_buffer_ptr);
1600
1601     exe_length = ((char*)exe_buffer_ptr - (char*)exdesc.lpData) - sizeof(test1_quads);
1602
1603     hr = IDirect3DExecuteBuffer_Unlock(ExecuteBuffer);
1604     if (FAILED(hr)) {
1605         trace("IDirect3DExecuteBuffer_Unlock failed with %08x\n", hr);
1606     }
1607
1608     memset(&exdata, 0, sizeof(exdata));
1609     exdata.dwSize = sizeof(exdata);
1610     exdata.dwVertexCount = 8;
1611     exdata.dwInstructionOffset = sizeof(test1_quads);
1612     exdata.dwInstructionLength = exe_length;
1613     hr = IDirect3DExecuteBuffer_SetExecuteData(ExecuteBuffer, &exdata);
1614     ok(hr == D3D_OK, "IDirect3DExecuteBuffer_SetExecuteData failed with %08x\n", hr);
1615
1616     hr = IDirect3DDevice_BeginScene(Direct3DDevice1);
1617     ok(hr == D3D_OK, "IDirect3DDevice3_BeginScene failed with %08x\n", hr);
1618
1619     if (SUCCEEDED(hr)) {
1620         hr = IDirect3DDevice_Execute(Direct3DDevice1, ExecuteBuffer, Viewport, D3DEXECUTE_UNCLIPPED);
1621         ok(hr == D3D_OK, "IDirect3DDevice_Execute failed, hr = %08x\n", hr);
1622         hr = IDirect3DDevice_EndScene(Direct3DDevice1);
1623         ok(hr == D3D_OK, "IDirect3DDevice3_EndScene failed, hr = %08x\n", hr);
1624     }
1625
1626     color = D3D1_getPixelColor(DirectDraw1, Surface1, 5, 5);
1627     red =   (color & 0x00ff0000) >> 16;
1628     green = (color & 0x0000ff00) >>  8;
1629     blue =  (color & 0x000000ff);
1630     ok(red == 0 &&  green == 0 && blue == 0xff, "Got color %08x, expected 000000ff or near\n", color);
1631
1632     color = D3D1_getPixelColor(DirectDraw1, Surface1, 400, 5);
1633     red =   (color & 0x00ff0000) >> 16;
1634     green = (color & 0x0000ff00) >>  8;
1635     blue =  (color & 0x000000ff);
1636     ok(red == 0 &&  green == 0 && blue == 0xff, "Got color %08x, expected 000000ff or near\n", color);
1637
1638     color = D3D1_getPixelColor(DirectDraw1, Surface1, 5, 245);
1639     red =   (color & 0x00ff0000) >> 16;
1640     green = (color & 0x0000ff00) >>  8;
1641     blue =  (color & 0x000000ff);
1642     ok(red == 0 &&  green == 0 && blue >= 0x7e && blue <= 0x82, "Got color %08x, expected 00000080 or near\n", color);
1643
1644     color = D3D1_getPixelColor(DirectDraw1, Surface1, 400, 245);
1645     red =   (color & 0x00ff0000) >> 16;
1646     green = (color & 0x0000ff00) >>  8;
1647     blue =  (color & 0x000000ff);
1648     ok(red == 0 &&  green == 0 && blue >= 0x7e && blue <= 0x82, "Got color %08x, expected 00000080 or near\n", color);
1649
1650     /* 3) Test RGB - should multiply color components from diffuse color and texture */
1651     if(Texture) IDirect3DTexture_Release(Texture);
1652     Texture = NULL;
1653     if(TexSurface) IDirectDrawSurface_Release(TexSurface);
1654     TexSurface = NULL;
1655
1656     memset (&ddsd, 0, sizeof (ddsd));
1657     ddsd.dwSize = sizeof (ddsd);
1658     ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
1659     ddsd.dwHeight = 128;
1660     ddsd.dwWidth = 128;
1661     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1662     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
1663     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1664     U1(ddsd.ddpfPixelFormat).dwRGBBitCount      = 32;
1665     U2(ddsd.ddpfPixelFormat).dwRBitMask         = 0x00ff0000;
1666     U3(ddsd.ddpfPixelFormat).dwGBitMask         = 0x0000ff00;
1667     U4(ddsd.ddpfPixelFormat).dwBBitMask         = 0x000000ff;
1668     U5(ddsd.ddpfPixelFormat).dwRGBAlphaBitMask  = 0xff000000;
1669     hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &TexSurface, NULL);
1670     ok(hr==D3D_OK, "CreateSurface returned: %x\n", hr);
1671     if (FAILED(hr)) {
1672         skip("IDirectDraw_CreateSurface failed; skipping further tests\n");
1673         goto out;
1674     }
1675
1676     hr = IDirectDrawSurface_QueryInterface(TexSurface, &IID_IDirect3DTexture,
1677                 (void *)&Texture);
1678     ok(hr==D3D_OK, "IDirectDrawSurface_QueryInterface returned: %x\n", hr);
1679     if (FAILED(hr)) {
1680         skip("Can't get IDirect3DTexture interface; skipping further tests\n");
1681         goto out;
1682     }
1683
1684     memset(&ddbltfx, 0, sizeof(ddbltfx));
1685     ddbltfx.dwSize = sizeof(ddbltfx);
1686     U5(ddbltfx).dwFillColor = 0;
1687     hr = IDirectDrawSurface_Blt(Surface1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1688     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1689
1690     U5(ddbltfx).dwFillColor = 0x00ffffff;
1691     hr = IDirectDrawSurface_Blt(TexSurface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1692     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1693     U5(ddbltfx).dwFillColor = 0x00ffff80;
1694     hr = IDirectDrawSurface_Blt(TexSurface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1695     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1696
1697     memset(&exdesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
1698     exdesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
1699     hr = IDirect3DExecuteBuffer_Lock(ExecuteBuffer, &exdesc);
1700     ok(hr == D3D_OK, "IDirect3DExecuteBuffer_Lock failed with %08x\n", hr);
1701     if (FAILED(hr)) {
1702         skip("IDirect3DExecuteBuffer_Lock failed; skipping further tests\n");
1703         goto out;
1704     }
1705
1706     memcpy(exdesc.lpData, test2_quads, sizeof(test2_quads));
1707
1708     exe_buffer_ptr = sizeof(test2_quads) + (char*)exdesc.lpData;
1709
1710     EXEBUF_PUT_PROCESSVERTICES(8, exe_buffer_ptr);
1711
1712     EXEBUF_START_RENDER_STATES(2, exe_buffer_ptr);
1713     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_ALPHABLENDENABLE, FALSE, exe_buffer_ptr);
1714     hr = IDirect3DTexture_GetHandle(Texture, Direct3DDevice1, &htex);
1715     ok(hr == D3D_OK, "IDirect3DTexture_GetHandle failed with %08x\n", hr);
1716     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_TEXTUREHANDLE,  htex, exe_buffer_ptr);
1717
1718     EXEBUF_PUT_QUAD(0, exe_buffer_ptr);
1719     EXEBUF_PUT_QUAD(4, exe_buffer_ptr);
1720
1721     EXEBUF_END(exe_buffer_ptr);
1722
1723     exe_length = ((char*)exe_buffer_ptr - (char*)exdesc.lpData) - sizeof(test2_quads);
1724
1725     hr = IDirect3DExecuteBuffer_Unlock(ExecuteBuffer);
1726     if (FAILED(hr)) {
1727         trace("IDirect3DExecuteBuffer_Unlock failed with %08x\n", hr);
1728     }
1729
1730     memset(&exdata, 0, sizeof(exdata));
1731     exdata.dwSize = sizeof(exdata);
1732     exdata.dwVertexCount = 8;
1733     exdata.dwInstructionOffset = sizeof(test2_quads);
1734     exdata.dwInstructionLength = exe_length;
1735     hr = IDirect3DExecuteBuffer_SetExecuteData(ExecuteBuffer, &exdata);
1736     ok(hr == D3D_OK, "IDirect3DExecuteBuffer_SetExecuteData failed with %08x\n", hr);
1737
1738     hr = IDirect3DDevice_BeginScene(Direct3DDevice1);
1739     ok(hr == D3D_OK, "IDirect3DDevice3_BeginScene failed with %08x\n", hr);
1740
1741     if (SUCCEEDED(hr)) {
1742         hr = IDirect3DDevice_Execute(Direct3DDevice1, ExecuteBuffer, Viewport, D3DEXECUTE_UNCLIPPED);
1743         ok(hr == D3D_OK, "IDirect3DDevice_Execute failed, hr = %08x\n", hr);
1744         hr = IDirect3DDevice_EndScene(Direct3DDevice1);
1745         ok(hr == D3D_OK, "IDirect3DDevice3_EndScene failed, hr = %08x\n", hr);
1746     }
1747
1748     color = D3D1_getPixelColor(DirectDraw1, Surface1, 5, 5);
1749     red =   (color & 0x00ff0000) >> 16;
1750     green = (color & 0x0000ff00) >>  8;
1751     blue =  (color & 0x000000ff);
1752     ok(red == 0xff &&  green == 0 && blue >= 0x3e && blue <= 0x42, "Got color %08x, expected 00ff0040 or near\n", color);
1753
1754     color = D3D1_getPixelColor(DirectDraw1, Surface1, 400, 5);
1755     red =   (color & 0x00ff0000) >> 16;
1756     green = (color & 0x0000ff00) >>  8;
1757     blue =  (color & 0x000000ff);
1758     ok(red == 0xff &&  green == 0 && blue == 0x80, "Got color %08x, expected 00ff0080 or near\n", color);
1759
1760     color = D3D1_getPixelColor(DirectDraw1, Surface1, 5, 245);
1761     red =   (color & 0x00ff0000) >> 16;
1762     green = (color & 0x0000ff00) >>  8;
1763     blue =  (color & 0x000000ff);
1764     ok(red >= 0x7e && red <= 0x82 &&  green == 0 && blue == 0x80, "Got color %08x, expected 00800080 or near\n", color);
1765
1766     color = D3D1_getPixelColor(DirectDraw1, Surface1, 400, 245);
1767     red =   (color & 0x00ff0000) >> 16;
1768     green = (color & 0x0000ff00) >>  8;
1769     blue =  (color & 0x000000ff);
1770     ok(red >= 0x7e && red <= 0x82 &&  green == 0 && blue == 0xff, "Got color %08x, expected 008000ff or near\n", color);
1771
1772     /* 4) Test alpha again, now with color keyed texture (colorkey emulation in wine can interfere) */
1773     if(Texture) IDirect3DTexture_Release(Texture);
1774     Texture = NULL;
1775     if(TexSurface) IDirectDrawSurface_Release(TexSurface);
1776     TexSurface = NULL;
1777
1778     memset (&ddsd, 0, sizeof (ddsd));
1779     ddsd.dwSize = sizeof (ddsd);
1780     ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
1781     ddsd.dwHeight = 128;
1782     ddsd.dwWidth = 128;
1783     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1784     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
1785     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
1786     U1(ddsd.ddpfPixelFormat).dwRGBBitCount      = 16;
1787     U2(ddsd.ddpfPixelFormat).dwRBitMask         = 0xf800;
1788     U3(ddsd.ddpfPixelFormat).dwGBitMask         = 0x07e0;
1789     U4(ddsd.ddpfPixelFormat).dwBBitMask         = 0x001f;
1790
1791     hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &TexSurface, NULL);
1792     ok(hr==D3D_OK, "CreateSurface returned: %x\n", hr);
1793     if (FAILED(hr)) {
1794         skip("IDirectDraw_CreateSurface failed; skipping further tests\n");
1795         goto out;
1796     }
1797
1798     hr = IDirectDrawSurface_QueryInterface(TexSurface, &IID_IDirect3DTexture,
1799                 (void *)&Texture);
1800     ok(hr==D3D_OK, "IDirectDrawSurface_QueryInterface returned: %x\n", hr);
1801     if (FAILED(hr)) {
1802         skip("Can't get IDirect3DTexture interface; skipping further tests\n");
1803         goto out;
1804     }
1805
1806     memset(&ddbltfx, 0, sizeof(ddbltfx));
1807     ddbltfx.dwSize = sizeof(ddbltfx);
1808     U5(ddbltfx).dwFillColor = 0;
1809     hr = IDirectDrawSurface_Blt(Surface1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1810     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1811     U5(ddbltfx).dwFillColor = 0xf800;
1812     hr = IDirectDrawSurface_Blt(TexSurface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1813     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1814     U5(ddbltfx).dwFillColor = 0x001f;
1815     hr = IDirectDrawSurface_Blt(TexSurface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1816     ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1817
1818     clrKey.dwColorSpaceLowValue = 0x001f;
1819     clrKey.dwColorSpaceHighValue = 0x001f;
1820     hr = IDirectDrawSurface_SetColorKey(TexSurface, DDCKEY_SRCBLT, &clrKey);
1821     ok(hr==D3D_OK, "IDirectDrawSurfac_SetColorKey returned: %x\n", hr);
1822
1823     memset(&exdesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
1824     exdesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
1825     hr = IDirect3DExecuteBuffer_Lock(ExecuteBuffer, &exdesc);
1826     ok(hr == D3D_OK, "IDirect3DExecuteBuffer_Lock failed with %08x\n", hr);
1827     if (FAILED(hr)) {
1828         skip("IDirect3DExecuteBuffer_Lock failed; skipping further tests\n");
1829         goto out;
1830     }
1831
1832     memcpy(exdesc.lpData, test1_quads, sizeof(test1_quads));
1833
1834     exe_buffer_ptr = sizeof(test1_quads) + (char*)exdesc.lpData;
1835
1836     EXEBUF_PUT_PROCESSVERTICES(8, exe_buffer_ptr);
1837
1838     EXEBUF_START_RENDER_STATES(2, exe_buffer_ptr);
1839     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_ALPHABLENDENABLE, TRUE, exe_buffer_ptr);
1840     hr = IDirect3DTexture_GetHandle(Texture, Direct3DDevice1, &htex);
1841     ok(hr == D3D_OK, "IDirect3DTexture_GetHandle failed with %08x\n", hr);
1842     EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_TEXTUREHANDLE,  htex, exe_buffer_ptr);
1843
1844     EXEBUF_PUT_QUAD(0, exe_buffer_ptr);
1845     EXEBUF_PUT_QUAD(4, exe_buffer_ptr);
1846
1847     EXEBUF_END(exe_buffer_ptr);
1848
1849     exe_length = ((char*)exe_buffer_ptr - (char*)exdesc.lpData) - sizeof(test1_quads);
1850
1851     hr = IDirect3DExecuteBuffer_Unlock(ExecuteBuffer);
1852     if (FAILED(hr)) {
1853         trace("IDirect3DExecuteBuffer_Unlock failed with %08x\n", hr);
1854     }
1855
1856     memset(&exdata, 0, sizeof(exdata));
1857     exdata.dwSize = sizeof(exdata);
1858     exdata.dwVertexCount = 8;
1859     exdata.dwInstructionOffset = sizeof(test1_quads);
1860     exdata.dwInstructionLength = exe_length;
1861     hr = IDirect3DExecuteBuffer_SetExecuteData(ExecuteBuffer, &exdata);
1862     ok(hr == D3D_OK, "IDirect3DExecuteBuffer_SetExecuteData failed with %08x\n", hr);
1863
1864     hr = IDirect3DDevice_BeginScene(Direct3DDevice1);
1865     ok(hr == D3D_OK, "IDirect3DDevice3_BeginScene failed with %08x\n", hr);
1866
1867     if (SUCCEEDED(hr)) {
1868         hr = IDirect3DDevice_Execute(Direct3DDevice1, ExecuteBuffer, Viewport, D3DEXECUTE_UNCLIPPED);
1869         ok(hr == D3D_OK, "IDirect3DDevice_Execute failed, hr = %08x\n", hr);
1870         hr = IDirect3DDevice_EndScene(Direct3DDevice1);
1871         ok(hr == D3D_OK, "IDirect3DDevice3_EndScene failed, hr = %08x\n", hr);
1872     }
1873
1874     color = D3D1_getPixelColor(DirectDraw1, Surface1, 5, 5);
1875     ok(color == 0, "Got color %08x, expected 00000000\n", color);
1876
1877     color = D3D1_getPixelColor(DirectDraw1, Surface1, 400, 5);
1878     red =   (color & 0x00ff0000) >> 16;
1879     green = (color & 0x0000ff00) >>  8;
1880     blue =  (color & 0x000000ff);
1881     ok(red == 0xff &&  green == 0 && blue == 0, "Got color %08x, expected 00ff0000 or near\n", color);
1882
1883     color = D3D1_getPixelColor(DirectDraw1, Surface1, 5, 245);
1884     ok(color == 0, "Got color %08x, expected 00000000\n", color);
1885
1886     color = D3D1_getPixelColor(DirectDraw1, Surface1, 400, 245);
1887     red =   (color & 0x00ff0000) >> 16;
1888     green = (color & 0x0000ff00) >>  8;
1889     blue =  (color & 0x000000ff);
1890     ok(red >= 0x7e && red <= 0x82 &&  green == 0 && blue == 0, "Got color %08x, expected 00800000 or near\n", color);
1891
1892     /* 5) Test alpha again, now with color keyed P8 texture */
1893     if(Texture) IDirect3DTexture_Release(Texture);
1894     Texture = NULL;
1895     if(TexSurface) IDirectDrawSurface_Release(TexSurface);
1896     TexSurface = NULL;
1897
1898     hr = IDirect3DDevice_EnumTextureFormats(Direct3DDevice1, TextureFormatEnumCallback,
1899                                                 &p8_textures_supported);
1900     ok(hr == DD_OK, "IDirect3DDevice_EnumTextureFormats returned %08x\n", hr);
1901
1902     if (!p8_textures_supported) {
1903         skip("device has no P8 texture support, skipping test\n");
1904     } else {
1905         memset (&ddsd, 0, sizeof (ddsd));
1906         ddsd.dwSize = sizeof (ddsd);
1907         ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
1908         ddsd.dwHeight = 128;
1909         ddsd.dwWidth = 128;
1910         ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1911         ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
1912         ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_PALETTEINDEXED8;
1913         U1(ddsd.ddpfPixelFormat).dwRGBBitCount      = 8;
1914
1915         hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &TexSurface, NULL);
1916         ok(hr==D3D_OK, "CreateSurface returned: %x\n", hr);
1917         if (FAILED(hr)) {
1918             skip("IDirectDraw_CreateSurface failed; skipping further tests\n");
1919             goto out;
1920         }
1921
1922         memset(table1, 0, sizeof(table1));
1923         table1[0].peBlue = 0xff;
1924         table1[1].peRed = 0xff;
1925
1926         hr = IDirectDraw_CreatePalette(DirectDraw1, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, table1, &Palette, NULL);
1927         ok(hr == DD_OK, "CreatePalette returned %08x\n", hr);
1928         if (FAILED(hr)) {
1929             skip("IDirectDraw_CreatePalette failed; skipping further tests\n");
1930             goto out;
1931         }
1932
1933         hr = IDirectDrawSurface_SetPalette(TexSurface, Palette);
1934         ok(hr==D3D_OK, "IDirectDrawSurface_SetPalette returned: %x\n", hr);
1935
1936         hr = IDirectDrawSurface_QueryInterface(TexSurface, &IID_IDirect3DTexture,
1937                     (void *)&Texture);
1938         ok(hr==D3D_OK, "IDirectDrawSurface_QueryInterface returned: %x\n", hr);
1939         if (FAILED(hr)) {
1940             skip("Can't get IDirect3DTexture interface; skipping further tests\n");
1941             goto out;
1942         }
1943
1944         memset(&ddbltfx, 0, sizeof(ddbltfx));
1945         ddbltfx.dwSize = sizeof(ddbltfx);
1946         U5(ddbltfx).dwFillColor = 0;
1947         hr = IDirectDrawSurface_Blt(Surface1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1948         ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1949         U5(ddbltfx).dwFillColor = 0;
1950         hr = IDirectDrawSurface_Blt(TexSurface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1951         ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1952         U5(ddbltfx).dwFillColor = 1;
1953         hr = IDirectDrawSurface_Blt(TexSurface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
1954         ok(hr == D3D_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
1955
1956         clrKey.dwColorSpaceLowValue = 1;
1957         clrKey.dwColorSpaceHighValue = 1;
1958         hr = IDirectDrawSurface_SetColorKey(TexSurface, DDCKEY_SRCBLT, &clrKey);
1959         ok(hr==D3D_OK, "IDirectDrawSurfac_SetColorKey returned: %x\n", hr);
1960
1961         memset(&exdesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
1962         exdesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
1963         hr = IDirect3DExecuteBuffer_Lock(ExecuteBuffer, &exdesc);
1964         ok(hr == D3D_OK, "IDirect3DExecuteBuffer_Lock failed with %08x\n", hr);
1965         if (FAILED(hr)) {
1966             skip("IDirect3DExecuteBuffer_Lock failed; skipping further tests\n");
1967             goto out;
1968         }
1969
1970         memcpy(exdesc.lpData, test1_quads, sizeof(test1_quads));
1971
1972         exe_buffer_ptr = sizeof(test1_quads) + (char*)exdesc.lpData;
1973
1974         EXEBUF_PUT_PROCESSVERTICES(8, exe_buffer_ptr);
1975
1976         EXEBUF_START_RENDER_STATES(2, exe_buffer_ptr);
1977         EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_ALPHABLENDENABLE, TRUE, exe_buffer_ptr);
1978         hr = IDirect3DTexture_GetHandle(Texture, Direct3DDevice1, &htex);
1979         ok(hr == D3D_OK, "IDirect3DTexture_GetHandle failed with %08x\n", hr);
1980         EXEBUF_PUT_RENDER_STATE(D3DRENDERSTATE_TEXTUREHANDLE,  htex, exe_buffer_ptr);
1981
1982         EXEBUF_PUT_QUAD(0, exe_buffer_ptr);
1983         EXEBUF_PUT_QUAD(4, exe_buffer_ptr);
1984
1985         EXEBUF_END(exe_buffer_ptr);
1986
1987         exe_length = ((char*)exe_buffer_ptr - (char*)exdesc.lpData) - sizeof(test1_quads);
1988
1989         hr = IDirect3DExecuteBuffer_Unlock(ExecuteBuffer);
1990         if (FAILED(hr)) {
1991             trace("IDirect3DExecuteBuffer_Unlock failed with %08x\n", hr);
1992         }
1993
1994         memset(&exdata, 0, sizeof(exdata));
1995         exdata.dwSize = sizeof(exdata);
1996         exdata.dwVertexCount = 8;
1997         exdata.dwInstructionOffset = sizeof(test1_quads);
1998         exdata.dwInstructionLength = exe_length;
1999         hr = IDirect3DExecuteBuffer_SetExecuteData(ExecuteBuffer, &exdata);
2000         ok(hr == D3D_OK, "IDirect3DExecuteBuffer_SetExecuteData failed with %08x\n", hr);
2001
2002         hr = IDirect3DDevice_BeginScene(Direct3DDevice1);
2003         ok(hr == D3D_OK, "IDirect3DDevice3_BeginScene failed with %08x\n", hr);
2004
2005         if (SUCCEEDED(hr)) {
2006             hr = IDirect3DDevice_Execute(Direct3DDevice1, ExecuteBuffer, Viewport, D3DEXECUTE_UNCLIPPED);
2007             ok(hr == D3D_OK, "IDirect3DDevice_Execute failed, hr = %08x\n", hr);
2008             hr = IDirect3DDevice_EndScene(Direct3DDevice1);
2009             ok(hr == D3D_OK, "IDirect3DDevice3_EndScene failed, hr = %08x\n", hr);
2010         }
2011
2012         color = D3D1_getPixelColor(DirectDraw1, Surface1, 5, 5);
2013         ok(color == 0, "Got color %08x, expected 00000000\n", color);
2014
2015         color = D3D1_getPixelColor(DirectDraw1, Surface1, 400, 5);
2016         red =   (color & 0x00ff0000) >> 16;
2017         green = (color & 0x0000ff00) >>  8;
2018         blue =  (color & 0x000000ff);
2019         ok(red == 0 &&  green == 0 && blue == 0xff, "Got color %08x, expected 000000ff or near\n", color);
2020
2021         color = D3D1_getPixelColor(DirectDraw1, Surface1, 5, 245);
2022         ok(color == 0, "Got color %08x, expected 00000000\n", color);
2023
2024         color = D3D1_getPixelColor(DirectDraw1, Surface1, 400, 245);
2025         red =   (color & 0x00ff0000) >> 16;
2026         green = (color & 0x0000ff00) >>  8;
2027         blue =  (color & 0x000000ff);
2028         ok(red == 0 &&  green == 0 && blue >= 0x7e && blue <= 0x82, "Got color %08x, expected 00000080 or near\n", color);
2029     }
2030
2031     out:
2032
2033     if (Palette) IDirectDrawPalette_Release(Palette);
2034     if (TexSurface) IDirectDrawSurface_Release(TexSurface);
2035     if (Texture) IDirect3DTexture_Release(Texture);
2036 }
2037
2038 static void D3D1_ViewportClearTest(void)
2039 {
2040     HRESULT hr;
2041     IDirect3DMaterial *bgMaterial = NULL;
2042     D3DMATERIAL mat;
2043     D3DMATERIALHANDLE hMat;
2044     D3DVIEWPORT vp_data;
2045     IDirect3DViewport *Viewport2 = NULL;
2046     DWORD color, red, green, blue;
2047
2048     hr = IDirect3D_CreateMaterial(Direct3D1, &bgMaterial, NULL);
2049     ok(hr == D3D_OK, "IDirect3D_CreateMaterial failed: %08x\n", hr);
2050     if (FAILED(hr)) {
2051         goto out;
2052     }
2053
2054     hr = IDirect3D_CreateViewport(Direct3D1, &Viewport2, NULL);
2055     ok(hr == D3D_OK, "IDirect3D_CreateViewport failed: %08x\n", hr);
2056     if (FAILED(hr)) {
2057         goto out;
2058     }
2059
2060     hr = IDirect3DViewport_Initialize(Viewport2, Direct3D1);
2061     ok(hr == D3D_OK || hr == DDERR_ALREADYINITIALIZED, "IDirect3DViewport_Initialize returned %08x\n", hr);
2062     hr = IDirect3DDevice_AddViewport(Direct3DDevice1, Viewport2);
2063     ok(hr == D3D_OK, "IDirect3DDevice_AddViewport returned %08x\n", hr);
2064     vp_data.dwSize = sizeof(vp_data);
2065     vp_data.dwX = 200;
2066     vp_data.dwY = 200;
2067     vp_data.dwWidth = 100;
2068     vp_data.dwHeight = 100;
2069     vp_data.dvScaleX = 1;
2070     vp_data.dvScaleY = 1;
2071     vp_data.dvMaxX = 100;
2072     vp_data.dvMaxY = 100;
2073     vp_data.dvMinZ = 0;
2074     vp_data.dvMaxZ = 1;
2075     hr = IDirect3DViewport_SetViewport(Viewport2, &vp_data);
2076     ok(hr == D3D_OK, "IDirect3DViewport_SetViewport returned %08x\n", hr);
2077
2078     memset(&mat, 0, sizeof(mat));
2079     mat.dwSize = sizeof(mat);
2080     U1(U(mat).diffuse).r = 1.0f;
2081     hr = IDirect3DMaterial_SetMaterial(bgMaterial, &mat);
2082     ok(hr == D3D_OK, "IDirect3DMaterial_SetMaterial failed: %08x\n", hr);
2083
2084     hr = IDirect3DMaterial_GetHandle(bgMaterial, Direct3DDevice1, &hMat);
2085     ok(hr == D3D_OK, "IDirect3DMaterial_GetHandle failed: %08x\n", hr);
2086
2087     hr = IDirect3DViewport_SetBackground(Viewport, hMat);
2088     ok(hr == D3D_OK, "IDirect3DViewport_SetBackground failed: %08x\n", hr);
2089     hr = IDirect3DViewport_SetBackground(Viewport2, hMat);
2090     ok(hr == D3D_OK, "IDirect3DViewport_SetBackground failed: %08x\n", hr);
2091
2092     hr = IDirect3DDevice_BeginScene(Direct3DDevice1);
2093     ok(hr == D3D_OK, "IDirect3DDevice3_BeginScene failed with %08x\n", hr);
2094
2095     if (SUCCEEDED(hr)) {
2096         D3DRECT rect;
2097
2098         U1(rect).x1 = U2(rect).y1 = 0;
2099         U3(rect).x2 = 640;
2100         U4(rect).y2 = 480;
2101
2102         hr = IDirect3DViewport_Clear(Viewport,  1,  &rect, D3DCLEAR_TARGET);
2103         ok(hr == D3D_OK, "IDirect3DViewport_Clear failed: %08x\n", hr);
2104
2105         memset(&mat, 0, sizeof(mat));
2106         mat.dwSize = sizeof(mat);
2107         U3(U(mat).diffuse).b = 1.0f;
2108         hr = IDirect3DMaterial_SetMaterial(bgMaterial, &mat);
2109         ok(hr == D3D_OK, "IDirect3DMaterial_SetMaterial failed: %08x\n", hr);
2110
2111         hr = IDirect3DViewport_Clear(Viewport2,  1,  &rect, D3DCLEAR_TARGET);
2112         ok(hr == D3D_OK, "IDirect3DViewport_Clear failed: %08x\n", hr);
2113
2114         hr = IDirect3DDevice_EndScene(Direct3DDevice1);
2115         ok(hr == D3D_OK, "IDirect3DDevice3_EndScene failed, hr = %08x\n", hr);
2116         }
2117
2118     color = D3D1_getPixelColor(DirectDraw1, Surface1, 5, 5);
2119     red =   (color & 0x00ff0000) >> 16;
2120     green = (color & 0x0000ff00) >>  8;
2121     blue =  (color & 0x000000ff);
2122     ok((red == 0xff && green == 0 && blue == 0) ||
2123        broken(red == 0 && green == 0 && blue == 0xff), /* VMware and some native boxes */
2124        "Got color %08x, expected 00ff0000\n", color);
2125
2126     color = D3D1_getPixelColor(DirectDraw1, Surface1, 205, 205);
2127     red =   (color & 0x00ff0000) >> 16;
2128     green = (color & 0x0000ff00) >>  8;
2129     blue =  (color & 0x000000ff);
2130     ok(red == 0 && green == 0 && blue == 0xff, "Got color %08x, expected 000000ff\n", color);
2131
2132     out:
2133
2134     if (bgMaterial) IDirect3DMaterial_Release(bgMaterial);
2135     if (Viewport2) IDirect3DViewport_Release(Viewport2);
2136 }
2137
2138 static DWORD D3D3_getPixelColor(IDirectDraw4 *DirectDraw, IDirectDrawSurface4 *Surface, UINT x, UINT y)
2139 {
2140     DWORD ret;
2141     HRESULT hr;
2142     DDSURFACEDESC2 ddsd;
2143     RECT rectToLock = {x, y, x+1, y+1};
2144     IDirectDrawSurface4 *surf = NULL;
2145
2146     /* Some implementations seem to dislike direct locking on the front buffer. Thus copy the front buffer
2147      * to an offscreen surface and lock it instead of the front buffer
2148      */
2149     memset(&ddsd, 0, sizeof(ddsd));
2150     ddsd.dwSize = sizeof(ddsd);
2151     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
2152     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
2153     ddsd.dwWidth = 640;
2154     ddsd.dwHeight = 480;
2155     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
2156     hr = IDirectDraw4_CreateSurface(DirectDraw, &ddsd, &surf, NULL);
2157     ok(hr == DD_OK, "IDirectDraw_CreateSurface failed with %08x\n", hr);
2158     if(!surf)
2159     {
2160         trace("cannot create helper surface\n");
2161         return 0xdeadbeef;
2162     }
2163
2164     memset(&ddsd, 0, sizeof(ddsd));
2165     ddsd.dwSize = sizeof(ddsd);
2166     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
2167
2168     hr = IDirectDrawSurface4_BltFast(surf, 0, 0, Surface, NULL, 0);
2169     ok(hr == DD_OK, "IDirectDrawSurface_BltFast returned %08x\n", hr);
2170     if(FAILED(hr))
2171     {
2172         trace("Cannot blit\n");
2173         ret = 0xdeadbee;
2174         goto out;
2175     }
2176
2177     hr = IDirectDrawSurface4_Lock(surf, &rectToLock, &ddsd, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
2178     if(FAILED(hr))
2179     {
2180         trace("Can't lock the offscreen surface, hr=%08x\n", hr);
2181         ret = 0xdeadbeec;
2182         goto out;
2183     }
2184
2185     /* Remove the X channel for now. DirectX and OpenGL have different ideas how to treat it apparently, and it isn't
2186      * really important for these tests
2187      */
2188     ret = ((DWORD *) ddsd.lpSurface)[0] & 0x00ffffff;
2189     hr = IDirectDrawSurface4_Unlock(surf, NULL);
2190     if(FAILED(hr))
2191     {
2192         trace("Can't unlock the offscreen surface, hr=%08x\n", hr);
2193     }
2194
2195 out:
2196     IDirectDrawSurface4_Release(surf);
2197     return ret;
2198 }
2199
2200 static void D3D3_ViewportClearTest(void)
2201 {
2202     HRESULT hr;
2203     IDirectDraw *DirectDraw1 = NULL;
2204     IDirectDraw4 *DirectDraw4 = NULL;
2205     IDirectDrawSurface4 *Primary = NULL;
2206     IDirect3D3 *Direct3D3 = NULL;
2207     IDirect3DViewport3 *Viewport3 = NULL;
2208     IDirect3DViewport3 *SmallViewport3 = NULL;
2209     IDirect3DDevice3 *Direct3DDevice3 = NULL;
2210     WNDCLASS wc = {0};
2211     DDSURFACEDESC2 ddsd;
2212     D3DVIEWPORT2 vp_data;
2213     DWORD color, red, green, blue;
2214     D3DRECT rect;
2215     float mat[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
2216                       0.0f, 1.0f, 0.0f, 0.0f,
2217                       0.0f, 0.0f, 1.0f, 0.0f,
2218                       0.0f, 0.0f, 0.0f, 1.0f };
2219     struct vertex quad[] =
2220     {
2221         {-1.0f, -1.0f,   0.1f,                          0xffffffff},
2222         {-1.0f,  1.0f,   0.1f,                          0xffffffff},
2223         { 1.0f,  1.0f,   0.1f,                          0xffffffff},
2224         { 1.0f, -1.0f,   0.1f,                          0xffffffff},
2225     };
2226
2227     WORD Indices[] = {0, 1, 2, 2, 3, 0};
2228     DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
2229
2230     wc.lpfnWndProc = DefWindowProc;
2231     wc.lpszClassName = "D3D3_ViewportClearTest_wc";
2232     RegisterClass(&wc);
2233     window = CreateWindow("D3D3_ViewportClearTest_wc", "D3D3_ViewportClearTest",
2234                             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
2235
2236     hr = DirectDrawCreate( NULL, &DirectDraw1, NULL );
2237     ok(hr==DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreate returned: %x\n", hr);
2238     if(FAILED(hr)) goto out;
2239
2240     hr = IDirectDraw_SetCooperativeLevel(DirectDraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2241     ok(hr==DD_OK, "SetCooperativeLevel returned: %x\n", hr);
2242     if(FAILED(hr)) goto out;
2243
2244     hr = IDirectDraw_SetDisplayMode(DirectDraw1, 640, 480, 32);
2245     if(FAILED(hr)) {
2246         /* 24 bit is fine too */
2247         hr = IDirectDraw_SetDisplayMode(DirectDraw1, 640, 480, 24);
2248     }
2249     ok(hr==DD_OK || hr == DDERR_UNSUPPORTED, "SetDisplayMode returned: %x\n", hr);
2250     if (FAILED(hr)) goto out;
2251
2252     hr = IDirectDraw_QueryInterface(DirectDraw1, &IID_IDirectDraw4, (void**)&DirectDraw4);
2253     ok(hr==DD_OK, "QueryInterface returned: %08x\n", hr);
2254     if(FAILED(hr)) goto out;
2255
2256     memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
2257     ddsd.dwSize = sizeof(DDSURFACEDESC2);
2258     ddsd.dwFlags    = DDSD_CAPS;
2259     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE;
2260
2261     hr = IDirectDraw_CreateSurface(DirectDraw4, &ddsd, &Primary, NULL);
2262     ok(hr==DD_OK, "IDirectDraw_CreateSurface returned: %08x\n", hr);
2263     if(FAILED(hr)) goto out;
2264
2265     hr = IDirectDraw4_QueryInterface(DirectDraw4, &IID_IDirect3D3, (void**)&Direct3D3);
2266     ok(hr==DD_OK, "IDirectDraw4_QueryInterface returned: %08x\n", hr);
2267     if(FAILED(hr)) goto out;
2268
2269     hr = IDirect3D3_CreateDevice(Direct3D3, &IID_IDirect3DHALDevice, Primary, &Direct3DDevice3, NULL);
2270     if(FAILED(hr)) {
2271         trace("Creating a HAL device failed, trying Ref\n");
2272         hr = IDirect3D3_CreateDevice(Direct3D3, &IID_IDirect3DRefDevice, Primary, &Direct3DDevice3, NULL);
2273     }
2274     ok(hr==D3D_OK, "Creating 3D device returned: %x\n", hr);
2275     if(FAILED(hr)) goto out;
2276
2277     hr = IDirect3D3_CreateViewport(Direct3D3, &Viewport3, NULL);
2278     ok(hr==DD_OK, "IDirect3D3_CreateViewport returned: %08x\n", hr);
2279     if(FAILED(hr)) goto out;
2280
2281     hr = IDirect3DDevice3_AddViewport(Direct3DDevice3, Viewport3);
2282     ok(hr==DD_OK, "IDirect3DDevice3_AddViewport returned: %08x\n", hr);
2283
2284     memset(&vp_data, 0, sizeof(D3DVIEWPORT2));
2285     vp_data.dwSize = sizeof(D3DVIEWPORT2);
2286     vp_data.dwWidth = 640;
2287     vp_data.dwHeight = 480;
2288     vp_data.dvClipX = -1.0f;
2289     vp_data.dvClipWidth = 2.0f;
2290     vp_data.dvClipY = 1.0f;
2291     vp_data.dvClipHeight = 2.0f;
2292     vp_data.dvMaxZ = 1.0f;
2293     hr = IDirect3DViewport3_SetViewport2(Viewport3, &vp_data);
2294     ok(hr==DD_OK, "IDirect3DViewport3_SetViewport2 returned: %08x\n", hr);
2295
2296     hr = IDirect3D3_CreateViewport(Direct3D3, &SmallViewport3, NULL);
2297     ok(hr==DD_OK, "IDirect3D3_CreateViewport returned: %08x\n", hr);
2298     if(FAILED(hr)) goto out;
2299
2300     hr = IDirect3DDevice3_AddViewport(Direct3DDevice3, SmallViewport3);
2301     ok(hr==DD_OK, "IDirect3DDevice3_AddViewport returned: %08x\n", hr);
2302
2303     memset(&vp_data, 0, sizeof(D3DVIEWPORT2));
2304     vp_data.dwSize = sizeof(D3DVIEWPORT2);
2305     vp_data.dwX = 400;
2306     vp_data.dwY = 100;
2307     vp_data.dwWidth = 100;
2308     vp_data.dwHeight = 100;
2309     vp_data.dvClipX = -1.0f;
2310     vp_data.dvClipWidth = 2.0f;
2311     vp_data.dvClipY = 1.0f;
2312     vp_data.dvClipHeight = 2.0f;
2313     vp_data.dvMaxZ = 1.0f;
2314     hr = IDirect3DViewport3_SetViewport2(SmallViewport3, &vp_data);
2315     ok(hr==DD_OK, "IDirect3DViewport3_SetViewport2 returned: %08x\n", hr);
2316
2317     hr = IDirect3DDevice3_BeginScene(Direct3DDevice3);
2318     ok(hr == D3D_OK, "IDirect3DDevice3_BeginScene failed with %08x\n", hr);
2319
2320     hr = IDirect3DDevice3_SetTransform(Direct3DDevice3, D3DTRANSFORMSTATE_WORLD, (D3DMATRIX *) mat);
2321     ok(hr == D3D_OK, "IDirect3DDevice3_SetTransform returned %08x\n", hr);
2322     hr = IDirect3DDevice3_SetTransform(Direct3DDevice3, D3DTRANSFORMSTATE_VIEW, (D3DMATRIX *)mat);
2323     ok(hr == D3D_OK, "IDirect3DDevice3_SetTransform returned %08x\n", hr);
2324     hr = IDirect3DDevice3_SetTransform(Direct3DDevice3, D3DTRANSFORMSTATE_PROJECTION, (D3DMATRIX *) mat);
2325     ok(hr == D3D_OK, "IDirect3DDevice3_SetTransform returned %08x\n", hr);
2326     hr = IDirect3DDevice3_SetRenderState(Direct3DDevice3, D3DRENDERSTATE_CLIPPING, FALSE);
2327     ok(hr == D3D_OK, "IDirect3DDevice3_SetRenderState returned %08x\n", hr);
2328     hr = IDirect3DDevice3_SetRenderState(Direct3DDevice3, D3DRENDERSTATE_ZENABLE, FALSE);
2329     ok(hr == D3D_OK, "IDirect3DDevice3_SetRenderState returned %08x\n", hr);
2330     hr = IDirect3DDevice3_SetRenderState(Direct3DDevice3, D3DRENDERSTATE_FOGENABLE, FALSE);
2331     ok(hr == D3D_OK, "IDirect3DDevice3_SetRenderState returned %08x\n", hr);
2332     hr = IDirect3DDevice3_SetRenderState(Direct3DDevice3, D3DRENDERSTATE_STENCILENABLE, FALSE);
2333     ok(hr == D3D_OK, "IDirect3DDevice3_SetRenderState returned %08x\n", hr);
2334     hr = IDirect3DDevice3_SetRenderState(Direct3DDevice3, D3DRENDERSTATE_ALPHATESTENABLE, FALSE);
2335     ok(hr == D3D_OK, "IDirect3DDevice3_SetRenderState returned %08x\n", hr);
2336     hr = IDirect3DDevice3_SetRenderState(Direct3DDevice3, D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
2337     ok(hr == D3D_OK, "IDirect3DDevice3_SetRenderState returned %08x\n", hr);
2338     hr = IDirect3DDevice3_SetRenderState(Direct3DDevice3, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
2339     ok(hr == D3D_OK, "IDirect3DDevice3_SetRenderState failed with %08x\n", hr);
2340     hr = IDirect3DDevice3_SetRenderState(Direct3DDevice3, D3DRENDERSTATE_LIGHTING, FALSE);
2341     ok(hr == D3D_OK, "IDirect3DDevice3_SetRenderState returned %08x\n", hr);
2342
2343     if (SUCCEEDED(hr)) {
2344         U1(rect).x1 = U2(rect).y1 = 0;
2345         U3(rect).x2 = 640;
2346         U4(rect).y2 = 480;
2347
2348         hr = IDirect3DViewport3_Clear2(Viewport3, 1, &rect, D3DCLEAR_TARGET, 0x00ff00, 0.0f, 0);
2349         ok(hr == D3D_OK, "IDirect3DViewport3_Clear2 failed, hr = %08x\n", hr);
2350
2351         hr = IDirect3DViewport3_Clear2(SmallViewport3, 1, &rect, D3DCLEAR_TARGET, 0xff0000, 0.0f, 0);
2352         ok(hr == D3D_OK, "IDirect3DViewport3_Clear2 failed, hr = %08x\n", hr);
2353
2354         hr = IDirect3DDevice3_EndScene(Direct3DDevice3);
2355         ok(hr == D3D_OK, "IDirect3DDevice3_EndScene failed, hr = %08x\n", hr);
2356         }
2357
2358     color = D3D3_getPixelColor(DirectDraw4, Primary, 5, 5);
2359     red =   (color & 0x00ff0000) >> 16;
2360     green = (color & 0x0000ff00) >>  8;
2361     blue =  (color & 0x000000ff);
2362     ok(red == 0 && green == 0xff && blue == 0, "Got color %08x, expected 0000ff00\n", color);
2363
2364     color = D3D3_getPixelColor(DirectDraw4, Primary, 405, 105);
2365     red =   (color & 0x00ff0000) >> 16;
2366     green = (color & 0x0000ff00) >>  8;
2367     blue =  (color & 0x000000ff);
2368     ok(red == 0xff && green == 0 && blue == 0, "Got color %08x, expected 00ff0000\n", color);
2369
2370     /* Test that clearing viewport doesn't interfere with rendering to previously active viewport. */
2371     hr = IDirect3DDevice3_BeginScene(Direct3DDevice3);
2372     ok(hr == D3D_OK, "IDirect3DDevice3_BeginScene failed with %08x\n", hr);
2373
2374     if (SUCCEEDED(hr)) {
2375         hr = IDirect3DDevice3_SetCurrentViewport(Direct3DDevice3, SmallViewport3);
2376         ok(hr == D3D_OK, "IDirect3DDevice3_SetCurrentViewport failed with %08x\n", hr);
2377
2378         hr = IDirect3DViewport3_Clear2(Viewport3, 1, &rect, D3DCLEAR_TARGET, 0x000000, 0.0f, 0);
2379         ok(hr == D3D_OK, "IDirect3DViewport3_Clear2 failed, hr = %08x\n", hr);
2380
2381         hr = IDirect3DDevice3_DrawIndexedPrimitive(Direct3DDevice3, D3DPT_TRIANGLELIST, fvf, quad, 4 /* NumVerts */,
2382                                                     Indices, 6 /* Indexcount */, 0 /* flags */);
2383         ok(hr == D3D_OK, "IDirect3DDevice3_DrawIndexedPrimitive failed with %08x\n", hr);
2384
2385         hr = IDirect3DDevice3_EndScene(Direct3DDevice3);
2386         ok(hr == D3D_OK, "IDirect3DDevice3_EndScene failed, hr = %08x\n", hr);
2387         }
2388
2389     color = D3D3_getPixelColor(DirectDraw4, Primary, 5, 5);
2390     red =   (color & 0x00ff0000) >> 16;
2391     green = (color & 0x0000ff00) >>  8;
2392     blue =  (color & 0x000000ff);
2393     ok(red == 0 && green == 0 && blue == 0, "Got color %08x, expected 00000000\n", color);
2394
2395     color = D3D3_getPixelColor(DirectDraw4, Primary, 405, 105);
2396     red =   (color & 0x00ff0000) >> 16;
2397     green = (color & 0x0000ff00) >>  8;
2398     blue =  (color & 0x000000ff);
2399     ok(red == 0xff && green == 0xff && blue == 0xff, "Got color %08x, expected 00ffffff\n", color);
2400
2401     out:
2402
2403     if (SmallViewport3) IDirect3DViewport3_Release(SmallViewport3);
2404     if (Viewport3) IDirect3DViewport3_Release(Viewport3);
2405     if (Direct3DDevice3) IDirect3DDevice3_Release(Direct3DDevice3);
2406     if (Direct3D3) IDirect3D3_Release(Direct3D3);
2407     if (Primary) IDirectDrawSurface4_Release(Primary);
2408     if (DirectDraw1) IDirectDraw_Release(DirectDraw1);
2409     if (DirectDraw4) IDirectDraw4_Release(DirectDraw4);
2410     if(window) DestroyWindow(window);
2411 }
2412
2413 static void p8_surface_fill_rect(IDirectDrawSurface *dest, UINT x, UINT y, UINT w, UINT h, BYTE colorindex)
2414 {
2415     DDSURFACEDESC ddsd;
2416     HRESULT hr;
2417     UINT i, i1;
2418     BYTE *p;
2419
2420     memset(&ddsd, 0, sizeof(ddsd));
2421     ddsd.dwSize = sizeof(ddsd);
2422
2423     hr = IDirectDrawSurface_Lock(dest, NULL, &ddsd, DDLOCK_WRITEONLY | DDLOCK_WAIT, NULL);
2424     ok(hr==DD_OK, "IDirectDrawSurface_Lock returned: %x\n", hr);
2425
2426     p = (BYTE *)ddsd.lpSurface + U1(ddsd).lPitch * y + x;
2427
2428     for (i = 0; i < h; i++) {
2429         for (i1 = 0; i1 < w; i1++) {
2430             p[i1] = colorindex;
2431         }
2432         p += U1(ddsd).lPitch;
2433     }
2434
2435     hr = IDirectDrawSurface_Unlock(dest, NULL);
2436     ok(hr==DD_OK, "IDirectDrawSurface_UnLock returned: %x\n", hr);
2437 }
2438
2439 static COLORREF getPixelColor_GDI(IDirectDrawSurface *Surface, UINT x, UINT y)
2440 {
2441     COLORREF clr = CLR_INVALID;
2442     HDC hdc;
2443     HRESULT hr;
2444
2445     hr = IDirectDrawSurface_GetDC(Surface, &hdc);
2446     ok(hr==DD_OK, "IDirectDrawSurface_GetDC returned: %x\n", hr);
2447
2448     if (SUCCEEDED(hr)) {
2449         clr = GetPixel(hdc, x, y);
2450
2451         hr = IDirectDrawSurface_ReleaseDC(Surface, hdc);
2452         ok(hr==DD_OK, "IDirectDrawSurface_ReleaseDC returned: %x\n", hr);
2453     }
2454
2455     return clr;
2456 }
2457
2458 static BOOL colortables_check_equality(PALETTEENTRY table1[256], RGBQUAD table2[256])
2459 {
2460     int i;
2461
2462     for (i = 0; i < 256; i++) {
2463        if (table1[i].peRed != table2[i].rgbRed || table1[i].peGreen != table2[i].rgbGreen ||
2464            table1[i].peBlue != table2[i].rgbBlue) return FALSE;
2465     }
2466
2467     return TRUE;
2468 }
2469
2470 static void p8_primary_test(void)
2471 {
2472     /* Test 8bit mode used by games like StarCraft, C&C Red Alert I etc */
2473     DDSURFACEDESC ddsd;
2474     HDC hdc;
2475     HRESULT hr;
2476     PALETTEENTRY entries[256];
2477     RGBQUAD coltable[256];
2478     UINT i, i1, i2;
2479     IDirectDrawPalette *ddprimpal = NULL;
2480     IDirectDrawSurface *offscreen = NULL;
2481     WNDCLASS wc = {0};
2482     DDBLTFX ddbltfx;
2483     COLORREF color;
2484     RECT rect;
2485     DDCOLORKEY clrKey;
2486     unsigned differences;
2487
2488     /* An IDirect3DDevice cannot be queryInterfaced from an IDirect3DDevice7 on windows */
2489     hr = DirectDrawCreate(NULL, &DirectDraw1, NULL);
2490
2491     ok(hr==DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreate returned: %x\n", hr);
2492     if (FAILED(hr)) {
2493         goto out;
2494     }
2495
2496     wc.lpfnWndProc = DefWindowProc;
2497     wc.lpszClassName = "p8_primary_test_wc";
2498     RegisterClass(&wc);
2499     window = CreateWindow("p8_primary_test_wc", "p8_primary_test", WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
2500
2501     hr = IDirectDraw_SetCooperativeLevel(DirectDraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2502     ok(hr==DD_OK, "SetCooperativeLevel returned: %x\n", hr);
2503     if(FAILED(hr)) {
2504         goto out;
2505     }
2506
2507     hr = IDirectDraw_SetDisplayMode(DirectDraw1, 640, 480, 8);
2508     ok(hr==DD_OK || hr == DDERR_UNSUPPORTED, "SetDisplayMode returned: %x\n", hr);
2509     if (FAILED(hr)) {
2510         goto out;
2511     }
2512
2513     memset(&ddsd, 0, sizeof(ddsd));
2514     ddsd.dwSize = sizeof(ddsd);
2515     ddsd.dwFlags = DDSD_CAPS;
2516     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2517     hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &Surface1, NULL);
2518     ok(hr==DD_OK, "CreateSurface returned: %x\n", hr);
2519     if (FAILED(hr)) {
2520         goto out;
2521     }
2522
2523     memset(entries, 0, sizeof(entries));
2524     entries[0].peRed = 0xff;
2525     entries[1].peGreen = 0xff;
2526     entries[2].peBlue = 0xff;
2527
2528     hr = IDirectDraw_CreatePalette(DirectDraw1, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, entries, &ddprimpal, NULL);
2529     ok(hr == DD_OK, "CreatePalette returned %08x\n", hr);
2530     if (FAILED(hr)) {
2531         skip("IDirectDraw_CreatePalette failed; skipping further tests\n");
2532         goto out;
2533     }
2534
2535     hr = IDirectDrawSurface_SetPalette(Surface1, ddprimpal);
2536     ok(hr==DD_OK, "IDirectDrawSurface_SetPalette returned: %x\n", hr);
2537
2538     p8_surface_fill_rect(Surface1, 0, 0, 640, 480, 2);
2539
2540     color = getPixelColor_GDI(Surface1, 10, 10);
2541     ok(GetRValue(color) == 0 && GetGValue(color) == 0 && GetBValue(color) == 0xFF,
2542             "got R %02X G %02X B %02X, expected R 00 G 00 B FF\n",
2543             GetRValue(color), GetGValue(color), GetBValue(color));
2544
2545     memset(&ddbltfx, 0, sizeof(ddbltfx));
2546     ddbltfx.dwSize = sizeof(ddbltfx);
2547     U5(ddbltfx).dwFillColor = 0;
2548     hr = IDirectDrawSurface_Blt(Surface1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
2549     ok(hr == DD_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
2550
2551     color = getPixelColor_GDI(Surface1, 10, 10);
2552     ok(GetRValue(color) == 0xFF && GetGValue(color) == 0 && GetBValue(color) == 0,
2553             "got R %02X G %02X B %02X, expected R FF G 00 B 00\n",
2554             GetRValue(color), GetGValue(color), GetBValue(color));
2555
2556     memset(&ddbltfx, 0, sizeof(ddbltfx));
2557     ddbltfx.dwSize = sizeof(ddbltfx);
2558     U5(ddbltfx).dwFillColor = 1;
2559     hr = IDirectDrawSurface_Blt(Surface1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
2560     ok(hr == DD_OK, "IDirectDrawSurface_Blt failed with %08x\n", hr);
2561
2562     color = getPixelColor_GDI(Surface1, 10, 10);
2563     ok(GetRValue(color) == 0 && GetGValue(color) == 0xFF && GetBValue(color) == 0,
2564             "got R %02X G %02X B %02X, expected R 00 G FF B 00\n",
2565             GetRValue(color), GetGValue(color), GetBValue(color));
2566
2567     memset (&ddsd, 0, sizeof (ddsd));
2568     ddsd.dwSize = sizeof (ddsd);
2569     ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
2570     ddsd.dwWidth = 16;
2571     ddsd.dwHeight = 16;
2572     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
2573     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
2574     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_PALETTEINDEXED8;
2575     U1(ddsd.ddpfPixelFormat).dwRGBBitCount      = 8;
2576     hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &offscreen, NULL);
2577     ok(hr == DD_OK ||
2578        broken(hr == DDERR_INVALIDPIXELFORMAT) || /* VMware */
2579        broken(hr == DDERR_NODIRECTDRAWHW), /* VMware */
2580        "IDirectDraw_CreateSurface returned %08x\n", hr);
2581     if (FAILED(hr)) goto out;
2582
2583     memset(entries, 0, sizeof(entries));
2584     for (i = 0; i < 256; i++) {
2585         entries[i].peBlue = i;
2586         }
2587     hr = IDirectDrawPalette_SetEntries(ddprimpal, 0, 0, 256, entries);
2588     ok(hr == DD_OK, "IDirectDrawPalette_SetEntries failed with %08x\n", hr);
2589
2590     hr = IDirectDrawSurface_GetDC(offscreen, &hdc);
2591     ok(hr==DD_OK, "IDirectDrawSurface_GetDC returned: %x\n", hr);
2592     i = GetDIBColorTable(hdc, 0, 256, coltable);
2593     ok(i == 256, "GetDIBColorTable returned %u, last error: %x\n", i, GetLastError());
2594     hr = IDirectDrawSurface_ReleaseDC(offscreen, hdc);
2595     ok(hr==DD_OK, "IDirectDrawSurface_ReleaseDC returned: %x\n", hr);
2596
2597     ok(colortables_check_equality(entries, coltable), "unexpected colortable on offscreen surface\n");
2598
2599     p8_surface_fill_rect(offscreen, 0, 0, 16, 16, 2);
2600
2601     memset(entries, 0, sizeof(entries));
2602     entries[0].peRed = 0xff;
2603     entries[1].peGreen = 0xff;
2604     entries[2].peBlue = 0xff;
2605     entries[3].peRed = 0x80;
2606     hr = IDirectDrawPalette_SetEntries(ddprimpal, 0, 0, 256, entries);
2607     ok(hr == DD_OK, "IDirectDrawPalette_SetEntries failed with %08x\n", hr);
2608
2609     hr = IDirectDrawSurface_BltFast(Surface1, 0, 0, offscreen, NULL, 0);
2610     ok(hr==DD_OK, "IDirectDrawSurface_BltFast returned: %x\n", hr);
2611
2612     color = getPixelColor_GDI(Surface1, 1, 1);
2613     ok(GetRValue(color) == 0 && GetGValue(color) == 0x00 && GetBValue(color) == 0xFF,
2614             "got R %02X G %02X B %02X, expected R 00 G 00 B FF\n",
2615             GetRValue(color), GetGValue(color), GetBValue(color));
2616
2617     /* Color keyed blit. */
2618     p8_surface_fill_rect(offscreen, 0, 0, 8, 8, 3);
2619     clrKey.dwColorSpaceLowValue = 3;
2620     clrKey.dwColorSpaceHighValue = 3;
2621     hr = IDirectDrawSurface_SetColorKey(offscreen, DDCKEY_SRCBLT, &clrKey);
2622     ok(hr==D3D_OK, "IDirectDrawSurfac_SetColorKey returned: %x\n", hr);
2623
2624     hr = IDirectDrawSurface_BltFast(Surface1, 100, 100, offscreen, NULL, DDBLTFAST_SRCCOLORKEY);
2625     ok(hr==DD_OK, "IDirectDrawSurface_BltFast returned: %x\n", hr);
2626
2627     color = getPixelColor_GDI(Surface1, 105, 105);
2628     ok(GetRValue(color) == 0 && GetGValue(color) == 0xFF && GetBValue(color) == 0,
2629             "got R %02X G %02X B %02X, expected R 00 G FF B 00\n",
2630             GetRValue(color), GetGValue(color), GetBValue(color));
2631
2632     color = getPixelColor_GDI(Surface1, 112, 112);
2633     ok(GetRValue(color) == 0 && GetGValue(color) == 0x00 && GetBValue(color) == 0xFF,
2634             "got R %02X G %02X B %02X, expected R 00 G 00 B FF\n",
2635             GetRValue(color), GetGValue(color), GetBValue(color));
2636
2637     rect.left = 100;
2638     rect.top = 200;
2639     rect.right = 116;
2640     rect.bottom = 216;
2641
2642     memset(&ddbltfx, 0, sizeof(ddbltfx));
2643     ddbltfx.dwSize = sizeof(ddbltfx);
2644     ddbltfx.ddckSrcColorkey.dwColorSpaceLowValue = ddbltfx.ddckSrcColorkey.dwColorSpaceHighValue = 2;
2645     hr = IDirectDrawSurface_Blt(Surface1, &rect, offscreen, NULL,
2646         DDBLT_WAIT | DDBLT_KEYSRC | DDBLT_KEYSRCOVERRIDE, &ddbltfx);
2647     ok(hr==DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned: %x\n", hr);
2648     hr = IDirectDrawSurface_Blt(Surface1, &rect, offscreen, NULL,
2649         DDBLT_WAIT | DDBLT_KEYSRCOVERRIDE, &ddbltfx);
2650     ok(hr==DD_OK, "IDirectDrawSurface_Blt returned: %x\n", hr);
2651
2652     color = getPixelColor_GDI(Surface1, 105, 205);
2653     ok(GetRValue(color) == 0x80 && GetGValue(color) == 0 && GetBValue(color) == 0,
2654             "got R %02X G %02X B %02X, expected R 80 G 00 B 00\n",
2655             GetRValue(color), GetGValue(color), GetBValue(color));
2656
2657     color = getPixelColor_GDI(Surface1, 112, 212);
2658     ok(GetRValue(color) == 0 && GetGValue(color) == 0xFF && GetBValue(color) == 0,
2659             "got R %02X G %02X B %02X, expected R 00 G FF B 00\n",
2660             GetRValue(color), GetGValue(color), GetBValue(color));
2661
2662     /* Test blitting and locking patterns that are likely to trigger bugs in opengl renderer (p8
2663        surface conversion and uploading/downloading to/from opengl texture). Similar patterns (
2664        blitting front buffer areas to/from an offscreen surface mixed with locking) are used by C&C
2665        Red Alert I. */
2666     IDirectDrawSurface_Release(offscreen);
2667
2668     memset (&ddsd, 0, sizeof (ddsd));
2669     ddsd.dwSize = sizeof (ddsd);
2670     ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
2671     ddsd.dwWidth = 640;
2672     ddsd.dwHeight = 480;
2673     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
2674     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
2675     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_PALETTEINDEXED8;
2676     U1(ddsd.ddpfPixelFormat).dwRGBBitCount      = 8;
2677     hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &offscreen, NULL);
2678     ok(hr == DD_OK, "IDirectDraw_CreateSurface returned %08x\n", hr);
2679
2680     if (FAILED(hr)) goto out;
2681
2682     /* Test two times, first time front buffer has a palette and second time front buffer
2683        has no palette; the latter is somewhat contrived example, but an app could set
2684        front buffer palette later. */
2685     for (i2 = 0; i2 < 2; i2++) {
2686         if (i2 == 1) {
2687             hr = IDirectDrawSurface_SetPalette(Surface1, NULL);
2688             ok(hr==DD_OK, "IDirectDrawSurface_SetPalette returned: %x\n", hr);
2689         }
2690
2691         memset(&ddsd, 0, sizeof(ddsd));
2692         ddsd.dwSize = sizeof(ddsd);
2693         hr = IDirectDrawSurface_Lock(Surface1, NULL, &ddsd, DDLOCK_WAIT, NULL);
2694         ok(hr==DD_OK, "IDirectDrawSurface_Lock returned: %x\n", hr);
2695
2696         for (i = 0; i < 256; i++) {
2697             unsigned x = (i % 128) * 4;
2698             unsigned y = (i / 128) * 4;
2699             BYTE *p = (BYTE *)ddsd.lpSurface + U1(ddsd).lPitch * y + x;
2700
2701             for (i1 = 0; i1 < 4; i1++) {
2702                 p[0] = p[1] = p[2] = p[3] = i;
2703                 p += U1(ddsd).lPitch;
2704             }
2705         }
2706
2707         hr = IDirectDrawSurface_Unlock(Surface1, NULL);
2708         ok(hr==DD_OK, "IDirectDrawSurface_UnLock returned: %x\n", hr);
2709
2710         hr = IDirectDrawSurface_BltFast(offscreen, 0, 0, Surface1, NULL, 0);
2711         ok(hr==DD_OK, "IDirectDrawSurface_BltFast returned: %x\n", hr);
2712
2713         /* This ensures offscreen surface contents will be downloaded to system memory. */
2714         memset(&ddsd, 0, sizeof(ddsd));
2715         ddsd.dwSize = sizeof(ddsd);
2716         hr = IDirectDrawSurface_Lock(offscreen, NULL, &ddsd, DDLOCK_WAIT, NULL);
2717         ok(hr==DD_OK, "IDirectDrawSurface_Lock returned: %x\n", hr);
2718         hr = IDirectDrawSurface_Unlock(offscreen, NULL);
2719         ok(hr==DD_OK, "IDirectDrawSurface_UnLock returned: %x\n", hr);
2720
2721         /* Offscreen surface data will have to be converted and uploaded to texture. */
2722         rect.left = 0;
2723         rect.top = 0;
2724         rect.right = 16;
2725         rect.bottom = 16;
2726         hr = IDirectDrawSurface_BltFast(offscreen, 600, 400, Surface1, &rect, 0);
2727         ok(hr==DD_OK, "IDirectDrawSurface_BltFast returned: %x\n", hr);
2728
2729         /* This ensures offscreen surface contents will be downloaded to system memory. */
2730         memset(&ddsd, 0, sizeof(ddsd));
2731         ddsd.dwSize = sizeof(ddsd);
2732         hr = IDirectDrawSurface_Lock(offscreen, NULL, &ddsd, DDLOCK_WAIT, NULL);
2733         ok(hr==DD_OK, "IDirectDrawSurface_Lock returned: %x\n", hr);
2734         hr = IDirectDrawSurface_Unlock(offscreen, NULL);
2735         ok(hr==DD_OK, "IDirectDrawSurface_UnLock returned: %x\n", hr);
2736
2737         hr = IDirectDrawSurface_BltFast(Surface1, 0, 0, offscreen, NULL, 0);
2738         ok(hr==DD_OK, "IDirectDrawSurface_BltFast returned: %x\n", hr);
2739
2740         memset(&ddsd, 0, sizeof(ddsd));
2741         ddsd.dwSize = sizeof(ddsd);
2742         hr = IDirectDrawSurface_Lock(Surface1, NULL, &ddsd, DDLOCK_WAIT, NULL);
2743         ok(hr==DD_OK, "IDirectDrawSurface_Lock returned: %x\n", hr);
2744
2745         differences = 0;
2746
2747         for (i = 0; i < 256; i++) {
2748             unsigned x = (i % 128) * 4 + 1;
2749             unsigned y = (i / 128) * 4 + 1;
2750             BYTE *p = (BYTE *)ddsd.lpSurface + U1(ddsd).lPitch * y + x;
2751
2752             if (*p != i) differences++;
2753         }
2754
2755         hr = IDirectDrawSurface_Unlock(Surface1, NULL);
2756         ok(hr==DD_OK, "IDirectDrawSurface_UnLock returned: %x\n", hr);
2757
2758         ok(differences == 0, i2 == 0 ? "Pass 1. Unexpected front buffer contents after blit (%u differences)\n" :
2759                 "Pass 2 (with NULL front buffer palette). Unexpected front buffer contents after blit (%u differences)\n",
2760                 differences);
2761     }
2762
2763     out:
2764
2765     if(ddprimpal) IDirectDrawPalette_Release(ddprimpal);
2766     if(offscreen) IDirectDrawSurface_Release(offscreen);
2767     if(Surface1) IDirectDrawSurface_Release(Surface1);
2768     if(DirectDraw1) IDirectDraw_Release(DirectDraw1);
2769     if(window) DestroyWindow(window);
2770 }
2771
2772 static void cubemap_test(IDirect3DDevice7 *device)
2773 {
2774     IDirect3D7 *d3d;
2775     IDirectDraw7 *ddraw;
2776     IDirectDrawSurface7 *cubemap, *surface;
2777     D3DDEVICEDESC7 d3dcaps;
2778     HRESULT hr;
2779     DWORD color;
2780     DDSURFACEDESC2 ddsd;
2781     DDBLTFX DDBltFx;
2782     DDSCAPS2 caps;
2783     static float quad[] = {
2784       -1.0,   -1.0,    0.1,    1.0,    0.0,    0.0, /* Lower left */
2785        0.0,   -1.0,    0.1,    1.0,    0.0,    0.0,
2786       -1.0,    0.0,    0.1,    1.0,    0.0,    0.0,
2787        0.0,    0.0,    0.1,    1.0,    0.0,    0.0,
2788
2789        0.0,   -1.0,    0.1,    0.0,    1.0,    0.0, /* Lower right */
2790        1.0,   -1.0,    0.1,    0.0,    1.0,    0.0,
2791        0.0,    0.0,    0.1,    0.0,    1.0,    0.0,
2792        1.0,    0.0,    0.1,    0.0,    1.0,    0.0,
2793
2794        0.0,    0.0,    0.1,    0.0,    0.0,    1.0, /* upper right */
2795        1.0,    0.0,    0.1,    0.0,    0.0,    1.0,
2796        0.0,    1.0,    0.1,    0.0,    0.0,    1.0,
2797        1.0,    1.0,    0.1,    0.0,    0.0,    1.0,
2798
2799       -1.0,    0.0,    0.1,   -1.0,    0.0,    0.0, /* Upper left */
2800        0.0,    0.0,    0.1,   -1.0,    0.0,    0.0,
2801       -1.0,    1.0,    0.1,   -1.0,    0.0,    0.0,
2802        0.0,    1.0,    0.1,   -1.0,    0.0,    0.0,
2803     };
2804
2805     memset(&DDBltFx, 0, sizeof(DDBltFx));
2806     DDBltFx.dwSize = sizeof(DDBltFx);
2807
2808     memset(&d3dcaps, 0, sizeof(d3dcaps));
2809     hr = IDirect3DDevice7_GetCaps(device, &d3dcaps);
2810     ok(hr == D3D_OK, "IDirect3DDevice7_GetCaps returned %08x\n", hr);
2811     if(!(d3dcaps.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_CUBEMAP))
2812     {
2813         skip("No cubemap support\n");
2814         return;
2815     }
2816
2817     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
2818     ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
2819
2820     hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
2821     ok(hr == D3D_OK, "IDirect3DDevice7_GetDirect3D returned %08x\n", hr);
2822     hr = IDirect3D7_QueryInterface(d3d, &IID_IDirectDraw7, (void **) &ddraw);
2823     ok(hr == D3D_OK, "IDirect3D7_QueryInterface returned %08x\n", hr);
2824     IDirect3D7_Release(d3d);
2825
2826
2827     memset(&ddsd, 0, sizeof(ddsd));
2828     ddsd.dwSize = sizeof(ddsd);
2829     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
2830     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
2831     ddsd.dwWidth = 16;
2832     ddsd.dwHeight = 16;
2833     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX;
2834     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES | DDSCAPS2_TEXTUREMANAGE;
2835     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
2836     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
2837     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00FF0000;
2838     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000FF00;
2839     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000FF;
2840
2841     hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &cubemap, NULL);
2842     ok(hr == DD_OK, "IDirectDraw7_CreateSurface returned %08x\n", hr);
2843     IDirectDraw7_Release(ddraw);
2844
2845     /* Positive X */
2846     U5(DDBltFx).dwFillColor = 0x00ff0000;
2847     hr = IDirectDrawSurface7_Blt(cubemap, NULL, NULL, NULL, DDBLT_COLORFILL, &DDBltFx);
2848     ok(hr == DD_OK, "IDirectDrawSurface7_Blt returned %08x\n", hr);
2849
2850     memset(&caps, 0, sizeof(caps));
2851     caps.dwCaps = DDSCAPS_TEXTURE;
2852     caps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX;
2853     hr = IDirectDrawSurface_GetAttachedSurface(cubemap, &caps, &surface);
2854     ok(hr == DD_OK, "IDirectDrawSurface7_Lock returned %08x\n", hr);
2855     U5(DDBltFx).dwFillColor = 0x0000ffff;
2856     hr = IDirectDrawSurface7_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL, &DDBltFx);
2857     ok(hr == DD_OK, "IDirectDrawSurface7_Blt returned %08x\n", hr);
2858
2859     caps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ;
2860     hr = IDirectDrawSurface_GetAttachedSurface(cubemap, &caps, &surface);
2861     ok(hr == DD_OK, "IDirectDrawSurface7_Lock returned %08x\n", hr);
2862     U5(DDBltFx).dwFillColor = 0x0000ff00;
2863     hr = IDirectDrawSurface7_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL, &DDBltFx);
2864     ok(hr == DD_OK, "IDirectDrawSurface7_Blt returned %08x\n", hr);
2865
2866     caps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ;
2867     hr = IDirectDrawSurface_GetAttachedSurface(cubemap, &caps, &surface);
2868     ok(hr == DD_OK, "IDirectDrawSurface7_Lock returned %08x\n", hr);
2869     U5(DDBltFx).dwFillColor = 0x000000ff;
2870     hr = IDirectDrawSurface7_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL, &DDBltFx);
2871     ok(hr == DD_OK, "IDirectDrawSurface7_Blt returned %08x\n", hr);
2872
2873     caps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY;
2874     hr = IDirectDrawSurface_GetAttachedSurface(cubemap, &caps, &surface);
2875     ok(hr == DD_OK, "IDirectDrawSurface7_Lock returned %08x\n", hr);
2876     U5(DDBltFx).dwFillColor = 0x00ffff00;
2877     hr = IDirectDrawSurface7_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL, &DDBltFx);
2878     ok(hr == DD_OK, "IDirectDrawSurface7_Blt returned %08x\n", hr);
2879
2880     caps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY;
2881     hr = IDirectDrawSurface_GetAttachedSurface(cubemap, &caps, &surface);
2882     ok(hr == DD_OK, "IDirectDrawSurface7_Lock returned %08x\n", hr);
2883     U5(DDBltFx).dwFillColor = 0x00ff00ff;
2884     hr = IDirectDrawSurface7_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL, &DDBltFx);
2885     ok(hr == DD_OK, "IDirectDrawSurface7_Blt returned %08x\n", hr);
2886
2887     hr = IDirect3DDevice7_SetTexture(device, 0, cubemap);
2888     ok(hr == DD_OK, "IDirect3DDevice7_SetTexture returned %08x\n", hr);
2889     hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2890     ok(hr == DD_OK, "IDirect3DDevice7_SetTextureStageState returned %08x\n", hr);
2891     hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
2892     ok(hr == DD_OK, "IDirect3DDevice7_SetTextureStageState returned %08x\n", hr);
2893
2894     hr = IDirect3DDevice7_BeginScene(device);
2895     ok(hr == DD_OK, "IDirect3DDevice7_BeginScene returned %08x\n", hr);
2896     if(SUCCEEDED(hr))
2897     {
2898         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEX1, quad + 0 * 6, 4, 0);
2899         if (hr == DDERR_UNSUPPORTED || hr == DDERR_NODIRECTDRAWHW)
2900         {
2901             /* VMware */
2902             win_skip("IDirect3DDevice7_DrawPrimitive is not completely implemented, colors won't be tested\n");
2903             hr = IDirect3DDevice7_EndScene(device);
2904             ok(hr == DD_OK, "IDirect3DDevice7_EndScene returned %08x\n", hr);
2905             goto out;
2906         }
2907         ok(hr == DD_OK, "IDirect3DDevice7_DrawPrimitive returned %08x\n", hr);
2908         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEX1, quad + 4 * 6, 4, 0);
2909         ok(hr == DD_OK, "IDirect3DDevice7_DrawPrimitive returned %08x\n", hr);
2910         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEX1, quad + 8 * 6, 4, 0);
2911         ok(hr == DD_OK, "IDirect3DDevice7_DrawPrimitive returned %08x\n", hr);
2912         hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEX1, quad + 12* 6, 4, 0);
2913         ok(hr == DD_OK, "IDirect3DDevice7_DrawPrimitive returned %08x\n", hr);
2914
2915         hr = IDirect3DDevice7_EndScene(device);
2916         ok(hr == DD_OK, "IDirect3DDevice7_EndScene returned %08x\n", hr);
2917     }
2918     hr = IDirect3DDevice7_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_DISABLE);
2919     ok(hr == DD_OK, "IDirect3DDevice7_SetTextureStageState returned %08x\n", hr);
2920
2921     color = getPixelColor(device, 160, 360); /* lower left quad - positivex */
2922     ok(color == 0x00ff0000, "DDSCAPS2_CUBEMAP_POSITIVEX has color 0x%08x, expected 0x00ff0000\n", color);
2923     color = getPixelColor(device, 160, 120); /* upper left quad - negativex */
2924     ok(color == 0x0000ffff, "DDSCAPS2_CUBEMAP_NEGATIVEX has color 0x%08x, expected 0x0000ffff\n", color);
2925     color = getPixelColor(device, 480, 360); /* lower right quad - positivey */
2926     ok(color == 0x00ff00ff, "DDSCAPS2_CUBEMAP_POSITIVEY has color 0x%08x, expected 0x00ff00ff\n", color);
2927     color = getPixelColor(device, 480, 120); /* upper right quad - positivez */
2928     ok(color == 0x000000ff, "DDSCAPS2_CUBEMAP_POSITIVEZ has color 0x%08x, expected 0x000000ff\n", color);
2929
2930 out:
2931     hr = IDirect3DDevice7_SetTexture(device, 0, NULL);
2932     ok(hr == DD_OK, "IDirect3DDevice7_SetTexture returned %08x\n", hr);
2933     IDirectDrawSurface7_Release(cubemap);
2934 }
2935
2936 /* This test tests depth clamping / clipping behaviour:
2937  *   - With software vertex processing, depth values are clamped to the
2938  *     minimum / maximum z value when D3DRS_CLIPPING is disabled, and clipped
2939  *     when D3DRS_CLIPPING is enabled. Pretransformed vertices behave the
2940  *     same as regular vertices here.
2941  *   - With hardware vertex processing, D3DRS_CLIPPING seems to be ignored.
2942  *     Normal vertices are always clipped. Pretransformed vertices are
2943  *     clipped when D3DPMISCCAPS_CLIPTLVERTS is set, clamped when it isn't.
2944  *   - The viewport's MinZ/MaxZ is irrelevant for this.
2945  */
2946 static void depth_clamp_test(IDirect3DDevice7 *device)
2947 {
2948     struct tvertex quad1[] =
2949     {
2950         {  0.0f,   0.0f,  5.0f, 1.0f, 0xff002b7f},
2951         {640.0f,   0.0f,  5.0f, 1.0f, 0xff002b7f},
2952         {  0.0f, 480.0f,  5.0f, 1.0f, 0xff002b7f},
2953         {640.0f, 480.0f,  5.0f, 1.0f, 0xff002b7f},
2954     };
2955     struct tvertex quad2[] =
2956     {
2957         {  0.0f, 300.0f, 10.0f, 1.0f, 0xfff9e814},
2958         {640.0f, 300.0f, 10.0f, 1.0f, 0xfff9e814},
2959         {  0.0f, 360.0f, 10.0f, 1.0f, 0xfff9e814},
2960         {640.0f, 360.0f, 10.0f, 1.0f, 0xfff9e814},
2961     };
2962     struct tvertex quad3[] =
2963     {
2964         {112.0f, 108.0f,  5.0f, 1.0f, 0xffffffff},
2965         {208.0f, 108.0f,  5.0f, 1.0f, 0xffffffff},
2966         {112.0f, 204.0f,  5.0f, 1.0f, 0xffffffff},
2967         {208.0f, 204.0f,  5.0f, 1.0f, 0xffffffff},
2968     };
2969     struct tvertex quad4[] =
2970     {
2971         { 42.0f,  41.0f, 10.0f, 1.0f, 0xffffffff},
2972         {112.0f,  41.0f, 10.0f, 1.0f, 0xffffffff},
2973         { 42.0f, 108.0f, 10.0f, 1.0f, 0xffffffff},
2974         {112.0f, 108.0f, 10.0f, 1.0f, 0xffffffff},
2975     };
2976     struct vertex quad5[] =
2977     {
2978         { -0.5f,   0.5f, 10.0f,       0xff14f914},
2979         {  0.5f,   0.5f, 10.0f,       0xff14f914},
2980         { -0.5f,  -0.5f, 10.0f,       0xff14f914},
2981         {  0.5f,  -0.5f, 10.0f,       0xff14f914},
2982     };
2983     struct vertex quad6[] =
2984     {
2985         { -1.0f,   0.5f, 10.0f,       0xfff91414},
2986         {  1.0f,   0.5f, 10.0f,       0xfff91414},
2987         { -1.0f,  0.25f, 10.0f,       0xfff91414},
2988         {  1.0f,  0.25f, 10.0f,       0xfff91414},
2989     };
2990
2991     D3DVIEWPORT7 vp;
2992     D3DCOLOR color;
2993     HRESULT hr;
2994
2995     vp.dwX = 0;
2996     vp.dwY = 0;
2997     vp.dwWidth = 640;
2998     vp.dwHeight = 480;
2999     vp.dvMinZ = 0.0;
3000     vp.dvMaxZ = 7.5;
3001
3002     hr = IDirect3DDevice7_SetViewport(device, &vp);
3003     ok(SUCCEEDED(hr), "SetViewport failed, hr %#x.\n", hr);
3004
3005     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff00ff00, 1.0, 0);
3006     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
3007
3008     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
3009     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
3010     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
3011     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
3012     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZWRITEENABLE, TRUE);
3013     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
3014     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL);
3015     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
3016
3017     hr = IDirect3DDevice7_BeginScene(device);
3018     ok(SUCCEEDED(hr), "BeginScene failed, hr %#x.\n", hr);
3019
3020     hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, quad1, 4, 0);
3021     ok(SUCCEEDED(hr), "DrawPrimitive failed, hr %#x.\n", hr);
3022     hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, quad2, 4, 0);
3023     ok(SUCCEEDED(hr), "DrawPrimitive failed, hr %#x.\n", hr);
3024
3025     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CLIPPING, TRUE);
3026     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
3027
3028     hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, quad3, 4, 0);
3029     ok(SUCCEEDED(hr), "DrawPrimitive failed, hr %#x.\n", hr);
3030     hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, quad4, 4, 0);
3031     ok(SUCCEEDED(hr), "DrawPrimitive failed, hr %#x.\n", hr);
3032
3033     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
3034     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
3035
3036     hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE, quad5, 4, 0);
3037     ok(SUCCEEDED(hr), "DrawPrimitive failed, hr %#x.\n", hr);
3038
3039     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CLIPPING, TRUE);
3040     ok(SUCCEEDED(hr), "SetRenderState failed, hr %#x.\n", hr);
3041
3042     hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE, quad6, 4, 0);
3043     ok(SUCCEEDED(hr), "DrawPrimitive failed, hr %#x.\n", hr);
3044
3045     hr = IDirect3DDevice7_EndScene(device);
3046     ok(SUCCEEDED(hr), "EndScene failed, hr %#x.\n", hr);
3047
3048     color = getPixelColor(device, 75, 75);
3049     ok(color_match(color, 0x00ffffff, 1) || color_match(color, 0x0000ff00, 1), "color 0x%08x.\n", color);
3050     color = getPixelColor(device, 150, 150);
3051     ok(color_match(color, 0x00ffffff, 1) || color_match(color, 0x0000ff00, 1), "color 0x%08x.\n", color);
3052     color = getPixelColor(device, 320, 240);
3053     ok(color_match(color, 0x00002b7f, 1) || color_match(color, 0x0000ff00, 1), "color 0x%08x.\n", color);
3054     color = getPixelColor(device, 320, 330);
3055     ok(color_match(color, 0x00f9e814, 1) || color_match(color, 0x0000ff00, 1), "color 0x%08x.\n", color);
3056     color = getPixelColor(device, 320, 330);
3057     ok(color_match(color, 0x00f9e814, 1) || color_match(color, 0x0000ff00, 1), "color 0x%08x.\n", color);
3058
3059     vp.dvMinZ = 0.0;
3060     vp.dvMaxZ = 1.0;
3061     hr = IDirect3DDevice7_SetViewport(device, &vp);
3062     ok(SUCCEEDED(hr), "SetViewport failed, hr %#x.\n", hr);
3063 }
3064
3065 static void DX1_BackBufferFlipTest(void)
3066 {
3067     HRESULT hr;
3068     IDirectDraw *DirectDraw1 = NULL;
3069     IDirectDrawSurface *Primary = NULL;
3070     IDirectDrawSurface *Backbuffer = NULL;
3071     WNDCLASS wc = {0};
3072     DDSURFACEDESC ddsd;
3073     DDBLTFX ddbltfx;
3074     COLORREF color;
3075     const DWORD white = 0xffffff;
3076     const DWORD red = 0xff0000;
3077     BOOL attached = FALSE;
3078
3079     wc.lpfnWndProc = DefWindowProc;
3080     wc.lpszClassName = "DX1_BackBufferFlipTest_wc";
3081     RegisterClass(&wc);
3082     window = CreateWindow("DX1_BackBufferFlipTest_wc", "DX1_BackBufferFlipTest",
3083                             WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
3084
3085     hr = DirectDrawCreate( NULL, &DirectDraw1, NULL );
3086     ok(hr==DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreate returned: %x\n", hr);
3087     if(FAILED(hr)) goto out;
3088
3089     hr = IDirectDraw_SetCooperativeLevel(DirectDraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3090     ok(hr==DD_OK, "SetCooperativeLevel returned: %x\n", hr);
3091     if(FAILED(hr)) goto out;
3092
3093     hr = IDirectDraw_SetDisplayMode(DirectDraw1, 640, 480, 32);
3094     if(FAILED(hr)) {
3095         /* 24 bit is fine too */
3096         hr = IDirectDraw_SetDisplayMode(DirectDraw1, 640, 480, 24);
3097     }
3098     ok(hr==DD_OK || hr == DDERR_UNSUPPORTED, "SetDisplayMode returned: %x\n", hr);
3099     if (FAILED(hr)) {
3100         goto out;
3101     }
3102
3103     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
3104     ddsd.dwSize = sizeof(DDSURFACEDESC);
3105     ddsd.dwFlags = DDSD_CAPS;
3106     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3107
3108     hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &Primary, NULL);
3109     ok(hr==DD_OK, "IDirectDraw_CreateSurface returned: %08x\n", hr);
3110
3111     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
3112     ddsd.dwSize = sizeof(DDSURFACEDESC);
3113     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
3114     ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
3115     ddsd.dwWidth = 640;
3116     ddsd.dwHeight = 480;
3117     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
3118     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
3119     U1(ddsd.ddpfPixelFormat).dwRGBBitCount      = 32;
3120     U2(ddsd.ddpfPixelFormat).dwRBitMask         = 0x00ff0000;
3121     U3(ddsd.ddpfPixelFormat).dwGBitMask         = 0x0000ff00;
3122     U4(ddsd.ddpfPixelFormat).dwBBitMask         = 0x000000ff;
3123
3124     hr = IDirectDraw_CreateSurface(DirectDraw1, &ddsd, &Backbuffer, NULL);
3125     ok(hr==DD_OK, "IDirectDraw_CreateSurface returned: %08x\n", hr);
3126     if(FAILED(hr)) goto out;
3127
3128     hr = IDirectDrawSurface_AddAttachedSurface(Primary, Backbuffer);
3129     todo_wine ok(hr == DD_OK || broken(hr == DDERR_CANNOTATTACHSURFACE),
3130        "Attaching a back buffer to a front buffer returned %08x\n", hr);
3131     if (FAILED(hr)) goto out;
3132
3133     attached = TRUE;
3134
3135     memset(&ddbltfx, 0, sizeof(ddbltfx));
3136     ddbltfx.dwSize = sizeof(ddbltfx);
3137     U5(ddbltfx).dwFillColor = red;
3138     hr = IDirectDrawSurface_Blt(Backbuffer, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
3139     ok(hr == DD_OK, "IDirectDrawSurface_Blt returned: %x\n", hr);
3140
3141     U5(ddbltfx).dwFillColor = white;
3142     hr = IDirectDrawSurface_Blt(Primary, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
3143     ok(hr == DD_OK, "IDirectDrawSurface_Blt returned: %x\n", hr);
3144
3145     /* Check it out */
3146     color = getPixelColor_GDI(Primary, 5, 5);
3147     ok(GetRValue(color) == 0xFF && GetGValue(color) == 0xFF && GetBValue(color) == 0xFF,
3148             "got R %02X G %02X B %02X, expected R FF G FF B FF\n",
3149             GetRValue(color), GetGValue(color), GetBValue(color));
3150
3151     color = getPixelColor_GDI(Backbuffer, 5, 5);
3152     ok(GetRValue(color) == 0xFF && GetGValue(color) == 0 && GetBValue(color) == 0,
3153             "got R %02X G %02X B %02X, expected R FF G 00 B 00\n",
3154             GetRValue(color), GetGValue(color), GetBValue(color));
3155
3156     hr = IDirectDrawSurface_Flip(Primary, NULL, DDFLIP_WAIT);
3157     todo_wine ok(hr == DD_OK, "IDirectDrawSurface_Flip returned 0x%08x\n", hr);
3158
3159     if (hr == DD_OK)
3160     {
3161         color = getPixelColor_GDI(Primary, 5, 5);
3162         ok(GetRValue(color) == 0xFF && GetGValue(color) == 0 && GetBValue(color) == 0,
3163                 "got R %02X G %02X B %02X, expected R FF G 00 B 00\n",
3164                 GetRValue(color), GetGValue(color), GetBValue(color));
3165
3166         color = getPixelColor_GDI(Backbuffer, 5, 5);
3167         ok((GetRValue(color) == 0xFF && GetGValue(color) == 0xFF && GetBValue(color) == 0xFF) ||
3168            broken(GetRValue(color) == 0xFF && GetGValue(color) == 0 && GetBValue(color) == 0),  /* broken driver */
3169                 "got R %02X G %02X B %02X, expected R FF G FF B FF\n",
3170                 GetRValue(color), GetGValue(color), GetBValue(color));
3171     }
3172
3173     out:
3174
3175     if (Backbuffer)
3176     {
3177         if (attached)
3178             IDirectDrawSurface_DeleteAttachedSurface(Primary, 0, Backbuffer);
3179         IDirectDrawSurface_Release(Backbuffer);
3180     }
3181     if (Primary) IDirectDrawSurface_Release(Primary);
3182     if (DirectDraw1) IDirectDraw_Release(DirectDraw1);
3183     if (window) DestroyWindow(window);
3184 }
3185
3186 START_TEST(visual)
3187 {
3188     HRESULT hr;
3189     DWORD color;
3190     if(!createObjects())
3191     {
3192         skip("Cannot initialize DirectDraw and Direct3D, skipping\n");
3193         return;
3194     }
3195
3196     /* Check for the reliability of the returned data */
3197     hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
3198     if(FAILED(hr))
3199     {
3200         skip("Clear failed, can't assure correctness of the test results, skipping\n");
3201         goto cleanup;
3202     }
3203
3204     color = getPixelColor(Direct3DDevice, 1, 1);
3205     if(color !=0x00ff0000)
3206     {
3207         skip("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
3208         goto cleanup;
3209     }
3210
3211     hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
3212     if(FAILED(hr))
3213     {
3214         skip("Clear failed, can't assure correctness of the test results, skipping\n");
3215         goto cleanup;
3216     }
3217
3218     color = getPixelColor(Direct3DDevice, 639, 479);
3219     if(color != 0x0000ddee)
3220     {
3221         skip("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
3222         goto cleanup;
3223     }
3224
3225     /* Now run the tests */
3226     blt_test(Direct3DDevice);
3227     depth_clamp_test(Direct3DDevice);
3228     lighting_test(Direct3DDevice);
3229     clear_test(Direct3DDevice);
3230     fog_test(Direct3DDevice);
3231     offscreen_test(Direct3DDevice);
3232     alpha_test(Direct3DDevice);
3233     rhw_zero_test(Direct3DDevice);
3234     cubemap_test(Direct3DDevice);
3235
3236     releaseObjects(); /* release DX7 interfaces to test D3D1 */
3237
3238     if(!D3D1_createObjects()) {
3239         skip("Cannot initialize D3D1, skipping\n");
3240     }
3241     else {
3242         D3D1_TextureMapBlendTest();
3243         D3D1_ViewportClearTest();
3244     }
3245     D3D1_releaseObjects();
3246
3247     D3D3_ViewportClearTest();
3248     p8_primary_test();
3249     DX1_BackBufferFlipTest();
3250
3251     return ;
3252
3253 cleanup:
3254     releaseObjects();
3255 }