ddraw/tests: Rewrite SetRenderState() tests.
[wine.git] / dlls / ddraw / tests / ddraw2.c
blobfaf7cefdc84d028409e18b3a3325316a1d5d3865
1 /*
2 * Copyright 2005 Antoine Chavasse (a.chavasse@gmail.com)
3 * Copyright 2008, 2011, 2012-2014 Stefan Dösinger for CodeWeavers
4 * Copyright 2011-2014 Henri Verbeet for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <math.h>
23 #define COBJMACROS
24 #include "wine/test.h"
25 #include <limits.h>
26 #include <math.h>
27 #include "d3d.h"
29 static BOOL is_ddraw64 = sizeof(DWORD) != sizeof(DWORD *);
30 static DEVMODEW registry_mode;
32 static HRESULT (WINAPI *pDwmIsCompositionEnabled)(BOOL *);
34 #ifndef ARRAY_SIZE
35 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
36 #endif
38 struct vec4
40 float x, y, z, w;
43 struct create_window_thread_param
45 HWND window;
46 HANDLE window_created;
47 HANDLE destroy_window;
48 HANDLE thread;
51 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
53 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
54 c1 >>= 8; c2 >>= 8;
55 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
56 c1 >>= 8; c2 >>= 8;
57 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
58 c1 >>= 8; c2 >>= 8;
59 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
60 return TRUE;
63 static BOOL compare_float(float f, float g, unsigned int ulps)
65 int x = *(int *)&f;
66 int y = *(int *)&g;
68 if (x < 0)
69 x = INT_MIN - x;
70 if (y < 0)
71 y = INT_MIN - y;
73 if (abs(x - y) > ulps)
74 return FALSE;
76 return TRUE;
79 static BOOL compare_vec4(const struct vec4 *vec, float x, float y, float z, float w, unsigned int ulps)
81 return compare_float(vec->x, x, ulps)
82 && compare_float(vec->y, y, ulps)
83 && compare_float(vec->z, z, ulps)
84 && compare_float(vec->w, w, ulps);
87 static BOOL ddraw_get_identifier(IDirectDraw2 *ddraw, DDDEVICEIDENTIFIER *identifier)
89 IDirectDraw4 *ddraw4;
90 HRESULT hr;
92 hr = IDirectDraw2_QueryInterface(ddraw, &IID_IDirectDraw4, (void **)&ddraw4);
93 ok(SUCCEEDED(hr), "Failed to get IDirectDraw4 interface, hr %#x.\n", hr);
94 hr = IDirectDraw4_GetDeviceIdentifier(ddraw4, identifier, 0);
95 ok(SUCCEEDED(hr), "Failed to get device identifier, hr %#x.\n", hr);
96 IDirectDraw4_Release(ddraw4);
98 return SUCCEEDED(hr);
101 static BOOL ddraw_is_warp(IDirectDraw2 *ddraw)
103 DDDEVICEIDENTIFIER identifier;
105 return strcmp(winetest_platform, "wine")
106 && ddraw_get_identifier(ddraw, &identifier)
107 && strstr(identifier.szDriver, "warp");
110 static BOOL ddraw_is_vendor(IDirectDraw2 *ddraw, DWORD vendor)
112 DDDEVICEIDENTIFIER identifier;
114 return strcmp(winetest_platform, "wine")
115 && ddraw_get_identifier(ddraw, &identifier)
116 && identifier.dwVendorId == vendor;
119 static BOOL ddraw_is_intel(IDirectDraw2 *ddraw)
121 return ddraw_is_vendor(ddraw, 0x8086);
124 static BOOL ddraw_is_nvidia(IDirectDraw2 *ddraw)
126 return ddraw_is_vendor(ddraw, 0x10de);
129 static BOOL ddraw_is_vmware(IDirectDraw2 *ddraw)
131 return ddraw_is_vendor(ddraw, 0x15ad);
134 static IDirectDrawSurface *create_overlay(IDirectDraw2 *ddraw,
135 unsigned int width, unsigned int height, DWORD format)
137 IDirectDrawSurface *surface;
138 DDSURFACEDESC desc;
140 memset(&desc, 0, sizeof(desc));
141 desc.dwSize = sizeof(desc);
142 desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
143 desc.dwWidth = width;
144 desc.dwHeight = height;
145 desc.ddsCaps.dwCaps = DDSCAPS_OVERLAY;
146 desc.ddpfPixelFormat.dwSize = sizeof(desc.ddpfPixelFormat);
147 desc.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
148 desc.ddpfPixelFormat.dwFourCC = format;
150 if (FAILED(IDirectDraw2_CreateSurface(ddraw, &desc, &surface, NULL)))
151 return NULL;
152 return surface;
155 static HWND create_window(void)
157 RECT r = {0, 0, 640, 480};
159 AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW | WS_VISIBLE, FALSE);
161 return CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
162 CW_USEDEFAULT, CW_USEDEFAULT, r.right - r.left, r.bottom - r.top, NULL, NULL, NULL, NULL);
165 static DWORD WINAPI create_window_thread_proc(void *param)
167 struct create_window_thread_param *p = param;
168 DWORD res;
169 BOOL ret;
171 p->window = create_window();
172 ret = SetEvent(p->window_created);
173 ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
175 for (;;)
177 MSG msg;
179 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
180 DispatchMessageA(&msg);
181 res = WaitForSingleObject(p->destroy_window, 100);
182 if (res == WAIT_OBJECT_0)
183 break;
184 if (res != WAIT_TIMEOUT)
186 ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
187 break;
191 DestroyWindow(p->window);
193 return 0;
196 static void create_window_thread(struct create_window_thread_param *p)
198 DWORD res, tid;
200 p->window_created = CreateEventA(NULL, FALSE, FALSE, NULL);
201 ok(!!p->window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
202 p->destroy_window = CreateEventA(NULL, FALSE, FALSE, NULL);
203 ok(!!p->destroy_window, "CreateEvent failed, last error %#x.\n", GetLastError());
204 p->thread = CreateThread(NULL, 0, create_window_thread_proc, p, 0, &tid);
205 ok(!!p->thread, "Failed to create thread, last error %#x.\n", GetLastError());
206 res = WaitForSingleObject(p->window_created, INFINITE);
207 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
210 static void destroy_window_thread(struct create_window_thread_param *p)
212 SetEvent(p->destroy_window);
213 WaitForSingleObject(p->thread, INFINITE);
214 CloseHandle(p->destroy_window);
215 CloseHandle(p->window_created);
216 CloseHandle(p->thread);
219 static IDirectDrawSurface *get_depth_stencil(IDirect3DDevice2 *device)
221 IDirectDrawSurface *rt, *ret;
222 DDSCAPS caps = {DDSCAPS_ZBUFFER};
223 HRESULT hr;
225 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
226 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
227 hr = IDirectDrawSurface_GetAttachedSurface(rt, &caps, &ret);
228 ok(SUCCEEDED(hr) || hr == DDERR_NOTFOUND, "Failed to get the z buffer, hr %#x.\n", hr);
229 IDirectDrawSurface_Release(rt);
230 return ret;
233 static HRESULT set_display_mode(IDirectDraw2 *ddraw, DWORD width, DWORD height)
235 if (SUCCEEDED(IDirectDraw2_SetDisplayMode(ddraw, width, height, 32, 0, 0)))
236 return DD_OK;
237 return IDirectDraw2_SetDisplayMode(ddraw, width, height, 24, 0, 0);
240 static D3DCOLOR get_surface_color(IDirectDrawSurface *surface, UINT x, UINT y)
242 RECT rect = {x, y, x + 1, y + 1};
243 DDSURFACEDESC surface_desc;
244 D3DCOLOR color;
245 HRESULT hr;
247 memset(&surface_desc, 0, sizeof(surface_desc));
248 surface_desc.dwSize = sizeof(surface_desc);
250 hr = IDirectDrawSurface_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
251 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
252 if (FAILED(hr))
253 return 0xdeadbeef;
255 color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
257 hr = IDirectDrawSurface_Unlock(surface, NULL);
258 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
260 return color;
263 static DWORD get_device_z_depth(IDirect3DDevice2 *device)
265 DDSCAPS caps = {DDSCAPS_ZBUFFER};
266 IDirectDrawSurface *ds, *rt;
267 DDSURFACEDESC desc;
268 HRESULT hr;
270 if (FAILED(IDirect3DDevice2_GetRenderTarget(device, &rt)))
271 return 0;
273 hr = IDirectDrawSurface_GetAttachedSurface(rt, &caps, &ds);
274 IDirectDrawSurface_Release(rt);
275 if (FAILED(hr))
276 return 0;
278 desc.dwSize = sizeof(desc);
279 hr = IDirectDrawSurface_GetSurfaceDesc(ds, &desc);
280 IDirectDrawSurface_Release(ds);
281 if (FAILED(hr))
282 return 0;
284 return U2(desc).dwZBufferBitDepth;
287 static IDirectDraw2 *create_ddraw(void)
289 IDirectDraw2 *ddraw2;
290 IDirectDraw *ddraw1;
291 HRESULT hr;
293 if (FAILED(DirectDrawCreate(NULL, &ddraw1, NULL)))
294 return NULL;
296 hr = IDirectDraw_QueryInterface(ddraw1, &IID_IDirectDraw2, (void **)&ddraw2);
297 IDirectDraw_Release(ddraw1);
298 if (FAILED(hr))
299 return NULL;
301 return ddraw2;
304 static IDirect3DDevice2 *create_device(IDirectDraw2 *ddraw, HWND window, DWORD coop_level)
306 /* Prefer 16 bit depth buffers because Nvidia gives us an unpadded D24 buffer if we ask
307 * for 24 bit and handles such buffers incorrectly in DDBLT_DEPTHFILL. AMD only supports
308 * 16 bit buffers in ddraw1/2. Stencil was added in ddraw4, so we cannot create a D24S8
309 * buffer here. */
310 static const DWORD z_depths[] = {16, 32, 24};
311 IDirectDrawSurface *surface, *ds;
312 IDirect3DDevice2 *device = NULL;
313 DDSURFACEDESC surface_desc;
314 IDirect3D2 *d3d;
315 unsigned int i;
316 HRESULT hr;
318 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, coop_level);
319 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
321 memset(&surface_desc, 0, sizeof(surface_desc));
322 surface_desc.dwSize = sizeof(surface_desc);
323 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
324 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
325 surface_desc.dwWidth = 640;
326 surface_desc.dwHeight = 480;
328 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
329 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
331 if (coop_level & DDSCL_NORMAL)
333 IDirectDrawClipper *clipper;
335 hr = IDirectDraw2_CreateClipper(ddraw, 0, &clipper, NULL);
336 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
337 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
338 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
339 hr = IDirectDrawSurface_SetClipper(surface, clipper);
340 ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
341 IDirectDrawClipper_Release(clipper);
344 hr = IDirectDraw2_QueryInterface(ddraw, &IID_IDirect3D2, (void **)&d3d);
345 if (FAILED(hr))
347 IDirectDrawSurface_Release(surface);
348 return NULL;
351 /* We used to use EnumDevices() for this, but it seems
352 * D3DDEVICEDESC.dwDeviceZBufferBitDepth only has a very casual
353 * relationship with reality. */
354 for (i = 0; i < ARRAY_SIZE(z_depths); ++i)
356 memset(&surface_desc, 0, sizeof(surface_desc));
357 surface_desc.dwSize = sizeof(surface_desc);
358 surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
359 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
360 U2(surface_desc).dwZBufferBitDepth = z_depths[i];
361 surface_desc.dwWidth = 640;
362 surface_desc.dwHeight = 480;
363 if (FAILED(IDirectDraw2_CreateSurface(ddraw, &surface_desc, &ds, NULL)))
364 continue;
366 hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
367 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
368 IDirectDrawSurface_Release(ds);
369 if (FAILED(hr))
370 continue;
372 if (SUCCEEDED(IDirect3D2_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device)))
373 break;
375 IDirectDrawSurface_DeleteAttachedSurface(surface, 0, ds);
378 IDirect3D2_Release(d3d);
379 IDirectDrawSurface_Release(surface);
380 return device;
383 static IDirect3DViewport2 *create_viewport(IDirect3DDevice2 *device, UINT x, UINT y, UINT w, UINT h)
385 IDirect3DViewport2 *viewport;
386 D3DVIEWPORT2 vp;
387 IDirect3D2 *d3d;
388 HRESULT hr;
390 hr = IDirect3DDevice2_GetDirect3D(device, &d3d);
391 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
392 hr = IDirect3D2_CreateViewport(d3d, &viewport, NULL);
393 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
394 hr = IDirect3DDevice2_AddViewport(device, viewport);
395 ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
396 memset(&vp, 0, sizeof(vp));
397 vp.dwSize = sizeof(vp);
398 vp.dwX = x;
399 vp.dwY = y;
400 vp.dwWidth = w;
401 vp.dwHeight = h;
402 vp.dvClipX = -1.0f;
403 vp.dvClipY = 1.0f;
404 vp.dvClipWidth = 2.0f;
405 vp.dvClipHeight = 2.0f;
406 vp.dvMinZ = 0.0f;
407 vp.dvMaxZ = 1.0f;
408 hr = IDirect3DViewport2_SetViewport2(viewport, &vp);
409 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
410 IDirect3D2_Release(d3d);
412 return viewport;
415 static void viewport_set_background(IDirect3DDevice2 *device, IDirect3DViewport2 *viewport,
416 IDirect3DMaterial2 *material)
418 D3DMATERIALHANDLE material_handle;
419 HRESULT hr;
421 hr = IDirect3DMaterial2_GetHandle(material, device, &material_handle);
422 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
423 hr = IDirect3DViewport2_SetBackground(viewport, material_handle);
424 ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
427 static void destroy_viewport(IDirect3DDevice2 *device, IDirect3DViewport2 *viewport)
429 HRESULT hr;
431 hr = IDirect3DDevice2_DeleteViewport(device, viewport);
432 ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
433 IDirect3DViewport2_Release(viewport);
436 static IDirect3DMaterial2 *create_material(IDirect3DDevice2 *device, D3DMATERIAL *mat)
438 IDirect3DMaterial2 *material;
439 IDirect3D2 *d3d;
440 HRESULT hr;
442 hr = IDirect3DDevice2_GetDirect3D(device, &d3d);
443 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
444 hr = IDirect3D2_CreateMaterial(d3d, &material, NULL);
445 ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
446 hr = IDirect3DMaterial2_SetMaterial(material, mat);
447 ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
448 IDirect3D2_Release(d3d);
450 return material;
453 static IDirect3DMaterial2 *create_diffuse_material(IDirect3DDevice2 *device, float r, float g, float b, float a)
455 D3DMATERIAL mat;
457 memset(&mat, 0, sizeof(mat));
458 mat.dwSize = sizeof(mat);
459 U1(U(mat).diffuse).r = r;
460 U2(U(mat).diffuse).g = g;
461 U3(U(mat).diffuse).b = b;
462 U4(U(mat).diffuse).a = a;
464 return create_material(device, &mat);
467 static IDirect3DMaterial2 *create_specular_material(IDirect3DDevice2 *device,
468 float r, float g, float b, float a, float power)
470 D3DMATERIAL mat;
472 memset(&mat, 0, sizeof(mat));
473 mat.dwSize = sizeof(mat);
474 U1(U2(mat).specular).r = r;
475 U2(U2(mat).specular).g = g;
476 U3(U2(mat).specular).b = b;
477 U4(U2(mat).specular).a = a;
478 U4(mat).power = power;
480 return create_material(device, &mat);
483 static IDirect3DMaterial2 *create_emissive_material(IDirect3DDevice2 *device, float r, float g, float b, float a)
485 D3DMATERIAL mat;
487 memset(&mat, 0, sizeof(mat));
488 mat.dwSize = sizeof(mat);
489 U1(U3(mat).emissive).r = r;
490 U2(U3(mat).emissive).g = g;
491 U3(U3(mat).emissive).b = b;
492 U4(U3(mat).emissive).a = a;
494 return create_material(device, &mat);
497 static void destroy_material(IDirect3DMaterial2 *material)
499 IDirect3DMaterial2_Release(material);
502 struct message
504 UINT message;
505 BOOL check_wparam;
506 WPARAM expect_wparam;
509 static const struct message *expect_messages;
511 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
513 if (expect_messages && message == expect_messages->message)
515 if (expect_messages->check_wparam)
516 ok (wparam == expect_messages->expect_wparam,
517 "Got unexpected wparam %lx for message %x, expected %lx.\n",
518 wparam, message, expect_messages->expect_wparam);
520 ++expect_messages;
523 return DefWindowProcA(hwnd, message, wparam, lparam);
526 /* Set the wndproc back to what ddraw expects it to be, and release the ddraw
527 * interface. This prevents subsequent SetCooperativeLevel() calls on a
528 * different window from failing with DDERR_HWNDALREADYSET. */
529 static void fix_wndproc(HWND window, LONG_PTR proc)
531 IDirectDraw2 *ddraw;
532 HRESULT hr;
534 if (!(ddraw = create_ddraw()))
535 return;
537 SetWindowLongPtrA(window, GWLP_WNDPROC, proc);
538 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
539 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
540 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
541 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
543 IDirectDraw2_Release(ddraw);
546 static HRESULT CALLBACK restore_callback(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
548 HRESULT hr = IDirectDrawSurface_Restore(surface);
549 ok(SUCCEEDED(hr) || hr == DDERR_IMPLICITLYCREATED, "Failed to restore surface, hr %#x.\n", hr);
550 IDirectDrawSurface_Release(surface);
552 return DDENUMRET_OK;
555 static HRESULT restore_surfaces(IDirectDraw2 *ddraw)
557 return IDirectDraw2_EnumSurfaces(ddraw, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
558 NULL, NULL, restore_callback);
561 static void test_coop_level_create_device_window(void)
563 HWND focus_window, device_window;
564 IDirectDraw2 *ddraw;
565 HRESULT hr;
567 focus_window = create_window();
568 ddraw = create_ddraw();
569 ok(!!ddraw, "Failed to create a ddraw object.\n");
571 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
572 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
573 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
574 ok(!device_window, "Unexpected device window found.\n");
575 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
576 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
577 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
578 ok(!device_window, "Unexpected device window found.\n");
579 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
580 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
581 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
582 ok(!device_window, "Unexpected device window found.\n");
583 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
584 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
585 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
586 ok(!device_window, "Unexpected device window found.\n");
587 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
588 ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
589 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
590 ok(!device_window, "Unexpected device window found.\n");
592 /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
593 if (broken(hr == DDERR_INVALIDPARAMS))
595 win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
596 IDirectDraw2_Release(ddraw);
597 DestroyWindow(focus_window);
598 return;
601 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
602 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
603 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
604 ok(!device_window, "Unexpected device window found.\n");
605 hr = IDirectDraw2_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
606 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
607 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
608 ok(!device_window, "Unexpected device window found.\n");
610 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
611 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
612 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
613 ok(!device_window, "Unexpected device window found.\n");
614 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
615 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
616 ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
617 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
618 ok(!!device_window, "Device window not found.\n");
620 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
621 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
622 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
623 ok(!device_window, "Unexpected device window found.\n");
624 hr = IDirectDraw2_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
625 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
626 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
627 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
628 ok(!!device_window, "Device window not found.\n");
630 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
631 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
632 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
633 ok(!device_window, "Unexpected device window found.\n");
634 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
635 ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
636 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
637 ok(!device_window, "Unexpected device window found.\n");
638 hr = IDirectDraw2_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
639 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
640 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
641 ok(!device_window, "Unexpected device window found.\n");
642 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
643 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
644 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
645 ok(!!device_window, "Device window not found.\n");
647 IDirectDraw2_Release(ddraw);
648 DestroyWindow(focus_window);
651 static void test_clipper_blt(void)
653 IDirectDrawSurface *src_surface, *dst_surface;
654 RECT client_rect, src_rect;
655 IDirectDrawClipper *clipper;
656 DDSURFACEDESC surface_desc;
657 unsigned int i, j, x, y;
658 IDirectDraw2 *ddraw;
659 RGNDATA *rgn_data;
660 D3DCOLOR color;
661 ULONG refcount;
662 HRGN r1, r2;
663 HWND window;
664 DDBLTFX fx;
665 HRESULT hr;
666 DWORD *ptr;
667 DWORD ret;
669 static const DWORD src_data[] =
671 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
672 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
673 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
675 static const D3DCOLOR expected1[] =
677 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
678 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
679 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
680 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
682 /* Nvidia on Windows seems to have an off-by-one error
683 * when processing source rectangles. Our left = 1 and
684 * right = 5 input reads from x = {1, 2, 3}. x = 4 is
685 * read as well, but only for the edge pixels on the
686 * output image. The bug happens on the y axis as well,
687 * but we only read one row there, and all source rows
688 * contain the same data. This bug is not dependent on
689 * the presence of a clipper. */
690 static const D3DCOLOR expected1_broken[] =
692 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
693 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
694 0x00000000, 0x00000000, 0x00ff0000, 0x00ff0000,
695 0x00000000, 0x00000000, 0x0000ff00, 0x00ff0000,
697 static const D3DCOLOR expected2[] =
699 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
700 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
701 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
702 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
705 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
706 10, 10, 640, 480, 0, 0, 0, 0);
707 ShowWindow(window, SW_SHOW);
708 ddraw = create_ddraw();
709 ok(!!ddraw, "Failed to create a ddraw object.\n");
711 ret = GetClientRect(window, &client_rect);
712 ok(ret, "Failed to get client rect.\n");
713 ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
714 ok(ret, "Failed to map client rect.\n");
716 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
717 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
719 hr = IDirectDraw2_CreateClipper(ddraw, 0, &clipper, NULL);
720 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
721 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
722 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
723 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
724 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
725 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
726 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
727 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
728 hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
729 ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
730 ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
731 ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
732 ok(rgn_data->rdh.nCount >= 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
733 ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
734 "Got unexpected bounding rect %s, expected %s.\n",
735 wine_dbgstr_rect(&rgn_data->rdh.rcBound), wine_dbgstr_rect(&client_rect));
736 HeapFree(GetProcessHeap(), 0, rgn_data);
738 r1 = CreateRectRgn(0, 0, 320, 240);
739 ok(!!r1, "Failed to create region.\n");
740 r2 = CreateRectRgn(320, 240, 640, 480);
741 ok(!!r2, "Failed to create region.\n");
742 CombineRgn(r1, r1, r2, RGN_OR);
743 ret = GetRegionData(r1, 0, NULL);
744 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
745 ret = GetRegionData(r1, ret, rgn_data);
746 ok(!!ret, "Failed to get region data.\n");
748 DeleteObject(r2);
749 DeleteObject(r1);
751 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
752 ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
753 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
754 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
755 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
756 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
758 HeapFree(GetProcessHeap(), 0, rgn_data);
760 memset(&surface_desc, 0, sizeof(surface_desc));
761 surface_desc.dwSize = sizeof(surface_desc);
762 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
763 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
764 surface_desc.dwWidth = 640;
765 surface_desc.dwHeight = 480;
766 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
767 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
768 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
769 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
770 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
771 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
773 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
774 ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
775 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
776 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
778 memset(&fx, 0, sizeof(fx));
779 fx.dwSize = sizeof(fx);
780 hr = IDirectDrawSurface_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
781 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
782 hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
783 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
785 hr = IDirectDrawSurface_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
786 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
787 ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
788 ptr = surface_desc.lpSurface;
789 memcpy(&ptr[ 0], &src_data[ 0], 6 * sizeof(DWORD));
790 memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
791 memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
792 hr = IDirectDrawSurface_Unlock(src_surface, NULL);
793 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
795 hr = IDirectDrawSurface_SetClipper(dst_surface, clipper);
796 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
798 SetRect(&src_rect, 1, 1, 5, 2);
799 hr = IDirectDrawSurface_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
800 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
801 for (i = 0; i < 4; ++i)
803 for (j = 0; j < 4; ++j)
805 x = 80 * ((2 * j) + 1);
806 y = 60 * ((2 * i) + 1);
807 color = get_surface_color(dst_surface, x, y);
808 ok(compare_color(color, expected1[i * 4 + j], 1)
809 || broken(compare_color(color, expected1_broken[i * 4 + j], 1)),
810 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
814 U5(fx).dwFillColor = 0xff0000ff;
815 hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
816 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
817 for (i = 0; i < 4; ++i)
819 for (j = 0; j < 4; ++j)
821 x = 80 * ((2 * j) + 1);
822 y = 60 * ((2 * i) + 1);
823 color = get_surface_color(dst_surface, x, y);
824 ok(compare_color(color, expected2[i * 4 + j], 1),
825 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
829 hr = IDirectDrawSurface_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
830 ok(hr == DDERR_BLTFASTCANTCLIP || broken(hr == E_NOTIMPL /* NT4 */), "Got unexpected hr %#x.\n", hr);
832 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
833 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
834 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
835 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
836 DestroyWindow(window);
837 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
838 ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
839 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
840 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
841 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
842 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
843 hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
844 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
845 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
846 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
847 hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
848 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
850 IDirectDrawSurface_Release(dst_surface);
851 IDirectDrawSurface_Release(src_surface);
852 refcount = IDirectDrawClipper_Release(clipper);
853 ok(!refcount, "Clipper has %u references left.\n", refcount);
854 IDirectDraw2_Release(ddraw);
857 static void test_coop_level_d3d_state(void)
859 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
860 IDirectDrawSurface *rt, *surface;
861 IDirect3DMaterial2 *background;
862 IDirect3DViewport2 *viewport;
863 IDirect3DDevice2 *device;
864 D3DMATERIAL material;
865 IDirectDraw2 *ddraw;
866 D3DCOLOR color;
867 DWORD value;
868 HWND window;
869 HRESULT hr;
871 window = create_window();
872 ddraw = create_ddraw();
873 ok(!!ddraw, "Failed to create a ddraw object.\n");
874 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
876 skip("Failed to create a 3D device, skipping test.\n");
877 IDirectDraw2_Release(ddraw);
878 DestroyWindow(window);
879 return;
882 background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
883 viewport = create_viewport(device, 0, 0, 640, 480);
884 viewport_set_background(device, viewport, background);
886 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
887 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
888 hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
889 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
890 ok(!!value, "Got unexpected z-enable state %#x.\n", value);
891 hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
892 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
893 ok(!value, "Got unexpected alpha blend enable state %#x.\n", value);
894 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
895 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
896 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
897 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
898 color = get_surface_color(rt, 320, 240);
899 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
901 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
902 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
903 hr = IDirectDrawSurface_IsLost(rt);
904 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
905 hr = restore_surfaces(ddraw);
906 ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
908 memset(&material, 0, sizeof(material));
909 material.dwSize = sizeof(material);
910 U1(U(material).diffuse).r = 0.0f;
911 U2(U(material).diffuse).g = 1.0f;
912 U3(U(material).diffuse).b = 0.0f;
913 U4(U(material).diffuse).a = 1.0f;
914 hr = IDirect3DMaterial2_SetMaterial(background, &material);
915 ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
917 hr = IDirect3DDevice2_GetRenderTarget(device, &surface);
918 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
919 ok(surface == rt, "Got unexpected surface %p.\n", surface);
920 hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
921 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
922 ok(!!value, "Got unexpected z-enable state %#x.\n", value);
923 hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
924 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
925 ok(!!value, "Got unexpected alpha blend enable state %#x.\n", value);
926 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
927 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
928 color = get_surface_color(rt, 320, 240);
929 ok(compare_color(color, 0x0000ff00, 1) || broken(compare_color(color, 0x00000000, 1)),
930 "Got unexpected color 0x%08x.\n", color);
932 destroy_viewport(device, viewport);
933 destroy_material(background);
934 IDirectDrawSurface_Release(surface);
935 IDirectDrawSurface_Release(rt);
936 IDirect3DDevice2_Release(device);
937 IDirectDraw2_Release(ddraw);
938 DestroyWindow(window);
941 static void test_surface_interface_mismatch(void)
943 IDirectDraw2 *ddraw = NULL;
944 IDirect3D2 *d3d = NULL;
945 IDirectDrawSurface *surface = NULL, *ds;
946 IDirectDrawSurface3 *surface3 = NULL;
947 IDirect3DDevice2 *device = NULL;
948 IDirect3DViewport2 *viewport = NULL;
949 IDirect3DMaterial2 *background = NULL;
950 DDSURFACEDESC surface_desc;
951 DWORD z_depth = 0;
952 ULONG refcount;
953 HRESULT hr;
954 D3DCOLOR color;
955 HWND window;
956 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
958 window = create_window();
959 ddraw = create_ddraw();
960 ok(!!ddraw, "Failed to create a ddraw object.\n");
961 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
963 skip("Failed to create a 3D device, skipping test.\n");
964 IDirectDraw2_Release(ddraw);
965 DestroyWindow(window);
966 return;
968 z_depth = get_device_z_depth(device);
969 ok(!!z_depth, "Failed to get device z depth.\n");
970 IDirect3DDevice2_Release(device);
971 device = NULL;
973 memset(&surface_desc, 0, sizeof(surface_desc));
974 surface_desc.dwSize = sizeof(surface_desc);
975 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
976 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
977 surface_desc.dwWidth = 640;
978 surface_desc.dwHeight = 480;
980 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
981 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
983 hr = IDirectDrawSurface2_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
984 if (FAILED(hr))
986 skip("Failed to get the IDirectDrawSurface3 interface, skipping test.\n");
987 goto cleanup;
990 if (FAILED(IDirectDraw2_QueryInterface(ddraw, &IID_IDirect3D2, (void **)&d3d)))
992 skip("D3D interface is not available, skipping test.\n");
993 goto cleanup;
996 memset(&surface_desc, 0, sizeof(surface_desc));
997 surface_desc.dwSize = sizeof(surface_desc);
998 surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
999 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
1000 U2(surface_desc).dwZBufferBitDepth = z_depth;
1001 surface_desc.dwWidth = 640;
1002 surface_desc.dwHeight = 480;
1003 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &ds, NULL);
1004 ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
1005 if (FAILED(hr))
1006 goto cleanup;
1008 /* Using a different surface interface version still works */
1009 hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
1010 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
1011 refcount = IDirectDrawSurface_Release(ds);
1012 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
1013 if (FAILED(hr))
1014 goto cleanup;
1016 /* Here too */
1017 hr = IDirect3D2_CreateDevice(d3d, &IID_IDirect3DHALDevice, (IDirectDrawSurface *)surface3, &device);
1018 ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
1019 if (FAILED(hr))
1020 goto cleanup;
1022 background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
1023 viewport = create_viewport(device, 0, 0, 640, 480);
1024 viewport_set_background(device, viewport, background);
1026 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1027 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
1028 color = get_surface_color(surface, 320, 240);
1029 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
1031 cleanup:
1032 if (viewport)
1033 destroy_viewport(device, viewport);
1034 if (background)
1035 destroy_material(background);
1036 if (surface3) IDirectDrawSurface3_Release(surface3);
1037 if (surface) IDirectDrawSurface_Release(surface);
1038 if (device) IDirect3DDevice2_Release(device);
1039 if (d3d) IDirect3D2_Release(d3d);
1040 if (ddraw) IDirectDraw2_Release(ddraw);
1041 DestroyWindow(window);
1044 static void test_coop_level_threaded(void)
1046 struct create_window_thread_param p;
1047 IDirectDraw2 *ddraw;
1048 HRESULT hr;
1050 ddraw = create_ddraw();
1051 ok(!!ddraw, "Failed to create a ddraw object.\n");
1052 create_window_thread(&p);
1054 hr = IDirectDraw2_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1055 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1057 IDirectDraw2_Release(ddraw);
1058 destroy_window_thread(&p);
1061 static void test_depth_blit(void)
1063 static D3DLVERTEX quad1[] =
1065 {{-1.0}, { 1.0}, {0.50f}, 0, {0xff00ff00}},
1066 {{ 1.0}, { 1.0}, {0.50f}, 0, {0xff00ff00}},
1067 {{-1.0}, {-1.0}, {0.50f}, 0, {0xff00ff00}},
1068 {{ 1.0}, {-1.0}, {0.50f}, 0, {0xff00ff00}},
1070 static const D3DCOLOR expected_colors[4][4] =
1072 {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1073 {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1074 {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1075 {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1077 DDSURFACEDESC ddsd_new, ddsd_existing;
1079 IDirect3DDevice2 *device;
1080 IDirectDrawSurface *ds1, *ds2, *ds3, *rt;
1081 IDirect3DViewport2 *viewport;
1082 RECT src_rect, dst_rect;
1083 unsigned int i, j;
1084 D3DCOLOR color;
1085 HRESULT hr;
1086 IDirectDraw2 *ddraw;
1087 DDBLTFX fx;
1088 HWND window;
1089 D3DRECT d3drect;
1090 IDirect3DMaterial2 *background;
1092 window = create_window();
1093 ddraw = create_ddraw();
1094 ok(!!ddraw, "Failed to create a ddraw object.\n");
1095 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1097 skip("Failed to create a 3D device, skipping test.\n");
1098 IDirectDraw2_Release(ddraw);
1099 DestroyWindow(window);
1100 return;
1103 ds1 = get_depth_stencil(device);
1105 memset(&ddsd_new, 0, sizeof(ddsd_new));
1106 ddsd_new.dwSize = sizeof(ddsd_new);
1107 memset(&ddsd_existing, 0, sizeof(ddsd_existing));
1108 ddsd_existing.dwSize = sizeof(ddsd_existing);
1109 hr = IDirectDrawSurface_GetSurfaceDesc(ds1, &ddsd_existing);
1110 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
1111 ddsd_new.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
1112 ddsd_new.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
1113 ddsd_new.dwWidth = ddsd_existing.dwWidth;
1114 ddsd_new.dwHeight = ddsd_existing.dwHeight;
1115 ddsd_new.ddpfPixelFormat = ddsd_existing.ddpfPixelFormat;
1116 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd_new, &ds2, NULL);
1117 ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
1118 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd_new, &ds3, NULL);
1119 ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
1121 background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
1122 viewport = create_viewport(device, 0, 0, ddsd_existing.dwWidth, ddsd_existing.dwHeight);
1123 viewport_set_background(device, viewport, background);
1124 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
1125 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
1127 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
1128 ok(SUCCEEDED(hr), "Failed to enable z testing, hr %#x.\n", hr);
1129 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL);
1130 ok(SUCCEEDED(hr), "Failed to set the z function, hr %#x.\n", hr);
1132 U1(d3drect).x1 = U2(d3drect).y1 = 0;
1133 U3(d3drect).x2 = ddsd_existing.dwWidth; U4(d3drect).y2 = ddsd_existing.dwHeight;
1134 hr = IDirect3DViewport2_Clear(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER);
1135 ok(SUCCEEDED(hr), "Failed to clear the z buffer, hr %#x.\n", hr);
1137 /* Partial blit. */
1138 SetRect(&src_rect, 0, 0, 320, 240);
1139 SetRect(&dst_rect, 0, 0, 320, 240);
1140 hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1141 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1142 /* Different locations. */
1143 SetRect(&src_rect, 0, 0, 320, 240);
1144 SetRect(&dst_rect, 320, 240, 640, 480);
1145 hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1146 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1147 /* Stretched. */
1148 SetRect(&src_rect, 0, 0, 320, 240);
1149 SetRect(&dst_rect, 0, 0, 640, 480);
1150 hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1151 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1152 /* Flipped. */
1153 SetRect(&src_rect, 0, 480, 640, 0);
1154 SetRect(&dst_rect, 0, 0, 640, 480);
1155 hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1156 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1157 SetRect(&src_rect, 0, 0, 640, 480);
1158 SetRect(&dst_rect, 0, 480, 640, 0);
1159 hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1160 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1161 /* Full, explicit. */
1162 SetRect(&src_rect, 0, 0, 640, 480);
1163 SetRect(&dst_rect, 0, 0, 640, 480);
1164 hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1165 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1166 /* Depth -> color blit: Succeeds on Win7 + Radeon HD 5700, fails on WinXP + Radeon X1600 */
1168 /* Depth blit inside a BeginScene / EndScene pair */
1169 hr = IDirect3DDevice2_BeginScene(device);
1170 ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1171 /* From the current depth stencil */
1172 hr = IDirectDrawSurface_Blt(ds2, NULL, ds1, NULL, DDBLT_WAIT, NULL);
1173 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1174 /* To the current depth stencil */
1175 hr = IDirectDrawSurface_Blt(ds1, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1176 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1177 /* Between unbound surfaces */
1178 hr = IDirectDrawSurface_Blt(ds3, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1179 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1180 hr = IDirect3DDevice2_EndScene(device);
1181 ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1183 /* Avoid changing the depth stencil, it doesn't work properly on Windows.
1184 * Instead use DDBLT_DEPTHFILL to clear the depth stencil. Unfortunately
1185 * drivers disagree on the meaning of dwFillDepth. Only 0 seems to produce
1186 * a reliable result(z = 0.0) */
1187 memset(&fx, 0, sizeof(fx));
1188 fx.dwSize = sizeof(fx);
1189 U5(fx).dwFillDepth = 0;
1190 hr = IDirectDrawSurface_Blt(ds2, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
1191 ok(SUCCEEDED(hr), "Failed to clear the source z buffer, hr %#x.\n", hr);
1193 /* This clears the Z buffer with 1.0 */
1194 hr = IDirect3DViewport2_Clear(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET);
1195 ok(SUCCEEDED(hr), "Failed to clear the color and z buffers, hr %#x.\n", hr);
1197 SetRect(&dst_rect, 0, 0, 320, 240);
1198 hr = IDirectDrawSurface_Blt(ds1, &dst_rect, ds2, NULL, DDBLT_WAIT, NULL);
1199 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1200 IDirectDrawSurface_Release(ds3);
1201 IDirectDrawSurface_Release(ds2);
1202 IDirectDrawSurface_Release(ds1);
1204 hr = IDirect3DDevice2_BeginScene(device);
1205 ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1206 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, quad1, 4, 0);
1207 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1208 hr = IDirect3DDevice2_EndScene(device);
1209 ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1211 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
1212 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1213 for (i = 0; i < 4; ++i)
1215 for (j = 0; j < 4; ++j)
1217 unsigned int x = 80 * ((2 * j) + 1);
1218 unsigned int y = 60 * ((2 * i) + 1);
1219 color = get_surface_color(rt, x, y);
1220 ok(compare_color(color, expected_colors[i][j], 1),
1221 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected_colors[i][j], x, y, color);
1224 IDirectDrawSurface_Release(rt);
1226 destroy_viewport(device, viewport);
1227 destroy_material(background);
1228 IDirect3DDevice2_Release(device);
1229 IDirectDraw2_Release(ddraw);
1230 DestroyWindow(window);
1233 static void test_texture_load_ckey(void)
1235 IDirectDraw2 *ddraw = NULL;
1236 IDirectDrawSurface *src = NULL;
1237 IDirectDrawSurface *dst = NULL;
1238 IDirect3DTexture *src_tex = NULL;
1239 IDirect3DTexture *dst_tex = NULL;
1240 DDSURFACEDESC ddsd;
1241 HRESULT hr;
1242 DDCOLORKEY ckey;
1244 ddraw = create_ddraw();
1245 ok(!!ddraw, "Failed to create a ddraw object.\n");
1246 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
1247 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1249 memset(&ddsd, 0, sizeof(ddsd));
1250 ddsd.dwSize = sizeof(ddsd);
1251 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
1252 ddsd.dwHeight = 128;
1253 ddsd.dwWidth = 128;
1254 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
1255 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &src, NULL);
1256 ok(SUCCEEDED(hr), "Failed to create source texture, hr %#x.\n", hr);
1257 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1258 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &dst, NULL);
1259 ok(SUCCEEDED(hr), "Failed to create destination texture, hr %#x.\n", hr);
1261 hr = IDirectDrawSurface_QueryInterface(src, &IID_IDirect3DTexture, (void **)&src_tex);
1262 ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get Direct3DTexture interface, hr %#x.\n", hr);
1263 if (FAILED(hr))
1265 /* 64 bit ddraw does not support d3d */
1266 skip("Could not get Direct3DTexture interface, skipping texture::Load color keying tests.\n");
1267 goto done;
1269 hr = IDirectDrawSurface_QueryInterface(dst, &IID_IDirect3DTexture, (void **)&dst_tex);
1270 ok(SUCCEEDED(hr), "Failed to get Direct3DTexture interface, hr %#x.\n", hr);
1272 /* No surface has a color key */
1273 hr = IDirect3DTexture_Load(dst_tex, src_tex);
1274 ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDCAPS), "Got unexpected hr %#x.\n", hr);
1275 if (FAILED(hr))
1277 /* Testbot Windows NT VMs */
1278 skip("IDirect3DTexture::Load does not work, skipping color keying tests.\n");
1279 goto done;
1282 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0xdeadbeef;
1283 hr = IDirectDrawSurface_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1284 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1285 ok(ckey.dwColorSpaceLowValue == 0xdeadbeef, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1286 ok(ckey.dwColorSpaceHighValue == 0xdeadbeef, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1288 /* Source surface has a color key */
1289 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x0000ff00;
1290 hr = IDirectDrawSurface_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
1291 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1292 hr = IDirect3DTexture_Load(dst_tex, src_tex);
1293 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1294 hr = IDirectDrawSurface_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1295 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1296 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1297 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1299 /* Both surfaces have a color key: Dest ckey is overwritten */
1300 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x000000ff;
1301 hr = IDirectDrawSurface_SetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1302 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1303 hr = IDirect3DTexture_Load(dst_tex, src_tex);
1304 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1305 hr = IDirectDrawSurface_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1306 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1307 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1308 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1310 /* Only the destination has a color key: It is not deleted */
1311 hr = IDirectDrawSurface_SetColorKey(src, DDCKEY_SRCBLT, NULL);
1312 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1313 hr = IDirectDrawSurface_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
1314 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1315 hr = IDirect3DTexture_Load(dst_tex, src_tex);
1316 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1317 hr = IDirectDrawSurface_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1318 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1319 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1320 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1322 done:
1323 if (dst_tex) IDirect3DTexture_Release(dst_tex);
1324 if (src_tex) IDirect3DTexture_Release(src_tex);
1325 if (dst) IDirectDrawSurface_Release(dst);
1326 if (src) IDirectDrawSurface_Release(src);
1327 if (ddraw) IDirectDraw2_Release(ddraw);
1330 static ULONG get_refcount(IUnknown *test_iface)
1332 IUnknown_AddRef(test_iface);
1333 return IUnknown_Release(test_iface);
1336 static void test_viewport(void)
1338 IDirectDraw2 *ddraw;
1339 IDirect3D2 *d3d;
1340 HRESULT hr;
1341 ULONG ref, old_d3d_ref;
1342 IDirect3DViewport *viewport;
1343 IDirect3DViewport2 *viewport2, *another_vp, *test_vp;
1344 IDirect3DViewport3 *viewport3;
1345 IDirectDrawGammaControl *gamma;
1346 IUnknown *unknown;
1347 IDirect3DDevice2 *device;
1348 HWND window;
1350 window = create_window();
1351 ddraw = create_ddraw();
1352 ok(!!ddraw, "Failed to create a ddraw object.\n");
1353 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1355 skip("Failed to create a 3D device, skipping test.\n");
1356 IDirectDraw_Release(ddraw);
1357 DestroyWindow(window);
1358 return;
1361 hr = IDirectDraw2_QueryInterface(ddraw, &IID_IDirect3D2, (void **)&d3d);
1362 ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get d3d interface, hr %#x.\n", hr);
1363 if (FAILED(hr))
1365 skip("D3D interface is not available, skipping test.\n");
1366 IDirectDraw2_Release(ddraw);
1367 return;
1369 old_d3d_ref = get_refcount((IUnknown *)d3d);
1371 hr = IDirect3D2_CreateViewport(d3d, &viewport2, NULL);
1372 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1373 ref = get_refcount((IUnknown *)viewport2);
1374 ok(ref == 1, "Initial IDirect3DViewport2 refcount is %u\n", ref);
1375 ref = get_refcount((IUnknown *)d3d);
1376 ok(ref == old_d3d_ref, "IDirect3D2 refcount is %u\n", ref);
1378 gamma = (IDirectDrawGammaControl *)0xdeadbeef;
1379 hr = IDirect3DViewport2_QueryInterface(viewport2, &IID_IDirectDrawGammaControl, (void **)&gamma);
1380 ok(hr == E_NOINTERFACE, "Got unexpected hr %#x.\n", hr);
1381 ok(gamma == NULL, "Interface not set to NULL by failed QI call: %p\n", gamma);
1382 if (SUCCEEDED(hr)) IDirectDrawGammaControl_Release(gamma);
1383 /* NULL iid: Segfaults */
1385 hr = IDirect3DViewport2_QueryInterface(viewport2, &IID_IDirect3DViewport, (void **)&viewport);
1386 ok(SUCCEEDED(hr), "Failed to QI IDirect3DViewport, hr %#x.\n", hr);
1387 if (viewport)
1389 ref = get_refcount((IUnknown *)viewport);
1390 ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1391 ref = get_refcount((IUnknown *)viewport2);
1392 ok(ref == 2, "IDirect3DViewport2 refcount is %u\n", ref);
1393 IDirect3DViewport_Release(viewport);
1394 viewport = NULL;
1397 hr = IDirect3DViewport2_QueryInterface(viewport2, &IID_IDirect3DViewport3, (void **)&viewport3);
1398 ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to QI IDirect3DViewport3, hr %#x.\n", hr);
1399 if (viewport3)
1401 ref = get_refcount((IUnknown *)viewport2);
1402 ok(ref == 2, "IDirect3DViewport2 refcount is %u\n", ref);
1403 ref = get_refcount((IUnknown *)viewport3);
1404 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1405 IDirect3DViewport3_Release(viewport3);
1408 hr = IDirect3DViewport2_QueryInterface(viewport2, &IID_IUnknown, (void **)&unknown);
1409 ok(SUCCEEDED(hr), "Failed to QI IUnknown, hr %#x.\n", hr);
1410 if (unknown)
1412 ref = get_refcount((IUnknown *)viewport2);
1413 ok(ref == 2, "IDirect3DViewport2 refcount is %u\n", ref);
1414 ref = get_refcount(unknown);
1415 ok(ref == 2, "IUnknown refcount is %u\n", ref);
1416 IUnknown_Release(unknown);
1419 /* AddViewport(NULL): Segfault */
1420 hr = IDirect3DDevice2_DeleteViewport(device, NULL);
1421 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
1422 hr = IDirect3DDevice2_GetCurrentViewport(device, NULL);
1423 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1425 hr = IDirect3D2_CreateViewport(d3d, &another_vp, NULL);
1426 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1428 /* Setting a viewport not in the viewport list fails */
1429 hr = IDirect3DDevice2_SetCurrentViewport(device, another_vp);
1430 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
1432 hr = IDirect3DDevice2_AddViewport(device, viewport2);
1433 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1434 ref = get_refcount((IUnknown *) viewport2);
1435 ok(ref == 2, "viewport2 refcount is %d\n", ref);
1436 hr = IDirect3DDevice2_AddViewport(device, another_vp);
1437 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1438 ref = get_refcount((IUnknown *) another_vp);
1439 ok(ref == 2, "another_vp refcount is %d\n", ref);
1441 test_vp = (IDirect3DViewport2 *) 0xbaadc0de;
1442 hr = IDirect3DDevice2_GetCurrentViewport(device, &test_vp);
1443 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1444 ok(test_vp == (IDirect3DViewport2 *) 0xbaadc0de, "Got unexpected pointer %p\n", test_vp);
1446 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport2);
1447 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1448 ref = get_refcount((IUnknown *) viewport2);
1449 ok(ref == 3, "viewport2 refcount is %d\n", ref);
1450 ref = get_refcount((IUnknown *) device);
1451 ok(ref == 1, "device refcount is %d\n", ref);
1453 test_vp = NULL;
1454 hr = IDirect3DDevice2_GetCurrentViewport(device, &test_vp);
1455 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
1456 ok(test_vp == viewport2, "Got unexpected viewport %p\n", test_vp);
1457 ref = get_refcount((IUnknown *) viewport2);
1458 ok(ref == 4, "viewport2 refcount is %d\n", ref);
1459 if(test_vp) IDirect3DViewport2_Release(test_vp);
1461 /* GetCurrentViewport with a viewport set and NULL input param: Segfault */
1463 /* Cannot set the viewport to NULL */
1464 hr = IDirect3DDevice2_SetCurrentViewport(device, NULL);
1465 ok(hr == DDERR_INVALIDPARAMS, "Failed to set viewport to NULL, hr %#x.\n", hr);
1466 test_vp = NULL;
1467 hr = IDirect3DDevice2_GetCurrentViewport(device, &test_vp);
1468 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
1469 ok(test_vp == viewport2, "Got unexpected viewport %p\n", test_vp);
1470 if(test_vp) IDirect3DViewport2_Release(test_vp);
1472 /* SetCurrentViewport properly releases the old viewport's reference */
1473 hr = IDirect3DDevice2_SetCurrentViewport(device, another_vp);
1474 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1475 ref = get_refcount((IUnknown *) viewport2);
1476 ok(ref == 2, "viewport2 refcount is %d\n", ref);
1477 ref = get_refcount((IUnknown *) another_vp);
1478 ok(ref == 3, "another_vp refcount is %d\n", ref);
1480 /* Deleting the viewport removes the reference added by AddViewport, but not
1481 * the one added by SetCurrentViewport. */
1482 hr = IDirect3DDevice2_DeleteViewport(device, another_vp);
1483 ok(SUCCEEDED(hr), "Failed to delete viewport from device, hr %#x.\n", hr);
1484 ref = get_refcount((IUnknown *) another_vp);
1485 todo_wine ok(ref == 2, "IDirect3DViewport2 refcount is %d\n", ref);
1487 /* GetCurrentViewport fails though */
1488 test_vp = NULL;
1489 hr = IDirect3DDevice2_GetCurrentViewport(device, &test_vp);
1490 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1491 ok(test_vp == NULL, "Got unexpected viewport %p\n", test_vp);
1492 if(test_vp) IDirect3DViewport2_Release(test_vp);
1494 /* Setting a different viewport does not free the leaked reference. How
1495 * do I get rid of it? Leak the viewport for now. */
1496 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport2);
1497 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1498 ref = get_refcount((IUnknown *) viewport2);
1499 ok(ref == 3, "viewport2 refcount is %d\n", ref);
1500 ref = get_refcount((IUnknown *) another_vp);
1501 todo_wine ok(ref == 2, "another_vp refcount is %d\n", ref);
1503 /* Destroying the device removes the viewport, but does not free the reference
1504 * added by SetCurrentViewport. */
1505 IDirect3DDevice2_Release(device);
1506 ref = get_refcount((IUnknown *) viewport2);
1507 todo_wine ok(ref == 2, "viewport2 refcount is %d\n", ref);
1509 IDirect3DViewport2_Release(another_vp);
1510 IDirect3DViewport2_Release(viewport2);
1511 IDirect3D2_Release(d3d);
1512 DestroyWindow(window);
1513 IDirectDraw2_Release(ddraw);
1516 static void test_zenable(void)
1518 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1519 static D3DTLVERTEX tquad[] =
1521 {{ 0.0f}, {480.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1522 {{ 0.0f}, { 0.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1523 {{640.0f}, {480.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1524 {{640.0f}, { 0.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1526 IDirect3DMaterial2 *background;
1527 IDirect3DViewport2 *viewport;
1528 IDirect3DDevice2 *device;
1529 IDirectDrawSurface *rt;
1530 IDirectDraw2 *ddraw;
1531 D3DCOLOR color;
1532 HWND window;
1533 HRESULT hr;
1534 UINT x, y;
1535 UINT i, j;
1537 window = create_window();
1538 ddraw = create_ddraw();
1539 ok(!!ddraw, "Failed to create a ddraw object.\n");
1540 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1542 skip("Failed to create a 3D device, skipping test.\n");
1543 IDirectDraw2_Release(ddraw);
1544 DestroyWindow(window);
1545 return;
1548 background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
1549 viewport = create_viewport(device, 0, 0, 640, 480);
1550 viewport_set_background(device, viewport, background);
1551 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
1552 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1554 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
1555 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
1557 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1558 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1559 hr = IDirect3DDevice2_BeginScene(device);
1560 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1561 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, tquad, 4, 0);
1562 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1563 hr = IDirect3DDevice2_EndScene(device);
1564 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1566 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
1567 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1568 for (i = 0; i < 4; ++i)
1570 for (j = 0; j < 4; ++j)
1572 x = 80 * ((2 * j) + 1);
1573 y = 60 * ((2 * i) + 1);
1574 color = get_surface_color(rt, x, y);
1575 ok(compare_color(color, 0x0000ff00, 1),
1576 "Expected color 0x0000ff00 at %u, %u, got 0x%08x.\n", x, y, color);
1579 IDirectDrawSurface_Release(rt);
1581 destroy_viewport(device, viewport);
1582 destroy_material(background);
1583 IDirect3DDevice2_Release(device);
1584 IDirectDraw2_Release(ddraw);
1585 DestroyWindow(window);
1588 static void test_ck_rgba(void)
1590 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1591 static D3DTLVERTEX tquad[] =
1593 {{ 0.0f}, {480.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1594 {{ 0.0f}, { 0.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1595 {{640.0f}, {480.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1596 {{640.0f}, { 0.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1597 {{ 0.0f}, {480.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1598 {{ 0.0f}, { 0.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1599 {{640.0f}, {480.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1600 {{640.0f}, { 0.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1602 static const struct
1604 D3DCOLOR fill_color;
1605 BOOL color_key;
1606 BOOL blend;
1607 D3DCOLOR result1, result1_broken;
1608 D3DCOLOR result2, result2_broken;
1610 tests[] =
1612 /* r200 on Windows doesn't check the alpha component when applying the color
1613 * key, so the key matches on every texel. */
1614 {0xff00ff00, TRUE, TRUE, 0x00ff0000, 0x00ff0000, 0x000000ff, 0x000000ff},
1615 {0xff00ff00, TRUE, FALSE, 0x00ff0000, 0x00ff0000, 0x000000ff, 0x000000ff},
1616 {0xff00ff00, FALSE, TRUE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1617 {0xff00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1618 {0x7f00ff00, TRUE, TRUE, 0x00807f00, 0x00ff0000, 0x00807f00, 0x000000ff},
1619 {0x7f00ff00, TRUE, FALSE, 0x0000ff00, 0x00ff0000, 0x0000ff00, 0x000000ff},
1620 {0x7f00ff00, FALSE, TRUE, 0x00807f00, 0x00807f00, 0x00807f00, 0x00807f00},
1621 {0x7f00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1624 D3DTEXTUREHANDLE texture_handle;
1625 IDirect3DMaterial2 *background;
1626 IDirectDrawSurface *surface;
1627 IDirect3DViewport2 *viewport;
1628 IDirect3DTexture2 *texture;
1629 DDSURFACEDESC surface_desc;
1630 IDirect3DDevice2 *device;
1631 IDirectDrawSurface *rt;
1632 IDirectDraw2 *ddraw;
1633 D3DCOLOR color;
1634 HWND window;
1635 DDBLTFX fx;
1636 HRESULT hr;
1637 UINT i;
1639 window = create_window();
1640 ddraw = create_ddraw();
1641 ok(!!ddraw, "Failed to create a ddraw object.\n");
1642 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1644 skip("Failed to create a 3D device, skipping test.\n");
1645 IDirectDraw2_Release(ddraw);
1646 DestroyWindow(window);
1647 return;
1650 background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
1651 viewport = create_viewport(device, 0, 0, 640, 480);
1652 viewport_set_background(device, viewport, background);
1653 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
1654 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1656 memset(&surface_desc, 0, sizeof(surface_desc));
1657 surface_desc.dwSize = sizeof(surface_desc);
1658 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1659 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1660 surface_desc.dwWidth = 256;
1661 surface_desc.dwHeight = 256;
1662 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1663 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1664 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1665 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1666 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1667 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
1668 U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
1669 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0xff00ff00;
1670 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0xff00ff00;
1671 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1672 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
1673 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
1674 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1675 hr = IDirect3DTexture2_GetHandle(texture, device, &texture_handle);
1676 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
1677 IDirect3DTexture2_Release(texture);
1679 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
1680 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
1681 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
1682 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1683 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
1684 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1686 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
1687 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1689 for (i = 0; i < ARRAY_SIZE(tests); ++i)
1691 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, tests[i].color_key);
1692 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
1693 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, tests[i].blend);
1694 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1696 memset(&fx, 0, sizeof(fx));
1697 fx.dwSize = sizeof(fx);
1698 U5(fx).dwFillColor = tests[i].fill_color;
1699 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1700 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1702 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER);
1703 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1704 hr = IDirect3DDevice2_BeginScene(device);
1705 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1706 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &tquad[0], 4, 0);
1707 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1708 hr = IDirect3DDevice2_EndScene(device);
1709 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1711 color = get_surface_color(rt, 320, 240);
1712 ok(compare_color(color, tests[i].result1, 1) || compare_color(color, tests[i].result1_broken, 1),
1713 "Expected color 0x%08x for test %u, got 0x%08x.\n",
1714 tests[i].result1, i, color);
1716 U5(fx).dwFillColor = 0xff0000ff;
1717 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1718 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1720 hr = IDirect3DDevice2_BeginScene(device);
1721 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1722 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &tquad[4], 4, 0);
1723 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1724 hr = IDirect3DDevice2_EndScene(device);
1725 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1727 /* This tests that fragments that are masked out by the color key are
1728 * discarded, instead of just fully transparent. */
1729 color = get_surface_color(rt, 320, 240);
1730 ok(compare_color(color, tests[i].result2, 1) || compare_color(color, tests[i].result2_broken, 1),
1731 "Expected color 0x%08x for test %u, got 0x%08x.\n",
1732 tests[i].result2, i, color);
1735 IDirectDrawSurface_Release(rt);
1736 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, 0);
1737 ok(SUCCEEDED(hr), "Failed to unset texture, hr %#x.\n", hr);
1738 IDirectDrawSurface_Release(surface);
1739 destroy_viewport(device, viewport);
1740 destroy_material(background);
1741 IDirect3DDevice2_Release(device);
1742 IDirectDraw2_Release(ddraw);
1743 DestroyWindow(window);
1746 static void test_ck_default(void)
1748 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1749 static D3DTLVERTEX tquad[] =
1751 {{ 0.0f}, {480.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1752 {{ 0.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1753 {{640.0f}, {480.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1754 {{640.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1756 IDirectDrawSurface *surface, *rt;
1757 D3DTEXTUREHANDLE texture_handle;
1758 IDirect3DMaterial2 *background;
1759 IDirect3DViewport2 *viewport;
1760 DDSURFACEDESC surface_desc;
1761 IDirect3DTexture2 *texture;
1762 IDirect3DDevice2 *device;
1763 IDirectDraw2 *ddraw;
1764 D3DCOLOR color;
1765 DWORD value;
1766 HWND window;
1767 DDBLTFX fx;
1768 HRESULT hr;
1770 window = create_window();
1771 ddraw = create_ddraw();
1772 ok(!!ddraw, "Failed to create a ddraw object.\n");
1773 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1775 skip("Failed to create a 3D device, skipping test.\n");
1776 IDirectDraw2_Release(ddraw);
1777 DestroyWindow(window);
1778 return;
1781 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
1782 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1784 background = create_diffuse_material(device, 0.0, 1.0f, 0.0f, 1.0f);
1785 viewport = create_viewport(device, 0, 0, 640, 480);
1786 viewport_set_background(device, viewport, background);
1787 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
1788 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1790 memset(&surface_desc, 0, sizeof(surface_desc));
1791 surface_desc.dwSize = sizeof(surface_desc);
1792 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1793 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1794 surface_desc.dwWidth = 256;
1795 surface_desc.dwHeight = 256;
1796 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1797 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
1798 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1799 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1800 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1801 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
1802 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x000000ff;
1803 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x000000ff;
1804 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1805 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1806 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
1807 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1808 hr = IDirect3DTexture2_GetHandle(texture, device, &texture_handle);
1809 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
1810 IDirect3DTexture_Release(texture);
1812 memset(&fx, 0, sizeof(fx));
1813 fx.dwSize = sizeof(fx);
1814 U5(fx).dwFillColor = 0x000000ff;
1815 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1816 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
1818 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1819 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1820 hr = IDirect3DDevice2_BeginScene(device);
1821 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1822 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
1823 ok(SUCCEEDED(hr), "Failed to set texture handle, hr %#x.\n", hr);
1824 hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, &value);
1825 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1826 ok(!value, "Got unexpected color keying state %#x.\n", value);
1827 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &tquad[0], 4, 0);
1828 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1829 hr = IDirect3DDevice2_EndScene(device);
1830 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1831 color = get_surface_color(rt, 320, 240);
1832 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
1834 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1835 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1836 hr = IDirect3DDevice2_BeginScene(device);
1837 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1838 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
1839 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
1840 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &tquad[0], 4, 0);
1841 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1842 hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, &value);
1843 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1844 ok(!!value, "Got unexpected color keying state %#x.\n", value);
1845 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, 0);
1846 ok(SUCCEEDED(hr), "Failed to set texture handle, hr %#x.\n", hr);
1847 hr = IDirect3DDevice2_EndScene(device);
1848 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1849 color = get_surface_color(rt, 320, 240);
1850 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1852 IDirectDrawSurface_Release(surface);
1853 destroy_viewport(device, viewport);
1854 destroy_material(background);
1855 IDirectDrawSurface_Release(rt);
1856 IDirect3DDevice2_Release(device);
1857 IDirectDraw2_Release(ddraw);
1858 DestroyWindow(window);
1861 static void test_ck_complex(void)
1863 IDirectDrawSurface *surface, *mipmap, *tmp;
1864 DDSCAPS caps = {DDSCAPS_COMPLEX};
1865 DDSURFACEDESC surface_desc;
1866 IDirect3DDevice2 *device;
1867 DDCOLORKEY color_key;
1868 IDirectDraw2 *ddraw;
1869 unsigned int i;
1870 ULONG refcount;
1871 HWND window;
1872 HRESULT hr;
1874 window = create_window();
1875 ddraw = create_ddraw();
1876 ok(!!ddraw, "Failed to create a ddraw object.\n");
1877 if (!(device = create_device(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
1879 skip("Failed to create a 3D device, skipping test.\n");
1880 DestroyWindow(window);
1881 IDirectDraw2_Release(ddraw);
1882 return;
1884 IDirect3DDevice2_Release(device);
1886 memset(&surface_desc, 0, sizeof(surface_desc));
1887 surface_desc.dwSize = sizeof(surface_desc);
1888 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1889 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1890 surface_desc.dwWidth = 128;
1891 surface_desc.dwHeight = 128;
1892 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1893 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1895 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1896 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1897 color_key.dwColorSpaceLowValue = 0x0000ff00;
1898 color_key.dwColorSpaceHighValue = 0x0000ff00;
1899 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1900 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1901 memset(&color_key, 0, sizeof(color_key));
1902 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1903 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1904 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1905 color_key.dwColorSpaceLowValue);
1906 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1907 color_key.dwColorSpaceHighValue);
1909 mipmap = surface;
1910 IDirectDrawSurface_AddRef(mipmap);
1911 for (i = 0; i < 7; ++i)
1913 hr = IDirectDrawSurface_GetAttachedSurface(mipmap, &caps, &tmp);
1914 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
1916 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1917 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
1918 color_key.dwColorSpaceLowValue = 0x000000ff;
1919 color_key.dwColorSpaceHighValue = 0x000000ff;
1920 hr = IDirectDrawSurface_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1921 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x, i %u.\n", hr, i);
1922 memset(&color_key, 0, sizeof(color_key));
1923 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1924 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x, i %u.\n", hr, i);
1925 ok(color_key.dwColorSpaceLowValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
1926 color_key.dwColorSpaceLowValue, i);
1927 ok(color_key.dwColorSpaceHighValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
1928 color_key.dwColorSpaceHighValue, i);
1930 IDirectDrawSurface_Release(mipmap);
1931 mipmap = tmp;
1934 memset(&color_key, 0, sizeof(color_key));
1935 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1936 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1937 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1938 color_key.dwColorSpaceLowValue);
1939 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1940 color_key.dwColorSpaceHighValue);
1942 hr = IDirectDrawSurface_GetAttachedSurface(mipmap, &caps, &tmp);
1943 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
1944 IDirectDrawSurface_Release(mipmap);
1945 refcount = IDirectDrawSurface_Release(surface);
1946 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1948 memset(&surface_desc, 0, sizeof(surface_desc));
1949 surface_desc.dwSize = sizeof(surface_desc);
1950 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
1951 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
1952 surface_desc.dwBackBufferCount = 1;
1953 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1954 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1956 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1957 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1958 color_key.dwColorSpaceLowValue = 0x0000ff00;
1959 color_key.dwColorSpaceHighValue = 0x0000ff00;
1960 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1961 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1962 memset(&color_key, 0, sizeof(color_key));
1963 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1964 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1965 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1966 color_key.dwColorSpaceLowValue);
1967 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1968 color_key.dwColorSpaceHighValue);
1970 hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &tmp);
1971 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
1973 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1974 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
1975 color_key.dwColorSpaceLowValue = 0x0000ff00;
1976 color_key.dwColorSpaceHighValue = 0x0000ff00;
1977 hr = IDirectDrawSurface_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1978 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1979 memset(&color_key, 0, sizeof(color_key));
1980 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1981 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1982 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1983 color_key.dwColorSpaceLowValue);
1984 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1985 color_key.dwColorSpaceHighValue);
1987 IDirectDrawSurface_Release(tmp);
1989 refcount = IDirectDrawSurface_Release(surface);
1990 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1991 refcount = IDirectDraw2_Release(ddraw);
1992 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1993 DestroyWindow(window);
1996 struct qi_test
1998 REFIID iid;
1999 REFIID refcount_iid;
2000 HRESULT hr;
2003 static void test_qi(const char *test_name, IUnknown *base_iface,
2004 REFIID refcount_iid, const struct qi_test *tests, UINT entry_count)
2006 ULONG refcount, expected_refcount;
2007 IUnknown *iface1, *iface2;
2008 HRESULT hr;
2009 UINT i, j;
2011 for (i = 0; i < entry_count; ++i)
2013 hr = IUnknown_QueryInterface(base_iface, tests[i].iid, (void **)&iface1);
2014 ok(hr == tests[i].hr, "Got hr %#x for test \"%s\" %u.\n", hr, test_name, i);
2015 if (SUCCEEDED(hr))
2017 for (j = 0; j < entry_count; ++j)
2019 hr = IUnknown_QueryInterface(iface1, tests[j].iid, (void **)&iface2);
2020 ok(hr == tests[j].hr, "Got hr %#x for test \"%s\" %u, %u.\n", hr, test_name, i, j);
2021 if (SUCCEEDED(hr))
2023 expected_refcount = 0;
2024 if (IsEqualGUID(refcount_iid, tests[j].refcount_iid))
2025 ++expected_refcount;
2026 if (IsEqualGUID(tests[i].refcount_iid, tests[j].refcount_iid))
2027 ++expected_refcount;
2028 refcount = IUnknown_Release(iface2);
2029 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, %u, expected %u.\n",
2030 refcount, test_name, i, j, expected_refcount);
2034 expected_refcount = 0;
2035 if (IsEqualGUID(refcount_iid, tests[i].refcount_iid))
2036 ++expected_refcount;
2037 refcount = IUnknown_Release(iface1);
2038 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, expected %u.\n",
2039 refcount, test_name, i, expected_refcount);
2044 static void test_surface_qi(void)
2046 static const struct qi_test tests[] =
2048 {&IID_IDirect3DTexture2, &IID_IDirectDrawSurface, S_OK },
2049 {&IID_IDirect3DTexture, &IID_IDirectDrawSurface, S_OK },
2050 {&IID_IDirectDrawGammaControl, &IID_IDirectDrawGammaControl, S_OK },
2051 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
2052 {&IID_IDirectDrawSurface7, &IID_IDirectDrawSurface7, S_OK },
2053 {&IID_IDirectDrawSurface4, &IID_IDirectDrawSurface4, S_OK },
2054 {&IID_IDirectDrawSurface3, &IID_IDirectDrawSurface3, S_OK },
2055 {&IID_IDirectDrawSurface2, &IID_IDirectDrawSurface2, S_OK },
2056 {&IID_IDirectDrawSurface, &IID_IDirectDrawSurface, S_OK },
2057 {&IID_IDirect3DDevice7, NULL, E_INVALIDARG },
2058 {&IID_IDirect3DDevice3, NULL, E_INVALIDARG },
2059 {&IID_IDirect3DDevice2, NULL, E_INVALIDARG },
2060 {&IID_IDirect3DDevice, NULL, E_INVALIDARG },
2061 {&IID_IDirect3D7, NULL, E_INVALIDARG },
2062 {&IID_IDirect3D3, NULL, E_INVALIDARG },
2063 {&IID_IDirect3D2, NULL, E_INVALIDARG },
2064 {&IID_IDirect3D, NULL, E_INVALIDARG },
2065 {&IID_IDirectDraw7, NULL, E_INVALIDARG },
2066 {&IID_IDirectDraw4, NULL, E_INVALIDARG },
2067 {&IID_IDirectDraw3, NULL, E_INVALIDARG },
2068 {&IID_IDirectDraw2, NULL, E_INVALIDARG },
2069 {&IID_IDirectDraw, NULL, E_INVALIDARG },
2070 {&IID_IDirect3DLight, NULL, E_INVALIDARG },
2071 {&IID_IDirect3DMaterial, NULL, E_INVALIDARG },
2072 {&IID_IDirect3DMaterial2, NULL, E_INVALIDARG },
2073 {&IID_IDirect3DMaterial3, NULL, E_INVALIDARG },
2074 {&IID_IDirect3DExecuteBuffer, NULL, E_INVALIDARG },
2075 {&IID_IDirect3DViewport, NULL, E_INVALIDARG },
2076 {&IID_IDirect3DViewport2, NULL, E_INVALIDARG },
2077 {&IID_IDirect3DViewport3, NULL, E_INVALIDARG },
2078 {&IID_IDirect3DVertexBuffer, NULL, E_INVALIDARG },
2079 {&IID_IDirect3DVertexBuffer7, NULL, E_INVALIDARG },
2080 {&IID_IDirectDrawPalette, NULL, E_INVALIDARG },
2081 {&IID_IDirectDrawClipper, NULL, E_INVALIDARG },
2082 {&IID_IUnknown, &IID_IDirectDrawSurface, S_OK },
2083 {NULL, NULL, E_INVALIDARG },
2086 IDirectDrawSurface *surface;
2087 DDSURFACEDESC surface_desc;
2088 IDirect3DDevice2 *device;
2089 IDirectDraw2 *ddraw;
2090 HWND window;
2091 HRESULT hr;
2093 if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
2095 win_skip("DirectDrawCreateEx not available, skipping test.\n");
2096 return;
2099 window = create_window();
2100 ddraw = create_ddraw();
2101 ok(!!ddraw, "Failed to create a ddraw object.\n");
2102 /* Try to create a D3D device to see if the ddraw implementation supports
2103 * D3D. 64-bit ddraw in particular doesn't seem to support D3D, and
2104 * doesn't support e.g. the IDirect3DTexture interfaces. */
2105 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
2107 skip("Failed to create a 3D device, skipping test.\n");
2108 IDirectDraw2_Release(ddraw);
2109 DestroyWindow(window);
2110 return;
2112 IDirect3DDevice_Release(device);
2114 memset(&surface_desc, 0, sizeof(surface_desc));
2115 surface_desc.dwSize = sizeof(surface_desc);
2116 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
2117 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
2118 surface_desc.dwWidth = 512;
2119 surface_desc.dwHeight = 512;
2120 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, (IDirectDrawSurface **)0xdeadbeef, NULL);
2121 ok(hr == E_INVALIDARG, "Got unexpected hr %#x.\n", hr);
2122 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
2123 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
2125 test_qi("surface_qi", (IUnknown *)surface, &IID_IDirectDrawSurface, tests, ARRAY_SIZE(tests));
2127 IDirectDrawSurface_Release(surface);
2128 IDirectDraw2_Release(ddraw);
2129 DestroyWindow(window);
2132 static void test_device_qi(void)
2134 static const struct qi_test tests[] =
2136 {&IID_IDirect3DTexture2, NULL, E_NOINTERFACE},
2137 {&IID_IDirect3DTexture, NULL, E_NOINTERFACE},
2138 {&IID_IDirectDrawGammaControl, NULL, E_NOINTERFACE},
2139 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
2140 {&IID_IDirectDrawSurface7, NULL, E_NOINTERFACE},
2141 {&IID_IDirectDrawSurface4, NULL, E_NOINTERFACE},
2142 {&IID_IDirectDrawSurface3, NULL, E_NOINTERFACE},
2143 {&IID_IDirectDrawSurface2, NULL, E_NOINTERFACE},
2144 {&IID_IDirectDrawSurface, NULL, E_NOINTERFACE},
2145 {&IID_IDirect3DDevice7, NULL, E_NOINTERFACE},
2146 {&IID_IDirect3DDevice3, NULL, E_NOINTERFACE},
2147 {&IID_IDirect3DDevice2, &IID_IDirect3DDevice2, S_OK },
2148 {&IID_IDirect3DDevice, &IID_IDirect3DDevice2, S_OK },
2149 {&IID_IDirect3DRampDevice, NULL, E_NOINTERFACE},
2150 {&IID_IDirect3DRGBDevice, NULL, E_NOINTERFACE},
2151 {&IID_IDirect3DHALDevice, NULL, E_NOINTERFACE},
2152 {&IID_IDirect3DMMXDevice, NULL, E_NOINTERFACE},
2153 {&IID_IDirect3DRefDevice, NULL, E_NOINTERFACE},
2154 {&IID_IDirect3DTnLHalDevice, NULL, E_NOINTERFACE},
2155 {&IID_IDirect3DNullDevice, NULL, E_NOINTERFACE},
2156 {&IID_IDirect3D7, NULL, E_NOINTERFACE},
2157 {&IID_IDirect3D3, NULL, E_NOINTERFACE},
2158 {&IID_IDirect3D2, NULL, E_NOINTERFACE},
2159 {&IID_IDirect3D, NULL, E_NOINTERFACE},
2160 {&IID_IDirectDraw7, NULL, E_NOINTERFACE},
2161 {&IID_IDirectDraw4, NULL, E_NOINTERFACE},
2162 {&IID_IDirectDraw3, NULL, E_NOINTERFACE},
2163 {&IID_IDirectDraw2, NULL, E_NOINTERFACE},
2164 {&IID_IDirectDraw, NULL, E_NOINTERFACE},
2165 {&IID_IDirect3DLight, NULL, E_NOINTERFACE},
2166 {&IID_IDirect3DMaterial, NULL, E_NOINTERFACE},
2167 {&IID_IDirect3DMaterial2, NULL, E_NOINTERFACE},
2168 {&IID_IDirect3DMaterial3, NULL, E_NOINTERFACE},
2169 {&IID_IDirect3DExecuteBuffer, NULL, E_NOINTERFACE},
2170 {&IID_IDirect3DViewport, NULL, E_NOINTERFACE},
2171 {&IID_IDirect3DViewport2, NULL, E_NOINTERFACE},
2172 {&IID_IDirect3DViewport3, NULL, E_NOINTERFACE},
2173 {&IID_IDirect3DVertexBuffer, NULL, E_NOINTERFACE},
2174 {&IID_IDirect3DVertexBuffer7, NULL, E_NOINTERFACE},
2175 {&IID_IDirectDrawPalette, NULL, E_NOINTERFACE},
2176 {&IID_IDirectDrawClipper, NULL, E_NOINTERFACE},
2177 {&IID_IUnknown, &IID_IDirect3DDevice2, S_OK },
2180 IDirect3DDevice2 *device;
2181 IDirectDraw2 *ddraw;
2182 HWND window;
2184 window = create_window();
2185 ddraw = create_ddraw();
2186 ok(!!ddraw, "Failed to create a ddraw object.\n");
2187 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
2189 skip("Failed to create a 3D device, skipping test.\n");
2190 IDirectDraw2_Release(ddraw);
2191 DestroyWindow(window);
2192 return;
2195 test_qi("device_qi", (IUnknown *)device, &IID_IDirect3DDevice2, tests, ARRAY_SIZE(tests));
2197 IDirect3DDevice2_Release(device);
2198 IDirectDraw2_Release(ddraw);
2199 DestroyWindow(window);
2202 static void test_wndproc(void)
2204 LONG_PTR proc, ddraw_proc;
2205 IDirectDraw2 *ddraw;
2206 WNDCLASSA wc = {0};
2207 HWND window;
2208 HRESULT hr;
2209 ULONG ref;
2211 static struct message messages[] =
2213 {WM_WINDOWPOSCHANGING, FALSE, 0},
2214 {WM_MOVE, FALSE, 0},
2215 {WM_SIZE, FALSE, 0},
2216 {WM_WINDOWPOSCHANGING, FALSE, 0},
2217 {WM_ACTIVATE, FALSE, 0},
2218 {WM_SETFOCUS, FALSE, 0},
2219 {0, FALSE, 0},
2222 /* DDSCL_EXCLUSIVE replaces the window's window proc. */
2223 ddraw = create_ddraw();
2224 ok(!!ddraw, "Failed to create a ddraw object.\n");
2226 wc.lpfnWndProc = test_proc;
2227 wc.lpszClassName = "ddraw_test_wndproc_wc";
2228 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2230 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
2231 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
2233 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2234 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2235 (LONG_PTR)test_proc, proc);
2236 expect_messages = messages;
2237 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2238 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2239 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2240 expect_messages = NULL;
2241 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2242 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2243 (LONG_PTR)test_proc, proc);
2244 ref = IDirectDraw2_Release(ddraw);
2245 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2246 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2247 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2248 (LONG_PTR)test_proc, proc);
2250 /* DDSCL_NORMAL doesn't. */
2251 ddraw = create_ddraw();
2252 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2253 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2254 (LONG_PTR)test_proc, proc);
2255 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
2256 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2257 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2258 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2259 (LONG_PTR)test_proc, proc);
2260 ref = IDirectDraw2_Release(ddraw);
2261 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2262 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2263 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2264 (LONG_PTR)test_proc, proc);
2266 /* The original window proc is only restored by ddraw if the current
2267 * window proc matches the one ddraw set. This also affects switching
2268 * from DDSCL_NORMAL to DDSCL_EXCLUSIVE. */
2269 ddraw = create_ddraw();
2270 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2271 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2272 (LONG_PTR)test_proc, proc);
2273 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2274 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2275 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2276 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2277 (LONG_PTR)test_proc, proc);
2278 ddraw_proc = proc;
2279 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2280 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2281 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2282 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2283 (LONG_PTR)test_proc, proc);
2284 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2285 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2286 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2287 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2288 (LONG_PTR)test_proc, proc);
2289 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2290 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2291 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2292 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2293 (LONG_PTR)DefWindowProcA, proc);
2294 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2295 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2296 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)ddraw_proc);
2297 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2298 (LONG_PTR)DefWindowProcA, proc);
2299 ref = IDirectDraw2_Release(ddraw);
2300 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2301 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2302 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2303 (LONG_PTR)test_proc, proc);
2305 ddraw = create_ddraw();
2306 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2307 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2308 (LONG_PTR)test_proc, proc);
2309 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2310 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2311 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2312 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2313 (LONG_PTR)test_proc, proc);
2314 ref = IDirectDraw2_Release(ddraw);
2315 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2316 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2317 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2318 (LONG_PTR)DefWindowProcA, proc);
2320 fix_wndproc(window, (LONG_PTR)test_proc);
2321 expect_messages = NULL;
2322 DestroyWindow(window);
2323 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
2326 static void test_window_style(void)
2328 LONG style, exstyle, tmp, expected_style;
2329 RECT fullscreen_rect, r;
2330 IDirectDraw2 *ddraw;
2331 HWND window;
2332 HRESULT hr;
2333 ULONG ref;
2334 BOOL ret;
2336 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2337 0, 0, 100, 100, 0, 0, 0, 0);
2338 ddraw = create_ddraw();
2339 ok(!!ddraw, "Failed to create a ddraw object.\n");
2341 style = GetWindowLongA(window, GWL_STYLE);
2342 exstyle = GetWindowLongA(window, GWL_EXSTYLE);
2343 SetRect(&fullscreen_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2345 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2346 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2348 tmp = GetWindowLongA(window, GWL_STYLE);
2349 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2350 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2351 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2353 GetWindowRect(window, &r);
2354 ok(EqualRect(&r, &fullscreen_rect), "Expected %s, got %s.\n",
2355 wine_dbgstr_rect(&fullscreen_rect), wine_dbgstr_rect(&r));
2356 GetClientRect(window, &r);
2357 todo_wine ok(!EqualRect(&r, &fullscreen_rect), "Client rect and window rect are equal.\n");
2359 ret = SetForegroundWindow(GetDesktopWindow());
2360 ok(ret, "Failed to set foreground window.\n");
2362 tmp = GetWindowLongA(window, GWL_STYLE);
2363 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2364 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2365 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2367 ret = SetForegroundWindow(window);
2368 ok(ret, "Failed to set foreground window.\n");
2369 /* Windows 7 (but not Vista and XP) shows the window when it receives focus. Hide it again,
2370 * the next tests expect this. */
2371 ShowWindow(window, SW_HIDE);
2373 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2374 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2376 tmp = GetWindowLongA(window, GWL_STYLE);
2377 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2378 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2379 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2381 ShowWindow(window, SW_SHOW);
2382 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2383 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2385 tmp = GetWindowLongA(window, GWL_STYLE);
2386 expected_style = style | WS_VISIBLE;
2387 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2388 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2389 expected_style = exstyle | WS_EX_TOPMOST;
2390 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2392 ret = SetForegroundWindow(GetDesktopWindow());
2393 ok(ret, "Failed to set foreground window.\n");
2394 tmp = GetWindowLongA(window, GWL_STYLE);
2395 expected_style = style | WS_VISIBLE | WS_MINIMIZE;
2396 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2397 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2398 expected_style = exstyle | WS_EX_TOPMOST;
2399 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2401 ref = IDirectDraw2_Release(ddraw);
2402 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2404 DestroyWindow(window);
2407 static void test_redundant_mode_set(void)
2409 DDSURFACEDESC surface_desc = {0};
2410 IDirectDraw2 *ddraw;
2411 RECT q, r, s;
2412 HWND window;
2413 HRESULT hr;
2414 ULONG ref;
2416 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2417 0, 0, 100, 100, 0, 0, 0, 0);
2418 ddraw = create_ddraw();
2419 ok(!!ddraw, "Failed to create a ddraw object.\n");
2421 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2422 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2424 surface_desc.dwSize = sizeof(surface_desc);
2425 hr = IDirectDraw2_GetDisplayMode(ddraw, &surface_desc);
2426 ok(SUCCEEDED(hr), "GetDisplayMode failed, hr %#x.\n", hr);
2428 hr = IDirectDraw2_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2429 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount, 0, 0);
2430 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2432 GetWindowRect(window, &q);
2433 r = q;
2434 r.right /= 2;
2435 r.bottom /= 2;
2436 SetWindowPos(window, HWND_TOP, r.left, r.top, r.right, r.bottom, 0);
2437 GetWindowRect(window, &s);
2438 ok(EqualRect(&r, &s), "Expected %s, got %s.\n", wine_dbgstr_rect(&r), wine_dbgstr_rect(&s));
2440 hr = IDirectDraw2_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2441 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount, 0, 0);
2442 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2444 GetWindowRect(window, &s);
2445 ok(EqualRect(&r, &s) || broken(EqualRect(&q, &s) /* Windows 10 */),
2446 "Expected %s, got %s.\n", wine_dbgstr_rect(&r), wine_dbgstr_rect(&s));
2448 ref = IDirectDraw2_Release(ddraw);
2449 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2451 DestroyWindow(window);
2454 static SIZE screen_size, screen_size2;
2456 static LRESULT CALLBACK mode_set_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2458 if (message == WM_SIZE)
2460 screen_size.cx = GetSystemMetrics(SM_CXSCREEN);
2461 screen_size.cy = GetSystemMetrics(SM_CYSCREEN);
2464 return test_proc(hwnd, message, wparam, lparam);
2467 static LRESULT CALLBACK mode_set_proc2(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2469 if (message == WM_SIZE)
2471 screen_size2.cx = GetSystemMetrics(SM_CXSCREEN);
2472 screen_size2.cy = GetSystemMetrics(SM_CYSCREEN);
2475 return test_proc(hwnd, message, wparam, lparam);
2478 struct test_coop_level_mode_set_enum_param
2480 DWORD ddraw_width, ddraw_height, user32_width, user32_height;
2483 static HRESULT CALLBACK test_coop_level_mode_set_enum_cb(DDSURFACEDESC *surface_desc, void *context)
2485 struct test_coop_level_mode_set_enum_param *param = context;
2487 if (U1(surface_desc->ddpfPixelFormat).dwRGBBitCount != registry_mode.dmBitsPerPel)
2488 return DDENUMRET_OK;
2489 if (surface_desc->dwWidth == registry_mode.dmPelsWidth
2490 && surface_desc->dwHeight == registry_mode.dmPelsHeight)
2491 return DDENUMRET_OK;
2493 if (!param->ddraw_width)
2495 param->ddraw_width = surface_desc->dwWidth;
2496 param->ddraw_height = surface_desc->dwHeight;
2497 return DDENUMRET_OK;
2499 if (surface_desc->dwWidth == param->ddraw_width && surface_desc->dwHeight == param->ddraw_height)
2500 return DDENUMRET_OK;
2502 param->user32_width = surface_desc->dwWidth;
2503 param->user32_height = surface_desc->dwHeight;
2504 return DDENUMRET_CANCEL;
2507 static void test_coop_level_mode_set(void)
2509 IDirectDrawSurface *primary;
2510 RECT registry_rect, ddraw_rect, user32_rect, r;
2511 IDirectDraw2 *ddraw;
2512 DDSURFACEDESC ddsd;
2513 WNDCLASSA wc = {0};
2514 HWND window, window2;
2515 HRESULT hr;
2516 ULONG ref;
2517 MSG msg;
2518 struct test_coop_level_mode_set_enum_param param;
2519 DEVMODEW devmode;
2520 BOOL ret;
2521 LONG change_ret;
2523 static const struct message exclusive_messages[] =
2525 {WM_WINDOWPOSCHANGING, FALSE, 0},
2526 {WM_WINDOWPOSCHANGED, FALSE, 0},
2527 {WM_SIZE, FALSE, 0},
2528 {WM_DISPLAYCHANGE, FALSE, 0},
2529 {0, FALSE, 0},
2531 static const struct message exclusive_focus_loss_messages[] =
2533 {WM_ACTIVATE, TRUE, WA_INACTIVE},
2534 {WM_DISPLAYCHANGE, FALSE, 0},
2535 {WM_WINDOWPOSCHANGING, FALSE, 0},
2536 /* Like d3d8 and d3d9 ddraw seems to use SW_SHOWMINIMIZED instead of
2537 * SW_MINIMIZED, causing a recursive window activation that does not
2538 * produce the same result in Wine yet. Ignore the difference for now.
2539 * {WM_ACTIVATE, TRUE, 0x200000 | WA_ACTIVE}, */
2540 {WM_WINDOWPOSCHANGED, FALSE, 0},
2541 {WM_MOVE, FALSE, 0},
2542 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2543 {WM_ACTIVATEAPP, TRUE, FALSE},
2544 {0, FALSE, 0},
2546 static const struct message exclusive_focus_restore_messages[] =
2548 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* From the ShowWindow(SW_RESTORE). */
2549 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* Generated by ddraw, matches d3d9 behavior. */
2550 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching previous message. */
2551 {WM_SIZE, FALSE, 0}, /* DefWindowProc. */
2552 {WM_DISPLAYCHANGE, FALSE, 0}, /* Ddraw restores mode. */
2553 /* Native redundantly sets the window size here. */
2554 {WM_ACTIVATEAPP, TRUE, TRUE}, /* End of ddraw's hooks. */
2555 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching the one from ShowWindow. */
2556 {WM_MOVE, FALSE, 0}, /* DefWindowProc. */
2557 {WM_SIZE, TRUE, SIZE_RESTORED}, /* DefWindowProc. */
2558 {0, FALSE, 0},
2560 static const struct message sc_restore_messages[] =
2562 {WM_SYSCOMMAND, TRUE, SC_RESTORE},
2563 {WM_WINDOWPOSCHANGING, FALSE, 0},
2564 {WM_WINDOWPOSCHANGED, FALSE, 0},
2565 {WM_SIZE, TRUE, SIZE_RESTORED},
2566 {0, FALSE, 0},
2568 static const struct message sc_minimize_messages[] =
2570 {WM_SYSCOMMAND, TRUE, SC_MINIMIZE},
2571 {WM_WINDOWPOSCHANGING, FALSE, 0},
2572 {WM_WINDOWPOSCHANGED, FALSE, 0},
2573 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2574 {0, FALSE, 0},
2576 static const struct message sc_maximize_messages[] =
2578 {WM_SYSCOMMAND, TRUE, SC_MAXIMIZE},
2579 {WM_WINDOWPOSCHANGING, FALSE, 0},
2580 {WM_WINDOWPOSCHANGED, FALSE, 0},
2581 {WM_SIZE, TRUE, SIZE_MAXIMIZED},
2582 {0, FALSE, 0},
2585 static const struct message normal_messages[] =
2587 {WM_DISPLAYCHANGE, FALSE, 0},
2588 {0, FALSE, 0},
2591 ddraw = create_ddraw();
2592 ok(!!ddraw, "Failed to create a ddraw object.\n");
2594 memset(&param, 0, sizeof(param));
2595 hr = IDirectDraw2_EnumDisplayModes(ddraw, 0, NULL, &param, test_coop_level_mode_set_enum_cb);
2596 ok(SUCCEEDED(hr), "Failed to enumerate display mode, hr %#x.\n", hr);
2597 ref = IDirectDraw2_Release(ddraw);
2598 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2600 if (!param.user32_height)
2602 skip("Fewer than 3 different modes supported, skipping mode restore test.\n");
2603 return;
2606 SetRect(&registry_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2607 SetRect(&ddraw_rect, 0, 0, param.ddraw_width, param.ddraw_height);
2608 SetRect(&user32_rect, 0, 0, param.user32_width, param.user32_height);
2610 memset(&devmode, 0, sizeof(devmode));
2611 devmode.dmSize = sizeof(devmode);
2612 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2613 devmode.dmPelsWidth = param.user32_width;
2614 devmode.dmPelsHeight = param.user32_height;
2615 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2616 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2618 ddraw = create_ddraw();
2619 ok(!!ddraw, "Failed to create a ddraw object.\n");
2621 wc.lpfnWndProc = mode_set_proc;
2622 wc.lpszClassName = "ddraw_test_wndproc_wc";
2623 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2624 wc.lpfnWndProc = mode_set_proc2;
2625 wc.lpszClassName = "ddraw_test_wndproc_wc2";
2626 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2628 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test", WS_OVERLAPPEDWINDOW,
2629 0, 0, 100, 100, 0, 0, 0, 0);
2630 window2 = CreateWindowA("ddraw_test_wndproc_wc2", "ddraw_test", WS_OVERLAPPEDWINDOW,
2631 0, 0, 100, 100, 0, 0, 0, 0);
2633 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2634 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2636 GetWindowRect(window, &r);
2637 ok(EqualRect(&r, &user32_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&user32_rect),
2638 wine_dbgstr_rect(&r));
2640 memset(&ddsd, 0, sizeof(ddsd));
2641 ddsd.dwSize = sizeof(ddsd);
2642 ddsd.dwFlags = DDSD_CAPS;
2643 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2645 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
2646 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2647 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2648 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2649 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2650 param.user32_width, ddsd.dwWidth);
2651 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2652 param.user32_height, ddsd.dwHeight);
2654 GetWindowRect(window, &r);
2655 ok(EqualRect(&r, &user32_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&user32_rect),
2656 wine_dbgstr_rect(&r));
2658 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2659 expect_messages = exclusive_messages;
2660 screen_size.cx = 0;
2661 screen_size.cy = 0;
2663 hr = IDirectDrawSurface_IsLost(primary);
2664 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2665 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2666 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2667 hr = IDirectDrawSurface_IsLost(primary);
2668 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2670 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2671 expect_messages = NULL;
2672 ok(screen_size.cx == param.ddraw_width && screen_size.cy == param.ddraw_height,
2673 "Expected screen size %ux%u, got %ux%u.\n",
2674 param.ddraw_width, param.ddraw_height, screen_size.cx, screen_size.cy);
2676 GetWindowRect(window, &r);
2677 ok(EqualRect(&r, &ddraw_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&ddraw_rect),
2678 wine_dbgstr_rect(&r));
2680 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2681 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2682 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2683 param.user32_width, ddsd.dwWidth);
2684 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2685 param.user32_height, ddsd.dwHeight);
2686 IDirectDrawSurface_Release(primary);
2688 memset(&ddsd, 0, sizeof(ddsd));
2689 ddsd.dwSize = sizeof(ddsd);
2690 ddsd.dwFlags = DDSD_CAPS;
2691 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2693 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
2694 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2695 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2696 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2697 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2698 param.ddraw_width, ddsd.dwWidth);
2699 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2700 param.ddraw_height, ddsd.dwHeight);
2702 GetWindowRect(window, &r);
2703 ok(EqualRect(&r, &ddraw_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&ddraw_rect),
2704 wine_dbgstr_rect(&r));
2706 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2707 expect_messages = exclusive_messages;
2708 screen_size.cx = 0;
2709 screen_size.cy = 0;
2711 hr = IDirectDrawSurface_IsLost(primary);
2712 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2713 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2714 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2715 hr = IDirectDrawSurface_IsLost(primary);
2716 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2718 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2719 expect_messages = NULL;
2720 ok(screen_size.cx == param.user32_width && screen_size.cy == param.user32_height,
2721 "Expected screen size %ux%u, got %ux%u.\n",
2722 param.user32_width, param.user32_height, screen_size.cx, screen_size.cy);
2724 GetWindowRect(window, &r);
2725 ok(EqualRect(&r, &user32_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&user32_rect),
2726 wine_dbgstr_rect(&r));
2728 expect_messages = exclusive_focus_loss_messages;
2729 ret = SetForegroundWindow(GetDesktopWindow());
2730 ok(ret, "Failed to set foreground window.\n");
2731 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2732 memset(&devmode, 0, sizeof(devmode));
2733 devmode.dmSize = sizeof(devmode);
2734 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2735 ok(ret, "Failed to get display mode.\n");
2736 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
2737 && devmode.dmPelsHeight == registry_mode.dmPelsHeight, "Got unexpect screen size %ux%u.\n",
2738 devmode.dmPelsWidth, devmode.dmPelsHeight);
2740 expect_messages = exclusive_focus_restore_messages;
2741 ShowWindow(window, SW_RESTORE);
2742 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2744 GetWindowRect(window, &r);
2745 ok(EqualRect(&r, &ddraw_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&ddraw_rect),
2746 wine_dbgstr_rect(&r));
2747 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2748 ok(ret, "Failed to get display mode.\n");
2749 ok(devmode.dmPelsWidth == param.ddraw_width
2750 && devmode.dmPelsHeight == param.ddraw_height, "Got unexpect screen size %ux%u.\n",
2751 devmode.dmPelsWidth, devmode.dmPelsHeight);
2753 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2754 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2755 /* Normally the primary should be restored here. Unfortunately this causes the
2756 * GetSurfaceDesc call after the next display mode change to crash on the Windows 8
2757 * testbot. Another Restore call would presumably avoid the crash, but it also moots
2758 * the point of the GetSurfaceDesc call. */
2760 expect_messages = sc_minimize_messages;
2761 SendMessageA(window, WM_SYSCOMMAND, SC_MINIMIZE, 0);
2762 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2763 expect_messages = NULL;
2765 expect_messages = sc_restore_messages;
2766 SendMessageA(window, WM_SYSCOMMAND, SC_RESTORE, 0);
2767 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2768 expect_messages = NULL;
2770 expect_messages = sc_maximize_messages;
2771 SendMessageA(window, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
2772 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2773 expect_messages = NULL;
2775 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2776 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2778 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2779 expect_messages = exclusive_messages;
2780 screen_size.cx = 0;
2781 screen_size.cy = 0;
2783 hr = IDirectDrawSurface_IsLost(primary);
2784 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2785 hr = IDirectDraw2_RestoreDisplayMode(ddraw);
2786 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2787 hr = IDirectDrawSurface_IsLost(primary);
2788 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2790 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2791 expect_messages = NULL;
2792 ok(screen_size.cx == registry_mode.dmPelsWidth
2793 && screen_size.cy == registry_mode.dmPelsHeight,
2794 "Expected screen size %ux%u, got %ux%u.\n",
2795 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight, screen_size.cx, screen_size.cy);
2797 GetWindowRect(window, &r);
2798 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
2799 wine_dbgstr_rect(&r));
2801 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2802 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2803 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2804 param.ddraw_width, ddsd.dwWidth);
2805 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2806 param.ddraw_height, ddsd.dwHeight);
2807 IDirectDrawSurface_Release(primary);
2809 /* For Wine. */
2810 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
2811 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2813 memset(&ddsd, 0, sizeof(ddsd));
2814 ddsd.dwSize = sizeof(ddsd);
2815 ddsd.dwFlags = DDSD_CAPS;
2816 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2818 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
2819 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2820 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2821 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2822 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2823 registry_mode.dmPelsWidth, ddsd.dwWidth);
2824 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2825 registry_mode.dmPelsHeight, ddsd.dwHeight);
2827 GetWindowRect(window, &r);
2828 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
2829 wine_dbgstr_rect(&r));
2831 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2832 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2834 GetWindowRect(window, &r);
2835 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
2836 wine_dbgstr_rect(&r));
2838 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2839 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2840 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2841 registry_mode.dmPelsWidth, ddsd.dwWidth);
2842 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2843 registry_mode.dmPelsHeight, ddsd.dwHeight);
2844 IDirectDrawSurface_Release(primary);
2846 memset(&ddsd, 0, sizeof(ddsd));
2847 ddsd.dwSize = sizeof(ddsd);
2848 ddsd.dwFlags = DDSD_CAPS;
2849 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2851 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
2852 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2853 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2854 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2855 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2856 registry_mode.dmPelsWidth, ddsd.dwWidth);
2857 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2858 registry_mode.dmPelsHeight, ddsd.dwHeight);
2860 GetWindowRect(window, &r);
2861 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
2862 wine_dbgstr_rect(&r));
2864 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2865 expect_messages = normal_messages;
2866 screen_size.cx = 0;
2867 screen_size.cy = 0;
2869 hr = IDirectDrawSurface_IsLost(primary);
2870 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2871 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2872 devmode.dmPelsWidth = param.user32_width;
2873 devmode.dmPelsHeight = param.user32_height;
2874 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2875 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2876 hr = IDirectDrawSurface_IsLost(primary);
2877 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2879 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2880 expect_messages = NULL;
2881 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2883 GetWindowRect(window, &r);
2884 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
2885 wine_dbgstr_rect(&r));
2887 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2888 expect_messages = normal_messages;
2889 screen_size.cx = 0;
2890 screen_size.cy = 0;
2892 hr = IDirectDrawSurface_Restore(primary);
2893 ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
2894 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2895 if (hr == DDERR_NOEXCLUSIVEMODE /* NT4 testbot */)
2897 win_skip("Broken SetDisplayMode(), skipping remaining tests.\n");
2898 IDirectDrawSurface_Release(primary);
2899 IDirectDraw2_Release(ddraw);
2900 goto done;
2902 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2903 hr = IDirectDrawSurface_Restore(primary);
2904 ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
2905 hr = IDirectDrawSurface_IsLost(primary);
2906 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2908 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2909 expect_messages = NULL;
2910 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2912 GetWindowRect(window, &r);
2913 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
2914 wine_dbgstr_rect(&r));
2916 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2917 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2918 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2919 registry_mode.dmPelsWidth, ddsd.dwWidth);
2920 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2921 registry_mode.dmPelsHeight, ddsd.dwHeight);
2922 IDirectDrawSurface_Release(primary);
2924 memset(&ddsd, 0, sizeof(ddsd));
2925 ddsd.dwSize = sizeof(ddsd);
2926 ddsd.dwFlags = DDSD_CAPS;
2927 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2929 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
2930 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2931 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2932 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2933 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2934 param.ddraw_width, ddsd.dwWidth);
2935 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2936 param.ddraw_height, ddsd.dwHeight);
2938 GetWindowRect(window, &r);
2939 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
2940 wine_dbgstr_rect(&r));
2942 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2943 expect_messages = normal_messages;
2944 screen_size.cx = 0;
2945 screen_size.cy = 0;
2947 hr = IDirectDrawSurface_IsLost(primary);
2948 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2949 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2950 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2951 hr = IDirectDrawSurface_IsLost(primary);
2952 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2954 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2955 expect_messages = NULL;
2956 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2958 GetWindowRect(window, &r);
2959 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
2960 wine_dbgstr_rect(&r));
2962 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2963 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2964 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2965 param.ddraw_width, ddsd.dwWidth);
2966 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2967 param.ddraw_height, ddsd.dwHeight);
2968 IDirectDrawSurface_Release(primary);
2970 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2971 ok(ret, "Failed to get display mode.\n");
2972 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
2973 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
2974 "Expected resolution %ux%u, got %ux%u.\n",
2975 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
2976 devmode.dmPelsWidth, devmode.dmPelsHeight);
2977 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
2978 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2980 memset(&ddsd, 0, sizeof(ddsd));
2981 ddsd.dwSize = sizeof(ddsd);
2982 ddsd.dwFlags = DDSD_CAPS;
2983 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2985 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
2986 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2987 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2988 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2989 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2990 registry_mode.dmPelsWidth, ddsd.dwWidth);
2991 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2992 registry_mode.dmPelsHeight, ddsd.dwHeight);
2994 GetWindowRect(window, &r);
2995 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
2996 wine_dbgstr_rect(&r));
2998 /* DDSCL_NORMAL | DDSCL_FULLSCREEN behaves the same as just DDSCL_NORMAL.
2999 * Resizing the window on mode changes is a property of DDSCL_EXCLUSIVE,
3000 * not DDSCL_FULLSCREEN. */
3001 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
3002 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3004 GetWindowRect(window, &r);
3005 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
3006 wine_dbgstr_rect(&r));
3008 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
3009 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3010 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3011 registry_mode.dmPelsWidth, ddsd.dwWidth);
3012 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3013 registry_mode.dmPelsHeight, ddsd.dwHeight);
3014 IDirectDrawSurface_Release(primary);
3016 memset(&ddsd, 0, sizeof(ddsd));
3017 ddsd.dwSize = sizeof(ddsd);
3018 ddsd.dwFlags = DDSD_CAPS;
3019 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3021 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
3022 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3023 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
3024 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3025 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3026 registry_mode.dmPelsWidth, ddsd.dwWidth);
3027 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3028 registry_mode.dmPelsHeight, ddsd.dwHeight);
3030 GetWindowRect(window, &r);
3031 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
3032 wine_dbgstr_rect(&r));
3034 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3035 expect_messages = normal_messages;
3036 screen_size.cx = 0;
3037 screen_size.cy = 0;
3039 hr = IDirectDrawSurface_IsLost(primary);
3040 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
3041 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
3042 devmode.dmPelsWidth = param.user32_width;
3043 devmode.dmPelsHeight = param.user32_height;
3044 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
3045 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
3046 hr = IDirectDrawSurface_IsLost(primary);
3047 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
3049 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3050 expect_messages = NULL;
3051 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3053 GetWindowRect(window, &r);
3054 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
3055 wine_dbgstr_rect(&r));
3057 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3058 expect_messages = normal_messages;
3059 screen_size.cx = 0;
3060 screen_size.cy = 0;
3062 hr = IDirectDrawSurface_Restore(primary);
3063 ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
3064 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3065 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3066 hr = IDirectDrawSurface_Restore(primary);
3067 ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
3068 hr = IDirectDrawSurface_IsLost(primary);
3069 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
3071 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3072 expect_messages = NULL;
3073 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3075 GetWindowRect(window, &r);
3076 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
3077 wine_dbgstr_rect(&r));
3079 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
3080 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3081 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3082 registry_mode.dmPelsWidth, ddsd.dwWidth);
3083 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3084 registry_mode.dmPelsHeight, ddsd.dwHeight);
3085 IDirectDrawSurface_Release(primary);
3087 memset(&ddsd, 0, sizeof(ddsd));
3088 ddsd.dwSize = sizeof(ddsd);
3089 ddsd.dwFlags = DDSD_CAPS;
3090 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3092 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
3093 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3094 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
3095 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3096 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3097 param.ddraw_width, ddsd.dwWidth);
3098 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3099 param.ddraw_height, ddsd.dwHeight);
3101 GetWindowRect(window, &r);
3102 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
3103 wine_dbgstr_rect(&r));
3105 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3106 expect_messages = normal_messages;
3107 screen_size.cx = 0;
3108 screen_size.cy = 0;
3110 hr = IDirectDrawSurface_IsLost(primary);
3111 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
3112 hr = IDirectDraw2_RestoreDisplayMode(ddraw);
3113 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3114 hr = IDirectDrawSurface_IsLost(primary);
3115 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
3117 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3118 expect_messages = NULL;
3119 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3121 GetWindowRect(window, &r);
3122 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
3123 wine_dbgstr_rect(&r));
3125 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
3126 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3127 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3128 param.ddraw_width, ddsd.dwWidth);
3129 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3130 param.ddraw_height, ddsd.dwHeight);
3131 IDirectDrawSurface_Release(primary);
3133 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3134 ok(ret, "Failed to get display mode.\n");
3135 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
3136 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
3137 "Expected resolution %ux%u, got %ux%u.\n",
3138 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
3139 devmode.dmPelsWidth, devmode.dmPelsHeight);
3140 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
3141 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
3143 memset(&ddsd, 0, sizeof(ddsd));
3144 ddsd.dwSize = sizeof(ddsd);
3145 ddsd.dwFlags = DDSD_CAPS;
3146 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3148 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
3149 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3150 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
3151 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3152 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3153 registry_mode.dmPelsWidth, ddsd.dwWidth);
3154 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3155 registry_mode.dmPelsHeight, ddsd.dwHeight);
3156 IDirectDrawSurface_Release(primary);
3158 GetWindowRect(window, &r);
3159 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
3160 wine_dbgstr_rect(&r));
3162 /* Changing the coop level from EXCLUSIVE to NORMAL restores the screen resolution */
3163 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3164 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3165 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3166 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3168 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3169 expect_messages = exclusive_messages;
3170 screen_size.cx = 0;
3171 screen_size.cy = 0;
3173 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3174 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3176 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3177 expect_messages = NULL;
3178 ok(screen_size.cx == registry_mode.dmPelsWidth
3179 && screen_size.cy == registry_mode.dmPelsHeight,
3180 "Expected screen size %ux%u, got %ux%u.\n",
3181 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
3182 screen_size.cx, screen_size.cy);
3184 GetWindowRect(window, &r);
3185 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
3186 wine_dbgstr_rect(&r));
3188 memset(&ddsd, 0, sizeof(ddsd));
3189 ddsd.dwSize = sizeof(ddsd);
3190 ddsd.dwFlags = DDSD_CAPS;
3191 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3193 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
3194 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3195 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
3196 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3197 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3198 registry_mode.dmPelsWidth, ddsd.dwWidth);
3199 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3200 registry_mode.dmPelsHeight, ddsd.dwHeight);
3201 IDirectDrawSurface_Release(primary);
3203 /* The screen restore is a property of DDSCL_EXCLUSIVE */
3204 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
3205 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3206 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3207 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3209 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3210 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3212 memset(&ddsd, 0, sizeof(ddsd));
3213 ddsd.dwSize = sizeof(ddsd);
3214 ddsd.dwFlags = DDSD_CAPS;
3215 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3217 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
3218 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3219 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
3220 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3221 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3222 param.ddraw_width, ddsd.dwWidth);
3223 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3224 param.ddraw_height, ddsd.dwHeight);
3225 IDirectDrawSurface_Release(primary);
3227 hr = IDirectDraw2_RestoreDisplayMode(ddraw);
3228 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3230 /* If the window is changed at the same time, messages are sent to the new window. */
3231 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3232 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3233 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3234 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3236 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3237 expect_messages = exclusive_messages;
3238 screen_size.cx = 0;
3239 screen_size.cy = 0;
3240 screen_size2.cx = 0;
3241 screen_size2.cy = 0;
3243 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
3244 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3246 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3247 expect_messages = NULL;
3248 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n",
3249 screen_size.cx, screen_size.cy);
3250 ok(screen_size2.cx == registry_mode.dmPelsWidth && screen_size2.cy == registry_mode.dmPelsHeight,
3251 "Expected screen size 2 %ux%u, got %ux%u.\n",
3252 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight, screen_size2.cx, screen_size2.cy);
3254 GetWindowRect(window, &r);
3255 ok(EqualRect(&r, &ddraw_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&ddraw_rect),
3256 wine_dbgstr_rect(&r));
3257 GetWindowRect(window2, &r);
3258 ok(EqualRect(&r, &registry_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&registry_rect),
3259 wine_dbgstr_rect(&r));
3261 memset(&ddsd, 0, sizeof(ddsd));
3262 ddsd.dwSize = sizeof(ddsd);
3263 ddsd.dwFlags = DDSD_CAPS;
3264 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3266 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
3267 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3268 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
3269 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3270 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3271 registry_mode.dmPelsWidth, ddsd.dwWidth);
3272 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3273 registry_mode.dmPelsHeight, ddsd.dwHeight);
3274 IDirectDrawSurface_Release(primary);
3276 ref = IDirectDraw2_Release(ddraw);
3277 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3279 GetWindowRect(window, &r);
3280 ok(EqualRect(&r, &ddraw_rect), "Expected %s, got %s.\n", wine_dbgstr_rect(&ddraw_rect),
3281 wine_dbgstr_rect(&r));
3283 done:
3284 expect_messages = NULL;
3285 DestroyWindow(window);
3286 DestroyWindow(window2);
3287 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
3288 UnregisterClassA("ddraw_test_wndproc_wc2", GetModuleHandleA(NULL));
3291 static void test_coop_level_mode_set_multi(void)
3293 IDirectDraw2 *ddraw1, *ddraw2;
3294 UINT w, h;
3295 HWND window;
3296 HRESULT hr;
3297 ULONG ref;
3299 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3300 0, 0, 100, 100, 0, 0, 0, 0);
3301 ddraw1 = create_ddraw();
3302 ok(!!ddraw1, "Failed to create a ddraw object.\n");
3304 /* With just a single ddraw object, the display mode is restored on
3305 * release. */
3306 hr = set_display_mode(ddraw1, 800, 600);
3307 if (hr == DDERR_NOEXCLUSIVEMODE /* NT4 testbot */)
3309 win_skip("Broken SetDisplayMode(), skipping test.\n");
3310 IDirectDraw2_Release(ddraw1);
3311 DestroyWindow(window);
3312 return;
3314 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3315 w = GetSystemMetrics(SM_CXSCREEN);
3316 ok(w == 800, "Got unexpected screen width %u.\n", w);
3317 h = GetSystemMetrics(SM_CYSCREEN);
3318 ok(h == 600, "Got unexpected screen height %u.\n", h);
3320 ref = IDirectDraw2_Release(ddraw1);
3321 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3322 w = GetSystemMetrics(SM_CXSCREEN);
3323 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3324 h = GetSystemMetrics(SM_CYSCREEN);
3325 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3327 /* When there are multiple ddraw objects, the display mode is restored to
3328 * the initial mode, before the first SetDisplayMode() call. */
3329 ddraw1 = create_ddraw();
3330 hr = set_display_mode(ddraw1, 800, 600);
3331 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3332 w = GetSystemMetrics(SM_CXSCREEN);
3333 ok(w == 800, "Got unexpected screen width %u.\n", w);
3334 h = GetSystemMetrics(SM_CYSCREEN);
3335 ok(h == 600, "Got unexpected screen height %u.\n", h);
3337 ddraw2 = create_ddraw();
3338 hr = set_display_mode(ddraw2, 640, 480);
3339 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3340 w = GetSystemMetrics(SM_CXSCREEN);
3341 ok(w == 640, "Got unexpected screen width %u.\n", w);
3342 h = GetSystemMetrics(SM_CYSCREEN);
3343 ok(h == 480, "Got unexpected screen height %u.\n", h);
3345 ref = IDirectDraw2_Release(ddraw2);
3346 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3347 w = GetSystemMetrics(SM_CXSCREEN);
3348 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3349 h = GetSystemMetrics(SM_CYSCREEN);
3350 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3352 ref = IDirectDraw2_Release(ddraw1);
3353 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3354 w = GetSystemMetrics(SM_CXSCREEN);
3355 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3356 h = GetSystemMetrics(SM_CYSCREEN);
3357 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3359 /* Regardless of release ordering. */
3360 ddraw1 = create_ddraw();
3361 hr = set_display_mode(ddraw1, 800, 600);
3362 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3363 w = GetSystemMetrics(SM_CXSCREEN);
3364 ok(w == 800, "Got unexpected screen width %u.\n", w);
3365 h = GetSystemMetrics(SM_CYSCREEN);
3366 ok(h == 600, "Got unexpected screen height %u.\n", h);
3368 ddraw2 = create_ddraw();
3369 hr = set_display_mode(ddraw2, 640, 480);
3370 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3371 w = GetSystemMetrics(SM_CXSCREEN);
3372 ok(w == 640, "Got unexpected screen width %u.\n", w);
3373 h = GetSystemMetrics(SM_CYSCREEN);
3374 ok(h == 480, "Got unexpected screen height %u.\n", h);
3376 ref = IDirectDraw2_Release(ddraw1);
3377 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3378 w = GetSystemMetrics(SM_CXSCREEN);
3379 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3380 h = GetSystemMetrics(SM_CYSCREEN);
3381 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3383 ref = IDirectDraw2_Release(ddraw2);
3384 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3385 w = GetSystemMetrics(SM_CXSCREEN);
3386 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3387 h = GetSystemMetrics(SM_CYSCREEN);
3388 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3390 /* But only for ddraw objects that called SetDisplayMode(). */
3391 ddraw1 = create_ddraw();
3392 ddraw2 = create_ddraw();
3393 hr = set_display_mode(ddraw2, 640, 480);
3394 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3395 w = GetSystemMetrics(SM_CXSCREEN);
3396 ok(w == 640, "Got unexpected screen width %u.\n", w);
3397 h = GetSystemMetrics(SM_CYSCREEN);
3398 ok(h == 480, "Got unexpected screen height %u.\n", h);
3400 ref = IDirectDraw2_Release(ddraw1);
3401 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3402 w = GetSystemMetrics(SM_CXSCREEN);
3403 ok(w == 640, "Got unexpected screen width %u.\n", w);
3404 h = GetSystemMetrics(SM_CYSCREEN);
3405 ok(h == 480, "Got unexpected screen height %u.\n", h);
3407 ref = IDirectDraw2_Release(ddraw2);
3408 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3409 w = GetSystemMetrics(SM_CXSCREEN);
3410 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3411 h = GetSystemMetrics(SM_CYSCREEN);
3412 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3414 /* If there's a ddraw object that's currently in exclusive mode, it blocks
3415 * restoring the display mode. */
3416 ddraw1 = create_ddraw();
3417 hr = set_display_mode(ddraw1, 800, 600);
3418 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3419 w = GetSystemMetrics(SM_CXSCREEN);
3420 ok(w == 800, "Got unexpected screen width %u.\n", w);
3421 h = GetSystemMetrics(SM_CYSCREEN);
3422 ok(h == 600, "Got unexpected screen height %u.\n", h);
3424 ddraw2 = create_ddraw();
3425 hr = set_display_mode(ddraw2, 640, 480);
3426 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3427 w = GetSystemMetrics(SM_CXSCREEN);
3428 ok(w == 640, "Got unexpected screen width %u.\n", w);
3429 h = GetSystemMetrics(SM_CYSCREEN);
3430 ok(h == 480, "Got unexpected screen height %u.\n", h);
3432 hr = IDirectDraw2_SetCooperativeLevel(ddraw2, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3433 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3435 ref = IDirectDraw2_Release(ddraw1);
3436 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3437 w = GetSystemMetrics(SM_CXSCREEN);
3438 ok(w == 640, "Got unexpected screen width %u.\n", w);
3439 h = GetSystemMetrics(SM_CYSCREEN);
3440 ok(h == 480, "Got unexpected screen height %u.\n", h);
3442 ref = IDirectDraw2_Release(ddraw2);
3443 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3444 w = GetSystemMetrics(SM_CXSCREEN);
3445 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3446 h = GetSystemMetrics(SM_CYSCREEN);
3447 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3449 /* Exclusive mode blocks mode setting on other ddraw objects in general. */
3450 ddraw1 = create_ddraw();
3451 hr = set_display_mode(ddraw1, 800, 600);
3452 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3453 w = GetSystemMetrics(SM_CXSCREEN);
3454 ok(w == 800, "Got unexpected screen width %u.\n", w);
3455 h = GetSystemMetrics(SM_CYSCREEN);
3456 ok(h == 600, "Got unexpected screen height %u.\n", h);
3458 hr = IDirectDraw2_SetCooperativeLevel(ddraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3459 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3461 ddraw2 = create_ddraw();
3462 hr = set_display_mode(ddraw2, 640, 480);
3463 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
3465 ref = IDirectDraw2_Release(ddraw1);
3466 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3467 w = GetSystemMetrics(SM_CXSCREEN);
3468 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3469 h = GetSystemMetrics(SM_CYSCREEN);
3470 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3472 ref = IDirectDraw2_Release(ddraw2);
3473 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3474 w = GetSystemMetrics(SM_CXSCREEN);
3475 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3476 h = GetSystemMetrics(SM_CYSCREEN);
3477 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3479 DestroyWindow(window);
3482 static void test_initialize(void)
3484 IDirectDraw2 *ddraw;
3485 HRESULT hr;
3487 ddraw = create_ddraw();
3488 ok(!!ddraw, "Failed to create a ddraw object.\n");
3490 hr = IDirectDraw2_Initialize(ddraw, NULL);
3491 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x.\n", hr);
3492 IDirectDraw2_Release(ddraw);
3494 CoInitialize(NULL);
3495 hr = CoCreateInstance(&CLSID_DirectDraw, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectDraw2, (void **)&ddraw);
3496 ok(SUCCEEDED(hr), "Failed to create IDirectDraw2 instance, hr %#x.\n", hr);
3497 hr = IDirectDraw2_Initialize(ddraw, NULL);
3498 ok(hr == DD_OK, "Initialize returned hr %#x, expected DD_OK.\n", hr);
3499 hr = IDirectDraw2_Initialize(ddraw, NULL);
3500 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x, expected DDERR_ALREADYINITIALIZED.\n", hr);
3501 IDirectDraw2_Release(ddraw);
3502 CoUninitialize();
3505 static void test_coop_level_surf_create(void)
3507 IDirectDrawSurface *surface;
3508 IDirectDraw2 *ddraw;
3509 DDSURFACEDESC ddsd;
3510 HRESULT hr;
3512 ddraw = create_ddraw();
3513 ok(!!ddraw, "Failed to create a ddraw object.\n");
3515 memset(&ddsd, 0, sizeof(ddsd));
3516 ddsd.dwSize = sizeof(ddsd);
3517 ddsd.dwFlags = DDSD_CAPS;
3518 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3519 surface = (void *)0xdeadbeef;
3520 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
3521 ok(hr == DDERR_NOCOOPERATIVELEVELSET, "Surface creation returned hr %#x.\n", hr);
3522 ok(surface == (void *)0xdeadbeef, "Got unexpected surface %p.\n", surface);
3524 surface = (void *)0xdeadbeef;
3525 hr = IDirectDraw2_CreateSurface(ddraw, NULL, &surface, NULL);
3526 ok(hr == DDERR_NOCOOPERATIVELEVELSET, "Surface creation returned hr %#x.\n", hr);
3527 ok(surface == (void *)0xdeadbeef, "Got unexpected surface %p.\n", surface);
3529 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3530 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3532 surface = (void *)0xdeadbeef;
3533 hr = IDirectDraw2_CreateSurface(ddraw, NULL, &surface, NULL);
3534 ok(hr == DDERR_INVALIDPARAMS, "Unexpected hr %#x.\n", hr);
3535 ok(surface == (void *)0xdeadbeef, "Got unexpected surface %p.\n", surface);
3537 IDirectDraw2_Release(ddraw);
3540 static void test_coop_level_multi_window(void)
3542 HWND window1, window2;
3543 IDirectDraw2 *ddraw;
3544 HRESULT hr;
3546 window1 = create_window();
3547 window2 = create_window();
3548 ddraw = create_ddraw();
3549 ok(!!ddraw, "Failed to create a ddraw object.\n");
3551 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
3552 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3553 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
3554 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3555 ok(IsWindow(window1), "Window 1 was destroyed.\n");
3556 ok(IsWindow(window2), "Window 2 was destroyed.\n");
3558 IDirectDraw2_Release(ddraw);
3559 DestroyWindow(window2);
3560 DestroyWindow(window1);
3563 static void test_clear_rect_count(void)
3565 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
3566 IDirect3DMaterial2 *white, *red, *green, *blue;
3567 IDirect3DViewport2 *viewport;
3568 IDirect3DDevice2 *device;
3569 IDirectDrawSurface *rt;
3570 IDirectDraw2 *ddraw;
3571 D3DCOLOR color;
3572 HWND window;
3573 HRESULT hr;
3575 window = create_window();
3576 ddraw = create_ddraw();
3577 ok(!!ddraw, "Failed to create a ddraw object.\n");
3578 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
3580 skip("Failed to create a 3D device, skipping test.\n");
3581 IDirectDraw2_Release(ddraw);
3582 DestroyWindow(window);
3583 return;
3586 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
3587 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3589 white = create_diffuse_material(device, 1.0f, 1.0f, 1.0f, 1.0f);
3590 red = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
3591 green = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 1.0f);
3592 blue = create_diffuse_material(device, 0.0f, 0.0f, 1.0f, 1.0f);
3594 viewport = create_viewport(device, 0, 0, 640, 480);
3595 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
3596 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
3598 viewport_set_background(device, viewport, white);
3599 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
3600 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3601 viewport_set_background(device, viewport, red);
3602 hr = IDirect3DViewport2_Clear(viewport, 0, &clear_rect, D3DCLEAR_TARGET);
3603 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3604 viewport_set_background(device, viewport, green);
3605 hr = IDirect3DViewport2_Clear(viewport, 0, NULL, D3DCLEAR_TARGET);
3606 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3607 viewport_set_background(device, viewport, blue);
3608 hr = IDirect3DViewport2_Clear(viewport, 0, &clear_rect, D3DCLEAR_TARGET);
3609 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3611 color = get_surface_color(rt, 320, 240);
3612 ok(compare_color(color, 0x00ffffff, 1) || broken(compare_color(color, 0x000000ff, 1)),
3613 "Got unexpected color 0x%08x.\n", color);
3615 IDirectDrawSurface_Release(rt);
3616 destroy_viewport(device, viewport);
3617 destroy_material(white);
3618 destroy_material(red);
3619 destroy_material(green);
3620 destroy_material(blue);
3621 IDirect3DDevice2_Release(device);
3622 IDirectDraw2_Release(ddraw);
3623 DestroyWindow(window);
3626 static BOOL test_mode_restored(IDirectDraw2 *ddraw, HWND window)
3628 DDSURFACEDESC ddsd1, ddsd2;
3629 HRESULT hr;
3631 memset(&ddsd1, 0, sizeof(ddsd1));
3632 ddsd1.dwSize = sizeof(ddsd1);
3633 hr = IDirectDraw2_GetDisplayMode(ddraw, &ddsd1);
3634 ok(SUCCEEDED(hr), "GetDisplayMode failed, hr %#x.\n", hr);
3636 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3637 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3638 hr = set_display_mode(ddraw, 640, 480);
3639 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3640 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3641 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3643 memset(&ddsd2, 0, sizeof(ddsd2));
3644 ddsd2.dwSize = sizeof(ddsd2);
3645 hr = IDirectDraw2_GetDisplayMode(ddraw, &ddsd2);
3646 ok(SUCCEEDED(hr), "GetDisplayMode failed, hr %#x.\n", hr);
3647 hr = IDirectDraw2_RestoreDisplayMode(ddraw);
3648 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3650 return ddsd1.dwWidth == ddsd2.dwWidth && ddsd1.dwHeight == ddsd2.dwHeight;
3653 static void test_coop_level_versions(void)
3655 HWND window;
3656 IDirectDraw *ddraw;
3657 HRESULT hr;
3658 BOOL restored;
3659 IDirectDrawSurface *surface;
3660 IDirectDraw2 *ddraw2;
3661 DDSURFACEDESC ddsd;
3663 window = create_window();
3664 ddraw2 = create_ddraw();
3665 ok(!!ddraw2, "Failed to create a ddraw object.\n");
3666 /* Newly created ddraw objects restore the mode on ddraw2+::SetCooperativeLevel(NORMAL) */
3667 restored = test_mode_restored(ddraw2, window);
3668 ok(restored, "Display mode not restored in new ddraw object\n");
3670 /* A failing ddraw1::SetCooperativeLevel call does not have an effect */
3671 hr = IDirectDraw2_QueryInterface(ddraw2, &IID_IDirectDraw, (void **)&ddraw);
3672 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3674 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
3675 ok(FAILED(hr), "SetCooperativeLevel returned %#x, expected failure.\n", hr);
3676 restored = test_mode_restored(ddraw2, window);
3677 ok(restored, "Display mode not restored after bad ddraw1::SetCooperativeLevel call\n");
3679 /* A successful one does */
3680 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3681 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3682 restored = test_mode_restored(ddraw2, window);
3683 ok(!restored, "Display mode restored after good ddraw1::SetCooperativeLevel call\n");
3685 IDirectDraw_Release(ddraw);
3686 IDirectDraw2_Release(ddraw2);
3688 ddraw2 = create_ddraw();
3689 ok(!!ddraw2, "Failed to create a ddraw object.\n");
3690 hr = IDirectDraw2_QueryInterface(ddraw2, &IID_IDirectDraw, (void **)&ddraw);
3691 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3693 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_SETFOCUSWINDOW);
3694 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3695 restored = test_mode_restored(ddraw2, window);
3696 ok(!restored, "Display mode restored after ddraw1::SetCooperativeLevel(SETFOCUSWINDOW) call\n");
3698 IDirectDraw_Release(ddraw);
3699 IDirectDraw2_Release(ddraw2);
3701 /* A failing call does not restore the ddraw2+ behavior */
3702 ddraw2 = create_ddraw();
3703 ok(!!ddraw2, "Failed to create a ddraw object.\n");
3704 hr = IDirectDraw2_QueryInterface(ddraw2, &IID_IDirectDraw, (void **)&ddraw);
3705 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3707 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3708 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3709 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
3710 ok(FAILED(hr), "SetCooperativeLevel returned %#x, expected failure.\n", hr);
3711 restored = test_mode_restored(ddraw2, window);
3712 ok(!restored, "Display mode restored after good-bad ddraw1::SetCooperativeLevel() call sequence\n");
3714 IDirectDraw_Release(ddraw);
3715 IDirectDraw2_Release(ddraw2);
3717 /* Neither does a sequence of successful calls with the new interface */
3718 ddraw2 = create_ddraw();
3719 ok(!!ddraw2, "Failed to create a ddraw object.\n");
3720 hr = IDirectDraw2_QueryInterface(ddraw2, &IID_IDirectDraw, (void **)&ddraw);
3721 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3723 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3724 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3725 hr = IDirectDraw2_SetCooperativeLevel(ddraw2, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
3726 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3727 hr = IDirectDraw2_SetCooperativeLevel(ddraw2, window, DDSCL_NORMAL);
3728 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3730 restored = test_mode_restored(ddraw2, window);
3731 ok(!restored, "Display mode restored after ddraw1-ddraw2 SetCooperativeLevel() call sequence\n");
3732 IDirectDraw_Release(ddraw);
3733 IDirectDraw2_Release(ddraw2);
3735 /* ddraw1::CreateSurface does not triger the ddraw1 behavior */
3736 ddraw2 = create_ddraw();
3737 ok(!!ddraw2, "Failed to create a ddraw object.\n");
3738 hr = IDirectDraw2_QueryInterface(ddraw2, &IID_IDirectDraw, (void **)&ddraw);
3739 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3741 hr = IDirectDraw2_SetCooperativeLevel(ddraw2, window, DDSCL_NORMAL);
3742 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3744 memset(&ddsd, 0, sizeof(ddsd));
3745 ddsd.dwSize = sizeof(ddsd);
3746 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3747 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3748 ddsd.dwWidth = ddsd.dwHeight = 8;
3749 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
3750 ok(SUCCEEDED(hr), "CreateSurface failed, hr %#x.\n", hr);
3751 IDirectDrawSurface_Release(surface);
3752 restored = test_mode_restored(ddraw2, window);
3753 ok(restored, "Display mode not restored after ddraw1::CreateSurface() call\n");
3755 IDirectDraw_Release(ddraw);
3756 IDirectDraw2_Release(ddraw2);
3757 DestroyWindow(window);
3760 static void test_lighting_interface_versions(void)
3762 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
3763 IDirect3DMaterial2 *emissive, *background;
3764 IDirect3DViewport2 *viewport;
3765 IDirect3DDevice2 *device;
3766 IDirectDrawSurface *rt;
3767 IDirectDraw2 *ddraw;
3768 D3DCOLOR color;
3769 HWND window;
3770 HRESULT hr;
3771 D3DMATERIALHANDLE mat_handle;
3772 DWORD rs;
3773 unsigned int i;
3774 ULONG ref;
3775 static D3DVERTEX quad[] =
3777 {{-1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3778 {{ 1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3779 {{-1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3780 {{ 1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3782 static D3DLVERTEX lquad[] =
3784 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3785 {{ 1.0f}, { 1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3786 {{-1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3787 {{ 1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3789 static D3DTLVERTEX tlquad[] =
3791 {{ 0.0f}, { 480.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3792 {{ 0.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3793 {{ 640.0f}, { 480.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3794 {{ 640.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3796 static const struct
3798 D3DVERTEXTYPE vertextype;
3799 void *data;
3800 DWORD d3drs_lighting, d3drs_specular;
3801 DWORD draw_flags;
3802 D3DCOLOR color;
3804 tests[] =
3806 /* Lighting is enabled when D3DVT_VERTEX is used and D3DDP_DONOTLIGHT is not
3807 * set. D3DVT_VERTEX has diffuse = 0xffffffff and specular = 0x00000000, as
3808 * in later d3d versions */
3809 { D3DVT_VERTEX, quad, FALSE, FALSE, 0, 0x0000ff00},
3810 { D3DVT_VERTEX, quad, TRUE, FALSE, 0, 0x0000ff00},
3811 { D3DVT_VERTEX, quad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ffffff},
3812 { D3DVT_VERTEX, quad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ffffff},
3813 { D3DVT_VERTEX, quad, FALSE, TRUE, 0, 0x0000ff00},
3814 { D3DVT_VERTEX, quad, TRUE, TRUE, 0, 0x0000ff00},
3815 { D3DVT_VERTEX, quad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ffffff},
3816 { D3DVT_VERTEX, quad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ffffff},
3818 { D3DVT_LVERTEX, lquad, FALSE, FALSE, 0, 0x00ff0000},
3819 { D3DVT_LVERTEX, lquad, TRUE, FALSE, 0, 0x00ff0000},
3820 { D3DVT_LVERTEX, lquad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
3821 { D3DVT_LVERTEX, lquad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
3822 { D3DVT_LVERTEX, lquad, FALSE, TRUE, 0, 0x00ff8080},
3823 { D3DVT_LVERTEX, lquad, TRUE, TRUE, 0, 0x00ff8080},
3824 { D3DVT_LVERTEX, lquad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
3825 { D3DVT_LVERTEX, lquad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
3827 { D3DVT_TLVERTEX, tlquad, FALSE, FALSE, 0, 0x000000ff},
3828 { D3DVT_TLVERTEX, tlquad, TRUE, FALSE, 0, 0x000000ff},
3829 { D3DVT_TLVERTEX, tlquad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x000000ff},
3830 { D3DVT_TLVERTEX, tlquad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x000000ff},
3831 { D3DVT_TLVERTEX, tlquad, FALSE, TRUE, 0, 0x008080ff},
3832 { D3DVT_TLVERTEX, tlquad, TRUE, TRUE, 0, 0x008080ff},
3833 { D3DVT_TLVERTEX, tlquad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x008080ff},
3834 { D3DVT_TLVERTEX, tlquad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x008080ff},
3837 window = create_window();
3838 ddraw = create_ddraw();
3839 ok(!!ddraw, "Failed to create a ddraw object.\n");
3840 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
3842 skip("Failed to create a 3D device, skipping test.\n");
3843 IDirectDraw2_Release(ddraw);
3844 DestroyWindow(window);
3845 return;
3848 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
3849 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3851 viewport = create_viewport(device, 0, 0, 640, 480);
3852 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
3853 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
3855 emissive = create_emissive_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
3856 hr = IDirect3DMaterial2_GetHandle(emissive, device, &mat_handle);
3857 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
3858 hr = IDirect3DDevice2_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
3859 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
3860 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
3861 ok(SUCCEEDED(hr), "Failed to disable z test, hr %#x.\n", hr);
3863 background = create_diffuse_material(device, 0.1f, 0.1f, 0.1f, 0.1f);
3864 viewport_set_background(device, viewport, background);
3866 hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_SPECULARENABLE, &rs);
3867 ok(SUCCEEDED(hr), "Failed to get specularenable render state, hr %#x.\n", hr);
3868 ok(rs == TRUE, "Initial D3DRENDERSTATE_SPECULARENABLE is %#x, expected TRUE.\n", rs);
3870 for (i = 0; i < ARRAY_SIZE(tests); i++)
3872 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
3873 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3875 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_LIGHTING, tests[i].d3drs_lighting);
3876 ok(SUCCEEDED(hr), "Failed to set lighting render state, hr %#x.\n", hr);
3877 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_SPECULARENABLE,
3878 tests[i].d3drs_specular);
3879 ok(SUCCEEDED(hr), "Failed to set specularenable render state, hr %#x.\n", hr);
3881 hr = IDirect3DDevice2_BeginScene(device);
3882 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3883 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP,
3884 tests[i].vertextype, tests[i].data, 4, tests[i].draw_flags | D3DDP_WAIT);
3885 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3886 hr = IDirect3DDevice2_EndScene(device);
3887 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
3889 color = get_surface_color(rt, 320, 240);
3890 ok(compare_color(color, tests[i].color, 1),
3891 "Got unexpected color 0x%08x, expected 0x%08x, test %u.\n",
3892 color, tests[i].color, i);
3895 destroy_material(background);
3896 destroy_material(emissive);
3897 IDirectDrawSurface_Release(rt);
3898 IDirect3DDevice2_Release(device);
3899 ref = IDirectDraw2_Release(ddraw);
3900 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
3901 DestroyWindow(window);
3904 static struct
3906 BOOL received;
3907 IDirectDraw2 *ddraw;
3908 HWND window;
3909 DWORD coop_level;
3910 } activateapp_testdata;
3912 static LRESULT CALLBACK activateapp_test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
3914 if (message == WM_ACTIVATEAPP)
3916 if (activateapp_testdata.ddraw)
3918 HRESULT hr;
3919 activateapp_testdata.received = FALSE;
3920 hr = IDirectDraw2_SetCooperativeLevel(activateapp_testdata.ddraw,
3921 activateapp_testdata.window, activateapp_testdata.coop_level);
3922 ok(SUCCEEDED(hr), "Recursive SetCooperativeLevel call failed, hr %#x.\n", hr);
3923 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP during recursive SetCooperativeLevel call.\n");
3925 activateapp_testdata.received = TRUE;
3928 return DefWindowProcA(hwnd, message, wparam, lparam);
3931 static void test_coop_level_activateapp(void)
3933 IDirectDraw2 *ddraw;
3934 HRESULT hr;
3935 HWND window;
3936 WNDCLASSA wc = {0};
3937 DDSURFACEDESC ddsd;
3938 IDirectDrawSurface *surface;
3940 ddraw = create_ddraw();
3941 ok(!!ddraw, "Failed to create a ddraw object.\n");
3943 wc.lpfnWndProc = activateapp_test_proc;
3944 wc.lpszClassName = "ddraw_test_wndproc_wc";
3945 ok(RegisterClassA(&wc), "Failed to register window class.\n");
3947 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
3948 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
3950 /* Exclusive with window already active. */
3951 SetForegroundWindow(window);
3952 activateapp_testdata.received = FALSE;
3953 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3954 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3955 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP although window was already active.\n");
3956 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3957 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3959 /* Exclusive with window not active. */
3960 SetForegroundWindow(GetDesktopWindow());
3961 activateapp_testdata.received = FALSE;
3962 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3963 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3964 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3965 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3966 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3968 /* Normal with window not active, then exclusive with the same window. */
3969 SetForegroundWindow(GetDesktopWindow());
3970 activateapp_testdata.received = FALSE;
3971 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3972 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3973 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP when setting DDSCL_NORMAL.\n");
3974 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3975 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3976 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3977 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3978 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3980 /* Recursive set of DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN. */
3981 SetForegroundWindow(GetDesktopWindow());
3982 activateapp_testdata.received = FALSE;
3983 activateapp_testdata.ddraw = ddraw;
3984 activateapp_testdata.window = window;
3985 activateapp_testdata.coop_level = DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN;
3986 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3987 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3988 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3989 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3990 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3992 /* The recursive call seems to have some bad effect on native ddraw, despite (apparently)
3993 * succeeding. Another switch to exclusive and back to normal is needed to release the
3994 * window properly. Without doing this, SetCooperativeLevel(EXCLUSIVE) will not send
3995 * WM_ACTIVATEAPP messages. */
3996 activateapp_testdata.ddraw = NULL;
3997 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3998 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3999 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4000 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4002 /* Setting DDSCL_NORMAL with recursive invocation. */
4003 SetForegroundWindow(GetDesktopWindow());
4004 activateapp_testdata.received = FALSE;
4005 activateapp_testdata.ddraw = ddraw;
4006 activateapp_testdata.window = window;
4007 activateapp_testdata.coop_level = DDSCL_NORMAL;
4008 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4009 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4010 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4012 /* DDraw is in exclusive mode now. */
4013 memset(&ddsd, 0, sizeof(ddsd));
4014 ddsd.dwSize = sizeof(ddsd);
4015 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
4016 ddsd.dwBackBufferCount = 1;
4017 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
4018 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
4019 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4020 IDirectDrawSurface_Release(surface);
4022 /* Recover again, just to be sure. */
4023 activateapp_testdata.ddraw = NULL;
4024 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4025 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4026 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4027 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4029 DestroyWindow(window);
4030 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
4031 IDirectDraw2_Release(ddraw);
4034 struct format_support_check
4036 const DDPIXELFORMAT *format;
4037 BOOL supported;
4040 static HRESULT WINAPI test_unsupported_formats_cb(DDSURFACEDESC *desc, void *ctx)
4042 struct format_support_check *format = ctx;
4044 if (!memcmp(format->format, &desc->ddpfPixelFormat, sizeof(*format->format)))
4046 format->supported = TRUE;
4047 return DDENUMRET_CANCEL;
4050 return DDENUMRET_OK;
4053 static void test_unsupported_formats(void)
4055 HRESULT hr;
4056 BOOL expect_success;
4057 HWND window;
4058 IDirectDraw2 *ddraw;
4059 IDirect3DDevice2 *device;
4060 IDirectDrawSurface *surface;
4061 DDSURFACEDESC ddsd;
4062 unsigned int i, j;
4063 DWORD expected_caps;
4064 static const struct
4066 const char *name;
4067 DDPIXELFORMAT fmt;
4069 formats[] =
4072 "D3DFMT_A8R8G8B8",
4074 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
4075 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
4079 "D3DFMT_P8",
4081 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
4082 {8 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
4086 static const DWORD caps[] = {0, DDSCAPS_SYSTEMMEMORY, DDSCAPS_VIDEOMEMORY};
4088 window = create_window();
4089 ddraw = create_ddraw();
4090 ok(!!ddraw, "Failed to create a ddraw object.\n");
4091 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
4093 skip("Failed to create a 3D device, skipping test.\n");
4094 IDirectDraw2_Release(ddraw);
4095 DestroyWindow(window);
4096 return;
4099 for (i = 0; i < ARRAY_SIZE(formats); i++)
4101 struct format_support_check check = {&formats[i].fmt, FALSE};
4102 hr = IDirect3DDevice2_EnumTextureFormats(device, test_unsupported_formats_cb, &check);
4103 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
4105 for (j = 0; j < ARRAY_SIZE(caps); j++)
4107 memset(&ddsd, 0, sizeof(ddsd));
4108 ddsd.dwSize = sizeof(ddsd);
4109 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
4110 ddsd.ddpfPixelFormat = formats[i].fmt;
4111 ddsd.dwWidth = 4;
4112 ddsd.dwHeight = 4;
4113 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | caps[j];
4115 if (caps[j] & DDSCAPS_VIDEOMEMORY && !check.supported)
4116 expect_success = FALSE;
4117 else
4118 expect_success = TRUE;
4120 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
4121 ok(SUCCEEDED(hr) == expect_success,
4122 "Got unexpected hr %#x for format %s, caps %#x, expected %s.\n",
4123 hr, formats[i].name, caps[j], expect_success ? "success" : "failure");
4124 if (FAILED(hr))
4125 continue;
4127 memset(&ddsd, 0, sizeof(ddsd));
4128 ddsd.dwSize = sizeof(ddsd);
4129 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &ddsd);
4130 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4132 if (caps[j] & DDSCAPS_VIDEOMEMORY)
4133 expected_caps = DDSCAPS_VIDEOMEMORY;
4134 else if (caps[j] & DDSCAPS_SYSTEMMEMORY)
4135 expected_caps = DDSCAPS_SYSTEMMEMORY;
4136 else if (check.supported)
4137 expected_caps = DDSCAPS_VIDEOMEMORY;
4138 else
4139 expected_caps = DDSCAPS_SYSTEMMEMORY;
4141 ok(ddsd.ddsCaps.dwCaps & expected_caps,
4142 "Expected capability %#x, format %s, input cap %#x.\n",
4143 expected_caps, formats[i].name, caps[j]);
4145 IDirectDrawSurface_Release(surface);
4149 IDirect3DDevice2_Release(device);
4150 IDirectDraw2_Release(ddraw);
4151 DestroyWindow(window);
4154 static void test_rt_caps(void)
4156 PALETTEENTRY palette_entries[256];
4157 IDirectDrawPalette *palette;
4158 IDirect3DDevice2 *device;
4159 IDirectDraw2 *ddraw;
4160 DWORD z_depth = 0;
4161 IDirect3D2 *d3d;
4162 unsigned int i;
4163 ULONG refcount;
4164 HWND window;
4165 HRESULT hr;
4167 static const DDPIXELFORMAT p8_fmt =
4169 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
4170 {8}, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000},
4173 static const struct
4175 const DDPIXELFORMAT *pf;
4176 DWORD caps_in;
4177 DWORD caps_out;
4178 HRESULT create_device_hr;
4179 HRESULT set_rt_hr;
4180 HRESULT alternative_set_rt_hr;
4181 BOOL create_may_fail;
4183 test_data[] =
4186 NULL,
4187 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
4188 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4189 D3D_OK,
4190 D3D_OK,
4191 D3D_OK,
4192 FALSE,
4195 NULL,
4196 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
4197 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4198 D3D_OK,
4199 D3D_OK,
4200 D3D_OK,
4201 FALSE,
4204 NULL,
4205 DDSCAPS_OFFSCREENPLAIN,
4206 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4207 DDERR_INVALIDCAPS,
4208 DDERR_INVALIDCAPS,
4209 DDERR_INVALIDCAPS,
4210 FALSE,
4213 NULL,
4214 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4215 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4216 D3DERR_SURFACENOTINVIDMEM,
4217 D3D_OK,
4218 D3D_OK,
4219 FALSE,
4222 NULL,
4223 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4224 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4225 DDERR_INVALIDCAPS,
4226 DDERR_INVALIDCAPS,
4227 DDERR_INVALIDCAPS,
4228 FALSE,
4231 NULL,
4232 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
4233 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4234 D3D_OK,
4235 D3D_OK,
4236 D3D_OK,
4237 FALSE,
4240 NULL,
4241 DDSCAPS_3DDEVICE,
4242 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4243 D3D_OK,
4244 D3D_OK,
4245 D3D_OK,
4246 FALSE,
4249 NULL,
4251 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4252 DDERR_INVALIDCAPS,
4253 DDERR_INVALIDCAPS,
4254 DDERR_INVALIDCAPS,
4255 FALSE,
4258 NULL,
4259 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4260 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4261 D3DERR_SURFACENOTINVIDMEM,
4262 D3D_OK,
4263 D3D_OK,
4264 FALSE,
4267 NULL,
4268 DDSCAPS_SYSTEMMEMORY,
4269 DDSCAPS_SYSTEMMEMORY,
4270 DDERR_INVALIDCAPS,
4271 DDERR_INVALIDCAPS,
4272 DDERR_INVALIDCAPS,
4273 FALSE,
4276 &p8_fmt,
4278 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4279 DDERR_INVALIDCAPS,
4280 DDERR_INVALIDCAPS,
4281 DDERR_INVALIDCAPS,
4282 FALSE,
4285 &p8_fmt,
4286 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
4287 ~0U /* AMD r200 */,
4288 DDERR_NOPALETTEATTACHED,
4289 DDERR_INVALIDCAPS,
4290 DDERR_INVALIDCAPS,
4291 FALSE,
4294 &p8_fmt,
4295 DDSCAPS_OFFSCREENPLAIN,
4296 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4297 DDERR_INVALIDCAPS,
4298 DDERR_INVALIDCAPS,
4299 DDERR_INVALIDCAPS,
4300 FALSE,
4303 &p8_fmt,
4304 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4305 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4306 DDERR_NOPALETTEATTACHED,
4307 DDERR_INVALIDCAPS,
4308 DDERR_INVALIDCAPS,
4309 FALSE,
4312 &p8_fmt,
4313 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4314 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4315 DDERR_INVALIDCAPS,
4316 DDERR_INVALIDCAPS,
4317 DDERR_INVALIDCAPS,
4318 FALSE,
4321 NULL,
4322 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER,
4323 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
4324 DDERR_INVALIDCAPS,
4325 DDERR_INVALIDPIXELFORMAT,
4326 DDERR_INVALIDCAPS,
4327 TRUE /* AMD Evergreen */,
4330 NULL,
4331 DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
4332 ~0U /* AMD Evergreen */,
4333 DDERR_INVALIDCAPS,
4334 DDERR_INVALIDPIXELFORMAT,
4335 DDERR_INVALIDCAPS,
4336 FALSE,
4339 NULL,
4340 DDSCAPS_ZBUFFER,
4341 ~0U /* AMD Evergreen */,
4342 DDERR_INVALIDCAPS,
4343 DDERR_INVALIDCAPS,
4344 DDERR_INVALIDCAPS,
4345 FALSE,
4348 NULL,
4349 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
4350 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
4351 DDERR_INVALIDCAPS,
4352 DDERR_INVALIDPIXELFORMAT,
4353 DDERR_INVALIDPIXELFORMAT,
4354 TRUE /* Nvidia Kepler */,
4357 NULL,
4358 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
4359 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
4360 DDERR_INVALIDCAPS,
4361 DDERR_INVALIDCAPS,
4362 DDERR_INVALIDCAPS,
4363 TRUE /* Nvidia Kepler */,
4367 window = create_window();
4368 ddraw = create_ddraw();
4369 ok(!!ddraw, "Failed to create a ddraw object.\n");
4370 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
4372 skip("Failed to create a 3D device, skipping test.\n");
4373 IDirectDraw2_Release(ddraw);
4374 DestroyWindow(window);
4375 return;
4377 z_depth = get_device_z_depth(device);
4378 ok(!!z_depth, "Failed to get device z depth.\n");
4379 IDirect3DDevice2_Release(device);
4381 if (FAILED(IDirectDraw2_QueryInterface(ddraw, &IID_IDirect3D2, (void **)&d3d)))
4383 skip("D3D interface is not available, skipping test.\n");
4384 goto done;
4387 memset(palette_entries, 0, sizeof(palette_entries));
4388 hr = IDirectDraw2_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
4389 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
4391 for (i = 0; i < ARRAY_SIZE(test_data); ++i)
4393 IDirectDrawSurface *surface, *rt, *expected_rt, *tmp;
4394 DDSURFACEDESC surface_desc;
4395 IDirect3DDevice2 *device;
4397 memset(&surface_desc, 0, sizeof(surface_desc));
4398 surface_desc.dwSize = sizeof(surface_desc);
4399 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4400 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
4401 if (test_data[i].pf)
4403 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
4404 surface_desc.ddpfPixelFormat = *test_data[i].pf;
4406 if (test_data[i].caps_in & DDSCAPS_ZBUFFER)
4408 surface_desc.dwFlags |= DDSD_ZBUFFERBITDEPTH;
4409 U2(surface_desc).dwZBufferBitDepth = z_depth;
4411 surface_desc.dwWidth = 640;
4412 surface_desc.dwHeight = 480;
4413 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
4414 ok(SUCCEEDED(hr) || broken(test_data[i].create_may_fail),
4415 "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
4416 i, test_data[i].caps_in, hr);
4417 if (FAILED(hr))
4418 continue;
4420 memset(&surface_desc, 0, sizeof(surface_desc));
4421 surface_desc.dwSize = sizeof(surface_desc);
4422 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
4423 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
4424 ok(test_data[i].caps_out == ~0U || surface_desc.ddsCaps.dwCaps == test_data[i].caps_out,
4425 "Test %u: Got unexpected caps %#x, expected %#x.\n",
4426 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
4428 hr = IDirect3D2_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device);
4429 ok(hr == test_data[i].create_device_hr, "Test %u: Got unexpected hr %#x, expected %#x.\n",
4430 i, hr, test_data[i].create_device_hr);
4431 if (FAILED(hr))
4433 if (hr == DDERR_NOPALETTEATTACHED)
4435 hr = IDirectDrawSurface_SetPalette(surface, palette);
4436 ok(SUCCEEDED(hr), "Test %u: Failed to set palette, hr %#x.\n", i, hr);
4437 hr = IDirect3D2_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device);
4438 if (surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
4439 ok(hr == DDERR_INVALIDPIXELFORMAT, "Test %u: Got unexpected hr %#x.\n", i, hr);
4440 else
4441 ok(hr == D3DERR_SURFACENOTINVIDMEM, "Test %u: Got unexpected hr %#x.\n", i, hr);
4443 IDirectDrawSurface_Release(surface);
4445 memset(&surface_desc, 0, sizeof(surface_desc));
4446 surface_desc.dwSize = sizeof(surface_desc);
4447 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4448 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
4449 surface_desc.dwWidth = 640;
4450 surface_desc.dwHeight = 480;
4451 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
4452 ok(SUCCEEDED(hr), "Test %u: Failed to create surface, hr %#x.\n", i, hr);
4454 hr = IDirect3D2_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device);
4455 ok(SUCCEEDED(hr), "Test %u: Failed to create device, hr %#x.\n", i, hr);
4458 memset(&surface_desc, 0, sizeof(surface_desc));
4459 surface_desc.dwSize = sizeof(surface_desc);
4460 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4461 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
4462 if (test_data[i].pf)
4464 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
4465 surface_desc.ddpfPixelFormat = *test_data[i].pf;
4467 if (test_data[i].caps_in & DDSCAPS_ZBUFFER)
4469 surface_desc.dwFlags |= DDSD_ZBUFFERBITDEPTH;
4470 U2(surface_desc).dwZBufferBitDepth = z_depth;
4472 surface_desc.dwWidth = 640;
4473 surface_desc.dwHeight = 480;
4474 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &rt, NULL);
4475 ok(SUCCEEDED(hr), "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
4476 i, test_data[i].caps_in, hr);
4478 hr = IDirect3DDevice2_SetRenderTarget(device, rt, 0);
4479 ok(hr == test_data[i].set_rt_hr || broken(hr == test_data[i].alternative_set_rt_hr),
4480 "Test %u: Got unexpected hr %#x, expected %#x.\n",
4481 i, hr, test_data[i].set_rt_hr);
4482 if (SUCCEEDED(hr) || hr == DDERR_INVALIDPIXELFORMAT)
4483 expected_rt = rt;
4484 else
4485 expected_rt = surface;
4487 /* It appears the surface is set as render target in this case, but no
4488 * reference is taken. */
4489 if (hr == DDERR_INVALIDPIXELFORMAT)
4491 refcount = IDirectDrawSurface_AddRef(rt);
4492 ok(refcount == 2, "Test %u: Got unexpected refcount %u.\n", i, refcount);
4495 hr = IDirect3DDevice2_GetRenderTarget(device, &tmp);
4496 ok(SUCCEEDED(hr), "Test %u: Failed to get render target, hr %#x.\n", i, hr);
4497 ok(tmp == expected_rt, "Test %u: Got unexpected rt %p.\n", i, tmp);
4499 IDirectDrawSurface_Release(tmp);
4500 IDirectDrawSurface_Release(rt);
4501 refcount = IDirect3DDevice2_Release(device);
4502 ok(refcount == 0, "Test %u: The device was not properly freed, refcount %u.\n", i, refcount);
4503 refcount = IDirectDrawSurface_Release(surface);
4504 ok(refcount == 0, "Test %u: The surface was not properly freed, refcount %u.\n", i, refcount);
4507 IDirectDrawPalette_Release(palette);
4508 IDirect3D2_Release(d3d);
4510 done:
4511 refcount = IDirectDraw2_Release(ddraw);
4512 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
4513 DestroyWindow(window);
4516 static void test_primary_caps(void)
4518 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
4519 IDirectDrawSurface *surface;
4520 DDSURFACEDESC surface_desc;
4521 IDirectDraw2 *ddraw;
4522 unsigned int i;
4523 ULONG refcount;
4524 HWND window;
4525 HRESULT hr;
4527 static const struct
4529 DWORD coop_level;
4530 DWORD caps_in;
4531 DWORD back_buffer_count;
4532 HRESULT hr;
4533 DWORD caps_out;
4535 test_data[] =
4538 DDSCL_NORMAL,
4539 DDSCAPS_PRIMARYSURFACE,
4540 ~0u,
4541 DD_OK,
4542 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE,
4545 DDSCL_NORMAL,
4546 DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE,
4547 ~0u,
4548 DDERR_INVALIDCAPS,
4549 ~0u,
4552 DDSCL_NORMAL,
4553 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER,
4554 ~0u,
4555 DDERR_INVALIDCAPS,
4556 ~0u,
4559 DDSCL_NORMAL,
4560 DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER,
4561 ~0u,
4562 DDERR_INVALIDCAPS,
4563 ~0u,
4566 DDSCL_NORMAL,
4567 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP,
4568 ~0u,
4569 DDERR_INVALIDCAPS,
4570 ~0u,
4573 DDSCL_NORMAL,
4574 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX,
4575 ~0u,
4576 DDERR_INVALIDCAPS,
4577 ~0u,
4580 DDSCL_NORMAL,
4581 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
4582 ~0u,
4583 DDERR_INVALIDCAPS,
4584 ~0u,
4587 DDSCL_NORMAL,
4588 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
4590 DDERR_INVALIDCAPS,
4591 ~0u,
4594 DDSCL_NORMAL,
4595 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
4597 DDERR_NOEXCLUSIVEMODE,
4598 ~0u,
4601 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
4602 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
4604 DDERR_INVALIDCAPS,
4605 ~0u,
4608 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
4609 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
4611 DD_OK,
4612 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX,
4615 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
4616 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER,
4618 DDERR_INVALIDCAPS,
4619 ~0u,
4622 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
4623 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_BACKBUFFER,
4625 DDERR_INVALIDCAPS,
4626 ~0u,
4630 window = create_window();
4631 ddraw = create_ddraw();
4632 ok(!!ddraw, "Failed to create a ddraw object.\n");
4634 for (i = 0; i < ARRAY_SIZE(test_data); ++i)
4636 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, test_data[i].coop_level);
4637 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4639 memset(&surface_desc, 0, sizeof(surface_desc));
4640 surface_desc.dwSize = sizeof(surface_desc);
4641 surface_desc.dwFlags = DDSD_CAPS;
4642 if (test_data[i].back_buffer_count != ~0u)
4643 surface_desc.dwFlags |= DDSD_BACKBUFFERCOUNT;
4644 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
4645 surface_desc.dwBackBufferCount = test_data[i].back_buffer_count;
4646 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
4647 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
4648 if (FAILED(hr))
4649 continue;
4651 memset(&surface_desc, 0, sizeof(surface_desc));
4652 surface_desc.dwSize = sizeof(surface_desc);
4653 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
4654 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
4655 ok((surface_desc.ddsCaps.dwCaps & ~placement) == test_data[i].caps_out,
4656 "Test %u: Got unexpected caps %#x, expected %#x.\n",
4657 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
4659 IDirectDrawSurface_Release(surface);
4662 refcount = IDirectDraw2_Release(ddraw);
4663 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
4664 DestroyWindow(window);
4667 static void test_surface_lock(void)
4669 IDirectDraw2 *ddraw;
4670 IDirectDrawSurface *surface;
4671 IDirect3DDevice2 *device;
4672 HRESULT hr;
4673 HWND window;
4674 unsigned int i;
4675 DDSURFACEDESC ddsd;
4676 ULONG refcount;
4677 DWORD z_depth = 0;
4678 static const struct
4680 DWORD caps;
4681 const char *name;
4683 tests[] =
4686 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
4687 "videomemory offscreenplain"
4690 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4691 "systemmemory offscreenplain"
4694 DDSCAPS_PRIMARYSURFACE,
4695 "primary"
4698 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
4699 "videomemory texture"
4702 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
4703 "systemmemory texture"
4706 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
4707 "render target"
4710 DDSCAPS_ZBUFFER,
4711 "Z buffer"
4715 window = create_window();
4716 ddraw = create_ddraw();
4717 ok(!!ddraw, "Failed to create a ddraw object.\n");
4718 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
4720 skip("Failed to create a 3D device, skipping test.\n");
4721 IDirectDraw2_Release(ddraw);
4722 DestroyWindow(window);
4723 return;
4725 z_depth = get_device_z_depth(device);
4726 ok(!!z_depth, "Failed to get device z depth.\n");
4727 IDirect3DDevice2_Release(device);
4729 for (i = 0; i < ARRAY_SIZE(tests); i++)
4731 memset(&ddsd, 0, sizeof(ddsd));
4732 ddsd.dwSize = sizeof(ddsd);
4733 ddsd.dwFlags = DDSD_CAPS;
4734 if (!(tests[i].caps & DDSCAPS_PRIMARYSURFACE))
4736 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
4737 ddsd.dwWidth = 64;
4738 ddsd.dwHeight = 64;
4740 if (tests[i].caps & DDSCAPS_ZBUFFER)
4742 ddsd.dwFlags |= DDSD_ZBUFFERBITDEPTH;
4743 U2(ddsd).dwZBufferBitDepth = z_depth;
4745 ddsd.ddsCaps.dwCaps = tests[i].caps;
4747 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
4748 ok(SUCCEEDED(hr), "Failed to create surface, type %s, hr %#x.\n", tests[i].name, hr);
4750 memset(&ddsd, 0, sizeof(ddsd));
4751 ddsd.dwSize = sizeof(ddsd);
4752 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_WAIT, NULL);
4753 ok(SUCCEEDED(hr), "Failed to lock surface, type %s, hr %#x.\n", tests[i].name, hr);
4754 if (SUCCEEDED(hr))
4756 hr = IDirectDrawSurface_Unlock(surface, NULL);
4757 ok(SUCCEEDED(hr), "Failed to unlock surface, type %s, hr %#x.\n", tests[i].name, hr);
4760 memset(&ddsd, 0, sizeof(ddsd));
4761 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_WAIT, NULL);
4762 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x, type %s.\n", hr, tests[i].name);
4764 IDirectDrawSurface_Release(surface);
4767 refcount = IDirectDraw2_Release(ddraw);
4768 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
4769 DestroyWindow(window);
4772 static void test_surface_discard(void)
4774 IDirectDraw2 *ddraw;
4775 IDirect3DDevice2 *device;
4776 HRESULT hr;
4777 HWND window;
4778 DDSURFACEDESC ddsd;
4779 IDirectDrawSurface *surface, *target;
4780 void *addr;
4781 static const struct
4783 DWORD caps;
4784 BOOL discard;
4786 tests[] =
4788 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, TRUE},
4789 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, FALSE},
4790 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, TRUE},
4791 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, FALSE},
4793 unsigned int i;
4795 window = create_window();
4796 ddraw = create_ddraw();
4797 ok(!!ddraw, "Failed to create a ddraw object.\n");
4798 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
4800 skip("Failed to create a 3D device, skipping test.\n");
4801 DestroyWindow(window);
4802 IDirectDraw2_Release(ddraw);
4803 return;
4806 hr = IDirect3DDevice2_GetRenderTarget(device, &target);
4807 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
4809 for (i = 0; i < ARRAY_SIZE(tests); i++)
4811 BOOL discarded;
4813 memset(&ddsd, 0, sizeof(ddsd));
4814 ddsd.dwSize = sizeof(ddsd);
4815 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4816 ddsd.ddsCaps.dwCaps = tests[i].caps;
4817 ddsd.dwWidth = 64;
4818 ddsd.dwHeight = 64;
4819 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
4820 if (FAILED(hr))
4822 skip("Failed to create surface, skipping.\n");
4823 continue;
4826 memset(&ddsd, 0, sizeof(ddsd));
4827 ddsd.dwSize = sizeof(ddsd);
4828 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_WAIT, NULL);
4829 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
4830 addr = ddsd.lpSurface;
4831 hr = IDirectDrawSurface_Unlock(surface, NULL);
4832 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
4834 memset(&ddsd, 0, sizeof(ddsd));
4835 ddsd.dwSize = sizeof(ddsd);
4836 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS | DDLOCK_WAIT, NULL);
4837 ok(SUCCEEDED(hr) , "Failed to lock surface, hr %#x.\n", hr);
4838 discarded = ddsd.lpSurface != addr;
4839 hr = IDirectDrawSurface_Unlock(surface, NULL);
4840 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
4842 hr = IDirectDrawSurface_Blt(target, NULL, surface, NULL, DDBLT_WAIT, NULL);
4843 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
4845 memset(&ddsd, 0, sizeof(ddsd));
4846 ddsd.dwSize = sizeof(ddsd);
4847 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS | DDLOCK_WAIT, NULL);
4848 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
4849 discarded |= ddsd.lpSurface != addr;
4850 hr = IDirectDrawSurface_Unlock(surface, NULL);
4851 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
4853 IDirectDrawSurface_Release(surface);
4855 /* Windows 7 reliably changes the address of surfaces that are discardable (Nvidia Kepler,
4856 * AMD r500, evergreen). Windows XP, at least on AMD r200, never changes the pointer. */
4857 ok(!discarded || tests[i].discard, "Expected surface not to be discarded, case %u\n", i);
4860 IDirectDrawSurface_Release(target);
4861 IDirect3DDevice2_Release(device);
4862 IDirectDraw2_Release(ddraw);
4863 DestroyWindow(window);
4866 static void fill_surface(IDirectDrawSurface *surface, D3DCOLOR color)
4868 DDSURFACEDESC surface_desc = {sizeof(surface_desc)};
4869 HRESULT hr;
4870 unsigned int x, y;
4871 DWORD *ptr;
4873 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
4874 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
4876 for (y = 0; y < surface_desc.dwHeight; ++y)
4878 ptr = (DWORD *)((BYTE *)surface_desc.lpSurface + y * surface_desc.lPitch);
4879 for (x = 0; x < surface_desc.dwWidth; ++x)
4881 ptr[x] = color;
4885 hr = IDirectDrawSurface_Unlock(surface, NULL);
4886 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
4889 static void test_flip(void)
4891 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
4892 IDirectDrawSurface *frontbuffer, *backbuffer1, *backbuffer2, *backbuffer3, *surface;
4893 DDSCAPS caps = {DDSCAPS_FLIP};
4894 DDSURFACEDESC surface_desc;
4895 BOOL sysmem_primary;
4896 IDirectDraw2 *ddraw;
4897 DWORD expected_caps;
4898 unsigned int i;
4899 D3DCOLOR color;
4900 ULONG refcount;
4901 HWND window;
4902 HRESULT hr;
4904 static const struct
4906 const char *name;
4907 DWORD caps;
4909 test_data[] =
4911 {"PRIMARYSURFACE", DDSCAPS_PRIMARYSURFACE},
4912 {"OFFSCREENPLAIN", DDSCAPS_OFFSCREENPLAIN},
4913 {"TEXTURE", DDSCAPS_TEXTURE},
4916 window = create_window();
4917 ddraw = create_ddraw();
4918 ok(!!ddraw, "Failed to create a ddraw object.\n");
4920 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4921 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4923 for (i = 0; i < ARRAY_SIZE(test_data); ++i)
4925 /* Creating a flippable texture induces a BSoD on some versions of the
4926 * Intel graphics driver. At least Intel GMA 950 with driver version
4927 * 6.14.10.4926 on Windows XP SP3 is affected. */
4928 if ((test_data[i].caps & DDSCAPS_TEXTURE) && ddraw_is_intel(ddraw))
4930 win_skip("Skipping flippable texture test.\n");
4931 continue;
4934 memset(&surface_desc, 0, sizeof(surface_desc));
4935 surface_desc.dwSize = sizeof(surface_desc);
4936 surface_desc.dwFlags = DDSD_CAPS;
4937 if (!(test_data[i].caps & DDSCAPS_PRIMARYSURFACE))
4938 surface_desc.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
4939 surface_desc.ddsCaps.dwCaps = DDSCAPS_COMPLEX | DDSCAPS_FLIP | test_data[i].caps;
4940 surface_desc.dwWidth = 512;
4941 surface_desc.dwHeight = 512;
4942 surface_desc.dwBackBufferCount = 3;
4943 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &frontbuffer, NULL);
4944 ok(hr == DDERR_INVALIDCAPS, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
4946 surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_FLIP;
4947 surface_desc.dwFlags |= DDSD_BACKBUFFERCOUNT;
4948 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &frontbuffer, NULL);
4949 ok(hr == DDERR_INVALIDCAPS, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
4951 surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_COMPLEX;
4952 surface_desc.ddsCaps.dwCaps |= DDSCAPS_FLIP;
4953 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &frontbuffer, NULL);
4954 ok(hr == DDERR_INVALIDCAPS, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
4956 surface_desc.ddsCaps.dwCaps |= DDSCAPS_COMPLEX;
4957 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &frontbuffer, NULL);
4958 todo_wine_if(test_data[i].caps & DDSCAPS_TEXTURE)
4959 ok(SUCCEEDED(hr), "%s: Failed to create surface, hr %#x.\n", test_data[i].name, hr);
4960 if (FAILED(hr))
4961 continue;
4963 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
4964 ok(SUCCEEDED(hr), "%s: Failed to set cooperative level, hr %#x.\n", test_data[i].name, hr);
4965 hr = IDirectDrawSurface_IsLost(frontbuffer);
4966 ok(hr == DD_OK, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
4967 hr = IDirectDrawSurface_Flip(frontbuffer, NULL, DDFLIP_WAIT);
4968 if (test_data[i].caps & DDSCAPS_PRIMARYSURFACE)
4969 ok(hr == DDERR_NOEXCLUSIVEMODE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
4970 else
4971 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
4972 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4973 ok(SUCCEEDED(hr), "%s: Failed to set cooperative level, hr %#x.\n", test_data[i].name, hr);
4974 hr = IDirectDrawSurface_IsLost(frontbuffer);
4975 todo_wine ok(hr == DDERR_SURFACELOST, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
4976 hr = restore_surfaces(ddraw);
4977 ok(SUCCEEDED(hr), "%s: Failed to restore surfaces, hr %#x.\n", test_data[i].name, hr);
4979 memset(&surface_desc, 0, sizeof(surface_desc));
4980 surface_desc.dwSize = sizeof(surface_desc);
4981 hr = IDirectDrawSurface_GetSurfaceDesc(frontbuffer, &surface_desc);
4982 ok(SUCCEEDED(hr), "%s: Failed to get surface desc, hr %#x.\n", test_data[i].name, hr);
4983 expected_caps = DDSCAPS_FRONTBUFFER | DDSCAPS_COMPLEX | DDSCAPS_FLIP | test_data[i].caps;
4984 if (test_data[i].caps & DDSCAPS_PRIMARYSURFACE)
4985 expected_caps |= DDSCAPS_VISIBLE;
4986 ok((surface_desc.ddsCaps.dwCaps & ~placement) == expected_caps,
4987 "%s: Got unexpected caps %#x.\n", test_data[i].name, surface_desc.ddsCaps.dwCaps);
4988 sysmem_primary = surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY;
4990 hr = IDirectDrawSurface_GetAttachedSurface(frontbuffer, &caps, &backbuffer1);
4991 ok(SUCCEEDED(hr), "%s: Failed to get attached surface, hr %#x.\n", test_data[i].name, hr);
4992 memset(&surface_desc, 0, sizeof(surface_desc));
4993 surface_desc.dwSize = sizeof(surface_desc);
4994 hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer1, &surface_desc);
4995 ok(SUCCEEDED(hr), "%s: Failed to get surface desc, hr %#x.\n", test_data[i].name, hr);
4996 ok(!surface_desc.dwBackBufferCount, "%s: Got unexpected back buffer count %u.\n",
4997 test_data[i].name, surface_desc.dwBackBufferCount);
4998 expected_caps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER);
4999 expected_caps |= DDSCAPS_BACKBUFFER;
5000 ok((surface_desc.ddsCaps.dwCaps & ~placement) == expected_caps,
5001 "%s: Got unexpected caps %#x.\n", test_data[i].name, surface_desc.ddsCaps.dwCaps);
5003 hr = IDirectDrawSurface_GetAttachedSurface(backbuffer1, &caps, &backbuffer2);
5004 ok(SUCCEEDED(hr), "%s: Failed to get attached surface, hr %#x.\n", test_data[i].name, hr);
5005 memset(&surface_desc, 0, sizeof(surface_desc));
5006 surface_desc.dwSize = sizeof(surface_desc);
5007 hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer2, &surface_desc);
5008 ok(SUCCEEDED(hr), "%s: Failed to get surface desc, hr %#x.\n", test_data[i].name, hr);
5009 ok(!surface_desc.dwBackBufferCount, "%s: Got unexpected back buffer count %u.\n",
5010 test_data[i].name, surface_desc.dwBackBufferCount);
5011 expected_caps &= ~DDSCAPS_BACKBUFFER;
5012 ok((surface_desc.ddsCaps.dwCaps & ~placement) == expected_caps,
5013 "%s: Got unexpected caps %#x.\n", test_data[i].name, surface_desc.ddsCaps.dwCaps);
5015 hr = IDirectDrawSurface_GetAttachedSurface(backbuffer2, &caps, &backbuffer3);
5016 ok(SUCCEEDED(hr), "%s: Failed to get attached surface, hr %#x.\n", test_data[i].name, hr);
5017 memset(&surface_desc, 0, sizeof(surface_desc));
5018 surface_desc.dwSize = sizeof(surface_desc);
5019 hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer3, &surface_desc);
5020 ok(SUCCEEDED(hr), "%s: Failed to get surface desc, hr %#x.\n", test_data[i].name, hr);
5021 ok(!surface_desc.dwBackBufferCount, "%s: Got unexpected back buffer count %u.\n",
5022 test_data[i].name, surface_desc.dwBackBufferCount);
5023 ok((surface_desc.ddsCaps.dwCaps & ~placement) == expected_caps,
5024 "%s: Got unexpected caps %#x.\n", test_data[i].name, surface_desc.ddsCaps.dwCaps);
5026 hr = IDirectDrawSurface_GetAttachedSurface(backbuffer3, &caps, &surface);
5027 ok(SUCCEEDED(hr), "%s: Failed to get attached surface, hr %#x.\n", test_data[i].name, hr);
5028 ok(surface == frontbuffer, "%s: Got unexpected surface %p, expected %p.\n",
5029 test_data[i].name, surface, frontbuffer);
5030 IDirectDrawSurface_Release(surface);
5032 memset(&surface_desc, 0, sizeof(surface_desc));
5033 surface_desc.dwSize = sizeof(surface_desc);
5034 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5035 surface_desc.ddsCaps.dwCaps = 0;
5036 surface_desc.dwWidth = 640;
5037 surface_desc.dwHeight = 480;
5038 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5039 ok(SUCCEEDED(hr), "%s: Failed to create surface, hr %#x.\n", test_data[i].name, hr);
5040 hr = IDirectDrawSurface_Flip(frontbuffer, surface, DDFLIP_WAIT);
5041 ok(hr == DDERR_NOTFLIPPABLE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
5042 IDirectDrawSurface_Release(surface);
5044 hr = IDirectDrawSurface_Flip(frontbuffer, frontbuffer, DDFLIP_WAIT);
5045 ok(hr == DDERR_NOTFLIPPABLE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
5046 hr = IDirectDrawSurface_Flip(backbuffer1, NULL, DDFLIP_WAIT);
5047 ok(hr == DDERR_NOTFLIPPABLE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
5048 hr = IDirectDrawSurface_Flip(backbuffer2, NULL, DDFLIP_WAIT);
5049 ok(hr == DDERR_NOTFLIPPABLE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
5050 hr = IDirectDrawSurface_Flip(backbuffer3, NULL, DDFLIP_WAIT);
5051 ok(hr == DDERR_NOTFLIPPABLE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
5053 /* The Nvidia Geforce 7 driver cannot do a color fill on a texture backbuffer after
5054 * the backbuffer has been locked or GetSurfaceDesc has been called. Do it ourselves
5055 * as a workaround. */
5056 fill_surface(backbuffer1, 0xffff0000);
5057 fill_surface(backbuffer2, 0xff00ff00);
5058 fill_surface(backbuffer3, 0xff0000ff);
5060 hr = IDirectDrawSurface_Flip(frontbuffer, NULL, DDFLIP_WAIT);
5061 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
5062 color = get_surface_color(backbuffer1, 320, 240);
5063 /* The testbot seems to just copy the contents of one surface to all the
5064 * others, instead of properly flipping. */
5065 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
5066 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5067 color = get_surface_color(backbuffer2, 320, 240);
5068 ok(compare_color(color, 0x000000ff, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5069 fill_surface(backbuffer3, 0xffff0000);
5071 hr = IDirectDrawSurface_Flip(frontbuffer, NULL, DDFLIP_WAIT);
5072 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
5073 color = get_surface_color(backbuffer1, 320, 240);
5074 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
5075 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5076 color = get_surface_color(backbuffer2, 320, 240);
5077 ok(compare_color(color, 0x00ff0000, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5078 fill_surface(backbuffer3, 0xff00ff00);
5080 hr = IDirectDrawSurface_Flip(frontbuffer, NULL, DDFLIP_WAIT);
5081 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
5082 color = get_surface_color(backbuffer1, 320, 240);
5083 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
5084 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5085 color = get_surface_color(backbuffer2, 320, 240);
5086 ok(compare_color(color, 0x0000ff00, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5087 fill_surface(backbuffer3, 0xff0000ff);
5089 hr = IDirectDrawSurface_Flip(frontbuffer, backbuffer1, DDFLIP_WAIT);
5090 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
5091 color = get_surface_color(backbuffer2, 320, 240);
5092 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
5093 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5094 color = get_surface_color(backbuffer3, 320, 240);
5095 ok(compare_color(color, 0x000000ff, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5096 fill_surface(backbuffer1, 0xffff0000);
5098 hr = IDirectDrawSurface_Flip(frontbuffer, backbuffer2, DDFLIP_WAIT);
5099 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
5100 color = get_surface_color(backbuffer1, 320, 240);
5101 ok(compare_color(color, 0x00ff0000, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5102 color = get_surface_color(backbuffer3, 320, 240);
5103 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
5104 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5105 fill_surface(backbuffer2, 0xff00ff00);
5107 hr = IDirectDrawSurface_Flip(frontbuffer, backbuffer3, DDFLIP_WAIT);
5108 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
5109 color = get_surface_color(backbuffer1, 320, 240);
5110 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
5111 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5112 color = get_surface_color(backbuffer2, 320, 240);
5113 ok(compare_color(color, 0x0000ff00, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
5115 IDirectDrawSurface_Release(backbuffer3);
5116 IDirectDrawSurface_Release(backbuffer2);
5117 IDirectDrawSurface_Release(backbuffer1);
5118 IDirectDrawSurface_Release(frontbuffer);
5121 refcount = IDirectDraw2_Release(ddraw);
5122 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
5123 DestroyWindow(window);
5126 static void reset_ddsd(DDSURFACEDESC *ddsd)
5128 memset(ddsd, 0, sizeof(*ddsd));
5129 ddsd->dwSize = sizeof(*ddsd);
5132 static void test_set_surface_desc(void)
5134 IDirectDraw2 *ddraw;
5135 HWND window;
5136 HRESULT hr;
5137 DDSURFACEDESC ddsd;
5138 IDirectDrawSurface *surface;
5139 IDirectDrawSurface3 *surface3;
5140 BYTE data[16*16*4];
5141 ULONG ref;
5142 unsigned int i;
5143 static const struct
5145 DWORD caps;
5146 BOOL supported;
5147 const char *name;
5149 invalid_caps_tests[] =
5151 {DDSCAPS_VIDEOMEMORY, FALSE, "videomemory plain"},
5152 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, TRUE, "systemmemory texture"},
5153 {DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY, FALSE, "systemmemory primary"},
5156 window = create_window();
5157 ddraw = create_ddraw();
5158 ok(!!ddraw, "Failed to create a ddraw object.\n");
5159 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5160 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5162 reset_ddsd(&ddsd);
5163 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
5164 ddsd.dwWidth = 8;
5165 ddsd.dwHeight = 8;
5166 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
5167 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
5168 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
5169 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5170 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5171 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
5172 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
5174 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
5175 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5177 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
5178 ok(SUCCEEDED(hr), "Failed to get IDirectDrawSurface3 interface, hr %#x.\n", hr);
5179 IDirectDrawSurface_Release(surface);
5181 reset_ddsd(&ddsd);
5182 ddsd.dwFlags = DDSD_LPSURFACE;
5183 ddsd.lpSurface = data;
5184 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5185 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5187 /* Redundantly setting the same lpSurface is not an error. */
5188 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5189 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5190 hr = IDirectDrawSurface3_GetSurfaceDesc(surface3, &ddsd);
5191 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5192 ok(!(ddsd.dwFlags & DDSD_LPSURFACE), "DDSD_LPSURFACE is set.\n");
5193 ok(ddsd.lpSurface == NULL, "lpSurface is %p, expected NULL.\n", ddsd.lpSurface);
5195 hr = IDirectDrawSurface3_Lock(surface3, NULL, &ddsd, 0, NULL);
5196 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
5197 ok(!(ddsd.dwFlags & DDSD_LPSURFACE), "DDSD_LPSURFACE is set.\n");
5198 ok(ddsd.lpSurface == data, "lpSurface is %p, expected %p.\n", data, data);
5199 hr = IDirectDrawSurface3_Unlock(surface3, NULL);
5200 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
5202 reset_ddsd(&ddsd);
5203 ddsd.dwFlags = DDSD_LPSURFACE;
5204 ddsd.lpSurface = data;
5205 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 1);
5206 ok(hr == DDERR_INVALIDPARAMS, "SetSurfaceDesc with flags=1 returned %#x.\n", hr);
5208 ddsd.lpSurface = NULL;
5209 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5210 ok(hr == DDERR_INVALIDPARAMS, "Setting lpSurface=NULL returned %#x.\n", hr);
5212 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, NULL, 0);
5213 ok(hr == DDERR_INVALIDPARAMS, "SetSurfaceDesc with NULL desc returned %#x.\n", hr);
5215 hr = IDirectDrawSurface3_GetSurfaceDesc(surface3, &ddsd);
5216 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5217 ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN),
5218 "Got unexpected caps %#x.\n", ddsd.ddsCaps.dwCaps);
5220 /* Setting the caps is an error. This also means the original description cannot be reapplied. */
5221 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5222 ok(hr == DDERR_INVALIDPARAMS, "Setting the original desc returned %#x.\n", hr);
5224 ddsd.dwFlags = DDSD_CAPS;
5225 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5226 ok(hr == DDERR_INVALIDPARAMS, "Setting DDSD_CAPS returned %#x.\n", hr);
5228 /* dwCaps = 0 is allowed, but ignored. */
5229 ddsd.dwFlags = DDSD_CAPS | DDSD_LPSURFACE;
5230 ddsd.lpSurface = data;
5231 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5232 ok(hr == DDERR_INVALIDCAPS, "Setting DDSD_CAPS returned %#x.\n", hr);
5233 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
5234 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5235 ok(hr == DDERR_INVALIDCAPS, "Setting DDSD_CAPS returned %#x.\n", hr);
5236 ddsd.ddsCaps.dwCaps = 0;
5237 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5238 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5240 hr = IDirectDrawSurface3_GetSurfaceDesc(surface3, &ddsd);
5241 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5242 ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN),
5243 "Got unexpected caps %#x.\n", ddsd.ddsCaps.dwCaps);
5245 /* Setting the height is allowed, but it cannot be set to 0, and only if LPSURFACE is set too. */
5246 reset_ddsd(&ddsd);
5247 ddsd.dwFlags = DDSD_HEIGHT;
5248 ddsd.dwHeight = 16;
5249 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5250 ok(hr == DDERR_INVALIDPARAMS, "Setting height without lpSurface returned %#x.\n", hr);
5252 ddsd.lpSurface = data;
5253 ddsd.dwFlags = DDSD_HEIGHT | DDSD_LPSURFACE;
5254 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5255 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5257 ddsd.dwHeight = 0;
5258 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5259 ok(hr == DDERR_INVALIDPARAMS, "Setting height=0 returned %#x.\n", hr);
5261 reset_ddsd(&ddsd);
5262 hr = IDirectDrawSurface3_GetSurfaceDesc(surface3, &ddsd);
5263 ok(SUCCEEDED(hr), "GetSurfaceDesc failed, hr %#x.\n", hr);
5264 ok(ddsd.dwWidth == 8, "SetSurfaceDesc: Expected width 8, got %u.\n", ddsd.dwWidth);
5265 ok(ddsd.dwHeight == 16, "SetSurfaceDesc: Expected height 16, got %u.\n", ddsd.dwHeight);
5267 /* Pitch and width can be set, but only together, and only with LPSURFACE. They must not be 0. */
5268 reset_ddsd(&ddsd);
5269 ddsd.dwFlags = DDSD_PITCH;
5270 U1(ddsd).lPitch = 8 * 4;
5271 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5272 ok(hr == DDERR_INVALIDPARAMS, "Setting pitch without lpSurface or width returned %#x.\n", hr);
5274 ddsd.dwFlags = DDSD_WIDTH;
5275 ddsd.dwWidth = 16;
5276 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5277 ok(hr == DDERR_INVALIDPARAMS, "Setting width without lpSurface or pitch returned %#x.\n", hr);
5279 ddsd.dwFlags = DDSD_PITCH | DDSD_LPSURFACE;
5280 ddsd.lpSurface = data;
5281 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5282 ok(hr == DDERR_INVALIDPARAMS, "Setting pitch and lpSurface without width returned %#x.\n", hr);
5284 ddsd.dwFlags = DDSD_WIDTH | DDSD_LPSURFACE;
5285 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5286 ok(hr == DDERR_INVALIDPARAMS, "Setting width and lpSurface without pitch returned %#x.\n", hr);
5288 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5289 U1(ddsd).lPitch = 16 * 4;
5290 ddsd.dwWidth = 16;
5291 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5292 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5294 reset_ddsd(&ddsd);
5295 hr = IDirectDrawSurface3_GetSurfaceDesc(surface3, &ddsd);
5296 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5297 ok(ddsd.dwWidth == 16, "SetSurfaceDesc: Expected width 8, got %u.\n", ddsd.dwWidth);
5298 ok(ddsd.dwHeight == 16, "SetSurfaceDesc: Expected height 16, got %u.\n", ddsd.dwHeight);
5299 ok(U1(ddsd).lPitch == 16 * 4, "SetSurfaceDesc: Expected pitch 64, got %u.\n", U1(ddsd).lPitch);
5301 /* The pitch must be 32 bit aligned and > 0, but is not verified for sanity otherwise.
5303 * VMware rejects those calls, but all real drivers accept it. Mark the VMware behavior broken. */
5304 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5305 U1(ddsd).lPitch = 4 * 4;
5306 ddsd.lpSurface = data;
5307 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5308 ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDPARAMS), "Failed to set surface desc, hr %#x.\n", hr);
5310 U1(ddsd).lPitch = 4;
5311 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5312 ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDPARAMS), "Failed to set surface desc, hr %#x.\n", hr);
5314 U1(ddsd).lPitch = 16 * 4 + 1;
5315 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5316 ok(hr == DDERR_INVALIDPARAMS, "Setting misaligned pitch returned %#x.\n", hr);
5318 U1(ddsd).lPitch = 16 * 4 + 3;
5319 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5320 ok(hr == DDERR_INVALIDPARAMS, "Setting misaligned pitch returned %#x.\n", hr);
5322 U1(ddsd).lPitch = -4;
5323 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5324 ok(hr == DDERR_INVALIDPARAMS, "Setting negative pitch returned %#x.\n", hr);
5326 U1(ddsd).lPitch = 16 * 4;
5327 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5328 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5330 reset_ddsd(&ddsd);
5331 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5332 U1(ddsd).lPitch = 0;
5333 ddsd.dwWidth = 16;
5334 ddsd.lpSurface = data;
5335 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5336 ok(hr == DDERR_INVALIDPARAMS, "Setting zero pitch returned %#x.\n", hr);
5338 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5339 U1(ddsd).lPitch = 16 * 4;
5340 ddsd.dwWidth = 0;
5341 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5342 ok(hr == DDERR_INVALIDPARAMS, "Setting zero width returned %#x.\n", hr);
5344 /* Setting the pixelformat without LPSURFACE is an error, but with LPSURFACE it works. */
5345 ddsd.dwFlags = DDSD_PIXELFORMAT;
5346 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
5347 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
5348 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
5349 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5350 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5351 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
5352 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5353 ok(hr == DDERR_INVALIDPARAMS, "Setting the pixel format returned %#x.\n", hr);
5355 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_LPSURFACE;
5356 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5357 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5359 /* Can't set color keys. */
5360 reset_ddsd(&ddsd);
5361 ddsd.dwFlags = DDSD_CKSRCBLT;
5362 ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00ff0000;
5363 ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00ff0000;
5364 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5365 ok(hr == DDERR_INVALIDPARAMS, "Setting ddckCKSrcBlt returned %#x.\n", hr);
5367 ddsd.dwFlags = DDSD_CKSRCBLT | DDSD_LPSURFACE;
5368 ddsd.lpSurface = data;
5369 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5370 ok(hr == DDERR_INVALIDPARAMS, "Setting ddckCKSrcBlt returned %#x.\n", hr);
5372 IDirectDrawSurface3_Release(surface3);
5374 /* SetSurfaceDesc needs systemmemory surfaces.
5376 * As a sidenote, fourcc surfaces aren't allowed in sysmem, thus testing DDSD_LINEARSIZE is moot. */
5377 for (i = 0; i < ARRAY_SIZE(invalid_caps_tests); i++)
5379 reset_ddsd(&ddsd);
5380 ddsd.dwFlags = DDSD_CAPS;
5381 ddsd.ddsCaps.dwCaps = invalid_caps_tests[i].caps;
5382 if (!(invalid_caps_tests[i].caps & DDSCAPS_PRIMARYSURFACE))
5384 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5385 ddsd.dwWidth = 8;
5386 ddsd.dwHeight = 8;
5387 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
5388 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
5389 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
5390 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5391 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5392 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
5395 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
5396 ok(SUCCEEDED(hr) || hr == DDERR_NODIRECTDRAWHW, "Failed to create surface, hr %#x.\n", hr);
5397 if (FAILED(hr))
5399 skip("Cannot create a %s surface, skipping vidmem SetSurfaceDesc test.\n",
5400 invalid_caps_tests[i].name);
5401 goto done;
5403 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
5404 ok(SUCCEEDED(hr), "Failed to get IDirectDrawSurface3 interface, hr %#x.\n", hr);
5405 IDirectDrawSurface_Release(surface);
5407 reset_ddsd(&ddsd);
5408 ddsd.dwFlags = DDSD_LPSURFACE;
5409 ddsd.lpSurface = data;
5410 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5411 if (invalid_caps_tests[i].supported)
5413 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5415 else
5417 ok(hr == DDERR_INVALIDSURFACETYPE, "SetSurfaceDesc on a %s surface returned %#x.\n",
5418 invalid_caps_tests[i].name, hr);
5420 /* Check priority of error conditions. */
5421 ddsd.dwFlags = DDSD_WIDTH;
5422 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5423 ok(hr == DDERR_INVALIDSURFACETYPE, "SetSurfaceDesc on a %s surface returned %#x.\n",
5424 invalid_caps_tests[i].name, hr);
5427 IDirectDrawSurface3_Release(surface3);
5430 done:
5431 ref = IDirectDraw2_Release(ddraw);
5432 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
5433 DestroyWindow(window);
5436 static void test_user_memory_getdc(void)
5438 IDirectDraw2 *ddraw;
5439 HWND window;
5440 HRESULT hr;
5441 DDSURFACEDESC ddsd;
5442 IDirectDrawSurface *surface;
5443 IDirectDrawSurface3 *surface3;
5444 DWORD data[16][16];
5445 HBITMAP bitmap;
5446 DIBSECTION dib;
5447 ULONG ref;
5448 int size;
5449 HDC dc;
5450 unsigned int x, y;
5452 window = create_window();
5453 ddraw = create_ddraw();
5454 ok(!!ddraw, "Failed to create a ddraw object.\n");
5456 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5457 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5459 reset_ddsd(&ddsd);
5460 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
5461 ddsd.dwWidth = 16;
5462 ddsd.dwHeight = 16;
5463 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
5464 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
5465 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
5466 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5467 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5468 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
5469 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
5470 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
5471 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5473 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
5474 ok(SUCCEEDED(hr), "Failed to get IDirectDrawSurface3 interface, hr %#x.\n", hr);
5475 IDirectDrawSurface_Release(surface);
5477 memset(data, 0xaa, sizeof(data));
5478 reset_ddsd(&ddsd);
5479 ddsd.dwFlags = DDSD_LPSURFACE;
5480 ddsd.lpSurface = data;
5481 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5482 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5484 hr = IDirectDrawSurface3_GetDC(surface3, &dc);
5485 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
5486 bitmap = GetCurrentObject(dc, OBJ_BITMAP);
5487 ok(!!bitmap, "Failed to get bitmap.\n");
5488 size = GetObjectA(bitmap, sizeof(dib), &dib);
5489 ok(size == sizeof(dib), "Got unexpected size %d.\n", size);
5490 ok(dib.dsBm.bmBits == data, "Got unexpected bits %p, expected %p.\n", dib.dsBm.bmBits, data);
5491 BitBlt(dc, 0, 0, 16, 8, NULL, 0, 0, WHITENESS);
5492 BitBlt(dc, 0, 8, 16, 8, NULL, 0, 0, BLACKNESS);
5493 hr = IDirectDrawSurface3_ReleaseDC(surface3, dc);
5494 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
5496 ok(data[0][0] == 0xffffffff, "Expected color 0xffffffff, got %#x.\n", data[0][0]);
5497 ok(data[15][15] == 0x00000000, "Expected color 0x00000000, got %#x.\n", data[15][15]);
5499 ddsd.dwFlags = DDSD_LPSURFACE | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PITCH;
5500 ddsd.lpSurface = data;
5501 ddsd.dwWidth = 4;
5502 ddsd.dwHeight = 8;
5503 U1(ddsd).lPitch = sizeof(*data);
5504 hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
5505 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5507 memset(data, 0xaa, sizeof(data));
5508 hr = IDirectDrawSurface3_GetDC(surface3, &dc);
5509 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
5510 BitBlt(dc, 0, 0, 4, 8, NULL, 0, 0, BLACKNESS);
5511 BitBlt(dc, 1, 1, 2, 2, NULL, 0, 0, WHITENESS);
5512 hr = IDirectDrawSurface3_ReleaseDC(surface3, dc);
5513 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
5515 for (y = 0; y < 4; y++)
5517 for (x = 0; x < 4; x++)
5519 if ((x == 1 || x == 2) && (y == 1 || y == 2))
5520 ok(data[y][x] == 0xffffffff, "Expected color 0xffffffff on position %ux%u, got %#x.\n",
5521 x, y, data[y][x]);
5522 else
5523 ok(data[y][x] == 0x00000000, "Expected color 0xaaaaaaaa on position %ux%u, got %#x.\n",
5524 x, y, data[y][x]);
5527 ok(data[0][5] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 5x0, got %#x.\n",
5528 data[0][5]);
5529 ok(data[7][3] == 0x00000000, "Expected color 0x00000000 on position 3x7, got %#x.\n",
5530 data[7][3]);
5531 ok(data[7][4] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 4x7, got %#x.\n",
5532 data[7][4]);
5533 ok(data[8][0] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 0x8, got %#x.\n",
5534 data[8][0]);
5536 IDirectDrawSurface3_Release(surface3);
5537 ref = IDirectDraw2_Release(ddraw);
5538 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
5539 DestroyWindow(window);
5542 static void test_sysmem_overlay(void)
5544 IDirectDraw2 *ddraw;
5545 HWND window;
5546 HRESULT hr;
5547 DDSURFACEDESC ddsd;
5548 IDirectDrawSurface *surface;
5549 ULONG ref;
5551 window = create_window();
5552 ddraw = create_ddraw();
5553 ok(!!ddraw, "Failed to create a ddraw object.\n");
5555 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5556 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5558 reset_ddsd(&ddsd);
5559 ddsd.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
5560 ddsd.dwWidth = 16;
5561 ddsd.dwHeight = 16;
5562 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OVERLAY;
5563 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
5564 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
5565 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
5566 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5567 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5568 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
5569 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
5570 ok(hr == DDERR_NOOVERLAYHW, "Got unexpected hr %#x.\n", hr);
5572 ref = IDirectDraw2_Release(ddraw);
5573 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
5574 DestroyWindow(window);
5577 static void test_primary_palette(void)
5579 DDSCAPS surface_caps = {DDSCAPS_FLIP};
5580 IDirectDrawSurface *primary, *backbuffer;
5581 PALETTEENTRY palette_entries[256];
5582 IDirectDrawPalette *palette, *tmp;
5583 DDSURFACEDESC surface_desc;
5584 IDirectDraw2 *ddraw;
5585 DWORD palette_caps;
5586 ULONG refcount;
5587 HWND window;
5588 HRESULT hr;
5590 window = create_window();
5591 ddraw = create_ddraw();
5592 ok(!!ddraw, "Failed to create a ddraw object.\n");
5593 if (FAILED(IDirectDraw2_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
5595 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
5596 IDirectDraw2_Release(ddraw);
5597 DestroyWindow(window);
5598 return;
5600 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
5601 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5603 memset(&surface_desc, 0, sizeof(surface_desc));
5604 surface_desc.dwSize = sizeof(surface_desc);
5605 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
5606 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
5607 surface_desc.dwBackBufferCount = 1;
5608 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &primary, NULL);
5609 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5610 hr = IDirectDrawSurface_GetAttachedSurface(primary, &surface_caps, &backbuffer);
5611 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
5613 memset(palette_entries, 0, sizeof(palette_entries));
5614 hr = IDirectDraw2_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256, palette_entries, &palette, NULL);
5615 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5616 refcount = get_refcount((IUnknown *)palette);
5617 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
5619 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
5620 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
5621 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
5623 hr = IDirectDrawSurface_SetPalette(primary, palette);
5624 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5626 /* The Windows 8 testbot attaches the palette to the backbuffer as well,
5627 * and is generally somewhat broken with respect to 8 bpp / palette
5628 * handling. */
5629 if (SUCCEEDED(IDirectDrawSurface_GetPalette(backbuffer, &tmp)))
5631 win_skip("Broken palette handling detected, skipping tests.\n");
5632 IDirectDrawPalette_Release(tmp);
5633 IDirectDrawPalette_Release(palette);
5634 /* The Windows 8 testbot keeps extra references to the primary and
5635 * backbuffer while in 8 bpp mode. */
5636 hr = IDirectDraw2_RestoreDisplayMode(ddraw);
5637 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
5638 goto done;
5641 refcount = get_refcount((IUnknown *)palette);
5642 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
5644 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
5645 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
5646 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE | DDPCAPS_ALLOW256),
5647 "Got unexpected palette caps %#x.\n", palette_caps);
5649 hr = IDirectDrawSurface_SetPalette(primary, NULL);
5650 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5651 refcount = get_refcount((IUnknown *)palette);
5652 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
5654 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
5655 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
5656 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
5658 hr = IDirectDrawSurface_SetPalette(primary, palette);
5659 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5660 refcount = get_refcount((IUnknown *)palette);
5661 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
5663 hr = IDirectDrawSurface_GetPalette(primary, &tmp);
5664 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
5665 ok(tmp == palette, "Got unexpected palette %p, expected %p.\n", tmp, palette);
5666 IDirectDrawPalette_Release(tmp);
5667 hr = IDirectDrawSurface_GetPalette(backbuffer, &tmp);
5668 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
5670 refcount = IDirectDrawPalette_Release(palette);
5671 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
5672 refcount = IDirectDrawPalette_Release(palette);
5673 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5675 /* Note that this only seems to work when the palette is attached to the
5676 * primary surface. When attached to a regular surface, attempting to get
5677 * the palette here will cause an access violation. */
5678 hr = IDirectDrawSurface_GetPalette(primary, &tmp);
5679 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
5681 hr = IDirectDrawSurface_IsLost(primary);
5682 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5684 memset(&surface_desc, 0, sizeof(surface_desc));
5685 surface_desc.dwSize = sizeof(surface_desc);
5686 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &surface_desc);
5687 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5688 ok(surface_desc.dwWidth == 640, "Got unexpected surface width %u.\n", surface_desc.dwWidth);
5689 ok(surface_desc.dwHeight == 480, "Got unexpected surface height %u.\n", surface_desc.dwHeight);
5690 ok(U1(surface_desc.ddpfPixelFormat).dwRGBBitCount == 8, "Got unexpected bit count %u.\n",
5691 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount);
5693 hr = set_display_mode(ddraw, 640, 480);
5694 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
5696 memset(&surface_desc, 0, sizeof(surface_desc));
5697 surface_desc.dwSize = sizeof(surface_desc);
5698 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &surface_desc);
5699 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5700 ok(surface_desc.dwWidth == 640, "Got unexpected surface width %u.\n", surface_desc.dwWidth);
5701 ok(surface_desc.dwHeight == 480, "Got unexpected surface height %u.\n", surface_desc.dwHeight);
5702 ok(U1(surface_desc.ddpfPixelFormat).dwRGBBitCount == 32
5703 || U1(surface_desc.ddpfPixelFormat).dwRGBBitCount == 24,
5704 "Got unexpected bit count %u.\n", U1(surface_desc.ddpfPixelFormat).dwRGBBitCount);
5706 hr = IDirectDrawSurface_IsLost(primary);
5707 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
5708 hr = IDirectDrawSurface_Restore(primary);
5709 ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
5710 hr = IDirectDrawSurface_IsLost(primary);
5711 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
5713 memset(&surface_desc, 0, sizeof(surface_desc));
5714 surface_desc.dwSize = sizeof(surface_desc);
5715 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &surface_desc);
5716 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5717 ok(surface_desc.dwWidth == 640, "Got unexpected surface width %u.\n", surface_desc.dwWidth);
5718 ok(surface_desc.dwHeight == 480, "Got unexpected surface height %u.\n", surface_desc.dwHeight);
5719 ok(U1(surface_desc.ddpfPixelFormat).dwRGBBitCount == 32
5720 || U1(surface_desc.ddpfPixelFormat).dwRGBBitCount == 24,
5721 "Got unexpected bit count %u.\n", U1(surface_desc.ddpfPixelFormat).dwRGBBitCount);
5723 done:
5724 refcount = IDirectDrawSurface_Release(backbuffer);
5725 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
5726 refcount = IDirectDrawSurface_Release(primary);
5727 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5728 refcount = IDirectDraw2_Release(ddraw);
5729 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5730 DestroyWindow(window);
5733 static HRESULT WINAPI surface_counter(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
5735 UINT *surface_count = context;
5737 ++(*surface_count);
5738 IDirectDrawSurface_Release(surface);
5740 return DDENUMRET_OK;
5743 static void test_surface_attachment(void)
5745 IDirectDrawSurface *surface1, *surface2, *surface3, *surface4;
5746 DDSCAPS caps = {DDSCAPS_TEXTURE};
5747 DDSURFACEDESC surface_desc;
5748 IDirectDraw2 *ddraw;
5749 UINT surface_count;
5750 ULONG refcount;
5751 HWND window;
5752 HRESULT hr;
5754 window = create_window();
5755 ddraw = create_ddraw();
5756 ok(!!ddraw, "Failed to create a ddraw object.\n");
5757 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5758 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5760 memset(&surface_desc, 0, sizeof(surface_desc));
5761 surface_desc.dwSize = sizeof(surface_desc);
5762 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
5763 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
5764 U2(surface_desc).dwMipMapCount = 3;
5765 surface_desc.dwWidth = 128;
5766 surface_desc.dwHeight = 128;
5767 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
5768 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5770 hr = IDirectDrawSurface_GetAttachedSurface(surface1, &caps, &surface2);
5771 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
5772 hr = IDirectDrawSurface_GetAttachedSurface(surface2, &caps, &surface3);
5773 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
5774 hr = IDirectDrawSurface_GetAttachedSurface(surface3, &caps, &surface4);
5775 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
5777 surface_count = 0;
5778 IDirectDrawSurface_EnumAttachedSurfaces(surface1, &surface_count, surface_counter);
5779 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
5780 surface_count = 0;
5781 IDirectDrawSurface_EnumAttachedSurfaces(surface2, &surface_count, surface_counter);
5782 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
5783 surface_count = 0;
5784 IDirectDrawSurface_EnumAttachedSurfaces(surface3, &surface_count, surface_counter);
5785 ok(!surface_count, "Got unexpected surface_count %u.\n", surface_count);
5787 memset(&surface_desc, 0, sizeof(surface_desc));
5788 surface_desc.dwSize = sizeof(surface_desc);
5789 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5790 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
5791 surface_desc.dwWidth = 16;
5792 surface_desc.dwHeight = 16;
5793 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
5794 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5796 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4);
5797 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5798 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
5799 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5800 hr = IDirectDrawSurface_AddAttachedSurface(surface3, surface4);
5801 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5802 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface3);
5803 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5804 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface4);
5805 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5806 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface2);
5807 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5809 IDirectDrawSurface_Release(surface4);
5811 memset(&surface_desc, 0, sizeof(surface_desc));
5812 surface_desc.dwSize = sizeof(surface_desc);
5813 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5814 surface_desc.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
5815 surface_desc.dwWidth = 16;
5816 surface_desc.dwHeight = 16;
5817 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
5818 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5820 if (SUCCEEDED(hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4)))
5822 skip("Running on refrast, skipping some tests.\n");
5823 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface4);
5824 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
5826 else
5828 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5829 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
5830 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5831 hr = IDirectDrawSurface_AddAttachedSurface(surface3, surface4);
5832 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5833 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface3);
5834 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5835 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface4);
5836 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5837 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface2);
5838 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5841 IDirectDrawSurface_Release(surface4);
5842 IDirectDrawSurface_Release(surface3);
5843 IDirectDrawSurface_Release(surface2);
5844 IDirectDrawSurface_Release(surface1);
5846 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
5847 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5849 /* Try a single primary and two offscreen plain surfaces. */
5850 memset(&surface_desc, 0, sizeof(surface_desc));
5851 surface_desc.dwSize = sizeof(surface_desc);
5852 surface_desc.dwFlags = DDSD_CAPS;
5853 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
5854 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
5855 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5857 memset(&surface_desc, 0, sizeof(surface_desc));
5858 surface_desc.dwSize = sizeof(surface_desc);
5859 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5860 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5861 surface_desc.dwWidth = registry_mode.dmPelsWidth;
5862 surface_desc.dwHeight = registry_mode.dmPelsHeight;
5863 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
5864 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5866 memset(&surface_desc, 0, sizeof(surface_desc));
5867 surface_desc.dwSize = sizeof(surface_desc);
5868 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5869 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5870 surface_desc.dwWidth = registry_mode.dmPelsWidth;
5871 surface_desc.dwHeight = registry_mode.dmPelsHeight;
5872 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
5873 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5875 /* This one has a different size. */
5876 memset(&surface_desc, 0, sizeof(surface_desc));
5877 surface_desc.dwSize = sizeof(surface_desc);
5878 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5879 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5880 surface_desc.dwWidth = 128;
5881 surface_desc.dwHeight = 128;
5882 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
5883 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5885 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
5886 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
5887 /* Try the reverse without detaching first. */
5888 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
5889 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
5890 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
5891 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
5893 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
5894 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
5895 /* Try to detach reversed. */
5896 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
5897 ok(hr == DDERR_CANNOTDETACHSURFACE, "Got unexpected hr %#x.\n", hr);
5898 hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface1);
5899 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
5901 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface3);
5902 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
5903 hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface3);
5904 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
5906 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4);
5907 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5908 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
5909 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5911 IDirectDrawSurface_Release(surface4);
5912 IDirectDrawSurface_Release(surface3);
5913 IDirectDrawSurface_Release(surface2);
5914 IDirectDrawSurface_Release(surface1);
5916 /* Test DeleteAttachedSurface() and automatic detachment of attached surfaces on release. */
5917 memset(&surface_desc, 0, sizeof(surface_desc));
5918 surface_desc.dwSize = sizeof(surface_desc);
5919 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5920 surface_desc.dwWidth = 64;
5921 surface_desc.dwHeight = 64;
5922 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
5923 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
5924 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB; /* D3DFMT_R5G6B5 */
5925 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 16;
5926 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0xf800;
5927 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x07e0;
5928 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x001f;
5929 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
5930 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5931 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
5932 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5934 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
5935 surface_desc.ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
5936 U1(surface_desc.ddpfPixelFormat).dwZBufferBitDepth = 16;
5937 U3(surface_desc.ddpfPixelFormat).dwZBitMask = 0x0000ffff;
5938 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
5939 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5941 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
5942 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
5943 refcount = get_refcount((IUnknown *)surface2);
5944 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
5945 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
5946 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
5948 /* Attaching while already attached to other surface. */
5949 hr = IDirectDrawSurface_AddAttachedSurface(surface3, surface2);
5950 todo_wine ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
5951 hr = IDirectDrawSurface_DeleteAttachedSurface(surface3, 0, surface2);
5952 todo_wine ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
5953 IDirectDrawSurface_Release(surface3);
5955 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
5956 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
5957 refcount = get_refcount((IUnknown *)surface2);
5958 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
5960 /* Automatic detachment on release. */
5961 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
5962 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
5963 refcount = get_refcount((IUnknown *)surface2);
5964 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
5965 refcount = IDirectDrawSurface_Release(surface1);
5966 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5967 refcount = IDirectDrawSurface_Release(surface2);
5968 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5969 refcount = IDirectDraw2_Release(ddraw);
5970 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5971 DestroyWindow(window);
5974 static void test_pixel_format(void)
5976 HWND window, window2 = NULL;
5977 HDC hdc, hdc2 = NULL;
5978 HMODULE gl = NULL;
5979 int format, test_format;
5980 PIXELFORMATDESCRIPTOR pfd;
5981 IDirectDraw2 *ddraw = NULL;
5982 IDirectDrawClipper *clipper = NULL;
5983 DDSURFACEDESC ddsd;
5984 IDirectDrawSurface *primary = NULL;
5985 DDBLTFX fx;
5986 HRESULT hr;
5988 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
5989 100, 100, 160, 160, NULL, NULL, NULL, NULL);
5990 if (!window)
5992 skip("Failed to create window\n");
5993 return;
5996 window2 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
5997 100, 100, 160, 160, NULL, NULL, NULL, NULL);
5999 hdc = GetDC(window);
6000 if (!hdc)
6002 skip("Failed to get DC\n");
6003 goto cleanup;
6006 if (window2)
6007 hdc2 = GetDC(window2);
6009 gl = LoadLibraryA("opengl32.dll");
6010 ok(!!gl, "failed to load opengl32.dll; SetPixelFormat()/GetPixelFormat() may not work right\n");
6012 format = GetPixelFormat(hdc);
6013 ok(format == 0, "new window has pixel format %d\n", format);
6015 ZeroMemory(&pfd, sizeof(pfd));
6016 pfd.nSize = sizeof(pfd);
6017 pfd.nVersion = 1;
6018 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
6019 pfd.iPixelType = PFD_TYPE_RGBA;
6020 pfd.iLayerType = PFD_MAIN_PLANE;
6021 format = ChoosePixelFormat(hdc, &pfd);
6022 if (format <= 0)
6024 skip("no pixel format available\n");
6025 goto cleanup;
6028 if (!SetPixelFormat(hdc, format, &pfd) || GetPixelFormat(hdc) != format)
6030 skip("failed to set pixel format\n");
6031 goto cleanup;
6034 if (!hdc2 || !SetPixelFormat(hdc2, format, &pfd) || GetPixelFormat(hdc2) != format)
6036 skip("failed to set pixel format on second window\n");
6037 if (hdc2)
6039 ReleaseDC(window2, hdc2);
6040 hdc2 = NULL;
6044 ddraw = create_ddraw();
6045 ok(!!ddraw, "Failed to create a ddraw object.\n");
6047 test_format = GetPixelFormat(hdc);
6048 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6050 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6051 if (FAILED(hr))
6053 skip("Failed to set cooperative level, hr %#x.\n", hr);
6054 goto cleanup;
6057 test_format = GetPixelFormat(hdc);
6058 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6060 if (hdc2)
6062 hr = IDirectDraw2_CreateClipper(ddraw, 0, &clipper, NULL);
6063 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
6064 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window2);
6065 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
6067 test_format = GetPixelFormat(hdc);
6068 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6070 test_format = GetPixelFormat(hdc2);
6071 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6074 memset(&ddsd, 0, sizeof(ddsd));
6075 ddsd.dwSize = sizeof(ddsd);
6076 ddsd.dwFlags = DDSD_CAPS;
6077 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
6079 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &primary, NULL);
6080 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
6082 test_format = GetPixelFormat(hdc);
6083 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6085 if (hdc2)
6087 test_format = GetPixelFormat(hdc2);
6088 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6091 if (clipper)
6093 hr = IDirectDrawSurface2_SetClipper(primary, clipper);
6094 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
6096 test_format = GetPixelFormat(hdc);
6097 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6099 test_format = GetPixelFormat(hdc2);
6100 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6103 memset(&fx, 0, sizeof(fx));
6104 fx.dwSize = sizeof(fx);
6105 hr = IDirectDrawSurface2_Blt(primary, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6106 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
6108 test_format = GetPixelFormat(hdc);
6109 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6111 if (hdc2)
6113 test_format = GetPixelFormat(hdc2);
6114 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6117 cleanup:
6118 if (primary) IDirectDrawSurface2_Release(primary);
6119 if (clipper) IDirectDrawClipper_Release(clipper);
6120 if (ddraw) IDirectDraw2_Release(ddraw);
6121 if (gl) FreeLibrary(gl);
6122 if (hdc) ReleaseDC(window, hdc);
6123 if (hdc2) ReleaseDC(window2, hdc2);
6124 if (window) DestroyWindow(window);
6125 if (window2) DestroyWindow(window2);
6128 static void test_create_surface_pitch(void)
6130 IDirectDrawSurface *surface;
6131 DDSURFACEDESC surface_desc;
6132 IDirectDraw2 *ddraw;
6133 unsigned int i;
6134 ULONG refcount;
6135 HWND window;
6136 HRESULT hr;
6137 void *mem;
6139 static const struct
6141 DWORD caps;
6142 DWORD flags_in;
6143 DWORD pitch_in;
6144 HRESULT hr;
6145 DWORD flags_out;
6146 DWORD pitch_out32;
6147 DWORD pitch_out64;
6149 test_data[] =
6151 /* 0 */
6152 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
6153 0, 0, DD_OK,
6154 DDSD_PITCH, 0x100, 0x100},
6155 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
6156 DDSD_PITCH, 0x104, DD_OK,
6157 DDSD_PITCH, 0x100, 0x100},
6158 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
6159 DDSD_PITCH, 0x0f8, DD_OK,
6160 DDSD_PITCH, 0x100, 0x100},
6161 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
6162 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDCAPS,
6163 0, 0, 0 },
6164 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
6165 0, 0, DD_OK,
6166 DDSD_PITCH, 0x100, 0x0fc},
6167 /* 5 */
6168 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
6169 DDSD_PITCH, 0x104, DD_OK,
6170 DDSD_PITCH, 0x100, 0x0fc},
6171 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
6172 DDSD_PITCH, 0x0f8, DD_OK,
6173 DDSD_PITCH, 0x100, 0x0fc},
6174 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
6175 DDSD_PITCH | DDSD_LINEARSIZE, 0, DD_OK,
6176 DDSD_PITCH, 0x100, 0x0fc},
6177 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
6178 DDSD_LPSURFACE, 0, DDERR_INVALIDPARAMS,
6179 0, 0, 0 },
6180 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
6181 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDPARAMS,
6182 0, 0, 0 },
6183 /* 10 */
6184 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_ALLOCONLOAD,
6185 0, 0, DDERR_INVALIDCAPS,
6186 0, 0, 0 },
6187 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
6188 0, 0, DD_OK,
6189 DDSD_PITCH, 0x100, 0 },
6190 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
6191 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDCAPS,
6192 0, 0, 0 },
6193 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_ALLOCONLOAD,
6194 0, 0, DDERR_INVALIDCAPS,
6195 0, 0, 0 },
6196 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
6197 0, 0, DD_OK,
6198 DDSD_PITCH, 0x100, 0 },
6199 /* 15 */
6200 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
6201 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDPARAMS,
6202 0, 0, 0 },
6204 DWORD flags_mask = DDSD_PITCH | DDSD_LPSURFACE | DDSD_LINEARSIZE;
6206 window = create_window();
6207 ddraw = create_ddraw();
6208 ok(!!ddraw, "Failed to create a ddraw object.\n");
6209 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6210 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6212 mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ((63 * 4) + 8) * 63);
6214 for (i = 0; i < ARRAY_SIZE(test_data); ++i)
6216 memset(&surface_desc, 0, sizeof(surface_desc));
6217 surface_desc.dwSize = sizeof(surface_desc);
6218 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | test_data[i].flags_in;
6219 surface_desc.ddsCaps.dwCaps = test_data[i].caps;
6220 surface_desc.dwWidth = 63;
6221 surface_desc.dwHeight = 63;
6222 U1(surface_desc).lPitch = test_data[i].pitch_in;
6223 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
6224 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
6225 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
6226 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6227 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6228 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
6229 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6230 if (test_data[i].flags_in & DDSD_LPSURFACE)
6232 HRESULT expected_hr = SUCCEEDED(test_data[i].hr) ? DDERR_INVALIDPARAMS : test_data[i].hr;
6233 ok(hr == expected_hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, expected_hr);
6234 surface_desc.lpSurface = mem;
6235 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6237 if ((test_data[i].caps & DDSCAPS_VIDEOMEMORY) && hr == DDERR_NODIRECTDRAWHW)
6238 continue;
6239 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
6240 if (FAILED(hr))
6241 continue;
6243 memset(&surface_desc, 0, sizeof(surface_desc));
6244 surface_desc.dwSize = sizeof(surface_desc);
6245 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
6246 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
6247 ok((surface_desc.dwFlags & flags_mask) == test_data[i].flags_out,
6248 "Test %u: Got unexpected flags %#x, expected %#x.\n",
6249 i, surface_desc.dwFlags & flags_mask, test_data[i].flags_out);
6250 /* The pitch for textures seems to be implementation specific. */
6251 if (!(test_data[i].caps & DDSCAPS_TEXTURE))
6253 if (is_ddraw64 && test_data[i].pitch_out32 != test_data[i].pitch_out64)
6254 todo_wine ok(U1(surface_desc).lPitch == test_data[i].pitch_out64,
6255 "Test %u: Got unexpected pitch %u, expected %u.\n",
6256 i, U1(surface_desc).lPitch, test_data[i].pitch_out64);
6257 else
6258 ok(U1(surface_desc).lPitch == test_data[i].pitch_out32,
6259 "Test %u: Got unexpected pitch %u, expected %u.\n",
6260 i, U1(surface_desc).lPitch, test_data[i].pitch_out32);
6262 ok(!surface_desc.lpSurface, "Test %u: Got unexpected lpSurface %p.\n", i, surface_desc.lpSurface);
6264 IDirectDrawSurface_Release(surface);
6267 HeapFree(GetProcessHeap(), 0, mem);
6268 refcount = IDirectDraw2_Release(ddraw);
6269 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6270 DestroyWindow(window);
6273 static void test_mipmap(void)
6275 IDirectDrawSurface *surface1;
6276 IDirectDrawSurface2 *surface, *surface2;
6277 DDSURFACEDESC surface_desc;
6278 IDirectDraw2 *ddraw;
6279 unsigned int i;
6280 ULONG refcount;
6281 HWND window;
6282 HRESULT hr;
6283 DDSCAPS caps = {DDSCAPS_COMPLEX};
6284 DDCAPS hal_caps;
6286 static const struct
6288 DWORD flags;
6289 DWORD caps;
6290 DWORD width;
6291 DWORD height;
6292 DWORD mipmap_count_in;
6293 HRESULT hr;
6294 DWORD mipmap_count_out;
6296 tests[] =
6298 {DDSD_MIPMAPCOUNT, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 128, 32, 3, DD_OK, 3},
6299 {DDSD_MIPMAPCOUNT, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 128, 32, 0, DDERR_INVALIDPARAMS, 0},
6300 {0, DDSCAPS_TEXTURE | DDSCAPS_MIPMAP, 128, 32, 0, DD_OK, 1},
6301 {0, DDSCAPS_MIPMAP, 128, 32, 0, DDERR_INVALIDCAPS, 0},
6302 {0, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 128, 32, 0, DD_OK, 6},
6303 {0, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 32, 64, 0, DD_OK, 6},
6306 window = create_window();
6307 ddraw = create_ddraw();
6308 ok(!!ddraw, "Failed to create a ddraw object.\n");
6309 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6310 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6312 memset(&hal_caps, 0, sizeof(hal_caps));
6313 hal_caps.dwSize = sizeof(hal_caps);
6314 hr = IDirectDraw2_GetCaps(ddraw, &hal_caps, NULL);
6315 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
6316 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
6318 skip("Mipmapped textures not supported, skipping tests.\n");
6319 IDirectDraw2_Release(ddraw);
6320 DestroyWindow(window);
6321 return;
6324 for (i = 0; i < ARRAY_SIZE(tests); ++i)
6326 memset(&surface_desc, 0, sizeof(surface_desc));
6327 surface_desc.dwSize = sizeof(surface_desc);
6328 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | tests[i].flags;
6329 surface_desc.ddsCaps.dwCaps = tests[i].caps;
6330 surface_desc.dwWidth = tests[i].width;
6331 surface_desc.dwHeight = tests[i].height;
6332 if (tests[i].flags & DDSD_MIPMAPCOUNT)
6333 U2(surface_desc).dwMipMapCount = tests[i].mipmap_count_in;
6334 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
6335 ok(hr == tests[i].hr, "Test %u: Got unexpected hr %#x.\n", i, hr);
6336 if (FAILED(hr))
6337 continue;
6339 hr = IDirectDrawSurface_QueryInterface(surface1, &IID_IDirectDrawSurface2, (void **)&surface);
6340 ok(SUCCEEDED(hr), "Test %u: Failed to get IDirectDrawSurface2 interface, hr %#x.\n", i, hr);
6341 IDirectDrawSurface_Release(surface1);
6343 memset(&surface_desc, 0, sizeof(surface_desc));
6344 surface_desc.dwSize = sizeof(surface_desc);
6345 hr = IDirectDrawSurface2_GetSurfaceDesc(surface, &surface_desc);
6346 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
6347 ok(surface_desc.dwFlags & DDSD_MIPMAPCOUNT,
6348 "Test %u: Got unexpected flags %#x.\n", i, surface_desc.dwFlags);
6349 ok(U2(surface_desc).dwMipMapCount == tests[i].mipmap_count_out,
6350 "Test %u: Got unexpected mipmap count %u.\n", i, U2(surface_desc).dwMipMapCount);
6352 if (U2(surface_desc).dwMipMapCount > 1)
6354 hr = IDirectDrawSurface2_GetAttachedSurface(surface, &caps, &surface2);
6355 ok(SUCCEEDED(hr), "Test %u: Failed to get attached surface, hr %#x.\n", i, hr);
6357 memset(&surface_desc, 0, sizeof(surface_desc));
6358 surface_desc.dwSize = sizeof(surface_desc);
6359 hr = IDirectDrawSurface2_Lock(surface, NULL, &surface_desc, 0, NULL);
6360 ok(SUCCEEDED(hr), "Test %u: Failed to lock surface, hr %#x.\n", i, hr);
6361 memset(&surface_desc, 0, sizeof(surface_desc));
6362 surface_desc.dwSize = sizeof(surface_desc);
6363 hr = IDirectDrawSurface2_Lock(surface2, NULL, &surface_desc, 0, NULL);
6364 ok(SUCCEEDED(hr), "Test %u: Failed to lock surface, hr %#x.\n", i, hr);
6365 IDirectDrawSurface2_Unlock(surface2, NULL);
6366 IDirectDrawSurface2_Unlock(surface, NULL);
6368 IDirectDrawSurface2_Release(surface2);
6371 IDirectDrawSurface2_Release(surface);
6374 refcount = IDirectDraw2_Release(ddraw);
6375 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6376 DestroyWindow(window);
6379 static void test_palette_complex(void)
6381 IDirectDrawSurface *surface1;
6382 IDirectDrawSurface2 *surface, *mipmap, *tmp;
6383 DDSURFACEDESC surface_desc;
6384 IDirectDraw2 *ddraw;
6385 IDirectDrawPalette *palette, *palette2, *palette_mipmap;
6386 ULONG refcount;
6387 HWND window;
6388 HRESULT hr;
6389 DDSCAPS caps = {DDSCAPS_COMPLEX};
6390 DDCAPS hal_caps;
6391 PALETTEENTRY palette_entries[256];
6392 unsigned int i;
6393 HDC dc;
6394 RGBQUAD rgbquad;
6395 UINT count;
6397 window = create_window();
6398 ddraw = create_ddraw();
6399 ok(!!ddraw, "Failed to create a ddraw object.\n");
6400 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6401 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6403 memset(&hal_caps, 0, sizeof(hal_caps));
6404 hal_caps.dwSize = sizeof(hal_caps);
6405 hr = IDirectDraw2_GetCaps(ddraw, &hal_caps, NULL);
6406 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
6407 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
6409 skip("Mipmapped textures not supported, skipping mipmap palette test.\n");
6410 IDirectDraw2_Release(ddraw);
6411 DestroyWindow(window);
6412 return;
6415 memset(&surface_desc, 0, sizeof(surface_desc));
6416 surface_desc.dwSize = sizeof(surface_desc);
6417 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6418 surface_desc.dwWidth = 128;
6419 surface_desc.dwHeight = 128;
6420 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
6421 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
6422 surface_desc.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
6423 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 8;
6424 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
6425 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6426 hr = IDirectDrawSurface_QueryInterface(surface1, &IID_IDirectDrawSurface2, (void **)&surface);
6427 ok(SUCCEEDED(hr), "Failed to get IDirectDrawSurface2 interface, hr %#x.\n", hr);
6428 IDirectDrawSurface_Release(surface1);
6430 memset(palette_entries, 0, sizeof(palette_entries));
6431 hr = IDirectDraw2_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
6432 palette_entries, &palette, NULL);
6433 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6435 memset(palette_entries, 0, sizeof(palette_entries));
6436 palette_entries[1].peRed = 0xff;
6437 palette_entries[1].peGreen = 0x80;
6438 hr = IDirectDraw2_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
6439 palette_entries, &palette_mipmap, NULL);
6440 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6442 palette2 = (void *)0xdeadbeef;
6443 hr = IDirectDrawSurface2_GetPalette(surface, &palette2);
6444 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
6445 ok(!palette2, "Got unexpected palette %p.\n", palette2);
6446 hr = IDirectDrawSurface2_SetPalette(surface, palette);
6447 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6448 hr = IDirectDrawSurface2_GetPalette(surface, &palette2);
6449 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
6450 ok(palette == palette2, "Got unexpected palette %p.\n", palette2);
6451 IDirectDrawPalette_Release(palette2);
6453 mipmap = surface;
6454 IDirectDrawSurface2_AddRef(mipmap);
6455 for (i = 0; i < 7; ++i)
6457 hr = IDirectDrawSurface2_GetAttachedSurface(mipmap, &caps, &tmp);
6458 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
6459 palette2 = (void *)0xdeadbeef;
6460 hr = IDirectDrawSurface2_GetPalette(tmp, &palette2);
6461 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x, i %u.\n", hr, i);
6462 ok(!palette2, "Got unexpected palette %p, i %u.\n", palette2, i);
6464 hr = IDirectDrawSurface2_SetPalette(tmp, palette_mipmap);
6465 ok(SUCCEEDED(hr), "Failed to set palette, i %u, hr %#x.\n", i, hr);
6467 hr = IDirectDrawSurface2_GetPalette(tmp, &palette2);
6468 ok(SUCCEEDED(hr), "Failed to get palette, i %u, hr %#x.\n", i, hr);
6469 ok(palette_mipmap == palette2, "Got unexpected palette %p.\n", palette2);
6470 IDirectDrawPalette_Release(palette2);
6472 hr = IDirectDrawSurface2_GetDC(tmp, &dc);
6473 ok(SUCCEEDED(hr), "Failed to get DC, i %u, hr %#x.\n", i, hr);
6474 count = GetDIBColorTable(dc, 1, 1, &rgbquad);
6475 ok(count == 1, "Expected count 1, got %u.\n", count);
6476 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x.\n", rgbquad.rgbRed);
6477 ok(rgbquad.rgbGreen == 0x80, "Expected rgbGreen = 0x80, got %#x.\n", rgbquad.rgbGreen);
6478 ok(rgbquad.rgbBlue == 0x0, "Expected rgbBlue = 0x0, got %#x.\n", rgbquad.rgbBlue);
6479 hr = IDirectDrawSurface2_ReleaseDC(tmp, dc);
6480 ok(SUCCEEDED(hr), "Failed to release DC, i %u, hr %#x.\n", i, hr);
6482 IDirectDrawSurface2_Release(mipmap);
6483 mipmap = tmp;
6486 hr = IDirectDrawSurface2_GetAttachedSurface(mipmap, &caps, &tmp);
6487 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
6488 IDirectDrawSurface2_Release(mipmap);
6489 refcount = IDirectDrawSurface2_Release(surface);
6490 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6491 refcount = IDirectDrawPalette_Release(palette_mipmap);
6492 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6493 refcount = IDirectDrawPalette_Release(palette);
6494 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6496 refcount = IDirectDraw2_Release(ddraw);
6497 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6498 DestroyWindow(window);
6501 static void test_p8_blit(void)
6503 IDirectDrawSurface *src, *dst, *dst_p8;
6504 DDSURFACEDESC surface_desc;
6505 IDirectDraw2 *ddraw;
6506 IDirectDrawPalette *palette, *palette2;
6507 ULONG refcount;
6508 HWND window;
6509 HRESULT hr;
6510 PALETTEENTRY palette_entries[256];
6511 unsigned int x;
6512 DDBLTFX fx;
6513 BOOL is_warp;
6514 static const BYTE src_data[] = {0x10, 0x1, 0x2, 0x3, 0x4, 0x5, 0xff, 0x80};
6515 static const BYTE src_data2[] = {0x10, 0x5, 0x4, 0x3, 0x2, 0x1, 0xff, 0x80};
6516 static const BYTE expected_p8[] = {0x10, 0x1, 0x4, 0x3, 0x4, 0x5, 0xff, 0x80};
6517 static const D3DCOLOR expected[] =
6519 0x00101010, 0x00010101, 0x00020202, 0x00030303,
6520 0x00040404, 0x00050505, 0x00ffffff, 0x00808080,
6522 D3DCOLOR color;
6524 window = create_window();
6525 ddraw = create_ddraw();
6526 ok(!!ddraw, "Failed to create a ddraw object.\n");
6527 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6528 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6529 is_warp = ddraw_is_warp(ddraw);
6531 memset(palette_entries, 0, sizeof(palette_entries));
6532 palette_entries[1].peGreen = 0xff;
6533 palette_entries[2].peBlue = 0xff;
6534 palette_entries[3].peFlags = 0xff;
6535 palette_entries[4].peRed = 0xff;
6536 hr = IDirectDraw2_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
6537 palette_entries, &palette, NULL);
6538 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6539 palette_entries[1].peBlue = 0xff;
6540 palette_entries[2].peGreen = 0xff;
6541 palette_entries[3].peRed = 0xff;
6542 palette_entries[4].peFlags = 0x0;
6543 hr = IDirectDraw2_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
6544 palette_entries, &palette2, NULL);
6545 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6547 memset(&surface_desc, 0, sizeof(surface_desc));
6548 surface_desc.dwSize = sizeof(surface_desc);
6549 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6550 surface_desc.dwWidth = 8;
6551 surface_desc.dwHeight = 1;
6552 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6553 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
6554 surface_desc.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
6555 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 8;
6556 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &src, NULL);
6557 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6558 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &dst_p8, NULL);
6559 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6560 hr = IDirectDrawSurface_SetPalette(dst_p8, palette2);
6561 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6563 memset(&surface_desc, 0, sizeof(surface_desc));
6564 surface_desc.dwSize = sizeof(surface_desc);
6565 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6566 surface_desc.dwWidth = 8;
6567 surface_desc.dwHeight = 1;
6568 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6569 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
6570 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
6571 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
6572 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6573 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6574 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
6575 U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
6576 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &dst, NULL);
6577 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6579 memset(&surface_desc, 0, sizeof(surface_desc));
6580 surface_desc.dwSize = sizeof(surface_desc);
6581 hr = IDirectDrawSurface_Lock(src, NULL, &surface_desc, DDLOCK_WAIT, NULL);
6582 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
6583 memcpy(surface_desc.lpSurface, src_data, sizeof(src_data));
6584 hr = IDirectDrawSurface_Unlock(src, NULL);
6585 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
6587 hr = IDirectDrawSurface_Lock(dst_p8, NULL, &surface_desc, DDLOCK_WAIT, NULL);
6588 ok(SUCCEEDED(hr), "Failed to lock destination surface, hr %#x.\n", hr);
6589 memcpy(surface_desc.lpSurface, src_data2, sizeof(src_data2));
6590 hr = IDirectDrawSurface_Unlock(dst_p8, NULL);
6591 ok(SUCCEEDED(hr), "Failed to unlock destination surface, hr %#x.\n", hr);
6593 hr = IDirectDrawSurface_SetPalette(src, palette);
6594 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6595 hr = IDirectDrawSurface_Blt(dst, NULL, src, NULL, DDBLT_WAIT, NULL);
6596 /* The r500 Windows 7 driver returns E_NOTIMPL. r200 on Windows XP works.
6597 * The Geforce 7 driver on Windows Vista returns E_FAIL. Newer Nvidia GPUs work. */
6598 ok(SUCCEEDED(hr) || broken(hr == E_NOTIMPL) || broken(hr == E_FAIL),
6599 "Failed to blit, hr %#x.\n", hr);
6601 if (SUCCEEDED(hr))
6603 for (x = 0; x < ARRAY_SIZE(expected); x++)
6605 color = get_surface_color(dst, x, 0);
6606 todo_wine ok(compare_color(color, expected[x], 0),
6607 "Pixel %u: Got color %#x, expected %#x.\n",
6608 x, color, expected[x]);
6612 memset(&fx, 0, sizeof(fx));
6613 fx.dwSize = sizeof(fx);
6614 fx.ddckSrcColorkey.dwColorSpaceHighValue = 0x2;
6615 fx.ddckSrcColorkey.dwColorSpaceLowValue = 0x2;
6616 hr = IDirectDrawSurface7_Blt(dst_p8, NULL, src, NULL, DDBLT_WAIT | DDBLT_KEYSRCOVERRIDE, &fx);
6617 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
6619 hr = IDirectDrawSurface_Lock(dst_p8, NULL, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
6620 ok(SUCCEEDED(hr), "Failed to lock destination surface, hr %#x.\n", hr);
6621 /* A color keyed P8 blit doesn't do anything on WARP - it just leaves the data in the destination
6622 * surface untouched. P8 blits without color keys work. Error checking (DDBLT_KEYSRC without a key
6623 * for example) also works as expected.
6625 * Using DDBLT_KEYSRC instead of DDBLT_KEYSRCOVERRIDE doesn't change this. Doing this blit with
6626 * the display mode set to P8 doesn't help either. */
6627 ok(!memcmp(surface_desc.lpSurface, expected_p8, sizeof(expected_p8))
6628 || broken(is_warp && !memcmp(surface_desc.lpSurface, src_data2, sizeof(src_data2))),
6629 "Got unexpected P8 color key blit result.\n");
6630 hr = IDirectDrawSurface_Unlock(dst_p8, NULL);
6631 ok(SUCCEEDED(hr), "Failed to unlock destination surface, hr %#x.\n", hr);
6633 IDirectDrawSurface_Release(src);
6634 IDirectDrawSurface_Release(dst);
6635 IDirectDrawSurface_Release(dst_p8);
6636 IDirectDrawPalette_Release(palette);
6637 IDirectDrawPalette_Release(palette2);
6639 refcount = IDirectDraw2_Release(ddraw);
6640 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6641 DestroyWindow(window);
6644 static void test_material(void)
6646 IDirect3DMaterial2 *background, *material;
6647 D3DMATERIALHANDLE mat_handle, tmp;
6648 IDirect3DViewport2 *viewport;
6649 IDirect3DDevice2 *device;
6650 IDirectDrawSurface *rt;
6651 IDirectDraw2 *ddraw;
6652 D3DCOLOR color;
6653 ULONG refcount;
6654 unsigned int i;
6655 HWND window;
6656 HRESULT hr;
6657 BOOL valid;
6659 static D3DVERTEX quad[] =
6661 {{-1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
6662 {{-1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
6663 {{ 1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
6664 {{ 1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
6666 static const struct
6668 BOOL material;
6669 D3DCOLOR expected_color;
6671 test_data[] =
6673 {TRUE, 0x0000ff00},
6674 {FALSE, 0x00ffffff},
6676 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
6678 window = create_window();
6679 ddraw = create_ddraw();
6680 ok(!!ddraw, "Failed to create a ddraw object.\n");
6681 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
6683 skip("Failed to create a 3D device, skipping test.\n");
6684 DestroyWindow(window);
6685 return;
6688 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
6689 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
6691 background = create_diffuse_material(device, 0.0f, 0.0f, 1.0f, 1.0f);
6692 viewport = create_viewport(device, 0, 0, 640, 480);
6693 viewport_set_background(device, viewport, background);
6694 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
6695 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
6697 material = create_emissive_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
6698 hr = IDirect3DMaterial2_GetHandle(material, device, &mat_handle);
6699 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
6701 hr = IDirect3DDevice2_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
6702 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
6703 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
6704 hr = IDirect3DDevice2_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
6705 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
6706 hr = IDirect3DDevice2_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
6707 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
6708 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
6709 hr = IDirect3DDevice2_SetLightState(device, D3DLIGHTSTATE_MATERIAL, 0);
6710 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
6711 hr = IDirect3DDevice2_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
6712 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
6713 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
6715 for (i = 0; i < ARRAY_SIZE(test_data); ++i)
6717 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER);
6718 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
6720 hr = IDirect3DDevice2_SetLightState(device, D3DLIGHTSTATE_MATERIAL, test_data[i].material ? mat_handle : 0);
6721 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
6723 hr = IDirect3DDevice2_BeginScene(device);
6724 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
6725 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_VERTEX, quad, 4, 0);
6726 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
6727 hr = IDirect3DDevice2_EndScene(device);
6728 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
6729 color = get_surface_color(rt, 320, 240);
6730 ok(compare_color(color, test_data[i].expected_color, 1),
6731 "Got unexpected color 0x%08x, test %u.\n", color, i);
6734 destroy_material(material);
6735 material = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
6736 hr = IDirect3DMaterial2_GetHandle(material, device, &mat_handle);
6737 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
6739 hr = IDirect3DViewport2_SetBackground(viewport, mat_handle);
6740 ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
6741 hr = IDirect3DViewport2_GetBackground(viewport, &tmp, &valid);
6742 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
6743 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
6744 ok(valid, "Got unexpected valid %#x.\n", valid);
6745 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6746 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
6747 color = get_surface_color(rt, 320, 240);
6748 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
6750 hr = IDirect3DViewport2_SetBackground(viewport, 0);
6751 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6752 hr = IDirect3DViewport2_GetBackground(viewport, &tmp, &valid);
6753 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
6754 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
6755 ok(valid, "Got unexpected valid %#x.\n", valid);
6756 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6757 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
6758 color = get_surface_color(rt, 320, 240);
6759 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
6761 destroy_viewport(device, viewport);
6762 viewport = create_viewport(device, 0, 0, 640, 480);
6764 hr = IDirect3DViewport2_GetBackground(viewport, &tmp, &valid);
6765 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
6766 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
6767 ok(!valid, "Got unexpected valid %#x.\n", valid);
6768 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6769 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
6770 color = get_surface_color(rt, 320, 240);
6771 ok(compare_color(color, 0x00000000, 1), "Got unexpected color 0x%08x.\n", color);
6773 destroy_viewport(device, viewport);
6774 destroy_material(background);
6775 destroy_material(material);
6776 IDirectDrawSurface_Release(rt);
6777 refcount = IDirect3DDevice2_Release(device);
6778 ok(!refcount, "Device has %u references left.\n", refcount);
6779 refcount = IDirectDraw2_Release(ddraw);
6780 ok(!refcount, "Ddraw object has %u references left.\n", refcount);
6781 DestroyWindow(window);
6784 static void test_lighting(void)
6786 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
6787 static D3DMATRIX mat =
6789 1.0f, 0.0f, 0.0f, 0.0f,
6790 0.0f, 1.0f, 0.0f, 0.0f,
6791 0.0f, 0.0f, 1.0f, 0.0f,
6792 0.0f, 0.0f, 0.0f, 1.0f,
6794 mat_singular =
6796 1.0f, 0.0f, 1.0f, 0.0f,
6797 0.0f, 1.0f, 0.0f, 0.0f,
6798 1.0f, 0.0f, 1.0f, 0.0f,
6799 0.0f, 0.0f, 0.5f, 1.0f,
6801 mat_transf =
6803 0.0f, 0.0f, 1.0f, 0.0f,
6804 0.0f, 1.0f, 0.0f, 0.0f,
6805 -1.0f, 0.0f, 0.0f, 0.0f,
6806 10.f, 10.0f, 10.0f, 1.0f,
6808 mat_nonaffine =
6810 1.0f, 0.0f, 0.0f, 0.0f,
6811 0.0f, 1.0f, 0.0f, 0.0f,
6812 0.0f, 0.0f, 1.0f, -1.0f,
6813 10.f, 10.0f, 10.0f, 0.0f,
6815 static D3DLVERTEX unlitquad[] =
6817 {{-1.0f}, {-1.0f}, {0.1f}, 0, {0xffff0000}, {0}, {0.0f}, {0.0f}},
6818 {{-1.0f}, { 0.0f}, {0.1f}, 0, {0xffff0000}, {0}, {0.0f}, {0.0f}},
6819 {{ 0.0f}, { 0.0f}, {0.1f}, 0, {0xffff0000}, {0}, {0.0f}, {0.0f}},
6820 {{ 0.0f}, {-1.0f}, {0.1f}, 0, {0xffff0000}, {0}, {0.0f}, {0.0f}},
6822 litquad[] =
6824 {{-1.0f}, { 0.0f}, {0.1f}, 0, {0xff00ff00}, {0}, {0.0f}, {0.0f}},
6825 {{-1.0f}, { 1.0f}, {0.1f}, 0, {0xff00ff00}, {0}, {0.0f}, {0.0f}},
6826 {{ 0.0f}, { 1.0f}, {0.1f}, 0, {0xff00ff00}, {0}, {0.0f}, {0.0f}},
6827 {{ 0.0f}, { 0.0f}, {0.1f}, 0, {0xff00ff00}, {0}, {0.0f}, {0.0f}},
6829 static D3DVERTEX unlitnquad[] =
6831 {{0.0f}, {-1.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
6832 {{0.0f}, { 0.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
6833 {{1.0f}, { 0.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
6834 {{1.0f}, {-1.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
6836 litnquad[] =
6838 {{0.0f}, { 0.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
6839 {{0.0f}, { 1.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
6840 {{1.0f}, { 1.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
6841 {{1.0f}, { 0.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
6843 nquad[] =
6845 {{-1.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
6846 {{-1.0f}, { 1.0f}, {0.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
6847 {{ 1.0f}, { 1.0f}, {0.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
6848 {{ 1.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
6850 rotatedquad[] =
6852 {{-10.0f}, {-11.0f}, {11.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {0.0f}},
6853 {{-10.0f}, { -9.0f}, {11.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {0.0f}},
6854 {{-10.0f}, { -9.0f}, { 9.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {0.0f}},
6855 {{-10.0f}, {-11.0f}, { 9.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {0.0f}},
6857 translatedquad[] =
6859 {{-11.0f}, {-11.0f}, {-10.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
6860 {{-11.0f}, { -9.0f}, {-10.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
6861 {{ -9.0f}, { -9.0f}, {-10.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
6862 {{ -9.0f}, {-11.0f}, {-10.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
6864 static WORD indices[] = {0, 1, 2, 2, 3, 0};
6865 static const struct
6867 D3DMATRIX *world_matrix;
6868 void *quad;
6869 DWORD expected;
6870 const char *message;
6872 tests[] =
6874 {&mat, nquad, 0x000000ff, "Lit quad with light"},
6875 {&mat_singular, nquad, 0x000000b4, "Lit quad with singular world matrix"},
6876 {&mat_transf, rotatedquad, 0x000000ff, "Lit quad with transformation matrix"},
6877 {&mat_nonaffine, translatedquad, 0x000000ff, "Lit quad with non-affine matrix"},
6880 HWND window;
6881 IDirect3D2 *d3d;
6882 IDirect3DDevice2 *device;
6883 IDirectDraw2 *ddraw;
6884 IDirectDrawSurface *rt;
6885 IDirect3DViewport2 *viewport;
6886 IDirect3DMaterial2 *material;
6887 IDirect3DLight *light;
6888 D3DMATERIALHANDLE mat_handle;
6889 D3DLIGHT2 light_desc;
6890 HRESULT hr;
6891 D3DCOLOR color;
6892 ULONG refcount;
6893 unsigned int i;
6895 window = create_window();
6896 ddraw = create_ddraw();
6897 ok(!!ddraw, "Failed to create a ddraw object.\n");
6898 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
6900 skip("Failed to create a 3D device, skipping test.\n");
6901 DestroyWindow(window);
6902 return;
6905 hr = IDirect3DDevice2_GetDirect3D(device, &d3d);
6906 ok(SUCCEEDED(hr), "Failed to get D3D interface, hr %#x.\n", hr);
6908 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
6909 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
6911 viewport = create_viewport(device, 0, 0, 640, 480);
6912 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
6913 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
6915 material = create_diffuse_material(device, 1.0f, 1.0f, 1.0f, 1.0f);
6916 viewport_set_background(device, viewport, material);
6918 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6919 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
6921 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &mat);
6922 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
6923 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &mat);
6924 ok(SUCCEEDED(hr), "Failed to set view transform, hr %#x.\n", hr);
6925 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &mat);
6926 ok(SUCCEEDED(hr), "Failed to set projection transform, hr %#x.\n", hr);
6927 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
6928 ok(SUCCEEDED(hr), "Failed to disable clipping, hr %#x.\n", hr);
6929 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
6930 ok(SUCCEEDED(hr), "Failed to disable zbuffer, hr %#x.\n", hr);
6931 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
6932 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
6933 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
6934 ok(SUCCEEDED(hr), "Failed to disable culling, hr %#x.\n", hr);
6936 hr = IDirect3DDevice2_BeginScene(device);
6937 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
6939 /* There is no D3DRENDERSTATE_LIGHTING on ddraw < 7. */
6940 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
6941 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
6942 hr = IDirect3DDevice2_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DVT_LVERTEX, unlitquad,
6943 4, indices, 6, 0);
6944 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
6946 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
6947 ok(SUCCEEDED(hr), "Failed to enable lighting, hr %#x.\n", hr);
6948 hr = IDirect3DDevice2_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DVT_LVERTEX, litquad,
6949 4, indices, 6, 0);
6950 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
6952 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
6953 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
6954 hr = IDirect3DDevice2_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DVT_VERTEX, unlitnquad,
6955 4, indices, 6, 0);
6956 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
6958 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
6959 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
6960 hr = IDirect3DDevice2_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DVT_VERTEX, litnquad,
6961 4, indices, 6, 0);
6962 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
6964 hr = IDirect3DDevice2_EndScene(device);
6965 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
6967 color = get_surface_color(rt, 160, 360);
6968 ok(color == 0x00ff0000, "Unlit quad without normals has color 0x%08x.\n", color);
6969 color = get_surface_color(rt, 160, 120);
6970 ok(color == 0x0000ff00, "Lit quad without normals has color 0x%08x.\n", color);
6971 color = get_surface_color(rt, 480, 360);
6972 ok(color == 0x00ffffff, "Unlit quad with normals has color 0x%08x.\n", color);
6973 color = get_surface_color(rt, 480, 120);
6974 ok(color == 0x00ffffff, "Lit quad with normals has color 0x%08x.\n", color);
6976 hr = IDirect3DMaterial2_GetHandle(material, device, &mat_handle);
6977 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
6978 hr = IDirect3DDevice2_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
6979 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
6981 hr = IDirect3D2_CreateLight(d3d, &light, NULL);
6982 ok(SUCCEEDED(hr), "Failed to create a light object, hr %#x.\n", hr);
6983 memset(&light_desc, 0, sizeof(light_desc));
6984 light_desc.dwSize = sizeof(light_desc);
6985 light_desc.dltType = D3DLIGHT_DIRECTIONAL;
6986 U1(light_desc.dcvColor).r = 0.0f;
6987 U2(light_desc.dcvColor).g = 0.0f;
6988 U3(light_desc.dcvColor).b = 1.0f;
6989 U4(light_desc.dcvColor).a = 1.0f;
6990 U3(light_desc.dvDirection).z = 1.0f;
6991 hr = IDirect3DLight_SetLight(light, (D3DLIGHT *)&light_desc);
6992 ok(SUCCEEDED(hr), "Failed to set light, hr %#x.\n", hr);
6993 hr = IDirect3DViewport2_AddLight(viewport, light);
6994 ok(SUCCEEDED(hr), "Failed to add a light to the viewport, hr %#x.\n", hr);
6996 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6997 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
6999 hr = IDirect3DDevice2_BeginScene(device);
7000 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
7002 hr = IDirect3DDevice2_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DVT_VERTEX, nquad,
7003 4, indices, 6, 0);
7004 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
7006 hr = IDirect3DDevice2_EndScene(device);
7007 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
7009 color = get_surface_color(rt, 320, 240);
7010 ok(color == 0x00000000, "Lit quad with no light has color 0x%08x.\n", color);
7012 light_desc.dwFlags = D3DLIGHT_ACTIVE;
7013 hr = IDirect3DLight_SetLight(light, (D3DLIGHT *)&light_desc);
7014 ok(SUCCEEDED(hr), "Failed to set light, hr %#x.\n", hr);
7016 for (i = 0; i < ARRAY_SIZE(tests); ++i)
7018 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_WORLD, tests[i].world_matrix);
7019 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
7021 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
7022 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
7024 hr = IDirect3DDevice2_BeginScene(device);
7025 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
7027 hr = IDirect3DDevice2_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DVT_VERTEX,
7028 tests[i].quad, 4, indices, 6, 0);
7029 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
7031 hr = IDirect3DDevice2_EndScene(device);
7032 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
7034 color = get_surface_color(rt, 320, 240);
7035 ok(color == tests[i].expected, "%s has color 0x%08x.\n", tests[i].message, color);
7038 hr = IDirect3DViewport2_DeleteLight(viewport, light);
7039 ok(SUCCEEDED(hr), "Failed to remove a light from the viewport, hr %#x.\n", hr);
7040 IDirect3DLight_Release(light);
7041 destroy_material(material);
7042 destroy_viewport(device, viewport);
7043 IDirectDrawSurface2_Release(rt);
7044 refcount = IDirect3DDevice2_Release(device);
7045 ok(!refcount, "Device has %u references left.\n", refcount);
7046 IDirect3D2_Release(d3d);
7047 refcount = IDirectDraw2_Release(ddraw);
7048 ok(!refcount, "Ddraw object has %u references left.\n", refcount);
7049 DestroyWindow(window);
7052 static void test_specular_lighting(void)
7054 static const unsigned int vertices_side = 5;
7055 const unsigned int indices_count = (vertices_side - 1) * (vertices_side - 1) * 2 * 3;
7056 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
7057 static D3DMATRIX mat =
7059 1.0f, 0.0f, 0.0f, 0.0f,
7060 0.0f, 1.0f, 0.0f, 0.0f,
7061 0.0f, 0.0f, 1.0f, 0.0f,
7062 0.0f, 0.0f, 0.0f, 1.0f,
7064 static D3DLIGHT2 directional =
7066 sizeof(D3DLIGHT2),
7067 D3DLIGHT_DIRECTIONAL,
7068 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
7069 {{0.0f}, {0.0f}, {0.0f}},
7070 {{0.0f}, {0.0f}, {1.0f}},
7072 point =
7074 sizeof(D3DLIGHT2),
7075 D3DLIGHT_POINT,
7076 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
7077 {{0.0f}, {0.0f}, {0.0f}},
7078 {{0.0f}, {0.0f}, {0.0f}},
7079 100.0f,
7080 0.0f,
7081 0.0f, 0.0f, 1.0f,
7083 spot =
7085 sizeof(D3DLIGHT2),
7086 D3DLIGHT_SPOT,
7087 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
7088 {{0.0f}, {0.0f}, {0.0f}},
7089 {{0.0f}, {0.0f}, {1.0f}},
7090 100.0f,
7091 1.0f,
7092 0.0f, 0.0f, 1.0f,
7093 M_PI / 12.0f, M_PI / 3.0f
7095 parallelpoint =
7097 sizeof(D3DLIGHT2),
7098 D3DLIGHT_PARALLELPOINT,
7099 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
7100 {{0.5f}, {0.0f}, {-1.0f}},
7101 {{0.0f}, {0.0f}, {0.0f}},
7103 point_side =
7105 sizeof(D3DLIGHT2),
7106 D3DLIGHT_POINT,
7107 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
7108 {{-1.1f}, {0.0f}, {1.1f}},
7109 {{0.0f}, {0.0f}, {0.0f}},
7110 100.0f,
7111 0.0f,
7112 1.0f, 0.0f, 0.0f,
7114 point_far =
7116 sizeof(D3DLIGHT2),
7117 D3DLIGHT_POINT,
7118 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
7119 {{0.0f}, {0.0f}, {0.1f}},
7120 {{0.0f}, {0.0f}, {0.0f}},
7121 1.0f,
7122 0.0f,
7123 1.0f, 0.0f, 0.0f,
7125 static const struct expected_color
7127 unsigned int x, y;
7128 D3DCOLOR color;
7130 expected_directional_local[] =
7132 {160, 120, 0x003c3c3c},
7133 {320, 120, 0x00717171},
7134 {480, 120, 0x003c3c3c},
7135 {160, 240, 0x00717171},
7136 {320, 240, 0x00ffffff},
7137 {480, 240, 0x00717171},
7138 {160, 360, 0x003c3c3c},
7139 {320, 360, 0x00717171},
7140 {480, 360, 0x003c3c3c},
7142 expected_point_local[] =
7144 {160, 120, 0x00000000},
7145 {320, 120, 0x00090909},
7146 {480, 120, 0x00000000},
7147 {160, 240, 0x00090909},
7148 {320, 240, 0x00fafafa},
7149 {480, 240, 0x00090909},
7150 {160, 360, 0x00000000},
7151 {320, 360, 0x00090909},
7152 {480, 360, 0x00000000},
7154 expected_spot_local[] =
7156 {160, 120, 0x00000000},
7157 {320, 120, 0x00020202},
7158 {480, 120, 0x00000000},
7159 {160, 240, 0x00020202},
7160 {320, 240, 0x00fafafa},
7161 {480, 240, 0x00020202},
7162 {160, 360, 0x00000000},
7163 {320, 360, 0x00020202},
7164 {480, 360, 0x00000000},
7166 expected_parallelpoint[] =
7168 {160, 120, 0x00050505},
7169 {320, 120, 0x002c2c2c},
7170 {480, 120, 0x006e6e6e},
7171 {160, 240, 0x00090909},
7172 {320, 240, 0x00717171},
7173 {480, 240, 0x00ffffff},
7174 {160, 360, 0x00050505},
7175 {320, 360, 0x002c2c2c},
7176 {480, 360, 0x006e6e6e},
7178 expected_point_side[] =
7180 {160, 120, 0x00000000},
7181 {320, 120, 0x00000000},
7182 {480, 120, 0x00000000},
7183 {160, 240, 0x00000000},
7184 {320, 240, 0x00000000},
7185 {480, 240, 0x00000000},
7186 {160, 360, 0x00000000},
7187 {320, 360, 0x00000000},
7188 {480, 360, 0x00000000},
7190 expected_point_far[] =
7192 {160, 120, 0x00000000},
7193 {320, 120, 0x00000000},
7194 {480, 120, 0x00000000},
7195 {160, 240, 0x00000000},
7196 {320, 240, 0x00ffffff},
7197 {480, 240, 0x00000000},
7198 {160, 360, 0x00000000},
7199 {320, 360, 0x00000000},
7200 {480, 360, 0x00000000},
7202 static const struct
7204 D3DLIGHT2 *light;
7205 float specular_power;
7206 const struct expected_color *expected;
7207 unsigned int expected_count;
7209 tests[] =
7211 {&directional, 30.0f, expected_directional_local, ARRAY_SIZE(expected_directional_local)},
7212 {&point, 30.0f, expected_point_local, ARRAY_SIZE(expected_point_local)},
7213 {&spot, 30.0f, expected_spot_local, ARRAY_SIZE(expected_spot_local)},
7214 {&parallelpoint, 30.0f, expected_parallelpoint, ARRAY_SIZE(expected_parallelpoint)},
7215 {&point_side, 0.0f, expected_point_side, ARRAY_SIZE(expected_point_side)},
7216 {&point_far, 1.0f, expected_point_far, ARRAY_SIZE(expected_point_far)},
7218 IDirect3D2 *d3d;
7219 IDirect3DDevice2 *device;
7220 IDirectDraw2 *ddraw;
7221 IDirectDrawSurface *rt;
7222 IDirect3DViewport2 *viewport;
7223 IDirect3DMaterial2 *material, *background_material;
7224 IDirect3DLight *light;
7225 D3DMATERIALHANDLE mat_handle;
7226 D3DCOLOR color;
7227 ULONG refcount;
7228 HWND window;
7229 HRESULT hr;
7230 unsigned int i, j, x, y;
7231 D3DVERTEX *quad;
7232 WORD *indices;
7234 window = create_window();
7235 ddraw = create_ddraw();
7236 ok(!!ddraw, "Failed to create a ddraw object.\n");
7237 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
7239 skip("Failed to create a 3D device, skipping test.\n");
7240 DestroyWindow(window);
7241 return;
7244 quad = HeapAlloc(GetProcessHeap(), 0, vertices_side * vertices_side * sizeof(*quad));
7245 indices = HeapAlloc(GetProcessHeap(), 0, indices_count * sizeof(*indices));
7246 for (i = 0, y = 0; y < vertices_side; ++y)
7248 for (x = 0; x < vertices_side; ++x)
7250 U1(quad[i]).x = x * 2.0f / (vertices_side - 1) - 1.0f;
7251 U2(quad[i]).y = y * 2.0f / (vertices_side - 1) - 1.0f;
7252 U3(quad[i]).z = 1.0f;
7253 U4(quad[i]).nx = 0.0f;
7254 U5(quad[i]).ny = 0.0f;
7255 U6(quad[i]).nz = -1.0f;
7256 U7(quad[i]).tu = 0.0f;
7257 U8(quad[i++]).tv = 0.0f;
7260 for (i = 0, y = 0; y < (vertices_side - 1); ++y)
7262 for (x = 0; x < (vertices_side - 1); ++x)
7264 indices[i++] = y * vertices_side + x + 1;
7265 indices[i++] = y * vertices_side + x;
7266 indices[i++] = (y + 1) * vertices_side + x;
7267 indices[i++] = y * vertices_side + x + 1;
7268 indices[i++] = (y + 1) * vertices_side + x;
7269 indices[i++] = (y + 1) * vertices_side + x + 1;
7273 hr = IDirect3DDevice2_GetDirect3D(device, &d3d);
7274 ok(SUCCEEDED(hr), "Failed to get D3D interface, hr %#x.\n", hr);
7276 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
7277 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
7279 viewport = create_viewport(device, 0, 0, 640, 480);
7280 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
7281 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
7283 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &mat);
7284 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
7285 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &mat);
7286 ok(SUCCEEDED(hr), "Failed to set view transform, hr %#x.\n", hr);
7287 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &mat);
7288 ok(SUCCEEDED(hr), "Failed to set projection transform, hr %#x.\n", hr);
7289 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
7290 ok(SUCCEEDED(hr), "Failed to disable clipping, hr %#x.\n", hr);
7291 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
7292 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
7293 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
7294 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
7296 background_material = create_diffuse_material(device, 1.0f, 1.0f, 1.0f, 1.0f);
7297 viewport_set_background(device, viewport, background_material);
7299 hr = IDirect3D2_CreateLight(d3d, &light, NULL);
7300 ok(SUCCEEDED(hr), "Failed to create a light object, hr %#x.\n", hr);
7301 hr = IDirect3DViewport2_AddLight(viewport, light);
7302 ok(SUCCEEDED(hr), "Failed to add a light to the viewport, hr %#x.\n", hr);
7304 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_SPECULARENABLE, TRUE);
7305 ok(SUCCEEDED(hr), "Failed to enable specular lighting, hr %#x.\n", hr);
7307 for (i = 0; i < ARRAY_SIZE(tests); ++i)
7309 tests[i].light->dwFlags = D3DLIGHT_ACTIVE;
7310 hr = IDirect3DLight_SetLight(light, (D3DLIGHT *)tests[i].light);
7311 ok(SUCCEEDED(hr), "Failed to set light, hr %#x.\n", hr);
7313 material = create_specular_material(device, 1.0f, 1.0f, 1.0f, 1.0f, tests[i].specular_power);
7314 hr = IDirect3DMaterial2_GetHandle(material, device, &mat_handle);
7315 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
7316 hr = IDirect3DDevice2_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
7317 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
7319 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
7320 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
7322 hr = IDirect3DDevice2_BeginScene(device);
7323 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
7325 hr = IDirect3DDevice2_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, D3DVT_VERTEX,
7326 quad, vertices_side * vertices_side, indices, indices_count, 0);
7327 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
7329 hr = IDirect3DDevice2_EndScene(device);
7330 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
7332 for (j = 0; j < tests[i].expected_count; ++j)
7334 color = get_surface_color(rt, tests[i].expected[j].x, tests[i].expected[j].y);
7335 ok(compare_color(color, tests[i].expected[j].color, 1),
7336 "Expected color 0x%08x at location (%u, %u), got 0x%08x, case %u.\n",
7337 tests[i].expected[j].color, tests[i].expected[j].x,
7338 tests[i].expected[j].y, color, i);
7341 destroy_material(material);
7344 hr = IDirect3DViewport2_DeleteLight(viewport, light);
7345 ok(SUCCEEDED(hr), "Failed to remove a light from the viewport, hr %#x.\n", hr);
7346 IDirect3DLight_Release(light);
7347 destroy_material(background_material);
7348 destroy_viewport(device, viewport);
7349 IDirectDrawSurface2_Release(rt);
7350 refcount = IDirect3DDevice2_Release(device);
7351 ok(!refcount, "Device has %u references left.\n", refcount);
7352 IDirect3D2_Release(d3d);
7353 refcount = IDirectDraw2_Release(ddraw);
7354 ok(!refcount, "Ddraw object has %u references left.\n", refcount);
7355 DestroyWindow(window);
7356 HeapFree(GetProcessHeap(), 0, indices);
7357 HeapFree(GetProcessHeap(), 0, quad);
7360 static void test_palette_gdi(void)
7362 IDirectDrawSurface *surface, *primary;
7363 DDSURFACEDESC surface_desc;
7364 IDirectDraw2 *ddraw;
7365 IDirectDrawPalette *palette, *palette2;
7366 ULONG refcount;
7367 HWND window;
7368 HRESULT hr;
7369 PALETTEENTRY palette_entries[256];
7370 UINT i;
7371 HDC dc;
7372 DDBLTFX fx;
7373 RECT r;
7374 COLORREF color;
7375 /* On the Windows 8 testbot palette index 0 of the onscreen palette is forced to
7376 * r = 0, g = 0, b = 0. Do not attempt to set it to something else as this is
7377 * not the point of this test. */
7378 static const RGBQUAD expected1[] =
7380 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
7381 {0x03, 0x00, 0x00, 0x00}, {0x15, 0x14, 0x13, 0x00},
7383 static const RGBQUAD expected2[] =
7385 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
7386 {0x03, 0x00, 0x00, 0x00}, {0x25, 0x24, 0x23, 0x00},
7388 static const RGBQUAD expected3[] =
7390 {0x00, 0x00, 0x00, 0x00}, {0x40, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x40, 0x00},
7391 {0x00, 0x40, 0x00, 0x00}, {0x56, 0x34, 0x12, 0x00},
7393 HPALETTE ddraw_palette_handle;
7394 /* Similar to index 0, index 255 is r = 0xff, g = 0xff, b = 0xff on the Win8 VMs. */
7395 RGBQUAD rgbquad[255];
7396 static const RGBQUAD rgb_zero = {0, 0, 0, 0};
7398 window = create_window();
7399 ddraw = create_ddraw();
7400 ok(!!ddraw, "Failed to create a ddraw object.\n");
7401 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7402 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7404 memset(&surface_desc, 0, sizeof(surface_desc));
7405 surface_desc.dwSize = sizeof(surface_desc);
7406 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7407 surface_desc.dwWidth = 16;
7408 surface_desc.dwHeight = 16;
7409 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7410 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
7411 surface_desc.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
7412 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 8;
7413 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7414 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7416 /* Avoid colors from the Windows default palette. */
7417 memset(palette_entries, 0, sizeof(palette_entries));
7418 palette_entries[1].peRed = 0x01;
7419 palette_entries[2].peGreen = 0x02;
7420 palette_entries[3].peBlue = 0x03;
7421 palette_entries[4].peRed = 0x13;
7422 palette_entries[4].peGreen = 0x14;
7423 palette_entries[4].peBlue = 0x15;
7424 hr = IDirectDraw2_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7425 palette_entries, &palette, NULL);
7426 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7428 /* If there is no palette assigned and the display mode is not 8 bpp, some
7429 * drivers refuse to create a DC while others allow it. If a DC is created,
7430 * the DIB color table is uninitialized and contains random colors. No error
7431 * is generated when trying to read pixels and random garbage is returned.
7433 * The most likely explanation is that if the driver creates a DC, it (or
7434 * the higher-level runtime) uses GetSystemPaletteEntries to find the
7435 * palette, but GetSystemPaletteEntries fails when bpp > 8 and the palette
7436 * contains uninitialized garbage. See comments below for the P8 case. */
7438 hr = IDirectDrawSurface_SetPalette(surface, palette);
7439 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7440 hr = IDirectDrawSurface_GetDC(surface, &dc);
7441 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7442 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
7443 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE),
7444 "Got unexpected palette %p, expected %p.\n",
7445 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
7447 i = GetDIBColorTable(dc, 0, ARRAY_SIZE(rgbquad), rgbquad);
7448 ok(i == ARRAY_SIZE(rgbquad), "Expected count 255, got %u.\n", i);
7449 for (i = 0; i < ARRAY_SIZE(expected1); i++)
7451 ok(!memcmp(&rgbquad[i], &expected1[i], sizeof(rgbquad[i])),
7452 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7453 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7454 expected1[i].rgbRed, expected1[i].rgbGreen, expected1[i].rgbBlue);
7456 for (; i < ARRAY_SIZE(rgbquad); i++)
7458 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7459 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7460 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7463 /* Update the palette while the DC is in use. This does not modify the DC. */
7464 palette_entries[4].peRed = 0x23;
7465 palette_entries[4].peGreen = 0x24;
7466 palette_entries[4].peBlue = 0x25;
7467 hr = IDirectDrawPalette_SetEntries(palette, 0, 4, 1, &palette_entries[4]);
7468 ok(SUCCEEDED(hr), "Failed to set palette entries, hr %#x.\n", hr);
7470 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
7471 ok(i == 1, "Expected count 1, got %u.\n", i);
7472 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
7473 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7474 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
7475 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
7477 /* Neither does re-setting the palette. */
7478 hr = IDirectDrawSurface_SetPalette(surface, NULL);
7479 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7480 hr = IDirectDrawSurface_SetPalette(surface, palette);
7481 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7483 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
7484 ok(i == 1, "Expected count 1, got %u.\n", i);
7485 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
7486 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7487 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
7488 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
7490 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
7491 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7493 /* Refresh the DC. This updates the palette. */
7494 hr = IDirectDrawSurface_GetDC(surface, &dc);
7495 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7496 i = GetDIBColorTable(dc, 0, ARRAY_SIZE(rgbquad), rgbquad);
7497 ok(i == ARRAY_SIZE(rgbquad), "Expected count 255, got %u.\n", i);
7498 for (i = 0; i < ARRAY_SIZE(expected2); i++)
7500 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
7501 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7502 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7503 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
7505 for (; i < ARRAY_SIZE(rgbquad); i++)
7507 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7508 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7509 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7511 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
7512 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7514 refcount = IDirectDrawSurface_Release(surface);
7515 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7517 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
7518 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7519 if (FAILED(IDirectDraw2_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
7521 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
7522 IDirectDrawPalette_Release(palette);
7523 IDirectDraw2_Release(ddraw);
7524 DestroyWindow(window);
7525 return;
7527 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
7529 memset(&surface_desc, 0, sizeof(surface_desc));
7530 surface_desc.dwSize = sizeof(surface_desc);
7531 surface_desc.dwFlags = DDSD_CAPS;
7532 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
7533 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &primary, NULL);
7534 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7536 memset(&fx, 0, sizeof(fx));
7537 fx.dwSize = sizeof(fx);
7538 U5(fx).dwFillColor = 3;
7539 SetRect(&r, 0, 0, 319, 479);
7540 hr = IDirectDrawSurface_Blt(primary, &r, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7541 ok(SUCCEEDED(hr), "Failed to clear surface, hr %#x.\n", hr);
7542 SetRect(&r, 320, 0, 639, 479);
7543 U5(fx).dwFillColor = 4;
7544 hr = IDirectDrawSurface_Blt(primary, &r, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7545 ok(SUCCEEDED(hr), "Failed to clear surface, hr %#x.\n", hr);
7547 hr = IDirectDrawSurface_SetPalette(primary, palette);
7548 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7549 hr = IDirectDrawSurface_GetDC(primary, &dc);
7550 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7552 color = GetPixel(dc, 160, 240);
7553 ok(color == 0x00030000, "Clear index 3: Got unexpected color 0x%08x.\n", color);
7554 color = GetPixel(dc, 480, 240);
7555 ok(color == 0x00252423, "Clear index 4: Got unexpected color 0x%08x.\n", color);
7557 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
7558 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE),
7559 "Got unexpected palette %p, expected %p.\n",
7560 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
7561 SelectPalette(dc, ddraw_palette_handle, FALSE);
7563 /* The primary uses the system palette. In exclusive mode, the system palette matches
7564 * the ddraw palette attached to the primary, so the result is what you would expect
7565 * from a regular surface. Tests for the interaction between the ddraw palette and
7566 * the system palette are not included pending an application that depends on this.
7567 * The relation between those causes problems on Windows Vista and newer for games
7568 * like Age of Empires or StarCraft. Don't emulate it without a real need. */
7569 i = GetDIBColorTable(dc, 0, ARRAY_SIZE(rgbquad), rgbquad);
7570 ok(i == ARRAY_SIZE(rgbquad), "Expected count 255, got %u.\n", i);
7571 for (i = 0; i < ARRAY_SIZE(expected2); i++)
7573 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
7574 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7575 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7576 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
7578 for (; i < ARRAY_SIZE(rgbquad); i++)
7580 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7581 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7582 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7584 hr = IDirectDrawSurface_ReleaseDC(primary, dc);
7585 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7587 memset(&surface_desc, 0, sizeof(surface_desc));
7588 surface_desc.dwSize = sizeof(surface_desc);
7589 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
7590 surface_desc.dwWidth = 16;
7591 surface_desc.dwHeight = 16;
7592 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7593 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7594 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7596 /* Here the offscreen surface appears to use the primary's palette,
7597 * but in all likelihood it is actually the system palette. */
7598 hr = IDirectDrawSurface_GetDC(surface, &dc);
7599 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7600 i = GetDIBColorTable(dc, 0, ARRAY_SIZE(rgbquad), rgbquad);
7601 ok(i == ARRAY_SIZE(rgbquad), "Expected count 255, got %u.\n", i);
7602 for (i = 0; i < ARRAY_SIZE(expected2); i++)
7604 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
7605 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7606 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7607 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
7609 for (; i < ARRAY_SIZE(rgbquad); i++)
7611 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7612 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7613 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7615 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
7616 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7618 /* On real hardware a change to the primary surface's palette applies immediately,
7619 * even on device contexts from offscreen surfaces that do not have their own
7620 * palette. On the testbot VMs this is not the case. Don't test this until we
7621 * know of an application that depends on this. */
7623 memset(palette_entries, 0, sizeof(palette_entries));
7624 palette_entries[1].peBlue = 0x40;
7625 palette_entries[2].peRed = 0x40;
7626 palette_entries[3].peGreen = 0x40;
7627 palette_entries[4].peRed = 0x12;
7628 palette_entries[4].peGreen = 0x34;
7629 palette_entries[4].peBlue = 0x56;
7630 hr = IDirectDraw2_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7631 palette_entries, &palette2, NULL);
7632 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7633 hr = IDirectDrawSurface_SetPalette(surface, palette2);
7634 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7636 /* A palette assigned to the offscreen surface overrides the primary / system
7637 * palette. */
7638 hr = IDirectDrawSurface_GetDC(surface, &dc);
7639 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7640 i = GetDIBColorTable(dc, 0, ARRAY_SIZE(rgbquad), rgbquad);
7641 ok(i == ARRAY_SIZE(rgbquad), "Expected count 255, got %u.\n", i);
7642 for (i = 0; i < ARRAY_SIZE(expected3); i++)
7644 ok(!memcmp(&rgbquad[i], &expected3[i], sizeof(rgbquad[i])),
7645 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7646 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7647 expected3[i].rgbRed, expected3[i].rgbGreen, expected3[i].rgbBlue);
7649 for (; i < ARRAY_SIZE(rgbquad); i++)
7651 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7652 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7653 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7655 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
7656 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7658 refcount = IDirectDrawSurface_Release(surface);
7659 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7661 /* The Windows 8 testbot keeps extra references to the primary and
7662 * backbuffer while in 8 bpp mode. */
7663 hr = IDirectDraw2_RestoreDisplayMode(ddraw);
7664 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
7666 refcount = IDirectDrawSurface_Release(primary);
7667 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7668 refcount = IDirectDrawPalette_Release(palette2);
7669 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7670 refcount = IDirectDrawPalette_Release(palette);
7671 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7672 refcount = IDirectDraw2_Release(ddraw);
7673 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7674 DestroyWindow(window);
7677 static void test_palette_alpha(void)
7679 IDirectDrawSurface *surface1;
7680 IDirectDrawSurface2 *surface;
7681 DDSURFACEDESC surface_desc;
7682 IDirectDraw2 *ddraw;
7683 IDirectDrawPalette *palette;
7684 ULONG refcount;
7685 HWND window;
7686 HRESULT hr;
7687 PALETTEENTRY palette_entries[256];
7688 unsigned int i;
7689 static const struct
7691 DWORD caps, flags;
7692 BOOL attach_allowed;
7693 const char *name;
7695 test_data[] =
7697 {DDSCAPS_OFFSCREENPLAIN, DDSD_WIDTH | DDSD_HEIGHT, FALSE, "offscreenplain"},
7698 {DDSCAPS_TEXTURE, DDSD_WIDTH | DDSD_HEIGHT, TRUE, "texture"},
7699 {DDSCAPS_PRIMARYSURFACE, 0, FALSE, "primary"}
7702 window = create_window();
7703 ddraw = create_ddraw();
7704 ok(!!ddraw, "Failed to create a ddraw object.\n");
7705 if (FAILED(IDirectDraw2_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
7707 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
7708 IDirectDraw2_Release(ddraw);
7709 DestroyWindow(window);
7710 return;
7712 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
7713 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7715 memset(palette_entries, 0, sizeof(palette_entries));
7716 palette_entries[1].peFlags = 0x42;
7717 palette_entries[2].peFlags = 0xff;
7718 palette_entries[3].peFlags = 0x80;
7719 hr = IDirectDraw2_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
7720 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7722 memset(palette_entries, 0x66, sizeof(palette_entries));
7723 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
7724 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
7725 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7726 palette_entries[0].peFlags);
7727 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7728 palette_entries[1].peFlags);
7729 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
7730 palette_entries[2].peFlags);
7731 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
7732 palette_entries[3].peFlags);
7734 IDirectDrawPalette_Release(palette);
7736 memset(palette_entries, 0, sizeof(palette_entries));
7737 palette_entries[1].peFlags = 0x42;
7738 palette_entries[1].peRed = 0xff;
7739 palette_entries[2].peFlags = 0xff;
7740 palette_entries[3].peFlags = 0x80;
7741 hr = IDirectDraw2_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT | DDPCAPS_ALPHA,
7742 palette_entries, &palette, NULL);
7743 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7745 memset(palette_entries, 0x66, sizeof(palette_entries));
7746 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
7747 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
7748 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7749 palette_entries[0].peFlags);
7750 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7751 palette_entries[1].peFlags);
7752 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
7753 palette_entries[2].peFlags);
7754 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
7755 palette_entries[3].peFlags);
7757 for (i = 0; i < ARRAY_SIZE(test_data); i++)
7759 memset(&surface_desc, 0, sizeof(surface_desc));
7760 surface_desc.dwSize = sizeof(surface_desc);
7761 surface_desc.dwFlags = DDSD_CAPS | test_data[i].flags;
7762 surface_desc.dwWidth = 128;
7763 surface_desc.dwHeight = 128;
7764 surface_desc.ddsCaps.dwCaps = test_data[i].caps;
7765 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
7766 ok(SUCCEEDED(hr), "Failed to create %s surface, hr %#x.\n", test_data[i].name, hr);
7767 hr = IDirectDrawSurface_QueryInterface(surface1, &IID_IDirectDrawSurface2, (void **)&surface);
7768 ok(SUCCEEDED(hr), "Failed to get IDirectDrawSurface2 interface, hr %#x.\n", hr);
7769 IDirectDrawSurface_Release(surface1);
7771 hr = IDirectDrawSurface2_SetPalette(surface, palette);
7772 if (test_data[i].attach_allowed)
7773 ok(SUCCEEDED(hr), "Failed to attach palette to %s surface, hr %#x.\n", test_data[i].name, hr);
7774 else
7775 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x, %s surface.\n", hr, test_data[i].name);
7777 if (SUCCEEDED(hr))
7779 HDC dc;
7780 RGBQUAD rgbquad;
7781 UINT retval;
7783 hr = IDirectDrawSurface2_GetDC(surface, &dc);
7784 ok(SUCCEEDED(hr) || broken(hr == DDERR_CANTCREATEDC) /* Win2k testbot */,
7785 "Failed to get DC, hr %#x, %s surface.\n", hr, test_data[i].name);
7786 if (SUCCEEDED(hr))
7788 retval = GetDIBColorTable(dc, 1, 1, &rgbquad);
7789 ok(retval == 1, "GetDIBColorTable returned unexpected result %u.\n", retval);
7790 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x, %s surface.\n",
7791 rgbquad.rgbRed, test_data[i].name);
7792 ok(rgbquad.rgbGreen == 0, "Expected rgbGreen = 0, got %#x, %s surface.\n",
7793 rgbquad.rgbGreen, test_data[i].name);
7794 ok(rgbquad.rgbBlue == 0, "Expected rgbBlue = 0, got %#x, %s surface.\n",
7795 rgbquad.rgbBlue, test_data[i].name);
7796 ok(rgbquad.rgbReserved == 0, "Expected rgbReserved = 0, got %u, %s surface.\n",
7797 rgbquad.rgbReserved, test_data[i].name);
7798 hr = IDirectDrawSurface2_ReleaseDC(surface, dc);
7799 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7802 IDirectDrawSurface2_Release(surface);
7805 /* Test INVALIDSURFACETYPE vs INVALIDPIXELFORMAT. */
7806 memset(&surface_desc, 0, sizeof(surface_desc));
7807 surface_desc.dwSize = sizeof(surface_desc);
7808 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7809 surface_desc.dwWidth = 128;
7810 surface_desc.dwHeight = 128;
7811 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7812 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
7813 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
7814 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
7815 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
7816 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
7817 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
7818 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
7819 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7820 hr = IDirectDrawSurface_QueryInterface(surface1, &IID_IDirectDrawSurface2, (void **)&surface);
7821 ok(SUCCEEDED(hr), "Failed to get IDirectDrawSurface2 interface, hr %#x.\n", hr);
7822 IDirectDrawSurface_Release(surface1);
7824 hr = IDirectDrawSurface2_SetPalette(surface, palette);
7825 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x.\n", hr);
7826 IDirectDrawSurface2_Release(surface);
7828 /* The Windows 8 testbot keeps extra references to the primary
7829 * while in 8 bpp mode. */
7830 hr = IDirectDraw2_RestoreDisplayMode(ddraw);
7831 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
7833 refcount = IDirectDrawPalette_Release(palette);
7834 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7835 refcount = IDirectDraw2_Release(ddraw);
7836 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7837 DestroyWindow(window);
7840 static void test_lost_device(void)
7842 IDirectDrawSurface *surface;
7843 DDSURFACEDESC surface_desc;
7844 HWND window1, window2;
7845 IDirectDraw2 *ddraw;
7846 ULONG refcount;
7847 HRESULT hr;
7848 BOOL ret;
7850 window1 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7851 0, 0, 640, 480, 0, 0, 0, 0);
7852 window2 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7853 0, 0, 640, 480, 0, 0, 0, 0);
7854 ddraw = create_ddraw();
7855 ok(!!ddraw, "Failed to create a ddraw object.\n");
7856 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
7857 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7859 memset(&surface_desc, 0, sizeof(surface_desc));
7860 surface_desc.dwSize = sizeof(surface_desc);
7861 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
7862 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
7863 surface_desc.dwBackBufferCount = 1;
7864 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7865 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7867 hr = IDirectDrawSurface_IsLost(surface);
7868 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7869 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
7870 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7872 ret = SetForegroundWindow(GetDesktopWindow());
7873 ok(ret, "Failed to set foreground window.\n");
7874 hr = IDirectDrawSurface_IsLost(surface);
7875 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7876 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
7877 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7879 ret = SetForegroundWindow(window1);
7880 ok(ret, "Failed to set foreground window.\n");
7881 hr = IDirectDrawSurface_IsLost(surface);
7882 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7883 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
7884 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7886 hr = restore_surfaces(ddraw);
7887 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7888 hr = IDirectDrawSurface_IsLost(surface);
7889 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7890 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
7891 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7893 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
7894 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7895 hr = IDirectDrawSurface_IsLost(surface);
7896 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7897 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
7898 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7900 /* Trying to restore the primary will crash, probably because flippable
7901 * surfaces can't exist in DDSCL_NORMAL. */
7902 IDirectDrawSurface_Release(surface);
7903 memset(&surface_desc, 0, sizeof(surface_desc));
7904 surface_desc.dwSize = sizeof(surface_desc);
7905 surface_desc.dwFlags = DDSD_CAPS;
7906 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
7907 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7908 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7910 hr = IDirectDrawSurface_IsLost(surface);
7911 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7913 ret = SetForegroundWindow(GetDesktopWindow());
7914 ok(ret, "Failed to set foreground window.\n");
7915 hr = IDirectDrawSurface_IsLost(surface);
7916 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7918 ret = SetForegroundWindow(window1);
7919 ok(ret, "Failed to set foreground window.\n");
7920 hr = IDirectDrawSurface_IsLost(surface);
7921 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7923 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
7924 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7925 hr = IDirectDrawSurface_IsLost(surface);
7926 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7928 hr = restore_surfaces(ddraw);
7929 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7930 hr = IDirectDrawSurface_IsLost(surface);
7931 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7933 IDirectDrawSurface_Release(surface);
7934 memset(&surface_desc, 0, sizeof(surface_desc));
7935 surface_desc.dwSize = sizeof(surface_desc);
7936 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
7937 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
7938 surface_desc.dwBackBufferCount = 1;
7939 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7940 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7942 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
7943 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7944 hr = IDirectDrawSurface_IsLost(surface);
7945 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7946 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
7947 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7949 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL | DDSCL_FULLSCREEN);
7950 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7951 hr = IDirectDrawSurface_IsLost(surface);
7952 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7953 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
7954 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
7956 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
7957 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7958 hr = IDirectDrawSurface_IsLost(surface);
7959 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7960 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
7961 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
7963 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
7964 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7965 hr = IDirectDrawSurface_IsLost(surface);
7966 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7967 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
7968 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
7970 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL | DDSCL_FULLSCREEN);
7971 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7972 hr = IDirectDrawSurface_IsLost(surface);
7973 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7974 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
7975 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
7977 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window2, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
7978 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7979 hr = IDirectDrawSurface_IsLost(surface);
7980 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7981 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
7982 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7984 IDirectDrawSurface_Release(surface);
7985 refcount = IDirectDraw2_Release(ddraw);
7986 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7987 DestroyWindow(window2);
7988 DestroyWindow(window1);
7991 static void test_surface_desc_lock(void)
7993 IDirectDrawSurface *surface;
7994 DDSURFACEDESC surface_desc;
7995 IDirectDraw2 *ddraw;
7996 ULONG refcount;
7997 HWND window;
7998 HRESULT hr;
8000 window = create_window();
8001 ddraw = create_ddraw();
8002 ok(!!ddraw, "Failed to create a ddraw object.\n");
8003 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
8004 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8006 memset(&surface_desc, 0, sizeof(surface_desc));
8007 surface_desc.dwSize = sizeof(surface_desc);
8008 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
8009 surface_desc.dwWidth = 16;
8010 surface_desc.dwHeight = 16;
8011 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
8012 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8013 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8015 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8016 surface_desc.dwSize = sizeof(surface_desc);
8017 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
8018 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8019 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8021 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8022 surface_desc.dwSize = sizeof(surface_desc);
8023 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, 0, NULL);
8024 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
8025 ok(surface_desc.lpSurface != NULL, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8026 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8027 surface_desc.dwSize = sizeof(surface_desc);
8028 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
8029 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8030 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8031 hr = IDirectDrawSurface_Unlock(surface, NULL);
8032 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
8034 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8035 surface_desc.dwSize = sizeof(surface_desc);
8036 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
8037 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8038 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8040 IDirectDrawSurface_Release(surface);
8041 refcount = IDirectDraw2_Release(ddraw);
8042 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8043 DestroyWindow(window);
8046 static void test_texturemapblend(void)
8048 HRESULT hr;
8049 DDSURFACEDESC ddsd;
8050 DDBLTFX fx;
8051 static RECT rect = {0, 0, 64, 128};
8052 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
8053 DDCOLORKEY ckey;
8054 IDirectDrawSurface *surface, *rt;
8055 IDirect3DTexture2 *texture;
8056 D3DTEXTUREHANDLE texture_handle;
8057 HWND window;
8058 IDirectDraw2 *ddraw;
8059 IDirect3DDevice2 *device;
8060 IDirect3DMaterial2 *material;
8061 IDirect3DViewport2 *viewport;
8062 ULONG ref;
8063 D3DCOLOR color;
8065 static D3DTLVERTEX test1_quads[] =
8067 {{0.0f}, {0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {0.0f}, {0.0f}},
8068 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {0.0f}, {1.0f}},
8069 {{640.0f}, {0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {1.0f}, {0.0f}},
8070 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {1.0f}, {1.0f}},
8071 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {0.0f}, {0.0f}},
8072 {{0.0f}, {480.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {0.0f}, {1.0f}},
8073 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {1.0f}, {0.0f}},
8074 {{640.0f}, {480.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {1.0f}, {1.0f}},
8076 test2_quads[] =
8078 {{0.0f}, {0.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {0.0f}, {0.0f}},
8079 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {0.0f}, {1.0f}},
8080 {{640.0f}, {0.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {1.0f}, {0.0f}},
8081 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {1.0f}, {1.0f}},
8082 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {0.0f}, {0.0f}},
8083 {{0.0f}, {480.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {0.0f}, {1.0f}},
8084 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {1.0f}, {0.0f}},
8085 {{640.0f}, {480.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {1.0f}, {1.0f}},
8088 window = create_window();
8089 ddraw = create_ddraw();
8090 ok(!!ddraw, "Failed to create a ddraw object.\n");
8091 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
8093 skip("Failed to create a 3D device, skipping test.\n");
8094 DestroyWindow(window);
8095 IDirectDraw2_Release(ddraw);
8096 return;
8099 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
8100 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
8102 material = create_diffuse_material(device, 0.0f, 0.0f, 0.0f, 1.0f);
8103 viewport = create_viewport(device, 0, 0, 640, 480);
8104 viewport_set_background(device, viewport, material);
8105 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
8106 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
8108 /* Test alpha with DDPF_ALPHAPIXELS texture - should be taken from texture alpha channel.
8110 * The vertex alpha is completely ignored in this case, so case 1 and 2 combined are not
8111 * a D3DTOP_MODULATE with texture alpha = 0xff in case 2 (no alpha in texture). */
8112 memset(&ddsd, 0, sizeof(ddsd));
8113 ddsd.dwSize = sizeof(ddsd);
8114 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
8115 ddsd.dwHeight = 128;
8116 ddsd.dwWidth = 128;
8117 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
8118 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
8119 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
8120 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
8121 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
8122 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
8123 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
8124 U5(ddsd.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
8125 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
8126 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8128 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
8129 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
8130 hr = IDirect3DTexture2_GetHandle(texture, device, &texture_handle);
8131 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
8132 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
8133 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8135 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
8136 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
8138 memset(&fx, 0, sizeof(fx));
8139 fx.dwSize = sizeof(fx);
8140 U5(fx).dwFillColor = 0xff0000ff;
8141 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8142 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
8143 U5(fx).dwFillColor = 0x800000ff;
8144 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8145 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
8147 /* Note that the ddraw1 version of this test runs tests 1-3 with D3DRENDERSTATE_COLORKEYENABLE
8148 * enabled, whereas this version only runs test 4 with color keying on. Because no color key
8149 * is set on the texture this should not result in different behavior. */
8150 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
8151 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8152 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
8153 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8154 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
8155 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8156 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
8157 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8158 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
8159 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8160 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREMAPBLEND, D3DTBLEND_MODULATE);
8161 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8163 hr = IDirect3DDevice2_BeginScene(device);
8164 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
8165 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &test1_quads[0], 4, 0);
8166 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8167 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &test1_quads[4], 4, 0);
8168 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8169 hr = IDirect3DDevice2_EndScene(device);
8170 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
8172 color = get_surface_color(rt, 5, 5);
8173 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
8174 color = get_surface_color(rt, 400, 5);
8175 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
8176 color = get_surface_color(rt, 5, 245);
8177 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
8178 color = get_surface_color(rt, 400, 245);
8179 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
8181 IDirect3DTexture2_Release(texture);
8182 ref = IDirectDrawSurface_Release(surface);
8183 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
8185 /* Test alpha with texture that has no alpha channel - alpha should be taken from diffuse vertex color. */
8186 memset(&ddsd, 0, sizeof(ddsd));
8187 ddsd.dwSize = sizeof(ddsd);
8188 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
8189 ddsd.dwHeight = 128;
8190 ddsd.dwWidth = 128;
8191 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
8192 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
8193 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
8194 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
8195 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
8196 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
8197 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
8199 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
8200 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8202 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
8203 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
8204 hr = IDirect3DTexture2_GetHandle(texture, device, &texture_handle);
8205 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
8206 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
8207 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8209 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
8210 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
8212 U5(fx).dwFillColor = 0xff0000ff;
8213 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8214 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
8215 U5(fx).dwFillColor = 0x800000ff;
8216 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8217 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
8219 hr = IDirect3DDevice2_BeginScene(device);
8220 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
8221 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &test1_quads[0], 4, 0);
8222 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8223 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &test1_quads[4], 4, 0);
8224 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8225 hr = IDirect3DDevice2_EndScene(device);
8226 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
8228 color = get_surface_color(rt, 5, 5);
8229 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
8230 color = get_surface_color(rt, 400, 5);
8231 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
8232 color = get_surface_color(rt, 5, 245);
8233 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
8234 color = get_surface_color(rt, 400, 245);
8235 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
8237 IDirect3DTexture2_Release(texture);
8238 ref = IDirectDrawSurface_Release(surface);
8239 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
8241 /* Test RGB - should multiply color components from diffuse vertex color and texture. */
8242 memset(&ddsd, 0, sizeof(ddsd));
8243 ddsd.dwSize = sizeof(ddsd);
8244 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
8245 ddsd.dwHeight = 128;
8246 ddsd.dwWidth = 128;
8247 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
8248 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
8249 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
8250 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
8251 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
8252 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
8253 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
8254 U5(ddsd.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
8255 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
8256 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8258 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
8259 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
8260 hr = IDirect3DTexture2_GetHandle(texture, device, &texture_handle);
8261 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
8262 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
8263 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8265 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
8266 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
8268 U5(fx).dwFillColor = 0x00ffffff;
8269 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8270 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
8271 U5(fx).dwFillColor = 0x00ffff80;
8272 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8273 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
8275 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
8276 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8278 hr = IDirect3DDevice2_BeginScene(device);
8279 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
8280 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &test2_quads[0], 4, 0);
8281 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8282 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &test2_quads[4], 4, 0);
8283 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8284 hr = IDirect3DDevice2_EndScene(device);
8285 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
8287 color = get_surface_color(rt, 5, 5);
8288 ok(compare_color(color, 0x00ff0040, 2), "Got unexpected color 0x%08x.\n", color);
8289 color = get_surface_color(rt, 400, 5);
8290 ok(compare_color(color, 0x00ff0080, 2), "Got unexpected color 0x%08x.\n", color);
8291 color = get_surface_color(rt, 5, 245);
8292 ok(compare_color(color, 0x00800080, 2), "Got unexpected color 0x%08x.\n", color);
8293 color = get_surface_color(rt, 400, 245);
8294 ok(compare_color(color, 0x008000ff, 2), "Got unexpected color 0x%08x.\n", color);
8296 IDirect3DTexture2_Release(texture);
8297 ref = IDirectDrawSurface_Release(surface);
8298 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
8300 /* Test alpha again, now with color keyed texture (colorkey emulation in wine can interfere). */
8301 memset(&ddsd, 0, sizeof(ddsd));
8302 ddsd.dwSize = sizeof(ddsd);
8303 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
8304 ddsd.dwHeight = 128;
8305 ddsd.dwWidth = 128;
8306 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
8307 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
8308 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
8309 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 16;
8310 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xf800;
8311 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x07e0;
8312 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x001f;
8314 hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &surface, NULL);
8315 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8317 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
8318 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
8319 hr = IDirect3DTexture2_GetHandle(texture, device, &texture_handle);
8320 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
8321 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
8322 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8324 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
8325 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
8327 U5(fx).dwFillColor = 0xf800;
8328 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8329 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
8330 U5(fx).dwFillColor = 0x001f;
8331 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8332 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
8334 ckey.dwColorSpaceLowValue = 0x001f;
8335 ckey.dwColorSpaceHighValue = 0x001f;
8336 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &ckey);
8337 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
8339 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
8340 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8341 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
8342 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8344 hr = IDirect3DDevice2_BeginScene(device);
8345 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
8346 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &test1_quads[0], 4, 0);
8347 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8348 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_TLVERTEX, &test1_quads[4], 4, 0);
8349 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8350 hr = IDirect3DDevice2_EndScene(device);
8351 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
8353 color = get_surface_color(rt, 5, 5);
8354 ok(compare_color(color, 0x00000000, 2), "Got unexpected color 0x%08x.\n", color);
8355 color = get_surface_color(rt, 400, 5);
8356 ok(compare_color(color, 0x00ff0000, 2), "Got unexpected color 0x%08x.\n", color);
8357 color = get_surface_color(rt, 5, 245);
8358 ok(compare_color(color, 0x00000000, 2), "Got unexpected color 0x%08x.\n", color);
8359 color = get_surface_color(rt, 400, 245);
8360 ok(compare_color(color, 0x00800000, 2), "Got unexpected color 0x%08x.\n", color);
8362 IDirect3DTexture2_Release(texture);
8363 ref = IDirectDrawSurface_Release(surface);
8364 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
8366 destroy_viewport(device, viewport);
8367 ref = IDirect3DMaterial2_Release(material);
8368 ok(ref == 0, "Material not properly released, refcount %u.\n", ref);
8369 IDirectDrawSurface_Release(rt);
8370 IDirect3DDevice2_Release(device);
8371 ref = IDirectDraw2_Release(ddraw);
8372 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
8373 DestroyWindow(window);
8376 static void test_viewport_clear_rect(void)
8378 HRESULT hr;
8379 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
8380 static D3DRECT clear_rect2 = {{90}, {90}, {110}, {110}};
8381 IDirectDrawSurface *rt;
8382 HWND window;
8383 IDirectDraw2 *ddraw;
8384 IDirect3DDevice2 *device;
8385 IDirect3DMaterial2 *red, *green;
8386 IDirect3DViewport2 *viewport, *viewport2;
8387 ULONG ref;
8388 D3DCOLOR color;
8390 window = create_window();
8391 ddraw = create_ddraw();
8392 ok(!!ddraw, "Failed to create a ddraw object.\n");
8393 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
8395 skip("Failed to create a 3D device, skipping test.\n");
8396 DestroyWindow(window);
8397 IDirectDraw2_Release(ddraw);
8398 return;
8401 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
8402 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
8404 red = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
8405 viewport = create_viewport(device, 0, 0, 640, 480);
8406 viewport_set_background(device, viewport, red);
8407 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
8408 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
8410 green = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 1.0f);
8411 viewport2 = create_viewport(device, 100, 100, 20, 20);
8412 viewport_set_background(device, viewport2, green);
8413 hr = IDirect3DViewport2_Clear(viewport2, 1, &clear_rect2, D3DCLEAR_TARGET);
8414 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
8416 color = get_surface_color(rt, 85, 85); /* Outside both. */
8417 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
8418 color = get_surface_color(rt, 95, 95); /* Outside vp, inside rect. */
8419 /* AMD GPUs ignore the viewport dimensions and only care about the rectangle. */
8420 ok(compare_color(color, 0x00ff0000, 1) || broken(compare_color(color, 0x0000ff00, 1)),
8421 "Got unexpected color 0x%08x.\n", color);
8422 color = get_surface_color(rt, 105, 105); /* Inside both. */
8423 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
8424 color = get_surface_color(rt, 115, 115); /* Inside vp, outside rect. */
8425 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
8426 color = get_surface_color(rt, 125, 125); /* Outside both. */
8427 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
8429 destroy_viewport(device, viewport2);
8430 destroy_material(green);
8431 destroy_viewport(device, viewport);
8432 destroy_material(red);
8433 IDirectDrawSurface_Release(rt);
8434 IDirect3DDevice2_Release(device);
8435 ref = IDirectDraw2_Release(ddraw);
8436 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
8437 DestroyWindow(window);
8440 static void test_color_fill(void)
8442 HRESULT hr;
8443 IDirect3DDevice2 *device;
8444 IDirectDraw2 *ddraw;
8445 IDirectDrawSurface *surface, *surface2;
8446 DDSURFACEDESC surface_desc;
8447 ULONG refcount;
8448 BOOL is_warp;
8449 HWND window;
8450 unsigned int i;
8451 DDBLTFX fx;
8452 RECT rect = {5, 5, 7, 7};
8453 DWORD *color;
8454 DWORD num_fourcc_codes, *fourcc_codes;
8455 DDCAPS hal_caps;
8456 BOOL support_uyvy = FALSE, support_yuy2 = FALSE;
8457 static const struct
8459 DWORD caps;
8460 HRESULT colorfill_hr, depthfill_hr;
8461 BOOL rop_success;
8462 const char *name;
8463 DWORD result;
8464 BOOL check_result;
8465 DDPIXELFORMAT format;
8467 tests[] =
8470 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
8471 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem offscreenplain RGB", 0xdeadbeef, TRUE,
8473 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
8474 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
8478 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
8479 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem offscreenplain RGB", 0xdeadbeef, TRUE,
8481 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
8482 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
8486 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
8487 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem texture RGB", 0xdeadbeef, TRUE,
8489 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
8490 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
8494 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
8495 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem texture RGB", 0xdeadbeef, TRUE,
8497 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
8498 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
8502 DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY,
8503 DDERR_INVALIDPARAMS, DD_OK, TRUE, "vidmem zbuffer", 0xdeadbeef, TRUE,
8504 {0, 0, 0, {0}, {0}, {0}, {0}, {0}}
8507 /* Colorfill on YUV surfaces always returns DD_OK, but the content is
8508 * different afterwards. DX9+ GPUs set one of the two luminance values
8509 * in each block, but AMD and Nvidia GPUs disagree on which luminance
8510 * value they set. r200 (dx8) just sets the entire block to the clear
8511 * value. */
8512 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
8513 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem offscreenplain YUY2", 0, FALSE,
8515 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'),
8516 {0}, {0}, {0}, {0}, {0}
8520 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
8521 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem offscreenplain UYVY", 0, FALSE,
8523 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'),
8524 {0}, {0}, {0}, {0}, {0}
8528 DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY,
8529 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem overlay YUY2", 0, FALSE,
8531 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'),
8532 {0}, {0}, {0}, {0}, {0}
8536 DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY,
8537 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem overlay UYVY", 0, FALSE,
8539 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'),
8540 {0}, {0}, {0}, {0}, {0}
8544 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
8545 E_NOTIMPL, DDERR_INVALIDPARAMS, FALSE, "vidmem texture DXT1", 0, FALSE,
8547 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '1'),
8548 {0}, {0}, {0}, {0}, {0}
8552 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
8553 E_NOTIMPL, DDERR_INVALIDPARAMS, FALSE, "sysmem texture DXT1", 0, FALSE,
8555 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '1'),
8556 {0}, {0}, {0}, {0}, {0}
8560 /* The testbot fills this with 0x00 instead of the blue channel. The sysmem
8561 * surface works, presumably because it is handled by the runtime instead of
8562 * the driver. */
8563 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
8564 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem offscreenplain P8", 0xefefefef, FALSE,
8566 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_PALETTEINDEXED8, 0,
8567 {8}, {0}, {0}, {0}, {0}
8571 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
8572 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem offscreenplain P8", 0xefefefef, TRUE,
8574 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_PALETTEINDEXED8, 0,
8575 {8}, {0}, {0}, {0}, {0}
8579 static const struct
8581 DWORD rop;
8582 const char *name;
8583 HRESULT hr;
8585 rops[] =
8587 {SRCCOPY, "SRCCOPY", DD_OK},
8588 {SRCPAINT, "SRCPAINT", DDERR_NORASTEROPHW},
8589 {SRCAND, "SRCAND", DDERR_NORASTEROPHW},
8590 {SRCINVERT, "SRCINVERT", DDERR_NORASTEROPHW},
8591 {SRCERASE, "SRCERASE", DDERR_NORASTEROPHW},
8592 {NOTSRCCOPY, "NOTSRCCOPY", DDERR_NORASTEROPHW},
8593 {NOTSRCERASE, "NOTSRCERASE", DDERR_NORASTEROPHW},
8594 {MERGECOPY, "MERGECOPY", DDERR_NORASTEROPHW},
8595 {MERGEPAINT, "MERGEPAINT", DDERR_NORASTEROPHW},
8596 {PATCOPY, "PATCOPY", DDERR_NORASTEROPHW},
8597 {PATPAINT, "PATPAINT", DDERR_NORASTEROPHW},
8598 {PATINVERT, "PATINVERT", DDERR_NORASTEROPHW},
8599 {DSTINVERT, "DSTINVERT", DDERR_NORASTEROPHW},
8600 {BLACKNESS, "BLACKNESS", DD_OK},
8601 {WHITENESS, "WHITENESS", DD_OK},
8602 {0xaa0029, "0xaa0029", DDERR_NORASTEROPHW} /* noop */
8605 window = create_window();
8606 ddraw = create_ddraw();
8607 ok(!!ddraw, "Failed to create a ddraw object.\n");
8608 is_warp = ddraw_is_warp(ddraw);
8609 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
8611 skip("Failed to create a 3D device, skipping test.\n");
8612 DestroyWindow(window);
8613 IDirectDraw2_Release(ddraw);
8614 return;
8617 hr = IDirectDraw2_GetFourCCCodes(ddraw, &num_fourcc_codes, NULL);
8618 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
8619 fourcc_codes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
8620 num_fourcc_codes * sizeof(*fourcc_codes));
8621 if (!fourcc_codes)
8622 goto done;
8623 hr = IDirectDraw2_GetFourCCCodes(ddraw, &num_fourcc_codes, fourcc_codes);
8624 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
8625 for (i = 0; i < num_fourcc_codes; i++)
8627 if (fourcc_codes[i] == MAKEFOURCC('Y', 'U', 'Y', '2'))
8628 support_yuy2 = TRUE;
8629 else if (fourcc_codes[i] == MAKEFOURCC('U', 'Y', 'V', 'Y'))
8630 support_uyvy = TRUE;
8632 HeapFree(GetProcessHeap(), 0, fourcc_codes);
8634 memset(&hal_caps, 0, sizeof(hal_caps));
8635 hal_caps.dwSize = sizeof(hal_caps);
8636 hr = IDirectDraw2_GetCaps(ddraw, &hal_caps, NULL);
8637 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
8639 if ((!support_yuy2 && !support_uyvy) || !(hal_caps.dwCaps & DDCAPS_OVERLAY))
8640 skip("Overlays or some YUV formats not supported, skipping YUV colorfill tests.\n");
8642 for (i = 0; i < ARRAY_SIZE(tests); i++)
8644 DWORD expected_broken = tests[i].result;
8645 DWORD mask = 0xffffffffu;
8647 /* Some Windows drivers modify dwFillColor when it is used on P8 or FourCC formats. */
8648 memset(&fx, 0, sizeof(fx));
8649 fx.dwSize = sizeof(fx);
8650 U5(fx).dwFillColor = 0xdeadbeef;
8652 memset(&surface_desc, 0, sizeof(surface_desc));
8653 surface_desc.dwSize = sizeof(surface_desc);
8654 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
8655 surface_desc.dwWidth = 64;
8656 surface_desc.dwHeight = 64;
8657 surface_desc.ddpfPixelFormat = tests[i].format;
8658 surface_desc.ddsCaps.dwCaps = tests[i].caps;
8660 if (tests[i].caps & DDSCAPS_TEXTURE)
8662 struct format_support_check check = {&tests[i].format, FALSE};
8663 hr = IDirect3DDevice2_EnumTextureFormats(device, test_unsupported_formats_cb, &check);
8664 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
8665 if (!check.supported)
8666 continue;
8669 if (tests[i].format.dwFourCC == MAKEFOURCC('Y','U','Y','2') && !support_yuy2)
8670 continue;
8671 if (tests[i].format.dwFourCC == MAKEFOURCC('U','Y','V','Y') && !support_uyvy)
8672 continue;
8673 if (tests[i].caps & DDSCAPS_OVERLAY && !(hal_caps.dwCaps & DDCAPS_OVERLAY))
8674 continue;
8676 if (tests[i].caps & DDSCAPS_ZBUFFER)
8678 surface_desc.dwFlags &= ~DDSD_PIXELFORMAT;
8679 surface_desc.dwFlags |= DDSD_ZBUFFERBITDEPTH;
8680 U2(surface_desc).dwZBufferBitDepth = get_device_z_depth(device);
8681 mask >>= (32 - U2(surface_desc).dwZBufferBitDepth);
8682 /* Some drivers seem to convert depth values incorrectly or not at
8683 * all. Affects at least AMD PALM, 8.17.10.1247. */
8684 if (tests[i].caps & DDSCAPS_VIDEOMEMORY)
8686 DWORD expected;
8687 float f, g;
8689 expected = tests[i].result & mask;
8690 f = ceilf(log2f(expected + 1.0f));
8691 g = (f + 1.0f) / 2.0f;
8692 g -= (int)g;
8693 expected_broken = (expected / exp2f(f) - g) * 256;
8694 expected_broken *= 0x01010101;
8698 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8699 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, surface %s.\n", hr, tests[i].name);
8701 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8702 todo_wine_if (tests[i].format.dwFourCC)
8703 ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
8704 hr, tests[i].colorfill_hr, tests[i].name);
8706 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8707 todo_wine_if (tests[i].format.dwFourCC)
8708 ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
8709 hr, tests[i].colorfill_hr, tests[i].name);
8711 if (SUCCEEDED(hr) && tests[i].check_result)
8713 memset(&surface_desc, 0, sizeof(surface_desc));
8714 surface_desc.dwSize = sizeof(surface_desc);
8715 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
8716 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
8717 color = surface_desc.lpSurface;
8718 ok(*color == tests[i].result, "Got clear result 0x%08x, expected 0x%08x, surface %s.\n",
8719 *color, tests[i].result, tests[i].name);
8720 hr = IDirectDrawSurface_Unlock(surface, NULL);
8721 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
8724 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8725 ok(hr == tests[i].depthfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
8726 hr, tests[i].depthfill_hr, tests[i].name);
8727 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8728 ok(hr == tests[i].depthfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
8729 hr, tests[i].depthfill_hr, tests[i].name);
8731 if (SUCCEEDED(hr) && tests[i].check_result)
8733 memset(&surface_desc, 0, sizeof(surface_desc));
8734 surface_desc.dwSize = sizeof(surface_desc);
8735 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
8736 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
8737 color = surface_desc.lpSurface;
8738 todo_wine_if(tests[i].caps & DDSCAPS_VIDEOMEMORY && U2(surface_desc).dwZBufferBitDepth != 16)
8739 ok((*color & mask) == (tests[i].result & mask) || broken((*color & mask) == (expected_broken & mask))
8740 || broken(is_warp && (*color & mask) == (~0u & mask)) /* Windows 8+ testbot. */,
8741 "Got clear result 0x%08x, expected 0x%08x, surface %s.\n",
8742 *color & mask, tests[i].result & mask, tests[i].name);
8743 hr = IDirectDrawSurface_Unlock(surface, NULL);
8744 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
8747 U5(fx).dwFillColor = 0xdeadbeef;
8748 fx.dwROP = BLACKNESS;
8749 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
8750 ok(FAILED(hr) == !tests[i].rop_success, "Blt returned %#x, expected %s, surface %s.\n",
8751 hr, tests[i].rop_success ? "success" : "failure", tests[i].name);
8752 ok(U5(fx).dwFillColor == 0xdeadbeef, "dwFillColor was set to 0x%08x, surface %s\n",
8753 U5(fx).dwFillColor, tests[i].name);
8755 if (SUCCEEDED(hr) && tests[i].check_result)
8757 memset(&surface_desc, 0, sizeof(surface_desc));
8758 surface_desc.dwSize = sizeof(surface_desc);
8759 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
8760 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
8761 color = surface_desc.lpSurface;
8762 ok(*color == 0, "Got clear result 0x%08x, expected 0x00000000, surface %s.\n",
8763 *color, tests[i].name);
8764 hr = IDirectDrawSurface_Unlock(surface, NULL);
8765 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
8768 fx.dwROP = WHITENESS;
8769 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
8770 ok(FAILED(hr) == !tests[i].rop_success, "Blt returned %#x, expected %s, surface %s.\n",
8771 hr, tests[i].rop_success ? "success" : "failure", tests[i].name);
8772 ok(U5(fx).dwFillColor == 0xdeadbeef, "dwFillColor was set to 0x%08x, surface %s\n",
8773 U5(fx).dwFillColor, tests[i].name);
8775 if (SUCCEEDED(hr) && tests[i].check_result)
8777 memset(&surface_desc, 0, sizeof(surface_desc));
8778 surface_desc.dwSize = sizeof(surface_desc);
8779 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
8780 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
8781 color = surface_desc.lpSurface;
8782 /* WHITENESS sets the alpha channel to 0x00. Ignore this for now. */
8783 ok((*color & 0x00ffffff) == 0x00ffffff, "Got clear result 0x%08x, expected 0xffffffff, surface %s.\n",
8784 *color, tests[i].name);
8785 hr = IDirectDrawSurface_Unlock(surface, NULL);
8786 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
8789 IDirectDrawSurface_Release(surface);
8792 memset(&fx, 0, sizeof(fx));
8793 fx.dwSize = sizeof(fx);
8794 U5(fx).dwFillColor = 0xdeadbeef;
8795 fx.dwROP = WHITENESS;
8797 memset(&surface_desc, 0, sizeof(surface_desc));
8798 surface_desc.dwSize = sizeof(surface_desc);
8799 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
8800 surface_desc.dwWidth = 64;
8801 surface_desc.dwHeight = 64;
8802 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
8803 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
8804 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
8805 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
8806 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
8807 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
8808 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
8809 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8810 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8811 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
8812 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8814 /* No DDBLTFX. */
8815 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, NULL);
8816 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8817 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_ROP | DDBLT_WAIT, NULL);
8818 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8820 /* Unused source rectangle. */
8821 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8822 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
8823 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
8824 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
8826 /* Unused source surface. */
8827 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8828 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8829 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
8830 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
8831 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8832 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8833 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
8834 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
8836 /* Inverted destination or source rectangle. */
8837 SetRect(&rect, 5, 7, 7, 5);
8838 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8839 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
8840 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8841 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
8842 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8843 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8844 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8845 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8846 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
8847 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
8849 /* Negative rectangle. */
8850 SetRect(&rect, -1, -1, 5, 5);
8851 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8852 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
8853 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8854 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
8855 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8856 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8857 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8858 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8859 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
8860 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
8862 /* Out of bounds rectangle. */
8863 SetRect(&rect, 0, 0, 65, 65);
8864 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
8865 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
8866 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
8867 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
8869 /* Combine multiple flags. */
8870 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8871 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
8872 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_ROP | DDBLT_WAIT, &fx);
8873 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8874 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_ROP | DDBLT_WAIT, &fx);
8875 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8877 for (i = 0; i < ARRAY_SIZE(rops); i++)
8879 fx.dwROP = rops[i].rop;
8880 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
8881 ok(hr == rops[i].hr, "Got unexpected hr %#x for rop %s.\n", hr, rops[i].name);
8884 IDirectDrawSurface_Release(surface2);
8885 IDirectDrawSurface_Release(surface);
8887 memset(&surface_desc, 0, sizeof(surface_desc));
8888 surface_desc.dwSize = sizeof(surface_desc);
8889 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_ZBUFFERBITDEPTH;
8890 surface_desc.dwWidth = 64;
8891 surface_desc.dwHeight = 64;
8892 U2(surface_desc).dwZBufferBitDepth = get_device_z_depth(device);
8893 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
8894 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8895 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8896 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
8897 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8899 /* No DDBLTFX. */
8900 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, NULL);
8901 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8903 /* Unused source rectangle. */
8904 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8905 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
8907 /* Unused source surface. */
8908 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8909 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8910 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8911 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8913 /* Inverted destination or source rectangle. */
8914 SetRect(&rect, 5, 7, 7, 5);
8915 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8916 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
8917 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8918 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
8919 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8920 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8921 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8922 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8924 /* Negative rectangle. */
8925 SetRect(&rect, -1, -1, 5, 5);
8926 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8927 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
8928 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8929 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
8930 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8931 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8932 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8933 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8935 /* Out of bounds rectangle. */
8936 SetRect(&rect, 0, 0, 65, 65);
8937 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8938 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
8940 /* Combine multiple flags. */
8941 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
8942 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8944 IDirectDrawSurface_Release(surface2);
8945 IDirectDrawSurface_Release(surface);
8947 done:
8948 IDirect3DDevice2_Release(device);
8949 refcount = IDirectDraw2_Release(ddraw);
8950 ok(refcount == 0, "Ddraw object not properly released, refcount %u.\n", refcount);
8951 DestroyWindow(window);
8954 static void test_colorkey_precision(void)
8956 static D3DLVERTEX quad[] =
8958 {{-1.0f}, {-1.0f}, {0.0f}, 0, {0xff000000}, {0}, {0.0f}, {0.0f}},
8959 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xff000000}, {0}, {0.0f}, {1.0f}},
8960 {{ 1.0f}, {-1.0f}, {0.0f}, 0, {0xff000000}, {0}, {1.0f}, {0.0f}},
8961 {{ 1.0f}, { 1.0f}, {0.0f}, 0, {0xff000000}, {0}, {1.0f}, {1.0f}},
8963 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
8964 IDirect3DDevice2 *device;
8965 IDirectDraw2 *ddraw;
8966 IDirectDrawSurface *rt;
8967 IDirect3DViewport2 *viewport;
8968 HWND window;
8969 HRESULT hr;
8970 IDirectDrawSurface *src, *dst, *texture;
8971 D3DTEXTUREHANDLE handle;
8972 IDirect3DTexture2 *d3d_texture;
8973 IDirect3DMaterial2 *green;
8974 DDSURFACEDESC surface_desc, lock_desc;
8975 ULONG refcount;
8976 D3DCOLOR color;
8977 unsigned int t, c;
8978 DDCOLORKEY ckey;
8979 DDBLTFX fx;
8980 DWORD data[4] = {0}, color_mask;
8981 BOOL is_nvidia, is_warp;
8982 static const struct
8984 unsigned int max, shift, bpp, clear;
8985 const char *name;
8986 BOOL skip_nv;
8987 DDPIXELFORMAT fmt;
8989 tests[] =
8992 255, 0, 4, 0x00345678, "D3DFMT_X8R8G8B8", FALSE,
8994 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
8995 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0x00000000}
9000 63, 5, 2, 0x5678, "D3DFMT_R5G6B5, G channel", FALSE,
9002 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
9003 {16}, {0xf800}, {0x07e0}, {0x001f}, {0x0000}
9008 31, 0, 2, 0x5678, "D3DFMT_R5G6B5, B channel", FALSE,
9010 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
9011 {16}, {0xf800}, {0x07e0}, {0x001f}, {0x0000}
9016 15, 0, 2, 0x0678, "D3DFMT_A4R4G4B4", TRUE,
9018 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9019 {16}, {0x0f00}, {0x00f0}, {0x000f}, {0xf000}
9024 window = create_window();
9025 ddraw = create_ddraw();
9026 ok(!!ddraw, "Failed to create a ddraw object.\n");
9027 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
9029 skip("Failed to create a 3D device, skipping test.\n");
9030 DestroyWindow(window);
9031 IDirectDraw2_Release(ddraw);
9032 return;
9034 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
9035 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
9037 is_nvidia = ddraw_is_nvidia(ddraw);
9038 /* The Windows 8 WARP driver has plenty of false negatives in X8R8G8B8
9039 * (color key doesn't match although the values are equal), and a false
9040 * positive when the color key is 0 and the texture contains the value 1.
9041 * I don't want to mark this broken unconditionally since this would
9042 * essentially disable the test on Windows. Also on random occasions
9043 * 254 == 255 and 255 != 255.*/
9044 is_warp = ddraw_is_warp(ddraw);
9046 green = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
9047 viewport = create_viewport(device, 0, 0, 640, 480);
9048 viewport_set_background(device, viewport, green);
9049 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
9050 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
9052 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
9053 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
9054 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
9055 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
9056 /* There's no way to ignore the texture color in d3d2, so multiply the texture color
9057 * with a black vertex color. */
9058 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREMAPBLEND, D3DTBLEND_MODULATEALPHA);
9059 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
9061 memset(&fx, 0, sizeof(fx));
9062 fx.dwSize = sizeof(fx);
9063 memset(&lock_desc, 0, sizeof(lock_desc));
9064 lock_desc.dwSize = sizeof(lock_desc);
9066 for (t = 0; t < ARRAY_SIZE(tests); ++t)
9068 if (is_nvidia && tests[t].skip_nv)
9070 win_skip("Skipping test %s on Nvidia Windows drivers.\n", tests[t].name);
9071 continue;
9074 memset(&surface_desc, 0, sizeof(surface_desc));
9075 surface_desc.dwSize = sizeof(surface_desc);
9076 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9077 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
9078 surface_desc.dwWidth = 4;
9079 surface_desc.dwHeight = 1;
9080 surface_desc.ddpfPixelFormat = tests[t].fmt;
9081 /* Windows XP (at least with the r200 driver, other drivers untested) produces
9082 * garbage when doing color keyed texture->texture blits. */
9083 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &src, NULL);
9084 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9085 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &dst, NULL);
9086 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9088 U5(fx).dwFillColor = tests[t].clear;
9089 /* On the w8 testbot (WARP driver) the blit result has different values in the
9090 * X channel. */
9091 color_mask = U2(tests[t].fmt).dwRBitMask
9092 | U3(tests[t].fmt).dwGBitMask
9093 | U4(tests[t].fmt).dwBBitMask;
9095 for (c = 0; c <= tests[t].max; ++c)
9097 /* The idiotic Nvidia Windows driver can't change the color key on a d3d
9098 * texture after it has been set once... */
9099 surface_desc.dwFlags |= DDSD_CKSRCBLT;
9100 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
9101 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = c << tests[t].shift;
9102 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = c << tests[t].shift;
9103 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &texture, NULL);
9104 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9106 hr = IDirectDrawSurface4_QueryInterface(texture, &IID_IDirect3DTexture2, (void **)&d3d_texture);
9107 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
9108 hr = IDirect3DTexture2_GetHandle(d3d_texture, device, &handle);
9109 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
9110 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, handle);
9111 ok(SUCCEEDED(hr), "Failed to set texture handle, hr %#x.\n", hr);
9112 IDirect3DTexture2_Release(d3d_texture);
9114 hr = IDirectDrawSurface_Blt(dst, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9115 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
9117 hr = IDirectDrawSurface_Lock(src, NULL, &lock_desc, DDLOCK_WAIT, NULL);
9118 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
9119 switch (tests[t].bpp)
9121 case 4:
9122 ((DWORD *)lock_desc.lpSurface)[0] = (c ? c - 1 : 0) << tests[t].shift;
9123 ((DWORD *)lock_desc.lpSurface)[1] = c << tests[t].shift;
9124 ((DWORD *)lock_desc.lpSurface)[2] = min(c + 1, tests[t].max) << tests[t].shift;
9125 ((DWORD *)lock_desc.lpSurface)[3] = 0xffffffff;
9126 break;
9128 case 2:
9129 ((WORD *)lock_desc.lpSurface)[0] = (c ? c - 1 : 0) << tests[t].shift;
9130 ((WORD *)lock_desc.lpSurface)[1] = c << tests[t].shift;
9131 ((WORD *)lock_desc.lpSurface)[2] = min(c + 1, tests[t].max) << tests[t].shift;
9132 ((WORD *)lock_desc.lpSurface)[3] = 0xffff;
9133 break;
9135 hr = IDirectDrawSurface_Unlock(src, 0);
9136 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
9137 hr = IDirectDrawSurface_Blt(texture, NULL, src, NULL, DDBLT_WAIT, NULL);
9138 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
9140 ckey.dwColorSpaceLowValue = c << tests[t].shift;
9141 ckey.dwColorSpaceHighValue = c << tests[t].shift;
9142 hr = IDirectDrawSurface_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
9143 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
9145 hr = IDirectDrawSurface_Blt(dst, NULL, src, NULL, DDBLT_KEYSRC | DDBLT_WAIT, NULL);
9146 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
9148 /* Don't make this read only, it somehow breaks the detection of the Nvidia bug below. */
9149 hr = IDirectDrawSurface_Lock(dst, NULL, &lock_desc, DDLOCK_WAIT, NULL);
9150 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
9151 switch (tests[t].bpp)
9153 case 4:
9154 data[0] = ((DWORD *)lock_desc.lpSurface)[0] & color_mask;
9155 data[1] = ((DWORD *)lock_desc.lpSurface)[1] & color_mask;
9156 data[2] = ((DWORD *)lock_desc.lpSurface)[2] & color_mask;
9157 data[3] = ((DWORD *)lock_desc.lpSurface)[3] & color_mask;
9158 break;
9160 case 2:
9161 data[0] = ((WORD *)lock_desc.lpSurface)[0] & color_mask;
9162 data[1] = ((WORD *)lock_desc.lpSurface)[1] & color_mask;
9163 data[2] = ((WORD *)lock_desc.lpSurface)[2] & color_mask;
9164 data[3] = ((WORD *)lock_desc.lpSurface)[3] & color_mask;
9165 break;
9167 hr = IDirectDrawSurface_Unlock(dst, 0);
9168 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
9170 if (!c)
9172 ok(data[0] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
9173 tests[t].clear, data[0], tests[t].name, c);
9175 if (data[3] == tests[t].clear)
9177 /* My Geforce GTX 460 on Windows 7 misbehaves when A4R4G4B4 is blitted with color
9178 * keying: The blit takes ~0.5 seconds, and subsequent color keying draws are broken,
9179 * even when a different surface is used. The blit itself doesn't draw anything,
9180 * so we can detect the bug by looking at the otherwise unused 4th texel. It should
9181 * never be masked out by the key.
9183 * On Windows 10 the problem is worse, Blt just hangs. For this reason the ARGB4444
9184 * test is disabled entirely.
9186 * Also appears to affect the testbot in some way with R5G6B5. Color keying is
9187 * terrible on WARP. */
9188 skip("Nvidia A4R4G4B4 color keying blit bug detected, skipping.\n");
9189 IDirectDrawSurface_Release(texture);
9190 IDirectDrawSurface_Release(src);
9191 IDirectDrawSurface_Release(dst);
9192 goto done;
9195 else
9196 ok(data[0] == (c - 1) << tests[t].shift, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
9197 (c - 1) << tests[t].shift, data[0], tests[t].name, c);
9199 ok(data[1] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
9200 tests[t].clear, data[1], tests[t].name, c);
9202 if (c == tests[t].max)
9203 ok(data[2] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
9204 tests[t].clear, data[2], tests[t].name, c);
9205 else
9206 ok(data[2] == (c + 1) << tests[t].shift, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
9207 (c + 1) << tests[t].shift, data[2], tests[t].name, c);
9209 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
9210 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
9212 hr = IDirect3DDevice2_BeginScene(device);
9213 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
9214 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, quad, 4, 0);
9215 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
9216 hr = IDirect3DDevice2_EndScene(device);
9217 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
9219 color = get_surface_color(rt, 80, 240);
9220 if (!c)
9221 ok(compare_color(color, 0x0000ff00, 1) || broken(is_warp && compare_color(color, 0x00000000, 1)),
9222 "Got unexpected color 0x%08x, format %s, c=%u.\n",
9223 color, tests[t].name, c);
9224 else
9225 ok(compare_color(color, 0x00000000, 1) || broken(is_warp && compare_color(color, 0x0000ff00, 1)),
9226 "Got unexpected color 0x%08x, format %s, c=%u.\n",
9227 color, tests[t].name, c);
9229 color = get_surface_color(rt, 240, 240);
9230 ok(compare_color(color, 0x0000ff00, 1) || broken(is_warp && compare_color(color, 0x00000000, 1)),
9231 "Got unexpected color 0x%08x, format %s, c=%u.\n",
9232 color, tests[t].name, c);
9234 color = get_surface_color(rt, 400, 240);
9235 if (c == tests[t].max)
9236 ok(compare_color(color, 0x0000ff00, 1) || broken(is_warp && compare_color(color, 0x00000000, 1)),
9237 "Got unexpected color 0x%08x, format %s, c=%u.\n",
9238 color, tests[t].name, c);
9239 else
9240 ok(compare_color(color, 0x00000000, 1) || broken(is_warp && compare_color(color, 0x0000ff00, 1)),
9241 "Got unexpected color 0x%08x, format %s, c=%u.\n",
9242 color, tests[t].name, c);
9244 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, 0);
9245 ok(SUCCEEDED(hr), "Failed to set texture handle, hr %#x.\n", hr);
9246 IDirectDrawSurface_Release(texture);
9248 IDirectDrawSurface_Release(src);
9249 IDirectDrawSurface_Release(dst);
9251 done:
9253 destroy_viewport(device, viewport);
9254 destroy_material(green);
9255 IDirectDrawSurface_Release(rt);
9256 IDirect3DDevice2_Release(device);
9257 refcount = IDirectDraw2_Release(ddraw);
9258 ok(refcount == 0, "Ddraw object not properly released, refcount %u.\n", refcount);
9259 DestroyWindow(window);
9262 static void test_range_colorkey(void)
9264 IDirectDraw2 *ddraw;
9265 HWND window;
9266 HRESULT hr;
9267 IDirectDrawSurface *surface;
9268 DDSURFACEDESC surface_desc;
9269 ULONG refcount;
9270 DDCOLORKEY ckey;
9272 window = create_window();
9273 ddraw = create_ddraw();
9274 ok(!!ddraw, "Failed to create a ddraw object.\n");
9275 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
9276 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
9278 memset(&surface_desc, 0, sizeof(surface_desc));
9279 surface_desc.dwSize = sizeof(surface_desc);
9280 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
9281 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
9282 surface_desc.dwWidth = 1;
9283 surface_desc.dwHeight = 1;
9284 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
9285 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
9286 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
9287 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
9288 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
9289 U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0x00000000;
9291 /* Creating a surface with a range color key fails with DDERR_NOCOLORKEY. */
9292 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
9293 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000001;
9294 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9295 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
9297 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000001;
9298 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
9299 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9300 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
9302 /* Same for DDSCAPS_OFFSCREENPLAIN. */
9303 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
9304 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
9305 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000001;
9306 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9307 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
9309 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000001;
9310 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
9311 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9312 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
9314 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
9315 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
9316 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9317 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9319 /* Setting a range color key without DDCKEY_COLORSPACE collapses the key. */
9320 ckey.dwColorSpaceLowValue = 0x00000000;
9321 ckey.dwColorSpaceHighValue = 0x00000001;
9322 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &ckey);
9323 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
9325 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
9326 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
9327 ok(!ckey.dwColorSpaceLowValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
9328 ok(!ckey.dwColorSpaceHighValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
9330 ckey.dwColorSpaceLowValue = 0x00000001;
9331 ckey.dwColorSpaceHighValue = 0x00000000;
9332 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &ckey);
9333 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
9335 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
9336 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
9337 ok(ckey.dwColorSpaceLowValue == 0x00000001, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
9338 ok(ckey.dwColorSpaceHighValue == 0x00000001, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
9340 /* DDCKEY_COLORSPACE is ignored if the key is a single value. */
9341 ckey.dwColorSpaceLowValue = 0x00000000;
9342 ckey.dwColorSpaceHighValue = 0x00000000;
9343 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
9344 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
9346 /* Using it with a range key results in DDERR_NOCOLORKEYHW. */
9347 ckey.dwColorSpaceLowValue = 0x00000001;
9348 ckey.dwColorSpaceHighValue = 0x00000000;
9349 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
9350 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
9351 ckey.dwColorSpaceLowValue = 0x00000000;
9352 ckey.dwColorSpaceHighValue = 0x00000001;
9353 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
9354 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
9355 /* Range destination keys don't work either. */
9356 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_DESTBLT | DDCKEY_COLORSPACE, &ckey);
9357 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
9359 /* Just to show it's not because of A, R, and G having equal values. */
9360 ckey.dwColorSpaceLowValue = 0x00000000;
9361 ckey.dwColorSpaceHighValue = 0x01010101;
9362 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
9363 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
9365 /* None of these operations modified the key. */
9366 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
9367 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
9368 ok(!ckey.dwColorSpaceLowValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
9369 ok(!ckey.dwColorSpaceHighValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
9371 IDirectDrawSurface_Release(surface),
9372 refcount = IDirectDraw2_Release(ddraw);
9373 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
9374 DestroyWindow(window);
9377 static void test_shademode(void)
9379 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
9380 IDirect3DMaterial2 *background;
9381 IDirect3DViewport2 *viewport;
9382 IDirect3DDevice2 *device;
9383 IDirectDrawSurface *rt;
9384 DWORD color0, color1;
9385 IDirectDraw2 *ddraw;
9386 D3DLVERTEX *quad;
9387 ULONG refcount;
9388 UINT i, count;
9389 HWND window;
9390 HRESULT hr;
9391 static D3DLVERTEX quad_strip[] =
9393 {{-1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}},
9394 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xff00ff00}},
9395 {{ 1.0f}, {-1.0f}, {0.0f}, 0, {0xff0000ff}},
9396 {{ 1.0f}, { 1.0f}, {0.0f}, 0, {0xffffffff}},
9398 quad_list[] =
9400 {{-1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}},
9401 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xff00ff00}},
9402 {{ 1.0f}, {-1.0f}, {0.0f}, 0, {0xff0000ff}},
9404 {{ 1.0f}, {-1.0f}, {0.0f}, 0, {0xff0000ff}},
9405 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xff00ff00}},
9406 {{ 1.0f}, { 1.0f}, {0.0f}, 0, {0xffffffff}},
9408 static const struct
9410 DWORD primtype;
9411 DWORD shademode;
9412 DWORD color0, color1;
9414 tests[] =
9416 {D3DPT_TRIANGLESTRIP, D3DSHADE_FLAT, 0x00ff0000, 0x0000ff00},
9417 {D3DPT_TRIANGLESTRIP, D3DSHADE_PHONG, 0x000dca28, 0x000d45c7},
9418 {D3DPT_TRIANGLESTRIP, D3DSHADE_GOURAUD, 0x000dca28, 0x000d45c7},
9419 {D3DPT_TRIANGLESTRIP, D3DSHADE_PHONG, 0x000dca28, 0x000d45c7},
9420 {D3DPT_TRIANGLELIST, D3DSHADE_FLAT, 0x00ff0000, 0x000000ff},
9421 {D3DPT_TRIANGLELIST, D3DSHADE_GOURAUD, 0x000dca28, 0x000d45c7},
9424 window = create_window();
9425 ddraw = create_ddraw();
9426 ok(!!ddraw, "Failed to create a ddraw object.\n");
9427 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
9429 skip("Failed to create a 3D device, skipping test.\n");
9430 IDirectDraw2_Release(ddraw);
9431 DestroyWindow(window);
9432 return;
9435 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
9436 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
9438 background = create_diffuse_material(device, 1.0f, 1.0f, 1.0f, 1.0f);
9439 viewport = create_viewport(device, 0, 0, 640, 480);
9440 viewport_set_background(device, viewport, background);
9441 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
9442 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
9444 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
9445 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
9447 /* Try it first with a TRIANGLESTRIP. Do it with different geometry because
9448 * the color fixups we have to do for FLAT shading will be dependent on that. */
9450 for (i = 0; i < ARRAY_SIZE(tests); ++i)
9452 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
9453 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
9455 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_SHADEMODE, tests[i].shademode);
9456 ok(hr == D3D_OK, "Failed to set shade mode, hr %#x.\n", hr);
9458 hr = IDirect3DDevice2_BeginScene(device);
9459 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
9460 quad = tests[i].primtype == D3DPT_TRIANGLESTRIP ? quad_strip : quad_list;
9461 count = tests[i].primtype == D3DPT_TRIANGLESTRIP ? 4 : 6;
9462 hr = IDirect3DDevice2_DrawPrimitive(device, tests[i].primtype, D3DVT_LVERTEX, quad, count, 0);
9463 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
9464 hr = IDirect3DDevice2_EndScene(device);
9465 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
9467 color0 = get_surface_color(rt, 100, 100); /* Inside first triangle */
9468 color1 = get_surface_color(rt, 500, 350); /* Inside second triangle */
9470 /* For D3DSHADE_FLAT it should take the color of the first vertex of
9471 * each triangle. This requires EXT_provoking_vertex or similar
9472 * functionality being available. */
9473 /* PHONG should be the same as GOURAUD, since no hardware implements
9474 * this. */
9475 ok(compare_color(color0, tests[i].color0, 1), "Test %u shading has color0 %08x, expected %08x.\n",
9476 i, color0, tests[i].color0);
9477 ok(compare_color(color1, tests[i].color1, 1), "Test %u shading has color1 %08x, expected %08x.\n",
9478 i, color1, tests[i].color1);
9481 destroy_viewport(device, viewport);
9482 destroy_material(background);
9483 IDirectDrawSurface_Release(rt);
9484 refcount = IDirect3DDevice2_Release(device);
9485 ok(!refcount, "Device has %u references left.\n", refcount);
9486 IDirectDraw_Release(ddraw);
9487 DestroyWindow(window);
9490 static void test_lockrect_invalid(void)
9492 unsigned int i, r;
9493 IDirectDraw2 *ddraw;
9494 IDirectDrawSurface *surface1;
9495 IDirectDrawSurface2 *surface;
9496 HWND window;
9497 HRESULT hr;
9498 DDSURFACEDESC surface_desc;
9499 DDCAPS hal_caps;
9500 DWORD needed_caps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
9501 static RECT valid[] =
9503 {60, 60, 68, 68},
9504 {60, 60, 60, 68},
9505 {60, 60, 68, 60},
9506 {120, 60, 128, 68},
9507 {60, 120, 68, 128},
9509 static RECT invalid[] =
9511 {68, 60, 60, 68}, /* left > right */
9512 {60, 68, 68, 60}, /* top > bottom */
9513 {-8, 60, 0, 68}, /* left < surface */
9514 {60, -8, 68, 0}, /* top < surface */
9515 {-16, 60, -8, 68}, /* right < surface */
9516 {60, -16, 68, -8}, /* bottom < surface */
9517 {60, 60, 136, 68}, /* right > surface */
9518 {60, 60, 68, 136}, /* bottom > surface */
9519 {136, 60, 144, 68}, /* left > surface */
9520 {60, 136, 68, 144}, /* top > surface */
9522 static const struct
9524 DWORD caps;
9525 const char *name;
9526 HRESULT hr;
9528 resources[] =
9530 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, "sysmem offscreenplain", DDERR_INVALIDPARAMS},
9531 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, "vidmem offscreenplain", DDERR_INVALIDPARAMS},
9532 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, "sysmem texture", DDERR_INVALIDPARAMS},
9533 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, "vidmem texture", DDERR_INVALIDPARAMS},
9536 window = create_window();
9537 ddraw = create_ddraw();
9538 ok(!!ddraw, "Failed to create a ddraw object.\n");
9539 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
9540 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
9542 memset(&hal_caps, 0, sizeof(hal_caps));
9543 hal_caps.dwSize = sizeof(hal_caps);
9544 hr = IDirectDraw2_GetCaps(ddraw, &hal_caps, NULL);
9545 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
9546 if ((hal_caps.ddsCaps.dwCaps & needed_caps) != needed_caps)
9548 skip("Required surface types not supported, skipping test.\n");
9549 goto done;
9552 for (r = 0; r < ARRAY_SIZE(resources); ++r)
9554 memset(&surface_desc, 0, sizeof(surface_desc));
9555 surface_desc.dwSize = sizeof(surface_desc);
9556 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9557 surface_desc.ddsCaps.dwCaps = resources[r].caps;
9558 surface_desc.dwWidth = 128;
9559 surface_desc.dwHeight = 128;
9560 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
9561 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
9562 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
9563 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0xff0000;
9564 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x00ff00;
9565 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x0000ff;
9567 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
9568 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, type %s.\n", hr, resources[r].name);
9569 hr = IDirectDrawSurface_QueryInterface(surface1, &IID_IDirectDrawSurface2, (void **)&surface);
9570 ok(SUCCEEDED(hr), "Failed to QI IDirectDrawSurface2 interface, hr %#x.\n", hr);
9571 IDirectDrawSurface_Release(surface1);
9573 hr = IDirectDrawSurface2_Lock(surface, NULL, NULL, DDLOCK_WAIT, NULL);
9574 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x, type %s.\n", hr, resources[r].name);
9576 for (i = 0; i < ARRAY_SIZE(valid); ++i)
9578 RECT *rect = &valid[i];
9580 memset(&surface_desc, 0, sizeof(surface_desc));
9581 surface_desc.dwSize = sizeof(surface_desc);
9583 hr = IDirectDrawSurface2_Lock(surface, rect, &surface_desc, DDLOCK_WAIT, NULL);
9584 ok(SUCCEEDED(hr), "Lock failed (%#x) for rect %s, type %s.\n",
9585 hr, wine_dbgstr_rect(rect), resources[r].name);
9587 hr = IDirectDrawSurface2_Unlock(surface, NULL);
9588 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
9591 for (i = 0; i < ARRAY_SIZE(invalid); ++i)
9593 RECT *rect = &invalid[i];
9595 memset(&surface_desc, 1, sizeof(surface_desc));
9596 surface_desc.dwSize = sizeof(surface_desc);
9598 hr = IDirectDrawSurface2_Lock(surface, rect, &surface_desc, DDLOCK_WAIT, NULL);
9599 ok(hr == resources[r].hr, "Lock returned %#x for rect %s, type %s.\n",
9600 hr, wine_dbgstr_rect(rect), resources[r].name);
9601 if (SUCCEEDED(hr))
9603 hr = IDirectDrawSurface2_Unlock(surface, NULL);
9604 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
9606 else
9607 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
9610 hr = IDirectDrawSurface2_Lock(surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
9611 ok(SUCCEEDED(hr), "Lock(rect = NULL) failed, hr %#x, type %s.\n",
9612 hr, resources[r].name);
9613 hr = IDirectDrawSurface2_Lock(surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
9614 ok(hr == DDERR_SURFACEBUSY, "Double lock(rect = NULL) returned %#x, type %s.\n",
9615 hr, resources[r].name);
9616 hr = IDirectDrawSurface2_Unlock(surface, NULL);
9617 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
9619 hr = IDirectDrawSurface2_Lock(surface, &valid[0], &surface_desc, DDLOCK_WAIT, NULL);
9620 ok(SUCCEEDED(hr), "Lock(rect = %s) failed (%#x).\n", wine_dbgstr_rect(&valid[0]), hr);
9621 hr = IDirectDrawSurface2_Lock(surface, &valid[0], &surface_desc, DDLOCK_WAIT, NULL);
9622 ok(hr == DDERR_SURFACEBUSY, "Double lock(rect = %s) failed (%#x).\n",
9623 wine_dbgstr_rect(&valid[0]), hr);
9625 /* Locking a different rectangle returns DD_OK, but it seems to break the surface.
9626 * Afterwards unlocking the surface fails(NULL rectangle or both locked rectangles) */
9628 hr = IDirectDrawSurface2_Unlock(surface, NULL);
9629 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
9631 IDirectDrawSurface2_Release(surface);
9634 done:
9635 IDirectDraw2_Release(ddraw);
9636 DestroyWindow(window);
9639 static void test_yv12_overlay(void)
9641 IDirectDrawSurface *src_surface, *dst_surface;
9642 RECT rect = {13, 17, 14, 18};
9643 unsigned int offset, y;
9644 unsigned char *base;
9645 IDirectDraw2 *ddraw;
9646 DDSURFACEDESC desc;
9647 HWND window;
9648 HRESULT hr;
9650 window = create_window();
9651 ddraw = create_ddraw();
9652 ok(!!ddraw, "Failed to create a ddraw object.\n");
9653 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
9654 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
9656 if (!(src_surface = create_overlay(ddraw, 256, 256, MAKEFOURCC('Y','V','1','2'))))
9658 skip("Failed to create a YV12 overlay, skipping test.\n");
9659 goto done;
9662 memset(&desc, 0, sizeof(desc));
9663 desc.dwSize = sizeof(desc);
9664 hr = IDirectDrawSurface_Lock(src_surface, NULL, &desc, DDLOCK_WAIT, NULL);
9665 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
9667 ok(desc.dwFlags == (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS | DDSD_PITCH),
9668 "Got unexpected flags %#x.\n", desc.dwFlags);
9669 ok(desc.ddsCaps.dwCaps == (DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM | DDSCAPS_HWCODEC)
9670 || desc.ddsCaps.dwCaps == (DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
9671 "Got unexpected caps %#x.\n", desc.ddsCaps.dwCaps);
9672 ok(desc.dwWidth == 256, "Got unexpected width %u.\n", desc.dwWidth);
9673 ok(desc.dwHeight == 256, "Got unexpected height %u.\n", desc.dwHeight);
9674 /* The overlay pitch seems to have 256 byte alignment. */
9675 ok(!(U1(desc).lPitch & 0xff), "Got unexpected pitch %u.\n", U1(desc).lPitch);
9677 /* Fill the surface with some data for the blit test. */
9678 base = desc.lpSurface;
9679 /* Luminance */
9680 for (y = 0; y < desc.dwHeight; ++y)
9682 memset(base + U1(desc).lPitch * y, 0x10, desc.dwWidth);
9684 /* V */
9685 for (; y < desc.dwHeight + desc.dwHeight / 4; ++y)
9687 memset(base + U1(desc).lPitch * y, 0x20, desc.dwWidth);
9689 /* U */
9690 for (; y < desc.dwHeight + desc.dwHeight / 2; ++y)
9692 memset(base + U1(desc).lPitch * y, 0x30, desc.dwWidth);
9695 hr = IDirectDrawSurface_Unlock(src_surface, NULL);
9696 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
9698 /* YV12 uses 2x2 blocks with 6 bytes per block (4*Y, 1*U, 1*V). Unlike
9699 * other block-based formats like DXT the entire Y channel is stored in
9700 * one big chunk of memory, followed by the chroma channels. So partial
9701 * locks do not really make sense. Show that they are allowed nevertheless
9702 * and the offset points into the luminance data. */
9703 hr = IDirectDrawSurface_Lock(src_surface, &rect, &desc, DDLOCK_WAIT, NULL);
9704 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
9705 offset = ((const unsigned char *)desc.lpSurface - base);
9706 ok(offset == rect.top * U1(desc).lPitch + rect.left, "Got unexpected offset %u, expected %u.\n",
9707 offset, rect.top * U1(desc).lPitch + rect.left);
9708 hr = IDirectDrawSurface_Unlock(src_surface, NULL);
9709 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
9711 if (!(dst_surface = create_overlay(ddraw, 256, 256, MAKEFOURCC('Y','V','1','2'))))
9713 /* Windows XP with a Radeon X1600 GPU refuses to create a second
9714 * overlay surface, DDERR_NOOVERLAYHW, making the blit tests moot. */
9715 skip("Failed to create a second YV12 surface, skipping blit test.\n");
9716 IDirectDrawSurface_Release(src_surface);
9717 goto done;
9720 hr = IDirectDrawSurface_Blt(dst_surface, NULL, src_surface, NULL, DDBLT_WAIT, NULL);
9721 /* VMware rejects YV12 blits. This behavior has not been seen on real
9722 * hardware yet, so mark it broken. */
9723 ok(SUCCEEDED(hr) || broken(hr == E_NOTIMPL), "Failed to blit, hr %#x.\n", hr);
9725 if (SUCCEEDED(hr))
9727 memset(&desc, 0, sizeof(desc));
9728 desc.dwSize = sizeof(desc);
9729 hr = IDirectDrawSurface_Lock(dst_surface, NULL, &desc, DDLOCK_WAIT, NULL);
9730 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
9732 base = desc.lpSurface;
9733 ok(base[0] == 0x10, "Got unexpected Y data 0x%02x.\n", base[0]);
9734 base += desc.dwHeight * U1(desc).lPitch;
9735 todo_wine ok(base[0] == 0x20, "Got unexpected V data 0x%02x.\n", base[0]);
9736 base += desc.dwHeight / 4 * U1(desc).lPitch;
9737 todo_wine ok(base[0] == 0x30, "Got unexpected U data 0x%02x.\n", base[0]);
9739 hr = IDirectDrawSurface_Unlock(dst_surface, NULL);
9740 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
9743 IDirectDrawSurface_Release(dst_surface);
9744 IDirectDrawSurface_Release(src_surface);
9745 done:
9746 IDirectDraw2_Release(ddraw);
9747 DestroyWindow(window);
9750 static BOOL dwm_enabled(void)
9752 BOOL ret = FALSE;
9754 if (!strcmp(winetest_platform, "wine"))
9755 return FALSE;
9756 if (!pDwmIsCompositionEnabled)
9757 return FALSE;
9758 if (FAILED(pDwmIsCompositionEnabled(&ret)))
9759 return FALSE;
9760 return ret;
9763 static void test_offscreen_overlay(void)
9765 IDirectDrawSurface *overlay, *offscreen, *primary;
9766 DDSURFACEDESC surface_desc;
9767 IDirectDraw2 *ddraw;
9768 HWND window;
9769 HRESULT hr;
9770 HDC dc;
9772 window = create_window();
9773 ddraw = create_ddraw();
9774 ok(!!ddraw, "Failed to create a ddraw object.\n");
9775 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
9776 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
9778 if (!(overlay = create_overlay(ddraw, 64, 64, MAKEFOURCC('U','Y','V','Y'))))
9780 skip("Failed to create a UYVY overlay, skipping test.\n");
9781 goto done;
9784 memset(&surface_desc, 0, sizeof(surface_desc));
9785 surface_desc.dwSize = sizeof(surface_desc);
9786 surface_desc.dwFlags = DDSD_CAPS;
9787 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
9788 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &primary, NULL);
9789 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
9791 /* On Windows 7, and probably Vista, UpdateOverlay() will return
9792 * DDERR_OUTOFCAPS if the dwm is active. Calling GetDC() on the primary
9793 * surface prevents this by disabling the dwm. */
9794 hr = IDirectDrawSurface_GetDC(primary, &dc);
9795 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
9796 hr = IDirectDrawSurface_ReleaseDC(primary, dc);
9797 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
9799 /* Try to overlay a NULL surface. */
9800 hr = IDirectDrawSurface_UpdateOverlay(overlay, NULL, NULL, NULL, DDOVER_SHOW, NULL);
9801 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9802 hr = IDirectDrawSurface_UpdateOverlay(overlay, NULL, NULL, NULL, DDOVER_HIDE, NULL);
9803 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9805 /* Try to overlay an offscreen surface. */
9806 memset(&surface_desc, 0, sizeof(surface_desc));
9807 surface_desc.dwSize = sizeof(surface_desc);
9808 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
9809 surface_desc.dwWidth = 64;
9810 surface_desc.dwHeight = 64;
9811 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
9812 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
9813 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
9814 surface_desc.ddpfPixelFormat.dwFourCC = 0;
9815 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 16;
9816 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0xf800;
9817 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x07e0;
9818 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x001f;
9819 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &offscreen, NULL);
9820 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
9822 hr = IDirectDrawSurface_UpdateOverlay(overlay, NULL, offscreen, NULL, DDOVER_SHOW, NULL);
9823 ok(SUCCEEDED(hr) || broken(hr == DDERR_OUTOFCAPS && dwm_enabled())
9824 || broken(hr == E_NOTIMPL && ddraw_is_vmware(ddraw)),
9825 "Failed to update overlay, hr %#x.\n", hr);
9827 /* Try to overlay the primary with a non-overlay surface. */
9828 hr = IDirectDrawSurface_UpdateOverlay(offscreen, NULL, primary, NULL, DDOVER_SHOW, NULL);
9829 ok(hr == DDERR_NOTAOVERLAYSURFACE, "Got unexpected hr %#x.\n", hr);
9830 hr = IDirectDrawSurface_UpdateOverlay(offscreen, NULL, primary, NULL, DDOVER_HIDE, NULL);
9831 ok(hr == DDERR_NOTAOVERLAYSURFACE, "Got unexpected hr %#x.\n", hr);
9833 IDirectDrawSurface_Release(offscreen);
9834 IDirectDrawSurface_Release(primary);
9835 IDirectDrawSurface_Release(overlay);
9836 done:
9837 IDirectDraw2_Release(ddraw);
9838 DestroyWindow(window);
9841 static void test_overlay_rect(void)
9843 IDirectDrawSurface *overlay, *primary = NULL;
9844 DDSURFACEDESC surface_desc;
9845 RECT rect = {0, 0, 64, 64};
9846 IDirectDraw2 *ddraw;
9847 LONG pos_x, pos_y;
9848 HRESULT hr, hr2;
9849 HWND window;
9850 HDC dc;
9852 window = create_window();
9853 ddraw = create_ddraw();
9854 ok(!!ddraw, "Failed to create a ddraw object.\n");
9855 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
9856 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
9858 if (!(overlay = create_overlay(ddraw, 64, 64, MAKEFOURCC('U','Y','V','Y'))))
9860 skip("Failed to create a UYVY overlay, skipping test.\n");
9861 goto done;
9864 memset(&surface_desc, 0, sizeof(surface_desc));
9865 surface_desc.dwSize = sizeof(surface_desc);
9866 surface_desc.dwFlags = DDSD_CAPS;
9867 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
9868 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &primary, NULL);
9869 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
9871 /* On Windows 7, and probably Vista, UpdateOverlay() will return
9872 * DDERR_OUTOFCAPS if the dwm is active. Calling GetDC() on the primary
9873 * surface prevents this by disabling the dwm. */
9874 hr = IDirectDrawSurface_GetDC(primary, &dc);
9875 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
9876 hr = IDirectDrawSurface_ReleaseDC(primary, dc);
9877 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
9879 /* On Windows 8 and newer DWM can't be turned off, making overlays unusable. */
9880 if (dwm_enabled())
9882 win_skip("Cannot disable DWM, skipping overlay test.\n");
9883 goto done;
9886 /* The dx sdk sort of implies that rect must be set when DDOVER_SHOW is
9887 * used. This is not true in Windows Vista and earlier, but changed in
9888 * Windows 7. */
9889 hr = IDirectDrawSurface_UpdateOverlay(overlay, NULL, primary, &rect, DDOVER_SHOW, NULL);
9890 ok(SUCCEEDED(hr), "Failed to update overlay, hr %#x.\n", hr);
9891 hr = IDirectDrawSurface_UpdateOverlay(overlay, NULL, primary, NULL, DDOVER_HIDE, NULL);
9892 ok(SUCCEEDED(hr), "Failed to update overlay, hr %#x.\n", hr);
9893 hr = IDirectDrawSurface_UpdateOverlay(overlay, NULL, primary, NULL, DDOVER_SHOW, NULL);
9894 ok(hr == DD_OK || hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9896 /* Show that the overlay position is the (top, left) coordinate of the
9897 * destination rectangle. */
9898 OffsetRect(&rect, 32, 16);
9899 hr = IDirectDrawSurface_UpdateOverlay(overlay, NULL, primary, &rect, DDOVER_SHOW, NULL);
9900 ok(SUCCEEDED(hr), "Failed to update overlay, hr %#x.\n", hr);
9901 pos_x = -1; pos_y = -1;
9902 hr = IDirectDrawSurface_GetOverlayPosition(overlay, &pos_x, &pos_y);
9903 ok(SUCCEEDED(hr), "Failed to get overlay position, hr %#x.\n", hr);
9904 ok(pos_x == rect.left, "Got unexpected pos_x %d, expected %d.\n", pos_x, rect.left);
9905 ok(pos_y == rect.top, "Got unexpected pos_y %d, expected %d.\n", pos_y, rect.top);
9907 /* Passing a NULL dest rect sets the position to 0/0. Visually it can be
9908 * seen that the overlay overlays the whole primary(==screen). */
9909 hr2 = IDirectDrawSurface_UpdateOverlay(overlay, NULL, primary, NULL, 0, NULL);
9910 ok(hr2 == DD_OK || hr2 == DDERR_INVALIDPARAMS || hr2 == DDERR_OUTOFCAPS, "Got unexpected hr %#x.\n", hr2);
9911 hr = IDirectDrawSurface_GetOverlayPosition(overlay, &pos_x, &pos_y);
9912 ok(SUCCEEDED(hr), "Failed to get overlay position, hr %#x.\n", hr);
9913 if (SUCCEEDED(hr2))
9915 ok(!pos_x, "Got unexpected pos_x %d.\n", pos_x);
9916 ok(!pos_y, "Got unexpected pos_y %d.\n", pos_y);
9918 else
9920 ok(pos_x == 32, "Got unexpected pos_x %d.\n", pos_x);
9921 ok(pos_y == 16, "Got unexpected pos_y %d.\n", pos_y);
9924 /* The position cannot be retrieved when the overlay is not shown. */
9925 hr = IDirectDrawSurface_UpdateOverlay(overlay, NULL, primary, &rect, DDOVER_HIDE, NULL);
9926 ok(SUCCEEDED(hr), "Failed to update overlay, hr %#x.\n", hr);
9927 pos_x = -1; pos_y = -1;
9928 hr = IDirectDrawSurface_GetOverlayPosition(overlay, &pos_x, &pos_y);
9929 ok(hr == DDERR_OVERLAYNOTVISIBLE, "Got unexpected hr %#x.\n", hr);
9930 ok(!pos_x, "Got unexpected pos_x %d.\n", pos_x);
9931 ok(!pos_y, "Got unexpected pos_y %d.\n", pos_y);
9933 IDirectDrawSurface_Release(overlay);
9934 done:
9935 if (primary)
9936 IDirectDrawSurface_Release(primary);
9937 IDirectDraw2_Release(ddraw);
9938 DestroyWindow(window);
9941 static void test_blt(void)
9943 IDirectDrawSurface *surface, *rt;
9944 DDSURFACEDESC surface_desc;
9945 IDirect3DDevice2 *device;
9946 IDirectDraw2 *ddraw;
9947 unsigned int i;
9948 ULONG refcount;
9949 HWND window;
9950 HRESULT hr;
9952 static struct
9954 RECT src_rect;
9955 RECT dst_rect;
9956 HRESULT hr;
9958 test_data[] =
9960 {{160, 0, 640, 480}, { 0, 0, 480, 480}, DD_OK}, /* Overlapped blit. */
9961 {{160, 480, 640, 0}, { 0, 0, 480, 480}, DDERR_INVALIDRECT}, /* Overlapped blit, flipped source. */
9962 {{640, 0, 160, 480}, { 0, 0, 480, 480}, DDERR_INVALIDRECT}, /* Overlapped blit, mirrored source. */
9963 {{160, 0, 480, 480}, { 0, 0, 480, 480}, DD_OK}, /* Overlapped blit, stretched x. */
9964 {{160, 160, 640, 480}, { 0, 0, 480, 480}, DD_OK}, /* Overlapped blit, stretched y. */
9965 {{ 0, 0, 640, 480}, { 0, 0, 640, 480}, DD_OK}, /* Full surface blit. */
9966 {{ 0, 0, 640, 480}, { 0, 480, 640, 0}, DDERR_INVALIDRECT}, /* Full surface, flipped destination. */
9967 {{ 0, 0, 640, 480}, {640, 0, 0, 480}, DDERR_INVALIDRECT}, /* Full surface, mirrored destination. */
9968 {{ 0, 480, 640, 0}, { 0, 0, 640, 480}, DDERR_INVALIDRECT}, /* Full surface, flipped source. */
9969 {{640, 0, 0, 480}, { 0, 0, 640, 480}, DDERR_INVALIDRECT}, /* Full surface, mirrored source. */
9972 window = create_window();
9973 ddraw = create_ddraw();
9974 ok(!!ddraw, "Failed to create a ddraw object.\n");
9975 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
9977 skip("Failed to create a 3D device, skipping test.\n");
9978 IDirectDraw2_Release(ddraw);
9979 DestroyWindow(window);
9980 return;
9983 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
9984 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
9986 memset(&surface_desc, 0, sizeof(surface_desc));
9987 surface_desc.dwSize = sizeof(surface_desc);
9988 surface_desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
9989 surface_desc.dwWidth = 640;
9990 surface_desc.dwHeight = 480;
9991 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
9992 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9993 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9995 hr = IDirectDrawSurface_Blt(surface, NULL, surface, NULL, 0, NULL);
9996 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
9998 hr = IDirectDrawSurface_Blt(surface, NULL, rt, NULL, 0, NULL);
9999 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
10001 for (i = 0; i < ARRAY_SIZE(test_data); ++i)
10003 hr = IDirectDrawSurface_Blt(surface, &test_data[i].dst_rect,
10004 surface, &test_data[i].src_rect, DDBLT_WAIT, NULL);
10005 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
10007 hr = IDirectDrawSurface_Blt(surface, &test_data[i].dst_rect,
10008 rt, &test_data[i].src_rect, DDBLT_WAIT, NULL);
10009 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
10011 hr = IDirectDrawSurface_Blt(surface, &test_data[i].dst_rect,
10012 NULL, &test_data[i].src_rect, DDBLT_WAIT, NULL);
10013 ok(hr == DDERR_INVALIDPARAMS, "Test %u: Got unexpected hr %#x.\n", i, hr);
10015 hr = IDirectDrawSurface_Blt(surface, &test_data[i].dst_rect, NULL, NULL, DDBLT_WAIT, NULL);
10016 ok(hr == DDERR_INVALIDPARAMS, "Test %u: Got unexpected hr %#x.\n", i, hr);
10019 IDirectDrawSurface_Release(surface);
10020 IDirectDrawSurface_Release(rt);
10021 refcount = IDirect3DDevice2_Release(device);
10022 ok(!refcount, "Device has %u references left.\n", refcount);
10023 IDirectDraw2_Release(ddraw);
10024 DestroyWindow(window);
10027 static void test_blt_z_alpha(void)
10029 DWORD blt_flags[] =
10031 /* 0 */
10032 DDBLT_ALPHADEST,
10033 DDBLT_ALPHADESTCONSTOVERRIDE,
10034 DDBLT_ALPHADESTNEG,
10035 DDBLT_ALPHADESTSURFACEOVERRIDE,
10036 DDBLT_ALPHAEDGEBLEND,
10037 /* 5 */
10038 DDBLT_ALPHASRC,
10039 DDBLT_ALPHASRCCONSTOVERRIDE,
10040 DDBLT_ALPHASRCNEG,
10041 DDBLT_ALPHASRCSURFACEOVERRIDE,
10042 DDBLT_ZBUFFER,
10043 /* 10 */
10044 DDBLT_ZBUFFERDESTCONSTOVERRIDE,
10045 DDBLT_ZBUFFERDESTOVERRIDE,
10046 DDBLT_ZBUFFERSRCCONSTOVERRIDE,
10047 DDBLT_ZBUFFERSRCOVERRIDE,
10049 IDirectDrawSurface *src_surface, *dst_surface;
10050 DDSURFACEDESC surface_desc;
10051 IDirectDraw2 *ddraw;
10052 DDPIXELFORMAT pf;
10053 ULONG refcount;
10054 unsigned int i;
10055 D3DCOLOR color;
10056 HWND window;
10057 HRESULT hr;
10058 DDBLTFX fx;
10060 window = create_window();
10061 ddraw = create_ddraw();
10062 ok(!!ddraw, "Failed to create a ddraw object.\n");
10063 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
10064 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
10066 memset(&pf, 0, sizeof(pf));
10067 pf.dwSize = sizeof(pf);
10068 pf.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
10069 U1(pf).dwRGBBitCount = 32;
10070 U2(pf).dwRBitMask = 0x00ff0000;
10071 U3(pf).dwGBitMask = 0x0000ff00;
10072 U4(pf).dwBBitMask = 0x000000ff;
10073 U5(pf).dwRGBAlphaBitMask = 0xff000000;
10075 memset(&surface_desc, 0, sizeof(surface_desc));
10076 surface_desc.dwSize = sizeof(surface_desc);
10077 surface_desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
10078 surface_desc.dwWidth = 64;
10079 surface_desc.dwHeight = 64;
10080 surface_desc.ddpfPixelFormat = pf;
10081 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
10083 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
10084 ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
10085 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
10086 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
10088 memset(&fx, 0, sizeof(fx));
10089 fx.dwSize = sizeof(fx);
10090 fx.dwZBufferOpCode = D3DCMP_NEVER;
10091 fx.dwZDestConstBitDepth = 32;
10092 U1(fx).dwZDestConst = 0x11111111;
10093 fx.dwZSrcConstBitDepth = 32;
10094 U2(fx).dwZSrcConst = 0xeeeeeeee;
10095 fx.dwAlphaEdgeBlendBitDepth = 8;
10096 fx.dwAlphaEdgeBlend = 0x7f;
10097 fx.dwAlphaDestConstBitDepth = 8;
10098 U3(fx).dwAlphaDestConst = 0xdd;
10099 fx.dwAlphaSrcConstBitDepth = 8;
10100 U4(fx).dwAlphaSrcConst = 0x22;
10102 for (i = 0; i < ARRAY_SIZE(blt_flags); ++i)
10104 fx.dwFillColor = 0x3300ff00;
10105 hr = IDirectDrawSurface_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
10106 ok(SUCCEEDED(hr), "Test %u: Got unexpected hr %#x.\n", i, hr);
10108 fx.dwFillColor = 0xccff0000;
10109 hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
10110 ok(SUCCEEDED(hr), "Test %u: Got unexpected hr %#x.\n", i, hr);
10112 hr = IDirectDrawSurface_Blt(dst_surface, NULL, src_surface, NULL, blt_flags[i] | DDBLT_WAIT, &fx);
10113 ok(SUCCEEDED(hr), "Test %u: Got unexpected hr %#x.\n", i, hr);
10115 color = get_surface_color(dst_surface, 32, 32);
10116 ok(compare_color(color, 0x0000ff00, 0), "Test %u: Got unexpected color 0x%08x.\n", i, color);
10119 IDirectDrawSurface_Release(dst_surface);
10120 IDirectDrawSurface_Release(src_surface);
10121 refcount = IDirectDraw2_Release(ddraw);
10122 ok(!refcount, "DirectDraw has %u references left.\n", refcount);
10123 DestroyWindow(window);
10126 static void test_getdc(void)
10128 IDirectDrawSurface *surface, *surface2, *tmp;
10129 DDSURFACEDESC surface_desc, map_desc;
10130 DDSCAPS caps = {DDSCAPS_COMPLEX};
10131 IDirectDraw2 *ddraw;
10132 unsigned int i;
10133 HWND window;
10134 HDC dc, dc2;
10135 HRESULT hr;
10137 static const struct
10139 const char *name;
10140 DDPIXELFORMAT format;
10141 BOOL getdc_supported;
10142 HRESULT alt_result;
10144 test_data[] =
10146 {"D3DFMT_A8R8G8B8", {sizeof(test_data->format), DDPF_RGB | DDPF_ALPHAPIXELS, 0, {32},
10147 {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}}, TRUE},
10148 {"D3DFMT_X8R8G8B8", {sizeof(test_data->format), DDPF_RGB, 0, {32},
10149 {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0x00000000}}, TRUE},
10150 {"D3DFMT_R5G6B5", {sizeof(test_data->format), DDPF_RGB, 0, {16},
10151 {0x0000f800}, {0x000007e0}, {0x0000001f}, {0x00000000}}, TRUE},
10152 {"D3DFMT_X1R5G5B5", {sizeof(test_data->format), DDPF_RGB, 0, {16},
10153 {0x00007c00}, {0x000003e0}, {0x0000001f}, {0x00000000}}, TRUE},
10154 {"D3DFMT_A1R5G5B5", {sizeof(test_data->format), DDPF_RGB | DDPF_ALPHAPIXELS, 0, {16},
10155 {0x00007c00}, {0x000003e0}, {0x0000001f}, {0x00008000}}, TRUE},
10156 {"D3DFMT_A4R4G4B4", {sizeof(test_data->format), DDPF_RGB | DDPF_ALPHAPIXELS, 0, {16},
10157 {0x00000f00}, {0x000000f0}, {0x0000000f}, {0x0000f000}}, TRUE, DDERR_CANTCREATEDC /* Vista+ */},
10158 {"D3DFMT_X4R4G4B4", {sizeof(test_data->format), DDPF_RGB, 0, {16},
10159 {0x00000f00}, {0x000000f0}, {0x0000000f}, {0x00000000}}, TRUE, DDERR_CANTCREATEDC /* Vista+ */},
10160 {"D3DFMT_A2R10G10B10", {sizeof(test_data->format), DDPF_RGB | DDPF_ALPHAPIXELS, 0, {32},
10161 {0xc0000000}, {0x3ff00000}, {0x000ffc00}, {0x000003ff}}, TRUE},
10162 {"D3DFMT_A8B8G8R8", {sizeof(test_data->format), DDPF_RGB | DDPF_ALPHAPIXELS, 0, {32},
10163 {0x000000ff}, {0x0000ff00}, {0x00ff0000}, {0xff000000}}, TRUE, DDERR_CANTCREATEDC /* Vista+ */},
10164 {"D3DFMT_X8B8G8R8", {sizeof(test_data->format), DDPF_RGB, 0, {32},
10165 {0x000000ff}, {0x0000ff00}, {0x00ff0000}, {0x00000000}}, TRUE, DDERR_CANTCREATEDC /* Vista+ */},
10166 {"D3DFMT_R3G3B2", {sizeof(test_data->format), DDPF_RGB, 0, {8},
10167 {0x000000e0}, {0x0000001c}, {0x00000003}, {0x00000000}}, FALSE},
10168 /* GetDC() on a P8 surface fails unless the display mode is 8 bpp.
10169 * This is not implemented in wine yet, so disable the test for now.
10170 * Succeeding P8 GetDC() calls are tested in the ddraw:visual test.
10171 {"D3DFMT_P8", {sizeof(test_data->format), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0, {8 },
10172 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
10174 {"D3DFMT_L8", {sizeof(test_data->format), DDPF_LUMINANCE, 0, {8},
10175 {0x000000ff}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
10176 {"D3DFMT_A8L8", {sizeof(test_data->format), DDPF_ALPHAPIXELS | DDPF_LUMINANCE, 0, {16},
10177 {0x000000ff}, {0x00000000}, {0x00000000}, {0x0000ff00}}, FALSE},
10178 {"D3DFMT_DXT1", {sizeof(test_data->format), DDPF_FOURCC, MAKEFOURCC('D','X','T','1'), {0},
10179 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
10180 {"D3DFMT_DXT2", {sizeof(test_data->format), DDPF_FOURCC, MAKEFOURCC('D','X','T','2'), {0},
10181 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
10182 {"D3DFMT_DXT3", {sizeof(test_data->format), DDPF_FOURCC, MAKEFOURCC('D','X','T','3'), {0},
10183 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
10184 {"D3DFMT_DXT4", {sizeof(test_data->format), DDPF_FOURCC, MAKEFOURCC('D','X','T','4'), {0},
10185 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
10186 {"D3DFMT_DXT5", {sizeof(test_data->format), DDPF_FOURCC, MAKEFOURCC('D','X','T','5'), {0},
10187 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
10190 window = create_window();
10191 ddraw = create_ddraw();
10192 ok(!!ddraw, "Failed to create a ddraw object.\n");
10193 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
10194 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
10196 for (i = 0; i < ARRAY_SIZE(test_data); ++i)
10198 memset(&surface_desc, 0, sizeof(surface_desc));
10199 surface_desc.dwSize = sizeof(surface_desc);
10200 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
10201 surface_desc.dwWidth = 64;
10202 surface_desc.dwHeight = 64;
10203 surface_desc.ddpfPixelFormat = test_data[i].format;
10204 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
10206 if (FAILED(IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL)))
10208 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
10209 if (FAILED(hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL)))
10211 skip("Failed to create surface for format %s (hr %#x), skipping tests.\n", test_data[i].name, hr);
10212 continue;
10216 dc = (void *)0x1234;
10217 hr = IDirectDrawSurface_GetDC(surface, &dc);
10218 if (test_data[i].getdc_supported)
10219 ok(SUCCEEDED(hr) || broken(hr == test_data[i].alt_result || ddraw_is_vmware(ddraw)),
10220 "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10221 else
10222 ok(FAILED(hr), "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10224 if (SUCCEEDED(hr))
10226 unsigned int width_bytes;
10227 DIBSECTION dib;
10228 HBITMAP bitmap;
10229 DWORD type;
10230 int size;
10232 type = GetObjectType(dc);
10233 ok(type == OBJ_MEMDC, "Got unexpected object type %#x for format %s.\n", type, test_data[i].name);
10234 bitmap = GetCurrentObject(dc, OBJ_BITMAP);
10235 type = GetObjectType(bitmap);
10236 ok(type == OBJ_BITMAP, "Got unexpected object type %#x for format %s.\n", type, test_data[i].name);
10238 size = GetObjectA(bitmap, sizeof(dib), &dib);
10239 ok(size == sizeof(dib), "Got unexpected size %d for format %s.\n", size, test_data[i].name);
10240 ok(!dib.dsBm.bmType, "Got unexpected type %#x for format %s.\n",
10241 dib.dsBm.bmType, test_data[i].name);
10242 ok(dib.dsBm.bmWidth == surface_desc.dwWidth, "Got unexpected width %d for format %s.\n",
10243 dib.dsBm.bmWidth, test_data[i].name);
10244 ok(dib.dsBm.bmHeight == surface_desc.dwHeight, "Got unexpected height %d for format %s.\n",
10245 dib.dsBm.bmHeight, test_data[i].name);
10246 width_bytes = ((dib.dsBm.bmWidth * U1(test_data[i].format).dwRGBBitCount + 31) >> 3) & ~3;
10247 ok(dib.dsBm.bmWidthBytes == width_bytes, "Got unexpected width bytes %d for format %s.\n",
10248 dib.dsBm.bmWidthBytes, test_data[i].name);
10249 ok(dib.dsBm.bmPlanes == 1, "Got unexpected plane count %d for format %s.\n",
10250 dib.dsBm.bmPlanes, test_data[i].name);
10251 ok(dib.dsBm.bmBitsPixel == U1(test_data[i].format).dwRGBBitCount,
10252 "Got unexpected bit count %d for format %s.\n",
10253 dib.dsBm.bmBitsPixel, test_data[i].name);
10254 ok(!!dib.dsBm.bmBits, "Got unexpected bits %p for format %s.\n",
10255 dib.dsBm.bmBits, test_data[i].name);
10257 ok(dib.dsBmih.biSize == sizeof(dib.dsBmih), "Got unexpected size %u for format %s.\n",
10258 dib.dsBmih.biSize, test_data[i].name);
10259 ok(dib.dsBmih.biWidth == surface_desc.dwWidth, "Got unexpected width %d for format %s.\n",
10260 dib.dsBmih.biHeight, test_data[i].name);
10261 ok(dib.dsBmih.biHeight == surface_desc.dwHeight, "Got unexpected height %d for format %s.\n",
10262 dib.dsBmih.biHeight, test_data[i].name);
10263 ok(dib.dsBmih.biPlanes == 1, "Got unexpected plane count %u for format %s.\n",
10264 dib.dsBmih.biPlanes, test_data[i].name);
10265 ok(dib.dsBmih.biBitCount == U1(test_data[i].format).dwRGBBitCount,
10266 "Got unexpected bit count %u for format %s.\n",
10267 dib.dsBmih.biBitCount, test_data[i].name);
10268 ok(dib.dsBmih.biCompression == (U1(test_data[i].format).dwRGBBitCount == 16 ? BI_BITFIELDS : BI_RGB)
10269 || broken(U1(test_data[i].format).dwRGBBitCount == 32 && dib.dsBmih.biCompression == BI_BITFIELDS),
10270 "Got unexpected compression %#x for format %s.\n",
10271 dib.dsBmih.biCompression, test_data[i].name);
10272 ok(!dib.dsBmih.biSizeImage, "Got unexpected image size %u for format %s.\n",
10273 dib.dsBmih.biSizeImage, test_data[i].name);
10274 ok(!dib.dsBmih.biXPelsPerMeter, "Got unexpected horizontal resolution %d for format %s.\n",
10275 dib.dsBmih.biXPelsPerMeter, test_data[i].name);
10276 ok(!dib.dsBmih.biYPelsPerMeter, "Got unexpected vertical resolution %d for format %s.\n",
10277 dib.dsBmih.biYPelsPerMeter, test_data[i].name);
10278 ok(!dib.dsBmih.biClrUsed, "Got unexpected used colour count %u for format %s.\n",
10279 dib.dsBmih.biClrUsed, test_data[i].name);
10280 ok(!dib.dsBmih.biClrImportant, "Got unexpected important colour count %u for format %s.\n",
10281 dib.dsBmih.biClrImportant, test_data[i].name);
10283 if (dib.dsBmih.biCompression == BI_BITFIELDS)
10285 ok((dib.dsBitfields[0] == U2(test_data[i].format).dwRBitMask
10286 && dib.dsBitfields[1] == U3(test_data[i].format).dwGBitMask
10287 && dib.dsBitfields[2] == U4(test_data[i].format).dwBBitMask)
10288 || broken(!dib.dsBitfields[0] && !dib.dsBitfields[1] && !dib.dsBitfields[2]),
10289 "Got unexpected colour masks 0x%08x 0x%08x 0x%08x for format %s.\n",
10290 dib.dsBitfields[0], dib.dsBitfields[1], dib.dsBitfields[2], test_data[i].name);
10292 else
10294 ok(!dib.dsBitfields[0] && !dib.dsBitfields[1] && !dib.dsBitfields[2],
10295 "Got unexpected colour masks 0x%08x 0x%08x 0x%08x for format %s.\n",
10296 dib.dsBitfields[0], dib.dsBitfields[1], dib.dsBitfields[2], test_data[i].name);
10298 ok(!dib.dshSection, "Got unexpected section %p for format %s.\n", dib.dshSection, test_data[i].name);
10299 ok(!dib.dsOffset, "Got unexpected offset %u for format %s.\n", dib.dsOffset, test_data[i].name);
10301 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
10302 ok(hr == DD_OK, "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10304 else
10306 ok(!dc, "Got unexpected dc %p for format %s.\n", dc, test_data[i].name);
10309 IDirectDrawSurface_Release(surface);
10311 if (FAILED(hr))
10312 continue;
10314 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
10315 if (FAILED(hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL)))
10317 skip("Failed to create mip-mapped texture for format %s (hr %#x), skipping tests.\n",
10318 test_data[i].name, hr);
10319 continue;
10322 hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &tmp);
10323 ok(SUCCEEDED(hr), "Failed to get attached surface for format %s, hr %#x.\n", test_data[i].name, hr);
10324 hr = IDirectDrawSurface_GetAttachedSurface(tmp, &caps, &surface2);
10325 ok(SUCCEEDED(hr), "Failed to get attached surface for format %s, hr %#x.\n", test_data[i].name, hr);
10326 IDirectDrawSurface_Release(tmp);
10328 hr = IDirectDrawSurface_GetDC(surface, &dc);
10329 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10330 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
10331 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10332 hr = IDirectDrawSurface_GetDC(surface2, &dc);
10333 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10334 hr = IDirectDrawSurface_ReleaseDC(surface2, dc);
10335 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10337 hr = IDirectDrawSurface_GetDC(surface, &dc);
10338 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10339 dc2 = (void *)0x1234;
10340 hr = IDirectDrawSurface_GetDC(surface, &dc2);
10341 ok(hr == DDERR_DCALREADYCREATED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10342 ok(dc2 == (void *)0x1234, "Got unexpected dc %p for format %s.\n", dc, test_data[i].name);
10343 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
10344 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10345 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
10346 ok(hr == DDERR_NODC, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10348 map_desc.dwSize = sizeof(map_desc);
10349 hr = IDirectDrawSurface_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
10350 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
10351 hr = IDirectDrawSurface_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
10352 ok(hr == DDERR_SURFACEBUSY, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10353 hr = IDirectDrawSurface_Unlock(surface, NULL);
10354 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
10355 hr = IDirectDrawSurface_Unlock(surface, NULL);
10356 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10358 hr = IDirectDrawSurface_GetDC(surface, &dc);
10359 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10360 hr = IDirectDrawSurface_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
10361 ok(hr == DDERR_SURFACEBUSY, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10362 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
10363 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10365 hr = IDirectDrawSurface_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
10366 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
10367 hr = IDirectDrawSurface_GetDC(surface, &dc);
10368 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10369 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
10370 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10371 hr = IDirectDrawSurface_Unlock(surface, NULL);
10372 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
10374 hr = IDirectDrawSurface_GetDC(surface, &dc);
10375 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10376 hr = IDirectDrawSurface_GetDC(surface2, &dc2);
10377 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10378 hr = IDirectDrawSurface_ReleaseDC(surface2, dc2);
10379 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10380 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
10381 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10383 hr = IDirectDrawSurface_GetDC(surface2, &dc);
10384 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10385 hr = IDirectDrawSurface_GetDC(surface, &dc2);
10386 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10387 hr = IDirectDrawSurface_ReleaseDC(surface, dc2);
10388 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10389 hr = IDirectDrawSurface_ReleaseDC(surface2, dc);
10390 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10392 hr = IDirectDrawSurface_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
10393 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
10394 hr = IDirectDrawSurface_Lock(surface2, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
10395 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
10396 hr = IDirectDrawSurface_Unlock(surface2, NULL);
10397 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
10398 hr = IDirectDrawSurface_Unlock(surface, NULL);
10399 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
10401 hr = IDirectDrawSurface_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
10402 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
10403 hr = IDirectDrawSurface_GetDC(surface, &dc);
10404 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10405 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
10406 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10407 hr = IDirectDrawSurface_Unlock(surface, NULL);
10408 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
10410 hr = IDirectDrawSurface_Lock(surface2, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
10411 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
10412 hr = IDirectDrawSurface_GetDC(surface, &dc);
10413 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10414 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
10415 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10416 hr = IDirectDrawSurface_Unlock(surface2, NULL);
10417 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
10419 hr = IDirectDrawSurface_GetDC(surface, &dc);
10420 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10421 hr = IDirectDrawSurface_Lock(surface2, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
10422 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
10423 hr = IDirectDrawSurface_Unlock(surface2, NULL);
10424 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
10425 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
10426 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10428 hr = IDirectDrawSurface_GetDC(surface2, &dc);
10429 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10430 hr = IDirectDrawSurface_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
10431 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
10432 hr = IDirectDrawSurface_Unlock(surface, NULL);
10433 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
10434 hr = IDirectDrawSurface_ReleaseDC(surface2, dc);
10435 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10437 hr = IDirectDrawSurface_Unlock(surface, NULL);
10438 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10439 hr = IDirectDrawSurface_GetDC(surface2, &dc);
10440 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10441 hr = IDirectDrawSurface_Unlock(surface, NULL);
10442 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10443 hr = IDirectDrawSurface_ReleaseDC(surface2, dc);
10444 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10445 hr = IDirectDrawSurface_Unlock(surface, NULL);
10446 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10448 hr = IDirectDrawSurface_Unlock(surface2, NULL);
10449 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10450 hr = IDirectDrawSurface_GetDC(surface, &dc);
10451 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
10452 hr = IDirectDrawSurface_Unlock(surface2, NULL);
10453 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10454 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
10455 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
10456 hr = IDirectDrawSurface_Unlock(surface2, NULL);
10457 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
10459 IDirectDrawSurface_Release(surface2);
10460 IDirectDrawSurface_Release(surface);
10463 IDirectDraw2_Release(ddraw);
10464 DestroyWindow(window);
10467 static void test_draw_primitive(void)
10469 static WORD indices[] = {0, 1, 2, 3};
10470 static D3DVERTEX quad[] =
10472 {{-1.0f}, {-1.0f}, {0.0f}},
10473 {{-1.0f}, { 1.0f}, {0.0f}},
10474 {{ 1.0f}, {-1.0f}, {0.0f}},
10475 {{ 1.0f}, { 1.0f}, {0.0f}},
10477 IDirect3DViewport2 *viewport;
10478 IDirect3DDevice2 *device;
10479 IDirectDraw2 *ddraw;
10480 IDirect3D2 *d3d;
10481 ULONG refcount;
10482 HWND window;
10483 HRESULT hr;
10485 window = create_window();
10486 ddraw = create_ddraw();
10487 ok(!!ddraw, "Failed to create a ddraw object.\n");
10488 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
10490 skip("Failed to create a 3D device, skipping test.\n");
10491 IDirectDraw2_Release(ddraw);
10492 DestroyWindow(window);
10493 return;
10496 viewport = create_viewport(device, 0, 0, 640, 480);
10497 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
10498 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
10500 hr = IDirect3DDevice2_GetDirect3D(device, &d3d);
10501 ok(SUCCEEDED(hr), "Failed to get D3D interface, hr %#x.\n", hr);
10503 IDirect3D2_Release(d3d);
10505 hr = IDirect3DDevice2_DrawIndexedPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, NULL, 0, NULL, 0, 0);
10506 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10507 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, NULL, 0, 0);
10508 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10510 hr = IDirect3DDevice2_DrawIndexedPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, NULL, 0, indices, 4, 0);
10511 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10513 hr = IDirect3DDevice2_DrawIndexedPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, quad, 4, NULL, 0, 0);
10514 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10515 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, quad, 4, 0);
10516 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10517 hr = IDirect3DDevice2_DrawIndexedPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, quad, 4, indices, 4, 0);
10518 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10520 destroy_viewport(device, viewport);
10521 refcount = IDirect3DDevice2_Release(device);
10522 ok(!refcount, "Device has %u references left.\n", refcount);
10523 IDirectDraw2_Release(ddraw);
10524 DestroyWindow(window);
10527 static void test_edge_antialiasing_blending(void)
10529 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
10530 IDirect3DMaterial2 *green_background;
10531 IDirect3DMaterial2 *red_background;
10532 IDirectDrawSurface *offscreen, *ds;
10533 D3DDEVICEDESC hal_desc, hel_desc;
10534 IDirect3DViewport2 *viewport;
10535 DDSURFACEDESC surface_desc;
10536 IDirect3DDevice2 *device;
10537 IDirectDraw2 *ddraw;
10538 ULONG refcount;
10539 D3DCOLOR color;
10540 HWND window;
10541 HRESULT hr;
10543 static D3DMATRIX mat =
10545 1.0f, 0.0f, 0.0f, 0.0f,
10546 0.0f, 1.0f, 0.0f, 0.0f,
10547 0.0f, 0.0f, 1.0f, 0.0f,
10548 0.0f, 0.0f, 0.0f, 1.0f,
10550 static D3DLVERTEX green_quad[] =
10552 {{-1.0f}, {-1.0f}, {0.1f}, 0, {0x7f00ff00}},
10553 {{-1.0f}, { 1.0f}, {0.1f}, 0, {0x7f00ff00}},
10554 {{ 1.0f}, {-1.0f}, {0.1f}, 0, {0x7f00ff00}},
10555 {{ 1.0f}, { 1.0f}, {0.1f}, 0, {0x7f00ff00}},
10557 static D3DLVERTEX red_quad[] =
10559 {{-1.0f}, {-1.0f}, {0.1f}, 0, {0xccff0000}},
10560 {{-1.0f}, { 1.0f}, {0.1f}, 0, {0xccff0000}},
10561 {{ 1.0f}, {-1.0f}, {0.1f}, 0, {0xccff0000}},
10562 {{ 1.0f}, { 1.0f}, {0.1f}, 0, {0xccff0000}},
10565 window = create_window();
10566 ddraw = create_ddraw();
10567 ok(!!ddraw, "Failed to create a ddraw object.\n");
10568 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
10570 skip("Failed to create a 3D device.\n");
10571 DestroyWindow(window);
10572 return;
10575 memset(&hal_desc, 0, sizeof(hal_desc));
10576 hal_desc.dwSize = sizeof(hal_desc);
10577 memset(&hel_desc, 0, sizeof(hel_desc));
10578 hel_desc.dwSize = sizeof(hel_desc);
10579 hr = IDirect3DDevice2_GetCaps(device, &hal_desc, &hel_desc);
10580 ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
10581 trace("HAL line edge antialiasing support: %#x.\n",
10582 hal_desc.dpcLineCaps.dwRasterCaps & D3DPRASTERCAPS_ANTIALIASEDGES);
10583 trace("HAL triangle edge antialiasing support: %#x.\n",
10584 hal_desc.dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_ANTIALIASEDGES);
10585 trace("HEL line edge antialiasing support: %#x.\n",
10586 hel_desc.dpcLineCaps.dwRasterCaps & D3DPRASTERCAPS_ANTIALIASEDGES);
10587 trace("HEL triangle edge antialiasing support: %#x.\n",
10588 hel_desc.dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_ANTIALIASEDGES);
10590 memset(&surface_desc, 0, sizeof(surface_desc));
10591 surface_desc.dwSize = sizeof(surface_desc);
10592 surface_desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
10593 surface_desc.dwWidth = 640;
10594 surface_desc.dwHeight = 480;
10595 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
10596 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
10597 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
10598 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
10599 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
10600 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
10601 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
10602 U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
10603 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &offscreen, NULL);
10604 ok(hr == D3D_OK, "Creating the offscreen render target failed, hr %#x.\n", hr);
10606 ds = get_depth_stencil(device);
10607 hr = IDirectDrawSurface_AddAttachedSurface(offscreen, ds);
10608 todo_wine ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
10609 IDirectDrawSurface_Release(ds);
10611 hr = IDirect3DDevice2_SetRenderTarget(device, offscreen, 0);
10612 ok(SUCCEEDED(hr), "Failed to set render target, hr %#x.\n", hr);
10614 red_background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 0.8f);
10615 green_background = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 0.5f);
10617 viewport = create_viewport(device, 0, 0, 640, 480);
10618 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
10619 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
10621 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &mat);
10622 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
10623 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &mat);
10624 ok(SUCCEEDED(hr), "Failed to set view transform, hr %#x.\n", hr);
10625 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &mat);
10626 ok(SUCCEEDED(hr), "Failed to set projection transform, hr %#x.\n", hr);
10627 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
10628 ok(SUCCEEDED(hr), "Failed to disable clipping, hr %#x.\n", hr);
10629 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
10630 ok(SUCCEEDED(hr), "Failed to disable Z test, hr %#x.\n", hr);
10631 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
10632 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
10633 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_STENCILENABLE, FALSE);
10634 ok(SUCCEEDED(hr), "Failed to disable stencil test, hr %#x.\n", hr);
10635 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
10636 ok(SUCCEEDED(hr), "Failed to disable culling, hr %#x.\n", hr);
10637 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
10638 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
10640 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
10641 ok(SUCCEEDED(hr), "Failed to enable blending, hr %#x.\n", hr);
10642 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
10643 ok(SUCCEEDED(hr), "Failed to set src blend, hr %#x.\n", hr);
10644 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_DESTALPHA);
10645 ok(SUCCEEDED(hr), "Failed to set dest blend, hr %#x.\n", hr);
10647 viewport_set_background(device, viewport, red_background);
10648 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
10649 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
10650 hr = IDirect3DDevice2_BeginScene(device);
10651 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
10652 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, green_quad, 4, 0);
10653 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10654 hr = IDirect3DDevice2_EndScene(device);
10655 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
10656 color = get_surface_color(offscreen, 320, 240);
10657 ok(compare_color(color, 0x00cc7f00, 1), "Got unexpected color 0x%08x.\n", color);
10659 viewport_set_background(device, viewport, green_background);
10660 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
10661 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
10662 hr = IDirect3DDevice2_BeginScene(device);
10663 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
10664 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, red_quad, 4, 0);
10665 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10666 hr = IDirect3DDevice2_EndScene(device);
10667 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
10668 color = get_surface_color(offscreen, 320, 240);
10669 ok(compare_color(color, 0x00cc7f00, 1), "Got unexpected color 0x%08x.\n", color);
10671 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
10672 ok(SUCCEEDED(hr), "Failed to disable blending, hr %#x.\n", hr);
10674 viewport_set_background(device, viewport, red_background);
10675 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
10676 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
10677 hr = IDirect3DDevice2_BeginScene(device);
10678 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
10679 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, green_quad, 4, 0);
10680 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10681 hr = IDirect3DDevice2_EndScene(device);
10682 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
10683 color = get_surface_color(offscreen, 320, 240);
10684 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
10686 viewport_set_background(device, viewport, green_background);
10687 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
10688 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
10689 hr = IDirect3DDevice2_BeginScene(device);
10690 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
10691 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, red_quad, 4, 0);
10692 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10693 hr = IDirect3DDevice2_EndScene(device);
10694 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
10695 color = get_surface_color(offscreen, 320, 240);
10696 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
10698 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_EDGEANTIALIAS, TRUE);
10699 ok(SUCCEEDED(hr), "Failed to enable edge antialiasing, hr %#x.\n", hr);
10701 viewport_set_background(device, viewport, red_background);
10702 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
10703 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
10704 hr = IDirect3DDevice2_BeginScene(device);
10705 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
10706 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, green_quad, 4, 0);
10707 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10708 hr = IDirect3DDevice2_EndScene(device);
10709 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
10710 color = get_surface_color(offscreen, 320, 240);
10711 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
10713 viewport_set_background(device, viewport, green_background);
10714 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
10715 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
10716 hr = IDirect3DDevice2_BeginScene(device);
10717 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
10718 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, red_quad, 4, 0);
10719 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10720 hr = IDirect3DDevice2_EndScene(device);
10721 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
10722 color = get_surface_color(offscreen, 320, 240);
10723 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
10725 IDirectDrawSurface_Release(offscreen);
10726 destroy_viewport(device, viewport);
10727 destroy_material(red_background);
10728 destroy_material(green_background);
10729 refcount = IDirect3DDevice2_Release(device);
10730 ok(!refcount, "Device has %u references left.\n", refcount);
10731 IDirectDraw2_Release(ddraw);
10732 DestroyWindow(window);
10735 /* TransformVertices always writes 32 bytes regardless of the input / output stride.
10736 * The stride is honored for navigating to the next vertex. 3 floats input position
10737 * are read, and 16 bytes extra vertex data are copied around. */
10738 struct transform_input
10740 float x, y, z, unused1; /* Position data, transformed. */
10741 DWORD v1, v2, v3, v4; /* Extra data, e.g. color and texture coords, copied. */
10742 DWORD unused2;
10745 struct transform_output
10747 float x, y, z, w;
10748 DWORD v1, v2, v3, v4;
10749 DWORD unused3, unused4;
10752 static void test_transform_vertices(void)
10754 IDirect3DDevice2 *device;
10755 IDirectDrawSurface *rt;
10756 IDirectDraw2 *ddraw;
10757 ULONG refcount;
10758 HWND window;
10759 HRESULT hr;
10760 D3DCOLOR color;
10761 IDirect3DViewport2 *viewport;
10762 IDirect3DMaterial2 *background;
10763 D3DMATERIAL mat;
10764 static struct transform_input position_tests[] =
10766 { 0.0f, 0.0f, 0.0f, 0.0f, 1, 2, 3, 4, 5},
10767 { 1.0f, 1.0f, 1.0f, 8.0f, 6, 7, 8, 9, 10},
10768 {-1.0f, -1.0f, -1.0f, 4.0f, 11, 12, 13, 14, 15},
10769 { 0.5f, 0.5f, 0.5f, 2.0f, 16, 17, 18, 19, 20},
10770 {-0.5f, -0.5f, -0.5f, 1.0f, ~1U, ~2U, ~3U, ~4U, ~5U},
10771 {-0.5f, -0.5f, 0.0f, 0.0f, ~6U, ~7U, ~8U, ~9U, ~0U},
10773 static struct transform_input cliptest[] =
10775 { 25.59f, 25.59f, 1.0f, 0.0f, 1, 2, 3, 4, 5},
10776 { 25.61f, 25.61f, 1.01f, 0.0f, 1, 2, 3, 4, 5},
10777 {-25.59f, -25.59f, 0.0f, 0.0f, 1, 2, 3, 4, 5},
10778 {-25.61f, -25.61f, -0.01f, 0.0f, 1, 2, 3, 4, 5},
10780 static struct transform_input offscreentest[] =
10782 {128.1f, 0.0f, 0.0f, 0.0f, 1, 2, 3, 4, 5},
10784 struct transform_output out[ARRAY_SIZE(position_tests)];
10785 D3DHVERTEX out_h[ARRAY_SIZE(position_tests)];
10786 D3DTRANSFORMDATA transformdata;
10787 static const D3DVIEWPORT vp_template =
10789 sizeof(vp_template), 0, 0, 256, 256, 5.0f, 5.0f, 256.0f, 256.0f, -25.0f, 60.0f
10791 D3DVIEWPORT vp_data =
10793 sizeof(vp_data), 0, 0, 256, 256, 1.0f, 1.0f, 256.0f, 256.0f, 0.0f, 1.0f
10795 D3DVIEWPORT2 vp2_data;
10796 unsigned int i;
10797 DWORD offscreen;
10798 static D3DMATRIX mat_scale =
10800 2.0f, 0.0f, 0.0f, 0.0f,
10801 0.0f, 2.0f, 0.0f, 0.0f,
10802 0.0f, 0.0f, 2.0f, 0.0f,
10803 0.0f, 0.0f, 0.0f, 1.0f,
10805 mat_translate1 =
10807 1.0f, 0.0f, 0.0f, 0.0f,
10808 0.0f, 1.0f, 0.0f, 0.0f,
10809 0.0f, 0.0f, 1.0f, 0.0f,
10810 1.0f, 0.0f, 0.0f, 1.0f,
10812 mat_translate2 =
10814 1.0f, 0.0f, 0.0f, 0.0f,
10815 0.0f, 1.0f, 0.0f, 0.0f,
10816 0.0f, 0.0f, 1.0f, 0.0f,
10817 0.0f, 1.0f, 0.0f, 1.0f,
10819 mat_transform3 =
10821 1.0f, 0.0f, 0.0f, 0.0f,
10822 0.0f, 1.0f, 0.0f, 0.0f,
10823 0.0f, 0.0f, 1.0f, 0.0f,
10824 0.0f, 19.2f, 0.0f, 2.0f,
10826 mat_identity =
10828 1.0f, 0.0f, 0.0f, 0.0f,
10829 0.0f, 1.0f, 0.0f, 0.0f,
10830 0.0f, 0.0f, 1.0f, 0.0f,
10831 0.0f, 0.0f, 0.0f, 1.0f,
10833 static D3DLVERTEX quad[] =
10835 {{-0.75f},{-0.5f }, {0.0f}, 0, {0xffff0000}},
10836 {{-0.75f},{ 0.25f}, {0.0f}, 0, {0xffff0000}},
10837 {{ 0.5f}, {-0.5f }, {0.0f}, 0, {0xffff0000}},
10838 {{ 0.5f}, { 0.25f}, {0.0f}, 0, {0xffff0000}},
10840 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
10843 for (i = 0; i < ARRAY_SIZE(out); ++i)
10845 out[i].unused3 = 0xdeadbeef;
10846 out[i].unused4 = 0xcafecafe;
10849 window = create_window();
10850 ddraw = create_ddraw();
10851 ok(!!ddraw, "Failed to create a ddraw object.\n");
10852 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
10854 skip("Failed to create a 3D device, skipping test.\n");
10855 IDirectDraw2_Release(ddraw);
10856 DestroyWindow(window);
10857 return;
10859 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
10860 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
10862 viewport = create_viewport(device, 0, 0, 256, 256);
10863 hr = IDirect3DViewport2_SetViewport(viewport, &vp_data);
10864 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
10866 memset(&transformdata, 0, sizeof(transformdata));
10867 transformdata.dwSize = sizeof(transformdata);
10868 transformdata.lpIn = position_tests;
10869 transformdata.dwInSize = sizeof(position_tests[0]);
10870 transformdata.lpOut = out;
10871 transformdata.dwOutSize = sizeof(out[0]);
10872 transformdata.lpHOut = NULL;
10874 hr = IDirect3DViewport2_TransformVertices(viewport, ARRAY_SIZE(position_tests),
10875 &transformdata, D3DTRANSFORM_UNCLIPPED, &offscreen);
10876 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
10877 ok(!offscreen, "Offscreen is %x.\n", offscreen);
10879 for (i = 0; i < ARRAY_SIZE(position_tests); ++i)
10881 static const struct vec4 cmp[] =
10883 {128.0f, 128.0f, 0.0f, 1.0f}, {129.0f, 127.0f, 1.0f, 1.0f}, {127.0f, 129.0f, -1.0f, 1.0f},
10884 {128.5f, 127.5f, 0.5f, 1.0f}, {127.5f, 128.5f, -0.5f, 1.0f}, {127.5f, 128.5f, 0.0f, 1.0f}
10887 ok(compare_vec4(&cmp[i], out[i].x, out[i].y, out[i].z, out[i].w, 4096),
10888 "Vertex %u differs. Got %f %f %f %f.\n", i,
10889 out[i].x, out[i].y, out[i].z, out[i].w);
10890 ok(out[i].v1 == position_tests[i].v1 && out[i].v2 == position_tests[i].v2
10891 && out[i].v3 == position_tests[i].v3 && out[i].v4 == position_tests[i].v4,
10892 "Vertex %u payload is %u %u %u %u.\n", i, out[i].v1, out[i].v2, out[i].v3, out[i].v4);
10893 ok(out[i].unused3 == 0xdeadbeef && out[i].unused4 == 0xcafecafe,
10894 "Vertex %u unused data is %#x, %#x.\n", i, out[i].unused3, out[i].unused4);
10897 vp_data = vp_template;
10898 hr = IDirect3DViewport2_SetViewport(viewport, &vp_data);
10899 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
10900 offscreen = 0xdeadbeef;
10901 hr = IDirect3DViewport2_TransformVertices(viewport, ARRAY_SIZE(position_tests),
10902 &transformdata, D3DTRANSFORM_UNCLIPPED, &offscreen);
10903 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
10904 ok(!offscreen, "Offscreen is %x.\n", offscreen);
10906 for (i = 0; i < ARRAY_SIZE(position_tests); ++i)
10908 static const struct vec4 cmp[] =
10910 {128.0f, 128.0f, 0.0f, 1.0f}, {133.0f, 123.0f, 1.0f, 1.0f}, {123.0f, 133.0f, -1.0f, 1.0f},
10911 {130.5f, 125.5f, 0.5f, 1.0f}, {125.5f, 130.5f, -0.5f, 1.0f}, {125.5f, 130.5f, 0.0f, 1.0f}
10913 ok(compare_vec4(&cmp[i], out[i].x, out[i].y, out[i].z, out[i].w, 4096),
10914 "Vertex %u differs. Got %f %f %f %f.\n", i,
10915 out[i].x, out[i].y, out[i].z, out[i].w);
10918 vp_data.dwX = 10;
10919 vp_data.dwY = 20;
10920 hr = IDirect3DViewport2_SetViewport(viewport, &vp_data);
10921 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
10922 offscreen = 0xdeadbeef;
10923 hr = IDirect3DViewport2_TransformVertices(viewport, ARRAY_SIZE(position_tests),
10924 &transformdata, D3DTRANSFORM_UNCLIPPED, &offscreen);
10925 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
10926 ok(!offscreen, "Offscreen is %x.\n", offscreen);
10927 for (i = 0; i < ARRAY_SIZE(position_tests); ++i)
10929 static const struct vec4 cmp[] =
10931 {138.0f, 148.0f, 0.0f, 1.0f}, {143.0f, 143.0f, 1.0f, 1.0f}, {133.0f, 153.0f, -1.0f, 1.0f},
10932 {140.5f, 145.5f, 0.5f, 1.0f}, {135.5f, 150.5f, -0.5f, 1.0f}, {135.5f, 150.5f, 0.0f, 1.0f}
10934 ok(compare_vec4(&cmp[i], out[i].x, out[i].y, out[i].z, out[i].w, 4096),
10935 "Vertex %u differs. Got %f %f %f %f.\n", i,
10936 out[i].x, out[i].y, out[i].z, out[i].w);
10939 transformdata.lpHOut = out_h;
10940 offscreen = 0xdeadbeef;
10941 hr = IDirect3DViewport2_TransformVertices(viewport, ARRAY_SIZE(position_tests),
10942 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
10943 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
10944 ok(!offscreen, "Offscreen is %x.\n", offscreen);
10945 for (i = 0; i < ARRAY_SIZE(position_tests); ++i)
10947 static const D3DHVERTEX cmp_h[] =
10949 {0, { 0.0f}, { 0.0f}, { 0.0f}}, {0, { 1.0f}, { 1.0f}, {1.0f}},
10950 {D3DCLIP_FRONT, {-1.0f}, {-1.0f}, {-1.0f}}, {0, { 0.5f}, { 0.5f}, {0.5f}},
10951 {D3DCLIP_FRONT, {-0.5f}, {-0.5f}, {-0.5f}}, {0, {-0.5f}, {-0.5f}, {0.0f}}
10953 ok(compare_float(U1(cmp_h[i]).hx, U1(out_h[i]).hx, 4096)
10954 && compare_float(U2(cmp_h[i]).hy, U2(out_h[i]).hy, 4096)
10955 && compare_float(U3(cmp_h[i]).hz, U3(out_h[i]).hz, 4096)
10956 && cmp_h[i].dwFlags == out_h[i].dwFlags,
10957 "HVertex %u differs. Got %#x %f %f %f.\n", i,
10958 out_h[i].dwFlags, U1(out_h[i]).hx, U2(out_h[i]).hy, U3(out_h[i]).hz);
10960 /* No scheme has been found behind those return values. It seems to be
10961 * whatever data windows has when throwing the vertex away. Modify the
10962 * input test vertices to test this more. Depending on the input data
10963 * it can happen that the z coord gets written into y, or similar things. */
10964 if (0)
10966 static const struct vec4 cmp[] =
10968 {138.0f, 148.0f, 0.0f, 1.0f}, {143.0f, 143.0f, 1.0f, 1.0f}, { -1.0f, -1.0f, 0.5f, 1.0f},
10969 {140.5f, 145.5f, 0.5f, 1.0f}, { -0.5f, -0.5f, -0.5f, 1.0f}, {135.5f, 150.5f, 0.0f, 1.0f}
10971 ok(compare_vec4(&cmp[i], out[i].x, out[i].y, out[i].z, out[i].w, 4096),
10972 "Vertex %u differs. Got %f %f %f %f.\n", i,
10973 out[i].x, out[i].y, out[i].z, out[i].w);
10977 transformdata.lpIn = cliptest;
10978 transformdata.dwInSize = sizeof(cliptest[0]);
10979 offscreen = 0xdeadbeef;
10980 hr = IDirect3DViewport2_TransformVertices(viewport, ARRAY_SIZE(cliptest),
10981 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
10982 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
10983 ok(!offscreen, "Offscreen is %x.\n", offscreen);
10984 for (i = 0; i < ARRAY_SIZE(cliptest); ++i)
10986 static const DWORD flags[] =
10989 D3DCLIP_RIGHT | D3DCLIP_BACK | D3DCLIP_TOP,
10991 D3DCLIP_LEFT | D3DCLIP_BOTTOM | D3DCLIP_FRONT,
10993 ok(flags[i] == out_h[i].dwFlags, "Cliptest %u returned %#x.\n", i, out_h[i].dwFlags);
10996 vp_data = vp_template;
10997 vp_data.dwWidth = 10;
10998 vp_data.dwHeight = 480;
10999 hr = IDirect3DViewport2_SetViewport(viewport, &vp_data);
11000 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
11001 offscreen = 0xdeadbeef;
11002 hr = IDirect3DViewport2_TransformVertices(viewport, ARRAY_SIZE(cliptest),
11003 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11004 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11005 ok(!offscreen, "Offscreen is %x.\n", offscreen);
11006 for (i = 0; i < ARRAY_SIZE(cliptest); ++i)
11008 static const DWORD flags[] =
11010 D3DCLIP_RIGHT,
11011 D3DCLIP_RIGHT | D3DCLIP_BACK,
11012 D3DCLIP_LEFT,
11013 D3DCLIP_LEFT | D3DCLIP_FRONT,
11015 ok(flags[i] == out_h[i].dwFlags, "Cliptest %u returned %#x.\n", i, out_h[i].dwFlags);
11018 vp_data = vp_template;
11019 vp_data.dwWidth = 256;
11020 vp_data.dwHeight = 256;
11021 vp_data.dvScaleX = 1;
11022 vp_data.dvScaleY = 1;
11023 hr = IDirect3DViewport2_SetViewport(viewport, &vp_data);
11024 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
11025 hr = IDirect3DViewport2_TransformVertices(viewport, ARRAY_SIZE(cliptest),
11026 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11027 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11028 ok(!offscreen, "Offscreen is %x.\n", offscreen);
11029 for (i = 0; i < ARRAY_SIZE(cliptest); ++i)
11031 static const DWORD flags[] =
11034 D3DCLIP_BACK,
11036 D3DCLIP_FRONT,
11038 ok(flags[i] == out_h[i].dwFlags, "Cliptest %u returned %#x.\n", i, out_h[i].dwFlags);
11041 /* Finally try to figure out how the DWORD dwOffscreen works.
11042 * It is a logical AND of the vertices' dwFlags members. */
11043 vp_data = vp_template;
11044 vp_data.dwWidth = 5;
11045 vp_data.dwHeight = 5;
11046 vp_data.dvScaleX = 10000.0f;
11047 vp_data.dvScaleY = 10000.0f;
11048 hr = IDirect3DViewport2_SetViewport(viewport, &vp_data);
11049 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
11050 transformdata.lpIn = cliptest;
11051 offscreen = 0xdeadbeef;
11052 hr = IDirect3DViewport2_TransformVertices(viewport, 1,
11053 &transformdata, D3DTRANSFORM_UNCLIPPED, &offscreen);
11054 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11055 ok(!offscreen, "Offscreen is %x.\n", offscreen);
11057 offscreen = 0xdeadbeef;
11058 hr = IDirect3DViewport2_TransformVertices(viewport, 1,
11059 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11060 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11061 ok(offscreen == (D3DCLIP_RIGHT | D3DCLIP_TOP), "Offscreen is %x.\n", offscreen);
11062 offscreen = 0xdeadbeef;
11063 hr = IDirect3DViewport2_TransformVertices(viewport, 2,
11064 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11065 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11066 ok(offscreen == (D3DCLIP_RIGHT | D3DCLIP_TOP), "Offscreen is %x.\n", offscreen);
11067 hr = IDirect3DViewport2_TransformVertices(viewport, 3,
11068 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11069 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11070 ok(!offscreen, "Offscreen is %x.\n", offscreen);
11072 transformdata.lpIn = cliptest + 1;
11073 hr = IDirect3DViewport2_TransformVertices(viewport, 1,
11074 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11075 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11076 ok(offscreen == (D3DCLIP_BACK | D3DCLIP_RIGHT | D3DCLIP_TOP), "Offscreen is %x.\n", offscreen);
11078 transformdata.lpIn = cliptest + 2;
11079 hr = IDirect3DViewport2_TransformVertices(viewport, 1,
11080 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11081 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11082 ok(offscreen == (D3DCLIP_BOTTOM | D3DCLIP_LEFT), "Offscreen is %x.\n", offscreen);
11083 offscreen = 0xdeadbeef;
11084 hr = IDirect3DViewport2_TransformVertices(viewport, 2,
11085 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11086 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11087 ok(offscreen == (D3DCLIP_BOTTOM | D3DCLIP_LEFT), "Offscreen is %x.\n", offscreen);
11089 transformdata.lpIn = cliptest + 3;
11090 hr = IDirect3DViewport2_TransformVertices(viewport, 1,
11091 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11092 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11093 ok(offscreen == (D3DCLIP_FRONT | D3DCLIP_BOTTOM | D3DCLIP_LEFT), "Offscreen is %x.\n", offscreen);
11095 transformdata.lpIn = offscreentest;
11096 transformdata.dwInSize = sizeof(offscreentest[0]);
11097 vp_data = vp_template;
11098 vp_data.dwWidth = 257;
11099 vp_data.dwHeight = 257;
11100 vp_data.dvScaleX = 1.0f;
11101 vp_data.dvScaleY = 1.0f;
11102 hr = IDirect3DViewport2_SetViewport(viewport, &vp_data);
11103 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
11104 offscreen = 0xdeadbeef;
11105 hr = IDirect3DViewport2_TransformVertices(viewport, 1,
11106 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11107 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11108 ok(!offscreen, "Offscreen is %x.\n", offscreen);
11110 vp_data.dwWidth = 256;
11111 vp_data.dwHeight = 256;
11112 hr = IDirect3DViewport2_SetViewport(viewport, &vp_data);
11113 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
11114 hr = IDirect3DViewport2_TransformVertices(viewport, 1,
11115 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11116 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11117 ok(offscreen == D3DCLIP_RIGHT, "Offscreen is %x.\n", offscreen);
11119 /* Test the effect of Matrices.
11121 * Basically the x coordinate ends up as ((x + 1) * 2 + 0) * 5 and
11122 * y as ((y + 0) * 2 + 1) * 5. The 5 comes from dvScaleX/Y, 2 from
11123 * the view matrix and the +1's from the world and projection matrix. */
11124 vp_data.dwX = 0;
11125 vp_data.dwY = 0;
11126 vp_data.dwWidth = 256;
11127 vp_data.dwHeight = 256;
11128 vp_data.dvScaleX = 5.0f;
11129 vp_data.dvScaleY = 5.0f;
11130 vp_data.dvMinZ = 0.0f;
11131 vp_data.dvMaxZ = 1.0f;
11132 hr = IDirect3DViewport2_SetViewport(viewport, &vp_data);
11133 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
11135 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &mat_translate1);
11136 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
11137 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &mat_scale);
11138 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
11139 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &mat_translate2);
11140 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
11142 transformdata.lpIn = position_tests;
11143 transformdata.dwInSize = sizeof(position_tests[0]);
11144 hr = IDirect3DViewport2_TransformVertices(viewport, ARRAY_SIZE(position_tests),
11145 &transformdata, D3DTRANSFORM_UNCLIPPED, &offscreen);
11146 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11148 for (i = 0; i < ARRAY_SIZE(position_tests); ++i)
11150 static const struct vec4 cmp[] =
11152 {138.0f, 123.0f, 0.0f, 1.0f}, {148.0f, 113.0f, 2.0f, 1.0f}, {128.0f, 133.0f, -2.0f, 1.0f},
11153 {143.0f, 118.0f, 1.0f, 1.0f}, {133.0f, 128.0f, -1.0f, 1.0f}, {133.0f, 128.0f, 0.0f, 1.0f}
11156 ok(compare_vec4(&cmp[i], out[i].x, out[i].y, out[i].z, out[i].w, 4096),
11157 "Vertex %u differs. Got %f %f %f %f.\n", i,
11158 out[i].x, out[i].y, out[i].z, out[i].w);
11161 /* Invalid flags. */
11162 offscreen = 0xdeadbeef;
11163 hr = IDirect3DViewport2_TransformVertices(viewport, ARRAY_SIZE(position_tests),
11164 &transformdata, 0, &offscreen);
11165 ok(hr == DDERR_INVALIDPARAMS, "TransformVertices returned %#x.\n", hr);
11166 ok(offscreen == 0xdeadbeef, "Offscreen is %x.\n", offscreen);
11168 /* NULL transform data. */
11169 hr = IDirect3DViewport2_TransformVertices(viewport, 1,
11170 NULL, D3DTRANSFORM_UNCLIPPED, &offscreen);
11171 ok(hr == DDERR_INVALIDPARAMS, "TransformVertices returned %#x.\n", hr);
11172 ok(offscreen == 0xdeadbeef, "Offscreen is %x.\n", offscreen);
11173 hr = IDirect3DViewport2_TransformVertices(viewport, 0,
11174 NULL, D3DTRANSFORM_UNCLIPPED, &offscreen);
11175 ok(hr == DDERR_INVALIDPARAMS, "TransformVertices returned %#x.\n", hr);
11176 ok(offscreen == 0xdeadbeef, "Offscreen is %x.\n", offscreen);
11178 /* NULL transform data and NULL dwOffscreen.
11180 * Valid transform data + NULL dwOffscreen -> crash. */
11181 hr = IDirect3DViewport2_TransformVertices(viewport, 1,
11182 NULL, D3DTRANSFORM_UNCLIPPED, NULL);
11183 ok(hr == DDERR_INVALIDPARAMS, "TransformVertices returned %#x.\n", hr);
11185 /* No vertices. */
11186 hr = IDirect3DViewport2_TransformVertices(viewport, 0,
11187 &transformdata, D3DTRANSFORM_UNCLIPPED, &offscreen);
11188 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11189 ok(!offscreen, "Offscreen is %x.\n", offscreen);
11190 hr = IDirect3DViewport2_TransformVertices(viewport, 0,
11191 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11192 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11193 ok(offscreen == ~0U, "Offscreen is %x.\n", offscreen);
11195 /* Invalid sizes. */
11196 offscreen = 0xdeadbeef;
11197 transformdata.dwSize = sizeof(transformdata) - 1;
11198 hr = IDirect3DViewport2_TransformVertices(viewport, 1,
11199 &transformdata, D3DTRANSFORM_UNCLIPPED, &offscreen);
11200 ok(hr == DDERR_INVALIDPARAMS, "TransformVertices returned %#x.\n", hr);
11201 ok(offscreen == 0xdeadbeef, "Offscreen is %x.\n", offscreen);
11202 transformdata.dwSize = sizeof(transformdata) + 1;
11203 hr = IDirect3DViewport2_TransformVertices(viewport, 1,
11204 &transformdata, D3DTRANSFORM_UNCLIPPED, &offscreen);
11205 ok(hr == DDERR_INVALIDPARAMS, "TransformVertices returned %#x.\n", hr);
11206 ok(offscreen == 0xdeadbeef, "Offscreen is %x.\n", offscreen);
11208 /* NULL lpIn or lpOut -> crash, except when transforming 0 vertices. */
11209 transformdata.dwSize = sizeof(transformdata);
11210 transformdata.lpIn = NULL;
11211 transformdata.lpOut = NULL;
11212 offscreen = 0xdeadbeef;
11213 hr = IDirect3DViewport2_TransformVertices(viewport, 0,
11214 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11215 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11216 ok(offscreen == ~0U, "Offscreen is %x.\n", offscreen);
11218 /* Test how vertices are transformed during draws. */
11219 vp_data.dwX = 20;
11220 vp_data.dwY = 20;
11221 vp_data.dwWidth = 200;
11222 vp_data.dwHeight = 400;
11223 vp_data.dvScaleX = 20.0f;
11224 vp_data.dvScaleY = 50.0f;
11225 vp_data.dvMinZ = 0.0f;
11226 vp_data.dvMaxZ = 1.0f;
11227 hr = IDirect3DViewport2_SetViewport(viewport, &vp_data);
11228 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
11229 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
11230 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
11232 ok(SUCCEEDED(hr), "Failed to clear the render target, hr %#x.\n", hr);
11233 background = create_diffuse_material(device, 0.0f, 0.0f, 1.0f, 0.0f);
11234 viewport_set_background(device, viewport, background);
11235 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
11236 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
11238 hr = IDirect3DDevice2_BeginScene(device);
11239 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
11240 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, quad, 4, 0);
11241 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11242 hr = IDirect3DDevice2_EndScene(device);
11243 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
11245 color = get_surface_color(rt, 128, 143);
11246 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
11247 color = get_surface_color(rt, 132, 143);
11248 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
11249 color = get_surface_color(rt, 128, 147);
11250 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
11251 color = get_surface_color(rt, 132, 147);
11252 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
11254 color = get_surface_color(rt, 177, 217);
11255 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
11256 color = get_surface_color(rt, 181, 217);
11257 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
11258 color = get_surface_color(rt, 177, 221);
11259 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
11260 color = get_surface_color(rt, 181, 221);
11261 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
11263 /* Test D3DVIEWPORT2 behavior. */
11264 vp2_data.dwSize = sizeof(vp2_data);
11265 vp2_data.dwX = 20;
11266 vp2_data.dwY = 20;
11267 vp2_data.dwWidth = 200;
11268 vp2_data.dwHeight = 400;
11269 vp2_data.dvClipX = -0.5f;
11270 vp2_data.dvClipY = 4.0f;
11271 vp2_data.dvClipWidth = 5.0f;
11272 vp2_data.dvClipHeight = 10.0f;
11273 vp2_data.dvMinZ = 0.0f;
11274 vp2_data.dvMaxZ = 2.0f;
11275 hr = IDirect3DViewport2_SetViewport2(viewport, &vp2_data);
11276 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
11277 transformdata.lpIn = position_tests;
11278 transformdata.lpOut = out;
11279 hr = IDirect3DViewport2_TransformVertices(viewport, ARRAY_SIZE(position_tests),
11280 &transformdata, D3DTRANSFORM_UNCLIPPED, &offscreen);
11281 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11282 for (i = 0; i < ARRAY_SIZE(position_tests); ++i)
11284 static const struct vec4 cmp[] =
11286 {120.0f, 140.0f, 0.0f, 1.0f}, {200.0f, 60.0f, 1.0f, 1.0f}, {40.0f, 220.0f, -1.0f, 1.0f},
11287 {160.0f, 100.0f, 0.5f, 1.0f}, { 80.0f, 180.0f, -0.5f, 1.0f}, {80.0f, 180.0f, 0.0f, 1.0f}
11290 ok(compare_vec4(&cmp[i], out[i].x, out[i].y, out[i].z, out[i].w, 4096),
11291 "Vertex %u differs. Got %f %f %f %f.\n", i,
11292 out[i].x, out[i].y, out[i].z, out[i].w);
11295 memset(&mat, 0, sizeof(mat));
11296 mat.dwSize = sizeof(mat);
11297 U1(U(mat).diffuse).r = 0.0f;
11298 U2(U(mat).diffuse).g = 1.0f;
11299 U3(U(mat).diffuse).b = 0.0f;
11300 U4(U(mat).diffuse).a = 0.0f;
11301 hr = IDirect3DMaterial2_SetMaterial(background, &mat);
11302 ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
11303 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
11304 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
11306 hr = IDirect3DDevice2_BeginScene(device);
11307 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
11308 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, quad, 4, 0);
11309 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11310 hr = IDirect3DDevice2_EndScene(device);
11311 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
11313 color = get_surface_color(rt, 58, 118);
11314 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
11315 color = get_surface_color(rt, 62, 118);
11316 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
11317 color = get_surface_color(rt, 58, 122);
11318 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
11319 color = get_surface_color(rt, 62, 122);
11320 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
11322 color = get_surface_color(rt, 157, 177);
11323 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
11324 color = get_surface_color(rt, 161, 177);
11325 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
11326 color = get_surface_color(rt, 157, 181);
11327 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
11328 color = get_surface_color(rt, 161, 181);
11329 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
11331 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &mat_identity);
11332 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
11333 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &mat_identity);
11334 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
11335 hr = IDirect3DDevice2_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &mat_transform3);
11336 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
11338 vp2_data.dwX = 0.0;
11339 vp2_data.dwY = 0.0;
11340 vp2_data.dwWidth = 1;
11341 vp2_data.dwHeight = 1;
11342 vp2_data.dvClipX = -12.8f;
11343 vp2_data.dvClipY = 12.8f + mat_transform3._42 / mat_transform3._44;
11344 vp2_data.dvClipWidth = 25.6f;
11345 vp2_data.dvClipHeight = 25.6f;
11346 vp2_data.dvMinZ = 0.0f;
11347 vp2_data.dvMaxZ = 0.5f;
11348 hr = IDirect3DViewport2_SetViewport2(viewport, &vp2_data);
11349 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
11350 transformdata.lpIn = cliptest;
11351 transformdata.dwInSize = sizeof(cliptest[0]);
11352 offscreen = 0xdeadbeef;
11353 hr = IDirect3DViewport2_TransformVertices(viewport, ARRAY_SIZE(cliptest),
11354 &transformdata, D3DTRANSFORM_CLIPPED, &offscreen);
11355 ok(SUCCEEDED(hr), "Failed to transform vertices, hr %#x.\n", hr);
11356 ok(!offscreen, "Offscreen is %x.\n", offscreen);
11357 for (i = 0; i < ARRAY_SIZE(cliptest); ++i)
11359 static const D3DHVERTEX cmp_h[] =
11361 {0, { 25.59f}, { 44.79f}, { 1.0f }},
11362 {D3DCLIP_RIGHT | D3DCLIP_TOP | D3DCLIP_BACK, { 25.61f}, { 44.81f}, { 1.01f}},
11363 {0, {-25.59f}, {-6.39f }, { 0.0f }},
11364 {D3DCLIP_LEFT | D3DCLIP_BOTTOM | D3DCLIP_FRONT,{-25.61f}, {-6.41f }, {-0.01f}},
11366 ok(compare_float(U1(cmp_h[i]).hx, U1(out_h[i]).hx, 4096)
11367 && compare_float(U2(cmp_h[i]).hy, U2(out_h[i]).hy, 4096)
11368 && compare_float(U3(cmp_h[i]).hz, U3(out_h[i]).hz, 4096)
11369 && cmp_h[i].dwFlags == out_h[i].dwFlags,
11370 "HVertex %u differs. Got %#x %f %f %f.\n", i,
11371 out_h[i].dwFlags, U1(out_h[i]).hx, U2(out_h[i]).hy, U3(out_h[i]).hz);
11374 IDirectDrawSurface_Release(rt);
11375 destroy_viewport(device, viewport);
11376 IDirect3DMaterial2_Release(background);
11377 refcount = IDirect3DDevice_Release(device);
11378 ok(!refcount, "Device has %u references left.\n", refcount);
11379 IDirectDraw2_Release(ddraw);
11380 DestroyWindow(window);
11383 static void test_display_mode_surface_pixel_format(void)
11385 unsigned int width, height, bpp;
11386 IDirectDrawSurface *surface;
11387 DDSURFACEDESC surface_desc;
11388 IDirectDraw2 *ddraw;
11389 ULONG refcount;
11390 HWND window;
11391 HRESULT hr;
11393 if (!(ddraw = create_ddraw()))
11395 skip("Failed to create ddraw.\n");
11396 return;
11399 surface_desc.dwSize = sizeof(surface_desc);
11400 hr = IDirectDraw2_GetDisplayMode(ddraw, &surface_desc);
11401 ok(SUCCEEDED(hr), "Failed to get display mode, hr %#x.\n", hr);
11402 width = surface_desc.dwWidth;
11403 height = surface_desc.dwHeight;
11405 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
11406 0, 0, width, height, NULL, NULL, NULL, NULL);
11407 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
11408 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
11410 bpp = 0;
11411 if (SUCCEEDED(IDirectDraw2_SetDisplayMode(ddraw, width, height, 16, 0, 0)))
11412 bpp = 16;
11413 if (SUCCEEDED(IDirectDraw2_SetDisplayMode(ddraw, width, height, 24, 0, 0)))
11414 bpp = 24;
11415 if (SUCCEEDED(IDirectDraw2_SetDisplayMode(ddraw, width, height, 32, 0, 0)))
11416 bpp = 32;
11417 ok(bpp, "Set display mode failed.\n");
11419 surface_desc.dwSize = sizeof(surface_desc);
11420 hr = IDirectDraw2_GetDisplayMode(ddraw, &surface_desc);
11421 ok(SUCCEEDED(hr), "Failed to get display mode, hr %#x.\n", hr);
11422 ok(surface_desc.dwWidth == width, "Got width %u, expected %u.\n", surface_desc.dwWidth, width);
11423 ok(surface_desc.dwHeight == height, "Got height %u, expected %u.\n", surface_desc.dwHeight, height);
11424 ok(U1(surface_desc.ddpfPixelFormat).dwRGBBitCount == bpp, "Got bpp %u, expected %u.\n",
11425 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount, bpp);
11427 memset(&surface_desc, 0, sizeof(surface_desc));
11428 surface_desc.dwSize = sizeof(surface_desc);
11429 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
11430 surface_desc.dwBackBufferCount = 1;
11431 surface_desc.ddsCaps.dwCaps = DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE;
11432 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
11433 ok(hr == D3D_OK, "Failed to create surface, hr %#x.\n", hr);
11434 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
11435 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
11436 ok(surface_desc.dwWidth == width, "Got width %u, expected %u.\n", surface_desc.dwWidth, width);
11437 ok(surface_desc.dwHeight == height, "Got height %u, expected %u.\n", surface_desc.dwHeight, height);
11438 ok(surface_desc.ddpfPixelFormat.dwFlags == DDPF_RGB, "Got unexpected pixel format flags %#x.\n",
11439 surface_desc.ddpfPixelFormat.dwFlags);
11440 ok(U1(surface_desc.ddpfPixelFormat).dwRGBBitCount == bpp, "Got bpp %u, expected %u.\n",
11441 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount, bpp);
11442 IDirectDrawSurface_Release(surface);
11444 memset(&surface_desc, 0, sizeof(surface_desc));
11445 surface_desc.dwSize = sizeof(surface_desc);
11446 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
11447 surface_desc.dwWidth = width;
11448 surface_desc.dwHeight = height;
11449 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
11450 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
11451 ok(hr == D3D_OK, "Failed to create surface, hr %#x.\n", hr);
11452 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
11453 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
11454 ok(surface_desc.ddpfPixelFormat.dwFlags == DDPF_RGB, "Got unexpected pixel format flags %#x.\n",
11455 surface_desc.ddpfPixelFormat.dwFlags);
11456 ok(U1(surface_desc.ddpfPixelFormat).dwRGBBitCount == bpp, "Got bpp %u, expected %u.\n",
11457 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount, bpp);
11458 IDirectDrawSurface_Release(surface);
11460 refcount = IDirectDraw2_Release(ddraw);
11461 ok(!refcount, "DirectDraw has %u references left.\n", refcount);
11462 DestroyWindow(window);
11465 static void test_surface_desc_size(void)
11467 union
11469 DWORD dwSize;
11470 DDSURFACEDESC desc1;
11471 DDSURFACEDESC2 desc2;
11472 BYTE blob[1024];
11473 } desc;
11474 IDirectDrawSurface7 *surface7;
11475 IDirectDrawSurface2 *surface2;
11476 IDirectDrawSurface *surface;
11477 DDSURFACEDESC surface_desc;
11478 HRESULT expected_hr, hr;
11479 IDirectDraw2 *ddraw;
11480 unsigned int i, j;
11481 ULONG refcount;
11483 static const struct
11485 unsigned int caps;
11486 const char *name;
11488 surface_caps[] =
11490 {DDSCAPS_OFFSCREENPLAIN, "offscreenplain"},
11491 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, "systemmemory texture"},
11492 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, "videomemory texture"},
11494 static const unsigned int desc_sizes[] =
11496 sizeof(DDSURFACEDESC),
11497 sizeof(DDSURFACEDESC2),
11498 sizeof(DDSURFACEDESC) + 1,
11499 sizeof(DDSURFACEDESC2) + 1,
11500 2 * sizeof(DDSURFACEDESC),
11501 2 * sizeof(DDSURFACEDESC2),
11502 sizeof(DDSURFACEDESC) - 1,
11503 sizeof(DDSURFACEDESC2) - 1,
11504 sizeof(DDSURFACEDESC) / 2,
11505 sizeof(DDSURFACEDESC2) / 2,
11509 sizeof(desc) / 2,
11510 sizeof(desc) - 100,
11513 if (!(ddraw = create_ddraw()))
11515 skip("Failed to create ddraw.\n");
11516 return;
11518 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
11519 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
11521 for (i = 0; i < ARRAY_SIZE(surface_caps); ++i)
11523 memset(&surface_desc, 0, sizeof(surface_desc));
11524 surface_desc.dwSize = sizeof(surface_desc);
11525 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
11526 surface_desc.ddsCaps.dwCaps = surface_caps[i].caps;
11527 surface_desc.dwHeight = 128;
11528 surface_desc.dwWidth = 128;
11529 if (FAILED(IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL)))
11531 skip("Failed to create surface, type %s.\n", surface_caps[i].name);
11532 continue;
11534 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface2, (void **)&surface2);
11535 ok(hr == DD_OK, "Failed to query IDirectDrawSurface2, hr %#x, type %s.\n", hr, surface_caps[i].name);
11536 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface7, (void **)&surface7);
11537 ok(hr == DD_OK, "Failed to query IDirectDrawSurface7, hr %#x, type %s.\n", hr, surface_caps[i].name);
11539 /* GetSurfaceDesc() */
11540 for (j = 0; j < ARRAY_SIZE(desc_sizes); ++j)
11542 memset(&desc, 0, sizeof(desc));
11543 desc.dwSize = desc_sizes[j];
11544 expected_hr = desc.dwSize == sizeof(DDSURFACEDESC) ? DD_OK : DDERR_INVALIDPARAMS;
11545 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &desc.desc1);
11546 ok(hr == expected_hr, "Got hr %#x, expected %#x, dwSize %u, type %s.\n",
11547 hr, expected_hr, desc_sizes[j], surface_caps[i].name);
11549 memset(&desc, 0, sizeof(desc));
11550 desc.dwSize = desc_sizes[j];
11551 expected_hr = desc.dwSize == sizeof(DDSURFACEDESC) ? DD_OK : DDERR_INVALIDPARAMS;
11552 hr = IDirectDrawSurface2_GetSurfaceDesc(surface2, &desc.desc1);
11553 ok(hr == expected_hr, "Got hr %#x, expected %#x, dwSize %u, type %s.\n",
11554 hr, expected_hr, desc_sizes[j], surface_caps[i].name);
11556 memset(&desc, 0, sizeof(desc));
11557 desc.dwSize = desc_sizes[j];
11558 expected_hr = desc.dwSize == sizeof(DDSURFACEDESC2) ? DD_OK : DDERR_INVALIDPARAMS;
11559 hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc.desc2);
11560 ok(hr == expected_hr, "Got hr %#x, expected %#x, dwSize %u, type %s.\n",
11561 hr, expected_hr, desc_sizes[j], surface_caps[i].name);
11564 /* Lock() */
11565 for (j = 0; j < ARRAY_SIZE(desc_sizes); ++j)
11567 const BOOL valid_size = desc_sizes[j] == sizeof(DDSURFACEDESC)
11568 || desc_sizes[j] == sizeof(DDSURFACEDESC2);
11569 DWORD expected_texture_stage;
11571 memset(&desc, 0, sizeof(desc));
11572 desc.dwSize = desc_sizes[j];
11573 desc.desc2.dwTextureStage = 0xdeadbeef;
11574 desc.blob[sizeof(DDSURFACEDESC2)] = 0xef;
11575 hr = IDirectDrawSurface_Lock(surface, NULL, &desc.desc1, 0, 0);
11576 expected_hr = valid_size ? DD_OK : DDERR_INVALIDPARAMS;
11577 ok(hr == expected_hr, "Got hr %#x, expected %#x, dwSize %u, type %s.\n",
11578 hr, expected_hr, desc_sizes[j], surface_caps[i].name);
11579 ok(desc.dwSize == desc_sizes[j], "dwSize was changed from %u to %u, type %s.\n",
11580 desc_sizes[j], desc.dwSize, surface_caps[i].name);
11581 ok(desc.blob[sizeof(DDSURFACEDESC2)] == 0xef, "Got unexpected byte %02x, dwSize %u, type %s.\n",
11582 desc.blob[sizeof(DDSURFACEDESC2)], desc_sizes[j], surface_caps[i].name);
11583 if (SUCCEEDED(hr))
11585 ok(desc.desc1.dwWidth == 128, "Got unexpected width %u, dwSize %u, type %s.\n",
11586 desc.desc1.dwWidth, desc_sizes[j], surface_caps[i].name);
11587 ok(desc.desc1.dwHeight == 128, "Got unexpected height %u, dwSize %u, type %s.\n",
11588 desc.desc1.dwHeight, desc_sizes[j], surface_caps[i].name);
11589 expected_texture_stage = desc_sizes[j] >= sizeof(DDSURFACEDESC2) ? 0 : 0xdeadbeef;
11590 todo_wine_if(!expected_texture_stage)
11591 ok(desc.desc2.dwTextureStage == expected_texture_stage,
11592 "Got unexpected texture stage %#x, dwSize %u, type %s.\n",
11593 desc.desc2.dwTextureStage, desc_sizes[j], surface_caps[i].name);
11594 IDirectDrawSurface_Unlock(surface, NULL);
11597 memset(&desc, 0, sizeof(desc));
11598 desc.dwSize = desc_sizes[j];
11599 desc.desc2.dwTextureStage = 0xdeadbeef;
11600 desc.blob[sizeof(DDSURFACEDESC2)] = 0xef;
11601 hr = IDirectDrawSurface2_Lock(surface2, NULL, &desc.desc1, 0, 0);
11602 expected_hr = valid_size ? DD_OK : DDERR_INVALIDPARAMS;
11603 ok(hr == expected_hr, "Got hr %#x, expected %#x, dwSize %u, type %s.\n",
11604 hr, expected_hr, desc_sizes[j], surface_caps[i].name);
11605 ok(desc.dwSize == desc_sizes[j], "dwSize was changed from %u to %u, type %s.\n",
11606 desc_sizes[j], desc.dwSize, surface_caps[i].name);
11607 ok(desc.blob[sizeof(DDSURFACEDESC2)] == 0xef, "Got unexpected byte %02x, dwSize %u, type %s.\n",
11608 desc.blob[sizeof(DDSURFACEDESC2)], desc_sizes[j], surface_caps[i].name);
11609 if (SUCCEEDED(hr))
11611 ok(desc.desc2.dwWidth == 128, "Got unexpected width %u, dwSize %u, type %s.\n",
11612 desc.desc2.dwWidth, desc_sizes[j], surface_caps[i].name);
11613 ok(desc.desc2.dwHeight == 128, "Got unexpected height %u, dwSize %u, type %s.\n",
11614 desc.desc2.dwHeight, desc_sizes[j], surface_caps[i].name);
11615 expected_texture_stage = desc_sizes[j] >= sizeof(DDSURFACEDESC2) ? 0 : 0xdeadbeef;
11616 todo_wine_if(!expected_texture_stage)
11617 ok(desc.desc2.dwTextureStage == expected_texture_stage,
11618 "Got unexpected texture stage %#x, dwSize %u, type %s.\n",
11619 desc.desc2.dwTextureStage, desc_sizes[j], surface_caps[i].name);
11620 IDirectDrawSurface2_Unlock(surface2, NULL);
11623 memset(&desc, 0, sizeof(desc));
11624 desc.dwSize = desc_sizes[j];
11625 desc.desc2.dwTextureStage = 0xdeadbeef;
11626 desc.blob[sizeof(DDSURFACEDESC2)] = 0xef;
11627 hr = IDirectDrawSurface7_Lock(surface7, NULL, &desc.desc2, 0, 0);
11628 expected_hr = valid_size ? DD_OK : DDERR_INVALIDPARAMS;
11629 ok(hr == expected_hr, "Got hr %#x, expected %#x, dwSize %u, type %s.\n",
11630 hr, expected_hr, desc_sizes[j], surface_caps[i].name);
11631 ok(desc.dwSize == desc_sizes[j], "dwSize was changed from %u to %u, type %s.\n",
11632 desc_sizes[j], desc.dwSize, surface_caps[i].name);
11633 ok(desc.blob[sizeof(DDSURFACEDESC2)] == 0xef, "Got unexpected byte %02x, dwSize %u, type %s.\n",
11634 desc.blob[sizeof(DDSURFACEDESC2)], desc_sizes[j], surface_caps[i].name);
11635 if (SUCCEEDED(hr))
11637 ok(desc.desc2.dwWidth == 128, "Got unexpected width %u, dwSize %u, type %s.\n",
11638 desc.desc2.dwWidth, desc_sizes[j], surface_caps[i].name);
11639 ok(desc.desc2.dwHeight == 128, "Got unexpected height %u, dwSize %u, type %s.\n",
11640 desc.desc2.dwHeight, desc_sizes[j], surface_caps[i].name);
11641 expected_texture_stage = desc_sizes[j] >= sizeof(DDSURFACEDESC2) ? 0 : 0xdeadbeef;
11642 ok(desc.desc2.dwTextureStage == expected_texture_stage,
11643 "Got unexpected texture stage %#x, dwSize %u, type %s.\n",
11644 desc.desc2.dwTextureStage, desc_sizes[j], surface_caps[i].name);
11645 IDirectDrawSurface7_Unlock(surface7, NULL);
11649 IDirectDrawSurface7_Release(surface7);
11650 IDirectDrawSurface2_Release(surface2);
11651 IDirectDrawSurface_Release(surface);
11654 /* GetDisplayMode() */
11655 for (j = 0; j < ARRAY_SIZE(desc_sizes); ++j)
11657 memset(&desc, 0xcc, sizeof(desc));
11658 desc.dwSize = desc_sizes[j];
11659 expected_hr = (desc.dwSize == sizeof(DDSURFACEDESC) || desc.dwSize == sizeof(DDSURFACEDESC2))
11660 ? DD_OK : DDERR_INVALIDPARAMS;
11661 hr = IDirectDraw2_GetDisplayMode(ddraw, &desc.desc1);
11662 ok(hr == expected_hr, "Got hr %#x, expected %#x, size %u.\n", hr, expected_hr, desc_sizes[j]);
11663 if (SUCCEEDED(hr))
11665 ok(desc.dwSize == sizeof(DDSURFACEDESC), "Wrong size %u for %u.\n", desc.dwSize, desc_sizes[j]);
11666 ok(desc.blob[desc_sizes[j]] == 0xcc, "Overflow for size %u.\n", desc_sizes[j]);
11667 ok(desc.blob[desc_sizes[j] - 1] != 0xcc, "Struct not cleared for size %u.\n", desc_sizes[j]);
11671 refcount = IDirectDraw2_Release(ddraw);
11672 ok(!refcount, "DirectDraw has %u references left.\n", refcount);
11675 static void test_ck_operation(void)
11677 IDirectDrawSurface2 *src, *dst;
11678 IDirectDrawSurface7 *src7, *dst7;
11679 IDirectDrawSurface *surface1;
11680 DDSURFACEDESC surface_desc;
11681 IDirectDraw2 *ddraw;
11682 ULONG refcount;
11683 HWND window;
11684 HRESULT hr;
11685 D3DCOLOR *color;
11686 unsigned int i;
11687 DDCOLORKEY ckey;
11688 DDBLTFX fx;
11690 window = create_window();
11691 ddraw = create_ddraw();
11692 ok(!!ddraw, "Failed to create a ddraw object.\n");
11693 hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
11694 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
11696 memset(&surface_desc, 0, sizeof(surface_desc));
11697 surface_desc.dwSize = sizeof(surface_desc);
11698 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
11699 surface_desc.dwWidth = 4;
11700 surface_desc.dwHeight = 1;
11701 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
11702 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
11703 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
11704 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
11705 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
11706 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
11707 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
11708 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
11709 hr = IDirectDrawSurface_QueryInterface(surface1, &IID_IDirectDrawSurface2, (void **)&dst);
11710 ok(SUCCEEDED(hr), "Failed to query IDirectDrawSurface2, hr %#x.\n", hr);
11711 IDirectDrawSurface_Release(surface1);
11713 surface_desc.dwFlags |= DDSD_CKSRCBLT;
11714 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00ff00ff;
11715 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00ff00ff;
11716 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
11717 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
11718 hr = IDirectDrawSurface_QueryInterface(surface1, &IID_IDirectDrawSurface2, (void **)&src);
11719 ok(SUCCEEDED(hr), "Failed to query IDirectDrawSurface2, hr %#x.\n", hr);
11720 IDirectDrawSurface_Release(surface1);
11722 hr = IDirectDrawSurface2_Lock(src, NULL, &surface_desc, DDLOCK_WAIT, NULL);
11723 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
11724 ok(!(surface_desc.dwFlags & DDSD_LPSURFACE), "Surface desc has LPSURFACE Flags set.\n");
11725 color = surface_desc.lpSurface;
11726 color[0] = 0x77010203;
11727 color[1] = 0x00010203;
11728 color[2] = 0x77ff00ff;
11729 color[3] = 0x00ff00ff;
11730 hr = IDirectDrawSurface2_Unlock(src, NULL);
11731 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
11733 for (i = 0; i < 2; ++i)
11735 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
11736 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
11737 color = surface_desc.lpSurface;
11738 color[0] = 0xcccccccc;
11739 color[1] = 0xcccccccc;
11740 color[2] = 0xcccccccc;
11741 color[3] = 0xcccccccc;
11742 hr = IDirectDrawSurface2_Unlock(dst, NULL);
11743 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
11745 if (i)
11747 hr = IDirectDrawSurface2_BltFast(dst, 0, 0, src, NULL, DDBLTFAST_SRCCOLORKEY);
11748 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
11750 else
11752 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYSRC, NULL);
11753 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
11756 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT | DDLOCK_READONLY, NULL);
11757 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
11758 ok(!(surface_desc.dwFlags & DDSD_LPSURFACE), "Surface desc has LPSURFACE Flags set.\n");
11759 color = surface_desc.lpSurface;
11760 /* Different behavior on some drivers / windows versions. Some versions ignore the X channel when
11761 * color keying, but copy it to the destination surface. Others (sysmem surfaces) apply it for
11762 * color keying, but do not copy it into the destination surface. Nvidia neither uses it for
11763 * color keying nor copies it. */
11764 ok((color[0] == 0x77010203 && color[1] == 0x00010203
11765 && color[2] == 0xcccccccc && color[3] == 0xcccccccc) /* AMD, Wine */
11766 || broken(color[0] == 0x00010203 && color[1] == 0x00010203
11767 && color[2] == 0x00ff00ff && color[3] == 0xcccccccc) /* Sysmem surfaces? */
11768 || broken(color[0] == 0x00010203 && color[1] == 0x00010203
11769 && color[2] == 0xcccccccc && color[3] == 0xcccccccc) /* Nvidia */
11770 || broken(color[0] == 0xff010203 && color[1] == 0xff010203
11771 && color[2] == 0xcccccccc && color[3] == 0xcccccccc) /* Testbot */,
11772 "Destination data after blitting is %08x %08x %08x %08x, i=%u.\n",
11773 color[0], color[1], color[2], color[3], i);
11774 hr = IDirectDrawSurface2_Unlock(dst, NULL);
11775 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
11778 hr = IDirectDrawSurface2_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
11779 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
11780 ok(ckey.dwColorSpaceLowValue == 0x00ff00ff && ckey.dwColorSpaceHighValue == 0x00ff00ff,
11781 "Got unexpected color key low=%08x high=%08x.\n", ckey.dwColorSpaceLowValue, ckey.dwColorSpaceHighValue);
11783 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x0000ff00;
11784 hr = IDirectDrawSurface2_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
11785 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
11787 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0;
11788 hr = IDirectDrawSurface2_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
11789 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
11790 ok(ckey.dwColorSpaceLowValue == 0x0000ff00 && ckey.dwColorSpaceHighValue == 0x0000ff00,
11791 "Got unexpected color key low=%08x high=%08x.\n", ckey.dwColorSpaceLowValue, ckey.dwColorSpaceHighValue);
11793 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0;
11794 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0;
11795 hr = IDirectDrawSurface2_GetSurfaceDesc(src, &surface_desc);
11796 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
11797 ok(surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue == 0x0000ff00
11798 && surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue == 0x0000ff00,
11799 "Got unexpected color key low=%08x high=%08x.\n", surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue,
11800 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue);
11802 /* Test SetColorKey with dwColorSpaceHighValue < dwColorSpaceLowValue */
11803 ckey.dwColorSpaceLowValue = 0x000000ff;
11804 ckey.dwColorSpaceHighValue = 0x00000000;
11805 hr = IDirectDrawSurface2_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
11806 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
11808 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0;
11809 hr = IDirectDrawSurface2_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
11810 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
11811 ok(ckey.dwColorSpaceLowValue == 0x000000ff && ckey.dwColorSpaceHighValue == 0x000000ff,
11812 "Got unexpected color key low=%08x high=%08x.\n", ckey.dwColorSpaceLowValue, ckey.dwColorSpaceHighValue);
11814 ckey.dwColorSpaceLowValue = 0x000000ff;
11815 ckey.dwColorSpaceHighValue = 0x00000001;
11816 hr = IDirectDrawSurface2_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
11817 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
11819 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0;
11820 hr = IDirectDrawSurface2_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
11821 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
11822 ok(ckey.dwColorSpaceLowValue == 0x000000ff && ckey.dwColorSpaceHighValue == 0x000000ff,
11823 "Got unexpected color key low=%08x high=%08x.\n", ckey.dwColorSpaceLowValue, ckey.dwColorSpaceHighValue);
11825 ckey.dwColorSpaceLowValue = 0x000000fe;
11826 ckey.dwColorSpaceHighValue = 0x000000fd;
11827 hr = IDirectDrawSurface2_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
11828 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
11830 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0;
11831 hr = IDirectDrawSurface2_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
11832 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
11833 ok(ckey.dwColorSpaceLowValue == 0x000000fe && ckey.dwColorSpaceHighValue == 0x000000fe,
11834 "Got unexpected color key low=%08x high=%08x.\n", ckey.dwColorSpaceLowValue, ckey.dwColorSpaceHighValue);
11836 IDirectDrawSurface2_Release(src);
11837 IDirectDrawSurface2_Release(dst);
11839 /* Test source and destination keys and where they are read from. Use a surface with alpha
11840 * to avoid driver-dependent content in the X channel. */
11841 memset(&surface_desc, 0, sizeof(surface_desc));
11842 surface_desc.dwSize = sizeof(surface_desc);
11843 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
11844 surface_desc.dwWidth = 6;
11845 surface_desc.dwHeight = 1;
11846 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
11847 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
11848 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
11849 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
11850 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
11851 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
11852 U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
11853 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
11854 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
11855 hr = IDirectDrawSurface_QueryInterface(surface1, &IID_IDirectDrawSurface2, (void **)&dst);
11856 ok(SUCCEEDED(hr), "Failed to query IDirectDrawSurface2, hr %#x.\n", hr);
11857 IDirectDrawSurface_Release(surface1);
11859 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
11860 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
11861 hr = IDirectDrawSurface_QueryInterface(surface1, &IID_IDirectDrawSurface2, (void **)&src);
11862 ok(SUCCEEDED(hr), "Failed to query IDirectDrawSurface2, hr %#x.\n", hr);
11863 IDirectDrawSurface_Release(surface1);
11865 ckey.dwColorSpaceLowValue = 0x0000ff00;
11866 ckey.dwColorSpaceHighValue = 0x0000ff00;
11867 hr = IDirectDrawSurface2_SetColorKey(dst, DDCKEY_SRCBLT, &ckey);
11868 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
11869 ckey.dwColorSpaceLowValue = 0x00ff0000;
11870 ckey.dwColorSpaceHighValue = 0x00ff0000;
11871 hr = IDirectDrawSurface2_SetColorKey(dst, DDCKEY_DESTBLT, &ckey);
11872 ok(SUCCEEDED(hr) || hr == DDERR_NOCOLORKEYHW, "Failed to set color key, hr %#x.\n", hr);
11873 if (FAILED(hr))
11875 /* Nvidia reject dest keys, AMD allows them. This applies to vidmem and sysmem surfaces. */
11876 skip("Failed to set destination color key, skipping related tests.\n");
11877 goto done;
11880 ckey.dwColorSpaceLowValue = 0x000000ff;
11881 ckey.dwColorSpaceHighValue = 0x000000ff;
11882 hr = IDirectDrawSurface2_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
11883 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
11884 ckey.dwColorSpaceLowValue = 0x000000aa;
11885 ckey.dwColorSpaceHighValue = 0x000000aa;
11886 hr = IDirectDrawSurface2_SetColorKey(src, DDCKEY_DESTBLT, &ckey);
11887 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
11889 memset(&fx, 0, sizeof(fx));
11890 fx.dwSize = sizeof(fx);
11891 fx.ddckSrcColorkey.dwColorSpaceHighValue = 0x00110000;
11892 fx.ddckSrcColorkey.dwColorSpaceLowValue = 0x00110000;
11893 fx.ddckDestColorkey.dwColorSpaceHighValue = 0x00001100;
11894 fx.ddckDestColorkey.dwColorSpaceLowValue = 0x00001100;
11896 hr = IDirectDrawSurface2_Lock(src, NULL, &surface_desc, DDLOCK_WAIT, NULL);
11897 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
11898 color = surface_desc.lpSurface;
11899 color[0] = 0x000000ff; /* Applies to src blt key in src surface. */
11900 color[1] = 0x000000aa; /* Applies to dst blt key in src surface. */
11901 color[2] = 0x00ff0000; /* Dst color key in dst surface. */
11902 color[3] = 0x0000ff00; /* Src color key in dst surface. */
11903 color[4] = 0x00001100; /* Src color key in ddbltfx. */
11904 color[5] = 0x00110000; /* Dst color key in ddbltfx. */
11905 hr = IDirectDrawSurface2_Unlock(src, NULL);
11906 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
11908 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
11909 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
11910 color = surface_desc.lpSurface;
11911 color[0] = color[1] = color[2] = color[3] = color[4] = color[5] = 0x55555555;
11912 hr = IDirectDrawSurface2_Unlock(dst, NULL);
11913 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
11915 /* Test a blit without keying. */
11916 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, 0, &fx);
11917 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
11919 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
11920 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
11921 color = surface_desc.lpSurface;
11922 /* Should have copied src data unmodified to dst. */
11923 ok(color[0] == 0x000000ff && color[1] == 0x000000aa && color[2] == 0x00ff0000 &&
11924 color[3] == 0x0000ff00 && color[4] == 0x00001100 && color[5] == 0x00110000,
11925 "Got unexpected content %08x %08x %08x %08x %08x %08x.\n",
11926 color[0], color[1], color[2], color[3], color[4], color[5]);
11928 color[0] = color[1] = color[2] = color[3] = color[4] = color[5] = 0x55555555;
11929 hr = IDirectDrawSurface2_Unlock(dst, NULL);
11930 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
11932 /* Src key. */
11933 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYSRC, &fx);
11934 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
11936 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
11937 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
11938 color = surface_desc.lpSurface;
11939 /* Src key applied to color[0]. It is unmodified, the others are copied. */
11940 ok(color[0] == 0x55555555 && color[1] == 0x000000aa && color[2] == 0x00ff0000 &&
11941 color[3] == 0x0000ff00 && color[4] == 0x00001100 && color[5] == 0x00110000,
11942 "Got unexpected content %08x %08x %08x %08x %08x %08x.\n",
11943 color[0], color[1], color[2], color[3], color[4], color[5]);
11945 color[0] = color[1] = color[2] = color[3] = color[4] = color[5] = 0x55555555;
11946 hr = IDirectDrawSurface2_Unlock(dst, NULL);
11947 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
11949 /* Src override. */
11950 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYSRCOVERRIDE, &fx);
11951 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
11953 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
11954 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
11955 color = surface_desc.lpSurface;
11956 /* Override key applied to color[5]. It is unmodified, the others are copied. */
11957 ok(color[0] == 0x000000ff && color[1] == 0x000000aa && color[2] == 0x00ff0000 &&
11958 color[3] == 0x0000ff00 && color[4] == 0x00001100 && color[5] == 0x55555555,
11959 "Got unexpected content %08x %08x %08x %08x %08x %08x.\n",
11960 color[0], color[1], color[2], color[3], color[4], color[5]);
11962 color[0] = color[1] = color[2] = color[3] = color[4] = color[5] = 0x55555555;
11963 hr = IDirectDrawSurface2_Unlock(dst, NULL);
11964 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
11966 /* Src override AND src key. That is not supposed to work. */
11967 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYSRC | DDBLT_KEYSRCOVERRIDE, &fx);
11968 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
11970 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
11971 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
11972 color = surface_desc.lpSurface;
11973 /* Ensure the destination was not changed. */
11974 ok(color[0] == 0x55555555 && color[1] == 0x55555555 && color[2] == 0x55555555 &&
11975 color[3] == 0x55555555 && color[4] == 0x55555555 && color[5] == 0x55555555,
11976 "Got unexpected content %08x %08x %08x %08x %08x %08x.\n",
11977 color[0], color[1], color[2], color[3], color[4], color[5]);
11979 /* Use different dst colors for the dst key test. */
11980 color[0] = 0x00ff0000; /* Dest key in dst surface. */
11981 color[1] = 0x00ff0000; /* Dest key in dst surface. */
11982 color[2] = 0x00001100; /* Dest key in override. */
11983 color[3] = 0x00001100; /* Dest key in override. */
11984 color[4] = 0x000000aa; /* Dest key in src surface. */
11985 color[5] = 0x000000aa; /* Dest key in src surface. */
11986 hr = IDirectDrawSurface2_Unlock(dst, NULL);
11987 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
11989 /* Dest key blit. The key is taken from the SOURCE surface in v2! */
11990 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYDEST, &fx);
11991 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
11993 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
11994 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
11995 color = surface_desc.lpSurface;
11996 /* Dst key applied to color[4,5], they are the only changed pixels. */
11997 ok(color[0] == 0x00ff0000 && color[1] == 0x00ff0000 && color[2] == 0x00001100 &&
11998 color[3] == 0x00001100 && color[4] == 0x00001100 && color[5] == 0x00110000,
11999 "Got unexpected content %08x %08x %08x %08x %08x %08x.\n",
12000 color[0], color[1], color[2], color[3], color[4], color[5]);
12002 color[0] = 0x00ff0000; /* Dest key in dst surface. */
12003 color[1] = 0x00ff0000; /* Dest key in dst surface. */
12004 color[2] = 0x00001100; /* Dest key in override. */
12005 color[3] = 0x00001100; /* Dest key in override. */
12006 color[4] = 0x000000aa; /* Dest key in src surface. */
12007 color[5] = 0x000000aa; /* Dest key in src surface. */
12008 hr = IDirectDrawSurface2_Unlock(dst, NULL);
12009 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
12011 /* What happens with a QI'd newer version of the interface? It takes the key
12012 * from the destination surface. */
12013 hr = IDirectDrawSurface2_QueryInterface(src, &IID_IDirectDrawSurface7, (void **)&src7);
12014 ok(SUCCEEDED(hr), "Failed to query IDirectDrawSurface interface, hr %#x.\n", hr);
12015 hr = IDirectDrawSurface2_QueryInterface(dst, &IID_IDirectDrawSurface7, (void **)&dst7);
12016 ok(SUCCEEDED(hr), "Failed to query IDirectDrawSurface interface, hr %#x.\n", hr);
12018 hr = IDirectDrawSurface7_Blt(dst7, NULL, src7, NULL, DDBLT_KEYDEST, &fx);
12019 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
12021 IDirectDrawSurface7_Release(dst7);
12022 IDirectDrawSurface7_Release(src7);
12024 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
12025 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
12026 color = surface_desc.lpSurface;
12027 /* Dst key applied to color[0,1], they are the only changed pixels. */
12028 todo_wine ok(color[0] == 0x000000ff && color[1] == 0x000000aa && color[2] == 0x00001100 &&
12029 color[3] == 0x00001100 && color[4] == 0x000000aa && color[5] == 0x000000aa,
12030 "Got unexpected content %08x %08x %08x %08x %08x %08x.\n",
12031 color[0], color[1], color[2], color[3], color[4], color[5]);
12033 color[0] = 0x00ff0000; /* Dest key in dst surface. */
12034 color[1] = 0x00ff0000; /* Dest key in dst surface. */
12035 color[2] = 0x00001100; /* Dest key in override. */
12036 color[3] = 0x00001100; /* Dest key in override. */
12037 color[4] = 0x000000aa; /* Dest key in src surface. */
12038 color[5] = 0x000000aa; /* Dest key in src surface. */
12039 hr = IDirectDrawSurface2_Unlock(dst, NULL);
12040 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
12042 /* Dest override key blit. */
12043 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYDESTOVERRIDE, &fx);
12044 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
12046 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
12047 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
12048 color = surface_desc.lpSurface;
12049 /* Dst key applied to color[2,3], they are the only changed pixels. */
12050 ok(color[0] == 0x00ff0000 && color[1] == 0x00ff0000 && color[2] == 0x00ff0000 &&
12051 color[3] == 0x0000ff00 && color[4] == 0x000000aa && color[5] == 0x000000aa,
12052 "Got unexpected content %08x %08x %08x %08x %08x %08x.\n",
12053 color[0], color[1], color[2], color[3], color[4], color[5]);
12055 color[0] = 0x00ff0000; /* Dest key in dst surface. */
12056 color[1] = 0x00ff0000; /* Dest key in dst surface. */
12057 color[2] = 0x00001100; /* Dest key in override. */
12058 color[3] = 0x00001100; /* Dest key in override. */
12059 color[4] = 0x000000aa; /* Dest key in src surface. */
12060 color[5] = 0x000000aa; /* Dest key in src surface. */
12061 hr = IDirectDrawSurface2_Unlock(dst, NULL);
12062 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
12064 /* Dest override together with surface key. Supposed to fail. */
12065 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYDEST | DDBLT_KEYDESTOVERRIDE, &fx);
12066 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
12068 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
12069 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
12070 color = surface_desc.lpSurface;
12071 /* Destination is unchanged. */
12072 ok(color[0] == 0x00ff0000 && color[1] == 0x00ff0000 && color[2] == 0x00001100 &&
12073 color[3] == 0x00001100 && color[4] == 0x000000aa && color[5] == 0x000000aa,
12074 "Got unexpected content %08x %08x %08x %08x %08x %08x.\n",
12075 color[0], color[1], color[2], color[3], color[4], color[5]);
12076 hr = IDirectDrawSurface2_Unlock(dst, NULL);
12077 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
12079 /* Source and destination key. This is driver dependent. New HW treats it like
12080 * DDBLT_KEYSRC. Older HW and some software renderers apply both keys. */
12081 if (0)
12083 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYDEST | DDBLT_KEYSRC, &fx);
12084 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
12086 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
12087 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
12088 color = surface_desc.lpSurface;
12089 /* Color[0] is filtered by the src key, 2-5 are filtered by the dst key, if
12090 * the driver applies it. */
12091 ok(color[0] == 0x00ff0000 && color[1] == 0x000000aa && color[2] == 0x00ff0000 &&
12092 color[3] == 0x0000ff00 && color[4] == 0x00001100 && color[5] == 0x00110000,
12093 "Got unexpected content %08x %08x %08x %08x %08x %08x.\n",
12094 color[0], color[1], color[2], color[3], color[4], color[5]);
12096 color[0] = 0x00ff0000; /* Dest key in dst surface. */
12097 color[1] = 0x00ff0000; /* Dest key in dst surface. */
12098 color[2] = 0x00001100; /* Dest key in override. */
12099 color[3] = 0x00001100; /* Dest key in override. */
12100 color[4] = 0x000000aa; /* Dest key in src surface. */
12101 color[5] = 0x000000aa; /* Dest key in src surface. */
12102 hr = IDirectDrawSurface2_Unlock(dst, NULL);
12103 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
12106 /* Override keys without ddbltfx parameter fail */
12107 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYDESTOVERRIDE, NULL);
12108 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
12109 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYSRCOVERRIDE, NULL);
12110 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
12112 /* Try blitting without keys in the source surface. */
12113 hr = IDirectDrawSurface2_SetColorKey(src, DDCKEY_SRCBLT, NULL);
12114 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
12115 hr = IDirectDrawSurface2_SetColorKey(src, DDCKEY_DESTBLT, NULL);
12116 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
12118 /* That fails now. Do not bother to check that the data is unmodified. */
12119 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYSRC, &fx);
12120 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
12122 /* Surprisingly this still works. It uses the old key from the src surface. */
12123 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYDEST, &fx);
12124 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
12126 hr = IDirectDrawSurface2_Lock(dst, NULL, &surface_desc, DDLOCK_WAIT, NULL);
12127 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
12128 color = surface_desc.lpSurface;
12129 /* Dst key applied to color[4,5], they are the only changed pixels. */
12130 ok(color[0] == 0x00ff0000 && color[1] == 0x00ff0000 && color[2] == 0x00001100 &&
12131 color[3] == 0x00001100 && color[4] == 0x00001100 && color[5] == 0x00110000,
12132 "Got unexpected content %08x %08x %08x %08x %08x %08x.\n",
12133 color[0], color[1], color[2], color[3], color[4], color[5]);
12134 hr = IDirectDrawSurface2_Unlock(dst, NULL);
12135 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
12137 /* This returns DDERR_NOCOLORKEY as expected. */
12138 hr = IDirectDrawSurface2_GetColorKey(src, DDCKEY_DESTBLT, &ckey);
12139 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
12141 /* GetSurfaceDesc returns a zeroed key as expected. */
12142 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x12345678;
12143 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x12345678;
12144 hr = IDirectDrawSurface2_GetSurfaceDesc(src, &surface_desc);
12145 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
12146 ok(!surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue
12147 && !surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue,
12148 "Got unexpected color key low=%08x high=%08x.\n", surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue,
12149 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue);
12151 /* Try blitting without keys in the destination surface. */
12152 hr = IDirectDrawSurface2_SetColorKey(dst, DDCKEY_SRCBLT, NULL);
12153 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
12154 hr = IDirectDrawSurface2_SetColorKey(dst, DDCKEY_DESTBLT, NULL);
12155 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
12157 /* This fails, as sanity would dictate. */
12158 hr = IDirectDrawSurface2_Blt(dst, NULL, src, NULL, DDBLT_KEYDEST, &fx);
12159 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
12161 done:
12162 IDirectDrawSurface2_Release(src);
12163 IDirectDrawSurface2_Release(dst);
12164 refcount = IDirectDraw2_Release(ddraw);
12165 ok(!refcount, "DirectDraw has %u references left.\n", refcount);
12166 DestroyWindow(window);
12169 static void test_set_render_state(void)
12171 IDirect3DDevice2 *device;
12172 IDirectDraw2 *ddraw;
12173 ULONG refcount;
12174 HWND window;
12175 DWORD state;
12176 HRESULT hr;
12178 window = create_window();
12179 ddraw = create_ddraw();
12180 ok(!!ddraw, "Failed to create a ddraw object.\n");
12181 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
12183 skip("Failed to create 3D device.\n");
12184 DestroyWindow(window);
12185 return;
12188 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZVISIBLE, TRUE);
12189 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
12190 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZVISIBLE, FALSE);
12191 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
12193 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, 0);
12194 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
12195 state = 0xdeadbeef;
12196 hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_TEXTUREHANDLE, &state);
12197 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
12198 ok(!state, "Got unexpected render state %#x.\n", state);
12199 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_TEXTUREMAPBLEND, D3DTBLEND_MODULATE);
12200 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
12201 hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_TEXTUREMAPBLEND, &state);
12202 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
12203 ok(state == D3DTBLEND_MODULATE, "Got unexpected render state %#x.\n", state);
12205 refcount = IDirect3DDevice2_Release(device);
12206 ok(!refcount, "Device has %u references left.\n", refcount);
12207 refcount = IDirectDraw2_Release(ddraw);
12208 ok(!refcount, "DirectDraw has %u references left.\n", refcount);
12209 DestroyWindow(window);
12212 static void test_depth_readback(void)
12214 DWORD depth, expected_depth, max_diff;
12215 IDirect3DMaterial2 *blue_background;
12216 IDirectDrawSurface *rt, *ds;
12217 IDirect3DViewport2 *viewport;
12218 DDSURFACEDESC surface_desc;
12219 IDirect3DDevice2 *device;
12220 unsigned int i, x, y;
12221 IDirectDraw2 *ddraw;
12222 ULONG refcount;
12223 HWND window;
12224 HRESULT hr;
12225 void *ptr;
12227 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
12228 static D3DLVERTEX quad[] =
12230 {{-1.0f}, {-1.0f}, {0.1f}, 0, {0xff00ff00}},
12231 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xff00ff00}},
12232 {{ 1.0f}, {-1.0f}, {1.0f}, 0, {0xff00ff00}},
12233 {{ 1.0f}, { 1.0f}, {0.9f}, 0, {0xff00ff00}},
12236 static const struct
12238 unsigned int z_depth, z_mask;
12240 tests[] =
12242 {16, 0x0000ffff},
12243 {24, 0x00ffffff},
12244 {32, 0xffffffff},
12247 window = create_window();
12248 ok(!!window, "Failed to create a window.\n");
12249 ddraw = create_ddraw();
12250 ok(!!ddraw, "Failed to create a ddraw object.\n");
12251 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
12253 skip("Failed to create a D3D device, skipping tests.\n");
12254 IDirectDraw2_Release(ddraw);
12255 DestroyWindow(window);
12256 return;
12259 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
12260 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
12261 blue_background = create_diffuse_material(device, 0.0f, 0.0f, 1.0f, 1.0f);
12262 viewport = create_viewport(device, 0, 0, 640, 480);
12263 viewport_set_background(device, viewport, blue_background);
12264 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
12265 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
12267 hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
12268 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
12270 ds = get_depth_stencil(device);
12271 hr = IDirectDrawSurface_DeleteAttachedSurface(rt, 0, ds);
12272 ok(SUCCEEDED(hr), "Failed to detach depth buffer, hr %#x.\n", hr);
12273 IDirectDrawSurface_Release(ds);
12275 for (i = 0; i < ARRAY_SIZE(tests); ++i)
12277 memset(&surface_desc, 0, sizeof(surface_desc));
12278 surface_desc.dwSize = sizeof(surface_desc);
12279 surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
12280 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY;
12281 U2(surface_desc).dwZBufferBitDepth = tests[i].z_depth;
12282 surface_desc.dwWidth = 640;
12283 surface_desc.dwHeight = 480;
12284 hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &ds, NULL);
12285 if (FAILED(hr))
12287 skip("Format %u not supported, skipping test.\n", i);
12288 continue;
12291 hr = IDirectDrawSurface_AddAttachedSurface(rt, ds);
12292 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
12293 hr = IDirect3DDevice2_SetRenderTarget(device, rt, 0);
12294 ok(SUCCEEDED(hr), "Failed to set render target, hr %#x.\n", hr);
12296 hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER);
12297 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
12298 hr = IDirect3DDevice2_BeginScene(device);
12299 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
12300 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, quad, 4, 0);
12301 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
12302 hr = IDirect3DDevice2_EndScene(device);
12303 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
12305 memset(&surface_desc, 0, sizeof(surface_desc));
12306 surface_desc.dwSize = sizeof(surface_desc);
12307 hr = IDirectDrawSurface_Lock(ds, NULL, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
12308 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
12310 for (y = 60; y < 480; y += 120)
12312 for (x = 80; x < 640; x += 160)
12314 ptr = (BYTE *)surface_desc.lpSurface
12315 + y * U1(surface_desc).lPitch
12316 + x * (tests[i].z_depth == 16 ? 2 : 4);
12317 depth = *((DWORD *)ptr) & tests[i].z_mask;
12318 expected_depth = (x * (0.9 / 640.0) + y * (0.1 / 480.0)) * tests[i].z_mask;
12319 max_diff = ((0.5f * 0.9f) / 640.0f) * tests[i].z_mask;
12320 ok(abs(expected_depth - depth) <= max_diff,
12321 "Test %u: Got depth 0x%08x (diff %d), expected 0x%08x+/-%u, at %u, %u.\n",
12322 i, depth, expected_depth - depth, expected_depth, max_diff, x, y);
12326 hr = IDirectDrawSurface_Unlock(ds, NULL);
12327 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
12329 hr = IDirectDrawSurface_DeleteAttachedSurface(rt, 0, ds);
12330 ok(SUCCEEDED(hr), "Failed to detach depth buffer, hr %#x.\n", hr);
12331 IDirectDrawSurface_Release(ds);
12334 destroy_viewport(device, viewport);
12335 destroy_material(blue_background);
12336 IDirectDrawSurface_Release(rt);
12337 refcount = IDirect3DDevice2_Release(device);
12338 ok(!refcount, "Device has %u references left.\n", refcount);
12339 IDirectDraw2_Release(ddraw);
12340 DestroyWindow(window);
12343 static void test_clear(void)
12345 D3DRECT rect_negneg, rect_full = {{0}, {0}, {640}, {480}};
12346 IDirect3DViewport2 *viewport, *viewport2, *viewport3;
12347 IDirect3DMaterial2 *white, *red, *green, *blue;
12348 IDirect3DDevice2 *device;
12349 IDirectDrawSurface *rt;
12350 IDirectDraw2 *ddraw;
12351 D3DRECT rect[2];
12352 D3DCOLOR color;
12353 ULONG refcount;
12354 HWND window;
12355 HRESULT hr;
12357 window = create_window();
12358 ddraw = create_ddraw();
12359 ok(!!ddraw, "Failed to create a ddraw object.\n");
12360 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
12362 skip("Failed to create a 3D device, skipping test.\n");
12363 IDirectDraw2_Release(ddraw);
12364 DestroyWindow(window);
12365 return;
12367 hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
12368 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
12370 viewport = create_viewport(device, 0, 0, 640, 480);
12371 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
12372 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
12374 white = create_diffuse_material(device, 1.0f, 1.0f, 1.0f, 1.0f);
12375 red = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
12376 green = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 1.0f);
12377 blue = create_diffuse_material(device, 0.0f, 0.0f, 1.0f, 1.0f);
12379 viewport_set_background(device, viewport, white);
12380 hr = IDirect3DViewport2_Clear(viewport, 1, &rect_full, D3DCLEAR_TARGET);
12381 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
12383 /* Positive x, negative y. */
12384 U1(rect[0]).x1 = 0;
12385 U2(rect[0]).y1 = 480;
12386 U3(rect[0]).x2 = 320;
12387 U4(rect[0]).y2 = 240;
12389 /* Positive x, positive y. */
12390 U1(rect[1]).x1 = 0;
12391 U2(rect[1]).y1 = 0;
12392 U3(rect[1]).x2 = 320;
12393 U4(rect[1]).y2 = 240;
12395 /* Clear 2 rectangles with one call. Unlike d3d8/9, the refrast does not
12396 * refuse negative rectangles, but it will not clear them either. */
12397 viewport_set_background(device, viewport, red);
12398 hr = IDirect3DViewport2_Clear(viewport, 2, rect, D3DCLEAR_TARGET);
12399 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
12401 color = get_surface_color(rt, 160, 360);
12402 ok(compare_color(color, 0x00ffffff, 0), "Clear rectangle 3 (pos, neg) has color 0x%08x.\n", color);
12403 color = get_surface_color(rt, 160, 120);
12404 ok(compare_color(color, 0x00ff0000, 0), "Clear rectangle 1 (pos, pos) has color 0x%08x.\n", color);
12405 color = get_surface_color(rt, 480, 360);
12406 ok(compare_color(color, 0x00ffffff, 0), "Clear rectangle 4 (NULL) has color 0x%08x.\n", color);
12407 color = get_surface_color(rt, 480, 120);
12408 ok(compare_color(color, 0x00ffffff, 0), "Clear rectangle 4 (neg, neg) has color 0x%08x.\n", color);
12410 viewport_set_background(device, viewport, white);
12411 hr = IDirect3DViewport2_Clear(viewport, 1, &rect_full, D3DCLEAR_TARGET);
12412 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
12414 /* negative x, negative y.
12415 * Also ignored, except on WARP, which clears the entire screen. */
12416 rect_negneg.x1 = 640;
12417 rect_negneg.y1 = 240;
12418 rect_negneg.x2 = 320;
12419 rect_negneg.y2 = 0;
12420 viewport_set_background(device, viewport, green);
12421 hr = IDirect3DViewport2_Clear(viewport, 1, &rect_negneg, D3DCLEAR_TARGET);
12422 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
12424 color = get_surface_color(rt, 160, 360);
12425 ok(compare_color(color, 0x00ffffff, 0)
12426 || broken(ddraw_is_warp(ddraw) && compare_color(color, 0x0000ff00, 0)),
12427 "Got unexpected color 0x%08x.\n", color);
12428 color = get_surface_color(rt, 160, 120);
12429 ok(compare_color(color, 0x00ffffff, 0)
12430 || broken(ddraw_is_warp(ddraw) && compare_color(color, 0x0000ff00, 0)),
12431 "Got unexpected color 0x%08x.\n", color);
12432 color = get_surface_color(rt, 480, 360);
12433 ok(compare_color(color, 0x00ffffff, 0)
12434 || broken(ddraw_is_warp(ddraw) && compare_color(color, 0x0000ff00, 0)),
12435 "Got unexpected color 0x%08x.\n", color);
12436 color = get_surface_color(rt, 480, 120);
12437 ok(compare_color(color, 0x00ffffff, 0)
12438 || broken(ddraw_is_warp(ddraw) && compare_color(color, 0x0000ff00, 0)),
12439 "Got unexpected color 0x%08x.\n", color);
12441 /* Test how the viewport affects clears. */
12442 viewport_set_background(device, viewport, white);
12443 hr = IDirect3DViewport2_Clear(viewport, 1, &rect_full, D3DCLEAR_TARGET);
12444 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
12446 viewport2 = create_viewport(device, 160, 120, 160, 120);
12447 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport2);
12448 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
12450 viewport_set_background(device, viewport2, blue);
12451 hr = IDirect3DViewport2_Clear(viewport2, 1, &rect_full, D3DCLEAR_TARGET);
12452 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
12454 viewport3 = create_viewport(device, 320, 240, 320, 240);
12455 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport3);
12456 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
12458 U1(rect[0]).x1 = 160;
12459 U2(rect[0]).y1 = 120;
12460 U3(rect[0]).x2 = 480;
12461 U4(rect[0]).y2 = 360;
12462 viewport_set_background(device, viewport3, green);
12463 hr = IDirect3DViewport2_Clear(viewport3, 1, &rect[0], D3DCLEAR_TARGET);
12464 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
12466 hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
12467 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
12469 color = get_surface_color(rt, 158, 118);
12470 ok(compare_color(color, 0x00ffffff, 0), "(158, 118) has color 0x%08x.\n", color);
12471 color = get_surface_color(rt, 162, 118);
12472 ok(compare_color(color, 0x00ffffff, 0), "(162, 118) has color 0x%08x.\n", color);
12473 color = get_surface_color(rt, 158, 122);
12474 ok(compare_color(color, 0x00ffffff, 0), "(158, 122) has color 0x%08x.\n", color);
12475 color = get_surface_color(rt, 162, 122);
12476 ok(compare_color(color, 0x000000ff, 0), "(162, 122) has color 0x%08x.\n", color);
12478 color = get_surface_color(rt, 318, 238);
12479 ok(compare_color(color, 0x000000ff, 0), "(318, 238) has color 0x%08x.\n", color);
12480 color = get_surface_color(rt, 322, 238);
12481 ok(compare_color(color, 0x00ffffff, 0), "(322, 238) has color 0x%08x.\n", color);
12482 color = get_surface_color(rt, 318, 242);
12483 ok(compare_color(color, 0x00ffffff, 0), "(318, 242) has color 0x%08x.\n", color);
12484 color = get_surface_color(rt, 322, 242);
12485 ok(compare_color(color, 0x0000ff00, 0), "(322, 242) has color 0x%08x.\n", color);
12487 color = get_surface_color(rt, 478, 358);
12488 ok(compare_color(color, 0x0000ff00, 0), "(478, 358) has color 0x%08x.\n", color);
12489 color = get_surface_color(rt, 482, 358);
12490 ok(compare_color(color, 0x00ffffff, 0), "(482, 358) has color 0x%08x.\n", color);
12491 color = get_surface_color(rt, 478, 362);
12492 ok(compare_color(color, 0x00ffffff, 0), "(478, 362) has color 0x%08x.\n", color);
12493 color = get_surface_color(rt, 482, 362);
12494 ok(compare_color(color, 0x00ffffff, 0), "(482, 362) has color 0x%08x.\n", color);
12496 /* The clear rectangle is rendertarget absolute, not relative to the
12497 * viewport. */
12498 hr = IDirect3DViewport2_Clear(viewport, 1, &rect_full, D3DCLEAR_TARGET);
12499 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
12500 U1(rect[0]).x1 = 330;
12501 U2(rect[0]).y1 = 250;
12502 U3(rect[0]).x2 = 340;
12503 U4(rect[0]).y2 = 260;
12504 hr = IDirect3DViewport2_Clear(viewport3, 1, &rect[0], D3DCLEAR_TARGET);
12505 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
12507 color = get_surface_color(rt, 328, 248);
12508 ok(compare_color(color, 0x00ffffff, 0), "(328, 248) has color 0x%08x.\n", color);
12509 color = get_surface_color(rt, 332, 248);
12510 ok(compare_color(color, 0x00ffffff, 0), "(332, 248) has color 0x%08x.\n", color);
12511 color = get_surface_color(rt, 328, 252);
12512 ok(compare_color(color, 0x00ffffff, 0), "(328, 252) has color 0x%08x.\n", color);
12513 color = get_surface_color(rt, 332, 252);
12514 ok(compare_color(color, 0x0000ff00, 0), "(332, 252) has color 0x%08x.\n", color);
12516 color = get_surface_color(rt, 338, 248);
12517 ok(compare_color(color, 0x00ffffff, 0), "(338, 248) has color 0x%08x.\n", color);
12518 color = get_surface_color(rt, 342, 248);
12519 ok(compare_color(color, 0x00ffffff, 0), "(342, 248) has color 0x%08x.\n", color);
12520 color = get_surface_color(rt, 338, 252);
12521 ok(compare_color(color, 0x0000ff00, 0), "(338, 252) has color 0x%08x.\n", color);
12522 color = get_surface_color(rt, 342, 252);
12523 ok(compare_color(color, 0x00ffffff, 0), "(342, 252) has color 0x%08x.\n", color);
12525 color = get_surface_color(rt, 328, 258);
12526 ok(compare_color(color, 0x00ffffff, 0), "(328, 258) has color 0x%08x.\n", color);
12527 color = get_surface_color(rt, 332, 258);
12528 ok(compare_color(color, 0x0000ff00, 0), "(332, 258) has color 0x%08x.\n", color);
12529 color = get_surface_color(rt, 328, 262);
12530 ok(compare_color(color, 0x00ffffff, 0), "(328, 262) has color 0x%08x.\n", color);
12531 color = get_surface_color(rt, 332, 262);
12532 ok(compare_color(color, 0x00ffffff, 0), "(332, 262) has color 0x%08x.\n", color);
12534 color = get_surface_color(rt, 338, 258);
12535 ok(compare_color(color, 0x0000ff00, 0), "(338, 258) has color 0x%08x.\n", color);
12536 color = get_surface_color(rt, 342, 258);
12537 ok(compare_color(color, 0x00ffffff, 0), "(342, 258) has color 0x%08x.\n", color);
12538 color = get_surface_color(rt, 338, 262);
12539 ok(compare_color(color, 0x00ffffff, 0), "(338, 262) has color 0x%08x.\n", color);
12540 color = get_surface_color(rt, 342, 262);
12541 ok(compare_color(color, 0x00ffffff, 0), "(342, 262) has color 0x%08x.\n", color);
12543 /* COLORWRITEENABLE, SRGBWRITEENABLE and scissor rectangles do not exist
12544 * in d3d2. */
12546 IDirect3DViewport2_Release(viewport3);
12547 IDirect3DViewport2_Release(viewport2);
12548 IDirect3DViewport2_Release(viewport);
12549 IDirect3DMaterial2_Release(white);
12550 IDirect3DMaterial2_Release(red);
12551 IDirect3DMaterial2_Release(green);
12552 IDirect3DMaterial2_Release(blue);
12553 IDirectDrawSurface_Release(rt);
12554 refcount = IDirect3DDevice2_Release(device);
12555 ok(!refcount, "Device has %u references left.\n", refcount);
12556 refcount = IDirectDraw2_Release(ddraw);
12557 ok(!refcount, "Ddraw object has %u references left.\n", refcount);
12558 DestroyWindow(window);
12561 struct enum_surfaces_param
12563 IDirectDrawSurface *surfaces[8];
12564 unsigned int count;
12567 static HRESULT WINAPI enum_surfaces_cb(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
12569 struct enum_surfaces_param *param = context;
12570 BOOL found = FALSE;
12571 unsigned int i;
12573 for (i = 0; i < ARRAY_SIZE(param->surfaces); ++i)
12575 if (param->surfaces[i] == surface)
12577 found = TRUE;
12578 break;
12582 ok(found, "Unexpected surface %p enumerated.\n", surface);
12583 IDirectDrawSurface_Release(surface);
12584 ++param->count;
12586 return DDENUMRET_OK;
12589 static void test_enum_surfaces(void)
12591 struct enum_surfaces_param param = {{0}};
12592 IDirectDraw2 *ddraw;
12593 DDSURFACEDESC desc;
12594 HRESULT hr;
12596 ddraw = create_ddraw();
12597 ok(!!ddraw, "Failed to create a ddraw object.\n");
12599 hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
12600 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
12602 memset(&desc, 0, sizeof(desc));
12603 desc.dwSize = sizeof(desc);
12604 desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
12605 desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
12606 U2(desc).dwMipMapCount = 3;
12607 desc.dwWidth = 32;
12608 desc.dwHeight = 32;
12609 hr = IDirectDraw2_CreateSurface(ddraw, &desc, &param.surfaces[0], NULL);
12610 ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
12612 hr = IDirectDrawSurface_GetAttachedSurface(param.surfaces[0], &desc.ddsCaps, &param.surfaces[1]);
12613 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
12614 hr = IDirectDrawSurface_GetAttachedSurface(param.surfaces[1], &desc.ddsCaps, &param.surfaces[2]);
12615 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
12616 hr = IDirectDrawSurface_GetAttachedSurface(param.surfaces[2], &desc.ddsCaps, &param.surfaces[3]);
12617 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
12618 ok(!param.surfaces[3], "Got unexpected pointer %p.\n", param.surfaces[3]);
12620 hr = IDirectDraw2_EnumSurfaces(ddraw, DDENUMSURFACES_DOESEXIST | DDENUMSURFACES_ALL,
12621 &desc, &param, enum_surfaces_cb);
12622 ok(SUCCEEDED(hr), "Failed to enumerate surfaces, hr %#x.\n", hr);
12623 ok(param.count == 3, "Got unexpected number of enumerated surfaces %u.\n", param.count);
12625 param.count = 0;
12626 hr = IDirectDraw2_EnumSurfaces(ddraw, DDENUMSURFACES_DOESEXIST | DDENUMSURFACES_ALL,
12627 NULL, &param, enum_surfaces_cb);
12628 ok(SUCCEEDED(hr), "Failed to enumerate surfaces, hr %#x.\n", hr);
12629 ok(param.count == 3, "Got unexpected number of enumerated surfaces %u.\n", param.count);
12631 IDirectDrawSurface_Release(param.surfaces[2]);
12632 IDirectDrawSurface_Release(param.surfaces[1]);
12633 IDirectDrawSurface_Release(param.surfaces[0]);
12634 IDirectDraw2_Release(ddraw);
12637 START_TEST(ddraw2)
12639 DDDEVICEIDENTIFIER identifier;
12640 DEVMODEW current_mode;
12641 IDirectDraw2 *ddraw;
12642 HMODULE dwmapi;
12644 if (!(ddraw = create_ddraw()))
12646 skip("Failed to create a ddraw object, skipping tests.\n");
12647 return;
12650 if (ddraw_get_identifier(ddraw, &identifier))
12652 trace("Driver string: \"%s\"\n", identifier.szDriver);
12653 trace("Description string: \"%s\"\n", identifier.szDescription);
12654 trace("Driver version %d.%d.%d.%d\n",
12655 HIWORD(U(identifier.liDriverVersion).HighPart), LOWORD(U(identifier.liDriverVersion).HighPart),
12656 HIWORD(U(identifier.liDriverVersion).LowPart), LOWORD(U(identifier.liDriverVersion).LowPart));
12658 IDirectDraw2_Release(ddraw);
12660 memset(&current_mode, 0, sizeof(current_mode));
12661 current_mode.dmSize = sizeof(current_mode);
12662 ok(EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &current_mode), "Failed to get display mode.\n");
12663 registry_mode.dmSize = sizeof(registry_mode);
12664 ok(EnumDisplaySettingsW(NULL, ENUM_REGISTRY_SETTINGS, &registry_mode), "Failed to get display mode.\n");
12665 if (registry_mode.dmPelsWidth != current_mode.dmPelsWidth
12666 || registry_mode.dmPelsHeight != current_mode.dmPelsHeight)
12668 skip("Current mode does not match registry mode, skipping test.\n");
12669 return;
12672 if ((dwmapi = LoadLibraryA("dwmapi.dll")))
12673 pDwmIsCompositionEnabled = (void *)GetProcAddress(dwmapi, "DwmIsCompositionEnabled");
12675 test_coop_level_create_device_window();
12676 test_clipper_blt();
12677 test_coop_level_d3d_state();
12678 test_surface_interface_mismatch();
12679 test_coop_level_threaded();
12680 test_depth_blit();
12681 test_texture_load_ckey();
12682 test_viewport();
12683 test_zenable();
12684 test_ck_rgba();
12685 test_ck_default();
12686 test_ck_complex();
12687 test_surface_qi();
12688 test_device_qi();
12689 test_wndproc();
12690 test_window_style();
12691 test_redundant_mode_set();
12692 test_coop_level_mode_set();
12693 test_coop_level_mode_set_multi();
12694 test_initialize();
12695 test_coop_level_surf_create();
12696 test_coop_level_multi_window();
12697 test_clear_rect_count();
12698 test_coop_level_versions();
12699 test_lighting_interface_versions();
12700 test_coop_level_activateapp();
12701 test_unsupported_formats();
12702 test_rt_caps();
12703 test_primary_caps();
12704 test_surface_lock();
12705 test_surface_discard();
12706 test_flip();
12707 test_set_surface_desc();
12708 test_user_memory_getdc();
12709 test_sysmem_overlay();
12710 test_primary_palette();
12711 test_surface_attachment();
12712 test_pixel_format();
12713 test_create_surface_pitch();
12714 test_mipmap();
12715 test_palette_complex();
12716 test_p8_blit();
12717 test_material();
12718 test_lighting();
12719 test_specular_lighting();
12720 test_palette_gdi();
12721 test_palette_alpha();
12722 test_lost_device();
12723 test_surface_desc_lock();
12724 test_texturemapblend();
12725 test_viewport_clear_rect();
12726 test_color_fill();
12727 test_colorkey_precision();
12728 test_range_colorkey();
12729 test_shademode();
12730 test_lockrect_invalid();
12731 test_yv12_overlay();
12732 test_offscreen_overlay();
12733 test_overlay_rect();
12734 test_blt();
12735 test_blt_z_alpha();
12736 test_getdc();
12737 test_draw_primitive();
12738 test_edge_antialiasing_blending();
12739 test_transform_vertices();
12740 test_display_mode_surface_pixel_format();
12741 test_surface_desc_size();
12742 test_ck_operation();
12743 test_set_render_state();
12744 test_depth_readback();
12745 test_clear();
12746 test_enum_surfaces();