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