TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / ddraw / tests / ddraw4.c
blob78e2aa09b0b9720d15bcc4cc7563394b81eee948
1 /*
2 * Copyright 2011-2014 Henri Verbeet for CodeWeavers
3 * Copyright 2012-2014 Stefan Dösinger for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define COBJMACROS
21 #include "wine/test.h"
22 #include <limits.h>
23 #include <math.h>
24 #include "d3d.h"
26 static DEVMODEW registry_mode;
28 struct vec2
30 float x, y;
33 struct vec3
35 float x, y, z;
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_float(float f, float g, unsigned int ulps)
53 int x = *(int *)&f;
54 int y = *(int *)&g;
56 if (x < 0)
57 x = INT_MIN - x;
58 if (y < 0)
59 y = INT_MIN - y;
61 if (abs(x - y) > ulps)
62 return FALSE;
64 return TRUE;
67 static BOOL compare_vec4(struct vec4 *vec, float x, float y, float z, float w, unsigned int ulps)
69 return compare_float(vec->x, x, ulps)
70 && compare_float(vec->y, y, ulps)
71 && compare_float(vec->z, z, ulps)
72 && compare_float(vec->w, w, ulps);
75 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
77 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
78 c1 >>= 8; c2 >>= 8;
79 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
80 c1 >>= 8; c2 >>= 8;
81 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
82 c1 >>= 8; c2 >>= 8;
83 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
84 return TRUE;
87 static DWORD WINAPI create_window_thread_proc(void *param)
89 struct create_window_thread_param *p = param;
90 DWORD res;
91 BOOL ret;
93 p->window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
94 0, 0, 640, 480, 0, 0, 0, 0);
95 ret = SetEvent(p->window_created);
96 ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
98 for (;;)
100 MSG msg;
102 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
103 DispatchMessageA(&msg);
104 res = WaitForSingleObject(p->destroy_window, 100);
105 if (res == WAIT_OBJECT_0)
106 break;
107 if (res != WAIT_TIMEOUT)
109 ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
110 break;
114 DestroyWindow(p->window);
116 return 0;
119 static void create_window_thread(struct create_window_thread_param *p)
121 DWORD res, tid;
123 p->window_created = CreateEventA(NULL, FALSE, FALSE, NULL);
124 ok(!!p->window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
125 p->destroy_window = CreateEventA(NULL, FALSE, FALSE, NULL);
126 ok(!!p->destroy_window, "CreateEvent failed, last error %#x.\n", GetLastError());
127 p->thread = CreateThread(NULL, 0, create_window_thread_proc, p, 0, &tid);
128 ok(!!p->thread, "Failed to create thread, last error %#x.\n", GetLastError());
129 res = WaitForSingleObject(p->window_created, INFINITE);
130 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
133 static void destroy_window_thread(struct create_window_thread_param *p)
135 SetEvent(p->destroy_window);
136 WaitForSingleObject(p->thread, INFINITE);
137 CloseHandle(p->destroy_window);
138 CloseHandle(p->window_created);
139 CloseHandle(p->thread);
142 static IDirectDrawSurface4 *get_depth_stencil(IDirect3DDevice3 *device)
144 IDirectDrawSurface4 *rt, *ret;
145 DDSCAPS2 caps = {DDSCAPS_ZBUFFER, 0, 0, {0}};
146 HRESULT hr;
148 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
149 ok(SUCCEEDED(hr), "Failed to get the render target, hr %#x.\n", hr);
150 hr = IDirectDrawSurface4_GetAttachedSurface(rt, &caps, &ret);
151 ok(SUCCEEDED(hr) || hr == DDERR_NOTFOUND, "Failed to get the z buffer, hr %#x.\n", hr);
152 IDirectDrawSurface4_Release(rt);
153 return ret;
156 static HRESULT set_display_mode(IDirectDraw4 *ddraw, DWORD width, DWORD height)
158 if (SUCCEEDED(IDirectDraw4_SetDisplayMode(ddraw, width, height, 32, 0, 0)))
159 return DD_OK;
160 return IDirectDraw4_SetDisplayMode(ddraw, width, height, 24, 0, 0);
163 static D3DCOLOR get_surface_color(IDirectDrawSurface4 *surface, UINT x, UINT y)
165 RECT rect = {x, y, x + 1, y + 1};
166 DDSURFACEDESC2 surface_desc;
167 D3DCOLOR color;
168 HRESULT hr;
170 memset(&surface_desc, 0, sizeof(surface_desc));
171 surface_desc.dwSize = sizeof(surface_desc);
173 hr = IDirectDrawSurface4_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
174 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
175 if (FAILED(hr))
176 return 0xdeadbeef;
178 color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
180 hr = IDirectDrawSurface4_Unlock(surface, &rect);
181 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
183 return color;
186 static HRESULT CALLBACK enum_z_fmt(DDPIXELFORMAT *format, void *ctx)
188 DDPIXELFORMAT *z_fmt = ctx;
190 if (U1(*format).dwZBufferBitDepth > U1(*z_fmt).dwZBufferBitDepth)
191 *z_fmt = *format;
193 return DDENUMRET_OK;
196 static IDirectDraw4 *create_ddraw(void)
198 IDirectDraw4 *ddraw4;
199 IDirectDraw *ddraw1;
200 HRESULT hr;
202 if (FAILED(DirectDrawCreate(NULL, &ddraw1, NULL)))
203 return NULL;
205 hr = IDirectDraw_QueryInterface(ddraw1, &IID_IDirectDraw4, (void **)&ddraw4);
206 IDirectDraw_Release(ddraw1);
207 if (FAILED(hr))
208 return NULL;
210 return ddraw4;
213 static IDirect3DDevice3 *create_device(HWND window, DWORD coop_level)
215 IDirectDrawSurface4 *surface, *ds;
216 IDirect3DDevice3 *device = NULL;
217 DDSURFACEDESC2 surface_desc;
218 IDirectDraw4 *ddraw4;
219 DDPIXELFORMAT z_fmt;
220 IDirect3D3 *d3d3;
221 HRESULT hr;
223 if (!(ddraw4 = create_ddraw()))
224 return NULL;
226 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, coop_level);
227 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
229 memset(&surface_desc, 0, sizeof(surface_desc));
230 surface_desc.dwSize = sizeof(surface_desc);
231 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
232 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
233 surface_desc.dwWidth = 640;
234 surface_desc.dwHeight = 480;
236 hr = IDirectDraw4_CreateSurface(ddraw4, &surface_desc, &surface, NULL);
237 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
239 if (coop_level & DDSCL_NORMAL)
241 IDirectDrawClipper *clipper;
243 hr = IDirectDraw4_CreateClipper(ddraw4, 0, &clipper, NULL);
244 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
245 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
246 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
247 hr = IDirectDrawSurface4_SetClipper(surface, clipper);
248 ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
249 IDirectDrawClipper_Release(clipper);
252 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirect3D3, (void **)&d3d3);
253 IDirectDraw4_Release(ddraw4);
254 if (FAILED(hr))
256 IDirectDrawSurface4_Release(surface);
257 return NULL;
260 memset(&z_fmt, 0, sizeof(z_fmt));
261 hr = IDirect3D3_EnumZBufferFormats(d3d3, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
262 if (FAILED(hr) || !z_fmt.dwSize)
264 IDirect3D3_Release(d3d3);
265 IDirectDrawSurface4_Release(surface);
266 return NULL;
269 memset(&surface_desc, 0, sizeof(surface_desc));
270 surface_desc.dwSize = sizeof(surface_desc);
271 surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
272 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
273 U4(surface_desc).ddpfPixelFormat = z_fmt;
274 surface_desc.dwWidth = 640;
275 surface_desc.dwHeight = 480;
276 hr = IDirectDraw4_CreateSurface(ddraw4, &surface_desc, &ds, NULL);
277 ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
278 if (FAILED(hr))
280 IDirect3D3_Release(d3d3);
281 IDirectDrawSurface4_Release(surface);
282 return NULL;
285 hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
286 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
287 IDirectDrawSurface4_Release(ds);
288 if (FAILED(hr))
290 IDirect3D3_Release(d3d3);
291 IDirectDrawSurface4_Release(surface);
292 return NULL;
295 hr = IDirect3D3_CreateDevice(d3d3, &IID_IDirect3DHALDevice, surface, &device, NULL);
296 IDirect3D3_Release(d3d3);
297 IDirectDrawSurface4_Release(surface);
298 if (FAILED(hr))
299 return NULL;
301 return device;
304 static IDirect3DViewport3 *create_viewport(IDirect3DDevice3 *device, UINT x, UINT y, UINT w, UINT h)
306 IDirect3DViewport3 *viewport;
307 D3DVIEWPORT2 vp;
308 IDirect3D3 *d3d;
309 HRESULT hr;
311 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
312 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
313 hr = IDirect3D3_CreateViewport(d3d, &viewport, NULL);
314 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
315 hr = IDirect3DDevice3_AddViewport(device, viewport);
316 ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
317 memset(&vp, 0, sizeof(vp));
318 vp.dwSize = sizeof(vp);
319 vp.dwX = x;
320 vp.dwY = y;
321 vp.dwWidth = w;
322 vp.dwHeight = h;
323 vp.dvClipX = -1.0f;
324 vp.dvClipY = 1.0f;
325 vp.dvClipWidth = 2.0f;
326 vp.dvClipHeight = 2.0f;
327 vp.dvMinZ = 0.0f;
328 vp.dvMaxZ = 1.0f;
329 hr = IDirect3DViewport3_SetViewport2(viewport, &vp);
330 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
331 IDirect3D3_Release(d3d);
333 return viewport;
336 static void destroy_viewport(IDirect3DDevice3 *device, IDirect3DViewport3 *viewport)
338 HRESULT hr;
340 hr = IDirect3DDevice3_DeleteViewport(device, viewport);
341 ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
342 IDirect3DViewport3_Release(viewport);
345 static IDirect3DMaterial3 *create_material(IDirect3DDevice3 *device, D3DMATERIAL *mat)
347 IDirect3DMaterial3 *material;
348 IDirect3D3 *d3d;
349 HRESULT hr;
351 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
352 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
353 hr = IDirect3D3_CreateMaterial(d3d, &material, NULL);
354 ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
355 hr = IDirect3DMaterial3_SetMaterial(material, mat);
356 ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
357 IDirect3D3_Release(d3d);
359 return material;
362 static IDirect3DMaterial3 *create_diffuse_material(IDirect3DDevice3 *device, float r, float g, float b, float a)
364 D3DMATERIAL mat;
366 memset(&mat, 0, sizeof(mat));
367 mat.dwSize = sizeof(mat);
368 U1(U(mat).diffuse).r = r;
369 U2(U(mat).diffuse).g = g;
370 U3(U(mat).diffuse).b = b;
371 U4(U(mat).diffuse).a = a;
373 return create_material(device, &mat);
376 static IDirect3DMaterial3 *create_specular_material(IDirect3DDevice3 *device,
377 float r, float g, float b, float a, float power)
379 D3DMATERIAL mat;
381 memset(&mat, 0, sizeof(mat));
382 mat.dwSize = sizeof(mat);
383 U1(U2(mat).specular).r = r;
384 U2(U2(mat).specular).g = g;
385 U3(U2(mat).specular).b = b;
386 U4(U2(mat).specular).a = a;
387 U4(mat).power = power;
389 return create_material(device, &mat);
392 static IDirect3DMaterial3 *create_emissive_material(IDirect3DDevice3 *device, float r, float g, float b, float a)
394 D3DMATERIAL mat;
396 memset(&mat, 0, sizeof(mat));
397 mat.dwSize = sizeof(mat);
398 U1(U3(mat).emissive).r = r;
399 U2(U3(mat).emissive).g = g;
400 U3(U3(mat).emissive).b = b;
401 U4(U3(mat).emissive).a = a;
403 return create_material(device, &mat);
406 static void destroy_material(IDirect3DMaterial3 *material)
408 IDirect3DMaterial3_Release(material);
411 struct message
413 UINT message;
414 BOOL check_wparam;
415 WPARAM expect_wparam;
418 static const struct message *expect_messages;
420 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
422 if (expect_messages && message == expect_messages->message)
424 if (expect_messages->check_wparam)
425 ok (wparam == expect_messages->expect_wparam,
426 "Got unexpected wparam %lx for message %x, expected %lx.\n",
427 wparam, message, expect_messages->expect_wparam);
429 ++expect_messages;
432 return DefWindowProcA(hwnd, message, wparam, lparam);
435 /* Set the wndproc back to what ddraw expects it to be, and release the ddraw
436 * interface. This prevents subsequent SetCooperativeLevel() calls on a
437 * different window from failing with DDERR_HWNDALREADYSET. */
438 static void fix_wndproc(HWND window, LONG_PTR proc)
440 IDirectDraw4 *ddraw;
441 HRESULT hr;
443 if (!(ddraw = create_ddraw()))
444 return;
446 SetWindowLongPtrA(window, GWLP_WNDPROC, proc);
447 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
448 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
449 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
450 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
452 IDirectDraw4_Release(ddraw);
455 static void test_process_vertices(void)
457 IDirect3DVertexBuffer *src_vb, *dst_vb;
458 IDirect3DViewport3 *viewport;
459 D3DVERTEXBUFFERDESC vb_desc;
460 IDirect3DDevice3 *device;
461 struct vec3 *src_data;
462 struct vec4 *dst_data;
463 IDirect3D3 *d3d3;
464 D3DVIEWPORT2 vp2;
465 D3DVIEWPORT vp1;
466 HWND window;
467 HRESULT hr;
469 static D3DMATRIX identity =
471 1.0f, 0.0f, 0.0f, 0.0f,
472 0.0f, 1.0f, 0.0f, 0.0f,
473 0.0f, 0.0f, 1.0f, 0.0f,
474 0.0f, 0.0f, 0.0f, 1.0f,
476 static D3DMATRIX projection =
478 1.0f, 0.0f, 0.0f, 0.0f,
479 0.0f, 1.0f, 0.0f, 0.0f,
480 0.0f, 0.0f, 1.0f, 0.0f,
481 6.0f, 7.0f, 8.0f, 1.0f,
484 window = CreateWindowA("static", "d3d7_test", WS_OVERLAPPEDWINDOW,
485 0, 0, 640, 480, 0, 0, 0, 0);
486 if (!(device = create_device(window, DDSCL_NORMAL)))
488 skip("Failed to create a 3D device, skipping test.\n");
489 DestroyWindow(window);
490 return;
493 hr = IDirect3DDevice3_GetDirect3D(device, &d3d3);
494 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
496 memset(&vb_desc, 0, sizeof(vb_desc));
497 vb_desc.dwSize = sizeof(vb_desc);
498 vb_desc.dwFVF = D3DFVF_XYZ;
499 vb_desc.dwNumVertices = 3;
500 hr = IDirect3D3_CreateVertexBuffer(d3d3, &vb_desc, &src_vb, 0, NULL);
501 ok(SUCCEEDED(hr), "Failed to create source vertex buffer, hr %#x.\n", hr);
503 hr = IDirect3DVertexBuffer_Lock(src_vb, DDLOCK_WRITEONLY, (void **)&src_data, NULL);
504 ok(SUCCEEDED(hr), "Failed to lock source vertex buffer, hr %#x.\n", hr);
505 src_data[0].x = -1.0f;
506 src_data[0].y = -1.0f;
507 src_data[0].z = -1.0f;
508 src_data[1].x = 0.0f;
509 src_data[1].y = 0.0f;
510 src_data[1].z = 0.0f;
511 src_data[2].x = 1.0f;
512 src_data[2].y = 1.0f;
513 src_data[2].z = 1.0f;
514 hr = IDirect3DVertexBuffer_Unlock(src_vb);
515 ok(SUCCEEDED(hr), "Failed to unlock source vertex buffer, hr %#x.\n", hr);
517 memset(&vb_desc, 0, sizeof(vb_desc));
518 vb_desc.dwSize = sizeof(vb_desc);
519 vb_desc.dwFVF = D3DFVF_XYZRHW;
520 vb_desc.dwNumVertices = 3;
521 hr = IDirect3D3_CreateVertexBuffer(d3d3, &vb_desc, &dst_vb, 0, NULL);
522 ok(SUCCEEDED(hr), "Failed to create destination vertex buffer, hr %#x.\n", hr);
524 hr = IDirect3D3_CreateViewport(d3d3, &viewport, NULL);
525 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
526 hr = IDirect3DDevice3_AddViewport(device, viewport);
527 ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
528 vp2.dwSize = sizeof(vp2);
529 vp2.dwX = 10;
530 vp2.dwY = 20;
531 vp2.dwWidth = 100;
532 vp2.dwHeight = 200;
533 vp2.dvClipX = 2.0f;
534 vp2.dvClipY = 3.0f;
535 vp2.dvClipWidth = 4.0f;
536 vp2.dvClipHeight = 5.0f;
537 vp2.dvMinZ = -2.0f;
538 vp2.dvMaxZ = 3.0f;
539 hr = IDirect3DViewport3_SetViewport2(viewport, &vp2);
540 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
541 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
542 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
544 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &identity);
545 ok(SUCCEEDED(hr), "Failed to set world transformation, hr %#x.\n", hr);
546 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &identity);
547 ok(SUCCEEDED(hr), "Failed to set view transformation, hr %#x.\n", hr);
548 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &identity);
549 ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
551 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
552 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
554 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
555 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
556 ok(compare_vec4(&dst_data[0], -6.500e+1f, +1.800e+2f, +2.000e-1f, +1.000e+0f, 4096),
557 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
558 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
559 ok(compare_vec4(&dst_data[1], -4.000e+1f, +1.400e+2f, +4.000e-1f, +1.000e+0f, 4096),
560 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
561 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
562 ok(compare_vec4(&dst_data[2], -1.500e+1f, +1.000e+2f, +6.000e-1f, +1.000e+0f, 4096),
563 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
564 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
565 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
566 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
568 hr = IDirect3DDevice3_MultiplyTransform(device, D3DTRANSFORMSTATE_PROJECTION, &projection);
569 ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
571 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
572 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
574 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
575 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
576 ok(compare_vec4(&dst_data[0], +8.500e+1f, -1.000e+2f, +1.800e+0f, +1.000e+0f, 4096),
577 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
578 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
579 ok(compare_vec4(&dst_data[1], +1.100e+2f, -1.400e+2f, +2.000e+0f, +1.000e+0f, 4096),
580 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
581 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
582 ok(compare_vec4(&dst_data[2], +1.350e+2f, -1.800e+2f, +2.200e+0f, +1.000e+0f, 4096),
583 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
584 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
585 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
586 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
588 vp2.dwSize = sizeof(vp2);
589 vp2.dwX = 30;
590 vp2.dwY = 40;
591 vp2.dwWidth = 90;
592 vp2.dwHeight = 80;
593 vp2.dvClipX = 4.0f;
594 vp2.dvClipY = 6.0f;
595 vp2.dvClipWidth = 2.0f;
596 vp2.dvClipHeight = 4.0f;
597 vp2.dvMinZ = 3.0f;
598 vp2.dvMaxZ = -2.0f;
599 hr = IDirect3DViewport3_SetViewport2(viewport, &vp2);
600 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
602 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
603 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
605 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
606 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
607 ok(compare_vec4(&dst_data[0], +7.500e+1f, +4.000e+1f, -8.000e-1f, +1.000e+0f, 4096),
608 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
609 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
610 ok(compare_vec4(&dst_data[1], +1.200e+2f, +2.000e+1f, -1.000e+0f, +1.000e+0f, 4096),
611 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
612 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
613 ok(compare_vec4(&dst_data[2], +1.650e+2f, +0.000e+0f, -1.200e+0f, +1.000e+0f, 4096),
614 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
615 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
616 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
617 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
619 vp1.dwSize = sizeof(vp1);
620 vp1.dwX = 30;
621 vp1.dwY = 40;
622 vp1.dwWidth = 90;
623 vp1.dwHeight = 80;
624 vp1.dvScaleX = 7.0f;
625 vp1.dvScaleY = 2.0f;
626 vp1.dvMaxX = 6.0f;
627 vp1.dvMaxY = 10.0f;
628 vp1.dvMinZ = -2.0f;
629 vp1.dvMaxZ = 3.0f;
630 hr = IDirect3DViewport3_SetViewport(viewport, &vp1);
631 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
633 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
634 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
636 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
637 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
638 ok(compare_vec4(&dst_data[0], +1.100e+2f, +6.800e+1f, +7.000e+0f, +1.000e+0f, 4096),
639 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
640 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
641 ok(compare_vec4(&dst_data[1], +1.170e+2f, +6.600e+1f, +8.000e+0f, +1.000e+0f, 4096),
642 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
643 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
644 ok(compare_vec4(&dst_data[2], +1.240e+2f, +6.400e+1f, +9.000e+0f, +1.000e+0f, 4096),
645 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
646 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
647 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
648 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
650 hr = IDirect3DDevice3_DeleteViewport(device, viewport);
651 ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
653 IDirect3DVertexBuffer_Release(dst_vb);
654 IDirect3DVertexBuffer_Release(src_vb);
655 IDirect3DViewport3_Release(viewport);
656 IDirect3D3_Release(d3d3);
657 IDirect3DDevice3_Release(device);
658 DestroyWindow(window);
661 static void test_coop_level_create_device_window(void)
663 HWND focus_window, device_window;
664 IDirectDraw4 *ddraw;
665 HRESULT hr;
667 focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
668 0, 0, 640, 480, 0, 0, 0, 0);
669 ddraw = create_ddraw();
670 ok(!!ddraw, "Failed to create a ddraw object.\n");
672 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
673 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
674 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
675 ok(!device_window, "Unexpected device window found.\n");
676 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
677 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
678 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
679 ok(!device_window, "Unexpected device window found.\n");
680 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
681 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
682 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
683 ok(!device_window, "Unexpected device window found.\n");
684 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
685 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
686 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
687 ok(!device_window, "Unexpected device window found.\n");
688 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
689 ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
690 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
691 ok(!device_window, "Unexpected device window found.\n");
693 /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
694 if (broken(hr == DDERR_INVALIDPARAMS))
696 win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
697 IDirectDraw4_Release(ddraw);
698 DestroyWindow(focus_window);
699 return;
702 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
703 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
704 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
705 ok(!device_window, "Unexpected device window found.\n");
706 hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
707 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
708 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
709 ok(!device_window, "Unexpected device window found.\n");
711 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
712 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
713 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
714 ok(!device_window, "Unexpected device window found.\n");
715 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
716 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
717 ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
718 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
719 ok(!!device_window, "Device window not found.\n");
721 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
722 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
723 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
724 ok(!device_window, "Unexpected device window found.\n");
725 hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
726 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
727 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
728 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
729 ok(!!device_window, "Device window not found.\n");
731 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
732 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
733 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
734 ok(!device_window, "Unexpected device window found.\n");
735 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
736 ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
737 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
738 ok(!device_window, "Unexpected device window found.\n");
739 hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
740 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
741 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
742 ok(!device_window, "Unexpected device window found.\n");
743 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
744 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
745 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
746 ok(!!device_window, "Device window not found.\n");
748 IDirectDraw4_Release(ddraw);
749 DestroyWindow(focus_window);
752 static void test_clipper_blt(void)
754 IDirectDrawSurface4 *src_surface, *dst_surface;
755 RECT client_rect, src_rect;
756 IDirectDrawClipper *clipper;
757 DDSURFACEDESC2 surface_desc;
758 unsigned int i, j, x, y;
759 IDirectDraw4 *ddraw;
760 RGNDATA *rgn_data;
761 D3DCOLOR color;
762 ULONG refcount;
763 HRGN r1, r2;
764 HWND window;
765 DDBLTFX fx;
766 HRESULT hr;
767 DWORD *ptr;
768 DWORD ret;
770 static const DWORD src_data[] =
772 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
773 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
774 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
776 static const D3DCOLOR expected1[] =
778 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
779 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
780 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
781 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
783 /* Nvidia on Windows seems to have an off-by-one error
784 * when processing source rectangles. Our left = 1 and
785 * right = 5 input reads from x = {1, 2, 3}. x = 4 is
786 * read as well, but only for the edge pixels on the
787 * output image. The bug happens on the y axis as well,
788 * but we only read one row there, and all source rows
789 * contain the same data. This bug is not dependent on
790 * the presence of a clipper. */
791 static const D3DCOLOR expected1_broken[] =
793 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
794 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
795 0x00000000, 0x00000000, 0x00ff0000, 0x00ff0000,
796 0x00000000, 0x00000000, 0x0000ff00, 0x00ff0000,
798 static const D3DCOLOR expected2[] =
800 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
801 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
802 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
803 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
806 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
807 10, 10, 640, 480, 0, 0, 0, 0);
808 ShowWindow(window, SW_SHOW);
809 ddraw = create_ddraw();
810 ok(!!ddraw, "Failed to create a ddraw object.\n");
812 ret = GetClientRect(window, &client_rect);
813 ok(ret, "Failed to get client rect.\n");
814 ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
815 ok(ret, "Failed to map client rect.\n");
817 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
818 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
820 hr = IDirectDraw4_CreateClipper(ddraw, 0, &clipper, NULL);
821 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
822 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
823 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
824 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
825 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
826 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
827 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
828 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
829 hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
830 ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
831 ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
832 ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
833 ok(rgn_data->rdh.nCount >= 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
834 ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
835 "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
836 rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
837 rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
838 client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
839 HeapFree(GetProcessHeap(), 0, rgn_data);
841 r1 = CreateRectRgn(0, 0, 320, 240);
842 ok(!!r1, "Failed to create region.\n");
843 r2 = CreateRectRgn(320, 240, 640, 480);
844 ok(!!r2, "Failed to create region.\n");
845 CombineRgn(r1, r1, r2, RGN_OR);
846 ret = GetRegionData(r1, 0, NULL);
847 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
848 ret = GetRegionData(r1, ret, rgn_data);
849 ok(!!ret, "Failed to get region data.\n");
851 DeleteObject(r2);
852 DeleteObject(r1);
854 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
855 ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
856 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
857 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
858 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
859 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
861 HeapFree(GetProcessHeap(), 0, rgn_data);
863 memset(&surface_desc, 0, sizeof(surface_desc));
864 surface_desc.dwSize = sizeof(surface_desc);
865 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
866 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
867 surface_desc.dwWidth = 640;
868 surface_desc.dwHeight = 480;
869 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
870 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
871 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
872 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
873 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
874 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
876 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
877 ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
878 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
879 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
881 memset(&fx, 0, sizeof(fx));
882 fx.dwSize = sizeof(fx);
883 hr = IDirectDrawSurface4_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
884 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
885 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
886 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
888 hr = IDirectDrawSurface4_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
889 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
890 ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
891 ptr = surface_desc.lpSurface;
892 memcpy(&ptr[ 0], &src_data[ 0], 6 * sizeof(DWORD));
893 memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
894 memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
895 hr = IDirectDrawSurface4_Unlock(src_surface, NULL);
896 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
898 hr = IDirectDrawSurface4_SetClipper(dst_surface, clipper);
899 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
901 SetRect(&src_rect, 1, 1, 5, 2);
902 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
903 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
904 for (i = 0; i < 4; ++i)
906 for (j = 0; j < 4; ++j)
908 x = 80 * ((2 * j) + 1);
909 y = 60 * ((2 * i) + 1);
910 color = get_surface_color(dst_surface, x, y);
911 ok(compare_color(color, expected1[i * 4 + j], 1)
912 || broken(compare_color(color, expected1_broken[i * 4 + j], 1)),
913 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
917 U5(fx).dwFillColor = 0xff0000ff;
918 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
919 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
920 for (i = 0; i < 4; ++i)
922 for (j = 0; j < 4; ++j)
924 x = 80 * ((2 * j) + 1);
925 y = 60 * ((2 * i) + 1);
926 color = get_surface_color(dst_surface, x, y);
927 ok(compare_color(color, expected2[i * 4 + j], 1),
928 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
932 hr = IDirectDrawSurface4_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
933 ok(hr == DDERR_BLTFASTCANTCLIP, "Got unexpected hr %#x.\n", hr);
935 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
936 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
937 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
938 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
939 DestroyWindow(window);
940 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
941 ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
942 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
943 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
944 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
945 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
946 hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
947 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
948 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
949 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
950 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
951 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
953 IDirectDrawSurface4_Release(dst_surface);
954 IDirectDrawSurface4_Release(src_surface);
955 refcount = IDirectDrawClipper_Release(clipper);
956 ok(!refcount, "Clipper has %u references left.\n", refcount);
957 IDirectDraw4_Release(ddraw);
960 static void test_coop_level_d3d_state(void)
962 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
963 IDirectDrawSurface4 *rt, *surface;
964 IDirect3DViewport3 *viewport;
965 IDirect3DDevice3 *device;
966 IDirectDraw4 *ddraw;
967 IDirect3D3 *d3d;
968 D3DCOLOR color;
969 DWORD value;
970 HWND window;
971 HRESULT hr;
973 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
974 0, 0, 640, 480, 0, 0, 0, 0);
975 if (!(device = create_device(window, DDSCL_NORMAL)))
977 skip("Failed to create a 3D device, skipping test.\n");
978 DestroyWindow(window);
979 return;
982 viewport = create_viewport(device, 0, 0, 640, 480);
984 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
985 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
986 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
987 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
988 ok(!!value, "Got unexpected z-enable state %#x.\n", value);
989 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
990 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
991 ok(!value, "Got unexpected alpha blend enable state %#x.\n", value);
992 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
993 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
994 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
995 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
996 color = get_surface_color(rt, 320, 240);
997 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
999 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1000 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1001 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1002 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1003 IDirect3D3_Release(d3d);
1004 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1005 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1006 hr = IDirectDrawSurface4_IsLost(rt);
1007 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
1008 hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
1009 ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
1010 IDirectDraw4_Release(ddraw);
1012 hr = IDirect3DDevice3_GetRenderTarget(device, &surface);
1013 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1014 ok(surface == rt, "Got unexpected surface %p.\n", surface);
1015 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
1016 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1017 ok(!!value, "Got unexpected z-enable state %#x.\n", value);
1018 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
1019 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1020 ok(!!value, "Got unexpected alpha blend enable state %#x.\n", value);
1021 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 0.0f, 0);
1022 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1023 color = get_surface_color(rt, 320, 240);
1024 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1026 destroy_viewport(device, viewport);
1027 IDirectDrawSurface4_Release(surface);
1028 IDirectDrawSurface4_Release(rt);
1029 IDirect3DDevice3_Release(device);
1030 DestroyWindow(window);
1033 static void test_surface_interface_mismatch(void)
1035 IDirectDraw4 *ddraw = NULL;
1036 IDirect3D3 *d3d = NULL;
1037 IDirectDrawSurface4 *surface = NULL, *ds;
1038 IDirectDrawSurface3 *surface3 = NULL;
1039 IDirect3DDevice3 *device = NULL;
1040 IDirect3DViewport3 *viewport = NULL;
1041 DDSURFACEDESC2 surface_desc;
1042 DDPIXELFORMAT z_fmt;
1043 ULONG refcount;
1044 HRESULT hr;
1045 D3DCOLOR color;
1046 HWND window;
1047 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1049 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1050 0, 0, 640, 480, 0, 0, 0, 0);
1051 ddraw = create_ddraw();
1052 ok(!!ddraw, "Failed to create a ddraw object.\n");
1053 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
1054 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1056 memset(&surface_desc, 0, sizeof(surface_desc));
1057 surface_desc.dwSize = sizeof(surface_desc);
1058 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1059 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
1060 surface_desc.dwWidth = 640;
1061 surface_desc.dwHeight = 480;
1063 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1064 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1066 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
1067 ok(SUCCEEDED(hr), "Failed to QI IDirectDrawSurface3, hr %#x.\n", hr);
1069 if (FAILED(IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d)))
1071 skip("D3D interface is not available, skipping test.\n");
1072 goto cleanup;
1075 memset(&z_fmt, 0, sizeof(z_fmt));
1076 hr = IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
1077 if (FAILED(hr) || !z_fmt.dwSize)
1079 skip("No depth buffer formats available, skipping test.\n");
1080 goto cleanup;
1083 memset(&surface_desc, 0, sizeof(surface_desc));
1084 surface_desc.dwSize = sizeof(surface_desc);
1085 surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
1086 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
1087 U4(surface_desc).ddpfPixelFormat = z_fmt;
1088 surface_desc.dwWidth = 640;
1089 surface_desc.dwHeight = 480;
1090 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &ds, NULL);
1091 ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
1092 if (FAILED(hr))
1093 goto cleanup;
1095 /* Using a different surface interface version still works */
1096 hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
1097 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
1098 refcount = IDirectDrawSurface4_Release(ds);
1099 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
1100 if (FAILED(hr))
1101 goto cleanup;
1103 /* Here too */
1104 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, (IDirectDrawSurface4 *)surface3, &device, NULL);
1105 ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
1106 if (FAILED(hr))
1107 goto cleanup;
1109 viewport = create_viewport(device, 0, 0, 640, 480);
1111 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
1112 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1113 color = get_surface_color(surface, 320, 240);
1114 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
1116 cleanup:
1117 if (viewport)
1118 destroy_viewport(device, viewport);
1119 if (surface3) IDirectDrawSurface3_Release(surface3);
1120 if (surface) IDirectDrawSurface4_Release(surface);
1121 if (device) IDirect3DDevice3_Release(device);
1122 if (d3d) IDirect3D3_Release(d3d);
1123 if (ddraw) IDirectDraw4_Release(ddraw);
1124 DestroyWindow(window);
1127 static void test_coop_level_threaded(void)
1129 struct create_window_thread_param p;
1130 IDirectDraw4 *ddraw;
1131 HRESULT hr;
1133 ddraw = create_ddraw();
1134 ok(!!ddraw, "Failed to create a ddraw object.\n");
1135 create_window_thread(&p);
1137 hr = IDirectDraw4_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1138 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1140 IDirectDraw4_Release(ddraw);
1141 destroy_window_thread(&p);
1144 static void test_depth_blit(void)
1146 static struct
1148 float x, y, z;
1149 DWORD color;
1151 quad1[] =
1153 { -1.0, 1.0, 0.50f, 0xff00ff00},
1154 { 1.0, 1.0, 0.50f, 0xff00ff00},
1155 { -1.0, -1.0, 0.50f, 0xff00ff00},
1156 { 1.0, -1.0, 0.50f, 0xff00ff00},
1158 static const D3DCOLOR expected_colors[4][4] =
1160 {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1161 {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1162 {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1163 {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1165 DDSURFACEDESC2 ddsd_new, ddsd_existing;
1167 IDirect3DDevice3 *device;
1168 IDirectDrawSurface4 *ds1, *ds2, *ds3, *rt;
1169 IDirect3DViewport3 *viewport;
1170 RECT src_rect, dst_rect;
1171 unsigned int i, j;
1172 D3DCOLOR color;
1173 HRESULT hr;
1174 IDirect3D3 *d3d;
1175 IDirectDraw4 *ddraw;
1176 DDBLTFX fx;
1177 HWND window;
1178 D3DRECT d3drect;
1180 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1181 0, 0, 640, 480, 0, 0, 0, 0);
1182 if (!(device = create_device(window, DDSCL_NORMAL)))
1184 skip("Failed to create a 3D device, skipping test.\n");
1185 DestroyWindow(window);
1186 return;
1189 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1190 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
1191 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1192 ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
1193 IDirect3D3_Release(d3d);
1195 ds1 = get_depth_stencil(device);
1197 memset(&ddsd_new, 0, sizeof(ddsd_new));
1198 ddsd_new.dwSize = sizeof(ddsd_new);
1199 memset(&ddsd_existing, 0, sizeof(ddsd_existing));
1200 ddsd_existing.dwSize = sizeof(ddsd_existing);
1201 hr = IDirectDrawSurface4_GetSurfaceDesc(ds1, &ddsd_existing);
1202 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
1203 ddsd_new.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
1204 ddsd_new.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
1205 ddsd_new.dwWidth = ddsd_existing.dwWidth;
1206 ddsd_new.dwHeight = ddsd_existing.dwHeight;
1207 U4(ddsd_new).ddpfPixelFormat = U4(ddsd_existing).ddpfPixelFormat;
1208 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd_new, &ds2, NULL);
1209 ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
1210 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd_new, &ds3, NULL);
1211 ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
1212 IDirectDraw4_Release(ddraw);
1214 viewport = create_viewport(device, 0, 0, ddsd_existing.dwWidth, ddsd_existing.dwHeight);
1215 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1216 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
1218 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
1219 ok(SUCCEEDED(hr), "Failed to enable z testing, hr %#x.\n", hr);
1220 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL);
1221 ok(SUCCEEDED(hr), "Failed to set the z function, hr %#x.\n", hr);
1223 U1(d3drect).x1 = U2(d3drect).y1 = 0;
1224 U3(d3drect).x2 = ddsd_existing.dwWidth; U4(d3drect).y2 = ddsd_existing.dwHeight;
1225 hr = IDirect3DViewport3_Clear2(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER, 0, 0.0f, 0);
1226 ok(SUCCEEDED(hr), "Failed to clear the z buffer, hr %#x.\n", hr);
1228 /* Partial blit. */
1229 SetRect(&src_rect, 0, 0, 320, 240);
1230 SetRect(&dst_rect, 0, 0, 320, 240);
1231 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1232 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1233 /* Different locations. */
1234 SetRect(&src_rect, 0, 0, 320, 240);
1235 SetRect(&dst_rect, 320, 240, 640, 480);
1236 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1237 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1238 /* Streched. */
1239 SetRect(&src_rect, 0, 0, 320, 240);
1240 SetRect(&dst_rect, 0, 0, 640, 480);
1241 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1242 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1243 /* Flipped. */
1244 SetRect(&src_rect, 0, 480, 640, 0);
1245 SetRect(&dst_rect, 0, 0, 640, 480);
1246 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1247 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1248 SetRect(&src_rect, 0, 0, 640, 480);
1249 SetRect(&dst_rect, 0, 480, 640, 0);
1250 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1251 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1252 /* Full, explicit. */
1253 SetRect(&src_rect, 0, 0, 640, 480);
1254 SetRect(&dst_rect, 0, 0, 640, 480);
1255 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1256 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1257 /* Depth -> color blit: Succeeds on Win7 + Radeon HD 5700, fails on WinXP + Radeon X1600 */
1259 /* Depth blit inside a BeginScene / EndScene pair */
1260 hr = IDirect3DDevice3_BeginScene(device);
1261 ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1262 /* From the current depth stencil */
1263 hr = IDirectDrawSurface4_Blt(ds2, NULL, ds1, NULL, DDBLT_WAIT, NULL);
1264 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1265 /* To the current depth stencil */
1266 hr = IDirectDrawSurface4_Blt(ds1, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1267 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1268 /* Between unbound surfaces */
1269 hr = IDirectDrawSurface4_Blt(ds3, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1270 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1271 hr = IDirect3DDevice3_EndScene(device);
1272 ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1274 /* Avoid changing the depth stencil, it doesn't work properly on Windows.
1275 * Instead use DDBLT_DEPTHFILL to clear the depth stencil. Unfortunately
1276 * drivers disagree on the meaning of dwFillDepth. Only 0 seems to produce
1277 * a reliable result(z = 0.0) */
1278 memset(&fx, 0, sizeof(fx));
1279 fx.dwSize = sizeof(fx);
1280 hr = IDirectDrawSurface4_Blt(ds2, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
1281 ok(SUCCEEDED(hr), "Failed to clear the source z buffer, hr %#x.\n", hr);
1283 hr = IDirect3DViewport3_Clear2(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET, 0xffff0000, 1.0f, 0);
1284 ok(SUCCEEDED(hr), "Failed to clear the color and z buffers, hr %#x.\n", hr);
1285 SetRect(&dst_rect, 0, 0, 320, 240);
1286 hr = IDirectDrawSurface4_Blt(ds1, &dst_rect, ds2, NULL, DDBLT_WAIT, NULL);
1287 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1288 IDirectDrawSurface4_Release(ds3);
1289 IDirectDrawSurface4_Release(ds2);
1290 IDirectDrawSurface4_Release(ds1);
1292 hr = IDirect3DDevice3_BeginScene(device);
1293 ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1294 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE,
1295 quad1, 4, 0);
1296 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1297 hr = IDirect3DDevice3_EndScene(device);
1298 ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1300 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1301 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1302 for (i = 0; i < 4; ++i)
1304 for (j = 0; j < 4; ++j)
1306 unsigned int x = 80 * ((2 * j) + 1);
1307 unsigned int y = 60 * ((2 * i) + 1);
1308 color = get_surface_color(rt, x, y);
1309 ok(compare_color(color, expected_colors[i][j], 1),
1310 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected_colors[i][j], x, y, color);
1313 IDirectDrawSurface4_Release(rt);
1315 destroy_viewport(device, viewport);
1316 IDirect3DDevice3_Release(device);
1317 DestroyWindow(window);
1320 static void test_texture_load_ckey(void)
1322 IDirectDraw4 *ddraw;
1323 IDirectDrawSurface4 *src;
1324 IDirectDrawSurface4 *dst;
1325 IDirect3DTexture2 *src_tex;
1326 IDirect3DTexture2 *dst_tex;
1327 DDSURFACEDESC2 ddsd;
1328 HRESULT hr;
1329 DDCOLORKEY ckey;
1331 ddraw = create_ddraw();
1332 ok(!!ddraw, "Failed to create a ddraw object.\n");
1333 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
1334 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1336 memset(&ddsd, 0, sizeof(ddsd));
1337 ddsd.dwSize = sizeof(ddsd);
1338 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
1339 ddsd.dwHeight = 128;
1340 ddsd.dwWidth = 128;
1341 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
1342 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &src, NULL);
1343 ok(SUCCEEDED(hr), "Failed to create source texture, hr %#x.\n", hr);
1344 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1345 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &dst, NULL);
1346 ok(SUCCEEDED(hr), "Failed to create destination texture, hr %#x.\n", hr);
1348 hr = IDirectDrawSurface4_QueryInterface(src, &IID_IDirect3DTexture2, (void **)&src_tex);
1349 ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get Direct3DTexture2 interface, hr %#x.\n", hr);
1350 if (FAILED(hr))
1352 /* 64 bit ddraw does not support d3d */
1353 skip("Could not get Direct3DTexture2 interface, skipping texture::Load color keying tests.\n");
1354 IDirectDrawSurface4_Release(dst);
1355 IDirectDrawSurface4_Release(src);
1356 IDirectDraw4_Release(ddraw);
1357 return;
1359 hr = IDirectDrawSurface4_QueryInterface(dst, &IID_IDirect3DTexture2, (void **)&dst_tex);
1360 ok(SUCCEEDED(hr), "Failed to get Direct3DTexture2 interface, hr %#x.\n", hr);
1362 /* No surface has a color key */
1363 hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1364 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1365 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0xdeadbeef;
1366 hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1367 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1368 ok(ckey.dwColorSpaceLowValue == 0xdeadbeef, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1369 ok(ckey.dwColorSpaceHighValue == 0xdeadbeef, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1371 /* Source surface has a color key */
1372 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x0000ff00;
1373 hr = IDirectDrawSurface4_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
1374 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1375 hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1376 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1377 hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1378 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1379 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1380 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1382 /* Both surfaces have a color key: Dest ckey is overwritten */
1383 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x000000ff;
1384 hr = IDirectDrawSurface4_SetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1385 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1386 hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1387 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1388 hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1389 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1390 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1391 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1393 /* Only the destination has a color key: It is not deleted */
1394 hr = IDirectDrawSurface4_SetColorKey(src, DDCKEY_SRCBLT, NULL);
1395 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1396 hr = IDirectDrawSurface4_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
1397 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1398 hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1399 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1400 hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1401 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1402 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1403 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1405 IDirect3DTexture2_Release(dst_tex);
1406 IDirect3DTexture2_Release(src_tex);
1407 IDirectDrawSurface4_Release(dst);
1408 IDirectDrawSurface4_Release(src);
1409 IDirectDraw4_Release(ddraw);
1412 static ULONG get_refcount(IUnknown *test_iface)
1414 IUnknown_AddRef(test_iface);
1415 return IUnknown_Release(test_iface);
1418 static void test_viewport(void)
1420 IDirectDraw4 *ddraw;
1421 IDirect3D3 *d3d;
1422 HRESULT hr, old_d3d_ref;
1423 ULONG ref;
1424 IDirect3DViewport *viewport;
1425 IDirect3DViewport2 *viewport2;
1426 IDirect3DViewport3 *viewport3, *another_vp, *test_vp;
1427 IDirectDrawGammaControl *gamma;
1428 IUnknown *unknown;
1429 HWND window;
1430 IDirect3DDevice3 *device;
1432 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1433 0, 0, 640, 480, 0, 0, 0, 0);
1434 if (!(device = create_device(window, DDSCL_NORMAL)))
1436 skip("Failed to create a 3D device, skipping test.\n");
1437 DestroyWindow(window);
1438 return;
1440 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1441 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
1442 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1443 ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
1444 old_d3d_ref = get_refcount((IUnknown *) d3d);
1446 hr = IDirect3D3_CreateViewport(d3d, &viewport3, NULL);
1447 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1448 ref = get_refcount((IUnknown *)viewport3);
1449 ok(ref == 1, "Initial IDirect3DViewport3 refcount is %u\n", ref);
1450 ref = get_refcount((IUnknown *)d3d);
1451 ok(ref == old_d3d_ref, "IDirect3D3 refcount is %u\n", ref);
1453 gamma = (IDirectDrawGammaControl *)0xdeadbeef;
1454 hr = IDirect3DViewport2_QueryInterface(viewport3, &IID_IDirectDrawGammaControl, (void **)&gamma);
1455 ok(hr == E_NOINTERFACE, "Got unexpected hr %#x.\n", hr);
1456 ok(gamma == NULL, "Interface not set to NULL by failed QI call: %p\n", gamma);
1457 if (SUCCEEDED(hr)) IDirectDrawGammaControl_Release(gamma);
1458 /* NULL iid: Segfaults */
1460 hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IDirect3DViewport, (void **)&viewport);
1461 ok(SUCCEEDED(hr), "Failed to QI IDirect3DViewport, hr %#x.\n", hr);
1462 if (viewport)
1464 ref = get_refcount((IUnknown *)viewport);
1465 ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1466 ref = get_refcount((IUnknown *)viewport3);
1467 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1468 IDirect3DViewport_Release(viewport);
1469 viewport = NULL;
1472 hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IDirect3DViewport3, (void **)&viewport2);
1473 ok(SUCCEEDED(hr), "Failed to QI IDirect3DViewport3, hr %#x.\n", hr);
1474 if (viewport2)
1476 ref = get_refcount((IUnknown *)viewport2);
1477 ok(ref == 2, "IDirect3DViewport2 refcount is %u\n", ref);
1478 ref = get_refcount((IUnknown *)viewport3);
1479 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1480 IDirect3DViewport3_Release(viewport2);
1483 hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IUnknown, (void **)&unknown);
1484 ok(SUCCEEDED(hr), "Failed to QI IUnknown, hr %#x.\n", hr);
1485 if (unknown)
1487 ref = get_refcount((IUnknown *)viewport3);
1488 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1489 ref = get_refcount(unknown);
1490 ok(ref == 2, "IUnknown refcount is %u\n", ref);
1491 IUnknown_Release(unknown);
1494 /* AddViewport(NULL): Segfault */
1495 hr = IDirect3DDevice3_DeleteViewport(device, NULL);
1496 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
1497 hr = IDirect3DDevice3_GetCurrentViewport(device, NULL);
1498 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1500 hr = IDirect3D3_CreateViewport(d3d, &another_vp, NULL);
1501 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1503 /* Setting a viewport not in the viewport list fails */
1504 hr = IDirect3DDevice3_SetCurrentViewport(device, another_vp);
1505 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
1507 hr = IDirect3DDevice3_AddViewport(device, viewport3);
1508 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1509 ref = get_refcount((IUnknown *) viewport3);
1510 ok(ref == 2, "viewport3 refcount is %d\n", ref);
1511 hr = IDirect3DDevice3_AddViewport(device, another_vp);
1512 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1513 ref = get_refcount((IUnknown *) another_vp);
1514 ok(ref == 2, "another_vp refcount is %d\n", ref);
1516 test_vp = (IDirect3DViewport3 *) 0xbaadc0de;
1517 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1518 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1519 ok(test_vp == (IDirect3DViewport3 *) 0xbaadc0de, "Got unexpected pointer %p\n", test_vp);
1521 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport3);
1522 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1523 ref = get_refcount((IUnknown *) viewport3);
1524 ok(ref == 3, "viewport3 refcount is %d\n", ref);
1525 ref = get_refcount((IUnknown *) device);
1526 ok(ref == 1, "device refcount is %d\n", ref);
1528 test_vp = NULL;
1529 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1530 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
1531 ok(test_vp == viewport3, "Got unexpected viewport %p\n", test_vp);
1532 ref = get_refcount((IUnknown *) viewport3);
1533 ok(ref == 4, "viewport3 refcount is %d\n", ref);
1534 if(test_vp) IDirect3DViewport3_Release(test_vp);
1536 /* GetCurrentViewport with a viewport set and NULL input param: Segfault */
1538 /* Cannot set the viewport to NULL */
1539 hr = IDirect3DDevice3_SetCurrentViewport(device, NULL);
1540 ok(hr == DDERR_INVALIDPARAMS, "Failed to set viewport to NULL, hr %#x.\n", hr);
1541 test_vp = NULL;
1542 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1543 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
1544 ok(test_vp == viewport3, "Got unexpected viewport %p\n", test_vp);
1545 if(test_vp) IDirect3DViewport3_Release(test_vp);
1547 /* SetCurrentViewport properly releases the old viewport's reference */
1548 hr = IDirect3DDevice3_SetCurrentViewport(device, another_vp);
1549 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1550 ref = get_refcount((IUnknown *) viewport3);
1551 ok(ref == 2, "viewport3 refcount is %d\n", ref);
1552 ref = get_refcount((IUnknown *) another_vp);
1553 ok(ref == 3, "another_vp refcount is %d\n", ref);
1555 /* Unlike device2::DeleteViewport, device3::DeleteViewport releases the
1556 * reference held by SetCurrentViewport */
1557 hr = IDirect3DDevice3_DeleteViewport(device, another_vp);
1558 ok(SUCCEEDED(hr), "Failed to delete viewport from device, hr %#x.\n", hr);
1559 ref = get_refcount((IUnknown *) another_vp);
1560 ok(ref == 1, "another_vp refcount is %d\n", ref);
1562 /* GetCurrentViewport still fails */
1563 test_vp = NULL;
1564 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1565 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1566 ok(test_vp == NULL, "Got unexpected viewport %p\n", test_vp);
1567 if(test_vp) IDirect3DViewport3_Release(test_vp);
1569 /* Setting a different viewport doesn't have any surprises now */
1570 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport3);
1571 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1572 ref = get_refcount((IUnknown *) viewport3);
1573 ok(ref == 3, "viewport3 refcount is %d\n", ref);
1574 ref = get_refcount((IUnknown *) another_vp);
1575 ok(ref == 1, "another_vp refcount is %d\n", ref);
1577 /* Destroying the device removes the viewport and releases the reference */
1578 IDirect3DDevice3_Release(device);
1579 ref = get_refcount((IUnknown *) viewport3);
1580 ok(ref == 1, "viewport3 refcount is %d\n", ref);
1582 ref = IDirect3DViewport3_Release(another_vp);
1583 ok(ref == 0, "Got unexpected ref %d\n", ref);
1584 ref = IDirect3DViewport3_Release(viewport3);
1585 ok(ref == 0, "Got unexpected ref %d\n", ref);
1586 IDirect3D3_Release(d3d);
1587 DestroyWindow(window);
1588 IDirectDraw4_Release(ddraw);
1591 static void test_zenable(void)
1593 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1594 static struct
1596 struct vec4 position;
1597 D3DCOLOR diffuse;
1599 tquad[] =
1601 {{ 0.0f, 480.0f, -0.5f, 1.0f}, 0xff00ff00},
1602 {{ 0.0f, 0.0f, -0.5f, 1.0f}, 0xff00ff00},
1603 {{640.0f, 480.0f, 1.5f, 1.0f}, 0xff00ff00},
1604 {{640.0f, 0.0f, 1.5f, 1.0f}, 0xff00ff00},
1606 IDirect3DViewport3 *viewport;
1607 IDirect3DDevice3 *device;
1608 IDirectDrawSurface4 *rt;
1609 D3DCOLOR color;
1610 HWND window;
1611 HRESULT hr;
1612 UINT x, y;
1613 UINT i, j;
1615 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1616 0, 0, 640, 480, 0, 0, 0, 0);
1617 if (!(device = create_device(window, DDSCL_NORMAL)))
1619 skip("Failed to create a 3D device, skipping test.\n");
1620 DestroyWindow(window);
1621 return;
1624 viewport = create_viewport(device, 0, 0, 640, 480);
1625 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1626 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1628 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
1629 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
1631 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff0000, 0.0f, 0);
1632 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1633 hr = IDirect3DDevice3_BeginScene(device);
1634 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1635 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, tquad, 4, 0);
1636 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1637 hr = IDirect3DDevice3_EndScene(device);
1638 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1640 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1641 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1642 for (i = 0; i < 4; ++i)
1644 for (j = 0; j < 4; ++j)
1646 x = 80 * ((2 * j) + 1);
1647 y = 60 * ((2 * i) + 1);
1648 color = get_surface_color(rt, x, y);
1649 ok(compare_color(color, 0x0000ff00, 1),
1650 "Expected color 0x0000ff00 at %u, %u, got 0x%08x.\n", x, y, color);
1653 IDirectDrawSurface4_Release(rt);
1655 destroy_viewport(device, viewport);
1656 IDirect3DDevice3_Release(device);
1657 DestroyWindow(window);
1660 static void test_ck_rgba(void)
1662 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1663 static struct
1665 struct vec4 position;
1666 struct vec2 texcoord;
1668 tquad[] =
1670 {{ 0.0f, 480.0f, 0.25f, 1.0f}, {0.0f, 0.0f}},
1671 {{ 0.0f, 0.0f, 0.25f, 1.0f}, {0.0f, 1.0f}},
1672 {{640.0f, 480.0f, 0.25f, 1.0f}, {1.0f, 0.0f}},
1673 {{640.0f, 0.0f, 0.25f, 1.0f}, {1.0f, 1.0f}},
1674 {{ 0.0f, 480.0f, 0.75f, 1.0f}, {0.0f, 0.0f}},
1675 {{ 0.0f, 0.0f, 0.75f, 1.0f}, {0.0f, 1.0f}},
1676 {{640.0f, 480.0f, 0.75f, 1.0f}, {1.0f, 0.0f}},
1677 {{640.0f, 0.0f, 0.75f, 1.0f}, {1.0f, 1.0f}},
1679 static const struct
1681 D3DCOLOR fill_color;
1682 BOOL color_key;
1683 BOOL blend;
1684 D3DCOLOR result1, result1_broken;
1685 D3DCOLOR result2, result2_broken;
1687 tests[] =
1689 /* r200 on Windows doesn't check the alpha component when applying the color
1690 * key, so the key matches on every texel. */
1691 {0xff00ff00, TRUE, TRUE, 0x00ff0000, 0x00ff0000, 0x000000ff, 0x000000ff},
1692 {0xff00ff00, TRUE, FALSE, 0x00ff0000, 0x00ff0000, 0x000000ff, 0x000000ff},
1693 {0xff00ff00, FALSE, TRUE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1694 {0xff00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1695 {0x7f00ff00, TRUE, TRUE, 0x00807f00, 0x00ff0000, 0x00807f00, 0x000000ff},
1696 {0x7f00ff00, TRUE, FALSE, 0x0000ff00, 0x00ff0000, 0x0000ff00, 0x000000ff},
1697 {0x7f00ff00, FALSE, TRUE, 0x00807f00, 0x00807f00, 0x00807f00, 0x00807f00},
1698 {0x7f00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1701 IDirectDrawSurface4 *surface;
1702 IDirect3DViewport3 *viewport;
1703 DDSURFACEDESC2 surface_desc;
1704 IDirect3DTexture2 *texture;
1705 IDirect3DDevice3 *device;
1706 IDirectDrawSurface4 *rt;
1707 IDirectDraw4 *ddraw;
1708 IDirect3D3 *d3d;
1709 D3DCOLOR color;
1710 HWND window;
1711 DDBLTFX fx;
1712 HRESULT hr;
1713 UINT i;
1715 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1716 0, 0, 640, 480, 0, 0, 0, 0);
1717 if (!(device = create_device(window, DDSCL_NORMAL)))
1719 skip("Failed to create a 3D device, skipping test.\n");
1720 DestroyWindow(window);
1721 return;
1724 viewport = create_viewport(device, 0, 0, 640, 480);
1725 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1726 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1728 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1729 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1730 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1731 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1732 IDirect3D3_Release(d3d);
1734 memset(&surface_desc, 0, sizeof(surface_desc));
1735 surface_desc.dwSize = sizeof(surface_desc);
1736 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1737 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1738 surface_desc.dwWidth = 256;
1739 surface_desc.dwHeight = 256;
1740 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
1741 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1742 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
1743 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1744 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1745 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
1746 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
1747 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0xff00ff00;
1748 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0xff00ff00;
1749 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1750 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
1751 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
1752 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1754 hr = IDirect3DDevice3_SetTexture(device, 0, texture);
1755 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
1756 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
1757 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1758 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
1759 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1761 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1762 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1764 for (i = 0; i < sizeof(tests) / sizeof(*tests); ++i)
1766 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, tests[i].color_key);
1767 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
1768 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, tests[i].blend);
1769 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1771 memset(&fx, 0, sizeof(fx));
1772 fx.dwSize = sizeof(fx);
1773 U5(fx).dwFillColor = tests[i].fill_color;
1774 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1775 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1777 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect,
1778 D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff0000, 1.0f, 0);
1779 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1780 hr = IDirect3DDevice3_BeginScene(device);
1781 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1782 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1783 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1784 hr = IDirect3DDevice3_EndScene(device);
1785 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1787 color = get_surface_color(rt, 320, 240);
1788 ok(compare_color(color, tests[i].result1, 1) || compare_color(color, tests[i].result1_broken, 1),
1789 "Expected color 0x%08x for test %u, got 0x%08x.\n",
1790 tests[i].result1, i, color);
1792 U5(fx).dwFillColor = 0xff0000ff;
1793 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1794 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1796 hr = IDirect3DDevice3_BeginScene(device);
1797 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1798 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[4], 4, 0);
1799 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1800 hr = IDirect3DDevice3_EndScene(device);
1801 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1803 /* This tests that fragments that are masked out by the color key are
1804 * discarded, instead of just fully transparent. */
1805 color = get_surface_color(rt, 320, 240);
1806 ok(compare_color(color, tests[i].result2, 1) || compare_color(color, tests[i].result2_broken, 1),
1807 "Expected color 0x%08x for test %u, got 0x%08x.\n",
1808 tests[i].result2, i, color);
1811 IDirectDrawSurface4_Release(rt);
1812 IDirect3DTexture2_Release(texture);
1813 IDirectDrawSurface4_Release(surface);
1814 destroy_viewport(device, viewport);
1815 IDirectDraw4_Release(ddraw);
1816 IDirect3DDevice3_Release(device);
1817 DestroyWindow(window);
1820 static void test_ck_default(void)
1822 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1823 static struct
1825 struct vec4 position;
1826 struct vec2 texcoord;
1828 tquad[] =
1830 {{ 0.0f, 480.0f, 0.0f, 1.0f}, {0.0f, 0.0f}},
1831 {{ 0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 1.0f}},
1832 {{640.0f, 480.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
1833 {{640.0f, 0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
1835 IDirectDrawSurface4 *surface, *rt;
1836 IDirect3DViewport3 *viewport;
1837 DDSURFACEDESC2 surface_desc;
1838 IDirect3DTexture2 *texture;
1839 IDirect3DDevice3 *device;
1840 IDirectDraw4 *ddraw;
1841 IDirect3D3 *d3d;
1842 D3DCOLOR color;
1843 DWORD value;
1844 HWND window;
1845 DDBLTFX fx;
1846 HRESULT hr;
1848 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1849 0, 0, 640, 480, 0, 0, 0, 0);
1851 if (!(device = create_device(window, DDSCL_NORMAL)))
1853 skip("Failed to create a 3D device, skipping test.\n");
1854 DestroyWindow(window);
1855 return;
1858 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1859 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1860 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1861 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1862 IDirect3D3_Release(d3d);
1864 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1865 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1867 viewport = create_viewport(device, 0, 0, 640, 480);
1868 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1869 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1871 memset(&surface_desc, 0, sizeof(surface_desc));
1872 surface_desc.dwSize = sizeof(surface_desc);
1873 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1874 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1875 surface_desc.dwWidth = 256;
1876 surface_desc.dwHeight = 256;
1877 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
1878 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
1879 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
1880 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1881 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1882 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
1883 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x000000ff;
1884 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x000000ff;
1885 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1886 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1887 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
1888 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1889 hr = IDirect3DDevice3_SetTexture(device, 0, texture);
1890 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
1892 memset(&fx, 0, sizeof(fx));
1893 fx.dwSize = sizeof(fx);
1894 U5(fx).dwFillColor = 0x000000ff;
1895 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1896 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
1898 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 1.0f, 0);
1899 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1900 hr = IDirect3DDevice3_BeginScene(device);
1901 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1902 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, &value);
1903 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1904 ok(!value, "Got unexpected color keying state %#x.\n", value);
1905 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1906 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1907 hr = IDirect3DDevice3_EndScene(device);
1908 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1909 color = get_surface_color(rt, 320, 240);
1910 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
1912 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 1.0f, 0);
1913 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1914 hr = IDirect3DDevice3_BeginScene(device);
1915 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1916 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
1917 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
1918 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1919 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1920 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, &value);
1921 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1922 ok(!!value, "Got unexpected color keying state %#x.\n", value);
1923 hr = IDirect3DDevice3_EndScene(device);
1924 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1925 color = get_surface_color(rt, 320, 240);
1926 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1928 IDirect3DTexture_Release(texture);
1929 IDirectDrawSurface4_Release(surface);
1930 destroy_viewport(device, viewport);
1931 IDirectDrawSurface4_Release(rt);
1932 IDirect3DDevice3_Release(device);
1933 IDirectDraw4_Release(ddraw);
1934 DestroyWindow(window);
1937 static void test_ck_complex(void)
1939 IDirectDrawSurface4 *surface, *mipmap, *tmp;
1940 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
1941 DDSURFACEDESC2 surface_desc;
1942 IDirect3DDevice3 *device;
1943 DDCOLORKEY color_key;
1944 IDirectDraw4 *ddraw;
1945 IDirect3D3 *d3d;
1946 unsigned int i;
1947 ULONG refcount;
1948 HWND window;
1949 HRESULT hr;
1951 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1952 0, 0, 640, 480, 0, 0, 0, 0);
1953 if (!(device = create_device(window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
1955 skip("Failed to create a 3D device, skipping test.\n");
1956 DestroyWindow(window);
1957 return;
1959 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1960 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1961 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1962 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1963 IDirect3D3_Release(d3d);
1965 memset(&surface_desc, 0, sizeof(surface_desc));
1966 surface_desc.dwSize = sizeof(surface_desc);
1967 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1968 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1969 surface_desc.dwWidth = 128;
1970 surface_desc.dwHeight = 128;
1971 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1972 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1974 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1975 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1976 color_key.dwColorSpaceLowValue = 0x0000ff00;
1977 color_key.dwColorSpaceHighValue = 0x0000ff00;
1978 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1979 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1980 memset(&color_key, 0, sizeof(color_key));
1981 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1982 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1983 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1984 color_key.dwColorSpaceLowValue);
1985 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1986 color_key.dwColorSpaceHighValue);
1988 mipmap = surface;
1989 IDirectDrawSurface_AddRef(mipmap);
1990 for (i = 0; i < 7; ++i)
1992 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
1993 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
1995 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1996 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
1997 color_key.dwColorSpaceLowValue = 0x000000ff;
1998 color_key.dwColorSpaceHighValue = 0x000000ff;
1999 hr = IDirectDrawSurface4_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2000 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x, i %u.\n", hr, i);
2001 memset(&color_key, 0, sizeof(color_key));
2002 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2003 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x, i %u.\n", hr, i);
2004 ok(color_key.dwColorSpaceLowValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
2005 color_key.dwColorSpaceLowValue, i);
2006 ok(color_key.dwColorSpaceHighValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
2007 color_key.dwColorSpaceHighValue, i);
2009 IDirectDrawSurface_Release(mipmap);
2010 mipmap = tmp;
2013 memset(&color_key, 0, sizeof(color_key));
2014 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2015 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
2016 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2017 color_key.dwColorSpaceLowValue);
2018 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2019 color_key.dwColorSpaceHighValue);
2021 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
2022 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
2023 IDirectDrawSurface_Release(mipmap);
2024 refcount = IDirectDrawSurface4_Release(surface);
2025 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
2027 memset(&surface_desc, 0, sizeof(surface_desc));
2028 surface_desc.dwSize = sizeof(surface_desc);
2029 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
2030 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
2031 U5(surface_desc).dwBackBufferCount = 1;
2032 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
2033 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
2035 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2036 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
2037 color_key.dwColorSpaceLowValue = 0x0000ff00;
2038 color_key.dwColorSpaceHighValue = 0x0000ff00;
2039 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2040 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
2041 memset(&color_key, 0, sizeof(color_key));
2042 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2043 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
2044 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2045 color_key.dwColorSpaceLowValue);
2046 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2047 color_key.dwColorSpaceHighValue);
2049 hr = IDirectDrawSurface4_GetAttachedSurface(surface, &caps, &tmp);
2050 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
2052 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2053 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
2054 color_key.dwColorSpaceLowValue = 0x0000ff00;
2055 color_key.dwColorSpaceHighValue = 0x0000ff00;
2056 hr = IDirectDrawSurface4_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2057 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
2058 memset(&color_key, 0, sizeof(color_key));
2059 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2060 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
2061 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2062 color_key.dwColorSpaceLowValue);
2063 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2064 color_key.dwColorSpaceHighValue);
2066 IDirectDrawSurface_Release(tmp);
2068 refcount = IDirectDrawSurface4_Release(surface);
2069 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
2070 IDirectDraw4_Release(ddraw);
2071 refcount = IDirect3DDevice3_Release(device);
2072 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
2073 DestroyWindow(window);
2076 struct qi_test
2078 REFIID iid;
2079 REFIID refcount_iid;
2080 HRESULT hr;
2083 static void test_qi(const char *test_name, IUnknown *base_iface,
2084 REFIID refcount_iid, const struct qi_test *tests, UINT entry_count)
2086 ULONG refcount, expected_refcount;
2087 IUnknown *iface1, *iface2;
2088 HRESULT hr;
2089 UINT i, j;
2091 for (i = 0; i < entry_count; ++i)
2093 hr = IUnknown_QueryInterface(base_iface, tests[i].iid, (void **)&iface1);
2094 ok(hr == tests[i].hr, "Got hr %#x for test \"%s\" %u.\n", hr, test_name, i);
2095 if (SUCCEEDED(hr))
2097 for (j = 0; j < entry_count; ++j)
2099 hr = IUnknown_QueryInterface(iface1, tests[j].iid, (void **)&iface2);
2100 ok(hr == tests[j].hr, "Got hr %#x for test \"%s\" %u, %u.\n", hr, test_name, i, j);
2101 if (SUCCEEDED(hr))
2103 expected_refcount = 0;
2104 if (IsEqualGUID(refcount_iid, tests[j].refcount_iid))
2105 ++expected_refcount;
2106 if (IsEqualGUID(tests[i].refcount_iid, tests[j].refcount_iid))
2107 ++expected_refcount;
2108 refcount = IUnknown_Release(iface2);
2109 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, %u, expected %u.\n",
2110 refcount, test_name, i, j, expected_refcount);
2114 expected_refcount = 0;
2115 if (IsEqualGUID(refcount_iid, tests[i].refcount_iid))
2116 ++expected_refcount;
2117 refcount = IUnknown_Release(iface1);
2118 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, expected %u.\n",
2119 refcount, test_name, i, expected_refcount);
2124 static void test_surface_qi(void)
2126 static const struct qi_test tests[] =
2128 {&IID_IDirect3DTexture2, &IID_IDirectDrawSurface4, S_OK },
2129 {&IID_IDirect3DTexture, &IID_IDirectDrawSurface4, S_OK },
2130 {&IID_IDirectDrawGammaControl, &IID_IDirectDrawGammaControl, S_OK },
2131 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
2132 {&IID_IDirectDrawSurface7, &IID_IDirectDrawSurface7, S_OK },
2133 {&IID_IDirectDrawSurface4, &IID_IDirectDrawSurface4, S_OK },
2134 {&IID_IDirectDrawSurface3, &IID_IDirectDrawSurface3, S_OK },
2135 {&IID_IDirectDrawSurface2, &IID_IDirectDrawSurface2, S_OK },
2136 {&IID_IDirectDrawSurface, &IID_IDirectDrawSurface, S_OK },
2137 {&IID_IDirect3DDevice7, NULL, E_INVALIDARG },
2138 {&IID_IDirect3DDevice3, NULL, E_INVALIDARG },
2139 {&IID_IDirect3DDevice2, NULL, E_INVALIDARG },
2140 {&IID_IDirect3DDevice, NULL, E_INVALIDARG },
2141 {&IID_IDirect3D7, NULL, E_INVALIDARG },
2142 {&IID_IDirect3D3, NULL, E_INVALIDARG },
2143 {&IID_IDirect3D2, NULL, E_INVALIDARG },
2144 {&IID_IDirect3D, NULL, E_INVALIDARG },
2145 {&IID_IDirectDraw7, NULL, E_INVALIDARG },
2146 {&IID_IDirectDraw4, NULL, E_INVALIDARG },
2147 {&IID_IDirectDraw3, NULL, E_INVALIDARG },
2148 {&IID_IDirectDraw2, NULL, E_INVALIDARG },
2149 {&IID_IDirectDraw, NULL, E_INVALIDARG },
2150 {&IID_IDirect3DLight, NULL, E_INVALIDARG },
2151 {&IID_IDirect3DMaterial, NULL, E_INVALIDARG },
2152 {&IID_IDirect3DMaterial2, NULL, E_INVALIDARG },
2153 {&IID_IDirect3DMaterial3, NULL, E_INVALIDARG },
2154 {&IID_IDirect3DExecuteBuffer, NULL, E_INVALIDARG },
2155 {&IID_IDirect3DViewport, NULL, E_INVALIDARG },
2156 {&IID_IDirect3DViewport2, NULL, E_INVALIDARG },
2157 {&IID_IDirect3DViewport3, NULL, E_INVALIDARG },
2158 {&IID_IDirect3DVertexBuffer, NULL, E_INVALIDARG },
2159 {&IID_IDirect3DVertexBuffer7, NULL, E_INVALIDARG },
2160 {&IID_IDirectDrawPalette, NULL, E_INVALIDARG },
2161 {&IID_IDirectDrawClipper, NULL, E_INVALIDARG },
2162 {&IID_IUnknown, &IID_IDirectDrawSurface, S_OK },
2165 IDirectDrawSurface4 *surface;
2166 DDSURFACEDESC2 surface_desc;
2167 IDirect3DDevice3 *device;
2168 IDirectDraw4 *ddraw;
2169 HWND window;
2170 HRESULT hr;
2172 if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
2174 win_skip("DirectDrawCreateEx not available, skipping test.\n");
2175 return;
2178 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2179 0, 0, 640, 480, 0, 0, 0, 0);
2180 /* Try to create a D3D device to see if the ddraw implementation supports
2181 * D3D. 64-bit ddraw in particular doesn't seem to support D3D, and
2182 * doesn't support e.g. the IDirect3DTexture interfaces. */
2183 if (!(device = create_device(window, DDSCL_NORMAL)))
2185 skip("Failed to create a 3D device, skipping test.\n");
2186 DestroyWindow(window);
2187 return;
2189 IDirect3DDevice_Release(device);
2190 ddraw = create_ddraw();
2191 ok(!!ddraw, "Failed to create a ddraw object.\n");
2192 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2193 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
2195 memset(&surface_desc, 0, sizeof(surface_desc));
2196 surface_desc.dwSize = sizeof(surface_desc);
2197 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
2198 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
2199 surface_desc.dwWidth = 512;
2200 surface_desc.dwHeight = 512;
2201 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
2202 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
2204 test_qi("surface_qi", (IUnknown *)surface, &IID_IDirectDrawSurface4, tests, sizeof(tests) / sizeof(*tests));
2206 IDirectDrawSurface4_Release(surface);
2207 IDirectDraw4_Release(ddraw);
2208 DestroyWindow(window);
2211 static void test_device_qi(void)
2213 static const struct qi_test tests[] =
2215 {&IID_IDirect3DTexture2, NULL, E_NOINTERFACE},
2216 {&IID_IDirect3DTexture, NULL, E_NOINTERFACE},
2217 {&IID_IDirectDrawGammaControl, NULL, E_NOINTERFACE},
2218 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
2219 {&IID_IDirectDrawSurface7, NULL, E_NOINTERFACE},
2220 {&IID_IDirectDrawSurface4, NULL, E_NOINTERFACE},
2221 {&IID_IDirectDrawSurface3, NULL, E_NOINTERFACE},
2222 {&IID_IDirectDrawSurface2, NULL, E_NOINTERFACE},
2223 {&IID_IDirectDrawSurface, NULL, E_NOINTERFACE},
2224 {&IID_IDirect3DDevice7, NULL, E_NOINTERFACE},
2225 {&IID_IDirect3DDevice3, &IID_IDirect3DDevice3, S_OK },
2226 {&IID_IDirect3DDevice2, &IID_IDirect3DDevice3, S_OK },
2227 {&IID_IDirect3DDevice, &IID_IDirect3DDevice3, S_OK },
2228 {&IID_IDirect3DRampDevice, NULL, E_NOINTERFACE},
2229 {&IID_IDirect3DRGBDevice, NULL, E_NOINTERFACE},
2230 {&IID_IDirect3DHALDevice, NULL, E_NOINTERFACE},
2231 {&IID_IDirect3DMMXDevice, NULL, E_NOINTERFACE},
2232 {&IID_IDirect3DRefDevice, NULL, E_NOINTERFACE},
2233 {&IID_IDirect3DTnLHalDevice, NULL, E_NOINTERFACE},
2234 {&IID_IDirect3DNullDevice, NULL, E_NOINTERFACE},
2235 {&IID_IDirect3D7, NULL, E_NOINTERFACE},
2236 {&IID_IDirect3D3, NULL, E_NOINTERFACE},
2237 {&IID_IDirect3D2, NULL, E_NOINTERFACE},
2238 {&IID_IDirect3D, NULL, E_NOINTERFACE},
2239 {&IID_IDirectDraw7, NULL, E_NOINTERFACE},
2240 {&IID_IDirectDraw4, NULL, E_NOINTERFACE},
2241 {&IID_IDirectDraw3, NULL, E_NOINTERFACE},
2242 {&IID_IDirectDraw2, NULL, E_NOINTERFACE},
2243 {&IID_IDirectDraw, NULL, E_NOINTERFACE},
2244 {&IID_IDirect3DLight, NULL, E_NOINTERFACE},
2245 {&IID_IDirect3DMaterial, NULL, E_NOINTERFACE},
2246 {&IID_IDirect3DMaterial2, NULL, E_NOINTERFACE},
2247 {&IID_IDirect3DMaterial3, NULL, E_NOINTERFACE},
2248 {&IID_IDirect3DExecuteBuffer, NULL, E_NOINTERFACE},
2249 {&IID_IDirect3DViewport, NULL, E_NOINTERFACE},
2250 {&IID_IDirect3DViewport2, NULL, E_NOINTERFACE},
2251 {&IID_IDirect3DViewport3, NULL, E_NOINTERFACE},
2252 {&IID_IDirect3DVertexBuffer, NULL, E_NOINTERFACE},
2253 {&IID_IDirect3DVertexBuffer7, NULL, E_NOINTERFACE},
2254 {&IID_IDirectDrawPalette, NULL, E_NOINTERFACE},
2255 {&IID_IDirectDrawClipper, NULL, E_NOINTERFACE},
2256 {&IID_IUnknown, &IID_IDirect3DDevice3, S_OK },
2259 IDirect3DDevice3 *device;
2260 HWND window;
2262 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2263 0, 0, 640, 480, 0, 0, 0, 0);
2264 if (!(device = create_device(window, DDSCL_NORMAL)))
2266 skip("Failed to create a 3D device, skipping test.\n");
2267 DestroyWindow(window);
2268 return;
2271 test_qi("device_qi", (IUnknown *)device, &IID_IDirect3DDevice3, tests, sizeof(tests) / sizeof(*tests));
2273 IDirect3DDevice3_Release(device);
2274 DestroyWindow(window);
2277 static void test_wndproc(void)
2279 LONG_PTR proc, ddraw_proc;
2280 IDirectDraw4 *ddraw;
2281 WNDCLASSA wc = {0};
2282 HWND window;
2283 HRESULT hr;
2284 ULONG ref;
2286 static struct message messages[] =
2288 {WM_WINDOWPOSCHANGING, FALSE, 0},
2289 {WM_MOVE, FALSE, 0},
2290 {WM_SIZE, FALSE, 0},
2291 {WM_WINDOWPOSCHANGING, FALSE, 0},
2292 {WM_ACTIVATE, FALSE, 0},
2293 {WM_SETFOCUS, FALSE, 0},
2294 {0, FALSE, 0},
2297 /* DDSCL_EXCLUSIVE replaces the window's window proc. */
2298 ddraw = create_ddraw();
2299 ok(!!ddraw, "Failed to create a ddraw object.\n");
2301 wc.lpfnWndProc = test_proc;
2302 wc.lpszClassName = "ddraw_test_wndproc_wc";
2303 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2305 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
2306 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
2308 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2309 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2310 (LONG_PTR)test_proc, proc);
2311 expect_messages = messages;
2312 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2313 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2314 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2315 expect_messages = NULL;
2316 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2317 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2318 (LONG_PTR)test_proc, proc);
2319 ref = IDirectDraw4_Release(ddraw);
2320 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2321 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2322 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2323 (LONG_PTR)test_proc, proc);
2325 /* DDSCL_NORMAL doesn't. */
2326 ddraw = create_ddraw();
2327 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2328 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2329 (LONG_PTR)test_proc, proc);
2330 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
2331 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2332 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2333 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2334 (LONG_PTR)test_proc, proc);
2335 ref = IDirectDraw4_Release(ddraw);
2336 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2337 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2338 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2339 (LONG_PTR)test_proc, proc);
2341 /* The original window proc is only restored by ddraw if the current
2342 * window proc matches the one ddraw set. This also affects switching
2343 * from DDSCL_NORMAL to DDSCL_EXCLUSIVE. */
2344 ddraw = create_ddraw();
2345 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2346 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2347 (LONG_PTR)test_proc, proc);
2348 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2349 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2350 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2351 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2352 (LONG_PTR)test_proc, proc);
2353 ddraw_proc = proc;
2354 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2355 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2356 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2357 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2358 (LONG_PTR)test_proc, proc);
2359 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2360 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2361 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2362 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2363 (LONG_PTR)test_proc, proc);
2364 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2365 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2366 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2367 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2368 (LONG_PTR)DefWindowProcA, proc);
2369 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2370 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2371 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)ddraw_proc);
2372 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2373 (LONG_PTR)DefWindowProcA, proc);
2374 ref = IDirectDraw4_Release(ddraw);
2375 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2376 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2377 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2378 (LONG_PTR)test_proc, proc);
2380 ddraw = create_ddraw();
2381 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2382 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2383 (LONG_PTR)test_proc, proc);
2384 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2385 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2386 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2387 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2388 (LONG_PTR)test_proc, proc);
2389 ref = IDirectDraw4_Release(ddraw);
2390 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2391 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2392 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2393 (LONG_PTR)DefWindowProcA, proc);
2395 fix_wndproc(window, (LONG_PTR)test_proc);
2396 expect_messages = NULL;
2397 DestroyWindow(window);
2398 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
2401 static void test_window_style(void)
2403 LONG style, exstyle, tmp, expected_style;
2404 RECT fullscreen_rect, r;
2405 IDirectDraw4 *ddraw;
2406 HWND window;
2407 HRESULT hr;
2408 ULONG ref;
2409 BOOL ret;
2411 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2412 0, 0, 100, 100, 0, 0, 0, 0);
2413 ddraw = create_ddraw();
2414 ok(!!ddraw, "Failed to create a ddraw object.\n");
2416 style = GetWindowLongA(window, GWL_STYLE);
2417 exstyle = GetWindowLongA(window, GWL_EXSTYLE);
2418 SetRect(&fullscreen_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2420 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2421 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2423 tmp = GetWindowLongA(window, GWL_STYLE);
2424 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2425 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2426 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2428 GetWindowRect(window, &r);
2429 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2430 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2431 r.left, r.top, r.right, r.bottom);
2432 GetClientRect(window, &r);
2433 todo_wine ok(!EqualRect(&r, &fullscreen_rect), "Client rect and window rect are equal.\n");
2435 ret = SetForegroundWindow(GetDesktopWindow());
2436 ok(ret, "Failed to set foreground window.\n");
2438 tmp = GetWindowLongA(window, GWL_STYLE);
2439 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2440 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2441 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2443 ret = SetForegroundWindow(window);
2444 ok(ret, "Failed to set foreground window.\n");
2445 /* Windows 7 (but not Vista and XP) shows the window when it receives focus. Hide it again,
2446 * the next tests expect this. */
2447 ShowWindow(window, SW_HIDE);
2449 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2450 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2452 tmp = GetWindowLongA(window, GWL_STYLE);
2453 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2454 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2455 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2457 ShowWindow(window, SW_SHOW);
2458 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2459 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2461 tmp = GetWindowLongA(window, GWL_STYLE);
2462 expected_style = style | WS_VISIBLE;
2463 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2464 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2465 expected_style = exstyle | WS_EX_TOPMOST;
2466 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2468 ret = SetForegroundWindow(GetDesktopWindow());
2469 ok(ret, "Failed to set foreground window.\n");
2470 tmp = GetWindowLongA(window, GWL_STYLE);
2471 expected_style = style | WS_VISIBLE | WS_MINIMIZE;
2472 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2473 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2474 expected_style = exstyle | WS_EX_TOPMOST;
2475 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2477 ref = IDirectDraw4_Release(ddraw);
2478 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2480 DestroyWindow(window);
2483 static void test_redundant_mode_set(void)
2485 DDSURFACEDESC2 surface_desc = {0};
2486 IDirectDraw4 *ddraw;
2487 HWND window;
2488 HRESULT hr;
2489 RECT r, s;
2490 ULONG ref;
2492 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2493 0, 0, 100, 100, 0, 0, 0, 0);
2494 ddraw = create_ddraw();
2495 ok(!!ddraw, "Failed to create a ddraw object.\n");
2497 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2498 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2500 surface_desc.dwSize = sizeof(surface_desc);
2501 hr = IDirectDraw4_GetDisplayMode(ddraw, &surface_desc);
2502 ok(SUCCEEDED(hr), "GetDipslayMode failed, hr %#x.\n", hr);
2504 hr = IDirectDraw4_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2505 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount, 0, 0);
2506 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2508 GetWindowRect(window, &r);
2509 r.right /= 2;
2510 r.bottom /= 2;
2511 SetWindowPos(window, HWND_TOP, r.left, r.top, r.right, r.bottom, 0);
2512 GetWindowRect(window, &s);
2513 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2514 r.left, r.top, r.right, r.bottom,
2515 s.left, s.top, s.right, s.bottom);
2517 hr = IDirectDraw4_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2518 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount, 0, 0);
2519 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2521 GetWindowRect(window, &s);
2522 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2523 r.left, r.top, r.right, r.bottom,
2524 s.left, s.top, s.right, s.bottom);
2526 ref = IDirectDraw4_Release(ddraw);
2527 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2529 DestroyWindow(window);
2532 static SIZE screen_size, screen_size2;
2534 static LRESULT CALLBACK mode_set_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2536 if (message == WM_SIZE)
2538 screen_size.cx = GetSystemMetrics(SM_CXSCREEN);
2539 screen_size.cy = GetSystemMetrics(SM_CYSCREEN);
2542 return test_proc(hwnd, message, wparam, lparam);
2545 static LRESULT CALLBACK mode_set_proc2(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2547 if (message == WM_SIZE)
2549 screen_size2.cx = GetSystemMetrics(SM_CXSCREEN);
2550 screen_size2.cy = GetSystemMetrics(SM_CYSCREEN);
2553 return test_proc(hwnd, message, wparam, lparam);
2556 struct test_coop_level_mode_set_enum_param
2558 DWORD ddraw_width, ddraw_height, user32_width, user32_height;
2561 static HRESULT CALLBACK test_coop_level_mode_set_enum_cb(DDSURFACEDESC2 *surface_desc, void *context)
2563 struct test_coop_level_mode_set_enum_param *param = context;
2565 if (U1(U4(*surface_desc).ddpfPixelFormat).dwRGBBitCount != registry_mode.dmBitsPerPel)
2566 return DDENUMRET_OK;
2567 if (surface_desc->dwWidth == registry_mode.dmPelsWidth
2568 && surface_desc->dwHeight == registry_mode.dmPelsHeight)
2569 return DDENUMRET_OK;
2571 if (!param->ddraw_width)
2573 param->ddraw_width = surface_desc->dwWidth;
2574 param->ddraw_height = surface_desc->dwHeight;
2575 return DDENUMRET_OK;
2577 if (surface_desc->dwWidth == param->ddraw_width && surface_desc->dwHeight == param->ddraw_height)
2578 return DDENUMRET_OK;
2580 param->user32_width = surface_desc->dwWidth;
2581 param->user32_height = surface_desc->dwHeight;
2582 return DDENUMRET_CANCEL;
2585 static void test_coop_level_mode_set(void)
2587 IDirectDrawSurface4 *primary;
2588 RECT registry_rect, ddraw_rect, user32_rect, r;
2589 IDirectDraw4 *ddraw;
2590 DDSURFACEDESC2 ddsd;
2591 WNDCLASSA wc = {0};
2592 HWND window, window2;
2593 HRESULT hr;
2594 ULONG ref;
2595 MSG msg;
2596 struct test_coop_level_mode_set_enum_param param;
2597 DEVMODEW devmode;
2598 BOOL ret;
2599 LONG change_ret;
2601 static const struct message exclusive_messages[] =
2603 {WM_WINDOWPOSCHANGING, FALSE, 0},
2604 {WM_WINDOWPOSCHANGED, FALSE, 0},
2605 {WM_SIZE, FALSE, 0},
2606 {WM_DISPLAYCHANGE, FALSE, 0},
2607 {0, FALSE, 0},
2609 static const struct message exclusive_focus_loss_messages[] =
2611 {WM_ACTIVATE, TRUE, WA_INACTIVE},
2612 {WM_DISPLAYCHANGE, FALSE, 0},
2613 {WM_WINDOWPOSCHANGING, FALSE, 0},
2614 /* Like d3d8 and d3d9 ddraw seems to use SW_SHOWMINIMIZED instead of
2615 * SW_MINIMIZED, causing a recursive window activation that does not
2616 * produce the same result in Wine yet. Ignore the difference for now.
2617 * {WM_ACTIVATE, TRUE, 0x200000 | WA_ACTIVE}, */
2618 {WM_WINDOWPOSCHANGED, FALSE, 0},
2619 {WM_MOVE, FALSE, 0},
2620 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2621 {WM_ACTIVATEAPP, TRUE, FALSE},
2622 {0, FALSE, 0},
2624 static const struct message exclusive_focus_restore_messages[] =
2626 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* From the ShowWindow(SW_RESTORE). */
2627 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* Generated by ddraw, matches d3d9 behavior. */
2628 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching previous message. */
2629 {WM_SIZE, FALSE, 0}, /* DefWindowProc. */
2630 {WM_DISPLAYCHANGE, FALSE, 0}, /* Ddraw restores mode. */
2631 /* Native redundantly sets the window size here. */
2632 {WM_ACTIVATEAPP, TRUE, TRUE}, /* End of ddraw's hooks. */
2633 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching the one from ShowWindow. */
2634 {WM_MOVE, FALSE, 0}, /* DefWindowProc. */
2635 {WM_SIZE, TRUE, SIZE_RESTORED}, /* DefWindowProc. */
2636 {0, FALSE, 0},
2638 static const struct message sc_restore_messages[] =
2640 {WM_SYSCOMMAND, TRUE, SC_RESTORE},
2641 {WM_WINDOWPOSCHANGING, FALSE, 0},
2642 {WM_WINDOWPOSCHANGED, FALSE, 0},
2643 {WM_SIZE, TRUE, SIZE_RESTORED},
2644 {0, FALSE, 0},
2646 static const struct message sc_minimize_messages[] =
2648 {WM_SYSCOMMAND, TRUE, SC_MINIMIZE},
2649 {WM_WINDOWPOSCHANGING, FALSE, 0},
2650 {WM_WINDOWPOSCHANGED, FALSE, 0},
2651 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2652 {0, FALSE, 0},
2654 static const struct message sc_maximize_messages[] =
2656 {WM_SYSCOMMAND, TRUE, SC_MAXIMIZE},
2657 {WM_WINDOWPOSCHANGING, FALSE, 0},
2658 {WM_WINDOWPOSCHANGED, FALSE, 0},
2659 {WM_SIZE, TRUE, SIZE_MAXIMIZED},
2660 {0, FALSE, 0},
2663 static const struct message normal_messages[] =
2665 {WM_DISPLAYCHANGE, FALSE, 0},
2666 {0, FALSE, 0},
2669 ddraw = create_ddraw();
2670 ok(!!ddraw, "Failed to create a ddraw object.\n");
2672 memset(&param, 0, sizeof(param));
2673 hr = IDirectDraw4_EnumDisplayModes(ddraw, 0, NULL, &param, test_coop_level_mode_set_enum_cb);
2674 ok(SUCCEEDED(hr), "Failed to enumerate display mode, hr %#x.\n", hr);
2675 ref = IDirectDraw4_Release(ddraw);
2676 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2678 if (!param.user32_height)
2680 skip("Fewer than 3 different modes supported, skipping mode restore test.\n");
2681 return;
2684 SetRect(&registry_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2685 SetRect(&ddraw_rect, 0, 0, param.ddraw_width, param.ddraw_height);
2686 SetRect(&user32_rect, 0, 0, param.user32_width, param.user32_height);
2688 memset(&devmode, 0, sizeof(devmode));
2689 devmode.dmSize = sizeof(devmode);
2690 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2691 devmode.dmPelsWidth = param.user32_width;
2692 devmode.dmPelsHeight = param.user32_height;
2693 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2694 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2696 ddraw = create_ddraw();
2697 ok(!!ddraw, "Failed to create a ddraw object.\n");
2699 wc.lpfnWndProc = mode_set_proc;
2700 wc.lpszClassName = "ddraw_test_wndproc_wc";
2701 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2702 wc.lpfnWndProc = mode_set_proc2;
2703 wc.lpszClassName = "ddraw_test_wndproc_wc2";
2704 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2706 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test", WS_OVERLAPPEDWINDOW,
2707 0, 0, 100, 100, 0, 0, 0, 0);
2708 window2 = CreateWindowA("ddraw_test_wndproc_wc2", "ddraw_test", WS_OVERLAPPEDWINDOW,
2709 0, 0, 100, 100, 0, 0, 0, 0);
2711 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2712 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2714 GetWindowRect(window, &r);
2715 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2716 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2717 r.left, r.top, r.right, r.bottom);
2719 memset(&ddsd, 0, sizeof(ddsd));
2720 ddsd.dwSize = sizeof(ddsd);
2721 ddsd.dwFlags = DDSD_CAPS;
2722 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2724 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2725 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2726 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2727 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2728 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2729 param.user32_width, ddsd.dwWidth);
2730 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2731 param.user32_height, ddsd.dwHeight);
2733 GetWindowRect(window, &r);
2734 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2735 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2736 r.left, r.top, r.right, r.bottom);
2738 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2739 expect_messages = exclusive_messages;
2740 screen_size.cx = 0;
2741 screen_size.cy = 0;
2743 hr = IDirectDrawSurface4_IsLost(primary);
2744 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2745 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2746 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2747 hr = IDirectDrawSurface4_IsLost(primary);
2748 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2750 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2751 expect_messages = NULL;
2752 ok(screen_size.cx == param.ddraw_width && screen_size.cy == param.ddraw_height,
2753 "Expected screen size %ux%u, got %ux%u.\n",
2754 param.ddraw_width, param.ddraw_height, screen_size.cx, screen_size.cy);
2756 GetWindowRect(window, &r);
2757 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2758 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2759 r.left, r.top, r.right, r.bottom);
2761 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2762 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2763 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2764 param.user32_width, ddsd.dwWidth);
2765 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2766 param.user32_height, ddsd.dwHeight);
2767 IDirectDrawSurface4_Release(primary);
2769 memset(&ddsd, 0, sizeof(ddsd));
2770 ddsd.dwSize = sizeof(ddsd);
2771 ddsd.dwFlags = DDSD_CAPS;
2772 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2774 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2775 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2776 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2777 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2778 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2779 param.ddraw_width, ddsd.dwWidth);
2780 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2781 param.ddraw_height, ddsd.dwHeight);
2783 GetWindowRect(window, &r);
2784 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2785 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2786 r.left, r.top, r.right, r.bottom);
2788 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2789 expect_messages = exclusive_messages;
2790 screen_size.cx = 0;
2791 screen_size.cy = 0;
2793 hr = IDirectDrawSurface4_IsLost(primary);
2794 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2795 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2796 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2797 hr = IDirectDrawSurface4_IsLost(primary);
2798 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2800 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2801 expect_messages = NULL;
2802 ok(screen_size.cx == param.user32_width && screen_size.cy == param.user32_height,
2803 "Expected screen size %ux%u, got %ux%u.\n",
2804 param.user32_width, param.user32_height, screen_size.cx, screen_size.cy);
2806 GetWindowRect(window, &r);
2807 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2808 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2809 r.left, r.top, r.right, r.bottom);
2811 expect_messages = exclusive_focus_loss_messages;
2812 ret = SetForegroundWindow(GetDesktopWindow());
2813 ok(ret, "Failed to set foreground window.\n");
2814 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2815 memset(&devmode, 0, sizeof(devmode));
2816 devmode.dmSize = sizeof(devmode);
2817 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2818 ok(ret, "Failed to get display mode.\n");
2819 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
2820 && devmode.dmPelsHeight == registry_mode.dmPelsHeight, "Got unexpect screen size %ux%u.\n",
2821 devmode.dmPelsWidth, devmode.dmPelsHeight);
2823 expect_messages = exclusive_focus_restore_messages;
2824 ShowWindow(window, SW_RESTORE);
2825 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2827 GetWindowRect(window, &r);
2828 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2829 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2830 r.left, r.top, r.right, r.bottom);
2831 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2832 ok(ret, "Failed to get display mode.\n");
2833 ok(devmode.dmPelsWidth == param.ddraw_width
2834 && devmode.dmPelsHeight == param.ddraw_height, "Got unexpect screen size %ux%u.\n",
2835 devmode.dmPelsWidth, devmode.dmPelsHeight);
2837 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2838 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2839 /* Normally the primary should be restored here. Unfortunately this causes the
2840 * GetSurfaceDesc call after the next display mode change to crash on the Windows 8
2841 * testbot. Another Restore call would presumably avoid the crash, but it also moots
2842 * the point of the GetSurfaceDesc call. */
2844 expect_messages = sc_minimize_messages;
2845 SendMessageA(window, WM_SYSCOMMAND, SC_MINIMIZE, 0);
2846 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2847 expect_messages = NULL;
2849 expect_messages = sc_restore_messages;
2850 SendMessageA(window, WM_SYSCOMMAND, SC_RESTORE, 0);
2851 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2852 expect_messages = NULL;
2854 expect_messages = sc_maximize_messages;
2855 SendMessageA(window, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
2856 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2857 expect_messages = NULL;
2859 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2860 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2862 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2863 expect_messages = exclusive_messages;
2864 screen_size.cx = 0;
2865 screen_size.cy = 0;
2867 hr = IDirectDrawSurface4_IsLost(primary);
2868 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2869 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
2870 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2871 hr = IDirectDrawSurface4_IsLost(primary);
2872 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2874 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2875 expect_messages = NULL;
2876 ok(screen_size.cx == registry_mode.dmPelsWidth
2877 && screen_size.cy == registry_mode.dmPelsHeight,
2878 "Expected screen size %ux%u, got %ux%u.\n",
2879 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight, screen_size.cx, screen_size.cy);
2881 GetWindowRect(window, &r);
2882 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2883 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2884 r.left, r.top, r.right, r.bottom);
2886 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2887 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2888 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2889 param.ddraw_width, ddsd.dwWidth);
2890 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2891 param.ddraw_height, ddsd.dwHeight);
2892 IDirectDrawSurface4_Release(primary);
2894 /* For Wine. */
2895 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
2896 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2898 memset(&ddsd, 0, sizeof(ddsd));
2899 ddsd.dwSize = sizeof(ddsd);
2900 ddsd.dwFlags = DDSD_CAPS;
2901 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2903 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2904 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2905 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2906 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2907 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2908 registry_mode.dmPelsWidth, ddsd.dwWidth);
2909 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2910 registry_mode.dmPelsHeight, ddsd.dwHeight);
2912 GetWindowRect(window, &r);
2913 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2914 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2915 r.left, r.top, r.right, r.bottom);
2917 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2918 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2920 GetWindowRect(window, &r);
2921 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2922 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2923 r.left, r.top, r.right, r.bottom);
2925 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2926 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2927 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2928 registry_mode.dmPelsWidth, ddsd.dwWidth);
2929 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2930 registry_mode.dmPelsHeight, ddsd.dwHeight);
2931 IDirectDrawSurface4_Release(primary);
2933 memset(&ddsd, 0, sizeof(ddsd));
2934 ddsd.dwSize = sizeof(ddsd);
2935 ddsd.dwFlags = DDSD_CAPS;
2936 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2938 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2939 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2940 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2941 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2942 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2943 registry_mode.dmPelsWidth, ddsd.dwWidth);
2944 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2945 registry_mode.dmPelsHeight, ddsd.dwHeight);
2947 GetWindowRect(window, &r);
2948 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2949 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2950 r.left, r.top, r.right, r.bottom);
2952 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2953 expect_messages = normal_messages;
2954 screen_size.cx = 0;
2955 screen_size.cy = 0;
2957 hr = IDirectDrawSurface4_IsLost(primary);
2958 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2959 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2960 devmode.dmPelsWidth = param.user32_width;
2961 devmode.dmPelsHeight = param.user32_height;
2962 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2963 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2964 hr = IDirectDrawSurface4_IsLost(primary);
2965 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2967 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2968 expect_messages = NULL;
2969 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2971 GetWindowRect(window, &r);
2972 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2973 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2974 r.left, r.top, r.right, r.bottom);
2976 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2977 expect_messages = normal_messages;
2978 screen_size.cx = 0;
2979 screen_size.cy = 0;
2981 hr = IDirectDrawSurface4_Restore(primary);
2982 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
2983 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2984 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2985 hr = IDirectDrawSurface4_Restore(primary);
2986 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
2987 hr = IDirectDrawSurface4_IsLost(primary);
2988 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2990 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2991 expect_messages = NULL;
2992 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2994 GetWindowRect(window, &r);
2995 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2996 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2997 r.left, r.top, r.right, r.bottom);
2999 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3000 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3001 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3002 registry_mode.dmPelsWidth, ddsd.dwWidth);
3003 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3004 registry_mode.dmPelsHeight, ddsd.dwHeight);
3005 IDirectDrawSurface4_Release(primary);
3007 memset(&ddsd, 0, sizeof(ddsd));
3008 ddsd.dwSize = sizeof(ddsd);
3009 ddsd.dwFlags = DDSD_CAPS;
3010 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3012 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3013 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3014 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3015 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3016 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3017 param.ddraw_width, ddsd.dwWidth);
3018 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3019 param.ddraw_height, ddsd.dwHeight);
3021 GetWindowRect(window, &r);
3022 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3023 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3024 r.left, r.top, r.right, r.bottom);
3026 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3027 expect_messages = normal_messages;
3028 screen_size.cx = 0;
3029 screen_size.cy = 0;
3031 hr = IDirectDrawSurface4_IsLost(primary);
3032 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
3033 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
3034 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3035 hr = IDirectDrawSurface4_IsLost(primary);
3036 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
3038 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3039 expect_messages = NULL;
3040 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3042 GetWindowRect(window, &r);
3043 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3044 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3045 r.left, r.top, r.right, r.bottom);
3047 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3048 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3049 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3050 param.ddraw_width, ddsd.dwWidth);
3051 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3052 param.ddraw_height, ddsd.dwHeight);
3053 IDirectDrawSurface4_Release(primary);
3055 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3056 ok(ret, "Failed to get display mode.\n");
3057 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
3058 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
3059 "Expected resolution %ux%u, got %ux%u.\n",
3060 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
3061 devmode.dmPelsWidth, devmode.dmPelsHeight);
3062 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
3063 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
3065 memset(&ddsd, 0, sizeof(ddsd));
3066 ddsd.dwSize = sizeof(ddsd);
3067 ddsd.dwFlags = DDSD_CAPS;
3068 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3070 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3071 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3072 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3073 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3074 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3075 registry_mode.dmPelsWidth, ddsd.dwWidth);
3076 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3077 registry_mode.dmPelsHeight, ddsd.dwHeight);
3079 GetWindowRect(window, &r);
3080 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3081 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3082 r.left, r.top, r.right, r.bottom);
3084 /* DDSCL_NORMAL | DDSCL_FULLSCREEN behaves the same as just DDSCL_NORMAL.
3085 * Resizing the window on mode changes is a property of DDSCL_EXCLUSIVE,
3086 * not DDSCL_FULLSCREEN. */
3087 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
3088 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3090 GetWindowRect(window, &r);
3091 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3092 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3093 r.left, r.top, r.right, r.bottom);
3095 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3096 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3097 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3098 registry_mode.dmPelsWidth, ddsd.dwWidth);
3099 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3100 registry_mode.dmPelsHeight, ddsd.dwHeight);
3101 IDirectDrawSurface4_Release(primary);
3103 memset(&ddsd, 0, sizeof(ddsd));
3104 ddsd.dwSize = sizeof(ddsd);
3105 ddsd.dwFlags = DDSD_CAPS;
3106 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3108 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3109 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3110 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3111 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3112 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3113 registry_mode.dmPelsWidth, ddsd.dwWidth);
3114 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3115 registry_mode.dmPelsHeight, ddsd.dwHeight);
3117 GetWindowRect(window, &r);
3118 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3119 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3120 r.left, r.top, r.right, r.bottom);
3122 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3123 expect_messages = normal_messages;
3124 screen_size.cx = 0;
3125 screen_size.cy = 0;
3127 hr = IDirectDrawSurface4_IsLost(primary);
3128 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
3129 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
3130 devmode.dmPelsWidth = param.user32_width;
3131 devmode.dmPelsHeight = param.user32_height;
3132 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
3133 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
3134 hr = IDirectDrawSurface4_IsLost(primary);
3135 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
3137 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3138 expect_messages = NULL;
3139 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3141 GetWindowRect(window, &r);
3142 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3143 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3144 r.left, r.top, r.right, r.bottom);
3146 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3147 expect_messages = normal_messages;
3148 screen_size.cx = 0;
3149 screen_size.cy = 0;
3151 hr = IDirectDrawSurface4_Restore(primary);
3152 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
3153 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3154 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3155 hr = IDirectDrawSurface4_Restore(primary);
3156 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
3157 hr = IDirectDrawSurface4_IsLost(primary);
3158 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
3160 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3161 expect_messages = NULL;
3162 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3164 GetWindowRect(window, &r);
3165 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3166 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3167 r.left, r.top, r.right, r.bottom);
3169 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3170 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3171 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3172 registry_mode.dmPelsWidth, ddsd.dwWidth);
3173 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3174 registry_mode.dmPelsHeight, ddsd.dwHeight);
3175 IDirectDrawSurface4_Release(primary);
3177 memset(&ddsd, 0, sizeof(ddsd));
3178 ddsd.dwSize = sizeof(ddsd);
3179 ddsd.dwFlags = DDSD_CAPS;
3180 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3182 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3183 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3184 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3185 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3186 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3187 param.ddraw_width, ddsd.dwWidth);
3188 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3189 param.ddraw_height, ddsd.dwHeight);
3191 GetWindowRect(window, &r);
3192 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3193 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3194 r.left, r.top, r.right, r.bottom);
3196 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3197 expect_messages = normal_messages;
3198 screen_size.cx = 0;
3199 screen_size.cy = 0;
3201 hr = IDirectDrawSurface4_IsLost(primary);
3202 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
3203 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
3204 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3205 hr = IDirectDrawSurface4_IsLost(primary);
3206 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
3208 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3209 expect_messages = NULL;
3210 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3212 GetWindowRect(window, &r);
3213 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3214 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3215 r.left, r.top, r.right, r.bottom);
3217 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3218 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3219 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3220 param.ddraw_width, ddsd.dwWidth);
3221 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3222 param.ddraw_height, ddsd.dwHeight);
3223 IDirectDrawSurface4_Release(primary);
3225 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3226 ok(ret, "Failed to get display mode.\n");
3227 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
3228 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
3229 "Expected resolution %ux%u, got %ux%u.\n",
3230 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
3231 devmode.dmPelsWidth, devmode.dmPelsHeight);
3232 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
3233 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
3235 memset(&ddsd, 0, sizeof(ddsd));
3236 ddsd.dwSize = sizeof(ddsd);
3237 ddsd.dwFlags = DDSD_CAPS;
3238 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3240 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3241 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3242 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3243 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3244 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3245 registry_mode.dmPelsWidth, ddsd.dwWidth);
3246 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3247 registry_mode.dmPelsHeight, ddsd.dwHeight);
3248 IDirectDrawSurface4_Release(primary);
3250 GetWindowRect(window, &r);
3251 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3252 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3253 r.left, r.top, r.right, r.bottom);
3255 /* Changing the coop level from EXCLUSIVE to NORMAL restores the screen resolution */
3256 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3257 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3258 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3259 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3261 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3262 expect_messages = exclusive_messages;
3263 screen_size.cx = 0;
3264 screen_size.cy = 0;
3266 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3267 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3269 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3270 expect_messages = NULL;
3271 ok(screen_size.cx == registry_mode.dmPelsWidth
3272 && screen_size.cy == registry_mode.dmPelsHeight,
3273 "Expected screen size %ux%u, got %ux%u.\n",
3274 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
3275 screen_size.cx, screen_size.cy);
3277 GetWindowRect(window, &r);
3278 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3279 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3280 r.left, r.top, r.right, r.bottom);
3282 memset(&ddsd, 0, sizeof(ddsd));
3283 ddsd.dwSize = sizeof(ddsd);
3284 ddsd.dwFlags = DDSD_CAPS;
3285 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3287 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3288 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3289 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3290 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3291 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3292 registry_mode.dmPelsWidth, ddsd.dwWidth);
3293 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3294 registry_mode.dmPelsHeight, ddsd.dwHeight);
3295 IDirectDrawSurface4_Release(primary);
3297 /* The screen restore is a property of DDSCL_EXCLUSIVE */
3298 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
3299 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3300 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3301 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3303 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3304 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3306 memset(&ddsd, 0, sizeof(ddsd));
3307 ddsd.dwSize = sizeof(ddsd);
3308 ddsd.dwFlags = DDSD_CAPS;
3309 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3311 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3312 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3313 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3314 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3315 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3316 param.ddraw_width, ddsd.dwWidth);
3317 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3318 param.ddraw_height, ddsd.dwHeight);
3319 IDirectDrawSurface4_Release(primary);
3321 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
3322 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3324 /* If the window is changed at the same time, messages are sent to the new window. */
3325 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3326 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3327 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3328 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3330 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3331 expect_messages = exclusive_messages;
3332 screen_size.cx = 0;
3333 screen_size.cy = 0;
3334 screen_size2.cx = 0;
3335 screen_size2.cy = 0;
3337 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
3338 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3340 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3341 expect_messages = NULL;
3342 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n",
3343 screen_size.cx, screen_size.cy);
3344 ok(screen_size2.cx == registry_mode.dmPelsWidth && screen_size2.cy == registry_mode.dmPelsHeight,
3345 "Expected screen size 2 %ux%u, got %ux%u.\n",
3346 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight, screen_size2.cx, screen_size2.cy);
3348 GetWindowRect(window, &r);
3349 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3350 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
3351 r.left, r.top, r.right, r.bottom);
3352 GetWindowRect(window2, &r);
3353 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3354 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3355 r.left, r.top, r.right, r.bottom);
3357 memset(&ddsd, 0, sizeof(ddsd));
3358 ddsd.dwSize = sizeof(ddsd);
3359 ddsd.dwFlags = DDSD_CAPS;
3360 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3362 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3363 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3364 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3365 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3366 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3367 registry_mode.dmPelsWidth, ddsd.dwWidth);
3368 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3369 registry_mode.dmPelsHeight, ddsd.dwHeight);
3370 IDirectDrawSurface4_Release(primary);
3372 ref = IDirectDraw4_Release(ddraw);
3373 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3375 GetWindowRect(window, &r);
3376 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3377 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
3378 r.left, r.top, r.right, r.bottom);
3380 expect_messages = NULL;
3381 DestroyWindow(window);
3382 DestroyWindow(window2);
3383 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
3384 UnregisterClassA("ddraw_test_wndproc_wc2", GetModuleHandleA(NULL));
3387 static void test_coop_level_mode_set_multi(void)
3389 IDirectDraw4 *ddraw1, *ddraw2;
3390 UINT w, h;
3391 HWND window;
3392 HRESULT hr;
3393 ULONG ref;
3395 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3396 0, 0, 100, 100, 0, 0, 0, 0);
3397 ddraw1 = create_ddraw();
3398 ok(!!ddraw1, "Failed to create a ddraw object.\n");
3400 /* With just a single ddraw object, the display mode is restored on
3401 * release. */
3402 hr = set_display_mode(ddraw1, 800, 600);
3403 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3404 w = GetSystemMetrics(SM_CXSCREEN);
3405 ok(w == 800, "Got unexpected screen width %u.\n", w);
3406 h = GetSystemMetrics(SM_CYSCREEN);
3407 ok(h == 600, "Got unexpected screen height %u.\n", h);
3409 ref = IDirectDraw4_Release(ddraw1);
3410 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3411 w = GetSystemMetrics(SM_CXSCREEN);
3412 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3413 h = GetSystemMetrics(SM_CYSCREEN);
3414 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3416 /* When there are multiple ddraw objects, the display mode is restored to
3417 * the initial mode, before the first SetDisplayMode() call. */
3418 ddraw1 = create_ddraw();
3419 hr = set_display_mode(ddraw1, 800, 600);
3420 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3421 w = GetSystemMetrics(SM_CXSCREEN);
3422 ok(w == 800, "Got unexpected screen width %u.\n", w);
3423 h = GetSystemMetrics(SM_CYSCREEN);
3424 ok(h == 600, "Got unexpected screen height %u.\n", h);
3426 ddraw2 = create_ddraw();
3427 hr = set_display_mode(ddraw2, 640, 480);
3428 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3429 w = GetSystemMetrics(SM_CXSCREEN);
3430 ok(w == 640, "Got unexpected screen width %u.\n", w);
3431 h = GetSystemMetrics(SM_CYSCREEN);
3432 ok(h == 480, "Got unexpected screen height %u.\n", h);
3434 ref = IDirectDraw4_Release(ddraw2);
3435 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3436 w = GetSystemMetrics(SM_CXSCREEN);
3437 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3438 h = GetSystemMetrics(SM_CYSCREEN);
3439 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3441 ref = IDirectDraw4_Release(ddraw1);
3442 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3443 w = GetSystemMetrics(SM_CXSCREEN);
3444 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3445 h = GetSystemMetrics(SM_CYSCREEN);
3446 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3448 /* Regardless of release ordering. */
3449 ddraw1 = create_ddraw();
3450 hr = set_display_mode(ddraw1, 800, 600);
3451 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3452 w = GetSystemMetrics(SM_CXSCREEN);
3453 ok(w == 800, "Got unexpected screen width %u.\n", w);
3454 h = GetSystemMetrics(SM_CYSCREEN);
3455 ok(h == 600, "Got unexpected screen height %u.\n", h);
3457 ddraw2 = create_ddraw();
3458 hr = set_display_mode(ddraw2, 640, 480);
3459 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3460 w = GetSystemMetrics(SM_CXSCREEN);
3461 ok(w == 640, "Got unexpected screen width %u.\n", w);
3462 h = GetSystemMetrics(SM_CYSCREEN);
3463 ok(h == 480, "Got unexpected screen height %u.\n", h);
3465 ref = IDirectDraw4_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 = IDirectDraw4_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 /* But only for ddraw objects that called SetDisplayMode(). */
3480 ddraw1 = create_ddraw();
3481 ddraw2 = create_ddraw();
3482 hr = set_display_mode(ddraw2, 640, 480);
3483 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3484 w = GetSystemMetrics(SM_CXSCREEN);
3485 ok(w == 640, "Got unexpected screen width %u.\n", w);
3486 h = GetSystemMetrics(SM_CYSCREEN);
3487 ok(h == 480, "Got unexpected screen height %u.\n", h);
3489 ref = IDirectDraw4_Release(ddraw1);
3490 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3491 w = GetSystemMetrics(SM_CXSCREEN);
3492 ok(w == 640, "Got unexpected screen width %u.\n", w);
3493 h = GetSystemMetrics(SM_CYSCREEN);
3494 ok(h == 480, "Got unexpected screen height %u.\n", h);
3496 ref = IDirectDraw4_Release(ddraw2);
3497 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3498 w = GetSystemMetrics(SM_CXSCREEN);
3499 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3500 h = GetSystemMetrics(SM_CYSCREEN);
3501 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3503 /* If there's a ddraw object that's currently in exclusive mode, it blocks
3504 * restoring the display mode. */
3505 ddraw1 = create_ddraw();
3506 hr = set_display_mode(ddraw1, 800, 600);
3507 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3508 w = GetSystemMetrics(SM_CXSCREEN);
3509 ok(w == 800, "Got unexpected screen width %u.\n", w);
3510 h = GetSystemMetrics(SM_CYSCREEN);
3511 ok(h == 600, "Got unexpected screen height %u.\n", h);
3513 ddraw2 = create_ddraw();
3514 hr = set_display_mode(ddraw2, 640, 480);
3515 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3516 w = GetSystemMetrics(SM_CXSCREEN);
3517 ok(w == 640, "Got unexpected screen width %u.\n", w);
3518 h = GetSystemMetrics(SM_CYSCREEN);
3519 ok(h == 480, "Got unexpected screen height %u.\n", h);
3521 hr = IDirectDraw4_SetCooperativeLevel(ddraw2, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3522 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3524 ref = IDirectDraw4_Release(ddraw1);
3525 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3526 w = GetSystemMetrics(SM_CXSCREEN);
3527 ok(w == 640, "Got unexpected screen width %u.\n", w);
3528 h = GetSystemMetrics(SM_CYSCREEN);
3529 ok(h == 480, "Got unexpected screen height %u.\n", h);
3531 ref = IDirectDraw4_Release(ddraw2);
3532 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3533 w = GetSystemMetrics(SM_CXSCREEN);
3534 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3535 h = GetSystemMetrics(SM_CYSCREEN);
3536 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3538 /* Exclusive mode blocks mode setting on other ddraw objects in general. */
3539 ddraw1 = create_ddraw();
3540 hr = set_display_mode(ddraw1, 800, 600);
3541 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3542 w = GetSystemMetrics(SM_CXSCREEN);
3543 ok(w == 800, "Got unexpected screen width %u.\n", w);
3544 h = GetSystemMetrics(SM_CYSCREEN);
3545 ok(h == 600, "Got unexpected screen height %u.\n", h);
3547 hr = IDirectDraw4_SetCooperativeLevel(ddraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3548 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3550 ddraw2 = create_ddraw();
3551 hr = set_display_mode(ddraw2, 640, 480);
3552 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
3554 ref = IDirectDraw4_Release(ddraw1);
3555 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3556 w = GetSystemMetrics(SM_CXSCREEN);
3557 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3558 h = GetSystemMetrics(SM_CYSCREEN);
3559 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3561 ref = IDirectDraw4_Release(ddraw2);
3562 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3563 w = GetSystemMetrics(SM_CXSCREEN);
3564 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3565 h = GetSystemMetrics(SM_CYSCREEN);
3566 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3568 DestroyWindow(window);
3571 static void test_initialize(void)
3573 IDirectDraw4 *ddraw;
3574 HRESULT hr;
3576 ddraw = create_ddraw();
3577 ok(!!ddraw, "Failed to create a ddraw object.\n");
3579 hr = IDirectDraw4_Initialize(ddraw, NULL);
3580 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x.\n", hr);
3581 IDirectDraw4_Release(ddraw);
3583 CoInitialize(NULL);
3584 hr = CoCreateInstance(&CLSID_DirectDraw, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectDraw4, (void **)&ddraw);
3585 ok(SUCCEEDED(hr), "Failed to create IDirectDraw4 instance, hr %#x.\n", hr);
3586 hr = IDirectDraw4_Initialize(ddraw, NULL);
3587 ok(hr == DD_OK, "Initialize returned hr %#x, expected DD_OK.\n", hr);
3588 hr = IDirectDraw4_Initialize(ddraw, NULL);
3589 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x, expected DDERR_ALREADYINITIALIZED.\n", hr);
3590 IDirectDraw4_Release(ddraw);
3591 CoUninitialize();
3594 static void test_coop_level_surf_create(void)
3596 IDirectDrawSurface4 *surface;
3597 IDirectDraw4 *ddraw;
3598 DDSURFACEDESC2 ddsd;
3599 HRESULT hr;
3601 ddraw = create_ddraw();
3602 ok(!!ddraw, "Failed to create a ddraw object.\n");
3604 memset(&ddsd, 0, sizeof(ddsd));
3605 ddsd.dwSize = sizeof(ddsd);
3606 ddsd.dwFlags = DDSD_CAPS;
3607 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3608 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
3609 ok(hr == DDERR_NOCOOPERATIVELEVELSET, "Surface creation returned hr %#x.\n", hr);
3611 IDirectDraw4_Release(ddraw);
3614 static void test_vb_discard(void)
3616 static const struct vec4 quad[] =
3618 { 0.0f, 480.0f, 0.0f, 1.0f},
3619 { 0.0f, 0.0f, 0.0f, 1.0f},
3620 {640.0f, 480.0f, 0.0f, 1.0f},
3621 {640.0f, 0.0f, 0.0f, 1.0f},
3624 IDirect3DDevice3 *device;
3625 IDirect3D3 *d3d;
3626 IDirect3DVertexBuffer *buffer;
3627 HWND window;
3628 HRESULT hr;
3629 D3DVERTEXBUFFERDESC desc;
3630 BYTE *data;
3631 static const unsigned int vbsize = 16;
3632 unsigned int i;
3634 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3635 0, 0, 640, 480, 0, 0, 0, 0);
3637 if (!(device = create_device(window, DDSCL_NORMAL)))
3639 skip("Failed to create a 3D device, skipping test.\n");
3640 DestroyWindow(window);
3641 return;
3644 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
3645 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
3647 memset(&desc, 0, sizeof(desc));
3648 desc.dwSize = sizeof(desc);
3649 desc.dwCaps = D3DVBCAPS_WRITEONLY;
3650 desc.dwFVF = D3DFVF_XYZRHW;
3651 desc.dwNumVertices = vbsize;
3652 hr = IDirect3D3_CreateVertexBuffer(d3d, &desc, &buffer, 0, NULL);
3653 ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr);
3655 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
3656 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
3657 memcpy(data, quad, sizeof(quad));
3658 hr = IDirect3DVertexBuffer_Unlock(buffer);
3659 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
3661 hr = IDirect3DDevice3_BeginScene(device);
3662 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3663 hr = IDirect3DDevice3_DrawPrimitiveVB(device, D3DPT_TRIANGLESTRIP, buffer, 0, 4, 0);
3664 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3665 hr = IDirect3DDevice3_EndScene(device);
3666 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
3668 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
3669 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
3670 memset(data, 0xaa, sizeof(struct vec4) * vbsize);
3671 hr = IDirect3DVertexBuffer_Unlock(buffer);
3672 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
3674 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
3675 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
3676 for (i = 0; i < sizeof(struct vec4) * vbsize; i++)
3678 if (data[i] != 0xaa)
3680 ok(FALSE, "Vertex buffer data byte %u is 0x%02x, expected 0xaa\n", i, data[i]);
3681 break;
3684 hr = IDirect3DVertexBuffer_Unlock(buffer);
3685 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
3687 IDirect3DVertexBuffer_Release(buffer);
3688 IDirect3D3_Release(d3d);
3689 IDirect3DDevice3_Release(device);
3690 DestroyWindow(window);
3693 static void test_coop_level_multi_window(void)
3695 HWND window1, window2;
3696 IDirectDraw4 *ddraw;
3697 HRESULT hr;
3699 window1 = CreateWindowA("static", "ddraw_test1", WS_OVERLAPPEDWINDOW,
3700 0, 0, 640, 480, 0, 0, 0, 0);
3701 window2 = CreateWindowA("static", "ddraw_test2", WS_OVERLAPPEDWINDOW,
3702 0, 0, 640, 480, 0, 0, 0, 0);
3703 ddraw = create_ddraw();
3704 ok(!!ddraw, "Failed to create a ddraw object.\n");
3706 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
3707 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3708 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
3709 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3710 ok(IsWindow(window1), "Window 1 was destroyed.\n");
3711 ok(IsWindow(window2), "Window 2 was destroyed.\n");
3713 IDirectDraw4_Release(ddraw);
3714 DestroyWindow(window2);
3715 DestroyWindow(window1);
3718 static void test_draw_strided(void)
3720 static struct vec3 position[] =
3722 {-1.0, -1.0, 0.0},
3723 {-1.0, 1.0, 0.0},
3724 { 1.0, 1.0, 0.0},
3725 { 1.0, -1.0, 0.0},
3727 static DWORD diffuse[] =
3729 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00,
3731 static WORD indices[] =
3733 0, 1, 2, 2, 3, 0
3736 IDirectDrawSurface4 *rt;
3737 IDirect3DDevice3 *device;
3738 D3DCOLOR color;
3739 HWND window;
3740 HRESULT hr;
3741 D3DDRAWPRIMITIVESTRIDEDDATA strided;
3742 IDirect3DViewport3 *viewport;
3743 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
3745 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3746 0, 0, 640, 480, 0, 0, 0, 0);
3748 if (!(device = create_device(window, DDSCL_NORMAL)))
3750 skip("Failed to create a 3D device, skipping test.\n");
3751 DestroyWindow(window);
3752 return;
3755 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
3756 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3757 viewport = create_viewport(device, 0, 0, 640, 480);
3758 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
3759 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
3760 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0x00000000, 0.0f, 0);
3761 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
3763 hr = IDirect3DDevice3_BeginScene(device);
3764 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3766 memset(&strided, 0x55, sizeof(strided));
3767 strided.position.lpvData = position;
3768 strided.position.dwStride = sizeof(*position);
3769 strided.diffuse.lpvData = diffuse;
3770 strided.diffuse.dwStride = sizeof(*diffuse);
3771 hr = IDirect3DDevice3_DrawIndexedPrimitiveStrided(device, D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE,
3772 &strided, 4, indices, 6, 0);
3773 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3775 hr = IDirect3DDevice3_EndScene(device);
3776 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
3778 color = get_surface_color(rt, 320, 240);
3779 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
3781 IDirect3DViewport3_Release(viewport);
3782 IDirectDrawSurface4_Release(rt);
3783 IDirect3DDevice3_Release(device);
3784 DestroyWindow(window);
3787 static void test_lighting(void)
3789 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
3790 static D3DMATRIX mat =
3792 1.0f, 0.0f, 0.0f, 0.0f,
3793 0.0f, 1.0f, 0.0f, 0.0f,
3794 0.0f, 0.0f, 1.0f, 0.0f,
3795 0.0f, 0.0f, 0.0f, 1.0f,
3797 mat_singular =
3799 1.0f, 0.0f, 1.0f, 0.0f,
3800 0.0f, 1.0f, 0.0f, 0.0f,
3801 1.0f, 0.0f, 1.0f, 0.0f,
3802 0.0f, 0.0f, 0.5f, 1.0f,
3804 mat_transf =
3806 0.0f, 0.0f, 1.0f, 0.0f,
3807 0.0f, 1.0f, 0.0f, 0.0f,
3808 -1.0f, 0.0f, 0.0f, 0.0f,
3809 10.f, 10.0f, 10.0f, 1.0f,
3811 mat_nonaffine =
3813 1.0f, 0.0f, 0.0f, 0.0f,
3814 0.0f, 1.0f, 0.0f, 0.0f,
3815 0.0f, 0.0f, 1.0f, -1.0f,
3816 10.f, 10.0f, 10.0f, 0.0f,
3818 static struct
3820 struct vec3 position;
3821 DWORD diffuse;
3823 unlitquad[] =
3825 {{-1.0f, -1.0f, 0.1f}, 0xffff0000},
3826 {{-1.0f, 0.0f, 0.1f}, 0xffff0000},
3827 {{ 0.0f, 0.0f, 0.1f}, 0xffff0000},
3828 {{ 0.0f, -1.0f, 0.1f}, 0xffff0000},
3830 litquad[] =
3832 {{-1.0f, 0.0f, 0.1f}, 0xff00ff00},
3833 {{-1.0f, 1.0f, 0.1f}, 0xff00ff00},
3834 {{ 0.0f, 1.0f, 0.1f}, 0xff00ff00},
3835 {{ 0.0f, 0.0f, 0.1f}, 0xff00ff00},
3837 static struct
3839 struct vec3 position;
3840 struct vec3 normal;
3841 DWORD diffuse;
3843 unlitnquad[] =
3845 {{0.0f, -1.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xff0000ff},
3846 {{0.0f, 0.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xff0000ff},
3847 {{1.0f, 0.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xff0000ff},
3848 {{1.0f, -1.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xff0000ff},
3850 litnquad[] =
3852 {{0.0f, 0.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xffffff00},
3853 {{0.0f, 1.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xffffff00},
3854 {{1.0f, 1.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xffffff00},
3855 {{1.0f, 0.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xffffff00},
3857 nquad[] =
3859 {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3860 {{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3861 {{ 1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3862 {{ 1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3864 rotatedquad[] =
3866 {{-10.0f, -11.0f, 11.0f}, {-1.0f, 0.0f, 0.0f}, 0xff0000ff},
3867 {{-10.0f, -9.0f, 11.0f}, {-1.0f, 0.0f, 0.0f}, 0xff0000ff},
3868 {{-10.0f, -9.0f, 9.0f}, {-1.0f, 0.0f, 0.0f}, 0xff0000ff},
3869 {{-10.0f, -11.0f, 9.0f}, {-1.0f, 0.0f, 0.0f}, 0xff0000ff},
3871 translatedquad[] =
3873 {{-11.0f, -11.0f, -10.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3874 {{-11.0f, -9.0f, -10.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3875 {{ -9.0f, -9.0f, -10.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3876 {{ -9.0f, -11.0f, -10.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3878 static WORD indices[] = {0, 1, 2, 2, 3, 0};
3879 static const struct
3881 D3DMATRIX *world_matrix;
3882 void *quad;
3883 DWORD expected;
3884 const char *message;
3886 tests[] =
3888 {&mat, nquad, 0x000000ff, "Lit quad with light"},
3889 {&mat_singular, nquad, 0x000000b4, "Lit quad with singular world matrix"},
3890 {&mat_transf, rotatedquad, 0x000000ff, "Lit quad with transformation matrix"},
3891 {&mat_nonaffine, translatedquad, 0x000000ff, "Lit quad with non-affine matrix"},
3894 HWND window;
3895 IDirect3D3 *d3d;
3896 IDirect3DDevice3 *device;
3897 IDirectDrawSurface4 *rt;
3898 IDirect3DViewport3 *viewport;
3899 IDirect3DMaterial3 *material;
3900 IDirect3DLight *light;
3901 D3DMATERIALHANDLE mat_handle;
3902 D3DLIGHT2 light_desc;
3903 HRESULT hr;
3904 DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
3905 DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
3906 D3DCOLOR color;
3907 ULONG refcount;
3908 unsigned int i;
3910 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3911 0, 0, 640, 480, 0, 0, 0, 0);
3912 if (!(device = create_device(window, DDSCL_NORMAL)))
3914 skip("Failed to create a 3D device, skipping test.\n");
3915 DestroyWindow(window);
3916 return;
3919 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
3920 ok(SUCCEEDED(hr), "Failed to get D3D interface, hr %#x.\n", hr);
3922 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
3923 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3925 viewport = create_viewport(device, 0, 0, 640, 480);
3926 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
3927 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
3929 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffffff, 0.0f, 0);
3930 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3932 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &mat);
3933 ok(SUCCEEDED(hr), "Failed to set world transformation, hr %#x.\n", hr);
3934 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &mat);
3935 ok(SUCCEEDED(hr), "Failed to set view transformation, hr %#x.\n", hr);
3936 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &mat);
3937 ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
3938 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
3939 ok(SUCCEEDED(hr), "Failed to disable clipping, hr %#x.\n", hr);
3940 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
3941 ok(SUCCEEDED(hr), "Failed to disable zbuffer, hr %#x.\n", hr);
3942 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
3943 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
3944 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_STENCILENABLE, FALSE);
3945 ok(SUCCEEDED(hr), "Failed to disable stencil buffer, hr %#x.\n", hr);
3946 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
3947 ok(SUCCEEDED(hr), "Failed to disable culling, hr %#x.\n", hr);
3949 hr = IDirect3DDevice3_BeginScene(device);
3950 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3952 /* There is no D3DRENDERSTATE_LIGHTING on ddraw < 7. */
3953 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
3954 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
3955 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, unlitquad, 4,
3956 indices, 6, 0);
3957 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3959 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
3960 ok(SUCCEEDED(hr), "Failed to enable lighting, hr %#x.\n", hr);
3961 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, litquad, 4,
3962 indices, 6, 0);
3963 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3965 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
3966 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
3967 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, unlitnquad, 4,
3968 indices, 6, 0);
3969 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3971 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
3972 ok(SUCCEEDED(hr), "Failed to enable lighting, hr %#x.\n", hr);
3973 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, litnquad, 4,
3974 indices, 6, 0);
3975 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3977 hr = IDirect3DDevice3_EndScene(device);
3978 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
3980 color = get_surface_color(rt, 160, 360);
3981 ok(color == 0x00ff0000, "Unlit quad without normals has color 0x%08x.\n", color);
3982 color = get_surface_color(rt, 160, 120);
3983 ok(color == 0x0000ff00, "Lit quad without normals has color 0x%08x.\n", color);
3984 color = get_surface_color(rt, 480, 360);
3985 ok(color == 0x000000ff, "Unlit quad with normals has color 0x%08x.\n", color);
3986 color = get_surface_color(rt, 480, 120);
3987 ok(color == 0x00ffff00, "Lit quad with normals has color 0x%08x.\n", color);
3989 material = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
3990 hr = IDirect3DMaterial3_GetHandle(material, device, &mat_handle);
3991 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
3992 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
3993 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
3995 hr = IDirect3D3_CreateLight(d3d, &light, NULL);
3996 ok(SUCCEEDED(hr), "Failed to create a light object, hr %#x.\n", hr);
3997 memset(&light_desc, 0, sizeof(light_desc));
3998 light_desc.dwSize = sizeof(light_desc);
3999 light_desc.dltType = D3DLIGHT_DIRECTIONAL;
4000 U1(light_desc.dcvColor).r = 1.0f;
4001 U2(light_desc.dcvColor).g = 1.0f;
4002 U3(light_desc.dcvColor).b = 1.0f;
4003 U4(light_desc.dcvColor).a = 1.0f;
4004 U3(light_desc.dvDirection).z = 1.0f;
4005 hr = IDirect3DLight_SetLight(light, (D3DLIGHT *)&light_desc);
4006 ok(SUCCEEDED(hr), "Failed to set light, hr %#x.\n", hr);
4007 hr = IDirect3DViewport3_AddLight(viewport, light);
4008 ok(SUCCEEDED(hr), "Failed to add a light to the viewport, hr %#x.\n", hr);
4010 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffffff, 0.0f, 0);
4011 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
4013 hr = IDirect3DDevice3_BeginScene(device);
4014 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
4016 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, nquad,
4017 4, indices, 6, 0);
4018 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
4020 hr = IDirect3DDevice3_EndScene(device);
4021 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
4023 color = get_surface_color(rt, 320, 240);
4024 ok(color == 0x00000000, "Lit quad with no light has color 0x%08x.\n", color);
4026 light_desc.dwFlags = D3DLIGHT_ACTIVE;
4027 hr = IDirect3DLight_SetLight(light, (D3DLIGHT *)&light_desc);
4028 ok(SUCCEEDED(hr), "Failed to set light, hr %#x.\n", hr);
4030 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
4032 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, tests[i].world_matrix);
4033 ok(SUCCEEDED(hr), "Failed to set world transformation, hr %#x.\n", hr);
4035 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffffff, 0.0f, 0);
4036 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
4038 hr = IDirect3DDevice3_BeginScene(device);
4039 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
4041 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, tests[i].quad,
4042 4, indices, 6, 0);
4043 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
4045 hr = IDirect3DDevice3_EndScene(device);
4046 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
4048 color = get_surface_color(rt, 320, 240);
4049 ok(color == tests[i].expected, "%s has color 0x%08x.\n", tests[i].message, color);
4052 hr = IDirect3DViewport3_DeleteLight(viewport, light);
4053 ok(SUCCEEDED(hr), "Failed to remove a light from the viewport, hr %#x.\n", hr);
4054 IDirect3DLight_Release(light);
4055 destroy_material(material);
4056 IDirect3DViewport3_Release(viewport);
4057 IDirectDrawSurface4_Release(rt);
4058 refcount = IDirect3DDevice3_Release(device);
4059 ok(!refcount, "Device has %u references left.\n", refcount);
4060 IDirect3D3_Release(d3d);
4061 DestroyWindow(window);
4064 static void test_specular_lighting(void)
4066 static const unsigned int vertices_side = 5;
4067 const unsigned int indices_count = (vertices_side - 1) * (vertices_side - 1) * 2 * 3;
4068 static const DWORD fvf = D3DFVF_XYZ | D3DFVF_NORMAL;
4069 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
4070 static D3DMATRIX mat =
4072 1.0f, 0.0f, 0.0f, 0.0f,
4073 0.0f, 1.0f, 0.0f, 0.0f,
4074 0.0f, 0.0f, 1.0f, 0.0f,
4075 0.0f, 0.0f, 0.0f, 1.0f,
4077 static D3DLIGHT2 directional =
4079 sizeof(D3DLIGHT2),
4080 D3DLIGHT_DIRECTIONAL,
4081 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
4082 {{0.0f}, {0.0f}, {0.0f}},
4083 {{0.0f}, {0.0f}, {1.0f}},
4085 point =
4087 sizeof(D3DLIGHT2),
4088 D3DLIGHT_POINT,
4089 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
4090 {{0.0f}, {0.0f}, {0.0f}},
4091 {{0.0f}, {0.0f}, {0.0f}},
4092 100.0f,
4093 0.0f,
4094 0.0f, 0.0f, 1.0f,
4096 spot =
4098 sizeof(D3DLIGHT2),
4099 D3DLIGHT_SPOT,
4100 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
4101 {{0.0f}, {0.0f}, {0.0f}},
4102 {{0.0f}, {0.0f}, {1.0f}},
4103 100.0f,
4104 1.0f,
4105 0.0f, 0.0f, 1.0f,
4106 M_PI / 12.0f, M_PI / 3.0f
4108 parallelpoint =
4110 sizeof(D3DLIGHT2),
4111 D3DLIGHT_PARALLELPOINT,
4112 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
4113 {{0.5f}, {0.0f}, {-1.0f}},
4114 {{0.0f}, {0.0f}, {0.0f}},
4116 static const struct expected_color
4118 unsigned int x, y;
4119 D3DCOLOR color;
4121 expected_directional[] =
4123 {160, 120, 0x003c3c3c},
4124 {320, 120, 0x00717171},
4125 {480, 120, 0x003c3c3c},
4126 {160, 240, 0x00717171},
4127 {320, 240, 0x00ffffff},
4128 {480, 240, 0x00717171},
4129 {160, 360, 0x003c3c3c},
4130 {320, 360, 0x00717171},
4131 {480, 360, 0x003c3c3c},
4133 expected_point[] =
4135 {160, 120, 0x00000000},
4136 {320, 120, 0x00090909},
4137 {480, 120, 0x00000000},
4138 {160, 240, 0x00090909},
4139 {320, 240, 0x00fafafa},
4140 {480, 240, 0x00090909},
4141 {160, 360, 0x00000000},
4142 {320, 360, 0x00090909},
4143 {480, 360, 0x00000000},
4145 expected_spot[] =
4147 {160, 120, 0x00000000},
4148 {320, 120, 0x00020202},
4149 {480, 120, 0x00000000},
4150 {160, 240, 0x00020202},
4151 {320, 240, 0x00fafafa},
4152 {480, 240, 0x00020202},
4153 {160, 360, 0x00000000},
4154 {320, 360, 0x00020202},
4155 {480, 360, 0x00000000},
4157 expected_parallelpoint[] =
4159 {160, 120, 0x00050505},
4160 {320, 120, 0x002c2c2c},
4161 {480, 120, 0x006e6e6e},
4162 {160, 240, 0x00090909},
4163 {320, 240, 0x00717171},
4164 {480, 240, 0x00ffffff},
4165 {160, 360, 0x00050505},
4166 {320, 360, 0x002c2c2c},
4167 {480, 360, 0x006e6e6e},
4169 static const struct
4171 D3DLIGHT2 *light;
4172 BOOL local_viewer;
4173 const struct expected_color *expected;
4174 unsigned int expected_count;
4176 tests[] =
4178 /* D3DRENDERSTATE_LOCALVIEWER does not exist in D3D < 7 (the behavior is
4179 * the one you get on newer D3D versions with it set as TRUE). */
4180 {&directional, FALSE, expected_directional,
4181 sizeof(expected_directional) / sizeof(expected_directional[0])},
4182 {&directional, TRUE, expected_directional,
4183 sizeof(expected_directional) / sizeof(expected_directional[0])},
4184 {&point, TRUE, expected_point,
4185 sizeof(expected_point) / sizeof(expected_point[0])},
4186 {&spot, TRUE, expected_spot,
4187 sizeof(expected_spot) / sizeof(expected_spot[0])},
4188 {&parallelpoint, TRUE, expected_parallelpoint,
4189 sizeof(expected_parallelpoint) / sizeof(expected_parallelpoint[0])},
4191 IDirect3D3 *d3d;
4192 IDirect3DDevice3 *device;
4193 IDirectDrawSurface4 *rt;
4194 IDirect3DViewport3 *viewport;
4195 IDirect3DMaterial3 *material;
4196 IDirect3DLight *light;
4197 D3DMATERIALHANDLE mat_handle;
4198 D3DCOLOR color;
4199 ULONG refcount;
4200 HWND window;
4201 HRESULT hr;
4202 unsigned int i, j, x, y;
4203 struct
4205 struct vec3 position;
4206 struct vec3 normal;
4207 } *quad;
4208 WORD *indices;
4210 quad = HeapAlloc(GetProcessHeap(), 0, vertices_side * vertices_side * sizeof(*quad));
4211 indices = HeapAlloc(GetProcessHeap(), 0, indices_count * sizeof(*indices));
4212 for (i = 0, y = 0; y < vertices_side; ++y)
4214 for (x = 0; x < vertices_side; ++x)
4216 quad[i].position.x = x * 2.0f / (vertices_side - 1) - 1.0f;
4217 quad[i].position.y = y * 2.0f / (vertices_side - 1) - 1.0f;
4218 quad[i].position.z = 1.0f;
4219 quad[i].normal.x = 0.0f;
4220 quad[i].normal.y = 0.0f;
4221 quad[i++].normal.z = -1.0f;
4224 for (i = 0, y = 0; y < (vertices_side - 1); ++y)
4226 for (x = 0; x < (vertices_side - 1); ++x)
4228 indices[i++] = y * vertices_side + x + 1;
4229 indices[i++] = y * vertices_side + x;
4230 indices[i++] = (y + 1) * vertices_side + x;
4231 indices[i++] = y * vertices_side + x + 1;
4232 indices[i++] = (y + 1) * vertices_side + x;
4233 indices[i++] = (y + 1) * vertices_side + x + 1;
4237 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4238 0, 0, 640, 480, 0, 0, 0, 0);
4239 if (!(device = create_device(window, DDSCL_NORMAL)))
4241 skip("Failed to create a 3D device, skipping test.\n");
4242 DestroyWindow(window);
4243 return;
4246 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
4247 ok(SUCCEEDED(hr), "Failed to get D3D interface, hr %#x.\n", hr);
4249 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
4250 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
4252 viewport = create_viewport(device, 0, 0, 640, 480);
4253 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
4254 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
4256 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &mat);
4257 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
4258 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &mat);
4259 ok(SUCCEEDED(hr), "Failed to set view transform, hr %#x.\n", hr);
4260 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &mat);
4261 ok(SUCCEEDED(hr), "Failed to set projection transform, hr %#x.\n", hr);
4262 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
4263 ok(SUCCEEDED(hr), "Failed to disable clipping, hr %#x.\n", hr);
4264 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
4265 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
4266 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
4267 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
4269 material = create_specular_material(device, 1.0f, 1.0f, 1.0f, 1.0f, 30.0f);
4270 hr = IDirect3DMaterial3_GetHandle(material, device, &mat_handle);
4271 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
4272 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
4273 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
4275 hr = IDirect3D3_CreateLight(d3d, &light, NULL);
4276 ok(SUCCEEDED(hr), "Failed to create a light object, hr %#x.\n", hr);
4277 hr = IDirect3DViewport3_AddLight(viewport, light);
4278 ok(SUCCEEDED(hr), "Failed to add a light to the viewport, hr %#x.\n", hr);
4280 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_SPECULARENABLE, TRUE);
4281 ok(SUCCEEDED(hr), "Failed to enable specular lighting, hr %#x.\n", hr);
4283 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
4285 tests[i].light->dwFlags = D3DLIGHT_ACTIVE;
4286 hr = IDirect3DLight_SetLight(light, (D3DLIGHT *)tests[i].light);
4287 ok(SUCCEEDED(hr), "Failed to set light, hr %#x.\n", hr);
4289 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LOCALVIEWER, tests[i].local_viewer);
4290 ok(SUCCEEDED(hr), "Failed to set local viewer state, hr %#x.\n", hr);
4292 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffffff, 0.0f, 0);
4293 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
4295 hr = IDirect3DDevice3_BeginScene(device);
4296 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
4298 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, quad,
4299 vertices_side * vertices_side, indices, indices_count, 0);
4300 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
4302 hr = IDirect3DDevice3_EndScene(device);
4303 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
4305 for (j = 0; j < tests[i].expected_count; ++j)
4307 color = get_surface_color(rt, tests[i].expected[j].x, tests[i].expected[j].y);
4308 ok(compare_color(color, tests[i].expected[j].color, 1),
4309 "Expected color 0x%08x at location (%u, %u), got 0x%08x, case %u.\n",
4310 tests[i].expected[j].color, tests[i].expected[j].x,
4311 tests[i].expected[j].y, color, i);
4315 hr = IDirect3DViewport3_DeleteLight(viewport, light);
4316 ok(SUCCEEDED(hr), "Failed to remove a light from the viewport, hr %#x.\n", hr);
4317 IDirect3DLight_Release(light);
4318 destroy_material(material);
4319 IDirect3DViewport3_Release(viewport);
4320 IDirectDrawSurface4_Release(rt);
4321 refcount = IDirect3DDevice3_Release(device);
4322 ok(!refcount, "Device has %u references left.\n", refcount);
4323 IDirect3D3_Release(d3d);
4324 DestroyWindow(window);
4325 HeapFree(GetProcessHeap(), 0, indices);
4326 HeapFree(GetProcessHeap(), 0, quad);
4329 static void test_clear_rect_count(void)
4331 IDirectDrawSurface4 *rt;
4332 IDirect3DDevice3 *device;
4333 D3DCOLOR color;
4334 HWND window;
4335 HRESULT hr;
4336 IDirect3DViewport3 *viewport;
4337 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
4339 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4340 0, 0, 640, 480, 0, 0, 0, 0);
4341 if (!(device = create_device(window, DDSCL_NORMAL)))
4343 skip("Failed to create a 3D device, skipping test.\n");
4344 DestroyWindow(window);
4345 return;
4348 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
4349 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
4351 viewport = create_viewport(device, 0, 0, 640, 480);
4352 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
4353 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
4354 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0x00ffffff, 0.0f, 0);
4355 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
4356 hr = IDirect3DViewport3_Clear2(viewport, 0, &clear_rect, D3DCLEAR_TARGET, 0x00ff0000, 0.0f, 0);
4357 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
4358 hr = IDirect3DViewport3_Clear2(viewport, 0, NULL, D3DCLEAR_TARGET, 0x0000ff00, 0.0f, 0);
4359 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
4360 hr = IDirect3DViewport3_Clear2(viewport, 1, NULL, D3DCLEAR_TARGET, 0x000000ff, 0.0f, 0);
4361 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
4363 color = get_surface_color(rt, 320, 240);
4364 ok(compare_color(color, 0x00ffffff, 1) || broken(compare_color(color, 0x000000ff, 1)),
4365 "Got unexpected color 0x%08x.\n", color);
4367 IDirect3DViewport3_Release(viewport);
4368 IDirectDrawSurface4_Release(rt);
4369 IDirect3DDevice3_Release(device);
4370 DestroyWindow(window);
4373 static BOOL test_mode_restored(IDirectDraw4 *ddraw, HWND window)
4375 DDSURFACEDESC2 ddsd1, ddsd2;
4376 HRESULT hr;
4378 memset(&ddsd1, 0, sizeof(ddsd1));
4379 ddsd1.dwSize = sizeof(ddsd1);
4380 hr = IDirectDraw4_GetDisplayMode(ddraw, &ddsd1);
4381 ok(SUCCEEDED(hr), "GetDisplayMode failed, hr %#x.\n", hr);
4383 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4384 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4385 hr = set_display_mode(ddraw, 640, 480);
4386 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
4387 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4388 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4390 memset(&ddsd2, 0, sizeof(ddsd2));
4391 ddsd2.dwSize = sizeof(ddsd2);
4392 hr = IDirectDraw4_GetDisplayMode(ddraw, &ddsd2);
4393 ok(SUCCEEDED(hr), "GetDisplayMode failed, hr %#x.\n", hr);
4394 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
4395 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
4397 return ddsd1.dwWidth == ddsd2.dwWidth && ddsd1.dwHeight == ddsd2.dwHeight;
4400 static void test_coop_level_versions(void)
4402 HWND window;
4403 IDirectDraw *ddraw;
4404 HRESULT hr;
4405 BOOL restored;
4406 IDirectDrawSurface *surface;
4407 IDirectDraw4 *ddraw4;
4408 DDSURFACEDESC ddsd;
4410 window = CreateWindowA("static", "ddraw_test1", WS_OVERLAPPEDWINDOW,
4411 0, 0, 640, 480, 0, 0, 0, 0);
4413 ddraw4 = create_ddraw();
4414 ok(!!ddraw4, "Failed to create a ddraw object.\n");
4415 /* Newly created ddraw objects restore the mode on ddraw2+::SetCooperativeLevel(NORMAL) */
4416 restored = test_mode_restored(ddraw4, window);
4417 ok(restored, "Display mode not restored in new ddraw object\n");
4419 /* A failing ddraw1::SetCooperativeLevel call does not have an effect */
4420 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
4421 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
4423 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
4424 ok(FAILED(hr), "SetCooperativeLevel returned %#x, expected failure.\n", hr);
4425 restored = test_mode_restored(ddraw4, window);
4426 ok(restored, "Display mode not restored after bad ddraw1::SetCooperativeLevel call\n");
4428 /* A successful one does */
4429 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4430 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4431 restored = test_mode_restored(ddraw4, window);
4432 ok(!restored, "Display mode restored after good ddraw1::SetCooperativeLevel call\n");
4434 IDirectDraw_Release(ddraw);
4435 IDirectDraw4_Release(ddraw4);
4437 ddraw4 = create_ddraw();
4438 ok(!!ddraw4, "Failed to create a ddraw object.\n");
4439 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
4440 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
4442 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_SETFOCUSWINDOW);
4443 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4444 restored = test_mode_restored(ddraw4, window);
4445 ok(!restored, "Display mode restored after ddraw1::SetCooperativeLevel(SETFOCUSWINDOW) call\n");
4447 IDirectDraw_Release(ddraw);
4448 IDirectDraw4_Release(ddraw4);
4450 /* A failing call does not restore the ddraw2+ behavior */
4451 ddraw4 = create_ddraw();
4452 ok(!!ddraw4, "Failed to create a ddraw object.\n");
4453 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
4454 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
4456 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4457 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4458 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
4459 ok(FAILED(hr), "SetCooperativeLevel returned %#x, expected failure.\n", hr);
4460 restored = test_mode_restored(ddraw4, window);
4461 ok(!restored, "Display mode restored after good-bad ddraw1::SetCooperativeLevel() call sequence\n");
4463 IDirectDraw_Release(ddraw);
4464 IDirectDraw4_Release(ddraw4);
4466 /* Neither does a sequence of successful calls with the new interface */
4467 ddraw4 = create_ddraw();
4468 ok(!!ddraw4, "Failed to create a ddraw object.\n");
4469 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
4470 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
4472 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4473 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4474 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
4475 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4476 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, DDSCL_NORMAL);
4477 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4479 restored = test_mode_restored(ddraw4, window);
4480 ok(!restored, "Display mode restored after ddraw1-ddraw4 SetCooperativeLevel() call sequence\n");
4481 IDirectDraw_Release(ddraw);
4482 IDirectDraw4_Release(ddraw4);
4484 /* ddraw1::CreateSurface does not triger the ddraw1 behavior */
4485 ddraw4 = create_ddraw();
4486 ok(!!ddraw4, "Failed to create a ddraw object.\n");
4487 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
4488 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
4490 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, DDSCL_NORMAL);
4491 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4493 memset(&ddsd, 0, sizeof(ddsd));
4494 ddsd.dwSize = sizeof(ddsd);
4495 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
4496 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
4497 ddsd.dwWidth = ddsd.dwHeight = 8;
4498 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
4499 ok(SUCCEEDED(hr), "CreateSurface failed, hr %#x.\n", hr);
4500 IDirectDrawSurface_Release(surface);
4501 restored = test_mode_restored(ddraw4, window);
4502 ok(restored, "Display mode not restored after ddraw1::CreateSurface() call\n");
4504 IDirectDraw_Release(ddraw);
4505 IDirectDraw4_Release(ddraw4);
4506 DestroyWindow(window);
4509 static void test_lighting_interface_versions(void)
4511 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
4512 IDirect3DMaterial3 *emissive;
4513 IDirect3DViewport3 *viewport;
4514 IDirect3DDevice3 *device;
4515 IDirectDrawSurface4 *rt;
4516 D3DCOLOR color;
4517 HWND window;
4518 HRESULT hr;
4519 D3DMATERIALHANDLE mat_handle;
4520 DWORD rs;
4521 unsigned int i;
4522 ULONG ref;
4523 static D3DVERTEX quad[] =
4525 {{-1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
4526 {{ 1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
4527 {{-1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
4528 {{ 1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
4531 #define FVF_COLORVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR)
4532 static struct
4534 struct vec3 position;
4535 struct vec3 normal;
4536 DWORD diffuse, specular;
4538 quad2[] =
4540 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
4541 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
4542 {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
4543 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
4546 static D3DLVERTEX lquad[] =
4548 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
4549 {{ 1.0f}, { 1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
4550 {{-1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
4551 {{ 1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
4554 #define FVF_LVERTEX2 (D3DFVF_LVERTEX & ~D3DFVF_RESERVED1)
4555 static struct
4557 struct vec3 position;
4558 DWORD diffuse, specular;
4559 struct vec2 texcoord;
4561 lquad2[] =
4563 {{-1.0f, 1.0f, 0.0f}, 0xffff0000, 0xff808080},
4564 {{ 1.0f, 1.0f, 0.0f}, 0xffff0000, 0xff808080},
4565 {{-1.0f, -1.0f, 0.0f}, 0xffff0000, 0xff808080},
4566 {{ 1.0f, -1.0f, 0.0f}, 0xffff0000, 0xff808080},
4569 static D3DTLVERTEX tlquad[] =
4571 {{ 0.0f}, { 480.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
4572 {{ 0.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
4573 {{ 640.0f}, { 480.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
4574 {{ 640.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
4577 static const struct
4579 DWORD vertextype;
4580 void *data;
4581 DWORD d3drs_lighting, d3drs_specular;
4582 DWORD draw_flags;
4583 D3DCOLOR color;
4585 tests[] =
4587 /* Lighting is enabled when all of these conditions are met:
4588 * 1) No pretransformed position(D3DFVF_XYZRHW)
4589 * 2) Normals are available (D3DFVF_NORMAL)
4590 * 3) D3DDP_DONOTLIGHT is not set.
4592 * D3DRENDERSTATE_LIGHTING is ignored, it is not defined
4593 * in this d3d version */
4595 /* 0 */
4596 { D3DFVF_VERTEX, quad, FALSE, FALSE, 0, 0x0000ff00},
4597 { D3DFVF_VERTEX, quad, TRUE, FALSE, 0, 0x0000ff00},
4598 { D3DFVF_VERTEX, quad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ffffff},
4599 { D3DFVF_VERTEX, quad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ffffff},
4600 { D3DFVF_VERTEX, quad, FALSE, TRUE, 0, 0x0000ff00},
4601 { D3DFVF_VERTEX, quad, TRUE, TRUE, 0, 0x0000ff00},
4602 { D3DFVF_VERTEX, quad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ffffff},
4603 { D3DFVF_VERTEX, quad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ffffff},
4605 /* 8 */
4606 { FVF_COLORVERTEX, quad2, FALSE, FALSE, 0, 0x0000ff00},
4607 { FVF_COLORVERTEX, quad2, TRUE, FALSE, 0, 0x0000ff00},
4608 { FVF_COLORVERTEX, quad2, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4609 { FVF_COLORVERTEX, quad2, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4610 /* The specular color in the vertex is ignored because
4611 * D3DRENDERSTATE_COLORVERTEX is not enabled */
4612 { FVF_COLORVERTEX, quad2, FALSE, TRUE, 0, 0x0000ff00},
4613 { FVF_COLORVERTEX, quad2, TRUE, TRUE, 0, 0x0000ff00},
4614 { FVF_COLORVERTEX, quad2, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4615 { FVF_COLORVERTEX, quad2, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4617 /* 16 */
4618 { D3DFVF_LVERTEX, lquad, FALSE, FALSE, 0, 0x00ff0000},
4619 { D3DFVF_LVERTEX, lquad, TRUE, FALSE, 0, 0x00ff0000},
4620 { D3DFVF_LVERTEX, lquad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4621 { D3DFVF_LVERTEX, lquad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4622 { D3DFVF_LVERTEX, lquad, FALSE, TRUE, 0, 0x00ff8080},
4623 { D3DFVF_LVERTEX, lquad, TRUE, TRUE, 0, 0x00ff8080},
4624 { D3DFVF_LVERTEX, lquad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4625 { D3DFVF_LVERTEX, lquad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4627 /* 24 */
4628 { FVF_LVERTEX2, lquad2, FALSE, FALSE, 0, 0x00ff0000},
4629 { FVF_LVERTEX2, lquad2, TRUE, FALSE, 0, 0x00ff0000},
4630 { FVF_LVERTEX2, lquad2, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4631 { FVF_LVERTEX2, lquad2, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4632 { FVF_LVERTEX2, lquad2, FALSE, TRUE, 0, 0x00ff8080},
4633 { FVF_LVERTEX2, lquad2, TRUE, TRUE, 0, 0x00ff8080},
4634 { FVF_LVERTEX2, lquad2, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4635 { FVF_LVERTEX2, lquad2, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4637 /* 32 */
4638 { D3DFVF_TLVERTEX, tlquad, FALSE, FALSE, 0, 0x000000ff},
4639 { D3DFVF_TLVERTEX, tlquad, TRUE, FALSE, 0, 0x000000ff},
4640 { D3DFVF_TLVERTEX, tlquad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x000000ff},
4641 { D3DFVF_TLVERTEX, tlquad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x000000ff},
4642 { D3DFVF_TLVERTEX, tlquad, FALSE, TRUE, 0, 0x008080ff},
4643 { D3DFVF_TLVERTEX, tlquad, TRUE, TRUE, 0, 0x008080ff},
4644 { D3DFVF_TLVERTEX, tlquad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x008080ff},
4645 { D3DFVF_TLVERTEX, tlquad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x008080ff},
4648 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4649 0, 0, 640, 480, 0, 0, 0, 0);
4651 if (!(device = create_device(window, DDSCL_NORMAL)))
4653 skip("Failed to create a 3D device, skipping test.\n");
4654 DestroyWindow(window);
4655 return;
4658 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
4659 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
4661 viewport = create_viewport(device, 0, 0, 640, 480);
4662 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
4663 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
4665 emissive = create_emissive_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
4666 hr = IDirect3DMaterial3_GetHandle(emissive, device, &mat_handle);
4667 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
4668 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
4669 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
4670 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
4671 ok(SUCCEEDED(hr), "Failed to disable z test, hr %#x.\n", hr);
4673 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_SPECULARENABLE, &rs);
4674 ok(SUCCEEDED(hr), "Failed to get specularenable render state, hr %#x.\n", hr);
4675 ok(rs == FALSE, "Initial D3DRENDERSTATE_SPECULARENABLE is %#x, expected FALSE.\n", rs);
4677 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
4679 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff202020, 0.0f, 0);
4680 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
4682 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, tests[i].d3drs_lighting);
4683 ok(SUCCEEDED(hr), "Failed to set lighting render state, hr %#x.\n", hr);
4684 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_SPECULARENABLE,
4685 tests[i].d3drs_specular);
4686 ok(SUCCEEDED(hr), "Failed to set specularenable render state, hr %#x.\n", hr);
4688 hr = IDirect3DDevice3_BeginScene(device);
4689 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
4690 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP,
4691 tests[i].vertextype, tests[i].data, 4, tests[i].draw_flags | D3DDP_WAIT);
4692 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
4693 hr = IDirect3DDevice3_EndScene(device);
4694 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
4696 color = get_surface_color(rt, 320, 240);
4697 ok(compare_color(color, tests[i].color, 1),
4698 "Got unexpected color 0x%08x, expected 0x%08x, test %u.\n",
4699 color, tests[i].color, i);
4702 destroy_material(emissive);
4703 IDirectDrawSurface4_Release(rt);
4704 ref = IDirect3DDevice3_Release(device);
4705 ok(ref == 0, "Device not properly released, refcount %u.\n", ref);
4706 DestroyWindow(window);
4709 static struct
4711 BOOL received;
4712 IDirectDraw4 *ddraw;
4713 HWND window;
4714 DWORD coop_level;
4715 } activateapp_testdata;
4717 static LRESULT CALLBACK activateapp_test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
4719 if (message == WM_ACTIVATEAPP)
4721 if (activateapp_testdata.ddraw)
4723 HRESULT hr;
4724 activateapp_testdata.received = FALSE;
4725 hr = IDirectDraw4_SetCooperativeLevel(activateapp_testdata.ddraw,
4726 activateapp_testdata.window, activateapp_testdata.coop_level);
4727 ok(SUCCEEDED(hr), "Recursive SetCooperativeLevel call failed, hr %#x.\n", hr);
4728 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP during recursive SetCooperativeLevel call.\n");
4730 activateapp_testdata.received = TRUE;
4733 return DefWindowProcA(hwnd, message, wparam, lparam);
4736 static void test_coop_level_activateapp(void)
4738 IDirectDraw4 *ddraw;
4739 HRESULT hr;
4740 HWND window;
4741 WNDCLASSA wc = {0};
4742 DDSURFACEDESC2 ddsd;
4743 IDirectDrawSurface4 *surface;
4745 ddraw = create_ddraw();
4746 ok(!!ddraw, "Failed to create a ddraw object.\n");
4748 wc.lpfnWndProc = activateapp_test_proc;
4749 wc.lpszClassName = "ddraw_test_wndproc_wc";
4750 ok(RegisterClassA(&wc), "Failed to register window class.\n");
4752 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
4753 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
4755 /* Exclusive with window already active. */
4756 SetForegroundWindow(window);
4757 activateapp_testdata.received = FALSE;
4758 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4759 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4760 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP although window was already active.\n");
4761 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4762 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4764 /* Exclusive with window not active. */
4765 SetForegroundWindow(GetDesktopWindow());
4766 activateapp_testdata.received = FALSE;
4767 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4768 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4769 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4770 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4771 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4773 /* Normal with window not active, then exclusive with the same window. */
4774 SetForegroundWindow(GetDesktopWindow());
4775 activateapp_testdata.received = FALSE;
4776 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4777 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4778 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP when setting DDSCL_NORMAL.\n");
4779 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4780 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4781 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4782 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4783 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4785 /* Recursive set of DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN. */
4786 SetForegroundWindow(GetDesktopWindow());
4787 activateapp_testdata.received = FALSE;
4788 activateapp_testdata.ddraw = ddraw;
4789 activateapp_testdata.window = window;
4790 activateapp_testdata.coop_level = DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN;
4791 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4792 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4793 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4794 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4795 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4797 /* The recursive call seems to have some bad effect on native ddraw, despite (apparently)
4798 * succeeding. Another switch to exclusive and back to normal is needed to release the
4799 * window properly. Without doing this, SetCooperativeLevel(EXCLUSIVE) will not send
4800 * WM_ACTIVATEAPP messages. */
4801 activateapp_testdata.ddraw = NULL;
4802 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4803 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4804 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4805 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4807 /* Setting DDSCL_NORMAL with recursive invocation. */
4808 SetForegroundWindow(GetDesktopWindow());
4809 activateapp_testdata.received = FALSE;
4810 activateapp_testdata.ddraw = ddraw;
4811 activateapp_testdata.window = window;
4812 activateapp_testdata.coop_level = DDSCL_NORMAL;
4813 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4814 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4815 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4817 /* DDraw is in exlusive mode now. */
4818 memset(&ddsd, 0, sizeof(ddsd));
4819 ddsd.dwSize = sizeof(ddsd);
4820 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
4821 U5(ddsd).dwBackBufferCount = 1;
4822 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
4823 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
4824 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4825 IDirectDrawSurface4_Release(surface);
4827 /* Recover again, just to be sure. */
4828 activateapp_testdata.ddraw = NULL;
4829 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4830 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4831 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4832 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4834 DestroyWindow(window);
4835 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
4836 IDirectDraw4_Release(ddraw);
4839 static void test_texturemanage(void)
4841 IDirectDraw4 *ddraw;
4842 HRESULT hr;
4843 DDSURFACEDESC2 ddsd;
4844 IDirectDrawSurface4 *surface;
4845 unsigned int i;
4846 DDCAPS hal_caps, hel_caps;
4847 DWORD needed_caps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
4848 static const struct
4850 DWORD caps_in, caps2_in;
4851 HRESULT hr;
4852 DWORD caps_out, caps2_out;
4854 tests[] =
4856 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4857 ~0U, ~0U},
4858 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4859 ~0U, ~0U},
4860 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4861 ~0U, ~0U},
4862 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4863 ~0U, ~0U},
4864 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DD_OK,
4865 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE},
4866 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DD_OK,
4867 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE},
4868 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, 0, DD_OK,
4869 DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE | DDSCAPS_LOCALVIDMEM, 0},
4870 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0, DD_OK,
4871 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0},
4873 {0, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4874 ~0U, ~0U},
4875 {0, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4876 ~0U, ~0U},
4877 {DDSCAPS_SYSTEMMEMORY, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4878 ~0U, ~0U},
4879 {DDSCAPS_SYSTEMMEMORY, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4880 ~0U, ~0U},
4881 {DDSCAPS_VIDEOMEMORY, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4882 ~0U, ~0U},
4883 {DDSCAPS_VIDEOMEMORY, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4884 ~0U, ~0U},
4885 {DDSCAPS_VIDEOMEMORY, 0, DD_OK,
4886 DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY, 0},
4887 {DDSCAPS_SYSTEMMEMORY, 0, DD_OK,
4888 DDSCAPS_SYSTEMMEMORY, 0},
4891 ddraw = create_ddraw();
4892 ok(!!ddraw, "Failed to create a ddraw object.\n");
4893 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4894 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4896 memset(&hal_caps, 0, sizeof(hal_caps));
4897 hal_caps.dwSize = sizeof(hal_caps);
4898 memset(&hel_caps, 0, sizeof(hel_caps));
4899 hel_caps.dwSize = sizeof(hel_caps);
4900 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, &hel_caps);
4901 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
4902 if ((hal_caps.ddsCaps.dwCaps & needed_caps) != needed_caps)
4904 skip("Managed textures not supported, skipping managed texture test.\n");
4905 IDirectDraw4_Release(ddraw);
4906 return;
4909 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
4911 memset(&ddsd, 0, sizeof(ddsd));
4912 ddsd.dwSize = sizeof(ddsd);
4913 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
4914 ddsd.ddsCaps.dwCaps = tests[i].caps_in;
4915 ddsd.ddsCaps.dwCaps2 = tests[i].caps2_in;
4916 ddsd.dwWidth = 4;
4917 ddsd.dwHeight = 4;
4919 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
4920 ok(hr == tests[i].hr, "Got unexpected, hr %#x, case %u.\n", hr, i);
4921 if (FAILED(hr))
4922 continue;
4924 memset(&ddsd, 0, sizeof(ddsd));
4925 ddsd.dwSize = sizeof(ddsd);
4926 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
4927 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4929 ok(ddsd.ddsCaps.dwCaps == tests[i].caps_out,
4930 "Input caps %#x, %#x, expected output caps %#x, got %#x, case %u.\n",
4931 tests[i].caps_in, tests[i].caps2_in, tests[i].caps_out, ddsd.ddsCaps.dwCaps, i);
4932 ok(ddsd.ddsCaps.dwCaps2 == tests[i].caps2_out,
4933 "Input caps %#x, %#x, expected output caps %#x, got %#x, case %u.\n",
4934 tests[i].caps_in, tests[i].caps2_in, tests[i].caps2_out, ddsd.ddsCaps.dwCaps2, i);
4936 IDirectDrawSurface4_Release(surface);
4939 IDirectDraw4_Release(ddraw);
4942 #define SUPPORT_DXT1 0x01
4943 #define SUPPORT_DXT2 0x02
4944 #define SUPPORT_DXT3 0x04
4945 #define SUPPORT_DXT4 0x08
4946 #define SUPPORT_DXT5 0x10
4947 #define SUPPORT_YUY2 0x20
4948 #define SUPPORT_UYVY 0x40
4950 static HRESULT WINAPI test_block_formats_creation_cb(DDPIXELFORMAT *fmt, void *ctx)
4952 DWORD *supported_fmts = ctx;
4954 if (!(fmt->dwFlags & DDPF_FOURCC))
4955 return DDENUMRET_OK;
4957 switch (fmt->dwFourCC)
4959 case MAKEFOURCC('D','X','T','1'):
4960 *supported_fmts |= SUPPORT_DXT1;
4961 break;
4962 case MAKEFOURCC('D','X','T','2'):
4963 *supported_fmts |= SUPPORT_DXT2;
4964 break;
4965 case MAKEFOURCC('D','X','T','3'):
4966 *supported_fmts |= SUPPORT_DXT3;
4967 break;
4968 case MAKEFOURCC('D','X','T','4'):
4969 *supported_fmts |= SUPPORT_DXT4;
4970 break;
4971 case MAKEFOURCC('D','X','T','5'):
4972 *supported_fmts |= SUPPORT_DXT5;
4973 break;
4974 case MAKEFOURCC('Y','U','Y','2'):
4975 *supported_fmts |= SUPPORT_YUY2;
4976 break;
4977 case MAKEFOURCC('U','Y','V','Y'):
4978 *supported_fmts |= SUPPORT_UYVY;
4979 break;
4980 default:
4981 break;
4984 return DDENUMRET_OK;
4987 static void test_block_formats_creation(void)
4989 HRESULT hr, expect_hr;
4990 unsigned int i, j, w, h;
4991 HWND window;
4992 IDirectDraw4 *ddraw;
4993 IDirect3D3 *d3d;
4994 IDirect3DDevice3 *device;
4995 IDirectDrawSurface4 *surface;
4996 DWORD supported_fmts = 0, supported_overlay_fmts = 0;
4997 DWORD num_fourcc_codes = 0, *fourcc_codes;
4998 DDSURFACEDESC2 ddsd;
4999 DDCAPS hal_caps;
5000 void *mem;
5002 static const struct
5004 DWORD fourcc;
5005 const char *name;
5006 DWORD support_flag;
5007 unsigned int block_width;
5008 unsigned int block_height;
5009 unsigned int block_size;
5010 BOOL create_size_checked, overlay;
5012 formats[] =
5014 {MAKEFOURCC('D','X','T','1'), "D3DFMT_DXT1", SUPPORT_DXT1, 4, 4, 8, TRUE, FALSE},
5015 {MAKEFOURCC('D','X','T','2'), "D3DFMT_DXT2", SUPPORT_DXT2, 4, 4, 16, TRUE, FALSE},
5016 {MAKEFOURCC('D','X','T','3'), "D3DFMT_DXT3", SUPPORT_DXT3, 4, 4, 16, TRUE, FALSE},
5017 {MAKEFOURCC('D','X','T','4'), "D3DFMT_DXT4", SUPPORT_DXT4, 4, 4, 16, TRUE, FALSE},
5018 {MAKEFOURCC('D','X','T','5'), "D3DFMT_DXT5", SUPPORT_DXT5, 4, 4, 16, TRUE, FALSE},
5019 {MAKEFOURCC('Y','U','Y','2'), "D3DFMT_YUY2", SUPPORT_YUY2, 2, 1, 4, FALSE, TRUE },
5020 {MAKEFOURCC('U','Y','V','Y'), "D3DFMT_UYVY", SUPPORT_UYVY, 2, 1, 4, FALSE, TRUE },
5022 static const struct
5024 DWORD caps, caps2;
5025 const char *name;
5026 BOOL overlay;
5028 types[] =
5030 /* DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY fails to create any fourcc
5031 * surface with DDERR_INVALIDPIXELFORMAT. Don't care about it for now.
5033 * Nvidia returns E_FAIL on DXTN DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY.
5034 * Other hw / drivers successfully create those surfaces. Ignore them, this
5035 * suggests that no game uses this, otherwise Nvidia would support it. */
5037 DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, 0,
5038 "videomemory texture", FALSE
5041 DDSCAPS_VIDEOMEMORY | DDSCAPS_OVERLAY, 0,
5042 "videomemory overlay", TRUE
5045 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0,
5046 "systemmemory texture", FALSE
5049 DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE,
5050 "managed texture", FALSE
5053 enum size_type
5055 SIZE_TYPE_ZERO,
5056 SIZE_TYPE_PITCH,
5057 SIZE_TYPE_SIZE,
5059 static const struct
5061 DWORD flags;
5062 enum size_type size_type;
5063 int rel_size;
5064 HRESULT hr;
5066 user_mem_tests[] =
5068 {DDSD_LINEARSIZE, SIZE_TYPE_ZERO, 0, DD_OK},
5069 {DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DD_OK},
5070 {DDSD_PITCH, SIZE_TYPE_ZERO, 0, DD_OK},
5071 {DDSD_PITCH, SIZE_TYPE_PITCH, 0, DD_OK},
5072 {DDSD_LPSURFACE, SIZE_TYPE_ZERO, 0, DDERR_INVALIDPARAMS},
5073 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_ZERO, 0, DDERR_INVALIDPARAMS},
5074 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_PITCH, 0, DDERR_INVALIDPARAMS},
5075 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DD_OK},
5076 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 1, DD_OK},
5077 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, -1, DDERR_INVALIDPARAMS},
5078 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_ZERO, 0, DD_OK},
5079 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_PITCH, 0, DD_OK},
5080 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_SIZE, 0, DD_OK},
5081 {DDSD_LPSURFACE | DDSD_PITCH | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DD_OK},
5084 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5085 0, 0, 640, 480, 0, 0, 0, 0);
5087 if (!(device = create_device(window, DDSCL_NORMAL)))
5089 skip("Failed to create a 3D device, skipping test.\n");
5090 DestroyWindow(window);
5091 return;
5094 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
5095 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
5096 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **) &ddraw);
5097 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
5098 IDirect3D3_Release(d3d);
5100 hr = IDirect3DDevice3_EnumTextureFormats(device, test_block_formats_creation_cb,
5101 &supported_fmts);
5102 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
5104 hr = IDirectDraw4_GetFourCCCodes(ddraw, &num_fourcc_codes, NULL);
5105 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
5106 fourcc_codes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
5107 num_fourcc_codes * sizeof(*fourcc_codes));
5108 if (!fourcc_codes)
5109 goto cleanup;
5110 hr = IDirectDraw4_GetFourCCCodes(ddraw, &num_fourcc_codes, fourcc_codes);
5111 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
5112 for (i = 0; i < num_fourcc_codes; i++)
5114 for (j = 0; j < sizeof(formats) / sizeof(*formats); j++)
5116 if (fourcc_codes[i] == formats[j].fourcc)
5117 supported_overlay_fmts |= formats[j].support_flag;
5120 HeapFree(GetProcessHeap(), 0, fourcc_codes);
5122 memset(&hal_caps, 0, sizeof(hal_caps));
5123 hal_caps.dwSize = sizeof(hal_caps);
5124 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
5125 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
5127 mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 2 * 2 * 16 + 1);
5129 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
5131 for (j = 0; j < sizeof(types) / sizeof(*types); j++)
5133 BOOL support;
5135 if (formats[i].overlay != types[j].overlay
5136 || (types[j].overlay && !(hal_caps.dwCaps & DDCAPS_OVERLAY)))
5137 continue;
5139 if (formats[i].overlay)
5140 support = supported_overlay_fmts & formats[i].support_flag;
5141 else
5142 support = supported_fmts & formats[i].support_flag;
5144 for (w = 1; w <= 8; w++)
5146 for (h = 1; h <= 8; h++)
5148 BOOL block_aligned = TRUE;
5149 BOOL todo = FALSE;
5151 if (w & (formats[i].block_width - 1) || h & (formats[i].block_height - 1))
5152 block_aligned = FALSE;
5154 memset(&ddsd, 0, sizeof(ddsd));
5155 ddsd.dwSize = sizeof(ddsd);
5156 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
5157 ddsd.ddsCaps.dwCaps = types[j].caps;
5158 ddsd.ddsCaps.dwCaps2 = types[j].caps2;
5159 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5160 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
5161 U4(ddsd).ddpfPixelFormat.dwFourCC = formats[i].fourcc;
5162 ddsd.dwWidth = w;
5163 ddsd.dwHeight = h;
5165 /* TODO: Handle power of two limitations. I cannot test the pow2
5166 * behavior on windows because I have no hardware that doesn't at
5167 * least support np2_conditional. There's probably no HW that
5168 * supports DXTN textures but no conditional np2 textures. */
5169 if (!support && !(types[j].caps & DDSCAPS_SYSTEMMEMORY))
5170 expect_hr = DDERR_INVALIDPARAMS;
5171 else if (formats[i].create_size_checked && !block_aligned)
5173 expect_hr = DDERR_INVALIDPARAMS;
5174 if (!(types[j].caps & DDSCAPS_TEXTURE))
5175 todo = TRUE;
5177 else
5178 expect_hr = D3D_OK;
5180 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5181 if (todo)
5182 todo_wine ok(hr == expect_hr,
5183 "Got unexpected hr %#x for format %s, resource type %s, size %ux%u, expected %#x.\n",
5184 hr, formats[i].name, types[j].name, w, h, expect_hr);
5185 else
5186 ok(hr == expect_hr,
5187 "Got unexpected hr %#x for format %s, resource type %s, size %ux%u, expected %#x.\n",
5188 hr, formats[i].name, types[j].name, w, h, expect_hr);
5190 if (SUCCEEDED(hr))
5191 IDirectDrawSurface4_Release(surface);
5196 if (formats[i].overlay)
5197 continue;
5199 for (j = 0; j < sizeof(user_mem_tests) / sizeof(*user_mem_tests); ++j)
5201 memset(&ddsd, 0, sizeof(ddsd));
5202 ddsd.dwSize = sizeof(ddsd);
5203 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | user_mem_tests[j].flags;
5204 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE;
5206 switch (user_mem_tests[j].size_type)
5208 case SIZE_TYPE_ZERO:
5209 U1(ddsd).dwLinearSize = 0;
5210 break;
5212 case SIZE_TYPE_PITCH:
5213 U1(ddsd).dwLinearSize = 2 * formats[i].block_size;
5214 break;
5216 case SIZE_TYPE_SIZE:
5217 U1(ddsd).dwLinearSize = 2 * 2 * formats[i].block_size;
5218 break;
5220 U1(ddsd).dwLinearSize += user_mem_tests[j].rel_size;
5222 ddsd.lpSurface = mem;
5223 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5224 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
5225 U4(ddsd).ddpfPixelFormat.dwFourCC = formats[i].fourcc;
5226 ddsd.dwWidth = 8;
5227 ddsd.dwHeight = 8;
5229 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5230 ok(hr == user_mem_tests[j].hr, "Test %u: Got unexpected hr %#x, format %s.\n", j, hr, formats[i].name);
5232 if (FAILED(hr))
5233 continue;
5235 memset(&ddsd, 0, sizeof(ddsd));
5236 ddsd.dwSize = sizeof(ddsd);
5237 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
5238 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", j, hr);
5239 ok(ddsd.dwFlags == (DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_LINEARSIZE),
5240 "Test %u: Got unexpected flags %#x.\n", j, ddsd.dwFlags);
5241 if (user_mem_tests[j].flags & DDSD_LPSURFACE)
5242 ok(U1(ddsd).dwLinearSize == ~0u, "Test %u: Got unexpected linear size %#x.\n",
5243 j, U1(ddsd).dwLinearSize);
5244 else
5245 ok(U1(ddsd).dwLinearSize == 2 * 2 * formats[i].block_size,
5246 "Test %u: Got unexpected linear size %#x, expected %#x.\n",
5247 j, U1(ddsd).dwLinearSize, 2 * 2 * formats[i].block_size);
5248 IDirectDrawSurface4_Release(surface);
5252 HeapFree(GetProcessHeap(), 0, mem);
5253 cleanup:
5254 IDirectDraw4_Release(ddraw);
5255 IDirect3DDevice3_Release(device);
5256 DestroyWindow(window);
5259 struct format_support_check
5261 const DDPIXELFORMAT *format;
5262 BOOL supported;
5265 static HRESULT WINAPI test_unsupported_formats_cb(DDPIXELFORMAT *fmt, void *ctx)
5267 struct format_support_check *format = ctx;
5269 if (!memcmp(format->format, fmt, sizeof(*fmt)))
5271 format->supported = TRUE;
5272 return DDENUMRET_CANCEL;
5275 return DDENUMRET_OK;
5278 static void test_unsupported_formats(void)
5280 HRESULT hr;
5281 BOOL expect_success;
5282 HWND window;
5283 IDirectDraw4 *ddraw;
5284 IDirect3D3 *d3d;
5285 IDirect3DDevice3 *device;
5286 IDirectDrawSurface4 *surface;
5287 DDSURFACEDESC2 ddsd;
5288 unsigned int i, j;
5289 DWORD expected_caps;
5290 static const struct
5292 const char *name;
5293 DDPIXELFORMAT fmt;
5295 formats[] =
5298 "D3DFMT_A8R8G8B8",
5300 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
5301 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
5305 "D3DFMT_P8",
5307 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
5308 {8 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
5312 static const DWORD caps[] = {0, DDSCAPS_SYSTEMMEMORY, DDSCAPS_VIDEOMEMORY};
5314 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5315 0, 0, 640, 480, 0, 0, 0, 0);
5317 if (!(device = create_device(window, DDSCL_NORMAL)))
5319 skip("Failed to create a 3D device, skipping test.\n");
5320 DestroyWindow(window);
5321 return;
5324 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
5325 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
5326 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **) &ddraw);
5327 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
5328 IDirect3D3_Release(d3d);
5330 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
5332 struct format_support_check check = {&formats[i].fmt, FALSE};
5333 hr = IDirect3DDevice3_EnumTextureFormats(device, test_unsupported_formats_cb, &check);
5334 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
5336 for (j = 0; j < sizeof(caps) / sizeof(*caps); j++)
5338 memset(&ddsd, 0, sizeof(ddsd));
5339 ddsd.dwSize = sizeof(ddsd);
5340 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5341 U4(ddsd).ddpfPixelFormat = formats[i].fmt;
5342 ddsd.dwWidth = 4;
5343 ddsd.dwHeight = 4;
5344 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | caps[j];
5346 if (caps[j] & DDSCAPS_VIDEOMEMORY && !check.supported)
5347 expect_success = FALSE;
5348 else
5349 expect_success = TRUE;
5351 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5352 ok(SUCCEEDED(hr) == expect_success,
5353 "Got unexpected hr %#x for format %s, caps %#x, expected %s.\n",
5354 hr, formats[i].name, caps[j], expect_success ? "success" : "failure");
5355 if (FAILED(hr))
5356 continue;
5358 memset(&ddsd, 0, sizeof(ddsd));
5359 ddsd.dwSize = sizeof(ddsd);
5360 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
5361 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5363 if (caps[j] & DDSCAPS_VIDEOMEMORY)
5364 expected_caps = DDSCAPS_VIDEOMEMORY;
5365 else if (caps[j] & DDSCAPS_SYSTEMMEMORY)
5366 expected_caps = DDSCAPS_SYSTEMMEMORY;
5367 else if (check.supported)
5368 expected_caps = DDSCAPS_VIDEOMEMORY;
5369 else
5370 expected_caps = DDSCAPS_SYSTEMMEMORY;
5372 ok(ddsd.ddsCaps.dwCaps & expected_caps,
5373 "Expected capability %#x, format %s, input cap %#x.\n",
5374 expected_caps, formats[i].name, caps[j]);
5376 IDirectDrawSurface4_Release(surface);
5380 IDirectDraw4_Release(ddraw);
5381 IDirect3DDevice3_Release(device);
5382 DestroyWindow(window);
5385 static void test_rt_caps(void)
5387 PALETTEENTRY palette_entries[256];
5388 IDirectDrawPalette *palette;
5389 IDirectDraw4 *ddraw;
5390 DDPIXELFORMAT z_fmt;
5391 IDirect3D3 *d3d;
5392 unsigned int i;
5393 ULONG refcount;
5394 HWND window;
5395 HRESULT hr;
5397 static const DDPIXELFORMAT p8_fmt =
5399 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
5400 {8}, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000},
5403 const struct
5405 const DDPIXELFORMAT *pf;
5406 DWORD caps_in;
5407 DWORD caps_out;
5408 HRESULT create_device_hr;
5409 HRESULT set_rt_hr, alternative_set_rt_hr;
5411 test_data[] =
5414 NULL,
5415 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
5416 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5417 D3D_OK,
5418 D3D_OK,
5419 D3D_OK,
5422 NULL,
5423 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
5424 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5425 D3D_OK,
5426 D3D_OK,
5427 D3D_OK,
5430 NULL,
5431 DDSCAPS_OFFSCREENPLAIN,
5432 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5433 DDERR_INVALIDCAPS,
5434 DDERR_INVALIDCAPS,
5435 DDERR_INVALIDCAPS,
5438 NULL,
5439 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5440 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5441 D3DERR_SURFACENOTINVIDMEM,
5442 D3D_OK,
5443 D3D_OK,
5446 NULL,
5447 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
5448 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
5449 DDERR_INVALIDCAPS,
5450 DDERR_INVALIDCAPS,
5451 DDERR_INVALIDCAPS,
5454 NULL,
5455 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
5456 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5457 D3D_OK,
5458 D3D_OK,
5459 D3D_OK,
5462 NULL,
5463 DDSCAPS_3DDEVICE,
5464 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5465 D3D_OK,
5466 D3D_OK,
5467 D3D_OK,
5470 NULL,
5472 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5473 DDERR_INVALIDCAPS,
5474 DDERR_INVALIDCAPS,
5475 DDERR_INVALIDCAPS,
5478 NULL,
5479 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5480 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5481 D3DERR_SURFACENOTINVIDMEM,
5482 D3D_OK,
5483 D3D_OK,
5486 NULL,
5487 DDSCAPS_SYSTEMMEMORY,
5488 DDSCAPS_SYSTEMMEMORY,
5489 DDERR_INVALIDCAPS,
5490 DDERR_INVALIDCAPS,
5491 DDERR_INVALIDCAPS,
5494 &p8_fmt,
5496 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5497 DDERR_INVALIDCAPS,
5498 DDERR_INVALIDCAPS,
5499 DDERR_INVALIDCAPS,
5502 &p8_fmt,
5503 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
5504 ~0U /* AMD r200 */,
5505 DDERR_NOPALETTEATTACHED,
5506 DDERR_INVALIDCAPS,
5507 DDERR_INVALIDCAPS,
5510 &p8_fmt,
5511 DDSCAPS_OFFSCREENPLAIN,
5512 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5513 DDERR_INVALIDCAPS,
5514 DDERR_INVALIDCAPS,
5515 DDERR_INVALIDCAPS,
5518 &p8_fmt,
5519 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5520 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5521 DDERR_NOPALETTEATTACHED,
5522 DDERR_INVALIDCAPS,
5523 DDERR_INVALIDCAPS,
5526 &p8_fmt,
5527 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
5528 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
5529 DDERR_INVALIDCAPS,
5530 DDERR_INVALIDCAPS,
5531 DDERR_INVALIDCAPS,
5534 &z_fmt,
5535 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER,
5536 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
5537 DDERR_INVALIDCAPS,
5538 DDERR_INVALIDPIXELFORMAT,
5539 D3D_OK /* r200 */,
5542 &z_fmt,
5543 DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
5544 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
5545 DDERR_INVALIDCAPS,
5546 DDERR_INVALIDPIXELFORMAT,
5547 D3D_OK /* r200 */,
5550 &z_fmt,
5551 DDSCAPS_ZBUFFER,
5552 DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
5553 DDERR_INVALIDCAPS,
5554 DDERR_INVALIDCAPS,
5555 DDERR_INVALIDCAPS,
5558 &z_fmt,
5559 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
5560 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
5561 DDERR_INVALIDCAPS,
5562 DDERR_INVALIDPIXELFORMAT,
5563 D3D_OK /* r200 */,
5566 &z_fmt,
5567 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
5568 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
5569 DDERR_INVALIDCAPS,
5570 DDERR_INVALIDCAPS,
5571 DDERR_INVALIDCAPS,
5575 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5576 0, 0, 640, 480, 0, 0, 0, 0);
5577 ddraw = create_ddraw();
5578 ok(!!ddraw, "Failed to create a ddraw object.\n");
5579 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5580 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5582 if (FAILED(IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d)))
5584 skip("D3D interface is not available, skipping test.\n");
5585 goto done;
5588 memset(&z_fmt, 0, sizeof(z_fmt));
5589 hr = IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
5590 if (FAILED(hr) || !z_fmt.dwSize)
5592 skip("No depth buffer formats available, skipping test.\n");
5593 IDirect3D3_Release(d3d);
5594 goto done;
5597 memset(palette_entries, 0, sizeof(palette_entries));
5598 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
5599 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5601 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
5603 IDirectDrawSurface4 *surface, *rt, *expected_rt, *tmp;
5604 DDSURFACEDESC2 surface_desc;
5605 IDirect3DDevice3 *device;
5607 memset(&surface_desc, 0, sizeof(surface_desc));
5608 surface_desc.dwSize = sizeof(surface_desc);
5609 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5610 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
5611 if (test_data[i].pf)
5613 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
5614 U4(surface_desc).ddpfPixelFormat = *test_data[i].pf;
5616 surface_desc.dwWidth = 640;
5617 surface_desc.dwHeight = 480;
5618 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5619 ok(SUCCEEDED(hr), "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
5620 i, test_data[i].caps_in, hr);
5622 memset(&surface_desc, 0, sizeof(surface_desc));
5623 surface_desc.dwSize = sizeof(surface_desc);
5624 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
5625 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
5626 ok(test_data[i].caps_out == ~0U || surface_desc.ddsCaps.dwCaps == test_data[i].caps_out,
5627 "Test %u: Got unexpected caps %#x, expected %#x.\n",
5628 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
5630 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device, NULL);
5631 ok(hr == test_data[i].create_device_hr, "Test %u: Got unexpected hr %#x, expected %#x.\n",
5632 i, hr, test_data[i].create_device_hr);
5633 if (FAILED(hr))
5635 if (hr == DDERR_NOPALETTEATTACHED)
5637 hr = IDirectDrawSurface4_SetPalette(surface, palette);
5638 ok(SUCCEEDED(hr), "Test %u: Failed to set palette, hr %#x.\n", i, hr);
5639 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device, NULL);
5640 if (surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5641 ok(hr == DDERR_INVALIDPIXELFORMAT, "Test %u: Got unexpected hr %#x.\n", i, hr);
5642 else
5643 ok(hr == D3DERR_SURFACENOTINVIDMEM, "Test %u: Got unexpected hr %#x.\n", i, hr);
5645 IDirectDrawSurface4_Release(surface);
5647 memset(&surface_desc, 0, sizeof(surface_desc));
5648 surface_desc.dwSize = sizeof(surface_desc);
5649 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5650 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
5651 surface_desc.dwWidth = 640;
5652 surface_desc.dwHeight = 480;
5653 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5654 ok(SUCCEEDED(hr), "Test %u: Failed to create surface, hr %#x.\n", i, hr);
5656 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device, NULL);
5657 ok(SUCCEEDED(hr), "Test %u: Failed to create device, hr %#x.\n", i, hr);
5660 memset(&surface_desc, 0, sizeof(surface_desc));
5661 surface_desc.dwSize = sizeof(surface_desc);
5662 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5663 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
5664 if (test_data[i].pf)
5666 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
5667 U4(surface_desc).ddpfPixelFormat = *test_data[i].pf;
5669 surface_desc.dwWidth = 640;
5670 surface_desc.dwHeight = 480;
5671 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &rt, NULL);
5672 ok(SUCCEEDED(hr), "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
5673 i, test_data[i].caps_in, hr);
5675 hr = IDirect3DDevice3_SetRenderTarget(device, rt, 0);
5676 ok(hr == test_data[i].set_rt_hr || broken(hr == test_data[i].alternative_set_rt_hr),
5677 "Test %u: Got unexpected hr %#x, expected %#x.\n",
5678 i, hr, test_data[i].set_rt_hr);
5679 if (SUCCEEDED(hr) || hr == DDERR_INVALIDPIXELFORMAT)
5680 expected_rt = rt;
5681 else
5682 expected_rt = surface;
5684 hr = IDirect3DDevice3_GetRenderTarget(device, &tmp);
5685 ok(SUCCEEDED(hr), "Test %u: Failed to get render target, hr %#x.\n", i, hr);
5686 ok(tmp == expected_rt, "Test %u: Got unexpected rt %p.\n", i, tmp);
5688 IDirectDrawSurface4_Release(tmp);
5689 IDirectDrawSurface4_Release(rt);
5690 refcount = IDirect3DDevice3_Release(device);
5691 ok(refcount == 0, "Test %u: The device was not properly freed, refcount %u.\n", i, refcount);
5692 refcount = IDirectDrawSurface4_Release(surface);
5693 ok(refcount == 0, "Test %u: The surface was not properly freed, refcount %u.\n", i, refcount);
5696 IDirectDrawPalette_Release(palette);
5697 IDirect3D3_Release(d3d);
5699 done:
5700 refcount = IDirectDraw4_Release(ddraw);
5701 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
5702 DestroyWindow(window);
5705 static void test_primary_caps(void)
5707 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
5708 IDirectDrawSurface4 *surface;
5709 DDSURFACEDESC2 surface_desc;
5710 IDirectDraw4 *ddraw;
5711 unsigned int i;
5712 ULONG refcount;
5713 HWND window;
5714 HRESULT hr;
5716 static const struct
5718 DWORD coop_level;
5719 DWORD caps_in;
5720 DWORD back_buffer_count;
5721 HRESULT hr;
5722 DWORD caps_out;
5724 test_data[] =
5727 DDSCL_NORMAL,
5728 DDSCAPS_PRIMARYSURFACE,
5729 ~0u,
5730 DD_OK,
5731 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE,
5734 DDSCL_NORMAL,
5735 DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE,
5736 ~0u,
5737 DDERR_INVALIDCAPS,
5738 ~0u,
5741 DDSCL_NORMAL,
5742 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER,
5743 ~0u,
5744 DDERR_INVALIDCAPS,
5745 ~0u,
5748 DDSCL_NORMAL,
5749 DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER,
5750 ~0u,
5751 DDERR_INVALIDCAPS,
5752 ~0u,
5755 DDSCL_NORMAL,
5756 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP,
5757 ~0u,
5758 DDERR_INVALIDCAPS,
5759 ~0u,
5762 DDSCL_NORMAL,
5763 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX,
5764 ~0u,
5765 DDERR_INVALIDCAPS,
5766 ~0u,
5769 DDSCL_NORMAL,
5770 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5771 ~0u,
5772 DDERR_INVALIDCAPS,
5773 ~0u,
5776 DDSCL_NORMAL,
5777 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5779 DDERR_INVALIDCAPS,
5780 ~0u,
5783 DDSCL_NORMAL,
5784 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5786 DDERR_NOEXCLUSIVEMODE,
5787 ~0u,
5790 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5791 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5793 DDERR_INVALIDCAPS,
5794 ~0u,
5797 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5798 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5800 DD_OK,
5801 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX,
5804 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5805 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER,
5807 DDERR_INVALIDCAPS,
5808 ~0u,
5811 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5812 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_BACKBUFFER,
5814 DDERR_INVALIDCAPS,
5815 ~0u,
5819 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5820 0, 0, 640, 480, 0, 0, 0, 0);
5821 ddraw = create_ddraw();
5822 ok(!!ddraw, "Failed to create a ddraw object.\n");
5824 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
5826 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, test_data[i].coop_level);
5827 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5829 memset(&surface_desc, 0, sizeof(surface_desc));
5830 surface_desc.dwSize = sizeof(surface_desc);
5831 surface_desc.dwFlags = DDSD_CAPS;
5832 if (test_data[i].back_buffer_count != ~0u)
5833 surface_desc.dwFlags |= DDSD_BACKBUFFERCOUNT;
5834 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
5835 U5(surface_desc).dwBackBufferCount = test_data[i].back_buffer_count;
5836 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5837 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
5838 if (FAILED(hr))
5839 continue;
5841 memset(&surface_desc, 0, sizeof(surface_desc));
5842 surface_desc.dwSize = sizeof(surface_desc);
5843 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
5844 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
5845 ok((surface_desc.ddsCaps.dwCaps & ~placement) == test_data[i].caps_out,
5846 "Test %u: Got unexpected caps %#x, expected %#x.\n",
5847 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
5849 IDirectDrawSurface4_Release(surface);
5852 refcount = IDirectDraw4_Release(ddraw);
5853 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
5854 DestroyWindow(window);
5857 static void test_surface_lock(void)
5859 IDirectDraw4 *ddraw;
5860 IDirect3D3 *d3d = NULL;
5861 IDirectDrawSurface4 *surface;
5862 HRESULT hr;
5863 HWND window;
5864 unsigned int i;
5865 DDSURFACEDESC2 ddsd;
5866 ULONG refcount;
5867 DDPIXELFORMAT z_fmt;
5868 static const struct
5870 DWORD caps;
5871 DWORD caps2;
5872 const char *name;
5874 tests[] =
5877 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
5879 "videomemory offscreenplain"
5882 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
5884 "systemmemory offscreenplain"
5887 DDSCAPS_PRIMARYSURFACE,
5889 "primary"
5892 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
5894 "videomemory texture"
5897 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
5898 DDSCAPS2_OPAQUE,
5899 "opaque videomemory texture"
5902 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
5904 "systemmemory texture"
5907 DDSCAPS_TEXTURE,
5908 DDSCAPS2_TEXTUREMANAGE,
5909 "managed texture"
5912 DDSCAPS_TEXTURE,
5913 DDSCAPS2_D3DTEXTUREMANAGE,
5914 "managed texture"
5917 DDSCAPS_TEXTURE,
5918 DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_OPAQUE,
5919 "opaque managed texture"
5922 DDSCAPS_TEXTURE,
5923 DDSCAPS2_D3DTEXTUREMANAGE | DDSCAPS2_OPAQUE,
5924 "opaque managed texture"
5927 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
5929 "render target"
5932 DDSCAPS_ZBUFFER,
5934 "Z buffer"
5938 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5939 0, 0, 640, 480, 0, 0, 0, 0);
5940 ddraw = create_ddraw();
5941 ok(!!ddraw, "Failed to create a ddraw object.\n");
5942 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5943 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5945 if (FAILED(IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d)))
5947 skip("D3D interface is not available, skipping test.\n");
5948 goto done;
5951 memset(&z_fmt, 0, sizeof(z_fmt));
5952 hr = IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
5953 if (FAILED(hr) || !z_fmt.dwSize)
5955 skip("No depth buffer formats available, skipping test.\n");
5956 goto done;
5959 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
5961 memset(&ddsd, 0, sizeof(ddsd));
5962 ddsd.dwSize = sizeof(ddsd);
5963 ddsd.dwFlags = DDSD_CAPS;
5964 if (!(tests[i].caps & DDSCAPS_PRIMARYSURFACE))
5966 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
5967 ddsd.dwWidth = 64;
5968 ddsd.dwHeight = 64;
5970 if (tests[i].caps & DDSCAPS_ZBUFFER)
5972 ddsd.dwFlags |= DDSD_PIXELFORMAT;
5973 U4(ddsd).ddpfPixelFormat = z_fmt;
5975 ddsd.ddsCaps.dwCaps = tests[i].caps;
5976 ddsd.ddsCaps.dwCaps2 = tests[i].caps2;
5978 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5979 ok(SUCCEEDED(hr), "Failed to create surface, type %s, hr %#x.\n", tests[i].name, hr);
5981 memset(&ddsd, 0, sizeof(ddsd));
5982 ddsd.dwSize = sizeof(ddsd);
5983 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, DDLOCK_WAIT, NULL);
5984 ok(SUCCEEDED(hr), "Failed to lock surface, type %s, hr %#x.\n", tests[i].name, hr);
5985 if (SUCCEEDED(hr))
5987 hr = IDirectDrawSurface4_Unlock(surface, NULL);
5988 ok(SUCCEEDED(hr), "Failed to unlock surface, type %s, hr %#x.\n", tests[i].name, hr);
5991 IDirectDrawSurface4_Release(surface);
5994 done:
5995 if (d3d)
5996 IDirect3D3_Release(d3d);
5997 refcount = IDirectDraw4_Release(ddraw);
5998 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
5999 DestroyWindow(window);
6002 static void test_surface_discard(void)
6004 IDirect3DDevice3 *device;
6005 IDirect3D3 *d3d;
6006 IDirectDraw4 *ddraw;
6007 HRESULT hr;
6008 HWND window;
6009 DDSURFACEDESC2 ddsd;
6010 IDirectDrawSurface4 *surface, *target;
6011 void *addr;
6012 static const struct
6014 DWORD caps, caps2;
6015 BOOL discard;
6017 tests[] =
6019 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0, TRUE},
6020 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, 0, FALSE},
6021 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, 0, TRUE},
6022 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0, FALSE},
6023 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, FALSE},
6024 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_HINTDYNAMIC, FALSE},
6025 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, FALSE},
6026 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE | DDSCAPS2_HINTDYNAMIC, FALSE},
6028 unsigned int i;
6030 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6031 0, 0, 640, 480, 0, 0, 0, 0);
6033 if (!(device = create_device(window, DDSCL_NORMAL)))
6035 skip("Failed to create a 3D device, skipping test.\n");
6036 DestroyWindow(window);
6037 return;
6039 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
6040 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
6041 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
6042 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
6043 hr = IDirect3DDevice3_GetRenderTarget(device, &target);
6044 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
6046 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
6048 BOOL discarded;
6050 memset(&ddsd, 0, sizeof(ddsd));
6051 ddsd.dwSize = sizeof(ddsd);
6052 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6053 ddsd.ddsCaps.dwCaps = tests[i].caps;
6054 ddsd.ddsCaps.dwCaps2 = tests[i].caps2;
6055 ddsd.dwWidth = 64;
6056 ddsd.dwHeight = 64;
6057 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6058 ok(SUCCEEDED(hr), "Failed to create offscreen surface, hr %#x, case %u.\n", hr, i);
6060 memset(&ddsd, 0, sizeof(ddsd));
6061 ddsd.dwSize = sizeof(ddsd);
6062 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, 0, NULL);
6063 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
6064 addr = ddsd.lpSurface;
6065 hr = IDirectDrawSurface4_Unlock(surface, NULL);
6066 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
6068 memset(&ddsd, 0, sizeof(ddsd));
6069 ddsd.dwSize = sizeof(ddsd);
6070 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS, NULL);
6071 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
6072 discarded = ddsd.lpSurface != addr;
6073 hr = IDirectDrawSurface4_Unlock(surface, NULL);
6074 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
6076 hr = IDirectDrawSurface4_Blt(target, NULL, surface, NULL, DDBLT_WAIT, NULL);
6077 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
6079 memset(&ddsd, 0, sizeof(ddsd));
6080 ddsd.dwSize = sizeof(ddsd);
6081 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS, NULL);
6082 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
6083 discarded |= ddsd.lpSurface != addr;
6084 hr = IDirectDrawSurface4_Unlock(surface, NULL);
6085 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
6087 IDirectDrawSurface4_Release(surface);
6089 /* Windows 7 reliably changes the address of surfaces that are discardable (Nvidia Kepler,
6090 * AMD r500, evergreen). Windows XP, at least on AMD r200, does not. */
6091 ok(!discarded || tests[i].discard, "Expected surface not to be discarded, case %u\n", i);
6094 IDirectDrawSurface4_Release(target);
6095 IDirectDraw4_Release(ddraw);
6096 IDirect3D3_Release(d3d);
6097 IDirect3DDevice3_Release(device);
6098 DestroyWindow(window);
6101 static void test_flip(void)
6103 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
6104 IDirectDrawSurface4 *primary, *backbuffer1, *backbuffer2, *backbuffer3, *surface;
6105 DDSCAPS2 caps = {DDSCAPS_FLIP, 0, 0, {0}};
6106 DDSURFACEDESC2 surface_desc;
6107 BOOL sysmem_primary;
6108 IDirectDraw4 *ddraw;
6109 D3DCOLOR color;
6110 ULONG refcount;
6111 HWND window;
6112 DDBLTFX fx;
6113 HRESULT hr;
6115 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6116 0, 0, 640, 480, 0, 0, 0, 0);
6117 ddraw = create_ddraw();
6118 ok(!!ddraw, "Failed to create a ddraw object.\n");
6120 hr = set_display_mode(ddraw, 640, 480);
6121 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
6122 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6123 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6125 memset(&surface_desc, 0, sizeof(surface_desc));
6126 surface_desc.dwSize = sizeof(surface_desc);
6127 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
6128 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
6129 U5(surface_desc).dwBackBufferCount = 3;
6130 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &primary, NULL);
6131 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6133 memset(&surface_desc, 0, sizeof(surface_desc));
6134 surface_desc.dwSize = sizeof(surface_desc);
6135 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &surface_desc);
6136 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6137 ok((surface_desc.ddsCaps.dwCaps & ~placement)
6138 == (DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX),
6139 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
6140 sysmem_primary = surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY;
6142 hr = IDirectDrawSurface4_GetAttachedSurface(primary, &caps, &backbuffer1);
6143 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6144 memset(&surface_desc, 0, sizeof(surface_desc));
6145 surface_desc.dwSize = sizeof(surface_desc);
6146 hr = IDirectDrawSurface4_GetSurfaceDesc(backbuffer1, &surface_desc);
6147 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6148 ok(!U5(surface_desc).dwBackBufferCount, "Got unexpected back buffer count %u.\n", U5(surface_desc).dwBackBufferCount);
6149 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_BACKBUFFER),
6150 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
6152 hr = IDirectDrawSurface4_GetAttachedSurface(backbuffer1, &caps, &backbuffer2);
6153 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6154 memset(&surface_desc, 0, sizeof(surface_desc));
6155 surface_desc.dwSize = sizeof(surface_desc);
6156 hr = IDirectDrawSurface4_GetSurfaceDesc(backbuffer2, &surface_desc);
6157 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6158 ok(!U5(surface_desc).dwBackBufferCount, "Got unexpected back buffer count %u.\n", U5(surface_desc).dwBackBufferCount);
6159 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX),
6160 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
6162 hr = IDirectDrawSurface4_GetAttachedSurface(backbuffer2, &caps, &backbuffer3);
6163 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6164 memset(&surface_desc, 0, sizeof(surface_desc));
6165 surface_desc.dwSize = sizeof(surface_desc);
6166 hr = IDirectDrawSurface4_GetSurfaceDesc(backbuffer3, &surface_desc);
6167 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6168 ok(!U5(surface_desc).dwBackBufferCount, "Got unexpected back buffer count %u.\n", U5(surface_desc).dwBackBufferCount);
6169 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX),
6170 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
6172 hr = IDirectDrawSurface4_GetAttachedSurface(backbuffer3, &caps, &surface);
6173 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6174 ok(surface == primary, "Got unexpected surface %p, expected %p.\n", surface, primary);
6175 IDirectDrawSurface4_Release(surface);
6177 memset(&surface_desc, 0, sizeof(surface_desc));
6178 surface_desc.dwSize = sizeof(surface_desc);
6179 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6180 surface_desc.ddsCaps.dwCaps = 0;
6181 surface_desc.dwWidth = 640;
6182 surface_desc.dwHeight = 480;
6183 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6184 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6185 hr = IDirectDrawSurface4_Flip(primary, surface, DDFLIP_WAIT);
6186 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
6187 IDirectDrawSurface4_Release(surface);
6189 hr = IDirectDrawSurface4_Flip(primary, primary, DDFLIP_WAIT);
6190 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
6191 hr = IDirectDrawSurface4_Flip(backbuffer1, NULL, DDFLIP_WAIT);
6192 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
6193 hr = IDirectDrawSurface4_Flip(backbuffer2, NULL, DDFLIP_WAIT);
6194 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
6195 hr = IDirectDrawSurface4_Flip(backbuffer3, NULL, DDFLIP_WAIT);
6196 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
6198 memset(&fx, 0, sizeof(fx));
6199 fx.dwSize = sizeof(fx);
6200 U5(fx).dwFillColor = 0xffff0000;
6201 hr = IDirectDrawSurface4_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6202 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
6203 U5(fx).dwFillColor = 0xff00ff00;
6204 hr = IDirectDrawSurface4_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6205 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
6206 U5(fx).dwFillColor = 0xff0000ff;
6207 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6208 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
6210 hr = IDirectDrawSurface4_Flip(primary, NULL, DDFLIP_WAIT);
6211 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
6212 color = get_surface_color(backbuffer1, 320, 240);
6213 /* The testbot seems to just copy the contents of one surface to all the
6214 * others, instead of properly flipping. */
6215 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
6216 "Got unexpected color 0x%08x.\n", color);
6217 color = get_surface_color(backbuffer2, 320, 240);
6218 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
6219 U5(fx).dwFillColor = 0xffff0000;
6220 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6221 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
6223 hr = IDirectDrawSurface4_Flip(primary, NULL, DDFLIP_WAIT);
6224 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
6225 color = get_surface_color(backbuffer1, 320, 240);
6226 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
6227 "Got unexpected color 0x%08x.\n", color);
6228 color = get_surface_color(backbuffer2, 320, 240);
6229 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
6230 U5(fx).dwFillColor = 0xff00ff00;
6231 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6232 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
6234 hr = IDirectDrawSurface4_Flip(primary, NULL, DDFLIP_WAIT);
6235 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
6236 color = get_surface_color(backbuffer1, 320, 240);
6237 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
6238 "Got unexpected color 0x%08x.\n", color);
6239 color = get_surface_color(backbuffer2, 320, 240);
6240 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
6241 U5(fx).dwFillColor = 0xff0000ff;
6242 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6243 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
6245 hr = IDirectDrawSurface4_Flip(primary, backbuffer1, DDFLIP_WAIT);
6246 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
6247 color = get_surface_color(backbuffer2, 320, 240);
6248 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
6249 "Got unexpected color 0x%08x.\n", color);
6250 color = get_surface_color(backbuffer3, 320, 240);
6251 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
6252 U5(fx).dwFillColor = 0xffff0000;
6253 hr = IDirectDrawSurface4_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6254 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
6256 hr = IDirectDrawSurface4_Flip(primary, backbuffer2, DDFLIP_WAIT);
6257 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
6258 color = get_surface_color(backbuffer1, 320, 240);
6259 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
6260 color = get_surface_color(backbuffer3, 320, 240);
6261 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
6262 "Got unexpected color 0x%08x.\n", color);
6263 U5(fx).dwFillColor = 0xff00ff00;
6264 hr = IDirectDrawSurface4_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6265 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
6267 hr = IDirectDrawSurface4_Flip(primary, backbuffer3, DDFLIP_WAIT);
6268 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
6269 color = get_surface_color(backbuffer1, 320, 240);
6270 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
6271 "Got unexpected color 0x%08x.\n", color);
6272 color = get_surface_color(backbuffer2, 320, 240);
6273 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
6275 IDirectDrawSurface4_Release(backbuffer3);
6276 IDirectDrawSurface4_Release(backbuffer2);
6277 IDirectDrawSurface4_Release(backbuffer1);
6278 IDirectDrawSurface4_Release(primary);
6279 refcount = IDirectDraw4_Release(ddraw);
6280 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
6281 DestroyWindow(window);
6284 static void reset_ddsd(DDSURFACEDESC2 *ddsd)
6286 memset(ddsd, 0, sizeof(*ddsd));
6287 ddsd->dwSize = sizeof(*ddsd);
6290 static void test_set_surface_desc(void)
6292 IDirectDraw4 *ddraw;
6293 HWND window;
6294 HRESULT hr;
6295 DDSURFACEDESC2 ddsd;
6296 IDirectDrawSurface4 *surface;
6297 BYTE data[16*16*4];
6298 ULONG ref;
6299 unsigned int i;
6300 static const struct
6302 DWORD caps, caps2;
6303 BOOL supported;
6304 const char *name;
6306 invalid_caps_tests[] =
6308 {DDSCAPS_VIDEOMEMORY, 0, FALSE, "videomemory plain"},
6309 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0, TRUE, "systemmemory texture"},
6310 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, FALSE, "managed texture"},
6311 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, FALSE, "managed texture"},
6312 {DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY, 0, FALSE, "systemmemory primary"},
6315 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6316 0, 0, 640, 480, 0, 0, 0, 0);
6317 ddraw = create_ddraw();
6318 ok(!!ddraw, "Failed to create a ddraw object.\n");
6319 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6320 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6322 reset_ddsd(&ddsd);
6323 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
6324 ddsd.dwWidth = 8;
6325 ddsd.dwHeight = 8;
6326 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6327 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6328 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6329 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6330 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6331 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6332 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
6334 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6335 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6337 reset_ddsd(&ddsd);
6338 ddsd.dwFlags = DDSD_LPSURFACE;
6339 ddsd.lpSurface = data;
6340 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6341 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6343 /* Redundantly setting the same lpSurface is not an error. */
6344 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6345 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6346 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
6347 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6348 ok(!(ddsd.dwFlags & DDSD_LPSURFACE), "DDSD_LPSURFACE is set.\n");
6349 ok(ddsd.lpSurface == NULL, "lpSurface is %p, expected NULL.\n", ddsd.lpSurface);
6351 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, 0, NULL);
6352 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
6353 ok(!(ddsd.dwFlags & DDSD_LPSURFACE), "DDSD_LPSURFACE is set.\n");
6354 ok(ddsd.lpSurface == data, "lpSurface is %p, expected %p.\n", data, data);
6355 hr = IDirectDrawSurface4_Unlock(surface, NULL);
6356 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
6358 reset_ddsd(&ddsd);
6359 ddsd.dwFlags = DDSD_LPSURFACE;
6360 ddsd.lpSurface = data;
6361 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 1);
6362 ok(hr == DDERR_INVALIDPARAMS, "SetSurfaceDesc with flags=1 returned %#x.\n", hr);
6364 ddsd.lpSurface = NULL;
6365 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6366 ok(hr == DDERR_INVALIDPARAMS, "Setting lpSurface=NULL returned %#x.\n", hr);
6368 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, NULL, 0);
6369 ok(hr == DDERR_INVALIDPARAMS, "SetSurfaceDesc with NULL desc returned %#x.\n", hr);
6371 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
6372 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6373 ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN),
6374 "Got unexpected caps %#x.\n", ddsd.ddsCaps.dwCaps);
6375 ok(ddsd.ddsCaps.dwCaps2 == 0, "Got unexpected caps2 %#x.\n", 0);
6377 /* Setting the caps is an error. This also means the original description cannot be reapplied. */
6378 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6379 ok(hr == DDERR_INVALIDPARAMS, "Setting the original desc returned %#x.\n", hr);
6381 ddsd.dwFlags = DDSD_CAPS;
6382 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6383 ok(hr == DDERR_INVALIDPARAMS, "Setting DDSD_CAPS returned %#x.\n", hr);
6385 /* dwCaps = 0 is allowed, but ignored. Caps2 can be anything and is ignored too. */
6386 ddsd.dwFlags = DDSD_CAPS | DDSD_LPSURFACE;
6387 ddsd.lpSurface = data;
6388 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6389 ok(hr == DDERR_INVALIDCAPS, "Setting DDSD_CAPS returned %#x.\n", hr);
6390 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
6391 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6392 ok(hr == DDERR_INVALIDCAPS, "Setting DDSD_CAPS returned %#x.\n", hr);
6393 ddsd.ddsCaps.dwCaps = 0;
6394 ddsd.ddsCaps.dwCaps2 = 0xdeadbeef;
6395 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6396 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6398 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
6399 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6400 ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN),
6401 "Got unexpected caps %#x.\n", ddsd.ddsCaps.dwCaps);
6402 ok(ddsd.ddsCaps.dwCaps2 == 0, "Got unexpected caps2 %#x.\n", 0);
6404 /* Setting the height is allowed, but it cannot be set to 0, and only if LPSURFACE is set too. */
6405 reset_ddsd(&ddsd);
6406 ddsd.dwFlags = DDSD_HEIGHT;
6407 ddsd.dwHeight = 16;
6408 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6409 ok(hr == DDERR_INVALIDPARAMS, "Setting height without lpSurface returned %#x.\n", hr);
6411 ddsd.lpSurface = data;
6412 ddsd.dwFlags = DDSD_HEIGHT | DDSD_LPSURFACE;
6413 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6414 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6416 ddsd.dwHeight = 0;
6417 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6418 ok(hr == DDERR_INVALIDPARAMS, "Setting height=0 returned %#x.\n", hr);
6420 reset_ddsd(&ddsd);
6421 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
6422 ok(SUCCEEDED(hr), "GetSurfaceDesc failed, hr %#x.\n", hr);
6423 ok(ddsd.dwWidth == 8, "SetSurfaceDesc: Expected width 8, got %u.\n", ddsd.dwWidth);
6424 ok(ddsd.dwHeight == 16, "SetSurfaceDesc: Expected height 16, got %u.\n", ddsd.dwHeight);
6426 /* Pitch and width can be set, but only together, and only with LPSURFACE. They must not be 0 */
6427 reset_ddsd(&ddsd);
6428 ddsd.dwFlags = DDSD_PITCH;
6429 U1(ddsd).lPitch = 8 * 4;
6430 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6431 ok(hr == DDERR_INVALIDPARAMS, "Setting pitch without lpSurface or width returned %#x.\n", hr);
6433 ddsd.dwFlags = DDSD_WIDTH;
6434 ddsd.dwWidth = 16;
6435 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6436 ok(hr == DDERR_INVALIDPARAMS, "Setting width without lpSurface or pitch returned %#x.\n", hr);
6438 ddsd.dwFlags = DDSD_PITCH | DDSD_LPSURFACE;
6439 ddsd.lpSurface = data;
6440 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6441 ok(hr == DDERR_INVALIDPARAMS, "Setting pitch and lpSurface without width returned %#x.\n", hr);
6443 ddsd.dwFlags = DDSD_WIDTH | DDSD_LPSURFACE;
6444 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6445 ok(hr == DDERR_INVALIDPARAMS, "Setting width and lpSurface without pitch returned %#x.\n", hr);
6447 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
6448 U1(ddsd).lPitch = 16 * 4;
6449 ddsd.dwWidth = 16;
6450 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6451 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6453 reset_ddsd(&ddsd);
6454 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
6455 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6456 ok(ddsd.dwWidth == 16, "SetSurfaceDesc: Expected width 8, got %u.\n", ddsd.dwWidth);
6457 ok(ddsd.dwHeight == 16, "SetSurfaceDesc: Expected height 16, got %u.\n", ddsd.dwHeight);
6458 ok(U1(ddsd).lPitch == 16 * 4, "SetSurfaceDesc: Expected pitch 64, got %u.\n", U1(ddsd).lPitch);
6460 /* The pitch must be 32 bit aligned and > 0, but is not verified for sanity otherwise.
6462 * VMware rejects those calls, but all real drivers accept it. Mark the VMware behavior broken. */
6463 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
6464 U1(ddsd).lPitch = 4 * 4;
6465 ddsd.lpSurface = data;
6466 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6467 ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDPARAMS), "Failed to set surface desc, hr %#x.\n", hr);
6469 U1(ddsd).lPitch = 4;
6470 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6471 ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDPARAMS), "Failed to set surface desc, hr %#x.\n", hr);
6473 U1(ddsd).lPitch = 16 * 4 + 1;
6474 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6475 ok(hr == DDERR_INVALIDPARAMS, "Setting misaligned pitch returned %#x.\n", hr);
6477 U1(ddsd).lPitch = 16 * 4 + 3;
6478 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6479 ok(hr == DDERR_INVALIDPARAMS, "Setting misaligned pitch returned %#x.\n", hr);
6481 U1(ddsd).lPitch = -4;
6482 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6483 ok(hr == DDERR_INVALIDPARAMS, "Setting negative pitch returned %#x.\n", hr);
6485 U1(ddsd).lPitch = 16 * 4;
6486 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6487 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6489 reset_ddsd(&ddsd);
6490 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
6491 U1(ddsd).lPitch = 0;
6492 ddsd.dwWidth = 16;
6493 ddsd.lpSurface = data;
6494 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6495 ok(hr == DDERR_INVALIDPARAMS, "Setting zero pitch returned %#x.\n", hr);
6497 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
6498 U1(ddsd).lPitch = 16 * 4;
6499 ddsd.dwWidth = 0;
6500 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6501 ok(hr == DDERR_INVALIDPARAMS, "Setting zero width returned %#x.\n", hr);
6503 /* Setting the pixelformat without LPSURFACE is an error, but with LPSURFACE it works. */
6504 ddsd.dwFlags = DDSD_PIXELFORMAT;
6505 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6506 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6507 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6508 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6509 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6510 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6511 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6512 ok(hr == DDERR_INVALIDPARAMS, "Setting the pixel format returned %#x.\n", hr);
6514 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_LPSURFACE;
6515 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6516 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6518 /* Can't set color keys. */
6519 reset_ddsd(&ddsd);
6520 ddsd.dwFlags = DDSD_CKSRCBLT;
6521 ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00ff0000;
6522 ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00ff0000;
6523 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6524 ok(hr == DDERR_INVALIDPARAMS, "Setting ddckCKSrcBlt returned %#x.\n", hr);
6526 ddsd.dwFlags = DDSD_CKSRCBLT | DDSD_LPSURFACE;
6527 ddsd.lpSurface = data;
6528 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6529 ok(hr == DDERR_INVALIDPARAMS, "Setting ddckCKSrcBlt returned %#x.\n", hr);
6531 IDirectDrawSurface4_Release(surface);
6533 /* SetSurfaceDesc needs systemmemory surfaces.
6535 * As a sidenote, fourcc surfaces aren't allowed in sysmem, thus testing DDSD_LINEARSIZE is moot. */
6536 for (i = 0; i < sizeof(invalid_caps_tests) / sizeof(*invalid_caps_tests); i++)
6538 reset_ddsd(&ddsd);
6539 ddsd.dwFlags = DDSD_CAPS;
6540 ddsd.ddsCaps.dwCaps = invalid_caps_tests[i].caps;
6541 ddsd.ddsCaps.dwCaps2 = invalid_caps_tests[i].caps2;
6542 if (!(invalid_caps_tests[i].caps & DDSCAPS_PRIMARYSURFACE))
6544 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6545 ddsd.dwWidth = 8;
6546 ddsd.dwHeight = 8;
6547 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6548 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6549 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6550 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6551 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6552 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6555 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6556 ok(SUCCEEDED(hr) || hr == DDERR_NODIRECTDRAWHW, "Failed to create surface, hr %#x.\n", hr);
6557 if (FAILED(hr))
6559 skip("Cannot create a %s surface, skipping vidmem SetSurfaceDesc test.\n",
6560 invalid_caps_tests[i].name);
6561 goto done;
6564 reset_ddsd(&ddsd);
6565 ddsd.dwFlags = DDSD_LPSURFACE;
6566 ddsd.lpSurface = data;
6567 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6568 if (invalid_caps_tests[i].supported)
6570 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6572 else
6574 ok(hr == DDERR_INVALIDSURFACETYPE, "SetSurfaceDesc on a %s surface returned %#x.\n",
6575 invalid_caps_tests[i].name, hr);
6577 /* Check priority of error conditions. */
6578 ddsd.dwFlags = DDSD_WIDTH;
6579 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6580 ok(hr == DDERR_INVALIDSURFACETYPE, "SetSurfaceDesc on a %s surface returned %#x.\n",
6581 invalid_caps_tests[i].name, hr);
6584 IDirectDrawSurface4_Release(surface);
6587 done:
6588 ref = IDirectDraw4_Release(ddraw);
6589 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6590 DestroyWindow(window);
6593 static void test_user_memory_getdc(void)
6595 IDirectDraw4 *ddraw;
6596 HWND window;
6597 HRESULT hr;
6598 DDSURFACEDESC2 ddsd;
6599 IDirectDrawSurface4 *surface;
6600 DWORD data[16][16];
6601 ULONG ref;
6602 HDC dc;
6603 unsigned int x, y;
6605 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6606 0, 0, 640, 480, 0, 0, 0, 0);
6607 ddraw = create_ddraw();
6608 ok(!!ddraw, "Failed to create a ddraw object.\n");
6610 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6611 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6613 reset_ddsd(&ddsd);
6614 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
6615 ddsd.dwWidth = 16;
6616 ddsd.dwHeight = 16;
6617 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6618 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6619 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6620 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6621 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6622 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6623 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
6624 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6625 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6627 memset(data, 0xaa, sizeof(data));
6628 reset_ddsd(&ddsd);
6629 ddsd.dwFlags = DDSD_LPSURFACE;
6630 ddsd.lpSurface = data;
6631 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6632 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6634 hr = IDirectDrawSurface4_GetDC(surface, &dc);
6635 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6636 BitBlt(dc, 0, 0, 16, 8, NULL, 0, 0, WHITENESS);
6637 BitBlt(dc, 0, 8, 16, 8, NULL, 0, 0, BLACKNESS);
6638 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
6639 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6641 ok(data[0][0] == 0xffffffff, "Expected color 0xffffffff, got %#x.\n", data[0][0]);
6642 ok(data[15][15] == 0x00000000, "Expected color 0x00000000, got %#x.\n", data[15][15]);
6644 ddsd.dwFlags = DDSD_LPSURFACE | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PITCH;
6645 ddsd.lpSurface = data;
6646 ddsd.dwWidth = 4;
6647 ddsd.dwHeight = 8;
6648 U1(ddsd).lPitch = sizeof(*data);
6649 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6650 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6652 memset(data, 0xaa, sizeof(data));
6653 hr = IDirectDrawSurface4_GetDC(surface, &dc);
6654 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6655 BitBlt(dc, 0, 0, 4, 8, NULL, 0, 0, BLACKNESS);
6656 BitBlt(dc, 1, 1, 2, 2, NULL, 0, 0, WHITENESS);
6657 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
6658 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6660 for (y = 0; y < 4; y++)
6662 for (x = 0; x < 4; x++)
6664 if ((x == 1 || x == 2) && (y == 1 || y == 2))
6665 ok(data[y][x] == 0xffffffff, "Expected color 0xffffffff on position %ux%u, got %#x.\n",
6666 x, y, data[y][x]);
6667 else
6668 ok(data[y][x] == 0x00000000, "Expected color 0x00000000 on position %ux%u, got %#x.\n",
6669 x, y, data[y][x]);
6672 ok(data[0][5] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 5x0, got %#x.\n",
6673 data[0][5]);
6674 ok(data[7][3] == 0x00000000, "Expected color 0x00000000 on position 3x7, got %#x.\n",
6675 data[7][3]);
6676 ok(data[7][4] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 4x7, got %#x.\n",
6677 data[7][4]);
6678 ok(data[8][0] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 0x8, got %#x.\n",
6679 data[8][0]);
6681 IDirectDrawSurface4_Release(surface);
6682 ref = IDirectDraw4_Release(ddraw);
6683 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6684 DestroyWindow(window);
6687 static void test_sysmem_overlay(void)
6689 IDirectDraw4 *ddraw;
6690 HWND window;
6691 HRESULT hr;
6692 DDSURFACEDESC2 ddsd;
6693 IDirectDrawSurface4 *surface;
6694 ULONG ref;
6696 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6697 0, 0, 640, 480, 0, 0, 0, 0);
6698 ddraw = create_ddraw();
6699 ok(!!ddraw, "Failed to create a ddraw object.\n");
6701 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6702 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6704 reset_ddsd(&ddsd);
6705 ddsd.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
6706 ddsd.dwWidth = 16;
6707 ddsd.dwHeight = 16;
6708 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OVERLAY;
6709 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6710 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6711 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6712 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6713 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6714 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6715 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6716 ok(hr == DDERR_NOOVERLAYHW, "Got unexpected hr %#x.\n", hr);
6718 ref = IDirectDraw4_Release(ddraw);
6719 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6720 DestroyWindow(window);
6723 static void test_primary_palette(void)
6725 DDSCAPS2 surface_caps = {DDSCAPS_FLIP, 0, 0, {0}};
6726 IDirectDrawSurface4 *primary, *backbuffer;
6727 PALETTEENTRY palette_entries[256];
6728 IDirectDrawPalette *palette, *tmp;
6729 DDSURFACEDESC2 surface_desc;
6730 IDirectDraw4 *ddraw;
6731 DWORD palette_caps;
6732 ULONG refcount;
6733 HWND window;
6734 HRESULT hr;
6736 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6737 0, 0, 640, 480, 0, 0, 0, 0);
6738 ddraw = create_ddraw();
6739 ok(!!ddraw, "Failed to create a ddraw object.\n");
6740 if (FAILED(IDirectDraw4_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
6742 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
6743 IDirectDraw4_Release(ddraw);
6744 DestroyWindow(window);
6745 return;
6747 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6748 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6750 memset(&surface_desc, 0, sizeof(surface_desc));
6751 surface_desc.dwSize = sizeof(surface_desc);
6752 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
6753 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
6754 U5(surface_desc).dwBackBufferCount = 1;
6755 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &primary, NULL);
6756 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6757 hr = IDirectDrawSurface4_GetAttachedSurface(primary, &surface_caps, &backbuffer);
6758 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6760 memset(palette_entries, 0, sizeof(palette_entries));
6761 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256, palette_entries, &palette, NULL);
6762 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6763 refcount = get_refcount((IUnknown *)palette);
6764 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6766 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
6767 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
6768 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
6770 hr = IDirectDrawSurface4_SetPalette(primary, palette);
6771 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6773 /* The Windows 8 testbot attaches the palette to the backbuffer as well,
6774 * and is generally somewhat broken with respect to 8 bpp / palette
6775 * handling. */
6776 if (SUCCEEDED(IDirectDrawSurface4_GetPalette(backbuffer, &tmp)))
6778 win_skip("Broken palette handling detected, skipping tests.\n");
6779 IDirectDrawPalette_Release(tmp);
6780 IDirectDrawPalette_Release(palette);
6781 /* The Windows 8 testbot keeps extra references to the primary and
6782 * backbuffer while in 8 bpp mode. */
6783 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
6784 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
6785 goto done;
6788 refcount = get_refcount((IUnknown *)palette);
6789 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
6791 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
6792 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
6793 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE | DDPCAPS_ALLOW256),
6794 "Got unexpected palette caps %#x.\n", palette_caps);
6796 hr = IDirectDrawSurface4_SetPalette(primary, NULL);
6797 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6798 refcount = get_refcount((IUnknown *)palette);
6799 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6801 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
6802 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
6803 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
6805 hr = IDirectDrawSurface4_SetPalette(primary, palette);
6806 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6807 refcount = get_refcount((IUnknown *)palette);
6808 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
6810 hr = IDirectDrawSurface4_GetPalette(primary, &tmp);
6811 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
6812 ok(tmp == palette, "Got unexpected palette %p, expected %p.\n", tmp, palette);
6813 IDirectDrawPalette_Release(tmp);
6814 hr = IDirectDrawSurface4_GetPalette(backbuffer, &tmp);
6815 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
6817 refcount = IDirectDrawPalette_Release(palette);
6818 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6819 refcount = IDirectDrawPalette_Release(palette);
6820 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6822 /* Note that this only seems to work when the palette is attached to the
6823 * primary surface. When attached to a regular surface, attempting to get
6824 * the palette here will cause an access violation. */
6825 hr = IDirectDrawSurface4_GetPalette(primary, &tmp);
6826 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
6828 done:
6829 refcount = IDirectDrawSurface4_Release(backbuffer);
6830 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6831 refcount = IDirectDrawSurface4_Release(primary);
6832 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6833 refcount = IDirectDraw4_Release(ddraw);
6834 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6835 DestroyWindow(window);
6838 static HRESULT WINAPI surface_counter(IDirectDrawSurface4 *surface, DDSURFACEDESC2 *desc, void *context)
6840 UINT *surface_count = context;
6842 ++(*surface_count);
6843 IDirectDrawSurface_Release(surface);
6845 return DDENUMRET_OK;
6848 static void test_surface_attachment(void)
6850 IDirectDrawSurface4 *surface1, *surface2, *surface3, *surface4;
6851 IDirectDrawSurface *surface1v1, *surface2v1;
6852 DDSCAPS2 caps = {DDSCAPS_TEXTURE, 0, 0, {0}};
6853 DDSURFACEDESC2 surface_desc;
6854 IDirectDraw4 *ddraw;
6855 UINT surface_count;
6856 ULONG refcount;
6857 HWND window;
6858 HRESULT hr;
6860 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6861 0, 0, 640, 480, 0, 0, 0, 0);
6862 ddraw = create_ddraw();
6863 ok(!!ddraw, "Failed to create a ddraw object.\n");
6864 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6865 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6867 memset(&surface_desc, 0, sizeof(surface_desc));
6868 surface_desc.dwSize = sizeof(surface_desc);
6869 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
6870 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
6871 U2(surface_desc).dwMipMapCount = 3;
6872 surface_desc.dwWidth = 128;
6873 surface_desc.dwHeight = 128;
6874 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
6875 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6877 hr = IDirectDrawSurface4_GetAttachedSurface(surface1, &caps, &surface2);
6878 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
6879 hr = IDirectDrawSurface4_GetAttachedSurface(surface2, &caps, &surface3);
6880 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
6881 hr = IDirectDrawSurface4_GetAttachedSurface(surface3, &caps, &surface4);
6882 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
6884 surface_count = 0;
6885 IDirectDrawSurface4_EnumAttachedSurfaces(surface1, &surface_count, surface_counter);
6886 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
6887 surface_count = 0;
6888 IDirectDrawSurface4_EnumAttachedSurfaces(surface2, &surface_count, surface_counter);
6889 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
6890 surface_count = 0;
6891 IDirectDrawSurface4_EnumAttachedSurfaces(surface3, &surface_count, surface_counter);
6892 ok(!surface_count, "Got unexpected surface_count %u.\n", surface_count);
6894 memset(&surface_desc, 0, sizeof(surface_desc));
6895 surface_desc.dwSize = sizeof(surface_desc);
6896 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6897 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6898 surface_desc.dwWidth = 16;
6899 surface_desc.dwHeight = 16;
6900 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
6901 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6903 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface4);
6904 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6905 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface1);
6906 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6907 hr = IDirectDrawSurface4_AddAttachedSurface(surface3, surface4);
6908 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6909 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface3);
6910 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6911 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface4);
6912 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6913 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface2);
6914 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6916 IDirectDrawSurface4_Release(surface4);
6918 memset(&surface_desc, 0, sizeof(surface_desc));
6919 surface_desc.dwSize = sizeof(surface_desc);
6920 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6921 surface_desc.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
6922 surface_desc.dwWidth = 16;
6923 surface_desc.dwHeight = 16;
6924 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
6925 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6927 if (SUCCEEDED(hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface4)))
6929 skip("Running on refrast, skipping some tests.\n");
6930 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface4);
6931 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
6933 else
6935 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6936 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface1);
6937 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6938 hr = IDirectDrawSurface4_AddAttachedSurface(surface3, surface4);
6939 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6940 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface3);
6941 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6942 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface4);
6943 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6944 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface2);
6945 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6948 IDirectDrawSurface4_Release(surface4);
6949 IDirectDrawSurface4_Release(surface3);
6950 IDirectDrawSurface4_Release(surface2);
6951 IDirectDrawSurface4_Release(surface1);
6953 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6954 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6956 /* Try a single primary and two offscreen plain surfaces. */
6957 memset(&surface_desc, 0, sizeof(surface_desc));
6958 surface_desc.dwSize = sizeof(surface_desc);
6959 surface_desc.dwFlags = DDSD_CAPS;
6960 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
6961 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
6962 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6964 memset(&surface_desc, 0, sizeof(surface_desc));
6965 surface_desc.dwSize = sizeof(surface_desc);
6966 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6967 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6968 surface_desc.dwWidth = registry_mode.dmPelsWidth;
6969 surface_desc.dwHeight = registry_mode.dmPelsHeight;
6970 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
6971 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6973 memset(&surface_desc, 0, sizeof(surface_desc));
6974 surface_desc.dwSize = sizeof(surface_desc);
6975 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6976 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6977 surface_desc.dwWidth = registry_mode.dmPelsWidth;
6978 surface_desc.dwHeight = registry_mode.dmPelsHeight;
6979 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
6980 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6982 /* This one has a different size. */
6983 memset(&surface_desc, 0, sizeof(surface_desc));
6984 surface_desc.dwSize = sizeof(surface_desc);
6985 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6986 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6987 surface_desc.dwWidth = 128;
6988 surface_desc.dwHeight = 128;
6989 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
6990 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6992 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface2);
6993 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
6994 /* Try the reverse without detaching first. */
6995 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface1);
6996 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
6997 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
6998 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7000 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface1);
7001 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7002 /* Try to detach reversed. */
7003 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
7004 ok(hr == DDERR_CANNOTDETACHSURFACE, "Got unexpected hr %#x.\n", hr);
7005 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface2, 0, surface1);
7006 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7008 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface3);
7009 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7010 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface2, 0, surface3);
7011 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7013 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface4);
7014 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7015 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface1);
7016 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7018 IDirectDrawSurface4_Release(surface4);
7019 IDirectDrawSurface4_Release(surface3);
7020 IDirectDrawSurface4_Release(surface2);
7021 IDirectDrawSurface4_Release(surface1);
7023 /* Test DeleteAttachedSurface() and automatic detachment of attached surfaces on release. */
7024 memset(&surface_desc, 0, sizeof(surface_desc));
7025 surface_desc.dwSize = sizeof(surface_desc);
7026 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7027 surface_desc.dwWidth = 64;
7028 surface_desc.dwHeight = 64;
7029 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
7030 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7031 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB; /* D3DFMT_R5G6B5 */
7032 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 16;
7033 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0xf800;
7034 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x07e0;
7035 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x001f;
7036 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
7037 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7038 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
7039 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7041 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
7042 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
7043 U1(U4(surface_desc).ddpfPixelFormat).dwZBufferBitDepth = 16;
7044 U3(U4(surface_desc).ddpfPixelFormat).dwZBitMask = 0x0000ffff;
7045 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
7046 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7048 hr = IDirectDrawSurface4_QueryInterface(surface1, &IID_IDirectDrawSurface, (void **)&surface1v1);
7049 ok(SUCCEEDED(hr), "Failed to get interface, hr %#x.\n", hr);
7050 hr = IDirectDrawSurface4_QueryInterface(surface2, &IID_IDirectDrawSurface, (void **)&surface2v1);
7051 ok(SUCCEEDED(hr), "Failed to get interface, hr %#x.\n", hr);
7053 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface2);
7054 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7055 refcount = get_refcount((IUnknown *)surface2);
7056 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
7057 refcount = get_refcount((IUnknown *)surface2v1);
7058 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
7059 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface2);
7060 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
7061 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
7062 todo_wine ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7063 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1v1, 0, surface2v1);
7064 ok(hr == DDERR_SURFACENOTATTACHED, "Got unexpected hr %#x.\n", hr);
7066 /* Attaching while already attached to other surface. */
7067 hr = IDirectDrawSurface4_AddAttachedSurface(surface3, surface2);
7068 todo_wine ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7069 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface3, 0, surface2);
7070 todo_wine ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7071 IDirectDrawSurface4_Release(surface3);
7073 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
7074 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7075 refcount = get_refcount((IUnknown *)surface2);
7076 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
7077 refcount = get_refcount((IUnknown *)surface2v1);
7078 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
7080 /* DeleteAttachedSurface() when attaching via IDirectDrawSurface. */
7081 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
7082 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7083 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
7084 ok(hr == DDERR_SURFACENOTATTACHED, "Got unexpected hr %#x.\n", hr);
7085 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1v1, 0, surface2v1);
7086 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7087 refcount = IDirectDrawSurface4_Release(surface2);
7088 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7089 refcount = IDirectDrawSurface4_Release(surface1);
7090 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7092 /* Automatic detachment on release. */
7093 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
7094 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7095 refcount = get_refcount((IUnknown *)surface2v1);
7096 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
7097 refcount = IDirectDrawSurface_Release(surface1v1);
7098 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7099 refcount = IDirectDrawSurface_Release(surface2v1);
7100 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7101 refcount = IDirectDraw4_Release(ddraw);
7102 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7103 DestroyWindow(window);
7106 static void test_private_data(void)
7108 IDirectDraw4 *ddraw;
7109 IDirectDrawSurface4 *surface, *surface2;
7110 DDSURFACEDESC2 surface_desc;
7111 ULONG refcount, refcount2, refcount3;
7112 IUnknown *ptr;
7113 DWORD size = sizeof(ptr);
7114 HRESULT hr;
7115 HWND window;
7116 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
7117 DWORD data[] = {1, 2, 3, 4};
7118 DDCAPS hal_caps;
7119 static const GUID ddraw_private_data_test_guid =
7121 0xfdb37466,
7122 0x428f,
7123 0x4edf,
7124 {0xa3,0x7f,0x9b,0x1d,0xf4,0x88,0xc5,0xfc}
7126 static const GUID ddraw_private_data_test_guid2 =
7128 0x2e5afac2,
7129 0x87b5,
7130 0x4c10,
7131 {0x9b,0x4b,0x89,0xd7,0xd1,0x12,0xe7,0x2b}
7134 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7135 0, 0, 640, 480, 0, 0, 0, 0);
7136 ddraw = create_ddraw();
7137 ok(!!ddraw, "Failed to create a ddraw object.\n");
7138 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7139 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7141 reset_ddsd(&surface_desc);
7142 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
7143 surface_desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
7144 surface_desc.dwHeight = 4;
7145 surface_desc.dwWidth = 4;
7146 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7147 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7149 /* NULL pointers are not valid, but don't cause a crash. */
7150 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL,
7151 sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
7152 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7153 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL, 0, 0);
7154 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7155 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL, 1, 0);
7156 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7158 /* DDSPD_IUNKNOWNPOINTER needs sizeof(IUnknown *) bytes of data. */
7159 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7160 0, DDSPD_IUNKNOWNPOINTER);
7161 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7162 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7163 5, DDSPD_IUNKNOWNPOINTER);
7164 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7165 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7166 sizeof(ddraw) * 2, DDSPD_IUNKNOWNPOINTER);
7167 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7169 /* Note that with a size != 0 and size != sizeof(IUnknown *) and
7170 * DDSPD_IUNKNOWNPOINTER set SetPrivateData in ddraw4 and ddraw7
7171 * erases the old content and returns an error. This behavior has
7172 * been fixed in d3d8 and d3d9. Unless an application is found
7173 * that depends on this we don't care about this behavior. */
7174 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7175 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
7176 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7177 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7178 0, DDSPD_IUNKNOWNPOINTER);
7179 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7180 size = sizeof(ptr);
7181 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
7182 ok(SUCCEEDED(hr), "Failed to get private data, hr %#x.\n", hr);
7183 hr = IDirectDrawSurface4_FreePrivateData(surface, &ddraw_private_data_test_guid);
7184 ok(SUCCEEDED(hr), "Failed to free private data, hr %#x.\n", hr);
7186 refcount = get_refcount((IUnknown *)ddraw);
7187 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7188 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
7189 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7190 refcount2 = get_refcount((IUnknown *)ddraw);
7191 ok(refcount2 == refcount + 1, "Got unexpected refcount %u.\n", refcount2);
7193 hr = IDirectDrawSurface4_FreePrivateData(surface, &ddraw_private_data_test_guid);
7194 ok(SUCCEEDED(hr), "Failed to free private data, hr %#x.\n", hr);
7195 refcount2 = get_refcount((IUnknown *)ddraw);
7196 ok(refcount2 == refcount, "Got unexpected refcount %u.\n", refcount2);
7198 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7199 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
7200 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7201 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, surface,
7202 sizeof(surface), DDSPD_IUNKNOWNPOINTER);
7203 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7204 refcount2 = get_refcount((IUnknown *)ddraw);
7205 ok(refcount2 == refcount, "Got unexpected refcount %u.\n", refcount2);
7207 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7208 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
7209 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7210 size = 2 * sizeof(ptr);
7211 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
7212 ok(SUCCEEDED(hr), "Failed to get private data, hr %#x.\n", hr);
7213 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
7214 refcount2 = get_refcount(ptr);
7215 /* Object is NOT addref'ed by the getter. */
7216 ok(ptr == (IUnknown *)ddraw, "Returned interface pointer is %p, expected %p.\n", ptr, ddraw);
7217 ok(refcount2 == refcount + 1, "Got unexpected refcount %u.\n", refcount2);
7219 ptr = (IUnknown *)0xdeadbeef;
7220 size = 1;
7221 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, &size);
7222 ok(hr == DDERR_MOREDATA, "Got unexpected hr %#x.\n", hr);
7223 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
7224 size = 2 * sizeof(ptr);
7225 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, &size);
7226 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7227 ok(size == 2 * sizeof(ptr), "Got unexpected size %u.\n", size);
7228 size = 1;
7229 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
7230 ok(hr == DDERR_MOREDATA, "Got unexpected hr %#x.\n", hr);
7231 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
7232 ok(ptr == (IUnknown *)0xdeadbeef, "Got unexpected pointer %p.\n", ptr);
7233 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid2, NULL, NULL);
7234 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
7235 size = 0xdeadbabe;
7236 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid2, &ptr, &size);
7237 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
7238 ok(ptr == (IUnknown *)0xdeadbeef, "Got unexpected pointer %p.\n", ptr);
7239 ok(size == 0xdeadbabe, "Got unexpected size %u.\n", size);
7240 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, NULL);
7241 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7243 refcount3 = IDirectDrawSurface4_Release(surface);
7244 ok(!refcount3, "Got unexpected refcount %u.\n", refcount3);
7246 /* Destroying the surface frees the reference held on the private data. It also frees
7247 * the reference the surface is holding on its creating object. */
7248 refcount2 = get_refcount((IUnknown *)ddraw);
7249 ok(refcount2 == refcount - 1, "Got unexpected refcount %u.\n", refcount2);
7251 memset(&hal_caps, 0, sizeof(hal_caps));
7252 hal_caps.dwSize = sizeof(hal_caps);
7253 hr = IDirectDraw7_GetCaps(ddraw, &hal_caps, NULL);
7254 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
7255 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) == (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
7257 reset_ddsd(&surface_desc);
7258 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_MIPMAPCOUNT;
7259 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
7260 surface_desc.dwHeight = 4;
7261 surface_desc.dwWidth = 4;
7262 U2(surface_desc).dwMipMapCount = 2;
7263 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7264 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7265 hr = IDirectDrawSurface4_GetAttachedSurface(surface, &caps, &surface2);
7266 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
7268 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, data, sizeof(data), 0);
7269 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7270 hr = IDirectDrawSurface4_GetPrivateData(surface2, &ddraw_private_data_test_guid, NULL, NULL);
7271 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
7273 IDirectDrawSurface4_Release(surface2);
7274 IDirectDrawSurface4_Release(surface);
7276 else
7277 skip("Mipmapped textures not supported, skipping mipmap private data test.\n");
7279 refcount = IDirectDraw4_Release(ddraw);
7280 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7281 DestroyWindow(window);
7284 static void test_pixel_format(void)
7286 HWND window, window2 = NULL;
7287 HDC hdc, hdc2 = NULL;
7288 HMODULE gl = NULL;
7289 int format, test_format;
7290 PIXELFORMATDESCRIPTOR pfd;
7291 IDirectDraw4 *ddraw = NULL;
7292 IDirectDrawClipper *clipper = NULL;
7293 DDSURFACEDESC2 ddsd;
7294 IDirectDrawSurface4 *primary = NULL;
7295 DDBLTFX fx;
7296 HRESULT hr;
7298 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
7299 100, 100, 160, 160, NULL, NULL, NULL, NULL);
7300 if (!window)
7302 skip("Failed to create window\n");
7303 return;
7306 window2 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
7307 100, 100, 160, 160, NULL, NULL, NULL, NULL);
7309 hdc = GetDC(window);
7310 if (!hdc)
7312 skip("Failed to get DC\n");
7313 goto cleanup;
7316 if (window2)
7317 hdc2 = GetDC(window2);
7319 gl = LoadLibraryA("opengl32.dll");
7320 ok(!!gl, "failed to load opengl32.dll; SetPixelFormat()/GetPixelFormat() may not work right\n");
7322 format = GetPixelFormat(hdc);
7323 ok(format == 0, "new window has pixel format %d\n", format);
7325 ZeroMemory(&pfd, sizeof(pfd));
7326 pfd.nSize = sizeof(pfd);
7327 pfd.nVersion = 1;
7328 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
7329 pfd.iPixelType = PFD_TYPE_RGBA;
7330 pfd.iLayerType = PFD_MAIN_PLANE;
7331 format = ChoosePixelFormat(hdc, &pfd);
7332 if (format <= 0)
7334 skip("no pixel format available\n");
7335 goto cleanup;
7338 if (!SetPixelFormat(hdc, format, &pfd) || GetPixelFormat(hdc) != format)
7340 skip("failed to set pixel format\n");
7341 goto cleanup;
7344 if (!hdc2 || !SetPixelFormat(hdc2, format, &pfd) || GetPixelFormat(hdc2) != format)
7346 skip("failed to set pixel format on second window\n");
7347 if (hdc2)
7349 ReleaseDC(window2, hdc2);
7350 hdc2 = NULL;
7354 ddraw = create_ddraw();
7355 ok(!!ddraw, "Failed to create a ddraw object.\n");
7357 test_format = GetPixelFormat(hdc);
7358 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7360 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7361 if (FAILED(hr))
7363 skip("Failed to set cooperative level, hr %#x.\n", hr);
7364 goto cleanup;
7367 test_format = GetPixelFormat(hdc);
7368 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7370 if (hdc2)
7372 hr = IDirectDraw4_CreateClipper(ddraw, 0, &clipper, NULL);
7373 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
7374 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window2);
7375 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
7377 test_format = GetPixelFormat(hdc);
7378 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7380 test_format = GetPixelFormat(hdc2);
7381 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
7384 memset(&ddsd, 0, sizeof(ddsd));
7385 ddsd.dwSize = sizeof(ddsd);
7386 ddsd.dwFlags = DDSD_CAPS;
7387 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
7389 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
7390 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
7392 test_format = GetPixelFormat(hdc);
7393 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7395 if (hdc2)
7397 test_format = GetPixelFormat(hdc2);
7398 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
7401 if (clipper)
7403 hr = IDirectDrawSurface4_SetClipper(primary, clipper);
7404 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
7406 test_format = GetPixelFormat(hdc);
7407 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7409 test_format = GetPixelFormat(hdc2);
7410 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
7413 memset(&fx, 0, sizeof(fx));
7414 fx.dwSize = sizeof(fx);
7415 hr = IDirectDrawSurface4_Blt(primary, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7416 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
7418 test_format = GetPixelFormat(hdc);
7419 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7421 if (hdc2)
7423 test_format = GetPixelFormat(hdc2);
7424 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
7427 cleanup:
7428 if (primary) IDirectDrawSurface4_Release(primary);
7429 if (clipper) IDirectDrawClipper_Release(clipper);
7430 if (ddraw) IDirectDraw4_Release(ddraw);
7431 if (gl) FreeLibrary(gl);
7432 if (hdc) ReleaseDC(window, hdc);
7433 if (hdc2) ReleaseDC(window2, hdc2);
7434 if (window) DestroyWindow(window);
7435 if (window2) DestroyWindow(window2);
7438 static void test_create_surface_pitch(void)
7440 IDirectDrawSurface4 *surface;
7441 DDSURFACEDESC2 surface_desc;
7442 IDirectDraw4 *ddraw;
7443 unsigned int i;
7444 ULONG refcount;
7445 HWND window;
7446 HRESULT hr;
7447 void *mem;
7449 static const struct
7451 DWORD placement;
7452 DWORD flags_in;
7453 DWORD pitch_in;
7454 HRESULT hr;
7455 DWORD flags_out;
7456 DWORD pitch_out32;
7457 DWORD pitch_out64;
7459 test_data[] =
7461 {DDSCAPS_VIDEOMEMORY, 0, 0, DD_OK,
7462 DDSD_PITCH, 0x100, 0x100},
7463 {DDSCAPS_VIDEOMEMORY, DDSD_PITCH, 0x104, DD_OK,
7464 DDSD_PITCH, 0x100, 0x100},
7465 {DDSCAPS_VIDEOMEMORY, DDSD_PITCH, 0x0f8, DD_OK,
7466 DDSD_PITCH, 0x100, 0x100},
7467 {DDSCAPS_VIDEOMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDCAPS,
7468 0, 0, 0 },
7469 {DDSCAPS_SYSTEMMEMORY, 0, 0, DD_OK,
7470 DDSD_PITCH, 0x100, 0x0fc},
7471 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH, 0x104, DD_OK,
7472 DDSD_PITCH, 0x100, 0x0fc},
7473 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH, 0x0f8, DD_OK,
7474 DDSD_PITCH, 0x100, 0x0fc},
7475 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH | DDSD_LINEARSIZE, 0, DD_OK,
7476 DDSD_PITCH, 0x100, 0x0fc},
7477 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE, 0, DDERR_INVALIDPARAMS,
7478 0, 0, 0 },
7479 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x100, DD_OK,
7480 DDSD_PITCH, 0x100, 0x100},
7481 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x0fe, DDERR_INVALIDPARAMS,
7482 0, 0, 0 },
7483 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x0fc, DD_OK,
7484 DDSD_PITCH, 0x0fc, 0x0fc},
7485 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x0f8, DDERR_INVALIDPARAMS,
7486 0, 0, 0 },
7487 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_LINEARSIZE, 0x100, DDERR_INVALIDPARAMS,
7488 0, 0, 0 },
7489 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_LINEARSIZE, 0x3f00, DDERR_INVALIDPARAMS,
7490 0, 0, 0 },
7491 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH | DDSD_LINEARSIZE, 0x100, DD_OK,
7492 DDSD_PITCH, 0x100, 0x100},
7494 DWORD flags_mask = DDSD_PITCH | DDSD_LPSURFACE | DDSD_LINEARSIZE;
7496 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7497 0, 0, 640, 480, 0, 0, 0, 0);
7498 ddraw = create_ddraw();
7499 ok(!!ddraw, "Failed to create a ddraw object.\n");
7500 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7501 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7503 mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ((63 * 4) + 8) * 63);
7505 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
7507 memset(&surface_desc, 0, sizeof(surface_desc));
7508 surface_desc.dwSize = sizeof(surface_desc);
7509 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | test_data[i].flags_in;
7510 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | test_data[i].placement;
7511 surface_desc.dwWidth = 63;
7512 surface_desc.dwHeight = 63;
7513 U1(surface_desc).lPitch = test_data[i].pitch_in;
7514 surface_desc.lpSurface = mem;
7515 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7516 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
7517 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
7518 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
7519 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
7520 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
7521 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7522 ok(hr == test_data[i].hr || (test_data[i].placement == DDSCAPS_VIDEOMEMORY && hr == DDERR_NODIRECTDRAWHW),
7523 "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
7524 if (FAILED(hr))
7525 continue;
7527 memset(&surface_desc, 0, sizeof(surface_desc));
7528 surface_desc.dwSize = sizeof(surface_desc);
7529 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
7530 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
7531 ok((surface_desc.dwFlags & flags_mask) == test_data[i].flags_out,
7532 "Test %u: Got unexpected flags %#x, expected %#x.\n",
7533 i, surface_desc.dwFlags & flags_mask, test_data[i].flags_out);
7534 if (sizeof(void *) != sizeof(DWORD) && test_data[i].pitch_out32 != test_data[i].pitch_out64)
7535 todo_wine ok(U1(surface_desc).lPitch == test_data[i].pitch_out64,
7536 "Test %u: Got unexpected pitch %u, expected %u.\n",
7537 i, U1(surface_desc).lPitch, test_data[i].pitch_out64);
7538 else
7539 ok(U1(surface_desc).lPitch == test_data[i].pitch_out32,
7540 "Test %u: Got unexpected pitch %u, expected %u.\n",
7541 i, U1(surface_desc).lPitch, test_data[i].pitch_out32);
7542 ok(!surface_desc.lpSurface, "Test %u: Got unexpected lpSurface %p.\n", i, surface_desc.lpSurface);
7544 IDirectDrawSurface4_Release(surface);
7547 HeapFree(GetProcessHeap(), 0, mem);
7548 refcount = IDirectDraw4_Release(ddraw);
7549 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7550 DestroyWindow(window);
7553 static void test_mipmap_lock(void)
7555 IDirectDrawSurface4 *surface, *surface2;
7556 DDSURFACEDESC2 surface_desc;
7557 IDirectDraw4 *ddraw;
7558 ULONG refcount;
7559 HWND window;
7560 HRESULT hr;
7561 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
7562 DDCAPS hal_caps;
7564 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7565 0, 0, 640, 480, 0, 0, 0, 0);
7566 ddraw = create_ddraw();
7567 ok(!!ddraw, "Failed to create a ddraw object.\n");
7568 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7569 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7571 memset(&hal_caps, 0, sizeof(hal_caps));
7572 hal_caps.dwSize = sizeof(hal_caps);
7573 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
7574 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
7575 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
7577 skip("Mipmapped textures not supported, skipping mipmap lock test.\n");
7578 IDirectDraw4_Release(ddraw);
7579 DestroyWindow(window);
7580 return;
7583 memset(&surface_desc, 0, sizeof(surface_desc));
7584 surface_desc.dwSize = sizeof(surface_desc);
7585 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
7586 surface_desc.dwWidth = 4;
7587 surface_desc.dwHeight = 4;
7588 U2(surface_desc).dwMipMapCount = 2;
7589 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP
7590 | DDSCAPS_SYSTEMMEMORY;
7591 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7592 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7593 hr = IDirectDrawSurface4_GetAttachedSurface(surface, &caps, &surface2);
7594 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
7596 memset(&surface_desc, 0, sizeof(surface_desc));
7597 surface_desc.dwSize = sizeof(surface_desc);
7598 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, 0, NULL);
7599 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
7600 memset(&surface_desc, 0, sizeof(surface_desc));
7601 surface_desc.dwSize = sizeof(surface_desc);
7602 hr = IDirectDrawSurface4_Lock(surface2, NULL, &surface_desc, 0, NULL);
7603 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
7604 IDirectDrawSurface4_Unlock(surface2, NULL);
7605 IDirectDrawSurface4_Unlock(surface, NULL);
7607 IDirectDrawSurface4_Release(surface2);
7608 IDirectDrawSurface4_Release(surface);
7609 refcount = IDirectDraw4_Release(ddraw);
7610 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7611 DestroyWindow(window);
7614 static void test_palette_complex(void)
7616 IDirectDrawSurface4 *surface, *mipmap, *tmp;
7617 DDSURFACEDESC2 surface_desc;
7618 IDirectDraw4 *ddraw;
7619 IDirectDrawPalette *palette, *palette2, *palette_mipmap;
7620 ULONG refcount;
7621 HWND window;
7622 HRESULT hr;
7623 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
7624 DDCAPS hal_caps;
7625 PALETTEENTRY palette_entries[256];
7626 unsigned int i;
7627 HDC dc;
7628 RGBQUAD rgbquad;
7629 UINT count;
7631 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7632 0, 0, 640, 480, 0, 0, 0, 0);
7633 ddraw = create_ddraw();
7634 ok(!!ddraw, "Failed to create a ddraw object.\n");
7635 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7636 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7638 memset(&hal_caps, 0, sizeof(hal_caps));
7639 hal_caps.dwSize = sizeof(hal_caps);
7640 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
7641 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
7642 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
7644 skip("Mipmapped textures not supported, skipping mipmap palette test.\n");
7645 IDirectDraw4_Release(ddraw);
7646 DestroyWindow(window);
7647 return;
7650 memset(&surface_desc, 0, sizeof(surface_desc));
7651 surface_desc.dwSize = sizeof(surface_desc);
7652 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7653 surface_desc.dwWidth = 128;
7654 surface_desc.dwHeight = 128;
7655 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
7656 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7657 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
7658 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
7659 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7660 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7662 memset(palette_entries, 0, sizeof(palette_entries));
7663 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7664 palette_entries, &palette, NULL);
7665 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7667 memset(palette_entries, 0, sizeof(palette_entries));
7668 palette_entries[1].peRed = 0xff;
7669 palette_entries[1].peGreen = 0x80;
7670 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7671 palette_entries, &palette_mipmap, NULL);
7672 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7674 palette2 = (void *)0xdeadbeef;
7675 hr = IDirectDrawSurface4_GetPalette(surface, &palette2);
7676 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
7677 ok(!palette2, "Got unexpected palette %p.\n", palette2);
7678 hr = IDirectDrawSurface4_SetPalette(surface, palette);
7679 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7680 hr = IDirectDrawSurface4_GetPalette(surface, &palette2);
7681 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
7682 ok(palette == palette2, "Got unexpected palette %p.\n", palette2);
7683 IDirectDrawPalette_Release(palette2);
7685 mipmap = surface;
7686 IDirectDrawSurface4_AddRef(mipmap);
7687 for (i = 0; i < 7; ++i)
7689 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
7690 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
7691 palette2 = (void *)0xdeadbeef;
7692 hr = IDirectDrawSurface4_GetPalette(tmp, &palette2);
7693 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x, i %u.\n", hr, i);
7694 ok(!palette2, "Got unexpected palette %p, i %u.\n", palette2, i);
7696 hr = IDirectDrawSurface4_SetPalette(tmp, palette_mipmap);
7697 ok(SUCCEEDED(hr), "Failed to set palette, i %u, hr %#x.\n", i, hr);
7699 hr = IDirectDrawSurface4_GetPalette(tmp, &palette2);
7700 ok(SUCCEEDED(hr), "Failed to get palette, i %u, hr %#x.\n", i, hr);
7701 ok(palette_mipmap == palette2, "Got unexpected palette %p.\n", palette2);
7702 IDirectDrawPalette_Release(palette2);
7704 hr = IDirectDrawSurface4_GetDC(tmp, &dc);
7705 ok(SUCCEEDED(hr), "Failed to get DC, i %u, hr %#x.\n", i, hr);
7706 count = GetDIBColorTable(dc, 1, 1, &rgbquad);
7707 ok(count == 1, "Expected count 1, got %u.\n", count);
7708 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x.\n", rgbquad.rgbRed);
7709 ok(rgbquad.rgbGreen == 0x80, "Expected rgbGreen = 0x80, got %#x.\n", rgbquad.rgbGreen);
7710 ok(rgbquad.rgbBlue == 0x0, "Expected rgbBlue = 0x0, got %#x.\n", rgbquad.rgbBlue);
7711 hr = IDirectDrawSurface4_ReleaseDC(tmp, dc);
7712 ok(SUCCEEDED(hr), "Failed to release DC, i %u, hr %#x.\n", i, hr);
7714 IDirectDrawSurface4_Release(mipmap);
7715 mipmap = tmp;
7718 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
7719 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
7720 IDirectDrawSurface4_Release(mipmap);
7721 refcount = IDirectDrawSurface4_Release(surface);
7722 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7723 refcount = IDirectDrawPalette_Release(palette_mipmap);
7724 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7725 refcount = IDirectDrawPalette_Release(palette);
7726 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7728 refcount = IDirectDraw4_Release(ddraw);
7729 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7730 DestroyWindow(window);
7733 static void test_p8_rgb_blit(void)
7735 IDirectDrawSurface4 *src, *dst;
7736 DDSURFACEDESC2 surface_desc;
7737 IDirectDraw4 *ddraw;
7738 IDirectDrawPalette *palette;
7739 ULONG refcount;
7740 HWND window;
7741 HRESULT hr;
7742 PALETTEENTRY palette_entries[256];
7743 unsigned int x;
7744 static const BYTE src_data[] = {0x10, 0x1, 0x2, 0x3, 0x4, 0x5, 0xff, 0x80};
7745 static const D3DCOLOR expected[] =
7747 0x00101010, 0x00010101, 0x00020202, 0x00030303,
7748 0x00040404, 0x00050505, 0x00ffffff, 0x00808080,
7750 D3DCOLOR color;
7752 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7753 0, 0, 640, 480, 0, 0, 0, 0);
7754 ddraw = create_ddraw();
7755 ok(!!ddraw, "Failed to create a ddraw object.\n");
7756 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7757 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7759 memset(palette_entries, 0, sizeof(palette_entries));
7760 palette_entries[1].peGreen = 0xff;
7761 palette_entries[2].peBlue = 0xff;
7762 palette_entries[3].peFlags = 0xff;
7763 palette_entries[4].peRed = 0xff;
7764 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7765 palette_entries, &palette, NULL);
7766 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7768 memset(&surface_desc, 0, sizeof(surface_desc));
7769 surface_desc.dwSize = sizeof(surface_desc);
7770 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7771 surface_desc.dwWidth = 8;
7772 surface_desc.dwHeight = 1;
7773 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7774 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7775 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
7776 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
7777 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &src, NULL);
7778 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7780 memset(&surface_desc, 0, sizeof(surface_desc));
7781 surface_desc.dwSize = sizeof(surface_desc);
7782 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7783 surface_desc.dwWidth = 8;
7784 surface_desc.dwHeight = 1;
7785 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7786 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7787 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
7788 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
7789 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
7790 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
7791 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
7792 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
7793 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &dst, NULL);
7794 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7796 memset(&surface_desc, 0, sizeof(surface_desc));
7797 surface_desc.dwSize = sizeof(surface_desc);
7798 hr = IDirectDrawSurface4_Lock(src, NULL, &surface_desc, 0, NULL);
7799 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
7800 memcpy(surface_desc.lpSurface, src_data, sizeof(src_data));
7801 hr = IDirectDrawSurface4_Unlock(src, NULL);
7802 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
7804 hr = IDirectDrawSurface4_SetPalette(src, palette);
7805 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7806 hr = IDirectDrawSurface4_Blt(dst, NULL, src, NULL, DDBLT_WAIT, NULL);
7807 /* The r500 Windows 7 driver returns E_NOTIMPL. r200 on Windows XP works.
7808 * The Geforce 7 driver on Windows Vista returns E_FAIL. Newer Nvidia GPUs work. */
7809 ok(SUCCEEDED(hr) || broken(hr == E_NOTIMPL) || broken(hr == E_FAIL),
7810 "Failed to blit, hr %#x.\n", hr);
7812 if (SUCCEEDED(hr))
7814 for (x = 0; x < sizeof(expected) / sizeof(*expected); x++)
7816 color = get_surface_color(dst, x, 0);
7817 todo_wine ok(compare_color(color, expected[x], 0),
7818 "Pixel %u: Got color %#x, expected %#x.\n",
7819 x, color, expected[x]);
7823 IDirectDrawSurface4_Release(src);
7824 IDirectDrawSurface4_Release(dst);
7825 IDirectDrawPalette_Release(palette);
7827 refcount = IDirectDraw4_Release(ddraw);
7828 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7829 DestroyWindow(window);
7832 static void test_material(void)
7834 D3DMATERIALHANDLE mat_handle, tmp;
7835 IDirect3DMaterial3 *material;
7836 IDirect3DViewport3 *viewport;
7837 IDirect3DDevice3 *device;
7838 IDirectDrawSurface4 *rt;
7839 D3DCOLOR color;
7840 ULONG refcount;
7841 unsigned int i;
7842 HWND window;
7843 HRESULT hr;
7844 BOOL valid;
7846 static struct
7848 struct vec3 position;
7849 struct vec3 normal;
7850 D3DCOLOR diffuse;
7852 quad1[] =
7854 {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
7855 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
7856 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
7857 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
7859 quad2[] =
7861 {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
7862 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
7863 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
7864 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
7866 static const struct
7868 void *data;
7869 BOOL material;
7870 D3DCOLOR expected_color;
7872 test_data[] =
7874 {quad1, TRUE, 0x0000ff00},
7875 {quad2, TRUE, 0x0000ff00},
7876 {quad1, FALSE, 0x00ffffff},
7877 {quad2, FALSE, 0x00ff0000},
7879 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
7881 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7882 0, 0, 640, 480, 0, 0, 0, 0);
7883 if (!(device = create_device(window, DDSCL_NORMAL)))
7885 skip("Failed to create a 3D device, skipping test.\n");
7886 DestroyWindow(window);
7887 return;
7890 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
7891 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
7893 viewport = create_viewport(device, 0, 0, 640, 480);
7894 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
7895 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
7897 material = create_emissive_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
7898 hr = IDirect3DMaterial3_GetHandle(material, device, &mat_handle);
7899 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
7901 hr = IDirect3DDevice3_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
7902 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
7903 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
7904 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
7905 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
7906 hr = IDirect3DDevice3_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
7907 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
7908 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
7909 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, 0);
7910 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
7911 hr = IDirect3DDevice3_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
7912 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
7913 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
7915 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
7917 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect,
7918 D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 1.0f, 0);
7919 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
7921 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, test_data[i].material ? mat_handle : 0);
7922 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
7924 hr = IDirect3DDevice3_BeginScene(device);
7925 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
7926 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP,
7927 D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE, test_data[i].data, 4, 0);
7928 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
7929 hr = IDirect3DDevice3_EndScene(device);
7930 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
7931 color = get_surface_color(rt, 320, 240);
7932 ok(compare_color(color, test_data[i].expected_color, 1),
7933 "Got unexpected color 0x%08x, test %u.\n", color, i);
7936 destroy_material(material);
7937 material = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
7938 hr = IDirect3DMaterial3_GetHandle(material, device, &mat_handle);
7939 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
7941 hr = IDirect3DViewport3_SetBackground(viewport, mat_handle);
7942 ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
7943 hr = IDirect3DViewport3_GetBackground(viewport, &tmp, &valid);
7944 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
7945 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
7946 ok(valid, "Got unexpected valid %#x.\n", valid);
7947 hr = IDirect3DViewport3_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
7948 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
7949 color = get_surface_color(rt, 320, 240);
7950 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
7952 hr = IDirect3DViewport3_SetBackground(viewport, 0);
7953 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7954 hr = IDirect3DViewport3_GetBackground(viewport, &tmp, &valid);
7955 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
7956 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
7957 ok(valid, "Got unexpected valid %#x.\n", valid);
7958 hr = IDirect3DViewport3_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
7959 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
7960 color = get_surface_color(rt, 320, 240);
7961 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
7963 destroy_viewport(device, viewport);
7964 viewport = create_viewport(device, 0, 0, 640, 480);
7966 hr = IDirect3DViewport3_GetBackground(viewport, &tmp, &valid);
7967 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
7968 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
7969 ok(!valid, "Got unexpected valid %#x.\n", valid);
7970 hr = IDirect3DViewport3_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
7971 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
7972 color = get_surface_color(rt, 320, 240);
7973 ok(compare_color(color, 0x00000000, 1), "Got unexpected color 0x%08x.\n", color);
7975 destroy_viewport(device, viewport);
7976 destroy_material(material);
7977 IDirectDrawSurface4_Release(rt);
7978 refcount = IDirect3DDevice3_Release(device);
7979 ok(!refcount, "Device has %u references left.\n", refcount);
7980 DestroyWindow(window);
7983 static void test_palette_gdi(void)
7985 IDirectDrawSurface4 *surface, *primary;
7986 DDSURFACEDESC2 surface_desc;
7987 IDirectDraw4 *ddraw;
7988 IDirectDrawPalette *palette, *palette2;
7989 ULONG refcount;
7990 HWND window;
7991 HRESULT hr;
7992 PALETTEENTRY palette_entries[256];
7993 UINT i;
7994 HDC dc;
7995 /* On the Windows 8 testbot palette index 0 of the onscreen palette is forced to
7996 * r = 0, g = 0, b = 0. Do not attempt to set it to something else as this is
7997 * not the point of this test. */
7998 static const RGBQUAD expected1[] =
8000 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
8001 {0x03, 0x00, 0x00, 0x00}, {0x15, 0x14, 0x13, 0x00},
8003 static const RGBQUAD expected2[] =
8005 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
8006 {0x03, 0x00, 0x00, 0x00}, {0x25, 0x24, 0x23, 0x00},
8008 static const RGBQUAD expected3[] =
8010 {0x00, 0x00, 0x00, 0x00}, {0x40, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x40, 0x00},
8011 {0x00, 0x40, 0x00, 0x00}, {0x56, 0x34, 0x12, 0x00},
8013 HPALETTE ddraw_palette_handle;
8014 /* Similar to index 0, index 255 is r = 0xff, g = 0xff, b = 0xff on the Win8 VMs. */
8015 RGBQUAD rgbquad[255];
8016 static const RGBQUAD rgb_zero = {0, 0, 0, 0};
8018 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8019 0, 0, 640, 480, 0, 0, 0, 0);
8020 ddraw = create_ddraw();
8021 ok(!!ddraw, "Failed to create a ddraw object.\n");
8022 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
8023 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8025 memset(&surface_desc, 0, sizeof(surface_desc));
8026 surface_desc.dwSize = sizeof(surface_desc);
8027 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
8028 surface_desc.dwWidth = 16;
8029 surface_desc.dwHeight = 16;
8030 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
8031 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
8032 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
8033 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
8034 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8035 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8037 /* Avoid colors from the Windows default palette. */
8038 memset(palette_entries, 0, sizeof(palette_entries));
8039 palette_entries[1].peRed = 0x01;
8040 palette_entries[2].peGreen = 0x02;
8041 palette_entries[3].peBlue = 0x03;
8042 palette_entries[4].peRed = 0x13;
8043 palette_entries[4].peGreen = 0x14;
8044 palette_entries[4].peBlue = 0x15;
8045 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
8046 palette_entries, &palette, NULL);
8047 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
8049 /* If there is no palette assigned and the display mode is not 8 bpp, some
8050 * drivers refuse to create a DC while others allow it. If a DC is created,
8051 * the DIB color table is uninitialized and contains random colors. No error
8052 * is generated when trying to read pixels and random garbage is returned.
8054 * The most likely explanation is that if the driver creates a DC, it (or
8055 * the higher-level runtime) uses GetSystemPaletteEntries to find the
8056 * palette, but GetSystemPaletteEntries fails when bpp > 8 and the palette
8057 * contains uninitialized garbage. See comments below for the P8 case. */
8059 hr = IDirectDrawSurface4_SetPalette(surface, palette);
8060 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
8061 hr = IDirectDrawSurface4_GetDC(surface, &dc);
8062 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
8063 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
8064 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE),
8065 "Got unexpected palette %p, expected %p.\n",
8066 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
8068 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
8069 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
8070 for (i = 0; i < sizeof(expected1) / sizeof(*expected1); i++)
8072 ok(!memcmp(&rgbquad[i], &expected1[i], sizeof(rgbquad[i])),
8073 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8074 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
8075 expected1[i].rgbRed, expected1[i].rgbGreen, expected1[i].rgbBlue);
8077 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
8079 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
8080 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
8081 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
8084 /* Update the palette while the DC is in use. This does not modify the DC. */
8085 palette_entries[4].peRed = 0x23;
8086 palette_entries[4].peGreen = 0x24;
8087 palette_entries[4].peBlue = 0x25;
8088 hr = IDirectDrawPalette_SetEntries(palette, 0, 4, 1, &palette_entries[4]);
8089 ok(SUCCEEDED(hr), "Failed to set palette entries, hr %#x.\n", hr);
8091 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
8092 ok(i == 1, "Expected count 1, got %u.\n", i);
8093 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
8094 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8095 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
8096 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
8098 /* Neither does re-setting the palette. */
8099 hr = IDirectDrawSurface4_SetPalette(surface, NULL);
8100 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
8101 hr = IDirectDrawSurface4_SetPalette(surface, palette);
8102 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
8104 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
8105 ok(i == 1, "Expected count 1, got %u.\n", i);
8106 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
8107 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8108 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
8109 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
8111 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
8112 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8114 /* Refresh the DC. This updates the palette. */
8115 hr = IDirectDrawSurface4_GetDC(surface, &dc);
8116 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
8117 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
8118 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
8119 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
8121 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
8122 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8123 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
8124 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
8126 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
8128 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
8129 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
8130 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
8132 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
8133 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8135 refcount = IDirectDrawSurface4_Release(surface);
8136 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8138 if (FAILED(IDirectDraw4_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
8140 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
8141 IDirectDrawPalette_Release(palette);
8142 IDirectDraw4_Release(ddraw);
8143 DestroyWindow(window);
8144 return;
8146 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
8147 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
8148 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8150 memset(&surface_desc, 0, sizeof(surface_desc));
8151 surface_desc.dwSize = sizeof(surface_desc);
8152 surface_desc.dwFlags = DDSD_CAPS;
8153 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
8154 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &primary, NULL);
8155 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8157 hr = IDirectDrawSurface4_SetPalette(primary, palette);
8158 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
8159 hr = IDirectDrawSurface4_GetDC(primary, &dc);
8160 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
8161 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
8162 /* Windows 2000 on the testbot assigns a different palette to the primary. Refrast? */
8163 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE) || broken(TRUE),
8164 "Got unexpected palette %p, expected %p.\n",
8165 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
8166 SelectPalette(dc, ddraw_palette_handle, FALSE);
8168 /* The primary uses the system palette. In exclusive mode, the system palette matches
8169 * the ddraw palette attached to the primary, so the result is what you would expect
8170 * from a regular surface. Tests for the interaction between the ddraw palette and
8171 * the system palette are not included pending an application that depends on this.
8172 * The relation between those causes problems on Windows Vista and newer for games
8173 * like Age of Empires or StarCraft. Don't emulate it without a real need. */
8174 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
8175 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
8176 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
8178 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
8179 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8180 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
8181 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
8183 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
8185 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
8186 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
8187 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
8189 hr = IDirectDrawSurface4_ReleaseDC(primary, dc);
8190 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8192 memset(&surface_desc, 0, sizeof(surface_desc));
8193 surface_desc.dwSize = sizeof(surface_desc);
8194 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
8195 surface_desc.dwWidth = 16;
8196 surface_desc.dwHeight = 16;
8197 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
8198 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8199 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8201 /* Here the offscreen surface appears to use the primary's palette,
8202 * but in all likelihood it is actually the system palette. */
8203 hr = IDirectDrawSurface4_GetDC(surface, &dc);
8204 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
8205 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
8206 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
8207 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
8209 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
8210 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8211 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
8212 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
8214 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
8216 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
8217 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
8218 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
8220 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
8221 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8223 /* On real hardware a change to the primary surface's palette applies immediately,
8224 * even on device contexts from offscreen surfaces that do not have their own
8225 * palette. On the testbot VMs this is not the case. Don't test this until we
8226 * know of an application that depends on this. */
8228 memset(palette_entries, 0, sizeof(palette_entries));
8229 palette_entries[1].peBlue = 0x40;
8230 palette_entries[2].peRed = 0x40;
8231 palette_entries[3].peGreen = 0x40;
8232 palette_entries[4].peRed = 0x12;
8233 palette_entries[4].peGreen = 0x34;
8234 palette_entries[4].peBlue = 0x56;
8235 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
8236 palette_entries, &palette2, NULL);
8237 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
8238 hr = IDirectDrawSurface4_SetPalette(surface, palette2);
8239 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
8241 /* A palette assigned to the offscreen surface overrides the primary / system
8242 * palette. */
8243 hr = IDirectDrawSurface4_GetDC(surface, &dc);
8244 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
8245 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
8246 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
8247 for (i = 0; i < sizeof(expected3) / sizeof(*expected3); i++)
8249 ok(!memcmp(&rgbquad[i], &expected3[i], sizeof(rgbquad[i])),
8250 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8251 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
8252 expected3[i].rgbRed, expected3[i].rgbGreen, expected3[i].rgbBlue);
8254 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
8256 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
8257 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
8258 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
8260 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
8261 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8263 refcount = IDirectDrawSurface4_Release(surface);
8264 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8266 /* The Windows 8 testbot keeps extra references to the primary and
8267 * backbuffer while in 8 bpp mode. */
8268 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
8269 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
8271 refcount = IDirectDrawSurface4_Release(primary);
8272 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8273 refcount = IDirectDrawPalette_Release(palette2);
8274 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8275 refcount = IDirectDrawPalette_Release(palette);
8276 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8277 refcount = IDirectDraw4_Release(ddraw);
8278 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8279 DestroyWindow(window);
8282 static void test_palette_alpha(void)
8284 IDirectDrawSurface4 *surface;
8285 DDSURFACEDESC2 surface_desc;
8286 IDirectDraw4 *ddraw;
8287 IDirectDrawPalette *palette;
8288 ULONG refcount;
8289 HWND window;
8290 HRESULT hr;
8291 PALETTEENTRY palette_entries[256];
8292 unsigned int i;
8293 static const struct
8295 DWORD caps, flags;
8296 BOOL attach_allowed;
8297 const char *name;
8299 test_data[] =
8301 {DDSCAPS_OFFSCREENPLAIN, DDSD_WIDTH | DDSD_HEIGHT, FALSE, "offscreenplain"},
8302 {DDSCAPS_TEXTURE, DDSD_WIDTH | DDSD_HEIGHT, TRUE, "texture"},
8303 {DDSCAPS_PRIMARYSURFACE, 0, FALSE, "primary"}
8306 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8307 0, 0, 640, 480, 0, 0, 0, 0);
8308 ddraw = create_ddraw();
8309 ok(!!ddraw, "Failed to create a ddraw object.\n");
8310 if (FAILED(IDirectDraw4_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
8312 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
8313 IDirectDraw4_Release(ddraw);
8314 DestroyWindow(window);
8315 return;
8317 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
8318 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8320 memset(palette_entries, 0, sizeof(palette_entries));
8321 palette_entries[1].peFlags = 0x42;
8322 palette_entries[2].peFlags = 0xff;
8323 palette_entries[3].peFlags = 0x80;
8324 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
8325 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
8327 memset(palette_entries, 0x66, sizeof(palette_entries));
8328 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
8329 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
8330 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
8331 palette_entries[0].peFlags);
8332 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
8333 palette_entries[1].peFlags);
8334 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
8335 palette_entries[2].peFlags);
8336 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
8337 palette_entries[3].peFlags);
8339 IDirectDrawPalette_Release(palette);
8341 memset(palette_entries, 0, sizeof(palette_entries));
8342 palette_entries[1].peFlags = 0x42;
8343 palette_entries[1].peRed = 0xff;
8344 palette_entries[2].peFlags = 0xff;
8345 palette_entries[3].peFlags = 0x80;
8346 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT | DDPCAPS_ALPHA,
8347 palette_entries, &palette, NULL);
8348 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
8350 memset(palette_entries, 0x66, sizeof(palette_entries));
8351 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
8352 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
8353 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
8354 palette_entries[0].peFlags);
8355 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
8356 palette_entries[1].peFlags);
8357 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
8358 palette_entries[2].peFlags);
8359 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
8360 palette_entries[3].peFlags);
8362 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); i++)
8364 memset(&surface_desc, 0, sizeof(surface_desc));
8365 surface_desc.dwSize = sizeof(surface_desc);
8366 surface_desc.dwFlags = DDSD_CAPS | test_data[i].flags;
8367 surface_desc.dwWidth = 128;
8368 surface_desc.dwHeight = 128;
8369 surface_desc.ddsCaps.dwCaps = test_data[i].caps;
8370 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8371 ok(SUCCEEDED(hr), "Failed to create %s surface, hr %#x.\n", test_data[i].name, hr);
8373 hr = IDirectDrawSurface4_SetPalette(surface, palette);
8374 if (test_data[i].attach_allowed)
8375 ok(SUCCEEDED(hr), "Failed to attach palette to %s surface, hr %#x.\n", test_data[i].name, hr);
8376 else
8377 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x, %s surface.\n", hr, test_data[i].name);
8379 if (SUCCEEDED(hr))
8381 HDC dc;
8382 RGBQUAD rgbquad;
8383 UINT retval;
8385 hr = IDirectDrawSurface4_GetDC(surface, &dc);
8386 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x, %s surface.\n", hr, test_data[i].name);
8387 retval = GetDIBColorTable(dc, 1, 1, &rgbquad);
8388 ok(retval == 1, "GetDIBColorTable returned unexpected result %u.\n", retval);
8389 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x, %s surface.\n",
8390 rgbquad.rgbRed, test_data[i].name);
8391 ok(rgbquad.rgbGreen == 0, "Expected rgbGreen = 0, got %#x, %s surface.\n",
8392 rgbquad.rgbGreen, test_data[i].name);
8393 ok(rgbquad.rgbBlue == 0, "Expected rgbBlue = 0, got %#x, %s surface.\n",
8394 rgbquad.rgbBlue, test_data[i].name);
8395 todo_wine ok(rgbquad.rgbReserved == 0, "Expected rgbReserved = 0, got %u, %s surface.\n",
8396 rgbquad.rgbReserved, test_data[i].name);
8397 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
8398 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8400 IDirectDrawSurface4_Release(surface);
8403 /* Test INVALIDSURFACETYPE vs INVALIDPIXELFORMAT. */
8404 memset(&surface_desc, 0, sizeof(surface_desc));
8405 surface_desc.dwSize = sizeof(surface_desc);
8406 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
8407 surface_desc.dwWidth = 128;
8408 surface_desc.dwHeight = 128;
8409 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
8410 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
8411 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
8412 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
8413 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
8414 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
8415 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
8416 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8417 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8418 hr = IDirectDrawSurface4_SetPalette(surface, palette);
8419 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x.\n", hr);
8420 IDirectDrawSurface4_Release(surface);
8422 /* The Windows 8 testbot keeps extra references to the primary
8423 * while in 8 bpp mode. */
8424 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
8425 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
8427 refcount = IDirectDrawPalette_Release(palette);
8428 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8429 refcount = IDirectDraw4_Release(ddraw);
8430 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8431 DestroyWindow(window);
8434 static void test_vb_writeonly(void)
8436 IDirect3DDevice3 *device;
8437 IDirect3D3 *d3d;
8438 IDirect3DVertexBuffer *buffer;
8439 HWND window;
8440 HRESULT hr;
8441 D3DVERTEXBUFFERDESC desc;
8442 void *ptr;
8443 static const struct vec4 quad[] =
8445 { 0.0f, 480.0f, 0.0f, 1.0f},
8446 { 0.0f, 0.0f, 0.0f, 1.0f},
8447 {640.0f, 480.0f, 0.0f, 1.0f},
8448 {640.0f, 0.0f, 0.0f, 1.0f},
8451 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8452 0, 0, 640, 480, 0, 0, 0, 0);
8454 if (!(device = create_device(window, DDSCL_NORMAL)))
8456 skip("Failed to create a 3D device, skipping test.\n");
8457 DestroyWindow(window);
8458 return;
8461 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
8462 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
8464 memset(&desc, 0, sizeof(desc));
8465 desc.dwSize = sizeof(desc);
8466 desc.dwCaps = D3DVBCAPS_WRITEONLY;
8467 desc.dwFVF = D3DFVF_XYZRHW;
8468 desc.dwNumVertices = sizeof(quad) / sizeof(*quad);
8469 hr = IDirect3D3_CreateVertexBuffer(d3d, &desc, &buffer, 0, NULL);
8470 ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr);
8472 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, &ptr, NULL);
8473 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
8474 memcpy(ptr, quad, sizeof(quad));
8475 hr = IDirect3DVertexBuffer_Unlock(buffer);
8476 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
8478 hr = IDirect3DDevice3_BeginScene(device);
8479 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
8480 hr = IDirect3DDevice3_DrawPrimitiveVB(device, D3DPT_TRIANGLESTRIP, buffer, 0, 4, 0);
8481 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8482 hr = IDirect3DDevice3_EndScene(device);
8483 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
8485 hr = IDirect3DVertexBuffer_Lock(buffer, 0, &ptr, NULL);
8486 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
8487 ok (!memcmp(ptr, quad, sizeof(quad)), "Got unexpected vertex buffer data.\n");
8488 hr = IDirect3DVertexBuffer_Unlock(buffer);
8489 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
8491 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_READONLY, &ptr, NULL);
8492 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
8493 ok (!memcmp(ptr, quad, sizeof(quad)), "Got unexpected vertex buffer data.\n");
8494 hr = IDirect3DVertexBuffer_Unlock(buffer);
8495 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
8497 IDirect3DVertexBuffer_Release(buffer);
8498 IDirect3D3_Release(d3d);
8499 IDirect3DDevice3_Release(device);
8500 DestroyWindow(window);
8503 static void test_lost_device(void)
8505 IDirectDrawSurface4 *surface;
8506 DDSURFACEDESC2 surface_desc;
8507 HWND window1, window2;
8508 IDirectDraw4 *ddraw;
8509 ULONG refcount;
8510 HRESULT hr;
8511 BOOL ret;
8513 window1 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8514 0, 0, 640, 480, 0, 0, 0, 0);
8515 window2 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8516 0, 0, 640, 480, 0, 0, 0, 0);
8517 ddraw = create_ddraw();
8518 ok(!!ddraw, "Failed to create a ddraw object.\n");
8519 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
8520 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8522 memset(&surface_desc, 0, sizeof(surface_desc));
8523 surface_desc.dwSize = sizeof(surface_desc);
8524 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
8525 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
8526 U5(surface_desc).dwBackBufferCount = 1;
8527 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8528 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8530 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8531 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8532 hr = IDirectDrawSurface4_IsLost(surface);
8533 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8534 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8535 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8537 ret = SetForegroundWindow(GetDesktopWindow());
8538 ok(ret, "Failed to set foreground window.\n");
8539 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8540 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
8541 hr = IDirectDrawSurface4_IsLost(surface);
8542 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8543 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8544 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8546 ret = SetForegroundWindow(window1);
8547 ok(ret, "Failed to set foreground window.\n");
8548 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8549 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8550 hr = IDirectDrawSurface4_IsLost(surface);
8551 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8552 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8553 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8555 hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
8556 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8557 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8558 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8559 hr = IDirectDrawSurface4_IsLost(surface);
8560 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8561 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8562 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8564 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
8565 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8566 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8567 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8568 hr = IDirectDrawSurface4_IsLost(surface);
8569 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8570 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8571 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8573 /* Trying to restore the primary will crash, probably because flippable
8574 * surfaces can't exist in DDSCL_NORMAL. */
8575 IDirectDrawSurface4_Release(surface);
8576 memset(&surface_desc, 0, sizeof(surface_desc));
8577 surface_desc.dwSize = sizeof(surface_desc);
8578 surface_desc.dwFlags = DDSD_CAPS;
8579 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
8580 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8581 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8583 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8584 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8585 hr = IDirectDrawSurface4_IsLost(surface);
8586 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8588 ret = SetForegroundWindow(GetDesktopWindow());
8589 ok(ret, "Failed to set foreground window.\n");
8590 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8591 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8592 hr = IDirectDrawSurface4_IsLost(surface);
8593 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8595 ret = SetForegroundWindow(window1);
8596 ok(ret, "Failed to set foreground window.\n");
8597 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8598 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8599 hr = IDirectDrawSurface4_IsLost(surface);
8600 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8602 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
8603 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8604 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8605 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8606 hr = IDirectDrawSurface4_IsLost(surface);
8607 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8609 hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
8610 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8611 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8612 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8613 hr = IDirectDrawSurface4_IsLost(surface);
8614 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8616 IDirectDrawSurface4_Release(surface);
8617 memset(&surface_desc, 0, sizeof(surface_desc));
8618 surface_desc.dwSize = sizeof(surface_desc);
8619 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
8620 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
8621 U5(surface_desc).dwBackBufferCount = 1;
8622 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8623 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8625 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
8626 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8627 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8628 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8629 hr = IDirectDrawSurface4_IsLost(surface);
8630 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8631 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8632 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8634 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL | DDSCL_FULLSCREEN);
8635 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8636 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8637 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8638 hr = IDirectDrawSurface4_IsLost(surface);
8639 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8640 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8641 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
8643 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
8644 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8645 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8646 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8647 hr = IDirectDrawSurface4_IsLost(surface);
8648 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8649 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8650 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
8652 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
8653 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8654 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8655 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8656 hr = IDirectDrawSurface4_IsLost(surface);
8657 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8658 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8659 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
8661 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL | DDSCL_FULLSCREEN);
8662 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8663 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8664 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8665 hr = IDirectDrawSurface4_IsLost(surface);
8666 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8667 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8668 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
8670 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
8671 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8672 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8673 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8674 hr = IDirectDrawSurface4_IsLost(surface);
8675 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8676 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8677 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8679 IDirectDrawSurface4_Release(surface);
8680 refcount = IDirectDraw4_Release(ddraw);
8681 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8682 DestroyWindow(window2);
8683 DestroyWindow(window1);
8686 static void test_surface_desc_lock(void)
8688 IDirectDrawSurface4 *surface;
8689 DDSURFACEDESC2 surface_desc;
8690 IDirectDraw4 *ddraw;
8691 ULONG refcount;
8692 HWND window;
8693 HRESULT hr;
8695 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8696 0, 0, 640, 480, 0, 0, 0, 0);
8697 ddraw = create_ddraw();
8698 ok(!!ddraw, "Failed to create a ddraw object.\n");
8699 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
8700 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8702 memset(&surface_desc, 0, sizeof(surface_desc));
8703 surface_desc.dwSize = sizeof(surface_desc);
8704 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
8705 surface_desc.dwWidth = 16;
8706 surface_desc.dwHeight = 16;
8707 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
8708 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8709 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8711 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8712 surface_desc.dwSize = sizeof(surface_desc);
8713 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
8714 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8715 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8717 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8718 surface_desc.dwSize = sizeof(surface_desc);
8719 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, 0, NULL);
8720 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
8721 ok(surface_desc.lpSurface != NULL, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8722 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8723 surface_desc.dwSize = sizeof(surface_desc);
8724 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
8725 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8726 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8727 hr = IDirectDrawSurface4_Unlock(surface, NULL);
8728 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
8730 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8731 surface_desc.dwSize = sizeof(surface_desc);
8732 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
8733 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8734 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8736 IDirectDrawSurface4_Release(surface);
8737 refcount = IDirectDraw4_Release(ddraw);
8738 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8739 DestroyWindow(window);
8742 static void test_signed_formats(void)
8744 HRESULT hr;
8745 IDirect3DDevice3 *device;
8746 IDirect3D3 *d3d;
8747 IDirectDraw4 *ddraw;
8748 IDirectDrawSurface4 *surface, *rt;
8749 IDirect3DTexture2 *texture;
8750 IDirect3DViewport3 *viewport;
8751 DDSURFACEDESC2 surface_desc;
8752 ULONG refcount;
8753 HWND window;
8754 D3DCOLOR color, expected_color;
8755 D3DRECT clear_rect;
8756 static struct
8758 struct vec3 position;
8759 struct vec2 texcoord;
8761 quad[] =
8763 {{-1.0f, -1.0f, 0.0f}, {0.0f, 1.0f}},
8764 {{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
8765 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 1.0f}},
8766 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
8768 /* See test_signed_formats() in dlls/d3d9/tests/visual.c for an explanation
8769 * of these values. */
8770 static const USHORT content_v8u8[4][4] =
8772 {0x0000, 0x7f7f, 0x8880, 0x0000},
8773 {0x0080, 0x8000, 0x7f00, 0x007f},
8774 {0x193b, 0xe8c8, 0x0808, 0xf8f8},
8775 {0x4444, 0xc0c0, 0xa066, 0x22e0},
8777 static const DWORD content_x8l8v8u8[4][4] =
8779 {0x00000000, 0x00ff7f7f, 0x00008880, 0x00ff0000},
8780 {0x00000080, 0x00008000, 0x00007f00, 0x0000007f},
8781 {0x0041193b, 0x0051e8c8, 0x00040808, 0x00fff8f8},
8782 {0x00824444, 0x0000c0c0, 0x00c2a066, 0x009222e0},
8784 static const USHORT content_l6v5u5[4][4] =
8786 {0x0000, 0xfdef, 0x0230, 0xfc00},
8787 {0x0010, 0x0200, 0x01e0, 0x000f},
8788 {0x4067, 0x53b9, 0x0421, 0xffff},
8789 {0x8108, 0x0318, 0xc28c, 0x909c},
8791 static const struct
8793 const char *name;
8794 const void *content;
8795 SIZE_T pixel_size;
8796 BOOL blue;
8797 unsigned int slop, slop_broken;
8798 DDPIXELFORMAT format;
8800 formats[] =
8803 "D3DFMT_V8U8", content_v8u8, sizeof(WORD), FALSE, 1, 0,
8805 sizeof(DDPIXELFORMAT), DDPF_BUMPDUDV, 0,
8806 {16}, {0x000000ff}, {0x0000ff00}, {0x00000000}, {0x00000000}
8810 "D3DFMT_X8L8V8U8", content_x8l8v8u8, sizeof(DWORD), TRUE, 1, 0,
8812 sizeof(DDPIXELFORMAT), DDPF_BUMPDUDV | DDPF_BUMPLUMINANCE, 0,
8813 {32}, {0x000000ff}, {0x0000ff00}, {0x00ff0000}, {0x00000000}
8817 "D3DFMT_L6V5U5", content_l6v5u5, sizeof(WORD), TRUE, 4, 7,
8819 sizeof(DDPIXELFORMAT), DDPF_BUMPDUDV | DDPF_BUMPLUMINANCE, 0,
8820 {16}, {0x0000001f}, {0x000003e0}, {0x0000fc00}, {0x00000000}
8824 /* No V16U16 or Q8W8V8U8 support in ddraw. */
8826 static const D3DCOLOR expected_colors[4][4] =
8828 {0x00808080, 0x00fefeff, 0x00010780, 0x008080ff},
8829 {0x00018080, 0x00800180, 0x0080fe80, 0x00fe8080},
8830 {0x00ba98a0, 0x004767a8, 0x00888881, 0x007878ff},
8831 {0x00c3c3c0, 0x003f3f80, 0x00e51fe1, 0x005fa2c8},
8833 unsigned int i, width, x, y;
8834 D3DDEVICEDESC device_desc, hel_desc;
8836 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8837 0, 0, 640, 480, 0, 0, 0, 0);
8839 if (!(device = create_device(window, DDSCL_NORMAL)))
8841 skip("Failed to create a 3D device, skipping test.\n");
8842 DestroyWindow(window);
8843 return;
8846 memset(&device_desc, 0, sizeof(device_desc));
8847 device_desc.dwSize = sizeof(device_desc);
8848 memset(&hel_desc, 0, sizeof(hel_desc));
8849 hel_desc.dwSize = sizeof(hel_desc);
8850 hr = IDirect3DDevice3_GetCaps(device, &device_desc, &hel_desc);
8851 ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
8852 if (!(device_desc.dwTextureOpCaps & D3DTEXOPCAPS_BLENDFACTORALPHA))
8854 skip("D3DTOP_BLENDFACTORALPHA not supported, skipping bumpmap format tests.\n");
8855 goto done;
8858 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
8859 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
8860 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
8861 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
8862 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
8863 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
8865 memset(&surface_desc, 0, sizeof(surface_desc));
8866 surface_desc.dwSize = sizeof(surface_desc);
8867 hr = IDirectDrawSurface4_GetSurfaceDesc(rt, &surface_desc);
8868 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8869 viewport = create_viewport(device, 0, 0, surface_desc.dwWidth, surface_desc.dwHeight);
8870 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
8871 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
8872 U1(clear_rect).x1 = 0;
8873 U2(clear_rect).y1 = 0;
8874 U3(clear_rect).x2 = surface_desc.dwWidth;
8875 U4(clear_rect).y2 = surface_desc.dwHeight;
8877 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
8878 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8880 /* dst = tex * 0.5 + 1.0 * (1.0 - 0.5) = tex * 0.5 + 0.5 */
8881 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_TEXTUREFACTOR, 0x80ffffff);
8882 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8883 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_BLENDFACTORALPHA);
8884 ok(SUCCEEDED(hr), "Failed to set texture stage state, hr %#x.\n", hr);
8885 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
8886 ok(SUCCEEDED(hr), "Failed to set texture stage state, hr %#x.\n", hr);
8887 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
8888 ok(SUCCEEDED(hr), "Failed to set texture stage state, hr %#x.\n", hr);
8890 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
8892 for (width = 1; width < 5; width += 3)
8894 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0x00000000, 0.0f, 0);
8895 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
8897 memset(&surface_desc, 0, sizeof(surface_desc));
8898 surface_desc.dwSize = sizeof(surface_desc);
8899 surface_desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
8900 surface_desc.dwWidth = width;
8901 surface_desc.dwHeight = 4;
8902 U4(surface_desc).ddpfPixelFormat = formats[i].format;
8903 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
8904 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8905 if (FAILED(hr))
8907 skip("%s textures not supported, skipping.\n", formats[i].name);
8908 continue;
8910 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, format %s.\n", hr, formats[i].name);
8912 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
8913 ok(SUCCEEDED(hr), "Failed to get Direct3DTexture2 interface, hr %#x, format %s.\n",
8914 hr, formats[i].name);
8915 hr = IDirect3DDevice3_SetTexture(device, 0, texture);
8916 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x, format %s.\n", hr, formats[i].name);
8917 IDirect3DTexture2_Release(texture);
8919 memset(&surface_desc, 0, sizeof(surface_desc));
8920 surface_desc.dwSize = sizeof(surface_desc);
8921 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, 0, NULL);
8922 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, format %s.\n", hr, formats[i].name);
8923 for (y = 0; y < 4; y++)
8925 memcpy((char *)surface_desc.lpSurface + y * U1(surface_desc).lPitch,
8926 (char *)formats[i].content + y * 4 * formats[i].pixel_size,
8927 width * formats[i].pixel_size);
8929 hr = IDirectDrawSurface4_Unlock(surface, NULL);
8930 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, format %s.\n", hr, formats[i].name);
8932 hr = IDirect3DDevice3_BeginScene(device);
8933 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
8934 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP,
8935 D3DFVF_XYZ | D3DFVF_TEX1, quad, 4, 0);
8936 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8937 hr = IDirect3DDevice3_EndScene(device);
8938 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
8940 for (y = 0; y < 4; y++)
8942 for (x = 0; x < width; x++)
8944 expected_color = expected_colors[y][x];
8945 if (!formats[i].blue)
8946 expected_color |= 0x000000ff;
8948 color = get_surface_color(rt, 80 + 160 * x, 60 + 120 * y);
8949 ok(compare_color(color, expected_color, formats[i].slop)
8950 || broken(compare_color(color, expected_color, formats[i].slop_broken)),
8951 "Expected color 0x%08x, got 0x%08x, format %s, location %ux%u.\n",
8952 expected_color, color, formats[i].name, x, y);
8956 IDirectDrawSurface4_Release(surface);
8960 destroy_viewport(device, viewport);
8961 IDirectDrawSurface4_Release(rt);
8962 IDirectDraw4_Release(ddraw);
8963 IDirect3D3_Release(d3d);
8965 done:
8966 refcount = IDirect3DDevice3_Release(device);
8967 ok(!refcount, "Device has %u references left.\n", refcount);
8968 DestroyWindow(window);
8971 static void test_color_fill(void)
8973 HRESULT hr;
8974 IDirect3DDevice3 *device;
8975 IDirect3D3 *d3d;
8976 IDirectDraw4 *ddraw;
8977 IDirectDrawSurface4 *surface, *surface2;
8978 DDSURFACEDESC2 surface_desc;
8979 DDPIXELFORMAT z_fmt;
8980 ULONG refcount;
8981 HWND window;
8982 unsigned int i;
8983 DDBLTFX fx;
8984 RECT rect = {5, 5, 7, 7};
8985 DWORD *color;
8986 DWORD supported_fmts = 0, num_fourcc_codes, *fourcc_codes;
8987 DDCAPS hal_caps;
8988 static const struct
8990 DWORD caps, caps2;
8991 HRESULT colorfill_hr, depthfill_hr;
8992 BOOL rop_success;
8993 const char *name;
8994 DWORD result;
8995 BOOL check_result;
8996 DDPIXELFORMAT format;
8998 tests[] =
9001 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0,
9002 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem offscreenplain RGB", 0xdeadbeef, TRUE,
9004 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9005 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
9009 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, 0,
9010 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem offscreenplain RGB", 0xdeadbeef, TRUE,
9012 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9013 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
9017 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, 0,
9018 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem texture RGB", 0xdeadbeef, TRUE,
9020 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9021 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
9025 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0,
9026 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem texture RGB", 0xdeadbeef, TRUE,
9028 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9029 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
9033 DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE,
9034 DD_OK, DDERR_INVALIDPARAMS, TRUE, "managed texture RGB", 0xdeadbeef, TRUE,
9036 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9037 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
9041 DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY, 0,
9042 DDERR_INVALIDPARAMS, DD_OK, TRUE, "vidmem zbuffer", 0, FALSE,
9043 {0, 0, 0, {0}, {0}, {0}, {0}, {0}}
9046 DDSCAPS_ZBUFFER | DDSCAPS_SYSTEMMEMORY, 0,
9047 DDERR_INVALIDPARAMS, DD_OK, TRUE, "sysmem zbuffer", 0, FALSE,
9048 {0, 0, 0, {0}, {0}, {0}, {0}, {0}}
9051 /* Colorfill on YUV surfaces always returns DD_OK, but the content is
9052 * different afterwards. DX9+ GPUs set one of the two luminance values
9053 * in each block, but AMD and Nvidia GPUs disagree on which luminance
9054 * value they set. r200 (dx8) just sets the entire block to the clear
9055 * value. */
9056 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0,
9057 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem offscreenplain YUY2", 0, FALSE,
9059 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'),
9060 {0}, {0}, {0}, {0}, {0}
9064 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0,
9065 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem offscreenplain UYVY", 0, FALSE,
9067 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'),
9068 {0}, {0}, {0}, {0}, {0}
9072 DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY, 0,
9073 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem overlay YUY2", 0, FALSE,
9075 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'),
9076 {0}, {0}, {0}, {0}, {0}
9080 DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY, 0,
9081 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem overlay UYVY", 0, FALSE,
9083 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'),
9084 {0}, {0}, {0}, {0}, {0}
9088 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, 0,
9089 E_NOTIMPL, DDERR_INVALIDPARAMS, FALSE, "vidmem texture DXT1", 0, FALSE,
9091 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '1'),
9092 {0}, {0}, {0}, {0}, {0}
9096 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0,
9097 E_NOTIMPL, DDERR_INVALIDPARAMS, FALSE, "sysmem texture DXT1", 0, FALSE,
9099 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '1'),
9100 {0}, {0}, {0}, {0}, {0}
9104 /* The testbot fills this with 0x00 instead of the blue channel. The sysmem
9105 * surface works, presumably because it is handled by the runtime instead of
9106 * the driver. */
9107 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0,
9108 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem offscreenplain P8", 0xefefefef, FALSE,
9110 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_PALETTEINDEXED8, 0,
9111 {8}, {0}, {0}, {0}, {0}
9115 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, 0,
9116 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem offscreenplain P8", 0xefefefef, TRUE,
9118 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_PALETTEINDEXED8, 0,
9119 {8}, {0}, {0}, {0}, {0}
9123 static const struct
9125 DWORD rop;
9126 const char *name;
9127 HRESULT hr;
9129 rops[] =
9131 {SRCCOPY, "SRCCOPY", DD_OK},
9132 {SRCPAINT, "SRCPAINT", DDERR_NORASTEROPHW},
9133 {SRCAND, "SRCAND", DDERR_NORASTEROPHW},
9134 {SRCINVERT, "SRCINVERT", DDERR_NORASTEROPHW},
9135 {SRCERASE, "SRCERASE", DDERR_NORASTEROPHW},
9136 {NOTSRCCOPY, "NOTSRCCOPY", DDERR_NORASTEROPHW},
9137 {NOTSRCERASE, "NOTSRCERASE", DDERR_NORASTEROPHW},
9138 {MERGECOPY, "MERGECOPY", DDERR_NORASTEROPHW},
9139 {MERGEPAINT, "MERGEPAINT", DDERR_NORASTEROPHW},
9140 {PATCOPY, "PATCOPY", DDERR_NORASTEROPHW},
9141 {PATPAINT, "PATPAINT", DDERR_NORASTEROPHW},
9142 {PATINVERT, "PATINVERT", DDERR_NORASTEROPHW},
9143 {DSTINVERT, "DSTINVERT", DDERR_NORASTEROPHW},
9144 {BLACKNESS, "BLACKNESS", DD_OK},
9145 {WHITENESS, "WHITENESS", DD_OK},
9146 {0xaa0029, "0xaa0029", DDERR_NORASTEROPHW} /* noop */
9149 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
9150 0, 0, 640, 480, 0, 0, 0, 0);
9152 if (!(device = create_device(window, DDSCL_NORMAL)))
9154 skip("Failed to create a 3D device, skipping test.\n");
9155 DestroyWindow(window);
9156 return;
9159 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
9160 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
9161 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
9162 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
9164 memset(&z_fmt, 0, sizeof(z_fmt));
9165 IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
9166 if (!z_fmt.dwSize)
9167 skip("No Z buffer formats supported, skipping Z buffer colorfill test.\n");
9169 IDirect3DDevice3_EnumTextureFormats(device, test_block_formats_creation_cb, &supported_fmts);
9170 if (!(supported_fmts & SUPPORT_DXT1))
9171 skip("DXT1 textures not supported, skipping DXT1 colorfill test.\n");
9173 IDirect3D3_Release(d3d);
9175 hr = IDirectDraw4_GetFourCCCodes(ddraw, &num_fourcc_codes, NULL);
9176 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
9177 fourcc_codes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
9178 num_fourcc_codes * sizeof(*fourcc_codes));
9179 if (!fourcc_codes)
9180 goto done;
9181 hr = IDirectDraw4_GetFourCCCodes(ddraw, &num_fourcc_codes, fourcc_codes);
9182 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
9183 for (i = 0; i < num_fourcc_codes; i++)
9185 if (fourcc_codes[i] == MAKEFOURCC('Y', 'U', 'Y', '2'))
9186 supported_fmts |= SUPPORT_YUY2;
9187 else if (fourcc_codes[i] == MAKEFOURCC('U', 'Y', 'V', 'Y'))
9188 supported_fmts |= SUPPORT_UYVY;
9190 HeapFree(GetProcessHeap(), 0, fourcc_codes);
9192 memset(&hal_caps, 0, sizeof(hal_caps));
9193 hal_caps.dwSize = sizeof(hal_caps);
9194 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
9195 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
9197 if (!(supported_fmts & (SUPPORT_YUY2 | SUPPORT_UYVY)) || !(hal_caps.dwCaps & DDCAPS_OVERLAY))
9198 skip("Overlays or some YUV formats not supported, skipping YUV colorfill tests.\n");
9200 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
9202 /* Some Windows drivers modify dwFillColor when it is used on P8 or FourCC formats. */
9203 memset(&fx, 0, sizeof(fx));
9204 fx.dwSize = sizeof(fx);
9205 U5(fx).dwFillColor = 0xdeadbeef;
9207 memset(&surface_desc, 0, sizeof(surface_desc));
9208 surface_desc.dwSize = sizeof(surface_desc);
9209 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9210 surface_desc.dwWidth = 64;
9211 surface_desc.dwHeight = 64;
9212 U4(surface_desc).ddpfPixelFormat = tests[i].format;
9213 surface_desc.ddsCaps.dwCaps = tests[i].caps;
9214 surface_desc.ddsCaps.dwCaps2 = tests[i].caps2;
9216 if (tests[i].format.dwFourCC == MAKEFOURCC('D','X','T','1') && !(supported_fmts & SUPPORT_DXT1))
9217 continue;
9218 if (tests[i].format.dwFourCC == MAKEFOURCC('Y','U','Y','2') && !(supported_fmts & SUPPORT_YUY2))
9219 continue;
9220 if (tests[i].format.dwFourCC == MAKEFOURCC('U','Y','V','Y') && !(supported_fmts & SUPPORT_UYVY))
9221 continue;
9222 if (tests[i].caps & DDSCAPS_OVERLAY && !(hal_caps.dwCaps & DDCAPS_OVERLAY))
9223 continue;
9225 if (tests[i].caps & DDSCAPS_ZBUFFER)
9227 if (!z_fmt.dwSize)
9228 continue;
9230 U4(surface_desc).ddpfPixelFormat = z_fmt;
9233 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9234 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, surface %s.\n", hr, tests[i].name);
9236 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9237 if (tests[i].format.dwFourCC)
9238 todo_wine ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
9239 hr, tests[i].colorfill_hr, tests[i].name);
9240 else
9241 ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
9242 hr, tests[i].colorfill_hr, tests[i].name);
9244 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9245 if (tests[i].format.dwFourCC)
9246 todo_wine ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
9247 hr, tests[i].colorfill_hr, tests[i].name);
9248 else
9249 ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
9250 hr, tests[i].colorfill_hr, tests[i].name);
9252 if (SUCCEEDED(hr) && tests[i].check_result)
9254 memset(&surface_desc, 0, sizeof(surface_desc));
9255 surface_desc.dwSize = sizeof(surface_desc);
9256 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
9257 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9258 color = surface_desc.lpSurface;
9259 ok(*color == tests[i].result, "Got clear result 0x%08x, expected 0x%08x, surface %s.\n",
9260 *color, tests[i].result, tests[i].name);
9261 hr = IDirectDrawSurface4_Unlock(surface, NULL);
9262 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9265 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9266 ok(hr == tests[i].depthfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
9267 hr, tests[i].depthfill_hr, tests[i].name);
9268 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9269 ok(hr == tests[i].depthfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
9270 hr, tests[i].depthfill_hr, tests[i].name);
9272 U5(fx).dwFillColor = 0xdeadbeef;
9273 fx.dwROP = BLACKNESS;
9274 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
9275 ok(FAILED(hr) == !tests[i].rop_success, "Blt returned %#x, expected %s, surface %s.\n",
9276 hr, tests[i].rop_success ? "success" : "failure", tests[i].name);
9277 ok(U5(fx).dwFillColor == 0xdeadbeef, "dwFillColor was set to 0x%08x, surface %s\n",
9278 U5(fx).dwFillColor, tests[i].name);
9280 if (SUCCEEDED(hr) && tests[i].check_result)
9282 memset(&surface_desc, 0, sizeof(surface_desc));
9283 surface_desc.dwSize = sizeof(surface_desc);
9284 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
9285 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9286 color = surface_desc.lpSurface;
9287 ok(*color == 0, "Got clear result 0x%08x, expected 0x00000000, surface %s.\n",
9288 *color, tests[i].name);
9289 hr = IDirectDrawSurface4_Unlock(surface, NULL);
9290 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9293 fx.dwROP = WHITENESS;
9294 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
9295 ok(FAILED(hr) == !tests[i].rop_success, "Blt returned %#x, expected %s, surface %s.\n",
9296 hr, tests[i].rop_success ? "success" : "failure", tests[i].name);
9297 ok(U5(fx).dwFillColor == 0xdeadbeef, "dwFillColor was set to 0x%08x, surface %s\n",
9298 U5(fx).dwFillColor, tests[i].name);
9300 if (SUCCEEDED(hr) && tests[i].check_result)
9302 memset(&surface_desc, 0, sizeof(surface_desc));
9303 surface_desc.dwSize = sizeof(surface_desc);
9304 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
9305 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9306 color = surface_desc.lpSurface;
9307 /* WHITENESS sets the alpha channel to 0x00. Ignore this for now. */
9308 ok((*color & 0x00ffffff) == 0x00ffffff, "Got clear result 0x%08x, expected 0xffffffff, surface %s.\n",
9309 *color, tests[i].name);
9310 hr = IDirectDrawSurface4_Unlock(surface, NULL);
9311 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9314 IDirectDrawSurface4_Release(surface);
9317 memset(&fx, 0, sizeof(fx));
9318 fx.dwSize = sizeof(fx);
9319 U5(fx).dwFillColor = 0xdeadbeef;
9320 fx.dwROP = WHITENESS;
9322 memset(&surface_desc, 0, sizeof(surface_desc));
9323 surface_desc.dwSize = sizeof(surface_desc);
9324 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9325 surface_desc.dwWidth = 64;
9326 surface_desc.dwHeight = 64;
9327 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
9328 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
9329 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
9330 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
9331 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
9332 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
9333 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
9334 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9335 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9336 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
9337 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9339 /* No DDBLTFX. */
9340 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, NULL);
9341 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9342 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_ROP | DDBLT_WAIT, NULL);
9343 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9345 /* Unused source rectangle. */
9346 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9347 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9348 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
9349 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9351 /* Unused source surface. */
9352 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9353 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9354 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
9355 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9356 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9357 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9358 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
9359 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9361 /* Inverted destination or source rectangle. */
9362 SetRect(&rect, 5, 7, 7, 5);
9363 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9364 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9365 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9366 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9367 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9368 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9369 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9370 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9371 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
9372 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9374 /* Negative rectangle. */
9375 SetRect(&rect, -1, -1, 5, 5);
9376 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9377 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9378 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9379 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9380 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9381 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9382 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9383 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9384 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
9385 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9387 /* Out of bounds rectangle. */
9388 SetRect(&rect, 0, 0, 65, 65);
9389 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9390 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9391 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
9392 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9394 /* Combine multiple flags. */
9395 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9396 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9397 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_ROP | DDBLT_WAIT, &fx);
9398 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9399 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_ROP | DDBLT_WAIT, &fx);
9400 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9402 for (i = 0; i < sizeof(rops) / sizeof(*rops); i++)
9404 fx.dwROP = rops[i].rop;
9405 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
9406 ok(hr == rops[i].hr, "Got unexpected hr %#x for rop %s.\n", hr, rops[i].name);
9409 IDirectDrawSurface4_Release(surface2);
9410 IDirectDrawSurface4_Release(surface);
9412 if (!z_fmt.dwSize)
9413 goto done;
9415 memset(&surface_desc, 0, sizeof(surface_desc));
9416 surface_desc.dwSize = sizeof(surface_desc);
9417 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9418 surface_desc.dwWidth = 64;
9419 surface_desc.dwHeight = 64;
9420 U4(surface_desc).ddpfPixelFormat = z_fmt;
9421 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
9422 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9423 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9424 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
9425 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9427 /* No DDBLTFX. */
9428 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, NULL);
9429 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9431 /* Unused source rectangle. */
9432 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9433 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9435 /* Unused source surface. */
9436 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9437 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9438 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9439 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9441 /* Inverted destination or source rectangle. */
9442 SetRect(&rect, 5, 7, 7, 5);
9443 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9444 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9445 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9446 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9447 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9448 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9449 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9450 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9452 /* Negative rectangle. */
9453 SetRect(&rect, -1, -1, 5, 5);
9454 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9455 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9456 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9457 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9458 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9459 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9460 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9461 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9463 /* Out of bounds rectangle. */
9464 SetRect(&rect, 0, 0, 65, 65);
9465 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9466 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9468 /* Combine multiple flags. */
9469 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9470 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9472 IDirectDrawSurface4_Release(surface2);
9473 IDirectDrawSurface4_Release(surface);
9475 done:
9476 IDirectDraw4_Release(ddraw);
9477 refcount = IDirect3DDevice3_Release(device);
9478 ok(!refcount, "Device has %u references left.\n", refcount);
9479 DestroyWindow(window);
9482 static void test_texcoordindex(void)
9484 static struct
9486 struct vec3 pos;
9487 struct vec2 texcoord1;
9488 struct vec2 texcoord2;
9489 struct vec2 texcoord3;
9491 quad[] =
9493 {{-1.0f, -1.0f, 0.0f}, {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f}},
9494 {{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 1.0f}, {1.0f, 0.0f}},
9495 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 1.0f}, {1.0f, 0.0f}, {0.0f, 1.0f}},
9496 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 0.0f}},
9498 static const DWORD fvf = D3DFVF_XYZ | D3DFVF_TEX3;
9499 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
9500 IDirect3DDevice3 *device;
9501 IDirect3D3 *d3d;
9502 IDirectDraw4 *ddraw;
9503 IDirectDrawSurface4 *rt;
9504 IDirect3DViewport3 *viewport;
9505 HWND window;
9506 HRESULT hr;
9507 IDirectDrawSurface4 *surface1, *surface2;
9508 IDirect3DTexture2 *texture1, *texture2;
9509 DDSURFACEDESC2 surface_desc;
9510 ULONG refcount;
9511 D3DCOLOR color;
9512 DWORD *ptr;
9514 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
9515 0, 0, 640, 480, 0, 0, 0, 0);
9516 if (!(device = create_device(window, DDSCL_NORMAL)))
9518 skip("Failed to create a 3D device, skipping test.\n");
9519 DestroyWindow(window);
9520 return;
9523 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
9524 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
9525 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
9526 ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
9527 IDirect3D3_Release(d3d);
9529 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
9530 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
9532 memset(&surface_desc, 0, sizeof(surface_desc));
9533 surface_desc.dwSize = sizeof(surface_desc);
9534 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9535 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
9536 surface_desc.dwWidth = 2;
9537 surface_desc.dwHeight = 2;
9538 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
9539 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
9540 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
9541 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
9542 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
9543 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
9544 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
9545 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
9546 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9547 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
9548 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9550 memset(&surface_desc, 0, sizeof(surface_desc));
9551 surface_desc.dwSize = sizeof(surface_desc);
9552 hr = IDirectDrawSurface4_Lock(surface1, 0, &surface_desc, 0, NULL);
9553 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
9554 ptr = surface_desc.lpSurface;
9555 ptr[0] = 0xff000000;
9556 ptr[1] = 0xff00ff00;
9557 ptr += surface_desc.lPitch / sizeof(*ptr);
9558 ptr[0] = 0xff0000ff;
9559 ptr[1] = 0xff00ffff;
9560 hr = IDirectDrawSurface4_Unlock(surface1, NULL);
9561 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
9563 memset(&surface_desc, 0, sizeof(surface_desc));
9564 surface_desc.dwSize = sizeof(surface_desc);
9565 hr = IDirectDrawSurface4_Lock(surface2, 0, &surface_desc, 0, NULL);
9566 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
9567 ptr = surface_desc.lpSurface;
9568 ptr[0] = 0xff000000;
9569 ptr[1] = 0xff0000ff;
9570 ptr += surface_desc.lPitch / sizeof(*ptr);
9571 ptr[0] = 0xffff0000;
9572 ptr[1] = 0xffff00ff;
9573 hr = IDirectDrawSurface4_Unlock(surface2, 0);
9574 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
9576 viewport = create_viewport(device, 0, 0, 640, 480);
9577 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
9578 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
9580 hr = IDirectDrawSurface4_QueryInterface(surface1, &IID_IDirect3DTexture2, (void **)&texture1);
9581 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
9582 hr = IDirectDrawSurface4_QueryInterface(surface2, &IID_IDirect3DTexture2, (void **)&texture2);
9583 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
9584 hr = IDirect3DDevice3_SetTexture(device, 0, texture1);
9585 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
9586 hr = IDirect3DDevice3_SetTexture(device, 1, texture2);
9587 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
9588 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
9589 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
9590 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
9591 ok(SUCCEEDED(hr), "Failed to set color op, hr %#x.\n", hr);
9592 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
9593 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
9594 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_COLOROP, D3DTOP_ADD);
9595 ok(SUCCEEDED(hr), "Failed to set color op, hr %#x.\n", hr);
9596 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
9597 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
9598 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_COLORARG2, D3DTA_CURRENT);
9599 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
9600 hr = IDirect3DDevice3_SetTextureStageState(device, 2, D3DTSS_COLOROP, D3DTOP_DISABLE);
9601 ok(SUCCEEDED(hr), "Failed to set color op, hr %#x.\n", hr);
9603 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_TEXCOORDINDEX, 1);
9604 ok(SUCCEEDED(hr), "Failed to set texcoord index, hr %#x.\n", hr);
9605 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_TEXCOORDINDEX, 0);
9606 ok(SUCCEEDED(hr), "Failed to set texcoord index, hr %#x.\n", hr);
9608 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
9609 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
9611 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffff00, 1.0f, 0);
9612 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
9614 hr = IDirect3DDevice3_BeginScene(device);
9615 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
9616 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, fvf, quad, 4, 0);
9617 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
9618 hr = IDirect3DDevice3_EndScene(device);
9619 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
9621 color = get_surface_color(rt, 160, 120);
9622 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
9623 color = get_surface_color(rt, 480, 120);
9624 ok(compare_color(color, 0x0000ffff, 2), "Got unexpected color 0x%08x.\n", color);
9625 color = get_surface_color(rt, 160, 360);
9626 ok(compare_color(color, 0x00ff0000, 2), "Got unexpected color 0x%08x.\n", color);
9627 color = get_surface_color(rt, 480, 360);
9628 ok(compare_color(color, 0x00ffffff, 2), "Got unexpected color 0x%08x.\n", color);
9630 /* D3DTSS_TEXTURETRANSFORMFLAGS was introduced in D3D7, can't test it here. */
9632 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_TEXCOORDINDEX, 2);
9633 ok(SUCCEEDED(hr), "Failed to set texcoord index, hr %#x.\n", hr);
9635 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffff00, 1.0f, 0);
9636 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
9638 hr = IDirect3DDevice3_BeginScene(device);
9639 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
9640 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, fvf, quad, 4, 0);
9641 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
9642 hr = IDirect3DDevice3_EndScene(device);
9643 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
9645 color = get_surface_color(rt, 160, 120);
9646 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
9647 color = get_surface_color(rt, 480, 120);
9648 ok(compare_color(color, 0x0000ffff, 2), "Got unexpected color 0x%08x.\n", color);
9649 color = get_surface_color(rt, 160, 360);
9650 ok(compare_color(color, 0x00ff00ff, 2), "Got unexpected color 0x%08x.\n", color);
9651 color = get_surface_color(rt, 480, 360);
9652 ok(compare_color(color, 0x00ffff00, 2), "Got unexpected color 0x%08x.\n", color);
9654 IDirect3DTexture2_Release(texture2);
9655 IDirect3DTexture2_Release(texture1);
9656 IDirectDrawSurface4_Release(surface2);
9657 IDirectDrawSurface4_Release(surface1);
9659 destroy_viewport(device, viewport);
9661 IDirectDrawSurface4_Release(rt);
9662 IDirectDraw_Release(ddraw);
9663 refcount = IDirect3DDevice3_Release(device);
9664 ok(!refcount, "Device has %u references left.\n", refcount);
9665 DestroyWindow(window);
9668 static void test_colorkey_precision(void)
9670 static struct
9672 struct vec3 pos;
9673 struct vec2 texcoord;
9675 quad[] =
9677 {{-1.0f, -1.0f, 0.0f}, {0.0f, 1.0f}},
9678 {{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
9679 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 1.0f}},
9680 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
9682 IDirect3DDevice3 *device;
9683 IDirect3D3 *d3d;
9684 IDirectDraw4 *ddraw;
9685 IDirectDrawSurface4 *rt;
9686 IDirect3DViewport3 *viewport;
9687 HWND window;
9688 HRESULT hr;
9689 IDirectDrawSurface4 *src, *dst, *texture;
9690 IDirect3DTexture2 *d3d_texture;
9691 DDSURFACEDESC2 surface_desc, lock_desc;
9692 ULONG refcount;
9693 D3DCOLOR color;
9694 unsigned int t, c;
9695 DDCOLORKEY ckey;
9696 DDBLTFX fx;
9697 DWORD data[4] = {0}, color_mask;
9698 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
9699 D3DDEVICEDESC device_desc, hel_desc;
9700 BOOL warp;
9701 static const struct
9703 unsigned int max, shift, bpp, clear;
9704 const char *name;
9705 DDPIXELFORMAT fmt;
9707 tests[] =
9710 255, 0, 4, 0x00345678, "D3DFMT_X8R8G8B8",
9712 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
9713 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0x00000000}
9718 63, 5, 2, 0x5678, "D3DFMT_R5G6B5, G channel",
9720 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
9721 {16}, {0xf800}, {0x07e0}, {0x001f}, {0x0000}
9726 31, 0, 2, 0x5678, "D3DFMT_R5G6B5, B channel",
9728 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
9729 {16}, {0xf800}, {0x07e0}, {0x001f}, {0x0000}
9734 15, 0, 2, 0x0678, "D3DFMT_A4R4G4B4",
9736 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9737 {16}, {0x0f00}, {0x00f0}, {0x000f}, {0xf000}
9743 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
9744 0, 0, 640, 480, 0, 0, 0, 0);
9745 if (!(device = create_device(window, DDSCL_NORMAL)))
9747 skip("Failed to create a 3D device, skipping test.\n");
9748 DestroyWindow(window);
9749 return;
9752 /* The Windows 8 WARP driver has plenty of false negatives in X8R8G8B8
9753 * (color key doesn't match although the values are equal), and a false
9754 * positive when the color key is 0 and the texture contains the value 1.
9755 * I don't want to mark this broken unconditionally since this would
9756 * essentially disable the test on Windows. Try to detect WARP (and I
9757 * guess mismatch other SW renderers) by its ability to texture from
9758 * system memory. Also on random occasions 254 == 255 and 255 != 255.*/
9759 memset(&device_desc, 0, sizeof(device_desc));
9760 device_desc.dwSize = sizeof(device_desc);
9761 memset(&hel_desc, 0, sizeof(hel_desc));
9762 hel_desc.dwSize = sizeof(hel_desc);
9763 hr = IDirect3DDevice3_GetCaps(device, &device_desc, &hel_desc);
9764 ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
9765 warp = !!(device_desc.dwDevCaps & D3DDEVCAPS_TEXTURESYSTEMMEMORY);
9767 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
9768 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
9769 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
9770 ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
9771 IDirect3D3_Release(d3d);
9772 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
9773 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
9775 viewport = create_viewport(device, 0, 0, 640, 480);
9776 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
9777 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
9779 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
9780 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
9781 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
9782 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
9783 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
9784 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
9785 /* Multiply the texture read result with 0, that way the result color if the key doesn't
9786 * match is constant. In theory color keying works without reading the texture result
9787 * (meaning we could just op=arg1, arg1=tfactor), but the Geforce7 Windows driver begs
9788 * to differ. */
9789 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
9790 ok(SUCCEEDED(hr), "Failed to set color op, hr %#x.\n", hr);
9791 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
9792 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
9793 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
9794 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
9795 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_TEXTUREFACTOR, 0x00000000);
9796 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
9798 memset(&fx, 0, sizeof(fx));
9799 fx.dwSize = sizeof(fx);
9800 memset(&lock_desc, 0, sizeof(lock_desc));
9801 lock_desc.dwSize = sizeof(lock_desc);
9803 for (t = 0; t < sizeof(tests) / sizeof(*tests); ++t)
9805 memset(&surface_desc, 0, sizeof(surface_desc));
9806 surface_desc.dwSize = sizeof(surface_desc);
9807 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9808 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
9809 surface_desc.dwWidth = 4;
9810 surface_desc.dwHeight = 1;
9811 U4(surface_desc).ddpfPixelFormat = tests[t].fmt;
9812 /* Windows XP (at least with the r200 driver, other drivers untested) produces
9813 * garbage when doing color keyed texture->texture blits. */
9814 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &src, NULL);
9815 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9816 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &dst, NULL);
9817 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9819 fx.dwFillColor = tests[t].clear;
9820 /* On the w8 testbot (WARP driver) the blit result has different values in the
9821 * X channel. */
9822 color_mask = U2(tests[t].fmt).dwRBitMask
9823 | U3(tests[t].fmt).dwGBitMask
9824 | U4(tests[t].fmt).dwBBitMask;
9826 for (c = 0; c <= tests[t].max; ++c)
9828 /* The idiotic Nvidia Windows driver can't change the color key on a d3d
9829 * texture after it has been set once... */
9830 surface_desc.dwFlags |= DDSD_CKSRCBLT;
9831 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
9832 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = c << tests[t].shift;
9833 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = c << tests[t].shift;
9834 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &texture, NULL);
9835 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9836 hr = IDirectDrawSurface4_QueryInterface(texture, &IID_IDirect3DTexture2, (void **)&d3d_texture);
9837 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
9838 hr = IDirect3DDevice3_SetTexture(device, 0, d3d_texture);
9839 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
9841 hr = IDirectDrawSurface4_Blt(dst, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9842 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
9844 hr = IDirectDrawSurface4_Lock(src, NULL, &lock_desc, DDLOCK_WAIT, NULL);
9845 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
9846 switch (tests[t].bpp)
9848 case 4:
9849 ((DWORD *)lock_desc.lpSurface)[0] = (c ? c - 1 : 0) << tests[t].shift;
9850 ((DWORD *)lock_desc.lpSurface)[1] = c << tests[t].shift;
9851 ((DWORD *)lock_desc.lpSurface)[2] = min(c + 1, tests[t].max) << tests[t].shift;
9852 ((DWORD *)lock_desc.lpSurface)[3] = 0xffffffff;
9853 break;
9855 case 2:
9856 ((WORD *)lock_desc.lpSurface)[0] = (c ? c - 1 : 0) << tests[t].shift;
9857 ((WORD *)lock_desc.lpSurface)[1] = c << tests[t].shift;
9858 ((WORD *)lock_desc.lpSurface)[2] = min(c + 1, tests[t].max) << tests[t].shift;
9859 ((WORD *)lock_desc.lpSurface)[3] = 0xffff;
9860 break;
9862 hr = IDirectDrawSurface4_Unlock(src, 0);
9863 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
9864 hr = IDirectDrawSurface4_Blt(texture, NULL, src, NULL, DDBLT_WAIT, NULL);
9865 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
9867 ckey.dwColorSpaceLowValue = c << tests[t].shift;
9868 ckey.dwColorSpaceHighValue = c << tests[t].shift;
9869 hr = IDirectDrawSurface4_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
9870 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
9872 hr = IDirectDrawSurface4_Blt(dst, NULL, src, NULL, DDBLT_KEYSRC | DDBLT_WAIT, NULL);
9873 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
9875 /* Don't make this read only, it somehow breaks the detection of the Nvidia bug below. */
9876 hr = IDirectDrawSurface4_Lock(dst, NULL, &lock_desc, DDLOCK_WAIT, NULL);
9877 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
9878 switch (tests[t].bpp)
9880 case 4:
9881 data[0] = ((DWORD *)lock_desc.lpSurface)[0] & color_mask;
9882 data[1] = ((DWORD *)lock_desc.lpSurface)[1] & color_mask;
9883 data[2] = ((DWORD *)lock_desc.lpSurface)[2] & color_mask;
9884 data[3] = ((DWORD *)lock_desc.lpSurface)[3] & color_mask;
9885 break;
9887 case 2:
9888 data[0] = ((WORD *)lock_desc.lpSurface)[0] & color_mask;
9889 data[1] = ((WORD *)lock_desc.lpSurface)[1] & color_mask;
9890 data[2] = ((WORD *)lock_desc.lpSurface)[2] & color_mask;
9891 data[3] = ((WORD *)lock_desc.lpSurface)[3] & color_mask;
9892 break;
9894 hr = IDirectDrawSurface4_Unlock(dst, 0);
9895 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
9897 if (!c)
9899 ok(data[0] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
9900 tests[t].clear, data[0], tests[t].name, c);
9902 if (data[3] == tests[t].clear)
9904 /* My Geforce GTX 460 on Windows 7 misbehaves when A4R4G4B4 is blitted with color
9905 * keying: The blit takes ~0.5 seconds, and subsequent color keying draws are broken,
9906 * even when a different surface is used. The blit itself doesn't draw anything,
9907 * so we can detect the bug by looking at the otherwise unused 4th texel. It should
9908 * never be masked out by the key.
9910 * Also appears to affect the testbot in some way with R5G6B5. Color keying is
9911 * terrible on WARP. */
9912 skip("Nvidia A4R4G4B4 color keying blit bug detected, skipping.\n");
9913 IDirect3DTexture2_Release(d3d_texture);
9914 IDirectDrawSurface4_Release(texture);
9915 IDirectDrawSurface4_Release(src);
9916 IDirectDrawSurface4_Release(dst);
9917 goto done;
9920 else
9921 ok(data[0] == (c - 1) << tests[t].shift, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
9922 (c - 1) << tests[t].shift, data[0], tests[t].name, c);
9924 ok(data[1] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
9925 tests[t].clear, data[1], tests[t].name, c);
9927 if (c == tests[t].max)
9928 ok(data[2] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
9929 tests[t].clear, data[2], tests[t].name, c);
9930 else
9931 ok(data[2] == (c + 1) << tests[t].shift, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
9932 (c + 1) << tests[t].shift, data[2], tests[t].name, c);
9934 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0x0000ff00, 1.0f, 0);
9935 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
9937 hr = IDirect3DDevice3_BeginScene(device);
9938 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
9939 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEX1, quad, 4, 0);
9940 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
9941 hr = IDirect3DDevice3_EndScene(device);
9942 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
9944 color = get_surface_color(rt, 80, 240);
9945 if (!c)
9946 ok(compare_color(color, 0x0000ff00, 1) || broken(warp && compare_color(color, 0x00000000, 1)),
9947 "Got unexpected color 0x%08x, format %s, c=%u.\n",
9948 color, tests[t].name, c);
9949 else
9950 ok(compare_color(color, 0x00000000, 1) || broken(warp && compare_color(color, 0x0000ff00, 1)),
9951 "Got unexpected color 0x%08x, format %s, c=%u.\n",
9952 color, tests[t].name, c);
9954 color = get_surface_color(rt, 240, 240);
9955 ok(compare_color(color, 0x0000ff00, 1) || broken(warp && compare_color(color, 0x00000000, 1)),
9956 "Got unexpected color 0x%08x, format %s, c=%u.\n",
9957 color, tests[t].name, c);
9959 color = get_surface_color(rt, 400, 240);
9960 if (c == tests[t].max)
9961 ok(compare_color(color, 0x0000ff00, 1) || broken(warp && compare_color(color, 0x00000000, 1)),
9962 "Got unexpected color 0x%08x, format %s, c=%u.\n",
9963 color, tests[t].name, c);
9964 else
9965 ok(compare_color(color, 0x00000000, 1) || broken(warp && compare_color(color, 0x0000ff00, 1)),
9966 "Got unexpected color 0x%08x, format %s, c=%u.\n",
9967 color, tests[t].name, c);
9969 IDirect3DTexture2_Release(d3d_texture);
9970 IDirectDrawSurface4_Release(texture);
9972 IDirectDrawSurface4_Release(src);
9973 IDirectDrawSurface4_Release(dst);
9975 done:
9977 destroy_viewport(device, viewport);
9978 IDirectDrawSurface4_Release(rt);
9979 IDirectDraw4_Release(ddraw);
9980 refcount = IDirect3DDevice3_Release(device);
9981 ok(!refcount, "Device has %u references left.\n", refcount);
9982 DestroyWindow(window);
9985 static void test_range_colorkey(void)
9987 IDirectDraw4 *ddraw;
9988 HWND window;
9989 HRESULT hr;
9990 IDirectDrawSurface4 *surface;
9991 DDSURFACEDESC2 surface_desc;
9992 ULONG refcount;
9993 DDCOLORKEY ckey;
9995 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
9996 0, 0, 640, 480, 0, 0, 0, 0);
9997 ddraw = create_ddraw();
9998 ok(!!ddraw, "Failed to create a ddraw object.\n");
9999 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
10000 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
10002 memset(&surface_desc, 0, sizeof(surface_desc));
10003 surface_desc.dwSize = sizeof(surface_desc);
10004 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
10005 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
10006 surface_desc.dwWidth = 1;
10007 surface_desc.dwHeight = 1;
10008 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
10009 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
10010 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
10011 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
10012 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
10013 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0x00000000;
10015 /* Creating a surface with a range color key fails with DDERR_NOCOLORKEY. */
10016 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
10017 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000001;
10018 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10019 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10021 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000001;
10022 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
10023 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10024 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10026 /* Same for DDSCAPS_OFFSCREENPLAIN. */
10027 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
10028 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
10029 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000001;
10030 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10031 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10033 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000001;
10034 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
10035 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10036 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10038 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
10039 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
10040 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10041 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
10043 /* Setting a range color key without DDCKEY_COLORSPACE collapses the key. */
10044 ckey.dwColorSpaceLowValue = 0x00000000;
10045 ckey.dwColorSpaceHighValue = 0x00000001;
10046 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT, &ckey);
10047 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
10049 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
10050 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
10051 ok(!ckey.dwColorSpaceLowValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
10052 ok(!ckey.dwColorSpaceHighValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
10054 ckey.dwColorSpaceLowValue = 0x00000001;
10055 ckey.dwColorSpaceHighValue = 0x00000000;
10056 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT, &ckey);
10057 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
10059 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
10060 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
10061 ok(ckey.dwColorSpaceLowValue == 0x00000001, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
10062 ok(ckey.dwColorSpaceHighValue == 0x00000001, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
10064 /* DDCKEY_COLORSPACE is ignored if the key is a single value. */
10065 ckey.dwColorSpaceLowValue = 0x00000000;
10066 ckey.dwColorSpaceHighValue = 0x00000000;
10067 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
10068 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
10070 /* Using it with a range key results in DDERR_NOCOLORKEYHW. */
10071 ckey.dwColorSpaceLowValue = 0x00000001;
10072 ckey.dwColorSpaceHighValue = 0x00000000;
10073 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
10074 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10075 ckey.dwColorSpaceLowValue = 0x00000000;
10076 ckey.dwColorSpaceHighValue = 0x00000001;
10077 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
10078 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10079 /* Range destination keys don't work either. */
10080 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_DESTBLT | DDCKEY_COLORSPACE, &ckey);
10081 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10083 /* Just to show it's not because of A, R, and G having equal values. */
10084 ckey.dwColorSpaceLowValue = 0x00000000;
10085 ckey.dwColorSpaceHighValue = 0x01010101;
10086 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
10087 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10089 /* None of these operations modified the key. */
10090 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
10091 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
10092 ok(!ckey.dwColorSpaceLowValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
10093 ok(!ckey.dwColorSpaceHighValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
10095 IDirectDrawSurface4_Release(surface),
10096 refcount = IDirectDraw4_Release(ddraw);
10097 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
10098 DestroyWindow(window);
10101 static void test_shademode(void)
10103 IDirect3DVertexBuffer *vb_strip, *vb_list, *buffer;
10104 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
10105 IDirect3DViewport3 *viewport;
10106 IDirect3DDevice3 *device;
10107 D3DVERTEXBUFFERDESC desc;
10108 IDirectDrawSurface4 *rt;
10109 DWORD color0, color1;
10110 void *data = NULL;
10111 IDirect3D3 *d3d;
10112 ULONG refcount;
10113 UINT i, count;
10114 HWND window;
10115 HRESULT hr;
10116 static const struct
10118 struct vec3 position;
10119 DWORD diffuse;
10121 quad_strip[] =
10123 {{-1.0f, -1.0f, 0.0f}, 0xffff0000},
10124 {{-1.0f, 1.0f, 0.0f}, 0xff00ff00},
10125 {{ 1.0f, -1.0f, 0.0f}, 0xff0000ff},
10126 {{ 1.0f, 1.0f, 0.0f}, 0xffffffff},
10128 quad_list[] =
10130 {{-1.0f, -1.0f, 0.0f}, 0xffff0000},
10131 {{-1.0f, 1.0f, 0.0f}, 0xff00ff00},
10132 {{ 1.0f, -1.0f, 0.0f}, 0xff0000ff},
10134 {{ 1.0f, -1.0f, 0.0f}, 0xff0000ff},
10135 {{-1.0f, 1.0f, 0.0f}, 0xff00ff00},
10136 {{ 1.0f, 1.0f, 0.0f}, 0xffffffff},
10138 static const struct
10140 DWORD primtype;
10141 DWORD shademode;
10142 DWORD color0, color1;
10144 tests[] =
10146 {D3DPT_TRIANGLESTRIP, D3DSHADE_FLAT, 0x00ff0000, 0x0000ff00},
10147 {D3DPT_TRIANGLESTRIP, D3DSHADE_PHONG, 0x000dca28, 0x000d45c7},
10148 {D3DPT_TRIANGLESTRIP, D3DSHADE_GOURAUD, 0x000dca28, 0x000d45c7},
10149 {D3DPT_TRIANGLESTRIP, D3DSHADE_PHONG, 0x000dca28, 0x000d45c7},
10150 {D3DPT_TRIANGLELIST, D3DSHADE_FLAT, 0x00ff0000, 0x000000ff},
10151 {D3DPT_TRIANGLELIST, D3DSHADE_GOURAUD, 0x000dca28, 0x000d45c7},
10154 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
10155 0, 0, 640, 480, 0, 0, 0, 0);
10157 if (!(device = create_device(window, DDSCL_NORMAL)))
10159 skip("Failed to create a 3D device, skipping test.\n");
10160 DestroyWindow(window);
10161 return;
10164 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
10165 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
10166 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
10167 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
10169 viewport = create_viewport(device, 0, 0, 640, 480);
10170 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
10171 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
10173 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
10174 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
10176 memset(&desc, 0, sizeof(desc));
10177 desc.dwSize = sizeof(desc);
10178 desc.dwCaps = D3DVBCAPS_WRITEONLY;
10179 desc.dwFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
10180 desc.dwNumVertices = sizeof(quad_strip) / sizeof(*quad_strip);
10181 hr = IDirect3D3_CreateVertexBuffer(d3d, &desc, &vb_strip, 0, NULL);
10182 ok(hr == D3D_OK, "Failed to create vertex buffer, hr %#x.\n", hr);
10183 hr = IDirect3DVertexBuffer_Lock(vb_strip, 0, &data, NULL);
10184 ok(hr == D3D_OK, "Failed to lock vertex buffer, hr %#x.\n", hr);
10185 memcpy(data, quad_strip, sizeof(quad_strip));
10186 hr = IDirect3DVertexBuffer_Unlock(vb_strip);
10187 ok(hr == D3D_OK, "Failed to unlock vertex buffer, hr %#x.\n", hr);
10189 desc.dwNumVertices = sizeof(quad_list) / sizeof(*quad_list);
10190 hr = IDirect3D3_CreateVertexBuffer(d3d, &desc, &vb_list, 0, NULL);
10191 ok(hr == D3D_OK, "Failed to create vertex buffer, hr %#x.\n", hr);
10192 hr = IDirect3DVertexBuffer_Lock(vb_list, 0, &data, NULL);
10193 ok(hr == D3D_OK, "Failed to lock vertex buffer, hr %#x.\n", hr);
10194 memcpy(data, quad_list, sizeof(quad_list));
10195 hr = IDirect3DVertexBuffer_Unlock(vb_list);
10196 ok(hr == D3D_OK, "Failed to unlock vertex buffer, hr %#x.\n", hr);
10198 /* Try it first with a TRIANGLESTRIP. Do it with different geometry because
10199 * the color fixups we have to do for FLAT shading will be dependent on that. */
10201 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
10203 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffffff, 0.0f, 0);
10204 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
10206 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_SHADEMODE, tests[i].shademode);
10207 ok(hr == D3D_OK, "Failed to set shade mode, hr %#x.\n", hr);
10209 hr = IDirect3DDevice3_BeginScene(device);
10210 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
10211 buffer = tests[i].primtype == D3DPT_TRIANGLESTRIP ? vb_strip : vb_list;
10212 count = tests[i].primtype == D3DPT_TRIANGLESTRIP ? 4 : 6;
10213 hr = IDirect3DDevice3_DrawPrimitiveVB(device, tests[i].primtype, buffer, 0, count, 0);
10214 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10215 hr = IDirect3DDevice3_EndScene(device);
10216 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
10218 color0 = get_surface_color(rt, 100, 100); /* Inside first triangle */
10219 color1 = get_surface_color(rt, 500, 350); /* Inside second triangle */
10221 /* For D3DSHADE_FLAT it should take the color of the first vertex of
10222 * each triangle. This requires EXT_provoking_vertex or similar
10223 * functionality being available. */
10224 /* PHONG should be the same as GOURAUD, since no hardware implements
10225 * this. */
10226 ok(color0 == tests[i].color0, "Test %u shading has color0 %08x, expected %08x.\n",
10227 i, color0, tests[i].color0);
10228 ok(color1 == tests[i].color1, "Test %u shading has color1 %08x, expected %08x.\n",
10229 i, color1, tests[i].color1);
10232 IDirect3DVertexBuffer_Release(vb_strip);
10233 IDirect3DVertexBuffer_Release(vb_list);
10234 destroy_viewport(device, viewport);
10235 IDirectDrawSurface4_Release(rt);
10236 IDirect3D3_Release(d3d);
10237 refcount = IDirect3DDevice3_Release(device);
10238 ok(!refcount, "Device has %u references left.\n", refcount);
10239 DestroyWindow(window);
10242 START_TEST(ddraw4)
10244 IDirectDraw4 *ddraw;
10245 DEVMODEW current_mode;
10247 if (!(ddraw = create_ddraw()))
10249 skip("Failed to create a ddraw object, skipping tests.\n");
10250 return;
10252 IDirectDraw4_Release(ddraw);
10254 memset(&current_mode, 0, sizeof(current_mode));
10255 current_mode.dmSize = sizeof(current_mode);
10256 ok(EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &current_mode), "Failed to get display mode.\n");
10257 registry_mode.dmSize = sizeof(registry_mode);
10258 ok(EnumDisplaySettingsW(NULL, ENUM_REGISTRY_SETTINGS, &registry_mode), "Failed to get display mode.\n");
10259 if (registry_mode.dmPelsWidth != current_mode.dmPelsWidth
10260 || registry_mode.dmPelsHeight != current_mode.dmPelsHeight)
10262 skip("Current mode does not match registry mode, skipping test.\n");
10263 return;
10266 test_process_vertices();
10267 test_coop_level_create_device_window();
10268 test_clipper_blt();
10269 test_coop_level_d3d_state();
10270 test_surface_interface_mismatch();
10271 test_coop_level_threaded();
10272 test_depth_blit();
10273 test_texture_load_ckey();
10274 test_viewport();
10275 test_zenable();
10276 test_ck_rgba();
10277 test_ck_default();
10278 test_ck_complex();
10279 test_surface_qi();
10280 test_device_qi();
10281 test_wndproc();
10282 test_window_style();
10283 test_redundant_mode_set();
10284 test_coop_level_mode_set();
10285 test_coop_level_mode_set_multi();
10286 test_initialize();
10287 test_coop_level_surf_create();
10288 test_vb_discard();
10289 test_coop_level_multi_window();
10290 test_draw_strided();
10291 test_lighting();
10292 test_specular_lighting();
10293 test_clear_rect_count();
10294 test_coop_level_versions();
10295 test_lighting_interface_versions();
10296 test_coop_level_activateapp();
10297 test_texturemanage();
10298 test_block_formats_creation();
10299 test_unsupported_formats();
10300 test_rt_caps();
10301 test_primary_caps();
10302 test_surface_lock();
10303 test_surface_discard();
10304 test_flip();
10305 test_set_surface_desc();
10306 test_user_memory_getdc();
10307 test_sysmem_overlay();
10308 test_primary_palette();
10309 test_surface_attachment();
10310 test_private_data();
10311 test_pixel_format();
10312 test_create_surface_pitch();
10313 test_mipmap_lock();
10314 test_palette_complex();
10315 test_p8_rgb_blit();
10316 test_material();
10317 test_palette_gdi();
10318 test_palette_alpha();
10319 test_vb_writeonly();
10320 test_lost_device();
10321 test_surface_desc_lock();
10322 test_signed_formats();
10323 test_color_fill();
10324 test_texcoordindex();
10325 test_colorkey_precision();
10326 test_range_colorkey();
10327 test_shademode();