d3d8/tests: Add a P8 texture test.
[wine.git] / dlls / d3d8 / tests / visual.c
blob601a33c4e35e445b073502a76b5e49bd49c84566
1 /*
2 * Copyright (C) 2005 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 /* See comment in dlls/d3d9/tests/visual.c for general guidelines */
22 #define COBJMACROS
23 #include <d3d8.h>
24 #include <dxerr8.h>
25 #include "wine/test.h"
27 static HMODULE d3d8_handle = 0;
29 static HWND create_window(void)
31 WNDCLASS wc = {0};
32 HWND ret;
33 wc.lpfnWndProc = &DefWindowProc;
34 wc.lpszClassName = "d3d8_test_wc";
35 RegisterClass(&wc);
37 ret = CreateWindow("d3d8_test_wc", "d3d8_test",
38 WS_MAXIMIZE | WS_VISIBLE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
39 return ret;
42 static DWORD getPixelColor(IDirect3DDevice8 *device, UINT x, UINT y)
44 DWORD ret;
45 IDirect3DSurface8 *surf;
46 IDirect3DTexture8 *tex;
47 HRESULT hr;
48 D3DLOCKED_RECT lockedRect;
49 RECT rectToLock = {x, y, x+1, y+1};
51 hr = IDirect3DDevice8_CreateTexture(device, 640, 480, 1 /* Levels */, 0 /* usage */, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &tex);
52 if(FAILED(hr) || !tex ) /* This is not a test */
54 trace("Can't create an offscreen plain surface to read the render target data, hr=%#08x\n", hr);
55 return 0xdeadbeef;
57 hr = IDirect3DTexture8_GetSurfaceLevel(tex, 0, &surf);
58 if(FAILED(hr) || !tex ) /* This is not a test */
60 trace("Can't get surface from texture, hr=%#08x\n", hr);
61 ret = 0xdeadbeee;
62 goto out;
65 hr = IDirect3DDevice8_GetFrontBuffer(device, surf);
66 if(FAILED(hr))
68 trace("Can't read the front buffer data, hr=%#08x\n", hr);
69 ret = 0xdeadbeed;
70 goto out;
73 hr = IDirect3DSurface8_LockRect(surf, &lockedRect, &rectToLock, D3DLOCK_READONLY);
74 if(FAILED(hr))
76 trace("Can't lock the offscreen surface, hr=%#08x\n", hr);
77 ret = 0xdeadbeec;
78 goto out;
80 /* Remove the X channel for now. DirectX and OpenGL have different ideas how to treat it apparently, and it isn't
81 * really important for these tests
83 ret = ((DWORD *) lockedRect.pBits)[0] & 0x00ffffff;
84 hr = IDirect3DSurface8_UnlockRect(surf);
85 if(FAILED(hr))
87 trace("Can't unlock the offscreen surface, hr=%#08x\n", hr);
90 out:
91 if(surf) IDirect3DSurface8_Release(surf);
92 if(tex) IDirect3DTexture8_Release(tex);
93 return ret;
96 static IDirect3DDevice8 *init_d3d8(void)
98 IDirect3D8 * (__stdcall * d3d8_create)(UINT SDKVersion) = 0;
99 IDirect3D8 *d3d8_ptr = 0;
100 IDirect3DDevice8 *device_ptr = 0;
101 D3DPRESENT_PARAMETERS present_parameters;
102 HRESULT hr;
104 d3d8_create = (void *)GetProcAddress(d3d8_handle, "Direct3DCreate8");
105 ok(d3d8_create != NULL, "Failed to get address of Direct3DCreate8\n");
106 if (!d3d8_create) return NULL;
108 d3d8_ptr = d3d8_create(D3D_SDK_VERSION);
109 ok(d3d8_ptr != NULL, "Failed to create IDirect3D8 object\n");
110 if (!d3d8_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;
119 present_parameters.EnableAutoDepthStencil = TRUE;
120 present_parameters.AutoDepthStencilFormat = D3DFMT_D16;
122 hr = IDirect3D8_CreateDevice(d3d8_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, present_parameters.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
123 ok(hr == D3D_OK || hr == D3DERR_INVALIDCALL, "IDirect3D_CreateDevice returned: %#08x\n", hr);
125 return device_ptr;
128 struct vertex
130 float x, y, z;
131 DWORD diffuse;
134 struct nvertex
136 float x, y, z;
137 float nx, ny, nz;
138 DWORD diffuse;
141 static void lighting_test(IDirect3DDevice8 *device)
143 HRESULT hr;
144 DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
145 DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
146 DWORD color;
148 float mat[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
149 0.0f, 1.0f, 0.0f, 0.0f,
150 0.0f, 0.0f, 1.0f, 0.0f,
151 0.0f, 0.0f, 0.0f, 1.0f };
153 struct vertex unlitquad[] =
155 {-1.0f, -1.0f, 0.1f, 0xffff0000},
156 {-1.0f, 0.0f, 0.1f, 0xffff0000},
157 { 0.0f, 0.0f, 0.1f, 0xffff0000},
158 { 0.0f, -1.0f, 0.1f, 0xffff0000},
160 struct vertex litquad[] =
162 {-1.0f, 0.0f, 0.1f, 0xff00ff00},
163 {-1.0f, 1.0f, 0.1f, 0xff00ff00},
164 { 0.0f, 1.0f, 0.1f, 0xff00ff00},
165 { 0.0f, 0.0f, 0.1f, 0xff00ff00},
167 struct nvertex unlitnquad[] =
169 { 0.0f, -1.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
170 { 0.0f, 0.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
171 { 1.0f, 0.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
172 { 1.0f, -1.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xff0000ff},
174 struct nvertex litnquad[] =
176 { 0.0f, 0.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xffffff00},
177 { 0.0f, 1.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xffffff00},
178 { 1.0f, 1.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xffffff00},
179 { 1.0f, 0.0f, 0.1f, 1.0f, 1.0f, 1.0f, 0xffffff00},
181 WORD Indices[] = {0, 1, 2, 2, 3, 0};
183 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
184 ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
186 /* Setup some states that may cause issues */
187 hr = IDirect3DDevice8_SetTransform(device, D3DTS_WORLDMATRIX(0), (D3DMATRIX *) mat);
188 ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %#08x\n", hr);
189 hr = IDirect3DDevice8_SetTransform(device, D3DTS_VIEW, (D3DMATRIX *)mat);
190 ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %#08x\n", hr);
191 hr = IDirect3DDevice8_SetTransform(device, D3DTS_PROJECTION, (D3DMATRIX *) mat);
192 ok(hr == D3D_OK, "IDirect3DDevice8_SetTransform returned %#08x\n", hr);
193 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CLIPPING, FALSE);
194 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
195 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, FALSE);
196 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
197 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
198 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
199 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_STENCILENABLE, FALSE);
200 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
201 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHATESTENABLE, FALSE);
202 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
203 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
204 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
205 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE);
206 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed with %#08x\n", hr);
207 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
208 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed with %#08x\n", hr);
210 hr = IDirect3DDevice8_SetVertexShader(device, fvf);
211 ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
213 hr = IDirect3DDevice8_BeginScene(device);
214 ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %#08x\n", hr);
215 if(hr == D3D_OK)
217 /* No lights are defined... That means, lit vertices should be entirely black */
218 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
219 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
220 hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
221 2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitquad, sizeof(unlitquad[0]));
222 ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
224 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, TRUE);
225 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
226 hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
227 2 /*PrimCount */, Indices, D3DFMT_INDEX16, litquad, sizeof(litquad[0]));
228 ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
230 hr = IDirect3DDevice8_SetVertexShader(device, nfvf);
231 ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader failed with %#08x\n", hr);
233 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
234 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
235 hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
236 2 /*PrimCount */, Indices, D3DFMT_INDEX16, unlitnquad, sizeof(unlitnquad[0]));
237 ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
239 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, TRUE);
240 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
241 hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
242 2 /*PrimCount */, Indices, D3DFMT_INDEX16, litnquad, sizeof(litnquad[0]));
243 ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %#08x\n", hr);
245 IDirect3DDevice8_EndScene(device);
246 ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %#08x\n", hr);
249 IDirect3DDevice8_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 = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
261 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %#08x\n", hr);
264 static void clear_test(IDirect3DDevice8 *device)
266 /* Tests the correctness of clearing parameters */
267 HRESULT hr;
268 D3DRECT rect[2];
269 D3DRECT rect_negneg;
270 DWORD color;
272 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.0, 0);
273 ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
275 /* Positive x, negative y */
276 rect[0].x1 = 0;
277 rect[0].y1 = 480;
278 rect[0].x2 = 320;
279 rect[0].y2 = 240;
281 /* Positive x, positive y */
282 rect[1].x1 = 0;
283 rect[1].y1 = 0;
284 rect[1].x2 = 320;
285 rect[1].y2 = 240;
286 /* Clear 2 rectangles with one call. Shows that a positive value is returned, but the negative rectangle
287 * is ignored, the positive is still cleared afterwards
289 hr = IDirect3DDevice8_Clear(device, 2, rect, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
290 ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
292 /* negative x, negative y */
293 rect_negneg.x1 = 640;
294 rect_negneg.x1 = 240;
295 rect_negneg.x2 = 320;
296 rect_negneg.y2 = 0;
297 hr = IDirect3DDevice8_Clear(device, 1, &rect_negneg, D3DCLEAR_TARGET, 0xff00ff00, 0.0, 0);
298 ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
300 IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
302 color = getPixelColor(device, 160, 360); /* lower left quad */
303 ok(color == 0x00ffffff, "Clear rectangle 3(pos, neg) has color %08x\n", color);
304 color = getPixelColor(device, 160, 120); /* upper left quad */
305 ok(color == 0x00ff0000, "Clear rectangle 1(pos, pos) has color %08x\n", color);
306 color = getPixelColor(device, 480, 360); /* lower right quad */
307 ok(color == 0x00ffffff, "Clear rectangle 4(NULL) has color %08x\n", color);
308 color = getPixelColor(device, 480, 120); /* upper right quad */
309 ok(color == 0x00ffffff, "Clear rectangle 4(neg, neg) has color %08x\n", color);
312 struct sVertex {
313 float x, y, z;
314 DWORD diffuse;
315 DWORD specular;
318 struct sVertexT {
319 float x, y, z, rhw;
320 DWORD diffuse;
321 DWORD specular;
324 static void fog_test(IDirect3DDevice8 *device)
326 HRESULT hr;
327 DWORD color;
328 float start = 0.0, end = 1.0;
330 /* Gets full z based fog with linear fog, no fog with specular color */
331 struct sVertex unstransformed_1[] = {
332 {-1, -1, 0.1f, 0xFFFF0000, 0xFF000000 },
333 {-1, 0, 0.1f, 0xFFFF0000, 0xFF000000 },
334 { 0, 0, 0.1f, 0xFFFF0000, 0xFF000000 },
335 { 0, -1, 0.1f, 0xFFFF0000, 0xFF000000 },
337 /* Ok, I am too lazy to deal with transform matrices */
338 struct sVertex unstransformed_2[] = {
339 {-1, 0, 1.0f, 0xFFFF0000, 0xFF000000 },
340 {-1, 1, 1.0f, 0xFFFF0000, 0xFF000000 },
341 { 0, 1, 1.0f, 0xFFFF0000, 0xFF000000 },
342 { 0, 0, 1.0f, 0xFFFF0000, 0xFF000000 },
344 /* Untransformed ones. Give them a different diffuse color to make the test look
345 * nicer. It also makes making sure that they are drawn correctly easier.
347 struct sVertexT transformed_1[] = {
348 {320, 0, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
349 {640, 0, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
350 {640, 240, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
351 {320, 240, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
353 struct sVertexT transformed_2[] = {
354 {320, 240, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
355 {640, 240, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
356 {640, 480, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
357 {320, 480, 1.0f, 1.0f, 0xFFFFFF00, 0xFF000000 },
359 WORD Indices[] = {0, 1, 2, 2, 3, 0};
361 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
362 ok(hr == D3D_OK, "IDirect3DDevice8_Clear returned %#08x\n", hr);
364 /* Setup initial states: No lighting, fog on, fog color */
365 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
366 ok(hr == D3D_OK, "Turning off lighting returned %#08x\n", hr);
367 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, TRUE);
368 ok(hr == D3D_OK, "Turning on fog calculations returned %#08x\n", hr);
369 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGCOLOR, 0xFF00FF00 /* A nice green */);
370 ok(hr == D3D_OK, "Turning on fog calculations returned %#08x\n", hr);
372 /* First test: Both table fog and vertex fog off */
373 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_NONE);
374 ok(hr == D3D_OK, "Turning off table fog returned %#08x\n", hr);
375 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
376 ok(hr == D3D_OK, "Turning off table fog returned %#08x\n", hr);
378 /* Start = 0, end = 1. Should be default, but set them */
379 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGSTART, *((DWORD *) &start));
380 ok(hr == D3D_OK, "Setting fog start returned %#08x\n", hr);
381 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGEND, *((DWORD *) &end));
382 ok(hr == D3D_OK, "Setting fog start returned %#08x\n", hr);
384 if(IDirect3DDevice8_BeginScene(device) == D3D_OK)
386 hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
387 ok( hr == D3D_OK, "SetVertexShader returned %#08x\n", hr);
388 /* Untransformed, vertex fog = NONE, table fog = NONE: Read the fog weighting from the specular color */
389 hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
390 2 /*PrimCount */, Indices, D3DFMT_INDEX16, unstransformed_1,
391 sizeof(unstransformed_1[0]));
392 ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
394 /* That makes it use the Z value */
395 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);
396 ok(hr == D3D_OK, "Turning off table fog returned %#08x\n", hr);
397 /* Untransformed, vertex fog != none (or table fog != none):
398 * Use the Z value as input into the equation
400 hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
401 2 /*PrimCount */, Indices, D3DFMT_INDEX16, unstransformed_2,
402 sizeof(unstransformed_1[0]));
403 ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
405 /* transformed verts */
406 hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR);
407 ok( hr == D3D_OK, "SetVertexShader returned %#08x\n", hr);
408 /* Transformed, vertex fog != NONE, pixel fog == NONE: Use specular color alpha component */
409 hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
410 2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_1,
411 sizeof(transformed_1[0]));
412 ok(hr == D3D_OK, "DrawIndexedPrimitiveUP returned %#08x\n", hr);
414 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
415 ok( hr == D3D_OK, "Setting fog table mode to D3DFOG_LINEAR returned %#08x\n", hr);
416 /* Transformed, table fog != none, vertex anything: Use Z value as input to the fog
417 * equation
419 hr = IDirect3DDevice8_DrawIndexedPrimitiveUP(device, D3DPT_TRIANGLELIST, 0 /* MinIndex */, 4 /* NumVerts */,
420 2 /*PrimCount */, Indices, D3DFMT_INDEX16, transformed_2,
421 sizeof(transformed_2[0]));
423 hr = IDirect3DDevice8_EndScene(device);
424 ok(hr == D3D_OK, "EndScene returned %#08x\n", hr);
426 else
428 ok(FALSE, "BeginScene failed\n");
431 IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
432 color = getPixelColor(device, 160, 360);
433 ok(color == 0x00FF0000, "Untransformed vertex with no table or vertex fog has color %08x\n", color);
434 color = getPixelColor(device, 160, 120);
435 ok(color == 0x0000FF00, "Untransformed vertex with linear vertex fog has color %08x\n", color);
436 color = getPixelColor(device, 480, 120);
437 ok(color == 0x00FFFF00, "Transformed vertex with linear vertex fog has color %08x\n", color);
438 color = getPixelColor(device, 480, 360);
439 ok(color == 0x0000FF00, "Transformed vertex with linear table fog has color %08x\n", color);
441 /* Turn off the fog master switch to avoid confusing other tests */
442 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_FOGENABLE, FALSE);
443 ok(hr == D3D_OK, "Turning off fog calculations returned %#08x\n", hr);
446 static void present_test(IDirect3DDevice8 *device)
448 struct vertex quad[] =
450 {-1.0f, -1.0f, 0.9f, 0xffff0000},
451 {-1.0f, 1.0f, 0.9f, 0xffff0000},
452 { 1.0f, -1.0f, 0.1f, 0xffff0000},
453 { 1.0f, 1.0f, 0.1f, 0xffff0000},
455 HRESULT hr;
456 DWORD color;
458 /* Does the Present clear the depth stencil? Clear the depth buffer with some value != 0,
459 * then call Present. Then clear the color buffer to make sure it has some defined content
460 * after the Present with D3DSWAPEFFECT_DISCARD. After that draw a plane that is somewhere cut
461 * by the depth value.
463 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 0.75, 0);
464 ok(hr == D3D_OK, "IDirect3DDevice8_Clear returned %s\n", DXGetErrorString8(hr));
465 hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
466 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 0.4, 0);
468 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_TRUE);
469 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
470 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZFUNC, D3DCMP_GREATER);
471 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
472 hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
473 ok(hr == D3D_OK, "IDirect3DDevice8_SetFVF returned %s\n", DXGetErrorString8(hr));
475 hr = IDirect3DDevice8_BeginScene(device);
476 ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed with %s\n", DXGetErrorString8(hr));
477 if(hr == D3D_OK)
479 /* No lights are defined... That means, lit vertices should be entirely black */
480 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2 /*PrimCount */, quad, sizeof(quad[0]));
481 ok(hr == D3D_OK, "IDirect3DDevice8_DrawIndexedPrimitiveUP failed with %s\n", DXGetErrorString8(hr));
483 hr = IDirect3DDevice8_EndScene(device);
484 ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed with %s\n", DXGetErrorString8(hr));
487 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ZENABLE, D3DZB_FALSE);
488 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
490 hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
491 ok(SUCCEEDED(hr), "Present failed (%#08x)\n", hr);
492 color = getPixelColor(device, 512, 240);
493 ok(color == 0x00ffffff, "Present failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
494 color = getPixelColor(device, 64, 240);
495 ok(color == 0x00ff0000, "Present failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
498 static void test_rcp_rsq(IDirect3DDevice8 *device)
500 HRESULT hr;
501 DWORD shader;
502 DWORD color;
503 unsigned char c1, c2, c3;
504 float constant[4] = {1.0, 1.0, 1.0, 2.0};
506 static const float quad[][3] = {
507 {-1.0f, -1.0f, 0.0f},
508 {-1.0f, 1.0f, 0.0f},
509 { 1.0f, -1.0f, 0.0f},
510 { 1.0f, 1.0f, 0.0f},
513 const DWORD rcp_test[] = {
514 0xfffe0101, /* vs.1.1 */
516 0x0009fffe, 0x30303030, 0x30303030, /* Shaders have to have a minimal size. */
517 0x30303030, 0x30303030, 0x30303030, /* Add a filler comment. Usually D3DX8's*/
518 0x30303030, 0x30303030, 0x30303030, /* version comment makes the shader big */
519 0x00303030, /* enough to make windows happy */
521 0x00000001, 0xc00f0000, 0x90e40000, /* mov oPos, v0 */
522 0x00000006, 0xd00f0000, 0xa0e40000, /* rcp oD0, c0 */
523 0x0000ffff /* END */
526 const DWORD rsq_test[] = {
527 0xfffe0101, /* vs.1.1 */
529 0x0009fffe, 0x30303030, 0x30303030, /* Shaders have to have a minimal size. */
530 0x30303030, 0x30303030, 0x30303030, /* Add a filler comment. Usually D3DX8's*/
531 0x30303030, 0x30303030, 0x30303030, /* version comment makes the shader big */
532 0x00303030, /* enough to make windows happy */
534 0x00000001, 0xc00f0000, 0x90e40000, /* mov oPos, v0 */
535 0x00000007, 0xd00f0000, 0xa0e40000, /* rsq oD0, c0 */
536 0x0000ffff /* END */
539 DWORD decl[] =
541 D3DVSD_STREAM(0),
542 D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3), /* D3DVSDE_POSITION, Register v0 */
543 D3DVSD_END()
546 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff800080, 0.0, 0);
547 ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
549 hr = IDirect3DDevice8_CreateVertexShader(device, decl, rcp_test, &shader, 0);
550 ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned with %#08x\n", hr);
552 IDirect3DDevice8_SetVertexShader(device, shader);
553 ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
554 IDirect3DDevice8_SetVertexShaderConstant(device, 0, constant, 1);
556 hr = IDirect3DDevice8_BeginScene(device);
557 ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
558 if(SUCCEEDED(hr))
560 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
561 ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%#08x)\n", hr);
562 hr = IDirect3DDevice8_EndScene(device);
563 ok(hr == D3D_OK, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
566 hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
567 ok(SUCCEEDED(hr), "Present failed (%#08x)\n", hr);
568 color = getPixelColor(device, 320, 240);
569 c1 = (color & 0x00ff0000 )>> 16;
570 c2 = (color & 0x0000ff00 )>> 8;
571 c3 = (color & 0x000000ff )>> 0;
572 ok(c1 == c2 && c2 == c3, "Color components differ: c1 = %02x, c2 = %02x, c3 = %02x\n",
573 c1, c2, c3);
574 ok(c1 >= 0x7c && c1 <= 0x84, "Color component value is %02x\n", c1);
576 IDirect3DDevice8_SetVertexShader(device, 0);
577 IDirect3DDevice8_DeleteVertexShader(device, shader);
579 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff800080, 0.0, 0);
580 ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed with %#08x\n", hr);
582 hr = IDirect3DDevice8_CreateVertexShader(device, decl, rsq_test, &shader, 0);
583 ok(hr == D3D_OK, "IDirect3DDevice8_CreateVertexShader returned with %#08x\n", hr);
585 IDirect3DDevice8_SetVertexShader(device, shader);
586 ok(hr == D3D_OK, "IDirect3DDevice8_SetVertexShader returned %#08x\n", hr);
587 IDirect3DDevice8_SetVertexShaderConstant(device, 0, constant, 1);
589 hr = IDirect3DDevice8_BeginScene(device);
590 ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene returned %#08x\n", hr);
591 if(SUCCEEDED(hr))
593 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, &quad[0], 3 * sizeof(float));
594 ok(SUCCEEDED(hr), "DrawPrimitiveUP failed (%#08x)\n", hr);
595 hr = IDirect3DDevice8_EndScene(device);
596 ok(hr == D3D_OK, "IDirect3DDevice8_EndScene returned %#08x\n", hr);
599 hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
600 ok(SUCCEEDED(hr), "Present failed (%#08x)\n", hr);
601 color = getPixelColor(device, 320, 240);
602 c1 = (color & 0x00ff0000 )>> 16;
603 c2 = (color & 0x0000ff00 )>> 8;
604 c3 = (color & 0x000000ff )>> 0;
605 ok(c1 == c2 && c2 == c3, "Color components differ: c1 = %02x, c2 = %02x, c3 = %02x\n",
606 c1, c2, c3);
607 ok(c1 >= 0xb0 && c1 <= 0xb8, "Color component value is %02x\n", c1);
609 IDirect3DDevice8_SetVertexShader(device, 0);
610 IDirect3DDevice8_DeleteVertexShader(device, shader);
613 static void offscreen_test(IDirect3DDevice8 *device)
615 HRESULT hr;
616 IDirect3DTexture8 *offscreenTexture = NULL;
617 IDirect3DSurface8 *backbuffer = NULL, *offscreen = NULL, *depthstencil = NULL;
618 DWORD color;
620 static const float quad[][5] = {
621 {-0.5f, -0.5f, 0.1f, 0.0f, 0.0f},
622 {-0.5f, 0.5f, 0.1f, 0.0f, 1.0f},
623 { 0.5f, -0.5f, 0.1f, 1.0f, 0.0f},
624 { 0.5f, 0.5f, 0.1f, 1.0f, 1.0f},
627 hr = IDirect3DDevice8_GetDepthStencilSurface(device, &depthstencil);
628 ok(hr == D3D_OK, "IDirect3DDevice8_GetDepthStencilSurface failed, hr = %#08x\n", hr);
630 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
631 ok(hr == D3D_OK, "Clear failed, hr = %#08x\n", hr);
633 hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &offscreenTexture);
634 ok(hr == D3D_OK || D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
635 if(!offscreenTexture) {
636 trace("Failed to create an X8R8G8B8 offscreen texture, trying R5G6B5\n");
637 hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &offscreenTexture);
638 ok(hr == D3D_OK || D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
639 if(!offscreenTexture) {
640 skip("Cannot create an offscreen render target\n");
641 goto out;
645 hr = IDirect3DDevice8_GetBackBuffer(device, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
646 ok(hr == D3D_OK, "Can't get back buffer, hr = %#08x\n", hr);
647 if(!backbuffer) {
648 goto out;
651 hr = IDirect3DTexture8_GetSurfaceLevel(offscreenTexture, 0, &offscreen);
652 ok(hr == D3D_OK, "Can't get offscreen surface, hr = %#08x\n", hr);
653 if(!offscreen) {
654 goto out;
657 hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
658 ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
660 hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
661 ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
662 hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
663 ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
664 hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_NONE);
665 ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (%#08x)\n", hr);
666 hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_NONE);
667 ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (%#08x)\n", hr);
668 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
669 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
671 if(IDirect3DDevice8_BeginScene(device) == D3D_OK) {
672 hr = IDirect3DDevice8_SetRenderTarget(device, offscreen, depthstencil);
673 ok(hr == D3D_OK, "SetRenderTarget failed, hr = %#08x\n", hr);
674 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff00ff, 0.0, 0);
675 ok(hr == D3D_OK, "Clear failed, hr = %#08x\n", hr);
677 /* Draw without textures - Should resut in a white quad */
678 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(quad[0]));
679 ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
681 hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
682 ok(hr == D3D_OK, "SetRenderTarget failed, hr = %#08x\n", hr);
683 hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) offscreenTexture);
684 ok(hr == D3D_OK, "SetTexture failed, %s\n", DXGetErrorString8(hr));
686 /* This time with the texture */
687 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, sizeof(quad[0]));
688 ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
690 IDirect3DDevice8_EndScene(device);
693 IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
695 /* Center quad - should be white */
696 color = getPixelColor(device, 320, 240);
697 ok(color == 0x00ffffff, "Offscreen failed: Got color 0x%08x, expected 0x00ffffff.\n", color);
698 /* Some quad in the cleared part of the texture */
699 color = getPixelColor(device, 170, 240);
700 ok(color == 0x00ff00ff, "Offscreen failed: Got color 0x%08x, expected 0x00ff00ff.\n", color);
701 /* Part of the originally cleared back buffer */
702 color = getPixelColor(device, 10, 10);
703 ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
704 if(0) {
705 /* Lower left corner of the screen, where back buffer offscreen rendering draws the offscreen texture.
706 * It should be red, but the offscreen texture may leave some junk there. Not tested yet. Depending on
707 * the offscreen rendering mode this test would succeed or fail
709 color = getPixelColor(device, 10, 470);
710 ok(color == 0x00ff0000, "Offscreen failed: Got color 0x%08x, expected 0x00ff0000.\n", color);
713 out:
714 hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
716 /* restore things */
717 if(backbuffer) {
718 hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
719 IDirect3DSurface8_Release(backbuffer);
721 if(offscreenTexture) {
722 IDirect3DTexture8_Release(offscreenTexture);
724 if(offscreen) {
725 IDirect3DSurface8_Release(offscreen);
727 if(depthstencil) {
728 IDirect3DSurface8_Release(depthstencil);
732 static void alpha_test(IDirect3DDevice8 *device)
734 HRESULT hr;
735 IDirect3DTexture8 *offscreenTexture;
736 IDirect3DSurface8 *backbuffer = NULL, *offscreen = NULL, *depthstencil = NULL;
737 DWORD color, red, green, blue;
739 struct vertex quad1[] =
741 {-1.0f, -1.0f, 0.1f, 0x4000ff00},
742 {-1.0f, 0.0f, 0.1f, 0x4000ff00},
743 { 1.0f, -1.0f, 0.1f, 0x4000ff00},
744 { 1.0f, 0.0f, 0.1f, 0x4000ff00},
746 struct vertex quad2[] =
748 {-1.0f, 0.0f, 0.1f, 0xc00000ff},
749 {-1.0f, 1.0f, 0.1f, 0xc00000ff},
750 { 1.0f, 0.0f, 0.1f, 0xc00000ff},
751 { 1.0f, 1.0f, 0.1f, 0xc00000ff},
753 static const float composite_quad[][5] = {
754 { 0.0f, -1.0f, 0.1f, 0.0f, 1.0f},
755 { 0.0f, 1.0f, 0.1f, 0.0f, 0.0f},
756 { 1.0f, -1.0f, 0.1f, 1.0f, 1.0f},
757 { 1.0f, 1.0f, 0.1f, 1.0f, 0.0f},
760 /* Clear the render target with alpha = 0.5 */
761 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
762 ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
764 hr = IDirect3DDevice8_CreateTexture(device, 128, 128, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &offscreenTexture);
765 ok(hr == D3D_OK || D3DERR_INVALIDCALL, "Creating the offscreen render target failed, hr = %#08x\n", hr);
767 hr = IDirect3DDevice8_GetDepthStencilSurface(device, &depthstencil);
768 ok(hr == D3D_OK, "IDirect3DDevice8_GetDepthStencilSurface failed, hr = %#08x\n", hr);
770 hr = IDirect3DDevice8_GetBackBuffer(device, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
771 ok(hr == D3D_OK, "Can't get back buffer, hr = %#08x\n", hr);
772 if(!backbuffer) {
773 goto out;
775 hr = IDirect3DTexture8_GetSurfaceLevel(offscreenTexture, 0, &offscreen);
776 ok(hr == D3D_OK, "Can't get offscreen surface, hr = %#08x\n", hr);
777 if(!offscreen) {
778 goto out;
781 hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
782 ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
784 hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
785 ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
786 hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
787 ok(hr == D3D_OK, "SetTextureStageState failed, hr = %#08x\n", hr);
788 hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MINFILTER, D3DTEXF_NONE);
789 ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MINFILTER failed (%#08x)\n", hr);
790 hr = IDirect3DDevice8_SetTextureStageState(device, 0, D3DTSS_MAGFILTER, D3DTEXF_NONE);
791 ok(SUCCEEDED(hr), "SetTextureStageState D3DSAMP_MAGFILTER failed (%#08x)\n", hr);
792 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
793 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState returned %s\n", DXGetErrorString8(hr));
795 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
796 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
797 if(IDirect3DDevice8_BeginScene(device) == D3D_OK) {
799 /* Draw two quads, one with src alpha blending, one with dest alpha blending. The
800 * SRCALPHA / INVSRCALPHA blend doesn't give any surprises. Colors are blended based on
801 * the input alpha
803 * The DESTALPHA / INVDESTALPHA do not "work" on the regular buffer because there is no alpha.
804 * They give essentially ZERO and ONE blend factors
806 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
807 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
808 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
809 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
810 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(quad1[0]));
811 ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
813 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_DESTALPHA);
814 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
815 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVDESTALPHA);
816 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
817 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(quad2[0]));
818 ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
820 /* Switch to the offscreen buffer, and redo the testing. SRCALPHA and DESTALPHA. The offscreen buffer
821 * has a alpha channel on its own. Clear the offscreen buffer with alpha = 0.5 again, then draw the
822 * quads again. The SRCALPHA/INVSRCALPHA doesn't give any surprises, but the DESTALPHA/INVDESTALPHA
823 * blending works as supposed now - blend factor is 0.5 in both cases, not 0.75 as from the input
824 * vertices
826 hr = IDirect3DDevice8_SetRenderTarget(device, offscreen, 0);
827 ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
828 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x80ff0000, 0.0, 0);
829 ok(hr == D3D_OK, "Clear failed, hr = %08x\n", hr);
831 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
832 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
833 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
834 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
835 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad1, sizeof(quad1[0]));
836 ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
838 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_DESTALPHA);
839 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
840 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVDESTALPHA);
841 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
842 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, sizeof(quad2[0]));
843 ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
845 hr = IDirect3DDevice8_SetRenderTarget(device, backbuffer, depthstencil);
846 ok(hr == D3D_OK, "Can't get back buffer, hr = %08x\n", hr);
848 /* Render the offscreen texture onto the frame buffer to be able to compare it regularly.
849 * Disable alpha blending for the final composition
851 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
852 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
853 hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
854 ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
856 hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) offscreenTexture);
857 ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
858 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, composite_quad, sizeof(float) * 5);
859 ok(hr == D3D_OK, "DrawPrimitiveUP failed, hr = %#08x\n", hr);
860 hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
861 ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
863 hr = IDirect3DDevice8_EndScene(device);
864 ok(hr == D3D_OK, "IDirect3DDevice7_EndScene failed, hr = %08x\n", hr);
867 IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
869 color = getPixelColor(device, 160, 360);
870 red = (color & 0x00ff0000) >> 16;
871 green = (color & 0x0000ff00) >> 8;
872 blue = (color & 0x000000ff);
873 ok(red >= 0xbe && red <= 0xc0 && green >= 0x39 && green <= 0x41 && blue == 0x00,
874 "SRCALPHA on frame buffer returned color %08x, expected 0x00bf4000\n", color);
876 color = getPixelColor(device, 160, 120);
877 red = (color & 0x00ff0000) >> 16;
878 green = (color & 0x0000ff00) >> 8;
879 blue = (color & 0x000000ff);
880 ok(red == 0x00 && green == 0x00 && blue >= 0xfe && blue <= 0xff ,
881 "DSTALPHA on frame buffer returned color %08x, expected 0x00ff0000\n", color);
883 color = getPixelColor(device, 480, 360);
884 red = (color & 0x00ff0000) >> 16;
885 green = (color & 0x0000ff00) >> 8;
886 blue = (color & 0x000000ff);
887 ok(red >= 0xbe && red <= 0xc0 && green >= 0x39 && green <= 0x41 && blue == 0x00,
888 "SRCALPHA on texture returned color %08x, expected bar\n", color);
890 color = getPixelColor(device, 480, 120);
891 red = (color & 0x00ff0000) >> 16;
892 green = (color & 0x0000ff00) >> 8;
893 blue = (color & 0x000000ff);
894 ok(red >= 0x7e && red <= 0x81 && green == 0x00 && blue >= 0x7e && blue <= 0x81,
895 "DSTALPHA on texture returned color %08x, expected foo\n", color);
897 out:
898 /* restore things */
899 if(backbuffer) {
900 IDirect3DSurface8_Release(backbuffer);
902 if(offscreenTexture) {
903 IDirect3DTexture8_Release(offscreenTexture);
905 if(offscreen) {
906 IDirect3DSurface8_Release(offscreen);
908 if(depthstencil) {
909 IDirect3DSurface8_Release(depthstencil);
913 static void p8_texture_test(IDirect3DDevice8 *device)
915 IDirect3D8 *d3d = NULL;
916 HRESULT hr;
917 IDirect3DTexture8 *texture = NULL;
918 D3DLOCKED_RECT lr;
919 unsigned char *data;
920 DWORD color, red, green, blue;
921 PALETTEENTRY table[256];
922 D3DCAPS8 caps;
923 UINT i;
924 float quad[] = {
925 -1.0, 0, 0.1, 0.0, 0.0,
926 -1.0, 1.0, 0.1, 0.0, 1.0,
927 1.0, 0, 0.1, 1.0, 0.0,
928 1.0, 1.0, 0.1, 1.0, 1.0,
930 float quad2[] = {
931 -1.0, -1.0, 0.1, 0.0, 0.0,
932 -1.0, 0, 0.1, 0.0, 1.0,
933 1.0, -1.0, 0.1, 1.0, 0.0,
934 1.0, 0, 0.1, 1.0, 1.0,
937 IDirect3DDevice8_GetDirect3D(device, &d3d);
939 if(IDirect3D8_CheckDeviceFormat(d3d, 0, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, 0,
940 D3DRTYPE_TEXTURE, D3DFMT_P8) != D3D_OK) {
941 skip("D3DFMT_P8 textures not supported\n");
942 goto out;
945 hr = IDirect3DDevice8_CreateTexture(device, 1, 1, 1, 0, D3DFMT_P8,
946 D3DPOOL_MANAGED, &texture);
947 ok(hr == D3D_OK, "IDirect3DDevice8_CreateTexture failed, hr = %08x\n", hr);
948 if(!texture) {
949 skip("Failed to create D3DFMT_P8 texture\n");
950 goto out;
953 memset(&lr, 0, sizeof(lr));
954 hr = IDirect3DTexture8_LockRect(texture, 0, &lr, NULL, 0);
955 ok(hr == D3D_OK, "IDirect3DTexture8_LockRect failed, hr = %08x\n", hr);
956 data = lr.pBits;
957 *data = 1;
959 hr = IDirect3DTexture8_UnlockRect(texture, 0);
960 ok(hr == D3D_OK, "IDirect3DTexture8_UnlockRect failed, hr = %08x\n", hr);
962 hr = IDirect3DDevice8_SetTexture(device, 0, (IDirect3DBaseTexture8 *) texture);
963 ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
965 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
966 ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
968 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
969 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
971 /* The first part of the test should work both with and without D3DPTEXTURECAPS_ALPHAPALETTE;
972 alpha of every entry is set to 1.0, which MS says is required when there's no
973 D3DPTEXTURECAPS_ALPHAPALETTE capability */
974 for (i = 0; i < 256; i++) {
975 table[i].peRed = table[i].peGreen = table[i].peBlue = 0;
976 table[i].peFlags = 0xff;
978 table[1].peRed = 0xff;
979 hr = IDirect3DDevice8_SetPaletteEntries(device, 0, table);
980 ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
982 table[1].peRed = 0;
983 table[1].peBlue = 0xff;
984 hr = IDirect3DDevice8_SetPaletteEntries(device, 1, table);
985 ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
987 hr = IDirect3DDevice8_BeginScene(device);
988 ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
989 if(SUCCEEDED(hr)) {
990 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
991 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
992 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
993 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
995 hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
996 ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
998 hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 0);
999 ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1001 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1002 ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1004 hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 1);
1005 ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1007 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, 5 * sizeof(float));
1008 ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1010 hr = IDirect3DDevice8_EndScene(device);
1011 ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1014 hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1015 ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1017 color = getPixelColor(device, 32, 32);
1018 red = (color & 0x00ff0000) >> 16;
1019 green = (color & 0x0000ff00) >> 8;
1020 blue = (color & 0x000000ff) >> 0;
1021 ok(red == 0xff && blue == 0 && green == 0,
1022 "got color %08x, expected 0x00ff0000\n", color);
1024 todo_wine {
1025 color = getPixelColor(device, 32, 320);
1026 red = (color & 0x00ff0000) >> 16;
1027 green = (color & 0x0000ff00) >> 8;
1028 blue = (color & 0x000000ff) >> 0;
1029 ok(red == 0 && blue == 0xff && green == 0,
1030 "got color %08x, expected 0x000000ff\n", color);
1033 /* Test palettes with alpha */
1034 IDirect3DDevice8_GetDeviceCaps(device, &caps);
1035 if (!(caps.TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)) {
1036 skip("no D3DPTEXTURECAPS_ALPHAPALETTE capability, tests with alpha in palette will be skipped\n");
1037 } else {
1038 hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 0.0, 0);
1039 ok(hr == D3D_OK, "IDirect3DDevice8_Clear failed, hr = %08x\n", hr);
1041 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, TRUE);
1042 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1044 for (i = 0; i < 256; i++) {
1045 table[i].peRed = table[i].peGreen = table[i].peBlue = 0;
1046 table[i].peFlags = 0xff;
1048 table[1].peRed = 0xff;
1049 table[1].peFlags = 0x80;
1050 hr = IDirect3DDevice8_SetPaletteEntries(device, 0, table);
1051 ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1053 table[1].peRed = 0;
1054 table[1].peBlue = 0xff;
1055 table[1].peFlags = 0x80;
1056 hr = IDirect3DDevice8_SetPaletteEntries(device, 1, table);
1057 ok(hr == D3D_OK, "IDirect3DDevice8_SetPaletteEntries failed, hr = %08x\n", hr);
1059 hr = IDirect3DDevice8_BeginScene(device);
1060 ok(hr == D3D_OK, "IDirect3DDevice8_BeginScene failed, hr = %08x\n", hr);
1061 if(SUCCEEDED(hr)) {
1062 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
1063 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1064 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
1065 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1067 hr = IDirect3DDevice8_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_TEX1);
1068 ok(hr == D3D_OK, "SetVertexShader failed, hr = %#08x\n", hr);
1070 hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 0);
1071 ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1073 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad, 5 * sizeof(float));
1074 ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1076 hr = IDirect3DDevice8_SetCurrentTexturePalette(device, 1);
1077 ok(hr == D3D_OK, "IDirect3DDevice8_SetCurrentTexturePalette failed, hr = %08x\n", hr);
1079 hr = IDirect3DDevice8_DrawPrimitiveUP(device, D3DPT_TRIANGLESTRIP, 2, quad2, 5 * sizeof(float));
1080 ok(hr == D3D_OK, "IDirect3DDevice8_DrawPrimitiveUP failed, hr = %08x\n", hr);
1082 hr = IDirect3DDevice8_EndScene(device);
1083 ok(hr == D3D_OK, "IDirect3DDevice8_EndScene failed, hr = %08x\n", hr);
1086 hr = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
1087 ok(hr == D3D_OK, "IDirect3DDevice8_Present failed, hr = %08x\n", hr);
1089 todo_wine {
1090 color = getPixelColor(device, 32, 32);
1091 red = (color & 0x00ff0000) >> 16;
1092 green = (color & 0x0000ff00) >> 8;
1093 blue = (color & 0x000000ff) >> 0;
1094 ok(red >= 0x7e && red <= 0x81 && blue == 0 && green == 0,
1095 "got color %08x, expected 0x00800000 or near\n", color);
1097 color = getPixelColor(device, 32, 320);
1098 red = (color & 0x00ff0000) >> 16;
1099 green = (color & 0x0000ff00) >> 8;
1100 blue = (color & 0x000000ff) >> 0;
1101 ok(red == 0 && blue >= 0x7e && blue <= 0x81 && green == 0,
1102 "got color %08x, expected 0x00000080 or near\n", color);
1106 hr = IDirect3DDevice8_SetTexture(device, 0, NULL);
1107 ok(hr == D3D_OK, "IDirect3DDevice8_SetTexture failed, hr = %08x\n", hr);
1108 hr = IDirect3DDevice8_SetRenderState(device, D3DRS_ALPHABLENDENABLE, FALSE);
1109 ok(hr == D3D_OK, "IDirect3DDevice8_SetRenderState failed, hr = %08x\n", hr);
1111 out:
1112 if(texture) IDirect3DTexture8_Release(texture);
1113 IDirect3D8_Release(d3d);
1116 START_TEST(visual)
1118 IDirect3DDevice8 *device_ptr;
1119 HRESULT hr;
1120 DWORD color;
1121 D3DCAPS8 caps;
1123 d3d8_handle = LoadLibraryA("d3d8.dll");
1124 if (!d3d8_handle)
1126 skip("Could not load d3d8.dll\n");
1127 return;
1130 device_ptr = init_d3d8();
1131 if (!device_ptr)
1133 skip("Could not initialize direct3d\n");
1134 return;
1137 IDirect3DDevice8_GetDeviceCaps(device_ptr, &caps);
1139 /* Check for the reliability of the returned data */
1140 hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0, 0);
1141 if(FAILED(hr))
1143 trace("Clear failed, can't assure correctness of the test results, skipping\n");
1144 goto cleanup;
1146 IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
1148 color = getPixelColor(device_ptr, 1, 1);
1149 if(color !=0x00ff0000)
1151 trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
1152 goto cleanup;
1155 hr = IDirect3DDevice8_Clear(device_ptr, 0, NULL, D3DCLEAR_TARGET, 0xff00ddee, 0.0, 0);
1156 if(FAILED(hr))
1158 trace("Clear failed, can't assure correctness of the test results, skipping\n");
1159 goto cleanup;
1161 IDirect3DDevice8_Present(device_ptr, NULL, NULL, NULL, NULL);
1163 color = getPixelColor(device_ptr, 639, 479);
1164 if(color != 0x0000ddee)
1166 trace("Sanity check returned an incorrect color(%08x), can't assure the correctness of the tests, skipping\n", color);
1167 goto cleanup;
1170 /* Now run the real test */
1171 lighting_test(device_ptr);
1172 clear_test(device_ptr);
1173 fog_test(device_ptr);
1174 present_test(device_ptr);
1175 offscreen_test(device_ptr);
1176 alpha_test(device_ptr);
1178 if (caps.VertexShaderVersion >= D3DVS_VERSION(1, 1))
1180 test_rcp_rsq(device_ptr);
1182 else
1184 skip("No vs.1.1 support\n");
1187 p8_texture_test(device_ptr);
1189 cleanup:
1190 if(device_ptr) {
1191 D3DDEVICE_CREATION_PARAMETERS creation_parameters;
1192 IDirect3DDevice8_GetCreationParameters(device_ptr, &creation_parameters);
1193 IDirect3DDevice8_Release(device_ptr);
1194 DestroyWindow(creation_parameters.hFocusWindow);