ddraw: Return early in d3d_device7_DrawIndexedPrimitiveStrided() with a 0 vertex...
[wine.git] / dlls / ddraw / tests / ddraw4.c
blob08ab9bdd8eb583ae8a5d15d28dddad77a224b4af
1 /*
2 * Copyright 2005 Antoine Chavasse (a.chavasse@gmail.com)
3 * Copyright 2008, 2011, 2012-2014 Stefan Dösinger for CodeWeavers
4 * Copyright 2011-2014 Henri Verbeet for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #define COBJMACROS
22 #include "wine/test.h"
23 #include <limits.h>
24 #include <math.h>
25 #include "d3d.h"
27 static BOOL is_ddraw64 = sizeof(DWORD) != sizeof(DWORD *);
28 static DEVMODEW registry_mode;
30 struct vec2
32 float x, y;
35 struct vec3
37 float x, y, z;
40 struct vec4
42 float x, y, z, w;
45 struct create_window_thread_param
47 HWND window;
48 HANDLE window_created;
49 HANDLE destroy_window;
50 HANDLE thread;
53 static BOOL compare_float(float f, float g, unsigned int ulps)
55 int x = *(int *)&f;
56 int y = *(int *)&g;
58 if (x < 0)
59 x = INT_MIN - x;
60 if (y < 0)
61 y = INT_MIN - y;
63 if (abs(x - y) > ulps)
64 return FALSE;
66 return TRUE;
69 static BOOL compare_vec4(struct vec4 *vec, float x, float y, float z, float w, unsigned int ulps)
71 return compare_float(vec->x, x, ulps)
72 && compare_float(vec->y, y, ulps)
73 && compare_float(vec->z, z, ulps)
74 && compare_float(vec->w, w, ulps);
77 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
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 c1 >>= 8; c2 >>= 8;
85 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
86 return TRUE;
89 static IDirectDrawSurface4 *create_overlay(IDirectDraw4 *ddraw,
90 unsigned int width, unsigned int height, DWORD format)
92 IDirectDrawSurface4 *surface;
93 DDSURFACEDESC2 desc;
95 memset(&desc, 0, sizeof(desc));
96 desc.dwSize = sizeof(desc);
97 desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
98 desc.dwWidth = width;
99 desc.dwHeight = height;
100 desc.ddsCaps.dwCaps = DDSCAPS_OVERLAY;
101 U4(desc).ddpfPixelFormat.dwSize = sizeof(U4(desc).ddpfPixelFormat);
102 U4(desc).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
103 U4(desc).ddpfPixelFormat.dwFourCC = format;
105 if (FAILED(IDirectDraw4_CreateSurface(ddraw, &desc, &surface, NULL)))
106 return NULL;
107 return surface;
110 static DWORD WINAPI create_window_thread_proc(void *param)
112 struct create_window_thread_param *p = param;
113 DWORD res;
114 BOOL ret;
116 p->window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
117 0, 0, 640, 480, 0, 0, 0, 0);
118 ret = SetEvent(p->window_created);
119 ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
121 for (;;)
123 MSG msg;
125 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
126 DispatchMessageA(&msg);
127 res = WaitForSingleObject(p->destroy_window, 100);
128 if (res == WAIT_OBJECT_0)
129 break;
130 if (res != WAIT_TIMEOUT)
132 ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
133 break;
137 DestroyWindow(p->window);
139 return 0;
142 static void create_window_thread(struct create_window_thread_param *p)
144 DWORD res, tid;
146 p->window_created = CreateEventA(NULL, FALSE, FALSE, NULL);
147 ok(!!p->window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
148 p->destroy_window = CreateEventA(NULL, FALSE, FALSE, NULL);
149 ok(!!p->destroy_window, "CreateEvent failed, last error %#x.\n", GetLastError());
150 p->thread = CreateThread(NULL, 0, create_window_thread_proc, p, 0, &tid);
151 ok(!!p->thread, "Failed to create thread, last error %#x.\n", GetLastError());
152 res = WaitForSingleObject(p->window_created, INFINITE);
153 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
156 static void destroy_window_thread(struct create_window_thread_param *p)
158 SetEvent(p->destroy_window);
159 WaitForSingleObject(p->thread, INFINITE);
160 CloseHandle(p->destroy_window);
161 CloseHandle(p->window_created);
162 CloseHandle(p->thread);
165 static IDirectDrawSurface4 *get_depth_stencil(IDirect3DDevice3 *device)
167 IDirectDrawSurface4 *rt, *ret;
168 DDSCAPS2 caps = {DDSCAPS_ZBUFFER, 0, 0, {0}};
169 HRESULT hr;
171 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
172 ok(SUCCEEDED(hr), "Failed to get the render target, hr %#x.\n", hr);
173 hr = IDirectDrawSurface4_GetAttachedSurface(rt, &caps, &ret);
174 ok(SUCCEEDED(hr) || hr == DDERR_NOTFOUND, "Failed to get the z buffer, hr %#x.\n", hr);
175 IDirectDrawSurface4_Release(rt);
176 return ret;
179 static HRESULT set_display_mode(IDirectDraw4 *ddraw, DWORD width, DWORD height)
181 if (SUCCEEDED(IDirectDraw4_SetDisplayMode(ddraw, width, height, 32, 0, 0)))
182 return DD_OK;
183 return IDirectDraw4_SetDisplayMode(ddraw, width, height, 24, 0, 0);
186 static D3DCOLOR get_surface_color(IDirectDrawSurface4 *surface, UINT x, UINT y)
188 RECT rect = {x, y, x + 1, y + 1};
189 DDSURFACEDESC2 surface_desc;
190 D3DCOLOR color;
191 HRESULT hr;
193 memset(&surface_desc, 0, sizeof(surface_desc));
194 surface_desc.dwSize = sizeof(surface_desc);
196 hr = IDirectDrawSurface4_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
197 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
198 if (FAILED(hr))
199 return 0xdeadbeef;
201 color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
203 hr = IDirectDrawSurface4_Unlock(surface, &rect);
204 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
206 return color;
209 static HRESULT CALLBACK enum_z_fmt(DDPIXELFORMAT *format, void *ctx)
211 DDPIXELFORMAT *z_fmt = ctx;
213 if (U1(*format).dwZBufferBitDepth > U1(*z_fmt).dwZBufferBitDepth)
214 *z_fmt = *format;
216 return DDENUMRET_OK;
219 static IDirectDraw4 *create_ddraw(void)
221 IDirectDraw4 *ddraw4;
222 IDirectDraw *ddraw1;
223 HRESULT hr;
225 if (FAILED(DirectDrawCreate(NULL, &ddraw1, NULL)))
226 return NULL;
228 hr = IDirectDraw_QueryInterface(ddraw1, &IID_IDirectDraw4, (void **)&ddraw4);
229 IDirectDraw_Release(ddraw1);
230 if (FAILED(hr))
231 return NULL;
233 return ddraw4;
236 static IDirect3DDevice3 *create_device(HWND window, DWORD coop_level)
238 IDirectDrawSurface4 *surface, *ds;
239 IDirect3DDevice3 *device = NULL;
240 DDSURFACEDESC2 surface_desc;
241 IDirectDraw4 *ddraw4;
242 DDPIXELFORMAT z_fmt;
243 IDirect3D3 *d3d3;
244 HRESULT hr;
246 if (!(ddraw4 = create_ddraw()))
247 return NULL;
249 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, coop_level);
250 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
252 memset(&surface_desc, 0, sizeof(surface_desc));
253 surface_desc.dwSize = sizeof(surface_desc);
254 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
255 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
256 surface_desc.dwWidth = 640;
257 surface_desc.dwHeight = 480;
259 hr = IDirectDraw4_CreateSurface(ddraw4, &surface_desc, &surface, NULL);
260 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
262 if (coop_level & DDSCL_NORMAL)
264 IDirectDrawClipper *clipper;
266 hr = IDirectDraw4_CreateClipper(ddraw4, 0, &clipper, NULL);
267 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
268 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
269 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
270 hr = IDirectDrawSurface4_SetClipper(surface, clipper);
271 ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
272 IDirectDrawClipper_Release(clipper);
275 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirect3D3, (void **)&d3d3);
276 IDirectDraw4_Release(ddraw4);
277 if (FAILED(hr))
279 IDirectDrawSurface4_Release(surface);
280 return NULL;
283 memset(&z_fmt, 0, sizeof(z_fmt));
284 hr = IDirect3D3_EnumZBufferFormats(d3d3, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
285 if (FAILED(hr) || !z_fmt.dwSize)
287 IDirect3D3_Release(d3d3);
288 IDirectDrawSurface4_Release(surface);
289 return NULL;
292 memset(&surface_desc, 0, sizeof(surface_desc));
293 surface_desc.dwSize = sizeof(surface_desc);
294 surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
295 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
296 U4(surface_desc).ddpfPixelFormat = z_fmt;
297 surface_desc.dwWidth = 640;
298 surface_desc.dwHeight = 480;
299 hr = IDirectDraw4_CreateSurface(ddraw4, &surface_desc, &ds, NULL);
300 ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
301 if (FAILED(hr))
303 IDirect3D3_Release(d3d3);
304 IDirectDrawSurface4_Release(surface);
305 return NULL;
308 hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
309 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
310 IDirectDrawSurface4_Release(ds);
311 if (FAILED(hr))
313 IDirect3D3_Release(d3d3);
314 IDirectDrawSurface4_Release(surface);
315 return NULL;
318 hr = IDirect3D3_CreateDevice(d3d3, &IID_IDirect3DHALDevice, surface, &device, NULL);
319 IDirect3D3_Release(d3d3);
320 IDirectDrawSurface4_Release(surface);
321 if (FAILED(hr))
322 return NULL;
324 return device;
327 static IDirect3DViewport3 *create_viewport(IDirect3DDevice3 *device, UINT x, UINT y, UINT w, UINT h)
329 IDirect3DViewport3 *viewport;
330 D3DVIEWPORT2 vp;
331 IDirect3D3 *d3d;
332 HRESULT hr;
334 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
335 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
336 hr = IDirect3D3_CreateViewport(d3d, &viewport, NULL);
337 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
338 hr = IDirect3DDevice3_AddViewport(device, viewport);
339 ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
340 memset(&vp, 0, sizeof(vp));
341 vp.dwSize = sizeof(vp);
342 vp.dwX = x;
343 vp.dwY = y;
344 vp.dwWidth = w;
345 vp.dwHeight = h;
346 vp.dvClipX = -1.0f;
347 vp.dvClipY = 1.0f;
348 vp.dvClipWidth = 2.0f;
349 vp.dvClipHeight = 2.0f;
350 vp.dvMinZ = 0.0f;
351 vp.dvMaxZ = 1.0f;
352 hr = IDirect3DViewport3_SetViewport2(viewport, &vp);
353 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
354 IDirect3D3_Release(d3d);
356 return viewport;
359 static void destroy_viewport(IDirect3DDevice3 *device, IDirect3DViewport3 *viewport)
361 HRESULT hr;
363 hr = IDirect3DDevice3_DeleteViewport(device, viewport);
364 ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
365 IDirect3DViewport3_Release(viewport);
368 static IDirect3DMaterial3 *create_material(IDirect3DDevice3 *device, D3DMATERIAL *mat)
370 IDirect3DMaterial3 *material;
371 IDirect3D3 *d3d;
372 HRESULT hr;
374 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
375 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
376 hr = IDirect3D3_CreateMaterial(d3d, &material, NULL);
377 ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
378 hr = IDirect3DMaterial3_SetMaterial(material, mat);
379 ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
380 IDirect3D3_Release(d3d);
382 return material;
385 static IDirect3DMaterial3 *create_diffuse_material(IDirect3DDevice3 *device, float r, float g, float b, float a)
387 D3DMATERIAL mat;
389 memset(&mat, 0, sizeof(mat));
390 mat.dwSize = sizeof(mat);
391 U1(U(mat).diffuse).r = r;
392 U2(U(mat).diffuse).g = g;
393 U3(U(mat).diffuse).b = b;
394 U4(U(mat).diffuse).a = a;
396 return create_material(device, &mat);
399 static IDirect3DMaterial3 *create_specular_material(IDirect3DDevice3 *device,
400 float r, float g, float b, float a, float power)
402 D3DMATERIAL mat;
404 memset(&mat, 0, sizeof(mat));
405 mat.dwSize = sizeof(mat);
406 U1(U2(mat).specular).r = r;
407 U2(U2(mat).specular).g = g;
408 U3(U2(mat).specular).b = b;
409 U4(U2(mat).specular).a = a;
410 U4(mat).power = power;
412 return create_material(device, &mat);
415 static IDirect3DMaterial3 *create_emissive_material(IDirect3DDevice3 *device, float r, float g, float b, float a)
417 D3DMATERIAL mat;
419 memset(&mat, 0, sizeof(mat));
420 mat.dwSize = sizeof(mat);
421 U1(U3(mat).emissive).r = r;
422 U2(U3(mat).emissive).g = g;
423 U3(U3(mat).emissive).b = b;
424 U4(U3(mat).emissive).a = a;
426 return create_material(device, &mat);
429 static void destroy_material(IDirect3DMaterial3 *material)
431 IDirect3DMaterial3_Release(material);
434 struct message
436 UINT message;
437 BOOL check_wparam;
438 WPARAM expect_wparam;
441 static const struct message *expect_messages;
443 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
445 if (expect_messages && message == expect_messages->message)
447 if (expect_messages->check_wparam)
448 ok (wparam == expect_messages->expect_wparam,
449 "Got unexpected wparam %lx for message %x, expected %lx.\n",
450 wparam, message, expect_messages->expect_wparam);
452 ++expect_messages;
455 return DefWindowProcA(hwnd, message, wparam, lparam);
458 /* Set the wndproc back to what ddraw expects it to be, and release the ddraw
459 * interface. This prevents subsequent SetCooperativeLevel() calls on a
460 * different window from failing with DDERR_HWNDALREADYSET. */
461 static void fix_wndproc(HWND window, LONG_PTR proc)
463 IDirectDraw4 *ddraw;
464 HRESULT hr;
466 if (!(ddraw = create_ddraw()))
467 return;
469 SetWindowLongPtrA(window, GWLP_WNDPROC, proc);
470 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
471 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
472 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
473 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
475 IDirectDraw4_Release(ddraw);
478 static void test_process_vertices(void)
480 IDirect3DVertexBuffer *src_vb, *dst_vb;
481 IDirect3DViewport3 *viewport;
482 D3DVERTEXBUFFERDESC vb_desc;
483 IDirect3DDevice3 *device;
484 struct vec3 *src_data;
485 struct vec4 *dst_data;
486 IDirect3D3 *d3d3;
487 D3DVIEWPORT2 vp2;
488 D3DVIEWPORT vp1;
489 HWND window;
490 HRESULT hr;
492 static D3DMATRIX identity =
494 1.0f, 0.0f, 0.0f, 0.0f,
495 0.0f, 1.0f, 0.0f, 0.0f,
496 0.0f, 0.0f, 1.0f, 0.0f,
497 0.0f, 0.0f, 0.0f, 1.0f,
499 static D3DMATRIX projection =
501 1.0f, 0.0f, 0.0f, 0.0f,
502 0.0f, 1.0f, 0.0f, 0.0f,
503 0.0f, 0.0f, 1.0f, 0.0f,
504 6.0f, 7.0f, 8.0f, 1.0f,
507 window = CreateWindowA("static", "d3d7_test", WS_OVERLAPPEDWINDOW,
508 0, 0, 640, 480, 0, 0, 0, 0);
509 if (!(device = create_device(window, DDSCL_NORMAL)))
511 skip("Failed to create a 3D device, skipping test.\n");
512 DestroyWindow(window);
513 return;
516 hr = IDirect3DDevice3_GetDirect3D(device, &d3d3);
517 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
519 memset(&vb_desc, 0, sizeof(vb_desc));
520 vb_desc.dwSize = sizeof(vb_desc);
521 vb_desc.dwFVF = D3DFVF_XYZ;
522 vb_desc.dwNumVertices = 3;
523 hr = IDirect3D3_CreateVertexBuffer(d3d3, &vb_desc, &src_vb, 0, NULL);
524 ok(SUCCEEDED(hr), "Failed to create source vertex buffer, hr %#x.\n", hr);
526 hr = IDirect3DVertexBuffer_Lock(src_vb, DDLOCK_WRITEONLY, (void **)&src_data, NULL);
527 ok(SUCCEEDED(hr), "Failed to lock source vertex buffer, hr %#x.\n", hr);
528 src_data[0].x = -1.0f;
529 src_data[0].y = -1.0f;
530 src_data[0].z = -1.0f;
531 src_data[1].x = 0.0f;
532 src_data[1].y = 0.0f;
533 src_data[1].z = 0.0f;
534 src_data[2].x = 1.0f;
535 src_data[2].y = 1.0f;
536 src_data[2].z = 1.0f;
537 hr = IDirect3DVertexBuffer_Unlock(src_vb);
538 ok(SUCCEEDED(hr), "Failed to unlock source vertex buffer, hr %#x.\n", hr);
540 memset(&vb_desc, 0, sizeof(vb_desc));
541 vb_desc.dwSize = sizeof(vb_desc);
542 vb_desc.dwFVF = D3DFVF_XYZRHW;
543 vb_desc.dwNumVertices = 3;
544 hr = IDirect3D3_CreateVertexBuffer(d3d3, &vb_desc, &dst_vb, 0, NULL);
545 ok(SUCCEEDED(hr), "Failed to create destination vertex buffer, hr %#x.\n", hr);
547 hr = IDirect3D3_CreateViewport(d3d3, &viewport, NULL);
548 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
549 hr = IDirect3DDevice3_AddViewport(device, viewport);
550 ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
551 vp2.dwSize = sizeof(vp2);
552 vp2.dwX = 10;
553 vp2.dwY = 20;
554 vp2.dwWidth = 100;
555 vp2.dwHeight = 200;
556 vp2.dvClipX = 2.0f;
557 vp2.dvClipY = 3.0f;
558 vp2.dvClipWidth = 4.0f;
559 vp2.dvClipHeight = 5.0f;
560 vp2.dvMinZ = -2.0f;
561 vp2.dvMaxZ = 3.0f;
562 hr = IDirect3DViewport3_SetViewport2(viewport, &vp2);
563 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
564 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
565 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
567 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &identity);
568 ok(SUCCEEDED(hr), "Failed to set world transformation, hr %#x.\n", hr);
569 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &identity);
570 ok(SUCCEEDED(hr), "Failed to set view transformation, hr %#x.\n", hr);
571 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &identity);
572 ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
574 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
575 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
577 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
578 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
579 ok(compare_vec4(&dst_data[0], -6.500e+1f, +1.800e+2f, +2.000e-1f, +1.000e+0f, 4096),
580 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
581 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
582 ok(compare_vec4(&dst_data[1], -4.000e+1f, +1.400e+2f, +4.000e-1f, +1.000e+0f, 4096),
583 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
584 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
585 ok(compare_vec4(&dst_data[2], -1.500e+1f, +1.000e+2f, +6.000e-1f, +1.000e+0f, 4096),
586 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
587 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
588 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
589 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
591 hr = IDirect3DDevice3_MultiplyTransform(device, D3DTRANSFORMSTATE_PROJECTION, &projection);
592 ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
594 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
595 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
597 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
598 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
599 ok(compare_vec4(&dst_data[0], +8.500e+1f, -1.000e+2f, +1.800e+0f, +1.000e+0f, 4096),
600 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
601 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
602 ok(compare_vec4(&dst_data[1], +1.100e+2f, -1.400e+2f, +2.000e+0f, +1.000e+0f, 4096),
603 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
604 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
605 ok(compare_vec4(&dst_data[2], +1.350e+2f, -1.800e+2f, +2.200e+0f, +1.000e+0f, 4096),
606 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
607 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
608 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
609 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
611 vp2.dwSize = sizeof(vp2);
612 vp2.dwX = 30;
613 vp2.dwY = 40;
614 vp2.dwWidth = 90;
615 vp2.dwHeight = 80;
616 vp2.dvClipX = 4.0f;
617 vp2.dvClipY = 6.0f;
618 vp2.dvClipWidth = 2.0f;
619 vp2.dvClipHeight = 4.0f;
620 vp2.dvMinZ = 3.0f;
621 vp2.dvMaxZ = -2.0f;
622 hr = IDirect3DViewport3_SetViewport2(viewport, &vp2);
623 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
625 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
626 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
628 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
629 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
630 ok(compare_vec4(&dst_data[0], +7.500e+1f, +4.000e+1f, -8.000e-1f, +1.000e+0f, 4096),
631 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
632 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
633 ok(compare_vec4(&dst_data[1], +1.200e+2f, +2.000e+1f, -1.000e+0f, +1.000e+0f, 4096),
634 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
635 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
636 ok(compare_vec4(&dst_data[2], +1.650e+2f, +0.000e+0f, -1.200e+0f, +1.000e+0f, 4096),
637 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
638 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
639 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
640 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
642 vp1.dwSize = sizeof(vp1);
643 vp1.dwX = 30;
644 vp1.dwY = 40;
645 vp1.dwWidth = 90;
646 vp1.dwHeight = 80;
647 vp1.dvScaleX = 7.0f;
648 vp1.dvScaleY = 2.0f;
649 vp1.dvMaxX = 6.0f;
650 vp1.dvMaxY = 10.0f;
651 vp1.dvMinZ = -2.0f;
652 vp1.dvMaxZ = 3.0f;
653 hr = IDirect3DViewport3_SetViewport(viewport, &vp1);
654 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
656 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
657 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
659 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
660 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
661 ok(compare_vec4(&dst_data[0], +1.100e+2f, +6.800e+1f, +7.000e+0f, +1.000e+0f, 4096),
662 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
663 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
664 ok(compare_vec4(&dst_data[1], +1.170e+2f, +6.600e+1f, +8.000e+0f, +1.000e+0f, 4096),
665 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
666 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
667 ok(compare_vec4(&dst_data[2], +1.240e+2f, +6.400e+1f, +9.000e+0f, +1.000e+0f, 4096),
668 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
669 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
670 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
671 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
673 hr = IDirect3DDevice3_DeleteViewport(device, viewport);
674 ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
676 IDirect3DVertexBuffer_Release(dst_vb);
677 IDirect3DVertexBuffer_Release(src_vb);
678 IDirect3DViewport3_Release(viewport);
679 IDirect3D3_Release(d3d3);
680 IDirect3DDevice3_Release(device);
681 DestroyWindow(window);
684 static void test_coop_level_create_device_window(void)
686 HWND focus_window, device_window;
687 IDirectDraw4 *ddraw;
688 HRESULT hr;
690 focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
691 0, 0, 640, 480, 0, 0, 0, 0);
692 ddraw = create_ddraw();
693 ok(!!ddraw, "Failed to create a ddraw object.\n");
695 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
696 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
697 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
698 ok(!device_window, "Unexpected device window found.\n");
699 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
700 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
701 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
702 ok(!device_window, "Unexpected device window found.\n");
703 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
704 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
705 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
706 ok(!device_window, "Unexpected device window found.\n");
707 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
708 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
709 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
710 ok(!device_window, "Unexpected device window found.\n");
711 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
712 ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
713 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
714 ok(!device_window, "Unexpected device window found.\n");
716 /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
717 if (broken(hr == DDERR_INVALIDPARAMS))
719 win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
720 IDirectDraw4_Release(ddraw);
721 DestroyWindow(focus_window);
722 return;
725 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
726 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
727 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
728 ok(!device_window, "Unexpected device window found.\n");
729 hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
730 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
731 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
732 ok(!device_window, "Unexpected device window found.\n");
734 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
735 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
736 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
737 ok(!device_window, "Unexpected device window found.\n");
738 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
739 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
740 ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
741 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
742 ok(!!device_window, "Device window not found.\n");
744 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
745 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
746 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
747 ok(!device_window, "Unexpected device window found.\n");
748 hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
749 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
750 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
751 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
752 ok(!!device_window, "Device window not found.\n");
754 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
755 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
756 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
757 ok(!device_window, "Unexpected device window found.\n");
758 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
759 ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
760 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
761 ok(!device_window, "Unexpected device window found.\n");
762 hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
763 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
764 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
765 ok(!device_window, "Unexpected device window found.\n");
766 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
767 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
768 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
769 ok(!!device_window, "Device window not found.\n");
771 IDirectDraw4_Release(ddraw);
772 DestroyWindow(focus_window);
775 static void test_clipper_blt(void)
777 IDirectDrawSurface4 *src_surface, *dst_surface;
778 RECT client_rect, src_rect;
779 IDirectDrawClipper *clipper;
780 DDSURFACEDESC2 surface_desc;
781 unsigned int i, j, x, y;
782 IDirectDraw4 *ddraw;
783 RGNDATA *rgn_data;
784 D3DCOLOR color;
785 ULONG refcount;
786 HRGN r1, r2;
787 HWND window;
788 DDBLTFX fx;
789 HRESULT hr;
790 DWORD *ptr;
791 DWORD ret;
793 static const DWORD src_data[] =
795 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
796 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
797 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
799 static const D3DCOLOR expected1[] =
801 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
802 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
803 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
804 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
806 /* Nvidia on Windows seems to have an off-by-one error
807 * when processing source rectangles. Our left = 1 and
808 * right = 5 input reads from x = {1, 2, 3}. x = 4 is
809 * read as well, but only for the edge pixels on the
810 * output image. The bug happens on the y axis as well,
811 * but we only read one row there, and all source rows
812 * contain the same data. This bug is not dependent on
813 * the presence of a clipper. */
814 static const D3DCOLOR expected1_broken[] =
816 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
817 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
818 0x00000000, 0x00000000, 0x00ff0000, 0x00ff0000,
819 0x00000000, 0x00000000, 0x0000ff00, 0x00ff0000,
821 static const D3DCOLOR expected2[] =
823 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
824 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
825 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
826 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
829 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
830 10, 10, 640, 480, 0, 0, 0, 0);
831 ShowWindow(window, SW_SHOW);
832 ddraw = create_ddraw();
833 ok(!!ddraw, "Failed to create a ddraw object.\n");
835 ret = GetClientRect(window, &client_rect);
836 ok(ret, "Failed to get client rect.\n");
837 ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
838 ok(ret, "Failed to map client rect.\n");
840 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
841 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
843 hr = IDirectDraw4_CreateClipper(ddraw, 0, &clipper, NULL);
844 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
845 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
846 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
847 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
848 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
849 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
850 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
851 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
852 hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
853 ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
854 ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
855 ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
856 ok(rgn_data->rdh.nCount >= 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
857 ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
858 "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
859 rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
860 rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
861 client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
862 HeapFree(GetProcessHeap(), 0, rgn_data);
864 r1 = CreateRectRgn(0, 0, 320, 240);
865 ok(!!r1, "Failed to create region.\n");
866 r2 = CreateRectRgn(320, 240, 640, 480);
867 ok(!!r2, "Failed to create region.\n");
868 CombineRgn(r1, r1, r2, RGN_OR);
869 ret = GetRegionData(r1, 0, NULL);
870 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
871 ret = GetRegionData(r1, ret, rgn_data);
872 ok(!!ret, "Failed to get region data.\n");
874 DeleteObject(r2);
875 DeleteObject(r1);
877 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
878 ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
879 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
880 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
881 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
882 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
884 HeapFree(GetProcessHeap(), 0, rgn_data);
886 memset(&surface_desc, 0, sizeof(surface_desc));
887 surface_desc.dwSize = sizeof(surface_desc);
888 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
889 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
890 surface_desc.dwWidth = 640;
891 surface_desc.dwHeight = 480;
892 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
893 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
894 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
895 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
896 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
897 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
899 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
900 ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
901 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
902 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
904 memset(&fx, 0, sizeof(fx));
905 fx.dwSize = sizeof(fx);
906 hr = IDirectDrawSurface4_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
907 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
908 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
909 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
911 hr = IDirectDrawSurface4_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
912 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
913 ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
914 ptr = surface_desc.lpSurface;
915 memcpy(&ptr[ 0], &src_data[ 0], 6 * sizeof(DWORD));
916 memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
917 memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
918 hr = IDirectDrawSurface4_Unlock(src_surface, NULL);
919 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
921 hr = IDirectDrawSurface4_SetClipper(dst_surface, clipper);
922 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
924 SetRect(&src_rect, 1, 1, 5, 2);
925 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
926 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
927 for (i = 0; i < 4; ++i)
929 for (j = 0; j < 4; ++j)
931 x = 80 * ((2 * j) + 1);
932 y = 60 * ((2 * i) + 1);
933 color = get_surface_color(dst_surface, x, y);
934 ok(compare_color(color, expected1[i * 4 + j], 1)
935 || broken(compare_color(color, expected1_broken[i * 4 + j], 1)),
936 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
940 U5(fx).dwFillColor = 0xff0000ff;
941 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
942 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
943 for (i = 0; i < 4; ++i)
945 for (j = 0; j < 4; ++j)
947 x = 80 * ((2 * j) + 1);
948 y = 60 * ((2 * i) + 1);
949 color = get_surface_color(dst_surface, x, y);
950 ok(compare_color(color, expected2[i * 4 + j], 1),
951 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
955 hr = IDirectDrawSurface4_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
956 ok(hr == DDERR_BLTFASTCANTCLIP, "Got unexpected hr %#x.\n", hr);
958 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
959 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
960 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
961 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
962 DestroyWindow(window);
963 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
964 ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
965 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
966 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
967 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
968 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
969 hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
970 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
971 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
972 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
973 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
974 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
976 IDirectDrawSurface4_Release(dst_surface);
977 IDirectDrawSurface4_Release(src_surface);
978 refcount = IDirectDrawClipper_Release(clipper);
979 ok(!refcount, "Clipper has %u references left.\n", refcount);
980 IDirectDraw4_Release(ddraw);
983 static void test_coop_level_d3d_state(void)
985 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
986 IDirectDrawSurface4 *rt, *surface;
987 IDirect3DViewport3 *viewport;
988 IDirect3DDevice3 *device;
989 IDirectDraw4 *ddraw;
990 IDirect3D3 *d3d;
991 D3DCOLOR color;
992 DWORD value;
993 HWND window;
994 HRESULT hr;
996 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
997 0, 0, 640, 480, 0, 0, 0, 0);
998 if (!(device = create_device(window, DDSCL_NORMAL)))
1000 skip("Failed to create a 3D device, skipping test.\n");
1001 DestroyWindow(window);
1002 return;
1005 viewport = create_viewport(device, 0, 0, 640, 480);
1007 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1008 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1009 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
1010 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1011 ok(!!value, "Got unexpected z-enable state %#x.\n", value);
1012 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
1013 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1014 ok(!value, "Got unexpected alpha blend enable state %#x.\n", value);
1015 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
1016 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
1017 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
1018 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1019 color = get_surface_color(rt, 320, 240);
1020 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
1022 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1023 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1024 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1025 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1026 IDirect3D3_Release(d3d);
1027 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1028 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1029 hr = IDirectDrawSurface4_IsLost(rt);
1030 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
1031 hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
1032 ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
1033 IDirectDraw4_Release(ddraw);
1035 hr = IDirect3DDevice3_GetRenderTarget(device, &surface);
1036 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1037 ok(surface == rt, "Got unexpected surface %p.\n", surface);
1038 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
1039 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1040 ok(!!value, "Got unexpected z-enable state %#x.\n", value);
1041 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
1042 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1043 ok(!!value, "Got unexpected alpha blend enable state %#x.\n", value);
1044 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 0.0f, 0);
1045 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1046 color = get_surface_color(rt, 320, 240);
1047 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1049 destroy_viewport(device, viewport);
1050 IDirectDrawSurface4_Release(surface);
1051 IDirectDrawSurface4_Release(rt);
1052 IDirect3DDevice3_Release(device);
1053 DestroyWindow(window);
1056 static void test_surface_interface_mismatch(void)
1058 IDirectDraw4 *ddraw = NULL;
1059 IDirect3D3 *d3d = NULL;
1060 IDirectDrawSurface4 *surface = NULL, *ds;
1061 IDirectDrawSurface3 *surface3 = NULL;
1062 IDirect3DDevice3 *device = NULL;
1063 IDirect3DViewport3 *viewport = NULL;
1064 DDSURFACEDESC2 surface_desc;
1065 DDPIXELFORMAT z_fmt;
1066 ULONG refcount;
1067 HRESULT hr;
1068 D3DCOLOR color;
1069 HWND window;
1070 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1072 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1073 0, 0, 640, 480, 0, 0, 0, 0);
1074 ddraw = create_ddraw();
1075 ok(!!ddraw, "Failed to create a ddraw object.\n");
1076 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
1077 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1079 memset(&surface_desc, 0, sizeof(surface_desc));
1080 surface_desc.dwSize = sizeof(surface_desc);
1081 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1082 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
1083 surface_desc.dwWidth = 640;
1084 surface_desc.dwHeight = 480;
1086 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1087 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1089 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
1090 ok(SUCCEEDED(hr), "Failed to QI IDirectDrawSurface3, hr %#x.\n", hr);
1092 if (FAILED(IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d)))
1094 skip("D3D interface is not available, skipping test.\n");
1095 goto cleanup;
1098 memset(&z_fmt, 0, sizeof(z_fmt));
1099 hr = IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
1100 if (FAILED(hr) || !z_fmt.dwSize)
1102 skip("No depth buffer formats available, skipping test.\n");
1103 goto cleanup;
1106 memset(&surface_desc, 0, sizeof(surface_desc));
1107 surface_desc.dwSize = sizeof(surface_desc);
1108 surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
1109 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
1110 U4(surface_desc).ddpfPixelFormat = z_fmt;
1111 surface_desc.dwWidth = 640;
1112 surface_desc.dwHeight = 480;
1113 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &ds, NULL);
1114 ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
1115 if (FAILED(hr))
1116 goto cleanup;
1118 /* Using a different surface interface version still works */
1119 hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
1120 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
1121 refcount = IDirectDrawSurface4_Release(ds);
1122 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
1123 if (FAILED(hr))
1124 goto cleanup;
1126 /* Here too */
1127 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, (IDirectDrawSurface4 *)surface3, &device, NULL);
1128 ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
1129 if (FAILED(hr))
1130 goto cleanup;
1132 viewport = create_viewport(device, 0, 0, 640, 480);
1134 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
1135 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1136 color = get_surface_color(surface, 320, 240);
1137 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
1139 cleanup:
1140 if (viewport)
1141 destroy_viewport(device, viewport);
1142 if (surface3) IDirectDrawSurface3_Release(surface3);
1143 if (surface) IDirectDrawSurface4_Release(surface);
1144 if (device) IDirect3DDevice3_Release(device);
1145 if (d3d) IDirect3D3_Release(d3d);
1146 if (ddraw) IDirectDraw4_Release(ddraw);
1147 DestroyWindow(window);
1150 static void test_coop_level_threaded(void)
1152 struct create_window_thread_param p;
1153 IDirectDraw4 *ddraw;
1154 HRESULT hr;
1156 ddraw = create_ddraw();
1157 ok(!!ddraw, "Failed to create a ddraw object.\n");
1158 create_window_thread(&p);
1160 hr = IDirectDraw4_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1161 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1163 IDirectDraw4_Release(ddraw);
1164 destroy_window_thread(&p);
1167 static void test_depth_blit(void)
1169 static struct
1171 float x, y, z;
1172 DWORD color;
1174 quad1[] =
1176 { -1.0, 1.0, 0.50f, 0xff00ff00},
1177 { 1.0, 1.0, 0.50f, 0xff00ff00},
1178 { -1.0, -1.0, 0.50f, 0xff00ff00},
1179 { 1.0, -1.0, 0.50f, 0xff00ff00},
1181 static const D3DCOLOR expected_colors[4][4] =
1183 {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1184 {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1185 {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1186 {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1188 DDSURFACEDESC2 ddsd_new, ddsd_existing;
1190 IDirect3DDevice3 *device;
1191 IDirectDrawSurface4 *ds1, *ds2, *ds3, *rt;
1192 IDirect3DViewport3 *viewport;
1193 RECT src_rect, dst_rect;
1194 unsigned int i, j;
1195 D3DCOLOR color;
1196 HRESULT hr;
1197 IDirect3D3 *d3d;
1198 IDirectDraw4 *ddraw;
1199 DDBLTFX fx;
1200 HWND window;
1201 D3DRECT d3drect;
1203 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1204 0, 0, 640, 480, 0, 0, 0, 0);
1205 if (!(device = create_device(window, DDSCL_NORMAL)))
1207 skip("Failed to create a 3D device, skipping test.\n");
1208 DestroyWindow(window);
1209 return;
1212 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1213 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
1214 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1215 ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
1216 IDirect3D3_Release(d3d);
1218 ds1 = get_depth_stencil(device);
1220 memset(&ddsd_new, 0, sizeof(ddsd_new));
1221 ddsd_new.dwSize = sizeof(ddsd_new);
1222 memset(&ddsd_existing, 0, sizeof(ddsd_existing));
1223 ddsd_existing.dwSize = sizeof(ddsd_existing);
1224 hr = IDirectDrawSurface4_GetSurfaceDesc(ds1, &ddsd_existing);
1225 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
1226 ddsd_new.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
1227 ddsd_new.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
1228 ddsd_new.dwWidth = ddsd_existing.dwWidth;
1229 ddsd_new.dwHeight = ddsd_existing.dwHeight;
1230 U4(ddsd_new).ddpfPixelFormat = U4(ddsd_existing).ddpfPixelFormat;
1231 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd_new, &ds2, NULL);
1232 ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
1233 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd_new, &ds3, NULL);
1234 ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
1235 IDirectDraw4_Release(ddraw);
1237 viewport = create_viewport(device, 0, 0, ddsd_existing.dwWidth, ddsd_existing.dwHeight);
1238 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1239 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
1241 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
1242 ok(SUCCEEDED(hr), "Failed to enable z testing, hr %#x.\n", hr);
1243 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL);
1244 ok(SUCCEEDED(hr), "Failed to set the z function, hr %#x.\n", hr);
1246 U1(d3drect).x1 = U2(d3drect).y1 = 0;
1247 U3(d3drect).x2 = ddsd_existing.dwWidth; U4(d3drect).y2 = ddsd_existing.dwHeight;
1248 hr = IDirect3DViewport3_Clear2(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER, 0, 0.0f, 0);
1249 ok(SUCCEEDED(hr), "Failed to clear the z buffer, hr %#x.\n", hr);
1251 /* Partial blit. */
1252 SetRect(&src_rect, 0, 0, 320, 240);
1253 SetRect(&dst_rect, 0, 0, 320, 240);
1254 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1255 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1256 /* Different locations. */
1257 SetRect(&src_rect, 0, 0, 320, 240);
1258 SetRect(&dst_rect, 320, 240, 640, 480);
1259 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1260 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1261 /* Streched. */
1262 SetRect(&src_rect, 0, 0, 320, 240);
1263 SetRect(&dst_rect, 0, 0, 640, 480);
1264 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1265 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1266 /* Flipped. */
1267 SetRect(&src_rect, 0, 480, 640, 0);
1268 SetRect(&dst_rect, 0, 0, 640, 480);
1269 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1270 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1271 SetRect(&src_rect, 0, 0, 640, 480);
1272 SetRect(&dst_rect, 0, 480, 640, 0);
1273 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1274 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1275 /* Full, explicit. */
1276 SetRect(&src_rect, 0, 0, 640, 480);
1277 SetRect(&dst_rect, 0, 0, 640, 480);
1278 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1279 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1280 /* Depth -> color blit: Succeeds on Win7 + Radeon HD 5700, fails on WinXP + Radeon X1600 */
1282 /* Depth blit inside a BeginScene / EndScene pair */
1283 hr = IDirect3DDevice3_BeginScene(device);
1284 ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1285 /* From the current depth stencil */
1286 hr = IDirectDrawSurface4_Blt(ds2, NULL, ds1, NULL, DDBLT_WAIT, NULL);
1287 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1288 /* To the current depth stencil */
1289 hr = IDirectDrawSurface4_Blt(ds1, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1290 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1291 /* Between unbound surfaces */
1292 hr = IDirectDrawSurface4_Blt(ds3, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1293 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1294 hr = IDirect3DDevice3_EndScene(device);
1295 ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1297 /* Avoid changing the depth stencil, it doesn't work properly on Windows.
1298 * Instead use DDBLT_DEPTHFILL to clear the depth stencil. Unfortunately
1299 * drivers disagree on the meaning of dwFillDepth. Only 0 seems to produce
1300 * a reliable result(z = 0.0) */
1301 memset(&fx, 0, sizeof(fx));
1302 fx.dwSize = sizeof(fx);
1303 hr = IDirectDrawSurface4_Blt(ds2, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
1304 ok(SUCCEEDED(hr), "Failed to clear the source z buffer, hr %#x.\n", hr);
1306 hr = IDirect3DViewport3_Clear2(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET, 0xffff0000, 1.0f, 0);
1307 ok(SUCCEEDED(hr), "Failed to clear the color and z buffers, hr %#x.\n", hr);
1308 SetRect(&dst_rect, 0, 0, 320, 240);
1309 hr = IDirectDrawSurface4_Blt(ds1, &dst_rect, ds2, NULL, DDBLT_WAIT, NULL);
1310 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1311 IDirectDrawSurface4_Release(ds3);
1312 IDirectDrawSurface4_Release(ds2);
1313 IDirectDrawSurface4_Release(ds1);
1315 hr = IDirect3DDevice3_BeginScene(device);
1316 ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1317 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE,
1318 quad1, 4, 0);
1319 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1320 hr = IDirect3DDevice3_EndScene(device);
1321 ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1323 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1324 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1325 for (i = 0; i < 4; ++i)
1327 for (j = 0; j < 4; ++j)
1329 unsigned int x = 80 * ((2 * j) + 1);
1330 unsigned int y = 60 * ((2 * i) + 1);
1331 color = get_surface_color(rt, x, y);
1332 ok(compare_color(color, expected_colors[i][j], 1),
1333 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected_colors[i][j], x, y, color);
1336 IDirectDrawSurface4_Release(rt);
1338 destroy_viewport(device, viewport);
1339 IDirect3DDevice3_Release(device);
1340 DestroyWindow(window);
1343 static void test_texture_load_ckey(void)
1345 IDirectDraw4 *ddraw;
1346 IDirectDrawSurface4 *src;
1347 IDirectDrawSurface4 *dst;
1348 IDirect3DTexture2 *src_tex;
1349 IDirect3DTexture2 *dst_tex;
1350 DDSURFACEDESC2 ddsd;
1351 HRESULT hr;
1352 DDCOLORKEY ckey;
1354 ddraw = create_ddraw();
1355 ok(!!ddraw, "Failed to create a ddraw object.\n");
1356 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
1357 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1359 memset(&ddsd, 0, sizeof(ddsd));
1360 ddsd.dwSize = sizeof(ddsd);
1361 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
1362 ddsd.dwHeight = 128;
1363 ddsd.dwWidth = 128;
1364 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
1365 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &src, NULL);
1366 ok(SUCCEEDED(hr), "Failed to create source texture, hr %#x.\n", hr);
1367 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1368 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &dst, NULL);
1369 ok(SUCCEEDED(hr), "Failed to create destination texture, hr %#x.\n", hr);
1371 hr = IDirectDrawSurface4_QueryInterface(src, &IID_IDirect3DTexture2, (void **)&src_tex);
1372 ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get Direct3DTexture2 interface, hr %#x.\n", hr);
1373 if (FAILED(hr))
1375 /* 64 bit ddraw does not support d3d */
1376 skip("Could not get Direct3DTexture2 interface, skipping texture::Load color keying tests.\n");
1377 IDirectDrawSurface4_Release(dst);
1378 IDirectDrawSurface4_Release(src);
1379 IDirectDraw4_Release(ddraw);
1380 return;
1382 hr = IDirectDrawSurface4_QueryInterface(dst, &IID_IDirect3DTexture2, (void **)&dst_tex);
1383 ok(SUCCEEDED(hr), "Failed to get Direct3DTexture2 interface, hr %#x.\n", hr);
1385 /* No surface has a color key */
1386 hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1387 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1388 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0xdeadbeef;
1389 hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1390 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1391 ok(ckey.dwColorSpaceLowValue == 0xdeadbeef, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1392 ok(ckey.dwColorSpaceHighValue == 0xdeadbeef, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1394 /* Source surface has a color key */
1395 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x0000ff00;
1396 hr = IDirectDrawSurface4_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
1397 ok(SUCCEEDED(hr), "Failed to set color key, 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 /* Both surfaces have a color key: Dest ckey is overwritten */
1406 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x000000ff;
1407 hr = IDirectDrawSurface4_SetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1408 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1409 hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1410 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1411 hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1412 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1413 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1414 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1416 /* Only the destination has a color key: It is not deleted */
1417 hr = IDirectDrawSurface4_SetColorKey(src, DDCKEY_SRCBLT, NULL);
1418 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1419 hr = IDirectDrawSurface4_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
1420 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1421 hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1422 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1423 hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1424 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1425 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1426 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1428 IDirect3DTexture2_Release(dst_tex);
1429 IDirect3DTexture2_Release(src_tex);
1430 IDirectDrawSurface4_Release(dst);
1431 IDirectDrawSurface4_Release(src);
1432 IDirectDraw4_Release(ddraw);
1435 static ULONG get_refcount(IUnknown *test_iface)
1437 IUnknown_AddRef(test_iface);
1438 return IUnknown_Release(test_iface);
1441 static void test_viewport(void)
1443 IDirectDraw4 *ddraw;
1444 IDirect3D3 *d3d;
1445 HRESULT hr, old_d3d_ref;
1446 ULONG ref;
1447 IDirect3DViewport *viewport;
1448 IDirect3DViewport2 *viewport2;
1449 IDirect3DViewport3 *viewport3, *another_vp, *test_vp;
1450 IDirectDrawGammaControl *gamma;
1451 IUnknown *unknown;
1452 HWND window;
1453 IDirect3DDevice3 *device;
1455 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1456 0, 0, 640, 480, 0, 0, 0, 0);
1457 if (!(device = create_device(window, DDSCL_NORMAL)))
1459 skip("Failed to create a 3D device, skipping test.\n");
1460 DestroyWindow(window);
1461 return;
1463 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1464 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
1465 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1466 ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
1467 old_d3d_ref = get_refcount((IUnknown *) d3d);
1469 hr = IDirect3D3_CreateViewport(d3d, &viewport3, NULL);
1470 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1471 ref = get_refcount((IUnknown *)viewport3);
1472 ok(ref == 1, "Initial IDirect3DViewport3 refcount is %u\n", ref);
1473 ref = get_refcount((IUnknown *)d3d);
1474 ok(ref == old_d3d_ref, "IDirect3D3 refcount is %u\n", ref);
1476 gamma = (IDirectDrawGammaControl *)0xdeadbeef;
1477 hr = IDirect3DViewport2_QueryInterface(viewport3, &IID_IDirectDrawGammaControl, (void **)&gamma);
1478 ok(hr == E_NOINTERFACE, "Got unexpected hr %#x.\n", hr);
1479 ok(gamma == NULL, "Interface not set to NULL by failed QI call: %p\n", gamma);
1480 if (SUCCEEDED(hr)) IDirectDrawGammaControl_Release(gamma);
1481 /* NULL iid: Segfaults */
1483 hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IDirect3DViewport, (void **)&viewport);
1484 ok(SUCCEEDED(hr), "Failed to QI IDirect3DViewport, hr %#x.\n", hr);
1485 if (viewport)
1487 ref = get_refcount((IUnknown *)viewport);
1488 ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1489 ref = get_refcount((IUnknown *)viewport3);
1490 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1491 IDirect3DViewport_Release(viewport);
1492 viewport = NULL;
1495 hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IDirect3DViewport3, (void **)&viewport2);
1496 ok(SUCCEEDED(hr), "Failed to QI IDirect3DViewport3, hr %#x.\n", hr);
1497 if (viewport2)
1499 ref = get_refcount((IUnknown *)viewport2);
1500 ok(ref == 2, "IDirect3DViewport2 refcount is %u\n", ref);
1501 ref = get_refcount((IUnknown *)viewport3);
1502 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1503 IDirect3DViewport3_Release(viewport2);
1506 hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IUnknown, (void **)&unknown);
1507 ok(SUCCEEDED(hr), "Failed to QI IUnknown, hr %#x.\n", hr);
1508 if (unknown)
1510 ref = get_refcount((IUnknown *)viewport3);
1511 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1512 ref = get_refcount(unknown);
1513 ok(ref == 2, "IUnknown refcount is %u\n", ref);
1514 IUnknown_Release(unknown);
1517 /* AddViewport(NULL): Segfault */
1518 hr = IDirect3DDevice3_DeleteViewport(device, NULL);
1519 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
1520 hr = IDirect3DDevice3_GetCurrentViewport(device, NULL);
1521 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1523 hr = IDirect3D3_CreateViewport(d3d, &another_vp, NULL);
1524 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1526 /* Setting a viewport not in the viewport list fails */
1527 hr = IDirect3DDevice3_SetCurrentViewport(device, another_vp);
1528 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
1530 hr = IDirect3DDevice3_AddViewport(device, viewport3);
1531 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1532 ref = get_refcount((IUnknown *) viewport3);
1533 ok(ref == 2, "viewport3 refcount is %d\n", ref);
1534 hr = IDirect3DDevice3_AddViewport(device, another_vp);
1535 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1536 ref = get_refcount((IUnknown *) another_vp);
1537 ok(ref == 2, "another_vp refcount is %d\n", ref);
1539 test_vp = (IDirect3DViewport3 *) 0xbaadc0de;
1540 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1541 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1542 ok(test_vp == (IDirect3DViewport3 *) 0xbaadc0de, "Got unexpected pointer %p\n", test_vp);
1544 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport3);
1545 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1546 ref = get_refcount((IUnknown *) viewport3);
1547 ok(ref == 3, "viewport3 refcount is %d\n", ref);
1548 ref = get_refcount((IUnknown *) device);
1549 ok(ref == 1, "device refcount is %d\n", ref);
1551 test_vp = NULL;
1552 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1553 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
1554 ok(test_vp == viewport3, "Got unexpected viewport %p\n", test_vp);
1555 ref = get_refcount((IUnknown *) viewport3);
1556 ok(ref == 4, "viewport3 refcount is %d\n", ref);
1557 if(test_vp) IDirect3DViewport3_Release(test_vp);
1559 /* GetCurrentViewport with a viewport set and NULL input param: Segfault */
1561 /* Cannot set the viewport to NULL */
1562 hr = IDirect3DDevice3_SetCurrentViewport(device, NULL);
1563 ok(hr == DDERR_INVALIDPARAMS, "Failed to set viewport to NULL, hr %#x.\n", hr);
1564 test_vp = NULL;
1565 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1566 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
1567 ok(test_vp == viewport3, "Got unexpected viewport %p\n", test_vp);
1568 if(test_vp) IDirect3DViewport3_Release(test_vp);
1570 /* SetCurrentViewport properly releases the old viewport's reference */
1571 hr = IDirect3DDevice3_SetCurrentViewport(device, another_vp);
1572 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1573 ref = get_refcount((IUnknown *) viewport3);
1574 ok(ref == 2, "viewport3 refcount is %d\n", ref);
1575 ref = get_refcount((IUnknown *) another_vp);
1576 ok(ref == 3, "another_vp refcount is %d\n", ref);
1578 /* Unlike device2::DeleteViewport, device3::DeleteViewport releases the
1579 * reference held by SetCurrentViewport */
1580 hr = IDirect3DDevice3_DeleteViewport(device, another_vp);
1581 ok(SUCCEEDED(hr), "Failed to delete viewport from device, hr %#x.\n", hr);
1582 ref = get_refcount((IUnknown *) another_vp);
1583 ok(ref == 1, "another_vp refcount is %d\n", ref);
1585 /* GetCurrentViewport still fails */
1586 test_vp = NULL;
1587 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1588 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1589 ok(test_vp == NULL, "Got unexpected viewport %p\n", test_vp);
1590 if(test_vp) IDirect3DViewport3_Release(test_vp);
1592 /* Setting a different viewport doesn't have any surprises now */
1593 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport3);
1594 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1595 ref = get_refcount((IUnknown *) viewport3);
1596 ok(ref == 3, "viewport3 refcount is %d\n", ref);
1597 ref = get_refcount((IUnknown *) another_vp);
1598 ok(ref == 1, "another_vp refcount is %d\n", ref);
1600 /* Destroying the device removes the viewport and releases the reference */
1601 IDirect3DDevice3_Release(device);
1602 ref = get_refcount((IUnknown *) viewport3);
1603 ok(ref == 1, "viewport3 refcount is %d\n", ref);
1605 ref = IDirect3DViewport3_Release(another_vp);
1606 ok(ref == 0, "Got unexpected ref %d\n", ref);
1607 ref = IDirect3DViewport3_Release(viewport3);
1608 ok(ref == 0, "Got unexpected ref %d\n", ref);
1609 IDirect3D3_Release(d3d);
1610 DestroyWindow(window);
1611 IDirectDraw4_Release(ddraw);
1614 static void test_zenable(void)
1616 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1617 static struct
1619 struct vec4 position;
1620 D3DCOLOR diffuse;
1622 tquad[] =
1624 {{ 0.0f, 480.0f, -0.5f, 1.0f}, 0xff00ff00},
1625 {{ 0.0f, 0.0f, -0.5f, 1.0f}, 0xff00ff00},
1626 {{640.0f, 480.0f, 1.5f, 1.0f}, 0xff00ff00},
1627 {{640.0f, 0.0f, 1.5f, 1.0f}, 0xff00ff00},
1629 IDirect3DViewport3 *viewport;
1630 IDirect3DDevice3 *device;
1631 IDirectDrawSurface4 *rt;
1632 D3DCOLOR color;
1633 HWND window;
1634 HRESULT hr;
1635 UINT x, y;
1636 UINT i, j;
1638 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1639 0, 0, 640, 480, 0, 0, 0, 0);
1640 if (!(device = create_device(window, DDSCL_NORMAL)))
1642 skip("Failed to create a 3D device, skipping test.\n");
1643 DestroyWindow(window);
1644 return;
1647 viewport = create_viewport(device, 0, 0, 640, 480);
1648 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1649 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1651 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
1652 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
1654 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff0000, 0.0f, 0);
1655 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1656 hr = IDirect3DDevice3_BeginScene(device);
1657 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1658 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, tquad, 4, 0);
1659 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1660 hr = IDirect3DDevice3_EndScene(device);
1661 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1663 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1664 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1665 for (i = 0; i < 4; ++i)
1667 for (j = 0; j < 4; ++j)
1669 x = 80 * ((2 * j) + 1);
1670 y = 60 * ((2 * i) + 1);
1671 color = get_surface_color(rt, x, y);
1672 ok(compare_color(color, 0x0000ff00, 1),
1673 "Expected color 0x0000ff00 at %u, %u, got 0x%08x.\n", x, y, color);
1676 IDirectDrawSurface4_Release(rt);
1678 destroy_viewport(device, viewport);
1679 IDirect3DDevice3_Release(device);
1680 DestroyWindow(window);
1683 static void test_ck_rgba(void)
1685 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1686 static struct
1688 struct vec4 position;
1689 struct vec2 texcoord;
1691 tquad[] =
1693 {{ 0.0f, 480.0f, 0.25f, 1.0f}, {0.0f, 0.0f}},
1694 {{ 0.0f, 0.0f, 0.25f, 1.0f}, {0.0f, 1.0f}},
1695 {{640.0f, 480.0f, 0.25f, 1.0f}, {1.0f, 0.0f}},
1696 {{640.0f, 0.0f, 0.25f, 1.0f}, {1.0f, 1.0f}},
1697 {{ 0.0f, 480.0f, 0.75f, 1.0f}, {0.0f, 0.0f}},
1698 {{ 0.0f, 0.0f, 0.75f, 1.0f}, {0.0f, 1.0f}},
1699 {{640.0f, 480.0f, 0.75f, 1.0f}, {1.0f, 0.0f}},
1700 {{640.0f, 0.0f, 0.75f, 1.0f}, {1.0f, 1.0f}},
1702 static const struct
1704 D3DCOLOR fill_color;
1705 BOOL color_key;
1706 BOOL blend;
1707 D3DCOLOR result1, result1_broken;
1708 D3DCOLOR result2, result2_broken;
1710 tests[] =
1712 /* r200 on Windows doesn't check the alpha component when applying the color
1713 * key, so the key matches on every texel. */
1714 {0xff00ff00, TRUE, TRUE, 0x00ff0000, 0x00ff0000, 0x000000ff, 0x000000ff},
1715 {0xff00ff00, TRUE, FALSE, 0x00ff0000, 0x00ff0000, 0x000000ff, 0x000000ff},
1716 {0xff00ff00, FALSE, TRUE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1717 {0xff00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1718 {0x7f00ff00, TRUE, TRUE, 0x00807f00, 0x00ff0000, 0x00807f00, 0x000000ff},
1719 {0x7f00ff00, TRUE, FALSE, 0x0000ff00, 0x00ff0000, 0x0000ff00, 0x000000ff},
1720 {0x7f00ff00, FALSE, TRUE, 0x00807f00, 0x00807f00, 0x00807f00, 0x00807f00},
1721 {0x7f00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1724 IDirectDrawSurface4 *surface;
1725 IDirect3DViewport3 *viewport;
1726 DDSURFACEDESC2 surface_desc;
1727 IDirect3DTexture2 *texture;
1728 IDirect3DDevice3 *device;
1729 IDirectDrawSurface4 *rt;
1730 IDirectDraw4 *ddraw;
1731 IDirect3D3 *d3d;
1732 D3DCOLOR color;
1733 HWND window;
1734 DDBLTFX fx;
1735 HRESULT hr;
1736 UINT i;
1738 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1739 0, 0, 640, 480, 0, 0, 0, 0);
1740 if (!(device = create_device(window, DDSCL_NORMAL)))
1742 skip("Failed to create a 3D device, skipping test.\n");
1743 DestroyWindow(window);
1744 return;
1747 viewport = create_viewport(device, 0, 0, 640, 480);
1748 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1749 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1751 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1752 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1753 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1754 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1755 IDirect3D3_Release(d3d);
1757 memset(&surface_desc, 0, sizeof(surface_desc));
1758 surface_desc.dwSize = sizeof(surface_desc);
1759 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1760 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1761 surface_desc.dwWidth = 256;
1762 surface_desc.dwHeight = 256;
1763 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
1764 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1765 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
1766 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1767 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1768 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
1769 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
1770 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0xff00ff00;
1771 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0xff00ff00;
1772 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1773 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
1774 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
1775 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1777 hr = IDirect3DDevice3_SetTexture(device, 0, texture);
1778 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
1779 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
1780 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1781 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
1782 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1784 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1785 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1787 for (i = 0; i < sizeof(tests) / sizeof(*tests); ++i)
1789 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, tests[i].color_key);
1790 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
1791 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, tests[i].blend);
1792 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1794 memset(&fx, 0, sizeof(fx));
1795 fx.dwSize = sizeof(fx);
1796 U5(fx).dwFillColor = tests[i].fill_color;
1797 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1798 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1800 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect,
1801 D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff0000, 1.0f, 0);
1802 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1803 hr = IDirect3DDevice3_BeginScene(device);
1804 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1805 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1806 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1807 hr = IDirect3DDevice3_EndScene(device);
1808 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1810 color = get_surface_color(rt, 320, 240);
1811 ok(compare_color(color, tests[i].result1, 1) || compare_color(color, tests[i].result1_broken, 1),
1812 "Expected color 0x%08x for test %u, got 0x%08x.\n",
1813 tests[i].result1, i, color);
1815 U5(fx).dwFillColor = 0xff0000ff;
1816 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1817 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1819 hr = IDirect3DDevice3_BeginScene(device);
1820 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1821 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[4], 4, 0);
1822 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1823 hr = IDirect3DDevice3_EndScene(device);
1824 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1826 /* This tests that fragments that are masked out by the color key are
1827 * discarded, instead of just fully transparent. */
1828 color = get_surface_color(rt, 320, 240);
1829 ok(compare_color(color, tests[i].result2, 1) || compare_color(color, tests[i].result2_broken, 1),
1830 "Expected color 0x%08x for test %u, got 0x%08x.\n",
1831 tests[i].result2, i, color);
1834 IDirectDrawSurface4_Release(rt);
1835 IDirect3DTexture2_Release(texture);
1836 IDirectDrawSurface4_Release(surface);
1837 destroy_viewport(device, viewport);
1838 IDirectDraw4_Release(ddraw);
1839 IDirect3DDevice3_Release(device);
1840 DestroyWindow(window);
1843 static void test_ck_default(void)
1845 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1846 static struct
1848 struct vec4 position;
1849 struct vec2 texcoord;
1851 tquad[] =
1853 {{ 0.0f, 480.0f, 0.0f, 1.0f}, {0.0f, 0.0f}},
1854 {{ 0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 1.0f}},
1855 {{640.0f, 480.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
1856 {{640.0f, 0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
1858 IDirectDrawSurface4 *surface, *rt;
1859 IDirect3DViewport3 *viewport;
1860 DDSURFACEDESC2 surface_desc;
1861 IDirect3DTexture2 *texture;
1862 IDirect3DDevice3 *device;
1863 IDirectDraw4 *ddraw;
1864 IDirect3D3 *d3d;
1865 D3DCOLOR color;
1866 DWORD value;
1867 HWND window;
1868 DDBLTFX fx;
1869 HRESULT hr;
1871 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1872 0, 0, 640, 480, 0, 0, 0, 0);
1874 if (!(device = create_device(window, DDSCL_NORMAL)))
1876 skip("Failed to create a 3D device, skipping test.\n");
1877 DestroyWindow(window);
1878 return;
1881 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1882 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1883 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1884 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1885 IDirect3D3_Release(d3d);
1887 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1888 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1890 viewport = create_viewport(device, 0, 0, 640, 480);
1891 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1892 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1894 memset(&surface_desc, 0, sizeof(surface_desc));
1895 surface_desc.dwSize = sizeof(surface_desc);
1896 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1897 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1898 surface_desc.dwWidth = 256;
1899 surface_desc.dwHeight = 256;
1900 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
1901 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
1902 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
1903 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1904 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1905 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
1906 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x000000ff;
1907 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x000000ff;
1908 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1909 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1910 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
1911 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1912 hr = IDirect3DDevice3_SetTexture(device, 0, texture);
1913 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
1915 memset(&fx, 0, sizeof(fx));
1916 fx.dwSize = sizeof(fx);
1917 U5(fx).dwFillColor = 0x000000ff;
1918 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1919 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
1921 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 1.0f, 0);
1922 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1923 hr = IDirect3DDevice3_BeginScene(device);
1924 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1925 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, &value);
1926 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1927 ok(!value, "Got unexpected color keying state %#x.\n", value);
1928 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1929 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1930 hr = IDirect3DDevice3_EndScene(device);
1931 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1932 color = get_surface_color(rt, 320, 240);
1933 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
1935 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 1.0f, 0);
1936 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1937 hr = IDirect3DDevice3_BeginScene(device);
1938 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1939 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
1940 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
1941 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1942 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1943 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, &value);
1944 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1945 ok(!!value, "Got unexpected color keying state %#x.\n", value);
1946 hr = IDirect3DDevice3_EndScene(device);
1947 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1948 color = get_surface_color(rt, 320, 240);
1949 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1951 IDirect3DTexture_Release(texture);
1952 IDirectDrawSurface4_Release(surface);
1953 destroy_viewport(device, viewport);
1954 IDirectDrawSurface4_Release(rt);
1955 IDirect3DDevice3_Release(device);
1956 IDirectDraw4_Release(ddraw);
1957 DestroyWindow(window);
1960 static void test_ck_complex(void)
1962 IDirectDrawSurface4 *surface, *mipmap, *tmp;
1963 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
1964 DDSURFACEDESC2 surface_desc;
1965 IDirect3DDevice3 *device;
1966 DDCOLORKEY color_key;
1967 IDirectDraw4 *ddraw;
1968 IDirect3D3 *d3d;
1969 unsigned int i;
1970 ULONG refcount;
1971 HWND window;
1972 HRESULT hr;
1974 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1975 0, 0, 640, 480, 0, 0, 0, 0);
1976 if (!(device = create_device(window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
1978 skip("Failed to create a 3D device, skipping test.\n");
1979 DestroyWindow(window);
1980 return;
1982 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1983 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1984 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1985 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1986 IDirect3D3_Release(d3d);
1988 memset(&surface_desc, 0, sizeof(surface_desc));
1989 surface_desc.dwSize = sizeof(surface_desc);
1990 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1991 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1992 surface_desc.dwWidth = 128;
1993 surface_desc.dwHeight = 128;
1994 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1995 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1997 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1998 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1999 color_key.dwColorSpaceLowValue = 0x0000ff00;
2000 color_key.dwColorSpaceHighValue = 0x0000ff00;
2001 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2002 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
2003 memset(&color_key, 0, sizeof(color_key));
2004 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2005 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
2006 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2007 color_key.dwColorSpaceLowValue);
2008 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2009 color_key.dwColorSpaceHighValue);
2011 mipmap = surface;
2012 IDirectDrawSurface_AddRef(mipmap);
2013 for (i = 0; i < 7; ++i)
2015 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
2016 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
2018 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2019 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
2020 color_key.dwColorSpaceLowValue = 0x000000ff;
2021 color_key.dwColorSpaceHighValue = 0x000000ff;
2022 hr = IDirectDrawSurface4_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2023 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x, i %u.\n", hr, i);
2024 memset(&color_key, 0, sizeof(color_key));
2025 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2026 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x, i %u.\n", hr, i);
2027 ok(color_key.dwColorSpaceLowValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
2028 color_key.dwColorSpaceLowValue, i);
2029 ok(color_key.dwColorSpaceHighValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
2030 color_key.dwColorSpaceHighValue, i);
2032 IDirectDrawSurface_Release(mipmap);
2033 mipmap = tmp;
2036 memset(&color_key, 0, sizeof(color_key));
2037 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2038 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
2039 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2040 color_key.dwColorSpaceLowValue);
2041 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2042 color_key.dwColorSpaceHighValue);
2044 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
2045 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
2046 IDirectDrawSurface_Release(mipmap);
2047 refcount = IDirectDrawSurface4_Release(surface);
2048 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
2050 memset(&surface_desc, 0, sizeof(surface_desc));
2051 surface_desc.dwSize = sizeof(surface_desc);
2052 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
2053 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
2054 U5(surface_desc).dwBackBufferCount = 1;
2055 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
2056 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
2058 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2059 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
2060 color_key.dwColorSpaceLowValue = 0x0000ff00;
2061 color_key.dwColorSpaceHighValue = 0x0000ff00;
2062 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2063 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
2064 memset(&color_key, 0, sizeof(color_key));
2065 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2066 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
2067 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2068 color_key.dwColorSpaceLowValue);
2069 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2070 color_key.dwColorSpaceHighValue);
2072 hr = IDirectDrawSurface4_GetAttachedSurface(surface, &caps, &tmp);
2073 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
2075 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2076 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
2077 color_key.dwColorSpaceLowValue = 0x0000ff00;
2078 color_key.dwColorSpaceHighValue = 0x0000ff00;
2079 hr = IDirectDrawSurface4_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2080 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
2081 memset(&color_key, 0, sizeof(color_key));
2082 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2083 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
2084 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2085 color_key.dwColorSpaceLowValue);
2086 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2087 color_key.dwColorSpaceHighValue);
2089 IDirectDrawSurface_Release(tmp);
2091 refcount = IDirectDrawSurface4_Release(surface);
2092 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
2093 IDirectDraw4_Release(ddraw);
2094 refcount = IDirect3DDevice3_Release(device);
2095 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
2096 DestroyWindow(window);
2099 struct qi_test
2101 REFIID iid;
2102 REFIID refcount_iid;
2103 HRESULT hr;
2106 static void test_qi(const char *test_name, IUnknown *base_iface,
2107 REFIID refcount_iid, const struct qi_test *tests, UINT entry_count)
2109 ULONG refcount, expected_refcount;
2110 IUnknown *iface1, *iface2;
2111 HRESULT hr;
2112 UINT i, j;
2114 for (i = 0; i < entry_count; ++i)
2116 hr = IUnknown_QueryInterface(base_iface, tests[i].iid, (void **)&iface1);
2117 ok(hr == tests[i].hr, "Got hr %#x for test \"%s\" %u.\n", hr, test_name, i);
2118 if (SUCCEEDED(hr))
2120 for (j = 0; j < entry_count; ++j)
2122 hr = IUnknown_QueryInterface(iface1, tests[j].iid, (void **)&iface2);
2123 ok(hr == tests[j].hr, "Got hr %#x for test \"%s\" %u, %u.\n", hr, test_name, i, j);
2124 if (SUCCEEDED(hr))
2126 expected_refcount = 0;
2127 if (IsEqualGUID(refcount_iid, tests[j].refcount_iid))
2128 ++expected_refcount;
2129 if (IsEqualGUID(tests[i].refcount_iid, tests[j].refcount_iid))
2130 ++expected_refcount;
2131 refcount = IUnknown_Release(iface2);
2132 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, %u, expected %u.\n",
2133 refcount, test_name, i, j, expected_refcount);
2137 expected_refcount = 0;
2138 if (IsEqualGUID(refcount_iid, tests[i].refcount_iid))
2139 ++expected_refcount;
2140 refcount = IUnknown_Release(iface1);
2141 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, expected %u.\n",
2142 refcount, test_name, i, expected_refcount);
2147 static void test_surface_qi(void)
2149 static const struct qi_test tests[] =
2151 {&IID_IDirect3DTexture2, &IID_IDirectDrawSurface4, S_OK },
2152 {&IID_IDirect3DTexture, &IID_IDirectDrawSurface4, S_OK },
2153 {&IID_IDirectDrawGammaControl, &IID_IDirectDrawGammaControl, S_OK },
2154 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
2155 {&IID_IDirectDrawSurface7, &IID_IDirectDrawSurface7, S_OK },
2156 {&IID_IDirectDrawSurface4, &IID_IDirectDrawSurface4, S_OK },
2157 {&IID_IDirectDrawSurface3, &IID_IDirectDrawSurface3, S_OK },
2158 {&IID_IDirectDrawSurface2, &IID_IDirectDrawSurface2, S_OK },
2159 {&IID_IDirectDrawSurface, &IID_IDirectDrawSurface, S_OK },
2160 {&IID_IDirect3DDevice7, NULL, E_INVALIDARG },
2161 {&IID_IDirect3DDevice3, NULL, E_INVALIDARG },
2162 {&IID_IDirect3DDevice2, NULL, E_INVALIDARG },
2163 {&IID_IDirect3DDevice, NULL, E_INVALIDARG },
2164 {&IID_IDirect3D7, NULL, E_INVALIDARG },
2165 {&IID_IDirect3D3, NULL, E_INVALIDARG },
2166 {&IID_IDirect3D2, NULL, E_INVALIDARG },
2167 {&IID_IDirect3D, NULL, E_INVALIDARG },
2168 {&IID_IDirectDraw7, NULL, E_INVALIDARG },
2169 {&IID_IDirectDraw4, NULL, E_INVALIDARG },
2170 {&IID_IDirectDraw3, NULL, E_INVALIDARG },
2171 {&IID_IDirectDraw2, NULL, E_INVALIDARG },
2172 {&IID_IDirectDraw, NULL, E_INVALIDARG },
2173 {&IID_IDirect3DLight, NULL, E_INVALIDARG },
2174 {&IID_IDirect3DMaterial, NULL, E_INVALIDARG },
2175 {&IID_IDirect3DMaterial2, NULL, E_INVALIDARG },
2176 {&IID_IDirect3DMaterial3, NULL, E_INVALIDARG },
2177 {&IID_IDirect3DExecuteBuffer, NULL, E_INVALIDARG },
2178 {&IID_IDirect3DViewport, NULL, E_INVALIDARG },
2179 {&IID_IDirect3DViewport2, NULL, E_INVALIDARG },
2180 {&IID_IDirect3DViewport3, NULL, E_INVALIDARG },
2181 {&IID_IDirect3DVertexBuffer, NULL, E_INVALIDARG },
2182 {&IID_IDirect3DVertexBuffer7, NULL, E_INVALIDARG },
2183 {&IID_IDirectDrawPalette, NULL, E_INVALIDARG },
2184 {&IID_IDirectDrawClipper, NULL, E_INVALIDARG },
2185 {&IID_IUnknown, &IID_IDirectDrawSurface, S_OK },
2188 IDirectDrawSurface4 *surface;
2189 DDSURFACEDESC2 surface_desc;
2190 IDirect3DDevice3 *device;
2191 IDirectDraw4 *ddraw;
2192 HWND window;
2193 HRESULT hr;
2195 if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
2197 win_skip("DirectDrawCreateEx not available, skipping test.\n");
2198 return;
2201 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2202 0, 0, 640, 480, 0, 0, 0, 0);
2203 /* Try to create a D3D device to see if the ddraw implementation supports
2204 * D3D. 64-bit ddraw in particular doesn't seem to support D3D, and
2205 * doesn't support e.g. the IDirect3DTexture interfaces. */
2206 if (!(device = create_device(window, DDSCL_NORMAL)))
2208 skip("Failed to create a 3D device, skipping test.\n");
2209 DestroyWindow(window);
2210 return;
2212 IDirect3DDevice_Release(device);
2213 ddraw = create_ddraw();
2214 ok(!!ddraw, "Failed to create a ddraw object.\n");
2215 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2216 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
2218 memset(&surface_desc, 0, sizeof(surface_desc));
2219 surface_desc.dwSize = sizeof(surface_desc);
2220 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
2221 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
2222 surface_desc.dwWidth = 512;
2223 surface_desc.dwHeight = 512;
2224 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
2225 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
2227 test_qi("surface_qi", (IUnknown *)surface, &IID_IDirectDrawSurface4, tests, sizeof(tests) / sizeof(*tests));
2229 IDirectDrawSurface4_Release(surface);
2230 IDirectDraw4_Release(ddraw);
2231 DestroyWindow(window);
2234 static void test_device_qi(void)
2236 static const struct qi_test tests[] =
2238 {&IID_IDirect3DTexture2, NULL, E_NOINTERFACE},
2239 {&IID_IDirect3DTexture, NULL, E_NOINTERFACE},
2240 {&IID_IDirectDrawGammaControl, NULL, E_NOINTERFACE},
2241 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
2242 {&IID_IDirectDrawSurface7, NULL, E_NOINTERFACE},
2243 {&IID_IDirectDrawSurface4, NULL, E_NOINTERFACE},
2244 {&IID_IDirectDrawSurface3, NULL, E_NOINTERFACE},
2245 {&IID_IDirectDrawSurface2, NULL, E_NOINTERFACE},
2246 {&IID_IDirectDrawSurface, NULL, E_NOINTERFACE},
2247 {&IID_IDirect3DDevice7, NULL, E_NOINTERFACE},
2248 {&IID_IDirect3DDevice3, &IID_IDirect3DDevice3, S_OK },
2249 {&IID_IDirect3DDevice2, &IID_IDirect3DDevice3, S_OK },
2250 {&IID_IDirect3DDevice, &IID_IDirect3DDevice3, S_OK },
2251 {&IID_IDirect3DRampDevice, NULL, E_NOINTERFACE},
2252 {&IID_IDirect3DRGBDevice, NULL, E_NOINTERFACE},
2253 {&IID_IDirect3DHALDevice, NULL, E_NOINTERFACE},
2254 {&IID_IDirect3DMMXDevice, NULL, E_NOINTERFACE},
2255 {&IID_IDirect3DRefDevice, NULL, E_NOINTERFACE},
2256 {&IID_IDirect3DTnLHalDevice, NULL, E_NOINTERFACE},
2257 {&IID_IDirect3DNullDevice, NULL, E_NOINTERFACE},
2258 {&IID_IDirect3D7, NULL, E_NOINTERFACE},
2259 {&IID_IDirect3D3, NULL, E_NOINTERFACE},
2260 {&IID_IDirect3D2, NULL, E_NOINTERFACE},
2261 {&IID_IDirect3D, NULL, E_NOINTERFACE},
2262 {&IID_IDirectDraw7, NULL, E_NOINTERFACE},
2263 {&IID_IDirectDraw4, NULL, E_NOINTERFACE},
2264 {&IID_IDirectDraw3, NULL, E_NOINTERFACE},
2265 {&IID_IDirectDraw2, NULL, E_NOINTERFACE},
2266 {&IID_IDirectDraw, NULL, E_NOINTERFACE},
2267 {&IID_IDirect3DLight, NULL, E_NOINTERFACE},
2268 {&IID_IDirect3DMaterial, NULL, E_NOINTERFACE},
2269 {&IID_IDirect3DMaterial2, NULL, E_NOINTERFACE},
2270 {&IID_IDirect3DMaterial3, NULL, E_NOINTERFACE},
2271 {&IID_IDirect3DExecuteBuffer, NULL, E_NOINTERFACE},
2272 {&IID_IDirect3DViewport, NULL, E_NOINTERFACE},
2273 {&IID_IDirect3DViewport2, NULL, E_NOINTERFACE},
2274 {&IID_IDirect3DViewport3, NULL, E_NOINTERFACE},
2275 {&IID_IDirect3DVertexBuffer, NULL, E_NOINTERFACE},
2276 {&IID_IDirect3DVertexBuffer7, NULL, E_NOINTERFACE},
2277 {&IID_IDirectDrawPalette, NULL, E_NOINTERFACE},
2278 {&IID_IDirectDrawClipper, NULL, E_NOINTERFACE},
2279 {&IID_IUnknown, &IID_IDirect3DDevice3, S_OK },
2282 IDirect3DDevice3 *device;
2283 HWND window;
2285 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2286 0, 0, 640, 480, 0, 0, 0, 0);
2287 if (!(device = create_device(window, DDSCL_NORMAL)))
2289 skip("Failed to create a 3D device, skipping test.\n");
2290 DestroyWindow(window);
2291 return;
2294 test_qi("device_qi", (IUnknown *)device, &IID_IDirect3DDevice3, tests, sizeof(tests) / sizeof(*tests));
2296 IDirect3DDevice3_Release(device);
2297 DestroyWindow(window);
2300 static void test_wndproc(void)
2302 LONG_PTR proc, ddraw_proc;
2303 IDirectDraw4 *ddraw;
2304 WNDCLASSA wc = {0};
2305 HWND window;
2306 HRESULT hr;
2307 ULONG ref;
2309 static struct message messages[] =
2311 {WM_WINDOWPOSCHANGING, FALSE, 0},
2312 {WM_MOVE, FALSE, 0},
2313 {WM_SIZE, FALSE, 0},
2314 {WM_WINDOWPOSCHANGING, FALSE, 0},
2315 {WM_ACTIVATE, FALSE, 0},
2316 {WM_SETFOCUS, FALSE, 0},
2317 {0, FALSE, 0},
2320 /* DDSCL_EXCLUSIVE replaces the window's window proc. */
2321 ddraw = create_ddraw();
2322 ok(!!ddraw, "Failed to create a ddraw object.\n");
2324 wc.lpfnWndProc = test_proc;
2325 wc.lpszClassName = "ddraw_test_wndproc_wc";
2326 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2328 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
2329 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
2331 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2332 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2333 (LONG_PTR)test_proc, proc);
2334 expect_messages = messages;
2335 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2336 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2337 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2338 expect_messages = NULL;
2339 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2340 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2341 (LONG_PTR)test_proc, proc);
2342 ref = IDirectDraw4_Release(ddraw);
2343 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2344 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2345 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2346 (LONG_PTR)test_proc, proc);
2348 /* DDSCL_NORMAL doesn't. */
2349 ddraw = create_ddraw();
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 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
2354 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2355 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2356 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2357 (LONG_PTR)test_proc, proc);
2358 ref = IDirectDraw4_Release(ddraw);
2359 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2360 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2361 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2362 (LONG_PTR)test_proc, proc);
2364 /* The original window proc is only restored by ddraw if the current
2365 * window proc matches the one ddraw set. This also affects switching
2366 * from DDSCL_NORMAL to DDSCL_EXCLUSIVE. */
2367 ddraw = create_ddraw();
2368 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2369 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2370 (LONG_PTR)test_proc, proc);
2371 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2372 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2373 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2374 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2375 (LONG_PTR)test_proc, proc);
2376 ddraw_proc = proc;
2377 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2378 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2379 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2380 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2381 (LONG_PTR)test_proc, proc);
2382 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2383 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2384 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2385 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2386 (LONG_PTR)test_proc, proc);
2387 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2388 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2389 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2390 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2391 (LONG_PTR)DefWindowProcA, proc);
2392 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2393 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2394 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)ddraw_proc);
2395 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2396 (LONG_PTR)DefWindowProcA, proc);
2397 ref = IDirectDraw4_Release(ddraw);
2398 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2399 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2400 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2401 (LONG_PTR)test_proc, proc);
2403 ddraw = create_ddraw();
2404 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2405 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2406 (LONG_PTR)test_proc, proc);
2407 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2408 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2409 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2410 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2411 (LONG_PTR)test_proc, proc);
2412 ref = IDirectDraw4_Release(ddraw);
2413 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2414 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2415 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2416 (LONG_PTR)DefWindowProcA, proc);
2418 fix_wndproc(window, (LONG_PTR)test_proc);
2419 expect_messages = NULL;
2420 DestroyWindow(window);
2421 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
2424 static void test_window_style(void)
2426 LONG style, exstyle, tmp, expected_style;
2427 RECT fullscreen_rect, r;
2428 IDirectDraw4 *ddraw;
2429 HWND window;
2430 HRESULT hr;
2431 ULONG ref;
2432 BOOL ret;
2434 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2435 0, 0, 100, 100, 0, 0, 0, 0);
2436 ddraw = create_ddraw();
2437 ok(!!ddraw, "Failed to create a ddraw object.\n");
2439 style = GetWindowLongA(window, GWL_STYLE);
2440 exstyle = GetWindowLongA(window, GWL_EXSTYLE);
2441 SetRect(&fullscreen_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2443 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2444 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2446 tmp = GetWindowLongA(window, GWL_STYLE);
2447 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2448 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2449 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2451 GetWindowRect(window, &r);
2452 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2453 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2454 r.left, r.top, r.right, r.bottom);
2455 GetClientRect(window, &r);
2456 todo_wine ok(!EqualRect(&r, &fullscreen_rect), "Client rect and window rect are equal.\n");
2458 ret = SetForegroundWindow(GetDesktopWindow());
2459 ok(ret, "Failed to set foreground window.\n");
2461 tmp = GetWindowLongA(window, GWL_STYLE);
2462 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2463 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2464 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2466 ret = SetForegroundWindow(window);
2467 ok(ret, "Failed to set foreground window.\n");
2468 /* Windows 7 (but not Vista and XP) shows the window when it receives focus. Hide it again,
2469 * the next tests expect this. */
2470 ShowWindow(window, SW_HIDE);
2472 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2473 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2475 tmp = GetWindowLongA(window, GWL_STYLE);
2476 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2477 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2478 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2480 ShowWindow(window, SW_SHOW);
2481 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2482 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2484 tmp = GetWindowLongA(window, GWL_STYLE);
2485 expected_style = style | WS_VISIBLE;
2486 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2487 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2488 expected_style = exstyle | WS_EX_TOPMOST;
2489 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2491 ret = SetForegroundWindow(GetDesktopWindow());
2492 ok(ret, "Failed to set foreground window.\n");
2493 tmp = GetWindowLongA(window, GWL_STYLE);
2494 expected_style = style | WS_VISIBLE | WS_MINIMIZE;
2495 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2496 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2497 expected_style = exstyle | WS_EX_TOPMOST;
2498 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2500 ref = IDirectDraw4_Release(ddraw);
2501 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2503 DestroyWindow(window);
2506 static void test_redundant_mode_set(void)
2508 DDSURFACEDESC2 surface_desc = {0};
2509 IDirectDraw4 *ddraw;
2510 HWND window;
2511 HRESULT hr;
2512 RECT r, s;
2513 ULONG ref;
2515 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2516 0, 0, 100, 100, 0, 0, 0, 0);
2517 ddraw = create_ddraw();
2518 ok(!!ddraw, "Failed to create a ddraw object.\n");
2520 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2521 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2523 surface_desc.dwSize = sizeof(surface_desc);
2524 hr = IDirectDraw4_GetDisplayMode(ddraw, &surface_desc);
2525 ok(SUCCEEDED(hr), "GetDipslayMode failed, hr %#x.\n", hr);
2527 hr = IDirectDraw4_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2528 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount, 0, 0);
2529 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2531 GetWindowRect(window, &r);
2532 r.right /= 2;
2533 r.bottom /= 2;
2534 SetWindowPos(window, HWND_TOP, r.left, r.top, r.right, r.bottom, 0);
2535 GetWindowRect(window, &s);
2536 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2537 r.left, r.top, r.right, r.bottom,
2538 s.left, s.top, s.right, s.bottom);
2540 hr = IDirectDraw4_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2541 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount, 0, 0);
2542 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2544 GetWindowRect(window, &s);
2545 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2546 r.left, r.top, r.right, r.bottom,
2547 s.left, s.top, s.right, s.bottom);
2549 ref = IDirectDraw4_Release(ddraw);
2550 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2552 DestroyWindow(window);
2555 static SIZE screen_size, screen_size2;
2557 static LRESULT CALLBACK mode_set_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2559 if (message == WM_SIZE)
2561 screen_size.cx = GetSystemMetrics(SM_CXSCREEN);
2562 screen_size.cy = GetSystemMetrics(SM_CYSCREEN);
2565 return test_proc(hwnd, message, wparam, lparam);
2568 static LRESULT CALLBACK mode_set_proc2(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2570 if (message == WM_SIZE)
2572 screen_size2.cx = GetSystemMetrics(SM_CXSCREEN);
2573 screen_size2.cy = GetSystemMetrics(SM_CYSCREEN);
2576 return test_proc(hwnd, message, wparam, lparam);
2579 struct test_coop_level_mode_set_enum_param
2581 DWORD ddraw_width, ddraw_height, user32_width, user32_height;
2584 static HRESULT CALLBACK test_coop_level_mode_set_enum_cb(DDSURFACEDESC2 *surface_desc, void *context)
2586 struct test_coop_level_mode_set_enum_param *param = context;
2588 if (U1(U4(*surface_desc).ddpfPixelFormat).dwRGBBitCount != registry_mode.dmBitsPerPel)
2589 return DDENUMRET_OK;
2590 if (surface_desc->dwWidth == registry_mode.dmPelsWidth
2591 && surface_desc->dwHeight == registry_mode.dmPelsHeight)
2592 return DDENUMRET_OK;
2594 if (!param->ddraw_width)
2596 param->ddraw_width = surface_desc->dwWidth;
2597 param->ddraw_height = surface_desc->dwHeight;
2598 return DDENUMRET_OK;
2600 if (surface_desc->dwWidth == param->ddraw_width && surface_desc->dwHeight == param->ddraw_height)
2601 return DDENUMRET_OK;
2603 param->user32_width = surface_desc->dwWidth;
2604 param->user32_height = surface_desc->dwHeight;
2605 return DDENUMRET_CANCEL;
2608 static void test_coop_level_mode_set(void)
2610 IDirectDrawSurface4 *primary;
2611 RECT registry_rect, ddraw_rect, user32_rect, r;
2612 IDirectDraw4 *ddraw;
2613 DDSURFACEDESC2 ddsd;
2614 WNDCLASSA wc = {0};
2615 HWND window, window2;
2616 HRESULT hr;
2617 ULONG ref;
2618 MSG msg;
2619 struct test_coop_level_mode_set_enum_param param;
2620 DEVMODEW devmode;
2621 BOOL ret;
2622 LONG change_ret;
2624 static const struct message exclusive_messages[] =
2626 {WM_WINDOWPOSCHANGING, FALSE, 0},
2627 {WM_WINDOWPOSCHANGED, FALSE, 0},
2628 {WM_SIZE, FALSE, 0},
2629 {WM_DISPLAYCHANGE, FALSE, 0},
2630 {0, FALSE, 0},
2632 static const struct message exclusive_focus_loss_messages[] =
2634 {WM_ACTIVATE, TRUE, WA_INACTIVE},
2635 {WM_DISPLAYCHANGE, FALSE, 0},
2636 {WM_WINDOWPOSCHANGING, FALSE, 0},
2637 /* Like d3d8 and d3d9 ddraw seems to use SW_SHOWMINIMIZED instead of
2638 * SW_MINIMIZED, causing a recursive window activation that does not
2639 * produce the same result in Wine yet. Ignore the difference for now.
2640 * {WM_ACTIVATE, TRUE, 0x200000 | WA_ACTIVE}, */
2641 {WM_WINDOWPOSCHANGED, FALSE, 0},
2642 {WM_MOVE, FALSE, 0},
2643 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2644 {WM_ACTIVATEAPP, TRUE, FALSE},
2645 {0, FALSE, 0},
2647 static const struct message exclusive_focus_restore_messages[] =
2649 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* From the ShowWindow(SW_RESTORE). */
2650 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* Generated by ddraw, matches d3d9 behavior. */
2651 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching previous message. */
2652 {WM_SIZE, FALSE, 0}, /* DefWindowProc. */
2653 {WM_DISPLAYCHANGE, FALSE, 0}, /* Ddraw restores mode. */
2654 /* Native redundantly sets the window size here. */
2655 {WM_ACTIVATEAPP, TRUE, TRUE}, /* End of ddraw's hooks. */
2656 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching the one from ShowWindow. */
2657 {WM_MOVE, FALSE, 0}, /* DefWindowProc. */
2658 {WM_SIZE, TRUE, SIZE_RESTORED}, /* DefWindowProc. */
2659 {0, FALSE, 0},
2661 static const struct message sc_restore_messages[] =
2663 {WM_SYSCOMMAND, TRUE, SC_RESTORE},
2664 {WM_WINDOWPOSCHANGING, FALSE, 0},
2665 {WM_WINDOWPOSCHANGED, FALSE, 0},
2666 {WM_SIZE, TRUE, SIZE_RESTORED},
2667 {0, FALSE, 0},
2669 static const struct message sc_minimize_messages[] =
2671 {WM_SYSCOMMAND, TRUE, SC_MINIMIZE},
2672 {WM_WINDOWPOSCHANGING, FALSE, 0},
2673 {WM_WINDOWPOSCHANGED, FALSE, 0},
2674 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2675 {0, FALSE, 0},
2677 static const struct message sc_maximize_messages[] =
2679 {WM_SYSCOMMAND, TRUE, SC_MAXIMIZE},
2680 {WM_WINDOWPOSCHANGING, FALSE, 0},
2681 {WM_WINDOWPOSCHANGED, FALSE, 0},
2682 {WM_SIZE, TRUE, SIZE_MAXIMIZED},
2683 {0, FALSE, 0},
2686 static const struct message normal_messages[] =
2688 {WM_DISPLAYCHANGE, FALSE, 0},
2689 {0, FALSE, 0},
2692 ddraw = create_ddraw();
2693 ok(!!ddraw, "Failed to create a ddraw object.\n");
2695 memset(&param, 0, sizeof(param));
2696 hr = IDirectDraw4_EnumDisplayModes(ddraw, 0, NULL, &param, test_coop_level_mode_set_enum_cb);
2697 ok(SUCCEEDED(hr), "Failed to enumerate display mode, hr %#x.\n", hr);
2698 ref = IDirectDraw4_Release(ddraw);
2699 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2701 if (!param.user32_height)
2703 skip("Fewer than 3 different modes supported, skipping mode restore test.\n");
2704 return;
2707 SetRect(&registry_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2708 SetRect(&ddraw_rect, 0, 0, param.ddraw_width, param.ddraw_height);
2709 SetRect(&user32_rect, 0, 0, param.user32_width, param.user32_height);
2711 memset(&devmode, 0, sizeof(devmode));
2712 devmode.dmSize = sizeof(devmode);
2713 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2714 devmode.dmPelsWidth = param.user32_width;
2715 devmode.dmPelsHeight = param.user32_height;
2716 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2717 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2719 ddraw = create_ddraw();
2720 ok(!!ddraw, "Failed to create a ddraw object.\n");
2722 wc.lpfnWndProc = mode_set_proc;
2723 wc.lpszClassName = "ddraw_test_wndproc_wc";
2724 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2725 wc.lpfnWndProc = mode_set_proc2;
2726 wc.lpszClassName = "ddraw_test_wndproc_wc2";
2727 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2729 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test", WS_OVERLAPPEDWINDOW,
2730 0, 0, 100, 100, 0, 0, 0, 0);
2731 window2 = CreateWindowA("ddraw_test_wndproc_wc2", "ddraw_test", WS_OVERLAPPEDWINDOW,
2732 0, 0, 100, 100, 0, 0, 0, 0);
2734 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2735 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2737 GetWindowRect(window, &r);
2738 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2739 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2740 r.left, r.top, r.right, r.bottom);
2742 memset(&ddsd, 0, sizeof(ddsd));
2743 ddsd.dwSize = sizeof(ddsd);
2744 ddsd.dwFlags = DDSD_CAPS;
2745 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2747 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2748 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2749 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2750 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2751 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2752 param.user32_width, ddsd.dwWidth);
2753 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2754 param.user32_height, ddsd.dwHeight);
2756 GetWindowRect(window, &r);
2757 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2758 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2759 r.left, r.top, r.right, r.bottom);
2761 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2762 expect_messages = exclusive_messages;
2763 screen_size.cx = 0;
2764 screen_size.cy = 0;
2766 hr = IDirectDrawSurface4_IsLost(primary);
2767 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2768 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2769 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2770 hr = IDirectDrawSurface4_IsLost(primary);
2771 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2773 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2774 expect_messages = NULL;
2775 ok(screen_size.cx == param.ddraw_width && screen_size.cy == param.ddraw_height,
2776 "Expected screen size %ux%u, got %ux%u.\n",
2777 param.ddraw_width, param.ddraw_height, screen_size.cx, screen_size.cy);
2779 GetWindowRect(window, &r);
2780 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2781 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2782 r.left, r.top, r.right, r.bottom);
2784 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2785 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2786 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2787 param.user32_width, ddsd.dwWidth);
2788 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2789 param.user32_height, ddsd.dwHeight);
2790 IDirectDrawSurface4_Release(primary);
2792 memset(&ddsd, 0, sizeof(ddsd));
2793 ddsd.dwSize = sizeof(ddsd);
2794 ddsd.dwFlags = DDSD_CAPS;
2795 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2797 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2798 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2799 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2800 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2801 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2802 param.ddraw_width, ddsd.dwWidth);
2803 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2804 param.ddraw_height, ddsd.dwHeight);
2806 GetWindowRect(window, &r);
2807 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2808 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2809 r.left, r.top, r.right, r.bottom);
2811 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2812 expect_messages = exclusive_messages;
2813 screen_size.cx = 0;
2814 screen_size.cy = 0;
2816 hr = IDirectDrawSurface4_IsLost(primary);
2817 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2818 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2819 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2820 hr = IDirectDrawSurface4_IsLost(primary);
2821 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2823 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2824 expect_messages = NULL;
2825 ok(screen_size.cx == param.user32_width && screen_size.cy == param.user32_height,
2826 "Expected screen size %ux%u, got %ux%u.\n",
2827 param.user32_width, param.user32_height, screen_size.cx, screen_size.cy);
2829 GetWindowRect(window, &r);
2830 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2831 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2832 r.left, r.top, r.right, r.bottom);
2834 expect_messages = exclusive_focus_loss_messages;
2835 ret = SetForegroundWindow(GetDesktopWindow());
2836 ok(ret, "Failed to set foreground window.\n");
2837 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2838 memset(&devmode, 0, sizeof(devmode));
2839 devmode.dmSize = sizeof(devmode);
2840 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2841 ok(ret, "Failed to get display mode.\n");
2842 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
2843 && devmode.dmPelsHeight == registry_mode.dmPelsHeight, "Got unexpect screen size %ux%u.\n",
2844 devmode.dmPelsWidth, devmode.dmPelsHeight);
2846 expect_messages = exclusive_focus_restore_messages;
2847 ShowWindow(window, SW_RESTORE);
2848 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2850 GetWindowRect(window, &r);
2851 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2852 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2853 r.left, r.top, r.right, r.bottom);
2854 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2855 ok(ret, "Failed to get display mode.\n");
2856 ok(devmode.dmPelsWidth == param.ddraw_width
2857 && devmode.dmPelsHeight == param.ddraw_height, "Got unexpect screen size %ux%u.\n",
2858 devmode.dmPelsWidth, devmode.dmPelsHeight);
2860 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2861 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2862 /* Normally the primary should be restored here. Unfortunately this causes the
2863 * GetSurfaceDesc call after the next display mode change to crash on the Windows 8
2864 * testbot. Another Restore call would presumably avoid the crash, but it also moots
2865 * the point of the GetSurfaceDesc call. */
2867 expect_messages = sc_minimize_messages;
2868 SendMessageA(window, WM_SYSCOMMAND, SC_MINIMIZE, 0);
2869 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2870 expect_messages = NULL;
2872 expect_messages = sc_restore_messages;
2873 SendMessageA(window, WM_SYSCOMMAND, SC_RESTORE, 0);
2874 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2875 expect_messages = NULL;
2877 expect_messages = sc_maximize_messages;
2878 SendMessageA(window, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
2879 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2880 expect_messages = NULL;
2882 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2883 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2885 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2886 expect_messages = exclusive_messages;
2887 screen_size.cx = 0;
2888 screen_size.cy = 0;
2890 hr = IDirectDrawSurface4_IsLost(primary);
2891 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2892 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
2893 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2894 hr = IDirectDrawSurface4_IsLost(primary);
2895 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2897 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2898 expect_messages = NULL;
2899 ok(screen_size.cx == registry_mode.dmPelsWidth
2900 && screen_size.cy == registry_mode.dmPelsHeight,
2901 "Expected screen size %ux%u, got %ux%u.\n",
2902 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight, screen_size.cx, screen_size.cy);
2904 GetWindowRect(window, &r);
2905 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2906 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2907 r.left, r.top, r.right, r.bottom);
2909 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2910 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2911 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2912 param.ddraw_width, ddsd.dwWidth);
2913 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2914 param.ddraw_height, ddsd.dwHeight);
2915 IDirectDrawSurface4_Release(primary);
2917 /* For Wine. */
2918 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
2919 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2921 memset(&ddsd, 0, sizeof(ddsd));
2922 ddsd.dwSize = sizeof(ddsd);
2923 ddsd.dwFlags = DDSD_CAPS;
2924 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2926 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2927 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2928 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2929 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2930 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2931 registry_mode.dmPelsWidth, ddsd.dwWidth);
2932 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2933 registry_mode.dmPelsHeight, ddsd.dwHeight);
2935 GetWindowRect(window, &r);
2936 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2937 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2938 r.left, r.top, r.right, r.bottom);
2940 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2941 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2943 GetWindowRect(window, &r);
2944 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2945 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2946 r.left, r.top, r.right, r.bottom);
2948 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2949 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2950 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2951 registry_mode.dmPelsWidth, ddsd.dwWidth);
2952 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2953 registry_mode.dmPelsHeight, ddsd.dwHeight);
2954 IDirectDrawSurface4_Release(primary);
2956 memset(&ddsd, 0, sizeof(ddsd));
2957 ddsd.dwSize = sizeof(ddsd);
2958 ddsd.dwFlags = DDSD_CAPS;
2959 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2961 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2962 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2963 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2964 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2965 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2966 registry_mode.dmPelsWidth, ddsd.dwWidth);
2967 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2968 registry_mode.dmPelsHeight, ddsd.dwHeight);
2970 GetWindowRect(window, &r);
2971 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2972 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2973 r.left, r.top, r.right, r.bottom);
2975 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2976 expect_messages = normal_messages;
2977 screen_size.cx = 0;
2978 screen_size.cy = 0;
2980 hr = IDirectDrawSurface4_IsLost(primary);
2981 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2982 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2983 devmode.dmPelsWidth = param.user32_width;
2984 devmode.dmPelsHeight = param.user32_height;
2985 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2986 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
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 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3000 expect_messages = normal_messages;
3001 screen_size.cx = 0;
3002 screen_size.cy = 0;
3004 hr = IDirectDrawSurface4_Restore(primary);
3005 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
3006 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3007 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3008 hr = IDirectDrawSurface4_Restore(primary);
3009 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
3010 hr = IDirectDrawSurface4_IsLost(primary);
3011 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
3013 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3014 expect_messages = NULL;
3015 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3017 GetWindowRect(window, &r);
3018 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3019 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3020 r.left, r.top, r.right, r.bottom);
3022 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3023 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3024 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3025 registry_mode.dmPelsWidth, ddsd.dwWidth);
3026 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3027 registry_mode.dmPelsHeight, ddsd.dwHeight);
3028 IDirectDrawSurface4_Release(primary);
3030 memset(&ddsd, 0, sizeof(ddsd));
3031 ddsd.dwSize = sizeof(ddsd);
3032 ddsd.dwFlags = DDSD_CAPS;
3033 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3035 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3036 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3037 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3038 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3039 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3040 param.ddraw_width, ddsd.dwWidth);
3041 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3042 param.ddraw_height, ddsd.dwHeight);
3044 GetWindowRect(window, &r);
3045 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3046 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3047 r.left, r.top, r.right, r.bottom);
3049 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3050 expect_messages = normal_messages;
3051 screen_size.cx = 0;
3052 screen_size.cy = 0;
3054 hr = IDirectDrawSurface4_IsLost(primary);
3055 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
3056 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
3057 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3058 hr = IDirectDrawSurface4_IsLost(primary);
3059 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
3061 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3062 expect_messages = NULL;
3063 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3065 GetWindowRect(window, &r);
3066 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3067 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3068 r.left, r.top, r.right, r.bottom);
3070 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3071 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3072 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3073 param.ddraw_width, ddsd.dwWidth);
3074 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3075 param.ddraw_height, ddsd.dwHeight);
3076 IDirectDrawSurface4_Release(primary);
3078 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3079 ok(ret, "Failed to get display mode.\n");
3080 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
3081 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
3082 "Expected resolution %ux%u, got %ux%u.\n",
3083 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
3084 devmode.dmPelsWidth, devmode.dmPelsHeight);
3085 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
3086 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
3088 memset(&ddsd, 0, sizeof(ddsd));
3089 ddsd.dwSize = sizeof(ddsd);
3090 ddsd.dwFlags = DDSD_CAPS;
3091 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3093 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3094 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
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);
3102 GetWindowRect(window, &r);
3103 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3104 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3105 r.left, r.top, r.right, r.bottom);
3107 /* DDSCL_NORMAL | DDSCL_FULLSCREEN behaves the same as just DDSCL_NORMAL.
3108 * Resizing the window on mode changes is a property of DDSCL_EXCLUSIVE,
3109 * not DDSCL_FULLSCREEN. */
3110 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
3111 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3113 GetWindowRect(window, &r);
3114 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3115 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3116 r.left, r.top, r.right, r.bottom);
3118 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3119 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3120 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3121 registry_mode.dmPelsWidth, ddsd.dwWidth);
3122 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3123 registry_mode.dmPelsHeight, ddsd.dwHeight);
3124 IDirectDrawSurface4_Release(primary);
3126 memset(&ddsd, 0, sizeof(ddsd));
3127 ddsd.dwSize = sizeof(ddsd);
3128 ddsd.dwFlags = DDSD_CAPS;
3129 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3131 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3132 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3133 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3134 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3135 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3136 registry_mode.dmPelsWidth, ddsd.dwWidth);
3137 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3138 registry_mode.dmPelsHeight, ddsd.dwHeight);
3140 GetWindowRect(window, &r);
3141 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3142 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3143 r.left, r.top, r.right, r.bottom);
3145 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3146 expect_messages = normal_messages;
3147 screen_size.cx = 0;
3148 screen_size.cy = 0;
3150 hr = IDirectDrawSurface4_IsLost(primary);
3151 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
3152 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
3153 devmode.dmPelsWidth = param.user32_width;
3154 devmode.dmPelsHeight = param.user32_height;
3155 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
3156 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
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 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3170 expect_messages = normal_messages;
3171 screen_size.cx = 0;
3172 screen_size.cy = 0;
3174 hr = IDirectDrawSurface4_Restore(primary);
3175 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
3176 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3177 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3178 hr = IDirectDrawSurface4_Restore(primary);
3179 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
3180 hr = IDirectDrawSurface4_IsLost(primary);
3181 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
3183 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3184 expect_messages = NULL;
3185 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3187 GetWindowRect(window, &r);
3188 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3189 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3190 r.left, r.top, r.right, r.bottom);
3192 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3193 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3194 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3195 registry_mode.dmPelsWidth, ddsd.dwWidth);
3196 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3197 registry_mode.dmPelsHeight, ddsd.dwHeight);
3198 IDirectDrawSurface4_Release(primary);
3200 memset(&ddsd, 0, sizeof(ddsd));
3201 ddsd.dwSize = sizeof(ddsd);
3202 ddsd.dwFlags = DDSD_CAPS;
3203 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3205 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3206 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3207 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3208 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3209 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3210 param.ddraw_width, ddsd.dwWidth);
3211 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3212 param.ddraw_height, ddsd.dwHeight);
3214 GetWindowRect(window, &r);
3215 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3216 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3217 r.left, r.top, r.right, r.bottom);
3219 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3220 expect_messages = normal_messages;
3221 screen_size.cx = 0;
3222 screen_size.cy = 0;
3224 hr = IDirectDrawSurface4_IsLost(primary);
3225 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
3226 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
3227 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3228 hr = IDirectDrawSurface4_IsLost(primary);
3229 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
3231 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3232 expect_messages = NULL;
3233 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3235 GetWindowRect(window, &r);
3236 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3237 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3238 r.left, r.top, r.right, r.bottom);
3240 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3241 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3242 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3243 param.ddraw_width, ddsd.dwWidth);
3244 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3245 param.ddraw_height, ddsd.dwHeight);
3246 IDirectDrawSurface4_Release(primary);
3248 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3249 ok(ret, "Failed to get display mode.\n");
3250 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
3251 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
3252 "Expected resolution %ux%u, got %ux%u.\n",
3253 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
3254 devmode.dmPelsWidth, devmode.dmPelsHeight);
3255 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
3256 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
3258 memset(&ddsd, 0, sizeof(ddsd));
3259 ddsd.dwSize = sizeof(ddsd);
3260 ddsd.dwFlags = DDSD_CAPS;
3261 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3263 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3264 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3265 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3266 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3267 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3268 registry_mode.dmPelsWidth, ddsd.dwWidth);
3269 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3270 registry_mode.dmPelsHeight, ddsd.dwHeight);
3271 IDirectDrawSurface4_Release(primary);
3273 GetWindowRect(window, &r);
3274 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3275 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3276 r.left, r.top, r.right, r.bottom);
3278 /* Changing the coop level from EXCLUSIVE to NORMAL restores the screen resolution */
3279 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3280 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3281 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3282 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3284 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3285 expect_messages = exclusive_messages;
3286 screen_size.cx = 0;
3287 screen_size.cy = 0;
3289 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3290 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3292 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3293 expect_messages = NULL;
3294 ok(screen_size.cx == registry_mode.dmPelsWidth
3295 && screen_size.cy == registry_mode.dmPelsHeight,
3296 "Expected screen size %ux%u, got %ux%u.\n",
3297 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
3298 screen_size.cx, screen_size.cy);
3300 GetWindowRect(window, &r);
3301 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3302 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3303 r.left, r.top, r.right, r.bottom);
3305 memset(&ddsd, 0, sizeof(ddsd));
3306 ddsd.dwSize = sizeof(ddsd);
3307 ddsd.dwFlags = DDSD_CAPS;
3308 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3310 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3311 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3312 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3313 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3314 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3315 registry_mode.dmPelsWidth, ddsd.dwWidth);
3316 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3317 registry_mode.dmPelsHeight, ddsd.dwHeight);
3318 IDirectDrawSurface4_Release(primary);
3320 /* The screen restore is a property of DDSCL_EXCLUSIVE */
3321 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
3322 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3323 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3324 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3326 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3327 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3329 memset(&ddsd, 0, sizeof(ddsd));
3330 ddsd.dwSize = sizeof(ddsd);
3331 ddsd.dwFlags = DDSD_CAPS;
3332 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3334 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3335 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3336 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3337 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3338 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3339 param.ddraw_width, ddsd.dwWidth);
3340 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3341 param.ddraw_height, ddsd.dwHeight);
3342 IDirectDrawSurface4_Release(primary);
3344 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
3345 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3347 /* If the window is changed at the same time, messages are sent to the new window. */
3348 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3349 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3350 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3351 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3353 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3354 expect_messages = exclusive_messages;
3355 screen_size.cx = 0;
3356 screen_size.cy = 0;
3357 screen_size2.cx = 0;
3358 screen_size2.cy = 0;
3360 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
3361 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3363 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3364 expect_messages = NULL;
3365 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n",
3366 screen_size.cx, screen_size.cy);
3367 ok(screen_size2.cx == registry_mode.dmPelsWidth && screen_size2.cy == registry_mode.dmPelsHeight,
3368 "Expected screen size 2 %ux%u, got %ux%u.\n",
3369 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight, screen_size2.cx, screen_size2.cy);
3371 GetWindowRect(window, &r);
3372 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3373 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
3374 r.left, r.top, r.right, r.bottom);
3375 GetWindowRect(window2, &r);
3376 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3377 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3378 r.left, r.top, r.right, r.bottom);
3380 memset(&ddsd, 0, sizeof(ddsd));
3381 ddsd.dwSize = sizeof(ddsd);
3382 ddsd.dwFlags = DDSD_CAPS;
3383 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3385 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3386 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3387 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3388 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3389 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3390 registry_mode.dmPelsWidth, ddsd.dwWidth);
3391 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3392 registry_mode.dmPelsHeight, ddsd.dwHeight);
3393 IDirectDrawSurface4_Release(primary);
3395 ref = IDirectDraw4_Release(ddraw);
3396 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3398 GetWindowRect(window, &r);
3399 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3400 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
3401 r.left, r.top, r.right, r.bottom);
3403 expect_messages = NULL;
3404 DestroyWindow(window);
3405 DestroyWindow(window2);
3406 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
3407 UnregisterClassA("ddraw_test_wndproc_wc2", GetModuleHandleA(NULL));
3410 static void test_coop_level_mode_set_multi(void)
3412 IDirectDraw4 *ddraw1, *ddraw2;
3413 UINT w, h;
3414 HWND window;
3415 HRESULT hr;
3416 ULONG ref;
3418 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3419 0, 0, 100, 100, 0, 0, 0, 0);
3420 ddraw1 = create_ddraw();
3421 ok(!!ddraw1, "Failed to create a ddraw object.\n");
3423 /* With just a single ddraw object, the display mode is restored on
3424 * release. */
3425 hr = set_display_mode(ddraw1, 800, 600);
3426 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3427 w = GetSystemMetrics(SM_CXSCREEN);
3428 ok(w == 800, "Got unexpected screen width %u.\n", w);
3429 h = GetSystemMetrics(SM_CYSCREEN);
3430 ok(h == 600, "Got unexpected screen height %u.\n", h);
3432 ref = IDirectDraw4_Release(ddraw1);
3433 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3434 w = GetSystemMetrics(SM_CXSCREEN);
3435 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3436 h = GetSystemMetrics(SM_CYSCREEN);
3437 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3439 /* When there are multiple ddraw objects, the display mode is restored to
3440 * the initial mode, before the first SetDisplayMode() call. */
3441 ddraw1 = create_ddraw();
3442 hr = set_display_mode(ddraw1, 800, 600);
3443 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3444 w = GetSystemMetrics(SM_CXSCREEN);
3445 ok(w == 800, "Got unexpected screen width %u.\n", w);
3446 h = GetSystemMetrics(SM_CYSCREEN);
3447 ok(h == 600, "Got unexpected screen height %u.\n", h);
3449 ddraw2 = create_ddraw();
3450 hr = set_display_mode(ddraw2, 640, 480);
3451 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3452 w = GetSystemMetrics(SM_CXSCREEN);
3453 ok(w == 640, "Got unexpected screen width %u.\n", w);
3454 h = GetSystemMetrics(SM_CYSCREEN);
3455 ok(h == 480, "Got unexpected screen height %u.\n", h);
3457 ref = IDirectDraw4_Release(ddraw2);
3458 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3459 w = GetSystemMetrics(SM_CXSCREEN);
3460 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3461 h = GetSystemMetrics(SM_CYSCREEN);
3462 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3464 ref = IDirectDraw4_Release(ddraw1);
3465 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3466 w = GetSystemMetrics(SM_CXSCREEN);
3467 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3468 h = GetSystemMetrics(SM_CYSCREEN);
3469 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3471 /* Regardless of release ordering. */
3472 ddraw1 = create_ddraw();
3473 hr = set_display_mode(ddraw1, 800, 600);
3474 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3475 w = GetSystemMetrics(SM_CXSCREEN);
3476 ok(w == 800, "Got unexpected screen width %u.\n", w);
3477 h = GetSystemMetrics(SM_CYSCREEN);
3478 ok(h == 600, "Got unexpected screen height %u.\n", h);
3480 ddraw2 = create_ddraw();
3481 hr = set_display_mode(ddraw2, 640, 480);
3482 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3483 w = GetSystemMetrics(SM_CXSCREEN);
3484 ok(w == 640, "Got unexpected screen width %u.\n", w);
3485 h = GetSystemMetrics(SM_CYSCREEN);
3486 ok(h == 480, "Got unexpected screen height %u.\n", h);
3488 ref = IDirectDraw4_Release(ddraw1);
3489 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3490 w = GetSystemMetrics(SM_CXSCREEN);
3491 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3492 h = GetSystemMetrics(SM_CYSCREEN);
3493 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3495 ref = IDirectDraw4_Release(ddraw2);
3496 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3497 w = GetSystemMetrics(SM_CXSCREEN);
3498 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3499 h = GetSystemMetrics(SM_CYSCREEN);
3500 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3502 /* But only for ddraw objects that called SetDisplayMode(). */
3503 ddraw1 = create_ddraw();
3504 ddraw2 = create_ddraw();
3505 hr = set_display_mode(ddraw2, 640, 480);
3506 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3507 w = GetSystemMetrics(SM_CXSCREEN);
3508 ok(w == 640, "Got unexpected screen width %u.\n", w);
3509 h = GetSystemMetrics(SM_CYSCREEN);
3510 ok(h == 480, "Got unexpected screen height %u.\n", h);
3512 ref = IDirectDraw4_Release(ddraw1);
3513 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3514 w = GetSystemMetrics(SM_CXSCREEN);
3515 ok(w == 640, "Got unexpected screen width %u.\n", w);
3516 h = GetSystemMetrics(SM_CYSCREEN);
3517 ok(h == 480, "Got unexpected screen height %u.\n", h);
3519 ref = IDirectDraw4_Release(ddraw2);
3520 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3521 w = GetSystemMetrics(SM_CXSCREEN);
3522 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3523 h = GetSystemMetrics(SM_CYSCREEN);
3524 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3526 /* If there's a ddraw object that's currently in exclusive mode, it blocks
3527 * restoring the display mode. */
3528 ddraw1 = create_ddraw();
3529 hr = set_display_mode(ddraw1, 800, 600);
3530 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3531 w = GetSystemMetrics(SM_CXSCREEN);
3532 ok(w == 800, "Got unexpected screen width %u.\n", w);
3533 h = GetSystemMetrics(SM_CYSCREEN);
3534 ok(h == 600, "Got unexpected screen height %u.\n", h);
3536 ddraw2 = create_ddraw();
3537 hr = set_display_mode(ddraw2, 640, 480);
3538 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3539 w = GetSystemMetrics(SM_CXSCREEN);
3540 ok(w == 640, "Got unexpected screen width %u.\n", w);
3541 h = GetSystemMetrics(SM_CYSCREEN);
3542 ok(h == 480, "Got unexpected screen height %u.\n", h);
3544 hr = IDirectDraw4_SetCooperativeLevel(ddraw2, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3545 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3547 ref = IDirectDraw4_Release(ddraw1);
3548 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3549 w = GetSystemMetrics(SM_CXSCREEN);
3550 ok(w == 640, "Got unexpected screen width %u.\n", w);
3551 h = GetSystemMetrics(SM_CYSCREEN);
3552 ok(h == 480, "Got unexpected screen height %u.\n", h);
3554 ref = IDirectDraw4_Release(ddraw2);
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 /* Exclusive mode blocks mode setting on other ddraw objects in general. */
3562 ddraw1 = create_ddraw();
3563 hr = set_display_mode(ddraw1, 800, 600);
3564 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3565 w = GetSystemMetrics(SM_CXSCREEN);
3566 ok(w == 800, "Got unexpected screen width %u.\n", w);
3567 h = GetSystemMetrics(SM_CYSCREEN);
3568 ok(h == 600, "Got unexpected screen height %u.\n", h);
3570 hr = IDirectDraw4_SetCooperativeLevel(ddraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3571 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3573 ddraw2 = create_ddraw();
3574 hr = set_display_mode(ddraw2, 640, 480);
3575 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
3577 ref = IDirectDraw4_Release(ddraw1);
3578 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3579 w = GetSystemMetrics(SM_CXSCREEN);
3580 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3581 h = GetSystemMetrics(SM_CYSCREEN);
3582 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3584 ref = IDirectDraw4_Release(ddraw2);
3585 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3586 w = GetSystemMetrics(SM_CXSCREEN);
3587 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3588 h = GetSystemMetrics(SM_CYSCREEN);
3589 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3591 DestroyWindow(window);
3594 static void test_initialize(void)
3596 IDirectDraw4 *ddraw;
3597 HRESULT hr;
3599 ddraw = create_ddraw();
3600 ok(!!ddraw, "Failed to create a ddraw object.\n");
3602 hr = IDirectDraw4_Initialize(ddraw, NULL);
3603 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x.\n", hr);
3604 IDirectDraw4_Release(ddraw);
3606 CoInitialize(NULL);
3607 hr = CoCreateInstance(&CLSID_DirectDraw, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectDraw4, (void **)&ddraw);
3608 ok(SUCCEEDED(hr), "Failed to create IDirectDraw4 instance, hr %#x.\n", hr);
3609 hr = IDirectDraw4_Initialize(ddraw, NULL);
3610 ok(hr == DD_OK, "Initialize returned hr %#x, expected DD_OK.\n", hr);
3611 hr = IDirectDraw4_Initialize(ddraw, NULL);
3612 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x, expected DDERR_ALREADYINITIALIZED.\n", hr);
3613 IDirectDraw4_Release(ddraw);
3614 CoUninitialize();
3617 static void test_coop_level_surf_create(void)
3619 IDirectDrawSurface4 *surface;
3620 IDirectDraw4 *ddraw;
3621 DDSURFACEDESC2 ddsd;
3622 HRESULT hr;
3624 ddraw = create_ddraw();
3625 ok(!!ddraw, "Failed to create a ddraw object.\n");
3627 memset(&ddsd, 0, sizeof(ddsd));
3628 ddsd.dwSize = sizeof(ddsd);
3629 ddsd.dwFlags = DDSD_CAPS;
3630 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3631 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
3632 ok(hr == DDERR_NOCOOPERATIVELEVELSET, "Surface creation returned hr %#x.\n", hr);
3634 IDirectDraw4_Release(ddraw);
3637 static void test_vb_discard(void)
3639 static const struct vec4 quad[] =
3641 { 0.0f, 480.0f, 0.0f, 1.0f},
3642 { 0.0f, 0.0f, 0.0f, 1.0f},
3643 {640.0f, 480.0f, 0.0f, 1.0f},
3644 {640.0f, 0.0f, 0.0f, 1.0f},
3647 IDirect3DDevice3 *device;
3648 IDirect3D3 *d3d;
3649 IDirect3DVertexBuffer *buffer;
3650 HWND window;
3651 HRESULT hr;
3652 D3DVERTEXBUFFERDESC desc;
3653 BYTE *data;
3654 static const unsigned int vbsize = 16;
3655 unsigned int i;
3657 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3658 0, 0, 640, 480, 0, 0, 0, 0);
3660 if (!(device = create_device(window, DDSCL_NORMAL)))
3662 skip("Failed to create a 3D device, skipping test.\n");
3663 DestroyWindow(window);
3664 return;
3667 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
3668 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
3670 memset(&desc, 0, sizeof(desc));
3671 desc.dwSize = sizeof(desc);
3672 desc.dwCaps = D3DVBCAPS_WRITEONLY;
3673 desc.dwFVF = D3DFVF_XYZRHW;
3674 desc.dwNumVertices = vbsize;
3675 hr = IDirect3D3_CreateVertexBuffer(d3d, &desc, &buffer, 0, NULL);
3676 ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr);
3678 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
3679 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
3680 memcpy(data, quad, sizeof(quad));
3681 hr = IDirect3DVertexBuffer_Unlock(buffer);
3682 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
3684 hr = IDirect3DDevice3_BeginScene(device);
3685 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3686 hr = IDirect3DDevice3_DrawPrimitiveVB(device, D3DPT_TRIANGLESTRIP, buffer, 0, 4, 0);
3687 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3688 hr = IDirect3DDevice3_EndScene(device);
3689 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
3691 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
3692 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
3693 memset(data, 0xaa, sizeof(struct vec4) * vbsize);
3694 hr = IDirect3DVertexBuffer_Unlock(buffer);
3695 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
3697 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
3698 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
3699 for (i = 0; i < sizeof(struct vec4) * vbsize; i++)
3701 if (data[i] != 0xaa)
3703 ok(FALSE, "Vertex buffer data byte %u is 0x%02x, expected 0xaa\n", i, data[i]);
3704 break;
3707 hr = IDirect3DVertexBuffer_Unlock(buffer);
3708 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
3710 IDirect3DVertexBuffer_Release(buffer);
3711 IDirect3D3_Release(d3d);
3712 IDirect3DDevice3_Release(device);
3713 DestroyWindow(window);
3716 static void test_coop_level_multi_window(void)
3718 HWND window1, window2;
3719 IDirectDraw4 *ddraw;
3720 HRESULT hr;
3722 window1 = CreateWindowA("static", "ddraw_test1", WS_OVERLAPPEDWINDOW,
3723 0, 0, 640, 480, 0, 0, 0, 0);
3724 window2 = CreateWindowA("static", "ddraw_test2", WS_OVERLAPPEDWINDOW,
3725 0, 0, 640, 480, 0, 0, 0, 0);
3726 ddraw = create_ddraw();
3727 ok(!!ddraw, "Failed to create a ddraw object.\n");
3729 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
3730 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3731 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
3732 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3733 ok(IsWindow(window1), "Window 1 was destroyed.\n");
3734 ok(IsWindow(window2), "Window 2 was destroyed.\n");
3736 IDirectDraw4_Release(ddraw);
3737 DestroyWindow(window2);
3738 DestroyWindow(window1);
3741 static void test_draw_strided(void)
3743 static struct vec3 position[] =
3745 {-1.0, -1.0, 0.0},
3746 {-1.0, 1.0, 0.0},
3747 { 1.0, 1.0, 0.0},
3748 { 1.0, -1.0, 0.0},
3750 static DWORD diffuse[] =
3752 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00,
3754 static WORD indices[] =
3756 0, 1, 2, 2, 3, 0
3759 IDirectDrawSurface4 *rt;
3760 IDirect3DDevice3 *device;
3761 D3DCOLOR color;
3762 HWND window;
3763 HRESULT hr;
3764 D3DDRAWPRIMITIVESTRIDEDDATA strided;
3765 IDirect3DViewport3 *viewport;
3766 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
3768 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3769 0, 0, 640, 480, 0, 0, 0, 0);
3771 if (!(device = create_device(window, DDSCL_NORMAL)))
3773 skip("Failed to create a 3D device, skipping test.\n");
3774 DestroyWindow(window);
3775 return;
3778 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
3779 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3780 viewport = create_viewport(device, 0, 0, 640, 480);
3781 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
3782 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
3783 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0x00000000, 0.0f, 0);
3784 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
3786 hr = IDirect3DDevice3_BeginScene(device);
3787 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3789 memset(&strided, 0x55, sizeof(strided));
3790 strided.position.lpvData = position;
3791 strided.position.dwStride = sizeof(*position);
3792 strided.diffuse.lpvData = diffuse;
3793 strided.diffuse.dwStride = sizeof(*diffuse);
3794 hr = IDirect3DDevice3_DrawIndexedPrimitiveStrided(device, D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE,
3795 &strided, 4, indices, 6, 0);
3796 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3798 hr = IDirect3DDevice3_EndScene(device);
3799 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
3801 color = get_surface_color(rt, 320, 240);
3802 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
3804 IDirect3DViewport3_Release(viewport);
3805 IDirectDrawSurface4_Release(rt);
3806 IDirect3DDevice3_Release(device);
3807 DestroyWindow(window);
3810 static void test_lighting(void)
3812 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
3813 static D3DMATRIX mat =
3815 1.0f, 0.0f, 0.0f, 0.0f,
3816 0.0f, 1.0f, 0.0f, 0.0f,
3817 0.0f, 0.0f, 1.0f, 0.0f,
3818 0.0f, 0.0f, 0.0f, 1.0f,
3820 mat_singular =
3822 1.0f, 0.0f, 1.0f, 0.0f,
3823 0.0f, 1.0f, 0.0f, 0.0f,
3824 1.0f, 0.0f, 1.0f, 0.0f,
3825 0.0f, 0.0f, 0.5f, 1.0f,
3827 mat_transf =
3829 0.0f, 0.0f, 1.0f, 0.0f,
3830 0.0f, 1.0f, 0.0f, 0.0f,
3831 -1.0f, 0.0f, 0.0f, 0.0f,
3832 10.f, 10.0f, 10.0f, 1.0f,
3834 mat_nonaffine =
3836 1.0f, 0.0f, 0.0f, 0.0f,
3837 0.0f, 1.0f, 0.0f, 0.0f,
3838 0.0f, 0.0f, 1.0f, -1.0f,
3839 10.f, 10.0f, 10.0f, 0.0f,
3841 static struct
3843 struct vec3 position;
3844 DWORD diffuse;
3846 unlitquad[] =
3848 {{-1.0f, -1.0f, 0.1f}, 0xffff0000},
3849 {{-1.0f, 0.0f, 0.1f}, 0xffff0000},
3850 {{ 0.0f, 0.0f, 0.1f}, 0xffff0000},
3851 {{ 0.0f, -1.0f, 0.1f}, 0xffff0000},
3853 litquad[] =
3855 {{-1.0f, 0.0f, 0.1f}, 0xff00ff00},
3856 {{-1.0f, 1.0f, 0.1f}, 0xff00ff00},
3857 {{ 0.0f, 1.0f, 0.1f}, 0xff00ff00},
3858 {{ 0.0f, 0.0f, 0.1f}, 0xff00ff00},
3860 static struct
3862 struct vec3 position;
3863 struct vec3 normal;
3864 DWORD diffuse;
3866 unlitnquad[] =
3868 {{0.0f, -1.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xff0000ff},
3869 {{0.0f, 0.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xff0000ff},
3870 {{1.0f, 0.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xff0000ff},
3871 {{1.0f, -1.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xff0000ff},
3873 litnquad[] =
3875 {{0.0f, 0.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xffffff00},
3876 {{0.0f, 1.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xffffff00},
3877 {{1.0f, 1.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xffffff00},
3878 {{1.0f, 0.0f, 0.1f}, {1.0f, 1.0f, 1.0f}, 0xffffff00},
3880 nquad[] =
3882 {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3883 {{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3884 {{ 1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3885 {{ 1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3887 rotatedquad[] =
3889 {{-10.0f, -11.0f, 11.0f}, {-1.0f, 0.0f, 0.0f}, 0xff0000ff},
3890 {{-10.0f, -9.0f, 11.0f}, {-1.0f, 0.0f, 0.0f}, 0xff0000ff},
3891 {{-10.0f, -9.0f, 9.0f}, {-1.0f, 0.0f, 0.0f}, 0xff0000ff},
3892 {{-10.0f, -11.0f, 9.0f}, {-1.0f, 0.0f, 0.0f}, 0xff0000ff},
3894 translatedquad[] =
3896 {{-11.0f, -11.0f, -10.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3897 {{-11.0f, -9.0f, -10.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3898 {{ -9.0f, -9.0f, -10.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3899 {{ -9.0f, -11.0f, -10.0f}, {0.0f, 0.0f, -1.0f}, 0xff0000ff},
3901 static WORD indices[] = {0, 1, 2, 2, 3, 0};
3902 static const struct
3904 D3DMATRIX *world_matrix;
3905 void *quad;
3906 DWORD expected;
3907 const char *message;
3909 tests[] =
3911 {&mat, nquad, 0x000000ff, "Lit quad with light"},
3912 {&mat_singular, nquad, 0x000000b4, "Lit quad with singular world matrix"},
3913 {&mat_transf, rotatedquad, 0x000000ff, "Lit quad with transformation matrix"},
3914 {&mat_nonaffine, translatedquad, 0x000000ff, "Lit quad with non-affine matrix"},
3917 HWND window;
3918 IDirect3D3 *d3d;
3919 IDirect3DDevice3 *device;
3920 IDirectDrawSurface4 *rt;
3921 IDirect3DViewport3 *viewport;
3922 IDirect3DMaterial3 *material;
3923 IDirect3DLight *light;
3924 D3DMATERIALHANDLE mat_handle;
3925 D3DLIGHT2 light_desc;
3926 HRESULT hr;
3927 DWORD fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
3928 DWORD nfvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL;
3929 D3DCOLOR color;
3930 ULONG refcount;
3931 unsigned int i;
3933 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3934 0, 0, 640, 480, 0, 0, 0, 0);
3935 if (!(device = create_device(window, DDSCL_NORMAL)))
3937 skip("Failed to create a 3D device, skipping test.\n");
3938 DestroyWindow(window);
3939 return;
3942 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
3943 ok(SUCCEEDED(hr), "Failed to get D3D interface, hr %#x.\n", hr);
3945 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
3946 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3948 viewport = create_viewport(device, 0, 0, 640, 480);
3949 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
3950 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
3952 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffffff, 0.0f, 0);
3953 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3955 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &mat);
3956 ok(SUCCEEDED(hr), "Failed to set world transformation, hr %#x.\n", hr);
3957 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &mat);
3958 ok(SUCCEEDED(hr), "Failed to set view transformation, hr %#x.\n", hr);
3959 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &mat);
3960 ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
3961 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
3962 ok(SUCCEEDED(hr), "Failed to disable clipping, hr %#x.\n", hr);
3963 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
3964 ok(SUCCEEDED(hr), "Failed to disable zbuffer, hr %#x.\n", hr);
3965 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
3966 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
3967 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_STENCILENABLE, FALSE);
3968 ok(SUCCEEDED(hr), "Failed to disable stencil buffer, hr %#x.\n", hr);
3969 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
3970 ok(SUCCEEDED(hr), "Failed to disable culling, hr %#x.\n", hr);
3972 hr = IDirect3DDevice3_BeginScene(device);
3973 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3975 /* There is no D3DRENDERSTATE_LIGHTING on ddraw < 7. */
3976 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
3977 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
3978 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, unlitquad, 4,
3979 indices, 6, 0);
3980 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3982 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
3983 ok(SUCCEEDED(hr), "Failed to enable lighting, hr %#x.\n", hr);
3984 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, litquad, 4,
3985 indices, 6, 0);
3986 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3988 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
3989 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
3990 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, unlitnquad, 4,
3991 indices, 6, 0);
3992 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3994 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, TRUE);
3995 ok(SUCCEEDED(hr), "Failed to enable lighting, hr %#x.\n", hr);
3996 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, litnquad, 4,
3997 indices, 6, 0);
3998 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
4000 hr = IDirect3DDevice3_EndScene(device);
4001 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
4003 color = get_surface_color(rt, 160, 360);
4004 ok(color == 0x00ff0000, "Unlit quad without normals has color 0x%08x.\n", color);
4005 color = get_surface_color(rt, 160, 120);
4006 ok(color == 0x0000ff00, "Lit quad without normals has color 0x%08x.\n", color);
4007 color = get_surface_color(rt, 480, 360);
4008 ok(color == 0x000000ff, "Unlit quad with normals has color 0x%08x.\n", color);
4009 color = get_surface_color(rt, 480, 120);
4010 ok(color == 0x00ffff00, "Lit quad with normals has color 0x%08x.\n", color);
4012 material = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
4013 hr = IDirect3DMaterial3_GetHandle(material, device, &mat_handle);
4014 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
4015 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
4016 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
4018 hr = IDirect3D3_CreateLight(d3d, &light, NULL);
4019 ok(SUCCEEDED(hr), "Failed to create a light object, hr %#x.\n", hr);
4020 memset(&light_desc, 0, sizeof(light_desc));
4021 light_desc.dwSize = sizeof(light_desc);
4022 light_desc.dltType = D3DLIGHT_DIRECTIONAL;
4023 U1(light_desc.dcvColor).r = 1.0f;
4024 U2(light_desc.dcvColor).g = 1.0f;
4025 U3(light_desc.dcvColor).b = 1.0f;
4026 U4(light_desc.dcvColor).a = 1.0f;
4027 U3(light_desc.dvDirection).z = 1.0f;
4028 hr = IDirect3DLight_SetLight(light, (D3DLIGHT *)&light_desc);
4029 ok(SUCCEEDED(hr), "Failed to set light, hr %#x.\n", hr);
4030 hr = IDirect3DViewport3_AddLight(viewport, light);
4031 ok(SUCCEEDED(hr), "Failed to add a light to the viewport, hr %#x.\n", hr);
4033 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffffff, 0.0f, 0);
4034 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
4036 hr = IDirect3DDevice3_BeginScene(device);
4037 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
4039 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, nquad,
4040 4, indices, 6, 0);
4041 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
4043 hr = IDirect3DDevice3_EndScene(device);
4044 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
4046 color = get_surface_color(rt, 320, 240);
4047 ok(color == 0x00000000, "Lit quad with no light has color 0x%08x.\n", color);
4049 light_desc.dwFlags = D3DLIGHT_ACTIVE;
4050 hr = IDirect3DLight_SetLight(light, (D3DLIGHT *)&light_desc);
4051 ok(SUCCEEDED(hr), "Failed to set light, hr %#x.\n", hr);
4053 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
4055 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, tests[i].world_matrix);
4056 ok(SUCCEEDED(hr), "Failed to set world transformation, hr %#x.\n", hr);
4058 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffffff, 0.0f, 0);
4059 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
4061 hr = IDirect3DDevice3_BeginScene(device);
4062 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
4064 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, nfvf, tests[i].quad,
4065 4, indices, 6, 0);
4066 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
4068 hr = IDirect3DDevice3_EndScene(device);
4069 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
4071 color = get_surface_color(rt, 320, 240);
4072 ok(color == tests[i].expected, "%s has color 0x%08x.\n", tests[i].message, color);
4075 hr = IDirect3DViewport3_DeleteLight(viewport, light);
4076 ok(SUCCEEDED(hr), "Failed to remove a light from the viewport, hr %#x.\n", hr);
4077 IDirect3DLight_Release(light);
4078 destroy_material(material);
4079 IDirect3DViewport3_Release(viewport);
4080 IDirectDrawSurface4_Release(rt);
4081 refcount = IDirect3DDevice3_Release(device);
4082 ok(!refcount, "Device has %u references left.\n", refcount);
4083 IDirect3D3_Release(d3d);
4084 DestroyWindow(window);
4087 static void test_specular_lighting(void)
4089 static const unsigned int vertices_side = 5;
4090 const unsigned int indices_count = (vertices_side - 1) * (vertices_side - 1) * 2 * 3;
4091 static const DWORD fvf = D3DFVF_XYZ | D3DFVF_NORMAL;
4092 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
4093 static D3DMATRIX mat =
4095 1.0f, 0.0f, 0.0f, 0.0f,
4096 0.0f, 1.0f, 0.0f, 0.0f,
4097 0.0f, 0.0f, 1.0f, 0.0f,
4098 0.0f, 0.0f, 0.0f, 1.0f,
4100 static D3DLIGHT2 directional =
4102 sizeof(D3DLIGHT2),
4103 D3DLIGHT_DIRECTIONAL,
4104 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
4105 {{0.0f}, {0.0f}, {0.0f}},
4106 {{0.0f}, {0.0f}, {1.0f}},
4108 point =
4110 sizeof(D3DLIGHT2),
4111 D3DLIGHT_POINT,
4112 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
4113 {{0.0f}, {0.0f}, {0.0f}},
4114 {{0.0f}, {0.0f}, {0.0f}},
4115 100.0f,
4116 0.0f,
4117 0.0f, 0.0f, 1.0f,
4119 spot =
4121 sizeof(D3DLIGHT2),
4122 D3DLIGHT_SPOT,
4123 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
4124 {{0.0f}, {0.0f}, {0.0f}},
4125 {{0.0f}, {0.0f}, {1.0f}},
4126 100.0f,
4127 1.0f,
4128 0.0f, 0.0f, 1.0f,
4129 M_PI / 12.0f, M_PI / 3.0f
4131 parallelpoint =
4133 sizeof(D3DLIGHT2),
4134 D3DLIGHT_PARALLELPOINT,
4135 {{1.0f}, {1.0f}, {1.0f}, {0.0f}},
4136 {{0.5f}, {0.0f}, {-1.0f}},
4137 {{0.0f}, {0.0f}, {0.0f}},
4139 static const struct expected_color
4141 unsigned int x, y;
4142 D3DCOLOR color;
4144 expected_directional[] =
4146 {160, 120, 0x003c3c3c},
4147 {320, 120, 0x00717171},
4148 {480, 120, 0x003c3c3c},
4149 {160, 240, 0x00717171},
4150 {320, 240, 0x00ffffff},
4151 {480, 240, 0x00717171},
4152 {160, 360, 0x003c3c3c},
4153 {320, 360, 0x00717171},
4154 {480, 360, 0x003c3c3c},
4156 expected_point[] =
4158 {160, 120, 0x00000000},
4159 {320, 120, 0x00090909},
4160 {480, 120, 0x00000000},
4161 {160, 240, 0x00090909},
4162 {320, 240, 0x00fafafa},
4163 {480, 240, 0x00090909},
4164 {160, 360, 0x00000000},
4165 {320, 360, 0x00090909},
4166 {480, 360, 0x00000000},
4168 expected_spot[] =
4170 {160, 120, 0x00000000},
4171 {320, 120, 0x00020202},
4172 {480, 120, 0x00000000},
4173 {160, 240, 0x00020202},
4174 {320, 240, 0x00fafafa},
4175 {480, 240, 0x00020202},
4176 {160, 360, 0x00000000},
4177 {320, 360, 0x00020202},
4178 {480, 360, 0x00000000},
4180 expected_parallelpoint[] =
4182 {160, 120, 0x00050505},
4183 {320, 120, 0x002c2c2c},
4184 {480, 120, 0x006e6e6e},
4185 {160, 240, 0x00090909},
4186 {320, 240, 0x00717171},
4187 {480, 240, 0x00ffffff},
4188 {160, 360, 0x00050505},
4189 {320, 360, 0x002c2c2c},
4190 {480, 360, 0x006e6e6e},
4192 static const struct
4194 D3DLIGHT2 *light;
4195 BOOL local_viewer;
4196 const struct expected_color *expected;
4197 unsigned int expected_count;
4199 tests[] =
4201 /* D3DRENDERSTATE_LOCALVIEWER does not exist in D3D < 7 (the behavior is
4202 * the one you get on newer D3D versions with it set as TRUE). */
4203 {&directional, FALSE, expected_directional,
4204 sizeof(expected_directional) / sizeof(expected_directional[0])},
4205 {&directional, TRUE, expected_directional,
4206 sizeof(expected_directional) / sizeof(expected_directional[0])},
4207 {&point, TRUE, expected_point,
4208 sizeof(expected_point) / sizeof(expected_point[0])},
4209 {&spot, TRUE, expected_spot,
4210 sizeof(expected_spot) / sizeof(expected_spot[0])},
4211 {&parallelpoint, TRUE, expected_parallelpoint,
4212 sizeof(expected_parallelpoint) / sizeof(expected_parallelpoint[0])},
4214 IDirect3D3 *d3d;
4215 IDirect3DDevice3 *device;
4216 IDirectDrawSurface4 *rt;
4217 IDirect3DViewport3 *viewport;
4218 IDirect3DMaterial3 *material;
4219 IDirect3DLight *light;
4220 D3DMATERIALHANDLE mat_handle;
4221 D3DCOLOR color;
4222 ULONG refcount;
4223 HWND window;
4224 HRESULT hr;
4225 unsigned int i, j, x, y;
4226 struct
4228 struct vec3 position;
4229 struct vec3 normal;
4230 } *quad;
4231 WORD *indices;
4233 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4234 0, 0, 640, 480, 0, 0, 0, 0);
4235 if (!(device = create_device(window, DDSCL_NORMAL)))
4237 skip("Failed to create a 3D device, skipping test.\n");
4238 DestroyWindow(window);
4239 return;
4242 quad = HeapAlloc(GetProcessHeap(), 0, vertices_side * vertices_side * sizeof(*quad));
4243 indices = HeapAlloc(GetProcessHeap(), 0, indices_count * sizeof(*indices));
4244 for (i = 0, y = 0; y < vertices_side; ++y)
4246 for (x = 0; x < vertices_side; ++x)
4248 quad[i].position.x = x * 2.0f / (vertices_side - 1) - 1.0f;
4249 quad[i].position.y = y * 2.0f / (vertices_side - 1) - 1.0f;
4250 quad[i].position.z = 1.0f;
4251 quad[i].normal.x = 0.0f;
4252 quad[i].normal.y = 0.0f;
4253 quad[i++].normal.z = -1.0f;
4256 for (i = 0, y = 0; y < (vertices_side - 1); ++y)
4258 for (x = 0; x < (vertices_side - 1); ++x)
4260 indices[i++] = y * vertices_side + x + 1;
4261 indices[i++] = y * vertices_side + x;
4262 indices[i++] = (y + 1) * vertices_side + x;
4263 indices[i++] = y * vertices_side + x + 1;
4264 indices[i++] = (y + 1) * vertices_side + x;
4265 indices[i++] = (y + 1) * vertices_side + x + 1;
4269 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
4270 ok(SUCCEEDED(hr), "Failed to get D3D interface, hr %#x.\n", hr);
4272 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
4273 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
4275 viewport = create_viewport(device, 0, 0, 640, 480);
4276 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
4277 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
4279 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &mat);
4280 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
4281 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &mat);
4282 ok(SUCCEEDED(hr), "Failed to set view transform, hr %#x.\n", hr);
4283 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &mat);
4284 ok(SUCCEEDED(hr), "Failed to set projection transform, hr %#x.\n", hr);
4285 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
4286 ok(SUCCEEDED(hr), "Failed to disable clipping, hr %#x.\n", hr);
4287 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
4288 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
4289 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
4290 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
4292 material = create_specular_material(device, 1.0f, 1.0f, 1.0f, 1.0f, 30.0f);
4293 hr = IDirect3DMaterial3_GetHandle(material, device, &mat_handle);
4294 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
4295 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
4296 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
4298 hr = IDirect3D3_CreateLight(d3d, &light, NULL);
4299 ok(SUCCEEDED(hr), "Failed to create a light object, hr %#x.\n", hr);
4300 hr = IDirect3DViewport3_AddLight(viewport, light);
4301 ok(SUCCEEDED(hr), "Failed to add a light to the viewport, hr %#x.\n", hr);
4303 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_SPECULARENABLE, TRUE);
4304 ok(SUCCEEDED(hr), "Failed to enable specular lighting, hr %#x.\n", hr);
4306 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
4308 tests[i].light->dwFlags = D3DLIGHT_ACTIVE;
4309 hr = IDirect3DLight_SetLight(light, (D3DLIGHT *)tests[i].light);
4310 ok(SUCCEEDED(hr), "Failed to set light, hr %#x.\n", hr);
4312 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LOCALVIEWER, tests[i].local_viewer);
4313 ok(SUCCEEDED(hr), "Failed to set local viewer state, hr %#x.\n", hr);
4315 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffffff, 0.0f, 0);
4316 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
4318 hr = IDirect3DDevice3_BeginScene(device);
4319 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
4321 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, fvf, quad,
4322 vertices_side * vertices_side, indices, indices_count, 0);
4323 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
4325 hr = IDirect3DDevice3_EndScene(device);
4326 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
4328 for (j = 0; j < tests[i].expected_count; ++j)
4330 color = get_surface_color(rt, tests[i].expected[j].x, tests[i].expected[j].y);
4331 ok(compare_color(color, tests[i].expected[j].color, 1),
4332 "Expected color 0x%08x at location (%u, %u), got 0x%08x, case %u.\n",
4333 tests[i].expected[j].color, tests[i].expected[j].x,
4334 tests[i].expected[j].y, color, i);
4338 hr = IDirect3DViewport3_DeleteLight(viewport, light);
4339 ok(SUCCEEDED(hr), "Failed to remove a light from the viewport, hr %#x.\n", hr);
4340 IDirect3DLight_Release(light);
4341 destroy_material(material);
4342 IDirect3DViewport3_Release(viewport);
4343 IDirectDrawSurface4_Release(rt);
4344 refcount = IDirect3DDevice3_Release(device);
4345 ok(!refcount, "Device has %u references left.\n", refcount);
4346 IDirect3D3_Release(d3d);
4347 DestroyWindow(window);
4348 HeapFree(GetProcessHeap(), 0, indices);
4349 HeapFree(GetProcessHeap(), 0, quad);
4352 static void test_clear_rect_count(void)
4354 IDirectDrawSurface4 *rt;
4355 IDirect3DDevice3 *device;
4356 D3DCOLOR color;
4357 HWND window;
4358 HRESULT hr;
4359 IDirect3DViewport3 *viewport;
4360 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
4362 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4363 0, 0, 640, 480, 0, 0, 0, 0);
4364 if (!(device = create_device(window, DDSCL_NORMAL)))
4366 skip("Failed to create a 3D device, skipping test.\n");
4367 DestroyWindow(window);
4368 return;
4371 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
4372 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
4374 viewport = create_viewport(device, 0, 0, 640, 480);
4375 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
4376 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
4377 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0x00ffffff, 0.0f, 0);
4378 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
4379 hr = IDirect3DViewport3_Clear2(viewport, 0, &clear_rect, D3DCLEAR_TARGET, 0x00ff0000, 0.0f, 0);
4380 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
4381 hr = IDirect3DViewport3_Clear2(viewport, 0, NULL, D3DCLEAR_TARGET, 0x0000ff00, 0.0f, 0);
4382 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
4383 hr = IDirect3DViewport3_Clear2(viewport, 1, NULL, D3DCLEAR_TARGET, 0x000000ff, 0.0f, 0);
4384 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
4386 color = get_surface_color(rt, 320, 240);
4387 ok(compare_color(color, 0x00ffffff, 1) || broken(compare_color(color, 0x000000ff, 1)),
4388 "Got unexpected color 0x%08x.\n", color);
4390 IDirect3DViewport3_Release(viewport);
4391 IDirectDrawSurface4_Release(rt);
4392 IDirect3DDevice3_Release(device);
4393 DestroyWindow(window);
4396 static BOOL test_mode_restored(IDirectDraw4 *ddraw, HWND window)
4398 DDSURFACEDESC2 ddsd1, ddsd2;
4399 HRESULT hr;
4401 memset(&ddsd1, 0, sizeof(ddsd1));
4402 ddsd1.dwSize = sizeof(ddsd1);
4403 hr = IDirectDraw4_GetDisplayMode(ddraw, &ddsd1);
4404 ok(SUCCEEDED(hr), "GetDisplayMode failed, hr %#x.\n", hr);
4406 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4407 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4408 hr = set_display_mode(ddraw, 640, 480);
4409 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
4410 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4411 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4413 memset(&ddsd2, 0, sizeof(ddsd2));
4414 ddsd2.dwSize = sizeof(ddsd2);
4415 hr = IDirectDraw4_GetDisplayMode(ddraw, &ddsd2);
4416 ok(SUCCEEDED(hr), "GetDisplayMode failed, hr %#x.\n", hr);
4417 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
4418 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
4420 return ddsd1.dwWidth == ddsd2.dwWidth && ddsd1.dwHeight == ddsd2.dwHeight;
4423 static void test_coop_level_versions(void)
4425 HWND window;
4426 IDirectDraw *ddraw;
4427 HRESULT hr;
4428 BOOL restored;
4429 IDirectDrawSurface *surface;
4430 IDirectDraw4 *ddraw4;
4431 DDSURFACEDESC ddsd;
4433 window = CreateWindowA("static", "ddraw_test1", WS_OVERLAPPEDWINDOW,
4434 0, 0, 640, 480, 0, 0, 0, 0);
4436 ddraw4 = create_ddraw();
4437 ok(!!ddraw4, "Failed to create a ddraw object.\n");
4438 /* Newly created ddraw objects restore the mode on ddraw2+::SetCooperativeLevel(NORMAL) */
4439 restored = test_mode_restored(ddraw4, window);
4440 ok(restored, "Display mode not restored in new ddraw object\n");
4442 /* A failing ddraw1::SetCooperativeLevel call does not have an effect */
4443 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
4444 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
4446 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
4447 ok(FAILED(hr), "SetCooperativeLevel returned %#x, expected failure.\n", hr);
4448 restored = test_mode_restored(ddraw4, window);
4449 ok(restored, "Display mode not restored after bad ddraw1::SetCooperativeLevel call\n");
4451 /* A successful one does */
4452 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4453 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4454 restored = test_mode_restored(ddraw4, window);
4455 ok(!restored, "Display mode restored after good ddraw1::SetCooperativeLevel call\n");
4457 IDirectDraw_Release(ddraw);
4458 IDirectDraw4_Release(ddraw4);
4460 ddraw4 = create_ddraw();
4461 ok(!!ddraw4, "Failed to create a ddraw object.\n");
4462 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
4463 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
4465 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_SETFOCUSWINDOW);
4466 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4467 restored = test_mode_restored(ddraw4, window);
4468 ok(!restored, "Display mode restored after ddraw1::SetCooperativeLevel(SETFOCUSWINDOW) call\n");
4470 IDirectDraw_Release(ddraw);
4471 IDirectDraw4_Release(ddraw4);
4473 /* A failing call does not restore the ddraw2+ behavior */
4474 ddraw4 = create_ddraw();
4475 ok(!!ddraw4, "Failed to create a ddraw object.\n");
4476 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
4477 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
4479 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4480 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4481 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
4482 ok(FAILED(hr), "SetCooperativeLevel returned %#x, expected failure.\n", hr);
4483 restored = test_mode_restored(ddraw4, window);
4484 ok(!restored, "Display mode restored after good-bad ddraw1::SetCooperativeLevel() call sequence\n");
4486 IDirectDraw_Release(ddraw);
4487 IDirectDraw4_Release(ddraw4);
4489 /* Neither does a sequence of successful calls with the new interface */
4490 ddraw4 = create_ddraw();
4491 ok(!!ddraw4, "Failed to create a ddraw object.\n");
4492 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
4493 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
4495 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4496 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4497 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
4498 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4499 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, DDSCL_NORMAL);
4500 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4502 restored = test_mode_restored(ddraw4, window);
4503 ok(!restored, "Display mode restored after ddraw1-ddraw4 SetCooperativeLevel() call sequence\n");
4504 IDirectDraw_Release(ddraw);
4505 IDirectDraw4_Release(ddraw4);
4507 /* ddraw1::CreateSurface does not triger the ddraw1 behavior */
4508 ddraw4 = create_ddraw();
4509 ok(!!ddraw4, "Failed to create a ddraw object.\n");
4510 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
4511 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
4513 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, DDSCL_NORMAL);
4514 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
4516 memset(&ddsd, 0, sizeof(ddsd));
4517 ddsd.dwSize = sizeof(ddsd);
4518 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
4519 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
4520 ddsd.dwWidth = ddsd.dwHeight = 8;
4521 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
4522 ok(SUCCEEDED(hr), "CreateSurface failed, hr %#x.\n", hr);
4523 IDirectDrawSurface_Release(surface);
4524 restored = test_mode_restored(ddraw4, window);
4525 ok(restored, "Display mode not restored after ddraw1::CreateSurface() call\n");
4527 IDirectDraw_Release(ddraw);
4528 IDirectDraw4_Release(ddraw4);
4529 DestroyWindow(window);
4532 static void test_lighting_interface_versions(void)
4534 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
4535 IDirect3DMaterial3 *emissive;
4536 IDirect3DViewport3 *viewport;
4537 IDirect3DDevice3 *device;
4538 IDirectDrawSurface4 *rt;
4539 D3DCOLOR color;
4540 HWND window;
4541 HRESULT hr;
4542 D3DMATERIALHANDLE mat_handle;
4543 DWORD rs;
4544 unsigned int i;
4545 ULONG ref;
4546 static D3DVERTEX quad[] =
4548 {{-1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
4549 {{ 1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
4550 {{-1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
4551 {{ 1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
4554 #define FVF_COLORVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR)
4555 static struct
4557 struct vec3 position;
4558 struct vec3 normal;
4559 DWORD diffuse, specular;
4561 quad2[] =
4563 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
4564 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
4565 {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
4566 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
4569 static D3DLVERTEX lquad[] =
4571 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
4572 {{ 1.0f}, { 1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
4573 {{-1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
4574 {{ 1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
4577 #define FVF_LVERTEX2 (D3DFVF_LVERTEX & ~D3DFVF_RESERVED1)
4578 static struct
4580 struct vec3 position;
4581 DWORD diffuse, specular;
4582 struct vec2 texcoord;
4584 lquad2[] =
4586 {{-1.0f, 1.0f, 0.0f}, 0xffff0000, 0xff808080},
4587 {{ 1.0f, 1.0f, 0.0f}, 0xffff0000, 0xff808080},
4588 {{-1.0f, -1.0f, 0.0f}, 0xffff0000, 0xff808080},
4589 {{ 1.0f, -1.0f, 0.0f}, 0xffff0000, 0xff808080},
4592 static D3DTLVERTEX tlquad[] =
4594 {{ 0.0f}, { 480.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
4595 {{ 0.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
4596 {{ 640.0f}, { 480.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
4597 {{ 640.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
4600 static const struct
4602 DWORD vertextype;
4603 void *data;
4604 DWORD d3drs_lighting, d3drs_specular;
4605 DWORD draw_flags;
4606 D3DCOLOR color;
4608 tests[] =
4610 /* Lighting is enabled when all of these conditions are met:
4611 * 1) No pretransformed position(D3DFVF_XYZRHW)
4612 * 2) Normals are available (D3DFVF_NORMAL)
4613 * 3) D3DDP_DONOTLIGHT is not set.
4615 * D3DRENDERSTATE_LIGHTING is ignored, it is not defined
4616 * in this d3d version */
4618 /* 0 */
4619 { D3DFVF_VERTEX, quad, FALSE, FALSE, 0, 0x0000ff00},
4620 { D3DFVF_VERTEX, quad, TRUE, FALSE, 0, 0x0000ff00},
4621 { D3DFVF_VERTEX, quad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ffffff},
4622 { D3DFVF_VERTEX, quad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ffffff},
4623 { D3DFVF_VERTEX, quad, FALSE, TRUE, 0, 0x0000ff00},
4624 { D3DFVF_VERTEX, quad, TRUE, TRUE, 0, 0x0000ff00},
4625 { D3DFVF_VERTEX, quad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ffffff},
4626 { D3DFVF_VERTEX, quad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ffffff},
4628 /* 8 */
4629 { FVF_COLORVERTEX, quad2, FALSE, FALSE, 0, 0x0000ff00},
4630 { FVF_COLORVERTEX, quad2, TRUE, FALSE, 0, 0x0000ff00},
4631 { FVF_COLORVERTEX, quad2, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4632 { FVF_COLORVERTEX, quad2, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4633 /* The specular color in the vertex is ignored because
4634 * D3DRENDERSTATE_COLORVERTEX is not enabled */
4635 { FVF_COLORVERTEX, quad2, FALSE, TRUE, 0, 0x0000ff00},
4636 { FVF_COLORVERTEX, quad2, TRUE, TRUE, 0, 0x0000ff00},
4637 { FVF_COLORVERTEX, quad2, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4638 { FVF_COLORVERTEX, quad2, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4640 /* 16 */
4641 { D3DFVF_LVERTEX, lquad, FALSE, FALSE, 0, 0x00ff0000},
4642 { D3DFVF_LVERTEX, lquad, TRUE, FALSE, 0, 0x00ff0000},
4643 { D3DFVF_LVERTEX, lquad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4644 { D3DFVF_LVERTEX, lquad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4645 { D3DFVF_LVERTEX, lquad, FALSE, TRUE, 0, 0x00ff8080},
4646 { D3DFVF_LVERTEX, lquad, TRUE, TRUE, 0, 0x00ff8080},
4647 { D3DFVF_LVERTEX, lquad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4648 { D3DFVF_LVERTEX, lquad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4650 /* 24 */
4651 { FVF_LVERTEX2, lquad2, FALSE, FALSE, 0, 0x00ff0000},
4652 { FVF_LVERTEX2, lquad2, TRUE, FALSE, 0, 0x00ff0000},
4653 { FVF_LVERTEX2, lquad2, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4654 { FVF_LVERTEX2, lquad2, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4655 { FVF_LVERTEX2, lquad2, FALSE, TRUE, 0, 0x00ff8080},
4656 { FVF_LVERTEX2, lquad2, TRUE, TRUE, 0, 0x00ff8080},
4657 { FVF_LVERTEX2, lquad2, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4658 { FVF_LVERTEX2, lquad2, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4660 /* 32 */
4661 { D3DFVF_TLVERTEX, tlquad, FALSE, FALSE, 0, 0x000000ff},
4662 { D3DFVF_TLVERTEX, tlquad, TRUE, FALSE, 0, 0x000000ff},
4663 { D3DFVF_TLVERTEX, tlquad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x000000ff},
4664 { D3DFVF_TLVERTEX, tlquad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x000000ff},
4665 { D3DFVF_TLVERTEX, tlquad, FALSE, TRUE, 0, 0x008080ff},
4666 { D3DFVF_TLVERTEX, tlquad, TRUE, TRUE, 0, 0x008080ff},
4667 { D3DFVF_TLVERTEX, tlquad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x008080ff},
4668 { D3DFVF_TLVERTEX, tlquad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x008080ff},
4671 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4672 0, 0, 640, 480, 0, 0, 0, 0);
4674 if (!(device = create_device(window, DDSCL_NORMAL)))
4676 skip("Failed to create a 3D device, skipping test.\n");
4677 DestroyWindow(window);
4678 return;
4681 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
4682 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
4684 viewport = create_viewport(device, 0, 0, 640, 480);
4685 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
4686 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
4688 emissive = create_emissive_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
4689 hr = IDirect3DMaterial3_GetHandle(emissive, device, &mat_handle);
4690 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
4691 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
4692 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
4693 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
4694 ok(SUCCEEDED(hr), "Failed to disable z test, hr %#x.\n", hr);
4696 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_SPECULARENABLE, &rs);
4697 ok(SUCCEEDED(hr), "Failed to get specularenable render state, hr %#x.\n", hr);
4698 ok(rs == FALSE, "Initial D3DRENDERSTATE_SPECULARENABLE is %#x, expected FALSE.\n", rs);
4700 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
4702 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff202020, 0.0f, 0);
4703 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
4705 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, tests[i].d3drs_lighting);
4706 ok(SUCCEEDED(hr), "Failed to set lighting render state, hr %#x.\n", hr);
4707 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_SPECULARENABLE,
4708 tests[i].d3drs_specular);
4709 ok(SUCCEEDED(hr), "Failed to set specularenable render state, hr %#x.\n", hr);
4711 hr = IDirect3DDevice3_BeginScene(device);
4712 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
4713 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP,
4714 tests[i].vertextype, tests[i].data, 4, tests[i].draw_flags | D3DDP_WAIT);
4715 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
4716 hr = IDirect3DDevice3_EndScene(device);
4717 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
4719 color = get_surface_color(rt, 320, 240);
4720 ok(compare_color(color, tests[i].color, 1),
4721 "Got unexpected color 0x%08x, expected 0x%08x, test %u.\n",
4722 color, tests[i].color, i);
4725 destroy_material(emissive);
4726 IDirectDrawSurface4_Release(rt);
4727 ref = IDirect3DDevice3_Release(device);
4728 ok(ref == 0, "Device not properly released, refcount %u.\n", ref);
4729 DestroyWindow(window);
4732 static struct
4734 BOOL received;
4735 IDirectDraw4 *ddraw;
4736 HWND window;
4737 DWORD coop_level;
4738 } activateapp_testdata;
4740 static LRESULT CALLBACK activateapp_test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
4742 if (message == WM_ACTIVATEAPP)
4744 if (activateapp_testdata.ddraw)
4746 HRESULT hr;
4747 activateapp_testdata.received = FALSE;
4748 hr = IDirectDraw4_SetCooperativeLevel(activateapp_testdata.ddraw,
4749 activateapp_testdata.window, activateapp_testdata.coop_level);
4750 ok(SUCCEEDED(hr), "Recursive SetCooperativeLevel call failed, hr %#x.\n", hr);
4751 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP during recursive SetCooperativeLevel call.\n");
4753 activateapp_testdata.received = TRUE;
4756 return DefWindowProcA(hwnd, message, wparam, lparam);
4759 static void test_coop_level_activateapp(void)
4761 IDirectDraw4 *ddraw;
4762 HRESULT hr;
4763 HWND window;
4764 WNDCLASSA wc = {0};
4765 DDSURFACEDESC2 ddsd;
4766 IDirectDrawSurface4 *surface;
4768 ddraw = create_ddraw();
4769 ok(!!ddraw, "Failed to create a ddraw object.\n");
4771 wc.lpfnWndProc = activateapp_test_proc;
4772 wc.lpszClassName = "ddraw_test_wndproc_wc";
4773 ok(RegisterClassA(&wc), "Failed to register window class.\n");
4775 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
4776 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
4778 /* Exclusive with window already active. */
4779 SetForegroundWindow(window);
4780 activateapp_testdata.received = FALSE;
4781 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4782 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4783 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP although window was already active.\n");
4784 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4785 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4787 /* Exclusive with window not active. */
4788 SetForegroundWindow(GetDesktopWindow());
4789 activateapp_testdata.received = FALSE;
4790 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4791 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4792 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4793 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4794 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4796 /* Normal with window not active, then exclusive with the same window. */
4797 SetForegroundWindow(GetDesktopWindow());
4798 activateapp_testdata.received = FALSE;
4799 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4800 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4801 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP when setting DDSCL_NORMAL.\n");
4802 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4803 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4804 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4805 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4806 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4808 /* Recursive set of DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN. */
4809 SetForegroundWindow(GetDesktopWindow());
4810 activateapp_testdata.received = FALSE;
4811 activateapp_testdata.ddraw = ddraw;
4812 activateapp_testdata.window = window;
4813 activateapp_testdata.coop_level = DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN;
4814 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4815 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4816 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4817 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4818 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4820 /* The recursive call seems to have some bad effect on native ddraw, despite (apparently)
4821 * succeeding. Another switch to exclusive and back to normal is needed to release the
4822 * window properly. Without doing this, SetCooperativeLevel(EXCLUSIVE) will not send
4823 * WM_ACTIVATEAPP messages. */
4824 activateapp_testdata.ddraw = NULL;
4825 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4826 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4827 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4828 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4830 /* Setting DDSCL_NORMAL with recursive invocation. */
4831 SetForegroundWindow(GetDesktopWindow());
4832 activateapp_testdata.received = FALSE;
4833 activateapp_testdata.ddraw = ddraw;
4834 activateapp_testdata.window = window;
4835 activateapp_testdata.coop_level = DDSCL_NORMAL;
4836 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4837 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4838 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4840 /* DDraw is in exlusive mode now. */
4841 memset(&ddsd, 0, sizeof(ddsd));
4842 ddsd.dwSize = sizeof(ddsd);
4843 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
4844 U5(ddsd).dwBackBufferCount = 1;
4845 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
4846 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
4847 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4848 IDirectDrawSurface4_Release(surface);
4850 /* Recover again, just to be sure. */
4851 activateapp_testdata.ddraw = NULL;
4852 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4853 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4854 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4855 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4857 DestroyWindow(window);
4858 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
4859 IDirectDraw4_Release(ddraw);
4862 static void test_texturemanage(void)
4864 IDirectDraw4 *ddraw;
4865 HRESULT hr;
4866 DDSURFACEDESC2 ddsd;
4867 IDirectDrawSurface4 *surface;
4868 unsigned int i;
4869 DDCAPS hal_caps, hel_caps;
4870 DWORD needed_caps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
4871 static const struct
4873 DWORD caps_in, caps2_in;
4874 HRESULT hr;
4875 DWORD caps_out, caps2_out;
4877 tests[] =
4879 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4880 ~0U, ~0U},
4881 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4882 ~0U, ~0U},
4883 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4884 ~0U, ~0U},
4885 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4886 ~0U, ~0U},
4887 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DD_OK,
4888 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE},
4889 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DD_OK,
4890 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE},
4891 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, 0, DD_OK,
4892 DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE | DDSCAPS_LOCALVIDMEM, 0},
4893 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0, DD_OK,
4894 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0},
4896 {0, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4897 ~0U, ~0U},
4898 {0, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4899 ~0U, ~0U},
4900 {DDSCAPS_SYSTEMMEMORY, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4901 ~0U, ~0U},
4902 {DDSCAPS_SYSTEMMEMORY, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4903 ~0U, ~0U},
4904 {DDSCAPS_VIDEOMEMORY, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4905 ~0U, ~0U},
4906 {DDSCAPS_VIDEOMEMORY, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4907 ~0U, ~0U},
4908 {DDSCAPS_VIDEOMEMORY, 0, DD_OK,
4909 DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY, 0},
4910 {DDSCAPS_SYSTEMMEMORY, 0, DD_OK,
4911 DDSCAPS_SYSTEMMEMORY, 0},
4914 ddraw = create_ddraw();
4915 ok(!!ddraw, "Failed to create a ddraw object.\n");
4916 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4917 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4919 memset(&hal_caps, 0, sizeof(hal_caps));
4920 hal_caps.dwSize = sizeof(hal_caps);
4921 memset(&hel_caps, 0, sizeof(hel_caps));
4922 hel_caps.dwSize = sizeof(hel_caps);
4923 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, &hel_caps);
4924 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
4925 if ((hal_caps.ddsCaps.dwCaps & needed_caps) != needed_caps)
4927 skip("Managed textures not supported, skipping managed texture test.\n");
4928 IDirectDraw4_Release(ddraw);
4929 return;
4932 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
4934 memset(&ddsd, 0, sizeof(ddsd));
4935 ddsd.dwSize = sizeof(ddsd);
4936 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
4937 ddsd.ddsCaps.dwCaps = tests[i].caps_in;
4938 ddsd.ddsCaps.dwCaps2 = tests[i].caps2_in;
4939 ddsd.dwWidth = 4;
4940 ddsd.dwHeight = 4;
4942 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
4943 ok(hr == tests[i].hr, "Got unexpected, hr %#x, case %u.\n", hr, i);
4944 if (FAILED(hr))
4945 continue;
4947 memset(&ddsd, 0, sizeof(ddsd));
4948 ddsd.dwSize = sizeof(ddsd);
4949 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
4950 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4952 ok(ddsd.ddsCaps.dwCaps == tests[i].caps_out,
4953 "Input caps %#x, %#x, expected output caps %#x, got %#x, case %u.\n",
4954 tests[i].caps_in, tests[i].caps2_in, tests[i].caps_out, ddsd.ddsCaps.dwCaps, i);
4955 ok(ddsd.ddsCaps.dwCaps2 == tests[i].caps2_out,
4956 "Input caps %#x, %#x, expected output caps %#x, got %#x, case %u.\n",
4957 tests[i].caps_in, tests[i].caps2_in, tests[i].caps2_out, ddsd.ddsCaps.dwCaps2, i);
4959 IDirectDrawSurface4_Release(surface);
4962 IDirectDraw4_Release(ddraw);
4965 #define SUPPORT_DXT1 0x01
4966 #define SUPPORT_DXT2 0x02
4967 #define SUPPORT_DXT3 0x04
4968 #define SUPPORT_DXT4 0x08
4969 #define SUPPORT_DXT5 0x10
4970 #define SUPPORT_YUY2 0x20
4971 #define SUPPORT_UYVY 0x40
4973 static HRESULT WINAPI test_block_formats_creation_cb(DDPIXELFORMAT *fmt, void *ctx)
4975 DWORD *supported_fmts = ctx;
4977 if (!(fmt->dwFlags & DDPF_FOURCC))
4978 return DDENUMRET_OK;
4980 switch (fmt->dwFourCC)
4982 case MAKEFOURCC('D','X','T','1'):
4983 *supported_fmts |= SUPPORT_DXT1;
4984 break;
4985 case MAKEFOURCC('D','X','T','2'):
4986 *supported_fmts |= SUPPORT_DXT2;
4987 break;
4988 case MAKEFOURCC('D','X','T','3'):
4989 *supported_fmts |= SUPPORT_DXT3;
4990 break;
4991 case MAKEFOURCC('D','X','T','4'):
4992 *supported_fmts |= SUPPORT_DXT4;
4993 break;
4994 case MAKEFOURCC('D','X','T','5'):
4995 *supported_fmts |= SUPPORT_DXT5;
4996 break;
4997 case MAKEFOURCC('Y','U','Y','2'):
4998 *supported_fmts |= SUPPORT_YUY2;
4999 break;
5000 case MAKEFOURCC('U','Y','V','Y'):
5001 *supported_fmts |= SUPPORT_UYVY;
5002 break;
5003 default:
5004 break;
5007 return DDENUMRET_OK;
5010 static void test_block_formats_creation(void)
5012 HRESULT hr, expect_hr;
5013 unsigned int i, j, w, h;
5014 HWND window;
5015 IDirectDraw4 *ddraw;
5016 IDirect3D3 *d3d;
5017 IDirect3DDevice3 *device;
5018 IDirectDrawSurface4 *surface;
5019 DWORD supported_fmts = 0, supported_overlay_fmts = 0;
5020 DWORD num_fourcc_codes = 0, *fourcc_codes;
5021 DDSURFACEDESC2 ddsd;
5022 DDCAPS hal_caps;
5023 void *mem;
5025 static const struct
5027 DWORD fourcc;
5028 const char *name;
5029 DWORD support_flag;
5030 unsigned int block_width;
5031 unsigned int block_height;
5032 unsigned int block_size;
5033 BOOL create_size_checked, overlay;
5035 formats[] =
5037 {MAKEFOURCC('D','X','T','1'), "D3DFMT_DXT1", SUPPORT_DXT1, 4, 4, 8, TRUE, FALSE},
5038 {MAKEFOURCC('D','X','T','2'), "D3DFMT_DXT2", SUPPORT_DXT2, 4, 4, 16, TRUE, FALSE},
5039 {MAKEFOURCC('D','X','T','3'), "D3DFMT_DXT3", SUPPORT_DXT3, 4, 4, 16, TRUE, FALSE},
5040 {MAKEFOURCC('D','X','T','4'), "D3DFMT_DXT4", SUPPORT_DXT4, 4, 4, 16, TRUE, FALSE},
5041 {MAKEFOURCC('D','X','T','5'), "D3DFMT_DXT5", SUPPORT_DXT5, 4, 4, 16, TRUE, FALSE},
5042 {MAKEFOURCC('Y','U','Y','2'), "D3DFMT_YUY2", SUPPORT_YUY2, 2, 1, 4, FALSE, TRUE },
5043 {MAKEFOURCC('U','Y','V','Y'), "D3DFMT_UYVY", SUPPORT_UYVY, 2, 1, 4, FALSE, TRUE },
5045 static const struct
5047 DWORD caps, caps2;
5048 const char *name;
5049 BOOL overlay;
5051 types[] =
5053 /* DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY fails to create any fourcc
5054 * surface with DDERR_INVALIDPIXELFORMAT. Don't care about it for now.
5056 * Nvidia returns E_FAIL on DXTN DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY.
5057 * Other hw / drivers successfully create those surfaces. Ignore them, this
5058 * suggests that no game uses this, otherwise Nvidia would support it. */
5060 DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, 0,
5061 "videomemory texture", FALSE
5064 DDSCAPS_VIDEOMEMORY | DDSCAPS_OVERLAY, 0,
5065 "videomemory overlay", TRUE
5068 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0,
5069 "systemmemory texture", FALSE
5072 DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE,
5073 "managed texture", FALSE
5076 enum size_type
5078 SIZE_TYPE_ZERO,
5079 SIZE_TYPE_PITCH,
5080 SIZE_TYPE_SIZE,
5082 static const struct
5084 DWORD flags;
5085 enum size_type size_type;
5086 int rel_size;
5087 HRESULT hr;
5089 user_mem_tests[] =
5091 {DDSD_LINEARSIZE, SIZE_TYPE_ZERO, 0, DD_OK},
5092 {DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DD_OK},
5093 {DDSD_PITCH, SIZE_TYPE_ZERO, 0, DD_OK},
5094 {DDSD_PITCH, SIZE_TYPE_PITCH, 0, DD_OK},
5095 {DDSD_LPSURFACE, SIZE_TYPE_ZERO, 0, DDERR_INVALIDPARAMS},
5096 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_ZERO, 0, DDERR_INVALIDPARAMS},
5097 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_PITCH, 0, DDERR_INVALIDPARAMS},
5098 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DD_OK},
5099 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 1, DD_OK},
5100 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, -1, DDERR_INVALIDPARAMS},
5101 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_ZERO, 0, DD_OK},
5102 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_PITCH, 0, DD_OK},
5103 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_SIZE, 0, DD_OK},
5104 {DDSD_LPSURFACE | DDSD_PITCH | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DD_OK},
5107 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5108 0, 0, 640, 480, 0, 0, 0, 0);
5110 if (!(device = create_device(window, DDSCL_NORMAL)))
5112 skip("Failed to create a 3D device, skipping test.\n");
5113 DestroyWindow(window);
5114 return;
5117 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
5118 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
5119 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **) &ddraw);
5120 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
5121 IDirect3D3_Release(d3d);
5123 hr = IDirect3DDevice3_EnumTextureFormats(device, test_block_formats_creation_cb,
5124 &supported_fmts);
5125 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
5127 hr = IDirectDraw4_GetFourCCCodes(ddraw, &num_fourcc_codes, NULL);
5128 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
5129 fourcc_codes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
5130 num_fourcc_codes * sizeof(*fourcc_codes));
5131 if (!fourcc_codes)
5132 goto cleanup;
5133 hr = IDirectDraw4_GetFourCCCodes(ddraw, &num_fourcc_codes, fourcc_codes);
5134 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
5135 for (i = 0; i < num_fourcc_codes; i++)
5137 for (j = 0; j < sizeof(formats) / sizeof(*formats); j++)
5139 if (fourcc_codes[i] == formats[j].fourcc)
5140 supported_overlay_fmts |= formats[j].support_flag;
5143 HeapFree(GetProcessHeap(), 0, fourcc_codes);
5145 memset(&hal_caps, 0, sizeof(hal_caps));
5146 hal_caps.dwSize = sizeof(hal_caps);
5147 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
5148 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
5150 mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 2 * 2 * 16 + 1);
5152 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
5154 for (j = 0; j < sizeof(types) / sizeof(*types); j++)
5156 BOOL support;
5158 if (formats[i].overlay != types[j].overlay
5159 || (types[j].overlay && !(hal_caps.dwCaps & DDCAPS_OVERLAY)))
5160 continue;
5162 if (formats[i].overlay)
5163 support = supported_overlay_fmts & formats[i].support_flag;
5164 else
5165 support = supported_fmts & formats[i].support_flag;
5167 for (w = 1; w <= 8; w++)
5169 for (h = 1; h <= 8; h++)
5171 BOOL block_aligned = TRUE;
5172 BOOL todo = FALSE;
5174 if (w & (formats[i].block_width - 1) || h & (formats[i].block_height - 1))
5175 block_aligned = FALSE;
5177 memset(&ddsd, 0, sizeof(ddsd));
5178 ddsd.dwSize = sizeof(ddsd);
5179 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
5180 ddsd.ddsCaps.dwCaps = types[j].caps;
5181 ddsd.ddsCaps.dwCaps2 = types[j].caps2;
5182 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5183 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
5184 U4(ddsd).ddpfPixelFormat.dwFourCC = formats[i].fourcc;
5185 ddsd.dwWidth = w;
5186 ddsd.dwHeight = h;
5188 /* TODO: Handle power of two limitations. I cannot test the pow2
5189 * behavior on windows because I have no hardware that doesn't at
5190 * least support np2_conditional. There's probably no HW that
5191 * supports DXTN textures but no conditional np2 textures. */
5192 if (!support && !(types[j].caps & DDSCAPS_SYSTEMMEMORY))
5193 expect_hr = DDERR_INVALIDPARAMS;
5194 else if (formats[i].create_size_checked && !block_aligned)
5196 expect_hr = DDERR_INVALIDPARAMS;
5197 if (!(types[j].caps & DDSCAPS_TEXTURE))
5198 todo = TRUE;
5200 else
5201 expect_hr = D3D_OK;
5203 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5204 todo_wine_if (todo)
5205 ok(hr == expect_hr,
5206 "Got unexpected hr %#x for format %s, resource type %s, size %ux%u, expected %#x.\n",
5207 hr, formats[i].name, types[j].name, w, h, expect_hr);
5209 if (SUCCEEDED(hr))
5210 IDirectDrawSurface4_Release(surface);
5215 if (formats[i].overlay)
5216 continue;
5218 for (j = 0; j < sizeof(user_mem_tests) / sizeof(*user_mem_tests); ++j)
5220 memset(&ddsd, 0, sizeof(ddsd));
5221 ddsd.dwSize = sizeof(ddsd);
5222 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | user_mem_tests[j].flags;
5223 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE;
5225 switch (user_mem_tests[j].size_type)
5227 case SIZE_TYPE_ZERO:
5228 U1(ddsd).dwLinearSize = 0;
5229 break;
5231 case SIZE_TYPE_PITCH:
5232 U1(ddsd).dwLinearSize = 2 * formats[i].block_size;
5233 break;
5235 case SIZE_TYPE_SIZE:
5236 U1(ddsd).dwLinearSize = 2 * 2 * formats[i].block_size;
5237 break;
5239 U1(ddsd).dwLinearSize += user_mem_tests[j].rel_size;
5241 ddsd.lpSurface = mem;
5242 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5243 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
5244 U4(ddsd).ddpfPixelFormat.dwFourCC = formats[i].fourcc;
5245 ddsd.dwWidth = 8;
5246 ddsd.dwHeight = 8;
5248 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5249 ok(hr == user_mem_tests[j].hr, "Test %u: Got unexpected hr %#x, format %s.\n", j, hr, formats[i].name);
5251 if (FAILED(hr))
5252 continue;
5254 memset(&ddsd, 0, sizeof(ddsd));
5255 ddsd.dwSize = sizeof(ddsd);
5256 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
5257 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", j, hr);
5258 ok(ddsd.dwFlags == (DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_LINEARSIZE),
5259 "Test %u: Got unexpected flags %#x.\n", j, ddsd.dwFlags);
5260 if (user_mem_tests[j].flags & DDSD_LPSURFACE)
5261 ok(U1(ddsd).dwLinearSize == ~0u, "Test %u: Got unexpected linear size %#x.\n",
5262 j, U1(ddsd).dwLinearSize);
5263 else
5264 ok(U1(ddsd).dwLinearSize == 2 * 2 * formats[i].block_size,
5265 "Test %u: Got unexpected linear size %#x, expected %#x.\n",
5266 j, U1(ddsd).dwLinearSize, 2 * 2 * formats[i].block_size);
5267 IDirectDrawSurface4_Release(surface);
5271 HeapFree(GetProcessHeap(), 0, mem);
5272 cleanup:
5273 IDirectDraw4_Release(ddraw);
5274 IDirect3DDevice3_Release(device);
5275 DestroyWindow(window);
5278 struct format_support_check
5280 const DDPIXELFORMAT *format;
5281 BOOL supported;
5284 static HRESULT WINAPI test_unsupported_formats_cb(DDPIXELFORMAT *fmt, void *ctx)
5286 struct format_support_check *format = ctx;
5288 if (!memcmp(format->format, fmt, sizeof(*fmt)))
5290 format->supported = TRUE;
5291 return DDENUMRET_CANCEL;
5294 return DDENUMRET_OK;
5297 static void test_unsupported_formats(void)
5299 HRESULT hr;
5300 BOOL expect_success;
5301 HWND window;
5302 IDirectDraw4 *ddraw;
5303 IDirect3D3 *d3d;
5304 IDirect3DDevice3 *device;
5305 IDirectDrawSurface4 *surface;
5306 DDSURFACEDESC2 ddsd;
5307 unsigned int i, j;
5308 DWORD expected_caps;
5309 static const struct
5311 const char *name;
5312 DDPIXELFORMAT fmt;
5314 formats[] =
5317 "D3DFMT_A8R8G8B8",
5319 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
5320 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
5324 "D3DFMT_P8",
5326 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
5327 {8 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
5331 static const DWORD caps[] = {0, DDSCAPS_SYSTEMMEMORY, DDSCAPS_VIDEOMEMORY};
5333 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5334 0, 0, 640, 480, 0, 0, 0, 0);
5336 if (!(device = create_device(window, DDSCL_NORMAL)))
5338 skip("Failed to create a 3D device, skipping test.\n");
5339 DestroyWindow(window);
5340 return;
5343 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
5344 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
5345 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **) &ddraw);
5346 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
5347 IDirect3D3_Release(d3d);
5349 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
5351 struct format_support_check check = {&formats[i].fmt, FALSE};
5352 hr = IDirect3DDevice3_EnumTextureFormats(device, test_unsupported_formats_cb, &check);
5353 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
5355 for (j = 0; j < sizeof(caps) / sizeof(*caps); j++)
5357 memset(&ddsd, 0, sizeof(ddsd));
5358 ddsd.dwSize = sizeof(ddsd);
5359 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5360 U4(ddsd).ddpfPixelFormat = formats[i].fmt;
5361 ddsd.dwWidth = 4;
5362 ddsd.dwHeight = 4;
5363 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | caps[j];
5365 if (caps[j] & DDSCAPS_VIDEOMEMORY && !check.supported)
5366 expect_success = FALSE;
5367 else
5368 expect_success = TRUE;
5370 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5371 ok(SUCCEEDED(hr) == expect_success,
5372 "Got unexpected hr %#x for format %s, caps %#x, expected %s.\n",
5373 hr, formats[i].name, caps[j], expect_success ? "success" : "failure");
5374 if (FAILED(hr))
5375 continue;
5377 memset(&ddsd, 0, sizeof(ddsd));
5378 ddsd.dwSize = sizeof(ddsd);
5379 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
5380 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5382 if (caps[j] & DDSCAPS_VIDEOMEMORY)
5383 expected_caps = DDSCAPS_VIDEOMEMORY;
5384 else if (caps[j] & DDSCAPS_SYSTEMMEMORY)
5385 expected_caps = DDSCAPS_SYSTEMMEMORY;
5386 else if (check.supported)
5387 expected_caps = DDSCAPS_VIDEOMEMORY;
5388 else
5389 expected_caps = DDSCAPS_SYSTEMMEMORY;
5391 ok(ddsd.ddsCaps.dwCaps & expected_caps,
5392 "Expected capability %#x, format %s, input cap %#x.\n",
5393 expected_caps, formats[i].name, caps[j]);
5395 IDirectDrawSurface4_Release(surface);
5399 IDirectDraw4_Release(ddraw);
5400 IDirect3DDevice3_Release(device);
5401 DestroyWindow(window);
5404 static void test_rt_caps(void)
5406 PALETTEENTRY palette_entries[256];
5407 IDirectDrawPalette *palette;
5408 IDirectDraw4 *ddraw;
5409 DDPIXELFORMAT z_fmt;
5410 IDirect3D3 *d3d;
5411 unsigned int i;
5412 ULONG refcount;
5413 HWND window;
5414 HRESULT hr;
5416 static const DDPIXELFORMAT p8_fmt =
5418 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
5419 {8}, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000},
5422 const struct
5424 const DDPIXELFORMAT *pf;
5425 DWORD caps_in;
5426 DWORD caps_out;
5427 HRESULT create_device_hr;
5428 HRESULT set_rt_hr, alternative_set_rt_hr;
5430 test_data[] =
5433 NULL,
5434 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
5435 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5436 D3D_OK,
5437 D3D_OK,
5438 D3D_OK,
5441 NULL,
5442 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
5443 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5444 D3D_OK,
5445 D3D_OK,
5446 D3D_OK,
5449 NULL,
5450 DDSCAPS_OFFSCREENPLAIN,
5451 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5452 DDERR_INVALIDCAPS,
5453 DDERR_INVALIDCAPS,
5454 DDERR_INVALIDCAPS,
5457 NULL,
5458 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5459 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5460 D3DERR_SURFACENOTINVIDMEM,
5461 D3D_OK,
5462 D3D_OK,
5465 NULL,
5466 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
5467 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
5468 DDERR_INVALIDCAPS,
5469 DDERR_INVALIDCAPS,
5470 DDERR_INVALIDCAPS,
5473 NULL,
5474 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
5475 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5476 D3D_OK,
5477 D3D_OK,
5478 D3D_OK,
5481 NULL,
5482 DDSCAPS_3DDEVICE,
5483 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5484 D3D_OK,
5485 D3D_OK,
5486 D3D_OK,
5489 NULL,
5491 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5492 DDERR_INVALIDCAPS,
5493 DDERR_INVALIDCAPS,
5494 DDERR_INVALIDCAPS,
5497 NULL,
5498 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5499 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5500 D3DERR_SURFACENOTINVIDMEM,
5501 D3D_OK,
5502 D3D_OK,
5505 NULL,
5506 DDSCAPS_SYSTEMMEMORY,
5507 DDSCAPS_SYSTEMMEMORY,
5508 DDERR_INVALIDCAPS,
5509 DDERR_INVALIDCAPS,
5510 DDERR_INVALIDCAPS,
5513 &p8_fmt,
5515 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5516 DDERR_INVALIDCAPS,
5517 DDERR_INVALIDCAPS,
5518 DDERR_INVALIDCAPS,
5521 &p8_fmt,
5522 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
5523 ~0U /* AMD r200 */,
5524 DDERR_NOPALETTEATTACHED,
5525 DDERR_INVALIDCAPS,
5526 DDERR_INVALIDCAPS,
5529 &p8_fmt,
5530 DDSCAPS_OFFSCREENPLAIN,
5531 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
5532 DDERR_INVALIDCAPS,
5533 DDERR_INVALIDCAPS,
5534 DDERR_INVALIDCAPS,
5537 &p8_fmt,
5538 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5539 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
5540 DDERR_NOPALETTEATTACHED,
5541 DDERR_INVALIDCAPS,
5542 DDERR_INVALIDCAPS,
5545 &p8_fmt,
5546 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
5547 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
5548 DDERR_INVALIDCAPS,
5549 DDERR_INVALIDCAPS,
5550 DDERR_INVALIDCAPS,
5553 &z_fmt,
5554 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER,
5555 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
5556 DDERR_INVALIDCAPS,
5557 DDERR_INVALIDPIXELFORMAT,
5558 D3D_OK /* r200 */,
5561 &z_fmt,
5562 DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
5563 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
5564 DDERR_INVALIDCAPS,
5565 DDERR_INVALIDPIXELFORMAT,
5566 D3D_OK /* r200 */,
5569 &z_fmt,
5570 DDSCAPS_ZBUFFER,
5571 DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
5572 DDERR_INVALIDCAPS,
5573 DDERR_INVALIDCAPS,
5574 DDERR_INVALIDCAPS,
5577 &z_fmt,
5578 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
5579 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
5580 DDERR_INVALIDCAPS,
5581 DDERR_INVALIDPIXELFORMAT,
5582 D3D_OK /* r200 */,
5585 &z_fmt,
5586 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
5587 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
5588 DDERR_INVALIDCAPS,
5589 DDERR_INVALIDCAPS,
5590 DDERR_INVALIDCAPS,
5594 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5595 0, 0, 640, 480, 0, 0, 0, 0);
5596 ddraw = create_ddraw();
5597 ok(!!ddraw, "Failed to create a ddraw object.\n");
5598 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5599 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5601 if (FAILED(IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d)))
5603 skip("D3D interface is not available, skipping test.\n");
5604 goto done;
5607 memset(&z_fmt, 0, sizeof(z_fmt));
5608 hr = IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
5609 if (FAILED(hr) || !z_fmt.dwSize)
5611 skip("No depth buffer formats available, skipping test.\n");
5612 IDirect3D3_Release(d3d);
5613 goto done;
5616 memset(palette_entries, 0, sizeof(palette_entries));
5617 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
5618 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5620 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
5622 IDirectDrawSurface4 *surface, *rt, *expected_rt, *tmp;
5623 DDSURFACEDESC2 surface_desc;
5624 IDirect3DDevice3 *device;
5626 memset(&surface_desc, 0, sizeof(surface_desc));
5627 surface_desc.dwSize = sizeof(surface_desc);
5628 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5629 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
5630 if (test_data[i].pf)
5632 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
5633 U4(surface_desc).ddpfPixelFormat = *test_data[i].pf;
5635 surface_desc.dwWidth = 640;
5636 surface_desc.dwHeight = 480;
5637 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5638 ok(SUCCEEDED(hr), "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
5639 i, test_data[i].caps_in, hr);
5641 memset(&surface_desc, 0, sizeof(surface_desc));
5642 surface_desc.dwSize = sizeof(surface_desc);
5643 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
5644 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
5645 ok(test_data[i].caps_out == ~0U || surface_desc.ddsCaps.dwCaps == test_data[i].caps_out,
5646 "Test %u: Got unexpected caps %#x, expected %#x.\n",
5647 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
5649 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device, NULL);
5650 ok(hr == test_data[i].create_device_hr, "Test %u: Got unexpected hr %#x, expected %#x.\n",
5651 i, hr, test_data[i].create_device_hr);
5652 if (FAILED(hr))
5654 if (hr == DDERR_NOPALETTEATTACHED)
5656 hr = IDirectDrawSurface4_SetPalette(surface, palette);
5657 ok(SUCCEEDED(hr), "Test %u: Failed to set palette, hr %#x.\n", i, hr);
5658 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device, NULL);
5659 if (surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5660 ok(hr == DDERR_INVALIDPIXELFORMAT, "Test %u: Got unexpected hr %#x.\n", i, hr);
5661 else
5662 ok(hr == D3DERR_SURFACENOTINVIDMEM, "Test %u: Got unexpected hr %#x.\n", i, hr);
5664 IDirectDrawSurface4_Release(surface);
5666 memset(&surface_desc, 0, sizeof(surface_desc));
5667 surface_desc.dwSize = sizeof(surface_desc);
5668 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5669 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
5670 surface_desc.dwWidth = 640;
5671 surface_desc.dwHeight = 480;
5672 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5673 ok(SUCCEEDED(hr), "Test %u: Failed to create surface, hr %#x.\n", i, hr);
5675 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device, NULL);
5676 ok(SUCCEEDED(hr), "Test %u: Failed to create device, hr %#x.\n", i, hr);
5679 memset(&surface_desc, 0, sizeof(surface_desc));
5680 surface_desc.dwSize = sizeof(surface_desc);
5681 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5682 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
5683 if (test_data[i].pf)
5685 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
5686 U4(surface_desc).ddpfPixelFormat = *test_data[i].pf;
5688 surface_desc.dwWidth = 640;
5689 surface_desc.dwHeight = 480;
5690 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &rt, NULL);
5691 ok(SUCCEEDED(hr), "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
5692 i, test_data[i].caps_in, hr);
5694 hr = IDirect3DDevice3_SetRenderTarget(device, rt, 0);
5695 ok(hr == test_data[i].set_rt_hr || broken(hr == test_data[i].alternative_set_rt_hr),
5696 "Test %u: Got unexpected hr %#x, expected %#x.\n",
5697 i, hr, test_data[i].set_rt_hr);
5698 if (SUCCEEDED(hr) || hr == DDERR_INVALIDPIXELFORMAT)
5699 expected_rt = rt;
5700 else
5701 expected_rt = surface;
5703 hr = IDirect3DDevice3_GetRenderTarget(device, &tmp);
5704 ok(SUCCEEDED(hr), "Test %u: Failed to get render target, hr %#x.\n", i, hr);
5705 ok(tmp == expected_rt, "Test %u: Got unexpected rt %p.\n", i, tmp);
5707 IDirectDrawSurface4_Release(tmp);
5708 IDirectDrawSurface4_Release(rt);
5709 refcount = IDirect3DDevice3_Release(device);
5710 ok(refcount == 0, "Test %u: The device was not properly freed, refcount %u.\n", i, refcount);
5711 refcount = IDirectDrawSurface4_Release(surface);
5712 ok(refcount == 0, "Test %u: The surface was not properly freed, refcount %u.\n", i, refcount);
5715 IDirectDrawPalette_Release(palette);
5716 IDirect3D3_Release(d3d);
5718 done:
5719 refcount = IDirectDraw4_Release(ddraw);
5720 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
5721 DestroyWindow(window);
5724 static void test_primary_caps(void)
5726 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
5727 IDirectDrawSurface4 *surface;
5728 DDSURFACEDESC2 surface_desc;
5729 IDirectDraw4 *ddraw;
5730 unsigned int i;
5731 ULONG refcount;
5732 HWND window;
5733 HRESULT hr;
5735 static const struct
5737 DWORD coop_level;
5738 DWORD caps_in;
5739 DWORD back_buffer_count;
5740 HRESULT hr;
5741 DWORD caps_out;
5743 test_data[] =
5746 DDSCL_NORMAL,
5747 DDSCAPS_PRIMARYSURFACE,
5748 ~0u,
5749 DD_OK,
5750 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE,
5753 DDSCL_NORMAL,
5754 DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE,
5755 ~0u,
5756 DDERR_INVALIDCAPS,
5757 ~0u,
5760 DDSCL_NORMAL,
5761 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER,
5762 ~0u,
5763 DDERR_INVALIDCAPS,
5764 ~0u,
5767 DDSCL_NORMAL,
5768 DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER,
5769 ~0u,
5770 DDERR_INVALIDCAPS,
5771 ~0u,
5774 DDSCL_NORMAL,
5775 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP,
5776 ~0u,
5777 DDERR_INVALIDCAPS,
5778 ~0u,
5781 DDSCL_NORMAL,
5782 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX,
5783 ~0u,
5784 DDERR_INVALIDCAPS,
5785 ~0u,
5788 DDSCL_NORMAL,
5789 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5790 ~0u,
5791 DDERR_INVALIDCAPS,
5792 ~0u,
5795 DDSCL_NORMAL,
5796 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5798 DDERR_INVALIDCAPS,
5799 ~0u,
5802 DDSCL_NORMAL,
5803 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5805 DDERR_NOEXCLUSIVEMODE,
5806 ~0u,
5809 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5810 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5812 DDERR_INVALIDCAPS,
5813 ~0u,
5816 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5817 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5819 DD_OK,
5820 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX,
5823 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5824 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER,
5826 DDERR_INVALIDCAPS,
5827 ~0u,
5830 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5831 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_BACKBUFFER,
5833 DDERR_INVALIDCAPS,
5834 ~0u,
5838 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5839 0, 0, 640, 480, 0, 0, 0, 0);
5840 ddraw = create_ddraw();
5841 ok(!!ddraw, "Failed to create a ddraw object.\n");
5843 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
5845 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, test_data[i].coop_level);
5846 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5848 memset(&surface_desc, 0, sizeof(surface_desc));
5849 surface_desc.dwSize = sizeof(surface_desc);
5850 surface_desc.dwFlags = DDSD_CAPS;
5851 if (test_data[i].back_buffer_count != ~0u)
5852 surface_desc.dwFlags |= DDSD_BACKBUFFERCOUNT;
5853 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
5854 U5(surface_desc).dwBackBufferCount = test_data[i].back_buffer_count;
5855 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5856 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
5857 if (FAILED(hr))
5858 continue;
5860 memset(&surface_desc, 0, sizeof(surface_desc));
5861 surface_desc.dwSize = sizeof(surface_desc);
5862 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
5863 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
5864 ok((surface_desc.ddsCaps.dwCaps & ~placement) == test_data[i].caps_out,
5865 "Test %u: Got unexpected caps %#x, expected %#x.\n",
5866 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
5868 IDirectDrawSurface4_Release(surface);
5871 refcount = IDirectDraw4_Release(ddraw);
5872 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
5873 DestroyWindow(window);
5876 static void test_surface_lock(void)
5878 IDirectDraw4 *ddraw;
5879 IDirect3D3 *d3d = NULL;
5880 IDirectDrawSurface4 *surface;
5881 HRESULT hr;
5882 HWND window;
5883 unsigned int i;
5884 DDSURFACEDESC2 ddsd;
5885 ULONG refcount;
5886 DDPIXELFORMAT z_fmt;
5887 static const struct
5889 DWORD caps;
5890 DWORD caps2;
5891 const char *name;
5893 tests[] =
5896 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
5898 "videomemory offscreenplain"
5901 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
5903 "systemmemory offscreenplain"
5906 DDSCAPS_PRIMARYSURFACE,
5908 "primary"
5911 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
5913 "videomemory texture"
5916 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
5917 DDSCAPS2_OPAQUE,
5918 "opaque videomemory texture"
5921 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
5923 "systemmemory texture"
5926 DDSCAPS_TEXTURE,
5927 DDSCAPS2_TEXTUREMANAGE,
5928 "managed texture"
5931 DDSCAPS_TEXTURE,
5932 DDSCAPS2_D3DTEXTUREMANAGE,
5933 "managed texture"
5936 DDSCAPS_TEXTURE,
5937 DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_OPAQUE,
5938 "opaque managed texture"
5941 DDSCAPS_TEXTURE,
5942 DDSCAPS2_D3DTEXTUREMANAGE | DDSCAPS2_OPAQUE,
5943 "opaque managed texture"
5946 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
5948 "render target"
5951 DDSCAPS_ZBUFFER,
5953 "Z buffer"
5957 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5958 0, 0, 640, 480, 0, 0, 0, 0);
5959 ddraw = create_ddraw();
5960 ok(!!ddraw, "Failed to create a ddraw object.\n");
5961 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5962 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5964 if (FAILED(IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d)))
5966 skip("D3D interface is not available, skipping test.\n");
5967 goto done;
5970 memset(&z_fmt, 0, sizeof(z_fmt));
5971 hr = IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
5972 if (FAILED(hr) || !z_fmt.dwSize)
5974 skip("No depth buffer formats available, skipping test.\n");
5975 goto done;
5978 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
5980 memset(&ddsd, 0, sizeof(ddsd));
5981 ddsd.dwSize = sizeof(ddsd);
5982 ddsd.dwFlags = DDSD_CAPS;
5983 if (!(tests[i].caps & DDSCAPS_PRIMARYSURFACE))
5985 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
5986 ddsd.dwWidth = 64;
5987 ddsd.dwHeight = 64;
5989 if (tests[i].caps & DDSCAPS_ZBUFFER)
5991 ddsd.dwFlags |= DDSD_PIXELFORMAT;
5992 U4(ddsd).ddpfPixelFormat = z_fmt;
5994 ddsd.ddsCaps.dwCaps = tests[i].caps;
5995 ddsd.ddsCaps.dwCaps2 = tests[i].caps2;
5997 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5998 ok(SUCCEEDED(hr), "Failed to create surface, type %s, hr %#x.\n", tests[i].name, hr);
6000 memset(&ddsd, 0, sizeof(ddsd));
6001 ddsd.dwSize = sizeof(ddsd);
6002 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, DDLOCK_WAIT, NULL);
6003 ok(SUCCEEDED(hr), "Failed to lock surface, type %s, hr %#x.\n", tests[i].name, hr);
6004 if (SUCCEEDED(hr))
6006 hr = IDirectDrawSurface4_Unlock(surface, NULL);
6007 ok(SUCCEEDED(hr), "Failed to unlock surface, type %s, hr %#x.\n", tests[i].name, hr);
6010 IDirectDrawSurface4_Release(surface);
6013 done:
6014 if (d3d)
6015 IDirect3D3_Release(d3d);
6016 refcount = IDirectDraw4_Release(ddraw);
6017 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
6018 DestroyWindow(window);
6021 static void test_surface_discard(void)
6023 IDirect3DDevice3 *device;
6024 IDirect3D3 *d3d;
6025 IDirectDraw4 *ddraw;
6026 HRESULT hr;
6027 HWND window;
6028 DDSURFACEDESC2 ddsd;
6029 IDirectDrawSurface4 *surface, *target;
6030 void *addr;
6031 static const struct
6033 DWORD caps, caps2;
6034 BOOL discard;
6036 tests[] =
6038 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0, TRUE},
6039 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, 0, FALSE},
6040 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, 0, TRUE},
6041 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0, FALSE},
6042 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, FALSE},
6043 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_HINTDYNAMIC, FALSE},
6044 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, FALSE},
6045 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE | DDSCAPS2_HINTDYNAMIC, FALSE},
6047 unsigned int i;
6049 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6050 0, 0, 640, 480, 0, 0, 0, 0);
6052 if (!(device = create_device(window, DDSCL_NORMAL)))
6054 skip("Failed to create a 3D device, skipping test.\n");
6055 DestroyWindow(window);
6056 return;
6058 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
6059 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
6060 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
6061 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
6062 hr = IDirect3DDevice3_GetRenderTarget(device, &target);
6063 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
6065 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
6067 BOOL discarded;
6069 memset(&ddsd, 0, sizeof(ddsd));
6070 ddsd.dwSize = sizeof(ddsd);
6071 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6072 ddsd.ddsCaps.dwCaps = tests[i].caps;
6073 ddsd.ddsCaps.dwCaps2 = tests[i].caps2;
6074 ddsd.dwWidth = 64;
6075 ddsd.dwHeight = 64;
6076 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6077 ok(SUCCEEDED(hr), "Failed to create offscreen surface, hr %#x, case %u.\n", hr, i);
6079 memset(&ddsd, 0, sizeof(ddsd));
6080 ddsd.dwSize = sizeof(ddsd);
6081 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, 0, NULL);
6082 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
6083 addr = ddsd.lpSurface;
6084 hr = IDirectDrawSurface4_Unlock(surface, NULL);
6085 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
6087 memset(&ddsd, 0, sizeof(ddsd));
6088 ddsd.dwSize = sizeof(ddsd);
6089 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS, NULL);
6090 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
6091 discarded = ddsd.lpSurface != addr;
6092 hr = IDirectDrawSurface4_Unlock(surface, NULL);
6093 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
6095 hr = IDirectDrawSurface4_Blt(target, NULL, surface, NULL, DDBLT_WAIT, NULL);
6096 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
6098 memset(&ddsd, 0, sizeof(ddsd));
6099 ddsd.dwSize = sizeof(ddsd);
6100 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS, NULL);
6101 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
6102 discarded |= ddsd.lpSurface != addr;
6103 hr = IDirectDrawSurface4_Unlock(surface, NULL);
6104 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
6106 IDirectDrawSurface4_Release(surface);
6108 /* Windows 7 reliably changes the address of surfaces that are discardable (Nvidia Kepler,
6109 * AMD r500, evergreen). Windows XP, at least on AMD r200, does not. */
6110 ok(!discarded || tests[i].discard, "Expected surface not to be discarded, case %u\n", i);
6113 IDirectDrawSurface4_Release(target);
6114 IDirectDraw4_Release(ddraw);
6115 IDirect3D3_Release(d3d);
6116 IDirect3DDevice3_Release(device);
6117 DestroyWindow(window);
6120 static void test_flip(void)
6122 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
6123 IDirectDrawSurface4 *frontbuffer, *backbuffer1, *backbuffer2, *backbuffer3, *surface;
6124 DDSCAPS2 caps = {DDSCAPS_FLIP, 0, 0, {0}};
6125 DDSURFACEDESC2 surface_desc;
6126 BOOL sysmem_primary;
6127 IDirectDraw4 *ddraw;
6128 DWORD expected_caps;
6129 unsigned int i;
6130 D3DCOLOR color;
6131 ULONG refcount;
6132 HWND window;
6133 DDBLTFX fx;
6134 HRESULT hr;
6136 static const struct
6138 const char *name;
6139 DWORD caps;
6141 test_data[] =
6143 {"PRIMARYSURFACE", DDSCAPS_PRIMARYSURFACE},
6144 {"OFFSCREENPLAIN", DDSCAPS_OFFSCREENPLAIN},
6145 {"TEXTURE", DDSCAPS_TEXTURE},
6148 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6149 0, 0, 640, 480, 0, 0, 0, 0);
6150 ddraw = create_ddraw();
6151 ok(!!ddraw, "Failed to create a ddraw object.\n");
6153 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6154 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6156 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
6158 memset(&surface_desc, 0, sizeof(surface_desc));
6159 surface_desc.dwSize = sizeof(surface_desc);
6160 surface_desc.dwFlags = DDSD_CAPS;
6161 if (!(test_data[i].caps & DDSCAPS_PRIMARYSURFACE))
6162 surface_desc.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
6163 surface_desc.ddsCaps.dwCaps = DDSCAPS_COMPLEX | DDSCAPS_FLIP | test_data[i].caps;
6164 surface_desc.dwWidth = 512;
6165 surface_desc.dwHeight = 512;
6166 U5(surface_desc).dwBackBufferCount = 3;
6167 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &frontbuffer, NULL);
6168 ok(hr == DDERR_INVALIDCAPS, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
6170 surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_FLIP;
6171 surface_desc.dwFlags |= DDSD_BACKBUFFERCOUNT;
6172 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &frontbuffer, NULL);
6173 ok(hr == DDERR_INVALIDCAPS, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
6175 surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_COMPLEX;
6176 surface_desc.ddsCaps.dwCaps |= DDSCAPS_FLIP;
6177 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &frontbuffer, NULL);
6178 ok(hr == DDERR_INVALIDCAPS, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
6180 surface_desc.ddsCaps.dwCaps |= DDSCAPS_COMPLEX;
6181 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &frontbuffer, NULL);
6182 todo_wine_if(test_data[i].caps & DDSCAPS_TEXTURE)
6183 ok(SUCCEEDED(hr), "%s: Failed to create surface, hr %#x.\n", test_data[i].name, hr);
6184 if (FAILED(hr))
6185 continue;
6187 memset(&surface_desc, 0, sizeof(surface_desc));
6188 surface_desc.dwSize = sizeof(surface_desc);
6189 hr = IDirectDrawSurface4_GetSurfaceDesc(frontbuffer, &surface_desc);
6190 ok(SUCCEEDED(hr), "%s: Failed to get surface desc, hr %#x.\n", test_data[i].name, hr);
6191 expected_caps = DDSCAPS_FRONTBUFFER | DDSCAPS_COMPLEX | DDSCAPS_FLIP | test_data[i].caps;
6192 if (test_data[i].caps & DDSCAPS_PRIMARYSURFACE)
6193 expected_caps |= DDSCAPS_VISIBLE;
6194 ok((surface_desc.ddsCaps.dwCaps & ~placement) == expected_caps,
6195 "%s: Got unexpected caps %#x.\n", test_data[i].name, surface_desc.ddsCaps.dwCaps);
6196 sysmem_primary = surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY;
6198 hr = IDirectDrawSurface4_GetAttachedSurface(frontbuffer, &caps, &backbuffer1);
6199 ok(SUCCEEDED(hr), "%s: Failed to get attached surface, hr %#x.\n", test_data[i].name, hr);
6200 memset(&surface_desc, 0, sizeof(surface_desc));
6201 surface_desc.dwSize = sizeof(surface_desc);
6202 hr = IDirectDrawSurface4_GetSurfaceDesc(backbuffer1, &surface_desc);
6203 ok(SUCCEEDED(hr), "%s: Failed to get surface desc, hr %#x.\n", test_data[i].name, hr);
6204 ok(!U5(surface_desc).dwBackBufferCount, "%s: Got unexpected back buffer count %u.\n",
6205 test_data[i].name, U5(surface_desc).dwBackBufferCount);
6206 expected_caps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER);
6207 expected_caps |= DDSCAPS_BACKBUFFER;
6208 ok((surface_desc.ddsCaps.dwCaps & ~placement) == expected_caps,
6209 "%s: Got unexpected caps %#x.\n", test_data[i].name, surface_desc.ddsCaps.dwCaps);
6211 hr = IDirectDrawSurface4_GetAttachedSurface(backbuffer1, &caps, &backbuffer2);
6212 ok(SUCCEEDED(hr), "%s: Failed to get attached surface, hr %#x.\n", test_data[i].name, hr);
6213 memset(&surface_desc, 0, sizeof(surface_desc));
6214 surface_desc.dwSize = sizeof(surface_desc);
6215 hr = IDirectDrawSurface4_GetSurfaceDesc(backbuffer2, &surface_desc);
6216 ok(SUCCEEDED(hr), "%s: Failed to get surface desc, hr %#x.\n", test_data[i].name, hr);
6217 ok(!U5(surface_desc).dwBackBufferCount, "%s: Got unexpected back buffer count %u.\n",
6218 test_data[i].name, U5(surface_desc).dwBackBufferCount);
6219 expected_caps &= ~DDSCAPS_BACKBUFFER;
6220 ok((surface_desc.ddsCaps.dwCaps & ~placement) == expected_caps,
6221 "%s: Got unexpected caps %#x.\n", test_data[i].name, surface_desc.ddsCaps.dwCaps);
6223 hr = IDirectDrawSurface4_GetAttachedSurface(backbuffer2, &caps, &backbuffer3);
6224 ok(SUCCEEDED(hr), "%s: Failed to get attached surface, hr %#x.\n", test_data[i].name, hr);
6225 memset(&surface_desc, 0, sizeof(surface_desc));
6226 surface_desc.dwSize = sizeof(surface_desc);
6227 hr = IDirectDrawSurface4_GetSurfaceDesc(backbuffer3, &surface_desc);
6228 ok(SUCCEEDED(hr), "%s: Failed to get surface desc, hr %#x.\n", test_data[i].name, hr);
6229 ok(!U5(surface_desc).dwBackBufferCount, "%s: Got unexpected back buffer count %u.\n",
6230 test_data[i].name, U5(surface_desc).dwBackBufferCount);
6231 ok((surface_desc.ddsCaps.dwCaps & ~placement) == expected_caps,
6232 "%s: Got unexpected caps %#x.\n", test_data[i].name, surface_desc.ddsCaps.dwCaps);
6234 hr = IDirectDrawSurface4_GetAttachedSurface(backbuffer3, &caps, &surface);
6235 ok(SUCCEEDED(hr), "%s: Failed to get attached surface, hr %#x.\n", test_data[i].name, hr);
6236 ok(surface == frontbuffer, "%s: Got unexpected surface %p, expected %p.\n",
6237 test_data[i].name, surface, frontbuffer);
6238 IDirectDrawSurface4_Release(surface);
6240 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
6241 ok(SUCCEEDED(hr), "%s: Failed to set cooperative level, hr %#x.\n", test_data[i].name, hr);
6242 hr = IDirectDrawSurface4_IsLost(frontbuffer);
6243 ok(hr == DD_OK, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
6244 hr = IDirectDrawSurface4_Flip(frontbuffer, NULL, DDFLIP_WAIT);
6245 if (test_data[i].caps & DDSCAPS_PRIMARYSURFACE)
6246 ok(hr == DDERR_NOEXCLUSIVEMODE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
6247 else
6248 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
6249 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6250 ok(SUCCEEDED(hr), "%s: Failed to set cooperative level, hr %#x.\n", test_data[i].name, hr);
6251 hr = IDirectDrawSurface4_IsLost(frontbuffer);
6252 todo_wine ok(hr == DDERR_SURFACELOST, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
6253 hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
6254 ok(SUCCEEDED(hr), "%s: Failed to restore surfaces, hr %#x.\n", test_data[i].name, hr);
6256 memset(&surface_desc, 0, sizeof(surface_desc));
6257 surface_desc.dwSize = sizeof(surface_desc);
6258 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6259 surface_desc.ddsCaps.dwCaps = 0;
6260 surface_desc.dwWidth = 640;
6261 surface_desc.dwHeight = 480;
6262 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6263 ok(SUCCEEDED(hr), "%s: Failed to create surface, hr %#x.\n", test_data[i].name, hr);
6264 hr = IDirectDrawSurface4_Flip(frontbuffer, surface, DDFLIP_WAIT);
6265 ok(hr == DDERR_NOTFLIPPABLE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
6266 IDirectDrawSurface4_Release(surface);
6268 hr = IDirectDrawSurface4_Flip(frontbuffer, frontbuffer, DDFLIP_WAIT);
6269 ok(hr == DDERR_NOTFLIPPABLE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
6270 hr = IDirectDrawSurface4_Flip(backbuffer1, NULL, DDFLIP_WAIT);
6271 ok(hr == DDERR_NOTFLIPPABLE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
6272 hr = IDirectDrawSurface4_Flip(backbuffer2, NULL, DDFLIP_WAIT);
6273 ok(hr == DDERR_NOTFLIPPABLE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
6274 hr = IDirectDrawSurface4_Flip(backbuffer3, NULL, DDFLIP_WAIT);
6275 ok(hr == DDERR_NOTFLIPPABLE, "%s: Got unexpected hr %#x.\n", test_data[i].name, hr);
6277 memset(&fx, 0, sizeof(fx));
6278 fx.dwSize = sizeof(fx);
6279 U5(fx).dwFillColor = 0xffff0000;
6280 hr = IDirectDrawSurface4_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6281 ok(SUCCEEDED(hr), "%s: Failed to fill surface, hr %#x.\n", test_data[i].name, hr);
6282 U5(fx).dwFillColor = 0xff00ff00;
6283 hr = IDirectDrawSurface4_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6284 ok(SUCCEEDED(hr), "%s: Failed to fill surface, hr %#x.\n", test_data[i].name, hr);
6285 U5(fx).dwFillColor = 0xff0000ff;
6286 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6287 ok(SUCCEEDED(hr), "%s: Failed to fill surface, hr %#x.\n", test_data[i].name, hr);
6289 hr = IDirectDrawSurface4_Flip(frontbuffer, NULL, DDFLIP_WAIT);
6290 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
6291 color = get_surface_color(backbuffer1, 320, 240);
6292 /* The testbot seems to just copy the contents of one surface to all the
6293 * others, instead of properly flipping. */
6294 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
6295 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6296 color = get_surface_color(backbuffer2, 320, 240);
6297 ok(compare_color(color, 0x000000ff, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6298 U5(fx).dwFillColor = 0xffff0000;
6299 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6300 ok(SUCCEEDED(hr), "%s: Failed to fill surface, hr %#x.\n", test_data[i].name, hr);
6302 hr = IDirectDrawSurface4_Flip(frontbuffer, NULL, DDFLIP_WAIT);
6303 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
6304 color = get_surface_color(backbuffer1, 320, 240);
6305 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
6306 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6307 color = get_surface_color(backbuffer2, 320, 240);
6308 ok(compare_color(color, 0x00ff0000, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6309 U5(fx).dwFillColor = 0xff00ff00;
6310 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6311 ok(SUCCEEDED(hr), "%s: Failed to fill surface, hr %#x.\n", test_data[i].name, hr);
6313 hr = IDirectDrawSurface4_Flip(frontbuffer, NULL, DDFLIP_WAIT);
6314 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
6315 color = get_surface_color(backbuffer1, 320, 240);
6316 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
6317 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6318 color = get_surface_color(backbuffer2, 320, 240);
6319 ok(compare_color(color, 0x0000ff00, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6320 U5(fx).dwFillColor = 0xff0000ff;
6321 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6322 ok(SUCCEEDED(hr), "%s: Failed to fill surface, hr %#x.\n", test_data[i].name, hr);
6324 hr = IDirectDrawSurface4_Flip(frontbuffer, backbuffer1, DDFLIP_WAIT);
6325 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
6326 color = get_surface_color(backbuffer2, 320, 240);
6327 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
6328 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6329 color = get_surface_color(backbuffer3, 320, 240);
6330 ok(compare_color(color, 0x000000ff, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6331 U5(fx).dwFillColor = 0xffff0000;
6332 hr = IDirectDrawSurface4_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6333 ok(SUCCEEDED(hr), "%s: Failed to fill surface, hr %#x.\n", test_data[i].name, hr);
6335 hr = IDirectDrawSurface4_Flip(frontbuffer, backbuffer2, DDFLIP_WAIT);
6336 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
6337 color = get_surface_color(backbuffer1, 320, 240);
6338 ok(compare_color(color, 0x00ff0000, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6339 color = get_surface_color(backbuffer3, 320, 240);
6340 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
6341 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6342 U5(fx).dwFillColor = 0xff00ff00;
6343 hr = IDirectDrawSurface4_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6344 ok(SUCCEEDED(hr), "%s: Failed to fill surface, hr %#x.\n", test_data[i].name, hr);
6346 hr = IDirectDrawSurface4_Flip(frontbuffer, backbuffer3, DDFLIP_WAIT);
6347 ok(SUCCEEDED(hr), "%s: Failed to flip, hr %#x.\n", test_data[i].name, hr);
6348 color = get_surface_color(backbuffer1, 320, 240);
6349 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
6350 "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6351 color = get_surface_color(backbuffer2, 320, 240);
6352 ok(compare_color(color, 0x0000ff00, 1), "%s: Got unexpected color 0x%08x.\n", test_data[i].name, color);
6354 IDirectDrawSurface4_Release(backbuffer3);
6355 IDirectDrawSurface4_Release(backbuffer2);
6356 IDirectDrawSurface4_Release(backbuffer1);
6357 IDirectDrawSurface4_Release(frontbuffer);
6360 refcount = IDirectDraw4_Release(ddraw);
6361 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
6362 DestroyWindow(window);
6365 static void reset_ddsd(DDSURFACEDESC2 *ddsd)
6367 memset(ddsd, 0, sizeof(*ddsd));
6368 ddsd->dwSize = sizeof(*ddsd);
6371 static void test_set_surface_desc(void)
6373 IDirectDraw4 *ddraw;
6374 HWND window;
6375 HRESULT hr;
6376 DDSURFACEDESC2 ddsd;
6377 IDirectDrawSurface4 *surface;
6378 BYTE data[16*16*4];
6379 ULONG ref;
6380 unsigned int i;
6381 static const struct
6383 DWORD caps, caps2;
6384 BOOL supported;
6385 const char *name;
6387 invalid_caps_tests[] =
6389 {DDSCAPS_VIDEOMEMORY, 0, FALSE, "videomemory plain"},
6390 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0, TRUE, "systemmemory texture"},
6391 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, FALSE, "managed texture"},
6392 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, FALSE, "managed texture"},
6393 {DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY, 0, FALSE, "systemmemory primary"},
6396 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6397 0, 0, 640, 480, 0, 0, 0, 0);
6398 ddraw = create_ddraw();
6399 ok(!!ddraw, "Failed to create a ddraw object.\n");
6400 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6401 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6403 reset_ddsd(&ddsd);
6404 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
6405 ddsd.dwWidth = 8;
6406 ddsd.dwHeight = 8;
6407 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6408 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6409 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6410 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6411 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6412 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6413 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
6415 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6416 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6418 reset_ddsd(&ddsd);
6419 ddsd.dwFlags = DDSD_LPSURFACE;
6420 ddsd.lpSurface = data;
6421 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6422 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6424 /* Redundantly setting the same lpSurface is not an error. */
6425 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6426 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6427 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
6428 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6429 ok(!(ddsd.dwFlags & DDSD_LPSURFACE), "DDSD_LPSURFACE is set.\n");
6430 ok(ddsd.lpSurface == NULL, "lpSurface is %p, expected NULL.\n", ddsd.lpSurface);
6432 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, 0, NULL);
6433 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
6434 ok(!(ddsd.dwFlags & DDSD_LPSURFACE), "DDSD_LPSURFACE is set.\n");
6435 ok(ddsd.lpSurface == data, "lpSurface is %p, expected %p.\n", data, data);
6436 hr = IDirectDrawSurface4_Unlock(surface, NULL);
6437 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
6439 reset_ddsd(&ddsd);
6440 ddsd.dwFlags = DDSD_LPSURFACE;
6441 ddsd.lpSurface = data;
6442 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 1);
6443 ok(hr == DDERR_INVALIDPARAMS, "SetSurfaceDesc with flags=1 returned %#x.\n", hr);
6445 ddsd.lpSurface = NULL;
6446 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6447 ok(hr == DDERR_INVALIDPARAMS, "Setting lpSurface=NULL returned %#x.\n", hr);
6449 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, NULL, 0);
6450 ok(hr == DDERR_INVALIDPARAMS, "SetSurfaceDesc with NULL desc returned %#x.\n", hr);
6452 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
6453 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6454 ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN),
6455 "Got unexpected caps %#x.\n", ddsd.ddsCaps.dwCaps);
6456 ok(ddsd.ddsCaps.dwCaps2 == 0, "Got unexpected caps2 %#x.\n", 0);
6458 /* Setting the caps is an error. This also means the original description cannot be reapplied. */
6459 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6460 ok(hr == DDERR_INVALIDPARAMS, "Setting the original desc returned %#x.\n", hr);
6462 ddsd.dwFlags = DDSD_CAPS;
6463 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6464 ok(hr == DDERR_INVALIDPARAMS, "Setting DDSD_CAPS returned %#x.\n", hr);
6466 /* dwCaps = 0 is allowed, but ignored. Caps2 can be anything and is ignored too. */
6467 ddsd.dwFlags = DDSD_CAPS | DDSD_LPSURFACE;
6468 ddsd.lpSurface = data;
6469 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6470 ok(hr == DDERR_INVALIDCAPS, "Setting DDSD_CAPS returned %#x.\n", hr);
6471 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
6472 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6473 ok(hr == DDERR_INVALIDCAPS, "Setting DDSD_CAPS returned %#x.\n", hr);
6474 ddsd.ddsCaps.dwCaps = 0;
6475 ddsd.ddsCaps.dwCaps2 = 0xdeadbeef;
6476 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6477 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6479 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
6480 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6481 ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN),
6482 "Got unexpected caps %#x.\n", ddsd.ddsCaps.dwCaps);
6483 ok(ddsd.ddsCaps.dwCaps2 == 0, "Got unexpected caps2 %#x.\n", 0);
6485 /* Setting the height is allowed, but it cannot be set to 0, and only if LPSURFACE is set too. */
6486 reset_ddsd(&ddsd);
6487 ddsd.dwFlags = DDSD_HEIGHT;
6488 ddsd.dwHeight = 16;
6489 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6490 ok(hr == DDERR_INVALIDPARAMS, "Setting height without lpSurface returned %#x.\n", hr);
6492 ddsd.lpSurface = data;
6493 ddsd.dwFlags = DDSD_HEIGHT | DDSD_LPSURFACE;
6494 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6495 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6497 ddsd.dwHeight = 0;
6498 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6499 ok(hr == DDERR_INVALIDPARAMS, "Setting height=0 returned %#x.\n", hr);
6501 reset_ddsd(&ddsd);
6502 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
6503 ok(SUCCEEDED(hr), "GetSurfaceDesc failed, hr %#x.\n", hr);
6504 ok(ddsd.dwWidth == 8, "SetSurfaceDesc: Expected width 8, got %u.\n", ddsd.dwWidth);
6505 ok(ddsd.dwHeight == 16, "SetSurfaceDesc: Expected height 16, got %u.\n", ddsd.dwHeight);
6507 /* Pitch and width can be set, but only together, and only with LPSURFACE. They must not be 0 */
6508 reset_ddsd(&ddsd);
6509 ddsd.dwFlags = DDSD_PITCH;
6510 U1(ddsd).lPitch = 8 * 4;
6511 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6512 ok(hr == DDERR_INVALIDPARAMS, "Setting pitch without lpSurface or width returned %#x.\n", hr);
6514 ddsd.dwFlags = DDSD_WIDTH;
6515 ddsd.dwWidth = 16;
6516 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6517 ok(hr == DDERR_INVALIDPARAMS, "Setting width without lpSurface or pitch returned %#x.\n", hr);
6519 ddsd.dwFlags = DDSD_PITCH | DDSD_LPSURFACE;
6520 ddsd.lpSurface = data;
6521 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6522 ok(hr == DDERR_INVALIDPARAMS, "Setting pitch and lpSurface without width returned %#x.\n", hr);
6524 ddsd.dwFlags = DDSD_WIDTH | DDSD_LPSURFACE;
6525 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6526 ok(hr == DDERR_INVALIDPARAMS, "Setting width and lpSurface without pitch returned %#x.\n", hr);
6528 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
6529 U1(ddsd).lPitch = 16 * 4;
6530 ddsd.dwWidth = 16;
6531 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6532 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6534 reset_ddsd(&ddsd);
6535 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
6536 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6537 ok(ddsd.dwWidth == 16, "SetSurfaceDesc: Expected width 8, got %u.\n", ddsd.dwWidth);
6538 ok(ddsd.dwHeight == 16, "SetSurfaceDesc: Expected height 16, got %u.\n", ddsd.dwHeight);
6539 ok(U1(ddsd).lPitch == 16 * 4, "SetSurfaceDesc: Expected pitch 64, got %u.\n", U1(ddsd).lPitch);
6541 /* The pitch must be 32 bit aligned and > 0, but is not verified for sanity otherwise.
6543 * VMware rejects those calls, but all real drivers accept it. Mark the VMware behavior broken. */
6544 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
6545 U1(ddsd).lPitch = 4 * 4;
6546 ddsd.lpSurface = data;
6547 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6548 ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDPARAMS), "Failed to set surface desc, hr %#x.\n", hr);
6550 U1(ddsd).lPitch = 4;
6551 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6552 ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDPARAMS), "Failed to set surface desc, hr %#x.\n", hr);
6554 U1(ddsd).lPitch = 16 * 4 + 1;
6555 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6556 ok(hr == DDERR_INVALIDPARAMS, "Setting misaligned pitch returned %#x.\n", hr);
6558 U1(ddsd).lPitch = 16 * 4 + 3;
6559 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6560 ok(hr == DDERR_INVALIDPARAMS, "Setting misaligned pitch returned %#x.\n", hr);
6562 U1(ddsd).lPitch = -4;
6563 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6564 ok(hr == DDERR_INVALIDPARAMS, "Setting negative pitch returned %#x.\n", hr);
6566 U1(ddsd).lPitch = 16 * 4;
6567 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6568 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6570 reset_ddsd(&ddsd);
6571 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
6572 U1(ddsd).lPitch = 0;
6573 ddsd.dwWidth = 16;
6574 ddsd.lpSurface = data;
6575 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6576 ok(hr == DDERR_INVALIDPARAMS, "Setting zero pitch returned %#x.\n", hr);
6578 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
6579 U1(ddsd).lPitch = 16 * 4;
6580 ddsd.dwWidth = 0;
6581 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6582 ok(hr == DDERR_INVALIDPARAMS, "Setting zero width returned %#x.\n", hr);
6584 /* Setting the pixelformat without LPSURFACE is an error, but with LPSURFACE it works. */
6585 ddsd.dwFlags = DDSD_PIXELFORMAT;
6586 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6587 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6588 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6589 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6590 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6591 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6592 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6593 ok(hr == DDERR_INVALIDPARAMS, "Setting the pixel format returned %#x.\n", hr);
6595 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_LPSURFACE;
6596 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6597 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6599 /* Can't set color keys. */
6600 reset_ddsd(&ddsd);
6601 ddsd.dwFlags = DDSD_CKSRCBLT;
6602 ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00ff0000;
6603 ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00ff0000;
6604 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6605 ok(hr == DDERR_INVALIDPARAMS, "Setting ddckCKSrcBlt returned %#x.\n", hr);
6607 ddsd.dwFlags = DDSD_CKSRCBLT | DDSD_LPSURFACE;
6608 ddsd.lpSurface = data;
6609 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6610 ok(hr == DDERR_INVALIDPARAMS, "Setting ddckCKSrcBlt returned %#x.\n", hr);
6612 IDirectDrawSurface4_Release(surface);
6614 /* SetSurfaceDesc needs systemmemory surfaces.
6616 * As a sidenote, fourcc surfaces aren't allowed in sysmem, thus testing DDSD_LINEARSIZE is moot. */
6617 for (i = 0; i < sizeof(invalid_caps_tests) / sizeof(*invalid_caps_tests); i++)
6619 reset_ddsd(&ddsd);
6620 ddsd.dwFlags = DDSD_CAPS;
6621 ddsd.ddsCaps.dwCaps = invalid_caps_tests[i].caps;
6622 ddsd.ddsCaps.dwCaps2 = invalid_caps_tests[i].caps2;
6623 if (!(invalid_caps_tests[i].caps & DDSCAPS_PRIMARYSURFACE))
6625 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6626 ddsd.dwWidth = 8;
6627 ddsd.dwHeight = 8;
6628 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6629 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6630 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6631 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6632 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6633 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6636 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6637 ok(SUCCEEDED(hr) || hr == DDERR_NODIRECTDRAWHW, "Failed to create surface, hr %#x.\n", hr);
6638 if (FAILED(hr))
6640 skip("Cannot create a %s surface, skipping vidmem SetSurfaceDesc test.\n",
6641 invalid_caps_tests[i].name);
6642 goto done;
6645 reset_ddsd(&ddsd);
6646 ddsd.dwFlags = DDSD_LPSURFACE;
6647 ddsd.lpSurface = data;
6648 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6649 if (invalid_caps_tests[i].supported)
6651 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6653 else
6655 ok(hr == DDERR_INVALIDSURFACETYPE, "SetSurfaceDesc on a %s surface returned %#x.\n",
6656 invalid_caps_tests[i].name, hr);
6658 /* Check priority of error conditions. */
6659 ddsd.dwFlags = DDSD_WIDTH;
6660 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6661 ok(hr == DDERR_INVALIDSURFACETYPE, "SetSurfaceDesc on a %s surface returned %#x.\n",
6662 invalid_caps_tests[i].name, hr);
6665 IDirectDrawSurface4_Release(surface);
6668 done:
6669 ref = IDirectDraw4_Release(ddraw);
6670 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6671 DestroyWindow(window);
6674 static void test_user_memory_getdc(void)
6676 IDirectDraw4 *ddraw;
6677 HWND window;
6678 HRESULT hr;
6679 DDSURFACEDESC2 ddsd;
6680 IDirectDrawSurface4 *surface;
6681 DWORD data[16][16];
6682 HBITMAP bitmap;
6683 DIBSECTION dib;
6684 ULONG ref;
6685 int size;
6686 HDC dc;
6687 unsigned int x, y;
6689 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6690 0, 0, 640, 480, 0, 0, 0, 0);
6691 ddraw = create_ddraw();
6692 ok(!!ddraw, "Failed to create a ddraw object.\n");
6694 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6695 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6697 reset_ddsd(&ddsd);
6698 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
6699 ddsd.dwWidth = 16;
6700 ddsd.dwHeight = 16;
6701 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6702 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6703 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6704 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6705 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6706 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6707 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
6708 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6709 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6711 memset(data, 0xaa, sizeof(data));
6712 reset_ddsd(&ddsd);
6713 ddsd.dwFlags = DDSD_LPSURFACE;
6714 ddsd.lpSurface = data;
6715 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6716 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6718 hr = IDirectDrawSurface4_GetDC(surface, &dc);
6719 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6720 bitmap = GetCurrentObject(dc, OBJ_BITMAP);
6721 ok(!!bitmap, "Failed to get bitmap.\n");
6722 size = GetObjectA(bitmap, sizeof(dib), &dib);
6723 ok(size == sizeof(dib), "Got unexpected size %d.\n", size);
6724 ok(dib.dsBm.bmBits == data, "Got unexpected bits %p, expected %p.\n", dib.dsBm.bmBits, data);
6725 BitBlt(dc, 0, 0, 16, 8, NULL, 0, 0, WHITENESS);
6726 BitBlt(dc, 0, 8, 16, 8, NULL, 0, 0, BLACKNESS);
6727 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
6728 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6730 ok(data[0][0] == 0xffffffff, "Expected color 0xffffffff, got %#x.\n", data[0][0]);
6731 ok(data[15][15] == 0x00000000, "Expected color 0x00000000, got %#x.\n", data[15][15]);
6733 ddsd.dwFlags = DDSD_LPSURFACE | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PITCH;
6734 ddsd.lpSurface = data;
6735 ddsd.dwWidth = 4;
6736 ddsd.dwHeight = 8;
6737 U1(ddsd).lPitch = sizeof(*data);
6738 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6739 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6741 memset(data, 0xaa, sizeof(data));
6742 hr = IDirectDrawSurface4_GetDC(surface, &dc);
6743 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6744 BitBlt(dc, 0, 0, 4, 8, NULL, 0, 0, BLACKNESS);
6745 BitBlt(dc, 1, 1, 2, 2, NULL, 0, 0, WHITENESS);
6746 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
6747 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6749 for (y = 0; y < 4; y++)
6751 for (x = 0; x < 4; x++)
6753 if ((x == 1 || x == 2) && (y == 1 || y == 2))
6754 ok(data[y][x] == 0xffffffff, "Expected color 0xffffffff on position %ux%u, got %#x.\n",
6755 x, y, data[y][x]);
6756 else
6757 ok(data[y][x] == 0x00000000, "Expected color 0x00000000 on position %ux%u, got %#x.\n",
6758 x, y, data[y][x]);
6761 ok(data[0][5] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 5x0, got %#x.\n",
6762 data[0][5]);
6763 ok(data[7][3] == 0x00000000, "Expected color 0x00000000 on position 3x7, got %#x.\n",
6764 data[7][3]);
6765 ok(data[7][4] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 4x7, got %#x.\n",
6766 data[7][4]);
6767 ok(data[8][0] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 0x8, got %#x.\n",
6768 data[8][0]);
6770 IDirectDrawSurface4_Release(surface);
6771 ref = IDirectDraw4_Release(ddraw);
6772 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6773 DestroyWindow(window);
6776 static void test_sysmem_overlay(void)
6778 IDirectDraw4 *ddraw;
6779 HWND window;
6780 HRESULT hr;
6781 DDSURFACEDESC2 ddsd;
6782 IDirectDrawSurface4 *surface;
6783 ULONG ref;
6785 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6786 0, 0, 640, 480, 0, 0, 0, 0);
6787 ddraw = create_ddraw();
6788 ok(!!ddraw, "Failed to create a ddraw object.\n");
6790 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6791 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6793 reset_ddsd(&ddsd);
6794 ddsd.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
6795 ddsd.dwWidth = 16;
6796 ddsd.dwHeight = 16;
6797 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OVERLAY;
6798 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6799 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6800 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6801 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6802 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6803 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6804 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6805 ok(hr == DDERR_NOOVERLAYHW, "Got unexpected hr %#x.\n", hr);
6807 ref = IDirectDraw4_Release(ddraw);
6808 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6809 DestroyWindow(window);
6812 static void test_primary_palette(void)
6814 DDSCAPS2 surface_caps = {DDSCAPS_FLIP, 0, 0, {0}};
6815 IDirectDrawSurface4 *primary, *backbuffer;
6816 PALETTEENTRY palette_entries[256];
6817 IDirectDrawPalette *palette, *tmp;
6818 DDSURFACEDESC2 surface_desc;
6819 IDirectDraw4 *ddraw;
6820 DWORD palette_caps;
6821 ULONG refcount;
6822 HWND window;
6823 HRESULT hr;
6825 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6826 0, 0, 640, 480, 0, 0, 0, 0);
6827 ddraw = create_ddraw();
6828 ok(!!ddraw, "Failed to create a ddraw object.\n");
6829 if (FAILED(IDirectDraw4_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
6831 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
6832 IDirectDraw4_Release(ddraw);
6833 DestroyWindow(window);
6834 return;
6836 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6837 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6839 memset(&surface_desc, 0, sizeof(surface_desc));
6840 surface_desc.dwSize = sizeof(surface_desc);
6841 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
6842 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
6843 U5(surface_desc).dwBackBufferCount = 1;
6844 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &primary, NULL);
6845 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6846 hr = IDirectDrawSurface4_GetAttachedSurface(primary, &surface_caps, &backbuffer);
6847 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6849 memset(palette_entries, 0, sizeof(palette_entries));
6850 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256, palette_entries, &palette, NULL);
6851 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6852 refcount = get_refcount((IUnknown *)palette);
6853 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6855 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
6856 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
6857 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
6859 hr = IDirectDrawSurface4_SetPalette(primary, palette);
6860 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6862 /* The Windows 8 testbot attaches the palette to the backbuffer as well,
6863 * and is generally somewhat broken with respect to 8 bpp / palette
6864 * handling. */
6865 if (SUCCEEDED(IDirectDrawSurface4_GetPalette(backbuffer, &tmp)))
6867 win_skip("Broken palette handling detected, skipping tests.\n");
6868 IDirectDrawPalette_Release(tmp);
6869 IDirectDrawPalette_Release(palette);
6870 /* The Windows 8 testbot keeps extra references to the primary and
6871 * backbuffer while in 8 bpp mode. */
6872 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
6873 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
6874 goto done;
6877 refcount = get_refcount((IUnknown *)palette);
6878 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
6880 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
6881 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
6882 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE | DDPCAPS_ALLOW256),
6883 "Got unexpected palette caps %#x.\n", palette_caps);
6885 hr = IDirectDrawSurface4_SetPalette(primary, NULL);
6886 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6887 refcount = get_refcount((IUnknown *)palette);
6888 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6890 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
6891 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
6892 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
6894 hr = IDirectDrawSurface4_SetPalette(primary, palette);
6895 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6896 refcount = get_refcount((IUnknown *)palette);
6897 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
6899 hr = IDirectDrawSurface4_GetPalette(primary, &tmp);
6900 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
6901 ok(tmp == palette, "Got unexpected palette %p, expected %p.\n", tmp, palette);
6902 IDirectDrawPalette_Release(tmp);
6903 hr = IDirectDrawSurface4_GetPalette(backbuffer, &tmp);
6904 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
6906 refcount = IDirectDrawPalette_Release(palette);
6907 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6908 refcount = IDirectDrawPalette_Release(palette);
6909 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6911 /* Note that this only seems to work when the palette is attached to the
6912 * primary surface. When attached to a regular surface, attempting to get
6913 * the palette here will cause an access violation. */
6914 hr = IDirectDrawSurface4_GetPalette(primary, &tmp);
6915 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
6917 done:
6918 refcount = IDirectDrawSurface4_Release(backbuffer);
6919 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6920 refcount = IDirectDrawSurface4_Release(primary);
6921 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6922 refcount = IDirectDraw4_Release(ddraw);
6923 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6924 DestroyWindow(window);
6927 static HRESULT WINAPI surface_counter(IDirectDrawSurface4 *surface, DDSURFACEDESC2 *desc, void *context)
6929 UINT *surface_count = context;
6931 ++(*surface_count);
6932 IDirectDrawSurface_Release(surface);
6934 return DDENUMRET_OK;
6937 static void test_surface_attachment(void)
6939 IDirectDrawSurface4 *surface1, *surface2, *surface3, *surface4;
6940 IDirectDrawSurface *surface1v1, *surface2v1;
6941 DDSCAPS2 caps = {DDSCAPS_TEXTURE, 0, 0, {0}};
6942 DDSURFACEDESC2 surface_desc;
6943 IDirectDraw4 *ddraw;
6944 UINT surface_count;
6945 ULONG refcount;
6946 HWND window;
6947 HRESULT hr;
6949 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6950 0, 0, 640, 480, 0, 0, 0, 0);
6951 ddraw = create_ddraw();
6952 ok(!!ddraw, "Failed to create a ddraw object.\n");
6953 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6954 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6956 memset(&surface_desc, 0, sizeof(surface_desc));
6957 surface_desc.dwSize = sizeof(surface_desc);
6958 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
6959 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
6960 U2(surface_desc).dwMipMapCount = 3;
6961 surface_desc.dwWidth = 128;
6962 surface_desc.dwHeight = 128;
6963 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
6964 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6966 hr = IDirectDrawSurface4_GetAttachedSurface(surface1, &caps, &surface2);
6967 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
6968 hr = IDirectDrawSurface4_GetAttachedSurface(surface2, &caps, &surface3);
6969 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
6970 hr = IDirectDrawSurface4_GetAttachedSurface(surface3, &caps, &surface4);
6971 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
6973 surface_count = 0;
6974 IDirectDrawSurface4_EnumAttachedSurfaces(surface1, &surface_count, surface_counter);
6975 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
6976 surface_count = 0;
6977 IDirectDrawSurface4_EnumAttachedSurfaces(surface2, &surface_count, surface_counter);
6978 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
6979 surface_count = 0;
6980 IDirectDrawSurface4_EnumAttachedSurfaces(surface3, &surface_count, surface_counter);
6981 ok(!surface_count, "Got unexpected surface_count %u.\n", surface_count);
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_TEXTURE;
6987 surface_desc.dwWidth = 16;
6988 surface_desc.dwHeight = 16;
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, surface4);
6993 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6994 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface1);
6995 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6996 hr = IDirectDrawSurface4_AddAttachedSurface(surface3, surface4);
6997 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6998 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface3);
6999 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7000 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface4);
7001 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7002 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface2);
7003 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7005 IDirectDrawSurface4_Release(surface4);
7007 memset(&surface_desc, 0, sizeof(surface_desc));
7008 surface_desc.dwSize = sizeof(surface_desc);
7009 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
7010 surface_desc.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
7011 surface_desc.dwWidth = 16;
7012 surface_desc.dwHeight = 16;
7013 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
7014 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7016 if (SUCCEEDED(hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface4)))
7018 skip("Running on refrast, skipping some tests.\n");
7019 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface4);
7020 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7022 else
7024 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7025 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface1);
7026 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7027 hr = IDirectDrawSurface4_AddAttachedSurface(surface3, surface4);
7028 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7029 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface3);
7030 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7031 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface4);
7032 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7033 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface2);
7034 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7037 IDirectDrawSurface4_Release(surface4);
7038 IDirectDrawSurface4_Release(surface3);
7039 IDirectDrawSurface4_Release(surface2);
7040 IDirectDrawSurface4_Release(surface1);
7042 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
7043 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7045 /* Try a single primary and two offscreen plain surfaces. */
7046 memset(&surface_desc, 0, sizeof(surface_desc));
7047 surface_desc.dwSize = sizeof(surface_desc);
7048 surface_desc.dwFlags = DDSD_CAPS;
7049 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
7050 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
7051 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7053 memset(&surface_desc, 0, sizeof(surface_desc));
7054 surface_desc.dwSize = sizeof(surface_desc);
7055 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
7056 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7057 surface_desc.dwWidth = registry_mode.dmPelsWidth;
7058 surface_desc.dwHeight = registry_mode.dmPelsHeight;
7059 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
7060 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7062 memset(&surface_desc, 0, sizeof(surface_desc));
7063 surface_desc.dwSize = sizeof(surface_desc);
7064 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
7065 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7066 surface_desc.dwWidth = registry_mode.dmPelsWidth;
7067 surface_desc.dwHeight = registry_mode.dmPelsHeight;
7068 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
7069 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7071 /* This one has a different size. */
7072 memset(&surface_desc, 0, sizeof(surface_desc));
7073 surface_desc.dwSize = sizeof(surface_desc);
7074 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
7075 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7076 surface_desc.dwWidth = 128;
7077 surface_desc.dwHeight = 128;
7078 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
7079 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7081 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface2);
7082 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7083 /* Try the reverse without detaching first. */
7084 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface1);
7085 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
7086 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
7087 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7089 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface1);
7090 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7091 /* Try to detach reversed. */
7092 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
7093 ok(hr == DDERR_CANNOTDETACHSURFACE, "Got unexpected hr %#x.\n", hr);
7094 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface2, 0, surface1);
7095 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7097 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface3);
7098 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7099 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface2, 0, surface3);
7100 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7102 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface4);
7103 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7104 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface1);
7105 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7107 IDirectDrawSurface4_Release(surface4);
7108 IDirectDrawSurface4_Release(surface3);
7109 IDirectDrawSurface4_Release(surface2);
7110 IDirectDrawSurface4_Release(surface1);
7112 /* Test DeleteAttachedSurface() and automatic detachment of attached surfaces on release. */
7113 memset(&surface_desc, 0, sizeof(surface_desc));
7114 surface_desc.dwSize = sizeof(surface_desc);
7115 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7116 surface_desc.dwWidth = 64;
7117 surface_desc.dwHeight = 64;
7118 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
7119 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7120 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB; /* D3DFMT_R5G6B5 */
7121 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 16;
7122 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0xf800;
7123 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x07e0;
7124 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x001f;
7125 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
7126 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7127 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
7128 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7130 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
7131 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
7132 U1(U4(surface_desc).ddpfPixelFormat).dwZBufferBitDepth = 16;
7133 U3(U4(surface_desc).ddpfPixelFormat).dwZBitMask = 0x0000ffff;
7134 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
7135 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7137 hr = IDirectDrawSurface4_QueryInterface(surface1, &IID_IDirectDrawSurface, (void **)&surface1v1);
7138 ok(SUCCEEDED(hr), "Failed to get interface, hr %#x.\n", hr);
7139 hr = IDirectDrawSurface4_QueryInterface(surface2, &IID_IDirectDrawSurface, (void **)&surface2v1);
7140 ok(SUCCEEDED(hr), "Failed to get interface, hr %#x.\n", hr);
7142 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface2);
7143 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7144 refcount = get_refcount((IUnknown *)surface2);
7145 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
7146 refcount = get_refcount((IUnknown *)surface2v1);
7147 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
7148 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface2);
7149 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
7150 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
7151 todo_wine ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
7152 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1v1, 0, surface2v1);
7153 ok(hr == DDERR_SURFACENOTATTACHED, "Got unexpected hr %#x.\n", hr);
7155 /* Attaching while already attached to other surface. */
7156 hr = IDirectDrawSurface4_AddAttachedSurface(surface3, surface2);
7157 todo_wine ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7158 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface3, 0, surface2);
7159 todo_wine ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7160 IDirectDrawSurface4_Release(surface3);
7162 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
7163 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7164 refcount = get_refcount((IUnknown *)surface2);
7165 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
7166 refcount = get_refcount((IUnknown *)surface2v1);
7167 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
7169 /* DeleteAttachedSurface() when attaching via IDirectDrawSurface. */
7170 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
7171 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7172 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
7173 ok(hr == DDERR_SURFACENOTATTACHED, "Got unexpected hr %#x.\n", hr);
7174 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1v1, 0, surface2v1);
7175 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
7176 refcount = IDirectDrawSurface4_Release(surface2);
7177 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7178 refcount = IDirectDrawSurface4_Release(surface1);
7179 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7181 /* Automatic detachment on release. */
7182 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
7183 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
7184 refcount = get_refcount((IUnknown *)surface2v1);
7185 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
7186 refcount = IDirectDrawSurface_Release(surface1v1);
7187 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7188 refcount = IDirectDrawSurface_Release(surface2v1);
7189 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7190 refcount = IDirectDraw4_Release(ddraw);
7191 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7192 DestroyWindow(window);
7195 static void test_private_data(void)
7197 IDirectDraw4 *ddraw;
7198 IDirectDrawSurface4 *surface, *surface2;
7199 DDSURFACEDESC2 surface_desc;
7200 ULONG refcount, refcount2, refcount3;
7201 IUnknown *ptr;
7202 DWORD size = sizeof(ptr);
7203 HRESULT hr;
7204 HWND window;
7205 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
7206 DWORD data[] = {1, 2, 3, 4};
7207 DDCAPS hal_caps;
7208 static const GUID ddraw_private_data_test_guid =
7210 0xfdb37466,
7211 0x428f,
7212 0x4edf,
7213 {0xa3,0x7f,0x9b,0x1d,0xf4,0x88,0xc5,0xfc}
7215 static const GUID ddraw_private_data_test_guid2 =
7217 0x2e5afac2,
7218 0x87b5,
7219 0x4c10,
7220 {0x9b,0x4b,0x89,0xd7,0xd1,0x12,0xe7,0x2b}
7223 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7224 0, 0, 640, 480, 0, 0, 0, 0);
7225 ddraw = create_ddraw();
7226 ok(!!ddraw, "Failed to create a ddraw object.\n");
7227 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7228 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7230 reset_ddsd(&surface_desc);
7231 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
7232 surface_desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
7233 surface_desc.dwHeight = 4;
7234 surface_desc.dwWidth = 4;
7235 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7236 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7238 /* NULL pointers are not valid, but don't cause a crash. */
7239 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL,
7240 sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
7241 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7242 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL, 0, 0);
7243 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7244 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL, 1, 0);
7245 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7247 /* DDSPD_IUNKNOWNPOINTER needs sizeof(IUnknown *) bytes of data. */
7248 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7249 0, DDSPD_IUNKNOWNPOINTER);
7250 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7251 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7252 5, DDSPD_IUNKNOWNPOINTER);
7253 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7254 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7255 sizeof(ddraw) * 2, DDSPD_IUNKNOWNPOINTER);
7256 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7258 /* Note that with a size != 0 and size != sizeof(IUnknown *) and
7259 * DDSPD_IUNKNOWNPOINTER set SetPrivateData in ddraw4 and ddraw7
7260 * erases the old content and returns an error. This behavior has
7261 * been fixed in d3d8 and d3d9. Unless an application is found
7262 * that depends on this we don't care about this behavior. */
7263 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7264 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
7265 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7266 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7267 0, DDSPD_IUNKNOWNPOINTER);
7268 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7269 size = sizeof(ptr);
7270 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
7271 ok(SUCCEEDED(hr), "Failed to get private data, hr %#x.\n", hr);
7272 hr = IDirectDrawSurface4_FreePrivateData(surface, &ddraw_private_data_test_guid);
7273 ok(SUCCEEDED(hr), "Failed to free private data, hr %#x.\n", hr);
7275 refcount = get_refcount((IUnknown *)ddraw);
7276 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7277 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
7278 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7279 refcount2 = get_refcount((IUnknown *)ddraw);
7280 ok(refcount2 == refcount + 1, "Got unexpected refcount %u.\n", refcount2);
7282 hr = IDirectDrawSurface4_FreePrivateData(surface, &ddraw_private_data_test_guid);
7283 ok(SUCCEEDED(hr), "Failed to free private data, hr %#x.\n", hr);
7284 refcount2 = get_refcount((IUnknown *)ddraw);
7285 ok(refcount2 == refcount, "Got unexpected refcount %u.\n", refcount2);
7287 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7288 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
7289 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7290 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, surface,
7291 sizeof(surface), DDSPD_IUNKNOWNPOINTER);
7292 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7293 refcount2 = get_refcount((IUnknown *)ddraw);
7294 ok(refcount2 == refcount, "Got unexpected refcount %u.\n", refcount2);
7296 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
7297 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
7298 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7299 size = 2 * sizeof(ptr);
7300 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
7301 ok(SUCCEEDED(hr), "Failed to get private data, hr %#x.\n", hr);
7302 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
7303 refcount2 = get_refcount(ptr);
7304 /* Object is NOT addref'ed by the getter. */
7305 ok(ptr == (IUnknown *)ddraw, "Returned interface pointer is %p, expected %p.\n", ptr, ddraw);
7306 ok(refcount2 == refcount + 1, "Got unexpected refcount %u.\n", refcount2);
7308 ptr = (IUnknown *)0xdeadbeef;
7309 size = 1;
7310 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, &size);
7311 ok(hr == DDERR_MOREDATA, "Got unexpected hr %#x.\n", hr);
7312 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
7313 size = 2 * sizeof(ptr);
7314 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, &size);
7315 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7316 ok(size == 2 * sizeof(ptr), "Got unexpected size %u.\n", size);
7317 size = 1;
7318 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
7319 ok(hr == DDERR_MOREDATA, "Got unexpected hr %#x.\n", hr);
7320 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
7321 ok(ptr == (IUnknown *)0xdeadbeef, "Got unexpected pointer %p.\n", ptr);
7322 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid2, NULL, NULL);
7323 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
7324 size = 0xdeadbabe;
7325 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid2, &ptr, &size);
7326 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
7327 ok(ptr == (IUnknown *)0xdeadbeef, "Got unexpected pointer %p.\n", ptr);
7328 ok(size == 0xdeadbabe, "Got unexpected size %u.\n", size);
7329 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, NULL);
7330 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7332 refcount3 = IDirectDrawSurface4_Release(surface);
7333 ok(!refcount3, "Got unexpected refcount %u.\n", refcount3);
7335 /* Destroying the surface frees the reference held on the private data. It also frees
7336 * the reference the surface is holding on its creating object. */
7337 refcount2 = get_refcount((IUnknown *)ddraw);
7338 ok(refcount2 == refcount - 1, "Got unexpected refcount %u.\n", refcount2);
7340 memset(&hal_caps, 0, sizeof(hal_caps));
7341 hal_caps.dwSize = sizeof(hal_caps);
7342 hr = IDirectDraw7_GetCaps(ddraw, &hal_caps, NULL);
7343 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
7344 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) == (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
7346 reset_ddsd(&surface_desc);
7347 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_MIPMAPCOUNT;
7348 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
7349 surface_desc.dwHeight = 4;
7350 surface_desc.dwWidth = 4;
7351 U2(surface_desc).dwMipMapCount = 2;
7352 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7353 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7354 hr = IDirectDrawSurface4_GetAttachedSurface(surface, &caps, &surface2);
7355 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
7357 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, data, sizeof(data), 0);
7358 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
7359 hr = IDirectDrawSurface4_GetPrivateData(surface2, &ddraw_private_data_test_guid, NULL, NULL);
7360 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
7362 IDirectDrawSurface4_Release(surface2);
7363 IDirectDrawSurface4_Release(surface);
7365 else
7366 skip("Mipmapped textures not supported, skipping mipmap private data test.\n");
7368 refcount = IDirectDraw4_Release(ddraw);
7369 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7370 DestroyWindow(window);
7373 static void test_pixel_format(void)
7375 HWND window, window2 = NULL;
7376 HDC hdc, hdc2 = NULL;
7377 HMODULE gl = NULL;
7378 int format, test_format;
7379 PIXELFORMATDESCRIPTOR pfd;
7380 IDirectDraw4 *ddraw = NULL;
7381 IDirectDrawClipper *clipper = NULL;
7382 DDSURFACEDESC2 ddsd;
7383 IDirectDrawSurface4 *primary = NULL;
7384 DDBLTFX fx;
7385 HRESULT hr;
7387 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
7388 100, 100, 160, 160, NULL, NULL, NULL, NULL);
7389 if (!window)
7391 skip("Failed to create window\n");
7392 return;
7395 window2 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
7396 100, 100, 160, 160, NULL, NULL, NULL, NULL);
7398 hdc = GetDC(window);
7399 if (!hdc)
7401 skip("Failed to get DC\n");
7402 goto cleanup;
7405 if (window2)
7406 hdc2 = GetDC(window2);
7408 gl = LoadLibraryA("opengl32.dll");
7409 ok(!!gl, "failed to load opengl32.dll; SetPixelFormat()/GetPixelFormat() may not work right\n");
7411 format = GetPixelFormat(hdc);
7412 ok(format == 0, "new window has pixel format %d\n", format);
7414 ZeroMemory(&pfd, sizeof(pfd));
7415 pfd.nSize = sizeof(pfd);
7416 pfd.nVersion = 1;
7417 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
7418 pfd.iPixelType = PFD_TYPE_RGBA;
7419 pfd.iLayerType = PFD_MAIN_PLANE;
7420 format = ChoosePixelFormat(hdc, &pfd);
7421 if (format <= 0)
7423 skip("no pixel format available\n");
7424 goto cleanup;
7427 if (!SetPixelFormat(hdc, format, &pfd) || GetPixelFormat(hdc) != format)
7429 skip("failed to set pixel format\n");
7430 goto cleanup;
7433 if (!hdc2 || !SetPixelFormat(hdc2, format, &pfd) || GetPixelFormat(hdc2) != format)
7435 skip("failed to set pixel format on second window\n");
7436 if (hdc2)
7438 ReleaseDC(window2, hdc2);
7439 hdc2 = NULL;
7443 ddraw = create_ddraw();
7444 ok(!!ddraw, "Failed to create a ddraw object.\n");
7446 test_format = GetPixelFormat(hdc);
7447 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7449 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7450 if (FAILED(hr))
7452 skip("Failed to set cooperative level, hr %#x.\n", hr);
7453 goto cleanup;
7456 test_format = GetPixelFormat(hdc);
7457 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7459 if (hdc2)
7461 hr = IDirectDraw4_CreateClipper(ddraw, 0, &clipper, NULL);
7462 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
7463 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window2);
7464 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
7466 test_format = GetPixelFormat(hdc);
7467 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7469 test_format = GetPixelFormat(hdc2);
7470 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
7473 memset(&ddsd, 0, sizeof(ddsd));
7474 ddsd.dwSize = sizeof(ddsd);
7475 ddsd.dwFlags = DDSD_CAPS;
7476 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
7478 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
7479 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
7481 test_format = GetPixelFormat(hdc);
7482 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7484 if (hdc2)
7486 test_format = GetPixelFormat(hdc2);
7487 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
7490 if (clipper)
7492 hr = IDirectDrawSurface4_SetClipper(primary, clipper);
7493 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
7495 test_format = GetPixelFormat(hdc);
7496 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7498 test_format = GetPixelFormat(hdc2);
7499 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
7502 memset(&fx, 0, sizeof(fx));
7503 fx.dwSize = sizeof(fx);
7504 hr = IDirectDrawSurface4_Blt(primary, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7505 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
7507 test_format = GetPixelFormat(hdc);
7508 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
7510 if (hdc2)
7512 test_format = GetPixelFormat(hdc2);
7513 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
7516 cleanup:
7517 if (primary) IDirectDrawSurface4_Release(primary);
7518 if (clipper) IDirectDrawClipper_Release(clipper);
7519 if (ddraw) IDirectDraw4_Release(ddraw);
7520 if (gl) FreeLibrary(gl);
7521 if (hdc) ReleaseDC(window, hdc);
7522 if (hdc2) ReleaseDC(window2, hdc2);
7523 if (window) DestroyWindow(window);
7524 if (window2) DestroyWindow(window2);
7527 static void test_create_surface_pitch(void)
7529 IDirectDrawSurface4 *surface;
7530 DDSURFACEDESC2 surface_desc;
7531 IDirectDraw4 *ddraw;
7532 unsigned int i;
7533 ULONG refcount;
7534 HWND window;
7535 HRESULT hr;
7536 void *mem;
7538 static const struct
7540 DWORD caps;
7541 DWORD flags_in;
7542 DWORD pitch_in;
7543 HRESULT hr;
7544 DWORD flags_out;
7545 DWORD pitch_out32;
7546 DWORD pitch_out64;
7548 test_data[] =
7550 /* 0 */
7551 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
7552 0, 0, DD_OK,
7553 DDSD_PITCH, 0x100, 0x100},
7554 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
7555 DDSD_PITCH, 0x104, DD_OK,
7556 DDSD_PITCH, 0x100, 0x100},
7557 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
7558 DDSD_PITCH, 0x0f8, DD_OK,
7559 DDSD_PITCH, 0x100, 0x100},
7560 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
7561 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDCAPS,
7562 0, 0, 0 },
7563 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7564 0, 0, DD_OK,
7565 DDSD_PITCH, 0x100, 0x0fc},
7566 /* 5 */
7567 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7568 DDSD_PITCH, 0x104, DD_OK,
7569 DDSD_PITCH, 0x100, 0x0fc},
7570 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7571 DDSD_PITCH, 0x0f8, DD_OK,
7572 DDSD_PITCH, 0x100, 0x0fc},
7573 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7574 DDSD_PITCH | DDSD_LINEARSIZE, 0, DD_OK,
7575 DDSD_PITCH, 0x100, 0x0fc},
7576 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7577 DDSD_LPSURFACE, 0, DDERR_INVALIDPARAMS,
7578 0, 0, 0 },
7579 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7580 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DD_OK,
7581 DDSD_PITCH, 0x100, 0x100},
7582 /* 10 */
7583 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7584 DDSD_LPSURFACE | DDSD_PITCH, 0x0fe, DDERR_INVALIDPARAMS,
7585 0, 0, 0 },
7586 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7587 DDSD_LPSURFACE | DDSD_PITCH, 0x0fc, DD_OK,
7588 DDSD_PITCH, 0x0fc, 0x0fc},
7589 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7590 DDSD_LPSURFACE | DDSD_PITCH, 0x0f8, DDERR_INVALIDPARAMS,
7591 0, 0, 0 },
7592 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7593 DDSD_LPSURFACE | DDSD_LINEARSIZE, 0x100, DDERR_INVALIDPARAMS,
7594 0, 0, 0 },
7595 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7596 DDSD_LPSURFACE | DDSD_LINEARSIZE, 0x3f00, DDERR_INVALIDPARAMS,
7597 0, 0, 0 },
7598 /* 15 */
7599 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
7600 DDSD_LPSURFACE | DDSD_PITCH | DDSD_LINEARSIZE, 0x100, DD_OK,
7601 DDSD_PITCH, 0x100, 0x100},
7602 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_ALLOCONLOAD,
7603 0, 0, DDERR_INVALIDCAPS,
7604 0, 0, 0 },
7605 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
7606 0, 0, DD_OK,
7607 DDSD_PITCH, 0x100, 0 },
7608 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
7609 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDCAPS,
7610 0, 0, 0 },
7611 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_ALLOCONLOAD,
7612 0, 0, DDERR_INVALIDCAPS,
7613 0, 0, 0 },
7614 /* 20 */
7615 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
7616 0, 0, DD_OK,
7617 DDSD_PITCH, 0x100, 0 },
7618 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
7619 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DD_OK,
7620 DDSD_PITCH, 0x100, 0 },
7622 DWORD flags_mask = DDSD_PITCH | DDSD_LPSURFACE | DDSD_LINEARSIZE;
7624 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7625 0, 0, 640, 480, 0, 0, 0, 0);
7626 ddraw = create_ddraw();
7627 ok(!!ddraw, "Failed to create a ddraw object.\n");
7628 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7629 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7631 mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ((63 * 4) + 8) * 63);
7633 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
7635 memset(&surface_desc, 0, sizeof(surface_desc));
7636 surface_desc.dwSize = sizeof(surface_desc);
7637 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | test_data[i].flags_in;
7638 surface_desc.ddsCaps.dwCaps = test_data[i].caps;
7639 surface_desc.dwWidth = 63;
7640 surface_desc.dwHeight = 63;
7641 U1(surface_desc).lPitch = test_data[i].pitch_in;
7642 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7643 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
7644 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
7645 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
7646 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
7647 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
7648 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7649 if (test_data[i].flags_in & DDSD_LPSURFACE)
7651 HRESULT expected_hr = SUCCEEDED(test_data[i].hr) ? DDERR_INVALIDPARAMS : test_data[i].hr;
7652 ok(hr == expected_hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, expected_hr);
7653 surface_desc.lpSurface = mem;
7654 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7656 if ((test_data[i].caps & DDSCAPS_VIDEOMEMORY) && hr == DDERR_NODIRECTDRAWHW)
7657 continue;
7658 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
7659 if (FAILED(hr))
7660 continue;
7662 memset(&surface_desc, 0, sizeof(surface_desc));
7663 surface_desc.dwSize = sizeof(surface_desc);
7664 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
7665 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
7666 ok((surface_desc.dwFlags & flags_mask) == test_data[i].flags_out,
7667 "Test %u: Got unexpected flags %#x, expected %#x.\n",
7668 i, surface_desc.dwFlags & flags_mask, test_data[i].flags_out);
7669 /* The pitch for textures seems to be implementation specific. */
7670 if (!(test_data[i].caps & DDSCAPS_TEXTURE))
7672 if (is_ddraw64 && test_data[i].pitch_out32 != test_data[i].pitch_out64)
7673 todo_wine ok(U1(surface_desc).lPitch == test_data[i].pitch_out64,
7674 "Test %u: Got unexpected pitch %u, expected %u.\n",
7675 i, U1(surface_desc).lPitch, test_data[i].pitch_out64);
7676 else
7677 ok(U1(surface_desc).lPitch == test_data[i].pitch_out32,
7678 "Test %u: Got unexpected pitch %u, expected %u.\n",
7679 i, U1(surface_desc).lPitch, test_data[i].pitch_out32);
7681 ok(!surface_desc.lpSurface, "Test %u: Got unexpected lpSurface %p.\n", i, surface_desc.lpSurface);
7683 IDirectDrawSurface4_Release(surface);
7686 HeapFree(GetProcessHeap(), 0, mem);
7687 refcount = IDirectDraw4_Release(ddraw);
7688 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7689 DestroyWindow(window);
7692 static void test_mipmap(void)
7694 IDirectDrawSurface4 *surface, *surface2;
7695 DDSURFACEDESC2 surface_desc;
7696 IDirectDraw4 *ddraw;
7697 unsigned int i;
7698 ULONG refcount;
7699 HWND window;
7700 HRESULT hr;
7701 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
7702 DDCAPS hal_caps;
7704 static const struct
7706 DWORD flags;
7707 DWORD caps;
7708 DWORD width;
7709 DWORD height;
7710 DWORD mipmap_count_in;
7711 HRESULT hr;
7712 DWORD mipmap_count_out;
7714 tests[] =
7716 {DDSD_MIPMAPCOUNT, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 128, 32, 3, DD_OK, 3},
7717 {DDSD_MIPMAPCOUNT, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 128, 32, 0, DDERR_INVALIDPARAMS, 0},
7718 {0, DDSCAPS_TEXTURE | DDSCAPS_MIPMAP, 128, 32, 0, DD_OK, 1},
7719 {0, DDSCAPS_MIPMAP, 128, 32, 0, DDERR_INVALIDCAPS, 0},
7720 {0, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 128, 32, 0, DD_OK, 6},
7721 {0, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 32, 64, 0, DD_OK, 6},
7724 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7725 0, 0, 640, 480, 0, 0, 0, 0);
7726 ddraw = create_ddraw();
7727 ok(!!ddraw, "Failed to create a ddraw object.\n");
7728 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7729 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7731 memset(&hal_caps, 0, sizeof(hal_caps));
7732 hal_caps.dwSize = sizeof(hal_caps);
7733 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
7734 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
7735 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
7737 skip("Mipmapped textures not supported, skipping tests.\n");
7738 IDirectDraw4_Release(ddraw);
7739 DestroyWindow(window);
7740 return;
7743 for (i = 0; i < sizeof(tests) / sizeof(*tests); ++i)
7745 memset(&surface_desc, 0, sizeof(surface_desc));
7746 surface_desc.dwSize = sizeof(surface_desc);
7747 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | tests[i].flags;
7748 surface_desc.ddsCaps.dwCaps = tests[i].caps;
7749 surface_desc.dwWidth = tests[i].width;
7750 surface_desc.dwHeight = tests[i].height;
7751 if (tests[i].flags & DDSD_MIPMAPCOUNT)
7752 U2(surface_desc).dwMipMapCount = tests[i].mipmap_count_in;
7753 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7754 ok(hr == tests[i].hr, "Test %u: Got unexpected hr %#x.\n", i, hr);
7755 if (FAILED(hr))
7756 continue;
7758 memset(&surface_desc, 0, sizeof(surface_desc));
7759 surface_desc.dwSize = sizeof(surface_desc);
7760 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
7761 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
7762 ok(surface_desc.dwFlags & DDSD_MIPMAPCOUNT,
7763 "Test %u: Got unexpected flags %#x.\n", i, surface_desc.dwFlags);
7764 ok(U2(surface_desc).dwMipMapCount == tests[i].mipmap_count_out,
7765 "Test %u: Got unexpected mipmap count %u.\n", i, U2(surface_desc).dwMipMapCount);
7767 if (U2(surface_desc).dwMipMapCount > 1)
7769 hr = IDirectDrawSurface4_GetAttachedSurface(surface, &caps, &surface2);
7770 ok(SUCCEEDED(hr), "Test %u: Failed to get attached surface, hr %#x.\n", i, hr);
7772 memset(&surface_desc, 0, sizeof(surface_desc));
7773 surface_desc.dwSize = sizeof(surface_desc);
7774 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, 0, NULL);
7775 ok(SUCCEEDED(hr), "Test %u: Failed to lock surface, hr %#x.\n", i, hr);
7776 memset(&surface_desc, 0, sizeof(surface_desc));
7777 surface_desc.dwSize = sizeof(surface_desc);
7778 hr = IDirectDrawSurface4_Lock(surface2, NULL, &surface_desc, 0, NULL);
7779 ok(SUCCEEDED(hr), "Test %u: Failed to lock surface, hr %#x.\n", i, hr);
7780 IDirectDrawSurface4_Unlock(surface2, NULL);
7781 IDirectDrawSurface4_Unlock(surface, NULL);
7783 IDirectDrawSurface4_Release(surface2);
7786 IDirectDrawSurface4_Release(surface);
7789 refcount = IDirectDraw4_Release(ddraw);
7790 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7791 DestroyWindow(window);
7794 static void test_palette_complex(void)
7796 IDirectDrawSurface4 *surface, *mipmap, *tmp;
7797 DDSURFACEDESC2 surface_desc;
7798 IDirectDraw4 *ddraw;
7799 IDirectDrawPalette *palette, *palette2, *palette_mipmap;
7800 ULONG refcount;
7801 HWND window;
7802 HRESULT hr;
7803 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
7804 DDCAPS hal_caps;
7805 PALETTEENTRY palette_entries[256];
7806 unsigned int i;
7807 HDC dc;
7808 RGBQUAD rgbquad;
7809 UINT count;
7811 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7812 0, 0, 640, 480, 0, 0, 0, 0);
7813 ddraw = create_ddraw();
7814 ok(!!ddraw, "Failed to create a ddraw object.\n");
7815 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7816 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7818 memset(&hal_caps, 0, sizeof(hal_caps));
7819 hal_caps.dwSize = sizeof(hal_caps);
7820 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
7821 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
7822 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
7824 skip("Mipmapped textures not supported, skipping mipmap palette test.\n");
7825 IDirectDraw4_Release(ddraw);
7826 DestroyWindow(window);
7827 return;
7830 memset(&surface_desc, 0, sizeof(surface_desc));
7831 surface_desc.dwSize = sizeof(surface_desc);
7832 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7833 surface_desc.dwWidth = 128;
7834 surface_desc.dwHeight = 128;
7835 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
7836 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7837 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
7838 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
7839 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7840 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7842 memset(palette_entries, 0, sizeof(palette_entries));
7843 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7844 palette_entries, &palette, NULL);
7845 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7847 memset(palette_entries, 0, sizeof(palette_entries));
7848 palette_entries[1].peRed = 0xff;
7849 palette_entries[1].peGreen = 0x80;
7850 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7851 palette_entries, &palette_mipmap, NULL);
7852 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7854 palette2 = (void *)0xdeadbeef;
7855 hr = IDirectDrawSurface4_GetPalette(surface, &palette2);
7856 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
7857 ok(!palette2, "Got unexpected palette %p.\n", palette2);
7858 hr = IDirectDrawSurface4_SetPalette(surface, palette);
7859 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7860 hr = IDirectDrawSurface4_GetPalette(surface, &palette2);
7861 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
7862 ok(palette == palette2, "Got unexpected palette %p.\n", palette2);
7863 IDirectDrawPalette_Release(palette2);
7865 mipmap = surface;
7866 IDirectDrawSurface4_AddRef(mipmap);
7867 for (i = 0; i < 7; ++i)
7869 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
7870 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
7871 palette2 = (void *)0xdeadbeef;
7872 hr = IDirectDrawSurface4_GetPalette(tmp, &palette2);
7873 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x, i %u.\n", hr, i);
7874 ok(!palette2, "Got unexpected palette %p, i %u.\n", palette2, i);
7876 hr = IDirectDrawSurface4_SetPalette(tmp, palette_mipmap);
7877 ok(SUCCEEDED(hr), "Failed to set palette, i %u, hr %#x.\n", i, hr);
7879 hr = IDirectDrawSurface4_GetPalette(tmp, &palette2);
7880 ok(SUCCEEDED(hr), "Failed to get palette, i %u, hr %#x.\n", i, hr);
7881 ok(palette_mipmap == palette2, "Got unexpected palette %p.\n", palette2);
7882 IDirectDrawPalette_Release(palette2);
7884 hr = IDirectDrawSurface4_GetDC(tmp, &dc);
7885 ok(SUCCEEDED(hr), "Failed to get DC, i %u, hr %#x.\n", i, hr);
7886 count = GetDIBColorTable(dc, 1, 1, &rgbquad);
7887 ok(count == 1, "Expected count 1, got %u.\n", count);
7888 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x.\n", rgbquad.rgbRed);
7889 ok(rgbquad.rgbGreen == 0x80, "Expected rgbGreen = 0x80, got %#x.\n", rgbquad.rgbGreen);
7890 ok(rgbquad.rgbBlue == 0x0, "Expected rgbBlue = 0x0, got %#x.\n", rgbquad.rgbBlue);
7891 hr = IDirectDrawSurface4_ReleaseDC(tmp, dc);
7892 ok(SUCCEEDED(hr), "Failed to release DC, i %u, hr %#x.\n", i, hr);
7894 IDirectDrawSurface4_Release(mipmap);
7895 mipmap = tmp;
7898 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
7899 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
7900 IDirectDrawSurface4_Release(mipmap);
7901 refcount = IDirectDrawSurface4_Release(surface);
7902 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7903 refcount = IDirectDrawPalette_Release(palette_mipmap);
7904 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7905 refcount = IDirectDrawPalette_Release(palette);
7906 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7908 refcount = IDirectDraw4_Release(ddraw);
7909 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7910 DestroyWindow(window);
7913 static void test_p8_rgb_blit(void)
7915 IDirectDrawSurface4 *src, *dst;
7916 DDSURFACEDESC2 surface_desc;
7917 IDirectDraw4 *ddraw;
7918 IDirectDrawPalette *palette;
7919 ULONG refcount;
7920 HWND window;
7921 HRESULT hr;
7922 PALETTEENTRY palette_entries[256];
7923 unsigned int x;
7924 static const BYTE src_data[] = {0x10, 0x1, 0x2, 0x3, 0x4, 0x5, 0xff, 0x80};
7925 static const D3DCOLOR expected[] =
7927 0x00101010, 0x00010101, 0x00020202, 0x00030303,
7928 0x00040404, 0x00050505, 0x00ffffff, 0x00808080,
7930 D3DCOLOR color;
7932 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7933 0, 0, 640, 480, 0, 0, 0, 0);
7934 ddraw = create_ddraw();
7935 ok(!!ddraw, "Failed to create a ddraw object.\n");
7936 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7937 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7939 memset(palette_entries, 0, sizeof(palette_entries));
7940 palette_entries[1].peGreen = 0xff;
7941 palette_entries[2].peBlue = 0xff;
7942 palette_entries[3].peFlags = 0xff;
7943 palette_entries[4].peRed = 0xff;
7944 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7945 palette_entries, &palette, NULL);
7946 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7948 memset(&surface_desc, 0, sizeof(surface_desc));
7949 surface_desc.dwSize = sizeof(surface_desc);
7950 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7951 surface_desc.dwWidth = 8;
7952 surface_desc.dwHeight = 1;
7953 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7954 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7955 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
7956 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
7957 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &src, NULL);
7958 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7960 memset(&surface_desc, 0, sizeof(surface_desc));
7961 surface_desc.dwSize = sizeof(surface_desc);
7962 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7963 surface_desc.dwWidth = 8;
7964 surface_desc.dwHeight = 1;
7965 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7966 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7967 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
7968 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
7969 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
7970 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
7971 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
7972 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
7973 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &dst, NULL);
7974 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7976 memset(&surface_desc, 0, sizeof(surface_desc));
7977 surface_desc.dwSize = sizeof(surface_desc);
7978 hr = IDirectDrawSurface4_Lock(src, NULL, &surface_desc, 0, NULL);
7979 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
7980 memcpy(surface_desc.lpSurface, src_data, sizeof(src_data));
7981 hr = IDirectDrawSurface4_Unlock(src, NULL);
7982 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
7984 hr = IDirectDrawSurface4_SetPalette(src, palette);
7985 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7986 hr = IDirectDrawSurface4_Blt(dst, NULL, src, NULL, DDBLT_WAIT, NULL);
7987 /* The r500 Windows 7 driver returns E_NOTIMPL. r200 on Windows XP works.
7988 * The Geforce 7 driver on Windows Vista returns E_FAIL. Newer Nvidia GPUs work. */
7989 ok(SUCCEEDED(hr) || broken(hr == E_NOTIMPL) || broken(hr == E_FAIL),
7990 "Failed to blit, hr %#x.\n", hr);
7992 if (SUCCEEDED(hr))
7994 for (x = 0; x < sizeof(expected) / sizeof(*expected); x++)
7996 color = get_surface_color(dst, x, 0);
7997 todo_wine ok(compare_color(color, expected[x], 0),
7998 "Pixel %u: Got color %#x, expected %#x.\n",
7999 x, color, expected[x]);
8003 IDirectDrawSurface4_Release(src);
8004 IDirectDrawSurface4_Release(dst);
8005 IDirectDrawPalette_Release(palette);
8007 refcount = IDirectDraw4_Release(ddraw);
8008 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8009 DestroyWindow(window);
8012 static void test_material(void)
8014 D3DMATERIALHANDLE mat_handle, tmp;
8015 IDirect3DMaterial3 *material;
8016 IDirect3DViewport3 *viewport;
8017 IDirect3DDevice3 *device;
8018 IDirectDrawSurface4 *rt;
8019 D3DCOLOR color;
8020 ULONG refcount;
8021 unsigned int i;
8022 HWND window;
8023 HRESULT hr;
8024 BOOL valid;
8026 static struct
8028 struct vec3 position;
8029 struct vec3 normal;
8030 D3DCOLOR diffuse;
8032 quad1[] =
8034 {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
8035 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
8036 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
8037 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
8039 quad2[] =
8041 {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
8042 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
8043 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
8044 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
8046 static const struct
8048 void *data;
8049 BOOL material;
8050 D3DCOLOR expected_color;
8052 test_data[] =
8054 {quad1, TRUE, 0x0000ff00},
8055 {quad2, TRUE, 0x0000ff00},
8056 {quad1, FALSE, 0x00ffffff},
8057 {quad2, FALSE, 0x00ff0000},
8059 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
8061 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8062 0, 0, 640, 480, 0, 0, 0, 0);
8063 if (!(device = create_device(window, DDSCL_NORMAL)))
8065 skip("Failed to create a 3D device, skipping test.\n");
8066 DestroyWindow(window);
8067 return;
8070 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
8071 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
8073 viewport = create_viewport(device, 0, 0, 640, 480);
8074 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
8075 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
8077 material = create_emissive_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
8078 hr = IDirect3DMaterial3_GetHandle(material, device, &mat_handle);
8079 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
8081 hr = IDirect3DDevice3_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
8082 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
8083 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
8084 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
8085 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
8086 hr = IDirect3DDevice3_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
8087 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
8088 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
8089 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, 0);
8090 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
8091 hr = IDirect3DDevice3_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
8092 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
8093 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
8095 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
8097 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect,
8098 D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 1.0f, 0);
8099 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
8101 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, test_data[i].material ? mat_handle : 0);
8102 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
8104 hr = IDirect3DDevice3_BeginScene(device);
8105 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
8106 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP,
8107 D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE, test_data[i].data, 4, 0);
8108 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8109 hr = IDirect3DDevice3_EndScene(device);
8110 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
8111 color = get_surface_color(rt, 320, 240);
8112 ok(compare_color(color, test_data[i].expected_color, 1),
8113 "Got unexpected color 0x%08x, test %u.\n", color, i);
8116 destroy_material(material);
8117 material = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
8118 hr = IDirect3DMaterial3_GetHandle(material, device, &mat_handle);
8119 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
8121 hr = IDirect3DViewport3_SetBackground(viewport, mat_handle);
8122 ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
8123 hr = IDirect3DViewport3_GetBackground(viewport, &tmp, &valid);
8124 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
8125 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
8126 ok(valid, "Got unexpected valid %#x.\n", valid);
8127 hr = IDirect3DViewport3_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
8128 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
8129 color = get_surface_color(rt, 320, 240);
8130 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
8132 hr = IDirect3DViewport3_SetBackground(viewport, 0);
8133 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8134 hr = IDirect3DViewport3_GetBackground(viewport, &tmp, &valid);
8135 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
8136 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
8137 ok(valid, "Got unexpected valid %#x.\n", valid);
8138 hr = IDirect3DViewport3_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
8139 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
8140 color = get_surface_color(rt, 320, 240);
8141 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
8143 destroy_viewport(device, viewport);
8144 viewport = create_viewport(device, 0, 0, 640, 480);
8146 hr = IDirect3DViewport3_GetBackground(viewport, &tmp, &valid);
8147 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
8148 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
8149 ok(!valid, "Got unexpected valid %#x.\n", valid);
8150 hr = IDirect3DViewport3_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
8151 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
8152 color = get_surface_color(rt, 320, 240);
8153 ok(compare_color(color, 0x00000000, 1), "Got unexpected color 0x%08x.\n", color);
8155 destroy_viewport(device, viewport);
8156 destroy_material(material);
8157 IDirectDrawSurface4_Release(rt);
8158 refcount = IDirect3DDevice3_Release(device);
8159 ok(!refcount, "Device has %u references left.\n", refcount);
8160 DestroyWindow(window);
8163 static void test_palette_gdi(void)
8165 IDirectDrawSurface4 *surface, *primary;
8166 DDSURFACEDESC2 surface_desc;
8167 IDirectDraw4 *ddraw;
8168 IDirectDrawPalette *palette, *palette2;
8169 ULONG refcount;
8170 HWND window;
8171 HRESULT hr;
8172 PALETTEENTRY palette_entries[256];
8173 UINT i;
8174 HDC dc;
8175 /* On the Windows 8 testbot palette index 0 of the onscreen palette is forced to
8176 * r = 0, g = 0, b = 0. Do not attempt to set it to something else as this is
8177 * not the point of this test. */
8178 static const RGBQUAD expected1[] =
8180 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
8181 {0x03, 0x00, 0x00, 0x00}, {0x15, 0x14, 0x13, 0x00},
8183 static const RGBQUAD expected2[] =
8185 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
8186 {0x03, 0x00, 0x00, 0x00}, {0x25, 0x24, 0x23, 0x00},
8188 static const RGBQUAD expected3[] =
8190 {0x00, 0x00, 0x00, 0x00}, {0x40, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x40, 0x00},
8191 {0x00, 0x40, 0x00, 0x00}, {0x56, 0x34, 0x12, 0x00},
8193 HPALETTE ddraw_palette_handle;
8194 /* Similar to index 0, index 255 is r = 0xff, g = 0xff, b = 0xff on the Win8 VMs. */
8195 RGBQUAD rgbquad[255];
8196 static const RGBQUAD rgb_zero = {0, 0, 0, 0};
8198 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8199 0, 0, 640, 480, 0, 0, 0, 0);
8200 ddraw = create_ddraw();
8201 ok(!!ddraw, "Failed to create a ddraw object.\n");
8202 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
8203 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8205 memset(&surface_desc, 0, sizeof(surface_desc));
8206 surface_desc.dwSize = sizeof(surface_desc);
8207 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
8208 surface_desc.dwWidth = 16;
8209 surface_desc.dwHeight = 16;
8210 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
8211 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
8212 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
8213 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
8214 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8215 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8217 /* Avoid colors from the Windows default palette. */
8218 memset(palette_entries, 0, sizeof(palette_entries));
8219 palette_entries[1].peRed = 0x01;
8220 palette_entries[2].peGreen = 0x02;
8221 palette_entries[3].peBlue = 0x03;
8222 palette_entries[4].peRed = 0x13;
8223 palette_entries[4].peGreen = 0x14;
8224 palette_entries[4].peBlue = 0x15;
8225 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
8226 palette_entries, &palette, NULL);
8227 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
8229 /* If there is no palette assigned and the display mode is not 8 bpp, some
8230 * drivers refuse to create a DC while others allow it. If a DC is created,
8231 * the DIB color table is uninitialized and contains random colors. No error
8232 * is generated when trying to read pixels and random garbage is returned.
8234 * The most likely explanation is that if the driver creates a DC, it (or
8235 * the higher-level runtime) uses GetSystemPaletteEntries to find the
8236 * palette, but GetSystemPaletteEntries fails when bpp > 8 and the palette
8237 * contains uninitialized garbage. See comments below for the P8 case. */
8239 hr = IDirectDrawSurface4_SetPalette(surface, palette);
8240 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
8241 hr = IDirectDrawSurface4_GetDC(surface, &dc);
8242 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
8243 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
8244 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE),
8245 "Got unexpected palette %p, expected %p.\n",
8246 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
8248 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
8249 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
8250 for (i = 0; i < sizeof(expected1) / sizeof(*expected1); i++)
8252 ok(!memcmp(&rgbquad[i], &expected1[i], sizeof(rgbquad[i])),
8253 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8254 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
8255 expected1[i].rgbRed, expected1[i].rgbGreen, expected1[i].rgbBlue);
8257 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
8259 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
8260 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
8261 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
8264 /* Update the palette while the DC is in use. This does not modify the DC. */
8265 palette_entries[4].peRed = 0x23;
8266 palette_entries[4].peGreen = 0x24;
8267 palette_entries[4].peBlue = 0x25;
8268 hr = IDirectDrawPalette_SetEntries(palette, 0, 4, 1, &palette_entries[4]);
8269 ok(SUCCEEDED(hr), "Failed to set palette entries, hr %#x.\n", hr);
8271 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
8272 ok(i == 1, "Expected count 1, got %u.\n", i);
8273 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
8274 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8275 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
8276 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
8278 /* Neither does re-setting the palette. */
8279 hr = IDirectDrawSurface4_SetPalette(surface, NULL);
8280 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
8281 hr = IDirectDrawSurface4_SetPalette(surface, palette);
8282 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
8284 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
8285 ok(i == 1, "Expected count 1, got %u.\n", i);
8286 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
8287 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8288 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
8289 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
8291 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
8292 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8294 /* Refresh the DC. This updates the palette. */
8295 hr = IDirectDrawSurface4_GetDC(surface, &dc);
8296 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
8297 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
8298 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
8299 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
8301 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
8302 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8303 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
8304 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
8306 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
8308 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
8309 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
8310 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
8312 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
8313 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8315 refcount = IDirectDrawSurface4_Release(surface);
8316 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8318 if (FAILED(IDirectDraw4_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
8320 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
8321 IDirectDrawPalette_Release(palette);
8322 IDirectDraw4_Release(ddraw);
8323 DestroyWindow(window);
8324 return;
8326 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
8327 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
8328 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8330 memset(&surface_desc, 0, sizeof(surface_desc));
8331 surface_desc.dwSize = sizeof(surface_desc);
8332 surface_desc.dwFlags = DDSD_CAPS;
8333 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
8334 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &primary, NULL);
8335 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8337 hr = IDirectDrawSurface4_SetPalette(primary, palette);
8338 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
8339 hr = IDirectDrawSurface4_GetDC(primary, &dc);
8340 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
8341 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
8342 /* Windows 2000 on the testbot assigns a different palette to the primary. Refrast? */
8343 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE) || broken(TRUE),
8344 "Got unexpected palette %p, expected %p.\n",
8345 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
8346 SelectPalette(dc, ddraw_palette_handle, FALSE);
8348 /* The primary uses the system palette. In exclusive mode, the system palette matches
8349 * the ddraw palette attached to the primary, so the result is what you would expect
8350 * from a regular surface. Tests for the interaction between the ddraw palette and
8351 * the system palette are not included pending an application that depends on this.
8352 * The relation between those causes problems on Windows Vista and newer for games
8353 * like Age of Empires or StarCraft. Don't emulate it without a real need. */
8354 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
8355 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
8356 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
8358 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
8359 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8360 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
8361 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
8363 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
8365 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
8366 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
8367 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
8369 hr = IDirectDrawSurface4_ReleaseDC(primary, dc);
8370 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8372 memset(&surface_desc, 0, sizeof(surface_desc));
8373 surface_desc.dwSize = sizeof(surface_desc);
8374 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
8375 surface_desc.dwWidth = 16;
8376 surface_desc.dwHeight = 16;
8377 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
8378 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8379 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8381 /* Here the offscreen surface appears to use the primary's palette,
8382 * but in all likelihood it is actually the system palette. */
8383 hr = IDirectDrawSurface4_GetDC(surface, &dc);
8384 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
8385 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
8386 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
8387 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
8389 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
8390 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8391 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
8392 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
8394 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
8396 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
8397 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
8398 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
8400 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
8401 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8403 /* On real hardware a change to the primary surface's palette applies immediately,
8404 * even on device contexts from offscreen surfaces that do not have their own
8405 * palette. On the testbot VMs this is not the case. Don't test this until we
8406 * know of an application that depends on this. */
8408 memset(palette_entries, 0, sizeof(palette_entries));
8409 palette_entries[1].peBlue = 0x40;
8410 palette_entries[2].peRed = 0x40;
8411 palette_entries[3].peGreen = 0x40;
8412 palette_entries[4].peRed = 0x12;
8413 palette_entries[4].peGreen = 0x34;
8414 palette_entries[4].peBlue = 0x56;
8415 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
8416 palette_entries, &palette2, NULL);
8417 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
8418 hr = IDirectDrawSurface4_SetPalette(surface, palette2);
8419 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
8421 /* A palette assigned to the offscreen surface overrides the primary / system
8422 * palette. */
8423 hr = IDirectDrawSurface4_GetDC(surface, &dc);
8424 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
8425 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
8426 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
8427 for (i = 0; i < sizeof(expected3) / sizeof(*expected3); i++)
8429 ok(!memcmp(&rgbquad[i], &expected3[i], sizeof(rgbquad[i])),
8430 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
8431 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
8432 expected3[i].rgbRed, expected3[i].rgbGreen, expected3[i].rgbBlue);
8434 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
8436 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
8437 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
8438 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
8440 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
8441 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8443 refcount = IDirectDrawSurface4_Release(surface);
8444 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8446 /* The Windows 8 testbot keeps extra references to the primary and
8447 * backbuffer while in 8 bpp mode. */
8448 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
8449 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
8451 refcount = IDirectDrawSurface4_Release(primary);
8452 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8453 refcount = IDirectDrawPalette_Release(palette2);
8454 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8455 refcount = IDirectDrawPalette_Release(palette);
8456 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8457 refcount = IDirectDraw4_Release(ddraw);
8458 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8459 DestroyWindow(window);
8462 static void test_palette_alpha(void)
8464 IDirectDrawSurface4 *surface;
8465 DDSURFACEDESC2 surface_desc;
8466 IDirectDraw4 *ddraw;
8467 IDirectDrawPalette *palette;
8468 ULONG refcount;
8469 HWND window;
8470 HRESULT hr;
8471 PALETTEENTRY palette_entries[256];
8472 unsigned int i;
8473 static const struct
8475 DWORD caps, flags;
8476 BOOL attach_allowed;
8477 const char *name;
8479 test_data[] =
8481 {DDSCAPS_OFFSCREENPLAIN, DDSD_WIDTH | DDSD_HEIGHT, FALSE, "offscreenplain"},
8482 {DDSCAPS_TEXTURE, DDSD_WIDTH | DDSD_HEIGHT, TRUE, "texture"},
8483 {DDSCAPS_PRIMARYSURFACE, 0, FALSE, "primary"}
8486 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8487 0, 0, 640, 480, 0, 0, 0, 0);
8488 ddraw = create_ddraw();
8489 ok(!!ddraw, "Failed to create a ddraw object.\n");
8490 if (FAILED(IDirectDraw4_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
8492 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
8493 IDirectDraw4_Release(ddraw);
8494 DestroyWindow(window);
8495 return;
8497 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
8498 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8500 memset(palette_entries, 0, sizeof(palette_entries));
8501 palette_entries[1].peFlags = 0x42;
8502 palette_entries[2].peFlags = 0xff;
8503 palette_entries[3].peFlags = 0x80;
8504 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
8505 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
8507 memset(palette_entries, 0x66, sizeof(palette_entries));
8508 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
8509 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
8510 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
8511 palette_entries[0].peFlags);
8512 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
8513 palette_entries[1].peFlags);
8514 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
8515 palette_entries[2].peFlags);
8516 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
8517 palette_entries[3].peFlags);
8519 IDirectDrawPalette_Release(palette);
8521 memset(palette_entries, 0, sizeof(palette_entries));
8522 palette_entries[1].peFlags = 0x42;
8523 palette_entries[1].peRed = 0xff;
8524 palette_entries[2].peFlags = 0xff;
8525 palette_entries[3].peFlags = 0x80;
8526 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT | DDPCAPS_ALPHA,
8527 palette_entries, &palette, NULL);
8528 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
8530 memset(palette_entries, 0x66, sizeof(palette_entries));
8531 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
8532 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
8533 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
8534 palette_entries[0].peFlags);
8535 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
8536 palette_entries[1].peFlags);
8537 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
8538 palette_entries[2].peFlags);
8539 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
8540 palette_entries[3].peFlags);
8542 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); i++)
8544 memset(&surface_desc, 0, sizeof(surface_desc));
8545 surface_desc.dwSize = sizeof(surface_desc);
8546 surface_desc.dwFlags = DDSD_CAPS | test_data[i].flags;
8547 surface_desc.dwWidth = 128;
8548 surface_desc.dwHeight = 128;
8549 surface_desc.ddsCaps.dwCaps = test_data[i].caps;
8550 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8551 ok(SUCCEEDED(hr), "Failed to create %s surface, hr %#x.\n", test_data[i].name, hr);
8553 hr = IDirectDrawSurface4_SetPalette(surface, palette);
8554 if (test_data[i].attach_allowed)
8555 ok(SUCCEEDED(hr), "Failed to attach palette to %s surface, hr %#x.\n", test_data[i].name, hr);
8556 else
8557 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x, %s surface.\n", hr, test_data[i].name);
8559 if (SUCCEEDED(hr))
8561 HDC dc;
8562 RGBQUAD rgbquad;
8563 UINT retval;
8565 hr = IDirectDrawSurface4_GetDC(surface, &dc);
8566 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x, %s surface.\n", hr, test_data[i].name);
8567 retval = GetDIBColorTable(dc, 1, 1, &rgbquad);
8568 ok(retval == 1, "GetDIBColorTable returned unexpected result %u.\n", retval);
8569 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x, %s surface.\n",
8570 rgbquad.rgbRed, test_data[i].name);
8571 ok(rgbquad.rgbGreen == 0, "Expected rgbGreen = 0, got %#x, %s surface.\n",
8572 rgbquad.rgbGreen, test_data[i].name);
8573 ok(rgbquad.rgbBlue == 0, "Expected rgbBlue = 0, got %#x, %s surface.\n",
8574 rgbquad.rgbBlue, test_data[i].name);
8575 todo_wine ok(rgbquad.rgbReserved == 0, "Expected rgbReserved = 0, got %u, %s surface.\n",
8576 rgbquad.rgbReserved, test_data[i].name);
8577 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
8578 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8580 IDirectDrawSurface4_Release(surface);
8583 /* Test INVALIDSURFACETYPE vs INVALIDPIXELFORMAT. */
8584 memset(&surface_desc, 0, sizeof(surface_desc));
8585 surface_desc.dwSize = sizeof(surface_desc);
8586 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
8587 surface_desc.dwWidth = 128;
8588 surface_desc.dwHeight = 128;
8589 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
8590 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
8591 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
8592 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
8593 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
8594 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
8595 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
8596 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8597 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8598 hr = IDirectDrawSurface4_SetPalette(surface, palette);
8599 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x.\n", hr);
8600 IDirectDrawSurface4_Release(surface);
8602 /* The Windows 8 testbot keeps extra references to the primary
8603 * while in 8 bpp mode. */
8604 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
8605 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
8607 refcount = IDirectDrawPalette_Release(palette);
8608 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8609 refcount = IDirectDraw4_Release(ddraw);
8610 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8611 DestroyWindow(window);
8614 static void test_vb_writeonly(void)
8616 IDirect3DDevice3 *device;
8617 IDirect3D3 *d3d;
8618 IDirect3DVertexBuffer *buffer;
8619 HWND window;
8620 HRESULT hr;
8621 D3DVERTEXBUFFERDESC desc;
8622 void *ptr;
8623 static const struct vec4 quad[] =
8625 { 0.0f, 480.0f, 0.0f, 1.0f},
8626 { 0.0f, 0.0f, 0.0f, 1.0f},
8627 {640.0f, 480.0f, 0.0f, 1.0f},
8628 {640.0f, 0.0f, 0.0f, 1.0f},
8631 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8632 0, 0, 640, 480, 0, 0, 0, 0);
8634 if (!(device = create_device(window, DDSCL_NORMAL)))
8636 skip("Failed to create a 3D device, skipping test.\n");
8637 DestroyWindow(window);
8638 return;
8641 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
8642 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
8644 memset(&desc, 0, sizeof(desc));
8645 desc.dwSize = sizeof(desc);
8646 desc.dwCaps = D3DVBCAPS_WRITEONLY;
8647 desc.dwFVF = D3DFVF_XYZRHW;
8648 desc.dwNumVertices = sizeof(quad) / sizeof(*quad);
8649 hr = IDirect3D3_CreateVertexBuffer(d3d, &desc, &buffer, 0, NULL);
8650 ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr);
8652 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, &ptr, NULL);
8653 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
8654 memcpy(ptr, quad, sizeof(quad));
8655 hr = IDirect3DVertexBuffer_Unlock(buffer);
8656 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
8658 hr = IDirect3DDevice3_BeginScene(device);
8659 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
8660 hr = IDirect3DDevice3_DrawPrimitiveVB(device, D3DPT_TRIANGLESTRIP, buffer, 0, 4, 0);
8661 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8662 hr = IDirect3DDevice3_EndScene(device);
8663 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
8665 hr = IDirect3DVertexBuffer_Lock(buffer, 0, &ptr, NULL);
8666 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
8667 ok (!memcmp(ptr, quad, sizeof(quad)), "Got unexpected vertex buffer data.\n");
8668 hr = IDirect3DVertexBuffer_Unlock(buffer);
8669 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
8671 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_READONLY, &ptr, NULL);
8672 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
8673 ok (!memcmp(ptr, quad, sizeof(quad)), "Got unexpected vertex buffer data.\n");
8674 hr = IDirect3DVertexBuffer_Unlock(buffer);
8675 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
8677 IDirect3DVertexBuffer_Release(buffer);
8678 IDirect3D3_Release(d3d);
8679 IDirect3DDevice3_Release(device);
8680 DestroyWindow(window);
8683 static void test_lost_device(void)
8685 IDirectDrawSurface4 *surface;
8686 DDSURFACEDESC2 surface_desc;
8687 HWND window1, window2;
8688 IDirectDraw4 *ddraw;
8689 ULONG refcount;
8690 HRESULT hr;
8691 BOOL ret;
8693 window1 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8694 0, 0, 640, 480, 0, 0, 0, 0);
8695 window2 = 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, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
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_BACKBUFFERCOUNT;
8705 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
8706 U5(surface_desc).dwBackBufferCount = 1;
8707 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8708 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8710 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8711 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8712 hr = IDirectDrawSurface4_IsLost(surface);
8713 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8714 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8715 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8717 ret = SetForegroundWindow(GetDesktopWindow());
8718 ok(ret, "Failed to set foreground window.\n");
8719 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8720 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
8721 hr = IDirectDrawSurface4_IsLost(surface);
8722 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8723 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8724 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8726 ret = SetForegroundWindow(window1);
8727 ok(ret, "Failed to set foreground window.\n");
8728 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8729 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8730 hr = IDirectDrawSurface4_IsLost(surface);
8731 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8732 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8733 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8735 hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
8736 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8737 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8738 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8739 hr = IDirectDrawSurface4_IsLost(surface);
8740 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8741 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8742 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8744 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
8745 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8746 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8747 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8748 hr = IDirectDrawSurface4_IsLost(surface);
8749 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8750 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8751 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8753 /* Trying to restore the primary will crash, probably because flippable
8754 * surfaces can't exist in DDSCL_NORMAL. */
8755 IDirectDrawSurface4_Release(surface);
8756 memset(&surface_desc, 0, sizeof(surface_desc));
8757 surface_desc.dwSize = sizeof(surface_desc);
8758 surface_desc.dwFlags = DDSD_CAPS;
8759 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
8760 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8761 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8763 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8764 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8765 hr = IDirectDrawSurface4_IsLost(surface);
8766 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8768 ret = SetForegroundWindow(GetDesktopWindow());
8769 ok(ret, "Failed to set foreground window.\n");
8770 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8771 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8772 hr = IDirectDrawSurface4_IsLost(surface);
8773 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8775 ret = SetForegroundWindow(window1);
8776 ok(ret, "Failed to set foreground window.\n");
8777 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8778 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8779 hr = IDirectDrawSurface4_IsLost(surface);
8780 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8782 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
8783 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8784 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8785 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8786 hr = IDirectDrawSurface4_IsLost(surface);
8787 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8789 hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
8790 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8791 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8792 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8793 hr = IDirectDrawSurface4_IsLost(surface);
8794 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8796 IDirectDrawSurface4_Release(surface);
8797 memset(&surface_desc, 0, sizeof(surface_desc));
8798 surface_desc.dwSize = sizeof(surface_desc);
8799 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
8800 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
8801 U5(surface_desc).dwBackBufferCount = 1;
8802 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8803 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8805 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
8806 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8807 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8808 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8809 hr = IDirectDrawSurface4_IsLost(surface);
8810 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8811 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8812 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8814 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL | DDSCL_FULLSCREEN);
8815 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8816 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8817 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8818 hr = IDirectDrawSurface4_IsLost(surface);
8819 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8820 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8821 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
8823 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
8824 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8825 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8826 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8827 hr = IDirectDrawSurface4_IsLost(surface);
8828 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8829 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8830 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
8832 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
8833 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8834 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8835 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8836 hr = IDirectDrawSurface4_IsLost(surface);
8837 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8838 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8839 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
8841 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL | DDSCL_FULLSCREEN);
8842 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8843 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8844 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8845 hr = IDirectDrawSurface4_IsLost(surface);
8846 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8847 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8848 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
8850 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
8851 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8852 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8853 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8854 hr = IDirectDrawSurface4_IsLost(surface);
8855 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8856 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
8857 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8859 IDirectDrawSurface4_Release(surface);
8860 refcount = IDirectDraw4_Release(ddraw);
8861 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8862 DestroyWindow(window2);
8863 DestroyWindow(window1);
8866 static void test_surface_desc_lock(void)
8868 IDirectDrawSurface4 *surface;
8869 DDSURFACEDESC2 surface_desc;
8870 IDirectDraw4 *ddraw;
8871 ULONG refcount;
8872 HWND window;
8873 HRESULT hr;
8875 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8876 0, 0, 640, 480, 0, 0, 0, 0);
8877 ddraw = create_ddraw();
8878 ok(!!ddraw, "Failed to create a ddraw object.\n");
8879 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
8880 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8882 memset(&surface_desc, 0, sizeof(surface_desc));
8883 surface_desc.dwSize = sizeof(surface_desc);
8884 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
8885 surface_desc.dwWidth = 16;
8886 surface_desc.dwHeight = 16;
8887 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
8888 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8889 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8891 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8892 surface_desc.dwSize = sizeof(surface_desc);
8893 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
8894 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8895 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8897 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8898 surface_desc.dwSize = sizeof(surface_desc);
8899 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, 0, NULL);
8900 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
8901 ok(surface_desc.lpSurface != NULL, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8902 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8903 surface_desc.dwSize = sizeof(surface_desc);
8904 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
8905 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8906 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8907 hr = IDirectDrawSurface4_Unlock(surface, NULL);
8908 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
8910 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8911 surface_desc.dwSize = sizeof(surface_desc);
8912 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
8913 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8914 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8916 IDirectDrawSurface4_Release(surface);
8917 refcount = IDirectDraw4_Release(ddraw);
8918 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8919 DestroyWindow(window);
8922 static void test_signed_formats(void)
8924 HRESULT hr;
8925 IDirect3DDevice3 *device;
8926 IDirect3D3 *d3d;
8927 IDirectDraw4 *ddraw;
8928 IDirectDrawSurface4 *surface, *rt;
8929 IDirect3DTexture2 *texture;
8930 IDirect3DViewport3 *viewport;
8931 DDSURFACEDESC2 surface_desc;
8932 ULONG refcount;
8933 HWND window;
8934 D3DCOLOR color, expected_color;
8935 D3DRECT clear_rect;
8936 static struct
8938 struct vec3 position;
8939 struct vec2 texcoord;
8941 quad[] =
8943 {{-1.0f, -1.0f, 0.0f}, {0.0f, 1.0f}},
8944 {{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
8945 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 1.0f}},
8946 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
8948 /* See test_signed_formats() in dlls/d3d9/tests/visual.c for an explanation
8949 * of these values. */
8950 static const USHORT content_v8u8[4][4] =
8952 {0x0000, 0x7f7f, 0x8880, 0x0000},
8953 {0x0080, 0x8000, 0x7f00, 0x007f},
8954 {0x193b, 0xe8c8, 0x0808, 0xf8f8},
8955 {0x4444, 0xc0c0, 0xa066, 0x22e0},
8957 static const DWORD content_x8l8v8u8[4][4] =
8959 {0x00000000, 0x00ff7f7f, 0x00008880, 0x00ff0000},
8960 {0x00000080, 0x00008000, 0x00007f00, 0x0000007f},
8961 {0x0041193b, 0x0051e8c8, 0x00040808, 0x00fff8f8},
8962 {0x00824444, 0x0000c0c0, 0x00c2a066, 0x009222e0},
8964 static const USHORT content_l6v5u5[4][4] =
8966 {0x0000, 0xfdef, 0x0230, 0xfc00},
8967 {0x0010, 0x0200, 0x01e0, 0x000f},
8968 {0x4067, 0x53b9, 0x0421, 0xffff},
8969 {0x8108, 0x0318, 0xc28c, 0x909c},
8971 static const struct
8973 const char *name;
8974 const void *content;
8975 SIZE_T pixel_size;
8976 BOOL blue;
8977 unsigned int slop, slop_broken;
8978 DDPIXELFORMAT format;
8980 formats[] =
8983 "D3DFMT_V8U8", content_v8u8, sizeof(WORD), FALSE, 1, 0,
8985 sizeof(DDPIXELFORMAT), DDPF_BUMPDUDV, 0,
8986 {16}, {0x000000ff}, {0x0000ff00}, {0x00000000}, {0x00000000}
8990 "D3DFMT_X8L8V8U8", content_x8l8v8u8, sizeof(DWORD), TRUE, 1, 0,
8992 sizeof(DDPIXELFORMAT), DDPF_BUMPDUDV | DDPF_BUMPLUMINANCE, 0,
8993 {32}, {0x000000ff}, {0x0000ff00}, {0x00ff0000}, {0x00000000}
8997 "D3DFMT_L6V5U5", content_l6v5u5, sizeof(WORD), TRUE, 4, 7,
8999 sizeof(DDPIXELFORMAT), DDPF_BUMPDUDV | DDPF_BUMPLUMINANCE, 0,
9000 {16}, {0x0000001f}, {0x000003e0}, {0x0000fc00}, {0x00000000}
9004 /* No V16U16 or Q8W8V8U8 support in ddraw. */
9006 static const D3DCOLOR expected_colors[4][4] =
9008 {0x00808080, 0x00fefeff, 0x00010780, 0x008080ff},
9009 {0x00018080, 0x00800180, 0x0080fe80, 0x00fe8080},
9010 {0x00ba98a0, 0x004767a8, 0x00888881, 0x007878ff},
9011 {0x00c3c3c0, 0x003f3f80, 0x00e51fe1, 0x005fa2c8},
9013 unsigned int i, width, x, y;
9014 D3DDEVICEDESC device_desc, hel_desc;
9016 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
9017 0, 0, 640, 480, 0, 0, 0, 0);
9019 if (!(device = create_device(window, DDSCL_NORMAL)))
9021 skip("Failed to create a 3D device, skipping test.\n");
9022 DestroyWindow(window);
9023 return;
9026 memset(&device_desc, 0, sizeof(device_desc));
9027 device_desc.dwSize = sizeof(device_desc);
9028 memset(&hel_desc, 0, sizeof(hel_desc));
9029 hel_desc.dwSize = sizeof(hel_desc);
9030 hr = IDirect3DDevice3_GetCaps(device, &device_desc, &hel_desc);
9031 ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
9032 if (!(device_desc.dwTextureOpCaps & D3DTEXOPCAPS_BLENDFACTORALPHA))
9034 skip("D3DTOP_BLENDFACTORALPHA not supported, skipping bumpmap format tests.\n");
9035 goto done;
9038 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
9039 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
9040 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
9041 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
9042 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
9043 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
9045 memset(&surface_desc, 0, sizeof(surface_desc));
9046 surface_desc.dwSize = sizeof(surface_desc);
9047 hr = IDirectDrawSurface4_GetSurfaceDesc(rt, &surface_desc);
9048 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
9049 viewport = create_viewport(device, 0, 0, surface_desc.dwWidth, surface_desc.dwHeight);
9050 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
9051 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
9052 U1(clear_rect).x1 = 0;
9053 U2(clear_rect).y1 = 0;
9054 U3(clear_rect).x2 = surface_desc.dwWidth;
9055 U4(clear_rect).y2 = surface_desc.dwHeight;
9057 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
9058 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
9060 /* dst = tex * 0.5 + 1.0 * (1.0 - 0.5) = tex * 0.5 + 0.5 */
9061 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_TEXTUREFACTOR, 0x80ffffff);
9062 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
9063 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_BLENDFACTORALPHA);
9064 ok(SUCCEEDED(hr), "Failed to set texture stage state, hr %#x.\n", hr);
9065 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
9066 ok(SUCCEEDED(hr), "Failed to set texture stage state, hr %#x.\n", hr);
9067 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
9068 ok(SUCCEEDED(hr), "Failed to set texture stage state, hr %#x.\n", hr);
9070 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
9072 for (width = 1; width < 5; width += 3)
9074 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0x00000000, 0.0f, 0);
9075 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
9077 memset(&surface_desc, 0, sizeof(surface_desc));
9078 surface_desc.dwSize = sizeof(surface_desc);
9079 surface_desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
9080 surface_desc.dwWidth = width;
9081 surface_desc.dwHeight = 4;
9082 U4(surface_desc).ddpfPixelFormat = formats[i].format;
9083 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
9084 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9085 if (FAILED(hr))
9087 skip("%s textures not supported, skipping.\n", formats[i].name);
9088 continue;
9090 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, format %s.\n", hr, formats[i].name);
9092 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
9093 ok(SUCCEEDED(hr), "Failed to get Direct3DTexture2 interface, hr %#x, format %s.\n",
9094 hr, formats[i].name);
9095 hr = IDirect3DDevice3_SetTexture(device, 0, texture);
9096 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x, format %s.\n", hr, formats[i].name);
9097 IDirect3DTexture2_Release(texture);
9099 memset(&surface_desc, 0, sizeof(surface_desc));
9100 surface_desc.dwSize = sizeof(surface_desc);
9101 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, 0, NULL);
9102 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, format %s.\n", hr, formats[i].name);
9103 for (y = 0; y < 4; y++)
9105 memcpy((char *)surface_desc.lpSurface + y * U1(surface_desc).lPitch,
9106 (char *)formats[i].content + y * 4 * formats[i].pixel_size,
9107 width * formats[i].pixel_size);
9109 hr = IDirectDrawSurface4_Unlock(surface, NULL);
9110 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, format %s.\n", hr, formats[i].name);
9112 hr = IDirect3DDevice3_BeginScene(device);
9113 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
9114 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP,
9115 D3DFVF_XYZ | D3DFVF_TEX1, quad, 4, 0);
9116 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
9117 hr = IDirect3DDevice3_EndScene(device);
9118 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
9120 for (y = 0; y < 4; y++)
9122 for (x = 0; x < width; x++)
9124 expected_color = expected_colors[y][x];
9125 if (!formats[i].blue)
9126 expected_color |= 0x000000ff;
9128 color = get_surface_color(rt, 80 + 160 * x, 60 + 120 * y);
9129 ok(compare_color(color, expected_color, formats[i].slop)
9130 || broken(compare_color(color, expected_color, formats[i].slop_broken)),
9131 "Expected color 0x%08x, got 0x%08x, format %s, location %ux%u.\n",
9132 expected_color, color, formats[i].name, x, y);
9136 IDirectDrawSurface4_Release(surface);
9140 destroy_viewport(device, viewport);
9141 IDirectDrawSurface4_Release(rt);
9142 IDirectDraw4_Release(ddraw);
9143 IDirect3D3_Release(d3d);
9145 done:
9146 refcount = IDirect3DDevice3_Release(device);
9147 ok(!refcount, "Device has %u references left.\n", refcount);
9148 DestroyWindow(window);
9151 static void test_color_fill(void)
9153 HRESULT hr;
9154 IDirect3DDevice3 *device;
9155 IDirect3D3 *d3d;
9156 IDirectDraw4 *ddraw;
9157 IDirectDrawSurface4 *surface, *surface2;
9158 DDSURFACEDESC2 surface_desc;
9159 DDPIXELFORMAT z_fmt;
9160 ULONG refcount;
9161 HWND window;
9162 unsigned int i;
9163 DDBLTFX fx;
9164 RECT rect = {5, 5, 7, 7};
9165 DWORD *color;
9166 DWORD supported_fmts = 0, num_fourcc_codes, *fourcc_codes;
9167 DDCAPS hal_caps;
9168 static const struct
9170 DWORD caps, caps2;
9171 HRESULT colorfill_hr, depthfill_hr;
9172 BOOL rop_success;
9173 const char *name;
9174 DWORD result;
9175 BOOL check_result;
9176 DDPIXELFORMAT format;
9178 tests[] =
9181 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0,
9182 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem offscreenplain RGB", 0xdeadbeef, TRUE,
9184 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9185 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
9189 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, 0,
9190 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem offscreenplain RGB", 0xdeadbeef, TRUE,
9192 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9193 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
9197 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, 0,
9198 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem texture RGB", 0xdeadbeef, TRUE,
9200 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9201 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
9205 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0,
9206 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem texture RGB", 0xdeadbeef, TRUE,
9208 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9209 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
9213 DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE,
9214 DD_OK, DDERR_INVALIDPARAMS, TRUE, "managed texture RGB", 0xdeadbeef, TRUE,
9216 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9217 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
9221 DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY, 0,
9222 DDERR_INVALIDPARAMS, DD_OK, TRUE, "vidmem zbuffer", 0, FALSE,
9223 {0, 0, 0, {0}, {0}, {0}, {0}, {0}}
9226 DDSCAPS_ZBUFFER | DDSCAPS_SYSTEMMEMORY, 0,
9227 DDERR_INVALIDPARAMS, DD_OK, TRUE, "sysmem zbuffer", 0, FALSE,
9228 {0, 0, 0, {0}, {0}, {0}, {0}, {0}}
9231 /* Colorfill on YUV surfaces always returns DD_OK, but the content is
9232 * different afterwards. DX9+ GPUs set one of the two luminance values
9233 * in each block, but AMD and Nvidia GPUs disagree on which luminance
9234 * value they set. r200 (dx8) just sets the entire block to the clear
9235 * value. */
9236 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0,
9237 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem offscreenplain YUY2", 0, FALSE,
9239 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'),
9240 {0}, {0}, {0}, {0}, {0}
9244 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0,
9245 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem offscreenplain UYVY", 0, FALSE,
9247 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'),
9248 {0}, {0}, {0}, {0}, {0}
9252 DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY, 0,
9253 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem overlay YUY2", 0, FALSE,
9255 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'),
9256 {0}, {0}, {0}, {0}, {0}
9260 DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY, 0,
9261 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem overlay UYVY", 0, FALSE,
9263 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'),
9264 {0}, {0}, {0}, {0}, {0}
9268 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, 0,
9269 E_NOTIMPL, DDERR_INVALIDPARAMS, FALSE, "vidmem texture DXT1", 0, FALSE,
9271 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '1'),
9272 {0}, {0}, {0}, {0}, {0}
9276 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0,
9277 E_NOTIMPL, DDERR_INVALIDPARAMS, FALSE, "sysmem texture DXT1", 0, FALSE,
9279 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '1'),
9280 {0}, {0}, {0}, {0}, {0}
9284 /* The testbot fills this with 0x00 instead of the blue channel. The sysmem
9285 * surface works, presumably because it is handled by the runtime instead of
9286 * the driver. */
9287 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0,
9288 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem offscreenplain P8", 0xefefefef, FALSE,
9290 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_PALETTEINDEXED8, 0,
9291 {8}, {0}, {0}, {0}, {0}
9295 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, 0,
9296 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem offscreenplain P8", 0xefefefef, TRUE,
9298 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_PALETTEINDEXED8, 0,
9299 {8}, {0}, {0}, {0}, {0}
9303 static const struct
9305 DWORD rop;
9306 const char *name;
9307 HRESULT hr;
9309 rops[] =
9311 {SRCCOPY, "SRCCOPY", DD_OK},
9312 {SRCPAINT, "SRCPAINT", DDERR_NORASTEROPHW},
9313 {SRCAND, "SRCAND", DDERR_NORASTEROPHW},
9314 {SRCINVERT, "SRCINVERT", DDERR_NORASTEROPHW},
9315 {SRCERASE, "SRCERASE", DDERR_NORASTEROPHW},
9316 {NOTSRCCOPY, "NOTSRCCOPY", DDERR_NORASTEROPHW},
9317 {NOTSRCERASE, "NOTSRCERASE", DDERR_NORASTEROPHW},
9318 {MERGECOPY, "MERGECOPY", DDERR_NORASTEROPHW},
9319 {MERGEPAINT, "MERGEPAINT", DDERR_NORASTEROPHW},
9320 {PATCOPY, "PATCOPY", DDERR_NORASTEROPHW},
9321 {PATPAINT, "PATPAINT", DDERR_NORASTEROPHW},
9322 {PATINVERT, "PATINVERT", DDERR_NORASTEROPHW},
9323 {DSTINVERT, "DSTINVERT", DDERR_NORASTEROPHW},
9324 {BLACKNESS, "BLACKNESS", DD_OK},
9325 {WHITENESS, "WHITENESS", DD_OK},
9326 {0xaa0029, "0xaa0029", DDERR_NORASTEROPHW} /* noop */
9329 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
9330 0, 0, 640, 480, 0, 0, 0, 0);
9332 if (!(device = create_device(window, DDSCL_NORMAL)))
9334 skip("Failed to create a 3D device, skipping test.\n");
9335 DestroyWindow(window);
9336 return;
9339 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
9340 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
9341 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
9342 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
9344 memset(&z_fmt, 0, sizeof(z_fmt));
9345 IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
9346 if (!z_fmt.dwSize)
9347 skip("No Z buffer formats supported, skipping Z buffer colorfill test.\n");
9349 IDirect3DDevice3_EnumTextureFormats(device, test_block_formats_creation_cb, &supported_fmts);
9350 if (!(supported_fmts & SUPPORT_DXT1))
9351 skip("DXT1 textures not supported, skipping DXT1 colorfill test.\n");
9353 IDirect3D3_Release(d3d);
9355 hr = IDirectDraw4_GetFourCCCodes(ddraw, &num_fourcc_codes, NULL);
9356 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
9357 fourcc_codes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
9358 num_fourcc_codes * sizeof(*fourcc_codes));
9359 if (!fourcc_codes)
9360 goto done;
9361 hr = IDirectDraw4_GetFourCCCodes(ddraw, &num_fourcc_codes, fourcc_codes);
9362 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
9363 for (i = 0; i < num_fourcc_codes; i++)
9365 if (fourcc_codes[i] == MAKEFOURCC('Y', 'U', 'Y', '2'))
9366 supported_fmts |= SUPPORT_YUY2;
9367 else if (fourcc_codes[i] == MAKEFOURCC('U', 'Y', 'V', 'Y'))
9368 supported_fmts |= SUPPORT_UYVY;
9370 HeapFree(GetProcessHeap(), 0, fourcc_codes);
9372 memset(&hal_caps, 0, sizeof(hal_caps));
9373 hal_caps.dwSize = sizeof(hal_caps);
9374 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
9375 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
9377 if (!(supported_fmts & (SUPPORT_YUY2 | SUPPORT_UYVY)) || !(hal_caps.dwCaps & DDCAPS_OVERLAY))
9378 skip("Overlays or some YUV formats not supported, skipping YUV colorfill tests.\n");
9380 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
9382 /* Some Windows drivers modify dwFillColor when it is used on P8 or FourCC formats. */
9383 memset(&fx, 0, sizeof(fx));
9384 fx.dwSize = sizeof(fx);
9385 U5(fx).dwFillColor = 0xdeadbeef;
9387 memset(&surface_desc, 0, sizeof(surface_desc));
9388 surface_desc.dwSize = sizeof(surface_desc);
9389 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9390 surface_desc.dwWidth = 64;
9391 surface_desc.dwHeight = 64;
9392 U4(surface_desc).ddpfPixelFormat = tests[i].format;
9393 surface_desc.ddsCaps.dwCaps = tests[i].caps;
9394 surface_desc.ddsCaps.dwCaps2 = tests[i].caps2;
9396 if (tests[i].format.dwFourCC == MAKEFOURCC('D','X','T','1') && !(supported_fmts & SUPPORT_DXT1))
9397 continue;
9398 if (tests[i].format.dwFourCC == MAKEFOURCC('Y','U','Y','2') && !(supported_fmts & SUPPORT_YUY2))
9399 continue;
9400 if (tests[i].format.dwFourCC == MAKEFOURCC('U','Y','V','Y') && !(supported_fmts & SUPPORT_UYVY))
9401 continue;
9402 if (tests[i].caps & DDSCAPS_OVERLAY && !(hal_caps.dwCaps & DDCAPS_OVERLAY))
9403 continue;
9405 if (tests[i].caps & DDSCAPS_ZBUFFER)
9407 if (!z_fmt.dwSize)
9408 continue;
9410 U4(surface_desc).ddpfPixelFormat = z_fmt;
9413 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9414 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, surface %s.\n", hr, tests[i].name);
9416 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9417 todo_wine_if (tests[i].format.dwFourCC)
9418 ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
9419 hr, tests[i].colorfill_hr, tests[i].name);
9421 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9422 todo_wine_if (tests[i].format.dwFourCC)
9423 ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
9424 hr, tests[i].colorfill_hr, tests[i].name);
9426 if (SUCCEEDED(hr) && tests[i].check_result)
9428 memset(&surface_desc, 0, sizeof(surface_desc));
9429 surface_desc.dwSize = sizeof(surface_desc);
9430 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
9431 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9432 color = surface_desc.lpSurface;
9433 ok(*color == tests[i].result, "Got clear result 0x%08x, expected 0x%08x, surface %s.\n",
9434 *color, tests[i].result, tests[i].name);
9435 hr = IDirectDrawSurface4_Unlock(surface, NULL);
9436 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9439 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9440 ok(hr == tests[i].depthfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
9441 hr, tests[i].depthfill_hr, tests[i].name);
9442 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9443 ok(hr == tests[i].depthfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
9444 hr, tests[i].depthfill_hr, tests[i].name);
9446 U5(fx).dwFillColor = 0xdeadbeef;
9447 fx.dwROP = BLACKNESS;
9448 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
9449 ok(FAILED(hr) == !tests[i].rop_success, "Blt returned %#x, expected %s, surface %s.\n",
9450 hr, tests[i].rop_success ? "success" : "failure", tests[i].name);
9451 ok(U5(fx).dwFillColor == 0xdeadbeef, "dwFillColor was set to 0x%08x, surface %s\n",
9452 U5(fx).dwFillColor, tests[i].name);
9454 if (SUCCEEDED(hr) && tests[i].check_result)
9456 memset(&surface_desc, 0, sizeof(surface_desc));
9457 surface_desc.dwSize = sizeof(surface_desc);
9458 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
9459 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9460 color = surface_desc.lpSurface;
9461 ok(*color == 0, "Got clear result 0x%08x, expected 0x00000000, surface %s.\n",
9462 *color, tests[i].name);
9463 hr = IDirectDrawSurface4_Unlock(surface, NULL);
9464 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9467 fx.dwROP = WHITENESS;
9468 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
9469 ok(FAILED(hr) == !tests[i].rop_success, "Blt returned %#x, expected %s, surface %s.\n",
9470 hr, tests[i].rop_success ? "success" : "failure", tests[i].name);
9471 ok(U5(fx).dwFillColor == 0xdeadbeef, "dwFillColor was set to 0x%08x, surface %s\n",
9472 U5(fx).dwFillColor, tests[i].name);
9474 if (SUCCEEDED(hr) && tests[i].check_result)
9476 memset(&surface_desc, 0, sizeof(surface_desc));
9477 surface_desc.dwSize = sizeof(surface_desc);
9478 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
9479 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9480 color = surface_desc.lpSurface;
9481 /* WHITENESS sets the alpha channel to 0x00. Ignore this for now. */
9482 ok((*color & 0x00ffffff) == 0x00ffffff, "Got clear result 0x%08x, expected 0xffffffff, surface %s.\n",
9483 *color, tests[i].name);
9484 hr = IDirectDrawSurface4_Unlock(surface, NULL);
9485 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
9488 IDirectDrawSurface4_Release(surface);
9491 memset(&fx, 0, sizeof(fx));
9492 fx.dwSize = sizeof(fx);
9493 U5(fx).dwFillColor = 0xdeadbeef;
9494 fx.dwROP = WHITENESS;
9496 memset(&surface_desc, 0, sizeof(surface_desc));
9497 surface_desc.dwSize = sizeof(surface_desc);
9498 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9499 surface_desc.dwWidth = 64;
9500 surface_desc.dwHeight = 64;
9501 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
9502 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
9503 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
9504 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
9505 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
9506 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
9507 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
9508 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9509 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9510 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
9511 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9513 /* No DDBLTFX. */
9514 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, NULL);
9515 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9516 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_ROP | DDBLT_WAIT, NULL);
9517 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9519 /* Unused source rectangle. */
9520 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9521 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9522 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
9523 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9525 /* Unused source surface. */
9526 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9527 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9528 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
9529 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9530 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9531 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9532 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
9533 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9535 /* Inverted destination or source rectangle. */
9536 SetRect(&rect, 5, 7, 7, 5);
9537 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9538 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9539 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9540 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9541 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9542 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9543 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9544 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9545 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
9546 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9548 /* Negative rectangle. */
9549 SetRect(&rect, -1, -1, 5, 5);
9550 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9551 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9552 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9553 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9554 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9555 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9556 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9557 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9558 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
9559 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9561 /* Out of bounds rectangle. */
9562 SetRect(&rect, 0, 0, 65, 65);
9563 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
9564 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9565 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
9566 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9568 /* Combine multiple flags. */
9569 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9570 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9571 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_ROP | DDBLT_WAIT, &fx);
9572 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9573 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_ROP | DDBLT_WAIT, &fx);
9574 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9576 for (i = 0; i < sizeof(rops) / sizeof(*rops); i++)
9578 fx.dwROP = rops[i].rop;
9579 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
9580 ok(hr == rops[i].hr, "Got unexpected hr %#x for rop %s.\n", hr, rops[i].name);
9583 IDirectDrawSurface4_Release(surface2);
9584 IDirectDrawSurface4_Release(surface);
9586 if (!z_fmt.dwSize)
9587 goto done;
9589 memset(&surface_desc, 0, sizeof(surface_desc));
9590 surface_desc.dwSize = sizeof(surface_desc);
9591 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9592 surface_desc.dwWidth = 64;
9593 surface_desc.dwHeight = 64;
9594 U4(surface_desc).ddpfPixelFormat = z_fmt;
9595 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
9596 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
9597 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9598 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
9599 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9601 /* No DDBLTFX. */
9602 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, NULL);
9603 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9605 /* Unused source rectangle. */
9606 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9607 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9609 /* Unused source surface. */
9610 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9611 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9612 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9613 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9615 /* Inverted destination or source rectangle. */
9616 SetRect(&rect, 5, 7, 7, 5);
9617 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9618 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9619 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9620 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9621 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9622 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9623 hr = IDirectDrawSurface4_Blt(surface, NULL, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9624 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9626 /* Negative rectangle. */
9627 SetRect(&rect, -1, -1, 5, 5);
9628 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9629 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9630 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9631 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
9632 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9633 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9634 hr = IDirectDrawSurface4_Blt(surface, &rect, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9635 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9637 /* Out of bounds rectangle. */
9638 SetRect(&rect, 0, 0, 65, 65);
9639 hr = IDirectDrawSurface4_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9640 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
9642 /* Combine multiple flags. */
9643 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
9644 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
9646 IDirectDrawSurface4_Release(surface2);
9647 IDirectDrawSurface4_Release(surface);
9649 done:
9650 IDirectDraw4_Release(ddraw);
9651 refcount = IDirect3DDevice3_Release(device);
9652 ok(!refcount, "Device has %u references left.\n", refcount);
9653 DestroyWindow(window);
9656 static void test_texcoordindex(void)
9658 static struct
9660 struct vec3 pos;
9661 struct vec2 texcoord1;
9662 struct vec2 texcoord2;
9663 struct vec2 texcoord3;
9665 quad[] =
9667 {{-1.0f, -1.0f, 0.0f}, {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f}},
9668 {{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 1.0f}, {1.0f, 0.0f}},
9669 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 1.0f}, {1.0f, 0.0f}, {0.0f, 1.0f}},
9670 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 0.0f}},
9672 static const DWORD fvf = D3DFVF_XYZ | D3DFVF_TEX3;
9673 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
9674 IDirect3DDevice3 *device;
9675 IDirect3D3 *d3d;
9676 IDirectDraw4 *ddraw;
9677 IDirectDrawSurface4 *rt;
9678 IDirect3DViewport3 *viewport;
9679 HWND window;
9680 HRESULT hr;
9681 IDirectDrawSurface4 *surface1, *surface2;
9682 IDirect3DTexture2 *texture1, *texture2;
9683 DDSURFACEDESC2 surface_desc;
9684 ULONG refcount;
9685 D3DCOLOR color;
9686 DWORD *ptr;
9688 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
9689 0, 0, 640, 480, 0, 0, 0, 0);
9690 if (!(device = create_device(window, DDSCL_NORMAL)))
9692 skip("Failed to create a 3D device, skipping test.\n");
9693 DestroyWindow(window);
9694 return;
9697 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
9698 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
9699 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
9700 ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
9701 IDirect3D3_Release(d3d);
9703 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
9704 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
9706 memset(&surface_desc, 0, sizeof(surface_desc));
9707 surface_desc.dwSize = sizeof(surface_desc);
9708 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9709 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
9710 surface_desc.dwWidth = 2;
9711 surface_desc.dwHeight = 2;
9712 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
9713 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
9714 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
9715 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
9716 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
9717 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
9718 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
9719 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
9720 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9721 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
9722 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9724 memset(&surface_desc, 0, sizeof(surface_desc));
9725 surface_desc.dwSize = sizeof(surface_desc);
9726 hr = IDirectDrawSurface4_Lock(surface1, 0, &surface_desc, 0, NULL);
9727 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
9728 ptr = surface_desc.lpSurface;
9729 ptr[0] = 0xff000000;
9730 ptr[1] = 0xff00ff00;
9731 ptr += surface_desc.lPitch / sizeof(*ptr);
9732 ptr[0] = 0xff0000ff;
9733 ptr[1] = 0xff00ffff;
9734 hr = IDirectDrawSurface4_Unlock(surface1, NULL);
9735 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
9737 memset(&surface_desc, 0, sizeof(surface_desc));
9738 surface_desc.dwSize = sizeof(surface_desc);
9739 hr = IDirectDrawSurface4_Lock(surface2, 0, &surface_desc, 0, NULL);
9740 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
9741 ptr = surface_desc.lpSurface;
9742 ptr[0] = 0xff000000;
9743 ptr[1] = 0xff0000ff;
9744 ptr += surface_desc.lPitch / sizeof(*ptr);
9745 ptr[0] = 0xffff0000;
9746 ptr[1] = 0xffff00ff;
9747 hr = IDirectDrawSurface4_Unlock(surface2, 0);
9748 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
9750 viewport = create_viewport(device, 0, 0, 640, 480);
9751 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
9752 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
9754 hr = IDirectDrawSurface4_QueryInterface(surface1, &IID_IDirect3DTexture2, (void **)&texture1);
9755 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
9756 hr = IDirectDrawSurface4_QueryInterface(surface2, &IID_IDirect3DTexture2, (void **)&texture2);
9757 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
9758 hr = IDirect3DDevice3_SetTexture(device, 0, texture1);
9759 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
9760 hr = IDirect3DDevice3_SetTexture(device, 1, texture2);
9761 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
9762 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
9763 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
9764 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
9765 ok(SUCCEEDED(hr), "Failed to set color op, hr %#x.\n", hr);
9766 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
9767 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
9768 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_COLOROP, D3DTOP_ADD);
9769 ok(SUCCEEDED(hr), "Failed to set color op, hr %#x.\n", hr);
9770 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
9771 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
9772 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_COLORARG2, D3DTA_CURRENT);
9773 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
9774 hr = IDirect3DDevice3_SetTextureStageState(device, 2, D3DTSS_COLOROP, D3DTOP_DISABLE);
9775 ok(SUCCEEDED(hr), "Failed to set color op, hr %#x.\n", hr);
9777 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_TEXCOORDINDEX, 1);
9778 ok(SUCCEEDED(hr), "Failed to set texcoord index, hr %#x.\n", hr);
9779 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_TEXCOORDINDEX, 0);
9780 ok(SUCCEEDED(hr), "Failed to set texcoord index, hr %#x.\n", hr);
9782 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
9783 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
9785 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffff00, 1.0f, 0);
9786 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
9788 hr = IDirect3DDevice3_BeginScene(device);
9789 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
9790 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, fvf, quad, 4, 0);
9791 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
9792 hr = IDirect3DDevice3_EndScene(device);
9793 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
9795 color = get_surface_color(rt, 160, 120);
9796 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
9797 color = get_surface_color(rt, 480, 120);
9798 ok(compare_color(color, 0x0000ffff, 2), "Got unexpected color 0x%08x.\n", color);
9799 color = get_surface_color(rt, 160, 360);
9800 ok(compare_color(color, 0x00ff0000, 2), "Got unexpected color 0x%08x.\n", color);
9801 color = get_surface_color(rt, 480, 360);
9802 ok(compare_color(color, 0x00ffffff, 2), "Got unexpected color 0x%08x.\n", color);
9804 /* D3DTSS_TEXTURETRANSFORMFLAGS was introduced in D3D7, can't test it here. */
9806 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_TEXCOORDINDEX, 2);
9807 ok(SUCCEEDED(hr), "Failed to set texcoord index, hr %#x.\n", hr);
9809 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffff00, 1.0f, 0);
9810 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
9812 hr = IDirect3DDevice3_BeginScene(device);
9813 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
9814 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, fvf, quad, 4, 0);
9815 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
9816 hr = IDirect3DDevice3_EndScene(device);
9817 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
9819 color = get_surface_color(rt, 160, 120);
9820 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
9821 color = get_surface_color(rt, 480, 120);
9822 ok(compare_color(color, 0x0000ffff, 2), "Got unexpected color 0x%08x.\n", color);
9823 color = get_surface_color(rt, 160, 360);
9824 ok(compare_color(color, 0x00ff00ff, 2), "Got unexpected color 0x%08x.\n", color);
9825 color = get_surface_color(rt, 480, 360);
9826 ok(compare_color(color, 0x00ffff00, 2), "Got unexpected color 0x%08x.\n", color);
9828 IDirect3DTexture2_Release(texture2);
9829 IDirect3DTexture2_Release(texture1);
9830 IDirectDrawSurface4_Release(surface2);
9831 IDirectDrawSurface4_Release(surface1);
9833 destroy_viewport(device, viewport);
9835 IDirectDrawSurface4_Release(rt);
9836 IDirectDraw_Release(ddraw);
9837 refcount = IDirect3DDevice3_Release(device);
9838 ok(!refcount, "Device has %u references left.\n", refcount);
9839 DestroyWindow(window);
9842 static void test_colorkey_precision(void)
9844 static struct
9846 struct vec3 pos;
9847 struct vec2 texcoord;
9849 quad[] =
9851 {{-1.0f, -1.0f, 0.0f}, {0.0f, 1.0f}},
9852 {{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
9853 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 1.0f}},
9854 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
9856 IDirect3DDevice3 *device;
9857 IDirect3D3 *d3d;
9858 IDirectDraw4 *ddraw;
9859 IDirectDrawSurface4 *rt;
9860 IDirect3DViewport3 *viewport;
9861 HWND window;
9862 HRESULT hr;
9863 IDirectDrawSurface4 *src, *dst, *texture;
9864 IDirect3DTexture2 *d3d_texture;
9865 DDSURFACEDESC2 surface_desc, lock_desc;
9866 ULONG refcount;
9867 D3DCOLOR color;
9868 unsigned int t, c;
9869 DDCOLORKEY ckey;
9870 DDBLTFX fx;
9871 DWORD data[4] = {0}, color_mask;
9872 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
9873 D3DDEVICEDESC device_desc, hel_desc;
9874 BOOL warp;
9875 static const struct
9877 unsigned int max, shift, bpp, clear;
9878 const char *name;
9879 DDPIXELFORMAT fmt;
9881 tests[] =
9884 255, 0, 4, 0x00345678, "D3DFMT_X8R8G8B8",
9886 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
9887 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0x00000000}
9892 63, 5, 2, 0x5678, "D3DFMT_R5G6B5, G channel",
9894 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
9895 {16}, {0xf800}, {0x07e0}, {0x001f}, {0x0000}
9900 31, 0, 2, 0x5678, "D3DFMT_R5G6B5, B channel",
9902 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
9903 {16}, {0xf800}, {0x07e0}, {0x001f}, {0x0000}
9908 15, 0, 2, 0x0678, "D3DFMT_A4R4G4B4",
9910 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
9911 {16}, {0x0f00}, {0x00f0}, {0x000f}, {0xf000}
9917 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
9918 0, 0, 640, 480, 0, 0, 0, 0);
9919 if (!(device = create_device(window, DDSCL_NORMAL)))
9921 skip("Failed to create a 3D device, skipping test.\n");
9922 DestroyWindow(window);
9923 return;
9926 /* The Windows 8 WARP driver has plenty of false negatives in X8R8G8B8
9927 * (color key doesn't match although the values are equal), and a false
9928 * positive when the color key is 0 and the texture contains the value 1.
9929 * I don't want to mark this broken unconditionally since this would
9930 * essentially disable the test on Windows. Try to detect WARP (and I
9931 * guess mismatch other SW renderers) by its ability to texture from
9932 * system memory. Also on random occasions 254 == 255 and 255 != 255.*/
9933 memset(&device_desc, 0, sizeof(device_desc));
9934 device_desc.dwSize = sizeof(device_desc);
9935 memset(&hel_desc, 0, sizeof(hel_desc));
9936 hel_desc.dwSize = sizeof(hel_desc);
9937 hr = IDirect3DDevice3_GetCaps(device, &device_desc, &hel_desc);
9938 ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
9939 warp = !!(device_desc.dwDevCaps & D3DDEVCAPS_TEXTURESYSTEMMEMORY);
9941 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
9942 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
9943 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
9944 ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
9945 IDirect3D3_Release(d3d);
9946 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
9947 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
9949 viewport = create_viewport(device, 0, 0, 640, 480);
9950 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
9951 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
9953 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
9954 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
9955 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
9956 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
9957 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
9958 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
9959 /* Multiply the texture read result with 0, that way the result color if the key doesn't
9960 * match is constant. In theory color keying works without reading the texture result
9961 * (meaning we could just op=arg1, arg1=tfactor), but the Geforce7 Windows driver begs
9962 * to differ. */
9963 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
9964 ok(SUCCEEDED(hr), "Failed to set color op, hr %#x.\n", hr);
9965 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
9966 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
9967 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
9968 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
9969 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_TEXTUREFACTOR, 0x00000000);
9970 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
9972 memset(&fx, 0, sizeof(fx));
9973 fx.dwSize = sizeof(fx);
9974 memset(&lock_desc, 0, sizeof(lock_desc));
9975 lock_desc.dwSize = sizeof(lock_desc);
9977 for (t = 0; t < sizeof(tests) / sizeof(*tests); ++t)
9979 memset(&surface_desc, 0, sizeof(surface_desc));
9980 surface_desc.dwSize = sizeof(surface_desc);
9981 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
9982 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
9983 surface_desc.dwWidth = 4;
9984 surface_desc.dwHeight = 1;
9985 U4(surface_desc).ddpfPixelFormat = tests[t].fmt;
9986 /* Windows XP (at least with the r200 driver, other drivers untested) produces
9987 * garbage when doing color keyed texture->texture blits. */
9988 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &src, NULL);
9989 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9990 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &dst, NULL);
9991 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
9993 fx.dwFillColor = tests[t].clear;
9994 /* On the w8 testbot (WARP driver) the blit result has different values in the
9995 * X channel. */
9996 color_mask = U2(tests[t].fmt).dwRBitMask
9997 | U3(tests[t].fmt).dwGBitMask
9998 | U4(tests[t].fmt).dwBBitMask;
10000 for (c = 0; c <= tests[t].max; ++c)
10002 /* The idiotic Nvidia Windows driver can't change the color key on a d3d
10003 * texture after it has been set once... */
10004 surface_desc.dwFlags |= DDSD_CKSRCBLT;
10005 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
10006 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = c << tests[t].shift;
10007 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = c << tests[t].shift;
10008 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &texture, NULL);
10009 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
10010 hr = IDirectDrawSurface4_QueryInterface(texture, &IID_IDirect3DTexture2, (void **)&d3d_texture);
10011 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
10012 hr = IDirect3DDevice3_SetTexture(device, 0, d3d_texture);
10013 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
10015 hr = IDirectDrawSurface4_Blt(dst, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
10016 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
10018 hr = IDirectDrawSurface4_Lock(src, NULL, &lock_desc, DDLOCK_WAIT, NULL);
10019 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
10020 switch (tests[t].bpp)
10022 case 4:
10023 ((DWORD *)lock_desc.lpSurface)[0] = (c ? c - 1 : 0) << tests[t].shift;
10024 ((DWORD *)lock_desc.lpSurface)[1] = c << tests[t].shift;
10025 ((DWORD *)lock_desc.lpSurface)[2] = min(c + 1, tests[t].max) << tests[t].shift;
10026 ((DWORD *)lock_desc.lpSurface)[3] = 0xffffffff;
10027 break;
10029 case 2:
10030 ((WORD *)lock_desc.lpSurface)[0] = (c ? c - 1 : 0) << tests[t].shift;
10031 ((WORD *)lock_desc.lpSurface)[1] = c << tests[t].shift;
10032 ((WORD *)lock_desc.lpSurface)[2] = min(c + 1, tests[t].max) << tests[t].shift;
10033 ((WORD *)lock_desc.lpSurface)[3] = 0xffff;
10034 break;
10036 hr = IDirectDrawSurface4_Unlock(src, 0);
10037 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
10038 hr = IDirectDrawSurface4_Blt(texture, NULL, src, NULL, DDBLT_WAIT, NULL);
10039 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
10041 ckey.dwColorSpaceLowValue = c << tests[t].shift;
10042 ckey.dwColorSpaceHighValue = c << tests[t].shift;
10043 hr = IDirectDrawSurface4_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
10044 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
10046 hr = IDirectDrawSurface4_Blt(dst, NULL, src, NULL, DDBLT_KEYSRC | DDBLT_WAIT, NULL);
10047 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
10049 /* Don't make this read only, it somehow breaks the detection of the Nvidia bug below. */
10050 hr = IDirectDrawSurface4_Lock(dst, NULL, &lock_desc, DDLOCK_WAIT, NULL);
10051 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
10052 switch (tests[t].bpp)
10054 case 4:
10055 data[0] = ((DWORD *)lock_desc.lpSurface)[0] & color_mask;
10056 data[1] = ((DWORD *)lock_desc.lpSurface)[1] & color_mask;
10057 data[2] = ((DWORD *)lock_desc.lpSurface)[2] & color_mask;
10058 data[3] = ((DWORD *)lock_desc.lpSurface)[3] & color_mask;
10059 break;
10061 case 2:
10062 data[0] = ((WORD *)lock_desc.lpSurface)[0] & color_mask;
10063 data[1] = ((WORD *)lock_desc.lpSurface)[1] & color_mask;
10064 data[2] = ((WORD *)lock_desc.lpSurface)[2] & color_mask;
10065 data[3] = ((WORD *)lock_desc.lpSurface)[3] & color_mask;
10066 break;
10068 hr = IDirectDrawSurface4_Unlock(dst, 0);
10069 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
10071 if (!c)
10073 ok(data[0] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
10074 tests[t].clear, data[0], tests[t].name, c);
10076 if (data[3] == tests[t].clear)
10078 /* My Geforce GTX 460 on Windows 7 misbehaves when A4R4G4B4 is blitted with color
10079 * keying: The blit takes ~0.5 seconds, and subsequent color keying draws are broken,
10080 * even when a different surface is used. The blit itself doesn't draw anything,
10081 * so we can detect the bug by looking at the otherwise unused 4th texel. It should
10082 * never be masked out by the key.
10084 * Also appears to affect the testbot in some way with R5G6B5. Color keying is
10085 * terrible on WARP. */
10086 skip("Nvidia A4R4G4B4 color keying blit bug detected, skipping.\n");
10087 IDirect3DTexture2_Release(d3d_texture);
10088 IDirectDrawSurface4_Release(texture);
10089 IDirectDrawSurface4_Release(src);
10090 IDirectDrawSurface4_Release(dst);
10091 goto done;
10094 else
10095 ok(data[0] == (c - 1) << tests[t].shift, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
10096 (c - 1) << tests[t].shift, data[0], tests[t].name, c);
10098 ok(data[1] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
10099 tests[t].clear, data[1], tests[t].name, c);
10101 if (c == tests[t].max)
10102 ok(data[2] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
10103 tests[t].clear, data[2], tests[t].name, c);
10104 else
10105 ok(data[2] == (c + 1) << tests[t].shift, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
10106 (c + 1) << tests[t].shift, data[2], tests[t].name, c);
10108 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0x0000ff00, 1.0f, 0);
10109 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
10111 hr = IDirect3DDevice3_BeginScene(device);
10112 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
10113 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_TEX1, quad, 4, 0);
10114 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10115 hr = IDirect3DDevice3_EndScene(device);
10116 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
10118 color = get_surface_color(rt, 80, 240);
10119 if (!c)
10120 ok(compare_color(color, 0x0000ff00, 1) || broken(warp && compare_color(color, 0x00000000, 1)),
10121 "Got unexpected color 0x%08x, format %s, c=%u.\n",
10122 color, tests[t].name, c);
10123 else
10124 ok(compare_color(color, 0x00000000, 1) || broken(warp && compare_color(color, 0x0000ff00, 1)),
10125 "Got unexpected color 0x%08x, format %s, c=%u.\n",
10126 color, tests[t].name, c);
10128 color = get_surface_color(rt, 240, 240);
10129 ok(compare_color(color, 0x0000ff00, 1) || broken(warp && compare_color(color, 0x00000000, 1)),
10130 "Got unexpected color 0x%08x, format %s, c=%u.\n",
10131 color, tests[t].name, c);
10133 color = get_surface_color(rt, 400, 240);
10134 if (c == tests[t].max)
10135 ok(compare_color(color, 0x0000ff00, 1) || broken(warp && compare_color(color, 0x00000000, 1)),
10136 "Got unexpected color 0x%08x, format %s, c=%u.\n",
10137 color, tests[t].name, c);
10138 else
10139 ok(compare_color(color, 0x00000000, 1) || broken(warp && compare_color(color, 0x0000ff00, 1)),
10140 "Got unexpected color 0x%08x, format %s, c=%u.\n",
10141 color, tests[t].name, c);
10143 IDirect3DTexture2_Release(d3d_texture);
10144 IDirectDrawSurface4_Release(texture);
10146 IDirectDrawSurface4_Release(src);
10147 IDirectDrawSurface4_Release(dst);
10149 done:
10151 destroy_viewport(device, viewport);
10152 IDirectDrawSurface4_Release(rt);
10153 IDirectDraw4_Release(ddraw);
10154 refcount = IDirect3DDevice3_Release(device);
10155 ok(!refcount, "Device has %u references left.\n", refcount);
10156 DestroyWindow(window);
10159 static void test_range_colorkey(void)
10161 IDirectDraw4 *ddraw;
10162 HWND window;
10163 HRESULT hr;
10164 IDirectDrawSurface4 *surface;
10165 DDSURFACEDESC2 surface_desc;
10166 ULONG refcount;
10167 DDCOLORKEY ckey;
10169 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
10170 0, 0, 640, 480, 0, 0, 0, 0);
10171 ddraw = create_ddraw();
10172 ok(!!ddraw, "Failed to create a ddraw object.\n");
10173 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
10174 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
10176 memset(&surface_desc, 0, sizeof(surface_desc));
10177 surface_desc.dwSize = sizeof(surface_desc);
10178 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
10179 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
10180 surface_desc.dwWidth = 1;
10181 surface_desc.dwHeight = 1;
10182 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
10183 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
10184 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
10185 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
10186 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
10187 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0x00000000;
10189 /* Creating a surface with a range color key fails with DDERR_NOCOLORKEY. */
10190 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
10191 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000001;
10192 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10193 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10195 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000001;
10196 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
10197 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10198 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10200 /* Same for DDSCAPS_OFFSCREENPLAIN. */
10201 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
10202 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
10203 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000001;
10204 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10205 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10207 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000001;
10208 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
10209 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10210 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10212 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
10213 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
10214 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10215 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
10217 /* Setting a range color key without DDCKEY_COLORSPACE collapses the key. */
10218 ckey.dwColorSpaceLowValue = 0x00000000;
10219 ckey.dwColorSpaceHighValue = 0x00000001;
10220 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT, &ckey);
10221 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
10223 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
10224 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
10225 ok(!ckey.dwColorSpaceLowValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
10226 ok(!ckey.dwColorSpaceHighValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
10228 ckey.dwColorSpaceLowValue = 0x00000001;
10229 ckey.dwColorSpaceHighValue = 0x00000000;
10230 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT, &ckey);
10231 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
10233 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
10234 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
10235 ok(ckey.dwColorSpaceLowValue == 0x00000001, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
10236 ok(ckey.dwColorSpaceHighValue == 0x00000001, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
10238 /* DDCKEY_COLORSPACE is ignored if the key is a single value. */
10239 ckey.dwColorSpaceLowValue = 0x00000000;
10240 ckey.dwColorSpaceHighValue = 0x00000000;
10241 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
10242 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
10244 /* Using it with a range key results in DDERR_NOCOLORKEYHW. */
10245 ckey.dwColorSpaceLowValue = 0x00000001;
10246 ckey.dwColorSpaceHighValue = 0x00000000;
10247 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
10248 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10249 ckey.dwColorSpaceLowValue = 0x00000000;
10250 ckey.dwColorSpaceHighValue = 0x00000001;
10251 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
10252 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10253 /* Range destination keys don't work either. */
10254 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_DESTBLT | DDCKEY_COLORSPACE, &ckey);
10255 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10257 /* Just to show it's not because of A, R, and G having equal values. */
10258 ckey.dwColorSpaceLowValue = 0x00000000;
10259 ckey.dwColorSpaceHighValue = 0x01010101;
10260 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
10261 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
10263 /* None of these operations modified the key. */
10264 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
10265 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
10266 ok(!ckey.dwColorSpaceLowValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
10267 ok(!ckey.dwColorSpaceHighValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
10269 IDirectDrawSurface4_Release(surface),
10270 refcount = IDirectDraw4_Release(ddraw);
10271 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
10272 DestroyWindow(window);
10275 static void test_shademode(void)
10277 IDirect3DVertexBuffer *vb_strip, *vb_list, *buffer;
10278 IDirect3DViewport3 *viewport;
10279 IDirect3DDevice3 *device;
10280 D3DVERTEXBUFFERDESC desc;
10281 IDirectDrawSurface4 *rt;
10282 DWORD color0, color1;
10283 void *data = NULL;
10284 IDirect3D3 *d3d;
10285 ULONG refcount;
10286 UINT i, count;
10287 HWND window;
10288 HRESULT hr;
10289 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
10290 static const struct
10292 struct vec3 position;
10293 DWORD diffuse;
10295 quad_strip[] =
10297 {{-1.0f, -1.0f, 0.0f}, 0xffff0000},
10298 {{-1.0f, 1.0f, 0.0f}, 0xff00ff00},
10299 {{ 1.0f, -1.0f, 0.0f}, 0xff0000ff},
10300 {{ 1.0f, 1.0f, 0.0f}, 0xffffffff},
10302 quad_list[] =
10304 {{-1.0f, -1.0f, 0.0f}, 0xffff0000},
10305 {{-1.0f, 1.0f, 0.0f}, 0xff00ff00},
10306 {{ 1.0f, -1.0f, 0.0f}, 0xff0000ff},
10308 {{ 1.0f, -1.0f, 0.0f}, 0xff0000ff},
10309 {{-1.0f, 1.0f, 0.0f}, 0xff00ff00},
10310 {{ 1.0f, 1.0f, 0.0f}, 0xffffffff},
10312 static const struct
10314 DWORD primtype;
10315 DWORD shademode;
10316 DWORD color0, color1;
10318 tests[] =
10320 {D3DPT_TRIANGLESTRIP, D3DSHADE_FLAT, 0x00ff0000, 0x0000ff00},
10321 {D3DPT_TRIANGLESTRIP, D3DSHADE_PHONG, 0x000dca28, 0x000d45c7},
10322 {D3DPT_TRIANGLESTRIP, D3DSHADE_GOURAUD, 0x000dca28, 0x000d45c7},
10323 {D3DPT_TRIANGLESTRIP, D3DSHADE_PHONG, 0x000dca28, 0x000d45c7},
10324 {D3DPT_TRIANGLELIST, D3DSHADE_FLAT, 0x00ff0000, 0x000000ff},
10325 {D3DPT_TRIANGLELIST, D3DSHADE_GOURAUD, 0x000dca28, 0x000d45c7},
10328 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
10329 0, 0, 640, 480, 0, 0, 0, 0);
10331 if (!(device = create_device(window, DDSCL_NORMAL)))
10333 skip("Failed to create a 3D device, skipping test.\n");
10334 DestroyWindow(window);
10335 return;
10338 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
10339 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
10340 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
10341 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
10343 viewport = create_viewport(device, 0, 0, 640, 480);
10344 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
10345 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
10347 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
10348 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
10350 memset(&desc, 0, sizeof(desc));
10351 desc.dwSize = sizeof(desc);
10352 desc.dwCaps = D3DVBCAPS_WRITEONLY;
10353 desc.dwFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
10354 desc.dwNumVertices = sizeof(quad_strip) / sizeof(*quad_strip);
10355 hr = IDirect3D3_CreateVertexBuffer(d3d, &desc, &vb_strip, 0, NULL);
10356 ok(hr == D3D_OK, "Failed to create vertex buffer, hr %#x.\n", hr);
10357 hr = IDirect3DVertexBuffer_Lock(vb_strip, 0, &data, NULL);
10358 ok(hr == D3D_OK, "Failed to lock vertex buffer, hr %#x.\n", hr);
10359 memcpy(data, quad_strip, sizeof(quad_strip));
10360 hr = IDirect3DVertexBuffer_Unlock(vb_strip);
10361 ok(hr == D3D_OK, "Failed to unlock vertex buffer, hr %#x.\n", hr);
10363 desc.dwNumVertices = sizeof(quad_list) / sizeof(*quad_list);
10364 hr = IDirect3D3_CreateVertexBuffer(d3d, &desc, &vb_list, 0, NULL);
10365 ok(hr == D3D_OK, "Failed to create vertex buffer, hr %#x.\n", hr);
10366 hr = IDirect3DVertexBuffer_Lock(vb_list, 0, &data, NULL);
10367 ok(hr == D3D_OK, "Failed to lock vertex buffer, hr %#x.\n", hr);
10368 memcpy(data, quad_list, sizeof(quad_list));
10369 hr = IDirect3DVertexBuffer_Unlock(vb_list);
10370 ok(hr == D3D_OK, "Failed to unlock vertex buffer, hr %#x.\n", hr);
10372 /* Try it first with a TRIANGLESTRIP. Do it with different geometry because
10373 * the color fixups we have to do for FLAT shading will be dependent on that. */
10375 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
10377 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffffffff, 0.0f, 0);
10378 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
10380 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_SHADEMODE, tests[i].shademode);
10381 ok(hr == D3D_OK, "Failed to set shade mode, hr %#x.\n", hr);
10383 hr = IDirect3DDevice3_BeginScene(device);
10384 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
10385 buffer = tests[i].primtype == D3DPT_TRIANGLESTRIP ? vb_strip : vb_list;
10386 count = tests[i].primtype == D3DPT_TRIANGLESTRIP ? 4 : 6;
10387 hr = IDirect3DDevice3_DrawPrimitiveVB(device, tests[i].primtype, buffer, 0, count, 0);
10388 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
10389 hr = IDirect3DDevice3_EndScene(device);
10390 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
10392 color0 = get_surface_color(rt, 100, 100); /* Inside first triangle */
10393 color1 = get_surface_color(rt, 500, 350); /* Inside second triangle */
10395 /* For D3DSHADE_FLAT it should take the color of the first vertex of
10396 * each triangle. This requires EXT_provoking_vertex or similar
10397 * functionality being available. */
10398 /* PHONG should be the same as GOURAUD, since no hardware implements
10399 * this. */
10400 ok(compare_color(color0, tests[i].color0, 1), "Test %u shading has color0 %08x, expected %08x.\n",
10401 i, color0, tests[i].color0);
10402 ok(compare_color(color1, tests[i].color1, 1), "Test %u shading has color1 %08x, expected %08x.\n",
10403 i, color1, tests[i].color1);
10406 IDirect3DVertexBuffer_Release(vb_strip);
10407 IDirect3DVertexBuffer_Release(vb_list);
10408 destroy_viewport(device, viewport);
10409 IDirectDrawSurface4_Release(rt);
10410 IDirect3D3_Release(d3d);
10411 refcount = IDirect3DDevice3_Release(device);
10412 ok(!refcount, "Device has %u references left.\n", refcount);
10413 DestroyWindow(window);
10416 static void test_lockrect_invalid(void)
10418 unsigned int i, r;
10419 IDirectDraw4 *ddraw;
10420 IDirectDrawSurface4 *surface;
10421 HWND window;
10422 HRESULT hr;
10423 DDSURFACEDESC2 surface_desc;
10424 DDCAPS hal_caps;
10425 DWORD needed_caps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
10426 static RECT valid[] =
10428 {60, 60, 68, 68},
10429 {60, 60, 60, 68},
10430 {60, 60, 68, 60},
10431 {120, 60, 128, 68},
10432 {60, 120, 68, 128},
10434 static RECT invalid[] =
10436 {68, 60, 60, 68}, /* left > right */
10437 {60, 68, 68, 60}, /* top > bottom */
10438 {-8, 60, 0, 68}, /* left < surface */
10439 {60, -8, 68, 0}, /* top < surface */
10440 {-16, 60, -8, 68}, /* right < surface */
10441 {60, -16, 68, -8}, /* bottom < surface */
10442 {60, 60, 136, 68}, /* right > surface */
10443 {60, 60, 68, 136}, /* bottom > surface */
10444 {136, 60, 144, 68}, /* left > surface */
10445 {60, 136, 68, 144}, /* top > surface */
10447 static const struct
10449 DWORD caps, caps2;
10450 const char *name;
10451 HRESULT hr;
10453 resources[] =
10455 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, 0, "sysmem offscreenplain", DDERR_INVALIDPARAMS},
10456 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0, "vidmem offscreenplain", DDERR_INVALIDPARAMS},
10457 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0, "sysmem texture", DDERR_INVALIDPARAMS},
10458 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, 0, "vidmem texture", DDERR_INVALIDPARAMS},
10459 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, "managed texture", DDERR_INVALIDPARAMS},
10462 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
10463 0, 0, 640, 480, 0, 0, 0, 0);
10464 ddraw = create_ddraw();
10465 ok(!!ddraw, "Failed to create a ddraw object.\n");
10466 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
10467 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
10469 memset(&hal_caps, 0, sizeof(hal_caps));
10470 hal_caps.dwSize = sizeof(hal_caps);
10471 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
10472 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
10473 if ((hal_caps.ddsCaps.dwCaps & needed_caps) != needed_caps
10474 || !(hal_caps.ddsCaps.dwCaps & DDSCAPS2_TEXTUREMANAGE))
10476 skip("Required surface types not supported, skipping test.\n");
10477 goto done;
10480 for (r = 0; r < sizeof(resources) / sizeof(*resources); ++r)
10482 memset(&surface_desc, 0, sizeof(surface_desc));
10483 surface_desc.dwSize = sizeof(surface_desc);
10484 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
10485 surface_desc.ddsCaps.dwCaps = resources[r].caps;
10486 surface_desc.ddsCaps.dwCaps2 = resources[r].caps2;
10487 surface_desc.dwWidth = 128;
10488 surface_desc.dwHeight = 128;
10489 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
10490 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
10491 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
10492 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0xff0000;
10493 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x00ff00;
10494 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x0000ff;
10496 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10497 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, type %s.\n", hr, resources[r].name);
10499 hr = IDirectDrawSurface4_Lock(surface, NULL, NULL, DDLOCK_WAIT, NULL);
10500 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x, type %s.\n", hr, resources[r].name);
10502 for (i = 0; i < sizeof(valid) / sizeof(*valid); ++i)
10504 RECT *rect = &valid[i];
10506 memset(&surface_desc, 0, sizeof(surface_desc));
10507 surface_desc.dwSize = sizeof(surface_desc);
10509 hr = IDirectDrawSurface4_Lock(surface, rect, &surface_desc, DDLOCK_WAIT, NULL);
10510 ok(SUCCEEDED(hr), "Lock failed (%#x) for rect [%d, %d]->[%d, %d], type %s.\n",
10511 hr, rect->left, rect->top, rect->right, rect->bottom, resources[r].name);
10513 hr = IDirectDrawSurface4_Unlock(surface, NULL);
10514 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
10517 for (i = 0; i < sizeof(invalid) / sizeof(*invalid); ++i)
10519 RECT *rect = &invalid[i];
10521 memset(&surface_desc, 1, sizeof(surface_desc));
10522 surface_desc.dwSize = sizeof(surface_desc);
10524 hr = IDirectDrawSurface4_Lock(surface, rect, &surface_desc, DDLOCK_WAIT, NULL);
10525 ok(hr == resources[r].hr, "Lock returned %#x for rect [%d, %d]->[%d, %d], type %s.\n",
10526 hr, rect->left, rect->top, rect->right, rect->bottom, resources[r].name);
10527 if (SUCCEEDED(hr))
10529 hr = IDirectDrawSurface4_Unlock(surface, NULL);
10530 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
10532 else
10533 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
10536 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
10537 ok(SUCCEEDED(hr), "Lock(rect = NULL) failed, hr %#x, type %s.\n",
10538 hr, resources[r].name);
10539 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
10540 ok(hr == DDERR_SURFACEBUSY, "Double lock(rect = NULL) returned %#x, type %s.\n",
10541 hr, resources[r].name);
10542 hr = IDirectDrawSurface4_Unlock(surface, NULL);
10543 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
10545 hr = IDirectDrawSurface4_Lock(surface, &valid[0], &surface_desc, DDLOCK_WAIT, NULL);
10546 ok(SUCCEEDED(hr), "Lock(rect = [%d, %d]->[%d, %d]) failed (%#x).\n",
10547 valid[0].left, valid[0].top, valid[0].right, valid[0].bottom, hr);
10548 hr = IDirectDrawSurface4_Lock(surface, &valid[0], &surface_desc, DDLOCK_WAIT, NULL);
10549 ok(hr == DDERR_SURFACEBUSY, "Double lock(rect = [%d, %d]->[%d, %d]) failed (%#x).\n",
10550 valid[0].left, valid[0].top, valid[0].right, valid[0].bottom, hr);
10552 /* Locking a different rectangle returns DD_OK, but it seems to break the surface.
10553 * Afterwards unlocking the surface fails(NULL rectangle or both locked rectangles) */
10555 hr = IDirectDrawSurface4_Unlock(surface, NULL);
10556 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
10558 IDirectDrawSurface4_Release(surface);
10561 done:
10562 IDirectDraw4_Release(ddraw);
10563 DestroyWindow(window);
10566 static void test_yv12_overlay(void)
10568 IDirectDrawSurface4 *src_surface, *dst_surface;
10569 RECT rect = {13, 17, 14, 18};
10570 unsigned int offset, y;
10571 DDSURFACEDESC2 desc;
10572 unsigned char *base;
10573 IDirectDraw4 *ddraw;
10574 HWND window;
10575 HRESULT hr;
10577 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
10578 0, 0, 640, 480, 0, 0, 0, 0);
10579 ddraw = create_ddraw();
10580 ok(!!ddraw, "Failed to create a ddraw object.\n");
10581 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
10582 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
10584 if (!(src_surface = create_overlay(ddraw, 256, 256, MAKEFOURCC('Y','V','1','2'))))
10586 skip("Failed to create a YV12 overlay, skipping test.\n");
10587 goto done;
10590 memset(&desc, 0, sizeof(desc));
10591 desc.dwSize = sizeof(desc);
10592 hr = IDirectDrawSurface4_Lock(src_surface, NULL, &desc, DDLOCK_WAIT, NULL);
10593 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
10595 ok(desc.dwFlags == (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS | DDSD_PITCH),
10596 "Got unexpected flags %#x.\n", desc.dwFlags);
10597 ok(desc.ddsCaps.dwCaps == (DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM | DDSCAPS_HWCODEC)
10598 || desc.ddsCaps.dwCaps == (DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
10599 "Got unexpected caps %#x.\n", desc.ddsCaps.dwCaps);
10600 ok(desc.dwWidth == 256, "Got unexpected width %u.\n", desc.dwWidth);
10601 ok(desc.dwHeight == 256, "Got unexpected height %u.\n", desc.dwHeight);
10602 /* The overlay pitch seems to have 256 byte alignment. */
10603 ok(!(U1(desc).lPitch & 0xff), "Got unexpected pitch %u.\n", U1(desc).lPitch);
10605 /* Fill the surface with some data for the blit test. */
10606 base = desc.lpSurface;
10607 /* Luminance */
10608 for (y = 0; y < desc.dwHeight; ++y)
10610 memset(base + U1(desc).lPitch * y, 0x10, desc.dwWidth);
10612 /* V */
10613 for (; y < desc.dwHeight + desc.dwHeight / 4; ++y)
10615 memset(base + U1(desc).lPitch * y, 0x20, desc.dwWidth);
10617 /* U */
10618 for (; y < desc.dwHeight + desc.dwHeight / 2; ++y)
10620 memset(base + U1(desc).lPitch * y, 0x30, desc.dwWidth);
10623 hr = IDirectDrawSurface4_Unlock(src_surface, NULL);
10624 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
10626 /* YV12 uses 2x2 blocks with 6 bytes per block (4*Y, 1*U, 1*V). Unlike
10627 * other block-based formats like DXT the entire Y channel is stored in
10628 * one big chunk of memory, followed by the chroma channels. So partial
10629 * locks do not really make sense. Show that they are allowed nevertheless
10630 * and the offset points into the luminance data. */
10631 hr = IDirectDrawSurface4_Lock(src_surface, &rect, &desc, DDLOCK_WAIT, NULL);
10632 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
10633 offset = ((const unsigned char *)desc.lpSurface - base);
10634 ok(offset == rect.top * U1(desc).lPitch + rect.left, "Got unexpected offset %u, expected %u.\n",
10635 offset, rect.top * U1(desc).lPitch + rect.left);
10636 hr = IDirectDrawSurface4_Unlock(src_surface, NULL);
10637 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
10639 if (!(dst_surface = create_overlay(ddraw, 256, 256, MAKEFOURCC('Y','V','1','2'))))
10641 /* Windows XP with a Radeon X1600 GPU refuses to create a second
10642 * overlay surface, DDERR_NOOVERLAYHW, making the blit tests moot. */
10643 skip("Failed to create a second YV12 surface, skipping blit test.\n");
10644 IDirectDrawSurface4_Release(src_surface);
10645 goto done;
10648 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, src_surface, NULL, DDBLT_WAIT, NULL);
10649 /* VMware rejects YV12 blits. This behavior has not been seen on real
10650 * hardware yet, so mark it broken. */
10651 ok(SUCCEEDED(hr) || broken(hr == E_NOTIMPL), "Failed to blit, hr %#x.\n", hr);
10653 if (SUCCEEDED(hr))
10655 memset(&desc, 0, sizeof(desc));
10656 desc.dwSize = sizeof(desc);
10657 hr = IDirectDrawSurface4_Lock(dst_surface, NULL, &desc, DDLOCK_WAIT, NULL);
10658 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
10660 base = desc.lpSurface;
10661 ok(base[0] == 0x10, "Got unexpected Y data 0x%02x.\n", base[0]);
10662 base += desc.dwHeight * U1(desc).lPitch;
10663 todo_wine ok(base[0] == 0x20, "Got unexpected V data 0x%02x.\n", base[0]);
10664 base += desc.dwHeight / 4 * U1(desc).lPitch;
10665 todo_wine ok(base[0] == 0x30, "Got unexpected U data 0x%02x.\n", base[0]);
10667 hr = IDirectDrawSurface4_Unlock(dst_surface, NULL);
10668 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
10671 IDirectDrawSurface4_Release(dst_surface);
10672 IDirectDrawSurface4_Release(src_surface);
10673 done:
10674 IDirectDraw4_Release(ddraw);
10675 DestroyWindow(window);
10678 static void test_offscreen_overlay(void)
10680 IDirectDrawSurface4 *overlay, *offscreen, *primary;
10681 DDSURFACEDESC2 surface_desc;
10682 IDirectDraw4 *ddraw;
10683 HWND window;
10684 HRESULT hr;
10685 HDC dc;
10687 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
10688 0, 0, 640, 480, 0, 0, 0, 0);
10689 ddraw = create_ddraw();
10690 ok(!!ddraw, "Failed to create a ddraw object.\n");
10691 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
10692 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
10694 if (!(overlay = create_overlay(ddraw, 64, 64, MAKEFOURCC('U','Y','V','Y'))))
10696 skip("Failed to create a UYVY overlay, skipping test.\n");
10697 goto done;
10700 memset(&surface_desc, 0, sizeof(surface_desc));
10701 surface_desc.dwSize = sizeof(surface_desc);
10702 surface_desc.dwFlags = DDSD_CAPS;
10703 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
10704 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &primary, NULL);
10705 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
10707 /* On Windows 7, and probably Vista, UpdateOverlay() will return
10708 * DDERR_OUTOFCAPS if the dwm is active. Calling GetDC() on the primary
10709 * surface prevents this by disabling the dwm. */
10710 hr = IDirectDrawSurface4_GetDC(primary, &dc);
10711 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
10712 hr = IDirectDrawSurface4_ReleaseDC(primary, dc);
10713 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
10715 /* Try to overlay a NULL surface. */
10716 hr = IDirectDrawSurface4_UpdateOverlay(overlay, NULL, NULL, NULL, DDOVER_SHOW, NULL);
10717 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
10718 hr = IDirectDrawSurface4_UpdateOverlay(overlay, NULL, NULL, NULL, DDOVER_HIDE, NULL);
10719 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
10721 /* Try to overlay an offscreen surface. */
10722 memset(&surface_desc, 0, sizeof(surface_desc));
10723 surface_desc.dwSize = sizeof(surface_desc);
10724 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
10725 surface_desc.dwWidth = 64;
10726 surface_desc.dwHeight = 64;
10727 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
10728 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
10729 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
10730 U4(surface_desc).ddpfPixelFormat.dwFourCC = 0;
10731 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 16;
10732 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0xf800;
10733 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x07e0;
10734 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x001f;
10735 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &offscreen, NULL);
10736 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
10738 hr = IDirectDrawSurface4_UpdateOverlay(overlay, NULL, offscreen, NULL, DDOVER_SHOW, NULL);
10739 ok(SUCCEEDED(hr), "Failed to update overlay, hr %#x.\n", hr);
10741 /* Try to overlay the primary with a non-overlay surface. */
10742 hr = IDirectDrawSurface4_UpdateOverlay(offscreen, NULL, primary, NULL, DDOVER_SHOW, NULL);
10743 ok(hr == DDERR_NOTAOVERLAYSURFACE, "Got unexpected hr %#x.\n", hr);
10744 hr = IDirectDrawSurface4_UpdateOverlay(offscreen, NULL, primary, NULL, DDOVER_HIDE, NULL);
10745 ok(hr == DDERR_NOTAOVERLAYSURFACE, "Got unexpected hr %#x.\n", hr);
10747 IDirectDrawSurface4_Release(offscreen);
10748 IDirectDrawSurface4_Release(primary);
10749 IDirectDrawSurface4_Release(overlay);
10750 done:
10751 IDirectDraw4_Release(ddraw);
10752 DestroyWindow(window);
10755 static void test_overlay_rect(void)
10757 IDirectDrawSurface4 *overlay, *primary;
10758 DDSURFACEDESC2 surface_desc;
10759 RECT rect = {0, 0, 64, 64};
10760 IDirectDraw4 *ddraw;
10761 LONG pos_x, pos_y;
10762 HRESULT hr, hr2;
10763 HWND window;
10764 HDC dc;
10766 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
10767 0, 0, 640, 480, 0, 0, 0, 0);
10768 ddraw = create_ddraw();
10769 ok(!!ddraw, "Failed to create a ddraw object.\n");
10770 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
10771 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
10773 if (!(overlay = create_overlay(ddraw, 64, 64, MAKEFOURCC('U','Y','V','Y'))))
10775 skip("Failed to create a UYVY overlay, skipping test.\n");
10776 goto done;
10779 memset(&surface_desc, 0, sizeof(surface_desc));
10780 surface_desc.dwSize = sizeof(surface_desc);
10781 surface_desc.dwFlags = DDSD_CAPS;
10782 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
10783 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &primary, NULL);
10784 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
10786 /* On Windows 7, and probably Vista, UpdateOverlay() will return
10787 * DDERR_OUTOFCAPS if the dwm is active. Calling GetDC() on the primary
10788 * surface prevents this by disabling the dwm. */
10789 hr = IDirectDrawSurface4_GetDC(primary, &dc);
10790 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
10791 hr = IDirectDrawSurface4_ReleaseDC(primary, dc);
10792 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
10794 /* The dx sdk sort of implies that rect must be set when DDOVER_SHOW is
10795 * used. This is not true in Windows Vista and earlier, but changed in
10796 * Windows 7. */
10797 hr = IDirectDrawSurface4_UpdateOverlay(overlay, NULL, primary, &rect, DDOVER_SHOW, NULL);
10798 ok(SUCCEEDED(hr), "Failed to update overlay, hr %#x.\n", hr);
10799 hr = IDirectDrawSurface4_UpdateOverlay(overlay, NULL, primary, NULL, DDOVER_HIDE, NULL);
10800 ok(SUCCEEDED(hr), "Failed to update overlay, hr %#x.\n", hr);
10801 hr = IDirectDrawSurface4_UpdateOverlay(overlay, NULL, primary, NULL, DDOVER_SHOW, NULL);
10802 ok(hr == DD_OK || hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
10804 /* Show that the overlay position is the (top, left) coordinate of the
10805 * destination rectangle. */
10806 OffsetRect(&rect, 32, 16);
10807 hr = IDirectDrawSurface4_UpdateOverlay(overlay, NULL, primary, &rect, DDOVER_SHOW, NULL);
10808 ok(SUCCEEDED(hr), "Failed to update overlay, hr %#x.\n", hr);
10809 pos_x = -1; pos_y = -1;
10810 hr = IDirectDrawSurface4_GetOverlayPosition(overlay, &pos_x, &pos_y);
10811 ok(SUCCEEDED(hr), "Failed to get overlay position, hr %#x.\n", hr);
10812 ok(pos_x == rect.left, "Got unexpected pos_x %d, expected %d.\n", pos_x, rect.left);
10813 ok(pos_y == rect.top, "Got unexpected pos_y %d, expected %d.\n", pos_y, rect.top);
10815 /* Passing a NULL dest rect sets the position to 0/0. Visually it can be
10816 * seen that the overlay overlays the whole primary(==screen). */
10817 hr2 = IDirectDrawSurface4_UpdateOverlay(overlay, NULL, primary, NULL, 0, NULL);
10818 ok(hr2 == DD_OK || hr2 == DDERR_INVALIDPARAMS || hr2 == DDERR_OUTOFCAPS, "Got unexpected hr %#x.\n", hr2);
10819 hr = IDirectDrawSurface4_GetOverlayPosition(overlay, &pos_x, &pos_y);
10820 ok(SUCCEEDED(hr), "Failed to get overlay position, hr %#x.\n", hr);
10821 if (SUCCEEDED(hr2))
10823 ok(!pos_x, "Got unexpected pos_x %d.\n", pos_x);
10824 ok(!pos_y, "Got unexpected pos_y %d.\n", pos_y);
10826 else
10828 ok(pos_x == 32, "Got unexpected pos_x %d.\n", pos_x);
10829 ok(pos_y == 16, "Got unexpected pos_y %d.\n", pos_y);
10832 /* The position cannot be retrieved when the overlay is not shown. */
10833 hr = IDirectDrawSurface4_UpdateOverlay(overlay, NULL, primary, &rect, DDOVER_HIDE, NULL);
10834 ok(SUCCEEDED(hr), "Failed to update overlay, hr %#x.\n", hr);
10835 pos_x = -1; pos_y = -1;
10836 hr = IDirectDrawSurface4_GetOverlayPosition(overlay, &pos_x, &pos_y);
10837 ok(hr == DDERR_OVERLAYNOTVISIBLE, "Got unexpected hr %#x.\n", hr);
10838 ok(!pos_x, "Got unexpected pos_x %d.\n", pos_x);
10839 ok(!pos_y, "Got unexpected pos_y %d.\n", pos_y);
10841 IDirectDrawSurface4_Release(primary);
10842 IDirectDrawSurface4_Release(overlay);
10843 done:
10844 IDirectDraw4_Release(ddraw);
10845 DestroyWindow(window);
10848 static void test_blt(void)
10850 IDirectDrawSurface4 *surface, *rt;
10851 DDSURFACEDESC2 surface_desc;
10852 IDirect3DDevice3 *device;
10853 IDirectDraw4 *ddraw;
10854 IDirect3D3 *d3d;
10855 unsigned int i;
10856 ULONG refcount;
10857 HWND window;
10858 HRESULT hr;
10860 static struct
10862 RECT src_rect;
10863 RECT dst_rect;
10864 HRESULT hr;
10866 test_data[] =
10868 {{160, 0, 640, 480}, { 0, 0, 480, 480}, DD_OK}, /* Overlapped blit. */
10869 {{160, 480, 640, 0}, { 0, 0, 480, 480}, DDERR_INVALIDRECT}, /* Overlapped blit, flipped source. */
10870 {{640, 0, 160, 480}, { 0, 0, 480, 480}, DDERR_INVALIDRECT}, /* Overlapped blit, mirrored source. */
10871 {{160, 0, 480, 480}, { 0, 0, 480, 480}, DD_OK}, /* Overlapped blit, stretched x. */
10872 {{160, 160, 640, 480}, { 0, 0, 480, 480}, DD_OK}, /* Overlapped blit, stretched y. */
10873 {{ 0, 0, 640, 480}, { 0, 0, 640, 480}, DD_OK}, /* Full surface blit. */
10874 {{ 0, 0, 640, 480}, { 0, 480, 640, 0}, DDERR_INVALIDRECT}, /* Full surface, flipped destination. */
10875 {{ 0, 0, 640, 480}, {640, 0, 0, 480}, DDERR_INVALIDRECT}, /* Full surface, mirrored destination. */
10876 {{ 0, 480, 640, 0}, { 0, 0, 640, 480}, DDERR_INVALIDRECT}, /* Full surface, flipped source. */
10877 {{640, 0, 0, 480}, { 0, 0, 640, 480}, DDERR_INVALIDRECT}, /* Full surface, mirrored source. */
10880 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
10881 0, 0, 640, 480, 0, 0, 0, 0);
10882 if (!(device = create_device(window, DDSCL_NORMAL)))
10884 skip("Failed to create a 3D device, skipping test.\n");
10885 DestroyWindow(window);
10886 return;
10889 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
10890 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
10891 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
10892 ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
10893 IDirect3D3_Release(d3d);
10894 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
10895 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
10897 memset(&surface_desc, 0, sizeof(surface_desc));
10898 surface_desc.dwSize = sizeof(surface_desc);
10899 surface_desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
10900 surface_desc.dwWidth = 640;
10901 surface_desc.dwHeight = 480;
10902 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
10903 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
10904 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
10906 hr = IDirectDrawSurface_Blt(surface, NULL, surface, NULL, 0, NULL);
10907 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
10909 hr = IDirectDrawSurface_Blt(surface, NULL, rt, NULL, 0, NULL);
10910 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
10912 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
10914 hr = IDirectDrawSurface_Blt(surface, &test_data[i].dst_rect,
10915 surface, &test_data[i].src_rect, DDBLT_WAIT, NULL);
10916 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
10918 hr = IDirectDrawSurface_Blt(surface, &test_data[i].dst_rect,
10919 rt, &test_data[i].src_rect, DDBLT_WAIT, NULL);
10920 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
10923 IDirectDrawSurface4_Release(surface);
10924 IDirectDrawSurface4_Release(rt);
10925 IDirectDraw4_Release(ddraw);
10926 refcount = IDirect3DDevice3_Release(device);
10927 ok(!refcount, "Device has %u references left.\n", refcount);
10928 DestroyWindow(window);
10931 static void test_color_clamping(void)
10933 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
10934 static D3DMATRIX mat =
10936 1.0f, 0.0f, 0.0f, 0.0f,
10937 0.0f, 1.0f, 0.0f, 0.0f,
10938 0.0f, 0.0f, 1.0f, 0.0f,
10939 0.0f, 0.0f, 0.0f, 1.0f,
10941 static struct vec3 quad[] =
10943 {-1.0f, -1.0f, 0.1f},
10944 {-1.0f, 1.0f, 0.1f},
10945 { 1.0f, -1.0f, 0.1f},
10946 { 1.0f, 1.0f, 0.1f},
10948 IDirect3DViewport3 *viewport;
10949 IDirect3DDevice3 *device;
10950 IDirectDrawSurface4 *rt;
10951 ULONG refcount;
10952 D3DCOLOR color;
10953 HWND window;
10954 HRESULT hr;
10956 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
10957 0, 0, 640, 480, NULL, NULL, NULL, NULL);
10959 if (!(device = create_device(window, DDSCL_NORMAL)))
10961 skip("Failed to create a 3D device, skipping test.\n");
10962 DestroyWindow(window);
10963 return;
10966 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
10967 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
10969 viewport = create_viewport(device, 0, 0, 640, 480);
10970 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
10971 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
10973 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &mat);
10974 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
10975 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &mat);
10976 ok(SUCCEEDED(hr), "Failed to set view transform, hr %#x.\n", hr);
10977 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &mat);
10978 ok(SUCCEEDED(hr), "Failed to set projection transform, hr %#x.\n", hr);
10979 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_CLIPPING, FALSE);
10980 ok(SUCCEEDED(hr), "Failed to disable clipping, hr %#x.\n", hr);
10981 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, FALSE);
10982 ok(SUCCEEDED(hr), "Failed to disable Z test, hr %#x.\n", hr);
10983 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
10984 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
10985 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_STENCILENABLE, FALSE);
10986 ok(SUCCEEDED(hr), "Failed to disable stencil test, hr %#x.\n", hr);
10987 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
10988 ok(SUCCEEDED(hr), "Failed to disable culling, hr %#x.\n", hr);
10989 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
10990 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
10992 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_TEXTUREFACTOR, 0xff404040);
10993 ok(SUCCEEDED(hr), "Failed to set texture factor, hr %#x.\n", hr);
10994 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_ADD);
10995 ok(SUCCEEDED(hr), "Failed to set color op, hr %#x.\n", hr);
10996 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
10997 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
10998 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG2, D3DTA_SPECULAR);
10999 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
11000 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_COLOROP, D3DTOP_MODULATE);
11001 ok(SUCCEEDED(hr), "Failed to set color op, hr %#x.\n", hr);
11002 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_COLORARG1, D3DTA_TFACTOR);
11003 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
11004 hr = IDirect3DDevice3_SetTextureStageState(device, 1, D3DTSS_COLORARG2, D3DTA_CURRENT);
11005 ok(SUCCEEDED(hr), "Failed to set color arg, hr %#x.\n", hr);
11007 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 0.0f, 0);
11008 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
11010 hr = IDirect3DDevice3_BeginScene(device);
11011 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
11013 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, quad, 4, 0);
11014 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11016 hr = IDirect3DDevice3_EndScene(device);
11017 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
11019 color = get_surface_color(rt, 320, 240);
11020 ok(compare_color(color, 0x00404040, 1), "Got unexpected color 0x%08x.\n", color);
11022 destroy_viewport(device, viewport);
11023 IDirectDrawSurface4_Release(rt);
11024 refcount = IDirect3DDevice3_Release(device);
11025 ok(!refcount, "Device has %u references left.\n", refcount);
11026 DestroyWindow(window);
11029 static void test_getdc(void)
11031 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
11032 IDirectDrawSurface4 *surface, *surface2, *tmp;
11033 DDSURFACEDESC2 surface_desc, map_desc;
11034 IDirectDraw4 *ddraw;
11035 unsigned int i;
11036 HWND window;
11037 HDC dc, dc2;
11038 HRESULT hr;
11040 static const struct
11042 const char *name;
11043 DDPIXELFORMAT format;
11044 BOOL getdc_supported;
11045 HRESULT alt_result;
11047 test_data[] =
11049 {"D3DFMT_A8R8G8B8", {sizeof(test_data->format), DDPF_RGB | DDPF_ALPHAPIXELS, 0, {32},
11050 {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}}, TRUE},
11051 {"D3DFMT_X8R8G8B8", {sizeof(test_data->format), DDPF_RGB, 0, {32},
11052 {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0x00000000}}, TRUE},
11053 {"D3DFMT_R5G6B5", {sizeof(test_data->format), DDPF_RGB, 0, {16},
11054 {0x0000f800}, {0x000007e0}, {0x0000001f}, {0x00000000}}, TRUE},
11055 {"D3DFMT_X1R5G5B5", {sizeof(test_data->format), DDPF_RGB, 0, {16},
11056 {0x00007c00}, {0x000003e0}, {0x0000001f}, {0x00000000}}, TRUE},
11057 {"D3DFMT_A1R5G5B5", {sizeof(test_data->format), DDPF_RGB | DDPF_ALPHAPIXELS, 0, {16},
11058 {0x00007c00}, {0x000003e0}, {0x0000001f}, {0x00008000}}, TRUE},
11059 {"D3DFMT_A4R4G4B4", {sizeof(test_data->format), DDPF_RGB | DDPF_ALPHAPIXELS, 0, {16},
11060 {0x00000f00}, {0x000000f0}, {0x0000000f}, {0x0000f000}}, TRUE, DDERR_CANTCREATEDC /* Vista+ */},
11061 {"D3DFMT_X4R4G4B4", {sizeof(test_data->format), DDPF_RGB, 0, {16},
11062 {0x00000f00}, {0x000000f0}, {0x0000000f}, {0x00000000}}, TRUE, DDERR_CANTCREATEDC /* Vista+ */},
11063 {"D3DFMT_A2R10G10B10", {sizeof(test_data->format), DDPF_RGB | DDPF_ALPHAPIXELS, 0, {32},
11064 {0xc0000000}, {0x3ff00000}, {0x000ffc00}, {0x000003ff}}, TRUE},
11065 {"D3DFMT_A8B8G8R8", {sizeof(test_data->format), DDPF_RGB | DDPF_ALPHAPIXELS, 0, {32},
11066 {0x000000ff}, {0x0000ff00}, {0x00ff0000}, {0xff000000}}, TRUE, DDERR_CANTCREATEDC /* Vista+ */},
11067 {"D3DFMT_X8B8G8R8", {sizeof(test_data->format), DDPF_RGB, 0, {32},
11068 {0x000000ff}, {0x0000ff00}, {0x00ff0000}, {0x00000000}}, TRUE, DDERR_CANTCREATEDC /* Vista+ */},
11069 {"D3DFMT_R3G3B2", {sizeof(test_data->format), DDPF_RGB, 0, {8},
11070 {0x000000e0}, {0x0000001c}, {0x00000003}, {0x00000000}}, FALSE},
11071 /* GetDC() on a P8 surface fails unless the display mode is 8 bpp.
11072 * This is not implemented in wine yet, so disable the test for now.
11073 * Succeeding P8 GetDC() calls are tested in the ddraw:visual test.
11074 {"D3DFMT_P8", {sizeof(test_data->format), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0, {8 },
11075 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
11077 {"D3DFMT_L8", {sizeof(test_data->format), DDPF_LUMINANCE, 0, {8},
11078 {0x000000ff}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
11079 {"D3DFMT_A8L8", {sizeof(test_data->format), DDPF_ALPHAPIXELS | DDPF_LUMINANCE, 0, {16},
11080 {0x000000ff}, {0x00000000}, {0x00000000}, {0x0000ff00}}, FALSE},
11081 {"D3DFMT_DXT1", {sizeof(test_data->format), DDPF_FOURCC, MAKEFOURCC('D','X','T','1'), {0},
11082 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
11083 {"D3DFMT_DXT2", {sizeof(test_data->format), DDPF_FOURCC, MAKEFOURCC('D','X','T','2'), {0},
11084 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
11085 {"D3DFMT_DXT3", {sizeof(test_data->format), DDPF_FOURCC, MAKEFOURCC('D','X','T','3'), {0},
11086 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
11087 {"D3DFMT_DXT4", {sizeof(test_data->format), DDPF_FOURCC, MAKEFOURCC('D','X','T','4'), {0},
11088 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
11089 {"D3DFMT_DXT5", {sizeof(test_data->format), DDPF_FOURCC, MAKEFOURCC('D','X','T','5'), {0},
11090 {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}}, FALSE},
11093 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
11094 0, 0, 640, 480, 0, 0, 0, 0);
11095 ddraw = create_ddraw();
11096 ok(!!ddraw, "Failed to create a ddraw object.\n");
11097 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
11098 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
11100 for (i = 0; i < (sizeof(test_data) / sizeof(*test_data)); ++i)
11102 memset(&surface_desc, 0, sizeof(surface_desc));
11103 surface_desc.dwSize = sizeof(surface_desc);
11104 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
11105 surface_desc.dwWidth = 64;
11106 surface_desc.dwHeight = 64;
11107 U4(surface_desc).ddpfPixelFormat = test_data[i].format;
11108 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
11110 if (FAILED(IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL)))
11112 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
11113 surface_desc.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
11114 if (FAILED(hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL)))
11116 skip("Failed to create surface for format %s (hr %#x), skipping tests.\n", test_data[i].name, hr);
11117 continue;
11121 dc = (void *)0x1234;
11122 hr = IDirectDrawSurface4_GetDC(surface, &dc);
11123 if (test_data[i].getdc_supported)
11124 ok(SUCCEEDED(hr) || (test_data[i].alt_result && hr == test_data[i].alt_result),
11125 "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11126 else
11127 ok(FAILED(hr), "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11129 if (SUCCEEDED(hr))
11131 unsigned int width_bytes;
11132 DIBSECTION dib;
11133 HBITMAP bitmap;
11134 DWORD type;
11135 int size;
11137 type = GetObjectType(dc);
11138 ok(type == OBJ_MEMDC, "Got unexpected object type %#x for format %s.\n", type, test_data[i].name);
11139 bitmap = GetCurrentObject(dc, OBJ_BITMAP);
11140 type = GetObjectType(bitmap);
11141 ok(type == OBJ_BITMAP, "Got unexpected object type %#x for format %s.\n", type, test_data[i].name);
11143 size = GetObjectA(bitmap, sizeof(dib), &dib);
11144 ok(size == sizeof(dib), "Got unexpected size %d for format %s.\n", size, test_data[i].name);
11145 ok(!dib.dsBm.bmType, "Got unexpected type %#x for format %s.\n",
11146 dib.dsBm.bmType, test_data[i].name);
11147 ok(dib.dsBm.bmWidth == surface_desc.dwWidth, "Got unexpected width %d for format %s.\n",
11148 dib.dsBm.bmWidth, test_data[i].name);
11149 ok(dib.dsBm.bmHeight == surface_desc.dwHeight, "Got unexpected height %d for format %s.\n",
11150 dib.dsBm.bmHeight, test_data[i].name);
11151 width_bytes = ((dib.dsBm.bmWidth * U1(test_data[i].format).dwRGBBitCount + 31) >> 3) & ~3;
11152 ok(dib.dsBm.bmWidthBytes == width_bytes, "Got unexpected width bytes %d for format %s.\n",
11153 dib.dsBm.bmWidthBytes, test_data[i].name);
11154 ok(dib.dsBm.bmPlanes == 1, "Got unexpected plane count %d for format %s.\n",
11155 dib.dsBm.bmPlanes, test_data[i].name);
11156 ok(dib.dsBm.bmBitsPixel == U1(test_data[i].format).dwRGBBitCount,
11157 "Got unexpected bit count %d for format %s.\n",
11158 dib.dsBm.bmBitsPixel, test_data[i].name);
11159 ok(!!dib.dsBm.bmBits, "Got unexpected bits %p for format %s.\n",
11160 dib.dsBm.bmBits, test_data[i].name);
11162 ok(dib.dsBmih.biSize == sizeof(dib.dsBmih), "Got unexpected size %u for format %s.\n",
11163 dib.dsBmih.biSize, test_data[i].name);
11164 ok(dib.dsBmih.biWidth == surface_desc.dwWidth, "Got unexpected width %d for format %s.\n",
11165 dib.dsBmih.biHeight, test_data[i].name);
11166 ok(dib.dsBmih.biHeight == surface_desc.dwHeight, "Got unexpected height %d for format %s.\n",
11167 dib.dsBmih.biHeight, test_data[i].name);
11168 ok(dib.dsBmih.biPlanes == 1, "Got unexpected plane count %u for format %s.\n",
11169 dib.dsBmih.biPlanes, test_data[i].name);
11170 ok(dib.dsBmih.biBitCount == U1(test_data[i].format).dwRGBBitCount,
11171 "Got unexpected bit count %u for format %s.\n",
11172 dib.dsBmih.biBitCount, test_data[i].name);
11173 ok(dib.dsBmih.biCompression == (U1(test_data[i].format).dwRGBBitCount == 16 ? BI_BITFIELDS : BI_RGB)
11174 || broken(U2(test_data[i].format).dwRGBBitCount == 32 && dib.dsBmih.biCompression == BI_BITFIELDS),
11175 "Got unexpected compression %#x for format %s.\n",
11176 dib.dsBmih.biCompression, test_data[i].name);
11177 ok(!dib.dsBmih.biSizeImage, "Got unexpected image size %u for format %s.\n",
11178 dib.dsBmih.biSizeImage, test_data[i].name);
11179 ok(!dib.dsBmih.biXPelsPerMeter, "Got unexpected horizontal resolution %d for format %s.\n",
11180 dib.dsBmih.biXPelsPerMeter, test_data[i].name);
11181 ok(!dib.dsBmih.biYPelsPerMeter, "Got unexpected vertical resolution %d for format %s.\n",
11182 dib.dsBmih.biYPelsPerMeter, test_data[i].name);
11183 ok(!dib.dsBmih.biClrUsed, "Got unexpected used colour count %u for format %s.\n",
11184 dib.dsBmih.biClrUsed, test_data[i].name);
11185 ok(!dib.dsBmih.biClrImportant, "Got unexpected important colour count %u for format %s.\n",
11186 dib.dsBmih.biClrImportant, test_data[i].name);
11188 if (dib.dsBmih.biCompression == BI_BITFIELDS)
11190 ok((dib.dsBitfields[0] == U2(test_data[i].format).dwRBitMask
11191 && dib.dsBitfields[1] == U3(test_data[i].format).dwGBitMask
11192 && dib.dsBitfields[2] == U4(test_data[i].format).dwBBitMask)
11193 || broken(!dib.dsBitfields[0] && !dib.dsBitfields[1] && !dib.dsBitfields[2]),
11194 "Got unexpected colour masks 0x%08x 0x%08x 0x%08x for format %s.\n",
11195 dib.dsBitfields[0], dib.dsBitfields[1], dib.dsBitfields[2], test_data[i].name);
11197 else
11199 ok(!dib.dsBitfields[0] && !dib.dsBitfields[1] && !dib.dsBitfields[2],
11200 "Got unexpected colour masks 0x%08x 0x%08x 0x%08x for format %s.\n",
11201 dib.dsBitfields[0], dib.dsBitfields[1], dib.dsBitfields[2], test_data[i].name);
11203 ok(!dib.dshSection, "Got unexpected section %p for format %s.\n", dib.dshSection, test_data[i].name);
11204 ok(!dib.dsOffset, "Got unexpected offset %u for format %s.\n", dib.dsOffset, test_data[i].name);
11206 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
11207 ok(hr == DD_OK, "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11209 else
11211 ok(!dc, "Got unexpected dc %p for format %s.\n", dc, test_data[i].name);
11214 IDirectDrawSurface4_Release(surface);
11216 if (FAILED(hr))
11217 continue;
11219 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
11220 surface_desc.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
11221 if (FAILED(hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL)))
11223 skip("Failed to create mip-mapped texture for format %s (hr %#x), skipping tests.\n",
11224 test_data[i].name, hr);
11225 continue;
11228 hr = IDirectDrawSurface4_GetAttachedSurface(surface, &caps, &tmp);
11229 ok(SUCCEEDED(hr), "Failed to get attached surface for format %s, hr %#x.\n", test_data[i].name, hr);
11230 hr = IDirectDrawSurface4_GetAttachedSurface(tmp, &caps, &surface2);
11231 ok(SUCCEEDED(hr), "Failed to get attached surface for format %s, hr %#x.\n", test_data[i].name, hr);
11232 IDirectDrawSurface4_Release(tmp);
11234 hr = IDirectDrawSurface4_GetDC(surface, &dc);
11235 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11236 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
11237 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11238 hr = IDirectDrawSurface4_GetDC(surface2, &dc);
11239 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11240 hr = IDirectDrawSurface4_ReleaseDC(surface2, dc);
11241 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11243 hr = IDirectDrawSurface4_GetDC(surface, &dc);
11244 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11245 dc2 = (void *)0x1234;
11246 hr = IDirectDrawSurface4_GetDC(surface, &dc2);
11247 ok(hr == DDERR_DCALREADYCREATED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11248 ok(dc2 == (void *)0x1234, "Got unexpected dc %p for format %s.\n", dc, test_data[i].name);
11249 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
11250 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11251 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
11252 ok(hr == DDERR_NODC, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11254 map_desc.dwSize = sizeof(map_desc);
11255 hr = IDirectDrawSurface4_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
11256 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
11257 hr = IDirectDrawSurface4_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
11258 ok(hr == DDERR_SURFACEBUSY, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11259 hr = IDirectDrawSurface4_Unlock(surface, NULL);
11260 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
11261 hr = IDirectDrawSurface4_Unlock(surface, NULL);
11262 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11264 hr = IDirectDrawSurface4_GetDC(surface, &dc);
11265 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11266 hr = IDirectDrawSurface4_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
11267 ok(hr == DDERR_SURFACEBUSY, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11268 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
11269 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11271 hr = IDirectDrawSurface4_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
11272 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
11273 hr = IDirectDrawSurface4_GetDC(surface, &dc);
11274 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11275 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
11276 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11277 hr = IDirectDrawSurface4_Unlock(surface, NULL);
11278 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
11280 hr = IDirectDrawSurface4_GetDC(surface, &dc);
11281 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11282 hr = IDirectDrawSurface4_GetDC(surface2, &dc2);
11283 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11284 hr = IDirectDrawSurface4_ReleaseDC(surface2, dc2);
11285 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11286 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
11287 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11289 hr = IDirectDrawSurface4_GetDC(surface2, &dc);
11290 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11291 hr = IDirectDrawSurface4_GetDC(surface, &dc2);
11292 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11293 hr = IDirectDrawSurface4_ReleaseDC(surface, dc2);
11294 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11295 hr = IDirectDrawSurface4_ReleaseDC(surface2, dc);
11296 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11298 hr = IDirectDrawSurface4_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
11299 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
11300 hr = IDirectDrawSurface4_Lock(surface2, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
11301 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
11302 hr = IDirectDrawSurface4_Unlock(surface2, NULL);
11303 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
11304 hr = IDirectDrawSurface4_Unlock(surface, NULL);
11305 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
11307 hr = IDirectDrawSurface4_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
11308 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
11309 hr = IDirectDrawSurface4_GetDC(surface, &dc);
11310 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11311 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
11312 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11313 hr = IDirectDrawSurface4_Unlock(surface, NULL);
11314 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
11316 hr = IDirectDrawSurface4_Lock(surface2, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
11317 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
11318 hr = IDirectDrawSurface4_GetDC(surface, &dc);
11319 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11320 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
11321 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11322 hr = IDirectDrawSurface4_Unlock(surface2, NULL);
11323 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
11325 hr = IDirectDrawSurface4_GetDC(surface, &dc);
11326 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11327 hr = IDirectDrawSurface4_Lock(surface2, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
11328 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
11329 hr = IDirectDrawSurface4_Unlock(surface2, NULL);
11330 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
11331 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
11332 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11334 hr = IDirectDrawSurface4_GetDC(surface2, &dc);
11335 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11336 hr = IDirectDrawSurface4_Lock(surface, NULL, &map_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
11337 ok(SUCCEEDED(hr), "Failed to map surface for format %s, hr %#x.\n", test_data[i].name, hr);
11338 hr = IDirectDrawSurface4_Unlock(surface, NULL);
11339 ok(SUCCEEDED(hr), "Failed to unmap surface for format %s, hr %#x.\n", test_data[i].name, hr);
11340 hr = IDirectDrawSurface4_ReleaseDC(surface2, dc);
11341 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11343 hr = IDirectDrawSurface4_Unlock(surface, NULL);
11344 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11345 hr = IDirectDrawSurface4_GetDC(surface2, &dc);
11346 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11347 hr = IDirectDrawSurface4_Unlock(surface, NULL);
11348 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11349 hr = IDirectDrawSurface4_ReleaseDC(surface2, dc);
11350 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11351 hr = IDirectDrawSurface4_Unlock(surface, NULL);
11352 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11354 hr = IDirectDrawSurface4_Unlock(surface2, NULL);
11355 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11356 hr = IDirectDrawSurface4_GetDC(surface, &dc);
11357 ok(SUCCEEDED(hr), "Failed to get DC for format %s, hr %#x.\n", test_data[i].name, hr);
11358 hr = IDirectDrawSurface4_Unlock(surface2, NULL);
11359 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11360 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
11361 ok(SUCCEEDED(hr), "Failed to release DC for format %s, hr %#x.\n", test_data[i].name, hr);
11362 hr = IDirectDrawSurface4_Unlock(surface2, NULL);
11363 ok(hr == DDERR_NOTLOCKED, "Got unexpected hr %#x for format %s.\n", hr, test_data[i].name);
11365 IDirectDrawSurface4_Release(surface2);
11366 IDirectDrawSurface4_Release(surface);
11369 IDirectDraw4_Release(ddraw);
11370 DestroyWindow(window);
11373 static void test_draw_primitive(void)
11375 static WORD indices[] = {0, 1, 2, 3};
11376 static struct vec3 quad[] =
11378 {-1.0f, -1.0f, 0.0f},
11379 {-1.0f, 1.0f, 0.0f},
11380 { 1.0f, -1.0f, 0.0f},
11381 { 1.0f, 1.0f, 0.0f},
11383 D3DDRAWPRIMITIVESTRIDEDDATA strided;
11384 IDirect3DViewport3 *viewport;
11385 D3DVERTEXBUFFERDESC vb_desc;
11386 IDirect3DVertexBuffer *vb;
11387 IDirect3DDevice3 *device;
11388 IDirect3D3 *d3d;
11389 ULONG refcount;
11390 HWND window;
11391 HRESULT hr;
11392 void *data;
11394 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
11395 0, 0, 640, 480, NULL, NULL, NULL, NULL);
11397 if (!(device = create_device(window, DDSCL_NORMAL)))
11399 skip("Failed to create a 3D device, skipping test.\n");
11400 DestroyWindow(window);
11401 return;
11404 viewport = create_viewport(device, 0, 0, 640, 480);
11405 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
11406 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
11408 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
11409 ok(SUCCEEDED(hr), "Failed to get D3D interface, hr %#x.\n", hr);
11411 memset(&vb_desc, 0, sizeof(vb_desc));
11412 vb_desc.dwSize = sizeof(vb_desc);
11413 vb_desc.dwFVF = D3DFVF_XYZ;
11414 vb_desc.dwNumVertices = 4;
11415 hr = IDirect3D3_CreateVertexBuffer(d3d, &vb_desc, &vb, 0, NULL);
11416 ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr);
11418 IDirect3D3_Release(d3d);
11420 memset(&strided, 0, sizeof(strided));
11422 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, NULL, 0, NULL, 0, 0);
11423 todo_wine ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11424 hr = IDirect3DDevice3_DrawIndexedPrimitiveStrided(device,
11425 D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, &strided, 0, NULL, 0, 0);
11426 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11427 hr = IDirect3DDevice3_DrawIndexedPrimitiveVB(device, D3DPT_TRIANGLESTRIP, vb, NULL, 0, 0);
11428 todo_wine ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11429 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, NULL, 0, 0);
11430 todo_wine ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11431 hr = IDirect3DDevice3_DrawPrimitiveStrided(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, &strided, 0, 0);
11432 todo_wine ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11433 hr = IDirect3DDevice3_DrawPrimitiveVB(device, D3DPT_TRIANGLESTRIP, vb, 0, 0, 0);
11434 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11436 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, NULL, 0, indices, 4, 0);
11437 todo_wine ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11438 hr = IDirect3DDevice3_DrawIndexedPrimitiveStrided(device,
11439 D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, &strided, 0, indices, 4, 0);
11440 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11441 hr = IDirect3DDevice3_DrawIndexedPrimitiveVB(device, D3DPT_TRIANGLESTRIP, vb, indices, 4, 0);
11442 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11444 strided.position.lpvData = quad;
11445 strided.position.dwStride = sizeof(*quad);
11446 hr = IDirect3DVertexBuffer_Lock(vb, 0, &data, NULL);
11447 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
11448 memcpy(data, quad, sizeof(quad));
11449 hr = IDirect3DVertexBuffer_Unlock(vb);
11450 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
11452 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, quad, 4, NULL, 0, 0);
11453 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11454 hr = IDirect3DDevice3_DrawIndexedPrimitiveStrided(device,
11455 D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, &strided, 4, NULL, 0, 0);
11456 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11457 hr = IDirect3DDevice3_DrawIndexedPrimitiveVB(device, D3DPT_TRIANGLESTRIP, vb, NULL, 0, 0);
11458 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11459 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, quad, 4, 0);
11460 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11461 hr = IDirect3DDevice3_DrawPrimitiveStrided(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, &strided, 4, 0);
11462 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11463 hr = IDirect3DDevice3_DrawPrimitiveVB(device, D3DPT_TRIANGLESTRIP, vb, 0, 4, 0);
11464 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11465 hr = IDirect3DDevice3_DrawIndexedPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, quad, 4, indices, 4, 0);
11466 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11467 hr = IDirect3DDevice3_DrawIndexedPrimitiveStrided(device,
11468 D3DPT_TRIANGLESTRIP, D3DFVF_XYZ, &strided, 4, indices, 4, 0);
11469 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11470 hr = IDirect3DDevice3_DrawIndexedPrimitiveVB(device, D3DPT_TRIANGLESTRIP, vb, indices, 4, 0);
11471 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
11473 IDirect3DVertexBuffer_Release(vb);
11474 destroy_viewport(device, viewport);
11475 refcount = IDirect3DDevice3_Release(device);
11476 ok(!refcount, "Device has %u references left.\n", refcount);
11477 DestroyWindow(window);
11480 START_TEST(ddraw4)
11482 IDirectDraw4 *ddraw;
11483 DEVMODEW current_mode;
11485 if (!(ddraw = create_ddraw()))
11487 skip("Failed to create a ddraw object, skipping tests.\n");
11488 return;
11490 IDirectDraw4_Release(ddraw);
11492 memset(&current_mode, 0, sizeof(current_mode));
11493 current_mode.dmSize = sizeof(current_mode);
11494 ok(EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &current_mode), "Failed to get display mode.\n");
11495 registry_mode.dmSize = sizeof(registry_mode);
11496 ok(EnumDisplaySettingsW(NULL, ENUM_REGISTRY_SETTINGS, &registry_mode), "Failed to get display mode.\n");
11497 if (registry_mode.dmPelsWidth != current_mode.dmPelsWidth
11498 || registry_mode.dmPelsHeight != current_mode.dmPelsHeight)
11500 skip("Current mode does not match registry mode, skipping test.\n");
11501 return;
11504 test_process_vertices();
11505 test_coop_level_create_device_window();
11506 test_clipper_blt();
11507 test_coop_level_d3d_state();
11508 test_surface_interface_mismatch();
11509 test_coop_level_threaded();
11510 test_depth_blit();
11511 test_texture_load_ckey();
11512 test_viewport();
11513 test_zenable();
11514 test_ck_rgba();
11515 test_ck_default();
11516 test_ck_complex();
11517 test_surface_qi();
11518 test_device_qi();
11519 test_wndproc();
11520 test_window_style();
11521 test_redundant_mode_set();
11522 test_coop_level_mode_set();
11523 test_coop_level_mode_set_multi();
11524 test_initialize();
11525 test_coop_level_surf_create();
11526 test_vb_discard();
11527 test_coop_level_multi_window();
11528 test_draw_strided();
11529 test_lighting();
11530 test_specular_lighting();
11531 test_clear_rect_count();
11532 test_coop_level_versions();
11533 test_lighting_interface_versions();
11534 test_coop_level_activateapp();
11535 test_texturemanage();
11536 test_block_formats_creation();
11537 test_unsupported_formats();
11538 test_rt_caps();
11539 test_primary_caps();
11540 test_surface_lock();
11541 test_surface_discard();
11542 test_flip();
11543 test_set_surface_desc();
11544 test_user_memory_getdc();
11545 test_sysmem_overlay();
11546 test_primary_palette();
11547 test_surface_attachment();
11548 test_private_data();
11549 test_pixel_format();
11550 test_create_surface_pitch();
11551 test_mipmap();
11552 test_palette_complex();
11553 test_p8_rgb_blit();
11554 test_material();
11555 test_palette_gdi();
11556 test_palette_alpha();
11557 test_vb_writeonly();
11558 test_lost_device();
11559 test_surface_desc_lock();
11560 test_signed_formats();
11561 test_color_fill();
11562 test_texcoordindex();
11563 test_colorkey_precision();
11564 test_range_colorkey();
11565 test_shademode();
11566 test_lockrect_invalid();
11567 test_yv12_overlay();
11568 test_offscreen_overlay();
11569 test_overlay_rect();
11570 test_blt();
11571 test_color_clamping();
11572 test_getdc();
11573 test_draw_primitive();