quartz: Use proper alloc/free functions for COM objects.
[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
57     if(hmod)
58     {
59         pDirectDrawCreateEx = (void*)GetProcAddress(hmod, "DirectDrawCreateEx");
60     }
61 }
62
63
64 static BOOL CreateDirect3D(void)
65 {
66     HRESULT rc;
67     DDSURFACEDESC2 ddsd;
68
69     rc = pDirectDrawCreateEx(NULL, (void**)&lpDD,
70         &IID_IDirectDraw7, NULL);
71     ok(rc==DD_OK || rc==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", rc);
72     if (!lpDD) {
73         trace("DirectDrawCreateEx() failed with an error %x\n", rc);
74         return FALSE;
75     }
76
77     rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
78     ok(rc==DD_OK, "SetCooperativeLevel returned: %x\n", rc);
79
80     rc = IDirectDraw7_QueryInterface(lpDD, &IID_IDirect3D7, (void**) &lpD3D);
81     if (rc == E_NOINTERFACE) return FALSE;
82     ok(rc==DD_OK, "QueryInterface returned: %x\n", rc);
83
84     memset(&ddsd, 0, sizeof(ddsd));
85     ddsd.dwSize = sizeof(ddsd);
86     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
87     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
88     ddsd.dwWidth = 256;
89     ddsd.dwHeight = 256;
90     rc = IDirectDraw7_CreateSurface(lpDD, &ddsd, &lpDDS, NULL);
91     ok(rc==DD_OK, "CreateSurface returned: %x\n", rc);
92
93     rc = IDirect3D7_CreateDevice(lpD3D, &IID_IDirect3DTnLHalDevice, lpDDS,
94         &lpD3DDevice);
95     ok(rc==D3D_OK || rc==DDERR_NOPALETTEATTACHED || rc==E_OUTOFMEMORY, "CreateDevice returned: %x\n", rc);
96     if (!lpD3DDevice) {
97         trace("IDirect3D7::CreateDevice() failed with an error %x\n", rc);
98         return FALSE;
99     }
100
101     return TRUE;
102 }
103
104 static void ReleaseDirect3D(void)
105 {
106     if (lpD3DDevice != NULL)
107     {
108         IDirect3DDevice7_Release(lpD3DDevice);
109         lpD3DDevice = NULL;
110     }
111
112     if (lpDDS != NULL)
113     {
114         IDirectDrawSurface_Release(lpDDS);
115         lpDDS = NULL;
116     }
117
118     if (lpD3D != NULL)
119     {
120         IDirect3D7_Release(lpD3D);
121         lpD3D = NULL;
122     }
123
124     if (lpDD != NULL)
125     {
126         IDirectDraw_Release(lpDD);
127         lpDD = NULL;
128     }
129 }
130
131 static void LightTest(void)
132 {
133     HRESULT rc;
134     D3DLIGHT7 light;
135     D3DLIGHT7 defaultlight;
136     BOOL bEnabled = FALSE;
137
138     /* Set a few lights with funky indices. */
139     memset(&light, 0, sizeof(light));
140     light.dltType = D3DLIGHT_DIRECTIONAL;
141     U1(light.dcvDiffuse).r = 0.5f;
142     U2(light.dcvDiffuse).g = 0.6f;
143     U3(light.dcvDiffuse).b = 0.7f;
144     U2(light.dvDirection).y = 1.f;
145
146     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 5, &light);
147     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
148     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 10, &light);
149     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
150     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 45, &light);
151     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
152
153
154     /* Try to retrieve a light beyond the indices of the lights that have
155        been set. */
156     rc = IDirect3DDevice7_GetLight(lpD3DDevice, 50, &light);
157     ok(rc==DDERR_INVALIDPARAMS, "GetLight returned: %x\n", rc);
158     rc = IDirect3DDevice7_GetLight(lpD3DDevice, 2, &light);
159     ok(rc==DDERR_INVALIDPARAMS, "GetLight returned: %x\n", rc);
160
161
162     /* Try to retrieve one of the lights that have been set */
163     rc = IDirect3DDevice7_GetLight(lpD3DDevice, 10, &light);
164     ok(rc==D3D_OK, "GetLight returned: %x\n", rc);
165
166
167     /* Enable a light that have been previously set. */
168     rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 10, TRUE);
169     ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
170
171
172     /* Enable some lights that have not been previously set, and verify that
173        they have been initialized with proper default values. */
174     memset(&defaultlight, 0, sizeof(D3DLIGHT7));
175     defaultlight.dltType = D3DLIGHT_DIRECTIONAL;
176     U1(defaultlight.dcvDiffuse).r = 1.f;
177     U2(defaultlight.dcvDiffuse).g = 1.f;
178     U3(defaultlight.dcvDiffuse).b = 1.f;
179     U3(defaultlight.dvDirection).z = 1.f;
180
181     rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 20, TRUE);
182     ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
183     memset(&light, 0, sizeof(D3DLIGHT7));
184     rc = IDirect3DDevice7_GetLight(lpD3DDevice, 20, &light);
185     ok(rc==D3D_OK, "GetLight returned: %x\n", rc);
186     ok(!memcmp(&light, &defaultlight, sizeof(D3DLIGHT7)),
187         "light data doesn't match expected default values\n" );
188
189     rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 50, TRUE);
190     ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
191     memset(&light, 0, sizeof(D3DLIGHT7));
192     rc = IDirect3DDevice7_GetLight(lpD3DDevice, 50, &light);
193     ok(rc==D3D_OK, "GetLight returned: %x\n", rc);
194     ok(!memcmp(&light, &defaultlight, sizeof(D3DLIGHT7)),
195         "light data doesn't match expected default values\n" );
196
197
198     /* Disable one of the light that have been previously enabled. */
199     rc = IDirect3DDevice7_LightEnable(lpD3DDevice, 20, FALSE);
200     ok(rc==D3D_OK, "LightEnable returned: %x\n", rc);
201
202     /* Try to retrieve the enable status of some lights */
203     /* Light 20 is supposed to be disabled */
204     rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 20, &bEnabled );
205     ok(rc==D3D_OK, "GetLightEnable returned: %x\n", rc);
206     ok(!bEnabled, "GetLightEnable says the light is enabled\n");
207
208     /* Light 10 is supposed to be enabled */
209     bEnabled = FALSE;
210     rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 10, &bEnabled );
211     ok(rc==D3D_OK, "GetLightEnable returned: %x\n", rc);
212     ok(bEnabled, "GetLightEnable says the light is disabled\n");
213
214     /* Light 80 has not been set */
215     rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 80, &bEnabled );
216     ok(rc==DDERR_INVALIDPARAMS, "GetLightEnable returned: %x\n", rc);
217
218     /* Light 23 has not been set */
219     rc = IDirect3DDevice7_GetLightEnable(lpD3DDevice, 23, &bEnabled );
220     ok(rc==DDERR_INVALIDPARAMS, "GetLightEnable returned: %x\n", rc);
221
222     /* Set some lights with invalid parameters */
223     memset(&light, 0, sizeof(D3DLIGHT7));
224     light.dltType = 0;
225     U1(light.dcvDiffuse).r = 1.f;
226     U2(light.dcvDiffuse).g = 1.f;
227     U3(light.dcvDiffuse).b = 1.f;
228     U3(light.dvDirection).z = 1.f;
229     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 100, &light);
230     ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);
231
232     memset(&light, 0, sizeof(D3DLIGHT7));
233     light.dltType = 12345;
234     U1(light.dcvDiffuse).r = 1.f;
235     U2(light.dcvDiffuse).g = 1.f;
236     U3(light.dcvDiffuse).b = 1.f;
237     U3(light.dvDirection).z = 1.f;
238     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 101, &light);
239     ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);
240
241     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 102, NULL);
242     ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);
243
244     memset(&light, 0, sizeof(D3DLIGHT7));
245     light.dltType = D3DLIGHT_SPOT;
246     U1(light.dcvDiffuse).r = 1.f;
247     U2(light.dcvDiffuse).g = 1.f;
248     U3(light.dcvDiffuse).b = 1.f;
249     U3(light.dvDirection).z = 1.f;
250
251     light.dvAttenuation0 = -1.0 / 0.0; /* -INFINITY */
252     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
253     ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);
254
255     light.dvAttenuation0 = -1.0;
256     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
257     ok(rc==DDERR_INVALIDPARAMS, "SetLight returned: %x\n", rc);
258
259     light.dvAttenuation0 = 0.0;
260     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
261     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
262
263     light.dvAttenuation0 = 1.0;
264     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
265     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
266
267     light.dvAttenuation0 = 1.0 / 0.0; /* +INFINITY */
268     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
269     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
270
271     light.dvAttenuation0 = 0.0 / 0.0; /* NaN */
272     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
273     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
274
275     /* Directional light ignores attenuation */
276     light.dltType = D3DLIGHT_DIRECTIONAL;
277     light.dvAttenuation0 = -1.0;
278     rc = IDirect3DDevice7_SetLight(lpD3DDevice, 103, &light);
279     ok(rc==D3D_OK, "SetLight returned: %x\n", rc);
280 }
281
282 static void ProcessVerticesTest(void)
283 {
284     D3DVERTEXBUFFERDESC desc;
285     HRESULT rc;
286     VERTEX *in;
287     TVERTEX *out;
288     VERTEX *out2;
289     D3DVIEWPORT7 vp;
290     D3DMATRIX view = {  2.0, 0.0, 0.0, 0.0,
291                         0.0, -1.0, 0.0, 0.0,
292                         0.0, 0.0, 1.0, 0.0,
293                         0.0, 0.0, 0.0, 3.0 };
294
295     D3DMATRIX world = { 0.0, 1.0, 0.0, 0.0,
296                         1.0, 0.0, 0.0, 0.0,
297                         0.0, 0.0, 0.0, 1.0,
298                         0.0, 1.0, 1.0, 1.0 };
299
300     D3DMATRIX proj = {  1.0, 0.0, 0.0, 1.0,
301                         0.0, 1.0, 1.0, 0.0,
302                         0.0, 1.0, 1.0, 0.0,
303                         1.0, 0.0, 0.0, 1.0 };
304     /* Create some vertex buffers */
305
306     memset(&desc, 0, sizeof(desc));
307     desc.dwSize = sizeof(desc);
308     desc.dwCaps = 0;
309     desc.dwFVF = D3DFVF_XYZ;
310     desc.dwNumVertices = 16;
311     rc = IDirect3D7_CreateVertexBuffer(lpD3D, &desc, &lpVBufSrc, 0);
312     ok(rc==D3D_OK || rc==E_OUTOFMEMORY, "CreateVertexBuffer returned: %x\n", rc);
313     if (!lpVBufSrc)
314     {
315         trace("IDirect3D7::CreateVertexBuffer() failed with an error %x\n", rc);
316         goto out;
317     }
318
319     memset(&desc, 0, sizeof(desc));
320     desc.dwSize = sizeof(desc);
321     desc.dwCaps = 0;
322     desc.dwFVF = D3DFVF_XYZRHW;
323     desc.dwNumVertices = 16;
324     /* Msdn says that the last parameter must be 0 - check that */
325     rc = IDirect3D7_CreateVertexBuffer(lpD3D, &desc, &lpVBufDest1, 4);
326     ok(rc==D3D_OK || rc==E_OUTOFMEMORY, "CreateVertexBuffer returned: %x\n", rc);
327     if (!lpVBufDest1)
328     {
329         trace("IDirect3D7::CreateVertexBuffer() failed with an error %x\n", rc);
330         goto out;
331     }
332
333     memset(&desc, 0, sizeof(desc));
334     desc.dwSize = sizeof(desc);
335     desc.dwCaps = 0;
336     desc.dwFVF = D3DFVF_XYZ;
337     desc.dwNumVertices = 16;
338     /* Msdn says that the last parameter must be 0 - check that */
339     rc = IDirect3D7_CreateVertexBuffer(lpD3D, &desc, &lpVBufDest2, 12345678);
340     ok(rc==D3D_OK || rc==E_OUTOFMEMORY, "CreateVertexBuffer returned: %x\n", rc);
341     if (!lpVBufDest2)
342     {
343         trace("IDirect3D7::CreateVertexBuffer() failed with an error %x\n", rc);
344         goto out;
345     }
346
347     rc = IDirect3DVertexBuffer7_Lock(lpVBufSrc, 0, (void **) &in, NULL);
348     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Lock returned: %x\n", rc);
349     if(!in) goto out;
350
351     /* Check basic transformation */
352
353     in[0].x = 0.0;
354     in[0].y = 0.0;
355     in[0].z = 0.0;
356
357     in[1].x = 1.0;
358     in[1].y = 1.0;
359     in[1].z = 1.0;
360
361     in[2].x = -1.0;
362     in[2].y = -1.0;
363     in[2].z = 0.5;
364
365     in[3].x = 0.5;
366     in[3].y = -0.5;
367     in[3].z = 0.25;
368     rc = IDirect3DVertexBuffer7_Unlock(lpVBufSrc);
369     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Unlock returned: %x\n", rc);
370
371     rc = IDirect3DVertexBuffer7_ProcessVertices(lpVBufDest1, D3DVOP_TRANSFORM, 0, 4, lpVBufSrc, 0, lpD3DDevice, 0);
372     ok(rc==D3D_OK , "IDirect3DVertexBuffer::ProcessVertices returned: %x\n", rc);
373
374     rc = IDirect3DVertexBuffer7_ProcessVertices(lpVBufDest2, D3DVOP_TRANSFORM, 0, 4, lpVBufSrc, 0, lpD3DDevice, 0);
375     ok(rc==D3D_OK , "IDirect3DVertexBuffer::ProcessVertices returned: %x\n", rc);
376
377     rc = IDirect3DVertexBuffer7_Lock(lpVBufDest1, 0, (void **) &out, NULL);
378     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Lock returned: %x\n", rc);
379     if(!out) goto out;
380
381     /* Check the results */
382     ok( comparefloat(out[0].x, 128.0 ) &&
383         comparefloat(out[0].y, 128.0 ) &&
384         comparefloat(out[0].z, 0.0 ) &&
385         comparefloat(out[0].rhw, 1.0 ),
386         "Output 0 vertex is (%f , %f , %f , %f)\n", out[0].x, out[0].y, out[0].z, out[0].rhw);
387
388     ok( comparefloat(out[1].x, 256.0 ) &&
389         comparefloat(out[1].y, 0.0 ) &&
390         comparefloat(out[1].z, 1.0 ) &&
391         comparefloat(out[1].rhw, 1.0 ),
392         "Output 1 vertex is (%f , %f , %f , %f)\n", out[1].x, out[1].y, out[1].z, out[1].rhw);
393
394     ok( comparefloat(out[2].x, 0.0 ) &&
395         comparefloat(out[2].y, 256.0 ) &&
396         comparefloat(out[2].z, 0.5 ) &&
397         comparefloat(out[2].rhw, 1.0 ),
398         "Output 2 vertex is (%f , %f , %f , %f)\n", out[2].x, out[2].y, out[2].z, out[2].rhw);
399
400     ok( comparefloat(out[3].x, 192.0 ) &&
401         comparefloat(out[3].y, 192.0 ) &&
402         comparefloat(out[3].z, 0.25 ) &&
403         comparefloat(out[3].rhw, 1.0 ),
404         "Output 3 vertex is (%f , %f , %f , %f)\n", out[3].x, out[3].y, out[3].z, out[3].rhw);
405
406     rc = IDirect3DVertexBuffer7_Unlock(lpVBufDest1);
407     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Unlock returned: %x\n", rc);
408     out = NULL;
409
410     rc = IDirect3DVertexBuffer7_Lock(lpVBufDest2, 0, (void **) &out2, NULL);
411     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Lock returned: %x\n", rc);
412     if(!out2) goto out;
413     /* Small thing without much practial meaning, but I stumbled upon it,
414      * so let's check for it: If the output vertex buffer has to RHW value,
415      * The RHW value of the last vertex is written into the next vertex
416      */
417     ok( comparefloat(out2[4].x, 1.0 ) &&
418         comparefloat(out2[4].y, 0.0 ) &&
419         comparefloat(out2[4].z, 0.0 ),
420         "Output 4 vertex is (%f , %f , %f)\n", out2[4].x, out2[4].y, out2[4].z);
421
422     rc = IDirect3DVertexBuffer7_Unlock(lpVBufDest2);
423     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Unlock returned: %x\n", rc);
424     out = NULL;
425
426     /* Try a more complicated viewport, same vertices */
427     memset(&vp, 0, sizeof(vp));
428     vp.dwX = 10;
429     vp.dwY = 5;
430     vp.dwWidth = 246;
431     vp.dwHeight = 130;
432     vp.dvMinZ = -2.0;
433     vp.dvMaxZ = 4.0;
434     rc = IDirect3DDevice7_SetViewport(lpD3DDevice, &vp);
435     ok(rc==D3D_OK, "IDirect3DDevice7_SetViewport failed with rc=%x\n", rc);
436
437     /* Process again */
438     rc = IDirect3DVertexBuffer7_ProcessVertices(lpVBufDest1, D3DVOP_TRANSFORM, 0, 4, lpVBufSrc, 0, lpD3DDevice, 0);
439     ok(rc==D3D_OK , "IDirect3DVertexBuffer::ProcessVertices returned: %x\n", rc);
440
441     rc = IDirect3DVertexBuffer7_Lock(lpVBufDest1, 0, (void **) &out, NULL);
442     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Lock returned: %x\n", rc);
443     if(!out) goto out;
444
445     /* Check the results */
446     ok( comparefloat(out[0].x, 133.0 ) &&
447         comparefloat(out[0].y, 70.0 ) &&
448         comparefloat(out[0].z, -2.0 ) &&
449         comparefloat(out[0].rhw, 1.0 ),
450         "Output 0 vertex is (%f , %f , %f , %f)\n", out[0].x, out[0].y, out[0].z, out[0].rhw);
451
452     ok( comparefloat(out[1].x, 256.0 ) &&
453         comparefloat(out[1].y, 5.0 ) &&
454         comparefloat(out[1].z, 4.0 ) &&
455         comparefloat(out[1].rhw, 1.0 ),
456         "Output 1 vertex is (%f , %f , %f , %f)\n", out[1].x, out[1].y, out[1].z, out[1].rhw);
457
458     ok( comparefloat(out[2].x, 10.0 ) &&
459         comparefloat(out[2].y, 135.0 ) &&
460         comparefloat(out[2].z, 1.0 ) &&
461         comparefloat(out[2].rhw, 1.0 ),
462         "Output 2 vertex is (%f , %f , %f , %f)\n", out[1].x, out[1].y, out[1].z, out[1].rhw);
463
464     ok( comparefloat(out[3].x, 194.5 ) &&
465         comparefloat(out[3].y, 102.5 ) &&
466         comparefloat(out[3].z, -0.5 ) &&
467         comparefloat(out[3].rhw, 1.0 ),
468         "Output 3 vertex is (%f , %f , %f , %f)\n", out[3].x, out[3].y, out[3].z, out[3].rhw);
469
470     rc = IDirect3DVertexBuffer7_Unlock(lpVBufDest1);
471     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Unlock returned: %x\n", rc);
472     out = NULL;
473
474     /* Play with some matrices. */
475
476     rc = IDirect3DDevice7_SetTransform(lpD3DDevice, D3DTRANSFORMSTATE_VIEW, &view);
477     ok(rc==D3D_OK, "IDirect3DDevice7_SetTransform failed\n");
478
479     rc = IDirect3DDevice7_SetTransform(lpD3DDevice, D3DTRANSFORMSTATE_PROJECTION, &proj);
480     ok(rc==D3D_OK, "IDirect3DDevice7_SetTransform failed\n");
481
482     rc = IDirect3DDevice7_SetTransform(lpD3DDevice, D3DTRANSFORMSTATE_WORLD, &world);
483     ok(rc==D3D_OK, "IDirect3DDevice7_SetTransform failed\n");
484
485     rc = IDirect3DVertexBuffer7_ProcessVertices(lpVBufDest1, D3DVOP_TRANSFORM, 0, 4, lpVBufSrc, 0, lpD3DDevice, 0);
486     ok(rc==D3D_OK , "IDirect3DVertexBuffer::ProcessVertices returned: %x\n", rc);
487
488     rc = IDirect3DVertexBuffer7_Lock(lpVBufDest1, 0, (void **) &out, NULL);
489     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Lock returned: %x\n", rc);
490     if(!out) goto out;
491
492     /* Keep the viewport simpler, otherwise we get bad numbers to compare */
493     vp.dwX = 0;
494     vp.dwY = 0;
495     vp.dwWidth = 100;
496     vp.dwHeight = 100;
497     vp.dvMinZ = 1.0;
498     vp.dvMaxZ = 0.0;
499     rc = IDirect3DDevice7_SetViewport(lpD3DDevice, &vp);
500     ok(rc==D3D_OK, "IDirect3DDevice7_SetViewport failed\n");
501
502     /* Check the results */
503     ok( comparefloat(out[0].x, 256.0 ) &&    /* X coordinate is cut at the surface edges */
504         comparefloat(out[0].y, 70.0 ) &&
505         comparefloat(out[0].z, -2.0 ) &&
506         comparefloat(out[0].rhw, (1.0 / 3.0)),
507         "Output 0 vertex is (%f , %f , %f , %f)\n", out[0].x, out[0].y, out[0].z, out[0].rhw);
508
509     ok( comparefloat(out[1].x, 256.0 ) &&
510         comparefloat(out[1].y, 78.125000 ) &&
511         comparefloat(out[1].z, -2.750000 ) &&
512         comparefloat(out[1].rhw, 0.125000 ),
513         "Output 1 vertex is (%f , %f , %f , %f)\n", out[1].x, out[1].y, out[1].z, out[1].rhw);
514
515     ok( comparefloat(out[2].x, 256.0 ) &&
516         comparefloat(out[2].y, 44.000000 ) &&
517         comparefloat(out[2].z, 0.400000 ) &&
518         comparefloat(out[2].rhw, 0.400000 ),
519         "Output 2 vertex is (%f , %f , %f , %f)\n", out[2].x, out[2].y, out[2].z, out[2].rhw);
520
521     ok( comparefloat(out[3].x, 256.0 ) &&
522         comparefloat(out[3].y, 81.818184 ) &&
523         comparefloat(out[3].z, -3.090909 ) &&
524         comparefloat(out[3].rhw, 0.363636 ),
525         "Output 3 vertex is (%f , %f , %f , %f)\n", out[3].x, out[3].y, out[3].z, out[3].rhw);
526
527     rc = IDirect3DVertexBuffer7_Unlock(lpVBufDest1);
528     ok(rc==D3D_OK , "IDirect3DVertexBuffer::Unlock returned: %x\n", rc);
529     out = NULL;
530
531 out:
532     IDirect3DVertexBuffer7_Release(lpVBufSrc);
533     IDirect3DVertexBuffer7_Release(lpVBufDest1);
534     IDirect3DVertexBuffer7_Release(lpVBufDest2);
535 }
536
537 static void StateTest( void )
538 {
539     HRESULT rc;
540
541     /* The msdn says its undocumented, does it return an error too? */
542     rc = IDirect3DDevice7_SetRenderState(lpD3DDevice, D3DRENDERSTATE_ZVISIBLE, TRUE);
543     ok(rc == D3D_OK, "IDirect3DDevice7_SetRenderState(D3DRENDERSTATE_ZVISIBLE, TRUE) returned %08x\n", rc);
544     rc = IDirect3DDevice7_SetRenderState(lpD3DDevice, D3DRENDERSTATE_ZVISIBLE, FALSE);
545     ok(rc == D3D_OK, "IDirect3DDevice7_SetRenderState(D3DRENDERSTATE_ZVISIBLE, FALSE) returned %08x\n", rc);
546 }
547
548
549 static void SceneTest(void)
550 {
551     HRESULT                      hr;
552
553     /* Test an EndScene without beginscene. Should return an error */
554     hr = IDirect3DDevice7_EndScene(lpD3DDevice);
555     ok(hr == D3DERR_SCENE_NOT_IN_SCENE, "IDirect3DDevice7_EndScene returned %08x\n", hr);
556
557     /* Test a normal BeginScene / EndScene pair, this should work */
558     hr = IDirect3DDevice7_BeginScene(lpD3DDevice);
559     ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
560     if(SUCCEEDED(hr))
561     {
562         hr = IDirect3DDevice7_EndScene(lpD3DDevice);
563         ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
564     }
565
566     /* Test another EndScene without having begun a new scene. Should return an error */
567     hr = IDirect3DDevice7_EndScene(lpD3DDevice);
568     ok(hr == D3DERR_SCENE_NOT_IN_SCENE, "IDirect3DDevice7_EndScene returned %08x\n", hr);
569
570     /* Two nested BeginScene and EndScene calls */
571     hr = IDirect3DDevice7_BeginScene(lpD3DDevice);
572     ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
573     hr = IDirect3DDevice7_BeginScene(lpD3DDevice);
574     ok(hr == D3DERR_SCENE_IN_SCENE, "IDirect3DDevice7_BeginScene returned %08x\n", hr);
575     hr = IDirect3DDevice7_EndScene(lpD3DDevice);
576     ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
577     hr = IDirect3DDevice7_EndScene(lpD3DDevice);
578     ok(hr == D3DERR_SCENE_NOT_IN_SCENE, "IDirect3DDevice7_EndScene returned %08x\n", hr);
579
580     /* TODO: Verify that blitting works in the same way as in d3d9 */
581 }
582
583 static void LimitTest(void)
584 {
585     IDirectDrawSurface7 *pTexture = NULL;
586     HRESULT hr;
587     int i;
588     DDSURFACEDESC2 ddsd;
589
590     memset(&ddsd, 0, sizeof(ddsd));
591     ddsd.dwSize = sizeof(ddsd);
592     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
593     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
594     ddsd.dwWidth = 16;
595     ddsd.dwHeight = 16;
596     hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &pTexture, NULL);
597     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
598     if(!pTexture) return;
599
600     for(i = 0; i < 8; i++) {
601         hr = IDirect3DDevice7_SetTexture(lpD3DDevice, i, pTexture);
602         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %08x\n", i, hr);
603         hr = IDirect3DDevice7_SetTexture(lpD3DDevice, i, NULL);
604         ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture for sampler %d failed with %08x\n", i, hr);
605         hr = IDirect3DDevice7_SetTextureStageState(lpD3DDevice, i, D3DTSS_COLOROP, D3DTOP_ADD);
606         ok(hr == D3D_OK, "IDirect3DDevice8_SetTextureStageState for texture %d failed with %08x\n", i, hr);
607     }
608
609     IDirectDrawSurface7_Release(pTexture);
610 }
611
612 START_TEST(d3d)
613 {
614     init_function_pointers();
615     if(!pDirectDrawCreateEx) {
616         trace("function DirectDrawCreateEx not available, skipping tests\n");
617         return;
618     }
619
620     if(!CreateDirect3D()) {
621         trace("Skipping tests\n");
622         return;
623     }
624     LightTest();
625     ProcessVerticesTest();
626     StateTest();
627     SceneTest();
628     LimitTest();
629     ReleaseDirect3D();
630 }