mcicda: Exclude unused headers.
[wine] / dlls / ddraw / tests / d3d.c
1 /*
2  * Some unit tests for d3d functions
3  *
4  * Copyright (C) 2005 Antoine Chavasse
5  * Copyright (C) 2006 Stefan Dösinger for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <assert.h>
23 #include "wine/test.h"
24 #include "ddraw.h"
25 #include "d3d.h"
26
27 static LPDIRECTDRAW7           lpDD = NULL;
28 static LPDIRECT3D7             lpD3D = NULL;
29 static LPDIRECTDRAWSURFACE7    lpDDS = NULL;
30 static LPDIRECT3DDEVICE7       lpD3DDevice = NULL;
31 static LPDIRECT3DVERTEXBUFFER7 lpVBufSrc = NULL;
32 static LPDIRECT3DVERTEXBUFFER7 lpVBufDest1 = NULL;
33 static LPDIRECT3DVERTEXBUFFER7 lpVBufDest2 = NULL;
34
35 /* To compare bad floating point numbers. Not the ideal way to do it,
36  * but it should be enough for here */
37 #define comparefloat(a, b) ( (((a) - (b)) < 0.0001) && (((a) - (b)) > -0.0001) )
38
39 static HRESULT (WINAPI *pDirectDrawCreateEx)(LPGUID,LPVOID*,REFIID,LPUNKNOWN);
40
41 typedef struct _VERTEX
42 {
43     float x, y, z;  /* position */
44 } VERTEX, *LPVERTEX;
45
46 typedef struct _TVERTEX
47 {
48     float x, y, z;  /* position */
49     float rhw;
50 } TVERTEX, *LPTVERTEX;
51
52
53 static void init_function_pointers(void)
54 {
55     HMODULE hmod = GetModuleHandleA("ddraw.dll");
56     pDirectDrawCreateEx = (void*)GetProcAddress(hmod, "DirectDrawCreateEx");
57 }
58
59
60 static BOOL CreateDirect3D(void)
61 {
62     HRESULT rc;
63     DDSURFACEDESC2 ddsd;
64
65     rc = pDirectDrawCreateEx(NULL, (void**)&lpDD,
66         &IID_IDirectDraw7, NULL);
67     ok(rc==DD_OK || rc==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", rc);
68     if (!lpDD) {
69         trace("DirectDrawCreateEx() failed with an error %x\n", rc);
70         return FALSE;
71     }
72
73     rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
74     ok(rc==DD_OK, "SetCooperativeLevel returned: %x\n", rc);
75
76     rc = IDirectDraw7_QueryInterface(lpDD, &IID_IDirect3D7, (void**) &lpD3D);
77     if (rc == E_NOINTERFACE) return FALSE;
78     ok(rc==DD_OK, "QueryInterface returned: %x\n", rc);
79
80     memset(&ddsd, 0, sizeof(ddsd));
81     ddsd.dwSize = sizeof(ddsd);
82     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
83     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
84     ddsd.dwWidth = 256;
85     ddsd.dwHeight = 256;
86     rc = IDirectDraw7_CreateSurface(lpDD, &ddsd, &lpDDS, NULL);
87     ok(rc==DD_OK, "CreateSurface returned: %x\n", rc);
88
89     rc = IDirect3D7_CreateDevice(lpD3D, &IID_IDirect3DTnLHalDevice, lpDDS,
90         &lpD3DDevice);
91     ok(rc==D3D_OK || rc==DDERR_NOPALETTEATTACHED || rc==E_OUTOFMEMORY, "CreateDevice returned: %x\n", rc);
92     if (!lpD3DDevice) {
93         trace("IDirect3D7::CreateDevice() for a TnL Hal device failed with an error %x, trying HAL\n", rc);
94         rc = IDirect3D7_CreateDevice(lpD3D, &IID_IDirect3DHALDevice, lpDDS,
95             &lpD3DDevice);
96         if (!lpD3DDevice) {
97             trace("IDirect3D7::CreateDevice() for a HAL device failed with an error %x, trying RGB\n", rc);
98             rc = IDirect3D7_CreateDevice(lpD3D, &IID_IDirect3DRGBDevice, lpDDS,
99                 &lpD3DDevice);
100             if (!lpD3DDevice) {
101                 trace("IDirect3D7::CreateDevice() for a RGB device failed with an error %x, giving up\n", rc);
102                 return FALSE;
103             }
104         }
105     }
106
107     return TRUE;
108 }
109
110 static void ReleaseDirect3D(void)
111 {
112     if (lpD3DDevice != NULL)
113     {
114         IDirect3DDevice7_Release(lpD3DDevice);
115         lpD3DDevice = NULL;
116     }
117
118     if (lpDDS != NULL)
119     {
120         IDirectDrawSurface_Release(lpDDS);
121         lpDDS = NULL;
122     }
123
124     if (lpD3D != NULL)
125     {
126         IDirect3D7_Release(lpD3D);
127         lpD3D = NULL;
128     }
129
130     if (lpDD != NULL)
131     {
132         IDirectDraw_Release(lpDD);
133         lpDD = NULL;
134     }
135 }
136
137 static void LightTest(void)
138 {
139     HRESULT rc;
140     D3DLIGHT7 light;
141     D3DLIGHT7 defaultlight;
142     BOOL bEnabled = FALSE;
143
144     /* Set a few lights with funky indices. */
145     memset(&light, 0, sizeof(light));
146     light.dltType = D3DLIGHT_DIRECTIONAL;
147     U1(light.dcvDiffuse).r = 0.5f;
148     U2(light.dcvDiffuse).g = 0.6f;
149     U3(light.dcvDiffuse).b = 0.7f;
150     U2(light.dvDirection).y = 1.f;
151
152     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 5, &light);
153     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
154     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 10, &light);
155     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
156     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 45, &light);
157     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
158
159
160     /* Try to retrieve a light beyond the indices of the lights that have
161        been set. */
162     rc = IDirect3DDevice7_GetLight(lpD3DDevice, 50, &light);
163     ok(rc==DDERR_INVALIDPARAMS, "GetLight returned: %x\n", rc);
164     rc = IDirect3DDevice7_GetLight(lpD3DDevice, 2, &light);
165     ok(rc==DDERR_INVALIDPARAMS, "GetLight returned: %x\n", rc);
166
167
168     /* Try to retrieve one of the lights that have been set */
169     rc = IDirect3DDevice7_GetLight(lpD3DDevice, 10, &light);
170     ok(rc==D3D_OK, "GetLight returned: %x\n", rc);
171
172
173     /* Enable a light that have been previously set. */
174     rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 10, TRUE);
175     ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
176
177
178     /* Enable some lights that have not been previously set, and verify that
179        they have been initialized with proper default values. */
180     memset(&defaultlight, 0, sizeof(D3DLIGHT7));
181     defaultlight.dltType = D3DLIGHT_DIRECTIONAL;
182     U1(defaultlight.dcvDiffuse).r = 1.f;
183     U2(defaultlight.dcvDiffuse).g = 1.f;
184     U3(defaultlight.dcvDiffuse).b = 1.f;
185     U3(defaultlight.dvDirection).z = 1.f;
186
187     rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 20, TRUE);
188     ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
189     memset(&light, 0, sizeof(D3DLIGHT7));
190     rc = IDirect3DDevice7_GetLight(lpD3DDevice, 20, &light);
191     ok(rc==D3D_OK, "GetLight returned: %x\n", rc);
192     ok(!memcmp(&light, &defaultlight, sizeof(D3DLIGHT7)),
193         "light data doesn't match expected default values\n" );
194
195     rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 50, TRUE);
196     ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
197     memset(&light, 0, sizeof(D3DLIGHT7));
198     rc = IDirect3DDevice7_GetLight(lpD3DDevice, 50, &light);
199     ok(rc==D3D_OK, "GetLight returned: %x\n", rc);
200     ok(!memcmp(&light, &defaultlight, sizeof(D3DLIGHT7)),
201         "light data doesn't match expected default values\n" );
202
203
204     /* Disable one of the light that have been previously enabled. */
205     rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 20, FALSE);
206     ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
207
208     /* Try to retrieve the enable status of some lights */
209     /* Light 20 is supposed to be disabled */
210     rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 20, &bEnabled );
211     ok(rc==D3D_OK, "GetLightEnable returned: %x\n", rc);
212     ok(!bEnabled, "GetLightEnable says the light is enabled\n");
213
214     /* Light 10 is supposed to be enabled */
215     bEnabled = FALSE;
216     rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 10, &bEnabled );
217     ok(rc==D3D_OK, "GetLightEnable returned: %x\n", rc);
218     ok(bEnabled, "GetLightEnable says the light is disabled\n");
219
220     /* Light 80 has not been set */
221     rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 80, &bEnabled );
222     ok(rc==DDERR_INVALIDPARAMS, "GetLightEnable returned: %x\n", rc);
223
224     /* Light 23 has not been set */
225     rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 23, &bEnabled );
226     ok(rc==DDERR_INVALIDPARAMS, "GetLightEnable returned: %x\n", rc);
227
228     /* Set some lights with invalid parameters */
229     memset(&light, 0, sizeof(D3DLIGHT7));
230     light.dltType = 0;
231     U1(light.dcvDiffuse).r = 1.f;
232     U2(light.dcvDiffuse).g = 1.f;
233     U3(light.dcvDiffuse).b = 1.f;
234     U3(light.dvDirection).z = 1.f;
235     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 100, &light);
236     ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);
237
238     memset(&light, 0, sizeof(D3DLIGHT7));
239     light.dltType = 12345;
240     U1(light.dcvDiffuse).r = 1.f;
241     U2(light.dcvDiffuse).g = 1.f;
242     U3(light.dcvDiffuse).b = 1.f;
243     U3(light.dvDirection).z = 1.f;
244     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 101, &light);
245     ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);
246
247     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 102, NULL);
248     ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);
249
250     memset(&light, 0, sizeof(D3DLIGHT7));
251     light.dltType = D3DLIGHT_SPOT;
252     U1(light.dcvDiffuse).r = 1.f;
253     U2(light.dcvDiffuse).g = 1.f;
254     U3(light.dcvDiffuse).b = 1.f;
255     U3(light.dvDirection).z = 1.f;
256
257     light.dvAttenuation0 = -1.0 / 0.0; /* -INFINITY */
258     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
259     ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);
260
261     light.dvAttenuation0 = -1.0;
262     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
263     ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);
264
265     light.dvAttenuation0 = 0.0;
266     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
267     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
268
269     light.dvAttenuation0 = 1.0;
270     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
271     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
272
273     light.dvAttenuation0 = 1.0 / 0.0; /* +INFINITY */
274     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
275     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
276
277     light.dvAttenuation0 = 0.0 / 0.0; /* NaN */
278     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
279     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
280
281     /* Directional light ignores attenuation */
282     light.dltType = D3DLIGHT_DIRECTIONAL;
283     light.dvAttenuation0 = -1.0;
284     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
285     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
286 }
287
288 static void ProcessVerticesTest(void)
289 {
290     D3DVERTEXBUFFERDESC desc;
291     HRESULT rc;
292     VERTEX *in;
293     TVERTEX *out;
294     VERTEX *out2;
295     D3DVIEWPORT7 vp;
296     D3DMATRIX view = {  2.0, 0.0, 0.0, 0.0,
297                         0.0, -1.0, 0.0, 0.0,
298                         0.0, 0.0, 1.0, 0.0,
299                         0.0, 0.0, 0.0, 3.0 };
300
301     D3DMATRIX world = { 0.0, 1.0, 0.0, 0.0,
302                         1.0, 0.0, 0.0, 0.0,
303                         0.0, 0.0, 0.0, 1.0,
304                         0.0, 1.0, 1.0, 1.0 };
305
306     D3DMATRIX proj = {  1.0, 0.0, 0.0, 1.0,
307                         0.0, 1.0, 1.0, 0.0,
308                         0.0, 1.0, 1.0, 0.0,
309                         1.0, 0.0, 0.0, 1.0 };
310     /* Create some vertex buffers */
311
312     memset(&desc, 0, sizeof(desc));
313     desc.dwSize = sizeof(desc);
314     desc.dwCaps = 0;
315     desc.dwFVF = D3DFVF_XYZ;
316     desc.dwNumVertices = 16;
317     rc = IDirect3D7_CreateVertexBuffer(lpD3D, &desc, &lpVBufSrc, 0);
318     ok(rc==D3D_OK || rc==E_OUTOFMEMORY, "CreateVertexBuffer returned: %x\n", rc);
319     if (!lpVBufSrc)
320     {
321         trace("IDirect3D7::CreateVertexBuffer() failed with an error %x\n", rc);
322         goto out;
323     }
324
325     memset(&desc, 0, sizeof(desc));
326     desc.dwSize = sizeof(desc);
327     desc.dwCaps = 0;
328     desc.dwFVF = D3DFVF_XYZRHW;
329     desc.dwNumVertices = 16;
330     /* Msdn says that the last parameter must be 0 - check that */
331     rc = IDirect3D7_CreateVertexBuffer(lpD3D, &desc, &lpVBufDest1, 4);
332     ok(rc==D3D_OK || rc==E_OUTOFMEMORY, "CreateVertexBuffer returned: %x\n", rc);
333     if (!lpVBufDest1)
334     {
335         trace("IDirect3D7::CreateVertexBuffer() failed with an error %x\n", rc);
336         goto out;
337     }
338
339     memset(&desc, 0, sizeof(desc));
340     desc.dwSize = sizeof(desc);
341     desc.dwCaps = 0;
342     desc.dwFVF = D3DFVF_XYZ;
343     desc.dwNumVertices = 16;
344     /* Msdn says that the last parameter must be 0 - check that */
345     rc = IDirect3D7_CreateVertexBuffer(lpD3D, &desc, &lpVBufDest2, 12345678);
346     ok(rc==D3D_OK || rc==E_OUTOFMEMORY, "CreateVertexBuffer returned: %x\n", rc);
347     if (!lpVBufDest2)
348     {
349         trace("IDirect3D7::CreateVertexBuffer() failed with an error %x\n", rc);
350         goto out;
351     }
352
353     rc = IDirect3DVertexBuffer7_Lock(lpVBufSrc, 0, (void **) &in, NULL);
354     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Lock returned: %x\n", rc);
355     if(!in) goto out;
356
357     /* Check basic transformation */
358
359     in[0].x = 0.0;
360     in[0].y = 0.0;
361     in[0].z = 0.0;
362
363     in[1].x = 1.0;
364     in[1].y = 1.0;
365     in[1].z = 1.0;
366
367     in[2].x = -1.0;
368     in[2].y = -1.0;
369     in[2].z = 0.5;
370
371     in[3].x = 0.5;
372     in[3].y = -0.5;
373     in[3].z = 0.25;
374     rc = IDirect3DVertexBuffer7_Unlock(lpVBufSrc);
375     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Unlock returned: %x\n", rc);
376
377     rc = IDirect3DVertexBuffer7_ProcessVertices(lpVBufDest1, D3DVOP_TRANSFORM, 0, 4, lpVBufSrc, 0, lpD3DDevice, 0);
378     ok(rc==D3D_OK , "IDirect3DVertexBuffer::ProcessVertices returned: %x\n", rc);
379
380     rc = IDirect3DVertexBuffer7_ProcessVertices(lpVBufDest2, D3DVOP_TRANSFORM, 0, 4, lpVBufSrc, 0, lpD3DDevice, 0);
381     ok(rc==D3D_OK , "IDirect3DVertexBuffer::ProcessVertices returned: %x\n", rc);
382
383     rc = IDirect3DVertexBuffer7_Lock(lpVBufDest1, 0, (void **) &out, NULL);
384     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Lock returned: %x\n", rc);
385     if(!out) goto out;
386
387     /* Check the results */
388     ok( comparefloat(out[0].x, 128.0 ) &&
389         comparefloat(out[0].y, 128.0 ) &&
390         comparefloat(out[0].z, 0.0 ) &&
391         comparefloat(out[0].rhw, 1.0 ),
392         "Output 0 vertex is (%f , %f , %f , %f)\n", out[0].x, out[0].y, out[0].z, out[0].rhw);
393
394     ok( comparefloat(out[1].x, 256.0 ) &&
395         comparefloat(out[1].y, 0.0 ) &&
396         comparefloat(out[1].z, 1.0 ) &&
397         comparefloat(out[1].rhw, 1.0 ),
398         "Output 1 vertex is (%f , %f , %f , %f)\n", out[1].x, out[1].y, out[1].z, out[1].rhw);
399
400     ok( comparefloat(out[2].x, 0.0 ) &&
401         comparefloat(out[2].y, 256.0 ) &&
402         comparefloat(out[2].z, 0.5 ) &&
403         comparefloat(out[2].rhw, 1.0 ),
404         "Output 2 vertex is (%f , %f , %f , %f)\n", out[2].x, out[2].y, out[2].z, out[2].rhw);
405
406     ok( comparefloat(out[3].x, 192.0 ) &&
407         comparefloat(out[3].y, 192.0 ) &&
408         comparefloat(out[3].z, 0.25 ) &&
409         comparefloat(out[3].rhw, 1.0 ),
410         "Output 3 vertex is (%f , %f , %f , %f)\n", out[3].x, out[3].y, out[3].z, out[3].rhw);
411
412     rc = IDirect3DVertexBuffer7_Unlock(lpVBufDest1);
413     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Unlock returned: %x\n", rc);
414     out = NULL;
415
416     rc = IDirect3DVertexBuffer7_Lock(lpVBufDest2, 0, (void **) &out2, NULL);
417     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Lock returned: %x\n", rc);
418     if(!out2) goto out;
419     /* Small thing without much practial meaning, but I stumbled upon it,
420      * so let's check for it: If the output vertex buffer has to RHW value,
421      * The RHW value of the last vertex is written into the next vertex
422      */
423     ok( comparefloat(out2[4].x, 1.0 ) &&
424         comparefloat(out2[4].y, 0.0 ) &&
425         comparefloat(out2[4].z, 0.0 ),
426         "Output 4 vertex is (%f , %f , %f)\n", out2[4].x, out2[4].y, out2[4].z);
427
428     rc = IDirect3DVertexBuffer7_Unlock(lpVBufDest2);
429     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Unlock returned: %x\n", rc);
430     out = NULL;
431
432     /* Try a more complicated viewport, same vertices */
433     memset(&vp, 0, sizeof(vp));
434     vp.dwX = 10;
435     vp.dwY = 5;
436     vp.dwWidth = 246;
437     vp.dwHeight = 130;
438     vp.dvMinZ = -2.0;
439     vp.dvMaxZ = 4.0;
440     rc = IDirect3DDevice7_SetViewport(lpD3DDevice, &vp);
441     ok(rc==D3D_OK, "IDirect3DDevice7_SetViewport failed with rc=%x\n", rc);
442
443     /* Process again */
444     rc = IDirect3DVertexBuffer7_ProcessVertices(lpVBufDest1, D3DVOP_TRANSFORM, 0, 4, lpVBufSrc, 0, lpD3DDevice, 0);
445     ok(rc==D3D_OK , "IDirect3DVertexBuffer::ProcessVertices returned: %x\n", rc);
446
447     rc = IDirect3DVertexBuffer7_Lock(lpVBufDest1, 0, (void **) &out, NULL);
448     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Lock returned: %x\n", rc);
449     if(!out) goto out;
450
451     /* Check the results */
452     ok( comparefloat(out[0].x, 133.0 ) &&
453         comparefloat(out[0].y, 70.0 ) &&
454         comparefloat(out[0].z, -2.0 ) &&
455         comparefloat(out[0].rhw, 1.0 ),
456         "Output 0 vertex is (%f , %f , %f , %f)\n", out[0].x, out[0].y, out[0].z, out[0].rhw);
457
458     ok( comparefloat(out[1].x, 256.0 ) &&
459         comparefloat(out[1].y, 5.0 ) &&
460         comparefloat(out[1].z, 4.0 ) &&
461         comparefloat(out[1].rhw, 1.0 ),
462         "Output 1 vertex is (%f , %f , %f , %f)\n", out[1].x, out[1].y, out[1].z, out[1].rhw);
463
464     ok( comparefloat(out[2].x, 10.0 ) &&
465         comparefloat(out[2].y, 135.0 ) &&
466         comparefloat(out[2].z, 1.0 ) &&
467         comparefloat(out[2].rhw, 1.0 ),
468         "Output 2 vertex is (%f , %f , %f , %f)\n", out[1].x, out[1].y, out[1].z, out[1].rhw);
469
470     ok( comparefloat(out[3].x, 194.5 ) &&
471         comparefloat(out[3].y, 102.5 ) &&
472         comparefloat(out[3].z, -0.5 ) &&
473         comparefloat(out[3].rhw, 1.0 ),
474         "Output 3 vertex is (%f , %f , %f , %f)\n", out[3].x, out[3].y, out[3].z, out[3].rhw);
475
476     rc = IDirect3DVertexBuffer7_Unlock(lpVBufDest1);
477     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Unlock returned: %x\n", rc);
478     out = NULL;
479
480     /* Play with some matrices. */
481
482     rc = IDirect3DDevice7_SetTransform(lpD3DDevice, D3DTRANSFORMSTATE_VIEW, &view);
483     ok(rc==D3D_OK, "IDirect3DDevice7_SetTransform failed\n");
484
485     rc = IDirect3DDevice7_SetTransform(lpD3DDevice, D3DTRANSFORMSTATE_PROJECTION, &proj);
486     ok(rc==D3D_OK, "IDirect3DDevice7_SetTransform failed\n");
487
488     rc = IDirect3DDevice7_SetTransform(lpD3DDevice, D3DTRANSFORMSTATE_WORLD, &world);
489     ok(rc==D3D_OK, "IDirect3DDevice7_SetTransform failed\n");
490
491     rc = IDirect3DVertexBuffer7_ProcessVertices(lpVBufDest1, D3DVOP_TRANSFORM, 0, 4, lpVBufSrc, 0, lpD3DDevice, 0);
492     ok(rc==D3D_OK , "IDirect3DVertexBuffer::ProcessVertices returned: %x\n", rc);
493
494     rc = IDirect3DVertexBuffer7_Lock(lpVBufDest1, 0, (void **) &out, NULL);
495     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Lock returned: %x\n", rc);
496     if(!out) goto out;
497
498     /* Keep the viewport simpler, otherwise we get bad numbers to compare */
499     vp.dwX = 0;
500     vp.dwY = 0;
501     vp.dwWidth = 100;
502     vp.dwHeight = 100;
503     vp.dvMinZ = 1.0;
504     vp.dvMaxZ = 0.0;
505     rc = IDirect3DDevice7_SetViewport(lpD3DDevice, &vp);
506     ok(rc==D3D_OK, "IDirect3DDevice7_SetViewport failed\n");
507
508     /* Check the results */
509     ok( comparefloat(out[0].x, 256.0 ) &&    /* X coordinate is cut at the surface edges */
510         comparefloat(out[0].y, 70.0 ) &&
511         comparefloat(out[0].z, -2.0 ) &&
512         comparefloat(out[0].rhw, (1.0 / 3.0)),
513         "Output 0 vertex is (%f , %f , %f , %f)\n", out[0].x, out[0].y, out[0].z, out[0].rhw);
514
515     ok( comparefloat(out[1].x, 256.0 ) &&
516         comparefloat(out[1].y, 78.125000 ) &&
517         comparefloat(out[1].z, -2.750000 ) &&
518         comparefloat(out[1].rhw, 0.125000 ),
519         "Output 1 vertex is (%f , %f , %f , %f)\n", out[1].x, out[1].y, out[1].z, out[1].rhw);
520
521     ok( comparefloat(out[2].x, 256.0 ) &&
522         comparefloat(out[2].y, 44.000000 ) &&
523         comparefloat(out[2].z, 0.400000 ) &&
524         comparefloat(out[2].rhw, 0.400000 ),
525         "Output 2 vertex is (%f , %f , %f , %f)\n", out[2].x, out[2].y, out[2].z, out[2].rhw);
526
527     ok( comparefloat(out[3].x, 256.0 ) &&
528         comparefloat(out[3].y, 81.818184 ) &&
529         comparefloat(out[3].z, -3.090909 ) &&
530         comparefloat(out[3].rhw, 0.363636 ),
531         "Output 3 vertex is (%f , %f , %f , %f)\n", out[3].x, out[3].y, out[3].z, out[3].rhw);
532
533     rc = IDirect3DVertexBuffer7_Unlock(lpVBufDest1);
534     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Unlock returned: %x\n", rc);
535     out = NULL;
536
537 out:
538     IDirect3DVertexBuffer7_Release(lpVBufSrc);
539     IDirect3DVertexBuffer7_Release(lpVBufDest1);
540     IDirect3DVertexBuffer7_Release(lpVBufDest2);
541 }
542
543 static void StateTest( void )
544 {
545     HRESULT rc;
546
547     /* The msdn says its undocumented, does it return an error too? */
548     rc = IDirect3DDevice7_SetRenderState(lpD3DDevice, D3DRENDERSTATE_ZVISIBLE, TRUE);
549     ok(rc == D3D_OK, "IDirect3DDevice7_SetRenderState(D3DRENDERSTATE_ZVISIBLE, TRUE) returned %08x\n", rc);
550     rc = IDirect3DDevice7_SetRenderState(lpD3DDevice, D3DRENDERSTATE_ZVISIBLE, FALSE);
551     ok(rc == D3D_OK, "IDirect3DDevice7_SetRenderState(D3DRENDERSTATE_ZVISIBLE, FALSE) returned %08x\n", rc);
552 }
553
554
555 static void SceneTest(void)
556 {
557     HRESULT                      hr;
558
559     /* Test an EndScene without beginscene. Should return an error */
560     hr = IDirect3DDevice7_EndScene(lpD3DDevice);
561     ok(hr == D3DERR_SCENE_NOT_IN_SCENE, "IDirect3DDevice7_EndScene returned %08x\n", hr);
562
563     /* Test a normal BeginScene / EndScene pair, this should work */
564     hr = IDirect3DDevice7_BeginScene(lpD3DDevice);
565     ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
566     if(SUCCEEDED(hr))
567     {
568         hr = IDirect3DDevice7_EndScene(lpD3DDevice);
569         ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
570     }
571
572     /* Test another EndScene without having begun a new scene. Should return an error */
573     hr = IDirect3DDevice7_EndScene(lpD3DDevice);
574     ok(hr == D3DERR_SCENE_NOT_IN_SCENE, "IDirect3DDevice7_EndScene returned %08x\n", hr);
575
576     /* Two nested BeginScene and EndScene calls */
577     hr = IDirect3DDevice7_BeginScene(lpD3DDevice);
578     ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
579     hr = IDirect3DDevice7_BeginScene(lpD3DDevice);
580     ok(hr == D3DERR_SCENE_IN_SCENE, "IDirect3DDevice7_BeginScene returned %08x\n", hr);
581     hr = IDirect3DDevice7_EndScene(lpD3DDevice);
582     ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
583     hr = IDirect3DDevice7_EndScene(lpD3DDevice);
584     ok(hr == D3DERR_SCENE_NOT_IN_SCENE, "IDirect3DDevice7_EndScene returned %08x\n", hr);
585
586     /* TODO: Verify that blitting works in the same way as in d3d9 */
587 }
588
589 static void LimitTest(void)
590 {
591     IDirectDrawSurface7 *pTexture = NULL;
592     HRESULT hr;
593     int i;
594     DDSURFACEDESC2 ddsd;
595
596     memset(&ddsd, 0, sizeof(ddsd));
597     ddsd.dwSize = sizeof(ddsd);
598     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
599     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
600     ddsd.dwWidth = 16;
601     ddsd.dwHeight = 16;
602     hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &pTexture, NULL);
603     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
604     if(!pTexture) return;
605
606     for(i = 0; i < 8; i++) {
607         hr = IDirect3DDevice7_SetTexture(lpD3DDevice, i, pTexture);
608         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %08x\n", i, hr);
609         hr = IDirect3DDevice7_SetTexture(lpD3DDevice, i, NULL);
610         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %08x\n", i, hr);
611         hr = IDirect3DDevice7_SetTextureStageState(lpD3DDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
612         ok(hr == D3D_OK, "IDirect3DDevice8_SetTextureStageState for texture %d failed with %08x\n", i, hr);
613     }
614
615     IDirectDrawSurface7_Release(pTexture);
616 }
617
618 static HRESULT WINAPI enumDevicesCallback(GUID *Guid,LPSTR DeviceDescription,LPSTR DeviceName, D3DDEVICEDESC *hal, D3DDEVICEDESC *hel, VOID *ctx)
619 {
620     UINT ver = *((UINT *) ctx);
621     if(IsEqualGUID(&IID_IDirect3DRGBDevice, Guid))
622     {
623         ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
624            "RGB Device %d hal line caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
625         ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
626            "RGB Device %d hal tri caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
627         ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
628            "RGB Device %d hel line caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
629         ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
630            "RGB Device %d hel tri caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
631
632         ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
633            "RGB Device %d hal line caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
634         ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
635            "RGB Device %d hal tri caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
636         ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
637            "RGB Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
638         ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
639            "RGB Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
640     }
641     else if(IsEqualGUID(&IID_IDirect3DHALDevice, Guid))
642     {
643         /* pow2 is hardware dependent */
644
645         ok(hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
646            "HAL Device %d hal line caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
647         ok(hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
648            "HAL Device %d hal tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
649         ok((hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
650            "HAL Device %d hel line caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
651         ok((hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
652            "HAL Device %d hel tri caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
653     }
654     else if(IsEqualGUID(&IID_IDirect3DRefDevice, Guid))
655     {
656         ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
657            "REF Device %d hal line caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
658         ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
659            "REF Device %d hal tri caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
660         ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
661            "REF Device %d hel line caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
662         ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
663            "REF Device %d hel tri caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
664
665         ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
666            "REF Device %d hal line caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
667         ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
668            "REF Device %d hal tri caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
669         ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
670            "REF Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
671         ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
672            "REF Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
673     }
674     else if(IsEqualGUID(&IID_IDirect3DRampDevice, Guid))
675     {
676         ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
677            "Ramp Device %d hal line caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
678         ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
679            "Ramp Device %d hal tri caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
680         ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
681            "Ramp Device %d hel line caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
682         ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
683            "Ramp Device %d hel tri caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
684
685         ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
686            "Ramp Device %d hal line caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
687         ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
688            "Ramp Device %d hal tri caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
689         ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
690            "Ramp Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
691         ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
692            "Ramp Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
693     }
694     else if(IsEqualGUID(&IID_IDirect3DMMXDevice, Guid))
695     {
696         ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
697            "MMX Device %d hal line caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
698         ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) == 0,
699            "MMX Device %d hal tri caps has D3DPTEXTURECAPS_POW2 flag set\n", ver);
700         ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
701            "MMX Device %d hel line caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
702         ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2,
703            "MMX Device %d hel tri caps does not have D3DPTEXTURECAPS_POW2 flag set\n", ver);
704
705         ok((hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
706            "MMX Device %d hal line caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
707         ok((hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) == 0,
708            "MMX Device %d hal tri caps has D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
709         ok(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
710            "MMX Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
711         ok(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE,
712            "MMX Device %d hel tri caps does not have D3DPTEXTURECAPS_PERSPECTIVE set\n", ver);
713     }
714     else
715     {
716         ok(FALSE, "Unexpected device enumerated: \"%s\" \"%s\"\n", DeviceDescription, DeviceName);
717         if(hal->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) trace("hal line has pow2 set\n");
718         else trace("hal line does NOT have pow2 set\n");
719         if(hal->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) trace("hal tri has pow2 set\n");
720         else trace("hal tri does NOT have pow2 set\n");
721         if(hel->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) trace("hel line has pow2 set\n");
722         else trace("hel line does NOT have pow2 set\n");
723         if(hel->dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2) trace("hel tri has pow2 set\n");
724         else trace("hel tri does NOT have pow2 set\n");
725     }
726     return DDENUMRET_OK;
727 }
728
729 static void CapsTest(void)
730 {
731     IDirect3D3 *d3d3;
732     IDirect3D3 *d3d2;
733     IDirectDraw *dd1;
734     HRESULT hr;
735     UINT ver;
736
737     hr = DirectDrawCreate(NULL, &dd1, NULL);
738     ok(hr == DD_OK, "Cannot create a DirectDraw 1 interface, hr = %08x\n", hr);
739     hr = IDirectDraw_QueryInterface(dd1, &IID_IDirect3D3, (void **) &d3d3);
740     ok(hr == D3D_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
741     ver = 3;
742     IDirect3D3_EnumDevices(d3d3, enumDevicesCallback, &ver);
743
744     IDirect3D3_Release(d3d3);
745     IDirectDraw_Release(dd1);
746
747     hr = DirectDrawCreate(NULL, &dd1, NULL);
748     ok(hr == DD_OK, "Cannot create a DirectDraw 1 interface, hr = %08x\n", hr);
749     hr = IDirectDraw_QueryInterface(dd1, &IID_IDirect3D2, (void **) &d3d2);
750     ok(hr == D3D_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
751     ver = 2;
752     IDirect3D2_EnumDevices(d3d2, enumDevicesCallback, &ver);
753
754     IDirect3D2_Release(d3d2);
755     IDirectDraw_Release(dd1);
756 }
757
758 START_TEST(d3d)
759 {
760     init_function_pointers();
761     if(!pDirectDrawCreateEx) {
762         trace("function DirectDrawCreateEx not available, skipping tests\n");
763         return;
764     }
765
766     if(!CreateDirect3D()) {
767         trace("Skipping tests\n");
768         return;
769     }
770     LightTest();
771     ProcessVerticesTest();
772     StateTest();
773     SceneTest();
774     LimitTest();
775     CapsTest();
776     ReleaseDirect3D();
777 }