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