wined3d: Lighting is calculated for vertices without normals.
[wine] / dlls / ddraw / tests / visual.c
1 /*
2  * Copyright (C) 2007 Stefan Dösinger(for CodeWeavers)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 /* See comment in dlls/d3d9/tests/visual.c for general guidelines */
20
21 #include <assert.h>
22 #include "wine/test.h"
23 #include "ddraw.h"
24 #include "d3d.h"
25
26 HWND window;
27 IDirectDraw7        *DirectDraw = NULL;
28 IDirectDrawSurface7 *Surface;
29 IDirect3D7          *Direct3D = NULL;
30 IDirect3DDevice7    *Direct3DDevice = NULL;
31
32 static HRESULT (WINAPI *pDirectDrawCreateEx)(LPGUID,LPVOID*,REFIID,LPUNKNOWN);
33
34 static BOOL createObjects()
35 {
36     HRESULT hr;
37     HMODULE hmod = GetModuleHandleA("ddraw.dll");
38     WNDCLASS wc = {0};
39     DDSURFACEDESC2 ddsd;
40
41
42     if(!hmod) return FALSE;
43     pDirectDrawCreateEx = (void*)GetProcAddress(hmod, "DirectDrawCreateEx");
44     if(!pDirectDrawCreateEx) return FALSE;
45
46     hr = pDirectDrawCreateEx(NULL, (void **) &DirectDraw, &IID_IDirectDraw7, NULL);
47     ok(hr==DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", hr);
48     if(!DirectDraw) goto err;
49
50     wc.lpfnWndProc = &DefWindowProc;
51     wc.lpszClassName = "d3d7_test_wc";
52     RegisterClass(&wc);
53     window = CreateWindow("d3d7_test_wc", "d3d7_test", WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
54
55     hr = IDirectDraw7_SetCooperativeLevel(DirectDraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
56     ok(hr == DD_OK, "IDirectDraw7_SetCooperativeLevel failed with %08x\n", hr);
57     if(FAILED(hr)) goto err;
58     hr = IDirectDraw7_SetDisplayMode(DirectDraw, 640, 480, 32, 0, 0);
59     if(FAILED(hr)) {
60         /* 24 bit is fine too */
61         hr = IDirectDraw7_SetDisplayMode(DirectDraw, 640, 480, 24, 0, 0);
62
63     }
64     ok(hr == DD_OK, "IDirectDraw7_SetDisplayMode failed with %08x\n", hr);
65     if(FAILED(hr)) goto err;
66
67     hr = IDirectDraw7_QueryInterface(DirectDraw, &IID_IDirect3D7, (void**) &Direct3D);
68     if (hr == E_NOINTERFACE) goto err;
69     ok(hr==DD_OK, "QueryInterface returned: %08x\n", hr);
70
71     /* DirectDraw Flipping behavior doesn't seem that well-defined. The reference rasterizer behaves differently
72      * than hardware implementations. Request single buffering, that seems to work everywhere
73      */
74     memset(&ddsd, 0, sizeof(ddsd));
75     ddsd.dwSize = sizeof(ddsd);
76     ddsd.dwFlags = DDSD_CAPS;
77     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE;
78     ddsd.dwBackBufferCount = 1;
79     hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &Surface, NULL);
80     ok(hr==DD_OK, "CreateSurface returned: %08x\n", hr);
81     if(!Surface) goto err;
82
83     hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DTnLHalDevice, Surface, &Direct3DDevice);
84     if(FAILED(hr))
85     {
86         trace("Creating a TnLHal Device failed, trying HAL\n");
87         hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DHALDevice, Surface, &Direct3DDevice);
88         if(FAILED(hr))
89         {
90             trace("Creating a HAL device failed, trying Ref\n");
91             hr = IDirect3D7_CreateDevice(Direct3D, &IID_IDirect3DRefDevice, Surface, &Direct3DDevice);
92         }
93     }
94     ok(hr == D3D_OK, "IDirect3D7_CreateDevice failed with %08x\n", hr);
95     if(!Direct3DDevice) goto err;
96     return TRUE;
97
98     err:
99     if(DirectDraw) IDirectDraw7_Release(DirectDraw);
100     if(Surface) IDirectDrawSurface7_Release(Surface);
101     if(Direct3D) IDirect3D7_Release(Direct3D);
102     if(Direct3DDevice) IDirect3DDevice7_Release(Direct3DDevice);
103     if(window) DestroyWindow(window);
104     return FALSE;
105 }
106
107 static void releaseObjects()
108 {
109     IDirect3DDevice7_Release(Direct3DDevice);
110     IDirect3D7_Release(Direct3D);
111     IDirectDrawSurface7_Release(Surface);
112     IDirectDraw7_Release(DirectDraw);
113     DestroyWindow(window);
114 }
115
116 static DWORD getPixelColor(IDirect3DDevice7 *device, UINT x, UINT y)
117 {
118     DWORD ret;
119     HRESULT hr;
120     DDSURFACEDESC2 ddsd;
121     RECT rectToLock = {x, y, x+1, y+1};
122     IDirectDrawSurface7 *surf = NULL;
123
124     /* Some implementations seem to dislike direct locking on the front buffer. Thus copy the front buffer
125      * to an offscreen surface and lock it instead of the front buffer
126      */
127     memset(&ddsd, 0, sizeof(ddsd));
128     ddsd.dwSize = sizeof(ddsd);
129     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
130     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
131     ddsd.dwWidth = 640;
132     ddsd.dwHeight = 480;
133     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
134     hr = IDirectDraw7_CreateSurface(DirectDraw, &ddsd, &surf, NULL);
135     ok(hr == DD_OK, "IDirectDraw7_CreateSurface failed with %08x\n", hr);
136     if(!surf)
137     {
138         trace("cannot create helper surface\n");
139         return 0xdeadbeef;
140     }
141
142     memset(&ddsd, 0, sizeof(ddsd));
143     ddsd.dwSize = sizeof(ddsd);
144     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
145
146     hr = IDirectDrawSurface_BltFast(surf, 0, 0, Surface, NULL, 0);
147     ok(hr == DD_OK, "IDirectDrawSurface7_BltFast returned %08x\n", hr);
148     if(FAILED(hr))
149     {
150         trace("Cannot blit\n");
151         ret = 0xdeadbee;
152         goto out;
153     }
154
155     hr = IDirectDrawSurface7_Lock(surf, &rectToLock, &ddsd, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
156     if(FAILED(hr))
157     {
158         trace("Can't lock the offscreen surface, hr=%08x\n", hr);
159         ret = 0xdeadbeec;
160         goto out;
161     }
162
163     /* Remove the X channel for now. DirectX and OpenGL have different ideas how to treat it apparently, and it isn't
164      * really important for these tests
165      */
166     ret = ((DWORD *) ddsd.lpSurface)[0] & 0x00ffffff;
167     hr = IDirectDrawSurface7_Unlock(surf, &rectToLock);
168     if(FAILED(hr))
169     {
170         trace("Can't unlock the offscreen surface, hr=%08x\n", hr);
171     }
172
173 out:
174     IDirectDrawSurface7_Release(surf);
175     return ret;
176 }
177
178 struct vertex
179 {
180     float x, y, z;
181     DWORD diffuse;
182 };
183
184 struct nvertex
185 {
186     float x, y, z;
187     float nx, ny, nz;
188     DWORD diffuse;
189 };
190
191 static void lighting_test(IDirect3DDevice7 *device)
192 {
193     HRESULT hr;
194     DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
195     DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
196     DWORD color;
197
198     float mat[16] = { 1.0, 0.0, 0.0, 0.0,
199                       0.0, 1.0, 0.0, 0.0,
200                       0.0, 0.0, 1.0, 0.0,
201                       0.0, 0.0, 0.0, 1.0 };
202
203     struct vertex unlitquad[] =
204     {
205         {-1.0,  -1.0,    0.1,                           0xffff0000},
206         {-1.0,   0.0,    0.1,                           0xffff0000},
207         { 0.0,   0.0,    0.1,                           0xffff0000},
208         { 0.0,  -1.0,    0.1,                           0xffff0000},
209     };
210     struct vertex litquad[] =
211     {
212         {-1.0,   0.0,    0.1,                           0xff00ff00},
213         {-1.0,   1.0,    0.1,                           0xff00ff00},
214         { 0.0,   1.0,    0.1,                           0xff00ff00},
215         { 0.0,   0.0,    0.1,                           0xff00ff00},
216     };
217     struct nvertex unlitnquad[] =
218     {
219         { 0.0,  -1.0,    0.1,   1.0,    1.0,    1.0,    0xff0000ff},
220         { 0.0,   0.0,    0.1,   1.0,    1.0,    1.0,    0xff0000ff},
221         { 1.0,   0.0,    0.1,   1.0,    1.0,    1.0,    0xff0000ff},
222         { 1.0,  -1.0,    0.1,   1.0,    1.0,    1.0,    0xff0000ff},
223     };
224     struct nvertex litnquad[] =
225     {
226         { 0.0,   0.0,    0.1,   1.0,    1.0,    1.0,    0xffffff00},
227         { 0.0,   1.0,    0.1,   1.0,    1.0,    1.0,    0xffffff00},
228         { 1.0,   1.0,    0.1,   1.0,    1.0,    1.0,    0xffffff00},
229         { 1.0,   0.0,    0.1,   1.0,    1.0,    1.0,    0xffffff00},
230     };
231     WORD Indices[] = {0, 1, 2, 2, 3, 0};
232
233     hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
234     ok(hr == D3D_OK, "IDirect3DDevice7_Clear failed with %08x\n", hr);
235
236     /* Setup some states that may cause issues */
237     hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_WORLD, (D3DMATRIX *) mat);
238     ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
239     hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_VIEW, (D3DMATRIX *)mat);
240     ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
241     hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, (D3DMATRIX *) mat);
242     ok(hr == D3D_OK, "IDirect3DDevice7_SetTransform returned %08x\n", hr);
243     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
244     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
245     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
246     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
247     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
248     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
249     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_STENCILENABLE, FALSE);
250     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
251     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHATESTENABLE, FALSE);
252     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
253     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
254     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
255     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
256     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState failed with %08x\n", hr);
257
258     hr = IDirect3DDevice7_BeginScene(device);
259     ok(hr == D3D_OK, "IDirect3DDevice7_BeginScene failed with %08x\n", hr);
260     if(hr == D3D_OK)
261     {
262         /* No lights are defined... That means, lit vertices should be entirely black */
263         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
264         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
265         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, unlitquad, 4 /* NumVerts */,
266                                                     Indices, 6 /* Indexcount */, 0 /* flags */);
267         ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
268
269         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
270         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
271         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, litquad, 4 /* NumVerts */,
272                                                     Indices, 6 /* Indexcount */, 0 /* flags */);
273         ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
274
275         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
276         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
277         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, unlitnquad, 4 /* NumVerts */,
278                                                     Indices, 6 /* Indexcount */, 0 /* flags */);
279         ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
280
281         hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
282         ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
283         hr = IDirect3DDevice7_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, litnquad, 4 /* NumVerts */,
284                                                     Indices, 6 /* Indexcount */, 0 /* flags */);
285         ok(hr == D3D_OK, "IDirect3DDevice7_DrawIndexedPrimitiveUP failed with %08x\n", hr);
286
287         IDirect3DDevice7_EndScene(device);
288         ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed with %08x\n", hr);
289     }
290
291     color = getPixelColor(device, 160, 360); /* lower left quad - unlit without normals */
292     ok(color == 0x00ff0000, "Unlit quad without normals has color %08x\n", color);
293     color = getPixelColor(device, 160, 120); /* upper left quad - lit without normals */
294     ok(color == 0x00000000, "Lit quad without normals has color %08x\n", color);
295     color = getPixelColor(device, 480, 360); /* lower left quad - unlit width normals */
296     ok(color == 0x000000ff, "Unlit quad width normals has color %08x\n", color);
297     color = getPixelColor(device, 480, 120); /* upper left quad - lit width normals */
298     ok(color == 0x00000000, "Lit quad width normals has color %08x\n", color);
299
300     hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
301     ok(hr == D3D_OK, "IDirect3DDevice7_SetRenderState returned %08x\n", hr);
302 }
303
304 START_TEST(visual)
305 {
306     HRESULT hr;
307     DWORD color;
308     if(!createObjects())
309     {
310         skip("Cannot initialize DirectDraw and Direct3D, skipping\n");
311         return;
312     }
313
314     /* Check for the reliability of the returned data */
315     hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
316     if(FAILED(hr))
317     {
318         trace("Clear failed, can't assure correctness of the test results, skipping\n");
319         goto cleanup;
320     }
321
322     color = getPixelColor(Direct3DDevice, 1, 1);
323     if(color !=0x00ff0000)
324     {
325         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
326         goto cleanup;
327     }
328
329     hr = IDirect3DDevice7_Clear(Direct3DDevice, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
330     if(FAILED(hr))
331     {
332         trace("Clear failed, can't assure correctness of the test results, skipping\n");
333         goto cleanup;
334     }
335
336     color = getPixelColor(Direct3DDevice, 639, 479);
337     if(color != 0x0000ddee)
338     {
339         trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
340         goto cleanup;
341     }
342
343     /* Now run the tests */
344     lighting_test(Direct3DDevice);
345
346 cleanup:
347     releaseObjects();
348 }