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 void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
44 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
46 ddraw_null_wined3d_object_destroyed,
49 static inline IDirectDrawImpl *impl_from_IDirectDraw(IDirectDraw *iface)
51 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw_iface);
54 static inline IDirectDrawImpl *impl_from_IDirectDraw2(IDirectDraw2 *iface)
56 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw2_iface);
59 static inline IDirectDrawImpl *impl_from_IDirectDraw3(IDirectDraw3 *iface)
61 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw3_iface);
64 static inline IDirectDrawImpl *impl_from_IDirectDraw4(IDirectDraw4 *iface)
66 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw4_iface);
69 static inline IDirectDrawImpl *impl_from_IDirectDraw7(IDirectDraw7 *iface)
71 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw7_iface);
74 static inline IDirectDrawImpl *impl_from_IDirect3D(IDirect3D *iface)
76 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D_iface);
79 static inline IDirectDrawImpl *impl_from_IDirect3D2(IDirect3D2 *iface)
81 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D2_iface);
84 static inline IDirectDrawImpl *impl_from_IDirect3D3(IDirect3D3 *iface)
86 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D3_iface);
89 static inline IDirectDrawImpl *impl_from_IDirect3D7(IDirect3D7 *iface)
91 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D7_iface);
94 /*****************************************************************************
96 *****************************************************************************/
98 /*****************************************************************************
99 * IDirectDraw7::QueryInterface
101 * Queries different interfaces of the DirectDraw object. It can return
102 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
103 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
105 * The returned interface is AddRef()-ed before it's returned
107 * Used for version 1, 2, 4 and 7
110 * refiid: Interface ID asked for
111 * obj: Used to return the interface pointer
114 * S_OK if an interface was found
115 * E_NOINTERFACE if the requested interface wasn't found
117 *****************************************************************************/
118 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID refiid, void **obj)
120 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
122 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
124 /* Can change surface impl type */
125 EnterCriticalSection(&ddraw_cs);
127 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
132 LeaveCriticalSection(&ddraw_cs);
133 return DDERR_INVALIDPARAMS;
136 /* Check DirectDraw Interfaces */
137 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
138 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
141 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
143 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
145 *obj = &This->IDirectDraw4_iface;
146 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
148 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
150 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
151 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
153 LeaveCriticalSection(&ddraw_cs);
154 return E_NOINTERFACE;
156 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
158 *obj = &This->IDirectDraw2_iface;
159 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
161 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
163 *obj = &This->IDirectDraw_iface;
164 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
168 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
169 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
170 * who had this idea and why. The older interfaces can query and IDirect3D version
171 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
172 * and messy to implement with the common creation function, so it has been left out here.
174 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
175 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
176 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
177 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
179 /* Check the surface implementation */
180 if(This->ImplType == SURFACE_UNKNOWN)
182 /* Apps may create the IDirect3D Interface before the primary surface.
183 * set the surface implementation */
184 This->ImplType = SURFACE_OPENGL;
185 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
187 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
189 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
190 ERR(" (%p) You may want to contact wine-devel for help\n", This);
191 /* Should I assert(0) here??? */
193 else if(This->ImplType != SURFACE_OPENGL)
195 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
196 /* Do not abort here, only reject 3D Device creation */
199 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
201 This->d3dversion = 1;
202 *obj = &This->IDirect3D_iface;
203 TRACE(" returning Direct3D interface at %p.\n", *obj);
205 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
207 This->d3dversion = 2;
208 *obj = &This->IDirect3D2_iface;
209 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
211 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
213 This->d3dversion = 3;
214 *obj = &This->IDirect3D3_iface;
215 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
217 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
219 This->d3dversion = 7;
220 *obj = &This->IDirect3D7_iface;
221 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
224 /* Unknown interface */
227 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
228 LeaveCriticalSection(&ddraw_cs);
229 return E_NOINTERFACE;
232 IUnknown_AddRef( (IUnknown *) *obj );
233 LeaveCriticalSection(&ddraw_cs);
237 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
239 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
241 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
243 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
246 static HRESULT WINAPI ddraw3_QueryInterface(IDirectDraw3 *iface, REFIID riid, void **object)
248 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
250 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
252 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
255 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
257 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
259 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
261 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
264 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
266 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
268 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
270 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
273 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
275 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
277 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
279 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
282 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
284 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
286 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
288 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
291 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
293 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
295 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
297 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
300 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
302 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
304 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
306 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
309 /*****************************************************************************
310 * IDirectDraw7::AddRef
312 * Increases the interfaces refcount, basically
314 * DDraw refcounting is a bit tricky. The different DirectDraw interface
315 * versions have individual refcounts, but the IDirect3D interfaces do not.
316 * All interfaces are from one object, that means calling QueryInterface on an
317 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
318 * IDirectDrawImpl object.
320 * That means all AddRef and Release implementations of IDirectDrawX work
321 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
322 * except of IDirect3D7 which thunks to IDirectDraw7
324 * Returns: The new refcount
326 *****************************************************************************/
327 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
329 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
330 ULONG ref = InterlockedIncrement(&This->ref7);
332 TRACE("%p increasing refcount to %u.\n", This, ref);
334 if(ref == 1) InterlockedIncrement(&This->numIfaces);
339 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
341 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
342 ULONG ref = InterlockedIncrement(&This->ref4);
344 TRACE("%p increasing refcount to %u.\n", This, ref);
346 if (ref == 1) InterlockedIncrement(&This->numIfaces);
351 static ULONG WINAPI ddraw3_AddRef(IDirectDraw3 *iface)
353 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
354 ULONG ref = InterlockedIncrement(&This->ref3);
356 TRACE("%p increasing refcount to %u.\n", This, ref);
358 if (ref == 1) InterlockedIncrement(&This->numIfaces);
363 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
365 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
366 ULONG ref = InterlockedIncrement(&This->ref2);
368 TRACE("%p increasing refcount to %u.\n", This, ref);
370 if (ref == 1) InterlockedIncrement(&This->numIfaces);
375 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
377 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
378 ULONG ref = InterlockedIncrement(&This->ref1);
380 TRACE("%p increasing refcount to %u.\n", This, ref);
382 if (ref == 1) InterlockedIncrement(&This->numIfaces);
387 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
389 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
391 TRACE("iface %p.\n", iface);
393 return ddraw7_AddRef(&This->IDirectDraw7_iface);
396 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
398 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
400 TRACE("iface %p.\n", iface);
402 return ddraw1_AddRef(&This->IDirectDraw_iface);
405 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
407 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
409 TRACE("iface %p.\n", iface);
411 return ddraw1_AddRef(&This->IDirectDraw_iface);
414 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
416 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
418 TRACE("iface %p.\n", iface);
420 return ddraw1_AddRef(&This->IDirectDraw_iface);
423 /*****************************************************************************
426 * Destroys a ddraw object if all refcounts are 0. This is to share code
427 * between the IDirectDrawX::Release functions
430 * This: DirectDraw object to destroy
432 *****************************************************************************/
433 static void ddraw_destroy(IDirectDrawImpl *This)
435 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
436 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
438 /* Destroy the device window if we created one */
439 if(This->devicewindow != 0)
441 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
442 DestroyWindow(This->devicewindow);
443 This->devicewindow = 0;
446 EnterCriticalSection(&ddraw_cs);
447 list_remove(&This->ddraw_list_entry);
448 LeaveCriticalSection(&ddraw_cs);
450 /* Release the attached WineD3D stuff */
451 wined3d_device_decref(This->wined3d_device);
452 wined3d_decref(This->wineD3D);
454 /* Now free the object */
455 HeapFree(GetProcessHeap(), 0, This);
458 /*****************************************************************************
459 * IDirectDraw7::Release
461 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
463 * Returns: The new refcount
464 *****************************************************************************/
465 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
467 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
468 ULONG ref = InterlockedDecrement(&This->ref7);
470 TRACE("%p decreasing refcount to %u.\n", This, ref);
472 if (!ref && !InterlockedDecrement(&This->numIfaces))
478 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
480 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
481 ULONG ref = InterlockedDecrement(&This->ref4);
483 TRACE("%p decreasing refcount to %u.\n", This, ref);
485 if (!ref && !InterlockedDecrement(&This->numIfaces))
491 static ULONG WINAPI ddraw3_Release(IDirectDraw3 *iface)
493 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
494 ULONG ref = InterlockedDecrement(&This->ref3);
496 TRACE("%p decreasing refcount to %u.\n", This, ref);
498 if (!ref && !InterlockedDecrement(&This->numIfaces))
504 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
506 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
507 ULONG ref = InterlockedDecrement(&This->ref2);
509 TRACE("%p decreasing refcount to %u.\n", This, ref);
511 if (!ref && !InterlockedDecrement(&This->numIfaces))
517 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
519 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
520 ULONG ref = InterlockedDecrement(&This->ref1);
522 TRACE("%p decreasing refcount to %u.\n", This, ref);
524 if (!ref && !InterlockedDecrement(&This->numIfaces))
530 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
532 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
534 TRACE("iface %p.\n", iface);
536 return ddraw7_Release(&This->IDirectDraw7_iface);
539 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
541 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
543 TRACE("iface %p.\n", iface);
545 return ddraw1_Release(&This->IDirectDraw_iface);
548 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
550 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
552 TRACE("iface %p.\n", iface);
554 return ddraw1_Release(&This->IDirectDraw_iface);
557 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
559 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
561 TRACE("iface %p.\n", iface);
563 return ddraw1_Release(&This->IDirectDraw_iface);
566 /*****************************************************************************
567 * IDirectDraw methods
568 *****************************************************************************/
570 /*****************************************************************************
571 * IDirectDraw7::SetCooperativeLevel
573 * Sets the cooperative level for the DirectDraw object, and the window
574 * assigned to it. The cooperative level determines the general behavior
575 * of the DirectDraw application
577 * Warning: This is quite tricky, as it's not really documented which
578 * cooperative levels can be combined with each other. If a game fails
579 * after this function, try to check the cooperative levels passed on
580 * Windows, and if it returns something different.
582 * If you think that this function caused the failure because it writes a
583 * fixme, be sure to run again with a +ddraw trace.
585 * What is known about cooperative levels (See the ddraw modes test):
586 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
587 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
588 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
589 * DDSCL_FULLSCREEN can be activated
590 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
592 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
593 * DDSCL_SETFOCUSWINDOW (partially),
594 * DDSCL_MULTITHREADED (work in progress)
596 * Unhandled flags, which should be implemented
597 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
598 * expect any difference to a normal window for wine)
599 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
600 * rendering (Possible test case: Half-Life)
602 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
604 * These don't seem very important for wine:
605 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
608 * DD_OK if the cooperative level was set successfully
609 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
610 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
611 * (Probably others too, have to investigate)
613 *****************************************************************************/
614 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND hwnd, DWORD cooplevel)
616 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
619 TRACE("iface %p, window %p, flags %#x.\n", iface, hwnd, cooplevel);
620 DDRAW_dump_cooperativelevel(cooplevel);
622 EnterCriticalSection(&ddraw_cs);
624 /* Get the old window */
625 window = This->dest_window;
627 /* Tests suggest that we need one of them: */
628 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
632 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
633 LeaveCriticalSection(&ddraw_cs);
634 return DDERR_INVALIDPARAMS;
637 /* Handle those levels first which set various hwnds */
638 if(cooplevel & DDSCL_SETFOCUSWINDOW)
640 /* This isn't compatible with a lot of flags */
641 if(cooplevel & ( DDSCL_MULTITHREADED |
642 DDSCL_CREATEDEVICEWINDOW |
647 DDSCL_SETDEVICEWINDOW |
652 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
653 LeaveCriticalSection(&ddraw_cs);
654 return DDERR_INVALIDPARAMS;
657 if( (This->cooperative_level & DDSCL_EXCLUSIVE) && window )
659 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
660 LeaveCriticalSection(&ddraw_cs);
661 return DDERR_HWNDALREADYSET;
664 This->focuswindow = hwnd;
665 /* Won't use the hwnd param for anything else */
668 /* Use the focus window for drawing too */
669 This->dest_window = This->focuswindow;
671 /* Destroy the device window, if we have one */
672 if(This->devicewindow)
674 DestroyWindow(This->devicewindow);
675 This->devicewindow = NULL;
678 LeaveCriticalSection(&ddraw_cs);
682 if(cooplevel & DDSCL_EXCLUSIVE)
684 if( !(cooplevel & DDSCL_FULLSCREEN) || !hwnd )
686 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN and a window\n", This);
687 LeaveCriticalSection(&ddraw_cs);
688 return DDERR_INVALIDPARAMS;
691 else if( !(cooplevel & DDSCL_NORMAL) )
693 TRACE("(%p) SetCooperativeLevel needs at least SetFocusWindow or Exclusive or Normal mode\n", This);
694 LeaveCriticalSection(&ddraw_cs);
695 return DDERR_INVALIDPARAMS;
698 if ((This->cooperative_level & DDSCL_EXCLUSIVE)
699 && (hwnd != window || !(cooplevel & DDSCL_EXCLUSIVE)))
700 wined3d_device_release_focus_window(This->wined3d_device);
702 if ((cooplevel & DDSCL_FULLSCREEN) != (This->cooperative_level & DDSCL_FULLSCREEN) || hwnd != window)
704 if (This->cooperative_level & DDSCL_FULLSCREEN)
705 wined3d_device_restore_fullscreen_window(This->wined3d_device, window);
707 if (cooplevel & DDSCL_FULLSCREEN)
709 WINED3DDISPLAYMODE display_mode;
711 wined3d_get_adapter_display_mode(This->wineD3D, WINED3DADAPTER_DEFAULT, &display_mode);
712 wined3d_device_setup_fullscreen_window(This->wined3d_device, hwnd,
713 display_mode.Width, display_mode.Height);
717 if ((cooplevel & DDSCL_EXCLUSIVE)
718 && (hwnd != window || !(This->cooperative_level & DDSCL_EXCLUSIVE)))
720 HRESULT hr = wined3d_device_acquire_focus_window(This->wined3d_device, hwnd);
723 ERR("Failed to acquire focus window, hr %#x.\n", hr);
724 LeaveCriticalSection(&ddraw_cs);
729 /* Don't override focus windows or private device windows */
730 if (hwnd && !This->focuswindow && !This->devicewindow && (hwnd != window))
731 This->dest_window = hwnd;
733 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
735 /* Don't create a device window if a focus window is set */
736 if( !(This->focuswindow) )
738 HWND devicewindow = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DDraw device window",
739 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
740 NULL, NULL, NULL, NULL);
743 ERR("Failed to create window, last error %#x.\n", GetLastError());
744 LeaveCriticalSection(&ddraw_cs);
748 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
749 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
751 This->devicewindow = devicewindow;
752 This->dest_window = devicewindow;
756 if (cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
757 wined3d_device_set_multithreaded(This->wined3d_device);
759 /* Unhandled flags */
760 if(cooplevel & DDSCL_ALLOWREBOOT)
761 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
762 if(cooplevel & DDSCL_ALLOWMODEX)
763 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
764 if(cooplevel & DDSCL_FPUSETUP)
765 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
767 /* Store the cooperative_level */
768 This->cooperative_level = cooplevel;
769 TRACE("SetCooperativeLevel retuning DD_OK\n");
770 LeaveCriticalSection(&ddraw_cs);
774 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
776 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
778 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
780 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
783 static HRESULT WINAPI ddraw3_SetCooperativeLevel(IDirectDraw3 *iface, HWND window, DWORD flags)
785 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
787 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
789 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
792 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
794 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
796 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
798 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
801 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
803 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
805 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
807 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
810 /*****************************************************************************
812 * Helper function for SetDisplayMode and RestoreDisplayMode
814 * Implements DirectDraw's SetDisplayMode, but ignores the value of
815 * ForceRefreshRate, since it is already handled by
816 * ddraw7_SetDisplayMode. RestoreDisplayMode can use this function
817 * without worrying that ForceRefreshRate will override the refresh rate. For
818 * argument and return value documentation, see
819 * ddraw7_SetDisplayMode.
821 *****************************************************************************/
822 static HRESULT ddraw_set_display_mode(IDirectDrawImpl *ddraw, DWORD Width, DWORD Height,
823 DWORD BPP, DWORD RefreshRate, DWORD Flags)
825 enum wined3d_format_id format;
826 WINED3DDISPLAYMODE Mode;
829 TRACE("ddraw %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n", ddraw, Width,
830 Height, BPP, RefreshRate, Flags);
832 EnterCriticalSection(&ddraw_cs);
833 if( !Width || !Height )
835 ERR("Width %u, Height %u, what to do?\n", Width, Height);
836 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
837 LeaveCriticalSection(&ddraw_cs);
843 case 8: format = WINED3DFMT_P8_UINT; break;
844 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
845 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
846 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
847 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
848 default: format = WINED3DFMT_UNKNOWN; break;
851 if (FAILED(hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &Mode)))
853 ERR("Failed to get current display mode, hr %#x.\n", hr);
855 else if (Mode.Width == Width
856 && Mode.Height == Height
857 && Mode.Format == format
858 && Mode.RefreshRate == RefreshRate)
860 TRACE("Skipping redundant mode setting call.\n");
861 LeaveCriticalSection(&ddraw_cs);
865 /* Check the exclusive mode
866 if(!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
867 return DDERR_NOEXCLUSIVEMODE;
868 * This is WRONG. Don't know if the SDK is completely
869 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
870 * is returned, but Half-Life 1.1.1.1 (Steam version)
875 Mode.Height = Height;
876 Mode.RefreshRate = RefreshRate;
877 Mode.Format = format;
879 /* TODO: The possible return values from msdn suggest that
880 * the screen mode can't be changed if a surface is locked
881 * or some drawing is in progress
884 /* TODO: Lose the primary surface */
885 hr = wined3d_device_set_display_mode(ddraw->wined3d_device, 0, &Mode);
887 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
889 wined3d_device_restore_fullscreen_window(ddraw->wined3d_device, ddraw->dest_window);
890 wined3d_device_setup_fullscreen_window(ddraw->wined3d_device, ddraw->dest_window, Width, Height);
893 LeaveCriticalSection(&ddraw_cs);
896 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
901 /*****************************************************************************
902 * IDirectDraw7::SetDisplayMode
904 * Sets the display screen resolution, color depth and refresh frequency
905 * when in fullscreen mode (in theory).
906 * Possible return values listed in the SDK suggest that this method fails
907 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
908 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
909 * It seems to be valid to pass 0 for With and Height, this has to be tested
910 * It could mean that the current video mode should be left as-is. (But why
914 * Height, Width: Screen dimension
915 * BPP: Color depth in Bits per pixel
916 * Refreshrate: Screen refresh rate
922 *****************************************************************************/
923 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD Width, DWORD Height,
924 DWORD BPP, DWORD RefreshRate, DWORD Flags)
926 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
928 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
929 iface, Width, Height, BPP, RefreshRate, Flags);
931 if (force_refresh_rate != 0)
933 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
934 RefreshRate, force_refresh_rate);
935 RefreshRate = force_refresh_rate;
938 return ddraw_set_display_mode(This, Width, Height, BPP, RefreshRate, Flags);
941 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
942 DWORD bpp, DWORD refresh_rate, DWORD flags)
944 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
946 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
947 iface, width, height, bpp, refresh_rate, flags);
949 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
952 static HRESULT WINAPI ddraw3_SetDisplayMode(IDirectDraw3 *iface, DWORD width, DWORD height,
953 DWORD bpp, DWORD refresh_rate, DWORD flags)
955 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
957 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
958 iface, width, height, bpp, refresh_rate, flags);
960 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
963 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
964 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
966 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
968 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
969 iface, width, height, bpp, refresh_rate, flags);
971 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
974 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
976 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
978 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
980 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, 0, 0);
983 /*****************************************************************************
984 * IDirectDraw7::RestoreDisplayMode
986 * Restores the display mode to what it was at creation time. Basically.
988 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
989 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
990 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
991 * -> DD_1 is released. The screen should be left at 1024x768x32.
992 * -> DD_2 is released. The screen should be set to 1400x1050x32
993 * This case is unhandled right now, but Empire Earth does it this way.
994 * (But perhaps there is something in SetCooperativeLevel to prevent this)
996 * The msdn says that this method resets the display mode to what it was before
997 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
1001 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
1003 *****************************************************************************/
1004 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
1006 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1008 TRACE("iface %p.\n", iface);
1010 return ddraw_set_display_mode(This, This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
1013 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
1015 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1017 TRACE("iface %p.\n", iface);
1019 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1022 static HRESULT WINAPI ddraw3_RestoreDisplayMode(IDirectDraw3 *iface)
1024 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1026 TRACE("iface %p.\n", iface);
1028 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1031 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
1033 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1035 TRACE("iface %p.\n", iface);
1037 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1040 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
1042 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1044 TRACE("iface %p.\n", iface);
1046 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1049 /*****************************************************************************
1050 * IDirectDraw7::GetCaps
1052 * Returns the drives capabilities
1054 * Used for version 1, 2, 4 and 7
1057 * DriverCaps: Structure to write the Hardware accelerated caps to
1058 * HelCaps: Structure to write the emulation caps to
1061 * This implementation returns DD_OK only
1063 *****************************************************************************/
1064 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1066 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1068 WINED3DCAPS winecaps;
1070 DDSCAPS2 ddscaps = {0, 0, 0, 0};
1072 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1074 /* One structure must be != NULL */
1075 if( (!DriverCaps) && (!HELCaps) )
1077 ERR("(%p) Invalid params to ddraw7_GetCaps\n", This);
1078 return DDERR_INVALIDPARAMS;
1081 memset(&caps, 0, sizeof(caps));
1082 memset(&winecaps, 0, sizeof(winecaps));
1083 caps.dwSize = sizeof(caps);
1084 EnterCriticalSection(&ddraw_cs);
1085 hr = wined3d_device_get_device_caps(This->wined3d_device, &winecaps);
1088 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1089 LeaveCriticalSection(&ddraw_cs);
1093 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1094 LeaveCriticalSection(&ddraw_cs);
1096 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1100 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
1101 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
1102 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
1103 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
1104 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
1105 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
1106 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
1107 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
1108 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
1109 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
1110 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
1111 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
1112 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
1113 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
1114 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
1116 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
1119 if(DefaultSurfaceType == SURFACE_GDI) {
1120 caps.dwCaps &= ~DDCAPS_3D;
1121 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
1123 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
1124 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1125 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
1130 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1131 if (TRACE_ON(ddraw))
1133 TRACE("Driver Caps :\n");
1134 DDRAW_dump_DDCAPS(DriverCaps);
1140 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1141 if (TRACE_ON(ddraw))
1143 TRACE("HEL Caps :\n");
1144 DDRAW_dump_DDCAPS(HELCaps);
1151 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1153 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1155 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1157 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1160 static HRESULT WINAPI ddraw3_GetCaps(IDirectDraw3 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1162 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1164 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1166 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1169 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1171 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1173 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1175 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1178 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1180 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1182 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1184 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1187 /*****************************************************************************
1188 * IDirectDraw7::Compact
1190 * No idea what it does, MSDN says it's not implemented.
1193 * DD_OK, but this is unchecked
1195 *****************************************************************************/
1196 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1198 TRACE("iface %p.\n", iface);
1203 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1205 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1207 TRACE("iface %p.\n", iface);
1209 return ddraw7_Compact(&This->IDirectDraw7_iface);
1212 static HRESULT WINAPI ddraw3_Compact(IDirectDraw3 *iface)
1214 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1216 TRACE("iface %p.\n", iface);
1218 return ddraw7_Compact(&This->IDirectDraw7_iface);
1221 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1223 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1225 TRACE("iface %p.\n", iface);
1227 return ddraw7_Compact(&This->IDirectDraw7_iface);
1230 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1232 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1234 TRACE("iface %p.\n", iface);
1236 return ddraw7_Compact(&This->IDirectDraw7_iface);
1239 /*****************************************************************************
1240 * IDirectDraw7::GetDisplayMode
1242 * Returns information about the current display mode
1244 * Exists in Version 1, 2, 4 and 7
1247 * DDSD: Address of a surface description structure to write the info to
1252 *****************************************************************************/
1253 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1255 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1257 WINED3DDISPLAYMODE Mode;
1260 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1262 EnterCriticalSection(&ddraw_cs);
1263 /* This seems sane */
1266 LeaveCriticalSection(&ddraw_cs);
1267 return DDERR_INVALIDPARAMS;
1270 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
1271 * so one method can be used for all versions (Hopefully) */
1272 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &Mode);
1275 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
1276 LeaveCriticalSection(&ddraw_cs);
1280 Size = DDSD->dwSize;
1281 memset(DDSD, 0, Size);
1283 DDSD->dwSize = Size;
1284 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1285 DDSD->dwWidth = Mode.Width;
1286 DDSD->dwHeight = Mode.Height;
1287 DDSD->u2.dwRefreshRate = 60;
1288 DDSD->ddsCaps.dwCaps = 0;
1289 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1290 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
1291 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1295 TRACE("Returning surface desc :\n");
1296 DDRAW_dump_surface_desc(DDSD);
1299 LeaveCriticalSection(&ddraw_cs);
1303 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1305 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1307 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1309 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, surface_desc);
1312 static HRESULT WINAPI ddraw3_GetDisplayMode(IDirectDraw3 *iface, DDSURFACEDESC *surface_desc)
1314 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1316 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1318 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1321 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1323 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1325 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1327 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1330 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1332 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1334 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1336 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1339 /*****************************************************************************
1340 * IDirectDraw7::GetFourCCCodes
1342 * Returns an array of supported FourCC codes.
1344 * Exists in Version 1, 2, 4 and 7
1347 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1348 * of enumerated codes
1349 * Codes: Pointer to an array of DWORDs where the supported codes are written
1353 * Always returns DD_OK, as it's a stub for now
1355 *****************************************************************************/
1356 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1358 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1359 static const enum wined3d_format_id formats[] =
1361 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1362 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1363 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1365 DWORD count = 0, i, outsize;
1367 WINED3DDISPLAYMODE d3ddm;
1368 WINED3DSURFTYPE type = This->ImplType;
1370 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1372 wined3d_device_get_display_mode(This->wined3d_device, 0, &d3ddm);
1374 outsize = NumCodes && Codes ? *NumCodes : 0;
1376 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
1378 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1380 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, WINED3DDEVTYPE_HAL,
1381 d3ddm.Format, 0, WINED3DRTYPE_SURFACE, formats[i], type);
1384 if (count < outsize)
1385 Codes[count] = formats[i];
1390 TRACE("Returning %u FourCC codes\n", count);
1397 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1399 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1401 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1403 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1406 static HRESULT WINAPI ddraw3_GetFourCCCodes(IDirectDraw3 *iface, DWORD *codes_count, DWORD *codes)
1408 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1410 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1412 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1415 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1417 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1419 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1421 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1424 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1426 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1428 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1430 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1433 /*****************************************************************************
1434 * IDirectDraw7::GetMonitorFrequency
1436 * Returns the monitor's frequency
1438 * Exists in Version 1, 2, 4 and 7
1441 * Freq: Pointer to a DWORD to write the frequency to
1444 * Always returns DD_OK
1446 *****************************************************************************/
1447 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *Freq)
1449 FIXME("iface %p, frequency %p stub!\n", iface, Freq);
1451 /* Ideally this should be in WineD3D, as it concerns the screen setup,
1452 * but for now this should make the games happy
1458 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1460 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1462 TRACE("iface %p, frequency %p.\n", iface, frequency);
1464 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1467 static HRESULT WINAPI ddraw3_GetMonitorFrequency(IDirectDraw3 *iface, DWORD *frequency)
1469 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1471 TRACE("iface %p, frequency %p.\n", iface, frequency);
1473 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1476 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1478 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1480 TRACE("iface %p, frequency %p.\n", iface, frequency);
1482 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1485 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1487 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1489 TRACE("iface %p, frequency %p.\n", iface, frequency);
1491 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1494 /*****************************************************************************
1495 * IDirectDraw7::GetVerticalBlankStatus
1497 * Returns the Vertical blank status of the monitor. This should be in WineD3D
1498 * too basically, but as it's a semi stub, I didn't create a function there
1501 * status: Pointer to a BOOL to be filled with the vertical blank status
1505 * DDERR_INVALIDPARAMS if status is NULL
1507 *****************************************************************************/
1508 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1510 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1512 TRACE("iface %p, status %p.\n", iface, status);
1514 /* This looks sane, the MSDN suggests it too */
1515 EnterCriticalSection(&ddraw_cs);
1518 LeaveCriticalSection(&ddraw_cs);
1519 return DDERR_INVALIDPARAMS;
1522 *status = This->fake_vblank;
1523 This->fake_vblank = !This->fake_vblank;
1524 LeaveCriticalSection(&ddraw_cs);
1528 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1530 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1532 TRACE("iface %p, status %p.\n", iface, status);
1534 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1537 static HRESULT WINAPI ddraw3_GetVerticalBlankStatus(IDirectDraw3 *iface, BOOL *status)
1539 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1541 TRACE("iface %p, status %p.\n", iface, status);
1543 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1546 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1548 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1550 TRACE("iface %p, status %p.\n", iface, status);
1552 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1555 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1557 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1559 TRACE("iface %p, status %p.\n", iface, status);
1561 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1564 /*****************************************************************************
1565 * IDirectDraw7::GetAvailableVidMem
1567 * Returns the total and free video memory
1570 * Caps: Specifies the memory type asked for
1571 * total: Pointer to a DWORD to be filled with the total memory
1572 * free: Pointer to a DWORD to be filled with the free memory
1576 * DDERR_INVALIDPARAMS of free and total are NULL
1578 *****************************************************************************/
1579 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total,
1582 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1584 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1588 TRACE("(%p) Asked for memory with description: ", This);
1589 DDRAW_dump_DDSCAPS2(Caps);
1591 EnterCriticalSection(&ddraw_cs);
1593 /* Todo: System memory vs local video memory vs non-local video memory
1594 * The MSDN also mentions differences between texture memory and other
1595 * resources, but that's not important
1598 if( (!total) && (!free) )
1600 LeaveCriticalSection(&ddraw_cs);
1601 return DDERR_INVALIDPARAMS;
1605 *total = This->total_vidmem;
1607 *free = wined3d_device_get_available_texture_mem(This->wined3d_device);
1609 LeaveCriticalSection(&ddraw_cs);
1613 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1614 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1616 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1618 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1620 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, caps, total, free);
1623 static HRESULT WINAPI ddraw3_GetAvailableVidMem(IDirectDraw3 *iface, DDSCAPS *caps, DWORD *total,
1626 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1629 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1631 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1632 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, &caps2, total, free);
1635 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1636 DDSCAPS *caps, DWORD *total, DWORD *free)
1638 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1641 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1643 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1644 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, &caps2, total, free);
1647 /*****************************************************************************
1648 * IDirectDraw7::Initialize
1650 * Initializes a DirectDraw interface.
1653 * GUID: Interface identifier. Well, don't know what this is really good
1657 * Returns DD_OK on the first call,
1658 * DDERR_ALREADYINITIALIZED on repeated calls
1660 *****************************************************************************/
1661 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *Guid)
1663 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1665 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(Guid));
1667 if(This->initialized)
1669 return DDERR_ALREADYINITIALIZED;
1677 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1679 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1681 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1683 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1686 static HRESULT WINAPI ddraw3_Initialize(IDirectDraw3 *iface, GUID *guid)
1688 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1690 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1692 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1695 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1697 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1699 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1701 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1704 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1706 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1708 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1710 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1713 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1715 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1720 /*****************************************************************************
1721 * IDirectDraw7::FlipToGDISurface
1723 * "Makes the surface that the GDI writes to the primary surface"
1724 * Looks like some windows specific thing we don't have to care about.
1725 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1726 * show error boxes ;)
1727 * Well, just return DD_OK.
1730 * Always returns DD_OK
1732 *****************************************************************************/
1733 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1735 FIXME("iface %p stub!\n", iface);
1740 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1742 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1744 TRACE("iface %p.\n", iface);
1746 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1749 static HRESULT WINAPI ddraw3_FlipToGDISurface(IDirectDraw3 *iface)
1751 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1753 TRACE("iface %p.\n", iface);
1755 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1758 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1760 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1762 TRACE("iface %p.\n", iface);
1764 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1767 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1769 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1771 TRACE("iface %p.\n", iface);
1773 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1776 /*****************************************************************************
1777 * IDirectDraw7::WaitForVerticalBlank
1779 * This method allows applications to get in sync with the vertical blank
1781 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1782 * redraw the screen, most likely because of this stub
1785 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1786 * or DDWAITVB_BLOCKEND
1787 * h: Not used, according to MSDN
1790 * Always returns DD_OK
1792 *****************************************************************************/
1793 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1797 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1799 /* This function is called often, so print the fixme only once */
1802 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1806 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1807 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1808 return DDERR_UNSUPPORTED; /* unchecked */
1813 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1815 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1817 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1819 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1822 static HRESULT WINAPI ddraw3_WaitForVerticalBlank(IDirectDraw3 *iface, DWORD flags, HANDLE event)
1824 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1826 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1828 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1831 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1833 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1835 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1837 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1840 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1842 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1844 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1846 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1849 /*****************************************************************************
1850 * IDirectDraw7::GetScanLine
1852 * Returns the scan line that is being drawn on the monitor
1855 * Scanline: Address to write the scan line value to
1858 * Always returns DD_OK
1860 *****************************************************************************/
1861 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1863 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1864 static BOOL hide = FALSE;
1865 WINED3DDISPLAYMODE Mode;
1867 TRACE("iface %p, line %p.\n", iface, Scanline);
1869 /* This function is called often, so print the fixme only once */
1870 EnterCriticalSection(&ddraw_cs);
1873 FIXME("iface %p, line %p partial stub!\n", iface, Scanline);
1877 wined3d_device_get_display_mode(This->wined3d_device, 0, &Mode);
1879 /* Fake the line sweeping of the monitor */
1880 /* FIXME: We should synchronize with a source to keep the refresh rate */
1881 *Scanline = This->cur_scanline++;
1882 /* Assume 20 scan lines in the vertical blank */
1883 if (This->cur_scanline >= Mode.Height + 20)
1884 This->cur_scanline = 0;
1886 LeaveCriticalSection(&ddraw_cs);
1890 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1892 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1894 TRACE("iface %p, line %p.\n", iface, line);
1896 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1899 static HRESULT WINAPI ddraw3_GetScanLine(IDirectDraw3 *iface, DWORD *line)
1901 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1903 TRACE("iface %p, line %p.\n", iface, line);
1905 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1908 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1910 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1912 TRACE("iface %p, line %p.\n", iface, line);
1914 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1917 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1919 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1921 TRACE("iface %p, line %p.\n", iface, line);
1923 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1926 /*****************************************************************************
1927 * IDirectDraw7::TestCooperativeLevel
1929 * Informs the application about the state of the video adapter, depending
1930 * on the cooperative level
1933 * DD_OK if the device is in a sane state
1934 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1935 * if the state is not correct(See below)
1937 *****************************************************************************/
1938 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1940 TRACE("iface %p.\n", iface);
1945 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
1947 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1949 TRACE("iface %p.\n", iface);
1951 return ddraw7_TestCooperativeLevel(&This->IDirectDraw7_iface);
1954 /*****************************************************************************
1955 * IDirectDraw7::GetGDISurface
1957 * Returns the surface that GDI is treating as the primary surface.
1958 * For Wine this is the front buffer
1961 * GDISurface: Address to write the surface pointer to
1964 * DD_OK if the surface was found
1965 * DDERR_NOTFOUND if the GDI surface wasn't found
1967 *****************************************************************************/
1968 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
1970 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1971 struct wined3d_surface *wined3d_surface;
1972 IDirectDrawSurface7 *ddsurf;
1976 TRACE("iface %p, surface %p.\n", iface, GDISurface);
1978 /* Get the back buffer from the wineD3DDevice and search its
1979 * attached surfaces for the front buffer. */
1980 EnterCriticalSection(&ddraw_cs);
1981 hr = wined3d_device_get_back_buffer(This->wined3d_device,
1982 0, 0, WINED3DBACKBUFFER_TYPE_MONO, &wined3d_surface);
1983 if (FAILED(hr) || !wined3d_surface)
1985 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1986 LeaveCriticalSection(&ddraw_cs);
1987 return DDERR_NOTFOUND;
1990 ddsurf = wined3d_surface_get_parent(wined3d_surface);
1991 wined3d_surface_decref(wined3d_surface);
1993 /* Find the front buffer */
1994 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1995 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
2000 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
2003 /* The AddRef is OK this time */
2004 LeaveCriticalSection(&ddraw_cs);
2008 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
2010 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2012 TRACE("iface %p, surface %p.\n", iface, surface);
2014 return ddraw7_GetGDISurface(&This->IDirectDraw7_iface, (IDirectDrawSurface7 **)surface);
2017 static HRESULT WINAPI ddraw3_GetGDISurface(IDirectDraw3 *iface, IDirectDrawSurface **surface)
2019 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
2020 IDirectDrawSurface7 *surface7;
2023 TRACE("iface %p, surface %p.\n", iface, surface);
2025 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2026 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
2031 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2033 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
2034 IDirectDrawSurface7 *surface7;
2037 TRACE("iface %p, surface %p.\n", iface, surface);
2039 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2040 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
2045 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2047 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
2048 IDirectDrawSurface7 *surface7;
2051 TRACE("iface %p, surface %p.\n", iface, surface);
2053 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2054 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
2059 struct displaymodescallback_context
2061 LPDDENUMMODESCALLBACK func;
2065 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2067 struct displaymodescallback_context *cbcontext = context;
2070 memcpy(&desc, surface_desc, sizeof(desc));
2071 desc.dwSize = sizeof(desc);
2073 return cbcontext->func(&desc, cbcontext->context);
2076 /*****************************************************************************
2077 * IDirectDraw7::EnumDisplayModes
2079 * Enumerates the supported Display modes. The modes can be filtered with
2080 * the DDSD parameter.
2083 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2084 * versions (3 and older?) this is reserved and must be 0.
2085 * DDSD: Surface description to filter the modes
2086 * Context: Pointer passed back to the callback function
2087 * cb: Application-provided callback function
2091 * DDERR_INVALIDPARAMS if the callback wasn't set
2093 *****************************************************************************/
2094 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2095 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2097 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2098 unsigned int modenum, fmt;
2099 WINED3DDISPLAYMODE mode;
2100 DDSURFACEDESC2 callback_sd;
2101 WINED3DDISPLAYMODE *enum_modes = NULL;
2102 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
2103 DDPIXELFORMAT pixelformat;
2105 static const enum wined3d_format_id checkFormatList[] =
2107 WINED3DFMT_B8G8R8X8_UNORM,
2108 WINED3DFMT_B5G6R5_UNORM,
2112 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2113 iface, Flags, DDSD, Context, cb);
2115 EnterCriticalSection(&ddraw_cs);
2116 /* This looks sane */
2119 LeaveCriticalSection(&ddraw_cs);
2120 return DDERR_INVALIDPARAMS;
2123 if(!(Flags & DDEDM_REFRESHRATES))
2125 enum_mode_array_size = 16;
2126 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
2129 ERR("Out of memory\n");
2130 LeaveCriticalSection(&ddraw_cs);
2131 return DDERR_OUTOFMEMORY;
2135 pixelformat.dwSize = sizeof(pixelformat);
2136 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2139 while (wined3d_enum_adapter_modes(This->wineD3D, WINED3DADAPTER_DEFAULT,
2140 checkFormatList[fmt], modenum++, &mode) == WINED3D_OK)
2142 PixelFormat_WineD3DtoDD(&pixelformat, mode.Format);
2145 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
2146 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
2147 if(DDSD->dwFlags & DDSD_REFRESHRATE && mode.RefreshRate != DDSD->u2.dwRefreshRate) continue;
2148 if (DDSD->dwFlags & DDSD_PIXELFORMAT &&
2149 pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount) continue;
2152 if(!(Flags & DDEDM_REFRESHRATES))
2154 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
2155 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
2156 * to be reduced to a single unique result in such case.
2161 for (i = 0; i < enum_mode_count; i++)
2163 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
2164 enum_modes[i].Format == mode.Format)
2174 memset(&callback_sd, 0, sizeof(callback_sd));
2175 callback_sd.dwSize = sizeof(callback_sd);
2176 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2178 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2179 if(Flags & DDEDM_REFRESHRATES)
2181 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
2184 callback_sd.dwWidth = mode.Width;
2185 callback_sd.dwHeight = mode.Height;
2187 callback_sd.u4.ddpfPixelFormat=pixelformat;
2189 /* Calc pitch and DWORD align like MSDN says */
2190 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
2191 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2193 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2194 callback_sd.u2.dwRefreshRate);
2196 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2198 TRACE("Application asked to terminate the enumeration\n");
2199 HeapFree(GetProcessHeap(), 0, enum_modes);
2200 LeaveCriticalSection(&ddraw_cs);
2204 if(!(Flags & DDEDM_REFRESHRATES))
2206 if (enum_mode_count == enum_mode_array_size)
2208 WINED3DDISPLAYMODE *new_enum_modes;
2210 enum_mode_array_size *= 2;
2211 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
2213 if (!new_enum_modes)
2215 ERR("Out of memory\n");
2216 HeapFree(GetProcessHeap(), 0, enum_modes);
2217 LeaveCriticalSection(&ddraw_cs);
2218 return DDERR_OUTOFMEMORY;
2221 enum_modes = new_enum_modes;
2224 enum_modes[enum_mode_count++] = mode;
2229 TRACE("End of enumeration\n");
2230 HeapFree(GetProcessHeap(), 0, enum_modes);
2231 LeaveCriticalSection(&ddraw_cs);
2235 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2236 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2238 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2240 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2241 iface, flags, surface_desc, context, callback);
2243 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, surface_desc, context, callback);
2246 static HRESULT WINAPI ddraw3_EnumDisplayModes(IDirectDraw3 *iface, DWORD flags,
2247 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2249 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
2250 struct displaymodescallback_context cbcontext;
2252 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2253 iface, flags, surface_desc, context, callback);
2255 cbcontext.func = callback;
2256 cbcontext.context = context;
2258 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
2259 &cbcontext, EnumDisplayModesCallbackThunk);
2262 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2263 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2265 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
2266 struct displaymodescallback_context cbcontext;
2268 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2269 iface, flags, surface_desc, context, callback);
2271 cbcontext.func = callback;
2272 cbcontext.context = context;
2274 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
2275 &cbcontext, EnumDisplayModesCallbackThunk);
2278 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2279 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2281 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
2282 struct displaymodescallback_context cbcontext;
2284 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2285 iface, flags, surface_desc, context, callback);
2287 cbcontext.func = callback;
2288 cbcontext.context = context;
2290 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
2291 &cbcontext, EnumDisplayModesCallbackThunk);
2294 /*****************************************************************************
2295 * IDirectDraw7::EvaluateMode
2297 * Used with IDirectDraw7::StartModeTest to test video modes.
2298 * EvaluateMode is used to pass or fail a mode, and continue with the next
2302 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2303 * Timeout: Returns the amount of seconds left before the mode would have
2304 * been failed automatically
2307 * This implementation always DD_OK, because it's a stub
2309 *****************************************************************************/
2310 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2312 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2314 /* When implementing this, implement it in WineD3D */
2319 /*****************************************************************************
2320 * IDirectDraw7::GetDeviceIdentifier
2322 * Returns the device identifier, which gives information about the driver
2323 * Our device identifier is defined at the beginning of this file.
2326 * DDDI: Address for the returned structure
2327 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2330 * On success it returns DD_OK
2331 * DDERR_INVALIDPARAMS if DDDI is NULL
2333 *****************************************************************************/
2334 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2335 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2337 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2340 return DDERR_INVALIDPARAMS;
2342 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2343 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2344 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2345 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2346 * bytes too long. So only copy the relevant part of the structure
2349 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2353 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2354 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2356 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2357 DDDEVICEIDENTIFIER2 identifier2;
2360 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2362 hr = ddraw7_GetDeviceIdentifier(&This->IDirectDraw7_iface, &identifier2, flags);
2363 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2368 /*****************************************************************************
2369 * IDirectDraw7::GetSurfaceFromDC
2371 * Returns the Surface for a GDI device context handle.
2372 * Is this related to IDirectDrawSurface::GetDC ???
2375 * hdc: hdc to return the surface for
2376 * Surface: Address to write the surface pointer to
2379 * Always returns DD_OK because it's a stub
2381 *****************************************************************************/
2382 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc,
2383 IDirectDrawSurface7 **Surface)
2385 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2386 struct wined3d_surface *wined3d_surface;
2389 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2391 if (!Surface) return E_INVALIDARG;
2393 hr = wined3d_device_get_surface_from_dc(This->wined3d_device, hdc, &wined3d_surface);
2396 TRACE("No surface found for dc %p.\n", hdc);
2398 return DDERR_NOTFOUND;
2401 *Surface = wined3d_surface_get_parent(wined3d_surface);
2402 IDirectDrawSurface7_AddRef(*Surface);
2403 TRACE("Returning surface %p.\n", Surface);
2407 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2408 IDirectDrawSurface4 **surface)
2410 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2411 IDirectDrawSurface7 *surface7;
2414 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2416 if (!surface) return E_INVALIDARG;
2418 hr = ddraw7_GetSurfaceFromDC(&This->IDirectDraw7_iface, dc, &surface7);
2419 *surface = surface7 ? (IDirectDrawSurface4 *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
2424 static HRESULT WINAPI ddraw3_GetSurfaceFromDC(IDirectDraw3 *iface, HDC dc,
2425 IDirectDrawSurface **surface)
2427 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
2429 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2431 return ddraw7_GetSurfaceFromDC(&This->IDirectDraw7_iface, dc, (IDirectDrawSurface7 **)surface);
2434 /*****************************************************************************
2435 * IDirectDraw7::RestoreAllSurfaces
2437 * Calls the restore method of all surfaces
2442 * Always returns DD_OK because it's a stub
2444 *****************************************************************************/
2445 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2447 FIXME("iface %p stub!\n", iface);
2449 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2450 * get their parent and call their restore method. Do not implement
2451 * it in WineD3D, as restoring a surface means re-creating the
2457 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2459 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2461 TRACE("iface %p.\n", iface);
2463 return ddraw7_RestoreAllSurfaces(&This->IDirectDraw7_iface);
2466 /*****************************************************************************
2467 * IDirectDraw7::StartModeTest
2469 * Tests the specified video modes to update the system registry with
2470 * refresh rate information. StartModeTest starts the mode test,
2471 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2472 * isn't called within 15 seconds, the mode is failed automatically
2474 * As refresh rates are handled by the X server, I don't think this
2475 * Method is important
2478 * Modes: An array of mode specifications
2479 * NumModes: The number of modes in Modes
2480 * Flags: Some flags...
2483 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2484 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2487 *****************************************************************************/
2488 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2490 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2491 iface, Modes, NumModes, Flags);
2493 /* This looks sane */
2494 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2496 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2497 * As it is not, DDERR_TESTFINISHED is returned
2498 * (hopefully that's correct
2500 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2501 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2507 /*****************************************************************************
2508 * ddraw_recreate_surfaces_cb
2510 * Enumeration callback for ddraw_recreate_surface.
2511 * It re-recreates the WineD3DSurface. It's pretty straightforward
2513 *****************************************************************************/
2514 HRESULT WINAPI ddraw_recreate_surfaces_cb(IDirectDrawSurface7 *surf, DDSURFACEDESC2 *desc, void *Context)
2516 IDirectDrawSurfaceImpl *surfImpl = (IDirectDrawSurfaceImpl *)surf;
2517 struct wined3d_resource_desc wined3d_desc;
2518 struct wined3d_resource *wined3d_resource;
2519 IDirectDrawImpl *This = surfImpl->ddraw;
2520 struct wined3d_surface *wined3d_surface;
2521 struct wined3d_swapchain *swapchain;
2522 struct wined3d_clipper *clipper;
2526 TRACE("surface %p, surface_desc %p, context %p.\n",
2527 surf, desc, Context);
2529 /* For the enumeration */
2530 IDirectDrawSurface7_Release(surf);
2532 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
2534 /* Get the objects */
2535 swapchain = surfImpl->wined3d_swapchain;
2536 surfImpl->wined3d_swapchain = NULL;
2537 wined3d_surface = surfImpl->wined3d_surface;
2539 /* get the clipper */
2540 clipper = wined3d_surface_get_clipper(wined3d_surface);
2542 /* Get the surface properties */
2543 wined3d_resource = wined3d_surface_get_resource(wined3d_surface);
2544 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
2546 parent = wined3d_surface_get_parent(wined3d_surface);
2547 hr = wined3d_surface_create(This->wined3d_device, wined3d_desc.width, wined3d_desc.height,
2548 wined3d_desc.format, TRUE, FALSE, surfImpl->mipmap_level, wined3d_desc.usage, wined3d_desc.pool,
2549 wined3d_desc.multisample_type, wined3d_desc.multisample_quality, This->ImplType,
2550 parent, &ddraw_surface_wined3d_parent_ops, &surfImpl->wined3d_surface);
2553 surfImpl->wined3d_surface = wined3d_surface;
2557 wined3d_surface_set_clipper(surfImpl->wined3d_surface, clipper);
2559 /* TODO: Copy the surface content, except for render targets */
2561 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
2566 /* The backbuffers have the swapchain set as well, but the primary
2567 * owns it and destroys it. */
2568 if (surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2569 wined3d_device_uninit_gdi(This->wined3d_device);
2570 surfImpl->isRenderTarget = FALSE;
2574 if (!wined3d_surface_decref(wined3d_surface))
2575 TRACE("Surface released successful, next surface\n");
2577 ERR("Something's still holding the old WineD3DSurface\n");
2580 surfImpl->ImplType = This->ImplType;
2582 return DDENUMRET_OK;
2585 /*****************************************************************************
2586 * ddraw_recreate_surfaces
2588 * A function, that converts all wineD3DSurfaces to the new implementation type
2589 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
2590 * new WineD3DSurface, copies the content and releases the old surface
2592 *****************************************************************************/
2593 static HRESULT ddraw_recreate_surfaces(IDirectDrawImpl *This)
2595 DDSURFACEDESC2 desc;
2596 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
2598 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
2600 /* Should happen almost never */
2601 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
2603 wined3d_device_uninit_3d(This->wined3d_device);
2605 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
2607 memset(&desc, 0, sizeof(desc));
2608 desc.dwSize = sizeof(desc);
2610 return IDirectDraw7_EnumSurfaces(&This->IDirectDraw7_iface, 0, &desc, This,
2611 ddraw_recreate_surfaces_cb);
2614 /*****************************************************************************
2615 * ddraw_create_surface
2617 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2618 * with the passed parameters.
2621 * DDSD: Description of the surface to create
2622 * Surf: Address to store the interface pointer at
2627 *****************************************************************************/
2628 static HRESULT ddraw_create_surface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD,
2629 IDirectDrawSurfaceImpl **ppSurf, UINT level)
2631 WINED3DSURFTYPE ImplType = This->ImplType;
2634 TRACE("ddraw %p, surface_desc %p, surface %p, level %u.\n",
2635 This, pDDSD, ppSurf, level);
2637 if (TRACE_ON(ddraw))
2639 TRACE(" (%p) Requesting surface desc :\n", This);
2640 DDRAW_dump_surface_desc(pDDSD);
2643 /* Select the surface type, if it wasn't choosen yet */
2644 if(ImplType == SURFACE_UNKNOWN)
2646 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
2647 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2649 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
2650 ImplType = SURFACE_OPENGL;
2653 /* Otherwise use GDI surfaces for now */
2656 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
2657 ImplType = SURFACE_GDI;
2660 /* Policy if all surface implementations are available:
2661 * First, check if a default type was set with winecfg. If not,
2662 * try Xrender surfaces, and use them if they work. Next, check if
2663 * accelerated OpenGL is available, and use GL surfaces in this
2664 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
2665 * was created, always use OpenGL surfaces.
2667 * (Note: Xrender surfaces are not implemented for now, the
2668 * unaccelerated implementation uses GDI to render in Software)
2671 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
2672 * be re-created. This could be done with IDirectDrawSurface7::Restore
2674 This->ImplType = ImplType;
2678 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2679 && (This->ImplType != SURFACE_OPENGL)
2680 && DefaultSurfaceType == SURFACE_UNKNOWN)
2682 /* We have to change to OpenGL,
2683 * and re-create all WineD3DSurfaces
2685 ImplType = SURFACE_OPENGL;
2686 This->ImplType = ImplType;
2687 TRACE("(%p) Re-creating all surfaces\n", This);
2688 ddraw_recreate_surfaces(This);
2689 TRACE("(%p) Done recreating all surfaces\n", This);
2691 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2693 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
2694 /* Do not fail surface creation, only fail 3D device creation */
2698 /* Create the Surface object */
2699 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2702 ERR("(%p) Error allocating memory for a surface\n", This);
2703 return DDERR_OUTOFVIDEOMEMORY;
2706 hr = ddraw_surface_init(*ppSurf, This, pDDSD, level, ImplType);
2709 WARN("Failed to initialize surface, hr %#x.\n", hr);
2710 HeapFree(GetProcessHeap(), 0, *ppSurf);
2714 /* Increase the surface counter, and attach the surface */
2715 InterlockedIncrement(&This->surfaces);
2716 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2718 TRACE("Created surface %p.\n", *ppSurf);
2722 /*****************************************************************************
2723 * CreateAdditionalSurfaces
2725 * Creates a new mipmap chain.
2728 * root: Root surface to attach the newly created chain to
2729 * count: number of surfaces to create
2730 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2731 * effects on the caller
2732 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2733 * creates an additional surface without the mipmapping flags
2735 *****************************************************************************/
2737 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2738 IDirectDrawSurfaceImpl *root,
2740 DDSURFACEDESC2 DDSD,
2743 UINT i, j, level = 0;
2745 IDirectDrawSurfaceImpl *last = root;
2747 for(i = 0; i < count; i++)
2749 IDirectDrawSurfaceImpl *object2 = NULL;
2751 /* increase the mipmap level, but only if a mipmap is created
2752 * In this case, also halve the size
2754 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2757 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2758 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2759 /* Set the mipmap sublevel flag according to msdn */
2760 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2764 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2766 CubeFaceRoot = FALSE;
2768 hr = ddraw_create_surface(This, &DDSD, &object2, level);
2774 /* Add the new surface to the complex attachment array */
2775 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2777 if(last->complex_array[j]) continue;
2778 last->complex_array[j] = object2;
2783 /* Remove the (possible) back buffer cap from the new surface description,
2784 * because only one surface in the flipping chain is a back buffer, one
2785 * is a front buffer, the others are just primary surfaces.
2787 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2792 /* Must set all attached surfaces (e.g. mipmaps) versions as well */
2793 static void ddraw_set_surface_version(IDirectDrawSurfaceImpl *surface, UINT version)
2797 TRACE("surface %p, version %u -> %u.\n", surface, surface->version, version);
2799 surface->version = version;
2800 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
2802 if (!surface->complex_array[i]) break;
2803 ddraw_set_surface_version(surface->complex_array[i], version);
2805 while ((surface = surface->next_attached))
2807 ddraw_set_surface_version(surface, version);
2811 /*****************************************************************************
2812 * ddraw_attach_d3d_device
2814 * Initializes the D3D capabilities of WineD3D
2817 * primary: The primary surface for D3D
2823 *****************************************************************************/
2824 static HRESULT ddraw_attach_d3d_device(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary)
2826 WINED3DPRESENT_PARAMETERS localParameters;
2827 HWND window = ddraw->dest_window;
2830 TRACE("ddraw %p, primary %p.\n", ddraw, primary);
2832 if (!window || window == GetDesktopWindow())
2834 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
2835 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
2836 NULL, NULL, NULL, NULL);
2839 ERR("Failed to create window, last error %#x.\n", GetLastError());
2843 ShowWindow(window, SW_HIDE); /* Just to be sure */
2844 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
2848 TRACE("Using existing window %p for Direct3D rendering.\n", window);
2850 ddraw->d3d_window = window;
2852 /* Store the future Render Target surface */
2853 ddraw->d3d_target = primary;
2855 /* Use the surface description for the device parameters, not the device
2856 * settings. The application might render to an offscreen surface. */
2857 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2858 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2859 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2860 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT)
2861 ? primary->surface_desc.dwBackBufferCount : 0;
2862 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2863 localParameters.MultiSampleQuality = 0;
2864 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2865 localParameters.hDeviceWindow = window;
2866 localParameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
2867 localParameters.EnableAutoDepthStencil = TRUE;
2868 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16_UNORM;
2869 localParameters.Flags = 0;
2870 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT;
2871 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2872 localParameters.AutoRestoreDisplayMode = FALSE;
2874 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2875 * recursive loop until ram or emulated video memory is full. */
2876 ddraw->d3d_initialized = TRUE;
2877 hr = wined3d_device_init_3d(ddraw->wined3d_device, &localParameters);
2880 ddraw->d3d_target = NULL;
2881 ddraw->d3d_initialized = FALSE;
2885 ddraw->declArraySize = 2;
2886 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
2889 ERR("Error allocating an array for the converted vertex decls.\n");
2890 ddraw->declArraySize = 0;
2891 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
2892 return E_OUTOFMEMORY;
2895 TRACE("Successfully initialized 3D.\n");
2900 static HRESULT ddraw_create_gdi_swapchain(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary)
2902 WINED3DPRESENT_PARAMETERS presentation_parameters;
2906 window = ddraw->dest_window;
2908 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2910 /* Use the surface description for the device parameters, not the device
2911 * settings. The application might render to an offscreen surface. */
2912 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
2913 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
2914 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2915 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT)
2916 ? primary->surface_desc.dwBackBufferCount : 0;
2917 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2918 presentation_parameters.MultiSampleQuality = 0;
2919 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
2920 presentation_parameters.hDeviceWindow = window;
2921 presentation_parameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
2922 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
2923 presentation_parameters.AutoDepthStencilFormat = 0;
2924 presentation_parameters.Flags = 0;
2925 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT;
2926 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2927 presentation_parameters.AutoRestoreDisplayMode = FALSE;
2929 ddraw->d3d_target = primary;
2930 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &presentation_parameters);
2931 ddraw->d3d_target = NULL;
2934 WARN("Failed to initialize GDI ddraw implementation, hr %#x.\n", hr);
2935 primary->wined3d_swapchain = NULL;
2941 /*****************************************************************************
2942 * IDirectDraw7::CreateSurface
2944 * Creates a new IDirectDrawSurface object and returns its interface.
2946 * The surface connections with wined3d are a bit tricky. Basically it works
2949 * |------------------------| |-----------------|
2950 * | DDraw surface | | WineD3DSurface |
2952 * | WineD3DSurface |-------------->| |
2953 * | Child |<------------->| Parent |
2954 * |------------------------| |-----------------|
2956 * The DDraw surface is the parent of the wined3d surface, and it releases
2957 * the WineD3DSurface when the ddraw surface is destroyed.
2959 * However, for all surfaces which can be in a container in WineD3D,
2960 * we have to do this. These surfaces are usually complex surfaces,
2961 * so this concerns primary surfaces with a front and a back buffer,
2964 * |------------------------| |-----------------|
2965 * | DDraw surface | | Container |
2967 * | Child |<------------->| Parent |
2968 * | Texture |<------------->| |
2969 * | WineD3DSurface |<----| | Levels |<--|
2970 * | Complex connection | | | | |
2971 * |------------------------| | |-----------------| |
2975 * | |------------------| | |-----------------| |
2976 * | | IParent | |-------->| WineD3DSurface | |
2978 * | | Child |<------------->| Parent | |
2979 * | | | | Container |<--|
2980 * | |------------------| |-----------------| |
2982 * | |----------------------| |
2983 * | | DDraw surface 2 | |
2985 * |<->| Complex root Child | |
2987 * | | WineD3DSurface |<----| |
2988 * | |----------------------| | |
2990 * | |---------------------| | |-----------------| |
2991 * | | IParent | |----->| WineD3DSurface | |
2993 * | | Child |<---------->| Parent | |
2994 * | |---------------------| | Container |<--|
2995 * | |-----------------| |
2997 * | ---More surfaces can follow--- |
2999 * The reason is that the IWineD3DSwapchain(render target container)
3000 * and the IWineD3DTexure(Texture container) release the parents
3001 * of their surface's children, but by releasing the complex root
3002 * the surfaces which are complexly attached to it are destroyed
3003 * too. See IDirectDrawSurface::Release for a more detailed
3007 * DDSD: Description of the surface to create
3008 * Surf: Address to store the interface pointer at
3009 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
3010 * aggregation, so it has to be NULL
3014 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3015 * DDERR_* if an error occurs
3017 *****************************************************************************/
3018 static HRESULT CreateSurface(IDirectDrawImpl *ddraw, DDSURFACEDESC2 *DDSD,
3019 IDirectDrawSurface7 **Surf, IUnknown *UnkOuter)
3021 IDirectDrawSurfaceImpl *object = NULL;
3023 LONG extra_surfaces = 0;
3024 DDSURFACEDESC2 desc2;
3025 WINED3DDISPLAYMODE Mode;
3026 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
3028 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p.\n", ddraw, DDSD, Surf, UnkOuter);
3030 /* Some checks before we start */
3031 if (TRACE_ON(ddraw))
3033 TRACE(" (%p) Requesting surface desc :\n", ddraw);
3034 DDRAW_dump_surface_desc(DDSD);
3036 EnterCriticalSection(&ddraw_cs);
3038 if (UnkOuter != NULL)
3040 FIXME("(%p) : outer != NULL?\n", ddraw);
3041 LeaveCriticalSection(&ddraw_cs);
3042 return CLASS_E_NOAGGREGATION; /* unchecked */
3047 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", ddraw);
3048 LeaveCriticalSection(&ddraw_cs);
3049 return E_POINTER; /* unchecked */
3052 if (!(DDSD->dwFlags & DDSD_CAPS))
3054 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
3055 DDSD->dwFlags |= DDSD_CAPS;
3058 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
3060 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
3061 DDSD->dwFlags &= ~DDSD_LPSURFACE;
3064 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
3066 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
3067 WARN("(%p) Null surface pointer specified, ignore it!\n", ddraw);
3068 DDSD->dwFlags &= ~DDSD_LPSURFACE;
3071 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
3072 !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
3074 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n",
3077 LeaveCriticalSection(&ddraw_cs);
3078 return DDERR_NOEXCLUSIVEMODE;
3081 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE))
3083 WARN("Application wanted to create back buffer primary surface\n");
3084 LeaveCriticalSection(&ddraw_cs);
3085 return DDERR_INVALIDCAPS;
3088 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
3090 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
3091 WARN("Application tries to put the surface in both system and video memory\n");
3092 LeaveCriticalSection(&ddraw_cs);
3094 return DDERR_INVALIDCAPS;
3097 /* Check cube maps but only if the size includes them */
3098 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
3100 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
3101 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
3103 WARN("Cube map faces requested without cube map flag\n");
3104 LeaveCriticalSection(&ddraw_cs);
3105 return DDERR_INVALIDCAPS;
3107 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
3108 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
3110 WARN("Cube map without faces requested\n");
3111 LeaveCriticalSection(&ddraw_cs);
3112 return DDERR_INVALIDPARAMS;
3115 /* Quick tests confirm those can be created, but we don't do that yet */
3116 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
3117 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
3119 FIXME("Partial cube maps not supported yet\n");
3123 /* According to the msdn this flag is ignored by CreateSurface */
3124 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
3125 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
3127 /* Modify some flags */
3128 copy_to_surfacedesc2(&desc2, DDSD);
3129 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
3131 /* Get the video mode from WineD3D - we will need it */
3132 hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &Mode);
3135 ERR("Failed to read display mode from wined3d\n");
3136 switch(ddraw->orig_bpp)
3139 Mode.Format = WINED3DFMT_P8_UINT;
3143 Mode.Format = WINED3DFMT_B5G5R5X1_UNORM;
3147 Mode.Format = WINED3DFMT_B5G6R5_UNORM;
3151 Mode.Format = WINED3DFMT_B8G8R8_UNORM;
3155 Mode.Format = WINED3DFMT_B8G8R8X8_UNORM;
3158 Mode.Width = ddraw->orig_width;
3159 Mode.Height = ddraw->orig_height;
3162 /* No pixelformat given? Use the current screen format */
3163 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
3165 desc2.dwFlags |= DDSD_PIXELFORMAT;
3166 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
3168 /* Wait: It could be a Z buffer */
3169 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
3171 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
3174 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_S1_UINT_D15_UNORM);
3177 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16_UNORM);
3180 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_X8D24_UNORM);
3183 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32_UNORM);
3186 ERR("Unknown Z buffer bit depth\n");
3191 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
3195 /* No Width or no Height? Use the original screen size
3197 if(!(desc2.dwFlags & DDSD_WIDTH) ||
3198 !(desc2.dwFlags & DDSD_HEIGHT) )
3200 /* Invalid for non-render targets */
3201 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
3203 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
3205 LeaveCriticalSection(&ddraw_cs);
3206 return DDERR_INVALIDPARAMS;
3209 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
3210 desc2.dwWidth = Mode.Width;
3211 desc2.dwHeight = Mode.Height;
3214 if (!desc2.dwWidth || !desc2.dwHeight)
3216 LeaveCriticalSection(&ddraw_cs);
3217 return DDERR_INVALIDPARAMS;
3220 /* Mipmap count fixes */
3221 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3223 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
3225 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
3227 /* Mipmap count is given, should not be 0 */
3228 if( desc2.u2.dwMipMapCount == 0 )
3230 LeaveCriticalSection(&ddraw_cs);
3231 return DDERR_INVALIDPARAMS;
3236 /* Undocumented feature: Create sublevels until
3237 * either the width or the height is 1
3239 DWORD min = desc2.dwWidth < desc2.dwHeight ?
3240 desc2.dwWidth : desc2.dwHeight;
3241 desc2.u2.dwMipMapCount = 0;
3244 desc2.u2.dwMipMapCount += 1;
3251 /* Not-complex mipmap -> Mipmapcount = 1 */
3252 desc2.u2.dwMipMapCount = 1;
3254 extra_surfaces = desc2.u2.dwMipMapCount - 1;
3256 /* There's a mipmap count in the created surface in any case */
3257 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
3259 /* If no mipmap is given, the texture has only one level */
3261 /* The first surface is a front buffer, the back buffer is created afterwards */
3262 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
3264 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
3267 /* The root surface in a cube map is positive x */
3268 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3270 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3271 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3274 /* Create the first surface */
3275 hr = ddraw_create_surface(ddraw, &desc2, &object, 0);
3278 WARN("ddraw_create_surface failed, hr %#x.\n", hr);
3279 LeaveCriticalSection(&ddraw_cs);
3282 object->is_complex_root = TRUE;
3284 *Surf = (IDirectDrawSurface7 *)object;
3286 /* Create Additional surfaces if necessary
3287 * This applies to Primary surfaces which have a back buffer count
3288 * set, but not to mipmap textures. In case of Mipmap textures,
3289 * wineD3D takes care of the creation of additional surfaces
3291 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
3293 extra_surfaces = DDSD->dwBackBufferCount;
3294 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
3295 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
3296 desc2.dwBackBufferCount = 0;
3300 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3302 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3303 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
3304 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3305 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
3306 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
3307 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3308 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
3309 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
3310 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3311 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
3312 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
3313 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3314 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
3315 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
3316 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3317 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
3318 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3321 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces, desc2, FALSE);
3324 /* This destroys and possibly created surfaces too */
3325 IDirectDrawSurface_Release((IDirectDrawSurface7 *)object);
3326 LeaveCriticalSection(&ddraw_cs);
3330 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
3331 * But attach the d3ddevice only if the currently created surface was
3332 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
3333 * The only case I can think of where this doesn't apply is when a
3334 * 2D app was configured by the user to run with OpenGL and it didn't create
3335 * the render target as first surface. In this case the render target creation
3336 * will cause the 3D init.
3338 if( (ddraw->ImplType == SURFACE_OPENGL) && !(ddraw->d3d_initialized) &&
3339 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
3341 IDirectDrawSurfaceImpl *target = object, *surface;
3344 /* Search for the primary to use as render target */
3345 LIST_FOR_EACH(entry, &ddraw->surface_list)
3347 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3348 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
3349 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
3353 TRACE("Using primary %p as render target\n", target);
3358 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", ddraw, target);
3359 hr = ddraw_attach_d3d_device(ddraw, target);
3362 IDirectDrawSurfaceImpl *release_surf;
3363 ERR("ddraw_attach_d3d_device failed, hr %#x\n", hr);
3366 /* The before created surface structures are in an incomplete state here.
3367 * WineD3D holds the reference on the IParents, and it released them on the failure
3368 * already. So the regular release method implementation would fail on the attempt
3369 * to destroy either the IParents or the swapchain. So free the surface here.
3370 * The surface structure here is a list, not a tree, because onscreen targets
3371 * cannot be cube textures
3375 release_surf = object;
3376 object = object->complex_array[0];
3377 ddraw_surface_destroy(release_surf);
3379 LeaveCriticalSection(&ddraw_cs);
3383 else if(!(ddraw->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3385 ddraw_create_gdi_swapchain(ddraw, object);
3388 /* Addref the ddraw interface to keep an reference for each surface */
3389 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
3390 object->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
3392 /* Create a WineD3DTexture if a texture was requested */
3393 if (desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
3395 ddraw->tex_root = object;
3396 ddraw_surface_create_texture(object);
3397 ddraw->tex_root = NULL;
3400 LeaveCriticalSection(&ddraw_cs);
3404 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
3405 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
3407 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3409 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3410 iface, surface_desc, surface, outer_unknown);
3412 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3414 WARN("Application supplied invalid surface descriptor\n");
3415 return DDERR_INVALIDPARAMS;
3418 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3420 if (TRACE_ON(ddraw))
3422 TRACE(" (%p) Requesting surface desc :\n", iface);
3423 DDRAW_dump_surface_desc(surface_desc);
3426 WARN("Application tried to create an explicit front or back buffer\n");
3427 return DDERR_INVALIDCAPS;
3430 return CreateSurface(This, surface_desc, surface, outer_unknown);
3433 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
3434 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
3436 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3437 IDirectDrawSurfaceImpl *impl;
3440 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3441 iface, surface_desc, surface, outer_unknown);
3443 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3445 WARN("Application supplied invalid surface descriptor\n");
3446 return DDERR_INVALIDPARAMS;
3449 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3451 if (TRACE_ON(ddraw))
3453 TRACE(" (%p) Requesting surface desc :\n", iface);
3454 DDRAW_dump_surface_desc(surface_desc);
3457 WARN("Application tried to create an explicit front or back buffer\n");
3458 return DDERR_INVALIDCAPS;
3461 hr = CreateSurface(This, surface_desc, (IDirectDrawSurface7 **)surface, outer_unknown);
3462 impl = (IDirectDrawSurfaceImpl *)*surface;
3463 if (SUCCEEDED(hr) && impl)
3465 ddraw_set_surface_version(impl, 4);
3466 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3467 IDirectDraw4_AddRef(iface);
3468 impl->ifaceToRelease = (IUnknown *)iface;
3474 static HRESULT WINAPI ddraw3_CreateSurface(IDirectDraw3 *iface, DDSURFACEDESC *surface_desc,
3475 IDirectDrawSurface **surface, IUnknown *outer_unknown)
3477 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
3478 IDirectDrawSurface7 *surface7;
3479 IDirectDrawSurfaceImpl *impl;
3482 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3483 iface, surface_desc, surface, outer_unknown);
3485 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3487 WARN("Application supplied invalid surface descriptor\n");
3488 return DDERR_INVALIDPARAMS;
3491 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3493 if (TRACE_ON(ddraw))
3495 TRACE(" (%p) Requesting surface desc :\n", iface);
3496 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3499 WARN("Application tried to create an explicit front or back buffer\n");
3500 return DDERR_INVALIDCAPS;
3503 hr = CreateSurface(This, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3510 impl = (IDirectDrawSurfaceImpl *)surface7;
3511 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_vtbl;
3512 ddraw_set_surface_version(impl, 3);
3513 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3514 IDirectDraw3_AddRef(iface);
3515 impl->ifaceToRelease = (IUnknown *)iface;
3520 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
3521 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3523 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3524 IDirectDrawSurface7 *surface7;
3525 IDirectDrawSurfaceImpl *impl;
3528 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3529 iface, surface_desc, surface, outer_unknown);
3531 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3533 WARN("Application supplied invalid surface descriptor\n");
3534 return DDERR_INVALIDPARAMS;
3537 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3539 if (TRACE_ON(ddraw))
3541 TRACE(" (%p) Requesting surface desc :\n", iface);
3542 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3545 WARN("Application tried to create an explicit front or back buffer\n");
3546 return DDERR_INVALIDCAPS;
3549 hr = CreateSurface(This, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3556 impl = (IDirectDrawSurfaceImpl *)surface7;
3557 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_vtbl;
3558 ddraw_set_surface_version(impl, 2);
3559 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3560 impl->ifaceToRelease = NULL;
3565 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3566 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3568 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3569 IDirectDrawSurface7 *surface7;
3570 IDirectDrawSurfaceImpl *impl;
3573 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3574 iface, surface_desc, surface, outer_unknown);
3576 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3578 WARN("Application supplied invalid surface descriptor\n");
3579 return DDERR_INVALIDPARAMS;
3582 /* Remove front buffer flag, this causes failure in v7, and its added to normal
3583 * primaries anyway. */
3584 surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3585 hr = CreateSurface(This, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3592 impl = (IDirectDrawSurfaceImpl *)surface7;
3593 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_vtbl;
3594 ddraw_set_surface_version(impl, 1);
3595 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3596 impl->ifaceToRelease = NULL;
3601 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3602 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3605 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3606 const DDPIXELFORMAT *provided)
3608 /* Some flags must be present in both or neither for a match. */
3609 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3610 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3611 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3613 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3616 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3619 if (requested->dwFlags & DDPF_FOURCC)
3620 if (requested->dwFourCC != provided->dwFourCC)
3623 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3624 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3625 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3628 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3629 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3630 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3633 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3634 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3637 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3638 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3640 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3643 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3644 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3650 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3659 #define CMP(FLAG, FIELD) \
3660 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3661 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3663 static const struct compare_info compare[] =
3665 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3666 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3668 CMP(CKDESTBLT, ddckCKDestBlt),
3669 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3670 CMP(CKSRCBLT, ddckCKSrcBlt),
3671 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3672 CMP(HEIGHT, dwHeight),
3673 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3674 CMP(LPSURFACE, lpSurface),
3675 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3676 CMP(PITCH, u1 /* lPitch */),
3677 /* PIXELFORMAT: manual */
3678 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3679 CMP(TEXTURESTAGE, dwTextureStage),
3680 CMP(WIDTH, dwWidth),
3681 /* ZBUFFERBITDEPTH: "obsolete" */
3688 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3691 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3693 if (requested->dwFlags & compare[i].flag
3694 && memcmp((const char *)provided + compare[i].offset,
3695 (const char *)requested + compare[i].offset,
3696 compare[i].size) != 0)
3700 if (requested->dwFlags & DDSD_PIXELFORMAT)
3702 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3703 &provided->u4.ddpfPixelFormat))
3710 #undef DDENUMSURFACES_SEARCHTYPE
3711 #undef DDENUMSURFACES_MATCHTYPE
3713 struct surfacescallback_context
3715 LPDDENUMSURFACESCALLBACK func;
3719 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3720 DDSURFACEDESC2 *surface_desc, void *context)
3722 struct surfacescallback_context *cbcontext = context;
3724 return cbcontext->func((IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface)->IDirectDrawSurface3_vtbl,
3725 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3728 /*****************************************************************************
3729 * IDirectDraw7::EnumSurfaces
3731 * Loops through all surfaces attached to this device and calls the
3732 * application callback. This can't be relayed to WineD3DDevice,
3733 * because some WineD3DSurfaces' parents are IParent objects
3736 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3737 * DDSD: Description to filter for
3738 * Context: Application-provided pointer, it's passed unmodified to the
3740 * Callback: Address to call for each surface
3743 * DDERR_INVALIDPARAMS if the callback is NULL
3746 *****************************************************************************/
3747 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3748 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3750 /* The surface enumeration is handled by WineDDraw,
3751 * because it keeps track of all surfaces attached to
3752 * it. The filtering is done by our callback function,
3753 * because WineDDraw doesn't handle ddraw-like surface
3756 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3757 IDirectDrawSurfaceImpl *surf;
3759 DDSURFACEDESC2 desc;
3760 struct list *entry, *entry2;
3762 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3763 iface, Flags, DDSD, Context, Callback);
3765 all = Flags & DDENUMSURFACES_ALL;
3766 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3768 EnterCriticalSection(&ddraw_cs);
3772 LeaveCriticalSection(&ddraw_cs);
3773 return DDERR_INVALIDPARAMS;
3776 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3777 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3779 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3780 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3782 desc = surf->surface_desc;
3783 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
3784 if (Callback((IDirectDrawSurface7 *)surf, &desc, Context) != DDENUMRET_OK)
3786 LeaveCriticalSection(&ddraw_cs);
3791 LeaveCriticalSection(&ddraw_cs);
3795 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3796 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3798 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3800 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3801 iface, flags, surface_desc, context, callback);
3803 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, surface_desc, context,
3804 (LPDDENUMSURFACESCALLBACK7)callback);
3807 static HRESULT WINAPI ddraw3_EnumSurfaces(IDirectDraw3 *iface, DWORD flags,
3808 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3810 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
3811 struct surfacescallback_context cbcontext;
3813 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3814 iface, flags, surface_desc, context, callback);
3816 cbcontext.func = callback;
3817 cbcontext.context = context;
3819 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
3820 &cbcontext, EnumSurfacesCallbackThunk);
3823 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3824 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3826 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3827 struct surfacescallback_context cbcontext;
3829 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3830 iface, flags, surface_desc, context, callback);
3832 cbcontext.func = callback;
3833 cbcontext.context = context;
3835 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
3836 &cbcontext, EnumSurfacesCallbackThunk);
3839 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3840 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3842 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3843 struct surfacescallback_context cbcontext;
3845 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3846 iface, flags, surface_desc, context, callback);
3848 cbcontext.func = callback;
3849 cbcontext.context = context;
3851 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
3852 &cbcontext, EnumSurfacesCallbackThunk);
3855 /*****************************************************************************
3856 * DirectDrawCreateClipper (DDRAW.@)
3858 * Creates a new IDirectDrawClipper object.
3861 * Clipper: Address to write the interface pointer to
3862 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3866 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3867 * E_OUTOFMEMORY if allocating the object failed
3869 *****************************************************************************/
3871 DirectDrawCreateClipper(DWORD Flags,
3872 LPDIRECTDRAWCLIPPER *Clipper,
3875 IDirectDrawClipperImpl* object;
3878 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3879 Flags, Clipper, UnkOuter);
3881 EnterCriticalSection(&ddraw_cs);
3882 if (UnkOuter != NULL)
3884 LeaveCriticalSection(&ddraw_cs);
3885 return CLASS_E_NOAGGREGATION;
3888 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3889 sizeof(IDirectDrawClipperImpl));
3892 LeaveCriticalSection(&ddraw_cs);
3893 return E_OUTOFMEMORY;
3896 hr = ddraw_clipper_init(object);
3899 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3900 HeapFree(GetProcessHeap(), 0, object);
3901 LeaveCriticalSection(&ddraw_cs);
3905 TRACE("Created clipper %p.\n", object);
3906 *Clipper = (IDirectDrawClipper *) object;
3907 LeaveCriticalSection(&ddraw_cs);
3911 /*****************************************************************************
3912 * IDirectDraw7::CreateClipper
3914 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3916 *****************************************************************************/
3917 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3918 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3920 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3921 iface, Flags, Clipper, UnkOuter);
3923 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3926 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3927 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3929 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3931 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3932 iface, flags, clipper, outer_unknown);
3934 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3937 static HRESULT WINAPI ddraw3_CreateClipper(IDirectDraw3 *iface, DWORD flags,
3938 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3940 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
3942 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3943 iface, flags, clipper, outer_unknown);
3945 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3948 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3949 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3951 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3953 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3954 iface, flags, clipper, outer_unknown);
3956 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3959 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3960 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3962 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3964 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3965 iface, flags, clipper, outer_unknown);
3967 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3970 /*****************************************************************************
3971 * IDirectDraw7::CreatePalette
3973 * Creates a new IDirectDrawPalette object
3976 * Flags: The flags for the new clipper
3977 * ColorTable: Color table to assign to the new clipper
3978 * Palette: Address to write the interface pointer to
3979 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3983 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3984 * E_OUTOFMEMORY if allocating the object failed
3986 *****************************************************************************/
3987 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3988 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3990 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3991 IDirectDrawPaletteImpl *object;
3994 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3995 iface, Flags, ColorTable, Palette, pUnkOuter);
3997 EnterCriticalSection(&ddraw_cs);
3998 if(pUnkOuter != NULL)
4000 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
4001 LeaveCriticalSection(&ddraw_cs);
4002 return CLASS_E_NOAGGREGATION;
4005 /* The refcount test shows that a cooplevel is required for this */
4006 if(!This->cooperative_level)
4008 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
4009 LeaveCriticalSection(&ddraw_cs);
4010 return DDERR_NOCOOPERATIVELEVELSET;
4013 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
4016 ERR("Out of memory when allocating memory for a palette implementation\n");
4017 LeaveCriticalSection(&ddraw_cs);
4018 return E_OUTOFMEMORY;
4021 hr = ddraw_palette_init(object, This, Flags, ColorTable);
4024 WARN("Failed to initialize palette, hr %#x.\n", hr);
4025 HeapFree(GetProcessHeap(), 0, object);
4026 LeaveCriticalSection(&ddraw_cs);
4030 TRACE("Created palette %p.\n", object);
4031 *Palette = (IDirectDrawPalette *)object;
4032 LeaveCriticalSection(&ddraw_cs);
4036 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
4037 IDirectDrawPalette **palette, IUnknown *outer_unknown)
4039 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
4042 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4043 iface, flags, entries, palette, outer_unknown);
4045 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4046 if (SUCCEEDED(hr) && *palette)
4048 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4049 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4050 IDirectDraw4_AddRef(iface);
4051 impl->ifaceToRelease = (IUnknown *)iface;
4056 static HRESULT WINAPI ddraw3_CreatePalette(IDirectDraw3 *iface, DWORD flags,
4057 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
4059 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
4062 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4063 iface, flags, entries, palette, outer_unknown);
4065 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4066 if (SUCCEEDED(hr) && *palette)
4068 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4069 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4070 IDirectDraw4_AddRef(iface);
4071 impl->ifaceToRelease = (IUnknown *)iface;
4077 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
4078 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
4080 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
4083 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4084 iface, flags, entries, palette, outer_unknown);
4086 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4087 if (SUCCEEDED(hr) && *palette)
4089 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4090 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4091 impl->ifaceToRelease = NULL;
4097 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
4098 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
4100 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
4103 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4104 iface, flags, entries, palette, outer_unknown);
4106 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4107 if (SUCCEEDED(hr) && *palette)
4109 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4110 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4111 impl->ifaceToRelease = NULL;
4117 /*****************************************************************************
4118 * IDirectDraw7::DuplicateSurface
4120 * Duplicates a surface. The surface memory points to the same memory as
4121 * the original surface, and it's released when the last surface referencing
4122 * it is released. I guess that's beyond Wine's surface management right now
4123 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
4124 * test application to implement this)
4127 * Src: Address of the source surface
4128 * Dest: Address to write the new surface pointer to
4131 * See IDirectDraw7::CreateSurface
4133 *****************************************************************************/
4134 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
4135 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
4137 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Src;
4139 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
4141 /* For now, simply create a new, independent surface */
4142 return IDirectDraw7_CreateSurface(iface,
4143 &Surf->surface_desc,
4148 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
4149 IDirectDrawSurface4 **dst)
4151 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
4153 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4155 return ddraw7_DuplicateSurface(&This->IDirectDraw7_iface, (IDirectDrawSurface7 *)src,
4156 (IDirectDrawSurface7 **)dst);
4159 static HRESULT WINAPI ddraw3_DuplicateSurface(IDirectDraw3 *iface, IDirectDrawSurface *src,
4160 IDirectDrawSurface **dst)
4162 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
4163 IDirectDrawSurface7 *src7, *dst7;
4166 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4167 src7 = (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)src);
4168 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface, src7, &dst7);
4171 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_vtbl;
4175 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
4176 IDirectDrawSurface *src, IDirectDrawSurface **dst)
4178 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
4179 IDirectDrawSurface7 *src7, *dst7;
4182 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4183 src7 = (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)src);
4184 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface, src7, &dst7);
4187 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_vtbl;
4191 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
4192 IDirectDrawSurface **dst)
4194 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
4195 IDirectDrawSurface7 *src7, *dst7;
4198 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4199 src7 = (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)src);
4200 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface, src7, &dst7);
4203 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_vtbl;
4207 /*****************************************************************************
4208 * IDirect3D7::EnumDevices
4210 * The EnumDevices method for IDirect3D7. It enumerates all supported
4211 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
4214 * callback: Function to call for each enumerated device
4215 * context: Pointer to pass back to the app
4218 * D3D_OK, or the return value of the GetCaps call
4220 *****************************************************************************/
4221 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
4223 char interface_name_tnl[] = "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D";
4224 char device_name_tnl[] = "Wine D3D7 T&L HAL";
4225 char interface_name_hal[] = "WINE Direct3D7 Hardware acceleration using WineD3D";
4226 char device_name_hal[] = "Wine D3D7 HAL";
4227 char interface_name_rgb[] = "WINE Direct3D7 RGB Software Emulation using WineD3D";
4228 char device_name_rgb[] = "Wine D3D7 RGB";
4230 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4231 D3DDEVICEDESC7 device_desc7;
4232 D3DDEVICEDESC device_desc1;
4235 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4237 EnterCriticalSection(&ddraw_cs);
4239 hr = IDirect3DImpl_GetCaps(This->wineD3D, &device_desc1, &device_desc7);
4242 LeaveCriticalSection(&ddraw_cs);
4245 callback(interface_name_tnl, device_name_tnl, &device_desc7, context);
4247 device_desc7.deviceGUID = IID_IDirect3DHALDevice;
4248 callback(interface_name_hal, device_name_hal, &device_desc7, context);
4250 device_desc7.deviceGUID = IID_IDirect3DRGBDevice;
4251 callback(interface_name_rgb, device_name_rgb, &device_desc7, context);
4253 TRACE("End of enumeration.\n");
4255 LeaveCriticalSection(&ddraw_cs);
4260 /*****************************************************************************
4261 * IDirect3D3::EnumDevices
4263 * Enumerates all supported Direct3DDevice interfaces. This is the
4264 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
4266 * Version 1, 2 and 3
4269 * callback: Application-provided routine to call for each enumerated device
4270 * Context: Pointer to pass to the callback
4273 * D3D_OK on success,
4274 * The result of IDirect3DImpl_GetCaps if it failed
4276 *****************************************************************************/
4277 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4279 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
4281 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4282 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
4283 D3DDEVICEDESC7 device_desc7;
4286 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
4287 * name string. Let's put the string in a sufficiently sized array in
4288 * writable memory. */
4289 char device_name[50];
4290 strcpy(device_name,"Direct3D HEL");
4292 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4294 EnterCriticalSection(&ddraw_cs);
4296 hr = IDirect3DImpl_GetCaps(This->wineD3D, &device_desc1, &device_desc7);
4299 LeaveCriticalSection(&ddraw_cs);
4303 /* Do I have to enumerate the reference id? Note from old d3d7:
4304 * "It seems that enumerating the reference IID on Direct3D 1 games
4305 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
4307 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
4308 * EnumReference which enables / disables enumerating the reference
4309 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
4310 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
4311 * demo directory suggest this.
4313 * Some games(GTA 2) seem to use the second enumerated device, so I have
4314 * to enumerate at least 2 devices. So enumerate the reference device to
4317 * Other games (Rollcage) tell emulation and hal device apart by certain
4318 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
4319 * limitation flag), and it refuses all devices that have the perspective
4320 * flag set. This way it refuses the emulation device, and HAL devices
4321 * never have POW2 unset in d3d7 on windows. */
4322 if (This->d3dversion != 1)
4324 static CHAR reference_description[] = "RGB Direct3D emulation";
4326 TRACE("Enumerating WineD3D D3DDevice interface.\n");
4327 hal_desc = device_desc1;
4328 hel_desc = device_desc1;
4329 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
4330 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4331 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4332 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4333 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4334 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
4335 device_name, &hal_desc, &hel_desc, context);
4336 if (hr != D3DENUMRET_OK)
4338 TRACE("Application cancelled the enumeration.\n");
4339 LeaveCriticalSection(&ddraw_cs);
4344 strcpy(device_name,"Direct3D HAL");
4346 TRACE("Enumerating HAL Direct3D device.\n");
4347 hal_desc = device_desc1;
4348 hel_desc = device_desc1;
4349 /* The hal device does not have the pow2 flag set in hel, but in hal. */
4350 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4351 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4352 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4353 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4354 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
4355 device_name, &hal_desc, &hel_desc, context);
4356 if (hr != D3DENUMRET_OK)
4358 TRACE("Application cancelled the enumeration.\n");
4359 LeaveCriticalSection(&ddraw_cs);
4363 TRACE("End of enumeration.\n");
4365 LeaveCriticalSection(&ddraw_cs);
4369 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4371 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4373 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4375 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
4378 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4380 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4382 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4384 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
4387 /*****************************************************************************
4388 * IDirect3D3::CreateLight
4390 * Creates an IDirect3DLight interface. This interface is used in
4391 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
4392 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
4393 * uses the IDirect3DDevice7 interface with D3D7 lights.
4395 * Version 1, 2 and 3
4398 * light: Address to store the new interface pointer
4399 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4404 * DDERR_OUTOFMEMORY if memory allocation failed
4405 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4407 *****************************************************************************/
4408 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
4409 IUnknown *outer_unknown)
4411 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4412 IDirect3DLightImpl *object;
4414 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4416 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4418 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4421 ERR("Failed to allocate light memory.\n");
4422 return DDERR_OUTOFMEMORY;
4425 d3d_light_init(object, This);
4427 TRACE("Created light %p.\n", object);
4428 *light = (IDirect3DLight *)object;
4433 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4435 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4437 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4439 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4442 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4444 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4446 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4448 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4451 /*****************************************************************************
4452 * IDirect3D3::CreateMaterial
4454 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
4455 * and older versions. The IDirect3DMaterial implementation wraps its
4456 * functionality to IDirect3DDevice7::SetMaterial and friends.
4458 * Version 1, 2 and 3
4461 * material: Address to store the new interface's pointer to
4462 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4467 * DDERR_OUTOFMEMORY if memory allocation failed
4468 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4470 *****************************************************************************/
4471 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
4472 IUnknown *outer_unknown)
4474 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4475 IDirect3DMaterialImpl *object;
4477 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4479 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4481 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4484 ERR("Failed to allocate material memory.\n");
4485 return DDERR_OUTOFMEMORY;
4488 d3d_material_init(object, This);
4490 TRACE("Created material %p.\n", object);
4491 *material = (IDirect3DMaterial3 *)object;
4496 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material, IUnknown *outer_unknown)
4498 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4499 IDirect3DMaterial3 *material3;
4502 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4504 hr = d3d3_CreateMaterial(&This->IDirect3D3_iface, &material3, outer_unknown);
4505 *material = material3 ? (IDirect3DMaterial2 *)&((IDirect3DMaterialImpl *)material3)->IDirect3DMaterial2_vtbl : NULL;
4507 TRACE("Returning material %p.\n", *material);
4512 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material, IUnknown *outer_unknown)
4514 IDirect3DMaterial3 *material3;
4517 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4519 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4521 hr = d3d3_CreateMaterial(&This->IDirect3D3_iface, &material3, outer_unknown);
4522 *material = material3 ? (IDirect3DMaterial *)&((IDirect3DMaterialImpl *)material3)->IDirect3DMaterial_vtbl : NULL;
4524 TRACE("Returning material %p.\n", *material);
4529 /*****************************************************************************
4530 * IDirect3D3::CreateViewport
4532 * Creates an IDirect3DViewport interface. This interface is used
4533 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4534 * it has been replaced by a viewport structure and
4535 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4536 * uses the IDirect3DDevice7 methods for its functionality
4539 * Viewport: Address to store the new interface pointer
4540 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4545 * DDERR_OUTOFMEMORY if memory allocation failed
4546 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4548 *****************************************************************************/
4549 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4550 IUnknown *outer_unknown)
4552 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4553 IDirect3DViewportImpl *object;
4555 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4557 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4559 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4562 ERR("Failed to allocate viewport memory.\n");
4563 return DDERR_OUTOFMEMORY;
4566 d3d_viewport_init(object, This);
4568 TRACE("Created viewport %p.\n", object);
4569 *viewport = (IDirect3DViewport3 *)object;
4574 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4576 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4578 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4580 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4584 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4586 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4588 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4590 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4594 /*****************************************************************************
4595 * IDirect3D3::FindDevice
4597 * This method finds a device with the requested properties and returns a
4598 * device description
4602 * fds: Describes the requested device characteristics
4603 * fdr: Returns the device description
4607 * DDERR_INVALIDPARAMS if no device was found
4609 *****************************************************************************/
4610 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4612 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4613 D3DDEVICEDESC7 desc7;
4614 D3DDEVICEDESC desc1;
4617 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4619 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4621 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4622 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4623 return DDERR_INVALIDPARAMS;
4625 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4626 && fds->dcmColorModel != D3DCOLOR_RGB)
4628 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4629 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4632 if (fds->dwFlags & D3DFDS_GUID)
4634 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4635 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4636 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4637 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4639 WARN("No match for this GUID.\n");
4640 return DDERR_NOTFOUND;
4645 hr = IDirect3DImpl_GetCaps(This->wineD3D, &desc1, &desc7);
4646 if (hr != D3D_OK) return hr;
4648 /* Now return our own GUID */
4649 fdr->guid = IID_D3DDEVICE_WineD3D;
4650 fdr->ddHwDesc = desc1;
4651 fdr->ddSwDesc = desc1;
4653 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4658 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4660 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4662 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4664 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4667 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4669 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4671 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4673 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4676 /*****************************************************************************
4677 * IDirect3D7::CreateDevice
4679 * Creates an IDirect3DDevice7 interface.
4681 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4682 * DirectDraw surfaces and are created with
4683 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4684 * create the device object and QueryInterfaces for IDirect3DDevice
4687 * refiid: IID of the device to create
4688 * Surface: Initial rendertarget
4689 * Device: Address to return the interface pointer
4693 * DDERR_OUTOFMEMORY if memory allocation failed
4694 * DDERR_INVALIDPARAMS if a device exists already
4696 *****************************************************************************/
4697 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4698 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4700 IDirectDrawSurfaceImpl *target = (IDirectDrawSurfaceImpl *)surface;
4701 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4702 IDirect3DDeviceImpl *object;
4705 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4707 EnterCriticalSection(&ddraw_cs);
4710 /* Fail device creation if non-opengl surfaces are used. */
4711 if (This->ImplType != SURFACE_OPENGL)
4713 ERR("The application wants to create a Direct3D device, but non-opengl surfaces are set in the registry.\n");
4714 ERR("Please set the surface implementation to opengl or autodetection to allow 3D rendering.\n");
4716 /* We only hit this path if a default surface is set in the registry. Incorrect autodetection
4717 * is caught in CreateSurface or QueryInterface. */
4718 LeaveCriticalSection(&ddraw_cs);
4722 if (This->d3ddevice)
4724 FIXME("Only one Direct3D device per DirectDraw object supported.\n");
4725 LeaveCriticalSection(&ddraw_cs);
4726 return DDERR_INVALIDPARAMS;
4729 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4732 ERR("Failed to allocate device memory.\n");
4733 LeaveCriticalSection(&ddraw_cs);
4734 return DDERR_OUTOFMEMORY;
4737 hr = d3d_device_init(object, This, target);
4740 WARN("Failed to initialize device, hr %#x.\n", hr);
4741 HeapFree(GetProcessHeap(), 0, object);
4742 LeaveCriticalSection(&ddraw_cs);
4746 TRACE("Created device %p.\n", object);
4747 *device = (IDirect3DDevice7 *)object;
4749 LeaveCriticalSection(&ddraw_cs);
4753 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4754 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4756 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4759 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4760 iface, debugstr_guid(riid), surface, device, outer_unknown);
4762 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4764 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid, (IDirectDrawSurface7 *)surface,
4765 (IDirect3DDevice7 **)device);
4766 if (*device) *device = (IDirect3DDevice3 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice3_vtbl;
4771 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4772 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4774 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4777 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4778 iface, debugstr_guid(riid), surface, device);
4780 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid,
4781 surface ? (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)surface) : NULL,
4782 (IDirect3DDevice7 **)device);
4783 if (*device) *device = (IDirect3DDevice2 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice2_vtbl;
4788 /*****************************************************************************
4789 * IDirect3D7::CreateVertexBuffer
4791 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4797 * desc: Requested Vertex buffer properties
4798 * vertex_buffer: Address to return the interface pointer at
4799 * flags: Some flags, should be 0
4803 * DDERR_OUTOFMEMORY if memory allocation failed
4804 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4805 * DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4807 *****************************************************************************/
4808 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4809 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4811 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4812 IDirect3DVertexBufferImpl *object;
4815 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4816 iface, desc, vertex_buffer, flags);
4818 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4820 TRACE("Vertex buffer description:\n");
4821 TRACE(" dwSize %u\n", desc->dwSize);
4822 TRACE(" dwCaps %#x\n", desc->dwCaps);
4823 TRACE(" FVF %#x\n", desc->dwFVF);
4824 TRACE(" dwNumVertices %u\n", desc->dwNumVertices);
4826 /* Now create the vertex buffer */
4827 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4830 ERR("Failed to allocate vertex buffer memory.\n");
4831 return DDERR_OUTOFMEMORY;
4834 hr = d3d_vertex_buffer_init(object, This, desc);
4837 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
4838 HeapFree(GetProcessHeap(), 0, object);
4842 TRACE("Created vertex buffer %p.\n", object);
4843 *vertex_buffer = (IDirect3DVertexBuffer7 *)object;
4848 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4849 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4851 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4854 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4855 iface, desc, vertex_buffer, flags, outer_unknown);
4857 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4859 hr = d3d7_CreateVertexBuffer(&This->IDirect3D7_iface, desc,
4860 (IDirect3DVertexBuffer7 **)vertex_buffer, flags);
4862 *vertex_buffer = (IDirect3DVertexBuffer *)&((IDirect3DVertexBufferImpl *)*vertex_buffer)->IDirect3DVertexBuffer_vtbl;
4867 /*****************************************************************************
4868 * IDirect3D7::EnumZBufferFormats
4870 * Enumerates all supported Z buffer pixel formats
4876 * callback: callback to call for each pixel format
4877 * context: Pointer to pass back to the callback
4881 * DDERR_INVALIDPARAMS if callback is NULL
4882 * For details, see IWineD3DDevice::EnumZBufferFormats
4884 *****************************************************************************/
4885 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4886 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4888 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4889 WINED3DDISPLAYMODE d3ddm;
4890 WINED3DDEVTYPE type;
4894 /* Order matters. Specifically, BattleZone II (full version) expects the
4895 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4896 static const enum wined3d_format_id formats[] =
4898 WINED3DFMT_S1_UINT_D15_UNORM,
4899 WINED3DFMT_D16_UNORM,
4900 WINED3DFMT_X8D24_UNORM,
4901 WINED3DFMT_S4X4_UINT_D24_UNORM,
4902 WINED3DFMT_D24_UNORM_S8_UINT,
4903 WINED3DFMT_D32_UNORM,
4906 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4907 iface, debugstr_guid(device_iid), callback, context);
4909 if (!callback) return DDERR_INVALIDPARAMS;
4911 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4912 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4913 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4915 TRACE("Asked for HAL device.\n");
4916 type = WINED3DDEVTYPE_HAL;
4918 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4919 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4921 TRACE("Asked for SW device.\n");
4922 type = WINED3DDEVTYPE_SW;
4924 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4926 TRACE("Asked for REF device.\n");
4927 type = WINED3DDEVTYPE_REF;
4929 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4931 TRACE("Asked for NULLREF device.\n");
4932 type = WINED3DDEVTYPE_NULLREF;
4936 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4937 type = WINED3DDEVTYPE_HAL;
4940 EnterCriticalSection(&ddraw_cs);
4941 /* We need an adapter format from somewhere to please wined3d and WGL.
4942 * Use the current display mode. So far all cards offer the same depth
4943 * stencil format for all modes, but if some do not and applications do
4944 * not like that we'll have to find some workaround, like iterating over
4945 * all imaginable formats and collecting all the depth stencil formats we
4947 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &d3ddm);
4949 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4951 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, type, d3ddm.Format,
4952 WINED3DUSAGE_DEPTHSTENCIL, WINED3DRTYPE_SURFACE, formats[i], SURFACE_OPENGL);
4955 DDPIXELFORMAT pformat;
4957 memset(&pformat, 0, sizeof(pformat));
4958 pformat.dwSize = sizeof(pformat);
4959 PixelFormat_WineD3DtoDD(&pformat, formats[i]);
4961 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4962 hr = callback(&pformat, context);
4963 if (hr != DDENUMRET_OK)
4965 TRACE("Format enumeration cancelled by application.\n");
4966 LeaveCriticalSection(&ddraw_cs);
4971 TRACE("End of enumeration.\n");
4973 LeaveCriticalSection(&ddraw_cs);
4977 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4978 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4980 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4982 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4983 iface, debugstr_guid(device_iid), callback, context);
4985 return d3d7_EnumZBufferFormats(&This->IDirect3D7_iface, device_iid, callback, context);
4988 /*****************************************************************************
4989 * IDirect3D7::EvictManagedTextures
4991 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4992 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4997 * D3D_OK, because it's a stub
4999 *****************************************************************************/
5000 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
5002 FIXME("iface %p stub!\n", iface);
5004 /* TODO: Just enumerate resources using IWineD3DDevice_EnumResources(),
5005 * then unload surfaces / textures. */
5010 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
5012 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
5014 TRACE("iface %p.\n", iface);
5016 return d3d7_EvictManagedTextures(&This->IDirect3D7_iface);
5019 /*****************************************************************************
5020 * IDirect3DImpl_GetCaps
5022 * This function retrieves the device caps from wined3d
5023 * and converts it into a D3D7 and D3D - D3D3 structure
5024 * This is a helper function called from various places in ddraw
5027 * wined3d: The interface to get the caps from
5028 * desc1: Old D3D <3 structure to fill (needed)
5029 * desc7: D3D7 device desc structure to fill (needed)
5032 * D3D_OK on success, or the return value of IWineD3D::GetCaps
5034 *****************************************************************************/
5035 HRESULT IDirect3DImpl_GetCaps(const struct wined3d *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
5037 WINED3DCAPS wined3d_caps;
5040 TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
5042 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
5044 EnterCriticalSection(&ddraw_cs);
5045 hr = wined3d_get_device_caps(wined3d, 0, WINED3DDEVTYPE_HAL, &wined3d_caps);
5046 LeaveCriticalSection(&ddraw_cs);
5049 WARN("Failed to get device caps, hr %#x.\n", hr);
5053 /* Copy the results into the d3d7 and d3d3 structures */
5054 desc7->dwDevCaps = wined3d_caps.DevCaps;
5055 desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
5056 desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
5057 desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
5058 desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
5059 desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
5060 desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
5061 desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
5062 desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
5063 desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
5064 desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
5066 desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
5067 desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
5069 desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
5070 desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
5071 desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
5072 desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
5074 desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
5075 desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
5076 desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
5077 desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
5079 desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
5080 desc7->dwStencilCaps = wined3d_caps.StencilCaps;
5082 desc7->dwFVFCaps = wined3d_caps.FVFCaps;
5083 desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
5085 desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
5086 desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
5088 /* Remove all non-d3d7 caps */
5089 desc7->dwDevCaps &= (
5090 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
5091 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
5092 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
5093 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
5094 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
5095 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
5096 D3DDEVCAPS_HWRASTERIZATION);
5098 desc7->dwStencilCaps &= (
5099 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
5100 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
5101 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
5105 desc7->dwTextureOpCaps &= (
5106 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
5107 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
5108 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
5109 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
5110 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
5111 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
5112 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
5113 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
5115 desc7->dwVertexProcessingCaps &= (
5116 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
5117 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
5119 desc7->dpcLineCaps.dwMiscCaps &= (
5120 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
5121 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
5122 D3DPMISCCAPS_CULLCCW);
5124 desc7->dpcLineCaps.dwRasterCaps &= (
5125 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
5126 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
5127 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
5128 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
5129 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
5130 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
5131 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
5132 D3DPRASTERCAPS_ZFOG);
5134 desc7->dpcLineCaps.dwZCmpCaps &= (
5135 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
5136 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
5137 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
5139 desc7->dpcLineCaps.dwSrcBlendCaps &= (
5140 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
5141 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
5142 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
5143 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
5144 D3DPBLENDCAPS_BOTHINVSRCALPHA);
5146 desc7->dpcLineCaps.dwDestBlendCaps &= (
5147 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
5148 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
5149 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
5150 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
5151 D3DPBLENDCAPS_BOTHINVSRCALPHA);
5153 desc7->dpcLineCaps.dwAlphaCmpCaps &= (
5154 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
5155 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
5156 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
5158 desc7->dpcLineCaps.dwShadeCaps &= (
5159 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
5160 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
5161 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
5162 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
5163 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
5164 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
5165 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
5167 desc7->dpcLineCaps.dwTextureCaps &= (
5168 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
5169 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
5170 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
5171 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
5173 desc7->dpcLineCaps.dwTextureFilterCaps &= (
5174 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
5175 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
5176 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
5177 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
5178 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
5179 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
5181 desc7->dpcLineCaps.dwTextureBlendCaps &= (
5182 D3DPTBLENDCAPS_DECAL | D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_DECALALPHA |
5183 D3DPTBLENDCAPS_MODULATEALPHA | D3DPTBLENDCAPS_DECALMASK | D3DPTBLENDCAPS_MODULATEMASK |
5184 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_ADD);
5186 desc7->dpcLineCaps.dwTextureAddressCaps &= (
5187 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
5188 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
5190 if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
5192 /* DirectX7 always has the np2 flag set, no matter what the card
5193 * supports. Some old games (Rollcage) check the caps incorrectly.
5194 * If wined3d supports nonpow2 textures it also has np2 conditional
5196 desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
5199 /* Fill the missing members, and do some fixup */
5200 desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
5201 desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
5202 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
5203 D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
5204 D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
5205 desc7->dpcLineCaps.dwStippleWidth = 32;
5206 desc7->dpcLineCaps.dwStippleHeight = 32;
5207 /* Use the same for the TriCaps */
5208 desc7->dpcTriCaps = desc7->dpcLineCaps;
5210 desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
5211 desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
5212 desc7->dwMinTextureWidth = 1;
5213 desc7->dwMinTextureHeight = 1;
5215 /* Convert DWORDs safely to WORDs */
5216 if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
5217 else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
5218 if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
5219 else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
5221 if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
5222 else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
5223 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
5224 else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
5226 desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
5228 desc7->dwReserved1 = 0;
5229 desc7->dwReserved2 = 0;
5230 desc7->dwReserved3 = 0;
5231 desc7->dwReserved4 = 0;
5233 /* Fill the old structure */
5234 memset(desc1, 0, sizeof(*desc1));
5235 desc1->dwSize = sizeof(D3DDEVICEDESC);
5236 desc1->dwFlags = D3DDD_COLORMODEL
5238 | D3DDD_TRANSFORMCAPS
5240 | D3DDD_LIGHTINGCAPS
5243 | D3DDD_DEVICERENDERBITDEPTH
5244 | D3DDD_DEVICEZBUFFERBITDEPTH
5245 | D3DDD_MAXBUFFERSIZE
5246 | D3DDD_MAXVERTEXCOUNT;
5248 desc1->dcmColorModel = D3DCOLOR_RGB;
5249 desc1->dwDevCaps = desc7->dwDevCaps;
5250 desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
5251 desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
5252 desc1->bClipping = TRUE;
5253 desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
5254 desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
5255 | D3DLIGHTCAPS_PARALLELPOINT
5256 | D3DLIGHTCAPS_POINT
5257 | D3DLIGHTCAPS_SPOT;
5259 desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
5260 desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
5262 desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
5263 desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
5264 desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
5265 desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
5266 desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
5267 desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
5268 desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
5269 desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
5270 desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
5271 desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
5272 desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
5273 desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
5274 desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
5276 desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
5277 desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
5278 desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
5279 desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
5280 desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
5281 desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
5282 desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
5283 desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
5284 desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
5285 desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
5286 desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
5287 desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
5288 desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
5290 desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
5291 desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
5292 desc1->dwMaxBufferSize = 0;
5293 desc1->dwMaxVertexCount = 65536;
5294 desc1->dwMinTextureWidth = desc7->dwMinTextureWidth;
5295 desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
5296 desc1->dwMaxTextureWidth = desc7->dwMaxTextureWidth;
5297 desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
5298 desc1->dwMinStippleWidth = 1;
5299 desc1->dwMinStippleHeight = 1;
5300 desc1->dwMaxStippleWidth = 32;
5301 desc1->dwMaxStippleHeight = 32;
5302 desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
5303 desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
5304 desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
5305 desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
5306 desc1->dvGuardBandRight = desc7->dvGuardBandRight;
5307 desc1->dvGuardBandTop = desc7->dvGuardBandTop;
5308 desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
5309 desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
5310 desc1->dwStencilCaps = desc7->dwStencilCaps;
5311 desc1->dwFVFCaps = desc7->dwFVFCaps;
5312 desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
5313 desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
5314 desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
5319 /*****************************************************************************
5320 * IDirectDraw7 VTable
5321 *****************************************************************************/
5322 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
5325 ddraw7_QueryInterface,
5330 ddraw7_CreateClipper,
5331 ddraw7_CreatePalette,
5332 ddraw7_CreateSurface,
5333 ddraw7_DuplicateSurface,
5334 ddraw7_EnumDisplayModes,
5335 ddraw7_EnumSurfaces,
5336 ddraw7_FlipToGDISurface,
5338 ddraw7_GetDisplayMode,
5339 ddraw7_GetFourCCCodes,
5340 ddraw7_GetGDISurface,
5341 ddraw7_GetMonitorFrequency,
5343 ddraw7_GetVerticalBlankStatus,
5345 ddraw7_RestoreDisplayMode,
5346 ddraw7_SetCooperativeLevel,
5347 ddraw7_SetDisplayMode,
5348 ddraw7_WaitForVerticalBlank,
5350 ddraw7_GetAvailableVidMem,
5352 ddraw7_GetSurfaceFromDC,
5354 ddraw7_RestoreAllSurfaces,
5355 ddraw7_TestCooperativeLevel,
5356 ddraw7_GetDeviceIdentifier,
5358 ddraw7_StartModeTest,
5362 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
5365 ddraw4_QueryInterface,
5370 ddraw4_CreateClipper,
5371 ddraw4_CreatePalette,
5372 ddraw4_CreateSurface,
5373 ddraw4_DuplicateSurface,
5374 ddraw4_EnumDisplayModes,
5375 ddraw4_EnumSurfaces,
5376 ddraw4_FlipToGDISurface,
5378 ddraw4_GetDisplayMode,
5379 ddraw4_GetFourCCCodes,
5380 ddraw4_GetGDISurface,
5381 ddraw4_GetMonitorFrequency,
5383 ddraw4_GetVerticalBlankStatus,
5385 ddraw4_RestoreDisplayMode,
5386 ddraw4_SetCooperativeLevel,
5387 ddraw4_SetDisplayMode,
5388 ddraw4_WaitForVerticalBlank,
5390 ddraw4_GetAvailableVidMem,
5392 ddraw4_GetSurfaceFromDC,
5394 ddraw4_RestoreAllSurfaces,
5395 ddraw4_TestCooperativeLevel,
5396 ddraw4_GetDeviceIdentifier,
5399 static const struct IDirectDraw3Vtbl ddraw3_vtbl =
5402 ddraw3_QueryInterface,
5407 ddraw3_CreateClipper,
5408 ddraw3_CreatePalette,
5409 ddraw3_CreateSurface,
5410 ddraw3_DuplicateSurface,
5411 ddraw3_EnumDisplayModes,
5412 ddraw3_EnumSurfaces,
5413 ddraw3_FlipToGDISurface,
5415 ddraw3_GetDisplayMode,
5416 ddraw3_GetFourCCCodes,
5417 ddraw3_GetGDISurface,
5418 ddraw3_GetMonitorFrequency,
5420 ddraw3_GetVerticalBlankStatus,
5422 ddraw3_RestoreDisplayMode,
5423 ddraw3_SetCooperativeLevel,
5424 ddraw3_SetDisplayMode,
5425 ddraw3_WaitForVerticalBlank,
5427 ddraw3_GetAvailableVidMem,
5429 ddraw3_GetSurfaceFromDC,
5432 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
5435 ddraw2_QueryInterface,
5440 ddraw2_CreateClipper,
5441 ddraw2_CreatePalette,
5442 ddraw2_CreateSurface,
5443 ddraw2_DuplicateSurface,
5444 ddraw2_EnumDisplayModes,
5445 ddraw2_EnumSurfaces,
5446 ddraw2_FlipToGDISurface,
5448 ddraw2_GetDisplayMode,
5449 ddraw2_GetFourCCCodes,
5450 ddraw2_GetGDISurface,
5451 ddraw2_GetMonitorFrequency,
5453 ddraw2_GetVerticalBlankStatus,
5455 ddraw2_RestoreDisplayMode,
5456 ddraw2_SetCooperativeLevel,
5457 ddraw2_SetDisplayMode,
5458 ddraw2_WaitForVerticalBlank,
5460 ddraw2_GetAvailableVidMem,
5463 static const struct IDirectDrawVtbl ddraw1_vtbl =
5466 ddraw1_QueryInterface,
5471 ddraw1_CreateClipper,
5472 ddraw1_CreatePalette,
5473 ddraw1_CreateSurface,
5474 ddraw1_DuplicateSurface,
5475 ddraw1_EnumDisplayModes,
5476 ddraw1_EnumSurfaces,
5477 ddraw1_FlipToGDISurface,
5479 ddraw1_GetDisplayMode,
5480 ddraw1_GetFourCCCodes,
5481 ddraw1_GetGDISurface,
5482 ddraw1_GetMonitorFrequency,
5484 ddraw1_GetVerticalBlankStatus,
5486 ddraw1_RestoreDisplayMode,
5487 ddraw1_SetCooperativeLevel,
5488 ddraw1_SetDisplayMode,
5489 ddraw1_WaitForVerticalBlank,
5492 static const struct IDirect3D7Vtbl d3d7_vtbl =
5494 /* IUnknown methods */
5495 d3d7_QueryInterface,
5498 /* IDirect3D7 methods */
5501 d3d7_CreateVertexBuffer,
5502 d3d7_EnumZBufferFormats,
5503 d3d7_EvictManagedTextures
5506 static const struct IDirect3D3Vtbl d3d3_vtbl =
5508 /* IUnknown methods */
5509 d3d3_QueryInterface,
5512 /* IDirect3D3 methods */
5515 d3d3_CreateMaterial,
5516 d3d3_CreateViewport,
5519 d3d3_CreateVertexBuffer,
5520 d3d3_EnumZBufferFormats,
5521 d3d3_EvictManagedTextures
5524 static const struct IDirect3D2Vtbl d3d2_vtbl =
5526 /* IUnknown methods */
5527 d3d2_QueryInterface,
5530 /* IDirect3D2 methods */
5533 d3d2_CreateMaterial,
5534 d3d2_CreateViewport,
5539 static const struct IDirect3DVtbl d3d1_vtbl =
5541 /* IUnknown methods */
5542 d3d1_QueryInterface,
5545 /* IDirect3D methods */
5549 d3d1_CreateMaterial,
5550 d3d1_CreateViewport,
5554 /*****************************************************************************
5557 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
5558 * if none was found.
5560 * This function is in ddraw.c and the DDraw object space because D3D7
5561 * vertex buffers are created using the IDirect3D interface to the ddraw
5562 * object, so they can be valid across D3D devices(theoretically. The ddraw
5563 * object also owns the wined3d device
5567 * fvf: Fvf to find the decl for
5570 * NULL in case of an error, the vertex declaration for the FVF otherwise.
5572 *****************************************************************************/
5573 struct wined3d_vertex_declaration *ddraw_find_decl(IDirectDrawImpl *This, DWORD fvf)
5575 struct wined3d_vertex_declaration *pDecl = NULL;
5577 int p, low, high; /* deliberately signed */
5578 struct FvfToDecl *convertedDecls = This->decls;
5580 TRACE("Searching for declaration for fvf %08x... ", fvf);
5583 high = This->numConvertedDecls - 1;
5584 while(low <= high) {
5585 p = (low + high) >> 1;
5587 if(convertedDecls[p].fvf == fvf) {
5588 TRACE("found %p\n", convertedDecls[p].decl);
5589 return convertedDecls[p].decl;
5590 } else if(convertedDecls[p].fvf < fvf) {
5596 TRACE("not found. Creating and inserting at position %d.\n", low);
5598 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
5599 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5600 if (hr != S_OK) return NULL;
5602 if(This->declArraySize == This->numConvertedDecls) {
5603 int grow = max(This->declArraySize / 2, 8);
5604 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5605 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5606 if (!convertedDecls)
5608 wined3d_vertex_declaration_decref(pDecl);
5611 This->decls = convertedDecls;
5612 This->declArraySize += grow;
5615 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5616 convertedDecls[low].decl = pDecl;
5617 convertedDecls[low].fvf = fvf;
5618 This->numConvertedDecls++;
5620 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5624 static inline struct IDirectDrawImpl *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
5626 return CONTAINING_RECORD(device_parent, struct IDirectDrawImpl, device_parent);
5629 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
5630 struct wined3d_device *device)
5632 TRACE("device_parent %p, device %p.\n", device_parent, device);
5635 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
5636 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5637 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, struct wined3d_surface **surface)
5639 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5640 IDirectDrawSurfaceImpl *surf = NULL;
5642 DDSCAPS2 searchcaps = ddraw->tex_root->surface_desc.ddsCaps;
5644 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
5645 "\tpool %#x, level %u, face %u, surface %p.\n",
5646 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
5648 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
5651 case WINED3DCUBEMAP_FACE_POSITIVE_X:
5652 TRACE("Asked for positive x\n");
5653 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5655 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
5657 surf = ddraw->tex_root; break;
5658 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
5659 TRACE("Asked for negative x\n");
5660 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
5661 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
5662 TRACE("Asked for positive y\n");
5663 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
5664 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
5665 TRACE("Asked for negative y\n");
5666 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
5667 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
5668 TRACE("Asked for positive z\n");
5669 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
5670 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
5671 TRACE("Asked for negative z\n");
5672 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
5673 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
5678 IDirectDrawSurface7 *attached;
5679 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)ddraw->tex_root, &searchcaps, &attached);
5680 surf = (IDirectDrawSurfaceImpl *)attached;
5681 IDirectDrawSurface7_Release(attached);
5683 if (!surf) ERR("root search surface not found\n");
5685 /* Find the wanted mipmap. There are enough mipmaps in the chain */
5688 IDirectDrawSurface7 *attached;
5689 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &searchcaps, &attached);
5690 if(!attached) ERR("Surface not found\n");
5691 surf = (IDirectDrawSurfaceImpl *)attached;
5692 IDirectDrawSurface7_Release(attached);
5696 /* Return the surface */
5697 *surface = surf->wined3d_surface;
5698 wined3d_surface_incref(*surface);
5700 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
5705 static HRESULT WINAPI findRenderTarget(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *ctx)
5707 IDirectDrawSurfaceImpl *s = (IDirectDrawSurfaceImpl *)surface;
5708 IDirectDrawSurfaceImpl **target = ctx;
5710 if (!s->isRenderTarget)
5713 IDirectDrawSurface7_Release(surface);
5714 return DDENUMRET_CANCEL;
5717 /* Recurse into the surface tree */
5718 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
5720 IDirectDrawSurface7_Release(surface);
5721 if (*target) return DDENUMRET_CANCEL;
5723 return DDENUMRET_OK;
5726 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
5727 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
5728 WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
5729 struct wined3d_surface **surface)
5731 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5732 IDirectDrawSurfaceImpl *d3d_surface = ddraw->d3d_target;
5733 IDirectDrawSurfaceImpl *target = NULL;
5735 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5736 "\tmultisample_quality %u, lockable %u, surface %p.\n",
5737 device_parent, container_parent, width, height, format, multisample_type,
5738 multisample_quality, lockable, surface);
5740 if (d3d_surface->isRenderTarget)
5742 IDirectDrawSurface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)d3d_surface, &target, findRenderTarget);
5746 target = d3d_surface;
5751 target = ddraw->d3d_target;
5752 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", ddraw);
5755 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
5757 *surface = target->wined3d_surface;
5758 wined3d_surface_incref(*surface);
5759 target->isRenderTarget = TRUE;
5761 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, d3d_surface);
5766 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
5767 UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
5768 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
5770 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5771 IDirectDrawSurfaceImpl *ddraw_surface;
5772 DDSURFACEDESC2 ddsd;
5775 TRACE("device_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5776 "\tmultisample_quality %u, discard %u, surface %p.\n",
5777 device_parent, width, height, format, multisample_type, multisample_quality, discard, surface);
5781 /* Create a DirectDraw surface */
5782 memset(&ddsd, 0, sizeof(ddsd));
5783 ddsd.dwSize = sizeof(ddsd);
5784 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
5785 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
5786 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5787 ddsd.dwHeight = height;
5788 ddsd.dwWidth = width;
5791 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, format);
5795 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
5798 ddraw->depthstencil = TRUE;
5799 hr = IDirectDraw7_CreateSurface(&ddraw->IDirectDraw7_iface, &ddsd,
5800 (IDirectDrawSurface7 **)&ddraw_surface, NULL);
5801 ddraw->depthstencil = FALSE;
5804 WARN("Failed to create depth/stencil surface, hr %#x.\n", hr);
5808 *surface = ddraw_surface->wined3d_surface;
5809 wined3d_surface_incref(*surface);
5810 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)ddraw_surface);
5815 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
5816 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5817 WINED3DPOOL pool, DWORD usage, struct wined3d_volume **volume)
5819 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
5820 "format %#x, pool %#x, usage %#x, volume %p.\n",
5821 device_parent, container_parent, width, height, depth,
5822 format, pool, usage, volume);
5824 ERR("Not implemented!\n");
5829 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
5830 WINED3DPRESENT_PARAMETERS *present_parameters, struct wined3d_swapchain **swapchain)
5832 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5833 IDirectDrawSurfaceImpl *iterator;
5836 TRACE("device_parent %p, present_parameters %p, swapchain %p.\n", device_parent, present_parameters, swapchain);
5838 hr = wined3d_swapchain_create(ddraw->wined3d_device, present_parameters,
5839 ddraw->ImplType, NULL, &ddraw_null_wined3d_parent_ops, swapchain);
5842 WARN("Failed to create swapchain, hr %#x.\n", hr);
5847 ddraw->d3d_target->wined3d_swapchain = *swapchain;
5848 iterator = ddraw->d3d_target->complex_array[0];
5851 iterator->wined3d_swapchain = *swapchain;
5852 iterator = iterator->complex_array[0];
5858 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5860 device_parent_wined3d_device_created,
5861 device_parent_create_surface,
5862 device_parent_create_rendertarget,
5863 device_parent_create_depth_stencil,
5864 device_parent_create_volume,
5865 device_parent_create_swapchain,
5868 HRESULT ddraw_init(IDirectDrawImpl *ddraw, WINED3DDEVTYPE device_type)
5873 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5874 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5875 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5876 ddraw->IDirectDraw3_iface.lpVtbl = &ddraw3_vtbl;
5877 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5878 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5879 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5880 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5881 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5882 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5883 ddraw->numIfaces = 1;
5886 /* See comments in IDirectDrawImpl_CreateNewSurface for a description of
5888 ddraw->ImplType = DefaultSurfaceType;
5890 /* Get the current screen settings. */
5892 ddraw->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
5894 ddraw->orig_width = GetSystemMetrics(SM_CXSCREEN);
5895 ddraw->orig_height = GetSystemMetrics(SM_CYSCREEN);
5897 ddraw->wineD3D = wined3d_create(7, &ddraw->IDirectDraw7_iface);
5898 if (!ddraw->wineD3D)
5900 WARN("Failed to create a wined3d object.\n");
5901 return E_OUTOFMEMORY;
5904 hr = wined3d_device_create(ddraw->wineD3D, WINED3DADAPTER_DEFAULT, device_type,
5905 NULL, 0, &ddraw->device_parent, &ddraw->wined3d_device);
5908 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5909 wined3d_decref(ddraw->wineD3D);
5913 /* Get the amount of video memory */
5914 ddraw->total_vidmem = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
5916 list_init(&ddraw->surface_list);