2 * Copyright 2005, 2007 Henri Verbeet
3 * Copyright (C) 2007 Stefan Dösinger(for CodeWeavers)
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.
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.
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
20 /* This test framework allows limited testing of rendering results. Things are rendered, shown on
21 * the framebuffer, read back from there and compared to expected colors.
23 * However, neither d3d nor opengl is guaranteed to be pixel exact, and thus the capability of this test
24 * is rather limited. As a general guideline for adding tests, do not rely on corner pixels. Draw a big enough
25 * area which shows specific behavior(like a quad on the whole screen), and try to get resulting colos with
26 * all bits set or unset in all channels(like pure red, green, blue, white, black). Hopefully everything that
27 * causes visible results in games can be tested in a way that does not depend on pixel exactness
33 #include "wine/test.h"
35 static HMODULE d3d9_handle = 0;
37 static HWND create_window(void)
41 wc.lpfnWndProc = &DefWindowProc;
42 wc.lpszClassName = "d3d9_test_wc";
45 ret = CreateWindow("d3d9_test_wc", "d3d9_test",
46 WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
50 static DWORD getPixelColor(IDirect3DDevice9 *device, UINT x, UINT y)
53 IDirect3DSurface9 *surf;
55 D3DLOCKED_RECT lockedRect;
56 RECT rectToLock = {x, y, x+1, y+1};
58 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 640, 480, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surf, NULL);
59 if(FAILED(hr) || !surf ) /* This is not a test */
61 trace("Can't create an offscreen plain surface to read the render target data, hr=%s\n", DXGetErrorString9(hr));
65 hr = IDirect3DDevice9_GetFrontBufferData(device, 0, surf);
68 trace("Can't read the front buffer data, hr=%s\n", DXGetErrorString9(hr));
73 hr = IDirect3DSurface9_LockRect(surf, &lockedRect, &rectToLock, D3DLOCK_READONLY);
76 trace("Can't lock the offscreen surface, hr=%s\n", DXGetErrorString9(hr));
81 /* Remove the X channel for now. DirectX and OpenGL have different ideas how to treat it apparently, and it isn't
82 * really important for these tests
84 ret = ((DWORD *) lockedRect.pBits)[0] & 0x00ffffff;
85 hr = IDirect3DSurface9_UnlockRect(surf);
88 trace("Can't unlock the offscreen surface, hr=%s\n", DXGetErrorString9(hr));
92 if(surf) IDirect3DSurface9_Release(surf);
96 static IDirect3DDevice9 *init_d3d9(void)
98 IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
99 IDirect3D9 *d3d9_ptr = 0;
100 IDirect3DDevice9 *device_ptr = 0;
101 D3DPRESENT_PARAMETERS present_parameters;
104 d3d9_create = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
105 ok(d3d9_create != NULL, "Failed to get address of Direct3DCreate9\n");
106 if (!d3d9_create) return NULL;
108 d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
109 ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
110 if (!d3d9_ptr) return NULL;
112 ZeroMemory(&present_parameters, sizeof(present_parameters));
113 present_parameters.Windowed = FALSE;
114 present_parameters.hDeviceWindow = create_window();
115 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
116 present_parameters.BackBufferWidth = 640;
117 present_parameters.BackBufferHeight = 480;
118 present_parameters.BackBufferFormat = D3DFMT_X8R8G8B8;
120 hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
121 ok(hr == D3D_OK, "IDirect3D_CreateDevice returned: %s\n", DXGetErrorString9(hr));
139 static void lighting_test(IDirect3DDevice9 *device)
142 DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
143 DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
146 float mat[16] = { 1.0, 0.0, 0.0, 0.0,
149 0.0, 0.0, 0.0, 1.0 };
151 struct vertex unlitquad[] =
153 {-1.0, -1.0, 0.1, 0xffff0000},
154 {-1.0, 0.0, 0.1, 0xffff0000},
155 { 0.0, 0.0, 0.1, 0xffff0000},
156 { 0.0, -1.0, 0.1, 0xffff0000},
158 struct vertex litquad[] =
160 {-1.0, 0.0, 0.1, 0xff00ff00},
161 {-1.0, 1.0, 0.1, 0xff00ff00},
162 { 0.0, 1.0, 0.1, 0xff00ff00},
163 { 0.0, 0.0, 0.1, 0xff00ff00},
165 struct nvertex unlitnquad[] =
167 { 0.0, -1.0, 0.1, 1.0, 1.0, 1.0, 0xff0000ff},
168 { 0.0, 0.0, 0.1, 1.0, 1.0, 1.0, 0xff0000ff},
169 { 1.0, 0.0, 0.1, 1.0, 1.0, 1.0, 0xff0000ff},
170 { 1.0, -1.0, 0.1, 1.0, 1.0, 1.0, 0xff0000ff},
172 struct nvertex litnquad[] =
174 { 0.0, 0.0, 0.1, 1.0, 1.0, 1.0, 0xffffff00},
175 { 0.0, 1.0, 0.1, 1.0, 1.0, 1.0, 0xffffff00},
176 { 1.0, 1.0, 0.1, 1.0, 1.0, 1.0, 0xffffff00},
177 { 1.0, 0.0, 0.1, 1.0, 1.0, 1.0, 0xffffff00},
179 WORD Indices[] = {0, 1, 2, 2, 3, 0};
181 hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
182 ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
184 /* Setup some states that may cause issues */
185 hr = IDirect3DDevice9_SetTransform(device, D3DTS_WORLDMATRIX(0), (D3DMATRIX *) mat);
186 ok(hr == D3D_OK, "IDirect3DDevice9_SetTransform returned %s\n", DXGetErrorString9(hr));
187 hr = IDirect3DDevice9_SetTransform(device, D3DTS_VIEW, (D3DMATRIX *)mat);
188 ok(hr == D3D_OK, "IDirect3DDevice9_SetTransform returned %s\n", DXGetErrorString9(hr));
189 hr = IDirect3DDevice9_SetTransform(device, D3DTS_PROJECTION, (D3DMATRIX *) mat);
190 ok(hr == D3D_OK, "IDirect3DDevice9_SetTransform returned %s\n", DXGetErrorString9(hr));
191 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_CLIPPING, FALSE);
192 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
193 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ZENABLE, FALSE);
194 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
195 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
196 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
197 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_STENCILENABLE, FALSE);
198 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
199 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHATESTENABLE, FALSE);
200 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
201 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
202 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
203 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_SCISSORTESTENABLE, FALSE);
204 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
205 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE);
206 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState failed with %s\n", DXGetErrorString9(hr));
207 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLED_RED | D3DCOLORWRITEENABLED_GREEN | D3DCOLORWRITEENABLED_BLUE);
208 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState failed with %s\n", DXGetErrorString9(hr));
210 hr = IDirect3DDevice9_SetFVF(device, fvf);
211 ok(hr == D3D_OK, "IDirect3DDevice9_SetFVF returned %s\n", DXGetErrorString9(hr));
213 hr = IDirect3DDevice9_BeginScene(device);
214 ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene failed with %s\n", DXGetErrorString9(hr));
217 /* No lights are defined... That means, lit vertices should be entirely black */
218 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
219 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
220 hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
221 2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitquad, sizeof(unlitquad[0]));
222 ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
224 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, TRUE);
225 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
226 hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
227 2 /*PrimCount */, Indices, D3DFMT_INDEX16, litquad, sizeof(litquad[0]));
228 ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
230 hr = IDirect3DDevice9_SetFVF(device, nfvf);
231 ok(hr == D3D_OK, "IDirect3DDevice9_SetFVF failed with %s\n", DXGetErrorString9(hr));
233 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
234 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
235 hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
236 2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitnquad, sizeof(unlitnquad[0]));
237 ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
239 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, TRUE);
240 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
241 hr = IDirect3DDevice9_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
242 2 /*PrimCount */, Indices, D3DFMT_INDEX16, litnquad, sizeof(litnquad[0]));
243 ok(hr == D3D_OK, "IDirect3DDevice9_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString9(hr));
245 IDirect3DDevice9_EndScene(device);
246 ok(hr == D3D_OK, "IDirect3DDevice9_EndScene failed with %s\n", DXGetErrorString9(hr));
249 IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
251 color = getPixelColor(device, 160, 360); /* lower left quad - unlit without normals */
252 ok(color == 0x00ff0000, "Unlit quad without normals has color %08x\n", color);
253 color = getPixelColor(device, 160, 120); /* upper left quad - lit without normals */
254 ok(color == 0x00000000, "Lit quad without normals has color %08x\n", color);
255 color = getPixelColor(device, 480, 360); /* lower left quad - unlit width normals */
256 ok(color == 0x000000ff, "Unlit quad width normals has color %08x\n", color);
257 color = getPixelColor(device, 480, 120); /* upper left quad - lit width normals */
258 ok(color == 0x00000000, "Lit quad width normals has color %08x\n", color);
260 hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
261 ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderState returned %s\n", DXGetErrorString9(hr));
263 /* Hack for a bug in d3d9: SetFVF creates a converted vertex declaration, with a circular refcount.
264 * This prevents the screen resolution from being restored correctly on device release. Unset the vdecl
266 IDirect3DDevice9_SetVertexDeclaration(device, NULL);
269 static void clear_test(IDirect3DDevice9 *device)
271 /* Tests the correctness of clearing parameters */
277 hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
278 ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
280 /* Positive x, negative y */
286 /* Positive x, positive y */
291 /* Clear 2 rectangles with one call. Shows that a positive value is returned, but the negative rectangle
292 * is ignored, the positive is still cleared afterwards
294 hr = IDirect3DDevice9_Clear(device, 2, rect, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
295 ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
297 /* negative x, negative y */
298 rect_negneg.x1 = 640;
299 rect_negneg.x1 = 240;
300 rect_negneg.x2 = 320;
302 hr = IDirect3DDevice9_Clear(device, 1, &rect_negneg, D3DCLEAR_TARGET, 0xff00ff00, 0.0, 0);
303 ok(hr == D3D_OK, "IDirect3DDevice9_Clear failed with %s\n", DXGetErrorString9(hr));
305 IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
307 color = getPixelColor(device, 160, 360); /* lower left quad */
308 ok(color == 0x00ffffff, "Clear rectangle 3(pos, neg) has color %08x\n", color);
309 color = getPixelColor(device, 160, 120); /* upper left quad */
310 ok(color == 0x00ff0000, "Clear rectangle 1(pos, pos) has color %08x\n", color);
311 color = getPixelColor(device, 480, 360); /* lower right quad */
312 ok(color == 0x00ffffff, "Clear rectangle 4(NULL) has color %08x\n", color);
313 color = getPixelColor(device, 480, 120); /* upper right quad */
314 ok(color == 0x00ffffff, "Clear rectangle 4(neg, neg) has color %08x\n", color);
331 static void test_mova(IDirect3DDevice9 *device)
333 static const DWORD mova_test[] = {
334 0xfffe0200, /* vs_2_0 */
335 0x0200001f, 0x80000000, 0x900f0000, /* dcl_position v0 */
336 0x05000051, 0xa00f0000, 0x3f800000, 0x00000000, 0x00000000, 0x3f800000, /* def c0, 1.0, 0.0, 0.0, 1.0 */
337 0x05000051, 0xa00f0001, 0x3f800000, 0x3f800000, 0x00000000, 0x3f800000, /* def c1, 1.0, 1.0, 0.0, 1.0 */
338 0x05000051, 0xa00f0002, 0x00000000, 0x3f800000, 0x00000000, 0x3f800000, /* def c2, 0.0, 1.0, 0.0, 1.0 */
339 0x05000051, 0xa00f0003, 0x00000000, 0x3f800000, 0x3f800000, 0x3f800000, /* def c3, 0.0, 1.0, 1.0, 1.0 */
340 0x05000051, 0xa00f0004, 0x00000000, 0x00000000, 0x3f800000, 0x3f800000, /* def c4, 0.0, 0.0, 1.0, 1.0 */
341 0x05000051, 0xa00f0005, 0x3f800000, 0x00000000, 0x3f800000, 0x3f800000, /* def c5, 1.0, 0.0, 1.0, 1.0 */
342 0x05000051, 0xa00f0006, 0x3f800000, 0x3f800000, 0x3f800000, 0x3f800000, /* def c6, 1.0, 1.0, 1.0, 1.0 */
343 0x0200002e, 0xb0010000, 0xa0000007, /* mova a0.x, c7.x */
344 0x03000001, 0xd00f0000, 0xa0e42003, 0xb0000000, /* mov oD0, c[a0.x + 3] */
345 0x02000001, 0xc00f0000, 0x90e40000, /* mov oPos, v0 */
349 static const test_data_t test_data[] = {
350 {{-2.4f, 0.0f, 0.0f, 0.0f}, 0x00ffff00},
351 {{-1.6f, 0.0f, 0.0f, 0.0f}, 0x00ffff00},
352 {{-0.4f, 0.0f, 0.0f, 0.0f}, 0x0000ffff},
353 {{ 0.4f, 0.0f, 0.0f, 0.0f}, 0x0000ffff},
354 {{ 1.6f, 0.0f, 0.0f, 0.0f}, 0x00ff00ff},
355 {{ 2.4f, 0.0f, 0.0f, 0.0f}, 0x00ff00ff}
358 static const float quad[][3] = {
359 {-1.0f, -1.0f, 0.0f},
361 { 1.0f, -1.0f, 0.0f},
365 static const D3DVERTEXELEMENT9 decl_elements[] = {
366 {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
370 IDirect3DVertexDeclaration9 *vertex_declaration = NULL;
371 IDirect3DVertexShader9 *mova_shader = NULL;
375 hr = IDirect3DDevice9_CreateVertexShader(device, mova_test, &mova_shader);
376 ok(SUCCEEDED(hr), "CreateVertexShader failed (%08x)\n", hr);
377 hr = IDirect3DDevice9_SetVertexShader(device, mova_shader);
378 ok(SUCCEEDED(hr), "SetVertexShader failed (%08x)\n", hr);
380 hr = IDirect3DDevice9_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
381 ok(SUCCEEDED(hr), "CreateVertexDeclaration failed (%08x)\n", hr);
382 hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
383 ok(SUCCEEDED(hr), "SetVertexDeclaration failed (%08x)\n", hr);
385 for (i = 0; i < (sizeof(test_data) / sizeof(test_data_t)); ++i)
389 hr = IDirect3DDevice9_SetVertexShaderConstantF(device, 7, test_data[i].in, 1);
390 ok(SUCCEEDED(hr), "SetVertexShaderConstantF failed (%08x)\n", hr);
392 hr = IDirect3DDevice9_BeginScene(device);
393 ok(SUCCEEDED(hr), "BeginScene failed (%08x)\n", hr);
395 hr = IDirect3DDevice9_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
396 ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%08x)\n", hr);
398 hr = IDirect3DDevice9_EndScene(device);
399 ok(SUCCEEDED(hr), "EndScene failed (%08x)\n", hr);
401 hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
402 ok(SUCCEEDED(hr), "Present failed (%08x)\n", hr);
404 color = getPixelColor(device, 320, 240);
405 ok(color == test_data[i].out, "Expected color %08x, got %08x (for input %f)\n", test_data[i].out, color, test_data[i].in[0]);
407 hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0, 0.0f, 0);
408 ok(SUCCEEDED(hr), "Clear failed (%08x)\n", hr);
411 IDirect3DVertexDeclaration9_Release(vertex_declaration);
412 IDirect3DVertexShader9_Release(mova_shader);
417 IDirect3DDevice9 *device_ptr;
422 d3d9_handle = LoadLibraryA("d3d9.dll");
425 trace("Could not load d3d9.dll, skipping tests\n");
429 device_ptr = init_d3d9();
430 if (!device_ptr) return;
432 IDirect3DDevice9_GetDeviceCaps(device_ptr, &caps);
434 /* Check for the reliability of the returned data */
435 hr = IDirect3DDevice9_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
438 trace("Clear failed, can't assure correctness of the test results, skipping\n");
441 IDirect3DDevice9_Present(device_ptr, NULL, NULL, NULL, NULL);
443 color = getPixelColor(device_ptr, 1, 1);
444 if(color !=0x00ff0000)
446 trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
450 hr = IDirect3DDevice9_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
453 trace("Clear failed, can't assure correctness of the test results, skipping\n");
456 IDirect3DDevice9_Present(device_ptr, NULL, NULL, NULL, NULL);
458 color = getPixelColor(device_ptr, 639, 479);
459 if(color != 0x0000ddee)
461 trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
465 /* Now execute the real tests */
466 lighting_test(device_ptr);
467 clear_test(device_ptr);
469 if (caps.VertexShaderVersion >= D3DVS_VERSION(2, 0))
471 test_mova(device_ptr);
473 else skip("No vs_2_0 support\n");
476 if(device_ptr) IDirect3DDevice9_Release(device_ptr);