2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
26 #include "ddraw_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
30 /* Device identifier. Don't relay it to WineD3D */
31 static const DDDEVICEIDENTIFIER2 deviceidentifier =
35 { { 0x00010001, 0x00010001 } },
37 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
38 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
42 static struct enum_device_entry
44 char interface_name[100];
45 char device_name[100];
46 const GUID *device_guid;
51 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
53 &IID_IDirect3DTnLHalDevice,
58 "WINE Direct3D7 Hardware acceleration using WineD3D",
60 &IID_IDirect3DHALDevice,
65 "WINE Direct3D7 RGB Software Emulation using WineD3D",
67 &IID_IDirect3DRGBDevice,
71 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
73 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
75 ddraw_null_wined3d_object_destroyed,
78 static inline IDirectDrawImpl *impl_from_IDirectDraw(IDirectDraw *iface)
80 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw_iface);
83 static inline IDirectDrawImpl *impl_from_IDirectDraw2(IDirectDraw2 *iface)
85 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw2_iface);
88 static inline IDirectDrawImpl *impl_from_IDirectDraw4(IDirectDraw4 *iface)
90 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw4_iface);
93 static inline IDirectDrawImpl *impl_from_IDirectDraw7(IDirectDraw7 *iface)
95 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw7_iface);
98 static inline IDirectDrawImpl *impl_from_IDirect3D(IDirect3D *iface)
100 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D_iface);
103 static inline IDirectDrawImpl *impl_from_IDirect3D2(IDirect3D2 *iface)
105 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D2_iface);
108 static inline IDirectDrawImpl *impl_from_IDirect3D3(IDirect3D3 *iface)
110 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D3_iface);
113 static inline IDirectDrawImpl *impl_from_IDirect3D7(IDirect3D7 *iface)
115 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D7_iface);
118 /*****************************************************************************
120 *****************************************************************************/
122 /*****************************************************************************
123 * IDirectDraw7::QueryInterface
125 * Queries different interfaces of the DirectDraw object. It can return
126 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
127 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
129 * The returned interface is AddRef()-ed before it's returned
131 * Used for version 1, 2, 4 and 7
134 * refiid: Interface ID asked for
135 * obj: Used to return the interface pointer
138 * S_OK if an interface was found
139 * E_NOINTERFACE if the requested interface wasn't found
141 *****************************************************************************/
142 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID refiid, void **obj)
144 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
146 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
148 /* Can change surface impl type */
149 EnterCriticalSection(&ddraw_cs);
151 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
156 LeaveCriticalSection(&ddraw_cs);
157 return DDERR_INVALIDPARAMS;
160 /* Check DirectDraw Interfaces */
161 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
162 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
165 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
167 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
169 *obj = &This->IDirectDraw4_iface;
170 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
172 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
174 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
175 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
177 LeaveCriticalSection(&ddraw_cs);
178 return E_NOINTERFACE;
180 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
182 *obj = &This->IDirectDraw2_iface;
183 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
185 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
187 *obj = &This->IDirectDraw_iface;
188 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
192 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
193 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
194 * who had this idea and why. The older interfaces can query and IDirect3D version
195 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
196 * and messy to implement with the common creation function, so it has been left out here.
198 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
199 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
200 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
201 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
203 /* Check the surface implementation */
204 if (DefaultSurfaceType != SURFACE_OPENGL)
206 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
207 /* Do not abort here, only reject 3D Device creation */
210 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
212 This->d3dversion = 1;
213 *obj = &This->IDirect3D_iface;
214 TRACE(" returning Direct3D interface at %p.\n", *obj);
216 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
218 This->d3dversion = 2;
219 *obj = &This->IDirect3D2_iface;
220 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
222 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
224 This->d3dversion = 3;
225 *obj = &This->IDirect3D3_iface;
226 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
228 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
230 This->d3dversion = 7;
231 *obj = &This->IDirect3D7_iface;
232 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
235 /* Unknown interface */
238 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
239 LeaveCriticalSection(&ddraw_cs);
240 return E_NOINTERFACE;
243 IUnknown_AddRef( (IUnknown *) *obj );
244 LeaveCriticalSection(&ddraw_cs);
248 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
250 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
252 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
254 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
257 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
259 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
261 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
263 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
266 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
268 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
270 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
272 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
275 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
277 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
279 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
281 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
284 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
286 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
288 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
290 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
293 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
295 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
297 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
299 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
302 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
304 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
306 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
308 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
311 /*****************************************************************************
312 * IDirectDraw7::AddRef
314 * Increases the interfaces refcount, basically
316 * DDraw refcounting is a bit tricky. The different DirectDraw interface
317 * versions have individual refcounts, but the IDirect3D interfaces do not.
318 * All interfaces are from one object, that means calling QueryInterface on an
319 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
320 * IDirectDrawImpl object.
322 * That means all AddRef and Release implementations of IDirectDrawX work
323 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
324 * except of IDirect3D7 which thunks to IDirectDraw7
326 * Returns: The new refcount
328 *****************************************************************************/
329 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
331 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
332 ULONG ref = InterlockedIncrement(&This->ref7);
334 TRACE("%p increasing refcount to %u.\n", This, ref);
336 if(ref == 1) InterlockedIncrement(&This->numIfaces);
341 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
343 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
344 ULONG ref = InterlockedIncrement(&This->ref4);
346 TRACE("%p increasing refcount to %u.\n", This, ref);
348 if (ref == 1) InterlockedIncrement(&This->numIfaces);
353 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
355 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
356 ULONG ref = InterlockedIncrement(&This->ref2);
358 TRACE("%p increasing refcount to %u.\n", This, ref);
360 if (ref == 1) InterlockedIncrement(&This->numIfaces);
365 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
367 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
368 ULONG ref = InterlockedIncrement(&This->ref1);
370 TRACE("%p increasing refcount to %u.\n", This, ref);
372 if (ref == 1) InterlockedIncrement(&This->numIfaces);
377 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
379 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
381 TRACE("iface %p.\n", iface);
383 return ddraw7_AddRef(&This->IDirectDraw7_iface);
386 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
388 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
390 TRACE("iface %p.\n", iface);
392 return ddraw1_AddRef(&This->IDirectDraw_iface);
395 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
397 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
399 TRACE("iface %p.\n", iface);
401 return ddraw1_AddRef(&This->IDirectDraw_iface);
404 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
406 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
408 TRACE("iface %p.\n", iface);
410 return ddraw1_AddRef(&This->IDirectDraw_iface);
413 /*****************************************************************************
416 * Destroys a ddraw object if all refcounts are 0. This is to share code
417 * between the IDirectDrawX::Release functions
420 * This: DirectDraw object to destroy
422 *****************************************************************************/
423 static void ddraw_destroy(IDirectDrawImpl *This)
425 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
426 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
428 /* Destroy the device window if we created one */
429 if(This->devicewindow != 0)
431 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
432 DestroyWindow(This->devicewindow);
433 This->devicewindow = 0;
436 EnterCriticalSection(&ddraw_cs);
437 list_remove(&This->ddraw_list_entry);
438 LeaveCriticalSection(&ddraw_cs);
440 /* Release the attached WineD3D stuff */
441 wined3d_device_decref(This->wined3d_device);
442 wined3d_decref(This->wineD3D);
444 /* Now free the object */
445 HeapFree(GetProcessHeap(), 0, This);
448 /*****************************************************************************
449 * IDirectDraw7::Release
451 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
453 * Returns: The new refcount
454 *****************************************************************************/
455 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
457 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
458 ULONG ref = InterlockedDecrement(&This->ref7);
460 TRACE("%p decreasing refcount to %u.\n", This, ref);
462 if (!ref && !InterlockedDecrement(&This->numIfaces))
468 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
470 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
471 ULONG ref = InterlockedDecrement(&This->ref4);
473 TRACE("%p decreasing refcount to %u.\n", This, ref);
475 if (!ref && !InterlockedDecrement(&This->numIfaces))
481 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
483 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
484 ULONG ref = InterlockedDecrement(&This->ref2);
486 TRACE("%p decreasing refcount to %u.\n", This, ref);
488 if (!ref && !InterlockedDecrement(&This->numIfaces))
494 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
496 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
497 ULONG ref = InterlockedDecrement(&This->ref1);
499 TRACE("%p decreasing refcount to %u.\n", This, ref);
501 if (!ref && !InterlockedDecrement(&This->numIfaces))
507 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
509 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
511 TRACE("iface %p.\n", iface);
513 return ddraw7_Release(&This->IDirectDraw7_iface);
516 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
518 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
520 TRACE("iface %p.\n", iface);
522 return ddraw1_Release(&This->IDirectDraw_iface);
525 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
527 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
529 TRACE("iface %p.\n", iface);
531 return ddraw1_Release(&This->IDirectDraw_iface);
534 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
536 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
538 TRACE("iface %p.\n", iface);
540 return ddraw1_Release(&This->IDirectDraw_iface);
543 /*****************************************************************************
544 * IDirectDraw methods
545 *****************************************************************************/
547 static HRESULT ddraw_set_focus_window(IDirectDrawImpl *ddraw, HWND window)
549 /* FIXME: This looks wrong, exclusive mode should imply a destination
551 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE) && ddraw->dest_window)
553 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET.\n");
554 return DDERR_HWNDALREADYSET;
557 ddraw->focuswindow = window;
559 /* Use the focus window for drawing too. */
560 ddraw->dest_window = ddraw->focuswindow;
562 /* Destroy the device window, if we have one. */
563 if (ddraw->devicewindow)
565 DestroyWindow(ddraw->devicewindow);
566 ddraw->devicewindow = NULL;
572 /*****************************************************************************
573 * IDirectDraw7::SetCooperativeLevel
575 * Sets the cooperative level for the DirectDraw object, and the window
576 * assigned to it. The cooperative level determines the general behavior
577 * of the DirectDraw application
579 * Warning: This is quite tricky, as it's not really documented which
580 * cooperative levels can be combined with each other. If a game fails
581 * after this function, try to check the cooperative levels passed on
582 * Windows, and if it returns something different.
584 * If you think that this function caused the failure because it writes a
585 * fixme, be sure to run again with a +ddraw trace.
587 * What is known about cooperative levels (See the ddraw modes test):
588 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
589 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
590 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
591 * DDSCL_FULLSCREEN can be activated
592 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
594 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
595 * DDSCL_SETFOCUSWINDOW (partially),
596 * DDSCL_MULTITHREADED (work in progress)
598 * Unhandled flags, which should be implemented
599 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
600 * expect any difference to a normal window for wine)
601 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
602 * rendering (Possible test case: Half-Life)
604 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
606 * These don't seem very important for wine:
607 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
610 * DD_OK if the cooperative level was set successfully
611 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
612 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
613 * (Probably others too, have to investigate)
615 *****************************************************************************/
616 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND hwnd, DWORD cooplevel)
618 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
622 TRACE("iface %p, window %p, flags %#x.\n", iface, hwnd, cooplevel);
623 DDRAW_dump_cooperativelevel(cooplevel);
625 EnterCriticalSection(&ddraw_cs);
627 /* Get the old window */
628 window = This->dest_window;
630 /* Tests suggest that we need one of them: */
631 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
635 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
636 LeaveCriticalSection(&ddraw_cs);
637 return DDERR_INVALIDPARAMS;
640 /* Handle those levels first which set various hwnds */
641 if(cooplevel & DDSCL_SETFOCUSWINDOW)
643 /* This isn't compatible with a lot of flags */
644 if(cooplevel & ( DDSCL_MULTITHREADED |
645 DDSCL_CREATEDEVICEWINDOW |
650 DDSCL_SETDEVICEWINDOW |
655 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
656 LeaveCriticalSection(&ddraw_cs);
657 return DDERR_INVALIDPARAMS;
660 hr = ddraw_set_focus_window(This, hwnd);
661 LeaveCriticalSection(&ddraw_cs);
665 if(cooplevel & DDSCL_EXCLUSIVE)
667 if( !(cooplevel & DDSCL_FULLSCREEN) || !hwnd )
669 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN and a window\n", This);
670 LeaveCriticalSection(&ddraw_cs);
671 return DDERR_INVALIDPARAMS;
675 if ((This->cooperative_level & DDSCL_EXCLUSIVE)
676 && (hwnd != window || !(cooplevel & DDSCL_EXCLUSIVE)))
677 wined3d_device_release_focus_window(This->wined3d_device);
679 if ((cooplevel & DDSCL_FULLSCREEN) != (This->cooperative_level & DDSCL_FULLSCREEN) || hwnd != window)
681 if (This->cooperative_level & DDSCL_FULLSCREEN)
682 wined3d_device_restore_fullscreen_window(This->wined3d_device, window);
684 if (cooplevel & DDSCL_FULLSCREEN)
686 WINED3DDISPLAYMODE display_mode;
688 wined3d_get_adapter_display_mode(This->wineD3D, WINED3DADAPTER_DEFAULT, &display_mode);
689 wined3d_device_setup_fullscreen_window(This->wined3d_device, hwnd,
690 display_mode.Width, display_mode.Height);
694 if ((cooplevel & DDSCL_EXCLUSIVE)
695 && (hwnd != window || !(This->cooperative_level & DDSCL_EXCLUSIVE)))
697 hr = wined3d_device_acquire_focus_window(This->wined3d_device, hwnd);
700 ERR("Failed to acquire focus window, hr %#x.\n", hr);
701 LeaveCriticalSection(&ddraw_cs);
706 /* Don't override focus windows or private device windows */
707 if (hwnd && !This->focuswindow && !This->devicewindow && (hwnd != window))
708 This->dest_window = hwnd;
710 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
712 /* Don't create a device window if a focus window is set */
713 if( !(This->focuswindow) )
715 HWND devicewindow = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DDraw device window",
716 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
717 NULL, NULL, NULL, NULL);
720 ERR("Failed to create window, last error %#x.\n", GetLastError());
721 LeaveCriticalSection(&ddraw_cs);
725 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
726 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
728 This->devicewindow = devicewindow;
729 This->dest_window = devicewindow;
733 if (cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
734 wined3d_device_set_multithreaded(This->wined3d_device);
736 /* Unhandled flags */
737 if(cooplevel & DDSCL_ALLOWREBOOT)
738 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
739 if(cooplevel & DDSCL_ALLOWMODEX)
740 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
741 if(cooplevel & DDSCL_FPUSETUP)
742 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
744 /* Store the cooperative_level */
745 This->cooperative_level = cooplevel;
746 TRACE("SetCooperativeLevel retuning DD_OK\n");
747 LeaveCriticalSection(&ddraw_cs);
751 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
753 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
755 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
757 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
760 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
762 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
764 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
766 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
769 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
771 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
773 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
775 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
778 /*****************************************************************************
780 * Helper function for SetDisplayMode and RestoreDisplayMode
782 * Implements DirectDraw's SetDisplayMode, but ignores the value of
783 * ForceRefreshRate, since it is already handled by
784 * ddraw7_SetDisplayMode. RestoreDisplayMode can use this function
785 * without worrying that ForceRefreshRate will override the refresh rate. For
786 * argument and return value documentation, see
787 * ddraw7_SetDisplayMode.
789 *****************************************************************************/
790 static HRESULT ddraw_set_display_mode(IDirectDrawImpl *ddraw, DWORD Width, DWORD Height,
791 DWORD BPP, DWORD RefreshRate, DWORD Flags)
793 enum wined3d_format_id format;
794 WINED3DDISPLAYMODE Mode;
797 TRACE("ddraw %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n", ddraw, Width,
798 Height, BPP, RefreshRate, Flags);
800 EnterCriticalSection(&ddraw_cs);
801 if( !Width || !Height )
803 ERR("Width %u, Height %u, what to do?\n", Width, Height);
804 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
805 LeaveCriticalSection(&ddraw_cs);
811 case 8: format = WINED3DFMT_P8_UINT; break;
812 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
813 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
814 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
815 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
816 default: format = WINED3DFMT_UNKNOWN; break;
819 if (FAILED(hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &Mode)))
821 ERR("Failed to get current display mode, hr %#x.\n", hr);
823 else if (Mode.Width == Width
824 && Mode.Height == Height
825 && Mode.Format == format
826 && Mode.RefreshRate == RefreshRate)
828 TRACE("Skipping redundant mode setting call.\n");
829 LeaveCriticalSection(&ddraw_cs);
833 /* Check the exclusive mode
834 if(!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
835 return DDERR_NOEXCLUSIVEMODE;
836 * This is WRONG. Don't know if the SDK is completely
837 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
838 * is returned, but Half-Life 1.1.1.1 (Steam version)
843 Mode.Height = Height;
844 Mode.RefreshRate = RefreshRate;
845 Mode.Format = format;
847 /* TODO: The possible return values from msdn suggest that
848 * the screen mode can't be changed if a surface is locked
849 * or some drawing is in progress
852 /* TODO: Lose the primary surface */
853 hr = wined3d_device_set_display_mode(ddraw->wined3d_device, 0, &Mode);
855 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
856 SetWindowPos(ddraw->dest_window, HWND_TOP, 0, 0, Width, Height, SWP_SHOWWINDOW | SWP_NOACTIVATE);
858 LeaveCriticalSection(&ddraw_cs);
861 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
866 /*****************************************************************************
867 * IDirectDraw7::SetDisplayMode
869 * Sets the display screen resolution, color depth and refresh frequency
870 * when in fullscreen mode (in theory).
871 * Possible return values listed in the SDK suggest that this method fails
872 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
873 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
874 * It seems to be valid to pass 0 for With and Height, this has to be tested
875 * It could mean that the current video mode should be left as-is. (But why
879 * Height, Width: Screen dimension
880 * BPP: Color depth in Bits per pixel
881 * Refreshrate: Screen refresh rate
887 *****************************************************************************/
888 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD Width, DWORD Height,
889 DWORD BPP, DWORD RefreshRate, DWORD Flags)
891 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
893 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
894 iface, Width, Height, BPP, RefreshRate, Flags);
896 if (force_refresh_rate != 0)
898 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
899 RefreshRate, force_refresh_rate);
900 RefreshRate = force_refresh_rate;
903 return ddraw_set_display_mode(This, Width, Height, BPP, RefreshRate, Flags);
906 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
907 DWORD bpp, DWORD refresh_rate, DWORD flags)
909 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
911 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
912 iface, width, height, bpp, refresh_rate, flags);
914 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
917 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
918 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
920 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
922 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
923 iface, width, height, bpp, refresh_rate, flags);
925 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
928 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
930 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
932 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
934 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, 0, 0);
937 /*****************************************************************************
938 * IDirectDraw7::RestoreDisplayMode
940 * Restores the display mode to what it was at creation time. Basically.
942 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
943 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
944 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
945 * -> DD_1 is released. The screen should be left at 1024x768x32.
946 * -> DD_2 is released. The screen should be set to 1400x1050x32
947 * This case is unhandled right now, but Empire Earth does it this way.
948 * (But perhaps there is something in SetCooperativeLevel to prevent this)
950 * The msdn says that this method resets the display mode to what it was before
951 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
955 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
957 *****************************************************************************/
958 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
960 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
962 TRACE("iface %p.\n", iface);
964 return ddraw_set_display_mode(This, This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
967 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
969 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
971 TRACE("iface %p.\n", iface);
973 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
976 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
978 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
980 TRACE("iface %p.\n", iface);
982 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
985 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
987 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
989 TRACE("iface %p.\n", iface);
991 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
994 /*****************************************************************************
995 * IDirectDraw7::GetCaps
997 * Returns the drives capabilities
999 * Used for version 1, 2, 4 and 7
1002 * DriverCaps: Structure to write the Hardware accelerated caps to
1003 * HelCaps: Structure to write the emulation caps to
1006 * This implementation returns DD_OK only
1008 *****************************************************************************/
1009 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1011 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1013 WINED3DCAPS winecaps;
1015 DDSCAPS2 ddscaps = {0, 0, 0, 0};
1017 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1019 /* One structure must be != NULL */
1020 if( (!DriverCaps) && (!HELCaps) )
1022 ERR("(%p) Invalid params to ddraw7_GetCaps\n", This);
1023 return DDERR_INVALIDPARAMS;
1026 memset(&caps, 0, sizeof(caps));
1027 memset(&winecaps, 0, sizeof(winecaps));
1028 caps.dwSize = sizeof(caps);
1029 EnterCriticalSection(&ddraw_cs);
1030 hr = wined3d_device_get_device_caps(This->wined3d_device, &winecaps);
1033 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1034 LeaveCriticalSection(&ddraw_cs);
1038 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1039 LeaveCriticalSection(&ddraw_cs);
1041 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1045 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
1046 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
1047 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
1048 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
1049 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
1050 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
1051 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
1052 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
1053 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
1054 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
1055 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
1056 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
1057 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
1058 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
1059 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
1061 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
1064 if(DefaultSurfaceType == SURFACE_GDI) {
1065 caps.dwCaps &= ~DDCAPS_3D;
1066 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
1068 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
1069 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1070 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
1075 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1076 if (TRACE_ON(ddraw))
1078 TRACE("Driver Caps :\n");
1079 DDRAW_dump_DDCAPS(DriverCaps);
1085 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1086 if (TRACE_ON(ddraw))
1088 TRACE("HEL Caps :\n");
1089 DDRAW_dump_DDCAPS(HELCaps);
1096 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1098 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1100 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1102 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1105 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1107 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1109 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1111 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1114 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1116 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1118 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1120 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1123 /*****************************************************************************
1124 * IDirectDraw7::Compact
1126 * No idea what it does, MSDN says it's not implemented.
1129 * DD_OK, but this is unchecked
1131 *****************************************************************************/
1132 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1134 TRACE("iface %p.\n", iface);
1139 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1141 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1143 TRACE("iface %p.\n", iface);
1145 return ddraw7_Compact(&This->IDirectDraw7_iface);
1148 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1150 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1152 TRACE("iface %p.\n", iface);
1154 return ddraw7_Compact(&This->IDirectDraw7_iface);
1157 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1159 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1161 TRACE("iface %p.\n", iface);
1163 return ddraw7_Compact(&This->IDirectDraw7_iface);
1166 /*****************************************************************************
1167 * IDirectDraw7::GetDisplayMode
1169 * Returns information about the current display mode
1171 * Exists in Version 1, 2, 4 and 7
1174 * DDSD: Address of a surface description structure to write the info to
1179 *****************************************************************************/
1180 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1182 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1184 WINED3DDISPLAYMODE Mode;
1187 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1189 EnterCriticalSection(&ddraw_cs);
1190 /* This seems sane */
1193 LeaveCriticalSection(&ddraw_cs);
1194 return DDERR_INVALIDPARAMS;
1197 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
1198 * so one method can be used for all versions (Hopefully) */
1199 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &Mode);
1202 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
1203 LeaveCriticalSection(&ddraw_cs);
1207 Size = DDSD->dwSize;
1208 memset(DDSD, 0, Size);
1210 DDSD->dwSize = Size;
1211 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1212 DDSD->dwWidth = Mode.Width;
1213 DDSD->dwHeight = Mode.Height;
1214 DDSD->u2.dwRefreshRate = 60;
1215 DDSD->ddsCaps.dwCaps = 0;
1216 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1217 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
1218 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1222 TRACE("Returning surface desc :\n");
1223 DDRAW_dump_surface_desc(DDSD);
1226 LeaveCriticalSection(&ddraw_cs);
1230 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1232 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1234 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1236 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, surface_desc);
1239 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1241 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1243 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1245 /* FIXME: Test sizes, properly convert surface_desc */
1246 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1249 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1251 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1253 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1255 /* FIXME: Test sizes, properly convert surface_desc */
1256 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1259 /*****************************************************************************
1260 * IDirectDraw7::GetFourCCCodes
1262 * Returns an array of supported FourCC codes.
1264 * Exists in Version 1, 2, 4 and 7
1267 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1268 * of enumerated codes
1269 * Codes: Pointer to an array of DWORDs where the supported codes are written
1273 * Always returns DD_OK, as it's a stub for now
1275 *****************************************************************************/
1276 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1278 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1279 static const enum wined3d_format_id formats[] =
1281 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1282 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1283 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1285 DWORD count = 0, i, outsize;
1287 WINED3DDISPLAYMODE d3ddm;
1289 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1291 wined3d_device_get_display_mode(This->wined3d_device, 0, &d3ddm);
1293 outsize = NumCodes && Codes ? *NumCodes : 0;
1295 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1297 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, WINED3DDEVTYPE_HAL,
1298 d3ddm.Format, 0, WINED3DRTYPE_SURFACE, formats[i], DefaultSurfaceType);
1301 if (count < outsize)
1302 Codes[count] = formats[i];
1307 TRACE("Returning %u FourCC codes\n", count);
1314 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1316 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1318 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1320 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1323 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1325 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1327 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1329 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1332 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1334 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1336 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1338 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1341 /*****************************************************************************
1342 * IDirectDraw7::GetMonitorFrequency
1344 * Returns the monitor's frequency
1346 * Exists in Version 1, 2, 4 and 7
1349 * Freq: Pointer to a DWORD to write the frequency to
1352 * Always returns DD_OK
1354 *****************************************************************************/
1355 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *Freq)
1357 FIXME("iface %p, frequency %p stub!\n", iface, Freq);
1359 /* Ideally this should be in WineD3D, as it concerns the screen setup,
1360 * but for now this should make the games happy
1366 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1368 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1370 TRACE("iface %p, frequency %p.\n", iface, frequency);
1372 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1375 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1377 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1379 TRACE("iface %p, frequency %p.\n", iface, frequency);
1381 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1384 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1386 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1388 TRACE("iface %p, frequency %p.\n", iface, frequency);
1390 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1393 /*****************************************************************************
1394 * IDirectDraw7::GetVerticalBlankStatus
1396 * Returns the Vertical blank status of the monitor. This should be in WineD3D
1397 * too basically, but as it's a semi stub, I didn't create a function there
1400 * status: Pointer to a BOOL to be filled with the vertical blank status
1404 * DDERR_INVALIDPARAMS if status is NULL
1406 *****************************************************************************/
1407 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1409 static BOOL fake_vblank;
1411 TRACE("iface %p, status %p.\n", iface, status);
1414 return DDERR_INVALIDPARAMS;
1416 *status = fake_vblank;
1417 fake_vblank = !fake_vblank;
1422 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1424 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1426 TRACE("iface %p, status %p.\n", iface, status);
1428 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1431 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1433 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1435 TRACE("iface %p, status %p.\n", iface, status);
1437 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1440 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1442 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1444 TRACE("iface %p, status %p.\n", iface, status);
1446 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1449 /*****************************************************************************
1450 * IDirectDraw7::GetAvailableVidMem
1452 * Returns the total and free video memory
1455 * Caps: Specifies the memory type asked for
1456 * total: Pointer to a DWORD to be filled with the total memory
1457 * free: Pointer to a DWORD to be filled with the free memory
1461 * DDERR_INVALIDPARAMS of free and total are NULL
1463 *****************************************************************************/
1464 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total,
1467 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1470 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1474 TRACE("(%p) Asked for memory with description: ", This);
1475 DDRAW_dump_DDSCAPS2(Caps);
1477 EnterCriticalSection(&ddraw_cs);
1479 /* Todo: System memory vs local video memory vs non-local video memory
1480 * The MSDN also mentions differences between texture memory and other
1481 * resources, but that's not important
1484 if( (!total) && (!free) )
1486 LeaveCriticalSection(&ddraw_cs);
1487 return DDERR_INVALIDPARAMS;
1491 *free = wined3d_device_get_available_texture_mem(This->wined3d_device);
1494 WINED3DADAPTER_IDENTIFIER desc = {0};
1496 hr = wined3d_get_adapter_identifier(This->wineD3D, WINED3DADAPTER_DEFAULT, 0, &desc);
1497 *total = desc.video_memory;
1500 LeaveCriticalSection(&ddraw_cs);
1504 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1505 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1507 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1509 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1511 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, caps, total, free);
1514 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1515 DDSCAPS *caps, DWORD *total, DWORD *free)
1517 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1520 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1522 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1523 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, &caps2, total, free);
1526 /*****************************************************************************
1527 * IDirectDraw7::Initialize
1529 * Initializes a DirectDraw interface.
1532 * GUID: Interface identifier. Well, don't know what this is really good
1536 * Returns DD_OK on the first call,
1537 * DDERR_ALREADYINITIALIZED on repeated calls
1539 *****************************************************************************/
1540 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *Guid)
1542 FIXME("iface %p, guid %s stub!\n", iface, debugstr_guid(Guid));
1544 return DDERR_ALREADYINITIALIZED;
1547 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1549 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1551 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1553 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1556 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1558 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1560 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1562 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1565 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1567 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1569 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1571 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1574 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1576 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1581 /*****************************************************************************
1582 * IDirectDraw7::FlipToGDISurface
1584 * "Makes the surface that the GDI writes to the primary surface"
1585 * Looks like some windows specific thing we don't have to care about.
1586 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1587 * show error boxes ;)
1588 * Well, just return DD_OK.
1591 * Always returns DD_OK
1593 *****************************************************************************/
1594 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1596 FIXME("iface %p stub!\n", iface);
1601 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1603 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1605 TRACE("iface %p.\n", iface);
1607 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1610 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1612 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1614 TRACE("iface %p.\n", iface);
1616 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1619 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1621 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1623 TRACE("iface %p.\n", iface);
1625 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1628 /*****************************************************************************
1629 * IDirectDraw7::WaitForVerticalBlank
1631 * This method allows applications to get in sync with the vertical blank
1633 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1634 * redraw the screen, most likely because of this stub
1637 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1638 * or DDWAITVB_BLOCKEND
1639 * h: Not used, according to MSDN
1642 * Always returns DD_OK
1644 *****************************************************************************/
1645 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1649 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1651 /* This function is called often, so print the fixme only once */
1654 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1658 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1659 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1660 return DDERR_UNSUPPORTED; /* unchecked */
1665 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1667 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1669 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1671 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1674 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1676 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1678 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1680 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1683 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1685 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1687 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1689 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1692 /*****************************************************************************
1693 * IDirectDraw7::GetScanLine
1695 * Returns the scan line that is being drawn on the monitor
1698 * Scanline: Address to write the scan line value to
1701 * Always returns DD_OK
1703 *****************************************************************************/
1704 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1706 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1707 static DWORD cur_scanline;
1708 static BOOL hide = FALSE;
1709 WINED3DDISPLAYMODE Mode;
1711 TRACE("iface %p, line %p.\n", iface, Scanline);
1713 /* This function is called often, so print the fixme only once */
1716 FIXME("iface %p, line %p partial stub!\n", iface, Scanline);
1720 EnterCriticalSection(&ddraw_cs);
1721 wined3d_device_get_display_mode(This->wined3d_device, 0, &Mode);
1722 LeaveCriticalSection(&ddraw_cs);
1724 /* Fake the line sweeping of the monitor */
1725 /* FIXME: We should synchronize with a source to keep the refresh rate */
1726 *Scanline = cur_scanline++;
1727 /* Assume 20 scan lines in the vertical blank */
1728 if (cur_scanline >= Mode.Height + 20)
1734 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1736 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1738 TRACE("iface %p, line %p.\n", iface, line);
1740 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1743 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1745 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1747 TRACE("iface %p, line %p.\n", iface, line);
1749 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1752 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1754 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1756 TRACE("iface %p, line %p.\n", iface, line);
1758 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1761 /*****************************************************************************
1762 * IDirectDraw7::TestCooperativeLevel
1764 * Informs the application about the state of the video adapter, depending
1765 * on the cooperative level
1768 * DD_OK if the device is in a sane state
1769 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1770 * if the state is not correct(See below)
1772 *****************************************************************************/
1773 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1775 TRACE("iface %p.\n", iface);
1780 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
1782 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1784 TRACE("iface %p.\n", iface);
1786 return ddraw7_TestCooperativeLevel(&This->IDirectDraw7_iface);
1789 /*****************************************************************************
1790 * IDirectDraw7::GetGDISurface
1792 * Returns the surface that GDI is treating as the primary surface.
1793 * For Wine this is the front buffer
1796 * GDISurface: Address to write the surface pointer to
1799 * DD_OK if the surface was found
1800 * DDERR_NOTFOUND if the GDI surface wasn't found
1802 *****************************************************************************/
1803 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
1805 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1807 TRACE("iface %p, surface %p.\n", iface, GDISurface);
1809 EnterCriticalSection(&ddraw_cs);
1811 if (!(*GDISurface = &This->primary->IDirectDrawSurface7_iface))
1813 WARN("Primary not created yet.\n");
1814 LeaveCriticalSection(&ddraw_cs);
1815 return DDERR_NOTFOUND;
1817 IDirectDrawSurface7_AddRef(*GDISurface);
1819 LeaveCriticalSection(&ddraw_cs);
1823 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
1825 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1826 IDirectDrawSurface7 *surface7;
1827 IDirectDrawSurfaceImpl *surface_impl;
1830 TRACE("iface %p, surface %p.\n", iface, surface);
1832 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
1838 surface_impl = impl_from_IDirectDrawSurface7(surface7);
1839 *surface = &surface_impl->IDirectDrawSurface4_iface;
1840 IDirectDrawSurface4_AddRef(*surface);
1841 IDirectDrawSurface7_Release(surface7);
1846 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
1848 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1849 IDirectDrawSurface7 *surface7;
1850 IDirectDrawSurfaceImpl *surface_impl;
1853 TRACE("iface %p, surface %p.\n", iface, surface);
1855 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
1861 surface_impl = impl_from_IDirectDrawSurface7(surface7);
1862 *surface = &surface_impl->IDirectDrawSurface_iface;
1863 IDirectDrawSurface_AddRef(*surface);
1864 IDirectDrawSurface7_Release(surface7);
1869 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
1871 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1872 IDirectDrawSurface7 *surface7;
1873 IDirectDrawSurfaceImpl *surface_impl;
1876 TRACE("iface %p, surface %p.\n", iface, surface);
1878 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
1884 surface_impl = impl_from_IDirectDrawSurface7(surface7);
1885 *surface = &surface_impl->IDirectDrawSurface_iface;
1886 IDirectDrawSurface_AddRef(*surface);
1887 IDirectDrawSurface7_Release(surface7);
1892 struct displaymodescallback_context
1894 LPDDENUMMODESCALLBACK func;
1898 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
1900 struct displaymodescallback_context *cbcontext = context;
1903 DDSD2_to_DDSD(surface_desc, &desc);
1904 return cbcontext->func(&desc, cbcontext->context);
1907 /*****************************************************************************
1908 * IDirectDraw7::EnumDisplayModes
1910 * Enumerates the supported Display modes. The modes can be filtered with
1911 * the DDSD parameter.
1914 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
1915 * versions (3 and older?) this is reserved and must be 0.
1916 * DDSD: Surface description to filter the modes
1917 * Context: Pointer passed back to the callback function
1918 * cb: Application-provided callback function
1922 * DDERR_INVALIDPARAMS if the callback wasn't set
1924 *****************************************************************************/
1925 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
1926 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
1928 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1929 unsigned int modenum, fmt;
1930 WINED3DDISPLAYMODE mode;
1931 DDSURFACEDESC2 callback_sd;
1932 WINED3DDISPLAYMODE *enum_modes = NULL;
1933 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1934 DDPIXELFORMAT pixelformat;
1936 static const enum wined3d_format_id checkFormatList[] =
1938 WINED3DFMT_B8G8R8X8_UNORM,
1939 WINED3DFMT_B5G6R5_UNORM,
1943 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
1944 iface, Flags, DDSD, Context, cb);
1946 EnterCriticalSection(&ddraw_cs);
1947 /* This looks sane */
1950 LeaveCriticalSection(&ddraw_cs);
1951 return DDERR_INVALIDPARAMS;
1954 if(!(Flags & DDEDM_REFRESHRATES))
1956 enum_mode_array_size = 16;
1957 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1960 ERR("Out of memory\n");
1961 LeaveCriticalSection(&ddraw_cs);
1962 return DDERR_OUTOFMEMORY;
1966 pixelformat.dwSize = sizeof(pixelformat);
1967 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1970 while (wined3d_enum_adapter_modes(This->wineD3D, WINED3DADAPTER_DEFAULT,
1971 checkFormatList[fmt], modenum++, &mode) == WINED3D_OK)
1973 PixelFormat_WineD3DtoDD(&pixelformat, mode.Format);
1976 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1977 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1978 if(DDSD->dwFlags & DDSD_REFRESHRATE && mode.RefreshRate != DDSD->u2.dwRefreshRate) continue;
1979 if (DDSD->dwFlags & DDSD_PIXELFORMAT &&
1980 pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount) continue;
1983 if(!(Flags & DDEDM_REFRESHRATES))
1985 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1986 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1987 * to be reduced to a single unique result in such case.
1992 for (i = 0; i < enum_mode_count; i++)
1994 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1995 enum_modes[i].Format == mode.Format)
2005 memset(&callback_sd, 0, sizeof(callback_sd));
2006 callback_sd.dwSize = sizeof(callback_sd);
2007 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2009 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2010 if(Flags & DDEDM_REFRESHRATES)
2012 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
2015 callback_sd.dwWidth = mode.Width;
2016 callback_sd.dwHeight = mode.Height;
2018 callback_sd.u4.ddpfPixelFormat=pixelformat;
2020 /* Calc pitch and DWORD align like MSDN says */
2021 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
2022 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2024 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2025 callback_sd.u2.dwRefreshRate);
2027 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2029 TRACE("Application asked to terminate the enumeration\n");
2030 HeapFree(GetProcessHeap(), 0, enum_modes);
2031 LeaveCriticalSection(&ddraw_cs);
2035 if(!(Flags & DDEDM_REFRESHRATES))
2037 if (enum_mode_count == enum_mode_array_size)
2039 WINED3DDISPLAYMODE *new_enum_modes;
2041 enum_mode_array_size *= 2;
2042 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
2044 if (!new_enum_modes)
2046 ERR("Out of memory\n");
2047 HeapFree(GetProcessHeap(), 0, enum_modes);
2048 LeaveCriticalSection(&ddraw_cs);
2049 return DDERR_OUTOFMEMORY;
2052 enum_modes = new_enum_modes;
2055 enum_modes[enum_mode_count++] = mode;
2060 TRACE("End of enumeration\n");
2061 HeapFree(GetProcessHeap(), 0, enum_modes);
2062 LeaveCriticalSection(&ddraw_cs);
2066 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2067 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2069 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2071 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2072 iface, flags, surface_desc, context, callback);
2074 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, surface_desc, context, callback);
2077 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2078 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2080 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
2081 struct displaymodescallback_context cbcontext;
2082 DDSURFACEDESC2 surface_desc2;
2084 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2085 iface, flags, surface_desc, context, callback);
2087 cbcontext.func = callback;
2088 cbcontext.context = context;
2090 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2091 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags,
2092 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2095 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2096 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2098 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
2099 struct displaymodescallback_context cbcontext;
2100 DDSURFACEDESC2 surface_desc2;
2102 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2103 iface, flags, surface_desc, context, callback);
2105 cbcontext.func = callback;
2106 cbcontext.context = context;
2108 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2109 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags,
2110 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2113 /*****************************************************************************
2114 * IDirectDraw7::EvaluateMode
2116 * Used with IDirectDraw7::StartModeTest to test video modes.
2117 * EvaluateMode is used to pass or fail a mode, and continue with the next
2121 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2122 * Timeout: Returns the amount of seconds left before the mode would have
2123 * been failed automatically
2126 * This implementation always DD_OK, because it's a stub
2128 *****************************************************************************/
2129 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2131 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2133 /* When implementing this, implement it in WineD3D */
2138 /*****************************************************************************
2139 * IDirectDraw7::GetDeviceIdentifier
2141 * Returns the device identifier, which gives information about the driver
2142 * Our device identifier is defined at the beginning of this file.
2145 * DDDI: Address for the returned structure
2146 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2149 * On success it returns DD_OK
2150 * DDERR_INVALIDPARAMS if DDDI is NULL
2152 *****************************************************************************/
2153 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2154 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2156 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2159 return DDERR_INVALIDPARAMS;
2161 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2162 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2163 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2164 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2165 * bytes too long. So only copy the relevant part of the structure
2168 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2172 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2173 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2175 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2176 DDDEVICEIDENTIFIER2 identifier2;
2179 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2181 hr = ddraw7_GetDeviceIdentifier(&This->IDirectDraw7_iface, &identifier2, flags);
2182 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2187 /*****************************************************************************
2188 * IDirectDraw7::GetSurfaceFromDC
2190 * Returns the Surface for a GDI device context handle.
2191 * Is this related to IDirectDrawSurface::GetDC ???
2194 * hdc: hdc to return the surface for
2195 * Surface: Address to write the surface pointer to
2198 * Always returns DD_OK because it's a stub
2200 *****************************************************************************/
2201 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc,
2202 IDirectDrawSurface7 **Surface)
2204 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2205 struct wined3d_surface *wined3d_surface;
2208 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2210 if (!Surface) return E_INVALIDARG;
2212 hr = wined3d_device_get_surface_from_dc(This->wined3d_device, hdc, &wined3d_surface);
2215 TRACE("No surface found for dc %p.\n", hdc);
2217 return DDERR_NOTFOUND;
2220 *Surface = wined3d_surface_get_parent(wined3d_surface);
2221 IDirectDrawSurface7_AddRef(*Surface);
2222 TRACE("Returning surface %p.\n", Surface);
2226 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2227 IDirectDrawSurface4 **surface)
2229 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2230 IDirectDrawSurface7 *surface7;
2231 IDirectDrawSurfaceImpl *surface_impl;
2234 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2236 if (!surface) return E_INVALIDARG;
2238 hr = ddraw7_GetSurfaceFromDC(&This->IDirectDraw7_iface, dc, &surface7);
2244 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2245 /* Tests say this is true */
2246 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2247 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2248 IDirectDrawSurface7_Release(surface7);
2253 /*****************************************************************************
2254 * IDirectDraw7::RestoreAllSurfaces
2256 * Calls the restore method of all surfaces
2261 * Always returns DD_OK because it's a stub
2263 *****************************************************************************/
2264 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2266 FIXME("iface %p stub!\n", iface);
2268 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2269 * get their parent and call their restore method. Do not implement
2270 * it in WineD3D, as restoring a surface means re-creating the
2276 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2278 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2280 TRACE("iface %p.\n", iface);
2282 return ddraw7_RestoreAllSurfaces(&This->IDirectDraw7_iface);
2285 /*****************************************************************************
2286 * IDirectDraw7::StartModeTest
2288 * Tests the specified video modes to update the system registry with
2289 * refresh rate information. StartModeTest starts the mode test,
2290 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2291 * isn't called within 15 seconds, the mode is failed automatically
2293 * As refresh rates are handled by the X server, I don't think this
2294 * Method is important
2297 * Modes: An array of mode specifications
2298 * NumModes: The number of modes in Modes
2299 * Flags: Some flags...
2302 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2303 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2306 *****************************************************************************/
2307 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2309 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2310 iface, Modes, NumModes, Flags);
2312 /* This looks sane */
2313 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2315 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2316 * As it is not, DDERR_TESTFINISHED is returned
2317 * (hopefully that's correct
2319 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2320 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2326 /*****************************************************************************
2327 * ddraw_create_surface
2329 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2330 * with the passed parameters.
2333 * DDSD: Description of the surface to create
2334 * Surf: Address to store the interface pointer at
2339 *****************************************************************************/
2340 static HRESULT ddraw_create_surface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD,
2341 IDirectDrawSurfaceImpl **ppSurf, UINT level, UINT version)
2345 TRACE("ddraw %p, surface_desc %p, surface %p, level %u.\n",
2346 This, pDDSD, ppSurf, level);
2348 if (TRACE_ON(ddraw))
2350 TRACE(" (%p) Requesting surface desc :\n", This);
2351 DDRAW_dump_surface_desc(pDDSD);
2354 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && DefaultSurfaceType != SURFACE_OPENGL)
2356 WARN("The application requests a 3D capable surface, but a non-OpenGL surface type was set in the registry.\n");
2357 /* Do not fail surface creation, only fail 3D device creation. */
2360 /* Create the Surface object */
2361 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2364 ERR("(%p) Error allocating memory for a surface\n", This);
2365 return DDERR_OUTOFVIDEOMEMORY;
2368 hr = ddraw_surface_init(*ppSurf, This, pDDSD, level, version);
2371 WARN("Failed to initialize surface, hr %#x.\n", hr);
2372 HeapFree(GetProcessHeap(), 0, *ppSurf);
2376 /* Increase the surface counter, and attach the surface */
2377 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2379 TRACE("Created surface %p.\n", *ppSurf);
2383 /*****************************************************************************
2384 * CreateAdditionalSurfaces
2386 * Creates a new mipmap chain.
2389 * root: Root surface to attach the newly created chain to
2390 * count: number of surfaces to create
2391 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2392 * effects on the caller
2393 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2394 * creates an additional surface without the mipmapping flags
2396 *****************************************************************************/
2398 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2399 IDirectDrawSurfaceImpl *root,
2401 DDSURFACEDESC2 DDSD,
2402 BOOL CubeFaceRoot, UINT version)
2404 UINT i, j, level = 0;
2406 IDirectDrawSurfaceImpl *last = root;
2408 for(i = 0; i < count; i++)
2410 IDirectDrawSurfaceImpl *object2 = NULL;
2412 /* increase the mipmap level, but only if a mipmap is created
2413 * In this case, also halve the size
2415 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2418 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2419 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2420 /* Set the mipmap sublevel flag according to msdn */
2421 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2425 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2427 CubeFaceRoot = FALSE;
2429 hr = ddraw_create_surface(This, &DDSD, &object2, level, version);
2435 /* Add the new surface to the complex attachment array */
2436 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2438 if(last->complex_array[j]) continue;
2439 last->complex_array[j] = object2;
2444 /* Remove the (possible) back buffer cap from the new surface description,
2445 * because only one surface in the flipping chain is a back buffer, one
2446 * is a front buffer, the others are just primary surfaces.
2448 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2453 /*****************************************************************************
2454 * ddraw_attach_d3d_device
2456 * Initializes the D3D capabilities of WineD3D
2459 * primary: The primary surface for D3D
2465 *****************************************************************************/
2466 static HRESULT ddraw_attach_d3d_device(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary,
2467 WINED3DPRESENT_PARAMETERS *presentation_parameters)
2469 HWND window = presentation_parameters->hDeviceWindow;
2472 TRACE("ddraw %p, primary %p.\n", ddraw, primary);
2474 if (!window || window == GetDesktopWindow())
2476 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
2477 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
2478 NULL, NULL, NULL, NULL);
2481 ERR("Failed to create window, last error %#x.\n", GetLastError());
2485 ShowWindow(window, SW_HIDE); /* Just to be sure */
2486 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
2488 presentation_parameters->hDeviceWindow = window;
2492 TRACE("Using existing window %p for Direct3D rendering.\n", window);
2494 ddraw->d3d_window = window;
2496 /* Store the future Render Target surface */
2497 ddraw->d3d_target = primary;
2499 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2500 * recursive loop until ram or emulated video memory is full. */
2501 ddraw->d3d_initialized = TRUE;
2502 hr = wined3d_device_init_3d(ddraw->wined3d_device, presentation_parameters);
2505 ddraw->d3d_target = NULL;
2506 ddraw->d3d_initialized = FALSE;
2510 ddraw->declArraySize = 2;
2511 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
2514 ERR("Error allocating an array for the converted vertex decls.\n");
2515 ddraw->declArraySize = 0;
2516 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
2517 return E_OUTOFMEMORY;
2520 TRACE("Successfully initialized 3D.\n");
2525 static HRESULT ddraw_create_gdi_swapchain(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary,
2526 WINED3DPRESENT_PARAMETERS *presentation_parameters)
2530 ddraw->d3d_target = primary;
2531 hr = wined3d_device_init_gdi(ddraw->wined3d_device, presentation_parameters);
2532 ddraw->d3d_target = NULL;
2535 WARN("Failed to initialize GDI ddraw implementation, hr %#x.\n", hr);
2536 primary->wined3d_swapchain = NULL;
2542 static HRESULT ddraw_create_swapchain(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *surface)
2544 WINED3DPRESENT_PARAMETERS presentation_parameters;
2545 IDirectDrawSurfaceImpl *target = surface;
2546 HRESULT hr = WINED3D_OK;
2550 TRACE("Using primary %p.\n", ddraw->primary);
2551 target = ddraw->primary;
2554 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2555 presentation_parameters.BackBufferWidth = target->surface_desc.dwWidth;
2556 presentation_parameters.BackBufferHeight = target->surface_desc.dwHeight;
2557 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&target->surface_desc.u4.ddpfPixelFormat);
2558 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2559 presentation_parameters.hDeviceWindow = ddraw->dest_window;
2560 presentation_parameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
2562 /* If the implementation is OpenGL and there's no d3ddevice, attach a
2563 * d3ddevice. But attach the d3ddevice only if the currently created
2564 * surface was a primary surface (2D app in 3D mode) or a 3DDEVICE surface
2565 * (3D app). The only case I can think of where this doesn't apply is when
2566 * a 2D app was configured by the user to run with OpenGL and it didn't
2567 * create the render target as first surface. In this case the render
2568 * target creation will cause the 3D init. */
2569 if (DefaultSurfaceType == SURFACE_OPENGL)
2571 TRACE("Attaching a D3DDevice, rendertarget = %p.\n", target);
2572 hr = ddraw_attach_d3d_device(ddraw, target, &presentation_parameters);
2574 else if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2576 hr = ddraw_create_gdi_swapchain(ddraw, target, &presentation_parameters);
2583 /*****************************************************************************
2584 * IDirectDraw7::CreateSurface
2586 * Creates a new IDirectDrawSurface object and returns its interface.
2588 * The surface connections with wined3d are a bit tricky. Basically it works
2591 * |------------------------| |-----------------|
2592 * | DDraw surface | | WineD3DSurface |
2594 * | WineD3DSurface |-------------->| |
2595 * | Child |<------------->| Parent |
2596 * |------------------------| |-----------------|
2598 * The DDraw surface is the parent of the wined3d surface, and it releases
2599 * the WineD3DSurface when the ddraw surface is destroyed.
2601 * However, for all surfaces which can be in a container in WineD3D,
2602 * we have to do this. These surfaces are usually complex surfaces,
2603 * so this concerns primary surfaces with a front and a back buffer,
2606 * |------------------------| |-----------------|
2607 * | DDraw surface | | Container |
2609 * | Child |<------------->| Parent |
2610 * | Texture |<------------->| |
2611 * | WineD3DSurface |<----| | Levels |<--|
2612 * | Complex connection | | | | |
2613 * |------------------------| | |-----------------| |
2617 * | |------------------| | |-----------------| |
2618 * | | IParent | |-------->| WineD3DSurface | |
2620 * | | Child |<------------->| Parent | |
2621 * | | | | Container |<--|
2622 * | |------------------| |-----------------| |
2624 * | |----------------------| |
2625 * | | DDraw surface 2 | |
2627 * |<->| Complex root Child | |
2629 * | | WineD3DSurface |<----| |
2630 * | |----------------------| | |
2632 * | |---------------------| | |-----------------| |
2633 * | | IParent | |----->| WineD3DSurface | |
2635 * | | Child |<---------->| Parent | |
2636 * | |---------------------| | Container |<--|
2637 * | |-----------------| |
2639 * | ---More surfaces can follow--- |
2641 * The reason is that the IWineD3DSwapchain(render target container)
2642 * and the IWineD3DTexure(Texture container) release the parents
2643 * of their surface's children, but by releasing the complex root
2644 * the surfaces which are complexly attached to it are destroyed
2645 * too. See IDirectDrawSurface::Release for a more detailed
2649 * DDSD: Description of the surface to create
2650 * Surf: Address to store the interface pointer at
2651 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2652 * aggregation, so it has to be NULL
2656 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2657 * DDERR_* if an error occurs
2659 *****************************************************************************/
2660 static HRESULT CreateSurface(IDirectDrawImpl *ddraw, DDSURFACEDESC2 *DDSD,
2661 IDirectDrawSurfaceImpl **Surf, IUnknown *UnkOuter, UINT version)
2663 IDirectDrawSurfaceImpl *object = NULL;
2665 LONG extra_surfaces = 0;
2666 DDSURFACEDESC2 desc2;
2667 WINED3DDISPLAYMODE Mode;
2668 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2670 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p.\n", ddraw, DDSD, Surf, UnkOuter);
2672 /* Some checks before we start */
2673 if (TRACE_ON(ddraw))
2675 TRACE(" (%p) Requesting surface desc :\n", ddraw);
2676 DDRAW_dump_surface_desc(DDSD);
2678 EnterCriticalSection(&ddraw_cs);
2680 if (UnkOuter != NULL)
2682 FIXME("(%p) : outer != NULL?\n", ddraw);
2683 LeaveCriticalSection(&ddraw_cs);
2684 return CLASS_E_NOAGGREGATION; /* unchecked */
2689 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", ddraw);
2690 LeaveCriticalSection(&ddraw_cs);
2691 return E_POINTER; /* unchecked */
2694 if (!(DDSD->dwFlags & DDSD_CAPS))
2696 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2697 DDSD->dwFlags |= DDSD_CAPS;
2700 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2702 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2703 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2706 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2708 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2709 WARN("(%p) Null surface pointer specified, ignore it!\n", ddraw);
2710 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2713 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2714 !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2716 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n",
2719 LeaveCriticalSection(&ddraw_cs);
2720 return DDERR_NOEXCLUSIVEMODE;
2723 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE))
2725 WARN("Application wanted to create back buffer primary surface\n");
2726 LeaveCriticalSection(&ddraw_cs);
2727 return DDERR_INVALIDCAPS;
2730 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2732 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2733 WARN("Application tries to put the surface in both system and video memory\n");
2734 LeaveCriticalSection(&ddraw_cs);
2736 return DDERR_INVALIDCAPS;
2739 /* Check cube maps but only if the size includes them */
2740 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2742 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2743 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2745 WARN("Cube map faces requested without cube map flag\n");
2746 LeaveCriticalSection(&ddraw_cs);
2747 return DDERR_INVALIDCAPS;
2749 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2750 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2752 WARN("Cube map without faces requested\n");
2753 LeaveCriticalSection(&ddraw_cs);
2754 return DDERR_INVALIDPARAMS;
2757 /* Quick tests confirm those can be created, but we don't do that yet */
2758 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2759 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2761 FIXME("Partial cube maps not supported yet\n");
2765 /* According to the msdn this flag is ignored by CreateSurface */
2766 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2767 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2769 /* Modify some flags */
2770 copy_to_surfacedesc2(&desc2, DDSD);
2771 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2773 /* Get the video mode from WineD3D - we will need it */
2774 hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &Mode);
2777 ERR("Failed to read display mode from wined3d\n");
2778 switch(ddraw->orig_bpp)
2781 Mode.Format = WINED3DFMT_P8_UINT;
2785 Mode.Format = WINED3DFMT_B5G5R5X1_UNORM;
2789 Mode.Format = WINED3DFMT_B5G6R5_UNORM;
2793 Mode.Format = WINED3DFMT_B8G8R8_UNORM;
2797 Mode.Format = WINED3DFMT_B8G8R8X8_UNORM;
2800 Mode.Width = ddraw->orig_width;
2801 Mode.Height = ddraw->orig_height;
2804 /* No pixelformat given? Use the current screen format */
2805 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2807 desc2.dwFlags |= DDSD_PIXELFORMAT;
2808 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2810 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2813 /* No Width or no Height? Use the original screen size
2815 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2816 !(desc2.dwFlags & DDSD_HEIGHT) )
2818 /* Invalid for non-render targets */
2819 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2821 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2823 LeaveCriticalSection(&ddraw_cs);
2824 return DDERR_INVALIDPARAMS;
2827 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2828 desc2.dwWidth = Mode.Width;
2829 desc2.dwHeight = Mode.Height;
2832 if (!desc2.dwWidth || !desc2.dwHeight)
2834 LeaveCriticalSection(&ddraw_cs);
2835 return DDERR_INVALIDPARAMS;
2838 /* Mipmap count fixes */
2839 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2841 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2843 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2845 /* Mipmap count is given, should not be 0 */
2846 if( desc2.u2.dwMipMapCount == 0 )
2848 LeaveCriticalSection(&ddraw_cs);
2849 return DDERR_INVALIDPARAMS;
2854 /* Undocumented feature: Create sublevels until
2855 * either the width or the height is 1
2857 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2858 desc2.dwWidth : desc2.dwHeight;
2859 desc2.u2.dwMipMapCount = 0;
2862 desc2.u2.dwMipMapCount += 1;
2869 /* Not-complex mipmap -> Mipmapcount = 1 */
2870 desc2.u2.dwMipMapCount = 1;
2872 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2874 /* There's a mipmap count in the created surface in any case */
2875 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2877 /* If no mipmap is given, the texture has only one level */
2879 /* The first surface is a front buffer, the back buffer is created afterwards */
2880 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2882 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2885 /* The root surface in a cube map is positive x */
2886 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2888 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2889 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2892 /* Create the first surface */
2893 hr = ddraw_create_surface(ddraw, &desc2, &object, 0, version);
2896 WARN("ddraw_create_surface failed, hr %#x.\n", hr);
2897 LeaveCriticalSection(&ddraw_cs);
2900 object->is_complex_root = TRUE;
2904 /* Create Additional surfaces if necessary
2905 * This applies to Primary surfaces which have a back buffer count
2906 * set, but not to mipmap textures. In case of Mipmap textures,
2907 * wineD3D takes care of the creation of additional surfaces
2909 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2911 extra_surfaces = DDSD->dwBackBufferCount;
2912 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2913 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2914 desc2.dwBackBufferCount = 0;
2918 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2920 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2921 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2922 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2923 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2924 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2925 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2926 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2927 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2928 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2929 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2930 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2931 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2932 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2933 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2934 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2935 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2936 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2939 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces, desc2, FALSE, version);
2942 /* This destroys and possibly created surfaces too */
2945 IDirectDrawSurface7_Release(&object->IDirectDrawSurface7_iface);
2947 else if (version == 4)
2949 IDirectDrawSurface4_Release(&object->IDirectDrawSurface4_iface);
2953 IDirectDrawSurface_Release(&object->IDirectDrawSurface_iface);
2955 LeaveCriticalSection(&ddraw_cs);
2959 if (!ddraw->d3d_initialized && desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
2961 if (FAILED(hr = ddraw_create_swapchain(ddraw, object)))
2963 IDirectDrawSurfaceImpl *release_surf;
2964 ERR("Failed to create swapchain, hr %#x.\n", hr);
2967 /* The earlier created surface structures are in an incomplete
2968 * state here. Wined3d holds the reference on the parents, and it
2969 * released them on the failure already. So the regular release
2970 * method implementation would fail on the attempt to destroy
2971 * either the parents or the swapchain. So free the surface here.
2972 * The surface structure here is a list, not a tree, because
2973 * onscreen targets cannot be cube textures. */
2976 release_surf = object;
2977 object = object->complex_array[0];
2978 ddraw_surface_destroy(release_surf);
2980 LeaveCriticalSection(&ddraw_cs);
2985 if (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2986 ddraw->primary = object;
2988 /* Create a WineD3DTexture if a texture was requested */
2989 if (desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2991 ddraw->tex_root = object;
2992 ddraw_surface_create_texture(object);
2993 ddraw->tex_root = NULL;
2996 LeaveCriticalSection(&ddraw_cs);
3000 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
3001 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
3003 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3004 IDirectDrawSurfaceImpl *impl;
3007 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3008 iface, surface_desc, surface, outer_unknown);
3010 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3012 WARN("Application supplied invalid surface descriptor\n");
3013 return DDERR_INVALIDPARAMS;
3016 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3018 if (TRACE_ON(ddraw))
3020 TRACE(" (%p) Requesting surface desc :\n", iface);
3021 DDRAW_dump_surface_desc(surface_desc);
3024 WARN("Application tried to create an explicit front or back buffer\n");
3025 return DDERR_INVALIDCAPS;
3028 hr = CreateSurface(This, surface_desc, &impl, outer_unknown, 7);
3035 *surface = &impl->IDirectDrawSurface7_iface;
3036 IDirectDraw7_AddRef(iface);
3037 impl->ifaceToRelease = (IUnknown *)iface;
3042 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
3043 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
3045 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3046 IDirectDrawSurfaceImpl *impl;
3049 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3050 iface, surface_desc, surface, outer_unknown);
3052 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3054 WARN("Application supplied invalid surface descriptor\n");
3055 return DDERR_INVALIDPARAMS;
3058 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3060 if (TRACE_ON(ddraw))
3062 TRACE(" (%p) Requesting surface desc :\n", iface);
3063 DDRAW_dump_surface_desc(surface_desc);
3066 WARN("Application tried to create an explicit front or back buffer\n");
3067 return DDERR_INVALIDCAPS;
3070 hr = CreateSurface(This, surface_desc, &impl, outer_unknown, 4);
3077 *surface = &impl->IDirectDrawSurface4_iface;
3078 IDirectDraw4_AddRef(iface);
3079 impl->ifaceToRelease = (IUnknown *)iface;
3084 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
3085 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3087 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3088 IDirectDrawSurfaceImpl *impl;
3090 DDSURFACEDESC2 surface_desc2;
3092 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3093 iface, surface_desc, surface, outer_unknown);
3095 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3097 WARN("Application supplied invalid surface descriptor\n");
3098 return DDERR_INVALIDPARAMS;
3101 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3102 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3104 if (TRACE_ON(ddraw))
3106 TRACE(" (%p) Requesting surface desc :\n", iface);
3107 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3110 WARN("Application tried to create an explicit front or back buffer\n");
3111 return DDERR_INVALIDCAPS;
3114 hr = CreateSurface(This, &surface_desc2, &impl, outer_unknown, 2);
3121 *surface = &impl->IDirectDrawSurface_iface;
3122 impl->ifaceToRelease = NULL;
3127 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3128 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3130 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3131 IDirectDrawSurfaceImpl *impl;
3133 DDSURFACEDESC2 surface_desc2;
3135 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3136 iface, surface_desc, surface, outer_unknown);
3138 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3140 WARN("Application supplied invalid surface descriptor\n");
3141 return DDERR_INVALIDPARAMS;
3144 /* Remove front buffer flag, this causes failure in v7, and its added to normal
3145 * primaries anyway. */
3146 surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3147 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3148 hr = CreateSurface(This, &surface_desc2, &impl, outer_unknown, 1);
3155 *surface = &impl->IDirectDrawSurface_iface;
3156 impl->ifaceToRelease = NULL;
3161 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3162 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3165 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3166 const DDPIXELFORMAT *provided)
3168 /* Some flags must be present in both or neither for a match. */
3169 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3170 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3171 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3173 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3176 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3179 if (requested->dwFlags & DDPF_FOURCC)
3180 if (requested->dwFourCC != provided->dwFourCC)
3183 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3184 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3185 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3188 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3189 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3190 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3193 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3194 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3197 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3198 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3200 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3203 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3204 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3210 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3219 #define CMP(FLAG, FIELD) \
3220 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3221 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3223 static const struct compare_info compare[] =
3225 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3226 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3228 CMP(CKDESTBLT, ddckCKDestBlt),
3229 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3230 CMP(CKSRCBLT, ddckCKSrcBlt),
3231 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3232 CMP(HEIGHT, dwHeight),
3233 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3234 CMP(LPSURFACE, lpSurface),
3235 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3236 CMP(PITCH, u1 /* lPitch */),
3237 /* PIXELFORMAT: manual */
3238 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3239 CMP(TEXTURESTAGE, dwTextureStage),
3240 CMP(WIDTH, dwWidth),
3241 /* ZBUFFERBITDEPTH: "obsolete" */
3248 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3251 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3253 if (requested->dwFlags & compare[i].flag
3254 && memcmp((const char *)provided + compare[i].offset,
3255 (const char *)requested + compare[i].offset,
3256 compare[i].size) != 0)
3260 if (requested->dwFlags & DDSD_PIXELFORMAT)
3262 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3263 &provided->u4.ddpfPixelFormat))
3270 #undef DDENUMSURFACES_SEARCHTYPE
3271 #undef DDENUMSURFACES_MATCHTYPE
3273 struct surfacescallback2_context
3275 LPDDENUMSURFACESCALLBACK2 func;
3279 struct surfacescallback_context
3281 LPDDENUMSURFACESCALLBACK func;
3285 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3286 DDSURFACEDESC2 *surface_desc, void *context)
3288 IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
3289 struct surfacescallback2_context *cbcontext = context;
3291 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3292 IDirectDrawSurface7_Release(surface);
3294 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3295 surface_desc, cbcontext->context);
3298 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3299 DDSURFACEDESC2 *surface_desc, void *context)
3301 IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
3302 struct surfacescallback_context *cbcontext = context;
3304 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3305 IDirectDrawSurface7_Release(surface);
3307 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3308 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3311 /*****************************************************************************
3312 * IDirectDraw7::EnumSurfaces
3314 * Loops through all surfaces attached to this device and calls the
3315 * application callback. This can't be relayed to WineD3DDevice,
3316 * because some WineD3DSurfaces' parents are IParent objects
3319 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3320 * DDSD: Description to filter for
3321 * Context: Application-provided pointer, it's passed unmodified to the
3323 * Callback: Address to call for each surface
3326 * DDERR_INVALIDPARAMS if the callback is NULL
3329 *****************************************************************************/
3330 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3331 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3333 /* The surface enumeration is handled by WineDDraw,
3334 * because it keeps track of all surfaces attached to
3335 * it. The filtering is done by our callback function,
3336 * because WineDDraw doesn't handle ddraw-like surface
3339 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3340 IDirectDrawSurfaceImpl *surf;
3342 DDSURFACEDESC2 desc;
3343 struct list *entry, *entry2;
3345 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3346 iface, Flags, DDSD, Context, Callback);
3348 all = Flags & DDENUMSURFACES_ALL;
3349 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3351 EnterCriticalSection(&ddraw_cs);
3355 LeaveCriticalSection(&ddraw_cs);
3356 return DDERR_INVALIDPARAMS;
3359 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3360 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3362 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3363 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3365 TRACE("Enumerating surface %p.\n", surf);
3366 desc = surf->surface_desc;
3367 IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3368 if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3370 LeaveCriticalSection(&ddraw_cs);
3375 LeaveCriticalSection(&ddraw_cs);
3379 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3380 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3382 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3383 struct surfacescallback2_context cbcontext;
3385 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3386 iface, flags, surface_desc, context, callback);
3388 cbcontext.func = callback;
3389 cbcontext.context = context;
3391 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, surface_desc,
3392 &cbcontext, EnumSurfacesCallback2Thunk);
3395 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3396 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3398 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3399 struct surfacescallback_context cbcontext;
3400 DDSURFACEDESC2 surface_desc2;
3402 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3403 iface, flags, surface_desc, context, callback);
3405 cbcontext.func = callback;
3406 cbcontext.context = context;
3408 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3409 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags,
3410 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3413 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3414 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3416 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3417 struct surfacescallback_context cbcontext;
3418 DDSURFACEDESC2 surface_desc2;
3420 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3421 iface, flags, surface_desc, context, callback);
3423 cbcontext.func = callback;
3424 cbcontext.context = context;
3426 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3427 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags,
3428 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3431 /*****************************************************************************
3432 * DirectDrawCreateClipper (DDRAW.@)
3434 * Creates a new IDirectDrawClipper object.
3437 * Clipper: Address to write the interface pointer to
3438 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3442 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3443 * E_OUTOFMEMORY if allocating the object failed
3445 *****************************************************************************/
3447 DirectDrawCreateClipper(DWORD Flags,
3448 LPDIRECTDRAWCLIPPER *Clipper,
3451 IDirectDrawClipperImpl* object;
3454 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3455 Flags, Clipper, UnkOuter);
3457 EnterCriticalSection(&ddraw_cs);
3458 if (UnkOuter != NULL)
3460 LeaveCriticalSection(&ddraw_cs);
3461 return CLASS_E_NOAGGREGATION;
3464 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3465 sizeof(IDirectDrawClipperImpl));
3468 LeaveCriticalSection(&ddraw_cs);
3469 return E_OUTOFMEMORY;
3472 hr = ddraw_clipper_init(object);
3475 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3476 HeapFree(GetProcessHeap(), 0, object);
3477 LeaveCriticalSection(&ddraw_cs);
3481 TRACE("Created clipper %p.\n", object);
3482 *Clipper = &object->IDirectDrawClipper_iface;
3483 LeaveCriticalSection(&ddraw_cs);
3487 /*****************************************************************************
3488 * IDirectDraw7::CreateClipper
3490 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3492 *****************************************************************************/
3493 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3494 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3496 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3497 iface, Flags, Clipper, UnkOuter);
3499 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3502 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3503 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3505 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3507 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3508 iface, flags, clipper, outer_unknown);
3510 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3513 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3514 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3516 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3518 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3519 iface, flags, clipper, outer_unknown);
3521 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3524 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3525 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3527 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3529 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3530 iface, flags, clipper, outer_unknown);
3532 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3535 /*****************************************************************************
3536 * IDirectDraw7::CreatePalette
3538 * Creates a new IDirectDrawPalette object
3541 * Flags: The flags for the new clipper
3542 * ColorTable: Color table to assign to the new clipper
3543 * Palette: Address to write the interface pointer to
3544 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3548 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3549 * E_OUTOFMEMORY if allocating the object failed
3551 *****************************************************************************/
3552 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3553 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3555 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3556 IDirectDrawPaletteImpl *object;
3559 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3560 iface, Flags, ColorTable, Palette, pUnkOuter);
3562 EnterCriticalSection(&ddraw_cs);
3563 if(pUnkOuter != NULL)
3565 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3566 LeaveCriticalSection(&ddraw_cs);
3567 return CLASS_E_NOAGGREGATION;
3570 /* The refcount test shows that a cooplevel is required for this */
3571 if(!This->cooperative_level)
3573 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3574 LeaveCriticalSection(&ddraw_cs);
3575 return DDERR_NOCOOPERATIVELEVELSET;
3578 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3581 ERR("Out of memory when allocating memory for a palette implementation\n");
3582 LeaveCriticalSection(&ddraw_cs);
3583 return E_OUTOFMEMORY;
3586 hr = ddraw_palette_init(object, This, Flags, ColorTable);
3589 WARN("Failed to initialize palette, hr %#x.\n", hr);
3590 HeapFree(GetProcessHeap(), 0, object);
3591 LeaveCriticalSection(&ddraw_cs);
3595 TRACE("Created palette %p.\n", object);
3596 *Palette = (IDirectDrawPalette *)object;
3597 LeaveCriticalSection(&ddraw_cs);
3601 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3602 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3604 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3607 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3608 iface, flags, entries, palette, outer_unknown);
3610 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3611 if (SUCCEEDED(hr) && *palette)
3613 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
3614 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3615 IDirectDraw4_AddRef(iface);
3616 impl->ifaceToRelease = (IUnknown *)iface;
3621 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3622 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3624 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3627 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3628 iface, flags, entries, palette, outer_unknown);
3630 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3631 if (SUCCEEDED(hr) && *palette)
3633 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
3634 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3635 impl->ifaceToRelease = NULL;
3641 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3642 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3644 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3647 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3648 iface, flags, entries, palette, outer_unknown);
3650 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3651 if (SUCCEEDED(hr) && *palette)
3653 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
3654 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3655 impl->ifaceToRelease = NULL;
3661 /*****************************************************************************
3662 * IDirectDraw7::DuplicateSurface
3664 * Duplicates a surface. The surface memory points to the same memory as
3665 * the original surface, and it's released when the last surface referencing
3666 * it is released. I guess that's beyond Wine's surface management right now
3667 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3668 * test application to implement this)
3671 * Src: Address of the source surface
3672 * Dest: Address to write the new surface pointer to
3675 * See IDirectDraw7::CreateSurface
3677 *****************************************************************************/
3678 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3679 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3681 IDirectDrawSurfaceImpl *Surf = unsafe_impl_from_IDirectDrawSurface7(Src);
3683 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3685 /* For now, simply create a new, independent surface */
3686 return IDirectDraw7_CreateSurface(iface,
3687 &Surf->surface_desc,
3692 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3693 IDirectDrawSurface4 **dst)
3695 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3696 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3697 IDirectDrawSurface7 *dst7;
3698 IDirectDrawSurfaceImpl *dst_impl;
3701 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3702 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
3703 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3709 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3710 *dst = &dst_impl->IDirectDrawSurface4_iface;
3711 IDirectDrawSurface4_AddRef(*dst);
3712 IDirectDrawSurface7_Release(dst7);
3717 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3718 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3720 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3721 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3722 IDirectDrawSurface7 *dst7;
3723 IDirectDrawSurfaceImpl *dst_impl;
3726 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3727 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
3728 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3731 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3732 *dst = &dst_impl->IDirectDrawSurface_iface;
3733 IDirectDrawSurface_AddRef(*dst);
3734 IDirectDrawSurface7_Release(dst7);
3739 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3740 IDirectDrawSurface **dst)
3742 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3743 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3744 IDirectDrawSurface7 *dst7;
3745 IDirectDrawSurfaceImpl *dst_impl;
3748 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3749 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
3750 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3753 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3754 *dst = &dst_impl->IDirectDrawSurface_iface;
3755 IDirectDrawSurface_AddRef(*dst);
3756 IDirectDrawSurface7_Release(dst7);
3761 /*****************************************************************************
3762 * IDirect3D7::EnumDevices
3764 * The EnumDevices method for IDirect3D7. It enumerates all supported
3765 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3768 * callback: Function to call for each enumerated device
3769 * context: Pointer to pass back to the app
3772 * D3D_OK, or the return value of the GetCaps call
3774 *****************************************************************************/
3775 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3777 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
3778 D3DDEVICEDESC7 device_desc7;
3779 D3DDEVICEDESC device_desc1;
3783 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3786 return DDERR_INVALIDPARAMS;
3788 EnterCriticalSection(&ddraw_cs);
3790 hr = IDirect3DImpl_GetCaps(This->wineD3D, &device_desc1, &device_desc7);
3793 LeaveCriticalSection(&ddraw_cs);
3797 for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
3801 device_desc7.deviceGUID = *device_list7[i].device_guid;
3802 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3803 if (ret != DDENUMRET_OK)
3805 TRACE("Application cancelled the enumeration.\n");
3806 LeaveCriticalSection(&ddraw_cs);
3811 TRACE("End of enumeration.\n");
3813 LeaveCriticalSection(&ddraw_cs);
3818 /*****************************************************************************
3819 * IDirect3D3::EnumDevices
3821 * Enumerates all supported Direct3DDevice interfaces. This is the
3822 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3824 * Version 1, 2 and 3
3827 * callback: Application-provided routine to call for each enumerated device
3828 * Context: Pointer to pass to the callback
3831 * D3D_OK on success,
3832 * The result of IDirect3DImpl_GetCaps if it failed
3834 *****************************************************************************/
3835 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3837 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3839 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
3840 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3841 D3DDEVICEDESC7 device_desc7;
3844 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3845 * name string. Let's put the string in a sufficiently sized array in
3846 * writable memory. */
3847 char device_name[50];
3848 strcpy(device_name,"Direct3D HEL");
3850 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3853 return DDERR_INVALIDPARAMS;
3855 EnterCriticalSection(&ddraw_cs);
3857 hr = IDirect3DImpl_GetCaps(This->wineD3D, &device_desc1, &device_desc7);
3860 LeaveCriticalSection(&ddraw_cs);
3864 /* Do I have to enumerate the reference id? Note from old d3d7:
3865 * "It seems that enumerating the reference IID on Direct3D 1 games
3866 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3868 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3869 * EnumReference which enables / disables enumerating the reference
3870 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3871 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3872 * demo directory suggest this.
3874 * Some games(GTA 2) seem to use the second enumerated device, so I have
3875 * to enumerate at least 2 devices. So enumerate the reference device to
3878 * Other games (Rollcage) tell emulation and hal device apart by certain
3879 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3880 * limitation flag), and it refuses all devices that have the perspective
3881 * flag set. This way it refuses the emulation device, and HAL devices
3882 * never have POW2 unset in d3d7 on windows. */
3883 if (This->d3dversion != 1)
3885 static CHAR reference_description[] = "RGB Direct3D emulation";
3887 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3888 hal_desc = device_desc1;
3889 hel_desc = device_desc1;
3890 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3891 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3892 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3893 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3894 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3895 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3896 device_name, &hal_desc, &hel_desc, context);
3897 if (hr != D3DENUMRET_OK)
3899 TRACE("Application cancelled the enumeration.\n");
3900 LeaveCriticalSection(&ddraw_cs);
3905 strcpy(device_name,"Direct3D HAL");
3907 TRACE("Enumerating HAL Direct3D device.\n");
3908 hal_desc = device_desc1;
3909 hel_desc = device_desc1;
3910 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3911 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3912 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3913 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3914 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3915 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3916 device_name, &hal_desc, &hel_desc, context);
3917 if (hr != D3DENUMRET_OK)
3919 TRACE("Application cancelled the enumeration.\n");
3920 LeaveCriticalSection(&ddraw_cs);
3924 TRACE("End of enumeration.\n");
3926 LeaveCriticalSection(&ddraw_cs);
3930 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3932 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
3934 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3936 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
3939 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3941 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
3943 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3945 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
3948 /*****************************************************************************
3949 * IDirect3D3::CreateLight
3951 * Creates an IDirect3DLight interface. This interface is used in
3952 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
3953 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
3954 * uses the IDirect3DDevice7 interface with D3D7 lights.
3956 * Version 1, 2 and 3
3959 * light: Address to store the new interface pointer
3960 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3965 * DDERR_OUTOFMEMORY if memory allocation failed
3966 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3968 *****************************************************************************/
3969 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
3970 IUnknown *outer_unknown)
3972 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
3973 IDirect3DLightImpl *object;
3975 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3977 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3979 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3982 ERR("Failed to allocate light memory.\n");
3983 return DDERR_OUTOFMEMORY;
3986 d3d_light_init(object, This);
3988 TRACE("Created light %p.\n", object);
3989 *light = &object->IDirect3DLight_iface;
3994 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3996 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
3998 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4000 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4003 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4005 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4007 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4009 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4012 /*****************************************************************************
4013 * IDirect3D3::CreateMaterial
4015 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
4016 * and older versions. The IDirect3DMaterial implementation wraps its
4017 * functionality to IDirect3DDevice7::SetMaterial and friends.
4019 * Version 1, 2 and 3
4022 * material: Address to store the new interface's pointer to
4023 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4028 * DDERR_OUTOFMEMORY if memory allocation failed
4029 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4031 *****************************************************************************/
4032 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
4033 IUnknown *outer_unknown)
4035 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4036 IDirect3DMaterialImpl *object;
4038 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4040 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4042 object = d3d_material_create(This);
4045 ERR("Failed to allocate material memory.\n");
4046 return DDERR_OUTOFMEMORY;
4049 TRACE("Created material %p.\n", object);
4050 *material = &object->IDirect3DMaterial3_iface;
4055 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
4056 IUnknown *outer_unknown)
4058 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4059 IDirect3DMaterialImpl *object;
4061 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4063 object = d3d_material_create(This);
4066 ERR("Failed to allocate material memory.\n");
4067 return DDERR_OUTOFMEMORY;
4070 TRACE("Created material %p.\n", object);
4071 *material = &object->IDirect3DMaterial2_iface;
4076 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
4077 IUnknown *outer_unknown)
4079 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4080 IDirect3DMaterialImpl *object;
4082 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4084 object = d3d_material_create(This);
4087 ERR("Failed to allocate material memory.\n");
4088 return DDERR_OUTOFMEMORY;
4091 TRACE("Created material %p.\n", object);
4092 *material = &object->IDirect3DMaterial_iface;
4097 /*****************************************************************************
4098 * IDirect3D3::CreateViewport
4100 * Creates an IDirect3DViewport interface. This interface is used
4101 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4102 * it has been replaced by a viewport structure and
4103 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4104 * uses the IDirect3DDevice7 methods for its functionality
4107 * Viewport: Address to store the new interface pointer
4108 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4113 * DDERR_OUTOFMEMORY if memory allocation failed
4114 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4116 *****************************************************************************/
4117 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4118 IUnknown *outer_unknown)
4120 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4121 IDirect3DViewportImpl *object;
4123 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4125 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4127 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4130 ERR("Failed to allocate viewport memory.\n");
4131 return DDERR_OUTOFMEMORY;
4134 d3d_viewport_init(object, This);
4136 TRACE("Created viewport %p.\n", object);
4137 *viewport = (IDirect3DViewport3 *)object;
4142 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4144 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4146 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4148 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4152 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4154 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4156 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4158 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4162 /*****************************************************************************
4163 * IDirect3D3::FindDevice
4165 * This method finds a device with the requested properties and returns a
4166 * device description
4170 * fds: Describes the requested device characteristics
4171 * fdr: Returns the device description
4175 * DDERR_INVALIDPARAMS if no device was found
4177 *****************************************************************************/
4178 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4180 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4181 D3DDEVICEDESC7 desc7;
4182 D3DDEVICEDESC desc1;
4185 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4187 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4189 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4190 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4191 return DDERR_INVALIDPARAMS;
4193 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4194 && fds->dcmColorModel != D3DCOLOR_RGB)
4196 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4197 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4200 if (fds->dwFlags & D3DFDS_GUID)
4202 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4203 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4204 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4205 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4207 WARN("No match for this GUID.\n");
4208 return DDERR_NOTFOUND;
4213 hr = IDirect3DImpl_GetCaps(This->wineD3D, &desc1, &desc7);
4214 if (hr != D3D_OK) return hr;
4216 /* Now return our own GUID */
4217 fdr->guid = IID_D3DDEVICE_WineD3D;
4218 fdr->ddHwDesc = desc1;
4219 fdr->ddSwDesc = desc1;
4221 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4226 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4228 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4230 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4232 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4235 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4237 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4239 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4241 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4244 /*****************************************************************************
4245 * IDirect3D7::CreateDevice
4247 * Creates an IDirect3DDevice7 interface.
4249 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4250 * DirectDraw surfaces and are created with
4251 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4252 * create the device object and QueryInterfaces for IDirect3DDevice
4255 * refiid: IID of the device to create
4256 * Surface: Initial rendertarget
4257 * Device: Address to return the interface pointer
4261 * DDERR_OUTOFMEMORY if memory allocation failed
4262 * DDERR_INVALIDPARAMS if a device exists already
4264 *****************************************************************************/
4265 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4266 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4268 IDirectDrawSurfaceImpl *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4269 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4270 IDirect3DDeviceImpl *object;
4273 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4275 EnterCriticalSection(&ddraw_cs);
4278 /* Fail device creation if non-opengl surfaces are used. */
4279 if (DefaultSurfaceType != SURFACE_OPENGL)
4281 ERR("The application wants to create a Direct3D device, but non-opengl surfaces are set in the registry.\n");
4282 ERR("Please set the surface implementation to opengl or autodetection to allow 3D rendering.\n");
4284 /* We only hit this path if a default surface is set in the registry. Incorrect autodetection
4285 * is caught in CreateSurface or QueryInterface. */
4286 LeaveCriticalSection(&ddraw_cs);
4290 if (This->d3ddevice)
4292 FIXME("Only one Direct3D device per DirectDraw object supported.\n");
4293 LeaveCriticalSection(&ddraw_cs);
4294 return DDERR_INVALIDPARAMS;
4297 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4300 ERR("Failed to allocate device memory.\n");
4301 LeaveCriticalSection(&ddraw_cs);
4302 return DDERR_OUTOFMEMORY;
4305 hr = d3d_device_init(object, This, target);
4308 WARN("Failed to initialize device, hr %#x.\n", hr);
4309 HeapFree(GetProcessHeap(), 0, object);
4310 LeaveCriticalSection(&ddraw_cs);
4314 TRACE("Created device %p.\n", object);
4315 *device = (IDirect3DDevice7 *)object;
4317 LeaveCriticalSection(&ddraw_cs);
4321 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4322 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4324 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4325 IDirectDrawSurfaceImpl *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4328 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4329 iface, debugstr_guid(riid), surface, device, outer_unknown);
4331 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4333 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid,
4334 surface_impl ? &surface_impl->IDirectDrawSurface7_iface : NULL,
4335 (IDirect3DDevice7 **)device);
4336 if (*device) *device = (IDirect3DDevice3 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice3_vtbl;
4341 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4342 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4344 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4345 IDirectDrawSurfaceImpl *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4348 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4349 iface, debugstr_guid(riid), surface, device);
4351 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid,
4352 surface_impl ? &surface_impl->IDirectDrawSurface7_iface : NULL,
4353 (IDirect3DDevice7 **)device);
4354 if (*device) *device = (IDirect3DDevice2 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice2_vtbl;
4359 /*****************************************************************************
4360 * IDirect3D7::CreateVertexBuffer
4362 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4368 * desc: Requested Vertex buffer properties
4369 * vertex_buffer: Address to return the interface pointer at
4370 * flags: Some flags, should be 0
4374 * DDERR_OUTOFMEMORY if memory allocation failed
4375 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4376 * DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4378 *****************************************************************************/
4379 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4380 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4382 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4383 IDirect3DVertexBufferImpl *object;
4386 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4387 iface, desc, vertex_buffer, flags);
4389 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4391 hr = d3d_vertex_buffer_create(&object, This, desc);
4394 TRACE("Created vertex buffer %p.\n", object);
4395 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4398 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4403 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4404 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4406 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4407 IDirect3DVertexBufferImpl *object;
4410 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4411 iface, desc, vertex_buffer, flags, outer_unknown);
4414 return CLASS_E_NOAGGREGATION;
4415 if (!vertex_buffer || !desc)
4416 return DDERR_INVALIDPARAMS;
4418 hr = d3d_vertex_buffer_create(&object, This, desc);
4421 TRACE("Created vertex buffer %p.\n", object);
4422 *vertex_buffer = &object->IDirect3DVertexBuffer_iface;
4425 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4430 /*****************************************************************************
4431 * IDirect3D7::EnumZBufferFormats
4433 * Enumerates all supported Z buffer pixel formats
4439 * callback: callback to call for each pixel format
4440 * context: Pointer to pass back to the callback
4444 * DDERR_INVALIDPARAMS if callback is NULL
4445 * For details, see IWineD3DDevice::EnumZBufferFormats
4447 *****************************************************************************/
4448 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4449 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4451 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4452 WINED3DDISPLAYMODE d3ddm;
4453 WINED3DDEVTYPE type;
4457 /* Order matters. Specifically, BattleZone II (full version) expects the
4458 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4459 static const enum wined3d_format_id formats[] =
4461 WINED3DFMT_S1_UINT_D15_UNORM,
4462 WINED3DFMT_D16_UNORM,
4463 WINED3DFMT_X8D24_UNORM,
4464 WINED3DFMT_S4X4_UINT_D24_UNORM,
4465 WINED3DFMT_D24_UNORM_S8_UINT,
4466 WINED3DFMT_D32_UNORM,
4469 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4470 iface, debugstr_guid(device_iid), callback, context);
4472 if (!callback) return DDERR_INVALIDPARAMS;
4474 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4475 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4476 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4478 TRACE("Asked for HAL device.\n");
4479 type = WINED3DDEVTYPE_HAL;
4481 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4482 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4484 TRACE("Asked for SW device.\n");
4485 type = WINED3DDEVTYPE_SW;
4487 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4489 TRACE("Asked for REF device.\n");
4490 type = WINED3DDEVTYPE_REF;
4492 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4494 TRACE("Asked for NULLREF device.\n");
4495 type = WINED3DDEVTYPE_NULLREF;
4499 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4500 type = WINED3DDEVTYPE_HAL;
4503 EnterCriticalSection(&ddraw_cs);
4504 /* We need an adapter format from somewhere to please wined3d and WGL.
4505 * Use the current display mode. So far all cards offer the same depth
4506 * stencil format for all modes, but if some do not and applications do
4507 * not like that we'll have to find some workaround, like iterating over
4508 * all imaginable formats and collecting all the depth stencil formats we
4510 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &d3ddm);
4512 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4514 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, type, d3ddm.Format,
4515 WINED3DUSAGE_DEPTHSTENCIL, WINED3DRTYPE_SURFACE, formats[i], SURFACE_OPENGL);
4518 DDPIXELFORMAT pformat;
4520 memset(&pformat, 0, sizeof(pformat));
4521 pformat.dwSize = sizeof(pformat);
4522 PixelFormat_WineD3DtoDD(&pformat, formats[i]);
4524 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4525 hr = callback(&pformat, context);
4526 if (hr != DDENUMRET_OK)
4528 TRACE("Format enumeration cancelled by application.\n");
4529 LeaveCriticalSection(&ddraw_cs);
4535 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4536 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4537 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4538 * newer enumerate both versions, so we do the same(bug 22434) */
4539 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, type, d3ddm.Format,
4540 WINED3DUSAGE_DEPTHSTENCIL, WINED3DRTYPE_SURFACE, WINED3DFMT_X8D24_UNORM, SURFACE_OPENGL);
4543 DDPIXELFORMAT x8d24 =
4545 sizeof(x8d24), DDPF_ZBUFFER, 0,
4546 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4548 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4549 callback(&x8d24, context);
4552 TRACE("End of enumeration.\n");
4554 LeaveCriticalSection(&ddraw_cs);
4558 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4559 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4561 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4563 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4564 iface, debugstr_guid(device_iid), callback, context);
4566 return d3d7_EnumZBufferFormats(&This->IDirect3D7_iface, device_iid, callback, context);
4569 /*****************************************************************************
4570 * IDirect3D7::EvictManagedTextures
4572 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4573 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4578 * D3D_OK, because it's a stub
4580 *****************************************************************************/
4581 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4583 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4585 TRACE("iface %p!\n", iface);
4587 EnterCriticalSection(&ddraw_cs);
4588 if (This->d3d_initialized)
4589 wined3d_device_evict_managed_resources(This->wined3d_device);
4590 LeaveCriticalSection(&ddraw_cs);
4595 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4597 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4599 TRACE("iface %p.\n", iface);
4601 return d3d7_EvictManagedTextures(&This->IDirect3D7_iface);
4604 /*****************************************************************************
4605 * IDirect3DImpl_GetCaps
4607 * This function retrieves the device caps from wined3d
4608 * and converts it into a D3D7 and D3D - D3D3 structure
4609 * This is a helper function called from various places in ddraw
4612 * wined3d: The interface to get the caps from
4613 * desc1: Old D3D <3 structure to fill (needed)
4614 * desc7: D3D7 device desc structure to fill (needed)
4617 * D3D_OK on success, or the return value of IWineD3D::GetCaps
4619 *****************************************************************************/
4620 HRESULT IDirect3DImpl_GetCaps(const struct wined3d *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
4622 WINED3DCAPS wined3d_caps;
4625 TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
4627 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
4629 EnterCriticalSection(&ddraw_cs);
4630 hr = wined3d_get_device_caps(wined3d, 0, WINED3DDEVTYPE_HAL, &wined3d_caps);
4631 LeaveCriticalSection(&ddraw_cs);
4634 WARN("Failed to get device caps, hr %#x.\n", hr);
4638 /* Copy the results into the d3d7 and d3d3 structures */
4639 desc7->dwDevCaps = wined3d_caps.DevCaps;
4640 desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
4641 desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
4642 desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
4643 desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
4644 desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
4645 desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
4646 desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
4647 desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
4648 desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
4649 desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
4651 desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
4652 desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
4654 desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
4655 desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
4656 desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
4657 desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
4659 desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
4660 desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
4661 desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
4662 desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
4664 desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
4665 desc7->dwStencilCaps = wined3d_caps.StencilCaps;
4667 desc7->dwFVFCaps = wined3d_caps.FVFCaps;
4668 desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
4670 desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
4671 desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
4673 /* Remove all non-d3d7 caps */
4674 desc7->dwDevCaps &= (
4675 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
4676 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
4677 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
4678 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
4679 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
4680 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
4681 D3DDEVCAPS_HWRASTERIZATION);
4683 desc7->dwStencilCaps &= (
4684 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
4685 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
4686 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
4690 desc7->dwTextureOpCaps &= (
4691 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
4692 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
4693 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
4694 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
4695 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
4696 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
4697 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
4698 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
4700 desc7->dwVertexProcessingCaps &= (
4701 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
4702 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
4704 desc7->dpcLineCaps.dwMiscCaps &= (
4705 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
4706 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
4707 D3DPMISCCAPS_CULLCCW);
4709 desc7->dpcLineCaps.dwRasterCaps &= (
4710 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
4711 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
4712 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
4713 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
4714 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
4715 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
4716 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
4717 D3DPRASTERCAPS_ZFOG);
4719 desc7->dpcLineCaps.dwZCmpCaps &= (
4720 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4721 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4722 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4724 desc7->dpcLineCaps.dwSrcBlendCaps &= (
4725 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4726 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4727 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4728 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4729 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4731 desc7->dpcLineCaps.dwDestBlendCaps &= (
4732 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4733 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4734 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4735 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4736 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4738 desc7->dpcLineCaps.dwAlphaCmpCaps &= (
4739 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4740 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4741 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4743 desc7->dpcLineCaps.dwShadeCaps &= (
4744 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
4745 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
4746 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
4747 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
4748 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
4749 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
4750 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
4752 desc7->dpcLineCaps.dwTextureCaps &= (
4753 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
4754 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
4755 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
4756 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
4758 desc7->dpcLineCaps.dwTextureFilterCaps &= (
4759 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
4760 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
4761 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
4762 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
4763 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
4764 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
4766 desc7->dpcLineCaps.dwTextureBlendCaps &= (
4767 D3DPTBLENDCAPS_DECAL | D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_DECALALPHA |
4768 D3DPTBLENDCAPS_MODULATEALPHA | D3DPTBLENDCAPS_DECALMASK | D3DPTBLENDCAPS_MODULATEMASK |
4769 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_ADD);
4771 desc7->dpcLineCaps.dwTextureAddressCaps &= (
4772 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
4773 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
4775 if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
4777 /* DirectX7 always has the np2 flag set, no matter what the card
4778 * supports. Some old games (Rollcage) check the caps incorrectly.
4779 * If wined3d supports nonpow2 textures it also has np2 conditional
4781 desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
4784 /* Fill the missing members, and do some fixup */
4785 desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
4786 desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
4787 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
4788 D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
4789 D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
4790 desc7->dpcLineCaps.dwStippleWidth = 32;
4791 desc7->dpcLineCaps.dwStippleHeight = 32;
4792 /* Use the same for the TriCaps */
4793 desc7->dpcTriCaps = desc7->dpcLineCaps;
4795 desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
4796 desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
4797 desc7->dwMinTextureWidth = 1;
4798 desc7->dwMinTextureHeight = 1;
4800 /* Convert DWORDs safely to WORDs */
4801 if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
4802 else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
4803 if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
4804 else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
4806 if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
4807 else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
4808 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
4809 else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
4811 desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
4813 desc7->dwReserved1 = 0;
4814 desc7->dwReserved2 = 0;
4815 desc7->dwReserved3 = 0;
4816 desc7->dwReserved4 = 0;
4818 /* Fill the old structure */
4819 memset(desc1, 0, sizeof(*desc1));
4820 desc1->dwSize = sizeof(D3DDEVICEDESC);
4821 desc1->dwFlags = D3DDD_COLORMODEL
4823 | D3DDD_TRANSFORMCAPS
4825 | D3DDD_LIGHTINGCAPS
4828 | D3DDD_DEVICERENDERBITDEPTH
4829 | D3DDD_DEVICEZBUFFERBITDEPTH
4830 | D3DDD_MAXBUFFERSIZE
4831 | D3DDD_MAXVERTEXCOUNT;
4833 desc1->dcmColorModel = D3DCOLOR_RGB;
4834 desc1->dwDevCaps = desc7->dwDevCaps;
4835 desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
4836 desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
4837 desc1->bClipping = TRUE;
4838 desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
4839 desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
4840 | D3DLIGHTCAPS_PARALLELPOINT
4841 | D3DLIGHTCAPS_POINT
4842 | D3DLIGHTCAPS_SPOT;
4844 desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
4845 desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
4847 desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
4848 desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
4849 desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
4850 desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
4851 desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
4852 desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
4853 desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
4854 desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
4855 desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
4856 desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
4857 desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
4858 desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
4859 desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
4861 desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
4862 desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
4863 desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
4864 desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
4865 desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
4866 desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
4867 desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
4868 desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
4869 desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
4870 desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
4871 desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
4872 desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
4873 desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
4875 desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
4876 desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
4877 desc1->dwMaxBufferSize = 0;
4878 desc1->dwMaxVertexCount = 65536;
4879 desc1->dwMinTextureWidth = desc7->dwMinTextureWidth;
4880 desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
4881 desc1->dwMaxTextureWidth = desc7->dwMaxTextureWidth;
4882 desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
4883 desc1->dwMinStippleWidth = 1;
4884 desc1->dwMinStippleHeight = 1;
4885 desc1->dwMaxStippleWidth = 32;
4886 desc1->dwMaxStippleHeight = 32;
4887 desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
4888 desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
4889 desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
4890 desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
4891 desc1->dvGuardBandRight = desc7->dvGuardBandRight;
4892 desc1->dvGuardBandTop = desc7->dvGuardBandTop;
4893 desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
4894 desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
4895 desc1->dwStencilCaps = desc7->dwStencilCaps;
4896 desc1->dwFVFCaps = desc7->dwFVFCaps;
4897 desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
4898 desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
4899 desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
4904 /*****************************************************************************
4905 * IDirectDraw7 VTable
4906 *****************************************************************************/
4907 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4910 ddraw7_QueryInterface,
4915 ddraw7_CreateClipper,
4916 ddraw7_CreatePalette,
4917 ddraw7_CreateSurface,
4918 ddraw7_DuplicateSurface,
4919 ddraw7_EnumDisplayModes,
4920 ddraw7_EnumSurfaces,
4921 ddraw7_FlipToGDISurface,
4923 ddraw7_GetDisplayMode,
4924 ddraw7_GetFourCCCodes,
4925 ddraw7_GetGDISurface,
4926 ddraw7_GetMonitorFrequency,
4928 ddraw7_GetVerticalBlankStatus,
4930 ddraw7_RestoreDisplayMode,
4931 ddraw7_SetCooperativeLevel,
4932 ddraw7_SetDisplayMode,
4933 ddraw7_WaitForVerticalBlank,
4935 ddraw7_GetAvailableVidMem,
4937 ddraw7_GetSurfaceFromDC,
4939 ddraw7_RestoreAllSurfaces,
4940 ddraw7_TestCooperativeLevel,
4941 ddraw7_GetDeviceIdentifier,
4943 ddraw7_StartModeTest,
4947 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
4950 ddraw4_QueryInterface,
4955 ddraw4_CreateClipper,
4956 ddraw4_CreatePalette,
4957 ddraw4_CreateSurface,
4958 ddraw4_DuplicateSurface,
4959 ddraw4_EnumDisplayModes,
4960 ddraw4_EnumSurfaces,
4961 ddraw4_FlipToGDISurface,
4963 ddraw4_GetDisplayMode,
4964 ddraw4_GetFourCCCodes,
4965 ddraw4_GetGDISurface,
4966 ddraw4_GetMonitorFrequency,
4968 ddraw4_GetVerticalBlankStatus,
4970 ddraw4_RestoreDisplayMode,
4971 ddraw4_SetCooperativeLevel,
4972 ddraw4_SetDisplayMode,
4973 ddraw4_WaitForVerticalBlank,
4975 ddraw4_GetAvailableVidMem,
4977 ddraw4_GetSurfaceFromDC,
4979 ddraw4_RestoreAllSurfaces,
4980 ddraw4_TestCooperativeLevel,
4981 ddraw4_GetDeviceIdentifier,
4984 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
4987 ddraw2_QueryInterface,
4992 ddraw2_CreateClipper,
4993 ddraw2_CreatePalette,
4994 ddraw2_CreateSurface,
4995 ddraw2_DuplicateSurface,
4996 ddraw2_EnumDisplayModes,
4997 ddraw2_EnumSurfaces,
4998 ddraw2_FlipToGDISurface,
5000 ddraw2_GetDisplayMode,
5001 ddraw2_GetFourCCCodes,
5002 ddraw2_GetGDISurface,
5003 ddraw2_GetMonitorFrequency,
5005 ddraw2_GetVerticalBlankStatus,
5007 ddraw2_RestoreDisplayMode,
5008 ddraw2_SetCooperativeLevel,
5009 ddraw2_SetDisplayMode,
5010 ddraw2_WaitForVerticalBlank,
5012 ddraw2_GetAvailableVidMem,
5015 static const struct IDirectDrawVtbl ddraw1_vtbl =
5018 ddraw1_QueryInterface,
5023 ddraw1_CreateClipper,
5024 ddraw1_CreatePalette,
5025 ddraw1_CreateSurface,
5026 ddraw1_DuplicateSurface,
5027 ddraw1_EnumDisplayModes,
5028 ddraw1_EnumSurfaces,
5029 ddraw1_FlipToGDISurface,
5031 ddraw1_GetDisplayMode,
5032 ddraw1_GetFourCCCodes,
5033 ddraw1_GetGDISurface,
5034 ddraw1_GetMonitorFrequency,
5036 ddraw1_GetVerticalBlankStatus,
5038 ddraw1_RestoreDisplayMode,
5039 ddraw1_SetCooperativeLevel,
5040 ddraw1_SetDisplayMode,
5041 ddraw1_WaitForVerticalBlank,
5044 static const struct IDirect3D7Vtbl d3d7_vtbl =
5046 /* IUnknown methods */
5047 d3d7_QueryInterface,
5050 /* IDirect3D7 methods */
5053 d3d7_CreateVertexBuffer,
5054 d3d7_EnumZBufferFormats,
5055 d3d7_EvictManagedTextures
5058 static const struct IDirect3D3Vtbl d3d3_vtbl =
5060 /* IUnknown methods */
5061 d3d3_QueryInterface,
5064 /* IDirect3D3 methods */
5067 d3d3_CreateMaterial,
5068 d3d3_CreateViewport,
5071 d3d3_CreateVertexBuffer,
5072 d3d3_EnumZBufferFormats,
5073 d3d3_EvictManagedTextures
5076 static const struct IDirect3D2Vtbl d3d2_vtbl =
5078 /* IUnknown methods */
5079 d3d2_QueryInterface,
5082 /* IDirect3D2 methods */
5085 d3d2_CreateMaterial,
5086 d3d2_CreateViewport,
5091 static const struct IDirect3DVtbl d3d1_vtbl =
5093 /* IUnknown methods */
5094 d3d1_QueryInterface,
5097 /* IDirect3D methods */
5101 d3d1_CreateMaterial,
5102 d3d1_CreateViewport,
5106 /*****************************************************************************
5109 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
5110 * if none was found.
5112 * This function is in ddraw.c and the DDraw object space because D3D7
5113 * vertex buffers are created using the IDirect3D interface to the ddraw
5114 * object, so they can be valid across D3D devices(theoretically. The ddraw
5115 * object also owns the wined3d device
5119 * fvf: Fvf to find the decl for
5122 * NULL in case of an error, the vertex declaration for the FVF otherwise.
5124 *****************************************************************************/
5125 struct wined3d_vertex_declaration *ddraw_find_decl(IDirectDrawImpl *This, DWORD fvf)
5127 struct wined3d_vertex_declaration *pDecl = NULL;
5129 int p, low, high; /* deliberately signed */
5130 struct FvfToDecl *convertedDecls = This->decls;
5132 TRACE("Searching for declaration for fvf %08x... ", fvf);
5135 high = This->numConvertedDecls - 1;
5136 while(low <= high) {
5137 p = (low + high) >> 1;
5139 if(convertedDecls[p].fvf == fvf) {
5140 TRACE("found %p\n", convertedDecls[p].decl);
5141 return convertedDecls[p].decl;
5142 } else if(convertedDecls[p].fvf < fvf) {
5148 TRACE("not found. Creating and inserting at position %d.\n", low);
5150 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
5151 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5152 if (hr != S_OK) return NULL;
5154 if(This->declArraySize == This->numConvertedDecls) {
5155 int grow = max(This->declArraySize / 2, 8);
5156 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5157 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5158 if (!convertedDecls)
5160 wined3d_vertex_declaration_decref(pDecl);
5163 This->decls = convertedDecls;
5164 This->declArraySize += grow;
5167 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5168 convertedDecls[low].decl = pDecl;
5169 convertedDecls[low].fvf = fvf;
5170 This->numConvertedDecls++;
5172 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5176 static inline struct IDirectDrawImpl *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
5178 return CONTAINING_RECORD(device_parent, struct IDirectDrawImpl, device_parent);
5181 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
5182 struct wined3d_device *device)
5184 TRACE("device_parent %p, device %p.\n", device_parent, device);
5187 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
5188 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5189 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, struct wined3d_surface **surface)
5191 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5192 IDirectDrawSurfaceImpl *surf = NULL;
5194 DDSCAPS2 searchcaps = ddraw->tex_root->surface_desc.ddsCaps;
5196 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
5197 "\tpool %#x, level %u, face %u, surface %p.\n",
5198 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
5200 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
5203 case WINED3DCUBEMAP_FACE_POSITIVE_X:
5204 TRACE("Asked for positive x\n");
5205 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5207 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
5209 surf = ddraw->tex_root; break;
5210 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
5211 TRACE("Asked for negative x\n");
5212 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
5213 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
5214 TRACE("Asked for positive y\n");
5215 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
5216 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
5217 TRACE("Asked for negative y\n");
5218 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
5219 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
5220 TRACE("Asked for positive z\n");
5221 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
5222 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
5223 TRACE("Asked for negative z\n");
5224 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
5225 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
5230 IDirectDrawSurface7 *attached;
5231 IDirectDrawSurface7_GetAttachedSurface(&ddraw->tex_root->IDirectDrawSurface7_iface, &searchcaps, &attached);
5232 surf = unsafe_impl_from_IDirectDrawSurface7(attached);
5233 IDirectDrawSurface7_Release(attached);
5235 if (!surf) ERR("root search surface not found\n");
5237 /* Find the wanted mipmap. There are enough mipmaps in the chain */
5240 IDirectDrawSurface7 *attached;
5241 IDirectDrawSurface7_GetAttachedSurface(&surf->IDirectDrawSurface7_iface, &searchcaps, &attached);
5242 if(!attached) ERR("Surface not found\n");
5243 surf = impl_from_IDirectDrawSurface7(attached);
5244 IDirectDrawSurface7_Release(attached);
5248 /* Return the surface */
5249 *surface = surf->wined3d_surface;
5250 wined3d_surface_incref(*surface);
5252 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
5257 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
5259 struct IDirectDrawImpl *ddraw = parent;
5260 ddraw->wined3d_frontbuffer = NULL;
5263 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
5265 ddraw_frontbuffer_destroyed,
5268 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
5269 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
5270 WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
5271 struct wined3d_surface **surface)
5273 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5276 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5277 "\tmultisample_quality %u, lockable %u, surface %p.\n",
5278 device_parent, container_parent, width, height, format, multisample_type,
5279 multisample_quality, lockable, surface);
5281 if (ddraw->wined3d_frontbuffer)
5283 ERR("Frontbuffer already created.\n");
5287 hr = wined3d_surface_create(ddraw->wined3d_device, width, height, format, lockable, FALSE, 0,
5288 WINED3DUSAGE_RENDERTARGET, WINED3DPOOL_DEFAULT, multisample_type, multisample_quality,
5289 DefaultSurfaceType, ddraw, &ddraw_frontbuffer_parent_ops, surface);
5291 ddraw->wined3d_frontbuffer = *surface;
5296 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
5297 UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
5298 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
5300 ERR("DirectDraw doesn't have and shouldn't try creating implicit depth buffers.\n");
5304 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
5305 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5306 WINED3DPOOL pool, DWORD usage, struct wined3d_volume **volume)
5308 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
5309 "format %#x, pool %#x, usage %#x, volume %p.\n",
5310 device_parent, container_parent, width, height, depth,
5311 format, pool, usage, volume);
5313 ERR("Not implemented!\n");
5318 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
5319 WINED3DPRESENT_PARAMETERS *present_parameters, struct wined3d_swapchain **swapchain)
5321 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5322 IDirectDrawSurfaceImpl *iterator;
5325 TRACE("device_parent %p, present_parameters %p, swapchain %p.\n", device_parent, present_parameters, swapchain);
5327 hr = wined3d_swapchain_create(ddraw->wined3d_device, present_parameters,
5328 DefaultSurfaceType, NULL, &ddraw_null_wined3d_parent_ops, swapchain);
5331 WARN("Failed to create swapchain, hr %#x.\n", hr);
5336 ddraw->d3d_target->wined3d_swapchain = *swapchain;
5337 iterator = ddraw->d3d_target->complex_array[0];
5340 iterator->wined3d_swapchain = *swapchain;
5341 iterator = iterator->complex_array[0];
5347 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5349 device_parent_wined3d_device_created,
5350 device_parent_create_surface,
5351 device_parent_create_rendertarget,
5352 device_parent_create_depth_stencil,
5353 device_parent_create_volume,
5354 device_parent_create_swapchain,
5357 HRESULT ddraw_init(IDirectDrawImpl *ddraw, WINED3DDEVTYPE device_type)
5362 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5363 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5364 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5365 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5366 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5367 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5368 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5369 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5370 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5371 ddraw->numIfaces = 1;
5374 /* Get the current screen settings. */
5376 ddraw->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
5378 ddraw->orig_width = GetSystemMetrics(SM_CXSCREEN);
5379 ddraw->orig_height = GetSystemMetrics(SM_CYSCREEN);
5381 ddraw->wineD3D = wined3d_create(7, WINED3D_PALETTE_PER_SURFACE | WINED3D_LEGACY_DEPTH_BIAS,
5382 &ddraw->IDirectDraw7_iface);
5383 if (!ddraw->wineD3D)
5385 WARN("Failed to create a wined3d object.\n");
5386 return E_OUTOFMEMORY;
5389 hr = wined3d_device_create(ddraw->wineD3D, WINED3DADAPTER_DEFAULT, device_type,
5390 NULL, 0, 8, &ddraw->device_parent, &ddraw->wined3d_device);
5393 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5394 wined3d_decref(ddraw->wineD3D);
5398 list_init(&ddraw->surface_list);